table.anubis 41.8 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

   
   
                             The Anubis/Paradize Project.
   
                                   The table widget. 
   
                         Copyright (c) Alain Proutรฉ 2004-2005. 

   
   Authors:   Alain Proutรฉ
              Olivier Duvernois


   
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.
   
   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 WidgetCell:
   cell
     (
       WidgetVerticalPosition         vpos,
       WidgetHorizontalPosition       hpos,
       Int32                          col_span,
       Int32                          row_span,
       Widget                         content
     ).
   
   For your convenience, we define the special case:
   
public define WidgetCell
   cell
     (
       WidgetVerticalPosition         vpos,
       WidgetHorizontalPosition       hpos,
       Widget                         content
     ) = 
   cell(vpos,hpos,1,1,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,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 TableStyle:
   nude, 
   borders (WidgetParameters     parameters, 
            Int32                inner_edge,
            Int32                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 'background' color
   from 'parameters'.
   
   
   Here is the function for creating a table.

public define Widget
   create_table
     (
       TableStyle               style, 
       Int32                    vglue,
       Int32                    hglue, 
       List(List(WidgetCell))   rows_of_cells
     ). 
   
   
   
   
   --- That's all for the public part ! --------------------------------------------------


   ------------------------------ Table of Contents --------------------------------------
   
   *** (1) Logical computations. 
   *** (2) Metric computations. 
   *** (3) Storing relative positions of childs. 
   *** (4) Recomputing the metrics. 
   *** (5) Drawing borders and background. 
   *** (6) Transmitting events. 
   *** (7) Setting positions of childs. 
   *** (8) Duplicating the initial list of rows. 
   *** (9) Drawing the childs. 
   *** (10) Creating the table widget. 
   
   ---------------------------------------------------------------------------------------
   
         
   The 'childs widgets' are  those widgets which are in the cells  of the table. Into each
   child, we  store its position  relative to the table  (using 'store_relative_position',
   see  'widgets.anubis').  This  is required  for setting  the positions  of  the childs,
   without having to recompute all the geometry of the table.  Also, knowing the positions
   of the childs  will be enough for transmitting normal events  (the table widget does'nt
   capture anything). 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,
       Int32                          col_span,
       Int32                          row_span,
       Int32                          column_number, 
       Int32                          row_number, 
       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(Int32    starting_column,      // first column of cell
                   Int32    col_span,             // number of columns spanned by the cell
                   Int32    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,
       Int32                  current_column,
       Int32                  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,
       Int32                  current_column,
       Int32                  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,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,
                                        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,
                                                      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,
       Int32                    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. 
   
   From  now  on,  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.
   
   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(Int32),     // widths of columns
        List(Int32))     // heights of rows
   widths_and_heights
     (
       List(LogicalCell)     cells,
       Int32                 vglue, 
       Int32                 hglue, 
       Int32                 iedge
     ). 
   
   This is done  by a simple induction on  'cells'. Each cell contributes to  the width of
   one or several columns  and to the heights of one or  several rows.  By 'contribute' we
   mean that  the new  width of  a column is  the max  of the previous  width and  what is
   required by the cell. Similarly for columns. 
   
   Each cell has a  content which has a width and a height  (in pixels).  These widths and
   heights do include  neither 'iedge' nor 'hglue' nor 'vglue'.  Hence, widths and heights
   of columns are strictly those dictated be the size of childs.
   
   When a  cell spans over several  columns, the width  of the content must  be dispatched
   over these columns. First of all, if 'k'  is the number of columns spanned by the cell,
   we must substract:
   
                                    (k - 1)*(hglue + 2*iedge)
   
   from the  width 'w' of the  content, because this  corresponds to the number  of pixels
   which do not  participate to column widths.  The remainder width:
   
                                 rw = w - (k - 1)*(hglue + 2*iedge)
   
   is divided by 'k'. However, 'rw' may  not be a multiple of 'k'.  The euclidian division
   yields a 'rest' 'r', such that:
   
                                          rw = k*d + r
                                          0 =< r < k
   
   where 'd'  is basically the 'width  per column' for our  cell.  However, if  'r' is non
   zero, it must  be dispatched over (say) the  'r' last columns spanned by  our cell (one
   pixel per column). Hence,  the number 'k' of columns spanned by  the cell is decomposed
   as:
   
                                         k = (k - r) + r
   
   where 'k -  r' is the number of columns  receiving the width 'd' and  'r' the number of
   columns receiving the width 'd+1'. Notice that 'k  - r' is never zero, while 'r' may be
   zero.
   
   The next function takes  the width 'w' of the child, the  number 'k' of columns spanned
   by the cell,  and produces the pair (d,r).  Of course, the computation is  the same for
   heights and rows.
   
define (Int32,Int32)
   d_r_pair
     (
       Int32           w,    // width/height of content
       Int32           k,    // number of columns/rows
       Int32        glue,    // either 'vglue' or 'hglue'
       Int32       iedge
     ) =
   euclid(w - (k - 1)*(glue + 2*iedge),k).   // 'euclid' defined in 'tools/basis.anubis'
   
       
   Now, assuming that for a cell we know (either for columns or rows):
   
                                d:         size_per_unit   (unit = column or row)
          the starting column/row:         start
                            k - r:         span_1          
                                r:         span_2
   
   we are able to update a list of widths/heights:
   
define List(Int32)             // new list of widths/heights of columns/rows
   update_sizes
     (
       List(Int32)   sizes,              // previous list of widths/heights
       Int32         size_per_unit,   
       Int32         start, 
       Int32         span_1,      
       Int32         span_2       
     ) =
   if sizes is 
     {
       [ ] then 
         if start =< 0
         then if span_1 =< 0
              then if span_2 =< 0
                   then [ ] 
                   else [size_per_unit+1 . update_sizes([],size_per_unit,0,0,span_2-1)]
              else [size_per_unit . update_sizes([],size_per_unit,0,span_1-1,span_2)]
         else [0 . update_sizes([],size_per_unit,start-1,span_1,span_2)],
   
       [s1 . ss] then 
         if start =< 0
         then if span_1 =< 0
              then if span_2 =< 0
                   then sizes
                   else [max(s1,size_per_unit+1) 
                        . update_sizes(ss,size_per_unit,0,0,span_2-1)]
              else [max(s1,size_per_unit)
                   . update_sizes(ss,size_per_unit,0,span_1-1,span_2)]
         else [s1 . update_sizes(ss,size_per_unit,start-1,span_1,span_2)]
     }.
   
   
   
   Finally, we can compute our lists of widths and heights from our flat list of cells.
   
define (List(Int32),List(Int32))
   widths_and_heights
     (
       List(LogicalCell)     cells,
       Int32                 vglue, 
       Int32                 hglue, 
       Int32                 iedge
     ) =
   if cells is 
     {
       [ ] then ([],[]), 
       [c1 . cs] then 
         if c1 is cell(vpos,hpos,col_span,row_span,col_num,row_num,content) then 
         if widths_and_heights(cs,vglue,hglue,iedge) is (widths,heights) then 
         if get_size(content)(unique) is (contw,conth) then 
         if d_r_pair(contw,col_span,hglue,iedge) is (width_d,width_r) then 
         if d_r_pair(conth,row_span,vglue,iedge) is (height_d,height_r) then
         (update_sizes(widths,  width_d,  col_num, col_span-width_r,  width_r),
          update_sizes(heights, height_d, row_num, row_span-height_r, height_r))
     }.
   
   
   
   
   
   
   *** (3) Storing 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 need to  use 'store_relative_position'
   (defined in 'widget.anubis') in order to store into each child its position relative to
   the table. 
   
   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 Int32
   sum
     (
       Int32           i,
       Int32           j,
       List(Int32)     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)
     }.

   
   
   
   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,  together with the effect  of storing the relative  position of the
   child,  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 WidgetRectangle
   store_relative_position
     (
       LogicalCell          c, 
       Int32            hglue, 
       Int32            vglue, 
       Int32            iedge, 
       Int32            oedge, 
       List(Int32)     widths, 
       List(Int32)    heights
     ) =
   if c is cell(vpos,hpos,col_span,row_span,col_num,row_num,content) then 
   if get_size(content)(unique) 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),
   store_relative_position(content,
                           position(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)
                                      }
                                    ));
   //
   // return the rectangle around the inner edge:
   //
   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(WidgetRectangle)
   store_relative_positions
     (
       List(LogicalCell)    l, 
       Int32            hglue, 
       Int32            vglue, 
       Int32            iedge, 
       Int32            oedge, 
       List(Int32)     widths, 
       List(Int32)    heights
     ) =
   if l is 
     {
       [ ] then [ ], 
       [c1 . cs] then 
         with r = store_relative_position(c1,hglue,vglue,iedge,oedge,widths,heights),
         others = store_relative_positions(cs,hglue,vglue,iedge,oedge,widths,heights),
         [r . others]
     }.
   
   
   
   
   
   *** (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), 
     
   and the relative positions of childs must also be stored again into the childs. 
   
   We begin by 'borders' table. 
   
define One
   recompute_metrics_borders
     (
       Var(Int32)                    total_width_loc, 
       Var(Int32)                   total_height_loc, 
       Var(List(Int32))                   widths_loc, 
       Var(List(Int32))                  heights_loc, 
       Var(List(WidgetRectangle))    inner_rects_loc,
       List(LogicalCell)               logical_cells,
       Int32                                   hglue, 
       Int32                                   vglue, 
       Int32                                   iedge,
       Int32                                   oedge
     ) = 
   if widths_and_heights(logical_cells,vglue,hglue,iedge) is (widths,heights) then 
   widths_loc <- widths; 
   total_width_loc <- 2*oedge + sum(widths) + (2*iedge+hglue)*length(widths) + hglue; 
   heights_loc <- heights; 
   total_height_loc <- 2*oedge + sum(heights) + (2*iedge+vglue)*length(heights) + vglue; 
   inner_rects_loc <- 
     store_relative_positions(logical_cells,hglue,vglue,iedge,oedge,widths,heights). 
   
   
   For 'nude' tables, we have the following simplified version:
   
define One
   recompute_metrics_nude
     (
       Var(Int32)                    total_width_loc, 
       Var(Int32)                   total_height_loc, 
       Var(List(Int32))                   widths_loc, 
       Var(List(Int32))                  heights_loc, 
       List(LogicalCell)               logical_cells,
       Int32                                   hglue, 
       Int32                                   vglue
     ) = 
   if widths_and_heights(logical_cells,vglue,hglue,0) is (widths,heights) then 
   widths_loc <- widths; 
   total_width_loc <- sum(widths) + hglue*(length(widths)+1); 
   heights_loc <- heights; 
   total_height_loc <- sum(heights) + vglue*(length(heights)+1); 
   forget(store_relative_positions(logical_cells,hglue,vglue,0,0,widths,heights)). 
   
   

   
   
   *** (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,
       Int32                        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, 
       Int32                     hglue, 
       Int32                     vglue, 
       Int32                     oedge, 
       Int32                     iedge, 
       List(Int32)              widths,  // of columns (not including any edge or glue)
       List(Int32)             heights,  // of rows (not including any edge or glue)
       Int32               total_width,  // of table
       Int32              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). 
   
   
   
   
   
   
   *** (6) Transmitting events. 
   
   The table  widget does  not capture the  keyboard nor  the mouse. Nevertheless  it must
   transmit  normal events  to the  appropriate child,  and eventually  recompute  all its
   metrics is the result is 'resized'.

   Before transmitting the event,  we first need to check if the  last impacted child will
   receive the event. If  not, a 'mouse_gone' message must be sent  to this child, and the
   'last_impacted_loc' variable set to 'failure'. 
   
define Maybe(WidgetAnswer)
   update_impact
     (
       WidgetEventToolBox                      etb, 
       Var(Maybe(Widget))        last_impacted_loc,
       WidgetNormalEvent                         e
     ) =
   if *last_impacted_loc is 
     {
       failure then failure, 
       success(child) then
         if get_position(child)(unique) is position(cx,cy) then 
         if get_size(child)(unique) is (w,h) then 
         with xe = x(e), ye = y(e), 
         if (cx =< xe & xe < cx+w & cy =< ye & ye < cy+h)
         then failure
         else (last_impacted_loc <- failure; 
               success(main_event_handler(child)(etb,mouse_gone)))
     }.
   
   
   
   Below is the function which transmits normal events to childs. 
   
define WidgetAnswer
   transmit_event
     (
       WidgetEventToolBox                     etb, 
       WidgetNormalEvent                        e,
       List(LogicalCell)                    cells,
       Var(Maybe(Widget))       last_impacted_loc,
       One -> One               recompute_metrics
     ) =
   if cells is 
     {
       [ ] then not_handled([]), 
       [c1 . cs] then 
         if c1 is cell(_,_,_,_,_,_,content) then 
         if get_position(content)(unique) is position(cx,cy) then 
         if get_size(content)(unique) is (w,h) then 
         with xe = x(e), ye = y(e), 
         if (cx =< xe & xe < cx+w & cy =< ye & ye < cy+h)
         then (last_impacted_loc <- success(content); 
               if main_event_handler(content)(etb,e) is
                 {
                   not_handled(l)    then not_handled(l),
                   handled(l)        then handled(l),
                   resized           then recompute_metrics(unique); resized
                 })
         else transmit_event(etb,e,cs,last_impacted_loc,recompute_metrics)
     }.
   

   When   a  mouse   event   arrives,  we   call   'update_impact'  and   'transmit_event'
   successively. The two answers must be merged into a single answer.
   
read tools.anubis   
   
define WidgetAnswer
   merge_answers
     (
       Maybe(WidgetAnswer) mb_mouse_gone_answer,
       WidgetAnswer        normal_answer
     ) =
   if mb_mouse_gone_answer is 
     {
       failure             then normal_answer, 
       success(mg_answer)  then join(normal_answer,mg_answer)
     }. 
   
      
   
   
   

   *** (7) Setting positions of childs. 
   
   Assuming that the relative  positions of childs have been stored in  childs, we can set
   the positions of childs as follows.
   
define One
   set_table_childs_positions
     (
       WidgetPositionToolBox    ptb,
       List(LogicalCell)      cells                
     ) =
   if cells is 
     {
       [ ] then unique, 
       [c1 . cs] then  
         if c1 is cell(_,_,_,_,_,_,content) then
         set_position(content)(ptb,get_position(content)(unique));
         set_table_childs_positions(ptb,cs)
     }.
   
   

   
   
   *** (8) Duplicating the initial list of rows. 
   
   
define List(WidgetCell)
   duplicate
     (
       List(WidgetCell) l
     ) =
   if l is 
     {
       [ ] then [ ],
       [c1 . cs] then if c1 is cell(vpos,hpos,col_span,row_span,content) then 
         [cell(vpos,hpos,col_span,row_span,duplicate(content)(unique))  . duplicate(cs)]
     }. 
   
define List(List(WidgetCell))
   duplicate
     (
       List(List(WidgetCell)) ll
     ) =
   if ll is 
     {
       [ ] then [ ], 
       [l1 . ls] then 
         [duplicate(l1) . duplicate(ls)]
     }.
   
   
   
   *** (9) Drawing the childs. 
   
   This is  performed by induction  on the  list of logical  cells. Each child  is redrawn
   within the clipping rectangle of the draw tool box.
   
define One
   draw_childs
     (
       WidgetDrawToolBox              dtb, 
       List(LogicalCell)            cells
     ) =
   if cells is 
     {
       [ ] then unique, 
       [c1 . cs] then if c1 is cell(_,_,_,_,_,_,content) then 
         redraw(content)(dtb,clipping_rectangle(dtb));
         draw_childs(dtb,cs)
     }.
   
   
   
   
   
   
   *** (10) 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_loc     Var(Int32)
     total_height_loc    Var(Int32)
     widths_loc          Var(List(Int32))
     heights_loc         Var(List(Int32))
     inner_rects_loc     Var(List(WidgetRectangle))   (borders table only)
     last_impacted_loc   Var(Maybe(Widget))
   
   The variable  'last_impacted_loc' contains (if  there is one)  the child which  was the
   last one to contain the mouse cursor.
   
   
   Creating the 'nude' table widget. 
   
define Widget
   create_nude_table
     (
       Int32                    vglue,
       Int32                    hglue, 
       List(List(WidgetCell))   rows_of_cells
     ) =
   with      logical_cells = logical_computation(rows_of_cells),
           total_width_loc = var((Int32)0), 
          total_height_loc = var((Int32)0), 
                widths_loc = var((List(Int32))[]), 
               heights_loc = var((List(Int32))[]), 
         last_impacted_loc = var((Maybe(Widget))failure), 
         recompute_metrics = (One u) |->
           recompute_metrics_nude(total_width_loc,
                                  total_height_loc, 
                                  widths_loc,
                                  heights_loc,
                                  logical_cells,
                                  hglue,
                                  vglue), 
   recompute_metrics(unique); 
   create_widget
     (
       /* setting childs positions */ 
       (WidgetPositionToolBox ptb) |-> 
          set_table_childs_positions(ptb,logical_cells), 
   
       /* getting table size */ 
       (One u) |-> (*total_width_loc,*total_height_loc),
   
       /* redrawing the table */ 
       (WidgetDrawToolBox dtb) |-> 
          draw_childs(dtb,logical_cells), 
       
       /* duplicate */ 
       (One u) |-> create_nude_table(vglue,hglue,duplicate(rows_of_cells)), 

       /* Change size */ 
       (Int32 w, Int32 h) |-> unique, 
   
       /* handling normal events */ 
       (WidgetEventToolBox etb, WidgetNormalEvent e) |->
          with a1 = update_impact(etb,last_impacted_loc,e),
               a2 = transmit_event(etb,e,logical_cells,last_impacted_loc,recompute_metrics),
            merge_answers(a1,a2), 
   
       /* monitoring */ 
       [ ]
     ). 
   
   
   
   
   Creating the 'borders' table widget. 
   
define Widget
   create_borders_table
     (
       Int32                            vglue,
       Int32                            hglue, 
       Int32                            iedge, 
       Int32                            oedge,
       Var(RGB)                  main_color_v, 
       Var(RGB)                 light_color_v, 
       Var(RGB)                  dark_color_v,
       Var(RGB)                  back_color_v,
       List(List(WidgetCell))   rows_of_cells
     ) =
   with      logical_cells = logical_computation(rows_of_cells), 
           total_width_loc = var((Int32)0), 
          total_height_loc = var((Int32)0), 
                widths_loc = var((List(Int32))[]), 
               heights_loc = var((List(Int32))[]), 
           inner_rects_loc = var((List(WidgetRectangle))[]), 
         last_impacted_loc = var((Maybe(Widget))failure), 
         recompute_metrics = (One u) |->
           recompute_metrics_borders(total_width_loc,
                                     total_height_loc, 
                                     widths_loc,
                                     heights_loc,
                                     inner_rects_loc,
                                     logical_cells,
                                     hglue,
                                     vglue,
                                     iedge, 
                                     oedge), 
            redraw_monitor = (List(WidgetRectangle) -> One redraw) |-> 
                                redraw([rect(0,0,*total_width_loc,*total_height_loc)]), 
   recompute_metrics(unique); 
   create_widget
     (
       /* setting childs positions */ 
       (WidgetPositionToolBox ptb) |-> 
          set_table_childs_positions(ptb,logical_cells), 
   
       /* getting table size */ 
       (One u) |-> (*total_width_loc,*total_height_loc),
   
       /* redrawing the table */ 
       (WidgetDrawToolBox dtb) |-> 
          draw_borders(dtb,
                       *inner_rects_loc,
                       hglue,
                       vglue,
                       oedge,
                       iedge,
                       *widths_loc,
                       *heights_loc,
                       *total_width_loc,
                       *total_height_loc,
                       *main_color_v,
                       *light_color_v,
                       *dark_color_v,
                       *back_color_v);
          draw_childs(dtb,logical_cells), 
   
       /* duplicate */ 
       (One u) |-> create_borders_table(vglue,hglue,iedge,oedge,
                                        main_color_v,
                                        light_color_v,
                                        dark_color_v,
                                        back_color_v,
                                        duplicate(rows_of_cells)), 
       
       /* Change size */ 
       (Int32 w, Int32 h) |-> unique, 
   
       /* handling normal events */ 
       (WidgetEventToolBox etb, WidgetNormalEvent e) |->
          with a1 = update_impact(etb,last_impacted_loc,e),
               a2 = transmit_event(etb,e,logical_cells,last_impacted_loc,recompute_metrics), 
            merge_answers(a1,a2), 
   
       /* monitoring */ 
       [ ]
     ). 

      
   
   Creating the table widget. 
   
public define Widget   
   create_table
     (
       TableStyle               style, 
       Int32                    vglue,
       Int32                    hglue, 
       List(List(WidgetCell))   rows_of_cells
     ) =
   if style is 
     {
       nude then 
         create_nude_table(vglue,
                           hglue,
                           rows_of_cells), 
   
       borders(parms,iedge,oedge) then 
         if parms is parameters(main_color_v,
                                light_color_v,
                                dark_color_v,
                                _,
                                back_color_v,
                                _,_,_,_,_,_) then
         create_borders_table(vglue,
                              hglue,
                              iedge,
                              oedge,
                              main_color_v,
                              light_color_v,
                              dark_color_v,
                              back_color_v,
                              rows_of_cells)
     }.