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