gmain.ml 47.6 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
open Whyconf
open Gconfig
François Bobot's avatar
François Bobot committed
32
33
open Util
open Debug
34
module C = Whyconf
François Bobot's avatar
François Bobot committed
35
36

let debug = register_flag "gui"
37
38
39
40
41
42
43
44

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

let includes = ref []
let file = ref None
let opt_version = ref false
45
let opt_config = ref None
46
47

let spec = Arg.align [
48
  ("-L",
49
50
   Arg.String (fun s -> includes := s :: !includes),
   "<s> add s to loadpath") ;
51
52
53
54
55
56
57
58
59
60
  ("--library",
   Arg.String (fun s -> includes := s :: !includes),
   " same as -L") ;
  ("-I",
   Arg.String (fun s -> includes := s :: !includes),
   " same as -L (obsolete)") ;
  "-C", Arg.String (fun s -> opt_config := Some s),
      "<file> Read configuration from <file>";
  "--config", Arg.String (fun s -> opt_config := Some s),
      " same as -C";
61
62
63
64
65
(*
  ("-f",
   Arg.String (fun s -> input_files := s :: !input_files),
   "<f> add file f to the project (ignored if it is already there)") ;
*)
François Bobot's avatar
François Bobot committed
66
67
68
  Debug.Opt.desc_debug_list;
  Debug.Opt.desc_debug_all;
  Debug.Opt.desc_debug;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  ("-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

95
96
let () = Gconfig.read_config !opt_config

97
98
99
100
101
102
103
let fname = match !file with
  | None ->
      Arg.usage spec usage_str;
      exit 1
  | Some f ->
      f

104
let (why_lang, any_lang) =
105
106
107
108
109
110
111
  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);
112
113
  let why_lang =
    match languages_manager#language "why" with
114
115
116
117
    | None ->
        Format.eprintf "language file for 'why' not found in directory %s@."
          load_path;
        exit 1
118
119
120
121
122
123
    | 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)
124
125
126
127
128
129
130
131
132
133
134
135
136

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 *)
(********************************)

137
let loadpath = (C.loadpath (get_main ())) @ List.rev !includes
François Bobot's avatar
François Bobot committed
138

139
let gconfig =
140
  let c = Gconfig.config () in
141
  c.env <- Env.create_env loadpath;
142
(*
143
  let provers = C.get_provers c.Gconfig.config in
144
  c.provers <-
145
    Util.Mstr.fold (Session.get_prover_data c.env) provers Util.Mstr.empty;
146
*)
147
148
149
  c

let () =
150
  C.load_plugins (get_main ())
151

François Bobot's avatar
François Bobot committed
152
153
154
let () =
  Debug.Opt.set_flags_selected ();
  if Debug.Opt.option_list () then exit 0
155
156
157
158
159
160
161
162
163
164


(***************)
(* 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
165
  ~title:"Why3 Interactive Proof Session" ()
166

MARCHE Claude's avatar
MARCHE Claude committed
167
168
169
let () =
  w#set_icon (Some !Gconfig.why_icon)

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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

MARCHE Claude's avatar
MARCHE Claude committed
187
188
189
190
191
192
193
194
195
let hb = GPack.hbox ~packing:vbox#add ()

let left_scrollview =
  try
    GBin.scrolled_window ~hpolicy:`NEVER ~vpolicy:`AUTOMATIC
      ~packing:(hb#pack ~expand:false) ()
  with Gtk.Error _ -> assert false

let () = left_scrollview#set_shadow_type `OUT
196
197
198

let tools_window_vbox =
  try
MARCHE Claude's avatar
MARCHE Claude committed
199
    GPack.vbox ~packing:left_scrollview#add_with_viewport ()
200
201
202
  with Gtk.Error _ -> assert false

let context_frame =
MARCHE Claude's avatar
MARCHE Claude committed
203
  GBin.frame ~label:"Context" ~shadow_type:`ETCHED_OUT
204
205
206
207
208
209
210
211
212
213
214
215
    ~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
François Bobot's avatar
François Bobot committed
216
217
  b1#misc#set_tooltip_markup
    "When selected, tools below are applied only to <b>unproved</b> goals";
218
219
220
221
222
223
224
  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
François Bobot's avatar
François Bobot committed
225
226
  b2#misc#set_tooltip_markup
    "When selected, tools below are applied to all goals";
227
228
229
230
231
232
233
  let (_ : GtkSignal.id) =
    b2#connect#clicked
      ~callback:(fun () -> context_unproved_goals_only := false)
  in ()


let provers_frame =
MARCHE Claude's avatar
MARCHE Claude committed
234
  GBin.frame ~label:"Provers" ~shadow_type:`ETCHED_OUT
235
236
    ~packing:(tools_window_vbox#pack ~expand:false) ()

MARCHE Claude's avatar
MARCHE Claude committed
237

238
239
240
241
242
243
244
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 =
MARCHE Claude's avatar
MARCHE Claude committed
245
  GBin.frame ~label:"Transformations" ~shadow_type:`ETCHED_OUT
246
247
248
249
250
251
252
    ~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 =
MARCHE Claude's avatar
MARCHE Claude committed
253
  GBin.frame ~label:"Tools" ~shadow_type:`ETCHED_OUT
254
255
256
257
258
259
260
    ~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 =
MARCHE Claude's avatar
MARCHE Claude committed
261
  GBin.frame ~label:"Cleaning" ~shadow_type:`ETCHED_OUT
262
263
264
265
266
267
    ~packing:(tools_window_vbox#pack ~expand:false) ()

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

268
let monitor_frame =
MARCHE Claude's avatar
MARCHE Claude committed
269
  GBin.frame ~label:"Proof monitoring" ~shadow_type:`ETCHED_OUT
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    ~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 ()


285
286
287
288
289
290
291
292
293
294

(* horizontal paned *)

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


(* tree view *)
let scrollview =
  try
    GBin.scrolled_window ~hpolicy:`AUTOMATIC ~vpolicy:`AUTOMATIC
MARCHE Claude's avatar
MARCHE Claude committed
295
      ~width:gconfig.tree_width ~shadow_type:`ETCHED_OUT
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
      ~packing:hp#add ()
  with Gtk.Error _ -> assert false

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
317
let index_column = cols#add Gobject.Data.int
318

319
320
321
322
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 [ ]
323

324
325
let view_name_column =
  GTree.view_column ~title:"Theories/Goals" ()
326

327
328
329
330
331
332
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;
333
334
335
336
  view_name_column#set_max_width 800;
(*
  view_name_column#set_alignment 1.0;
*)
337
  ()
338

339
340
341
342
let view_status_column =
  GTree.view_column ~title:"Status"
    ~renderer:(image_renderer, ["pixbuf", status_column])
    ()
343

344
345
346
let view_time_column =
  GTree.view_column ~title:"Time"
    ~renderer:(renderer, ["text", time_column]) ()
347

348
349
350
351
352
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
353

354
let goals_model,goals_view =
355
  eprintf "[Info] Creating tree model...@?";
356
357
  let model = GTree.tree_store cols in
  let view = GTree.view ~model ~packing:scrollview#add () in
358
  let () = view#selection#set_mode (* `SINGLE *) `MULTIPLE in
359
360
361
362
  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);
363
  eprintf " done@.";
364
  model,view
365

366
367
368
369
let clear model = model#clear ()

let image_of_result ~obsolete result =
  match result with
François Bobot's avatar
François Bobot committed
370
371
372
373
    | Session.Undone Session.Interrupted -> !image_undone
    | Session.Undone Session.Unedited -> !image_unknown
    | Session.Undone Session.Scheduled -> !image_scheduled
    | Session.Undone Session.Running -> !image_running
374
375
    | Session.InternalFailure _ -> !image_failure
    | Session.Done r -> match r.Call_provers.pr_answer with
376
377
378
379
380
381
382
383
384
385
386
387
        | 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
388

389
(* connecting to the Session model *)
390

391
392
393
394
395
396
397
398
let fan n =
  match n mod 4 with
    | 0 -> "|"
    | 1 | -3 -> "\\"
    | 2 | -2 -> "-"
    | 3 | -1 -> "/"
    | _ -> assert false

François Bobot's avatar
François Bobot committed
399
module S = Session
MARCHE Claude's avatar
MARCHE Claude committed
400
401
402

let set_row_status row b =
  if b then
403
    goals_model#set ~row:row#iter ~column:status_column !image_yes
MARCHE Claude's avatar
MARCHE Claude committed
404
  else
405
    goals_model#set ~row:row#iter ~column:status_column !image_unknown
MARCHE Claude's avatar
MARCHE Claude committed
406

407
408
let set_proof_state a =
  let obsolete = a.S.proof_obsolete in
François Bobot's avatar
François Bobot committed
409
410
  let row = a.S.proof_key in
  let res = a.S.proof_state in
411
  goals_model#set ~row:row#iter ~column:status_column
MARCHE Claude's avatar
MARCHE Claude committed
412
413
    (image_of_result ~obsolete res);
  let t = match res with
François Bobot's avatar
François Bobot committed
414
415
416
    | S.Done { Call_provers.pr_time = time } ->
        if gconfig.show_time_limit then
          Format.sprintf "%.2f [%d.0]" time a.S.proof_timelimit
MARCHE Claude's avatar
MARCHE Claude committed
417
418
        else
          Format.sprintf "%.2f" time
François Bobot's avatar
François Bobot committed
419
420
421
422
423
    | S.Undone S.Unedited -> "(not yet edited, edit with \"Edit\" button)"
    | S.InternalFailure _ -> "(internal failure)"
    | S.Undone S.Interrupted -> "(interrupted)"
    | S.Undone (S.Scheduled | S.Running) ->
        Format.sprintf "[limit=%d.0]" a.S.proof_timelimit
MARCHE Claude's avatar
MARCHE Claude committed
424
425
  in
  let t = if obsolete then t ^ " (obsolete)" else t in
426
427
  (* TODO find a better way to signal arhived row *)
  let t = if a.S.proof_archived then t ^ " (archived)" else t in
428
  goals_model#set ~row:row#iter ~column:time_column t
MARCHE Claude's avatar
MARCHE Claude committed
429

430
431
let model_index = Hashtbl.create 17

432

433
let get_any_from_iter row =
MARCHE Claude's avatar
MARCHE Claude committed
434
  try
435
436
    let idx = goals_model#get ~row ~column:index_column in
    Hashtbl.find model_index idx
437
438
  with Not_found -> invalid_arg "Gmain.get_any_from_iter"

439
(*
440
441
let get_any (row:Gtk.tree_path) : M.any =
  get_any_from_iter (goals_model#get_iter row)
442
443
444
445
446
447
448
449
*)

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
450

451
let row_expanded b iter _path =
452
  match get_any_from_iter iter with
François Bobot's avatar
François Bobot committed
453
454
455
456
457
458
459
460
461
    | S.File f ->
        S.set_file_expanded f b
    | S.Theory t ->
        S.set_theory_expanded t b
    | S.Goal g ->
        S.set_goal_expanded g b
    | S.Transf tr ->
        S.set_transf_expanded tr b
    | S.Proof_attempt _ -> ()
462

463

464
let (_:GtkSignal.id) =
465
466
  goals_view#connect#row_collapsed ~callback:(row_expanded false)

467
let (_:GtkSignal.id) =
468
  goals_view#connect#row_expanded ~callback:(row_expanded true)
469

François Bobot's avatar
François Bobot committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
module M = Session_scheduler.Make
  (struct
     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
       goals_model#set ~row:iter ~column:index_column (-1);
       goals_model#get_row_reference (goals_model#get_path iter)


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

     let reset () = goals_model#clear ()

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

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

     let notify_timer_state =
       let c = ref 0 in
       fun t s r ->
         incr c;
         monitor_waiting#set_text ("Waiting: " ^ (string_of_int t));
         monitor_scheduled#set_text ("Scheduled: " ^ (string_of_int s));
         monitor_running#set_text
           (if r=0 then "Running: 0" else
              "Running: " ^ (string_of_int r)^ " " ^ (fan (!c / 10)))

MARCHE Claude's avatar
MARCHE Claude committed
506
let notify any =
507
508
  let row,exp =
    match any with
François Bobot's avatar
François Bobot committed
509
510
511
512
513
514
      | S.Goal g ->
          g.S.goal_key, g.S.goal_expanded
      | S.Theory t -> t.S.theory_key, t.S.theory_expanded
      | S.File f -> f.S.file_key, f.S.file_expanded
      | S.Proof_attempt a -> a.S.proof_key,false
      | S.Transf tr -> tr.S.transf_key,tr.S.transf_expanded
515
  in
516
  if exp then goals_view#expand_to_path row#path else
517
    goals_view#collapse_row row#path;
MARCHE Claude's avatar
MARCHE Claude committed
518
  match any with
François Bobot's avatar
François Bobot committed
519
520
521
522
523
524
525
    | S.Goal g ->
        set_row_status row g.S.goal_verified
    | S.Theory th ->
        set_row_status row th.S.theory_verified
    | S.File file ->
        set_row_status row file.S.file_verified
    | S.Proof_attempt a ->
526
        set_proof_state a
François Bobot's avatar
François Bobot committed
527
528
    | S.Transf tr ->
        set_row_status row tr.S.transf_verified
529

MARCHE Claude's avatar
MARCHE Claude committed
530
531
let init =
  let cpt = ref (-1) in
532
  fun row any ->
MARCHE Claude's avatar
MARCHE Claude committed
533
534
535
536
537
538
539
540
541
542
543
    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;
544
    (* useless since it has no child: goals_view#expand_row row#path; *)
MARCHE Claude's avatar
MARCHE Claude committed
545
    goals_model#set ~row:row#iter ~column:icon_column
546
      (match any with
François Bobot's avatar
François Bobot committed
547
548
549
550
551
         | S.Goal _ -> !image_file
         | S.Theory _
         | S.File _ -> !image_directory
         | S.Proof_attempt _ -> !image_prover
         | S.Transf _ -> !image_transf);
MARCHE Claude's avatar
MARCHE Claude committed
552
    goals_model#set ~row:row#iter ~column:name_column
553
      (match any with
François Bobot's avatar
François Bobot committed
554
         | S.Goal g -> S.goal_expl g
François Bobot's avatar
François Bobot committed
555
556
557
558
         | S.Theory th -> th.S.theory_name.Ident.id_string
         | S.File f -> Filename.basename f.S.file_name
         | S.Proof_attempt a ->
           let p = a.S.proof_prover in
559
           Pp.string_of_wnl C.print_prover p
François Bobot's avatar
François Bobot committed
560
         | S.Transf tr -> tr.S.transf_name);
MARCHE Claude's avatar
MARCHE Claude committed
561
    notify any
MARCHE Claude's avatar
MARCHE Claude committed
562

563
564
565
566
let unknown_prover = Gconfig.unknown_prover gconfig

let replace_prover = Gconfig.replace_prover gconfig

François Bobot's avatar
François Bobot committed
567
   end)
568
569


570
571
572
573
(********************)
(* opening database *)
(********************)

574
(** TODO remove that should done only in session *)
575
576
577
578
579
let project_dir, file_to_read =
  if Sys.file_exists fname then
    begin
      if Sys.is_directory fname then
        begin
580
          eprintf "[Info] found directory '%s' for the project@." fname;
581
582
583
584
          fname, None
        end
      else
        begin
585
          eprintf "[Info] found regular file '%s'@." fname;
586
587
588
589
          let d =
            try Filename.chop_extension fname
            with Invalid_argument _ -> fname
          in
590
          eprintf "[Info] using '%s' as directory for the project@." d;
591
          d, Some (Filename.concat Filename.parent_dir_name
592
                     (Filename.basename fname))
593
594
595
596
597
598
599
600
        end
    end
  else
    fname, None

let () =
  if not (Sys.file_exists project_dir) then
    begin
601
      eprintf "[Info] '%s' does not exists. Creating directory of that name \
602
603
604
605
 for the project@." project_dir;
      Unix.mkdir project_dir 0o777
    end

606

607
608
609
610
611
612
613
614
615
616
617
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
618
    ~title:"Why3IDE"
619
    ~icon:(!Gconfig.why_icon)
620
    ~modal:true
621
622
623
624
    ~show:true ()
  in
  let (_ : GtkSignal.id) =
    d#connect#response
625
      ~callback:(function x ->
626
627
                   d#destroy ();
                   if mt <> `QUESTION || x = `OK then callback ())
628
629
  in ()

630
631
(* check if provers are present *)
let () =
632
  if C.Mprover.is_empty (C.get_provers gconfig.Gconfig.config) then
633
    begin
François Bobot's avatar
François Bobot committed
634
635
      info_window `ERROR
        "No prover configured.\nPlease run 'why3config --detect-provers' first"
636
637
638
639
640
        ~callback:GMain.quit;
      GMain.main ();
      exit 2;
    end

François Bobot's avatar
François Bobot committed
641
let env_session,sched =
642
643
  try
    eprintf "[Info] Opening session...@\n@[<v 2>  ";
François Bobot's avatar
François Bobot committed
644
645
646
    let session =
      if Sys.file_exists project_dir then S.read_session project_dir
      else S.create_session project_dir in
François Bobot's avatar
François Bobot committed
647
648
649
    let env_session,(_:bool) =
      M.update_session ~allow_obsolete:true session gconfig.env
        gconfig.Gconfig.config
650
    in
François Bobot's avatar
François Bobot committed
651
652
653
    let sched = M.init gconfig.max_running_processes in
    dprintf debug "@]@\n[Info] Opening session: done@.";
    ref env_session, sched
654
655
656
657
658
659
  with e ->
    eprintf "@[Error while opening session:@ %a@.@]"
      Exn_printer.exn_printer e;
    exit 1


660
661
662
663
664
665
666
667
668

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

let () =
  match file_to_read with
    | None -> ()
    | Some fn ->
François Bobot's avatar
François Bobot committed
669
670
        if S.PHstr.mem !env_session.S.session.S.session_files fn then
          dprintf debug "[Info] file %s already in database@." fn
671
672
        else
          try
François Bobot's avatar
François Bobot committed
673
            ignore (M.add_file !env_session fn)
674
          with e ->
675
            eprintf "@[Error while reading file@ '%s':@ %a@.@]" fn
676
              Exn_printer.exn_printer e;
677
            exit 1
678
679
680
681
682
683
684
685
686
687




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

let prover_on_selected_goals pr =
  List.iter
688
    (fun row ->
MARCHE Claude's avatar
MARCHE Claude committed
689
      try
690
       let a = get_any_from_row_reference row in
MARCHE Claude's avatar
MARCHE Claude committed
691
       M.run_prover
François Bobot's avatar
François Bobot committed
692
         !env_session sched
MARCHE Claude's avatar
MARCHE Claude committed
693
694
695
696
697
698
         ~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)
699
    (get_selected_row_references ())
700
701
702
703
704
705
706

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

let replay_obsolete_proofs () =
  List.iter
707
708
    (fun r ->
       let a = get_any_from_row_reference r in
François Bobot's avatar
François Bobot committed
709
       M.replay !env_session sched ~obsolete_only:true
710
         ~context_unproved_goals_only:!context_unproved_goals_only a)
711
    (get_selected_row_references ())
712

713
714
715
716
717
718
719
720
721
722
(***********************************)
(* 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 ())
723

724
725
726
727
728
729
730
731
732
733
(*****************************************)
(* method: Set or unset the archive flag *)
(*****************************************)

let set_archive_proofs b () =
  List.iter
    (fun r ->
       let a = get_any_from_row_reference r in
       S.iter_proof_attempt (fun a -> M.set_archive a b) a)
    (get_selected_row_references ())
734
735
736
737
738

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

François Bobot's avatar
François Bobot committed
739
740
741
let split_transformation = "split_goal"
let inline_transformation = "inline_goal"
let intro_transformation = "introduce_premises"
742
743


744
let apply_trans_on_selection tr =
745
  List.iter
746
747
    (fun r ->
       let a = get_any_from_row_reference r in
François Bobot's avatar
François Bobot committed
748
        M.transform !env_session sched
749
          ~context_unproved_goals_only:!context_unproved_goals_only
750
          tr
751
          a)
752
    (get_selected_row_references ())
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783


(*********************************)
(* 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 ->
784
              let f = Sysutil.relativize_filename project_dir f in
785
786
              eprintf "Adding file '%s'@." f;
              try
François Bobot's avatar
François Bobot committed
787
                ignore (M.add_file !env_session f)
788
              with e ->
789
                fprintf str_formatter
790
791
                  "@[Error while reading file@ '%s':@ %a@]" f
                  Exn_printer.exn_printer e;
792
793
                let msg = flush_str_formatter () in
                info_window `ERROR msg
794
795
796
797
798
799
800
801
802
      end
  | `DELETE_EVENT | `CANCEL -> ()
  end ;
  d#destroy ()


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

803

804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
(*************)
(* 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;
François Bobot's avatar
François Bobot committed
820
       M.set_maximum_running_proofs gconfig.max_running_processes sched)
821
822
823
824
    ()

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

MARCHE Claude's avatar
MARCHE Claude committed
825
826
let add_refresh_provers f _msg =
(*
827
  eprintf "[Info] recording '%s' for refresh provers@." msg;
MARCHE Claude's avatar
MARCHE Claude committed
828
*)
829
830
831
  let rp = !refresh_provers in
  refresh_provers := (fun () -> rp (); f ())

832
(*
833
834
835
836
let (_ : GMenu.image_menu_item) =
  file_factory#add_image_item ~label:"_Detect provers" ~callback:
    (fun () -> Gconfig.run_auto_detection gconfig; !refresh_provers () )
    ()
837
*)
838

839
840
let save_session () =
  eprintf "[Info] saving session@.";
François Bobot's avatar
François Bobot committed
841
  S.save_session !env_session.S.session
842

843
let exit_function ?(destroy=false) () =
844
  eprintf "[Info] saving IDE config file@.";
845
  save_config ();
846
  (*
MARCHE Claude's avatar
MARCHE Claude committed
847
848
849
850
851
852
853
  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
854
855
856
857
        | [] -> ()
        | 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
858

MARCHE Claude's avatar
MARCHE Claude committed
859
860
861
    with e -> eprintf "test reloading failed with exception %s@."
      (Printexc.to_string e)
  end;
François Bobot's avatar
François Bobot committed
862
863
  let ret = Sys.command
    "xmllint --noout --dtdvalid share/why3session.dtd essai.xml" in
MARCHE Claude's avatar
MARCHE Claude committed
864
  if ret = 0 then eprintf "DTD validation succeeded, good!@.";
865
  *)
866
  match (Gconfig.config ()).saving_policy with
867
868
869
    | 0 -> save_session (); GMain.quit ()
    | 1 -> GMain.quit ()
    | 2 ->
870
871
872
873
874
        let answer =
          GToolbox.question_box
            ~title:"Why3 saving session"
            ~buttons:(["Yes"; "No"] @ (if destroy then [] else ["Cancel"]))
            "Do you want to save the session?"
875
        in
876
877
878
879
880
881
        begin
          match answer with
            | 1 -> save_session (); GMain.quit ()
            | 2 -> GMain.quit ()
            | _ -> if destroy then GMain.quit () else ()
        end
882
    | _ ->
883
884
        eprintf "unexpected value for saving_policy@.";
        GMain.quit ()
885

886
887
888
889
(*************)
(* View menu *)
(*************)

890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
let modifiable_font_views = ref [goals_view#misc]

let font_family = "Monospace"
let font_size = ref 10

let change_font () =
(*
  Tools.resize_images (!Colors.font_size * 2 - 4);
*)
  let f =
    Pango.Font.from_string (font_family ^ " " ^ string_of_int !font_size)
  in
  List.iter (fun v -> v#modify_font f) !modifiable_font_views

let enlarge_font () =
  incr font_size;
  change_font ();
(*
  GConfig.save ()
*)
  ()

let reduce_font () =
  decr font_size; 
  change_font ();
(*
  GConfig.save ()
*)
()

920
921
let view_menu = factory#add_submenu "_View"
let view_factory = new GMenu.factory view_menu ~accel_group
922
923
924
925
926
927
928
929
930

let (_ : GMenu.menu_item) =
  view_factory#add_item ~key:GdkKeysyms._plus
    ~callback:enlarge_font "Enlarge font"

let (_ : GMenu.menu_item) =
    view_factory#add_item ~key:GdkKeysyms._minus
      ~callback:reduce_font "Reduce font"

931
932
933
934
let (_ : GMenu.image_menu_item) =
  view_factory#add_image_item ~key:GdkKeysyms._E
    ~label:"Expand all" ~callback:(fun () -> goals_view#expand_all ()) ()

François Bobot's avatar
François Bobot committed
935
936
937
938
939
940
941
942
943
944
945
let rec collapse_verified = function
  | S.Goal g when g.S.goal_verified ->
    let row = g.S.goal_key in
    goals_view#collapse_row row#path
  | S.Theory th when th.S.theory_verified ->
    let row = th.S.theory_key in
    goals_view#collapse_row row#path
  | S.File f when f.S.file_verified ->
    let row = f.S.file_key in
    goals_view#collapse_row row#path
  | any -> S.iter collapse_verified any
946
947

let collapse_all_verified_things () =
François Bobot's avatar
François Bobot committed
948
  S.session_iter collapse_verified !env_session.S.session
949
950
951
952
953
954
955
956
957

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 =
958
  if g.M.proved then
959
    begin
960
      let row = g.M.goal_row in
961
962
      goals_view#collapse_row (goals_model#get_path row);
(*
963
      goals_model#set ~row ~column:M.visible_column false
964
965
966
967
*)
    end
  else
    Hashtbl.iter
968
969
      (fun _ t -> List.iter hide_proved_in_goal t.M.subgoals)
      g.M.transformations
970
971

let hide_proved_in_theory th =
972
  if th.M.verified then
973
    begin
974
      let row = th.M.theory_row in
975
      goals_view#collapse_row (goals_model#get_path row);
976
      goals_model#set ~row ~column:M.visible_column false
977
978
    end
  else
979
    List.iter hide_proved_in_goal th.M.goals
980
981

let hide_proved_in_file f =
982
  if f.M.file_verified then
983
    begin
984
      let row = f.M.file_row in
985
      goals_view#collapse_row (goals_model#get_path row);
986
      goals_model#set ~row ~column:M.visible_column false
987
988
    end
  else
989
    List.iter hide_proved_in_theory f.M.theories
990
991

let hide_proved_in_files () =
992
  List.iter hide_proved_in_file !M.all_files
993
994

let rec show_all_in_goal g =
995
996
997
  let row = g.M.goal_row in
  goals_model#set ~row ~column:M.visible_column true;
  if g.M.proved then
998
999
1000
1001
    goals_view#collapse_row (goals_model#get_path row)
  else
    goals_view#expand_row (goals_model#get_path row);
  Hashtbl.iter
1002
1003
    (fun _ t -> List.iter show_all_in_goal t.M.subgoals)
    g.M.transformations
1004
1005

let show_all_in_theory th =
1006
1007
1008
  let row = th.M.theory_row in
  goals_model#set ~row ~column:M.visible_column true;
  if th.M.verified then
1009
1010
1011
1012
    goals_view#collapse_row (goals_model#get_path row)
  else
    begin
      goals_view#expand_row (goals_model#get_path row);
1013
      List.iter show_all_in_goal th.M.goals
1014
1015
1016
    end

let show_all_in_file f =
1017
1018
1019
  let row = f.M.file_row in
  goals_model#set ~row ~column:M.visible_column true;
  if f.M.file_verified then
1020
1021
1022
1023
    goals_view#collapse_row (goals_model#get_path row)
  else
    begin
      goals_view#expand_row (goals_model#get_path row);
1024
      List.iter show_all_in_theory f.M.theories
1025
1026
1027
    end

let show_all_in_files () =
1028
  List.iter show_all_in_file !M.all_files
1029
1030
1031
1032


let (_ : GMenu.check_menu_item) = view_factory#add_check_item
  ~callback:(fun b ->
1033
               M.toggle_hide_proved_goals := b;
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
               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)
1047
  "remove from provers box"
1048
1049
1050
1051
1052
1053

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)
1054
  "remove from tools menu"
1055
1056
1057

let () =
  let add_item_provers () =
1058
1059
    C.Mprover.iter
      (fun p _ ->
1060
         let n = Pp.string_of_wnl C.print_prover p in
1061
1062
1063
1064
1065
1066
         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
1067
1068
1069
         b#misc#set_tooltip_markup
           (Pp.sprintf_wnl "Start <tt>%a</tt> on the <b>selected goals</b>"
              C.print_prover p);
1070
1071
1072
1073
1074
1075
1076
1077
1078

(* 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 ())
1079
      (C.get_provers gconfig.Gconfig.config)
1080
  in
1081
  add_refresh_provers add_item_provers "Add in tools menu and provers box";
1082
1083
  add_item_provers ()

1084
1085
1086
1087
1088
1089
let split_selected_goals () =
  apply_trans_on_selection split_transformation

let inline_selected_goals () =
  apply_trans_on_selection inline_transformation

1090
let () =
1091
1092
1093
  let add_separator () =
    ignore(tools_factory#add_separator
             () : GMenu.menu_item) in
1094
1095
  let add_item_split () =
    ignore(tools_factory#add_image_item
1096
             ~label:"Split in selection"
1097
1098
             ~callback:split_selected_goals
             () : GMenu.image_menu_item) in
1099
1100
1101
1102
1103
  let add_item_inline () =
    ignore(tools_factory#add_image_item
             ~label:"Inline in selection"
             ~callback:inline_selected_goals
             () : GMenu.image_menu_item) in
1104
1105
1106
  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";
1107
1108
1109
  add_separator ();
  add_item_split ();
  add_item_inline ()
1110
1111
1112
1113


let () =
  let b = GButton.button ~packing:transf_box#add ~label:"Split" () in
François Bobot's avatar
François Bobot committed
1114
1115
  b#misc#set_tooltip_markup "Apply the transformation <tt>split_goal</tt> \
to the <b>selected goals</b>";
1116
1117
1118
1119
1120

  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
1121
1122
  in
  ()
1123
1124
1125

let () =
  let b = GButton.button ~packing:transf_box#add ~label:"Inline" () in
François Bobot's avatar
François Bobot committed
1126
1127
  b#misc#set_tooltip_markup "Apply the transformation <tt>inline_goal</tt> \
to the <b>selected goals</b>";
1128
1129
1130
1131
  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
1132
  in
1133
  ()
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174



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


let help_menu = factory#add_submenu "_Help"
let help_factory = new GMenu.factory help_menu ~accel_group

let (_ : GMenu.image_menu_item) =
  help_factory#add_image_item
    ~label:"Legend"
    ~callback:show_legend_window
    ()

let (_ : GMenu.image_menu_item) =
  help_factory#add_image_item
    ~label:"About"
    ~callback:show_about_window
    ()


(******************************)
(* vertical paned on the right*)
(******************************)

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

let vp =
  try
    GPack.paned `VERTICAL ~packing:right_vb#add ()
  with Gtk.Error _ -> assert false

let right_hb = GPack.hbox ~packing:(right_vb#pack ~expand:false) ()

(******************)
(* goal text view *)
(******************)

MARCHE Claude's avatar
MARCHE Claude committed
1175
1176
1177
1178
let scrolled_task_view =
  GBin.scrolled_window
    ~hpolicy: `AUTOMATIC ~vpolicy: `AUTOMATIC
    ~shadow_type:`ETCHED_OUT ~packing:vp#add ()
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193

let (_ : GtkSignal.id) =
  scrolled_task_view#misc#connect#size_allocate
    ~callback:
    (fun {Gtk.width=_w;Gtk.height=h} ->
       gconfig.task_height <- h)

let task_view =
  GSourceView2.source_view
    ~editable:false
    ~show_line_numbers:true
    ~packing:scrolled_task_view#add
    ~height:gconfig.task_height
    ()

1194
let () = modifiable_font_views := task_view#misc :: !modifiable_font_views
1195
let () = task_view#source_buffer#set_language why_lang
1196
1197
1198
1199
1200
1201
1202
1203
let () = task_view#set_highlight_current_line true

(***************)
(* source view *)
(***************)

let scrolled_source_view = GBin.scrolled_window
  ~hpolicy: `AUTOMATIC ~vpolicy: `AUTOMATIC
MARCHE Claude's avatar
MARCHE Claude committed
1204
  ~packing:vp#add ~shadow_type:`ETCHED_OUT
1205
1206
1207
1208
1209
1210
1211
1212
1213
  ()

let source_view =
  GSourceView2.source_view
    ~auto_indent:true
    ~insert_spaces_instead_of_tabs:true ~tab_width:2
    ~show_line_numbers:true
    ~right_margin_position:80 ~show_right_margin:true
    (* ~smart_home_end:true *)
1214
    ~editable:false
1215
1216
1217
    ~packing:scrolled_source_view#add
    ()

1218
1219


1220
1221
1222
(*
  source_view#misc#modify_font_by_name font_name;
*)
1223
let () = modifiable_font_views := source_view#misc :: !modifiable_font_views
1224
let () = source_view#source_buffer#set_language None
1225
1226
1227
1228
1229
let () = source_view#set_highlight_current_line true
(*
let () = source_view#source_buffer#set_text (source_text fname)
*)

1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
let current_file = ref ""

let file_info = GMisc.label ~text:""
  ~packing:(right_hb#pack ~fill:true) ()

let set_current_file f =
  current_file := f;
  file_info#set_text ("file: " ^ !current_file)


1240
1241
1242
1243
1244
1245
1246
let move_to_line ~yalign (v : GSourceView2.source_view) line =
  let line = max 0 line in
  let line = min line v#buffer#line_count in
  let it = v#buffer#get_iter (`LINE line) in
  let mark = `MARK (v#buffer#create_mark it) in
  v#scroll_to_mark ~use_align:true ~yalign mark

1247

Andrei Paskevich's avatar
minor    
Andrei Paskevich committed
1248
1249
let premise_tag = source_view#buffer#create_tag
  ~name:"premise_tag" [`BACKGROUND gconfig.premise_color]
1250

Andrei Paskevich's avatar
minor    
Andrei Paskevich committed
1251
1252
let goal_tag = source_view#buffer#create_tag
  ~name:"goal_tag" [`BACKGROUND gconfig.goal_color]
1253
1254

let error_tag = source_view#buffer#create_tag
1255
  ~name:"error_tag" [`BACKGROUND gconfig.error_color]