CHOOSECOLOR (Sample)

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

This sample demonstrates the usage of CHOOSECOLOR dialogs.

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        : CHOOSECOLOR_Usage.c
Purpose     : Sample that demonstrates the usage of CHOOSECOLOR
              dialogues in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

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

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
static const GUI_COLOR _aColors[] = {
  GUI_BLUE,
  GUI_GREEN,
  GUI_RED,
  GUI_CYAN,
  GUI_MAGENTA,
  GUI_YELLOW,
  GUI_LIGHTBLUE,
  GUI_LIGHTGREEN,
  GUI_LIGHTRED,
  GUI_LIGHTCYAN,
  GUI_LIGHTMAGENTA,
  GUI_LIGHTYELLOW,
  GUI_DARKBLUE,
  GUI_DARKGREEN,
  GUI_DARKRED,
  GUI_DARKCYAN,
  GUI_DARKMAGENTA,
  GUI_DARKYELLOW,
  GUI_WHITE,
  GUI_LIGHTGRAY,
  GUI_GRAY,
  GUI_DARKGRAY,
  GUI_BROWN,
  GUI_ORANGE
};

static const char * _acColorNames[] = {
  "blue",
  "green",
  "red",
  "cyan",
  "magenta",
  "yellow",
  "light blue",
  "light green",
  "light red",
  "light cyan",
  "light magenta",
  "light yellow",
  "dark blue",
  "dark green",
  "dark red",
  "dark cyan",
  "dark magenta",
  "dark yellow",
  "white",
  "light gray",
  "gray",
  "dark gray",
  "brown",
  "orange"
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbWin
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  int            NCode;
  char           acBuffer[128];
  static int     Sel;
  static WM_HWIN hChoosecolor;

  switch (pMsg->MsgId) {
  case WM_CREATE:
    Sel = -1;
    //
    // Create choosecolor dialogue
    // We also add a framewin flag to make the dialogue movable.
    //
    hChoosecolor = CHOOSECOLOR_Create(pMsg->hWin, 10, 10, 200, 150, _aColors, GUI_COUNTOF(_aColors), 3, 0, "Choose a color", FRAMEWIN_CF_MOVEABLE);
    break;
  case WM_PAINT:
    GUI_SetBkColor(GUI_BLACK);
    GUI_Clear();
    //
    // Do something with the selection
    //
    GUI_SetColor(_aColors[Sel]);
    GUI_SetFont(&GUI_Font16B_1);
    if (Sel > -1) {
      sprintf(acBuffer, "You selected the color %s with the index %d", _acColorNames[Sel], Sel);
      GUI_DispStringAt(acBuffer, 10, 10);
      sprintf(acBuffer, "The hexadecimal display of the color is: 0x%X", _aColors[Sel]);
      GUI_DispStringAt(acBuffer, 10, 30);
    }
    break;
  case WM_NOTIFY_PARENT:
    NCode = pMsg->Data.v;
    //
    // When the ok button of the dialogue is pressed, it gets deleted.
    // Therefore, a CHILD_DELETED notification is sent to the parent window.
    //
    if (NCode == WM_NOTIFICATION_CHILD_DELETED) {
      //
      // Check if the deleted window is our dialogue
      //
      if (pMsg->hWinSrc == hChoosecolor) {
        Sel = CHOOSECOLOR_GetSel(hChoosecolor);
        WM_InvalidateWindow(pMsg->hWin);
      }
    }
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  //
  // Init GUI
  //
  GUI_Init();
  //
  // Enable multi-buffering to avoid flickering during movement of a framewin.
  //
  WM_MULTIBUF_Enable(1);
  //
  // Create a window
  //
  WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);

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

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