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)
123 wxGenericFontDialog::wxGenericFontDialog(wxWindow
*parent
, wxFontData
*data
):
124 wxDialog(parent
, -1, "Font", wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
127 Create(parent
, data
);
130 wxGenericFontDialog::~wxGenericFontDialog(void)
134 bool wxGenericFontDialog::OnClose(void)
140 bool wxGenericFontDialog::Create(wxWindow
*parent
, wxFontData
*data
)
142 dialogParent
= parent
;
153 int wxGenericFontDialog::ShowModal(void)
155 int ret
= wxDialog::ShowModal();
157 if (ret
!= wxID_CANCEL
)
159 fontData
.chosenFont
= dialogFont
;
166 void wxGenericFontDialog::OnPaint(wxPaintEvent
& event
)
168 wxDialog::OnPaint(event
);
171 PaintFontBackground(dc
);
176 static void wxGenericChangeFontText(wxTextCtrl& text, wxCommandEvent& event)
178 if (event.GetEventType() == wxEVENT_TYPE_TEXT_ENTER_COMMAND)
180 wxGenericFontDialog *dialog = (wxGenericFontDialog *)text.GetParent();
181 dialog->OnChangeFont();
186 void wxGenericFontDialog::CreateWidgets(void)
196 fontRect
.width
= 350;
197 fontRect
.height
= 100;
200 static char *families[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
201 static char *styles[] = { "Normal", "Italic", "Slant" };
202 static char *weights[] = { "Normal", "Light", "Bold" };
204 static wxString families
[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
205 static wxString styles
[] = { "Normal", "Italic", "Slant" };
206 static wxString weights
[] = { "Normal", "Light", "Bold" };
210 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxPoint(10, 10), wxSize(120, -1), 5, families
);
211 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxPoint(140, 10), wxSize(120, -1), 3, styles
);
212 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxPoint(270, 10), wxSize(120, -1), 3, weights
);
214 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxPoint(10, 40), wxSize(190, -1), NUM_COLS
, wxColourDialogNames
);
216 // We want the pointSizeText to line up on the y axis with the colourChoice
217 colourChoice
->GetPosition(&fontRect
.x
, &y
); //NL mod
221 wxString pointSizes
[40];
223 for ( i
= 0; i
< 40; i
++)
226 sprintf(buf
, "%d", i
+ 1);
230 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxPoint(210, y
), wxSize(50, -1), 40, pointSizes
);
231 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, "Underline", wxPoint(280, y
));
234 pointSizeChoice
->GetPosition(&x
, &rectY
); //NL mod
236 pointSizeChoice
->GetSize(&x
, &y
); //NL mod
238 // Calculate the position of the bottom of the pointSizeChoice, and place
239 // the fontRect there (+5 for a nice gap)
241 fontRect
.y
+=y
+5; //NL mod
243 int by
= (fontRect
.y
+ fontRect
.height
+ 5);
245 wxButton
*okButton
= new wxButton(this, wxID_OK
, "OK", wxPoint(5, by
));
246 (void) new wxButton(this, wxID_OK
, "Cancel", wxPoint(50, by
));
248 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
249 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
250 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
251 wxString
name(wxTheColourDatabase
->FindName(fontData
.fontColour
));
252 colourChoice
->SetStringSelection(name
);
254 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
255 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
257 okButton
->SetDefault();
259 SetClientSize(400, by
+ 30);
268 void wxGenericFontDialog::InitializeFont(void)
270 int fontFamily
= wxSWISS
;
271 int fontWeight
= wxNORMAL
;
272 int fontStyle
= wxNORMAL
;
274 int fontUnderline
= FALSE
;
275 if (fontData
.initialFont
.Ok())
277 fontFamily
= fontData
.initialFont
.GetFamily();
278 fontWeight
= fontData
.initialFont
.GetWeight();
279 fontStyle
= fontData
.initialFont
.GetStyle();
280 fontSize
= fontData
.initialFont
.GetPointSize();
281 fontUnderline
= fontData
.initialFont
.GetUnderlined();
283 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
286 void wxGenericFontDialog::PaintFontBackground(wxDC
& dc
)
290 dc
.SetPen(*wxBLACK_PEN
);
291 dc
.SetBrush(*wxWHITE_BRUSH
);
292 dc
.DrawRectangle( fontRect
.x
, fontRect
.y
, fontRect
.width
, fontRect
.height
);
296 void wxGenericFontDialog::PaintFont(wxDC
& dc
)
301 dc
.SetFont(dialogFont
);
302 // Calculate vertical centre
304 dc
.GetTextExtent("X", &w
, &h
);
305 float cx
= (float)(fontRect
.x
+ 10);
306 float cy
= (float)(fontRect
.y
+ (fontRect
.height
/2.0) - (h
/2.0));
307 dc
.SetTextForeground(fontData
.fontColour
);
308 dc
.SetClippingRegion( fontRect
.x
, fontRect
.y
, (long)(fontRect
.width
-2.0), (long)(fontRect
.height
-2.0));
309 dc
.DrawText("ABCDEFGabcdefg12345", (long)cx
, (long)cy
);
310 dc
.DestroyClippingRegion();
311 dc
.SetFont(wxNullFont
);
316 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
318 if (!m_useEvents
) return;
320 int fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
321 int fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
322 int fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
323 int fontSize
= atoi(pointSizeChoice
->GetStringSelection());
324 int fontUnderline
= underLineCheckBox
->GetValue();
326 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
327 if (colourChoice
->GetStringSelection() != "")
329 wxColour
*col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
332 fontData
.fontColour
= *col
;
336 PaintFontBackground(dc
);
340 char *wxFontWeightIntToString(int weight
)
355 char *wxFontStyleIntToString(int style
)
370 char *wxFontFamilyIntToString(int family
)
389 int wxFontFamilyStringToInt(char *family
)
394 if (strcmp(family
, "Roman") == 0)
396 else if (strcmp(family
, "Decorative") == 0)
398 else if (strcmp(family
, "Modern") == 0)
400 else if (strcmp(family
, "Script") == 0)
405 int wxFontStyleStringToInt(char *style
)
409 if (strcmp(style
, "Italic") == 0)
411 else if (strcmp(style
, "Slant") == 0)
417 int wxFontWeightStringToInt(char *weight
)
421 if (strcmp(weight
, "Bold") == 0)
423 else if (strcmp(weight
, "Light") == 0)