]>
Commit | Line | Data |
---|---|---|
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 | ||
50 | class WXDLLEXPORT wxFontPreviewer : public wxWindow | |
51 | { | |
52 | public: | |
53 | wxFontPreviewer(wxWindow *parent) : wxWindow(parent, -1) {} | |
54 | ||
55 | private: | |
56 | void OnPaint(wxPaintEvent& event); | |
57 | DECLARE_EVENT_TABLE() | |
58 | }; | |
59 | ||
60 | BEGIN_EVENT_TABLE(wxFontPreviewer, wxWindow) | |
61 | EVT_PAINT(wxFontPreviewer::OnPaint) | |
62 | END_EVENT_TABLE() | |
63 | ||
64 | void 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 |
93 | IMPLEMENT_DYNAMIC_CLASS(wxGenericFontDialog, wxDialog) |
94 | ||
95 | BEGIN_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 |
103 | END_EVENT_TABLE() |
104 | ||
c801d85f KB |
105 | |
106 | #define NUM_COLS 48 | |
223d09f6 KB |
107 | static 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 | 166 | void wxGenericFontDialog::Init() |
c801d85f | 167 | { |
66bd6b93 | 168 | m_useEvents = FALSE; |
53cf79fa | 169 | m_previewer = NULL; |
1bab9242 | 170 | Create( m_parent ) ; |
c801d85f KB |
171 | } |
172 | ||
c2c59b22 | 173 | wxGenericFontDialog::~wxGenericFontDialog() |
c801d85f KB |
174 | { |
175 | } | |
176 | ||
74e3313b | 177 | void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
c801d85f | 178 | { |
e3065973 | 179 | EndModal(wxID_CANCEL); |
c801d85f | 180 | } |
c35414db | 181 | |
c2c59b22 | 182 | bool 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 | |
c801d85f KB |
194 | return TRUE; |
195 | } | |
196 | ||
c2c59b22 | 197 | int wxGenericFontDialog::ShowModal() |
c801d85f | 198 | { |
1bab9242 | 199 | int ret = wxDialog::ShowModal(); |
c801d85f KB |
200 | |
201 | if (ret != wxID_CANCEL) | |
202 | { | |
c2c59b22 | 203 | m_fontData.chosenFont = dialogFont; |
c801d85f KB |
204 | } |
205 | ||
c35414db | 206 | return ret; |
c801d85f KB |
207 | } |
208 | ||
c2c59b22 | 209 | void wxGenericFontDialog::CreateWidgets() |
c801d85f | 210 | { |
53cf79fa | 211 | wxBusyCursor bcur; |
c35414db | 212 | |
59043ba2 | 213 | wxString |
36b3b54a | 214 | *families = new wxString[6], |
59043ba2 KB |
215 | *styles = new wxString[3], |
216 | *weights = new wxString[3]; | |
217 | families[0] = _("Roman"); | |
218 | families[1] = _("Decorative"); | |
219 | families[2] = _("Modern"); | |
220 | families[3] = _("Script"); | |
221 | families[4] = _("Swiss" ); | |
36b3b54a | 222 | families[5] = _("Teletype" ); |
59043ba2 KB |
223 | styles[0] = _("Normal"); |
224 | styles[1] = _("Italic"); | |
225 | styles[2] = _("Slant"); | |
226 | weights[0] = _("Normal"); | |
227 | weights[1] = _("Light"); | |
228 | weights[2] = _("Bold"); | |
c35414db | 229 | |
53cf79fa VS |
230 | familyChoice = new wxChoice(this, wxID_FONT_FAMILY, wxDefaultPosition, wxDefaultSize, 5, families); |
231 | styleChoice = new wxChoice(this, wxID_FONT_STYLE, wxDefaultPosition, wxDefaultSize, 3, styles); | |
232 | weightChoice = new wxChoice(this, wxID_FONT_WEIGHT, wxDefaultPosition, wxDefaultSize, 3, weights); | |
233 | ||
234 | colourChoice = new wxChoice(this, wxID_FONT_COLOUR, wxDefaultPosition, wxDefaultSize, NUM_COLS, wxColourDialogNames); | |
c801d85f | 235 | |
59043ba2 | 236 | wxString *pointSizes = new wxString[40]; |
c801d85f KB |
237 | int i; |
238 | for ( i = 0; i < 40; i++) | |
239 | { | |
c35414db VZ |
240 | char buf[5]; |
241 | sprintf(buf, "%d", i + 1); | |
242 | pointSizes[i] = buf; | |
c801d85f KB |
243 | } |
244 | ||
53cf79fa VS |
245 | pointSizeChoice = new wxChoice(this, wxID_FONT_SIZE, wxDefaultPosition, wxDefaultSize, 40, pointSizes); |
246 | underLineCheckBox = new wxCheckBox(this, wxID_FONT_UNDERLINE, _("Underline")); | |
c801d85f | 247 | |
53cf79fa | 248 | m_previewer = new wxFontPreviewer(this); |
c801d85f | 249 | |
53cf79fa VS |
250 | wxButton *okButton = new wxButton(this, wxID_OK, _("OK")); |
251 | wxButton *cancelButton = new wxButton(this, wxID_OK, _("Cancel")); | |
c801d85f | 252 | |
66bd6b93 | 253 | familyChoice->SetStringSelection( wxFontFamilyIntToString(dialogFont.GetFamily()) ); |
c801d85f KB |
254 | styleChoice->SetStringSelection(wxFontStyleIntToString(dialogFont.GetStyle())); |
255 | weightChoice->SetStringSelection(wxFontWeightIntToString(dialogFont.GetWeight())); | |
c2c59b22 | 256 | wxString name(wxTheColourDatabase->FindName(m_fontData.fontColour)); |
c801d85f | 257 | colourChoice->SetStringSelection(name); |
c35414db | 258 | |
c801d85f | 259 | underLineCheckBox->SetValue(dialogFont.GetUnderlined()); |
66bd6b93 | 260 | pointSizeChoice->SetSelection(dialogFont.GetPointSize()-1); |
c801d85f KB |
261 | |
262 | okButton->SetDefault(); | |
263 | ||
53cf79fa VS |
264 | wxSizer *topsizer, *sizer; |
265 | topsizer = new wxBoxSizer(wxVERTICAL); | |
266 | ||
267 | sizer = new wxBoxSizer(wxHORIZONTAL); | |
268 | sizer->Add(familyChoice, 0, wxALIGN_CENTER | wxLEFT, 10); | |
269 | sizer->Add(styleChoice, 0, wxALIGN_CENTER | wxLEFT, 10); | |
270 | sizer->Add(weightChoice, 0, wxALIGN_CENTER | wxLEFT, 10); | |
271 | topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10); | |
272 | ||
273 | sizer = new wxBoxSizer(wxHORIZONTAL); | |
274 | sizer->Add(colourChoice, 0, wxALIGN_CENTER | wxLEFT, 10); | |
275 | sizer->Add(pointSizeChoice, 0, wxALIGN_CENTER | wxLEFT, 10); | |
276 | sizer->Add(underLineCheckBox, 0, wxALIGN_CENTER | wxLEFT, 10); | |
277 | topsizer->Add(sizer, 0, wxLEFT| wxTOP| wxRIGHT, 10); | |
278 | ||
279 | topsizer->Add(m_previewer, 1, wxALL | wxEXPAND, 10); | |
280 | topsizer->SetItemMinSize(m_previewer, 430, 100); | |
281 | ||
282 | sizer = new wxBoxSizer(wxHORIZONTAL); | |
283 | sizer->Add(okButton, 0, wxRIGHT, 10); | |
284 | sizer->Add(cancelButton, 0, wxRIGHT, 10); | |
285 | topsizer->Add(sizer, 0, wxALIGN_RIGHT | wxBOTTOM, 10); | |
286 | ||
287 | SetAutoLayout(TRUE); | |
288 | SetSizer(topsizer); | |
289 | topsizer->SetSizeHints(this); | |
290 | topsizer->Fit(this); | |
c801d85f KB |
291 | |
292 | Centre(wxBOTH); | |
293 | ||
59043ba2 KB |
294 | delete[] families; |
295 | delete[] styles; | |
296 | delete[] weights; | |
297 | delete[] pointSizes; | |
66bd6b93 | 298 | m_useEvents = TRUE; |
c801d85f KB |
299 | } |
300 | ||
c2c59b22 | 301 | void wxGenericFontDialog::InitializeFont() |
c801d85f KB |
302 | { |
303 | int fontFamily = wxSWISS; | |
304 | int fontWeight = wxNORMAL; | |
305 | int fontStyle = wxNORMAL; | |
306 | int fontSize = 12; | |
307 | int fontUnderline = FALSE; | |
53cf79fa | 308 | |
c2c59b22 | 309 | if (m_fontData.initialFont.Ok()) |
c801d85f | 310 | { |
c2c59b22 VZ |
311 | fontFamily = m_fontData.initialFont.GetFamily(); |
312 | fontWeight = m_fontData.initialFont.GetWeight(); | |
313 | fontStyle = m_fontData.initialFont.GetStyle(); | |
314 | fontSize = m_fontData.initialFont.GetPointSize(); | |
315 | fontUnderline = m_fontData.initialFont.GetUnderlined(); | |
c801d85f | 316 | } |
c801d85f | 317 | |
53cf79fa | 318 | dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0)); |
c801d85f | 319 | |
53cf79fa VS |
320 | if (m_previewer) |
321 | m_previewer->SetFont(dialogFont); | |
c801d85f KB |
322 | } |
323 | ||
324 | void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event)) | |
325 | { | |
66bd6b93 | 326 | if (!m_useEvents) return; |
c35414db | 327 | |
bbe0af5b RR |
328 | int fontFamily = 0; /* shut up buggy egcs warnings */ |
329 | fontFamily = wxFontFamilyStringToInt(WXSTRINGCAST familyChoice->GetStringSelection()); | |
330 | int fontWeight = 0; | |
331 | fontWeight = wxFontWeightStringToInt(WXSTRINGCAST weightChoice->GetStringSelection()); | |
332 | int fontStyle = 0; | |
333 | fontStyle = wxFontStyleStringToInt(WXSTRINGCAST styleChoice->GetStringSelection()); | |
87138c52 | 334 | int fontSize = wxAtoi(pointSizeChoice->GetStringSelection()); |
c801d85f KB |
335 | int fontUnderline = underLineCheckBox->GetValue(); |
336 | ||
337 | dialogFont = wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0)); | |
53cf79fa | 338 | m_previewer->SetFont(dialogFont); |
223d09f6 | 339 | if (colourChoice->GetStringSelection() != wxT("")) |
c801d85f | 340 | { |
bbe0af5b RR |
341 | wxColour *col = (wxColour*) NULL; |
342 | col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection()); | |
c801d85f KB |
343 | if (col) |
344 | { | |
c2c59b22 | 345 | m_fontData.fontColour = *col; |
53cf79fa | 346 | m_previewer->SetForegroundColour(*col); |
c801d85f KB |
347 | } |
348 | } | |
53cf79fa | 349 | m_previewer->Refresh(); |
c801d85f KB |
350 | } |
351 | ||
d0060e77 | 352 | const 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 | 366 | const 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 | 380 | const 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 | 400 | int 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 | 418 | int 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 | 430 | int 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 |