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 );
68 NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
69 [cancelButton setTitle:(NSString*)cfCancelString.Detach()];
70 [cancelButton setBezelStyle:NSRoundedBezelStyle];
71 [cancelButton setButtonType:NSMomentaryPushInButton];
72 [cancelButton setAction:@selector(cancelPressed:)];
73 [cancelButton setTarget:self];
74 m_cancelButton = cancelButton ;
76 NSButton* okButton = [[NSButton alloc] initWithFrame:rectOK];
77 [okButton setTitle:(NSString*)cfOkString.Detach()];
78 [okButton setBezelStyle:NSRoundedBezelStyle];
79 [okButton setButtonType:NSMomentaryPushInButton];
80 [okButton setAction:@selector(okPressed:)];
81 [okButton setTarget:self];
82 // doesn't help either, the button is not highlighted after a color dialog has been used
83 // [okButton setKeyEquivalent:@"\r"];
84 m_okButton = okButton ;
87 [self addSubview:cancelButton];
88 [self addSubview:okButton];
100 - (IBAction)cancelPressed:(id)sender
102 m_shouldClose = YES ;
106 - (IBAction)okPressed:(id)sender
109 m_shouldClose = YES ;
118 -(BOOL)shouldCloseCarbon
120 return m_shouldClose ;
130 extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
132 int RunMixedFontDialog(wxFontDialog* dialog)
134 int retval = wxID_CANCEL ;
136 bool cocoaLoaded = NSApplicationLoad();
137 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
139 wxAutoNSAutoreleasePool pool;
141 // setting up the ok/cancel buttons
142 NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
144 // adjust modality for carbon environment
145 WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
146 SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
147 SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
149 [fontPanel setFloatingPanel:NO] ;
150 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled: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 we don't reenable it, FPShowHideFontPanel does not work
171 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:YES] ;
172 if( FPIsFontPanelVisible())
173 FPShowHideFontPanel() ;
175 if ( [accessoryView closedWithOk])
186 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
188 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
192 #import <AppKit/NSFont.h>
193 #import <AppKit/NSFontManager.h>
194 #import <AppKit/NSFontPanel.h>
195 #import <AppKit/NSColor.h>
196 #import <AppKit/NSColorPanel.h>
198 // ---------------------------------------------------------------------------
199 // wxWCDelegate - Window Closed delegate
200 // ---------------------------------------------------------------------------
202 @interface wxWCDelegate : NSObject
209 - (BOOL)windowShouldClose:(id)sender;
211 @end // interface wxNSFontPanelDelegate : NSObject
213 @implementation wxWCDelegate : NSObject
223 - (BOOL)windowShouldClose:(id)sender
237 @end // wxNSFontPanelDelegate
239 // ---------------------------------------------------------------------------
240 // wxWCODelegate - Window Closed or Open delegate
241 // ---------------------------------------------------------------------------
243 @interface wxWCODelegate : NSObject
251 - (BOOL)windowShouldClose:(id)sender;
252 - (void)windowDidUpdate:(NSNotification *)aNotification;
255 @end // interface wxNSFontPanelDelegate : NSObject
257 @implementation wxWCODelegate : NSObject
268 - (BOOL)windowShouldClose:(id)sender
278 - (void)windowDidUpdate:(NSNotification *)aNotification
300 @end // wxNSFontPanelDelegate
302 // ---------------------------------------------------------------------------
304 // ---------------------------------------------------------------------------
306 wxFontDialog::wxFontDialog()
310 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
312 Create(parent, data);
315 wxFontDialog::~wxFontDialog()
319 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
324 // This is the key call - this initializes
325 // events and window stuff for cocoa for carbon
328 // This is also the only call here that is
329 // 10.2+ specific (the rest is OSX only),
330 // which, ironically, the carbon font
333 bool bOK = NSApplicationLoad();
335 //autorelease pool - req'd for carbon
336 NSAutoreleasePool *thePool;
337 thePool = [[NSAutoreleasePool alloc] init];
339 //Get the initial wx font
340 wxFont& thewxfont = m_fontData.m_initialFont;
342 //if the font is valid set the default (selected) font of the
343 //NSFontDialog to that font
346 NSFontTraitMask theMask = 0;
348 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
349 theMask |= NSItalicFontMask;
351 if(thewxfont.IsFixedWidth())
352 theMask |= NSFixedPitchFontMask;
354 NSFont* theDefaultFont =
355 [[NSFontManager sharedFontManager] fontWithFamily:
356 wxNSStringWithWxString(thewxfont.GetFaceName())
358 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
359 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
360 size: (float)(thewxfont.GetPointSize())
363 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
365 //Apple docs say to call NSFontManager::setSelectedFont
366 //However, 10.3 doesn't seem to create the font panel
367 //is this is done, so create it ourselves
368 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
372 if(m_fontData.m_fontColour.Ok())
373 [[NSColorPanel sharedColorPanel] setColor:
374 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
375 green:m_fontData.m_fontColour.Green() / 255.0
376 blue:m_fontData.m_fontColour.Blue() / 255.0
380 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
382 //We're done - free up the pool
388 int wxFontDialog::ShowModal()
390 //Start the pool. Required for carbon interaction
391 //(For those curious, the only thing that happens
392 //if you don't do this is a bunch of error
393 //messages about leaks on the console,
394 //with no windows shown or anything).
395 NSAutoreleasePool *thePool;
396 thePool = [[NSAutoreleasePool alloc] init];
398 //Get the shared color and font panel
399 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
400 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
402 //Create and assign the delegates (cocoa event handlers) so
403 //we can tell if a window has closed/open or not
404 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
405 [theFontPanel setDelegate:theFPDelegate];
407 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
408 [theColorPanel setDelegate:theCPDelegate];
411 // Begin the modal loop for the font and color panels
413 // The idea is that we first make the font panel modal,
414 // but if the color panel is opened, unless we stop the
415 // modal loop the color panel opens behind the font panel
416 // with no input acceptable to it - which makes it useless.
418 // So we set up delegates for both the color and font panels,
419 // and the if the font panel opens the color panel, we
420 // stop the modal loop, and start a separate modal loop for
421 // the color panel until the color panel closes, switching
422 // back to the font panel modal loop once it does close.
427 // Start the font panel modal loop
429 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
432 [NSApp runModalSession:session];
434 //If the font panel is closed or the font panel
435 //opened the color panel, break
436 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
439 [NSApp endModalSession:session];
441 //is the color panel open?
442 if ([theCPDelegate isOpen])
445 // Start the color panel modal loop
447 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
450 [NSApp runModalSession:session];
452 //If the color panel is closed, return the font panel modal loop
453 if ([theCPDelegate isClosed])
456 [NSApp endModalSession:session];
458 //If the font panel is still alive (I.E. we broke
459 //out of its modal loop because the color panel was
460 //opened) return the font panel modal loop
461 }while([theFPDelegate isClosed] == NO);
463 //free up the memory for the delegates - we don't need them anymore
464 [theFPDelegate release];
465 [theCPDelegate release];
467 //Get the font the user selected
468 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
470 //Get more information about the user's chosen font
471 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
472 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
473 int theFontSize = (int) [theFont pointSize];
475 //Set the wx font to the appropriate data
476 if(theTraits & NSFixedPitchFontMask)
477 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
479 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
480 m_fontData.m_chosenFont.SetPointSize(theFontSize);
481 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
482 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
483 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
485 //Get the shared color panel along with the chosen color and set the chosen color
486 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
488 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
489 (unsigned char) ([theColor greenComponent] * 255.0),
490 (unsigned char) ([theColor blueComponent] * 255.0));
492 //Friendly debug stuff
494 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"),
500 m_fontData.m_chosenFont.GetFaceName().c_str(),
501 m_fontData.m_chosenFont.GetPointSize(),
502 m_fontData.m_chosenFont.GetStyle(),
503 m_fontData.m_chosenFont.GetWeight(),
504 m_fontData.m_fontColour.Red(),
505 m_fontData.m_fontColour.Green(),
506 m_fontData.m_fontColour.Blue() );
509 //Release the pool, we're done :)
512 //Return ID_OK - there are no "apply" buttons or the like
513 //on either the font or color panel
517 //old api stuff (REMOVE ME)
518 bool wxFontDialog::IsShown() const