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 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
35 #include "wx/cocoa/autorelease.h"
36 #include "wx/cocoa/string.h"
38 #import <AppKit/NSFont.h>
39 #import <AppKit/NSFontManager.h>
40 #import <AppKit/NSFontPanel.h>
41 #import <AppKit/NSColor.h>
42 #import <AppKit/NSColorPanel.h>
44 // ---------------------------------------------------------------------------
45 // wxWCDelegate - Window Closed delegate
46 // ---------------------------------------------------------------------------
48 @interface wxWCDelegate : NSObject
55 - (BOOL)windowShouldClose:(id)sender;
57 @end // interface wxNSFontPanelDelegate : NSObject
59 @implementation wxWCDelegate : NSObject
69 - (BOOL)windowShouldClose:(id)sender
83 @end // wxNSFontPanelDelegate
85 // ---------------------------------------------------------------------------
86 // wxWCODelegate - Window Closed or Open delegate
87 // ---------------------------------------------------------------------------
89 @interface wxWCODelegate : NSObject
97 - (BOOL)windowShouldClose:(id)sender;
98 - (void)windowDidUpdate:(NSNotification *)aNotification;
101 @end // interface wxNSFontPanelDelegate : NSObject
103 @implementation wxWCODelegate : NSObject
114 - (BOOL)windowShouldClose:(id)sender
124 - (void)windowDidUpdate:(NSNotification *)aNotification
146 @end // wxNSFontPanelDelegate
148 // ---------------------------------------------------------------------------
150 // ---------------------------------------------------------------------------
152 wxFontDialog::wxFontDialog()
156 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
158 Create(parent, data);
161 wxFontDialog::~wxFontDialog()
165 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
170 // This is the key call - this initializes
171 // events and window stuff for cocoa for carbon
174 // This is also the only call here that is
175 // 10.2+ specific (the rest is OSX only),
176 // which, ironically, the carbon font
179 bool bOK = NSApplicationLoad();
181 //autorelease pool - req'd for carbon
182 NSAutoreleasePool *thePool;
183 thePool = [[NSAutoreleasePool alloc] init];
185 //Get the initial wx font
186 wxFont& thewxfont = m_fontData.m_initialFont;
188 //if the font is valid set the default (selected) font of the
189 //NSFontDialog to that font
192 NSFontTraitMask theMask = 0;
194 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
195 theMask |= NSItalicFontMask;
197 if(thewxfont.IsFixedWidth())
198 theMask |= NSFixedPitchFontMask;
200 NSFont* theDefaultFont =
201 [[NSFontManager sharedFontManager] fontWithFamily:
202 wxNSStringWithWxString(thewxfont.GetFaceName())
204 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
205 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
206 size: (float)(thewxfont.GetPointSize())
209 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
211 //Apple docs say to call NSFontManager::setSelectedFont
212 //However, 10.3 doesn't seem to create the font panel
213 //is this is done, so create it ourselves
214 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
218 if(m_fontData.m_fontColour.Ok())
219 [[NSColorPanel sharedColorPanel] setColor:
220 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
221 green:m_fontData.m_fontColour.Green() / 255.0
222 blue:m_fontData.m_fontColour.Blue() / 255.0
226 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
228 //We're done - free up the pool
234 int wxFontDialog::ShowModal()
236 //Start the pool. Required for carbon interaction
237 //(For those curious, the only thing that happens
238 //if you don't do this is a bunch of error
239 //messages about leaks on the console,
240 //with no windows shown or anything).
241 NSAutoreleasePool *thePool;
242 thePool = [[NSAutoreleasePool alloc] init];
244 //Get the shared color and font panel
245 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
246 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
248 //Create and assign the delegates (cocoa event handlers) so
249 //we can tell if a window has closed/open or not
250 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
251 [theFontPanel setDelegate:theFPDelegate];
253 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
254 [theColorPanel setDelegate:theCPDelegate];
257 // Begin the modal loop for the font and color panels
259 // The idea is that we first make the font panel modal,
260 // but if the color panel is opened, unless we stop the
261 // modal loop the color panel opens behind the font panel
262 // with no input acceptable to it - which makes it useless.
264 // So we set up delegates for both the color and font panels,
265 // and the if the font panel opens the color panel, we
266 // stop the modal loop, and start a separate modal loop for
267 // the color panel until the color panel closes, switching
268 // back to the font panel modal loop once it does close.
273 // Start the font panel modal loop
275 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
278 [NSApp runModalSession:session];
280 //If the font panel is closed or the font panel
281 //opened the color panel, break
282 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
285 [NSApp endModalSession:session];
287 //is the color panel open?
288 if ([theCPDelegate isOpen])
291 // Start the color panel modal loop
293 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
296 [NSApp runModalSession:session];
298 //If the color panel is closed, return the font panel modal loop
299 if ([theCPDelegate isClosed])
302 [NSApp endModalSession:session];
304 //If the font panel is still alive (I.E. we broke
305 //out of its modal loop because the color panel was
306 //opened) return the font panel modal loop
307 }while([theFPDelegate isClosed] == NO);
309 //free up the memory for the delegates - we don't need them anymore
310 [theFPDelegate release];
311 [theCPDelegate release];
313 //Get the font the user selected
314 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
316 //Get more information about the user's chosen font
317 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
318 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
319 int theFontSize = (int) [theFont pointSize];
321 //Set the wx font to the appropriate data
322 if(theTraits & NSFixedPitchFontMask)
323 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
325 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
326 m_fontData.m_chosenFont.SetPointSize(theFontSize);
327 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
328 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
329 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
331 //Get the shared color panel along with the chosen color and set the chosen color
332 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
334 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
335 (unsigned char) ([theColor greenComponent] * 255.0),
336 (unsigned char) ([theColor blueComponent] * 255.0));
338 //Friendly debug stuff
340 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"),
346 m_fontData.m_chosenFont.GetFaceName().c_str(),
347 m_fontData.m_chosenFont.GetPointSize(),
348 m_fontData.m_chosenFont.GetStyle(),
349 m_fontData.m_chosenFont.GetWeight(),
350 m_fontData.m_fontColour.Red(),
351 m_fontData.m_fontColour.Green(),
352 m_fontData.m_fontColour.Blue() );
355 //Release the pool, we're done :)
358 //Return ID_OK - there are no "apply" buttons or the like
359 //on either the font or color panel
363 //old api stuff (REMOVE ME)
364 bool wxFontDialog::IsShown() const