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