Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions examples/double_editor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,63 @@ let main () =
let waiter, wakener = Lwt.wait () in

let ctrl_c = [make_key ~ctrl:true @@ `Char 'c'] in
let ctrl_d = [make_key ~ctrl:true @@ `Char 'd'] in
let tab = [make_key @@ `Other LTerm_key.Tab] in
let quit = [LTerm_edit.Custom (Lwt.wakeup wakener)] in

let vbox = new LTerm_widget.vbox in

let top_editor = new LTerm_edit.edit () in
let top_frame = frame top_editor in
top_frame#set_label "Editor 1";

(* make bottom editor a fixed 10 rows in size *)
let bottom_editor = new LTerm_edit.edit ~size:{ rows = 10; cols = 1 } () in
(* changed my mind: make it 5 rows smaller *)
bottom_editor#set_allocation
{ bottom_editor#allocation with row1 = bottom_editor#allocation.row1 - 5 };
let bottom_frame = frame bottom_editor in
bottom_frame#set_label "Editor 2";
let editor_box = new LTerm_widget.vbox in

vbox#add top_frame;
editor_box#add top_frame;
(* in versions before PR#42 this would either crash or make the bottom editor unusable *)
vbox#add ~expand:false bottom_frame;
editor_box#add ~expand:false bottom_frame;

vbox#add editor_box;

let swap =
let top = ref true in
let swapper () =
if !top then begin
editor_box#clear;
editor_box#add ~expand:false bottom_frame;
editor_box#add top_frame;
top := false
end else begin
editor_box#clear;
editor_box#add top_frame;
editor_box#add ~expand:false bottom_frame;
top := true
end in
(* Alternative Version (slightly better actually) *)
(*
let swapper () =
if !top then begin
editor_box#remove bottom_frame;
editor_box#add ~position:0 ~expand:false bottom_frame;
top := false
end else begin
editor_box#remove top_frame;
editor_box#add ~position:0 top_frame;
top := true
end in
*)
[LTerm_edit.Custom swapper] in

(* swap on C-d *)
top_editor#bind ctrl_d swap;
bottom_editor#bind ctrl_d swap;

(* exit on C-c *)
top_editor#bind ctrl_c quit;
Expand All @@ -54,10 +93,15 @@ let main () =
LTerm_edit.Custom (fun () -> vbox#send_event @@ LTerm_event.Key (make_key key)) in

(* switch editors on Tab *)
top_editor#set_focus { top_editor#focus with down = Some (bottom_editor :> LTerm_widget.t) };
bottom_editor#set_focus { bottom_editor#focus with up = Some (top_editor :> LTerm_widget.t) };
top_editor#bind tab [send_key @@ `Other LTerm_key.Down];
bottom_editor#bind tab [send_key @@ `Other LTerm_key.Up];

let label = new LTerm_widget.label "Press Tab to switch between editors.\nPress C-c to exit." in
let label =
let descr =
"Press Tab to switch between editors.\nPress C-d to swap editors.\nPress C-c to exit." in
new LTerm_widget.label descr in
vbox#add ~expand:false label;

Lazy.force LTerm.stdout
Expand Down
3 changes: 3 additions & 0 deletions src/lTerm_widget.mli
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class type box = object

method remove : #t -> unit
(** [remove widget] remove a widget from the box. *)

method clear : unit
(** [clear] removes all widgets from the box. *)
end

(** A widget displaying a list of widgets, listed horizontally. *)
Expand Down
8 changes: 8 additions & 0 deletions src/widget_impl/lTerm_containers_impl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class type box = object
inherit t
method add : ?position : int -> ?expand : bool -> #t -> unit
method remove : #t -> unit
method clear : unit
end

class virtual abox rc = object(self)
Expand Down Expand Up @@ -68,6 +69,13 @@ class virtual abox rc = object(self)
self#compute_size_request;
self#compute_allocations;
self#queue_draw

method clear =
List.iter (fun child -> child.widget#set_parent None) children;
children <- [];
self#compute_size_request;
self#compute_allocations;
self#queue_draw
end

class hbox = object(self)
Expand Down