Richlist

ulis, 2003-11-13. A pure Tcl package: http://wfr.tcl.tk/fichiers/ulis/richlist-fr.html

a(Rich)List is a powerful and easy to use Tcl package which manages enriched items lists. As widgets, these items have options taking values and, as widgets, can execute actions.

Following the need a Richlist can be viewed as a poor man class of objects, as a database table (each item being a row and each option name being a column) or as a structure description (each item being an occurence of the structure and each option being an element of the structure).

The items can be grouped inside sublists. Each sublist could be viewed as a view on the table. The first sublist is created with the Richlist and the other sublists can be built from a simple list of items, from an index on a sublist or from set operations on the sublists (union, intersection, difference). An index on a sublist is as a table index and allows to access the items from the values of an option (search by pattern). So it is easy to create sublists that split the items set of the Richlist following the values taken by an option. Powerful operations allow to retrieve, modify and format the values of all or part ot the items of a sublist.

The access to a sublist or an item is thru a reference. A sublist reference is a Tcl command allowing the access to the sublist actions. An item reference is a Tcl command allowing the access to the item options and actions.

Callbacks associated to the Richlist allow to define actions on events. The init and dispose actions are called on init and destruction of the items. The unknown action is called when items try to call unknown actions.


Demo result

   Asia
       China     Beijing
       India     New Delhi
       Japan     Tokyo
   Europe
       England   London
       France    Paris
       Germany   Berlin
       Russia    Moscow
   North America
       Canada    Ottawa
       Mexico    Mexico City
       USA       Washington
   South America
       Brazil    Brasilia

Demo source

   # create the rich list
   richlist create countries -country "" -continent "" -capital ""
   # fill the rich list
   set list \ 
   {
     {-country Brazil  -continent "South America"  -capital Brasilia}
     {-country Canada  -continent "North America"  -capital Ottawa}
     {-country China   -continent Asia             -capital Beijing}
     {-country England -continent Europe           -capital London}
     {-country France  -continent Europe           -capital Paris}
     {-country Germany -continent Europe           -capital Berlin}
     {-country India   -continent Asia             -capital "New Delhi"}
     {-country Japan   -continent Asia             -capital Tokyo}
     {-country Mexico  -continent "North America"  -capital "Mexico City"}
     {-country Russia  -continent Asia             -capital Moscow}
     {-country USA     -continent "North America"  -capital Washington}
   }
   foreach item $list { countries add [eval countries create $item] }
   # update an item
   [countries ref (-country|Russia)] config -continent Europe
   # display the countries by continent
     # create an index
   countries index create -continent
     # go thru all continents
   foreach continent [countries values -continent] \ 
   {
     # get continent subset
     set sublist($continent) [countries selection -continent $continent]
     # display continent name
     puts $continent
     # sort continent sublist by countries & capitals names
     $sublist($continent) sort {-country -capital} -dictionary
     # display continent countries & capitals names
     puts [$($continent) foreach item { $item cget -country -capital } \ 
           \n {\t\t%-10s%s}]
   }