The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
scrollpane.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2004 - 2016 by Philippe Plantier <[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 <algorithm>
22 
23 #include "widgets/scrollpane.hpp"
24 
25 namespace {
26 class widget_finder {
27 public:
28  widget_finder(gui::widget* w) : w_(w) {}
29 
30  bool operator()(const std::pair<int, gui::scrollpane::scrollpane_widget>& p)
31  {
32  if(p.second.w == w_)
33  return true;
34  return false;
35  }
36 private:
37  gui::widget* w_;
38 };
39 }
40 
41 namespace gui {
42 
43 scrollpane::scrollpane(CVideo &video) : scrollarea(video), border_(5)
44 {
45  content_pos_.x = 0;
46  content_pos_.y = 0;
48  set_scroll_rate(40);
49 }
50 
52 {
53  content_.clear();
55 }
56 
57 void scrollpane::set_location(SDL_Rect const& rect)
58 {
62 }
63 
65 {
66  for(widget_map::iterator itor = content_.begin(); itor != content_.end(); ++itor) {
67  itor->second.w->hide_override(value);
68  }
69 }
70 
71 void scrollpane::add_widget(widget* w, int x, int y, int z_order)
72 {
73  if (w == nullptr)
74  return;
75 
76  widget_map::iterator itor = std::find_if(content_.begin(), content_.end(), widget_finder(w));
77  if (itor != content_.end())
78  return;
79 
80  scrollpane_widget spw(w, x, y, z_order);
81 
83  content_.insert(std::pair<int, scrollpane_widget>(z_order, spw));
84 
85  position_widget(spw);
86 
87  // Recalculates the whole content size
89 }
90 
92 {
93  widget_map::iterator itor = std::find_if(content_.begin(), content_.end(), widget_finder(w));
94 
95  if (itor != content_.end())
96  content_.erase(itor);
97 
99 }
100 
101 void scrollpane::set_inner_location(const SDL_Rect& /*rect*/)
102 {
103  for(widget_map::iterator itor = content_.begin(); itor != content_.end(); ++itor) {
104  itor->second.w->set_clip_rect(client_area());
105  }
106 }
107 
109 {
110  //draws the scrollpane background
111 }
112 
113 void scrollpane::scroll(unsigned int pos)
114 {
115  if (static_cast<int>(pos) == content_pos_.y)
116  return;
117 
118  content_pos_.y = pos;
120 }
121 
123 {
125  std::vector<bool> hidden(content_.size());
126  int i = 0;
127  for(itor = content_.begin(); itor != content_.end(); ++itor) {
128  hidden[i++] = (itor->second.w->state_ == HIDDEN);
129  itor->second.w->hide();
130  }
131 
132  for(itor = content_.begin(); itor != content_.end(); ++itor) {
133  position_widget(itor->second);
134  }
135 
136  i = 0;
137  for(itor = content_.begin(); itor != content_.end(); ++itor) {
138  if (!hidden[i++])
139  itor->second.w->hide(false);
140  }
141 
142  set_dirty();
143 }
144 
146 {
147  spw.w->set_location(spw.x + location().x + border_,
148  spw.y + location().y - content_pos_.y + border_);
149 }
150 
151 SDL_Rect scrollpane::client_area() const
152 {
153  SDL_Rect res;
154 
155  res.x = location().x + border_;
156  res.y = location().y + border_;
157  res.w = location().w > 2 * border_ ? location().w - 2 * border_ : 0;
158  res.h = location().h > 2 * border_ ? location().h - 2 * border_ : 0;
159 
160  return res;
161 }
162 
164 {
165  int maxx = 0;
166  int maxy = 0;
167 
168  for(widget_map::iterator itor = content_.begin(); itor != content_.end(); ++itor) {
169  if(itor->second.x + itor->second.w->width() > maxx) {
170  maxx = itor->second.x + itor->second.w->width();
171  }
172  if(itor->second.y + itor->second.w->height() > maxy) {
173  maxy = itor->second.y + itor->second.w->height();
174  }
175  }
176 
177  content_pos_.w = maxx;
178  content_pos_.h = maxy;
179 
180  set_full_size(maxy);
182 
183  set_dirty();
184 }
185 
186 } // namespace gui
187 
void position_widget(scrollpane_widget &spw)
Definition: scrollpane.cpp:145
int w_
Definition: font.cpp:607
SDL_Rect content_pos_
Definition: scrollpane.hpp:80
void set_shown_size(unsigned h)
Definition: scrollarea.cpp:110
int pos
Definition: formula.cpp:800
Definition: video.hpp:58
virtual void set_inner_location(SDL_Rect const &rect)
Definition: scrollpane.cpp:101
void set_clip_rect(const SDL_Rect &rect)
Definition: widget.cpp:191
General purpose widgets.
void set_scroll_rate(unsigned r)
Definition: scrollarea.cpp:124
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
virtual void set_location(SDL_Rect const &rect)
Definition: scrollpane.cpp:57
bool hidden() const
Definition: widget.cpp:198
void set_full_size(unsigned h)
Definition: scrollarea.cpp:117
void set_dirty(bool dirty=true)
Definition: widget.cpp:217
scrollpane(CVideo &video)
Create a scrollpane.
Definition: scrollpane.cpp:43
void update_content_size()
Definition: scrollpane.cpp:163
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
GLsizei const GLfloat * value
Definition: glew.h:1817
void update_widget_positions()
Definition: scrollpane.cpp:122
GLfloat GLfloat p
Definition: glew.h:12766
Encapsulates the map of the game.
Definition: location.hpp:38
GLuint res
Definition: glew.h:9258
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
void remove_widget(widget *w)
Definition: scrollpane.cpp:91
virtual void draw()
Definition: scrollpane.cpp:108
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
SDL_Rect client_area() const
Definition: scrollpane.cpp:151
virtual void hide(bool value=true)
Definition: scrollpane.cpp:64
widget_map content_
Definition: scrollpane.hpp:79
virtual void scroll(unsigned int pos)
Definition: scrollpane.cpp:113
virtual void set_location(SDL_Rect const &rect)
Definition: widget.cpp:85
SDL_Rect const & location() const
Definition: widget.cpp:144
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
void add_widget(widget *w, int x, int y, int z_order=0)
Definition: scrollpane.cpp:71