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