1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/fontdlgg.cpp
3 // Purpose: Generic font dialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "fontdlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_FONTDLG && (!defined(__WXGTK__) || defined(__WXGPE__) || defined(__WXUNIVERSAL__))
28 #include "wx/dialog.h"
29 #include "wx/listbox.h"
30 #include "wx/button.h"
31 #include "wx/stattext.h"
32 #include "wx/layout.h"
33 #include "wx/dcclient.h"
34 #include "wx/choice.h"
35 #include "wx/checkbox.h"
42 #include "wx/cmndata.h"
44 #include "wx/fontdlg.h"
45 #include "wx/settings.h"
47 //-----------------------------------------------------------------------------
48 // helper class - wxFontPreviewer
49 //-----------------------------------------------------------------------------
51 class WXDLLEXPORT wxFontPreviewer
: public wxWindow
54 wxFontPreviewer(wxWindow
*parent
) : wxWindow(parent
, -1) {}
57 void OnPaint(wxPaintEvent
& event
);
61 BEGIN_EVENT_TABLE(wxFontPreviewer
, wxWindow
)
62 EVT_PAINT(wxFontPreviewer::OnPaint
)
65 void wxFontPreviewer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
69 wxSize size
= GetSize();
70 wxFont font
= GetFont();
72 dc
.SetPen(*wxBLACK_PEN
);
73 dc
.SetBrush(*wxWHITE_BRUSH
);
74 dc
.DrawRectangle(0, 0, size
.x
, size
.y
);
79 // Calculate vertical centre
81 dc
.GetTextExtent( wxT("X"), &w
, &h
);
82 dc
.SetTextForeground(GetForegroundColour());
83 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
84 dc
.DrawText(_("ABCDEFGabcdefg12345"),
86 dc
.DestroyClippingRegion();
90 //-----------------------------------------------------------------------------
91 // wxGenericFontDialog
92 //-----------------------------------------------------------------------------
94 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
96 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
97 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
98 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
99 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
100 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
101 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
102 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
103 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
108 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
114 wxT("MEDIUM VIOLET RED"),
119 wxT("MEDIUM SPRING GREEN"),
122 wxT("LIGHT STEEL BLUE"),
124 wxT("LIGHT MAGENTA"),
142 wxT("MEDIUM VIOLET RED"),
146 wxT("MEDIUM SEA GREEN"),
148 wxT("MIDNIGHT BLUE"),
154 wxT("MEDIUM FOREST GREEN"),
159 wxT("MEDIUM SLATE BLUE"),
164 * Generic wxFontDialog
167 void wxGenericFontDialog::Init()
174 wxGenericFontDialog::~wxGenericFontDialog()
178 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
180 EndModal(wxID_CANCEL
);
183 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
185 if ( !wxDialog::Create( parent
, -1 , _T("Choose Font") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
188 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
195 // sets initial font in preview area
196 wxCommandEvent dummy
;
202 int wxGenericFontDialog::ShowModal()
204 int ret
= wxDialog::ShowModal();
206 if (ret
!= wxID_CANCEL
)
208 m_fontData
.m_chosenFont
= dialogFont
;
214 void wxGenericFontDialog::CreateWidgets()
217 *families
= new wxString
[6],
218 *styles
= new wxString
[3],
219 *weights
= new wxString
[3];
220 families
[0] = _("Roman");
221 families
[1] = _("Decorative");
222 families
[2] = _("Modern");
223 families
[3] = _("Script");
224 families
[4] = _("Swiss" );
225 families
[5] = _("Teletype" );
226 styles
[0] = _("Normal");
227 styles
[1] = _("Italic");
228 styles
[2] = _("Slant");
229 weights
[0] = _("Normal");
230 weights
[1] = _("Light");
231 weights
[2] = _("Bold");
233 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
);
234 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
);
235 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
);
237 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxDefaultPosition
, wxDefaultSize
, NUM_COLS
, wxColourDialogNames
);
239 wxString
*pointSizes
= new wxString
[40];
241 for ( i
= 0; i
< 40; i
++)
244 wxSprintf(buf
, wxT("%d"), i
+ 1);
248 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
);
249 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, _("Underline"));
251 m_previewer
= new wxFontPreviewer(this);
253 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"));
254 wxButton
*cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
256 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
257 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
258 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
259 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
260 colourChoice
->SetStringSelection(name
);
262 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
263 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
265 okButton
->SetDefault();
269 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
272 wxSizer
*topsizer
, *sizer
;
273 topsizer
= new wxBoxSizer(wxVERTICAL
);
278 sizer
= new wxBoxSizer(wxHORIZONTAL
);
279 sizer
->Add(familyChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
280 sizer
->Add(styleChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
281 sizer
->Add(weightChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
282 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
284 sizer
= new wxBoxSizer(wxHORIZONTAL
);
285 sizer
->Add(colourChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
286 sizer
->Add(pointSizeChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
287 sizer
->Add(underLineCheckBox
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
288 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
293 sizer
= new wxBoxSizer(wxHORIZONTAL
);
294 sizer
->Add(familyChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
295 sizer
->Add(styleChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
296 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
298 sizer
= new wxBoxSizer(wxHORIZONTAL
);
299 sizer
->Add(weightChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
300 sizer
->Add(colourChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
301 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
303 sizer
= new wxBoxSizer(wxHORIZONTAL
);
304 sizer
->Add(pointSizeChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
305 sizer
->Add(underLineCheckBox
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
306 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
309 topsizer
->Add(m_previewer
, 1, wxALL
| wxEXPAND
, 10);
310 topsizer
->SetItemMinSize(m_previewer
, 430, 100);
312 sizer
= new wxBoxSizer(wxHORIZONTAL
);
313 sizer
->Add(okButton
, 0, wxRIGHT
, 10);
314 sizer
->Add(cancelButton
, 0, wxRIGHT
, 10);
315 topsizer
->Add(sizer
, 0, wxALIGN_RIGHT
| wxBOTTOM
, 10);
319 topsizer
->SetSizeHints(this);
329 // Don't block events any more
333 void wxGenericFontDialog::InitializeFont()
335 int fontFamily
= wxSWISS
;
336 int fontWeight
= wxNORMAL
;
337 int fontStyle
= wxNORMAL
;
339 int fontUnderline
= FALSE
;
341 if (m_fontData
.m_initialFont
.Ok())
343 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
344 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
345 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
346 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
347 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
350 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
353 m_previewer
->SetFont(dialogFont
);
356 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
358 if (!m_useEvents
) return;
360 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
361 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
362 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
363 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
364 int fontUnderline
= underLineCheckBox
->GetValue();
366 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
367 m_previewer
->SetFont(dialogFont
);
368 if (colourChoice
->GetStringSelection() != wxT(""))
370 wxColour col
= wxTheColourDatabase
->Find(colourChoice
->GetStringSelection());
373 m_fontData
.m_fontColour
= col
;
374 m_previewer
->SetForegroundColour(col
);
377 m_previewer
->Refresh();
380 const wxChar
*wxFontWeightIntToString(int weight
)
390 return wxT("Normal");
394 const wxChar
*wxFontStyleIntToString(int style
)
399 return wxT("Italic");
404 return wxT("Normal");
408 const wxChar
*wxFontFamilyIntToString(int family
)
415 return wxT("Decorative");
417 return wxT("Modern");
419 return wxT("Script");
421 return wxT("Teletype");
428 int wxFontFamilyStringToInt(wxChar
*family
)
433 if (wxStrcmp(family
, wxT("Roman")) == 0)
435 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
437 else if (wxStrcmp(family
, wxT("Modern")) == 0)
439 else if (wxStrcmp(family
, wxT("Script")) == 0)
441 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
446 int wxFontStyleStringToInt(wxChar
*style
)
450 if (wxStrcmp(style
, wxT("Italic")) == 0)
452 else if (wxStrcmp(style
, wxT("Slant")) == 0)
458 int wxFontWeightStringToInt(wxChar
*weight
)
462 if (wxStrcmp(weight
, wxT("Bold")) == 0)
464 else if (wxStrcmp(weight
, wxT("Light")) == 0)