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"
31 #include "wx/testing.h"
33 // ============================================================================
35 // ============================================================================
38 #include "wx/cocoa/autorelease.h"
39 #include "wx/cocoa/string.h"
41 #if wxOSX_USE_EXPERIMENTAL_FONTDIALOG
43 #import <Foundation/Foundation.h>
44 #import <AppKit/AppKit.h>
46 #include "wx/osx/private.h"
48 @interface wxMacFontPanelAccView : NSView
52 NSButton* m_cancelButton ;
53 NSButton* m_okButton ;
56 - (IBAction)cancelPressed:(id)sender;
57 - (IBAction)okPressed:(id)sender;
60 - (BOOL)shouldCloseCarbon;
61 - (NSButton*)okButton;
64 @implementation wxMacFontPanelAccView : NSView
65 - (id)initWithFrame:(NSRect)rectBox
67 [super initWithFrame:rectBox];
69 wxCFStringRef cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
70 wxCFStringRef cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
72 NSRect rectCancel = NSMakeRect( (CGFloat) 10.0 , (CGFloat)10.0 , (CGFloat)82 , (CGFloat)24 );
73 NSRect rectOK = NSMakeRect( (CGFloat)100.0 , (CGFloat)10.0 , (CGFloat)82 , (CGFloat)24 );
75 NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
76 [cancelButton setTitle:(NSString*)wxCFRetain((CFStringRef)cfCancelString)];
77 [cancelButton setBezelStyle:NSRoundedBezelStyle];
78 [cancelButton setButtonType:NSMomentaryPushInButton];
79 [cancelButton setAction:@selector(cancelPressed:)];
80 [cancelButton setTarget:self];
81 m_cancelButton = cancelButton ;
83 NSButton* okButton = [[NSButton alloc] initWithFrame:rectOK];
84 [okButton setTitle:(NSString*)wxCFRetain((CFStringRef)cfOkString)];
85 [okButton setBezelStyle:NSRoundedBezelStyle];
86 [okButton setButtonType:NSMomentaryPushInButton];
87 [okButton setAction:@selector(okPressed:)];
88 [okButton setTarget:self];
89 // doesn't help either, the button is not highlighted after a color dialog has been used
90 // [okButton setKeyEquivalent:@"\r"];
91 m_okButton = okButton ;
94 [self addSubview:cancelButton];
95 [self addSubview:okButton];
107 - (IBAction)cancelPressed:(id)sender
110 m_shouldClose = YES ;
114 - (IBAction)okPressed:(id)sender
118 m_shouldClose = YES ;
127 -(BOOL)shouldCloseCarbon
129 return m_shouldClose ;
139 extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
141 int RunMixedFontDialog(wxFontDialog* dialog)
144 wxFontData& fontdata= ((wxFontDialog*)dialog)->GetFontData() ;
148 int retval = wxID_CANCEL ;
150 wxAutoNSAutoreleasePool pool;
152 // setting up the ok/cancel buttons
153 NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
155 // adjust modality for carbon environment
157 WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
158 SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
159 SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
162 [fontPanel setFloatingPanel:NO] ;
163 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:NO] ;
165 wxMacFontPanelAccView* accessoryView = (wxMacFontPanelAccView*) [fontPanel accessoryView] ;
166 if ( accessoryView == nil)
168 NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
169 accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
170 [fontPanel setAccessoryView:accessoryView];
171 [accessoryView release];
173 [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
176 [accessoryView resetFlags];
178 wxFont font = *wxNORMAL_FONT ;
179 if ( fontdata.m_initialFont.IsOk() )
181 font = fontdata.m_initialFont ;
184 [[NSFontPanel sharedFontPanel] setPanelFont: font.OSXGetNSFont() isMultiple:NO];
186 if(fontdata.m_fontColour.IsOk())
187 [[NSColorPanel sharedColorPanel] setColor:
188 [NSColor colorWithCalibratedRed:fontdata.m_fontColour.Red() / 255.0
189 green:fontdata.m_fontColour.Green() / 255.0
190 blue:fontdata.m_fontColour.Blue() / 255.0
194 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
197 [NSApp runModalForWindow:fontPanel];
199 // if we don't reenable it, FPShowHideFontPanel does not work
200 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:YES] ;
202 if( FPIsFontPanelVisible())
203 FPShowHideFontPanel() ;
208 if ( [accessoryView closedWithOk])
211 NSFont* theFont = [fontPanel panelConvertFont:[NSFont userFontOfSize:0]];
213 fontdata.m_chosenFont = wxFont( theFont );
215 //Get the shared color panel along with the chosen color and set the chosen color
216 NSColor* theColor = [[[NSColorPanel sharedColorPanel] color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
218 fontdata.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
219 (unsigned char) ([theColor greenComponent] * 255.0),
220 (unsigned char) ([theColor blueComponent] * 255.0));
224 [fontPanel setAccessoryView:nil];
231 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
233 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
237 #import <AppKit/NSFont.h>
238 #import <AppKit/NSFontManager.h>
239 #import <AppKit/NSFontPanel.h>
240 #import <AppKit/NSColor.h>
241 #import <AppKit/NSColorPanel.h>
243 // ---------------------------------------------------------------------------
244 // wxWCDelegate - Window Closed delegate
245 // ---------------------------------------------------------------------------
247 @interface wxWCDelegate : NSObject
254 - (BOOL)windowShouldClose:(id)sender;
256 @end // interface wxNSFontPanelDelegate : NSObject
258 @implementation wxWCDelegate : NSObject
268 - (BOOL)windowShouldClose:(id)sender
282 @end // wxNSFontPanelDelegate
284 // ---------------------------------------------------------------------------
285 // wxWCODelegate - Window Closed or Open delegate
286 // ---------------------------------------------------------------------------
288 @interface wxWCODelegate : NSObject
296 - (BOOL)windowShouldClose:(id)sender;
297 - (void)windowDidUpdate:(NSNotification *)aNotification;
300 @end // interface wxNSFontPanelDelegate : NSObject
302 @implementation wxWCODelegate : NSObject
313 - (BOOL)windowShouldClose:(id)sender
323 - (void)windowDidUpdate:(NSNotification *)aNotification
345 @end // wxNSFontPanelDelegate
347 // ---------------------------------------------------------------------------
349 // ---------------------------------------------------------------------------
351 wxFontDialog::wxFontDialog()
355 wxFontDialog::wxFontDialog(wxWindow *parent)
360 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
362 Create(parent, data);
365 wxFontDialog::~wxFontDialog()
369 bool wxFontDialog::Create(wxWindow *parent)
371 return Create(parent);
374 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
378 return Create(parent);
381 bool wxFontDialog::Create(wxWindow *parent)
383 //autorelease pool - req'd for carbon
384 NSAutoreleasePool *thePool;
385 thePool = [[NSAutoreleasePool alloc] init];
387 //Get the initial wx font
388 wxFont& thewxfont = m_fontData.m_initialFont;
390 //if the font is valid set the default (selected) font of the
391 //NSFontDialog to that font
392 if (thewxfont.IsOk())
394 NSFontTraitMask theMask = 0;
396 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
397 theMask |= NSItalicFontMask;
399 if(thewxfont.IsFixedWidth())
400 theMask |= NSFixedPitchFontMask;
402 NSFont* theDefaultFont =
403 [[NSFontManager sharedFontManager] fontWithFamily:
404 wxNSStringWithWxString(thewxfont.GetFaceName())
406 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
407 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
408 size: (float)(thewxfont.GetPointSize())
411 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
413 //Apple docs say to call NSFontManager::setSelectedFont
414 //However, 10.3 doesn't seem to create the font panel
415 //is this is done, so create it ourselves
416 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
420 if(m_fontData.m_fontColour.IsOk())
421 [[NSColorPanel sharedColorPanel] setColor:
422 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
423 green:m_fontData.m_fontColour.Green() / 255.0
424 blue:m_fontData.m_fontColour.Blue() / 255.0
428 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
430 //We're done - free up the pool
436 int wxFontDialog::ShowModal()
438 WX_TESTING_SHOW_MODAL_HOOK();
440 //Start the pool. Required for carbon interaction
441 //(For those curious, the only thing that happens
442 //if you don't do this is a bunch of error
443 //messages about leaks on the console,
444 //with no windows shown or anything).
445 NSAutoreleasePool *thePool;
446 thePool = [[NSAutoreleasePool alloc] init];
448 //Get the shared color and font panel
449 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
450 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
452 //Create and assign the delegates (cocoa event handlers) so
453 //we can tell if a window has closed/open or not
454 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
455 [theFontPanel setDelegate:theFPDelegate];
457 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
458 [theColorPanel setDelegate:theCPDelegate];
461 // Begin the modal loop for the font and color panels
463 // The idea is that we first make the font panel modal,
464 // but if the color panel is opened, unless we stop the
465 // modal loop the color panel opens behind the font panel
466 // with no input acceptable to it - which makes it useless.
468 // So we set up delegates for both the color and font panels,
469 // and the if the font panel opens the color panel, we
470 // stop the modal loop, and start a separate modal loop for
471 // the color panel until the color panel closes, switching
472 // back to the font panel modal loop once it does close.
474 wxDialog::OSXBeginModalDialog();
478 // Start the font panel modal loop
480 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
483 [NSApp runModalSession:session];
485 //If the font panel is closed or the font panel
486 //opened the color panel, break
487 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
490 [NSApp endModalSession:session];
492 //is the color panel open?
493 if ([theCPDelegate isOpen])
496 // Start the color panel modal loop
498 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
501 [NSApp runModalSession:session];
503 //If the color panel is closed, return the font panel modal loop
504 if ([theCPDelegate isClosed])
507 [NSApp endModalSession:session];
509 //If the font panel is still alive (I.E. we broke
510 //out of its modal loop because the color panel was
511 //opened) return the font panel modal loop
512 }while([theFPDelegate isClosed] == NO);
513 wxDialog::OSXEndModalDialog();
515 //free up the memory for the delegates - we don't need them anymore
516 [theFPDelegate release];
517 [theCPDelegate release];
519 //Get the font the user selected
520 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
522 //Get more information about the user's chosen font
523 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
524 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
525 int theFontSize = (int) [theFont pointSize];
527 //Set the wx font to the appropriate data
528 if(theTraits & NSFixedPitchFontMask)
529 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
531 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
532 m_fontData.m_chosenFont.SetPointSize(theFontSize);
533 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
534 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
535 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
537 //Get the shared color panel along with the chosen color and set the chosen color
538 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
540 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
541 (unsigned char) ([theColor greenComponent] * 255.0),
542 (unsigned char) ([theColor blueComponent] * 255.0));
544 //Friendly debug stuff
546 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"),
552 m_fontData.m_chosenFont.GetFaceName().c_str(),
553 m_fontData.m_chosenFont.GetPointSize(),
554 m_fontData.m_chosenFont.GetStyle(),
555 m_fontData.m_chosenFont.GetWeight(),
556 m_fontData.m_fontColour.Red(),
557 m_fontData.m_fontColour.Green(),
558 m_fontData.m_fontColour.Blue() );
561 //Release the pool, we're done :)
564 //Return ID_OK - there are no "apply" buttons or the like
565 //on either the font or color panel
569 //old api stuff (REMOVE ME)
570 bool wxFontDialog::IsShown() const