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