[go: up one dir, main page]

File: browser.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 (1427 lines) | stat: -rw-r--r-- 36,883 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
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
/*
 *  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 browser.c
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
 *  All rights reserved.
 *
 *   Browser composite.
 *   scrollbar redrawing can be further optimized.
 */


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

#include "include/forms.h"
#include "flinternal.h"
#include "private/pbrowser.h"
#include "private/flvasprintf.h"


/***************************************
 * Some attribue of the object changed, we better recalculate how
 * it should oook like
 ***************************************/

static void
attrib_change( FL_OBJECT * ob )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    /* Text box stuff */

    sp->tb->x = ob->x;
    sp->tb->y = ob->y;

    fli_set_object_visibility( sp->tb, FL_VISIBLE );
    sp->tb->input = ob->input;

    sp->tb->type    = ob->type;
    sp->tb->boxtype = ob->boxtype;
    sp->tb->lcol    = ob->lcol;
    sp->tb->col1    = ob->col1;
    sp->tb->col2    = ob->col2;
    sp->tb->bw      = ob->bw;
    fli_notify_object( sp->tb, FL_RESIZED );

    /* Scrollbars */

    if (    ob->boxtype == FL_DOWN_BOX
         && sp->hsl->type == FL_HOR_NICE_SCROLLBAR )
    {
        sp->hsl->boxtype = FL_FRAME_BOX;
        sp->vsl->boxtype = FL_FRAME_BOX;
    }
    else if (    ob->boxtype == FL_DOWN_BOX
              && sp->hsl->type == FL_HOR_SCROLLBAR )
    {
        sp->hsl->boxtype = FL_UP_BOX;
        sp->vsl->boxtype = FL_UP_BOX;
    }
    else
    {
        sp->hsl->boxtype = ob->boxtype;
        sp->vsl->boxtype = ob->boxtype;
    }

    sp->hsl->bw = sp->vsl->bw = ob->bw;

    if ( ! sp->user_set && ob->boxtype != FL_DOWN_BOX )
        sp->vw = sp->vw_def = sp->hh = sp->hh_def =
                                          fli_get_default_scrollbarsize( ob );
}


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

static void
get_geometry( FL_OBJECT * obj )
{
    FLI_BROWSER_SPEC *comp = obj->spec;
    FL_OBJECT *tb = comp->tb;
    FLI_TBOX_SPEC *sp = comp->tb->spec;
    int h_on = comp->h_on,
        v_on = comp->v_on;
    double old_xrel = fli_tbox_get_rel_xoffset( tb );
    double old_yrel = fli_tbox_get_rel_yoffset( tb );

    comp->hh = comp->vw = 0;
    comp->h_on = comp->v_on = 0;

    tb->w = obj->w;
    tb->h = obj->h;
    fli_tbox_recalc_area( tb );

    /* Check if we need a vertical slider */

    if (    ( sp->max_height > sp->h && comp->v_pref != FL_OFF )
         || comp->v_pref == FL_ON )
    {
        comp->v_on  = 1;
        comp->vw    = comp->vw_def;

        tb->w      -= comp->vw;
        fli_tbox_recalc_area( tb );
    }

    /* Check if we need a horizontal slider */

    if (    ( sp->max_width > sp->w && comp->h_pref != FL_OFF )
         || comp->h_pref == FL_ON )
    {
        comp->h_on  = 1;
        comp->hh    = comp->hh_def;

        tb->h      -= comp->hh;
        fli_tbox_recalc_area( tb );
    }

    /* Due to the addition of a horizontal slider also a vertical slider
       may now be needed, so recheck for this possibility */

    if ( ! comp->v_on && sp->max_height > sp->h && comp->v_pref != FL_OFF )
    {
        comp->v_on  = 1;
        comp->vw    = comp->vw_def;

        tb->w      -= comp->vw;
        fli_tbox_recalc_area( tb );
    }

    if ( comp->v_on )
    {
        comp->vsl->x = obj->x + obj->w - comp->vw;
        comp->vsl->y = obj->y;
        comp->vsl->w = comp->vw;
        comp->vsl->h = obj->h - comp->hh;
        fli_notify_object( comp->vsl, FL_RESIZED );

        comp->vval  = old_yrel;
        comp->vsize = comp->vinc1 = ( double ) sp->h / sp->max_height;
        comp->vinc2 = ( double ) sp->def_height / sp->max_height;
    }
    else
    {
        comp->vsize = 1.0;
        comp->vval  = 0.0;
    }

    if ( comp->h_on )
    {
        comp->hsl->x = obj->x;
        comp->hsl->y = obj->y + obj->h - comp->hh;
        comp->hsl->w = obj->w - comp->vw;
        comp->hsl->h = comp->hh;
        fli_notify_object( comp->hsl, FL_RESIZED );

        comp->hval  = old_xrel;
        comp->hsize = ( double ) sp->w / sp->max_width;
        comp->hinc1 = ( 8.0 * sp->def_height ) / sp->max_width;
        comp->hinc2 = ( sp->def_height - 2.0 ) / sp->max_width;
    }
    else
    {
        comp->hsize = 1.0;
        comp->hval  = 1.0;
    }

    comp->dead_area = comp->h_on && comp->v_on;
    fli_set_object_visibility( comp->hsl, comp->h_on );
    fli_set_object_visibility( comp->vsl, comp->v_on );

    comp->attrib = 1;

    sp->no_redraw = 1;

    if ( comp->v_on )
    {
        comp->vval = fli_tbox_set_rel_yoffset( tb, comp->vval );
        fl_set_scrollbar_value( comp->vsl, comp->vval );
        fl_set_scrollbar_size( comp->vsl, comp->vsize );
    }

    if ( comp->h_on )
    {
        comp->hval = fli_tbox_set_rel_xoffset( tb, comp->hval );
        fl_set_scrollbar_value( comp->hsl, comp->hval );
        fl_set_scrollbar_size( comp->hsl, comp->hsize );    
    }

    sp->no_redraw = 0;

    if ( h_on != comp->h_on || v_on != comp->v_on )
        fli_notify_object( tb, FL_RESIZED );
}


/***************************************
 * The "dead area" is the small square in the lower right hand corner
 * of the browser (beside the horizontal slider and below the vertical
 * one) that shows up when both sliders are displayed.
 ***************************************/

static void
draw_dead_area( FL_OBJECT * obj )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    if ( FL_ObjWin( sp->tb ) )
    {
        fl_winset( FL_ObjWin( sp->tb ) );
        fl_drw_box( FL_FLAT_BOX, obj->x + obj->w - sp->vw,
                    obj->y + obj->h - sp->hh, sp->vw, sp->hh,
                    sp->vsl->col1, 1 );
    }
}


/***************************************
 * Called for events concerning the browser
 ***************************************/

static int
handle_browser( 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 )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    switch ( event )
    {
        case FL_RESIZED :
        case FL_ATTRIB :
            sp->attrib = 1;
            break;

        case FL_DRAW:
            if ( sp->attrib )
            {
                attrib_change( obj );
                get_geometry( obj );
                sp->attrib = 0;
            }

            draw_dead_area( obj );
            /* fall through */

        case FL_DRAWLABEL:
            fl_draw_object_label( obj );
            break;

        case FL_FREEMEM:
            fl_free( sp );
            break;
    }

    return FL_RETURN_NONE;
}


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

static void
redraw_scrollbar( FL_OBJECT * ob )
{
    FLI_BROWSER_SPEC *comp = ob->spec;

    attrib_change( ob );
    get_geometry( ob );

    fl_freeze_form( ob->form );

    if ( comp->v_on )
    {
        fl_set_scrollbar_size( comp->vsl, comp->vsize );
        fl_set_scrollbar_value( comp->vsl, comp->vval );

        if ( comp->vsize != 1.0 )
            fl_set_scrollbar_increment( comp->vsl, comp->vinc1, comp->vinc2 );
    }

    if ( comp->h_on )
    {
        fl_set_scrollbar_size( comp->hsl, comp->hsize );
        fl_set_scrollbar_value( comp->hsl, comp->hval );

        if ( comp->hsize != 1.0 )
            fl_set_scrollbar_increment( comp->hsl, comp->hinc1, comp->hinc2 );
    }

    if ( comp->attrib )
    {
        if ( comp->v_on )
            fl_redraw_object( comp->vsl );
        if ( comp->h_on )
            fl_redraw_object( comp->hsl );
        fl_redraw_object( comp->tb );

        comp->attrib = 0;
    }

    draw_dead_area( ob );
    fl_unfreeze_form( ob->form );
}


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

static void
hcb( FL_OBJECT * obj,
     long        data  FL_UNUSED_ARG )
{
    FLI_BROWSER_SPEC *comp = obj->parent->spec;
    double hp = fli_tbox_set_rel_xoffset( comp->tb,
                                          fl_get_scrollbar_value( comp->hsl ) );

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

    if ( hp != comp->old_hp )
        obj->parent->returned |= FL_RETURN_CHANGED;

    if (    obj->parent->how_return & FL_RETURN_END_CHANGED
         && ! (    obj->parent->returned & FL_RETURN_CHANGED
                && obj->parent->returned & FL_RETURN_END ) )
            obj->parent->returned = FL_RETURN_NONE;

    if ( obj->parent->returned & FL_RETURN_END )
        comp->old_hp = hp;

    if ( obj->returned & FL_RETURN_CHANGED && comp->hcb )
        comp->hcb( obj->parent, fli_tbox_get_topline( comp->tb ) + 1,
                   comp->hcb_data );
}


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

static void
vcb( FL_OBJECT * obj,
     long        data  FL_UNUSED_ARG )
{
    FLI_BROWSER_SPEC *comp = obj->parent->spec;
    double vp = fli_tbox_set_rel_yoffset( comp->tb,
                                          fl_get_scrollbar_value( comp->vsl ) );

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

    if ( vp != comp->old_vp )
        obj->parent->returned |= FL_RETURN_CHANGED;

    if (    obj->parent->how_return & FL_RETURN_END_CHANGED
         && ! (    obj->parent->returned & FL_RETURN_CHANGED
                && obj->parent->returned & FL_RETURN_END ) )
            obj->parent->returned = FL_RETURN_NONE;

    if ( obj->parent->returned & FL_RETURN_END )
        comp->old_vp = vp;

    if ( obj->returned & FL_RETURN_CHANGED && comp->vcb )
        comp->vcb( obj->parent, fli_tbox_get_topline( comp->tb ) + 1,
                   comp->vcb_data );
}


/***************************************
 * Textbox callback routine, we simply pass the return value of the
 * textbox on as the parents new return value after a readjustment of
 * the scollbars.
 ***************************************/

static void
tbcb( FL_OBJECT * obj,
      long        data  FL_UNUSED_ARG )
{
    FLI_BROWSER_SPEC *psp = obj->parent->spec;
    double vp = fli_tbox_get_rel_yoffset( obj );
    double hp = fli_tbox_get_rel_xoffset( obj );

    if ( obj->returned & FL_RETURN_CHANGED )
    {
        if ( hp != psp->old_hp )
        {
            fl_set_scrollbar_value( psp->hsl, psp->old_hp = hp );
            if ( psp->hcb )
                psp->hcb( obj->parent, fli_tbox_get_topline( psp->tb ) + 1,
                          psp->hcb_data );
        }

        if ( vp != psp->old_vp )
        {
            fl_set_scrollbar_value( psp->vsl, psp->old_vp = vp );
            if ( psp->vcb )
                psp->vcb( obj->parent, fli_tbox_get_topline( psp->tb ) + 1,
                          psp->vcb_data );
        }
    }   

    obj->parent->returned = obj->returned;
}


/***************************************
 * Textbox dblclick callback
 ***************************************/

static void
tb_dblcallback( FL_OBJECT * ob,
                long        data  FL_UNUSED_ARG )
{
    FLI_BROWSER_SPEC *sp = ob->parent->spec;

    if ( sp->callback )
        sp->callback( ob->parent, sp->callback_data );
}


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

static int
tbpost( FL_OBJECT * ob,
        int         ev,
        FL_Coord    mx,
        FL_Coord    my,
       int          key,
        void      * xev )
{
    FL_OBJECT *br = ob->parent;

    return br->posthandle ? br->posthandle( br, ev, mx, my, key, xev ) : 0;
}


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

static int
tbpre( FL_OBJECT * ob,
       int         ev,
       FL_Coord    mx,
       FL_Coord    my,
      int          key,
       void      * xev )
{

    FL_OBJECT *br = ob->parent;

    return br->prehandle ? br->prehandle( br, ev, mx, my, key, xev ) : 0;
}


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

int
fli_get_default_scrollbarsize( FL_OBJECT * ob )
{
    int delta = ( FL_abs( ob->bw ) + 3 * ( ob->bw > 0 ) );
    int flat = IS_FLATBOX( ob->boxtype ) ? 2 : 0;

    if ( ob->w > 250 && ob->h > 250 )
        return 15 + delta - flat;
    else if ( ob->w < 150 || ob->h < 150 )
        return 13 + delta - flat;
    else
        return 14 + delta - flat;
}


/***************************************
 * Creates a new browser object to be added to a form
 ***************************************/

FL_OBJECT *
fl_create_browser( int          type,
                   FL_Coord     x,
                   FL_Coord     y,
                   FL_Coord     w,
                   FL_Coord     h,
                   const char * label )
{
    FL_OBJECT *ob;
    FLI_BROWSER_SPEC *sp;
    int D;

    ob = fl_make_object( FL_BROWSER, type, x, y, w, h, label,
                         handle_browser );

    sp = ob->spec = fl_calloc( 1, sizeof *sp );
    sp->tb = fli_create_tbox( type, x, y, w, h, NULL );

    sp->callback  = NULL;
    sp->hsize     = sp->vsize = sp->hval = sp->vval =
    sp->hinc1     = sp->hinc2 = sp->vinc1 = sp->vinc2 = 0.0;
    sp->hcb       = sp->vcb = NULL;
    sp->hcb_data  = sp->vcb_data = NULL;
    sp->old_hp    = sp->old_vp = 0.0;
    sp->attrib    = 1;

    /* Copy browser attributes from textbox */

    ob->boxtype  = sp->tb->boxtype;
    ob->lcol     = sp->tb->lcol;
    ob->col1     = sp->tb->col1;
    ob->col2     = sp->tb->col2;
    ob->align    = sp->tb->align;

    /* Textbox handlers */
 
    fl_set_object_callback( sp->tb, tbcb, 0 );
    fli_tbox_set_dblclick_callback( sp->tb, tb_dblcallback, 0 );
    fl_set_object_posthandler( sp->tb, tbpost );
    fl_set_object_prehandler( sp->tb, tbpre );

    /* Scrollbars */

    D = sp->vw_def = sp->hh_def = fli_get_default_scrollbarsize( ob );
    sp->v_pref = sp->h_pref = FL_AUTO;

    sp->hsl = fl_create_scrollbar( fli_context->hscb, x, y + h - D,
                                   w - D, D, NULL );
    fli_set_object_visibility( sp->hsl, sp->h_pref == FL_ON );
    fl_set_object_callback( sp->hsl, hcb, 0 );
    fl_set_scrollbar_value( sp->hsl, 0.0 );
    fl_set_scrollbar_bounds( sp->hsl, 0.0, 1.0 );
    sp->hsl->resize = FL_RESIZE_NONE;

    sp->vsl = fl_create_scrollbar( fli_context->vscb, x + w - D, y,
                                   D, h - D, NULL );
    fli_set_object_visibility( sp->vsl, sp->v_pref == FL_ON );
    fl_set_object_callback( sp->vsl, vcb, 0 );
    fl_set_scrollbar_value( sp->vsl, 0.0 );
    fl_set_scrollbar_bounds( sp->hsl, 0.0, 1.0 );
    sp->vsl->resize = FL_RESIZE_NONE;

    fl_add_child( ob, sp->tb  );
    fl_add_child( ob, sp->hsl );
    fl_add_child( ob, sp->vsl );

    /* In older versions scrollbars and browsers weren't returned to 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( ob, FL_RETURN_SELECTION | FL_RETURN_DESELECTION );
#else
    fl_set_object_return( ob, FL_RETURN_NONE );
#endif

    fl_set_object_return( sp->hsl, FL_RETURN_ALWAYS );
    fl_set_object_return( sp->vsl, FL_RETURN_ALWAYS );
    fl_set_object_return( sp->tb,  FL_RETURN_ALWAYS );

    return ob;
}


/***************************************
 * Adds a new browser object to the current form
 ***************************************/

FL_OBJECT *
fl_add_browser( int          type,
                FL_Coord     x,
                FL_Coord     y,
                FL_Coord     w,
                FL_Coord     h,
                const char * label )
{
    FL_OBJECT *ob = fl_create_browser( type, x, y, w, h, label );

    fl_add_object( fl_current_form, ob );

    return ob;
}


/***************************************
 * Switches the vertical scrollbar of the browser on or off
 ***************************************/

void
fl_set_browser_vscrollbar( FL_OBJECT * obj,
                           int         on_off )
{
    FLI_BROWSER_SPEC *comp = obj->spec;

    if ( comp->v_pref == on_off )
        return;

    comp->v_pref = on_off;
    redraw_scrollbar( obj );
    fli_tbox_react_to_vert( comp->tb, on_off );
    get_geometry( obj );
    fl_redraw_object( obj );
}


/***************************************
 * Switches the horizontal scrollbar of the browser on or off
 ***************************************/

void
fl_set_browser_hscrollbar( FL_OBJECT * obj,
                           int         on_off )
{
    FLI_BROWSER_SPEC *comp = obj->spec;

    if ( comp->h_pref == on_off )
        return;

    comp->h_pref = on_off;
    redraw_scrollbar( obj );
    fli_tbox_react_to_hori( comp->tb, on_off );
    get_geometry( obj );
    fl_redraw_object( obj );
}


/***************************************
 * Sets the callback for the horizontal scrollbar of the browser
 ***************************************/

void
fl_set_browser_hscroll_callback( FL_OBJECT                  * ob,
                                 FL_BROWSER_SCROLL_CALLBACK   cb,
                                 void                       * data )
{
      FLI_BROWSER_SPEC *comp = ob->spec;

      comp->hcb = cb;
      comp->hcb_data = data;
}


/***************************************
 * Returns the callback for the horizontal scrollbar of the browser
 ***************************************/

FL_BROWSER_SCROLL_CALLBACK
fl_get_browser_hscroll_callback( FL_OBJECT * ob )
{
      return ( ( FLI_BROWSER_SPEC * ) ob->spec )->hcb;
}


/***************************************
 * Sets the callback for the vertical scrollbar of the browser
 ***************************************/

void
fl_set_browser_vscroll_callback( FL_OBJECT                  * ob,
                                 FL_BROWSER_SCROLL_CALLBACK   cb,
                                 void                       * data )
{
      FLI_BROWSER_SPEC *comp = ob->spec;

      comp->vcb = cb;
      comp->vcb_data = data;
}


/***************************************
 * Returns the callback for the vertical scrollbar of the browser
 ***************************************/

FL_BROWSER_SCROLL_CALLBACK
fl_get_browser_vscroll_callback( FL_OBJECT * ob )
{
      return ( ( FLI_BROWSER_SPEC * ) ob->spec )->vcb;
}


/***************************************
 * Meant for the textbox to handle scroll callback properly
 ***************************************/

void
fli_adjust_browser_scrollbar( FL_OBJECT * ob )
{
      FLI_BROWSER_SPEC *comp = ob->spec;

      fl_call_object_callback( comp->hsl );
      fl_call_object_callback( comp->vsl );
}


/***************************************
 * Removes all text from the browser
 ***************************************/

void
fl_clear_browser( FL_OBJECT * ob )
{
    FLI_BROWSER_SPEC *comp = ob->spec;

    fl_freeze_form( ob->form );
    fli_tbox_clear( comp->tb );
    fl_set_scrollbar_value( comp->hsl, 0.0 );
    fl_set_scrollbar_size( comp->hsl, 1.0 );
    fl_set_scrollbar_value( comp->vsl, 0.0 );
    fl_set_scrollbar_size( comp->vsl, 1.0 );
    redraw_scrollbar( ob );
    fl_unfreeze_form( ob->form );
}


/***************************************
 * Returns the x-offset of the text shown in the browser as
 * the number of pixels
 ***************************************/

FL_Coord
fl_get_browser_xoffset( FL_OBJECT * obj )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    return fli_tbox_get_xoffset( sp->tb );
}


/***************************************
 * Returns the x-offset of the text shown in the browser as a
 * number between 0 (starts of lines are shown) and 1 (end of
 * longest line is shown)
 ***************************************/

double
fl_get_browser_rel_xoffset( FL_OBJECT * obj )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    return fli_tbox_get_rel_xoffset( sp->tb );
}


/***************************************
 * Sets the x-offset of the text shown in the browser as given
 * by the number of pixels
 ***************************************/

void
fl_set_browser_xoffset( FL_OBJECT * ob,
                        FL_Coord    npixels )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_set_xoffset( sp->tb, npixels );
    redraw_scrollbar( ob );
}


/***************************************
 * Sets the x-offset of the text shown in the browser as given
 * by a value between 0 (show start of lines) and 1 (show end
 * of longest line)
 ***************************************/

void
fl_set_browser_rel_xoffset( FL_OBJECT * ob,
                            double      val )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_set_rel_xoffset( sp->tb, val );
    redraw_scrollbar( ob );
}


/***************************************
 * Returns the y-offset of the text shown in the browser as
 * the number of pixels
 ***************************************/

FL_Coord
fl_get_browser_yoffset( FL_OBJECT * obj )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    return fli_tbox_get_yoffset( sp->tb );
}


/***************************************
 * Returns the y-offset of the text shown in the browser as a
 * number between 0 (start of text is shown) and 1 (end of
 * text is shown)
 ***************************************/

double
fl_get_browser_rel_yoffset( FL_OBJECT * obj )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    return fli_tbox_get_rel_yoffset( sp->tb );
}


/***************************************
 * Sets the y-offset of the text shown in the browser as given
 * by the number of pixels
 ***************************************/

void
fl_set_browser_yoffset( FL_OBJECT * ob,
                        FL_Coord    npixels )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_set_yoffset( sp->tb, npixels );
    redraw_scrollbar( ob );
}


/***************************************
 * Sets the y-offset of the text shown in the browser as given
 * by a value between 0 (show start of text) and 1 (show end of
 * text)
 ***************************************/

void
fl_set_browser_rel_yoffset( FL_OBJECT * ob,
                            double      val )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_set_rel_yoffset( sp->tb, val );
    redraw_scrollbar( ob );
}


/***************************************
 * Returns the y-offset for a line (or -1 if the line does not exist).
 ***************************************/

int
fl_get_browser_line_yoffset( FL_OBJECT * obj,
                             int         line )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    return fli_tbox_get_line_yoffset( sp->tb, line + 1 );
}


/***************************************
 * Move a line to the top of the browser
 ***************************************/

void
fl_set_browser_topline( FL_OBJECT * ob,
                        int         line )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_set_topline( sp->tb, line - 1 );
    redraw_scrollbar( ob );
}


/***************************************
 * Move a line to the bottom of the browser
 ***************************************/

void
fl_set_browser_bottomline( FL_OBJECT * ob,
                           int         line )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_set_bottomline( sp->tb, line - 1 );
    redraw_scrollbar( ob );
}


/***************************************
 * Marks a line of the browser as selected
 ***************************************/

void
fl_select_browser_line( FL_OBJECT * ob,
                        int         line )
{
    fli_tbox_select_line( ( ( FLI_BROWSER_SPEC * ) ob->spec )->tb, line - 1 );
}


/***************************************
 * Adds a line (given by a forma string and the appropriate number of
 * argumens) to the (end of the) browser and shifts the displayed
 * area so that the line is visible
 ***************************************/

void
fl_addto_browser( FL_OBJECT  * obj,
                  const char * text )
{
    fli_tbox_add_line( ( ( FLI_BROWSER_SPEC * ) obj->spec )->tb, text, 1 );
    redraw_scrollbar( obj );
}


/***************************************
 * Adds a line (specified using a format string and an unspecified number
 * of further arguments) to the (end of the) browser and shifts the
 * displayed area so that the line is visible
 ***************************************/

void
fl_addto_browser_f( FL_OBJECT  * obj,
                    const char * fmt,
                    ...)
{
    char *buf;

    EXPAND_FORMAT_STRING( buf, fmt );
    fl_addto_browser( obj, buf );
    fl_free( buf );
}


/***************************************
 * Inserts a line into the browser before the line currently
 * having the number 'linenumb'
 ***************************************/

void
fl_insert_browser_line( FL_OBJECT  * ob,
                        int          linenumb,
                        const char * newtext )
{
    FLI_BROWSER_SPEC *sp = ob->spec;
    FLI_TBOX_SPEC *tbsp = sp->tb->spec;

    /* When inserting into an empty browser or appending at the end
       it's treated exactly the same way as for fl_add_browser_line()
       (including interpretation of newline characters). */

    if ( tbsp->num_lines == 0 || linenumb > tbsp->num_lines )
        fli_tbox_insert_lines( sp->tb, linenumb - 1, newtext );
    else
        fli_tbox_insert_line( sp->tb, linenumb - 1, newtext );

    redraw_scrollbar( ob );
}


/***************************************
 * Inserts a line (given by a format string and the aprropriate number
 * of arguments) into the browser before the line currently having the
 * number 'linenumb'
 ***************************************/

void
fl_insert_browser_line_f( FL_OBJECT  * ob,
                          int          linenumb,
                          const char * fmt,
                          ... )
{
    char *buf;

    EXPAND_FORMAT_STRING( buf, fmt );
    fl_insert_browser_line( ob, linenumb, buf );
    fl_free( buf );
}


/***************************************
 * Deletes the line at line number 'linenumb' from the browser
 ***************************************/

void
fl_delete_browser_line( FL_OBJECT * ob,
                        int         linenumb )
{
    fli_tbox_delete_line( ( ( FLI_BROWSER_SPEC * ) ob->spec )->tb,
                          linenumb - 1 );
    redraw_scrollbar( ob );
}


/***************************************
 * Replaces the browser line at line number 'linenumb' with a new one
 ***************************************/

void
fl_replace_browser_line( FL_OBJECT  * ob,
                         int          linenumb,
                         const char * newtext )
{
    fli_tbox_replace_line( ( ( FLI_BROWSER_SPEC * ) ob->spec )->tb,
                           linenumb - 1, newtext );
    redraw_scrollbar( ob );
}


/***************************************
 * Replaces the browser line at line number 'linenumb' with a new one
 * derived from the format string and the following arguments.
 ***************************************/

void
fl_replace_browser_line_f( FL_OBJECT  * ob,
                           int          linenumb,
                           const char * fmt,
                           ... )
{
    char *buf;

    EXPAND_FORMAT_STRING( buf, fmt );
    fl_replace_browser_line( ob, linenumb, buf );
    fl_free( buf );
}


/***************************************
 * Returns the text of the browser's line at line number 'linenum'.
 * Returned text may not be modified (and not free'ed!)
 ***************************************/

const char *
fl_get_browser_line( FL_OBJECT * ob,
                     int         linenumb )
{
    return fli_tbox_get_line( ( ( FLI_BROWSER_SPEC * ) ob->spec )->tb,
                              linenumb - 1 );
}


/***************************************
 * Returns the number of the last line of the browser
 ***************************************/

int
fl_get_browser_maxline( FL_OBJECT * obj )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    return fli_tbox_get_num_lines( sp->tb );
}


/***************************************
 * Unselects a line in the browser
 ***************************************/

void
fl_deselect_browser_line( FL_OBJECT * ob,
                          int         line )
{
    fli_tbox_deselect_line( ( ( FLI_BROWSER_SPEC * ) ob->spec )->tb, line - 1 );
}


/***************************************
 * De-selects all lines of the browser
 ***************************************/

void
fl_deselect_browser( FL_OBJECT * ob )
{
    fli_tbox_deselect( ( ( FLI_BROWSER_SPEC * ) ob->spec )->tb );
}


/***************************************
 * Tests if a certain line of the browser is selected
 ***************************************/

int
fl_isselected_browser_line( FL_OBJECT * ob,
                            int         line )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    return fli_tbox_is_line_selected( sp->tb, line - 1 );
}


/***************************************
 * Returns the line last selected (or deselected) by the user
 ***************************************/

int
fl_get_browser( FL_OBJECT * ob )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    return fli_tbox_get_selection( sp->tb );
}


/***************************************
 * Sets the font size to be used per default
 ***************************************/

void
fl_set_browser_fontsize( FL_OBJECT * ob,
                         int         size )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_set_fontsize( sp->tb, size );
    redraw_scrollbar( ob );
    fl_redraw_object( ob );
}


/***************************************
 * Sets the font style to be used per default
 ***************************************/

void
fl_set_browser_fontstyle( FL_OBJECT * ob,
                          int         style )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_set_fontstyle( sp->tb, style );
    redraw_scrollbar( ob );
    fl_redraw_object( ob );
}


/***************************************
 * Returns the number of the topmost line that is completely visible,
 * may return 0 if there are no lines in the browser
 ***************************************/

int
fl_get_browser_topline( FL_OBJECT * obj )
{
    FLI_BROWSER_SPEC *sp = obj->spec;

    return fli_tbox_get_topline( sp->tb ) + 1;
}


/***************************************
 * Loads a browser with text from a file
 ***************************************/

int
fl_load_browser( FL_OBJECT  * obj,
                 const char * f )
{
    FLI_BROWSER_SPEC *sp = obj->spec;
    int status;

    fl_clear_browser( obj );
    status = fli_tbox_load( sp->tb, f );
    redraw_scrollbar( obj );
    return status;
}


/***************************************
 * Adds a line to the (end of the) browser (does not make the line
 * visible)
 ***************************************/

void
fl_add_browser_line( FL_OBJECT  * ob,
                     const char * newtext )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_add_line( sp->tb, newtext, 0 );
    redraw_scrollbar( ob );
}


/***************************************
 * Adds a line to the (end of the) browser (does not make the line
 * visible)
 ***************************************/

void
fl_add_browser_line_f( FL_OBJECT  * ob,
                       const char * fmt,
                       ... )
{
    char *buf;

    EXPAND_FORMAT_STRING( buf, fmt );
    fl_add_browser_line( ob,buf );
    fl_free( buf );
}


/***************************************
 * Sets a callback for double clicks in the browser
 ***************************************/

void
fl_set_browser_dblclick_callback( FL_OBJECT      * ob,
                                  FL_CALLBACKPTR   cb,
                                  long             a )
{
    FLI_BROWSER_SPEC *comp = ob->spec;

    comp->callback = cb;
    comp->callback_data = a;
}


/***************************************
 * Sets the height of the vertical and the width of the horzontal
 * scrollbar
 ***************************************/

void
fl_set_browser_scrollbarsize( FL_OBJECT * ob,
                              int         hh,
                              int         vw )
{
    FLI_BROWSER_SPEC *comp = ob->spec;
    int redraw = 0;

    if ( hh > 0 && hh != comp->hsl->h )
    {
        comp->hsl->h = comp->hh_def = hh;
        redraw = 1;
    }

    if ( vw > 0 && vw != comp->vsl->w )
    {
        comp->vsl->w = comp->vw_def = vw;
        redraw = 1;
    }

    if ( redraw )
    {
        comp->user_set = 1;
        fl_redraw_object( ob );
        fl_redraw_object( comp->tb );
        fl_redraw_object( comp->hsl );
        fl_redraw_object( comp->vsl );
    }
}


/***************************************
 * Returns the position and width and height of the browsers text area
 ***************************************/

void
fl_get_browser_dimension( FL_OBJECT * obj,
                          FL_Coord  * x,
                          FL_Coord  * y,
                          FL_Coord  * w,
                          FL_Coord  * h )
{
    FLI_TBOX_SPEC *sp = ( ( FLI_BROWSER_SPEC * ) obj->spec )->tb->spec;

    *x = obj->x + sp->x;
    *y = obj->y + sp->y;
    *w = sp->w;
    *h = sp->h;
}


/***************************************
 * Makes a browser line selectable or unselectable
 ***************************************/

void
fl_set_browser_line_selectable( FL_OBJECT * ob,
                                int         line,
                                int         flag )
{
    fli_tbox_make_line_selectable( ob, line - 1, flag );
}


/***************************************
 * Appends characters to the end of the last line in the browser
 ***************************************/

void
fl_addto_browser_chars( FL_OBJECT  * ob,
                        const char * str )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    fli_tbox_add_chars( sp->tb, str );
    redraw_scrollbar( ob );
}


/***************************************
 * Appends characters to the end of the last line in the browser
 ***************************************/

void
fl_addto_browser_chars_f( FL_OBJECT  * ob,
                          const char * fmt,
                          ... )
{
    char *buf;

    EXPAND_FORMAT_STRING( buf, fmt );
    fl_addto_browser_chars_f( ob, buf );
    fl_free( buf );
}


/***************************************
 * Returns an approximation of the number of lines shown in the browser
 ***************************************/

int
fl_get_browser_screenlines( FL_OBJECT * ob )
{
    FLI_BROWSER_SPEC *sp = ob->spec;

    int t = fli_tbox_get_topline( sp->tb );
    int b = fli_tbox_get_bottomline( sp->tb );

    if ( t < 0 || b < 0 )
        return 0;
    return b - t + 1;
}


/***************************************
 * Sets the escape key used in the text
 ***************************************/

void
fl_set_browser_specialkey( FL_OBJECT * ob,
                           int         specialkey )
{
    FLI_TBOX_SPEC *sp = ( ( FLI_BROWSER_SPEC * ) ob->spec )->tb->spec;

    sp->specialkey = specialkey;
}


/***************************************
 * Brings a line into view
 ***************************************/

void
fl_show_browser_line( FL_OBJECT * ob,
                      int         line )
{
    fli_tbox_set_centerline( ( ( FLI_BROWSER_SPEC * ) ob->spec )->tb, line );
    redraw_scrollbar( ob );
}


/***************************************
 * Deprecated function, only left in for backward compatibility
 ***************************************/

int
fl_set_default_browser_maxlinelength( int n  FL_UNUSED_ARG )
{
    return 0;
}


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

int
fl_get_browser_scrollbar_repeat( FL_OBJECT * ob )
{
    return fl_get_slider_repeat( ( ( FLI_BROWSER_SPEC * ) ob->spec )->vsl );
}


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

void
fl_set_browser_scrollbar_repeat( FL_OBJECT * ob,
                                 int         millisec )
{
    if ( millisec > 0 )
    {
        fl_set_slider_repeat( ( ( FLI_BROWSER_SPEC * ) ob->spec )->vsl,
                              millisec );
        fl_set_slider_repeat( ( ( FLI_BROWSER_SPEC * ) ob->spec )->hsl,
                              millisec );
    }
}


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