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