1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Generic font dialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
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/generic/fontdlgg.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
);
77 dc
.SetFont(GetFont());
78 // Calculate vertical centre
80 dc
.GetTextExtent("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 wxGenericFontDialog::wxGenericFontDialog(void)
173 wxGenericFontDialog::wxGenericFontDialog(wxWindow
*parent
, const wxFontData
& data
):
174 wxDialog(parent
, -1, _("Font"), wxDefaultPosition
, wxDefaultSize
,
175 wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
|wxRESIZE_BORDER
)
179 Create(parent
, data
);
182 wxGenericFontDialog::~wxGenericFontDialog(void)
186 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
188 EndModal(wxID_CANCEL
);
191 bool wxGenericFontDialog::Create(wxWindow
*parent
, const wxFontData
& data
)
193 dialogParent
= parent
;
203 int wxGenericFontDialog::ShowModal(void)
205 int ret
= wxDialog::ShowModal();
207 if (ret
!= wxID_CANCEL
)
209 fontData
.chosenFont
= dialogFont
;
215 void wxGenericFontDialog::CreateWidgets(void)
220 *families
= new wxString
[6],
221 *styles
= new wxString
[3],
222 *weights
= new wxString
[3];
223 families
[0] = _("Roman");
224 families
[1] = _("Decorative");
225 families
[2] = _("Modern");
226 families
[3] = _("Script");
227 families
[4] = _("Swiss" );
228 families
[5] = _("Teletype" );
229 styles
[0] = _("Normal");
230 styles
[1] = _("Italic");
231 styles
[2] = _("Slant");
232 weights
[0] = _("Normal");
233 weights
[1] = _("Light");
234 weights
[2] = _("Bold");
236 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
);
237 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
);
238 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
);
240 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxDefaultPosition
, wxDefaultSize
, NUM_COLS
, wxColourDialogNames
);
242 wxString
*pointSizes
= new wxString
[40];
244 for ( i
= 0; i
< 40; i
++)
247 sprintf(buf
, "%d", i
+ 1);
251 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
);
252 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, _("Underline"));
254 m_previewer
= new wxFontPreviewer(this);
256 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"));
257 wxButton
*cancelButton
= new wxButton(this, wxID_OK
, _("Cancel"));
259 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
260 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
261 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
262 wxString
name(wxTheColourDatabase
->FindName(fontData
.fontColour
));
263 colourChoice
->SetStringSelection(name
);
265 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
266 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
268 okButton
->SetDefault();
270 wxSizer
*topsizer
, *sizer
;
271 topsizer
= new wxBoxSizer(wxVERTICAL
);
273 sizer
= new wxBoxSizer(wxHORIZONTAL
);
274 sizer
->Add(familyChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
275 sizer
->Add(styleChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
276 sizer
->Add(weightChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
277 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
279 sizer
= new wxBoxSizer(wxHORIZONTAL
);
280 sizer
->Add(colourChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
281 sizer
->Add(pointSizeChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
282 sizer
->Add(underLineCheckBox
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
283 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
285 topsizer
->Add(m_previewer
, 1, wxALL
| wxEXPAND
, 10);
286 topsizer
->SetItemMinSize(m_previewer
, 430, 100);
288 sizer
= new wxBoxSizer(wxHORIZONTAL
);
289 sizer
->Add(okButton
, 0, wxRIGHT
, 10);
290 sizer
->Add(cancelButton
, 0, wxRIGHT
, 10);
291 topsizer
->Add(sizer
, 0, wxALIGN_RIGHT
| wxBOTTOM
, 10);
295 topsizer
->SetSizeHints(this);
307 void wxGenericFontDialog::InitializeFont(void)
309 int fontFamily
= wxSWISS
;
310 int fontWeight
= wxNORMAL
;
311 int fontStyle
= wxNORMAL
;
313 int fontUnderline
= FALSE
;
315 if (fontData
.initialFont
.Ok())
317 fontFamily
= fontData
.initialFont
.GetFamily();
318 fontWeight
= fontData
.initialFont
.GetWeight();
319 fontStyle
= fontData
.initialFont
.GetStyle();
320 fontSize
= fontData
.initialFont
.GetPointSize();
321 fontUnderline
= fontData
.initialFont
.GetUnderlined();
324 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
327 m_previewer
->SetFont(dialogFont
);
330 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
332 if (!m_useEvents
) return;
334 int fontFamily
= 0; /* shut up buggy egcs warnings */
335 fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
337 fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
339 fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
340 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
341 int fontUnderline
= underLineCheckBox
->GetValue();
343 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
344 m_previewer
->SetFont(dialogFont
);
345 if (colourChoice
->GetStringSelection() != wxT(""))
347 wxColour
*col
= (wxColour
*) NULL
;
348 col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
351 fontData
.fontColour
= *col
;
352 m_previewer
->SetForegroundColour(*col
);
355 m_previewer
->Refresh();
358 const wxChar
*wxFontWeightIntToString(int weight
)
368 return wxT("Normal");
372 const wxChar
*wxFontStyleIntToString(int style
)
377 return wxT("Italic");
382 return wxT("Normal");
386 const wxChar
*wxFontFamilyIntToString(int family
)
393 return wxT("Decorative");
395 return wxT("Modern");
397 return wxT("Script");
399 return wxT("Teletype");
406 int wxFontFamilyStringToInt(wxChar
*family
)
411 if (wxStrcmp(family
, wxT("Roman")) == 0)
413 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
415 else if (wxStrcmp(family
, wxT("Modern")) == 0)
417 else if (wxStrcmp(family
, wxT("Script")) == 0)
419 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
424 int wxFontStyleStringToInt(wxChar
*style
)
428 if (wxStrcmp(style
, wxT("Italic")) == 0)
430 else if (wxStrcmp(style
, wxT("Slant")) == 0)
436 int wxFontWeightStringToInt(wxChar
*weight
)
440 if (wxStrcmp(weight
, wxT("Bold")) == 0)
442 else if (wxStrcmp(weight
, wxT("Light")) == 0)