WM SetUserData (Sample)

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

This sample demonstrates how user data can be attached to a window. User data are additional bytes that are allocated upon the window's creation to hold a value or data, e.g. a pointer.

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_SetUserData.c
Purpose     : Sample that shows how to set user data to windows or
              widgets.
Notes       : User data are extra bytes that are allocated upon
              creation of the window or widget. These bytes can then
              be used to easily transfer data between windows.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"
#include <stdio.h>

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

/*********************************************************************
*
*       _cbWindow
*/
static void _cbWindow(WM_MESSAGE * pMsg) {
  char         acBuffer[32];
  static int * pData;

  switch (pMsg->MsgId) {
  case WM_PAINT:
    GUI_SetBkColor(GUI_RED);
    GUI_Clear();
    //
    // Check if we got a valid pointer and process the data.
    //
    if (pData) {
      GUI_SetColor(GUI_WHITE);
      sprintf(acBuffer, "Counter (child window): %d", *pData);
      GUI_DispString(acBuffer);
    }
    break;
  case WM_USER_DATA:
    //
    // Save user data that the window received.
    //
    WM_GetUserData(pMsg->hWin, &pData, sizeof(int *));
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       _cbBk
*/
static void _cbBk(WM_MESSAGE * pMsg) {
  WM_HWIN          hWin;
  char             acBuffer[32];
  int            * pCounter;
  static WM_HTIMER hTimer;
  static int       Counter;
  
  switch (pMsg->MsgId) {
  case WM_CREATE:
    //
    // Create child window with extra bytes for the size of a pointer to an int.
    //
    hWin = WM_CreateWindowAsChild(30, 30, 200, 50, pMsg->hWin, WM_CF_SHOW, _cbWindow, sizeof(int *));
    //
    // Point to address of counter and set the address of the pointer as user data.
    //
    pCounter = &Counter;
    WM_SetUserData(hWin, &pCounter, sizeof(int *));
    //
    // Create a timer.
    //
    hTimer = WM_CreateTimer(pMsg->hWin, 0, 0, 0);
    break;
  case WM_PAINT:
    GUI_SetBkColor(GUI_BLUE);
    GUI_Clear();
    //
    // Display counter.
    //
    GUI_SetColor(GUI_WHITE);
    sprintf(acBuffer, "Counter (parent window): %d", Counter);
    GUI_DispString(acBuffer);
    break;
  case WM_TIMER:
    //
    // Restart timer.
    //
    Counter++;
    WM_InvalidateWindowAndDescs(pMsg->hWin);
    WM_RestartTimer(hTimer, 500);
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  WM_HWIN hBk;
  
  //
  // Init GUI.
  //
  GUI_Init();
  //
  // Create parent window.
  //
  hBk = WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbBk, 0);

  while (1) {
    GUI_Delay(100);
  }
}

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