]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/fontdlgosx.mm
correcting condition: only interfere in the non-native case
[wxWidgets.git] / src / mac / carbon / fontdlgosx.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 #include "wx/cmndata.h"
21 #include "wx/fontdlg.h"
22 #include "wx/fontutil.h"
23 #include "wx/log.h"
24
25 // ============================================================================
26 // implementation
27 // ============================================================================
28
29 //Mac OSX 10.2+ only
30 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
31
32 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
33
34 // Cocoa headers
35 #include "wx/cocoa/autorelease.h"
36 #include "wx/cocoa/string.h"
37
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>
43
44 // ---------------------------------------------------------------------------
45 // wxWCDelegate - Window Closed delegate
46 // ---------------------------------------------------------------------------
47
48 @interface wxWCDelegate : NSObject
49 {
50 bool m_bIsClosed;
51 }
52
53 // Delegate methods
54 - (id)init;
55 - (BOOL)windowShouldClose:(id)sender;
56 - (BOOL)isClosed;
57 @end // interface wxNSFontPanelDelegate : NSObject
58
59 @implementation wxWCDelegate : NSObject
60
61 - (id)init
62 {
63 [super init];
64 m_bIsClosed = false;
65
66 return self;
67 }
68
69 - (BOOL)windowShouldClose:(id)sender
70 {
71 m_bIsClosed = true;
72
73 [NSApp abortModal];
74 [NSApp stopModal];
75 return YES;
76 }
77
78 - (BOOL)isClosed
79 {
80 return m_bIsClosed;
81 }
82
83 @end // wxNSFontPanelDelegate
84
85 // ---------------------------------------------------------------------------
86 // wxWCODelegate - Window Closed or Open delegate
87 // ---------------------------------------------------------------------------
88
89 @interface wxWCODelegate : NSObject
90 {
91 bool m_bIsClosed;
92 bool m_bIsOpen;
93 }
94
95 // Delegate methods
96 - (id)init;
97 - (BOOL)windowShouldClose:(id)sender;
98 - (void)windowDidUpdate:(NSNotification *)aNotification;
99 - (BOOL)isClosed;
100 - (BOOL)isOpen;
101 @end // interface wxNSFontPanelDelegate : NSObject
102
103 @implementation wxWCODelegate : NSObject
104
105 - (id)init
106 {
107 [super init];
108 m_bIsClosed = false;
109 m_bIsOpen = false;
110
111 return self;
112 }
113
114 - (BOOL)windowShouldClose:(id)sender
115 {
116 m_bIsClosed = true;
117 m_bIsOpen = false;
118
119 [NSApp abortModal];
120 [NSApp stopModal];
121 return YES;
122 }
123
124 - (void)windowDidUpdate:(NSNotification *)aNotification
125 {
126 if (m_bIsOpen == NO)
127 {
128 m_bIsClosed = false;
129 m_bIsOpen = true;
130
131 [NSApp abortModal];
132 [NSApp stopModal];
133 }
134 }
135
136 - (BOOL)isClosed
137 {
138 return m_bIsClosed;
139 }
140
141 - (BOOL)isOpen
142 {
143 return m_bIsOpen;
144 }
145
146 @end // wxNSFontPanelDelegate
147
148 // ---------------------------------------------------------------------------
149 // wxFontDialog
150 // ---------------------------------------------------------------------------
151
152 wxFontDialog::wxFontDialog()
153 {
154 }
155
156 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
157 {
158 Create(parent, data);
159 }
160
161 wxFontDialog::~wxFontDialog()
162 {
163 }
164
165 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
166 {
167 m_fontData = data;
168
169 //
170 // This is the key call - this initializes
171 // events and window stuff for cocoa for carbon
172 // applications.
173 //
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
177 // panel requires.
178 //
179 bool bOK = NSApplicationLoad();
180
181 //autorelease pool - req'd for carbon
182 NSAutoreleasePool *thePool;
183 thePool = [[NSAutoreleasePool alloc] init];
184
185 //Get the initial wx font
186 wxFont& thewxfont = m_fontData.m_initialFont;
187
188 //if the font is valid set the default (selected) font of the
189 //NSFontDialog to that font
190 if (thewxfont.Ok())
191 {
192 NSFontTraitMask theMask = 0;
193
194 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
195 theMask |= NSItalicFontMask;
196
197 if(thewxfont.IsFixedWidth())
198 theMask |= NSFixedPitchFontMask;
199
200 NSFont* theDefaultFont =
201 [[NSFontManager sharedFontManager] fontWithFamily:
202 wxNSStringWithWxString(thewxfont.GetFaceName())
203 traits:theMask
204 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
205 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
206 size: (float)(thewxfont.GetPointSize())
207 ];
208
209 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
210
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];
215
216 }
217
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
223 alpha:1.0]
224 ];
225 else
226 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
227
228 //We're done - free up the pool
229 [thePool release];
230
231 return bOK;
232 }
233
234 int wxFontDialog::ShowModal()
235 {
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];
243
244 //Get the shared color and font panel
245 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
246 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
247
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];
252
253 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
254 [theColorPanel setDelegate:theCPDelegate];
255
256 //
257 // Begin the modal loop for the font and color panels
258 //
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.
263 //
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.
269 //
270 do
271 {
272 //
273 // Start the font panel modal loop
274 //
275 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
276 for (;;)
277 {
278 [NSApp runModalSession:session];
279
280 //If the font panel is closed or the font panel
281 //opened the color panel, break
282 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
283 break;
284 }
285 [NSApp endModalSession:session];
286
287 //is the color panel open?
288 if ([theCPDelegate isOpen])
289 {
290 //
291 // Start the color panel modal loop
292 //
293 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
294 for (;;)
295 {
296 [NSApp runModalSession:session];
297
298 //If the color panel is closed, return the font panel modal loop
299 if ([theCPDelegate isClosed])
300 break;
301 }
302 [NSApp endModalSession:session];
303 }
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);
308
309 //free up the memory for the delegates - we don't need them anymore
310 [theFPDelegate release];
311 [theCPDelegate release];
312
313 //Get the font the user selected
314 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
315
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];
320
321 //Set the wx font to the appropriate data
322 if(theTraits & NSFixedPitchFontMask)
323 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
324
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);
330
331 //Get the shared color panel along with the chosen color and set the chosen color
332 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
333
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));
337
338 //Friendly debug stuff
339 #ifdef FONTDLGDEBUG
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"),
341
342 (float) theFontSize,
343 theFontWeight,
344 theTraits,
345
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() );
353 #endif
354
355 //Release the pool, we're done :)
356 [thePool release];
357
358 //Return ID_OK - there are no "apply" buttons or the like
359 //on either the font or color panel
360 return wxID_OK;
361 }
362
363 //old api stuff (REMOVE ME)
364 bool wxFontDialog::IsShown() const
365 {
366 return false;
367 }
368
369 #endif // 10.2+