source: trunk/src/gui/kernel/qcocoawindowdelegate_mac.mm@ 134

Last change on this file since 134 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 12.8 KB
Line 
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 "private/qcocoawindowdelegate_mac_p.h"
46#ifdef QT_MAC_USE_COCOA
47#include <private/qwidget_p.h>
48#include <private/qapplication_p.h>
49#include <private/qt_cocoa_helpers_mac_p.h>
50#include <qevent.h>
51#include <qlayout.h>
52#include <qcoreapplication.h>
53#include <qmenubar.h>
54
55QT_BEGIN_NAMESPACE
56extern QWidgetData *qt_qwidget_data(QWidget *); // qwidget.cpp
57extern void onApplicationWindowChangedActivation(QWidget *, bool); //qapplication_mac.mm
58extern bool qt_sendSpontaneousEvent(QObject *, QEvent *); // qapplication.cpp
59QT_END_NAMESPACE
60
61QT_USE_NAMESPACE
62
63static QT_MANGLE_NAMESPACE(QCocoaWindowDelegate) *sharedCocoaWindowDelegate = nil;
64
65// This is a singleton, but unlike most Cocoa singletons, it lives in a library and could be
66// pontentially loaded and unloaded. This means we should at least attempt to do the
67// memory management correctly.
68
69static void cleanupCocoaWindowDelegate()
70{
71 [sharedCocoaWindowDelegate release];
72}
73
74@implementation QT_MANGLE_NAMESPACE(QCocoaWindowDelegate)
75
76- (id)init
77{
78 self = [super init];
79 if (self != nil) {
80 m_windowHash = new QHash<NSWindow *, QWidget *>();
81 m_drawerHash = new QHash<NSDrawer *, QWidget *>();
82 }
83 return self;
84}
85
86- (void)dealloc
87{
88 sharedCocoaWindowDelegate = nil;
89 QHash<NSWindow *, QWidget *>::const_iterator windowIt = m_windowHash->constBegin();
90 while (windowIt != m_windowHash->constEnd()) {
91 [windowIt.key() setDelegate:nil];
92 ++windowIt;
93 }
94 delete m_windowHash;
95 QHash<NSDrawer *, QWidget *>::const_iterator drawerIt = m_drawerHash->constBegin();
96 while (drawerIt != m_drawerHash->constEnd()) {
97 [drawerIt.key() setDelegate:nil];
98 ++drawerIt;
99 }
100 delete m_drawerHash;
101 [super dealloc];
102}
103
104+ (id)allocWithZone:(NSZone *)zone
105{
106 @synchronized(self) {
107 if (sharedCocoaWindowDelegate == nil) {
108 sharedCocoaWindowDelegate = [super allocWithZone:zone];
109 return sharedCocoaWindowDelegate;
110 qAddPostRoutine(cleanupCocoaWindowDelegate);
111 }
112 }
113 return nil;
114}
115
116+ (QT_MANGLE_NAMESPACE(QCocoaWindowDelegate)*)sharedDelegate
117{
118 @synchronized(self) {
119 if (sharedCocoaWindowDelegate == nil)
120 [[self alloc] init];
121 }
122 return [[sharedCocoaWindowDelegate retain] autorelease];
123}
124
125-(void)syncSizeForWidget:(QWidget *)qwidget toSize:(const QSize &)newSize fromSize:(const QSize &)oldSize
126{
127 qt_qwidget_data(qwidget)->crect.setSize(newSize);
128 // ### static contents optimization needs to go here
129 const OSViewRef view = qt_mac_nativeview_for(qwidget);
130 [view setFrameSize:NSMakeSize(newSize.width(), newSize.height())];
131 if (!qwidget->isVisible()) {
132 qwidget->setAttribute(Qt::WA_PendingResizeEvent, true);
133 } else {
134 QResizeEvent qre(newSize, oldSize);
135 qt_sendSpontaneousEvent(qwidget, &qre);
136 }
137}
138
139- (void)dumpMaximizedStateforWidget:(QWidget*)qwidget window:(NSWindow *)window;
140{
141 if (!window)
142 return; // Nothing to do.
143 QWidgetData *widgetData = qt_qwidget_data(qwidget);
144 if ((widgetData->window_state & Qt::WindowMaximized) && ![window isZoomed]) {
145 widgetData->window_state &= ~Qt::WindowMaximized;
146 QWindowStateChangeEvent e(Qt::WindowState(widgetData->window_state | Qt::WindowMaximized));
147 qt_sendSpontaneousEvent(qwidget, &e);
148 }
149}
150
151- (NSSize)closestAcceptableSizeForWidget:(QWidget *)qwidget window:(NSWindow *)window
152 withNewSize:(NSSize)proposedSize
153{
154 [self dumpMaximizedStateforWidget:qwidget window:window];
155 QSize newSize = QLayout::closestAcceptableSize(qwidget,
156 QSize(proposedSize.width, proposedSize.height));
157 return [NSWindow frameRectForContentRect:
158 NSMakeRect(0., 0., newSize.width(), newSize.height())
159 styleMask:[window styleMask]].size;
160}
161
162- (NSSize)windowWillResize:(NSWindow *)windowToResize toSize:(NSSize)proposedFrameSize
163{
164 QWidget *qwidget = m_windowHash->value(windowToResize);
165 return [self closestAcceptableSizeForWidget:qwidget window:windowToResize
166 withNewSize:[NSWindow contentRectForFrameRect:
167 NSMakeRect(0, 0,
168 proposedFrameSize.width,
169 proposedFrameSize.height)
170 styleMask:[windowToResize styleMask]].size];
171}
172
173- (NSSize)drawerWillResizeContents:(NSDrawer *)sender toSize:(NSSize)contentSize
174{
175 QWidget *qwidget = m_drawerHash->value(sender);
176 return [self closestAcceptableSizeForWidget:qwidget window:nil withNewSize:contentSize];
177}
178
179-(void)windowDidMiniaturize:(NSNotification*)notification
180{
181 QWidget *qwidget = m_windowHash->value([notification object]);
182 if (!qwidget->isMinimized()) {
183 QWidgetData *widgetData = qt_qwidget_data(qwidget);
184 widgetData->window_state = widgetData->window_state | Qt::WindowMinimized;
185 QWindowStateChangeEvent e(Qt::WindowStates(widgetData->window_state & ~Qt::WindowMinimized));
186 qt_sendSpontaneousEvent(qwidget, &e);
187 }
188 // Send hide to match Qt on X11 and Windows
189 QEvent e(QEvent::Hide);
190 qt_sendSpontaneousEvent(qwidget, &e);
191}
192
193- (void)windowDidResize:(NSNotification *)notification
194{
195 NSWindow *window = [notification object];
196 QWidget *qwidget = m_windowHash->value(window);
197 // Just here to handle the is zoomed method.
198 QWidgetData *widgetData = qt_qwidget_data(qwidget);
199 if (!(qwidget->windowState() & (Qt::WindowMaximized | Qt::WindowFullScreen)) && [window isZoomed]) {
200 widgetData->window_state = widgetData->window_state | Qt::WindowMaximized;
201 QWindowStateChangeEvent e(Qt::WindowStates(widgetData->window_state
202 & ~Qt::WindowMaximized));
203 qt_sendSpontaneousEvent(qwidget, &e);
204 }
205 [self checkForMove:[window frame] forWidget:qwidget];
206 NSRect rect = [[window contentView] frame];
207 const QSize newSize(rect.size.width, rect.size.height);
208 const QSize &oldSize = widgetData->crect.size();
209 if (newSize != oldSize) {
210 QWidgetPrivate::qt_mac_update_sizer(qwidget);
211 [self syncSizeForWidget:qwidget toSize:newSize fromSize:oldSize];
212 }
213}
214
215- (void)checkForMove:(const NSRect &)newRect forWidget:(QWidget *)qwidget
216{
217 // newRect's origin is bottom left.
218 QPoint qtPoint = flipPoint(NSMakePoint(newRect.origin.x,
219 newRect.origin.y + newRect.size.height)).toPoint();
220 const QRect &oldRect = qwidget->frameGeometry();
221 if (qtPoint.x() != oldRect.x() || qtPoint.y() != oldRect.y()) {
222 QWidgetData *widgetData = qt_qwidget_data(qwidget);
223 QRect oldCRect = widgetData->crect;
224 QWidgetPrivate *widgetPrivate = qt_widget_private(qwidget);
225 const QRect &fStrut = widgetPrivate->frameStrut();
226 widgetData->crect.moveTo(qtPoint.x() + fStrut.left(), qtPoint.y() + fStrut.top());
227 if (!qwidget->isVisible()) {
228 qwidget->setAttribute(Qt::WA_PendingMoveEvent, true);
229 } else {
230 QMoveEvent qme(qtPoint, oldRect.topLeft());
231 qt_sendSpontaneousEvent(qwidget, &qme);
232 }
233 }
234}
235
236- (void)windowDidMove:(NSNotification *)notification
237{
238 [self windowDidResize:notification];
239}
240
241-(BOOL)windowShouldClose:(id)windowThatWantsToClose
242{
243 QWidget *qwidget = m_windowHash->value(windowThatWantsToClose);
244 QScopedLoopLevelCounter counter(qt_widget_private(qwidget)->threadData);
245 return qt_widget_private(qwidget)->close_helper(QWidgetPrivate::CloseWithSpontaneousEvent);
246}
247
248-(void)windowDidDeminiaturize:(NSNotification *)notification
249{
250 QWidget *qwidget = m_windowHash->value([notification object]);
251 QWidgetData *widgetData = qt_qwidget_data(qwidget);
252 Qt::WindowStates currState = Qt::WindowStates(widgetData->window_state);
253 Qt::WindowStates newState = currState;
254 if (currState & Qt::WindowMinimized)
255 newState &= ~Qt::WindowMinimized;
256 if (!(currState & Qt::WindowActive))
257 newState |= Qt::WindowActive;
258 if (newState != currState) {
259 widgetData->window_state = newState;
260 QWindowStateChangeEvent e(currState);
261 qt_sendSpontaneousEvent(qwidget, &e);
262 }
263 QShowEvent qse;
264 qt_sendSpontaneousEvent(qwidget, &qse);
265}
266
267-(void)windowDidBecomeMain:(NSNotification*)notification
268{
269 QWidget *qwidget = m_windowHash->value([notification object]);
270 Q_ASSERT(qwidget);
271 if (qwidget->isActiveWindow())
272 return; // Widget is already active, no need to go through re-activation.
273
274 onApplicationWindowChangedActivation(qwidget, true);
275}
276
277-(void)windowDidResignMain:(NSNotification*)notification
278{
279 QWidget *qwidget = m_windowHash->value([notification object]);
280 Q_ASSERT(qwidget);
281 onApplicationWindowChangedActivation(qwidget, false);
282}
283
284// These are the same as main, but they are probably better to keep separate since there is a
285// tiny difference between main and key windows.
286-(void)windowDidBecomeKey:(NSNotification*)notification
287{
288 QWidget *qwidget = m_windowHash->value([notification object]);
289 Q_ASSERT(qwidget);
290 if (qwidget->isActiveWindow())
291 return; // Widget is already active, no need to go through re-activation
292
293
294 onApplicationWindowChangedActivation(qwidget, true);
295}
296
297-(void)windowDidResignKey:(NSNotification*)notification
298{
299 QWidget *qwidget = m_windowHash->value([notification object]);
300 Q_ASSERT(qwidget);
301 onApplicationWindowChangedActivation(qwidget, false);
302}
303
304-(QWidget *)qt_qwidgetForWindow:(NSWindow *)window
305{
306 return m_windowHash->value(window);
307}
308
309- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)defaultFrame
310{
311 NSRect frameToReturn = defaultFrame;
312 QWidget *qwidget = m_windowHash->value(window);
313 QSizeF size = qwidget->maximumSize();
314 frameToReturn.size.width = qMin<CGFloat>(frameToReturn.size.width, size.width());
315 frameToReturn.size.height = qMin<CGFloat>(frameToReturn.size.height, size.height());
316 return frameToReturn;
317}
318
319- (void)becomeDelegteForWindow:(NSWindow *)window widget:(QWidget *)widget
320{
321 m_windowHash->insert(window, widget);
322 [window setDelegate:self];
323}
324
325- (void)resignDelegateForWindow:(NSWindow *)window
326{
327 [window setDelegate:nil];
328 m_windowHash->remove(window);
329}
330
331- (void)becomeDelegateForDrawer:(NSDrawer *)drawer widget:(QWidget *)widget
332{
333 m_drawerHash->insert(drawer, widget);
334 [drawer setDelegate:self];
335 NSWindow *window = [[drawer contentView] window];
336 [self becomeDelegteForWindow:window widget:widget];
337}
338
339- (void)resignDelegateForDrawer:(NSDrawer *)drawer
340{
341 QWidget *widget = m_drawerHash->value(drawer);
342 [drawer setDelegate:nil];
343 if (widget)
344 [self resignDelegateForWindow:[[drawer contentView] window]];
345 m_drawerHash->remove(drawer);
346}
347
348@end
349#endif// QT_MAC_USE_COCOA
Note: See TracBrowser for help on using the repository browser.