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