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, const wxFontData& data)
356 Create(parent, data);
359 wxFontDialog::~wxFontDialog()
363 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
367 //autorelease pool - req'd for carbon
368 NSAutoreleasePool *thePool;
369 thePool = [[NSAutoreleasePool alloc] init];
371 //Get the initial wx font
372 wxFont& thewxfont = m_fontData.m_initialFont;
374 //if the font is valid set the default (selected) font of the
375 //NSFontDialog to that font
376 if (thewxfont.IsOk())
378 NSFontTraitMask theMask = 0;
380 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
381 theMask |= NSItalicFontMask;
383 if(thewxfont.IsFixedWidth())
384 theMask |= NSFixedPitchFontMask;
386 NSFont* theDefaultFont =
387 [[NSFontManager sharedFontManager] fontWithFamily:
388 wxNSStringWithWxString(thewxfont.GetFaceName())
390 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
391 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
392 size: (float)(thewxfont.GetPointSize())
395 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
397 //Apple docs say to call NSFontManager::setSelectedFont
398 //However, 10.3 doesn't seem to create the font panel
399 //is this is done, so create it ourselves
400 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
404 if(m_fontData.m_fontColour.IsOk())
405 [[NSColorPanel sharedColorPanel] setColor:
406 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
407 green:m_fontData.m_fontColour.Green() / 255.0
408 blue:m_fontData.m_fontColour.Blue() / 255.0
412 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
414 //We're done - free up the pool
420 int wxFontDialog::ShowModal()
422 //Start the pool. Required for carbon interaction
423 //(For those curious, the only thing that happens
424 //if you don't do this is a bunch of error
425 //messages about leaks on the console,
426 //with no windows shown or anything).
427 NSAutoreleasePool *thePool;
428 thePool = [[NSAutoreleasePool alloc] init];
430 //Get the shared color and font panel
431 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
432 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
434 //Create and assign the delegates (cocoa event handlers) so
435 //we can tell if a window has closed/open or not
436 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
437 [theFontPanel setDelegate:theFPDelegate];
439 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
440 [theColorPanel setDelegate:theCPDelegate];
443 // Begin the modal loop for the font and color panels
445 // The idea is that we first make the font panel modal,
446 // but if the color panel is opened, unless we stop the
447 // modal loop the color panel opens behind the font panel
448 // with no input acceptable to it - which makes it useless.
450 // So we set up delegates for both the color and font panels,
451 // and the if the font panel opens the color panel, we
452 // stop the modal loop, and start a separate modal loop for
453 // the color panel until the color panel closes, switching
454 // back to the font panel modal loop once it does close.
456 wxDialog::OSXBeginModalDialog();
460 // Start the font panel modal loop
462 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
465 [NSApp runModalSession:session];
467 //If the font panel is closed or the font panel
468 //opened the color panel, break
469 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
472 [NSApp endModalSession:session];
474 //is the color panel open?
475 if ([theCPDelegate isOpen])
478 // Start the color panel modal loop
480 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
483 [NSApp runModalSession:session];
485 //If the color panel is closed, return the font panel modal loop
486 if ([theCPDelegate isClosed])
489 [NSApp endModalSession:session];
491 //If the font panel is still alive (I.E. we broke
492 //out of its modal loop because the color panel was
493 //opened) return the font panel modal loop
494 }while([theFPDelegate isClosed] == NO);
495 wxDialog::OSXEndModalDialog();
497 //free up the memory for the delegates - we don't need them anymore
498 [theFPDelegate release];
499 [theCPDelegate release];
501 //Get the font the user selected
502 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
504 //Get more information about the user's chosen font
505 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
506 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
507 int theFontSize = (int) [theFont pointSize];
509 //Set the wx font to the appropriate data
510 if(theTraits & NSFixedPitchFontMask)
511 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
513 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
514 m_fontData.m_chosenFont.SetPointSize(theFontSize);
515 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
516 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
517 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
519 //Get the shared color panel along with the chosen color and set the chosen color
520 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
522 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
523 (unsigned char) ([theColor greenComponent] * 255.0),
524 (unsigned char) ([theColor blueComponent] * 255.0));
526 //Friendly debug stuff
528 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"),
534 m_fontData.m_chosenFont.GetFaceName().c_str(),
535 m_fontData.m_chosenFont.GetPointSize(),
536 m_fontData.m_chosenFont.GetStyle(),
537 m_fontData.m_chosenFont.GetWeight(),
538 m_fontData.m_fontColour.Red(),
539 m_fontData.m_fontColour.Green(),
540 m_fontData.m_fontColour.Blue() );
543 //Release the pool, we're done :)
546 //Return ID_OK - there are no "apply" buttons or the like
547 //on either the font or color panel
551 //old api stuff (REMOVE ME)
552 bool wxFontDialog::IsShown() const