generic_editor_example.anubis 4.18 KB

   
   
                             The Anubis/Paradize Project.
   
                     A example of use of the generic editor widget. 
   
                          Copyright (c) Alain Prouté 2004-2005. 

   
   Authors:   Alain Prouté
              Olivier Duvernois


   
   
read widgets3/host_window.anubis
read widgets3/generic_editor.anubis   
read widgets3/table.anubis   
   

   In this  file we construct a  simple albeit significant  example if use of  the generic
   editor (which is  defined in 'generic_editor.anubis').  Study this  example if you want
   to  use the  generic  editor, because  it  contains important  hints  not described  in
   'generic_editor.anubis'.
   
   First of all we need to decide which kind of data we want to edit. In order to show how
   to handle such  situation, we choose a  type 'Datum1' of data which  is cross recursive
   with another one.
   
type Datum2:...   
   
type Datum1:   
   a,
   b(Datum1,Datum2).
   
type Datum2:
   c,
   d(Datum1,Datum1,Datum1). 
   
   
   Actually, these  types will not  be used here  (but they should  probably be used  in a
   realistic  situation) because,  as  explained in  'generic_editor.anubis',  we need  to
   represent 'incomplete data' or 'extended data'. Hence, we also define:
   
type ExDatum2:...   
   
type ExDatum1:
   missing(List(ExDatum1)),
   a,
   b(ExDatum1,ExDatum2).
   
type ExDatum2:
   missing(List(ExDatum2)),
   c,
   d(ExDatum1,ExDatum1,ExDatum1). 
   
   Notice that  the new types just  mimic the previous ones,  and that they  have an extra
   alternative  for  representing  missing  data.   The unique  component  of  this  extra
   alternative is a list of proposals for the missing datum.
   
   
   Now, we must define the  functions 'to_editable', transforming data of types 'ExDatum1'
   and  'ExDatum2'  into editables.  Of  course,  these two  functions  need  to be  cross
   recursive.
   
define Editable(ExDatum2)
   to_editable
     (
       ExDatum2 d
     ). 
   
define Editable(ExDatum1)
   to_editable
     (
       ExDatum1 d
     ) =
   if d is 
     {
       missing(l)    then 
         with area_rect = rect(0,-10,10,0), 
         create_editable
           (
             (Int32 x, Int32 y, Int32 margin) |-> unique, 
             (One u) |-> [area_rect],
             (Int32 mx, Int32 my) |-> 
                if belongs(mx,my,area_rect)
                then [area_rect]
                else [],
             (WidgetDrawToolBox dtb, Int32 margin) |->
                
           ),
   
       a             then 
       b(d1,d2)      then 
     }. 
   
define Editable(ExDatum2)
   to_editable
     (
       ExDatum2 d
     ) =
   if d is 
     {
       missing(l)    then 
       c             then 
       b(d1,d2,d3)   then 
     }.
   
   
   
define Editable(String)
   to_editable
     (
       String s
     ) =
   with get_area = (One u) |-> (List(WidgetRectangle))[rect(10,0,30,30),rect(0,40,100,80)], 
             red = rgb(255,0,0), 
   create_editable(
                    (Int32 x,Int32 y,Int32 margin) |-> unique,
   
                    get_area, 
   
                    (Int32 mx, Int32 my) |-> 
                       with area = get_area(unique), 
                       if belongs(mx,my,area)
                       then area
                       else [],
   
                    (WidgetDrawToolBox dtb, Int32 margin) |-> 
                       draw(dtb)(rect(15,5,25,25),red);
                       draw(dtb)(rect(5,45,95,75),red), 
   
                    (EditableEvent(String) e) |-> not_handled([]), 
   
                    (One u) |-> "gaga"
   
                  ). 
   
   
define Widget
   the_editor
     (
       WidgetParameters parms
     ) =
   create_table(borders(parms,2,2),2,2,
     [[ cell(center,center, 
             create_generic_editor(parms, 
                                   "", 
                                   to_editable,
                                   var(undo)))
     ]]). 
     
   
   
global define One 
   try_generic_editor
     (
       List(String) args
     ) =
   with parms = create_default_parameters_set, 
   forget(open_host_window(100,100,
                           "Generic Editor Example", 
                           the_editor(parms))).