📄 dropdown.c
字号:
/*
*********************************************************************************************************
* uC/GUI V3.98
* 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 : DROPDOWN.c
Purpose : Implementation of dropdown widget
---------------------------END-OF-HEADER------------------------------
*/
#include "GUI_ARRAY.h"
#include <stdlib.h>
#include <string.h>
#include "DROPDOWN.h"
#include "DROPDOWN_Private.h"
#include "SCROLLBAR.h"
#include "WIDGET.h"
#include "GUIDebug.h"
#include "GUI_Protected.h"
#include "WM_Intern.h"
#include "LISTBOX.h"
#if GUI_WINSUPPORT
/*********************************************************************
*
* Private config defaults
*
**********************************************************************
*/
/* Define default fonts */
#ifndef DROPDOWN_FONT_DEFAULT
#if WIDGET_USE_SCHEME_SMALL
#define DROPDOWN_FONT_DEFAULT &GUI_Font13_1
#elif WIDGET_USE_SCHEME_MEDIUM
#define DROPDOWN_FONT_DEFAULT &GUI_Font16_1
#elif WIDGET_USE_SCHEME_LARGE
#define DROPDOWN_FONT_DEFAULT &GUI_Font24_1
#endif
#endif
/* Define colors */
#ifndef DROPDOWN_BKCOLOR0_DEFAULT
#define DROPDOWN_BKCOLOR0_DEFAULT GUI_WHITE /* Not selected */
#endif
#ifndef DROPDOWN_BKCOLOR1_DEFAULT
#define DROPDOWN_BKCOLOR1_DEFAULT GUI_GRAY /* Selected, no focus */
#endif
#ifndef DROPDOWN_BKCOLOR2_DEFAULT
#define DROPDOWN_BKCOLOR2_DEFAULT GUI_BLUE /* Selected, focus */
#endif
#ifndef DROPDOWN_TEXTCOLOR0_DEFAULT
#define DROPDOWN_TEXTCOLOR0_DEFAULT GUI_BLACK /* Not selected */
#endif
#ifndef DROPDOWN_TEXTCOLOR1_DEFAULT
#define DROPDOWN_TEXTCOLOR1_DEFAULT GUI_WHITE /* Selected, no focus */
#endif
#ifndef DROPDOWN_TEXTCOLOR2_DEFAULT
#define DROPDOWN_TEXTCOLOR2_DEFAULT GUI_WHITE /* Selected, focus */
#endif
#ifndef SCROLLBAR_COLOR_ARROW_DEFAULT
#define SCROLLBAR_COLOR_ARROW_DEFAULT GUI_BLACK /* Arrow color */
#endif
#ifndef SCROLLBAR_COLOR_BUTTON_DEFAULT
#define SCROLLBAR_COLOR_BUTTON_DEFAULT 0xc0c0c0 /* Button color */
#endif
#ifndef DROPDOWN_BORDER_DEFAULT
#define DROPDOWN_BORDER_DEFAULT 2
#endif
#ifndef DROPDOWN_ALIGN_DEFAULT
#define DROPDOWN_ALIGN_DEFAULT GUI_TA_LEFT /* Default text alignment */
#endif
#ifndef DROPDOWN_KEY_EXPAND
#define DROPDOWN_KEY_EXPAND GUI_KEY_SPACE
#endif
#ifndef DROPDOWN_KEY_SELECT
#define DROPDOWN_KEY_SELECT GUI_KEY_ENTER
#endif
/*********************************************************************
*
* Static data
*
**********************************************************************
*/
DROPDOWN_PROPS DROPDOWN__DefaultProps = {
DROPDOWN_FONT_DEFAULT,
DROPDOWN_BKCOLOR0_DEFAULT,
DROPDOWN_BKCOLOR1_DEFAULT,
DROPDOWN_BKCOLOR2_DEFAULT,
DROPDOWN_TEXTCOLOR0_DEFAULT,
DROPDOWN_TEXTCOLOR1_DEFAULT,
DROPDOWN_TEXTCOLOR2_DEFAULT,
SCROLLBAR_COLOR_ARROW_DEFAULT,
SCROLLBAR_COLOR_BUTTON_DEFAULT,
GUI_INVALID_COLOR,
GUI_INVALID_COLOR,
GUI_INVALID_COLOR,
DROPDOWN_BORDER_DEFAULT,
DROPDOWN_ALIGN_DEFAULT
};
/*********************************************************************
*
* Static routines
*
**********************************************************************
*/
/*********************************************************************
*
* DROPDOWN__GetNumItems
Returns:
Number of fully or partially visible items
*/
int DROPDOWN__GetNumItems(DROPDOWN_Obj* pObj) {
return pObj->Handles.NumItems;
}
/*********************************************************************
*
* _GethItem
Returns:
Handle of the specified item
*/
static WM_HMEM _GethItem(DROPDOWN_Obj* pObj, int Index) {
return GUI_ARRAY_GethItem(&pObj->Handles, Index);
}
/*********************************************************************
*
* _DrawTriangleDown
*/
static void _DrawTriangleDown(int x, int y, int Size) {
for (; Size >= 0; Size--, y++ ) {
GUI_DrawHLine(y, x - Size, x + Size);
}
}
/*********************************************************************
*
* _GetpItem
Returns:
Pointer to the specified item
*/
static const char* _GetpItem(DROPDOWN_Obj* pObj, int Index) {
const char* s = NULL;
WM_HMEM h = _GethItem(pObj, Index);
if (h) {
s = (const char*) GUI_ALLOC_h2p(h);
}
return s;
}
/*********************************************************************
*
* _Tolower
*/
static int _Tolower(int Key) {
if ((Key >= 0x41) && (Key <= 0x5a)) {
Key += 0x20;
}
return Key;
}
/*********************************************************************
*
* _SelectByKey
*/
static void _SelectByKey(DROPDOWN_Handle hObj, int Key) {
int i;
DROPDOWN_Obj* pObj;
pObj = DROPDOWN_H2P(hObj);
Key = _Tolower(Key);
for (i = 0; i < DROPDOWN__GetNumItems(pObj); i++) {
char c = _Tolower(*_GetpItem(pObj, i));
if (c == Key) {
DROPDOWN_SetSel(hObj, i);
break;
}
}
}
/*********************************************************************
*
* _FreeAttached
*/
static void _FreeAttached(DROPDOWN_Obj* pObj) {
GUI_ARRAY_Delete(&pObj->Handles);
WM_DeleteWindow(pObj->hListWin);
}
/*********************************************************************
*
* _Paint
*/
static void _Paint(DROPDOWN_Handle hObj) {
int Border;
GUI_RECT r;
const char* s;
int InnerSize, ColorIndex;
DROPDOWN_Obj* pObj;
int TextBorderSize;
/* Do some initial calculations */
pObj = DROPDOWN_H2P(hObj);
Border = pObj->Widget.pEffect->EffectSize;
TextBorderSize = pObj->Props.TextBorderSize;
GUI_SetFont(pObj->Props.pFont);
ColorIndex = (pObj->Widget.State & WIDGET_STATE_FOCUS) ? 2 : 1;
s = _GetpItem(pObj, pObj->Sel);
WM_GetClientRect(&r);
GUI__ReduceRect(&r, &r, Border);
InnerSize = r.y1 - r.y0 + 1;
/* Draw the 3D effect (if configured) */
WIDGET__EFFECT_DrawDown(&pObj->Widget);
/* Draw the outer text frames */
r.x1 -= InnerSize; /* Spare square area to the right */
LCD_SetColor(pObj->Props.aBackColor[ColorIndex]);
/* Draw the text */
LCD_SetBkColor(pObj->Props.aBackColor[ColorIndex]);
GUI_FillRectEx(&r);
r.x0 += TextBorderSize;
r.x1 -= TextBorderSize;
LCD_SetColor (pObj->Props.aTextColor[ColorIndex]);
GUI_DispStringInRect(s, &r, pObj->Props.Align);
/* Draw arrow */
WM_GetClientRect(&r);
GUI__ReduceRect(&r, &r, Border);
r.x0 = r.x1 + 1 - InnerSize;
LCD_SetColor(pObj->Props.aColor[DROPDOWN_CI_BUTTON]);
GUI_FillRectEx(&r);
LCD_SetColor(pObj->Props.aColor[DROPDOWN_CI_ARROW]);
_DrawTriangleDown((r.x1 + r.x0) / 2, r.y0 + 5, (r.y1 - r.y0 - 8) / 2);
WIDGET__EFFECT_DrawUpRect(&pObj->Widget, &r);
}
/*********************************************************************
*
* _OnTouch
*/
static int _OnTouch(DROPDOWN_Handle hObj, WM_MESSAGE*pMsg) {
const GUI_PID_STATE* pState = (const GUI_PID_STATE*)pMsg->Data.p;
if (pMsg->Data.p) { /* Something happened in our area (pressed or released) */
if (pState->Pressed) {
WM_NotifyParent(hObj, WM_NOTIFICATION_CLICKED);
} else {
WM_NotifyParent(hObj, WM_NOTIFICATION_RELEASED);
}
} else { /* Mouse moved out */
WM_NotifyParent(hObj, WM_NOTIFICATION_MOVED_OUT);
}
return 0; /* Message handled */
}
/*********************************************************************
*
* Private routines
*
**********************************************************************
*/
/*********************************************************************
*
* DROPDOWN_h2p
*/
#if GUI_DEBUG_LEVEL >= GUI_DEBUG_LEVEL_CHECK_ALL
DROPDOWN_Obj * DROPDOWN_h2p(DROPDOWN_Handle h) {
DROPDOWN_Obj * p = (DROPDOWN_Obj *)GUI_ALLOC_h2p(h);
if (p) {
if (p->DebugId != DROPDOWN_ID) {
GUI_DEBUG_ERROROUT("DROPDOWN.c: Wrong handle type or Object not init'ed");
return 0;
}
}
return p;
}
#endif
/*********************************************************************
*
* DROPDOWN__AdjustHeight
*/
void DROPDOWN__AdjustHeight(DROPDOWN_Handle hObj, DROPDOWN_Obj* pObj) {
int Height;
Height = pObj->TextHeight;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -