Debian Codegraph @

ide/rocqide/wg_Command.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Preferences
12
13 class command_window name rocqtop rocqops router sid =
14 let frame = Wg_Detachable.detachable
15 ~title:(Printf.sprintf "Query pane (%s)" name) () in
16 let _ = frame#hide in
17 let _ = GtkData.AccelGroup.create () in
18 let notebook =
19 GPack.notebook ~height:200 ~scrollable:true ~packing:frame#add () in
20 let _ = frame#connect#attached ~callback:(fun _ ->
21 notebook#misc#set_size_request ~height:200 ()) in
22 let _ = frame#connect#detached ~callback:(fun _ ->
23 notebook#misc#set_size_request ~width:600 ~height:500 ();
24 notebook#misc#grab_focus ()) in
25
26 let route_id =
27 let r = ref 0 in
28 fun () -> incr r; !r in
29
30 object(self)
31 val frame = frame
32
33 val notebook = notebook
34
35 (* We need access to rocqops in order to place queries in the proper
36 document stint. This should remove access from this module to the
37 low-level Rocq one. *)
38 val rocqops = rocqops
39
40 method pack_in (f : GObj.widget -> unit) = f frame#coerce
41
42 val mutable new_page : GObj.widget = (GMisc.label ())#coerce
43
44 val mutable views = []
45
46 method private new_page_maker =
47 let page = notebook#append_page
48 (GMisc.label ~text:"No query" ())#coerce in
49 let page = notebook#get_nth_page page in
50 let b = GButton.button () in
51 b#add (Ideutils.stock_to_widget ~size:(`CUSTOM(12,12)) `NEW);
52 ignore(b#connect#clicked ~callback:self#new_query);
53 notebook#set_page ~tab_label:b#coerce page;
54 new_page <- page
55
56 method new_query ?command ?term () = self#new_query_aux ?command ?term ()
57
58 method private new_query_aux ?command ?term ?(grab_now=true) () =
59 let frame = GBin.frame ~shadow_type:`NONE () in
60 ignore(notebook#insert_page ~pos:(notebook#page_num new_page) frame#coerce);
61 let route_id = route_id () in
62 let new_tab_lbl text =
63 let hbox = GPack.hbox ~homogeneous:false () in
64 ignore(GMisc.label ~width:100 ~ellipsize:`END ~text ~packing:hbox#pack());
65 let b = GButton.button ~packing:hbox#pack () in
66 ignore(b#connect#clicked ~callback:(fun () ->
67 router#delete_route route_id;
68 views <-
69 List.filter (fun (f,_,_) -> f#get_oid <> frame#coerce#get_oid) views;
70 notebook#remove_page (notebook#page_num frame#coerce)));
71 b#add (Ideutils.stock_to_widget ~size:(`CUSTOM(12,10)) `CLOSE);
72 hbox#coerce in
73 notebook#set_page ~tab_label:(new_tab_lbl "New query") frame#coerce;
74 notebook#goto_page (notebook#page_num frame#coerce);
75 let vbox = GPack.vbox ~homogeneous:false ~packing:frame#add () in
76 let combo, entry, ok_b =
77 let bar =
78 GButton.toolbar ~style:`ICONS ~packing:(vbox#pack ~expand:false) () in
79 let bar_add ~expand w =
80 let item = GButton.tool_item ~expand () in
81 item#add w#coerce;
82 bar#insert item in
83 let combo, _ =
84 GEdit.combo_box_entry_text ~strings:Rocq_commands.state_preserving () in
85 combo#entry#set_text "Search";
86 let entry = GEdit.entry () in
87 entry#misc#set_can_default true;
88 let ok_b = GButton.button () in
89 ok_b#add (Ideutils.stock_to_widget `OK);
90 bar_add ~expand:false combo;
91 bar_add ~expand:true entry;
92 bar_add ~expand:false ok_b;
93 combo, entry, ok_b in
94 let r_bin =
95 GBin.scrolled_window
96 ~vpolicy:`AUTOMATIC
97 ~hpolicy:`AUTOMATIC
98 ~packing:(vbox#pack ~fill:true ~expand:true) () in
99 let result = Wg_MessageView.message_view sid in
100 router#register_route route_id result;
101 r_bin#add_with_viewport (result :> GObj.widget);
102 views <- (frame#coerce, result, combo#entry) :: views;
103 (* FIXME: handle this using CSS *)
104 (* let cb clr = result#misc#modify_bg [`NORMAL, `NAME clr] in *)
105 (* let _ = background_color#connect#changed ~callback:cb in *)
106 (* let _ = result#misc#connect#realize ~callback:(fun () -> cb background_color#get) in *)
107 let cb ft = result#misc#modify_font (GPango.font_description_from_string ft) in
108 stick text_font result cb;
109 result#misc#set_can_focus true; (* false causes problems for selection *)
110 let callback () =
111 let com = combo#entry#text in
112 let arg = entry#text in
113 if Str.string_match (Str.regexp "^ *$") (com^arg) 0 then () else
114 let phrase =
115 if Str.string_match (Str.regexp "\\. *$") com 0 then com
116 else com ^ " " ^ arg ^" . "
117 in
118 let process =
119 let next = function
120 | Interface.Fail (_, _, err) ->
121 let err = Ideutils.validate err in
122 result#set err;
123 notebook#set_page ~tab_label:(new_tab_lbl "Error") frame#coerce;
124 RocqDriver.return ()
125 | Interface.Good () ->
126 notebook#set_page ~tab_label:(new_tab_lbl arg) frame#coerce;
127 RocqDriver.return ()
128 in
129 rocqops#raw_rocq_query ~route_id ~next phrase
130 in
131 result#set (Pp.str ("Result for command " ^ phrase ^ ":\n"));
132 ignore @@ RocqDriver.try_grab rocqtop process ignore
133 in
134 ignore (combo#entry#connect#activate ~callback);
135 ignore (ok_b#connect#clicked ~callback);
136 begin match command with
137 | None -> ()
138 | Some c -> combo#entry#set_text c
139 end;
140 begin match term with
141 | None -> ()
142 | Some t -> entry#set_text t
143 end;
144 combo#entry#misc#grab_focus ();
145 if grab_now then entry#misc#grab_default ();
146 ignore (entry#connect#activate ~callback);
147 ignore (combo#entry#connect#activate ~callback);
148 ignore (combo#entry#event#connect#key_press ~callback:(fun ev ->
149 if GdkEvent.Key.keyval ev = GdkKeysyms._Tab then
150 (entry#misc#grab_focus ();true)
151 else false))
152
153 method show =
154 frame#show;
155 let cur_page = notebook#get_nth_page notebook#current_page in
156 match List.find (fun (p,_,_) -> p#get_oid == cur_page#get_oid) views with
157 | (_, _, e) -> e#misc#grab_focus ()
158 | exception Not_found -> ()
159
160 method hide =
161 frame#hide
162
163 method visible =
164 frame#visible
165
166 method private refresh_color clr =
167 let clr = Gdk.Color.color_parse clr in
168 let iter (_,view,_) = view#misc#modify_bg [`NORMAL, `COLOR clr] in
169 List.iter iter views
170
171 initializer
172 self#new_page_maker;
173 self#new_query_aux ~grab_now:false ();
174 frame#misc#hide ();
175 (* FIXME: handle this using CSS *)
176 (* let _ = background_color#connect#changed ~callback:self#refresh_color in *)
177 (* self#refresh_color background_color#get; *)
178 ignore(notebook#event#connect#key_press ~callback:(fun ev ->
179 if GdkEvent.Key.keyval ev = GdkKeysyms._Escape then (self#hide; true)
180 else false
181 ));
182
183 end