author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 03 May 2010 13:17:34 +0300 | |
changeset 19 | fcece45ef507 |
parent 18 | 2f34d5167611 |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qcolordialog_p.h" |
|
43 |
#if !defined(QT_NO_COLORDIALOG) && defined(Q_WS_MAC) |
|
44 |
#include <qapplication.h> |
|
45 |
#include <qtimer.h> |
|
46 |
#include <qdialogbuttonbox.h> |
|
47 |
#include <qabstracteventdispatcher.h> |
|
48 |
#include <private/qapplication_p.h> |
|
49 |
#include <private/qt_mac_p.h> |
|
50 |
#include <qdebug.h> |
|
51 |
#import <AppKit/AppKit.h> |
|
52 |
#import <Foundation/Foundation.h> |
|
53 |
||
54 |
#if !CGFLOAT_DEFINED |
|
55 |
typedef float CGFloat; // Should only not be defined on 32-bit platforms |
|
56 |
#endif |
|
57 |
||
58 |
||
59 |
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5 |
|
60 |
@protocol NSWindowDelegate <NSObject> |
|
61 |
- (void)windowDidResize:(NSNotification *)notification; |
|
62 |
- (BOOL)windowShouldClose:(id)window; |
|
63 |
@end |
|
64 |
#endif |
|
65 |
||
66 |
QT_USE_NAMESPACE |
|
67 |
||
68 |
@class QCocoaColorPanelDelegate; |
|
69 |
||
70 |
@interface QCocoaColorPanelDelegate : NSObject<NSWindowDelegate> { |
|
71 |
NSColorPanel *mColorPanel; |
|
72 |
NSView *mStolenContentView; |
|
73 |
NSButton *mOkButton; |
|
74 |
NSButton *mCancelButton; |
|
75 |
QColorDialogPrivate *mPriv; |
|
76 |
QColor *mQtColor; |
|
77 |
CGFloat mMinWidth; // currently unused |
|
78 |
CGFloat mExtraHeight; // currently unused |
|
79 |
BOOL mHackedPanel; |
|
80 |
NSInteger mResultCode; |
|
81 |
BOOL mDialogIsExecuting; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
BOOL mResultSet; |
0 | 83 |
} |
84 |
- (id)initWithColorPanel:(NSColorPanel *)panel |
|
85 |
stolenContentView:(NSView *)stolenContentView |
|
86 |
okButton:(NSButton *)okButton |
|
87 |
cancelButton:(NSButton *)cancelButton |
|
88 |
priv:(QColorDialogPrivate *)priv; |
|
89 |
- (void)colorChanged:(NSNotification *)notification; |
|
90 |
- (void)relayout; |
|
91 |
- (void)onOkClicked; |
|
92 |
- (void)onCancelClicked; |
|
93 |
- (void)updateQtColor; |
|
94 |
- (NSColorPanel *)colorPanel; |
|
95 |
- (QColor)qtColor; |
|
96 |
- (void)finishOffWithCode:(NSInteger)result; |
|
97 |
- (void)showColorPanel; |
|
98 |
- (void)exec; |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
99 |
- (void)setResultSet:(BOOL)result; |
0 | 100 |
@end |
101 |
||
102 |
@implementation QCocoaColorPanelDelegate |
|
103 |
- (id)initWithColorPanel:(NSColorPanel *)panel |
|
104 |
stolenContentView:(NSView *)stolenContentView |
|
105 |
okButton:(NSButton *)okButton |
|
106 |
cancelButton:(NSButton *)cancelButton |
|
107 |
priv:(QColorDialogPrivate *)priv |
|
108 |
{ |
|
109 |
self = [super init]; |
|
110 |
||
111 |
mColorPanel = panel; |
|
112 |
mStolenContentView = stolenContentView; |
|
113 |
mOkButton = okButton; |
|
114 |
mCancelButton = cancelButton; |
|
115 |
mPriv = priv; |
|
116 |
mMinWidth = 0.0; |
|
117 |
mExtraHeight = 0.0; |
|
118 |
mHackedPanel = (okButton != 0); |
|
119 |
mResultCode = NSCancelButton; |
|
120 |
mDialogIsExecuting = false; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
121 |
mResultSet = false; |
0 | 122 |
|
123 |
if (mHackedPanel) { |
|
124 |
[self relayout]; |
|
125 |
||
126 |
[okButton setAction:@selector(onOkClicked)]; |
|
127 |
[okButton setTarget:self]; |
|
128 |
||
129 |
[cancelButton setAction:@selector(onCancelClicked)]; |
|
130 |
[cancelButton setTarget:self]; |
|
131 |
} |
|
132 |
||
133 |
[[NSNotificationCenter defaultCenter] addObserver:self |
|
134 |
selector:@selector(colorChanged:) |
|
135 |
name:NSColorPanelColorDidChangeNotification |
|
136 |
object:mColorPanel]; |
|
137 |
||
138 |
mQtColor = new QColor(); |
|
139 |
return self; |
|
140 |
} |
|
141 |
||
142 |
- (void)dealloc |
|
143 |
{ |
|
144 |
QMacCocoaAutoReleasePool pool; |
|
145 |
if (mHackedPanel) { |
|
146 |
NSView *ourContentView = [mColorPanel contentView]; |
|
147 |
||
148 |
// return stolen stuff to its rightful owner |
|
149 |
[mStolenContentView removeFromSuperview]; |
|
150 |
[mColorPanel setContentView:mStolenContentView]; |
|
151 |
||
152 |
[mOkButton release]; |
|
153 |
[mCancelButton release]; |
|
154 |
[ourContentView release]; |
|
155 |
} |
|
156 |
[mColorPanel setDelegate:nil]; |
|
157 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
|
158 |
delete mQtColor; |
|
159 |
[super dealloc]; |
|
160 |
} |
|
161 |
||
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
162 |
- (void)setResultSet:(BOOL)result |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
163 |
{ |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
164 |
mResultSet = result; |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
165 |
} |
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
166 |
|
0 | 167 |
- (BOOL)windowShouldClose:(id)window |
168 |
{ |
|
169 |
Q_UNUSED(window); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
if (!mHackedPanel) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
[self updateQtColor]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
if (mDialogIsExecuting) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
[self finishOffWithCode:NSCancelButton]; |
0 | 174 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
mResultSet = true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
mPriv->colorDialog()->reject(); |
0 | 177 |
} |
178 |
return true; |
|
179 |
} |
|
180 |
||
181 |
- (void)windowDidResize:(NSNotification *)notification |
|
182 |
{ |
|
183 |
Q_UNUSED(notification); |
|
184 |
if (mHackedPanel) |
|
185 |
[self relayout]; |
|
186 |
} |
|
187 |
||
188 |
- (void)colorChanged:(NSNotification *)notification; |
|
189 |
{ |
|
190 |
Q_UNUSED(notification); |
|
191 |
[self updateQtColor]; |
|
192 |
} |
|
193 |
||
194 |
- (void)relayout |
|
195 |
{ |
|
196 |
Q_ASSERT(mHackedPanel); |
|
197 |
||
198 |
NSRect rect = [[mStolenContentView superview] frame]; |
|
199 |
||
200 |
// should a priori be kept in sync with qfontdialog_mac.mm |
|
201 |
const CGFloat ButtonMinWidth = 78.0; // 84.0 for Carbon |
|
202 |
const CGFloat ButtonMinHeight = 32.0; |
|
203 |
const CGFloat ButtonSpacing = 0.0; |
|
204 |
const CGFloat ButtonTopMargin = 0.0; |
|
205 |
const CGFloat ButtonBottomMargin = 7.0; |
|
206 |
const CGFloat ButtonSideMargin = 9.0; |
|
207 |
||
208 |
[mOkButton sizeToFit]; |
|
209 |
NSSize okSizeHint = [mOkButton frame].size; |
|
210 |
||
211 |
[mCancelButton sizeToFit]; |
|
212 |
NSSize cancelSizeHint = [mCancelButton frame].size; |
|
213 |
||
214 |
const CGFloat ButtonWidth = qMin(qMax(ButtonMinWidth, |
|
215 |
qMax(okSizeHint.width, cancelSizeHint.width)), |
|
216 |
CGFloat((rect.size.width - 2.0 * ButtonSideMargin - ButtonSpacing) * 0.5)); |
|
217 |
const CGFloat ButtonHeight = qMax(ButtonMinHeight, |
|
218 |
qMax(okSizeHint.height, cancelSizeHint.height)); |
|
219 |
||
220 |
NSRect okRect = { { rect.size.width - ButtonSideMargin - ButtonWidth, |
|
221 |
ButtonBottomMargin }, |
|
222 |
{ ButtonWidth, ButtonHeight } }; |
|
223 |
[mOkButton setFrame:okRect]; |
|
224 |
[mOkButton setNeedsDisplay:YES]; |
|
225 |
||
226 |
NSRect cancelRect = { { okRect.origin.x - ButtonSpacing - ButtonWidth, |
|
227 |
ButtonBottomMargin }, |
|
228 |
{ ButtonWidth, ButtonHeight } }; |
|
229 |
[mCancelButton setFrame:cancelRect]; |
|
230 |
[mCancelButton setNeedsDisplay:YES]; |
|
231 |
||
232 |
const CGFloat Y = ButtonBottomMargin + ButtonHeight + ButtonTopMargin; |
|
233 |
NSRect stolenCVRect = { { 0.0, Y }, |
|
234 |
{ rect.size.width, rect.size.height - Y } }; |
|
235 |
[mStolenContentView setFrame:stolenCVRect]; |
|
236 |
[mStolenContentView setNeedsDisplay:YES]; |
|
237 |
||
238 |
[[mStolenContentView superview] setNeedsDisplay:YES]; |
|
239 |
mMinWidth = 2 * ButtonSideMargin + ButtonSpacing + 2 * ButtonWidth; |
|
240 |
mExtraHeight = Y; |
|
241 |
} |
|
242 |
||
243 |
- (void)onOkClicked |
|
244 |
{ |
|
245 |
Q_ASSERT(mHackedPanel); |
|
246 |
[[mStolenContentView window] close]; |
|
247 |
[self updateQtColor]; |
|
248 |
[self finishOffWithCode:NSOKButton]; |
|
249 |
} |
|
250 |
||
251 |
- (void)onCancelClicked |
|
252 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
253 |
if (mHackedPanel) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
254 |
[[mStolenContentView window] close]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
255 |
delete mQtColor; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
256 |
mQtColor = new QColor(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
257 |
[self finishOffWithCode:NSCancelButton]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
258 |
} |
0 | 259 |
} |
260 |
||
261 |
- (void)updateQtColor |
|
262 |
{ |
|
263 |
delete mQtColor; |
|
264 |
mQtColor = new QColor(); |
|
265 |
NSColor *color = [mColorPanel color]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
266 |
NSString *colorSpaceName = [color colorSpaceName]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
267 |
if (colorSpaceName == NSDeviceCMYKColorSpace) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
268 |
CGFloat cyan = 0, magenta = 0, yellow = 0, black = 0, alpha = 0; |
0 | 269 |
[color getCyan:&cyan magenta:&magenta yellow:&yellow black:&black alpha:&alpha]; |
270 |
mQtColor->setCmykF(cyan, magenta, yellow, black, alpha); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
271 |
} else if (colorSpaceName == NSCalibratedRGBColorSpace || colorSpaceName == NSDeviceRGBColorSpace) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
272 |
CGFloat red = 0, green = 0, blue = 0, alpha = 0; |
0 | 273 |
[color getRed:&red green:&green blue:&blue alpha:&alpha]; |
274 |
mQtColor->setRgbF(red, green, blue, alpha); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
275 |
} else if (colorSpaceName == NSNamedColorSpace) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
276 |
NSColor *tmpColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
277 |
CGFloat red = 0, green = 0, blue = 0, alpha = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
278 |
[tmpColor getRed:&red green:&green blue:&blue alpha:&alpha]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
279 |
mQtColor->setRgbF(red, green, blue, alpha); |
0 | 280 |
} else { |
281 |
NSColorSpace *colorSpace = [color colorSpace]; |
|
282 |
if ([colorSpace colorSpaceModel] == NSCMYKColorSpaceModel && [color numberOfComponents] == 5){ |
|
283 |
CGFloat components[5]; |
|
284 |
[color getComponents:components]; |
|
285 |
mQtColor->setCmykF(components[0], components[1], components[2], components[3], components[4]); |
|
286 |
} else { |
|
287 |
NSColor *tmpColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
288 |
CGFloat red = 0, green = 0, blue = 0, alpha = 0; |
0 | 289 |
[tmpColor getRed:&red green:&green blue:&blue alpha:&alpha]; |
290 |
mQtColor->setRgbF(red, green, blue, alpha); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
mPriv->setCurrentQColor(*mQtColor); |
|
295 |
} |
|
296 |
||
297 |
- (NSColorPanel *)colorPanel |
|
298 |
{ |
|
299 |
return mColorPanel; |
|
300 |
} |
|
301 |
||
302 |
- (QColor)qtColor |
|
303 |
{ |
|
304 |
return *mQtColor; |
|
305 |
} |
|
306 |
||
307 |
- (void)finishOffWithCode:(NSInteger)code |
|
308 |
{ |
|
309 |
mResultCode = code; |
|
310 |
if (mDialogIsExecuting) { |
|
311 |
// We stop the current modal event loop. The control |
|
312 |
// will then return inside -(void)exec below. |
|
313 |
// It's important that the modal event loop is stopped before |
|
314 |
// we accept/reject QColorDialog, since QColorDialog has its |
|
315 |
// own event loop that needs to be stopped last. |
|
316 |
[NSApp stopModalWithCode:code]; |
|
317 |
} else { |
|
318 |
// Since we are not in a modal event loop, we can safely close |
|
319 |
// down QColorDialog |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
320 |
// Calling accept() or reject() can in turn call closeCocoaColorPanel. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
321 |
// This check will prevent any such recursion. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
322 |
if (!mResultSet) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
323 |
mResultSet = true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
324 |
if (mResultCode == NSCancelButton) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
325 |
mPriv->colorDialog()->reject(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
326 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
327 |
mPriv->colorDialog()->accept(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
328 |
} |
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
329 |
} |
0 | 330 |
} |
331 |
} |
|
332 |
||
333 |
- (void)showColorPanel |
|
334 |
{ |
|
335 |
mDialogIsExecuting = false; |
|
336 |
[mColorPanel makeKeyAndOrderFront:mColorPanel]; |
|
337 |
} |
|
338 |
||
339 |
- (void)exec |
|
340 |
{ |
|
341 |
QBoolBlocker nativeDialogOnTop(QApplicationPrivate::native_modal_dialog_active); |
|
342 |
QMacCocoaAutoReleasePool pool; |
|
343 |
mDialogIsExecuting = true; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
344 |
bool modalEnded = false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
345 |
while (!modalEnded) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
346 |
@try { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
347 |
[NSApp runModalForWindow:mColorPanel]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
348 |
modalEnded = true; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
349 |
} @catch (NSException *) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
350 |
// For some reason, NSColorPanel throws an exception when |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
351 |
// clicking on 'SelectedMenuItemColor' from the 'Developer' |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
352 |
// palette (tab three). |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
353 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
354 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
355 |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
356 |
QAbstractEventDispatcher::instance()->interrupt(); |
0 | 357 |
if (mResultCode == NSCancelButton) |
358 |
mPriv->colorDialog()->reject(); |
|
359 |
else |
|
360 |
mPriv->colorDialog()->accept(); |
|
361 |
} |
|
362 |
||
363 |
@end |
|
364 |
||
365 |
QT_BEGIN_NAMESPACE |
|
366 |
||
367 |
extern void macStartInterceptNSPanelCtor(); |
|
368 |
extern void macStopInterceptNSPanelCtor(); |
|
369 |
extern NSButton *macCreateButton(const char *text, NSView *superview); |
|
370 |
||
371 |
void QColorDialogPrivate::openCocoaColorPanel(const QColor &initial, |
|
372 |
QWidget *parent, const QString &title, QColorDialog::ColorDialogOptions options) |
|
373 |
{ |
|
374 |
Q_UNUSED(parent); // we would use the parent if only NSColorPanel could be a sheet |
|
375 |
QMacCocoaAutoReleasePool pool; |
|
376 |
||
377 |
if (!delegate) { |
|
378 |
/* |
|
379 |
The standard Cocoa color panel has no OK or Cancel button and |
|
380 |
is created as a utility window, whereas we want something like |
|
381 |
the Carbon color panel. We need to take the following steps: |
|
382 |
||
383 |
1. Intercept the color panel constructor to turn off the |
|
384 |
NSUtilityWindowMask flag. This is done by temporarily |
|
385 |
replacing initWithContentRect:styleMask:backing:defer: |
|
386 |
in NSPanel by our own method. |
|
387 |
||
388 |
2. Modify the color panel so that its content view is part |
|
389 |
of a new content view that contains it as well as two |
|
390 |
buttons (OK and Cancel). |
|
391 |
||
392 |
3. Lay out the original content view and the buttons when |
|
393 |
the color panel is shown and whenever it is resized. |
|
394 |
||
395 |
4. Clean up after ourselves. |
|
396 |
*/ |
|
397 |
||
398 |
bool hackColorPanel = !(options & QColorDialog::NoButtons); |
|
399 |
||
400 |
if (hackColorPanel) |
|
401 |
macStartInterceptNSPanelCtor(); |
|
402 |
NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel]; |
|
403 |
if (hackColorPanel) |
|
404 |
macStopInterceptNSPanelCtor(); |
|
405 |
||
406 |
[colorPanel setHidesOnDeactivate:false]; |
|
407 |
||
408 |
// set up the Cocoa color panel |
|
409 |
[colorPanel setShowsAlpha:options & QColorDialog::ShowAlphaChannel]; |
|
410 |
[colorPanel setTitle:(NSString*)(CFStringRef)QCFString(title)]; |
|
411 |
||
412 |
NSView *stolenContentView = 0; |
|
413 |
NSButton *okButton = 0; |
|
414 |
NSButton *cancelButton = 0; |
|
415 |
||
416 |
if (hackColorPanel) { |
|
417 |
// steal the color panel's contents view |
|
418 |
stolenContentView = [colorPanel contentView]; |
|
419 |
[stolenContentView retain]; |
|
420 |
[colorPanel setContentView:0]; |
|
421 |
||
422 |
// create a new content view and add the stolen one as a subview |
|
423 |
NSRect frameRect = { { 0.0, 0.0 }, { 0.0, 0.0 } }; |
|
424 |
NSView *ourContentView = [[NSView alloc] initWithFrame:frameRect]; |
|
425 |
[ourContentView addSubview:stolenContentView]; |
|
426 |
||
427 |
// create OK and Cancel buttons and add these as subviews |
|
428 |
okButton = macCreateButton("&OK", ourContentView); |
|
429 |
cancelButton = macCreateButton("Cancel", ourContentView); |
|
430 |
||
431 |
[colorPanel setContentView:ourContentView]; |
|
432 |
[colorPanel setDefaultButtonCell:[okButton cell]]; |
|
433 |
} |
|
434 |
||
435 |
delegate = [[QCocoaColorPanelDelegate alloc] initWithColorPanel:colorPanel |
|
436 |
stolenContentView:stolenContentView |
|
437 |
okButton:okButton |
|
438 |
cancelButton:cancelButton |
|
439 |
priv:this]; |
|
440 |
[colorPanel setDelegate:static_cast<QCocoaColorPanelDelegate *>(delegate)]; |
|
441 |
} |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
442 |
[delegate setResultSet:false]; |
0 | 443 |
setCocoaPanelColor(initial); |
444 |
[static_cast<QCocoaColorPanelDelegate *>(delegate) showColorPanel]; |
|
445 |
} |
|
446 |
||
447 |
void QColorDialogPrivate::closeCocoaColorPanel() |
|
448 |
{ |
|
449 |
[static_cast<QCocoaColorPanelDelegate *>(delegate) onCancelClicked]; |
|
450 |
} |
|
451 |
||
452 |
void QColorDialogPrivate::releaseCocoaColorPanelDelegate() |
|
453 |
{ |
|
454 |
[static_cast<QCocoaColorPanelDelegate *>(delegate) release]; |
|
455 |
} |
|
456 |
||
457 |
void QColorDialogPrivate::mac_nativeDialogModalHelp() |
|
458 |
{ |
|
459 |
// Do a queued meta-call to open the native modal dialog so it opens after the new |
|
460 |
// event loop has started to execute (in QDialog::exec). Using a timer rather than |
|
461 |
// a queued meta call is intentional to ensure that the call is only delivered when |
|
462 |
// [NSApp run] runs (timers are handeled special in cocoa). If NSApp is not |
|
463 |
// running (which is the case if e.g a top-most QEventLoop has been |
|
464 |
// interrupted, and the second-most event loop has not yet been reactivated (regardless |
|
465 |
// if [NSApp run] is still on the stack)), showing a native modal dialog will fail. |
|
466 |
if (delegate){ |
|
467 |
Q_Q(QColorDialog); |
|
468 |
QTimer::singleShot(1, q, SLOT(_q_macRunNativeAppModalPanel())); |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
void QColorDialogPrivate::_q_macRunNativeAppModalPanel() |
|
473 |
{ |
|
474 |
[static_cast<QCocoaColorPanelDelegate *>(delegate) exec]; |
|
475 |
} |
|
476 |
||
477 |
void QColorDialogPrivate::setCocoaPanelColor(const QColor &color) |
|
478 |
{ |
|
479 |
QMacCocoaAutoReleasePool pool; |
|
480 |
QCocoaColorPanelDelegate *theDelegate = static_cast<QCocoaColorPanelDelegate *>(delegate); |
|
481 |
NSColor *nsColor; |
|
482 |
const QColor::Spec spec = color.spec(); |
|
483 |
if (spec == QColor::Cmyk) { |
|
484 |
nsColor = [NSColor colorWithDeviceCyan:color.cyanF() |
|
485 |
magenta:color.magentaF() |
|
486 |
yellow:color.yellowF() |
|
487 |
black:color.blackF() |
|
488 |
alpha:color.alphaF()]; |
|
489 |
} else { |
|
490 |
nsColor = [NSColor colorWithCalibratedRed:color.redF() |
|
491 |
green:color.greenF() |
|
492 |
blue:color.blueF() |
|
493 |
alpha:color.alphaF()]; |
|
494 |
} |
|
495 |
[[theDelegate colorPanel] setColor:nsColor]; |
|
496 |
} |
|
497 |
||
498 |
QT_END_NAMESPACE |
|
499 |
||
500 |
#endif |