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 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
32 #if !USE_SHARED_LIBRARY
33 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
37 #include "wx/cocoa/autorelease.h"
38 #include "wx/cocoa/string.h"
40 #import <AppKit/NSFont.h>
41 #import <AppKit/NSFontManager.h>
42 #import <AppKit/NSFontPanel.h>
43 #import <AppKit/NSColor.h>
44 #import <AppKit/NSColorPanel.h>
46 // ---------------------------------------------------------------------------
47 // wxWCDelegate - Window Closed delegate
48 // ---------------------------------------------------------------------------
50 @interface wxWCDelegate : NSObject
57 - (BOOL)windowShouldClose:(id)sender;
59 @end // interface wxNSFontPanelDelegate : NSObject
61 @implementation wxWCDelegate : NSObject
71 - (BOOL)windowShouldClose:(id)sender
85 @end // wxNSFontPanelDelegate
87 // ---------------------------------------------------------------------------
88 // wxWCODelegate - Window Closed or Open delegate
89 // ---------------------------------------------------------------------------
91 @interface wxWCODelegate : NSObject
99 - (BOOL)windowShouldClose:(id)sender;
100 - (void)windowDidUpdate:(NSNotification *)aNotification;
103 @end // interface wxNSFontPanelDelegate : NSObject
105 @implementation wxWCODelegate : NSObject
116 - (BOOL)windowShouldClose:(id)sender
126 - (void)windowDidUpdate:(NSNotification *)aNotification
148 @end // wxNSFontPanelDelegate
150 // ---------------------------------------------------------------------------
152 // ---------------------------------------------------------------------------
154 wxFontDialog::wxFontDialog()
158 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
160 Create(parent, data);
163 wxFontDialog::~wxFontDialog()
167 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
172 // This is the key call - this initializes
173 // events and window stuff for cocoa for carbon
176 // This is also the only call here that is
177 // 10.2+ specific (the rest is OSX only),
178 // which, ironically, the carbon font
181 bool bOK = NSApplicationLoad();
183 //autorelease pool - req'd for carbon
184 NSAutoreleasePool *thePool;
185 thePool = [[NSAutoreleasePool alloc] init];
187 //Get the initial wx font
188 wxFont& thewxfont = m_fontData.m_initialFont;
190 //if the font is valid set the default (selected) font of the
191 //NSFontDialog to that font
194 NSFontTraitMask theMask = 0;
196 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
197 theMask |= NSItalicFontMask;
199 if(thewxfont.IsFixedWidth())
200 theMask |= NSFixedPitchFontMask;
202 NSFont* theDefaultFont =
203 [[NSFontManager sharedFontManager] fontWithFamily:
204 wxNSStringWithWxString(thewxfont.GetFaceName())
206 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
207 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
208 size: (float)(thewxfont.GetPointSize())
211 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
213 //Apple docs say to call NSFontManager::setSelectedFont
214 //However, 10.3 doesn't seem to create the font panel
215 //is this is done, so create it ourselves
216 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
220 if(m_fontData.m_fontColour.Ok())
221 [[NSColorPanel sharedColorPanel] setColor:
222 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
223 green:m_fontData.m_fontColour.Green() / 255.0
224 blue:m_fontData.m_fontColour.Blue() / 255.0
228 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
230 //We're done - free up the pool
236 int wxFontDialog::ShowModal()
238 //Start the pool. Required for carbon interaction
239 //(For those curious, the only thing that happens
240 //if you don't do this is a bunch of error
241 //messages about leaks on the console,
242 //with no windows shown or anything).
243 NSAutoreleasePool *thePool;
244 thePool = [[NSAutoreleasePool alloc] init];
246 //Get the shared color and font panel
247 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
248 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
250 //Create and assign the delegates (cocoa event handlers) so
251 //we can tell if a window has closed/open or not
252 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
253 [theFontPanel setDelegate:theFPDelegate];
255 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
256 [theColorPanel setDelegate:theCPDelegate];
259 // Begin the modal loop for the font and color panels
261 // The idea is that we first make the font panel modal,
262 // but if the color panel is opened, unless we stop the
263 // modal loop the color panel opens behind the font panel
264 // with no input acceptable to it - which makes it useless.
266 // So we set up delegates for both the color and font panels,
267 // and the if the font panel opens the color panel, we
268 // stop the modal loop, and start a separate modal loop for
269 // the color panel until the color panel closes, switching
270 // back to the font panel modal loop once it does close.
275 // Start the font panel modal loop
277 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
280 [NSApp runModalSession:session];
282 //If the font panel is closed or the font panel
283 //opened the color panel, break
284 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
287 [NSApp endModalSession:session];
289 //is the color panel open?
290 if ([theCPDelegate isOpen])
293 // Start the color panel modal loop
295 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
298 [NSApp runModalSession:session];
300 //If the color panel is closed, return the font panel modal loop
301 if ([theCPDelegate isClosed])
304 [NSApp endModalSession:session];
306 //If the font panel is still alive (I.E. we broke
307 //out of its modal loop because the color panel was
308 //opened) return the font panel modal loop
309 }while([theFPDelegate isClosed] == NO);
311 //free up the memory for the delegates - we don't need them anymore
312 [theFPDelegate release];
313 [theCPDelegate release];
315 //Get the font the user selected
316 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
318 //Get more information about the user's chosen font
319 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
320 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
321 int theFontSize = (int) [theFont pointSize];
323 //Set the wx font to the appropriate data
324 if(theTraits & NSFixedPitchFontMask)
325 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
327 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
328 m_fontData.m_chosenFont.SetPointSize(theFontSize);
329 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
330 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
331 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
333 //Get the shared color panel along with the chosen color and set the chosen color
334 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
336 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
337 (unsigned char) ([theColor greenComponent] * 255.0),
338 (unsigned char) ([theColor blueComponent] * 255.0));
340 //Friendly debug stuff
342 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"),
348 m_fontData.m_chosenFont.GetFaceName().c_str(),
349 m_fontData.m_chosenFont.GetPointSize(),
350 m_fontData.m_chosenFont.GetStyle(),
351 m_fontData.m_chosenFont.GetWeight(),
352 m_fontData.m_fontColour.Red(),
353 m_fontData.m_fontColour.Green(),
354 m_fontData.m_fontColour.Blue() );
357 //Release the pool, we're done :)
360 //Return ID_OK - there are no "apply" buttons or the like
361 //on either the font or color panel
365 //old api stuff (REMOVE ME)
366 bool wxFontDialog::IsShown() const