-
Notifications
You must be signed in to change notification settings - Fork 2
/
proof_window.ml
1503 lines (1337 loc) · 52.8 KB
/
proof_window.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
183
184
185
186
187
188
189
190
191
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* prooftree --- proof tree display for Proof General
*
* Copyright (C) 2011 - 2024 Hendrik Tews
*
* This file is part of "prooftree".
*
* "prooftree" is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* "prooftree" 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. See the GNU
* General Public License in file COPYING in this or one of the parent
* directories for more details.
*
* You should have received a copy of the GNU General Public License
* along with "prooftree". If not, see <http://www.gnu.org/licenses/>.
*)
(** Creation, display and drawing of the main proof-tree window *)
(* open Util *)
open Gtk_ext
open Configuration
open Emacs_commands
open Draw_tree
open Tree_layers
open Node_window
open Help_window
open About_window
open Ext_dialog
(** Callback for higher-level modules when the user deletes a proof
window. During start-up the reference is changed to
{!Proof_tree.quit_proof}.
*)
let delete_proof_tree_callback = ref (fun (_ : string) -> ())
(** Contains proof window clones. See also
[Proof_tree.original_proof_trees].
*)
let cloned_proof_windows = ref []
(** Class for managing the main proof-tree window. Contains methods
for all callbacks and the necessary state. The widget tree the
binding to singals must be done outside.
Arguments are
- top_window {xref lablgtk class GWindow.window}
of the top-level widget
- drawing_h_adjustment {xref lablgtk class GData.adjustment}
of the horizontal scroll bar
of the drawing area
- drawing_v_adjustment {xref lablgtk class GData.adjustment}
of the vertical scroll bar
of the drawing area
- drawing_area {xref lablgtk class GMisc.drawing_area}
for the proof tree
- drawable {!Gtk_ext.better_drawable} for the actual
drawing operations
- labeled_sequent_frame {xref lablgtk class GBin.frame}
labelled frame of the sequent area
- sequent_window {xref lablgtk class GText.view}
of the sequent area
- sequent_v_adjustment {xref lablgtk class GData.adjustment}
of the vertical scroll bar of the sequent area
- message_label {xref lablgtk class GMisc.label}
for messages in the bottom line
- context_menu {xref lablgtk class GMenu.menu}
of the context menu
- proof_name the name of the proof this display is showing
*)
class proof_window (top_window : GWindow.window)
drawing_h_adjustment drawing_v_adjustment (drawing_area : GMisc.drawing_area)
(drawable : better_drawable)
labeled_sequent_frame sequent_window sequent_v_adjustment
(message_label : GMisc.label) context_menu proof_name
=
object (self)
(***************************************************************************
*
* Internal state and setters/accessors
*
***************************************************************************)
(** x-offset of the bounding box of the complete proof tree layer
stack. This is only non-zero when the width of the complete
layer stack is smaller than the width of the drawing area. *)
val mutable top_left = 0
(** y-offset of the bounding box of the top layer. Constantly 0. *)
val top_top = 0
(** [true] if the sequent area should always show the last line.
This is set to [true] when a sequent is shown and to false when
a proof command is shown. *)
val mutable sequent_window_scroll_to_bottom = false
(** The stack of layers of proof trees *)
val layer_stack = new tree_layer_stack
(** The current node of the proof tree, if there is one *)
val mutable current_node = None
(** Cache holding the relative bounding box of the current node. If
not [None], it holds the tuple [(x_low, x_high, y_low, y_high)]
relative to the top-left corner (i.e., ({!top_left},
{!top_top})) of the bounding box of the complete proof tree.
*)
val mutable current_node_offset_cache = None
(** Set to [true] when we still have to reposition the drawing area
to make the current node visible. This is needed because of the
asynchronous nature of GTK, which does not immediately resize
the drawing area when I request it. Therefore the positioning
code has to wait until the drawing area has changed its size and
all needed scrollbars have been added.
*)
val mutable position_to_current_node = true
(** List of position hints, containing lists of proof tree nodes. If
possible, the current node will positioned such that some hint
nodes are also visible. If the list is longer than one, hints
are tried one after each other.
*)
val mutable position_hints = []
(** Holds the selected node, if there is one. *)
val mutable selected_node = None
(** List of all external non-orphaned node windows that belong to nodes
in the current proof tree. This list is stored only for optimization.
*)
val mutable node_windows = []
(** The management object for the dialog for existential
variables, if present.
*)
val mutable existential_window = None
(** When we are asked to destroy this proof-tree window (because of
an undo or because Proof General sent a quit-proof command) then
the destruction of the top-level GTK window will emit a destroy
signal that we interpret as a user destroy button click.
Further, the deletion of this proof from the various list with
proof_tree structures, might cause a second call of the
{!delete_proof_window} method. This flag is used to avoid this
double or triple killing.
*)
val mutable destroy_in_progress = false
(** True when this is a clone. *)
val mutable is_clone = false
(** A proof is disconnected if it is not the current proof that is
currently updated by Proof General. Used for the context menu.
*)
val mutable disconnected = false
(** The stack of layers, containing all proof trees belonging to
this proof.
*)
method layer_stack = layer_stack
(** Return the selected node or [None] if there is none. *)
method get_selected_node = selected_node
(** Delete the external node window from {!node_windows}. *)
method delete_node_window win =
node_windows <- List.filter (fun owin -> owin <> win) node_windows
(** Setter for {!is_clone}. *)
method set_clone_flag = is_clone <- true
(** Setter for {!position_hints}. *)
method set_position_hints hints =
position_hints <- (hints : proof_tree_element list list)
(** Clear {!position_hints}. *)
method clear_position_hints = position_hints <- []
(***************************************************************************
*
* Messages
*
***************************************************************************)
(** Display text in the message label. *)
method message text = message_label#set_label text
(***************************************************************************
*
* Sequent window
*
***************************************************************************)
(** Make the last line visible in the sequent area, if
{!sequent_window_scroll_to_bottom} is [true].
This is the callback for the changed signal of the vertical
adjustment of the scrollbar of the sequent area (emitted when
the sequent area changes its size but not when the scrollbar is
moved).
*)
method sequent_area_changed () =
if sequent_window_scroll_to_bottom then
let a = sequent_v_adjustment in
a#set_value (max a#lower (a#upper -. a#page_size))
(** [update_sequent_area label content scroll_to_bottom] updates the
sequent area to show [content] with label [label] on the frame.
It further sets {!sequent_window_scroll_to_bottom} to
[scroll_to_bottom].
*)
method private update_sequent_area label content scroll_to_bottom =
labeled_sequent_frame#set_label (Some label);
sequent_window#buffer#set_text content;
sequent_window_scroll_to_bottom <- scroll_to_bottom
(** Clear the sequent area. *)
method private clear_sequent_area =
self#update_sequent_area "no sequent" "" false
(** Refresh the sequent area. If there is a selected node, show it.
Otherwise, if there is a current sequent node that has a parent
sequent, then show the parent sequent. Otherwise clear the sequent
area.
*)
method refresh_sequent_area =
match selected_node with
| Some node ->
let (frame_text, scroll_to_bottom) = match node#node_kind with
| Turnstile -> ("Selected sequent", true)
| Proof_command -> ("Selected command", false)
in
self#update_sequent_area frame_text node#displayed_text scroll_to_bottom
| None ->
match current_node with
| None -> self#clear_sequent_area
| Some node ->
if node#node_kind = Turnstile
then
match node#parent with
| None -> self#clear_sequent_area
| Some p -> match p#parent with
| None -> self#clear_sequent_area
| Some p ->
self#update_sequent_area
"Previous sequent"
p#displayed_text
true
else
self#clear_sequent_area
(***************************************************************************
*
* Current node
*
***************************************************************************)
(** Set the current node. *)
method set_current_node n =
current_node_offset_cache <- None;
current_node <- Some (n : proof_tree_element)
(** Clear the current node. *)
method clear_current_node =
current_node_offset_cache <- None;
current_node <- None;
(***************************************************************************
*
* Unclassified methods
*
***************************************************************************)
(** Tell whether this proof-tree window should stay alive when the
user retracted to a point before the start of the proof. If the
window shows more than just the root sequent, it will stay
alive.
*)
method survive_undo_before_start =
layer_stack#survive_undo_before_start_hint
(** Disconnect the proof tree. Clears all current node and current
branch indications.
*)
method disconnect_proof =
disconnected <- true;
layer_stack#disconnect
(** Reflect changes in the current configuration. *)
method configuration_updated =
sequent_window#misc#modify_font !sequent_font_desc;
drawable#set_line_attributes
~width:(!current_config.turnstile_line_width) ();
layer_stack#configuration_updated;
self#expand_drawing_area;
ignore(self#position_tree);
GtkBase.Widget.queue_draw top_window#as_widget;
self#configuration_updated_ext_dialog
(** Clear this window and the existential variable dialog, if there
is one, for reuse when the proof it displays is started again.
Deletes and destroys all non-sticky external node windows.
*)
method clear_for_reuse =
disconnected <- false;
layer_stack#clear_for_reuse;
current_node <- None;
current_node_offset_cache <- None;
selected_node <- None;
self#refresh_sequent_area;
List.iter (fun w -> w#delete_attached_node_window) node_windows;
assert(node_windows = []);
self#clear_existential_dialog_for_reuse
(** Update the existentials info in external sequent displays. *)
method update_sequent_existentials_info =
layer_stack#update_sequent_existentials_info
(** Find the first node in the proof tree satisfying the predicate
using depth-first search.
*)
method find_node p = layer_stack#find_node p
(***************************************************************************
*
* Position to nodes and points
*
***************************************************************************)
(** Make node visible. *)
method private clamp_to_node node =
let (x_l, x_u, y_l, y_u) = node#bounding_box top_left top_top in
drawing_h_adjustment#clamp_page ~lower:x_l ~upper:x_u;
drawing_v_adjustment#clamp_page ~lower:y_l ~upper:y_u
(** Move the drawing area to show point [(x,y)] as centre point. *)
method private centre_point x y =
let x_size = drawing_h_adjustment#page_size in
let adj_x_l = drawing_h_adjustment#lower in
let adj_x_u = drawing_h_adjustment#upper in
let x = x -. x_size /. 2.0 in
let x = if x +. x_size > adj_x_u then adj_x_u -. x_size else x in
drawing_h_adjustment#set_value (max adj_x_l x);
let y_size = drawing_v_adjustment#page_size in
let adj_y_l = drawing_v_adjustment#lower in
let adj_y_u = drawing_v_adjustment#upper in
let y = y -. y_size /. 2.0 in
let y = if y +. y_size > adj_y_u then adj_y_u -. y_size else y in
drawing_v_adjustment#set_value (max adj_y_l y)
(** Show the given node in the centre of the drawing area. *)
method private centre_node node =
let (x_l, x_u, y_l, y_u) = node#bounding_box top_left top_top in
self#centre_point ((x_l +. x_u) /. 2.0) ((y_l +. y_u) /. 2.0)
(** Test if the given node is (partially) visible. *)
method private is_partially_visible node =
let (node_x_l, node_x_u, node_y_l, node_y_u) =
node#bounding_box top_left top_top in
let in_x = inside_adj_range drawing_h_adjustment in
let in_y = inside_adj_range drawing_v_adjustment
in
((in_x node_x_l) || (in_x node_x_u))
&& ((in_y node_y_l) || (in_y node_y_u))
(** Make the given node visible in a user friendly way. *)
method show_node node =
if self#is_partially_visible node
then self#clamp_to_node node
else self#centre_node node
(** Make the two given nodes visible in a user friendly way. If both
nodes do not fit together in the drawing area, then only the second
node is shown.
*)
method show_both_nodes n1 n2 =
let (x1_l, x1_u, y1_l, y1_u) = n1#bounding_box top_left top_top in
let (x2_l, x2_u, y2_l, y2_u) = n2#bounding_box top_left top_top in
let x_l = min x1_l x2_l in
let x_u = max x1_u x2_u in
let y_l = min y1_l y2_l in
let y_u = max y1_u y2_u in
(*
* Printf.fprintf (debugc())
* "SBN n1 x %.0f-%.0f y %.0f-%.0f "
* "n2 x %.0f-%.0f y %.0f-%.0f bb x %.0f-%.0f y %.0f-%.0f\n%!"
* x1_l x1_u y1_l y1_u
* x2_l x2_u y2_l y2_u
* x_l x_u y_l y_u;
*)
if x_u -. x_l <= drawing_h_adjustment#page_size
&& y_u -. y_l <= drawing_v_adjustment#page_size
then
(* both n1 and n2 fit on the page *)
if self#is_partially_visible n1 || self#is_partially_visible n2
then begin
self#clamp_to_node n1;
self#clamp_to_node n2
end
else
self#centre_point ((x_l +. x_u) /. 2.0) ((y_l +. y_u) /. 2.0)
else
self#show_node n2
(***************************************************************************
*
* Key events
*
***************************************************************************)
(** Delete and destroy this proof-tree window, including all its
non-sticky external node windows and the existential variable
dialog. Delete this window from {!cloned_proof_windows}. Other
data structures in higher-level modules are not touched.
The destruction process might cause destroy signals which will
cause this method to be called again. The flag
{!destroy_in_progress} is used to protect against such cases.
*)
method delete_proof_window =
if destroy_in_progress = false then begin
destroy_in_progress <- true;
List.iter (fun w -> w#delete_attached_node_window) node_windows;
self#destroy_existential_dialog;
let self = (self :> proof_window) in
cloned_proof_windows :=
List.fold_left
(fun res w -> if w = self then res else w :: res)
[] !cloned_proof_windows;
top_window#destroy()
end
(** Callback for the "Dismiss" button and the destroy signal
(emitted, for instance, when the window manager kills this
window). Delete and destroy this window as for
{!delete_proof_window} and delete it from all data structures in
the module {!Proof_tree}.
*)
method user_delete_proof_window () =
if destroy_in_progress = false then begin
if is_clone = false then
!delete_proof_tree_callback proof_name;
self#delete_proof_window
end
(** Callback for key events. Call the appropriate action for each
key.
*)
method key_pressed_callback ev =
match GdkEvent.Key.keyval ev with
| ks when
(ks = GdkKeysyms._Q || ks = GdkKeysyms._q)
&& (List.mem `CONTROL (GdkEvent.Key.state ev))
->
exit 0
| ks when (ks = GdkKeysyms._Q || ks = GdkKeysyms._q) ->
self#user_delete_proof_window (); true
| ks when ks = GdkKeysyms._Left ->
scroll_adjustment drawing_h_adjustment (-1); true
| ks when ks = GdkKeysyms._Right ->
scroll_adjustment drawing_h_adjustment 1; true
| ks when ks = GdkKeysyms._Up ->
scroll_adjustment drawing_v_adjustment (-1); true
| ks when ks = GdkKeysyms._Down ->
scroll_adjustment drawing_v_adjustment 1; true
| ks when (ks = GdkKeysyms._E || ks = GdkKeysyms._e) ->
self#show_existential_window (); true
(*
* | ks when (ks = GdkKeysyms._C or ks = GdkKeysyms._c) ->
* show_config_window (); true
*)
| _ -> false
(***************************************************************************
*
* Window for existential variables
*
***************************************************************************)
(** Clear {!existential_window}. *)
method existential_clear () =
existential_window <- None
(** Show a dialog for existential variables. If there is currently
none, a new one is created and initialized.
*)
method show_existential_window () =
match existential_window with
| Some w -> w#present
| None ->
let ext =
make_ext_dialog (self :> proof_window) proof_name
in
layer_stack#init_ext_dialog ext;
ext#finish_table_init;
existential_window <- Some ext
(** Destroy the existential variables dialog, if one is present. *)
method private destroy_existential_dialog =
match existential_window with
| None -> ()
| Some w ->
w#destroy ();
existential_window <- None
(** Prepare the existential variable dialog for reuse, if there is
one.
*)
method private clear_existential_dialog_for_reuse =
match existential_window with
| None -> ()
| Some w -> w#clear_for_reuse
(** Schedule some existentials to be added to the existential
variable dialog, if there is one.
*)
method ext_dialog_add new_ext =
match existential_window with
| Some w -> w#change_and_add new_ext
| None -> ()
(** Schedule some existentials to be removed from the existential
variable dialog, if there is one.
*)
method ext_dialog_undo remove_ext =
match existential_window with
| Some w -> w#change_and_delete remove_ext
| None -> ()
(** Process all pending requests for the existential variable
dialog, if there is one.
*)
method update_ext_dialog =
match existential_window with
| Some w -> w#update_ext_dialog
| None -> ()
(** Ask the existential variable dialog to reflect changes in the
current configuration.
*)
method private configuration_updated_ext_dialog =
match existential_window with
| Some w -> w#configuration_updated
| None -> ()
(***************************************************************************
*
* Redraw / expose events
*
***************************************************************************)
(** Return the relative bounding box of the current node, see
{!current_node_offset_cache}. If there nothing in the cache, try
to fill it. Return [None] if there is no current node.
*)
method private get_current_offset =
match current_node_offset_cache with
| Some _ as res -> res
| None -> match current_node with
| None -> None
| Some node ->
let res = Some node#bounding_box_offsets in
current_node_offset_cache <- res;
res
(** Erase the complete drawing area. *)
method private erase =
(* Printf.fprintf (debugc()) "ERASE\n%!"; *)
let (x,y) = drawable#size in
let gc = save_gc drawable in
let bg = top_window#misc#style#bg `PRELIGHT in
(* let bg = drawable#get_background in *)
(* drawable#set_foreground (`NAME("gray85")); *)
drawable#set_foreground (`COLOR bg);
drawable#polygon ~filled:true [(0,0); (x,0); (x,y); (0,y)];
restore_gc drawable gc
(** Try to change the scrollbars of the drawing area such that the
current node becomes visible in the bottom part. Do nothing if
{!position_to_current_node} is [false]. Making the current node
visible might be impossible, because the resize request for the
drawing area might not have been processed yet. In this case
{!position_to_current_node} stays true and this method is called
again at some later stage.
*)
method private try_adjustment =
if position_to_current_node = true then
match self#get_current_offset with
| None ->
position_to_current_node <- false
| Some((x_l_off, x_u_off, y_l_off, y_u_off)) ->
let x_page_size = int_of_float drawing_h_adjustment#page_size in
let y_page_size = int_of_float drawing_v_adjustment#page_size in
let x_l_f = float_of_int(top_left + x_l_off) in
let x_u_f = float_of_int(top_left + x_u_off) in
let y_l_f = float_of_int(top_top + y_l_off) in
let y_u_i = top_top + y_u_off in
let y_u_f = float_of_int y_u_i in
(*
* Printf.fprintf (debugc())
* "TRY ADJUSTMENT x %.1f-%.1f y %.1f-%.1f\n%!"
* x_l_f x_u_f y_l_f y_u_f;
*)
let success = ref true in
(* The following code might immediately trigger
* expose events, which will call try_adjustment again. To avoid
* entering this function a second time before leaving it, I
* temporarily switch position_to_current_node off.
*)
position_to_current_node <- false;
if x_page_size >= (x_u_off - x_l_off) &&
y_page_size >= (y_u_off - y_l_off)
then begin
(* current node fits into the viewport, be sophisticated *)
let req_width = layer_stack#width in
let req_height = layer_stack#height in
if (float_of_int req_width) > drawing_h_adjustment#upper ||
(float_of_int req_height) > drawing_v_adjustment#upper
then begin
(* The resize request for the drawing area has not
* been processed yet. It might happen that this resize
* request causes the addition of some scrollbars. In
* this case the viewport gets smaller and the
* current node would possible (partially) hidden.
* Therefore we mimic an adjustment error. Note that
* in this case also clamp_page would not give a proper
* adjustment.
*)
success := false;
(* Printf.fprintf (debugc()) "clever forced error %!" *)
end else begin
let y_val =
max drawing_v_adjustment#lower
(float_of_int (y_u_i - y_page_size))
in
drawing_v_adjustment#set_value y_val;
(* make now the _h_adjustment, first try if one of the
* hint nodes can be made visible as well
*)
if
List.exists
(fun hint_nodes ->
let (off_l, off_u) =
List.fold_left
(fun (off_l, off_u) hint_node ->
let (h_x_l_off, h_x_u_off, _, _) =
hint_node#bounding_box_offsets
in
(min off_l h_x_l_off, max off_u h_x_u_off))
(x_l_off, x_u_off)
hint_nodes
in
if x_page_size >= (off_u - off_l)
then begin
(* the hints fit *)
drawing_h_adjustment#clamp_page
~lower:(float_of_int (top_left + off_l))
~upper:(float_of_int (top_top + off_u));
true
end else
(* the hints don't fit *)
false
)
position_hints
then ()
else
drawing_h_adjustment#clamp_page ~lower:x_l_f ~upper:x_u_f;
(*
* Printf.fprintf (debugc()) "clever y_u_i %d up %d y_val %d %!"
* y_u_i
* (int_of_float drawing_v_adjustment#upper)
* (int_of_float y_val);
*)
end
end else begin
(* very small viewport, use dump strategy *)
(* Printf.fprintf (debugc()) "dump clamp %!"; *)
drawing_h_adjustment#clamp_page ~lower:x_l_f ~upper:x_u_f;
drawing_v_adjustment#clamp_page ~lower:y_l_f ~upper:y_u_f;
end;
if !success &&
range_inside_adj_range drawing_h_adjustment x_l_f x_u_f &&
range_inside_adj_range drawing_v_adjustment y_l_f y_u_f
then begin
(* Printf.fprintf (debugc()) "SUCCESSFUL\n%!"; *)
() (* Do nothing: leave position_to_current_node disabled *)
end else begin
(* Printf.fprintf (debugc()) "UNSUCCESSFUL %b\n%!" !success; *)
(* Schedule the adjustment again, hope that we are more
* successful next time.
*)
position_to_current_node <- true;
end;
(*
* (let a = drawing_h_adjustment in
* Printf.fprintf (debugc())
* ("TA HADJ low %.1f val %.1f " ^^
* "up %.1f size %.1f step %.1f page %.1f\n%!")
* a#lower a#value a#upper a#page_size
* a#step_increment a#page_increment);
* (let a = drawing_v_adjustment in
* Printf.fprintf (debugc())
* ("TA VADJ low %.1f val %.1f " ^^
* "up %.1f size %.1f step %.1f page %.1f\n%!")
* a#lower a#value a#upper a#page_size
* a#step_increment a#page_increment);
*)
()
(** Request the drawing are to change its size to the size of the
current proof tree.
*)
method private expand_drawing_area =
let new_width = layer_stack#width in
let new_height = layer_stack#height in
(*
* Printf.fprintf (debugc()) "DRAWING AREA SIZE REQUEST %d x %d\n%!"
* new_width new_height;
*)
(*
* if new_width > current_width || new_height > current_height then
* drawing_area#misc#set_size_request
* ~width:(max current_width new_width)
* ~height:(max current_height new_height) ();
*)
drawing_area#misc#set_size_request ~width:new_width ~height:new_height ();
(** Sets the position of the proof tree in the drawing area by
computing [top_left]. Returns true if the position changed.
In that case the complete drawing area must be redrawn.
*)
method private position_tree =
let old_top_left = top_left in
let (width, _) = drawable#size in
top_left <- max 0 ((width - layer_stack#width) / 2);
top_left <> old_top_left
(** Handle expose events. Try to move to the current node, erase and
redraw the complete drawing area.
*)
method private redraw =
(*
* (let a = drawing_v_adjustment in
* Printf.fprintf (debugc())
* "RD %s VADJ low %f val %f up %f size %f step %f page %f\n%!"
* (match scheduled_adjustment with | None -> "N" | Some _ -> "S")
* a#lower a#value a#upper a#page_size
* a#step_increment a#page_increment);
*)
self#try_adjustment;
self#erase;
(* Printf.fprintf (debugc()) "REDRAW\n%!"; *)
layer_stack#draw top_left top_top
(** Schedule an expose event for the drawing area, thereby causing
it to get redrawn.
*)
method invalidate_drawing_area =
(* Printf.fprintf (debugc()) "INVALIDATE\n%!"; *)
GtkBase.Widget.queue_draw drawing_area#as_widget
(** Method for updating the display after the proof tree has changed.
Adjusts the tree position in the drawing area, schedules a
complete redraw and make the current node (if any) visible.
*)
method refresh_and_position =
(* Printf.fprintf (debugc()) "REFRESH & POSITION\n%!"; *)
position_to_current_node <- true;
self#expand_drawing_area;
ignore(self#position_tree);
self#try_adjustment;
self#invalidate_drawing_area;
(* Printf.fprintf (debugc()) "REFRESH & POSITION END\n%!"; *)
()
(** Position the current node in the bottom part of the drawing
area.
*)
method reposition_current_node () =
position_to_current_node <- true;
self#try_adjustment
(** Make the current node visible in a user friendly way. *)
method show_current_node () =
match current_node with
| None -> ()
| Some current ->
if self#is_partially_visible current
then self#show_node current
else self#reposition_current_node ()
(** Make the selected node visible in a user friendly way. *)
method show_selected_node () =
match selected_node with
| None -> ()
| Some selected -> self#show_node selected
(** Callback for the size_allocate signal of the drawing area.
Position the proof tree in the viewport of the drawing area and
redraw.
*)
method draw_scroll_size_allocate_callback (_size : Gtk.rectangle) =
(*
* Printf.fprintf (debugc()) "SCROLLING SIZE ALLOC SIGNAL size %d x %d\n%!"
* (int_of_float (drawing_h_adjustment#upper +. 0.5))
* (int_of_float (drawing_v_adjustment#upper +. 0.5));
*)
let need_redraw = self#position_tree in
(*
* (let a = drawing_v_adjustment in
* Printf.fprintf (debugc())
* "SA %s VADJ low %f val %f up %f size %f step %f page %f\n%!"
* (match scheduled_adjustment with | None -> "N" | Some _ -> "S")
* a#lower a#value a#upper a#page_size
* a#step_increment a#page_increment);
*)
self#try_adjustment;
if need_redraw
then self#invalidate_drawing_area
(*
* method draw_area_size_allocate_callback (_size : Gtk.rectangle) =
* Printf.fprintf (debugc()) "AREA SIZE ALLOC SIGNAL size %d x %d\n%!"
* (int_of_float (drawing_h_adjustment#upper +. 0.5))
* (int_of_float (drawing_v_adjustment#upper +. 0.5));
*)
(*
* method draw_area_configure_callback configure_event =
* Printf.fprintf (debugc())
* "AREA CONFIGURE SIGNAL area size %d x %d scroll size %d x %d\n%!"
* (GdkEvent.Configure.width configure_event)
* (GdkEvent.Configure.height configure_event)
* (int_of_float (drawing_h_adjustment#upper +. 0.5))
* (int_of_float (drawing_v_adjustment#upper +. 0.5));
* false
*)
(** Callback for exposure events. Redraw the complete tree. *)
method expose_callback (_ev : GdkEvent.Expose.t) =
(*
* let r = GdkEvent.Expose.area ev in
* Printf.fprintf (debugc()) "EXPOSE count %d %d x %d at %d x %d\n%!"
* (GdkEvent.Expose.count ev)
* (Gdk.Rectangle.width r) (Gdk.Rectangle.height r)
* (Gdk.Rectangle.x r) (Gdk.Rectangle.y r);
*)
(*
* (let a = drawing_v_adjustment in
* Printf.fprintf (debugc())
* "EX VADJ low %f val %f up %f size %f step %f page %f\n%!"
* a#lower a#value a#upper a#page_size
* a#step_increment a#page_increment);
*)
(*
* (let a = drawing_h_adjustment in
* Printf.fprintf (debugc())
* "HADJ low %f val %f up %f size %f step %f page %f\n"
* a#lower a#value a#upper a#page_size a#step_increment a#page_increment);
* (let a = drawing_v_adjustment in
* Printf.fprintf (debugc())
* "VADJ low %f val %f up %f size %f step %f page %f\n%!"
* a#lower a#value a#upper a#page_size a#step_increment a#page_increment);
*)
self#redraw;
(* prerr_endline "END EXPOSE EVENT"; *)
false
(***************************************************************************
*
* numbers for external node windows
*
***************************************************************************)
(** Counter for external node windows. *)
val mutable last_node_number = 0
(** Return the number for the next external node window. *)
method private next_node_number =
last_node_number <- last_node_number + 1;
last_node_number
(***************************************************************************
*
* Button events
*
***************************************************************************)
(** The dragging feature has the problem that it changes the
viewport to the drawing area, which itself generates pointer
motion events for the drawing area. The approach is therefore to
remember the mouse position at the start of the dragging (i.e.,
the button press) in coordinates relative to the complete X
window.
X position of the mouse at the last button press relative to
[top_window].
*)
val mutable last_button_press_top_x = 0
(** Y position of the mouse at the last button press relative to
[top_window]. *)
val mutable last_button_press_top_y = 0
(** Value of the vertical scrollbar at the last button press. *)
val mutable last_button_press_v_adjustment_value = 0.0
(** Value of the horizontal scrollbar at the last button press. *)
val mutable last_button_press_h_adjustment_value = 0.0
(** Fill {!last_button_press_top_x}, {!last_button_press_top_y},
{!last_button_press_v_adjustment_value} and
{!last_button_press_h_adjustment_value} in case dragging
follows.
*)
method private remember_for_dragging =
let (x, y) = Gdk.Window.get_pointer_location top_window#misc#window in
(*
* Printf.fprintf (debugc()) "Button press %d x %d\n%!"
* (fst new_poi_loc) (snd new_poi_loc);
*)
last_button_press_top_x <- x;
last_button_press_top_y <- y;
last_button_press_v_adjustment_value <- drawing_v_adjustment#value;
last_button_press_h_adjustment_value <- drawing_h_adjustment#value;
(** We have different and incompatible actions for single and double
clicks and for dragging. Because single click events do always occur
before double clicks and dragging events, we have to undo the
changes that were done for the single click. A double click is of
course always preceeded by two single clicks.
Remember the selected node state before the last but one
mouse press. This state needs to get reestablished when we
detect a double click.
*)
val mutable old_old_selected_node = None
(** Remember the selected node state before the last mouse press.
This state needs to get reestablished on dragging events.
*)
val mutable old_selected_node = None
(** Remember if we have already restored the selected node state
since the last button press.
*)
val mutable restored_selected_node = false
(** Reset the selected node state to state [node_opt] and update
drawing and sequent area.
*)
method set_selected_node node_opt =
(match selected_node with
| None -> ()
| Some node -> node#selected false);
selected_node <- node_opt;
(match node_opt with
| None -> ()
| Some node -> node#selected true);
self#invalidate_drawing_area;
self#refresh_sequent_area
(** Make the root node the selected node or clear the selected node
if there is no root node
*)
method select_root_node =
self#set_selected_node (layer_stack#get_root_node)
(** Save the selected node state on a button press and clear
{!restored_selected_node}.
*)
method private save_selected_node_state =
old_old_selected_node <- old_selected_node;
old_selected_node <- selected_node;