GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/shortcutwindow.cpp Lines: 77 129 59.7 %
Date: 2021-03-17 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-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/windows/shortcutwindow.h"
25
26
#include "enums/gui/layouttype.h"
27
28
#include "gui/windows/setupwindow.h"
29
#include "gui/widgets/tabbedarea.h"
30
31
#include "gui/widgets/button.h"
32
#include "gui/widgets/createwidget.h"
33
#include "gui/widgets/layout.h"
34
#include "gui/widgets/scrollarea.h"
35
#include "gui/widgets/shortcutcontainer.h"
36
37
#include "gui/widgets/tabs/shortcuttab.h"
38
39
#include "utils/delete2.h"
40
#include "utils/foreach.h"
41
42
#include "debug.h"
43
44
ShortcutWindow *dropShortcutWindow = nullptr;
45
ShortcutWindow *emoteShortcutWindow = nullptr;
46
ShortcutWindow *itemShortcutWindow = nullptr;
47
ShortcutWindow *spellShortcutWindow = nullptr;
48
static const int SCROLL_PADDING = 0;
49
50
int ShortcutWindow::mBoxesWidth = 0;
51
52
1
ShortcutWindow::ShortcutWindow(const std::string &restrict title,
53
                               ShortcutContainer *restrict const content,
54
                               const std::string &restrict skinFile,
55
1
                               int width, int height) :
56
    Window("Window", Modal_false, nullptr, skinFile),
57
    mItems(content),
58

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

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


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

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

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

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