⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu.c

📁 ucgui的3.98版本的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
    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();
  }
}

/*********************************************************************
*
*       _MoveSel
*
* Purpose:
*   Moves the selection of the given menu to the desired direction.
*   If the last or the first item is selected, the selection moves to the begin or the end.
*   Separators will be skipped.
*/
static void _MoveSel(MENU_Handle hObj, int Dir) {
  MENU_Obj * pObj;
  int NewIndex, Index, NumItems, Cnt = 0;
  pObj = (MENU_Obj*) GUI_ALLOC_h2p(hObj);
  Index = pObj->Sel;
  NewIndex = -1;
  NumItems = pObj->ItemArray.NumItems;
  do {
    MENU_ITEM * pItem;
    Index += Dir;
    if (Index >= NumItems) {
      Index = 0;
      Cnt++;
    } else if (Index < 0) {
      Index = NumItems - 1;
      Cnt++;
    }
    pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
    if (!(pItem->Flags & MENU_IF_SEPARATOR)) {
      NewIndex = Index;
    }
  } while ((NewIndex < 0) && (Cnt < 2));
  if (NewIndex >= 0) {
    _SetSelection(hObj, pObj, Index);
  }
}

/*********************************************************************
*
*       _OpenMenu
*/
static void _OpenMenu(MENU_Handle hObj, MENU_Obj * pObj, int Index, int SubSel) {
  MENU_ITEM * pItem;
  MENU_Obj  * pSubObj;
  pItem = (MENU_ITEM *)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
  pObj->Flags |= MENU_SF_ACTIVE;
  _OpenSubmenu(hObj, pObj, Index);
  pSubObj = (MENU_Obj *) GUI_ALLOC_h2p(pItem->hSubmenu);
  if (SubSel >= pSubObj->ItemArray.NumItems) {
    SubSel = pSubObj->ItemArray.NumItems - 1;
  }
  _SetSelection(pItem->hSubmenu, pSubObj, SubSel);
  WM_SetFocus(pItem->hSubmenu);
  _SetSelection(hObj, pObj, Index);
}

/*********************************************************************
*
*       _OnKey
*/
static int _OnKey(MENU_Handle hObj, MENU_Obj * pObj, int Key) {
  int KeyOpen, KeyBack, KeyNext, KeyPrev, Index, SubSel = 0;
  MENU_ITEM * pItem;
  MENU_Handle hObjTopLevel;
  MENU_Obj * pObjTopLevel;
  Index = pObj->Sel;
  _GetTopLevelMenu(hObj, pObj, &hObjTopLevel, &pObjTopLevel, &SubSel);
  if (Index < 0) {
    if (Key != GUI_KEY_ESCAPE) {
      _SetSelection(hObj, pObj, 0);
      _SetCapture(hObj, pObj);
    }
    return 0;
  }
  if (pObj->Flags & MENU_CF_VERTICAL) {
    KeyOpen = GUI_KEY_RIGHT;
    KeyBack = GUI_KEY_LEFT;
    KeyNext = GUI_KEY_DOWN;
    KeyPrev = GUI_KEY_UP;
  } else {
    KeyOpen = GUI_KEY_DOWN;
    KeyBack = 0;
    KeyNext = GUI_KEY_RIGHT;
    KeyPrev = GUI_KEY_LEFT;
  }
  pItem = (MENU_ITEM *)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
  if (pItem->hSubmenu && ((Key == KeyOpen) || (Key == GUI_KEY_ENTER))) {
    /*
     *  If the current menu item is a submenu and <ENTER> or KeyOpen has been pressed, open the submenu
     */
    _OpenMenu(hObj, pObj, Index, 0);
  } else if (!pItem->hSubmenu && (Key == GUI_KEY_ENTER)) {
    /*
     *  If the current menu item is not a submenu and <ENTER> has been pressed,
     *  set the focus to the top level menu, close the submenus and send an MENU_ON_ITEMSELECT message to the owner
     */
    WM_SetFocus(hObjTopLevel);
    _ActivateItem(hObj, pObj, Index);
  } else if (((hObjTopLevel != hObj) && !(pObjTopLevel->Flags & MENU_CF_VERTICAL) && (Key == KeyOpen) && (!pItem->hSubmenu)) ||
             ((hObjTopLevel != hObj) && !(pObjTopLevel->Flags & MENU_CF_VERTICAL) && (Key == KeyBack) && (hObjTopLevel == pObj->hOwner))) {
    /*
     *  If the current menu is not the top level menu and the top level menu is horizontal
     *  and <GUI_KEY_RIGHT> or <GUI_KEY_LEFT> has been pressed close the current submenus,
     *  move the selection of the top level menu to next/previous item and open it
     */
    pObj->Flags &= ~MENU_SF_ACTIVE;
    /* Set focus to top level menu */
    WM_SetFocus(hObjTopLevel);
    /* Move selection of top level menu */
    if (Key == KeyOpen) {
      _MoveSel(hObjTopLevel, +1);
    } else {
      _MoveSel(hObjTopLevel, -1);
    }
    /* Open top level menu */
    _OpenMenu(hObjTopLevel, pObjTopLevel, pObjTopLevel->Sel, (Key == KeyBack) ? SubSel : 0);
  } else if ((hObjTopLevel != hObj) && 
             ((Key == GUI_KEY_ESCAPE) || ((Key == KeyBack) && ((!(pObjTopLevel->Flags & MENU_CF_VERTICAL)) && (!pItem->hSubmenu) && (hObjTopLevel != pObj->hOwner)) || (pObjTopLevel->Flags & MENU_CF_VERTICAL)))) {
    /*
     *  If the current menu is not the top level menu and the top level menu is vertical and <GUI_KEY_LEFT> has been pressed
     *  or the top level menu is vertical or horizontal and <GUI_KEY_ESCAPE> has been pressed,
     *  close the current submenu
     */
    WM_SetFocus(pObj->hOwner);
  } else if ((hObjTopLevel == hObj) && (Key == GUI_KEY_ESCAPE)) {
    /*
     *  If the current menu is the top level menu and <GUI_KEY_ESCAPE> has been pressed,
     *  unselect the menu and release capture
     */
    _SetSelection(hObj, pObj, -1);
    _ReleaseCapture(hObj, pObj);
  } else if (Key == KeyNext) {
    /*
     *  If KeyNext has been pressed move the selection to the next menu item
     */
    _CloseSubmenu(hObj, pObj);
    _MoveSel(hObj, +1);
  } else if (Key == KeyPrev) {
    /*
     *  If KeyPrev has been pressed move the selection to the previous menu item
     */
    _CloseSubmenu(hObj, pObj);
    _MoveSel(hObj, -1);
  } else {
    return 1;
  }
  return 0;
}

/*********************************************************************
*
*       Private routines
*
**********************************************************************
*/
/*********************************************************************
*
*       MENU_h2p
*/
#if GUI_DEBUG_LEVEL >= GUI_DEBUG_LEVEL_CHECK_ALL
MENU_Obj* MENU_h2p(MENU_Handle h) {
  MENU_Obj* p = (MENU_Obj*)GUI_ALLOC_h2p(h);
  if (p) {
    if (p->DebugId != MENU_ID) {
      GUI_DEBUG_ERROROUT("MENU.c: Wrong handle type or Object not init'ed");
      return 0;
    }
  }
  return p;
}
#endif

/*********************************************************************
*
*       Public code, Callback
*
**********************************************************************
*/
/*********************************************************************
*
*       MENU_Callback
*/
void MENU_Callback(WM_MESSAGE* pMsg) {
  MENU_Handle hObj;
  MENU_Obj* pObj;
  int OldWidgetState, Result, HasFocus;
  hObj = pMsg->hWin;
  pObj = (MENU_Obj*) GUI_ALLOC_h2p(hObj); /* Don't use use WIDGET_H2P because WIDGET_INIT_ID() has not be called at this point */
  OldWidgetState = pObj->Widget.State;
  Result = 1;
  if (pMsg->MsgId != WM_PID_STATE_CHANGED) {
    /* Let widget handle the standard messages */
    Result = WIDGET_HandleActive(hObj, pMsg);
  }
  HasFocus = pObj->Widget.State & WIDGET_STATE_FOCUS;
  /* React on focus change */
  if ((OldWidgetState & WIDGET_STATE_FOCUS) != HasFocus) {
    if (HasFocus) {
      _CloseSubmenu(hObj, pObj);
      pObj->Flags &= ~MENU_SF_ACTIVE;
      if (pObj->Sel <= 0) {
        _SetSelection(hObj, pObj, 0);
        _SetCapture(hObj, pObj);
      }
    } else {
      _SetSelection(hObj, pObj, -1);
      _ReleaseCapture(hObj, pObj);
    }
  }
  /* Return if message already has been processed */
  if (!Result) {
    return;
  }
  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_KEY:
    if (((const WM_KEY_INFO*)(pMsg->Data.p))->PressedCnt > 0) {
      if (!_OnKey(hObj, pObj, ((const WM_KEY_INFO*)(pMsg->Data.p))->Key)) {
        return;
      }
    }
    break;
  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 */
  WM_LOCK();
  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;
    pObj = (MENU_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->ItemArray);
    /* init widget specific variables */
    WIDGET__Init(&pObj->Widget, Id, WIDGET_STATE_FOCUSSABLE);
    /* init member variables */
    MENU_INIT_ID(pObj);
    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);
  } else {
    GUI_DEBUG_ERROROUT_IF(hObj==0, "MENU_CreateEx failed")
  }
  WM_UNLOCK();
  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);
    WM_DetachWindow(Item.hSubmenu);   /* Ensures all submenus are initially closed */
    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 + -