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