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",
91  this);
93  {
94  mQuitButton = new Button(this,
95  // TRANSLATORS: sell dialog button
96  _("Quit"),
97  "quit",
99  this);
100  }
101 
102  mSelectButton->setEnabled(false);
103 
106  mItemList->setActionEventId("item");
108 
109  ContainerPlacer placer(nullptr, nullptr);
110  placer = getPlacer(0, 0);
111 
112  placer(0, 0, mScrollArea, 8, 5).setPadding(3);
113  if (mQuitButton != nullptr)
114  {
115  placer(6, 5, mSelectButton, 1, 1);
116  placer(7, 5, mQuitButton, 1, 1);
117  }
118  else
119  {
120  placer(7, 5, mSelectButton, 1, 1);
121  }
122 
123  Layout &layout = getLayout();
124  layout.setRowHeight(0, LayoutType::SET);
125 
126  center();
127  loadWindowState();
128 
130  enableVisibleSound(true);
131 }
132 
134 {
135  delete2(mModel);
136 }
137 
139 {
140  const std::string &eventId = event.getId();
141 
142  if (eventId == "quit")
143  {
144  close();
145  return;
146  }
147 
148  const int selectedItem = mItemList->getSelected();
149 
150  // The following actions require a valid item selection
151  if (selectedItem == -1 ||
152  selectedItem >= mModel->getNumberOfElements())
153  {
154  return;
155  }
156  else if (eventId == "select")
157  {
158  const int index = mItemList->getSelected();
159  if (index < 0 || index >= CAST_S32(mModel->size()))
160  return;
161  mText = mModel->getElementAt(index);
163  close();
164  }
165 }
166 
168 {
170 }
171 
173 {
175 }
176 
178 {
179  Window::setVisible(visible);
180 
181  if (visible == Visible_true)
182  {
183  if (mItemList != nullptr)
185  }
186  else
187  {
188  scheduleDelete();
189  }
190 }
191 
192 void TextSelectDialog::addText(const std::string &text)
193 {
194  if (text.empty())
195  return;
196  mModel->add(text);
197 }
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:96
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
const std::string BUTTON_SKIN
Definition: button.h:88
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:151
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)