📄 listbox.c
字号:
/*
*********************************************************************************************************
* uC/GUI
* Universal graphic software for embedded applications
*
* (c) Copyright 2002, Micrium Inc., Weston, FL
* (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
* 礐/GUI 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 redistributed
* in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File : LISTBOX.c
Purpose : Multiple choice for emWin GSC
---------------------------END-OF-HEADER------------------------------
*/
#include <stdlib.h>
#include <string.h>
#include "LISTBOX.h"
#include "SCROLLBAR.h"
#include "WIDGET.h"
#include "GUIDebug.h"
#include "GUI_Protected.h"
#include "WM_Intern.h"
#if GUI_WINSUPPORT
/*********************************************************************
*
* Private config defaults
*
**********************************************************************
*/
/* Support for 3D effects */
#ifndef LISTBOX_USE_3D
#define LISTBOX_USE_3D 1
#endif
/* Define default fonts */
#ifndef LISTBOX_FONT_DEFAULT
#define LISTBOX_FONT_DEFAULT &GUI_Font13_1
#endif
/* Define colors */
#ifndef LISTBOX_BKCOLOR0_DEFAULT
#define LISTBOX_BKCOLOR0_DEFAULT GUI_WHITE /* Not selected */
#endif
#ifndef LISTBOX_BKCOLOR1_DEFAULT
#define LISTBOX_BKCOLOR1_DEFAULT GUI_GRAY /* Selected, no focus */
#endif
#ifndef LISTBOX_BKCOLOR2_DEFAULT
#define LISTBOX_BKCOLOR2_DEFAULT GUI_BLUE /* Selected, focus */
#endif
#ifndef LISTBOX_TEXTCOLOR0_DEFAULT
#define LISTBOX_TEXTCOLOR0_DEFAULT GUI_BLACK /* Not selected */
#endif
#ifndef LISTBOX_TEXTCOLOR1_DEFAULT
#define LISTBOX_TEXTCOLOR1_DEFAULT GUI_WHITE /* Selected, no focus */
#endif
#ifndef LISTBOX_TEXTCOLOR2_DEFAULT
#define LISTBOX_TEXTCOLOR2_DEFAULT GUI_WHITE /* Selected, focus */
#endif
/*********************************************************************
*
* Object definition
*
**********************************************************************
*/
typedef struct {
WIDGET Widget;
I16 Sel; /* current selection */
I16 NumItems;
WM_HMEM haHandle; /* Handle to buffer holding handles */
WM_SCROLL_STATE ScrollState;
const GUI_FONT* pFont;
GUI_COLOR aBackColor[3];
GUI_COLOR aTextColor[3];
#if GUI_DEBUG_LEVEL >1
int DebugId;
#endif
} LISTBOX_Obj;
/*********************************************************************
*
* Static data
*
**********************************************************************
*/
static const GUI_FONT* _pDefaultFont = LISTBOX_FONT_DEFAULT;
/*********************************************************************
*
* Macros for internal use
*
**********************************************************************
*/
#define LISTBOX_H2P(h) (LISTBOX_Obj*) WM_HMEM2Ptr(h)
#if GUI_DEBUG_LEVEL >1
#define OBJECT_ID 0x4C69 /* Magic numer, should be unique if possible */
#define ASSERT_IS_VALID_PTR(p) GUI_DEBUG_ERROROUT_IF(p->DebugId != OBJECT_ID, "EDIT.C: Wrong handle type or Object not init'ed")
#define INIT_ID(p) p->DebugId = OBJECT_ID
#define DEINIT_ID(p) p->DebugId = 0
#else
#define ASSERT_IS_VALID_PTR(p)
#define INIT_ID(p)
#define DEINIT_ID(p)
#endif
/*********************************************************************
*
* Static routines
*
**********************************************************************
*/
/*********************************************************************
*
* _GetNumVisItems
Returns:
Number of fully or partially visible items
*/
static int _GetNumVisItems(LISTBOX_Obj* pObj) {
int FontDist = GUI_GetYDistOfFont(pObj->pFont);
int ysize = WM__GetWindowSizeY(&pObj->Widget.Win);
ysize -= 2* pObj->Widget.pEffect->EffectSize;
if (FontDist)
return ysize / FontDist;
return 1;
}
/*********************************************************************
*
* _GetNumItems
Returns:
Number of fully or partially visible items
*/
static int _GetNumItems(LISTBOX_Obj* pObj) {
return pObj->NumItems;
}
/*********************************************************************
*
* _GethItem
Returns:
Handle of the specified item
*/
static WM_HMEM _GethItem(LISTBOX_Obj* pObj, int Index) {
WM_HMEM h = 0;
if (Index < pObj->NumItems) {
WM_HMEM ha;
WM_HMEM* pa;
ha = pObj->haHandle;
if (ha) {
pa = (WM_HMEM*) WM_HMEM2Ptr(ha);
h = *(pa + Index);
}
}
return h;
}
/*********************************************************************
*
* _GetpItem
Returns:
Pointer to the specified item
*/
static const char* _GetpItem(LISTBOX_Obj* pObj, int Index) {
const char* s = NULL;
WM_HMEM h = _GethItem(pObj, Index);
if (h) {
s = (const char*) WM_HMEM2Ptr(h);
}
return s;
}
static void _SetScrollState(WM_HWIN hWin) {
LISTBOX_Obj* pObj = LISTBOX_H2P(hWin);
WIDGET__SetScrollState(hWin, &pObj->ScrollState, NULL);
}
static void _CalcScrollParas(WM_HWIN hWin) {
LISTBOX_Obj* pObj = LISTBOX_H2P(hWin);
pObj->ScrollState.NumItems = _GetNumItems(pObj);
pObj->ScrollState.PageSize = _GetNumVisItems(pObj);
}
static void _SetScroll(LISTBOX_Handle hObj, LISTBOX_Obj* pObj, int iScroll) {
pObj->ScrollState.v = iScroll;
_SetScrollState(hObj);
}
static void _CheckSel(LISTBOX_Handle hObj) {
int Sel;
LISTBOX_Obj* pObj = LISTBOX_H2P(hObj);
Sel = pObj->Sel;
/* Check if we have to scroll up */
if (Sel < pObj->ScrollState.v) {
_SetScroll(hObj, pObj, Sel);
} else {
/* Check if we have to scroll down */
if (Sel > pObj->ScrollState.v + pObj->ScrollState.PageSize -1) {
_SetScroll(hObj, pObj, Sel - (pObj->ScrollState.PageSize -1));
}
}
}
static int _Tolower(int Key) {
if ((Key >= 0x41) && (Key <= 0x5a)) {
Key += 0x20;
}
return Key;
}
static void _SelectByKey(LISTBOX_Handle hObj, int Key) {
int i;
LISTBOX_Obj* pObj;
pObj = LISTBOX_H2P(hObj);
Key = _Tolower(Key);
for (i = 0; i < _GetNumItems(pObj); i++) {
char c = _Tolower(*_GetpItem(pObj, i));
if (c == Key) {
LISTBOX_SetSel(hObj, i);
break;
}
}
}
/*********************************************************************
*
* _Delete
*/
static void _FreeAttached(LISTBOX_Obj* pObj) {
int i;
WM_HMEM ha;
WM_HMEM* pa;
ha = pObj->haHandle;
if (ha) {
pa = (WM_HMEM*) WM_HMEM2Ptr(ha);
/* Fre the attached strings, one at a time */
for (i = 0; i < pObj->NumItems; i++) {
WM_FREEPTR(pa+i);
}
/* Free the handle buffer */
WM_FREEPTR(&pObj->haHandle);
}
}
/*********************************************************************
*
* _Paint
*/
static void _Paint(LISTBOX_Handle hObj) {
int i;
int Border;
GUI_RECT r;
int FontDistY;
LISTBOX_Obj* pObj;
int NumItems;
pObj = LISTBOX_H2P(hObj);
NumItems = _GetNumItems(pObj);
Border = pObj->Widget.pEffect->EffectSize;
GUI_SetFont(pObj->pFont);
FontDistY = GUI_GetFontDistY();
if (Border) {
GUI_SetBkColor(pObj->aBackColor[0]);
GUI_Clear();
}
/* Calculate rect used for painting (subtract border) */
WM_GetClientRect(&r);
r.x1 -= Border;
r.y1 -= Border;
r.y0 -= Border;
WM_SetUserClipArea(&r);
for (i = pObj->ScrollState.v; i < NumItems; i++) {
const char* s;
int y, ColorIndex;
y = Border + (i - pObj->ScrollState.v) * FontDistY;
s = _GetpItem(pObj, i);
if (i == pObj->Sel) {
ColorIndex = (pObj->Widget.State & WIDGET_STATE_FOCUS) ? 2 : 1;
} else {
ColorIndex = 0;
}
GUI_SetBkColor(pObj->aBackColor[ColorIndex]);
GUI_SetColor (pObj->aTextColor[ColorIndex]);
GUI_ClearRect(Border, y, Border, y + FontDistY -1);
GUI_DispStringAt(s, Border+1, y);
GUI_DispCEOL();
}
WM_SetUserClipArea(NULL);
/* Draw the 3D effect (if configured) */
WIDGET__EFFECT_DrawDown(&pObj->Widget);
}
/*********************************************************************
*
* _OnTouch
*/
static int _OnTouch(LISTBOX_Handle hObj, LISTBOX_Obj* pObj, WM_MESSAGE*pMsg) {
int Notification;
int Sel;
GUI_TOUCH_tState* pState = (GUI_TOUCH_tState*)pMsg->Data.p;
int FontDistY = GUI_GetYDistOfFont(pObj->pFont);
if (pMsg->Data.p) { /* Something happened in our area (pressed or released) */
if (pState->Pressed) {
Sel = pState->y / FontDistY + pObj->ScrollState.v;
WM_SetFocus(hObj);
Notification = WM_NOTIFICATION_CLICKED;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -