GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windowmenu.cpp Lines: 1 225 0.4 %
Date: 2018-05-19 03:07:18 Branches: 2 336 0.6 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-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/windowmenu.h"
24
25
#include "configuration.h"
26
27
#include "input/inputmanager.h"
28
29
#include "gui/buttoninfo.h"
30
#include "gui/buttontext.h"
31
#include "gui/popupmanager.h"
32
#include "gui/skin.h"
33
34
#include "gui/popups/popupmenu.h"
35
#include "gui/popups/textpopup.h"
36
37
#include "gui/windows/skilldialog.h"
38
39
#include "gui/widgets/button.h"
40
41
#include "utils/dtor.h"
42
#include "utils/foreach.h"
43
#include "utils/gettext.h"
44
45
#include "debug.h"
46
47
WindowMenu *windowMenu = nullptr;
48
49
WindowMenu::WindowMenu(const Widget2 *const widget) :
50
    Container(widget),
51
    ConfigListener(),
52
    ActionListener(),
53
    SelectionListener(),
54
    MouseListener(),
55
    mSkin(theme != nullptr ? theme->load("windowmenu.xml", "",
56
        true, Theme::getThemePath()) : nullptr),
57
    mPadding(mSkin != nullptr ? mSkin->getPadding() : 1),
58
    mSpacing(mSkin != nullptr ? mSkin->getOption("spacing", 3) : 3),
59
    mButtons(),
60
    mButtonTexts(),
61
    mButtonNames(),
62
    mHaveMouse(false),
63
    mAutoHide(1),
64
    mSmallWindow(mainGraphics->getWidth() < 600)
65
{
66
    int x = mPadding;
67
    int h = 0;
68
69
    setFocusable(false);
70
    // TRANSLATORS: short button name for who is online window.
71
    addButton(N_("ONL"),
72
        // TRANSLATORS: long button name for who is online window.
73
        _("Who is online"), x, h, InputAction::WINDOW_ONLINE,
74
        Visible_true);
75
    // TRANSLATORS: short button name for help window.
76
    addButton(N_("HLP"),
77
        // TRANSLATORS: long button name for help window.
78
        _("Help"), x, h, InputAction::WINDOW_HELP,
79
        Visible_true);
80
    // TRANSLATORS: short button name for quests window.
81
    addButton(N_("QE"),
82
        // TRANSLATORS: long button name for quests window.
83
        _("Quests"), x, h, InputAction::WINDOW_QUESTS,
84
        Visible_true);
85
    // TRANSLATORS: short button name for kill stats window.
86
    addButton(N_("KS"),
87
        // TRANSLATORS: long button name for kill stats window.
88
        _("Kill stats"), x, h, InputAction::WINDOW_KILLS,
89
        Visible_true);
90
    addButton(":-)",
91
        // TRANSLATORS: long button name for emotes window.
92
        _("Smilies"), x, h, InputAction::WINDOW_EMOTE_SHORTCUT,
93
        Visible_true);
94
    // TRANSLATORS: short button name for chat window.
95
    addButton(N_("CH"),
96
        // TRANSLATORS: longt button name for chat window.
97
        _("Chat"), x, h, InputAction::WINDOW_CHAT,
98
#ifdef ANDROID
99
        Visible_true);
100
#else  // ANDROID
101
        Visible_false);
102
#endif  // ANDROID
103
104
    // TRANSLATORS: short button name for status window.
105
    addButton(N_("STA"),
106
        // TRANSLATORS: long button name for status window.
107
        _("Status"), x, h, InputAction::WINDOW_STATUS,
108
        Visible_true);
109
    // TRANSLATORS: short button name for equipment window.
110
    addButton(N_("EQU"),
111
        // TRANSLATORS: long button name for equipment window.
112
        _("Equipment"), x, h, InputAction::WINDOW_EQUIPMENT,
113
        Visible_true);
114
    // TRANSLATORS: short button name for inventory window.
115
    addButton(N_("INV"),
116
        // TRANSLATORS: long button name for inventory window.
117
        _("Inventory"), x, h, InputAction::WINDOW_INVENTORY,
118
        Visible_true);
119
    // TRANSLATORS: short button name for cart window.
120
    addButton(N_("CA"),
121
        // TRANSLATORS: long button name for cart window.
122
        _("Cart"), x, h, InputAction::WINDOW_CART,
123
        Visible_true);
124
    // TRANSLATORS: short button name for map window.
125
    addButton(N_("MAP"),
126
        // TRANSLATORS: long button name for map window.
127
        _("Map"), x, h, InputAction::WINDOW_MINIMAP,
128
        Visible_false);
129
130
    if (skillDialog->hasSkills())
131
    {
132
        // TRANSLATORS: short button name for skills window.
133
        addButton(N_("SKI"),
134
            // TRANSLATORS: long button name for skills window.
135
            _("Skills"), x, h, InputAction::WINDOW_SKILL,
136
        Visible_true);
137
    }
138
139
    // TRANSLATORS: short button name for social window.
140
    addButton(N_("SOC"),
141
        // TRANSLATORS: long button name for social window.
142
        _("Social"), x, h, InputAction::WINDOW_SOCIAL,
143
        Visible_true);
144
    // TRANSLATORS: short button name for shortcuts window.
145
    addButton(N_("SH"),
146
        // TRANSLATORS: long button name for shortcuts window.
147
        _("Shortcuts"), x, h, InputAction::WINDOW_SHORTCUT,
148
        Visible_true);
149
    // TRANSLATORS: short button name for spells window.
150
    addButton(N_("SP"),
151
        // TRANSLATORS: long button name for spells window.
152
        _("Spells"), x, h, InputAction::WINDOW_SPELLS,
153
        Visible_true);
154
    // TRANSLATORS: short button name for drops window.
155
    addButton(N_("DR"),
156
        // TRANSLATORS: long button name for drops window.
157
        _("Drop"), x, h, InputAction::WINDOW_DROP,
158
        Visible_false);
159
    // TRANSLATORS: short button name for did you know window.
160
    addButton(N_("YK"),
161
        // TRANSLATORS: long button name for did you know window.
162
        _("Did you know"), x, h, InputAction::WINDOW_DIDYOUKNOW,
163
        Visible_false);
164
    // TRANSLATORS: short button name for shop window.
165
    addButton(N_("SHP"),
166
        // TRANSLATORS: long button name for shop window.
167
        _("Shop"), x, h, InputAction::WINDOW_SHOP,
168
        Visible_false);
169
    // TRANSLATORS: short button name for outfits window.
170
    addButton(N_("OU"),
171
        // TRANSLATORS: long button name for outfits window.
172
        _("Outfits"), x, h, InputAction::WINDOW_OUTFIT,
173
        Visible_false);
174
    // TRANSLATORS: short button name for updates window.
175
    addButton(N_("UP"),
176
        // TRANSLATORS: long button name for updates window.
177
        _("Updates"), x, h, InputAction::WINDOW_UPDATER,
178
        Visible_false);
179
    // TRANSLATORS: short button name for bank window.
180
    addButton(N_("BA"),
181
        // TRANSLATORS: long button name for bank window.
182
        _("Bank"), x, h, InputAction::WINDOW_BANK,
183
        Visible_true);
184
    // TRANSLATORS: short button name for mail window.
185
    addButton(N_("MA"),
186
        // TRANSLATORS: long button name for mail window.
187
        _("Mail"), x, h, InputAction::WINDOW_MAIL,
188
        Visible_true);
189
    // TRANSLATORS: short button name for clan window.
190
    addButton(N_("CL"),
191
        // TRANSLATORS: long button name for clan window.
192
        _("Clan"), x, h, InputAction::WINDOW_CLAN,
193
        Visible_true);
194
    // TRANSLATORS: short button name for server info window.
195
    addButton(N_("SI"),
196
        // TRANSLATORS: long button name for server info window.
197
        _("Server info"), x, h, InputAction::WINDOW_SERVER_INFO,
198
        Visible_true);
199
    // TRANSLATORS: short button name for debug window.
200
    addButton(N_("DBG"),
201
        // TRANSLATORS: long button name for debug window.
202
        _("Debug"), x, h, InputAction::WINDOW_DEBUG,
203
#ifdef ANDROID
204
        Visible_true);
205
#else  // ANDROID
206
        Visible_false);
207
#endif  // ANDROID
208
209
    // TRANSLATORS: short button name for windows list menu.
210
    addButton(N_("WIN"),
211
        // TRANSLATORS: long button name for windows list menu.
212
        _("Windows"), x, h, InputAction::SHOW_WINDOWS,
213
        Visible_false);
214
    // TRANSLATORS: short button name for setup window.
215
    addButton(N_("SET"),
216
        // TRANSLATORS: long button name for setup window.
217
        _("Setup"), x, h, InputAction::WINDOW_SETUP,
218
        Visible_true);
219
220
    x += mPadding - mSpacing;
221
    if (mainGraphics != nullptr)
222
        setDimension(Rect(mainGraphics->mWidth - x, 0, x, h));
223
224
    loadButtons();
225
226
    addMouseListener(this);
227
    setVisible(Visible_true);
228
229
    config.addListener("autohideButtons", this);
230
    mAutoHide = config.getIntValue("autohideButtons");
231
}
232
233
WindowMenu::~WindowMenu()
234
{
235
    config.removeListener("autohideButtons", this);
236
    CHECKLISTENERS
237
238
    for (std::map <std::string, ButtonInfo*>::iterator
239
         it = mButtonNames.begin(),
240
         it_fend = mButtonNames.end(); it != it_fend; ++it)
241
    {
242
        delete (*it).second;
243
    }
244
    mButtonNames.clear();
245
    FOR_EACH (STD_VECTOR <Button*>::iterator, it, mButtons)
246
    {
247
        Button *const btn = *it;
248
        if (btn == nullptr)
249
            continue;
250
        if (btn->mVisible == Visible_false)
251
            delete btn;
252
    }
253
    delete_all(mButtonTexts);
254
    mButtonTexts.clear();
255
    if (mSkin != nullptr)
256
    {
257
        if (theme != nullptr)
258
            theme->unload(mSkin);
259
        mSkin = nullptr;
260
    }
261
}
262
263
void WindowMenu::action(const ActionEvent &event)
264
{
265
    const std::string &eventId = event.getId();
266
    const std::map <std::string, ButtonInfo*>::iterator
267
        it = mButtonNames.find(eventId);
268
    if (it == mButtonNames.end())
269
        return;
270
271
    const ButtonInfo *const info = (*it).second;
272
    if (info == nullptr)
273
        return;
274
275
    inputManager.executeAction(info->key);
276
}
277
278
void WindowMenu::addButton(const char *const text,
279
                           const std::string &description,
280
                           int &restrict x,
281
                           int &restrict h,
282
                           const InputActionT key,
283
                           const Visible visible)
284
{
285
    Button *const btn = new Button(this,
286
        gettext(text),
287
        text,
288
        BUTTON_SKIN,
289
        this);
290
    btn->setPosition(x, mPadding);
291
    btn->setDescription(description);
292
    btn->setTag(CAST_S32(key));
293
    add(btn);
294
    btn->setFocusable(false);
295
    x += btn->getWidth() + mSpacing;
296
    h = btn->getHeight() + 2 * mPadding;
297
    mButtons.push_back(btn);
298
    mButtonNames[text] = new ButtonInfo(btn, key, visible);
299
    if (key != InputAction::SHOW_WINDOWS)
300
        mButtonTexts.push_back(new ButtonText(description, key));
301
}
302
303
void WindowMenu::mousePressed(MouseEvent &event)
304
{
305
    if (popupManager == nullptr)
306
        return;
307
308
    if (event.getButton() == MouseButton::RIGHT)
309
    {
310
        if (mSmallWindow)
311
            return;
312
313
        event.consume();
314
        Button *const btn = dynamic_cast<Button*>(event.getSource());
315
        if (btn == nullptr)
316
            return;
317
        if (popupMenu != nullptr)
318
        {
319
            popupMenu->showPopup(getX() + event.getX(),
320
                getY() + event.getY(), btn);
321
        }
322
    }
323
}
324
325
void WindowMenu::mouseMoved(MouseEvent &event)
326
{
327
    mHaveMouse = true;
328
329
    if (textPopup == nullptr)
330
        return;
331
332
    if (event.getSource() == this)
333
    {
334
        textPopup->hide();
335
        return;
336
    }
337
338
    const Button *const btn = dynamic_cast<const Button *>(
339
        event.getSource());
340
341
    if (btn == nullptr)
342
    {
343
        textPopup->hide();
344
        return;
345
    }
346
347
    const int x = event.getX();
348
    const int y = event.getY();
349
    const InputActionT key = static_cast<InputActionT>(btn->getTag());
350
    const Rect &rect = mDimension;
351
    if (key != InputAction::NO_VALUE)
352
    {
353
        textPopup->show(x + rect.x, y + rect.y, btn->getDescription(),
354
            // TRANSLATORS: short key name
355
            strprintf(_("Key: %s"), inputManager.getKeyValueString(
356
            key).c_str()));
357
    }
358
    else
359
    {
360
        textPopup->show(x + rect.x, y + rect.y, btn->getDescription());
361
    }
362
}
363
364
void WindowMenu::mouseExited(MouseEvent& event A_UNUSED)
365
{
366
    mHaveMouse = false;
367
    if (textPopup == nullptr)
368
        return;
369
370
    textPopup->hide();
371
}
372
373
void WindowMenu::showButton(const std::string &name,
374
                            const Visible visible)
375
{
376
    const ButtonInfo *const info = mButtonNames[name];
377
    if ((info == nullptr) || (info->button == nullptr))
378
        return;
379
380
    info->button->setVisible(visible);
381
    updateButtons();
382
    saveButtons();
383
}
384
385
void WindowMenu::updateButtons()
386
{
387
    int x = mPadding;
388
    int h = 0;
389
    FOR_EACH (STD_VECTOR <Button*>::const_iterator, it, mButtons)
390
        safeRemove(*it);
391
    const int pad2 = 2 * mPadding;
392
    FOR_EACH (STD_VECTOR <Button*>::iterator, it, mButtons)
393
    {
394
        Button *const btn = *it;
395
        if (btn == nullptr)
396
            continue;
397
        if (btn->mVisible == Visible_true)
398
        {
399
            btn->setPosition(x, mPadding);
400
            add(btn);
401
            x += btn->getWidth() + mSpacing;
402
            h = btn->getHeight() + pad2;
403
        }
404
    }
405
    x += mPadding - mSpacing;
406
    if (mainGraphics != nullptr)
407
        setDimension(Rect(mainGraphics->mWidth - x, 0, x, h));
408
}
409
410
void WindowMenu::loadButtons()
411
{
412
    if (!mSmallWindow)
413
    {
414
        if (config.getValue("windowmenu0", "").empty())
415
        {
416
            for (std::map <std::string, ButtonInfo*>::iterator
417
                 it = mButtonNames.begin(),
418
                 it_fend = mButtonNames.end(); it != it_fend; ++it)
419
            {
420
                const ButtonInfo *const info = (*it).second;
421
                if (info == nullptr ||
422
                    info->button == nullptr ||
423
                    info->visible == Visible_true)
424
                {
425
                    continue;
426
                }
427
                info->button->setVisible(Visible_false);
428
            }
429
            updateButtons();
430
            return;
431
        }
432
433
        for (int f = 0; f < 30; f ++)
434
        {
435
            const std::string str = config.getValue(
436
                "windowmenu" + toString(f), "");
437
            if (str.empty() || str == "SET")
438
                continue;
439
            const ButtonInfo *const info = mButtonNames[str];
440
            if ((info == nullptr) || (info->button == nullptr))
441
                continue;
442
            info->button->setVisible(Visible_false);
443
        }
444
    }
445
    else
446
    {
447
        for (std::map <std::string, ButtonInfo*>::iterator
448
             it = mButtonNames.begin(),
449
             it_fend = mButtonNames.end(); it != it_fend; ++it)
450
        {
451
            const ButtonInfo *const info = (*it).second;
452
            if ((info == nullptr) || (info->button == nullptr))
453
                continue;
454
            Button *const button = info->button;
455
            const std::string &str = button->getActionEventId();
456
            button->setVisible(fromBool(
457
                str == "SET" || str == "WIN", Visible));
458
        }
459
    }
460
    updateButtons();
461
}
462
463
void WindowMenu::saveButtons() const
464
{
465
    int i = 0;
466
    FOR_EACH (STD_VECTOR <Button*>::const_iterator, it, mButtons)
467
    {
468
        const Button *const btn = *it;
469
        if ((btn != nullptr) && btn->mVisible == Visible_false)
470
        {
471
            config.setValue("windowmenu" + toString(i),
472
                btn->getActionEventId());
473
            i ++;
474
        }
475
    }
476
    for (int f = i; f < 30; f ++)
477
        config.deleteKey("windowmenu" + toString(f));
478
}
479
480
void WindowMenu::drawChildren(Graphics *const graphics)
481
{
482
    if (mHaveMouse ||
483
        mAutoHide == 0 ||
484
        (mAutoHide == 1 &&
485
        mainGraphics != nullptr &&
486
        (mSmallWindow || mainGraphics->mWidth > 800)))
487
    {
488
        Container::drawChildren(graphics);
489
    }
490
}
491
492
void WindowMenu::safeDrawChildren(Graphics *const graphics)
493
{
494
    if (mHaveMouse ||
495
        mAutoHide == 0 ||
496
        (mAutoHide == 1 &&
497
        mainGraphics != nullptr &&
498
        (mSmallWindow || mainGraphics->mWidth > 800)))
499
    {
500
        Container::safeDrawChildren(graphics);
501
    }
502
}
503
504
void WindowMenu::optionChanged(const std::string &name)
505
{
506
    if (name == "autohideButtons")
507
        mAutoHide = config.getIntValue("autohideButtons");
508

6
}
509
510
#ifdef USE_PROFILER
511
void WindowMenu::logicChildren()
512
{
513
    BLOCK_START("WindowMenu::logicChildren")
514
    BasicContainer::logicChildren();
515
    BLOCK_END("WindowMenu::logicChildren")
516
}
517
#endif  // USE_PROFILER