LISTBOX - Custom (Sample)

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

This sample demonstrates custom drawing 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_Custom.c
Purpose     : Sample that demonstrates how to give LISTBOX widgets
              in emWin a custom appearance using an OwnerDraw routine.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

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

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

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _OwnerDrawListbox
*/
static int _OwnerDrawListbox(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo) {
  int  Sel;
  char acBuffer[32];

  switch (pDrawItemInfo->Cmd) {
  case WIDGET_ITEM_DRAW:
    if (pDrawItemInfo->ItemIndex > -1) { // -1 is an invalid selection
      //
      // Draw background
      //
      Sel = LISTBOX_GetSel(pDrawItemInfo->hWin);
      if (pDrawItemInfo->ItemIndex == Sel) {
        GUI_SetColor(GUI_LIGHTBLUE);
      } else {
        GUI_SetColor(GUI_WHITE);
      }
      GUI_FillRect(pDrawItemInfo->x0, pDrawItemInfo->y0, pDrawItemInfo->x1, pDrawItemInfo->y1);
      //
      // Draw item text
      //
      LISTBOX_GetItemText(pDrawItemInfo->hWin, pDrawItemInfo->ItemIndex, acBuffer, sizeof(acBuffer));
      GUI_SetTextMode(GUI_TM_TRANS);
      if (pDrawItemInfo->ItemIndex == Sel) {
        GUI_SetColor(GUI_WHITE);
      } else {
        GUI_SetColor(GUI_BLACK);
      }
      GUI_DispStringAt(acBuffer, pDrawItemInfo->x0 + 5, pDrawItemInfo->y0);
    }
    return 0;
  default:
    return LISTBOX_OwnerDraw(pDrawItemInfo);
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  LISTBOX_Handle hListbox;

  //
  // Init GUI
  //
  GUI_Init();
  //
  // Create listbox widget
  //
  hListbox = LISTBOX_CreateEx(10, 10, 120, 80, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_LISTBOX0, acContent);
  LISTBOX_SetAutoScrollV(hListbox, 1);
  //
  // Set OwnerDraw function
  //
  LISTBOX_SetOwnerDraw(hListbox, _OwnerDrawListbox);

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

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