Require the Win7 SDK & drop glue code
[TortoiseGit.git] / src / Utils / TaskbarUUID.cpp
blob14b414521f02728058fc4ae957a958b7d4fa167e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2012 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
20 #include "stdafx.h"
21 #include "TaskbarUUID.h"
22 #include "registry.h"
23 #include "CmdLineParser.h"
25 #include <Shobjidl.h>
26 #include "SmartHandle.h"
27 #include <atlbase.h>
29 #define APPID (_T("TGIT.TGIT.1"))
31 void SetTaskIDPerUUID()
33 typedef HRESULT STDAPICALLTYPE SetCurrentProcessExplicitAppUserModelIDFN(PCWSTR AppID);
34 CAutoLibrary hShell = AtlLoadSystemLibraryUsingFullPath(_T("shell32.dll"));
35 if (hShell)
37 SetCurrentProcessExplicitAppUserModelIDFN *pfnSetCurrentProcessExplicitAppUserModelID = (SetCurrentProcessExplicitAppUserModelIDFN*)GetProcAddress(hShell, "SetCurrentProcessExplicitAppUserModelID");
38 if (pfnSetCurrentProcessExplicitAppUserModelID)
40 std::wstring id = GetTaskIDPerUUID();
41 pfnSetCurrentProcessExplicitAppUserModelID(id.c_str());
46 std::wstring GetTaskIDPerUUID(LPCTSTR uuid /*= NULL */)
48 CRegStdDWORD r = CRegStdDWORD(_T("Software\\TortoiseGit\\GroupTaskbarIconsPerRepo"), 3);
49 std::wstring id = APPID;
50 if ((r < 2)||(r == 3))
52 wchar_t buf[MAX_PATH] = {0};
53 GetModuleFileName(NULL, buf, MAX_PATH);
54 std::wstring n = buf;
55 n = n.substr(n.find_last_of('\\'));
56 id += n;
59 if (r >= 3)
61 if (uuid)
63 id += uuid;
65 else
67 CCmdLineParser parser(GetCommandLine());
68 if (parser.HasVal(L"groupuuid"))
70 id += parser.GetVal(L"groupuuid");
74 return id;
77 #ifdef _MFC_VER
78 extern CString g_sGroupingUUID;
79 #endif
81 void SetUUIDOverlayIcon( HWND hWnd )
83 if (CRegStdDWORD(_T("Software\\TortoiseGit\\GroupTaskbarIconsPerRepo"), 3))
85 if (CRegStdDWORD(_T("Software\\TortoiseGit\\GroupTaskbarIconsPerRepoOverlay"), TRUE))
87 std::wstring uuid;
88 #ifdef _MFC_VER
89 uuid = g_sGroupingUUID;
90 #else
91 CCmdLineParser parser(GetCommandLine());
92 if (parser.HasVal(L"groupuuid"))
93 uuid = parser.GetVal(L"groupuuid");
94 #endif
95 if (!uuid.empty())
97 ITaskbarList3 * pTaskbarInterface = NULL;
98 HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList3, reinterpret_cast<void**> (&(pTaskbarInterface)));
100 if (SUCCEEDED(hr))
102 int foundUUIDIndex = 0;
105 wchar_t buf[MAX_PATH];
106 swprintf_s(buf, _countof(buf), L"%s%d", L"Software\\TortoiseGit\\LastUsedUUIDsForGrouping\\", foundUUIDIndex);
107 CRegStdString r = CRegStdString(buf);
108 std::wstring sr = r;
109 if (sr.empty() || (sr.compare(uuid)==0))
111 r = uuid;
112 break;
114 foundUUIDIndex++;
115 } while (foundUUIDIndex < 20);
116 if (foundUUIDIndex >= 20)
118 CRegStdString r = CRegStdString(L"Software\\TortoiseGit\\LastUsedUUIDsForGrouping\\1");
119 r.removeKey();
122 DWORD colors[6] = {0x80FF0000, 0x80FFFF00, 0x8000FF00, 0x800000FF, 0x80000000, 0x8000FFFF};
124 // AND mask - monochrome - determines which pixels get drawn
125 BYTE AND[32];
126 for( int i=0; i<32; i++ )
128 AND[i] = 0xFF;
131 // XOR mask - 32bpp ARGB - determines the pixel values
132 DWORD XOR[256];
133 for( int i=0; i<256; i++ )
135 XOR[i] = colors[foundUUIDIndex % 6];
138 HICON icon = ::CreateIcon(NULL,16,16,1,32,AND,(BYTE*)XOR);
139 pTaskbarInterface->SetOverlayIcon(hWnd, icon, uuid.c_str());
140 pTaskbarInterface->Release();
141 DestroyIcon(icon);