Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp	(revision 26729)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.cpp	(revision 26730)
@@ -157,49 +157,4 @@
     }
     return view;
-}
-
-void UIMachineView::onViewOpened()
-{
-    /* Variable m_bIsMachineWindowResizeIgnored was initially "true" to ignore QT
-     * initial resize event in case of auto-resize feature is on.
-     * Currently, initial resize event is already processed, so we set
-     * m_bIsMachineWindowResizeIgnored to "false" to process all further resize
-     * events as user-initiated window resize events. */
-    m_bIsMachineWindowResizeIgnored = false;
-}
-
-int UIMachineView::contentsX() const
-{
-    return horizontalScrollBar()->value();
-}
-
-int UIMachineView::contentsY() const
-{
-    return verticalScrollBar()->value();
-}
-
-QRect UIMachineView::desktopGeometry() const
-{
-    QRect rc;
-    switch (mDesktopGeo)
-    {
-        case DesktopGeo_Fixed:
-        case DesktopGeo_Automatic:
-            rc = QRect(0, 0,
-                       qMax(mDesktopGeometry.width(), mStoredConsoleSize.width()),
-                       qMax(mDesktopGeometry.height(), mStoredConsoleSize.height()));
-            break;
-        case DesktopGeo_Any:
-            rc = QRect(0, 0, 0, 0);
-            break;
-        default:
-            AssertMsgFailed(("Bad geometry type %d\n", mDesktopGeo));
-    }
-    return rc;
-}
-
-void UIMachineView::setIgnoreGuestResize(bool bIgnore)
-{
-    m_bIsGuestResizeIgnored = bIgnore;
 }
 
@@ -285,286 +240,8 @@
     , mHideHostPointer (true)
 {
-    Assert (!m_console.isNull() && !m_console.GetDisplay().isNull() && !m_console.GetKeyboard().isNull() && !m_console.GetMouse().isNull());
-
-#ifdef Q_WS_MAC
-    /* Overlay logo for the dock icon */
-    //mVirtualBoxLogo = ::darwinToCGImageRef("VirtualBox_cube_42px.png");
-    QString osTypeId = m_console.GetGuest().GetOSTypeId();
-
-    // TODO_NEW_CORE
-//    mDockIconPreview = new VBoxDockIconPreview(machineWindowWrapper(), vboxGlobal().vmGuestOSTypeIcon (osTypeId));
-
-# ifdef QT_MAC_USE_COCOA
-    /** @todo Carbon -> Cocoa */
-# else /* !QT_MAC_USE_COCOA */
-    /* Install the event handler which will proceed external window handling */
-    EventHandlerUPP eventHandler = ::NewEventHandlerUPP(::darwinOverlayWindowHandler);
-    EventTypeSpec eventTypes[] =
-    {
-        { kEventClassVBox, kEventVBoxShowWindow },
-        { kEventClassVBox, kEventVBoxHideWindow },
-        { kEventClassVBox, kEventVBoxMoveWindow },
-        { kEventClassVBox, kEventVBoxResizeWindow },
-        { kEventClassVBox, kEventVBoxDisposeWindow },
-        { kEventClassVBox, kEventVBoxUpdateDock }
-    };
-
-    mDarwinWindowOverlayHandlerRef = NULL;
-    ::InstallApplicationEventHandler(eventHandler, RT_ELEMENTS (eventTypes), &eventTypes[0], this, &mDarwinWindowOverlayHandlerRef);
-    ::DisposeEventHandlerUPP(eventHandler);
-# endif /* !QT_MAC_USE_COCOA */
-#endif /* QT_WS_MAC */
-
-    /* No frame around the view */
-    setFrameStyle(QFrame::NoFrame);
-
-#ifdef VBOX_GUI_USE_QGL
-    QWidget *pViewport;
-    switch (mode)
-    {
-        // TODO: Fix that!
-        //case VBoxDefs::QGLMode:
-        //    pViewport = new VBoxGLWidget(this, this, NULL);
-        //    break;
-        default:
-            pViewport = new VBoxViewport(this);
-    }
-#else
-    VBoxViewport *pViewport = new VBoxViewport(this);
-#endif
-    setViewport(pViewport);
-//    pViewport->vboxDoInit();
-
-    /* enable MouseMove events */
-    viewport()->setMouseTracking(true);
-
-    /*
-     *  QScrollView does the below on its own, but let's do it anyway
-     *  for the case it will not do it in the future.
-     */
-    viewport()->installEventFilter(this);
-
-    /* to fix some focus issues */
-    // TODO: Fix that!
-    //machineWindowWrapper()->menuBar()->installEventFilter(this);
-
-    /* we want to be notified on some parent's events */
-    machineWindowWrapper()->machineWindow()->installEventFilter(this);
-
-#ifdef Q_WS_X11
-    /* initialize the X keyboard subsystem */
-    initMappedX11Keyboard(QX11Info::display(), vboxGlobal().settings().publicProperty("GUI/RemapScancodes"));
-#endif
-
-    ::memset(mPressedKeys, 0, sizeof(mPressedKeys));
-
-    /* setup rendering */
-
-    CDisplay display = m_console.GetDisplay();
-    Assert(!display.isNull());
-
-    mFrameBuf = NULL;
-
-    LogFlowFunc(("Rendering mode: %d\n", mode));
-
-    switch (mode)
-    {
-#if defined (VBOX_GUI_USE_QGL)
-// TODO: Fix that!
-//        case VBoxDefs::QGLMode:
-//            mFrameBuf = new VBoxQGLFrameBuffer(this);
-//            break;
-//        case VBoxDefs::QGLOverlayMode:
-//            mFrameBuf = new VBoxQGLOverlayFrameBuffer(this);
-//            break;
-#endif
-#if defined (VBOX_GUI_USE_QIMAGE)
-        case VBoxDefs::QImageMode:
-            mFrameBuf =
-#ifdef VBOX_WITH_VIDEOHWACCEL
-//                    mAccelerate2DVideo ? new VBoxOverlayFrameBuffer<UIFrameBufferQImage>(this, &machineWindowWrapper()->session()) :
-#endif
-                    new UIFrameBufferQImage(this);
-            break;
-#endif
-#if defined (VBOX_GUI_USE_SDL)
-        case VBoxDefs::SDLMode:
-            /* Indicate that we are doing all
-             * drawing stuff ourself */
-            pViewport->setAttribute(Qt::WA_PaintOnScreen);
-# ifdef Q_WS_X11
-            /* This is somehow necessary to prevent strange X11 warnings on
-             * i386 and segfaults on x86_64. */
-            XFlush(QX11Info::display());
-# endif
-//            mFrameBuf =
-#if defined(VBOX_WITH_VIDEOHWACCEL) && defined(DEBUG_misha) /* not tested yet */
-//                mAccelerate2DVideo ? new VBoxOverlayFrameBuffer<VBoxSDLFrameBuffer> (this, &machineWindowWrapper()->session()) :
-#endif
-//                new VBoxSDLFrameBuffer(this);
-            /*
-             *  disable scrollbars because we cannot correctly draw in a
-             *  scrolled window using SDL
-             */
-            horizontalScrollBar()->setEnabled(false);
-            verticalScrollBar()->setEnabled(false);
-            break;
-#endif
-#if defined (VBOX_GUI_USE_DDRAW)
-        case VBoxDefs::DDRAWMode:
-            mFrameBuf = new VBoxDDRAWFrameBuffer(this);
-            break;
-#endif
-#if defined (VBOX_GUI_USE_QUARTZ2D)
-        case VBoxDefs::Quartz2DMode:
-            /* Indicate that we are doing all
-             * drawing stuff ourself */
-            pViewport->setAttribute(Qt::WA_PaintOnScreen);
-//            mFrameBuf =
-#ifdef VBOX_WITH_VIDEOHWACCEL
-              // TODO_NEW_CORE
-//                    mAccelerate2DVideo ? new VBoxOverlayFrameBuffer<VBoxQuartz2DFrameBuffer>(this, &machineWindowWrapper()->session()) :
-#endif
-//                  new UIFrameBufferQuartz2D(this);
-            break;
-#endif
-        default:
-            AssertReleaseMsgFailed(("Render mode must be valid: %d\n", mode));
-            LogRel (("Invalid render mode: %d\n", mode));
-            qApp->exit (1);
-            break;
-    }
-
-#if defined (VBOX_GUI_USE_DDRAW)
-    if (!mFrameBuf || mFrameBuf->address() == NULL)
-    {
-        if (mFrameBuf)
-            delete mFrameBuf;
-        mode = VBoxDefs::QImageMode;
-        mFrameBuf = new UIFrameBufferQImage(this);
-    }
-#endif
-
-    if (mFrameBuf)
-    {
-        mFrameBuf->AddRef();
-        display.SetFramebuffer(VBOX_VIDEO_PRIMARY_SCREEN, CFramebuffer(mFrameBuf));
-    }
-
-    QPalette palette(viewport()->palette());
-    palette.setColor(viewport()->backgroundRole(), Qt::black);
-    viewport()->setPalette(palette);
-
-    setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum));
-    setMaximumSize(sizeHint());
-
-    setFocusPolicy(Qt::WheelFocus);
-
-    /* Remember the desktop geometry and register for geometry change
-       events for telling the guest about video modes we like. */
-
-    QString desktopGeometry = vboxGlobal().settings().publicProperty("GUI/MaxGuestResolution");
-    if ((desktopGeometry == QString::null) || (desktopGeometry == "auto"))
-        setDesktopGeometry(DesktopGeo_Automatic, 0, 0);
-    else if (desktopGeometry == "any")
-        setDesktopGeometry(DesktopGeo_Any, 0, 0);
-    else
-    {
-        int width = desktopGeometry.section(',', 0, 0).toInt();
-        int height = desktopGeometry.section(',', 1, 1).toInt();
-        setDesktopGeometry(DesktopGeo_Fixed, width, height);
-    }
-    connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(doResizeDesktop(int)));
-
-    QString passCAD = m_console.GetMachine().GetExtraData(VBoxDefs::GUI_PassCAD);
-    if (!passCAD.isEmpty() && ((passCAD != "false") || (passCAD != "no")))
-        mPassCAD = true;
-
-#if defined (Q_WS_WIN)
-    gView = this;
-#endif
-
-#if defined (Q_WS_PM)
-    bool ok = VBoxHlpInstallKbdHook(0, winId(), UM_PREACCEL_CHAR);
-    Assert (ok);
-    NOREF (ok);
-#endif
-
-#ifdef Q_WS_MAC
-    /* Dock icon update connection */
-    connect(&vboxGlobal(), SIGNAL(dockIconUpdateChanged(const VBoxChangeDockIconUpdateEvent &)),
-            this, SLOT(sltChangeDockIconUpdate(const VBoxChangeDockIconUpdateEvent &)));
-    /* Presentation Mode connection */
-    connect(&vboxGlobal(), SIGNAL(presentationModeChanged(const VBoxChangePresentationModeEvent &)),
-            this, SLOT(sltChangePresentationMode(const VBoxChangePresentationModeEvent &)));
-#endif
 }
 
 UIMachineView::~UIMachineView()
 {
-#if 0
-#if defined (Q_WS_PM)
-    bool ok = VBoxHlpUninstallKbdHook(0, winId(), UM_PREACCEL_CHAR);
-    Assert (ok);
-    NOREF (ok);
-#endif
-
-#if defined (Q_WS_WIN)
-    if (gKbdHook)
-        UnhookWindowsHookEx(gKbdHook);
-    gView = 0;
-    if (mAlphaCursor)
-        DestroyIcon(mAlphaCursor);
-#endif
-
-    if (mFrameBuf)
-    {
-        /* detach our framebuffer from Display */
-        CDisplay display = m_console.GetDisplay();
-        Assert(!display.isNull());
-        display.SetFramebuffer(VBOX_VIDEO_PRIMARY_SCREEN, CFramebuffer(NULL));
-        /* release the reference */
-        mFrameBuf->Release();
-        mFrameBuf = NULL;
-    }
-
-#if defined (Q_WS_MAC)
-# if !defined (QT_MAC_USE_COCOA)
-    if (mDarwinWindowOverlayHandlerRef)
-    {
-        ::RemoveEventHandler(mDarwinWindowOverlayHandlerRef);
-        mDarwinWindowOverlayHandlerRef = NULL;
-    }
-# endif
-    delete mDockIconPreview;
-    mDockIconPreview = NULL;
-#endif
-#endif
-}
-
-void UIMachineView::calculateDesktopGeometry()
-{
-    /* This method should not get called until we have initially set up the */
-    Assert((mDesktopGeo != DesktopGeo_Invalid));
-    /* If we are not doing automatic geometry calculation then there is nothing to do. */
-    if (DesktopGeo_Automatic == mDesktopGeo)
-    {
-        /* Available geometry of the desktop.  If the desktop is a single
-         * screen, this will exclude space taken up by desktop taskbars
-         * and things, but this is unfortunately not true for the more
-         * complex case of a desktop spanning multiple screens. */
-        QRect desktop = availableGeometry();
-        /* The area taken up by the console window on the desktop,
-         * including window frame, title and menu bar and whatnot. */
-        QRect frame = machineWindowWrapper()->machineWindow()->frameGeometry();
-        /* The area taken up by the console window, minus all decorations. */
-        //QRect window = machineWindowWrapper()->centralWidget()->geometry(); // TODO check that!
-        /* To work out how big we can make the console window while still
-         * fitting on the desktop, we calculate desktop - frame + window.
-         * This works because the difference between frame and window
-         * (or at least its width and height) is a constant. */
-        //mDesktopGeometry = QRect(0, 0, desktop.width() - frame.width() + window.width(),
-        //                               desktop.height() - frame.height() + window.height());
-    }
 }
 
@@ -584,4 +261,348 @@
 }
 
+int UIMachineView::contentsX() const
+{
+    return horizontalScrollBar()->value();
+}
+
+int UIMachineView::contentsY() const
+{
+    return verticalScrollBar()->value();
+}
+
+int UIMachineView::contentsWidth() const
+{
+    return mFrameBuf->width();
+}
+
+int UIMachineView::contentsHeight() const
+{
+    return mFrameBuf->height();
+}
+
+int UIMachineView::visibleWidth() const
+{
+    return horizontalScrollBar()->pageStep();
+}
+
+int UIMachineView::visibleHeight() const
+{
+    return verticalScrollBar()->pageStep();
+}
+
+void UIMachineView::calculateDesktopGeometry()
+{
+    /* This method should not get called until we have initially set up the mDesktopGeo: */
+    Assert((mDesktopGeo != DesktopGeo_Invalid));
+    /* If we are not doing automatic geometry calculation then there is nothing to do: */
+    if (DesktopGeo_Automatic == mDesktopGeo)
+    {
+        /* Available geometry of the desktop.  If the desktop is a single
+         * screen, this will exclude space taken up by desktop taskbars
+         * and things, but this is unfortunately not true for the more
+         * complex case of a desktop spanning multiple screens: */
+        QRect desktop = availableGeometry();
+        /* The area taken up by the console window on the desktop,
+         * including window frame, title and menu bar and whatnot. */
+        QRect frame = machineWindowWrapper()->machineWindow()->frameGeometry();
+        /* The area taken up by the console window, minus all decorations. */
+        QRect window = geometry();
+        /* To work out how big we can make the console window while still
+         * fitting on the desktop, we calculate desktop - frame + window.
+         * This works because the difference between frame and window
+         * (or at least its width and height) is a constant. */
+        mDesktopGeometry = QRect(0, 0, desktop.width() - frame.width() + window.width(),
+                                       desktop.height() - frame.height() + window.height());
+    }
+}
+
+QRect UIMachineView::desktopGeometry() const
+{
+    QRect rc;
+    switch (mDesktopGeo)
+    {
+        case DesktopGeo_Fixed:
+        case DesktopGeo_Automatic:
+            rc = QRect(0, 0,
+                       qMax(mDesktopGeometry.width(), mStoredConsoleSize.width()),
+                       qMax(mDesktopGeometry.height(), mStoredConsoleSize.height()));
+            break;
+        case DesktopGeo_Any:
+            rc = QRect(0, 0, 0, 0);
+            break;
+        default:
+            AssertMsgFailed(("Bad geometry type %d\n", mDesktopGeo));
+    }
+    return rc;
+}
+
+void UIMachineView::prepareFrameBuffer()
+{
+    CDisplay display = m_console.GetDisplay();
+    Assert(!display.isNull());
+    mFrameBuf = NULL;
+
+    switch (mode)
+    {
+#if defined (VBOX_GUI_USE_QIMAGE)
+        case VBoxDefs::QImageMode:
+#if 0 // TODO: Enable QImage + Video Acceleration!
+# ifdef VBOX_WITH_VIDEOHWACCEL
+            mFrameBuf = mAccelerate2DVideo ? new VBoxOverlayFrameBuffer<UIFrameBufferQImage>(this, &machineWindowWrapper()->session()) : new UIFrameBufferQImage(this);
+# else
+            mFrameBuf = new UIFrameBufferQImage(this);
+# endif
+#endif
+            mFrameBuf = new UIFrameBufferQImage(this);
+            break;
+#endif
+#if 0 // TODO: Enable OpenGL frame buffer!
+#if defined (VBOX_GUI_USE_QGL)
+        case VBoxDefs::QGLMode:
+            mFrameBuf = new UIQGLFrameBuffer(this);
+            break;
+        case VBoxDefs::QGLOverlayMode:
+            mFrameBuf = new UIQGLOverlayFrameBuffer(this);
+            break;
+#endif
+#endif
+#if 0 // TODO: Enable SDL frame buffer!
+#if defined (VBOX_GUI_USE_SDL)
+        case VBoxDefs::SDLMode:
+            /* Indicate that we are doing all drawing stuff ourself: */
+            pViewport->setAttribute(Qt::WA_PaintOnScreen);
+# ifdef Q_WS_X11
+            /* This is somehow necessary to prevent strange X11 warnings on i386 and segfaults on x86_64: */
+            XFlush(QX11Info::display());
+# endif
+# if defined(VBOX_WITH_VIDEOHWACCEL) && defined(DEBUG_misha) /* not tested yet */
+            mFrameBuf = mAccelerate2DVideo ? new VBoxOverlayFrameBuffer<UISDLFrameBuffer> (this, &machineWindowWrapper()->session()) : new UISDLFrameBuffer(this);
+# else
+            mFrameBuf = new UISDLFrameBuffer(this);
+# endif
+            /* Disable scrollbars because we cannot correctly draw in a scrolled window using SDL: */
+            horizontalScrollBar()->setEnabled(false);
+            verticalScrollBar()->setEnabled(false);
+            break;
+#endif
+#endif
+#if 0 // TODO: Enable DDraw frame buffer!
+#if defined (VBOX_GUI_USE_DDRAW)
+        case VBoxDefs::DDRAWMode:
+            mFrameBuf = new UIDDRAWFrameBuffer(this);
+            if (!mFrameBuf || mFrameBuf->address() == NULL)
+            {
+                if (mFrameBuf)
+                    delete mFrameBuf;
+                mode = VBoxDefs::QImageMode;
+                mFrameBuf = new UIFrameBufferQImage(this);
+            }
+            break;
+#endif
+#endif
+#if 0 // TODO: Enable Quartz2D frame buffer!
+#if defined (VBOX_GUI_USE_QUARTZ2D)
+        case VBoxDefs::Quartz2DMode:
+            /* Indicate that we are doing all drawing stuff ourself: */
+            pViewport->setAttribute(Qt::WA_PaintOnScreen);
+# ifdef VBOX_WITH_VIDEOHWACCEL
+            mFrameBuf = mAccelerate2DVideo ? new VBoxOverlayFrameBuffer<VBoxQuartz2DFrameBuffer>(this, &machineWindowWrapper()->session()) : new UIFrameBufferQuartz2D(this);
+# else
+            mFrameBuf = new UIFrameBufferQuartz2D(this);
+# endif
+            break;
+#endif
+#endif
+        default:
+            AssertReleaseMsgFailed(("Render mode must be valid: %d\n", mode));
+            LogRel(("Invalid render mode: %d\n", mode));
+            qApp->exit(1);
+            break;
+    }
+    if (mFrameBuf)
+    {
+        mFrameBuf->AddRef();
+        display.SetFramebuffer(VBOX_VIDEO_PRIMARY_SCREEN, CFramebuffer(mFrameBuf));
+    }
+}
+
+void UIMachineView::prepareCommon()
+{
+    /* Prepare view frame: */
+    setFrameStyle(QFrame::NoFrame);
+
+    /* Pressed keys: */
+    ::memset(mPressedKeys, 0, sizeof(mPressedKeys));
+
+    /* Prepare viewport: */
+#ifdef VBOX_GUI_USE_QGL
+    QWidget *pViewport;
+    switch (mode)
+    {
+#if 0 // TODO: Create Open GL viewport!
+        case VBoxDefs::QGLMode:
+            pViewport = new VBoxGLWidget(this, this, NULL);
+            break;
+#endif
+        default:
+            pViewport = new VBoxViewport(this);
+    }
+#else
+    VBoxViewport *pViewport = new VBoxViewport(this);
+#endif
+    setViewport(pViewport);
+
+    /* Setup palette: */
+    QPalette palette(viewport()->palette());
+    palette.setColor(viewport()->backgroundRole(), Qt::black);
+    viewport()->setPalette(palette);
+
+    /* Setup size-policy: */
+    setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum));
+    setMaximumSize(sizeHint());
+
+    /* Setup focus policy: */
+    setFocusPolicy(Qt::WheelFocus);
+
+#if defined Q_WS_WIN
+    gView = this;
+#endif
+
+#if defined Q_WS_PM
+    bool ok = VBoxHlpInstallKbdHook(0, winId(), UM_PREACCEL_CHAR);
+    Assert(ok);
+    NOREF(ok);
+#endif
+
+#if defined Q_WS_MAC
+    /* Dock icon update connection */
+    connect(&vboxGlobal(), SIGNAL(dockIconUpdateChanged(const VBoxChangeDockIconUpdateEvent &)),
+            this, SLOT(sltChangeDockIconUpdate(const VBoxChangeDockIconUpdateEvent &)));
+    /* Presentation Mode connection */
+    connect(&vboxGlobal(), SIGNAL(presentationModeChanged(const VBoxChangePresentationModeEvent &)),
+            this, SLOT(sltChangePresentationMode(const VBoxChangePresentationModeEvent &)));
+
+    /* Overlay logo for the dock icon */
+    //mVirtualBoxLogo = ::darwinToCGImageRef("VirtualBox_cube_42px.png");
+    QString osTypeId = m_console.GetGuest().GetOSTypeId();
+
+    // TODO_NEW_CORE
+//    mDockIconPreview = new VBoxDockIconPreview(machineWindowWrapper(), vboxGlobal().vmGuestOSTypeIcon (osTypeId));
+
+# ifdef QT_MAC_USE_COCOA
+    /** @todo Carbon -> Cocoa */
+# else /* !QT_MAC_USE_COCOA */
+    /* Install the event handler which will proceed external window handling */
+    EventHandlerUPP eventHandler = ::NewEventHandlerUPP(::darwinOverlayWindowHandler);
+    EventTypeSpec eventTypes[] =
+    {
+        { kEventClassVBox, kEventVBoxShowWindow },
+        { kEventClassVBox, kEventVBoxHideWindow },
+        { kEventClassVBox, kEventVBoxMoveWindow },
+        { kEventClassVBox, kEventVBoxResizeWindow },
+        { kEventClassVBox, kEventVBoxDisposeWindow },
+        { kEventClassVBox, kEventVBoxUpdateDock }
+    };
+
+    mDarwinWindowOverlayHandlerRef = NULL;
+    ::InstallApplicationEventHandler(eventHandler, RT_ELEMENTS (eventTypes), &eventTypes[0], this, &mDarwinWindowOverlayHandlerRef);
+    ::DisposeEventHandlerUPP(eventHandler);
+# endif /* !QT_MAC_USE_COCOA */
+#endif
+}
+
+void UIMachineView::prepareFilters()
+{
+    /* Enable MouseMove events: */
+    viewport()->setMouseTracking(true);
+
+    /* QScrollView does the below on its own, but let's
+     * do it anyway for the case it will not do it in the future: */
+    viewport()->installEventFilter(this);
+
+    /* To fix some focus issues: */
+    //qobject_cast<QMainWindow*>(machineWindowWrapper()->machineWindow())->menuBar()->installEventFilter(this);
+
+    /* We want to be notified on some parent's events: */
+    machineWindowWrapper()->machineWindow()->installEventFilter(this);
+}
+
+void UIMachineView::loadMachineViewSettings()
+{
+    /* Global settings: */
+    {
+        /* Remember the desktop geometry and register for geometry
+         * change events for telling the guest about video modes we like: */
+        QString desktopGeometry = vboxGlobal().settings().publicProperty("GUI/MaxGuestResolution");
+        if ((desktopGeometry == QString::null) || (desktopGeometry == "auto"))
+            setDesktopGeometry(DesktopGeo_Automatic, 0, 0);
+        else if (desktopGeometry == "any")
+            setDesktopGeometry(DesktopGeo_Any, 0, 0);
+        else
+        {
+            int width = desktopGeometry.section(',', 0, 0).toInt();
+            int height = desktopGeometry.section(',', 1, 1).toInt();
+            setDesktopGeometry(DesktopGeo_Fixed, width, height);
+        }
+        connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(doResizeDesktop(int)));
+
+#ifdef Q_WS_X11
+        /* Initialize the X keyboard subsystem: */
+        initMappedX11Keyboard(QX11Info::display(), vboxGlobal().settings().publicProperty("GUI/RemapScancodes"));
+#endif
+    }
+
+    /* Exatra data settings: */
+    {
+        /* CAD settings: */
+        QString passCAD = m_console.GetMachine().GetExtraData(VBoxDefs::GUI_PassCAD);
+        if (!passCAD.isEmpty() && ((passCAD != "false") || (passCAD != "no")))
+            mPassCAD = true;
+    }
+}
+
+void UIMachineView::cleanupCommon()
+{
+#if defined (Q_WS_PM)
+    bool ok = VBoxHlpUninstallKbdHook(0, winId(), UM_PREACCEL_CHAR);
+    Assert(ok);
+    NOREF(ok);
+#endif
+
+#if defined (Q_WS_WIN)
+    if (gKbdHook)
+        UnhookWindowsHookEx(gKbdHook);
+    gView = 0;
+    if (mAlphaCursor)
+        DestroyIcon(mAlphaCursor);
+#endif
+
+#if defined (Q_WS_MAC)
+# if !defined (QT_MAC_USE_COCOA)
+    if (mDarwinWindowOverlayHandlerRef)
+    {
+        ::RemoveEventHandler(mDarwinWindowOverlayHandlerRef);
+        mDarwinWindowOverlayHandlerRef = NULL;
+    }
+# endif
+    delete mDockIconPreview;
+    mDockIconPreview = NULL;
+#endif
+}
+
+void UIMachineView::cleanupFrameBuffer()
+{
+    /* Cleanup: */
+    if (mFrameBuf)
+    {
+        /* Detach framebuffer from Display: */
+        CDisplay display = console().GetDisplay();
+        display.SetFramebuffer(VBOX_VIDEO_PRIMARY_SCREEN, CFramebuffer(NULL));
+        /* Release the reference: */
+        mFrameBuf->Release();
+        mFrameBuf = NULL;
+    }
+}
+
 #ifdef Q_WS_MAC
 void UIMachineView::sltChangeDockIconUpdate(const VBoxChangeDockIconUpdateEvent &event)
@@ -617,24 +638,4 @@
 # endif /* QT_MAC_USE_COCOA */
 #endif
-
-int UIMachineView::contentsWidth() const
-{
-    return mFrameBuf->width();
-}
-
-int UIMachineView::contentsHeight() const
-{
-    return mFrameBuf->height();
-}
-
-int UIMachineView::visibleWidth() const
-{
-    return horizontalScrollBar()->pageStep();
-}
-
-int UIMachineView::visibleHeight() const
-{
-    return verticalScrollBar()->pageStep();
-}
 
 bool UIMachineView::event(QEvent *e)
@@ -2509,70 +2510,4 @@
 #endif
 
-#if defined(Q_WS_MAC)
-void UIMachineView::updateDockIcon()
-{
-    if (mDockIconEnabled)
-    {
-        if (!mPausedShot.isNull())
-        {
-            CGImageRef pauseImg = ::darwinToCGImageRef (&mPausedShot);
-            /* Use the pause image as background */
-            mDockIconPreview->updateDockPreview (pauseImg);
-            CGImageRelease (pauseImg);
-        }
-        else
-        {
-# if defined (VBOX_GUI_USE_QUARTZ2D)
-                // TODO_NEW_CORE
-//            if (mode == VBoxDefs::Quartz2DMode)
-//            {
-                /* If the render mode is Quartz2D we could use the CGImageRef
-                 * of the framebuffer for the dock icon creation. This saves
-                 * some conversion time. */
-//                mDockIconPreview->updateDockPreview (static_cast <VBoxQuartz2DFrameBuffer *> (mFrameBuf)->imageRef());
-//            }
-//            else
-# endif
-                /* In image mode we have to create the image ref out of the
-                 * framebuffer */
-                // TODO_NEW_CORE
-//                mDockIconPreview->updateDockPreview (mFrameBuf);
-        }
-    }
-}
-
-void UIMachineView::updateDockOverlay()
-{
-    /* Only to an update to the realtime preview if this is enabled by the user
-     * & we are in an state where the framebuffer is likely valid. Otherwise to
-     * the overlay stuff only. */
-    if (mDockIconEnabled &&
-        (mLastState == KMachineState_Running ||
-         mLastState == KMachineState_Paused ||
-         mLastState == KMachineState_Teleporting ||
-         mLastState == KMachineState_LiveSnapshotting ||
-         mLastState == KMachineState_Restoring ||
-         mLastState == KMachineState_TeleportingPausedVM ||
-         mLastState == KMachineState_TeleportingIn ||
-         mLastState == KMachineState_Saving))
-        updateDockIcon();
-    else
-        mDockIconPreview->updateDockOverlay();
-}
-
-void UIMachineView::setMouseCoalescingEnabled (bool aOn)
-{
-    /* Enable mouse event compression if we leave the VM view. This
-       is necessary for having smooth resizing of the VM/other
-       windows.
-       Disable mouse event compression if we enter the VM view. So
-       all mouse events are registered in the VM. Only do this if
-       the keyboard/mouse is grabbed (this is when we have a valid
-       event handler). */
-    if (aOn || mKeyboardGrabbed)
-        ::darwinSetMouseCoalescingEnabled (aOn);
-}
-#endif /* Q_WS_MAC */
-
 void UIMachineView::onStateChange(KMachineState state)
 {
@@ -3227,2 +3162,68 @@
 }
 
+#if defined(Q_WS_MAC)
+void UIMachineView::updateDockIcon()
+{
+    if (mDockIconEnabled)
+    {
+        if (!mPausedShot.isNull())
+        {
+            CGImageRef pauseImg = ::darwinToCGImageRef (&mPausedShot);
+            /* Use the pause image as background */
+            mDockIconPreview->updateDockPreview (pauseImg);
+            CGImageRelease (pauseImg);
+        }
+        else
+        {
+# if defined (VBOX_GUI_USE_QUARTZ2D)
+                // TODO_NEW_CORE
+//            if (mode == VBoxDefs::Quartz2DMode)
+//            {
+                /* If the render mode is Quartz2D we could use the CGImageRef
+                 * of the framebuffer for the dock icon creation. This saves
+                 * some conversion time. */
+//                mDockIconPreview->updateDockPreview (static_cast <VBoxQuartz2DFrameBuffer *> (mFrameBuf)->imageRef());
+//            }
+//            else
+# endif
+                /* In image mode we have to create the image ref out of the
+                 * framebuffer */
+                // TODO_NEW_CORE
+//                mDockIconPreview->updateDockPreview (mFrameBuf);
+        }
+    }
+}
+
+void UIMachineView::updateDockOverlay()
+{
+    /* Only to an update to the realtime preview if this is enabled by the user
+     * & we are in an state where the framebuffer is likely valid. Otherwise to
+     * the overlay stuff only. */
+    if (mDockIconEnabled &&
+        (mLastState == KMachineState_Running ||
+         mLastState == KMachineState_Paused ||
+         mLastState == KMachineState_Teleporting ||
+         mLastState == KMachineState_LiveSnapshotting ||
+         mLastState == KMachineState_Restoring ||
+         mLastState == KMachineState_TeleportingPausedVM ||
+         mLastState == KMachineState_TeleportingIn ||
+         mLastState == KMachineState_Saving))
+        updateDockIcon();
+    else
+        mDockIconPreview->updateDockOverlay();
+}
+
+void UIMachineView::setMouseCoalescingEnabled (bool aOn)
+{
+    /* Enable mouse event compression if we leave the VM view. This
+       is necessary for having smooth resizing of the VM/other
+       windows.
+       Disable mouse event compression if we enter the VM view. So
+       all mouse events are registered in the VM. Only do this if
+       the keyboard/mouse is grabbed (this is when we have a valid
+       event handler). */
+    if (aOn || mKeyboardGrabbed)
+        ::darwinSetMouseCoalescingEnabled (aOn);
+}
+#endif /* Q_WS_MAC */
+
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h	(revision 26729)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineView.h	(revision 26730)
@@ -59,60 +59,61 @@
                                  , UIVisualStateType visualStateType);
 
-    /* Adjust view geometry: */
+    /* Public setters: */
+    void setIgnoreGuestResize(bool bIgnore) { m_bIsGuestResizeIgnored = bIgnore; }
+    void setMouseIntegrationEnabled(bool bEnabled);
     virtual void normalizeGeometry(bool bAdjustPosition = false) = 0;
-
-    /* Public members: */
-    // TODO: Is it needed now?
-    void onViewOpened();
-
-    /* Public getters: */
+    //void setMachineViewFinalized(bool fTrue = true) { m_bIsMachineWindowResizeIgnored = !fTrue; }
+
+signals:
+
+    /* Machine view signals: */
+    void keyboardStateChanged(int iState);
+    void mouseStateChanged(int iState);
+
+    /* Utility signals: */
+    void resizeHintDone();
+
+protected slots:
+
+    /* Initiate resize request to guest: */
+    virtual void doResizeHint(const QSize &aSize = QSize()) = 0;
+
+protected:
+
+    UIMachineView(  UIMachineWindow *pMachineWindow
+                  , VBoxDefs::RenderMode renderMode
+#ifdef VBOX_WITH_VIDEOHWACCEL
+                  , bool bAccelerate2DVideo
+#endif
+    );
+    virtual ~UIMachineView();
+
+    /* Protected getters: */
+    UIMachineWindow* machineWindowWrapper() { return m_pMachineWindow; }
+    CConsole &console() { return m_console; }
+
+    /* Protected members: */
+    QSize sizeHint() const;
     int contentsX() const;
     int contentsY() const;
+    int contentsWidth() const;
+    int contentsHeight() const;
+    int visibleWidth() const;
+    int visibleHeight() const;
+    void calculateDesktopGeometry();
     QRect desktopGeometry() const;
-    bool isMouseAbsolute() const { return m_bIsMouseAbsolute; }
-
-    /* Public setters: */
-    void setIgnoreGuestResize(bool bIgnore);
-    void setMouseIntegrationEnabled(bool bEnabled);
-
-#if defined(Q_WS_MAC)
-    void updateDockIcon();
-    void updateDockOverlay();
-    void setDockIconEnabled(bool aOn) { mDockIconEnabled = aOn; };
-    void setMouseCoalescingEnabled(bool aOn);
-#endif
-
-signals:
-
-    void keyboardStateChanged(int iState);
-    void mouseStateChanged(int iState);
-    void machineStateChanged(KMachineState state);
-    void additionsStateChanged(const QString &strVersion, bool bIsActive, bool bIsGraphicSupported, bool bIsSeamlessSupported);
-    void mediaDriveChanged(VBoxDefs::MediumType type);
-    void networkStateChange();
-    void usbStateChange();
-    void sharedFoldersChanged();
-    void resizeHintDone();
-
-protected slots:
-
-    virtual void doResizeHint(const QSize &aSize = QSize()) = 0;
-
-protected:
-
-    UIMachineView(  UIMachineWindow *pMachineWindow
-                  , VBoxDefs::RenderMode renderMode
-#ifdef VBOX_WITH_VIDEOHWACCEL
-                  , bool bAccelerate2DVideo
-#endif
-    );
-    virtual ~UIMachineView();
-
-    /* Protected members: */
-    void calculateDesktopGeometry();
-
-    /* Protected getters: */
-    UIMachineWindow* machineWindowWrapper() { return m_pMachineWindow; }
-    QSize sizeHint() const;
+    //bool isMouseAbsolute() const { return m_bIsMouseAbsolute; }
+
+    /* Prepare routines: */
+    void prepareFrameBuffer();
+    void prepareCommon();
+    void prepareFilters();
+    void loadMachineViewSettings();
+
+    /* Cleanup routines: */
+    //void saveMachineViewSettings();
+    //void cleanupFilters();
+    void cleanupCommon();
+    void cleanupFrameBuffer();
 
     /* Protected variables: */
@@ -125,5 +126,4 @@
     /* Dock icon update handler */
     void sltChangeDockIconUpdate(const VBoxChangeDockIconUpdateEvent &event);
-
 # ifdef QT_MAC_USE_COCOA
     /* Presentation mode handler */
@@ -133,14 +133,4 @@
 
 private:
-
-    /* Private getters: */
-    int contentsWidth() const;
-    int contentsHeight() const;
-    int visibleWidth() const;
-    int visibleHeight() const;
-    const QPixmap& pauseShot() const { return mPausedShot; }
-    bool shouldHideHostPointer() const { return m_bIsMouseCaptured || (m_bIsMouseAbsolute && mHideHostPointer); }
-    bool isRunning() { return mLastState == KMachineState_Running || mLastState == KMachineState_Teleporting || mLastState == KMachineState_LiveSnapshotting; }
-    CConsole &console() { return m_console; }
 
     /* Event processors: */
@@ -192,4 +182,6 @@
 
     /* Private members: */
+    bool shouldHideHostPointer() const { return m_bIsMouseCaptured || (m_bIsMouseAbsolute && mHideHostPointer); }
+    bool isRunning() { return mLastState == KMachineState_Running || mLastState == KMachineState_Teleporting || mLastState == KMachineState_LiveSnapshotting; }
     void fixModifierState(LONG *piCodes, uint *puCount);
     void scrollBy(int dx, int dy);
@@ -217,4 +209,13 @@
 
     static void dimImage(QImage &img);
+
+#if defined(Q_WS_MAC)
+    void updateDockIcon();
+    void updateDockOverlay();
+    void setMouseCoalescingEnabled(bool aOn);
+    void setDockIconEnabled(bool aOn) { mDockIconEnabled = aOn; };
+
+    const QPixmap& pauseShot() const { return mPausedShot; }
+#endif
 
     /* Private members: */
@@ -286,4 +287,8 @@
     bool mHideHostPointer;
     QCursor mLastCursor;
+
+    /* Friend classes: */
+    friend class UIFrameBuffer;
+    friend class UIFrameBufferQImage;
 };
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp	(revision 26729)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/UIMachineWindow.cpp	(revision 26730)
@@ -120,7 +120,9 @@
             strSnapshotName = " (" + snapshot.GetName() + ")";
         }
-        machineWindow()->setWindowTitle(machine.GetName() + strSnapshotName + " [" +
-                                        vboxGlobal().toString(machineLogic()->machineState()) + "] - " +
-                                        m_strWindowTitlePrefix);
+        QString strMachineName = machine.GetName() + strSnapshotName;
+        if (machineLogic()->machineState() != KMachineState_Null)
+            strMachineName += " [" + vboxGlobal().toString(machineLogic()->machineState()) + "] - ";
+        strMachineName += m_strWindowTitlePrefix;
+        machineWindow()->setWindowTitle(strMachineName);
 
         // TODO: Move that to fullscreen/seamless update routine:
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineViewNormal.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineViewNormal.cpp	(revision 26729)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineViewNormal.cpp	(revision 26730)
@@ -46,7 +46,24 @@
                    )
 {
-    /* Connect view to handlers */
-    connect(this, SIGNAL(additionsStateChanged(const QString&, bool, bool, bool)),
-            this, SLOT(sltAdditionsStateChanged(const QString &, bool, bool, bool)));
+    /* Prepare frame buffer: */
+    prepareFrameBuffer();
+
+    /* Prepare common things: */
+    prepareCommon();
+
+    /* Prepare event-filters: */
+    prepareFilters();
+
+    /* Load machine view settings: */
+    loadMachineViewSettings();
+}
+
+UIMachineViewNormal::~UIMachineViewNormal()
+{
+    /* Cleanup common things: */
+    cleanupCommon();
+
+    /* Cleanup frame buffer: */
+    cleanupFrameBuffer();
 }
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineViewNormal.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineViewNormal.h	(revision 26729)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineViewNormal.h	(revision 26730)
@@ -39,4 +39,5 @@
 #endif
     );
+    virtual ~UIMachineViewNormal();
 
     void normalizeGeometry(bool bAdjustPosition = false);
Index: /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineWindowNormal.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineWindowNormal.cpp	(revision 26729)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/runtime/normal/UIMachineWindowNormal.cpp	(revision 26730)
@@ -700,6 +700,4 @@
 void UIMachineWindowNormal::prepareMachineView()
 {
-    return; // TODO: Do not create view for now!
-
     CMachine machine = session().GetMachine();
 
@@ -710,14 +708,15 @@
 
     m_pMachineView = UIMachineView::create(  this
-                                           , vboxGlobal().vmRenderMode() // TODO: use correct variable here!
+                                           , vboxGlobal().vmRenderMode()
 #ifdef VBOX_WITH_VIDEOHWACCEL
-                                           , bAccelerate2DVideo // TODO: use correct variable here!
+                                           , bAccelerate2DVideo
 #endif
                                            , machineLogic()->visualStateType());
-    qobject_cast<QGridLayout*>(centralWidget()->layout())->addWidget(m_pMachineView, 1, 1, Qt::AlignVCenter | Qt::AlignHCenter);
+
+    //qobject_cast<QGridLayout*>(centralWidget()->layout())->addWidget(m_pMachineView, 1, 1, Qt::AlignVCenter | Qt::AlignHCenter);
+    setCentralWidget(m_pMachineView);
 
     /* Setup machine view <-> indicators connections: */
-    connect(machineView(), SIGNAL(keyboardStateChanged(int)),
-            indicatorsPool()->indicator(UIIndicatorIndex_Hostkey), SLOT(setState(int)));
+    connect(machineView(), SIGNAL(keyboardStateChanged(int)), indicatorsPool()->indicator(UIIndicatorIndex_Hostkey), SLOT(setState(int)));
     connect(machineView(), SIGNAL(mouseStateChanged(int)), this, SLOT(sltUpdateMouseState(int)));
 }
