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"
26 #include "wx/dialog.h"
27 #include "wx/listbox.h"
28 #include "wx/button.h"
29 #include "wx/stattext.h"
30 #include "wx/layout.h"
31 #include "wx/dcclient.h"
32 #include "wx/choice.h"
33 #include "wx/checkbox.h"
40 #include "wx/cmndata.h"
41 #include "wx/generic/fontdlgg.h"
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
46 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
47 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
48 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
49 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
50 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
51 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
52 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
53 EVT_PAINT(wxGenericFontDialog::OnPaint
)
54 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
60 static wxString wxColourDialogNames
[NUM_COLS
]={_T("ORANGE"),
66 _T("MEDIUM VIOLET RED"),
71 _T("MEDIUM SPRING GREEN"),
74 _T("LIGHT STEEL BLUE"),
94 _T("MEDIUM VIOLET RED"),
98 _T("MEDIUM SEA GREEN"),
106 _T("MEDIUM FOREST GREEN"),
111 _T("MEDIUM SLATE BLUE"),
116 * Generic wxFontDialog
119 wxGenericFontDialog::wxGenericFontDialog(void)
125 wxGenericFontDialog::wxGenericFontDialog(wxWindow
*parent
, wxFontData
*data
):
126 wxDialog(parent
, -1, _("Font"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
129 Create(parent
, data
);
132 wxGenericFontDialog::~wxGenericFontDialog(void)
136 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
138 EndModal(wxID_CANCEL
);
141 bool wxGenericFontDialog::Create(wxWindow
*parent
, wxFontData
*data
)
143 dialogParent
= parent
;
154 int wxGenericFontDialog::ShowModal(void)
156 int ret
= wxDialog::ShowModal();
158 if (ret
!= wxID_CANCEL
)
160 fontData
.chosenFont
= dialogFont
;
167 void wxGenericFontDialog::OnPaint(wxPaintEvent
& event
)
170 PaintFontBackground(dc
);
175 static void wxGenericChangeFontText(wxTextCtrl& text, wxCommandEvent& event)
177 if (event.GetEventType() == wxEVENT_TYPE_TEXT_ENTER_COMMAND)
179 wxGenericFontDialog *dialog = (wxGenericFontDialog *)text.GetParent();
180 dialog->OnChangeFont();
185 void wxGenericFontDialog::CreateWidgets(void)
195 fontRect
.width
= 430;
196 fontRect
.height
= 100;
199 static char *families[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
200 static char *styles[] = { "Normal", "Italic", "Slant" };
201 static char *weights[] = { "Normal", "Light", "Bold" };
205 *families
= new wxString
[6],
206 *styles
= new wxString
[3],
207 *weights
= new wxString
[3];
208 families
[0] = _("Roman");
209 families
[1] = _("Decorative");
210 families
[2] = _("Modern");
211 families
[3] = _("Script");
212 families
[4] = _("Swiss" );
213 families
[5] = _("Teletype" );
214 styles
[0] = _("Normal");
215 styles
[1] = _("Italic");
216 styles
[2] = _("Slant");
217 weights
[0] = _("Normal");
218 weights
[1] = _("Light");
219 weights
[2] = _("Bold");
223 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxPoint(10, 10), wxSize(120, -1), 5, families
);
224 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxPoint(170, 10), wxSize(120, -1), 3, styles
);
225 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxPoint(330, 10), wxSize(120, -1), 3, weights
);
227 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxPoint(10, 40), wxSize(180, -1), NUM_COLS
, wxColourDialogNames
);
228 #if 0 // def __WXMOTIF__ // TODO: This necessary now?
229 // We want the pointSizeText to line up on the y axis with the colourChoice
230 colourChoice
->GetPosition(&fontRect
.x
, &y
); //NL mod
234 wxString
*pointSizes
= new wxString
[40];
236 for ( i
= 0; i
< 40; i
++)
239 sprintf(buf
, "%d", i
+ 1);
243 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxPoint(230, y
), wxSize(50, -1), 40, pointSizes
);
244 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, _("Underline"), wxPoint(320, y
));
247 pointSizeChoice
->GetPosition(&x
, &rectY
); //NL mod
249 pointSizeChoice
->GetSize(&x
, &y
); //NL mod
251 // Calculate the position of the bottom of the pointSizeChoice, and place
252 // the fontRect there (+5 for a nice gap)
254 fontRect
.y
+=y
+5; //NL mod
256 int by
= (fontRect
.y
+ fontRect
.height
+ 15);
258 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(230, by
), wxSize(75,-1));
259 (void) new wxButton(this, wxID_OK
, _("Cancel"), wxPoint(330, by
), wxSize(75,-1));
261 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
262 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
263 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
264 wxString
name(wxTheColourDatabase
->FindName(fontData
.fontColour
));
265 colourChoice
->SetStringSelection(name
);
267 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
268 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
270 okButton
->SetDefault();
272 // SetClientSize(450, by + 40);
286 void wxGenericFontDialog::InitializeFont(void)
288 int fontFamily
= wxSWISS
;
289 int fontWeight
= wxNORMAL
;
290 int fontStyle
= wxNORMAL
;
292 int fontUnderline
= FALSE
;
293 if (fontData
.initialFont
.Ok())
295 fontFamily
= fontData
.initialFont
.GetFamily();
296 fontWeight
= fontData
.initialFont
.GetWeight();
297 fontStyle
= fontData
.initialFont
.GetStyle();
298 fontSize
= fontData
.initialFont
.GetPointSize();
299 fontUnderline
= fontData
.initialFont
.GetUnderlined();
301 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
304 void wxGenericFontDialog::PaintFontBackground(wxDC
& dc
)
308 dc
.SetPen(*wxBLACK_PEN
);
309 dc
.SetBrush(*wxWHITE_BRUSH
);
310 dc
.DrawRectangle( fontRect
.x
, fontRect
.y
, fontRect
.width
, fontRect
.height
);
314 void wxGenericFontDialog::PaintFont(wxDC
& dc
)
319 dc
.SetFont(dialogFont
);
320 // Calculate vertical centre
322 dc
.GetTextExtent("X", &w
, &h
);
323 float cx
= (float)(fontRect
.x
+ 10);
324 float cy
= (float)(fontRect
.y
+ (fontRect
.height
/2.0) - (h
/2.0));
325 dc
.SetTextForeground(fontData
.fontColour
);
326 dc
.SetClippingRegion( fontRect
.x
, fontRect
.y
, (long)(fontRect
.width
-2.0), (long)(fontRect
.height
-2.0));
327 dc
.DrawText(_("ABCDEFGabcdefg12345"), (long)cx
, (long)cy
);
328 dc
.DestroyClippingRegion();
329 dc
.SetFont(wxNullFont
);
334 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
336 if (!m_useEvents
) return;
338 int fontFamily
= 0; /* shut up buggy egcs warnings */
339 fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
341 fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
343 fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
344 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
345 int fontUnderline
= underLineCheckBox
->GetValue();
347 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
348 if (colourChoice
->GetStringSelection() != _T(""))
350 wxColour
*col
= (wxColour
*) NULL
;
351 col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
354 fontData
.fontColour
= *col
;
358 PaintFontBackground(dc
);
362 wxChar
*wxFontWeightIntToString(int weight
)
377 wxChar
*wxFontStyleIntToString(int style
)
392 wxChar
*wxFontFamilyIntToString(int family
)
399 return _T("Decorative");
405 return _T("Teletype");
413 int wxFontFamilyStringToInt(wxChar
*family
)
418 if (wxStrcmp(family
, _T("Roman")) == 0)
420 else if (wxStrcmp(family
, _T("Decorative")) == 0)
422 else if (wxStrcmp(family
, _T("Modern")) == 0)
424 else if (wxStrcmp(family
, _T("Script")) == 0)
426 else if (wxStrcmp(family
, _T("Teletype")) == 0)
431 int wxFontStyleStringToInt(wxChar
*style
)
435 if (wxStrcmp(style
, _T("Italic")) == 0)
437 else if (wxStrcmp(style
, _T("Slant")) == 0)
443 int wxFontWeightStringToInt(wxChar
*weight
)
447 if (wxStrcmp(weight
, _T("Bold")) == 0)
449 else if (wxStrcmp(weight
, _T("Light")) == 0)