[26714] | 1 | /* $Id: VBoxUtils-win.cpp 99837 2023-05-18 10:03:39Z vboxsync $ */
|
---|
[25177] | 2 | /** @file
|
---|
[71784] | 3 | * VBox Qt GUI - Declarations of utility classes and functions for handling Windows specific tasks.
|
---|
[25177] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[25177] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[25177] | 26 | */
|
---|
| 27 |
|
---|
[99837] | 28 | /* Qt includes: */
|
---|
| 29 | #include <QList>
|
---|
| 30 | #include <QRect>
|
---|
| 31 |
|
---|
[71784] | 32 | /* GUI includes: */
|
---|
[35940] | 33 | #include "VBoxUtils-win.h"
|
---|
[25177] | 34 |
|
---|
[71784] | 35 |
|
---|
| 36 | /** Namespace for native window sub-system functions. */
|
---|
[35940] | 37 | namespace NativeWindowSubsystem
|
---|
[25177] | 38 | {
|
---|
[71784] | 39 | /** Enumerates visible always-on-top (top-most) windows. */
|
---|
[85121] | 40 | BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) RT_NOTHROW_PROTO;
|
---|
[71784] | 41 | /** Contains visible top-most-window rectangles. */
|
---|
[35940] | 42 | QList<QRect> topMostRects;
|
---|
[25177] | 43 | }
|
---|
| 44 |
|
---|
[85121] | 45 | BOOL CALLBACK NativeWindowSubsystem::EnumWindowsProc(HWND hWnd, LPARAM) RT_NOTHROW_DEF
|
---|
[25177] | 46 | {
|
---|
[35906] | 47 | /* Ignore NULL HWNDs: */
|
---|
| 48 | if (!hWnd)
|
---|
| 49 | return TRUE;
|
---|
| 50 |
|
---|
| 51 | /* Ignore hidden windows: */
|
---|
| 52 | if (!IsWindowVisible(hWnd))
|
---|
| 53 | return TRUE;
|
---|
| 54 |
|
---|
| 55 | /* Get window style: */
|
---|
| 56 | LONG uStyle = GetWindowLong(hWnd, GWL_STYLE);
|
---|
| 57 | /* Ignore minimized windows: */
|
---|
| 58 | if (uStyle & WS_MINIMIZE)
|
---|
| 59 | return TRUE;
|
---|
| 60 |
|
---|
| 61 | /* Get extended window style: */
|
---|
| 62 | LONG uExtendedStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
|
---|
| 63 | /* Ignore non-top-most windows: */
|
---|
| 64 | if (!(uExtendedStyle & WS_EX_TOPMOST))
|
---|
| 65 | return TRUE;
|
---|
| 66 |
|
---|
| 67 | /* Get that window rectangle: */
|
---|
| 68 | RECT rect;
|
---|
| 69 | GetWindowRect(hWnd, &rect);
|
---|
[35940] | 70 | topMostRects << QRect(QPoint(rect.left, rect.top), QPoint(rect.right - 1, rect.bottom - 1));
|
---|
[35906] | 71 |
|
---|
| 72 | /* Proceed to the next window: */
|
---|
| 73 | return TRUE;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[35940] | 76 | const QRegion NativeWindowSubsystem::areaCoveredByTopMostWindows()
|
---|
[35906] | 77 | {
|
---|
| 78 | /* Prepare the top-most region: */
|
---|
| 79 | QRegion topMostRegion;
|
---|
| 80 | /* Initialize the list of the top-most rectangles: */
|
---|
[35940] | 81 | topMostRects.clear();
|
---|
[35906] | 82 | /* Populate the list of top-most rectangles: */
|
---|
| 83 | EnumWindows((WNDENUMPROC)EnumWindowsProc, 0);
|
---|
| 84 | /* Update the top-most region with top-most rectangles: */
|
---|
[35940] | 85 | for (int iRectIndex = 0; iRectIndex < topMostRects.size(); ++iRectIndex)
|
---|
| 86 | topMostRegion += topMostRects[iRectIndex];
|
---|
[35906] | 87 | /* Return top-most region: */
|
---|
| 88 | return topMostRegion;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[89740] | 91 | const void NativeWindowSubsystem::setScreenSaverActive(BOOL fDisableScreenSaver)
|
---|
| 92 | {
|
---|
| 93 | BOOL fIsActive;
|
---|
| 94 | SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &fIsActive, 0);
|
---|
| 95 | if (fIsActive == !fDisableScreenSaver)
|
---|
| 96 | return;
|
---|
| 97 | //printf("before %d\n", fIsActive);
|
---|
| 98 |
|
---|
| 99 | SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, !fDisableScreenSaver, NULL, 0);
|
---|
| 100 |
|
---|
| 101 | SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &fIsActive, 0);
|
---|
| 102 | /*if (fIsActive == !fDisableScreenSaver)
|
---|
| 103 | printf("success %d %d\n", fIsActive, fDisableScreenSaver);
|
---|
| 104 | */
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[91079] | 107 | BOOL NativeWindowSubsystem::ShutdownBlockReasonCreateAPI(HWND hWnd, LPCWSTR pwszReason)
|
---|
| 108 | {
|
---|
| 109 | BOOL fResult = FALSE;
|
---|
| 110 | typedef BOOL(WINAPI *PFNSHUTDOWNBLOCKREASONCREATE)(HWND hWnd, LPCWSTR pwszReason);
|
---|
| 111 |
|
---|
| 112 | PFNSHUTDOWNBLOCKREASONCREATE pfn = (PFNSHUTDOWNBLOCKREASONCREATE)GetProcAddress(
|
---|
| 113 | GetModuleHandle(L"User32.dll"), "ShutdownBlockReasonCreate");
|
---|
| 114 | _ASSERTE(pfn);
|
---|
| 115 | if (pfn)
|
---|
| 116 | fResult = pfn(hWnd, pwszReason);
|
---|
| 117 | return fResult;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | bool NativeWindowSubsystem::WinActivateWindow(WId wId, bool)
|
---|
| 121 | {
|
---|
| 122 | bool fResult = true;
|
---|
| 123 | HWND handle = (HWND)wId;
|
---|
| 124 |
|
---|
| 125 | if (IsIconic(handle))
|
---|
| 126 | fResult &= !!ShowWindow(handle, SW_RESTORE);
|
---|
| 127 | else if (!IsWindowVisible(handle))
|
---|
| 128 | fResult &= !!ShowWindow(handle, SW_SHOW);
|
---|
| 129 |
|
---|
| 130 | fResult &= !!SetForegroundWindow(handle);
|
---|
| 131 | return fResult;
|
---|
| 132 | }
|
---|