FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
panel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE:: *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <cassert>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
33 #include "util/base/exception.h"
34 
35 #include "dockarea.h"
36 #include "panel.h"
37 
38 namespace fcn {
40  m_dockable(true),
41  m_docked(false) {
42  }
43 
44  Panel::Panel(bool dockable):
45  m_dockable(dockable),
46  m_docked(false) {
47  }
48 
50  }
51 
52 
53  void Panel::setDockable(bool dockable) {
54  m_dockable = dockable;
55  }
56 
57  bool Panel::isDockable() const {
58  return m_dockable;
59  }
60 
61  void Panel::setDocked(bool docked) {
62  if (isDockable()) {
63  if (docked && !isDocked()) {
64  // save settings
65  m_state.dimension = getDimension();
66  //m_state.innerBorder = getInnerBorderSize();
68  m_state.movable = isMovable();
69  setResizable(false);
70  m_docked = true;
71  getDockedArea()->adaptLayout(false);
72  //resizeToContent(true);
73  //expandContent(true);
74  } else if (!docked && isDocked()) {
75  m_docked = false;
76  // restore settings
77  //setDimension(m_state.dimension);
78  setSize(m_state.dimension.width, m_state.dimension.height);
79  //setInnerBorderSize(m_state.innerBorder);
80  // center it
81  Rectangle childrenRec = getParent()->getChildrenArea();
82  int32_t x = childrenRec.x + childrenRec.width / 2 - getWidth() / 2;
83  int32_t y = childrenRec.y + childrenRec.height / 2 - getHeight() / 2;
84  setPosition(x, y);
86  setMovable(m_state.movable);
87  }
88  }
89  }
90 
91  bool Panel::isDocked() const {
92  return m_docked;
93  }
94 
96  DockArea* dockedArea = NULL;
97  if (isDocked()) {
98  dockedArea = dynamic_cast<DockArea*>(getParent());
99  }
100  return dockedArea;
101  }
102 
104  DockArea* dockArea = NULL;
105  if (!isDocked()) {
106  if (getParent()) {
107  DockArea* newDockArea = NULL;
108  std::list<Widget*> widgets = getParent()->getWidgetsIn(getDimension(), this);
109  if (widgets.size() > 0) {
110  std::list<Widget*>::iterator it = widgets.begin();
111  for (; it != widgets.end(); ++it) {
112  // check if the Widget is a DockArea
113  DockArea* tmp = dynamic_cast<DockArea*>(*it);
114  if (!tmp) {
115  // check if the Widget contains a DockArea
116  std::list<Widget*> deepWidgets = (*it)->getWidgetsIn((*it)->getChildrenArea());
117  if (deepWidgets.size() > 0) {
118  Rectangle dim = getDimension();
119  getAbsolutePosition(dim.x, dim.y);
120  std::list<Widget*>::iterator dit = deepWidgets.begin();
121  for (; dit != deepWidgets.end(); ++dit) {
122  tmp = dynamic_cast<DockArea*>(*dit);
123  if (tmp) {
124  Rectangle tdim = (*dit)->getDimension();
125  (*dit)->getAbsolutePosition(tdim.x, tdim.y);
126  if (dim.isIntersecting(tdim)) {
127  break;
128  }
129  tmp = NULL;
130  }
131  }
132  }
133  if (!tmp) {
134  continue;
135  }
136  }
137  newDockArea = tmp;
138  break;
139  }
140  }
141  dockArea = newDockArea;
142  }
143  }
144  return dockArea;
145  }
146 
147  void Panel::resizeToContent(bool recursiv) {
148  if (!m_docked) {
150  } else {
151  Window::resizeToContent(recursiv);
152  }
153  }
154 
155  void Panel::expandContent(bool recursiv) {
157  }
158 
159  void Panel::mouseEntered(MouseEvent& mouseEvent) {
160  ResizableWindow::mouseEntered(mouseEvent);
161  }
162 
163  void Panel::mouseExited(MouseEvent& mouseEvent) {
164  ResizableWindow::mouseExited(mouseEvent);
165  }
166 
167  void Panel::mousePressed(MouseEvent& mouseEvent) {
168  if (!m_docked) {
169  ResizableWindow::mousePressed(mouseEvent);
170  } else {
171  int32_t height = getBorderSize() + getPaddingTop() + getTitleBarHeight();
172  mDragOffsetX = mouseEvent.getX();
173  mDragOffsetY = mouseEvent.getY();
174 
175  mMoved = mouseEvent.getY() <= height;
176  }
177  }
178 
179  void Panel::mouseReleased(MouseEvent& mouseEvent) {
180  if (mouseEvent.getButton() == MouseEvent::Left) {
181  // move it inside the dock area
182  if (m_docked && isMovable() && mMoved) {
184  getDockedArea()->adaptLayout(false);
185  }
186  } else if (mouseEvent.getButton() == MouseEvent::Right) {
187  if (getDockedArea()) {
188  restoreCursor();
189  }
190  }
191  ResizableWindow::mouseReleased(mouseEvent);
192  }
193 
194  void Panel::mouseMoved(MouseEvent& mouseEvent) {
195  ResizableWindow::mouseMoved(mouseEvent);
196  }
197 
198  void Panel::mouseDragged(MouseEvent& mouseEvent) {
199  if (m_docked) {
200  if (isMovable() && mMoved) {
201  Rectangle rec = getDockedArea()->getChildrenArea();
202  int32_t x = mouseEvent.getX() - mDragOffsetX + getX();
203  int32_t y = mouseEvent.getY() - mDragOffsetY + getY();
204  if (x < 0) {
205  x = 0;
206  } else if (x + getWidth() > rec.width) {
207  x = rec.width - getWidth();
208  }
209  if (y < 0) {
210  y = 0;
211  } else if (y + getHeight() > rec.height) {
212  y = rec.height - getHeight();
213  }
214  setPosition(x, y);
216  // move to top on dragged instead of pressed
217  getDockedArea()->moveToTop(this);
218  // restore "old" position, layouting can change it
219  setPosition(x, y);
220  }
221 
222  mouseEvent.consume();
223  } else {
224  ResizableWindow::mouseDragged(mouseEvent);
225  }
226  }
227 }
virtual void mouseDragged(MouseEvent &mouseEvent)
Definition: panel.cpp:198
virtual void mousePressed(MouseEvent &mouseEvent)
bool isDocked() const
Gets if the widget is docked.
Definition: panel.cpp:91
virtual void mouseMoved(MouseEvent &mouseEvent)
Definition: panel.cpp:194
virtual void mouseExited(MouseEvent &mouseEvent)
virtual void mouseReleased(MouseEvent &mouseEvent)
DockArea * getDockedArea()
Definition: panel.cpp:95
virtual void mousePressed(MouseEvent &mouseEvent)
Definition: panel.cpp:167
virtual void mouseMoved(MouseEvent &mouseEvent)
bool isResizable() const
Gets if the widget is resizable.
bool m_dockable
Definition: panel.h:91
virtual void resizeToContent(bool recursiv=true)
void setResizable(bool resizable)
Sets if the widget is resizable.
virtual void expandContent(bool recursiv=true)
Definition: panel.cpp:155
SavedState m_state
Definition: panel.h:101
bool isDockable() const
Gets if the widget is dockable.
Definition: panel.cpp:57
virtual void resizeToContent(bool recursiv=true)
Definition: panel.cpp:147
virtual DockArea * findDockArea()
Definition: panel.cpp:103
Rectangle dimension
Definition: panel.h:96
virtual void mouseExited(MouseEvent &mouseEvent)
Definition: panel.cpp:163
virtual void mouseReleased(MouseEvent &mouseEvent)
Definition: panel.cpp:179
bool m_docked
Definition: panel.h:93
void repositionWidget(Widget *widget)
Definition: dockarea.cpp:167
virtual void mouseDragged(MouseEvent &mouseEvent)
virtual void mouseEntered(MouseEvent &mouseEvent)
virtual ~Panel()
Definition: panel.cpp:49
void setDocked(bool docked)
Sets if the widget is docked.
Definition: panel.cpp:61
virtual void mouseEntered(MouseEvent &mouseEvent)
Definition: panel.cpp:159
void setDockable(bool dockable)
Sets if the widget is dockable.
Definition: panel.cpp:53