VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/runtime/seamless/UIMachineWindowSeamless.cpp@ 35740

Last change on this file since 35740 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1/* $Id: UIMachineWindowSeamless.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
2/** @file
3 *
4 * VBox frontends: Qt GUI ("VirtualBox"):
5 * UIMachineWindowSeamless class implementation
6 */
7
8/*
9 * Copyright (C) 2010 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/* Global includes */
21#include <QDesktopWidget>
22#include <QTimer>
23#ifdef Q_WS_MAC
24# include <QMenuBar>
25#endif /* Q_WS_MAC */
26
27/* Local includes */
28#include "VBoxGlobal.h"
29#ifndef Q_WS_MAC
30# include "VBoxMiniToolBar.h"
31#endif /* Q_WS_MAC */
32
33#include "UISession.h"
34#include "UIActionsPool.h"
35#include "UIMachineLogicSeamless.h"
36#include "UIMachineWindowSeamless.h"
37#include "UIMachineViewSeamless.h"
38
39#ifdef Q_WS_MAC
40# include "VBoxUtils.h"
41#endif /* Q_WS_MAC */
42
43UIMachineWindowSeamless::UIMachineWindowSeamless(UIMachineLogic *pMachineLogic, ulong uScreenId)
44 : QIWithRetranslateUI2<QMainWindow>(0, Qt::FramelessWindowHint)
45 , UIMachineWindow(pMachineLogic, uScreenId)
46 , m_pMainMenu(0)
47#ifndef Q_WS_MAC
48 , m_pMiniToolBar(0)
49#endif /* Q_WS_MAC */
50{
51 /* "This" is machine window: */
52 m_pMachineWindow = this;
53
54 /* Set the main window in VBoxGlobal: */
55 if (uScreenId == 0)
56 vboxGlobal().setMainWindow(this);
57
58 /* Prepare seamless window icon: */
59 prepareWindowIcon();
60
61 /* Prepare console connections: */
62 prepareConsoleConnections();
63
64 /* Prepare seamless window: */
65 prepareSeamless();
66
67 /* Prepare seamless menu: */
68 prepareMenu();
69
70 /* Prepare machine view container: */
71 prepareMachineViewContainer();
72
73 /* Prepare seamless machine view: */
74 prepareMachineView();
75
76 /* Prepare handlers: */
77 prepareHandlers();
78
79#ifndef Q_WS_MAC
80 /* Prepare mini tool-bar: */
81 prepareMiniToolBar();
82#endif /* Q_WS_MAC */
83
84 /* Retranslate fullscreen window finally: */
85 retranslateUi();
86
87#ifdef Q_WS_MAC
88 /* Load seamless window settings: */
89 loadWindowSettings();
90#endif /* Q_WS_MAC */
91
92 /* Update all the elements: */
93 updateAppearanceOf(UIVisualElement_AllStuff);
94
95 /* Show window: */
96 showSeamless();
97}
98
99UIMachineWindowSeamless::~UIMachineWindowSeamless()
100{
101 /* Save window settings: */
102 saveWindowSettings();
103
104#ifndef Q_WS_MAC
105 /* Cleanup mini tool-bar: */
106 cleanupMiniToolBar();
107#endif /* Q_WS_MAC */
108
109 /* Prepare handlers: */
110 cleanupHandlers();
111
112 /* Cleanup machine view: */
113 cleanupMachineView();
114
115 /* Cleanup menu: */
116 cleanupMenu();
117}
118
119void UIMachineWindowSeamless::sltPlaceOnScreen()
120{
121 /* Get corresponding screen: */
122 int iScreen = static_cast<UIMachineLogicSeamless*>(machineLogic())->hostScreenForGuestScreen(m_uScreenId);
123 /* Calculate working area: */
124 QRect workingArea = vboxGlobal().availableGeometry(iScreen);
125 /* Move to the appropriate position: */
126 move(workingArea.topLeft());
127 /* Resize to the appropriate size: */
128 resize(workingArea.size());
129 /* Process pending move & resize events: */
130 qApp->processEvents();
131}
132
133void UIMachineWindowSeamless::sltMachineStateChanged()
134{
135 UIMachineWindow::sltMachineStateChanged();
136}
137
138void UIMachineWindowSeamless::sltPopupMainMenu()
139{
140 /* Popup main menu if present: */
141 if (m_pMainMenu && !m_pMainMenu->isEmpty())
142 {
143 m_pMainMenu->popup(machineWindow()->geometry().center());
144 QTimer::singleShot(0, m_pMainMenu, SLOT(sltSelectFirstAction()));
145 }
146}
147
148#ifndef Q_WS_MAC
149void UIMachineWindowSeamless::sltUpdateMiniToolBarMask()
150{
151 if (m_pMiniToolBar)
152 setMask(qobject_cast<UIMachineViewSeamless*>(machineView())->lastVisibleRegion());
153}
154#endif /* Q_WS_MAC */
155
156void UIMachineWindowSeamless::sltTryClose()
157{
158 UIMachineWindow::sltTryClose();
159}
160
161void UIMachineWindowSeamless::retranslateUi()
162{
163 /* Translate parent class: */
164 UIMachineWindow::retranslateUi();
165}
166
167#ifdef Q_WS_MAC
168bool UIMachineWindowSeamless::event(QEvent *pEvent)
169{
170 switch (pEvent->type())
171 {
172 case QEvent::Paint:
173 {
174 /* Clear the background */
175 CGContextClearRect(::darwinToCGContextRef(this), ::darwinToCGRect(frameGeometry()));
176 break;
177 }
178 default:
179 break;
180 }
181 return QMainWindow::event(pEvent);
182}
183#endif /* Q_WS_MAC */
184
185#ifdef Q_WS_X11
186bool UIMachineWindowSeamless::x11Event(XEvent *pEvent)
187{
188 return UIMachineWindow::x11Event(pEvent);
189}
190#endif
191
192void UIMachineWindowSeamless::closeEvent(QCloseEvent *pEvent)
193{
194 return UIMachineWindow::closeEvent(pEvent);
195}
196
197void UIMachineWindowSeamless::prepareSeamless()
198{
199#ifdef Q_WS_WIN
200 /* Get corresponding screen: */
201 int iScreen = static_cast<UIMachineLogicSeamless*>(machineLogic())->hostScreenForGuestScreen(m_uScreenId);
202 /* Prepare previous region: */
203 m_prevRegion = vboxGlobal().availableGeometry(iScreen);
204#endif
205
206#ifdef Q_WS_MAC
207 /* Please note: All the stuff below has to be done after the window has
208 * switched to fullscreen. Qt changes the winId on the fullscreen
209 * switch and make this stuff useless with the old winId. So please be
210 * careful on rearrangement of the method calls. */
211 ::darwinSetShowsWindowTransparent(this, true);
212#endif
213}
214
215void UIMachineWindowSeamless::prepareMenu()
216{
217 UIMainMenuType fMenus = UIMainMenuType_All;
218 /* Remove the view menu in the case there is one screen only. */
219 if (QApplication::desktop()->numScreens() == 1)
220 fMenus = UIMainMenuType(fMenus ^ UIMainMenuType_View);
221#ifdef Q_WS_MAC
222 setMenuBar(uisession()->newMenuBar(fMenus));
223#endif /* Q_WS_MAC */
224 m_pMainMenu = uisession()->newMenu(fMenus);
225}
226
227#ifndef Q_WS_MAC
228void UIMachineWindowSeamless::prepareMiniToolBar()
229{
230 /* Get current machine: */
231 CMachine machine = session().GetConsole().GetMachine();
232 /* Check if mini tool-bar should present: */
233 bool fIsActive = machine.GetExtraData(VBoxDefs::GUI_ShowMiniToolBar) != "no";
234 if (fIsActive)
235 {
236 /* Get the mini tool-bar alignment: */
237 bool fIsAtTop = machine.GetExtraData(VBoxDefs::GUI_MiniToolBarAlignment) == "top";
238 /* Get the mini tool-bar auto-hide feature availability: */
239 bool fIsAutoHide = machine.GetExtraData(VBoxDefs::GUI_MiniToolBarAutoHide) != "off";
240 m_pMiniToolBar = new VBoxMiniToolBar(centralWidget(),
241 fIsAtTop ? VBoxMiniToolBar::AlignTop : VBoxMiniToolBar::AlignBottom,
242 true, fIsAutoHide);
243 m_pMiniToolBar->setSeamlessMode(true);
244 m_pMiniToolBar->updateDisplay(true, true);
245 QList<QMenu*> menus;
246 UIMainMenuType fMenu = UIMainMenuType(UIMainMenuType_Machine | UIMainMenuType_Devices);
247 if (QApplication::desktop()->numScreens() > 1)
248 fMenu = UIMainMenuType(fMenu | UIMainMenuType_View);
249 QList<QAction*> actions = uisession()->newMenu(fMenu)->actions();
250 for (int i=0; i < actions.size(); ++i)
251 menus << actions.at(i)->menu();
252 *m_pMiniToolBar << menus;
253 connect(m_pMiniToolBar, SIGNAL(minimizeAction()), this, SLOT(showMinimized()));
254 connect(m_pMiniToolBar, SIGNAL(exitAction()),
255 uisession()->actionsPool()->action(UIActionIndex_Toggle_Seamless), SLOT(trigger()));
256 connect(m_pMiniToolBar, SIGNAL(closeAction()),
257 uisession()->actionsPool()->action(UIActionIndex_Simple_Close), SLOT(trigger()));
258 connect(m_pMiniToolBar, SIGNAL(geometryUpdated()), this, SLOT(sltUpdateMiniToolBarMask()));
259 }
260}
261#endif /* Q_WS_MAC */
262
263void UIMachineWindowSeamless::prepareMachineView()
264{
265#ifdef VBOX_WITH_VIDEOHWACCEL
266 /* Need to force the QGL framebuffer in case 2D Video Acceleration is supported & enabled: */
267 bool bAccelerate2DVideo = session().GetMachine().GetAccelerate2DVideoEnabled() && VBoxGlobal::isAcceleration2DVideoAvailable();
268#endif
269
270 /* Set central widget: */
271 setCentralWidget(new QWidget);
272
273 /* Set central widget layout: */
274 centralWidget()->setLayout(m_pMachineViewContainer);
275
276 m_pMachineView = UIMachineView::create( this
277 , m_uScreenId
278 , machineLogic()->visualStateType()
279#ifdef VBOX_WITH_VIDEOHWACCEL
280 , bAccelerate2DVideo
281#endif
282 );
283
284 /* Add machine view into layout: */
285 m_pMachineViewContainer->addWidget(m_pMachineView, 1, 1, Qt::AlignVCenter | Qt::AlignHCenter);
286
287 /* This might be required to correctly mask: */
288 centralWidget()->setAutoFillBackground(false);
289}
290
291#ifdef Q_WS_MAC
292void UIMachineWindowSeamless::loadWindowSettings()
293{
294 /* Load global settings: */
295 {
296 VBoxGlobalSettings settings = vboxGlobal().settings();
297 menuBar()->setHidden(settings.isFeatureActive("noMenuBar"));
298 }
299}
300#endif
301
302void UIMachineWindowSeamless::saveWindowSettings()
303{
304#ifndef Q_WS_MAC
305 /* Get machine: */
306 CMachine machine = session().GetConsole().GetMachine();
307
308 /* Save extra-data settings: */
309 {
310 /* Save mini tool-bar settings: */
311 if (m_pMiniToolBar)
312 machine.SetExtraData(VBoxDefs::GUI_MiniToolBarAutoHide, m_pMiniToolBar->isAutoHide() ? QString() : "off");
313 }
314#endif /* Q_WS_MAC */
315}
316
317void UIMachineWindowSeamless::cleanupMachineView()
318{
319 /* Do not cleanup machine view if it is not present: */
320 if (!machineView())
321 return;
322
323 UIMachineView::destroy(m_pMachineView);
324 m_pMachineView = 0;
325}
326
327#ifndef Q_WS_MAC
328void UIMachineWindowSeamless::cleanupMiniToolBar()
329{
330 if (m_pMiniToolBar)
331 {
332 delete m_pMiniToolBar;
333 m_pMiniToolBar = 0;
334 }
335}
336#endif /* Q_WS_MAC */
337
338void UIMachineWindowSeamless::cleanupMenu()
339{
340 delete m_pMainMenu;
341 m_pMainMenu = 0;
342}
343
344void UIMachineWindowSeamless::updateAppearanceOf(int iElement)
345{
346 /* Base class update: */
347 UIMachineWindow::updateAppearanceOf(iElement);
348
349 /* If mini tool-bar is present: */
350#ifndef Q_WS_MAC
351 if (m_pMiniToolBar)
352 {
353 /* Get machine: */
354 CMachine machine = session().GetConsole().GetMachine();
355 /* Get snapshot(s): */
356 QString strSnapshotName;
357 if (machine.GetSnapshotCount() > 0)
358 {
359 CSnapshot snapshot = machine.GetCurrentSnapshot();
360 strSnapshotName = " (" + snapshot.GetName() + ")";
361 }
362 /* Update mini tool-bar text: */
363 m_pMiniToolBar->setDisplayText(machine.GetName() + strSnapshotName);
364 }
365#endif /* Q_WS_MAC */
366}
367
368void UIMachineWindowSeamless::showSeamless()
369{
370 /* Show manually maximized window: */
371 sltPlaceOnScreen();
372 show();
373
374#ifdef Q_WS_MAC
375 /* Make sure it is really on the right place (especially on the Mac): */
376 int iScreen = static_cast<UIMachineLogicSeamless*>(machineLogic())->hostScreenForGuestScreen(m_uScreenId);
377 QRect r = vboxGlobal().availableGeometry(iScreen);
378 move(r.topLeft());
379#endif /* Q_WS_MAC */
380}
381
382void UIMachineWindowSeamless::setMask(const QRegion &constRegion)
383{
384 QRegion region = constRegion;
385
386 /* Shift region if left spacer width is NOT zero or top spacer height is NOT zero: */
387 if (m_pLeftSpacer->geometry().width() || m_pTopSpacer->geometry().height())
388 region.translate(m_pLeftSpacer->geometry().width(), m_pTopSpacer->geometry().height());
389
390#if 0 // TODO: Is it really needed now?
391 /* The global mask shift cause of toolbars and such things. */
392 region.translate(mMaskShift.width(), mMaskShift.height());
393#endif
394
395 /* Mini tool-bar: */
396#ifndef Q_WS_MAC
397 if (m_pMiniToolBar)
398 {
399 /* Get mini-toolbar mask: */
400 QRegion toolBarRegion(m_pMiniToolBar->mask());
401
402 /* Move mini-toolbar mask to mini-toolbar position: */
403 toolBarRegion.translate(QPoint(m_pMiniToolBar->x(), m_pMiniToolBar->y()));
404
405 /* Including mini tool-bar mask: */
406 region += toolBarRegion;
407 }
408#endif /* Q_WS_MAC */
409
410#if 0 // TODO: Is it really needed now?
411 /* Restrict the drawing to the available space on the screen.
412 * (The &operator is better than the previous used -operator,
413 * because this excludes space around the real screen also.
414 * This is necessary for the mac.) */
415 region &= mStrictedRegion;
416#endif
417
418#ifdef Q_WS_WIN
419 QRegion difference = m_prevRegion.subtract(region);
420
421 /* Region offset calculation */
422 int fleft = 0, ftop = 0;
423
424 /* Visible region calculation */
425 HRGN newReg = CreateRectRgn(0, 0, 0, 0);
426 CombineRgn(newReg, region.handle(), 0, RGN_COPY);
427 OffsetRgn(newReg, fleft, ftop);
428
429 /* Invisible region calculation */
430 HRGN diffReg = CreateRectRgn(0, 0, 0, 0);
431 CombineRgn(diffReg, difference.handle(), 0, RGN_COPY);
432 OffsetRgn(diffReg, fleft, ftop);
433
434 /* Set the current visible region and clean the previous */
435 SetWindowRgn(winId(), newReg, FALSE);
436 RedrawWindow(0, 0, diffReg, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
437 RedrawWindow(machineView()->viewport()->winId(), 0, 0, RDW_INVALIDATE);
438
439 m_prevRegion = region;
440#elif defined (Q_WS_MAC)
441# if defined (VBOX_GUI_USE_QUARTZ2D)
442 if (vboxGlobal().vmRenderMode() == VBoxDefs::Quartz2DMode)
443 {
444 /* If we are using the Quartz2D backend we have to trigger
445 * an repaint only. All the magic clipping stuff is done
446 * in the paint engine. */
447 ::darwinWindowInvalidateShape(m_pMachineView->viewport());
448 }
449 else
450# endif
451 {
452 /* This is necessary to avoid the flicker by an mask update.
453 * See http://lists.apple.com/archives/Carbon-development/2001/Apr/msg01651.html
454 * for the hint.
455 * There *must* be a better solution. */
456 if (!region.isEmpty())
457 region |= QRect (0, 0, 1, 1);
458 // /* Save the current region for later processing in the darwin event handler. */
459 // mCurrRegion = region;
460 // /* We repaint the screen before the ReshapeCustomWindow command. Unfortunately
461 // * this command flushes a copy of the backbuffer to the screen after the new
462 // * mask is set. This leads into a misplaced drawing of the content. Currently
463 // * no alternative to this and also this is not 100% perfect. */
464 // repaint();
465 // qApp->processEvents();
466 // /* Now force the reshaping of the window. This is definitely necessary. */
467 // ReshapeCustomWindow (reinterpret_cast <WindowPtr> (winId()));
468 QMainWindow::setMask(region);
469 // HIWindowInvalidateShadow (::darwinToWindowRef (mConsole->viewport()));
470 }
471#else
472 QMainWindow::setMask(region);
473#endif
474}
475
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use