/*********************************************************************
*                    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        : GUI_Tutorial_AddingUserData.c
Purpose     : Code of the wiki tutorial on adding user data to windows
              in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"
#include <stdio.h>

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbChild
*/
static void _cbChild(WM_MESSAGE * pMsg) {
  static int * pCounter;
  char         acBuffer[64];
  
  switch(pMsg->MsgId) {
  case WM_PAINT:
    GUI_SetBkColor(GUI_RED);
    GUI_Clear();
    //
    // Check if pointer is valid.
    //
    if (pCounter) {
      GUI_SetColor(GUI_WHITE);
      sprintf(acBuffer, "Counter: %d", *pCounter);
      GUI_DispStringAt(acBuffer, 10, 10);
    }
    break;
  case WM_USER_DATA:
    //
    // Get user data of this window and save it at the address of our pointer.
    //
    WM_GetUserData(pMsg->hWin, &pCounter, sizeof(int *));
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
*
*       _cbParent
*/
static void _cbParent(WM_MESSAGE * pMsg) {
  static int       Counter;
  static int     * pCounter;
  static WM_HWIN   hChild;
  WM_HWIN          hItem;
  int              Id, NCode;
  char             acBuffer[64];

  switch(pMsg->MsgId) {
  case WM_CREATE:
    hItem = BUTTON_CreateEx(10, 30, 100, 20, pMsg->hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON0);
    BUTTON_SetText(hItem, "Increase Counter");
    //
    // Create child window with the size of an int pointer as extra bytes.
    //
    hChild = WM_CreateWindowAsChild(10, 70, 150, 100, pMsg->hWin, WM_CF_SHOW, _cbChild, sizeof(int *));
    //
    // Save the address of the counter in a pointer variable.
    //
    pCounter = &Counter;
    //
    // Set the user data to child window.
    // The address of the pointer is the source of the user data.
    //
    WM_SetUserData(hChild, &pCounter, sizeof(int *));
    break;
  case WM_PAINT:
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    //
    // Display counter.
    //
    GUI_SetColor(GUI_BLACK);
    GUI_SetFont(&GUI_Font16B_1);
    sprintf(acBuffer, "Counter: %d", Counter);
    GUI_DispStringAt(acBuffer, 10, 10);
    break;
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch(Id) {
    case GUI_ID_BUTTON0:
      switch(NCode) {
      case WM_NOTIFICATION_RELEASED:
        //
        // Increase counter when button is pressed and redraw window.
        //
        Counter++;
        WM_InvalidateWindow(pMsg->hWin);
        //
        // Also redraw the child window.
        //
        WM_InvalidateWindow(hChild);
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  GUI_Init();
  //
  // Create parent window.
  //
  WM_CreateWindow(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_CF_SHOW, _cbParent, 0);

  while(1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/