kbd.anubis 6.18 KB

   
 *Project*                             The Anubis Project
   
 *Title*               Testing mouse and keyboard events in a host window.   
   
 *Copyright*                     Copyright (c) Alain Prouté 2005. 

    
 *Author* Alain Prouté
   
   
define Int32 length( List($T) l) =
	if l is { [] then 0, [h . t] then 1 + length(t)}.
	
   *** Formating functions. 
   
define String
   Int32 n + String s
     = 
   integer_to_string(n) + s. 
   
   
define String
   format
     (
       KeyboardState   ks
     ) =
   if ks is
     {
       kbdstate(s,c,a,l,m,r) then 
         (if s  then "s" else "-") +
         (if c  then "c" else "-") +
         (if a  then "a" else "-") +
         (if l  then "l" else "-") +
         (if m  then "m" else "-") +
         (if r  then "r" else "-")
     }. 
   
   
define String 
   format
     (
       KeyboardSpecialKey  k
     ) =
   if k is 
     {
       escape    then "escape",
       f1        then "f1",
       f2        then "f2",
       f3        then "f3", 
       f4        then "f4",
       f5        then "f5", 
       f6        then "f6",
       f7        then "f7",
       f8        then "f8",
       f9        then "f9",
       f10       then "f10",
       backspace then "backspace", 
       home      then "home", 
       end       then "end", 
       tab       then "tab",
       enter     then "enter",
       insert    then "insert",
       del       then "del",
       page_up   then "page_up",
       page_down then "page_down",
       up        then "up",
       down      then "down",
       left      then "left", 
       right     then "right"
    }.

   
define String 
   format
     (
       MouseClick   mc
     ) =
   if mc is 
     {
       left_down     then "left_down", 
       left_up       then "left_up", 
       middle_down   then "middle_down",
       middle_up     then "middle_up",
       right_down    then "right_down",
       right_up      then "right_up"
  }. 
   
   
define String
   format
     (
       Rectangle r
     ) =
   if r is rect(x,y,u,v) then "["+x+","+y+","+u+","+v+"]". 
   
   
define String
   format 
     (
       List(Rectangle) l
     ) =
   if l is 
     {
       [ ] then "", 
       [h . t] then format(h) + if t is [] then "" else ","+format(t)
     }.
   

define String 
   format
     (
       KeyboardKey  kk
     ) =
   if kk is 
     {
       unknown         then "unknown", 
       char(c)         then "char("+int8_to_int32(c)+")", 
       unicode(a,b)    then "unicode("+int8_to_int32(a)+","+int8_to_int32(b)+")", 
       special(s)      then "special("+format(s)+")"
     }. 
   
   
define String
   format
     (
       HostWindowEvent(One)   e
     ) =
   if e is 
     {
       quit                    then "quit", 
       expose                  then "expose", 
       pointer_entering        then "pointer_entering", 
       pointer_leaving         then "pointer_leaving",
       key_down(ks,kk)         then "key_down("+format(ks)+","+format(kk)+")", 
       mouse_move(ks,x,y)      then "mouse_move("+format(ks)+","+x+","+y+")", 
       mouse_click(ks,mc,x,y)  then "mouse_click("+format(ks)+","+format(mc)+","+x+","+y+")", 
       mouse_wheel(ks,d,x,y)   then "mouse_wheel("+format(ks)+","+d+","+x+","+y+")", 
       tick                    then "tick",
       repaint(l)              then "repaint("+format(l)+")", 
       specific(u)             then "specific(unique)"
     }. 
   
   
   
   
   
   *** Redrawing the host window.
   
define (HostWindow,List(Rectangle)) -> One
   redraw_window
     (
       HostImage       buffer, 
     ) =
   (HostWindow hw, List(Rectangle) l) |->   
   unique; 
   if size(buffer) is (w,h) then 
   paint_image(hw,rect(0,0,w,h),0,0,buffer). 
   

   
   
   *** Event handler. 

define One
   draw_h_scale
     (
       HostImage         buffer, 
       Int32             w,
       Int32             x
     ) =
   if x >= w then unique else
   draw_rectangle(buffer,rect(x,0,x+1,8),rgb(0,0,0)); 
   draw_h_scale(buffer,w,x+10). 
   
   
define One
   draw_v_scale
     (
       HostImage         buffer, 
       Int32             h,
       Int32             y
     ) =
   if y >= h then unique else
   draw_rectangle(buffer,rect(0,y,8,y+1),rgb(0,0,0)); 
   draw_v_scale(buffer,h,y+10). 
   
   
   
define One
   draw_scales
     (
       HostImage       buffer, 
       Int32           w,
       Int32           h
     ) =
   draw_rectangle(buffer,rect(0,0,w,h),rgb(0,255,0));
   draw_h_scale(buffer,w,0);
   draw_v_scale(buffer,h,0).
  

   
   
define One
   drawchar
     (
       KeyboardKey      kk, 
       HostImage        buffer,
       SystemFont       font
     ) =
   if size(buffer) is (w,h) then 
   if kk is char(c) 
     then forget(draw_system_character(buffer,rect(0,0,w,h),30,50,font,int8_to_int32(c),rgb(0,0,0)))
     else unique.  
   
define (HostWindow, HostWindowEvent(One)) -> List(Rectangle)
   event_handler
     (
       HostImage           buffer, 
       SystemFont          font, 
     ) =
   (HostWindow hw, HostWindowEvent(One) e) |->
     if size(buffer) is (w,h) then 
     if e is tick then [] else
       (
         if e is key_down(ks,kk) 
         then (draw_scales(buffer,w,h); drawchar(kk,buffer,font); [rect(0,0,w,h)]) 
         else []
       ). 
     
   
   
     ; 
     (if e is key_down(ks,kk) then drawchar(kk,buffer,font) else unique); 
     (if e is tick then unique else print(format(e)+"\n")); 
     [rect(0,0,w,h)]. 
   
   
   
global define One    
   kbd
     (
       List(String) args
     ) =
   if load_system_font("helvetica_bold_r_24") is 
     {
       failure then print("cannot load system font.\n"), 
       success(font) then 
         with total_width = (Int32)300, 
             total_height = (Int32)200, 
                   buffer = to_host_image(
                              create_rgba_image( 
                                 total_width,total_height,rgba(0,0,255,255)),1),
   if open_host_window
       (
         rect(500,500,500+total_width,500+total_height), 
         "kbd test", 
         managed,
         redraw_window(buffer), 
         event_handler(buffer,font), 
         (List(HostWindowEvent(One)) l) |-> l) is 
     {
       failure     then print("Cannot open host window.\n"),
       success(hw) then show(hw)
     }
   }.