ManaPlus
buyselldialog.cpp
Go to the documentation of this file.
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 
24 
25 #include "actormanager.h"
26 
27 #include "net/buysellhandler.h"
28 #include "net/npchandler.h"
29 
31 
32 #include "gui/widgets/button.h"
33 
34 #include "utils/foreach.h"
35 #include "utils/gettext.h"
36 
37 #include "debug.h"
38 
40 
42  // TRANSLATORS: shop window name
43  Window(_("Shop"), Modal_false, nullptr, "buysell.xml"),
45  mNpcId(npcId),
46  mNick(""),
47  mBuyButton(nullptr)
48 {
49  init();
50 }
51 
52 BuySellDialog::BuySellDialog(const std::string &nick) :
53  // TRANSLATORS: shop window name
54  Window(_("Shop"), Modal_false, nullptr, "buysell.xml"),
57  mNick(nick),
59 {
60  init();
61 }
62 
64 {
65  setWindowName("BuySell");
66  setCloseButton(true);
67 
68  if (setupWindow != nullptr)
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  const int buttonPadding = getOption("buttonpadding", 10);
82  int x = buttonPadding;
83  const int y = buttonPadding;
84 
85  for (const char *const *curBtn = buttonNames;
86  *curBtn != nullptr;
87  curBtn++)
88  {
89  Button *const btn = new Button(this,
90  gettext(*curBtn),
91  *curBtn,
93  this);
94  if (mBuyButton == nullptr)
95  mBuyButton = btn; // For focus request
96  btn->setPosition(x, y);
97  add(btn);
98  x += btn->getWidth() + buttonPadding;
99  }
100  if (mBuyButton != nullptr)
101  {
103  setContentSize(x, 2 * y + mBuyButton->getHeight());
104  }
105 
106  center();
107  setDefaultSize();
108  loadWindowState();
109  enableVisibleSound(true);
110 
111  dialogInstances.push_back(this);
113 }
114 
116 {
117  dialogInstances.remove(this);
118 }
119 
121 {
122  Window::setVisible(visible);
123 
124  if (visible == Visible_true)
125  {
126  if (mBuyButton != nullptr)
128  }
129  else
130  {
131  scheduleDelete();
132  }
133 }
134 
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
147  }
148  else
149  {
151  }
152  }
153  else if (eventId == "Sell")
154  {
155  if (mNpcId != BeingId_negOne)
157  else
159  }
160 
161  close();
162 }
163 
165 {
166  FOR_EACH (DialogList::const_iterator, it, dialogInstances)
167  {
168  if (*it != nullptr)
169  (*it)->close();
170  }
171 }
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
void setDefaultSize()
Definition: window.cpp:1197
#define _(s)
Definition: gettext.h:34
int getWidth() const
Definition: widget.h:220
const bool Visible_true
Definition: visible.h:29
virtual void buy(const Being *const being) const =0
#define N_(s)
Definition: gettext.h:35
void loadWindowState()
Definition: window.cpp:1086
Definition: window.h:98
Definition: button.h:96
int BeingId
Definition: beingid.h:29
int getOption(const std::string &name, const int def) const
Definition: window.cpp:1453
Button * mBuyButton
Definition: buyselldialog.h:83
std::list< BuySellDialog * > DialogList
Definition: buyselldialog.h:78
static void closeAll()
const BeingId BeingId_negOne
Definition: beingid.h:30
void registerWindowForReset(Window *const window)
void setVisible(Visible visible)
virtual void close()
Definition: window.cpp:901
void setContentSize(int width, int height)
Definition: window.cpp:491
virtual void requestSellList(const std::string &nick) const =0
virtual void requestFocus()
Definition: widget.cpp:203
const std::string BUTTON_SKIN
Definition: button.h:88
std::string mNick
Definition: buyselldialog.h:82
virtual void add(Widget *const widget)
Being * findBeing(const BeingId id) const
#define nullptr
Definition: localconsts.h:44
BeingId mNpcId
Definition: buyselldialog.h:81
void setPosition(const int x, const int y)
Definition: widget.cpp:160
void setCloseButton(const bool flag)
Definition: window.cpp:748
void enableVisibleSound(bool b)
Definition: window.h:480
virtual void scheduleDelete()
Definition: window.cpp:830
virtual void requestBuyList(const std::string &nick) const =0
static DialogList dialogInstances
Definition: buyselldialog.h:79
virtual void setVisible(Visible visible)
Definition: window.cpp:777
void center()
Definition: window.cpp:1416
void action(const ActionEvent &event)
Net::BuySellHandler * buySellHandler
Definition: net.cpp:97
int getHeight() const
Definition: widget.h:239
Net::NpcHandler * npcHandler
Definition: net.cpp:90
SetupWindow * setupWindow
Definition: setupwindow.cpp:63
Definition: being.h:93
virtual void sell(const BeingId beingId) const =0
BuySellDialog(const BeingId npcId)
ActorManager * actorManager
void setWindowName(const std::string &name)
Definition: window.h:354