]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/fontdlgg.cpp
SetSelection() for generic wxSpinCtrl (forgot to commit before)
[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 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#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(GetFont());
78 // Calculate vertical centre
79 long w, h;
80 dc.GetTextExtent("X", &w, &h);
81 dc.SetTextForeground(GetForegroundColour());
82 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
83 dc.DrawText(_("ABCDEFGabcdefg12345"),
84 10, h/2 + size.y/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#ifdef __WXMAC__
171 Create( m_parent ) ;
172#endif
173}
174
175wxGenericFontDialog::~wxGenericFontDialog()
176{
177}
178
179void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
180{
181 EndModal(wxID_CANCEL);
182}
183
184bool wxGenericFontDialog::DoCreate(wxWindow *parent)
185{
186#ifdef __WXMAC__
187 if ( !wxDialog::Create( parent , -1 , _T("Choose Font") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
188 _T("fontdialog") ) )
189 {
190 wxFAIL_MSG( wxT("wxFontDialog creation failed") );
191 return FALSE;
192 }
193#endif
194 InitializeFont();
195 CreateWidgets();
196
197 return TRUE;
198}
199
200int wxGenericFontDialog::ShowModal()
201{
202 int ret = wxDialog::ShowModal();
203
204 if (ret != wxID_CANCEL)
205 {
206 m_fontData.chosenFont = dialogFont;
207 }
208
209 return ret;
210}
211
212void wxGenericFontDialog::CreateWidgets()
213{
214 wxBusyCursor bcur;
215
216 wxString
217 *families = new wxString[6],
218 *styles = new wxString[3],
219 *weights = new wxString[3];
220 families[0] = _("Roman");
221 families[1] = _("Decorative");
222 families[2] = _("Modern");
223 families[3] = _("Script");
224 families[4] = _("Swiss" );
225 families[5] = _("Teletype" );
226 styles[0] = _("Normal");
227 styles[1] = _("Italic");
228 styles[2] = _("Slant");
229 weights[0] = _("Normal");
230 weights[1] = _("Light");
231 weights[2] = _("Bold");
232
233 familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxDefaultPosition, wxDefaultSize, 5, families);
234 styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxDefaultPosition, wxDefaultSize, 3, styles);
235 weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxDefaultPosition, wxDefaultSize, 3, weights);
236
237 colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxDefaultPosition, wxDefaultSize, NUM_COLS, wxColourDialogNames);
238
239 wxString *pointSizes = new wxString[40];
240 int i;
241 for ( i = 0; i < 40; i++)
242 {
243 char buf[5];
244 sprintf(buf, "%d", i + 1);
245 pointSizes[i] = buf;
246 }
247
248 pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes);
249 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"));
250
251 m_previewer = new wxFontPreviewer(this);
252
253 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
254 wxButton *cancelButton = new wxButton(this, wxID_OK, _("Cancel"));
255
256 familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
257 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
258 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
259 wxString name(wxTheColourDatabase->FindName(m_fontData.fontColour));
260 colourChoice->SetStringSelection(name);
261
262 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
263 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
264
265 okButton->SetDefault();
266
267 wxSizer *topsizer, *sizer;
268 topsizer = new wxBoxSizer(wxVERTICAL);
269
270 sizer = new wxBoxSizer(wxHORIZONTAL);
271 sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
272 sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
273 sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
274 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
275
276 sizer = new wxBoxSizer(wxHORIZONTAL);
277 sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
278 sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10);
279 sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10);
280 topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10);
281
282 topsizer->Add(m_previewer, 1, wxALL | wxEXPAND, 10);
283 topsizer->SetItemMinSize(m_previewer, 430, 100);
284
285 sizer = new wxBoxSizer(wxHORIZONTAL);
286 sizer->Add(okButton, 0, wxRIGHT, 10);
287 sizer->Add(cancelButton, 0, wxRIGHT, 10);
288 topsizer->Add(sizer, 0, wxALIGN_RIGHT | wxBOTTOM, 10);
289
290 SetAutoLayout(TRUE);
291 SetSizer(topsizer);
292 topsizer->SetSizeHints(this);
293 topsizer->Fit(this);
294
295 Centre(wxBOTH);
296
297 delete[] families;
298 delete[] styles;
299 delete[] weights;
300 delete[] pointSizes;
301 m_useEvents = TRUE;
302}
303
304void wxGenericFontDialog::InitializeFont()
305{
306 int fontFamily = wxSWISS;
307 int fontWeight = wxNORMAL;
308 int fontStyle = wxNORMAL;
309 int fontSize = 12;
310 int fontUnderline = FALSE;
311
312 if (m_fontData.initialFont.Ok())
313 {
314 fontFamily = m_fontData.initialFont.GetFamily();
315 fontWeight = m_fontData.initialFont.GetWeight();
316 fontStyle = m_fontData.initialFont.GetStyle();
317 fontSize = m_fontData.initialFont.GetPointSize();
318 fontUnderline = m_fontData.initialFont.GetUnderlined();
319 }
320
321 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
322
323 if (m_previewer)
324 m_previewer->SetFont(dialogFont);
325}
326
327void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
328{
329 if (!m_useEvents) return;
330
331 int fontFamily = 0; /* shut up buggy egcs warnings */
332 fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
333 int fontWeight = 0;
334 fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
335 int fontStyle = 0;
336 fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
337 int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
338 int fontUnderline = underLineCheckBox->GetValue();
339
340 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
341 m_previewer->SetFont(dialogFont);
342 if (colourChoice->GetStringSelection() != wxT(""))
343 {
344 wxColour *col = (wxColour*) NULL;
345 col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection());
346 if (col)
347 {
348 m_fontData.fontColour = *col;
349 m_previewer->SetForegroundColour(*col);
350 }
351 }
352 m_previewer->Refresh();
353}
354
355const wxChar *wxFontWeightIntToString(int weight)
356{
357 switch (weight)
358 {
359 case wxLIGHT:
360 return wxT("Light");
361 case wxBOLD:
362 return wxT("Bold");
363 case wxNORMAL:
364 default:
365 return wxT("Normal");
366 }
367}
368
369const wxChar *wxFontStyleIntToString(int style)
370{
371 switch (style)
372 {
373 case wxITALIC:
374 return wxT("Italic");
375 case wxSLANT:
376 return wxT("Slant");
377 case wxNORMAL:
378 default:
379 return wxT("Normal");
380 }
381}
382
383const wxChar *wxFontFamilyIntToString(int family)
384{
385 switch (family)
386 {
387 case wxROMAN:
388 return wxT("Roman");
389 case wxDECORATIVE:
390 return wxT("Decorative");
391 case wxMODERN:
392 return wxT("Modern");
393 case wxSCRIPT:
394 return wxT("Script");
395 case wxTELETYPE:
396 return wxT("Teletype");
397 case wxSWISS:
398 default:
399 return wxT("Swiss");
400 }
401}
402
403int wxFontFamilyStringToInt(wxChar *family)
404{
405 if (!family)
406 return wxSWISS;
407
408 if (wxStrcmp(family, wxT("Roman")) == 0)
409 return wxROMAN;
410 else if (wxStrcmp(family, wxT("Decorative")) == 0)
411 return wxDECORATIVE;
412 else if (wxStrcmp(family, wxT("Modern")) == 0)
413 return wxMODERN;
414 else if (wxStrcmp(family, wxT("Script")) == 0)
415 return wxSCRIPT;
416 else if (wxStrcmp(family, wxT("Teletype")) == 0)
417 return wxTELETYPE;
418 else return wxSWISS;
419}
420
421int wxFontStyleStringToInt(wxChar *style)
422{
423 if (!style)
424 return wxNORMAL;
425 if (wxStrcmp(style, wxT("Italic")) == 0)
426 return wxITALIC;
427 else if (wxStrcmp(style, wxT("Slant")) == 0)
428 return wxSLANT;
429 else
430 return wxNORMAL;
431}
432
433int wxFontWeightStringToInt(wxChar *weight)
434{
435 if (!weight)
436 return wxNORMAL;
437 if (wxStrcmp(weight, wxT("Bold")) == 0)
438 return wxBOLD;
439 else if (wxStrcmp(weight, wxT("Light")) == 0)
440 return wxLIGHT;
441 else
442 return wxNORMAL;
443}
444
445#endif
446 // wxUSE_FONTDLG
447