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