gmain.ml 44.2 KB
Newer Older
1 2
(**************************************************************************)
(*                                                                        *)
3
(*  Copyright (C) 2010-2011                                               *)
4 5 6
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)


open Format

let () =
24
  eprintf "[Info] Init the GTK interface...@?";
25 26 27 28
  ignore (GtkMain.Main.init ());
  eprintf " done.@."


29
open Why3
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
open Whyconf
open Gconfig

(************************)
(* parsing command line *)
(************************)

let includes = ref []
let file = ref None
let opt_version = ref false

let spec = Arg.align [
  ("-I",
   Arg.String (fun s -> includes := s :: !includes),
   "<s> add s to loadpath") ;
(*
  ("-f",
   Arg.String (fun s -> input_files := s :: !input_files),
   "<f> add file f to the project (ignored if it is already there)") ;
*)
  ("-v",
   Arg.Set opt_version,
   " print version information") ;
]

let version_msg = sprintf "Why3 IDE, version %s (build date: %s)"
  Config.version Config.builddate

let usage_str = sprintf
  "Usage: %s [options] [<file.why>|<project directory>]"
  (Filename.basename Sys.argv.(0))

let set_file f = match !file with
  | Some _ ->
      raise (Arg.Bad "only one parameter")
  | None ->
      file := Some f

let () = Arg.parse spec set_file usage_str

let () =
  if !opt_version then begin
    printf "%s@." version_msg;
    exit 0
  end

let fname = match !file with
  | None ->
      Arg.usage spec usage_str;
      exit 1
  | Some f ->
      f

83
let (why_lang, any_lang) =
84 85 86 87 88 89 90
  let main = get_main () in
  let load_path = Filename.concat (datadir main) "lang" in
  let languages_manager =
    GSourceView2.source_language_manager ~default:true
  in
  languages_manager#set_search_path
    (load_path :: languages_manager#search_path);
91 92
  let why_lang =
    match languages_manager#language "why" with
93 94 95 96
    | None ->
        Format.eprintf "language file for 'why' not found in directory %s@."
          load_path;
        exit 1
97 98 99 100 101 102
    | Some _ as l -> l in
  let any_lang filename =
    match languages_manager#guess_language ~filename () with
    | None -> why_lang
    | Some _ as l -> l in
  (why_lang, any_lang)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

let source_text fname =
  let ic = open_in fname in
  let size = in_channel_length ic in
  let buf = String.create size in
  really_input ic buf 0 size;
  close_in ic;
  buf

(********************************)
(* loading WhyIDE configuration *)
(********************************)

let gconfig =
  let c = Gconfig.config in
  let loadpath = (Whyconf.loadpath (get_main ())) @ List.rev !includes in
Andrei Paskevich's avatar
Andrei Paskevich committed
119
  c.env <- Env.create_env_of_loadpath loadpath;
120
(*
121 122
  let provers = Whyconf.get_provers c.Gconfig.config in
  c.provers <-
123
    Util.Mstr.fold (Session.get_prover_data c.env) provers Util.Mstr.empty;
124
*)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  c

let () =
  Whyconf.load_plugins (get_main ())



(***************)
(* Main window *)
(***************)

let w = GWindow.window
  ~allow_grow:true ~allow_shrink:true
  ~width:gconfig.window_width
  ~height:gconfig.window_height
MARCHE Claude's avatar
MARCHE Claude committed
140
  ~title:"Why3 Interactive Proof Session" ()
141

MARCHE Claude's avatar
MARCHE Claude committed
142 143 144
let () =
  w#set_icon (Some !Gconfig.why_icon)

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
let (_ : GtkSignal.id) =
  w#misc#connect#size_allocate
    ~callback:
    (fun {Gtk.width=w;Gtk.height=h} ->
       gconfig.window_height <- h;
       gconfig.window_width <- w)

let vbox = GPack.vbox ~packing:w#add ()

(* Menu *)

let menubar = GMenu.menu_bar ~packing:vbox#pack ()

let factory = new GMenu.factory menubar

let accel_group = factory#accel_group

let hb = GPack.hbox ~packing:vbox#add ~border_width:2 ()

let tools_window_vbox =
  try
    GPack.vbox ~packing:(hb#pack ~expand:false) ~border_width:2 ()
  with Gtk.Error _ -> assert false

let context_frame =
  GBin.frame ~label:"Context"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let context_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
    ~packing:context_frame#add ()

let context_unproved_goals_only = ref true

let () =
  let b1 = GButton.radio_button
    ~packing:context_box#add ~label:"Unproved goals" ()
  in
183
  b1#misc#set_tooltip_markup "When selected, tools below are applied only to <b>unproved</b> goals";
184 185 186 187 188 189 190
  let (_ : GtkSignal.id) =
    b1#connect#clicked
      ~callback:(fun () -> context_unproved_goals_only := true)
  in
  let b2 = GButton.radio_button
    ~group:b1#group ~packing:context_box#add ~label:"All goals" ()
  in
191
  b2#misc#set_tooltip_markup "When selected, tools below are applied to all goals";
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  let (_ : GtkSignal.id) =
    b2#connect#clicked
      ~callback:(fun () -> context_unproved_goals_only := false)
  in ()


let provers_frame =
  GBin.frame ~label:"Provers"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let provers_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
  ~packing:provers_frame#add ()

let () = provers_frame#set_resize_mode `PARENT

let transf_frame =
  GBin.frame ~label:"Transformations"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let transf_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
  ~packing:transf_frame#add ()

let tools_frame =
  GBin.frame ~label:"Tools"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let tools_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
  ~packing:tools_frame#add ()

let cleaning_frame =
  GBin.frame ~label:"Cleaning"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let cleaning_box =
  GPack.button_box `VERTICAL ~border_width:5 ~spacing:5
  ~packing:cleaning_frame#add ()

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
let monitor_frame =
  GBin.frame ~label:"Monitor"
    ~packing:(tools_window_vbox#pack ~expand:false) ()

let monitor_box =
  GPack.vbox ~homogeneous:false ~packing:monitor_frame#add ()

let monitor_waiting =
  GMisc.label ~text:"  Waiting: 0" ~packing:monitor_box#add ()

let monitor_scheduled =
  GMisc.label ~text:"Scheduled: 0" ~packing:monitor_box#add ()

let monitor_running =
  GMisc.label ~text:"  Running: 0" ~packing:monitor_box#add ()


249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

(* horizontal paned *)

let hp = GPack.paned `HORIZONTAL ~packing:hb#add ()


(* tree view *)
let scrollview =
  try
    GBin.scrolled_window ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
      ~width:gconfig.tree_width
      ~packing:hp#add ()
  with Gtk.Error _ -> assert false

let () = scrollview#set_shadow_type `ETCHED_OUT
let (_ : GtkSignal.id) =
  scrollview#misc#connect#size_allocate
    ~callback:
    (fun {Gtk.width=w;Gtk.height=_h} ->
       gconfig.tree_width <- w)




(****************)
(* goals widget *)
(****************)

let cols = new GTree.column_list
let name_column = cols#add Gobject.Data.string
let icon_column = cols#add Gobject.Data.gobject
let status_column = cols#add Gobject.Data.gobject
let time_column = cols#add Gobject.Data.string
282
let index_column = cols#add Gobject.Data.int
283

284 285 286 287
let name_renderer = GTree.cell_renderer_text [`XALIGN 0.]
let renderer = GTree.cell_renderer_text [`XALIGN 0.]
let image_renderer = GTree.cell_renderer_pixbuf [ ]
let icon_renderer = GTree.cell_renderer_pixbuf [ ]
288

289 290
let view_name_column =
  GTree.view_column ~title:"Theories/Goals" ()
291

292 293 294 295 296 297
let () =
  view_name_column#pack icon_renderer ;
  view_name_column#add_attribute icon_renderer "pixbuf" icon_column ;
  view_name_column#pack name_renderer;
  view_name_column#add_attribute name_renderer "text" name_column;
  view_name_column#set_resizable true;
298 299 300 301
  view_name_column#set_max_width 800;
(*
  view_name_column#set_alignment 1.0;
*)
302
  ()
303

304 305 306 307
let view_status_column =
  GTree.view_column ~title:"Status"
    ~renderer:(image_renderer, ["pixbuf", status_column])
    ()
308

309 310 311
let view_time_column =
  GTree.view_column ~title:"Time"
    ~renderer:(renderer, ["text", time_column]) ()
312

313 314 315 316 317
let () =
  view_status_column#set_resizable false;
  view_status_column#set_visible true;
  view_time_column#set_resizable false;
  view_time_column#set_visible true
318

319
let goals_model,goals_view =
320
  eprintf "[Info] Creating tree model...@?";
321 322
  let model = GTree.tree_store cols in
  let view = GTree.view ~model ~packing:scrollview#add () in
323
  let () = view#selection#set_mode (* `SINGLE *) `MULTIPLE in
324 325 326 327
  let () = view#set_rules_hint true in
  ignore (view#append_column view_name_column);
  ignore (view#append_column view_status_column);
  ignore (view#append_column view_time_column);
328
  eprintf " done@.";
329
  model,view
330

331 332 333 334
let clear model = model#clear ()

let image_of_result ~obsolete result =
  match result with
MARCHE Claude's avatar
MARCHE Claude committed
335
    | Session.Undone -> !image_undone
336 337
    | Session.Scheduled -> !image_scheduled
    | Session.Running -> !image_running
338
    | Session.Interrupted -> assert false
339 340
    | Session.InternalFailure _ -> !image_failure
    | Session.Done r -> match r.Call_provers.pr_answer with
341 342 343 344 345 346 347 348 349 350 351 352
        | Call_provers.Valid ->
            if obsolete then !image_valid_obs else !image_valid
        | Call_provers.Invalid ->
            if obsolete then !image_invalid_obs else !image_invalid
        | Call_provers.Timeout ->
            if obsolete then !image_timeout_obs else !image_timeout
        | Call_provers.Unknown _ ->
            if obsolete then !image_unknown_obs else !image_unknown
        | Call_provers.Failure _ ->
            if obsolete then !image_failure_obs else !image_failure
        | Call_provers.HighFailure ->
            if obsolete then !image_failure_obs else !image_failure
353

354
(* connecting to the Session model *)
355 356 357

module M = Session.Make
  (struct
358 359 360 361 362 363 364 365
     type key = GTree.row_reference

     let create ?parent () =
       let parent = match parent with
         | None -> None
         | Some r -> Some r#iter
       in
       let iter = goals_model#append ?parent () in
MARCHE Claude's avatar
MARCHE Claude committed
366
       goals_model#set ~row:iter ~column:index_column (-1);
367
       goals_model#get_row_reference (goals_model#get_path iter)
368 369


370
     let remove row =
371
       let (_:bool) = goals_model#remove row#iter in ()
372

MARCHE Claude's avatar
MARCHE Claude committed
373 374
     let reset () = goals_model#clear ()

375
     let idle f =
376
       let (_ : GMain.Idle.id) = GMain.Idle.add f in ()
377

378
     let timeout ~ms f =
379 380 381
       let (_ : GMain.Timeout.id) = GMain.Timeout.add ~ms ~callback:f in
       ()

382 383 384 385 386
     let notify_timer_state t s r =
       monitor_waiting#set_text ("Waiting: " ^ (string_of_int t));
       monitor_scheduled#set_text ("Scheduled: " ^ (string_of_int s));
       monitor_running#set_text ("Running: " ^ (string_of_int r));

387
   end)
MARCHE Claude's avatar
MARCHE Claude committed
388 389 390

let set_row_status row b =
  if b then
391
    goals_model#set ~row:row#iter ~column:status_column !image_yes
MARCHE Claude's avatar
MARCHE Claude committed
392
  else
393
    goals_model#set ~row:row#iter ~column:status_column !image_unknown
MARCHE Claude's avatar
MARCHE Claude committed
394 395 396 397

let set_proof_state ~obsolete a =
  let row = a.M.proof_key in
  let res = a.M.proof_state in
398
  goals_model#set ~row:row#iter ~column:status_column
MARCHE Claude's avatar
MARCHE Claude committed
399 400 401
    (image_of_result ~obsolete res);
  let t = match res with
    | Session.Done { Call_provers.pr_time = time } ->
402
        Format.sprintf "%.2f" time
MARCHE Claude's avatar
MARCHE Claude committed
403 404 405
    | _ -> ""
  in
  let t = if obsolete then t ^ " (obsolete)" else t in
406
  goals_model#set ~row:row#iter ~column:time_column t
MARCHE Claude's avatar
MARCHE Claude committed
407

408 409
let model_index = Hashtbl.create 17

410

411
let get_any_from_iter row =
MARCHE Claude's avatar
MARCHE Claude committed
412
  try
413 414
    let idx = goals_model#get ~row ~column:index_column in
    Hashtbl.find model_index idx
415 416
  with Not_found -> invalid_arg "Gmain.get_any_from_iter"

417
(*
418 419
let get_any (row:Gtk.tree_path) : M.any =
  get_any_from_iter (goals_model#get_iter row)
420 421 422 423 424 425 426 427
*)

let get_any_from_row_reference r = get_any_from_iter r#iter

let get_selected_row_references () =
  List.map
    (fun path -> goals_model#get_row_reference path)
    goals_view#selection#get_selected_rows
428

429
let row_expanded b iter _path =
430
  match get_any_from_iter iter with
431
    | M.File f ->
432
(*
433
        eprintf "file_expanded <- %b@." b;
434
*)
435
        M.set_file_expanded f b
436
    | M.Theory t ->
437
(*
438
        eprintf "theory_expanded <- %b@." b;
439
*)
440
        M.set_theory_expanded t b
441
    | M.Goal g ->
442
(*
443
        eprintf "goal_expanded <- %b@." b;
444
*)
445
        M.set_goal_expanded g b
446
    | M.Transformation tr ->
447
(*
448
        eprintf "transf_expanded <- %b@." b;
449
*)
450
        M.set_transf_expanded tr b
451 452
    | M.Proof_attempt _ -> ()

453

454
let (_:GtkSignal.id) =
455 456
  goals_view#connect#row_collapsed ~callback:(row_expanded false)

457
let (_:GtkSignal.id) =
458
  goals_view#connect#row_expanded ~callback:(row_expanded true)
459

MARCHE Claude's avatar
MARCHE Claude committed
460
let notify any =
461 462
  let row,exp =
    match any with
463
      | M.Goal g ->
464
(*
465 466
          if M.goal_expanded g then
            begin
467
              let n =
468 469 470 471
                Hashtbl.fold (fun _ _ acc -> acc+1) (M.external_proofs g) 0
              in
              eprintf "expand_row on a goal with %d proofs@." n;
            end;
472
*)
473
          (M.goal_key g),(M.goal_expanded g)
474 475 476
      | M.Theory t -> (M.theory_key t),(M.theory_expanded t)
      | M.File f -> f.M.file_key,f.M.file_expanded
      | M.Proof_attempt a -> a.M.proof_key,false
477
      | M.Transformation tr -> tr.M.transf_key,tr.M.transf_expanded
478
  in
479
  if exp then goals_view#expand_to_path row#path else
480
    goals_view#collapse_row row#path;
MARCHE Claude's avatar
MARCHE Claude committed
481 482
  match any with
    | M.Goal g ->
483
        set_row_status row (M.goal_proved g)
MARCHE Claude's avatar
MARCHE Claude committed
484
    | M.Theory th ->
485
        set_row_status row (M.verified th)
MARCHE Claude's avatar
MARCHE Claude committed
486
    | M.File file ->
487
        set_row_status row file.M.file_verified
MARCHE Claude's avatar
MARCHE Claude committed
488
    | M.Proof_attempt a ->
489
        set_proof_state ~obsolete:a.M.proof_obsolete a
MARCHE Claude's avatar
MARCHE Claude committed
490
    | M.Transformation tr ->
491
        set_row_status row tr.M.transf_proved
492

MARCHE Claude's avatar
MARCHE Claude committed
493 494
let init =
  let cpt = ref (-1) in
495
  fun row any ->
MARCHE Claude's avatar
MARCHE Claude committed
496 497 498 499 500 501 502 503 504 505 506
    let ind = goals_model#get ~row:row#iter ~column:index_column in
    if ind < 0 then
      begin
        incr cpt;
        Hashtbl.add model_index !cpt any;
        goals_model#set ~row:row#iter ~column:index_column !cpt
      end
    else
      begin
        Hashtbl.replace model_index ind any;
      end;
507
    (* useless since it has no child: goals_view#expand_row row#path; *)
MARCHE Claude's avatar
MARCHE Claude committed
508
    goals_model#set ~row:row#iter ~column:icon_column
509
      (match any with
510 511 512 513 514
         | M.Goal _ -> !image_file
         | M.Theory _
         | M.File _ -> !image_directory
         | M.Proof_attempt _ -> !image_prover
         | M.Transformation _ -> !image_transf);
MARCHE Claude's avatar
MARCHE Claude committed
515
    goals_model#set ~row:row#iter ~column:name_column
516
      (match any with
517 518 519
         | M.Goal g -> M.goal_expl g
         | M.Theory th -> M.theory_name th
         | M.File f -> Filename.basename f.M.file_name
520
         | M.Proof_attempt a ->
521 522 523
             begin
               match a.M.prover with
                 | M.Detected_prover p ->
524
                     p.Session.prover_name ^ " " ^ p.Session.prover_version
525 526
                 | M.Undetected_prover s -> s
             end
527
         | M.Transformation tr -> Session.transformation_id tr.M.transf);
MARCHE Claude's avatar
MARCHE Claude committed
528
    notify any
MARCHE Claude's avatar
MARCHE Claude committed
529

530 531


532 533 534 535 536 537 538 539 540
(********************)
(* opening database *)
(********************)

let project_dir, file_to_read =
  if Sys.file_exists fname then
    begin
      if Sys.is_directory fname then
        begin
541
          eprintf "[Info] found directory '%s' for the project@." fname;
542 543 544 545
          fname, None
        end
      else
        begin
546
          eprintf "[Info] found regular file '%s'@." fname;
547 548 549 550
          let d =
            try Filename.chop_extension fname
            with Invalid_argument _ -> fname
          in
551
          eprintf "[Info] using '%s' as directory for the project@." d;
552
          d, Some (Filename.concat Filename.parent_dir_name
553
                     (Filename.basename fname))
554 555 556 557 558 559 560 561
        end
    end
  else
    fname, None

let () =
  if not (Sys.file_exists project_dir) then
    begin
562
      eprintf "[Info] '%s' does not exists. Creating directory of that name \
563 564 565 566
 for the project@." project_dir;
      Unix.mkdir project_dir 0o777
    end

567

568 569 570 571 572 573 574 575 576 577 578
let info_window ?(callback=(fun () -> ())) mt s =
  let buttons = match mt with
    | `INFO -> GWindow.Buttons.close
    | `WARNING -> GWindow.Buttons.close
    | `QUESTION -> GWindow.Buttons.ok_cancel
    | `ERROR -> GWindow.Buttons.close
  in
  let d = GWindow.message_dialog
    ~message:s
    ~message_type:mt
    ~buttons
579
    ~title:"Why3IDE"
580
    ~icon:(!Gconfig.why_icon)
581
    ~modal:true
582 583 584 585
    ~show:true ()
  in
  let (_ : GtkSignal.id) =
    d#connect#response
586
      ~callback:(function x ->
587 588
                   d#destroy ();
                   if mt <> `QUESTION || x = `OK then callback ())
589 590
  in ()

591 592 593 594
(* check if provers are present *)
let () =
  if Util.Mstr.is_empty (Whyconf.get_provers gconfig.Gconfig.config) then
    begin
595
      info_window `ERROR "No prover configured.\nPlease run 'why3config --detect-provers' first"
596 597 598 599 600 601 602 603
        ~callback:GMain.quit;
      GMain.main ();
      exit 2;
    end

let () =
  try
    eprintf "[Info] Opening session...@\n@[<v 2>  ";
604 605 606 607 608 609
    let (_:bool) =
      M.open_session ~allow_obsolete:true
        ~env:gconfig.env
        ~config:gconfig.Gconfig.config
        ~init ~notify project_dir
    in
610 611 612 613 614 615 616 617
    M.maximum_running_proofs := gconfig.max_running_processes;
    eprintf "@]@\n[Info] Opening session: done@."
  with e ->
    eprintf "@[Error while opening session:@ %a@.@]"
      Exn_printer.exn_printer e;
    exit 1


618 619 620 621 622 623 624 625 626

(**********************************)
(* add new file from command line *)
(**********************************)

let () =
  match file_to_read with
    | None -> ()
    | Some fn ->
627
        if M.file_exists fn then
628
          eprintf "[Info] file %s already in database@." fn
629 630
        else
          try
MARCHE Claude's avatar
MARCHE Claude committed
631
            M.add_file fn
632
          with e ->
633
            eprintf "@[Error while reading file@ '%s':@ %a@.@]" fn
634
              Exn_printer.exn_printer e;
635
            exit 1
636 637 638 639 640 641 642 643 644 645




(*****************************************************)
(* method: run a given prover on each unproved goals *)
(*****************************************************)

let prover_on_selected_goals pr =
  List.iter
646
    (fun row ->
MARCHE Claude's avatar
MARCHE Claude committed
647
      try
648
       let a = get_any_from_row_reference row in
MARCHE Claude's avatar
MARCHE Claude committed
649 650 651 652 653 654 655
       M.run_prover
         ~context_unproved_goals_only:!context_unproved_goals_only
         ~timelimit:gconfig.time_limit
         pr a
      with e ->
        eprintf "@[Exception raised while running a prover:@ %a@.@]"
          Exn_printer.exn_printer e)
656
    (get_selected_row_references ())
657 658 659 660 661 662 663

(**********************************)
(* method: replay obsolete proofs *)
(**********************************)

let replay_obsolete_proofs () =
  List.iter
664 665
    (fun r ->
       let a = get_any_from_row_reference r in
MARCHE Claude's avatar
MARCHE Claude committed
666
       M.replay ~obsolete_only:true
667
         ~context_unproved_goals_only:!context_unproved_goals_only a)
668
    (get_selected_row_references ())
669

670 671 672 673 674 675 676 677 678 679
(***********************************)
(* method: mark proofs as obsolete *)
(***********************************)

let cancel_proofs () =
  List.iter
    (fun r ->
       let a = get_any_from_row_reference r in
       M.cancel a)
    (get_selected_row_references ())
680 681 682 683 684 685


(*****************************************************)
(* method: split selected goals *)
(*****************************************************)

686 687 688 689
let lookup_trans = Session.lookup_transformation gconfig.env
let split_transformation = lookup_trans "split_goal"
let inline_transformation = lookup_trans "inline_goal"
let intro_transformation = lookup_trans "introduce_premises"
690 691


692
let apply_trans_on_selection tr =
693
  List.iter
694 695
    (fun r ->
       let a = get_any_from_row_reference r in
696 697
        M.transform
          ~context_unproved_goals_only:!context_unproved_goals_only
698
          tr
699
          a)
700
    (get_selected_row_references ())
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731


(*********************************)
(* add a new file in the project *)
(*********************************)

let filter_all_files () =
  let f = GFile.filter ~name:"All" () in
  f#add_pattern "*" ;
  f

let filter_why_files () =
  GFile.filter
    ~name:"Why3 source files"
    ~patterns:[ "*.why"; "*.mlw"] ()

let select_file () =
  let d = GWindow.file_chooser_dialog ~action:`OPEN
    ~title:"Why3: Add file in project"
    ()
  in
  d#add_button_stock `CANCEL `CANCEL ;
  d#add_select_button_stock `OPEN `OPEN ;
  d#add_filter (filter_why_files ()) ;
  d#add_filter (filter_all_files ()) ;
  begin match d#run () with
  | `OPEN ->
      begin
        match d#filename with
          | None -> ()
          | Some f ->
732
              let f = Sysutil.relativize_filename project_dir f in
733 734
              eprintf "Adding file '%s'@." f;
              try
MARCHE Claude's avatar
MARCHE Claude committed
735
                M.add_file f
736
              with e ->
737
                fprintf str_formatter
738 739
                  "@[Error while reading file@ '%s':@ %a@]" f
                  Exn_printer.exn_printer e;
740 741
                let msg = flush_str_formatter () in
                info_window `ERROR msg
742 743 744 745 746 747 748 749 750
      end
  | `DELETE_EVENT | `CANCEL -> ()
  end ;
  d#destroy ()


let not_implemented () =
  info_window `INFO "This feature is not yet implemented, sorry."

751

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
(*************)
(* File menu *)
(*************)

let file_menu = factory#add_submenu "_File"
let file_factory = new GMenu.factory file_menu ~accel_group

let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~key:GdkKeysyms._A
    ~label:"_Add file" ~callback:select_file
    ()

let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~label:"_Preferences" ~callback:
    (fun () ->
       Gconfig.preferences gconfig;
768
       M.maximum_running_proofs := gconfig.max_running_processes)
769 770 771 772
    ()

let refresh_provers = ref (fun () -> ())

MARCHE Claude's avatar
MARCHE Claude committed
773 774
let add_refresh_provers f _msg =
(*
775
  eprintf "[Info] recording '%s' for refresh provers@." msg;
MARCHE Claude's avatar
MARCHE Claude committed
776
*)
777 778 779
  let rp = !refresh_provers in
  refresh_provers := (fun () -> rp (); f ())

780
(*
781 782 783 784
let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~label:"_Detect provers" ~callback:
    (fun () -> Gconfig.run_auto_detection gconfig; !refresh_provers () )
    ()
785
*)
786

787 788 789 790
let save_session () =
  eprintf "[Info] saving session@.";
  M.save_session ()

791
let exit_function ?(destroy=false) () =
792
  eprintf "[Info] saving IDE config file@.";
793
  save_config ();
794
  (*
MARCHE Claude's avatar
MARCHE Claude committed
795 796 797 798 799 800 801
  eprintf "saving session (testing only)@.";
  begin
    M.test_save ();
    try
      let l = M.test_load () in
      eprintf "reloaded successfully %d elements@." (List.length l);
      match l with
802 803 804 805
        | [] -> ()
        | f :: _ ->
            eprintf "first element is a '%s' with %d sub-elements@."
              f.Xml.name (List.length f.Xml.elements);
MARCHE Claude's avatar
MARCHE Claude committed
806

MARCHE Claude's avatar
MARCHE Claude committed
807 808 809 810
    with e -> eprintf "test reloading failed with exception %s@."
      (Printexc.to_string e)
  end;
  let ret = Sys.command "xmllint --noout --dtdvalid share/why3session.dtd essai.xml" in
MARCHE Claude's avatar
MARCHE Claude committed
811
  if ret = 0 then eprintf "DTD validation succeeded, good!@.";
812
  *)
813 814 815 816
  match config.saving_policy with
    | 0 -> save_session (); GMain.quit ()
    | 1 -> GMain.quit ()
    | 2 ->
817 818 819 820 821
        let answer =
          GToolbox.question_box
            ~title:"Why3 saving session"
            ~buttons:(["Yes"; "No"] @ (if destroy then [] else ["Cancel"]))
            "Do you want to save the session?"
822
        in
823 824 825 826 827 828
        begin
          match answer with
            | 1 -> save_session (); GMain.quit ()
            | 2 -> GMain.quit ()
            | _ -> if destroy then GMain.quit () else ()
        end
829
    | _ ->
830 831
        eprintf "unexpected value for saving_policy@.";
        GMain.quit ()
832

833 834 835 836 837 838 839 840 841 842 843 844 845
(*************)
(* View menu *)
(*************)

let view_menu = factory#add_submenu "_View"
let view_factory = new GMenu.factory view_menu ~accel_group
let (_ : GMenu.image_menu_item) =
  view_factory#add_image_item ~key:GdkKeysyms._E
    ~label:"Expand all" ~callback:(fun () -> goals_view#expand_all ()) ()



let rec collapse_proved_goal g =
MARCHE Claude's avatar
MARCHE Claude committed
846
  if M.goal_proved g then
847
    begin
MARCHE Claude's avatar
MARCHE Claude committed
848
      let row = M.goal_key g in
849
      goals_view#collapse_row row#path;
850 851 852
    end
  else
    Hashtbl.iter
853
      (fun _ t -> List.iter collapse_proved_goal t.M.subgoals)
MARCHE Claude's avatar
MARCHE Claude committed
854
      (M.transformations g)
855 856

let collapse_verified_theory th =
MARCHE Claude's avatar
MARCHE Claude committed
857
  if M.verified th then
858
    begin
MARCHE Claude's avatar
MARCHE Claude committed
859
      let row = M.theory_key th in
860
      goals_view#collapse_row row#path;
861 862
    end
  else
MARCHE Claude's avatar
MARCHE Claude committed
863
    List.iter collapse_proved_goal (M.goals th)
864 865

let collapse_verified_file f =
866
  if f.M.file_verified then
867
    begin
868
      let row = f.M.file_key in
869
      goals_view#collapse_row row#path;
870 871
    end
  else
872
    List.iter collapse_verified_theory f.M.theories
873 874

let collapse_all_verified_things () =
875
  List.iter collapse_verified_file (M.get_all_files ())
876 877 878 879 880 881 882 883 884

let (_ : GMenu.image_menu_item) =
  view_factory#add_image_item ~key:GdkKeysyms._C
    ~label:"Collapse proved goals"
    ~callback:collapse_all_verified_things
    ()

(*
let rec hide_proved_in_goal g =
885
  if g.M.proved then
886
    begin
887
      let row = g.M.goal_row in
888 889
      goals_view#collapse_row (goals_model#get_path row);
(*
890
      goals_model#set ~row ~column:M.visible_column false
891 892 893 894
*)
    end
  else
    Hashtbl.iter
895 896
      (fun _ t -> List.iter hide_proved_in_goal t.M.subgoals)
      g.M.transformations
897 898

let hide_proved_in_theory th =
899
  if th.M.verified then
900
    begin
901
      let row = th.M.theory_row in
902
      goals_view#collapse_row (goals_model#get_path row);
903
      goals_model#set ~row ~column:M.visible_column false
904 905
    end
  else
906
    List.iter hide_proved_in_goal th.M.goals
907 908

let hide_proved_in_file f =
909
  if f.M.file_verified then
910
    begin
911
      let row = f.M.file_row in
912
      goals_view#collapse_row (goals_model#get_path row);
913
      goals_model#set ~row ~column:M.visible_column false
914 915
    end
  else
916
    List.iter hide_proved_in_theory f.M.theories
917 918

let hide_proved_in_files () =
919
  List.iter hide_proved_in_file !M.all_files
920 921

let rec show_all_in_goal g =
922 923 924
  let row = g.M.goal_row in
  goals_model#set ~row ~column:M.visible_column true;
  if g.M.proved then
925 926 927 928
    goals_view#collapse_row (goals_model#get_path row)
  else
    goals_view#expand_row (goals_model#get_path row);
  Hashtbl.iter
929 930
    (fun _ t -> List.iter show_all_in_goal t.M.subgoals)
    g.M.transformations
931 932

let show_all_in_theory th =
933 934 935
  let row = th.M.theory_row in
  goals_model#set ~row ~column:M.visible_column true;
  if th.M.verified then
936 937 938 939
    goals_view#collapse_row (goals_model#get_path row)
  else
    begin
      goals_view#expand_row (goals_model#get_path row);
940
      List.iter show_all_in_goal th.M.goals
941 942 943
    end

let show_all_in_file f =
944 945 946
  let row = f.M.file_row in
  goals_model#set ~row ~column:M.visible_column true;
  if f.M.file_verified then
947 948 949 950
    goals_view#collapse_row (goals_model#get_path row)
  else
    begin
      goals_view#expand_row (goals_model#get_path row);
951
      List.iter show_all_in_theory f.M.theories
952 953 954
    end

let show_all_in_files () =
955
  List.iter show_all_in_file !M.all_files
956 957 958 959


let (_ : GMenu.check_menu_item) = view_factory#add_check_item
  ~callback:(fun b ->
960
               M.toggle_hide_proved_goals := b;
961 962 963 964 965 966 967 968 969 970 971 972 973
               if b then hide_proved_in_files ()
               else show_all_in_files ())
  "Hide proved goals"

*)

(**************)
(* Tools menu *)
(**************)


let () = add_refresh_provers (fun () ->
  List.iter (fun item -> item#destroy ()) provers_box#all_children)
974
  "remove from provers box"
975 976 977 978 979 980

let tools_menu = factory#add_submenu "_Tools"
let tools_factory = new GMenu.factory tools_menu ~accel_group

let () = add_refresh_provers (fun () ->
  List.iter (fun item -> item#destroy ()) tools_menu#all_children)
981
  "remove from tools menu"
982 983 984 985 986

let () =
  let add_item_provers () =
    Util.Mstr.iter
      (fun _ p ->
987
         let n = p.Session.prover_name ^ " " ^ p.Session.prover_version in
988 989 990 991 992 993
         let (_ : GMenu.image_menu_item) =
           tools_factory#add_image_item ~label:n
             ~callback:(fun () -> prover_on_selected_goals p)
             ()
         in
         let b = GButton.button ~packing:provers_box#add ~label:n () in
994 995
         b#misc#set_tooltip_markup ("Start <tt>" ^ p.Session.prover_name ^
           "</tt> on the <b>selected goals</b>");
996 997 998 999 1000 1001 1002 1003 1004

(* prend de la place pour rien
         let i = GMisc.image ~pixbuf:(!image_prover) () in
         let () = b#set_image i#coerce in
*)
         let (_ : GtkSignal.id) =
           b#connect#pressed
             ~callback:(fun () -> prover_on_selected_goals p)
         in ())
1005
      (M.get_provers ())
1006
  in
1007
  add_refresh_provers add_item_provers "Add in tools menu and provers box";
1008 1009
  add_item_provers ()

1010 1011 1012 1013 1014 1015
let split_selected_goals () =
  apply_trans_on_selection split_transformation

let inline_selected_goals () =
  apply_trans_on_selection inline_transformation

1016
let () =
1017 1018 1019
  let add_separator () =
    ignore(tools_factory#add_separator
             () : GMenu.menu_item) in
1020 1021
  let add_item_split () =
    ignore(tools_factory#add_image_item
1022
             ~label:"Split in selection"
1023 1024
             ~callback:split_selected_goals
             () : GMenu.image_menu_item) in
1025 1026 1027 1028 1029
  let add_item_inline () =
    ignore(tools_factory#add_image_item
             ~label:"Inline in selection"
             ~callback:inline_selected_goals
             () : GMenu.image_menu_item) in
1030 1031 1032
  add_refresh_provers add_separator "add separator in tools menu";
  add_refresh_provers add_item_split "add split in tools menu";
  add_refresh_provers add_item_inline  "add inline in tools menu";
1033 1034 1035
  add_separator ();
  add_item_split ();
  add_item_inline ()
1036 1037 1038 1039


let () =
  let b = GButton.button ~packing:transf_box#add ~label:"Split" () in
1040
  b#misc#set_tooltip_markup "Apply the transformation <tt>split_goal</tt> to the <b>selected goals</b>";
1041 1042 1043 1044 1045

  let i = GMisc.image ~pixbuf:(!image_transf) () in
  let () = b#set_image i#coerce in
  let (_ : GtkSignal.id) =
    b#connect#pressed ~callback:split_selected_goals
1046 1047
  in
  ()
1048 1049 1050

let () =
  let b = GButton.button ~packing:transf_box#add ~label:"Inline" () in
1051
  b#misc#set_tooltip_markup "Apply the transformation <tt>inline_goal</tt> to the <b>selected goals</b>";
1052 1053 1054 1055
  let i = GMisc.image ~pixbuf:(!image_transf) () in
  let () = b#set_image i#coerce in
  let (_ : GtkSignal.id) =
    b#connect#pressed ~callback:inline_selected_goals
MARCHE Claude's avatar
MARCHE Claude committed
1056
  in
1057
  ()
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118



(*************)
(* Help menu *)
(*************)


let help_menu = factory#add_submenu "_Help"
let help_factory = new GMenu