1 // TortoiseSVN - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2008 - TortoiseSVN
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
20 #include "IconMenu.h"
\r
22 CIconMenu::CIconMenu(void) : CMenu()
\r
23 , pfnBeginBufferedPaint(NULL)
\r
24 , pfnEndBufferedPaint(NULL)
\r
25 , pfnGetBufferedPaintBits(NULL)
\r
27 OSVERSIONINFOEX inf;
\r
28 SecureZeroMemory(&inf, sizeof(OSVERSIONINFOEX));
\r
29 inf.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
\r
30 GetVersionEx((OSVERSIONINFO *)&inf);
\r
31 winVersion = MAKEWORD(inf.dwMinorVersion, inf.dwMajorVersion);
\r
33 if (winVersion >= 0x0600)
\r
35 HMODULE hUxTheme = ::GetModuleHandle (_T("UXTHEME.DLL"));
\r
37 pfnGetBufferedPaintBits = (FN_GetBufferedPaintBits)::GetProcAddress(hUxTheme, "GetBufferedPaintBits");
\r
38 pfnBeginBufferedPaint = (FN_BeginBufferedPaint)::GetProcAddress(hUxTheme, "BeginBufferedPaint");
\r
39 pfnEndBufferedPaint = (FN_EndBufferedPaint)::GetProcAddress(hUxTheme, "EndBufferedPaint");
\r
43 CIconMenu::~CIconMenu(void)
\r
45 std::map<UINT, HBITMAP>::iterator it;
\r
46 for (it = bitmaps.begin(); it != bitmaps.end(); ++it)
\r
48 ::DeleteObject(it->second);
\r
53 BOOL CIconMenu::AppendMenuIcon(UINT_PTR nIDNewItem, LPCTSTR lpszNewItem, UINT uIcon /* = 0 */)
\r
55 TCHAR menutextbuffer[255] = {0};
\r
56 _tcscpy_s(menutextbuffer, 255, lpszNewItem);
\r
59 return CMenu::AppendMenu(MF_STRING | MF_ENABLED, nIDNewItem, menutextbuffer);
\r
61 MENUITEMINFO info = {0};
\r
62 info.cbSize = sizeof(info);
\r
63 info.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID;
\r
64 info.fType = MFT_STRING;
\r
65 info.wID = nIDNewItem;
\r
66 info.dwTypeData = menutextbuffer;
\r
67 if (winVersion >= 0x600)
\r
69 info.fMask |= MIIM_BITMAP;
\r
70 info.hbmpItem = IconToBitmapPARGB32(uIcon);
\r
74 info.fMask |= MIIM_BITMAP;
\r
75 info.hbmpItem = HBMMENU_CALLBACK;
\r
77 icons[nIDNewItem] = uIcon;
\r
78 return InsertMenuItem(nIDNewItem, &info);
\r
81 BOOL CIconMenu::AppendMenuIcon(UINT_PTR nIDNewItem, UINT_PTR nNewItem, UINT uIcon /* = 0 */)
\r
84 temp.LoadString(nNewItem);
\r
86 return AppendMenuIcon(nIDNewItem, temp, uIcon);
\r
89 void CIconMenu::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
\r
91 if ((lpDrawItemStruct==NULL)||(lpDrawItemStruct->CtlType != ODT_MENU))
\r
92 return; //not for a menu
\r
93 HICON hIcon = (HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(icons[lpDrawItemStruct->itemID]), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
\r
96 DrawIconEx(lpDrawItemStruct->hDC,
\r
97 lpDrawItemStruct->rcItem.left - 16,
\r
98 lpDrawItemStruct->rcItem.top + (lpDrawItemStruct->rcItem.bottom - lpDrawItemStruct->rcItem.top - 16) / 2,
\r
100 0, NULL, DI_NORMAL);
\r
101 DestroyIcon(hIcon);
\r
104 void CIconMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
\r
106 if (lpMeasureItemStruct==NULL)
\r
108 lpMeasureItemStruct->itemWidth += 2;
\r
109 if (lpMeasureItemStruct->itemHeight < 16)
\r
110 lpMeasureItemStruct->itemHeight = 16;
\r
113 HBITMAP CIconMenu::IconToBitmapPARGB32(UINT uIcon)
\r
115 std::map<UINT, HBITMAP>::iterator bitmap_it = bitmaps.lower_bound(uIcon);
\r
116 if (bitmap_it != bitmaps.end() && bitmap_it->first == uIcon)
\r
117 return bitmap_it->second;
\r
119 HICON hIcon = (HICON)LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(uIcon), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
\r
123 if (pfnBeginBufferedPaint == NULL || pfnEndBufferedPaint == NULL || pfnGetBufferedPaintBits == NULL)
\r
127 sizIcon.cx = GetSystemMetrics(SM_CXSMICON);
\r
128 sizIcon.cy = GetSystemMetrics(SM_CYSMICON);
\r
131 SetRect(&rcIcon, 0, 0, sizIcon.cx, sizIcon.cy);
\r
132 HBITMAP hBmp = NULL;
\r
134 HDC hdcDest = CreateCompatibleDC(NULL);
\r
137 if (SUCCEEDED(Create32BitHBITMAP(hdcDest, &sizIcon, NULL, &hBmp)))
\r
139 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcDest, hBmp);
\r
142 BLENDFUNCTION bfAlpha = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
\r
143 BP_PAINTPARAMS paintParams = {0};
\r
144 paintParams.cbSize = sizeof(paintParams);
\r
145 paintParams.dwFlags = BPPF_ERASE;
\r
146 paintParams.pBlendFunction = &bfAlpha;
\r
149 HPAINTBUFFER hPaintBuffer = pfnBeginBufferedPaint(hdcDest, &rcIcon, BPBF_DIB, &paintParams, &hdcBuffer);
\r
152 if (DrawIconEx(hdcBuffer, 0, 0, hIcon, sizIcon.cx, sizIcon.cy, 0, NULL, DI_NORMAL))
\r
154 // If icon did not have an alpha channel we need to convert buffer to PARGB
\r
155 ConvertBufferToPARGB32(hPaintBuffer, hdcDest, hIcon, sizIcon);
\r
158 // This will write the buffer contents to the destination bitmap
\r
159 pfnEndBufferedPaint(hPaintBuffer, TRUE);
\r
162 SelectObject(hdcDest, hbmpOld);
\r
169 DestroyIcon(hIcon);
\r
172 bitmaps.insert(bitmap_it, std::make_pair(uIcon, hBmp));
\r
176 HRESULT CIconMenu::Create32BitHBITMAP(HDC hdc, const SIZE *psize, __deref_opt_out void **ppvBits, __out HBITMAP* phBmp)
\r
181 SecureZeroMemory(&bmi, sizeof(bmi));
\r
182 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
\r
183 bmi.bmiHeader.biPlanes = 1;
\r
184 bmi.bmiHeader.biCompression = BI_RGB;
\r
186 bmi.bmiHeader.biWidth = psize->cx;
\r
187 bmi.bmiHeader.biHeight = psize->cy;
\r
188 bmi.bmiHeader.biBitCount = 32;
\r
190 HDC hdcUsed = hdc ? hdc : GetDC(NULL);
\r
193 *phBmp = CreateDIBSection(hdcUsed, &bmi, DIB_RGB_COLORS, ppvBits, NULL, 0);
\r
194 if (hdc != hdcUsed)
\r
196 ReleaseDC(NULL, hdcUsed);
\r
199 return (NULL == *phBmp) ? E_OUTOFMEMORY : S_OK;
\r
202 HRESULT CIconMenu::ConvertBufferToPARGB32(HPAINTBUFFER hPaintBuffer, HDC hdc, HICON hicon, SIZE& sizIcon)
\r
206 HRESULT hr = pfnGetBufferedPaintBits(hPaintBuffer, &prgbQuad, &cxRow);
\r
209 Gdiplus::ARGB *pargb = reinterpret_cast<Gdiplus::ARGB *>(prgbQuad);
\r
210 if (!HasAlpha(pargb, sizIcon, cxRow))
\r
213 if (GetIconInfo(hicon, &info))
\r
217 hr = ConvertToPARGB32(hdc, pargb, info.hbmMask, sizIcon, cxRow);
\r
220 DeleteObject(info.hbmColor);
\r
221 DeleteObject(info.hbmMask);
\r
229 bool CIconMenu::HasAlpha(__in Gdiplus::ARGB *pargb, SIZE& sizImage, int cxRow)
\r
231 ULONG cxDelta = cxRow - sizImage.cx;
\r
232 for (ULONG y = sizImage.cy; y; --y)
\r
234 for (ULONG x = sizImage.cx; x; --x)
\r
236 if (*pargb++ & 0xFF000000)
\r
248 HRESULT CIconMenu::ConvertToPARGB32(HDC hdc, __inout Gdiplus::ARGB *pargb, HBITMAP hbmp, SIZE& sizImage, int cxRow)
\r
251 SecureZeroMemory(&bmi, sizeof(bmi));
\r
252 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
\r
253 bmi.bmiHeader.biPlanes = 1;
\r
254 bmi.bmiHeader.biCompression = BI_RGB;
\r
256 bmi.bmiHeader.biWidth = sizImage.cx;
\r
257 bmi.bmiHeader.biHeight = sizImage.cy;
\r
258 bmi.bmiHeader.biBitCount = 32;
\r
260 HRESULT hr = E_OUTOFMEMORY;
\r
261 HANDLE hHeap = GetProcessHeap();
\r
262 void *pvBits = HeapAlloc(hHeap, 0, bmi.bmiHeader.biWidth * 4 * bmi.bmiHeader.biHeight);
\r
266 if (GetDIBits(hdc, hbmp, 0, bmi.bmiHeader.biHeight, pvBits, &bmi, DIB_RGB_COLORS) == bmi.bmiHeader.biHeight)
\r
268 ULONG cxDelta = cxRow - bmi.bmiHeader.biWidth;
\r
269 Gdiplus::ARGB *pargbMask = static_cast<Gdiplus::ARGB *>(pvBits);
\r
271 for (ULONG y = bmi.bmiHeader.biHeight; y; --y)
\r
273 for (ULONG x = bmi.bmiHeader.biWidth; x; --x)
\r
277 // transparent pixel
\r
283 *pargb++ |= 0xFF000000;
\r
293 HeapFree(hHeap, 0, pvBits);
\r