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