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