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