Android release v6.7_preview1
[xcsoar.git] / src / Dialogs / Settings / FontEdit.cpp
blob075618ab72b0e17487138f4da7b22f69fa4ea8ac
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "FontEdit.hpp"
25 #include "Dialogs/WidgetDialog.hpp"
26 #include "Look/DialogLook.hpp"
27 #include "Widget/RowFormWidget.hpp"
28 #include "Form/ActionListener.hpp"
29 #include "Screen/Layout.hpp"
30 #include "Screen/Font.hpp"
31 #include "Screen/LargeTextWindow.hpp"
32 #include "Form/DataField/Enum.hpp"
33 #include "Form/DataField/Listener.hpp"
34 #include "Util/StringUtil.hpp"
35 #include "UIGlobals.hpp"
36 #include "Language/Language.hpp"
37 #include "Compiler.h"
39 #ifdef ENABLE_OPENGL
40 #include "Screen/OpenGL/Cache.hpp"
41 #endif
43 enum ControlIndex {
44 #ifdef USE_GDI
45 FACE,
46 #endif
47 HEIGHT,
48 WEIGHT,
49 ITALIC,
50 PREVIEW,
53 class FontEditWidget final
54 : public RowFormWidget, public ActionListener, DataFieldListener
56 LOGFONT data;
57 const LOGFONT &default_data;
59 Font font;
61 public:
62 FontEditWidget(const LOGFONT &_data, const LOGFONT &_default_data)
63 :RowFormWidget(UIGlobals::GetDialogLook()),
64 data(_data), default_data(_default_data) {}
66 const LOGFONT &GetData() const {
67 return data;
70 void Load();
71 void SaveValues();
72 void UpdatePreview();
74 /* methods from Widget */
75 virtual void Prepare(ContainerWindow &parent, const PixelRect &rc) override;
77 /* methods from ActionListener */
78 virtual void OnAction(int id) override;
80 private:
81 /* methods from DataFieldListener */
82 virtual void OnModified(DataField &df) override;
85 void
86 FontEditWidget::Load()
88 #ifdef USE_GDI
90 DataFieldEnum &df = (DataFieldEnum &)GetDataField(FACE);
91 df.SetStringAutoAdd(data.lfFaceName);
92 GetControl(FACE).RefreshDisplay();
94 #endif
96 LoadValue(HEIGHT, (int)data.lfHeight);
97 LoadValue(WEIGHT, data.lfWeight > 500);
98 LoadValue(ITALIC, !!data.lfItalic);
100 UpdatePreview();
103 inline void
104 FontEditWidget::SaveValues()
106 #ifdef USE_GDI
107 CopyString(data.lfFaceName, GetDataField(FACE).GetAsString(), LF_FACESIZE);
108 #endif
110 data.lfHeight = GetValueInteger(HEIGHT);
111 data.lfWeight = GetValueBoolean(WEIGHT) ? 700 : 500;
112 data.lfItalic = GetValueBoolean(ITALIC);
115 void
116 FontEditWidget::UpdatePreview()
118 /* revert to default font first, to avoid freeing the Font while it
119 is still being referenced */
120 LargeTextWindow &preview = (LargeTextWindow &)GetGeneric(PREVIEW);
121 preview.SetFont(*GetLook().text_font);
123 font.Load(data);
125 #ifdef ENABLE_OPENGL
126 TextCache::Flush();
127 #endif
129 if (font.IsDefined()) {
130 preview.SetFont(font);
131 preview.SetText(_T("Sample Text\n123"));
132 } else {
133 preview.SetText(_("Font not found."));
137 void
138 FontEditWidget::Prepare(ContainerWindow &parent, const PixelRect &rc)
140 RowFormWidget::Prepare(parent, rc);
142 #ifdef USE_GDI
143 WndProperty *wp = AddEnum(_("Font face"), NULL, this);
145 DataFieldEnum &df = *(DataFieldEnum *)wp->GetDataField();
146 df.addEnumText(_T("Tahoma"));
147 df.addEnumText(_T("TahomaBD"));
148 df.addEnumText(_T("DejaVu Sans Condensed"));
150 #else
151 /* we cannot obtain a list of fonts on SDL/OpenGL currently */
152 #endif
154 AddInteger(_("Height"), NULL, _T("%d"), _T("%d"), 1, 200, 1, 0, this);
155 AddBoolean(_("Bold"), NULL, false, this);
156 AddBoolean(_("Italic"), NULL, false, this);
158 PixelRect preview_rc { 0, 0, Layout::Scale(250), Layout::Scale(100) };
159 LargeTextWindowStyle preview_style;
160 preview_style.Border();
161 LargeTextWindow *preview = new LargeTextWindow();
162 preview->Create(*(ContainerWindow *)GetWindow(), preview_rc, preview_style);
163 Add(preview);
165 Load();
168 void
169 FontEditWidget::OnModified(DataField &df)
171 SaveValues();
172 UpdatePreview();
175 void
176 FontEditWidget::OnAction(int id)
178 data = default_data;
179 Load();
182 bool
183 dlgFontEditShowModal(const TCHAR *type, LOGFONT &data,
184 const LOGFONT &default_data)
186 StaticString<128> title;
187 title.Format(_T("%s: %s"), _("Edit Font"), type);
189 FontEditWidget *widget =
190 new FontEditWidget(data, default_data);
192 WidgetDialog dialog(UIGlobals::GetDialogLook());
193 dialog.CreateAuto(UIGlobals::GetMainWindow(), title, widget);
194 dialog.AddButton(_("OK"), mrOK);
195 dialog.AddButton(_("Reset"), *widget, 1);
196 dialog.AddButton(_("Cancel"), mrCancel);
198 if (dialog.ShowModal() != mrOK)
199 return false;
201 data = widget->GetData();
202 return true;