OwnerDraw (Sample)

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

This sample demonstrates how OwnerDraw routines can be set and used for certain widgets.

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        : WIDGET_OwnerDraw.c
Purpose     : Sample that demonstrates how to implement an OwnerDraw
              routine to give the widget a custom look.
Notes       : Not all widgets support OwnerDraw functions.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

/*********************************************************************
*
*       _acItems: Items used for the listbox widget
*/
static const char * _acItems[] = {
  "Item 1",
  "Item 2",
  "Item 3",
  "Item 4",
  "Item 5"
};

/*********************************************************************
*
*       _GetItemSizeX
*/
static int _GetItemSizeX(WM_HWIN hWin, int ItemIndex) {
  char acBuffer[128];
  int  DistX;

  LISTBOX_GetItemText(hWin, ItemIndex, acBuffer, sizeof(acBuffer));
  DistX = GUI_GetStringDistX(acBuffer);
  return DistX;
}

/*********************************************************************
*
*       _GetItemSizeY
*/
static int _GetItemSizeY(WM_HWIN hWin, int ItemIndex) {
  return GUI_GetFontDistY() + 1;
}

/*********************************************************************
*
*       _OwnerDraw
*/
static int _OwnerDraw(const WIDGET_ITEM_DRAW_INFO * pDrawItemInfo) {
  char acBuffer[128];
  int  FontSize;
  int  ItemSize;
  int  xSize;

  switch (pDrawItemInfo->Cmd) {
  case WIDGET_ITEM_DRAW:
    //
    // Draw background of item
    //
    GUI_DrawGradientV(pDrawItemInfo->x0, pDrawItemInfo->y0, pDrawItemInfo->x1, pDrawItemInfo->y1, GUI_MAKE_COLOR(0x00FAF7F5), GUI_MAKE_COLOR(0x00DBC6B8));
    if (pDrawItemInfo->ItemIndex == LISTBOX_GetSel(pDrawItemInfo->hWin)) {
      GUI_SetColor(GUI_RED);
    } else {
      GUI_SetColor(GUI_BLACK);
    }
    GUI_DrawRect(pDrawItemInfo->x0, pDrawItemInfo->y0, pDrawItemInfo->x1, pDrawItemInfo->y1);
    //
    // Draw item text
    //
    LISTBOX_GetItemText(pDrawItemInfo->hWin, pDrawItemInfo->ItemIndex, acBuffer, sizeof(acBuffer));
    GUI_SetFont(&GUI_Font13B_1);
    GUI_SetTextMode(GUI_TM_TRANS);
    xSize = _GetItemSizeX(pDrawItemInfo->hWin, pDrawItemInfo->ItemIndex);
    FontSize = GUI_GetFontDistY();
    ItemSize = _GetItemSizeY(pDrawItemInfo->hWin, pDrawItemInfo->ItemIndex);
    GUI_DispStringAt(acBuffer, (pDrawItemInfo->x1 - pDrawItemInfo->x0) / 2 - (xSize / 2), pDrawItemInfo->y0 + (ItemSize - FontSize) / 2);
    break;
  case WIDGET_ITEM_GET_XSIZE:
    return _GetItemSizeX(pDrawItemInfo->hWin, pDrawItemInfo->ItemIndex);
  case WIDGET_ITEM_GET_YSIZE:
    return _GetItemSizeY(pDrawItemInfo->hWin, pDrawItemInfo->ItemIndex);
  default:
    return LISTBOX_OwnerDraw(pDrawItemInfo);
  }
  return 0;
}

/*********************************************************************
*
*       _cbWin
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  WM_HWIN hListbox;

  switch (pMsg->MsgId) {
  case WM_CREATE:
    //
    // Create a widget that supports OwnerDraw functions
    //
    hListbox = LISTBOX_Create(_acItems, 10, 10, 80, 72, WM_CF_SHOW);
    //
    // Set OwnerDraw routine
    //
    LISTBOX_SetOwnerDraw(hListbox, _OwnerDraw);
    break;
  case WM_PAINT:
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  GUI_Init();

  //
  // Create window
  //
  WM_CreateWindow(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_CF_SHOW, _cbWin, 0);

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

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