resizer.anubis 4.55 KB

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

   
   The resizer is  just a small rectangle with  may capture the mouse.  When  the mouse is
   captured,   moving   the  mouse   updates   the   given   two  variables   accordingly,
   i.e. displacements of the mouse are added to the variables.

read tools/basis.anubis   
   
read widget.anubis
   
public define Widget   
   resizer
     (
       Word32           width,     // width of resizer
       Word32           height,    // height of resizer
       Var(Word32)      x_v,  
       Var(Word32)      y_v,
       One -> Word32    x_min,     // gives the minimal legal value for 'x_v'
       One -> Word32    y_min,     // gives the minimal legal value for 'y_v'
       One -> Word32    x_max,     // gives the maximal legal value for 'x_v'
       One -> Word32    y_max,     // gives the maximal legal value for 'y_v'
       RGB             color
     ). 
   
   For example,  if you left click  the resizer and move  the mouse down  right by (10,20)
   pixels, the value of 'x_v' is incremented  by 10, and the value of 'y_v' is incremented
   by 20.
   
   
   --- That's all for the public part ! --------------------------------------------------
   
   
read tools.anubis
   
   
define WidgetDrawToolBox -> One
   draw_method
     (
       Word32          width, 
       Word32          height,
       RGB            color
     ) =
   (WidgetDrawToolBox dtb) |-> 
   draw_relief_area(dtb,2,rect(0,0,width,height),color,lighten(color),darken(color)). 
   
   
   
define (WidgetEventToolBox,WidgetEvent) -> WidgetAnswer
   event_handler
     (
       Var(Word32)      x_v,  
       Var(Word32)      y_v,
       One -> Word32    x_min,
       One -> Word32    y_min,
       One -> Word32    x_max,
       One -> Word32    y_max,
       Var(Word32)      initial_mx_v, // position of mouse on resizer when the mouse is captured
       Var(Word32)      initial_my_v, 
       Var(One)        capture_ticket
     ) =
   (WidgetEventToolBox etb, WidgetEvent e) |->
     if e is 
       {
         quit                         then ignored, 
         mouse_move(ks,x,y)           then ignored,
         mouse_click(ks,mc,x,y)       then 
           if mc is left_down 
           then 
             (
               initial_mx_v <- x; 
               initial_my_v <- y; 
               want_to_capture_mouse(capture_ticket,compress,area(etb)([]))
             )
           else ignored, 

         mouse_wheel(ks,delta,mx,my)  then ignored, 
   
         mouse_gone                   then ignored,
   
         captured_mouse_move(f)       then 
           if f(capture_ticket,etb) is 
             {
               failure then ignored, 
               success(p) then if p is (mx,my) then 
                 x_v <- max(x_min(unique),min(x_max(unique),*x_v + (mx - *initial_mx_v))); 
                 y_v <- max(y_min(unique),min(y_max(unique),*y_v + (my - *initial_my_v))); 
                 resized
             },
   
         captured_mouse_liberated(f)  then ignored, 
         key_down(f)                  then ignored,
         keyboard_recaptured(f)       then ignored,
         changed(l)                   then ignored,
         could_drop(_,_,_,_)          then ignored,
         want_to_drop(_,_,_)          then ignored 
       }. 
   
   
   
public define Widget   
   resizer
     (
       Word32           width,     // width of resizer
       Word32           height,    // height of resizer
       Var(Word32)      x_v,  
       Var(Word32)      y_v,
       One -> Word32    x_min,     // gives the minimal legal value for 'x_v'
       One -> Word32    y_min,     // gives the minimal legal value for 'y_v'
       One -> Word32    x_max,     // gives the maximal legal value for 'x_v'
       One -> Word32    y_max,     // gives the maximal legal value for 'y_v'
       RGB             color
     ) =
   with   initial_mx_v = var((Word32)0), 
          initial_my_v = var((Word32)0), 
        capture_ticket = var((One)unique), 
     create_widget
       (
         (One u) |-> stretch_cap(width,height,width,height), 
         (Word32 w, Word32 h) |-> unique, 
         (One u) |-> (width,height), 
         draw_method(width,height,color), 
         event_handler(x_v,y_v,
                       x_min,y_min,x_max,y_max,
                       initial_mx_v,initial_my_v,
                       capture_ticket), 
         (One u) |-> []
       ).