English version
Accueil     Ŕ propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis ŕ jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml ŕ l'adresse ocaml.org.

Browse thread
Does LablTk have a future?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-08-30 (16:04)
From: Jon Harrop <jon@f...>
Subject: Re: GUI for OCaml (was: Re: [Caml-list] Does LablTk have a future?)
On Tuesday 30 August 2005 15:14, Richard Jones wrote:
> Lablgtk2 is a pain, but I think the pain comes from Gtk itself, not
> any shortcomings in lablgtk2 or ocaml.

Yes. My understanding is that lablgtk does a lot to hide the hideousness 

> > However this is a huge task. [...]
> It is a huge task.

Depending on what exactly we're talking about, I think it is entirely 
tractable for one person, let alone a team.

> I'm not even sure what a "functional" API for a 
> GUI toolkit would look like.  Ideas?  Example code snippets?

I think the GUI code should be split into definition (i.e. how the widgets are 
laid out) and execution (i.e. what functions are called for GUI events). The 
former should be functional in style because it is easier to write and more 
succinct and the latter should be imperative in style because it is 
essentially poking a state machine about.

The functional definition could be a data structure that is folded over to 
accumulate the widgets needed by the subsequent event code. For example, 
consider a searchable list with a labelled search box, a scrollable list and 
a button to add new entries (off the top of my head):

let gui =
  `Frame [`Frame [`Label "Search: "; `Entry];
          `Button "Add"];;

let rec make parent accu = function
  | `Frame l -> List.fold_left (make (Frame.create `Top parent)) accu l
  | `Label text ->
      let widget = Label.create ~text parent in
      pack [widget];
      `Label widget :: accu
  | `Entry ->
      let widget = Entry.create parent in
      pack [widget];
      `Entry widget :: accu
  | `List ->
      let parent = build_frame side fill expand parent in
      let list = Listbox.create ~width parent in
      pack ~side:`Left ~fill:`Both ~expand:true [list];
      let scroll_bar = Scrollbar.create parent in
      Listbox.configure ~yscrollcommand:(Scrollbar.set scroll_bar) list;
      Scrollbar.configure ~command:(Listbox.yview list) scroll_bar;
      pack ~side:`Right ~fill:`Y [scroll_bar];
      `List list :: accu
  | `Button text ->
      let widget = Button.create ~text parent in
      pack [widget];
      `Button widget :: accu

let [_; `Entry search; `List list; `Button add] = make top [] gui;;

You'd want to add a call to "pack" after each call to "*.create" and you'd 
want to have HBox and VBox instead of Frame but the basic idea is there. I 
can't see a good way to statically type such code in general so I've left it 
with an incomplete pattern match.

Perhaps we should start by writing such a wrapper that can target either 
labltk or lablgtk?

Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists