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