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