1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFontDialog class.
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "fontdlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/cmndata.h"
33 #include "wx/fontdlg.h"
34 #include "wx/fontutil.h"
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
42 // ============================================================================
44 // ============================================================================
47 #if defined( __WXMAC_OSX__ ) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
50 #include "wx/cocoa/autorelease.h"
51 #include "wx/cocoa/string.h"
53 #import <AppKit/NSFont.h>
54 #import <AppKit/NSFontManager.h>
55 #import <AppKit/NSFontPanel.h>
56 #import <AppKit/NSColor.h>
57 #import <AppKit/NSColorPanel.h>
59 // ---------------------------------------------------------------------------
60 // wxWCDelegate - Window Closed delegate
61 // ---------------------------------------------------------------------------
63 @interface wxWCDelegate : NSObject
70 - (BOOL)windowShouldClose:(id)sender;
72 @end // interface wxNSFontPanelDelegate : NSObject
74 @implementation wxWCDelegate : NSObject
84 - (BOOL)windowShouldClose:(id)sender
98 @end // wxNSFontPanelDelegate
100 // ---------------------------------------------------------------------------
101 // wxWCODelegate - Window Closed or Open delegate
102 // ---------------------------------------------------------------------------
104 @interface wxWCODelegate : NSObject
112 - (BOOL)windowShouldClose:(id)sender;
113 - (void)windowDidUpdate:(NSNotification *)aNotification;
116 @end // interface wxNSFontPanelDelegate : NSObject
118 @implementation wxWCODelegate : NSObject
129 - (BOOL)windowShouldClose:(id)sender
139 - (void)windowDidUpdate:(NSNotification *)aNotification
161 @end // wxNSFontPanelDelegate
163 // ---------------------------------------------------------------------------
165 // ---------------------------------------------------------------------------
167 wxFontDialog::wxFontDialog()
171 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
173 Create(parent, data);
176 wxFontDialog::~wxFontDialog()
180 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
185 // This is the key call - this initializes
186 // events and window stuff for cocoa for carbon
189 // This is also the only call here that is
190 // 10.2+ specific (the rest is OSX only),
191 // which, ironically, the carbon font
194 bool bOK = NSApplicationLoad();
196 //autorelease pool - req'd for carbon
197 NSAutoreleasePool *thePool;
198 thePool = [[NSAutoreleasePool alloc] init];
200 //Get the initial wx font
201 wxFont& thewxfont = m_fontData.m_initialFont;
203 //if the font is valid set the default (selected) font of the
204 //NSFontDialog to that font
207 NSFontTraitMask theMask = 0;
209 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
210 theMask |= NSItalicFontMask;
212 if(thewxfont.IsFixedWidth())
213 theMask |= NSFixedPitchFontMask;
215 NSFont* theDefaultFont =
216 [[NSFontManager sharedFontManager] fontWithFamily:
217 wxNSStringWithWxString(thewxfont.GetFaceName())
219 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
220 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
221 size: (float)(thewxfont.GetPointSize())
224 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
226 //set the initial font of the NSFontPanel
227 //(the font manager calls the appropriate NSFontPanel method)
228 [[NSFontManager sharedFontManager] setSelectedFont:theDefaultFont isMultiple:NO];
232 if([NSColorPanel sharedColorPanelExists] == NO)
234 //The color panel has an annoying tendancy to start
235 //out at a WHITE color - so when the user
236 //chooses his first font he would probably be in
237 //for an unpleasent surprise if we didn't setup
238 //the color panel to use black :)
239 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
242 //We're done - free up the pool
248 int wxFontDialog::ShowModal()
250 //Start the pool. Required for carbon interaction
251 //(For those curious, the only thing that happens
252 //if you don't do this is a bunch of error
253 //messages about leaks on the console,
254 //with no windows shown or anything).
255 NSAutoreleasePool *thePool;
256 thePool = [[NSAutoreleasePool alloc] init];
258 //Get the shared color and font panel
259 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
260 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
262 //Create and assign the delegates (cocoa event handlers) so
263 //we can tell if a window has closed/open or not
264 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
265 [theFontPanel setDelegate:theFPDelegate];
267 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
268 [theColorPanel setDelegate:theCPDelegate];
271 // Begin the modal loop for the font and color panels
273 // The idea is that we first make the font panel modal,
274 // but if the color panel is opened, unless we stop the
275 // modal loop the color panel opens behind the font panel
276 // with no input acceptable to it - which makes it useless.
278 // So we set up delegates for both the color and font panels,
279 // and the if the font panel opens the color panel, we
280 // stop the modal loop, and start a seperate modal loop for
281 // the color panel until the color panel closes, switching
282 // back to the font panel modal loop once it does close.
287 // Start the font panel modal loop
289 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
292 [NSApp runModalSession:session];
294 //If the font panel is closed or the font panel
295 //opened the color panel, break
296 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
299 [NSApp endModalSession:session];
301 //is the color panel open?
302 if ([theCPDelegate isOpen])
305 // Start the color panel modal loop
307 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
310 [NSApp runModalSession:session];
312 //If the color panel is closed, return the font panel modal loop
313 if ([theCPDelegate isClosed])
316 [NSApp endModalSession:session];
318 //If the font panel is still alive (I.E. we broke
319 //out of its modal loop because the color panel was
320 //opened) return the font panel modal loop
321 }while([theFPDelegate isClosed] == NO);
323 //free up the memory for the delegates - we don't need them anymore
324 [theFPDelegate release];
325 [theCPDelegate release];
327 //Get the font the user selected
328 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
330 //Get more information about the user's chosen font
331 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
332 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
333 int theFontSize = (int) [theFont pointSize];
335 //Set the wx font to the appropriate data
336 if(theTraits & NSFixedPitchFontMask)
337 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
339 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
340 m_fontData.m_chosenFont.SetPointSize(theFontSize);
341 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
342 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
343 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
345 //Get the shared color panel along with the chosen color and set the chosen color
346 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
348 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
349 (unsigned char) ([theColor greenComponent] * 255.0),
350 (unsigned char) ([theColor blueComponent] * 255.0));
352 //Friendly debug stuff
354 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"),
360 m_fontData.m_chosenFont.GetFaceName().c_str(),
361 m_fontData.m_chosenFont.GetPointSize(),
362 m_fontData.m_chosenFont.GetStyle(),
363 m_fontData.m_chosenFont.GetWeight(),
364 m_fontData.m_fontColour.Red(),
365 m_fontData.m_fontColour.Green(),
366 m_fontData.m_fontColour.Blue() );
369 //Release the pool, we're done :)
372 //Return ID_OK - there are no "apply" buttons or the like
373 //on either the font or color panel
377 //old api stuff (REMOVE ME)
378 bool wxFontDialog::IsShown() const
386 // ---------------------------------------------------------------------------
387 // wxFontDialog stub for mac OS's without a native font dialog
388 // ---------------------------------------------------------------------------
390 wxFontDialog::wxFontDialog()
392 m_dialogParent = NULL;
395 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
397 Create(parent, data);
400 void wxFontDialog::SetData(wxFontData& fontdata)
402 m_fontData = fontdata;
405 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
407 m_dialogParent = parent;
411 // TODO: you may need to do dialog creation here, unless it's
412 // done in ShowModal.
416 bool wxFontDialog::IsShown() const
421 int wxFontDialog::ShowModal()
423 // TODO: show (maybe create) the dialog