ManaPlus
popuplist.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2012-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "gui/widgets/popuplist.h"
23 
24 #include "gui/gui.h"
25 
27 #include "gui/widgets/dropdown.h"
29 #include "gui/widgets/scrollarea.h"
30 
31 #include "render/graphics.h"
32 
33 #include "debug.h"
34 
36  ListModel *const listModel,
37  const bool isExtended,
38  const Modal modal) :
39  Popup("PopupList", "popuplist.xml"),
40  FocusListener(),
41  mListModel(listModel),
42  mListBox(isExtended ? CREATEWIDGETR(ExtendedListBox,
43  widget, listModel, "extendedlistbox.xml", 0) :
45  widget, listModel, "popuplistbox.xml")),
46  mScrollArea(new ScrollArea(this, mListBox, Opaque_false, std::string())),
47  mDropDown(widget),
48  mPressedIndex(-2),
49  mModal(modal)
50 {
51  mListBox->setMouseConsume(false);
53  mAllowLogic = false;
54  setFocusable(true);
55 
58 }
59 
61 {
64 
65  if (gui != nullptr)
67 
69  addMouseListener(this);
70  adjustSize();
71 }
72 
74 {
75  if (mParent != nullptr)
77  if (gui != nullptr)
80 }
81 
82 void PopupList::show(int x, int y)
83 {
84  int len = mListBox->getHeight() + 8;
85  if (len > 250)
86  len = 250;
87  setContentSize(mListBox->getWidth() + 8, len);
88  const int width = mDimension.width;
89  const int height = mDimension.height;
90  if (mainGraphics->mWidth < (x + width + 5))
91  x = mainGraphics->mWidth - width;
92  if (mainGraphics->mHeight < (y + height + 5))
93  y = mainGraphics->mHeight - height;
94  setPosition(x, y);
97  if (mModal == Modal_true)
99 }
100 
101 void PopupList::widgetResized(const Event &event)
102 {
103  Popup::widgetResized(event);
104  adjustSize();
105 }
106 
107 void PopupList::setSelected(const int selected)
108 {
109  if (mListBox == nullptr)
110  return;
111 
112  mListBox->setSelected(selected);
113 }
114 
116 {
117  if (mListBox == nullptr)
118  return -1;
119 
120  return mListBox->getSelected();
121 }
122 
124 {
125  if (mListBox != nullptr)
126  mListBox->setListModel(model);
127  mListModel = model;
128 }
129 
131 {
132  const int pad2 = 2 * mPadding;
133  const int width = mDimension.width - pad2;
134  mScrollArea->setWidth(width);
136  mListBox->adjustSize();
137  mListBox->setWidth(width);
138 }
139 
141 {
143  event.getY() + mPadding);
144  event.consume();
145 }
146 
148 {
150  event.getY() + mPadding))
151  {
152  mPressedIndex = -2;
153  return;
154  }
155 
156  mPressedIndex = -2;
157  if (event.getSource() == mScrollArea)
158  return;
159  if (mDropDown != nullptr)
162  if (mModal == Modal_true)
164 }
165 
166 void PopupList::focusGained(const Event& event)
167 {
168  const Widget *const source = event.getSource();
169  if (mVisible == Visible_false ||
170  source == this ||
171  source == mListBox ||
172  source == mScrollArea ||
173  source == mDropDown)
174  {
175  return;
176  }
177 
178  if (mDropDown != nullptr)
181  if (mModal == Modal_true)
183 }
184 
186 {
187  if (mDropDown != nullptr)
189 }
virtual void add(Widget *const widget)
void updateSelection()
Definition: dropdown.cpp:557
Definition: event.h:79
Widget * getSource() const
Definition: event.h:104
int mWidth
Definition: graphics.h:484
int mHeight
Definition: graphics.h:485
void removeGlobalFocusListener(FocusListener *focusListener)
Definition: gui.cpp:1147
void addGlobalFocusListener(FocusListener *focusListener)
Definition: gui.cpp:1142
void setDistributeMousePressed(const bool b)
Definition: listbox.h:141
void setListModel(ListModel *listModel)
Definition: listbox.cpp:428
virtual void adjustSize()
Definition: listbox.cpp:374
int getSelected() const
Definition: listbox.h:168
void setSelected(const int selected)
Definition: listbox.cpp:399
virtual int getSelectionByMouse(const int y) const
Definition: listbox.cpp:392
int getY() const
Definition: mouseevent.h:138
void widgetResized(const Event &event)
Definition: popuplist.cpp:101
PopupList(DropDown *const widget, ListModel *const listModel, const bool extended, const Modal modal)
Definition: popuplist.cpp:35
ListBox * mListBox
Definition: popuplist.h:78
ScrollArea * mScrollArea
Definition: popuplist.h:79
void setSelected(const int selected)
Definition: popuplist.cpp:107
int getSelected() const
Definition: popuplist.cpp:115
void setListModel(ListModel *const model)
Definition: popuplist.cpp:123
void adjustSize()
Definition: popuplist.cpp:130
void focusLost(const Event &event)
Definition: popuplist.cpp:185
int mPressedIndex
Definition: popuplist.h:81
void focusGained(const Event &event)
Definition: popuplist.cpp:166
void postInit()
Definition: popuplist.cpp:60
void mouseReleased(MouseEvent &event)
Definition: popuplist.cpp:147
ListModel * mListModel
Definition: popuplist.h:77
Modal mModal
Definition: popuplist.h:82
DropDown * mDropDown
Definition: popuplist.h:80
void mousePressed(MouseEvent &event)
Definition: popuplist.cpp:140
void show(int x, int y)
Definition: popuplist.cpp:82
Definition: popup.h:52
void setContentSize(int width, int height)
Definition: popup.cpp:155
void widgetResized(const Event &event)
Definition: popup.cpp:271
int mPadding
Definition: popup.h:181
void postInit()
Definition: popup.h:177
int width
Definition: rect.h:219
int height
Definition: rect.h:224
void setWidth(int width)
void setHeight(int height)
Definition: widget.h:99
void setVisible(Visible visible)
Definition: widget.cpp:225
void setFocusable(const bool focusable)
Definition: widget.cpp:192
void setWidth(const int width)
Definition: widget.cpp:133
void setMouseConsume(const bool b)
Definition: widget.h:936
virtual void requestMoveToTop()
Definition: widget.cpp:213
Widget * mParent
Definition: widget.h:1128
Rect mDimension
Definition: widget.h:1101
void removeKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:277
bool mAllowLogic
Definition: widget.h:1160
void requestModalFocus()
Definition: widget.cpp:380
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:272
Visible mVisible
Definition: widget.h:963
void setPosition(const int x, const int y)
Definition: widget.cpp:161
virtual void releaseModalFocus()
Definition: widget.cpp:394
void removeFocusListener(FocusListener *const focusListener)
Definition: widget.cpp:287
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define new
Definition: debug_new.h:147
Graphics * mainGraphics
Definition: graphics.cpp:109
Gui * gui
Definition: gui.cpp:111
#define A_UNUSED
Definition: localconsts.h:160
bool Modal
Definition: modal.h:30
const bool Modal_true
Definition: modal.h:30
const bool Opaque_false
Definition: opaque.h:30
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30