1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/fontdlgosx.cpp
3 // Purpose: wxFontDialog class.
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 // ===========================================================================
16 // ===========================================================================
18 // ---------------------------------------------------------------------------
20 // ---------------------------------------------------------------------------
22 #include "wx/fontdlg.h"
27 #include "wx/cmndata.h"
30 #include "wx/fontutil.h"
32 // ============================================================================
34 // ============================================================================
37 #include "wx/cocoa/autorelease.h"
38 #include "wx/cocoa/string.h"
40 #if wxMAC_USE_EXPERIMENTAL_FONTDIALOG
42 #import <Foundation/Foundation.h>
43 #import <AppKit/AppKit.h>
45 #include "wx/mac/uma.h"
47 @interface wxMacFontPanelAccView : NSView
51 NSButton* m_cancelButton ;
52 NSButton* m_okButton ;
55 - (IBAction)cancelPressed:(id)sender;
56 - (IBAction)okPressed:(id)sender;
59 - (BOOL)shouldCloseCarbon;
60 - (NSButton*)okButton;
63 @implementation wxMacFontPanelAccView : NSView
64 - (id)initWithFrame:(NSRect)rectBox
66 [super initWithFrame:rectBox];
68 wxMacCFStringHolder cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
69 wxMacCFStringHolder cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
71 NSRect rectCancel = NSMakeRect( 10.0 , 10.0 , 82 , 24 );
72 NSRect rectOK = NSMakeRect( 100.0 , 10.0 , 82 , 24 );
74 NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
75 [cancelButton setTitle:(NSString*)cfCancelString.Detach()];
76 [cancelButton setBezelStyle:NSRoundedBezelStyle];
77 [cancelButton setButtonType:NSMomentaryPushInButton];
78 [cancelButton setAction:@selector(cancelPressed:)];
79 [cancelButton setTarget:self];
80 m_cancelButton = cancelButton ;
82 NSButton* okButton = [[NSButton alloc] initWithFrame:rectOK];
83 [okButton setTitle:(NSString*)cfOkString.Detach()];
84 [okButton setBezelStyle:NSRoundedBezelStyle];
85 [okButton setButtonType:NSMomentaryPushInButton];
86 [okButton setAction:@selector(okPressed:)];
87 [okButton setTarget:self];
88 // doesn't help either, the button is not highlighted after a color dialog has been used
89 // [okButton setKeyEquivalent:@"\r"];
90 m_okButton = okButton ;
93 [self addSubview:cancelButton];
94 [self addSubview:okButton];
106 - (IBAction)cancelPressed:(id)sender
108 m_shouldClose = YES ;
112 - (IBAction)okPressed:(id)sender
115 m_shouldClose = YES ;
124 -(BOOL)shouldCloseCarbon
126 return m_shouldClose ;
136 extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
138 int RunMixedFontDialog(wxFontDialog* dialog)
140 int retval = wxID_CANCEL ;
142 bool cocoaLoaded = NSApplicationLoad();
143 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
145 wxAutoNSAutoreleasePool pool;
147 // setting up the ok/cancel buttons
148 NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
150 // adjust modality for carbon environment
151 WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
152 SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
153 SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
155 [fontPanel setFloatingPanel:NO] ;
156 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:NO] ;
158 wxMacFontPanelAccView* accessoryView = (wxMacFontPanelAccView*) [fontPanel accessoryView] ;
159 if ( accessoryView == nil)
161 NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
162 accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
163 [fontPanel setAccessoryView:accessoryView];
165 [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
168 [accessoryView resetFlags];
170 NSModalSession session = [NSApp beginModalSessionForWindow:fontPanel];
172 [NSApp runModalSession:session];
174 [NSApp endModalSession:session];
176 // if we don't reenable it, FPShowHideFontPanel does not work
177 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:YES] ;
178 if( FPIsFontPanelVisible())
179 FPShowHideFontPanel() ;
181 if ( [accessoryView closedWithOk])
192 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
194 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
198 #import <AppKit/NSFont.h>
199 #import <AppKit/NSFontManager.h>
200 #import <AppKit/NSFontPanel.h>
201 #import <AppKit/NSColor.h>
202 #import <AppKit/NSColorPanel.h>
204 // ---------------------------------------------------------------------------
205 // wxWCDelegate - Window Closed delegate
206 // ---------------------------------------------------------------------------
208 @interface wxWCDelegate : NSObject
215 - (BOOL)windowShouldClose:(id)sender;
217 @end // interface wxNSFontPanelDelegate : NSObject
219 @implementation wxWCDelegate : NSObject
229 - (BOOL)windowShouldClose:(id)sender
243 @end // wxNSFontPanelDelegate
245 // ---------------------------------------------------------------------------
246 // wxWCODelegate - Window Closed or Open delegate
247 // ---------------------------------------------------------------------------
249 @interface wxWCODelegate : NSObject
257 - (BOOL)windowShouldClose:(id)sender;
258 - (void)windowDidUpdate:(NSNotification *)aNotification;
261 @end // interface wxNSFontPanelDelegate : NSObject
263 @implementation wxWCODelegate : NSObject
274 - (BOOL)windowShouldClose:(id)sender
284 - (void)windowDidUpdate:(NSNotification *)aNotification
306 @end // wxNSFontPanelDelegate
308 // ---------------------------------------------------------------------------
310 // ---------------------------------------------------------------------------
312 wxFontDialog::wxFontDialog()
316 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
318 Create(parent, data);
321 wxFontDialog::~wxFontDialog()
325 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
330 // This is the key call - this initializes
331 // events and window stuff for cocoa for carbon
334 // This is also the only call here that is
335 // 10.2+ specific (the rest is OSX only),
336 // which, ironically, the carbon font
339 bool bOK = NSApplicationLoad();
341 //autorelease pool - req'd for carbon
342 NSAutoreleasePool *thePool;
343 thePool = [[NSAutoreleasePool alloc] init];
345 //Get the initial wx font
346 wxFont& thewxfont = m_fontData.m_initialFont;
348 //if the font is valid set the default (selected) font of the
349 //NSFontDialog to that font
352 NSFontTraitMask theMask = 0;
354 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
355 theMask |= NSItalicFontMask;
357 if(thewxfont.IsFixedWidth())
358 theMask |= NSFixedPitchFontMask;
360 NSFont* theDefaultFont =
361 [[NSFontManager sharedFontManager] fontWithFamily:
362 wxNSStringWithWxString(thewxfont.GetFaceName())
364 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
365 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
366 size: (float)(thewxfont.GetPointSize())
369 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
371 //Apple docs say to call NSFontManager::setSelectedFont
372 //However, 10.3 doesn't seem to create the font panel
373 //is this is done, so create it ourselves
374 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
378 if(m_fontData.m_fontColour.Ok())
379 [[NSColorPanel sharedColorPanel] setColor:
380 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
381 green:m_fontData.m_fontColour.Green() / 255.0
382 blue:m_fontData.m_fontColour.Blue() / 255.0
386 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
388 //We're done - free up the pool
394 int wxFontDialog::ShowModal()
396 //Start the pool. Required for carbon interaction
397 //(For those curious, the only thing that happens
398 //if you don't do this is a bunch of error
399 //messages about leaks on the console,
400 //with no windows shown or anything).
401 NSAutoreleasePool *thePool;
402 thePool = [[NSAutoreleasePool alloc] init];
404 //Get the shared color and font panel
405 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
406 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
408 //Create and assign the delegates (cocoa event handlers) so
409 //we can tell if a window has closed/open or not
410 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
411 [theFontPanel setDelegate:theFPDelegate];
413 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
414 [theColorPanel setDelegate:theCPDelegate];
417 // Begin the modal loop for the font and color panels
419 // The idea is that we first make the font panel modal,
420 // but if the color panel is opened, unless we stop the
421 // modal loop the color panel opens behind the font panel
422 // with no input acceptable to it - which makes it useless.
424 // So we set up delegates for both the color and font panels,
425 // and the if the font panel opens the color panel, we
426 // stop the modal loop, and start a separate modal loop for
427 // the color panel until the color panel closes, switching
428 // back to the font panel modal loop once it does close.
433 // Start the font panel modal loop
435 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
438 [NSApp runModalSession:session];
440 //If the font panel is closed or the font panel
441 //opened the color panel, break
442 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
445 [NSApp endModalSession:session];
447 //is the color panel open?
448 if ([theCPDelegate isOpen])
451 // Start the color panel modal loop
453 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
456 [NSApp runModalSession:session];
458 //If the color panel is closed, return the font panel modal loop
459 if ([theCPDelegate isClosed])
462 [NSApp endModalSession:session];
464 //If the font panel is still alive (I.E. we broke
465 //out of its modal loop because the color panel was
466 //opened) return the font panel modal loop
467 }while([theFPDelegate isClosed] == NO);
469 //free up the memory for the delegates - we don't need them anymore
470 [theFPDelegate release];
471 [theCPDelegate release];
473 //Get the font the user selected
474 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
476 //Get more information about the user's chosen font
477 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
478 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
479 int theFontSize = (int) [theFont pointSize];
481 //Set the wx font to the appropriate data
482 if(theTraits & NSFixedPitchFontMask)
483 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
485 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
486 m_fontData.m_chosenFont.SetPointSize(theFontSize);
487 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
488 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
489 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
491 //Get the shared color panel along with the chosen color and set the chosen color
492 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
494 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
495 (unsigned char) ([theColor greenComponent] * 255.0),
496 (unsigned char) ([theColor blueComponent] * 255.0));
498 //Friendly debug stuff
500 wxPrintf(wxT("---Font Panel---\n--NS--\nSize:%f\nWeight:%i\nTraits:%i\n--WX--\nFaceName:%s\nPointSize:%i\nStyle:%i\nWeight:%i\nColor:%i,%i,%i\n---END Font Panel---\n"),
506 m_fontData.m_chosenFont.GetFaceName().c_str(),
507 m_fontData.m_chosenFont.GetPointSize(),
508 m_fontData.m_chosenFont.GetStyle(),
509 m_fontData.m_chosenFont.GetWeight(),
510 m_fontData.m_fontColour.Red(),
511 m_fontData.m_fontColour.Green(),
512 m_fontData.m_fontColour.Blue() );
515 //Release the pool, we're done :)
518 //Return ID_OK - there are no "apply" buttons or the like
519 //on either the font or color panel
523 //old api stuff (REMOVE ME)
524 bool wxFontDialog::IsShown() const