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