LISTBOX - Usage (Sample)

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

This sample demonstrates the usage of a LISTBOX widget.

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

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

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
/*********************************************************************
*
*       _acContent
*/
static const char * _acContent[] = {
  "English",
  "Deutsch",
  "Espanol",
  "Francais",
  "Italiano",
  "Dansk",
  "Eesti",
  "Hrvatski",
  "Portugues",
  "Suomi",
  NULL
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbWin
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  char                  acBuffer[32];
  int                   Sel;
  int                   NCode, Id;
  static LISTBOX_Handle hListbox;

  switch (pMsg->MsgId) {
  case WM_CREATE:
    //
    // Create listbox widget
    //
    hListbox = LISTBOX_CreateEx(10, 10, 120, 80, pMsg->hWin, WM_CF_SHOW, 0, GUI_ID_LISTBOX0, _acContent);
    //
    // Add string to the end of the list
    //
    LISTBOX_AddString(hListbox, "Esperanto");
    //
    // Insert new string at position 5
    //
    LISTBOX_InsertString(hListbox, "Nederlands", 5);
    //
    // Change some of the appearance of the widget
    //
    LISTBOX_SetFont(hListbox, &GUI_Font16_1);
    LISTBOX_SetBkColor(hListbox, LISTBOX_CI_SELFOCUS, GUI_DARKBLUE);
    //
    // Enable motion scrolling
    //
    LISTBOX_EnableMotion(hListbox, 1);
    //
    // Manually set the selection to 3
    //
    LISTBOX_SetSel(hListbox, 3);
    break;
  case WM_PAINT:
    GUI_SetBkColor(GUI_BLACK);
    GUI_Clear();
    //
    // Get current selection
    // -1 means no selection
    //
    Sel = LISTBOX_GetSel(hListbox);
    if (Sel > -1) {
      sprintf(acBuffer, "The current selection is: %d", Sel);
    } else {
      sprintf(acBuffer, "No item is currently selected.");
    }
    GUI_DispStringAt(acBuffer, 150, 10);
    break;
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch(Id) {
    case GUI_ID_LISTBOX0:
      switch (NCode) {
      case WM_NOTIFICATION_SEL_CHANGED:
        //
        // Redraw parent window when selection has changed.
        //
        WM_InvalidateWindow(pMsg->hWin);
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  //
  // Init GUI
  //
  GUI_Init();
  //
  // Enable multi-buffering
  //
  WM_MULTIBUF_Enable(1);
  //
  // Create window
  //
  WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);

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

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