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