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