]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaGui.ml
added find&replace facility
[helm.git] / helm / matita / matitaGui.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 open MatitaGeneratedGui
29 open MatitaGtkMisc
30 open MatitaMisc
31
32 let gui_instance = ref None ;;
33
34 class type browserWin =
35   (* this class exists only because GEdit.combo_box_entry is not supported by
36    * lablgladecc :-(((( *)
37 object
38   inherit MatitaGeneratedGui.browserWin
39   method browserUri: GEdit.combo_box_entry
40 end
41
42 class console ~(buffer: GText.buffer) () =
43   object (self)
44     val error_tag   = buffer#create_tag [ `FOREGROUND "red" ]
45     val warning_tag = buffer#create_tag [ `FOREGROUND "orange" ]
46     val message_tag = buffer#create_tag []
47     val debug_tag   = buffer#create_tag [ `FOREGROUND "#888888" ]
48     method message s = buffer#insert ~iter:buffer#end_iter ~tags:[message_tag] s
49     method error s   = buffer#insert ~iter:buffer#end_iter ~tags:[error_tag] s
50     method warning s = buffer#insert ~iter:buffer#end_iter ~tags:[warning_tag] s
51     method debug s   = buffer#insert ~iter:buffer#end_iter ~tags:[debug_tag] s
52     method clear () =
53       buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter
54     method log_callback (tag: MatitaLog.log_tag) s =
55       match tag with
56       | `Debug -> self#debug (s ^ "\n")
57       | `Error -> self#error (s ^ "\n")
58       | `Message -> self#message (s ^ "\n")
59       | `Warning -> self#warning (s ^ "\n")
60   end
61         
62 let clean_current_baseuri status = 
63     try  
64       let baseuri = MatitaTypes.get_string_option status "baseuri" in
65       MatitacleanLib.clean_baseuris [baseuri]
66     with MatitaTypes.Option_error _ -> ()
67
68 let ask_and_save_moo_if_needed parent fname status = 
69   let save () =
70     MatitacLib.dump_moo_to_file fname status.MatitaTypes.moo_content_rev in
71   if (MatitaScript.instance ())#eos &&
72      status.MatitaTypes.proof_status = MatitaTypes.No_proof
73   then
74     begin
75       let mooname = 
76         MatitaMisc.obj_file_of_script fname
77       in
78       let rc = 
79         MatitaGtkMisc.ask_confirmation
80         ~title:"A .moo can be generated"
81         ~message:(Printf.sprintf 
82           "%s can be generated for %s.\n<i>Should I generate it?</i>"
83           mooname fname)
84         ~parent ()
85       in
86       let b = 
87         match rc with 
88         | `YES -> true 
89         | `NO -> false 
90         | `CANCEL -> raise MatitaTypes.Cancel 
91       in
92       if b then
93         save ()
94       else
95         clean_current_baseuri status
96     end
97   else
98     clean_current_baseuri status 
99     
100 let ask_unsaved parent =
101   MatitaGtkMisc.ask_confirmation 
102     ~parent ~title:"Unsaved work!" 
103     ~message:("Your work is <b>unsaved</b>!\n\n"^
104          "<i>Do you want to save the script before exiting?</i>")
105     ()
106
107 class gui () =
108     (* creation order _is_ relevant for windows placement *)
109   let main = new mainWin () in
110   let about = new aboutWin () in
111   let fileSel = new fileSelectionWin () in
112   let findRepl = new findReplWin () in
113   let keyBindingBoxes = (* event boxes which should receive global key events *)
114     [ main#mainWinEventBox ]
115   in
116   let console = new console ~buffer:main#logTextView#buffer () in
117   let (source_view: GSourceView.source_view) =
118     GSourceView.source_view
119       ~auto_indent:true
120       ~insert_spaces_instead_of_tabs:true ~tabs_width:2
121       ~margin:80 ~show_margin:true
122       ~smart_home_end:true
123       ~packing:main#scriptScrolledWin#add
124       ()
125   in
126   let default_font_size =
127     Helm_registry.get_opt_default Helm_registry.int
128       ~default:BuildTimeConf.default_font_size "matita.font_size"
129   in
130   let source_buffer = source_view#source_buffer in
131   object (self)
132     val mutable chosen_file = None
133     val mutable _ok_not_exists = false
134     val mutable script_fname = None
135     val mutable font_size = default_font_size
136    
137     initializer
138         (* glade's check widgets *)
139       List.iter (fun w -> w#check_widgets ())
140         (let c w = (w :> <check_widgets: unit -> unit>) in
141         [ c about; c fileSel; c main; c findRepl]);
142         (* key bindings *)
143       List.iter (* global key bindings *)
144         (fun (key, callback) -> self#addKeyBinding key callback)
145 (*
146         [ GdkKeysyms._F3,
147             toggle_win ~check:main#showProofMenuItem proof#proofWin;
148           GdkKeysyms._F4,
149             toggle_win ~check:main#showCheckMenuItem check#checkWin;
150 *)
151         [ ];
152         (* about win *)
153       ignore (about#aboutWin#event#connect#delete (fun _ -> true));
154       ignore (main#aboutMenuItem#connect#activate (fun _ ->
155         about#aboutWin#show ()));
156       connect_button about#aboutDismissButton (fun _ ->
157         about#aboutWin#misc#hide ());
158       about#aboutLabel#set_label (Pcre.replace ~pat:"@VERSION@"
159         ~templ:BuildTimeConf.version about#aboutLabel#label);
160         (* findRepl win *)
161       let show_find_Repl () = 
162         findRepl#toplevel#misc#show ();
163         findRepl#toplevel#misc#grab_focus ()
164       in
165       let hide_find_Repl () = findRepl#toplevel#misc#hide () in
166       let find_forward _ = 
167           let highlight start end_ =
168             source_buffer#move_mark `INSERT ~where:start;
169             source_buffer#move_mark `SEL_BOUND ~where:end_
170           in
171           let text = findRepl#findEntry#text in
172           let iter = source_buffer#get_iter `SEL_BOUND in
173           match iter#forward_search text with
174           | None -> 
175               (match source_buffer#start_iter#forward_search text with
176               | None -> ()
177               | Some (start,end_) -> highlight start end_)
178           | Some (start,end_) -> highlight start end_ 
179       in
180       let replace _ =
181         let text = findRepl#replaceEntry#text in
182         let ins = source_buffer#get_iter `INSERT in
183         let sel = source_buffer#get_iter `SEL_BOUND in
184         if ins#compare sel < 0 then 
185           begin
186             ignore(source_buffer#delete_selection ());
187             source_buffer#insert text
188           end
189       in
190       connect_button findRepl#findButton find_forward;
191       connect_button findRepl#findReplButton replace;
192       connect_button findRepl#cancelButton (fun _ -> hide_find_Repl ());
193       ignore(findRepl#toplevel#event#connect#delete 
194         ~callback:(fun _ -> hide_find_Repl ();true));
195       ignore(self#main#findReplMenuItem#connect#activate
196         ~callback:show_find_Repl);
197       ignore (findRepl#findEntry#connect#activate ~callback:find_forward);
198         (* file selection win *)
199       ignore (fileSel#fileSelectionWin#event#connect#delete (fun _ -> true));
200       ignore (fileSel#fileSelectionWin#connect#response (fun event ->
201         let return r =
202           chosen_file <- r;
203           fileSel#fileSelectionWin#misc#hide ();
204           GMain.Main.quit ()
205         in
206         match event with
207         | `OK ->
208             let fname = fileSel#fileSelectionWin#filename in
209             if Sys.file_exists fname then
210               (if is_regular fname then return (Some fname))
211             else
212               (if _ok_not_exists then return (Some fname))
213         | `CANCEL -> return None
214         | `HELP -> ()
215         | `DELETE_EVENT -> return None));
216         (* menus *)
217       List.iter (fun w -> w#misc#set_sensitive false) [ main#saveMenuItem ];
218       main#helpMenu#set_right_justified true;
219         (* console *)
220       let adj = main#logScrolledWin#vadjustment in
221       ignore (adj#connect#changed
222                 (fun _ -> adj#set_value (adj#upper -. adj#page_size)));
223       console#message (sprintf "\tMatita version %s\n" BuildTimeConf.version);
224         (* toolbar *)
225       let module A = TacticAst in
226       let hole = CicAst.UserInput in
227       let loc = CicAst.dummy_floc in
228       let tac ast _ =
229         if (MatitaScript.instance ())#onGoingProof () then
230           (MatitaScript.instance ())#advance
231             ~statement:("\n" ^ TacticAstPp.pp_tactical (A.Tactic (loc, ast))) ()
232       in
233       let tac_w_term ast _ =
234         if (MatitaScript.instance ())#onGoingProof () then
235           let buf = source_buffer in
236           buf#insert ~iter:(buf#get_iter_at_mark (`NAME "locked"))
237             ("\n" ^ TacticAstPp.pp_tactic ast)
238       in
239       let tbar = self#main in
240       connect_button tbar#introsButton (tac (A.Intros (loc, None, [])));
241       connect_button tbar#applyButton (tac_w_term (A.Apply (loc, hole)));
242       connect_button tbar#exactButton (tac_w_term (A.Exact (loc, hole)));
243       connect_button tbar#elimButton (tac_w_term (A.Elim (loc, hole, None, None, [])));
244       connect_button tbar#elimTypeButton (tac_w_term (A.ElimType (loc, hole, None, None, [])));
245       connect_button tbar#splitButton (tac (A.Split loc));
246       connect_button tbar#leftButton (tac (A.Left loc));
247       connect_button tbar#rightButton (tac (A.Right loc));
248       connect_button tbar#existsButton (tac (A.Exists loc));
249       connect_button tbar#reflexivityButton (tac (A.Reflexivity loc));
250       connect_button tbar#symmetryButton (tac (A.Symmetry loc));
251       connect_button tbar#transitivityButton
252         (tac_w_term (A.Transitivity (loc, hole)));
253       connect_button tbar#assumptionButton (tac (A.Assumption loc));
254       connect_button tbar#cutButton (tac_w_term (A.Cut (loc, None, hole)));
255       connect_button tbar#autoButton (tac (A.Auto (loc,None,None)));
256       MatitaGtkMisc.toggle_widget_visibility
257        ~widget:(self#main#tacticsButtonsHandlebox :> GObj.widget)
258        ~check:self#main#tacticsBarMenuItem;
259       let module Hr = Helm_registry in
260       if
261         not (Hr.get_opt_default Hr.bool ~default:false "matita.tactics_bar")
262       then 
263         self#main#tacticsBarMenuItem#set_active false;
264       MatitaGtkMisc.toggle_callback 
265         ~callback:(function 
266           | true -> self#main#toplevel#fullscreen () 
267           | false -> self#main#toplevel#unfullscreen ())
268         ~check:self#main#fullscreenMenuItem;
269       self#main#fullscreenMenuItem#set_active false;
270         (* log *)
271       MatitaLog.set_log_callback self#console#log_callback;
272       GtkSignal.user_handler :=
273         (fun exn -> MatitaLog.error (MatitaExcPp.to_string exn));
274         (* script *)
275       let _ =
276         match GSourceView.source_language_from_file BuildTimeConf.lang_file with
277         | None ->
278             MatitaLog.warn (sprintf "can't load language file %s"
279               BuildTimeConf.lang_file)
280         | Some matita_lang ->
281             source_buffer#set_language matita_lang;
282             source_buffer#set_highlight true
283       in
284       let s () = MatitaScript.instance () in
285       let disableSave () =
286         script_fname <- None;
287         self#main#saveMenuItem#misc#set_sensitive false
288       in
289       let saveAsScript () =
290         let script = s () in
291         match self#chooseFile ~ok_not_exists:true () with
292         | Some f -> 
293               script#assignFileName f;
294               script#saveToFile (); 
295               console#message ("'"^f^"' saved.\n");
296               self#_enableSaveTo f
297         | None -> ()
298       in
299       let saveScript () =
300         match script_fname with
301         | None -> saveAsScript ()
302         | Some f -> 
303               (s ())#assignFileName f;
304               (s ())#saveToFile ();
305               console#message ("'"^f^"' saved.\n");
306       in
307       let loadScript () =
308         let script = s () in 
309         let status = script#status in
310         try 
311           if source_view#buffer#modified then
312             begin
313               match ask_unsaved main#toplevel with
314               | `YES -> saveScript ()
315               | `NO -> ()
316               | `CANCEL -> raise MatitaTypes.Cancel
317             end;
318           (match script_fname with
319           | None -> ()
320           | Some fname -> 
321               ask_and_save_moo_if_needed main#toplevel fname status);
322           match self#chooseFile () with
323           | Some f -> 
324                 script#reset (); 
325                 script#assignFileName f;
326                 script#loadFromFile (); 
327                 console#message ("'"^f^"' loaded.\n");
328                 self#_enableSaveTo f
329           | None -> ()
330         with MatitaTypes.Cancel -> ()
331       in
332       let newScript () = 
333         (s ())#reset (); 
334         (s ())#template (); 
335         disableSave ();
336         script_fname <- None
337       in
338       let cursor () =
339         source_buffer#place_cursor
340           (source_buffer#get_iter_at_mark (`NAME "locked"))
341       in
342       let advance _ = (MatitaScript.instance ())#advance (); cursor () in
343       let retract _ = (MatitaScript.instance ())#retract (); cursor () in
344       let top _ = (MatitaScript.instance ())#goto `Top (); cursor () in
345       let bottom _ = (MatitaScript.instance ())#goto `Bottom (); cursor () in
346       let jump _ = (MatitaScript.instance ())#goto `Cursor (); cursor () in
347       let connect_key sym f =
348         connect_key self#main#mainWinEventBox#event
349           ~modifiers:[`CONTROL] ~stop:true sym f;
350         connect_key self#sourceView#event
351           ~modifiers:[`CONTROL] ~stop:true sym f
352       in
353         (* quit *)
354       self#setQuitCallback (fun () -> 
355         let status = (MatitaScript.instance ())#status in
356         if source_view#buffer#modified then
357           begin
358             let rc = ask_unsaved main#toplevel in 
359             try
360               match rc with
361               | `YES -> saveScript ();
362                         if not source_view#buffer#modified then
363                           begin
364                             (match script_fname with
365                             | None -> ()
366                             | Some fname -> 
367                                ask_and_save_moo_if_needed 
368                                  main#toplevel fname status);
369                           GMain.Main.quit ()
370                           end
371               | `NO -> GMain.Main.quit ()
372               | `CANCEL -> raise MatitaTypes.Cancel
373             with MatitaTypes.Cancel -> ()
374           end 
375         else 
376           begin  
377             (match script_fname with
378             | None -> clean_current_baseuri status; GMain.Main.quit ()
379             | Some fname ->
380                 try
381                   ask_and_save_moo_if_needed main#toplevel fname status;
382                   GMain.Main.quit ()
383                 with MatitaTypes.Cancel -> ())
384           end);
385       connect_button self#main#scriptAdvanceButton advance;
386       connect_button self#main#scriptRetractButton retract;
387       connect_button self#main#scriptTopButton top;
388       connect_button self#main#scriptBottomButton bottom;
389       connect_key GdkKeysyms._Down advance;
390       connect_key GdkKeysyms._Up retract;
391       connect_key GdkKeysyms._Home top;
392       connect_key GdkKeysyms._End bottom;
393       connect_button self#main#scriptJumpButton jump;
394       connect_menu_item self#main#openMenuItem   loadScript;
395       connect_menu_item self#main#saveMenuItem   saveScript;
396       connect_menu_item self#main#saveAsMenuItem saveAsScript;
397       connect_menu_item self#main#newMenuItem    newScript;
398       connect_key GdkKeysyms._period
399         (fun () ->
400           source_buffer#insert ~iter:(source_buffer#get_iter_at_mark `INSERT)
401             ".\n";
402           advance ());
403       connect_key GdkKeysyms._Return
404         (fun () ->
405           source_buffer#insert ~iter:(source_buffer#get_iter_at_mark `INSERT)
406             "\n";
407           advance ());
408          (* script monospace font stuff *)  
409       self#updateFontSize ();
410         (* debug menu *)
411       self#main#debugMenu#misc#hide ();
412         (* status bar *)
413       self#main#hintLowImage#set_file (image_path "matita-bulb-low.png");
414       self#main#hintMediumImage#set_file (image_path "matita-bulb-medium.png");
415       self#main#hintHighImage#set_file (image_path "matita-bulb-high.png");
416         (* focus *)
417       self#sourceView#misc#grab_focus ();
418         (* main win dimension *)
419       let width = Gdk.Screen.width () in
420       let height = Gdk.Screen.height () in
421       let main_w = width * 90 / 100 in 
422       let main_h = height * 80 / 100 in
423       let script_w = main_w * 6 / 10 in
424       self#main#toplevel#resize ~width:main_w ~height:main_h;
425       self#main#hpaneScriptSequent#set_position script_w;
426         (* source_view *)
427       ignore(source_view#connect#after#paste_clipboard 
428         ~callback:(fun () -> (MatitaScript.instance ())#clean_dirty_lock))
429     
430     method loadScript file =       
431       let script = MatitaScript.instance () in
432       script#reset (); 
433       script#assignFileName file;
434       if not (Sys.file_exists file) then
435         begin
436           let oc = open_out file in
437           let template = MatitaMisc.input_file BuildTimeConf.script_template in 
438           output_string oc template;
439           close_out oc
440         end;
441       script#loadFromFile ();
442       console#message ("'"^file^"' loaded.");
443       self#_enableSaveTo file
444       
445     method setStar name b =
446       let l = main#scriptLabel in
447       if b then
448         l#set_text (name ^  " *")
449       else
450         l#set_text (name)
451         
452     method private _enableSaveTo file =
453       script_fname <- Some file;
454       self#main#saveMenuItem#misc#set_sensitive true
455         
456
457     method console = console
458     method sourceView: GSourceView.source_view = (source_view: GSourceView.source_view)
459     method about = about
460     method fileSel = fileSel
461     method findRepl = findRepl
462     method main = main
463
464     method newBrowserWin () =
465       object (self)
466         inherit browserWin ()
467         val combo = GEdit.combo_box_entry ()
468         initializer
469           self#check_widgets ();
470           let combo_widget = combo#coerce in
471           uriHBox#pack ~from:`END ~fill:true ~expand:true combo_widget;
472           combo#entry#misc#grab_focus ()
473         method browserUri = combo
474       end
475
476     method newUriDialog () =
477       let dialog = new uriChoiceDialog () in
478       dialog#check_widgets ();
479       dialog
480
481     method newInterpDialog () =
482       let dialog = new interpChoiceDialog () in
483       dialog#check_widgets ();
484       dialog
485
486     method newConfirmationDialog () =
487       let dialog = new confirmationDialog () in
488       dialog#check_widgets ();
489       dialog
490
491     method newEmptyDialog () =
492       let dialog = new emptyDialog () in
493       dialog#check_widgets ();
494       dialog
495
496     method private addKeyBinding key callback =
497       List.iter (fun evbox -> add_key_binding key callback evbox)
498         keyBindingBoxes
499
500     method setQuitCallback callback =
501       ignore (main#quitMenuItem#connect#activate callback);
502       ignore (main#toplevel#event#connect#delete 
503         (fun _ -> callback ();true));
504       self#addKeyBinding GdkKeysyms._q callback
505
506     method chooseFile ?(ok_not_exists = false) () =
507       _ok_not_exists <- ok_not_exists;
508       fileSel#fileSelectionWin#show ();
509       GtkThread.main ();
510       chosen_file
511
512     method askText ?(title = "") ?(msg = "") () =
513       let dialog = new textDialog () in
514       dialog#textDialog#set_title title;
515       dialog#textDialogLabel#set_label msg;
516       let text = ref None in
517       let return v =
518         text := v;
519         dialog#textDialog#destroy ();
520         GMain.Main.quit ()
521       in
522       ignore (dialog#textDialog#event#connect#delete (fun _ -> true));
523       connect_button dialog#textDialogCancelButton (fun _ -> return None);
524       connect_button dialog#textDialogOkButton (fun _ ->
525         let text = dialog#textDialogTextView#buffer#get_text () in
526         return (Some text));
527       dialog#textDialog#show ();
528       GtkThread.main ();
529       !text
530
531     method private updateFontSize () =
532       self#sourceView#misc#modify_font_by_name
533         (sprintf "%s %d" BuildTimeConf.script_font font_size)
534
535     method increaseFontSize () =
536       font_size <- font_size + 1;
537       self#updateFontSize ()
538
539     method decreaseFontSize () =
540       font_size <- font_size - 1;
541       self#updateFontSize ()
542
543     method resetFontSize () =
544       font_size <- default_font_size;
545       self#updateFontSize ()
546
547   end
548
549 let gui () = 
550   let g = new gui () in
551   gui_instance := Some g;
552   g
553   
554 let instance = singleton gui
555
556 let non p x = not (p x)
557
558 (* this is a shit and should be changed :-{ *)
559 let interactive_uri_choice
560   ?(selection_mode:[`SINGLE|`MULTIPLE] = `MULTIPLE) ?(title = "")
561   ?(msg = "") ?(nonvars_button = false) ?(hide_uri_entry=false) 
562   ?(hide_try=false) ?(ok_label="_Auto") ?(ok_action:[`SELECT|`AUTO] = `AUTO) 
563   ?copy_cb ()
564   ~id uris
565 =
566   let gui = instance () in
567   let nonvars_uris = lazy (List.filter (non UriManager.uri_is_var) uris) in
568   if (selection_mode <> `SINGLE) &&
569     (Helm_registry.get_bool "matita.auto_disambiguation")
570   then
571     Lazy.force nonvars_uris
572   else begin
573     let dialog = gui#newUriDialog () in
574     if hide_uri_entry then
575       dialog#uriEntryHBox#misc#hide ();
576     if hide_try then
577       begin
578       dialog#uriChoiceSelectedButton#misc#hide ();
579       dialog#uriChoiceConstantsButton#misc#hide ();
580       end;
581     dialog#okLabel#set_label ok_label;  
582     dialog#uriChoiceTreeView#selection#set_mode
583       (selection_mode :> Gtk.Tags.selection_mode);
584     let model = new stringListModel dialog#uriChoiceTreeView in
585     let choices = ref None in
586     let nonvars = ref false in
587     (match copy_cb with
588     | None -> ()
589     | Some cb ->
590         dialog#copyButton#misc#show ();
591         connect_button dialog#copyButton 
592         (fun _ ->
593           match model#easy_selection () with
594           | [u] -> (cb u)
595           | _ -> ()));
596     dialog#uriChoiceDialog#set_title title;
597     dialog#uriChoiceLabel#set_text msg;
598     List.iter model#easy_append (List.map UriManager.string_of_uri uris);
599     dialog#uriChoiceConstantsButton#misc#set_sensitive nonvars_button;
600     let return v =
601       choices := v;
602       dialog#uriChoiceDialog#destroy ();
603       GMain.Main.quit ()
604     in
605     ignore (dialog#uriChoiceDialog#event#connect#delete (fun _ -> true));
606     connect_button dialog#uriChoiceConstantsButton (fun _ ->
607       return (Some (Lazy.force nonvars_uris)));
608     if ok_action = `AUTO then
609       connect_button dialog#uriChoiceAutoButton (fun _ ->
610         Helm_registry.set_bool "matita.auto_disambiguation" true;
611         return (Some (Lazy.force nonvars_uris)))
612     else
613       connect_button dialog#uriChoiceAutoButton (fun _ ->
614         match model#easy_selection () with
615         | [] -> ()
616         | uris -> return (Some (List.map UriManager.uri_of_string uris)));
617     connect_button dialog#uriChoiceSelectedButton (fun _ ->
618       match model#easy_selection () with
619       | [] -> ()
620       | uris -> return (Some (List.map UriManager.uri_of_string uris)));
621     connect_button dialog#uriChoiceAbortButton (fun _ -> return None);
622     dialog#uriChoiceDialog#show ();
623     GtkThread.main ();
624     (match !choices with 
625     | None -> raise MatitaTypes.Cancel
626     | Some uris -> uris)
627   end
628
629 class interpModel =
630   let cols = new GTree.column_list in
631   let id_col = cols#add Gobject.Data.string in
632   let dsc_col = cols#add Gobject.Data.string in
633   let interp_no_col = cols#add Gobject.Data.int in
634   let tree_store = GTree.tree_store cols in
635   let id_renderer = GTree.cell_renderer_text [], ["text", id_col] in
636   let dsc_renderer = GTree.cell_renderer_text [], ["text", dsc_col] in
637   let id_view_col = GTree.view_column ~renderer:id_renderer () in
638   let dsc_view_col = GTree.view_column ~renderer:dsc_renderer () in
639   fun tree_view choices ->
640     object
641       initializer
642         tree_view#set_model (Some (tree_store :> GTree.model));
643         ignore (tree_view#append_column id_view_col);
644         ignore (tree_view#append_column dsc_view_col);
645         let name_of_interp =
646           (* try to find a reasonable name for an interpretation *)
647           let idx = ref 0 in
648           fun interp ->
649             try
650               List.assoc "0" interp
651             with Not_found ->
652               incr idx; string_of_int !idx
653         in
654         tree_store#clear ();
655         let idx = ref ~-1 in
656         List.iter
657           (fun interp ->
658             incr idx;
659             let interp_row = tree_store#append () in
660             tree_store#set ~row:interp_row ~column:id_col
661               (name_of_interp interp);
662             tree_store#set ~row:interp_row ~column:interp_no_col !idx;
663             List.iter
664               (fun (id, dsc) ->
665                 let row = tree_store#append ~parent:interp_row () in
666                 tree_store#set ~row ~column:id_col id;
667                 tree_store#set ~row ~column:dsc_col dsc;
668                 tree_store#set ~row ~column:interp_no_col !idx)
669               interp)
670           choices
671
672       method get_interp_no tree_path =
673         let iter = tree_store#get_iter tree_path in
674         tree_store#get ~row:iter ~column:interp_no_col
675     end
676
677 let interactive_interp_choice () choices =
678   let gui = instance () in
679   assert (choices <> []);
680   let dialog = gui#newInterpDialog () in
681   let model = new interpModel dialog#interpChoiceTreeView choices in
682   let interp_len = List.length (List.hd choices) in
683   dialog#interpChoiceDialog#set_title "Interpretation choice";
684   dialog#interpChoiceDialogLabel#set_label "Choose an interpretation:";
685   let interp_no = ref None in
686   let return _ =
687     dialog#interpChoiceDialog#destroy ();
688     GMain.Main.quit ()
689   in
690   let fail _ = interp_no := None; return () in
691   ignore (dialog#interpChoiceDialog#event#connect#delete (fun _ -> true));
692   connect_button dialog#interpChoiceOkButton (fun _ ->
693     match !interp_no with None -> () | Some _ -> return ());
694   connect_button dialog#interpChoiceCancelButton fail;
695   ignore (dialog#interpChoiceTreeView#connect#row_activated (fun path _ ->
696     interp_no := Some (model#get_interp_no path);
697     return ()));
698   let selection = dialog#interpChoiceTreeView#selection in
699   ignore (selection#connect#changed (fun _ ->
700     match selection#get_selected_rows with
701     | [path] ->
702         MatitaLog.debug (sprintf "selection: %d" (model#get_interp_no path));
703         interp_no := Some (model#get_interp_no path)
704     | _ -> assert false));
705   dialog#interpChoiceDialog#show ();
706   GtkThread.main ();
707   (match !interp_no with Some row -> [row] | _ -> raise MatitaTypes.Cancel)
708