📄 dropdown.c
字号:
if (!Height) {
Height = GUI_GetYDistOfFont(pObj->Props.pFont);
}
Height += pObj->Widget.pEffect->EffectSize + 2 * pObj->Props.TextBorderSize;
WM_SetSize(hObj, WM__GetWindowSizeX(&pObj->Widget.Win), Height);
}
/*********************************************************************
*
* Exported routines: Callback
*
**********************************************************************
*/
/*********************************************************************
*
* DROPDOWN_Callback
*/
void DROPDOWN_Callback (WM_MESSAGE*pMsg) {
DROPDOWN_Handle hObj;
DROPDOWN_Obj* pObj;
char IsExpandedBeforeMsg;
hObj = pMsg->hWin;
pObj = (DROPDOWN_Obj *)GUI_ALLOC_h2p(hObj); /* Don't use use WIDGET_H2P because WIDGET_INIT_ID() has not be called at this point */
IsExpandedBeforeMsg = pObj->hListWin ? 1 : 0;
/* Let widget handle the standard messages */
if (WIDGET_HandleActive(hObj, pMsg) == 0) {
return;
}
switch (pMsg->MsgId) {
case WM_NOTIFY_OWNER_KEY:
/* Close the listbox if DROPDOWN_KEY_SELECT has been pressed */
if (((const WM_KEY_INFO*)(pMsg->Data.p))->PressedCnt > 0) {
int Key;
Key = ((const WM_KEY_INFO*)(pMsg->Data.p))->Key;
if (Key == DROPDOWN_KEY_SELECT) {
DROPDOWN_SetSel(hObj, LISTBOX_GetSel(pObj->hListWin));
DROPDOWN_Collapse(hObj);
WM_SetFocus(hObj);
}
}
break;
case WM_NOTIFY_PARENT:
switch (pMsg->Data.v) {
case WM_NOTIFICATION_SCROLL_CHANGED:
WM_NotifyParent(hObj, WM_NOTIFICATION_SCROLL_CHANGED);
break;
case WM_NOTIFICATION_CLICKED:
DROPDOWN_SetSel(hObj, LISTBOX_GetSel(pObj->hListWin));
WM_SetFocus(hObj);
break;
case LISTBOX_NOTIFICATION_LOST_FOCUS:
DROPDOWN_Collapse(hObj);
break;
}
break;
case WM_PID_STATE_CHANGED:
if (IsExpandedBeforeMsg == 0) { /* Make sure we do not react a second time */
const WM_PID_STATE_CHANGED_INFO * pInfo = (const WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p;
if (pInfo->State) {
DROPDOWN_Expand(hObj);
}
}
break;
case WM_TOUCH:
if (_OnTouch(hObj, pMsg) == 0) {
return;
}
break;
case WM_PAINT:
_Paint(hObj);
break;
case WM_DELETE:
_FreeAttached(pObj);
break; /* No return here ... WM_DefaultProc needs to be called */
case WM_KEY:
if ( ((const WM_KEY_INFO*)(pMsg->Data.p))->PressedCnt >0) {
int Key = ((const WM_KEY_INFO*)(pMsg->Data.p))->Key;
switch (Key) {
case DROPDOWN_KEY_EXPAND:
DROPDOWN_Expand(hObj);
break;
case GUI_KEY_BACKTAB:
break;
case GUI_KEY_TAB:
if (WM_GetFocussedWindow() == pObj->hListWin) {
WM_SetFocus(hObj); /* Make sure, WM_SetFocusOnNextChild() works right when calling WM_DefaultProc() */
}
break; /* Send to parent by not doing anything */
default:
DROPDOWN_AddKey(hObj, Key);
return;
}
}
break;
}
WM_DefaultProc(pMsg);
}
/*********************************************************************
*
* Exported routines: Create
*
**********************************************************************
*/
/*********************************************************************
*
* DROPDOWN_CreateEx
*/
DROPDOWN_Handle DROPDOWN_CreateEx(int x0, int y0, int xsize, int ysize, WM_HWIN hParent,
int WinFlags, int ExFlags, int Id)
{
DROPDOWN_Handle hObj;
WM_LOCK();
hObj = WM_CreateWindowAsChild(x0, y0, xsize, -1, hParent, WinFlags, DROPDOWN_Callback,
sizeof(DROPDOWN_Obj) - sizeof(WM_Obj));
if (hObj) {
DROPDOWN_Obj* pObj;
pObj = (DROPDOWN_Obj *)GUI_ALLOC_h2p(hObj); /* Don't use use WIDGET_H2P because WIDGET_INIT_ID() has not be called at this point */
/* Init sub-classes */
GUI_ARRAY_CREATE(&pObj->Handles);
/* init widget specific variables */
WIDGET__Init(&pObj->Widget, Id, WIDGET_STATE_FOCUSSABLE);
pObj->Flags = ExFlags;
pObj->Props = DROPDOWN__DefaultProps;
pObj->ScrollbarWidth = 0;
DROPDOWN_INIT_ID(pObj);
pObj->ySizeEx = ysize;
DROPDOWN__AdjustHeight(hObj, pObj);
}
WM_UNLOCK();
return hObj;
}
/*********************************************************************
*
* Exported routines: Various methods
*
**********************************************************************
*/
/*********************************************************************
*
* DROPDOWN_Collapse
*/
void DROPDOWN_Collapse(DROPDOWN_Handle hObj) {
DROPDOWN_Obj* pObj;
if (hObj) {
WM_LOCK();
pObj = DROPDOWN_H2P(hObj);
if (pObj->hListWin) {
WM_DeleteWindow(pObj->hListWin);
pObj->hListWin = 0;
}
WM_UNLOCK();
}
}
/*********************************************************************
*
* DROPDOWN_Expand
*/
void DROPDOWN_Expand(DROPDOWN_Handle hObj) {
int xSize, ySize, i, NumItems;
WM_HWIN hLst;
GUI_RECT r;
WM_HWIN hParent;
WM_Obj* pParent;
DROPDOWN_Obj* pObj;
if (hObj) {
WM_LOCK();
pObj = DROPDOWN_H2P(hObj);
if (pObj->hListWin == 0) {
hParent = WM_GetParent(hObj);
pParent = WM_H2P(hParent);
xSize = WM__GetWindowSizeX(&pObj->Widget.Win);
ySize = pObj->ySizeEx;
NumItems = DROPDOWN__GetNumItems(pObj);
/* Get coordinates of window in client coordiantes of parent */
r = pObj->Widget.Win.Rect;
GUI_MoveRect(&r, -pParent->Rect.x0, -pParent->Rect.y0);
if (pObj->Flags & DROPDOWN_CF_UP) {
r.y0 -= ySize;
} else {
r.y0 = r.y1;
}
hLst = LISTBOX_CreateAsChild(NULL, WM_GetParent(hObj), r.x0, r.y0
, xSize, ySize, WM_CF_SHOW);
#if WIDGET_USE_PARENT_EFFECT
WIDGET_SetEffect(hLst, pObj->Widget.pEffect);
#endif
if (pObj->Flags & DROPDOWN_SF_AUTOSCROLLBAR) {
LISTBOX_SetScrollbarWidth(hLst, pObj->ScrollbarWidth);
LISTBOX_SetAutoScrollV(hLst, 1);
}
for (i = 0; i< NumItems; i++) {
LISTBOX_AddString(hLst, _GetpItem(pObj, i));
}
for (i = 0; i < GUI_COUNTOF(pObj->Props.aBackColor); i++) {
LISTBOX_SetBkColor(hLst, i, pObj->Props.aBackColor[i]);
}
for (i = 0; i < GUI_COUNTOF(pObj->Props.aTextColor); i++) {
LISTBOX_SetTextColor(hLst, i, pObj->Props.aTextColor[i]);
}
for (i = 0; i < GUI_COUNTOF(pObj->Props.aScrollbarColor); i++) {
LISTBOX_SetScrollbarColor(hLst, i, pObj->Props.aScrollbarColor[i]);
}
LISTBOX_SetItemSpacing(hLst, pObj->ItemSpacing);
LISTBOX_SetFont(hLst, pObj->Props.pFont);
LISTBOX_SetTextAlign(hLst, pObj->Props.Align);
WM_SetFocus(hLst);
pObj->hListWin = hLst;
LISTBOX_SetOwner(hLst, hObj);
LISTBOX_SetSel(hLst, pObj->Sel);
WM_NotifyParent(hObj, WM_NOTIFICATION_CLICKED);
}
WM_UNLOCK();
}
}
/*********************************************************************
*
* DROPDOWN_AddKey
*/
void DROPDOWN_AddKey(DROPDOWN_Handle hObj, int Key) {
if (hObj) {
WM_LOCK();
switch (Key) {
case GUI_KEY_DOWN:
DROPDOWN_IncSel(hObj);
break;
case GUI_KEY_UP:
DROPDOWN_DecSel(hObj);
break;
default:
_SelectByKey(hObj, Key);
break;
}
WM_UNLOCK();
}
}
/*********************************************************************
*
* DROPDOWN_SetSel
*/
void DROPDOWN_SetSel(DROPDOWN_Handle hObj, int Sel) {
int NumItems, MaxSel;
DROPDOWN_Obj* pObj;
if (hObj) {
WM_LOCK();
pObj = DROPDOWN_H2P(hObj);
NumItems = DROPDOWN__GetNumItems(pObj);
MaxSel = NumItems ? NumItems-1 : 0;
if (Sel > MaxSel) {
Sel = MaxSel;
}
if (Sel != pObj->Sel) {
pObj->Sel = Sel;
DROPDOWN_Invalidate(hObj);
WM_NotifyParent(hObj, WM_NOTIFICATION_SEL_CHANGED);
}
WM_UNLOCK();
}
}
/*********************************************************************
*
* DROPDOWN_IncSel
*/
void DROPDOWN_IncSel(DROPDOWN_Handle hObj) {
int Sel = DROPDOWN_GetSel(hObj);
DROPDOWN_SetSel(hObj, Sel+1);
}
/*********************************************************************
*
* DROPDOWN_DecSel
*/
void DROPDOWN_DecSel(DROPDOWN_Handle hObj) {
int Sel = DROPDOWN_GetSel(hObj);
if (Sel)
Sel--;
DROPDOWN_SetSel(hObj, Sel);
}
/*********************************************************************
*
* DROPDOWN_GetSel
*/
int DROPDOWN_GetSel (DROPDOWN_Handle hObj) {
int r = 0;
DROPDOWN_Obj* pObj;
if (hObj) {
WM_LOCK();
pObj = DROPDOWN_H2P(hObj);
r = pObj->Sel;
WM_UNLOCK();
}
return r;
}
#else /* Avoid problems with empty object modules */
void DROPDOWN_C(void) {}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -