Source file gui.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
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
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
(** Main gui classes *)
module Widget = Stk.Widget
module Pack = Stk.Pack
let hidden_views = ref ([] : View.gui_view list)
let hide_view paned v =
hidden_views := v :: !hidden_views ;
paned#unpack v#box
let pop_last_hidden_view () =
match !hidden_views with
| [] -> None
| v :: q ->
hidden_views := q;
Some v
let pick_hidden_view =
let rec iter ?view ?kind acc l =
match l, view, kind with
| [], _, _ -> (None, List.rev acc)
| v :: q, Some v2, _ when Oo.id v = Oo.id v2 -> (Some v, (List.rev acc) @ q)
| v :: q, _, Some k when v#kind = k && v#pickable -> (Some v, (List.rev acc) @ q)
| v :: q, _, _ -> iter ?view ?kind (v::acc) q
in
fun ?view ?kind () ->
let (r, l) = iter ?view ?kind [] !hidden_views in
hidden_views := l;
match r with
| None -> None
| Some v ->
Some v
let init_view topwin (v : View.gui_view) =
v#set_on_focus_in
(fun _ ->
Log.debug (fun m -> m "on_focus_in(view %S)" v#label);
topwin#set_active_view (Some v))
let insert_in_pos ele p =
let rec iter n = function
[] -> [ele]
| h :: q ->
if n = p
then ele :: h :: q
else h :: (iter (n+1) q)
in
iter 0
let label_of_contents = function
`View o -> o#label
| `Paned o -> o#label
| `Notebook o -> o#label
let id_of_contents = function
`View o -> Oo.id o
| `Paned o -> Oo.id o
| `Notebook o -> Oo.id o
let widget_of_contents = function
`View gv -> gv#box
| `Notebook gn -> gn#notebook#coerce
| `Paned gp -> gp#paned#coerce
let contents_grab_focus = function
`View gv -> gv#grab_focus
| `Notebook gn -> gn#grab_focus
| `Paned gp -> gp#grab_focus
let rec find_container c = function
`View gv -> None
| `Paned gp ->
begin
let pred c2 = id_of_contents c2 = id_of_contents c in
let search_child = function
None -> None
| Some c2 ->
if pred c2 then
Some (`Paned gp)
else
find_container c c2
in
match search_child gp#child1 with
| None -> search_child gp#child2
| Some x -> Some x
end
| `Notebook gn ->
let pred c2 = id_of_contents c2 = id_of_contents c in
let rec iter = function
[] -> None
| (_,c2) :: q ->
if pred c2 then
Some (`Notebook gn)
else
match find_container c c2 with
None -> iter q
| Some x -> Some x
in
iter gn#tabs
class gui_window ?x ?y ?w ?h () =
let minibuffer = new Minibuffer.minibuffer () in
object(self)
inherit Gui_base.main ?x ?y ?w ?h ()
method x = fst (toplevel#position)
method y = snd (toplevel#position)
method width = toplevel#geometry.w
method height = toplevel#geometry.h
val mutable key_bindings_trees = []
val mutable contents :
[
`Paned of gui_paned
| `Notebook of gui_notebook
| `View of View.gui_view
] option = None
method window = toplevel
method contents = contents
method minibuffer = minibuffer
val mutable active_view : View.gui_view option = None
method set_active_view gvopt =
active_view <- gvopt;
key_bindings_trees <- Commands.trees_for_window
(match gvopt with None -> [] | Some v -> v#key_bindings);
self#set_view_interface gvopt;
minibuffer#set_active false
method set_view_interface gvopt =
item_edit#remove_menu ;
let = Menu.menu ~pack:item_edit#set_menu () in
let l =
match gvopt with
None -> [false; false; false]
| Some gv -> [gv#save <> None; gv#save_as <> None; gv#reload <> None]
in
List.iter2 (fun mi b -> mi#set_sensitive b)
[item_save;item_save_as;item_reload] l;
let l =
match gvopt with
None -> [None;None;None]
| Some gv -> [gv#paste;gv#copy;gv#cut]
in
List.iter2
(fun text fopt ->
let (mi,_) = Menu.label_menuitem ~pack:menu#add_item ~text () in
match fopt with
None -> mi#set_sensitive false
| Some f -> ignore(mi#connect Widget.Activated f)
)
["Paste";"Copy";"Cut"]
l;
let mb = view_menubar in
List.iter mb#unpack mb#children_widgets;
let f (text,entries) =
let (mi,_) = Menu.label_menuitem ~text ~pack:mb#add_item () in
let mn = Menu.menu ~pack:mi#set_menu () in
Menu.fill_menu mn entries
in
match gvopt with
None -> ()
| Some gv -> List.iter f (List.rev gv#menus)
method update_menus =
self#set_view_interface self#active_view
method active_view = active_view
method contains_view (v : View.gui_view) =
match contents with
None -> false
| Some (`View v2) -> Oo.id v = Oo.id v2
| Some c -> find_container (`View v) c <> None
method get_active_view_container =
match active_view with
None -> None
| Some gv ->
match contents with
None -> None
| Some (`View v) ->
if Oo.id v = Oo.id gv then
Some (`Window (self :> gui_window))
else
None
| Some (`Paned gp) ->
gp#find_view_container gv
| Some (`Notebook gn) ->
gn#find_view_container gv
method destroy_active_view =
match active_view with
None -> ()
| Some v -> v#destroy
method on_about () =
Commands.async_command "about"
method on_new_window () =
Commands.async_command "new_window"
method on_open_file () =
Commands.async_command "open_file"
method on_destroy_active_view () =
Commands.async_command "destroy_active_view"
method reload_active_view =
match active_view with
None -> Lwt.return_unit
| Some gv ->
match gv#reload with
None -> Lwt.return_unit
| Some f -> f ()
method ask_open_file =
let dir =
match active_view with
None -> Sys.getcwd ()
| Some v -> Filename.dirname v#filename
in
Lwt.async (fun () ->
Misc.select_file minibuffer
~title: "Open file" (Misc.filename_to_utf8 (dir^"/"))
(fun s -> Commands.eval_command (Printf.sprintf "open_file %s" (Filename.quote s)))
)
method widget_opt_of_contents_opt = function
None -> None
| Some x -> Some (widget_of_contents x)
method set_contents =
begin
match self#widget_opt_of_contents_opt contents with
None -> ()
| Some widget -> vbox#unpack widget
end;
fun c ->
contents <- c;
match self#widget_opt_of_contents_opt c with
None -> ()
| Some widget ->
begin
match c with
| None -> ()
| Some (`View v) ->
v#set_on_destroy self#on_view_destroy;
v#set_on_label_change self#set_title
| Some (`Notebook gn) ->
gn#set_on_destroy (fun c -> self#set_contents c);
gn#set_on_label_change self#set_title
| Some (`Paned gp) ->
gp#set_on_destroy (fun c -> self#set_contents c);
gp#set_on_label_change self#set_title
end;
vbox#pack widget;
vbox#reorder_child widget 1;
let label =
match c with
None -> ""
| Some c -> contents_grab_focus c; label_of_contents c
in
self#set_title label
method add_view v =
match contents with
None ->
init_view (self :> View.topwin) v;
self#set_contents (Some (`View v));
| Some ((`View _) as current_c)
| Some ((`Paned _) as current_c) ->
(** TODO: whether a paned or notebook is created should
be a choice in preferences *)
let gn = new gui_notebook (self :> View.topwin) () in
self#set_contents (Some (`Notebook gn));
gn#add_tab None current_c;
gn#add_view v
| Some (`Notebook gn) ->
gn#add_view v
method private on_view_destroy () =
match self#widget_opt_of_contents_opt contents with
None -> ()
| Some w ->
contents <- None;
vbox#unpack w
method add_view_in_active_view_container ?(replace=false) v =
match self#get_active_view_container with
None -> self#add_view v
| Some (`Window _) -> self#add_view v
| Some (`Paned gp) -> gp#add_view ~replace v
| Some (`Notebook gn) -> gn#add_view v
method pop_last_hidden_view =
match pop_last_hidden_view () with
| None -> ()
| Some v ->
self#add_view_in_active_view_container ~replace:true v
method popup_pick_hidden_view =
match !hidden_views with
| [] -> Misc.set_active_action_message "No hidden view"
| l ->
let entries = List.map
(fun view ->
`I (Misc.escape_menu_label (Printf.sprintf "%s [%s]" view#label view#kind),
fun () ->
match pick_hidden_view ~view () with
| None -> ()
| Some v -> self#add_view_in_active_view_container ~replace:true v))
l
in
Stk.Menu.popup_menu_entries entries
method open_file ?attributes f =
try
let factory = View.factory_of_filename f in
match View.factory_open_file ~factory
(self :> View.topwin) active_view ?attributes f
with
`Use_view v -> v#grab_focus
| `New_view v ->
init_view (self :> View.topwin) v;
self#add_view_in_active_view_container ~replace:true v
with
Failure s ->
self#error_message s
method set_title s =
let s = if s = "" then "" else ": "^s in
toplevel#set_title (Printf.sprintf "%s%s" (Misc.to_utf8 Messages.software) s)
method new_tab =
match self#get_active_view_container with
| None -> ()
| Some (`Window _) ->
(
match contents with
Some (`View v) ->
(
match v#dup (self :> View.topwin) with
None -> ()
| Some v ->
init_view (self :> View.topwin) v;
self#add_view v
)
| _ -> prerr_endline "Should not be here"
)
| Some (`Paned gp) ->
(
match active_view with
None -> ()
| Some v ->
match v#dup (self :> View.topwin) with
None -> ()
| Some v ->
init_view (self :> View.topwin) v;
gp#new_tab (`View v);
v#grab_focus;
)
| Some (`Notebook gn) ->
(
match active_view with
None -> ()
| Some v ->
match v#dup (self :> View.topwin) with
None -> ()
| Some v ->
init_view (self :> View.topwin) v;
gn#add_view v
)
method split_active_view (orientation : Stk.Props.orientation) =
match self#get_active_view_container with
None
| Some (`Window _) ->
begin
match contents with
Some (`View v1) ->
(
match v1#dup (self :> View.topwin) with
None -> ()
| Some v2 ->
let size =
let g = v1#box#geometry in
g.w, g.h
in
let gp = new gui_paned (self :> View.topwin) orientation () in
self#set_contents (Some (`Paned gp));
init_view (self :> View.topwin) v2;
gp#set_children_views ~size v1 v2;
v2#grab_focus;
)
| _ -> ()
end
| Some (`Paned gp) -> gp#split_active_view orientation
| Some (`Notebook gn) -> gn#split_active_view orientation
method on_new_tab () = Commands.async_command "new_tab"
method on_split_active_view o () =
Commands.async_command
(Printf.sprintf "split_%s"
(match o with
`HORIZONTAL -> "horizontally"
| `VERTICAL -> "vertically"))
method on_store_layout () = Commands.async_command "store_layout"
method cycle_tab forward =
match active_view with
None -> ()
| Some v ->
match contents with
None -> ()
| Some mycontents ->
let rec iter c =
match find_container c mycontents with
None -> None
| Some (`Notebook gn) ->
Some gn
| Some x -> iter x
in
match iter (`View v) with
None -> ()
| Some gn -> gn#cycle_tab forward
method cycle_view =
match active_view with
None -> ()
| Some v ->
match contents with
None -> ()
| Some mycontents ->
let rec iter c =
match find_container c mycontents with
None ->
begin
match c with
`Paned gp ->
begin
match gp#child1, gp#child2 with
Some c, _
| None, Some c -> contents_grab_focus c
| None, None -> ()
end
| `View v ->
v#grab_focus
| `Notebook nb ->
nb#grab_focus
end
| Some ((`Paned gp) as x) ->
begin
match gp#child1, gp#child2 with
| Some c1, Some c2 when id_of_contents c1 = id_of_contents c ->
contents_grab_focus c2
| _ -> iter x
end
| Some x -> iter x
in
iter (`View v)
method on_close () = Commands.async_command "close_active_window"
method close = self#window#destroy
method set_state_message = wl_keystate#set_text
method set_action_message msg =
if minibuffer#active then
()
else
minibuffer#set_text msg
method error_message s =
Misc.error_message (Misc.to_utf8 s)
method save_active_view =
match active_view with
None -> Lwt.return_unit
| Some v ->
match v#save with
None -> Lwt.return_unit
| Some f -> f ()
method save_active_view_as =
match active_view with
None -> Lwt.return_unit
| Some v ->
match v#save_as with
None -> Lwt.return_unit
| Some f -> f ()
method display_keyhit_state ~after_handler st =
let s = Stk.Wkey.string_of_keyhit_state st in
self#set_state_message s ;
if not after_handler then
self#set_action_message ""
method print_key_bindings =
let l = Ocf.get Gui_rc.window_key_bindings @
(match active_view with
None -> []
| Some v -> v#key_bindings)
in
List.iter
(fun (st,com) ->
Log.app (fun m -> m "%s -> %s"
(Stk.Key.string_of_keystate_list st) com))
l
method on_minibuffer_active_change active =
if active then
begin
Stk.Wkey.reset_state toplevel#coerce;
self#display_keyhit_state ~after_handler: true [];
key_bindings_trees <- Commands.trees_for_window minibuffer#key_bindings
end
else
match active_view with
None -> () | Some v -> v#grab_focus
method paste =
match active_view with
None -> ()
| Some v ->
match v#paste with
None -> ()
| Some f -> f ()
method copy =
match active_view with
None -> ()
| Some v ->
match v#copy with
None -> ()
| Some f -> f ()
method cut =
match active_view with
None -> ()
| Some v ->
match v#cut with
None -> ()
| Some f -> f ()
initializer
hbox_state#pack minibuffer#box;
minibuffer#set_on_active_change self#on_minibuffer_active_change;
ignore(item_close#connect Widget.Activated self#on_close);
ignore(item_log_window#connect Widget.Activated
(fun () -> Commands.async_command "log_window"));
ignore(item_save#connect Widget.Activated
(fun () -> Commands.async_command "save_active_view"));
ignore(item_save_as#connect Widget.Activated
(fun () -> Commands.async_command "save_active_view_as"));
ignore(item_reload#connect Widget.Activated
(fun () -> Commands.async_command "reload_active_view"));
ignore(item_about#connect Widget.Activated
self#on_about);
ignore(item_new_window#connect Widget.Activated
self#on_new_window);
ignore(item_open#connect Widget.Activated
self#on_open_file);
ignore(item_new_tab#connect Widget.Activated
self#on_new_tab);
ignore(item_split_horizontally#connect Widget.Activated
(self#on_split_active_view `HORIZONTAL));
ignore(item_split_vertically#connect Widget.Activated
(self#on_split_active_view `VERTICAL));
ignore(item_destroy#connect Widget.Activated
self#on_destroy_active_view);
ignore(item_store_layout#connect Widget.Activated
self#on_store_layout);
ignore(item_cycle_tab#connect Widget.Activated
(fun () -> Commands.async_command "cycle_tab"));
ignore(item_cycle_view#connect Widget.Activated
(fun () -> Commands.async_command "cycle_view"));
key_bindings_trees <- Commands.trees_for_window [];
Stk.Wkey.set_handler_trees
~stop: (Ocf.get Gui_rc.abort_binding)
(fun () ->
Log.debug (fun m ->
m "%a" Stk.Wkey.pp_handler_trees key_bindings_trees);
key_bindings_trees)
~display_state: self#display_keyhit_state
evbox#coerce;
end
and gui_paned (topwin : View.topwin) orientation () =
let paned = Pack.paned orientation () in
object(self)
inherit View.dyn_label
method paned = paned
method orientation = orientation
method position =
let g = paned#geometry in
match paned#handle_positions with
| [] | None :: _ -> g.w
| (Some (`Percent p)) :: _ ->
let base = match paned#orientation with Horizontal -> g.w | Vertical -> g.h in
truncate ((float base *. p) /. 100.)
| (Some (`Absolute p)) :: _ -> p
method set_position p =
let g = paned#geometry in
paned#set_handle_positions [Some (`Absolute (min p g.w))]
val mutable child1 :
[
`Paned of gui_paned
| `Notebook of gui_notebook
| `View of View.gui_view
] option = None
val mutable child2 :
[
`Paned of gui_paned
| `Notebook of gui_notebook
| `View of View.gui_view
] option = None
method child1 = child1
method child2 = child2
val mutable on_destroy =
(fun (c : [ `Paned of gui_paned
| `Notebook of gui_notebook
| `View of View.gui_view
] option) -> ())
method on_child_view_destroy n =
List.iter paned#unpack paned#children_widgets;
on_destroy (if n = 1 then child2 else child1);
child1 <- None;
child2 <- None;
paned#destroy
method set_on_destroy f = on_destroy <- f
method on_child_destroy n c =
if (n <> 1 && n <> 2) then
Log.err (fun m -> m "Gui.paned#on_child_destroy invalid n = %d" n);
match (if n = 1 then child1 else child2) with
None -> ()
| Some c_old ->
match c with
None ->
self#on_child_view_destroy n
| Some c ->
let w = widget_of_contents c_old in
paned#unpack w;
(if n = 1 then paned#pack ~pos:0 else paned#pack ~pos:1) (widget_of_contents c);
if n = 1 then child1 <- Some c else child2 <- Some c;
self#on_child_label_change;
contents_grab_focus c
method on_child_label_change =
let s =
match child1, child2 with
None, Some c
| Some c, None -> label_of_contents c
| None, None -> " "
| Some c1, Some c2 -> Printf.sprintf "%s | %s"
(label_of_contents c1) (label_of_contents c2)
in
self#set_label s
method set_one_child n c =
begin
match (if n = 1 then child1 else child2) with
None -> ()
| Some c ->
paned#unpack (widget_of_contents c)
end;
if n = 1 then child1 <- Some c else child2 <- Some c;
begin
match c with
`View v ->
v#set_on_destroy (fun () -> self#on_child_view_destroy n);
v#set_on_label_change (fun _ -> self#on_child_label_change);
| `Paned gp ->
gp#set_on_destroy (self#on_child_destroy n);
gp#set_on_label_change (fun _ -> self#on_child_label_change);
| `Notebook gn ->
gn#set_on_destroy (self#on_child_destroy n);
gn#set_on_label_change (fun _ -> self#on_child_label_change);
end;
(if n = 1 then paned#pack ~pos:0 else paned#pack ~pos:1) (widget_of_contents c);
self#on_child_label_change;
contents_grab_focus c
method set_children_views ~size v1 v2 =
self#set_one_child 1 (`View v1);
self#set_one_child 2 (`View v2);
let g = paned#geometry in
let p = match orientation with
| Stk.Props.Vertical -> g.h
| Horizontal -> g.w
in
self#set_position (p / 2)
method find_view_container :
View.gui_view -> [ `Notebook of gui_notebook
| `Paned of gui_paned
| `Window of gui_window ] option = fun gv ->
let find_in_child = function
None -> None
| Some (`Notebook (gn:gui_notebook)) ->
gn#find_view_container gv
| Some (`Paned (gp:gui_paned)) ->
gp#find_view_container gv
| Some (`View v) ->
if Oo.id v = Oo.id gv then
Some (`Paned (self :> gui_paned))
else
None
in
match find_in_child child1 with
None -> find_in_child child2
| Some x -> Some x
method new_tab c =
match topwin#active_view with
None -> ()
| Some v ->
let res =
match child1 with
Some (`View v1) when Oo.id v1 = Oo.id v -> Some (1, v1)
| _ ->
match child2 with
Some (`View v2) when Oo.id v2 = Oo.id v -> Some (2, v2)
| _ -> None
in
match res with
None -> prerr_endline "Can't insert tab here, we should not be here in this paned"
| Some (n, cur_view) ->
paned#unpack cur_view#box;
let gn = new gui_notebook topwin () in
(if n = 1 then paned#pack ~pos:0 else paned#pack ~pos:1) gn#notebook#coerce;
gn#set_on_destroy (self#on_child_destroy n);
gn#set_on_label_change (fun _ -> self#on_child_label_change);
if n = 1 then
child1 <- Some (`Notebook gn)
else
child2 <- Some (`Notebook gn);
gn#add_tab None (`View cur_view);
gn#add_tab None c;
method add_view ~replace (v : View.gui_view) =
match topwin#active_view with
None -> ()
| Some av ->
let res =
match child1 with
Some (`View v1) when Oo.id v1 = Oo.id av -> Some (1, v1)
| _ ->
match child2 with
Some (`View v2) when Oo.id v2 = Oo.id av -> Some (2, v2)
| _ -> None
in
match res with
None -> prerr_endline "Can't insert view here, we should not be here in this paned"
| Some (n, cur_view) ->
if replace then
(
hide_view paned cur_view ;
(if n = 1 then paned#pack ~pos:0 else paned#pack ~pos:1) v#box#coerce ;
if n = 1 then
child1 <- Some (`View v)
else
child2 <- Some (`View v)
)
else
(
let size = let g = paned#geometry in g.w, g.h in
paned#unpack cur_view#box;
let gp = new gui_paned topwin orientation () in
(if n = 1 then paned#pack ~pos:0 else paned#pack ~pos:2) gp#paned#coerce;
gp#set_on_destroy (self#on_child_destroy n);
gp#set_on_label_change (fun _ -> self#on_child_label_change);
if n = 1 then
child1 <- Some (`Paned gp)
else
child2 <- Some (`Paned gp);
gp#set_children_views ~size cur_view v;
);
v#grab_focus
method split_active_view orientation =
match topwin#active_view with
None -> ()
| Some v ->
let res =
match child1 with
Some (`View v1) when Oo.id v1 = Oo.id v -> Some (1, v1)
| _ ->
match child2 with
Some (`View v2) when Oo.id v2 = Oo.id v -> Some (2, v2)
| _ -> None
in
match res with
None -> prerr_endline "can't split this, not a view"
| Some (n, cur_view) ->
match cur_view#dup topwin with
None -> ()
| Some view_copy ->
let gp = new gui_paned topwin orientation () in
let size = let g = paned#geometry in g.w, g.h in
paned#unpack cur_view#box;
if n = 1 then
child1 <- Some (`Paned gp)
else
child2 <- Some (`Paned gp);
(if n = 1 then paned#pack ~pos:0 else paned#pack ~pos:1) gp#paned#coerce;
gp#set_on_label_change (fun _ -> self#on_child_label_change);
gp#set_on_destroy (self#on_child_destroy n);
init_view topwin view_copy;
gp#set_children_views ~size cur_view view_copy;
view_copy#grab_focus
method grab_focus =
match child1 with
Some c -> contents_grab_focus c
| None ->
match child2 with
Some c -> contents_grab_focus c
| None -> ()
end
and gui_notebook (topwin : View.topwin) () =
let nb = Stk.Notebook.hnotebook
()
in
object(self)
inherit View.dyn_label
method notebook = nb
val mutable tabs :
(Stk.Text.label *
[
`Paned of gui_paned
| `Notebook of gui_notebook
| `View of View.gui_view
]
) list = []
method tabs = tabs
val mutable on_destroy =
(fun (c : [ `Paned of gui_paned
| `Notebook of gui_notebook
| `View of View.gui_view
] option) -> ())
method destroy =
match tabs with
(_,c) :: _ ->
for i = 0 to List.length tabs - 1 do
nb#remove_page i
done;
on_destroy (Some c);
nb#destroy
| [] -> on_destroy None; nb#destroy
method set_on_destroy f = on_destroy <- f
method on_tab_destroy c new_c =
match self#tab_of_contents c with
None -> Log.warn (fun m -> m "no tab with contents %s (tab contents: %s)"
(label_of_contents c)
(String.concat ", " (List.map (fun (_,c) -> label_of_contents c) tabs)))
| Some n ->
tabs <- List.filter
(fun (_,c2) -> id_of_contents c2 <> id_of_contents c) tabs;
nb#remove_page n;
match new_c, tabs with
None, []
| None, [_] -> self#destroy
| None, _ -> ()
| Some new_c, [] ->
on_destroy (Some new_c);
nb#destroy
| Some new_c, _ ->
self#add_tab (Some n) new_c
method on_view_destroy v () = self#on_tab_destroy (`View v) None
method find_view_container :
View.gui_view -> [ `Notebook of gui_notebook
| `Paned of gui_paned
| `Window of gui_window ] option =
fun gv ->
let find_in_child = function
| `Notebook (gn:gui_notebook) ->
gn#find_view_container gv
| `Paned (gp:gui_paned) ->
gp#find_view_container gv
| `View v ->
if Oo.id v = Oo.id gv then
Some (`Notebook (self :> gui_notebook))
else
None
in
let rec iter = function
[] -> None
| (_,h) :: q ->
match find_in_child h with
None -> iter q
| Some x -> Some x
in
iter self#tabs
method tab_of_contents c =
let oid = id_of_contents c in
let pred c2 = id_of_contents c2 = oid in
let rec iter n = function
[] -> None
| (_,h) :: q ->
if pred h then Some n else iter (n+1) q
in
iter 0 tabs
method add_view v =
self#add_tab None (`View v);
v#grab_focus
method set_tab_label c s =
match self#tab_of_contents c with
None -> ()
| Some n ->
let (w,_) = List.nth tabs n in
w#set_text (Misc.to_utf8 (label_of_contents c));
self#set_label s
method add_tab pos c =
let label = label_of_contents c in
let wlabel = Stk.Text.label ~text:label () in
tabs <-
(match pos with
None -> tabs @ [wlabel,c]
| Some pos -> insert_in_pos (wlabel,c) pos tabs
);
let w = match c with
`View gv ->
gv#set_on_label_change (self#set_tab_label c);
gv#set_on_destroy (self#on_view_destroy gv);
gv#box
| `Notebook gn ->
gn#set_on_label_change (self#set_tab_label c);
gn#set_on_destroy (self#on_tab_destroy c);
gn#notebook#coerce
| `Paned gp ->
gp#set_on_label_change (self#set_tab_label c);
gp#set_on_destroy (self#on_tab_destroy c);
gp#paned#coerce
in
let n =
match pos with
None ->
ignore(nb#pack ~label: wlabel#coerce w);
List.length tabs - 1
| Some pos ->
ignore(nb#pack ~pos ~label: wlabel#coerce w);
pos
in
self#goto_page n;
contents_grab_focus c
method split_active_view orientation =
match topwin#active_view with
None -> ()
| Some v1 ->
match self#tab_of_contents (`View v1) with
None -> ()
| Some n ->
let (wl,_) = List.nth tabs n in
match v1#dup topwin with
None -> ()
| Some v2 ->
let size = let g = v1#box#geometry in g.w, g.h in
let gp = new gui_paned topwin orientation () in
gp#set_on_label_change (self#set_tab_label (`Paned gp));
gp#set_on_destroy (self#on_tab_destroy (`Paned gp));
init_view topwin v2;
gp#set_children_views ~size v1 v2;
let rec iter m = function
[] -> []
| (wl,`View v') :: q when n = m ->
(wl, `Paned gp) :: q
| h :: q ->
h :: (iter (m+1) q)
in
tabs <- iter 0 tabs;
ignore(nb#pack ~label: wl#coerce ~pos:n gp#paned#coerce);
self#goto_page n;
v2#grab_focus;
method grab_focus =
try
match nb#active_page with
| None -> ()
| Some i ->
let (_,c) = List.nth tabs i in
contents_grab_focus c
with _ -> ()
method cycle_tab forward =
match nb#active_page with
| None -> ()
| Some i ->
let new_page =
((if forward then (+) else (-)) i 1) mod (List.length tabs)
in
self#goto_page new_page
method goto_page n = ignore(nb#set_active_page n)
method on_switch_page ~prev ~now =
self#set_label (label_of_contents (snd (List.nth tabs now)))
initializer
ignore (nb#connect (Stk.Object.Prop_changed Stk.Notebook.active_page)
self#on_switch_page);
end
type gui_windows = gui_window list
let gui_windows : gui_windows ref = ref []
let active_window = ref (None : gui_window option)
let on_last_window_close = ref (fun () -> ())
let on_window_destroy w () =
gui_windows :=
List.filter (fun w2 -> Oo.id w <> Oo.id w2) !gui_windows;
match !gui_windows with
[] -> !on_last_window_close (); false
| _ -> false
let create_window ?x ?y ?w ?h () =
let o = new gui_window ?x ?y ?w ?h () in
gui_windows := o :: !gui_windows;
let w = o#window in
ignore(w#connect Widget.Destroy (on_window_destroy o));
ignore(w#connect (Stk.Object.Prop_changed Stk.Props.has_focus)
(fun ~prev ~now -> if now then active_window := Some o));
w#show;
o
let _ = Commands.register (Commands.unit_com "new_window"
(fun () -> ignore(create_window ())))
let in_new_window args =
let com = Commands.argv_to_string args in
let w = create_window () in
active_window := Some w;
Commands.async_command com
let _ =
let com = Commands.create_com "in_new_window"
~more: "command and arguments to launch with the new window active"
[| |]
(fun args -> in_new_window args; Lwt.return_unit)
in
Commands.register com
let on_active_window f () =
match !active_window with
None -> Log.warn (fun m -> m "no active window.")
| Some o -> f o
let on_active_window_lwt f () =
match !active_window with
None -> Log.warn (fun m -> m "no active window."); Lwt.return_unit
| Some o -> f o
let on_active_window_args (f : gui_window -> string array -> unit) args =
match !active_window with
| None -> Log.warn (fun m -> m "no active window.") ; Lwt.return_unit
| Some o -> f o args; Lwt.return_unit
let _ =
let f args =
let len = Array.length args in
let () =
if len <= 0 then
let g w = w#ask_open_file in
on_active_window g ()
else
let filename = args.(0) in
let loc = if len = 1 then None else Some args.(1) in
let attributes =
match loc with
None -> None
| Some loc -> Some ["location", loc]
in
let g w = w#open_file ?attributes filename in
on_active_window g ()
in
Lwt.return_unit
in
let com =
{ Commands.com_name = "open_file" ;
Commands.com_args = [| "file" |] ;
Commands.com_more_args = Some "optional location" ;
Commands.com_f = f;
}
in
Commands.register com
;;
let open_file_with_encoding args =
let len = Array.length args in
if len < 1 then
let g w =
let dir =
match w#active_view with
None -> Sys.getcwd ()
| Some v -> Filename.dirname v#filename
in
Misc.select_file w#minibuffer
~title: "Open file with encoding" (Misc.filename_to_utf8 (dir^"/"))
(fun s -> Commands.launch_command "open_file_with_encoding" [|s|])
in
on_active_window_lwt g ()
else if len < 2 then
let g w =
let f s = Commands.launch_command "open_file_with_encoding" [| args.(0); s |] in
Misc.select_string w#minibuffer
~title: (Printf.sprintf "Open %s with encoding" (Misc.filename_to_utf8 args.(0)))
~choices: Iconv.encoding_names
"" f
in
on_active_window_lwt g ()
else
let g (w : gui_window) =
w#open_file ~attributes: ["encoding", args.(1)] args.(0);
Lwt.return_unit
in
on_active_window_lwt g ()
;;
Commands.register
{ Commands.com_name = "open_file_with_encoding" ;
Commands.com_args = [| "file"; "encoding" |] ;
Commands.com_more_args = None ;
Commands.com_f = open_file_with_encoding;
}
;;
let prompt_command_history = Minibuffer.history ()
let prompt_command (w : gui_window) =
let mb = w#minibuffer in
let on_return com =
match Misc.no_blanks com with
"" -> Lwt.return_unit
| _ -> Commands.eval_command com
in
Lwt.async (fun () ->
Misc.select_string
~history: prompt_command_history
mb ~title: "Command"
~choices: (Commands.available_command_names ())
""
on_return)
let unit_coms_on_active_window =
[
"close_active_window", (fun w -> w#close) ;
"new_tab", (fun w -> w#new_tab) ;
"split_vertically", (fun w -> w#split_active_view Stk.Props.Horizontal) ;
"split_horizontally", (fun w -> w#split_active_view Stk.Props.Vertical) ;
"destroy_active_view", (fun w -> w#destroy_active_view) ;
"cycle_tab", (fun w -> w#cycle_tab true) ;
"rev_cycle_tab", (fun w -> w#cycle_tab false) ;
"cycle_view", (fun w -> w#cycle_view) ;
"pop_last_hidden_view", (fun w -> w#pop_last_hidden_view) ;
"popup_pick_hidden_view", (fun w -> w#popup_pick_hidden_view) ;
"print_key_bindings", (fun w -> w#print_key_bindings) ;
"paste", (fun w -> w#paste) ;
"copy", (fun w -> w#copy) ;
"cut", (fun w -> w#cut) ;
"prompt_command", (fun w -> prompt_command w) ;
Minibuffer_rc.base_name ^ "_eval", (fun w -> Lwt.async (fun () -> w#minibuffer#eval)) ;
Minibuffer_rc.base_name ^ "_complete", (fun w -> w#minibuffer#complete) ;
Minibuffer_rc.base_name ^ "_history_previous", (fun w -> w#minibuffer#history_previous) ;
Minibuffer_rc.base_name ^ "_history_next", (fun w -> w#minibuffer#history_next) ;
Minibuffer_rc.base_name ^ "_exit", (fun w -> w#minibuffer#exit ()) ;
]
let _ = List.iter
(fun (name, f) ->
Commands.register (Commands.unit_com name (on_active_window f)))
unit_coms_on_active_window
let unit_coms_on_active_window_lwt =
[
"save_active_view", (fun w -> w#save_active_view) ;
"save_active_view_as", (fun w -> w#save_active_view_as) ;
"reload_active_view", (fun w -> w#reload_active_view) ;
]
let _ = List.iter
(fun (name, f) ->
Commands.register (Commands.unit_com_lwt name (on_active_window_lwt f)))
unit_coms_on_active_window_lwt
let args_coms_on_active_window =
[
"set_active_state_message", [| "message" |],
(fun w args -> w#set_state_message (if Array.length args > 0 then args.(0) else "")) ;
"set_active_action_message", [| "message" |],
(fun w args -> w#set_action_message (if Array.length args > 0 then args.(0) else ""));
Minibuffer_rc.base_name ^ "_eval_custom_key_binding", [| "binding" |],
(fun w args -> if Array.length args > 0 then w#minibuffer#eval_custom_key_binding args.(0)) ;
Minibuffer_rc.base_name ^ "_insert", [| "string" |],
(fun w args -> if Array.length args > 0 then w#minibuffer#insert args.(0)) ;
]
let _ = List.iter
(fun (name,args, f) ->
Commands.register (Commands.create_com name args (on_active_window_args f)))
args_coms_on_active_window