1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontDialog class.
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #include "wx/cmndata.h"
21 #include "wx/fontdlg.h"
22 #include "wx/fontutil.h"
25 // ============================================================================
27 // ============================================================================
30 #include "wx/cocoa/autorelease.h"
31 #include "wx/cocoa/string.h"
33 #if wxMAC_USE_EXPERIMENTAL_FONTDIALOG
35 #import <Foundation/Foundation.h>
36 #import <AppKit/AppKit.h>
38 #include "wx/mac/uma.h"
41 @interface wxMacFontPanelAccView : NSView
45 NSButton* m_cancelButton ;
46 NSButton* m_okButton ;
49 - (IBAction)cancelPressed:(id)sender;
50 - (IBAction)okPressed:(id)sender;
53 - (BOOL)shouldCloseCarbon;
54 - (NSButton*)okButton;
57 @implementation wxMacFontPanelAccView : NSView
58 - (id)initWithFrame:(NSRect)rectBox
60 [super initWithFrame:rectBox];
62 wxMacCFStringHolder cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
63 wxMacCFStringHolder cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
65 NSRect rectCancel = NSMakeRect( 10.0 , 10.0 , 82 , 24 );
66 NSRect rectOK = NSMakeRect( 100.0 , 10.0 , 82 , 24 );
67 NSView* panel = [[NSView alloc] initWithFrame:rectBox];
69 NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
70 [cancelButton setTitle:(NSString*)cfCancelString.Detach()];
71 [cancelButton setBezelStyle:NSRoundedBezelStyle];
72 [cancelButton setButtonType:NSMomentaryPushInButton];
73 [cancelButton setAction:@selector(cancelPressed:)];
74 [cancelButton setTarget:self];
75 m_cancelButton = cancelButton ;
77 NSButton* okButton = [[NSButton alloc] initWithFrame:rectOK];
78 [okButton setTitle:(NSString*)cfOkString.Detach()];
79 [okButton setBezelStyle:NSRoundedBezelStyle];
80 [okButton setButtonType:NSMomentaryPushInButton];
81 [okButton setAction:@selector(okPressed:)];
82 [okButton setTarget:self];
83 // doesn't help either, the button is not highlighted after a color dialog has been used
84 // [okButton setKeyEquivalent:@"\r"];
85 m_okButton = okButton ;
88 [self addSubview:cancelButton];
89 [self addSubview:okButton];
101 - (IBAction)cancelPressed:(id)sender
103 m_shouldClose = YES ;
107 - (IBAction)okPressed:(id)sender
110 m_shouldClose = YES ;
119 -(BOOL)shouldCloseCarbon
121 return m_shouldClose ;
131 extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
133 int RunMixedFontDialog(wxFontDialog* dialog)
135 int retval = wxID_CANCEL ;
137 bool cocoaLoaded = NSApplicationLoad();
138 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
140 wxAutoNSAutoreleasePool pool;
142 // setting up the ok/cancel buttons
143 NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
145 // adjust modality for carbon environment
146 WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
147 SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
148 SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
150 [fontPanel setFloatingPanel:NO] ;
152 wxMacFontPanelAccView* accessoryView = (wxMacFontPanelAccView*) [fontPanel accessoryView] ;
153 if ( accessoryView == nil)
155 NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
156 accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
157 [fontPanel setAccessoryView:accessoryView];
159 [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
162 [accessoryView resetFlags];
164 NSModalSession session = [NSApp beginModalSessionForWindow:fontPanel];
166 [NSApp runModalSession:session];
168 [NSApp endModalSession:session];
170 if( FPIsFontPanelVisible())
171 FPShowHideFontPanel() ;
173 if ( [accessoryView closedWithOk])
184 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
186 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
190 #import <AppKit/NSFont.h>
191 #import <AppKit/NSFontManager.h>
192 #import <AppKit/NSFontPanel.h>
193 #import <AppKit/NSColor.h>
194 #import <AppKit/NSColorPanel.h>
196 // ---------------------------------------------------------------------------
197 // wxWCDelegate - Window Closed delegate
198 // ---------------------------------------------------------------------------
200 @interface wxWCDelegate : NSObject
207 - (BOOL)windowShouldClose:(id)sender;
209 @end // interface wxNSFontPanelDelegate : NSObject
211 @implementation wxWCDelegate : NSObject
221 - (BOOL)windowShouldClose:(id)sender
235 @end // wxNSFontPanelDelegate
237 // ---------------------------------------------------------------------------
238 // wxWCODelegate - Window Closed or Open delegate
239 // ---------------------------------------------------------------------------
241 @interface wxWCODelegate : NSObject
249 - (BOOL)windowShouldClose:(id)sender;
250 - (void)windowDidUpdate:(NSNotification *)aNotification;
253 @end // interface wxNSFontPanelDelegate : NSObject
255 @implementation wxWCODelegate : NSObject
266 - (BOOL)windowShouldClose:(id)sender
276 - (void)windowDidUpdate:(NSNotification *)aNotification
298 @end // wxNSFontPanelDelegate
300 // ---------------------------------------------------------------------------
302 // ---------------------------------------------------------------------------
304 wxFontDialog::wxFontDialog()
308 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
310 Create(parent, data);
313 wxFontDialog::~wxFontDialog()
317 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
322 // This is the key call - this initializes
323 // events and window stuff for cocoa for carbon
326 // This is also the only call here that is
327 // 10.2+ specific (the rest is OSX only),
328 // which, ironically, the carbon font
331 bool bOK = NSApplicationLoad();
333 //autorelease pool - req'd for carbon
334 NSAutoreleasePool *thePool;
335 thePool = [[NSAutoreleasePool alloc] init];
337 //Get the initial wx font
338 wxFont& thewxfont = m_fontData.m_initialFont;
340 //if the font is valid set the default (selected) font of the
341 //NSFontDialog to that font
344 NSFontTraitMask theMask = 0;
346 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
347 theMask |= NSItalicFontMask;
349 if(thewxfont.IsFixedWidth())
350 theMask |= NSFixedPitchFontMask;
352 NSFont* theDefaultFont =
353 [[NSFontManager sharedFontManager] fontWithFamily:
354 wxNSStringWithWxString(thewxfont.GetFaceName())
356 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
357 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
358 size: (float)(thewxfont.GetPointSize())
361 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
363 //Apple docs say to call NSFontManager::setSelectedFont
364 //However, 10.3 doesn't seem to create the font panel
365 //is this is done, so create it ourselves
366 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
370 if(m_fontData.m_fontColour.Ok())
371 [[NSColorPanel sharedColorPanel] setColor:
372 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
373 green:m_fontData.m_fontColour.Green() / 255.0
374 blue:m_fontData.m_fontColour.Blue() / 255.0
378 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
380 //We're done - free up the pool
386 int wxFontDialog::ShowModal()
388 //Start the pool. Required for carbon interaction
389 //(For those curious, the only thing that happens
390 //if you don't do this is a bunch of error
391 //messages about leaks on the console,
392 //with no windows shown or anything).
393 NSAutoreleasePool *thePool;
394 thePool = [[NSAutoreleasePool alloc] init];
396 //Get the shared color and font panel
397 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
398 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
400 //Create and assign the delegates (cocoa event handlers) so
401 //we can tell if a window has closed/open or not
402 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
403 [theFontPanel setDelegate:theFPDelegate];
405 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
406 [theColorPanel setDelegate:theCPDelegate];
409 // Begin the modal loop for the font and color panels
411 // The idea is that we first make the font panel modal,
412 // but if the color panel is opened, unless we stop the
413 // modal loop the color panel opens behind the font panel
414 // with no input acceptable to it - which makes it useless.
416 // So we set up delegates for both the color and font panels,
417 // and the if the font panel opens the color panel, we
418 // stop the modal loop, and start a separate modal loop for
419 // the color panel until the color panel closes, switching
420 // back to the font panel modal loop once it does close.
425 // Start the font panel modal loop
427 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
430 [NSApp runModalSession:session];
432 //If the font panel is closed or the font panel
433 //opened the color panel, break
434 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
437 [NSApp endModalSession:session];
439 //is the color panel open?
440 if ([theCPDelegate isOpen])
443 // Start the color panel modal loop
445 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
448 [NSApp runModalSession:session];
450 //If the color panel is closed, return the font panel modal loop
451 if ([theCPDelegate isClosed])
454 [NSApp endModalSession:session];
456 //If the font panel is still alive (I.E. we broke
457 //out of its modal loop because the color panel was
458 //opened) return the font panel modal loop
459 }while([theFPDelegate isClosed] == NO);
461 //free up the memory for the delegates - we don't need them anymore
462 [theFPDelegate release];
463 [theCPDelegate release];
465 //Get the font the user selected
466 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
468 //Get more information about the user's chosen font
469 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
470 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
471 int theFontSize = (int) [theFont pointSize];
473 //Set the wx font to the appropriate data
474 if(theTraits & NSFixedPitchFontMask)
475 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
477 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
478 m_fontData.m_chosenFont.SetPointSize(theFontSize);
479 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
480 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
481 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
483 //Get the shared color panel along with the chosen color and set the chosen color
484 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
486 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
487 (unsigned char) ([theColor greenComponent] * 255.0),
488 (unsigned char) ([theColor blueComponent] * 255.0));
490 //Friendly debug stuff
492 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"),
498 m_fontData.m_chosenFont.GetFaceName().c_str(),
499 m_fontData.m_chosenFont.GetPointSize(),
500 m_fontData.m_chosenFont.GetStyle(),
501 m_fontData.m_chosenFont.GetWeight(),
502 m_fontData.m_fontColour.Red(),
503 m_fontData.m_fontColour.Green(),
504 m_fontData.m_fontColour.Blue() );
507 //Release the pool, we're done :)
510 //Return ID_OK - there are no "apply" buttons or the like
511 //on either the font or color panel
515 //old api stuff (REMOVE ME)
516 bool wxFontDialog::IsShown() const