GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/buyselldialog.cpp Lines: 47 67 70.1 %
Date: 2021-03-17 Branches: 21 52 40.4 %

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-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/buyselldialog.h"
25
26
#include "actormanager.h"
27
28
#include "net/buysellhandler.h"
29
#include "net/npchandler.h"
30
31
#include "gui/windows/setupwindow.h"
32
33
#include "gui/widgets/button.h"
34
35
#include "utils/foreach.h"
36
#include "utils/gettext.h"
37
38
#include "debug.h"
39
40
1
BuySellDialog::DialogList BuySellDialog::dialogInstances;
41
42
1
BuySellDialog::BuySellDialog(const BeingId npcId) :
43
    // TRANSLATORS: shop window name
44
1
    Window(_("Shop"), Modal_false, nullptr, "buysell.xml"),
45
    ActionListener(),
46
    mNpcId(npcId),
47
    mNick(""),
48

10
    mBuyButton(nullptr)
49
{
50
1
    init();
51
1
}
52
53
1
BuySellDialog::BuySellDialog(const std::string &nick) :
54
    // TRANSLATORS: shop window name
55
1
    Window(_("Shop"), Modal_false, nullptr, "buysell.xml"),
56
    ActionListener(),
57
    mNpcId(BeingId_negOne),
58
    mNick(nick),
59

9
    mBuyButton(nullptr)
60
{
61
1
    init();
62
1
}
63
64
2
void BuySellDialog::init()
65
{
66
10
    setWindowName("BuySell");
67
2
    setCloseButton(true);
68
69
2
    if (setupWindow != nullptr)
70
        setupWindow->registerWindowForReset(this);
71
72
    static const char *const buttonNames[] =
73
    {
74
        // TRANSLATORS: shop window button
75
        N_("Buy"),
76
        // TRANSLATORS: shop window button
77
        N_("Sell"),
78
        // TRANSLATORS: shop window button
79
        N_("Cancel"),
80
        nullptr
81
    };
82
8
    const int buttonPadding = getOption("buttonpadding", 10);
83
2
    int x = buttonPadding;
84
2
    const int y = buttonPadding;
85
86
8
    for (const char *const *curBtn = buttonNames;
87
8
         *curBtn != nullptr;
88
         curBtn++)
89
    {
90
        Button *const btn = new Button(this,
91
6
            gettext(*curBtn),
92
            *curBtn,
93
            BUTTON_SKIN,
94

42
            this);
95
6
        if (mBuyButton == nullptr)
96
2
            mBuyButton = btn;  // For focus request
97
6
        btn->setPosition(x, y);
98
6
        add(btn);
99
12
        x += btn->getWidth() + buttonPadding;
100
    }
101
2
    if (mBuyButton != nullptr)
102
    {
103
2
        mBuyButton->requestFocus();
104
4
        setContentSize(x, 2 * y + mBuyButton->getHeight());
105
    }
106
107
2
    center();
108
2
    setDefaultSize();
109
2
    loadWindowState();
110
4
    enableVisibleSound(true);
111
112
4
    dialogInstances.push_back(this);
113
2
    setVisible(Visible_true);
114
2
}
115
116
10
BuySellDialog::~BuySellDialog()
117
{
118
2
    dialogInstances.remove(this);
119
4
}
120
121
2
void BuySellDialog::setVisible(Visible visible)
122
{
123
2
    Window::setVisible(visible);
124
125
2
    if (visible == Visible_true)
126
    {
127
2
        if (mBuyButton != nullptr)
128
2
            mBuyButton->requestFocus();
129
    }
130
    else
131
    {
132
        scheduleDelete();
133
    }
134
2
}
135
136
void BuySellDialog::action(const ActionEvent &event)
137
{
138
    const std::string &eventId = event.getId();
139
    if (eventId == "Buy")
140
    {
141
        if (mNpcId != BeingId_negOne)
142
        {
143
            const Being *const being = actorManager->findBeing(mNpcId);
144
            if (being != nullptr)
145
                npcHandler->buy(being);
146
            else
147
                npcHandler->buy(mNpcId);
148
        }
149
        else
150
        {
151
            buySellHandler->requestSellList(mNick);
152
        }
153
    }
154
    else if (eventId == "Sell")
155
    {
156
        if (mNpcId != BeingId_negOne)
157
            npcHandler->sell(mNpcId);
158
        else
159
            buySellHandler->requestBuyList(mNick);
160
    }
161
162
    close();
163
}
164
165
void BuySellDialog::closeAll()
166
{
167
    FOR_EACH (DialogList::const_iterator, it, dialogInstances)
168
    {
169
        if (*it != nullptr)
170
            (*it)->close();
171
    }
172

3
}