📄 menu.c
字号:
_SetCapture(hObj, pObj);
MENU__ResizeMenu(hObj, pObj);
break;
case MENU_ON_CLOSE:
_CloseSubmenu(hObj, pObj);
break;
case MENU_IS_MENU:
pMsg->Data.v = 1;
break;
}
}
}
/*********************************************************************
*
* _OnTouch
*/
static char _OnTouch(MENU_Handle hObj, MENU_Obj* pObj, WM_MESSAGE* pMsg) {
const GUI_PID_STATE* pState = (const GUI_PID_STATE*)pMsg->Data.p;
if (pState) { /* Something happened in our area (pressed or released) */
return _HandlePID(hObj, pObj, pState->x, pState->y, pState->Pressed);
}
return _HandlePID(hObj, pObj, -1, -1, -1); /* Moved out */
}
/*********************************************************************
*
* _OnMouseOver
*/
#if (GUI_SUPPORT_MOUSE)
static char _OnMouseOver(MENU_Handle hObj, MENU_Obj* pObj, WM_MESSAGE* pMsg) {
const GUI_PID_STATE* pState = (const GUI_PID_STATE *)pMsg->Data.p;
if (pState) {
return _HandlePID(hObj, pObj, pState->x, pState->y, -1);
}
return 0;
}
#endif
/*********************************************************************
*
* _SetPaintColors
*/
static void _SetPaintColors(const MENU_Obj* pObj, const MENU_ITEM* pItem, int ItemIndex) {
char Selected;
unsigned ColorIndex;
Selected = (ItemIndex == pObj->Sel) ? 1 : 0;
if (pObj->IsSubmenuActive && Selected) {
ColorIndex = MENU_CI_ACTIVE_SUBMENU;
} else if (pItem->Flags & MENU_IF_SEPARATOR) {
ColorIndex = MENU_CI_ENABLED;
} else {
ColorIndex = (Selected) ? MENU_CI_SELECTED : MENU_CI_ENABLED;
if (pItem->Flags & MENU_IF_DISABLED) {
if (pObj->Flags & MENU_CF_HIDE_DISABLED_SEL) {
ColorIndex = MENU_CI_DISABLED;
} else {
ColorIndex += MENU_CI_DISABLED;
}
}
}
GUI_SetBkColor(pObj->Props.aBkColor[ColorIndex]);
GUI_SetColor(pObj->Props.aTextColor[ColorIndex]);
}
/*********************************************************************
*
* _OnPaint
*/
static void _OnPaint(MENU_Handle hObj, MENU_Obj* pObj) {
GUI_RECT FillRect, TextRect;
MENU_ITEM* pItem;
unsigned TextWidth, NumItems, i;
U8 BorderLeft = pObj->Props.aBorder[MENU_BI_LEFT];
U8 BorderTop = pObj->Props.aBorder[MENU_BI_TOP];
int FontHeight = GUI_GetYDistOfFont(pObj->Props.pFont);
int EffectSize = _GetEffectSize(hObj, pObj);
NumItems = MENU__GetNumItems(pObj);
WM__GetClientRectWin(&pObj->Widget.Win, &FillRect);
GUI__ReduceRect(&FillRect, &FillRect, EffectSize);
GUI_SetFont(pObj->Props.pFont);
if (pObj->Flags & MENU_SF_VERTICAL) {
int ItemHeight, xSize;
xSize = _CalcMenuSizeX(hObj, pObj);
FillRect.x1 = xSize - EffectSize - 1;
TextRect.x0 = FillRect.x0 + BorderLeft;
for (i = 0; i < NumItems; i++) {
pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, i);
ItemHeight = _GetItemHeight(hObj, pObj, i);
_SetPaintColors(pObj, pItem, i);
FillRect.y1 = FillRect.y0 + ItemHeight - 1;
if (pItem->Flags & MENU_IF_SEPARATOR) {
GUI_ClearRectEx(&FillRect);
GUI_SetColor(0x7C7C7C);
GUI_DrawHLine(FillRect.y0 + BorderTop + 1, FillRect.x0 + 2, FillRect.x1 - 2);
} else {
TextWidth = pItem->TextWidth;
TextRect.x1 = TextRect.x0 + TextWidth - 1;
TextRect.y0 = FillRect.y0 + BorderTop;
TextRect.y1 = TextRect.y0 + FontHeight - 1;
WIDGET__FillStringInRect(pItem->acText, &FillRect, &TextRect, &TextRect);
}
FillRect.y0 += ItemHeight;
}
} else {
int ItemWidth, ySize;
ySize = _CalcMenuSizeY(hObj, pObj);
FillRect.y1 = ySize - EffectSize - 1;
TextRect.y0 = FillRect.y0 + BorderTop;
TextRect.y1 = TextRect.y0 + FontHeight - 1;
for (i = 0; i < NumItems; i++) {
pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, i);
ItemWidth = _GetItemWidth(hObj, pObj, i);
_SetPaintColors(pObj, pItem, i);
FillRect.x1 = FillRect.x0 + ItemWidth - 1;
if (pItem->Flags & MENU_IF_SEPARATOR) {
GUI_ClearRectEx(&FillRect);
GUI_SetColor(0x7C7C7C);
GUI_DrawVLine(FillRect.x0 + BorderLeft + 1, FillRect.y0 + 2, FillRect.y1 - 2);
} else {
TextWidth = pItem->TextWidth;
TextRect.x0 = FillRect.x0 + BorderLeft;
TextRect.x1 = TextRect.x0 + TextWidth - 1;
WIDGET__FillStringInRect(pItem->acText, &FillRect, &TextRect, &TextRect);
}
FillRect.x0 += ItemWidth;
}
}
if (pObj->Width || pObj->Height) {
GUI_RECT r;
WM__GetClientRectWin(&pObj->Widget.Win, &r);
GUI__ReduceRect(&r, &r, EffectSize);
GUI_SetBkColor(pObj->Props.aBkColor[MENU_CI_ENABLED]);
GUI_ClearRect(FillRect.x1 + 1, EffectSize, r.x1, FillRect.y1);
GUI_ClearRect(EffectSize, FillRect.y1 + 1, r.x1, r.y1);
}
/* Draw 3D effect (if configured) */
if (_HasEffect(hObj, pObj)) {
pObj->Widget.pEffect->pfDrawUp();
}
}
/*********************************************************************
*
* _MENU_Callback
*/
static void _MENU_Callback(WM_MESSAGE* pMsg) {
MENU_Handle hObj;
MENU_Obj* pObj;
hObj = pMsg->hWin;
if (pMsg->MsgId != WM_PID_STATE_CHANGED) {
/* Let widget handle the standard messages */
if (WIDGET_HandleActive(hObj, pMsg) == 0) {
return;
}
}
pObj = (MENU_Obj*) GUI_ALLOC_h2p(hObj);
switch (pMsg->MsgId) {
case WM_MENU:
_OnMenu(hObj, pObj, pMsg);
return; /* Message handled, do not call WM_DefaultProc() here. */
case WM_TOUCH:
if (_OnTouch(hObj, pObj, pMsg)) {
_ForwardPIDMsgToOwner(hObj, pObj, pMsg);
}
break;
#if (GUI_SUPPORT_MOUSE)
case WM_MOUSEOVER:
if (_OnMouseOver(hObj, pObj, pMsg)) {
_ForwardPIDMsgToOwner(hObj, pObj, pMsg);
}
break;
#endif
case WM_PAINT:
_OnPaint(hObj, pObj);
break;
case WM_DELETE:
GUI_ARRAY_Delete(&pObj->ItemArray);
break; /* No return here ... WM_DefaultProc needs to be called */
}
WM_DefaultProc(pMsg);
}
/*********************************************************************
*
* Public code, Create
*
**********************************************************************
*/
/*********************************************************************
*
* MENU_CreateEx
*/
MENU_Handle MENU_CreateEx(int x0, int y0, int xSize, int ySize, WM_HWIN hParent, int WinFlags, int ExFlags, int Id) {
MENU_Handle hObj;
/* Create the window */
hObj = WM_CreateWindowAsChild(x0, y0, xSize, ySize, hParent, WM_CF_SHOW | WM_CF_STAYONTOP | WinFlags, &_MENU_Callback,
sizeof(MENU_Obj) - sizeof(WM_Obj));
if (hObj) {
MENU_Obj* pObj;
WM_LOCK();
pObj = (MENU_Obj*)GUI_ALLOC_h2p(hObj);
/* Init sub-classes */
GUI_ARRAY_CREATE(&pObj->ItemArray);
/* init widget specific variables */
WIDGET__Init(&pObj->Widget, Id, WIDGET_STATE_FOCUSSABLE);
/* init member variables */
if (ExFlags & MENU_SF_OPEN_ON_POINTEROVER) {
ExFlags |= MENU_SF_ACTIVE;
} else {
ExFlags &= ~(MENU_SF_ACTIVE);
}
pObj->Props = MENU__DefaultProps;
pObj->Flags = ExFlags;
pObj->Width = ((xSize > 0) ? xSize : 0);
pObj->Height = ((ySize > 0) ? ySize : 0);
pObj->Sel = -1;
pObj->hOwner = 0;
pObj->IsSubmenuActive = 0;
WIDGET_SetEffect(hObj, MENU__pDefaultEffect);
INIT_ID(pObj);
WM_UNLOCK();
} else {
GUI_DEBUG_ERROROUT_IF(hObj==0, "MENU_CreateEx failed")
}
return hObj;
}
/*********************************************************************
*
* Public code, modul internal functions
*
**********************************************************************
*/
/*********************************************************************
*
* MENU__GetNumItems
*/
unsigned MENU__GetNumItems(MENU_Obj* pObj) {
return GUI_ARRAY_GetNumItems(&pObj->ItemArray);
}
/*********************************************************************
*
* MENU__InvalidateItem
*/
void MENU__InvalidateItem(MENU_Handle hObj, const MENU_Obj* pObj, unsigned Index) {
GUI_USE_PARA(pObj);
GUI_USE_PARA(Index);
WM_InvalidateWindow(hObj); /* Can be optimized, no need to invalidate all items */
}
/*********************************************************************
*
* MENU__RecalcTextWidthOfItems
*/
void MENU__RecalcTextWidthOfItems(MENU_Obj* pObj) {
const GUI_FONT GUI_UNI_PTR* pOldFont;
MENU_ITEM* pItem;
unsigned i, NumItems;
NumItems = MENU__GetNumItems(pObj);
pOldFont = GUI_SetFont(pObj->Props.pFont);
for (i = 0; i < NumItems; i++) {
pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, i);
pItem->TextWidth = GUI_GetStringDistX(pItem->acText);
}
GUI_SetFont(pOldFont);
}
/*********************************************************************
*
* MENU__ResizeMenu
*/
void MENU__ResizeMenu(MENU_Handle hObj, MENU_Obj* pObj) {
int xSize, ySize;
xSize = _CalcWindowSizeX(hObj, pObj);
ySize = _CalcWindowSizeY(hObj, pObj);
WM_SetSize(hObj, xSize, ySize);
WM_InvalidateWindow(hObj);
}
/*********************************************************************
*
* MENU__SetItem
*/
char MENU__SetItem(MENU_Handle hObj, MENU_Obj* pObj, unsigned Index, const MENU_ITEM_DATA* pItemData) {
MENU_ITEM Item = {0};
const char* pText;
pText = pItemData->pText;
if (!pText) {
pText = "";
}
Item.Id = pItemData->Id;
Item.Flags = pItemData->Flags;
Item.hSubmenu = pItemData->hSubmenu;
Item.TextWidth = _CalcTextWidth(pObj, pText);
if (Item.Flags & MENU_IF_SEPARATOR) {
Item.hSubmenu = 0; /* Ensures that no separator is a submenu */
}
if (GUI_ARRAY_SetItem(&pObj->ItemArray, Index, &Item, sizeof(MENU_ITEM) + strlen(pText)) != 0) {
MENU_ITEM* pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
strcpy(pItem->acText, pText);
MENU_SetOwner(Item.hSubmenu, hObj);
return 1;
}
return 0;
}
/*********************************************************************
*
* MENU__SetItemFlags
*/
void MENU__SetItemFlags(MENU_Obj* pObj, unsigned Index, U16 Mask, U16 Flags) {
MENU_ITEM* pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
pItem->Flags &= ~Mask;
pItem->Flags |= Flags;
}
/*********************************************************************
*
* MENU__SendMenuMessage
*/
int MENU__SendMenuMessage(MENU_Handle hObj, WM_HWIN hDestWin, U16 MsgType, U16 ItemId) {
MENU_MSG_DATA MsgData;
WM_MESSAGE Msg = {0};
MsgData.MsgType = MsgType;
MsgData.ItemId = ItemId;
Msg.MsgId = WM_MENU;
Msg.Data.p = &MsgData;
Msg.hWinSrc = hObj;
if (!hDestWin) {
hDestWin = WM_GetParent(hObj);
}
if (hDestWin) {
WM__SendMessage(hDestWin, &Msg);
return Msg.Data.v;
}
return 0;
}
/*********************************************************************
*
* Public code, member functions
*
**********************************************************************
*/
/*********************************************************************
*
* MENU_AddItem
*/
void MENU_AddItem(MENU_Handle hObj, const MENU_ITEM_DATA* pItemData) {
if (hObj && pItemData) {
MENU_Obj* pObj;
WM_LOCK();
pObj = MENU_H2P(hObj);
if (pObj) {
if (GUI_ARRAY_AddItem(&pObj->ItemArray, NULL, 0) == 0) {
unsigned Index;
Index = MENU__GetNumItems(pObj) - 1;
if (MENU__SetItem(hObj, pObj, Index, pItemData) == 0) {
GUI_ARRAY_DeleteItem(&pObj->ItemArray, Index);
} else {
MENU__ResizeMenu(hObj, pObj);
}
}
}
WM_UNLOCK();
}
}
/*********************************************************************
*
* MENU_SetOwner
*/
void MENU_SetOwner(MENU_Handle hObj, WM_HWIN hOwner) {
if (hObj) {
MENU_Obj* pObj;
WM_LOCK();
pObj = MENU_H2P(hObj);
if (pObj) {
pObj->hOwner = hOwner;
}
WM_UNLOCK();
}
}
#else /* avoid empty object files */
void Menu_C(void);
void Menu_C(void) {}
#endif
/*************************** End of file ****************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -