Cocoa: Add setNSToolbar(QWindow *, NSToolBar *)

Calling this function associates the given native
toolbar with the QWindow. QWindow will then set
it on the native NSWindow at the appropriate time
during window creation.

Change-Id: I2a50f79b2a0453cc739f8d68e965e37b95998083
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
This commit is contained in:
Morten Johan Sørvig 2014-01-20 12:08:49 +01:00 committed by The Qt Project
parent df86721bb4
commit 3d08681169
6 changed files with 70 additions and 0 deletions

View File

@ -134,6 +134,9 @@ public:
void updateScreens();
QCocoaScreen *screenAtIndex(int index);
void setToolbar(QWindow *window, NSToolbar *toolbar);
NSToolbar *toolbar(QWindow *window) const;
void clearToolbars();
private:
static QCocoaIntegration *mInstance;
@ -150,6 +153,8 @@ private:
QScopedPointer<QCocoaNativeInterface> mNativeInterface;
QScopedPointer<QCocoaServices> mServices;
QScopedPointer<QCocoaKeyMapper> mKeyboardMapper;
QHash<QWindow *, NSToolbar *> mToolbars;
};
QT_END_NAMESPACE

View File

@ -301,6 +301,8 @@ QCocoaIntegration::~QCocoaIntegration()
while (!mScreens.isEmpty()) {
delete mScreens.takeLast();
}
clearToolbars();
}
QCocoaIntegration *QCocoaIntegration::instance()
@ -466,4 +468,27 @@ QList<int> QCocoaIntegration::possibleKeys(const QKeyEvent *event) const
return mKeyboardMapper->possibleKeys(event);
}
void QCocoaIntegration::setToolbar(QWindow *window, NSToolbar *toolbar)
{
if (NSToolbar *prevToolbar = mToolbars.value(window))
[prevToolbar release];
[toolbar retain];
mToolbars.insert(window, toolbar);
}
NSToolbar *QCocoaIntegration::toolbar(QWindow *window) const
{
return mToolbars.value(window);
}
void QCocoaIntegration::clearToolbars()
{
QHash<QWindow *, NSToolbar *>::const_iterator it = mToolbars.constBegin();
while (it != mToolbars.constEnd()) {
[it.value() release];
}
mToolbars.clear();
}
QT_END_NAMESPACE

View File

@ -135,6 +135,11 @@ private:
// Request a unified title and toolbar look for the window.
static void setContentBorderThickness(QWindow *window, int topThickness, int bottomThickness);
// Sets a NSToolbar instance for the given QWindow. The
// toolbar will be attached to the native NSWindow when
// that is created;
static void setNSToolbar(QWindow *window, void *nsToolbar);
};
QT_END_NAMESPACE

View File

@ -47,6 +47,7 @@
#include "qmacmime.h"
#include "qcocoahelpers.h"
#include "qcocoaapplication.h"
#include "qcocoaintegration.h"
#include <qbytearray.h>
#include <qwindow.h>
@ -125,6 +126,8 @@ QPlatformNativeInterface::NativeResourceForIntegrationFunction QCocoaNativeInter
return NativeResourceForIntegrationFunction(QCocoaNativeInterface::setEmbeddedInForeignView);
if (resource.toLower() == "setcontentborderthickness")
return NativeResourceForIntegrationFunction(QCocoaNativeInterface::setContentBorderThickness);
if (resource.toLower() == "setnstoolbar")
return NativeResourceForIntegrationFunction(QCocoaNativeInterface::setNSToolbar);
return 0;
}
@ -285,4 +288,16 @@ void QCocoaNativeInterface::setContentBorderThickness(QWindow *window, int topTh
cocoaWindow->setContentBorderThickness(topThickness, bottomThickness);
}
void QCocoaNativeInterface::setNSToolbar(QWindow *window, void *nsToolbar)
{
if (!window)
return;
QCocoaIntegration::instance()->setToolbar(window, static_cast<NSToolbar *>(nsToolbar));
QCocoaWindow *cocoaWindow = static_cast<QCocoaWindow *>(window->handle());
if (cocoaWindow)
cocoaWindow->updateNSToolbar();
}
QT_END_NAMESPACE

View File

@ -164,6 +164,7 @@ public:
void registerTouch(bool enable);
void setContentBorderThickness(int topThickness, int bottomThickness);
void applyContentBorderThickness(NSWindow *window);
void updateNSToolbar();
qreal devicePixelRatio() const;
bool isWindowExposable();

View File

@ -45,6 +45,7 @@
#include "qcocoaeventdispatcher.h"
#include "qcocoaglcontext.h"
#include "qcocoahelpers.h"
#include "qcocoanativeinterface.h"
#include "qnsview.h"
#include <QtCore/qfileinfo.h>
#include <QtCore/private/qcore_mac_p.h>
@ -1124,6 +1125,11 @@ void QCocoaWindow::recreateWindow(const QPlatformWindow *parentWindow)
const qreal opacity = qt_window_private(window())->opacity;
if (!qFuzzyCompare(opacity, qreal(1.0)))
setOpacity(opacity);
// top-level QWindows may have an attached NSToolBar, call
// update function which will attach to the NSWindow.
if (!parentWindow)
updateNSToolbar();
}
void QCocoaWindow::reinsertChildWindow(QCocoaWindow *child)
@ -1387,6 +1393,19 @@ void QCocoaWindow::applyContentBorderThickness(NSWindow *window)
}
}
void QCocoaWindow::updateNSToolbar()
{
if (!m_nsWindow)
return;
NSToolbar *toolbar = QCocoaIntegration::instance()->toolbar(window());
if ([m_nsWindow toolbar] == toolbar)
return;
[m_nsWindow setToolbar: toolbar];
[m_nsWindow setShowsToolbarButton:YES];
}
qreal QCocoaWindow::devicePixelRatio() const
{