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