Create dialogs (Sample)

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

This sample shows how custom dialogs can be created in emWin.

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        : DIALOG_CreateDialogs.c
Purpose     : Sample that demonstrates how to create a custom dialog
              in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define DIALOG_X_SIZE  320
#define DIALOG_Y_SIZE  240
#define ID_WINDOW_0    (GUI_ID_USER + 0x00)
#define ID_BUTTON_0    (GUI_ID_USER + 0x01)
#define ID_EDIT_0      (GUI_ID_USER + 0x02)
#define ID_TEXT_0      (GUI_ID_USER + 0x03)
#define ID_TEXT_1      (GUI_ID_USER + 0x04)
#define ID_TEXT_2      (GUI_ID_USER + 0x05)

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
/*********************************************************************
*
*       _aDialogCreate
*
*  Description: To create a dialog, we first need a resource table
*               that specifies all widgets to be included into the
*               dialog.
*/
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
  { WINDOW_CreateIndirect, "Window",     ID_WINDOW_0, 0,   0,  DIALOG_X_SIZE, DIALOG_Y_SIZE, 0, 0x0,  0 },
  { BUTTON_CreateIndirect, "Set Text",   ID_BUTTON_0, 150, 50, 80,            20,            0, 0x0,  0 },
  { EDIT_CreateIndirect,   "Text",       ID_EDIT_0,   20,  50, 120,           20,            0, 0x64, 0 },
  { TEXT_CreateIndirect,   "Header",     ID_TEXT_0,   20,  20, 200,           20,            0, 0x0,  0 },
  { TEXT_CreateIndirect,   "Your text:", ID_TEXT_1,   20,  90, 80,            20,            0, 0x0,  0 },
  { TEXT_CreateIndirect,   "",           ID_TEXT_2,   80,  90, 80,            20,            0, 0x0,  0 },
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbDialog
*
*  Function description:
*    Callback used for the dialog.
*/
static void _cbDialog(WM_MESSAGE * pMsg) {
  char    acBuffer[256];
  WM_HWIN hItem;
  int     NCode;
  int     Id;

  switch (pMsg->MsgId) {
  case WM_INIT_DIALOG:
    //
    // Init dialog items
    //
    hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
    TEXT_SetText(hItem, "Custom Dialog Sample");
    TEXT_SetTextAlign(hItem, GUI_TA_LEFT);
    TEXT_SetFont(hItem, &GUI_Font20B_1);
    hItem = WM_GetDialogItem(hItem, ID_EDIT_0);
    EDIT_SetMaxLen(hItem, sizeof(acBuffer) - 1);
    hItem = WM_GetDialogItem(hItem, ID_TEXT_1);
    TEXT_SetFont(hItem, &GUI_Font13B_1);
    break;
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch(Id) {
    case ID_BUTTON_0:
      switch(NCode) {
      case WM_NOTIFICATION_RELEASED:
        //
        // Retrieve text from edit widget
        //
        hItem = WM_GetDialogItem(pMsg->hWin, ID_EDIT_0);
        EDIT_GetText(hItem, acBuffer, sizeof(acBuffer));
        //
        // Set text widget
        //
        hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_2);
        TEXT_SetText(hItem, acBuffer);
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

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

  //
  // Init GUI
  //
  GUI_Init();
  //
  // Create dialog
  //
  hDialog = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);

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

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