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