host_window.anubis 15.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

   
   
   
                             The Anubis/Paradize Project.
   
                      Opening a host window containing widgets. 
   
                        Copyright (c) Alain Prouté 2004-2005. 

   
   Authors:   Alain Prouté
   
      
read widget.anubis   
   

   *** (1) Opening a host window containing a widget. 
      
   In order to open a host window with a widget inside, use the following:
   
public define Bool
   open_host_window
     (
       Int32 x, Int32 y,       // position of host window on screen
       String title,           // title of host window
       Widget content          // content of host window
     ).
      
   The value returned is 'true' if and only  if the host window has been created. The size
   of the host window is adapted to the size of the content. Here is a variant which opens
   the host window centered in the screen:
   
public define Bool
   open_host_window
     (
       String title, 
       Widget content
     ). 

   
   

   *** (2) A simple example. 
   
   In order to help to understand how to use widgets, we provide a example, which is of no
   use by itself, but illustrates as simply  as possible the main principles of the widget
   system:
   
      - constructing a tree of widgets,
      - using state variables,
      - monitoring variables in order to be always up to date. 

   The  example   (fully  commented   and  ready   to  be  compiled)   may  be   found  in
   'widgets3/simple_example.anubis'.
   
   
   
   
   
   --- That's all for the public part ! --------------------------------------------------
   
      
   
 read widgets3/trace.anubis   
   
   
   
   
   *** The paint method
   
   Given the  rectangle 'clip' within which painting  must occur, we first  divide it into
   rectangles small  enough to fit into the  (double) buffer. Then, for  each rectangle we
   redraw  the root  widget within  this rectangle,  but into  the buffer.  The  buffer is
   positioned at the same  place as the rectangle in the host  window. Finally, the buffer
   is mapped to the host window.
   
   
read tools.anubis   
   
define (HostWindow,List(Rectangle)) -> One
   widget_paint_method
     (
       HostImage buffer, 
       Widget root_widget
     )  =
   with black = rgb(0,0,0), 
   (HostWindow win, List(Rectangle) clips) |->
      set_position(root_widget)(position_tool_box(0,0),position(0,0)); 
      if size(buffer) is (width_b,height_b) then 
      if size(win) is (win_width,win_height) then 
      with wrects = divide(rect(0,0,win_width,win_height),width_b,height_b), 
      if get_size(root_widget)(unique) is (root_w,root_h) then 
      //with clips = (List(Rectangle))[rect(0,30,300,90)], 
   
      //print("Subdivision:\n"); 
      //print(wrects); 
      //print("Clipping:\n"); 
      //print(clips); 
   
      map_forget((Rectangle wr) |-> 
          map_forget((Rectangle cr) |-> 
              if cr is rect(cx,cy,cu,cv) then 
              if wr is rect(bx,by,bu,bv) then 
              redraw(root_widget)(draw_tool_box(win,buffer,bx,by,0,0,cx,cy,cu,cv)); 
              paint_image(win,cr,bx,by,buffer),
            clips),
        wrects). 
   
   
   
      map_forget((Rectangle clip) |-> 
        (if clip is rect(cx,cy,cu,cv) then 
         if (cx < cu & cy < cv)
         then (
               with clip1 = (Rectangle)rect(max(0,cx),max(0,cy),cu,cv),
               with rects = divide(clip1,width_b,height_b), 
               forget(map(
                 (Rectangle r) |-> if r is rect(x,y,u,v) then 
                                   (
                                     //paint_rectangle(buffer,rect(0,0,u-x,v-y),black);
                                     redraw(root_widget)(draw_tool_box(win,buffer,x,y,0,0,x,y,u,v));
                                     paint_image(win,r,x,y,buffer)
                                   ),
               rects)))
             else unique),
        clips). 
   
   
   
   
   
   *** Events handling. 
   
   
   We must translate a widget answer into a list of rectangles.
   
define List(Rectangle)
   translate
     (
       HostWindow win, 
       WidgetAnswer a
     ) =
   if a is 
     {
       not_handled(l)  then if size(win) is (w,h) then 
                              map((WidgetRectangle wr) |-> 
                                    if wr is rect(x,y,u,v) then 
                                      rect(max(0,x),max(0,y),min(w,u),min(h,v)),
                                  l),
       handled(l)      then if size(win) is (w,h) then 
                              map((WidgetRectangle wr) |-> 
                                    if wr is rect(x,y,u,v) then 
                                      rect(max(0,x),max(0,y),min(w,u),min(h,v)),
                                  l),
       resized         then if size(win) is (w,h) then [rect(0,0,w,h)]
     }.
   

define Rectangle
   dummy_send_r
     (
       WidgetRectangle r
     ) =
   rect(0,0,0,0). 
   
define WidgetRectangle
   dummy_recv_r
     (
       Rectangle r
     ) =
   rect(0,0,0,0). 
   
   
   
   Handling events in widgets. 
   
define (HostWindow win, HostWindowEvent(One) e) -> List(Rectangle)   
   widget_event_handler
     (
       Widget                                                                 root_widget,
       Var((Var((Int32,Int32)),
            (KeyboardState ks, KeyboardKey k) -> WidgetAnswer,
            (List(WidgetRectangle) -> One redraw) -> One))                    k_handler_loc, 
       Var(Var(Bool))                                                         k_handler_v, 
       Var(Maybe((Var((Int32,Int32)),
                  (KeyboardState,WidgetMouseCapturedEvent) -> WidgetAnswer))) m_handler_loc,
       Var(Var(Bool))                                                         m_handler_v,
       Var(One)                                                               tick_v
     ) =
   (HostWindow win, HostWindowEvent(One) e) |-> 
   with dummy_p_v = var(((Int32,Int32))(0,0)), 
     if e is 
       {
         quit                   then alert, 
         expose                 then alert, 
         pointer_entering       then [ ], 
         pointer_leaving        then 
           translate(win,if *m_handler_loc is 
                       {
                         failure     then main_event_handler(root_widget)
                                           (event_tool_box(win,
                                                           dummy_p_v,k_handler_loc,k_handler_v,
                                                                     m_handler_loc,m_handler_v,
                                                                     tick_v,dummy_send_r,dummy_recv_r),
                                            mouse_gone),
                         success(xyh) then handled([])
                       }), 
         key_down(ks,k)         then if *k_handler_loc is (pos_loc,handler,eoc) then 
                                     if *pos_loc is (wx,wy) then 
                                     if handler(ks,k) is 
                                       {
                                         not_handled(l)  then 
                                           map((WidgetRectangle wr) |->
                                             if wr is rect(x,y,u,v) then 
                                               rect(x+wx,y+wy,u+wx,v+wy),l)
                                         handled(l)  then 
                                           map((WidgetRectangle wr) |->
                                             if wr is rect(x,y,u,v) then 
                                               rect(x+wx,y+wy,u+wx,v+wy),l)
                                         resized     then if size(win) is (w,h) then [rect(0,0,w,h)],
                                       }, 
         mouse_move(ks,x,y)     then 
           translate(win,if *m_handler_loc is 
                       {
                         failure     then main_event_handler(root_widget)
                                           (event_tool_box(win,
                                                           dummy_p_v,k_handler_loc,k_handler_v,
                                                                     m_handler_loc,m_handler_v,
                                                                     tick_v,dummy_send_r,dummy_recv_r),
                                            mouse_move(ks,x,y)),
                         success(xyh) then if xyh is (p_v,h) then if *p_v is (wx,wy) then 
                           if h(ks,moved(x-wx,y-wy)) is
                             {
                               not_handled(l)     then 
                                 not_handled(map((WidgetRectangle r) |->
                                   if r is rect(x,y,u,v) then rect(x+wx,y+wy,u+wx,v+wy),l)),
                               handled(l)     then 
                                 handled(map((WidgetRectangle r) |->
                                   if r is rect(x,y,u,v) then rect(x+wx,y+wy,u+wx,v+wy),l)),
                               resized        then resized
                             }
                       }), 
         mouse_click(ks,mc,x,y) then 
           translate(win,if *m_handler_loc is 
                       {
                         failure then main_event_handler(root_widget)
                                         (event_tool_box(win,
                                                         dummy_p_v,k_handler_loc,k_handler_v,
                                                                   m_handler_loc,m_handler_v,
                                                                   tick_v,dummy_send_r,dummy_recv_r),
                                          mouse_click(ks,mc,x,y)), 
                         success(xyh) then if xyh is (p_v,h) then if *p_v is (wx,wy) then 
                           m_handler_loc <- failure; 
                           (*m_handler_v) <- false; 
                           if h(ks,liberated(x-wx,y-wy)) is 
                             {
                               not_handled(l)     then 
                                 not_handled(map((WidgetRectangle r) |-> 
                                   if r is rect(x,y,u,v) then rect(x+wx,y+wy,u+wx,v+wy),l)),
                               handled(l)     then 
                                 handled(map((WidgetRectangle r) |-> 
                                   if r is rect(x,y,u,v) then rect(x+wx,y+wy,u+wx,v+wy),l)),
                               resized        then resized
                             }
                       }), 
         mouse_wheel(ks,d,x,y)  then [ ], 
         tick                   then tick_v <- unique; [ ], 
         repaint(l)             then l,
         specific(_)            then [ ]
       }.
   
   
   
   
   
   *** Events compression. 
   
   Events compression is necessary, because in  general too many window events arrive. The
   generic host  window handler (see  'predefined.anubis') recovers window events  not one
   after  the other  but  all  available events  in  a list  of  events (in  chronological
   order). This list must be optimized (compressed).
   
   The compression  is a kind  of rewritting  of the list.  We have choosen  the following
   rewrite rules:
   
     [expose . t]  --> [expose]          because expose redraws the whole window
     [mouse_move(ks1,x1,y1), mouse_move(ks2,x2,y2) . t] --> [mouse_move(ks2,x2,y2) . t]
   
   define List(HostWindowEvent(One))
   compress_events
     (
       List(HostWindowEvent(One)) l
     ) =
   if l is 
     {
       [ ] then [ ],
       [e1 . es1] then 
         if e1 is expose then [expose] else
         if e1 is pointer_entering then compress_events(es1) else
         if e1 is mouse_move(ks1,x1,y1) then if es1 is 
           {
             [ ] then l,
             [e2 . es2] then if e2 is mouse_move(ks2,x2,y2) 
                             then compress_events(es1)
                             else [e1 . compress_events(es1)]
           }
         else [e1 . compress_events(es1)]
     }.
   
define Bool     
   has_mouse_move
     (
       List(HostWindowEvent(One)) l
     ) =
     if l is 
       {
	       [] then false,
	       [e . es] then if e is mouse_move(_,_,_) then true else has_mouse_move(es)
       }.            

 
define List(HostWindowEvent(One))     
     compress_events1
       (
         List(HostWindowEvent(One)) l
       ) =
     if l is 
       {
	       [] then [], 
	       [e . es] then with es1 = compress_events1(es), 
	         if e is
	           {
		           quit                   then [quit], 
		           expose                 then [expose],
		           pointer_entering       then if member(es1,e) then es1 else [e . es1],
		           pointer_leaving        then if member(es1,e) then es1 else [e . es1],
		           key_down(_,_)          then [e . es1],
		           mouse_move(_,_,_)      then if has_mouse_move(es1) then es1 else [e . es1],
		           mouse_click(_,_,_,_)   then [e . es1],
		           mouse_wheel(_,_,_,_)   then [e . es1],
		           tick                   then [e . es1],
		           repaint(_)             then [e . es1],
		           specific(_)            then [e . es1]
	           }
       }.
   
       
define List(HostWindowEvent(One))     
     compress_events
       (
         List(HostWindowEvent(One)) l
       ) =
    with n = length(l), 
    result = compress_events1(l), 
    //print(n+" ---> "+length(result)+"\n");
    result. 
       
       
       
   *** Opening a host window with a root widget. 
   
   The function 'open_host_window' below creates  the buffer and the locations for capture
   handlers, sets the positions of the widgets and opens the host window.
   
public define Bool
   open_host_window
     (
       Int32                       x,   // position of host window on screen
       Int32                       y, 
       String                  title,   // title of host window
       Widget                content    // root widget in host window
     ) =
   with     buffer = to_host_image(create_rgba_image(1000,800,rgba(0,0,0,0)),1), 
     k_handler_loc = var((var(((Int32,Int32))(0,0)),
                          (KeyboardState ks, KeyboardKey k) |-> handled([]),
                          (List(WidgetRectangle) -> One rd) |-> unique)), 
     k_handler_v   = var(var((Bool)false)),
     m_handler_loc = var((Maybe((Var((Int32,Int32)),
                                 (KeyboardState,WidgetMouseCapturedEvent) -> WidgetAnswer)))failure),
     m_handler_v   = var(var((Bool)false)),
            tick_v = var((One)unique), 
   //with content = duplicate(content)(unique), 
   set_position(content)(position_tool_box(0,0),position(0,0)); 
   if get_size(content)(unique) is (w,h) then 
   if open_host_window(rect(x,y,x+w,y+h),
                       title,
                       managed, 
                       widget_paint_method(buffer,content),
                      (HostWindow win, HostWindowEvent(One) e) |->
                         with answer =
                           widget_event_handler(content,k_handler_loc,k_handler_v,
                                                        m_handler_loc,m_handler_v,
                                                        tick_v)(win,e),
                         set_position(content)(position_tool_box(0,0),position(0,0));
                         //(if e is tick then unique else (print("answer:\n"); print(answer))); 
                         answer, 
                       compress_events) is 
     {
       failure then print("Cannot open the window.\n"); false,
       success(win) then show(win); true
     }.
   
read tools/basis.anubis
   
public define Bool
   open_host_window
     (
       String title, 
       Widget content
     ) = 
   if get_size(content)(unique) is (w,h) then
   if screen_size is (sw,sh) then 
   open_host_window((max(0,sw-w))>>1,(max(0,sh-h))>>1,
                    title, 
                    content).