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