]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/fontdlg.cpp
Forgot header with OSX prefix
[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
45 #if wxOSX_USE_EXPERIMENTAL_FONTDIALOG
46
47 IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
48
49 #include "wx/osx/private.h"
50
51 // ---------------------------------------------------------------------------
52 // wxFontDialog
53 // ---------------------------------------------------------------------------
54
55 #if wxOSX_USE_CARBON
56
57 static const EventTypeSpec eventList[] =
58 {
59 { kEventClassFont, kEventFontSelection } ,
60 } ;
61
62
63 pascal OSStatus
64 wxMacCarbonFontPanelHandler(EventHandlerCallRef WXUNUSED(nextHandler),
65 EventRef event,
66 void *userData)
67 {
68 OSStatus result = eventNotHandledErr ;
69 wxFontDialog *fontdialog = (wxFontDialog*) userData ;
70 wxFontData& fontdata= fontdialog->GetFontData() ;
71
72 wxMacCarbonEvent cEvent( event );
73 switch(cEvent.GetKind())
74 {
75 case kEventFontSelection :
76 {
77 bool setup = false ;
78 #if wxOSX_USE_CORE_TEXT
79 if ( UMAGetSystemVersion() >= 0x1050 )
80 {
81 CTFontDescriptorRef descr;
82 if ( cEvent.GetParameter<CTFontDescriptorRef>( kEventParamCTFontDescriptor, typeCTFontDescriptorRef, &descr ) == noErr )
83 {
84 wxFont font;
85 font.MacCreateFromCTFontDescriptor(descr);
86 fontdata.SetChosenFont( font ) ;
87 setup = true;
88 }
89 }
90 #endif
91 #if wxOSX_USE_ATSU_TEXT
92 ATSUFontID fontId = 0 ;
93 if ( !setup && (cEvent.GetParameter<ATSUFontID>(kEventParamATSUFontID, &fontId) == noErr) )
94 {
95 FMFontStyle fontStyle = cEvent.GetParameter<FMFontStyle>(kEventParamFMFontStyle);
96 FMFontSize fontSize = cEvent.GetParameter<FMFontSize>(kEventParamFMFontSize);
97
98 CFStringRef cfName = NULL;
99 #if 1
100 FMFontFamily fontFamily = cEvent.GetParameter<FMFontFamily>(kEventParamFMFontFamily);
101 ATSFontFamilyRef atsfontfamilyref = FMGetATSFontFamilyRefFromFontFamily( fontFamily ) ;
102 OSStatus err = ATSFontFamilyGetName( atsfontfamilyref , kATSOptionFlagsDefault , &cfName ) ;
103 if ( err == noErr )
104 {
105 wxFAIL_MSG("ATSFontFamilyGetName failed");
106 }
107 #else
108 // we don't use the ATSU naming anymore
109 ByteCount actualLength = 0;
110 char *c = NULL;
111 OSStatus err = ATSUFindFontName(fontId , kFontFamilyName, kFontUnicodePlatform, kFontNoScriptCode,
112 kFontNoLanguageCode , 0 , NULL , &actualLength , NULL );
113 if ( err == noErr)
114 {
115 actualLength += 1 ;
116 char *c = (char*)malloc( actualLength );
117 err = ATSUFindFontName(fontId, kFontFamilyName, kFontUnicodePlatform, kFontNoScriptCode,
118 kFontNoLanguageCode, actualLength, c , NULL, NULL);
119 cfName = CFStringCreateWithCharacters(NULL, (UniChar*) c, (actualLength-1) >> 1);
120 }
121 else
122 {
123 err = ATSUFindFontName(fontId , kFontFamilyName, kFontNoPlatformCode, kFontNoScriptCode,
124 kFontNoLanguageCode , 0 , NULL , &actualLength , NULL );
125 if ( err == noErr )
126 {
127 actualLength += 1 ;
128 c = (char*)malloc(actualLength);
129 err = ATSUFindFontName(fontId, kFontFamilyName, kFontNoPlatformCode, kFontNoScriptCode,
130 kFontNoLanguageCode, actualLength, c , NULL, NULL);
131 c[actualLength-1] = 0;
132 cfName = CFStringCreateWithCString(NULL, c, kCFStringEncodingMacRoman );
133 }
134 }
135 if ( c!=NULL )
136 free(c);
137 #endif
138 if ( cfName!=NULL )
139 {
140 fontdata.m_chosenFont.SetFaceName(wxCFStringRef(cfName).AsString(wxLocale::GetSystemEncoding()));
141 fontdata.m_chosenFont.SetPointSize(fontSize);
142 fontdata.m_chosenFont.SetStyle(fontStyle & italic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL);
143 fontdata.m_chosenFont.SetUnderlined((fontStyle & underline)!=0);
144 fontdata.m_chosenFont.SetWeight(fontStyle & bold ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL);
145 }
146 }
147 #endif // wxOSX_USE_ATSU_TEXT
148
149 // retrieving the color
150 RGBColor fontColor ;
151 if ( cEvent.GetParameter<RGBColor>(kEventParamFontColor, &fontColor) == noErr )
152 {
153 fontdata.m_fontColour = fontColor;
154 }
155 else
156 {
157 CFDictionaryRef dict ;
158 if ( cEvent.GetParameter<CFDictionaryRef>(kEventParamDictionary, &dict) == noErr )
159 {
160 CFDictionaryRef attributesDict ;
161 if ( CFDictionaryGetValueIfPresent(dict, kFontPanelAttributesKey, (const void **)&attributesDict) )
162 {
163 CFDataRef tagsData;
164 CFDataRef sizesData;
165 CFDataRef valuesData;
166 if ( CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeTagsKey, (const void **)&tagsData) &&
167 CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeSizesKey, (const void **)&sizesData) &&
168 CFDictionaryGetValueIfPresent(attributesDict, kFontPanelAttributeValuesKey, (const void **)&valuesData) )
169 {
170 ItemCount count = CFDataGetLength(tagsData)/sizeof(ATSUAttributeTag);
171 ATSUAttributeTag *tagPtr = (ATSUAttributeTag *)CFDataGetBytePtr(tagsData);
172 ByteCount *sizePtr = (ByteCount *)CFDataGetBytePtr(sizesData);
173 UInt32 *bytePtr = (UInt32*)CFDataGetBytePtr(valuesData);
174 ATSUAttributeValuePtr valuesPtr = bytePtr ;
175 for ( ItemCount i = 0 ; i < count ; ++i)
176 {
177 if ( tagPtr[i] == kATSUColorTag && sizePtr[i] == sizeof(RGBColor))
178 {
179 fontdata.m_fontColour = *(RGBColor *)valuesPtr;
180 break ;
181 }
182 bytePtr = (UInt32*)( (UInt8*)bytePtr + sizePtr[i]);
183 }
184 }
185 }
186 }
187 }
188 }
189 break ;
190 }
191
192 return result ;
193 }
194
195 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacCarbonFontPanelHandler )
196 #endif
197
198 wxFontDialog::wxFontDialog()
199 {
200 }
201
202 wxFontDialog::wxFontDialog(wxWindow *parent, const wxFontData& data)
203 {
204 Create(parent, data);
205 }
206
207 wxFontDialog::~wxFontDialog()
208 {
209 }
210
211 bool wxFontDialog::Create(wxWindow *WXUNUSED(parent), const wxFontData& data)
212 {
213 m_fontData = data;
214 return true ;
215 }
216
217 int wxFontDialog::ShowModal()
218 {
219 #if wxOSX_USE_CARBON
220
221 OSStatus err ;
222 wxFont font = *wxNORMAL_FONT ;
223 if ( m_fontData.m_initialFont.Ok() )
224 {
225 font = m_fontData.m_initialFont ;
226 }
227
228 bool setup = false;
229 #if wxOSX_USE_CORE_TEXT
230 if ( UMAGetSystemVersion() >= 0x1050 )
231 {
232 CTFontDescriptorRef descr = (CTFontDescriptorRef) CTFontCopyFontDescriptor( (CTFontRef) font.MacGetCTFont() );
233 err = SetFontInfoForSelection (kFontSelectionCoreTextType,1, &descr , NULL);
234 CFRelease( descr );
235 setup = true;
236 }
237 #endif
238 #if wxOSX_USE_ATSU_TEXT
239 if ( !setup )
240 {
241 ATSUStyle style = (ATSUStyle)font.MacGetATSUStyle();
242 err = SetFontInfoForSelection (kFontSelectionATSUIType,1, &style , NULL);
243 setup = true;
244 }
245 #endif
246 // just clicking on ENTER will not send us any font setting event, therefore we have to make sure
247 // that field is already correct
248 m_fontData.m_chosenFont = font ;
249
250 EventHandlerRef handler ;
251
252 err = InstallApplicationEventHandler( GetwxMacCarbonFontPanelHandlerUPP(), GetEventTypeCount(eventList), eventList, this , &handler );
253
254 if ( !FPIsFontPanelVisible() )
255 FPShowHideFontPanel();
256 #endif
257 int retval = RunMixedFontDialog(this);
258
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:
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
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.Ok() )
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
343 void SetColour(const wxColour& colour) { m_colour = colour; SetBackgroundColour(m_colour); }
344
345 wxColour& GetColour() { return m_colour; }
346
347 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
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
372 wxFontColourSwatchCtrl::~wxFontColourSwatchCtrl()
373 {
374 }
375
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_COMMAND_BUTTON_CLICKED, GetId());
402 HandleWindowEvent(event);
403 }
404 }
405
406 /*!
407 * wxFontDialog type definition
408 */
409
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
436 wxFontDialog::wxFontDialog(wxWindow* parent, const wxFontData& fontData)
437 {
438 m_dialogParent = NULL;
439
440 Create(parent, fontData);
441 }
442
443 wxFontDialog::~wxFontDialog()
444 {
445 // empty
446 }
447
448 /*!
449 * wxFontDialog creator
450 */
451
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
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|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, _T("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)
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().Ok())
604 {
605 m_previewCtrl->SetForegroundColour(m_fontData.GetColour());
606 }
607 m_previewCtrl->Refresh();
608 }
609
610 /*!
611 * wxEVT_COMMAND_SPINCTRL_UPDATED event handler for wxID_FONTDIALOG_FONTSIZE
612 */
613
614 void wxFontDialog::OnFontdialogFontsizeUpdated( wxSpinEvent& WXUNUSED(event) )
615 {
616 ChangeFont();
617 }
618
619 /*!
620 * wxEVT_COMMAND_TEXT_UPDATED event handler for wxID_FONTDIALOG_FONTSIZE
621 */
622
623 void wxFontDialog::OnFontdialogFontsizeTextUpdated( wxCommandEvent& WXUNUSED(event) )
624 {
625 ChangeFont();
626 }
627
628 /*!
629 * wxEVT_COMMAND_CHECKBOX_CLICKED event handler for wxID_FONTDIALOG_BOLD
630 */
631
632 void wxFontDialog::OnFontdialogBoldClick( wxCommandEvent& WXUNUSED(event) )
633 {
634 ChangeFont();
635 }
636
637 /*!
638 * wxEVT_COMMAND_CHECKBOX_CLICKED event handler for wxID_FONTDIALOG_ITALIC
639 */
640
641 void wxFontDialog::OnFontdialogItalicClick( wxCommandEvent& WXUNUSED(event) )
642 {
643 ChangeFont();
644 }
645
646 /*!
647 * wxEVT_COMMAND_CHECKBOX_CLICKED event handler for wxID_FONTDIALOG_UNDERLINED
648 */
649
650 void wxFontDialog::OnFontdialogUnderlinedClick( wxCommandEvent& WXUNUSED(event) )
651 {
652 ChangeFont();
653 }
654
655 /*!
656 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
657 */
658
659 void wxFontDialog::OnOkClick( wxCommandEvent& event )
660 {
661 event.Skip();
662 }
663
664
665 /*!
666 * wxEVT_COMMAND_LISTBOX_SELECTED event handler for wxID_FONTDIALOG_FACENAME
667 */
668
669 void wxFontDialog::OnFontdialogFacenameSelected( wxCommandEvent& WXUNUSED(event) )
670 {
671 ChangeFont();
672 }
673
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
685 bool wxFontDialog::ShowToolTips()
686 {
687 return true;
688 }
689
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.Ok())
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
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().Ok())
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
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
773 void wxFontDialog::SetData(const wxFontData& fontdata)
774 {
775 m_fontData = fontdata;
776 }
777
778 bool wxFontDialog::IsShown() const
779 {
780 return false;
781 }
782
783 int wxFontDialog::ShowModal()
784 {
785 return wxDialog::ShowModal();
786 }
787
788 void wxFontDialog::OnPanelClose()
789 {
790 }
791
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
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