ManaPlus
setup_theme.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 Andrei Karas
5  * Copyright (C) 2011-2018 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "gui/gui.h"
26 #include "gui/themeinfo.h"
27 
28 #include "gui/windows/okdialog.h"
29 
31 #include "gui/models/fontsmodel.h"
33 #include "gui/models/themesmodel.h"
34 
35 #include "gui/widgets/button.h"
38 #include "gui/widgets/dropdown.h"
39 #include "gui/widgets/label.h"
41 
42 #include "configuration.h"
43 
44 #include "utils/delete2.h"
45 
46 #include "debug.h"
47 
48 const char* ACTION_THEME = "theme";
49 const char* ACTION_FONT = "font";
50 const char* ACTION_LANG = "lang";
51 const char* ACTION_BOLD_FONT = "bold font";
52 const char* ACTION_PARTICLE_FONT = "particle font";
53 const char* ACTION_HELP_FONT = "help font";
54 const char* ACTION_SECURE_FONT = "secure font";
55 const char* ACTION_NPC_FONT = "npc font";
56 const char* ACTION_JAPAN_FONT = "japanese font";
57 const char* ACTION_CHINA_FONT = "chinese font";
58 const char* ACTION_INFO = "info";
59 
60 Setup_Theme::Setup_Theme(const Widget2 *const widget) :
61  SetupTab(widget),
62  // TRANSLATORS: theme settings label
63  mThemeLabel(new Label(this, _("Gui theme"))),
64  mThemesModel(new ThemesModel),
65  mThemeDropDown(new DropDown(this, mThemesModel,
66  false, Modal_false, nullptr, std::string())),
67  mTheme(config.getStringValue("theme")),
68  mInfo(Theme::loadInfo(mTheme)),
69  mFontsModel(new FontsModel),
70  // TRANSLATORS: theme settings label
71  mFontLabel(new Label(this, _("Main Font"))),
72  mFontDropDown(new DropDown(this, mFontsModel,
73  false, Modal_false, nullptr, std::string())),
74  mFont(config.getStringValue("font")),
75  mLangListModel(new LangListModel),
76  // TRANSLATORS: theme settings label
77  mLangLabel(new Label(this, _("Language"))),
78  mLangDropDown(new DropDown(this, mLangListModel,
79  true, Modal_false, nullptr, std::string())),
80  mLang(config.getStringValue("lang")),
81  // TRANSLATORS: theme settings label
82  mBoldFontLabel(new Label(this, _("Bold font"))),
83  mBoldFontDropDown(new DropDown(this, mFontsModel,
84  false, Modal_false, nullptr, std::string())),
85  mBoldFont(config.getStringValue("boldFont")),
86  // TRANSLATORS: theme settings label
87  mParticleFontLabel(new Label(this, _("Particle font"))),
88  mParticleFontDropDown(new DropDown(this, mFontsModel,
89  false, Modal_false, nullptr, std::string())),
90  mParticleFont(config.getStringValue("particleFont")),
91  // TRANSLATORS: theme settings label
92  mHelpFontLabel(new Label(this, _("Help font"))),
93  mHelpFontDropDown(new DropDown(this, mFontsModel,
94  false, Modal_false, nullptr, std::string())),
95  mHelpFont(config.getStringValue("helpFont")),
96  // TRANSLATORS: theme settings label
97  mSecureFontLabel(new Label(this, _("Secure font"))),
98  mSecureFontDropDown(new DropDown(this, mFontsModel,
99  false, Modal_false, nullptr, std::string())),
100  mSecureFont(config.getStringValue("secureFont")),
101  // TRANSLATORS: theme settings label
102  mNpcFontLabel(new Label(this, _("Npc font"))),
103  mNpcFontDropDown(new DropDown(this, mFontsModel,
104  false, Modal_false, nullptr, std::string())),
105  mNpcFont(config.getStringValue("npcFont")),
106  // TRANSLATORS: theme settings label
107  mJapanFontLabel(new Label(this, _("Japanese font"))),
108  mJapanFontDropDown(new DropDown(this, mFontsModel,
109  false, Modal_false, nullptr, std::string())),
110  mJapanFont(config.getStringValue("japanFont")),
111  // TRANSLATORS: theme settings label
112  mChinaFontLabel(new Label(this, _("Chinese font"))),
113  mChinaFontDropDown(new DropDown(this, mFontsModel,
114  false, Modal_false, nullptr, std::string())),
115  mChinaFont(config.getStringValue("chinaFont")),
116  mFontSizeListModel(new FontSizeChoiceListModel),
117  // TRANSLATORS: theme settings label
118  mFontSizeLabel(new Label(this, _("Font size"))),
119  mFontSize(config.getIntValue("fontSize")),
120  mFontSizeDropDown(new DropDown(this, mFontSizeListModel,
121  false, Modal_false, nullptr, std::string())),
122  mNpcFontSizeListModel(new FontSizeChoiceListModel),
123  // TRANSLATORS: theme settings label
124  mNpcFontSizeLabel(new Label(this, _("Npc font size"))),
125  mNpcFontSize(config.getIntValue("npcfontSize")),
126  mNpcFontSizeDropDown(new DropDown(this, mNpcFontSizeListModel,
127  false, Modal_false, nullptr, std::string())),
128  // TRANSLATORS: button name with information about selected theme
129  mInfoButton(new Button(this, _("i"), ACTION_INFO, this)),
130  mThemeInfo()
131 {
132  // TRANSLATORS: theme settings tab name
133  setName(_("Theme"));
134 
159 
160  const std::string skin = Theme::getThemeName();
161  if (!skin.empty())
163  else
165 
166  const std::string str = config.getStringValue("lang");
167  for (int f = 0; f < langs_count; f ++)
168  {
169  if (LANG_NAME[f].value == str)
170  {
172  break;
173  }
174  }
175 
177  config.getStringValue("font")));
179  config.getStringValue("boldFont")));
181  config.getStringValue("particleFont")));
183  config.getStringValue("helpFont")));
185  config.getStringValue("secureFont")));
187  config.getStringValue("npcFont")));
189  config.getStringValue("japanFont")));
191  config.getStringValue("chinaFont")));
192 
193  updateInfo();
194 
195  // Do the layout
196  LayoutHelper h(this);
197  ContainerPlacer place = h.getPlacer(0, 0);
198 
199  place(0, 0, mThemeLabel, 5, 1);
200  place(0, 1, mLangLabel, 5, 1);
201  place(0, 2, mFontSizeLabel, 5, 1);
202  place(0, 3, mNpcFontSizeLabel, 5, 1);
203  place(0, 4, mFontLabel, 5, 1);
204  place(0, 5, mBoldFontLabel, 5, 1);
205  place(0, 6, mParticleFontLabel, 5, 1);
206  place(0, 7, mHelpFontLabel, 5, 1);
207  place(0, 8, mSecureFontLabel, 5, 1);
208  place(0, 9, mNpcFontLabel, 5, 1);
209  place(0, 10, mJapanFontLabel, 5, 1);
210  place(0, 11, mChinaFontLabel, 5, 1);
211 
212  place(6, 0, mThemeDropDown, 10, 1);
213  place(6, 1, mLangDropDown, 10, 1);
214  place(6, 2, mFontSizeDropDown, 10, 1);
215  place(6, 3, mNpcFontSizeDropDown, 10, 1);
216  place(6, 4, mFontDropDown, 10, 1);
217  place(6, 5, mBoldFontDropDown, 10, 1);
218  place(6, 6, mParticleFontDropDown, 10, 1);
219  place(6, 7, mHelpFontDropDown, 10, 1);
220  place(6, 8, mSecureFontDropDown, 10, 1);
221  place(6, 9, mNpcFontDropDown, 10, 1);
222  place(6, 10, mJapanFontDropDown, 10, 1);
223  place(6, 11, mChinaFontDropDown, 10, 1);
224 
225  place(17, 0, mInfoButton, 1, 1);
226 
227  int size = mainGraphics->mWidth - 10;
228  const int maxWidth = mFontSize * 30 + 290;
229  if (size < 465)
230  size = 465;
231  else if (size > maxWidth)
232  size = maxWidth;
233 
234  setDimension(Rect(0, 0, size, 500));
235 }
236 
238 {
239  delete2(mInfo);
245  delete2(mInfo);
246 }
247 
249 {
250  delete mInfo;
252  if (mInfo != nullptr)
253  {
254  // TRANSLATORS: theme name
255  mThemeInfo = std::string(_("Name: ")).append(mInfo->name)
256  // TRANSLATORS: theme copyright
257  .append("\n").append(_("Copyright:")).append("\n")
258  .append(mInfo->copyright);
259  }
260  else
261  {
262  mThemeInfo.clear();
263  }
264  replaceAll(mThemeInfo, "\\n", "\n");
265  mInfoButton->setEnabled(!mThemeInfo.empty());
266 }
267 
269 {
270  const std::string &eventId = event.getId();
271  if (eventId == ACTION_THEME)
272  {
273  if (mThemeDropDown->getSelected() == 0)
274  mTheme.clear();
275  else
277  updateInfo();
278  }
279  else if (eventId == ACTION_FONT)
280  {
282  }
283  else if (eventId == ACTION_LANG)
284  {
285  const int id = mLangDropDown->getSelected();
286  if (id < 0 || id >= langs_count)
287  mLang.clear();
288  else
289  mLang = LANG_NAME[id].value;
290  }
291  else if (eventId == ACTION_BOLD_FONT)
292  {
294  }
295  else if (eventId == ACTION_PARTICLE_FONT)
296  {
298  }
299  else if (eventId == ACTION_HELP_FONT)
300  {
302  }
303  else if (eventId == ACTION_SECURE_FONT)
304  {
306  }
307  else if (eventId == ACTION_NPC_FONT)
308  {
310  }
311  else if (eventId == ACTION_JAPAN_FONT)
312  {
314  }
315  else if (eventId == ACTION_CHINA_FONT)
316  {
318  }
319  else if (eventId == ACTION_INFO)
320  {
322  // TRANSLATORS: theme info dialog header
323  _("Theme info"),
324  mThemeInfo,
325  // TRANSLATORS: ok dialog button
326  _("OK"),
328  Modal_true,
330  nullptr,
331  600);
332  }
333 }
334 
336 {
337  mTheme = config.getStringValue("theme");
338  mLang = config.getStringValue("lang");
341  mParticleFont = getFileName(config.getStringValue("particleFont"));
343  mSecureFont = getFileName(config.getStringValue("secureFont"));
347 }
348 
349 #define updateField(name1, name2) if (!mInfo->name1.empty()) \
350  name2 = mInfo->name1;
351 
353 {
354  if (config.getStringValue("theme") != mTheme)
355  {
357  // TRANSLATORS: theme message dialog
358  _("Theme Changed"),
359  // TRANSLATORS: ok dialog message
360  _("Restart your client for the change to take effect."),
361  // TRANSLATORS: ok dialog button
362  _("OK"),
364  Modal_true,
366  nullptr,
367  260);
368  }
369 
370  config.setValue("selectedSkin", "");
371  if (config.getStringValue("theme") != mTheme && (mInfo != nullptr))
372  {
375  updateField(particleFont, mParticleFont);
376  updateField(helpFont, mHelpFont);
377  updateField(secureFont, mSecureFont);
378  updateField(npcFont, mNpcFont);
379  updateField(japanFont, mJapanFont);
380  updateField(chinaFont, mChinaFont);
381  if (mInfo->fontSize != 0)
382  {
383  const int size = mInfo->fontSize - 9;
384  if (size >= 0)
386  }
387  if (mInfo->npcfontSize != 0)
388  {
389  const int size = mInfo->npcfontSize - 9;
390  if (size >= 0)
392  }
393  if (mInfo->guiAlpha > 0.01F)
394  config.setValue("guialpha", mInfo->guiAlpha);
395  }
396  config.setValue("theme", mTheme);
397  config.setValue("lang", mLang);
398  if (config.getValue("font", "dejavusans.ttf") != mFont
399  || config.getValue("boldFont", "dejavusans-bold.ttf") != mBoldFont
400  || config.getValue("particleFont", "dejavusans.ttf") != mParticleFont
401  || config.getValue("helpFont", "dejavusansmono.ttf") != mHelpFont
402  || config.getValue("secureFont", "dejavusansmono.ttf") != mSecureFont
403  || config.getValue("npcFont", "dejavusans.ttf") != mNpcFont
404  || config.getValue("japanFont", "mplus-1p-regular.ttf") != mJapanFont
405  || config.getValue("chinaFont", "fonts/wqy-microhei.ttf")
406  != mChinaFont
407  || config.getIntValue("fontSize")
409  || config.getIntValue("npcfontSize")
411  {
412  config.setValue("font", "fonts/" + getFileName(mFont));
413  config.setValue("boldFont", "fonts/" + getFileName(mBoldFont));
414  config.setValue("particleFont", "fonts/" + getFileName(mParticleFont));
415  config.setValue("helpFont", "fonts/" + getFileName(mHelpFont));
416  config.setValue("secureFont", "fonts/" + getFileName(mSecureFont));
417  config.setValue("npcFont", "fonts/" + getFileName(mNpcFont));
418  config.setValue("japanFont", "fonts/" + getFileName(mJapanFont));
419  config.setValue("chinaFont", "fonts/" + getFileName(mChinaFont));
420  config.setValue("fontSize", mFontSizeDropDown->getSelected() + 9);
421  config.setValue("npcfontSize",
423  gui->updateFonts();
424  }
425 }
426 
427 #undef updateField
std::string getStringValue(const std::string &key) const
#define _(s)
Definition: gettext.h:34
const char * ACTION_INFO
Definition: setup_theme.cpp:58
void updateFonts()
Definition: gui.cpp:822
Font * boldFont
Definition: gui.cpp:111
int mNpcFontSize
Definition: setup_theme.h:106
ThemesModel * mThemesModel
Definition: setup_theme.h:55
#define updateField(name1, name2)
const char * ACTION_BOLD_FONT
Definition: setup_theme.cpp:51
const char * ACTION_CHINA_FONT
Definition: setup_theme.cpp:57
Gui * gui
Definition: gui.cpp:110
std::string mParticleFont
Definition: setup_theme.h:77
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
std::string mHelpFont
Definition: setup_theme.h:81
Label * mChinaFontLabel
Definition: setup_theme.h:95
std::string mTheme
Definition: setup_theme.h:57
Definition: rect.h:72
Label * mFontSizeLabel
Definition: setup_theme.h:100
int mWidth
Definition: graphics.h:483
Definition: button.h:94
Configuration config
void setDimension(const Rect &dimension)
Definition: widget.cpp:168
Label * mJapanFontLabel
Definition: setup_theme.h:91
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
void action(const ActionEvent &event)
DropDown * mParticleFontDropDown
Definition: setup_theme.h:76
void loadInfo(const std::string &fileName, const SkipError skipError)
Definition: mapdb.cpp:151
Label * mBoldFontLabel
Definition: setup_theme.h:71
int getIntValue(const std::string &key) const
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
const char * ACTION_SECURE_FONT
Definition: setup_theme.cpp:54
#define delete2(var)
Definition: delete2.h:24
Setup_Theme(const Widget2 *const widget)
Definition: setup_theme.cpp:60
std::string mLang
Definition: setup_theme.h:69
Button * mInfoButton
Definition: setup_theme.h:109
FontsModel * mFontsModel
Definition: setup_theme.h:60
DropDown * mJapanFontDropDown
Definition: setup_theme.h:92
const char * ACTION_LANG
Definition: setup_theme.cpp:50
std::string mJapanFont
Definition: setup_theme.h:93
const char * ACTION_NPC_FONT
Definition: setup_theme.cpp:55
DropDown * mFontSizeDropDown
Definition: setup_theme.h:102
#define new
Definition: debug_new.h:147
void setSelectedString(const std::string &str)
Definition: dropdown.cpp:445
std::string name
Definition: themeinfo.h:49
DropDown * mLangDropDown
Definition: setup_theme.h:68
float guiAlpha
Definition: themeinfo.h:61
std::string mChinaFont
Definition: setup_theme.h:97
#define CAST_S32
Definition: cast.h:29
Label * mSecureFontLabel
Definition: setup_theme.h:83
DropDown * mNpcFontDropDown
Definition: setup_theme.h:88
void adjustHeight()
Definition: dropdown.cpp:470
DropDown * mHelpFontDropDown
Definition: setup_theme.h:80
Definition: label.h:87
Label * mFontLabel
Definition: setup_theme.h:61
std::string mSecureFont
Definition: setup_theme.h:85
const char * ACTION_PARTICLE_FONT
Definition: setup_theme.cpp:52
#define nullptr
Definition: localconsts.h:44
Graphics * mainGraphics
Definition: graphics.cpp:108
const bool ShowCenter_true
Definition: showcenter.h:29
static std::string getThemeName()
Definition: theme.h:69
Definition: theme.h:52
std::string mNpcFont
Definition: setup_theme.h:89
const int langs_count
Definition: langlistmodel.h:42
Label * mHelpFontLabel
Definition: setup_theme.h:79
FontSizeChoiceListModel * mFontSizeListModel
Definition: setup_theme.h:99
std::string mThemeInfo
Definition: setup_theme.h:110
ContainerPlacer getPlacer(const int x, const int y)
void setEnabled(const bool enabled)
Definition: widget.h:351
std::string copyright
Definition: themeinfo.h:50
std::string getSelectedString() const
Definition: dropdown.cpp:461
std::string getFileName(const std::string &path)
int fontSize
Definition: themeinfo.h:59
const char * ACTION_THEME
Definition: setup_theme.cpp:48
DropDown * mThemeDropDown
Definition: setup_theme.h:56
DropDown * mNpcFontSizeDropDown
Definition: setup_theme.h:107
std::string getValue(const std::string &key, const std::string &deflt) const
void updateInfo()
#define CREATEWIDGET(type,...)
Definition: createwidget.h:28
static ThemeInfo * loadInfo(const std::string &themeName)
Definition: theme.cpp:1215
DropDown * mFontDropDown
Definition: setup_theme.h:62
std::string mBoldFont
Definition: setup_theme.h:73
DropDown * mSecureFontDropDown
Definition: setup_theme.h:84
void setName(const std::string &name)
Definition: setuptab.h:67
const char * ACTION_HELP_FONT
Definition: setup_theme.cpp:53
LangListModel * mLangListModel
Definition: setup_theme.h:65
DropDown * mBoldFontDropDown
Definition: setup_theme.h:72
ThemeInfo * mInfo
Definition: setup_theme.h:58
const char * ACTION_JAPAN_FONT
Definition: setup_theme.cpp:56
const char * ACTION_FONT
Definition: setup_theme.cpp:49
const Language LANG_NAME[langs_count]
Definition: langlistmodel.h:44
Label * mLangLabel
Definition: setup_theme.h:67
void setSelected(int selected)
Definition: dropdown.cpp:514
Label * mThemeLabel
Definition: setup_theme.h:54
std::string value
Definition: langlistmodel.h:38
void setValue(const std::string &key, const std::string &value)
Label * mNpcFontSizeLabel
Definition: setup_theme.h:105
FontSizeChoiceListModel * mNpcFontSizeListModel
Definition: setup_theme.h:104
std::string mFont
Definition: setup_theme.h:63
int getSelected() const
Definition: dropdown.cpp:509
Label * mParticleFontLabel
Definition: setup_theme.h:75
Label * mNpcFontLabel
Definition: setup_theme.h:87
DropDown * mChinaFontDropDown
Definition: setup_theme.h:96
int npcfontSize
Definition: themeinfo.h:60