aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/qtmacnativewidget/main.mm129
-rw-r--r--examples/qtmacnativewidget/qtmacnativewidget.pro11
-rw-r--r--src/qtmacextras.pri11
-rw-r--r--src/qtmacnativewidget.h84
-rw-r--r--src/qtmacnativewidget.mm132
5 files changed, 367 insertions, 0 deletions
diff --git a/examples/qtmacnativewidget/main.mm b/examples/qtmacnativewidget/main.mm
new file mode 100644
index 0000000..fd3cc87
--- /dev/null
+++ b/examples/qtmacnativewidget/main.mm
@@ -0,0 +1,129 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+#include <Cocoa/Cocoa.h>
+#include <qtmacnativewidget.h>
+
+class RedWidget : public QWidget
+{
+public:
+ RedWidget() {
+
+ }
+
+ void resizeEvent(QResizeEvent *)
+ {
+ qDebug() << "RedWidget::resize" << size();
+ }
+
+ void paintEvent(QPaintEvent *event)
+ {
+ QPainter p(this);
+ QRect rect(QPoint(0, 0), size());
+ qDebug() << "Painting geometry" << rect;
+ p.fillRect(rect, QColor(133, 50, 50));
+ }
+};
+
+@interface WindowCreator : NSObject {}
+- (void)createWindow;
+@end
+
+@implementation WindowCreator
+- (void)createWindow {
+
+ // Create the NSWindow
+ NSRect frame = NSMakeRect(500, 500, 500, 500);
+ NSWindow* window = [[NSWindow alloc] initWithContentRect:frame
+ styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask
+ backing:NSBackingStoreBuffered
+ defer:NO];
+ [window setTitle:@"NSWindow"];
+
+ // Create widget hierarchy with QPushButton and QLineEdit
+ QtMacNativeWidget *nativeWidget = new QtMacNativeWidget();
+
+ QHBoxLayout *hlayout = new QHBoxLayout();
+ hlayout->addWidget(new QPushButton("Push", nativeWidget));
+ hlayout->addWidget(new QLineEdit);
+
+ QVBoxLayout *vlayout = new QVBoxLayout();
+ vlayout->addLayout(hlayout);
+
+ //RedWidget * redWidget = new RedWidget;
+ //vlayout->addWidget(redWidget);
+
+ nativeWidget->setLayout(vlayout);
+
+ // Get the NSView for QMacNativeWidget and set it as the content view for the NSWindow
+ [window setContentView:nativeWidget->nativeView()];
+
+ // show() must be called on nativeWiget to get the widgets int he correct state.
+ nativeWidget->show();
+
+ // Show the NSWindow
+ [window makeKeyAndOrderFront:NSApp];
+}
+@end
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ // Start Cocoa. Create NSApplicaiton.
+ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+ [NSApplication sharedApplication];
+
+ // Schedule call to create the UI using a zero timer.
+ WindowCreator *windowCreator= [WindowCreator alloc];
+ [NSTimer scheduledTimerWithTimeInterval:0 target:windowCreator selector:@selector(createWindow) userInfo:nil repeats:NO];
+
+ // Stare the Cocoa event loop.
+ [(NSApplication *)NSApp run];
+ [NSApp release];
+ [pool release];
+ exit(0);
+ return 0;
+}
+
+
+
diff --git a/examples/qtmacnativewidget/qtmacnativewidget.pro b/examples/qtmacnativewidget/qtmacnativewidget.pro
new file mode 100644
index 0000000..07d271e
--- /dev/null
+++ b/examples/qtmacnativewidget/qtmacnativewidget.pro
@@ -0,0 +1,11 @@
+TEMPLATE = app
+
+include (../../src/qtmacextras.pri)
+
+OBJECTIVE_SOURCES += main.mm
+LIBS += -framework Cocoa
+
+QT += gui widgets
+QT += widgets-private gui-private core-private
+
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
diff --git a/src/qtmacextras.pri b/src/qtmacextras.pri
index 32bdce3..3675461 100644
--- a/src/qtmacextras.pri
+++ b/src/qtmacextras.pri
@@ -26,3 +26,14 @@ OBJECTIVE_SOURCES += $$PWD/qmacpasteboardmime.mm
# qt_mac_set_dock_menu
HEADERS += $$PWD/qtmacfunctions.h
OBJECTIVE_SOURCES += $$PWD/qtmacfunctions.mm
+
+minQtVersion(5, 0, 1) {
+ HEADERS += $$PWD/qtmacfunctions.h
+ OBJECTIVE_SOURCES += $$PWD/qtmacfunctions.mm
+} else {
+ message("qt_mac_set_dock_menu requires Qt 5.0.1 and will be exculded from this build. You have Qt" $$QT_VERSION)
+}
+
+# QtMacNativeWidget
+HEADERS += $$PWD/qtmacnativewidget.h
+OBJECTIVE_SOURCES += $$PWD/qtmacnativewidget.mm
diff --git a/src/qtmacnativewidget.h b/src/qtmacnativewidget.h
new file mode 100644
index 0000000..78c453e
--- /dev/null
+++ b/src/qtmacnativewidget.h
@@ -0,0 +1,84 @@
+/****************************************************************************
+ **
+ ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+ ** Contact: http://www.qt-project.org/legal
+ **
+ ** This file is part of QtMacExtras in the Qt Toolkit.
+ **
+ ** $QT_BEGIN_LICENSE:LGPL$
+ ** Commercial License Usage
+ ** Licensees holding valid commercial Qt licenses may use this file in
+ ** accordance with the commercial license agreement provided with the
+ ** Software or, alternatively, in accordance with the terms contained in
+ ** a written agreement between you and Digia. For licensing terms and
+ ** conditions see http://qt.digia.com/licensing. For further information
+ ** use the contact form at http://qt.digia.com/contact-us.
+ **
+ ** GNU Lesser General Public License Usage
+ ** Alternatively, this file may be used under the terms of the GNU Lesser
+ ** General Public License version 2.1 as published by the Free Software
+ ** Foundation and appearing in the file LICENSE.LGPL included in the
+ ** packaging of this file. Please review the following information to
+ ** ensure the GNU Lesser General Public License version 2.1 requirements
+ ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ **
+ ** In addition, as a special exception, Digia gives you certain additional
+ ** rights. These rights are described in the Digia Qt LGPL Exception
+ ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+ **
+ ** GNU General Public License Usage
+ ** Alternatively, this file may be used under the terms of the GNU
+ ** General Public License version 3.0 as published by the Free Software
+ ** Foundation and appearing in the file LICENSE.GPL included in the
+ ** packaging of this file. Please review the following information to
+ ** ensure the GNU General Public License version 3.0 requirements will be
+ ** met: http://www.gnu.org/copyleft/gpl.html.
+ **
+ **
+ ** $QT_END_LICENSE$
+ **
+ ****************************************************************************/
+#ifndef QTMACNATIVEWIDGET_H
+#define QTMACNATIVEWIDGET_H
+
+#include <QtWidgets/QWidget>
+#import <Availability.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Gui)
+
+#ifdef __OBJC__
+@class NSView;
+#endif
+
+class QtMacNativeWidget : public QWidget
+{
+ Q_OBJECT
+public:
+#ifdef __OBJC__
+ QtMacNativeWidget(NSView *parentView = 0);
+#else
+ QtMacNativeWidget(void *parentView = 0);
+#endif
+ ~QtMacNativeWidget();
+
+#ifdef __OBJC__
+ NSView *nativeView() const;
+#else
+ void *nativeView() const;
+#endif
+
+ QSize sizeHint() const;
+protected:
+ void init(void *parentView);
+ bool event(QEvent *ev);
+};
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // QTMACNATIVEWIDGET_H
diff --git a/src/qtmacnativewidget.mm b/src/qtmacnativewidget.mm
new file mode 100644
index 0000000..b1bb5a1
--- /dev/null
+++ b/src/qtmacnativewidget.mm
@@ -0,0 +1,132 @@
+/****************************************************************************
+ **
+ ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+ ** Contact: http://www.qt-project.org/legal
+ **
+ ** This file is part of QtMacExtras in the Qt Toolkit.
+ **
+ ** $QT_BEGIN_LICENSE:LGPL$
+ ** Commercial License Usage
+ ** Licensees holding valid commercial Qt licenses may use this file in
+ ** accordance with the commercial license agreement provided with the
+ ** Software or, alternatively, in accordance with the terms contained in
+ ** a written agreement between you and Digia. For licensing terms and
+ ** conditions see http://qt.digia.com/licensing. For further information
+ ** use the contact form at http://qt.digia.com/contact-us.
+ **
+ ** GNU Lesser General Public License Usage
+ ** Alternatively, this file may be used under the terms of the GNU Lesser
+ ** General Public License version 2.1 as published by the Free Software
+ ** Foundation and appearing in the file LICENSE.LGPL included in the
+ ** packaging of this file. Please review the following information to
+ ** ensure the GNU Lesser General Public License version 2.1 requirements
+ ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ **
+ ** In addition, as a special exception, Digia gives you certain additional
+ ** rights. These rights are described in the Digia Qt LGPL Exception
+ ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+ **
+ ** GNU General Public License Usage
+ ** Alternatively, this file may be used under the terms of the GNU
+ ** General Public License version 3.0 as published by the Free Software
+ ** Foundation and appearing in the file LICENSE.GPL included in the
+ ** packaging of this file. Please review the following information to
+ ** ensure the GNU General Public License version 3.0 requirements will be
+ ** met: http://www.gnu.org/copyleft/gpl.html.
+ **
+ **
+ ** $QT_END_LICENSE$
+ **
+ ****************************************************************************/
+
+#include "qtmacnativewidget.h"
+#include <QtGui/QWindow>
+#include <QtGui/QGuiApplication>
+#include <qpa/qplatformnativeinterface.h>
+#include <qdebug.h>
+
+#import <Cocoa/Cocoa.h>
+
+/*!
+ \class QtMacNativeWidget
+ \brief The QtMacNativeWidget class provides a widget for Mac OS X that provides a way to put Qt widgets into Carbon
+ or Cocoa hierarchies depending on how Qt was configured.
+
+ QtMacNativeWidget bridges the gap between NSViews and QWidgets and makes it possible to put a
+ hierarchy of Qt widgets into a non-Qt window or view.
+
+ QtMacNativeWidget pretends it is a window (i.e. isWindow() will return true),
+ but it cannot be shown on its own. It needs to be put into a window
+ when it is created or later through a native call.
+
+ Note that QtMacNativeWidget requires knowledge of Cocoa. All it
+ does is get the Qt hierarchy into a window not owned by Qt. It is then up
+ to the programmer to ensure it is placed correctly in the window and
+ responds correctly to events.
+*/
+
+QT_BEGIN_NAMESPACE
+
+NSView *getEmbeddableView(QWindow *qtWindow)
+{
+ // Set Qt::SubWindow flag. Should be done before crate() is
+ // called - if you call create() or caused it ot be called
+ // before calling this function you need to set SubWindow
+ // yourself
+ qtWindow->setFlags(qtWindow->flags() | Qt::SubWindow);
+
+ // Make sure the platform window is created
+ qtWindow->create();
+
+ // Get the Qt content NSView for the QWindow forom the Qt platform plugin
+ QPlatformNativeInterface *platformNativeInterface = QGuiApplication::platformNativeInterface();
+ NSView *qtView = (NSView *)platformNativeInterface->nativeResourceForWindow("nsview", qtWindow);
+ return qtView; // qtView is ready for use.
+}
+
+/*!
+ Create a QtMacNativeWidget with \a parentView as its "superview" (i.e.,
+ parent). The \a parentView is a NSView pointer.
+*/
+QtMacNativeWidget::QtMacNativeWidget(NSView *parentView)
+ : QWidget(0, Qt::Window | Qt::SubWindow)
+{
+ //d_func()->topData()->embedded = true;
+ setPalette(QPalette(Qt::transparent));
+ setAttribute(Qt::WA_SetPalette, false);
+ setAttribute(Qt::WA_LayoutUsesWidgetRect);
+}
+
+NSView *QtMacNativeWidget::nativeView() const
+{
+ winId();
+ return getEmbeddableView(windowHandle());
+}
+
+/*!
+ Destroy the QtMacNativeWidget.
+*/
+QtMacNativeWidget::~QtMacNativeWidget()
+{
+}
+
+/*!
+ \reimp
+*/
+QSize QtMacNativeWidget::sizeHint() const
+{
+ // QtMacNativeWidget really does not have any other choice
+ // than to fill its designated area.
+ if (windowHandle())
+ return windowHandle()->size();
+ return QWidget::sizeHint();
+}
+/*!
+ \reimp
+*/
+bool QtMacNativeWidget::event(QEvent *ev)
+{
+ return QWidget::event(ev);
+}
+
+QT_END_NAMESPACE