]> icculus.org git repositories - btb/d2x.git/blob - ui/scroll.c
no critical error handler (not used by PhysicsFS)
[btb/d2x.git] / ui / scroll.c
1 /* $Id: scroll.c,v 1.5 2005-01-25 19:44:27 schaffner Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 #ifdef RCS
16 static char rcsid[] = "$Id: scroll.c,v 1.5 2005-01-25 19:44:27 schaffner Exp $";
17 #endif
18
19 #ifdef HAVE_CONFIG_H
20 #include "conf.h"
21 #endif
22
23 #include <stdlib.h>
24
25 #include "fix.h"
26 #include "pstypes.h"
27 #include "gr.h"
28 #include "ui.h"
29 #include "key.h"
30 #include "timer.h"
31
32 void ui_draw_scrollbar( UI_GADGET_SCROLLBAR * scrollbar )
33 {
34         int x, y;
35
36         if (scrollbar->status==0)
37                 return;
38
39         scrollbar->status = 0;
40         x = y = 0;
41         ui_mouse_hide();
42         gr_set_current_canvas( scrollbar->canvas );
43
44         if (CurWindow->keyboard_focus_gadget == (UI_GADGET *)scrollbar)
45                 gr_setcolor( CRED );
46         else
47                 gr_setcolor( CGREY );
48
49         gr_rect( 0, 0, scrollbar->width-1, scrollbar->fake_position-1 );
50         gr_rect( 0, scrollbar->fake_position+scrollbar->fake_size, scrollbar->width-1, scrollbar->height-1);
51
52         ui_draw_box_out(0, scrollbar->fake_position, scrollbar->width-1, scrollbar->fake_position+scrollbar->fake_size-1 );
53
54         ui_mouse_show();
55
56 }
57
58 UI_GADGET_SCROLLBAR * ui_add_gadget_scrollbar( UI_WINDOW * wnd, short x, short y, short w, short h, int start, int stop, int position, int window_size  )
59 {
60         int tw, th, taw;
61
62         UI_GADGET_SCROLLBAR * scrollbar;
63         char up[2];
64         char down[2];
65         up[0] = 30; up[1] = 0;
66         down[0] = 31; down[1] = 0;
67
68         gr_get_string_size( up, &tw, &th, &taw );
69
70         w = tw + 10;
71
72         if (stop < start ) stop = start;
73
74         scrollbar = (UI_GADGET_SCROLLBAR *)ui_gadget_add( wnd, 3, x, y+w, x+w-1, y+h-w-1 );
75
76         scrollbar->up_button = ui_add_gadget_button( wnd, x, y, w, w, up, NULL );
77         scrollbar->up_button->parent = (UI_GADGET *)scrollbar;
78
79         scrollbar->down_button =ui_add_gadget_button( wnd, x, y+h-w, w, w, down, NULL );
80         scrollbar->down_button->parent = (UI_GADGET *)scrollbar;
81
82         scrollbar->horz = 0;
83         scrollbar->width = scrollbar->x2-scrollbar->x1+1;
84         scrollbar->height = scrollbar->y2-scrollbar->y1+1;
85         scrollbar->start = start;
86         scrollbar->stop = stop;
87         scrollbar->position = position;
88         scrollbar->window_size = window_size;
89         scrollbar->fake_length = scrollbar->height;
90         scrollbar->fake_position =  0;
91         if (stop!=start)
92                 scrollbar->fake_size = (window_size * scrollbar->height)/(stop-start+1+window_size);
93         else
94                 scrollbar->fake_size = scrollbar->height;
95
96         if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
97         scrollbar->dragging = 0;
98         scrollbar->moved=0;
99         scrollbar->last_scrolled = 0;
100         return scrollbar;
101
102 }
103
104 void ui_scrollbar_do( UI_GADGET_SCROLLBAR * scrollbar, int keypress )
105 {
106         int OnMe, OnSlider, keyfocus;
107
108         int oldpos, op;
109
110         keyfocus = 0;
111         keypress = keypress;
112
113         if (CurWindow->keyboard_focus_gadget==(UI_GADGET *)scrollbar)
114                 keyfocus = 1;
115
116         if (scrollbar->start==scrollbar->stop)
117         {
118                 scrollbar->position = 0;
119                 scrollbar->fake_position = 0;
120                 ui_draw_scrollbar( scrollbar );
121                 return;
122         }
123
124         op = scrollbar->position;
125
126         oldpos = scrollbar->fake_position;
127
128         scrollbar->moved = 0;
129
130
131         if ( (scrollbar->up_button->position!=0) || (keyfocus && keyd_pressed[KEY_UP]) )
132         {
133                 if (timer_get_fixed_seconds() > scrollbar->last_scrolled + 1)
134                 {
135                         scrollbar->last_scrolled = timer_get_fixed_seconds();
136                         scrollbar->position--;
137                         if (scrollbar->position < scrollbar->start )
138                                 scrollbar->position = scrollbar->start;
139                         scrollbar->fake_position = scrollbar->position-scrollbar->start;
140                         scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
141                         scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
142                 }
143         }
144
145         if ( (scrollbar->down_button->position!=0) || (keyfocus && keyd_pressed[KEY_DOWN]) )
146         {
147                 if (timer_get_fixed_seconds() > scrollbar->last_scrolled + 1)
148                 {
149                         scrollbar->last_scrolled = timer_get_fixed_seconds();
150                         scrollbar->position++;
151                         if (scrollbar->position > scrollbar->stop )
152                                 scrollbar->position = scrollbar->stop;
153                         scrollbar->fake_position = scrollbar->position-scrollbar->start;
154                         scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
155                         scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
156                 }
157         }
158
159         OnMe = ui_mouse_on_gadget( (UI_GADGET *)scrollbar );
160
161         //gr_ubox(0, scrollbar->fake_position, scrollbar->width-1, scrollbar->fake_position+scrollbar->fake_size-1 );
162
163         if (!B1_PRESSED )
164                 scrollbar->dragging = 0;
165
166         //if (B1_PRESSED && OnMe )
167         //    listbox->dragging = 1;
168
169
170         OnSlider = 0;
171         if ((Mouse.y >= scrollbar->fake_position+scrollbar->y1) && \
172                 (Mouse.y < scrollbar->fake_position+scrollbar->y1+scrollbar->fake_size) && OnMe )
173                 OnSlider = 1;
174
175         if (B1_JUST_PRESSED && OnSlider )
176         {
177                 scrollbar->dragging = 1;
178                 scrollbar->drag_x = Mouse.x;
179                 scrollbar->drag_y = Mouse.y;
180                 scrollbar->drag_starting = scrollbar->fake_position;
181         }
182
183         if  (B1_PRESSED && OnMe && !OnSlider && (timer_get_fixed_seconds() > scrollbar->last_scrolled + 4))
184         {
185                 scrollbar->last_scrolled = timer_get_fixed_seconds();
186
187                 if ( Mouse.y < scrollbar->fake_position+scrollbar->y1 )
188                 {
189                         // Page Up
190                         scrollbar->position -= scrollbar->window_size;
191                         if (scrollbar->position < scrollbar->start )
192                                 scrollbar->position = scrollbar->start;
193
194                 } else {
195                         // Page Down
196                         scrollbar->position += scrollbar->window_size;
197                         if (scrollbar->position > scrollbar->stop )
198                                 scrollbar->position = scrollbar->stop;
199                 }
200                 scrollbar->fake_position = scrollbar->position-scrollbar->start;
201                 scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
202                 scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
203         }
204
205         if ((selected_gadget==(UI_GADGET *)scrollbar) && B1_PRESSED && scrollbar->dragging )
206         {
207                 //Mouse.x = scrollbar->drag_x;
208                 scrollbar->fake_position = scrollbar->drag_starting + (Mouse.y - scrollbar->drag_y );
209                 if (scrollbar->fake_position<0)
210                 {
211                         scrollbar->fake_position = 0;
212                         //Mouse.y = scrollbar->fake_position + scrollbar->drag_y - scrollbar->drag_starting;
213                 }
214                 if (scrollbar->fake_position > (scrollbar->height-scrollbar->fake_size))
215                 {
216                         scrollbar->fake_position = (scrollbar->height-scrollbar->fake_size);
217                         //Mouse.y = scrollbar->fake_position + scrollbar->drag_y - scrollbar->drag_starting;
218                 }
219
220                 //mouse_set_pos( Mouse.x, Mouse.y );
221
222                 scrollbar->position = scrollbar->fake_position;
223                 scrollbar->position *= (scrollbar->stop-scrollbar->start);
224                 scrollbar->position /= ( scrollbar->height-scrollbar->fake_size ) ;
225                 scrollbar->position += scrollbar->start;
226
227                 if (scrollbar->position > scrollbar->stop )
228                         scrollbar->position = scrollbar->stop;
229
230                 if (scrollbar->position < scrollbar->start )
231                         scrollbar->position = scrollbar->start;
232
233                 //scrollbar->fake_position = scrollbar->position-scrollbar->start;
234                 //scrollbar->fake_position *= scrollbar->height-scrollbar->fake_size;
235                 //scrollbar->fake_position /= (scrollbar->stop-scrollbar->start);
236
237         }
238
239         if (op != scrollbar->position )
240                 scrollbar->moved = 1;
241
242         if (oldpos != scrollbar->fake_position)
243                 scrollbar->status = 1;
244         ui_draw_scrollbar( scrollbar );
245
246 }
247