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