]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
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" | |
1a5a8367 | 34 | #include <wx/intl.h> |
c801d85f KB |
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) | |
e3065973 | 54 | EVT_CLOSE(wxGenericFontDialog::OnCloseWindow) |
c801d85f KB |
55 | END_EVENT_TABLE() |
56 | ||
57 | #endif | |
58 | ||
59 | #define NUM_COLS 48 | |
6836fded OK |
60 | static wxString wxColourDialogNames[NUM_COLS]={_T("ORANGE"), |
61 | _T("GOLDENROD"), | |
62 | _T("WHEAT"), | |
63 | _T("SPRING GREEN"), | |
64 | _T("SKY BLUE"), | |
65 | _T("SLATE BLUE"), | |
66 | _T("MEDIUM VIOLET RED"), | |
67 | _T("PURPLE"), | |
68 | ||
69 | _T("RED"), | |
70 | _T("YELLOW"), | |
71 | _T("MEDIUM SPRING GREEN"), | |
72 | _T("PALE GREEN"), | |
73 | _T("CYAN"), | |
74 | _T("LIGHT STEEL BLUE"), | |
75 | _T("ORCHID"), | |
76 | _T("LIGHT MAGENTA"), | |
c801d85f | 77 | |
6836fded OK |
78 | _T("BROWN"), |
79 | _T("YELLOW"), | |
80 | _T("GREEN"), | |
81 | _T("CADET BLUE"), | |
82 | _T("MEDIUM BLUE"), | |
83 | _T("MAGENTA"), | |
84 | _T("MAROON"), | |
85 | _T("ORANGE RED"), | |
86 | ||
87 | _T("FIREBRICK"), | |
88 | _T("CORAL"), | |
89 | _T("FOREST GREEN"), | |
90 | _T("AQUARAMINE"), | |
91 | _T("BLUE"), | |
92 | _T("NAVY"), | |
93 | _T("THISTLE"), | |
94 | _T("MEDIUM VIOLET RED"), | |
c801d85f | 95 | |
6836fded OK |
96 | _T("INDIAN RED"), |
97 | _T("GOLD"), | |
98 | _T("MEDIUM SEA GREEN"), | |
99 | _T("MEDIUM BLUE"), | |
100 | _T("MIDNIGHT BLUE"), | |
101 | _T("GREY"), | |
102 | _T("PURPLE"), | |
103 | _T("KHAKI"), | |
c801d85f | 104 | |
6836fded OK |
105 | _T("BLACK"), |
106 | _T("MEDIUM FOREST GREEN"), | |
107 | _T("KHAKI"), | |
108 | _T("DARK GREY"), | |
109 | _T("SEA GREEN"), | |
110 | _T("LIGHT GREY"), | |
111 | _T("MEDIUM SLATE BLUE"), | |
112 | _T("WHITE") | |
c801d85f KB |
113 | }; |
114 | ||
115 | /* | |
116 | * Generic wxFontDialog | |
117 | */ | |
118 | ||
119 | wxGenericFontDialog::wxGenericFontDialog(void) | |
120 | { | |
66bd6b93 | 121 | m_useEvents = FALSE; |
c801d85f KB |
122 | dialogParent = NULL; |
123 | } | |
124 | ||
125 | wxGenericFontDialog::wxGenericFontDialog(wxWindow *parent, wxFontData *data): | |
1a5a8367 | 126 | wxDialog(parent, -1, _("Font"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL) |
c801d85f | 127 | { |
66bd6b93 | 128 | m_useEvents = FALSE; |
c801d85f KB |
129 | Create(parent, data); |
130 | } | |
131 | ||
132 | wxGenericFontDialog::~wxGenericFontDialog(void) | |
133 | { | |
134 | } | |
135 | ||
74e3313b | 136 | void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
c801d85f | 137 | { |
e3065973 | 138 | EndModal(wxID_CANCEL); |
c801d85f KB |
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 | ||
b527aac5 | 191 | fontRect.x = 10; |
c801d85f KB |
192 | #ifdef __X__ |
193 | fontRect.y = 125; | |
194 | #else | |
195 | fontRect.y = 115; | |
196 | #endif | |
b527aac5 | 197 | fontRect.width = 430; |
c801d85f KB |
198 | fontRect.height = 100; |
199 | ||
59043ba2 KB |
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 | |
36b3b54a | 207 | *families = new wxString[6], |
59043ba2 KB |
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" ); | |
36b3b54a | 215 | families[5] = _("Teletype" ); |
59043ba2 KB |
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 | ||
c801d85f KB |
223 | int x=-1; |
224 | int y=40; | |
225 | familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxPoint(10, 10), wxSize(120, -1), 5, families); | |
98ffbab9 JS |
226 | styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxPoint(170, 10), wxSize(120, -1), 3, styles); |
227 | weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxPoint(330, 10), wxSize(120, -1), 3, weights); | |
c801d85f | 228 | |
98ffbab9 | 229 | colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxPoint(10, 40), wxSize(180, -1), NUM_COLS, wxColourDialogNames); |
c030b70f | 230 | #if 0 // def __WXMOTIF__ // TODO: This necessary now? |
c801d85f KB |
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 | ||
59043ba2 | 236 | wxString *pointSizes = new wxString[40]; |
c801d85f KB |
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 | ||
55acd85e JS |
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)); | |
c801d85f KB |
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 | ||
3502e687 | 258 | int by = (fontRect.y + fontRect.height + 15); |
c801d85f | 259 | |
3502e687 RR |
260 | wxButton *okButton = new wxButton(this, wxID_OK, _("OK"), wxPoint(230, by), wxSize(75,-1)); |
261 | (void) new wxButton(this, wxID_OK, _("Cancel"), wxPoint(330, by), wxSize(75,-1)); | |
c801d85f | 262 | |
66bd6b93 | 263 | familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) ); |
c801d85f KB |
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()); | |
66bd6b93 | 270 | pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1); |
c801d85f KB |
271 | |
272 | okButton->SetDefault(); | |
273 | ||
98ffbab9 JS |
274 | // SetClientSize(450, by + 40); |
275 | Fit(); | |
c801d85f KB |
276 | |
277 | Centre(wxBOTH); | |
278 | ||
279 | wxEndBusyCursor(); | |
59043ba2 KB |
280 | |
281 | delete[] families; | |
282 | delete[] styles; | |
283 | delete[] weights; | |
284 | delete[] pointSizes; | |
66bd6b93 | 285 | m_useEvents = TRUE; |
c801d85f KB |
286 | } |
287 | ||
288 | void wxGenericFontDialog::InitializeFont(void) | |
289 | { | |
290 | int fontFamily = wxSWISS; | |
291 | int fontWeight = wxNORMAL; | |
292 | int fontStyle = wxNORMAL; | |
293 | int fontSize = 12; | |
294 | int fontUnderline = FALSE; | |
295 | if (fontData.initialFont.Ok()) | |
296 | { | |
297 | fontFamily = fontData.initialFont.GetFamily(); | |
298 | fontWeight = fontData.initialFont.GetWeight(); | |
299 | fontStyle = fontData.initialFont.GetStyle(); | |
300 | fontSize = fontData.initialFont.GetPointSize(); | |
301 | fontUnderline = fontData.initialFont.GetUnderlined(); | |
302 | } | |
303 | dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0)); | |
c801d85f KB |
304 | } |
305 | ||
306 | void wxGenericFontDialog::PaintFontBackground(wxDC& dc) | |
307 | { | |
308 | dc.BeginDrawing(); | |
309 | ||
310 | dc.SetPen(*wxBLACK_PEN); | |
311 | dc.SetBrush(*wxWHITE_BRUSH); | |
312 | dc.DrawRectangle( fontRect.x, fontRect.y, fontRect.width, fontRect.height); | |
313 | dc.EndDrawing(); | |
314 | } | |
315 | ||
316 | void wxGenericFontDialog::PaintFont(wxDC& dc) | |
317 | { | |
318 | dc.BeginDrawing(); | |
319 | if (dialogFont.Ok()) | |
320 | { | |
321 | dc.SetFont(dialogFont); | |
322 | // Calculate vertical centre | |
323 | long w, h; | |
324 | dc.GetTextExtent("X", &w, &h); | |
325 | float cx = (float)(fontRect.x + 10); | |
326 | float cy = (float)(fontRect.y + (fontRect.height/2.0) - (h/2.0)); | |
327 | dc.SetTextForeground(fontData.fontColour); | |
328 | dc.SetClippingRegion( fontRect.x, fontRect.y, (long)(fontRect.width-2.0), (long)(fontRect.height-2.0)); | |
1a5a8367 | 329 | dc.DrawText(_("ABCDEFGabcdefg12345"), (long)cx, (long)cy); |
c801d85f KB |
330 | dc.DestroyClippingRegion(); |
331 | dc.SetFont(wxNullFont); | |
332 | } | |
333 | dc.EndDrawing(); | |
334 | } | |
335 | ||
336 | void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event)) | |
337 | { | |
66bd6b93 RR |
338 | if (!m_useEvents) return; |
339 | ||
bbe0af5b RR |
340 | int fontFamily = 0; /* shut up buggy egcs warnings */ |
341 | fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection()); | |
342 | int fontWeight = 0; | |
343 | fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection()); | |
344 | int fontStyle = 0; | |
345 | fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection()); | |
87138c52 | 346 | int fontSize = wxAtoi(pointSizeChoice->GetStringSelection()); |
c801d85f KB |
347 | int fontUnderline = underLineCheckBox->GetValue(); |
348 | ||
349 | dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0)); | |
87138c52 | 350 | if (colourChoice->GetStringSelection() != _T("")) |
c801d85f | 351 | { |
bbe0af5b RR |
352 | wxColour *col = (wxColour*) NULL; |
353 | col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection()); | |
c801d85f KB |
354 | if (col) |
355 | { | |
356 | fontData.fontColour = *col; | |
357 | } | |
358 | } | |
359 | wxClientDC dc(this); | |
360 | PaintFontBackground(dc); | |
361 | PaintFont(dc); | |
362 | } | |
363 | ||
87138c52 | 364 | wxChar *wxFontWeightIntToString(int weight) |
c801d85f KB |
365 | { |
366 | switch (weight) | |
367 | { | |
368 | case wxLIGHT: | |
87138c52 | 369 | return _T("Light"); |
c801d85f | 370 | case wxBOLD: |
87138c52 | 371 | return _T("Bold"); |
c801d85f KB |
372 | case wxNORMAL: |
373 | default: | |
87138c52 | 374 | return _T("Normal"); |
c801d85f | 375 | } |
87138c52 | 376 | return _T("Normal"); |
c801d85f KB |
377 | } |
378 | ||
87138c52 | 379 | wxChar *wxFontStyleIntToString(int style) |
c801d85f KB |
380 | { |
381 | switch (style) | |
382 | { | |
383 | case wxITALIC: | |
87138c52 | 384 | return _T("Italic"); |
c801d85f | 385 | case wxSLANT: |
87138c52 | 386 | return _T("Slant"); |
c801d85f KB |
387 | case wxNORMAL: |
388 | default: | |
87138c52 | 389 | return _T("Normal"); |
c801d85f | 390 | } |
87138c52 | 391 | return _T("Normal"); |
c801d85f KB |
392 | } |
393 | ||
87138c52 | 394 | wxChar *wxFontFamilyIntToString(int family) |
c801d85f KB |
395 | { |
396 | switch (family) | |
397 | { | |
398 | case wxROMAN: | |
87138c52 | 399 | return _T("Roman"); |
c801d85f | 400 | case wxDECORATIVE: |
87138c52 | 401 | return _T("Decorative"); |
c801d85f | 402 | case wxMODERN: |
87138c52 | 403 | return _T("Modern"); |
c801d85f | 404 | case wxSCRIPT: |
87138c52 | 405 | return _T("Script"); |
36b3b54a | 406 | case wxTELETYPE: |
87138c52 | 407 | return _T("Teletype"); |
c801d85f KB |
408 | case wxSWISS: |
409 | default: | |
87138c52 | 410 | return _T("Swiss"); |
c801d85f | 411 | } |
87138c52 | 412 | return _T("Swiss"); |
c801d85f KB |
413 | } |
414 | ||
87138c52 | 415 | int wxFontFamilyStringToInt(wxChar *family) |
c801d85f KB |
416 | { |
417 | if (!family) | |
418 | return wxSWISS; | |
419 | ||
87138c52 | 420 | if (wxStrcmp(family, _T("Roman")) == 0) |
c801d85f | 421 | return wxROMAN; |
87138c52 | 422 | else if (wxStrcmp(family, _T("Decorative")) == 0) |
c801d85f | 423 | return wxDECORATIVE; |
87138c52 | 424 | else if (wxStrcmp(family, _T("Modern")) == 0) |
c801d85f | 425 | return wxMODERN; |
87138c52 | 426 | else if (wxStrcmp(family, _T("Script")) == 0) |
c801d85f | 427 | return wxSCRIPT; |
87138c52 | 428 | else if (wxStrcmp(family, _T("Teletype")) == 0) |
36b3b54a | 429 | return wxTELETYPE; |
c801d85f KB |
430 | else return wxSWISS; |
431 | } | |
432 | ||
87138c52 | 433 | int wxFontStyleStringToInt(wxChar *style) |
c801d85f KB |
434 | { |
435 | if (!style) | |
436 | return wxNORMAL; | |
87138c52 | 437 | if (wxStrcmp(style, _T("Italic")) == 0) |
c801d85f | 438 | return wxITALIC; |
87138c52 | 439 | else if (wxStrcmp(style, _T("Slant")) == 0) |
c801d85f KB |
440 | return wxSLANT; |
441 | else | |
442 | return wxNORMAL; | |
443 | } | |
444 | ||
87138c52 | 445 | int wxFontWeightStringToInt(wxChar *weight) |
c801d85f KB |
446 | { |
447 | if (!weight) | |
448 | return wxNORMAL; | |
87138c52 | 449 | if (wxStrcmp(weight, _T("Bold")) == 0) |
c801d85f | 450 | return wxBOLD; |
87138c52 | 451 | else if (wxStrcmp(weight, _T("Light")) == 0) |
c801d85f KB |
452 | return wxLIGHT; |
453 | else | |
454 | return wxNORMAL; | |
455 | } | |
456 | ||
457 |