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"
40 @interface wxMacFontPanelAccView : NSView
44 NSButton* m_cancelButton ;
45 NSButton* m_okButton ;
48 - (IBAction)cancelPressed:(id)sender;
49 - (IBAction)okPressed:(id)sender;
52 - (BOOL)shouldCloseCarbon;
53 - (NSButton*)okButton;
56 @implementation wxMacFontPanelAccView : NSView
57 - (id)initWithFrame:(NSRect)rectBox
59 [super initWithFrame:rectBox];
61 wxMacCFStringHolder cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
62 wxMacCFStringHolder cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
64 NSRect rectCancel = NSMakeRect( 10.0 , 10.0 , 82 , 24 );
65 NSRect rectOK = NSMakeRect( 100.0 , 10.0 , 82 , 24 );
66 NSView* panel = [[NSView alloc] initWithFrame:rectBox];
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] ;
151 wxMacFontPanelAccView* accessoryView = (wxMacFontPanelAccView*) [fontPanel accessoryView] ;
152 if ( accessoryView == nil)
154 NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
155 accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
156 [fontPanel setAccessoryView:accessoryView];
158 [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
161 [accessoryView resetFlags];
163 NSModalSession session = [NSApp beginModalSessionForWindow:fontPanel];
165 [NSApp runModalSession:session];
167 [NSApp endModalSession:session];
169 if( FPIsFontPanelVisible())
170 FPShowHideFontPanel() ;
172 if ( [accessoryView closedWithOk])
183 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
185 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
189 #import <AppKit/NSFont.h>
190 #import <AppKit/NSFontManager.h>
191 #import <AppKit/NSFontPanel.h>
192 #import <AppKit/NSColor.h>
193 #import <AppKit/NSColorPanel.h>
195 // ---------------------------------------------------------------------------
196 // wxWCDelegate - Window Closed delegate
197 // ---------------------------------------------------------------------------
199 @interface wxWCDelegate : NSObject
206 - (BOOL)windowShouldClose:(id)sender;
208 @end // interface wxNSFontPanelDelegate : NSObject
210 @implementation wxWCDelegate : NSObject
220 - (BOOL)windowShouldClose:(id)sender
234 @end // wxNSFontPanelDelegate
236 // ---------------------------------------------------------------------------
237 // wxWCODelegate - Window Closed or Open delegate
238 // ---------------------------------------------------------------------------
240 @interface wxWCODelegate : NSObject
248 - (BOOL)windowShouldClose:(id)sender;
249 - (void)windowDidUpdate:(NSNotification *)aNotification;
252 @end // interface wxNSFontPanelDelegate : NSObject
254 @implementation wxWCODelegate : NSObject
265 - (BOOL)windowShouldClose:(id)sender
275 - (void)windowDidUpdate:(NSNotification *)aNotification
297 @end // wxNSFontPanelDelegate
299 // ---------------------------------------------------------------------------
301 // ---------------------------------------------------------------------------
303 wxFontDialog::wxFontDialog()
307 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
309 Create(parent, data);
312 wxFontDialog::~wxFontDialog()
316 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
321 // This is the key call - this initializes
322 // events and window stuff for cocoa for carbon
325 // This is also the only call here that is
326 // 10.2+ specific (the rest is OSX only),
327 // which, ironically, the carbon font
330 bool bOK = NSApplicationLoad();
332 //autorelease pool - req'd for carbon
333 NSAutoreleasePool *thePool;
334 thePool = [[NSAutoreleasePool alloc] init];
336 //Get the initial wx font
337 wxFont& thewxfont = m_fontData.m_initialFont;
339 //if the font is valid set the default (selected) font of the
340 //NSFontDialog to that font
343 NSFontTraitMask theMask = 0;
345 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
346 theMask |= NSItalicFontMask;
348 if(thewxfont.IsFixedWidth())
349 theMask |= NSFixedPitchFontMask;
351 NSFont* theDefaultFont =
352 [[NSFontManager sharedFontManager] fontWithFamily:
353 wxNSStringWithWxString(thewxfont.GetFaceName())
355 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
356 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
357 size: (float)(thewxfont.GetPointSize())
360 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
362 //Apple docs say to call NSFontManager::setSelectedFont
363 //However, 10.3 doesn't seem to create the font panel
364 //is this is done, so create it ourselves
365 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
369 if(m_fontData.m_fontColour.Ok())
370 [[NSColorPanel sharedColorPanel] setColor:
371 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
372 green:m_fontData.m_fontColour.Green() / 255.0
373 blue:m_fontData.m_fontColour.Blue() / 255.0
377 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
379 //We're done - free up the pool
385 int wxFontDialog::ShowModal()
387 //Start the pool. Required for carbon interaction
388 //(For those curious, the only thing that happens
389 //if you don't do this is a bunch of error
390 //messages about leaks on the console,
391 //with no windows shown or anything).
392 NSAutoreleasePool *thePool;
393 thePool = [[NSAutoreleasePool alloc] init];
395 //Get the shared color and font panel
396 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
397 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
399 //Create and assign the delegates (cocoa event handlers) so
400 //we can tell if a window has closed/open or not
401 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
402 [theFontPanel setDelegate:theFPDelegate];
404 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
405 [theColorPanel setDelegate:theCPDelegate];
408 // Begin the modal loop for the font and color panels
410 // The idea is that we first make the font panel modal,
411 // but if the color panel is opened, unless we stop the
412 // modal loop the color panel opens behind the font panel
413 // with no input acceptable to it - which makes it useless.
415 // So we set up delegates for both the color and font panels,
416 // and the if the font panel opens the color panel, we
417 // stop the modal loop, and start a separate modal loop for
418 // the color panel until the color panel closes, switching
419 // back to the font panel modal loop once it does close.
424 // Start the font panel modal loop
426 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
429 [NSApp runModalSession:session];
431 //If the font panel is closed or the font panel
432 //opened the color panel, break
433 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
436 [NSApp endModalSession:session];
438 //is the color panel open?
439 if ([theCPDelegate isOpen])
442 // Start the color panel modal loop
444 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
447 [NSApp runModalSession:session];
449 //If the color panel is closed, return the font panel modal loop
450 if ([theCPDelegate isClosed])
453 [NSApp endModalSession:session];
455 //If the font panel is still alive (I.E. we broke
456 //out of its modal loop because the color panel was
457 //opened) return the font panel modal loop
458 }while([theFPDelegate isClosed] == NO);
460 //free up the memory for the delegates - we don't need them anymore
461 [theFPDelegate release];
462 [theCPDelegate release];
464 //Get the font the user selected
465 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
467 //Get more information about the user's chosen font
468 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
469 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
470 int theFontSize = (int) [theFont pointSize];
472 //Set the wx font to the appropriate data
473 if(theTraits & NSFixedPitchFontMask)
474 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
476 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
477 m_fontData.m_chosenFont.SetPointSize(theFontSize);
478 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
479 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
480 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
482 //Get the shared color panel along with the chosen color and set the chosen color
483 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
485 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
486 (unsigned char) ([theColor greenComponent] * 255.0),
487 (unsigned char) ([theColor blueComponent] * 255.0));
489 //Friendly debug stuff
491 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"),
497 m_fontData.m_chosenFont.GetFaceName().c_str(),
498 m_fontData.m_chosenFont.GetPointSize(),
499 m_fontData.m_chosenFont.GetStyle(),
500 m_fontData.m_chosenFont.GetWeight(),
501 m_fontData.m_fontColour.Red(),
502 m_fontData.m_fontColour.Green(),
503 m_fontData.m_fontColour.Blue() );
506 //Release the pool, we're done :)
509 //Return ID_OK - there are no "apply" buttons or the like
510 //on either the font or color panel
514 //old api stuff (REMOVE ME)
515 bool wxFontDialog::IsShown() const