ManaPlus
textselectdialog.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 "enums/gui/layouttype.h"
26 
28 
29 #include "gui/models/namesmodel.h"
30 
31 #include "gui/widgets/button.h"
34 #include "gui/widgets/layout.h"
35 #include "gui/widgets/listbox.h"
36 #include "gui/widgets/scrollarea.h"
37 
38 #include "utils/delete2.h"
39 #include "utils/gettext.h"
40 
41 #include "debug.h"
42 
43 TextSelectDialog::TextSelectDialog(const std::string &name,
44  const std::string &selectButton,
45  const AllowQuit allowQuit) :
46  // TRANSLATORS: sell dialog name
47  Window(name, Modal_false, nullptr, "sell.xml"),
50  mSelectButtonName(selectButton),
51  mText(),
52  mSelectButton(nullptr),
53  mQuitButton(nullptr),
54  mItemList(nullptr),
55  mScrollArea(nullptr),
56  mModel(nullptr),
57  mAllowQuit(allowQuit),
58  mTag(0)
59 {
60 }
61 
63 {
65  setWindowName("TextSelectDialog");
66  setResizable(true);
68  setStickyButtonLock(true);
69  setMinWidth(260);
70  setMinHeight(220);
71  setDefaultSize(260, 230, ImagePosition::CENTER, 0, 0);
72 
73  if (setupWindow != nullptr)
75 
76  setActionEventId("OK");
77  mModel = new NamesModel;
79  this,
80  mModel,
81  "listbox.xml");
82  mScrollArea = new ScrollArea(this, mItemList,
83  fromBool(getOptionBool("showbackground", false), Opaque),
84  "sell_background.xml");
86 
87  mSelectButton = new Button(this,
89  "select",
90  this);
92  {
93  // TRANSLATORS: sell dialog button
94  mQuitButton = new Button(this, _("Quit"), "quit", this);
95  }
96 
97  mSelectButton->setEnabled(false);
98 
101  mItemList->setActionEventId("item");
103 
104  ContainerPlacer placer(nullptr, nullptr);
105  placer = getPlacer(0, 0);
106 
107  placer(0, 0, mScrollArea, 8, 5).setPadding(3);
108  if (mQuitButton != nullptr)
109  {
110  placer(6, 5, mSelectButton, 1, 1);
111  placer(7, 5, mQuitButton, 1, 1);
112  }
113  else
114  {
115  placer(7, 5, mSelectButton, 1, 1);
116  }
117 
118  Layout &layout = getLayout();
119  layout.setRowHeight(0, LayoutType::SET);
120 
121  center();
122  loadWindowState();
123 
125  enableVisibleSound(true);
126 }
127 
129 {
130  delete2(mModel);
131 }
132 
134 {
135  const std::string &eventId = event.getId();
136 
137  if (eventId == "quit")
138  {
139  close();
140  return;
141  }
142 
143  const int selectedItem = mItemList->getSelected();
144 
145  // The following actions require a valid item selection
146  if (selectedItem == -1 ||
147  selectedItem >= mModel->getNumberOfElements())
148  {
149  return;
150  }
151  else if (eventId == "select")
152  {
153  const int index = mItemList->getSelected();
154  if (index < 0 || index >= CAST_S32(mModel->size()))
155  return;
156  mText = mModel->getElementAt(index);
158  close();
159  }
160 }
161 
163 {
165 }
166 
168 {
170 }
171 
173 {
174  Window::setVisible(visible);
175 
176  if (visible == Visible_true)
177  {
178  if (mItemList != nullptr)
180  }
181  else
182  {
183  scheduleDelete();
184  }
185 }
186 
187 void TextSelectDialog::addText(const std::string &text)
188 {
189  if (text.empty())
190  return;
191  mModel->add(text);
192 }
void setDefaultSize()
Definition: window.cpp:1193
#define _(s)
Definition: gettext.h:34
bool getOptionBool(const std::string &name, const bool def) const
Definition: window.cpp:1461
std::string getElementAt(int i)
Definition: namesmodel.cpp:44
const bool Visible_true
Definition: visible.h:29
void loadWindowState()
Definition: window.cpp:1082
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:251
void addSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:434
void setMinWidth(const int width)
Definition: window.cpp:586
Definition: window.h:98
Definition: button.h:94
void setDistributeMousePressed(const bool b)
Definition: listbox.h:140
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:595
#define delete2(var)
Definition: delete2.h:24
std::string mSelectButtonName
void registerWindowForReset(Window *const window)
void add(const std::string &str)
Definition: namesmodel.h:52
TextSelectDialog(const std::string &name, const std::string &selectButton, const AllowQuit allowQuit)
void distributeActionEvent()
Definition: widget.cpp:492
virtual void close()
Definition: window.cpp:897
const bool AllowQuit_true
Definition: allowquit.h:29
#define CAST_S32
Definition: cast.h:29
virtual void requestFocus()
Definition: widget.cpp:203
void addText(const std::string &text)
#define fromBool(val, name)
Definition: booldefines.h:48
void action(const ActionEvent &event)
int getSelected() const
Definition: listbox.h:167
#define nullptr
Definition: localconsts.h:44
void setCloseButton(const bool flag)
Definition: window.cpp:744
void enableVisibleSound(bool b)
Definition: window.h:480
void setMinHeight(const int height)
Definition: window.cpp:599
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1386
void setEnabled(const bool enabled)
Definition: widget.h:351
virtual void scheduleDelete()
Definition: window.cpp:826
size_t size() const
Definition: namesmodel.h:46
Layout & getLayout()
Definition: window.cpp:1360
ScrollArea * mScrollArea
int getNumberOfElements()
Definition: namesmodel.cpp:39
#define A_UNUSED
Definition: localconsts.h:171
virtual void setVisible(Visible visible)
Definition: window.cpp:773
void center()
Definition: window.cpp:1412
void valueChanged(const SelectionEvent &event)
void setResizable(const bool resize)
Definition: window.cpp:622
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:767
SetupWindow * setupWindow
Definition: setupwindow.cpp:63
void setVisible(Visible visible)
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
Definition: layout.h:43
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:127
void postInit()
Definition: window.cpp:248
NamesModel * mModel
void setWindowName(const std::string &name)
Definition: window.h:354
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)