generic_editor.anubis 5.53 KB

   
   
read tools/basis.anubis   
read widgets3/widget.anubis
read widgets3/tools.anubis
read widgets3/host_window.anubis
read widgets3/menu_manager.anubis   
read widgets3/menus.anubis   
   
   
   
   
define RGB black   = rgb(0,0,0). 
define RGB red     = rgb(255,0,0). 
define RGB yellow  = rgb(255,255,210).
define RGB white   = rgb(255,255,255).


   
type ENode:
   enode (WidgetRectangle             area_1,
          List(WidgetRectangle)       area_others, 
          Bool                        is_place_holder,
          Var(Bool)                   h_v,
          WidgetDrawToolBox -> One    redraw, 
          List(Var(ENode))            childs). 
   

   
   *** Copying a enode. 
   
   For 'cut/copy/paste' operations we need to make a copy of an enode. 
   
define ENode   
   copy
     (
       ENode e
     ) =
   if e is enode(a1,ao,iph,h_v,rd,cs) then 
   enode(a1,ao,iph,var(*h_v),rd,
         map((Var(ENode) e_v) |-> var(copy(*e_v)) ,cs)). 
   
   
   
   *** Making a place holder. 
   
define ENode
   make_place_holder
     (
       ENode       model
     ) =
   if model is enode(area_1,_,_,_,_,_) then 
   if area_1 is rect(x,y,u,v) then 
   with r = (WidgetRectangle)rect(x,y,x+10,y+10),
   enode(r,
         [],
         true,
         var(false),
         (WidgetDrawToolBox dtb) |-> draw(dtb)(r,red),
         []). 
   
   

   
   
   *** Drawing the highlighting. 
   
define One 
   draw_highlighting
     (
       WidgetDrawToolBox     dtb, 
       List(WidgetRectangle) l 
     ) =
   if l is 
     {
       [ ] then unique, 
       [h . t] then draw(dtb)(h,yellow); 
                    draw_highlighting(dtb,t)
     }.
   
   
   
   
   
   *** Drawing the editor and its content.
   
define WidgetDrawToolBox -> One
   redraw_editor
     (
       Var(Int32)      ed_width_v, 
       Var(Int32)      ed_height_v, 
       Var(ENode)      content_v
     ) =
   (WidgetDrawToolBox dtb) |-> 
     draw(dtb)(rect(0,0,*ed_width_v,*ed_height_v),white); 
     if *content_v is enode(_,_,_,_,rd,_) then rd(dtb). 
   
   
   
   
   
   
   *** Drawing a node. 
   
define One
   draw_node
     (
       ENode e
     ) =
   if e is enode(WidgetRectangle              area_1,
                 List(WidgetRectangle)        area_others,
                 Bool                         is_place_holder,
                 Var(Bool)                    h_v,
                 ((WidgetDrawToolBox) -> One) redraw,
                 List(Var(ENode))             childs) then alert. 

   
   
   
   *** Getting the location of node which is highlighted (if any). 
   
define Maybe(Var(ENode))   
   get_highlighted
     (
       Var(ENode)     e
     ).
   
define Maybe(Var(ENode))
   get_highlighted
     (
       List(Var(ENode)) l
     ) =
   if l is 
     {
       [ ] then failure, 
       [h . t] then if get_highlighted(h) is 
         {
           failure then get_highlighted(t)
           success(v) then success(v)
         }
     }.
   
define Maybe(Var(ENode))   
   get_highlighted
     (
       Var(ENode)     e
     ) =
   if *e is enode(a1,ao,iph,h_v,rd,cs) then 
   if *h_v 
   then success(e)
   else get_highlighted(cs). 
   
   
   
   *** Getting the location of the smallest node containing the mouse pointer (if any). 
   
define Maybe(Var(ENode))
   get_smallest_node
     (
       Var(ENode)      e, 
       Int32           mx, 
       Int32           my
     ).
   
define Maybe(Var(ENode))
   get_smallest_node
     (
       List(Var(ENode))      l, 
       Int32                 mx, 
       Int32                 my
     ) =
   if l is 
     {
       [ ] then failure, 
       [h . t] then if get_smallest_node(h,mx,my) is 
         {
           failure then get_smallest_node(t,mx,my),
           success(v) then success(v)
         }
     }.
   
define Maybe(Var(ENode))
   get_smallest_node
     (
       Var(ENode)      e, 
       Int32           mx, 
       Int32           my
     ) =
   if *e is enode(a1,ao,iph,h_v,rd,cs) then 
   if mapor((WidgetRectangle r) |-> belongs(mx,my,r),[a1 . ao]) 
   then if get_smallest_node(cs,mx,my) is 
          {
            failure    then success(e)
            success(c) then success(c)
          }
   else failure. 
   
   
   
   
   
   *** Performing a 'Cut', a 'Copy' or a 'Paste'. 
   
define WidgetEventToolBox -> List(WidgetRectangle)
   do_cut
     (
       Var(MenuCommand)        menu_cv,
       Rectangle               editor_area, 
       Var(ENode)              where, 
       Var(ENode)              paste_v
     ) =
   (WidgetEventToolBox mmetb) |-> 
     paste_v <- *where; 
     where <- make_place_holder(*where); 
     menu_cv <- clear; 
     [receive_rectangle(mmetb)(editor_area)]. 
   
define WidgetEventToolBox -> List(WidgetRectangle)
   do_copy
     (
       Var(MenuCommand)     menu_cv,
       Rectangle            editor_area,
       Var(ENode)           where,
       Var(ENode)           paste_v
     ) =
   (WidgetEventToolBox mmetb) |-> 
     paste_v <- copy(*where);
     menu_cv <- clear; 
     [receive_rectangle(mmetb)(editor_area)]. 
 
define WidgetEventToolBox -> List(WidgetRectangle)
   do_paste
     (
       Var(MenuCommand)     menu_cv,
       Rectangle            editor_area, 
       Var(ENode)           where,
       Var(ENode)           paste_v
     ) =
   (WidgetEventToolBox mmetb) |-> 
     where <- copy(*paste_v);
     menu_cv <- clear; 
     [receive_rectangle(mmetb)(editor_area)]. 

   
   
   
   
   *** The event handler of the editor. 
   
 define WidgetAnswer
   handle_mouse_move
     (
       Var(ENode)               content_v
     ) =
   with mb_already_highlighted = get_highlighted(content_v),