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

📄 menu.c

📁 ucgui的3.98版本的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
*********************************************************************************************************
*                                             uC/GUI V3.98
*                        Universal graphic software for embedded applications
*
*                       (c) Copyright 2002, Micrium Inc., Weston, FL
*                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
*              礐/GUI is protected by international copyright laws. Knowledge of the
*              source code may not be used to write a similar product. This file may
*              only be used in accordance with a license and should not be redistributed
*              in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File        : MENU.c
Purpose     : Implementation of menu widget
---------------------------END-OF-HEADER------------------------------
*/

#include <stdlib.h>
#include <string.h>

#define MENU_C       /* Required to generate intermodule data */

#include "MENU.h"
#include "MENU_Private.h"
#include "GUIDebug.h"
#include "GUI_Protected.h"

#if GUI_WINSUPPORT

/*********************************************************************
*
*       Private config defaults
*
**********************************************************************
*/

/* Define default font */
#ifndef MENU_FONT_DEFAULT
  #if   WIDGET_USE_SCHEME_SMALL
    #define MENU_FONT_DEFAULT &GUI_Font13_1
  #elif WIDGET_USE_SCHEME_MEDIUM
    #define MENU_FONT_DEFAULT &GUI_Font16_1
  #elif WIDGET_USE_SCHEME_LARGE
    #define MENU_FONT_DEFAULT &GUI_Font24_1
  #endif
#endif

/* Define default effect */
#ifndef MENU_EFFECT_DEFAULT
  #define MENU_EFFECT_DEFAULT       &WIDGET_Effect_3D1L
#endif

/* Define colors, index 0, enabled, not selected */
#ifndef MENU_TEXTCOLOR0_DEFAULT
  #define MENU_TEXTCOLOR0_DEFAULT   GUI_BLACK
#endif

#ifndef MENU_BKCOLOR0_DEFAULT
  #define MENU_BKCOLOR0_DEFAULT     GUI_LIGHTGRAY
#endif

/* Define colors, index 1, enabled, selected */
#ifndef MENU_TEXTCOLOR1_DEFAULT
  #define MENU_TEXTCOLOR1_DEFAULT   GUI_WHITE
#endif

#ifndef MENU_BKCOLOR1_DEFAULT
  #define MENU_BKCOLOR1_DEFAULT     0x980000
#endif

/* Define colors, index 2, disabled, not selected */
#ifndef MENU_TEXTCOLOR2_DEFAULT
  #define MENU_TEXTCOLOR2_DEFAULT   0x7C7C7C
#endif

#ifndef MENU_BKCOLOR2_DEFAULT
  #define MENU_BKCOLOR2_DEFAULT     GUI_LIGHTGRAY
#endif

/* Define colors, index 3, disabled, selected */
#ifndef MENU_TEXTCOLOR3_DEFAULT
  #define MENU_TEXTCOLOR3_DEFAULT   GUI_LIGHTGRAY
#endif

#ifndef MENU_BKCOLOR3_DEFAULT
  #define MENU_BKCOLOR3_DEFAULT     0x980000
#endif

/* Define colors, index 4, active submenu */
#ifndef MENU_TEXTCOLOR4_DEFAULT
  #define MENU_TEXTCOLOR4_DEFAULT   GUI_WHITE
#endif

#ifndef MENU_BKCOLOR4_DEFAULT
  #define MENU_BKCOLOR4_DEFAULT     0x7C7C7C
#endif

/* Define borders */
#ifndef MENU_BORDER_LEFT_DEFAULT
  #define MENU_BORDER_LEFT_DEFAULT    4
#endif

#ifndef MENU_BORDER_RIGHT_DEFAULT
  #define MENU_BORDER_RIGHT_DEFAULT   4
#endif

#ifndef MENU_BORDER_TOP_DEFAULT
  #define MENU_BORDER_TOP_DEFAULT     2
#endif

#ifndef MENU_BORDER_BOTTOM_DEFAULT
  #define MENU_BORDER_BOTTOM_DEFAULT  2
#endif

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

MENU_PROPS MENU__DefaultProps = {
  MENU_TEXTCOLOR0_DEFAULT,
  MENU_TEXTCOLOR1_DEFAULT,
  MENU_TEXTCOLOR2_DEFAULT,
  MENU_TEXTCOLOR3_DEFAULT,
  MENU_TEXTCOLOR4_DEFAULT,
  MENU_BKCOLOR0_DEFAULT,
  MENU_BKCOLOR1_DEFAULT,
  MENU_BKCOLOR2_DEFAULT,
  MENU_BKCOLOR3_DEFAULT,
  MENU_BKCOLOR4_DEFAULT,
  MENU_BORDER_LEFT_DEFAULT,
  MENU_BORDER_RIGHT_DEFAULT,
  MENU_BORDER_TOP_DEFAULT,
  MENU_BORDER_BOTTOM_DEFAULT,
  MENU_FONT_DEFAULT
};

const WIDGET_EFFECT* MENU__pDefaultEffect = MENU_EFFECT_DEFAULT;

/*********************************************************************
*
*       Static routines
*
**********************************************************************
*/
/*********************************************************************
*
*       _IsTopLevelMenu
*/
static char _IsTopLevelMenu(MENU_Handle hObj, const MENU_Obj* pObj) {
  if (MENU__SendMenuMessage(hObj, pObj->hOwner, MENU_IS_MENU, 0) == 0) {
    return 1;
  }
  return 0;
}

/*********************************************************************
*
*       _GetTopLevelMenu
*
* Purpose:
*   The function returns the top level menu of the given menu.
*
* Parameter:
*   hObj, pObj                  : Obvious
*   phObjTopLevel, ppObjTopLevel: Pointer to handle and object pointer for the result
*   pSubSel                     : Pointer to an integer variable for returning the current 
*                                 selection of the last open sub menu.
*/
static void _GetTopLevelMenu(MENU_Handle hObj, MENU_Obj * pObj, MENU_Handle * phObjTopLevel, MENU_Obj ** ppObjTopLevel, int * pSubSel) {
  MENU_Handle hObjTopLevel;
  MENU_Obj * pObjTopLevel;
  pObjTopLevel = pObj;
  if (_IsTopLevelMenu(hObj, pObj)) {
    hObjTopLevel = hObj;
    pObjTopLevel = pObj;
  } else {
    do {
      hObjTopLevel = pObjTopLevel->hOwner;
      pObjTopLevel = (MENU_Obj *)GUI_ALLOC_h2p(hObjTopLevel);
      if (_IsTopLevelMenu(hObjTopLevel, pObjTopLevel)) {
        break;
      } else {
        if (pSubSel) {
          *pSubSel = pObjTopLevel->Sel;
        }
      }
    } while (1);
  }
  *phObjTopLevel = hObjTopLevel;
  *ppObjTopLevel = pObjTopLevel;
}

/*********************************************************************
*
*       _HasEffect
*/
static int _HasEffect(MENU_Handle hObj, MENU_Obj* pObj) {
  if (!(pObj->Flags & MENU_SF_POPUP)) {
    if (_IsTopLevelMenu(hObj, pObj)) {
      return 0;
    }
  }
  return 1;
}

/*********************************************************************
*
*       _GetEffectSize
*/
static int _GetEffectSize(MENU_Handle hObj, MENU_Obj* pObj) {
  int r = 0;
  if (_HasEffect(hObj, pObj)) { 
    r = pObj->Widget.pEffect->EffectSize;
  }
  return r;
}

/*********************************************************************
*
*       _CalcTextWidth
*/
static int _CalcTextWidth(MENU_Obj* pObj, const char GUI_UNI_PTR* sText) {
  int TextWidth = 0;
  if (sText) {
    const GUI_FONT GUI_UNI_PTR* pOldFont;
    pOldFont  = GUI_SetFont(pObj->Props.pFont);
    TextWidth = GUI_GetStringDistX(sText);
    GUI_SetFont(pOldFont);
  }
  return TextWidth;
}

/*********************************************************************
*
*       _GetItemWidth
*/
static int _GetItemWidth(MENU_Handle hObj, MENU_Obj* pObj, unsigned Index) {
  int ItemWidth;
  if (pObj->Width && (pObj->Flags & MENU_SF_VERTICAL)) {
    ItemWidth = pObj->Width - (_GetEffectSize(hObj, pObj) << 1);
  } else {
    MENU_ITEM* pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
    if ((pObj->Flags & MENU_SF_VERTICAL) || !(pItem->Flags & MENU_IF_SEPARATOR)) {
      ItemWidth = pItem->TextWidth;
    } else {
      ItemWidth = 3;
   }
    ItemWidth += pObj->Props.aBorder[MENU_BI_LEFT] + pObj->Props.aBorder[MENU_BI_RIGHT];
  }
  return ItemWidth;
}

/*********************************************************************
*
*       _GetItemHeight
*/
static int _GetItemHeight(MENU_Handle hObj, MENU_Obj* pObj, unsigned Index) {
  int ItemHeight;
  if (pObj->Height && !(pObj->Flags & MENU_SF_VERTICAL)) {
    ItemHeight = pObj->Height - (_GetEffectSize(hObj, pObj) << 1);
  } else {
    ItemHeight = GUI_GetYDistOfFont(pObj->Props.pFont);
    if (pObj->Flags & MENU_SF_VERTICAL) {
      MENU_ITEM* pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, Index);
      if (pItem->Flags & MENU_IF_SEPARATOR) {
        ItemHeight = 3;
      }
    }
    ItemHeight += pObj->Props.aBorder[MENU_BI_TOP] + pObj->Props.aBorder[MENU_BI_BOTTOM];
  }
  return ItemHeight;
}

/*********************************************************************
*
*       _CalcMenuSizeX
*/
static int _CalcMenuSizeX(MENU_Handle hObj, MENU_Obj* pObj) {
  unsigned i, NumItems = MENU__GetNumItems(pObj);
  int xSize = 0;
  if (pObj->Flags & MENU_SF_VERTICAL) {
    int ItemWidth;
    for (i = 0; i < NumItems; i++) {
      ItemWidth = _GetItemWidth(hObj, pObj, i);
      if (ItemWidth > xSize) {
        xSize = ItemWidth;
      }
    }
  } else {
    for (i = 0; i < NumItems; i++) {
      xSize += _GetItemWidth(hObj, pObj, i);
    }
  }
  xSize += (_GetEffectSize(hObj, pObj) << 1);
  return xSize;
}

/*********************************************************************
*
*       _CalcMenuSizeY
*/
static int _CalcMenuSizeY(MENU_Handle hObj, MENU_Obj* pObj) {
  unsigned i, NumItems = MENU__GetNumItems(pObj);
  int ySize = 0;
  if (pObj->Flags & MENU_SF_VERTICAL) {
    for (i = 0; i < NumItems; i++) {
      ySize += _GetItemHeight(hObj, pObj, i);
    }
  } else {
    int ItemHeight;
    for (i = 0; i < NumItems; i++) {
      ItemHeight = _GetItemHeight(hObj, pObj, i);
      if (ItemHeight > ySize) {
        ySize = ItemHeight;
      }
    }
  }
  ySize += (_GetEffectSize(hObj, pObj) << 1);
  return ySize;
}

/*********************************************************************
*
*       _CalcWindowSizeX
*/
static int _CalcWindowSizeX(MENU_Handle hObj, MENU_Obj* pObj) {
  int xSize = pObj->Width;
  if (xSize == 0) {
    xSize = _CalcMenuSizeX(hObj, pObj);
  }
  return xSize;
}

/*********************************************************************
*
*       _CalcWindowSizeY
*/
static int _CalcWindowSizeY(MENU_Handle hObj, MENU_Obj* pObj) {
  int ySize = pObj->Height;
  if (ySize == 0) {
    ySize = _CalcMenuSizeY(hObj, pObj);
  }
  return ySize;
}

/*********************************************************************
*
*       _GetItemFromPos
*
* Return value:
*   Zero based index of item at given position or -1.
*
* NOTE:
*   This function has to ensure that index is always less than the
*   maximum number of items.
*/
static int _GetItemFromPos(MENU_Handle hObj, MENU_Obj* pObj, int x, int y) {
  int xSize, ySize, EffectSize, r = -1;
  ySize = _CalcMenuSizeY(hObj, pObj);
  if ((pObj->Height) && (pObj->Height < ySize)) {
    ySize = pObj->Height;
  }
  xSize = _CalcMenuSizeX(hObj, pObj);
  if ((pObj->Width) && (pObj->Width < xSize)) {
    xSize = pObj->Width;
  }
  EffectSize = _GetEffectSize(hObj, pObj);
  x     -= EffectSize;
  y     -= EffectSize;
  xSize -= (EffectSize << 1);
  ySize -= (EffectSize << 1);
  if ((x >= 0) && (y >= 0) && (x < xSize) && (y < ySize)) {
    unsigned i, NumItems = MENU__GetNumItems(pObj);
    if (pObj->Flags & MENU_SF_VERTICAL) {
      int yPos = 0;
      for (i = 0; i < NumItems; i++) {
        yPos += _GetItemHeight(hObj, pObj, i);
        if (y < yPos) {
          r = i;
          break;
        }
      }
    } else {
      int xPos = 0;
      for (i = 0; i < NumItems; i++) {
        xPos += _GetItemWidth(hObj, pObj, i);
        if (x < xPos) {
          r = i;
          break;
        }
      }
    }
  }
  return r;
}

/*********************************************************************
*
*       _GetItemPos
*/
static void _GetItemPos(MENU_Handle hObj, MENU_Obj* pObj, unsigned Index, int* px, int* py) {
  int i, EffectSize;
  EffectSize = _GetEffectSize(hObj, pObj);
  if (pObj->Flags & MENU_SF_VERTICAL) {
    int yPos = 0;
    for (i = 0; i < (int)Index; i++) {
      yPos += _GetItemHeight(hObj, pObj, i);
    }
    *px = EffectSize;
    *py = EffectSize + yPos;
  } else {
    int xPos = 0;
    for (i = 0; i < (int)Index; i++) {
      xPos += _GetItemWidth(hObj, pObj, i);
    }
    *px = EffectSize + xPos;
    *py = EffectSize;
  }
}

/*********************************************************************
*
*       _SetCapture
*/
static void _SetCapture(MENU_Handle hObj, const MENU_Obj* pObj) {
  if (pObj->IsSubmenuActive == 0) {
    if (WM_HasCaptured(hObj) == 0) {
      WM_SetCapture(hObj, 0);
    }
  }
}

/*********************************************************************
*
*       _ReleaseCapture
*/
static void _ReleaseCapture(MENU_Handle hObj, const MENU_Obj* pObj) {
  if (WM_HasCaptured(hObj)) {
    if (_IsTopLevelMenu(hObj, pObj) && !(pObj->Flags & MENU_SF_POPUP)) {
      WM_ReleaseCapture();
    }
  }
}

/*********************************************************************
*
*       _CloseSubmenu
*/
static void _CloseSubmenu(MENU_Handle hObj, MENU_Obj* pObj) {
  if (pObj->Flags & MENU_SF_ACTIVE) {
    if (pObj->IsSubmenuActive) {
      MENU_ITEM* pItem = (MENU_ITEM*)GUI_ARRAY_GetpItem(&pObj->ItemArray, pObj->Sel);
      /* Inform submenu about its deactivation and detach it */
      MENU__SendMenuMessage(hObj, pItem->hSubmenu, MENU_ON_CLOSE, 0);
      WM_DetachWindow(pItem->hSubmenu);
      pObj->IsSubmenuActive = 0;
      /*
       * Keep capture in menu widget. The capture may only released
       * by clicking outside the menu or when mouse moved out.
       * And it may only released from a top level menu.
       */
      _SetCapture(hObj, pObj);
      /* Invalidate menu item. This is needed because the appearance may have changed */
      MENU__InvalidateItem(hObj, pObj, pObj->Sel);
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -