GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/emotewindow.cpp Lines: 85 143 59.4 %
Date: 2021-03-17 Branches: 59 148 39.9 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "gui/windows/emotewindow.h"
23
24
#include "gui/models/colormodel.h"
25
#include "gui/models/namesmodel.h"
26
27
#include "gui/windows/setupwindow.h"
28
29
#include "gui/widgets/colorpage.h"
30
#include "gui/widgets/createwidget.h"
31
#include "gui/widgets/emotepage.h"
32
#include "gui/widgets/scrollarea.h"
33
#include "gui/widgets/tabbedarea.h"
34
35
#include "utils/delete2.h"
36
#include "utils/foreach.h"
37
#include "utils/gettext.h"
38
39
#include "utils/translation/podict.h"
40
41
#include "resources/imageset.h"
42
43
#include "resources/db/textdb.h"
44
45
#include "resources/image/image.h"
46
47
#include "debug.h"
48
49
EmoteWindow *emoteWindow = nullptr;
50
static const int fontSizeListSize = 2;
51
52
static const char *const fontSizeList[] =
53
{
54
    // TRANSLATORS: font size
55
    N_("Normal font"),
56
    // TRANSLATORS: font size
57
    N_("Bold font"),
58
};
59
60
1
EmoteWindow::EmoteWindow() :
61
    // TRANSLATORS: emotes window name
62
1
    Window(_("Emotes"), Modal_false, nullptr, "emotes.xml"),
63


1
    mTabs(CREATEWIDGETR(TabbedArea, this)),
64

1
    mEmotePage(new EmotePage(this)),
65
1
    mColorModel(ColorModel::createDefault(this)),
66


4
    mColorPage(CREATEWIDGETR(ColorPage, this, mColorModel, "colorpage.xml")),
67
1
    mScrollColorPage(new ScrollArea(this, mColorPage, Opaque_false,
68

2
        "emotepage.xml")),
69

1
    mFontModel(new NamesModel),
70


4
    mFontPage(CREATEWIDGETR(ListBox, this, mFontModel, "")),
71
1
    mScrollFontPage(new ScrollArea(this, mFontPage, Opaque_false,
72

2
        "fontpage.xml")),
73

1
    mTextModel(new NamesModel),
74


4
    mTextPage(CREATEWIDGETR(ListBox, this, mTextModel, "")),
75
1
    mScrollTextPage(new ScrollArea(this, mTextPage, Opaque_false,
76

2
        "textpage.xml")),
77




36
    mImageSet(Theme::getImageSetFromThemeXml("emotetabs.xml", "", 17, 16))
78
{
79
2
    setShowTitle(false);
80
1
    setResizable(true);
81
82
1
    if (setupWindow != nullptr)
83
        setupWindow->registerWindowForReset(this);
84
85
1
    addMouseListener(this);
86
1
}
87
88
1
void EmoteWindow::postInit()
89
{
90
1
    Window::postInit();
91
1
    const int pad2 = mPadding * 2;
92
1
    const int width = 200;
93
1
    const int height = 150;
94
1
    setWidth(width + pad2);
95
1
    setHeight(height + pad2);
96
1
    add(mTabs);
97
1
    mTabs->setPosition(mPadding, mPadding);
98
1
    mTabs->setWidth(width);
99
1
    mTabs->setHeight(height);
100
1
    center();
101
102
3
    setTitleBarHeight(getPadding() + getTitlePadding());
103
1
    mScrollColorPage->setVerticalScrollPolicy(ScrollArea::SHOW_ALWAYS);
104
1
    mScrollColorPage->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
105
1
    mScrollFontPage->setVerticalScrollPolicy(ScrollArea::SHOW_NEVER);
106
1
    mScrollFontPage->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
107
1
    mScrollTextPage->setVerticalScrollPolicy(ScrollArea::SHOW_NEVER);
108
1
    mScrollTextPage->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
109
110
1
    mFontModel->fillFromArray(&fontSizeList[0], fontSizeListSize);
111
112
2
    mFontPage->setCenter(true);
113
2
    mTextPage->setCenter(true);
114
115

1
    if ((mImageSet != nullptr) && mImageSet->size() >= 3)
116
    {
117
        for (int f = 0; f < 3; f ++)
118
        {
119
            Image *const image = mImageSet->get(f);
120
            if (image != nullptr)
121
                image->incRef();
122
        }
123
124
        mTabs->addTab(mImageSet->get(0), mEmotePage);
125
        mTabs->addTab(mImageSet->get(2), mScrollColorPage);
126
        mTabs->addTab(mImageSet->get(1), mScrollFontPage);
127
    }
128
    else
129
    {
130
        // TRANSLATORS: emotes tab name
131
3
        mTabs->addTab(_("Emotes"), mEmotePage);
132
        // TRANSLATORS: emotes tab name
133
4
        mTabs->addTab(_("Colors"), mScrollColorPage);
134
        // TRANSLATORS: emotes tab name
135
4
        mTabs->addTab(_("Fonts"), mScrollFontPage);
136
    }
137
    // TRANSLATORS: emotes tab name
138
4
    mTabs->addTab(_("T"), mScrollTextPage);
139
140
5
    mEmotePage->setActionEventId("emote");
141
5
    mColorPage->setActionEventId("color");
142
5
    mFontPage->setActionEventId("font");
143
5
    mTextPage->setActionEventId("text");
144
1
}
145
146
3
EmoteWindow::~EmoteWindow()
147
{
148
1
    mTabs->removeAll(false);
149
1
    mTabs->removeTab(mTabs->getTabByIndex(0));
150
1
    delete2(mEmotePage)
151
1
    delete2(mColorPage)
152
1
    delete2(mColorModel)
153
1
    delete2(mScrollColorPage)
154
1
    delete2(mFontPage)
155
1
    delete2(mFontModel)
156
1
    delete2(mScrollFontPage)
157
1
    delete2(mTextPage)
158
1
    delete2(mTextModel)
159
1
    delete2(mScrollTextPage)
160
1
    if (mImageSet != nullptr)
161
    {
162
        mImageSet->decRef();
163
        mImageSet = nullptr;
164
    }
165
2
}
166
167
void EmoteWindow::show()
168
{
169
    setVisible(Visible_true);
170
    if (dictionary != nullptr)
171
    {
172
        mTextModel->clear();
173
        const STD_VECTOR<std::string> &texts = TextDb::getTexts();
174
        FOR_EACH (StringVectCIter, it, texts)
175
        {
176
            mTextModel->add(dictionary->getStr(*it));
177
        }
178
    }
179
}
180
181
void EmoteWindow::hide()
182
{
183
    setVisible(Visible_false);
184
}
185
186
std::string EmoteWindow::getSelectedEmote() const
187
{
188
    const int index = mEmotePage->getSelectedIndex();
189
    if (index < 0)
190
        return std::string();
191
192
    char chr[2];
193
    chr[0] = CAST_8('0' + index);
194
    chr[1] = 0;
195
    return std::string("%%").append(&chr[0]);
196
}
197
198
void EmoteWindow::clearEmote()
199
{
200
    const int index = mEmotePage->getSelectedIndex();
201
    mEmotePage->resetAction();
202
    if (index >= 0)
203
        setVisible(Visible_false);
204
}
205
206
std::string EmoteWindow::getSelectedColor() const
207
{
208
    const int index = mColorPage->getSelected();
209
    if (index < 0)
210
        return std::string();
211
212
    char chr[2];
213
    chr[0] = CAST_8('0' + index);
214
    chr[1] = 0;
215
    return std::string("##").append(&chr[0]);
216
}
217
218
int EmoteWindow::getSelectedTextIndex() const
219
{
220
    return mTextPage->getSelected();
221
}
222
223
void EmoteWindow::clearColor()
224
{
225
    mColorPage->resetAction();
226
    setVisible(Visible_false);
227
}
228
229
std::string EmoteWindow::getSelectedFont() const
230
{
231
    const int index = mFontPage->getSelected();
232
    if (index < 0)
233
        return std::string();
234
235
    if (index == 0)
236
        return "##b";
237
    return "##B";
238
}
239
240
void EmoteWindow::clearFont()
241
{
242
    mFontPage->setSelected(-1);
243
    setVisible(Visible_false);
244
}
245
246
void EmoteWindow::clearText()
247
{
248
    mTextPage->setSelected(-1);
249
    setVisible(Visible_false);
250
}
251
252
void EmoteWindow::addListeners(ActionListener *const listener)
253
{
254
    mEmotePage->addActionListener(listener);
255
    mColorPage->addActionListener(listener);
256
    mFontPage->addActionListener(listener);
257
    mTextPage->addActionListener(listener);
258
}
259
260
2
void EmoteWindow::widgetResized(const Event &event)
261
{
262
2
    Window::widgetResized(event);
263
2
    const int pad2 = mPadding * 2;
264
2
    const int width = mDimension.width;
265
2
    const int height = mDimension.height;
266
267
2
    mTabs->setSize(width - pad2, height - pad2);
268
2
    mTabs->adjustWidget(mEmotePage);
269
2
    mTabs->adjustWidget(mScrollColorPage);
270
6
    mColorPage->setSize(mScrollColorPage->getWidth(),
271
4
        mScrollColorPage->getHeight());
272
2
    mEmotePage->widgetResized(event);
273
2
}
274
275
1
void EmoteWindow::widgetMoved(const Event &event)
276
{
277
1
    Window::widgetMoved(event);
278
1
    mEmotePage->widgetResized(event);
279
1
}