| [2] | 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| [846] | 3 | ** Copyright (C) 2011 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>
|
|---|
| [769] | 52 | #include <qabstracteventdispatcher.h>
|
|---|
| [2] | 53 | #include <qdebug.h>
|
|---|
| 54 | #import <AppKit/AppKit.h>
|
|---|
| 55 | #import <Foundation/Foundation.h>
|
|---|
| 56 |
|
|---|
| 57 | #if !CGFLOAT_DEFINED
|
|---|
| 58 | typedef float CGFloat; // Should only not be defined on 32-bit platforms
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| [846] | 61 | QT_BEGIN_NAMESPACE
|
|---|
| 62 |
|
|---|
| 63 | extern void macStartInterceptNSPanelCtor();
|
|---|
| 64 | extern void macStopInterceptNSPanelCtor();
|
|---|
| 65 | extern NSButton *macCreateButton(const char *text, NSView *superview);
|
|---|
| 66 | extern bool qt_mac_is_macsheet(const QWidget *w); // qwidget_mac.mm
|
|---|
| 67 |
|
|---|
| 68 | QT_END_NAMESPACE
|
|---|
| [2] | 69 | QT_USE_NAMESPACE
|
|---|
| 70 |
|
|---|
| 71 | // should a priori be kept in sync with qcolordialog_mac.mm
|
|---|
| 72 | const CGFloat ButtonMinWidth = 78.0;
|
|---|
| 73 | const CGFloat ButtonMinHeight = 32.0;
|
|---|
| 74 | const CGFloat ButtonSpacing = 0.0;
|
|---|
| 75 | const CGFloat ButtonTopMargin = 0.0;
|
|---|
| 76 | const CGFloat ButtonBottomMargin = 7.0;
|
|---|
| 77 | const CGFloat ButtonSideMargin = 9.0;
|
|---|
| 78 |
|
|---|
| 79 | // looks better with some margins
|
|---|
| 80 | const CGFloat DialogTopMargin = 7.0;
|
|---|
| 81 | const CGFloat DialogSideMargin = 9.0;
|
|---|
| 82 |
|
|---|
| 83 | const int StyleMask = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
|
|---|
| 84 |
|
|---|
| [846] | 85 | @class QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate);
|
|---|
| [2] | 86 |
|
|---|
| 87 |
|
|---|
| 88 | #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
|
|---|
| 89 |
|
|---|
| [561] | 90 | @protocol NSWindowDelegate <NSObject>
|
|---|
| 91 | - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
|
|---|
| 92 | @end
|
|---|
| [2] | 93 |
|
|---|
| 94 | #endif
|
|---|
| 95 |
|
|---|
| [846] | 96 | @interface QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) : NSObject <NSWindowDelegate> {
|
|---|
| [2] | 97 | NSFontPanel *mFontPanel;
|
|---|
| 98 | NSView *mStolenContentView;
|
|---|
| 99 | NSButton *mOkButton;
|
|---|
| 100 | NSButton *mCancelButton;
|
|---|
| 101 | QFontDialogPrivate *mPriv;
|
|---|
| 102 | QFont *mQtFont;
|
|---|
| 103 | BOOL mPanelHackedWithButtons;
|
|---|
| 104 | CGFloat mDialogExtraWidth;
|
|---|
| 105 | CGFloat mDialogExtraHeight;
|
|---|
| [846] | 106 | int mReturnCode;
|
|---|
| 107 | BOOL mAppModal;
|
|---|
| [2] | 108 | }
|
|---|
| 109 | - (id)initWithFontPanel:(NSFontPanel *)panel
|
|---|
| 110 | stolenContentView:(NSView *)stolenContentView
|
|---|
| 111 | okButton:(NSButton *)okButton
|
|---|
| 112 | cancelButton:(NSButton *)cancelButton
|
|---|
| 113 | priv:(QFontDialogPrivate *)priv
|
|---|
| 114 | extraWidth:(CGFloat)extraWidth
|
|---|
| 115 | extraHeight:(CGFloat)extraHeight;
|
|---|
| [846] | 116 | - (void)showModelessPanel;
|
|---|
| 117 | - (void)showWindowModalSheet:(QWidget *)docWidget;
|
|---|
| 118 | - (void)runApplicationModalPanel;
|
|---|
| 119 | - (BOOL)isAppModal;
|
|---|
| [2] | 120 | - (void)changeFont:(id)sender;
|
|---|
| 121 | - (void)changeAttributes:(id)sender;
|
|---|
| 122 | - (BOOL)windowShouldClose:(id)window;
|
|---|
| 123 | - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
|
|---|
| 124 | - (void)relayout;
|
|---|
| 125 | - (void)relayoutToContentSize:(NSSize)frameSize;
|
|---|
| 126 | - (void)onOkClicked;
|
|---|
| 127 | - (void)onCancelClicked;
|
|---|
| 128 | - (NSFontPanel *)fontPanel;
|
|---|
| 129 | - (NSWindow *)actualPanel;
|
|---|
| 130 | - (NSSize)dialogExtraSize;
|
|---|
| 131 | - (void)setQtFont:(const QFont &)newFont;
|
|---|
| 132 | - (QFont)qtFont;
|
|---|
| 133 | - (void)finishOffWithCode:(NSInteger)result;
|
|---|
| 134 | - (void)cleanUpAfterMyself;
|
|---|
| [846] | 135 | - (void)setSubwindowStacking;
|
|---|
| [2] | 136 | @end
|
|---|
| 137 |
|
|---|
| [561] | 138 | static QFont qfontForCocoaFont(NSFont *cocoaFont, const QFont &resolveFont)
|
|---|
| 139 | {
|
|---|
| 140 | QFont newFont;
|
|---|
| 141 | if (cocoaFont) {
|
|---|
| 142 | int pSize = qRound([cocoaFont pointSize]);
|
|---|
| 143 | QString family(qt_mac_NSStringToQString([cocoaFont familyName]));
|
|---|
| 144 | QString typeface(qt_mac_NSStringToQString([cocoaFont fontName]));
|
|---|
| 145 |
|
|---|
| 146 | int hyphenPos = typeface.indexOf(QLatin1Char('-'));
|
|---|
| 147 | if (hyphenPos != -1) {
|
|---|
| 148 | typeface.remove(0, hyphenPos + 1);
|
|---|
| 149 | } else {
|
|---|
| 150 | typeface = QLatin1String("Normal");
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | newFont = QFontDatabase().font(family, typeface, pSize);
|
|---|
| 154 | newFont.setUnderline(resolveFont.underline());
|
|---|
| 155 | newFont.setStrikeOut(resolveFont.strikeOut());
|
|---|
| 156 |
|
|---|
| 157 | }
|
|---|
| 158 | return newFont;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| [846] | 161 | @implementation QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate)
|
|---|
| [2] | 162 | - (id)initWithFontPanel:(NSFontPanel *)panel
|
|---|
| 163 | stolenContentView:(NSView *)stolenContentView
|
|---|
| 164 | okButton:(NSButton *)okButton
|
|---|
| 165 | cancelButton:(NSButton *)cancelButton
|
|---|
| 166 | priv:(QFontDialogPrivate *)priv
|
|---|
| 167 | extraWidth:(CGFloat)extraWidth
|
|---|
| 168 | extraHeight:(CGFloat)extraHeight
|
|---|
| 169 | {
|
|---|
| 170 | self = [super init];
|
|---|
| 171 | mFontPanel = panel;
|
|---|
| 172 | mStolenContentView = stolenContentView;
|
|---|
| 173 | mOkButton = okButton;
|
|---|
| 174 | mCancelButton = cancelButton;
|
|---|
| 175 | mPriv = priv;
|
|---|
| 176 | mPanelHackedWithButtons = (okButton != 0);
|
|---|
| 177 | mDialogExtraWidth = extraWidth;
|
|---|
| 178 | mDialogExtraHeight = extraHeight;
|
|---|
| [846] | 179 | mReturnCode = -1;
|
|---|
| 180 | mAppModal = false;
|
|---|
| [2] | 181 |
|
|---|
| 182 | if (mPanelHackedWithButtons) {
|
|---|
| 183 | [self relayout];
|
|---|
| 184 |
|
|---|
| 185 | [okButton setAction:@selector(onOkClicked)];
|
|---|
| 186 | [okButton setTarget:self];
|
|---|
| 187 |
|
|---|
| 188 | [cancelButton setAction:@selector(onCancelClicked)];
|
|---|
| 189 | [cancelButton setTarget:self];
|
|---|
| 190 | }
|
|---|
| [846] | 191 |
|
|---|
| [2] | 192 | mQtFont = new QFont();
|
|---|
| 193 | return self;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| [846] | 196 | - (void)setSubwindowStacking
|
|---|
| 197 | {
|
|---|
| 198 | #ifdef QT_MAC_USE_COCOA
|
|---|
| 199 | // Stack the native dialog in front of its parent, if any:
|
|---|
| 200 | QFontDialog *q = mPriv->fontDialog();
|
|---|
| 201 | if (!qt_mac_is_macsheet(q)) {
|
|---|
| 202 | if (QWidget *parent = q->parentWidget()) {
|
|---|
| 203 | if (parent->isWindow()) {
|
|---|
| 204 | [qt_mac_window_for(parent)
|
|---|
| 205 | addChildWindow:[mStolenContentView window] ordered:NSWindowAbove];
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 | #endif
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| [2] | 212 | - (void)dealloc
|
|---|
| 213 | {
|
|---|
| 214 | delete mQtFont;
|
|---|
| 215 | [super dealloc];
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| [846] | 218 | - (void)showModelessPanel
|
|---|
| 219 | {
|
|---|
| 220 | mAppModal = false;
|
|---|
| 221 | NSWindow *ourPanel = [mStolenContentView window];
|
|---|
| 222 | [ourPanel makeKeyAndOrderFront:self];
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | - (void)runApplicationModalPanel
|
|---|
| 226 | {
|
|---|
| 227 | QBoolBlocker nativeDialogOnTop(QApplicationPrivate::native_modal_dialog_active);
|
|---|
| 228 | mAppModal = true;
|
|---|
| 229 | NSWindow *ourPanel = [mStolenContentView window];
|
|---|
| 230 | [ourPanel setReleasedWhenClosed:NO];
|
|---|
| 231 | [NSApp runModalForWindow:ourPanel];
|
|---|
| 232 | QAbstractEventDispatcher::instance()->interrupt();
|
|---|
| 233 |
|
|---|
| 234 | if (mReturnCode == NSOKButton)
|
|---|
| 235 | mPriv->fontDialog()->accept();
|
|---|
| 236 | else
|
|---|
| 237 | mPriv->fontDialog()->reject();
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | - (BOOL)isAppModal
|
|---|
| 241 | {
|
|---|
| 242 | return mAppModal;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | - (void)showWindowModalSheet:(QWidget *)docWidget
|
|---|
| 246 | {
|
|---|
| 247 | #ifdef QT_MAC_USE_COCOA
|
|---|
| 248 | NSWindow *window = qt_mac_window_for(docWidget);
|
|---|
| 249 | #else
|
|---|
| 250 | WindowRef hiwindowRef = qt_mac_window_for(docWidget);
|
|---|
| 251 | NSWindow *window = [[NSWindow alloc] initWithWindowRef:hiwindowRef];
|
|---|
| 252 | CFRetain(hiwindowRef);
|
|---|
| 253 | #endif
|
|---|
| 254 |
|
|---|
| 255 | mAppModal = false;
|
|---|
| 256 | NSWindow *ourPanel = [mStolenContentView window];
|
|---|
| 257 | [NSApp beginSheet:ourPanel
|
|---|
| 258 | modalForWindow:window
|
|---|
| 259 | modalDelegate:0
|
|---|
| 260 | didEndSelector:0
|
|---|
| 261 | contextInfo:0 ];
|
|---|
| 262 |
|
|---|
| 263 | #ifndef QT_MAC_USE_COCOA
|
|---|
| 264 | CFRelease(hiwindowRef);
|
|---|
| 265 | #endif
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| [2] | 268 | - (void)changeFont:(id)sender
|
|---|
| 269 | {
|
|---|
| 270 | NSFont *dummyFont = [NSFont userFontOfSize:12.0];
|
|---|
| [561] | 271 | [self setQtFont:qfontForCocoaFont([sender convertFont:dummyFont], *mQtFont)];
|
|---|
| [2] | 272 | if (mPriv)
|
|---|
| 273 | mPriv->updateSampleFont(*mQtFont);
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | - (void)changeAttributes:(id)sender
|
|---|
| 277 | {
|
|---|
| 278 | NSDictionary *dummyAttribs = [NSDictionary dictionary];
|
|---|
| 279 | NSDictionary *attribs = [sender convertAttributes:dummyAttribs];
|
|---|
| 280 |
|
|---|
| 281 | #ifdef QT_MAC_USE_COCOA
|
|---|
| 282 | for (id key in attribs) {
|
|---|
| 283 | #else
|
|---|
| 284 | NSEnumerator *enumerator = [attribs keyEnumerator];
|
|---|
| 285 | id key;
|
|---|
| 286 | while((key = [enumerator nextObject])) {
|
|---|
| 287 | #endif
|
|---|
| 288 | NSNumber *number = static_cast<NSNumber *>([attribs objectForKey:key]);
|
|---|
| 289 | if ([key isEqual:NSUnderlineStyleAttributeName]) {
|
|---|
| 290 | mQtFont->setUnderline([number intValue] != NSUnderlineStyleNone);
|
|---|
| 291 | } else if ([key isEqual:NSStrikethroughStyleAttributeName]) {
|
|---|
| 292 | mQtFont->setStrikeOut([number intValue] != NSUnderlineStyleNone);
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | if (mPriv)
|
|---|
| 297 | mPriv->updateSampleFont(*mQtFont);
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | - (BOOL)windowShouldClose:(id)window
|
|---|
| 301 | {
|
|---|
| 302 | Q_UNUSED(window);
|
|---|
| 303 | if (mPanelHackedWithButtons) {
|
|---|
| 304 | [self onCancelClicked];
|
|---|
| 305 | } else {
|
|---|
| 306 | [self finishOffWithCode:NSCancelButton];
|
|---|
| 307 | }
|
|---|
| 308 | return true;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
|
|---|
| 312 | {
|
|---|
| 313 | if (mFontPanel == window) {
|
|---|
| 314 | proposedFrameSize = [static_cast<id <NSWindowDelegate> >(mFontPanel) windowWillResize:mFontPanel toSize:proposedFrameSize];
|
|---|
| 315 | } else {
|
|---|
| 316 | /*
|
|---|
| 317 | Ugly hack: NSFontPanel rearranges the layout of its main
|
|---|
| 318 | component in windowWillResize:toSize:. So we temporarily
|
|---|
| 319 | restore the stolen content view to its rightful owner,
|
|---|
| 320 | call windowWillResize:toSize:, and steal the content view
|
|---|
| 321 | again.
|
|---|
| 322 | */
|
|---|
| 323 | [mStolenContentView removeFromSuperview];
|
|---|
| 324 | [mFontPanel setContentView:mStolenContentView];
|
|---|
| 325 | NSSize extraSize = [self dialogExtraSize];
|
|---|
| 326 | proposedFrameSize.width -= extraSize.width;
|
|---|
| 327 | proposedFrameSize.height -= extraSize.height;
|
|---|
| 328 | proposedFrameSize = [static_cast<id <NSWindowDelegate> >(mFontPanel) windowWillResize:mFontPanel toSize:proposedFrameSize];
|
|---|
| 329 | NSRect frameRect = { { 0.0, 0.0 }, proposedFrameSize };
|
|---|
| 330 | [mFontPanel setFrame:frameRect display:NO];
|
|---|
| 331 | [mFontPanel setContentView:0];
|
|---|
| 332 | [[window contentView] addSubview:mStolenContentView];
|
|---|
| 333 | proposedFrameSize.width += extraSize.width;
|
|---|
| 334 | proposedFrameSize.height += extraSize.height;
|
|---|
| 335 | }
|
|---|
| 336 | if (mPanelHackedWithButtons) {
|
|---|
| 337 | NSRect frameRect = { { 0.0, 0.0 }, proposedFrameSize };
|
|---|
| 338 | NSRect contentRect = [NSWindow contentRectForFrameRect:frameRect styleMask:[window styleMask]];
|
|---|
| 339 | [self relayoutToContentSize:contentRect.size];
|
|---|
| 340 | }
|
|---|
| 341 | return proposedFrameSize;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | - (void)relayout
|
|---|
| 345 | {
|
|---|
| 346 | [self relayoutToContentSize:[[mStolenContentView superview] frame].size];
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | - (void)relayoutToContentSize:(NSSize)frameSize;
|
|---|
| 350 | {
|
|---|
| 351 | Q_ASSERT(mPanelHackedWithButtons);
|
|---|
| 352 |
|
|---|
| 353 | [mOkButton sizeToFit];
|
|---|
| 354 | NSSize okSizeHint = [mOkButton frame].size;
|
|---|
| 355 |
|
|---|
| 356 | [mCancelButton sizeToFit];
|
|---|
| 357 | NSSize cancelSizeHint = [mCancelButton frame].size;
|
|---|
| 358 |
|
|---|
| 359 | const CGFloat ButtonWidth = qMin(qMax(ButtonMinWidth,
|
|---|
| [846] | 360 | qMax(okSizeHint.width, cancelSizeHint.width)),
|
|---|
| 361 | CGFloat((frameSize.width - 2.0 * ButtonSideMargin - ButtonSpacing) * 0.5));
|
|---|
| [2] | 362 | const CGFloat ButtonHeight = qMax(ButtonMinHeight,
|
|---|
| 363 | qMax(okSizeHint.height, cancelSizeHint.height));
|
|---|
| 364 |
|
|---|
| 365 | const CGFloat X = DialogSideMargin;
|
|---|
| 366 | const CGFloat Y = ButtonBottomMargin + ButtonHeight + ButtonTopMargin;
|
|---|
| 367 |
|
|---|
| 368 | NSRect okRect = { { frameSize.width - ButtonSideMargin - ButtonWidth,
|
|---|
| 369 | ButtonBottomMargin },
|
|---|
| 370 | { ButtonWidth, ButtonHeight } };
|
|---|
| 371 | [mOkButton setFrame:okRect];
|
|---|
| 372 | [mOkButton setNeedsDisplay:YES];
|
|---|
| 373 |
|
|---|
| 374 | NSRect cancelRect = { { okRect.origin.x - ButtonSpacing - ButtonWidth,
|
|---|
| 375 | ButtonBottomMargin },
|
|---|
| 376 | { ButtonWidth, ButtonHeight } };
|
|---|
| 377 | [mCancelButton setFrame:cancelRect];
|
|---|
| 378 | [mCancelButton setNeedsDisplay:YES];
|
|---|
| 379 |
|
|---|
| 380 | NSRect stolenCVRect = { { X, Y },
|
|---|
| 381 | { frameSize.width - X - X, frameSize.height - Y - DialogTopMargin } };
|
|---|
| 382 | [mStolenContentView setFrame:stolenCVRect];
|
|---|
| 383 | [mStolenContentView setNeedsDisplay:YES];
|
|---|
| 384 |
|
|---|
| 385 | [[mStolenContentView superview] setNeedsDisplay:YES];
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | - (void)onOkClicked
|
|---|
| 389 | {
|
|---|
| 390 | Q_ASSERT(mPanelHackedWithButtons);
|
|---|
| [561] | 391 | NSFontManager *fontManager = [NSFontManager sharedFontManager];
|
|---|
| 392 | [self setQtFont:qfontForCocoaFont([fontManager convertFont:[fontManager selectedFont]],
|
|---|
| 393 | *mQtFont)];
|
|---|
| [2] | 394 | [self finishOffWithCode:NSOKButton];
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | - (void)onCancelClicked
|
|---|
| 398 | {
|
|---|
| 399 | Q_ASSERT(mPanelHackedWithButtons);
|
|---|
| 400 | [self finishOffWithCode:NSCancelButton];
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | - (NSFontPanel *)fontPanel
|
|---|
| 404 | {
|
|---|
| 405 | return mFontPanel;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | - (NSWindow *)actualPanel
|
|---|
| 409 | {
|
|---|
| 410 | return [mStolenContentView window];
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | - (NSSize)dialogExtraSize
|
|---|
| 414 | {
|
|---|
| 415 | // this must be recomputed each time, because sometimes the
|
|---|
| 416 | // NSFontPanel has the NSDocModalWindowMask flag set, and sometimes
|
|---|
| 417 | // not -- which affects the frame rect vs. content rect measurements
|
|---|
| 418 |
|
|---|
| 419 | // take the different frame rectangles into account for dialogExtra{Width,Height}
|
|---|
| 420 | NSRect someRect = { { 0.0, 0.0 }, { 100000.0, 100000.0 } };
|
|---|
| 421 | NSRect sharedFontPanelContentRect = [mFontPanel contentRectForFrameRect:someRect];
|
|---|
| 422 | NSRect ourPanelContentRect = [NSWindow contentRectForFrameRect:someRect styleMask:StyleMask];
|
|---|
| 423 |
|
|---|
| 424 | NSSize result = { mDialogExtraWidth, mDialogExtraHeight };
|
|---|
| 425 | result.width -= ourPanelContentRect.size.width - sharedFontPanelContentRect.size.width;
|
|---|
| 426 | result.height -= ourPanelContentRect.size.height - sharedFontPanelContentRect.size.height;
|
|---|
| 427 | return result;
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | - (void)setQtFont:(const QFont &)newFont
|
|---|
| 431 | {
|
|---|
| 432 | delete mQtFont;
|
|---|
| 433 | mQtFont = new QFont(newFont);
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | - (QFont)qtFont
|
|---|
| 437 | {
|
|---|
| 438 | return *mQtFont;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | - (void)finishOffWithCode:(NSInteger)code
|
|---|
| 442 | {
|
|---|
| [846] | 443 | #ifdef QT_MAC_USE_COCOA
|
|---|
| 444 | QFontDialog *q = mPriv->fontDialog();
|
|---|
| 445 | if (QWidget *parent = q->parentWidget()) {
|
|---|
| 446 | if (parent->isWindow()) {
|
|---|
| 447 | [qt_mac_window_for(parent) removeChildWindow:[mStolenContentView window]];
|
|---|
| [2] | 448 | }
|
|---|
| [846] | 449 | }
|
|---|
| 450 | #endif
|
|---|
| 451 |
|
|---|
| 452 | if(code == NSOKButton)
|
|---|
| 453 | mPriv->sampleEdit->setFont([self qtFont]);
|
|---|
| 454 |
|
|---|
| 455 | if (mAppModal) {
|
|---|
| 456 | mReturnCode = code;
|
|---|
| 457 | [NSApp stopModalWithCode:code];
|
|---|
| [2] | 458 | } else {
|
|---|
| [846] | 459 | if (code == NSOKButton)
|
|---|
| 460 | mPriv->fontDialog()->accept();
|
|---|
| 461 | else
|
|---|
| 462 | mPriv->fontDialog()->reject();
|
|---|
| [2] | 463 | }
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | - (void)cleanUpAfterMyself
|
|---|
| 467 | {
|
|---|
| 468 | if (mPanelHackedWithButtons) {
|
|---|
| 469 | NSView *ourContentView = [mFontPanel contentView];
|
|---|
| 470 |
|
|---|
| 471 | // return stolen stuff to its rightful owner
|
|---|
| 472 | [mStolenContentView removeFromSuperview];
|
|---|
| 473 | [mFontPanel setContentView:mStolenContentView];
|
|---|
| 474 |
|
|---|
| 475 | [mOkButton release];
|
|---|
| 476 | [mCancelButton release];
|
|---|
| 477 | [ourContentView release];
|
|---|
| 478 | }
|
|---|
| 479 | [mFontPanel setDelegate:nil];
|
|---|
| 480 | [[NSFontManager sharedFontManager] setDelegate:nil];
|
|---|
| [561] | 481 | #ifdef QT_MAC_USE_COCOA
|
|---|
| [2] | 482 | [[NSFontManager sharedFontManager] setTarget:nil];
|
|---|
| [561] | 483 | #endif
|
|---|
| [2] | 484 | }
|
|---|
| 485 | @end
|
|---|
| 486 |
|
|---|
| 487 | QT_BEGIN_NAMESPACE
|
|---|
| 488 |
|
|---|
| [846] | 489 | void QFontDialogPrivate::closeCocoaFontPanel()
|
|---|
| [2] | 490 | {
|
|---|
| 491 | QMacCocoaAutoReleasePool pool;
|
|---|
| [846] | 492 | QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) *theDelegate = static_cast<QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) *>(delegate);
|
|---|
| [2] | 493 | NSWindow *ourPanel = [theDelegate actualPanel];
|
|---|
| 494 | [ourPanel close];
|
|---|
| [846] | 495 | if ([theDelegate isAppModal])
|
|---|
| 496 | [ourPanel release];
|
|---|
| [2] | 497 | [theDelegate cleanUpAfterMyself];
|
|---|
| [846] | 498 | [theDelegate release];
|
|---|
| 499 | this->delegate = 0;
|
|---|
| 500 | sharedFontPanelAvailable = true;
|
|---|
| [2] | 501 | }
|
|---|
| 502 |
|
|---|
| [561] | 503 | void QFontDialogPrivate::setFont(void *delegate, const QFont &font)
|
|---|
| [2] | 504 | {
|
|---|
| [561] | 505 | QMacCocoaAutoReleasePool pool;
|
|---|
| [2] | 506 | QFontEngine *fe = font.d->engineForScript(QUnicodeTables::Common);
|
|---|
| [561] | 507 | NSFontManager *mgr = [NSFontManager sharedFontManager];
|
|---|
| 508 | NSFont *nsFont = 0;
|
|---|
| 509 |
|
|---|
| [2] | 510 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
|---|
| 511 | if (qstrcmp(fe->name(), "CoreText") == 0) {
|
|---|
| [561] | 512 | nsFont = reinterpret_cast<const NSFont *>(static_cast<QCoreTextFontEngineMulti *>(fe)->ctfont);
|
|---|
| 513 | } else
|
|---|
| 514 | #endif
|
|---|
| |
|---|