ManaPlus
didyouknowwindow.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
25 
26 #include "configuration.h"
27 
28 #include "enums/gui/layouttype.h"
29 
30 #include "gui/gui.h"
31 
33 
34 #include "gui/widgets/button.h"
35 #include "gui/widgets/checkbox.h"
37 #include "gui/widgets/layout.h"
38 #include "gui/widgets/scrollarea.h"
40 
41 #include "utils/delete2.h"
42 #include "utils/gettext.h"
43 
46 
47 #include "debug.h"
48 
50 
51 static const int minTip = 1;
52 static const int maxTip = 18;
53 
55  // TRANSLATORS: did you know window name
56  Window(_("Did You Know?"), Modal_false, nullptr, "didyouknow.xml"),
58  mItemLinkHandler(new ItemLinkHandler),
59  mBrowserBox(new StaticBrowserBox(this, Opaque_true,
60  "browserbox.xml")),
61  mScrollArea(new ScrollArea(this, mBrowserBox,
62  Opaque_true, "didyouknow_background.xml")),
63  // TRANSLATORS: did you know window button
64  mButtonPrev(new Button(this, _("< Previous"), "prev", BUTTON_SKIN, this)),
65  // TRANSLATORS: did you know window button
66  mButtonNext(new Button(this, _("Next >"), "next", BUTTON_SKIN, this)),
67  // TRANSLATORS: did you know window checkbox
68  mOpenAgainCheckBox(new CheckBox(this, _("Auto open this window"),
69  config.getBoolValue("showDidYouKnow"), this, "openagain"))
70 {
71  setMinWidth(300);
72  setMinHeight(220);
73  setContentSize(455, 350);
74  setWindowName("DidYouKnow");
75  setCloseButton(true);
76  setResizable(true);
77  setStickyButtonLock(true);
78 
79  if (setupWindow != nullptr)
81  setDefaultSize(500, 400, ImagePosition::CENTER, 0, 0);
82 
84  Button *const okButton = new Button(this,
85  // TRANSLATORS: did you know window button
86  _("Close"),
87  "close",
89  this);
90 
92  if (gui != nullptr)
98 
99  place(0, 0, mScrollArea, 5, 3).setPadding(3);
100  place(0, 3, mOpenAgainCheckBox, 5, 1);
101  place(1, 4, mButtonPrev, 1, 1);
102  place(2, 4, mButtonNext, 1, 1);
103  place(4, 4, okButton, 1, 1);
104 
105  Layout &layout = getLayout();
106  layout.setRowHeight(0, LayoutType::SET);
107 
108  loadWindowState();
109  enableVisibleSound(true);
110 }
111 
113 {
115  widgetResized(Event(nullptr));
116 }
117 
119 {
121 }
122 
124 {
125  const std::string &eventId = event.getId();
126  if (eventId == "close")
127  {
129  }
130  else
131  {
132  const unsigned num = config.getIntValue("currentTip");
133  if (eventId == "prev")
134  {
135  loadData(num - 1);
136  }
137  else if (eventId == "next")
138  {
139  loadData(num + 1);
140  }
141  else if (eventId == "openagain")
142  {
143  config.setValue("showDidYouKnow",
145  }
146  }
147 }
148 
150 {
152  if (num == 0)
153  {
154  const int curTip = config.getIntValue("currentTip");
155  if (curTip == 1)
156  num = maxTip;
157  else
158  num = curTip + 1;
159  }
160 
161  if (num < minTip || num > maxTip)
162  num = minTip;
163 
164  config.setValue("currentTip", num);
165 
166  loadFile(num);
167 
170 }
171 
172 void DidYouKnowWindow::loadFile(const int num)
173 {
174  const std::string file = strprintf("tips/%d", num);
175  std::string helpPath = branding.getStringValue("helpPath");
176  if (helpPath.empty())
177  helpPath = paths.getStringValue("help");
178 
179  StringVect lines;
180  TranslationManager::translateFile(pathJoin(helpPath, file).append(".txt"),
181  translator, lines);
182 
183  for (size_t i = 0, sz = lines.size(); i < sz; ++i)
184  mBrowserBox->addRow(lines[i], false);
185 }
186 
188 {
189  Window::setVisible(visible);
190 
191  if (visible == Visible_true || isWindowVisible())
192  loadData(0);
193 }
const std::string BUTTON_SKIN
Definition: button.h:89
Definition: button.h:102
bool isSelected() const
Definition: checkbox.h:147
std::string getStringValue(const std::string &key) const
void setValue(const std::string &key, const std::string &value)
int getIntValue(const std::string &key) const
void action(const ActionEvent &event)
ScrollArea * mScrollArea
void loadData(int num)
void setVisible(Visible visible)
void loadFile(const int num)
StaticBrowserBox * mBrowserBox
ItemLinkHandler * mItemLinkHandler
CheckBox * mOpenAgainCheckBox
Definition: event.h:79
Font * getHelpFont() const
Definition: gui.h:166
LayoutCell & setPadding(int p)
Definition: layoutcell.h:60
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:128
Definition: layout.h:45
void setVerticalScrollAmount(const int vScroll)
void registerWindowForReset(Window *const window)
void addRow(const std::string &row, const bool atTop)
void setOpaque(Opaque opaque)
void setEnableKeys(const bool n)
void setEnableImages(const bool n)
void setEnableTabs(const bool n)
void setProcessVars(const bool n)
void setLinkHandler(LinkHandler *linkHandler)
static bool translateFile(const std::string &fileName, PoDict *const dict, StringVect &lines)
void setFont(Font *const font)
Definition: widget.cpp:349
Definition: window.h:102
void setResizable(const bool resize)
Definition: window.cpp:627
virtual void setVisible(Visible visible)
Definition: window.cpp:778
bool isWindowVisible() const
Definition: window.h:484
void setContentSize(int width, int height)
Definition: window.cpp:492
Layout & getLayout()
Definition: window.cpp:1365
void setWindowName(const std::string &name)
Definition: window.h:355
void setMinHeight(const int height)
Definition: window.cpp:604
void postInit()
Definition: window.cpp:249
void setMinWidth(const int width)
Definition: window.cpp:591
void enableVisibleSound(bool b)
Definition: window.h:481
void widgetResized(const Event &event)
Definition: window.cpp:655
void setCloseButton(const bool flag)
Definition: window.cpp:749
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
Configuration config
Configuration paths
Configuration branding
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
static const int minTip
DidYouKnowWindow * didYouKnowWindow
static const int maxTip
#define _(s)
Definition: gettext.h:35
Gui * gui
Definition: gui.cpp:111
#define nullptr
Definition: localconsts.h:45
const bool Modal_false
Definition: modal.h:30
const bool Opaque_false
Definition: opaque.h:30
const bool Opaque_true
Definition: opaque.h:30
PoDict * translator
Definition: podict.cpp:28
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
std::string strprintf(const char *const format,...)
std::string pathJoin(std::string str1, const std::string &str2)
std::vector< std::string > StringVect
Definition: stringvector.h:29
bool Visible
Definition: visible.h:30
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30