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
)
59 static wxString wxColourDialogNames
[NUM_COLS
]={"ORANGE",
70 "MEDIUM SPRING GREEN",
105 "MEDIUM FOREST GREEN",
115 * Generic wxFontDialog
118 wxGenericFontDialog::wxGenericFontDialog(void)
124 wxGenericFontDialog::wxGenericFontDialog(wxWindow
*parent
, wxFontData
*data
):
125 wxDialog(parent
, -1, _("Font"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
128 Create(parent
, data
);
131 wxGenericFontDialog::~wxGenericFontDialog(void)
135 bool wxGenericFontDialog::OnClose(void)
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
)
169 wxDialog::OnPaint(event
);
172 PaintFontBackground(dc
);
177 static void wxGenericChangeFontText(wxTextCtrl& text, wxCommandEvent& event)
179 if (event.GetEventType() == wxEVENT_TYPE_TEXT_ENTER_COMMAND)
181 wxGenericFontDialog *dialog = (wxGenericFontDialog *)text.GetParent();
182 dialog->OnChangeFont();
187 void wxGenericFontDialog::CreateWidgets(void)
197 fontRect
.width
= 350;
198 fontRect
.height
= 100;
201 static char *families[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
202 static char *styles[] = { "Normal", "Italic", "Slant" };
203 static char *weights[] = { "Normal", "Light", "Bold" };
205 static wxString families
[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
206 static wxString styles
[] = { "Normal", "Italic", "Slant" };
207 static wxString weights
[] = { "Normal", "Light", "Bold" };
211 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxPoint(10, 10), wxSize(120, -1), 5, families
);
212 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxPoint(160, 10), wxSize(120, -1), 3, styles
);
213 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxPoint(310, 10), wxSize(120, -1), 3, weights
);
215 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxPoint(10, 40), wxSize(190, -1), NUM_COLS
, wxColourDialogNames
);
217 // We want the pointSizeText to line up on the y axis with the colourChoice
218 colourChoice
->GetPosition(&fontRect
.x
, &y
); //NL mod
222 wxString pointSizes
[40];
224 for ( i
= 0; i
< 40; i
++)
227 sprintf(buf
, "%d", i
+ 1);
231 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxPoint(230, y
), wxSize(50, -1), 40, pointSizes
);
232 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, _("Underline"), wxPoint(320, y
));
235 pointSizeChoice
->GetPosition(&x
, &rectY
); //NL mod
237 pointSizeChoice
->GetSize(&x
, &y
); //NL mod
239 // Calculate the position of the bottom of the pointSizeChoice, and place
240 // the fontRect there (+5 for a nice gap)
242 fontRect
.y
+=y
+5; //NL mod
244 int by
= (fontRect
.y
+ fontRect
.height
+ 5);
246 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, by
));
247 (void) new wxButton(this, wxID_OK
, _("Cancel"), wxPoint(50, by
));
249 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
250 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
251 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
252 wxString
name(wxTheColourDatabase
->FindName(fontData
.fontColour
));
253 colourChoice
->SetStringSelection(name
);
255 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
256 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
258 okButton
->SetDefault();
260 SetClientSize(450, by
+ 40);
269 void wxGenericFontDialog::InitializeFont(void)
271 int fontFamily
= wxSWISS
;
272 int fontWeight
= wxNORMAL
;
273 int fontStyle
= wxNORMAL
;
275 int fontUnderline
= FALSE
;
276 if (fontData
.initialFont
.Ok())
278 fontFamily
= fontData
.initialFont
.GetFamily();
279 fontWeight
= fontData
.initialFont
.GetWeight();
280 fontStyle
= fontData
.initialFont
.GetStyle();
281 fontSize
= fontData
.initialFont
.GetPointSize();
282 fontUnderline
= fontData
.initialFont
.GetUnderlined();
284 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
287 void wxGenericFontDialog::PaintFontBackground(wxDC
& dc
)
291 dc
.SetPen(*wxBLACK_PEN
);
292 dc
.SetBrush(*wxWHITE_BRUSH
);
293 dc
.DrawRectangle( fontRect
.x
, fontRect
.y
, fontRect
.width
, fontRect
.height
);
297 void wxGenericFontDialog::PaintFont(wxDC
& dc
)
302 dc
.SetFont(dialogFont
);
303 // Calculate vertical centre
305 dc
.GetTextExtent("X", &w
, &h
);
306 float cx
= (float)(fontRect
.x
+ 10);
307 float cy
= (float)(fontRect
.y
+ (fontRect
.height
/2.0) - (h
/2.0));
308 dc
.SetTextForeground(fontData
.fontColour
);
309 dc
.SetClippingRegion( fontRect
.x
, fontRect
.y
, (long)(fontRect
.width
-2.0), (long)(fontRect
.height
-2.0));
310 dc
.DrawText(_("ABCDEFGabcdefg12345"), (long)cx
, (long)cy
);
311 dc
.DestroyClippingRegion();
312 dc
.SetFont(wxNullFont
);
317 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
319 if (!m_useEvents
) return;
321 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
322 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
323 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
324 int fontSize
= atoi(pointSizeChoice
->GetStringSelection());
325 int fontUnderline
= underLineCheckBox
->GetValue();
327 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
328 if (colourChoice
->GetStringSelection() != "")
330 wxColour
*col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
333 fontData
.fontColour
= *col
;
337 PaintFontBackground(dc
);
341 char *wxFontWeightIntToString(int weight
)
356 char *wxFontStyleIntToString(int style
)
371 char *wxFontFamilyIntToString(int family
)
390 int wxFontFamilyStringToInt(char *family
)
395 if (strcmp(family
, "Roman") == 0)
397 else if (strcmp(family
, "Decorative") == 0)
399 else if (strcmp(family
, "Modern") == 0)
401 else if (strcmp(family
, "Script") == 0)
406 int wxFontStyleStringToInt(char *style
)
410 if (strcmp(style
, "Italic") == 0)
412 else if (strcmp(style
, "Slant") == 0)
418 int wxFontWeightStringToInt(char *weight
)
422 if (strcmp(weight
, "Bold") == 0)
424 else if (strcmp(weight
, "Light") == 0)