]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/carbon/fontdlgosx.mm
solving include order problems for stl and xti
[wxWidgets.git] / src / osx / carbon / fontdlgosx.mm
... / ...
CommitLineData
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
138extern "C" int RunMixedFontDialog(wxFontDialog* dialog) ;
139
140int 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
232IMPLEMENT_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
350wxFontDialog::wxFontDialog()
351{
352}
353
354wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
355{
356 Create(parent, data);
357}
358
359wxFontDialog::~wxFontDialog()
360{
361}
362
363bool 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.IsOk())
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.IsOk())
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
420int 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 wxDialog::OSXBeginModalDialog();
457 do
458 {
459 //
460 // Start the font panel modal loop
461 //
462 NSModalSession session = [NSApp beginModalSessionForWindow:theFontPanel];
463 for (;;)
464 {
465 [NSApp runModalSession:session];
466
467 //If the font panel is closed or the font panel
468 //opened the color panel, break
469 if ([theFPDelegate isClosed] || [theCPDelegate isOpen])
470 break;
471 }
472 [NSApp endModalSession:session];
473
474 //is the color panel open?
475 if ([theCPDelegate isOpen])
476 {
477 //
478 // Start the color panel modal loop
479 //
480 NSModalSession session = [NSApp beginModalSessionForWindow:theColorPanel];
481 for (;;)
482 {
483 [NSApp runModalSession:session];
484
485 //If the color panel is closed, return the font panel modal loop
486 if ([theCPDelegate isClosed])
487 break;
488 }
489 [NSApp endModalSession:session];
490 }
491 //If the font panel is still alive (I.E. we broke
492 //out of its modal loop because the color panel was
493 //opened) return the font panel modal loop
494 }while([theFPDelegate isClosed] == NO);
495 wxDialog::OSXEndModalDialog();
496
497 //free up the memory for the delegates - we don't need them anymore
498 [theFPDelegate release];
499 [theCPDelegate release];
500
501 //Get the font the user selected
502 NSFont* theFont = [theFontPanel panelConvertFont:[NSFont userFontOfSize:0]];
503
504 //Get more information about the user's chosen font
505 NSFontTraitMask theTraits = [[NSFontManager sharedFontManager] traitsOfFont:theFont];
506 int theFontWeight = [[NSFontManager sharedFontManager] weightOfFont:theFont];
507 int theFontSize = (int) [theFont pointSize];
508
509 //Set the wx font to the appropriate data
510 if(theTraits & NSFixedPitchFontMask)
511 m_fontData.m_chosenFont.SetFamily(wxTELETYPE);
512
513 m_fontData.m_chosenFont.SetFaceName(wxStringWithNSString([theFont familyName]));
514 m_fontData.m_chosenFont.SetPointSize(theFontSize);
515 m_fontData.m_chosenFont.SetStyle(theTraits & NSItalicFontMask ? wxFONTSTYLE_ITALIC : 0);
516 m_fontData.m_chosenFont.SetWeight(theFontWeight < 5 ? wxLIGHT :
517 theFontWeight >= 9 ? wxBOLD : wxNORMAL);
518
519 //Get the shared color panel along with the chosen color and set the chosen color
520 NSColor* theColor = [[theColorPanel color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
521
522 m_fontData.m_fontColour.Set((unsigned char) ([theColor redComponent] * 255.0),
523 (unsigned char) ([theColor greenComponent] * 255.0),
524 (unsigned char) ([theColor blueComponent] * 255.0));
525
526 //Friendly debug stuff
527#ifdef FONTDLGDEBUG
528 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"),
529
530 (float) theFontSize,
531 theFontWeight,
532 theTraits,
533
534 m_fontData.m_chosenFont.GetFaceName().c_str(),
535 m_fontData.m_chosenFont.GetPointSize(),
536 m_fontData.m_chosenFont.GetStyle(),
537 m_fontData.m_chosenFont.GetWeight(),
538 m_fontData.m_fontColour.Red(),
539 m_fontData.m_fontColour.Green(),
540 m_fontData.m_fontColour.Blue() );
541#endif
542
543 //Release the pool, we're done :)
544 [thePool release];
545
546 //Return ID_OK - there are no "apply" buttons or the like
547 //on either the font or color panel
548 return wxID_OK;
549}
550
551//old api stuff (REMOVE ME)
552bool wxFontDialog::IsShown() const
553{
554 return false;
555}
556
557#endif // 10.2+
558
559#endif