📄 listbox.c
字号:
return 0;
}
}
return 0;
}
/*********************************************************************
*
* _UpdateScrollPos
*
* Purpose:
* Checks whether if we must scroll up or scroll down to ensure
* that selection is in the visible area. This function also
* makes sure that scroll positions are in valid ranges.
*
* Return value:
* Difference between old and new vertical scroll pos.
*/
static int _UpdateScrollPos(LISTBOX_Handle hObj, LISTBOX_Obj* pObj) {
int PrevScrollStateV;
PrevScrollStateV = pObj->ScrollStateV.v;
if (pObj->Sel >= 0) {
/* Check upper limit */
if (_IsPartiallyVis(hObj, pObj)) {
pObj->ScrollStateV.v = pObj->Sel - (pObj->ScrollStateV.PageSize - 1);
}
/* Check lower limit */
if (pObj->Sel < pObj->ScrollStateV.v) {
pObj->ScrollStateV.v = pObj->Sel;
}
}
WM_CheckScrollBounds(&pObj->ScrollStateV);
WM_CheckScrollBounds(&pObj->ScrollStateH);
WIDGET__SetScrollState(hObj, &pObj->ScrollStateV, &pObj->ScrollStateH);
return pObj->ScrollStateV.v - PrevScrollStateV;
}
/*********************************************************************
*
* _CalcScrollParas
*/
static int _CalcScrollParas(LISTBOX_Handle hObj) {
GUI_RECT Rect;
LISTBOX_Obj* pObj = LISTBOX_H2P(hObj);
/* Calc vertical scroll parameters */
pObj->ScrollStateV.NumItems = LISTBOX__GetNumItems(pObj);
pObj->ScrollStateV.PageSize = _GetNumVisItems(pObj, hObj);
/* Calc horizontal scroll parameters */
WM_GetInsideRectExScrollbar(hObj, &Rect);
pObj->ScrollStateH.NumItems = _GetContentsSizeX(hObj);
pObj->ScrollStateH.PageSize = Rect.x1 - Rect.x0 + 1;
return _UpdateScrollPos(hObj, pObj);
}
/*********************************************************************
*
* _ManageAutoScroll
*/
static void _ManageAutoScroll(LISTBOX_Handle hObj) {
char IsRequired;
LISTBOX_Obj* pObj = LISTBOX_H2P(hObj);
if (pObj->Flags & LISTBOX_SF_AUTOSCROLLBAR_V) {
IsRequired = (_GetNumVisItems(pObj, hObj) < LISTBOX__GetNumItems(pObj));
WM_SetScrollbarV(hObj, IsRequired);
}
if (pObj->Flags & LISTBOX_SF_AUTOSCROLLBAR_H) {
GUI_RECT Rect;
int xSize, xSizeContents;
xSizeContents = _GetContentsSizeX(hObj);
WM_GetInsideRectExScrollbar(hObj, &Rect);
xSize = Rect.x1 - Rect.x0 + 1;
IsRequired = (xSizeContents > xSize);
WM_SetScrollbarH(hObj, IsRequired);
}
if (pObj->ScrollbarWidth) {
LISTBOX__SetScrollbarWidth(hObj, pObj);
}
LISTBOX__SetScrollbarColor(hObj, pObj);
}
/*********************************************************************
*
* LISTBOX_UpdateScrollers
*/
int LISTBOX_UpdateScrollers(LISTBOX_Handle hObj) {
_ManageAutoScroll(hObj);
return _CalcScrollParas(hObj);
}
/*********************************************************************
*
* _Tolower
*/
static int _Tolower(int Key) {
if ((Key >= 0x41) && (Key <= 0x5a)) {
Key += 0x20;
}
return Key;
}
/*********************************************************************
*
* _IsAlphaNum
*/
static int _IsAlphaNum(int Key) {
Key = _Tolower(Key);
if (Key >= 'a' && Key <= 'z') {
return 1;
}
if (Key >= '0' && Key <= '9') {
return 1;
}
return 0;
}
/*********************************************************************
*
* _SelectByKey
*/
static void _SelectByKey(LISTBOX_Handle hObj, int Key) {
unsigned i;
LISTBOX_Obj* pObj;
pObj = LISTBOX_H2P(hObj);
Key = _Tolower(Key);
for (i = 0; i < LISTBOX__GetNumItems(pObj); i++) {
const char* s = LISTBOX__GetpString(pObj, i);
if (_Tolower(*s) == Key) {
LISTBOX_SetSel(hObj, i);
break;
}
}
}
/*********************************************************************
*
* _FreeAttached
*/
static void _FreeAttached(LISTBOX_Obj* pObj) {
GUI_ARRAY_Delete(&pObj->ItemArray);
}
/*********************************************************************
*
* _OnPaint
*/
static void _OnPaint(LISTBOX_Handle hObj, LISTBOX_Obj* pObj, WM_MESSAGE* pMsg) {
WIDGET_ITEM_DRAW_INFO ItemInfo;
GUI_RECT RectInside, RectItem, ClipRect;
int ItemDistY, NumItems, i;
NumItems = LISTBOX__GetNumItems(pObj);
GUI_SetFont(pObj->Props.pFont);
/* Calculate clipping rectangle */
ClipRect = *(const GUI_RECT*)pMsg->Data.p;
GUI_MoveRect(&ClipRect, -pObj->Widget.Win.Rect.x0, -pObj->Widget.Win.Rect.y0);
WM_GetInsideRectExScrollbar(hObj, &RectInside);
GUI__IntersectRect(&ClipRect, &RectInside);
RectItem.x0 = ClipRect.x0;
RectItem.x1 = ClipRect.x1;
/* Fill item info structure */
ItemInfo.Cmd = WIDGET_ITEM_DRAW;
ItemInfo.hWin = hObj;
ItemInfo.x0 = RectInside.x0 - pObj->ScrollStateH.v;
ItemInfo.y0 = RectInside.y0;
/* Do the drawing */
for (i = pObj->ScrollStateV.v; i < NumItems; i++) {
RectItem.y0 = ItemInfo.y0;
/* Break when all other rows are outside the drawing area */
if (RectItem.y0 > ClipRect.y1) {
break;
}
ItemDistY = _GetItemSizeY(hObj, pObj, i);
RectItem.y1 = RectItem.y0 + ItemDistY - 1;
/* Make sure that we draw only when row is in drawing area */
if (RectItem.y1 >= ClipRect.y0) {
/* Set user clip rect */
WM_SetUserClipArea(&RectItem);
/* Fill item info structure */
ItemInfo.ItemIndex = i;
/* Draw item */
if (pObj->pfDrawItem) {
pObj->pfDrawItem(&ItemInfo);
} else {
LISTBOX_OwnerDraw(&ItemInfo);
}
}
ItemInfo.y0 += ItemDistY;
}
WM_SetUserClipArea(NULL);
/* Calculate & clear 'data free' area */
RectItem.y0 = ItemInfo.y0;
RectItem.y1 = RectInside.y1;
LCD_SetBkColor(pObj->Props.aBackColor[0]);
GUI_ClearRectEx(&RectItem);
/* Draw the 3D effect (if configured) */
WIDGET__EFFECT_DrawDown(&pObj->Widget);
}
/*********************************************************************
*
* _ToggleMultiSel
*/
static void _ToggleMultiSel(LISTBOX_Handle hObj, LISTBOX_Obj* pObj, int Sel) {
if (pObj->Flags & LISTBOX_SF_MULTISEL) {
WM_HMEM hItem = GUI_ARRAY_GethItem(&pObj->ItemArray, Sel);
if (hItem) {
LISTBOX_ITEM * pItem = (LISTBOX_ITEM *)GUI_ALLOC_h2p(hItem);
if (!(pItem->Status & LISTBOX_ITEM_DISABLED)) {
pItem->Status ^= LISTBOX_ITEM_SELECTED;
_NotifyOwner(hObj, WM_NOTIFICATION_SEL_CHANGED);
LISTBOX__InvalidateItem(hObj, pObj, Sel);
}
}
}
}
/*********************************************************************
*
* _GetItemFromPos
*/
static int _GetItemFromPos(LISTBOX_Handle hObj, LISTBOX_Obj* pObj, int x, int y) {
int Sel = -1;
GUI_RECT Rect;
WM_GetInsideRectExScrollbar(hObj, &Rect);
if ((x >= Rect.x0) && (y >= Rect.y0)) {
if ((x <= Rect.x1) && (y <= Rect.y1)) {
int NumItems = LISTBOX__GetNumItems(pObj);
int i, y0 = Rect.y0;
for (i = pObj->ScrollStateV.v; i < NumItems; i++) {
if (y >= y0) {
Sel = i;
}
y0 += _GetItemSizeY(hObj, pObj, i);
}
}
}
return Sel;
}
/*********************************************************************
*
* _OnTouch
*/
static void _OnTouch(LISTBOX_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 == 0) {
_NotifyOwner(hObj, WM_NOTIFICATION_RELEASED);
}
} else { /* Mouse moved out */
_NotifyOwner(hObj, WM_NOTIFICATION_MOVED_OUT);
}
}
/*********************************************************************
*
* _OnMouseOver
*/
#if GUI_SUPPORT_MOUSE
static int _OnMouseOver(LISTBOX_Handle hObj, LISTBOX_Obj* pObj, WM_MESSAGE* pMsg) {
const GUI_PID_STATE* pState = (const GUI_PID_STATE*)pMsg->Data.p;
if (pObj->hOwner) {
if (pState) { /* Something happened in our area (pressed or released) */
int Sel;
Sel = _GetItemFromPos(hObj, pObj, pState->x, pState->y);
if (Sel >= 0) {
if (Sel < (int)(pObj->ScrollStateV.v + _GetNumVisItems(pObj, hObj))) {
LISTBOX_SetSel(hObj, Sel);
}
}
}
}
return 0; /* Message handled */
}
#endif
/*********************************************************************
*
* _MoveSel
*
* Moves the selection/focus to the next valid item
*/
static void _MoveSel(LISTBOX_Handle hObj, int Dir) {
int Index, NewSel = -1, NumItems;
LISTBOX_Obj * pObj;
pObj = LISTBOX_H2P(hObj);
Index = LISTBOX_GetSel(hObj);
NumItems = LISTBOX__GetNumItems(pObj);
do {
WM_HMEM hItem;
Index += Dir;
if ((Index < 0) || (Index >= NumItems)) {
break;
}
hItem = GUI_ARRAY_GethItem(&pObj->ItemArray, Index);
if (hItem) {
LISTBOX_ITEM * pItem = (LISTBOX_ITEM *)GUI_ALLOC_h2p(hItem);
if (!(pItem->Status & LISTBOX_ITEM_DISABLED)) {
NewSel = Index;
}
}
} while(NewSel < 0);
if (NewSel >= 0) {
LISTBOX_SetSel(hObj, NewSel);
} else {
int PrevScrollStateV;
PrevScrollStateV = pObj->ScrollStateV.v;
pObj->ScrollStateV.v += Dir;
WM_CheckScrollBounds(&pObj->ScrollStateV);
if (PrevScrollStateV != pObj->ScrollStateV.v) {
LISTBOX__InvalidateInsideArea(hObj);
}
LISTBOX_UpdateScrollers(hObj);
}
}
/*********************************************************************
*
* _AddKey
*
* Returns: 1 if Key has been consumed
* 0 else
*/
static int _AddKey(LISTBOX_Handle hObj, int Key) {
LISTBOX_Obj* pObj;
pObj = LISTBOX_H2P(hObj);
switch (Key) {
case GUI_KEY_SPACE:
_ToggleMultiSel(hObj, pObj, pObj->Sel);
return 1; /* Key has been consumed */
case GUI_KEY_RIGHT:
if (WM_SetScrollValue(&pObj->ScrollStateH, pObj->ScrollStateH.v + pObj->Props.ScrollStepH)) {
LISTBOX_UpdateScrollers(hObj);
LISTBOX__InvalidateInsideArea(hObj);
}
return 1; /* Key has been consumed */
case GUI_KEY_LEFT:
if (WM_SetScrollValue(&pObj->ScrollStateH, pObj->ScrollStateH.v - pObj->Props.ScrollStepH)) {
LISTBOX_UpdateScrollers(hObj);
LISTBOX__InvalidateInsideArea(hObj);
}
return 1; /* Key has been consumed */
case GUI_KEY_DOWN:
LISTBOX_IncSel(hObj);
return 1; /* Key has been consumed */
case GUI_KEY_UP:
LISTBOX_DecSel(hObj);
return 1; /* Key has been consumed */
default:
if(_IsAlphaNum(Key)) {
_SelectByKey(hObj, Key);
return 1; /* Key has been consumed */
}
}
return 0;
}
/*********************************************************************
*
* Private routines
*
**********************************************************************
*/
/*********************************************************************
*
* LISTBOX_h2p
*/
#if GUI_DEBUG_LEVEL >= GUI_DEBUG_LEVEL_CHECK_ALL
LISTBOX_Obj * LISTBOX_h2p(LISTBOX_Handle h) {
LISTBOX_Obj * p = (LISTBOX_Obj *)GUI_ALLOC_h2p(h);
if (p) {
if (p->DebugId != LISTBOX_ID) {
GUI_DEBUG_ERROROUT("LISTBOX.c: Wrong handle type or Object not init'ed");
return 0;
}
}
return p;
}
#endif
/*********************************************************************
*
* LISTBOX__GetNumItems
*
* Returns:
* Number of items
*/
unsigned LISTBOX__GetNumItems(const LISTBOX_Obj* pObj) {
return GUI_ARRAY_GetNumItems(&pObj->ItemArray);
}
/*********************************************************************
*
* LISTBOX__GetpString
*
* Returns:
* Pointer to the specified item
*/
const char* LISTBOX__GetpString(const LISTBOX_Obj* pObj, int Index) {
const char* s = NULL;
LISTBOX_ITEM* pItem = (LISTBOX_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
if (pItem) {
s = pItem->acText;
}
return s;
}
/*********************************************************************
*
* LISTBOX__InvalidateItemSize
*/
void LISTBOX__InvalidateItemSize(const LISTBOX_Obj* pObj, unsigned Index) {
LISTBOX_ITEM* pItem;
pItem = (LISTBOX_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
if (pItem) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -