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