1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
41 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
42 | **
|
---|
43 | ****************************************************************************/
|
---|
44 |
|
---|
45 | #import <Cocoa/Cocoa.h>
|
---|
46 | #include <private/qwidget_p.h>
|
---|
47 | #include "qmaccocoaviewcontainer_mac.h"
|
---|
48 | #include <private/qt_mac_p.h>
|
---|
49 |
|
---|
50 | /*!
|
---|
51 | \class QMacCocoaViewContainer
|
---|
52 | \since 4.5
|
---|
53 |
|
---|
54 | \brief The QMacCocoaViewContainer class provides a widget for Mac OS X that can be used to wrap arbitrary
|
---|
55 | Cocoa views (i.e., NSView subclasses) and insert them into Qt hierarchies.
|
---|
56 |
|
---|
57 | \ingroup advanced
|
---|
58 |
|
---|
59 | While Qt offers a lot of classes for writing your application, Apple's
|
---|
60 | Cocoa framework offers lots of functionality that is not currently in Qt or
|
---|
61 | may never end up in Qt. Using QMacCocoaViewContainer, it is possible to put an
|
---|
62 | arbitrary NSView-derived class from Cocoa and put it in a Qt hierarchy.
|
---|
63 | Depending on how comfortable you are with using objective-C, you can use
|
---|
64 | QMacCocoaViewContainer directly, or subclass it to wrap further functionality
|
---|
65 | of the underlying NSView.
|
---|
66 |
|
---|
67 | QMacCocoaViewContainer works regardless if Qt is built against Carbon or
|
---|
68 | Cocoa. However, QCocoaContainerView requires Mac OS X 10.5 or better to be
|
---|
69 | used with Carbon.
|
---|
70 |
|
---|
71 | It should be also noted that at the low level on Mac OS X, there is a
|
---|
72 | difference between windows (top-levels) and view (widgets that are inside a
|
---|
73 | window). For this reason, make sure that the NSView that you are wrapping
|
---|
74 | doesn't end up as a top-level. The best way to ensure this is to make sure
|
---|
75 | you always have a parent and not set the parent to 0.
|
---|
76 |
|
---|
77 | If you are using QMacCocoaViewContainer as a sub-class and are mixing and
|
---|
78 | matching objective-C with C++ (a.k.a. objective-C++). It is probably
|
---|
79 | simpler to have your file end with \tt{.mm} than \tt{.cpp}. Most Apple tools will
|
---|
80 | correctly identify the source as objective-C++.
|
---|
81 |
|
---|
82 | QMacCocoaViewContainer requires knowledge of how Cocoa works, especially in
|
---|
83 | regard to its reference counting (retain/release) nature. It is noted in
|
---|
84 | the functions below if there is any change in the reference count. Cocoa
|
---|
85 | views often generate temporary objects that are released by an autorelease
|
---|
86 | pool. If this is done outside of a running event loop, it is up to the
|
---|
87 | developer to provide the autorelease pool.
|
---|
88 |
|
---|
89 | The following is a snippet of subclassing QMacCocoaViewContainer to wrap a NSSearchField.
|
---|
90 | \snippet demos/macmainwindow/macmainwindow.mm 0
|
---|
91 |
|
---|
92 | */
|
---|
93 |
|
---|
94 | QT_BEGIN_NAMESPACE
|
---|
95 |
|
---|
96 | class QMacCocoaViewContainerPrivate : public QWidgetPrivate
|
---|
97 | {
|
---|
98 | Q_DECLARE_PUBLIC(QMacCocoaViewContainer)
|
---|
99 | public:
|
---|
100 | NSView *nsview;
|
---|
101 | #ifndef QT_MAC_USE_COCOA
|
---|
102 | HIViewRef wrapperView;
|
---|
103 | #endif
|
---|
104 | QMacCocoaViewContainerPrivate();
|
---|
105 | ~QMacCocoaViewContainerPrivate();
|
---|
106 | };
|
---|
107 |
|
---|
108 | QMacCocoaViewContainerPrivate::QMacCocoaViewContainerPrivate()
|
---|
109 | : nsview(0)
|
---|
110 | #ifndef QT_MAC_USE_COCOA
|
---|
111 | , wrapperView(0)
|
---|
112 | #endif
|
---|
113 | {
|
---|
114 | }
|
---|
115 |
|
---|
116 | QMacCocoaViewContainerPrivate::~QMacCocoaViewContainerPrivate()
|
---|
117 | {
|
---|
118 | [nsview release];
|
---|
119 | #ifndef QT_MAC_USE_COCOA
|
---|
120 | if (wrapperView)
|
---|
121 | CFRelease(wrapperView);
|
---|
122 | #endif
|
---|
123 | }
|
---|
124 |
|
---|
125 | /*!
|
---|
126 | \fn QMacCocoaViewContainer::QMacCocoaViewContainer(void *cocoaViewToWrap, QWidget *parent)
|
---|
127 |
|
---|
128 | Create a new QMacCocoaViewContainer using the NSView pointer in \a
|
---|
129 | cocoaViewToWrap with parent, \a parent. QMacCocoaViewContainer will
|
---|
130 | retain \a cocoaViewToWrap.
|
---|
131 |
|
---|
132 | \a cocoaViewToWrap is a void pointer that allows the header to be included
|
---|
133 | with C++ source.
|
---|
134 | */
|
---|
135 | QMacCocoaViewContainer::QMacCocoaViewContainer(void *cocoaViewToWrap, QWidget *parent)
|
---|
136 | : QWidget(*new QMacCocoaViewContainerPrivate, parent, 0)
|
---|
137 | {
|
---|
138 | if (cocoaViewToWrap)
|
---|
139 | setCocoaView(cocoaViewToWrap);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*!
|
---|
143 | Destroy the QMacCocoaViewContainer and release the wrapped view.
|
---|
144 | */
|
---|
145 | QMacCocoaViewContainer::~QMacCocoaViewContainer()
|
---|
146 | {
|
---|
147 | }
|
---|
148 |
|
---|
149 | /*!
|
---|
150 | Returns the NSView that has been set on this container. The returned view
|
---|
151 | has been autoreleased, so you will need to retain it if you want to make
|
---|
152 | use of it.
|
---|
153 | */
|
---|
154 | void *QMacCocoaViewContainer::cocoaView() const
|
---|
155 | {
|
---|
156 | Q_D(const QMacCocoaViewContainer);
|
---|
157 | return [[d->nsview retain] autorelease];
|
---|
158 | }
|
---|
159 |
|
---|
160 | /*!
|
---|
161 | Sets the NSView to contain to be \a cocoaViewToWrap and retains it. If this
|
---|
162 | container already had a view set, it will release the previously set view.
|
---|
163 | */
|
---|
164 | void QMacCocoaViewContainer::setCocoaView(void *cocoaViewToWrap)
|
---|
165 | {
|
---|
166 | Q_D(QMacCocoaViewContainer);
|
---|
167 | QMacCocoaAutoReleasePool pool;
|
---|
168 | NSView *view = static_cast<NSView *>(cocoaViewToWrap);
|
---|
169 | NSView *oldView = d->nsview;
|
---|
170 | destroy(true, true);
|
---|
171 | [view retain];
|
---|
172 | d->nsview = view;
|
---|
173 | #ifndef QT_MAC_USE_COCOA
|
---|
174 | if (QSysInfo::MacintoshVersion < QSysInfo::MV_10_5) {
|
---|
175 | qWarning("QMacCocoaViewContainer::setCocoaView: You cannot use this class with Carbon on versions of Mac OS X less than 10.5.");
|
---|
176 | return;
|
---|
177 | }
|
---|
178 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
---|
179 | if (d->wrapperView)
|
---|
180 | CFRelease(d->wrapperView);
|
---|
181 | HICocoaViewCreate(d->nsview, 0, &d->wrapperView);
|
---|
182 | create(WId(d->wrapperView), false, true);
|
---|
183 | #endif
|
---|
184 | #else
|
---|
185 | create(WId(d->nsview), false, true);
|
---|
186 | #endif
|
---|
187 | [oldView release];
|
---|
188 | }
|
---|
189 |
|
---|
190 | QT_END_NAMESPACE
|
---|