table.anubis 51.2 KB
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

   
   
                                  The Anubis Project.
   
                              A Widget System (4th version)
   
                                   The table widget. 
   
                         Copyright (c) Alain Prouté 2004-2005. 

   
   Authors:   Alain Prouté
              Olivier Duvernois


read tools/basis.anubis   
   
read widget.anubis   
read tools.anubis
   
   
   In  this file the  'table' widget  is defined.   A table  enables to  display a  set of
   widgets  in  rows  and  columns.   Each  widget occupies  a  so-called  'cell'  of  the
   table. Each  cell has parameters which allow  a precise positioning of  the cell within
   the table. Also a  cell may span over several columns and/or  rows.  A separation space
   (some kind of 'glue') may be put between the rows and/or columns of the table, but also
   within each cell, and around the table.

                   +---------+--------+------+--------------+-----------+
                   |         |        |      |              |           |
                   |         |        |      |              |           |
                   |         |        |      |              |           |
                   +---------+--------+------+--------------+-----------+
                   | cell spaning     |                     |           |
                   | over 2 columns   |     cell spaning    |           |
                   |                  |     over 3 rows     |           |
                   +---------+--------+     and 2 columns   +-----------+
                   |         |        |                     | cell      |
                   |         |        |                     | spaning   |
                   |         |        |                     | over 2    |
                   +---------+--------+                     | rows      |
                   |         |        |                     |           |
                   |         |        |                     |           |
                   |         |        |                     |           |
                   +---------+--------+---------------------+-----------+
   
   The vertical position of a widget into its cell is defined by: 
   
public type WidgetVerticalPosition:        // vertical positioning
   top, 
   center,
   bottom. 

   and similarly for the horizontal position:
   
public type WidgetHorizontalPosition:      // horizontal positioning
   left,
   center,
   right.

   Now a cell  includes a vertical position, a horizontal position,  the number of columns
   and rows spanned by the cell, and a content (which is a widget).

public type WidgetTableCellBackground:
   none,                            // use default table background
   color          (RGB), 
   image_centered (HostImage), 
   image_tiled    (HostImage). 
   
public type WidgetCell:
   cell
     (
       WidgetVerticalPosition         vpos,
       WidgetHorizontalPosition       hpos,
       Word32                         col_span,
       Word32                         row_span,
       WidgetTableCellBackground      background, 
       Widget                         content
     ).
   
   For your convenience, we define the special case:
   
public define WidgetCell
   cell
     (
       WidgetVerticalPosition         vpos,
       WidgetHorizontalPosition       hpos,
       WidgetTableCellBackground      background, 
       Widget                         content
     ) = 
   cell(vpos,hpos,1,1,background,content).

   which may  be used for  (most usual) cells  spanning just over 1  column and 1  row. We
   define also this one:
   
public define WidgetCell
   cell
     (
       Widget content
     ) =
   cell(center,center,1,1,none,content). 
   
      
   The table  also may have  glue between  the columns (hglue)  and glue between  the rows
   (vglue). Use value 0  for no glue at all.  The cells themselves are  given as a list of
   lists of cells. Each list of cells represents  a row of the table. Now, tables may have
   several presentation styles.
   
public type WidgetTableStyle:
   nude, 
   borders (RGB                  cell_background_color, 
            RGB                  edge_color, 
            Word32               inner_edge,
            Word32               outer_edge). 
   
   The 'nude' table does not show anything but the widgets in the cells. In particular the
   father widget of the table may be visible through the spaces between the widgets in the
   cells. On the contrary,  the 'borders' table fills the gap between  widgets so that you
   cannot see anything behind the table. The  glue between cells is filled with the 'main'
   color taken  from 'parameters'. The egdes  (inner: around each cell,  and outer: around
   the table)  also have the  main color  (but with relief  effects).  The space  (if any)
   between the  inner edges  and the widgets  in the  cells receives the  'cell background
   color'.
   
   
   Here is the function for creating a table.

public define Widget
   table
     (
       WidgetTableStyle         style, 
       Word32                   vglue,
       Word32                   hglue, 
       List(List(WidgetCell))   rows_of_cells
     ). 
   
   
   
   
   --- That's all for the public part ! --------------------------------------------------

   
   

   ------------------------------ Table of Contents --------------------------------------
   
   *** [1] Logical computations. 
   *** [2] Metric computations. 
   *** [3] Computing relative positions of childs. 
   *** [4] Recomputing the metrics. 
   *** [5] Drawing borders and background. 
   *** [6] Transmitting events. 
   *** [7] Creating the table widget. 
   
   ---------------------------------------------------------------------------------------

read trace.anubis   
   
         
   The 'childs widgets' are those widgets which are in the cells of the table.  We must be
   able to compute and remember the relative positions of all childs in the table. This is
   required for  drawing and transmitting  events.  However, this  will not be  enough for
   drawing the table  in the case of the  'borders' style.  In this case, we  also need to
   know the list of widths of the  columns (including neither 'iedge' nor 'hglue') and the
   list  of heights of  the rows  (including neither  'iedge' nor  'vglue').  Essentially,
   these computations are  made non trivial by  the fact that cells may  span over several
   columns and/or rows.  Otherwise, it would be fairly simple.
   
   We  separate  the  computations  into   two  sorts:  'logical'  and  'metric'.   Metric
   computations are those which involve computations of widths and heights or positions in
   pixels. Logical computations are the others. It is clear that cells in the i-th list of
   WidgetCells are  in the i-th row  of the table. On  the contrary, the j-th  cell in the
   i-th list  of WidgetCells needs  not be in  the j-th column  of the table,  because its
   column depends  on the number  of columns spanned  by cells on left  of it in  the same
   list, but also  on cells of previous rows spanning over  several rows. Hence, computing
   the number of the column at which a cell starts is our main 'logical' computation.
   
   When the row number and column number of each cell is known, it's easier to compute the
   widths of  the columns and heights  of the rows of  the table. Indeed, each  cell has a
   'contribution' to the width of its columns.  If a cell spans over several columns, this
   contribution is divided between these columns. The process is the same for rows. Hence,
   at that point we do not need to keep  a list of lists of cells. On the contrary we just
   keep a single  (so-called 'flat') list of  cells, but within which each  cell knows its
   starting row and column.
   
   
   
   
   *** [1] Logical computations. 
   
   We begin by defining an enriched sort of cell. 
   
type LogicalCell:
   cell
     (
       WidgetVerticalPosition     vpos,
       WidgetHorizontalPosition   hpos,
       Word32                     col_span,
       Word32                     row_span,
       Word32                     column_number, 
       Word32                     row_number, 
       WidgetTableCellBackground  background, 
       Widget                     content
     ).
   
   Our objective is to replace the given list  of lists of WidgetCells by a single list of
   LogicalCells. This is performed by:
   
define List(LogicalCell)
   logical_computation
     (
       List(List(WidgetCell)) original_cells
     ).   
   
   We  proceed by induction  on the  original list.  Of course,  at each  step we  need to
   remember the cells of the previous rows,  still spanning over the current row. For each
   such 'cell from above' the pertinent information is:
   
type CellFromAbove:
   cell_from_above(Word32    starting_column,      // first column of cell
                   Word32    col_span,             // number of columns spanned by the cell
                   Word32    row_span_remainder).  // number of rows spanned by the cell
                                                   // starting at current row

   To process a  single row (function 'logical_row_computation' below),  we start with the
   list of  cells from above, the row  itself (of type 'List(WidgetCell)'),  the number of
   the current row (i.e. of this row).  Notice that columns and rows are numbered starting
   from 0.  Actually our function  is able to  perform a computation  for any tail  of the
   current row. Hence, we  also need to maintain a current column  number. For such a tail
   of row, our function returns a pair of lists of type:
   
                          (List(LogicalCell),List(CellFromAbove))
   
   The  first  list contains  the  cells  created from  this  tail  of  row (actually  one
   'LogicalCell' for each  'WidgetCell' in our tail of row). The  second list contains the
   cells from above  starting at or after the  current column number, to be  used for next
   row.
   
define (List(LogicalCell),List(CellFromAbove))
   logical_row_computation
     (
       List(CellFromAbove)    above,           // previous list of cells from above
       List(WidgetCell)       row,
       Word32                  current_column,
       Word32                  current_row
     ).   
   
   The function  'logical_row_computation' proceeds by  induction on the  'WidgetCells' in
   'row'.
   
   Case 1. There is no more 'WidgetCell' in 'row'
   
      Case 1.1. There is no cell from above in 'above'. 
   
         In this case,  we produce no new  'LogicalCell', and no cell from  above for next
         row. Hence, the result is ([ ],[ ]).
   
      Case 1.2. There is at least one cell from above in 'above'. 
   
         The  only  thing  we  have   to  do  is  to  decrement  the  'row_span_remainder'
         component.  If it  becomes  0 this  cell from  above  does not  survive for  next
         row. Otherwise, it survives for next row.  We continue until we have no more cell
         from above in 'above'.
   
   Case 2. There is at least one WidgetCell in 'row'. 
   
      Case 2.1. There is no cell from above in 'above'. 
   
         Of course, we create one new  'LogicalCell', and perhaps one new cell from above,
         if our  cell spans over several  rows.  The remaining cells  are treated starting
         from current_column + the number of columns spanned by our cell.
   
      Case 2.2. There is at least one cell from above in 'above'. 
   
         The difference with case 2.1. is that we can meet a cell from above.
   
         Case 2.2.1.  The current  column is the  starting column  of the first  cell from
                      above.
   
            In this case, we apply our function  to the row, but starting from the current
            column + the  number of columns spanned  by the first cell from  above, and we
            just have to update (or eventally kill) this cell from above. 
   
         Case 2.2.2.  The current  column is  strictly before the  starting column  of the
                      first cell from above.
   
            In  this  case  the  number  of  columns spanned  by  the  WidgetCell  may  be
            diminuished so that the cell fits on the left of the first cell from above. We
            may have to create a new cell from above for next row, and the remaining cells
            are treated starting at current column  + the new number of columns spanned by
            our cell.
   
   
   Now, here is the function 'logical_row_computation'. 
   
define (List(LogicalCell),List(CellFromAbove))
   logical_row_computation
     (
       List(CellFromAbove)    above, 
       List(WidgetCell)       row,
       Word32                  current_column,
       Word32                  current_row
     ) = 
   if row is 
     {
       [ ] then if above is 
         {
           [ ] then 
             //
             // --- Case 1.1. --------------------
             //
             ([ ],[ ]),
   
           [ac1 . acs] then 
             //
             // --- Case 1.2. --------------------
             //
             if ac1 is cell_from_above(s_col,col_span,row_span) then 
             if logical_row_computation(acs,[ ],0,0) is (_,new_above) then 
             if row_span -=< 1
             then ([ ],new_above)
             else ([ ],[cell_from_above(s_col,col_span,row_span-1) . new_above])   
         },
   
       [wc1 . wcs] then 
         if wc1 is cell(vpos,hpos,wc1_col_span,wc1_row_span,bg,content) then
         if above is 
           {
             [ ] then 
               //
               // --- Case 2.1. --------------------
               //
               if logical_row_computation([],wcs,current_column+wc1_col_span,current_row) is 
                 (other_new_cells,other_new_above) then 
                 with new_cells = (List(LogicalCell))[cell(vpos,hpos,
                                        wc1_col_span,wc1_row_span,
                                        current_column,current_row,
                                        bg,content) . other_new_cells], 
                 if wc1_row_span -=< 1
                 then (new_cells,other_new_above)
                 else (new_cells,[cell_from_above(current_column,
                                                  wc1_col_span,
                                                  wc1_row_span-1) . other_new_above]),
   
             [ac1 . acs] then 
               //
               // --- Case 2.2. --------------------
               //
               if ac1 is cell_from_above(ac1_start,ac1_col_span,ac1_row_span) then 
               if ac1_start -=< current_column
                 //
                 // --- Case 2.2.1. --------------------
                 //
                 then (if logical_row_computation(acs,row,current_column+ac1_col_span,current_row) is 
                   (new_cells,other_new_above) then 
                     if wc1_row_span -=< 1
                     then (new_cells,other_new_above)
                     else (new_cells,[cell_from_above(current_column+ac1_col_span,
                                                      wc1_col_span,
                                                      wc1_row_span-1) 
                                     . other_new_above]))
                 //
                 // --- Case 2.2.2. --------------------
                 //
                 else (with new_col_span = min(wc1_col_span,ac1_start-current_column),
                       if logical_row_computation(above,wcs,
                                                  current_column+new_col_span,
                                                  current_row) is 
                   (other_new_cells,other_new_above) then 
                   with new_cells = (List(LogicalCell))[cell(vpos,hpos,
                                                      new_col_span,wc1_row_span,
                                                      current_column,current_row,
                                                      bg,content) . other_new_cells], 
                     if wc1_row_span -=< 1
                     then (new_cells,other_new_above)
                     else (new_cells,[cell_from_above(current_column,
                                                      new_col_span,
                                                      wc1_row_span-1) 
                                     . other_new_above]))
           }
     }. 
   
   
   
   Now, its easy  to compute the list of  'LogicalCells' for a tail of  our table, knowing
   the cells from above for this tail of table.
   
define List(LogicalCell)
   logical_computation
     (
       List(CellFromAbove)      above_cells,
       List(List(WidgetCell))   original_cells,
       Word32                    current_row
     ) =
   if original_cells is 
     {
       [ ] then [ ], 
   
       [row1 . rows] then 
         if logical_row_computation(above_cells,row1,0,current_row) is 
           (new_cells,new_above_cells) then 
             new_cells + logical_computation(new_above_cells,rows,current_row+1)
     }. 
   

   Now, we can perform the logical computation for the whole table. 
   
define List(LogicalCell)
   logical_computation
     (
       List(List(WidgetCell)) original_cells
     ) = 
   logical_computation([],original_cells,0). 
   

   
   
   

   *** [2] Metric computations. 

   
   
   Now   we   forget  about   'WidgetCells',   and   we  have   only   a   flat  list   of
   'LogicalCells'. Notice that  even if the size of  one of the childs changes,  we do not
   need to recompute our flat list, because the logical information does not change.  Only
   the metric computations will have to be performed again.
   
   
      *** [2.1] The method. 
   
   Our metric computation consists in computing the  list of widths of the columns and the
   list of heights of the rows of the table. This is performed by:
   
define (List(Word32),     // widths of columns
        List(Word32))     // heights of rows
   widths_and_heights
     (
       List(LogicalCell)      cells,
       Word32                 vglue, 
       Word32                 hglue, 
       Word32                 iedge
     ). 

   There is a difficulty which is arising from  the fact that a cell may span over several
   columns/rows. For example, consider the following table:
   
     +------------------------------+
     |                              |
     |      |<--- w1 -------->|     |
     |                              |
     +----------+-------------------+
     |          |                   |
     |<-- w2 -->|<------ w3 ------->|
     |          |                   |
     |          |                   |
     +----------+-------------------+
   
   containing three  cells, the first  one spanning over  the two columns. 'w1',  'w2' and
   'w3' are the widths of the contents of the cells. 
   
   In the case  of the figure, we have 'w1  =< w2+w3'. As a consequence,  the width of the
   two columns  are 'w2' and  'w3'. But, if  it happens that 'w1  > w2+w3', we  would have
   this:
   
     +--------------------------------------+
     |                                      |
     |<----------------- w1 --------------->|
     |                                      |
     +--------------+-----------------------+
     |              |                       |
     | |<-- w2 -->| | |<------ w3 ------->| |
     |              |                       |
     |              |                       |
     +--------------+-----------------------+
   
   In other words, we would have an 'excess', equal to 'w1-(w2+w3)', to be disptached (say
   by equal parts) between the two columns.
   
   As a consequence of this, our computation is in two phases:
   
      - Phase 1: we compute  the widths of the columns with a  contribution of 0 for cells
        spanning over several columns.
   
      - Phase 2:  For each cell  spanning over several  columns we compute  the difference
        between  the width  of this  cell and  the total  width (computed  so far)  of the
        columns spanned by this cell. This  is called the 'excess'. This excess is divided
        into as many (almost  equal) parts as there are spanned columns,  and each part is
        added to the width of the corresponding column.
   
   We do the same  for rows. Of course, this is a little  more complicated because we must
   also take the  glue and and inner edges  into account. Mots of the  functions below are
   used for both columns and rows, despite  the fact that our comments are in general only
   for columns.
   
   
      *** [2.2] Tools. 

   
   
         *** [2.2.1] Computing the total width of a set of columns. 
   
   Given a list of widths of columns, a  'rank' (number of first column to consider) and a
   'span' (number  of columns to consider),  we want to  compute the total width  of these
   columns. It  is assumed that the  list has enough  elements (this is true  because used
   only in phase 2).
   
define Word32
   head
     (
       List(Word32) l
     ) =
   if l is 
     {
       [ ] then alert, // will never happen because our lists have enough elements
       [h . t] then h
     }.
   
define List(Word32)
   tail
     (
       List(Word32) l
     ) =
   if l is 
     {
       [ ] then alert, // will never happen because our lists have enough elements
       [h . t] then t
     }. 
   
define Word32
   total_width
     (
       List(Word32)            widths,
       Word32                  rank, 
       Word32                  span
     ) =
   if rank = 0
   then if span = 0
        then 0
        else head(widths) + total_width(tail(widths),0,span-1)
   else total_width(tail(widths),rank-1,span). 
   
   
   
         *** [2.2.2] Taking glue and inner edges into account. 
   
   The  width that  we compute  for the  columns do  not include  the glue  nor  the inner
   edges. So, when a  cell is spanning over k columns, and if the  width of its content is
   'w', the part of its width which actually participates to the computation is:
   
                                   w - (k-1)(glue + 2*iedge)
   
   This is called the 'pertinent width'.  In the picture below, 'g' is the glue and 'i' is
   the inner edge (and k = 3).
   
                   |i|<----------------------- w ---------------------->|i|
                   |i|<--- w1 --->|i|g|i|<--- w2 --->|i|g|i|<--- w3 --->|i|
   
   
define Word32
   pertinent_width
     (
       Word32         content_width,      // width of content of cell
       Word32         span,               // number of columns spanned by the cell
       Word32         glue, 
       Word32         iedge
     ) =
   content_width - (span - 1)*(glue + 2*iedge). 
   

   
         *** [2.2.3] Dispatching the excess. 
   
   When, during  phase 2,  we have  computed the excess  of a  cell spanning  over several
   columns, and  if this excess  is at least  1, we have to  dispatch it over  the spanned
   columns.  Let's represent the  excess by 'e' and the number of  spanned columns by 's'.
   The excess per  columns should be 'e/s',  but this does not work  because this division
   may have a non zero remainder.
   
   So, we use euclidian division:
   
                                           e = qs + r
   
   with 0 =<  r < s. We will add 'q'  to the 's-r' first spanned columns  and 'q+1' to the
   'r' last spanned columns. 
   
define List(Word32)
   dispatch_excess
     (
       List(Word32)            widths_so_far, 
       Word32                  excess_per_column,   // this is our 'q' above
       Word32                  rank,
       Word32                  span_1,     // number of columns receiving 'q'
       Word32                  span_2      // number of columns receiving 'q+1'
     ) =
   if rank = 0
   then if span_1 = 0
        then if span_2 = 0
             then widths_so_far
             else [head(widths_so_far)+excess_per_column+1
                   . dispatch_excess(tail(widths_so_far),excess_per_column,0,0,span_2-1)]
        else [head(widths_so_far)+excess_per_column
              . dispatch_excess(tail(widths_so_far),excess_per_column,0,span_1-1,span_2)]
   else [head(widths_so_far) 
         . dispatch_excess(tail(widths_so_far),excess_per_column,rank-1,span_1,span_2)]. 
   
   
define List(Word32)
   dispatch_excess
     (
       List(Word32)            widths_so_far, 
       Word32                  content_width,    // width of content of spanning cell
       Word32                  rank,             // first column of cell
       Word32                  span,             // number of columns spanned by cell
       Word32                  glue, 
       Word32                  iedge
     ) =
   with excess = pertinent_width(content_width,span,glue,iedge)
                 - total_width(widths_so_far,rank,span), 
     if excess -=< 0
     then widths_so_far
     else (if excess/span is 
             {
               failure then alert, 
               success(p) then if p is (q,r) then
                   dispatch_excess(widths_so_far,q,rank,span-r,r)
             }). 
     
       
       
   
   
   
         *** [2.2.4] Updating a list of widths. 
   
   During the computation, we  have a list of widths at hand  (of type 'List(Word32)'). At
   the very beginning of  the computation, this list is empty. Hence,  we have to 'create'
   elements in  this list. At the  end of phase 1,  the list contains as  many integers as
   there are  columns in the  table. So during  phase 2, we  just have to  update existing
   widths, not to create new ones.
   
   During  phase  1, each  cell  encountered,  may create  new  elements  in  the list  of
   widths. Since a cell  may span over several columns, it may  create several widths, but
   in this case, newly created width are  0, since the contribution of such cells is taken
   into account only during phase 2.
   
   Our first function creates a list of zeros of a given length.
   
define List(Word32)
   list_of_zeros
     (
       Word32       n
     ) =
   if n -=< 0
   then [ ]
   else [0 . list_of_zeros(n-1)]. 
   
   
   The function below updates element number 'k', using value 'w' in a list of widths. The
   element is created if it does not exist.  It is used during phase 1 for a cell spanning
   just one column.
   
define List(Word32)
   update_one_element
     (
       List(Word32)       widths_so_far, 
       Word32             rank,             // first column of cell
       Word32             new_width
     ) =
   if rank = 0
   then if widths_so_far is
     {
       [ ]        then [new_width],
       [w1 . ws]  then [max(w1,new_width) . ws]
     }
   else if widths_so_far is 
     {
       [ ]        then [0 . update_one_element([],rank-1,new_width)],
       [w1 . ws]  then [w1 . update_one_element(ws,rank-1,new_width)]
     }. 
   
   
   The next function  is used during phase 1  for cells spanning at least  two columns. It
   updates a list of widths, creating new width (with value 0) if needed. 
   
define List(Word32)
   update_several_elements
     (
       List(Word32)       widths_so_far, 
       Word32             rank,             // first column of cell
       Word32             span              // number of cells spanned
     ) =
   if widths_so_far is 
     {
       [ ]        then list_of_zeros(rank+span), 
   
       [w1 . ws]  then 
         if rank = 0
         then [w1 . update_several_elements(ws,0,span-1)]
         else [w1 . update_several_elements(ws,rank-1,span)]
     }.
   


         *** [2.2.5] Chosing between columns and rows. 
   
   Most  of our  functions may  be used  for  columns and  for rows.   Nevertheless it  is
   sometimes necessary  to know if  we are  dealing with columns  or with rows.  Hence the
   following type:
   
type DealingWith:
   columns, 
   rows. 
      
   
   
      *** [2.3] Phase 1. 
   
   Now, we are ready for phase 1. 
   
define List(Word32)
   phase_1
     (
       List(LogicalCell)    cells,
       List(Word32)         so_far, 
       DealingWith          dw
     ) =
   if cells is 
     {
       [ ] then so_far,
   
       [c1 . cs] then 
         if c1 is cell(_,_,col_span,row_span,col_num,row_num,bg,content) then 
         with span = if dw is columns then col_span else row_span, 
              rank = if dw is columns then col_num  else row_num, 
         if span >- 1
         then phase_1(cs,update_several_elements(so_far,rank,span),dw)
         else phase_1(cs,update_one_element(so_far,rank,
                        if size(content) is (w,h) then 
                        if dw is columns then w else h),dw)
     }. 
   
   
   
   
   
      *** [2.4] Phase 2. 
   
   During phase 2,  we update the list of widths  obtained at the end of  phase 1. We have
   something to do for each cell spanning over at least two columns.
   
define List(Word32)
   phase_2
     (
       List(LogicalCell)       cells, 
       List(Word32)             widths_so_far,
       Word32                   glue, 
       Word32                   iedge, 
       DealingWith             dw
     ) =
   if cells is 
     {
       [ ] then widths_so_far, 
       [c1 . cs] then 
         if c1 is cell(_,_,col_span,row_span,col_num,row_num,bg,content) then 
         with span = if dw is columns then col_span else row_span, 
         if span -=< 1
         then phase_2(cs,widths_so_far,glue,iedge,dw)
         else with  rank = if dw is columns then col_num else row_num, 
            content_size = if size(content) is (w,h) then 
                           if dw is columns then w else h, 
                phase_2(cs,
                        dispatch_excess(widths_so_far,
                                        content_size,
                                        rank,
                                        span,
                                        glue,
                                        iedge),
                        glue,
                        iedge,
                        dw)
     }. 
   
   
   
   
   
      *** [2.5] computing the metrics. 
   
   
define List(Word32)
   compute_sizes
     (
       List(LogicalCell)     cells, 
       Word32                 glue, 
       Word32                 iedge,
       DealingWith           dw
     ) =
   phase_2(cells,
           phase_1(cells,[],dw), 
           glue, 
           iedge,
           dw). 
   
   
   
   
define (List(Word32),     // widths of columns
        List(Word32))     // heights of rows
   widths_and_heights
     (
       List(LogicalCell)     cells,
       Word32                 vglue, 
       Word32                 hglue, 
       Word32                 iedge
     ) =
   (compute_sizes(cells,hglue,iedge,columns),
    compute_sizes(cells,vglue,iedge,rows)). 
   
   
   
   
   
   
   
   
   *** [3] Computing relative positions of childs. 
   
   At that point  we know the width of each  column, the height of each  row, and for each
   cell the column and the row it starts  in. We define a new kind of logical cell, called
   'logical positioned cell':
   
type PositionedCell:
   cell
     (
       WidgetVerticalPosition      vpos,
       WidgetHorizontalPosition    hpos,
       Word32                      col_span,
       Word32                      row_span,
       Word32                      column_number, 
       Word32                      row_number, 
       WidgetTableCellBackground   background, 
       Widget                      content,
       Word32                      x,  // position of 'content' relative to the table
       Word32                      y,
       WidgetRectangle             bg_rect
     ).
   
   Our purpose  in this section is  to tranform our list  of 'LogicalCell' into  a list of
   'PositionedCell'.
   
   
   We need a tool for computing the sum of a sequence of consecutive integers taken from a
   list of integers. If the list 'l' is [n_0,...,n_i,...,n_j,...,n_k] then 
   
          sum(i,j,l)
   
   is the sum n_i+n_(i+1)+...+n(j-1), i.e. the sum of all integers in the list starting at
   n_i (included), stopping at n_j (not included). 
   
define Word32
   sum
     (
       Word32           i,
       Word32           j,
       List(Word32)     l
     ) =
   if l is 
     {                                    // ----------- induction schema -------------
       [ ] then 0,                        //          sum(i,j,[]) = 0
       [n1 . ns] then 
         if i -=< 0
         then if j -=< 0
              then 0                      //           sum(0,0,l) = 0
              else n1 + sum(0,j-1,ns)     // sum(0,j+1,[n1 . ns]) = n1 + sum(0,j,ns)
         else sum(i-1,j-1,ns)             //       sum(i+1,j+1,l) = sum(i,j,l)
     }.

   
   
define Word32
   sum
     (
       List(Word32) l
     ) =
   if l is 
     {
       [ ] then 0, 
       [h . t] then h + sum(t)
     }.
   
   
   
   The horizontal relative position 'xa' of the area of the cell:
   
         cell(vpos,hpos,col_span,row_span,col_num,row_num,_)
   
   is computed as:
   
     xa =
      oedge +                        // outer edge of table
      sum(0,col_num,widths) +        // contribution of columns
      col_num*hglue +                // contribution of horizontal glue
      (2*col_span+1)*iedge           // contribution of inner edges
     
   This area also has a width for accommodating the child. This width 'wa' is computed as:
   
     wa = 
      sum(col_num+1,col_num+1+col_span,widths) +  // contribution of columns
      (col_span-1)*(hglue+2*iedge)                // contribution of glue and inner edges
   
   
   Now,  the  child must  be  horizontally positioned  within  this  area. The  horizontal
   position depends on the 'hpos' parameter for this cell:
   
      left:    xa
    center:    xa + (wa-w)/2
     right:    xa + (wa-w)
   
   where w is the width of the child. 
   
   
   These  computations  are  performed  for  a  single cell  by  the  next  function.  The
   WidgetRectangle returned  is the rectangle  within which the  inner edge for  that cell
   fits exactly.
   
define (PositionedCell,WidgetRectangle)
   compute_relative_position
     (
       LogicalCell          c, 
       Word32            hglue, 
       Word32            vglue, 
       Word32            iedge, 
       Word32            oedge, 
       List(Word32)     widths, 
       List(Word32)    heights
     ) =
   if c is cell(vpos,hpos,col_span,row_span,col_num,row_num,bg,content) then 
   if size(content) is (w,h) then 
   with xa = oedge +
             sum(0,col_num,widths) +
             (col_num+1)*hglue +
             (2*col_num+1)*iedge, 
        wa = sum(col_num,col_num+col_span,widths) +
             (col_span-1)*(hglue+2*iedge), 
        ya = oedge +
             sum(0,row_num,heights) +
             (row_num+1)*vglue +
             (2*row_num+1)*iedge,
        ha = sum(row_num,row_num+row_span,heights) +
             (row_span-1)*(vglue+2*iedge),
   (cell(vpos,hpos,col_span,row_span,col_num,row_num,bg,content,
        if hpos is 
          {
            left    then xa,
            center  then xa + ((wa-w)>>1),
            right   then xa + (wa-w)
          },
        if vpos is 
          {
            top     then ya,
            center  then ya + ((ha-h)>>1),
            bottom  then ya + (ha-h)
          },rect(xa,ya,xa+wa,ya+ha)),
    rect(xa-iedge,ya-iedge,xa+wa+iedge,ya+ha+iedge)). 
   
   
   
   Now, we do  that for all cells.  The  list of rectangles around all the  inner edges is
   returned.
   
define (List(PositionedCell),List(WidgetRectangle))
   compute_relative_positions
     (
       List(LogicalCell)    l, 
       Word32            hglue, 
       Word32            vglue, 
       Word32            iedge, 
       Word32            oedge, 
       List(Word32)     widths, 
       List(Word32)    heights
     ) =
   if l is 
     {
       [ ] then ([ ],[ ]), 
       [c1 . cs] then 
         if compute_relative_position(c1,hglue,vglue,iedge,oedge,widths,heights) is
           (lpc1,r1) then 
         if compute_relative_positions(cs,hglue,vglue,iedge,oedge,widths,heights) is 
           (other_cells,other_rects) then 
         ([lpc1 . other_cells],[r1 . other_rects])
     }.
   
   
   
   
   
   *** [4] Recomputing the metrics. 
   
   When the size of a child changes the metrics of the table must be recomputed. Actually,
   this metrics is made of the following:
   
     - the list of widths of columns,
     - the list of heights of rows,
     - the total width of the table,
     - the total height of the table, 
     - the list of rectangles around the inner edges ('borders' table only), 
     - the relative positions of the childs
     
   
   We begin by 'borders' table. 
   
define One
   recompute_metrics_borders
     (
       Var(Word32)                         total_width_v, 
       Var(Word32)                         total_height_v, 
       Var(List(Word32))                   widths_v, 
       Var(List(Word32))                   heights_v, 
       Var(List(WidgetRectangle))          inner_rects_v,
       Var(List(PositionedCell))           positioned_cells_v, 
       List(LogicalCell)                   logical_cells,
       Word32                              hglue, 
       Word32                              vglue, 
       Word32                              iedge,
       Word32                              oedge
     ) = 
   if widths_and_heights(logical_cells,vglue,hglue,iedge) is (widths,heights) then 
   widths_v <- widths; 
   total_width_v <- 2*oedge + sum(widths) + (2*iedge+hglue)*(truncate_to_Word32(length(widths))) + hglue; 
   heights_v <- heights; 
   total_height_v <- 2*oedge + sum(heights) + (2*iedge+vglue)*(truncate_to_Word32(length(heights))) + vglue; 
   if compute_relative_positions(logical_cells,hglue,vglue,iedge,oedge,widths,heights) is
     (positioned_cells,rects) then 
     positioned_cells_v <- positioned_cells; 
     inner_rects_v <- rects.
   
   
   For 'nude' tables, we have the following simplified version:
   
define One
   recompute_metrics_nude
     (
       Var(Word32)                          total_width_v, 
       Var(Word32)                          total_height_v, 
       Var(List(Word32))                    widths_v, 
       Var(List(Word32))                    heights_v, 
       Var(List(PositionedCell))           positioned_cells_v, 
       List(LogicalCell)                   logical_cells,
       Word32                               hglue, 
       Word32                               vglue
     ) = 
   if widths_and_heights(logical_cells,vglue,hglue,0) is (widths,heights) then 
   widths_v <- widths; 
   total_width_v <- sum(widths) + hglue*(truncate_to_Word32(length(widths))+1); 
   heights_v <- heights; 
   total_height_v <- sum(heights) + vglue*(truncate_to_Word32(length(heights))+1); 
   if compute_relative_positions(logical_cells,hglue,vglue,0,0,widths,heights) is 
     (positioned_cells,_) then 
     positioned_cells_v <- positioned_cells. 
   
   
   
   
   *** [5] Drawing borders and background. 
   
   In the case  of the 'borders' style, we  have to draw borders, i.e.   outer edge, inner
   edges, fill vertical  and horizontal glue with  the main color, and the  areas of cells
   with the background color.
   
   Drawing the background and the inner edges around the areas of cells:
   
define One
   draw_inner_edges
     (
       WidgetDrawToolBox              dtb,
       Word32                        iedge,
       List(WidgetRectangle)        rects,
       RGB                    light_color,
       RGB                     dark_color,
       RGB                     back_color
     ) =
   if rects is 
     {
       [ ] then unique, 
       [r1 . rs] then 
         draw(dtb)(r1,back_color);
         draw_hollow_edge(dtb,iedge,r1,light_color,dark_color);
         draw_inner_edges(dtb,iedge,rs,light_color,dark_color,back_color)
     }. 
   
   
   Now, we can draw our borders. 
   
define One
   draw_borders
     (
       WidgetDrawToolBox           dtb,
       List(WidgetRectangle)     rects, 
       Word32                     hglue, 
       Word32                     vglue, 
       Word32                     oedge, 
       Word32                     iedge, 
       List(Word32)              widths,  // of columns (not including any edge or glue)
       List(Word32)             heights,  // of rows (not including any edge or glue)
       Word32               total_width,  // of table
       Word32              total_height,  // of table
       RGB                  main_color, 
       RGB                 light_color, 
       RGB                  dark_color,
       RGB                  back_color
     ) = 
   //
   // draw the glue in just one rectangle
   //
   draw(dtb)(rect(oedge,
                  oedge,
                  total_width-oedge,
                  total_height-oedge),main_color); 
   //
   // draw outer edge
   //
   draw_relief_edge(dtb,oedge,rect(0,0,total_width,total_height),light_color,dark_color); 
   //
   // draw inner edges and backgrounds
   //
   draw_inner_edges(dtb,iedge,rects,light_color,dark_color,back_color). 
   
   
   
   Drawing the background of a cell.
   
define One
   draw_tiled_image
     (
       WidgetDrawToolBox           dtb,
       HostImage                   im,
       Word32                       im_w,
       Word32                       im_h, 
       Word32                       x0, 
       Word32                       x,
       Word32                       y, 
       Word32                       u,
       Word32                       v
     ) =
   if y >=- v then unique else
   if x >=- u then draw_tiled_image(dtb,im,im_w,im_h,x0,x0,y+im_h,u,v) else
   draw(dtb)(im,x,y,rect(x,y,u,v)); 
   draw_tiled_image(dtb,im,im_w,im_h,x0,x+im_w,y,u,v). 
   
define One
   draw_cell_background
     (
       WidgetDrawToolBox           dtb, 
       WidgetTableCellBackground   bg, 
       WidgetRectangle             bg_rect
     ) =
   if bg is 
     {
       none                then unique, 
       color(c)            then draw(dtb)(bg_rect,c), 
       image_centered(im)  then if size(im) is (w,h) then 
                                if bg_rect is rect(x,y,u,v) then 
                                draw(dtb)(im,x+((u-x-w)>>1),y+((v-y-h)>>1),bg_rect), 
                                //draw(dtb)(im,(u-w)>>1,(v-h)>>1,bg_rect), 
       image_tiled(im)     then if size(im) is (im_w,im_h) then 
                                if bg_rect is rect(x,y,u,v) then 
                                draw_tiled_image(dtb,im,im_w,im_h,x,x,y,u,v)
     }. 
   
   
   
   Drawing the  childs is  performed by induction  on the  list of positioned  cells. Each
   child is redrawn within the clipping rectangle of the draw tool box.
   
define One
   draw_childs
     (
       WidgetDrawToolBox              dtb, 
       List(PositionedCell)           cells
     ) =
   if cells is 
     {
       [ ] then unique, 
       [c1 . cs] then if c1 is cell(_,_,_,_,_,_,bg,content,cx,cy,bg_rect) then 
         draw_cell_background(dtb,bg,bg_rect); 
         draw(dtb)(content,cx,cy);
         draw_childs(dtb,cs)
     }.
   
   
   
   
   
   *** [6] Transmitting events. 
   
   The table  widget does  not capture the  keyboard nor  the mouse. Nevertheless  it must
   transmit events to  the appropriate child, and eventually recompute  all its metrics if
   the result is 'resized'.

   We  transmit  each  event  to  all  childs because  the  general  mechanism  exists  in
   'widget.anubis'  which  will  transmit  events  to   a  child  only  if  the  child  is
   concerned. Nevertheless, we need to compute the answer of the table from the answers of
   the childs.
   
define WidgetAnswer
   merge_answers
     (
       WidgetAnswer  a1, 
       WidgetAnswer  a2
     ) =
   if a1 is 
     {
       not_handled(area1) then if a2 is 
         {
           not_handled(area2)            then not_handled(area1+area2), 
           handled(area2)                then handled(area1+area2), 
           resized                       then resized,
           ignored                       then a1
           want_to_capture_mouse(_,_,_)  then a2,
           want_to_capture_keyboard(_,_) then a2
         },
   
       handled(area1) then if a2 is 
         {
           not_handled(area2)            then handled(area1+area2), 
           handled(area2)                then handled(area1+area2), 
           resized                       then resized,
           ignored                       then a1,
           want_to_capture_mouse(_,_,_)  then a2,
           want_to_capture_keyboard(_,_) then a2
         }, 
   
       resized                           then resized,
       ignored                           then a2,
       want_to_capture_mouse(v,cf,area)  then a1,
       want_to_capture_keyboard(v,area)  then a1,
     }. 
   
   
   Below is the function which transmits normal events to childs. 
   
define WidgetAnswer
   transmit_event
     (
       WidgetEventToolBox       etb, 
       WidgetEvent              e,
       List(PositionedCell)     cells,
       One -> One               recompute_metrics
     ) =
   if cells is 
     {
       [ ] then not_handled(area(etb)([])), 
       [c1 . cs] then 
         if c1 is cell(_,_,_,_,_,_,_,content,cx,cy,_) then 
         with answer1 = transmit(etb)(content,cx,cy,e), 
               others = transmit_event(etb,e,cs,recompute_metrics), 
               answer = merge_answers(answer1,others),
         if answer is 
           {
             not_handled(_)                 then answer,
             handled(_)                     then answer,
             resized                        then recompute_metrics(unique);
                                                 resized,
             ignored                        then ignored,
             want_to_capture_mouse(_,_,_)   then answer,
             want_to_capture_keyboard(_,_)  then answer
           }
     }. 
   


   
     
   
   *** [7] Creating the table widget. 
   
   Now we have everything  we need for creating the table widget.  We create the following
   variables within this widget for storing informations:
   
     name                type
     ------------------------------------------------------------------
     total_width_v       Var(Word32)
     total_height_v      Var(Word32)
     widths_v            Var(List(Word32))
     heights_v           Var(List(Word32))
     inner_rects_v       Var(List(WidgetRectangle))   (borders table only)
     positioned_cells_v  Var(List(PositionedCell))
      
   
   Creating the 'nude' table widget. 
   
define Widget
   create_nude_table
     (
       Word32                    vglue,
       Word32                    hglue, 
       List(List(WidgetCell))   rows_of_cells
     ) =
   with      logical_cells = logical_computation(rows_of_cells),
           total_width_v = var((Word32)0), 
          total_height_v = var((Word32)0), 
                widths_v = var((List(Word32))[]), 
               heights_v = var((List(Word32))[]), 
      positioned_cells_v = var((List(PositionedCell))[]), 
         recompute_metrics = (One u) |->
           recompute_metrics_nude(total_width_v,
                                  total_height_v, 
                                  widths_v,
                                  heights_v,
                                  positioned_cells_v, 
                                  logical_cells,
                                  hglue,
                                  vglue), 
   recompute_metrics(unique); 
   create_widget
     (   
       /* getting stretching capabilities */ 
       (One u) |-> stretch_cap(*total_width_v,*total_height_v,*total_width_v,*total_height_v), 
   
       /* stretching the table */ 
       (Word32 w, Word32 h) |-> unique, 
   
       /* getting table size */ 
       (One u) |-> (*total_width_v,*total_height_v),
   
       /* redrawing the table */ 
       (WidgetDrawToolBox dtb) |-> 
          recompute_metrics(unique); 
          draw_childs(dtb,*positioned_cells_v), 
       
       /* handling normal events */ 
       (WidgetEventToolBox etb, WidgetEvent e) |->
          transmit_event(etb,e,*positioned_cells_v,recompute_metrics),
   
       /* gathering registrations */ 
       (One u) |-> flat(map((LogicalCell c) |-> if c is cell(_,_,_,_,_,_,_,content) then 
                                 registrations(content)(unique),
                              logical_cells))
     ). 
   
   
   
   
   Creating the 'borders' table widget. 
   
define Widget
   create_borders_table
     (
       Word32                            vglue,
       Word32                            hglue, 
       Word32                            iedge, 
       Word32                            oedge,
       RGB                              main_color, 
       RGB                              light_color, 
       RGB                              dark_color,
       RGB                              back_color,
       List(List(WidgetCell))   rows_of_cells
     ) =
   with    logical_cells = logical_computation(rows_of_cells), 
           total_width_v = var((Word32)0), 
          total_height_v = var((Word32)0), 
                widths_v = var((List(Word32))[]), 
               heights_v = var((List(Word32))[]), 
           inner_rects_v = var((List(WidgetRectangle))[]), 
      positioned_cells_v = var((List(PositionedCell))[]), 
         recompute_metrics = (One u) |->
           recompute_metrics_borders(total_width_v,
                                     total_height_v, 
                                     widths_v,
                                     heights_v,
                                     inner_rects_v,
                                     positioned_cells_v, 
                                     logical_cells,
                                     hglue,
                                     vglue,
                                     iedge, 
                                     oedge), 
   recompute_metrics(unique); 
   create_widget
     (
       /* getting stretching capabilities */ 
       (One u) |-> stretch_cap(*total_width_v,*total_height_v,*total_width_v,*total_height_v), 
   
       /* stretching the table */ 
       (Word32 w, Word32 h) |-> unique, 
   
       /* getting table size */ 
       (One u) |-> (*total_width_v,*total_height_v),
   
       /* redrawing the table */ 
       (WidgetDrawToolBox dtb) |-> 
          recompute_metrics(unique); 
          draw_borders(dtb,
                       *inner_rects_v,
                       hglue,
                       vglue,
                       oedge,
                       iedge,
                       *widths_v,
                       *heights_v,
                       *total_width_v,
                       *total_height_v,
                       main_color,
                       light_color,
                       dark_color,
                       back_color);
          draw_childs(dtb,*positioned_cells_v), 
   
       /* handling normal events */ 
       (WidgetEventToolBox etb, WidgetEvent e) |->
             transmit_event(etb,e,*positioned_cells_v,recompute_metrics), 
   
       /* gathering registrations */ 
       (One u) |-> flat(map((LogicalCell c) |-> if c is cell(_,_,_,_,_,_,_,content) then 
                                 registrations(content)(unique),
                              logical_cells))
     ). 

      
   
   Creating the table widget. 
   
public define Widget   
   table
     (
       WidgetTableStyle         style, 
       Word32                    vglue,
       Word32                    hglue, 
       List(List(WidgetCell))   rows_of_cells
     ) =
   if style is 
     {
       nude then 
         create_nude_table(vglue,
                           hglue,
                           rows_of_cells), 
   
       borders(bg_color,edge_color,iedge,oedge) then 
         create_borders_table(vglue,
                              hglue,
                              iedge,
                              oedge,
                              edge_color,
                              lighten(edge_color),
                              darken(edge_color),
                              bg_color,
                              rows_of_cells)
     }.