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