added creation of dialog - perhaps needed for all platforms
[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 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/fontdlg.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 void wxGenericFontDialog::Init()
167 {
168 m_useEvents = FALSE;
169 m_previewer = NULL;
170 Create( m_parent ) ;
171 }
172
173 wxGenericFontDialog::~wxGenericFontDialog()
174 {
175 }
176
177 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
178 {
179 EndModal(wxID_CANCEL);
180 }
181
182 bool wxGenericFontDialog::DoCreate(wxWindow *parent)
183 {
184 #ifdef __WXMAC__
185 if ( !wxDialog::Create( parent , -1 , _T("Choose Font") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
186 _T("fontdialog") ) )
187 {
188 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
189 return FALSE;
190 }
191 #endif
192 InitializeFont();
193 CreateWidgets();
194
195 return TRUE;
196 }
197
198 int wxGenericFontDialog::ShowModal()
199 {
200 int ret = wxDialog::ShowModal();
201
202 if (ret != wxID_CANCEL)
203 {
204 m_fontData.chosenFont = dialogFont;
205 }
206
207 return ret;
208 }
209
210 void wxGenericFontDialog::CreateWidgets()
211 {
212 wxBusyCursor bcur;
213
214 wxString
215 *families = new wxString[6],
216 *styles = new wxString[3],
217 *weights = new wxString[3];
218 families[0] = _("Roman");
219 families[1] = _("Decorative");
220 families[2] = _("Modern");
221 families[3] = _("Script");
222 families[4] = _("Swiss" );
223 families[5] = _("Teletype" );
224 styles[0] = _("Normal");
225 styles[1] = _("Italic");
226 styles[2] = _("Slant");
227 weights[0] = _("Normal");
228 weights[1] = _("Light");
229 weights[2] = _("Bold");
230
231 familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxDefaultPosition, wxDefaultSize, 5, families);
232 styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxDefaultPosition, wxDefaultSize, 3, styles);
233 weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxDefaultPosition, wxDefaultSize, 3, weights);
234
235 colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxDefaultPosition, wxDefaultSize, NUM_COLS, wxColourDialogNames);
236
237 wxString *pointSizes = new wxString[40];
238 int i;
239 for ( i = 0; i < 40; i++)
240 {
241 char buf[5];
242 sprintf(buf, "%d", i + 1);
243 pointSizes[i] = buf;
244 }
245
246 pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes);
247 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"));
248
249 m_previewer = new wxFontPreviewer(this);
250
251 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
252 wxButton *cancelButton = new wxButton(this, wxID_OK, _("Cancel"));
253
254 familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
255 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
256 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
257 wxString name(wxTheColourDatabase->FindName(m_fontData.fontColour));
258 colourChoice->SetStringSelection(name);
259
260 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
261 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
262
263 okButton->SetDefault();
264
265 wxSizer *topsizer, *sizer;
266 topsizer = new wxBoxSizer(wxVERTICAL);
267
268 sizer = new wxBoxSizer(wxHORIZONTAL);
269 sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
270 sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
271 sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
272 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
273
274 sizer = new wxBoxSizer(wxHORIZONTAL);
275 sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
276 sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
277 sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10);
278 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
279
280 topsizer->Add(m_previewer, 1, wxALL | wxEXPAND, 10);
281 topsizer->SetItemMinSize(m_previewer, 430, 100);
282
283 sizer = new wxBoxSizer(wxHORIZONTAL);
284 sizer->Add(okButton, 0, wxRIGHT, 10);
285 sizer->Add(cancelButton, 0, wxRIGHT, 10);
286 topsizer->Add(sizer, 0, wxALIGN_RIGHT | wxBOTTOM, 10);
287
288 SetAutoLayout(TRUE);
289 SetSizer(topsizer);
290 topsizer->SetSizeHints(this);
291 topsizer->Fit(this);
292
293 Centre(wxBOTH);
294
295 delete[] families;
296 delete[] styles;
297 delete[] weights;
298 delete[] pointSizes;
299 m_useEvents = TRUE;
300 }
301
302 void wxGenericFontDialog::InitializeFont()
303 {
304 int fontFamily = wxSWISS;
305 int fontWeight = wxNORMAL;
306 int fontStyle = wxNORMAL;
307 int fontSize = 12;
308 int fontUnderline = FALSE;
309
310 if (m_fontData.initialFont.Ok())
311 {
312 fontFamily = m_fontData.initialFont.GetFamily();
313 fontWeight = m_fontData.initialFont.GetWeight();
314 fontStyle = m_fontData.initialFont.GetStyle();
315 fontSize = m_fontData.initialFont.GetPointSize();
316 fontUnderline = m_fontData.initialFont.GetUnderlined();
317 }
318
319 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
320
321 if (m_previewer)
322 m_previewer->SetFont(dialogFont);
323 }
324
325 void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
326 {
327 if (!m_useEvents) return;
328
329 int fontFamily = 0; /* shut up buggy egcs warnings */
330 fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
331 int fontWeight = 0;
332 fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
333 int fontStyle = 0;
334 fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
335 int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
336 int fontUnderline = underLineCheckBox->GetValue();
337
338 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
339 m_previewer->SetFont(dialogFont);
340 if (colourChoice->GetStringSelection() != wxT(""))
341 {
342 wxColour *col = (wxColour*) NULL;
343 col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection());
344 if (col)
345 {
346 m_fontData.fontColour = *col;
347 m_previewer->SetForegroundColour(*col);
348 }
349 }
350 m_previewer->Refresh();
351 }
352
353 const wxChar *wxFontWeightIntToString(int weight)
354 {
355 switch (weight)
356 {
357 case wxLIGHT:
358 return wxT("Light");
359 case wxBOLD:
360 return wxT("Bold");
361 case wxNORMAL:
362 default:
363 return wxT("Normal");
364 }
365 }
366
367 const wxChar *wxFontStyleIntToString(int style)
368 {
369 switch (style)
370 {
371 case wxITALIC:
372 return wxT("Italic");
373 case wxSLANT:
374 return wxT("Slant");
375 case wxNORMAL:
376 default:
377 return wxT("Normal");
378 }
379 }
380
381 const wxChar *wxFontFamilyIntToString(int family)
382 {
383 switch (family)
384 {
385 case wxROMAN:
386 return wxT("Roman");
387 case wxDECORATIVE:
388 return wxT("Decorative");
389 case wxMODERN:
390 return wxT("Modern");
391 case wxSCRIPT:
392 return wxT("Script");
393 case wxTELETYPE:
394 return wxT("Teletype");
395 case wxSWISS:
396 default:
397 return wxT("Swiss");
398 }
399 }
400
401 int wxFontFamilyStringToInt(wxChar *family)
402 {
403 if (!family)
404 return wxSWISS;
405
406 if (wxStrcmp(family, wxT("Roman")) == 0)
407 return wxROMAN;
408 else if (wxStrcmp(family, wxT("Decorative")) == 0)
409 return wxDECORATIVE;
410 else if (wxStrcmp(family, wxT("Modern")) == 0)
411 return wxMODERN;
412 else if (wxStrcmp(family, wxT("Script")) == 0)
413 return wxSCRIPT;
414 else if (wxStrcmp(family, wxT("Teletype")) == 0)
415 return wxTELETYPE;
416 else return wxSWISS;
417 }
418
419 int wxFontStyleStringToInt(wxChar *style)
420 {
421 if (!style)
422 return wxNORMAL;
423 if (wxStrcmp(style, wxT("Italic")) == 0)
424 return wxITALIC;
425 else if (wxStrcmp(style, wxT("Slant")) == 0)
426 return wxSLANT;
427 else
428 return wxNORMAL;
429 }
430
431 int wxFontWeightStringToInt(wxChar *weight)
432 {
433 if (!weight)
434 return wxNORMAL;
435 if (wxStrcmp(weight, wxT("Bold")) == 0)
436 return wxBOLD;
437 else if (wxStrcmp(weight, wxT("Light")) == 0)
438 return wxLIGHT;
439 else
440 return wxNORMAL;
441 }
442
443 #endif
444 // wxUSE_FONTDLG
445