GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/setupwindow.cpp Lines: 92 155 59.4 %
Date: 2019-06-27 Branches: 50 140 35.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-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
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 (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)
204
            chatWindow->saveState();
205
        config.write();
206
        serverConfig.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

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

7
}