22#include "wx/listbox.h"
24#include "wx/stattext.h"
26#include "wx/dcclient.h"
28#include "wx/checkbox.h"
30#include "wx/settings.h"
37#include "wx/fontdlg.h"
38#include "ocpn_fontdlg.h"
40#if USE_SPINCTRL_FOR_POINT_SIZE
41#include "wx/spinctrl.h"
51 : wxWindow(parent, wxID_ANY, wxDefaultPosition, sz) {}
54 void OnPaint(wxPaintEvent& event);
55 wxDECLARE_EVENT_TABLE();
59 EVT_PAINT(OCPNFontPreviewer::OnPaint) wxEND_EVENT_TABLE()
64 wxSize size = GetSize();
65 wxFont font = GetFont();
67 dc.SetPen(*wxBLACK_PEN);
68 dc.SetBrush(*wxWHITE_BRUSH);
69 dc.DrawRectangle(0, 0, size.x, size.y);
73 dc.SetTextForeground(GetForegroundColour());
74 dc.SetClippingRegion(2, 2, size.x - 4, size.y - 4);
75 dc.DrawText(_(
"ABCDEFGabcdefg12345"), 10,
76 (size.y - dc.GetTextExtent(
"X").y) / 2);
77 dc.DestroyClippingRegion();
85static const wxChar* ocpnFontWeightIntToString(
int weight) {
87 case wxFONTWEIGHT_LIGHT:
88 return wxString(
"Light");
89 case wxFONTWEIGHT_BOLD:
90 return wxString(
"Bold");
91 case wxFONTWEIGHT_NORMAL:
93 return wxString(
"Normal");
97static const wxChar* ocpnFontStyleIntToString(
int style) {
99 case wxFONTSTYLE_ITALIC:
100 return wxString(
"Italic");
101 case wxFONTSTYLE_SLANT:
102 return wxString(
"Slant");
103 case wxFONTSTYLE_NORMAL:
105 return wxString(
"Normal");
109static const wxChar* ocpnFontFamilyIntToString(
int family) {
111 case wxFONTFAMILY_ROMAN:
112 return wxString(
"Roman");
113 case wxFONTFAMILY_DECORATIVE:
114 return wxString(
"Decorative");
115 case wxFONTFAMILY_MODERN:
116 return wxString(
"Modern");
117 case wxFONTFAMILY_SCRIPT:
118 return wxString(
"Script");
119 case wxFONTFAMILY_TELETYPE:
120 return wxString(
"Teletype");
121 case wxFONTFAMILY_SWISS:
123 return wxString(
"Swiss");
127static wxFontFamily ocpnFontFamilyStringToInt(
const wxString& family) {
128 if (family.empty())
return wxFONTFAMILY_SWISS;
130 if (wxStrcmp(family,
"Roman") == 0)
131 return wxFONTFAMILY_ROMAN;
132 else if (wxStrcmp(family,
"Decorative") == 0)
133 return wxFONTFAMILY_DECORATIVE;
134 else if (wxStrcmp(family,
"Modern") == 0)
135 return wxFONTFAMILY_MODERN;
136 else if (wxStrcmp(family,
"Script") == 0)
137 return wxFONTFAMILY_SCRIPT;
138 else if (wxStrcmp(family,
"Teletype") == 0)
139 return wxFONTFAMILY_TELETYPE;
141 return wxFONTFAMILY_SWISS;
144static wxFontStyle ocpnFontStyleStringToInt(
const wxString& style) {
145 if (style.empty())
return wxFONTSTYLE_NORMAL;
146 if (wxStrcmp(style,
"Italic") == 0)
147 return wxFONTSTYLE_ITALIC;
148 else if (wxStrcmp(style,
"Slant") == 0)
149 return wxFONTSTYLE_SLANT;
151 return wxFONTSTYLE_NORMAL;
154static wxFontWeight ocpnFontWeightStringToInt(
const wxString& weight) {
155 if (weight.empty())
return wxFONTWEIGHT_NORMAL;
156 if (wxStrcmp(weight,
"Bold") == 0)
157 return wxFONTWEIGHT_BOLD;
158 else if (wxStrcmp(weight,
"Light") == 0)
159 return wxFONTWEIGHT_LIGHT;
161 return wxFONTWEIGHT_NORMAL;
169 EVT_CHECKBOX(wxID_FONT_UNDERLINE, ocpnGenericFontDialog::OnChangeFont)
170 EVT_CHOICE(wxID_FONT_STYLE, ocpnGenericFontDialog::OnChangeFont)
171 EVT_CHOICE(wxID_FONT_WEIGHT, ocpnGenericFontDialog::OnChangeFont)
172 EVT_CHOICE(wxID_FONT_FAMILY,
173 ocpnGenericFontDialog::OnChangeFont)
174 EVT_CHOICE(wxID_FONT_COLOUR,
175 ocpnGenericFontDialog::OnChangeFont)
176#if USE_SPINCTRL_FOR_POINT_SIZE
177 EVT_SPINCTRL(wxID_FONT_SIZE,
178 ocpnGenericFontDialog::OnChangeSize)
179 EVT_TEXT(wxID_FONT_SIZE,
180 ocpnGenericFontDialog::OnChangeFont)
182 EVT_CHOICE(wxID_FONT_SIZE,
183 ocpnGenericFontDialog::OnChangeFont)
185 EVT_CLOSE(ocpnGenericFontDialog::OnCloseWindow)
190 ocpnColourDialogNames[NUM_COLS] = {
"ORANGE",
201 "MEDIUM SPRING GREEN",
236 "MEDIUM FOREST GREEN",
248void ocpnGenericFontDialog::Init() {
254ocpnGenericFontDialog::~ocpnGenericFontDialog() {}
256void ocpnGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) {
257 EndModal(wxID_CANCEL);
260bool ocpnGenericFontDialog::DoCreate(wxWindow* parent) {
261 parent = GetParentForModalDialog(parent, 0);
263 if (!wxDialog::Create(parent, wxID_ANY,
"Choose Font", wxDefaultPosition,
264 wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
"fontdialog")) {
265 wxFAIL_MSG(
"wxFontDialog creation failed");
278int ocpnGenericFontDialog::ShowModal() {
279 int ret = wxDialog::ShowModal();
281 if (ret != wxID_CANCEL) {
282 m_fontData.m_chosenFont = m_dialogFont;
289static bool ShowToolTips() {
return false; }
291void ocpnGenericFontDialog::CreateWidgets() {
292 wxString *families =
new wxString[6], *styles =
new wxString[3],
293 *weights =
new wxString[3];
294 families[0] = _(
"Roman");
295 families[1] = _(
"Decorative");
296 families[2] = _(
"Modern");
297 families[3] = _(
"Script");
298 families[4] = _(
"Swiss");
299 families[5] = _(
"Teletype");
300 styles[0] = _(
"Normal");
301 styles[1] = _(
"Italic");
302 styles[2] = _(
"Slant");
303 weights[0] = _(
"Normal");
304 weights[1] = _(
"Light");
305 weights[2] = _(
"Bold");
308 wxString* pointSizes =
new wxString[40];
310 for (i = 0; i < 40; i++) {
312 wxSprintf(buf,
"%d", i + 1);
319 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
329 wxBoxSizer* itemBoxSizer2 =
new wxBoxSizer(wxVERTICAL);
330 this->SetSizer(itemBoxSizer2);
332 wxBoxSizer* itemBoxSizer3 =
new wxBoxSizer(wxVERTICAL);
333 itemBoxSizer2->Add(itemBoxSizer3, 1, wxGROW | wxALL, 5);
335 wxFlexGridSizer* itemGridSizer4 =
new wxFlexGridSizer(noRows, noCols, 0, 0);
336 itemBoxSizer3->Add(itemGridSizer4, 0, wxGROW, 5);
338 wxBoxSizer* itemBoxSizer5 =
new wxBoxSizer(wxVERTICAL);
339 itemGridSizer4->Add(itemBoxSizer5, 0, wxALIGN_CENTER_HORIZONTAL | wxGROW, 5);
340 wxStaticText* itemStaticText6 =
341 new wxStaticText(
this, wxID_STATIC, _(
"&Font family:"), wxDefaultPosition,
343 itemBoxSizer5->Add(itemStaticText6, 0,
344 wxALIGN_LEFT | wxLEFT | wxRIGHT | wxTOP, 5);
346 m_familyChoice =
new wxChoice(
this, wxID_FONT_FAMILY, wxDefaultPosition,
347 wxDefaultSize, 5, families, 0);
348 m_familyChoice->SetHelpText(_(
"The font family."));
349 if (ShowToolTips()) m_familyChoice->SetToolTip(_(
"The font family."));
350 itemBoxSizer5->Add(m_familyChoice, 0, wxALIGN_LEFT | wxALL, 5);
352 wxBoxSizer* itemBoxSizer8 =
new wxBoxSizer(wxVERTICAL);
353 itemGridSizer4->Add(itemBoxSizer8, 0, wxALIGN_CENTER_HORIZONTAL | wxGROW, 5);
354 wxStaticText* itemStaticText9 =
new wxStaticText(
355 this, wxID_STATIC, _(
"&Style:"), wxDefaultPosition, wxDefaultSize, 0);
356 itemBoxSizer8->Add(itemStaticText9, 0,
357 wxALIGN_LEFT | wxLEFT | wxRIGHT | wxTOP, 5);
359 m_styleChoice =
new wxChoice(
this, wxID_FONT_STYLE, wxDefaultPosition,
360 wxDefaultSize, 3, styles, 0);
361 m_styleChoice->SetHelpText(_(
"The font style."));
362 if (ShowToolTips()) m_styleChoice->SetToolTip(_(
"The font style."));
363 itemBoxSizer8->Add(m_styleChoice, 0, wxALIGN_LEFT | wxALL, 5);
365 wxBoxSizer* itemBoxSizer11 =
new wxBoxSizer(wxVERTICAL);
366 itemGridSizer4->Add(itemBoxSizer11, 0, wxALIGN_CENTER_HORIZONTAL | wxGROW, 5);
367 wxStaticText* itemStaticText12 =
new wxStaticText(
368 this, wxID_STATIC, _(
"&Weight:"), wxDefaultPosition, wxDefaultSize, 0);
369 itemBoxSizer11->Add(itemStaticText12, 0,
370 wxALIGN_LEFT | wxLEFT | wxRIGHT | wxTOP, 5);
372 m_weightChoice =
new wxChoice(
this, wxID_FONT_WEIGHT, wxDefaultPosition,
373 wxDefaultSize, 3, weights, 0);
374 m_weightChoice->SetHelpText(_(
"The font weight."));
375 if (ShowToolTips()) m_weightChoice->SetToolTip(_(
"The font weight."));
376 itemBoxSizer11->Add(m_weightChoice, 0, wxALIGN_LEFT | wxALL, 5);
378 wxBoxSizer* itemBoxSizer14 =
new wxBoxSizer(wxVERTICAL);
379 itemGridSizer4->Add(itemBoxSizer14, 0, wxALIGN_CENTER_HORIZONTAL | wxGROW, 5);
380 m_colourChoice = NULL;
381 if (m_fontData.GetEnableEffects()) {
382 wxStaticText* itemStaticText15 =
new wxStaticText(
383 this, wxID_STATIC, _(
"C&olour:"), wxDefaultPosition, wxDefaultSize, 0);
384 itemBoxSizer14->Add(itemStaticText15, 0,
385 wxALIGN_LEFT | wxLEFT | wxRIGHT | wxTOP, 5);
387 wxSize colourSize = wxDefaultSize;
388 if (is_pda) colourSize.x = 100;
391 new wxChoice(
this, wxID_FONT_COLOUR, wxDefaultPosition, colourSize,
392 NUM_COLS, ocpnColourDialogNames, 0);
393 m_colourChoice->SetHelpText(_(
"The font colour."));
394 if (ShowToolTips()) m_colourChoice->SetToolTip(_(
"The font colour."));
395 itemBoxSizer14->Add(m_colourChoice, 0, wxALIGN_LEFT | wxALL, 5);
398 wxBoxSizer* itemBoxSizer17 =
new wxBoxSizer(wxVERTICAL);
399 itemGridSizer4->Add(itemBoxSizer17, 0, wxALIGN_CENTER_HORIZONTAL | wxGROW, 5);
400 wxStaticText* itemStaticText18 =
401 new wxStaticText(
this, wxID_STATIC, _(
"&Point size:"), wxDefaultPosition,
403 itemBoxSizer17->Add(itemStaticText18, 0,
404 wxALIGN_LEFT | wxLEFT | wxRIGHT | wxTOP, 5);
406#if USE_SPINCTRL_FOR_POINT_SIZE
408 new wxSpinCtrl(
this, wxID_FONT_SIZE,
"12", wxDefaultPosition,
409 wxSize(80, wxDefaultCoord), wxSP_ARROW_KEYS, 1, 500, 12);
410 m_pointSizeSpin->SetHelpText(_(
"The font point size."));
411 if (ShowToolTips()) m_pointSizeSpin->SetToolTip(_(
"The font point size."));
412 itemBoxSizer17->Add(m_pointSizeSpin, 0, wxALIGN_LEFT | wxALL, 5);
414 m_pointSizeChoice =
new wxChoice(
this, wxID_FONT_SIZE, wxDefaultPosition,
415 wxDefaultSize, 40, pointSizes, 0);
416 m_pointSizeChoice->SetHelpText(_(
"The font point size."));
417 if (ShowToolTips()) m_pointSizeChoice->SetToolTip(_(
"The font point size."));
418 itemBoxSizer17->Add(m_pointSizeChoice, 0, wxALIGN_LEFT | wxALL, 5);
421 m_underLineCheckBox = NULL;
422 if (m_fontData.GetEnableEffects()) {
423 wxBoxSizer* itemBoxSizer20 =
new wxBoxSizer(wxVERTICAL);
424 itemGridSizer4->Add(itemBoxSizer20, 0,
425 wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL, 5);
426 m_underLineCheckBox =
427 new wxCheckBox(
this, wxID_FONT_UNDERLINE, _(
"&Underline"),
428 wxDefaultPosition, wxDefaultSize, 0);
429 m_underLineCheckBox->SetValue(
false);
430 m_underLineCheckBox->SetHelpText(_(
"Whether the font is underlined."));
432 m_underLineCheckBox->SetToolTip(_(
"Whether the font is underlined."));
433 itemBoxSizer20->Add(m_underLineCheckBox, 0, wxALIGN_LEFT | wxALL, 5);
437 itemBoxSizer3->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
439 wxStaticText* itemStaticText23 =
new wxStaticText(
440 this, wxID_STATIC, _(
"Preview:"), wxDefaultPosition, wxDefaultSize, 0);
441 itemBoxSizer3->Add(itemStaticText23, 0,
442 wxALIGN_LEFT | wxLEFT | wxRIGHT | wxTOP, 5);
445 m_previewer->SetHelpText(_(
"Shows the font preview."));
446 if (ShowToolTips()) m_previewer->SetToolTip(_(
"Shows the font preview."));
447 itemBoxSizer3->Add(m_previewer, 1, wxGROW | wxALL, 5);
449 wxBoxSizer* itemBoxSizer25 =
new wxBoxSizer(wxHORIZONTAL);
450 itemBoxSizer3->Add(itemBoxSizer25, 0, wxGROW, 5);
451 itemBoxSizer25->Add(5, 5, 1, wxGROW | wxALL, 5);
454 wxButton* itemButton28 =
new wxButton(
this, wxID_CANCEL, _(
"&Cancel"),
455 wxDefaultPosition, wxDefaultSize, 0);
457 itemButton28->SetToolTip(_(
"Click to cancel the font selection."));
458 itemBoxSizer25->Add(itemButton28, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
460 wxButton* itemButton27 =
new wxButton(
this, wxID_OK, _(
"&OK"),
461 wxDefaultPosition, wxDefaultSize, 0);
462 itemButton27->SetDefault();
463 itemButton27->SetHelpText(_(
"Click to confirm the font selection."));
465 itemButton27->SetToolTip(_(
"Click to confirm the font selection."));
466 itemBoxSizer25->Add(itemButton27, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
468 wxButton* itemButton27 =
new wxButton(
this, wxID_OK, _(
"&OK"),
469 wxDefaultPosition, wxDefaultSize, 0);
470 itemButton27->SetDefault();
471 itemButton27->SetHelpText(_(
"Click to confirm the font selection."));
473 itemButton27->SetToolTip(_(
"Click to confirm the font selection."));
474 itemBoxSizer25->Add(itemButton27, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
476 wxButton* itemButton28 =
new wxButton(
this, wxID_CANCEL, _(
"&Cancel"),
477 wxDefaultPosition, wxDefaultSize, 0);
479 itemButton28->SetToolTip(_(
"Click to cancel the font selection."));
480 itemBoxSizer25->Add(itemButton28, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5);
483 m_familyChoice->SetStringSelection(
484 ocpnFontFamilyIntToString(m_dialogFont.GetFamily()));
485 m_styleChoice->SetStringSelection(
486 ocpnFontStyleIntToString(m_dialogFont.GetStyle()));
487 m_weightChoice->SetStringSelection(
488 ocpnFontWeightIntToString(m_dialogFont.GetWeight()));
490 if (m_colourChoice) {
491 wxString name(wxTheColourDatabase->FindName(m_fontData.GetColour()));
493 m_colourChoice->SetStringSelection(
"BLACK");
495 m_colourChoice->SetStringSelection(name);
498 if (m_underLineCheckBox) {
499 m_underLineCheckBox->SetValue(m_dialogFont.GetUnderlined());
502#if USE_SPINCTRL_FOR_POINT_SIZE
503 m_pointSizeSpin->SetValue(m_dialogFont.GetPointSize());
505 m_pointSizeChoice->SetSelection(m_dialogFont.GetPointSize() - 1);
508 GetSizer()->SetItemMinSize(m_previewer, is_pda ? 100 : 430,
510 GetSizer()->SetSizeHints(
this);
517#if !USE_SPINCTRL_FOR_POINT_SIZE
525void ocpnGenericFontDialog::InitializeFont() {
526 wxFontFamily fontFamily = wxFONTFAMILY_SWISS;
527 wxFontWeight fontWeight = wxFONTWEIGHT_NORMAL;
528 wxFontStyle fontStyle = wxFONTSTYLE_NORMAL;
530 bool fontUnderline =
false;
532 if (m_fontData.m_initialFont.IsOk()) {
533 fontFamily = m_fontData.m_initialFont.GetFamily();
534 fontWeight = m_fontData.m_initialFont.GetWeight();
535 fontStyle = m_fontData.m_initialFont.GetStyle();
536 fontSize = m_fontData.m_initialFont.GetPointSize();
537 fontUnderline = m_fontData.m_initialFont.GetUnderlined();
541 wxFont(fontSize, fontFamily, fontStyle, fontWeight, fontUnderline);
543 if (m_previewer) m_previewer->SetFont(m_dialogFont);
546void ocpnGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event)) {
550void ocpnGenericFontDialog::DoChangeFont() {
551 if (!m_useEvents)
return;
553 wxFontFamily fontFamily =
554 ocpnFontFamilyStringToInt(m_familyChoice->GetStringSelection());
555 wxFontWeight fontWeight =
556 ocpnFontWeightStringToInt(m_weightChoice->GetStringSelection());
557 wxFontStyle fontStyle =
558 ocpnFontStyleStringToInt(m_styleChoice->GetStringSelection());
559#if USE_SPINCTRL_FOR_POINT_SIZE
560 int fontSize = m_pointSizeSpin->GetValue();
562 int fontSize = wxAtoi(m_pointSizeChoice->GetStringSelection());
568 int fontUnderline = m_dialogFont.GetUnderlined();
570 if (m_underLineCheckBox) {
571 fontUnderline = m_underLineCheckBox->GetValue();
575 wxFont(fontSize, fontFamily, fontStyle, fontWeight, (fontUnderline != 0));
576 m_previewer->SetFont(m_dialogFont);
578 if (m_colourChoice) {
579 if (!m_colourChoice->GetStringSelection().empty()) {
581 wxTheColourDatabase->Find(m_colourChoice->GetStringSelection());
583 m_fontData.m_fontColour = col;
589 if (m_fontData.m_fontColour.IsOk())
590 m_previewer->SetForegroundColour(m_fontData.m_fontColour);
592 m_previewer->Refresh();
595#if USE_SPINCTRL_FOR_POINT_SIZE
596void ocpnGenericFontDialog::OnChangeSize(wxSpinEvent& WXUNUSED(event)) {