]> git.saurik.com Git - wxWidgets.git/blame - src/generic/fontdlgg.cpp
OS/2 VisualAge doesn't know about lower case true and false and redefine a for loop...
[wxWidgets.git] / src / generic / fontdlgg.cpp
CommitLineData
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
c35414db 9// Licence: wxWindows license
c801d85f
KB
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"
3096bd2f 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
c801d85f
KB
43IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog, wxDialog)
44
45BEGIN_EVENT_TABLE(wxGenericFontDialog, wxDialog)
c35414db
VZ
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)
c801d85f
KB
54END_EVENT_TABLE()
55
c801d85f
KB
56
57#define NUM_COLS 48
223d09f6
KB
58static 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")
c35414db 111 };
c801d85f
KB
112
113/*
114 * Generic wxFontDialog
115 */
116
117wxGenericFontDialog::wxGenericFontDialog(void)
118{
66bd6b93 119 m_useEvents = FALSE;
c801d85f
KB
120 dialogParent = NULL;
121}
122
123wxGenericFontDialog::wxGenericFontDialog(wxWindow *parent, wxFontData *data):
1a5a8367 124 wxDialog(parent, -1, _("Font"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
c801d85f 125{
66bd6b93 126 m_useEvents = FALSE;
c801d85f
KB
127 Create(parent, data);
128}
129
130wxGenericFontDialog::~wxGenericFontDialog(void)
131{
132}
133
74e3313b 134void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 135{
e3065973 136 EndModal(wxID_CANCEL);
c801d85f 137}
c35414db 138
c801d85f
KB
139bool wxGenericFontDialog::Create(wxWindow *parent, wxFontData *data)
140{
141 dialogParent = parent;
c35414db 142
c801d85f
KB
143 if (data)
144 fontData = *data;
145
146 InitializeFont();
147 CreateWidgets();
c35414db 148
c801d85f
KB
149 return TRUE;
150}
151
152int wxGenericFontDialog::ShowModal(void)
153{
154 int ret = wxDialog::ShowModal();
155
156 if (ret != wxID_CANCEL)
157 {
158 fontData.chosenFont = dialogFont;
159 }
160
c35414db 161 return ret;
c801d85f
KB
162}
163
164
af111fc3 165void wxGenericFontDialog::OnPaint(wxPaintEvent& WXUNUSED(event))
c801d85f 166{
c801d85f
KB
167 wxPaintDC dc(this);
168 PaintFontBackground(dc);
169 PaintFont(dc);
170}
171
172/*
173static 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
183void wxGenericFontDialog::CreateWidgets(void)
184{
185 wxBeginBusyCursor();
186
b527aac5 187 fontRect.x = 10;
c801d85f
KB
188#ifdef __X__
189 fontRect.y = 125;
190#else
191 fontRect.y = 115;
192#endif
b527aac5 193 fontRect.width = 430;
c801d85f
KB
194 fontRect.height = 100;
195
59043ba2
KB
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 */
c35414db 201
59043ba2 202 wxString
36b3b54a 203 *families = new wxString[6],
59043ba2
KB
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" );
36b3b54a 211 families[5] = _("Teletype" );
59043ba2
KB
212 styles[0] = _("Normal");
213 styles[1] = _("Italic");
214 styles[2] = _("Slant");
215 weights[0] = _("Normal");
216 weights[1] = _("Light");
217 weights[2] = _("Bold");
c35414db 218
c801d85f
KB
219 int x=-1;
220 int y=40;
221 familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxPoint(10, 10), wxSize(120, -1), 5, families);
98ffbab9
JS
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);
c801d85f 224
98ffbab9 225 colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxPoint(10, 40), wxSize(180, -1), NUM_COLS, wxColourDialogNames);
c030b70f 226#if 0 // def __WXMOTIF__ // TODO: This necessary now?
c801d85f
KB
227 // We want the pointSizeText to line up on the y axis with the colourChoice
228 colourChoice->GetPosition(&fontRect.x, &y); //NL mod
c35414db 229 y+=3; //NL mod
c801d85f
KB
230#endif
231
59043ba2 232 wxString *pointSizes = new wxString[40];
c801d85f
KB
233 int i;
234 for ( i = 0; i < 40; i++)
235 {
c35414db
VZ
236 char buf[5];
237 sprintf(buf, "%d", i + 1);
238 pointSizes[i] = buf;
c801d85f
KB
239 }
240
55acd85e
JS
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));
c801d85f
KB
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
c35414db 250 // the fontRect there (+5 for a nice gap)
c801d85f
KB
251
252 fontRect.y+=y+5; //NL mod
253
3502e687 254 int by = (fontRect.y + fontRect.height + 15);
c801d85f 255
3502e687
RR
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));
c801d85f 258
66bd6b93 259 familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
c801d85f
KB
260 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
261 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
262 wxString name(wxTheColourDatabase->FindName(fontData.fontColour));
263 colourChoice->SetStringSelection(name);
c35414db 264
c801d85f 265 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
66bd6b93 266 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
c801d85f
KB
267
268 okButton->SetDefault();
269
98ffbab9
JS
270 // SetClientSize(450, by + 40);
271 Fit();
c801d85f
KB
272
273 Centre(wxBOTH);
274
275 wxEndBusyCursor();
59043ba2
KB
276
277 delete[] families;
278 delete[] styles;
279 delete[] weights;
280 delete[] pointSizes;
66bd6b93 281 m_useEvents = TRUE;
c801d85f
KB
282}
283
284void 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));
c801d85f
KB
300}
301
302void 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
312void 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));
1a5a8367 325 dc.DrawText(_("ABCDEFGabcdefg12345"), (long)cx, (long)cy);
c801d85f 326 dc.DestroyClippingRegion();
c35414db 327 dc.SetFont(wxNullFont);
c801d85f
KB
328 }
329 dc.EndDrawing();
330}
331
332void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
333{
66bd6b93 334 if (!m_useEvents) return;
c35414db 335
bbe0af5b
RR
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());
87138c52 342 int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
c801d85f
KB
343 int fontUnderline = underLineCheckBox->GetValue();
344
345 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
223d09f6 346 if (colourChoice->GetStringSelection() != wxT(""))
c801d85f 347 {
bbe0af5b
RR
348 wxColour *col = (wxColour*) NULL;
349 col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection());
c801d85f
KB
350 if (col)
351 {
352 fontData.fontColour = *col;
353 }
354 }
355 wxClientDC dc(this);
356 PaintFontBackground(dc);
357 PaintFont(dc);
358}
359
87138c52 360wxChar *wxFontWeightIntToString(int weight)
c801d85f
KB
361{
362 switch (weight)
363 {
364 case wxLIGHT:
223d09f6 365 return wxT("Light");
c801d85f 366 case wxBOLD:
223d09f6 367 return wxT("Bold");
c801d85f
KB
368 case wxNORMAL:
369 default:
223d09f6 370 return wxT("Normal");
c801d85f 371 }
c801d85f
KB
372}
373
87138c52 374wxChar *wxFontStyleIntToString(int style)
c801d85f
KB
375{
376 switch (style)
377 {
378 case wxITALIC:
223d09f6 379 return wxT("Italic");
c801d85f 380 case wxSLANT:
223d09f6 381 return wxT("Slant");
c801d85f
KB
382 case wxNORMAL:
383 default:
223d09f6 384 return wxT("Normal");
c801d85f 385 }
c801d85f
KB
386}
387
87138c52 388wxChar *wxFontFamilyIntToString(int family)
c801d85f
KB
389{
390 switch (family)
391 {
392 case wxROMAN:
223d09f6 393 return wxT("Roman");
c801d85f 394 case wxDECORATIVE:
223d09f6 395 return wxT("Decorative");
c801d85f 396 case wxMODERN:
223d09f6 397 return wxT("Modern");
c801d85f 398 case wxSCRIPT:
223d09f6 399 return wxT("Script");
36b3b54a 400 case wxTELETYPE:
223d09f6 401 return wxT("Teletype");
c801d85f
KB
402 case wxSWISS:
403 default:
223d09f6 404 return wxT("Swiss");
c801d85f 405 }
c801d85f
KB
406}
407
87138c52 408int wxFontFamilyStringToInt(wxChar *family)
c801d85f
KB
409{
410 if (!family)
411 return wxSWISS;
c35414db 412
223d09f6 413 if (wxStrcmp(family, wxT("Roman")) == 0)
c801d85f 414 return wxROMAN;
223d09f6 415 else if (wxStrcmp(family, wxT("Decorative")) == 0)
c801d85f 416 return wxDECORATIVE;
223d09f6 417 else if (wxStrcmp(family, wxT("Modern")) == 0)
c801d85f 418 return wxMODERN;
223d09f6 419 else if (wxStrcmp(family, wxT("Script")) == 0)
c801d85f 420 return wxSCRIPT;
223d09f6 421 else if (wxStrcmp(family, wxT("Teletype")) == 0)
36b3b54a 422 return wxTELETYPE;
c801d85f
KB
423 else return wxSWISS;
424}
425
87138c52 426int wxFontStyleStringToInt(wxChar *style)
c801d85f
KB
427{
428 if (!style)
429 return wxNORMAL;
223d09f6 430 if (wxStrcmp(style, wxT("Italic")) == 0)
c801d85f 431 return wxITALIC;
223d09f6 432 else if (wxStrcmp(style, wxT("Slant")) == 0)
c801d85f
KB
433 return wxSLANT;
434 else
435 return wxNORMAL;
436}
437
87138c52 438int wxFontWeightStringToInt(wxChar *weight)
c801d85f
KB
439{
440 if (!weight)
441 return wxNORMAL;
223d09f6 442 if (wxStrcmp(weight, wxT("Bold")) == 0)
c801d85f 443 return wxBOLD;
223d09f6 444 else if (wxStrcmp(weight, wxT("Light")) == 0)
c801d85f
KB
445 return wxLIGHT;
446 else
447 return wxNORMAL;
448}
449
450