]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/matitaAutoGui.ml
added support for font scaling to autogui
[helm.git] / helm / software / matita / matitaAutoGui.ml
1 (* Copyright (C) 2003, HELM Team.
2  * 
3  * 
4  * This file is part of HELM, an Hypertextual, Electronic
5  * Library of Mathematics, developed at the Computer Science
6  * Department, University of Bologna, Italy.
7  * 
8  * HELM is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * HELM is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with HELM; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  * MA  02111-1307, USA.
22  * 
23  * For details, see the HELM World-Wide-Web page,
24  * http://cs.unibo.it/helm/.
25  *)
26
27 type status = 
28   Cic.context * (int * Cic.term * bool * int * (int * Cic.term) list) list * 
29   (int * Cic.term * int) list *
30   Cic.term list
31 let fake_goal = Cic.Implicit None;;
32 let fake_candidates = [];;
33
34 let start = ref 0;;
35 let len = ref 10;;
36
37
38 let font_size = ref 
39   (Helm_registry.get_opt_default Helm_registry.int
40     ~default:BuildTimeConf.default_font_size "matita.font_size")
41 ;;
42
43 let set_font_size n = font_size := n;;
44
45 let pp ?(size=(!font_size)) c t =
46   let t = 
47     ProofEngineReduction.replace 
48       ~equality:(fun a b -> match b with Cic.Meta _ -> true | _ -> false) 
49       ~what:[Cic.Rel 1] ~with_what:[Cic.Implicit None] ~where:t
50   in
51   (if size > 0 then "<span font_desc=\""^string_of_int size^"\">" else "") ^
52     ApplyTransformation.txt_of_cic_term ~map_unicode_to_tex:false
53       max_int [] c t^(if size > 0 then "</span>" else "")
54 ;;
55 let pp_goal context x = 
56   if x == fake_goal then "" else pp context x
57 ;;
58 let pp_candidate context x = pp context x
59 let pp_candidate_tip context x = pp ~size:0 context x
60 let pp_candidate_type context x = 
61   try 
62     let ty, _ = 
63       CicTypeChecker.type_of_aux' [] ~subst:[] 
64        context x CicUniv.oblivion_ugraph
65     in
66       pp ~size:0 context ty
67   with CicUtil.Meta_not_found _ ->  pp ~size:0 context x
68 ;;
69
70 let sublist start len l = 
71   let rec aux pos len = function
72     | [] when pos < start -> aux (pos+1) len []
73     | [] when len > 0 -> 
74           (0,fake_goal, false, 0, fake_candidates) :: aux (pos+1) (len-1) [] 
75     | [] (* when len <= 0 *) -> []
76     | he::tl when pos < start -> aux (pos+1) len tl
77     | he::tl when pos >= start && len > 0 -> he::aux (pos+1) (len-1) tl
78     | he::tl (* when pos >= start && len <= 0 *) -> []
79   in
80   aux 0 len l
81 ;;
82
83 let cell_of_candidate height context ?(active=false) g id c = 
84   let tooltip = GData.tooltips () in (* should be only one isn't it? *)
85   let button = GButton.button () in
86   let l = GMisc.label ~markup:(pp_candidate context c) () in
87   button#add l#coerce;
88   button#misc#set_size_request ~height ();
89   if active then
90     button#misc#set_sensitive false;
91   let text = 
92     "Follow/Prune computation of\n"^pp_candidate_tip context c^": "^
93     pp_candidate_type context c
94   in
95   tooltip#set_tip ~text (button :> GObj.widget);
96   ignore(button#connect#clicked 
97     (fun _ -> 
98       let menu = GMenu.menu () in
99       let follow = GMenu.menu_item ~label:"Follow" () in
100       let prune = GMenu.menu_item ~label:"Prune" () in
101       ignore (follow#connect#activate 
102         (fun _ -> HLog.warn (string_of_int id); Auto.give_hint id));
103       ignore (prune#connect#activate 
104         (fun _ -> HLog.warn (string_of_int id); Auto.give_prune_hint id));
105       menu#append follow;
106       menu#append prune;
107       menu#popup 0 (GtkMain.Main.get_current_event_time ())));
108   button
109 ;;
110 let cell_of_goal height win_width context goal = 
111   GMisc.label 
112     ~markup:(pp_goal context goal) ~xalign:0.0 
113     ~width:(min (win_width * 60 / 100) 500) 
114     ~line_wrap:false
115     ~ellipsize:`END
116     ~height
117     ()
118 ;;
119 let cell_of_row_header height n k id = 
120   GMisc.label
121     ~markup:("<span stretch=\"ultracondensed\">" ^ string_of_int n ^ "<sub>(" ^
122              string_of_int id ^ "|" ^ string_of_int k ^ ")</sub></span>") 
123     ~line_wrap:true ~justify:`LEFT ~height ~width:80 ()
124 ;;
125 let cell_of_candidates height grey context goal cads = 
126   let hbox = GPack.hbox () in
127   match cads with
128   | [] -> hbox
129   | (id,c)::tl ->
130       hbox#pack 
131         (cell_of_candidate height ~active:grey 
132           context goal id c :> GObj.widget);
133       List.iter
134         (fun (id,c) -> 
135         hbox#pack (cell_of_candidate height context goal id c :> GObj.widget)) tl;
136         hbox
137 ;;
138
139 let elems_to_rows height context win_width (table : GPack.table) (or_list) =
140   let height = height / ((List.length or_list) + 1) in
141   let _ = 
142    List.fold_left 
143     (fun position (goal_id, goal, grey, depth, candidates) ->
144       table#attach ~left:0 ~top:position 
145         (cell_of_row_header height (position + !start) 
146           depth goal_id :> GObj.widget);
147       table#attach ~left:1 ~top:position ~fill:`BOTH
148         (cell_of_goal height win_width context goal :> GObj.widget);
149       table#attach ~left:2 ~top:position 
150         (cell_of_candidates height grey context goal candidates :> GObj.widget);
151       position+1)
152     0 or_list
153   in 
154   ()
155 ;;
156
157 let old_displayed_status = ref [];;
158 let old_size = ref 0;;
159
160 let fill_win (win : MatitaGeneratedGui.autoWin) cb =
161   let init = Unix.gettimeofday () in
162     try 
163       let (status : status) = cb () in
164       let win_size = win#toplevel#misc#allocation.Gtk.width in
165       let ctx, or_list, and_list, last_moves = status in
166       let displayed_status = 
167         sublist !start !len 
168           (or_list @ (List.map (fun (id,x,d) -> id,x,false,d,[]) and_list)) 
169       in
170       if displayed_status <> !old_displayed_status || !old_size <> win_size then
171         begin
172           old_displayed_status := displayed_status;
173           old_size := win_size;
174 (*
175           win#labelLAST#set_text 
176             (String.concat ";" (List.map (pp_candidate ctx) last_moves));
177 *)
178           List.iter win#table#remove win#table#children;
179           let height = win#viewportAREA#misc#allocation.Gtk.height in
180           elems_to_rows height ctx win_size win#table displayed_status;
181           Printf.eprintf 
182             "REDRAW COST: %2.1f\n%!" (Unix.gettimeofday () -.  init);
183         end
184     with exn -> prerr_endline (Printexc.to_string exn); ()
185 ;;
186
187 let auto_dialog elems =
188   let win = new MatitaGeneratedGui.autoWin () in
189   let width = Gdk.Screen.width () in
190   let height = Gdk.Screen.height () in
191   let main_w = width * 70 / 100 in 
192   let main_h = height * 60 / 100 in
193   win#toplevel#resize ~width:main_w ~height:main_h;
194   win#check_widgets ();
195   win#table#set_columns 3;
196   win#table#set_col_spacings 6;
197   win#table#set_row_spacings 4;
198   ignore(win#buttonUP#connect#clicked 
199     (fun _ -> start := max 0 (!start -1); fill_win win elems));
200   ignore(win#buttonDOWN#connect#clicked 
201     (fun _ -> incr start; fill_win win elems));
202   ignore(win#buttonCLOSE#connect#clicked 
203     (fun _ -> 
204       Auto.pause false;
205       Auto.step ();
206       win#toplevel#destroy ();
207       GMain.Main.quit ()));
208   ignore(win#toplevel#event#connect#delete
209     (fun _ -> 
210       Auto.pause false;
211       Auto.step ();
212       win#toplevel#destroy ();
213       GMain.Main.quit ();true));
214   let redraw_callback _ = fill_win win elems;true in
215   let redraw = GMain.Timeout.add ~ms:400 ~callback:redraw_callback in
216   ignore(win#buttonPAUSE#connect#clicked 
217     (fun _ -> 
218       Auto.pause true;
219       win#buttonPLAY#misc#set_sensitive true;
220       win#buttonPAUSE#misc#set_sensitive false;));
221   ignore(win#buttonPLAY#connect#clicked 
222     (fun _ -> 
223       Auto.pause false;
224       Auto.step ();
225       win#buttonPLAY#misc#set_sensitive false;
226       win#buttonPAUSE#misc#set_sensitive true;));
227   ignore(win#buttonNEXT#connect#clicked 
228     (fun _ -> Auto.step ()));
229   Auto.pause true;
230   win#buttonPLAY#misc#set_sensitive true;
231   win#buttonPAUSE#misc#set_sensitive false;  
232   fill_win win elems;
233   win#toplevel#show ();
234   GtkThread.main ();
235   GMain.Timeout.remove redraw;
236 ;;
237