ManaPlus
setupwindow.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 #include "game.h"
28 #include "main.h"
29 
30 #include "gui/windows/chatwindow.h"
32 
34 
50 
51 #include "gui/widgets/button.h"
52 #include "gui/widgets/label.h"
53 #include "gui/widgets/tabbedarea.h"
54 
56 
57 #include "utils/delete2.h"
58 #include "utils/dtor.h"
59 #include "utils/foreach.h"
60 #include "utils/gettext.h"
61 
62 #include "debug.h"
63 
65 
67  // TRANSLATORS: setup window name
68  Window(_("Setup"), Modal_false, nullptr, "setup.xml"),
70  mTabs(),
71  mWindowsToReset(),
72  mButtons(),
73  mModsTab(nullptr),
74  mQuickTab(nullptr),
75  mResetWindows(nullptr),
76  mPanel(CREATEWIDGETR(TabbedArea, this)),
77  mVersion(new Label(this, FULL_VERSION)),
78  mButtonPadding(5)
79 {
80  setCloseButton(true);
81  setResizable(true);
82  setStickyButtonLock(true);
83 }
84 
86 {
88  int width = 620;
89  const int height = 450;
90 
91  if (config.getIntValue("screenwidth") >= 730)
92  width += 100;
93 
94  setContentSize(width, height);
95  setMinWidth(310);
96  setMinHeight(210);
97 
98  mPanel->setSelectable(false);
101 
102  static const char *const buttonNames[] =
103  {
104  // TRANSLATORS: setup button
105  N_("Apply"),
106  // TRANSLATORS: setup button
107  N_("Cancel"),
108  // TRANSLATORS: setup button
109  N_("Store"),
110  // TRANSLATORS: setup button
111  N_("Reset Windows"),
112  nullptr
113  };
114  int x = width;
115  mButtonPadding = getOption("buttonPadding", 5);
116  for (const char *const * curBtn = buttonNames;
117  *curBtn != nullptr;
118  ++ curBtn)
119  {
120  Button *const btn = new Button(this,
121  gettext(*curBtn),
122  *curBtn,
123  BUTTON_SKIN,
124  this);
125  mButtons.push_back(btn);
126  x -= btn->getWidth() + mButtonPadding;
127  btn->setPosition(x, height - btn->getHeight() - mButtonPadding);
128  add(btn);
129 
130  // Store this button, as it needs to be enabled/disabled
131  if (strcmp(*curBtn, "Reset Windows") == 0)
132  mResetWindows = btn;
133  }
134 
135  mPanel->setDimension(Rect(5, 5, width - 10, height - 40));
137 
138  mTabs.push_back(new Setup_Video(this));
139  mTabs.push_back(new Setup_Visual(this));
140  mTabs.push_back(new Setup_Audio(this));
141  mTabs.push_back(new Setup_Perfomance(this));
142  mTabs.push_back(new Setup_Touch(this));
143  mTabs.push_back(new Setup_Input(this));
144  mTabs.push_back(new Setup_Joystick(this));
145  mTabs.push_back(new Setup_Colors(this));
146  mTabs.push_back(new Setup_Chat(this));
147  mTabs.push_back(new Setup_Players(this));
148  mTabs.push_back(new Setup_Relations(this));
149  mTabs.push_back(new Setup_Theme(this));
150  mTabs.push_back(new Setup_Misc(this));
151 
152  FOR_EACH (std::list<SetupTab*>::const_iterator, i, mTabs)
153  {
154  SetupTab *const tab = *i;
155  mPanel->addTab(tab->getName(), tab);
156  }
157  add(mPanel);
158 
159  if (mResetWindows != nullptr)
160  {
162  height - mVersion->getHeight() - mResetWindows->getHeight() - 9);
163  }
164  else
165  {
166  mVersion->setPosition(9, height - mVersion->getHeight() - 30);
167  }
168  add(mVersion);
169 
170  center();
171 
172  widgetResized(Event(nullptr));
173  setInGame(false);
174  enableVisibleSound(true);
175 }
176 
178 {
179  delete_all(mTabs);
180  mButtons.clear();
181  setupWindow = nullptr;
182 }
183 
185 {
186  if (Game::instance() != nullptr)
188  const std::string &eventId = event.getId();
189 
190  if (eventId == "Apply")
191  {
193  FOR_EACH (std::list<SetupTab*>::iterator, it, mTabs)
194  {
195  (*it)->apply();
196  }
197  }
198  else if (eventId == "Cancel")
199  {
200  doCancel();
201  }
202  else if (eventId == "Store")
203  {
204  if (chatWindow != nullptr)
206  config.write();
208  }
209  else if (eventId == "Reset Windows")
210  {
211  // Bail out if this action happens to be activated before the windows
212  // are created (though it should be disabled then)
213  if (statusWindow == nullptr)
214  return;
215 
216  FOR_EACH (std::list<Window*>::const_iterator, it, mWindowsToReset)
217  {
218  if (*it != nullptr)
219  (*it)->resetToDefaultSize();
220  }
221  }
222 }
223 
224 void SetupWindow::setInGame(const bool inGame)
225 {
226  if (mResetWindows != nullptr)
227  mResetWindows->setEnabled(inGame);
228 }
229 
231 {
233  mModsTab = new Setup_Mods(this);
234  mTabs.push_back(mModsTab);
236  mQuickTab = new Setup_Quick(this);
237  mTabs.push_back(mQuickTab);
239  FOR_EACH (std::list<SetupTab*>::const_iterator, it, mTabs)
240  {
241  if (*it != nullptr)
242  (*it)->externalUpdated();
243  }
244 }
245 
247 {
248  if (page != nullptr)
249  {
250  mTabs.remove(page);
251  mPanel->removeTab(mPanel->getTab(page->getName()));
252  }
253 }
254 
256 {
261 }
262 
264 {
265  FOR_EACH (std::list<SetupTab*>::const_iterator, it, mTabs)
266  {
267  if (*it != nullptr)
268  (*it)->externalUnloaded();
269  }
271 }
272 
274 {
275  mWindowsToReset.push_back(window);
276 }
277 
279 {
280  FOR_EACH (std::list<Window*>::iterator, it, mWindowsToReset)
281  {
282  if (*it == window)
283  {
284  mWindowsToReset.erase(it);
285  return;
286  }
287  }
288 }
289 
291 {
292  FOR_EACH (std::list<Window*>::const_iterator, it, mWindowsToReset)
293  {
294  Window *const window = *it;
295  if ((window != nullptr) && !window->isSticky())
296  window->setVisible(Visible_false);
297  }
299 }
300 
302 {
304  FOR_EACH (std::list<SetupTab*>::iterator, it, mTabs)
305  {
306  (*it)->cancel();
307  }
308 }
309 
310 void SetupWindow::activateTab(const std::string &name)
311 {
312  std::string tmp = gettext(name.c_str());
314 }
315 
317 {
319  Window::setVisible(visible);
320 }
321 
323 {
324  Window::widgetResized(event);
325 
326  const Rect area = getChildrenArea();
327  int x = area.width;
328  const int height = area.height;
329  const int width = area.width;
330  mPanel->setDimension(Rect(5, 5, width - 10, height - 40));
331  FOR_EACH (STD_VECTOR<Button*>::iterator, it, mButtons)
332  {
333  Button *const btn = *it;
334  x -= btn->getWidth() + mButtonPadding;
335  btn->setPosition(x, height - btn->getHeight() - mButtonPadding);
336  }
337  if (mResetWindows != nullptr)
338  {
340  height - mVersion->getHeight() - mResetWindows->getHeight() - 9);
341  }
342  else
343  {
344  mVersion->setPosition(9, height - mVersion->getHeight() - 30);
345  }
346 }
const std::string BUTTON_SKIN
Definition: button.h:89
ChatWindow * chatWindow
Definition: chatwindow.cpp:94
virtual void add(Widget *const widget)
Definition: button.h:102
void saveState() const
int getIntValue(const std::string &key) const
Definition: event.h:79
static Game * instance()
Definition: game.h:82
void resetAdjustLevel()
Definition: game.cpp:889
Definition: label.h:91
Definition: rect.h:74
int width
Definition: rect.h:219
int height
Definition: rect.h:224
const std::string & getName() const
Definition: setuptab.h:44
void action(const ActionEvent &event)
void doCancel()
SetupTab * mQuickTab
Definition: setupwindow.h:88
TabbedArea * mPanel
Definition: setupwindow.h:90
void hideWindows()
int mButtonPadding
Definition: setupwindow.h:92
Label * mVersion
Definition: setupwindow.h:91
void unregisterWindowForReset(const Window *const window)
void unloadAdditionalTabs()
void setVisible(Visible visible)
void activateTab(const std::string &name)
Button * mResetWindows
Definition: setupwindow.h:89
void registerWindowForReset(Window *const window)
void widgetResized(const Event &event)
std::list< SetupTab * > mTabs
Definition: setupwindow.h:84
std::vector< Button * > mButtons
Definition: setupwindow.h:86
void postInit()
Definition: setupwindow.cpp:85
void externalUpdate()
void unloadTab(SetupTab *const page)
void setInGame(const bool inGame)
std::list< Window * > mWindowsToReset
Definition: setupwindow.h:85
SetupTab * mModsTab
Definition: setupwindow.h:87
void externalUnload()
void removeTab(Tab *const tab)
Definition: tabbedarea.cpp:323
Widget * getTabContainer() const
Definition: tabbedarea.h:241
void setSelectedTabByName(const std::string &name)
Definition: tabbedarea.cpp:462
Tab * getTab(const std::string &name) const
Definition: tabbedarea.cpp:174
Widget * getWidgetContainer() const
Definition: tabbedarea.h:244
void setDimension(const Rect &dimension)
Definition: tabbedarea.cpp:771
void addTab(Tab *const tab, Widget *const widget)
Definition: tabbedarea.cpp:238
void enableScrollButtons(const bool enable)
Definition: tabbedarea.cpp:150
void setTempHide(const bool b)
void setEnabled(const bool enabled)
Definition: widget.h:352
void setSelectable(const bool selectable)
Definition: widget.h:948
void setPosition(const int x, const int y)
Definition: widget.cpp:161
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
Definition: window.h:102
void center()
Definition: window.cpp:1417
void setResizable(const bool resize)
Definition: window.cpp:627
virtual void setVisible(Visible visible)
Definition: window.cpp:778
void setContentSize(int width, int height)
Definition: window.cpp:492
int getOption(const std::string &name, const int def) const
Definition: window.cpp:1454
bool isSticky() const
Definition: window.h:253
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
Rect getChildrenArea()
Definition: window.cpp:1473
void setCloseButton(const bool flag)
Definition: window.cpp:749
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
Configuration config
Configuration serverConfig
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
void delete_all(Container &c)
Definition: dtor.h:56
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define N_(s)
Definition: gettext.h:36
#define _(s)
Definition: gettext.h:35
#define nullptr
Definition: localconsts.h:45
#define FULL_VERSION
Definition: main.h:164
const bool Modal_false
Definition: modal.h:30
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
StatusWindow * statusWindow
TouchManager touchManager
bool Visible
Definition: visible.h:30
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30