1 (* Copyright (C) 2006, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 type attribute = string * string (* <key, value> pair *)
32 let png_flags = "-Tpng"
33 let map_flags = "-Tcmapx"
35 let tempfile () = Filename.temp_file "matita_" ""
37 class type graphviz_widget =
39 method load_graph_from_file: ?gviz_cmd:string -> string -> unit
41 (GdkEvent.Button.t -> (string * string) list -> unit) -> unit
42 method center_on_href: string -> unit
43 method as_image: GMisc.image
44 method as_viewport: GBin.viewport
47 class graphviz_impl ?packing () =
48 let viewport = GBin.viewport ?packing () in
49 let mk_gviz_cmd gviz_cmd flags src_fname dest_fname =
50 sprintf "cat %s | %s %s > %s" src_fname gviz_cmd flags
53 GMisc.image ~packing:viewport#add ~xalign:0. ~yalign:0. ~xpad:0 ~ypad:0 ()
56 match List.map float_of_string (HExtlib.split ~sep:',' s) with
57 | [x1; y1; x2; y2 ] -> x1, y1, x2, y2
58 | _ -> assert false in
60 val mutable href_cb = fun _ _ -> ()
61 val mutable map = [] (* list of associative list attr name -> attr value *)
64 ignore (viewport#event#connect#button_press (fun button ->
65 (*eprintf "x: %f; y: %f;\n%!" (GdkEvent.Button.x button +. viewport#hadjustment#value) (GdkEvent.Button.y button +. viewport#vadjustment#value);*)
66 (* compute coordinates relative to image origin *)
67 let x = GdkEvent.Button.x button +. viewport#hadjustment#value in
68 let y = GdkEvent.Button.y button +. viewport#vadjustment#value in
69 (try href_cb button (self#find_href x y) with Not_found -> ());
72 method load_graph_from_file ?(gviz_cmd = "dot") fname =
73 let tmp_png = tempfile () in
74 let rc = Sys.command (mk_gviz_cmd gviz_cmd png_flags fname tmp_png) in
77 ("Graphviz command failed (exit code: %d) on the following graph:\n"
79 rc (HExtlib.input_file fname);
80 image#set_file tmp_png;
81 HExtlib.safe_remove tmp_png;
82 let tmp_map = tempfile () in
83 ignore (Sys.command (mk_gviz_cmd gviz_cmd map_flags fname tmp_map));
84 self#load_map tmp_map;
85 HExtlib.safe_remove tmp_map
87 method private load_map fname =
90 try List.assoc "shape" l = "rect" with Not_found -> false
93 XmlPushParser.create_parser
94 { XmlPushParser.default_callbacks with
95 XmlPushParser.start_element =
96 Some (fun elt attrs ->
98 | "area" when is_rect attrs -> areas := attrs :: !areas
100 XmlPushParser.parse p (`File fname);
103 method private find_href x y =
106 let x1, y1, x2, y2 = parse_coords (List.assoc "coords" attrs) in
107 x1 <= x && x <= x2 && y1 <= y && y <= y2)
111 (cb: GdkEvent.Button.t -> (string * string) list -> unit)
115 method center_on_href href =
116 (*eprintf "Centering viewport on uri %s\n%!" href;*)
120 try List.assoc "href" attrs = href with Not_found -> false)
123 let x1, y1, x2, y2 = parse_coords (List.assoc "coords" attrs) in
124 viewport#hadjustment#clamp_page ~lower:x1 ~upper:x2;
125 viewport#vadjustment#clamp_page ~lower:y1 ~upper:y2;
128 method as_image = image
129 method as_viewport = viewport
133 let graphviz ?packing () =
134 (new graphviz_impl ?packing () :> graphviz_widget)