Delivered-To: garrigue at math.nagoya-u.ac.jp Authentication-Results: mailhost.math.nagoya-u.ac.jp sender=lablgtk-bounces at yquem.inria.fr; domainkey=neutral (no query protocol specified; no policy for yquem.inria.fr) Delivered-To: lablgtk at yquem.inria.fr DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:date:message-id :subject:from:to:content-type; bh=WiSbMAOiITs4YCcDNr3cjRY7+TCfQYa4xbOV3Nxex4c=; b=s2NhkdLpfHkgju0safkcS2AIjXK9zikFT9hrCUPxF9LQ2KnFX34nqTw1nnwzhX5a4s +k1iUnkCOo2F5Rs5VHjj+vPz+6e2MOjEgvFhaY1Mr9CfB4hMQrhOoYXpZzRFd2nOrNN0 A4GqL7abHATcL8jeAtR2qiJqyBRKr2rxCxeGI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=eGd+AulvTv7wEIZeyJq6Z5AKII9nf05AsF7RMkTfX62vSqSvmbMWCblxE30vJ/ETcn XCw8U/hjRBTCsp833bQZtEK869pPiDHSrZ839BBAyvSjuTRL0cwxHo9jWIH/ElvCDWMX 1UsOMcuz1gk7nzcJ7R1S5lxjqrAHBIZ9rR3cc= MIME-Version: 1.0 Date: Tue, 16 Nov 2010 12:28:00 +0100 Message-ID: From: Serge Le Huitouze To: lablgtk Content-Type: text/plain; charset=ISO-8859-1 Subject: [Lablgtk] Advice on clist Status: U Hi there, I'm willing to code a widget akin to multi-file selection. After a lot of fiddling with lablgtk2 (and gtk) documentation, I finally got something that seems to work. I thought I'd take the liberty to post my small code (based on Maxence Guesdon's "clist.ml" example in lablgtk's distribution) to seek advice from you gurus! It's very easy to try: Just compile it: ocamlc -I +lablgtk2 -o ex_clist lablgtk.cma gtkInit.cmo ex_clist.ml Then run it: ./ex_clist You can add an entry (eventually, there will be a file-selection widget here), remove the selected entry (if such exists) or clear all entries (if you have at least one). I'd like to have some feedback to know if that's the way one should write such code. In particular, am I right in managing the selected row by myself? IIUC, there is no way to get the selected row from a clist, one has to do it oneself by catching callback "select_row" and "unselect_row"... [In contrast, I don't need to manage myself the number of rows, that I can directly access via the "rows" method.] Also, any stylistic remark (be it lablgtk or Ocaml oriented) is welcome! Thanks for your time. --Serge ---- START OF PROGRAM ------ open StdLabels open GMain let main () = let window = GWindow.window ~title:"Multifile" ~width:300 ~height:150 () in ignore (window#connect#destroy ~callback:Main.quit); let vbox = GPack.vbox ~border_width:5 ~packing:window#add () in let hbox = GPack.hbox ~packing:vbox#add () in let sb = GRange.scrollbar `VERTICAL ~packing:(hbox#pack ~from:`END) () in let clist = GList.clist ~titles:["_ENTRIES_"] ~shadow_type:`OUT ~packing:hbox#add ~vadjustment:sb#adjustment () in (* Layout all the buttons before defining clist's callback to allow * for enabling/disabling some of them *) let hbox = GPack.hbox ~packing:vbox#pack () in let button_add = GButton.button ~label:"Add entry" ~packing:hbox#add () in let button_remove = GButton.button ~label:"Remove" ~packing:hbox#add () in let button_clear = GButton.button ~label:"Clear" ~packing:hbox#add () in let curSelectedRow = ref None in let unselectRow () = curSelectedRow := None in let getSelectedRow () = match !curSelectedRow with None -> 0 | Some r -> r in let updateButtonsStatus () = begin button_clear#misc#set_sensitive (clist#rows != 0); button_remove#misc#set_sensitive (match !curSelectedRow with None -> false | _ -> true) end in ignore (clist#connect#select_row ~callback: begin fun ~row ~column ~event -> curSelectedRow := Some row; updateButtonsStatus () end); ignore (clist#connect#unselect_row ~callback: begin fun ~row ~column ~event -> unselectRow (); updateButtonsStatus () end); let globalAddCnt = ref 0 in ignore (button_add#connect#clicked ~callback: begin fun () -> Printf.printf "A file-selection widget should be used\n"; flush stdout; let new_entry = Printf.sprintf "entry%d" !globalAddCnt in globalAddCnt := !globalAddCnt + 1; ignore (clist#append [new_entry]); updateButtonsStatus () end); ignore (button_remove#connect#clicked ~callback: begin fun () -> clist#remove (getSelectedRow ()); unselectRow (); updateButtonsStatus () end); ignore (button_clear#connect#clicked ~callback: begin fun () -> clist#clear (); unselectRow (); updateButtonsStatus () end); window#show (); updateButtonsStatus (); Main.main () let _ = main () ---- END OF PROGRAM ------ _______________________________________________ Lablgtk mailing list Lablgtk@yquem.inria.fr http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk