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