WM messages (Sample)

From SEGGER Wiki
Jump to: navigation, search
WM_AllMessages.c
File(s) required
  • WM_AllMessages.c
Runs in simulation Yes
Runs on target Yes
Download WM_AllMessages.c

This sample lists all pre-defined messages sent to windows by the Window Manager. The code shows how the message's data can be accessed, if data is available. The sample's purpose is not to be executed, rather its code can be taken as reference.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : WM_AllMessages.c
Purpose     : Sample that shows all window messages that can be used.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/
#include "WM.h"  // This includes almost every required header

#define ID_CHILD_0  (GUI_ID_USER + 0x01)

/*********************************************************************
*
*       _cbWin
*
*  Function description
*    This function should be used as template for a custom callback.
*    It can be used for all windows and widgets but in most cases not
*    all messages will occur.
*
*  Parameters
*    pMsg - A WM_MESSAGE structure which contains besides the message
*           itself information about the window who's the receiver of
*           the message. Depending on the message, further information
*           can be extracted via data values and/or pointers.
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  int            NCode;
  int            Id;
  int            Key;
  GUI_RECT     * pRect;
  WM_KEY_INFO  * pKeyInfo;
  WM_MOVE_INFO * pMoveInfo;
  WM_HTIMER      hTimer;

  switch (pMsg->MsgId) {
  case WM_CREATE:              // Sent after window has been created
    WM_DefaultProc(pMsg);
    break;
  case WM_DELETE:              // Sent before window gets deleted
    WM_DefaultProc(pMsg);
    break;
  case WM_GET_ACCEPT_FOCUS:    // Sent to determin if the window is able to receive input focus
    WM_DefaultProc(pMsg);
    break;
  case WM_GET_ID:              // Sent for requesting ID
    WM_DefaultProc(pMsg);
    break;
  case WM_GET_INSIDE_RECT:     // Sent to a window to get the client rectangle without the effect size
    pRect = (GUI_RECT *)pMsg->Data.p;
    break;
  case WM_INIT_DIALOG:         // Sent to a dialog window after creation (dialog only)
    WM_DefaultProc(pMsg);
    break;
  case WM_KEY:                 // Sent if a key has been pressed
    pKeyInfo = (WM_KEY_INFO *)pMsg->Data.p; // Data pointer containing key info struct
    Key      = pKeyInfo->Key;
    if (pKeyInfo->PressedCnt == 0) {
      // Key has been released
    } else {
      // Key has been pressed
    }
    break;
  case WM_MOTION:              // Sent to a window to achieve advanced motion support
    WM_DefaultProc(pMsg);
    break;
  case WM_MOUSEOVER:           // Sent to a window if PID is over window, only if mouse support is enabled
    WM_DefaultProc(pMsg);
    break;
  case WM_MOUSEOVER_END:       // Sent to a window if PID leaves window area, only if mouse support is enabled
    WM_DefaultProc(pMsg);
    break;
  case WM_MOVE:                // Sent to a window after it has been moved
    pMoveInfo = (WM_MOVE_INFO *)pMsg->Data.p;
    pMoveInfo->dx; // Difference between old and new position on the x-axis.
    pMoveInfo->dy; // Difference between old and new position on the y-axis.
    break;
  case WM_NOTIFY_PARENT:       // Sent if something has occured in a child window
    Id = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch (Id) {
    case ID_CHILD_0:           // Notifications sent by 'Child' with ID = ID_CHILD_0
      switch (NCode) {
      case WM_NOTIFICATION_CHILD_DELETED:    // Sent to a window before a child window gets deleted
        break;
      case WM_NOTIFICATION_CLICKED:          // Sent to a window if a child has been clicked
        break;
      case WM_NOTIFICATION_GOT_FOCUS:        // Sent to a window if a child gains and accepts the focus
        break;
      case WM_NOTIFICATION_LOST_FOCUS:       // Sent to a window if a child loses the focus
        break;
      case WM_NOTIFICATION_MOVED_OUT:        // Sent to a window if the PID input moves out the window while it is pressed
        break;
      case WM_NOTIFICATION_RELEASED:         // Sent to a window if a clicked child has been released
        break;
      case WM_NOTIFICATION_SCROLL_CHANGED:   // Sent to a window when the scrollposition of an attached scrollbar has changed
        break;
      case WM_NOTIFICATION_SCROLLBAR_ADDED:  // Sent to a window if a scrollbar gets attached
        break;
      case WM_NOTIFICATION_SEL_CHANGED:      // Sent to a window if the selection of a child has changed (e.g. LISTBOX)
        break;
      case WM_NOTIFICATION_VALUE_CHANGED:    // Sent to a window if the value of child has changed (e.g. SPINBOX)
        break;
      }
      break;
    }
    WM_DefaultProc(pMsg);
    break;
  case WM_NOTIFY_VIS_CHANGED:  // Sent to a window if its visibility has been changed
    WM_DefaultProc(pMsg);
    break;
  case WM_PAINT:               // Sent to a window after it has become invalid and it should be redrawn
    //
    // Data pointer points to a GUI_RECT struct containing the invalid rectangle in screen coordinates.
    //
    pRect = (GUI_RECT *)pMsg->Data.p; 

    WM_DefaultProc(pMsg);      // Handle default drawing (not necessary)
    GUI_SetBkColor(GUI_BLUE);  // Set a background color
    GUI_Clear();               // Clear the window 
    break;
  case WM_POST_PAINT:          // Sent to a window after the last WM_PAINT message
    WM_DefaultProc(pMsg);
    break;
  case WM_PRE_PAINT:           // Sent to a window before the first WM_PAINT message
    WM_DefaultProc(pMsg);
    break;
  case WM_SET_CALLBACK:        // Sent to a window after a callback has been set
    WM_DefaultProc(pMsg);
    break;
  case WM_SET_FOCUS:           // Sent to window if it gains or loses the focus
    if (pMsg->Data.v == 1) { // Window gains input focus
      // Do something
    } else if (pMsg->Data.v == 0) { // Window has lost input focus
      // Do something
    }
    break;
  case WM_SET_ID:              // Sent to a window to change the window ID
    Id = pMsg->Data.v; // Data value contains new window ID
    break;
  case WM_SIZE:                // Sent to a window after its size has changed
    WM_DefaultProc(pMsg);
    break;
  case WM_TIMER:               // Sent to a window after a timer has expired
    hTimer = pMsg->Data.v; // Data value contains handle of the timer that expired
    break;
  case WM_USER_DATA:           // Sent to a window after user data has been set
    WM_DefaultProc(pMsg);
    break;
  case WM_PID_STATE_CHANGED:   // Sent to a window if the pressed state has changed
    WM_DefaultProc(pMsg);
    break;
  case WM_TOUCH:               // Sent to a window if a PID input is pressed, pressed
    WM_DefaultProc(pMsg);      // and moved or released in the window area
    break;
  case WM_TOUCH_CHILD:         // Sent to a window if a child window has been touch by PID
    WM_DefaultProc(pMsg);
    break;
  case WM_USER:                // User defined message, usally as 'WM_USER + X' where X is an integer value
    WM_DefaultProc(pMsg);
    break;
  default:                     // Any messages not handled above are sent to the default callback
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  GUI_Init();
  WM_CreateWindow(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_CF_SHOW, _cbWin, 0);
  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/