BCMenu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #ifndef BCMenu_H
  2. #define BCMenu_H
  3. #include <afxtempl.h>
  4. // BCMenuData class. Fill this class structure to define a single menu item:
  5. class BCMenuData
  6. {
  7. wchar_t *m_szMenuText;
  8. public:
  9. BCMenuData () {menuIconNormal=-1;xoffset=-1;bitmap=NULL;pContext=NULL;
  10. nFlags=0;nID=0;syncflag=0;m_szMenuText=NULL;global_offset=-1;threeD=FALSE;};
  11. void SetAnsiString(LPCSTR szAnsiString);
  12. void SetWideString(const wchar_t *szWideString);
  13. const wchar_t *GetWideString(void) {return m_szMenuText;};
  14. ~BCMenuData ();
  15. CString GetString(void);//returns the menu text in ANSI or UNICODE
  16. int xoffset,global_offset;
  17. BOOL threeD;
  18. int menuIconNormal;
  19. UINT nFlags,nID,syncflag;
  20. CImageList *bitmap;
  21. void *pContext; // used to attach user data
  22. };
  23. //struct CMenuItemInfo : public MENUITEMINFO {
  24. struct CMenuItemInfo : public
  25. //MENUITEMINFO
  26. #ifndef UNICODE //SK: this fixes warning C4097: typedef-name 'MENUITEMINFO' used as synonym for class-name 'tagMENUITEMINFOA'
  27. tagMENUITEMINFOA
  28. #else
  29. tagMENUITEMINFOW
  30. #endif
  31. {
  32. CMenuItemInfo()
  33. {
  34. memset(this, 0, sizeof(MENUITEMINFO));
  35. cbSize = sizeof(MENUITEMINFO);
  36. }
  37. };
  38. // how the menu's are drawn, either original or XP style
  39. typedef enum {BCMENU_DRAWMODE_ORIGINAL,BCMENU_DRAWMODE_XP} BC_MenuDrawMode;
  40. // how seperators are handled when removing a menu (Tongzhe Cui)
  41. typedef enum {BCMENU_NONE, BCMENU_HEAD, BCMENU_TAIL, BCMENU_BOTH} BC_Seperator;
  42. // defines for unicode support
  43. #ifndef UNICODE
  44. #define AppendMenu AppendMenuA
  45. #define InsertMenu InsertMenuA
  46. #define InsertODMenu InsertODMenuA
  47. #define AppendODMenu AppendODMenuA
  48. #define AppendODPopupMenu AppendODPopupMenuA
  49. #define ModifyODMenu ModifyODMenuA
  50. #else
  51. #define AppendMenu AppendMenuW
  52. #define InsertMenu InsertMenuW
  53. #define InsertODMenu InsertODMenuW
  54. #define AppendODMenu AppendODMenuW
  55. #define ModifyODMenu ModifyODMenuW
  56. #define AppendODPopupMenu AppendODPopupMenuW
  57. #endif
  58. class BCMenu : public CMenu
  59. {
  60. DECLARE_DYNAMIC( BCMenu )
  61. public:
  62. BCMenu();
  63. virtual ~BCMenu();
  64. // Functions for loading and applying bitmaps to menus (see example application)
  65. virtual BOOL LoadMenu(LPCTSTR lpszResourceName);
  66. virtual BOOL LoadMenu(int nResource);
  67. BOOL LoadToolbar(UINT nToolBar);
  68. BOOL LoadToolbars(const UINT *arID,int n);
  69. void AddFromToolBar(CToolBar* pToolBar, int nResourceID);
  70. BOOL LoadFromToolBar(UINT nID,UINT nToolBar,int& xoffset);
  71. BOOL AddBitmapToImageList(CImageList *list,UINT nResourceID);
  72. static HBITMAP LoadSysColorBitmap(int nResourceId);
  73. void LoadCheckmarkBitmap(int unselect,int select); // custom check mark bitmaps
  74. // functions for appending a menu option, use the AppendMenu call (see above define)
  75. BOOL AppendMenuA(UINT nFlags,UINT nIDNewItem=0,const char *lpszNewItem=NULL,int nIconNormal=-1);
  76. BOOL AppendMenuA(UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,CImageList *il,int xoffset);
  77. BOOL AppendMenuA(UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,CBitmap *bmp);
  78. BOOL AppendMenuW(UINT nFlags,UINT nIDNewItem=0,wchar_t *lpszNewItem=NULL,int nIconNormal=-1);
  79. BOOL AppendMenuW(UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,CImageList *il,int xoffset);
  80. BOOL AppendMenuW(UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,CBitmap *bmp);
  81. BOOL AppendODMenuA(LPCSTR lpstrText,UINT nFlags = MF_OWNERDRAW,UINT nID = 0,int nIconNormal = -1);
  82. BOOL AppendODMenuW(wchar_t *lpstrText,UINT nFlags = MF_OWNERDRAW,UINT nID = 0,int nIconNormal = -1);
  83. BOOL AppendODMenuA(LPCSTR lpstrText,UINT nFlags,UINT nID,CImageList *il,int xoffset);
  84. BOOL AppendODMenuW(wchar_t *lpstrText,UINT nFlags,UINT nID,CImageList *il,int xoffset);
  85. // for appending a popup menu (see example application)
  86. BCMenu* AppendODPopupMenuA(LPCSTR lpstrText);
  87. BCMenu* AppendODPopupMenuW(wchar_t *lpstrText);
  88. // functions for inserting a menu option, use the InsertMenu call (see above define)
  89. BOOL InsertMenuA(UINT nPosition,UINT nFlags,UINT nIDNewItem=0,const char *lpszNewItem=NULL,int nIconNormal=-1);
  90. BOOL InsertMenuA(UINT nPosition,UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,CImageList *il,int xoffset);
  91. BOOL InsertMenuA(UINT nPosition,UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,CBitmap *bmp);
  92. BOOL InsertMenuW(UINT nPosition,UINT nFlags,UINT nIDNewItem=0,wchar_t *lpszNewItem=NULL,int nIconNormal=-1);
  93. BOOL InsertMenuW(UINT nPosition,UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,CImageList *il,int xoffset);
  94. BOOL InsertMenuW(UINT nPosition,UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,CBitmap *bmp);
  95. BOOL InsertODMenuA(UINT nPosition,LPCSTR lpstrText,UINT nFlags = MF_OWNERDRAW,UINT nID = 0,int nIconNormal = -1);
  96. BOOL InsertODMenuW(UINT nPosition,wchar_t *lpstrText,UINT nFlags = MF_OWNERDRAW,UINT nID = 0,int nIconNormal = -1);
  97. BOOL InsertODMenuA(UINT nPosition,LPCSTR lpstrText,UINT nFlags,UINT nID,CImageList *il,int xoffset);
  98. BOOL InsertODMenuW(UINT nPosition,wchar_t *lpstrText,UINT nFlags,UINT nID,CImageList *il,int xoffset);
  99. // functions for modifying a menu option, use the ModifyODMenu call (see above define)
  100. BOOL ModifyODMenuA(const char *lpstrText,UINT nID=0,int nIconNormal=-1);
  101. BOOL ModifyODMenuA(const char *lpstrText,UINT nID,CImageList *il,int xoffset);
  102. BOOL ModifyODMenuA(const char *lpstrText,UINT nID,CBitmap *bmp);
  103. BOOL ModifyODMenuA(const char *lpstrText,const char *OptionText,int nIconNormal);
  104. BOOL ModifyODMenuW(wchar_t *lpstrText,UINT nID=0,int nIconNormal=-1);
  105. BOOL ModifyODMenuW(wchar_t *lpstrText,UINT nID,CImageList *il,int xoffset);
  106. BOOL ModifyODMenuW(wchar_t *lpstrText,UINT nID,CBitmap *bmp);
  107. BOOL ModifyODMenuW(wchar_t *lpstrText,wchar_t *OptionText,int nIconNormal);
  108. // use this method for adding a solid/hatched colored square beside a menu option
  109. // courtesy of Warren Stevens
  110. BOOL ModifyODMenuA(const char *lpstrText,UINT nID,COLORREF fill,COLORREF border,int hatchstyle=-1,CSize *pSize=NULL);
  111. BOOL ModifyODMenuW(wchar_t *lpstrText,UINT nID,COLORREF fill,COLORREF border,int hatchstyle=-1,CSize *pSize=NULL);
  112. // for deleting and removing menu options
  113. BOOL RemoveMenu(UINT uiId,UINT nFlags);
  114. BOOL DeleteMenu(UINT uiId,UINT nFlags);
  115. // sPos means Seperator's position, since we have no way to find the seperator's position in the menu
  116. // we have to specify them when we call the RemoveMenu to make sure the unused seperators are removed;
  117. // sPos = None no seperator removal;
  118. // = Head seperator in front of this menu item;
  119. // = Tail seperator right after this menu item;
  120. // = Both seperators at both ends;
  121. // remove the menu item based on their text, return -1 if not found, otherwise return the menu position;
  122. int RemoveMenu(char* pText, BC_Seperator sPos=BCMENU_NONE);
  123. int RemoveMenu(wchar_t* pText, BC_Seperator sPos=BCMENU_NONE);
  124. int DeleteMenu(char* pText, BC_Seperator sPos=BCMENU_NONE);
  125. int DeleteMenu(wchar_t* pText, BC_Seperator sPos=BCMENU_NONE);
  126. // Destoying
  127. virtual BOOL DestroyMenu();
  128. // function for retrieving and setting a menu options text (use this function
  129. // because it is ownerdrawn)
  130. BOOL GetMenuText(UINT id,CString &string,UINT nFlags = MF_BYPOSITION);
  131. BOOL SetMenuText(UINT id,CString string, UINT nFlags = MF_BYPOSITION);
  132. // Getting a submenu from it's name or position
  133. BCMenu* GetSubBCMenu(char* lpszSubMenuName);
  134. BCMenu* GetSubBCMenu(wchar_t* lpszSubMenuName);
  135. CMenu* GetSubMenu (LPCTSTR lpszSubMenuName);
  136. CMenu* GetSubMenu (int nPos);
  137. int GetMenuPosition(char* pText);
  138. int GetMenuPosition(wchar_t* pText);
  139. // Drawing:
  140. virtual void DrawItem( LPDRAWITEMSTRUCT); // Draw an item
  141. virtual void MeasureItem( LPMEASUREITEMSTRUCT ); // Measure an item
  142. // Static functions used for handling menu's in the mainframe
  143. static void UpdateMenu(CMenu *pmenu);
  144. static BOOL IsMenu(CMenu *submenu);
  145. static BOOL IsMenu(HMENU submenu);
  146. static LRESULT FindKeyboardShortcut(UINT nChar,UINT nFlags,CMenu *pMenu);
  147. // Function to set how menu is drawn, either original or XP style
  148. static void SetMenuDrawMode(UINT mode){
  149. BCMenu::original_drawmode=mode;
  150. BCMenu::xp_drawmode=mode;
  151. };
  152. // Function to set how disabled items are drawn (mode=FALSE means they are not drawn selected)
  153. static void SetSelectDisableMode(BOOL mode){
  154. BCMenu::original_select_disabled=mode;
  155. BCMenu::xp_select_disabled=mode;
  156. };
  157. static int BCMenu::GetMenuDrawMode(void);
  158. static BOOL BCMenu::GetSelectDisableMode(void);
  159. // how the bitmaps are drawn in XP Luna mode
  160. static void SetXPBitmap3D(BOOL val){
  161. BCMenu::xp_draw_3D_bitmaps=val;
  162. };
  163. static BOOL GetXPBitmap3D(void){return BCMenu::xp_draw_3D_bitmaps;}
  164. // Customizing:
  165. // Set icon size
  166. void SetIconSize (int, int);
  167. // set the color in the bitmaps that is the background transparent color
  168. void SetBitmapBackground(COLORREF color);
  169. void UnSetBitmapBackground(void);
  170. // obsolete functions for setting how menu images are dithered for disabled menu options
  171. BOOL GetDisableOldStyle(void);
  172. void SetDisableOldStyle(void);
  173. void UnSetDisableOldStyle(void);
  174. static COLORREF LightenColor(COLORREF col,double factor);
  175. static COLORREF DarkenColor(COLORREF col,double factor);
  176. // Miscellaneous Protected Member functions
  177. protected:
  178. static BOOL IsNewShell(void);
  179. static BOOL IsWinXPLuna(void);
  180. static BOOL IsLunaMenuStyle(void);
  181. static BOOL IsWindowsClassicTheme(void);
  182. BCMenuData *BCMenu::FindMenuItem(UINT nID);
  183. BCMenu *FindMenuOption(int nId,int& nLoc);
  184. BCMenu *FindAnotherMenuOption(int nId,int& nLoc,CArray<BCMenu*,BCMenu*>&bcsubs,
  185. CArray<int,int&>&bclocs);
  186. BCMenuData *FindMenuOption(wchar_t *lpstrText);
  187. void InsertSpaces(void);
  188. void DrawCheckMark(CDC *pDC,int x,int y,COLORREF color);
  189. void DrawRadioDot(CDC *pDC,int x,int y,COLORREF color);
  190. BCMenuData *NewODMenu(UINT pos,UINT nFlags,UINT nID,CString string);
  191. void SynchronizeMenu(void);
  192. void BCMenu::InitializeMenuList(int value);
  193. void BCMenu::DeleteMenuList(void);
  194. BCMenuData *BCMenu::FindMenuList(UINT nID);
  195. void DrawItem_Win9xNT2000 (LPDRAWITEMSTRUCT lpDIS);
  196. void DrawItem_WinXP (LPDRAWITEMSTRUCT lpDIS);
  197. BOOL Draw3DCheckmark(CDC *dc, const CRect& rc,BOOL bSelected,HBITMAP hbmCheck);
  198. BOOL DrawXPCheckmark(CDC *dc, const CRect& rc, HBITMAP hbmCheck,COLORREF &colorout);
  199. void DitherBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth,
  200. int nHeight, HBITMAP hbm, int nXSrc, int nYSrc,COLORREF bgcolor);
  201. void DitherBlt2(CDC *drawdc, int nXDest, int nYDest, int nWidth,
  202. int nHeight, CBitmap &bmp, int nXSrc, int nYSrc,COLORREF bgcolor);
  203. BOOL GetBitmapFromImageList(CDC* pDC,CImageList *imglist,int nIndex,CBitmap &bmp);
  204. BOOL ImageListDuplicate(CImageList *il,int xoffset,CImageList *newlist);
  205. static WORD NumBitmapColors(LPBITMAPINFOHEADER lpBitmap);
  206. void ColorBitmap(CDC* pDC, CBitmap& bmp,CSize bitmap_size,CSize icon_size,COLORREF fill,COLORREF border,int hatchstyle=-1);
  207. void RemoveTopLevelOwnerDraw(void);
  208. int GetMenuStart(void);
  209. void GetFadedBitmap(CBitmap &bmp);
  210. void GetShadowBitmap(CBitmap &bmp);
  211. int AddToGlobalImageList(CImageList *il,int xoffset,int nID);
  212. int GlobalImageListOffset(int nID);
  213. BOOL CanDraw3DImageList(int offset);
  214. // Member Variables
  215. protected:
  216. CTypedPtrArray<CPtrArray, BCMenuData*> m_MenuList; // Stores list of menu items
  217. // When loading an owner-drawn menu using a Resource, BCMenu must keep track of
  218. // the popup menu's that it creates. Warning, this list *MUST* be destroyed
  219. // last item first :)
  220. CTypedPtrArray<CPtrArray, HMENU> m_SubMenus; // Stores list of sub-menus
  221. // Stores a list of all BCMenu's ever created
  222. static CTypedPtrArray<CPtrArray, HMENU> m_AllSubMenus;
  223. // Global ImageList
  224. static CImageList m_AllImages;
  225. static CArray<int,int&> m_AllImagesID;
  226. // icon size
  227. int m_iconX,m_iconY;
  228. COLORREF m_bitmapBackground;
  229. BOOL m_bitmapBackgroundFlag;
  230. BOOL disable_old_style;
  231. static UINT original_drawmode;
  232. static BOOL original_select_disabled;
  233. static UINT xp_drawmode;
  234. static BOOL xp_select_disabled;
  235. static BOOL xp_draw_3D_bitmaps;
  236. CImageList *checkmaps;
  237. BOOL checkmapsshare;
  238. int m_selectcheck;
  239. int m_unselectcheck;
  240. BOOL m_bDynIcons;
  241. BOOL m_loadmenu;
  242. };
  243. class BCMenuToolBar : public CToolBar{
  244. public:
  245. BCMenuToolBar() : CToolBar() {m_iconX=m_iconY=0;}
  246. BOOL LoadToolBar(LPCTSTR lpszResourceName);
  247. BOOL LoadToolBar(UINT nIDResource){
  248. return LoadToolBar(MAKEINTRESOURCE(nIDResource));
  249. }
  250. BOOL LoadBitmap(LPCTSTR lpszResourceName);
  251. void GetIconSize(int &iconx,int &icony){iconx=m_iconX;icony=m_iconY;}
  252. protected:
  253. int m_iconX,m_iconY;
  254. };
  255. #define BCMENU_USE_MEMDC
  256. #ifdef BCMENU_USE_MEMDC
  257. //////////////////////////////////////////////////
  258. // BCMenuMemDC - memory DC
  259. //
  260. // Author: Keith Rule
  261. // Email: keithr@europa.com
  262. // Copyright 1996-1997, Keith Rule
  263. //
  264. // You may freely use or modify this code provided this
  265. // Copyright is included in all derived versions.
  266. //
  267. // History - 10/3/97 Fixed scrolling bug.
  268. // Added print support.
  269. // 25 feb 98 - fixed minor assertion bug
  270. //
  271. // This class implements a memory Device Context
  272. class BCMenuMemDC : public CDC
  273. {
  274. public:
  275. // constructor sets up the memory DC
  276. BCMenuMemDC(CDC* pDC,LPCRECT lpSrcRect) : CDC()
  277. {
  278. ASSERT(pDC != NULL);
  279. m_rect.CopyRect(lpSrcRect);
  280. m_pDC = pDC;
  281. m_pOldBitmap = NULL;
  282. m_bMemDC = !pDC->IsPrinting();
  283. if (m_bMemDC) // Create a Memory DC
  284. {
  285. CreateCompatibleDC(pDC);
  286. m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
  287. m_pOldBitmap = SelectObject(&m_bitmap);
  288. SetWindowOrg(m_rect.left, m_rect.top);
  289. }
  290. else // Make a copy of the relevent parts of the current DC for printing
  291. {
  292. m_bPrinting = pDC->m_bPrinting;
  293. m_hDC = pDC->m_hDC;
  294. m_hAttribDC = pDC->m_hAttribDC;
  295. }
  296. }
  297. // Destructor copies the contents of the mem DC to the original DC
  298. ~BCMenuMemDC()
  299. {
  300. if (m_bMemDC)
  301. {
  302. // Copy the offscreen bitmap onto the screen.
  303. m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
  304. this, m_rect.left, m_rect.top, SRCCOPY);
  305. //Swap back the original bitmap.
  306. SelectObject(m_pOldBitmap);
  307. } else {
  308. // All we need to do is replace the DC with an illegal value,
  309. // this keeps us from accidently deleting the handles associated with
  310. // the CDC that was passed to the constructor.
  311. m_hDC = m_hAttribDC = NULL;
  312. }
  313. }
  314. // Allow usage as a pointer
  315. BCMenuMemDC* operator->() {return this;}
  316. // Allow usage as a pointer
  317. operator BCMenuMemDC*() {return this;}
  318. private:
  319. CBitmap m_bitmap; // Offscreen bitmap
  320. CBitmap* m_pOldBitmap; // bitmap originally found in BCMenuMemDC
  321. CDC* m_pDC; // Saves CDC passed in constructor
  322. CRect m_rect; // Rectangle of drawing area.
  323. BOOL m_bMemDC; // TRUE if CDC really is a Memory DC.
  324. };
  325. #endif
  326. #endif
  327. //*************************************************************************