GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/buyselldialog.cpp Lines: 47 67 70.1 %
Date: 2018-09-20 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-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/windows/buyselldialog.h"
24
25
#include "actormanager.h"
26
27
#include "net/buysellhandler.h"
28
#include "net/npchandler.h"
29
30
#include "gui/windows/setupwindow.h"
31
32
#include "gui/widgets/button.h"
33
34
#include "utils/foreach.h"
35
#include "utils/gettext.h"
36
37
#include "debug.h"
38
39
1
BuySellDialog::DialogList BuySellDialog::dialogInstances;
40
41
1
BuySellDialog::BuySellDialog(const BeingId npcId) :
42
    // TRANSLATORS: shop window name
43
1
    Window(_("Shop"), Modal_false, nullptr, "buysell.xml"),
44
    ActionListener(),
45
    mNpcId(npcId),
46
    mNick(""),
47

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

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

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

3
}