GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/emotewindow.cpp Lines: 85 143 59.4 %
Date: 2017-11-29 Branches: 61 152 40.1 %

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

2
    mTabs(CREATEWIDGETR(TabbedArea, this)),
63

2
    mEmotePage(new EmotePage(this)),
64
2
    mColorModel(ColorModel::createDefault(this)),
65


8
    mColorPage(CREATEWIDGETR(ColorPage, this, mColorModel, "colorpage.xml")),
66
2
    mScrollColorPage(new ScrollArea(this, mColorPage, Opaque_false,
67

4
        "emotepage.xml")),
68

2
    mFontModel(new NamesModel),
69


8
    mFontPage(CREATEWIDGETR(ListBox, this, mFontModel, "")),
70
2
    mScrollFontPage(new ScrollArea(this, mFontPage, Opaque_false,
71

4
        "fontpage.xml")),
72

2
    mTextModel(new NamesModel),
73


8
    mTextPage(CREATEWIDGETR(ListBox, this, mTextModel, "")),
74
2
    mScrollTextPage(new ScrollArea(this, mTextPage, Opaque_false,
75

4
        "textpage.xml")),
76




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

2
    if ((mImageSet != nullptr) && mImageSet->size() >= 3)
115
    {
116
        for (int f = 0; f < 3; f ++)
117
        {
118
            Image *const image = mImageSet->get(f);
119
            if (image != nullptr)
120
                image->incRef();
121
        }
122
123
        mTabs->addTab(mImageSet->get(0), mEmotePage);
124
        mTabs->addTab(mImageSet->get(2), mScrollColorPage);
125
        mTabs->addTab(mImageSet->get(1), mScrollFontPage);
126
    }
127
    else
128
    {
129
        // TRANSLATORS: emotes tab name
130

10
        mTabs->addTab(_("Emotes"), mEmotePage);
131
        // TRANSLATORS: emotes tab name
132

8
        mTabs->addTab(_("Colors"), mScrollColorPage);
133
        // TRANSLATORS: emotes tab name
134

8
        mTabs->addTab(_("Fonts"), mScrollFontPage);
135
    }
136
    // TRANSLATORS: emotes tab name
137

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