Debian Codegraph @

common/ui/scroll.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20
21 #include <stdlib.h>
22
23 #include "maths.h"
24 #include "pstypes.h"
25 #include "event.h"
26 #include "gr.h"
27 #include "ui.h"
28 #include "mouse.h"
29 #include "key.h"
30 #include "timer.h"
31
32 namespace dcx {
33
34 namespace {
35
36 void ui_draw_scrollbar(UI_DIALOG &dlg, UI_GADGET_SCROLLBAR &scrollbar)
37 {
38 #if 0 //ndef OGL
39 if (scrollbar.status==0)
40 return;
41 #endif
42
43 gr_set_current_canvas(scrollbar.canvas);
44 auto &canvas = *grd_curcanv;
45
46 const auto color = (dlg.keyboard_focus_gadget == &scrollbar)
47 ? CRED
48 : CGREY;
49
50 gr_rect(canvas, 0, 0, scrollbar.width - 1, scrollbar.fake_position - 1, color);
51 gr_rect(canvas, 0, scrollbar.fake_position + scrollbar.fake_size, scrollbar.width - 1, scrollbar.height - 1, color);
52 ui_draw_box_out(canvas, 0, scrollbar.fake_position, scrollbar.width - 1, scrollbar.fake_position + scrollbar.fake_size - 1);
53 }
54
55 }
56
57 std::unique_ptr<UI_GADGET_SCROLLBAR> ui_add_gadget_scrollbar(UI_DIALOG &dlg, short x, short y, short w, short h, int start, int stop, int position, int window_size)
58 {
59 auto &up = "\x1e";
60 auto &down = "\x1f";
61
62 const auto tw = gr_get_string_size(*grd_curcanv->cv_font, up).width;
63
64 w = tw + 10;
65
66 if (stop < start ) stop = start;
67
68 auto scrollbar = ui_gadget_add<UI_GADGET_SCROLLBAR>(dlg, x, y + w, x + w - 1, y + h - w - 1);
69
70 scrollbar->up_button = ui_add_gadget_button(dlg, x, y, w, w, up, nullptr);
71 scrollbar->up_button->parent = scrollbar.get();
72
73 scrollbar->down_button = ui_add_gadget_button(dlg, x, y+h-w, w, w, down, nullptr);
74 scrollbar->down_button->parent = scrollbar.get();
75
76 scrollbar->horz = 0;
77 scrollbar->width = scrollbar->x2-scrollbar->x1+1;
78 scrollbar->height = scrollbar->y2-scrollbar->y1+1;
79 scrollbar->start = start;
80 scrollbar->stop = stop;
81 scrollbar->position = position;
82 scrollbar->window_size = window_size;
83 scrollbar->fake_length = scrollbar->height;
84 scrollbar->fake_position = 0;
85 if (stop!=start)
86 scrollbar->fake_size = (window_size * scrollbar->height)/(stop-start+1+window_size);
87 else
88 scrollbar->fake_size = scrollbar->height;
89
90 if (scrollbar->fake_size < 7) scrollbar->fake_size = 7;
91 scrollbar->dragging = 0;
92 scrollbar->moved=0;
93 scrollbar->last_scrolled = 0;
94 return scrollbar;
95
96 }
97
98 window_event_result UI_GADGET_SCROLLBAR::event_handler(UI_DIALOG &dlg, const d_event &event)
99 {
100 window_event_result rval = window_event_result::ignored;
101 if (event.type == event_type::window_draw)
102 {
103 ui_draw_scrollbar(dlg, *this);
104 return window_event_result::ignored;
105 }
106
107 const auto keyfocus = (dlg.keyboard_focus_gadget == this);
108
109 if (start==stop)
110 {
111 position = 0;
112 fake_position = 0;
113 ui_draw_scrollbar(dlg, *this);
114 return window_event_result::ignored;
115 }
116
117 const auto op{position};
118
119 moved = 0;
120
121
122 if (keyfocus && event.type == event_type::key_command)
123 {
124 int key;
125 key = event_key_get(event);
126
127 if (key & KEY_UP)
128 {
129 up_button->position = 2;
130 rval = window_event_result::handled;
131 }
132 else if (key & KEY_DOWN)
133 {
134 down_button->position = 2;
135 rval = window_event_result::handled;
136 }
137 }
138 else if (keyfocus && event.type == event_type::key_release)
139 {
140 int key;
141 key = event_key_get(event);
142 if (key & KEY_UP)
143 {
144 up_button->position = 0;
145 rval = window_event_result::handled;
146 }
147 else if (key & KEY_DOWN)
148 {
149 down_button->position = 0;
150 rval = window_event_result::handled;
151 }
152 }
153
154 if (up_button->position!=0)
155 {
156 if (timer_query() > last_scrolled + 1)
157 {
158 last_scrolled = timer_query();
159 position--;
160 if (position < start )
161 position = start;
162 fake_position = position-start;
163 fake_position *= height-fake_size;
164 fake_position /= (stop-start);
165 }
166 }
167
168 if (down_button->position!=0)
169 {
170 if (timer_query() > last_scrolled + 1)
171 {
172 last_scrolled = timer_query();
173 position++;
174 if (position > stop )
175 position = stop;
176 fake_position = position-start;
177 fake_position *= height-fake_size;
178 fake_position /= (stop-start);
179 }
180 }
181
182 const auto OnMe = ui_mouse_on_gadget(*this);
183
184 //gr_ubox(0, fake_position, width-1, fake_position+fake_size-1 );
185
186 if (B1_JUST_RELEASED)
187 dragging = 0;
188
189 //if (B1_PRESSED && OnMe )
190 // listbox->dragging = 1;
191
192
193 const auto [x, y, z] = mouse_get_pos();
194
195 const auto OnSlider = y >= fake_position + y1 &&
196 y < fake_position + y1 + fake_size &&
197 OnMe;
198
199 if (B1_JUST_PRESSED && OnSlider )
200 {
201 dragging = 1;
202 drag_x = x;
203 drag_y = y;
204 drag_starting = fake_position;
205 rval = window_event_result::handled;
206 }
207 else if (B1_JUST_PRESSED && OnMe)
208 {
209 dragging = 2; // outside the slider
210 rval = window_event_result::handled;
211 }
212
213 if ((dragging == 2) && OnMe && !OnSlider && (timer_query() > last_scrolled + 4))
214 {
215 last_scrolled = timer_query();
216
217 if ( y < fake_position+y1 )
218 {
219 // Page Up
220 position -= window_size;
221 if (position < start )
222 position = start;
223
224 } else {
225 // Page Down
226 position += window_size;
227 if (position > stop )
228 position = stop;
229 }
230 fake_position = position-start;
231 fake_position *= height-fake_size;
232 fake_position /= (stop-start);
233 }
234
235 if (selected_gadget == this && dragging == 1)
236 {
237 //x = drag_x;
238 fake_position = drag_starting + (y - drag_y );
239 if (fake_position<0)
240 {
241 fake_position = 0;
242 //y = fake_position + drag_y - drag_starting;
243 }
244 if (fake_position > (height-fake_size))
245 {
246 fake_position = (height-fake_size);
247 //y = fake_position + drag_y - drag_starting;
248 }
249
250 //mouse_set_pos( x, y );
251
252 position = fake_position;
253 position *= (stop-start);
254 position /= ( height-fake_size ) ;
255 position += start;
256
257 if (position > stop )
258 position = stop;
259
260 if (position < start )
261 position = start;
262
263 //fake_position = position-start;
264 //fake_position *= height-fake_size;
265 //fake_position /= (stop-start);
266
267 }
268
269 if (op != position )
270 moved = 1;
271 if (moved)
272 {
273 rval = ui_gadget_send_event(dlg, event_type::ui_gadget_pressed, *this);
274 if (rval == window_event_result::ignored)
275 rval = window_event_result::handled;
276 }
277 return rval;
278 }
279
280 }