Qt 4.8
qfontdialog_mac.mm
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
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_cocoa_helpers_mac_p.h>
51 #include <private/qt_mac_p.h>
53 #include <qdebug.h>
54 #include <private/qfontengine_coretext_p.h>
55 #import <AppKit/AppKit.h>
56 #import <Foundation/Foundation.h>
57 
58 #if !CGFLOAT_DEFINED
59 typedef float CGFloat; // Should only not be defined on 32-bit platforms
60 #endif
61 
63 
64 extern void macStartInterceptNSPanelCtor();
65 extern void macStopInterceptNSPanelCtor();
66 extern NSButton *macCreateButton(const char *text, NSView *superview);
67 extern bool qt_mac_is_macsheet(const QWidget *w); // qwidget_mac.mm
68 
71 
72 // should a priori be kept in sync with qcolordialog_mac.mm
73 const CGFloat ButtonMinWidth = 78.0;
74 const CGFloat ButtonMinHeight = 32.0;
75 const CGFloat ButtonSpacing = 0.0;
79 
80 // looks better with some margins
83 
84 const int StyleMask = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
85 
87 
88 
89 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
90 
91 @protocol NSWindowDelegate <NSObject>
92 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
93 @end
94 
95 #endif
96 
97 @interface QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) : NSObject <NSWindowDelegate> {
98  NSFontPanel *mFontPanel;
100  NSButton *mOkButton;
101  NSButton *mCancelButton;
108  BOOL mAppModal;
109 }
110 - (id)initWithFontPanel:(NSFontPanel *)panel
111  stolenContentView:(NSView *)stolenContentView
112  okButton:(NSButton *)okButton
113  cancelButton:(NSButton *)cancelButton
114  priv:(QFontDialogPrivate *)priv
115  extraWidth:(CGFloat)extraWidth
116  extraHeight:(CGFloat)extraHeight;
117 - (void)showModelessPanel;
118 - (void)showWindowModalSheet:(QWidget *)docWidget;
119 - (void)runApplicationModalPanel;
120 - (BOOL)isAppModal;
121 - (void)changeFont:(id)sender;
122 - (void)changeAttributes:(id)sender;
123 - (BOOL)windowShouldClose:(id)window;
124 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
125 - (void)relayout;
126 - (void)relayoutToContentSize:(NSSize)frameSize;
127 - (void)onOkClicked;
128 - (void)onCancelClicked;
129 - (NSFontPanel *)fontPanel;
130 - (NSWindow *)actualPanel;
131 - (NSSize)dialogExtraSize;
132 - (void)setQtFont:(const QFont &)newFont;
133 - (QFont)qtFont;
134 - (void)finishOffWithCode:(NSInteger)result;
135 - (void)cleanUpAfterMyself;
136 - (void)setSubwindowStacking;
137 @end
138 
139 static QFont qfontForCocoaFont(NSFont *cocoaFont, const QFont &resolveFont)
140 {
141  QFont newFont;
142  if (cocoaFont) {
143  int pSize = qRound([cocoaFont pointSize]);
144  CTFontDescriptorRef font = CTFontCopyFontDescriptor((CTFontRef)cocoaFont);
145  // QCoreTextFontDatabase::populateFontDatabase() is using localized names
146  QString family = QCFString::toQString((CFStringRef) CTFontDescriptorCopyLocalizedAttribute(font, kCTFontFamilyNameAttribute, NULL));
147  QString style = QCFString::toQString((CFStringRef) CTFontDescriptorCopyLocalizedAttribute(font, kCTFontStyleNameAttribute, NULL));
148 
149  newFont = QFontDatabase().font(family, style, pSize);
150  newFont.setUnderline(resolveFont.underline());
151  newFont.setStrikeOut(resolveFont.strikeOut());
152 
153  CFRelease(font);
154  }
155  return newFont;
156 }
157 
158 @implementation QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate)
159 - (id)initWithFontPanel:(NSFontPanel *)panel
160  stolenContentView:(NSView *)stolenContentView
161  okButton:(NSButton *)okButton
162  cancelButton:(NSButton *)cancelButton
163  priv:(QFontDialogPrivate *)priv
164  extraWidth:(CGFloat)extraWidth
165  extraHeight:(CGFloat)extraHeight
166 {
167  self = [super init];
168  mFontPanel = panel;
169  mStolenContentView = stolenContentView;
170  mOkButton = okButton;
171  mCancelButton = cancelButton;
172  mPriv = priv;
173  mPanelHackedWithButtons = (okButton != 0);
174  mDialogExtraWidth = extraWidth;
175  mDialogExtraHeight = extraHeight;
176  mReturnCode = -1;
177  mAppModal = false;
178 
179 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
181  [mFontPanel setRestorable:NO];
182 #endif
183 
184  if (mPanelHackedWithButtons) {
185  [self relayout];
186 
187  [okButton setAction:@selector(onOkClicked)];
188  [okButton setTarget:self];
189 
190  [cancelButton setAction:@selector(onCancelClicked)];
191  [cancelButton setTarget:self];
192  }
193 
194  mQtFont = new QFont();
195  return self;
196 }
197 
198 - (void)setSubwindowStacking
199 {
200 #ifdef QT_MAC_USE_COCOA
201  // Stack the native dialog in front of its parent, if any:
202  QFontDialog *q = mPriv->fontDialog();
203  if (!qt_mac_is_macsheet(q)) {
204  if (QWidget *parent = q->parentWidget()) {
205  if (parent->isWindow()) {
206  [qt_mac_window_for(parent)
207  addChildWindow:[mStolenContentView window] ordered:NSWindowAbove];
208  }
209  }
210  }
211 #endif
212 }
213 
214 - (void)dealloc
215 {
216  delete mQtFont;
217  [super dealloc];
218 }
219 
220 - (void)showModelessPanel
221 {
222  mAppModal = false;
223  NSWindow *ourPanel = [mStolenContentView window];
224  [ourPanel makeKeyAndOrderFront:self];
225 }
226 
227 - (void)runApplicationModalPanel
228 {
230  mAppModal = true;
231  NSWindow *ourPanel = [mStolenContentView window];
232  [ourPanel setReleasedWhenClosed:NO];
233  [NSApp runModalForWindow:ourPanel];
235 
236  if (mReturnCode == NSOKButton)
237  mPriv->fontDialog()->accept();
238  else
239  mPriv->fontDialog()->reject();
240 }
241 
242 - (BOOL)isAppModal
243 {
244  return mAppModal;
245 }
246 
247 - (void)showWindowModalSheet:(QWidget *)docWidget
248 {
249 #ifdef QT_MAC_USE_COCOA
250  NSWindow *window = qt_mac_window_for(docWidget);
251 #else
252  WindowRef hiwindowRef = qt_mac_window_for(docWidget);
253  NSWindow *window = [[NSWindow alloc] initWithWindowRef:hiwindowRef];
254  CFRetain(hiwindowRef);
255 #endif
256 
257  mAppModal = false;
258  NSWindow *ourPanel = [mStolenContentView window];
259  [NSApp beginSheet:ourPanel
260  modalForWindow:window
261  modalDelegate:0
262  didEndSelector:0
263  contextInfo:0 ];
264 
265 #ifndef QT_MAC_USE_COCOA
266  CFRelease(hiwindowRef);
267 #endif
268 }
269 
270 - (void)changeFont:(id)sender
271 {
272  NSFont *dummyFont = [NSFont userFontOfSize:12.0];
273  [self setQtFont:qfontForCocoaFont([sender convertFont:dummyFont], *mQtFont)];
274  if (mPriv)
275  mPriv->updateSampleFont(*mQtFont);
276 }
277 
278 - (void)changeAttributes:(id)sender
279 {
280  NSDictionary *dummyAttribs = [NSDictionary dictionary];
281  NSDictionary *attribs = [sender convertAttributes:dummyAttribs];
282 
283 #ifdef QT_MAC_USE_COCOA
284  for (id key in attribs) {
285 #else
286  NSEnumerator *enumerator = [attribs keyEnumerator];
287  id key;
288  while((key = [enumerator nextObject])) {
289 #endif
290  NSNumber *number = static_cast<NSNumber *>([attribs objectForKey:key]);
291  if ([key isEqual:NSUnderlineStyleAttributeName]) {
292  mQtFont->setUnderline([number intValue] != NSUnderlineStyleNone);
293  } else if ([key isEqual:NSStrikethroughStyleAttributeName]) {
294  mQtFont->setStrikeOut([number intValue] != NSUnderlineStyleNone);
295  }
296  }
297 
298  if (mPriv)
299  mPriv->updateSampleFont(*mQtFont);
300 }
301 
302 - (BOOL)windowShouldClose:(id)window
303 {
304  Q_UNUSED(window);
305  if (mPanelHackedWithButtons) {
306  [self onCancelClicked];
307  } else {
308  [self finishOffWithCode:NSCancelButton];
309  }
310  return true;
311 }
312 
313 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
314 {
315  if (mFontPanel == window) {
316  proposedFrameSize = [static_cast<id <NSWindowDelegate> >(mFontPanel) windowWillResize:mFontPanel toSize:proposedFrameSize];
317  } else {
318  /*
319  Ugly hack: NSFontPanel rearranges the layout of its main
320  component in windowWillResize:toSize:. So we temporarily
321  restore the stolen content view to its rightful owner,
322  call windowWillResize:toSize:, and steal the content view
323  again.
324  */
325  [mStolenContentView removeFromSuperview];
326  [mFontPanel setContentView:mStolenContentView];
327  NSSize extraSize = [self dialogExtraSize];
328  proposedFrameSize.width -= extraSize.width;
329  proposedFrameSize.height -= extraSize.height;
330  proposedFrameSize = [static_cast<id <NSWindowDelegate> >(mFontPanel) windowWillResize:mFontPanel toSize:proposedFrameSize];
331  NSRect frameRect = { { 0.0, 0.0 }, proposedFrameSize };
332  [mFontPanel setFrame:frameRect display:NO];
333  [mFontPanel setContentView:0];
334  [[window contentView] addSubview:mStolenContentView];
335  proposedFrameSize.width += extraSize.width;
336  proposedFrameSize.height += extraSize.height;
337  }
338  if (mPanelHackedWithButtons) {
339  NSRect frameRect = { { 0.0, 0.0 }, proposedFrameSize };
340  NSRect contentRect = [NSWindow contentRectForFrameRect:frameRect styleMask:[window styleMask]];
341  [self relayoutToContentSize:contentRect.size];
342  }
343  return proposedFrameSize;
344 }
345 
346 - (void)relayout
347 {
348  [self relayoutToContentSize:[[mStolenContentView superview] frame].size];
349 }
350 
351 - (void)relayoutToContentSize:(NSSize)frameSize
352 {
353  Q_ASSERT(mPanelHackedWithButtons);
354 
355  [mOkButton sizeToFit];
356  NSSize okSizeHint = [mOkButton frame].size;
357 
358  [mCancelButton sizeToFit];
359  NSSize cancelSizeHint = [mCancelButton frame].size;
360 
361  const CGFloat ButtonWidth = qMin(qMax(ButtonMinWidth,
362  qMax(okSizeHint.width, cancelSizeHint.width)),
363  CGFloat((frameSize.width - 2.0 * ButtonSideMargin - ButtonSpacing) * 0.5));
364  const CGFloat ButtonHeight = qMax(ButtonMinHeight,
365  qMax(okSizeHint.height, cancelSizeHint.height));
366 
367  const CGFloat X = DialogSideMargin;
368  const CGFloat Y = ButtonBottomMargin + ButtonHeight + ButtonTopMargin;
369 
370  NSRect okRect = { { frameSize.width - ButtonSideMargin - ButtonWidth,
372  { ButtonWidth, ButtonHeight } };
373  [mOkButton setFrame:okRect];
374  [mOkButton setNeedsDisplay:YES];
375 
376  NSRect cancelRect = { { okRect.origin.x - ButtonSpacing - ButtonWidth,
378  { ButtonWidth, ButtonHeight } };
379  [mCancelButton setFrame:cancelRect];
380  [mCancelButton setNeedsDisplay:YES];
381 
382  NSRect stolenCVRect = { { X, Y },
383  { frameSize.width - X - X, frameSize.height - Y - DialogTopMargin } };
384  [mStolenContentView setFrame:stolenCVRect];
385  [mStolenContentView setNeedsDisplay:YES];
386 
387  [[mStolenContentView superview] setNeedsDisplay:YES];
388 }
389 
390 - (void)onOkClicked
391 {
392  Q_ASSERT(mPanelHackedWithButtons);
393  NSFontManager *fontManager = [NSFontManager sharedFontManager];
394  [self setQtFont:qfontForCocoaFont([fontManager convertFont:[fontManager selectedFont]],
395  *mQtFont)];
396  [self finishOffWithCode:NSOKButton];
397 }
398 
399 - (void)onCancelClicked
400 {
401  Q_ASSERT(mPanelHackedWithButtons);
402  [self finishOffWithCode:NSCancelButton];
403 }
404 
405 - (NSFontPanel *)fontPanel
406 {
407  return mFontPanel;
408 }
409 
410 - (NSWindow *)actualPanel
411 {
412  return [mStolenContentView window];
413 }
414 
415 - (NSSize)dialogExtraSize
416 {
417  // this must be recomputed each time, because sometimes the
418  // NSFontPanel has the NSDocModalWindowMask flag set, and sometimes
419  // not -- which affects the frame rect vs. content rect measurements
420 
421  // take the different frame rectangles into account for dialogExtra{Width,Height}
422  NSRect someRect = { { 0.0, 0.0 }, { 100000.0, 100000.0 } };
423  NSRect sharedFontPanelContentRect = [mFontPanel contentRectForFrameRect:someRect];
424  NSRect ourPanelContentRect = [NSWindow contentRectForFrameRect:someRect styleMask:StyleMask];
425 
426  NSSize result = { mDialogExtraWidth, mDialogExtraHeight };
427  result.width -= ourPanelContentRect.size.width - sharedFontPanelContentRect.size.width;
428  result.height -= ourPanelContentRect.size.height - sharedFontPanelContentRect.size.height;
429  return result;
430 }
431 
432 - (void)setQtFont:(const QFont &)newFont
433 {
434  delete mQtFont;
435  mQtFont = new QFont(newFont);
436 }
437 
438 - (QFont)qtFont
439 {
440  return *mQtFont;
441 }
442 
443 - (void)finishOffWithCode:(NSInteger)code
444 {
445 #ifdef QT_MAC_USE_COCOA
446  QFontDialog *q = mPriv->fontDialog();
447  if (QWidget *parent = q->parentWidget()) {
448  if (parent->isWindow()) {
449  [qt_mac_window_for(parent) removeChildWindow:[mStolenContentView window]];
450  }
451  }
452 #endif
453 
454  if(code == NSOKButton)
455  mPriv->sampleEdit->setFont([self qtFont]);
456 
457  if (mAppModal) {
458  mReturnCode = code;
459  [NSApp stopModalWithCode:code];
460  } else {
461  if (code == NSOKButton)
462  mPriv->fontDialog()->accept();
463  else
464  mPriv->fontDialog()->reject();
465  }
466 }
467 
468 - (void)cleanUpAfterMyself
469 {
470  if (mPanelHackedWithButtons) {
471  NSView *ourContentView = [mFontPanel contentView];
472 
473  // return stolen stuff to its rightful owner
474  [mStolenContentView removeFromSuperview];
475  [mFontPanel setContentView:mStolenContentView];
476 
477  [mOkButton release];
478  [mCancelButton release];
479  [ourContentView release];
480  }
481  [mFontPanel setDelegate:nil];
482  [[NSFontManager sharedFontManager] setDelegate:nil];
483 #ifdef QT_MAC_USE_COCOA
484  [[NSFontManager sharedFontManager] setTarget:nil];
485 #endif
486 }
487 @end
488 
490 
492 {
495  NSWindow *ourPanel = [theDelegate actualPanel];
496  [ourPanel close];
497  if ([theDelegate isAppModal])
498  [ourPanel release];
499  [theDelegate cleanUpAfterMyself];
500  [theDelegate release];
501  this->delegate = 0;
502  sharedFontPanelAvailable = true;
503 }
504 
505 void QFontDialogPrivate::setFont(void *delegate, const QFont &font)
506 {
509  NSFontManager *mgr = [NSFontManager sharedFontManager];
510  const NSFont *nsFont = 0;
511 
512 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
513  if (qstrcmp(fe->name(), "CoreText") == 0) {
514  nsFont = reinterpret_cast<const NSFont *>(static_cast<QCoreTextFontEngineMulti *>(fe)->ctfont);
515  } else
516 #endif
517  {
518  int weight = 5;
519  NSFontTraitMask mask = 0;
520  if (font.style() == QFont::StyleItalic) {
521  mask |= NSItalicFontMask;
522  }
523  if (font.weight() == QFont::Bold) {
524  weight = 9;
525  mask |= NSBoldFontMask;
526  }
527 
528  NSFontManager *mgr = [NSFontManager sharedFontManager];
529  QFontInfo fontInfo(font);
530  nsFont = [mgr fontWithFamily:qt_mac_QStringToNSString(fontInfo.family())
531  traits:mask
532  weight:weight
533  size:fontInfo.pointSize()];
534  }
535 
536  [mgr setSelectedFont:const_cast<NSFont *>(nsFont) isMultiple:NO];
537  [static_cast<QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) *>(delegate) setQtFont:font];
538 }
539 
541 {
542  if (delegate)
543  return;
544 
545  sharedFontPanelAvailable = false;
547  bool sharedFontPanelExisted = [NSFontPanel sharedFontPanelExists];
548  NSFontPanel *sharedFontPanel = [NSFontPanel sharedFontPanel];
549  [sharedFontPanel setHidesOnDeactivate:false];
550 
551  // hack to ensure that QCocoaApplication's validModesForFontPanel:
552  // implementation is honored
553  if (!sharedFontPanelExisted) {
554  [sharedFontPanel makeKeyAndOrderFront:sharedFontPanel];
555  [sharedFontPanel close];
556  }
557 
558  NSPanel *ourPanel = 0;
559  NSView *stolenContentView = 0;
560  NSButton *okButton = 0;
561  NSButton *cancelButton = 0;
562 
563  CGFloat dialogExtraWidth = 0.0;
564  CGFloat dialogExtraHeight = 0.0;
565 
566  // compute dialogExtra{Width,Height}
567  dialogExtraWidth = 2.0 * DialogSideMargin;
569 
570  // compute initial contents rectangle
571  NSRect contentRect = [sharedFontPanel contentRectForFrameRect:[sharedFontPanel frame]];
572  contentRect.size.width += dialogExtraWidth;
573  contentRect.size.height += dialogExtraHeight;
574 
575  // create the new panel
576  ourPanel = [[NSPanel alloc] initWithContentRect:contentRect
577  styleMask:StyleMask
578  backing:NSBackingStoreBuffered
579  defer:YES];
580  [ourPanel setReleasedWhenClosed:YES];
581  stolenContentView = [sharedFontPanel contentView];
582 
583  // steal the font panel's contents view
584  [stolenContentView retain];
585  [sharedFontPanel setContentView:0];
586 
587  {
588  // create a new content view and add the stolen one as a subview
589  NSRect frameRect = { { 0.0, 0.0 }, { 0.0, 0.0 } };
590  NSView *ourContentView = [[NSView alloc] initWithFrame:frameRect];
591  [ourContentView addSubview:stolenContentView];
592 
593  // create OK and Cancel buttons and add these as subviews
594  okButton = macCreateButton("&OK", ourContentView);
595  cancelButton = macCreateButton("Cancel", ourContentView);
596 
597  [ourPanel setContentView:ourContentView];
598  [ourPanel setDefaultButtonCell:[okButton cell]];
599  }
600 
601  // create the delegate and set it
602  QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) *del = [[QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) alloc] initWithFontPanel:sharedFontPanel
603  stolenContentView:stolenContentView
604  okButton:okButton
605  cancelButton:cancelButton
606  priv:this
607  extraWidth:dialogExtraWidth
608  extraHeight:dialogExtraHeight];
609  delegate = del;
610  [ourPanel setDelegate:del];
611 
612  [[NSFontManager sharedFontManager] setDelegate:del];
613 #ifdef QT_MAC_USE_COCOA
614  [[NSFontManager sharedFontManager] setTarget:del];
615 #endif
616  setFont(del, q_func()->currentFont());
617 
618  {
619  // hack to get correct initial layout
620  NSRect frameRect = [ourPanel frame];
621  frameRect.size.width += 1.0;
622  [ourPanel setFrame:frameRect display:NO];
623  frameRect.size.width -= 1.0;
624  frameRect.size = [del windowWillResize:ourPanel toSize:frameRect.size];
625  [ourPanel setFrame:frameRect display:NO];
626  [ourPanel center];
627  }
628  [del setSubwindowStacking];
629  NSString *title = @"Select font";
630  [ourPanel setTitle:title];
631 }
632 
634 {
635  // Copied from QFileDialogPrivate
636  // Do a queued meta-call to open the native modal dialog so it opens after the new
637  // event loop has started to execute (in QDialog::exec). Using a timer rather than
638  // a queued meta call is intentional to ensure that the call is only delivered when
639  // [NSApp run] runs (timers are handeled special in cocoa). If NSApp is not
640  // running (which is the case if e.g a top-most QEventLoop has been
641  // interrupted, and the second-most event loop has not yet been reactivated (regardless
642  // if [NSApp run] is still on the stack)), showing a native modal dialog will fail.
643  if (nativeDialogInUse) {
644  Q_Q(QFontDialog);
645  QTimer::singleShot(1, q, SLOT(_q_macRunNativeAppModalPanel()));
646  }
647 }
648 
649 // The problem with the native font dialog is that OS X does not
650 // offer a proper dialog, but a panel (i.e. without Ok and Cancel buttons).
651 // This means we need to "construct" a native dialog by taking the panel
652 // and "adding" the buttons.
654 {
655  createNSFontPanelDelegate();
657  [del runApplicationModalPanel];
658 }
659 
661 {
662  if (!sharedFontPanelAvailable)
663  return false;
664 
665  Q_Q(QFontDialog);
667  createNSFontPanelDelegate();
669  if (qt_mac_is_macsheet(q))
670  [del showWindowModalSheet:q->parentWidget()];
671  else
672  [del showModelessPanel];
673  return true;
674 }
675 
677 {
678  if (!delegate){
679  // Nothing to do. We return false to leave the question
680  // open regarding whether or not to go native:
681  return false;
682  } else {
683  closeCocoaFontPanel();
684  // Even when we hide it, we are still using a
685  // native dialog, so return true:
686  return true;
687  }
688 }
690 {
691  Q_Q(QFontDialog);
692  if (!visible == q->isHidden())
693  return false;
694 
695  return visible ? showCocoaFontPanel() : hideCocoaFontPanel();
696 }
697 
699 
700 #endif
const struct __CFString * CFStringRef
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
void setFont(const QFont &)
Use the single-argument overload instead.
Definition: qwidget.cpp:4996
const CGFloat ButtonSideMargin
QFont font(const QString &family, const QString &style, int pointSize) const
Returns a QFont object that has family family, style style and point size pointSize.
virtual void interrupt()=0
Interrupts event dispatching; i.
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
const CGFloat DialogTopMargin
const CGFloat ButtonBottomMargin
float CGFloat
void relayoutToContentSize:(NSSize frameSize)
bool underline() const
Returns true if underline has been set; otherwise returns false.
Definition: qfont.cpp:1320
struct OpaqueWindowPtr * WindowRef
void setUnderline(bool)
If enable is true, sets underline on; otherwise sets underline off.
Definition: qfont.cpp:1331
static QAbstractEventDispatcher * instance(QThread *thread=0)
Returns a pointer to the event dispatcher object for the specified thread.
#define SLOT(a)
Definition: qobjectdefs.h:226
Style style() const
Returns the style of the font.
Definition: qfont.cpp:1223
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
const CGFloat ButtonSpacing
void _q_macRunNativeAppModalPanel()
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
bool setVisible_sys(bool visible)
OSWindowRef qt_mac_window_for(const QWidget *)
Definition: qwidget_mac.mm:484
#define Q_Q(Class)
Definition: qglobal.h:2483
bool isHidden() const
Returns true if the widget is hidden, otherwise returns false.
Definition: qwidget.h:1008
const CGFloat DialogSideMargin
NSWindow * window
virtual const char * name() const =0
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QFontDatabase class provides information about the fonts available in the underlying window syste...
Definition: qfontdatabase.h:66
void setStrikeOut(bool)
If enable is true, sets strikeout on; otherwise sets strikeout off.
Definition: qfont.cpp:1378
const CGFloat ButtonTopMargin
bool qt_mac_is_macsheet(const QWidget *w)
Definition: qwidget_mac.mm:295
void finishOffWithCode:(NSInteger result)
const CGFloat ButtonMinWidth
The QFontDialog class provides a dialog widget for selecting a font.
Definition: qfontdialog.h:59
The QFontInfo class provides general information about fonts.
Definition: qfontinfo.h:54
QFontEngine * engineForScript(int script) const
Definition: qfont.cpp:294
void macStartInterceptNSPanelCtor()
static void setFont(void *delegate, const QFont &font)
const CGFloat ButtonMinHeight
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
#define QT_MANGLE_NAMESPACE(name)
Definition: qglobal.h:106
int key
bool singleShot
This static function calls a slot after a given time interval.
Definition: qtimer.h:59
QExplicitlySharedDataPointer< QFontPrivate > d
Definition: qfont.h:343
static const QMetaObjectPrivate * priv(const uint *data)
static QFont qfontForCocoaFont(NSFont *cocoaFont, const QFont &resolveFont)
static bool native_modal_dialog_active
int weight() const
Returns the weight of the font which is one of the enumerated values from QFont::Weight.
Definition: qfont.cpp:1248
void setQtFont:(const QFont &newFont)
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
Definition: qglobal.h:88
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
Definition: qglobal.h:1646
int qstrcmp(const QByteArray &str1, const char *str2)
Definition: qbytearray.cpp:336
QFontDialogPrivate * mPriv
bool strikeOut() const
Returns true if strikeout has been set; otherwise returns false.
Definition: qfont.cpp:1367
const int StyleMask
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static float pointSize(const QFontDef &fd, int dpi)
Definition: qfont_win.cpp:90
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
void macStopInterceptNSPanelCtor()
#define text
Definition: qobjectdefs.h:80
NSButton * macCreateButton(const char *text, NSView *superview)
float CGFloat