Sync TortoiseIDiff and TortoiseUDiff from TortoiseSVN
[TortoiseGit.git] / src / Utils / MiscUI / BaseWindow.cpp
blob5d7aa551b8c1276adbd4a69b1bd754fa557fdc98
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2009, 2012-2013 - 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.
20 #include "stdafx.h"
21 #include "BaseWindow.h"
22 #include <memory>
23 #include <Shlwapi.h>
25 #pragma comment(lib, "shlwapi.lib")
28 bool CWindow::RegisterWindow(UINT style, HICON hIcon, HCURSOR hCursor, HBRUSH hbrBackground,
29 LPCTSTR lpszMenuName, LPCTSTR lpszClassName, HICON hIconSm)
31 WNDCLASSEX wcx;
33 // Fill in the window class structure with default parameters
35 wcx.cbSize = sizeof(WNDCLASSEX); // size of structure
36 wcx.style = style; // redraw if size changes
37 wcx.lpfnWndProc = CWindow::stWinMsgHandler; // points to window procedure
38 wcx.cbClsExtra = 0; // no extra class memory
39 wcx.cbWndExtra = 0; // no extra window memory
40 wcx.hInstance = hResource; // handle to instance
41 wcx.hIcon = hIcon; // predefined app. icon
42 wcx.hCursor = hCursor; // predefined arrow
43 wcx.hbrBackground = hbrBackground; // white background brush
44 wcx.lpszMenuName = lpszMenuName; // name of menu resource
45 wcx.lpszClassName = lpszClassName; // name of window class
46 wcx.hIconSm = hIconSm; // small class icon
48 // Register the window class.
49 return RegisterWindow(&wcx);
52 bool CWindow::RegisterWindow(CONST WNDCLASSEX* wcx)
54 // Register the window class.
55 sClassName = std::wstring(wcx->lpszClassName);
57 if (RegisterClassEx(wcx) == 0)
59 if (GetLastError() == ERROR_CLASS_ALREADY_EXISTS)
60 return TRUE;
61 return FALSE;
63 else
64 return TRUE;
67 LRESULT CALLBACK CWindow::stWinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
69 CWindow * pWnd = nullptr;
71 if (uMsg == WM_NCCREATE)
73 // get the pointer to the window from lpCreateParams which was set in CreateWindow
74 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)((LPCREATESTRUCT(lParam))->lpCreateParams));
77 // get the pointer to the window
78 pWnd = GetObjectFromWindow(hwnd);
80 // if we have the pointer, go to the message handler of the window
81 // else, use DefWindowProc
82 if (pWnd)
84 switch (uMsg)
86 case WM_ACTIVATE:
87 if ((wParam == WA_ACTIVE) && (!pWnd->bWindowRestored) && (!pWnd->sRegistryPath.empty()))
89 WINDOWPLACEMENT wpl = {0};
90 DWORD size = sizeof(wpl);
91 if (SHGetValue(HKEY_CURRENT_USER, pWnd->sRegistryPath.c_str(), pWnd->sRegistryValue.c_str(), REG_NONE, &wpl, &size) == ERROR_SUCCESS)
92 SetWindowPlacement(hwnd, &wpl);
93 else
94 ShowWindow(hwnd, SW_SHOW);
95 pWnd->bWindowRestored = true;
97 break;
98 case WM_CLOSE:
99 if (!pWnd->sRegistryPath.empty())
101 WINDOWPLACEMENT wpl = {0};
102 wpl.length = sizeof(WINDOWPLACEMENT);
103 GetWindowPlacement(hwnd, &wpl);
104 SHSetValue(HKEY_CURRENT_USER, pWnd->sRegistryPath.c_str(), pWnd->sRegistryValue.c_str(), REG_NONE, &wpl, sizeof(wpl));
106 break;
108 return pWnd->WinMsgHandler(hwnd, uMsg, wParam, lParam);
110 else
111 return DefWindowProc(hwnd, uMsg, wParam, lParam);
114 bool CWindow::Create()
116 // Create the window
117 RECT rect;
119 rect.top = 0;
120 rect.left = 0;
121 rect.right = 600;
122 rect.bottom = 400;
124 return Create(WS_OVERLAPPEDWINDOW | WS_VISIBLE, NULL, &rect);
127 bool CWindow::Create(DWORD dwStyles, HWND hParent /* = NULL */, RECT* rect /* = NULL */)
129 return CreateEx(0, dwStyles, hParent, rect);
132 bool CWindow::CreateEx(DWORD dwExStyles, DWORD dwStyles, HWND hParent /* = NULL */, RECT* rect /* = NULL */, LPCTSTR classname /* = NULL */)
134 // send the this pointer as the window creation parameter
135 if (rect == NULL)
136 m_hwnd = CreateWindowEx(dwExStyles, classname ? classname : sClassName.c_str(), sWindowTitle.c_str(), dwStyles, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hParent, NULL, hResource, (void *)this);
137 else
139 m_hwnd = CreateWindowEx(dwExStyles, classname ? classname : sClassName.c_str(), sWindowTitle.c_str(), dwStyles, rect->left, rect->top,
140 rect->right - rect->left, rect->bottom - rect->top, hParent, NULL, hResource,
141 (void *)this);
143 m_hParent = hParent;
144 return (m_hwnd != NULL);
147 void CWindow::SetTransparency(BYTE alpha, COLORREF color /* = 0xFF000000 */)
149 if (alpha == 255)
151 LONG_PTR exstyle = GetWindowLongPtr(*this, GWL_EXSTYLE);
152 exstyle &= ~WS_EX_LAYERED;
153 SetWindowLongPtr(*this, GWL_EXSTYLE, exstyle);
155 else
157 LONG_PTR exstyle = GetWindowLongPtr(*this, GWL_EXSTYLE);
158 exstyle |= WS_EX_LAYERED;
159 SetWindowLongPtr(*this, GWL_EXSTYLE, exstyle);
161 COLORREF col = color;
162 DWORD flags = LWA_ALPHA;
163 if (col & 0xFF000000)
165 col = RGB(255, 255, 255);
166 flags = LWA_ALPHA;
168 else
170 flags = LWA_COLORKEY;
172 SetLayeredWindowAttributes(*this, col, alpha, flags);