Attention une mise à jour du serveur va être effectuée le vendredi 16 avril entre 12h et 12h30. Cette mise à jour va générer une interruption du service de quelques minutes.

grew_gtk.ml 36.8 KB
Newer Older
bguillaum's avatar
bguillaum committed
1 2 3 4 5 6 7 8 9 10
(***********************************************************************)
(*    Grew - a Graph Rewriting tool dedicated to NLP applications      *)
(*                                                                     *)
(*    Copyright 2011-2013 Inria, Université de Lorraine                *)
(*                                                                     *)
(*    Webpage: http://grew.loria.fr                                    *)
(*    License: CeCILL (see LICENSE folder or "http://www.cecill.info") *)
(*    Authors: see AUTHORS file                                        *)
(***********************************************************************)

bguillaum's avatar
bguillaum committed
11 12
open Printf
open Log
13 14 15
open GMain

open Libgrew
bguillaum's avatar
bguillaum committed
16 17 18 19 20 21

open Grew_glade
open Grew_rew_display
open Grew_utils
open Grew_args

bguillaum's avatar
bguillaum committed
22 23 24 25 26 27 28 29 30 31
type msg =
 | Error of string
 | Warning of string
 | Info of string

let messages = ref []

let error fmt = Printf.ksprintf (fun x -> messages := Error (Printf.sprintf "%s\n" x) :: !messages) fmt
let warning fmt = Printf.ksprintf (fun x -> messages := Warning (Printf.sprintf "%s\n" x) :: !messages) fmt
let info fmt = Printf.ksprintf (fun x -> messages := Info (Printf.sprintf "%s\n" x) :: !messages) fmt
bguillaum's avatar
bguillaum committed
32 33 34

(* ==================================================================================================== *)
(* code taken from lablgtk2 examples *)
bguillaum's avatar
bguillaum committed
35
let ask_for_file_to_open filter parent =
bguillaum's avatar
bguillaum committed
36 37 38 39 40 41 42 43
  let res = ref None in
  let dialog = GWindow.file_chooser_dialog
    ~action:`OPEN
    ~title:"Open File"
    ~parent () in
  dialog#add_button_stock `CANCEL `CANCEL ;
  dialog#add_select_button_stock `OPEN `OPEN ;
  dialog#add_filter filter;
bguillaum's avatar
bguillaum committed
44 45
  dialog#add_filter (GFile.filter ~name:"All files" ~patterns:["*"] ());

bguillaum's avatar
bguillaum committed
46 47 48 49 50 51 52
  begin match dialog#run () with
    | `OPEN -> res := dialog#filename
    | `DELETE_EVENT | `CANCEL -> ()
  end ;
  dialog#destroy ();
  !res

bguillaum's avatar
bguillaum committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
(* ==================================================================================================== *)
let ask_for_file_to_save filter parent =
  let res = ref None in
  let dialog = GWindow.file_chooser_dialog
    ~action:`SAVE
    ~title:"Save File"
    ~parent () in
  dialog#add_button_stock `CANCEL `CANCEL ;
  dialog#add_select_button_stock `SAVE `SAVE ;
  dialog#add_filter filter;
  begin match dialog#run () with
    | `SAVE -> res := dialog#filename
    | `DELETE_EVENT | `CANCEL -> ()
  end ;
  dialog#destroy ();
  !res

bguillaum's avatar
bguillaum committed
70 71 72
(* ==================================================================================================== *)
module Resources = struct
  let current_grs = ref None
73
  let current_grs_file = ref None
bguillaum's avatar
bguillaum committed
74
  let current_gr = ref None
75
  let current_gr_file = ref None
bguillaum's avatar
bguillaum committed
76 77

  (* -------------------------------------------------------------------------------- *)
78
  let load_grs () =
bguillaum's avatar
bguillaum committed
79
    current_grs := None;
80 81
    match !current_grs_file with
    | None -> ()
Bruno Guillaume's avatar
Bruno Guillaume committed
82
    | Some file ->
83
        Log.fmessage "Loading grs file: '%s'" file;
Bruno Guillaume's avatar
Bruno Guillaume committed
84
        current_grs := Some (if !Grew_args.old_grs then Grs.load_old file else Grs.load file)
bguillaum's avatar
bguillaum committed
85

86
  (* -------------------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
87
  let domain () = match !current_grs with
88
    | Some grs -> Grs.domain grs
bguillaum's avatar
bguillaum committed
89
    | None -> None
90

bguillaum's avatar
bguillaum committed
91
  (* -------------------------------------------------------------------------------- *)
92
  let load_gr () =
bguillaum's avatar
bguillaum committed
93
    current_gr := None;
94 95
    match !current_gr_file with
    | None -> ()
Bruno Guillaume's avatar
Bruno Guillaume committed
96
    | Some file ->
97 98 99
      Log.fmessage "Loading gr file: '%s'" file;
      let domain = domain () in
      current_gr := Some (Graph.load ?domain file)
bguillaum's avatar
bguillaum committed
100 101 102

  (* -------------------------------------------------------------------------------- *)
  exception Cannot_rewrite of string
103
  let rewrite strat =
bguillaum's avatar
bguillaum committed
104
    match (!current_grs, !current_gr) with
105 106 107 108
      | (Some grs, Some gr) ->
        begin
          match (Rewrite.at_least_one ~grs ~strat, Rewrite.at_most_one ~grs ~strat) with
        | (true, true) -> Rewrite.display gr grs strat
109 110
        | (false, true) -> raise (Cannot_rewrite "The current strategy may not be productive (cannot be used in GUI)")
        | (_, false) -> raise (Cannot_rewrite "The current strategy is not deterministic (cannot be used in GUI)")
111
        end;
bguillaum's avatar
bguillaum committed
112 113 114 115 116
      | (None, _) -> raise (Cannot_rewrite "No grs file loaded")
      | (_, None) -> raise (Cannot_rewrite "No graph file loaded")
end (* module Resources *)
(* ==================================================================================================== *)

bguillaum's avatar
bguillaum committed
117
(* ------------------------------------------------------------ *)
bguillaum's avatar
bguillaum committed
118
let filter_features = ref false
bguillaum's avatar
bguillaum committed
119 120 121 122 123
let current_features = ref []

let get_current_filter () =
  match (!filter_features, !current_features) with
    | (false, _) -> None
bguillaum's avatar
bguillaum committed
124
    | (true, l) -> Some (List.map fst (List.filter snd l))
bguillaum's avatar
bguillaum committed
125 126

let feat_set label value =
bguillaum's avatar
bguillaum committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  let rec loop = function
    | [] -> []
    | (x,_)::t when x=label -> (x,value)::t
    | c::t -> c::(loop t) in
  current_features := loop !current_features

let (config_vbox : GPack.box option ref) = ref None (* the flag is true iff the config windows is open *)

let fill_vbox vbox =
  List.iter
    (fun (label,active) ->
      let item = GButton.check_button ~label ~active ~packing:vbox#add () in
      let _ = item#connect#toggled ~callback:(fun () -> feat_set label item#active) in
      ()
    ) !current_features

(* when GRS change *)
let update_features () =
145
  begin
bguillaum's avatar
bguillaum committed
146
    match Resources.domain () with
147 148 149
    | None -> ()
    | Some dom -> current_features := List.map (fun x -> (x,true)) (Domain.feature_names dom)
  end;
bguillaum's avatar
bguillaum committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  match !config_vbox with
    | None -> ()
    | Some vbox -> (* if the config windows is opened, it must be updated *)
      List.iter (fun item -> item#destroy ()) vbox#children;
      fill_vbox vbox

(* ------------------------------------------------------------ *)
let display_config_window () =
  if !config_vbox = None
  then
    begin
      let win = new config_window () in
      config_vbox := Some win#config_vbox;

      fill_vbox win#config_vbox;

bguillaum's avatar
bguillaum committed
166 167 168 169 170
      let main_feat = match !Grew_args.main_feat with
        | None -> "phon"
        | Some s -> s in

      let () = win#main_feat#set_text main_feat in
bguillaum's avatar
bguillaum committed
171 172

      let _ = win#main_feat#connect#changed
bguillaum's avatar
bguillaum committed
173
        ~callback: (fun () -> Grew_args.main_feat := Some win#main_feat#text) in
bguillaum's avatar
bguillaum committed
174 175 176 177 178 179 180

      let _ = win#btn_config_close#connect#clicked ~callback:
        (fun () -> config_vbox := None; win#toplevel#destroy ()) in
      let _ = win#toplevel#event#connect#delete ~callback: (fun _ -> config_vbox := None; false) in
      win#toplevel#show ()
    end

bguillaum's avatar
bguillaum committed
181
type save = Png | Pdf_dep | Pdf_dot | Dep | Dot | Gr | Conll
bguillaum's avatar
bguillaum committed
182
type side = Top | Bottom
bguillaum's avatar
bguillaum committed
183

bguillaum's avatar
bguillaum committed
184
let string_of_side = function Top -> "top" | Bottom -> "bottom"
bguillaum's avatar
bguillaum committed
185

bguillaum's avatar
bguillaum committed
186 187
(* ==================================================================================================== *)
let init () =
bguillaum's avatar
bguillaum committed
188 189
  let _ = GMain.Main.init () in
  let grew_window = new grew_window () in
bguillaum's avatar
bguillaum committed
190

bguillaum's avatar
bguillaum committed
191
  (* combo_box_text not implemented in lablgladecc2 *)
Bruno Guillaume's avatar
Bruno Guillaume committed
192
  let combo_box_text = GEdit.combo_box_text ~packing:grew_window#strat_list_viewport#add () in
bguillaum's avatar
bguillaum committed
193

bguillaum's avatar
bguillaum committed
194 195 196
  let main_feat = match !Grew_args.main_feat with
    | None -> "phon"
    | Some s -> s in
bguillaum's avatar
bguillaum committed
197 198 199

  let empty_html = "<html><body><font color=red fontname=Arial>Nothing to display</font></body></html>" in

bguillaum's avatar
bguillaum committed
200 201
  let _ = grew_window#btn_preferences#connect#clicked ~callback: (fun _ -> display_config_window ()) in (* XXX *)

bguillaum's avatar
bguillaum committed
202 203 204 205
  (** WEBKITS CREATIONS *)
  let grs_webkit = GWebView.web_view ~packing:grew_window#grs_view#add () in
  let module_webkit = GWebView.web_view ~packing:grew_window#module_view#add () in
  let error_webkit = GWebView.web_view ~packing:grew_window#err_view_scroll#add () in
bguillaum's avatar
bguillaum committed
206 207
  let graph_top_webkit = GWebView.web_view ~packing:grew_window#graph_view_top#add () in
  let graph_bottom_webkit = GWebView.web_view ~packing:grew_window#graph_view_bottom#add () in
bguillaum's avatar
bguillaum committed
208

bguillaum's avatar
bguillaum committed
209 210 211 212 213 214
  grs_webkit#set_full_content_zoom true;
  module_webkit#set_full_content_zoom true;
  graph_top_webkit#set_full_content_zoom true;
  graph_bottom_webkit#set_full_content_zoom true;
  error_webkit#set_full_content_zoom true;

bguillaum's avatar
bguillaum committed
215
  (* ensure UTF-8 encoding *)
bguillaum's avatar
bguillaum committed
216 217 218 219 220 221 222 223
  grs_webkit#set_custom_encoding "UTF-8";
  module_webkit#set_custom_encoding "UTF-8";
  graph_top_webkit#set_custom_encoding "UTF-8";
  graph_bottom_webkit#set_custom_encoding "UTF-8";
  error_webkit#set_custom_encoding "UTF-8";

  (* By default disable the contextual menu on webview *)
  let web_settings_def = GWebSettings.web_settings () in
bguillaum's avatar
bguillaum committed
224
  web_settings_def#set_enable_default_context_menu false;
bguillaum's avatar
bguillaum committed
225 226 227 228 229 230
  grs_webkit#set_settings web_settings_def;
  module_webkit#set_settings web_settings_def;
  graph_top_webkit#set_settings web_settings_def;
  graph_bottom_webkit#set_settings web_settings_def;
  error_webkit#set_settings web_settings_def;

231 232 233
  grew_window#graph_label#set_use_markup true;
  grew_window#grs_label#set_use_markup true;

bguillaum's avatar
bguillaum committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  let refresh_error () =
    match !messages with
    | [] -> grew_window#err_view_scroll#misc#hide ();
    | l ->
      let html = String.concat "<br/>\n"
        (List.map (function
          | Error s -> sprintf "<html><body><font color=red fontname=Arial>ERROR: %s</font></body></html>" s
          | Warning s -> sprintf "<html><body><font color=orange fontname=Arial>WARNING: %s</font></body></html>" s
          | Info s -> sprintf "<html><body><font color=blue fontname=Arial>INFO: %s</font></body></html>" s
          ) l
        ) in
    error_webkit#load_html_string html "";
    grew_window#err_view_scroll#misc#show () in

  let show_error msg =
    messages := [];
    error "%s" msg;
    refresh_error () in

bguillaum's avatar
bguillaum committed
253 254
  let reset ()  =
    (* empty all webkits *)
bguillaum's avatar
bguillaum committed
255 256 257 258
    graph_top_webkit#load_html_string empty_html "";
    graph_bottom_webkit#load_html_string empty_html "";
    module_webkit#load_html_string empty_html "";
    grs_webkit#load_html_string empty_html "";
bguillaum's avatar
bguillaum committed
259 260 261

    Grew_rew_display.current_bottom_graph := "";
    Grew_rew_display.current_top_graph := "";
bguillaum's avatar
bguillaum committed
262

bguillaum's avatar
bguillaum committed
263
    (* reset the default panes *)
264
    grew_window#vpaned_corpus#misc#show ();
bguillaum's avatar
bguillaum committed
265 266 267
    grew_window#err_view_scroll#misc#hide ();
    grew_window#vpane_right#set_position 30;
    grew_window#btn_show_module#set_active false;
268 269
    grew_window#vpaned_corpus#set_position 30;
    grew_window#btn_show_corpus#set_active false;
bguillaum's avatar
bguillaum committed
270 271 272
    grew_window#vpaned_left#set_position 30;
    grew_window#btn_show_grs#set_active false in

273
  let error_handling fct arg =
274 275 276
    begin
      try fct arg
      with
277 278 279
      | Libgrew.Error msg -> show_error msg
      | Libgrew.Bug msg -> show_error (sprintf "Libgrew.bug, please report: %s" msg)
      | exc -> show_error (sprintf "Uncaught exception, please report: %s" (Printexc.to_string exc))
280 281
    end;
    refresh_error () in
bguillaum's avatar
bguillaum committed
282 283 284

  (** CALLBACKS *)

Bruno Guillaume's avatar
Bruno Guillaume committed
285
  let strat_list = ref [] in
bguillaum's avatar
bguillaum committed
286

287
  let refresh_btn_run () =
Bruno Guillaume's avatar
Bruno Guillaume committed
288
    match (!Resources.current_grs, !Resources.current_gr, !strat_list) with
bguillaum's avatar
bguillaum committed
289 290
    | (Some _, Some _, _::_) -> grew_window#btn_run#misc#set_sensitive true
    | _ -> grew_window#btn_run#misc#set_sensitive false in
291

292 293
  let _ = (fst combo_box_text)#connect#changed
    (fun () ->
Bruno Guillaume's avatar
Bruno Guillaume committed
294 295
    try
      let name = List.nth !strat_list (fst combo_box_text)#active in
296
      grew_window#strat#set_text name;
Bruno Guillaume's avatar
Bruno Guillaume committed
297
    with _ -> ()) in
298

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  let nc = grew_window#statusbar#new_context ~name:"context" in

  let _ = grew_window#strat#connect#changed
      (fun () ->
        ignore (nc#pop () );
        let strat = grew_window#strat#text in
        match !Resources.current_grs with
          | Some grs ->
            begin
              try
                match (Rewrite.at_least_one ~grs ~strat, Rewrite.at_most_one ~grs ~strat) with
                | (true, true) -> grew_window#btn_run#misc#set_sensitive true
                | (false, true) ->
                  grew_window#btn_run#misc#set_sensitive false;
                  ignore (nc#push "The current strategy may not be productive (cannot be used in GUI)")
                | (_, false) ->
                  grew_window#btn_run#misc#set_sensitive false;
                  ignore (nc#push "The current strategy is not deterministic (cannot be used in GUI)")
              with Error s ->
                grew_window#btn_run#misc#set_sensitive false;
                ignore (nc#push ("Cannot parse strategy: "^s))
            end
          | None -> ()


      ) in

326
  (* -------------------------------------------------------------------------------- *)
327
  let load_gr () =
328
    reset();
329
    error_handling Resources.load_gr ();
330

331 332
    match (!Resources.current_gr, !Resources.current_gr_file) with
    | (Some graph, Some gr_file) ->
333 334 335 336 337 338
        grew_window#graph_label#set_label (Filename.basename gr_file);
        Grew_rew_display.graph_map := [("init", (graph, ("", "", None)))];
        Grew_rew_display.current_top_graph := "init";
        let domain = Resources.domain () in
        let svg_file =
          if grew_window#btn_gr_top_dot#active
bguillaum's avatar
bguillaum committed
339 340
          then Grew_rew_display.svg_dot_temp_file ?domain ~main_feat graph
          else Grew_rew_display.svg_dep_temp_file ?domain ~main_feat graph in
341
        grew_window#vpaned_corpus#misc#show ();
342
        grew_window#err_view_scroll#misc#hide ();
343
        graph_top_webkit#load_uri ("file://"^svg_file)
Bruno Guillaume's avatar
Bruno Guillaume committed
344
    | (_, Some file) ->
345 346 347
        grew_window#graph_label#set_label ("<span color=\"red\">"^(Filename.basename file)^"</span>")
    | _ ->
        grew_window#graph_label#set_label "No graph loaded" in
348 349

  let refresh_gr () =
350 351 352 353 354 355 356
    (match !Resources.current_gr_file with
      | None -> ()
      | Some gr_file ->
        error_handling load_gr ();
        refresh_btn_run ()
    );
    refresh_error () in
357

bguillaum's avatar
bguillaum committed
358

bguillaum's avatar
bguillaum committed
359
  (* -------------------------------------------------------------------------------- *)
bguillaum's avatar
bguillaum committed
360
  let gr_or_conll_filter = GFile.filter ~name:"Graph *.(gr|conll|melt)" ~patterns:["*.gr"; "*.conll"; "*.melt"] () in
bguillaum's avatar
bguillaum committed
361 362 363 364

  (* click on the gr file name *)
  let _ = grew_window#graph_button#connect#clicked
    (fun () ->
bguillaum's avatar
bguillaum committed
365
      match ask_for_file_to_open gr_or_conll_filter grew_window#toplevel with
bguillaum's avatar
bguillaum committed
366
        | None -> ()
367 368 369 370
        | Some f ->
          Resources.current_gr_file := Some f;
          error_handling load_gr ();
          refresh_btn_run ()
bguillaum's avatar
bguillaum committed
371 372
    ) in

373

bguillaum's avatar
bguillaum committed
374
  (* click on the gr refresh button *)
375 376 377 378 379
  let _ = grew_window#btn_refresh_gr#connect#clicked
    ~callback: (fun () ->
      messages := [];
      refresh_gr ()
    ) in
bguillaum's avatar
bguillaum committed
380 381

  (* -------------------------------------------------------------------------------- *)
Bruno Guillaume's avatar
Bruno Guillaume committed
382
  let load_grs ?strat () =
bguillaum's avatar
bguillaum committed
383
    reset ();
384 385 386 387 388
    error_handling Resources.load_grs ();
    begin
    match (!Resources.current_grs, !Resources.current_grs_file) with
    | (Some grs, Some file) ->
        grew_window#grs_label#set_label (Filename.basename file);
389

Bruno Guillaume's avatar
Bruno Guillaume committed
390 391
        (* update global var [strat_list] *)
        strat_list := Grs.get_strat_list grs;
bguillaum's avatar
bguillaum committed
392

Bruno Guillaume's avatar
Bruno Guillaume committed
393
        (* remove strat list in combo box *)
bguillaum's avatar
bguillaum committed
394 395
        (fst (snd combo_box_text))#clear ();

Bruno Guillaume's avatar
Bruno Guillaume committed
396 397 398
        (* update combo box *)
        List.iter (fun s -> GEdit.text_combo_add combo_box_text s) !strat_list;

bguillaum's avatar
bguillaum committed
399
        begin
Bruno Guillaume's avatar
Bruno Guillaume committed
400 401 402 403 404 405 406 407 408
          match strat with
          | None -> ()
          | Some strat ->
            grew_window#strat#set_text strat;
              begin
              match List_.index strat !strat_list with
              | None -> ()
              | Some i -> (fst combo_box_text)#set_active i
              end
409 410 411 412 413 414
        end
    | (None, Some file) ->
        grew_window#grs_label#set_label ("<span color=\"red\">"^(Filename.basename file)^"</span>")
    | _ ->
      grew_window#grs_label#set_label "No Grs loaded"
    end;
bguillaum's avatar
bguillaum committed
415

416
    update_features () in
417

bguillaum's avatar
bguillaum committed
418 419 420
  (* end: load_grs *)
  (* -------------------------------------------------------------------------------- *)

bguillaum's avatar
bguillaum committed
421 422 423



bguillaum's avatar
bguillaum committed
424 425 426 427
  (* click on the grs file name *)
  let grs_filter = GFile.filter ~name:"Graph Rewriting System (*.grs)" ~patterns:["*.grs"] () in
  let _ = grew_window#grs_button#connect#clicked
    (fun () ->
bguillaum's avatar
bguillaum committed
428
      match ask_for_file_to_open grs_filter grew_window#toplevel with
bguillaum's avatar
bguillaum committed
429 430
        | None -> ()
        | Some new_grs ->
431 432
          Resources.current_grs_file := Some new_grs;
          error_handling load_grs ();
433
          refresh_gr ();
bguillaum's avatar
bguillaum committed
434 435 436
    ) in

  (* click on the grs refresh button *)
437 438
  let _ = grew_window#btn_refresh_grs#connect#clicked
    ~callback: (fun () ->
439 440 441 442
      messages := [];
      error_handling load_grs ();
      refresh_gr ()
    ) in
bguillaum's avatar
bguillaum committed
443 444 445


  let check_positions () =
446 447 448
    if (grew_window#vpaned_corpus#position < 30)
    then (grew_window#vpaned_corpus#set_position 30;
          grew_window#btn_show_corpus#set_active false);
bguillaum's avatar
bguillaum committed
449 450 451 452 453 454 455 456 457

    if (grew_window#vpaned_left#position < 30)
    then (grew_window#vpaned_left#set_position 30;
          grew_window#btn_show_grs#set_active false);

    if (grew_window#vpane_right#position < 30)
    then (grew_window#vpane_right#set_position 30;
          grew_window#btn_show_module#set_active false) in

458
  let _ = grew_window#btn_show_corpus#connect#clicked
bguillaum's avatar
bguillaum committed
459 460
    ~callback:
    (fun () ->
461 462 463
      if grew_window#btn_show_corpus#active
      then grew_window#vpaned_corpus#set_position 250
      else (grew_window#vpaned_corpus#set_position 30; check_positions ())
bguillaum's avatar
bguillaum committed
464 465
    ) in

466
  let _ = grew_window#vpaned_corpus#event#connect#button_release
bguillaum's avatar
bguillaum committed
467 468
    ~callback:
    (fun b ->
469 470 471 472
      if (grew_window#vpaned_corpus#position > 30)
      then (grew_window#btn_show_corpus#set_active true)
      else (grew_window#vpaned_corpus#set_position 30;
            grew_window#btn_show_corpus#set_active false);
bguillaum's avatar
bguillaum committed
473 474 475 476 477 478 479 480
      check_positions ();
      false
    ) in

  let _ = grew_window#btn_show_grs#connect#clicked
    ~callback:
    (fun () ->
      if grew_window#btn_show_grs#active
bguillaum's avatar
bguillaum committed
481
      then grew_window#vpaned_left#set_position 400
bguillaum's avatar
bguillaum committed
482 483 484 485 486 487 488
      else (grew_window#vpaned_left#set_position 30; check_positions ())
    ) in

  let _ = grew_window#vpaned_left#event#connect#button_release
    ~callback:
    (fun _ ->
      if (grew_window#vpaned_left#position > 30)
bguillaum's avatar
bguillaum committed
489
      then grew_window#btn_show_grs#set_active true
bguillaum's avatar
bguillaum committed
490 491 492 493 494 495 496 497 498 499
      else (grew_window#vpaned_left#set_position 30;
            grew_window#btn_show_grs#set_active false);
      check_positions ();
      false
    ) in

  let _ = grew_window#btn_show_module#connect#clicked
    ~callback:
    (fun () ->
      if grew_window#btn_show_module#active
bguillaum's avatar
bguillaum committed
500
      then grew_window#vpane_right#set_position 400
bguillaum's avatar
bguillaum committed
501 502 503 504 505 506 507
      else (grew_window#vpane_right#set_position 30; check_positions ())
    ) in

  let _ = grew_window#vpane_right#event#connect#button_release
    ~callback:
    (fun b ->
      if (grew_window#vpane_right#position > 30)
bguillaum's avatar
bguillaum committed
508
      then grew_window#btn_show_module#set_active true
bguillaum's avatar
bguillaum committed
509 510 511 512 513 514 515 516 517
      else (grew_window#vpane_right#set_position 30;
            grew_window#btn_show_module#set_active false);
      check_positions ();
      false
    ) in

  let _ =  grew_window#toplevel#connect#destroy ~callback:(GMain.quit) in

  let _ =
bguillaum's avatar
bguillaum committed
518
    grew_window#btn_run#connect#clicked
bguillaum's avatar
bguillaum committed
519 520 521
      ~callback:
      (fun () ->
        try
522
          let rew_display = Resources.rewrite grew_window#strat#text in
bguillaum's avatar
bguillaum committed
523 524 525
          let fl = ref "G0" in
          grew_window#vpane_right#set_position 30;
          grew_window#btn_show_module#set_active false;
526 527
          grew_window#vpaned_corpus#set_position 30;
          grew_window#btn_show_corpus#set_active false;
bguillaum's avatar
bguillaum committed
528 529 530 531 532 533 534 535 536 537 538 539 540
          grew_window#btn_show_grs#set_active true;
          graph_top_webkit#load_html_string empty_html "";
          graph_bottom_webkit#load_html_string empty_html "";
          module_webkit#load_html_string empty_html "";

          if (grew_window#vpaned_left#position <= 30)
          then (grew_window#vpaned_left#set_position 250);

          let (fleaf,file_svg) = Grew_rew_display.rew_display_to_svg rew_display in
          fl := fleaf;
          Grew_rew_display.transform ~show_bottom:true file_svg (file_svg^".trans.svg") !fl;
          grs_webkit#load_uri ("file://"^file_svg^".trans.svg");
          Log.debug ("[Grew_gtk] file://"^file_svg^".trans.svg");
541
          grew_window#vpaned_corpus#misc#show ();
bguillaum's avatar
bguillaum committed
542 543 544 545 546
          grew_window#err_view_scroll#misc#hide ();
          grs_webkit#execute_script("alert('showOnTop2::G0')");
          grs_webkit#execute_script("alert('showOnBottom2::"^(!fl)^"')");
        with
          | Resources.Cannot_rewrite msg -> show_error msg
547 548 549
          | Libgrew.Error msg -> show_error msg
          | Libgrew.Bug msg -> show_error (sprintf "Libgrew.bug, please report: %s" msg)
          | exc -> show_error (sprintf "Uncaught exception, please report: %s" (Printexc.to_string exc))
bguillaum's avatar
bguillaum committed
550 551 552 553 554 555
      ) in

  (** CLICK ON SVG GRAPHS *)
  let _ = grs_webkit#connect#script_alert
    ~callback:
    (fun _ msg ->
bguillaum's avatar
bguillaum committed
556
      let domain = Resources.domain () in
bguillaum's avatar
bguillaum committed
557 558 559 560
      match Str.split (Str.regexp "::") msg with
       | ["showOnBottom"; graph] ->
          let svg_file =
            if grew_window#btn_gr_bottom_dot#active
561
            then (Grew_rew_display.get_dot_graph_with_background ?domain
bguillaum's avatar
bguillaum committed
562
                   ~main_feat ~botop:(true,false) graph)
563
            else (Grew_rew_display.get_dep_graph_with_background ?domain ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
564
                   ~main_feat ~botop:(true,false) graph) in
bguillaum's avatar
bguillaum committed
565 566 567 568 569 570 571 572 573 574 575 576
          graph_bottom_webkit#load_uri ("file://"^svg_file);
          Grew_rew_display.current_bottom_graph := graph;
          module_webkit#load_html_string empty_html "";
          grs_webkit#execute_script
           "if (get_edge_flag()) { remove_back_from_current_top(); hide_current_edge(); current_edge_two='qsd';current_top_graph='';current_bottom_graph=''; alert('removeTop'); }";
          grs_webkit#execute_script "set_edge_flag(false)";
          grew_window#btn_show_module#set_active false;
          grew_window#vpane_right#set_position 30;
          true
        | ["showOnTop"; graph] ->
          let svg_file =
            if grew_window#btn_gr_top_dot#active
577
            then (Grew_rew_display.get_dot_graph_with_background ?domain
bguillaum's avatar
bguillaum committed
578
                    ~main_feat ~botop:(false,true) graph)
579
            else (Grew_rew_display.get_dep_graph_with_background ?domain ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
580
                    ~main_feat ~botop:(false,true) graph) in
bguillaum's avatar
bguillaum committed
581 582 583 584 585 586 587 588 589 590 591
          graph_top_webkit#load_uri ("file://"^svg_file);
          Grew_rew_display.current_top_graph := graph;
          module_webkit#load_html_string empty_html "";
          grs_webkit#execute_script "if (get_edge_flag()) { remove_back_from_current_bottom(); hide_current_edge(); current_edge_two='qsd';current_top_graph='';current_bottom_graph=''; alert('removeBottom'); }";
          grs_webkit#execute_script "set_edge_flag(false)";
          grew_window#btn_show_module#set_active false;
          grew_window#vpane_right#set_position 30;
          true
        | ["showOnBottom2"; graph] ->
          let svg_file =
            if grew_window#btn_gr_bottom_dot#active
592
            then (Grew_rew_display.get_dot_graph_with_background ?domain
bguillaum's avatar
bguillaum committed
593
                    ~main_feat ~botop:(true,false) graph)
594
            else (Grew_rew_display.get_dep_graph_with_background ?domain ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
595
                    ~main_feat ~botop:(true,false) graph) in
bguillaum's avatar
bguillaum committed
596 597 598 599 600
          graph_bottom_webkit#load_uri ("file://"^svg_file);
          Grew_rew_display.current_bottom_graph := graph;
          true
        | ["showOnTop2"; graph] ->
          let svg_file = if grew_window#btn_gr_top_dot#active
601
            then (Grew_rew_display.get_dot_graph_with_background ?domain
bguillaum's avatar
bguillaum committed
602
                    ~main_feat ~botop:(false,true) graph)
603
            else (Grew_rew_display.get_dep_graph_with_background ?domain ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
604
                    ~main_feat ~botop:(false,true) graph) in
bguillaum's avatar
bguillaum committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
          graph_top_webkit#load_uri ("file://"^svg_file);
          Grew_rew_display.current_top_graph := graph;
          true
        | ["showModuleFromGraph"; graph] ->
          if (grew_window#vpane_right#position <= 30)
          then (grew_window#vpane_right#set_position 250);
          let svg_file = Grew_rew_display.get_big_step_for graph in
          module_webkit#load_uri ("file://"^svg_file);
          grs_webkit#execute_script "set_edge_flag(true)";
          grew_window#btn_show_module#set_active true;
          true
        | ["removeTop"] ->
          graph_top_webkit#load_html_string empty_html "";
          true
        | ["removeBottom"] ->
          graph_bottom_webkit#load_html_string empty_html "";
          true
        | _ -> false
bguillaum's avatar
bguillaum committed
623 624 625 626 627 628 629
    ) in

  let click_marker1 = ref false
  and click_marker2 = ref false in

  let _ = module_webkit#connect#script_alert
    ~callback:(fun _ msg ->
bguillaum's avatar
bguillaum committed
630
      let domain = Resources.domain () in
bguillaum's avatar
bguillaum committed
631 632 633 634 635 636
      match Str.split (Str.regexp "::") msg with
        | ["showOnBottom"; graph]
        | ["showOnBottom2"; graph] ->
          if !click_marker1
          then click_marker1 := false
          else
bguillaum's avatar
bguillaum committed
637 638 639
            begin
              let svg_file =
                if grew_window#btn_gr_bottom_dot#active
640
                then (Grew_rew_display.get_dot_graph_with_background2 ?domain
bguillaum's avatar
bguillaum committed
641
                        ~main_feat ~botop:(true,false) (graph^".2"))
642
                else (Grew_rew_display.get_dep_graph_with_background2 ?domain ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
643
                        ~main_feat ~botop:(true,false) (graph^".2")) in
bguillaum's avatar
bguillaum committed
644 645 646
              graph_bottom_webkit#load_uri ("file://"^svg_file);
              Grew_rew_display.current_bottom_graph := (graph^".2");
              grs_webkit#execute_script "remove_back_from_current_bottom()";
bguillaum's avatar
bguillaum committed
647
            end;
bguillaum's avatar
bguillaum committed
648
          true
bguillaum's avatar
bguillaum committed
649 650 651 652 653
        | ["showOnTop"; graph]
        | ["showOnTop2"; graph] ->
          if !click_marker2
          then click_marker2 := false
          else
bguillaum's avatar
bguillaum committed
654 655 656
            begin
              let svg_file =
                if grew_window#btn_gr_top_dot#active
657
                then (Grew_rew_display.get_dot_graph_with_background2 ?domain
bguillaum's avatar
bguillaum committed
658
                        ~main_feat ~botop:(false,true) (graph^".2"))
659
                else (Grew_rew_display.get_dep_graph_with_background2 ?domain ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
660
                        ~main_feat ~botop:(false,true) (graph^".2")) in
bguillaum's avatar
bguillaum committed
661 662 663
              graph_top_webkit#load_uri ("file://"^svg_file);
              Grew_rew_display.current_top_graph := (graph^".2");
              grs_webkit#execute_script "remove_back_from_current_top()";
bguillaum's avatar
bguillaum committed
664
            end;
bguillaum's avatar
bguillaum committed
665
          true
bguillaum's avatar
bguillaum committed
666
        | ["showModuleFromGraph"; graph] ->
bguillaum's avatar
bguillaum committed
667 668 669
          let top_dot = grew_window#btn_gr_top_dot#active
          and bottom_dot = grew_window#btn_gr_bottom_dot#active in
          let (svg_file_top,svg_file_bottom,graph_top,doc_file) =
670
            Grew_rew_display.get_rule_for ?domain
bguillaum's avatar
bguillaum committed
671
              ~main_feat top_dot bottom_dot (graph^".2") in
bguillaum's avatar
bguillaum committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

          graph_top_webkit#load_uri ("file://"^svg_file_top);
          graph_bottom_webkit#load_uri ("file://"^svg_file_bottom);

          Grew_rew_display.current_top_graph := (graph_top);
          Grew_rew_display.current_bottom_graph := (graph^".2");
          click_marker1 := true;
          click_marker2 := true;
          true
        | _ -> false
    ) in

  (* -------------------------------------------------------------------------------- *)
  (** SWAP BETWEEN DEP AND DOT *)
  let _ =  grew_window#btn_gr_bottom_dep#connect#clicked
    ~callback:
    (fun () ->
bguillaum's avatar
bguillaum committed
689
      let domain = Resources.domain () in
bguillaum's avatar
bguillaum committed
690 691 692 693 694 695
      grew_window#btn_gr_bottom_dot#set_active (not grew_window#btn_gr_bottom_dep#active);
      if (grew_window#btn_gr_bottom_dep#active) && (!Grew_rew_display.current_bottom_graph <> "")
      then
        begin
          Log.fdebug "[Grew_gtk] Try to display dep for '%s'" !Grew_rew_display.current_bottom_graph;
          let svg_file =
696
            try Grew_rew_display.get_dep_graph_with_background ?domain
bguillaum's avatar
bguillaum committed
697
                  ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
698
                  ~main_feat
bguillaum's avatar
bguillaum committed
699 700 701
                  ~botop:(true,false)
                  !Grew_rew_display.current_bottom_graph
            with Not_found ->
702
              Grew_rew_display.get_dep_graph_with_background2 ?domain
bguillaum's avatar
bguillaum committed
703
                ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
704
                ~main_feat
bguillaum's avatar
bguillaum committed
705 706 707 708 709 710 711 712
                ~botop:(true,false) !Grew_rew_display.current_bottom_graph in
          graph_bottom_webkit#load_uri ("file://"^svg_file);
        end
    ) in

  let _ = grew_window#btn_gr_top_dep#connect#clicked
    ~callback:
    (fun () ->
bguillaum's avatar
bguillaum committed
713
      let domain = Resources.domain () in
bguillaum's avatar
bguillaum committed
714 715 716 717 718 719
      grew_window#btn_gr_top_dot#set_active (not grew_window#btn_gr_top_dep#active);
      if (grew_window#btn_gr_top_dep#active) && (!Grew_rew_display.current_top_graph <> "")
      then
        begin
          Log.fdebug "[Grew_gtk] Try to display dep for '%s'" !Grew_rew_display.current_top_graph;
          let svg_file =
720
            try Grew_rew_display.get_dep_graph_with_background ?domain
bguillaum's avatar
bguillaum committed
721
                  ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
722
                  ~main_feat
bguillaum's avatar
bguillaum committed
723
                  ~botop:(false,true) !Grew_rew_display.current_top_graph
724
            with Not_found -> Grew_rew_display.get_dep_graph_with_background2 ?domain
bguillaum's avatar
bguillaum committed
725
              ~filter:(get_current_filter ())
bguillaum's avatar
bguillaum committed
726
              ~main_feat
bguillaum's avatar
bguillaum committed
727 728 729 730 731 732 733 734
              ~botop:(false,true) !Grew_rew_display.current_top_graph in
          graph_top_webkit#load_uri ("file://"^svg_file);
        end
    ) in

  let _ = grew_window#btn_gr_bottom_dot#connect#clicked
    ~callback:
    (fun () ->
bguillaum's avatar
bguillaum committed
735
      let domain = Resources.domain () in
bguillaum's avatar
bguillaum committed
736 737 738 739 740 741
      grew_window#btn_gr_bottom_dep#set_active (not grew_window#btn_gr_bottom_dot#active);
      if (grew_window#btn_gr_bottom_dot#active) && (!Grew_rew_display.current_bottom_graph <> "")
      then
        begin
          Log.fdebug "[Grew_gtk] Try to display dot for '%s'" !Grew_rew_display.current_bottom_graph;
          let svg_file =
742
            try Grew_rew_display.get_dot_graph_with_background ?domain
bguillaum's avatar
bguillaum committed
743
                  ~main_feat
bguillaum's avatar
bguillaum committed
744
                  ~botop:(true,false) !Grew_rew_display.current_bottom_graph
745
            with Not_found -> Grew_rew_display.get_dot_graph_with_background2 ?domain
bguillaum's avatar
bguillaum committed
746
              ~main_feat
bguillaum's avatar
bguillaum committed
747 748 749 750 751
              ~botop:(true,false) !Grew_rew_display.current_bottom_graph in
          graph_bottom_webkit#load_uri ("file://"^svg_file);
        end
    ) in

bguillaum's avatar
bguillaum committed
752
  (* XXX *)
bguillaum's avatar
bguillaum committed
753 754 755
  let _ = grew_window#btn_gr_top_dot#connect#clicked
    ~callback:
    (fun () ->
bguillaum's avatar
bguillaum committed
756
      let domain = Resources.domain () in
bguillaum's avatar
bguillaum committed
757 758 759 760 761 762
      grew_window#btn_gr_top_dep#set_active (not grew_window#btn_gr_top_dot#active);
      if (grew_window#btn_gr_top_dot#active) && (!Grew_rew_display.current_top_graph <> "")
      then
        begin
          Log.fdebug "[Grew_gtk] Try to display dot for '%s'" !Grew_rew_display.current_top_graph;
          let svg_file =
763
            try Grew_rew_display.get_dot_graph_with_background ?domain
bguillaum's avatar
bguillaum committed
764
                  ?deco:!Grew_rew_display.current_top_deco
bguillaum's avatar
bguillaum committed
765
                  ~main_feat
bguillaum's avatar
bguillaum committed
766
                  ~botop:(false,true) !Grew_rew_display.current_top_graph
767
            with Not_found -> Grew_rew_display.get_dot_graph_with_background2 ?domain
bguillaum's avatar
bguillaum committed
768
              ~main_feat
bguillaum's avatar
bguillaum committed
769 770 771 772 773 774 775 776 777 778 779
              ~botop:(false,true) !Grew_rew_display.current_top_graph in
          graph_top_webkit#load_uri ("file://"^svg_file);
        end
    ) in


  (* -------------------------------------------------------------------------------- *)
  (** ZOOMS *)
  let _ = grew_window#grs_zoom#connect#value_changed
    ~callback:
    (fun () ->
bguillaum's avatar
bguillaum committed
780
      grs_webkit#set_zoom_level (grew_window#grs_zoom#adjustment#value /. 100.)
bguillaum's avatar
bguillaum committed
781 782 783
    ) in

  let _ = grew_window#graph_top_zoom#connect#value_changed
bguillaum's avatar
bguillaum committed
784 785
    ~callback:
    (fun () ->
bguillaum's avatar
bguillaum committed
786
      graph_top_webkit#set_zoom_level (grew_window#graph_top_zoom#adjustment#value /. 100.)
bguillaum's avatar
bguillaum committed
787
    ) in
bguillaum's avatar
bguillaum committed
788 789 790 791

  let _ = grew_window#graph_bottom_zoom#connect#value_changed
    ~callback:
    (fun () ->
bguillaum's avatar
bguillaum committed
792
      graph_bottom_webkit#set_zoom_level (grew_window#graph_bottom_zoom#adjustment#value /. 100.)
bguillaum's avatar
bguillaum committed
793 794 795 796 797
    ) in

  let _ = grew_window#module_zoom#connect#value_changed
    ~callback:
    (fun () ->
bguillaum's avatar
bguillaum committed
798
      module_webkit#set_zoom_level (grew_window#module_zoom#adjustment#value /. 100.)
bguillaum's avatar
bguillaum committed
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
    ) in

  (* -------------------------------------------------------------------------------- *)
  (** FULLSCREEN *)
  let fullscreen = ref false in
  let _ = grew_window#btn_enter_fullscreen#connect#clicked
    ~callback: (fun () -> grew_window#toplevel#fullscreen (); fullscreen := true) in

  let _ = grew_window#btn_leave_fullscreen#connect#clicked
    ~callback: (fun () -> grew_window#toplevel#unfullscreen (); fullscreen := false) in

  (* cpt is used to avoid a loop between the two propagations of value_changed *)
  let cpt = ref 0 in
  let _ = GMain.Timeout.add ~ms:50 ~callback:(fun () -> cpt := 0; true) in

bguillaum's avatar
bguillaum committed
814
  let _ = grew_window#graph_bottom#hadjustment#connect#value_changed
bguillaum's avatar
bguillaum committed
815 816 817 818 819
    ~callback:
    (fun () ->
      if (!cpt<1 && grew_window#synchronize#active)
      then
        begin
bguillaum's avatar
bguillaum committed
820 821
          let percent = ((grew_window#graph_bottom#hadjustment#value +. (grew_window#graph_bottom#hadjustment#page_size /. 2.)) /. grew_window#graph_bottom#hadjustment#upper) in
          let value = percent *. grew_window#graph_top#hadjustment#upper -. (grew_window#graph_top#hadjustment#page_size /. 2.) in
bguillaum's avatar
bguillaum committed
822
          incr cpt;
bguillaum's avatar
bguillaum committed
823
          grew_window#graph_top#hadjustment#set_value value;
bguillaum's avatar
bguillaum committed
824 825 826
        end
    ) in

bguillaum's avatar
bguillaum committed
827
  let _ = grew_window#graph_top#hadjustment#connect#value_changed
bguillaum's avatar
bguillaum committed
828 829 830 831
    ~callback:
    (fun () ->
      if (!cpt<1 && grew_window#synchronize#active)
      then
bguillaum's avatar
bguillaum committed
832 833
        (let percent = ((grew_window#graph_top#hadjustment#value +. (grew_window#graph_top#hadjustment#page_size /. 2.)) /. grew_window#graph_top#hadjustment#upper) in
         let value = percent *. grew_window#graph_bottom#hadjustment#upper -. (grew_window#graph_bottom#hadjustment#page_size /. 2.) in
bguillaum's avatar
bguillaum committed
834
         incr cpt;
bguillaum's avatar
bguillaum committed
835
         grew_window#graph_bottom#hadjustment#set_value value;
bguillaum's avatar
bguillaum committed
836 837 838
        )
    ) in

bguillaum's avatar
bguillaum committed
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
  (* ==================== Contextual menu to export ==================== *)
  let save extension graph save_function () =
    let filter = GFile.filter ~name:("*."^extension) ~patterns:["*."^extension] () in
    match ask_for_file_to_save filter grew_window#toplevel with
      | None -> ()
      | Some filename -> save_function graph filename in

  (* -------------------------------------------------------------------------------- *)
  let view fct graph () =
    let text = fct graph in
    let sv = new src_viewer () in
    sv#source#buffer#set_text text;
    ignore(sv#toplevel#connect#destroy ~callback:sv#toplevel#destroy);
    ignore(sv#close#connect#clicked ~callback:sv#toplevel#destroy);
    sv#check_widgets ();
    sv#toplevel#show ();
    () in

  let contextual_menu side ev =
    if GdkEvent.Button.button ev <> 3
    then false (* we did not handle this *)
    else
bguillaum's avatar
bguillaum committed
861
      let domain = Resources.domain () in
bguillaum's avatar
bguillaum committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
      let graph = match side with
        | Top -> !Grew_rew_display.current_top_graph
        | Bottom -> !Grew_rew_display.current_bottom_graph in
      if graph = ""
      then true
      else begin
        let dot =
          match side with
          | Top -> grew_window#btn_gr_top_dot#active
          | Bottom -> grew_window#btn_gr_bottom_dot#active in
        let deco =
          match side with
          | Top -> !Grew_rew_display.current_top_deco
          | Bottom -> !Grew_rew_display.current_bottom_deco in

        (* create the contextual menu *)
        let menu = GMenu.menu () in
        let add_item (label,callback) =
          let menuitem = GMenu.menu_item ~label ~packing:menu#append () in
          ignore (menuitem#connect#activate ~callback) in
        (* build save items and put them in the menu *)
        let save_items =
884 885
          ("Save as gr", save "gr" graph (Grew_rew_display.to_grfile_graph ?domain)) ::
          ("Save as conll", save "conll" graph (Grew_rew_display.save_conll_graph ?domain)) ::
bguillaum's avatar
bguillaum committed
886 887
          ("Save as pdf",
            if dot
888 889
            then save "pdf" graph (Grew_rew_display.to_pdf_dotfile_graph ?domain ?deco ~main_feat)
            else save "pdf" graph (Grew_rew_display.to_pdf_depfile_graph ?domain ?deco ~main_feat)
bguillaum's avatar
bguillaum committed
890 891 892
          ) ::
          ("Save as svg",
            if dot
893 894
            then save "svg" graph (Grew_rew_display.to_svg_dotfile_graph ?domain ?deco ~main_feat)
            else save "svg" graph (Grew_rew_display.to_svg_depfile_graph ?domain ?deco ~main_feat)
bguillaum's avatar
bguillaum committed
895
          ) ::
896
          ("Save as png", save "png" graph (Grew_rew_display.to_pngfile_graph ?domain ?deco ~main_feat)) ::
bguillaum's avatar
bguillaum committed
897
          (if dot
898 899
           then ["Save as dot", save "dot" graph (Grew_rew_display.to_dotfile_graph ?domain ?deco ~main_feat)]
           else ["Save as dep", save "dep" graph (Grew_rew_display.to_depfile_graph ?domain ?deco ~main_feat)]
bguillaum's avatar
bguillaum committed
900 901 902 903 904 905
          ) in
        List.iter add_item save_items;
        let _ = GMenu.separator_item ~packing:menu#append () in

        (* build view items and put them in the menu *)
        let view_items =
906 907
          ("View gr", view (Grew_rew_display.to_grstring_graph ?domain) graph) ::
          ("View conll", view (Grew_rew_display.to_conll_graph ?domain) graph) ::
bguillaum's avatar
bguillaum committed
908
          (if dot
909 910
           then ["View dot", view (Grew_rew_display.to_dotstring_graph ?domain ?deco ~main_feat) graph]
           else ["View dep", view (Grew_rew_display.to_depstring_graph ?domain ?deco ~main_feat) graph]
bguillaum's avatar
bguillaum committed
911 912 913 914 915 916 917
          ) in
        List.iter add_item view_items;

        menu#popup ~button:(GdkEvent.Button.button ev) ~time:(GdkEvent.Button.time ev);
      true (* we handled this *)
    end in

bguillaum's avatar
bguillaum committed
918 919 920 921 922 923
  (* Listen to right click in graph view *)
  let _ = grew_window#graph_view_top#event#add [`BUTTON_PRESS] in
  let _ = grew_window#graph_view_top#event#connect#button_press ~callback: (contextual_menu Top) in
  let _ = grew_window#graph_view_bottom#event#add [`BUTTON_PRESS] in
  let _ = grew_window#graph_view_bottom#event#connect#button_press ~callback: (contextual_menu Bottom) in

bguillaum's avatar
bguillaum committed
924
  (* Really start the gui *)
bguillaum's avatar
bguillaum committed
925 926
  grew_window#check_widgets ();
  grew_window#toplevel#show ();
bguillaum's avatar
bguillaum committed
927 928

  (* startup load of grs files (which implies loading of the gr file) *)
929
  Resources.current_grs_file := !Grew_args.grs;
Bruno Guillaume's avatar
Bruno Guillaume committed
930
  load_grs ~strat:!Grew_args.strat ();
931 932 933 934

  Resources.current_gr_file := !Grew_args.gr;
  load_gr ();

935
  refresh_btn_run ();
bguillaum's avatar
bguillaum committed
936
  refresh_error ();
bguillaum's avatar
bguillaum committed
937
  GMain.Main.main ()