ManaPlus
shortcutwindow.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2007-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 #include "gui/widgets/tabbedarea.h"
29 
30 #include "gui/widgets/button.h"
32 #include "gui/widgets/layout.h"
33 #include "gui/widgets/scrollarea.h"
35 
37 
38 #include "utils/delete2.h"
39 #include "utils/foreach.h"
40 
41 #include "debug.h"
42 
47 static const int SCROLL_PADDING = 0;
48 
50 
51 ShortcutWindow::ShortcutWindow(const std::string &restrict title,
52  ShortcutContainer *restrict const content,
53  const std::string &restrict skinFile,
54  int width, int height) :
55  Window("Window", Modal_false, nullptr, skinFile),
56  mItems(content),
57  mScrollArea(new ScrollArea(this, mItems, Opaque_false, std::string())),
58  mTabs(nullptr),
59  mPages(),
60  mButtonIndex(0)
61 {
62  setWindowName(title);
64 
65  setShowTitle(false);
66  setResizable(true);
67  setDefaultVisible(false);
68  setSaveVisible(true);
69  setAllowClose(true);
70 
71  mDragOffsetX = 0;
72  mDragOffsetY = 0;
73 
74  if (content != nullptr)
75  content->setSkin(this, mSkin);
76  if (setupWindow != nullptr)
78 
79  setMinWidth(32);
80  setMinHeight(32);
81  const int border = SCROLL_PADDING * 2 + getPadding() * 2;
82  if (mItems != nullptr)
83  {
84  const int bw = mItems->getBoxWidth();
85  const int bh = mItems->getBoxHeight();
86  const int maxItems = mItems->getMaxItems();
87  setMaxWidth(bw * maxItems + border);
88  setMaxHeight(bh * maxItems + border);
89 
90  if (width == 0)
91  width = bw + border;
92  if (height == 0)
93  height = bh * maxItems + border;
94 
95  setDefaultSize(width, height, ImagePosition::LOWER_RIGHT, 0, 0);
96 
97  mBoxesWidth += bw + border;
98  }
99 
102 
103  place(0, 0, mScrollArea, 5, 5).setPadding(0);
104 
105  Layout &layout = getLayout();
106  layout.setRowHeight(0, LayoutType::SET);
107  layout.setMargin(0);
108 
109  loadWindowState();
110  enableVisibleSound(true);
111 }
112 
113 ShortcutWindow::ShortcutWindow(const std::string &restrict title,
114  const std::string &restrict skinFile,
115  const int width, const int height) :
116  Window("Window", Modal_false, nullptr, skinFile),
117  mItems(nullptr),
120  mPages(),
121  mButtonIndex(0)
122 {
123  setWindowName(title);
125  setShowTitle(false);
126  setResizable(true);
127  setDefaultVisible(false);
128  setSaveVisible(true);
129  setAllowClose(true);
130 
131  mDragOffsetX = 0;
132  mDragOffsetY = 0;
133 
134  if (setupWindow != nullptr)
136 
137  if ((width != 0) && (height != 0))
138  setDefaultSize(width, height, ImagePosition::LOWER_RIGHT, 0, 0);
139 
140  setMinWidth(32);
141  setMinHeight(32);
142 
143  place(0, 0, mTabs, 5, 5);
144 
145  Layout &layout = getLayout();
146  layout.setRowHeight(0, LayoutType::SET);
147  layout.setMargin(0);
148 
149  loadWindowState();
150  enableVisibleSound(true);
151 }
152 
154 {
155  if (mTabs != nullptr)
156  mTabs->removeAll(true);
157  delete2(mTabs)
158  delete2(mItems)
159 }
160 
161 void ShortcutWindow::addButton(const std::string &text,
162  const std::string &eventName,
163  ActionListener *const listener)
164 {
166  5,
167  new Button(this, text, eventName, BUTTON_SKIN, listener),
168  1,
169  1);
170  Window::widgetResized(Event(nullptr));
171 }
172 
173 void ShortcutWindow::addTab(const std::string &name,
174  ShortcutContainer *const content)
175 {
176  if ((content == nullptr) || (mTabs == nullptr))
177  return;
178  ScrollArea *const scroll = new ScrollArea(this,
179  content,
180  Opaque_false,
181  std::string());
184  content->setSkin(this, mSkin);
185  Tab *const tab = new ShortcutTab(this, name, content);
186  mTabs->addTab(tab, scroll);
187  mPages.push_back(content);
188 }
189 
191 {
192  if (mTabs == nullptr)
193  return 0;
194  return mTabs->getSelectedTabIndex();
195 }
196 
198 {
199  Window::widgetHidden(event);
200  if (mItems != nullptr)
201  mItems->widgetHidden(event);
202  if (mTabs != nullptr)
203  {
204  ScrollArea *const scroll = static_cast<ScrollArea *>(
206  if (scroll != nullptr)
207  {
208  ShortcutContainer *const content = static_cast<ShortcutContainer*>(
209  scroll->getContent());
210 
211  if (content != nullptr)
212  content->widgetHidden(event);
213  }
214  }
215 }
216 
218 {
219  Window::mousePressed(event);
220 
221  if (event.isConsumed())
222  return;
223 
224  if (event.getButton() == MouseButton::LEFT)
225  {
226  mDragOffsetX = event.getX();
227  mDragOffsetY = event.getY();
228  }
229 }
230 
232 {
233  Window::mouseDragged(event);
234 
235  if (event.isConsumed())
236  return;
237 
238  if (canMove() && isMovable() && mMoved)
239  {
240  int newX = std::max(0, getX() + event.getX() - mDragOffsetX);
241  int newY = std::max(0, getY() + event.getY() - mDragOffsetY);
242  newX = std::min(mainGraphics->mWidth - getWidth(), newX);
243  newY = std::min(mainGraphics->mHeight - getHeight(), newY);
244  setPosition(newX, newY);
245  }
246 }
247 
249 {
250  Window::widgetMoved(event);
251  if (mItems != nullptr)
252  mItems->setRedraw(true);
253  FOR_EACH (STD_VECTOR<ShortcutContainer*>::iterator, it, mPages)
254  (*it)->setRedraw(true);
255 }
256 
258 {
259  if (mTabs != nullptr)
260  mTabs->selectNextTab();
261 }
262 
264 {
265  if (mTabs != nullptr)
266  mTabs->selectPrevTab();
267 }
268 
269 #ifdef USE_PROFILER
271 {
272  BLOCK_START("ShortcutWindow::logicChildren")
274  BLOCK_END("ShortcutWindow::logicChildren")
275 }
276 #endif // USE_PROFILER
void addButton(const std::string &text, const std::string &eventName, ActionListener *const listener)
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
void setDefaultSize()
Definition: window.cpp:1197
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1383
std::vector< ShortcutContainer * > mPages
void removeAll(const bool del)
Definition: tabbedarea.cpp:729
void setDefaultVisible(const bool save)
Definition: window.h:287
int getWidth() const
Definition: widget.h:220
Skin * mSkin
Definition: window.h:648
int mDragOffsetY
Definition: window.h:641
Definition: tab.h:84
void setMaxWidth(const int width)
Definition: window.cpp:616
void loadWindowState()
Definition: window.cpp:1086
void setRedraw(const bool b)
Definition: widget.h:941
int getY() const
Definition: widget.h:287
void setMaxHeight(const int height)
Definition: window.cpp:621
MouseButtonT getButton() const
Definition: mouseevent.h:115
void setMinWidth(const int width)
Definition: window.cpp:590
int getBoxWidth() const
Definition: window.h:98
Widget * getCurrentWidget() const
Definition: tabbedarea.cpp:228
int mWidth
Definition: graphics.h:483
#define BLOCK_START(name)
Definition: perfomance.h:78
Definition: button.h:96
void setShowTitle(bool flag)
Definition: window.h:234
#define BLOCK_END(name)
Definition: perfomance.h:79
bool mMoved
Definition: window.h:646
static int mBoxesWidth
#define delete2(var)
Definition: delete2.h:24
int mDragOffsetX
Definition: window.h:634
bool isMovable() const
Definition: window.h:575
void registerWindowForReset(Window *const window)
ShortcutWindow * dropShortcutWindow
void widgetHidden(const Event &event)
Definition: window.cpp:724
#define new
Definition: debug_new.h:147
ScrollArea * mScrollArea
void widgetResized(const Event &event)
Definition: window.cpp:654
int getTitlePadding() const
Definition: window.h:601
const std::string BUTTON_SKIN
Definition: button.h:88
void widgetHidden(const Event &event)
int getTabIndex() const
int getMaxItems() const
int getBoxHeight() const
bool canMove() const
Definition: window.cpp:976
virtual void setSkin(const Widget2 *const widget, Skin *const skin)
int getSelectedTabIndex() const
Definition: tabbedarea.cpp:448
virtual void logicChildren()
void setMargin(int m)
Definition: layout.h:53
#define nullptr
Definition: localconsts.h:44
Graphics * mainGraphics
Definition: graphics.cpp:108
const bool Opaque_false
Definition: opaque.h:29
ShortcutWindow * itemShortcutWindow
void setPosition(const int x, const int y)
Definition: widget.cpp:160
ShortcutWindow * spellShortcutWindow
int mHeight
Definition: graphics.h:484
bool isConsumed() const
void enableVisibleSound(bool b)
Definition: window.h:480
void setMinHeight(const int height)
Definition: window.cpp:603
LayoutCell & setPadding(int p)
Definition: layoutcell.h:59
Layout & getLayout()
Definition: window.cpp:1364
void widgetMoved(const Event &event)
Definition: window.cpp:719
ShortcutWindow * emoteShortcutWindow
void selectPrevTab()
Definition: tabbedarea.cpp:827
void setAllowClose(const bool b)
Definition: window.h:188
int getY() const
Definition: mouseevent.h:137
void setSaveVisible(const bool save)
Definition: window.h:299
std::vector< std::string > mPages
Definition: statdb.cpp:40
int getX() const
Definition: widget.h:268
ShortcutContainer * mItems
TabbedArea * mTabs
Definition: event.h:77
void mouseDragged(MouseEvent &event)
Definition: window.cpp:981
void mousePressed(MouseEvent &event)
Definition: window.cpp:835
int getHeight() const
Definition: widget.h:239
virtual void widgetHidden(const Event &event)
#define restrict
Definition: localconsts.h:164
void setResizable(const bool resize)
Definition: window.cpp:626
int getX() const
Definition: mouseevent.h:126
SetupWindow * setupWindow
Definition: setupwindow.cpp:63
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:35
void setTitleBarHeight(unsigned int height)
Definition: window.h:512
void mouseDragged(MouseEvent &event)
Definition: layout.h:43
void setRowHeight(const int n, const int h)
Definition: layoutcell.cpp:127
void widgetMoved(const Event &event)
int getPadding() const
Definition: window.h:503
void mousePressed(MouseEvent &event)
void addTab(const std::string &name, ShortcutContainer *const content)
void selectNextTab()
Definition: tabbedarea.cpp:818
void addTab(Tab *const tab, Widget *const widget)
Definition: tabbedarea.cpp:237
void setWindowName(const std::string &name)
Definition: window.h:354
static const int SCROLL_PADDING
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
Widget * getContent()