Updated font dialog constructors to use a reference to the font data
[wxWidgets.git] / src / generic / fontdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: fontdlgg.cpp
3 // Purpose: Generic font dialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "fontdlgg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_FONTDLG && (!defined(__WXGTK__) || defined(__WXUNIVERSAL__))
24
25 #ifndef WX_PRECOMP
26 #include <stdio.h>
27 #include "wx/utils.h"
28 #include "wx/dialog.h"
29 #include "wx/listbox.h"
30 #include "wx/button.h"
31 #include "wx/stattext.h"
32 #include "wx/layout.h"
33 #include "wx/dcclient.h"
34 #include "wx/choice.h"
35 #include "wx/checkbox.h"
36 #include "wx/intl.h"
37 #endif
38
39 #include <string.h>
40 #include <stdlib.h>
41
42 #include "wx/cmndata.h"
43 #include "wx/sizer.h"
44 #include "wx/generic/fontdlgg.h"
45
46 //-----------------------------------------------------------------------------
47 // helper class - wxFontPreviewer
48 //-----------------------------------------------------------------------------
49
50 class WXDLLEXPORT wxFontPreviewer : public wxWindow
51 {
52 public:
53 wxFontPreviewer(wxWindow *parent) : wxWindow(parent, -1) {}
54
55 private:
56 void OnPaint(wxPaintEvent& event);
57 DECLARE_EVENT_TABLE()
58 };
59
60 BEGIN_EVENT_TABLE(wxFontPreviewer, wxWindow)
61 EVT_PAINT(wxFontPreviewer::OnPaint)
62 END_EVENT_TABLE()
63
64 void wxFontPreviewer::OnPaint(wxPaintEvent& WXUNUSED(event))
65 {
66 wxPaintDC dc(this);
67
68 wxSize size = GetSize();
69 wxFont font = GetFont();
70
71 dc.SetPen(*wxBLACK_PEN);
72 dc.SetBrush(*wxWHITE_BRUSH);
73 dc.DrawRectangle(0, 0, size.x, size.y);
74
75 if ( font.Ok() )
76 {
77 dc.SetFont(GetFont());
78 // Calculate vertical centre
79 long w, h;
80 dc.GetTextExtent("X", &w, &h);
81 dc.SetTextForeground(GetForegroundColour());
82 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
83 dc.DrawText(_("ABCDEFGabcdefg12345"),
84 10, h/2 + size.y/2);
85 dc.DestroyClippingRegion();
86 }
87 }
88
89 //-----------------------------------------------------------------------------
90 // wxGenericFontDialog
91 //-----------------------------------------------------------------------------
92
93 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog, wxDialog)
94
95 BEGIN_EVENT_TABLE(wxGenericFontDialog, wxDialog)
96 EVT_CHECKBOX(wxID_FONT_UNDERLINE, wxGenericFontDialog::OnChangeFont)
97 EVT_CHOICE(wxID_FONT_STYLE, wxGenericFontDialog::OnChangeFont)
98 EVT_CHOICE(wxID_FONT_WEIGHT, wxGenericFontDialog::OnChangeFont)
99 EVT_CHOICE(wxID_FONT_FAMILY, wxGenericFontDialog::OnChangeFont)
100 EVT_CHOICE(wxID_FONT_COLOUR, wxGenericFontDialog::OnChangeFont)
101 EVT_CHOICE(wxID_FONT_SIZE, wxGenericFontDialog::OnChangeFont)
102 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow)
103 END_EVENT_TABLE()
104
105
106 #define NUM_COLS 48
107 static wxString wxColourDialogNames[NUM_COLS]={wxT("ORANGE"),
108 wxT("GOLDENROD"),
109 wxT("WHEAT"),
110 wxT("SPRING GREEN"),
111 wxT("SKY BLUE"),
112 wxT("SLATE BLUE"),
113 wxT("MEDIUM VIOLET RED"),
114 wxT("PURPLE"),
115
116 wxT("RED"),
117 wxT("YELLOW"),
118 wxT("MEDIUM SPRING GREEN"),
119 wxT("PALE GREEN"),
120 wxT("CYAN"),
121 wxT("LIGHT STEEL BLUE"),
122 wxT("ORCHID"),
123 wxT("LIGHT MAGENTA"),
124
125 wxT("BROWN"),
126 wxT("YELLOW"),
127 wxT("GREEN"),
128 wxT("CADET BLUE"),
129 wxT("MEDIUM BLUE"),
130 wxT("MAGENTA"),
131 wxT("MAROON"),
132 wxT("ORANGE RED"),
133
134 wxT("FIREBRICK"),
135 wxT("CORAL"),
136 wxT("FOREST GREEN"),
137 wxT("AQUARAMINE"),
138 wxT("BLUE"),
139 wxT("NAVY"),
140 wxT("THISTLE"),
141 wxT("MEDIUM VIOLET RED"),
142
143 wxT("INDIAN RED"),
144 wxT("GOLD"),
145 wxT("MEDIUM SEA GREEN"),
146 wxT("MEDIUM BLUE"),
147 wxT("MIDNIGHT BLUE"),
148 wxT("GREY"),
149 wxT("PURPLE"),
150 wxT("KHAKI"),
151
152 wxT("BLACK"),
153 wxT("MEDIUM FOREST GREEN"),
154 wxT("KHAKI"),
155 wxT("DARK GREY"),
156 wxT("SEA GREEN"),
157 wxT("LIGHT GREY"),
158 wxT("MEDIUM SLATE BLUE"),
159 wxT("WHITE")
160 };
161
162 /*
163 * Generic wxFontDialog
164 */
165
166 wxGenericFontDialog::wxGenericFontDialog(void)
167 {
168 m_useEvents = FALSE;
169 m_previewer = NULL;
170 dialogParent = NULL;
171 }
172
173 wxGenericFontDialog::wxGenericFontDialog(wxWindow *parent, const wxFontData& data):
174 wxDialog(parent, -1, _("Font"), wxDefaultPosition, wxDefaultSize,
175 wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL|wxRESIZE_BORDER)
176 {
177 m_useEvents = FALSE;
178 m_previewer = NULL;
179 Create(parent, data);
180 }
181
182 wxGenericFontDialog::~wxGenericFontDialog(void)
183 {
184 }
185
186 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
187 {
188 EndModal(wxID_CANCEL);
189 }
190
191 bool wxGenericFontDialog::Create(wxWindow *parent, const wxFontData& data)
192 {
193 dialogParent = parent;
194
195 fontData = data;
196
197 InitializeFont();
198 CreateWidgets();
199
200 return TRUE;
201 }
202
203 int wxGenericFontDialog::ShowModal(void)
204 {
205 int ret = wxDialog::ShowModal();
206
207 if (ret != wxID_CANCEL)
208 {
209 fontData.chosenFont = dialogFont;
210 }
211
212 return ret;
213 }
214
215 void wxGenericFontDialog::CreateWidgets(void)
216 {
217 wxBusyCursor bcur;
218
219 wxString
220 *families = new wxString[6],
221 *styles = new wxString[3],
222 *weights = new wxString[3];
223 families[0] = _("Roman");
224 families[1] = _("Decorative");
225 families[2] = _("Modern");
226 families[3] = _("Script");
227 families[4] = _("Swiss" );
228 families[5] = _("Teletype" );
229 styles[0] = _("Normal");
230 styles[1] = _("Italic");
231 styles[2] = _("Slant");
232 weights[0] = _("Normal");
233 weights[1] = _("Light");
234 weights[2] = _("Bold");
235
236 familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxDefaultPosition, wxDefaultSize, 5, families);
237 styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxDefaultPosition, wxDefaultSize, 3, styles);
238 weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxDefaultPosition, wxDefaultSize, 3, weights);
239
240 colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxDefaultPosition, wxDefaultSize, NUM_COLS, wxColourDialogNames);
241
242 wxString *pointSizes = new wxString[40];
243 int i;
244 for ( i = 0; i < 40; i++)
245 {
246 char buf[5];
247 sprintf(buf, "%d", i + 1);
248 pointSizes[i] = buf;
249 }
250
251 pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes);
252 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"));
253
254 m_previewer = new wxFontPreviewer(this);
255
256 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
257 wxButton *cancelButton = new wxButton(this, wxID_OK, _("Cancel"));
258
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);
264
265 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
266 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
267
268 okButton->SetDefault();
269
270 wxSizer *topsizer, *sizer;
271 topsizer = new wxBoxSizer(wxVERTICAL);
272
273 sizer = new wxBoxSizer(wxHORIZONTAL);
274 sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
275 sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
276 sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
277 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
278
279 sizer = new wxBoxSizer(wxHORIZONTAL);
280 sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
281 sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
282 sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10);
283 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
284
285 topsizer->Add(m_previewer, 1, wxALL | wxEXPAND, 10);
286 topsizer->SetItemMinSize(m_previewer, 430, 100);
287
288 sizer = new wxBoxSizer(wxHORIZONTAL);
289 sizer->Add(okButton, 0, wxRIGHT, 10);
290 sizer->Add(cancelButton, 0, wxRIGHT, 10);
291 topsizer->Add(sizer, 0, wxALIGN_RIGHT | wxBOTTOM, 10);
292
293 SetAutoLayout(TRUE);
294 SetSizer(topsizer);
295 topsizer->SetSizeHints(this);
296 topsizer->Fit(this);
297
298 Centre(wxBOTH);
299
300 delete[] families;
301 delete[] styles;
302 delete[] weights;
303 delete[] pointSizes;
304 m_useEvents = TRUE;
305 }
306
307 void wxGenericFontDialog::InitializeFont(void)
308 {
309 int fontFamily = wxSWISS;
310 int fontWeight = wxNORMAL;
311 int fontStyle = wxNORMAL;
312 int fontSize = 12;
313 int fontUnderline = FALSE;
314
315 if (fontData.initialFont.Ok())
316 {
317 fontFamily = fontData.initialFont.GetFamily();
318 fontWeight = fontData.initialFont.GetWeight();
319 fontStyle = fontData.initialFont.GetStyle();
320 fontSize = fontData.initialFont.GetPointSize();
321 fontUnderline = fontData.initialFont.GetUnderlined();
322 }
323
324 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
325
326 if (m_previewer)
327 m_previewer->SetFont(dialogFont);
328 }
329
330 void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
331 {
332 if (!m_useEvents) return;
333
334 int fontFamily = 0; /* shut up buggy egcs warnings */
335 fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
336 int fontWeight = 0;
337 fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
338 int fontStyle = 0;
339 fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
340 int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
341 int fontUnderline = underLineCheckBox->GetValue();
342
343 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
344 m_previewer->SetFont(dialogFont);
345 if (colourChoice->GetStringSelection() != wxT(""))
346 {
347 wxColour *col = (wxColour*) NULL;
348 col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection());
349 if (col)
350 {
351 fontData.fontColour = *col;
352 m_previewer->SetForegroundColour(*col);
353 }
354 }
355 m_previewer->Refresh();
356 }
357
358 const wxChar *wxFontWeightIntToString(int weight)
359 {
360 switch (weight)
361 {
362 case wxLIGHT:
363 return wxT("Light");
364 case wxBOLD:
365 return wxT("Bold");
366 case wxNORMAL:
367 default:
368 return wxT("Normal");
369 }
370 }
371
372 const wxChar *wxFontStyleIntToString(int style)
373 {
374 switch (style)
375 {
376 case wxITALIC:
377 return wxT("Italic");
378 case wxSLANT:
379 return wxT("Slant");
380 case wxNORMAL:
381 default:
382 return wxT("Normal");
383 }
384 }
385
386 const wxChar *wxFontFamilyIntToString(int family)
387 {
388 switch (family)
389 {
390 case wxROMAN:
391 return wxT("Roman");
392 case wxDECORATIVE:
393 return wxT("Decorative");
394 case wxMODERN:
395 return wxT("Modern");
396 case wxSCRIPT:
397 return wxT("Script");
398 case wxTELETYPE:
399 return wxT("Teletype");
400 case wxSWISS:
401 default:
402 return wxT("Swiss");
403 }
404 }
405
406 int wxFontFamilyStringToInt(wxChar *family)
407 {
408 if (!family)
409 return wxSWISS;
410
411 if (wxStrcmp(family, wxT("Roman")) == 0)
412 return wxROMAN;
413 else if (wxStrcmp(family, wxT("Decorative")) == 0)
414 return wxDECORATIVE;
415 else if (wxStrcmp(family, wxT("Modern")) == 0)
416 return wxMODERN;
417 else if (wxStrcmp(family, wxT("Script")) == 0)
418 return wxSCRIPT;
419 else if (wxStrcmp(family, wxT("Teletype")) == 0)
420 return wxTELETYPE;
421 else return wxSWISS;
422 }
423
424 int wxFontStyleStringToInt(wxChar *style)
425 {
426 if (!style)
427 return wxNORMAL;
428 if (wxStrcmp(style, wxT("Italic")) == 0)
429 return wxITALIC;
430 else if (wxStrcmp(style, wxT("Slant")) == 0)
431 return wxSLANT;
432 else
433 return wxNORMAL;
434 }
435
436 int wxFontWeightStringToInt(wxChar *weight)
437 {
438 if (!weight)
439 return wxNORMAL;
440 if (wxStrcmp(weight, wxT("Bold")) == 0)
441 return wxBOLD;
442 else if (wxStrcmp(weight, wxT("Light")) == 0)
443 return wxLIGHT;
444 else
445 return wxNORMAL;
446 }
447
448 #endif
449 // wxUSE_FONTDLG
450