]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/fontdlg.mm
native wxFontDialog for carbon, second shot
[wxWidgets.git] / src / mac / carbon / fontdlg.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: fontdlg.cpp
3 // Purpose: wxFontDialog class.
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 2004-10-03
7 // RCS-ID: $Id$
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "fontdlg.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/cmndata.h"
33 #include "wx/fontdlg.h"
34 #include "wx/fontutil.h"
35 #include "wx/log.h"
36 #endif
37
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
40 #endif
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 //Mac OSX 10.2+ only
47 #if defined( __WXMAC_OSX__ ) && ( MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2 )
48
49 // Cocoa headers
50 #include "wx/cocoa/autorelease.h"
51 #include "wx/cocoa/string.h"
52
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>
58
59 // ---------------------------------------------------------------------------
60 // wxWCDelegate - Window Closed delegate
61 // ---------------------------------------------------------------------------
62
63 @interface wxWCDelegate : NSObject
64 {
65 bool m_bIsClosed;
66 }
67
68 // Delegate methods
69 - (id)init;
70 - (BOOL)windowShouldClose:(id)sender;
71 - (BOOL)isClosed;
72 @end // interface wxNSFontPanelDelegate : NSObject
73
74 @implementation wxWCDelegate : NSObject
75
76 - (id)init
77 {
78 [super init];
79 m_bIsClosed = false;
80
81 return self;
82 }
83
84 - (BOOL)windowShouldClose:(id)sender
85 {
86 m_bIsClosed = true;
87
88 [NSApp abortModal];
89 [NSApp stopModal];
90 return YES;
91 }
92
93 - (BOOL)isClosed
94 {
95 return m_bIsClosed;
96 }
97
98 @end // wxNSFontPanelDelegate
99
100 // ---------------------------------------------------------------------------
101 // wxWCODelegate - Window Closed or Open delegate
102 // ---------------------------------------------------------------------------
103
104 @interface wxWCODelegate : NSObject
105 {
106 bool m_bIsClosed;
107 bool m_bIsOpen;
108 }
109
110 // Delegate methods
111 - (id)init;
112 - (BOOL)windowShouldClose:(id)sender;
113 - (void)windowDidUpdate:(NSNotification *)aNotification;
114 - (BOOL)isClosed;
115 - (BOOL)isOpen;
116 @end // interface wxNSFontPanelDelegate : NSObject
117
118 @implementation wxWCODelegate : NSObject
119
120 - (id)init
121 {
122 [super init];
123 m_bIsClosed = false;
124 m_bIsOpen = false;
125
126 return self;
127 }
128
129 - (BOOL)windowShouldClose:(id)sender
130 {
131 m_bIsClosed = true;
132 m_bIsOpen = false;
133
134 [NSApp abortModal];
135 [NSApp stopModal];
136 return YES;
137 }
138
139 - (void)windowDidUpdate:(NSNotification *)aNotification
140 {
141 if (m_bIsOpen == NO)
142 {
143 m_bIsClosed = false;
144 m_bIsOpen = true;
145
146 [NSApp abortModal];
147 [NSApp stopModal];
148 }
149 }
150
151 - (BOOL)isClosed
152 {
153 return m_bIsClosed;
154 }
155
156 - (BOOL)isOpen
157 {
158 return m_bIsOpen;
159 }
160
161 @end // wxNSFontPanelDelegate
162
163 // ---------------------------------------------------------------------------
164 // wxFontDialog
165 // ---------------------------------------------------------------------------
166
167 wxFontDialog::wxFontDialog()
168 {
169 }
170
171 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
172 {
173 Create(parent, data);
174 }
175
176 wxFontDialog::~wxFontDialog()
177 {
178 }
179
180 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
181 {
182 m_fontData = data;
183
184 //
185 // This is the key call - this initializes
186 // events and window stuff for cocoa for carbon
187 // applications.
188 //
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
192 // panel requires.
193 //
194 bool bOK = NSApplicationLoad();
195
196 //autorelease pool - req'd for carbon
197 NSAutoreleasePool *thePool;
198 thePool = [[NSAutoreleasePool alloc] init];
199
200 //Get the initial wx font
201 wxFont& thewxfont = m_fontData.m_initialFont;
202
203 //if the font is valid set the default (selected) font of the
204 //NSFontDialog to that font
205 if (thewxfont.Ok())
206 {
207 NSFontTraitMask theMask = 0;
208
209 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
210 theMask |= NSItalicFontMask;
211
212 if(thewxfont.IsFixedWidth())
213 theMask |= NSFixedPitchFontMask;
214
215 NSFont* theDefaultFont =
216 [[NSFontManager sharedFontManager] fontWithFamily:
217 wxNSStringWithWxString(thewxfont.GetFaceName())
218 traits:theMask
219 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
220 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
221 size: (float)(thewxfont.GetPointSize())
222 ];
223
224 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
225
226 //set the initial font of the NSFontPanel
227 //(the font manager calls the appropriate NSFontPanel method)
228 [[NSFontManager sharedFontManager] setSelectedFont:theDefaultFont isMultiple:NO];
229
230 }
231
232 if([NSColorPanel sharedColorPanelExists] == NO)
233 {
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]];
240 }
241
242 //We're done - free up the pool
243 [thePool release];
244
245 return bOK;
246 }
247
248 int wxFontDialog::ShowModal()
249 {
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];
257
258 //Get the shared color and font panel
259 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
260 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
261
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];
266
267 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
268 [theColorPanel setDelegate:theCPDelegate];
269
270 //
271 // Begin the modal loop for the font and color panels
272 //
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.
277 //
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.
283 //
284 do
285 {
286 //
287 // Start the font panel modal loop
288 //
289 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
290 for (;;)
291 {
292 [NSApp runModalSession:session];
293
294 //If the font panel is closed or the font panel
295 //opened the color panel, break
296 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
297 break;
298 }
299 [NSApp endModalSession:session];
300
301 //is the color panel open?
302 if ([theCPDelegate isOpen])
303 {
304 //
305 // Start the color panel modal loop
306 //
307 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
308 for (;;)
309 {
310 [NSApp runModalSession:session];
311
312 //If the color panel is closed, return the font panel modal loop
313 if ([theCPDelegate isClosed])
314 break;
315 }
316 [NSApp endModalSession:session];
317 }
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);
322
323 //free up the memory for the delegates - we don't need them anymore
324 [theFPDelegate release];
325 [theCPDelegate release];
326
327 //Get the font the user selected
328 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
329
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];
334
335 //Set the wx font to the appropriate data
336 if(theTraits & NSFixedPitchFontMask)
337 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
338
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);
344
345 //Get the shared color panel along with the chosen color and set the chosen color
346 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
347
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));
351
352 //Friendly debug stuff
353 #ifdef FONTDLGDEBUG
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"),
355
356 (float) theFontSize,
357 theFontWeight,
358 theTraits,
359
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() );
367 #endif
368
369 //Release the pool, we're done :)
370 [thePool release];
371
372 //Return ID_OK - there are no "apply" buttons or the like
373 //on either the font or color panel
374 return wxID_OK;
375 }
376
377 //old api stuff (REMOVE ME)
378 bool wxFontDialog::IsShown() const
379 {
380 return false;
381 }
382
383 #else
384 //10.2+ only
385
386 // ---------------------------------------------------------------------------
387 // wxFontDialog stub for mac OS's without a native font dialog
388 // ---------------------------------------------------------------------------
389
390 wxFontDialog::wxFontDialog()
391 {
392 m_dialogParent = NULL;
393 }
394
395 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
396 {
397 Create(parent, data);
398 }
399
400 void wxFontDialog::SetData(wxFontData& fontdata)
401 {
402 m_fontData = fontdata;
403 }
404
405 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
406 {
407 m_dialogParent = parent;
408
409 m_fontData = data;
410
411 // TODO: you may need to do dialog creation here, unless it's
412 // done in ShowModal.
413 return TRUE;
414 }
415
416 bool wxFontDialog::IsShown() const
417 {
418 return false;
419 }
420
421 int wxFontDialog::ShowModal()
422 {
423 // TODO: show (maybe create) the dialog
424 return wxID_CANCEL;
425 }
426
427 #endif // 10.2+