WM Focusable Window (Sample)

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

Normally, emWin windows do not receive input focus. This sample demonstrates how a window can be realized, that is able to receive input focus.

Demo

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_FocusableWindow.c
Purpose     : Sample that demonstrates how a window can accept focus.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
----------------------------------------------------------------------
*/

#include "DIALOG.h"

/*********************************************************************
*
*       Externals
*
**********************************************************************
*/

/*********************************************************************
*
*       Types
*
**********************************************************************
*/

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_WIN0  (GUI_ID_USER + 0x00)

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbWin
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  WM_PID_STATE_CHANGED_INFO * pInfo;
  WM_MESSAGE                  Message;
  static int                  Id;

  switch (pMsg->MsgId) {
  case WM_PAINT:
    GUI_SetBkColor(GUI_RED);
    GUI_Clear();
    break;
  case WM_SET_ID:
    Id = pMsg->Data.v;  // Remember Id
    break;
  case WM_GET_ID:
    pMsg->Data.v = Id;  // Return Id
    break;
  case WM_SET_FOCUS:
    //
    // If pMsg->Data.v is 1 the window manager tries to set the focus
    //
    if (pMsg->Data.v == 1) {
      pMsg->Data.v   = 0;
      Message.Data.v = WM_NOTIFICATION_GOT_FOCUS;  // Prepare get-focus message
    } else if (pMsg->Data.v == 0) {
      Message.Data.v = WM_NOTIFICATION_LOST_FOCUS; // Prepare lost-focus message
    }
    Message.MsgId   = WM_NOTIFY_PARENT;
    Message.hWin    = WM_GetParent(pMsg->hWin);
    Message.hWinSrc = pMsg->hWin;
    WM_SendMessage(Message.hWin, &Message);        // Send message to parent
    break;
  case WM_PID_STATE_CHANGED:
    //
    // Check how pid state has changed
    //
    pInfo = (WM_PID_STATE_CHANGED_INFO *)pMsg->Data.p;
    if (pInfo) {
      //
      // If previous state is 0 and current is 1 we have a click
      //
      if ((pInfo->State == 1) && (pInfo->StatePrev == 0)) {
        //
        // On click set focus to this window
        //
        WM_SetFocus(pMsg->hWin);
      }
    }
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       _cbBk
*/
static void _cbBk(WM_MESSAGE * pMsg) {
  int Id;
  int NCode;
  WM_HWIN hItem;

  switch (pMsg->MsgId) {
  case WM_PAINT:
    GUI_SetBkColor(GUI_BLACK);
    GUI_Clear();
    break;
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch (Id) {
    case GUI_ID_EDIT0:
      switch (NCode) {
      case WM_NOTIFICATION_GOT_FOCUS:
        hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_TEXT0);
        TEXT_SetText(hItem, "Enter: EDIT0");
        break;
      case WM_NOTIFICATION_LOST_FOCUS:
        hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_TEXT1);
        TEXT_SetText(hItem, "Leave: EDIT0");
        break;
      }
      break;                                           
    case GUI_ID_EDIT1:
      switch (NCode) {
      case WM_NOTIFICATION_GOT_FOCUS:
        hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_TEXT0);
        TEXT_SetText(hItem, "Enter: EDIT1");
        break;
      case WM_NOTIFICATION_LOST_FOCUS:
        hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_TEXT1);
        TEXT_SetText(hItem, "Leave: EDIT1");
        break;
      }
      break;
    case GUI_ID_TEXT2:
      switch (NCode) {
      case WM_NOTIFICATION_GOT_FOCUS:
        hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_TEXT0);
        TEXT_SetText(hItem, "Enter: TEXT2");
        break;
      case WM_NOTIFICATION_LOST_FOCUS:
        hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_TEXT1);
        TEXT_SetText(hItem, "Leave: TEXT2");
        break;
      }
      break;
    case ID_WIN0:
      switch (NCode) {
      case WM_NOTIFICATION_GOT_FOCUS:
        hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_TEXT0);
        TEXT_SetText(hItem, "Enter: WIN0");
        break;
      case WM_NOTIFICATION_LOST_FOCUS:
        hItem = WM_GetDialogItem(pMsg->hWin, GUI_ID_TEXT1);
        TEXT_SetText(hItem, "Leave: WIN0");
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  WM_HWIN hItem;

  GUI_Init();
  WM_MULTIBUF_Enable(1);
  WM_SetCallback(WM_HBKWIN, _cbBk);
  //
  // Create widgets
  //
  EDIT_CreateEx(10, 10, 80, 20, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_EDIT0, 20);
  EDIT_CreateEx(10, 40, 80, 20, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_EDIT1, 20);
  hItem = TEXT_CreateEx(10, 70, 80, 20, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_TEXT2, "Text");
  TEXT_SetTextColor(hItem, GUI_WHITE);
  //
  // Create window and teach it to manage focus
  //
  hItem = WM_CreateWindowAsChild(10, 100, 80, 20, WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);
  WM_SetId(hItem, ID_WIN0);
  //
  // Show leave and enter messages
  //
  hItem = TEXT_CreateEx(160, 10, 120, 20, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_TEXT0, "Enter: ");
  TEXT_SetTextColor(hItem, GUI_WHITE);
  hItem = TEXT_CreateEx(160, 40, 120, 20, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_TEXT1, "Leave: ");
  TEXT_SetTextColor(hItem, GUI_WHITE);
  while (1) {
    GUI_Delay(100);
  }
}

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