1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library 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 GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
23 #define WIN32_LEAN_AND_MEAN 1
27 #include <MUtils/GUI.h>
28 #include <MUtils/OSSupport.h>
31 #include "Utils_Win32.h"
35 #include <QApplication>
37 #include <QReadWriteLock>
41 ///////////////////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////////
45 static QReadWriteLock g_themes_lock;
46 static bool g_themes_initialized = false;
47 static bool g_themes_enabled = false;
49 typedef int (WINAPI *IsAppThemedFunction)(void);
51 bool MUtils::GUI::themes_enabled(void)
53 QReadLocker readLock(&g_themes_lock);
55 if(g_themes_initialized)
57 return g_themes_enabled;
61 QWriteLocker writeLock(&g_themes_lock);
63 if(g_themes_initialized)
65 return g_themes_enabled;
68 const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
69 if(osVersion >= MUtils::OS::Version::WINDOWS_WINXP)
71 IsAppThemedFunction IsAppThemedPtr = NULL;
72 QLibrary uxTheme("UxTheme.dll");
75 IsAppThemedPtr = (IsAppThemedFunction) uxTheme.resolve("IsAppThemed");
79 g_themes_enabled = IsAppThemedPtr();
82 qWarning("Theme support is disabled for this process!");
87 g_themes_initialized = true;
88 return g_themes_enabled;
91 ///////////////////////////////////////////////////////////////////////////////
93 ///////////////////////////////////////////////////////////////////////////////
95 bool MUtils::GUI::sysmenu_append(const QWidget *win, const unsigned int identifier, const QString &text)
99 if(HMENU hMenu = GetSystemMenu(win->winId(), FALSE))
101 ok = (AppendMenuW(hMenu, MF_SEPARATOR, 0, 0) == TRUE);
102 ok = (AppendMenuW(hMenu, MF_STRING, identifier, MUTILS_WCHR(text)) == TRUE);
108 bool MUtils::GUI::sysmenu_update(const QWidget *win, const unsigned int identifier, const QString &text)
112 if(HMENU hMenu = ::GetSystemMenu(win->winId(), FALSE))
114 ok = (ModifyMenu(hMenu, identifier, MF_STRING | MF_BYCOMMAND, identifier, MUTILS_WCHR(text)) == TRUE);
119 bool MUtils::GUI::sysmenu_check_msg(void *const message, const unsigned int &identifier)
121 return (((MSG*)message)->message == WM_SYSCOMMAND) && ((((MSG*)message)->wParam & 0xFFF0) == identifier);
124 ///////////////////////////////////////////////////////////////////////////////
126 ///////////////////////////////////////////////////////////////////////////////
128 bool MUtils::GUI::enable_close_button(const QWidget *win, const bool &bEnable)
132 if(HMENU hMenu = GetSystemMenu(win->winId(), FALSE))
134 ok = (EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | (bEnable ? MF_ENABLED : MF_GRAYED)) == TRUE);
140 ///////////////////////////////////////////////////////////////////////////////
141 // BRING WINDOW TO FRONT
142 ///////////////////////////////////////////////////////////////////////////////
144 static BOOL CALLBACK bring_process_to_front_helper(HWND hwnd, LPARAM lParam)
146 DWORD processId = *reinterpret_cast<WORD*>(lParam);
147 DWORD windowProcessId = NULL;
148 GetWindowThreadProcessId(hwnd, &windowProcessId);
149 if(windowProcessId == processId)
151 SwitchToThisWindow(hwnd, TRUE);
152 SetForegroundWindow(hwnd);
159 bool MUtils::GUI::bring_to_front(const QWidget *window)
165 for(int i = 0; (i < 5) && (!ret); i++)
167 ret = (SetForegroundWindow(window->winId()) != FALSE);
168 SwitchToThisWindow(window->winId(), TRUE);
170 LockSetForegroundWindow(LSFW_LOCK);
176 bool MUtils::GUI::bring_to_front(const unsigned long pid)
178 return EnumWindows(bring_process_to_front_helper, reinterpret_cast<LPARAM>(&pid)) == TRUE;
181 ///////////////////////////////////////////////////////////////////////////////
182 // SHEET OF GLASS EFFECT
183 ///////////////////////////////////////////////////////////////////////////////
185 static QReadWriteLock g_dwmapi_lock;
186 static QScopedPointer<QLibrary> g_dwmapi_library;
187 static bool g_dwmapi_initialized = false;
191 HRESULT (__stdcall *dwmIsCompositionEnabled)(BOOL *bEnabled);
192 HRESULT (__stdcall *dwmExtendFrameIntoClientArea)(HWND hWnd, const MARGINS* pMarInset);
193 HRESULT (__stdcall *dwmEnableBlurBehindWindow)(HWND hWnd, const DWM_BLURBEHIND* pBlurBehind);
195 g_dwmapi_pointers = { NULL, NULL, NULL };
197 static void initialize_dwmapi(void)
199 QReadLocker writeLock(&g_dwmapi_lock);
201 //Not initialized yet?
202 if(g_dwmapi_initialized)
207 //Reset function pointers
208 g_dwmapi_pointers.dwmIsCompositionEnabled = NULL;
209 g_dwmapi_pointers.dwmExtendFrameIntoClientArea = NULL;
210 g_dwmapi_pointers.dwmEnableBlurBehindWindow = NULL;
212 //Does OS support DWM?
213 const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
214 if(osVersion >= MUtils::OS::Version::WINDOWS_VISTA)
217 g_dwmapi_library.reset(new QLibrary("dwmapi.dll"));
218 if(g_dwmapi_library->load())
220 //Initialize function pointers
221 g_dwmapi_pointers.dwmIsCompositionEnabled = (HRESULT (__stdcall*)(BOOL*)) g_dwmapi_library->resolve("DwmIsCompositionEnabled");
222 g_dwmapi_pointers.dwmExtendFrameIntoClientArea = (HRESULT (__stdcall*)(HWND, const MARGINS*)) g_dwmapi_library->resolve("DwmExtendFrameIntoClientArea");
223 g_dwmapi_pointers.dwmEnableBlurBehindWindow = (HRESULT (__stdcall*)(HWND, const DWM_BLURBEHIND*)) g_dwmapi_library->resolve("DwmEnableBlurBehindWindow");
227 g_dwmapi_library.reset(NULL);
228 qWarning("Failed to load DWMAPI.DLL on a DWM-enabled system!");
232 g_dwmapi_initialized = true;
235 bool MUtils::GUI::sheet_of_glass(QWidget *const window)
237 QReadLocker readLock(&g_dwmapi_lock);
239 //Initialize the DWM API
240 if(!g_dwmapi_initialized)
247 //Required functions available?
248 BOOL bCompositionEnabled = FALSE;
249 if(g_dwmapi_pointers.dwmIsCompositionEnabled && g_dwmapi_pointers.dwmExtendFrameIntoClientArea && g_dwmapi_pointers.dwmEnableBlurBehindWindow)
251 //Check if composition is currently enabled
252 if(HRESULT hr = g_dwmapi_pointers.dwmIsCompositionEnabled(&bCompositionEnabled))
254 qWarning("DwmIsCompositionEnabled function has failed! (error %d)", hr);
259 //All functions available *and* composition enabled?
260 if(!bCompositionEnabled)
265 //Enable the "sheet of glass" effect on this window
266 MARGINS margins = {-1, -1, -1, -1};
267 if(HRESULT hr = g_dwmapi_pointers.dwmExtendFrameIntoClientArea(window->winId(), &margins))
269 qWarning("DwmExtendFrameIntoClientArea function has failed! (error %d)", hr);
273 //Create and populate the Blur Behind structure
275 memset(&bb, 0, sizeof(DWM_BLURBEHIND));
277 bb.dwFlags = DWM_BB_ENABLE;
278 if(HRESULT hr = g_dwmapi_pointers.dwmEnableBlurBehindWindow(window->winId(), &bb))
280 qWarning("DwmEnableBlurBehindWindow function has failed! (error %d)", hr);
285 window->setAutoFillBackground(false);
286 window->setAttribute(Qt::WA_TranslucentBackground);
287 window->setAttribute(Qt::WA_NoSystemBackground);
292 bool MUtils::GUI::sheet_of_glass_update(QWidget *const window)
294 QReadLocker readLock(&g_dwmapi_lock);
296 //Initialize the DWM API
297 if(!g_dwmapi_initialized)
304 //Required functions available?
305 BOOL bCompositionEnabled = FALSE;
306 if(g_dwmapi_pointers.dwmIsCompositionEnabled && g_dwmapi_pointers.dwmExtendFrameIntoClientArea && g_dwmapi_pointers.dwmEnableBlurBehindWindow)
308 //Check if composition is currently enabled
309 if(HRESULT hr = g_dwmapi_pointers.dwmIsCompositionEnabled(&bCompositionEnabled))
311 qWarning("DwmIsCompositionEnabled function has failed! (error %d)", hr);
316 //All functions available *and* composition enabled?
317 if(!bCompositionEnabled)
322 //Create and populate the Blur Behind structure
324 memset(&bb, 0, sizeof(DWM_BLURBEHIND));
326 bb.dwFlags = DWM_BB_ENABLE;
327 if(HRESULT hr = g_dwmapi_pointers.dwmEnableBlurBehindWindow(window->winId(), &bb))
329 qWarning("DwmEnableBlurBehindWindow function has failed! (error %d)", hr);
336 ///////////////////////////////////////////////////////////////////////////////
338 ///////////////////////////////////////////////////////////////////////////////
340 QColor MUtils::GUI::system_color(const int &color_id)
347 nIndex = COLOR_WINDOWTEXT; /*Text in windows*/
349 case SYSCOLOR_BACKGROUND:
350 nIndex = COLOR_WINDOW; /*Window background*/
352 case SYSCOLOR_CAPTION:
353 nIndex = COLOR_CAPTIONTEXT; /*Text in caption, size box, and scroll bar arrow box*/
356 qWarning("Unknown system color id (%d) specified!", color_id);
357 nIndex = COLOR_WINDOWTEXT;
360 const DWORD rgb = GetSysColor(nIndex);
361 QColor color(GetRValue(rgb), GetGValue(rgb), GetBValue(rgb));