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