]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/fontdlgosx.mm
938613c5f65398929dcfa7e308a15f68dce1498e
[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
30 #include "wx/cocoa/autorelease.h"
31 #include "wx/cocoa/string.h"
32
33 #if wxMAC_USE_EXPERIMENTAL_FONTDIALOG
34
35 #import <Foundation/Foundation.h>
36 #import <AppKit/AppKit.h>
37
38 #include "wx/mac/uma.h"
39 #include "wx/intl.h"
40
41 @interface wxMacFontPanelAccView : NSView
42 {
43 BOOL m_okPressed ;
44 BOOL m_shouldClose ;
45 NSButton* m_cancelButton ;
46 NSButton* m_okButton ;
47 }
48
49 - (IBAction)cancelPressed:(id)sender;
50 - (IBAction)okPressed:(id)sender;
51 - (void)resetFlags;
52 - (BOOL)closedWithOk;
53 - (BOOL)shouldCloseCarbon;
54 - (NSButton*)okButton;
55 @end
56
57 @implementation wxMacFontPanelAccView : NSView
58 - (id)initWithFrame:(NSRect)rectBox
59 {
60 [super initWithFrame:rectBox];
61
62 wxMacCFStringHolder cfOkString( wxT("OK"), wxLocale::GetSystemEncoding() );
63 wxMacCFStringHolder cfCancelString( wxT("Cancel"), wxLocale::GetSystemEncoding() );
64
65 NSRect rectCancel = NSMakeRect( 10.0 , 10.0 , 82 , 24 );
66 NSRect rectOK = NSMakeRect( 100.0 , 10.0 , 82 , 24 );
67 NSView* panel = [[NSView alloc] initWithFrame:rectBox];
68
69 NSButton* cancelButton = [[NSButton alloc] initWithFrame:rectCancel];
70 [cancelButton setTitle:(NSString*)cfCancelString.Detach()];
71 [cancelButton setBezelStyle:NSRoundedBezelStyle];
72 [cancelButton setButtonType:NSMomentaryPushInButton];
73 [cancelButton setAction:@selector(cancelPressed:)];
74 [cancelButton setTarget:self];
75 m_cancelButton = cancelButton ;
76
77 NSButton* okButton = [[NSButton alloc] initWithFrame:rectOK];
78 [okButton setTitle:(NSString*)cfOkString.Detach()];
79 [okButton setBezelStyle:NSRoundedBezelStyle];
80 [okButton setButtonType:NSMomentaryPushInButton];
81 [okButton setAction:@selector(okPressed:)];
82 [okButton setTarget:self];
83 // doesn't help either, the button is not highlighted after a color dialog has been used
84 // [okButton setKeyEquivalent:@"\r"];
85 m_okButton = okButton ;
86
87
88 [self addSubview:cancelButton];
89 [self addSubview:okButton];
90
91 [self resetFlags];
92 return self;
93 }
94
95 - (void)resetFlags
96 {
97 m_okPressed = NO ;
98 m_shouldClose = NO ;
99 }
100
101 - (IBAction)cancelPressed:(id)sender
102 {
103 m_shouldClose = YES ;
104 [NSApp stopModal];
105 }
106
107 - (IBAction)okPressed:(id)sender
108 {
109 m_okPressed = YES ;
110 m_shouldClose = YES ;
111 [NSApp stopModal];
112 }
113
114 -(BOOL)closedWithOk
115 {
116 return m_okPressed ;
117 }
118
119 -(BOOL)shouldCloseCarbon
120 {
121 return m_shouldClose ;
122 }
123
124 -(NSButton*)okButton
125 {
126 return m_okButton ;
127 }
128 @end
129
130
131 extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
132
133 int RunMixedFontDialog(wxFontDialog* dialog)
134 {
135 int retval = wxID_CANCEL ;
136
137 bool cocoaLoaded = NSApplicationLoad();
138 wxASSERT_MSG(cocoaLoaded,wxT("Couldn't load Cocoa in Carbon Environment")) ;
139
140 wxAutoNSAutoreleasePool pool;
141
142 // setting up the ok/cancel buttons
143 NSFontPanel* fontPanel = [NSFontPanel sharedFontPanel] ;
144
145 // adjust modality for carbon environment
146 WindowRef carbonWindowRef = (WindowRef)[fontPanel windowRef] ;
147 SetWindowModality(carbonWindowRef, kWindowModalityAppModal , 0) ;
148 SetWindowGroup(carbonWindowRef , GetWindowGroupOfClass(kMovableModalWindowClass));
149
150 [fontPanel setFloatingPanel:NO] ;
151
152 wxMacFontPanelAccView* accessoryView = (wxMacFontPanelAccView*) [fontPanel accessoryView] ;
153 if ( accessoryView == nil)
154 {
155 NSRect rectBox = NSMakeRect( 0 , 0 , 192 , 40 );
156 accessoryView = [[wxMacFontPanelAccView alloc] initWithFrame:rectBox];
157 [fontPanel setAccessoryView:accessoryView];
158
159 [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
160 }
161
162 [accessoryView resetFlags];
163
164 NSModalSession session = [NSApp beginModalSessionForWindow:fontPanel];
165
166 [NSApp runModalSession:session];
167
168 [NSApp endModalSession:session];
169
170 if( FPIsFontPanelVisible())
171 FPShowHideFontPanel() ;
172
173 if ( [accessoryView closedWithOk])
174 {
175 retval = wxID_OK ;
176 }
177
178
179 return retval ;
180 }
181
182 #else
183
184 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
185
186 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
187
188 // Cocoa headers
189
190 #import <AppKit/NSFont.h>
191 #import <AppKit/NSFontManager.h>
192 #import <AppKit/NSFontPanel.h>
193 #import <AppKit/NSColor.h>
194 #import <AppKit/NSColorPanel.h>
195
196 // ---------------------------------------------------------------------------
197 // wxWCDelegate - Window Closed delegate
198 // ---------------------------------------------------------------------------
199
200 @interface wxWCDelegate : NSObject
201 {
202 bool m_bIsClosed;
203 }
204
205 // Delegate methods
206 - (id)init;
207 - (BOOL)windowShouldClose:(id)sender;
208 - (BOOL)isClosed;
209 @end // interface wxNSFontPanelDelegate : NSObject
210
211 @implementation wxWCDelegate : NSObject
212
213 - (id)init
214 {
215 [super init];
216 m_bIsClosed = false;
217
218 return self;
219 }
220
221 - (BOOL)windowShouldClose:(id)sender
222 {
223 m_bIsClosed = true;
224
225 [NSApp abortModal];
226 [NSApp stopModal];
227 return YES;
228 }
229
230 - (BOOL)isClosed
231 {
232 return m_bIsClosed;
233 }
234
235 @end // wxNSFontPanelDelegate
236
237 // ---------------------------------------------------------------------------
238 // wxWCODelegate - Window Closed or Open delegate
239 // ---------------------------------------------------------------------------
240
241 @interface wxWCODelegate : NSObject
242 {
243 bool m_bIsClosed;
244 bool m_bIsOpen;
245 }
246
247 // Delegate methods
248 - (id)init;
249 - (BOOL)windowShouldClose:(id)sender;
250 - (void)windowDidUpdate:(NSNotification *)aNotification;
251 - (BOOL)isClosed;
252 - (BOOL)isOpen;
253 @end // interface wxNSFontPanelDelegate : NSObject
254
255 @implementation wxWCODelegate : NSObject
256
257 - (id)init
258 {
259 [super init];
260 m_bIsClosed = false;
261 m_bIsOpen = false;
262
263 return self;
264 }
265
266 - (BOOL)windowShouldClose:(id)sender
267 {
268 m_bIsClosed = true;
269 m_bIsOpen = false;
270
271 [NSApp abortModal];
272 [NSApp stopModal];
273 return YES;
274 }
275
276 - (void)windowDidUpdate:(NSNotification *)aNotification
277 {
278 if (m_bIsOpen == NO)
279 {
280 m_bIsClosed = false;
281 m_bIsOpen = true;
282
283 [NSApp abortModal];
284 [NSApp stopModal];
285 }
286 }
287
288 - (BOOL)isClosed
289 {
290 return m_bIsClosed;
291 }
292
293 - (BOOL)isOpen
294 {
295 return m_bIsOpen;
296 }
297
298 @end // wxNSFontPanelDelegate
299
300 // ---------------------------------------------------------------------------
301 // wxFontDialog
302 // ---------------------------------------------------------------------------
303
304 wxFontDialog::wxFontDialog()
305 {
306 }
307
308 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
309 {
310 Create(parent, data);
311 }
312
313 wxFontDialog::~wxFontDialog()
314 {
315 }
316
317 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
318 {
319 m_fontData = data;
320
321 //
322 // This is the key call - this initializes
323 // events and window stuff for cocoa for carbon
324 // applications.
325 //
326 // This is also the only call here that is
327 // 10.2+ specific (the rest is OSX only),
328 // which, ironically, the carbon font
329 // panel requires.
330 //
331 bool bOK = NSApplicationLoad();
332
333 //autorelease pool - req'd for carbon
334 NSAutoreleasePool *thePool;
335 thePool = [[NSAutoreleasePool alloc] init];
336
337 //Get the initial wx font
338 wxFont& thewxfont = m_fontData.m_initialFont;
339
340 //if the font is valid set the default (selected) font of the
341 //NSFontDialog to that font
342 if (thewxfont.Ok())
343 {
344 NSFontTraitMask theMask = 0;
345
346 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
347 theMask |= NSItalicFontMask;
348
349 if(thewxfont.IsFixedWidth())
350 theMask |= NSFixedPitchFontMask;
351
352 NSFont* theDefaultFont =
353 [[NSFontManager sharedFontManager] fontWithFamily:
354 wxNSStringWithWxString(thewxfont.GetFaceName())
355 traits:theMask
356 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
357 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
358 size: (float)(thewxfont.GetPointSize())
359 ];
360
361 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
362
363 //Apple docs say to call NSFontManager::setSelectedFont
364 //However, 10.3 doesn't seem to create the font panel
365 //is this is done, so create it ourselves
366 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
367
368 }
369
370 if(m_fontData.m_fontColour.Ok())
371 [[NSColorPanel sharedColorPanel] setColor:
372 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
373 green:m_fontData.m_fontColour.Green() / 255.0
374 blue:m_fontData.m_fontColour.Blue() / 255.0
375 alpha:1.0]
376 ];
377 else
378 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
379
380 //We're done - free up the pool
381 [thePool release];
382
383 return bOK;
384 }
385
386 int wxFontDialog::ShowModal()
387 {
388 //Start the pool. Required for carbon interaction
389 //(For those curious, the only thing that happens
390 //if you don't do this is a bunch of error
391 //messages about leaks on the console,
392 //with no windows shown or anything).
393 NSAutoreleasePool *thePool;
394 thePool = [[NSAutoreleasePool alloc] init];
395
396 //Get the shared color and font panel
397 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
398 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
399
400 //Create and assign the delegates (cocoa event handlers) so
401 //we can tell if a window has closed/open or not
402 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
403 [theFontPanel setDelegate:theFPDelegate];
404
405 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
406 [theColorPanel setDelegate:theCPDelegate];
407
408 //
409 // Begin the modal loop for the font and color panels
410 //
411 // The idea is that we first make the font panel modal,
412 // but if the color panel is opened, unless we stop the
413 // modal loop the color panel opens behind the font panel
414 // with no input acceptable to it - which makes it useless.
415 //
416 // So we set up delegates for both the color and font panels,
417 // and the if the font panel opens the color panel, we
418 // stop the modal loop, and start a separate modal loop for
419 // the color panel until the color panel closes, switching
420 // back to the font panel modal loop once it does close.
421 //
422 do
423 {
424 //
425 // Start the font panel modal loop
426 //
427 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
428 for (;;)
429 {
430 [NSApp runModalSession:session];
431
432 //If the font panel is closed or the font panel
433 //opened the color panel, break
434 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
435 break;
436 }
437 [NSApp endModalSession:session];
438
439 //is the color panel open?
440 if ([theCPDelegate isOpen])
441 {
442 //
443 // Start the color panel modal loop
444 //
445 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
446 for (;;)
447 {
448 [NSApp runModalSession:session];
449
450 //If the color panel is closed, return the font panel modal loop
451 if ([theCPDelegate isClosed])
452 break;
453 }
454 [NSApp endModalSession:session];
455 }
456 //If the font panel is still alive (I.E. we broke
457 //out of its modal loop because the color panel was
458 //opened) return the font panel modal loop
459 }while([theFPDelegate isClosed] == NO);
460
461 //free up the memory for the delegates - we don't need them anymore
462 [theFPDelegate release];
463 [theCPDelegate release];
464
465 //Get the font the user selected
466 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
467
468 //Get more information about the user's chosen font
469 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
470 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
471 int theFontSize = (int) [theFont pointSize];
472
473 //Set the wx font to the appropriate data
474 if(theTraits & NSFixedPitchFontMask)
475 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
476
477 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
478 m_fontData.m_chosenFont.SetPointSize(theFontSize);
479 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
480 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
481 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
482
483 //Get the shared color panel along with the chosen color and set the chosen color
484 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
485
486 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
487 (unsigned char) ([theColor greenComponent] * 255.0),
488 (unsigned char) ([theColor blueComponent] * 255.0));
489
490 //Friendly debug stuff
491 #ifdef FONTDLGDEBUG
492 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"),
493
494 (float) theFontSize,
495 theFontWeight,
496 theTraits,
497
498 m_fontData.m_chosenFont.GetFaceName().c_str(),
499 m_fontData.m_chosenFont.GetPointSize(),
500 m_fontData.m_chosenFont.GetStyle(),
501 m_fontData.m_chosenFont.GetWeight(),
502 m_fontData.m_fontColour.Red(),
503 m_fontData.m_fontColour.Green(),
504 m_fontData.m_fontColour.Blue() );
505 #endif
506
507 //Release the pool, we're done :)
508 [thePool release];
509
510 //Return ID_OK - there are no "apply" buttons or the like
511 //on either the font or color panel
512 return wxID_OK;
513 }
514
515 //old api stuff (REMOVE ME)
516 bool wxFontDialog::IsShown() const
517 {
518 return false;
519 }
520
521 #endif // 10.2+
522
523 #endif