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  *
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 (std::list<SetupTab*>::iterator, it, mTabs)
193  {
194  (*it)->apply();
195  }
196  }
197  else if (eventId == "Cancel")
198  {
199  doCancel();
200  }
201  else if (eventId == "Store")
202  {
203  if (chatWindow != nullptr)
205  config.write();
207  }
208  else if (eventId == "Reset Windows")
209  {
210  // Bail out if this action happens to be activated before the windows
211  // are created (though it should be disabled then)
212  if (statusWindow == nullptr)
213  return;
214 
215  FOR_EACH (std::list<Window*>::const_iterator, it, mWindowsToReset)
216  {
217  if (*it != nullptr)
218  (*it)->resetToDefaultSize();
219  }
220  }
221 }
222 
223 void SetupWindow::setInGame(const bool inGame)
224 {
225  if (mResetWindows != nullptr)
226  mResetWindows->setEnabled(inGame);
227 }
228 
230 {
232  mModsTab = new Setup_Mods(this);
233  mTabs.push_back(mModsTab);
235  mQuickTab = new Setup_Quick(this);
236  mTabs.push_back(mQuickTab);
238  FOR_EACH (std::list<SetupTab*>::const_iterator, it, mTabs)
239  {
240  if (*it != nullptr)
241  (*it)->externalUpdated();
242  }
243 }
244 
246 {
247  if (page != nullptr)
248  {
249  mTabs.remove(page);
250  mPanel->removeTab(mPanel->getTab(page->getName()));
251  }
252 }
253 
255 {
260 }
261 
263 {
264  FOR_EACH (std::list<SetupTab*>::const_iterator, it, mTabs)
265  {
266  if (*it != nullptr)
267  (*it)->externalUnloaded();
268  }
270 }
271 
273 {
274  mWindowsToReset.push_back(window);
275 }
276 
278 {
279  FOR_EACH (std::list<Window*>::iterator, it, mWindowsToReset)
280  {
281  if (*it == window)
282  {
283  mWindowsToReset.erase(it);
284  return;
285  }
286  }
287 }
288 
290 {
291  FOR_EACH (std::list<Window*>::const_iterator, it, mWindowsToReset)
292  {
293  Window *const window = *it;
294  if ((window != nullptr) && !window->isSticky())
295  window->setVisible(Visible_false);
296  }
298 }
299 
301 {
303  FOR_EACH (std::list<SetupTab*>::iterator, it, mTabs)
304  {
305  (*it)->cancel();
306  }
307 }
308 
309 void SetupWindow::activateTab(const std::string &name)
310 {
311  std::string tmp = gettext(name.c_str());
313 }
314 
316 {
318  Window::setVisible(visible);
319 }
320 
322 {
323  Window::widgetResized(event);
324 
325  const Rect area = getChildrenArea();
326  int x = area.width;
327  const int height = area.height;
328  const int width = area.width;
329  mPanel->setDimension(Rect(5, 5, width - 10, height - 40));
330  FOR_EACH (STD_VECTOR<Button*>::iterator, it, mButtons)
331  {
332  Button *const btn = *it;
333  x -= btn->getWidth() + mButtonPadding;
334  btn->setPosition(x, height - btn->getHeight() - mButtonPadding);
335  }
336  if (mResetWindows != nullptr)
337  {
339  height - mVersion->getHeight() - mResetWindows->getHeight() - 9);
340  }
341  else
342  {
343  mVersion->setPosition(9, height - mVersion->getHeight() - 30);
344  }
345 }
SetupWindow::action
void action(const ActionEvent &event)
Definition: setupwindow.cpp:183
Window::getChildrenArea
Rect getChildrenArea()
Definition: window.cpp:1472
touchmanager.h
SetupWindow::activateTab
void activateTab(const std::string &name)
Definition: setupwindow.cpp:309
SetupWindow::mResetWindows
Button * mResetWindows
Definition: setupwindow.h:88
TabbedArea::getTab
Tab * getTab(const std::string &name) const
Definition: tabbedarea.cpp:173
Rect::height
int height
Definition: rect.h:223
Setup_Theme
Definition: setup_theme.h:36
Setup_Relations
Definition: setup_relations.h:39
touchManager
TouchManager touchManager
Definition: touchmanager.cpp:49
Widget::setPosition
void setPosition(const int x, const int y)
Definition: widget.cpp:160
Setup_Misc
Definition: setup_misc.h:30
SetupWindow
Definition: setupwindow.h:41
SetupWindow::mVersion
Label * mVersion
Definition: setupwindow.h:90
Window::setMinHeight
void setMinHeight(const int height)
Definition: window.cpp:603
chatwindow.h
ActionEvent
Definition: actionevent.h:96
Window::center
void center()
Definition: window.cpp:1416
Setup_Players
Definition: setup_players.h:30
SetupWindow::hideWindows
void hideWindows()
Definition: setupwindow.cpp:289
SetupWindow::externalUnload
void externalUnload()
Definition: setupwindow.cpp:262
Window::postInit
void postInit()
Definition: window.cpp:248
SetupWindow::mModsTab
SetupTab * mModsTab
Definition: setupwindow.h:86
SetupWindow::SetupWindow
SetupWindow()
Definition: setupwindow.cpp:65
Window
Definition: window.h:98
Setup_Mods
Definition: setup_mods.h:28
statuswindow.h
BUTTON_SKIN
const std::string BUTTON_SKIN
Definition: button.h:88
Button
Definition: button.h:96
button.h
Widget::setEnabled
void setEnabled(const bool enabled)
Definition: widget.h:351
Setup_Colors
Definition: setup_colors.h:37
delete2.h
Setup_Joystick
Definition: setup_joystick.h:34
setup_players.h
SetupWindow::unloadTab
void unloadTab(SetupTab *const page)
Definition: setupwindow.cpp:245
Game::instance
static Game * instance()
Definition: game.h:81
Visible_true
const bool Visible_true
Definition: visible.h:29
SetupWindow::externalUpdate
void externalUpdate()
Definition: setupwindow.cpp:229
TabbedArea::removeTab
void removeTab(Tab *const tab)
Definition: tabbedarea.cpp:322
SetupWindow::registerWindowForReset
void registerWindowForReset(Window *const window)
Definition: setupwindow.cpp:272
Window::isSticky
bool isSticky() const
Definition: window.h:252
Window::setStickyButtonLock
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:771
setup_quick.h
SetupWindow::unloadAdditionalTabs
void unloadAdditionalTabs()
Definition: setupwindow.cpp:254
Widget::setSelectable
void setSelectable(const bool selectable)
Definition: widget.h:947
TabbedArea::getWidgetContainer
Widget * getWidgetContainer() const
Definition: tabbedarea.h:243
TabbedArea
Definition: tabbedarea.h:84
SetupWindow::mButtons
std::vector< Button * > mButtons
Definition: setupwindow.h:85
SetupWindow::mQuickTab
SetupTab * mQuickTab
Definition: setupwindow.h:87
delete2
#define delete2(var)
Definition: delete2.h:24
bool
Window::setCloseButton
void setCloseButton(const bool flag)
Definition: window.cpp:748
setup_audio.h
Modal_false
const bool Modal_false
Definition: modal.h:29
SetupTab::getName
const std::string & getName() const
Definition: setuptab.h:43
Window::enableVisibleSound
void enableVisibleSound(bool b)
Definition: window.h:480
setup_visual.h
ActionListener
Definition: actionlistener.h:77
Event
Definition: event.h:77
nullptr
#define nullptr
Definition: localconsts.h:44
SetupWindow::~SetupWindow
~SetupWindow()
Definition: setupwindow.cpp:176
Setup_Perfomance
Definition: setup_perfomance.h:30
Rect::width
int width
Definition: rect.h:218
Visible_false
const bool Visible_false
Definition: visible.h:29
SetupWindow::setVisible
void setVisible(Visible visible)
Definition: setupwindow.cpp:315
setup_mods.h
SetupWindow::mButtonPadding
int mButtonPadding
Definition: setupwindow.h:91
Game::resetAdjustLevel
void resetAdjustLevel()
Definition: game.cpp:888
createwidget.h
gettext.h
Setup_Input
Definition: setup_input.h:39
Widget::getWidth
int getWidth() const
Definition: widget.h:220
chatWindow
ChatWindow * chatWindow
Definition: chatwindow.cpp:93
x
x
Definition: graphics_calcImageRect.hpp:72
CREATEWIDGETR
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
SetupWindow::postInit
void postInit()
Definition: setupwindow.cpp:84
setup_input.h
TabbedArea::getTabContainer
Widget * getTabContainer() const
Definition: tabbedarea.h:240
Setup_Audio
Definition: setup_audio.h:30
SetupWindow::setInGame
void setInGame(const bool inGame)
Definition: setupwindow.cpp:223
Setup_Chat
Definition: setup_chat.h:28
serverConfig
Configuration serverConfig
Definition: configuration.cpp:52
Setup_Touch
Definition: setup_touch.h:29
Window::setResizable
void setResizable(const bool resize)
Definition: window.cpp:626
label.h
setupwindow.h
Configuration::getIntValue
int getIntValue(const std::string &key) const
Definition: configuration.cpp:403
setup_colors.h
Widget::getHeight
int getHeight() const
Definition: widget.h:239
statusWindow
StatusWindow * statusWindow
Definition: statuswindow.cpp:64
config
Configuration config
Definition: configuration.cpp:51
SetupWindow::doCancel
void doCancel()
Definition: setupwindow.cpp:300
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
tabbedarea.h
foreach.h
Window::getOption
int getOption(const std::string &name, const int def) const
Definition: window.cpp:1453
Configuration::write
void write()
Definition: configuration.cpp:858
main.h
TouchManager::setTempHide
void setTempHide(const bool b)
Definition: touchmanager.cpp:747
setup_joystick.h
setup_perfomance.h
Rect
Definition: rect.h:72
SetupTab
Definition: setuptab.h:36
Setup_Video
Definition: setup_video.h:40
Label
Definition: label.h:87
SetupWindow::unregisterWindowForReset
void unregisterWindowForReset(const Window *const window)
Definition: setupwindow.cpp:277
setupWindow
SetupWindow * setupWindow
Definition: setupwindow.cpp:63
configuration.h
Setup_Quick
Definition: setup_quick.h:30
BasicContainer2::add
virtual void add(Widget *const widget)
Definition: basiccontainer2.cpp:112
setup_video.h
setup_chat.h
TabbedArea::setDimension
void setDimension(const Rect &dimension)
Definition: tabbedarea.cpp:770
ChatWindow::saveState
void saveState() const
Definition: chatwindow.cpp:1912
Window::setContentSize
void setContentSize(int width, int height)
Definition: window.cpp:491
game.h
Window::widgetResized
void widgetResized(const Event &event)
Definition: window.cpp:654
new
#define new
Definition: debug_new.h:147
Setup_Visual
Definition: setup_visual.h:31
setup_relations.h
dtor.h
_
#define _(s)
Definition: gettext.h:34
TabbedArea::addTab
void addTab(Tab *const tab, Widget *const widget)
Definition: tabbedarea.cpp:237
SetupWindow::mTabs
std::list< SetupTab * > mTabs
Definition: setupwindow.h:83
Window::setVisible
virtual void setVisible(Visible visible)
Definition: window.cpp:777
SetupWindow::widgetResized
void widgetResized(const Event &event)
Definition: setupwindow.cpp:321
N_
#define N_(s)
Definition: gettext.h:35
setup_theme.h
setup_touch.h
SetupWindow::mPanel
TabbedArea * mPanel
Definition: setupwindow.h:89
FOR_EACH
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
debug.h
SetupWindow::mWindowsToReset
std::list< Window * > mWindowsToReset
Definition: setupwindow.h:84
setup_misc.h
Window::setMinWidth
void setMinWidth(const int width)
Definition: window.cpp:590
TabbedArea::enableScrollButtons
void enableScrollButtons(const bool enable)
Definition: tabbedarea.cpp:149
TabbedArea::setSelectedTabByName
void setSelectedTabByName(const std::string &name)
Definition: tabbedarea.cpp:461
FULL_VERSION
#define FULL_VERSION
Definition: main.h:163