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-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
25 
26 #include "enums/gui/layouttype.h"
27 
29 
30 #include "gui/models/namesmodel.h"
31 
32 #include "gui/widgets/button.h"
35 #include "gui/widgets/layout.h"
36 #include "gui/widgets/listbox.h"
37 #include "gui/widgets/scrollarea.h"
38 
39 #include "utils/delete2.h"
40 #include "utils/gettext.h"
41 
42 #include "debug.h"
43 
44 TextSelectDialog::TextSelectDialog(const std::string &name,
45  const std::string &selectButton,
46  const AllowQuit allowQuit) :
47  // TRANSLATORS: sell dialog name
48  Window(name, Modal_false, nullptr, "sell.xml"),
51  mSelectButtonName(selectButton),
52  mText(),
53  mSelectButton(nullptr),
54  mQuitButton(nullptr),
55  mItemList(nullptr),
56  mScrollArea(nullptr),
57  mModel(nullptr),
58  mAllowQuit(allowQuit),
59  mTag(0)
60 {
61 }
62 
64 {
66  setWindowName("TextSelectDialog");
67  setResizable(true);
69  setStickyButtonLock(true);
70  setMinWidth(260);
71  setMinHeight(220);
72  setDefaultSize(260, 230, ImagePosition::CENTER, 0, 0);
73 
74  if (setupWindow != nullptr)
76 
77  setActionEventId("OK");
78  mModel = new NamesModel;
80  this,
81  mModel,
82  "listbox.xml");
83  mScrollArea = new ScrollArea(this, mItemList,
84  fromBool(getOptionBool("showbackground", false), Opaque),
85  "sell_background.xml");
87 
88  mSelectButton = new Button(this,
90  "select",
92  this);
94  {
95  mQuitButton = new Button(this,
96  // TRANSLATORS: sell dialog button
97  _("Quit"),
98  "quit",
100  this);
101  }
102 
103  mSelectButton->setEnabled(false);
104 
107  mItemList->setActionEventId("item");
109 
110  ContainerPlacer placer(nullptr, nullptr);
111  placer = getPlacer(0, 0);
112 
113  placer(0, 0, mScrollArea, 8, 5).setPadding(3);
114  if (mQuitButton != nullptr)
115  {
116  placer(6, 5, mSelectButton, 1, 1);
117  placer(7, 5, mQuitButton, 1, 1);
118  }
119  else
120  {
121  placer(7, 5, mSelectButton, 1, 1);
122  }
123 
124  Layout &layout = getLayout();
125  layout.setRowHeight(0, LayoutType::SET);
126 
127  center();
128  loadWindowState();
129 
131  enableVisibleSound(true);
132 }
133 
135 {
136  delete2(mModel)
137 }
138 
140 {
141  const std::string &eventId = event.getId();
142 
143  if (eventId == "quit")
144  {
145  close();
146  return;
147  }
148 
149  const int selectedItem = mItemList->getSelected();
150 
151  // The following actions require a valid item selection
152  if (selectedItem == -1 ||
153  selectedItem >= mModel->getNumberOfElements())
154  {
155  return;
156  }
157  else if (eventId == "select")
158  {
159  const int index = mItemList->getSelected();
160  if (index < 0 || index >= CAST_S32(mModel->size()))
161  return;
162  mText = mModel->getElementAt(index);
164  close();
165  }
166 }
167 
169 {
171 }
172 
174 {
176 }
177 
179 {
180  Window::setVisible(visible);
181 
182  if (visible == Visible_true)
183  {
184  if (mItemList != nullptr)
186  }
187  else
188  {
189  scheduleDelete();
190  }
191 }
192 
193 void TextSelectDialog::addText(const std::string &text)
194 {
195  if (text.empty())
196  return;
197  mModel->add(text);
198 }
bool AllowQuit
Definition: allowquit.h:30
const bool AllowQuit_true
Definition: allowquit.h:30
#define fromBool(val, name)
Definition: booldefines.h:49
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
Definition: button.h:102
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:128
Definition: layout.h:45
void setDistributeMousePressed(const bool b)
Definition: listbox.h:141
int getSelected() const
Definition: listbox.h:168
void addSelectionListener(SelectionListener *const selectionListener)
Definition: listbox.cpp:435
int getNumberOfElements()
Definition: namesmodel.cpp:40
void add(const std::string &str)
Definition: namesmodel.h:53
size_t size() const
Definition: namesmodel.h:47
std::string getElementAt(int i)
Definition: namesmodel.cpp:45
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
void registerWindowForReset(Window *const window)
void valueChanged(const SelectionEvent &event)
ScrollArea * mScrollArea
void addText(const std::string &text)
void setVisible(Visible visible)
void action(const ActionEvent &event)
NamesModel * mModel
std::string mSelectButtonName
TextSelectDialog(const std::string &name, const std::string &selectButton, const AllowQuit allowQuit)
void distributeActionEvent()
Definition: widget.cpp:493
void setEnabled(const bool enabled)
Definition: widget.h:352
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
virtual void requestFocus()
Definition: widget.cpp:204
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
Definition: window.h:102
void center()
Definition: window.cpp:1417
void setResizable(const bool resize)
Definition: window.cpp:627
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1391
virtual void close()
Definition: window.cpp:902
virtual void setVisible(Visible visible)
Definition: window.cpp:778
bool getOptionBool(const std::string &name, const bool def) const
Definition: window.cpp:1466
Layout & getLayout()
Definition: window.cpp:1365
void setWindowName(const std::string &name)
Definition: window.h:355
void setMinHeight(const int height)
Definition: window.cpp:604
void postInit()
Definition: window.cpp:249
void setMinWidth(const int width)
Definition: window.cpp:591
void enableVisibleSound(bool b)
Definition: window.h:481
void setCloseButton(const bool flag)
Definition: window.cpp:749
virtual void scheduleDelete()
Definition: window.cpp:831
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define delete2(var)
Definition: delete2.h:25
#define _(s)
Definition: gettext.h:35
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
const bool Modal_false
Definition: modal.h:30
bool Opaque
Definition: opaque.h:30
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
bool Visible
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30