File: handling.c

package info (click to toggle)
libforms 1.0.93sp1-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,548 kB
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (1561 lines) | stat: -rw-r--r-- 45,992 bytes parent folder | download | duplicates (2)
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
/*
 *  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/>.
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <ctype.h>
#include "include/forms.h"
#include "flinternal.h"
#include "global.h"


static void do_interaction_step( int );
static int get_next_event_or_idle( int,
                                   FL_FORM **,
                                   XEvent * );
static void handle_EnterNotify_event( FL_FORM * );
static void handle_LeaveNotify_event( void );
static void handle_MotionNotify_event( FL_FORM * );
static void handle_Expose_event( FL_FORM *,
                                 FL_FORM ** );
static void handle_ConfigureNotify_event( FL_FORM *,
                                          FL_FORM ** );
static int form_event_queued( XEvent *,
                              int );


/* Waiting time (in ms) for fl_check_forms() and fl_check_only_forms().
   Originally this value was 10 ms. */

#define SHORT_PAUSE   1


/* How frequently to generate FL_STEP event, in milli-seconds is set here.
 * These are modified if an idle callback exists */

static int delta_msec = FLI_TIMER_RES;
static XEvent st_xev;


extern void ( * fli_handle_signal )( void );       /* defined in signal.c */
extern int ( * fli_handle_clipboard )( void * );   /* defined in clipboard.c */

/* When set results in behaviour as in version 1.0.90 and before where
   clicking on a non-input object didn't make an input object lose focus
   shortly and thus no "end of editing" was signaled to the user */

static int end_event_for_input = FL_INPUT_END_EVENT_ALWAYS;


/***************************************
 * Function for switching between "old" and "new" input handling
 * when another non-input object is clicked on
 ***************************************/

int
fl_input_end_return_handling( int type )
{
    int old_end_event_for_input = end_event_for_input;

    end_event_for_input = type;
    return old_end_event_for_input;
}


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

static int
fli_XLookupString( XKeyEvent * xkey,
                   char      * buf,
                   int         buflen,
                   KeySym    * ks )
{
    int len = INT_MIN;

    if ( ! fli_context->xic )
        len = XLookupString( xkey, buf, buflen, ks, 0 );
    else
    {
        Status status;

        if ( XFilterEvent( ( XEvent * ) xkey, None ) )
        {
            *ks = NoSymbol;
            return 0;
        }

        len = XmbLookupString( fli_context->xic, xkey, buf, buflen, ks,
                               &status );

        if ( status == XBufferOverflow )
            len = -len;
    }

    return len;
}


/***************************************
 * Converts 'state' member of the 'xkey' member of an XEvent
 * into the corresponding mouse button number
 ***************************************/

static int
xmask2button( unsigned int mask )
{
    if ( mask & Button1Mask )
        return FL_LEFT_MOUSE;

    if ( mask & Button2Mask )
        return FL_MIDDLE_MOUSE;

    if ( mask & Button3Mask )
        return FL_RIGHT_MOUSE;

    if ( mask & Button4Mask )
        return FL_SCROLLUP_MOUSE;

    if ( mask & Button5Mask )
        return FL_SCROLLDOWN_MOUSE;

    return 0;
}


/***************************************
 * A radio object is pushed
 ***************************************/

void
fli_do_radio_push( FL_OBJECT * obj,
                   FL_Coord    x,
                   FL_Coord    y,
                   int         key,
                   void      * xev )
{
    FL_OBJECT *o = obj;

    if ( ! obj || ! obj->radio )
        return;

    /* If this radio button does not belong to any group we have to search
       the entire form, otherwise just treat the members of the group */

    if ( obj->group_id == 0 )
    {
        for ( o = obj->form->first; o; o = o->next )
            if (    o != obj
                 && o->radio
                 && o->group_id == 0
                 && fl_get_button( o ) )
            {
                fli_handle_object( o, FL_RELEASE, x, y, key, xev, 0 );
                break;
            }
    }
    else
    {
        while ( o->prev && o->prev->objclass != FL_BEGIN_GROUP )
            o = o->prev;

        for ( ; o && o->objclass != FL_END_GROUP; o = o->next )
            if ( o != obj && o->radio && fl_get_button( o ) )
            {
                fli_handle_object( o, FL_RELEASE, x, y, key, xev, 0 );
                break;
            }
    }

    fli_handle_object( obj, FL_PUSH, x, y, key, xev, 1 );
}


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

static int
do_shortcut( FL_FORM  * form,
             int        key,
             FL_Coord   x,
             FL_Coord   y,
             XEvent   * xev )
{
    int key1,
        key2;
    FL_OBJECT *obj;
    long *s;

    key1 = key2 = key;

    /* Check for ALT modifier key */

    if ( fl_keypressed( XK_Alt_L ) || fl_keypressed( XK_Alt_R ) )
    {
        if ( key < 256 )
        {
            key1 = FL_ALT_MASK + ( islower( ( int ) key ) ?
                                   toupper( ( int ) key ) : key );
            key2 = FL_ALT_MASK + key;
        }
        else
            key1 = key2 = FL_ALT_MASK + key;
    }

    M_info( "do_shortcut", "win = %ld key = %d %d %d",
            form->window, key, key1, key2 );

    /* Check if an object has this as a shortcut */

    for ( obj = form->first; obj; obj = obj->next )
    {
        if ( ! obj->shortcut || ! obj->active || ! obj->visible)
            continue;

        for ( s = obj->shortcut; *s; s++ )
        {
            if ( *s != key1 && *s != key2 )
                continue;

            if ( obj->objclass == FL_INPUT )
            {
                if ( obj != form->focusobj )
                {
                    fli_handle_object( form->focusobj, FL_UNFOCUS,
                                       x, y, 0, xev, 1 );
                    fli_handle_object( obj, FL_FOCUS, x, y, 0, xev, 1 );
                }
            }
            else
            {
                if ( obj->radio )
                    fli_do_radio_push( obj, x, y, FL_MBUTTON1, xev );

                XAutoRepeatOff( flx->display );
                if ( ! obj->radio )
                    fli_handle_object( obj, FL_SHORTCUT, x, y, key1, xev, 1 );
                fli_context->mouse_button = FL_SHORTCUT + key1;

                /* This is not exactly correct as shortcut might quit,
                   fl_finish() will restore the keyboard state */

                if ( fli_keybdcontrol.auto_repeat_mode == AutoRepeatModeOn )
                    XAutoRepeatOn( flx->display );
            }

            return 1;
        }
    }

    return 0;
}


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

int
fli_do_shortcut( FL_FORM  * form,
                 int        key,
                 FL_Coord   x,
                 FL_Coord   y,
                 XEvent   * xev )
{
    int ret = do_shortcut( form, key, x, y, xev );

    if ( ! ret )
    {
        if ( form->child )
            ret = do_shortcut( form->child, key, x, y, xev );
        if ( ! ret && form->parent )
            ret = do_shortcut( form->parent, key, x, y, xev );
    }

    return ret;
}


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

static void
handle_keyboard( FL_FORM  * form,
                 int        key,
                 FL_Coord   x,
                 FL_Coord   y,
                 void     * xev )
{
    FL_OBJECT *obj,
              *special;

    /* Always check shortcut first */

    if ( fli_do_shortcut( form, key, x, y, xev ) )
        return;

    /* Focus policy is done as follows: Input object has the highest
       priority. Next comes the object that wants special keys, finally
       followed by 'mouseobj', having the lowest proiority. */

    special = fli_find_first( form, FLI_FIND_KEYSPECIAL, 0, 0 );
    obj = special ?
          fli_find_object( special->next, FLI_FIND_KEYSPECIAL, 0, 0 ) : NULL;

    /* If two or more objects want keyboard input none will get it and
       keyboard input will go to mouseobj instead */

    if ( obj && obj != special )
        special = fli_int.mouseobj;

    if ( form->focusobj )
    {
        FL_OBJECT *focusobj = form->focusobj;

        /* Handle special keys first */

        if ( key > 255 )
        {
            if (    IsLeft( key )
                 || IsRight( key )
                 || IsHome( key )
                 || IsEnd( key ) )
                fli_handle_object( focusobj, FL_KEYPRESS, x, y, key, xev, 1 );
            else if (    (    IsUp( key )
                           || IsDown( key )
                           || IsPageUp( key )
                           || IsPageDown( key ) )
                      && focusobj->wantkey & FL_KEY_TAB )
                fli_handle_object( focusobj, FL_KEYPRESS, x, y, key, xev, 1 );
            else if ( special && special->wantkey & FL_KEY_SPECIAL )
            {
                /* Moving the cursor in input field that does not have focus
                   looks weird */

                if ( special->objclass != FL_INPUT )
                    fli_handle_object( special, FL_KEYPRESS,
                                       x, y, key, xev, 1 );
            }
            else if ( key == XK_BackSpace || key == XK_Delete )
                fli_handle_object( focusobj, FL_KEYPRESS, x, y, key, xev, 1 );
            return;
        }

        /* The <Tab> and <Return> keys move the focus to the next or previous
           input object (depending on <SHIFT> being pressed also, and for
           <Return> only if the current focus object hasn't set FL_KEY_TAB as
           it's the case for  multiline input objects) */

        if (    key == '\t'
             || ( key == '\r' && ! ( focusobj->wantkey & FL_KEY_TAB ) ) )
        {
            fli_handle_object( focusobj, FL_UNFOCUS, x, y, 0, xev, 1 );

            if ( ( ( XKeyEvent * ) xev )->state & fli_context->navigate_mask )
            {
                if ( ! ( obj = fli_find_object_backwards( focusobj->prev,
                                                          FLI_FIND_INPUT,
                                                          0, 0 ) ) )
                    obj = fli_find_last( form, FLI_FIND_INPUT, 0, 0 );
            }
            else                                      /* search forward */
            {
                if ( ! ( obj = fli_find_object( focusobj->next,
                                                FLI_FIND_INPUT, 0, 0 ) ) )
                        
                    obj = fli_find_first( form, FLI_FIND_INPUT, 0, 0 );
            }
                
            fli_handle_object( obj, FL_FOCUS, x, y, 0, xev, 1 );
        }
        else if ( focusobj->wantkey != FL_KEY_SPECIAL )
            fli_handle_object( focusobj, FL_KEYPRESS, x, y, key, xev, 1 );
        return;
    }

    /* Keyboard input is not wanted */

    if ( ! special || special->wantkey == 0 )
        return;

    /* Space is an exception for browser */

    if ( ( key > 255 || key == ' ' ) && special->wantkey & FL_KEY_SPECIAL )
        fli_handle_object( special, FL_KEYPRESS, x, y, key, xev, 1 );
    else if ( key < 255 && special->wantkey & FL_KEY_NORMAL )
        fli_handle_object( special, FL_KEYPRESS, x, y, key, xev, 1 );
    else if ( special->wantkey == FL_KEY_ALL )
        fli_handle_object( special, FL_KEYPRESS, x, y, key, xev, 1 );

#if FL_DEBUG >= ML_INFO1
    M_info( "handle_keyboard", "(%d %d) pushing %d to %s\n",
            x, y, key, special->label );
#endif
}


/***************************************
 * Updates a form according to an event
 ***************************************/

void
fli_handle_form( FL_FORM * form,
                 int       event,
                 int       key,
                 XEvent  * xev )
{
    FL_OBJECT *obj = NULL;
    FL_Coord x,
             y;

    if ( ! form || form->visible != FL_VISIBLE )
        return;

    if ( form->deactivated && event != FL_DRAW )
        return;

    if (    form->parent_obj
         && ! form->parent_obj->active
         && event != FL_DRAW )
        return;

    if ( event != FL_STEP )
        fli_set_form_window( form );

    if ( fli_int.query_age > 0 && fli_int.mouseform )
    {
        fl_get_form_mouse( fli_int.mouseform, &fli_int.mousex, &fli_int.mousey,
                           &fli_int.keymask );
        if ( event != FL_KEYPRESS )
            key = xmask2button( fli_int.keymask );
        fli_int.query_age = 0;
    }

    x = fli_int.mousex;
    y = fli_int.mousey;

    /* Except for step and draw events search for the object the event is
       for */

    if ( event != FL_STEP && event != FL_DRAW )
        obj = fli_find_last( form, FLI_FIND_MOUSE, x, y );

    switch ( event )
    {
        case FL_DRAW:       /* form must be redrawn */
            fli_redraw_form_using_xevent( form, key, xev );
            break;

        case FL_ENTER:      /* mouse did enter the form */
            fli_int.mouseobj = obj;
            fli_handle_object( fli_int.mouseobj, FL_ENTER, x, y, 0, xev, 1 );
            break;

        case FL_LEAVE:      /* mouse left the form */
            fli_handle_object( fli_int.mouseobj, FL_LEAVE, x, y, 0, xev, 1 );
            if ( fli_int.pushobj == fli_int.mouseobj )
                fli_int.pushobj = NULL;
            fli_int.mouseobj = NULL;
            break;

        case FL_PUSH:       /* mouse button was pushed inside the form */
            /* Change focus: If an input object has the focus make it lose it
               (and thus report changes) and then set the focus to either the
               object that got pushed (if it's an input object) or back to the
               original one. Then we have to recheck that the object the
               FL_PUSH was for is still active - it may have become deactivated
               due to the handler for the object that became unfocused! */

            if (    obj
                 && form->focusobj
                 && form->focusobj != obj
                 && ( obj->input || end_event_for_input ) )
            {
                FL_OBJECT *old_focusobj = form->focusobj;

                fli_handle_object( form->focusobj, FL_UNFOCUS,
                                   x, y, key, xev, 1 );

                if ( ! obj->input || ! obj->active )
                    fli_handle_object( old_focusobj, FL_FOCUS,
                                       x, y, key, xev, 1 );
            }

            if ( obj && obj->input && obj->active )
                fli_handle_object( obj, FL_FOCUS, x, y, key, xev, 1 );

            if ( form->focusobj )
                fli_int.keyform = form;

            if ( ! obj || ! obj->active )
                break;

            /* Radio button only get handled on button release, other objects
               get the button press unless focus is overriden */

            if (    ! obj->radio
                 && (    ! obj->input
                      || ( obj->input && obj->active && obj->focus ) ) )
            {
                fli_handle_object( obj, FL_PUSH, x, y, key, xev, 1 );
                fli_int.pushobj = obj;
            }
            else if ( obj->radio )
                fli_do_radio_push( obj, x, y, key, xev );
            break;

        case FL_RELEASE:        /* mouse button was released inside the form */
            if ( fli_int.pushobj )
            {
                obj = fli_int.pushobj;
                fli_int.pushobj = NULL;
                fli_handle_object( obj, FL_RELEASE, x, y, key, xev, 1 );

            }
            break;

        case FL_MOTION:          /* mouse position changed in the form */
            /* "Pushable" objects always get FL_MOTION events. Since there's
               no direct EnterNotify or LeaveNotify event for objects we
               "fake" them when an object gets entered or left. */

            if ( fli_int.pushobj != NULL )
                fli_handle_object( fli_int.pushobj, FL_MOTION, x, y, key,
                                   xev, 1 );
            else if ( obj != fli_int.mouseobj )
            {
                fli_handle_object( fli_int.mouseobj, FL_LEAVE, x, y, 0,
                                   xev, 1 );
                fli_handle_object( fli_int.mouseobj = obj, FL_ENTER,
                                   x, y, 0, xev, 1 );
            }

            /* Objects can declare that they want FL_MOTION events even
               though they're not "pushable" objects e.g. because they
               have some internal structure that depends on the mouse
               position (e.g. choice and counter objects). */

            if ( obj != fli_int.pushobj && obj && obj->want_motion )
                fli_handle_object( obj, FL_MOTION, x, y, key, xev, 1 );

            break;

        case FL_KEYPRESS:      /* key was pressed */
            handle_keyboard( form, key, x, y, xev );
            break;

        case FL_STEP:          /* simple step */
            obj = fli_find_first( form, FLI_FIND_AUTOMATIC, 0, 0 );

            if ( obj )
                fli_set_form_window( form );    /* set only if required */

            while ( obj )
            {
                fli_handle_object( obj, FL_STEP, x, y, 0, xev, 1 );
                obj = fli_find_object( obj->next, FLI_FIND_AUTOMATIC, 0, 0 );
            }
            break;

        case FL_UPDATE:
            /* "Pushable" objects may request an FL_UPDATE event by an
               artificial (but not very precise) timer.*/

            if ( fli_int.pushobj && fli_int.pushobj->want_update )
                fli_handle_object( fli_int.pushobj, FL_UPDATE, x, y, key,
                                   xev, 1 );
            break;

        case FL_MOVEORIGIN:
        case FL_OTHER:
            /* Need to dispatch it through all objects and monitor the status
               of forms as it may get closed */

            for ( obj = form->first; obj && form->visible == FL_VISIBLE;
                  obj = obj->next )
                if ( obj->visible )
                    fli_handle_object( obj, event, x, y, key, xev, 0 );
            break;
    }
}


/***************************************
 * Formevent is either FL_KEYPRESS or FL_KEYRELEASE
 ***************************************/

static void
do_keyboard( XEvent * xev,
             int      formevent )
{
    Window win = xev->xkey.window;
    KeySym keysym = 0;
    unsigned char keybuf[ 227 ];
    int kbuflen;

    fli_int.mousex    = xev->xkey.x;
    fli_int.mousey    = xev->xkey.y;
    fli_int.keymask   = xev->xkey.state;
    fli_int.query_age = 0;

    /* Before doing anything save the current modifier key for the handlers */

    if (    win
         && (    ! fli_int.keyform
              || fli_get_visible_forms_index( fli_int.keyform ) < 0 ) )
        fli_int.keyform = fl_win_to_form( win );

    /* Switch keyboard input only if different top-level form */

    if ( fli_int.keyform && fli_int.keyform->window != win )
    {
        M_warn( "do_keyboard", "pointer/keybd focus differ" );

        if (    fli_int.keyform->child
             && fli_int.keyform->child->window != win
             && fli_int.keyform->parent
             && fli_int.keyform->parent->window != win )
            fli_int.keyform = fl_win_to_form( win );
    }

    if ( ! fli_int.keyform )
        return;

    kbuflen = fli_XLookupString( ( XKeyEvent * ) xev, ( char * ) keybuf,
                                 sizeof keybuf, &keysym );

    if ( kbuflen < 0 )
    {
        if ( kbuflen != INT_MIN )
            M_err( "do_keyboard", "keyboad buffer overflow?" );
        else
            M_err( "do_keyboard", "fli_XLookupString failed?" );

        return;
    }

    /* Ignore modifier keys as they don't cause action and are taken care
       of by the lookupstring routine */

    if ( IsModifierKey( keysym ) )
        /* empty */ ;
    else if ( IsTab( keysym ) )
    {
        /* Fake a tab key, some systems shift+tab do not generate a tab */

        fli_handle_form( fli_int.keyform, formevent, '\t', xev );
    }
    else if ( IsCursorKey( keysym ) || kbuflen == 0 )
        fli_handle_form( fli_int.keyform, formevent, keysym, xev );
    else
    {
        unsigned char *ch;

        /* All regular keys, including mapped strings */

        for ( ch = keybuf; ch < keybuf + kbuflen && fli_int.keyform; ch++ )
            fli_handle_form( fli_int.keyform, formevent, *ch, xev );
    }
}


/***************************************
 * ClientMessage is intercepted if it's WM_DELETE_WINDOW
 ***************************************/

static void
handle_ClientMessage_event( FL_FORM * form,
                            void    * xev )
{
    XClientMessageEvent *xcm = xev;
    static Atom atom_protocol;
    static Atom atom_del_win = None;


    if ( ! atom_del_win )
    {
        atom_protocol = XInternAtom( xcm->display, "WM_PROTOCOLS", 0 );
        atom_del_win = XInternAtom( xcm->display, "WM_DELETE_WINDOW", 0 );
    }

    /* If delete top-level window, quit unless handlers are installed */

    if (    xcm->message_type == atom_protocol
         && ( Atom ) xcm->data.l[ 0 ] == atom_del_win )
    {
        if ( form->close_callback )
        {
            if (    form->close_callback( form, form->close_data ) != FL_IGNORE
                 && form->visible == FL_VISIBLE )
                fl_hide_form( form );

            if ( form->sort_of_modal )
                fl_activate_all_forms( );
        }
        else if ( fli_context->atclose )
        {
            if ( fli_context->atclose( form,
                                       fli_context->close_data ) != FL_IGNORE )
                exit( 1 );
        }
        else
            exit( 1 );
    }
    else    /* pump it through current form */
        fli_handle_form( form, FL_OTHER, 0, xev );
}


static int preemptive_consumed( FL_FORM *,
                                int,
                                XEvent * );

/***************************************
 * Given an X event check for which of our forms it is
 ***************************************/

FL_FORM *
fli_find_event_form( XEvent * xev )
{
    return fl_win_to_form( ( ( XAnyEvent * ) xev )->window );
}


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

const XEvent *
fl_last_event( void )
{
    return &st_xev;
}


static int ignored_fake_configure;


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

static int
button_is_really_down( void )
{
    FL_Coord x,
             y;
    unsigned int km;

    fl_get_mouse( &x, &y, &km );

    return button_down( km );
}


/***************************************
 * Handle all events in the queue and flush output buffer
 ***************************************/

void
fli_treat_interaction_events( int wait_io )
{
    XEvent xev;

    /* If no event is present output buffer will be flushed. If event
       exists XNextEvent in do_interaction() will flush the output buffer */

    do
        do_interaction_step( wait_io );
    while ( form_event_queued( &xev, QueuedAfterFlush ) );
}


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

static void
do_interaction_step( int wait_io )
{
    FL_FORM *evform = NULL;
    static FL_FORM *redraw_form = NULL;

    if ( ! get_next_event_or_idle( wait_io, &evform, &st_xev ) )
        return;

    /* Got an event for one of the forms */

#if FL_DEBUG >= ML_WARN
    if ( st_xev.type != MotionNotify || fli_cntl.debug > 2 )
        fli_xevent_name( "MainLoop", &st_xev );
#endif

    fli_compress_event( &st_xev, evform->compress_mask );

    fli_int.query_age++;

    /* Run user raw callbacks for events, we're done if we get told that
       we're not supposed to do anything else with the event */

    if ( preemptive_consumed( evform, st_xev.type, &st_xev ) )
        return;

    /* Otherwise we need to handle the event ourself... */

    switch ( st_xev.type )
    {
        case MappingNotify:
            XRefreshKeyboardMapping( ( XMappingEvent * ) &st_xev );
            break;

        case FocusIn:
            if ( evform->focusobj )
                fli_int.keyform = evform;

            if ( fli_context->xic )
                XSetICValues( fli_context->xic,
                              XNFocusWindow, st_xev.xfocus.window,
                              XNClientWindow, st_xev.xfocus.window,
                              ( char * ) NULL );
            break;

        case FocusOut:
            fli_int.keyform = NULL;
            break;

        case KeyPress:
            do_keyboard( &st_xev, FL_KEYPRESS );
            break;

        case KeyRelease:
            do_keyboard( &st_xev, FL_KEYRELEASE );
            break;

        case EnterNotify:
            handle_EnterNotify_event( evform );
            break;

        case LeaveNotify:
            handle_LeaveNotify_event( );
            break;

        case MotionNotify:
            handle_MotionNotify_event( evform );
            break;

        case ButtonPress:
            fli_int.mousex  = st_xev.xbutton.x;
            fli_int.mousey  = st_xev.xbutton.y;
            fli_int.keymask = st_xev.xbutton.state
                           | ( Button1Mask << ( st_xev.xbutton.button - 1 ) );
            fli_int.query_age = 0;

            fli_context->mouse_button = st_xev.xbutton.button;
            if ( metakey_down( fli_int.keymask ) && st_xev.xbutton.button == 2 )
                fli_print_version( 1 );
            else
                fli_handle_form( fli_int.mouseform, FL_PUSH,
                                 st_xev.xbutton.button, &st_xev );
            break;

        case ButtonRelease:
            fli_int.mousex  = st_xev.xbutton.x;
            fli_int.mousey  = st_xev.xbutton.y;
            fli_int.keymask = st_xev.xbutton.state
                          & ~ ( Button1Mask << ( st_xev.xbutton.button - 1 ) );
            fli_int.query_age = 0;

            fli_context->mouse_button = st_xev.xbutton.button;

            if ( fli_int.mouseform )
                fli_handle_form( fli_int.mouseform, FL_RELEASE,
                                 st_xev.xbutton.button, &st_xev );

            fli_int.mouseform = evform;
            break;

        case Expose:
            handle_Expose_event( evform, &redraw_form );
            break;

        case ConfigureNotify:
            handle_ConfigureNotify_event( evform, &redraw_form );
            break;

        case ClientMessage:
            handle_ClientMessage_event( evform, &st_xev );
            break;

        case DestroyNotify: /* only sub-form gets this due to parent destroy */
            fl_hide_form( evform );
            break;

        case SelectionClear:
        case SelectionRequest:
        case SelectionNotify:
            if ( ! fli_handle_clipboard || fli_handle_clipboard( &st_xev ) < 0 )
                fli_handle_form( evform, FL_OTHER, 0, &st_xev );
            break;

        default:
            fli_handle_form( evform, FL_OTHER, 0, &st_xev );
            break;
    }
}


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

void
fli_handle_idling( XEvent * xev,
                   long     msec,
                   int      do_idle_cb )
{
    int i;

    /* Sleep a bit while keeping a lookout for async IO events */

    fli_watch_io( fli_context->io_rec, msec );

    /* Deal with signals */

    if ( fli_handle_signal )
        fli_handle_signal( );

    /* Make sure we have an up-to-date set of data for the mouse position
       and the state of the keyboard and mouse buttons */

    if ( fli_int.query_age > 0 && fli_int.mouseform )
    {
        fl_get_form_mouse( fli_int.mouseform, &fli_int.mousex, &fli_int.mousey,
                           &fli_int.keymask );
        fli_int.query_age = 0;
        xev->xmotion.time = CurrentTime;
    }
    else
        xev->xmotion.time += msec;

    /* FL_UPDATE and automatic handlers as well as idle callbacks can expect
       a synthetic MotionNotify event, make it up, then call the handler */

    xev->type            = MotionNotify;
    xev->xany.window     = fli_int.mouseform ? fli_int.mouseform->window : None;
    xev->xany.send_event = 1;
    xev->xmotion.state   = fli_int.keymask;
    xev->xmotion.x       = fli_int.mousex;
    xev->xmotion.y       = fli_int.mousey;
    xev->xmotion.is_hint = 0;

    /* We need to send an FL_UPDATE while a mouse button is down to "pushable"
       objects that want it (currently touch button, slider, choice, textbox
       and counter objects) */

    if (    button_down( fli_int.keymask )
         && fli_int.pushobj
         && fli_int.pushobj->want_update
         && fli_int.mouseform )
        fli_handle_form( fli_int.mouseform, FL_UPDATE,
                         xmask2button( fli_int.keymask ), xev );

    /* Handle automatic tasks */

    if ( fli_int.auto_count )
        for ( i = 0; i < fli_int.formnumb; i++ )
            if ( fli_int.forms[ i ]->num_auto_objects )
                fli_handle_form( fli_int.forms[ i ], FL_STEP, 0, xev );

    /* If asked to also execute user idle callbacks */

    if (    do_idle_cb
         && fli_context->idle_rec
         && fli_context->idle_rec->callback )
        fli_context->idle_rec->callback( xev, fli_context->idle_rec->data );
}


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

static int
get_next_event_or_idle( int        wait_io,
                        FL_FORM ** form,
                        XEvent   * xev )
{
    static unsigned int cnt = 0;
    long msec;

    /* Timeouts should be as precise as possible, so check them each time
       round. Since they may dictate how long we're going to wait if there
       is no event determine how how much time we will have to wait now */

    if ( ! wait_io )
        msec = SHORT_PAUSE;
    else if (    fli_int.auto_count
              || fli_int.pushobj
              || fli_context->idle_rec )
        msec = delta_msec;
    else
        msec = FL_min( delta_msec * 3, 300 );

    if ( fli_context->timeout_rec )
        fli_handle_timeouts( &msec );

    /* Skip checking for an X event after 10 events, thus giving X events
       a 10:1 priority over async IO, UPDATE events, automatic handlers and
       idle callbacks etc. */

    if ( ++cnt % 11 && XEventsQueued( flx->display, QueuedAfterFlush ) )
    {
        XNextEvent( flx->display, xev );

        /* Find the form the event is for - if it's for none of "our" forms
           it must be for e.g. a canvas window and must be put on the internal
           event queue */

        if ( ( *form = fli_find_event_form( xev ) ) != NULL )
           return 1;

        /* Please note: we do event compression before the user ever sees the
           events. This is a bit questionable at least for mouse movements
           since a user may want to get all events (e.g. because s/he wants
           to draw something exactly following the mouse movements). If this
           is removed then care must be taken that in the mask for MotionNotify
           PointerMotionHintMask is *not* set (see the fli_xevent_to_mask()
           function in appwin.c) since that keeps most motion events from
           coming through! */

        fli_compress_event( xev,
                              ExposureMask
                            | PointerMotionMask
                            | ButtonMotionMask );
        fl_XPutBackEvent( xev );

        return 0;
    }

    cnt = 0;

    fli_handle_idling( &st_xev, msec, 1 );

    return 0;
}


/***************************************
 * Handling of EnterNotiy events
 ***************************************/

static void
handle_EnterNotify_event( FL_FORM * evform )
{
    Window win = st_xev.xany.window;

    fli_int.mousex    = st_xev.xcrossing.x;
    fli_int.mousey    = st_xev.xcrossing.y;
    fli_int.keymask   = st_xev.xcrossing.state;
    fli_int.query_age = 0;

    if (    button_down( fli_int.keymask )
         && st_xev.xcrossing.mode != NotifyUngrab )
        return;

    if ( fli_int.mouseform )
        fli_handle_form( fli_int.mouseform, FL_LEAVE,
                         xmask2button( fli_int.keymask ), &st_xev );

    if ( evform )
    {
        fli_int.mouseform = evform;

        /* This is necessary because the window might be un-managed. To be
           friendly to other applications, grab focus only if absolutely
           necessary */

        if (    ! fli_int.mouseform->deactivated
             && ! st_xev.xcrossing.focus && fli_int.unmanaged_count > 0 )
        {
            fli_check_key_focus( "EnterNotify", win );
            fl_winfocus( win );
        }

        fli_handle_form( fli_int.mouseform, FL_ENTER,
                         xmask2button( fli_int.keymask ), &st_xev );
    }
#if FL_DEBUG >= ML_DEBUG
    else
        M_err( "handle_EnterNotify_event", "Null form" );
#endif
}


/***************************************
 * Handling of LeaveNotiy events
 ***************************************/

static void
handle_LeaveNotify_event( void )
{
    fli_int.mousex    = st_xev.xcrossing.x;
    fli_int.mousey    = st_xev.xcrossing.y;
    fli_int.keymask   = st_xev.xcrossing.state;
    fli_int.query_age = 0;

    if (    button_down( fli_int.keymask )
         && st_xev.xcrossing.mode == NotifyNormal )
        return;

    /* olvwm sends LeaveNotify with NotifyGrab whenever button is clicked,
       ignore it. Due to Xpopup grab, (maybe Wm bug?), end grab can also
       generate this event. We can tell these two situations by doing a real
       button_down test (as opposed to relying on the keymask in event) */

    if ( st_xev.xcrossing.mode == NotifyGrab && button_is_really_down( ) )
        return;

    if ( ! fli_int.mouseform )
        return;

    fli_handle_form( fli_int.mouseform, FL_LEAVE,
                     xmask2button( fli_int.keymask ), &st_xev );
}


/***************************************
 * Handling of MotionNotify events
 ***************************************/

static void
handle_MotionNotify_event( FL_FORM * evform )
{
    Window win = st_xev.xany.window;

    fli_int.keymask   = st_xev.xmotion.state;
    fli_int.mousex    = st_xev.xmotion.x;
    fli_int.mousey    = st_xev.xmotion.y;
    fli_int.query_age = 0;

    if ( ! fli_int.mouseform )
    {
        M_warn( "handle_MotionNotify_event", "event win = %ld", win );
        return;
    }

    if ( fli_int.mouseform->window != win )
    {
        M_warn( "handle_MotionNotify_event", "mousewin = %ld event win = %ld",
                fli_int.mouseform->window, win );
        fli_int.mousex += evform->x - fli_int.mouseform->x;
        fli_int.mousey += evform->y - fli_int.mouseform->y;
    }

    fli_handle_form( fli_int.mouseform, FL_MOTION,
                     xmask2button( fli_int.keymask ), &st_xev );
}


/***************************************
 * Handling of Expose events
 ***************************************/

static void
handle_Expose_event( FL_FORM  * evform,
                     FL_FORM ** redraw_form )
{
    if ( ! evform )
        return;

    /* If 'redraw_form' is the same as 'evform' we actually got a
       ConfigureNotify before that isn't handled yet and the data for the
       Exposure event must be modified - set clipping to the complete area
       of the form since we got to redraw it completely. */

    if ( *redraw_form == evform )
    {
        st_xev.xexpose.x = 0;
        st_xev.xexpose.y = 0;
        st_xev.xexpose.width  = evform->w;
        st_xev.xexpose.height = evform->h;
        *redraw_form = NULL;
    }
    else
    {
        if ( st_xev.xexpose.x + st_xev.xexpose.width > evform->w )
            st_xev.xexpose.width = evform->w - st_xev.xexpose.x;
        if ( st_xev.xexpose.y + st_xev.xexpose.height > evform->h )
            st_xev.xexpose.height = evform->h - st_xev.xexpose.y;
    }

    fli_set_perm_clipping( st_xev.xexpose.x, st_xev.xexpose.y,
                           st_xev.xexpose.width, st_xev.xexpose.height );
    fl_set_clipping( st_xev.xexpose.x, st_xev.xexpose.y,
                     st_xev.xexpose.width, st_xev.xexpose.height );

    /* Run into trouble by ignoring configure notify */

    if ( ignored_fake_configure )
    {
        FL_Coord neww,
                 newh;

        M_warn( "handle_Expose_event", "Run into trouble - correcting it" );
        fl_get_winsize( evform->window, &neww, &newh );
        fli_scale_form( evform, ( double ) neww / evform->w,
                        ( double ) newh / evform->h );
        ignored_fake_configure = 0;
    }

    fli_handle_form( evform, FL_DRAW, 0, &st_xev );

    fli_unset_perm_clipping( );
    fl_unset_clipping( );
    fl_unset_text_clipping( );
}


/***************************************
 * Handling of ConfigureNotify events
 ***************************************/

static void
handle_ConfigureNotify_event( FL_FORM  * evform,
                              FL_FORM ** redraw_form )
{
    Window win = st_xev.xany.window;
    int old_w = evform->w;
    int old_h = evform->h;

    if ( ! evform )
        return;

    if ( ! st_xev.xconfigure.send_event )
        fl_get_winorigin( win, &evform->x, &evform->y );
    else
    {
        evform->x = st_xev.xconfigure.x;
        evform->y = st_xev.xconfigure.y;
        M_warn( "handle_ConfigureNotify_event", "WMConfigure:x = %d y = %d"
                "w = %d h = %d", evform->x, evform->y, st_xev.xconfigure.width,
                st_xev.xconfigure.height );
    }

    /* mwm sends bogus ConfigureNotify randomly without following up with a
       redraw event, but it does set send_event. The check is somewhat
       dangerous, use 'ignored_fake_configure' to make sure when we got expose
       we can respond correctly. The correct fix is always to get window
       geometry in Expose handler, but that has a two-way traffic overhead */

    ignored_fake_configure =    st_xev.xconfigure.send_event
                             && (    st_xev.xconfigure.width  != evform->w
                                  || st_xev.xconfigure.height != evform->h );

    /* Dragging a form across the screen changes its absolute x, y coords.
       Objects that themselves contain forms should ensure that they are up to
       date. */

    fli_handle_form( evform, FL_MOVEORIGIN, 0, &st_xev );

    if ( st_xev.xconfigure.send_event )
        return;

    /* Can't just set form->{w,h}. Need to take care of obj gravity */

    fli_scale_form( evform, ( double ) st_xev.xconfigure.width  / evform->w,
                    ( double ) st_xev.xconfigure.height / evform->h );

    /* If both the width and the height got smaller (or one got smaller and
       the other one remained unchanged) we're not going to get an Expose
       event, so we need to redraw the form. If only one of the lengths got
       smaller or remained unchanged while the other got larger the next
       (compressed) Expose event will only cover the added part. In this
       case store the forms address so on the next Expose event we receive
       for it its full area will be redrawn. */

    if ( evform->w <= old_w && evform->h <= old_h )
        fl_redraw_form( evform );
    else if ( ! ( evform->w > old_w && evform->h > old_h ) ) 
        *redraw_form = evform;
}


/***************************************
 * Checks all forms. Does not wait.
 ***************************************/

FL_OBJECT *
fl_check_forms( void )
{
    FL_OBJECT *obj;

    if ( ( obj = fli_object_qread( ) ) == NULL )
    {
        fli_treat_interaction_events( 0 );
        fli_treat_user_events( );
        obj = fli_object_qread( );
    }

    return obj;
}


/***************************************
 * Same as fl_check_forms() but never returns FL_EVENT.
 ***************************************/

FL_OBJECT *
fl_check_only_forms( void )
{
    FL_OBJECT *obj;

    if ( ( obj = fli_object_qread( ) ) == NULL )
    {
        fli_treat_interaction_events( 0 );
        obj = fli_object_qread( );
    }

    return obj;
}


/***************************************
 * Checks all forms and keeps checking as long as nothing happens.
 ***************************************/

FL_OBJECT *
fl_do_forms( void )
{
    FL_OBJECT *obj;

    while ( ! ( obj = fli_object_qread( ) ) )
    {
        fli_treat_interaction_events( 1 );
        fli_treat_user_events( );
    }

    return obj;
}


/***************************************
 * Same as fl_do_forms() but never returns FL_EVENT.
 ***************************************/

FL_OBJECT *
fl_do_only_forms( void )
{
    FL_OBJECT *obj;

    while ( ! ( obj = fli_object_qread( ) ) )
        fli_treat_interaction_events( 1 );

    if ( obj == FL_EVENT )
        M_warn( "fl_do_only_forms", "Shouldn't happen" );

    return obj;
}


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

static int
form_event_queued( XEvent * xev,
                   int      mode )
{
    if ( XEventsQueued( flx->display, mode ) )
    {
        XPeekEvent( flx->display, xev );
        return fli_find_event_form( xev ) != NULL;
    }

    return 0;
}


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

static int
preemptive_consumed( FL_FORM * form,
                     int       type,
                     XEvent  * xev )
{
    if ( ! form || ! form->evmask || form->deactivated )
        return 0;

    if (    ( form->evmask & FL_ALL_EVENT ) == FL_ALL_EVENT
         && form->all_callback )
        return form->all_callback( form, xev );

    switch ( type )
    {
        case ButtonPress:
            if (    form->evmask & ButtonPressMask
                 && form->push_callback )
                return form->push_callback( form, xev );
            break;

        case ButtonRelease:
            if (    form->evmask & ButtonReleaseMask
                 && form->push_callback )
                return form->push_callback( form, xev );
            break;

        case KeyPress:
            if (    form->evmask & KeyPressMask
                 && form->key_callback )
                return form->key_callback( form, xev );
            break;

        case KeyRelease:
            if (    form->evmask & KeyRelease
                 && form->key_callback )
                return form->key_callback( form, xev );
            break;

        case EnterNotify:
            if (    form->evmask & EnterWindowMask
                 && form->crossing_callback )
                return form->crossing_callback( form, xev );
            break;

        case LeaveNotify:
            if (    form->evmask & LeaveWindowMask
                 && form->crossing_callback )
                return form->crossing_callback( form, xev );
            break;

        case MotionNotify:
            if (    form->evmask & ( ButtonMotionMask | PointerMotionMask )
                 && form->motion_callback )
                return form->motion_callback( form, xev );
    }

    return 0;
}


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

long
fl_mouse_button( void )
{
    return fli_context->mouse_button;
}


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

FLI_TARGET *
fli_internal_init( void )
{
    static FLI_TARGET *default_flx;

    if ( ! default_flx )
        default_flx = fl_calloc( 1, sizeof *default_flx );

    return flx = default_flx;
}


/***************************************
 * fl_display is exposed to the outside world. Bad
 ***************************************/

void
fli_switch_target( FLI_TARGET * newtarget )
{
    flx = newtarget;
    fl_display = flx->display;
}


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

void
fli_restore_target( void )
{
    fli_internal_init( );
    fl_display = flx->display;
}


/***************************************
 * Currently only a single idle callback is support
 ***************************************/

static void
add_idle_callback( FL_APPEVENT_CB   cb,
                   void           * data )
{
    if ( ! fli_context->idle_rec )
    {
        fli_context->idle_rec = fl_malloc( sizeof *fli_context->io_rec );
        fli_context->idle_rec->next = NULL;
    }

    fli_context->idle_rec->callback = cb;
    fli_context->idle_rec->data = data;
}


/***************************************
 * Sets an idle callback
 ***************************************/

FL_APPEVENT_CB
fl_set_idle_callback( FL_APPEVENT_CB   callback,
                      void           * user_data )
{
    FL_APPEVENT_CB old =
                 fli_context->idle_rec ? fli_context->idle_rec->callback : NULL;

    add_idle_callback( callback, user_data );

    /* If we have idle callbacks, decrease the wait time */

    delta_msec = FLI_TIMER_RES * ( callback ? 0.8 : 1.0 );
    fli_context->idle_delta = delta_msec;

    return old;
}


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

void
fl_set_idle_delta( long delta )
{
    if ( delta < 0 )
        delta = FLI_TIMER_RES;
    else if ( delta == 0 )
        delta = FLI_TIMER_RES / 10;

    delta_msec = delta;
    fli_context->idle_delta = delta;
}


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