(* This program demonstrates how to build an application on top of the CIS 120 GUI library. It assumes that the eventloop.ml*, gctx.ml*, and widget.ml* files are all present. *) (* Create the state affected by the light switch *) let switched_on : bool ref = ref false (* Create a lightswitch button, initially labeled "ON " *) let (on_button_w,l,n) = Widget.button "ON " (* The action associated with clicking the switch. *) let flip () : unit = switched_on := not (!switched_on); if !switched_on then l.Widget.set_label "OFF" else l.Widget.set_label "ON " (* Add the flip action as a mouseclick_listener *) ;; n.Widget.add_event_listener (Widget.mouseclick_listener flip) (* Create the "QUIT" button *) let (quit_button_w,_,n2) = Widget.button "QUIT" (* The action associated with the QUIT button *) let quit () : unit = exit 0 (* Add the quit action as a mouseclick_listener *) ;; n2.Widget.add_event_listener (Widget.mouseclick_listener quit) (* Create the "lightbulb" canvas *) let repaint_light (g:Gctx.t) : unit = if !switched_on then let g = Gctx.set_color g Gctx.yellow in Gctx.fill_rect g (0,100) (100,100) else () let (light,_) = Widget.canvas (100,100) repaint_light (* Package all the pieces together into a root widget *) let w : Widget.t = Widget.border (Widget.border (Widget.hpair light (Widget.hpair (Widget.border on_button_w) (Widget.border quit_button_w)))) (* Start waiting for events *) ;; Eventloop.run w