]> git.saurik.com Git - wxWidgets.git/blame - src/generic/fontdlgg.cpp
added wxTextCtrl::HitTest(); implemented it for MSW
[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
JS
8// Copyright: (c) Julian Smart
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
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 {
2b5f62a0 77 dc.SetFont(font);
53cf79fa
VS
78 // Calculate vertical centre
79 long w, h;
2b5f62a0 80 dc.GetTextExtent( wxT("X"), &w, &h);
53cf79fa
VS
81 dc.SetTextForeground(GetForegroundColour());
82 dc.SetClippingRegion(2, 2, size.x-4, size.y-4);
83 dc.DrawText(_("ABCDEFGabcdefg12345"),
2b5f62a0 84 10, size.y/2 - h/2);
53cf79fa
VS
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;
1bab9242 170 Create( m_parent ) ;
c801d85f
KB
171}
172
c2c59b22 173wxGenericFontDialog::~wxGenericFontDialog()
c801d85f
KB
174{
175}
176
74e3313b 177void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 178{
e3065973 179 EndModal(wxID_CANCEL);
c801d85f 180}
c35414db 181
c2c59b22 182bool wxGenericFontDialog::DoCreate(wxWindow *parent)
c801d85f 183{
1bab9242
SC
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 }
fc8eba27 190
c801d85f
KB
191 InitializeFont();
192 CreateWidgets();
53cf79fa 193
2b5f62a0
VZ
194 // sets initial font in preview area
195 wxCommandEvent dummy;
196 OnChangeFont(dummy);
197
c801d85f
KB
198 return TRUE;
199}
200
c2c59b22 201int wxGenericFontDialog::ShowModal()
c801d85f 202{
1bab9242 203 int ret = wxDialog::ShowModal();
c801d85f
KB
204
205 if (ret != wxID_CANCEL)
206 {
ae500232 207 m_fontData.m_chosenFont = dialogFont;
c801d85f
KB
208 }
209
c35414db 210 return ret;
c801d85f
KB
211}
212
c2c59b22 213void wxGenericFontDialog::CreateWidgets()
c801d85f 214{
53cf79fa 215 wxBusyCursor bcur;
c35414db 216
59043ba2 217 wxString
36b3b54a 218 *families = new wxString[6],
59043ba2
KB
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" );
36b3b54a 226 families[5] = _("Teletype" );
59043ba2
KB
227 styles[0] = _("Normal");
228 styles[1] = _("Italic");
229 styles[2] = _("Slant");
230 weights[0] = _("Normal");
231 weights[1] = _("Light");
232 weights[2] = _("Bold");
c35414db 233
53cf79fa
VS
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);
c801d85f 239
59043ba2 240 wxString *pointSizes = new wxString[40];
c801d85f
KB
241 int i;
242 for ( i = 0; i < 40; i++)
243 {
2b5f62a0
VZ
244 wxChar buf[5];
245 wxSprintf(buf, wxT("%d"), i + 1);
c35414db 246 pointSizes[i] = buf;
c801d85f
KB
247 }
248
53cf79fa
VS
249 pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes);
250 underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline"));
c801d85f 251
53cf79fa 252 m_previewer = new wxFontPreviewer(this);
c801d85f 253
53cf79fa 254 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"));
2b5f62a0 255 wxButton *cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel"));
c801d85f 256
66bd6b93 257 familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) );
c801d85f
KB
258 styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle()));
259 weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight()));
ae500232 260 wxString name(wxTheColourDatabase->FindName(m_fontData.GetColour()));
c801d85f 261 colourChoice->SetStringSelection(name);
c35414db 262
c801d85f 263 underLineCheckBox->SetValue(dialogFont.GetUnderlined());
66bd6b93 264 pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1);
c801d85f
KB
265
266 okButton->SetDefault();
267
53cf79fa
VS
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);
c801d85f
KB
295
296 Centre(wxBOTH);
297
59043ba2
KB
298 delete[] families;
299 delete[] styles;
300 delete[] weights;
301 delete[] pointSizes;
66bd6b93 302 m_useEvents = TRUE;
c801d85f
KB
303}
304
c2c59b22 305void wxGenericFontDialog::InitializeFont()
c801d85f
KB
306{
307 int fontFamily = wxSWISS;
308 int fontWeight = wxNORMAL;
309 int fontStyle = wxNORMAL;
310 int fontSize = 12;
311 int fontUnderline = FALSE;
53cf79fa 312
ae500232 313 if (m_fontData.m_initialFont.Ok())
c801d85f 314 {
ae500232
JS
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();
c801d85f 320 }
c801d85f 321
53cf79fa 322 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
c801d85f 323
53cf79fa
VS
324 if (m_previewer)
325 m_previewer->SetFont(dialogFont);
c801d85f
KB
326}
327
328void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event))
329{
66bd6b93 330 if (!m_useEvents) return;
c35414db 331
999836aa
VZ
332 int fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection());
333 int fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection());
334 int fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection());
87138c52 335 int fontSize = wxAtoi(pointSizeChoice->GetStringSelection());
c801d85f
KB
336 int fontUnderline = underLineCheckBox->GetValue();
337
338 dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
53cf79fa 339 m_previewer->SetFont(dialogFont);
223d09f6 340 if (colourChoice->GetStringSelection() != wxT(""))
c801d85f 341 {
564a150b
VZ
342 wxColour col = wxTheColourDatabase->Find(colourChoice->GetStringSelection());
343 if (col.Ok())
c801d85f 344 {
564a150b
VZ
345 m_fontData.m_fontColour = col;
346 m_previewer->SetForegroundColour(col);
c801d85f
KB
347 }
348 }
53cf79fa 349 m_previewer->Refresh();
c801d85f
KB
350}
351
d0060e77 352const wxChar *wxFontWeightIntToString(int weight)
c801d85f
KB
353{
354 switch (weight)
355 {
356 case wxLIGHT:
223d09f6 357 return wxT("Light");
c801d85f 358 case wxBOLD:
223d09f6 359 return wxT("Bold");
c801d85f
KB
360 case wxNORMAL:
361 default:
223d09f6 362 return wxT("Normal");
c801d85f 363 }
c801d85f
KB
364}
365
d0060e77 366const wxChar *wxFontStyleIntToString(int style)
c801d85f
KB
367{
368 switch (style)
369 {
370 case wxITALIC:
223d09f6 371 return wxT("Italic");
c801d85f 372 case wxSLANT:
223d09f6 373 return wxT("Slant");
c801d85f
KB
374 case wxNORMAL:
375 default:
223d09f6 376 return wxT("Normal");
c801d85f 377 }
c801d85f
KB
378}
379
d0060e77 380const wxChar *wxFontFamilyIntToString(int family)
c801d85f
KB
381{
382 switch (family)
383 {
384 case wxROMAN:
223d09f6 385 return wxT("Roman");
c801d85f 386 case wxDECORATIVE:
223d09f6 387 return wxT("Decorative");
c801d85f 388 case wxMODERN:
223d09f6 389 return wxT("Modern");
c801d85f 390 case wxSCRIPT:
223d09f6 391 return wxT("Script");
36b3b54a 392 case wxTELETYPE:
223d09f6 393 return wxT("Teletype");
c801d85f
KB
394 case wxSWISS:
395 default:
223d09f6 396 return wxT("Swiss");
c801d85f 397 }
c801d85f
KB
398}
399
87138c52 400int wxFontFamilyStringToInt(wxChar *family)
c801d85f
KB
401{
402 if (!family)
403 return wxSWISS;
c35414db 404
223d09f6 405 if (wxStrcmp(family, wxT("Roman")) == 0)
c801d85f 406 return wxROMAN;
223d09f6 407 else if (wxStrcmp(family, wxT("Decorative")) == 0)
c801d85f 408 return wxDECORATIVE;
223d09f6 409 else if (wxStrcmp(family, wxT("Modern")) == 0)
c801d85f 410 return wxMODERN;
223d09f6 411 else if (wxStrcmp(family, wxT("Script")) == 0)
c801d85f 412 return wxSCRIPT;
223d09f6 413 else if (wxStrcmp(family, wxT("Teletype")) == 0)
36b3b54a 414 return wxTELETYPE;
c801d85f
KB
415 else return wxSWISS;
416}
417
87138c52 418int wxFontStyleStringToInt(wxChar *style)
c801d85f
KB
419{
420 if (!style)
421 return wxNORMAL;
223d09f6 422 if (wxStrcmp(style, wxT("Italic")) == 0)
c801d85f 423 return wxITALIC;
223d09f6 424 else if (wxStrcmp(style, wxT("Slant")) == 0)
c801d85f
KB
425 return wxSLANT;
426 else
427 return wxNORMAL;
428}
429
87138c52 430int wxFontWeightStringToInt(wxChar *weight)
c801d85f
KB
431{
432 if (!weight)
433 return wxNORMAL;
223d09f6 434 if (wxStrcmp(weight, wxT("Bold")) == 0)
c801d85f 435 return wxBOLD;
223d09f6 436 else if (wxStrcmp(weight, wxT("Light")) == 0)
c801d85f
KB
437 return wxLIGHT;
438 else
439 return wxNORMAL;
440}
441
3808e191
JS
442#endif
443 // wxUSE_FONTDLG
c801d85f 444