]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/fontdlgosx.mm
translating background style BG_PAINT into opaqueness for speed-up of OS redraws...
[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 fontdata.m_chosenFont = wxFont( theFont );
212
213 //Get the shared color panel along with the chosen color and set the chosen color
214 NSColor* theColor = [[[NSColorPanel sharedColorPanel] color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
215
216 fontdata.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
217 (unsigned char) ([theColor greenComponent] * 255.0),
218 (unsigned char) ([theColor blueComponent] * 255.0));
219 #endif
220 retval = wxID_OK ;
221 }
222 [fontPanel setAccessoryView:nil];
223 [accessoryView release];
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, const wxFontData& data)
355 {
356 Create(parent, data);
357 }
358
359 wxFontDialog::~wxFontDialog()
360 {
361 }
362
363 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
364 {
365 m_fontData = data;
366
367 //autorelease pool - req'd for carbon
368 NSAutoreleasePool *thePool;
369 thePool = [[NSAutoreleasePool alloc] init];
370
371 //Get the initial wx font
372 wxFont& thewxfont = m_fontData.m_initialFont;
373
374 //if the font is valid set the default (selected) font of the
375 //NSFontDialog to that font
376 if (thewxfont.Ok())
377 {
378 NSFontTraitMask theMask = 0;
379
380 if(thewxfont.GetStyle() == wxFONTSTYLE_ITALIC)
381 theMask |= NSItalicFontMask;
382
383 if(thewxfont.IsFixedWidth())
384 theMask |= NSFixedPitchFontMask;
385
386 NSFont* theDefaultFont =
387 [[NSFontManager sharedFontManager] fontWithFamily:
388 wxNSStringWithWxString(thewxfont.GetFaceName())
389 traits:theMask
390 weight:thewxfont.GetWeight() == wxBOLD ? 9 :
391 thewxfont.GetWeight() == wxLIGHT ? 0 : 5
392 size: (float)(thewxfont.GetPointSize())
393 ];
394
395 wxASSERT_MSG(theDefaultFont, wxT("Invalid default font for wxCocoaFontDialog!"));
396
397 //Apple docs say to call NSFontManager::setSelectedFont
398 //However, 10.3 doesn't seem to create the font panel
399 //is this is done, so create it ourselves
400 [[NSFontPanel sharedFontPanel] setPanelFont:theDefaultFont isMultiple:NO];
401
402 }
403
404 if(m_fontData.m_fontColour.Ok())
405 [[NSColorPanel sharedColorPanel] setColor:
406 [NSColor colorWithCalibratedRed:m_fontData.m_fontColour.Red() / 255.0
407 green:m_fontData.m_fontColour.Green() / 255.0
408 blue:m_fontData.m_fontColour.Blue() / 255.0
409 alpha:1.0]
410 ];
411 else
412 [[NSColorPanel sharedColorPanel] setColor:[NSColor blackColor]];
413
414 //We're done - free up the pool
415 [thePool release];
416
417 return true;
418 }
419
420 int wxFontDialog::ShowModal()
421 {
422 //Start the pool. Required for carbon interaction
423 //(For those curious, the only thing that happens
424 //if you don't do this is a bunch of error
425 //messages about leaks on the console,
426 //with no windows shown or anything).
427 NSAutoreleasePool *thePool;
428 thePool = [[NSAutoreleasePool alloc] init];
429
430 //Get the shared color and font panel
431 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
432 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
433
434 //Create and assign the delegates (cocoa event handlers) so
435 //we can tell if a window has closed/open or not
436 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
437 [theFontPanel setDelegate:theFPDelegate];
438
439 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
440 [theColorPanel setDelegate:theCPDelegate];
441
442 //
443 // Begin the modal loop for the font and color panels
444 //
445 // The idea is that we first make the font panel modal,
446 // but if the color panel is opened, unless we stop the
447 // modal loop the color panel opens behind the font panel
448 // with no input acceptable to it - which makes it useless.
449 //
450 // So we set up delegates for both the color and font panels,
451 // and the if the font panel opens the color panel, we
452 // stop the modal loop, and start a separate modal loop for
453 // the color panel until the color panel closes, switching
454 // back to the font panel modal loop once it does close.
455 //
456 do
457 {
458 //
459 // Start the font panel modal loop
460 //
461 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
462 for (;;)
463 {
464 [NSApp runModalSession:session];
465
466 //If the font panel is closed or the font panel
467 //opened the color panel, break
468 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
469 break;
470 }
471 [NSApp endModalSession:session];
472
473 //is the color panel open?
474 if ([theCPDelegate isOpen])
475 {
476 //
477 // Start the color panel modal loop
478 //
479 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
480 for (;;)
481 {
482 [NSApp runModalSession:session];
483
484 //If the color panel is closed, return the font panel modal loop
485 if ([theCPDelegate isClosed])
486 break;
487 }
488 [NSApp endModalSession:session];
489 }
490 //If the font panel is still alive (I.E. we broke
491 //out of its modal loop because the color panel was
492 //opened) return the font panel modal loop
493 }while([theFPDelegate isClosed] == NO);
494
495 //free up the memory for the delegates - we don't need them anymore
496 [theFPDelegate release];
497 [theCPDelegate release];
498
499 //Get the font the user selected
500 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
501
502 //Get more information about the user's chosen font
503 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
504 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
505 int theFontSize = (int) [theFont pointSize];
506
507 //Set the wx font to the appropriate data
508 if(theTraits & NSFixedPitchFontMask)
509 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
510
511 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
512 m_fontData.m_chosenFont.SetPointSize(theFontSize);
513 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
514 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
515 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
516
517 //Get the shared color panel along with the chosen color and set the chosen color
518 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
519
520 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
521 (unsigned char) ([theColor greenComponent] * 255.0),
522 (unsigned char) ([theColor blueComponent] * 255.0));
523
524 //Friendly debug stuff
525 #ifdef FONTDLGDEBUG
526 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"),
527
528 (float) theFontSize,
529 theFontWeight,
530 theTraits,
531
532 m_fontData.m_chosenFont.GetFaceName().c_str(),
533 m_fontData.m_chosenFont.GetPointSize(),
534 m_fontData.m_chosenFont.GetStyle(),
535 m_fontData.m_chosenFont.GetWeight(),
536 m_fontData.m_fontColour.Red(),
537 m_fontData.m_fontColour.Green(),
538 m_fontData.m_fontColour.Blue() );
539 #endif
540
541 //Release the pool, we're done :)
542 [thePool release];
543
544 //Return ID_OK - there are no "apply" buttons or the like
545 //on either the font or color panel
546 return wxID_OK;
547 }
548
549 //old api stuff (REMOVE ME)
550 bool wxFontDialog::IsShown() const
551 {
552 return false;
553 }
554
555 #endif // 10.2+
556
557 #endif