The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
scrollarea.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2016 by Guillaume Melquiond <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /** @file */
16 
17 #define GETTEXT_DOMAIN "wesnoth-lib"
18 
19 #include "global.hpp"
20 
21 #include "widgets/scrollarea.hpp"
22 #include "sdl/rect.hpp"
23 
24 
25 namespace gui {
26 
27 scrollarea::scrollarea(CVideo &video, const bool auto_join)
28  : widget(video, auto_join), scrollbar_(video),
29  old_position_(0), recursive_(false), shown_scrollbar_(false),
30  shown_size_(0), full_size_(0)
31 {
32  scrollbar_.hide(true);
33 }
34 
36 {
38 }
39 
41 {
43  h.push_back(&scrollbar_);
44  return h;
45 }
46 
47 void scrollarea::update_location(SDL_Rect const &rect)
48 {
49  SDL_Rect r = rect;
51  if (shown_scrollbar_) {
52  int w = r.w - scrollbar_.width();
53  r.x += w;
54  r.w -= w;
56  r.x -= w;
57  r.w = w;
58  }
59 
60  if (!hidden())
63 }
64 
66 {
67  if (recursive_)
68  return;
69  recursive_ = true;
71  bg_restore();
72  bg_cancel();
74  }
75  recursive_ = false;
76 }
77 
79 {
80  widget::hide(value);
81  if (shown_scrollbar_)
82  scrollbar_.hide(value);
83 }
84 
85 unsigned scrollarea::get_position() const
86 {
87  return scrollbar_.get_position();
88 }
89 
91 {
93 }
94 
96 {
98 }
99 
101 {
103 }
104 
106 {
108 }
109 
111 {
113  shown_size_ = h;
114  test_scrollbar();
115 }
116 
118 {
120  full_size_ = h;
121  test_scrollbar();
122 }
123 
125 {
127 }
128 
130 {
131  int grip_position = scrollbar_.get_position();
132  if (grip_position == old_position_)
133  return;
134  old_position_ = grip_position;
135  scroll(grip_position);
136 }
137 
139 {
140  SDL_Rect r = location();
141  if (shown_scrollbar_)
142  r.w -= scrollbar_.width();
143  return r;
144 }
145 
147 {
148  return scrollbar_.width();
149 }
150 
151 void scrollarea::handle_event(const SDL_Event& event)
152 {
154 
155  if (mouse_locked() || hidden())
156  return;
157 
158  if (event.type != SDL_MOUSEWHEEL)
159  return;
160 
161  const SDL_MouseWheelEvent &ev = event.wheel;
162  int x, y;
163  SDL_GetMouseState(&x, &y);
164  if (sdl::point_in_rect(x, y, inner_location())) {
165  if (ev.y > 0) {
167  } else if (ev.y < 0) {
169  }
170  }
171 }
172 
173 } // end namespace gui
174 
void bg_cancel()
Definition: widget.cpp:77
void set_shown_size(unsigned h)
Definition: scrollarea.cpp:110
void adjust_position(unsigned pos)
Definition: scrollarea.cpp:100
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:163
int pos
Definition: formula.cpp:800
virtual sdl_handler_vector handler_members()
Definition: scrollarea.cpp:40
Definition: video.hpp:58
General purpose widgets.
bool is_valid_height(int height) const
Return true if the scrollbar has a valid size.
Definition: scrollbar.cpp:179
bool has_scrollbar() const
Definition: scrollarea.cpp:35
unsigned scrollbar_width() const
Definition: scrollarea.cpp:146
#define h
void set_scroll_rate(unsigned r)
Definition: scrollarea.cpp:124
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
void test_scrollbar()
Definition: scrollarea.cpp:65
void move_position(int dep)
Definition: scrollarea.cpp:105
void scroll_down()
Scrolls down one step.
Definition: scrollbar.cpp:190
virtual void scroll(unsigned int pos)=0
bool hidden() const
Definition: widget.cpp:198
void set_full_size(unsigned h)
Definition: scrollarea.cpp:117
void set_position(unsigned pos)
Definition: scrollarea.cpp:95
SDL_Rect inner_location() const
Definition: scrollarea.cpp:138
virtual void hide(bool value=true)
Definition: widget.cpp:162
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
GLsizei const GLfloat * value
Definition: glew.h:1817
virtual void hide(bool value=true)
Definition: scrollbar.cpp:100
virtual void set_inner_location(SDL_Rect const &rect)=0
void set_position(unsigned pos)
Manually update the scrollbar.
Definition: scrollbar.cpp:117
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:47
unsigned get_position() const
Definition: scrollarea.cpp:85
void adjust_position(unsigned pos)
Ensure the viewport contains the position.
Definition: scrollbar.cpp:129
unsigned full_size_
Definition: scrollarea.hpp:63
scrollbar scrollbar_
Definition: scrollarea.hpp:59
virtual void handle_event(const SDL_Event &event)
Definition: scrollarea.cpp:151
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
unsigned get_max_position() const
Definition: scrollarea.cpp:90
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
void scroll_up()
Scrolls up one step.
Definition: scrollbar.cpp:195
virtual void hide(bool value=true)
Definition: scrollarea.cpp:78
unsigned get_max_position() const
Definition: scrollbar.cpp:112
unsigned shown_size_
Definition: scrollarea.hpp:62
Contains the SDL_Rect helper code.
void bg_restore() const
Definition: widget.cpp:251
void set_shown_size(unsigned h)
Set the relative size of the grip.
Definition: scrollbar.cpp:146
cl_event event
Definition: glew.h:3070
bool mouse_locked() const
Definition: widget.cpp:72
virtual void set_location(SDL_Rect const &rect)
Definition: widget.cpp:85
void set_full_size(unsigned h)
Set the relative size of the scrollbar.
Definition: scrollbar.cpp:160
void set_scroll_rate(unsigned r)
Set scroll rate.
Definition: scrollbar.cpp:174
scrollarea(CVideo &video, bool auto_join=true)
Create a zone with automatic handling of scrollbar.
Definition: scrollarea.cpp:27
virtual void handle_event(SDL_Event const &)
Definition: widget.cpp:345
int width() const
Definition: widget.cpp:134
SDL_Rect const & location() const
Definition: widget.cpp:144
void move_position(int dep)
Move the scrollbar.
Definition: scrollbar.cpp:137
virtual void process_event()
Definition: scrollarea.cpp:129
virtual void update_location(SDL_Rect const &rect)
Definition: scrollarea.cpp:47
unsigned get_position() const
Determine where the scrollbar is.
Definition: scrollbar.cpp:107