check_box.anubis 4.97 KB

                                  The Anubis Project. 
   
                             A Widget System (4th version). 
                                The 'check box' widget.
   
                            Copyright (c) Alain Proute' 2005. 
   
   
   Authors: Alain Proute'

   
   In this file, the  'check box' widget is defined. 
   
   In  order  to  create a  check  box,  you  must  provide  a dynamic  variable  of  type
   'Var(Bool)'. The check box always reflects  the state of this variable. If the variable
   contains 'true', the  box is checked, otherwise  it is not checked. When  the user left
   clicks the check box, the state of the variable is negated. 
      
   By default, this widget  looks like a small square of 10x10  pixels, with another small
   solid square inside it when it is checked.
   
read widget.anubis
     
public define Widget
   check_box
     (
       Var(Bool)    v
     ).    
   
   
   However,  you can  optionally provide  two images,  one for  the non  checked  box, and
   another one for the checked box.  These two images should be of the same size.

public define Widget
   check_box
     (
       Var(Bool)    v, 
       HostImage    not_checked, 
       HostImage    checked
     ). 
   
   
   
   --- That's all for the public part ! --------------------------------------------------
  
   
read tools/basis.anubis   
read tools.anubis   

read trace.anubis   
   
   
   The size method when there is no image:
   
define (Int32,Int32)
   size_no_image
     (
       One u
     ) = 
   (10,10). 
   
   
   The size method when there are images:
   
define One -> (Int32,Int32)
   size_images
     (
       HostImage     not_checked,
       HostImage     checked
     ) =
   (One u) |-> 
   if size(not_checked) is (w1,h1) then 
   if size(checked)     is (w2,h2) then 
   (max(w1,w2),max(h1,h2)). 
   
   
   
   The redraw method when there is no image. 
   
define WidgetDrawToolBox -> One
   redraw_no_image
     (
       Var(Bool) v
     ) =
   with black = rgb(0,0,0), 
          red = rgb(180,0,0), 
        white = rgb(255,255,255), 
   (WidgetDrawToolBox dtb) |-> 
      draw(dtb)(rect(0,0,10,10),white); 
      draw_relief_edge(dtb,1,rect(0,0,10,10),black,black);
      if *v
      then draw(dtb)(rect(2,2,8,8),red)
      else unique. 
   
   
   The redraw method when there are images. 
   
define WidgetDrawToolBox -> One
   redraw_images
     (
       Var(Bool)    v,
       HostImage    not_checked,
       HostImage    checked
     ) =
   (WidgetDrawToolBox dtb) |-> 
   draw(dtb)(if *v then checked else not_checked,0,0).
  
   
   
define Widget
   check_box
     (
       Var(Bool)                      v,
       Maybe((HostImage,HostImage))   mb_images
     ) =
   create_widget
     (
       /* getting the size of the check box */
       if mb_images is 
         {
           failure then size_no_image, 
           success(p) then if p is (not_checked,checked) then 
             size_images(not_checked,checked)
         }, 
   
       /* redrawing the check box */ 
       if mb_images is
         {
           failure then redraw_no_image(v), 
           success(p) then if p is (not_checked,checked) then 
             redraw_images(v,not_checked,checked)
         },
   
       /* handling events */ 
       with the_rect = (WidgetRectangle)if mb_images is 
         {
           failure then rect(0,0,10,10), 
           success(p) then if p is (not_checked,checked) then 
             if size_images(not_checked,checked)(unique) is (w,h) then 
             rect(0,0,w,h)
         }, 
       (WidgetEventToolBox etb, WidgetEvent e) |-> 
          if e is 
            {
              mouse_move(ks,x,y)             then ignored, 
              mouse_click(ks,mc,x,y)         then if mc is
                {
                  left_down   then v <- not(*v); 
                                   handled(area(etb)([the_rect])),
                  left_up     then ignored, 
                  middle_down then ignored, 
                  middle_up   then ignored, 
                  right_down  then ignored, 
                  right_up    then ignored
                },
              mouse_wheel(ks,d,x,y)          then ignored, 
              mouse_gone                     then ignored, 
              captured_mouse_move(_)         then ignored, 
              captured_mouse_liberated(_)    then ignored, 
              key_down(_)                    then ignored,
              keyboard_recaptured(_)         then ignored,
              changed(l)                     then 
                if member(l,var_id(v))
                then handled(area(etb)([the_rect]))
                else ignored
            },
   
        /* registering the variable */ 
        (One u) |-> [register(v)]
     ). 
   
   
   
public define Widget
   check_box
     (
       Var(Bool)   v
     ) =
   check_box(v,failure). 
   
public define Widget
   check_box
     (
       Var(Bool)    v, 
       HostImage    not_checked, 
       HostImage    checked
     ) =
   check_box(v,success((not_checked,checked))).