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 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
)
53 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow
)
58 static wxString wxColourDialogNames
[NUM_COLS
]={wxT("ORANGE"),
64 wxT("MEDIUM VIOLET RED"),
69 wxT("MEDIUM SPRING GREEN"),
72 wxT("LIGHT STEEL BLUE"),
92 wxT("MEDIUM VIOLET RED"),
96 wxT("MEDIUM SEA GREEN"),
104 wxT("MEDIUM FOREST GREEN"),
109 wxT("MEDIUM SLATE BLUE"),
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 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
136 EndModal(wxID_CANCEL
);
139 bool wxGenericFontDialog::Create(wxWindow
*parent
, wxFontData
*data
)
141 dialogParent
= parent
;
152 int wxGenericFontDialog::ShowModal(void)
154 int ret
= wxDialog::ShowModal();
156 if (ret
!= wxID_CANCEL
)
158 fontData
.chosenFont
= dialogFont
;
165 void wxGenericFontDialog::OnPaint(wxPaintEvent
& WXUNUSED(event
))
168 PaintFontBackground(dc
);
173 static void wxGenericChangeFontText(wxTextCtrl& text, wxCommandEvent& event)
175 if (event.GetEventType() == wxEVENT_TYPE_TEXT_ENTER_COMMAND)
177 wxGenericFontDialog *dialog = (wxGenericFontDialog *)text.GetParent();
178 dialog->OnChangeFont();
183 void wxGenericFontDialog::CreateWidgets(void)
193 fontRect
.width
= 430;
194 fontRect
.height
= 100;
197 static char *families[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
198 static char *styles[] = { "Normal", "Italic", "Slant" };
199 static char *weights[] = { "Normal", "Light", "Bold" };
203 *families
= new wxString
[6],
204 *styles
= new wxString
[3],
205 *weights
= new wxString
[3];
206 families
[0] = _("Roman");
207 families
[1] = _("Decorative");
208 families
[2] = _("Modern");
209 families
[3] = _("Script");
210 families
[4] = _("Swiss" );
211 families
[5] = _("Teletype" );
212 styles
[0] = _("Normal");
213 styles
[1] = _("Italic");
214 styles
[2] = _("Slant");
215 weights
[0] = _("Normal");
216 weights
[1] = _("Light");
217 weights
[2] = _("Bold");
221 familyChoice
= new wxChoice(this, wxID_FONT_FAMILY
, wxPoint(10, 10), wxSize(120, -1), 5, families
);
222 styleChoice
= new wxChoice(this, wxID_FONT_STYLE
, wxPoint(170, 10), wxSize(120, -1), 3, styles
);
223 weightChoice
= new wxChoice(this, wxID_FONT_WEIGHT
, wxPoint(330, 10), wxSize(120, -1), 3, weights
);
225 colourChoice
= new wxChoice(this, wxID_FONT_COLOUR
, wxPoint(10, 40), wxSize(180, -1), NUM_COLS
, wxColourDialogNames
);
226 #if 0 // def __WXMOTIF__ // TODO: This necessary now?
227 // We want the pointSizeText to line up on the y axis with the colourChoice
228 colourChoice
->GetPosition(&fontRect
.x
, &y
); //NL mod
232 wxString
*pointSizes
= new wxString
[40];
234 for ( i
= 0; i
< 40; i
++)
237 sprintf(buf
, "%d", i
+ 1);
241 pointSizeChoice
= new wxChoice(this, wxID_FONT_SIZE
, wxPoint(230, y
), wxSize(50, -1), 40, pointSizes
);
242 underLineCheckBox
= new wxCheckBox(this, wxID_FONT_UNDERLINE
, _("Underline"), wxPoint(320, y
));
245 pointSizeChoice
->GetPosition(&x
, &rectY
); //NL mod
247 pointSizeChoice
->GetSize(&x
, &y
); //NL mod
249 // Calculate the position of the bottom of the pointSizeChoice, and place
250 // the fontRect there (+5 for a nice gap)
252 fontRect
.y
+=y
+5; //NL mod
254 int by
= (fontRect
.y
+ fontRect
.height
+ 15);
256 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(230, by
), wxSize(75,-1));
257 (void) new wxButton(this, wxID_OK
, _("Cancel"), wxPoint(330, by
), wxSize(75,-1));
259 familyChoice
->SetStringSelection( wxFontFamilyIntToString(dialogFont
.GetFamily()) );
260 styleChoice
->SetStringSelection(wxFontStyleIntToString(dialogFont
.GetStyle()));
261 weightChoice
->SetStringSelection(wxFontWeightIntToString(dialogFont
.GetWeight()));
262 wxString
name(wxTheColourDatabase
->FindName(fontData
.fontColour
));
263 colourChoice
->SetStringSelection(name
);
265 underLineCheckBox
->SetValue(dialogFont
.GetUnderlined());
266 pointSizeChoice
->SetSelection(dialogFont
.GetPointSize()-1);
268 okButton
->SetDefault();
270 // SetClientSize(450, by + 40);
284 void wxGenericFontDialog::InitializeFont(void)
286 int fontFamily
= wxSWISS
;
287 int fontWeight
= wxNORMAL
;
288 int fontStyle
= wxNORMAL
;
290 int fontUnderline
= FALSE
;
291 if (fontData
.initialFont
.Ok())
293 fontFamily
= fontData
.initialFont
.GetFamily();
294 fontWeight
= fontData
.initialFont
.GetWeight();
295 fontStyle
= fontData
.initialFont
.GetStyle();
296 fontSize
= fontData
.initialFont
.GetPointSize();
297 fontUnderline
= fontData
.initialFont
.GetUnderlined();
299 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
302 void wxGenericFontDialog::PaintFontBackground(wxDC
& dc
)
306 dc
.SetPen(*wxBLACK_PEN
);
307 dc
.SetBrush(*wxWHITE_BRUSH
);
308 dc
.DrawRectangle( fontRect
.x
, fontRect
.y
, fontRect
.width
, fontRect
.height
);
312 void wxGenericFontDialog::PaintFont(wxDC
& dc
)
317 dc
.SetFont(dialogFont
);
318 // Calculate vertical centre
320 dc
.GetTextExtent("X", &w
, &h
);
321 float cx
= (float)(fontRect
.x
+ 10);
322 float cy
= (float)(fontRect
.y
+ (fontRect
.height
/2.0) - (h
/2.0));
323 dc
.SetTextForeground(fontData
.fontColour
);
324 dc
.SetClippingRegion( fontRect
.x
, fontRect
.y
, (long)(fontRect
.width
-2.0), (long)(fontRect
.height
-2.0));
325 dc
.DrawText(_("ABCDEFGabcdefg12345"), (long)cx
, (long)cy
);
326 dc
.DestroyClippingRegion();
327 dc
.SetFont(wxNullFont
);
332 void wxGenericFontDialog::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
334 if (!m_useEvents
) return;
336 int fontFamily
= 0; /* shut up buggy egcs warnings */
337 fontFamily
= wxFontFamilyStringToInt(WXSTRINGCAST familyChoice
->GetStringSelection());
339 fontWeight
= wxFontWeightStringToInt(WXSTRINGCAST weightChoice
->GetStringSelection());
341 fontStyle
= wxFontStyleStringToInt(WXSTRINGCAST styleChoice
->GetStringSelection());
342 int fontSize
= wxAtoi(pointSizeChoice
->GetStringSelection());
343 int fontUnderline
= underLineCheckBox
->GetValue();
345 dialogFont
= wxFont(fontSize
, fontFamily
, fontStyle
, fontWeight
, (fontUnderline
!= 0));
346 if (colourChoice
->GetStringSelection() != wxT(""))
348 wxColour
*col
= (wxColour
*) NULL
;
349 col
= wxTheColourDatabase
->FindColour(colourChoice
->GetStringSelection());
352 fontData
.fontColour
= *col
;
356 PaintFontBackground(dc
);
360 wxChar
*wxFontWeightIntToString(int weight
)
370 return wxT("Normal");
374 wxChar
*wxFontStyleIntToString(int style
)
379 return wxT("Italic");
384 return wxT("Normal");
388 wxChar
*wxFontFamilyIntToString(int family
)
395 return wxT("Decorative");
397 return wxT("Modern");
399 return wxT("Script");
401 return wxT("Teletype");
408 int wxFontFamilyStringToInt(wxChar
*family
)
413 if (wxStrcmp(family
, wxT("Roman")) == 0)
415 else if (wxStrcmp(family
, wxT("Decorative")) == 0)
417 else if (wxStrcmp(family
, wxT("Modern")) == 0)
419 else if (wxStrcmp(family
, wxT("Script")) == 0)
421 else if (wxStrcmp(family
, wxT("Teletype")) == 0)
426 int wxFontStyleStringToInt(wxChar
*style
)
430 if (wxStrcmp(style
, wxT("Italic")) == 0)
432 else if (wxStrcmp(style
, wxT("Slant")) == 0)
438 int wxFontWeightStringToInt(wxChar
*weight
)
442 if (wxStrcmp(weight
, wxT("Bold")) == 0)
444 else if (wxStrcmp(weight
, wxT("Light")) == 0)