1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/fontdlgosx.mm
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 wxOSX_USE_EXPERIMENTAL_FONTDIALOG
42 #import <Foundation/Foundation.h>
43 #import <AppKit/AppKit.h>
45 #include "wx/osx/private.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 wxCFStringRef cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
69 wxCFStringRef cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
71 NSRect rectCancel = NSMakeRect( (CGFloat) 10.0 , (CGFloat)10.0 , (CGFloat)82 , (CGFloat)24 );
72 NSRect rectOK = NSMakeRect( (CGFloat)100.0 , (CGFloat)10.0 , (CGFloat)82 , (CGFloat)24 );
74 NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
75 [cancelButton setTitle:(NSString*)wxCFRetain((CFStringRef)cfCancelString)];
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*)wxCFRetain((CFStringRef)cfOkString)];
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
109 m_shouldClose = YES ;
113 - (IBAction)okPressed:(id)sender
117 m_shouldClose = YES ;
126 -(BOOL)shouldCloseCarbon
128 return m_shouldClose ;
138 extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
140 int RunMixedFontDialog(wxFontDialog* dialog)
143 wxFontData& fontdata= ((wxFontDialog*)dialog)->GetFontData() ;
147 int retval = wxID_CANCEL ;
149 wxAutoNSAutoreleasePool pool;
151 // setting up the ok/cancel buttons
152 NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
154 // adjust modality for carbon environment
156 WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
157 SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
158 SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
161 [fontPanel setFloatingPanel:NO] ;
162 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:NO] ;
164 wxMacFontPanelAccView* accessoryView = (wxMacFontPanelAccView*) [fontPanel accessoryView] ;
165 if ( accessoryView == nil)
167 NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
168 accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
169 [fontPanel setAccessoryView:accessoryView];
170 [accessoryView release];
172 [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
175 [accessoryView resetFlags];
177 wxFont font = *wxNORMAL_FONT ;
178 if ( fontdata.m_initialFont.IsOk() )
180 font = fontdata.m_initialFont ;
183 [[NSFontPanel sharedFontPanel] setPanelFont: font.OSXGetNSFont() isMultiple:NO];
185 if(fontdata.m_fontColour.IsOk())
186 [[NSColorPanel sharedColorPanel] setColor:
187 [NSColor colorWithCalibratedRed:fontdata.m_fontColour.Red() / 255.0
188 green:fontdata.m_fontColour.Green() / 255.0
189 blue:fontdata.m_fontColour.Blue() / 255.0
193 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
196 [NSApp runModalForWindow:fontPanel];
198 // if we don't reenable it, FPShowHideFontPanel does not work
199 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:YES] ;
201 if( FPIsFontPanelVisible())
202 FPShowHideFontPanel() ;
207 if ( [accessoryView closedWithOk])
210 NSFont* theFont = [fontPanel panelConvertFont:[NSFont userFontOfSize:0]];
212 fontdata.m_chosenFont = wxFont( theFont );
214 //Get the shared color panel along with the chosen color and set the chosen color
215 NSColor* theColor = [[[NSColorPanel sharedColorPanel] color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
217 fontdata.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
218 (unsigned char) ([theColor greenComponent] * 255.0),
219 (unsigned char) ([theColor blueComponent] * 255.0));
223 [fontPanel setAccessoryView:nil];
230 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
232 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
236 #import <AppKit/NSFont.h>
237 #import <AppKit/NSFontManager.h>
238 #import <AppKit/NSFontPanel.h>
239 #import <AppKit/NSColor.h>
240 #import <AppKit/NSColorPanel.h>
242 // ---------------------------------------------------------------------------
243 // wxWCDelegate - Window Closed delegate
244 // ---------------------------------------------------------------------------
246 @interface wxWCDelegate : NSObject
253 - (BOOL)windowShouldClose:(id)sender;
255 @end // interface wxNSFontPanelDelegate : NSObject
257 @implementation wxWCDelegate : NSObject
267 - (BOOL)windowShouldClose:(id)sender
281 @end // wxNSFontPanelDelegate
283 // ---------------------------------------------------------------------------
284 // wxWCODelegate - Window Closed or Open delegate
285 // ---------------------------------------------------------------------------
287 @interface wxWCODelegate : NSObject
295 - (BOOL)windowShouldClose:(id)sender;
296 - (void)windowDidUpdate:(NSNotification *)aNotification;
299 @end // interface wxNSFontPanelDelegate : NSObject
301 @implementation wxWCODelegate : NSObject
312 - (BOOL)windowShouldClose:(id)sender
322 - (void)windowDidUpdate:(NSNotification *)aNotification
344 @end // wxNSFontPanelDelegate
346 // ---------------------------------------------------------------------------
348 // ---------------------------------------------------------------------------
350 wxFontDialog::wxFontDialog()
354 wxFontDialog::wxFontDialog(wxWindow *parent)
359 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
361 Create(parent, data);
364 wxFontDialog::~wxFontDialog()
368 bool wxFontDialog::Create(wxWindow *parent)
370 return Create(parent);
373 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
377 return Create(parent);
380 bool wxFontDialog::Create(wxWindow *parent)
382 //autorelease pool - req'd for carbon
383 NSAutoreleasePool *thePool;
384 thePool = [[NSAutoreleasePool alloc] init];
386 //Get the initial wx font
387 wxFont& thewxfont = m_fontData.m_initialFont;
389 //if the font is valid set the default (selected) font of the
390 //NSFontDialog to that font
391 if (thewxfont.IsOk())
393 NSFontTraitMask theMask = 0;
395 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
396 theMask |= NSItalicFontMask;
398 if(thewxfont.IsFixedWidth())
399 theMask |= NSFixedPitchFontMask;
401 NSFont* theDefaultFont =
402 [[NSFontManager sharedFontManager] fontWithFamily:
403 wxNSStringWithWxString(thewxfont.GetFaceName())
405 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
406 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
407 size: (float)(thewxfont.GetPointSize())
410 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
412 //Apple docs say to call NSFontManager::setSelectedFont
413 //However, 10.3 doesn't seem to create the font panel
414 //is this is done, so create it ourselves
415 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
419 if(m_fontData.m_fontColour.IsOk())
420 [[NSColorPanel sharedColorPanel] setColor:
421 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
422 green:m_fontData.m_fontColour.Green() / 255.0
423 blue:m_fontData.m_fontColour.Blue() / 255.0
427 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
429 //We're done - free up the pool
435 int wxFontDialog::ShowModal()
437 //Start the pool. Required for carbon interaction
438 //(For those curious, the only thing that happens
439 //if you don't do this is a bunch of error
440 //messages about leaks on the console,
441 //with no windows shown or anything).
442 NSAutoreleasePool *thePool;
443 thePool = [[NSAutoreleasePool alloc] init];
445 //Get the shared color and font panel
446 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
447 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
449 //Create and assign the delegates (cocoa event handlers) so
450 //we can tell if a window has closed/open or not
451 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
452 [theFontPanel setDelegate:theFPDelegate];
454 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
455 [theColorPanel setDelegate:theCPDelegate];
458 // Begin the modal loop for the font and color panels
460 // The idea is that we first make the font panel modal,
461 // but if the color panel is opened, unless we stop the
462 // modal loop the color panel opens behind the font panel
463 // with no input acceptable to it - which makes it useless.
465 // So we set up delegates for both the color and font panels,
466 // and the if the font panel opens the color panel, we
467 // stop the modal loop, and start a separate modal loop for
468 // the color panel until the color panel closes, switching
469 // back to the font panel modal loop once it does close.
471 wxDialog::OSXBeginModalDialog();
475 // Start the font panel modal loop
477 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
480 [NSApp runModalSession:session];
482 //If the font panel is closed or the font panel
483 //opened the color panel, break
484 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
487 [NSApp endModalSession:session];
489 //is the color panel open?
490 if ([theCPDelegate isOpen])
493 // Start the color panel modal loop
495 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
498 [NSApp runModalSession:session];
500 //If the color panel is closed, return the font panel modal loop
501 if ([theCPDelegate isClosed])
504 [NSApp endModalSession:session];
506 //If the font panel is still alive (I.E. we broke
507 //out of its modal loop because the color panel was
508 //opened) return the font panel modal loop
509 }while([theFPDelegate isClosed] == NO);
510 wxDialog::OSXEndModalDialog();
512 //free up the memory for the delegates - we don't need them anymore
513 [theFPDelegate release];
514 [theCPDelegate release];
516 //Get the font the user selected
517 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
519 //Get more information about the user's chosen font
520 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
521 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
522 int theFontSize = (int) [theFont pointSize];
524 //Set the wx font to the appropriate data
525 if(theTraits & NSFixedPitchFontMask)
526 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
528 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
529 m_fontData.m_chosenFont.SetPointSize(theFontSize);
530 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
531 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
532 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
534 //Get the shared color panel along with the chosen color and set the chosen color
535 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
537 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
538 (unsigned char) ([theColor greenComponent] * 255.0),
539 (unsigned char) ([theColor blueComponent] * 255.0));
541 //Friendly debug stuff
543 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"),
549 m_fontData.m_chosenFont.GetFaceName().c_str(),
550 m_fontData.m_chosenFont.GetPointSize(),
551 m_fontData.m_chosenFont.GetStyle(),
552 m_fontData.m_chosenFont.GetWeight(),
553 m_fontData.m_fontColour.Red(),
554 m_fontData.m_fontColour.Green(),
555 m_fontData.m_fontColour.Blue() );
558 //Release the pool, we're done :)
561 //Return ID_OK - there are no "apply" buttons or the like
562 //on either the font or color panel
566 //old api stuff (REMOVE ME)
567 bool wxFontDialog::IsShown() const