GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/shortcutwindow.cpp Lines: 77 127 60.6 %
Date: 2017-11-29 Branches: 48 136 35.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2007-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/shortcutwindow.h"
24
25
#include "enums/gui/layouttype.h"
26
27
#include "gui/windows/setupwindow.h"
28
#include "gui/widgets/tabbedarea.h"
29
30
#include "gui/widgets/button.h"
31
#include "gui/widgets/createwidget.h"
32
#include "gui/widgets/layout.h"
33
#include "gui/widgets/scrollarea.h"
34
#include "gui/widgets/shortcutcontainer.h"
35
36
#include "gui/widgets/tabs/shortcuttab.h"
37
38
#include "utils/delete2.h"
39
#include "utils/foreach.h"
40
41
#include "debug.h"
42
43
ShortcutWindow *dropShortcutWindow = nullptr;
44
ShortcutWindow *emoteShortcutWindow = nullptr;
45
ShortcutWindow *itemShortcutWindow = nullptr;
46
ShortcutWindow *spellShortcutWindow = nullptr;
47
static const int SCROLL_PADDING = 0;
48
49
int ShortcutWindow::mBoxesWidth = 0;
50
51
2
ShortcutWindow::ShortcutWindow(const std::string &restrict title,
52
                               ShortcutContainer *restrict const content,
53
                               const std::string &restrict skinFile,
54
2
                               int width, int height) :
55
    Window("Window", Modal_false, nullptr, skinFile),
56
    mItems(content),
57

2
    mScrollArea(new ScrollArea(this, mItems, Opaque_false)),
58
    mTabs(nullptr),
59
    mPages(),
60

22
    mButtonIndex(0)
61
{
62
4
    setWindowName(title);
63
6
    setTitleBarHeight(getPadding() + getTitlePadding());
64
65
4
    setShowTitle(false);
66
2
    setResizable(true);
67
4
    setDefaultVisible(false);
68
4
    setSaveVisible(true);
69
4
    setAllowClose(true);
70
71
2
    mDragOffsetX = 0;
72
2
    mDragOffsetY = 0;
73
74
2
    if (content != nullptr)
75
2
        content->setWidget2(this);
76
2
    if (setupWindow != nullptr)
77
        setupWindow->registerWindowForReset(this);
78
79
2
    setMinWidth(32);
80
2
    setMinHeight(32);
81
2
    const int border = SCROLL_PADDING * 2 + getPadding() * 2;
82
2
    if (mItems != nullptr)
83
    {
84
2
        const int bw = mItems->getBoxWidth();
85
4
        const int bh = mItems->getBoxHeight();
86
4
        const int maxItems = mItems->getMaxItems();
87
2
        setMaxWidth(bw * maxItems + border);
88
2
        setMaxHeight(bh * maxItems + border);
89
90
2
        if (width == 0)
91
2
            width = bw + border;
92
2
        if (height == 0)
93
2
            height = bh * maxItems + border;
94
95
2
        setDefaultSize(width, height, ImagePosition::LOWER_RIGHT);
96
97
2
        mBoxesWidth += bw + border;
98
    }
99
100
2
    mScrollArea->setPosition(SCROLL_PADDING, SCROLL_PADDING);
101
2
    mScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
102
103
4
    place(0, 0, mScrollArea, 5, 5).setPadding(0);
104
105
2
    Layout &layout = getLayout();
106
2
    layout.setRowHeight(0, LayoutType::SET);
107
2
    layout.setMargin(0);
108
109
2
    loadWindowState();
110
4
    enableVisibleSound(true);
111
2
}
112
113
2
ShortcutWindow::ShortcutWindow(const std::string &restrict title,
114
                               const std::string &restrict skinFile,
115
2
                               const int width, const int height) :
116
    Window("Window", Modal_false, nullptr, skinFile),
117
    mItems(nullptr),
118
    mScrollArea(nullptr),
119

2
    mTabs(CREATEWIDGETR(TabbedArea, this)),
120
    mPages(),
121

16
    mButtonIndex(0)
122
{
123
4
    setWindowName(title);
124
6
    setTitleBarHeight(getPadding() + getTitlePadding());
125
4
    setShowTitle(false);
126
2
    setResizable(true);
127
4
    setDefaultVisible(false);
128
4
    setSaveVisible(true);
129
4
    setAllowClose(true);
130
131
2
    mDragOffsetX = 0;
132
2
    mDragOffsetY = 0;
133
134
2
    if (setupWindow != nullptr)
135
        setupWindow->registerWindowForReset(this);
136
137
2
    if ((width != 0) && (height != 0))
138
        setDefaultSize(width, height, ImagePosition::LOWER_RIGHT);
139
140
2
    setMinWidth(32);
141
2
    setMinHeight(32);
142
143

2
    place(0, 0, mTabs, 5, 5);
144
145
2
    Layout &layout = getLayout();
146
2
    layout.setRowHeight(0, LayoutType::SET);
147
2
    layout.setMargin(0);
148
149
2
    loadWindowState();
150
4
    enableVisibleSound(true);
151
2
}
152
153
16
ShortcutWindow::~ShortcutWindow()
154
{
155
4
    if (mTabs != nullptr)
156
2
        mTabs->removeAll();
157
4
    delete2(mTabs);
158
4
    delete2(mItems);
159
8
}
160
161
void ShortcutWindow::addButton(const std::string &text,
162
                               const std::string &eventName,
163
                               ActionListener *const listener)
164
{
165
    place(mButtonIndex++, 5, new Button(this, text, eventName, listener));
166
    Window::widgetResized(Event(nullptr));
167
}
168
169
void ShortcutWindow::addTab(const std::string &name,
170
                            ShortcutContainer *const content)
171
{
172
    if ((content == nullptr) || (mTabs == nullptr))
173
        return;
174
    ScrollArea *const scroll = new ScrollArea(this, content, Opaque_false);
175
    scroll->setPosition(SCROLL_PADDING, SCROLL_PADDING);
176
    scroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
177
    content->setWidget2(this);
178
    Tab *const tab = new ShortcutTab(this, name, content);
179
    mTabs->addTab(tab, scroll);
180
    mPages.push_back(content);
181
}
182
183
int ShortcutWindow::getTabIndex() const
184
{
185
    if (mTabs == nullptr)
186
        return 0;
187
    return mTabs->getSelectedTabIndex();
188
}
189
190
void ShortcutWindow::widgetHidden(const Event &event)
191
{
192
    Window::widgetHidden(event);
193
    if (mItems != nullptr)
194
        mItems->widgetHidden(event);
195
    if (mTabs != nullptr)
196
    {
197
        ScrollArea *const scroll = static_cast<ScrollArea *>(
198
            mTabs->getCurrentWidget());
199
        if (scroll != nullptr)
200
        {
201
            ShortcutContainer *const content = static_cast<ShortcutContainer*>(
202
                scroll->getContent());
203
204
            if (content != nullptr)
205
                content->widgetHidden(event);
206
        }
207
    }
208
}
209
210
void ShortcutWindow::mousePressed(MouseEvent &event)
211
{
212
    Window::mousePressed(event);
213
214
    if (event.isConsumed())
215
        return;
216
217
    if (event.getButton() == MouseButton::LEFT)
218
    {
219
        mDragOffsetX = event.getX();
220
        mDragOffsetY = event.getY();
221
    }
222
}
223
224
void ShortcutWindow::mouseDragged(MouseEvent &event)
225
{
226
    Window::mouseDragged(event);
227
228
    if (event.isConsumed())
229
        return;
230
231
    if (canMove() && isMovable() && mMoved)
232
    {
233
        int newX = std::max(0, getX() + event.getX() - mDragOffsetX);
234
        int newY = std::max(0, getY() + event.getY() - mDragOffsetY);
235
        newX = std::min(mainGraphics->mWidth - getWidth(), newX);
236
        newY = std::min(mainGraphics->mHeight - getHeight(), newY);
237
        setPosition(newX, newY);
238
    }
239
}
240
241
2
void ShortcutWindow::widgetMoved(const Event& event)
242
{
243
2
    Window::widgetMoved(event);
244
2
    if (mItems != nullptr)
245
2
        mItems->setRedraw(true);
246
8
    FOR_EACH (STD_VECTOR<ShortcutContainer*>::iterator, it, mPages)
247
        (*it)->setRedraw(true);
248
2
}
249
250
void ShortcutWindow::nextTab()
251
{
252
    if (mTabs != nullptr)
253
        mTabs->selectNextTab();
254
}
255
256
void ShortcutWindow::prevTab()
257
{
258
    if (mTabs != nullptr)
259
        mTabs->selectPrevTab();
260

6
}
261
262
#ifdef USE_PROFILER
263
void ShortcutWindow::logicChildren()
264
{
265
    BLOCK_START("ShortcutWindow::logicChildren")
266
    BasicContainer::logicChildren();
267
    BLOCK_END("ShortcutWindow::logicChildren")
268
}
269
#endif  // USE_PROFILER