Removed lots of OnClose functions; doc'ed OnCloseWindow better;
[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 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/utils.h"
26 #include "wx/dialog.h"
27 #include "wx/listbox.h"
28 #include "wx/button.h"
29 #include "wx/stattext.h"
30 #include "wx/layout.h"
31 #include "wx/dcclient.h"
32 #include "wx/choice.h"
33 #include "wx/checkbox.h"
34 #include <wx/intl.h>
35 #endif
36
37 #include <string.h>
38 #include <stdlib.h>
39
40 #include "wx/cmndata.h"
41 #include "wx/generic/fontdlgg.h"
42
43 #if !USE_SHARED_LIBRARY
44 IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog, wxDialog)
45
46 BEGIN_EVENT_TABLE(wxGenericFontDialog, wxDialog)
47 EVT_CHECKBOX(wxID_FONT_UNDERLINE, wxGenericFontDialog::OnChangeFont)
48 EVT_CHOICE(wxID_FONT_STYLE, wxGenericFontDialog::OnChangeFont)
49 EVT_CHOICE(wxID_FONT_WEIGHT, wxGenericFontDialog::OnChangeFont)
50 EVT_CHOICE(wxID_FONT_FAMILY, wxGenericFontDialog::OnChangeFont)
51 EVT_CHOICE(wxID_FONT_COLOUR, wxGenericFontDialog::OnChangeFont)
52 EVT_CHOICE(wxID_FONT_SIZE, wxGenericFontDialog::OnChangeFont)
53 EVT_PAINT(wxGenericFontDialog::OnPaint)
54 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow)
55 END_EVENT_TABLE()
56
57 #endif
58
59 #define NUM_COLS 48
60 static wxString wxColourDialogNames[NUM_COLS]={"ORANGE",
61 "GOLDENROD",
62 "WHEAT",
63 "SPRING GREEN",
64 "SKY BLUE",
65 "SLATE BLUE",
66 "MEDIUM VIOLET RED",
67 "PURPLE",
68
69 "RED",
70 "YELLOW",
71 "MEDIUM SPRING GREEN",
72 "PALE GREEN",
73 "CYAN",
74 "LIGHT STEEL BLUE",
75 "ORCHID",
76 "LIGHT MAGENTA",
77
78 "BROWN",
79 "YELLOW",
80 "GREEN",
81 "CADET BLUE",
82 "MEDIUM BLUE",
83 "MAGENTA",
84 "MAROON",
85 "ORANGE RED",
86
87 "FIREBRICK",
88 "CORAL",
89 "FOREST GREEN",
90 "AQUARAMINE",
91 "BLUE",
92 "NAVY",
93 "THISTLE",
94 "MEDIUM VIOLET RED",
95
96 "INDIAN RED",
97 "GOLD",
98 "MEDIUM SEA GREEN",
99 "MEDIUM BLUE",
100 "MIDNIGHT BLUE",
101 "GREY",
102 "PURPLE",
103 "KHAKI",
104
105 "BLACK",
106 "MEDIUM FOREST GREEN",
107 "KHAKI",
108 "DARK GREY",
109 "SEA GREEN",
110 "LIGHT GREY",
111 "MEDIUM SLATE BLUE",
112 "WHITE"
113 };
114
115 /*
116 * Generic wxFontDialog
117 */
118
119 wxGenericFontDialog::wxGenericFontDialog(void)
120 {
121 m_useEvents = FALSE;
122 dialogParent = NULL;
123 }
124
125 wxGenericFontDialog::wxGenericFontDialog(wxWindow *parent, wxFontData *data):
126 wxDialog(parent, -1, _("Font"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
127 {
128 m_useEvents = FALSE;
129 Create(parent, data);
130 }
131
132 wxGenericFontDialog::~wxGenericFontDialog(void)
133 {
134 }
135
136 void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& event)
137 {
138 EndModal(wxID_CANCEL);
139 }
140
141 bool wxGenericFontDialog::Create(wxWindow *parent, wxFontData *data)
142 {
143 dialogParent = parent;
144
145 if (data)
146 fontData = *data;
147
148 InitializeFont();
149 CreateWidgets();
150
151 return TRUE;
152 }
153
154 int wxGenericFontDialog::ShowModal(void)
155 {
156 int ret = wxDialog::ShowModal();
157
158 if (ret != wxID_CANCEL)
159 {
160 fontData.chosenFont = dialogFont;
161 }
162
163 return ret;
164 }
165
166
167 void wxGenericFontDialog::OnPaint(wxPaintEvent& event)
168 {
169 wxDialog::OnPaint(event);
170
171 wxPaintDC dc(this);
172 PaintFontBackground(dc);
173 PaintFont(dc);
174 }
175
176 /*
177 static void wxGenericChangeFontText(wxTextCtrl& text, wxCommandEvent& event)
178 {
179 if (event.GetEventType() == wxEVENT_TYPE_TEXT_ENTER_COMMAND)
180 {
181 wxGenericFontDialog *dialog = (wxGenericFontDialog *)text.GetParent();
182 dialog->OnChangeFont();
183 }
184 }
185 */
186
187 void wxGenericFontDialog::CreateWidgets(void)
188 {
189 wxBeginBusyCursor();
190
191 fontRect.x = 10;
192 #ifdef __X__
193 fontRect.y = 125;
194 #else
195 fontRect.y = 115;
196 #endif
197 fontRect.width = 430;
198 fontRect.height = 100;
199
200 /*
201 static char *families[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
202 static char *styles[] = { "Normal", "Italic", "Slant" };
203 static char *weights[] = { "Normal", "Light", "Bold" };
204 */
205
206 wxString
207 *families = new wxString[6],
208 *styles = new wxString[3],
209 *weights = new wxString[3];
210 families[0] = _("Roman");
211 families[1] = _("Decorative");
212 families[2] = _("Modern");
213 families[3] = _("Script");
214 families[4] = _("Swiss" );
215 families[5] = _("Teletype" );
216 styles[0] = _("Normal");
217 styles[1] = _("Italic");
218 styles[2] = _("Slant");
219 weights[0] = _("Normal");
220 weights[1] = _("Light");
221 weights[2] = _("Bold");
222
223 int x=-1;
224 int y=40;
225 familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxPoint(10, 10), wxSize(120, -1), 5, families);
226 styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxPoint(160, 10), wxSize(120, -1), 3, styles);
227 weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxPoint(310, 10), wxSize(120, -1), 3, weights);
228
229 colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxPoint(10, 40), wxSize(190, -1), NUM_COLS, wxColourDialogNames);
230 #if 0 // def __WXMOTIF__ // TODO: This necessary now?
231 // We want the pointSizeText to line up on the y axis with the colourChoice
232 colourChoice->GetPosition(&fontRect.x, &y); //NL mod
233 y+=3; //NL mod
234 #endif
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, wxPoint(230, y), wxSize(50, -1), 40, pointSizes);
246 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"), wxPoint(320, y));
247
248 int rectY;
249 pointSizeChoice->GetPosition(&x, &rectY); //NL mod
250 fontRect.y = rectY;
251 pointSizeChoice->GetSize(&x, &y); //NL mod
252
253 // Calculate the position of the bottom of the pointSizeChoice, and place
254 // the fontRect there (+5 for a nice gap)
255
256 fontRect.y+=y+5; //NL mod
257
258 int by = (fontRect.y + fontRect.height + 10);
259
260 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"), wxPoint(220, by), wxSize(100,-1));
261 (void) new wxButton(this, wxID_OK, _("Cancel"), wxPoint(340, by), wxSize(100,-1));
262
263 familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
264 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
265 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
266 wxString name(wxTheColourDatabase->FindName(fontData.fontColour));
267 colourChoice->SetStringSelection(name);
268
269 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
270 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
271
272 okButton->SetDefault();
273
274 SetClientSize(450, by + 40);
275
276 Centre(wxBOTH);
277
278 wxEndBusyCursor();
279
280 delete[] families;
281 delete[] styles;
282 delete[] weights;
283 delete[] pointSizes;
284 m_useEvents = TRUE;
285 }
286
287 void wxGenericFontDialog::InitializeFont(void)
288 {
289 int fontFamily = wxSWISS;
290 int fontWeight = wxNORMAL;
291 int fontStyle = wxNORMAL;
292 int fontSize = 12;
293 int fontUnderline = FALSE;
294 if (fontData.initialFont.Ok())
295 {
296 fontFamily = fontData.initialFont.GetFamily();
297 fontWeight = fontData.initialFont.GetWeight();
298 fontStyle = fontData.initialFont.GetStyle();
299 fontSize = fontData.initialFont.GetPointSize();
300 fontUnderline = fontData.initialFont.GetUnderlined();
301 }
302 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
303 }
304
305 void wxGenericFontDialog::PaintFontBackground(wxDC& dc)
306 {
307 dc.BeginDrawing();
308
309 dc.SetPen(*wxBLACK_PEN);
310 dc.SetBrush(*wxWHITE_BRUSH);
311 dc.DrawRectangle( fontRect.x, fontRect.y, fontRect.width, fontRect.height);
312 dc.EndDrawing();
313 }
314
315 void wxGenericFontDialog::PaintFont(wxDC& dc)
316 {
317 dc.BeginDrawing();
318 if (dialogFont.Ok())
319 {
320 dc.SetFont(dialogFont);
321 // Calculate vertical centre
322 long w, h;
323 dc.GetTextExtent("X", &w, &h);
324 float cx = (float)(fontRect.x + 10);
325 float cy = (float)(fontRect.y + (fontRect.height/2.0) - (h/2.0));
326 dc.SetTextForeground(fontData.fontColour);
327 dc.SetClippingRegion( fontRect.x, fontRect.y, (long)(fontRect.width-2.0), (long)(fontRect.height-2.0));
328 dc.DrawText(_("ABCDEFGabcdefg12345"), (long)cx, (long)cy);
329 dc.DestroyClippingRegion();
330 dc.SetFont(wxNullFont);
331 }
332 dc.EndDrawing();
333 }
334
335 void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
336 {
337 if (!m_useEvents) return;
338
339 int fontFamily = 0; /* shut up buggy egcs warnings */
340 fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
341 int fontWeight = 0;
342 fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
343 int fontStyle = 0;
344 fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
345 int fontSize = atoi(pointSizeChoice->GetStringSelection());
346 int fontUnderline = underLineCheckBox->GetValue();
347
348 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
349 if (colourChoice->GetStringSelection() != "")
350 {
351 wxColour *col = (wxColour*) NULL;
352 col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection());
353 if (col)
354 {
355 fontData.fontColour = *col;
356 }
357 }
358 wxClientDC dc(this);
359 PaintFontBackground(dc);
360 PaintFont(dc);
361 }
362
363 char *wxFontWeightIntToString(int weight)
364 {
365 switch (weight)
366 {
367 case wxLIGHT:
368 return "Light";
369 case wxBOLD:
370 return "Bold";
371 case wxNORMAL:
372 default:
373 return "Normal";
374 }
375 return "Normal";
376 }
377
378 char *wxFontStyleIntToString(int style)
379 {
380 switch (style)
381 {
382 case wxITALIC:
383 return "Italic";
384 case wxSLANT:
385 return "Slant";
386 case wxNORMAL:
387 default:
388 return "Normal";
389 }
390 return "Normal";
391 }
392
393 char *wxFontFamilyIntToString(int family)
394 {
395 switch (family)
396 {
397 case wxROMAN:
398 return "Roman";
399 case wxDECORATIVE:
400 return "Decorative";
401 case wxMODERN:
402 return "Modern";
403 case wxSCRIPT:
404 return "Script";
405 case wxTELETYPE:
406 return "Teletype";
407 case wxSWISS:
408 default:
409 return "Swiss";
410 }
411 return "Swiss";
412 }
413
414 int wxFontFamilyStringToInt(char *family)
415 {
416 if (!family)
417 return wxSWISS;
418
419 if (strcmp(family, "Roman") == 0)
420 return wxROMAN;
421 else if (strcmp(family, "Decorative") == 0)
422 return wxDECORATIVE;
423 else if (strcmp(family, "Modern") == 0)
424 return wxMODERN;
425 else if (strcmp(family, "Script") == 0)
426 return wxSCRIPT;
427 else if (strcmp(family, "Teletype") == 0)
428 return wxTELETYPE;
429 else return wxSWISS;
430 }
431
432 int wxFontStyleStringToInt(char *style)
433 {
434 if (!style)
435 return wxNORMAL;
436 if (strcmp(style, "Italic") == 0)
437 return wxITALIC;
438 else if (strcmp(style, "Slant") == 0)
439 return wxSLANT;
440 else
441 return wxNORMAL;
442 }
443
444 int wxFontWeightStringToInt(char *weight)
445 {
446 if (!weight)
447 return wxNORMAL;
448 if (strcmp(weight, "Bold") == 0)
449 return wxBOLD;
450 else if (strcmp(weight, "Light") == 0)
451 return wxLIGHT;
452 else
453 return wxNORMAL;
454 }
455
456