GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/setupwindow.cpp Lines: 92 152 60.5 %
Date: 2018-12-09 Branches: 50 134 37.3 %

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-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
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
2
SetupWindow::SetupWindow() :
66
    // TRANSLATORS: setup window name
67
2
    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


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

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

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

8
    if (config.getIntValue("screenwidth") >= 730)
91
2
        width += 100;
92
93
2
    setContentSize(width, height);
94
2
    setMinWidth(310);
95
2
    setMinHeight(210);
96
97
4
    mPanel->setSelectable(false);
98
6
    mPanel->getTabContainer()->setSelectable(false);
99
6
    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
2
    int x = width;
114
6
    mButtonPadding = getOption("buttonPadding", 5);
115
10
    for (const char *const * curBtn = buttonNames;
116
10
         *curBtn != nullptr;
117
         ++ curBtn)
118
    {
119
        Button *const btn = new Button(this,
120
8
            gettext(*curBtn),
121
            *curBtn,
122
            BUTTON_SKIN,
123

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

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

7
}