📄 listbox.c
字号:
}
}
}
/* Display item */
LCD_SetBkColor(pObj->Props.aBackColor[ColorIndex]);
LCD_SetColor (pObj->Props.aTextColor[ColorIndex]);
s = LISTBOX__GetpString(pObj, ItemIndex);
GUI_SetTextMode(GUI_TM_TRANS);
GUI_Clear();
GUI_DispStringAt(s, pDrawItemInfo->x0 + 1, pDrawItemInfo->y0);
/* Display focus rectangle */
if ((pObj->Flags & LISTBOX_SF_MULTISEL) && (ItemIndex == pObj->Sel)) {
GUI_RECT rFocus;
rFocus.x0 = pDrawItemInfo->x0;
rFocus.y0 = pDrawItemInfo->y0;
rFocus.x1 = r.x1;
rFocus.y1 = pDrawItemInfo->y0 + FontDistY - 1;
LCD_SetColor(GUI_WHITE - pObj->Props.aBackColor[ColorIndex]);
GUI_DrawFocusRect(&rFocus, 0);
}
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;
}
/*********************************************************************
*
* 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) {
pItem->xSize = 0;
pItem->ySize = 0;
}
}
/*********************************************************************
*
* LISTBOX__InvalidateInsideArea
*/
void LISTBOX__InvalidateInsideArea(LISTBOX_Handle hObj) {
GUI_RECT Rect;
WM_GetInsideRectExScrollbar(hObj, &Rect);
WM_InvalidateRect(hObj, &Rect);
}
/*********************************************************************
*
* LISTBOX__InvalidateItem
*/
void LISTBOX__InvalidateItem(LISTBOX_Handle hObj, const LISTBOX_Obj* pObj, int Sel) {
if (Sel >= 0) {
int ItemPosY;
ItemPosY = _GetItemPosY(hObj, pObj, Sel);
if (ItemPosY >= 0) {
GUI_RECT Rect;
int ItemDistY;
ItemDistY = _GetItemSizeY(hObj, pObj, Sel);
WM_GetInsideRectExScrollbar(hObj, &Rect);
Rect.y0 += ItemPosY;
Rect.y1 = Rect.y0 + ItemDistY - 1;
WM_InvalidateRect(hObj, &Rect);
}
}
}
/*********************************************************************
*
* LISTBOX__InvalidateItemAndBelow
*/
void LISTBOX__InvalidateItemAndBelow(LISTBOX_Handle hObj, const LISTBOX_Obj* pObj, int Sel) {
if (Sel >= 0) {
int ItemPosY;
ItemPosY = _GetItemPosY(hObj, pObj, Sel);
if (ItemPosY >= 0) {
GUI_RECT Rect;
WM_GetInsideRectExScrollbar(hObj, &Rect);
Rect.y0 += ItemPosY;
WM_InvalidateRect(hObj, &Rect);
}
}
}
/*********************************************************************
*
* LISTBOX__SetScrollbarWidth
*/
void LISTBOX__SetScrollbarWidth(LISTBOX_Handle hObj, const LISTBOX_Obj* pObj) {
WM_HWIN hBarH, hBarV;
int Width;
Width = pObj->ScrollbarWidth;
if (Width == 0) {
Width = SCROLLBAR_GetDefaultWidth();
}
hBarH = WM_GetDialogItem(hObj, GUI_ID_HSCROLL);
hBarV = WM_GetDialogItem(hObj, GUI_ID_VSCROLL);
SCROLLBAR_SetWidth(hBarH, Width);
SCROLLBAR_SetWidth(hBarV, Width);
}
/*********************************************************************
*
* _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_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
/*********************************************************************
*
* _LISTBOX_Callback
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -