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,
120  gettext(*curBtn),
121  *curBtn,
122  BUTTON_SKIN,
123  this);
124  mButtons.push_back(btn);
125  x -= btn->getWidth() + mButtonPadding;
126  btn->setPosition(x, height - btn->getHeight() - mButtonPadding);
127  add(btn);
128 
129  // Store this button, as it needs to be enabled/disabled
130  if (strcmp(*curBtn, "Reset Windows") == 0)
131  mResetWindows = btn;
132  }
133 
134  mPanel->setDimension(Rect(5, 5, width - 10, height - 40));
136 
137  mTabs.push_back(new Setup_Video(this));
138  mTabs.push_back(new Setup_Visual(this));
139  mTabs.push_back(new Setup_Audio(this));
140  mTabs.push_back(new Setup_Perfomance(this));
141  mTabs.push_back(new Setup_Touch(this));
142  mTabs.push_back(new Setup_Input(this));
143  mTabs.push_back(new Setup_Joystick(this));
144  mTabs.push_back(new Setup_Colors(this));
145  mTabs.push_back(new Setup_Chat(this));
146  mTabs.push_back(new Setup_Players(this));
147  mTabs.push_back(new Setup_Relations(this));
148  mTabs.push_back(new Setup_Theme(this));
149  mTabs.push_back(new Setup_Misc(this));
150 
151  FOR_EACH (std::list<SetupTab*>::const_iterator, i, mTabs)
152  {
153  SetupTab *const tab = *i;
154  mPanel->addTab(tab->getName(), tab);
155  }
156  add(mPanel);
157 
158  if (mResetWindows != nullptr)
159  {
161  height - mVersion->getHeight() - mResetWindows->getHeight() - 9);
162  }
163  else
164  {
165  mVersion->setPosition(9, height - mVersion->getHeight() - 30);
166  }
167  add(mVersion);
168 
169  center();
170 
171  widgetResized(Event(nullptr));
172  setInGame(false);
173  enableVisibleSound(true);
174 }
175 
177 {
178  delete_all(mTabs);
179  mButtons.clear();
180  setupWindow = nullptr;
181 }
182 
184 {
185  if (Game::instance() != nullptr)
187  const std::string &eventId = event.getId();
188 
189  if (eventId == "Apply")
190  {
192  for_each(mTabs.begin(), mTabs.end(), std::mem_fun(&SetupTab::apply));
193  }
194  else if (eventId == "Cancel")
195  {
196  doCancel();
197  }
198  else if (eventId == "Store")
199  {
200  if (chatWindow != nullptr)
202  config.write();
204  }
205  else if (eventId == "Reset Windows")
206  {
207  // Bail out if this action happens to be activated before the windows
208  // are created (though it should be disabled then)
209  if (statusWindow == nullptr)
210  return;
211 
212  FOR_EACH (std::list<Window*>::const_iterator, it, mWindowsToReset)
213  {
214  if (*it != nullptr)
215  (*it)->resetToDefaultSize();
216  }
217  }
218 }
219 
220 void SetupWindow::setInGame(const bool inGame)
221 {
222  if (mResetWindows != nullptr)
223  mResetWindows->setEnabled(inGame);
224 }
225 
227 {
229  mModsTab = new Setup_Mods(this);
230  mTabs.push_back(mModsTab);
232  mQuickTab = new Setup_Quick(this);
233  mTabs.push_back(mQuickTab);
235  FOR_EACH (std::list<SetupTab*>::const_iterator, it, mTabs)
236  {
237  if (*it != nullptr)
238  (*it)->externalUpdated();
239  }
240 }
241 
243 {
244  if (page != nullptr)
245  {
246  mTabs.remove(page);
247  mPanel->removeTab(mPanel->getTab(page->getName()));
248  }
249 }
250 
252 {
255  delete2(mModsTab);
257 }
258 
260 {
261  FOR_EACH (std::list<SetupTab*>::const_iterator, it, mTabs)
262  {
263  if (*it != nullptr)
264  (*it)->externalUnloaded();
265  }
267 }
268 
270 {
271  mWindowsToReset.push_back(window);
272 }
273 
275 {
276  FOR_EACH (std::list<Window*>::iterator, it, mWindowsToReset)
277  {
278  if (*it == window)
279  {
280  mWindowsToReset.erase(it);
281  return;
282  }
283  }
284 }
285 
287 {
288  FOR_EACH (std::list<Window*>::const_iterator, it, mWindowsToReset)
289  {
290  Window *const window = *it;
291  if ((window != nullptr) && !window->isSticky())
292  window->setVisible(Visible_false);
293  }
295 }
296 
298 {
300  for_each(mTabs.begin(), mTabs.end(), std::mem_fun(&SetupTab::cancel));
301 }
302 
303 void SetupWindow::activateTab(const std::string &name)
304 {
305  std::string tmp = gettext(name.c_str());
307 }
308 
310 {
312  Window::setVisible(visible);
313 }
314 
316 {
317  Window::widgetResized(event);
318 
319  const Rect area = getChildrenArea();
320  int x = area.width;
321  const int height = area.height;
322  const int width = area.width;
323  mPanel->setDimension(Rect(5, 5, width - 10, height - 40));
324  FOR_EACH (STD_VECTOR<Button*>::iterator, it, mButtons)
325  {
326  Button *const btn = *it;
327  x -= btn->getWidth() + mButtonPadding;
328  btn->setPosition(x, height - btn->getHeight() - mButtonPadding);
329  }
330  if (mResetWindows != nullptr)
331  {
333  height - mVersion->getHeight() - mResetWindows->getHeight() - 9);
334  }
335  else
336  {
337  mVersion->setPosition(9, height - mVersion->getHeight() - 30);
338  }
339 }
void hideWindows()
const std::string & getName() const
Definition: setuptab.h:43
ChatWindow * chatWindow
Definition: chatwindow.cpp:89
#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:322
Definition: rect.h:72
void setMinWidth(const int width)
Definition: window.cpp:586
Definition: window.h:98
Definition: button.h:96
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:149
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
const std::string BUTTON_SKIN
Definition: button.h:88
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:768
Definition: label.h:87
void saveState() const
Tab * getTab(const std::string &name) const
Definition: tabbedarea.cpp:173
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:461
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:237
void resetAdjustLevel()
Definition: game.cpp:887