Skip to content

Commit b7dcdd1

Browse files
Fabian Bonkjeremiedimino
Fabian Bonk
authored andcommitted
Allow specifying the size of an LTerm_edit.edit widget (#42)
Allow different initial size for LTerm_edit.edit and add the double_editor.ml example making use of this feature
1 parent e7bcb06 commit b7dcdd1

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

_oasis

100644100755
+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ Library "lambda-term"
9090
# | Examples |
9191
# +-------------------------------------------------------------------+
9292

93+
Executable double_editor
94+
Path: examples
95+
Install: false
96+
CompiledObject: best
97+
MainIs: double_editor.ml
98+
BuildDepends: lambda-term
99+
Build$: flag(examples)
100+
93101
Executable events
94102
Path: examples
95103
Install: false

examples/double_editor.ml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
(*
2+
* double_editor.ml
3+
* ----------
4+
* Copyright : (c) 2016, Fabian Bonk <[email protected]>
5+
* Licence : BSD3
6+
*
7+
* This file is a part of Lambda-Term.
8+
*)
9+
open LTerm_geom
10+
11+
let ( >>= ) = Lwt.( >>= )
12+
13+
(* helper functions *)
14+
let make_key ?(ctrl = false) ?(meta = false) ?(shift = false) c =
15+
let code =
16+
match c with
17+
| `Char c -> LTerm_key.Char (CamomileLibrary.UChar.of_char c)
18+
| `Other key -> key in
19+
{ LTerm_key.control = ctrl; meta; shift; code }
20+
21+
let frame widget =
22+
let frame = new LTerm_widget.frame in
23+
frame#set widget;
24+
frame
25+
26+
let main () =
27+
let waiter, wakener = Lwt.wait () in
28+
29+
let ctrl_c = [make_key ~ctrl:true @@ `Char 'c'] in
30+
let tab = [make_key @@ `Other LTerm_key.Tab] in
31+
let quit = [LTerm_edit.Custom (Lwt.wakeup wakener)] in
32+
33+
let vbox = new LTerm_widget.vbox in
34+
35+
let top_editor = new LTerm_edit.edit () in
36+
let top_frame = frame top_editor in
37+
38+
(* make bottom editor a fixed 10 rows in size *)
39+
let bottom_editor = new LTerm_edit.edit ~size:{ rows = 10; cols = 1 } () in
40+
(* changed my mind: make it 5 rows smaller *)
41+
bottom_editor#set_allocation
42+
{ bottom_editor#allocation with row1 = bottom_editor#allocation.row1 - 5 };
43+
let bottom_frame = frame bottom_editor in
44+
45+
vbox#add top_frame;
46+
(* in versions before PR#42 this would either crash or make the bottom editor unusable *)
47+
vbox#add ~expand:false bottom_frame;
48+
49+
(* exit on C-c *)
50+
top_editor#bind ctrl_c quit;
51+
bottom_editor#bind ctrl_c quit;
52+
53+
let send_key key =
54+
LTerm_edit.Custom (fun () -> vbox#send_event @@ LTerm_event.Key (make_key key)) in
55+
56+
(* switch editors on Tab *)
57+
top_editor#bind tab [send_key @@ `Other LTerm_key.Down];
58+
bottom_editor#bind tab [send_key @@ `Other LTerm_key.Up];
59+
60+
let label = new LTerm_widget.label "Press Tab to switch between editors.\nPress C-c to exit." in
61+
vbox#add ~expand:false label;
62+
63+
Lazy.force LTerm.stdout
64+
>>= fun term ->
65+
LTerm.enable_mouse term
66+
>>= fun () ->
67+
Lwt.finalize
68+
(fun () -> LTerm_widget.run term ~save_state:false ~load_resources:false vbox waiter)
69+
(fun () -> LTerm.disable_mouse term)
70+
71+
let () = Lwt_main.run (main ())

src/lTerm_edit.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class scrollable = object(self)
169169
method calculate_range page_size document_size = (document_size - page_size/2)
170170
end
171171

172-
class edit ?(clipboard = clipboard) ?(macro = macro) () =
172+
class edit ?(clipboard = clipboard) ?(macro = macro) ?(size = { cols = 1; rows = 1 }) () =
173173
let locale, set_locale = S.create None in
174174
object(self)
175175
inherit LTerm_widget.t "edit" as super
@@ -216,7 +216,9 @@ object(self)
216216
val mutable shift = 0
217217
val mutable start = 0
218218
val mutable start_line = 0
219-
val mutable size = { cols = 0; rows = 0 }
219+
val mutable size = size
220+
221+
method! size_request = size
220222

221223
method private update_window_position =
222224
let line_set = Zed_edit.lines engine in

src/lTerm_edit.mli

+4-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ val macro : action Zed_macro.t
6868

6969
(** Class of edition widgets. If no clipboard is provided, then the
7070
global one is used. *)
71-
class edit : ?clipboard : Zed_edit.clipboard -> ?macro : action Zed_macro.t -> unit -> object
71+
class edit :
72+
?clipboard : Zed_edit.clipboard ->
73+
?macro : action Zed_macro.t ->
74+
?size : LTerm_geom.size -> unit -> object
7275
inherit LTerm_widget.t
7376

7477
method engine : edit Zed_edit.t

0 commit comments

Comments
 (0)