download Fl_Browser.cxx
Language: C++
Copyright: Copyright 1998-2003 by Bill Spitzak and others.
LOC: 1144
Project Info
Fast Light Tool Kit(fltk)
Server: SourceForge
Type: cvs
...Forge\f\fltk\fltk\fltk\src\
   allfiles.xbm
   ARRAY.h
   browser_minus.xbm
   browser_plus.xbm
   cmap.cxx
   CycleButton.cxx
   d1.xbm
   d1_mask.xbm
   dlload_osx.cxx
   dump_compose.c
   ew.xbm
   ew_mask.xbm
   fastarrow.h
   filename_absolute.cxx
   filename_expand.cxx
   filename_ext.cxx
   filename_isdir.cxx
   filename_list.cxx
   filename_match.cxx
   filename_setext.cxx
   Fl.cxx
   Fl_abort.cxx
   Fl_add_idle.cxx
   Fl_Adjuster.cxx
   Fl_Align_Group.cxx
   fl_arc.cxx
   fl_arci.cxx
   Fl_arg.cxx
   fl_ask.cxx
   Fl_Bar.cxx
   Fl_Bitmap.cxx
   fl_bmp.cxx
   Fl_Box.cxx
   Fl_Boxtype.cxx
   Fl_Browser.cxx
   Fl_Browser_load.cxx
   Fl_Button.cxx
   fl_call_main.c
   Fl_Check_Button.cxx
   Fl_Choice.cxx
   fl_clip.cxx
   Fl_Clock.cxx
   fl_cmap.h
   fl_color.cxx
   Fl_Color_Chooser.cxx
   fl_color_mac.cxx
   fl_color_win32.cxx
   fl_color_x.cxx
   Fl_compose.cxx
   fl_cursor.cxx
   fl_curve.cxx
   Fl_Dial.cxx
   fl_diamond_box.cxx
   Fl_display.cxx
   fl_dnd.cxx
   fl_dnd_mac.cxx
   fl_dnd_win32.cxx
   fl_dnd_x.cxx
   fl_draw.cxx
   fl_draw_image.cxx
   fl_draw_image_mac.cxx
   fl_draw_image_win32.cxx
   fl_draw_image_x.cxx
   fl_draw_pixmap.cxx
   fl_engraved_label.cxx
   fl_file_chooser.cxx
   Fl_FileBrowser.cxx
   Fl_FileChooser.cxx
   Fl_FileChooser.fl
   Fl_FileChooser2.cxx
   Fl_FileIcon.cxx
   Fl_FileIcon2.cxx
   Fl_FileInput.cxx
   Fl_Float_Input.cxx
   fl_font.cxx
   fl_font_mac.cxx
   fl_font_win32.cxx
   fl_font_x.cxx
   fl_font_xft.cxx
   Fl_get_key.cxx
   Fl_get_key_mac.cxx
   Fl_get_key_win32.cxx
   fl_gif.cxx
   fl_glyph.cxx
   Fl_Group.cxx
   Fl_Help_Dialog.cxx
   Fl_Help_View.cxx
   Fl_Highlight_Button.cxx
   Fl_Image.cxx
   Fl_Input.cxx
   Fl_Input_Browser.cxx
   Fl_Item.cxx
   Fl_key_name.cxx
   fl_labeltype.cxx
   Fl_Light_Button.cxx
   fl_list_fonts.cxx
   fl_list_fonts_mac.cxx
   fl_list_fonts_win32.cxx
   fl_list_fonts_x.cxx
   fl_load_plugin.cxx
   Fl_lock.cxx
   Fl_mac.cxx
   Fl_Menu.cxx
   Fl_Menu_.cxx
   Fl_Menu_add.cxx
   Fl_Menu_Bar.cxx
   Fl_Menu_Button.cxx
   Fl_Menu_global.cxx
   Fl_Menu_Item.cxx
   Fl_Menu_Window.cxx
   Fl_Multi_Image.cxx
   Fl_Numeric_Input.cxx
   Fl_Output.cxx
   fl_oval_box.cxx
   fl_overlay.cxx
   fl_overlay_visual.cxx
   Fl_own_colormap.cxx
   Fl_Pack.cxx
   Fl_Pixmap.cxx
   fl_plastic_box.cxx
   Fl_ProgressBar.cxx
   Fl_Radio_Button.cxx
   fl_rect.cxx
   Fl_Repeat_Button.cxx
   Fl_Return_Button.cxx
   fl_rgb.cxx
   Fl_RGB_Image.cxx
   Fl_Roller.cxx
   fl_round_box.cxx
   fl_rounded_box.cxx
   Fl_Scroll.cxx
   fl_scroll_area.cxx
   Fl_Scrollbar.cxx
   fl_set_fonts_mac.cxx
   fl_shadow_box.cxx
   Fl_Shaped_Window.cxx
   Fl_Shared_Image.cxx
   fl_show_colormap.cxx
   Fl_Single_Window.cxx
   Fl_Slider.cxx
   Fl_String_List.cxx
   Fl_Style.cxx
   Fl_Style_Set.cxx
   fl_symbols.cxx
   Fl_Tabs.cxx
   Fl_Text_Buffer.cxx
   Fl_Text_Display.cxx
   Fl_Text_Editor.cxx
   Fl_Tile.cxx
   Fl_Tiled_Image.cxx
   Fl_Tooltip.cxx
   Fl_Valuator.cxx
   Fl_Value_Input.cxx
   Fl_Value_Slider.cxx
   fl_vertex.cxx
   Fl_visual.cxx
   Fl_Widget.cxx
   Fl_win32.cxx
   Fl_Window.cxx
   Fl_Window_fullscreen.cxx
   Fl_Window_hotspot.cxx
   Fl_Window_iconize.cxx
   Fl_x.cxx
   Fl_XColor.h
   fl_xpm.cxx
   fltk_theme.cxx
   fltk_theme_win32.cxx
   fltk_theme_x.cxx
   IFont.h
   mediumarrow.h
   new.xbm
   ns.xbm
   ns_mask.xbm
   pktdef.h
   scandir.c
   scandir_win32.c
   shortcuts.cxx
   slowarrow.h
   string.c
   up.xbm
   utf.c
   ValueOutput.cxx
   vsnprintf.c
   wintab.h
   wintabx.h
   XColorMap.h
   xutf8.cxx

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
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
//
// "$Id: Fl_Browser.cxx,v 1.104 2005/01/25 09:49:11 spitzak Exp $"
//
// Copyright 1998-2003 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".

#include <fltk/Browser.h>
#include <fltk/Button.h>
#include <fltk/events.h>
#include <fltk/damage.h>
#include <fltk/layout.h>
#include <fltk/Box.h>
#include <fltk/Item.h>
#include <fltk/draw.h>
#include <fltk/error.h>
#include <fltk/Cursor.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace fltk;

/*! \class fltk::Browser

  Displays a scrolling vertical list of text widgets, possibly with a
  hierachial arrangement, and lets the user select one of them.

  \image html browser.gif

  The items may be created as child widgets (usually the same widgets
  as are used to create menus: fltk::Item widgets, or fltk::ItemGroup
  widgets to make a hierarchy). Alternatively you can build simple
  text lists by using the add() method, which will create the child
  widgets for you (and even create a hierarchy if you put slashes in
  the text). You can also us an fltk::List which allows you to control
  the storage by dynamically creating a "fake" widget for the browser
  to use to draw each item.

  All the functions used to add, remove, or modify items in the list
  are defined by the base class fltk::Menu. See that for much more
  information.

  For a simple constant list you can populate the list by calling
  browser->add("text of item") once for each item.  If you give the
  browser a callback you can find out what item was selected with
  value(), the first item is zero (this is different from
  older versions of fltk that started at 1!), and will be negative if
  no item is selected. You can change the selected item with
  value(new_value).

  The subclass fltk::MultiBrowser lets the user select more than one
  item at the same time.

  The callback() is done when the user changes the selected items or
  when they open/close parents. In addition, if the user double-clicks
  a non-parent item, then it is "executed" which usually means that
  the callback() on the item itself is run. However if that is the
  default callback, the callback() of this widget is called with the
  user_data() of the item.

  You can control when callbacks are done with the when() method. The
  following values are useful, the default value is fltk::WHEN_CHANGED.

  - fltk::WHEN_NEVER - Callback is never done. changed() can be used
    to see if the user has modified the browser.

  - fltk::WHEN_CHANGED - Callback is called on every change to each
    item as it happens. The method item() will return the one that
    is being changed. Notice that selecting large numbers in a mulit
    browser will produce large numbers of callbacks.

  - fltk::WHEN_RELEASE - Callback is done when the user releases the
    mouse after some changes, and on any keystroke that
    changes the item. For a multi browser you will only be able
    to find out all the changes by scanning all the items in the callback.

  - fltk::WHEN_RELEASE_ALWAYS - Callback is done when the user
    releases the mouse even if the current item has not changed, and
    on any arrow keystroke even when at the top or bottom of the
    browser.

  - fltk::WHEN_ENTER_KEY - If you turn this on then the enter key is
    a shortcut and executes the current item like double-click.

*/

////////////////////////////////////////////////////////////////
// Compile-time style configuration options.
// For "run time configuration" copy this file, rename the widget,
// compile with your desired options, and use your renamed widget.
// Deal with it.

#define DRAW_STRIPES 0 // draw Macintosh-style stripes
#define TRIANGLE_GLYPH 0 // use Macintosh/fltk-style glyphs

/*! \fn int Browser::width() const
  The width of the longest item in the browser, measured in pixels. If
  this is changed (by adding or deleting items or opening or closing a
  parent item) then layout() must be called before this is correct.
*/

/*! \fn int Browser::height() const
  The height of all the visible items in the browser, measured in
  pixels. If this is changed (by adding or deleting items or opening
  or closing a parent item) then layout() must be called before this
  is correct.
*/

/*! \fn int Browser::box_width() const
  The width of the display area of the browser in pixels, this is w()
  minus the edges of the box() minus the width of the vertical
  scrollbar, if visible. If this is changed (by resizing the widget,
  adding or deleting items or opening or closing a parent item such
  that the scrollbar visibility changes) then layout() must be called
  before this is correct.
*/

/*! \fn int Browser::box_height() const
  The height of the display area of the browser in pixels, this is h()
  minus the edges of the box() minus the height of the horizontal
  scrollbar, if visible. If this is changed (by resizing the widget,
  adding or deleting items or opening or closing a parent item such
  that the scrollbar visibility changes) then layout() must be called
  before this is correct.
*/

/*! \fn void Browser::indented(bool v)
  Turn this on to for space to be reserved for open/close boxes drawn
  to the left of top-level items. You usually want this for a
  hierarchial browser. This should be off for a flat browser, or to
  emulate Windows Explorer where "my computer" does not have an
  open/close to the left of it. The default value is false.
*/

/*! \fn Widget *Browser::header(int col)
  Return pointer to Widget in column \a col, starting from index 0.
  If column \a col is invalid, NULL is returned.
*/
  
/*! \fn int Browser::nheader() const
  Return number of columns in browser.
*/

////////////////////////////////////////////////////////////////
// Moving between items:

// A "mark" is like a pointer to a widget somewhere in the hierarchy
// of the Browser. Actually it is an array of child indicies, and also
// the vertical position and index number (used to decide stripe color)
// of the item.

// The Browser has space to store a fixed number of marks.

// The "current item" is the one pointed to by the mark called HERE.
// A pointer to it is stored in item(). To allow List to work, this
// pointer is only valid until the next item is referenced, so don't
// copy it to any storage. You can move the current item forward and
// backward, skipping invisible items. You can also place it on any
// item, even ones that cannot be seen, by using goto_item.

/*!
  Return true if the item would be visible to the user if the browser
  was scrolled to the correct location. This means that the
  fltk::INVISIBLE flag is not set on it, and all parents of it are
  open and visible as well.
*/
bool Browser::item_is_visible() const {
  return open_level[HERE] >= item_level[HERE] && item()->visible();
}

/*! This function increases the number of levels we can store in each
  mark, and sets the level of the current mark to n. */
void Browser::set_level(int n) {
  if (n > levels) {
    if (n > 255) fatal("More than 255 levels in Browser");
    for (int i = 0; i < NUMMARKS; i++) {
      item_index[i]=(int*)realloc((void*)item_index[i],(n+1)*sizeof(int));
    }
    levels = n;
  }
  item_level[HERE] = n;
}

/*!
  Because of the hierarchial structure it is difficult to identify
  an item in the browser. Instead of passing an identifier to all the
  calls that can modify an item, the browser provides several calls to
  set item() based on various rules, and then calls to modify the
  current item().

  This call sets item() to the very first visible widget in
  the browser. It returns the widget for that item, or null if the
  browser is empty.

  If you have invisible items in the browser you should use
  goto_index(0) if you want to go to the first item even if it is
  invisible.
*/
Widget* Browser::goto_top() {
  item_level[HERE] = 0;
  open_level[HERE] = 0;
  item_position[HERE] = 0;
  item_index[HERE][0] = 0;
  siblings = children(item_index[HERE],0);
  // empty browser must return a null widget
  if (siblings <= 0) {
    item(0);
  } else {
    item(child(item_index[HERE],0));
    // skip leading invisible widgets:
    if (!item()->visible()) return next_visible();
  }
  return item();
}

/*! \fn Widget* Browser::goto_focus()
  Sets the item() to the "focus" (the item with the dotted square in
  an fltk::MultiBrowser, and the selected item in a normal
  fltk::Browser.
*/

/*! set current item to a particular mark */
Widget* Browser::goto_mark(int mark) {
  item_position[HERE] = item_position[mark];
  item_level[HERE] = item_level[mark];
  open_level[HERE] = open_level[mark];
  for (int L = 0; L <= item_level[HERE]; L++) {
    int i = item_index[HERE][L] = item_index[mark][L];
    siblings = children(item_index[HERE], L);
    if (i < 0 || i >= siblings) {item(0); return 0;}
  }
  item(child(item_index[HERE], item_level[HERE]));
  return item();
}

// copy one mark to another
void Browser::set_mark(int dest_mark, int src_mark) {
  item_position[dest_mark] = item_position[src_mark];
  item_level[dest_mark] = item_level[src_mark];
  open_level[dest_mark] = open_level[src_mark];
  for (int L = item_level[src_mark]; L >= 0; L--)
    item_index[dest_mark][L] = item_index[src_mark][L];
}

// compare the relative locations of these two marks
// return < 0 if mark1 is before mark2
// return   0 if mark1 is mark2
// return > 0 if mark1 is after mark2

static int compare_marks(const int* index1,int L1, const int* index2, int L2) {
  for (int L = 0; ; L++) {
    if (L > L1) {
      if (L > L2) return 0;
      return -1; // mark1 is on a parent of mark2
    } else if (L > L2) {
      return +1; // mark2 is on a parent of mark1
    }
    int n = index1[L] - index2[L];
    if (n) return n > 0 ? +2 : -2;
  }
}

int Browser::compare_marks(int mark1, int mark2) {
  return ::compare_marks(item_index[mark1], item_level[mark1],
			 item_index[mark2], item_level[mark2]);
}

// unsets this mark in toplevel
void Browser::unset_mark(int mark) {
  item_index[mark][0] = -1;
}

// is this toplevel mark set?
bool Browser::is_set(int mark) {
  return item_index[mark][0] >= 0;
}

/*!  Return true if the current item is a parent. Notice that it may
  have zero children. */
bool Browser::item_is_parent() const {
  return children(item_index[HERE],item_level[HERE]+1) >= 0;
}

/*! Move forward to the next visible item (what down-arrow does).
  This does not move and returns null if we are at the bottom. */
Widget* Browser::next_visible() {
  if (item_is_visible()) {
    if (!layout_damage()) item_position[HERE] += item()->height();
    // If we are on an open group title with children, go to first item in group
    if (item()->flags()&VALUE && item_is_parent()) {
      int n = item_level[HERE]+1;
      set_level(n);
      open_level[HERE] = n;
      item_index[HERE][n] = 0;
      siblings = children(item_index[HERE], n);
    } else {
      // go to next item in this group
      item_index[HERE][item_level[HERE]] ++;
    }
  } else {
    // item is not visible, point after it (or after closed/invisible parent)
    item_level[HERE] = open_level[HERE];
    siblings = children(item_index[HERE], item_level[HERE]);
    item_index[HERE][item_level[HERE]] ++;
  }

  // loop to find the next real item:
  for (;;) {

    if (item_index[HERE][item_level[HERE]] >= siblings) {
      // we moved off the end of a group
      if (!item_level[HERE]) {item(0); return 0;} // end of the entire browser
      open_level[HERE] = --item_level[HERE];
      item_index[HERE][item_level[HERE]] ++;
      siblings = children(item_index[HERE], item_level[HERE]);
      continue;
    }

    item(child(item_index[HERE], item_level[HERE]));

    // skip invisible items:
    if (item()->visible()) break;
    item_index[HERE][item_level[HERE]] ++;
  }

  return item();
}

/*! Move backward to previous visible item:
  This does not move and returns null if we are at the top. */
Widget* Browser::previous_visible() {

  // if we are on a child of a closed or invisible parent, pretend
  // we are on the item after that parent:
  if (item_level[HERE] > open_level[HERE]) {
    item_level[HERE] = open_level[HERE];
    item_index[HERE][item_level[HERE]] ++;
  }

  // there's got to be simpler logic for this loop...
  for (;;) {
    // go up to parent from first item in a group:
    if (!item_index[HERE][item_level[HERE]]) {
      if (!item_level[HERE]) { // start of browser
	item_position[HERE] = 0;
	return 0;
      }
      open_level[HERE] = --item_level[HERE];
      item(child(item_index[HERE], item_level[HERE]));
      siblings = children(item_index[HERE], item_level[HERE]);
      break;
    }

    // go back to previous item in this group:
    item_index[HERE][item_level[HERE]] --;
    item(child(item_index[HERE], item_level[HERE]));

    // go to last child in a group:
    while (item()->flags()&VALUE && item()->visible() && item_is_parent()) {
      int n = children(item_index[HERE], item_level[HERE]+1);
      if (n <= 0) break; // the group is empty, remain on it's title
      set_level(item_level[HERE]+1);
      open_level[HERE] = item_level[HERE];
      item_index[HERE][item_level[HERE]] = n-1;
      item(child(item_index[HERE], item_level[HERE]));
      siblings = n;
    }

    if (item()->visible()) break;
  }

  if (!layout_damage()) item_position[HERE] -= item()->height();
  return item();
}

/*!  Move the current item to the next item. If if is a parent it
  moves to the first child. If not a parent, it moves to the next
  child of it's parent. If it is the last child it moves to the
  parent's brother. Repeatedly calling this will visit every child of
  the browser. This returns the widget. If the current widget is the
  last one this returns null, but the current widget remains on the
  last one.

  The current_position() is NOT set by this! It cannot be calculated
  efficiently and would slow down the use of this function for
  visiting all items.
*/
Widget* Browser::next() {
  int n = children(item_index[HERE], item_level[HERE]+1);
  if (n > 0) {
    set_level(item_level[HERE]+1);
    item_index[HERE][item_level[HERE]] = 0;
    siblings = n;
  } else {
    item_index[HERE][item_level[HERE]]++;
  }
  for (;;) {
    if (item_index[HERE][item_level[HERE]] < siblings) {
      item(child(item_index[HERE], item_level[HERE]));
      return item();
    }
    if (item_level[HERE] <= 0) {item(0); return 0;}
    item_level[HERE]--;
    item_index[HERE][item_level[HERE]]++;
    siblings = children(item_index[HERE], item_level[HERE]);
  }
}

/*! Set the current item() to the last one who's top is at or before
  \a Y pixels from the top. */
Widget* Browser::goto_position(int Y) {
  if (Y < 0) Y = 0;
  if (layout_damage() || Y<=yposition_/2 || !goto_mark(FIRST_VISIBLE)) {
    goto_top();
  } else {
    // move backwards until we are before or at the position:
    while (item_position[HERE] > Y) {
      if (!previous_visible()) {goto_top(); break;}
    }
  }
  // move forward to the item:
  if (item()) for (;;) {
    int h = item()->height();
    if (item_position[HERE]+h > Y) break;
    if (!next_visible()) {previous_visible(); return 0;}
  }
  return item();
}

/*! \fn void Browser::damage_item()
  Tell the browser to redraw the current item. Do this if you know it
  has changed appearance. This is better than redrawing the entire
  browser because it will blink a lot less.
*/

static bool nodamage;
/*! Set item referenced by this mark as being damaged. */
void Browser::damage_item(int mark) {
  if (nodamage) return;
  if (!compare_marks(REDRAW_0, mark) || !compare_marks(REDRAW_1, mark))
    return;
  int m = REDRAW_0;
  if (is_set(m)) {
    m = REDRAW_1;
    // if both marks are used we give up and damage the whole thing:
    if (is_set(m)) {redraw(DAMAGE_CONTENTS); return;}
  }
  set_mark(m, mark);
  redraw(DAMAGE_VALUE);
}

/*! \fn int Browser::current_level() const
  Return the nesting level of the current item (how many parents it has).  */

/*! \fn const int* Browser::current_index() const
 Return an array of current_level()+1 indexes saying which child at
 each level includes the current item.
*/

/*! \fn int Browser::current_position() const
  Return the y position, in pixels, of the top edge of the current
  item. You may also want the height, which is in
  item()->height().
*/

/*! \fn int Browser::focus_level() const
  Return the nesting level of the focus (how many parents it has).
  The focus is the selected item the user sees.
*/

/*! \fn const int* Browser::focus_index() const
 Return an array of focus_level()+1 indexes saying which child at
 each level includes the focus.
*/

/*! \fn int Browser::focus_position() const
  Return the y position, in pixels, of the top edge of the focus
  item. You may also want the height, which is in
  goto_focus(); item()->height().
*/

/*! \fn void Browser::value(int v)
  Same as goto_index(v);set_focus();, to change the current item in a
  non-hierarchial browser.
*/

/*! \fn int Browser::value() const
  Returns focus_index(v)[0], to get the current item in a
  non-hierarchial browser.
*/

////////////////////////////////////////////////////////////////
// Drawing

// Glyphs that are drawn to the left of items.  Enumeration order
// is important!
enum {
  NONE, BAR, ELL, TEE, CLOSED_ELL, CLOSED_TEE, OPEN_ELL, OPEN_TEE
};

#define BOXSIZE 9

void
browser_glyph(int glyph, const Rectangle& r, const Style* style, Flags f)
{
  Color bg, fg; style->boxcolors(f|OUTPUT, bg, fg);
  //if (fg == BLACK) fg = GRAY33;
  setcolor(fg);
  int lx = r.center_x();
  int ly = r.y()+(r.h()-1)/2; // slightly higher than center_y() for some reason
  switch (glyph) {
  case NONE:
    break;
  case BAR:
    drawline(lx, r.y(), lx, r.b()-1);
    break;
  case ELL:
    drawline(lx, r.y(), lx, ly);
    drawline(lx, ly, r.r(), ly);
    break;
  case TEE:
    drawline(lx, r.y(), lx, r.b()-1);
    drawline(lx, ly, r.r(), ly);
    break;
#if TRIANGLE_GLYPH
  case CLOSED_TEE:
  case OPEN_TEE:
    drawline(lx, r.y(), lx, r.b()-1);
    goto J1;
  case CLOSED_ELL:
  case OPEN_ELL:
    drawline(lx, r.y(), lx, ly);
  J1:
    Widget::default_glyph(glyph < OPEN_ELL ? GLYPH_RIGHT : GLYPH_DOWN,
	     r, style, f);
    break;
#else
  default: {
    const int boxsize = BOXSIZE/2;
    drawline(lx, r.y(), lx, ly-boxsize);
    if (glyph&1) drawline(lx, ly+boxsize, lx, r.b()-1);
    drawline(lx+boxsize, ly, r.r(), ly);
    strokerect(Rectangle(lx-boxsize, ly-boxsize, BOXSIZE, BOXSIZE));
    drawline(lx-boxsize+2, ly, lx+boxsize-2, ly);
    if (glyph < OPEN_ELL) drawline(lx, ly-boxsize+2, lx, ly+boxsize-2);
    }
#endif
  }
}

// this is non-zero if a drag was started in a group open/close box:
static char openclose_drag;

// Draws the current item:
void Browser::draw_item() {

  Widget* widget = item();
  Rectangle r(interior.x(), interior.y()+item_position[HERE]-yposition_,
	      interior.w(), widget->height());

  Flags flags;

  int is_focus = at_mark(FOCUS);

  if (multi() ? widget->selected() : is_focus) {
    setcolor(selection_color());
    fillrect(r);
    widget->set_selected();
    flags = SELECTED;
  } else {
    widget->clear_selected();
    flags = 0;
#if DRAW_STRIPES
    Color c0 = color();
    Color c1 = buttoncolor();
    int x=1; for (int j=0; j<=item_level[HERE]; j++) x ^= item_index[HERE][j]+1;
    if (x & 1 && c1 != c0) {
      // draw odd-numbered items with a dark stripe, plus contrast-enhancing
      // pixel rows on top and bottom:
      setcolor(c1);
      Rectangle ir(r); ir.inset(0,1,0,1); fillrect(ir);
      setcolor(c0 <= GRAY85 ? c1 : GRAY85);
      //setcolor(lerp(c1, c0, 1.9));
      drawline(r.x(), r.y(), r.r(), r.y());
      drawline(r.x(), r.b()-1, r.r(), r.b()-1);
    } else {
      setcolor(c0);
      fillrect(r);
    }
#else
    setcolor(color());
    fillrect(r);
#endif
  }

  int col_shift = interior.x();
  int arrow_size = int(textsize())|1;
  int preview_open =
    (openclose_drag == 1 && pushed() && at_mark(FOCUS)) ? VALUE : 0;
  widget->invert_flag(preview_open);

  // draw the glyphs, one for each nesting level:
  for (int j = indented() ? 0 : 1; j <= item_level[HERE]; j++) {
    int g = item_index[HERE][j] < children(item_index[HERE],j) - 1;
    if (j == item_level[HERE]) {
      if (children(item_index[HERE],j+1)>=0)
	if (widget->flags() & VALUE)
	  g += OPEN_ELL;
	else
	  g += CLOSED_ELL;
      else
	g += ELL;
    }
    Rectangle gr(r.x()-xposition_, r.y(), arrow_size, r.h());
    draw_glyph(g, gr, flags);
    r.move(arrow_size,0);
    col_shift += arrow_size;
  }

  if (focused() && is_focus) {
    focusbox()->draw(r, style(), flags|(FOCUSED|OUTPUT));
  }

  // Shift image width 
  if (widget->image()) {
    int iw,ih; widget->image()->measure(iw,ih);
    col_shift += iw;
  }

  // Shift first column width, so labels after 1. column are lined up correctly.
  int saved_colw = 0;
  int *cols = (int *)column_widths_p;
  if(cols) {
    saved_colw = cols[0];    
    cols[0] -= col_shift;
    if (cols[0]==0) cols[0]--;    
  }

  push_matrix();
  widget->x(r.x()-xposition_);
  widget->y(r.y()+(int(leading())-1)/2);
  translate(widget->x(), widget->y());
  widget->set_damage(DAMAGE_ALL|DAMAGE_EXPOSE);
  int save_w = widget->w();
  if (!save_w) widget->w(interior.r()-r.x());
  widget->draw();
  widget->w(save_w);
  widget->set_damage(0);
  pop_matrix();

  // Restore column width
  if (cols) cols[0] = saved_colw;

  widget->invert_flag(preview_open);
}

void Browser::draw_clip_cb(void* v, const Rectangle& r) {
  ((Browser*)v)->draw_clip(r);
}

void Browser::draw_clip(const Rectangle& r) {
  push_clip(r);

  int draw_all = damage() & (DAMAGE_ALL|DAMAGE_CONTENTS);
  if (goto_mark(FIRST_VISIBLE)) for (;;) {
    int item_y = interior.y()+item_position[HERE]-yposition_;
    if (item_y >= r.b()) break;
    if (draw_all || !at_mark(REDRAW_0) && !at_mark(REDRAW_1)) draw_item();
    if (!next_visible()) break;
  }

  // erase the area below the last item:
  int bottom_y = interior.y()+item_position[HERE]-yposition_;
  if (bottom_y < r.b()) {
    setcolor(color());
    fillrect(Rectangle(r.x(), bottom_y, r.w(), r.b()-bottom_y));
  }
  pop_clip();
}

void Browser::draw() {
  const int *last_columns = fltk::column_widths();
  fltk::column_widths(column_widths_p);
  uchar d = damage();
  Item::set_style(this);
  if (d & DAMAGE_ALL) { // full redraw
    //printf("full redraw damage %x\n", d);
    draw_frame();
    draw_clip(interior);
  } else if (d & DAMAGE_CONTENTS) { // redraw contents
    //printf("contents redraw damage %x\n", d);
    draw_clip(interior);
  } else { // minimal update
    //printf("minimal redraw damage %x\n", d);
    if (scrolldx || scrolldy) {
      scrollrect(interior, scrolldx, scrolldy, draw_clip_cb, this);
    }
    int clipped = 0;
    for (int n = REDRAW_0; n <= REDRAW_1; n++) {
      if (goto_mark(n)) {
	if (!clipped) {push_clip(interior); clipped = 1;}
	draw_item();
      }
    }
    if (d & DAMAGE_CHILD) {
      if (goto_mark(FIRST_VISIBLE)) for (;;) {
	int y = interior.y()+item_position[HERE]-yposition_+(int(leading())-1)/2;
	if (y >= interior.b()) break;
	if (item()->damage()) {
	  if (!clipped) {push_clip(interior); clipped = 1;}
	  int arrow_size = int(textsize())|1;
	  int x = interior.x()+(item_level[HERE]+indented())*arrow_size-xposition_;
	  push_matrix();
	  translate(x, y);
	  item()->draw();
	  item()->set_damage(0);
	  pop_matrix();
	}
	if (!next_visible()) break;
      }
    }
    if (clipped) pop_clip();
  }
  fltk::column_widths(last_columns);
  //fltk::column_widths(0);
  scrolldx = scrolldy = 0;
  unset_mark(REDRAW_0);
  unset_mark(REDRAW_1);

  // draw the scrollbars:
  if (d & DAMAGE_ALL) {
    scrollbar.set_damage(DAMAGE_ALL);
    hscrollbar.set_damage(DAMAGE_ALL);
    if (scrollbar.visible() && hscrollbar.visible()) {
      // fill in the little box in the corner
      setcolor(buttoncolor());
      fillrect(Rectangle(scrollbar.x(), hscrollbar.y(), scrollbar.w(),hscrollbar.h()));
    }
    if (header_) {
      for (int i=0; i<nHeader; i++) {
        header_[i]->set_damage(DAMAGE_ALL);
      }
    }
  }
  update_child(scrollbar);
  update_child(hscrollbar);

  if (header_) {
    Rectangle r(box()->dx(), box()->dy(), w()-box()->dw(), header_[0]->h());
    push_clip(r);
    for (int i=0; i<nHeader; i++) {
      update_child(*header_[i]);
      r.set_x(header_[i]->r());
    }
    // Update box in upper-right corner, if necessary
    if (r.w()>0) {
      setcolor(buttoncolor());
      fillrect(r);
    }
    pop_clip();
  }  
  Item::clear_style();
}

////////////////////////////////////////////////////////////////
// Scrolling and layout:

/*!  If the current item is a parent, set the open state (the
  fltk::VALUE flags) to the given value and redraw the browser
  correctly. Returns true if the state was actually changed, returns
  false if it was already in that state.  */
bool Browser::set_item_opened(bool open)
{
  if (!item() || !item_is_parent()) return false;
  if (open) {
    if (item()->flags() & VALUE) return false;
    item()->set_flag(VALUE);
  } else {
    if (!(item()->flags() & VALUE)) return false;
    item()->clear_flag(VALUE);
  }
  list()->flags_changed(this, item());
  relayout(LAYOUT_CHILD);
  return true;
}

/*!  Turn off or on the fltk::INVISIBLE flag on the given item and
  redraw the browser if necessary. Returns true if the state was
  actually changed, returns false if it was already in that state.  */
bool Browser::set_item_visible(bool value)
{
  if (!item()) return false;
  if (value) {
    if (item()->visible()) return false;
    item()->set_flag(INVISIBLE);
  } else {
    if (!item()->visible()) return false;
    item()->clear_flag(INVISIBLE);
  }
  list()->flags_changed(this, item());
  if (open_level[HERE] >= item_level[HERE]) relayout(LAYOUT_CHILD);
  return true;
}

void Browser::layout() {
  // This flag is used by relayout() to indicate that autoscroll is needed:
  bool scroll_to_item = layout_damage()&LAYOUT_CHILD;

  // clear the flags first so the other methods know it is ok to measure
  // the widgets:
  Widget::layout();

  Item::set_style(this);
  const int *last_columns = fltk::column_widths();
  fltk::column_widths(column_widths_p);

  // figure out the visible area:
  const int sw = scrollbar_width();
  interior.set(0,0,w(),h());
  box()->inset(interior);
  if (scrollbar.visible()) interior.move_r(-sw);
  if (hscrollbar.visible()) interior.move_b(-sw);

  int headerh = 0;
  if (header_) {
    header_[0]->layout();
    headerh = header_[0]->h();
    interior.move_y(headerh);
  }

  // Measure the height of all items and find widest one
  width_ = 0;

  // count all the items scrolled off the top:
  int arrow_size = int(textsize())|1;
  if (!goto_top()) yposition_ = 0;
  else for (;;) {
    if (item_position[HERE]+item()->height() > yposition_) break;
    //if (!indented_ && item_is_parent()) indented_ = true;
    if (at_mark(FOCUS)) set_mark(FOCUS, HERE);
    int w = item()->width()+arrow_size*item_level[HERE];
    if (w > width_) width_ = w;
    if (!next_visible()) {goto_top(); yposition_ = 0; break;}
  }
  set_mark(FIRST_VISIBLE, HERE);
  // count all the rest of the items:
  if (item()) for (;;) {
    if (at_mark(FOCUS)) set_mark(FOCUS, HERE);
    int w = item()->width()+arrow_size*item_level[HERE];
    if (w > width_) width_ = w;
    //if (!indented_ && item_is_parent()) indented_ = true;
    if (!next_visible()) break;
  }
  if (indented()) width_ += arrow_size;
  height_ = item_position[HERE];

  // Do we have flexible column?
  bool has_flex = false;

  if (header_) {
    // calculate the combined column width and detect flexible column
    int col_width = 0, i;
    for (i=0; i<nHeader; i++) {
      int itemwidth = (i<nColumn)?column_widths_i[i]:0;
      if (itemwidth==0) itemwidth = -1;
      if (itemwidth<0)
        has_flex = true;
      else
        col_width += itemwidth;
    }
    //if (col_width > width_)
    // Always set to headers width
    width_ = col_width;
  }

  // Act as width=0, if we have flexible column so hcrollbar is invisible always
  if (has_flex) width_ = 0;

  // turn the scrollbars on and off as necessary:
  for (int z = 0; z<2; z++) {
    if (height_ > interior.h() || yposition_) {
      if (!scrollbar.visible()) {
	scrollbar.set_visible();
	interior.move_r(-sw);
	redraw(DAMAGE_ALL);
      }
    } else {
      if (scrollbar.visible()) {
	scrollbar.clear_visible();
	interior.move_r(sw);
	redraw(DAMAGE_ALL);
      }
    }
    if (width_ > interior.w() || xposition_) {
      if (!hscrollbar.visible()) {
	hscrollbar.set_visible();
	interior.move_b(-sw);
	redraw(DAMAGE_ALL);
      }
    } else {
      if (hscrollbar.visible()) {
	hscrollbar.clear_visible();
	interior.move_b(sw);
	redraw(DAMAGE_ALL);
      }
    }
  }

  // If we have flexible column, set width to W
  if (has_flex) width_ = interior.w();

  if (scrollbar.visible() && scrollbar_align()&ALIGN_LEFT) interior.move(sw,0);
  if (hscrollbar.visible() && scrollbar_align()&ALIGN_TOP) interior.move(0,sw);

  scrollbar.resize(scrollbar_align()&ALIGN_LEFT ?
		   interior.x()-sw : interior.r(),
		   interior.y(), sw, interior.h());
  scrollbar.value(yposition_, interior.h(), 0, height_);
  scrollbar.linesize(textsize()+leading());

  hscrollbar.resize(interior.x(),
		    scrollbar_align()&ALIGN_TOP ?
		    interior.y()-sw : interior.b(),
		    interior.w(), sw);
  hscrollbar.value(xposition_, interior.w(), 0, width_);
  hscrollbar.linesize(scrollbar.linesize());

  if (header_) {
    // first, calculate the combined column width    
    int width = 0, nflex = 0, i;
    for (i=0; i<nHeader; i++) {
      int itemwidth = (i<nColumn)?column_widths_i[i]:0;
      if (itemwidth==0) itemwidth = -1;
      if (itemwidth<0)
        nflex -= itemwidth;
      else
        width += itemwidth;
    }
    int space = interior.w()-width; // number of pixels that will fill the flex columns
    int hx = interior.x();          // current x position for this column
    // now set the actual column widths
    for (i=0; i<nHeader; i++) {
      Widget *hi = header_[i];
      int itemwidth = (i<nColumn)?column_widths_i[i]:0;
      if (itemwidth==0) itemwidth = -1;
      if (itemwidth<0)
        itemwidth = -itemwidth*space/nflex;
      int ww = itemwidth;
      if (ww<0) ww = 0;
      if (column_widths_p)
        column_widths_p[i] = itemwidth;
      hi->resize(-xposition_+hx, interior.y()-headerh, ww, headerh);	  
      hi->layout();
      hx += itemwidth;
    }
  }
  
  layout_damage(0); // resize of scrollbars may have turned this on

  // Now that we got the sizes of everything, scroll to show current item:
  if (scroll_to_item) {
    goto_mark(FOCUS);
    make_item_visible(NOSCROLL);
  }

  redraw(DAMAGE_CONTENTS); // assumme we need to redraw
  fltk::column_widths(last_columns);
  Item::clear_style();
}

void Browser::hscrollbar_cb(Widget* o, void*) {
  ((Browser*)(o->parent()))->xposition(int(((Scrollbar*)o)->value()));
}

/*! Set the horizontal scrolling position, measured in pixels. Zero is
  the normal position where the left edge of the child widgets is visible. */
void Browser::xposition(int X) {
  int dx = xposition_-X;
  if (dx) {
    xposition_ = X; 
    scrolldx += dx; 
    if (nHeader) relayout();
    redraw(DAMAGE_VALUE);
  }
}

void Browser::scrollbar_cb(Widget* o, void*) {
  ((Browser*)(o->parent()))->yposition(int(((Scrollbar*)o)->value()));
}

/*! Set the vertical scrolling position, measured in pixels. Zero means
  the top of the first item is visible. Positive numbers scroll the
  display up. */
void Browser::yposition(int Y) {
  if (Y == yposition_) return;
  ((Slider*)(&scrollbar))->value(Y);
  goto_position(Y);
  set_mark(FIRST_VISIBLE, HERE);
  scrolldy += (yposition_-Y); redraw(DAMAGE_VALUE);
  yposition_ = Y;
}

////////////////////////////////////////////////////////////////
// Event handling

/*! Change the focus (the selected item, or in an fltk::MultiBrowser
  the item that has a dotted box around it, to the current item.
  This calls make_item_visible().  */
bool Browser::set_focus() {
  bool ret = !at_mark(FOCUS);
  if (ret) {
    damage_item(HERE); // so will draw focus box around item
    damage_item(FOCUS); // so focus box around old focus item will be removed
    set_mark(FOCUS, HERE); // current item is new focus item
  }
  // make the item do it's own focus highlighting:
  if (item() && item()->take_focus());
  // otherwise take the focus from any other child:
  else if (contains(fltk::focus())) fltk::focus(this);
  // scroll to show the item:
  make_item_visible();
  return ret;
}

/*! This makes the current item visible to the user.

  First it turns off the fltk::INVISIBLE flag on the current item, and
  turns off the fltk::INVISIBLE flag and opens (turning on the
  fltk::VALUE flag) all parent items. These flag changes cause
  flags_changed() to be called on any fltk::List that you have
  assigned to the browser.

  The browser is then scrolled by calling yposition() so the item is
  visible. The optional argument tells how to scroll. If not specified
  (or the default value of fltk::Browser::NOSCROLL is given) then the
  browser is scrolled as little as possible to show the item. If it is
  fltk::Browser::TOP then the item is put at the top of the
  browser. If it is fltk::Browser::MIDDLE then the item is centered
  vertically in the browser. If it is fltk::Browser::BOTTOM then the
  item is put at the bottom of the browser.

  This does nothing if the current item is null.  
*/
bool Browser::make_item_visible(linepos where) {
  if (!item()) return false;
  bool changed = set_item_visible(true);
  // make any parents open and visible:
  if (open_level[HERE] < item_level[HERE]) {
    for (int n = open_level[HERE]; n < item_level[HERE]; n++) {
      if (item_index[HERE][n] < 0) break;
      int children = this->children(item_index[HERE], n);
      if (item_index[HERE][n] >= children) break;
      Widget* i = child(item_index[HERE], n);
      i->set_visible();
      i->set_value();
      list()->flags_changed(this, item());
    }
    changed = true;
    relayout(LAYOUT_CHILD);
  } else if (!layout_damage()) {
    set_mark(TEMP,HERE);
    int h = item()->height();
    int p = item_position[HERE];
    switch (where) {
    case 0:
      if (p < yposition_) break; // act like TOP
      if (p+h-yposition_ <= interior.h()) return changed;
      // fall through:
    case BOTTOM:
      p += h-interior.h(); break;
    case MIDDLE:
      p += h-interior.h()/2; break;
    case TOP:
      break;
    }
    // clip scrolling to the useful range:
    if (p > height_-interior.h()) p = height_-interior.h();
    if (p < 0) p = 0;
    // go there:
    yposition(p);
    goto_mark(TEMP);
  } else {
    // turn on flag indicating that scrolling to item is wanted:
    relayout(LAYOUT_CHILD);
  }
  return changed;
}

/*! This is for use by the MultiBrowser subclass.
  Turn the fltk::VALUE flag on or off in the current item (use
  goto_index() to set the current item before calling this).

  If this is not a MultiBrowser, this does select_only_this()
  if \a value is true, and deselect() if \a value is false.

  If do_callback has some bits that are also in when() then the
  callback is done for each item that changes selected state.
*/
bool Browser::set_item_selected(bool value, int do_callback) {
  if (multi()) {
    //if (value) set_focus();
    if (value) {
      if (item()->selected()) return false;
      item()->set_selected();
    } else {
      if (!item()->selected()) return false;
      item()->clear_selected();
    }
    list()->flags_changed(this, item());
    damage_item(HERE);
    if (when() & do_callback) {
      clear_changed();
      this->do_callback();
    } else if (do_callback) {
      set_changed();
    }
    return true;
  } else {
    if (value) return (select_only_this(do_callback));
    else return deselect(do_callback);
  }
}

/*! Turn off selection of all items in the browser. For the normal
  (not Multi) Browser, this puts it in a special state where nothing
  is highlighted and index(0) returns -1. The user cannot get it into
  this state with the GUI.

  For a MultiBrowser the user can get this state by ctrl+clicking the
  selected items off.

  If \a do_callback has some bits that are also in when() then the
  callback is done for each item that changes selected state.
*/
bool Browser::deselect(int do_callback) {
  unset_mark(HERE); item(0);
  return select_only_this(do_callback);
}

/*! Make the given item be the current one. For the MultiBrowser subclass
  this will turn off selection of all other items and turn it on
  for this one and also set the focus here. If the selection
  changes and when()&do_callback is non-zero, the callback is done. */
bool Browser::select_only_this(int do_callback) {
  if (multi()) {
    set_focus();
    bool ret = false;
    // Turn off all other items and set damage:
    if (goto_top()) do {
      if (set_item_selected(at_mark(FOCUS), do_callback)) ret = true;
    } while (next_visible());
    // turn off any invisible ones:
    nodamage = true;
    if (goto_top()) do {
      if (set_item_selected(at_mark(FOCUS), do_callback)) ret = true;
    } while (next());
    nodamage = false;
    goto_mark(FOCUS);
    return ret;
  } else {
    if (!set_focus()) return false;
    if (when() & do_callback) {
      clear_changed();
      this->do_callback();
    } else if (do_callback) {
      set_changed();
    }
    return true;
  }
}

int Browser::handle(int event) {
  static bool drag_type; // for multibrowser

  switch (event) {
  case fltk::FOCUS:
    if (goto_mark(FOCUS) && item()) item()->take_focus();
  case UNFOCUS:
    damage_item(FOCUS);
    return 1;

  case PUSH:
  case ENTER:
  case MOVE: {
    // For all mouse events check to see if we are in the scrollbar
    // areas and send to them:
    if (scrollbar.visible() &&
        (event_y() >= scrollbar.y()) && 
	(scrollbar_align()&ALIGN_LEFT ?
	 (event_x() < scrollbar.x()+scrollbar.w()) :
	 (event_x() >= scrollbar.x())))
      return scrollbar.send(event);
    if (hscrollbar.visible() &&
	(scrollbar_align()&ALIGN_TOP ?
	 (event_y() < hscrollbar.y()+hscrollbar.h()) :
	 (event_y() >= hscrollbar.y())))
      return hscrollbar.send(event);
    if (header_ && nHeader && event_y()<header_[0]->y()+header_[0]->h()) {
      for (int i=0; i<nHeader; i++) {
        Widget *hi = header_[i];
	if (event_x()>=hi->x() && event_x()<hi->x()+hi->w())
	return hi->send(event);
      }
    }
    // find the item we are pointing at:
    if (!goto_position(event_y()-interior.y()+yposition_) && !item()) return 0;
    // set xx to how far to left of widget they clicked:
    int arrow_size = int(textsize())|1;
    int xx = interior.x()+(item_level[HERE]+indented())*arrow_size-xposition_-event_x();
    // see if they are inside the widget and it takes the event:
    if (xx <= 0 && item()->send(event));
    else fltk::belowmouse(this);
    // accept enter/move events so the browser's tooltip appears:
    if (event != PUSH) return 1;
    // drag must start & end in open/close box for it to work:
    openclose_drag = (xx > 0 && xx < arrow_size && item_is_parent());
    if (openclose_drag) {
      set_focus();
      damage_item(HERE);
      return 1;
    }
    //take_focus();
    if (multi()) {
      if (event_state(CTRL)) {
	// start a new selection block without changing state
	drag_type = !item()->selected();
	if (openclose_drag) drag_type = !drag_type; // don't change it
	set_item_selected(drag_type, WHEN_CHANGED);
	set_focus();
	event_clicks(0); // make it not be a double-click for callback
	return 1;
      } else if (event_state(SHIFT)) {
	// extend the current focus
	drag_type = !item()->selected();
	event_clicks(0); // make it not be a double-click for callback
      } else {
	select_only_this(WHEN_CHANGED);
	drag_type = true;
	return 1;
      }
    }
    goto MOUSE_TO_ITEM;}

  case DRAG:
    // find the item they are now pointing at:
    if (!goto_position(event_y()-interior.y()+yposition_) && !item()) break;
    if (openclose_drag) {
      set_focus();
      // set xx to how far to left of widget they clicked:
      int arrow_size = int(textsize())|1;
      int xx = interior.x()+(item_level[HERE]+indented())*arrow_size-xposition_-event_x();
      if (xx > 0 && xx < arrow_size && item_is_parent()) {
	if (openclose_drag != 1) {openclose_drag = 1; damage_item(HERE);}
      } else {
	if (openclose_drag == 1) {openclose_drag = 2; damage_item(HERE);}
      }
      return 1;
    }
  MOUSE_TO_ITEM:
    if (multi()) {
      // set everything from old focus to current to drag_type:
      int direction = compare_marks(HERE,FOCUS);
      set_mark(TEMP, HERE);
      for (;;) {
	set_item_selected(drag_type, WHEN_CHANGED);
	if (at_mark(FOCUS)) break;
	if (!(direction<0 ? next_visible() : previous_visible())) break;
      }
      goto_mark(TEMP);
      set_focus();
    } else {
      select_only_this(WHEN_CHANGED);
    }
    return 1;

  case RELEASE:
    goto_mark(FOCUS);
    if (openclose_drag == 1 || event_clicks() && item_is_parent()) {
      // toggle the open/close state of this item:
      set_item_opened(!(item()->flags()&VALUE));
      event_is_click(0); // make next click not be double
      if (when()&WHEN_CHANGED) do_callback();
      return 1;
    } else if (openclose_drag) return 1;
    if (event_clicks()) {
      // double clicks act like ReturnKey
      //e_keysym = ReturnKey; // fltk1 did this
      clear_changed();
      execute(item());
      return 1;
    }
    goto RELEASE;

  case KEY:
    event_clicks(0); // make program not think it is a double-click
    switch (event_key()) {
    case RightKey:
      if (goto_visible_focus())
	{set_item_opened(true); next_visible();}
      goto AFTER_MOVEMENT_KEY;
    case LeftKey:
      if (goto_visible_focus())
	if (!set_item_opened(false)) {previous_visible(); set_item_opened(false);}
      goto AFTER_MOVEMENT_KEY;
    case UpKey:
      if (goto_visible_focus()) previous_visible();
      goto AFTER_MOVEMENT_KEY;
    case DownKey:
      if (goto_visible_focus()) next_visible();
    AFTER_MOVEMENT_KEY:
      if (!item()) return 1;
      if (multi() && event_state(SHIFT|CTRL)) {
	if (event_state(SHIFT)) set_item_selected(1,WHEN_CHANGED);
	set_focus();
      } else {
	bool did_callback = when()&WHEN_CHANGED;
        select_only_this(WHEN_CHANGED);
	// allow selection of item to destroy the browser:
	if (did_callback) return 1;
      }
      goto RELEASE;
    case SpaceKey:
      if (!multi() || !goto_visible_focus()) break;
      set_item_selected(!item()->selected(), WHEN_CHANGED);
    RELEASE:
      if ((when()&WHEN_RELEASE) &&
	  (changed() || (when()&WHEN_NOT_CHANGED))) {
	clear_changed();
	do_callback();
      }
      return 1;
    case ReturnKey:
    case KeypadEnter:
      if (!(when() & WHEN_ENTER_KEY)) break;
      if (!goto_visible_focus()) break;
      clear_changed();
      execute(item());
      return 1;
    default:
      if (scrollbar.send(event)) return 1;
      if (hscrollbar.send(event)) return 1;
    }
    break;

  case MOUSEWHEEL:
    return scrollbar.send(event);
#if 0
    int n = event_dy() * Style::wheel_scroll_lines;
    goto_mark(FIRST_VISIBLE);
    while (n > 0 && previous_visible()) n--;
    while (n < 0 && next_visible()) n++;
    set_top();
    return 1;
#endif
  }
  return 0;
}

/*! Go to the focus if it is visible and return it.
  If it is not visible, go to the top of the visible region and return
  zero. This is used by keystrokes so the browser does not scroll
  unexpectedly. */
Widget* Browser::goto_visible_focus() {
  if (item_position[FOCUS] >= yposition_ &&
      item_position[FOCUS] <= yposition_+interior.h()) {
    if (goto_mark(FOCUS)) return item();
  }
  if (goto_mark(FIRST_VISIBLE)) {
    if (item_position[HERE] < yposition_) next_visible();
    if (item()) return 0;
  }
  goto_top();
  return 0;
}

/*! Go to a nested item. indexes must contain level+1 index
  numbers. The first number indicates the top-level item number, the
  second indicates the child number of that parent, and so on. This
  sets the current item() to the given item and also returns it. If
  the values are out of range then null is returned.

  A negative number in indexes[0] will make it go into a special
  no-item state where select_only_this() will do deselect().
*/
Widget* Browser::goto_index(const int* indexes, int level) {
  // negative numbers make nothing be selected:
  if (indexes[0] < 0) {
    item_index[HERE][0] = -1;
    item(0);
    return 0;
  }
  // go to the 0'th item if needed (otherwise go to the focus):
  if (!indexes[0] && !level || layout_damage() || !goto_mark(FOCUS)) {
    item_level[HERE] = 0;
    open_level[HERE] = 0;
    item_position[HERE] = 0;
    item_index[HERE][0] = 0;
    siblings = children(item_index[HERE],0);
    if (siblings <= 0) {item(0); return 0;}// empty browser
    item(child(item_index[HERE],0));
    // quit if this is correct:
    if (!level && !indexes[0]) return item();
  } else {
    // move from the focus backwards until we are before it:
    while (::compare_marks(item_index[HERE],item_level[HERE],indexes,level)>0)
      if (!previous_visible()) {goto_index(0); break;}
  }
  // move forward until we are after it:
  for (;;) {
    int n = ::compare_marks(item_index[HERE],item_level[HERE],indexes,level);
    if (!n) return item(); // we found it!
    if (n > 0 || !next_visible()) break;
  }
  // if we get here we passed the item because it was invisible or a
  // parent was closed or invisible, directly set the indexes in this case:
  set_level(level);
  for (int n = 0; n <= level; n++) item_index[HERE][n] = indexes[n];
  item(child(item_index[HERE], item_level[HERE]));
  return item();
}

/*! Go to the i'th item in the top level. If i is out of range null is
  returned. */
Widget* Browser::goto_index(int i) {
  return goto_index(&i,0);
}

/*! Go to an item at any level up to 5. Negative numbers indicate that
  no more levels should be looked at. */
Widget* Browser::goto_index(int a, int b, int c, int d, int e) {
  int indexes[6];
  int i = 0;
  indexes[0] = a;
  if (b>=0) {
    indexes[++i] = b;
    if (c>=0) {
      indexes[++i] = b;
      if (d>=0) {
	indexes[++i] = d;
	if (e>=0) {
	  indexes[++i] = e;
	}
      }
    }
  }
  return goto_index(indexes,i);
}

/*!  Sets the horizontal locations that each '\\t' character in an item
  should start printing text at. These are measured from the left edge
  of the browser, including any area for the open/close + glyphs.  

  \li Array must end with 0 (zero) always
  
  \li You can define flexible column by setting column width to -1.
      If you have flexible column in browser, all columns are resized to 
      match width of the browser, by resizing flexible column.

\code
  // Example 1: make three columns, total width of columns is 300 pixels.
  // Columns are resizable, but total width is kept always.
  const int widths[]   = { 100, 100, 100, 0 };

  // Example 2: make three columns, total width of columns is always width of the browser.
  // Columns are resizable, third column is flexible and will take remaining space left.
  const int widths[]   = { 100, 100, -1, 0 };
\endcode
  */
void Browser::column_widths(const int *t) {
  column_widths_ = t;
  int pnc = nColumn;
  nColumn = 0;
  // count the number of columns, end with 0 or <-1 , -1 is used for flexible column
  if (t) while (*t++!=0) nColumn++;
  if (nColumn==0) {
    // free the column memory
    if (column_widths_p) free(column_widths_p);
    column_widths_p = 0;
    if (column_widths_i) free(column_widths_i);
    column_widths_i = 0;
  } else {
    // reallocate the column storage if needed
    if (nColumn>pnc) {
      column_widths_p = (int*)realloc(column_widths_p, (nColumn+1)*sizeof(int));
      column_widths_i = (int*)realloc(column_widths_i, (nColumn+1)*sizeof(int));
    }
    // copy the widths over into the new array
    memcpy(column_widths_p, column_widths_, (nColumn+1)*sizeof(int));
    memcpy(column_widths_i, column_widths_, (nColumn+1)*sizeof(int));
  }
  // now recalculate the layout
  relayout();
  redraw();
}

int Browser::set_column_start(int col, int x) {
  // we must adjust all this column and the column to the left so that the
  // resulting edge ends at x
  if (col<=0) return -1; // we don't adjust the first column
  if (col>nColumn) return -1; // out of bounds

  bool has_flex = false;
  // find the current column x and calculate the desired delta
  int ox = 0;
  for (int i=0; i<col; i++) { 
    ox += column_widths_p[i];
    if (column_widths_i[i]==-1) has_flex = true;
  }
  int dx = x + xposition_ - ox;
  int cwp, cwi;

  if (col==nColumn) {
    // Cant tesize last column, if we have flexible column
    int *p=column_widths_i; while (*p!=0) { if (*p++==-1) return 0; }
    // Resize last column.
    if (column_widths_p[col]==-1) return 0;
    if (column_widths_p[col-1]+dx<4) dx = -column_widths_p[col-1]+4;
    column_widths_i[col-1] += dx;
    goto RETURN;
  }

  // make sure that no column is smaller than 4 pixels (to the left)
  if (column_widths_p[col-1]+dx<4) {
    dx = -column_widths_p[col-1]+4;
  }
  // make sure that no column is smaller than 4 pixels (to the right)
  cwp = column_widths_p[col], cwi = column_widths_i[col];
  if (cwi>0 && cwi<cwp) cwp = cwi;
  if (cwp-dx<4) {
    dx = cwp-4;
  }
  // now adjust the columns in the interactive field
  if (column_widths_i[col-1]>0) column_widths_i[col-1] += dx;
  if (has_flex) { column_widths_i[col] -= dx; }

RETURN:
  // finally recalculate the layout
  relayout();
  redraw();
  return dx;
}

void Browser::column_click_cb_(Widget *ww, void *d) {
  Browser *w = (Browser*)(ww->parent());
  w->selected_column_ = (int)d;
  w->do_callback();
  w->selected_column_ = -1;
}
#include <fltk/Font.h>
class BButton : public Button {
  uchar sides; // bit 0 set: user can drag left side, bit 1: right side
public:
  BButton(uchar s, const char *l=0) 
  : Button(0,0,0,0, l) 
  { 
    sides = s;
    align(ALIGN_INSIDE|ALIGN_CLIP);
  }
  int handle(int event) {
    static int ox = -1;
    static bool left = true;
    static bool enter_before_leave = false;
    switch (event) {
      case fltk::LEAVE: 
        if (!enter_before_leave) cursor(fltk::CURSOR_DEFAULT);
        enter_before_leave = false;
        break;
      case fltk::ENTER:
        enter_before_leave = true;
        // fall through
      case fltk::MOVE:
        if (sides&1 && fltk::event_x()<=2) {
          cursor(fltk::CURSOR_WE);
        } else if (sides&2 && fltk::event_x()>=w()-2) {
          cursor(fltk::CURSOR_WE);
        } else {
          cursor(fltk::CURSOR_DEFAULT);
        }
        if (event==fltk::ENTER) { Button::handle(event); return 1; }
        break;
      case fltk::PUSH:
        if (sides&1 && fltk::event_x()<=2) {
          left = true;
          ox = fltk::event_x_root() - x();
          return 1;
        } else if (sides&2 && fltk::event_x()>=w()-2) {
          left = false;
          ox = fltk::event_x_root() - x() - w();
          return 1;
        }
        break;
      case fltk::DRAG: {
        if (ox==-1) break;
        Browser *w = (Browser*)(parent());
        int col = (int)user_data();
        if (left)
          w->set_column_start(col, fltk::event_x_root()-ox);
        else
          w->set_column_start(col+1, fltk::event_x_root()-ox);
        return 1; }
      case fltk::RELEASE:
        if (ox==-1) break;
        ox = -1;
        return 1;
    }
    return Button::handle(event);
  }

  void layout() {
    setfont(labelfont(),labelsize());
    h(int(getascent()+getdescent()+leading()+box()->dh()));
    Button::layout();
  }
};

/*! Set an array of labels to put at the top of the browser. The initial
  sizes of them are set with column_widths(). Items in the browser can
  print into the correct columns by putting '\\t' characters into
  their text. Or they can look at fltk::column_widths() to find
  the settings from their draw() methods.
*/
void Browser::column_labels(const char **t) {
  column_labels_ = t;
  int i;
  if (header_) {
    for (i=0; i<nHeader; i++) delete header_[i];  
    delete[] header_;
  }
  nHeader = 0; header_ = 0;
  if (t) { // create new header_ widgets
    Group *g = Group::current();
    Group::current(0);
    while (*t++) nHeader++;
    header_ = new Widget*[nHeader];
    for (i=0; i<nHeader; i++) {
      uchar sides = 0;
      if (i>0) sides |= 1;
      if (i<nHeader) sides |= 2;
      header_[i] = new BButton(sides, column_labels_[i]);
      header_[i]->parent(this);
      header_[i]->callback(column_click_cb_, (void*)i);
    }
    Group::current(g);
  }
  layout();
}

/*! \fn int Browser::selected_column()
  It will call the callback() if the user clicks on a column title.
  Check this to see which one they clicked. This will return a
  negative number if the callback is being done for some other
  reason, such as the user clicking on an item.
*/

////////////////////////////////////////////////////////////////
// Fltk 1.0 emulation
// Items are numbered and children of top-level items are ignored.
// In 1.0 the item numbers started at 1, I changed this to zero so
// this matches Menu.

/*! Same as goto_index(line),set_item_selected(value), to change the
  selected state of an item in a non-hierarchial
  MultiBrowser. If \a line is out of range nothing happens. */
bool Browser::select(int line, bool value) {
  if (!goto_index(line)) return false;
  return set_item_selected(value, false);
}

/*! Does goto_index(line),item_selected() to return the selection
  state of an item in a non-hierarchial MultiBrowser. If \a line
  is out of range it returns false.
*/
bool Browser::selected(int line) {
  if (!goto_index(line)) return false;
  return item()->selected();
}

/*! Convenience function for non-hierarchial browsers.
  Returns true if the indexed item is visible (ie not hidden).
*/
bool Browser::displayed(int line) {
  if (!goto_index(line)) return false;
  return item()->visible();
}

/*! Convenience function for non-hierarchial browsers. Make the
  indexed item be visible() and scroll the browser so it can be seen
  by the user. */
bool Browser::display(int line, bool value) {
  if (!goto_index(line)) return false;
  return set_item_visible(value);
}

/*! \fn int Browser::topline() const
  Convenience function for non-hierarchial browsers.
  Returns the index if the top-level item that is at the top of the
  scrolling window. */

/*! \fn void Browser::topline(int line)
  Convenience function for non-hierarchial browsers.
  Make the indexed item visible and scroll to put it at the top of
  the browser. */

/*! \fn void Browser::middleline(int line)
  Convenience function for non-hierarchial browsers.
  Make the indexed item visible and scroll to put it in the middle
  of the browser if it is not already visible (passes NO_SCROLL
  to make_item_visible(). */

/*! \fn void Browser::bottomline(int line)
  Convenience function for non-hierarchial browsers.
  Make the indexed item visible and scroll to put it at the bottom of
  the browser. */

////////////////////////////////////////////////////////////////
// Constructor

#define SLIDER_WIDTH 16

static void revert(Style* s) {
  s->glyph_ = ::browser_glyph;
}
static NamedStyle style("Browser", revert, &Browser::default_style);
/*! This style mostly serves to set the parenting back to
  Widget::default_style to avoid the gray color and larger leading
  set by Menu::default_style. However it also sets it's own glyph()
  function to one that draws the [+] and [-] indicators.
*/
NamedStyle* Browser::default_style = &::style;

static int nogroup(int x) {Group::current(0); return x;}

/*! The constructor makes an empty browser. */
Browser::Browser(int X,int Y,int W,int H,const char* L)
  : Menu(X,Y,W,H,L),
    scrollbar(nogroup(X+W-SLIDER_WIDTH),Y,SLIDER_WIDTH,H-SLIDER_WIDTH),
    hscrollbar(X,Y+H-SLIDER_WIDTH,W-SLIDER_WIDTH,SLIDER_WIDTH) {
  set_click_to_focus();
  style(default_style);
  xposition_ = 0;
  yposition_ = 0;
  scrolldx = scrolldy = 0;
  hscrollbar.parent(this);
  hscrollbar.callback(hscrollbar_cb);
  scrollbar.set_vertical();
  scrollbar.parent(this);
  scrollbar.callback(scrollbar_cb);
  indented_ = 0;
  column_widths_ = 0;
  column_widths_p = 0;
  column_widths_i = 0;
  column_labels_ = 0;
  selected_column_ = -1;
  nColumn = 0;
  nHeader = 0; header_ = 0;
  // set all the marks to the top:
  levels = 0;
  for (int i = 0; i < NUMMARKS; i++) {
    // allocate space for the top level of indexes
    item_index[i] = (int*)malloc(sizeof(int));
    item_index[i][0] = 0;
    item_position[i] = 0;
    item_level[i] = 0;
  }
  Group::current(parent());
}

/*! The destructor deletes all the list items (because they are child
  fltk::Widgets of an fltk::Group) and destroys the browser. */
Browser::~Browser() {
  int i; for (i = 0; i < NUMMARKS; i++) free(item_index[i]);
  if (column_widths_p) free(column_widths_p);
  if (column_widths_i) free(column_widths_i);
  if (header_) {
    for (i=0; i<nHeader; i++) delete header_[i];  
    delete[] header_;
  }
}

////////////////////////////////////////////////////////////////

/*! \class fltk::MultiBrowser

  The fltk::MultiBrowser class is a subclass of fltk::Browser which
  lets the user select any set of the lines. Clicking on an item
  selects only that one. Ctrl+click toggles items on/off. Shift+drag
  (or shift+arrows) will extend selections. Normally the callback is
  done when any item changes it's state, but you can change this with
  when().

  See fltk::Browser for methods to control the display and "current
  item", and fltk::Menu for methods to add and remove lines from the
  browser.

  The methods on fltk::Browser for controlling the "value" control which
  item has the keyboard focus in a multi-browser. You must use the
  "select" methods described here to change what items are turned on:
  - set_item_selected()
  - select_only_this()
  - deselect()
  - select()
  - selected()

*/

//
// End of "$Id: Fl_Browser.cxx,v 1.104 2005/01/25 09:49:11 spitzak Exp $".
//

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us