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