1 (* Copyright (C) 2004-2005, 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/
28 open MatitaGeneratedGui
32 let gui_instance = ref None ;;
34 class type browserWin =
35 (* this class exists only because GEdit.combo_box_entry is not supported by
36 * lablgladecc :-(((( *)
38 inherit MatitaGeneratedGui.browserWin
39 method browserUri: GEdit.combo_box_entry
42 class console ~(buffer: GText.buffer) () =
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
53 buffer#delete ~start:buffer#start_iter ~stop:buffer#end_iter
54 method log_callback (tag: MatitaLog.log_tag) s =
56 | `Debug -> self#debug (s ^ "\n")
57 | `Error -> self#error (s ^ "\n")
58 | `Message -> self#message (s ^ "\n")
59 | `Warning -> self#warning (s ^ "\n")
62 let clean_current_baseuri status =
64 let baseuri = MatitaTypes.get_string_option status "baseuri" in
65 MatitacleanLib.clean_baseuris [baseuri]
66 with MatitaTypes.Option_error _ -> ()
68 let ask_and_save_moo_if_needed parent fname status =
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
76 MatitaMisc.obj_file_of_script fname
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>"
90 | `CANCEL -> raise MatitaTypes.Cancel
95 clean_current_baseuri status
98 clean_current_baseuri status
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>")
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 ]
116 let console = new console ~buffer:main#logTextView#buffer () in
117 let (source_view: GSourceView.source_view) =
118 GSourceView.source_view
120 ~insert_spaces_instead_of_tabs:true ~tabs_width:2
121 ~margin:80 ~show_margin:true
123 ~packing:main#scriptScrolledWin#add
126 let default_font_size =
127 Helm_registry.get_opt_default Helm_registry.int
128 ~default:BuildTimeConf.default_font_size "matita.font_size"
130 let source_buffer = source_view#source_buffer in
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
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]);
143 List.iter (* global key bindings *)
144 (fun (key, callback) -> self#addKeyBinding key callback)
147 toggle_win ~check:main#showProofMenuItem proof#proofWin;
149 toggle_win ~check:main#showCheckMenuItem check#checkWin;
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);
161 let show_find_Repl () =
162 findRepl#toplevel#misc#show ();
163 findRepl#toplevel#misc#grab_focus ()
165 let hide_find_Repl () = findRepl#toplevel#misc#hide () in
167 let highlight start end_ =
168 source_buffer#move_mark `INSERT ~where:start;
169 source_buffer#move_mark `SEL_BOUND ~where:end_
171 let text = findRepl#findEntry#text in
172 let iter = source_buffer#get_iter `SEL_BOUND in
173 match iter#forward_search text with
175 (match source_buffer#start_iter#forward_search text with
177 | Some (start,end_) -> highlight start end_)
178 | Some (start,end_) -> highlight start end_
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
186 ignore(source_buffer#delete_selection ());
187 source_buffer#insert text
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 ->
203 fileSel#fileSelectionWin#misc#hide ();
208 let fname = fileSel#fileSelectionWin#filename in
209 if Sys.file_exists fname then
210 (if is_regular fname then return (Some fname))
212 (if _ok_not_exists then return (Some fname))
213 | `CANCEL -> return None
215 | `DELETE_EVENT -> return None));
217 List.iter (fun w -> w#misc#set_sensitive false) [ main#saveMenuItem ];
218 main#helpMenu#set_right_justified true;
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);
225 let module A = TacticAst in
226 let hole = CicAst.UserInput in
227 let loc = CicAst.dummy_floc in
229 if (MatitaScript.instance ())#onGoingProof () then
230 (MatitaScript.instance ())#advance
231 ~statement:("\n" ^ TacticAstPp.pp_tactical (A.Tactic (loc, ast))) ()
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)
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
261 not (Hr.get_opt_default Hr.bool ~default:false "matita.tactics_bar")
263 self#main#tacticsBarMenuItem#set_active false;
264 MatitaGtkMisc.toggle_callback
266 | true -> self#main#toplevel#fullscreen ()
267 | false -> self#main#toplevel#unfullscreen ())
268 ~check:self#main#fullscreenMenuItem;
269 self#main#fullscreenMenuItem#set_active false;
271 MatitaLog.set_log_callback self#console#log_callback;
272 GtkSignal.user_handler :=
273 (fun exn -> MatitaLog.error (MatitaExcPp.to_string exn));
276 match GSourceView.source_language_from_file BuildTimeConf.lang_file with
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
284 let s () = MatitaScript.instance () in
286 script_fname <- None;
287 self#main#saveMenuItem#misc#set_sensitive false
289 let saveAsScript () =
291 match self#chooseFile ~ok_not_exists:true () with
293 script#assignFileName f;
294 script#saveToFile ();
295 console#message ("'"^f^"' saved.\n");
300 match script_fname with
301 | None -> saveAsScript ()
303 (s ())#assignFileName f;
304 (s ())#saveToFile ();
305 console#message ("'"^f^"' saved.\n");
309 let status = script#status in
311 if source_view#buffer#modified then
313 match ask_unsaved main#toplevel with
314 | `YES -> saveScript ()
316 | `CANCEL -> raise MatitaTypes.Cancel
318 (match script_fname with
321 ask_and_save_moo_if_needed main#toplevel fname status);
322 match self#chooseFile () with
325 script#assignFileName f;
326 script#loadFromFile ();
327 console#message ("'"^f^"' loaded.\n");
330 with MatitaTypes.Cancel -> ()
339 source_buffer#place_cursor
340 (source_buffer#get_iter_at_mark (`NAME "locked"))
343 main#buttonsToolbar#misc#set_sensitive false;
344 source_view#set_editable false
347 main#buttonsToolbar#misc#set_sensitive true;
348 source_view#set_editable true
350 let advance _ = (MatitaScript.instance ())#advance (); cursor () in
351 let retract _ = (MatitaScript.instance ())#retract (); cursor () in
352 let top _ = (MatitaScript.instance ())#goto `Top (); cursor () in
353 let bottom _ = (MatitaScript.instance ())#goto `Bottom (); cursor () in
354 let jump _ = (MatitaScript.instance ())#goto `Cursor (); cursor () in
358 try f ();unlock_world () with exc -> unlock_world (); raise exc
360 let advance = locker advance in
361 let retract = locker retract in
362 let top = locker top in
363 let bottom = locker bottom in
364 let jump = locker jump in
365 let connect_key sym f =
366 connect_key self#main#mainWinEventBox#event
367 ~modifiers:[`CONTROL] ~stop:true sym f;
368 connect_key self#sourceView#event
369 ~modifiers:[`CONTROL] ~stop:true sym f
372 self#setQuitCallback (fun () ->
373 let status = (MatitaScript.instance ())#status in
374 if source_view#buffer#modified then
376 let rc = ask_unsaved main#toplevel in
379 | `YES -> saveScript ();
380 if not source_view#buffer#modified then
382 (match script_fname with
385 ask_and_save_moo_if_needed
386 main#toplevel fname status);
389 | `NO -> GMain.Main.quit ()
390 | `CANCEL -> raise MatitaTypes.Cancel
391 with MatitaTypes.Cancel -> ()
395 (match script_fname with
396 | None -> clean_current_baseuri status; GMain.Main.quit ()
399 ask_and_save_moo_if_needed main#toplevel fname status;
401 with MatitaTypes.Cancel -> ())
403 connect_button self#main#scriptAdvanceButton advance;
404 connect_button self#main#scriptRetractButton retract;
405 connect_button self#main#scriptTopButton top;
406 connect_button self#main#scriptBottomButton bottom;
407 connect_key GdkKeysyms._Down advance;
408 connect_key GdkKeysyms._Up retract;
409 connect_key GdkKeysyms._Home top;
410 connect_key GdkKeysyms._End bottom;
411 connect_button self#main#scriptJumpButton jump;
412 connect_menu_item self#main#openMenuItem loadScript;
413 connect_menu_item self#main#saveMenuItem saveScript;
414 connect_menu_item self#main#saveAsMenuItem saveAsScript;
415 connect_menu_item self#main#newMenuItem newScript;
416 connect_key GdkKeysyms._period
418 source_buffer#insert ~iter:(source_buffer#get_iter_at_mark `INSERT)
421 connect_key GdkKeysyms._Return
423 source_buffer#insert ~iter:(source_buffer#get_iter_at_mark `INSERT)
426 (* script monospace font stuff *)
427 self#updateFontSize ();
429 self#main#debugMenu#misc#hide ();
431 self#main#hintLowImage#set_file (image_path "matita-bulb-low.png");
432 self#main#hintMediumImage#set_file (image_path "matita-bulb-medium.png");
433 self#main#hintHighImage#set_file (image_path "matita-bulb-high.png");
435 self#sourceView#misc#grab_focus ();
436 (* main win dimension *)
437 let width = Gdk.Screen.width () in
438 let height = Gdk.Screen.height () in
439 let main_w = width * 90 / 100 in
440 let main_h = height * 80 / 100 in
441 let script_w = main_w * 6 / 10 in
442 self#main#toplevel#resize ~width:main_w ~height:main_h;
443 self#main#hpaneScriptSequent#set_position script_w;
445 ignore(source_view#connect#after#paste_clipboard
446 ~callback:(fun () -> (MatitaScript.instance ())#clean_dirty_lock))
448 method loadScript file =
449 let script = MatitaScript.instance () in
451 script#assignFileName file;
452 if not (Sys.file_exists file) then
454 let oc = open_out file in
455 let template = MatitaMisc.input_file BuildTimeConf.script_template in
456 output_string oc template;
459 script#loadFromFile ();
460 console#message ("'"^file^"' loaded.");
461 self#_enableSaveTo file
463 method setStar name b =
464 let l = main#scriptLabel in
466 l#set_text (name ^ " *")
470 method private _enableSaveTo file =
471 script_fname <- Some file;
472 self#main#saveMenuItem#misc#set_sensitive true
475 method console = console
476 method sourceView: GSourceView.source_view = (source_view: GSourceView.source_view)
478 method fileSel = fileSel
479 method findRepl = findRepl
482 method newBrowserWin () =
484 inherit browserWin ()
485 val combo = GEdit.combo_box_entry ()
487 self#check_widgets ();
488 let combo_widget = combo#coerce in
489 uriHBox#pack ~from:`END ~fill:true ~expand:true combo_widget;
490 combo#entry#misc#grab_focus ()
491 method browserUri = combo
494 method newUriDialog () =
495 let dialog = new uriChoiceDialog () in
496 dialog#check_widgets ();
499 method newInterpDialog () =
500 let dialog = new interpChoiceDialog () in
501 dialog#check_widgets ();
504 method newConfirmationDialog () =
505 let dialog = new confirmationDialog () in
506 dialog#check_widgets ();
509 method newEmptyDialog () =
510 let dialog = new emptyDialog () in
511 dialog#check_widgets ();
514 method private addKeyBinding key callback =
515 List.iter (fun evbox -> add_key_binding key callback evbox)
518 method setQuitCallback callback =
519 ignore (main#quitMenuItem#connect#activate callback);
520 ignore (main#toplevel#event#connect#delete
521 (fun _ -> callback ();true));
522 self#addKeyBinding GdkKeysyms._q callback
524 method chooseFile ?(ok_not_exists = false) () =
525 _ok_not_exists <- ok_not_exists;
526 fileSel#fileSelectionWin#show ();
530 method askText ?(title = "") ?(msg = "") () =
531 let dialog = new textDialog () in
532 dialog#textDialog#set_title title;
533 dialog#textDialogLabel#set_label msg;
534 let text = ref None in
537 dialog#textDialog#destroy ();
540 ignore (dialog#textDialog#event#connect#delete (fun _ -> true));
541 connect_button dialog#textDialogCancelButton (fun _ -> return None);
542 connect_button dialog#textDialogOkButton (fun _ ->
543 let text = dialog#textDialogTextView#buffer#get_text () in
545 dialog#textDialog#show ();
549 method private updateFontSize () =
550 self#sourceView#misc#modify_font_by_name
551 (sprintf "%s %d" BuildTimeConf.script_font font_size)
553 method increaseFontSize () =
554 font_size <- font_size + 1;
555 self#updateFontSize ()
557 method decreaseFontSize () =
558 font_size <- font_size - 1;
559 self#updateFontSize ()
561 method resetFontSize () =
562 font_size <- default_font_size;
563 self#updateFontSize ()
568 let g = new gui () in
569 gui_instance := Some g;
572 let instance = singleton gui
574 let non p x = not (p x)
576 (* this is a shit and should be changed :-{ *)
577 let interactive_uri_choice
578 ?(selection_mode:[`SINGLE|`MULTIPLE] = `MULTIPLE) ?(title = "")
579 ?(msg = "") ?(nonvars_button = false) ?(hide_uri_entry=false)
580 ?(hide_try=false) ?(ok_label="_Auto") ?(ok_action:[`SELECT|`AUTO] = `AUTO)
584 let gui = instance () in
585 let nonvars_uris = lazy (List.filter (non UriManager.uri_is_var) uris) in
586 if (selection_mode <> `SINGLE) &&
587 (Helm_registry.get_bool "matita.auto_disambiguation")
589 Lazy.force nonvars_uris
591 let dialog = gui#newUriDialog () in
592 if hide_uri_entry then
593 dialog#uriEntryHBox#misc#hide ();
596 dialog#uriChoiceSelectedButton#misc#hide ();
597 dialog#uriChoiceConstantsButton#misc#hide ();
599 dialog#okLabel#set_label ok_label;
600 dialog#uriChoiceTreeView#selection#set_mode
601 (selection_mode :> Gtk.Tags.selection_mode);
602 let model = new stringListModel dialog#uriChoiceTreeView in
603 let choices = ref None in
604 let nonvars = ref false in
608 dialog#copyButton#misc#show ();
609 connect_button dialog#copyButton
611 match model#easy_selection () with
614 dialog#uriChoiceDialog#set_title title;
615 dialog#uriChoiceLabel#set_text msg;
616 List.iter model#easy_append (List.map UriManager.string_of_uri uris);
617 dialog#uriChoiceConstantsButton#misc#set_sensitive nonvars_button;
620 dialog#uriChoiceDialog#destroy ();
623 ignore (dialog#uriChoiceDialog#event#connect#delete (fun _ -> true));
624 connect_button dialog#uriChoiceConstantsButton (fun _ ->
625 return (Some (Lazy.force nonvars_uris)));
626 if ok_action = `AUTO then
627 connect_button dialog#uriChoiceAutoButton (fun _ ->
628 Helm_registry.set_bool "matita.auto_disambiguation" true;
629 return (Some (Lazy.force nonvars_uris)))
631 connect_button dialog#uriChoiceAutoButton (fun _ ->
632 match model#easy_selection () with
634 | uris -> return (Some (List.map UriManager.uri_of_string uris)));
635 connect_button dialog#uriChoiceSelectedButton (fun _ ->
636 match model#easy_selection () with
638 | uris -> return (Some (List.map UriManager.uri_of_string uris)));
639 connect_button dialog#uriChoiceAbortButton (fun _ -> return None);
640 dialog#uriChoiceDialog#show ();
643 | None -> raise MatitaTypes.Cancel
648 let cols = new GTree.column_list in
649 let id_col = cols#add Gobject.Data.string in
650 let dsc_col = cols#add Gobject.Data.string in
651 let interp_no_col = cols#add Gobject.Data.int in
652 let tree_store = GTree.tree_store cols in
653 let id_renderer = GTree.cell_renderer_text [], ["text", id_col] in
654 let dsc_renderer = GTree.cell_renderer_text [], ["text", dsc_col] in
655 let id_view_col = GTree.view_column ~renderer:id_renderer () in
656 let dsc_view_col = GTree.view_column ~renderer:dsc_renderer () in
657 fun tree_view choices ->
660 tree_view#set_model (Some (tree_store :> GTree.model));
661 ignore (tree_view#append_column id_view_col);
662 ignore (tree_view#append_column dsc_view_col);
664 (* try to find a reasonable name for an interpretation *)
668 List.assoc "0" interp
670 incr idx; string_of_int !idx
677 let interp_row = tree_store#append () in
678 tree_store#set ~row:interp_row ~column:id_col
679 (name_of_interp interp);
680 tree_store#set ~row:interp_row ~column:interp_no_col !idx;
683 let row = tree_store#append ~parent:interp_row () in
684 tree_store#set ~row ~column:id_col id;
685 tree_store#set ~row ~column:dsc_col dsc;
686 tree_store#set ~row ~column:interp_no_col !idx)
690 method get_interp_no tree_path =
691 let iter = tree_store#get_iter tree_path in
692 tree_store#get ~row:iter ~column:interp_no_col
695 let interactive_interp_choice () choices =
696 let gui = instance () in
697 assert (choices <> []);
698 let dialog = gui#newInterpDialog () in
699 let model = new interpModel dialog#interpChoiceTreeView choices in
700 let interp_len = List.length (List.hd choices) in
701 dialog#interpChoiceDialog#set_title "Interpretation choice";
702 dialog#interpChoiceDialogLabel#set_label "Choose an interpretation:";
703 let interp_no = ref None in
705 dialog#interpChoiceDialog#destroy ();
708 let fail _ = interp_no := None; return () in
709 ignore (dialog#interpChoiceDialog#event#connect#delete (fun _ -> true));
710 connect_button dialog#interpChoiceOkButton (fun _ ->
711 match !interp_no with None -> () | Some _ -> return ());
712 connect_button dialog#interpChoiceCancelButton fail;
713 ignore (dialog#interpChoiceTreeView#connect#row_activated (fun path _ ->
714 interp_no := Some (model#get_interp_no path);
716 let selection = dialog#interpChoiceTreeView#selection in
717 ignore (selection#connect#changed (fun _ ->
718 match selection#get_selected_rows with
720 MatitaLog.debug (sprintf "selection: %d" (model#get_interp_no path));
721 interp_no := Some (model#get_interp_no path)
722 | _ -> assert false));
723 dialog#interpChoiceDialog#show ();
725 (match !interp_no with Some row -> [row] | _ -> raise MatitaTypes.Cancel)