Message-ID: <42FBE5FC.8010103 at rftp.com> Date: Thu, 11 Aug 2005 16:57:48 -0700 From: Robert Roessler Organization: Robert's High-performance Software MIME-Version: 1.0 To: romildo at uber.com.br CC: lablgtk at math.nagoya-u.ac.jp Subject: Re: How to define a customized button References: <20050811120246.GA10175 at malaquias> In-Reply-To: <20050811120246.GA10175 at malaquias> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit romildo at uber.com.br wrote: > I want to learn how to define customized widgets > with lablgtk. For that I would like to define a > new button widget which just automatically encloses > its text label inside square brackets. > > Attached is my attempt in doing so. But it does not > compile. > > As I did not find any explanation on how to > define customized widgets, I am lost on how to > do it. Maybe someone kind enough could show > me how to implement this simple program. How about -------------------- class my_button obj = object (self) inherit GButton.button obj as super method set_label t = super#set_label ("[" ^ t ^ "]") end let pack_return create p ?packing ?show () = GObj.pack_return (create p) ~packing ~show let my_button ?label = let label = match label with | None -> None | Some t -> Some ("[" ^ t ^ "]") in GtkButton.Button.make_params [] ?label ~cont:( pack_return (fun p -> new my_button (GtkButton.Button.create p))) -------------------- To [minimally] test it, you just need -------------------- let delete_event ev = false let main () = let w = GWindow.window ~title:"Test" () in let b = my_button ~label:"my_button" () in let v = GPack.vbox ~packing:w#add () in v#pack b#coerce ~expand:true; ignore (w#event#connect#delete ~callback:delete_event); ignore (w#connect#destroy ~callback:GMain.Main.quit); w#misc#show_all (); GMain.Main.main () let _ = main () -------------------- Yes, it is something of a hack... :) But it does show the classic "inheritance with method override" approach available with objects - the only complicated part about the my_button creation function is handling the optional label - and there may be a cleaner way of doing this. Robert Roessler robertr@rftp.com http://www.rftp.com