1 | /* $Id: UIMachineWindowFullscreen.cpp 50631 2014-02-27 14:46:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
5 | * UIMachineWindowFullscreen class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2010-2013 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* Qt includes: */
|
---|
21 | #include <QDesktopWidget>
|
---|
22 | #include <QMenu>
|
---|
23 | #include <QTimer>
|
---|
24 |
|
---|
25 | /* GUI includes: */
|
---|
26 | #include "VBoxGlobal.h"
|
---|
27 | #include "UISession.h"
|
---|
28 | #include "UIActionPoolRuntime.h"
|
---|
29 | #include "UIMachineLogicFullscreen.h"
|
---|
30 | #include "UIMachineWindowFullscreen.h"
|
---|
31 | #include "UIMachineView.h"
|
---|
32 | #include "UIMachineDefs.h"
|
---|
33 | #include "UIMiniToolBar.h"
|
---|
34 | #ifdef Q_WS_MAC
|
---|
35 | # include "VBoxUtils-darwin.h"
|
---|
36 | # include "UICocoaApplication.h"
|
---|
37 | #endif /* Q_WS_MAC */
|
---|
38 |
|
---|
39 | /* COM includes: */
|
---|
40 | #include "CSnapshot.h"
|
---|
41 |
|
---|
42 | UIMachineWindowFullscreen::UIMachineWindowFullscreen(UIMachineLogic *pMachineLogic, ulong uScreenId)
|
---|
43 | : UIMachineWindow(pMachineLogic, uScreenId)
|
---|
44 | , m_pMainMenu(0)
|
---|
45 | , m_pMiniToolBar(0)
|
---|
46 | {
|
---|
47 | }
|
---|
48 |
|
---|
49 | #ifdef Q_WS_MAC
|
---|
50 | void UIMachineWindowFullscreen::handleNativeNotification(const QString &strNativeNotificationName)
|
---|
51 | {
|
---|
52 | /* Make sure this method is only used for ML and next: */
|
---|
53 | AssertReturnVoid(vboxGlobal().osRelease() > MacOSXRelease_Lion);
|
---|
54 |
|
---|
55 | /* Handle arrived notification: */
|
---|
56 | LogRel(("UIMachineWindowFullscreen::handleNativeNotification: Notification '%s' received.\n",
|
---|
57 | strNativeNotificationName.toAscii().constData()));
|
---|
58 | /* Handle 'NSWindowDidEnterFullScreenNotification' notification: */
|
---|
59 | if (strNativeNotificationName == "NSWindowDidEnterFullScreenNotification")
|
---|
60 | {
|
---|
61 | LogRel(("UIMachineWindowFullscreen::handleNativeNotification: "
|
---|
62 | "Native fullscreen mode entered, notifying listener...\n"));
|
---|
63 | emit sigNotifyAboutNativeFullscreenDidEnter();
|
---|
64 | }
|
---|
65 | /* Handle 'NSWindowDidExitFullScreenNotification' notification: */
|
---|
66 | else if (strNativeNotificationName == "NSWindowDidExitFullScreenNotification")
|
---|
67 | {
|
---|
68 | LogRel(("UIMachineWindowFullscreen::handleNativeNotification: "
|
---|
69 | "Native fullscreen mode exited, notifying listener...\n"));
|
---|
70 | emit sigNotifyAboutNativeFullscreenDidExit();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | #endif /* Q_WS_MAC */
|
---|
74 |
|
---|
75 | void UIMachineWindowFullscreen::sltMachineStateChanged()
|
---|
76 | {
|
---|
77 | /* Call to base-class: */
|
---|
78 | UIMachineWindow::sltMachineStateChanged();
|
---|
79 |
|
---|
80 | /* Update mini-toolbar: */
|
---|
81 | updateAppearanceOf(UIVisualElement_MiniToolBar);
|
---|
82 | }
|
---|
83 |
|
---|
84 | void UIMachineWindowFullscreen::sltPopupMainMenu()
|
---|
85 | {
|
---|
86 | /* Popup main-menu if present: */
|
---|
87 | if (m_pMainMenu && !m_pMainMenu->isEmpty())
|
---|
88 | {
|
---|
89 | m_pMainMenu->popup(geometry().center());
|
---|
90 | QTimer::singleShot(0, m_pMainMenu, SLOT(sltHighlightFirstAction()));
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | void UIMachineWindowFullscreen::prepareMenu()
|
---|
95 | {
|
---|
96 | /* Call to base-class: */
|
---|
97 | UIMachineWindow::prepareMenu();
|
---|
98 |
|
---|
99 | /* Prepare menu: */
|
---|
100 | CMachine machine = session().GetMachine();
|
---|
101 | RuntimeMenuType restrictedMenus = VBoxGlobal::restrictedRuntimeMenuTypes(machine);
|
---|
102 | RuntimeMenuType allowedMenus = static_cast<RuntimeMenuType>(RuntimeMenuType_All ^ restrictedMenus);
|
---|
103 | m_pMainMenu = uisession()->newMenu(allowedMenus);
|
---|
104 | }
|
---|
105 |
|
---|
106 | void UIMachineWindowFullscreen::prepareVisualState()
|
---|
107 | {
|
---|
108 | /* Call to base-class: */
|
---|
109 | UIMachineWindow::prepareVisualState();
|
---|
110 |
|
---|
111 | /* The background has to go black: */
|
---|
112 | QPalette palette(centralWidget()->palette());
|
---|
113 | palette.setColor(centralWidget()->backgroundRole(), Qt::black);
|
---|
114 | centralWidget()->setPalette(palette);
|
---|
115 | centralWidget()->setAutoFillBackground(true);
|
---|
116 | setAutoFillBackground(true);
|
---|
117 |
|
---|
118 | /* Prepare mini-toolbar: */
|
---|
119 | prepareMiniToolbar();
|
---|
120 |
|
---|
121 | #ifdef Q_WS_MAC
|
---|
122 | /* Native fullscreen stuff on ML and next: */
|
---|
123 | if (vboxGlobal().osRelease() > MacOSXRelease_Lion)
|
---|
124 | {
|
---|
125 | /* Enable fullscreen support for every screen which requires it: */
|
---|
126 | if (darwinScreensHaveSeparateSpaces() || m_uScreenId == 0)
|
---|
127 | darwinEnableFullscreenSupport(this);
|
---|
128 | /* Enable transience support for other screens: */
|
---|
129 | else
|
---|
130 | darwinEnableTransienceSupport(this);
|
---|
131 | /* Register to native fullscreen notifications: */
|
---|
132 | UICocoaApplication::instance()->registerToNativeNotification("NSWindowDidEnterFullScreenNotification", this,
|
---|
133 | UIMachineWindow::handleNativeNotification);
|
---|
134 | UICocoaApplication::instance()->registerToNativeNotification("NSWindowDidExitFullScreenNotification", this,
|
---|
135 | UIMachineWindow::handleNativeNotification);
|
---|
136 | }
|
---|
137 | #endif /* Q_WS_MAC */
|
---|
138 | }
|
---|
139 |
|
---|
140 | void UIMachineWindowFullscreen::prepareMiniToolbar()
|
---|
141 | {
|
---|
142 | /* Get machine: */
|
---|
143 | CMachine m = machine();
|
---|
144 |
|
---|
145 | /* Make sure mini-toolbar is necessary: */
|
---|
146 | bool fIsActive = m.GetExtraData(GUI_ShowMiniToolBar) != "no";
|
---|
147 | if (!fIsActive)
|
---|
148 | return;
|
---|
149 |
|
---|
150 | /* Get the mini-toolbar alignment: */
|
---|
151 | bool fIsAtTop = m.GetExtraData(GUI_MiniToolBarAlignment) == "top";
|
---|
152 | /* Get the mini-toolbar auto-hide feature availability: */
|
---|
153 | bool fIsAutoHide = m.GetExtraData(GUI_MiniToolBarAutoHide) != "off";
|
---|
154 | /* Create mini-toolbar: */
|
---|
155 | m_pMiniToolBar = new UIRuntimeMiniToolBar(this,
|
---|
156 | fIsAtTop ? Qt::AlignTop : Qt::AlignBottom,
|
---|
157 | IntegrationMode_Embedded,
|
---|
158 | fIsAutoHide);
|
---|
159 | QList<QMenu*> menus;
|
---|
160 | RuntimeMenuType restrictedMenus = VBoxGlobal::restrictedRuntimeMenuTypes(m);
|
---|
161 | RuntimeMenuType allowedMenus = static_cast<RuntimeMenuType>(RuntimeMenuType_All ^ restrictedMenus);
|
---|
162 | QList<QAction*> actions = uisession()->newMenu(allowedMenus)->actions();
|
---|
163 | for (int i=0; i < actions.size(); ++i)
|
---|
164 | menus << actions.at(i)->menu();
|
---|
165 | m_pMiniToolBar->addMenus(menus);
|
---|
166 | #ifndef RT_OS_DARWIN
|
---|
167 | connect(m_pMiniToolBar, SIGNAL(sigMinimizeAction()), this, SLOT(showMinimized()));
|
---|
168 | #endif /* !RT_OS_DARWIN */
|
---|
169 | connect(m_pMiniToolBar, SIGNAL(sigExitAction()),
|
---|
170 | gActionPool->action(UIActionIndexRuntime_Toggle_Fullscreen), SLOT(trigger()));
|
---|
171 | connect(m_pMiniToolBar, SIGNAL(sigCloseAction()),
|
---|
172 | gActionPool->action(UIActionIndexRuntime_Simple_Close), SLOT(trigger()));
|
---|
173 | }
|
---|
174 |
|
---|
175 | void UIMachineWindowFullscreen::cleanupMiniToolbar()
|
---|
176 | {
|
---|
177 | /* Make sure mini-toolbar was created: */
|
---|
178 | if (!m_pMiniToolBar)
|
---|
179 | return;
|
---|
180 |
|
---|
181 | /* Save mini-toolbar settings: */
|
---|
182 | machine().SetExtraData(GUI_MiniToolBarAutoHide, m_pMiniToolBar->autoHide() ? QString() : "off");
|
---|
183 | /* Delete mini-toolbar: */
|
---|
184 | delete m_pMiniToolBar;
|
---|
185 | m_pMiniToolBar = 0;
|
---|
186 | }
|
---|
187 |
|
---|
188 | void UIMachineWindowFullscreen::cleanupVisualState()
|
---|
189 | {
|
---|
190 | #ifdef Q_WS_MAC
|
---|
191 | /* Native fullscreen stuff on ML and next: */
|
---|
192 | if (vboxGlobal().osRelease() > MacOSXRelease_Lion)
|
---|
193 | {
|
---|
194 | /* Unregister from native fullscreen notifications: */
|
---|
195 | UICocoaApplication::instance()->unregisterFromNativeNotification("NSWindowDidEnterFullScreenNotification", this);
|
---|
196 | UICocoaApplication::instance()->unregisterFromNativeNotification("NSWindowDidExitFullScreenNotification", this);
|
---|
197 | }
|
---|
198 | #endif /* Q_WS_MAC */
|
---|
199 |
|
---|
200 | /* Cleanup mini-toolbar: */
|
---|
201 | cleanupMiniToolbar();
|
---|
202 |
|
---|
203 | /* Call to base-class: */
|
---|
204 | UIMachineWindow::cleanupVisualState();
|
---|
205 | }
|
---|
206 |
|
---|
207 | void UIMachineWindowFullscreen::cleanupMenu()
|
---|
208 | {
|
---|
209 | /* Cleanup menu: */
|
---|
210 | delete m_pMainMenu;
|
---|
211 | m_pMainMenu = 0;
|
---|
212 |
|
---|
213 | /* Call to base-class: */
|
---|
214 | UIMachineWindow::cleanupMenu();
|
---|
215 | }
|
---|
216 |
|
---|
217 | void UIMachineWindowFullscreen::placeOnScreen()
|
---|
218 | {
|
---|
219 | /* Get corresponding screen: */
|
---|
220 | int iScreen = qobject_cast<UIMachineLogicFullscreen*>(machineLogic())->hostScreenForGuestScreen(m_uScreenId);
|
---|
221 | /* Calculate working area: */
|
---|
222 | QRect workingArea = QApplication::desktop()->screenGeometry(iScreen);
|
---|
223 | /* Move to the appropriate position: */
|
---|
224 | move(workingArea.topLeft());
|
---|
225 | #ifdef Q_WS_MAC
|
---|
226 | /* Resize to the appropriate size on Lion and previous: */
|
---|
227 | if (vboxGlobal().osRelease() <= MacOSXRelease_Lion)
|
---|
228 | resize(workingArea.size());
|
---|
229 | /* Resize to the appropriate size on ML and next
|
---|
230 | * only if that screen has no own user-space: */
|
---|
231 | else if (!darwinScreensHaveSeparateSpaces() && m_uScreenId != 0)
|
---|
232 | resize(workingArea.size());
|
---|
233 | #else /* !Q_WS_MAC */
|
---|
234 | /* Resize to the appropriate size: */
|
---|
235 | resize(workingArea.size());
|
---|
236 | #endif /* !Q_WS_MAC */
|
---|
237 | /* Adjust guest screen size if necessary: */
|
---|
238 | machineView()->maybeAdjustGuestScreenSize();
|
---|
239 | /* Move mini-toolbar into appropriate place: */
|
---|
240 | if (m_pMiniToolBar)
|
---|
241 | m_pMiniToolBar->adjustGeometry();
|
---|
242 | /* Process pending move & resize events: */
|
---|
243 | qApp->processEvents();
|
---|
244 | }
|
---|
245 |
|
---|
246 | void UIMachineWindowFullscreen::showInNecessaryMode()
|
---|
247 | {
|
---|
248 | /* Make sure this window should be shown at all: */
|
---|
249 | if (!uisession()->isScreenVisible(m_uScreenId))
|
---|
250 | return hide();
|
---|
251 |
|
---|
252 | /* Make sure this window has fullscreen logic: */
|
---|
253 | UIMachineLogicFullscreen *pFullscreenLogic = qobject_cast<UIMachineLogicFullscreen*>(machineLogic());
|
---|
254 | if (!pFullscreenLogic)
|
---|
255 | return hide();
|
---|
256 |
|
---|
257 | /* Make sure this window mapped to some host-screen: */
|
---|
258 | if (!pFullscreenLogic->hasHostScreenForGuestScreen(m_uScreenId))
|
---|
259 | return hide();
|
---|
260 |
|
---|
261 | /* Make sure this window is not minimized: */
|
---|
262 | if (isMinimized())
|
---|
263 | return;
|
---|
264 |
|
---|
265 | /* Make sure this window is maximized and placed on valid screen: */
|
---|
266 | placeOnScreen();
|
---|
267 |
|
---|
268 | #ifdef Q_WS_WIN
|
---|
269 | /* On Windows we should activate main window first,
|
---|
270 | * because entering fullscreen there doesn't means window will be auto-activated,
|
---|
271 | * so no window-activation event will be received and no keyboard-hook created otherwise... */
|
---|
272 | if (m_uScreenId == 0)
|
---|
273 | setWindowState(windowState() | Qt::WindowActive);
|
---|
274 | #endif /* Q_WS_WIN */
|
---|
275 |
|
---|
276 | #ifdef Q_WS_MAC
|
---|
277 | /* ML and next using native stuff, so we can call for simple show(): */
|
---|
278 | if (vboxGlobal().osRelease() > MacOSXRelease_Lion) show();
|
---|
279 | /* Lion and previous using Qt stuff, so we should call for showFullScreen(): */
|
---|
280 | else showFullScreen();
|
---|
281 | #else /* !Q_WS_MAC */
|
---|
282 | /* Show in fullscreen mode: */
|
---|
283 | showFullScreen();
|
---|
284 | #endif /* !Q_WS_MAC */
|
---|
285 |
|
---|
286 | #ifdef Q_WS_X11
|
---|
287 | /* Make sure the window is placed on valid screen again
|
---|
288 | * after window is shown & window's decorations applied.
|
---|
289 | * That is required (still?) due to X11 Window Geometry Rules. */
|
---|
290 | placeOnScreen();
|
---|
291 | #endif /* Q_WS_X11 */
|
---|
292 | }
|
---|
293 |
|
---|
294 | void UIMachineWindowFullscreen::updateAppearanceOf(int iElement)
|
---|
295 | {
|
---|
296 | /* Call to base-class: */
|
---|
297 | UIMachineWindow::updateAppearanceOf(iElement);
|
---|
298 |
|
---|
299 | /* Update mini-toolbar: */
|
---|
300 | if (iElement & UIVisualElement_MiniToolBar)
|
---|
301 | {
|
---|
302 | if (m_pMiniToolBar)
|
---|
303 | {
|
---|
304 | /* Get machine: */
|
---|
305 | const CMachine &m = machine();
|
---|
306 | /* Get snapshot(s): */
|
---|
307 | QString strSnapshotName;
|
---|
308 | if (m.GetSnapshotCount() > 0)
|
---|
309 | {
|
---|
310 | CSnapshot snapshot = m.GetCurrentSnapshot();
|
---|
311 | strSnapshotName = " (" + snapshot.GetName() + ")";
|
---|
312 | }
|
---|
313 | /* Update mini-toolbar text: */
|
---|
314 | m_pMiniToolBar->setText(m.GetName() + strSnapshotName);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|