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/generic/fontdlgg.h"
46 #include "wx/settings.h"
48 //-----------------------------------------------------------------------------
49 // helper class - wxFontPreviewer
50 //-----------------------------------------------------------------------------
52 class WXDLLEXPORT wxFontPreviewer
: public wxWindow
55 wxFontPreviewer(wxWindow
*parent
) : wxWindow(parent
, wxID_ANY
) {}
58 void OnPaint(wxPaintEvent
& event
);
62 BEGIN_EVENT_TABLE(wxFontPreviewer
, wxWindow
)
63 EVT_PAINT(wxFontPreviewer::OnPaint
)
66 void wxFontPreviewer::OnPaint(wxPaintEvent
& WXUNUSED(event
))
70 wxSize size
= GetSize();
71 wxFont font
= GetFont();
73 dc
.SetPen(*wxBLACK_PEN
);
74 dc
.SetBrush(*wxWHITE_BRUSH
);
75 dc
.DrawRectangle(0, 0, size
.x
, size
.y
);
80 // Calculate vertical centre
82 dc
.GetTextExtent( wxT("X"), &w
, &h
);
83 dc
.SetTextForeground(GetForegroundColour());
84 dc
.SetClippingRegion(2, 2, size
.x
-4, size
.y
-4);
85 dc
.DrawText(_("ABCDEFGabcdefg12345"),
87 dc
.DestroyClippingRegion();
91 //-----------------------------------------------------------------------------
92 // wxGenericFontDialog
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
97 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
98 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
99 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
100 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
101 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
102 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
103 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
104 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
109 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
115 wxT("MEDIUM VIOLET RED"),
120 wxT("MEDIUM SPRING GREEN"),
123 wxT("LIGHT STEEL BLUE"),
125 wxT("LIGHT MAGENTA"),
143 wxT("MEDIUM VIOLET RED"),
147 wxT("MEDIUM SEA GREEN"),
149 wxT("MIDNIGHT BLUE"),
155 wxT("MEDIUM FOREST GREEN"),
160 wxT("MEDIUM SLATE BLUE"),
165 * Generic wxFontDialog
168 void wxGenericFontDialog::Init()
175 wxGenericFontDialog::~wxGenericFontDialog()
179 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
181 EndModal(wxID_CANCEL
);
184 bool wxGenericFontDialog::DoCreate(wxWindow
*parent
)
186 if ( !wxDialog::Create( parent
, wxID_ANY
, _T("Choose Font") , wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
,
189 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
196 // sets initial font in preview area
197 wxCommandEvent dummy
;
203 int wxGenericFontDialog::ShowModal()
205 int ret
= wxDialog::ShowModal();
207 if (ret
!= wxID_CANCEL
)
209 m_fontData
.m_chosenFont
= dialogFont
;
215 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()));
262 colourChoice
->SetStringSelection(name
);
264 colourChoice
->SetStringSelection(wxT("BLACK"));
266 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
267 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
269 okButton
->SetDefault();
273 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
276 wxSizer
*topsizer
, *sizer
;
277 topsizer
= new wxBoxSizer(wxVERTICAL
);
282 sizer
= new wxBoxSizer(wxHORIZONTAL
);
283 sizer
->Add(familyChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
284 sizer
->Add(styleChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
285 sizer
->Add(weightChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
286 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
288 sizer
= new wxBoxSizer(wxHORIZONTAL
);
289 sizer
->Add(colourChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
290 sizer
->Add(pointSizeChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
291 sizer
->Add(underLineCheckBox
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
292 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
297 sizer
= new wxBoxSizer(wxHORIZONTAL
);
298 sizer
->Add(familyChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
299 sizer
->Add(styleChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
300 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
302 sizer
= new wxBoxSizer(wxHORIZONTAL
);
303 sizer
->Add(weightChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
304 sizer
->Add(colourChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
305 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
307 sizer
= new wxBoxSizer(wxHORIZONTAL
);
308 sizer
->Add(pointSizeChoice
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
309 sizer
->Add(underLineCheckBox
, 0, wxALIGN_CENTER
| wxLEFT
, 10);
310 topsizer
->Add(sizer
, 0, wxLEFT
| wxTOP
| wxRIGHT
, 10);
313 topsizer
->Add(m_previewer
, 1, wxALL
| wxEXPAND
, 10);
314 topsizer
->SetItemMinSize(m_previewer
, 430, 100);
316 sizer
= new wxBoxSizer(wxHORIZONTAL
);
317 sizer
->Add(okButton
, 0, wxRIGHT
, 10);
318 sizer
->Add(cancelButton
, 0, wxRIGHT
, 10);
319 topsizer
->Add(sizer
, 0, wxALIGN_RIGHT
| wxBOTTOM
, 10);
322 topsizer
->SetSizeHints(this);
332 // Don't block events any more
336 void wxGenericFontDialog::InitializeFont()
338 int fontFamily
= wxSWISS
;
339 int fontWeight
= wxNORMAL
;
340 int fontStyle
= wxNORMAL
;
342 bool fontUnderline
= false;
344 if (m_fontData
.m_initialFont
.Ok())
346 fontFamily
= m_fontData
.m_initialFont
.GetFamily();
347 fontWeight
= m_fontData
.m_initialFont
.GetWeight();
348 fontStyle
= m_fontData
.m_initialFont
.GetStyle();
349 fontSize
= m_fontData
.m_initialFont
.GetPointSize();
350 fontUnderline
= m_fontData
.m_initialFont
.GetUnderlined();
353 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
,
354 fontWeight
, fontUnderline
);
357 m_previewer
->SetFont(dialogFont
);
360 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
362 if (!m_useEvents
) return;
364 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
365 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
366 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
367 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
368 int fontUnderline
= underLineCheckBox
->GetValue();
370 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
371 m_previewer
->SetFont(dialogFont
);
372 if ( !colourChoice
->GetStringSelection().empty() )
374 wxColour col
= wxTheColourDatabase
->Find(colourChoice
->GetStringSelection());
377 m_fontData
.m_fontColour
= col
;
378 m_previewer
->SetForegroundColour(col
);
381 m_previewer
->Refresh();
384 const wxChar
*wxFontWeightIntToString(int weight
)
394 return wxT("Normal");
398 const wxChar
*wxFontStyleIntToString(int style
)
403 return wxT("Italic");
408 return wxT("Normal");
412 const wxChar
*wxFontFamilyIntToString(int family
)
419 return wxT("Decorative");
421 return wxT("Modern");
423 return wxT("Script");
425 return wxT("Teletype");
432 int wxFontFamilyStringToInt(wxChar
*family
)
437 if (wxStrcmp(family
, wxT("Roman")) == 0)
439 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
441 else if (wxStrcmp(family
, wxT("Modern")) == 0)
443 else if (wxStrcmp(family
, wxT("Script")) == 0)
445 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
450 int wxFontStyleStringToInt(wxChar
*style
)
454 if (wxStrcmp(style
, wxT("Italic")) == 0)
456 else if (wxStrcmp(style
, wxT("Slant")) == 0)
462 int wxFontWeightStringToInt(wxChar
*weight
)
466 if (wxStrcmp(weight
, wxT("Bold")) == 0)
468 else if (wxStrcmp(weight
, wxT("Light")) == 0)