1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2009 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
20 //
21
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27
28 #include "txt_scrollpane.h"
29 #include "txt_gui.h"
30 #include "txt_io.h"
31 #include "txt_main.h"
32 #include "txt_table.h"
33
34 #include "doomkeys.h"
35
36 #define SCROLLBAR_VERTICAL (1 << 0)
37 #define SCROLLBAR_HORIZONTAL (1 << 1)
38
FullWidth(txt_scrollpane_t * scrollpane)39 static int FullWidth(txt_scrollpane_t *scrollpane)
40 {
41 if (scrollpane->child != NULL)
42 {
43 return scrollpane->child->w;
44 }
45 else
46 {
47 return 0;
48 }
49 }
50
FullHeight(txt_scrollpane_t * scrollpane)51 static int FullHeight(txt_scrollpane_t *scrollpane)
52 {
53 if (scrollpane->child != NULL)
54 {
55 return scrollpane->child->h;
56 }
57 else
58 {
59 return 0;
60 }
61 }
62
63 // Calculate which scroll bars the pane needs.
64
NeedsScrollbars(txt_scrollpane_t * scrollpane)65 static int NeedsScrollbars(txt_scrollpane_t *scrollpane)
66 {
67 int result;
68
69 result = 0;
70
71 if (FullWidth(scrollpane) > scrollpane->w)
72 {
73 result |= SCROLLBAR_HORIZONTAL;
74 }
75 if (FullHeight(scrollpane) > scrollpane->h)
76 {
77 result |= SCROLLBAR_VERTICAL;
78 }
79
80 return result;
81 }
82
83 // If a scrollbar isn't needed, the scroll position is reset.
84
SanityCheckScrollbars(txt_scrollpane_t * scrollpane)85 static void SanityCheckScrollbars(txt_scrollpane_t *scrollpane)
86 {
87 int scrollbars;
88 int max_x, max_y;
89
90 scrollbars = NeedsScrollbars(scrollpane);
91
92 if ((scrollbars & SCROLLBAR_HORIZONTAL) == 0)
93 {
94 scrollpane->x = 0;
95 }
96 if ((scrollbars & SCROLLBAR_VERTICAL) == 0)
97 {
98 scrollpane->y = 0;
99 }
100
101 max_x = FullWidth(scrollpane) - scrollpane->w;
102 max_y = FullHeight(scrollpane) - scrollpane->h;
103
104 if (scrollpane->x < 0)
105 {
106 scrollpane->x = 0;
107 }
108 else if (scrollpane->x > max_x)
109 {
110 scrollpane->x = max_x;
111 }
112
113 if (scrollpane->y < 0)
114 {
115 scrollpane->y = 0;
116 }
117 else if (scrollpane->y > max_y)
118 {
119 scrollpane->y = max_y;
120 }
121 }
122
TXT_ScrollPaneSizeCalc(TXT_UNCAST_ARG (scrollpane))123 static void TXT_ScrollPaneSizeCalc(TXT_UNCAST_ARG(scrollpane))
124 {
125 TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
126 int scrollbars;
127
128 if (scrollpane->child != NULL)
129 {
130 TXT_CalcWidgetSize(scrollpane->child);
131 }
132
133 // Expand as necessary (to ensure that no scrollbars are needed)?
134
135 if (scrollpane->expand_w)
136 {
137 scrollpane->w = FullWidth(scrollpane);
138 }
139 if (scrollpane->expand_h)
140 {
141 scrollpane->h = FullHeight(scrollpane);
142 }
143
144 scrollpane->widget.w = scrollpane->w;
145 scrollpane->widget.h = scrollpane->h;
146
147 // If we have scroll bars, we need to expand slightly to
148 // accomodate them. Eg. if we have a vertical scrollbar, we
149 // need to be an extra character wide.
150
151 scrollbars = NeedsScrollbars(scrollpane);
152
153 if (scrollbars & SCROLLBAR_HORIZONTAL)
154 {
155 ++scrollpane->widget.h;
156 }
157 if (scrollbars & SCROLLBAR_VERTICAL)
158 {
159 ++scrollpane->widget.w;
160 }
161
162 if (scrollpane->child != NULL)
163 {
164 if (scrollpane->child->w < scrollpane->w)
165 {
166 scrollpane->child->w = scrollpane->w;
167 }
168 if (scrollpane->child->h < scrollpane->h)
169 {
170 scrollpane->child->h = scrollpane->h;
171 }
172 }
173 }
174
TXT_ScrollPaneDrawer(TXT_UNCAST_ARG (scrollpane))175 static void TXT_ScrollPaneDrawer(TXT_UNCAST_ARG(scrollpane))
176 {
177 TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
178 int x1, y1, x2, y2;
179 int scrollbars;
180
181 // We set a clipping area of the scroll pane.
182
183 x1 = scrollpane->widget.x,
184 y1 = scrollpane->widget.y,
185 x2 = x1 + scrollpane->w,
186 y2 = y1 + scrollpane->h;
187
188 scrollbars = NeedsScrollbars(scrollpane);
189
190 if (scrollbars & SCROLLBAR_HORIZONTAL)
191 {
192 TXT_DrawHorizScrollbar(x1,
193 y1 + scrollpane->h,
194 scrollpane->w,
195 scrollpane->x,
196 FullWidth(scrollpane) - scrollpane->w);
197 }
198
199 if (scrollbars & SCROLLBAR_VERTICAL)
200 {
201 TXT_DrawVertScrollbar(x1 + scrollpane->w,
202 y1,
203 scrollpane->h,
204 scrollpane->y,
205 FullHeight(scrollpane) - scrollpane->h);
206 }
207
208 TXT_PushClipArea(x1, x2, y1, y2);
209
210 // Draw the child widget
211
212 if (scrollpane->child != NULL)
213 {
214 TXT_DrawWidget(scrollpane->child);
215 }
216
217 // Restore old clipping area.
218
219 TXT_PopClipArea();
220 }
221
TXT_ScrollPaneDestructor(TXT_UNCAST_ARG (scrollpane))222 static void TXT_ScrollPaneDestructor(TXT_UNCAST_ARG(scrollpane))
223 {
224 TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
225
226 if (scrollpane->child != NULL)
227 {
228 TXT_DestroyWidget(scrollpane->child);
229 }
230 }
231
TXT_ScrollPaneFocused(TXT_UNCAST_ARG (scrollpane),int focused)232 static void TXT_ScrollPaneFocused(TXT_UNCAST_ARG(scrollpane), int focused)
233 {
234 TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
235
236 // Whether the child is focused depends only on whether the scroll pane
237 // itself is focused. Pass through focus to the child.
238
239 if (scrollpane->child != NULL)
240 {
241 TXT_SetWidgetFocus(scrollpane->child, focused);
242 }
243 }
244
245 // Hack for tables - when browsing a table inside a scroll pane,
246 // automatically scroll the window to show the newly-selected
247 // item.
248
ShowSelectedWidget(txt_scrollpane_t * scrollpane)249 static void ShowSelectedWidget(txt_scrollpane_t *scrollpane)
250 {
251 txt_widget_t *selected;
252
253 selected = TXT_GetSelectedWidget(scrollpane->child);
254
255 // Scroll up or down?
256
257 if (selected->y <= scrollpane->widget.y)
258 {
259 scrollpane->y -= scrollpane->widget.y - selected->y;
260 }
261 else if ((signed) (selected->y + selected->h) >
262 (signed) (scrollpane->widget.y + scrollpane->h))
263 {
264 scrollpane->y += (selected->y + selected->h)
265 - (scrollpane->widget.y + scrollpane->h);
266 }
267
268 // Scroll left or right?
269
270 if (selected->x <= scrollpane->widget.x)
271 {
272 scrollpane->x -= scrollpane->widget.x - selected->x;
273 }
274 else if ((signed) (selected->x + selected->w) >
275 (signed) (scrollpane->widget.x + scrollpane->w))
276 {
277 scrollpane->x += (selected->x + selected->w)
278 - (scrollpane->widget.x + scrollpane->w);
279 }
280 }
281
282 // Another hack for tables - when scrolling in 'pages', the normal key press
283 // event does not provide children with enough information to know how far
284 // to move their selection to reach a new page. This function does so.
285 // Note that it *only* affects scrolling in pages, not with arrows!
286 // A side-effect of this, rather than 'pulling' the selection to fit within
287 // the new page, is that we will jump straight over ranges of unselectable
288 // items longer than a page, but that is also true of arrow-key scrolling.
289 // The other unfortunate effect of doing things this way is that page keys
290 // have no effect on tables _not_ in scrollpanes: not even home/end.
291
PageSelectedWidget(txt_scrollpane_t * scrollpane,int key)292 static int PageSelectedWidget(txt_scrollpane_t *scrollpane, int key)
293 {
294 int pagex = 0; // No page left/right yet, but some keyboards have them
295 int pagey = 0;
296
297 // Subtract one from the absolute page distance as this is slightly more
298 // intuitive: a page down first jumps to the bottom of the current page,
299 // then proceeds to scroll onwards.
300
301 switch (key)
302 {
303 case KEY_PGUP:
304 pagey = 1 - scrollpane->h;
305 break;
306
307 case KEY_PGDN:
308 pagey = scrollpane->h - 1;
309 break;
310
311 default: // We shouldn't even be in this function
312 return 0;
313 }
314
315 if (scrollpane->child->widget_class == &txt_table_class)
316 {
317 return TXT_PageTable(scrollpane->child, pagex, pagey);
318 }
319
320 return 0;
321 }
322
323 // Interpret arrow key presses as scroll commands
324
InterpretScrollKey(txt_scrollpane_t * scrollpane,int key)325 static int InterpretScrollKey(txt_scrollpane_t *scrollpane, int key)
326 {
327 int maxy;
328
329 switch (key)
330 {
331 case KEY_UPARROW:
332 if (scrollpane->y > 0)
333 {
334 --scrollpane->y;
335 return 1;
336 }
337 break;
338
339 case KEY_DOWNARROW:
340 if (scrollpane->y < FullHeight(scrollpane) - scrollpane->h)
341 {
342 ++scrollpane->y;
343 return 1;
344 }
345 break;
346
347 case KEY_LEFTARROW:
348 if (scrollpane->x > 0)
349 {
350 --scrollpane->x;
351 return 1;
352 }
353 break;
354
355 case KEY_RIGHTARROW:
356 if (scrollpane->x < FullWidth(scrollpane) - scrollpane->w)
357 {
358 ++scrollpane->x;
359 return 1;
360 }
361 break;
362
363 case KEY_PGUP:
364 if (scrollpane->y > 0)
365 {
366 scrollpane->y -= scrollpane->h;
367 if (scrollpane->y < 0)
368 {
369 scrollpane->y = 0;
370 }
371 return 1;
372 }
373 break;
374
375 case KEY_PGDN:
376 maxy = FullHeight(scrollpane) - scrollpane->h;
377 if (scrollpane->y < maxy)
378 {
379 scrollpane->y += scrollpane->h;
380 if (scrollpane->y > maxy)
381 {
382 scrollpane->y = maxy;
383 }
384 return 1;
385 }
386 break;
387
388 default:
389 break;
390 }
391
392 return 0;
393 }
394
TXT_ScrollPaneKeyPress(TXT_UNCAST_ARG (scrollpane),int key)395 static int TXT_ScrollPaneKeyPress(TXT_UNCAST_ARG(scrollpane), int key)
396 {
397 TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
398 int result;
399
400 result = 0;
401
402 if (scrollpane->child != NULL)
403 {
404 result = TXT_WidgetKeyPress(scrollpane->child, key);
405
406 // Gross hack - if we're scrolling in a menu with the keyboard,
407 // automatically move the scroll pane to show the new
408 // selected item.
409
410 if ((key == KEY_UPARROW || key == KEY_DOWNARROW
411 || key == KEY_LEFTARROW || key == KEY_RIGHTARROW
412 || key == KEY_PGUP || key == KEY_PGDN)
413 && scrollpane->child->widget_class == &txt_table_class)
414 {
415 if (PageSelectedWidget(scrollpane, key))
416 {
417 result = 1;
418 }
419
420 ShowSelectedWidget(scrollpane);
421 }
422
423 // If the child widget didn't use the keypress, we can see
424 // if it can be interpreted as a scrolling command.
425
426 if (result == 0)
427 {
428 result = InterpretScrollKey(scrollpane, key);
429 }
430 }
431
432 return result;
433 }
434
TXT_ScrollPaneMousePress(TXT_UNCAST_ARG (scrollpane),int x,int y,int b)435 static void TXT_ScrollPaneMousePress(TXT_UNCAST_ARG(scrollpane),
436 int x, int y, int b)
437 {
438 TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
439 int scrollbars;
440 int rel_x, rel_y;
441
442 scrollbars = NeedsScrollbars(scrollpane);
443
444 if (b == TXT_MOUSE_SCROLLUP)
445 {
446 if (scrollbars & SCROLLBAR_VERTICAL)
447 {
448 scrollpane->y -= 3;
449 }
450 else if (scrollbars & SCROLLBAR_HORIZONTAL)
451 {
452 scrollpane->x -= 3;
453 }
454
455 return;
456 }
457 else if (b == TXT_MOUSE_SCROLLDOWN)
458 {
459 if (scrollbars & SCROLLBAR_VERTICAL)
460 {
461 scrollpane->y += 3;
462 }
463 else if (scrollbars & SCROLLBAR_HORIZONTAL)
464 {
465 scrollpane->x += 3;
466 }
467
468 return;
469 }
470
471 rel_x = x - scrollpane->widget.x;
472 rel_y = y - scrollpane->widget.y;
473
474 // Click on the horizontal scrollbar?
475 if ((scrollbars & SCROLLBAR_HORIZONTAL) && rel_y == scrollpane->h)
476 {
477 if (rel_x == 0)
478 {
479 --scrollpane->x;
480 }
481 else if (rel_x == scrollpane->w - 1)
482 {
483 ++scrollpane->x;
484 }
485 else
486 {
487 int range = FullWidth(scrollpane) - scrollpane->w;
488 int bar_max = scrollpane->w - 3;
489
490 scrollpane->x = ((rel_x - 1) * range + (bar_max / 2)) / bar_max;
491 }
492
493 return;
494 }
495
496 // Click on the vertical scrollbar?
497 if ((scrollbars & SCROLLBAR_VERTICAL) && rel_x == scrollpane->w)
498 {
499 if (rel_y == 0)
500 {
501 --scrollpane->y;
502 }
503 else if (rel_y == scrollpane->h - 1)
504 {
505 ++scrollpane->y;
506 }
507 else
508 {
509 int range = FullHeight(scrollpane) - scrollpane->h;
510 int bar_max = scrollpane->h - 3;
511
512 scrollpane->y = ((rel_y - 1) * range + (bar_max / 2)) / bar_max;
513 }
514
515 return;
516 }
517
518 if (scrollpane->child != NULL)
519 {
520 TXT_WidgetMousePress(scrollpane->child, x, y, b);
521 }
522 }
523
TXT_ScrollPaneLayout(TXT_UNCAST_ARG (scrollpane))524 static void TXT_ScrollPaneLayout(TXT_UNCAST_ARG(scrollpane))
525 {
526 TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
527
528 SanityCheckScrollbars(scrollpane);
529
530 // The child widget takes the same position as the scroll pane
531 // itself, but is offset by the scroll position.
532
533 if (scrollpane->child != NULL)
534 {
535 scrollpane->child->x = scrollpane->widget.x - scrollpane->x;
536 scrollpane->child->y = scrollpane->widget.y - scrollpane->y;
537
538 TXT_LayoutWidget(scrollpane->child);
539 }
540 }
541
TXT_ScrollPaneSelectable(TXT_UNCAST_ARG (scrollpane))542 static int TXT_ScrollPaneSelectable(TXT_UNCAST_ARG(scrollpane))
543 {
544 TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
545
546 // If scroll bars are displayed, the scroll pane must be selectable
547 // so that we can use the arrow keys to scroll around.
548
549 if (NeedsScrollbars(scrollpane))
550 {
551 return 1;
552 }
553
554 // Otherwise, whether this is selectable depends on the child widget.
555
556 return TXT_SelectableWidget(scrollpane->child);
557 }
558
559 txt_widget_class_t txt_scrollpane_class =
560 {
561 TXT_ScrollPaneSelectable,
562 TXT_ScrollPaneSizeCalc,
563 TXT_ScrollPaneDrawer,
564 TXT_ScrollPaneKeyPress,
565 TXT_ScrollPaneDestructor,
566 TXT_ScrollPaneMousePress,
567 TXT_ScrollPaneLayout,
568 TXT_ScrollPaneFocused,
569 };
570
TXT_NewScrollPane(int w,int h,TXT_UNCAST_ARG (target))571 txt_scrollpane_t *TXT_NewScrollPane(int w, int h, TXT_UNCAST_ARG(target))
572 {
573 TXT_CAST_ARG(txt_widget_t, target);
574 txt_scrollpane_t *scrollpane;
575
576 scrollpane = malloc(sizeof(txt_scrollpane_t));
577 TXT_InitWidget(scrollpane, &txt_scrollpane_class);
578 scrollpane->w = w;
579 scrollpane->h = h;
580 scrollpane->x = 0;
581 scrollpane->y = 0;
582 scrollpane->child = target;
583 scrollpane->expand_w = w <= 0;
584 scrollpane->expand_h = h <= 0;
585
586 // Set parent pointer for inner widget.
587
588 target->parent = &scrollpane->widget;
589
590 return scrollpane;
591 }
592
593