]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/cicMathView.ml
ec42ee8df543f0fd7db44d8372a96874cbc244e0
[helm.git] / matita / matita / cicMathView.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 MatitaGuiTypes
29 open GtkSourceView3
30
31 let matita_script_current = ref (fun _ -> (assert false : < advance: ?statement:string -> unit -> unit; status: GrafiteTypes.status >));;
32 let register_matita_script_current f = matita_script_current := f;;
33 let get_matita_script_current () = !matita_script_current ();;
34
35 type document_element = (int * int * string) list * string (* hyperlinks,text *)
36
37 class type cicMathView =
38 object
39   inherit GObj.widget
40
41   method load_root : root:document_element -> unit
42   method remove_selections: unit
43   method set_selection: document_element option -> unit
44   method get_selections: document_element list
45
46     (** @raise Failure "no selection" *)
47   method strings_of_selection: (MatitaGuiTypes.paste_kind * string) list
48
49     (** set hyperlink callback. None disable hyperlink handling *)
50   method set_href_callback: (string -> unit) option -> unit
51
52     (** load a sequent and render it into parent widget *)
53   method nload_sequent:
54    #ApplyTransformation.status -> NCic.metasenv -> NCic.substitution -> int -> unit
55
56   method load_nobject: #ApplyTransformation.status -> NCic.obj -> unit
57 end
58
59 (*
60 let xlink_ns = Gdome.domString "http://www.w3.org/1999/xlink"
61 let helm_ns = Gdome.domString "http://www.cs.unibo.it/helm"
62 let href_ds = Gdome.domString "href"
63 let xref_ds = Gdome.domString "xref"
64 *)
65
66   (** Gdome.element of a MathML document whose rendering should be blank. Used
67   * by cicBrowser to render "about:blank" document *)
68 let empty_mathml = lazy ([],"")
69
70   (** shown for goals closed by side effects *)
71 let closed_goal_mathml = lazy ([],"chiuso per side effect...")
72
73 (*
74 let rec has_maction (elt :Gdome.element) = 
75   (* fix this comparison *)
76   if elt#get_tagName#to_string = "m:maction" ||
77    elt#get_tagName#to_string = "b:action" then
78     true
79   else 
80     match elt#get_parentNode with
81     | Some node when node#get_nodeType = GdomeNodeTypeT.ELEMENT_NODE -> 
82         has_maction (new Gdome.element_of_node node)
83     | _ -> false
84 ;;
85
86 let hrefs_of_elt elt =
87   let localName = href_ds in
88   if elt#hasAttributeNS ~namespaceURI:xlink_ns ~localName then
89     let text =
90       (elt#getAttributeNS ~namespaceURI:xlink_ns ~localName)#to_string in
91     Some (HExtlib.split text)
92   else
93     None
94
95 let id_of_node (node: Gdome.element) =
96   let xref_attr =
97     node#getAttributeNS ~namespaceURI:helm_ns ~localName:xref_ds in
98   try
99     List.hd (HExtlib.split ~sep:' ' xref_attr#to_string)
100   with Failure _ -> assert false
101
102 type selected_term =
103   | SelTerm of NCic.term * string option (* term, parent hypothesis (if any) *)
104   | SelHyp of string * NCic.context (* hypothesis, context *)
105 *)
106
107 let near (x1, y1) (x2, y2) =
108   let distance = sqrt (((x2 -. x1) ** 2.) +. ((y2 -. y1) ** 2.)) in
109   (distance < 4.)
110
111 (*
112 let name_of_hypothesis = function
113   | Some (Cic.Name s, _) -> s
114   | _ -> assert false
115
116 let domImpl = Gdome.domImplementation ()
117
118 (* ids_to_terms should not be passed here, is just for debugging *)
119 let find_root_id annobj id ids_to_father_ids ids_to_terms ids_to_inner_types =
120   assert false (* MATITA 1.0
121   let find_parent id ids =
122     let rec aux id =
123 (*       (prerr_endline (sprintf "id %s = %s" id
124         (try
125           CicPp.ppterm (Hashtbl.find ids_to_terms id)
126         with Not_found -> "NONE"))); *)
127       if List.mem id ids then Some id
128       else
129         (match
130           (try Hashtbl.find ids_to_father_ids id with Not_found -> None)
131         with
132         | None -> None
133         | Some id' -> aux id')
134     in
135     aux id
136   in
137   let return_father id ids =
138     match find_parent id ids with
139     | None -> assert false
140     | Some parent_id -> parent_id
141   in
142   let mk_ids terms = List.map CicUtil.id_of_annterm terms in
143   let inner_types =
144    Hashtbl.fold
145     (fun _ types acc ->
146       match types.Cic2acic.annexpected with
147          None -> types.Cic2acic.annsynthesized :: acc
148        | Some ty -> ty :: types.Cic2acic.annsynthesized :: acc
149     ) ids_to_inner_types [] in
150   match annobj with
151   | Cic.AConstant (_, _, _, Some bo, ty, _, _)
152   | Cic.AVariable (_, _, Some bo, ty, _, _)
153   | Cic.ACurrentProof (_, _, _, _, bo, ty, _, _) ->
154       return_father id (mk_ids (ty :: bo :: inner_types))
155   | Cic.AConstant (_, _, _, None, ty, _, _)
156   | Cic.AVariable (_, _, None, ty, _, _) ->
157       return_father id (mk_ids (ty::inner_types))
158   | Cic.AInductiveDefinition _ ->
159       assert false  (* TODO *)
160       *)
161
162   (** @return string content of a dom node having a single text child node, e.g.
163    * <m:mi xlink:href="...">bool</m:mi> *)
164 let string_of_dom_node node =
165   match node#get_firstChild with
166   | None -> ""
167   | Some node ->
168       (try
169         let text = new Gdome.text_of_node node in
170         text#get_data#to_string
171       with GdomeInit.DOMCastException _ -> "")
172 *)
173
174 class clickableMathView obj =
175 (*let text_width = 80 in*)
176 object (self)
177   inherit GSourceView3.source_view obj
178
179   method strings_of_selection = (assert false : (paste_kind * string) list)
180
181   val mutable href_callback: (string -> unit) option = None
182   method set_href_callback f = href_callback <- f
183
184   val mutable href_statusbar_msg:
185     (GMisc.statusbar_context * Gtk.statusbar_message) option = None
186     (* <statusbar ctxt, statusbar msg> *)
187
188   method private set_cic_info = (function _ -> () : unit (*(Cic.conjecture option * (Cic.id, Cic.term) Hashtbl.t *
189          (Cic.id, Cic.hypothesis) Hashtbl.t *
190          (Cic.id, Cic.id option) Hashtbl.t * ('a, 'b) Hashtbl.t * 'c option)*) option -> unit)
191   (* dal widget di Luca *)
192   method load_root ~root:(hyperlinks,text) =
193     self#buffer#set_text text;
194     let all_tag = self#buffer#create_tag [] in
195     self#buffer#apply_tag all_tag ~start:(self#buffer#get_iter `START)
196      ~stop:(self#buffer#get_iter `END);
197     ignore(all_tag#connect#event
198       ~callback:(fun ~origin:_ event _pos ->
199         match GdkEvent.get_type event with
200          | `MOTION_NOTIFY -> 
201              Gdk.Window.set_cursor
202               (match self#get_window `TEXT with None -> assert false | Some x -> x)
203               (Gdk.Cursor.create `ARROW);
204              HExtlib.iter_option (fun (ctxt, msg) -> ctxt#remove msg)
205               href_statusbar_msg;
206              false
207          | _ -> false));
208      let hyperlink_tag = self#buffer#create_tag [] in
209      ignore(hyperlink_tag#connect#event
210        ~callback:(fun ~origin:_ event pos ->
211          let offset = (new GText.iter pos)#offset in
212          let _,_,href =
213           try
214            List.find
215             (fun (start,stop,_href) -> start <= offset && offset <= stop
216             ) hyperlinks
217           with
218            Not_found -> assert false
219          in
220          match GdkEvent.get_type event with
221             `BUTTON_PRESS -> 
222               (match href_callback with
223                   None -> ()
224                 | Some f -> f href);
225               true
226           | `MOTION_NOTIFY -> 
227               Gdk.Window.set_cursor
228                (match self#get_window `TEXT with None -> assert false | Some x -> x)
229                (Gdk.Cursor.create `HAND1);
230               let ctxt = (MatitaMisc.get_gui ())#main#statusBar#new_context ~name:"href" in
231               let msg = ctxt#push href in
232               href_statusbar_msg <- Some (ctxt, msg);
233               false
234           | _ -> false));
235      List.iter
236       ( fun (start,stop,(_href : string)) ->
237           self#buffer#apply_tag hyperlink_tag
238            ~start:(self#buffer#get_iter_at_char start)
239            ~stop:(self#buffer#get_iter_at_char (stop+1));
240       ) hyperlinks
241
242
243   method action_toggle = (fun _ -> assert false : document_element -> bool)
244   method remove_selections = (() : unit)
245   method set_selection = (fun _ -> () : document_element option -> unit)
246   method get_selections = (assert false : document_element list)
247
248   initializer
249     self#source_buffer#set_language (Some MatitaGtkMisc.matita_lang);
250     self#source_buffer#set_highlight_syntax true;
251     self#set_editable false;
252     MatitaMisc.observe_font_size
253      (fun size ->
254        self#misc#modify_font_by_name
255         (sprintf "%s %d" BuildTimeConf.script_font size))
256
257 (* MATITA 1.0
258   inherit GMathViewAux.multi_selection_math_view obj
259
260   val mutable href_callback: (string -> unit) option = None
261   method set_href_callback f = href_callback <- f
262
263   val mutable _cic_info = None
264   method private set_cic_info info = _cic_info <- info
265   method private cic_info = _cic_info
266
267   val normal_cursor = Gdk.Cursor.create `LEFT_PTR
268   val href_cursor = Gdk.Cursor.create `HAND2
269   val maction_cursor = Gdk.Cursor.create `QUESTION_ARROW
270
271   initializer
272     ignore (self#connect#selection_changed self#choose_selection_cb);
273     ignore (self#event#connect#button_press self#button_press_cb);
274     ignore (self#event#connect#button_release self#button_release_cb);
275     ignore (self#event#connect#selection_clear self#selection_clear_cb);
276     ignore (self#connect#element_over self#element_over_cb);
277     ignore (self#coerce#misc#connect#selection_get self#selection_get_cb)
278
279   val mutable button_press_x = -1.
280   val mutable button_press_y = -1.
281   val mutable selection_changed = false
282   val mutable href_statusbar_msg:
283     (GMisc.statusbar_context * Gtk.statusbar_message) option = None
284     (* <statusbar ctxt, statusbar msg> *)
285
286   method private selection_get_cb ctxt ~info ~time =
287     let text =
288       match ctxt#target with
289       | "PATTERN" -> self#text_of_selection `Pattern
290       | "TERM" | _ -> self#text_of_selection `Term
291     in
292     match text with
293     | None -> ()
294     | Some s -> ctxt#return s
295
296   method private text_of_selection fmt =
297     match self#get_selections with
298     | [] -> None
299     | node :: _ -> Some (self#string_of_node ~paste_kind:fmt node)
300
301   method private selection_clear_cb sel_event =
302     self#remove_selections;
303     (GData.clipboard Gdk.Atom.clipboard)#clear ();
304     false
305
306   method private button_press_cb gdk_button =
307     let button = GdkEvent.Button.button gdk_button in
308     if  button = MatitaMisc.left_button then begin
309       button_press_x <- GdkEvent.Button.x gdk_button;
310       button_press_y <- GdkEvent.Button.y gdk_button;
311       selection_changed <- false
312     end else if button = MatitaMisc.right_button then
313       self#popup_contextual_menu 
314         (self#get_element_at 
315           (int_of_float (GdkEvent.Button.x gdk_button)) 
316           (int_of_float (GdkEvent.Button.y gdk_button)))  
317         (GdkEvent.Button.time gdk_button);
318     false
319
320   method private element_over_cb (elt_opt, _, _, _) =
321     let win () = self#misc#window in
322     let leave_href () =
323       Gdk.Window.set_cursor (win ()) normal_cursor;
324       HExtlib.iter_option (fun (ctxt, msg) -> ctxt#remove msg)
325         href_statusbar_msg
326     in
327     match elt_opt with
328     | Some elt ->
329         if has_maction elt then
330           Gdk.Window.set_cursor (win ()) maction_cursor
331         else
332         (match hrefs_of_elt elt with
333         | Some ((_ :: _) as hrefs) ->
334             Gdk.Window.set_cursor (win ()) href_cursor;
335             let msg_text = (* now create statusbar msg and store it *)
336               match hrefs with
337               | [ href ] -> sprintf "Hyperlink to %s" href
338               | _ -> sprintf "Hyperlinks to: %s" (String.concat ", " hrefs) in
339             let ctxt = (get_gui ())#main#statusBar#new_context ~name:"href" in
340             let msg = ctxt#push msg_text in
341             href_statusbar_msg <- Some (ctxt, msg)
342         | _ -> leave_href ())
343     | None -> leave_href ()
344
345   method private tactic_text_pattern_of_node node =
346    let id = id_of_node node in
347    let cic_info, unsh_sequent = self#get_cic_info id in
348    match self#get_term_by_id cic_info id with
349    | SelTerm (t, father_hyp) ->
350        let sequent = self#sequent_of_id ~paste_kind:`Pattern id in
351        let text = self#string_of_cic_sequent ~output_type:`Pattern sequent in
352        (match father_hyp with
353        | None -> None, [], Some text
354        | Some hyp_name -> None, [ hyp_name, text ], None)
355    | SelHyp (hyp_name, _ctxt) -> None, [ hyp_name, "%" ], None
356
357   method private tactic_text_of_node node =
358    let id = id_of_node node in
359    let cic_info, unsh_sequent = self#get_cic_info id in
360    match self#get_term_by_id cic_info id with
361    | SelTerm (t, father_hyp) ->
362        let sequent = self#sequent_of_id ~paste_kind:`Term id in
363        let text = self#string_of_cic_sequent ~output_type:`Term sequent in
364        text
365    | SelHyp (hyp_name, _ctxt) -> hyp_name
366
367     (** @return a pattern structure which contains pretty printed terms *)
368   method private tactic_text_pattern_of_selection =
369     match self#get_selections with
370     | [] -> assert false (* this method is invoked only if there's a sel. *)
371     | node :: _ -> self#tactic_text_pattern_of_node node
372
373   method private popup_contextual_menu element time =
374     let menu = GMenu.menu () in
375     let add_menu_item ?(menu = menu) ?stock ?label () =
376       GMenu.image_menu_item ?stock ?label ~packing:menu#append () in
377     let check = add_menu_item ~label:"Check" () in
378     let reductions_menu_item = GMenu.menu_item ~label:"βδιζ-reduce" () in
379     let tactics_menu_item = GMenu.menu_item ~label:"Apply tactic" () in
380     let hyperlinks_menu_item = GMenu.menu_item ~label:"Hyperlinks" () in
381     menu#append reductions_menu_item;
382     menu#append tactics_menu_item;
383     menu#append hyperlinks_menu_item;
384     let reductions = GMenu.menu () in
385     let tactics = GMenu.menu () in
386     let hyperlinks = GMenu.menu () in
387     reductions_menu_item#set_submenu reductions;
388     tactics_menu_item#set_submenu tactics;
389     hyperlinks_menu_item#set_submenu hyperlinks;
390     let normalize = add_menu_item ~menu:reductions ~label:"Normalize" () in
391     let simplify = add_menu_item ~menu:reductions ~label:"Simplify" () in
392     let whd = add_menu_item ~menu:reductions ~label:"Weak head" () in
393     (match element with 
394     | None -> hyperlinks_menu_item#misc#set_sensitive false
395     | Some elt -> 
396         match hrefs_of_elt elt, href_callback with
397         | Some l, Some f ->
398             List.iter 
399               (fun h ->
400                 let item = add_menu_item ~menu:hyperlinks ~label:h () in
401                 connect_menu_item item (fun () -> f h)) l
402         | _ -> hyperlinks_menu_item#misc#set_sensitive false);
403     menu#append (GMenu.separator_item ());
404     let copy = add_menu_item ~stock:`COPY () in
405     let gui = get_gui () in
406     List.iter (fun item -> item#misc#set_sensitive gui#canCopy)
407       [ copy; check; normalize; simplify; whd ];
408     let reduction_action kind () =
409       let pat = self#tactic_text_pattern_of_selection in
410       let statement =
411         let loc = HExtlib.dummy_floc in
412         "\n" ^
413         GrafiteAstPp.pp_executable ~term_pp:(fun s -> s)
414           ~lazy_term_pp:(fun _ -> assert false) ~obj_pp:(fun _ -> assert false)
415           ~map_unicode_to_tex:(Helm_registry.get_bool
416             "matita.paste_unicode_as_tex")
417           (GrafiteAst.Tactic (loc,
418             Some (GrafiteAst.Reduce (loc, kind, pat)),
419             GrafiteAst.Semicolon loc)) in
420       (get_matita_script_current ())#advance ~statement () in
421     connect_menu_item copy gui#copy;
422     connect_menu_item normalize (reduction_action `Normalize);
423     connect_menu_item simplify (reduction_action `Simpl);
424     connect_menu_item whd (reduction_action `Whd);
425     menu#popup ~button:MatitaMisc.right_button ~time
426
427   method private button_release_cb gdk_button =
428     if GdkEvent.Button.button gdk_button = MatitaMisc.left_button then begin
429       let button_release_x = GdkEvent.Button.x gdk_button in
430       let button_release_y = GdkEvent.Button.y gdk_button in
431       if selection_changed then
432         ()
433       else  (* selection _not_ changed *)
434         if near (button_press_x, button_press_y)
435           (button_release_x, button_release_y)
436         then
437           let x = int_of_float button_press_x in
438           let y = int_of_float button_press_y in
439           (match self#get_element_at x y with
440           | None -> ()
441           | Some elt ->
442               if has_maction elt then ignore(self#action_toggle elt) else
443               (match hrefs_of_elt elt with
444               | Some hrefs -> self#invoke_href_callback hrefs gdk_button
445               | None -> ()))
446     end;
447     false
448
449   method private invoke_href_callback hrefs gdk_button =
450     let button = GdkEvent.Button.button gdk_button in
451     if button = MatitaMisc.left_button then
452       let time = GdkEvent.Button.time gdk_button in
453       match href_callback with
454       | None -> ()
455       | Some f ->
456           (match hrefs with
457           | [ uri ] ->  f uri
458           | uris ->
459               let menu = GMenu.menu () in
460               List.iter
461                 (fun uri ->
462                   let menu_item =
463                     GMenu.menu_item ~label:uri ~packing:menu#append () in
464                   connect_menu_item menu_item 
465                   (fun () -> try f uri with Not_found -> assert false))
466                 uris;
467               menu#popup ~button ~time)
468
469   method private choose_selection_cb gdome_elt =
470     let set_selection elt =
471       let misc = self#coerce#misc in
472       self#set_selection (Some elt);
473       misc#add_selection_target ~target:"STRING" Gdk.Atom.primary;
474       ignore (misc#grab_selection Gdk.Atom.primary);
475     in
476     let rec aux elt =
477       if (elt#getAttributeNS ~namespaceURI:helm_ns
478             ~localName:xref_ds)#to_string <> ""
479       then
480         set_selection elt
481       else
482         try
483           (match elt#get_parentNode with
484           | None -> assert false
485           | Some p -> aux (new Gdome.element_of_node p))
486         with GdomeInit.DOMCastException _ -> ()
487     in
488     (match gdome_elt with
489     | Some elt when (elt#getAttributeNS ~namespaceURI:xlink_ns
490         ~localName:href_ds)#to_string <> "" ->
491           set_selection elt
492     | Some elt -> aux elt
493     | None -> self#set_selection None);
494     selection_changed <- true
495
496     (** find a term by id from stored CIC infos @return either `Hyp if the id
497      * correspond to an hypothesis or `Term (cic, hyp) if the id correspond to a
498      * term. In the latter case hyp is either None (if the term is a subterm of
499      * the sequent conclusion) or Some hyp_name if the term belongs to an
500      * hypothesis *)
501   method private get_term_by_id cic_info id =
502     let unsh_item, ids_to_terms, ids_to_hypotheses, ids_to_father_ids, _, _ =
503       cic_info in
504     let rec find_father_hyp id =
505       if Hashtbl.mem ids_to_hypotheses id
506       then Some (name_of_hypothesis (Hashtbl.find ids_to_hypotheses id))
507       else
508         let father_id =
509           try Hashtbl.find ids_to_father_ids id
510           with Not_found -> assert false in
511         match father_id with
512         | Some id -> find_father_hyp id
513         | None -> None
514     in
515     try
516       let term = Hashtbl.find ids_to_terms id in
517       let father_hyp = find_father_hyp id in
518       SelTerm (term, father_hyp)
519     with Not_found ->
520       try
521         let hyp = Hashtbl.find ids_to_hypotheses id in
522         let _, context, _ =
523           match unsh_item with Some seq -> seq | None -> assert false in
524         let context' = MatitaMisc.list_tl_at hyp context in
525         SelHyp (name_of_hypothesis hyp, context')
526       with Not_found -> assert false
527     
528   method private find_obj_conclusion id =
529     match self#cic_info with
530     | None
531     | Some (_, _, _, _, _, None) -> assert false
532     | Some (_, ids_to_terms, _, ids_to_father_ids, ids_to_inner_types, Some annobj) ->
533         let id =
534          find_root_id annobj id ids_to_father_ids ids_to_terms ids_to_inner_types
535         in
536          (try Hashtbl.find ids_to_terms id with Not_found -> assert false)
537
538   method private string_of_node ~(paste_kind:paste_kind) node =
539     if node#hasAttributeNS ~namespaceURI:helm_ns ~localName:xref_ds
540     then
541       match paste_kind with
542       | `Pattern ->
543           let tactic_text_pattern =  self#tactic_text_pattern_of_node node in
544           GrafiteAstPp.pp_tactic_pattern
545             ~term_pp:(fun s -> s) ~lazy_term_pp:(fun _ -> assert false)
546             ~map_unicode_to_tex:(Helm_registry.get_bool
547               "matita.paste_unicode_as_tex")
548             tactic_text_pattern
549       | `Term -> self#tactic_text_of_node node
550     else string_of_dom_node node
551
552   method private string_of_cic_sequent ~output_type cic_sequent =
553     let script = get_matita_script_current () in
554     let metasenv =
555       if script#onGoingProof () then script#proofMetasenv else [] in
556     let map_unicode_to_tex =
557       Helm_registry.get_bool "matita.paste_unicode_as_tex" in
558     ApplyTransformation.txt_of_cic_sequent_conclusion ~map_unicode_to_tex
559      ~output_type text_width metasenv cic_sequent
560
561   method private pattern_of term father_hyp unsh_sequent =
562     let _, unsh_context, conclusion = unsh_sequent in
563     let where =
564      match father_hyp with
565         None -> conclusion
566       | Some name ->
567          let rec aux =
568           function
569              [] -> assert false
570            | Some (Cic.Name name', Cic.Decl ty)::_ when name' = name -> ty
571            | Some (Cic.Name name', Cic.Def (bo,_))::_ when name' = name-> bo
572            | _::tl -> aux tl
573          in
574           aux unsh_context
575     in
576      ProofEngineHelpers.pattern_of ~term:where [term]
577
578   method private get_cic_info id =
579     match self#cic_info with
580     | Some ((Some unsh_sequent, _, _, _, _, _) as info) -> info, unsh_sequent
581     | Some ((None, _, _, _, _, _) as info) ->
582         let t = self#find_obj_conclusion id in
583         info, (~-1, [], t) (* dummy sequent for obj *)
584     | None -> assert false
585
586   method private sequent_of_id ~(paste_kind:paste_kind) id =
587     let cic_info, unsh_sequent = self#get_cic_info id in
588     let cic_sequent =
589       match self#get_term_by_id cic_info id with
590       | SelTerm (t, father_hyp) ->
591 (*
592 IDIOTA: PRIMA SI FA LA LOCATE, POI LA PATTERN_OF. MEGLIO UN'UNICA pattern_of CHE PRENDA IN INPUT UN TERMINE E UN SEQUENTE. PER IL MOMENTO RISOLVO USANDO LA father_hyp PER RITROVARE L'IPOTESI PERDUTA
593 *)
594           let occurrences =
595             ProofEngineHelpers.locate_in_conjecture t unsh_sequent in
596           (match occurrences with
597           | [ context, _t ] ->
598               (match paste_kind with
599               | `Term -> ~-1, context, t
600               | `Pattern -> ~-1, [], self#pattern_of t father_hyp unsh_sequent)
601           | _ ->
602               HLog.error (sprintf "found %d occurrences while 1 was expected"
603                 (List.length occurrences));
604               assert false) (* since it uses physical equality *)
605       | SelHyp (_name, context) -> ~-1, context, Cic.Rel 1 in
606     cic_sequent
607
608   method private string_of_selection ~(paste_kind:paste_kind) =
609     match self#get_selections with
610     | [] -> None
611     | node :: _ -> Some (self#string_of_node ~paste_kind node)
612
613     (** @return an associative list format -> string with all possible selection
614      * formats. Rationale: in order to convert the selection to TERM or PATTERN
615      * format we need the sequent, the metasenv, ... keeping all of them in a
616      * closure would be more expensive than keeping their already converted
617      * forms *)
618   method strings_of_selection =
619     try
620       let misc = self#coerce#misc in
621       List.iter
622         (fun target -> misc#add_selection_target ~target Gdk.Atom.clipboard)
623         [ "TERM"; "PATTERN"; "STRING" ];
624       ignore (misc#grab_selection Gdk.Atom.clipboard);
625       List.map
626         (fun paste_kind ->
627           paste_kind, HExtlib.unopt (self#string_of_selection ~paste_kind))
628         [ `Term; `Pattern ]
629     with Failure _ -> failwith "no selection"
630 *)
631 end
632
633 class _cicMathView obj =
634 object (self)
635   inherit clickableMathView obj
636
637   val mutable current_mathml = None
638
639   method nload_sequent:
640    'status. #ApplyTransformation.status as 'status -> NCic.metasenv ->
641      NCic.substitution -> int -> unit
642    = fun status metasenv subst metano ->
643     let sequent = List.assoc metano metasenv in
644     let txt =
645      ApplyTransformation.ntxt_of_cic_sequent
646       ~map_unicode_to_tex:false 80 status ~metasenv ~subst (metano,sequent)
647     in
648     (* MATITA 1.0 if BuildTimeConf.debug then begin
649       let name =
650        "/tmp/sequent_viewer_" ^ string_of_int (Unix.getuid ()) ^ ".xml" in
651       HLog.debug ("load_sequent: dumping MathML to ./" ^ name);
652       ignore (domImpl#saveDocumentToFile ~name ~doc:mathml ())
653     end;*)
654     self#load_root ~root:txt
655
656   method load_nobject :
657    'status. #ApplyTransformation.status as 'status -> NCic.obj -> unit
658    = fun status obj ->
659     let txt = ApplyTransformation.ntxt_of_cic_object ~map_unicode_to_tex:false
660     80 status obj in
661 (*
662     self#set_cic_info
663       (Some (None, ids_to_terms, ids_to_hypotheses, ids_to_father_ids, ids_to_inner_types, Some annobj));
664     (match current_mathml with
665     | Some current_mathml when use_diff ->
666         self#freeze;
667         XmlDiff.update_dom ~from:current_mathml mathml;
668         self#thaw
669     |  _ ->
670 *)
671         (* MATITA 1.0 if BuildTimeConf.debug then begin
672           let name =
673            "/tmp/cic_browser_" ^ string_of_int (Unix.getuid ()) ^ ".xml" in
674           HLog.debug ("cic_browser: dumping MathML to ./" ^ name);
675           ignore (domImpl#saveDocumentToFile ~name ~doc:mathml ())
676         end;*)
677         self#load_root ~root:txt;
678         (*current_mathml <- Some mathml*)(*)*);
679 end
680
681  (** constructors *)
682
683 let cicMathView (*?auto_indent ?highlight_current_line ?indent_on_tab ?indent_width ?insert_spaces_instead_of_tabs ?right_margin_position ?show_line_marks ?show_line_numbers ?show_right_margin ?smart_home_end ?tab_width ?editable ?cursor_visible ?justification ?wrap_mode ?accepts_tab ?border_width*) ?width ?height ?packing ?show () =
684  ((*SourceView.make_params [] ~cont:(
685     GtkText.View.make_params ~cont:( *)
686       GContainer.pack_container ~create:(fun pl ->
687         let obj = SourceView.new_ () in
688         Gobject.set_params (Gobject.try_cast obj "GtkSourceView") pl;
689         new _cicMathView obj)(*)) ?auto_indent ?highlight_current_line ?indent_on_tab ?indent_width ?insert_spaces_instead_of_tabs ?right_margin_position ?show_line_marks ?show_line_numbers ?show_right_margin ?smart_home_end ?tab_width ?editable ?cursor_visible ?justification ?wrap_mode ?accepts_tab ?border_width*) [] ?width ?height ?packing ?show () :> cicMathView)
690
691 let screenshot _status _sequents _metasenv _subst (_filename (*as ofn*)) =
692  () (*MATITA 1.0
693   let w = GWindow.window ~title:"screenshot" () in
694   let width = 500 in
695   let height = 2000 in
696   let m = GMathView.math_view 
697      ~font_size:(MatitaMisc.get_current_font_size ()) ~width ~height
698      ~packing:w#add
699      ~show:true ()
700   in
701   w#show ();
702   let filenames = 
703    HExtlib.list_mapi
704     (fun (mno,_ as sequent) i ->
705        let mathml = 
706          ApplyTransformation.nmml_of_cic_sequent 
707            status metasenv subst sequent
708        in
709        m#load_root ~root:mathml#get_documentElement;
710        let pixmap = m#get_buffer in
711        let pixbuf = GdkPixbuf.create ~width ~height () in
712        GdkPixbuf.get_from_drawable ~dest:pixbuf pixmap;
713        let filename = 
714          filename ^ "-raw" ^ string_of_int i ^ ".png" 
715        in
716        GdkPixbuf.save ~filename ~typ:"png" pixbuf;
717        filename,mno)
718     sequents
719   in
720   let items = 
721     List.map (fun (x,mno) -> 
722       ignore(Sys.command
723         (Printf.sprintf
724          ("convert "^^
725          " '(' -gravity west -bordercolor grey -border 1 label:%d ')' "^^
726          " '(' -trim -bordercolor white -border 5 "^^
727            " -bordercolor grey -border 1 %s ')' -append %s ")
728          mno
729          (Filename.quote x)
730          (Filename.quote (x ^ ".label.png"))));
731         x ^ ".label.png")
732     filenames
733   in
734   let rec div2 = function 
735     | [] -> []
736     | [x] -> [[x]]
737     | x::y::tl -> [x;y] :: div2 tl
738   in
739   let items = div2 items in
740   ignore(Sys.command (Printf.sprintf 
741     "convert %s -append  %s" 
742      (String.concat ""
743        (List.map (fun items ->
744          Printf.sprintf " '(' %s +append ')' "
745            (String.concat 
746               (" '(' -gravity center -size 10x10 xc: ')' ") items)) items))
747     (Filename.quote (ofn ^ ".png")))); 
748   List.iter (fun x,_ -> Sys.remove x) filenames;
749   List.iter Sys.remove (List.flatten items);
750   w#destroy ()*)