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"
39 #include "wx/cmndata.h"
40 #include "wx/generic/fontdlgg.h"
42 #if !USE_SHARED_LIBRARY
43 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog
, wxDialog
)
45 BEGIN_EVENT_TABLE(wxGenericFontDialog
, wxDialog
)
46 EVT_CHECKBOX(wxID_FONT_UNDERLINE
, wxGenericFontDialog::OnChangeFont
)
47 EVT_CHOICE(wxID_FONT_STYLE
, wxGenericFontDialog::OnChangeFont
)
48 EVT_CHOICE(wxID_FONT_WEIGHT
, wxGenericFontDialog::OnChangeFont
)
49 EVT_CHOICE(wxID_FONT_FAMILY
, wxGenericFontDialog::OnChangeFont
)
50 EVT_CHOICE(wxID_FONT_COLOUR
, wxGenericFontDialog::OnChangeFont
)
51 EVT_CHOICE(wxID_FONT_SIZE
, wxGenericFontDialog::OnChangeFont
)
52 EVT_PAINT(wxGenericFontDialog::OnPaint
)
58 static wxString wxColourDialogNames
[NUM_COLS
]={"ORANGE",
69 "MEDIUM SPRING GREEN",
104 "MEDIUM FOREST GREEN",
114 * Generic wxFontDialog
117 wxGenericFontDialog::wxGenericFontDialog(void)
122 wxGenericFontDialog::wxGenericFontDialog(wxWindow
*parent
, wxFontData
*data
):
123 wxDialog(parent
, -1, "Font", wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
125 Create(parent
, data
);
128 wxGenericFontDialog::~wxGenericFontDialog(void)
132 bool wxGenericFontDialog::OnClose(void)
138 bool wxGenericFontDialog::Create(wxWindow
*parent
, wxFontData
*data
)
140 dialogParent
= parent
;
151 int wxGenericFontDialog::ShowModal(void)
153 int ret
= wxDialog::ShowModal();
155 if (ret
!= wxID_CANCEL
)
157 fontData
.chosenFont
= dialogFont
;
164 void wxGenericFontDialog::OnPaint(wxPaintEvent
& event
)
166 wxDialog::OnPaint(event
);
169 PaintFontBackground(dc
);
174 static void wxGenericChangeFontText(wxTextCtrl& text, wxCommandEvent& event)
176 if (event.GetEventType() == wxEVENT_TYPE_TEXT_ENTER_COMMAND)
178 wxGenericFontDialog *dialog = (wxGenericFontDialog *)text.GetParent();
179 dialog->OnChangeFont();
184 void wxGenericFontDialog::CreateWidgets(void)
194 fontRect
.width
= 350;
195 fontRect
.height
= 100;
198 static char *families[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
199 static char *styles[] = { "Normal", "Italic", "Slant" };
200 static char *weights[] = { "Normal", "Light", "Bold" };
202 static wxString families
[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
203 static wxString styles
[] = { "Normal", "Italic", "Slant" };
204 static wxString weights
[] = { "Normal", "Light", "Bold" };
208 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxPoint(10, 10), wxSize(120, -1), 5, families
);
209 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxPoint(140, 10), wxSize(120, -1), 3, styles
);
210 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxPoint(270, 10), wxSize(120, -1), 3, weights
);
212 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxPoint(10, 40), wxSize(190, -1), NUM_COLS
, wxColourDialogNames
);
214 // We want the pointSizeText to line up on the y axis with the colourChoice
215 colourChoice
->GetPosition(&fontRect
.x
, &y
); //NL mod
219 wxString pointSizes
[40];
221 for ( i
= 0; i
< 40; i
++)
224 sprintf(buf
, "%d", i
+ 1);
228 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxPoint(210, y
), wxSize(50, -1), 40, pointSizes
);
229 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, "Underline", wxPoint(280, y
));
232 pointSizeChoice
->GetPosition(&x
, &rectY
); //NL mod
234 pointSizeChoice
->GetSize(&x
, &y
); //NL mod
236 // Calculate the position of the bottom of the pointSizeChoice, and place
237 // the fontRect there (+5 for a nice gap)
239 fontRect
.y
+=y
+5; //NL mod
241 int by
= (fontRect
.y
+ fontRect
.height
+ 5);
243 wxButton
*okButton
= new wxButton(this, wxID_OK
, "OK", wxPoint(5, by
));
244 (void) new wxButton(this, wxID_OK
, "Cancel", wxPoint(50, by
));
246 familyChoice
->SetStringSelection(wxFontFamilyIntToString(dialogFont
.GetFamily()));
247 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
248 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
249 wxString
name(wxTheColourDatabase
->FindName(fontData
.fontColour
));
250 colourChoice
->SetStringSelection(name
);
252 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
254 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize());
256 okButton
->SetDefault();
258 SetClientSize(400, by
+ 30);
265 void wxGenericFontDialog::InitializeFont(void)
267 int fontFamily
= wxSWISS
;
268 int fontWeight
= wxNORMAL
;
269 int fontStyle
= wxNORMAL
;
271 int fontUnderline
= FALSE
;
272 if (fontData
.initialFont
.Ok())
274 fontFamily
= fontData
.initialFont
.GetFamily();
275 fontWeight
= fontData
.initialFont
.GetWeight();
276 fontStyle
= fontData
.initialFont
.GetStyle();
277 fontSize
= fontData
.initialFont
.GetPointSize();
278 fontUnderline
= fontData
.initialFont
.GetUnderlined();
280 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
284 void wxGenericFontDialog::PaintFontBackground(wxDC
& dc
)
288 dc
.SetPen(*wxBLACK_PEN
);
289 dc
.SetBrush(*wxWHITE_BRUSH
);
290 dc
.DrawRectangle( fontRect
.x
, fontRect
.y
, fontRect
.width
, fontRect
.height
);
294 void wxGenericFontDialog::PaintFont(wxDC
& dc
)
299 dc
.SetFont(dialogFont
);
300 // Calculate vertical centre
302 dc
.GetTextExtent("X", &w
, &h
);
303 float cx
= (float)(fontRect
.x
+ 10);
304 float cy
= (float)(fontRect
.y
+ (fontRect
.height
/2.0) - (h
/2.0));
305 dc
.SetTextForeground(fontData
.fontColour
);
306 dc
.SetClippingRegion( fontRect
.x
, fontRect
.y
, (long)(fontRect
.width
-2.0), (long)(fontRect
.height
-2.0));
307 dc
.DrawText("ABCDEFGabcdefg12345", (long)cx
, (long)cy
);
308 dc
.DestroyClippingRegion();
309 dc
.SetFont(wxNullFont
);
314 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
316 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
317 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
318 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
319 int fontSize
= atoi(pointSizeChoice
->GetStringSelection());
320 int fontUnderline
= underLineCheckBox
->GetValue();
322 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
323 if (colourChoice
->GetStringSelection() != "")
325 wxColour
*col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
328 fontData
.fontColour
= *col
;
332 PaintFontBackground(dc
);
336 char *wxFontWeightIntToString(int weight
)
351 char *wxFontStyleIntToString(int style
)
366 char *wxFontFamilyIntToString(int family
)
385 int wxFontFamilyStringToInt(char *family
)
390 if (strcmp(family
, "Roman") == 0)
392 else if (strcmp(family
, "Decorative") == 0)
394 else if (strcmp(family
, "Modern") == 0)
396 else if (strcmp(family
, "Script") == 0)
401 int wxFontStyleStringToInt(char *style
)
405 if (strcmp(style
, "Italic") == 0)
407 else if (strcmp(style
, "Slant") == 0)
413 int wxFontWeightStringToInt(char *weight
)
417 if (strcmp(weight
, "Bold") == 0)
419 else if (strcmp(weight
, "Light") == 0)