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()
172 wxGenericFontDialog::~wxGenericFontDialog()
176 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
178 EndModal(wxID_CANCEL
);
181 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
189 int wxGenericFontDialog::ShowModal()
191 int ret
= wxDialog::ShowModal();
193 if (ret
!= wxID_CANCEL
)
195 m_fontData
.chosenFont
= dialogFont
;
201 void wxGenericFontDialog::CreateWidgets()
206 *families
= new wxString
[6],
207 *styles
= new wxString
[3],
208 *weights
= new wxString
[3];
209 families
[0] = _("Roman");
210 families
[1] = _("Decorative");
211 families
[2] = _("Modern");
212 families
[3] = _("Script");
213 families
[4] = _("Swiss" );
214 families
[5] = _("Teletype" );
215 styles
[0] = _("Normal");
216 styles
[1] = _("Italic");
217 styles
[2] = _("Slant");
218 weights
[0] = _("Normal");
219 weights
[1] = _("Light");
220 weights
[2] = _("Bold");
222 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxDefaultPosition
, wxDefaultSize
, 5, families
);
223 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxDefaultPosition
, wxDefaultSize
, 3, styles
);
224 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxDefaultPosition
, wxDefaultSize
, 3, weights
);
226 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxDefaultPosition
, wxDefaultSize
, NUM_COLS
, wxColourDialogNames
);
228 wxString
*pointSizes
= new wxString
[40];
230 for ( i
= 0; i
< 40; i
++)
233 sprintf(buf
, "%d", i
+ 1);
237 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxDefaultPosition
, wxDefaultSize
, 40, pointSizes
);
238 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, _("Underline"));
240 m_previewer
= new wxFontPreviewer(this);
242 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"));
243 wxButton
*cancelButton
= new wxButton(this, wxID_OK
, _("Cancel"));
245 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
246 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
247 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
248 wxString
name(wxTheColourDatabase
->FindName(m_fontData
.fontColour
));
249 colourChoice
->SetStringSelection(name
);
251 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
252 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
254 okButton
->SetDefault();
256 wxSizer
*topsizer
, *sizer
;
257 topsizer
= new wxBoxSizer(wxVERTICAL
);
259 sizer
= new wxBoxSizer(wxHORIZONTAL
);
260 sizer
->Add(familyChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
261 sizer
->Add(styleChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
262 sizer
->Add(weightChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
263 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
265 sizer
= new wxBoxSizer(wxHORIZONTAL
);
266 sizer
->Add(colourChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
267 sizer
->Add(pointSizeChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
268 sizer
->Add(underLineCheckBox
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
269 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
271 topsizer
->Add(m_previewer
, 1, wxALL
| wxEXPAND
, 10);
272 topsizer
->SetItemMinSize(m_previewer
, 430, 100);
274 sizer
= new wxBoxSizer(wxHORIZONTAL
);
275 sizer
->Add(okButton
, 0, wxRIGHT
, 10);
276 sizer
->Add(cancelButton
, 0, wxRIGHT
, 10);
277 topsizer
->Add(sizer
, 0, wxALIGN_RIGHT
| wxBOTTOM
, 10);
281 topsizer
->SetSizeHints(this);
293 void wxGenericFontDialog::InitializeFont()
295 int fontFamily
= wxSWISS
;
296 int fontWeight
= wxNORMAL
;
297 int fontStyle
= wxNORMAL
;
299 int fontUnderline
= FALSE
;
301 if (m_fontData
.initialFont
.Ok())
303 fontFamily
= m_fontData
.initialFont
.GetFamily();
304 fontWeight
= m_fontData
.initialFont
.GetWeight();
305 fontStyle
= m_fontData
.initialFont
.GetStyle();
306 fontSize
= m_fontData
.initialFont
.GetPointSize();
307 fontUnderline
= m_fontData
.initialFont
.GetUnderlined();
310 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
313 m_previewer
->SetFont(dialogFont
);
316 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
318 if (!m_useEvents
) return;
320 int fontFamily
= 0; /* shut up buggy egcs warnings */
321 fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
323 fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
325 fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
326 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
327 int fontUnderline
= underLineCheckBox
->GetValue();
329 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
330 m_previewer
->SetFont(dialogFont
);
331 if (colourChoice
->GetStringSelection() != wxT(""))
333 wxColour
*col
= (wxColour
*) NULL
;
334 col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
337 m_fontData
.fontColour
= *col
;
338 m_previewer
->SetForegroundColour(*col
);
341 m_previewer
->Refresh();
344 const wxChar
*wxFontWeightIntToString(int weight
)
354 return wxT("Normal");
358 const wxChar
*wxFontStyleIntToString(int style
)
363 return wxT("Italic");
368 return wxT("Normal");
372 const wxChar
*wxFontFamilyIntToString(int family
)
379 return wxT("Decorative");
381 return wxT("Modern");
383 return wxT("Script");
385 return wxT("Teletype");
392 int wxFontFamilyStringToInt(wxChar
*family
)
397 if (wxStrcmp(family
, wxT("Roman")) == 0)
399 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
401 else if (wxStrcmp(family
, wxT("Modern")) == 0)
403 else if (wxStrcmp(family
, wxT("Script")) == 0)
405 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
410 int wxFontStyleStringToInt(wxChar
*style
)
414 if (wxStrcmp(style
, wxT("Italic")) == 0)
416 else if (wxStrcmp(style
, wxT("Slant")) == 0)
422 int wxFontWeightStringToInt(wxChar
*weight
)
426 if (wxStrcmp(weight
, wxT("Bold")) == 0)
428 else if (wxStrcmp(weight
, wxT("Light")) == 0)