[go: up one dir, main page]

File: scrollbar.c

package info (click to toggle)
libforms 1.2.3-1.7
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,904 kB
  • sloc: ansic: 97,669; sh: 11,156; makefile: 951
file content (607 lines) | stat: -rw-r--r-- 16,675 bytes parent folder | download | duplicates (4)
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
 *  This file is part of the XForms library package.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * \file scrollbar.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao
 *  All rights reserved.
 *
 *  Scrollbar
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "include/forms.h"
#include "flinternal.h"
#include "private/pscrollbar.h"


static void get_geom( FL_OBJECT * );

static void attrib_change( FL_OBJECT * );


#define IsVThin( t )  (    t == FL_VERT_THIN_SCROLLBAR    \
                        || t == FL_VERT_PLAIN_SCROLLBAR )
#define IsHThin( t )  (    t == FL_HOR_THIN_SCROLLBAR     \
                        || t == FL_HOR_PLAIN_SCROLLBAR )
#define IsThin( t )   ( IsVThin( t ) || IsHThin( t ) )


/***************************************
 ***************************************/

static int
handle_scrollbar( FL_OBJECT * obj,
                  int         event,
                  FL_Coord    mx   FL_UNUSED_ARG,
                  FL_Coord    my   FL_UNUSED_ARG,
                  int         key  FL_UNUSED_ARG,
                  void      * ev   FL_UNUSED_ARG )
{
    switch ( event )
    {
        case FL_ATTRIB :
        case FL_RESIZED :
            attrib_change( obj );
            get_geom( obj );
            break;

        case FL_DRAW :
            if ( IsThin( obj->type ) )
                fl_drw_box( obj->boxtype, obj->x, obj->y, obj->w, obj->h,
                            obj->col1, obj->bw );
            /* fall through */

        case FL_DRAWLABEL :
            fl_draw_object_label_outside( obj );
            break;

        case FL_FREEMEM :
            /* children will take care of themselves */
            fl_free( obj->spec );
            break;
    }

    return FL_RETURN_NONE;
}

#define IS_HORIZ( o )  ( ( o )->type & FL_HOR_FLAG )


/***************************************
 ***************************************/

static void
attrib_change( FL_OBJECT * obj )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    sp->slider->col1 = obj->col1;
    sp->slider->col2 = obj->col2;
    sp->up->col1     = sp->down->col1    = obj->col1;
    sp->up->col2     = sp->down->col2    = obj->col2;
    sp->up->boxtype  = sp->down->boxtype = sp->slider->boxtype = obj->boxtype;

    fli_notify_object( sp->slider, FL_ATTRIB );
}


/***************************************
 ***************************************/

static void
get_geom( FL_OBJECT * obj )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;
    FL_OBJECT *up     = sp->up,
              *down   = sp->down,
              *slider = sp->slider;
    int x = obj->x,
        y = obj->y,
        w = obj->w,
        h = obj->h;
    int absbw = FL_abs( obj->bw );
    int t = obj->type;

    if ( IS_HORIZ( obj ) )
    {
        down->x = x;
        up->x = x + w - h;
        up->y = down->y = y;
        down->h = up->h = h;
        down->w = up->w = FL_min( w, h );

        slider->x = x + h;
        slider->y = y;
        slider->h = h;

        if ( ( slider->w = w - 2 * up->w ) < 0 )
        {
            slider->w = up->w / 3;
            slider->x = x + up->w / 3;
        }
    }
    else
    {
        up->x = down->x = x;
        up->y = y;
        up->w = down->w = w;
        up->h = down->h = FL_min( w, h );

        slider->x = x;
        slider->y = y + up->h;
        slider->w = w;

        if ( ( slider->h = h - 2 * up->h ) < 0 )
        {
            slider->h = h / 3;
            slider->y = y + up->h / 3;
        }

        down->y = y + h - down->h;
    }

    up->bw     = obj->bw;
    down->bw   = obj->bw;
    slider->bw = obj->bw;

    if ( absbw > 2 )
        absbw--;

    if ( obj->bw > 0 )
        up->bw = down->bw = absbw;
    else
        up->bw = down->bw = -absbw;

    if ( IsThin( t ) )
    {
        absbw = IS_FLATBOX( obj->boxtype ) ? 1 : absbw;

        up->boxtype = down->boxtype = FL_NO_BOX;
        up->bw = down->bw = absbw;

        /* Due to slider double buffering we have to be completely clear of
           the scrollbar bounding box, otherwise the slider will wipe out the
           scrollbars bounding box */

        if ( IsVThin( t ) )
        {
            slider->x += absbw + 1;
            slider->w -= 2 * absbw + 2;
            slider->y -= absbw + ( absbw > 1 );
            slider->h += 2 * absbw + ( absbw > 1 );
        }
        else
        {
            slider->y += absbw + 1;
            slider->h -= 2 * absbw + 2;
            slider->x -= absbw + ( absbw > 1 );
            slider->w += 2 * absbw + ( absbw > 1 );
        }
    }

    fli_notify_object( slider, FL_RESIZED );
}


/***************************************
 * Callback for the slider in the scrollbar
 ***************************************/

static void
slider_cb( FL_OBJECT * obj,
           long        data  FL_UNUSED_ARG )
{
    FLI_SCROLLBAR_SPEC *sp = obj->parent->spec;

    if ( obj->returned & FL_RETURN_END )
        obj->parent->returned |= FL_RETURN_END;

    if (    obj->parent->how_return & FL_RETURN_END_CHANGED
         && obj->returned & FL_RETURN_END )
    {
        double nval = fl_get_slider_value( obj );

        if ( nval != sp->old_val )
            obj->parent->returned |= FL_RETURN_CHANGED;
        sp->old_val = nval;
    }
    else if ( obj->returned & FL_RETURN_CHANGED )
        obj->parent->returned |= FL_RETURN_CHANGED;
}


/***************************************
 * Callback for the buttons of the scrollbar
 ***************************************/

static void
button_cb( FL_OBJECT * obj,
           long        data )
{
    FLI_SCROLLBAR_SPEC *sp = obj->parent->spec;
    double ival = fl_get_slider_value( sp->slider ),
           nval = ival,
           slmax,
           slmin;

    /* Update the slider and get the new value */

    if ( obj->returned == FL_RETURN_TRIGGERED )
        obj->returned = FL_RETURN_END | FL_RETURN_CHANGED;

    if ( obj->returned & FL_RETURN_CHANGED )
    {
        fl_get_slider_bounds( sp->slider, &slmin, &slmax );

        if ( slmax > slmin )
            nval = ival + data * sp->increment;
        else
            nval = ival - data * sp->increment;

        fl_set_slider_value( sp->slider, nval );

        nval = fl_get_slider_value( sp->slider );
    }

    if ( obj->returned & FL_RETURN_END )
        obj->parent->returned |= FL_RETURN_END;

    /* If we're supposed to return only on end and change check if the
       slider value changed since interaction started, if we have to return
       on everty change check if if it changed this time round */

    if (    obj->parent->how_return & FL_RETURN_END_CHANGED
         && obj->returned & FL_RETURN_END )
    {
        if ( nval != sp->old_val )
        {
            obj->parent->returned |= FL_RETURN_CHANGED;
            sp->old_val = nval;
        }
    }
    else if ( ival != nval )
        obj->parent->returned |= FL_RETURN_CHANGED;
}


/***************************************
 ***************************************/

FL_OBJECT *
fl_create_scrollbar( int          type,
                     FL_Coord     x,
                     FL_Coord     y,
                     FL_Coord     w,
                     FL_Coord     h,
                     const char * l )
{
    FLI_SCROLLBAR_SPEC *sp;
    FL_OBJECT *obj;

    obj = fl_make_object( FL_SCROLLBAR, type, x, y, w, h, l, handle_scrollbar );

    obj->spec       = sp = fl_calloc( 1, sizeof *sp );
    obj->col1       = FL_COL1;
    obj->col2       = FL_COL1;
    obj->align      = FL_SCROLLBAR_ALIGN;
    obj->set_return = fl_set_scrollbar_return;

    if ( IsThin( type ) )
        obj->boxtype = FL_DOWN_BOX;
    else if ( type == FL_HOR_NICE_SCROLLBAR || type == FL_VERT_NICE_SCROLLBAR )
        obj->boxtype = FL_FRAME_BOX;
    else
        obj->boxtype = FL_UP_BOX;

    if ( IS_HORIZ( obj ) )
    {
        fl_set_object_resize( obj, FL_RESIZE_X );

        sp->up   = fl_create_scrollbutton( FL_TOUCH_BUTTON, 1, 1, 1, 1, "6" );
        sp->down = fl_create_scrollbutton( FL_TOUCH_BUTTON, 1, 1, 1, 1, "4" );
        fl_set_object_callback( sp->up, button_cb, 1 );
        fl_set_object_callback( sp->down, button_cb, -1 );
        fl_set_object_resize( sp->up, FL_RESIZE_NONE );
        fl_set_object_resize( sp->down, FL_RESIZE_NONE );

        if ( type == FL_HOR_SCROLLBAR )
            sp->slider = fl_create_slider( FL_HOR_BROWSER_SLIDER2,
                                           1, 1, 1, 1, "" );
        else if ( type == FL_HOR_THIN_SCROLLBAR )
            sp->slider = fl_create_slider( FL_HOR_THIN_SLIDER,
                                           1, 1, 1, 1, "" );
        else if ( type == FL_HOR_PLAIN_SCROLLBAR )
            sp->slider = fl_create_slider( FL_HOR_BASIC_SLIDER,
                                           1, 1, 1, 1, "" );
        else if ( type == FL_HOR_NICE_SCROLLBAR )
            sp->slider = fl_create_slider( FL_HOR_NICE_SLIDER2,
                                           1, 1, 1, 1, "" );

        fl_set_object_resize( sp->slider, FL_RESIZE_NONE );
    }
    else
    {
        fl_set_object_resize( obj, FL_RESIZE_Y );

        sp->up = fl_create_scrollbutton( FL_TOUCH_BUTTON, 1, 1, 1, 1, "8" );
        sp->down = fl_create_scrollbutton( FL_TOUCH_BUTTON, 1, 1, 1, 1, "2" );
        fl_set_object_callback( sp->up, button_cb, -1 );
        fl_set_object_callback( sp->down, button_cb, 1 );
        fl_set_object_resize( sp->up, FL_RESIZE_NONE );
        fl_set_object_resize( sp->down, FL_RESIZE_NONE );

        if ( type == FL_VERT_SCROLLBAR )
            sp->slider = fl_create_slider( FL_VERT_BROWSER_SLIDER2, 1, 1,
                                           1, 1, "" );
        else if ( type == FL_VERT_THIN_SCROLLBAR )
            sp->slider = fl_create_slider( FL_VERT_THIN_SLIDER, 1, 1,
                                           1, 1, "" );
        else if ( type == FL_VERT_PLAIN_SCROLLBAR )
            sp->slider = fl_create_slider( FL_VERT_BASIC_SLIDER, 1, 1,
                                           1, 1, "" );
        else if ( type == FL_VERT_NICE_SCROLLBAR )
            sp->slider = fl_create_slider( FL_VERT_NICE_SLIDER2, 1, 1,
                                           1, 1, "" );
        else
            M_err( "fl_create_scrollbar", "Unknown type %d", type );

        fl_set_object_resize( sp->slider, FL_RESIZE_NONE );
    }

    sp->increment = 0.02;
    fl_set_slider_increment( sp->slider, 5 * sp->increment, sp->increment );
    fl_set_object_callback( sp->slider, slider_cb, 0 );
    fl_set_slider_bounds( sp->slider, 0.0, 1.0 );

    sp->old_val = fl_get_slider_value( sp->slider );

    fl_add_child( obj, sp->slider );
    fl_add_child( obj, sp->down );
    fl_add_child( obj, sp->up );

    /* In older versions scrollbars and browsers didn't return to the
       application on e.g. fl_do_forms() but still a callback associated
       with the object got called. To emulate the old behaviour we have
       to set the return policy to default to FL_RETURN_NONE and only
       change that to FL_RETURN_CHANGED when a callback is installed
       (which is done in fl_set_object_callback()) */

#if ! USE_BWC_BS_HACK
    fl_set_object_return( obj, FL_RETURN_CHANGED );
#else
    fl_set_object_return( obj, FL_RETURN_NONE );
#endif

    return obj;
}


/*
 * User routines
 */

#define ISSCROLLBAR( o )  ( ( o ) && ( o )->objclass == FL_SCROLLBAR )


/***************************************
 ***************************************/

FL_OBJECT *
fl_add_scrollbar( int          type,
                  FL_Coord     x,
                  FL_Coord     y,
                  FL_Coord     w,
                  FL_Coord     h,
                  const char * l )
{
    FL_OBJECT *obj = fl_create_scrollbar( type, x, y, w, h, l );

    attrib_change( obj );
    get_geom( obj );
    fl_add_object( fl_current_form, obj );

    return obj;
}


/***************************************
 ***************************************/

double
fl_get_scrollbar_value( FL_OBJECT * obj )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    if ( ! ISSCROLLBAR( obj ) )
    {
        M_err( "fl_get_scrollbar_value", "%s not a scrollbar",
               obj ? obj->label : "Object" );
        return - HUGE_VAL;
    }

    return fl_get_slider_value( sp->slider );
}


/***************************************
 ***************************************/

void
fl_set_scrollbar_value( FL_OBJECT * obj,
                        double      val )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    if ( ! ISSCROLLBAR( obj ) )
    {
        M_err( "fl_set_scrollbar_value", "%s not a scrollbar",
               obj ? obj->label : "Object" );
        return;
    }

    sp->old_val = val;
    fl_set_slider_value( sp->slider, val );
}


/***************************************
 * Sets the size of the knob of the scrollbar
 * (the function name is a bit of misnomer)
 ***************************************/

void
fl_set_scrollbar_size( FL_OBJECT * obj,
                       double      val )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    fl_set_slider_size( sp->slider, val );
    get_geom( obj );
}


/***************************************
 * Sets the size of the knob of the scrollbar
 * (the function name is a bit of misnomer)
 ***************************************/

double
fl_get_scrollbar_size( FL_OBJECT * obj )
{
    return fl_get_slider_size( ( ( FLI_SCROLLBAR_SPEC * ) obj->spec )->slider );
}


/***************************************
 ***************************************/

void
fl_set_scrollbar_increment( FL_OBJECT * obj,
                            double      l,
                            double      r )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    fl_set_slider_increment( sp->slider, l, r );
    sp->increment = r;
}


/***************************************
 ***************************************/

void
fl_get_scrollbar_increment( FL_OBJECT * obj,
                            double    * a,
                            double    * b )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    fl_get_slider_increment( sp->slider, a, b );
}


/***************************************
 ***************************************/

void
fl_set_scrollbar_bounds( FL_OBJECT * obj,
                         double      b1,
                         double      b2 )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    if ( ! ISSCROLLBAR( obj ) )
    {
        M_err( "fl_set_scrollbar_bounds", "%s not a scrollbar",
               obj ? obj->label : "Object" );
        return;
    }

    fl_set_slider_bounds( sp->slider, b1, b2 );
}


/***************************************
 ***************************************/

void
fl_get_scrollbar_bounds( FL_OBJECT * obj,
                         double    * b1,
                         double    * b2 )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    fl_get_slider_bounds( sp->slider, b1, b2 );
}


/***************************************
 * Sets under which conditions the object is to be returned to the
 * application. This function should be regarded as for internal use
 * only and fl_set_object_return() should be used instead (which then
 * will call this function).
 ***************************************/

void
fl_set_scrollbar_return( FL_OBJECT    * obj,
                         unsigned int   when )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    if ( when & FL_RETURN_END_CHANGED )
        when &= ~ ( FL_RETURN_NONE | FL_RETURN_CHANGED );

    obj->how_return = when;

    fl_set_object_return( sp->slider, FL_RETURN_ALWAYS );
    fl_set_object_return( sp->up,     FL_RETURN_ALWAYS );
    fl_set_object_return( sp->down,   FL_RETURN_ALWAYS );

    /* We may need the value of the slider at this moment in the
       callback function... */

    sp->old_val = fl_get_slider_value( sp->slider );
}


/***************************************
 ***************************************/

void
fl_set_scrollbar_step( FL_OBJECT * obj,
                       double      step )
{
    FLI_SCROLLBAR_SPEC *sp = obj->spec;

    fl_set_slider_step( sp->slider, step );
}


/*
 * Local variables:
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */