GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/setupwindow.cpp Lines: 91 152 59.9 %
Date: 2017-11-29 Branches: 52 138 37.7 %

Line Branch Exec Source
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-2017  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
23
#include "gui/windows/setupwindow.h"
24
25
#include "configuration.h"
26
#include "game.h"
27
#include "main.h"
28
29
#include "gui/windows/chatwindow.h"
30
#include "gui/windows/statuswindow.h"
31
32
#include "gui/widgets/createwidget.h"
33
34
#include "gui/widgets/tabs/setup_audio.h"
35
#include "gui/widgets/tabs/setup_chat.h"
36
#include "gui/widgets/tabs/setup_colors.h"
37
#include "gui/widgets/tabs/setup_input.h"
38
#include "gui/widgets/tabs/setup_joystick.h"
39
#include "gui/widgets/tabs/setup_misc.h"
40
#include "gui/widgets/tabs/setup_mods.h"
41
#include "gui/widgets/tabs/setup_perfomance.h"
42
#include "gui/widgets/tabs/setup_players.h"
43
#include "gui/widgets/tabs/setup_quick.h"
44
#include "gui/widgets/tabs/setup_relations.h"
45
#include "gui/widgets/tabs/setup_theme.h"
46
#include "gui/widgets/tabs/setup_touch.h"
47
#include "gui/widgets/tabs/setup_video.h"
48
#include "gui/widgets/tabs/setup_visual.h"
49
50
#include "gui/widgets/button.h"
51
#include "gui/widgets/label.h"
52
#include "gui/widgets/tabbedarea.h"
53
54
#include "input/touch/touchmanager.h"
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
63
SetupWindow *setupWindow = nullptr;
64
65
4
SetupWindow::SetupWindow() :
66
    // TRANSLATORS: setup window name
67
4
    Window(_("Setup"), Modal_false, nullptr, "setup.xml"),
68
    ActionListener(),
69
    mTabs(),
70
    mWindowsToReset(),
71
    mButtons(),
72
    mModsTab(nullptr),
73
    mQuickTab(nullptr),
74
    mResetWindows(nullptr),
75

4
    mPanel(CREATEWIDGETR(TabbedArea, this)),
76

4
    mVersion(new Label(this, FULL_VERSION)),
77


64
    mButtonPadding(5)
78
{
79
4
    setCloseButton(true);
80
4
    setResizable(true);
81
4
    setStickyButtonLock(true);
82
4
}
83
84
4
void SetupWindow::postInit()
85
{
86
4
    Window::postInit();
87
4
    int width = 620;
88
4
    const int height = 450;
89
90

16
    if (config.getIntValue("screenwidth") >= 730)
91
4
        width += 100;
92
93
4
    setContentSize(width, height);
94
4
    setMinWidth(310);
95
4
    setMinHeight(210);
96
97
8
    mPanel->setSelectable(false);
98
12
    mPanel->getTabContainer()->setSelectable(false);
99
12
    mPanel->getWidgetContainer()->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
4
    int x = width;
114

16
    mButtonPadding = getOption("buttonPadding", 5);
115
20
    for (const char *const * curBtn = buttonNames;
116
20
         *curBtn != nullptr;
117
         ++ curBtn)
118
    {
119


112
        Button *const btn = new Button(this, gettext(*curBtn), *curBtn, this);
120
16
        mButtons.push_back(btn);
121
32
        x -= btn->getWidth() + mButtonPadding;
122
32
        btn->setPosition(x, height - btn->getHeight() - mButtonPadding);
123
16
        add(btn);
124
125
        // Store this button, as it needs to be enabled/disabled
126
16
        if (strcmp(*curBtn, "Reset Windows") == 0)
127
4
            mResetWindows = btn;
128
    }
129
130
8
    mPanel->setDimension(Rect(5, 5, width - 10, height - 40));
131
4
    mPanel->enableScrollButtons(true);
132
133
8
    mTabs.push_back(new Setup_Video(this));
134
8
    mTabs.push_back(new Setup_Visual(this));
135
8
    mTabs.push_back(new Setup_Audio(this));
136
8
    mTabs.push_back(new Setup_Perfomance(this));
137
8
    mTabs.push_back(new Setup_Touch(this));
138
8
    mTabs.push_back(new Setup_Input(this));
139
8
    mTabs.push_back(new Setup_Joystick(this));
140
8
    mTabs.push_back(new Setup_Colors(this));
141
8
    mTabs.push_back(new Setup_Chat(this));
142
8
    mTabs.push_back(new Setup_Players(this));
143
8
    mTabs.push_back(new Setup_Relations(this));
144
8
    mTabs.push_back(new Setup_Theme(this));
145
8
    mTabs.push_back(new Setup_Misc(this));
146
147
20
    FOR_EACH (std::list<SetupTab*>::const_iterator, i, mTabs)
148
    {
149
52
        SetupTab *const tab = *i;
150
104
        mPanel->addTab(tab->getName(), tab);
151
    }
152
4
    add(mPanel);
153
154
4
    if (mResetWindows != nullptr)
155
    {
156
8
        mVersion->setPosition(9,
157
12
            height - mVersion->getHeight() - mResetWindows->getHeight() - 9);
158
    }
159
    else
160
    {
161
        mVersion->setPosition(9, height - mVersion->getHeight() - 30);
162
    }
163
4
    add(mVersion);
164
165
4
    center();
166
167
4
    widgetResized(Event(nullptr));
168
4
    setInGame(false);
169
8
    enableVisibleSound(true);
170
4
}
171
172
28
SetupWindow::~SetupWindow()
173
{
174
8
    delete_all(mTabs);
175
8
    mButtons.clear();
176
4
    setupWindow = nullptr;
177
8
}
178
179
void SetupWindow::action(const ActionEvent &event)
180
{
181
    if (Game::instance() != nullptr)
182
        Game::instance()->resetAdjustLevel();
183
    const std::string &eventId = event.getId();
184
185
    if (eventId == "Apply")
186
    {
187
        setVisible(Visible_false);
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)
197
            chatWindow->saveState();
198
        config.write();
199
        serverConfig.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

4
    if (mResetWindows != nullptr)
219
4
        mResetWindows->setEnabled(inGame);
220
}
221
222
void SetupWindow::externalUpdate()
223
{
224
    unloadAdditionalTabs();
225
    mModsTab = new Setup_Mods(this);
226
    mTabs.push_back(mModsTab);
227
    mPanel->addTab(mModsTab->getName(), mModsTab);
228
    mQuickTab = new Setup_Quick(this);
229
    mTabs.push_back(mQuickTab);
230
    mPanel->addTab(mQuickTab->getName(), mQuickTab);
231
    FOR_EACH (std::list<SetupTab*>::const_iterator, it, mTabs)
232
    {
233
        if (*it != nullptr)
234
            (*it)->externalUpdated();
235
    }
236
}
237
238
void SetupWindow::unloadTab(SetupTab *const page)
239
{
240
    if (page != nullptr)
241
    {
242
        mTabs.remove(page);
243
        mPanel->removeTab(mPanel->getTab(page->getName()));
244
    }
245
}
246
247
void SetupWindow::unloadAdditionalTabs()
248
{
249
    unloadTab(mModsTab);
250
    unloadTab(mQuickTab);
251
    delete2(mModsTab);
252
    delete2(mQuickTab);
253
}
254
255
void SetupWindow::externalUnload()
256
{
257
    FOR_EACH (std::list<SetupTab*>::const_iterator, it, mTabs)
258
    {
259
        if (*it != nullptr)
260
            (*it)->externalUnloaded();
261
    }
262
    unloadAdditionalTabs();
263
}
264
265
8
void SetupWindow::registerWindowForReset(Window *const window)
266
{
267
16
    mWindowsToReset.push_back(window);
268
8
}
269
270
4
void SetupWindow::unregisterWindowForReset(const Window *const window)
271
{
272
12
    FOR_EACH (std::list<Window*>::iterator, it, mWindowsToReset)
273
    {
274
6
        if (*it == window)
275
        {
276
8
            mWindowsToReset.erase(it);
277
4
            return;
278
        }
279
    }
280
}
281
282
void SetupWindow::hideWindows()
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
    }
290
    setVisible(Visible_false);
291
}
292
293
void SetupWindow::doCancel()
294
{
295
    setVisible(Visible_false);
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());
302
    mPanel->setSelectedTabByName(tmp);
303
}
304
305
void SetupWindow::setVisible(Visible visible)
306
{
307
    touchManager.setTempHide(visible == Visible_true);
308
    Window::setVisible(visible);
309
}
310
311
8
void SetupWindow::widgetResized(const Event &event)
312
{
313
8
    Window::widgetResized(event);
314
315
16
    const Rect area = getChildrenArea();
316
8
    int x = area.width;
317
8
    const int height = area.height;
318
8
    const int width = area.width;
319
16
    mPanel->setDimension(Rect(5, 5, width - 10, height - 40));
320
48
    FOR_EACH (STD_VECTOR<Button*>::iterator, it, mButtons)
321
    {
322
16
        Button *const btn = *it;
323
32
        x -= btn->getWidth() + mButtonPadding;
324
32
        btn->setPosition(x, height - btn->getHeight() - mButtonPadding);
325
    }
326
8
    if (mResetWindows != nullptr)
327
    {
328
8
        mVersion->setPosition(9,
329
12
            height - mVersion->getHeight() - mResetWindows->getHeight() - 9);
330
    }
331
    else
332
    {
333
8
        mVersion->setPosition(9, height - mVersion->getHeight() - 30);
334
    }
335

14
}