Generic font dialog is still an option in wxMSW in non Univ mode
[wxWidgets.git] / src / generic / fontdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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(__WXGPE__) || 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/fontdlg.h"
45 #include "wx/generic/fontdlgg.h"
46 #include "wx/settings.h"
47
48 //-----------------------------------------------------------------------------
49 // helper class - wxFontPreviewer
50 //-----------------------------------------------------------------------------
51
52 class WXDLLEXPORT wxFontPreviewer : public wxWindow
53 {
54 public:
55 wxFontPreviewer(wxWindow *parent) : wxWindow(parent, -1) {}
56
57 private:
58 void OnPaint(wxPaintEvent& event);
59 DECLARE_EVENT_TABLE()
60 };
61
62 BEGIN_EVENT_TABLE(wxFontPreviewer, wxWindow)
63 EVT_PAINT(wxFontPreviewer::OnPaint)
64 END_EVENT_TABLE()
65
66 void wxFontPreviewer::OnPaint(wxPaintEvent& WXUNUSED(event))
67 {
68 wxPaintDC dc(this);
69
70 wxSize size = GetSize();
71 wxFont font = GetFont();
72
73 dc.SetPen(*wxBLACK_PEN);
74 dc.SetBrush(*wxWHITE_BRUSH);
75 dc.DrawRectangle(0, 0, size.x, size.y);
76
77 if ( font.Ok() )
78 {
79 dc.SetFont(font);
80 // Calculate vertical centre
81 long w, h;
82 dc.GetTextExtent( wxT("X"), &w, &h);
83 dc.SetTextForeground(GetForegroundColour());
84 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
85 dc.DrawText(_("ABCDEFGabcdefg12345"),
86 10, size.y/2 - h/2);
87 dc.DestroyClippingRegion();
88 }
89 }
90
91 //-----------------------------------------------------------------------------
92 // wxGenericFontDialog
93 //-----------------------------------------------------------------------------
94
95 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog, wxDialog)
96
97 BEGIN_EVENT_TABLE(wxGenericFontDialog, wxDialog)
98 EVT_CHECKBOX(wxID_FONT_UNDERLINE, wxGenericFontDialog::OnChangeFont)
99 EVT_CHOICE(wxID_FONT_STYLE, wxGenericFontDialog::OnChangeFont)
100 EVT_CHOICE(wxID_FONT_WEIGHT, wxGenericFontDialog::OnChangeFont)
101 EVT_CHOICE(wxID_FONT_FAMILY, wxGenericFontDialog::OnChangeFont)
102 EVT_CHOICE(wxID_FONT_COLOUR, wxGenericFontDialog::OnChangeFont)
103 EVT_CHOICE(wxID_FONT_SIZE, wxGenericFontDialog::OnChangeFont)
104 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow)
105 END_EVENT_TABLE()
106
107
108 #define NUM_COLS 48
109 static wxString wxColourDialogNames[NUM_COLS]={wxT("ORANGE"),
110 wxT("GOLDENROD"),
111 wxT("WHEAT"),
112 wxT("SPRING GREEN"),
113 wxT("SKY BLUE"),
114 wxT("SLATE BLUE"),
115 wxT("MEDIUM VIOLET RED"),
116 wxT("PURPLE"),
117
118 wxT("RED"),
119 wxT("YELLOW"),
120 wxT("MEDIUM SPRING GREEN"),
121 wxT("PALE GREEN"),
122 wxT("CYAN"),
123 wxT("LIGHT STEEL BLUE"),
124 wxT("ORCHID"),
125 wxT("LIGHT MAGENTA"),
126
127 wxT("BROWN"),
128 wxT("YELLOW"),
129 wxT("GREEN"),
130 wxT("CADET BLUE"),
131 wxT("MEDIUM BLUE"),
132 wxT("MAGENTA"),
133 wxT("MAROON"),
134 wxT("ORANGE RED"),
135
136 wxT("FIREBRICK"),
137 wxT("CORAL"),
138 wxT("FOREST GREEN"),
139 wxT("AQUARAMINE"),
140 wxT("BLUE"),
141 wxT("NAVY"),
142 wxT("THISTLE"),
143 wxT("MEDIUM VIOLET RED"),
144
145 wxT("INDIAN RED"),
146 wxT("GOLD"),
147 wxT("MEDIUM SEA GREEN"),
148 wxT("MEDIUM BLUE"),
149 wxT("MIDNIGHT BLUE"),
150 wxT("GREY"),
151 wxT("PURPLE"),
152 wxT("KHAKI"),
153
154 wxT("BLACK"),
155 wxT("MEDIUM FOREST GREEN"),
156 wxT("KHAKI"),
157 wxT("DARK GREY"),
158 wxT("SEA GREEN"),
159 wxT("LIGHT GREY"),
160 wxT("MEDIUM SLATE BLUE"),
161 wxT("WHITE")
162 };
163
164 /*
165 * Generic wxFontDialog
166 */
167
168 void wxGenericFontDialog::Init()
169 {
170 m_useEvents = FALSE;
171 m_previewer = NULL;
172 Create( m_parent ) ;
173 }
174
175 wxGenericFontDialog::~wxGenericFontDialog()
176 {
177 }
178
179 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
180 {
181 EndModal(wxID_CANCEL);
182 }
183
184 bool wxGenericFontDialog::DoCreate(wxWindow *parent)
185 {
186 if ( !wxDialog::Create( parent , -1 , _T("Choose Font") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
187 _T("fontdialog") ) )
188 {
189 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
190 return FALSE;
191 }
192
193 InitializeFont();
194 CreateWidgets();
195
196 // sets initial font in preview area
197 wxCommandEvent dummy;
198 OnChangeFont(dummy);
199
200 return TRUE;
201 }
202
203 int wxGenericFontDialog::ShowModal()
204 {
205 int ret = wxDialog::ShowModal();
206
207 if (ret != wxID_CANCEL)
208 {
209 m_fontData.m_chosenFont = dialogFont;
210 }
211
212 return ret;
213 }
214
215 void wxGenericFontDialog::CreateWidgets()
216 {
217 wxString
218 *families = new wxString[6],
219 *styles = new wxString[3],
220 *weights = new wxString[3];
221 families[0] = _("Roman");
222 families[1] = _("Decorative");
223 families[2] = _("Modern");
224 families[3] = _("Script");
225 families[4] = _("Swiss" );
226 families[5] = _("Teletype" );
227 styles[0] = _("Normal");
228 styles[1] = _("Italic");
229 styles[2] = _("Slant");
230 weights[0] = _("Normal");
231 weights[1] = _("Light");
232 weights[2] = _("Bold");
233
234 familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxDefaultPosition, wxDefaultSize, 5, families);
235 styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxDefaultPosition, wxDefaultSize, 3, styles);
236 weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxDefaultPosition, wxDefaultSize, 3, weights);
237
238 colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxDefaultPosition, wxDefaultSize, NUM_COLS, wxColourDialogNames);
239
240 wxString *pointSizes = new wxString[40];
241 int i;
242 for ( i = 0; i < 40; i++)
243 {
244 wxChar buf[5];
245 wxSprintf(buf, wxT("%d"), i + 1);
246 pointSizes[i] = buf;
247 }
248
249 pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes);
250 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"));
251
252 m_previewer = new wxFontPreviewer(this);
253
254 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
255 wxButton *cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel"));
256
257 familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
258 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
259 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
260 wxString name(wxTheColourDatabase->FindName(m_fontData.GetColour()));
261 colourChoice->SetStringSelection(name);
262
263 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
264 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
265
266 okButton->SetDefault();
267
268 // layout
269
270 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
271
272
273 wxSizer *topsizer, *sizer;
274 topsizer = new wxBoxSizer(wxVERTICAL);
275
276 if (!is_pda)
277 {
278 // 2 row design
279 sizer = new wxBoxSizer(wxHORIZONTAL);
280 sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
281 sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
282 sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
283 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
284
285 sizer = new wxBoxSizer(wxHORIZONTAL);
286 sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
287 sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
288 sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10);
289 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
290 }
291 else
292 {
293 // 3 row design
294 sizer = new wxBoxSizer(wxHORIZONTAL);
295 sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
296 sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
297 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
298
299 sizer = new wxBoxSizer(wxHORIZONTAL);
300 sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
301 sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
302 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
303
304 sizer = new wxBoxSizer(wxHORIZONTAL);
305 sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
306 sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10);
307 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
308 }
309
310 topsizer->Add(m_previewer, 1, wxALL | wxEXPAND, 10);
311 topsizer->SetItemMinSize(m_previewer, 430, 100);
312
313 sizer = new wxBoxSizer(wxHORIZONTAL);
314 sizer->Add(okButton, 0, wxRIGHT, 10);
315 sizer->Add(cancelButton, 0, wxRIGHT, 10);
316 topsizer->Add(sizer, 0, wxALIGN_RIGHT | wxBOTTOM, 10);
317
318 SetAutoLayout(TRUE);
319 SetSizer(topsizer);
320 topsizer->SetSizeHints(this);
321 topsizer->Fit(this);
322
323 Centre(wxBOTH);
324
325 delete[] families;
326 delete[] styles;
327 delete[] weights;
328 delete[] pointSizes;
329
330 // Don't block events any more
331 m_useEvents = TRUE;
332 }
333
334 void wxGenericFontDialog::InitializeFont()
335 {
336 int fontFamily = wxSWISS;
337 int fontWeight = wxNORMAL;
338 int fontStyle = wxNORMAL;
339 int fontSize = 12;
340 int fontUnderline = FALSE;
341
342 if (m_fontData.m_initialFont.Ok())
343 {
344 fontFamily = m_fontData.m_initialFont.GetFamily();
345 fontWeight = m_fontData.m_initialFont.GetWeight();
346 fontStyle = m_fontData.m_initialFont.GetStyle();
347 fontSize = m_fontData.m_initialFont.GetPointSize();
348 fontUnderline = m_fontData.m_initialFont.GetUnderlined();
349 }
350
351 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
352
353 if (m_previewer)
354 m_previewer->SetFont(dialogFont);
355 }
356
357 void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
358 {
359 if (!m_useEvents) return;
360
361 int fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
362 int fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
363 int fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
364 int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
365 int fontUnderline = underLineCheckBox->GetValue();
366
367 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
368 m_previewer->SetFont(dialogFont);
369 if (colourChoice->GetStringSelection() != wxT(""))
370 {
371 wxColour col = wxTheColourDatabase->Find(colourChoice->GetStringSelection());
372 if (col.Ok())
373 {
374 m_fontData.m_fontColour = col;
375 m_previewer->SetForegroundColour(col);
376 }
377 }
378 m_previewer->Refresh();
379 }
380
381 const wxChar *wxFontWeightIntToString(int weight)
382 {
383 switch (weight)
384 {
385 case wxLIGHT:
386 return wxT("Light");
387 case wxBOLD:
388 return wxT("Bold");
389 case wxNORMAL:
390 default:
391 return wxT("Normal");
392 }
393 }
394
395 const wxChar *wxFontStyleIntToString(int style)
396 {
397 switch (style)
398 {
399 case wxITALIC:
400 return wxT("Italic");
401 case wxSLANT:
402 return wxT("Slant");
403 case wxNORMAL:
404 default:
405 return wxT("Normal");
406 }
407 }
408
409 const wxChar *wxFontFamilyIntToString(int family)
410 {
411 switch (family)
412 {
413 case wxROMAN:
414 return wxT("Roman");
415 case wxDECORATIVE:
416 return wxT("Decorative");
417 case wxMODERN:
418 return wxT("Modern");
419 case wxSCRIPT:
420 return wxT("Script");
421 case wxTELETYPE:
422 return wxT("Teletype");
423 case wxSWISS:
424 default:
425 return wxT("Swiss");
426 }
427 }
428
429 int wxFontFamilyStringToInt(wxChar *family)
430 {
431 if (!family)
432 return wxSWISS;
433
434 if (wxStrcmp(family, wxT("Roman")) == 0)
435 return wxROMAN;
436 else if (wxStrcmp(family, wxT("Decorative")) == 0)
437 return wxDECORATIVE;
438 else if (wxStrcmp(family, wxT("Modern")) == 0)
439 return wxMODERN;
440 else if (wxStrcmp(family, wxT("Script")) == 0)
441 return wxSCRIPT;
442 else if (wxStrcmp(family, wxT("Teletype")) == 0)
443 return wxTELETYPE;
444 else return wxSWISS;
445 }
446
447 int wxFontStyleStringToInt(wxChar *style)
448 {
449 if (!style)
450 return wxNORMAL;
451 if (wxStrcmp(style, wxT("Italic")) == 0)
452 return wxITALIC;
453 else if (wxStrcmp(style, wxT("Slant")) == 0)
454 return wxSLANT;
455 else
456 return wxNORMAL;
457 }
458
459 int wxFontWeightStringToInt(wxChar *weight)
460 {
461 if (!weight)
462 return wxNORMAL;
463 if (wxStrcmp(weight, wxT("Bold")) == 0)
464 return wxBOLD;
465 else if (wxStrcmp(weight, wxT("Light")) == 0)
466 return wxLIGHT;
467 else
468 return wxNORMAL;
469 }
470
471 #endif
472 // wxUSE_FONTDLG
473