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