]> git.saurik.com Git - wxWidgets.git/blob - src/generic/fontdlgg.cpp
wxImageList ctor now takes the same arguments as the MSW version (but it's
[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 dialogParent = NULL;
120 }
121
122 wxGenericFontDialog::wxGenericFontDialog(wxWindow *parent, wxFontData *data):
123 wxDialog(parent, -1, "Font", wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
124 {
125 Create(parent, data);
126 }
127
128 wxGenericFontDialog::~wxGenericFontDialog(void)
129 {
130 }
131
132 bool wxGenericFontDialog::OnClose(void)
133 {
134 Show(FALSE);
135 return FALSE;
136 }
137
138 bool wxGenericFontDialog::Create(wxWindow *parent, wxFontData *data)
139 {
140 dialogParent = parent;
141
142 if (data)
143 fontData = *data;
144
145 InitializeFont();
146 CreateWidgets();
147
148 return TRUE;
149 }
150
151 int wxGenericFontDialog::ShowModal(void)
152 {
153 int ret = wxDialog::ShowModal();
154
155 if (ret != wxID_CANCEL)
156 {
157 fontData.chosenFont = dialogFont;
158 }
159
160 return ret;
161 }
162
163
164 void wxGenericFontDialog::OnPaint(wxPaintEvent& event)
165 {
166 wxDialog::OnPaint(event);
167
168 wxPaintDC dc(this);
169 PaintFontBackground(dc);
170 PaintFont(dc);
171 }
172
173 /*
174 static void wxGenericChangeFontText(wxTextCtrl& text, wxCommandEvent& event)
175 {
176 if (event.GetEventType() == wxEVENT_TYPE_TEXT_ENTER_COMMAND)
177 {
178 wxGenericFontDialog *dialog = (wxGenericFontDialog *)text.GetParent();
179 dialog->OnChangeFont();
180 }
181 }
182 */
183
184 void wxGenericFontDialog::CreateWidgets(void)
185 {
186 wxBeginBusyCursor();
187
188 fontRect.x = 5;
189 #ifdef __X__
190 fontRect.y = 125;
191 #else
192 fontRect.y = 115;
193 #endif
194 fontRect.width = 350;
195 fontRect.height = 100;
196
197 /*
198 static char *families[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
199 static char *styles[] = { "Normal", "Italic", "Slant" };
200 static char *weights[] = { "Normal", "Light", "Bold" };
201 */
202 static wxString families[] = { "Roman", "Decorative", "Modern", "Script", "Swiss" };
203 static wxString styles[] = { "Normal", "Italic", "Slant" };
204 static wxString weights[] = { "Normal", "Light", "Bold" };
205
206 int x=-1;
207 int y=40;
208 familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxPoint(10, 10), wxSize(120, -1), 5, families);
209 styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxPoint(140, 10), wxSize(120, -1), 3, styles);
210 weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxPoint(270, 10), wxSize(120, -1), 3, weights);
211
212 colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxPoint(10, 40), wxSize(190, -1), NUM_COLS, wxColourDialogNames);
213 #ifdef __MOTIF__
214 // We want the pointSizeText to line up on the y axis with the colourChoice
215 colourChoice->GetPosition(&fontRect.x, &y); //NL mod
216 y+=3; //NL mod
217 #endif
218
219 wxString pointSizes[40];
220 int i;
221 for ( i = 0; i < 40; i++)
222 {
223 char buf[5];
224 sprintf(buf, "%d", i + 1);
225 pointSizes[i] = buf;
226 }
227
228 pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxPoint(210, y), wxSize(50, -1), 40, pointSizes);
229 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, "Underline", wxPoint(280, y));
230
231 int rectY;
232 pointSizeChoice->GetPosition(&x, &rectY); //NL mod
233 fontRect.y = rectY;
234 pointSizeChoice->GetSize(&x, &y); //NL mod
235
236 // Calculate the position of the bottom of the pointSizeChoice, and place
237 // the fontRect there (+5 for a nice gap)
238
239 fontRect.y+=y+5; //NL mod
240
241 int by = (fontRect.y + fontRect.height + 5);
242
243 wxButton *okButton = new wxButton(this, wxID_OK, "OK", wxPoint(5, by));
244 (void) new wxButton(this, wxID_OK, "Cancel", wxPoint(50, by));
245
246 familyChoice->SetStringSelection(wxFontFamilyIntToString(dialogFont.GetFamily()));
247 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
248 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
249 wxString name(wxTheColourDatabase->FindName(fontData.fontColour));
250 colourChoice->SetStringSelection(name);
251
252 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
253
254 pointSizeChoice->SetSelection(dialogFont.GetPointSize());
255
256 okButton->SetDefault();
257
258 SetClientSize(400, by + 30);
259
260 Centre(wxBOTH);
261
262 wxEndBusyCursor();
263 }
264
265 void wxGenericFontDialog::InitializeFont(void)
266 {
267 int fontFamily = wxSWISS;
268 int fontWeight = wxNORMAL;
269 int fontStyle = wxNORMAL;
270 int fontSize = 12;
271 int fontUnderline = FALSE;
272 if (fontData.initialFont.Ok())
273 {
274 fontFamily = fontData.initialFont.GetFamily();
275 fontWeight = fontData.initialFont.GetWeight();
276 fontStyle = fontData.initialFont.GetStyle();
277 fontSize = fontData.initialFont.GetPointSize();
278 fontUnderline = fontData.initialFont.GetUnderlined();
279 }
280 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
281
282 }
283
284 void wxGenericFontDialog::PaintFontBackground(wxDC& dc)
285 {
286 dc.BeginDrawing();
287
288 dc.SetPen(*wxBLACK_PEN);
289 dc.SetBrush(*wxWHITE_BRUSH);
290 dc.DrawRectangle( fontRect.x, fontRect.y, fontRect.width, fontRect.height);
291 dc.EndDrawing();
292 }
293
294 void wxGenericFontDialog::PaintFont(wxDC& dc)
295 {
296 dc.BeginDrawing();
297 if (dialogFont.Ok())
298 {
299 dc.SetFont(dialogFont);
300 // Calculate vertical centre
301 long w, h;
302 dc.GetTextExtent("X", &w, &h);
303 float cx = (float)(fontRect.x + 10);
304 float cy = (float)(fontRect.y + (fontRect.height/2.0) - (h/2.0));
305 dc.SetTextForeground(fontData.fontColour);
306 dc.SetClippingRegion( fontRect.x, fontRect.y, (long)(fontRect.width-2.0), (long)(fontRect.height-2.0));
307 dc.DrawText("ABCDEFGabcdefg12345", (long)cx, (long)cy);
308 dc.DestroyClippingRegion();
309 dc.SetFont(wxNullFont);
310 }
311 dc.EndDrawing();
312 }
313
314 void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
315 {
316 int fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
317 int fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
318 int fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
319 int fontSize = atoi(pointSizeChoice->GetStringSelection());
320 int fontUnderline = underLineCheckBox->GetValue();
321
322 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
323 if (colourChoice->GetStringSelection() != "")
324 {
325 wxColour *col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection());
326 if (col)
327 {
328 fontData.fontColour = *col;
329 }
330 }
331 wxClientDC dc(this);
332 PaintFontBackground(dc);
333 PaintFont(dc);
334 }
335
336 char *wxFontWeightIntToString(int weight)
337 {
338 switch (weight)
339 {
340 case wxLIGHT:
341 return "Light";
342 case wxBOLD:
343 return "Bold";
344 case wxNORMAL:
345 default:
346 return "Normal";
347 }
348 return "Normal";
349 }
350
351 char *wxFontStyleIntToString(int style)
352 {
353 switch (style)
354 {
355 case wxITALIC:
356 return "Italic";
357 case wxSLANT:
358 return "Slant";
359 case wxNORMAL:
360 default:
361 return "Normal";
362 }
363 return "Normal";
364 }
365
366 char *wxFontFamilyIntToString(int family)
367 {
368 switch (family)
369 {
370 case wxROMAN:
371 return "Roman";
372 case wxDECORATIVE:
373 return "Decorative";
374 case wxMODERN:
375 return "Modern";
376 case wxSCRIPT:
377 return "Script";
378 case wxSWISS:
379 default:
380 return "Swiss";
381 }
382 return "Swiss";
383 }
384
385 int wxFontFamilyStringToInt(char *family)
386 {
387 if (!family)
388 return wxSWISS;
389
390 if (strcmp(family, "Roman") == 0)
391 return wxROMAN;
392 else if (strcmp(family, "Decorative") == 0)
393 return wxDECORATIVE;
394 else if (strcmp(family, "Modern") == 0)
395 return wxMODERN;
396 else if (strcmp(family, "Script") == 0)
397 return wxSCRIPT;
398 else return wxSWISS;
399 }
400
401 int wxFontStyleStringToInt(char *style)
402 {
403 if (!style)
404 return wxNORMAL;
405 if (strcmp(style, "Italic") == 0)
406 return wxITALIC;
407 else if (strcmp(style, "Slant") == 0)
408 return wxSLANT;
409 else
410 return wxNORMAL;
411 }
412
413 int wxFontWeightStringToInt(char *weight)
414 {
415 if (!weight)
416 return wxNORMAL;
417 if (strcmp(weight, "Bold") == 0)
418 return wxBOLD;
419 else if (strcmp(weight, "Light") == 0)
420 return wxLIGHT;
421 else
422 return wxNORMAL;
423 }
424
425