GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/shortcutwindow.cpp Lines: 77 129 59.7 %
Date: 2018-11-12 Branches: 49 138 35.5 %

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-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/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
1
ShortcutWindow::ShortcutWindow(const std::string &restrict title,
52
                               ShortcutContainer *restrict const content,
53
                               const std::string &restrict skinFile,
54
1
                               int width, int height) :
55
    Window("Window", Modal_false, nullptr, skinFile),
56
    mItems(content),
57

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

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


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

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

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

3
}
268
269
#ifdef USE_PROFILER
270
void ShortcutWindow::logicChildren()
271
{
272
    BLOCK_START("ShortcutWindow::logicChildren")
273
    BasicContainer::logicChildren();
274
    BLOCK_END("ShortcutWindow::logicChildren")
275
}
276
#endif  // USE_PROFILER