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"))
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
354 self#setQuitCallback (fun () ->
355 let status = (MatitaScript.instance ())#status in
356 if source_view#buffer#modified then
358 let rc = ask_unsaved main#toplevel in
361 | `YES -> saveScript ();
362 if not source_view#buffer#modified then
364 (match script_fname with
367 ask_and_save_moo_if_needed
368 main#toplevel fname status);
371 | `NO -> GMain.Main.quit ()
372 | `CANCEL -> raise MatitaTypes.Cancel
373 with MatitaTypes.Cancel -> ()
377 (match script_fname with
378 | None -> clean_current_baseuri status; GMain.Main.quit ()
381 ask_and_save_moo_if_needed main#toplevel fname status;
383 with MatitaTypes.Cancel -> ())
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
400 source_buffer#insert ~iter:(source_buffer#get_iter_at_mark `INSERT)
403 connect_key GdkKeysyms._Return
405 source_buffer#insert ~iter:(source_buffer#get_iter_at_mark `INSERT)
408 (* script monospace font stuff *)
409 self#updateFontSize ();
411 self#main#debugMenu#misc#hide ();
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");
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;
427 ignore(source_view#connect#after#paste_clipboard
428 ~callback:(fun () -> (MatitaScript.instance ())#clean_dirty_lock))
430 method loadScript file =
431 let script = MatitaScript.instance () in
433 script#assignFileName file;
434 if not (Sys.file_exists file) then
436 let oc = open_out file in
437 let template = MatitaMisc.input_file BuildTimeConf.script_template in
438 output_string oc template;
441 script#loadFromFile ();
442 console#message ("'"^file^"' loaded.");
443 self#_enableSaveTo file
445 method setStar name b =
446 let l = main#scriptLabel in
448 l#set_text (name ^ " *")
452 method private _enableSaveTo file =
453 script_fname <- Some file;
454 self#main#saveMenuItem#misc#set_sensitive true
457 method console = console
458 method sourceView: GSourceView.source_view = (source_view: GSourceView.source_view)
460 method fileSel = fileSel
461 method findRepl = findRepl
464 method newBrowserWin () =
466 inherit browserWin ()
467 val combo = GEdit.combo_box_entry ()
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
476 method newUriDialog () =
477 let dialog = new uriChoiceDialog () in
478 dialog#check_widgets ();
481 method newInterpDialog () =
482 let dialog = new interpChoiceDialog () in
483 dialog#check_widgets ();
486 method newConfirmationDialog () =
487 let dialog = new confirmationDialog () in
488 dialog#check_widgets ();
491 method newEmptyDialog () =
492 let dialog = new emptyDialog () in
493 dialog#check_widgets ();
496 method private addKeyBinding key callback =
497 List.iter (fun evbox -> add_key_binding key callback evbox)
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
506 method chooseFile ?(ok_not_exists = false) () =
507 _ok_not_exists <- ok_not_exists;
508 fileSel#fileSelectionWin#show ();
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
519 dialog#textDialog#destroy ();
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
527 dialog#textDialog#show ();
531 method private updateFontSize () =
532 self#sourceView#misc#modify_font_by_name
533 (sprintf "%s %d" BuildTimeConf.script_font font_size)
535 method increaseFontSize () =
536 font_size <- font_size + 1;
537 self#updateFontSize ()
539 method decreaseFontSize () =
540 font_size <- font_size - 1;
541 self#updateFontSize ()
543 method resetFontSize () =
544 font_size <- default_font_size;
545 self#updateFontSize ()
550 let g = new gui () in
551 gui_instance := Some g;
554 let instance = singleton gui
556 let non p x = not (p x)
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)
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")
571 Lazy.force nonvars_uris
573 let dialog = gui#newUriDialog () in
574 if hide_uri_entry then
575 dialog#uriEntryHBox#misc#hide ();
578 dialog#uriChoiceSelectedButton#misc#hide ();
579 dialog#uriChoiceConstantsButton#misc#hide ();
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
590 dialog#copyButton#misc#show ();
591 connect_button dialog#copyButton
593 match model#easy_selection () with
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;
602 dialog#uriChoiceDialog#destroy ();
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)))
613 connect_button dialog#uriChoiceAutoButton (fun _ ->
614 match model#easy_selection () with
616 | uris -> return (Some (List.map UriManager.uri_of_string uris)));
617 connect_button dialog#uriChoiceSelectedButton (fun _ ->
618 match model#easy_selection () with
620 | uris -> return (Some (List.map UriManager.uri_of_string uris)));
621 connect_button dialog#uriChoiceAbortButton (fun _ -> return None);
622 dialog#uriChoiceDialog#show ();
625 | None -> raise MatitaTypes.Cancel
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 ->
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);
646 (* try to find a reasonable name for an interpretation *)
650 List.assoc "0" interp
652 incr idx; string_of_int !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;
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)
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
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
687 dialog#interpChoiceDialog#destroy ();
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);
698 let selection = dialog#interpChoiceTreeView#selection in
699 ignore (selection#connect#changed (fun _ ->
700 match selection#get_selected_rows with
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 ();
707 (match !interp_no with Some row -> [row] | _ -> raise MatitaTypes.Cancel)