]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/fontdlgosx.mm
fixes #13557
[wxWidgets.git] / src / osx / carbon / fontdlgosx.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/fontdlgosx.mm
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 [accessoryView release];
171
172 [fontPanel setDefaultButtonCell:[[accessoryView okButton] cell]] ;
173 }
174
175 [accessoryView resetFlags];
176 #if wxOSX_USE_COCOA
177 wxFont font = *wxNORMAL_FONT ;
178 if ( fontdata.m_initialFont.IsOk() )
179 {
180 font = fontdata.m_initialFont ;
181 }
182
183 [[NSFontPanel sharedFontPanel] setPanelFont: font.OSXGetNSFont() isMultiple:NO];
184
185 if(fontdata.m_fontColour.IsOk())
186 [[NSColorPanel sharedColorPanel] setColor:
187 [NSColor colorWithCalibratedRed:fontdata.m_fontColour.Red() / 255.0
188 green:fontdata.m_fontColour.Green() / 255.0
189 blue:fontdata.m_fontColour.Blue() / 255.0
190 alpha:1.0]
191 ];
192 else
193 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
194 #endif
195
196 [NSApp runModalForWindow:fontPanel];
197
198 // if we don't reenable it, FPShowHideFontPanel does not work
199 [[fontPanel standardWindowButton:NSWindowCloseButton] setEnabled:YES] ;
200 #if wxOSX_USE_CARBON
201 if( FPIsFontPanelVisible())
202 FPShowHideFontPanel() ;
203 #else
204 [fontPanel close];
205 #endif
206
207 if ( [accessoryView closedWithOk])
208 {
209 #if wxOSX_USE_COCOA
210 NSFont* theFont = [fontPanel panelConvertFont:[NSFont userFontOfSize:0]];
211
212 fontdata.m_chosenFont = wxFont( theFont );
213
214 //Get the shared color panel along with the chosen color and set the chosen color
215 NSColor* theColor = [[[NSColorPanel sharedColorPanel] color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
216
217 fontdata.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
218 (unsigned char) ([theColor greenComponent] * 255.0),
219 (unsigned char) ([theColor blueComponent] * 255.0));
220 #endif
221 retval = wxID_OK ;
222 }
223 [fontPanel setAccessoryView:nil];
224
225 return retval ;
226 }
227
228 #else
229
230 #if USE_NATIVE_FONT_DIALOG_FOR_MACOSX
231
232 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
233
234 // Cocoa headers
235
236 #import <AppKit/NSFont.h>
237 #import <AppKit/NSFontManager.h>
238 #import <AppKit/NSFontPanel.h>
239 #import <AppKit/NSColor.h>
240 #import <AppKit/NSColorPanel.h>
241
242 // ---------------------------------------------------------------------------
243 // wxWCDelegate - Window Closed delegate
244 // ---------------------------------------------------------------------------
245
246 @interface wxWCDelegate : NSObject
247 {
248 bool m_bIsClosed;
249 }
250
251 // Delegate methods
252 - (id)init;
253 - (BOOL)windowShouldClose:(id)sender;
254 - (BOOL)isClosed;
255 @end // interface wxNSFontPanelDelegate : NSObject
256
257 @implementation wxWCDelegate : NSObject
258
259 - (id)init
260 {
261 [super init];
262 m_bIsClosed = false;
263
264 return self;
265 }
266
267 - (BOOL)windowShouldClose:(id)sender
268 {
269 m_bIsClosed = true;
270
271 [NSApp abortModal];
272 [NSApp stopModal];
273 return YES;
274 }
275
276 - (BOOL)isClosed
277 {
278 return m_bIsClosed;
279 }
280
281 @end // wxNSFontPanelDelegate
282
283 // ---------------------------------------------------------------------------
284 // wxWCODelegate - Window Closed or Open delegate
285 // ---------------------------------------------------------------------------
286
287 @interface wxWCODelegate : NSObject
288 {
289 bool m_bIsClosed;
290 bool m_bIsOpen;
291 }
292
293 // Delegate methods
294 - (id)init;
295 - (BOOL)windowShouldClose:(id)sender;
296 - (void)windowDidUpdate:(NSNotification *)aNotification;
297 - (BOOL)isClosed;
298 - (BOOL)isOpen;
299 @end // interface wxNSFontPanelDelegate : NSObject
300
301 @implementation wxWCODelegate : NSObject
302
303 - (id)init
304 {
305 [super init];
306 m_bIsClosed = false;
307 m_bIsOpen = false;
308
309 return self;
310 }
311
312 - (BOOL)windowShouldClose:(id)sender
313 {
314 m_bIsClosed = true;
315 m_bIsOpen = false;
316
317 [NSApp abortModal];
318 [NSApp stopModal];
319 return YES;
320 }
321
322 - (void)windowDidUpdate:(NSNotification *)aNotification
323 {
324 if (m_bIsOpen == NO)
325 {
326 m_bIsClosed = false;
327 m_bIsOpen = true;
328
329 [NSApp abortModal];
330 [NSApp stopModal];
331 }
332 }
333
334 - (BOOL)isClosed
335 {
336 return m_bIsClosed;
337 }
338
339 - (BOOL)isOpen
340 {
341 return m_bIsOpen;
342 }
343
344 @end // wxNSFontPanelDelegate
345
346 // ---------------------------------------------------------------------------
347 // wxFontDialog
348 // ---------------------------------------------------------------------------
349
350 wxFontDialog::wxFontDialog()
351 {
352 }
353
354 wxFontDialog::wxFontDialog(wxWindow *parent)
355 {
356 Create(parent);
357 }
358
359 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
360 {
361 Create(parent, data);
362 }
363
364 wxFontDialog::~wxFontDialog()
365 {
366 }
367
368 bool wxFontDialog::Create(wxWindow *parent)
369 {
370 return Create(parent);
371 }
372
373 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
374 {
375 m_fontData = data;
376
377 return Create(parent);
378 }
379
380 bool wxFontDialog::Create(wxWindow *parent)
381 {
382 //autorelease pool - req'd for carbon
383 NSAutoreleasePool *thePool;
384 thePool = [[NSAutoreleasePool alloc] init];
385
386 //Get the initial wx font
387 wxFont& thewxfont = m_fontData.m_initialFont;
388
389 //if the font is valid set the default (selected) font of the
390 //NSFontDialog to that font
391 if (thewxfont.IsOk())
392 {
393 NSFontTraitMask theMask = 0;
394
395 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
396 theMask |= NSItalicFontMask;
397
398 if(thewxfont.IsFixedWidth())
399 theMask |= NSFixedPitchFontMask;
400
401 NSFont* theDefaultFont =
402 [[NSFontManager sharedFontManager] fontWithFamily:
403 wxNSStringWithWxString(thewxfont.GetFaceName())
404 traits:theMask
405 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
406 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
407 size: (float)(thewxfont.GetPointSize())
408 ];
409
410 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
411
412 //Apple docs say to call NSFontManager::setSelectedFont
413 //However, 10.3 doesn't seem to create the font panel
414 //is this is done, so create it ourselves
415 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
416
417 }
418
419 if(m_fontData.m_fontColour.IsOk())
420 [[NSColorPanel sharedColorPanel] setColor:
421 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
422 green:m_fontData.m_fontColour.Green() / 255.0
423 blue:m_fontData.m_fontColour.Blue() / 255.0
424 alpha:1.0]
425 ];
426 else
427 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
428
429 //We're done - free up the pool
430 [thePool release];
431
432 return true;
433 }
434
435 int wxFontDialog::ShowModal()
436 {
437 //Start the pool. Required for carbon interaction
438 //(For those curious, the only thing that happens
439 //if you don't do this is a bunch of error
440 //messages about leaks on the console,
441 //with no windows shown or anything).
442 NSAutoreleasePool *thePool;
443 thePool = [[NSAutoreleasePool alloc] init];
444
445 //Get the shared color and font panel
446 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
447 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
448
449 //Create and assign the delegates (cocoa event handlers) so
450 //we can tell if a window has closed/open or not
451 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
452 [theFontPanel setDelegate:theFPDelegate];
453
454 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
455 [theColorPanel setDelegate:theCPDelegate];
456
457 //
458 // Begin the modal loop for the font and color panels
459 //
460 // The idea is that we first make the font panel modal,
461 // but if the color panel is opened, unless we stop the
462 // modal loop the color panel opens behind the font panel
463 // with no input acceptable to it - which makes it useless.
464 //
465 // So we set up delegates for both the color and font panels,
466 // and the if the font panel opens the color panel, we
467 // stop the modal loop, and start a separate modal loop for
468 // the color panel until the color panel closes, switching
469 // back to the font panel modal loop once it does close.
470 //
471 wxDialog::OSXBeginModalDialog();
472 do
473 {
474 //
475 // Start the font panel modal loop
476 //
477 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
478 for (;;)
479 {
480 [NSApp runModalSession:session];
481
482 //If the font panel is closed or the font panel
483 //opened the color panel, break
484 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
485 break;
486 }
487 [NSApp endModalSession:session];
488
489 //is the color panel open?
490 if ([theCPDelegate isOpen])
491 {
492 //
493 // Start the color panel modal loop
494 //
495 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
496 for (;;)
497 {
498 [NSApp runModalSession:session];
499
500 //If the color panel is closed, return the font panel modal loop
501 if ([theCPDelegate isClosed])
502 break;
503 }
504 [NSApp endModalSession:session];
505 }
506 //If the font panel is still alive (I.E. we broke
507 //out of its modal loop because the color panel was
508 //opened) return the font panel modal loop
509 }while([theFPDelegate isClosed] == NO);
510 wxDialog::OSXEndModalDialog();
511
512 //free up the memory for the delegates - we don't need them anymore
513 [theFPDelegate release];
514 [theCPDelegate release];
515
516 //Get the font the user selected
517 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
518
519 //Get more information about the user's chosen font
520 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
521 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
522 int theFontSize = (int) [theFont pointSize];
523
524 //Set the wx font to the appropriate data
525 if(theTraits & NSFixedPitchFontMask)
526 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
527
528 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
529 m_fontData.m_chosenFont.SetPointSize(theFontSize);
530 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
531 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
532 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
533
534 //Get the shared color panel along with the chosen color and set the chosen color
535 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
536
537 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
538 (unsigned char) ([theColor greenComponent] * 255.0),
539 (unsigned char) ([theColor blueComponent] * 255.0));
540
541 //Friendly debug stuff
542 #ifdef FONTDLGDEBUG
543 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"),
544
545 (float) theFontSize,
546 theFontWeight,
547 theTraits,
548
549 m_fontData.m_chosenFont.GetFaceName().c_str(),
550 m_fontData.m_chosenFont.GetPointSize(),
551 m_fontData.m_chosenFont.GetStyle(),
552 m_fontData.m_chosenFont.GetWeight(),
553 m_fontData.m_fontColour.Red(),
554 m_fontData.m_fontColour.Green(),
555 m_fontData.m_fontColour.Blue() );
556 #endif
557
558 //Release the pool, we're done :)
559 [thePool release];
560
561 //Return ID_OK - there are no "apply" buttons or the like
562 //on either the font or color panel
563 return wxID_OK;
564 }
565
566 //old api stuff (REMOVE ME)
567 bool wxFontDialog::IsShown() const
568 {
569 return false;
570 }
571
572 #endif // 10.2+
573
574 #endif