The Battle for Wesnoth  1.15.0-dev
scrollarea.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2018 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
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 "widgets/scrollarea.hpp"
20 #include "sdl/rect.hpp"
21 
22 
23 namespace gui {
24 
25 scrollarea::scrollarea(CVideo &video, const bool auto_join)
26  : widget(video, auto_join), scrollbar_(video),
27  old_position_(0), recursive_(false), shown_scrollbar_(false),
28  shown_size_(0), full_size_(0)
29 {
30  scrollbar_.hide(true);
31 }
32 
34 {
36 }
37 
39 {
41  h.push_back(&scrollbar_);
42  return h;
43 }
44 
45 void scrollarea::update_location(const SDL_Rect& rect)
46 {
47  SDL_Rect r = rect;
49  if (shown_scrollbar_) {
50  int w = r.w - scrollbar_.width();
51  r.x += w;
52  r.w -= w;
54  r.x -= w;
55  r.w = w;
56  }
57 
58  if (!hidden())
61 }
62 
64 {
65  if (recursive_)
66  return;
67  recursive_ = true;
69  bg_restore();
70  bg_cancel();
72  }
73  recursive_ = false;
74 }
75 
76 void scrollarea::hide(bool value)
77 {
78  widget::hide(value);
79  if (shown_scrollbar_)
80  scrollbar_.hide(value);
81 }
82 
83 unsigned scrollarea::get_position() const
84 {
85  return scrollbar_.get_position();
86 }
87 
89 {
91 }
92 
93 void scrollarea::set_position(unsigned pos)
94 {
96 }
97 
98 void scrollarea::adjust_position(unsigned pos)
99 {
101 }
102 
104 {
106 }
107 
109 {
111  shown_size_ = h;
112  test_scrollbar();
113 }
114 
116 {
118  full_size_ = h;
119  test_scrollbar();
120 }
121 
123 {
125 }
126 
128 {
129  int grip_position = scrollbar_.get_position();
130  if (grip_position == old_position_)
131  return;
132  old_position_ = grip_position;
133  scroll(grip_position);
134 }
135 
137 {
138  SDL_Rect r = location();
139  if (shown_scrollbar_)
140  r.w -= scrollbar_.width();
141  return r;
142 }
143 
145 {
146  return scrollbar_.width();
147 }
148 
149 void scrollarea::handle_event(const SDL_Event& event)
150 {
152 
153  if (mouse_locked() || hidden())
154  return;
155 
156  if (event.type != SDL_MOUSEWHEEL)
157  return;
158 
159  const SDL_MouseWheelEvent &ev = event.wheel;
160  int x, y;
161  SDL_GetMouseState(&x, &y);
162  if (sdl::point_in_rect(x, y, inner_location())) {
163  if (ev.y > 0) {
165  } else if (ev.y < 0) {
167  }
168  }
169 }
170 
171 } // end namespace gui
void bg_cancel()
Definition: widget.cpp:75
virtual void set_inner_location(const SDL_Rect &rect)=0
void set_shown_size(unsigned h)
Definition: scrollarea.cpp:108
void adjust_position(unsigned pos)
Definition: scrollarea.cpp:98
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:177
bool hidden() const
Definition: widget.cpp:196
unsigned get_position() const
Determine where the scrollbar is.
Definition: scrollbar.cpp:105
unsigned scrollbar_width() const
Definition: scrollarea.cpp:144
virtual sdl_handler_vector handler_members()
Definition: scrollarea.cpp:38
Definition: video.hpp:35
unsigned get_max_position() const
Definition: scrollbar.cpp:110
General purpose widgets.
virtual void update_location(const SDL_Rect &rect)
Definition: scrollarea.cpp:45
#define h
void set_scroll_rate(unsigned r)
Definition: scrollarea.cpp:122
void test_scrollbar()
Definition: scrollarea.cpp:63
void move_position(int dep)
Definition: scrollarea.cpp:103
void scroll_down()
Scrolls down one step.
Definition: scrollbar.cpp:188
virtual void scroll(unsigned int pos)=0
void set_full_size(unsigned h)
Definition: scrollarea.cpp:115
bool has_scrollbar() const
Definition: scrollarea.cpp:33
virtual void handle_event(const SDL_Event &)
Definition: widget.cpp:338
void set_position(unsigned pos)
Definition: scrollarea.cpp:93
virtual void hide(bool value=true)
Definition: widget.cpp:160
const SDL_Rect & location() const
Definition: widget.cpp:142
virtual void hide(bool value=true)
Definition: scrollbar.cpp:98
virtual void set_location(const SDL_Rect &rect)
Definition: widget.cpp:83
void set_position(unsigned pos)
Manually update the scrollbar.
Definition: scrollbar.cpp:115
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:22
void adjust_position(unsigned pos)
Ensure the viewport contains the position.
Definition: scrollbar.cpp:127
unsigned full_size_
Definition: scrollarea.hpp:62
scrollbar scrollbar_
Definition: scrollarea.hpp:58
virtual void handle_event(const SDL_Event &event)
Definition: scrollarea.cpp:149
SDL_Rect inner_location() const
Definition: scrollarea.cpp:136
unsigned get_max_position() const
Definition: scrollarea.cpp:88
int width() const
Definition: widget.cpp:132
int w
void scroll_up()
Scrolls up one step.
Definition: scrollbar.cpp:193
void bg_restore() const
Definition: widget.cpp:249
bool mouse_locked() const
Definition: widget.cpp:70
virtual void hide(bool value=true)
Definition: scrollarea.cpp:76
unsigned shown_size_
Definition: scrollarea.hpp:61
Contains the SDL_Rect helper code.
void set_shown_size(unsigned h)
Set the relative size of the grip.
Definition: scrollbar.cpp:144
bool is_valid_height(int height) const
Return true if the scrollbar has a valid size.
Definition: scrollbar.cpp:177
void set_full_size(unsigned h)
Set the relative size of the scrollbar.
Definition: scrollbar.cpp:158
void set_scroll_rate(unsigned r)
Set scroll rate.
Definition: scrollbar.cpp:172
scrollarea(CVideo &video, bool auto_join=true)
Create a zone with automatic handling of scrollbar.
Definition: scrollarea.cpp:25
void move_position(int dep)
Move the scrollbar.
Definition: scrollbar.cpp:135
virtual void process_event()
Definition: scrollarea.cpp:127
unsigned get_position() const
Definition: scrollarea.cpp:83