Friday, May 25, 2007

Labltk is somewhat more primitive than Tk, but Tk still doesn't have native support for a tabbed interface, or notebook. This is a function which will make a widget that works pretty much like a tabbed interface.

open Tk

let create_notebook frames top =
let tv = Textvariable.create () in
let currentframe = ref None in
let bounding = Frame.create ~borderwidth:2 ~relief:`Raised top in
let buttonframe = Frame.create bounding in
let bottomframe = Frame.create ~relief:`Groove bounding in
let buttons =
List.map
(fun (name,f) ->
let f = f bottomframe in
name,f,Button.create ~text:name ~command:(fun () -> Textvariable.set tv name) buttonframe
)
frames
in
let trd (_,_,c) = c in
if frames <> [] then pack ~side:`Left (List.map trd buttons);
let rec toggle () =
(
match !currentframe with
None -> ()
| Some f -> Pack.forget [f]
);
let b = Textvariable.get tv in
List.iter
(fun (n,f,button) ->
if n = b then
(
Button.configure ~state:`Disabled button;
if Pack.slaves f <> [] then
Frame.configure ~borderwidth:2 bottomframe
else
Frame.configure ~borderwidth:0 bottomframe;
pack [f];
currentframe := Some f
)
else
Button.configure ~state:`Normal button
)
buttons;
Textvariable.handle tv ~callback:toggle;
in
Textvariable.handle tv ~callback:toggle;
pack ~anchor:`W [buttonframe];
pack ~fill:`Both ~expand:true [bottomframe];
if buttons <> [] then Button.invoke (trd (List.hd buttons));
bounding


Here is a function you can use to test it:
let test () =
let top = openTk () in
let frames =
["one",(fun f -> let f2 = Frame.create f in let l = Label.create ~text:"one" f2 in pack [l]; f2);
"two",(fun f -> let f2 = Frame.create f in let l = Label.create ~text:"two" f2 in pack [l]; f2);
"nothing",(fun f -> Frame.create f)]
in
let nb = create_notebook frames top in
pack ~fill:`Both ~expand:true [nb];
mainLoop ()

2 comments:

Anonymous said...

www.reverse-phonedective-review.com

Anonymous said...

www.tradinguide.blogspot.com