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