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