]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/fontdlgosx.mm
Set missing Language: headers in PO files.
[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 // Copyright: (c) Ryan Norton
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 // ===========================================================================
14 // declarations
15 // ===========================================================================
16
17 // ---------------------------------------------------------------------------
18 // headers
19 // ---------------------------------------------------------------------------
20
21 #include "wx/fontdlg.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/intl.h"
25 #include "wx/log.h"
26 #include "wx/cmndata.h"
27 #endif
28
29 #include "wx/fontutil.h"
30 #include "wx/modalhook.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 WX_HOOK_MODAL_DIALOG();
438
439 //Start the pool. Required for carbon interaction
440 //(For those curious, the only thing that happens
441 //if you don't do this is a bunch of error
442 //messages about leaks on the console,
443 //with no windows shown or anything).
444 NSAutoreleasePool *thePool;
445 thePool = [[NSAutoreleasePool alloc] init];
446
447 //Get the shared color and font panel
448 NSFontPanel* theFontPanel = [NSFontPanel sharedFontPanel];
449 NSColorPanel* theColorPanel = [NSColorPanel sharedColorPanel];
450
451 //Create and assign the delegates (cocoa event handlers) so
452 //we can tell if a window has closed/open or not
453 wxWCDelegate* theFPDelegate = [[wxWCDelegate alloc] init];
454 [theFontPanel setDelegate:theFPDelegate];
455
456 wxWCODelegate* theCPDelegate = [[wxWCODelegate alloc] init];
457 [theColorPanel setDelegate:theCPDelegate];
458
459 //
460 // Begin the modal loop for the font and color panels
461 //
462 // The idea is that we first make the font panel modal,
463 // but if the color panel is opened, unless we stop the
464 // modal loop the color panel opens behind the font panel
465 // with no input acceptable to it - which makes it useless.
466 //
467 // So we set up delegates for both the color and font panels,
468 // and the if the font panel opens the color panel, we
469 // stop the modal loop, and start a separate modal loop for
470 // the color panel until the color panel closes, switching
471 // back to the font panel modal loop once it does close.
472 //
473 wxDialog::OSXBeginModalDialog();
474 do
475 {
476 //
477 // Start the font panel modal loop
478 //
479 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
480 for (;;)
481 {
482 [NSApp runModalSession:session];
483
484 //If the font panel is closed or the font panel
485 //opened the color panel, break
486 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
487 break;
488 }
489 [NSApp endModalSession:session];
490
491 //is the color panel open?
492 if ([theCPDelegate isOpen])
493 {
494 //
495 // Start the color panel modal loop
496 //
497 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
498 for (;;)
499 {
500 [NSApp runModalSession:session];
501
502 //If the color panel is closed, return the font panel modal loop
503 if ([theCPDelegate isClosed])
504 break;
505 }
506 [NSApp endModalSession:session];
507 }
508 //If the font panel is still alive (I.E. we broke
509 //out of its modal loop because the color panel was
510 //opened) return the font panel modal loop
511 }while([theFPDelegate isClosed] == NO);
512 wxDialog::OSXEndModalDialog();
513
514 //free up the memory for the delegates - we don't need them anymore
515 [theFPDelegate release];
516 [theCPDelegate release];
517
518 //Get the font the user selected
519 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
520
521 //Get more information about the user's chosen font
522 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
523 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
524 int theFontSize = (int) [theFont pointSize];
525
526 //Set the wx font to the appropriate data
527 if(theTraits & NSFixedPitchFontMask)
528 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
529
530 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
531 m_fontData.m_chosenFont.SetPointSize(theFontSize);
532 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
533 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
534 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
535
536 //Get the shared color panel along with the chosen color and set the chosen color
537 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
538
539 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
540 (unsigned char) ([theColor greenComponent] * 255.0),
541 (unsigned char) ([theColor blueComponent] * 255.0));
542
543 //Friendly debug stuff
544 #ifdef FONTDLGDEBUG
545 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"),
546
547 (float) theFontSize,
548 theFontWeight,
549 theTraits,
550
551 m_fontData.m_chosenFont.GetFaceName().c_str(),
552 m_fontData.m_chosenFont.GetPointSize(),
553 m_fontData.m_chosenFont.GetStyle(),
554 m_fontData.m_chosenFont.GetWeight(),
555 m_fontData.m_fontColour.Red(),
556 m_fontData.m_fontColour.Green(),
557 m_fontData.m_fontColour.Blue() );
558 #endif
559
560 //Release the pool, we're done :)
561 [thePool release];
562
563 //Return ID_OK - there are no "apply" buttons or the like
564 //on either the font or color panel
565 return wxID_OK;
566 }
567
568 //old api stuff (REMOVE ME)
569 bool wxFontDialog::IsShown() const
570 {
571 return false;
572 }
573
574 #endif // 10.2+
575
576 #endif