]> git.saurik.com Git - wxWidgets.git/blame - src/generic/fontdlgg.cpp
don't free user data before sending wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, this prevent...
[wxWidgets.git] / src / generic / fontdlgg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
c2c59b22 2// Name: src/generic/fontdlgg.cpp
c801d85f
KB
3// Purpose: Generic font dialog
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
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
47e118ba 23#if wxUSE_FONTDLG && (!defined(__WXGTK__) || defined(__WXGPE__) || defined(__WXUNIVERSAL__))
4234ea8b 24
c801d85f 25#ifndef WX_PRECOMP
53cf79fa
VS
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"
36 #include "wx/intl.h"
c801d85f
KB
37#endif
38
39#include <string.h>
40#include <stdlib.h>
41
42#include "wx/cmndata.h"
53cf79fa 43#include "wx/sizer.h"
c2c59b22 44#include "wx/fontdlg.h"
23c47bc1 45#include "wx/generic/fontdlgg.h"
47e118ba 46#include "wx/settings.h"
c801d85f 47
53cf79fa
VS
48//-----------------------------------------------------------------------------
49// helper class - wxFontPreviewer
50//-----------------------------------------------------------------------------
51
52class WXDLLEXPORT wxFontPreviewer : public wxWindow
53{
54public:
dabbc6a5 55 wxFontPreviewer(wxWindow *parent) : wxWindow(parent, wxID_ANY) {}
53cf79fa
VS
56
57private:
58 void OnPaint(wxPaintEvent& event);
59 DECLARE_EVENT_TABLE()
60};
61
62BEGIN_EVENT_TABLE(wxFontPreviewer, wxWindow)
63 EVT_PAINT(wxFontPreviewer::OnPaint)
64END_EVENT_TABLE()
65
66void wxFontPreviewer::OnPaint(wxPaintEvent& WXUNUSED(event))
67{
68 wxPaintDC dc(this);
69
70 wxSize size = GetSize();
71 wxFont font = GetFont();
72
73 dc.SetPen(*wxBLACK_PEN);
74 dc.SetBrush(*wxWHITE_BRUSH);
75 dc.DrawRectangle(0, 0, size.x, size.y);
76
77 if ( font.Ok() )
78 {
2b5f62a0 79 dc.SetFont(font);
53cf79fa
VS
80 // Calculate vertical centre
81 long w, h;
2b5f62a0 82 dc.GetTextExtent( wxT("X"), &w, &h);
53cf79fa
VS
83 dc.SetTextForeground(GetForegroundColour());
84 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
dabbc6a5 85 dc.DrawText(_("ABCDEFGabcdefg12345"),
2b5f62a0 86 10, size.y/2 - h/2);
53cf79fa
VS
87 dc.DestroyClippingRegion();
88 }
89}
90
91//-----------------------------------------------------------------------------
92// wxGenericFontDialog
93//-----------------------------------------------------------------------------
94
c801d85f
KB
95IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog, wxDialog)
96
97BEGIN_EVENT_TABLE(wxGenericFontDialog, wxDialog)
c35414db
VZ
98 EVT_CHECKBOX(wxID_FONT_UNDERLINE, wxGenericFontDialog::OnChangeFont)
99 EVT_CHOICE(wxID_FONT_STYLE, wxGenericFontDialog::OnChangeFont)
100 EVT_CHOICE(wxID_FONT_WEIGHT, wxGenericFontDialog::OnChangeFont)
101 EVT_CHOICE(wxID_FONT_FAMILY, wxGenericFontDialog::OnChangeFont)
102 EVT_CHOICE(wxID_FONT_COLOUR, wxGenericFontDialog::OnChangeFont)
103 EVT_CHOICE(wxID_FONT_SIZE, wxGenericFontDialog::OnChangeFont)
c35414db 104 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow)
c801d85f
KB
105END_EVENT_TABLE()
106
c801d85f
KB
107
108#define NUM_COLS 48
223d09f6
KB
109static wxString wxColourDialogNames[NUM_COLS]={wxT("ORANGE"),
110 wxT("GOLDENROD"),
111 wxT("WHEAT"),
112 wxT("SPRING GREEN"),
113 wxT("SKY BLUE"),
114 wxT("SLATE BLUE"),
115 wxT("MEDIUM VIOLET RED"),
116 wxT("PURPLE"),
117
118 wxT("RED"),
119 wxT("YELLOW"),
120 wxT("MEDIUM SPRING GREEN"),
121 wxT("PALE GREEN"),
122 wxT("CYAN"),
123 wxT("LIGHT STEEL BLUE"),
124 wxT("ORCHID"),
125 wxT("LIGHT MAGENTA"),
126
127 wxT("BROWN"),
128 wxT("YELLOW"),
129 wxT("GREEN"),
130 wxT("CADET BLUE"),
131 wxT("MEDIUM BLUE"),
132 wxT("MAGENTA"),
133 wxT("MAROON"),
134 wxT("ORANGE RED"),
135
136 wxT("FIREBRICK"),
137 wxT("CORAL"),
138 wxT("FOREST GREEN"),
139 wxT("AQUARAMINE"),
140 wxT("BLUE"),
141 wxT("NAVY"),
142 wxT("THISTLE"),
143 wxT("MEDIUM VIOLET RED"),
144
145 wxT("INDIAN RED"),
146 wxT("GOLD"),
147 wxT("MEDIUM SEA GREEN"),
148 wxT("MEDIUM BLUE"),
149 wxT("MIDNIGHT BLUE"),
150 wxT("GREY"),
151 wxT("PURPLE"),
152 wxT("KHAKI"),
153
154 wxT("BLACK"),
155 wxT("MEDIUM FOREST GREEN"),
156 wxT("KHAKI"),
157 wxT("DARK GREY"),
158 wxT("SEA GREEN"),
159 wxT("LIGHT GREY"),
160 wxT("MEDIUM SLATE BLUE"),
161 wxT("WHITE")
c35414db 162 };
c801d85f
KB
163
164/*
165 * Generic wxFontDialog
166 */
167
c2c59b22 168void wxGenericFontDialog::Init()
c801d85f 169{
dabbc6a5 170 m_useEvents = false;
53cf79fa 171 m_previewer = NULL;
1bab9242 172 Create( m_parent ) ;
c801d85f
KB
173}
174
c2c59b22 175wxGenericFontDialog::~wxGenericFontDialog()
c801d85f
KB
176{
177}
178
74e3313b 179void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 180{
47e118ba 181 EndModal(wxID_CANCEL);
c801d85f 182}
c35414db 183
c2c59b22 184bool wxGenericFontDialog::DoCreate(wxWindow *parent)
c801d85f 185{
dabbc6a5 186 if ( !wxDialog::Create( parent , wxID_ANY , _T("Choose Font") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
1bab9242
SC
187 _T("fontdialog") ) )
188 {
189 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
dabbc6a5 190 return false;
1bab9242 191 }
fc8eba27 192
47e118ba
RR
193 InitializeFont();
194 CreateWidgets();
dabbc6a5 195
47e118ba
RR
196 // sets initial font in preview area
197 wxCommandEvent dummy;
198 OnChangeFont(dummy);
dabbc6a5
DS
199
200 return true;
c801d85f
KB
201}
202
c2c59b22 203int wxGenericFontDialog::ShowModal()
c801d85f 204{
1bab9242 205 int ret = wxDialog::ShowModal();
c801d85f
KB
206
207 if (ret != wxID_CANCEL)
208 {
ae500232 209 m_fontData.m_chosenFont = dialogFont;
c801d85f
KB
210 }
211
c35414db 212 return ret;
c801d85f
KB
213}
214
c2c59b22 215void wxGenericFontDialog::CreateWidgets()
c801d85f 216{
47e118ba 217 wxString
36b3b54a 218 *families = new wxString[6],
59043ba2
KB
219 *styles = new wxString[3],
220 *weights = new wxString[3];
47e118ba
RR
221 families[0] = _("Roman");
222 families[1] = _("Decorative");
223 families[2] = _("Modern");
224 families[3] = _("Script");
225 families[4] = _("Swiss" );
226 families[5] = _("Teletype" );
227 styles[0] = _("Normal");
228 styles[1] = _("Italic");
229 styles[2] = _("Slant");
230 weights[0] = _("Normal");
231 weights[1] = _("Light");
232 weights[2] = _("Bold");
233
234 familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxDefaultPosition, wxDefaultSize, 5, families);
235 styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxDefaultPosition, wxDefaultSize, 3, styles);
236 weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxDefaultPosition, wxDefaultSize, 3, weights);
237
238 colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxDefaultPosition, wxDefaultSize, NUM_COLS, wxColourDialogNames);
239
240 wxString *pointSizes = new wxString[40];
241 int i;
242 for ( i = 0; i < 40; i++)
243 {
244 wxChar buf[5];
245 wxSprintf(buf, wxT("%d"), i + 1);
246 pointSizes[i] = buf;
247 }
c801d85f 248
47e118ba
RR
249 pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes);
250 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"));
c801d85f 251
47e118ba 252 m_previewer = new wxFontPreviewer(this);
c801d85f 253
47e118ba
RR
254 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
255 wxButton *cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel"));
c801d85f 256
47e118ba
RR
257 familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
258 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
259 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
260 wxString name(wxTheColourDatabase->FindName(m_fontData.GetColour()));
448971f3
RD
261 if (name.length())
262 colourChoice->SetStringSelection(name);
263 else
264 colourChoice->SetStringSelection(wxT("BLACK"));
265
47e118ba
RR
266 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
267 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
c801d85f 268
47e118ba 269 okButton->SetDefault();
c801d85f 270
47e118ba 271 // layout
53cf79fa 272
47e118ba 273 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
53cf79fa 274
47e118ba
RR
275
276 wxSizer *topsizer, *sizer;
277 topsizer = new wxBoxSizer(wxVERTICAL);
278
279 if (!is_pda)
280 {
281 // 2 row design
282 sizer = new wxBoxSizer(wxHORIZONTAL);
283 sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
284 sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
285 sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
286 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
287
288 sizer = new wxBoxSizer(wxHORIZONTAL);
289 sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
290 sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
291 sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10);
292 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
293 }
294 else
295 {
296 // 3 row design
297 sizer = new wxBoxSizer(wxHORIZONTAL);
298 sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
299 sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
300 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
301
302 sizer = new wxBoxSizer(wxHORIZONTAL);
303 sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
304 sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
305 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
dabbc6a5 306
47e118ba
RR
307 sizer = new wxBoxSizer(wxHORIZONTAL);
308 sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
309 sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10);
310 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
311 }
dabbc6a5 312
47e118ba
RR
313 topsizer->Add(m_previewer, 1, wxALL | wxEXPAND, 10);
314 topsizer->SetItemMinSize(m_previewer, 430, 100);
315
316 sizer = new wxBoxSizer(wxHORIZONTAL);
317 sizer->Add(okButton, 0, wxRIGHT, 10);
318 sizer->Add(cancelButton, 0, wxRIGHT, 10);
319 topsizer->Add(sizer, 0, wxALIGN_RIGHT | wxBOTTOM, 10);
320
47e118ba
RR
321 SetSizer(topsizer);
322 topsizer->SetSizeHints(this);
323 topsizer->Fit(this);
324
325 Centre(wxBOTH);
326
327 delete[] families;
328 delete[] styles;
329 delete[] weights;
330 delete[] pointSizes;
dabbc6a5 331
47e118ba 332 // Don't block events any more
dabbc6a5 333 m_useEvents = true;
c801d85f
KB
334}
335
c2c59b22 336void wxGenericFontDialog::InitializeFont()
c801d85f 337{
47e118ba
RR
338 int fontFamily = wxSWISS;
339 int fontWeight = wxNORMAL;
340 int fontStyle = wxNORMAL;
341 int fontSize = 12;
dabbc6a5 342 bool fontUnderline = false;
53cf79fa 343
47e118ba
RR
344 if (m_fontData.m_initialFont.Ok())
345 {
346 fontFamily = m_fontData.m_initialFont.GetFamily();
347 fontWeight = m_fontData.m_initialFont.GetWeight();
348 fontStyle = m_fontData.m_initialFont.GetStyle();
349 fontSize = m_fontData.m_initialFont.GetPointSize();
350 fontUnderline = m_fontData.m_initialFont.GetUnderlined();
351 }
c801d85f 352
dabbc6a5
DS
353 dialogFont = wxFont(fontSize, fontFamily, fontStyle,
354 fontWeight, fontUnderline);
c801d85f 355
47e118ba
RR
356 if (m_previewer)
357 m_previewer->SetFont(dialogFont);
c801d85f
KB
358}
359
360void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
361{
47e118ba 362 if (!m_useEvents) return;
c35414db 363
47e118ba
RR
364 int fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
365 int fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
366 int fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
367 int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
368 int fontUnderline = underLineCheckBox->GetValue();
c801d85f 369
47e118ba
RR
370 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
371 m_previewer->SetFont(dialogFont);
dabbc6a5 372 if ( !colourChoice->GetStringSelection().empty() )
c801d85f 373 {
47e118ba
RR
374 wxColour col = wxTheColourDatabase->Find(colourChoice->GetStringSelection());
375 if (col.Ok())
376 {
377 m_fontData.m_fontColour = col;
378 m_previewer->SetForegroundColour(col);
379 }
c801d85f 380 }
47e118ba 381 m_previewer->Refresh();
c801d85f
KB
382}
383
d0060e77 384const wxChar *wxFontWeightIntToString(int weight)
c801d85f
KB
385{
386 switch (weight)
387 {
388 case wxLIGHT:
223d09f6 389 return wxT("Light");
c801d85f 390 case wxBOLD:
223d09f6 391 return wxT("Bold");
c801d85f
KB
392 case wxNORMAL:
393 default:
223d09f6 394 return wxT("Normal");
c801d85f 395 }
c801d85f
KB
396}
397
d0060e77 398const wxChar *wxFontStyleIntToString(int style)
c801d85f
KB
399{
400 switch (style)
401 {
402 case wxITALIC:
223d09f6 403 return wxT("Italic");
c801d85f 404 case wxSLANT:
223d09f6 405 return wxT("Slant");
c801d85f
KB
406 case wxNORMAL:
407 default:
223d09f6 408 return wxT("Normal");
c801d85f 409 }
c801d85f
KB
410}
411
d0060e77 412const wxChar *wxFontFamilyIntToString(int family)
c801d85f
KB
413{
414 switch (family)
415 {
416 case wxROMAN:
223d09f6 417 return wxT("Roman");
c801d85f 418 case wxDECORATIVE:
223d09f6 419 return wxT("Decorative");
c801d85f 420 case wxMODERN:
223d09f6 421 return wxT("Modern");
c801d85f 422 case wxSCRIPT:
223d09f6 423 return wxT("Script");
36b3b54a 424 case wxTELETYPE:
223d09f6 425 return wxT("Teletype");
c801d85f
KB
426 case wxSWISS:
427 default:
223d09f6 428 return wxT("Swiss");
c801d85f 429 }
c801d85f
KB
430}
431
87138c52 432int wxFontFamilyStringToInt(wxChar *family)
c801d85f
KB
433{
434 if (!family)
435 return wxSWISS;
c35414db 436
223d09f6 437 if (wxStrcmp(family, wxT("Roman")) == 0)
c801d85f 438 return wxROMAN;
223d09f6 439 else if (wxStrcmp(family, wxT("Decorative")) == 0)
c801d85f 440 return wxDECORATIVE;
223d09f6 441 else if (wxStrcmp(family, wxT("Modern")) == 0)
c801d85f 442 return wxMODERN;
223d09f6 443 else if (wxStrcmp(family, wxT("Script")) == 0)
c801d85f 444 return wxSCRIPT;
223d09f6 445 else if (wxStrcmp(family, wxT("Teletype")) == 0)
36b3b54a 446 return wxTELETYPE;
c801d85f
KB
447 else return wxSWISS;
448}
449
87138c52 450int wxFontStyleStringToInt(wxChar *style)
c801d85f
KB
451{
452 if (!style)
453 return wxNORMAL;
223d09f6 454 if (wxStrcmp(style, wxT("Italic")) == 0)
c801d85f 455 return wxITALIC;
223d09f6 456 else if (wxStrcmp(style, wxT("Slant")) == 0)
c801d85f
KB
457 return wxSLANT;
458 else
459 return wxNORMAL;
460}
461
87138c52 462int wxFontWeightStringToInt(wxChar *weight)
c801d85f
KB
463{
464 if (!weight)
465 return wxNORMAL;
223d09f6 466 if (wxStrcmp(weight, wxT("Bold")) == 0)
c801d85f 467 return wxBOLD;
223d09f6 468 else if (wxStrcmp(weight, wxT("Light")) == 0)
c801d85f
KB
469 return wxLIGHT;
470 else
471 return wxNORMAL;
472}
473
3808e191
JS
474#endif
475 // wxUSE_FONTDLG
c801d85f 476