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(__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"
46 //-----------------------------------------------------------------------------
47 // helper class - wxFontPreviewer
48 //-----------------------------------------------------------------------------
50 class WXDLLEXPORT wxFontPreviewer
: public wxWindow
53 wxFontPreviewer(wxWindow
*parent
) : wxWindow(parent
, -1) {}
56 void OnPaint(wxPaintEvent
& event
);
60 BEGIN_EVENT_TABLE(wxFontPreviewer
, wxWindow
)
61 EVT_PAINT(wxFontPreviewer::OnPaint
)
64 void wxFontPreviewer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
68 wxSize size
= GetSize();
69 wxFont font
= GetFont();
71 dc
.SetPen(*wxBLACK_PEN
);
72 dc
.SetBrush(*wxWHITE_BRUSH
);
73 dc
.DrawRectangle(0, 0, size
.x
, size
.y
);
78 // Calculate vertical centre
80 dc
.GetTextExtent( wxT("X"), &w
, &h
);
81 dc
.SetTextForeground(GetForegroundColour());
82 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
83 dc
.DrawText(_("ABCDEFGabcdefg12345"),
85 dc
.DestroyClippingRegion();
89 //-----------------------------------------------------------------------------
90 // wxGenericFontDialog
91 //-----------------------------------------------------------------------------
93 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
95 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
96 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
97 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
98 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
99 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
100 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
101 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
102 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
107 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
113 wxT("MEDIUM VIOLET RED"),
118 wxT("MEDIUM SPRING GREEN"),
121 wxT("LIGHT STEEL BLUE"),
123 wxT("LIGHT MAGENTA"),
141 wxT("MEDIUM VIOLET RED"),
145 wxT("MEDIUM SEA GREEN"),
147 wxT("MIDNIGHT BLUE"),
153 wxT("MEDIUM FOREST GREEN"),
158 wxT("MEDIUM SLATE BLUE"),
163 * Generic wxFontDialog
166 void wxGenericFontDialog::Init()
173 wxGenericFontDialog::~wxGenericFontDialog()
177 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
179 EndModal(wxID_CANCEL
);
182 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
184 if ( !wxDialog::Create( parent
, -1 , _T("Choose Font") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
187 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
194 // sets initial font in preview area
195 wxCommandEvent dummy
;
201 int wxGenericFontDialog::ShowModal()
203 int ret
= wxDialog::ShowModal();
205 if (ret
!= wxID_CANCEL
)
207 m_fontData
.m_chosenFont
= dialogFont
;
213 void wxGenericFontDialog::CreateWidgets()
218 *families
= new wxString
[6],
219 *styles
= new wxString
[3],
220 *weights
= new wxString
[3];
221 families
[0] = _("Roman");
222 families
[1] = _("Decorative");
223 families
[2] = _("Modern");
224 families
[3] = _("Script");
225 families
[4] = _("Swiss" );
226 families
[5] = _("Teletype" );
227 styles
[0] = _("Normal");
228 styles
[1] = _("Italic");
229 styles
[2] = _("Slant");
230 weights
[0] = _("Normal");
231 weights
[1] = _("Light");
232 weights
[2] = _("Bold");
234 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
);
235 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
);
236 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
);
238 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxDefaultPosition
, wxDefaultSize
, NUM_COLS
, wxColourDialogNames
);
240 wxString
*pointSizes
= new wxString
[40];
242 for ( i
= 0; i
< 40; i
++)
245 wxSprintf(buf
, wxT("%d"), i
+ 1);
249 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
);
250 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, _("Underline"));
252 m_previewer
= new wxFontPreviewer(this);
254 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"));
255 wxButton
*cancelButton
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
257 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
258 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
259 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
260 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.GetColour()));
261 colourChoice
->SetStringSelection(name
);
263 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
264 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
266 okButton
->SetDefault();
268 wxSizer
*topsizer
, *sizer
;
269 topsizer
= new wxBoxSizer(wxVERTICAL
);
271 sizer
= new wxBoxSizer(wxHORIZONTAL
);
272 sizer
->Add(familyChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
273 sizer
->Add(styleChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
274 sizer
->Add(weightChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
275 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
277 sizer
= new wxBoxSizer(wxHORIZONTAL
);
278 sizer
->Add(colourChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
279 sizer
->Add(pointSizeChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
280 sizer
->Add(underLineCheckBox
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
281 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
283 topsizer
->Add(m_previewer
, 1, wxALL
| wxEXPAND
, 10);
284 topsizer
->SetItemMinSize(m_previewer
, 430, 100);
286 sizer
= new wxBoxSizer(wxHORIZONTAL
);
287 sizer
->Add(okButton
, 0, wxRIGHT
, 10);
288 sizer
->Add(cancelButton
, 0, wxRIGHT
, 10);
289 topsizer
->Add(sizer
, 0, wxALIGN_RIGHT
| wxBOTTOM
, 10);
293 topsizer
->SetSizeHints(this);
305 void wxGenericFontDialog::InitializeFont()
307 int fontFamily
= wxSWISS
;
308 int fontWeight
= wxNORMAL
;
309 int fontStyle
= wxNORMAL
;
311 int fontUnderline
= FALSE
;
313 if (m_fontData
.m_initialFont
.Ok())
315 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
316 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
317 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
318 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
319 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
322 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
325 m_previewer
->SetFont(dialogFont
);
328 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
330 if (!m_useEvents
) return;
332 int fontFamily
= 0; /* shut up buggy egcs warnings */
333 fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
335 fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
337 fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
338 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
339 int fontUnderline
= underLineCheckBox
->GetValue();
341 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
342 m_previewer
->SetFont(dialogFont
);
343 if (colourChoice
->GetStringSelection() != wxT(""))
345 wxColour
*col
= (wxColour
*) NULL
;
346 col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
349 m_fontData
.m_fontColour
= *col
;
350 m_previewer
->SetForegroundColour(*col
);
353 m_previewer
->Refresh();
356 const wxChar
*wxFontWeightIntToString(int weight
)
366 return wxT("Normal");
370 const wxChar
*wxFontStyleIntToString(int style
)
375 return wxT("Italic");
380 return wxT("Normal");
384 const wxChar
*wxFontFamilyIntToString(int family
)
391 return wxT("Decorative");
393 return wxT("Modern");
395 return wxT("Script");
397 return wxT("Teletype");
404 int wxFontFamilyStringToInt(wxChar
*family
)
409 if (wxStrcmp(family
, wxT("Roman")) == 0)
411 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
413 else if (wxStrcmp(family
, wxT("Modern")) == 0)
415 else if (wxStrcmp(family
, wxT("Script")) == 0)
417 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
422 int wxFontStyleStringToInt(wxChar
*style
)
426 if (wxStrcmp(style
, wxT("Italic")) == 0)
428 else if (wxStrcmp(style
, wxT("Slant")) == 0)
434 int wxFontWeightStringToInt(wxChar
*weight
)
438 if (wxStrcmp(weight
, wxT("Bold")) == 0)
440 else if (wxStrcmp(weight
, wxT("Light")) == 0)