GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/buyselldialog.cpp Lines: 46 66 69.7 %
Date: 2017-11-29 Branches: 26 62 41.9 %

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-2017  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
2
BuySellDialog::DialogList BuySellDialog::dialogInstances;
40
41
2
BuySellDialog::BuySellDialog(const BeingId npcId) :
42
    // TRANSLATORS: shop window name
43
2
    Window(_("Shop"), Modal_false, nullptr, "buysell.xml"),
44
    ActionListener(),
45
    mNpcId(npcId),
46
    mNick(""),
47


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

18
    mBuyButton(nullptr)
59
{
60
2
    init();
61
2
}
62
63
4
void BuySellDialog::init()
64
{
65
20
    setWindowName("BuySell");
66
4
    setCloseButton(true);
67
68
4
    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

16
    const int buttonPadding = getOption("buttonpadding", 10);
82
4
    int x = buttonPadding;
83
4
    const int y = buttonPadding;
84
85
16
    for (const char *const *curBtn = buttonNames;
86
16
         *curBtn != nullptr;
87
         curBtn++)
88
    {
89


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

6
}