Adonthell  0.4
win_scroll.cc
1 /*
2  (C) Copyright 2000 Joel Vennin
3  Part of the Adonthell Project <http://adonthell.nongnu.org>
4 
5  Adonthell 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 
10  Adonthell is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Adonthell. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "win_scroll.h"
20 
21 
22 win_scroll::win_scroll():win_scrollbar(this)
23 {
24 
25  cursor_y_=0;
26 
27  max_amplitude_=0;
28 
29  cur_amplitude_=0;
30 
31  index_pad_=PAD_DEFAULT;
32 
33  set_auto_scrollbar(false);
34 
35  set_auto_refresh(false);
36 
37 }
38 
39 
40 bool win_scroll::down()
41 {
42 
43  if( max_amplitude_ == 0 ) return false;
44 
45  if( cur_amplitude_ + index_pad_ > max_amplitude_ ) cur_amplitude_ = max_amplitude_;
46  else cur_amplitude_ += index_pad_;
47 
48  update_amplitude();
49 
50  on_down();
51 
52  return true;
53 
54 }
55 
56 
58 {
59  win_container::resize(tl,th);
60 
61  find_amplitude();
62 
63  win_scrollbar::update_back();
64 
65  win_scrollbar::update_bar();
66 }
67 
68 
69 bool win_scroll::up()
70 {
71  if( max_amplitude_==0 ) return false ;
72 
73  if( cur_amplitude_-index_pad_ < 0) cur_amplitude_ = 0;
74  else cur_amplitude_ -= index_pad_;
75 
76  update_amplitude();
77 
78  on_up();
79 
80  return true;
81 }
82 
83 void win_scroll::set_pos (const u_int8 pos)
84 {
85  cur_amplitude_ = (max_amplitude_ * pos) / 255;
86  if (max_amplitude_ != 0) update_amplitude ();
87 }
88 
89 void win_scroll::update_amplitude()
90 {
91  for(lwb::iterator i=list_wb_.begin() ; i!=list_wb_.end() ; i++)
92  {
93  (*i)->pad_y() = -cur_amplitude_ ;
94  (*i)->update_position();
95  }
96 
97  cursor_y_= ((height() - win_scrollbar::height_bar()) * cur_amplitude_) / max_amplitude_;
98 }
99 
100 
101 void win_scroll::set_space_between_border(u_int16 tmp)
102 {
103 
104  win_container::set_space_with_border(tmp);
105 
106  find_amplitude();
107 
108  win_scrollbar::update_bar();
109 
110 }
111 
112 
113 void win_scroll::set_space_between_object(u_int16 tmp)
114 {
115  win_container::set_space_with_object(tmp);
116 
117  find_amplitude();
118 
119  win_scrollbar::update_bar();
120 }
121 
122 
123 void win_scroll::add(win_base *wb)
124 {
125  win_container::add(wb);
126 
127  find_amplitude();
128 
129  win_scrollbar::update_bar();
130 }
131 
132 
133 void win_scroll::remove(win_base *wb)
134 {
135  win_container::remove(wb);
136 
137  find_amplitude();
138 
139  win_scrollbar::update_bar();
140 }
141 
142 
143 void win_scroll::remove_all()
144 {
145  win_container::remove_all();
146 
147  max_amplitude_=0;
148 
149  cur_amplitude_=0;
150 
151  win_scrollbar::update_bar();
152 }
153 
154 
155 void win_scroll::destroy()
156 {
157  win_container::destroy();
158 
159  max_amplitude_=0;
160 
161  cur_amplitude_=0;
162 
163  win_scrollbar::update_bar();
164 }
165 
166 
168 {
169  if(win_base::draw())
170  {
171  assign_drawing_area(wb_father_);
172 
173  win_background::draw(this);
174 
175  for(lwb::iterator i=list_wb_.begin();i!=list_wb_.end();i++)
176  (*i)->draw();
177 
178  win_scrollbar::draw(wb_father_);
179 
180  win_border::draw(wb_father_);
181 
183 
184  return true;
185  }
186  return false;
187 }
188 
189 
191 {
193  {
194  if(auto_scrollbar_)
195  {
196  u_int16 old = amplitude();
197  find_amplitude();
198  if(old != amplitude())
199  {
200  win_scrollbar::set_visible_scrollbar(amplitude()!=0);
201  win_scrollbar::update_bar();
202  }
203  }
204  else if(auto_refresh_)
205  {
206  u_int16 old = amplitude(); find_amplitude();
207  if(old != amplitude())
208  win_scrollbar::update_bar();
209  }
210  return true;
211  }
212  return false;
213 }
214 
215 
217 {
219  {
220  if(focus_object_) return true;
221  if(input::is_pushed(win_keys::KEY_UP)) up();
222  if(input::is_pushed(win_keys::KEY_DOWN)) down();
223  return true;
224  }
225  return false;
226 }
227 
228 
229 void win_scroll::find_amplitude()
230 {
231  //search the max y object to calcul the amplitude
232  max_amplitude_ = cursor_y_ = cur_amplitude_ = 0;
233 
234  // max_amplitude_ = 0;
235 
236  for(lwb::iterator i=list_wb_.begin() ; i!=list_wb_.end() ; i++)
237  if((*i)->y() + (*i)->height() > height() - space_with_border() && (*i)->y() + (*i)->height() - height() + space_with_border() > max_amplitude_ )
238  max_amplitude_ = (*i)->y() + (*i)->height() - height() + space_with_border();
239 }
240 
u_int16 height() const
Returns the height of the drawing_area.
Definition: drawing_area.h:101
#define u_int16
16 bits long unsigned integer
Definition: types.h:38
void detach_drawing_area()
Detach (if needed) the drawing_area which was attached to this one.
Definition: drawing_area.h:150
bool draw()
Draw process.
Definition: win_scroll.cc:167
bool input_update()
Input Update process .
Definition: win_scroll.cc:216
void resize(u_int16, u_int16)
Rezise the win_*.
virtual bool input_update()
Input Update process .
#define u_int8
8 bits long unsigned integer
Definition: types.h:35
void assign_drawing_area(const drawing_area *da)
Assign a drawing_area to this drawing_area.
Definition: drawing_area.h:131
bool update()
Update process.
Definition: win_scroll.cc:190
void resize(u_int16 tl, u_int16 th)
Rezise the win_*.
Definition: win_scroll.cc:57
static bool is_pushed(SDL_Keycode key)
Returns whether a key is currently pushed or not.
Definition: input.cc:110
virtual bool update()
Update process.
Common properties for each win_base&#39;s object.
Definition: win_base.h:51
virtual bool draw()
Draw process.
Definition: win_base.cc:111