Replace all calls to LoadIcon and LoadImage with calls to LoadIconWithScaleDown
[TortoiseGit.git] / src / Utils / MiscUI / IconMenu.cpp
blobd721581914e94a8d5c6c51ba56b174a285455cd1
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2010-2011, 2013, 2016 - TortoiseGit
4 // Copyright (C) 2008-2009, 2011 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include "IconMenu.h"
22 #include "registry.h"
23 #include "LoadIconEx.h"
25 CIconMenu::CIconMenu(void) : CMenu()
27 bShowIcons = !!DWORD(CRegDWORD(L"Software\\TortoiseGit\\ShowAppContextMenuIcons", TRUE));
30 CIconMenu::~CIconMenu(void)
32 for (const auto& iconhandle : iconhandles)
33 DestroyIcon(iconhandle.second);
36 BOOL CIconMenu::CreateMenu()
38 if (__super::CreateMenu() == FALSE)
39 return FALSE;
41 SetMenuStyle();
43 return TRUE;
46 BOOL CIconMenu::CreatePopupMenu()
48 if (__super::CreatePopupMenu() == FALSE)
49 return FALSE;
51 SetMenuStyle();
53 return TRUE;
56 BOOL CIconMenu::SetMenuStyle(void)
58 MENUINFO MenuInfo = { 0 };
60 MenuInfo.cbSize = sizeof(MenuInfo);
61 MenuInfo.fMask = MIM_STYLE | MIM_APPLYTOSUBMENUS;
62 MenuInfo.dwStyle = MNS_CHECKORBMP;
64 SetMenuInfo(&MenuInfo);
66 return TRUE;
69 BOOL CIconMenu::AppendMenuIcon(UINT_PTR nIDNewItem, LPCTSTR lpszNewItem, UINT uIcon /* = 0 */, HMENU hsubmenu /* = nullptr */)
71 TCHAR menutextbuffer[255] = {0};
72 wcscpy_s(menutextbuffer, lpszNewItem);
74 MENUITEMINFO info = {0};
75 info.cbSize = sizeof(info);
76 info.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID;
77 info.fType = MFT_STRING;
79 if (hsubmenu)
81 info.fMask |= MIIM_SUBMENU;
82 info.hSubMenu = hsubmenu;
85 info.wID = (UINT)nIDNewItem;
86 info.dwTypeData = menutextbuffer;
87 if ((uIcon != 0) && bShowIcons)
89 info.fMask |= MIIM_BITMAP;
90 info.hbmpItem = bitmapUtils.IconToBitmapPARGB32(AfxGetResourceHandle(), uIcon);
92 icons[nIDNewItem] = uIcon;
95 return InsertMenuItem((UINT)nIDNewItem, &info);
98 BOOL CIconMenu::AppendMenuIcon(UINT_PTR nIDNewItem, UINT_PTR nNewItem, UINT uIcon /* = 0 */, HMENU hsubmenu /* = nullptr */)
100 CString temp;
101 temp.LoadString((UINT)nNewItem);
103 return AppendMenuIcon(nIDNewItem, temp, uIcon, hsubmenu);
106 void CIconMenu::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
108 if (!lpDrawItemStruct || lpDrawItemStruct->CtlType != ODT_MENU)
109 return; //not for a menu
110 HICON hIcon = nullptr;
111 bool bDestroyIcon = true;
112 int iconWidth = GetSystemMetrics(SM_CXSMICON);
113 int iconHeight = GetSystemMetrics(SM_CYSMICON);
114 if (iconhandles.find(lpDrawItemStruct->itemID) != iconhandles.end())
116 hIcon = iconhandles[lpDrawItemStruct->itemID];
117 bDestroyIcon = false;
119 else
120 hIcon = LoadIconEx(AfxGetResourceHandle(), MAKEINTRESOURCE(icons[lpDrawItemStruct->itemID]), iconWidth, iconHeight);
121 if (!hIcon)
122 return;
123 DrawIconEx(lpDrawItemStruct->hDC,
124 lpDrawItemStruct->rcItem.left - iconWidth,
125 lpDrawItemStruct->rcItem.top + (lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top - iconHeight) / 2,
126 hIcon, iconWidth, iconHeight,
127 0, nullptr, DI_NORMAL);
128 if (bDestroyIcon)
129 DestroyIcon(hIcon);
132 void CIconMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
134 if (!lpMeasureItemStruct)
135 return;
136 lpMeasureItemStruct->itemWidth += 2;
137 if (lpMeasureItemStruct->itemHeight < 16)
138 lpMeasureItemStruct->itemHeight = 16;
141 BOOL CIconMenu::SetMenuItemData(UINT_PTR nIDNewItem, LONG_PTR data)
143 MENUITEMINFO menuinfo ={0};
144 menuinfo.cbSize = sizeof(menuinfo);
145 GetMenuItemInfo((UINT)nIDNewItem, &menuinfo);
146 menuinfo.dwItemData =data;
147 menuinfo.fMask |= MIIM_DATA;
148 return SetMenuItemInfo((UINT)nIDNewItem ,&menuinfo);
151 LONG_PTR CIconMenu::GetMenuItemData(UINT_PTR nIDNewItem)
153 MENUITEMINFO menuinfo ={0};
154 menuinfo.fMask |= MIIM_DATA;
155 menuinfo.cbSize = sizeof(menuinfo);
156 GetMenuItemInfo((UINT)nIDNewItem, &menuinfo);
158 return menuinfo.dwItemData;