]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/fontdlg.cpp
Add wxEventLoop::ScheduleExit().
[wxWidgets.git] / src / osx / carbon / fontdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/fontdlg.cpp
3 // Purpose: wxFontDialog class for carbon 10.2+.
4 // Author: Ryan Norton
5 // Modified by:
6 // Created: 1998-01-01
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
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if wxUSE_FONTDLG
24
25 #ifdef __BORLANDC__
26 #pragma hdrstop
27 #endif
28
29 #ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/crt.h"
32 #include "wx/dcclient.h"
33 #include "wx/frame.h"
34 #include "wx/textctrl.h"
35 #include "wx/listbox.h"
36 #include "wx/checkbox.h"
37 #include "wx/choice.h"
38 #include "wx/sizer.h"
39 #include "wx/stattext.h"
40 #include "wx/button.h"
41 #endif
42
43 #include "wx/fontdlg.h"
44 #include "wx/fontutil.h"
45 #include "wx/modalhook.h"
46
47 #if wxOSX_USE_EXPERIMENTAL_FONTDIALOG
48
49 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
50
51 #include "wx/osx/private.h"
52
53 // ---------------------------------------------------------------------------
54 // wxFontDialog
55 // ---------------------------------------------------------------------------
56
57 #if wxOSX_USE_CARBON
58
59 static const EventTypeSpec eventList[] =
60 {
61 { kEventClassFont, kEventFontSelection } ,
62 } ;
63
64
65 pascal OSStatus
66 wxMacCarbonFontPanelHandler(EventHandlerCallRef WXUNUSED(nextHandler),
67 EventRef event,
68 void *userData)
69 {
70 OSStatus result = eventNotHandledErr ;
71 wxFontDialog *fontdialog = (wxFontDialog*) userData ;
72 wxFontData& fontdata= fontdialog->GetFontData() ;
73
74 wxMacCarbonEvent cEvent( event );
75 switch(cEvent.GetKind())
76 {
77 case kEventFontSelection :
78 {
79 bool setup = false ;
80 #if wxOSX_USE_CORE_TEXT
81 if ( !setup )
82 {
83 CTFontDescriptorRef descr;
84 if ( cEvent.GetParameter<CTFontDescriptorRef>( kEventParamCTFontDescriptor, typeCTFontDescriptorRef, &descr ) == noErr )
85 {
86 wxFont font;
87 wxNativeFontInfo fontinfo;
88 fontinfo.Init(descr);
89 font.Create(fontinfo);
90 fontdata.SetChosenFont( font ) ;
91 setup = true;
92 }
93 }
94 #endif
95 #if wxOSX_USE_ATSU_TEXT
96 ATSUFontID fontId = 0 ;
97 if ( !setup && (cEvent.GetParameter<ATSUFontID>(kEventParamATSUFontID, &fontId) == noErr) )
98 {
99 FMFontStyle fontStyle = cEvent.GetParameter<FMFontStyle>(kEventParamFMFontStyle);
100 FMFontSize fontSize = cEvent.GetParameter<FMFontSize>(kEventParamFMFontSize);
101
102 CFStringRef cfName = NULL;
103 #if 1
104 FMFontFamily fontFamily = cEvent.GetParameter<FMFontFamily>(kEventParamFMFontFamily);
105 ATSFontFamilyRef atsfontfamilyref = FMGetATSFontFamilyRefFromFontFamily( fontFamily ) ;
106 OSStatus err = ATSFontFamilyGetName( atsfontfamilyref , kATSOptionFlagsDefault , &cfName ) ;
107 if ( err != noErr )
108 {
109 wxFAIL_MSG("ATSFontFamilyGetName failed");
110 }
111 #else
112 // we don't use the ATSU naming anymore
113 ByteCount actualLength = 0;
114 char *c = NULL;
115 OSStatus err = ATSUFindFontName(fontId , kFontFamilyName, kFontUnicodePlatform, kFontNoScriptCode,
116 kFontNoLanguageCode , 0 , NULL , &actualLength , NULL );
117 if ( err == noErr)
118 {
119 actualLength += 1 ;
120 char *c = (char*)malloc( actualLength );
121 err = ATSUFindFontName(fontId, kFontFamilyName, kFontUnicodePlatform, kFontNoScriptCode,
122 kFontNoLanguageCode, actualLength, c , NULL, NULL);
123 cfName = CFStringCreateWithCharacters(NULL, (UniChar*) c, (actualLength-1) >> 1);
124 }
125 else
126 {
127 err = ATSUFindFontName(fontId , kFontFamilyName, kFontNoPlatformCode, kFontNoScriptCode,
128 kFontNoLanguageCode , 0 , NULL , &actualLength , NULL );
129 if ( err == noErr )
130 {
131 actualLength += 1 ;
132 c = (char*)malloc(actualLength);
133 err = ATSUFindFontName(fontId, kFontFamilyName, kFontNoPlatformCode, kFontNoScriptCode,
134 kFontNoLanguageCode, actualLength, c , NULL, NULL);
135 c[actualLength-1] = 0;
136 cfName = CFStringCreateWithCString(NULL, c, kCFStringEncodingMacRoman );
137 }
138 }
139 if ( c!=NULL )
140 free(c);
141 #endif
142 if ( cfName!=NULL )
143 {
144 fontdata.m_chosenFont.SetFaceName(wxCFStringRef(cfName).AsString(wxLocale::GetSystemEncoding()));
145 fontdata.m_chosenFont.SetPointSize(fontSize);
146 fontdata.m_chosenFont.SetStyle(fontStyle & italic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL);
147 fontdata.m_chosenFont.SetUnderlined((fontStyle & underline)!=0);
148 fontdata.m_chosenFont.SetWeight(fontStyle & bold ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
149 }
150 }
151 #endif // wxOSX_USE_ATSU_TEXT
152
153 // retrieving the color
154 RGBColor fontColor ;
155 if ( cEvent.GetParameter<RGBColor>(kEventParamFontColor, &fontColor) == noErr )
156 {
157 fontdata.m_fontColour = fontColor;
158 }
159 else
160 {
161 CFDictionaryRef dict ;
162 if ( cEvent.GetParameter<CFDictionaryRef>(kEventParamDictionary, &dict) == noErr )
163 {
164 CFDictionaryRef attributesDict ;
165 if ( CFDictionaryGetValueIfPresent(dict, kFontPanelAttributesKey, (const void **)&attributesDict) )
166 {
167 CFDataRef tagsData;
168 CFDataRef sizesData;
169 CFDataRef valuesData;
170 if ( CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeTagsKey, (const void **)&tagsData) &&
171 CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeSizesKey, (const void **)&sizesData) &&
172 CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeValuesKey, (const void **)&valuesData) )
173 {
174 ItemCount count = CFDataGetLength(tagsData)/sizeof(ATSUAttributeTag);
175 ATSUAttributeTag *tagPtr = (ATSUAttributeTag *)CFDataGetBytePtr(tagsData);
176 ByteCount *sizePtr = (ByteCount *)CFDataGetBytePtr(sizesData);
177 UInt32 *bytePtr = (UInt32*)CFDataGetBytePtr(valuesData);
178 ATSUAttributeValuePtr valuesPtr = bytePtr ;
179 for ( ItemCount i = 0 ; i < count ; ++i)
180 {
181 if ( tagPtr[i] == kATSUColorTag && sizePtr[i] == sizeof(RGBColor))
182 {
183 fontdata.m_fontColour = *(RGBColor *)valuesPtr;
184 break ;
185 }
186 bytePtr = (UInt32*)( (UInt8*)bytePtr + sizePtr[i]);
187 }
188 }
189 }
190 }
191 }
192 }
193 break ;
194 }
195
196 return result ;
197 }
198
199 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacCarbonFontPanelHandler )
200 #endif
201
202 wxFontDialog::wxFontDialog()
203 {
204 }
205
206 wxFontDialog::wxFontDialog(wxWindow *parent)
207 {
208 Create(parent);
209 }
210
211 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
212 {
213 Create(parent, data);
214 }
215
216 wxFontDialog::~wxFontDialog()
217 {
218 }
219
220 bool wxFontDialog::Create(wxWindow *WXUNUSED(parent))
221 {
222 return true ;
223 }
224
225 bool wxFontDialog::Create(wxWindow *parent, const wxFontData& data)
226 {
227 m_fontData = data;
228 return Create(parent) ;
229 }
230
231 int wxFontDialog::ShowModal()
232 {
233 WX_HOOK_MODAL_DIALOG();
234
235 #if wxOSX_USE_CARBON
236
237 OSStatus err ;
238 wxFont font = *wxNORMAL_FONT ;
239 if ( m_fontData.m_initialFont.IsOk() )
240 {
241 font = m_fontData.m_initialFont ;
242 }
243
244 bool setup = false;
245 #if wxOSX_USE_CORE_TEXT
246 if ( !setup )
247 {
248 CTFontDescriptorRef descr = (CTFontDescriptorRef) CTFontCopyFontDescriptor( (CTFontRef) font.OSXGetCTFont() );
249 err = SetFontInfoForSelection (kFontSelectionCoreTextType,1, &descr , NULL);
250 CFRelease( descr );
251 setup = true;
252 }
253 #endif
254 #if wxOSX_USE_ATSU_TEXT
255 if ( !setup )
256 {
257 ATSUStyle style = (ATSUStyle)font.MacGetATSUStyle();
258 err = SetFontInfoForSelection (kFontSelectionATSUIType,1, &style , NULL);
259 setup = true;
260 }
261 #endif
262 // just clicking on ENTER will not send us any font setting event, therefore we have to make sure
263 // that field is already correct
264 m_fontData.m_chosenFont = font ;
265
266 EventHandlerRef handler ;
267
268 err = InstallApplicationEventHandler( GetwxMacCarbonFontPanelHandlerUPP(), GetEventTypeCount(eventList), eventList, this , &handler );
269
270 if ( !FPIsFontPanelVisible() )
271 FPShowHideFontPanel();
272 #endif
273 wxDialog::OSXBeginModalDialog();
274 int retval = RunMixedFontDialog(this);
275 wxDialog::OSXEndModalDialog();
276 #if wxOSX_USE_CARBON
277 ::RemoveEventHandler(handler);
278 #endif
279
280 return retval ;
281 }
282
283 #else
284
285 #if !USE_NATIVE_FONT_DIALOG_FOR_MACOSX
286
287 #undef wxFontDialog
288
289 #include "wx/osx/fontdlg.h"
290
291 #include "wx/fontenum.h"
292 #include "wx/colordlg.h"
293 #include "wx/spinctrl.h"
294
295 // ---------------------------------------------------------------------------
296 // wxFontDialog stub for mac OS's without a native font dialog
297 // ---------------------------------------------------------------------------
298
299 static const wxChar *FontFamilyIntToString(int family);
300 static int FontFamilyStringToInt(const wxChar *family);
301
302
303 //-----------------------------------------------------------------------------
304 // helper class - wxFontPreviewCtrl
305 //-----------------------------------------------------------------------------
306
307 class WXDLLEXPORT wxFontPreviewCtrl : public wxWindow
308 {
309 public:
310 wxFontPreviewCtrl(wxWindow *parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& sz = wxDefaultSize, long style = 0) :
311 wxWindow(parent, id, pos, sz, style)
312 {
313 SetBackgroundColour(*wxWHITE);
314 }
315
316 private:
317 void OnPaint(wxPaintEvent& event);
318 DECLARE_EVENT_TABLE()
319 };
320
321 BEGIN_EVENT_TABLE(wxFontPreviewCtrl, wxWindow)
322 EVT_PAINT(wxFontPreviewCtrl::OnPaint)
323 END_EVENT_TABLE()
324
325 void wxFontPreviewCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
326 {
327 wxPaintDC dc(this);
328
329 wxSize size = GetSize();
330 wxFont font = GetFont();
331
332 if ( font.IsOk() )
333 {
334 dc.SetFont(font);
335 // Calculate vertical centre
336 long w = 0, h = 0;
337 dc.GetTextExtent( wxT("X"), &w, &h);
338 dc.SetTextForeground(GetForegroundColour());
339 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
340 dc.DrawText(_("ABCDEFGabcdefg12345"),
341 10, size.y/2 - h/2);
342 dc.DestroyClippingRegion();
343 }
344 }
345
346 /*
347 * A control for displaying a small preview of a colour or bitmap
348 */
349
350 class wxFontColourSwatchCtrl: public wxControl
351 {
352 DECLARE_CLASS(wxFontColourSwatchCtrl)
353 public:
354 wxFontColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0);
355 virtual ~wxFontColourSwatchCtrl();
356
357 void OnPaint(wxPaintEvent& event);
358 void OnMouseEvent(wxMouseEvent& event);
359
360 void SetColour(const wxColour& colour) { m_colour = colour; SetBackgroundColour(m_colour); }
361
362 wxColour& GetColour() { return m_colour; }
363
364 virtual wxSize DoGetBestSize() const { return GetSize(); }
365
366 protected:
367 wxColour m_colour;
368
369 DECLARE_EVENT_TABLE()
370 };
371
372 /*
373 * A control for displaying a small preview of a colour or bitmap
374 */
375
376 BEGIN_EVENT_TABLE(wxFontColourSwatchCtrl, wxControl)
377 EVT_MOUSE_EVENTS(wxFontColourSwatchCtrl::OnMouseEvent)
378 END_EVENT_TABLE()
379
380 IMPLEMENT_CLASS(wxFontColourSwatchCtrl, wxControl)
381
382 wxFontColourSwatchCtrl::wxFontColourSwatchCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style):
383 wxControl(parent, id, pos, size, style)
384 {
385 SetColour(* wxWHITE);
386 SetBackgroundStyle(wxBG_STYLE_COLOUR);
387 }
388
389 wxFontColourSwatchCtrl::~wxFontColourSwatchCtrl()
390 {
391 }
392
393 void wxFontColourSwatchCtrl::OnMouseEvent(wxMouseEvent& event)
394 {
395 if (event.LeftDown())
396 {
397 wxWindow* parent = GetParent();
398 while (parent != NULL && !parent->IsKindOf(CLASSINFO(wxDialog)) && !parent->IsKindOf(CLASSINFO(wxFrame)))
399 parent = parent->GetParent();
400
401 wxColourData data;
402 data.SetChooseFull(true);
403 data.SetColour(m_colour);
404 wxColourDialog *dialog = new wxColourDialog(parent, &data);
405 // Crashes on wxMac (no m_peer)
406 #ifndef __WXMAC__
407 dialog->SetTitle(_("Background colour"));
408 #endif
409 if (dialog->ShowModal() == wxID_OK)
410 {
411 wxColourData retData = dialog->GetColourData();
412 m_colour = retData.GetColour();
413 SetBackgroundColour(m_colour);
414 }
415 dialog->Destroy();
416 Refresh();
417
418 wxCommandEvent event(wxEVT_BUTTON, GetId());
419 HandleWindowEvent(event);
420 }
421 }
422
423 /*!
424 * wxFontDialog type definition
425 */
426
427 IMPLEMENT_DYNAMIC_CLASS( wxFontDialog, wxDialog )
428
429 /*!
430 * wxFontDialog event table definition
431 */
432
433 BEGIN_EVENT_TABLE( wxFontDialog, wxDialog )
434 EVT_LISTBOX( wxID_FONTDIALOG_FACENAME, wxFontDialog::OnFontdialogFacenameSelected )
435 EVT_SPINCTRL( wxID_FONTDIALOG_FONTSIZE, wxFontDialog::OnFontdialogFontsizeUpdated )
436 EVT_TEXT( wxID_FONTDIALOG_FONTSIZE, wxFontDialog::OnFontdialogFontsizeTextUpdated )
437 EVT_CHECKBOX( wxID_FONTDIALOG_BOLD, wxFontDialog::OnFontdialogBoldClick )
438 EVT_CHECKBOX( wxID_FONTDIALOG_ITALIC, wxFontDialog::OnFontdialogItalicClick )
439 EVT_CHECKBOX( wxID_FONTDIALOG_UNDERLINED, wxFontDialog::OnFontdialogUnderlinedClick )
440 EVT_BUTTON( wxID_OK, wxFontDialog::OnOkClick )
441 EVT_BUTTON(wxID_FONTDIALOG_COLOUR, wxFontDialog::OnColourChanged)
442 END_EVENT_TABLE()
443
444 /*!
445 * wxFontDialog constructors
446 */
447
448 wxFontDialog::wxFontDialog( )
449 {
450 m_dialogParent = NULL;
451 }
452
453 wxFontDialog::wxFontDialog(wxWindow* parent, const wxFontData& fontData)
454 {
455 m_dialogParent = NULL;
456
457 Create(parent, fontData);
458 }
459
460 wxFontDialog::~wxFontDialog()
461 {
462 // empty
463 }
464
465 /*!
466 * wxFontDialog creator
467 */
468
469 bool wxFontDialog::Create(wxWindow* parent, const wxFontData& fontData)
470 {
471 m_fontData = fontData;
472 m_suppressUpdates = false;
473
474 wxString caption = _("Font");
475
476 m_facenameCtrl = NULL;
477 m_sizeCtrl = NULL;
478 m_boldCtrl = NULL;
479 m_italicCtrl = NULL;
480 m_underlinedCtrl = NULL;
481 m_colourCtrl = NULL;
482 m_previewCtrl = NULL;
483
484 InitializeFont();
485
486 SetExtraStyle(GetExtraStyle()|wxWS_EX_BLOCK_EVENTS);
487 wxDialog::Create( parent, wxID_ANY, caption, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER );
488
489 CreateControls();
490 GetSizer()->Fit(this);
491 GetSizer()->SetSizeHints(this);
492 Centre();
493
494 return true;
495 }
496
497 /*!
498 * Control creation for wxFontDialog
499 */
500
501 void wxFontDialog::CreateControls()
502 {
503 wxFontDialog* itemDialog1 = this;
504
505 wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
506 itemDialog1->SetSizer(itemBoxSizer2);
507
508 wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
509 itemBoxSizer2->Add(itemBoxSizer3, 1, wxGROW|wxALL, 5);
510
511 wxFlexGridSizer* itemFlexGridSizer4 = new wxFlexGridSizer(6, 2, 10, 0);
512 itemFlexGridSizer4->AddGrowableRow(4);
513 itemFlexGridSizer4->AddGrowableCol(1);
514 itemBoxSizer3->Add(itemFlexGridSizer4, 1, wxGROW|wxALL, 5);
515
516 wxStaticText* itemStaticText5 = new wxStaticText( itemDialog1, wxID_STATIC, _("Font:"), wxDefaultPosition, wxDefaultSize, 0 );
517 itemFlexGridSizer4->Add(itemStaticText5, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL, 5);
518
519 wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxVERTICAL);
520 itemFlexGridSizer4->Add(itemBoxSizer6, 0, wxGROW, 5);
521
522 wxString* m_facenameCtrlStrings = NULL;
523 m_facenameCtrl = new wxListBox( itemDialog1, wxID_FONTDIALOG_FACENAME, wxDefaultPosition, wxSize(320, 100), 0, m_facenameCtrlStrings, wxLB_SINGLE );
524 itemBoxSizer6->Add(m_facenameCtrl, 0, wxGROW|wxALL, 5);
525
526 wxStaticText* itemStaticText8 = new wxStaticText( itemDialog1, wxID_STATIC, _("Size:"), wxDefaultPosition, wxDefaultSize, 0 );
527 itemFlexGridSizer4->Add(itemStaticText8, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
528
529 m_sizeCtrl = new wxSpinCtrl( itemDialog1, wxID_FONTDIALOG_FONTSIZE, wxT("12"), wxDefaultPosition, wxSize(60, -1), wxSP_ARROW_KEYS, 1, 300, 12 );
530 m_sizeCtrl->SetHelpText(_("The font size in points."));
531 if (ShowToolTips())
532 m_sizeCtrl->SetToolTip(_("The font size in points."));
533 itemFlexGridSizer4->Add(m_sizeCtrl, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
534
535 wxStaticText* itemStaticText10 = new wxStaticText( itemDialog1, wxID_STATIC, _("Style:"), wxDefaultPosition, wxDefaultSize, 0 );
536 itemFlexGridSizer4->Add(itemStaticText10, 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxALL, 5);
537
538 wxBoxSizer* itemBoxSizer11 = new wxBoxSizer(wxHORIZONTAL);
539 itemFlexGridSizer4->Add(itemBoxSizer11, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL, 5);
540
541 m_boldCtrl = new wxCheckBox( itemDialog1, wxID_FONTDIALOG_BOLD, _("Bold"), wxDefaultPosition, wxDefaultSize, wxCHK_2STATE );
542 m_boldCtrl->SetValue(false);
543 m_boldCtrl->SetHelpText(_("Check to make the font bold."));
544 if (ShowToolTips())
545 m_boldCtrl->SetToolTip(_("Check to make the font bold."));
546 itemBoxSizer11->Add(m_boldCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
547
548 m_italicCtrl = new wxCheckBox( itemDialog1, wxID_FONTDIALOG_ITALIC, _("Italic"), wxDefaultPosition, wxDefaultSize, wxCHK_2STATE );
549 m_italicCtrl->SetValue(false);
550 m_italicCtrl->SetHelpText(_("Check to make the font italic."));
551 if (ShowToolTips())
552 m_italicCtrl->SetToolTip(_("Check to make the font italic."));
553 itemBoxSizer11->Add(m_italicCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
554
555 if (m_fontData.GetEnableEffects())
556 {
557 m_underlinedCtrl = new wxCheckBox( itemDialog1, wxID_FONTDIALOG_UNDERLINED, _("Underlined"), wxDefaultPosition, wxDefaultSize, wxCHK_2STATE );
558 m_underlinedCtrl->SetValue(false);
559 m_underlinedCtrl->SetHelpText(_("Check to make the font underlined."));
560 if (ShowToolTips())
561 m_underlinedCtrl->SetToolTip(_("Check to make the font underlined."));
562 itemBoxSizer11->Add(m_underlinedCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
563 }
564
565 if (m_fontData.GetEnableEffects())
566 {
567 wxStaticText* itemStaticText15 = new wxStaticText( itemDialog1, wxID_STATIC, _("Colour:"), wxDefaultPosition, wxDefaultSize, 0 );
568 itemFlexGridSizer4->Add(itemStaticText15, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL, 5);
569
570 m_colourCtrl = new wxFontColourSwatchCtrl( itemDialog1, wxID_FONTDIALOG_COLOUR, wxDefaultPosition, wxSize(-1, 30), wxSUNKEN_BORDER|wxTAB_TRAVERSAL );
571 m_colourCtrl->SetHelpText(_("Click to change the font colour."));
572 if (ShowToolTips())
573 m_colourCtrl->SetToolTip(_("Click to change the font colour."));
574 itemFlexGridSizer4->Add(m_colourCtrl, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
575 }
576
577 wxStaticText* itemStaticText17 = new wxStaticText( itemDialog1, wxID_STATIC, _("Preview:"), wxDefaultPosition, wxDefaultSize, 0 );
578 itemFlexGridSizer4->Add(itemStaticText17, 0, wxALIGN_RIGHT|wxALIGN_TOP|wxALL, 5);
579
580 m_previewCtrl = new wxFontPreviewCtrl( itemDialog1, wxID_FONTDIALOG_PREVIEW, wxDefaultPosition, wxSize(-1, 70), wxSUNKEN_BORDER|wxTAB_TRAVERSAL );
581 m_previewCtrl->SetHelpText(_("Shows a preview of the font."));
582 if (ShowToolTips())
583 m_previewCtrl->SetToolTip(_("Shows a preview of the font."));
584 itemFlexGridSizer4->Add(m_previewCtrl, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
585
586 wxBoxSizer* itemBoxSizer19 = new wxBoxSizer(wxHORIZONTAL);
587 itemBoxSizer3->Add(itemBoxSizer19, 0, wxALIGN_RIGHT|wxALL, 5);
588
589 wxButton* itemButton20 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
590 itemButton20->SetHelpText(_("Click to cancel changes to the font."));
591 if (ShowToolTips())
592 itemButton20->SetToolTip(_("Click to cancel changes to the font."));
593 itemBoxSizer19->Add(itemButton20, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
594
595 wxButton* itemButton21 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
596 itemButton21->SetDefault();
597 itemButton21->SetHelpText(_("Click to confirm changes to the font."));
598 if (ShowToolTips())
599 itemButton21->SetToolTip(_("Click to confirm changes to the font."));
600 itemBoxSizer19->Add(itemButton21, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
601
602 wxFontEnumerator enumerator;
603 enumerator.EnumerateFacenames();
604 wxArrayString facenames = enumerator.GetFacenames();
605 if (!facenames.empty())
606 {
607 facenames.Add(_("<Any>"));
608 facenames.Add(_("<Any Roman>"));
609 facenames.Add(_("<Any Decorative>"));
610 facenames.Add(_("<Any Modern>"));
611 facenames.Add(_("<Any Script>"));
612 facenames.Add(_("<Any Swiss>"));
613 facenames.Add(_("<Any Teletype>"));
614 facenames.Sort();
615 m_facenameCtrl->Append(facenames);
616 }
617
618 InitializeControls();
619 m_previewCtrl->SetFont(m_dialogFont);
620 if (m_fontData.GetColour().IsOk())
621 {
622 m_previewCtrl->SetForegroundColour(m_fontData.GetColour());
623 }
624 m_previewCtrl->Refresh();
625 }
626
627 /*!
628 * wxEVT_SPINCTRL event handler for wxID_FONTDIALOG_FONTSIZE
629 */
630
631 void wxFontDialog::OnFontdialogFontsizeUpdated( wxSpinEvent& WXUNUSED(event) )
632 {
633 ChangeFont();
634 }
635
636 /*!
637 * wxEVT_TEXT event handler for wxID_FONTDIALOG_FONTSIZE
638 */
639
640 void wxFontDialog::OnFontdialogFontsizeTextUpdated( wxCommandEvent& WXUNUSED(event) )
641 {
642 ChangeFont();
643 }
644
645 /*!
646 * wxEVT_CHECKBOX event handler for wxID_FONTDIALOG_BOLD
647 */
648
649 void wxFontDialog::OnFontdialogBoldClick( wxCommandEvent& WXUNUSED(event) )
650 {
651 ChangeFont();
652 }
653
654 /*!
655 * wxEVT_CHECKBOX event handler for wxID_FONTDIALOG_ITALIC
656 */
657
658 void wxFontDialog::OnFontdialogItalicClick( wxCommandEvent& WXUNUSED(event) )
659 {
660 ChangeFont();
661 }
662
663 /*!
664 * wxEVT_CHECKBOX event handler for wxID_FONTDIALOG_UNDERLINED
665 */
666
667 void wxFontDialog::OnFontdialogUnderlinedClick( wxCommandEvent& WXUNUSED(event) )
668 {
669 ChangeFont();
670 }
671
672 /*!
673 * wxEVT_BUTTON event handler for wxID_OK
674 */
675
676 void wxFontDialog::OnOkClick( wxCommandEvent& event )
677 {
678 event.Skip();
679 }
680
681
682 /*!
683 * wxEVT_LISTBOX event handler for wxID_FONTDIALOG_FACENAME
684 */
685
686 void wxFontDialog::OnFontdialogFacenameSelected( wxCommandEvent& WXUNUSED(event) )
687 {
688 ChangeFont();
689 }
690
691 void wxFontDialog::OnColourChanged(wxCommandEvent & WXUNUSED(event))
692 {
693 m_fontData.SetColour(m_colourCtrl->GetColour());
694 m_previewCtrl->SetForegroundColour(m_colourCtrl->GetColour());
695 m_previewCtrl->Refresh();
696 }
697
698 /*!
699 * Should we show tooltips?
700 */
701
702 bool wxFontDialog::ShowToolTips()
703 {
704 return true;
705 }
706
707 void wxFontDialog::InitializeFont()
708 {
709 int fontFamily = wxSWISS;
710 int fontWeight = wxNORMAL;
711 int fontStyle = wxNORMAL;
712 int fontSize = 12;
713 bool fontUnderline = false;
714 wxString fontName;
715
716 if (m_fontData.m_initialFont.IsOk())
717 {
718 fontFamily = m_fontData.m_initialFont.GetFamily();
719 fontWeight = m_fontData.m_initialFont.GetWeight();
720 fontStyle = m_fontData.m_initialFont.GetStyle();
721 fontSize = m_fontData.m_initialFont.GetPointSize();
722 fontUnderline = m_fontData.m_initialFont.GetUnderlined();
723 fontName = m_fontData.m_initialFont.GetFaceName();
724 }
725
726 m_dialogFont = wxFont(fontSize, fontFamily, fontStyle,
727 fontWeight, fontUnderline, fontName);
728
729 if (m_previewCtrl)
730 m_previewCtrl->SetFont(m_dialogFont);
731
732 m_fontData.SetChosenFont(m_dialogFont);
733 }
734
735 /// Set controls according to current font
736 void wxFontDialog::InitializeControls()
737 {
738 m_suppressUpdates = true;
739
740 if (m_underlinedCtrl)
741 m_underlinedCtrl->SetValue(m_dialogFont.GetUnderlined());
742
743 m_boldCtrl->SetValue(m_dialogFont.GetWeight() == wxBOLD);
744 m_italicCtrl->SetValue(m_dialogFont.GetStyle() == wxITALIC);
745 m_sizeCtrl->SetValue(m_dialogFont.GetPointSize());
746
747 wxString facename = m_dialogFont.GetFaceName();
748 if (facename.empty() || m_facenameCtrl->FindString(facename) == wxNOT_FOUND)
749 {
750 facename = FontFamilyIntToString(m_dialogFont.GetFamily());
751 }
752 m_facenameCtrl->SetStringSelection(facename);
753
754 if (m_colourCtrl && m_fontData.GetColour().IsOk())
755 {
756 m_colourCtrl->SetColour(m_fontData.GetColour());
757 m_colourCtrl->Refresh();
758 }
759
760 m_suppressUpdates = false;
761 }
762
763 /// Respond to font change
764 void wxFontDialog::ChangeFont()
765 {
766 if (m_suppressUpdates)
767 return;
768
769 bool underlined = m_underlinedCtrl ? m_underlinedCtrl->GetValue() : false;
770 bool italic = m_italicCtrl->GetValue();
771 bool bold = m_boldCtrl->GetValue();
772 int size = m_sizeCtrl->GetValue();
773 wxString facename = m_facenameCtrl->GetStringSelection();
774
775 int family = FontFamilyStringToInt(facename);
776 if (family == -1)
777 family = wxDEFAULT;
778 else
779 facename = wxEmptyString;
780
781 m_dialogFont = wxFont(size, family, italic ? wxITALIC : wxNORMAL, bold ? wxBOLD : wxNORMAL,
782 underlined, facename);
783
784 m_fontData.SetChosenFont(m_dialogFont);
785
786 m_previewCtrl->SetFont(m_dialogFont);
787 m_previewCtrl->Refresh();
788 }
789
790 void wxFontDialog::SetData(const wxFontData& fontdata)
791 {
792 m_fontData = fontdata;
793 }
794
795 bool wxFontDialog::IsShown() const
796 {
797 return false;
798 }
799
800 int wxFontDialog::ShowModal()
801 {
802 return wxDialog::ShowModal();
803 }
804
805 void wxFontDialog::OnPanelClose()
806 {
807 }
808
809 const wxChar *FontFamilyIntToString(int family)
810 {
811 switch (family)
812 {
813 case wxROMAN:
814 return _("<Any Roman>");
815 case wxDECORATIVE:
816 return _("<Any Decorative>");
817 case wxMODERN:
818 return _("<Any Modern>");
819 case wxSCRIPT:
820 return _("<Any Script>");
821 case wxTELETYPE:
822 return _("<Any Teletype>");
823 case wxSWISS:
824 default:
825 return _("<Any Swiss>");
826 }
827 }
828
829 int FontFamilyStringToInt(const wxChar *family)
830 {
831 if (!family)
832 return wxSWISS;
833
834 if (wxStrcmp(family, _("<Any Roman>")) == 0)
835 return wxROMAN;
836 else if (wxStrcmp(family, _("<Any Decorative>")) == 0)
837 return wxDECORATIVE;
838 else if (wxStrcmp(family, _("<Any Modern>")) == 0)
839 return wxMODERN;
840 else if (wxStrcmp(family, _("<Any Script>")) == 0)
841 return wxSCRIPT;
842 else if (wxStrcmp(family, _("<Any Teletype>")) == 0)
843 return wxTELETYPE;
844 else if (wxStrcmp(family, _("<Any Swiss>")) == 0)
845 return wxSWISS;
846 else return -1;
847 }
848
849 #endif // !USE_NATIVE_FONT_DIALOG_FOR_MACOSX
850
851 #endif // wxOSX_USE_EXPERIMENTAL_FONTDIALOG
852
853 #endif // wxUSE_FONTDLG