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 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qfontdialog_p.h"
|
---|
43 | #if !defined(QT_NO_FONTDIALOG) && defined(Q_WS_MAC)
|
---|
44 | #include <qapplication.h>
|
---|
45 | #include <qdialogbuttonbox.h>
|
---|
46 | #include <qlineedit.h>
|
---|
47 | #include <private/qapplication_p.h>
|
---|
48 | #include <private/qfont_p.h>
|
---|
49 | #include <private/qfontengine_p.h>
|
---|
50 | #include <private/qt_mac_p.h>
|
---|
51 | #include <qdebug.h>
|
---|
52 | #import <AppKit/AppKit.h>
|
---|
53 | #import <Foundation/Foundation.h>
|
---|
54 |
|
---|
55 | #if !CGFLOAT_DEFINED
|
---|
56 | typedef float CGFloat; // Should only not be defined on 32-bit platforms
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | QT_USE_NAMESPACE
|
---|
60 |
|
---|
61 | // should a priori be kept in sync with qcolordialog_mac.mm
|
---|
62 | const CGFloat ButtonMinWidth = 78.0;
|
---|
63 | const CGFloat ButtonMinHeight = 32.0;
|
---|
64 | const CGFloat ButtonSpacing = 0.0;
|
---|
65 | const CGFloat ButtonTopMargin = 0.0;
|
---|
66 | const CGFloat ButtonBottomMargin = 7.0;
|
---|
67 | const CGFloat ButtonSideMargin = 9.0;
|
---|
68 |
|
---|
69 | // looks better with some margins
|
---|
70 | const CGFloat DialogTopMargin = 7.0;
|
---|
71 | const CGFloat DialogSideMargin = 9.0;
|
---|
72 |
|
---|
73 | const int StyleMask = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
|
---|
74 |
|
---|
75 | @class QCocoaFontPanelDelegate;
|
---|
76 |
|
---|
77 |
|
---|
78 | #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
|
---|
79 |
|
---|
80 | @protocol NSWindowDelegate <NSObject> @end
|
---|
81 |
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | @interface QCocoaFontPanelDelegate : NSObject <NSWindowDelegate> {
|
---|
85 | NSFontPanel *mFontPanel;
|
---|
86 | NSView *mStolenContentView;
|
---|
87 | NSButton *mOkButton;
|
---|
88 | NSButton *mCancelButton;
|
---|
89 | QFontDialogPrivate *mPriv;
|
---|
90 | NSFont *mCocoaFont;
|
---|
91 | QFont *mQtFont;
|
---|
92 | BOOL mPanelHackedWithButtons;
|
---|
93 | CGFloat mDialogExtraWidth;
|
---|
94 | CGFloat mDialogExtraHeight;
|
---|
95 | NSModalSession mModalSession;
|
---|
96 | }
|
---|
97 | - (id)initWithFontPanel:(NSFontPanel *)panel
|
---|
98 | stolenContentView:(NSView *)stolenContentView
|
---|
99 | okButton:(NSButton *)okButton
|
---|
100 | cancelButton:(NSButton *)cancelButton
|
---|
101 | priv:(QFontDialogPrivate *)priv
|
---|
102 | extraWidth:(CGFloat)extraWidth
|
---|
103 | extraHeight:(CGFloat)extraHeight;
|
---|
104 | - (void)changeFont:(id)sender;
|
---|
105 | - (void)changeAttributes:(id)sender;
|
---|
106 | - (void)setModalSession:(NSModalSession)session;
|
---|
107 | - (BOOL)windowShouldClose:(id)window;
|
---|
108 | - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
|
---|
109 | - (void)relayout;
|
---|
110 | - (void)relayoutToContentSize:(NSSize)frameSize;
|
---|
111 | - (void)onOkClicked;
|
---|
112 | - (void)onCancelClicked;
|
---|
113 | - (NSFontPanel *)fontPanel;
|
---|
114 | - (NSWindow *)actualPanel;
|
---|
115 | - (NSSize)dialogExtraSize;
|
---|
116 | - (void)setQtFont:(const QFont &)newFont;
|
---|
117 | - (QFont)qtFont;
|
---|
118 | - (void)finishOffWithCode:(NSInteger)result;
|
---|
119 | - (void)cleanUpAfterMyself;
|
---|
120 | @end
|
---|
121 |
|
---|
122 | @implementation QCocoaFontPanelDelegate
|
---|
123 | - (id)initWithFontPanel:(NSFontPanel *)panel
|
---|
124 | stolenContentView:(NSView *)stolenContentView
|
---|
125 | okButton:(NSButton *)okButton
|
---|
126 | cancelButton:(NSButton *)cancelButton
|
---|
127 | priv:(QFontDialogPrivate *)priv
|
---|
128 | extraWidth:(CGFloat)extraWidth
|
---|
129 | extraHeight:(CGFloat)extraHeight
|
---|
130 | {
|
---|
131 | self = [super init];
|
---|
132 | mFontPanel = panel;
|
---|
133 | mStolenContentView = stolenContentView;
|
---|
134 | mOkButton = okButton;
|
---|
135 | mCancelButton = cancelButton;
|
---|
136 | mPriv = priv;
|
---|
137 | mCocoaFont = 0;
|
---|
138 | mPanelHackedWithButtons = (okButton != 0);
|
---|
139 | mDialogExtraWidth = extraWidth;
|
---|
140 | mDialogExtraHeight = extraHeight;
|
---|
141 | mModalSession = 0;
|
---|
142 |
|
---|
143 | if (mPanelHackedWithButtons) {
|
---|
144 | [self relayout];
|
---|
145 |
|
---|
146 | [okButton setAction:@selector(onOkClicked)];
|
---|
147 | [okButton setTarget:self];
|
---|
148 |
|
---|
149 | [cancelButton setAction:@selector(onCancelClicked)];
|
---|
150 | [cancelButton setTarget:self];
|
---|
151 | }
|
---|
152 | mQtFont = new QFont();
|
---|
153 | return self;
|
---|
154 | }
|
---|
155 |
|
---|
156 | - (void)dealloc
|
---|
157 | {
|
---|
158 | if (mCocoaFont)
|
---|
159 | [mCocoaFont release];
|
---|
160 | delete mQtFont;
|
---|
161 | [super dealloc];
|
---|
162 | }
|
---|
163 |
|
---|
164 | - (void)changeFont:(id)sender
|
---|
165 | {
|
---|
166 | Q_UNUSED(sender);
|
---|
167 |
|
---|
168 | QFont newFont;
|
---|
169 |
|
---|
170 | if (mCocoaFont)
|
---|
171 | [mCocoaFont autorelease];
|
---|
172 | NSFont *dummyFont = [NSFont userFontOfSize:12.0];
|
---|
173 | mCocoaFont = [sender convertFont:dummyFont];
|
---|
174 | if (mCocoaFont) {
|
---|
175 | [mCocoaFont retain];
|
---|
176 |
|
---|
177 | int pSize = qRound([mCocoaFont pointSize]);
|
---|
178 | QString family(QCFString::toQString(reinterpret_cast<CFStringRef>([mCocoaFont familyName])));
|
---|
179 | QString typeface(QCFString::toQString(reinterpret_cast<CFStringRef>([mCocoaFont fontName])));
|
---|
180 | // qDebug() << "original family" << family << "typeface" << typeface << "psize" << pSize;
|
---|
181 | int hyphenPos = typeface.indexOf(QLatin1Char('-'));
|
---|
182 | if (hyphenPos != -1) {
|
---|
183 | typeface.remove(0, hyphenPos + 1);
|
---|
184 | } else {
|
---|
185 | typeface = QLatin1String("Normal");
|
---|
186 | }
|
---|
187 | // qDebug() << " massaged family" << family << "typeface" << typeface << "psize" << pSize;
|
---|
188 | newFont = QFontDatabase().font(family, typeface, pSize);
|
---|
189 | newFont.setUnderline(mQtFont->underline());
|
---|
190 | newFont.setStrikeOut(mQtFont->strikeOut());
|
---|
191 | }
|
---|
192 |
|
---|
193 | [self setQtFont:newFont];
|
---|
194 | if (mPriv)
|
---|
195 | mPriv->updateSampleFont(*mQtFont);
|
---|
196 | }
|
---|
197 |
|
---|
198 | - (void)changeAttributes:(id)sender
|
---|
199 | {
|
---|
200 | NSDictionary *dummyAttribs = [NSDictionary dictionary];
|
---|
201 | NSDictionary *attribs = [sender convertAttributes:dummyAttribs];
|
---|
202 |
|
---|
203 | #ifdef QT_MAC_USE_COCOA
|
---|
204 | for (id key in attribs) {
|
---|
205 | #else
|
---|
206 | NSEnumerator *enumerator = [attribs keyEnumerator];
|
---|
207 | id key;
|
---|
208 | while((key = [enumerator nextObject])) {
|
---|
209 | #endif
|
---|
210 | NSNumber *number = static_cast<NSNumber *>([attribs objectForKey:key]);
|
---|
211 | if ([key isEqual:NSUnderlineStyleAttributeName]) {
|
---|
212 | mQtFont->setUnderline([number intValue] != NSUnderlineStyleNone);
|
---|
213 | } else if ([key isEqual:NSStrikethroughStyleAttributeName]) {
|
---|
214 | mQtFont->setStrikeOut([number intValue] != NSUnderlineStyleNone);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (mPriv)
|
---|
219 | mPriv->updateSampleFont(*mQtFont);
|
---|
220 | }
|
---|
221 |
|
---|
222 | - (void)setModalSession:(NSModalSession)session
|
---|
223 | {
|
---|
224 | Q_ASSERT(!mModalSession);
|
---|
225 | mModalSession = session;
|
---|
226 | }
|
---|
227 |
|
---|
228 | - (BOOL)windowShouldClose:(id)window
|
---|
229 | {
|
---|
230 | Q_UNUSED(window);
|
---|
231 | if (mPanelHackedWithButtons) {
|
---|
232 | [self onCancelClicked];
|
---|
233 | } else {
|
---|
234 | [self finishOffWithCode:NSCancelButton];
|
---|
235 | }
|
---|
236 | return true;
|
---|
237 | }
|
---|
238 |
|
---|
239 | - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
|
---|
240 | {
|
---|
241 | if (mFontPanel == window) {
|
---|
242 | proposedFrameSize = [static_cast<id <NSWindowDelegate> >(mFontPanel) windowWillResize:mFontPanel toSize:proposedFrameSize];
|
---|
243 | } else {
|
---|
244 | /*
|
---|
245 | Ugly hack: NSFontPanel rearranges the layout of its main
|
---|
246 | component in windowWillResize:toSize:. So we temporarily
|
---|
247 | restore the stolen content view to its rightful owner,
|
---|
248 | call windowWillResize:toSize:, and steal the content view
|
---|
249 | again.
|
---|
250 | */
|
---|
251 | [mStolenContentView removeFromSuperview];
|
---|
252 | [mFontPanel setContentView:mStolenContentView];
|
---|
253 | NSSize extraSize = [self dialogExtraSize];
|
---|
254 | proposedFrameSize.width -= extraSize.width;
|
---|
255 | proposedFrameSize.height -= extraSize.height;
|
---|
256 | proposedFrameSize = [static_cast<id <NSWindowDelegate> >(mFontPanel) windowWillResize:mFontPanel toSize:proposedFrameSize];
|
---|
257 | NSRect frameRect = { { 0.0, 0.0 }, proposedFrameSize };
|
---|
258 | [mFontPanel setFrame:frameRect display:NO];
|
---|
259 | [mFontPanel setContentView:0];
|
---|
260 | [[window contentView] addSubview:mStolenContentView];
|
---|
261 | proposedFrameSize.width += extraSize.width;
|
---|
262 | proposedFrameSize.height += extraSize.height;
|
---|
263 | }
|
---|
264 | if (mPanelHackedWithButtons) {
|
---|
265 | NSRect frameRect = { { 0.0, 0.0 }, proposedFrameSize };
|
---|
266 | NSRect contentRect = [NSWindow contentRectForFrameRect:frameRect styleMask:[window styleMask]];
|
---|
267 | [self relayoutToContentSize:contentRect.size];
|
---|
268 | }
|
---|
269 | return proposedFrameSize;
|
---|
270 | }
|
---|
271 |
|
---|
272 | - (void)relayout
|
---|
273 | {
|
---|
274 | [self relayoutToContentSize:[[mStolenContentView superview] frame].size];
|
---|
275 | }
|
---|
276 |
|
---|
277 | - (void)relayoutToContentSize:(NSSize)frameSize;
|
---|
278 | {
|
---|
279 | Q_ASSERT(mPanelHackedWithButtons);
|
---|
280 |
|
---|
281 | [mOkButton sizeToFit];
|
---|
282 | NSSize okSizeHint = [mOkButton frame].size;
|
---|
283 |
|
---|
284 | [mCancelButton sizeToFit];
|
---|
285 | NSSize cancelSizeHint = [mCancelButton frame].size;
|
---|
286 |
|
---|
287 | const CGFloat ButtonWidth = qMin(qMax(ButtonMinWidth,
|
---|
288 | qMax(okSizeHint.width, cancelSizeHint.width)),
|
---|
289 | CGFloat((frameSize.width - 2.0 * ButtonSideMargin
|
---|
290 | - ButtonSpacing) * 0.5));
|
---|
291 | const CGFloat ButtonHeight = qMax(ButtonMinHeight,
|
---|
292 | qMax(okSizeHint.height, cancelSizeHint.height));
|
---|
293 |
|
---|
294 | const CGFloat X = DialogSideMargin;
|
---|
295 | const CGFloat Y = ButtonBottomMargin + ButtonHeight + ButtonTopMargin;
|
---|
296 |
|
---|
297 | NSRect okRect = { { frameSize.width - ButtonSideMargin - ButtonWidth,
|
---|
298 | ButtonBottomMargin },
|
---|
299 | { ButtonWidth, ButtonHeight } };
|
---|
300 | [mOkButton setFrame:okRect];
|
---|
301 | [mOkButton setNeedsDisplay:YES];
|
---|
302 |
|
---|
303 | NSRect cancelRect = { { okRect.origin.x - ButtonSpacing - ButtonWidth,
|
---|
304 | ButtonBottomMargin },
|
---|
305 | { ButtonWidth, ButtonHeight } };
|
---|
306 | [mCancelButton setFrame:cancelRect];
|
---|
307 | [mCancelButton setNeedsDisplay:YES];
|
---|
308 |
|
---|
309 | NSRect stolenCVRect = { { X, Y },
|
---|
310 | { frameSize.width - X - X, frameSize.height - Y - DialogTopMargin } };
|
---|
311 | [mStolenContentView setFrame:stolenCVRect];
|
---|
312 | [mStolenContentView setNeedsDisplay:YES];
|
---|
313 |
|
---|
314 | [[mStolenContentView superview] setNeedsDisplay:YES];
|
---|
315 | }
|
---|
316 |
|
---|
317 | - (void)onOkClicked
|
---|
318 | {
|
---|
319 | Q_ASSERT(mPanelHackedWithButtons);
|
---|
320 | [[mStolenContentView window] close];
|
---|
321 | [self finishOffWithCode:NSOKButton];
|
---|
322 | }
|
---|
323 |
|
---|
324 | - (void)onCancelClicked
|
---|
325 | {
|
---|
326 | Q_ASSERT(mPanelHackedWithButtons);
|
---|
327 | [[mStolenContentView window] close];
|
---|
328 | [self finishOffWithCode:NSCancelButton];
|
---|
329 | }
|
---|
330 |
|
---|
331 | - (NSFontPanel *)fontPanel
|
---|
332 | {
|
---|
333 | return mFontPanel;
|
---|
334 | }
|
---|
335 |
|
---|
336 | - (NSWindow *)actualPanel
|
---|
337 | {
|
---|
338 | return [mStolenContentView window];
|
---|
339 | }
|
---|
340 |
|
---|
341 | - (NSSize)dialogExtraSize
|
---|
342 | {
|
---|
343 | // this must be recomputed each time, because sometimes the
|
---|
344 | // NSFontPanel has the NSDocModalWindowMask flag set, and sometimes
|
---|
345 | // not -- which affects the frame rect vs. content rect measurements
|
---|
346 |
|
---|
347 | // take the different frame rectangles into account for dialogExtra{Width,Height}
|
---|
348 | NSRect someRect = { { 0.0, 0.0 }, { 100000.0, 100000.0 } };
|
---|
349 | NSRect sharedFontPanelContentRect = [mFontPanel contentRectForFrameRect:someRect];
|
---|
350 | NSRect ourPanelContentRect = [NSWindow contentRectForFrameRect:someRect styleMask:StyleMask];
|
---|
351 |
|
---|
352 | NSSize result = { mDialogExtraWidth, mDialogExtraHeight };
|
---|
353 | result.width -= ourPanelContentRect.size.width - sharedFontPanelContentRect.size.width;
|
---|
354 | result.height -= ourPanelContentRect.size.height - sharedFontPanelContentRect.size.height;
|
---|
355 | return result;
|
---|
356 | }
|
---|
357 |
|
---|
358 | - (void)setQtFont:(const QFont &)newFont
|
---|
359 | {
|
---|
360 | delete mQtFont;
|
---|
361 | mQtFont = new QFont(newFont);
|
---|
362 | }
|
---|
363 |
|
---|
364 | - (QFont)qtFont
|
---|
365 | {
|
---|
366 | return *mQtFont;
|
---|
367 | }
|
---|
368 |
|
---|
369 | - (void)finishOffWithCode:(NSInteger)code
|
---|
370 | {
|
---|
371 | if (mPriv) {
|
---|
372 | if (mModalSession) {
|
---|
373 | [NSApp endModalSession:mModalSession];
|
---|
374 | mModalSession = 0;
|
---|
375 | }
|
---|
376 |
|
---|
377 | // temporary hack to work around bug in deleteLater() in Qt/Mac Cocoa
|
---|
378 | #if 1
|
---|
379 | bool deleteDialog = mPriv->fontDialog()->testAttribute(Qt::WA_DeleteOnClose);
|
---|
380 | mPriv->fontDialog()->setAttribute(Qt::WA_DeleteOnClose, false);
|
---|
381 | #endif
|
---|
382 | mPriv->done((code == NSOKButton) ? QDialog::Accepted : QDialog::Rejected);
|
---|
383 | #if 1
|
---|
384 | if (deleteDialog)
|
---|
385 | delete mPriv->fontDialog();
|
---|
386 | #endif
|
---|
387 | } else {
|
---|
388 | [NSApp stopModalWithCode:code];
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | - (void)cleanUpAfterMyself
|
---|
393 | {
|
---|
394 | if (mPanelHackedWithButtons) {
|
---|
395 | NSView *ourContentView = [mFontPanel contentView];
|
---|
396 |
|
---|
397 | // return stolen stuff to its rightful owner
|
---|
398 | [mStolenContentView removeFromSuperview];
|
---|
399 | [mFontPanel setContentView:mStolenContentView];
|
---|
400 |
|
---|
401 | [mOkButton release];
|
---|
402 | [mCancelButton release];
|
---|
403 | [ourContentView release];
|
---|
404 | }
|
---|
405 | [mFontPanel setDelegate:nil];
|
---|
406 | [[NSFontManager sharedFontManager] setDelegate:nil];
|
---|
407 | [[NSFontManager sharedFontManager] setTarget:nil];
|
---|
408 | }
|
---|
409 | @end
|
---|
410 |
|
---|
411 | QT_BEGIN_NAMESPACE
|
---|
412 |
|
---|
413 | extern void macStartInterceptNSPanelCtor();
|
---|
414 | extern void macStopInterceptNSPanelCtor();
|
---|
415 | extern NSButton *macCreateButton(const char *text, NSView *superview);
|
---|
416 |
|
---|
417 | void *QFontDialogPrivate::openCocoaFontPanel(const QFont &initial,
|
---|
418 | QWidget *parent, const QString &title, QFontDialog::FontDialogOptions options,
|
---|
419 | QFontDialogPrivate *priv)
|
---|
420 | {
|
---|
421 | Q_UNUSED(parent); // we would use the parent if only NSFontPanel could be a sheet
|
---|
422 | QMacCocoaAutoReleasePool pool;
|
---|
423 |
|
---|
424 | /*
|
---|
425 | The standard Cocoa font panel has no OK or Cancel button and
|
---|
426 | is created as a utility window. For strange reasons (which seem
|
---|
427 | to stem from the fact that the font panel is based on a NIB
|
---|
428 | file), the approach we use for the color panel doesn't work for
|
---|
429 | the font panel (and, inversely, the approach we use here doesn't
|
---|
430 | quite work for color panel, and crashed last time I tried). So
|
---|
431 | instead, we take the following steps:
|
---|
432 |
|
---|
433 | 1. Constructs a plain NSPanel that looks the way we want it
|
---|
434 | to look. Specifically, if the NoButtons option is off, we
|
---|
435 | construct a panel without the NSUtilityWindowMask flag
|
---|
436 | and with buttons (OK and Cancel).
|
---|
437 |
|
---|
438 | 2. Steal the content view from the shared NSFontPanel and
|
---|
439 | put it inside our new NSPanel's content view, together
|
---|
440 | with the OK and Cancel buttons.
|
---|
441 |
|
---|
442 | 3. Lay out the original content view and the buttons when
|
---|
443 | the font panel is shown and whenever it is resized.
|
---|
444 |
|
---|
445 | 4. Clean up after ourselves.
|
---|
446 |
|
---|
447 | PS. Some customization is also done in QCocoaApplication
|
---|
448 | validModesForFontPanel:.
|
---|
449 | */
|
---|
450 |
|
---|
451 | Qt::WindowModality modality = Qt::ApplicationModal;
|
---|
452 | if (priv)
|
---|
453 | modality = priv->fontDialog()->windowModality();
|
---|
454 |
|
---|
455 | bool needButtons = !(options & QFontDialog::NoButtons);
|
---|
456 | // don't need our own panel if the title bar isn't visible anyway (in a sheet)
|
---|
457 | bool needOwnPanel = (needButtons && modality != Qt::WindowModal);
|
---|
458 |
|
---|
459 | bool sharedFontPanelExisted = [NSFontPanel sharedFontPanelExists];
|
---|
460 | NSFontPanel *sharedFontPanel = [NSFontPanel sharedFontPanel];
|
---|
461 | [sharedFontPanel setHidesOnDeactivate:false];
|
---|
462 |
|
---|
463 | // hack to ensure that QCocoaApplication's validModesForFontPanel:
|
---|
464 | // implementation is honored
|
---|
465 | if (!sharedFontPanelExisted && needOwnPanel) {
|
---|
466 | [sharedFontPanel makeKeyAndOrderFront:sharedFontPanel];
|
---|
467 | [sharedFontPanel close];
|
---|
468 | }
|
---|
469 |
|
---|
470 | NSPanel *ourPanel = 0;
|
---|
471 | NSView *stolenContentView = 0;
|
---|
472 | NSButton *okButton = 0;
|
---|
473 | NSButton *cancelButton = 0;
|
---|
474 |
|
---|
475 | CGFloat dialogExtraWidth = 0.0;
|
---|
476 | CGFloat dialogExtraHeight = 0.0;
|
---|
477 |
|
---|
478 | if (!needOwnPanel) {
|
---|
479 | // we can reuse the NSFontPanel unchanged
|
---|
480 | ourPanel = sharedFontPanel;
|
---|
481 | } else {
|
---|
482 | // compute dialogExtra{Width,Height}
|
---|
483 | dialogExtraWidth = 2.0 * DialogSideMargin;
|
---|
484 | dialogExtraHeight = DialogTopMargin + ButtonTopMargin + ButtonMinHeight
|
---|
485 | + ButtonBottomMargin;
|
---|
486 |
|
---|
487 | // compute initial contents rectangle
|
---|
488 | NSRect contentRect = [sharedFontPanel contentRectForFrameRect:[sharedFontPanel frame]];
|
---|
489 | contentRect.size.width += dialogExtraWidth;
|
---|
490 | contentRect.size.height += dialogExtraHeight;
|
---|
491 |
|
---|
492 | // create the new panel
|
---|
493 | ourPanel = [[NSPanel alloc] initWithContentRect:contentRect
|
---|
494 | styleMask:StyleMask
|
---|
495 | backing:NSBackingStoreBuffered
|
---|
496 | defer:YES];
|
---|
497 | [ourPanel setReleasedWhenClosed:YES];
|
---|
498 | }
|
---|
499 |
|
---|
500 | stolenContentView = [sharedFontPanel contentView];
|
---|
501 |
|
---|
502 | if (needButtons) {
|
---|
503 | // steal the font panel's contents view
|
---|
504 | [stolenContentView retain];
|
---|
505 | [sharedFontPanel setContentView:0];
|
---|
506 |
|
---|
507 | // create a new content view and add the stolen one as a subview
|
---|
508 | NSRect frameRect = { { 0.0, 0.0 }, { 0.0, 0.0 } };
|
---|
509 | NSView *ourContentView = [[NSView alloc] initWithFrame:frameRect];
|
---|
510 | [ourContentView addSubview:stolenContentView];
|
---|
511 |
|
---|
512 | // create OK and Cancel buttons and add these as subviews
|
---|
513 | okButton = macCreateButton("&OK", ourContentView);
|
---|
514 | cancelButton = macCreateButton("Cancel", ourContentView);
|
---|
515 |
|
---|
516 | [ourPanel setContentView:ourContentView];
|
---|
517 | [ourPanel setDefaultButtonCell:[okButton cell]];
|
---|
518 | }
|
---|
519 |
|
---|
520 | // create a delegate and set it
|
---|
521 | QCocoaFontPanelDelegate *delegate =
|
---|
522 | [[QCocoaFontPanelDelegate alloc] initWithFontPanel:sharedFontPanel
|
---|
523 | stolenContentView:stolenContentView
|
---|
524 | okButton:okButton
|
---|
525 | cancelButton:cancelButton
|
---|
526 | priv:priv
|
---|
527 | extraWidth:dialogExtraWidth
|
---|
528 | extraHeight:dialogExtraHeight];
|
---|
529 | [ourPanel setDelegate:delegate];
|
---|
530 | [[NSFontManager sharedFontManager] setDelegate:delegate];
|
---|
531 | [[NSFontManager sharedFontManager] setTarget:delegate];
|
---|
532 | setFont(delegate, initial);
|
---|
533 |
|
---|
534 | // hack to get correct initial layout
|
---|
535 | NSRect frameRect = [ourPanel frame];
|
---|
536 | frameRect.size.width += 1.0;
|
---|
537 | [ourPanel setFrame:frameRect display:NO];
|
---|
538 | frameRect.size.width -= 1.0;
|
---|
539 | frameRect.size = [delegate windowWillResize:ourPanel toSize:frameRect.size];
|
---|
540 | [ourPanel setFrame:frameRect display:NO];
|
---|
541 | [ourPanel center];
|
---|
542 |
|
---|
543 | [ourPanel setTitle:(NSString*)(CFStringRef)QCFString(title)];
|
---|
544 |
|
---|
545 | if (priv) {
|
---|
546 | switch (modality) {
|
---|
547 | case Qt::WindowModal:
|
---|
548 | if (parent) {
|
---|
549 | #ifndef QT_MAC_USE_COCOA
|
---|
550 | WindowRef hiwindowRef = qt_mac_window_for(parent);
|
---|
551 | NSWindow *window =
|
---|
552 | [[NSWindow alloc] initWithWindowRef:hiwindowRef];
|
---|
553 | // Cocoa docs say I should retain the Carbon ref.
|
---|
554 | CFRetain(hiwindowRef);
|
---|
555 | #else
|
---|
556 | NSWindow *window = qt_mac_window_for(parent);
|
---|
557 | #endif
|
---|
558 | [NSApp beginSheet:ourPanel
|
---|
559 | modalForWindow:window
|
---|
560 | modalDelegate:0
|
---|
561 | didEndSelector:0
|
---|
562 | contextInfo:0];
|
---|
563 | #ifndef QT_MAC_USE_COCOA
|
---|
564 | [window release];
|
---|
565 | #endif
|
---|
566 | break;
|
---|
567 | }
|
---|
568 | // fallthrough
|
---|
569 | case Qt::ApplicationModal:
|
---|
570 | [delegate setModalSession:[NSApp beginModalSessionForWindow:ourPanel]];
|
---|
571 | break;
|
---|
572 | default:
|
---|
573 | [ourPanel makeKeyAndOrderFront:ourPanel];
|
---|
574 | }
|
---|
575 | }
|
---|
576 |
|
---|
577 | return delegate;
|
---|
578 | }
|
---|
579 |
|
---|
580 | void QFontDialogPrivate::closeCocoaFontPanel(void *delegate)
|
---|
581 | {
|
---|
582 | QCocoaFontPanelDelegate *theDelegate = static_cast<QCocoaFontPanelDelegate *>(delegate);
|
---|
583 | NSWindow *ourPanel = [theDelegate actualPanel];
|
---|
584 | [ourPanel close];
|
---|
585 | [theDelegate cleanUpAfterMyself];
|
---|
586 | [theDelegate autorelease];
|
---|
587 | }
|
---|
588 |
|
---|
589 | QFont QFontDialogPrivate::execCocoaFontPanel(bool *ok, const QFont &initial,
|
---|
590 | QWidget *parent, const QString &title, QFontDialog::FontDialogOptions options)
|
---|
591 | {
|
---|
592 | QMacCocoaAutoReleasePool pool;
|
---|
593 | QCocoaFontPanelDelegate *delegate =
|
---|
594 | static_cast<QCocoaFontPanelDelegate *>(
|
---|
595 | openCocoaFontPanel(initial, parent, title, options));
|
---|
596 | NSWindow *ourPanel = [delegate actualPanel];
|
---|
597 | [ourPanel retain];
|
---|
598 | int rval = [NSApp runModalForWindow:ourPanel];
|
---|
599 | QFont font([delegate qtFont]);
|
---|
600 | [ourPanel release];
|
---|
601 | [delegate cleanUpAfterMyself];
|
---|
602 | [delegate release];
|
---|
603 | bool isOk = ((options & QFontDialog::NoButtons) || rval == NSOKButton);
|
---|
604 | if (ok)
|
---|
605 | *ok = isOk;
|
---|
606 | if (isOk) {
|
---|
607 | return font;
|
---|
608 | } else {
|
---|
609 | return initial;
|
---|
610 | }
|
---|
611 | }
|
---|
612 |
|
---|
613 | void QFontDialogPrivate::setFont(void * delegate, const QFont &font)
|
---|
614 | {
|
---|
615 | QFontEngine *fe = font.d->engineForScript(QUnicodeTables::Common);
|
---|
616 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
---|
617 | if (qstrcmp(fe->name(), "CoreText") == 0) {
|
---|
618 | const NSFont *nsFont = reinterpret_cast<const NSFont *>(static_cast<QCoreTextFontEngineMulti *>(fe)->ctfont);
|
---|
619 | [[NSFontManager sharedFontManager] setSelectedFont:nsFont isMultiple:NO];
|
---|
620 | }
|
---|
621 | #endif
|
---|
622 | [static_cast<QCocoaFontPanelDelegate *>(delegate) setQtFont:font];
|
---|
623 | }
|
---|
624 |
|
---|
625 | QT_END_NAMESPACE
|
---|
626 |
|
---|
627 | #endif
|
---|