summaryrefslogtreecommitdiff
path: root/textscreen/txt_scrollpane.c
blob: 4f468ffe1f14ce524586461778e169e94f7a0cb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// Copyright(C) 2009 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "txt_scrollpane.h"
#include "txt_gui.h"
#include "txt_io.h"
#include "txt_main.h"
#include "txt_table.h"

#include "doomkeys.h"

#define SCROLLBAR_VERTICAL   (1 << 0)
#define SCROLLBAR_HORIZONTAL (1 << 1)

static int FullWidth(txt_scrollpane_t *scrollpane)
{
    if (scrollpane->child != NULL)
    {
        return scrollpane->child->w;
    }
    else
    {
        return 0;
    }
}

static int FullHeight(txt_scrollpane_t *scrollpane)
{
    if (scrollpane->child != NULL)
    {
        return scrollpane->child->h;
    }
    else
    {
        return 0;
    }
}

// Calculate which scroll bars the pane needs.

static int NeedsScrollbars(txt_scrollpane_t *scrollpane)
{
    int result;

    result = 0;

    if (FullWidth(scrollpane) > scrollpane->w)
    {
        result |= SCROLLBAR_HORIZONTAL;
    }
    if (FullHeight(scrollpane) > scrollpane->h)
    {
        result |= SCROLLBAR_VERTICAL;
    }

    return result;
}

// If a scrollbar isn't needed, the scroll position is reset.

static void SanityCheckScrollbars(txt_scrollpane_t *scrollpane)
{
    int scrollbars;
    int max_x, max_y;

    scrollbars = NeedsScrollbars(scrollpane);

    if ((scrollbars & SCROLLBAR_HORIZONTAL) == 0)
    {
        scrollpane->x = 0;
    }
    if ((scrollbars & SCROLLBAR_VERTICAL) == 0)
    {
        scrollpane->y = 0;
    }

    max_x = FullWidth(scrollpane) - scrollpane->w;
    max_y = FullHeight(scrollpane) - scrollpane->h;

    if (scrollpane->x < 0)
    {
        scrollpane->x = 0;
    }
    else if (scrollpane->x > max_x)
    {
        scrollpane->x = max_x;
    }

    if (scrollpane->y < 0)
    {
        scrollpane->y = 0;
    }
    else if (scrollpane->y > max_y)
    {
        scrollpane->y = max_y;
    }
}

static void TXT_ScrollPaneSizeCalc(TXT_UNCAST_ARG(scrollpane))
{
    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
    int scrollbars;

    if (scrollpane->child != NULL)
    {
        TXT_CalcWidgetSize(scrollpane->child);
    }

    // Expand as necessary (to ensure that no scrollbars are needed)?

    if (scrollpane->expand_w)
    {
        scrollpane->w = FullWidth(scrollpane);
    }
    if (scrollpane->expand_h)
    {
        scrollpane->h = FullHeight(scrollpane);
    }

    scrollpane->widget.w = scrollpane->w;
    scrollpane->widget.h = scrollpane->h;

    // If we have scroll bars, we need to expand slightly to
    // accomodate them. Eg. if we have a vertical scrollbar, we
    // need to be an extra character wide.

    scrollbars = NeedsScrollbars(scrollpane);

    if (scrollbars & SCROLLBAR_HORIZONTAL)
    {
        ++scrollpane->widget.h;
    }
    if (scrollbars & SCROLLBAR_VERTICAL)
    {
        ++scrollpane->widget.w;
    }
}

static void TXT_ScrollPaneDrawer(TXT_UNCAST_ARG(scrollpane))
{
    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
    int x1, y1, x2, y2;
    int scrollbars;

    // We set a clipping area of the scroll pane.

    x1 = scrollpane->widget.x,
    y1 = scrollpane->widget.y,
    x2 = x1 + scrollpane->w,
    y2 = y1 + scrollpane->h;

    scrollbars = NeedsScrollbars(scrollpane);

    if (scrollbars & SCROLLBAR_HORIZONTAL)
    {
        TXT_DrawHorizScrollbar(x1,
                               y1 + scrollpane->h,
                               scrollpane->w,
                               scrollpane->x,
                               FullWidth(scrollpane) - scrollpane->w);
    }

    if (scrollbars & SCROLLBAR_VERTICAL)
    {
        TXT_DrawVertScrollbar(x1 + scrollpane->w,
                              y1,
                              scrollpane->h,
                              scrollpane->y,
                              FullHeight(scrollpane) - scrollpane->h);
    }

    TXT_PushClipArea(x1, x2, y1, y2);

    // Draw the child widget

    if (scrollpane->child != NULL)
    {
        TXT_DrawWidget(scrollpane->child);
    }

    // Restore old clipping area.

    TXT_PopClipArea();
}

static void TXT_ScrollPaneDestructor(TXT_UNCAST_ARG(scrollpane))
{
    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);

    if (scrollpane->child != NULL)
    {
        TXT_DestroyWidget(scrollpane->child);
    }
}

static void TXT_ScrollPaneFocused(TXT_UNCAST_ARG(scrollpane), int focused)
{
    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);

    // Whether the child is focused depends only on whether the scroll pane
    // itself is focused. Pass through focus to the child.

    if (scrollpane->child != NULL)
    {
        TXT_SetWidgetFocus(scrollpane->child, focused);
    }
}

// Hack for tables - when browsing a table inside a scroll pane,
// automatically scroll the window to show the newly-selected
// item.

static void ShowSelectedWidget(txt_scrollpane_t *scrollpane)
{
    txt_widget_t *selected;

    selected = TXT_GetSelectedWidget(scrollpane->child);

    // Scroll up or down?

    if (selected->y <= scrollpane->widget.y)
    {
        scrollpane->y -= scrollpane->widget.y - selected->y;
    }
    else if ((signed) (selected->y + selected->h) >
             (signed) (scrollpane->widget.y + scrollpane->h))
    {
        scrollpane->y += (selected->y + selected->h)
                       - (scrollpane->widget.y + scrollpane->h);
    }

    // Scroll left or right?

    if (selected->x <= scrollpane->widget.x)
    {
        scrollpane->x -= scrollpane->widget.x - selected->x;
    }
    else if ((signed) (selected->x + selected->w) >
             (signed) (scrollpane->widget.x + scrollpane->w))
    {
        scrollpane->x += (selected->x + selected->w)
                       - (scrollpane->widget.x + scrollpane->w);
    }
}

// Another hack for tables - when scrolling in 'pages', the normal key press
// event does not provide children with enough information to know how far
// to move their selection to reach a new page. This function does so.
// Note that it *only* affects scrolling in pages, not with arrows!
// A side-effect of this, rather than 'pulling' the selection to fit within
// the new page, is that we will jump straight over ranges of unselectable
// items longer than a page, but that is also true of arrow-key scrolling.
// The other unfortunate effect of doing things this way is that page keys
// have no effect on tables _not_ in scrollpanes: not even home/end.

static int PageSelectedWidget(txt_scrollpane_t *scrollpane, int key)
{
    int pagex = 0; // No page left/right yet, but some keyboards have them
    int pagey = 0;

    // Subtract one from the absolute page distance as this is slightly more
    // intuitive: a page down first jumps to the bottom of the current page,
    // then proceeds to scroll onwards.

    switch (key)
    {
        case KEY_PGUP:
            pagey = 1 - scrollpane->h;
            break;

        case KEY_PGDN:
            pagey = scrollpane->h - 1;
            break;

        default: // We shouldn't even be in this function
            return 0;
    }

    if (scrollpane->child->widget_class == &txt_table_class)
    {
        return TXT_PageTable(scrollpane->child, pagex, pagey);
    }

    return 0;
}

// Interpret arrow key presses as scroll commands

static int InterpretScrollKey(txt_scrollpane_t *scrollpane, int key)
{
    int maxy;

    switch (key)
    {
        case KEY_UPARROW:
            if (scrollpane->y > 0)
            {
                --scrollpane->y;
                return 1;
            }
            break;

        case KEY_DOWNARROW:
            if (scrollpane->y < FullHeight(scrollpane) - scrollpane->h)
            {
                ++scrollpane->y;
                return 1;
            }
            break;

        case KEY_LEFTARROW:
            if (scrollpane->x > 0)
            {
                --scrollpane->x;
                return 1;
            }
            break;

        case KEY_RIGHTARROW:
            if (scrollpane->x < FullWidth(scrollpane) - scrollpane->w)
            {
                ++scrollpane->x;
                return 1;
            }
            break;

        case KEY_PGUP:
            if (scrollpane->y > 0)
            {
                scrollpane->y -= scrollpane->h;
                if (scrollpane->y < 0)
                {
                    scrollpane->y = 0;
                }
                return 1;
            }
            break;

        case KEY_PGDN:
            maxy = FullHeight(scrollpane) - scrollpane->h;
            if (scrollpane->y < maxy)
            {
                scrollpane->y += scrollpane->h;
                if (scrollpane->y > maxy)
                {
                    scrollpane->y = maxy;
                }
                return 1;
            }
            break;

        default:
            break;
    }

    return 0;
}

static int TXT_ScrollPaneKeyPress(TXT_UNCAST_ARG(scrollpane), int key)
{
    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
    int result;

    result = 0;

    if (scrollpane->child != NULL)
    {
        result = TXT_WidgetKeyPress(scrollpane->child, key);

        // Gross hack - if we're scrolling in a menu with the keyboard,
        // automatically move the scroll pane to show the new
        // selected item.

        if (scrollpane->child->widget_class == &txt_table_class
         && (key == KEY_UPARROW || key == KEY_DOWNARROW
          || key == KEY_LEFTARROW || key == KEY_RIGHTARROW
          || key == KEY_PGUP || key == KEY_PGDN))
        {
            if (PageSelectedWidget(scrollpane, key))
            {
                result = 1;
            }

            ShowSelectedWidget(scrollpane);
        }

        // If the child widget didn't use the keypress, we can see 
        // if it can be interpreted as a scrolling command.

        if (result == 0)
        {
            result = InterpretScrollKey(scrollpane, key);
        }
    }

    return result;
}

static void TXT_ScrollPaneMousePress(TXT_UNCAST_ARG(scrollpane),
                                     int x, int y, int b)
{
    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);
    int scrollbars;
    int rel_x, rel_y;

    scrollbars = NeedsScrollbars(scrollpane);

    if (b == TXT_MOUSE_SCROLLUP)
    {
        if (scrollbars & SCROLLBAR_VERTICAL)
        {
            scrollpane->y -= 3;
        }
        else if (scrollbars & SCROLLBAR_HORIZONTAL)
        {
            scrollpane->x -= 3;
        }

        return;
    }
    else if (b == TXT_MOUSE_SCROLLDOWN)
    {
        if (scrollbars & SCROLLBAR_VERTICAL)
        {
            scrollpane->y += 3;
        }
        else if (scrollbars & SCROLLBAR_HORIZONTAL)
        {
            scrollpane->x += 3;
        }

        return;
    }

    rel_x = x - scrollpane->widget.x;
    rel_y = y - scrollpane->widget.y;

    // Click on the horizontal scrollbar?
    if ((scrollbars & SCROLLBAR_HORIZONTAL) && rel_y == scrollpane->h)
    {
        if (rel_x == 0)
        {
            --scrollpane->x;
        }
        else if (rel_x == scrollpane->w - 1)
        {
            ++scrollpane->x;
        }
        else
        {
            int range = FullWidth(scrollpane) - scrollpane->w;
            int bar_max = scrollpane->w - 3;

            scrollpane->x = ((rel_x - 1) * range + (bar_max / 2)) / bar_max;
        }

        return;
    }

    // Click on the vertical scrollbar?
    if ((scrollbars & SCROLLBAR_VERTICAL) && rel_x == scrollpane->w)
    {
        if (rel_y == 0)
        {
            --scrollpane->y;
        }
        else if (rel_y == scrollpane->h - 1)
        {
            ++scrollpane->y;
        }
        else
        {
            int range = FullHeight(scrollpane) - scrollpane->h;
            int bar_max = scrollpane->h - 3;

            scrollpane->y = ((rel_y - 1) * range + (bar_max / 2)) / bar_max;
        }

        return;
    }

    if (scrollpane->child != NULL)
    {
        TXT_WidgetMousePress(scrollpane->child, x, y, b);
    }
}

static void TXT_ScrollPaneLayout(TXT_UNCAST_ARG(scrollpane))
{
    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);

    SanityCheckScrollbars(scrollpane);

    // The child widget takes the same position as the scroll pane
    // itself, but is offset by the scroll position.

    if (scrollpane->child != NULL)
    {
        scrollpane->child->x = scrollpane->widget.x - scrollpane->x;
        scrollpane->child->y = scrollpane->widget.y - scrollpane->y;

        TXT_LayoutWidget(scrollpane->child);
    }
}

static int TXT_ScrollPaneSelectable(TXT_UNCAST_ARG(scrollpane))
{
    TXT_CAST_ARG(txt_scrollpane_t, scrollpane);

    // If scroll bars are displayed, the scroll pane must be selectable
    // so that we can use the arrow keys to scroll around.

    if (NeedsScrollbars(scrollpane))
    {
        return 1;
    }

    // Otherwise, whether this is selectable depends on the child widget.

    return TXT_SelectableWidget(scrollpane->child);
}

txt_widget_class_t txt_scrollpane_class =
{
    TXT_ScrollPaneSelectable,
    TXT_ScrollPaneSizeCalc,
    TXT_ScrollPaneDrawer,
    TXT_ScrollPaneKeyPress,
    TXT_ScrollPaneDestructor,
    TXT_ScrollPaneMousePress,
    TXT_ScrollPaneLayout,
    TXT_ScrollPaneFocused,
};

txt_scrollpane_t *TXT_NewScrollPane(int w, int h, TXT_UNCAST_ARG(target))
{
    TXT_CAST_ARG(txt_widget_t, target);
    txt_scrollpane_t *scrollpane;

    scrollpane = malloc(sizeof(txt_scrollpane_t));
    TXT_InitWidget(scrollpane, &txt_scrollpane_class);
    scrollpane->w = w;
    scrollpane->h = h;
    scrollpane->x = 0;
    scrollpane->y = 0;
    scrollpane->child = target;
    scrollpane->expand_w = w <= 0;
    scrollpane->expand_h = h <= 0;

    // Set parent pointer for inner widget.

    target->parent = &scrollpane->widget;

    return scrollpane;
}