[go: up one dir, main page]

File: button.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 (674 lines) | stat: -rw-r--r-- 18,209 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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
 *  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 button.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *
 *  All Buttons. Additional button class can be added via
 *  fl_add_button_class and fl_create_generic_button
 */

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

#include "include/forms.h"
#include "flinternal.h"
#include <sys/types.h>

#define ISTABBOX( t )   (    t == FL_TOPTAB_UPBOX                  \
                          || t == FL_SELECTED_TOPTAB_UPBOX         \
                          || t == FL_BOTTOMTAB_UPBOX               \
                          || t == FL_SELECTED_BOTTOMTAB_UPBOX    )

#define WITHIN( obj, mx, my )   (    ( mx ) >= ( obj )->x                 \
                                  && ( mx ) <  ( obj )->x + ( obj )->w    \
                                  && ( my ) >= ( obj )->y                 \
                                  && ( my ) <  ( obj )->y + ( obj )->h )


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

static void
free_pixmap( FL_BUTTON_STRUCT * sp )
{
    if ( sp->pixmap )
    {
        XFreePixmap( flx->display, sp->pixmap );
        sp->pixmap = None;
    }
}


/********** DRAWING *************/

/***************************************
 * Draws a button object
 ***************************************/

void
fli_draw_button( FL_OBJECT * obj )
{
    FL_Coord dh,
             dw,
             ww,
             absbw = FL_abs( obj->bw );
    int off2 = 0;
    FL_BUTTON_STRUCT *sp = obj->spec;
    FL_COLOR col = sp->val ? obj->col2 : obj->col1;

    if ( obj->belowmouse && obj->active )
    {
        if ( col == FL_BUTTON_COL1 )
            col = FL_BUTTON_MCOL1;
        else if ( col == FL_BUTTON_COL2 )
            col = FL_BUTTON_MCOL2;
    }

    if ( FL_IS_UPBOX( obj->boxtype ) && ( sp->val || sp->is_pushed ) )
        fl_drw_box( FL_TO_DOWNBOX( obj->boxtype ), obj->x, obj->y, obj->w,
                    obj->h, col, obj->bw );
    else
        fl_drw_box( obj->boxtype, obj->x, obj->y, obj->w, obj->h, col,
                    obj->bw );

    dh = FL_crnd( 0.6 * obj->h );
    dw = FL_crnd( FL_min( 0.6 * obj->w, dh ) );

    ww = FL_crnd( 0.75 * obj->h );
    if ( ww < dw + absbw + 1 + ( obj->bw > 0 ) )
        ww = dw + absbw + 1 + ( obj->bw > 0 );

    if ( obj->type == FL_RETURN_BUTTON )
    {
        fl_drw_text( 0, obj->x + obj->w - ww, FL_crnd( obj->y + 0.2 * obj->h ),
                     dw, dh, obj->lcol, 0, 0, "@returnarrow" );
        off2 = dw - 2;
    }

    if ( obj->type == FL_MENU_BUTTON && obj->boxtype == FL_UP_BOX )
    {
        int dbh = FL_max( absbw - 1, 1 );

        dw = FL_max( 0.11 * obj->w, 13 );
        dh = FL_max( 6 + (obj->bw > 0 ), obj->h * 0.1 );

        fl_drw_box( FL_UP_BOX, obj->x + obj->w - dw - absbw - 2,
                    obj->y + ( obj->h - dh ) / 2, dw, dh, obj->col1, -dbh );
        off2 = dw - 1;
    }

    if ( obj->type == FL_MENU_BUTTON || obj->type == FL_RETURN_BUTTON )
    {
        obj->w -= off2;
        fl_draw_object_label( obj );
        obj->w += off2;
    }
    else if ( obj->boxtype & FLI_BROKEN_BOX || ISTABBOX( obj->boxtype ) )
    {
        fl_set_text_clipping( obj->x + 3, obj->y, obj->w - 6, obj->h );
        fl_draw_object_label( obj );
        fl_unset_text_clipping( );
    }
    else
        fl_draw_object_label( obj );
}


/***************************************
 * All button classes differ only in the way they are drawn, so we
 * separate this info out from generic button handlers
 ***************************************/

#define MAX_BUTTON_CLASS 12

typedef struct
{
    FL_DrawButton    drawbutton;
    FL_CleanupButton cleanup;
    int              bclass;            /* button class */
} ButtonRec;


static ButtonRec how_draw[ MAX_BUTTON_CLASS ];


/***************************************
 * Look up a drawing function given a button class ID
 ***************************************/

static FL_DrawButton
lookup_drawfunc( int bclass )
{
    ButtonRec *db  = how_draw,
              *dbs = how_draw + MAX_BUTTON_CLASS;

    for ( ; db < dbs; db++ )
        if ( db->bclass == bclass )
            return db->drawbutton;

    return NULL;
}


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

static FL_CleanupButton
lookup_cleanupfunc( int bclass )
{
    ButtonRec *db  = how_draw,
              *dbs = how_draw + MAX_BUTTON_CLASS;

    for ( ; db < dbs; db++ )
        if ( db->bclass == bclass )
            return db->cleanup;

    return NULL;
}


/***************************************
 * Associates a button class with a drawing function
 ***************************************/

void
fl_add_button_class( int              bclass,
                     FL_DrawButton    drawit,
                     FL_CleanupButton cleanup )
{
    static int initialized;
    ButtonRec *db = how_draw,
              *dbs = how_draw + MAX_BUTTON_CLASS,
              *first_avail;

    if ( ! initialized )
    {
        for ( ; db < dbs; db++ )
            db->bclass = -1;

        initialized = 1;
    }

    for ( db = how_draw, first_avail = NULL; db < dbs; db++ )
        if ( db->bclass == bclass )
        {
            db->drawbutton = drawit;
            db->cleanup    = cleanup;
            return;
        }
        else if ( db->bclass < 0 && ! first_avail )
            first_avail = db;

    /* If we get here, the class is not defined yet */

    if ( first_avail )
    {
        first_avail->bclass     = bclass;
        first_avail->drawbutton = drawit;
        first_avail->cleanup    = cleanup;
    }
    else
        M_err( "fl_add_button_class", "Exceeding limit: %d", MAX_BUTTON_CLASS );
}


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

static void
wait_for_release( XKeyEvent * ev )
{
    KeySym ksm;

    if ( ( ksm = XLookupKeysym( ev, 0 ) ) == NoSymbol )
        return;

    while ( fl_keysym_pressed( ksm ) )
    {
        XSync( flx->display, 0 );
        fl_msleep( 15 );
    }
}


/***************************************
 * Handles an event for a button object
 ***************************************/

static int
handle_button( FL_OBJECT * obj,
               int         event,
               FL_Coord    mx,
               FL_Coord    my,
               int         key,
               void      * ev )
{
    static int oldval;
    int newval;
    FL_BUTTON_STRUCT *sp = obj->spec;
    FL_DrawButton drawit;
    FL_CleanupButton cleanup;
    int ret = FL_RETURN_NONE;

    switch ( event )
    {
        case FL_DRAW :
            sp->event = FL_DRAW;
            if (    obj->type != FL_HIDDEN_BUTTON
                 && obj->type != FL_HIDDEN_RET_BUTTON )
            {
                if ( ( drawit = lookup_drawfunc( obj->objclass ) ) )
                    drawit( obj );
                else
                    M_err( "handle_button", "Unknown button class: %d",
                           obj->objclass );
            }
            break;

        case FL_DRAWLABEL :
            sp->event = FL_DRAWLABEL;
            break;          /* TODO. Missing labels */

        case FL_LEAVE:
            /* FL_MENU_BUTTON objects never get a FL_RELEASE event,
               so we have to "fake" one */

            if ( obj->type == FL_MENU_BUTTON )
            {
                sp->event = FL_RELEASE;
                sp->is_pushed = 0;
                sp->val = 0;
            }
            /* fall through */

        case FL_ENTER :
            /* Keep active radio buttons from reacting */

            if ( obj->type == FL_RADIO_BUTTON && sp->val == 1 )
                obj->belowmouse = 0;

            sp->event = event;
            fl_redraw_object( obj );
            break;

        case FL_PUSH :
            /* Don't accept pushes for an already pushed button (e.g. if the
               user pushes another mouse button) or for mouse buttons the
               button isn't set up to react to */

            if (    sp->is_pushed )
                break;

            if (    key < FL_MBUTTON1
                 || key > FL_MBUTTON5
                 || ! sp->react_to[ key - 1 ] )
            {
                fli_int.pushobj = NULL;
                break;
            }

            sp->event = FL_PUSH;

            if ( obj->type == FL_RADIO_BUTTON )
            {
                obj->belowmouse = 0;
                sp->val = 1;
                fl_redraw_object( obj );
                return FL_RETURN_CHANGED | FL_RETURN_END;
            }

            oldval        = sp->val;
            sp->val       = ! sp->val;
            sp->is_pushed = 1;
            sp->mousebut  = key;
            sp->timdel    = 1;
            fl_redraw_object( obj );

            if ( obj->type == FL_MENU_BUTTON )
                ret |= FL_RETURN_END;

            if (    obj->type == FL_INOUT_BUTTON
                 || obj->type == FL_MENU_BUTTON
                 || obj->type == FL_TOUCH_BUTTON )
                ret |= FL_RETURN_CHANGED;
            break;

        case FL_MOTION :
            if (    obj->type == FL_RADIO_BUTTON
                 || obj->type == FL_INOUT_BUTTON
                 || obj->type == FL_MENU_BUTTON )
                break;

            newval = sp->val;

            if ( WITHIN( obj, mx, my ) )
            {
                obj->belowmouse = 1;
                if ( sp->react_to[ key - 1 ] )
                    newval = ! oldval;
            }
            else
            {
                obj->belowmouse = 0;
                if ( sp->react_to[ key - 1 ] )
                    newval = oldval;
            }

            if ( sp->val != newval )
            {
                sp->val = newval;
                fl_redraw_object( obj );
            }
            break;

        case FL_RELEASE :
            if ( key != sp->mousebut && obj->type != FL_RADIO_BUTTON )
            {
                fli_int.pushobj = obj;
                break;
            }

            sp->event = FL_RELEASE;
            sp->is_pushed = 0;

            if ( obj->type == FL_INOUT_BUTTON && ! WITHIN( obj, mx, my ) )
                obj->belowmouse = 0;

            if ( obj->type == FL_PUSH_BUTTON )
            {
                fl_redraw_object( obj );
                if ( sp->val != oldval )
                    ret |= FL_RETURN_END | FL_RETURN_CHANGED;
            }
            else if ( sp->val == 0 && obj->type != FL_MENU_BUTTON )
                fl_redraw_object( obj );
            else
            {
                sp->val = 0;
                fl_redraw_object( obj );

                if (    obj->type != FL_MENU_BUTTON
                     && obj->type != FL_TOUCH_BUTTON )
                    ret |= FL_RETURN_END | FL_RETURN_CHANGED;

                if ( obj->type == FL_TOUCH_BUTTON )
                    ret |= FL_RETURN_END;
            }

            break;

        case FL_UPDATE :                /* only FL_TOUCH_BUTTON receives it */
            sp->event = FL_UPDATE;
            if (    sp->val
                 && sp->timdel++ > 10
                 && ( sp->timdel & 1 ) == 0 )
                ret |= FL_RETURN_CHANGED;
            break;

        case FL_SHORTCUT :
            sp->event = FL_SHORTCUT;

            /* This is a horrible hack */

            if ( obj->type == FL_PUSH_BUTTON || obj->type == FL_RADIO_BUTTON )
            {
                sp->val = ! sp->val;
                obj->pushed = obj->type == FL_RADIO_BUTTON;
                fl_redraw_object( obj );
                wait_for_release( ev );
            }
            else if (    obj->type == FL_NORMAL_BUTTON
                      || obj->type == FL_RETURN_BUTTON )
            {
                int obl = obj->belowmouse;

                sp->val = obj->belowmouse = 1;
                fl_redraw_object( obj );
                wait_for_release( ev );
                sp->val = 0;
                obj->belowmouse = obl;
                fl_redraw_object( obj );
            }
            sp->mousebut = FL_SHORTCUT + key;
            ret |= FL_RETURN_END | FL_RETURN_CHANGED;
            break;

        case FL_FREEMEM :
            if ( ( cleanup = lookup_cleanupfunc( obj->objclass ) ) )
                cleanup( sp );
            free_pixmap( sp );
            fli_safe_free( obj->spec );
            break;
    }

    return ret;
}


/***************************************
 * Creates a (generic) button object
 ***************************************/

FL_OBJECT *
fl_create_generic_button( int          objclass,
                          int          type,
                          FL_Coord     x,
                          FL_Coord     y,
                          FL_Coord     w,
                          FL_Coord     h,
                          const char * label )
{
    FL_OBJECT *obj;
    FL_BUTTON_STRUCT *sp;
    int i;

    obj = fl_make_object( objclass, type, x, y, w, h, label, handle_button );
    if ( type == FL_RADIO_BUTTON )
        obj->radio = 1;

    if ( type == FL_RETURN_BUTTON || type == FL_HIDDEN_RET_BUTTON )
        fl_set_object_shortcut( obj, "^M", 0 );

    if ( type == FL_HIDDEN_BUTTON || type == FL_HIDDEN_RET_BUTTON )
        obj->boxtype = FL_NO_BOX;

    if ( obj->type == FL_TOUCH_BUTTON )
    {
        obj->want_update = 1;
        obj->how_return = FL_RETURN_CHANGED;
    }

    sp = obj->spec = fl_calloc( 1, sizeof *sp );

    sp->event     = FL_DRAW;
    sp->is_pushed = 0;
    sp->pixmap    = sp->mask = sp->focus_pixmap = sp->focus_mask = None;
    sp->cspecv    = NULL;
    sp-> filename = sp->focus_filename = NULL;
    sp->is_pushed = 0;
    sp->mousebut  = 0;

    /* Per default a button (unfortunately) reacts to all mouse buttons */

    for ( i = 0; i < 5; i++ )
        sp->react_to[ i ] = 1;

    if ( fli_cntl.buttonLabelSize )
        obj->lsize = fli_cntl.buttonLabelSize;

    return obj;
}


/***************************************
 * Sets the buttons state
 ***************************************/

void
fl_set_button( FL_OBJECT * obj,
               int         pushed )
{
    FL_BUTTON_STRUCT *sp = obj->spec;

    pushed = pushed ? 1 : 0;     /* button can only be on or off */

    /* Nothing to do if the new state is already what the button is set to */

    if ( sp->val == pushed )
        return;

    /* If this is a radio button to be shown as switched on unset other
       radio button in its group */

    if ( obj->type == FL_RADIO_BUTTON && pushed )
        fli_do_radio_push( obj, obj->x, obj->y, FL_MBUTTON1, NULL, 1 );

    /* Set new state and redraw the button */

    sp->val = pushed;
    fl_redraw_object( obj );
}


/***************************************
 * Returns the value of the button
 ***************************************/

int
fl_get_button( FL_OBJECT * obj )
{
    return ( ( FL_BUTTON_STRUCT * ) obj->spec )->val;
}


/***************************************
 * Returns the number of the last used mouse button.
 * fl_mouse_button will also return the mouse number
 ***************************************/

int
fl_get_button_numb( FL_OBJECT * obj )
{
    return ( ( FL_BUTTON_STRUCT * ) obj->spec )->mousebut;
}


/***************************************
 * Creates a button
 ***************************************/

FL_OBJECT *
fl_create_button( int          type,
                  FL_Coord     x,
                  FL_Coord     y,
                  FL_Coord     w,
                  FL_Coord     h,
                  const char * label )
{
    FL_OBJECT *obj;

    fl_add_button_class( FL_BUTTON, fli_draw_button, 0 );
    obj = fl_create_generic_button( FL_BUTTON, type, x, y, w, h, label );
    obj->boxtype = FL_BUTTON_BOXTYPE;
    obj->col1    = FL_BUTTON_COL1;
    obj->col2    = FL_BUTTON_COL2;
    obj->align   = FL_BUTTON_ALIGN;
    obj->lcol    = FL_BUTTON_LCOL;

    return obj;
}


/***************************************
 * Adds a button to the current form
 ***************************************/

FL_OBJECT *
fl_add_button( int          type,
               FL_Coord     x,
               FL_Coord     y,
               FL_Coord     w,
               FL_Coord     h,
               const char * label)
{
    FL_OBJECT *obj = fl_create_button( type, x, y, w, h, label );

    fl_add_object( fl_current_form, obj );
    return obj;
}


/***************************************
 * Function allows to set up to which mouse
 * buttons the button object will react.
 ***************************************/

void
fl_set_button_mouse_buttons( FL_OBJECT    * obj,
                             unsigned int   mouse_buttons )
{
    FL_BUTTON_STRUCT *sp = obj->spec;
    unsigned int i;

    for ( i = 0; i < 5; i++, mouse_buttons >>= 1 )
        sp->react_to[ i ] = mouse_buttons & 1;
}


/***************************************
 * Function returns a value via 'mouse_buttons', indicating
 * which mouse buttons the button object will react to.
 ***************************************/

void
fl_get_button_mouse_buttons( FL_OBJECT    * obj,
                             unsigned int * mouse_buttons )
{
    FL_BUTTON_STRUCT *sp;
    int i;
    unsigned int k;

    if ( ! obj )
    {
        M_err( "fl_get_button_mouse_buttons", "NULL object" );
        return;
    }

    if ( ! mouse_buttons )
        return;

    sp = obj->spec;

    *mouse_buttons = 0;
    for ( i = 0, k = 1; i < 5; i++, k <<= 1 )
        *mouse_buttons |= sp->react_to[ i ] ? k : 0;
}


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