]> git.saurik.com Git - wxWidgets.git/blame - src/generic/fontdlgg.cpp
SetSelection() for generic wxSpinCtrl (forgot to commit before)
[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$
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 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"
c801d85f 45
53cf79fa
VS
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
c801d85f
KB
93IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog, wxDialog)
94
95BEGIN_EVENT_TABLE(wxGenericFontDialog, wxDialog)
c35414db
VZ
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)
c35414db 102 EVT_CLOSE(wxGenericFontDialog::OnCloseWindow)
c801d85f
KB
103END_EVENT_TABLE()
104
c801d85f
KB
105
106#define NUM_COLS 48
223d09f6
KB
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")
c35414db 160 };
c801d85f
KB
161
162/*
163 * Generic wxFontDialog
164 */
165
c2c59b22 166void wxGenericFontDialog::Init()
c801d85f 167{
66bd6b93 168 m_useEvents = FALSE;
53cf79fa 169 m_previewer = NULL;
06e8a59d 170#ifdef __WXMAC__
1bab9242 171 Create( m_parent ) ;
06e8a59d 172#endif
c801d85f
KB
173}
174
c2c59b22 175wxGenericFontDialog::~wxGenericFontDialog()
c801d85f
KB
176{
177}
178
74e3313b 179void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 180{
e3065973 181 EndModal(wxID_CANCEL);
c801d85f 182}
c35414db 183
c2c59b22 184bool wxGenericFontDialog::DoCreate(wxWindow *parent)
c801d85f 185{
1bab9242
SC
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
c801d85f
KB
194 InitializeFont();
195 CreateWidgets();
53cf79fa 196
c801d85f
KB
197 return TRUE;
198}
199
c2c59b22 200int wxGenericFontDialog::ShowModal()
c801d85f 201{
1bab9242 202 int ret = wxDialog::ShowModal();
c801d85f
KB
203
204 if (ret != wxID_CANCEL)
205 {
c2c59b22 206 m_fontData.chosenFont = dialogFont;
c801d85f
KB
207 }
208
c35414db 209 return ret;
c801d85f
KB
210}
211
c2c59b22 212void wxGenericFontDialog::CreateWidgets()
c801d85f 213{
53cf79fa 214 wxBusyCursor bcur;
c35414db 215
59043ba2 216 wxString
36b3b54a 217 *families = new wxString[6],
59043ba2
KB
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" );
36b3b54a 225 families[5] = _("Teletype" );
59043ba2
KB
226 styles[0] = _("Normal");
227 styles[1] = _("Italic");
228 styles[2] = _("Slant");
229 weights[0] = _("Normal");
230 weights[1] = _("Light");
231 weights[2] = _("Bold");
c35414db 232
53cf79fa
VS
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);
c801d85f 238
59043ba2 239 wxString *pointSizes = new wxString[40];
c801d85f
KB
240 int i;
241 for ( i = 0; i < 40; i++)
242 {
c35414db
VZ
243 char buf[5];
244 sprintf(buf, "%d", i + 1);
245 pointSizes[i] = buf;
c801d85f
KB
246 }
247
53cf79fa
VS
248 pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes);
249 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"));
c801d85f 250
53cf79fa 251 m_previewer = new wxFontPreviewer(this);
c801d85f 252
53cf79fa
VS
253 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
254 wxButton *cancelButton = new wxButton(this, wxID_OK, _("Cancel"));
c801d85f 255
66bd6b93 256 familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
c801d85f
KB
257 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
258 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
c2c59b22 259 wxString name(wxTheColourDatabase->FindName(m_fontData.fontColour));
c801d85f 260 colourChoice->SetStringSelection(name);
c35414db 261
c801d85f 262 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
66bd6b93 263 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
c801d85f
KB
264
265 okButton->SetDefault();
266
53cf79fa
VS
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);
c801d85f
KB
294
295 Centre(wxBOTH);
296
59043ba2
KB
297 delete[] families;
298 delete[] styles;
299 delete[] weights;
300 delete[] pointSizes;
66bd6b93 301 m_useEvents = TRUE;
c801d85f
KB
302}
303
c2c59b22 304void wxGenericFontDialog::InitializeFont()
c801d85f
KB
305{
306 int fontFamily = wxSWISS;
307 int fontWeight = wxNORMAL;
308 int fontStyle = wxNORMAL;
309 int fontSize = 12;
310 int fontUnderline = FALSE;
53cf79fa 311
c2c59b22 312 if (m_fontData.initialFont.Ok())
c801d85f 313 {
c2c59b22
VZ
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();
c801d85f 319 }
c801d85f 320
53cf79fa 321 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
c801d85f 322
53cf79fa
VS
323 if (m_previewer)
324 m_previewer->SetFont(dialogFont);
c801d85f
KB
325}
326
327void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
328{
66bd6b93 329 if (!m_useEvents) return;
c35414db 330
bbe0af5b
RR
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());
87138c52 337 int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
c801d85f
KB
338 int fontUnderline = underLineCheckBox->GetValue();
339
340 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
53cf79fa 341 m_previewer->SetFont(dialogFont);
223d09f6 342 if (colourChoice->GetStringSelection() != wxT(""))
c801d85f 343 {
bbe0af5b
RR
344 wxColour *col = (wxColour*) NULL;
345 col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection());
c801d85f
KB
346 if (col)
347 {
c2c59b22 348 m_fontData.fontColour = *col;
53cf79fa 349 m_previewer->SetForegroundColour(*col);
c801d85f
KB
350 }
351 }
53cf79fa 352 m_previewer->Refresh();
c801d85f
KB
353}
354
d0060e77 355const wxChar *wxFontWeightIntToString(int weight)
c801d85f
KB
356{
357 switch (weight)
358 {
359 case wxLIGHT:
223d09f6 360 return wxT("Light");
c801d85f 361 case wxBOLD:
223d09f6 362 return wxT("Bold");
c801d85f
KB
363 case wxNORMAL:
364 default:
223d09f6 365 return wxT("Normal");
c801d85f 366 }
c801d85f
KB
367}
368
d0060e77 369const wxChar *wxFontStyleIntToString(int style)
c801d85f
KB
370{
371 switch (style)
372 {
373 case wxITALIC:
223d09f6 374 return wxT("Italic");
c801d85f 375 case wxSLANT:
223d09f6 376 return wxT("Slant");
c801d85f
KB
377 case wxNORMAL:
378 default:
223d09f6 379 return wxT("Normal");
c801d85f 380 }
c801d85f
KB
381}
382
d0060e77 383const wxChar *wxFontFamilyIntToString(int family)
c801d85f
KB
384{
385 switch (family)
386 {
387 case wxROMAN:
223d09f6 388 return wxT("Roman");
c801d85f 389 case wxDECORATIVE:
223d09f6 390 return wxT("Decorative");
c801d85f 391 case wxMODERN:
223d09f6 392 return wxT("Modern");
c801d85f 393 case wxSCRIPT:
223d09f6 394 return wxT("Script");
36b3b54a 395 case wxTELETYPE:
223d09f6 396 return wxT("Teletype");
c801d85f
KB
397 case wxSWISS:
398 default:
223d09f6 399 return wxT("Swiss");
c801d85f 400 }
c801d85f
KB
401}
402
87138c52 403int wxFontFamilyStringToInt(wxChar *family)
c801d85f
KB
404{
405 if (!family)
406 return wxSWISS;
c35414db 407
223d09f6 408 if (wxStrcmp(family, wxT("Roman")) == 0)
c801d85f 409 return wxROMAN;
223d09f6 410 else if (wxStrcmp(family, wxT("Decorative")) == 0)
c801d85f 411 return wxDECORATIVE;
223d09f6 412 else if (wxStrcmp(family, wxT("Modern")) == 0)
c801d85f 413 return wxMODERN;
223d09f6 414 else if (wxStrcmp(family, wxT("Script")) == 0)
c801d85f 415 return wxSCRIPT;
223d09f6 416 else if (wxStrcmp(family, wxT("Teletype")) == 0)
36b3b54a 417 return wxTELETYPE;
c801d85f
KB
418 else return wxSWISS;
419}
420
87138c52 421int wxFontStyleStringToInt(wxChar *style)
c801d85f
KB
422{
423 if (!style)
424 return wxNORMAL;
223d09f6 425 if (wxStrcmp(style, wxT("Italic")) == 0)
c801d85f 426 return wxITALIC;
223d09f6 427 else if (wxStrcmp(style, wxT("Slant")) == 0)
c801d85f
KB
428 return wxSLANT;
429 else
430 return wxNORMAL;
431}
432
87138c52 433int wxFontWeightStringToInt(wxChar *weight)
c801d85f
KB
434{
435 if (!weight)
436 return wxNORMAL;
223d09f6 437 if (wxStrcmp(weight, wxT("Bold")) == 0)
c801d85f 438 return wxBOLD;
223d09f6 439 else if (wxStrcmp(weight, wxT("Light")) == 0)
c801d85f
KB
440 return wxLIGHT;
441 else
442 return wxNORMAL;
443}
444
3808e191
JS
445#endif
446 // wxUSE_FONTDLG
c801d85f 447