1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/fontdlgg.cpp
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/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
);
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 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") );
197 int wxGenericFontDialog::ShowModal()
199 int ret
= wxDialog::ShowModal();
201 if (ret
!= wxID_CANCEL
)
203 m_fontData
.chosenFont
= dialogFont
;
209 void wxGenericFontDialog::CreateWidgets()
214 *families
= new wxString
[6],
215 *styles
= new wxString
[3],
216 *weights
= new wxString
[3];
217 families
[0] = _("Roman");
218 families
[1] = _("Decorative");
219 families
[2] = _("Modern");
220 families
[3] = _("Script");
221 families
[4] = _("Swiss" );
222 families
[5] = _("Teletype" );
223 styles
[0] = _("Normal");
224 styles
[1] = _("Italic");
225 styles
[2] = _("Slant");
226 weights
[0] = _("Normal");
227 weights
[1] = _("Light");
228 weights
[2] = _("Bold");
230 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
);
231 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
);
232 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
);
234 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxDefaultPosition
, wxDefaultSize
, NUM_COLS
, wxColourDialogNames
);
236 wxString
*pointSizes
= new wxString
[40];
238 for ( i
= 0; i
< 40; i
++)
241 sprintf(buf
, "%d", i
+ 1);
245 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
);
246 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, _("Underline"));
248 m_previewer
= new wxFontPreviewer(this);
250 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"));
251 wxButton
*cancelButton
= new wxButton(this, wxID_OK
, _("Cancel"));
253 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
254 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
255 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
256 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.fontColour
));
257 colourChoice
->SetStringSelection(name
);
259 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
260 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
262 okButton
->SetDefault();
264 wxSizer
*topsizer
, *sizer
;
265 topsizer
= new wxBoxSizer(wxVERTICAL
);
267 sizer
= new wxBoxSizer(wxHORIZONTAL
);
268 sizer
->Add(familyChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
269 sizer
->Add(styleChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
270 sizer
->Add(weightChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
271 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
273 sizer
= new wxBoxSizer(wxHORIZONTAL
);
274 sizer
->Add(colourChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
275 sizer
->Add(pointSizeChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
276 sizer
->Add(underLineCheckBox
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
277 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
279 topsizer
->Add(m_previewer
, 1, wxALL
| wxEXPAND
, 10);
280 topsizer
->SetItemMinSize(m_previewer
, 430, 100);
282 sizer
= new wxBoxSizer(wxHORIZONTAL
);
283 sizer
->Add(okButton
, 0, wxRIGHT
, 10);
284 sizer
->Add(cancelButton
, 0, wxRIGHT
, 10);
285 topsizer
->Add(sizer
, 0, wxALIGN_RIGHT
| wxBOTTOM
, 10);
289 topsizer
->SetSizeHints(this);
301 void wxGenericFontDialog::InitializeFont()
303 int fontFamily
= wxSWISS
;
304 int fontWeight
= wxNORMAL
;
305 int fontStyle
= wxNORMAL
;
307 int fontUnderline
= FALSE
;
309 if (m_fontData
.initialFont
.Ok())
311 fontFamily
= m_fontData
.initialFont
.GetFamily();
312 fontWeight
= m_fontData
.initialFont
.GetWeight();
313 fontStyle
= m_fontData
.initialFont
.GetStyle();
314 fontSize
= m_fontData
.initialFont
.GetPointSize();
315 fontUnderline
= m_fontData
.initialFont
.GetUnderlined();
318 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
321 m_previewer
->SetFont(dialogFont
);
324 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
326 if (!m_useEvents
) return;
328 int fontFamily
= 0; /* shut up buggy egcs warnings */
329 fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
331 fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
333 fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
334 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
335 int fontUnderline
= underLineCheckBox
->GetValue();
337 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
338 m_previewer
->SetFont(dialogFont
);
339 if (colourChoice
->GetStringSelection() != wxT(""))
341 wxColour
*col
= (wxColour
*) NULL
;
342 col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
345 m_fontData
.fontColour
= *col
;
346 m_previewer
->SetForegroundColour(*col
);
349 m_previewer
->Refresh();
352 const wxChar
*wxFontWeightIntToString(int weight
)
362 return wxT("Normal");
366 const wxChar
*wxFontStyleIntToString(int style
)
371 return wxT("Italic");
376 return wxT("Normal");
380 const wxChar
*wxFontFamilyIntToString(int family
)
387 return wxT("Decorative");
389 return wxT("Modern");
391 return wxT("Script");
393 return wxT("Teletype");
400 int wxFontFamilyStringToInt(wxChar
*family
)
405 if (wxStrcmp(family
, wxT("Roman")) == 0)
407 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
409 else if (wxStrcmp(family
, wxT("Modern")) == 0)
411 else if (wxStrcmp(family
, wxT("Script")) == 0)
413 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
418 int wxFontStyleStringToInt(wxChar
*style
)
422 if (wxStrcmp(style
, wxT("Italic")) == 0)
424 else if (wxStrcmp(style
, wxT("Slant")) == 0)
430 int wxFontWeightStringToInt(wxChar
*weight
)
434 if (wxStrcmp(weight
, wxT("Bold")) == 0)
436 else if (wxStrcmp(weight
, wxT("Light")) == 0)