(* A demo of how to use the simple widgets *) ;; open SimpleWidget (* This paint function draws a fractal tree -- Google "L-systems" for *) (* more explanation / inspiration *) let paint (g:Gctx.gctx) : unit = let gnew = Gctx.with_color g Gctx.green in let rec fractal ((x,y):int*int) (angle:int) (len:int) = if len <= 1 then () else let lf = float_of_int len in let af = ((float_of_int angle) *. 3.14159) /. 180.0 in let nx = x + (int_of_float (lf *. (cos af))) in let ny = y + (int_of_float (lf *. (sin af))) in begin Gctx.draw_line gnew (x,y) (nx, ny); fractal (nx, ny) (angle + 20) (len - 2); fractal (nx, ny) (angle - 10) (len - 1) end in fractal (75,100) 270 15 (* Create some simple label widgets *) let l1 = label "Hello" let l2 = label "CIS" let l3 = label "120" (* Compose them horizontally, adding some borders *) let h = border (hpair (border l1) (hpair (space (10,10)) (hpair (border l2) (hpair (space (10,10)) (border l3))))) (* Create a canvas widget that draws the fractal tree *) let c = border (canvas (150, 120) paint) (* Put h next to c, with some space and more borders, just for fun.*) let top = border (hpair c (hpair (space (10, 10)) (hpair c (hpair (space (10,10)) h)))) (* Simplified event loop, no events in this example. *) let run (_:unit):unit = Graphics.open_graph ""; let g = Gctx.top_level in begin top.repaint g; ignore (Graphics.read_key ()) end ;; run ()