1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/fontdlgosx.cpp
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 wxMAC_USE_EXPERIMENTAL_FONTDIALOG
42 #import <Foundation/Foundation.h>
43 #import <AppKit/AppKit.h>
45 #include "wx/mac/uma.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 wxMacCFStringHolder cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
69 wxMacCFStringHolder cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
71 NSRect rectCancel = NSMakeRect( 10.0 , 10.0 , 82 , 24 );
72 NSRect rectOK = NSMakeRect( 100.0 , 10.0 , 82 , 24 );
74 NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
75 [cancelButton setTitle:(NSString*)cfCancelString.Detach()];
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*)cfOkString.Detach()];
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* WXUNUSED(dialog))
142 int retval = wxID_CANCEL ;
144 bool cocoaLoaded = NSApplicationLoad();
145 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
147 wxAutoNSAutoreleasePool pool;
149 // setting up the ok/cancel buttons
150 NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
152 // adjust modality for carbon environment
153 WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
154 SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
155 SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
157 [fontPanel setFloatingPanel:NO] ;
158 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:NO] ;
160 wxMacFontPanelAccView* accessoryView = (wxMacFontPanelAccView*) [fontPanel accessoryView] ;
161 if ( accessoryView == nil)
163 NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
164 accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
165 [fontPanel setAccessoryView:accessoryView];
167 [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
170 [accessoryView resetFlags];
172 NSModalSession session = [NSApp beginModalSessionForWindow:fontPanel];
174 [NSApp runModalSession:session];
176 [NSApp endModalSession:session];
178 // if we don't reenable it, FPShowHideFontPanel does not work
179 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:YES] ;
180 if( FPIsFontPanelVisible())
181 FPShowHideFontPanel() ;
183 if ( [accessoryView closedWithOk])
194 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
196 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
200 #import <AppKit/NSFont.h>
201 #import <AppKit/NSFontManager.h>
202 #import <AppKit/NSFontPanel.h>
203 #import <AppKit/NSColor.h>
204 #import <AppKit/NSColorPanel.h>
206 // ---------------------------------------------------------------------------
207 // wxWCDelegate - Window Closed delegate
208 // ---------------------------------------------------------------------------
210 @interface wxWCDelegate : NSObject
217 - (BOOL)windowShouldClose:(id)sender;
219 @end // interface wxNSFontPanelDelegate : NSObject
221 @implementation wxWCDelegate : NSObject
231 - (BOOL)windowShouldClose:(id)sender
245 @end // wxNSFontPanelDelegate
247 // ---------------------------------------------------------------------------
248 // wxWCODelegate - Window Closed or Open delegate
249 // ---------------------------------------------------------------------------
251 @interface wxWCODelegate : NSObject
259 - (BOOL)windowShouldClose:(id)sender;
260 - (void)windowDidUpdate:(NSNotification *)aNotification;
263 @end // interface wxNSFontPanelDelegate : NSObject
265 @implementation wxWCODelegate : NSObject
276 - (BOOL)windowShouldClose:(id)sender
286 - (void)windowDidUpdate:(NSNotification *)aNotification
308 @end // wxNSFontPanelDelegate
310 // ---------------------------------------------------------------------------
312 // ---------------------------------------------------------------------------
314 wxFontDialog::wxFontDialog()
318 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
320 Create(parent, data);
323 wxFontDialog::~wxFontDialog()
327 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
332 // This is the key call - this initializes
333 // events and window stuff for cocoa for carbon
336 // This is also the only call here that is
337 // 10.2+ specific (the rest is OSX only),
338 // which, ironically, the carbon font
341 bool bOK = NSApplicationLoad();
343 //autorelease pool - req'd for carbon
344 NSAutoreleasePool *thePool;
345 thePool = [[NSAutoreleasePool alloc] init];
347 //Get the initial wx font
348 wxFont& thewxfont = m_fontData.m_initialFont;
350 //if the font is valid set the default (selected) font of the
351 //NSFontDialog to that font
354 NSFontTraitMask theMask = 0;
356 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
357 theMask |= NSItalicFontMask;
359 if(thewxfont.IsFixedWidth())
360 theMask |= NSFixedPitchFontMask;
362 NSFont* theDefaultFont =
363 [[NSFontManager sharedFontManager] fontWithFamily:
364 wxNSStringWithWxString(thewxfont.GetFaceName())
366 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
367 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
368 size: (float)(thewxfont.GetPointSize())
371 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
373 //Apple docs say to call NSFontManager::setSelectedFont
374 //However, 10.3 doesn't seem to create the font panel
375 //is this is done, so create it ourselves
376 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
380 if(m_fontData.m_fontColour.Ok())
381 [[NSColorPanel sharedColorPanel] setColor:
382 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
383 green:m_fontData.m_fontColour.Green() / 255.0
384 blue:m_fontData.m_fontColour.Blue() / 255.0
388 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
390 //We're done - free up the pool
396 int wxFontDialog::ShowModal()
398 //Start the pool. Required for carbon interaction
399 //(For those curious, the only thing that happens
400 //if you don't do this is a bunch of error
401 //messages about leaks on the console,
402 //with no windows shown or anything).
403 NSAutoreleasePool *thePool;
404 thePool = [[NSAutoreleasePool alloc] init];
406 //Get the shared color and font panel
407 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
408 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
410 //Create and assign the delegates (cocoa event handlers) so
411 //we can tell if a window has closed/open or not
412 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
413 [theFontPanel setDelegate:theFPDelegate];
415 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
416 [theColorPanel setDelegate:theCPDelegate];
419 // Begin the modal loop for the font and color panels
421 // The idea is that we first make the font panel modal,
422 // but if the color panel is opened, unless we stop the
423 // modal loop the color panel opens behind the font panel
424 // with no input acceptable to it - which makes it useless.
426 // So we set up delegates for both the color and font panels,
427 // and the if the font panel opens the color panel, we
428 // stop the modal loop, and start a separate modal loop for
429 // the color panel until the color panel closes, switching
430 // back to the font panel modal loop once it does close.
435 // Start the font panel modal loop
437 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
440 [NSApp runModalSession:session];
442 //If the font panel is closed or the font panel
443 //opened the color panel, break
444 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
447 [NSApp endModalSession:session];
449 //is the color panel open?
450 if ([theCPDelegate isOpen])
453 // Start the color panel modal loop
455 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
458 [NSApp runModalSession:session];
460 //If the color panel is closed, return the font panel modal loop
461 if ([theCPDelegate isClosed])
464 [NSApp endModalSession:session];
466 //If the font panel is still alive (I.E. we broke
467 //out of its modal loop because the color panel was
468 //opened) return the font panel modal loop
469 }while([theFPDelegate isClosed] == NO);
471 //free up the memory for the delegates - we don't need them anymore
472 [theFPDelegate release];
473 [theCPDelegate release];
475 //Get the font the user selected
476 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
478 //Get more information about the user's chosen font
479 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
480 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
481 int theFontSize = (int) [theFont pointSize];
483 //Set the wx font to the appropriate data
484 if(theTraits & NSFixedPitchFontMask)
485 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
487 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
488 m_fontData.m_chosenFont.SetPointSize(theFontSize);
489 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
490 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
491 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
493 //Get the shared color panel along with the chosen color and set the chosen color
494 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
496 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
497 (unsigned char) ([theColor greenComponent] * 255.0),
498 (unsigned char) ([theColor blueComponent] * 255.0));
500 //Friendly debug stuff
502 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"),
508 m_fontData.m_chosenFont.GetFaceName().c_str(),
509 m_fontData.m_chosenFont.GetPointSize(),
510 m_fontData.m_chosenFont.GetStyle(),
511 m_fontData.m_chosenFont.GetWeight(),
512 m_fontData.m_fontColour.Red(),
513 m_fontData.m_fontColour.Green(),
514 m_fontData.m_fontColour.Blue() );
517 //Release the pool, we're done :)
520 //Return ID_OK - there are no "apply" buttons or the like
521 //on either the font or color panel
525 //old api stuff (REMOVE ME)
526 bool wxFontDialog::IsShown() const