Debian Codegraph @

engine/core/gui/fifechan/widgets/resizablewindow.cpp

raw
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
31 #include "gui/fifechan/base/gui_font.h"
32 #include "gui/fifechan/fifechanmanager.h"
33 #include "util/base/exception.h"
34
35 #include "resizablewindow.h"
36
37 namespace fcn {
38 ResizableWindow::ResizableWindow():
39 Window(),
40 m_cursor(FIFE::FifechanManager::instance()->getCursor()),
41 m_borderDistance(5),
42 m_resizable(true),
43 m_resizableTop(true),
44 m_resizableRight(true),
45 m_resizableBottom(true),
46 m_resizableLeft(true),
47 m_resizing(false),
48 m_resizeTop(false),
49 m_resizeRight(false),
50 m_resizeBottom(false),
51 m_resizeLeft(false),
52 m_shove(false) {
53
54 addKeyListener(this);
55 addFocusListener(this);
56 initCursors();
57 }
58
59 ResizableWindow::ResizableWindow(bool resizable):
60 Window(),
61 m_cursor(FIFE::FifechanManager::instance()->getCursor()),
62 m_borderDistance(5),
63 m_resizable(resizable),
64 m_resizableTop(true),
65 m_resizableRight(true),
66 m_resizableBottom(true),
67 m_resizableLeft(true),
68 m_resizing(false),
69 m_resizeTop(false),
70 m_resizeRight(false),
71 m_resizeBottom(false),
72 m_resizeLeft(false),
73 m_shove(false) {
74
75 addKeyListener(this);
76 addFocusListener(this);
77 initCursors();
78 }
79
80 ResizableWindow::ResizableWindow(const std::string& caption, bool resizable):
81 Window(),
82 m_cursor(FIFE::FifechanManager::instance()->getCursor()),
83 m_borderDistance(5),
84 m_resizable(resizable),
85 m_resizableTop(true),
86 m_resizableRight(true),
87 m_resizableBottom(true),
88 m_resizableLeft(true),
89 m_resizing(false),
90 m_resizeTop(false),
91 m_resizeRight(false),
92 m_resizeBottom(false),
93 m_resizeLeft(false),
94 m_shove(false) {
95
96 setCaption(caption);
97 addKeyListener(this);
98 addFocusListener(this);
99 initCursors();
100 }
101
102 ResizableWindow::~ResizableWindow() {
103 }
104
105 void ResizableWindow::initCursors() {
106 // defines default cursors
107 // don't change the sequence!
108 CursorState state;
109 state.cursorId = FIFE::NC_ARROW;
110 state.cursorType = FIFE::CURSOR_NONE;
111 m_saved = state;
112 state.cursorType = FIFE::CURSOR_NATIVE;
113 // left
114 state.cursorId = FIFE::NC_RESIZEWE;
115 m_cursors.push_back(state);
116 // right
117 state.cursorId = FIFE::NC_RESIZEWE;
118 m_cursors.push_back(state);
119 // top
120 state.cursorId = FIFE::NC_RESIZENS;
121 m_cursors.push_back(state);
122 // left and top
123 state.cursorId = FIFE::NC_RESIZENWSE;
124 m_cursors.push_back(state);
125 // right and top
126 state.cursorId = FIFE::NC_RESIZENESW;
127 m_cursors.push_back(state);
128 // bottom
129 state.cursorId = FIFE::NC_RESIZENS;
130 m_cursors.push_back(state);
131 // left and bottom
132 state.cursorId = FIFE::NC_RESIZENESW;
133 m_cursors.push_back(state);
134 // right and bottom
135 state.cursorId = FIFE::NC_RESIZENWSE;
136 m_cursors.push_back(state);
137 }
138
139 void ResizableWindow::restoreCursor() {
140 FIFE::MouseCursorType type = m_saved.cursorType;
141 switch (type) {
142 case FIFE::CURSOR_NATIVE:
143 m_cursor->set(m_saved.cursorId);
144 break;
145 case FIFE::CURSOR_IMAGE:
146 m_cursor->set(m_saved.cursorImage);
147 break;
148 case FIFE::CURSOR_ANIMATION:
149 m_cursor->set(m_saved.cursorAnimation);
150 break;
151 default:
152 return;
153 }
154 }
155
156 void ResizableWindow::saveCursor() {
157 FIFE::MouseCursorType type = m_cursor->getType();
158 switch (type) {
159 case FIFE::CURSOR_NATIVE:
160 m_saved.cursorType = type;
161 m_saved.cursorId = m_cursor->getId();
162 m_saved.cursorImage.reset();
163 m_saved.cursorAnimation.reset();
164 break;
165 case FIFE::CURSOR_IMAGE:
166 m_saved.cursorType = type;
167 m_saved.cursorId = FIFE::NC_ARROW;;
168 m_saved.cursorImage = m_cursor->getImage();
169 m_saved.cursorAnimation.reset();
170 break;
171 case FIFE::CURSOR_ANIMATION:
172 m_saved.cursorType = type;
173 m_saved.cursorId = FIFE::NC_ARROW;;
174 m_saved.cursorImage.reset();
175 m_saved.cursorAnimation = m_cursor->getAnimation();
176 break;
177 default:
178 m_saved.cursorType = FIFE::CURSOR_NONE;
179 }
180 }
181
182 void ResizableWindow::setResizableBorderDistance(int32_t border) {
183 m_borderDistance = border;
184 }
185
186 int32_t ResizableWindow::getResizableBorderDistance() const {
187 return m_borderDistance;
188 }
189
190 void ResizableWindow::setResizable(bool resizable) {
191 m_resizable = resizable;
192 }
193
194 bool ResizableWindow::isResizable() const {
195 return m_resizable;
196 }
197
198 void ResizableWindow::setTopResizable(bool resizable) {
199 m_resizableTop = resizable;
200 }
201
202 bool ResizableWindow::isTopResizable() const {
203 return m_resizableTop;
204 }
205
206 void ResizableWindow::setRightResizable(bool resizable) {
207 m_resizableRight = resizable;
208 }
209
210 bool ResizableWindow::isRightResizable() const {
211 return m_resizableRight;
212 }
213
214 void ResizableWindow::setBottomResizable(bool resizable) {
215 m_resizableBottom = resizable;
216 }
217
218 bool ResizableWindow::isBottomResizable() const {
219 return m_resizableBottom;
220 }
221
222 void ResizableWindow::setLeftResizable(bool resizable) {
223 m_resizableLeft = resizable;
224 }
225
226 bool ResizableWindow::isLeftResizable() const {
227 return m_resizableLeft;
228 }
229
230 void ResizableWindow::setShove(bool shove) {
231 m_shove = shove;
232 }
233
234 bool ResizableWindow::getShove() const {
235 return m_shove;
236 }
237
238 void ResizableWindow::set(CursorDirections direction, uint32_t cursor_id) {
239 CursorState& state = m_cursors[direction];
240 state.cursorType = FIFE::CURSOR_NATIVE;
241 state.cursorId = cursor_id;
242 state.cursorImage.reset();
243 state.cursorAnimation.reset();
244 }
245
246 void ResizableWindow::set(CursorDirections direction, FIFE::ImagePtr image) {
247 CursorState& state = m_cursors[direction];
248 state.cursorType = FIFE::CURSOR_IMAGE;
249 state.cursorId = FIFE::NC_ARROW;
250 state.cursorImage = image;
251 state.cursorAnimation.reset();
252 }
253
254 void ResizableWindow::set(CursorDirections direction, FIFE::AnimationPtr anim) {
255 CursorState& state = m_cursors[direction];
256 state.cursorType = FIFE::CURSOR_ANIMATION;
257 state.cursorId = FIFE::NC_ARROW;
258 state.cursorImage.reset();
259 state.cursorAnimation = anim;
260 }
261
262 FIFE::MouseCursorType ResizableWindow::getType(CursorDirections direction) const {
263 return m_cursors[direction].cursorType;
264 }
265
266 uint32_t ResizableWindow::getId(CursorDirections direction) const {
267 return m_cursors[direction].cursorId;
268 }
269
270 FIFE::ImagePtr ResizableWindow::getImage(CursorDirections direction) {
271 return m_cursors[direction].cursorImage;
272 }
273
274 FIFE::AnimationPtr ResizableWindow::getAnimation(CursorDirections direction) {
275 return m_cursors[direction].cursorAnimation;
276 }
277
278 void ResizableWindow::resizeToContent(bool recursiv) {
279 // we keep the size in case we want it resizable
280 if (m_resizable) {
281 int w = getWidth();
282 int h = getHeight();
283 Window::resizeToContent(recursiv);
284 setSize(w, h);
285 } else {
286 Window::resizeToContent(recursiv);
287 }
288 }
289
290 void ResizableWindow::mouseEntered(MouseEvent& mouseEvent) {
291 if (m_resizable && !m_resizing) {
292 saveCursor();
293 }
294 Window::mouseEntered(mouseEvent);
295 }
296
297 void ResizableWindow::mouseExited(MouseEvent& mouseEvent) {
298 if (m_resizable && !m_resizing && m_saved.cursorType != FIFE::CURSOR_NONE) {
299 restoreCursor();
300 } else if (m_resizable && m_resizing) {
301 mouseEvent.consume();
302 }
303 Window::mouseExited(mouseEvent);
304 }
305
306 void ResizableWindow::mousePressed(MouseEvent& mouseEvent) {
307 if (m_resizable && mouseEvent.getButton() == MouseEvent::Left) {
308 m_resizeLeft = m_resizableLeft && mouseEvent.getX() < m_borderDistance;
309 m_resizeRight = m_resizableRight && mouseEvent.getX() > getWidth() - m_borderDistance;
310 m_resizeTop = m_resizableTop && mouseEvent.getY() < m_borderDistance;
311 m_resizeBottom = m_resizableBottom && mouseEvent.getY() > getHeight() - m_borderDistance;
312 if (m_resizeTop || m_resizeBottom || m_resizeLeft || m_resizeRight) {
313 m_resizing = true;
314 mouseEvent.consume();
315 }
316 }
317 Window::mousePressed(mouseEvent);
318 }
319
320 void ResizableWindow::mouseReleased(MouseEvent& mouseEvent) {
321 if (m_resizing) {
322 adaptLayout();
323 m_resizing = false;
324 if (mouseEvent.getX() <= 0 || mouseEvent.getX() >= getWidth() ||
325 mouseEvent.getY() <= 0 || mouseEvent.getY() >= getHeight()) {
326 mouseExited(mouseEvent);
327 }
328 mouseEvent.consume();
329 } else {
330 Window::mouseReleased(mouseEvent);
331 }
332 }
333
334 void ResizableWindow::mouseMoved(MouseEvent& mouseEvent) {
335 if (m_resizable && !m_resizing && m_saved.cursorType != FIFE::CURSOR_NONE) {
336 // use the CursorDirections directly
337 uint32_t index = 0;
338 index += (m_resizableLeft && mouseEvent.getX() < m_borderDistance) ? 1 : 0;
339 index += (m_resizableRight && mouseEvent.getX() > getWidth() - m_borderDistance) ? 2 : 0;
340 index += (m_resizableTop && mouseEvent.getY() < m_borderDistance) ? 3 : 0;
341 index += (m_resizableBottom && mouseEvent.getY() > getHeight() - m_borderDistance) ? 6 : 0;
342
343 if (index > 0) {
344 // to get the real index value
345 index -= 1;
346 CursorState state = m_cursors[index];
347 FIFE::MouseCursorType type = state.cursorType;
348 switch (type) {
349 case FIFE::CURSOR_NATIVE:
350 m_cursor->set(state.cursorId);
351 break;
352 case FIFE::CURSOR_IMAGE:
353 m_cursor->set(state.cursorImage);
354 break;
355 case FIFE::CURSOR_ANIMATION:
356 m_cursor->set(state.cursorAnimation);
357 break;
358 default:
359 break;
360 }
361 mouseEvent.consume();
362 } else {
363 restoreCursor();
364 }
365 }
366 Window::mouseMoved(mouseEvent);
367 }
368
369 void ResizableWindow::mouseDragged(MouseEvent& mouseEvent) {
370 if (m_resizable && m_resizing) {
371 int x = mouseEvent.getX();
372 int y = mouseEvent.getY();
373 if (m_resizeLeft) {
374 int oldW = getWidth();
375 int newW = oldW - x;
376 if (newW < 0) {
377 newW = 0;
378 }
379 setWidth(newW);
380 if (m_shove) {
381 setX(getX() + x);
382 } else {
383 newW = getWidth();
384 int newX = oldW - newW;
385 setX(getX() + newX);
386 }
387 } else if (m_resizeRight) {
388 setWidth(x);
389 if (x != getWidth() && m_shove) {
390 setX(getX() + (x-getWidth()));
391 }
392 }
393
394 if (m_resizeTop) {
395 int oldH = getHeight();
396 int newH = oldH - y;
397 if (newH < 0) {
398 newH = 0;
399 }
400 setHeight(newH);
401 if (m_shove) {
402 setY(getY() + y);
403 } else {
404 newH = getHeight();
405 int newY = oldH - newH;
406 setY(getY() + newY);
407 }
408 } else if (m_resizeBottom) {
409 setHeight(y);
410 if (y != getHeight() && m_shove) {
411 setY(getY() + (y-getHeight()));
412 }
413 }
414 mouseEvent.consume();
415 } else {
416 Window::mouseDragged(mouseEvent);
417 }
418 }
419
420
421 void ResizableWindow::focusLost(const Event& event) {
422 m_resizing = false;
423 restoreCursor();
424 }
425 }