1999-12-16 04:25:11 -08:00
|
|
|
(*************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml LablTk library *)
|
|
|
|
(* *)
|
|
|
|
(* Jun Furuse, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* Jacques Garrigue, Kyoto University RIMS *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1999 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique and Kyoto University. All rights reserved. *)
|
|
|
|
(* This file is distributed under the terms of the GNU Library *)
|
|
|
|
(* General Public License. *)
|
|
|
|
(* *)
|
|
|
|
(*************************************************************************)
|
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Clock/V, a simple clock.
|
|
|
|
Reverts every time you push the right button.
|
|
|
|
Adapted from ASCII/V May 1997
|
|
|
|
|
|
|
|
Uses Tk and Unix, so you must link with
|
|
|
|
labltklink unix.cma clock.ml -o clock -cclib -lunix
|
|
|
|
*)
|
|
|
|
|
|
|
|
open Tk
|
|
|
|
|
|
|
|
(* pi is not a constant! *)
|
|
|
|
let pi = acos (-1.)
|
|
|
|
|
|
|
|
(* The main class:
|
|
|
|
* create it with a parent: [new clock parent:top]
|
|
|
|
* initialize with [#init]
|
|
|
|
*)
|
|
|
|
|
2000-04-12 02:53:09 -07:00
|
|
|
class clock ~parent = object (self)
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* Instance variables *)
|
2000-04-12 02:53:09 -07:00
|
|
|
val canvas = Canvas.create ~width:100 ~height:100 parent
|
1999-11-30 06:59:39 -08:00
|
|
|
val mutable height = 100
|
|
|
|
val mutable width = 100
|
|
|
|
val mutable rflag = -1
|
|
|
|
|
|
|
|
(* Convert from -1.0 .. 1.0 to actual positions on the canvas *)
|
1999-12-16 00:37:38 -08:00
|
|
|
method x x0 = truncate (float width *. (x0 +. 1.) /. 2.)
|
|
|
|
method y y0 = truncate (float height *. (y0 +. 1.) /. 2.)
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
initializer
|
|
|
|
(* Create the oval border *)
|
2000-04-12 02:53:09 -07:00
|
|
|
Canvas.create_oval ~x1:1 ~y1:1 ~x2:(width - 2) ~y2:(height - 2)
|
|
|
|
~tags:["cadran"] ~width:3 ~outline:`Yellow ~fill:`White
|
2000-04-03 00:57:36 -07:00
|
|
|
canvas;
|
1999-11-30 06:59:39 -08:00
|
|
|
(* Draw the figures *)
|
|
|
|
self#draw_figures;
|
|
|
|
(* Create the arrows with dummy position *)
|
2000-04-16 05:38:28 -07:00
|
|
|
Canvas.create_line ~xys:[self#x 0., self#y 0.; self#x 0., self#y 0.]
|
2000-04-12 02:53:09 -07:00
|
|
|
~tags:["hours"] ~fill:`Red
|
2000-04-03 00:57:36 -07:00
|
|
|
canvas;
|
2000-04-16 05:38:28 -07:00
|
|
|
Canvas.create_line ~xys:[self#x 0., self#y 0.; self#x 0., self#y 0.]
|
2000-04-12 02:53:09 -07:00
|
|
|
~tags:["minutes"] ~fill:`Blue
|
2000-04-03 00:57:36 -07:00
|
|
|
canvas;
|
2000-04-16 05:38:28 -07:00
|
|
|
Canvas.create_line ~xys:[self#x 0., self#y 0.; self#x 0., self#y 0.]
|
2000-04-12 02:53:09 -07:00
|
|
|
~tags:["seconds"] ~fill:`Black
|
2000-04-03 00:57:36 -07:00
|
|
|
canvas;
|
1999-11-30 06:59:39 -08:00
|
|
|
(* Setup a timer every second *)
|
|
|
|
let rec timer () =
|
|
|
|
self#draw_arrows (Unix.localtime (Unix.time ()));
|
2000-04-12 02:53:09 -07:00
|
|
|
Timer.add ~ms:1000 ~callback:timer; ()
|
1999-11-30 06:59:39 -08:00
|
|
|
in timer ();
|
|
|
|
(* Redraw when configured (changes size) *)
|
2000-04-12 02:53:09 -07:00
|
|
|
bind ~events:[`Configure]
|
|
|
|
~action:(fun _ ->
|
1999-11-30 06:59:39 -08:00
|
|
|
width <- Winfo.width canvas;
|
|
|
|
height <- Winfo.height canvas;
|
2000-04-03 00:57:36 -07:00
|
|
|
self#redraw)
|
|
|
|
canvas;
|
1999-11-30 06:59:39 -08:00
|
|
|
(* Change direction with right button *)
|
2000-04-12 02:53:09 -07:00
|
|
|
bind ~events:[`ButtonPressDetail 3]
|
|
|
|
~action:(fun _ -> rflag <- -rflag; self#redraw)
|
2000-04-03 00:57:36 -07:00
|
|
|
canvas;
|
1999-11-30 06:59:39 -08:00
|
|
|
(* Pack, expanding in both directions *)
|
2000-04-12 02:53:09 -07:00
|
|
|
pack ~fill:`Both ~expand:true [canvas]
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* Redraw everything *)
|
|
|
|
method redraw =
|
2000-04-14 02:46:08 -07:00
|
|
|
Canvas.coords_set canvas (`Tag "cadran")
|
2000-04-16 05:38:28 -07:00
|
|
|
~xys:[ 1, 1; width - 2, height - 2 ];
|
1999-11-30 06:59:39 -08:00
|
|
|
self#draw_figures;
|
|
|
|
self#draw_arrows (Unix.localtime (Unix.time ()))
|
|
|
|
|
|
|
|
(* Delete and redraw the figures *)
|
|
|
|
method draw_figures =
|
2000-04-12 18:11:06 -07:00
|
|
|
Canvas.delete canvas [`Tag "figures"];
|
1999-11-30 06:59:39 -08:00
|
|
|
for i = 1 to 12 do
|
|
|
|
let angle = float (rflag * i - 3) *. pi /. 6. in
|
2000-04-03 00:57:36 -07:00
|
|
|
Canvas.create_text
|
2000-04-12 02:53:09 -07:00
|
|
|
~x:(self#x (0.8 *. cos angle)) ~y:(self#y (0.8 *. sin angle))
|
|
|
|
~tags:["figures"]
|
|
|
|
~text:(string_of_int i) ~font:"variable"
|
|
|
|
~anchor:`Center
|
2000-04-03 00:57:36 -07:00
|
|
|
canvas
|
1999-11-30 06:59:39 -08:00
|
|
|
done
|
|
|
|
|
|
|
|
(* Resize and reposition the arrows *)
|
|
|
|
method draw_arrows tm =
|
2000-04-12 18:11:06 -07:00
|
|
|
Canvas.configure_line ~width:(min width height / 40)
|
|
|
|
canvas (`Tag "hours");
|
1999-11-30 06:59:39 -08:00
|
|
|
let hangle =
|
|
|
|
float (rflag * (tm.Unix.tm_hour * 60 + tm.Unix.tm_min) - 180)
|
|
|
|
*. pi /. 360. in
|
2000-04-14 02:46:08 -07:00
|
|
|
Canvas.coords_set canvas (`Tag "hours")
|
2000-04-16 05:38:28 -07:00
|
|
|
~xys:[ self#x 0., self#y 0.;
|
|
|
|
self#x (cos hangle /. 2.), self#y (sin hangle /. 2.) ];
|
2000-04-12 18:11:06 -07:00
|
|
|
Canvas.configure_line ~width:(min width height / 50)
|
|
|
|
canvas (`Tag "minutes");
|
1999-11-30 06:59:39 -08:00
|
|
|
let mangle = float (rflag * tm.Unix.tm_min - 15) *. pi /. 30. in
|
2000-04-14 02:46:08 -07:00
|
|
|
Canvas.coords_set canvas (`Tag "minutes")
|
2000-04-16 05:38:28 -07:00
|
|
|
~xys:[ self#x 0., self#y 0.;
|
|
|
|
self#x (cos mangle /. 1.5), self#y (sin mangle /. 1.5) ];
|
1999-11-30 06:59:39 -08:00
|
|
|
let sangle = float (rflag * tm.Unix.tm_sec - 15) *. pi /. 30. in
|
2000-04-14 02:46:08 -07:00
|
|
|
Canvas.coords_set canvas (`Tag "seconds")
|
2000-04-16 05:38:28 -07:00
|
|
|
~xys:[ self#x 0., self#y 0.;
|
|
|
|
self#x (cos sangle /. 1.25), self#y (sin sangle /. 1.25) ]
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
(* Initialize the Tcl interpreter *)
|
|
|
|
let top = openTk ()
|
|
|
|
|
|
|
|
(* Create a clock on the main window *)
|
|
|
|
let clock =
|
2000-04-12 02:53:09 -07:00
|
|
|
new clock ~parent:top
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* Wait for events *)
|
|
|
|
let _ = mainLoop ()
|