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