simple_example.anubis 5.87 KB
   
   
                             The Anubis/Paradize Project.
   
                        A simple example of how to use widgets. 
   
                         Copyright (c) Alain Prouté 2004-2005. 

   
   Authors:   Alain Prouté

   

   This file contains the simple example announced in 'widgets3/host_window.anubis'. 
   
   This very simple example illustrates the following features of the widget system:
   
      - using state variables, 
      - making a tree of widgets, 
      - monitoring variables in order to be always up to date. 
   
   We propose to open  a host window containing a table with two  cells. In the first cell
   we have a 'check box', reflecting the state of a variable of type 'Var(Bool)' (i.e. the
   check  box is  checked when  this variable  contains 'true',  and not  checked  when it
   contains 'false').   The second cell contains a  button.  This button acts  on the same
   variable.  Each time  the  button is  clicked, the  value  of the  variable is  negated
   (flip-flopped).  Also  the  decoration of  the  button  depends  on  the value  of  the
   variable. In order to have this decoration  always up to date, the button is registered
   at the variable.
   
   To test this program, compile this file:
   
 anubis simple_example.anubis
   
   and try the example:
   
 simple_example
   
   Remark that the variable is monitored by the check box and by the button, whose aspects
   are updated automatically.
   
   
read tools/basis.anubis   
read host_window.anubis   
read widget.anubis
read tools.anubis
read table.anubis
read check_box.anubis
read button.anubis
read text.anubis   
   
   
global define One
   simple_example
     (
       List(String) args
     ) =
   //
   // (1) Create a set of parameters. This set of parameters contains colors and some others
   //     'visual' parameters. (See 'widgets3/tools.anubis')
   //
   with my_parameters = 
      if create_default_parameters_set is 
        {
          failure    then alert, 
          success(s) then s
        }, 
   //
   // (2) Create a state variable. (For dynamic variables, see 'predefined.anubis')
   //
   with my_variable = var((Bool)false),     // initial value: 'false'
   //
   // (3) Create a tree of widgets. In this case, a table containing a check box and a button
   //     both acting on 'my_variable'. (For details on each sort of widget, see the corresponding
   //     file in this directory, i.e: 'widgets3/table.anubis' etc...)
   //
   with my_widgets = create_table(borders       // make a table with visible borders
                                   (
                                     my_parameters,   // use the colors in 'my_parameters'
                                     2,               // make inner edges 2 pixels wide
                                     4                // make an outer edge 4 pixels wide
                                   ),
                                  2,     // put 2 pixels of vertical glue
                                  2,     // put 2 pixels of horizontal glue
                       [

                        [
                           //
                           // put an explanation 
                           //
                           cell(
                             center, 
                             left,
                             2,      // span over 2 columns
                             1,      // and 1 line
                             create_text("This is a simple example.\n"+
                                         "Click either the button or the check box.\n"+
                                         "Remark that they are linked to the same variable. ",
                                         *font_v(my_parameters),
                                         rgb(0,0,0),
                                         4              // margin on 4 sides
                                        )
                               ),
                        ],
                        [
                           //
                           // put a check box refering to 'my_variable' in the first cell
                           //
                           cell(
                             bottom,
                             left,
                             create_check_box(my_variable)),    
                           //
                           // put a button flip-flopping 'my_variable' in the second cell
                           //
                           cell(
                             center,
                             center,
                             create_button(100,                         // width of button
                                           25,                          // height of button
                                           push_down(my_parameters),    // style of button
                                           (WidgetEventToolBox etb) |-> // action performed
                                             my_variable                //   by button
                                               <- not(*my_variable),
                                           (One u) |->                  // how to get the 
                                             center_text(my_parameters, //   decoration of the button
                                                         if *my_variable
                                                         then "True"
                                                         else "False"),    
                                           [register(my_variable)]))    // so that button is always
                                                                        //   up to date
                       ]
                     ]),
   //
   // (4) Open a host window containing this tree of widgets
   //
   forget(open_host_window("Simple example",    // title of host window
                           my_widgets)).        // content of host window