43 #if !defined(QT_NO_FONTDIALOG) && defined(Q_WS_MAC) 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> 54 #include <private/qfontengine_coretext_p.h> 55 #import <AppKit/AppKit.h> 56 #import <Foundation/Foundation.h> 84 const int StyleMask = NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
89 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5 91 @protocol NSWindowDelegate <NSObject>
92 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
97 @interface QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) : NSObject <NSWindowDelegate> {
110 - (id)initWithFontPanel:(NSFontPanel *)panel
111 stolenContentView:(NSView *)stolenContentView
112 okButton:(NSButton *)okButton
113 cancelButton:(NSButton *)cancelButton
116 extraHeight:(
CGFloat)extraHeight;
117 - (void)showModelessPanel;
118 - (void)showWindowModalSheet:(
QWidget *)docWidget;
119 - (void)runApplicationModalPanel;
121 - (void)changeFont:(
id)sender;
122 - (void)changeAttributes:(
id)sender;
123 - (BOOL)windowShouldClose:(
id)window;
124 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
126 - (void)relayoutToContentSize:(NSSize)frameSize;
128 - (void)onCancelClicked;
129 - (NSFontPanel *)fontPanel;
130 - (NSWindow *)actualPanel;
131 - (NSSize)dialogExtraSize;
132 - (void)setQtFont:(const
QFont &)newFont;
134 - (void)finishOffWithCode:(NSInteger)result;
135 - (void)cleanUpAfterMyself;
136 - (void)setSubwindowStacking;
144 CTFontDescriptorRef font = CTFontCopyFontDescriptor((CTFontRef)cocoaFont);
158 @implementation QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate)
159 - (id)initWithFontPanel:(NSFontPanel *)panel
160 stolenContentView:(NSView *)stolenContentView
161 okButton:(NSButton *)okButton
162 cancelButton:(NSButton *)cancelButton
165 extraHeight:(
CGFloat)extraHeight
169 mStolenContentView = stolenContentView;
170 mOkButton = okButton;
171 mCancelButton = cancelButton;
173 mPanelHackedWithButtons = (okButton != 0);
174 mDialogExtraWidth = extraWidth;
175 mDialogExtraHeight = extraHeight;
179 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7 184 if (mPanelHackedWithButtons) {
187 [okButton setAction:@selector(onOkClicked)];
188 [okButton setTarget:self];
190 [cancelButton setAction:@selector(onCancelClicked)];
191 [cancelButton setTarget:self];
194 mQtFont =
new QFont();
198 - (void)setSubwindowStacking
200 #ifdef QT_MAC_USE_COCOA 205 if (parent->isWindow()) {
206 [qt_mac_window_for(parent)
220 - (void)showModelessPanel
224 [ourPanel makeKeyAndOrderFront:self];
227 - (void)runApplicationModalPanel
232 [ourPanel setReleasedWhenClosed:NO];
233 [NSApp runModalForWindow:ourPanel];
236 if (mReturnCode == NSOKButton)
237 mPriv->fontDialog()->accept();
239 mPriv->fontDialog()->reject();
247 - (void)showWindowModalSheet:(
QWidget *)docWidget
249 #ifdef QT_MAC_USE_COCOA 253 NSWindow *
window = [[NSWindow alloc] initWithWindowRef:hiwindowRef];
254 CFRetain(hiwindowRef);
259 [NSApp beginSheet:ourPanel
260 modalForWindow:window
265 #ifndef QT_MAC_USE_COCOA 266 CFRelease(hiwindowRef);
270 - (void)changeFont:(
id)sender
272 NSFont *dummyFont = [NSFont userFontOfSize:12.0];
273 [
self setQtFont:qfontForCocoaFont([sender convertFont:dummyFont], *mQtFont)];
275 mPriv->updateSampleFont(*mQtFont);
278 - (void)changeAttributes:(
id)sender
280 NSDictionary *dummyAttribs = [NSDictionary dictionary];
281 NSDictionary *attribs = [sender convertAttributes:dummyAttribs];
283 #ifdef QT_MAC_USE_COCOA 284 for (
id key in attribs) {
286 NSEnumerator *enumerator = [attribs keyEnumerator];
288 while((key = [enumerator nextObject])) {
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);
299 mPriv->updateSampleFont(*mQtFont);
302 - (BOOL)windowShouldClose:(
id)window
305 if (mPanelHackedWithButtons) {
313 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
315 if (mFontPanel ==
window) {
316 proposedFrameSize = [static_cast<id <NSWindowDelegate> >(mFontPanel) windowWillResize:mFontPanel toSize:proposedFrameSize];
326 [
mFontPanel setContentView:mStolenContentView];
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 };
334 [[window contentView] addSubview:mStolenContentView];
335 proposedFrameSize.width += extraSize.width;
336 proposedFrameSize.height += extraSize.height;
338 if (mPanelHackedWithButtons) {
339 NSRect frameRect = { { 0.0, 0.0 }, proposedFrameSize };
340 NSRect contentRect = [NSWindow contentRectForFrameRect:frameRect styleMask:[window styleMask]];
343 return proposedFrameSize;
351 - (void)relayoutToContentSize:(NSSize)frameSize
356 NSSize okSizeHint = [
mOkButton frame].size;
362 qMax(okSizeHint.width, cancelSizeHint.width)),
365 qMax(okSizeHint.height, cancelSizeHint.height));
372 { ButtonWidth, ButtonHeight } };
376 NSRect cancelRect = { { okRect.origin.x -
ButtonSpacing - ButtonWidth,
378 { ButtonWidth, ButtonHeight } };
382 NSRect stolenCVRect = { { X, Y },
393 NSFontManager *fontManager = [NSFontManager sharedFontManager];
394 [
self setQtFont:qfontForCocoaFont([fontManager convertFont:[fontManager selectedFont]],
399 - (void)onCancelClicked
405 - (NSFontPanel *)fontPanel
410 - (NSWindow *)actualPanel
415 - (NSSize)dialogExtraSize
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];
426 NSSize result = { mDialogExtraWidth, mDialogExtraHeight };
427 result.width -= ourPanelContentRect.size.width - sharedFontPanelContentRect.size.width;
428 result.height -= ourPanelContentRect.size.height - sharedFontPanelContentRect.size.height;
432 - (void)setQtFont:(const
QFont &)newFont
435 mQtFont =
new QFont(newFont);
443 - (void)finishOffWithCode:(NSInteger)code
445 #ifdef QT_MAC_USE_COCOA 448 if (parent->isWindow()) {
454 if(code == NSOKButton)
455 mPriv->sampleEdit->
setFont([
self qtFont]);
459 [NSApp stopModalWithCode:code];
461 if (code == NSOKButton)
462 mPriv->fontDialog()->accept();
464 mPriv->fontDialog()->reject();
468 - (void)cleanUpAfterMyself
470 if (mPanelHackedWithButtons) {
471 NSView *ourContentView = [
mFontPanel contentView];
475 [
mFontPanel setContentView:mStolenContentView];
479 [ourContentView release];
482 [[NSFontManager sharedFontManager] setDelegate:nil];
483 #ifdef QT_MAC_USE_COCOA 484 [[NSFontManager sharedFontManager] setTarget:nil];
495 NSWindow *ourPanel = [theDelegate actualPanel];
497 if ([theDelegate isAppModal])
499 [theDelegate cleanUpAfterMyself];
500 [theDelegate release];
502 sharedFontPanelAvailable =
true;
509 NSFontManager *mgr = [NSFontManager sharedFontManager];
510 const NSFont *nsFont = 0;
512 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5 519 NSFontTraitMask mask = 0;
521 mask |= NSItalicFontMask;
525 mask |= NSBoldFontMask;
528 NSFontManager *mgr = [NSFontManager sharedFontManager];
530 nsFont = [mgr fontWithFamily:qt_mac_QStringToNSString(fontInfo.family())
533 size:fontInfo.pointSize()];
536 [mgr setSelectedFont:const_cast<NSFont *>(nsFont) isMultiple:NO];
537 [static_cast<QT_MANGLE_NAMESPACE(QCocoaFontPanelDelegate) *>(delegate) setQtFont:font];
545 sharedFontPanelAvailable =
false;
547 bool sharedFontPanelExisted = [NSFontPanel sharedFontPanelExists];
548 NSFontPanel *sharedFontPanel = [NSFontPanel sharedFontPanel];
549 [sharedFontPanel setHidesOnDeactivate:false];
553 if (!sharedFontPanelExisted) {
554 [sharedFontPanel makeKeyAndOrderFront:sharedFontPanel];
555 [sharedFontPanel close];
558 NSPanel *ourPanel = 0;
559 NSView *stolenContentView = 0;
560 NSButton *okButton = 0;
561 NSButton *cancelButton = 0;
563 CGFloat dialogExtraWidth = 0.0;
564 CGFloat dialogExtraHeight = 0.0;
571 NSRect contentRect = [sharedFontPanel contentRectForFrameRect:[sharedFontPanel frame]];
572 contentRect.size.width += dialogExtraWidth;
573 contentRect.size.height += dialogExtraHeight;
576 ourPanel = [[NSPanel alloc] initWithContentRect:contentRect
578 backing:NSBackingStoreBuffered
580 [ourPanel setReleasedWhenClosed:YES];
581 stolenContentView = [sharedFontPanel contentView];
584 [stolenContentView retain];
585 [sharedFontPanel setContentView:0];
589 NSRect frameRect = { { 0.0, 0.0 }, { 0.0, 0.0 } };
590 NSView *ourContentView = [[NSView alloc] initWithFrame:frameRect];
591 [ourContentView addSubview:stolenContentView];
597 [ourPanel setContentView:ourContentView];
598 [ourPanel setDefaultButtonCell:[okButton cell]];
603 stolenContentView:stolenContentView
605 cancelButton:cancelButton
607 extraWidth:dialogExtraWidth
608 extraHeight:dialogExtraHeight];
610 [ourPanel setDelegate:del];
612 [[NSFontManager sharedFontManager] setDelegate:del];
613 #ifdef QT_MAC_USE_COCOA 614 [[NSFontManager sharedFontManager] setTarget:del];
616 setFont(del, q_func()->currentFont());
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];
628 [del setSubwindowStacking];
629 NSString *title =
@"Select font";
630 [ourPanel setTitle:title];
643 if (nativeDialogInUse) {
655 createNSFontPanelDelegate();
657 [del runApplicationModalPanel];
662 if (!sharedFontPanelAvailable)
667 createNSFontPanelDelegate();
672 [del showModelessPanel];
683 closeCocoaFontPanel();
695 return visible ? showCocoaFontPanel() : hideCocoaFontPanel();
const struct __CFString * CFStringRef
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)
#define QT_END_NAMESPACE
This macro expands to.
const CGFloat DialogTopMargin
const CGFloat ButtonBottomMargin
void relayoutToContentSize:(NSSize frameSize)
CGFloat mDialogExtraHeight
bool underline() const
Returns true if underline has been set; otherwise returns false.
struct OpaqueWindowPtr * WindowRef
void setUnderline(bool)
If enable is true, sets underline on; otherwise sets underline off.
static QAbstractEventDispatcher * instance(QThread *thread=0)
Returns a pointer to the event dispatcher object for the specified thread.
Style style() const
Returns the style of the font.
bool showCocoaFontPanel()
static QString toQString(CFStringRef cfstr)
The QString class provides a Unicode character string.
const CGFloat ButtonSpacing
void _q_macRunNativeAppModalPanel()
void closeCocoaFontPanel()
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
bool setVisible_sys(bool visible)
OSWindowRef qt_mac_window_for(const QWidget *)
void createNSFontPanelDelegate()
const CGFloat DialogSideMargin
virtual const char * name() const =0
#define QT_BEGIN_NAMESPACE
This macro expands to.
The QFontDatabase class provides information about the fonts available in the underlying window syste...
CGFloat mDialogExtraWidth
void setStrikeOut(bool)
If enable is true, sets strikeout on; otherwise sets strikeout off.
const CGFloat ButtonTopMargin
bool qt_mac_is_macsheet(const QWidget *w)
void finishOffWithCode:(NSInteger result)
const CGFloat ButtonMinWidth
The QFontDialog class provides a dialog widget for selecting a font.
The QFontInfo class provides general information about fonts.
QFontEngine * engineForScript(int script) const
void macStartInterceptNSPanelCtor()
static void setFont(void *delegate, const QFont &font)
NSView * mStolenContentView
const CGFloat ButtonMinHeight
The QFont class specifies a font used for drawing text.
#define QT_MANGLE_NAMESPACE(name)
BOOL mPanelHackedWithButtons
bool singleShot
This static function calls a slot after a given time interval.
QExplicitlySharedDataPointer< QFontPrivate > d
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.
void setQtFont:(const QFont &newFont)
#define QT_USE_NAMESPACE
This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined and nothing otherwise.
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
int qstrcmp(const QByteArray &str1, const char *str2)
QFontDialogPrivate * mPriv
bool hideCocoaFontPanel()
bool strikeOut() const
Returns true if strikeout has been set; otherwise returns false.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
void mac_nativeDialogModalHelp()
static float pointSize(const QFontDef &fd, int dpi)
Q_DECL_CONSTEXPR int qRound(qreal d)
void macStopInterceptNSPanelCtor()
NSButton * macCreateButton(const char *text, NSView *superview)