Date: Fri, 04 Mar 2005 10:57:49 +0100 (CET) Message-Id: <20050304.105749.112609272.andrieu at ijm.jussieu.fr> To: aubineth at ccs.neu.edu, rich at annexia.org Cc: lablgtk at kaba.or.jp Subject: Re: tree view models written in ocaml From: Olivier Andrieu In-Reply-To: <20050303234255.GA25179@furbychan.cocan.org> References: <20050303234255.GA25179@furbychan.cocan.org> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Ethan Aubin [Sat, 19 Feb 2005]: > > Hi, I'm trying to figure out Tree Views but I'm running into some > problems. I've a few objects/records written in ocaml I'd like to > use as the model of the tree view, but I'm missing how to setup the > column views, so that they pull and format (I can convert what I > need into text) the right fields. > > type gender = Male | Female > type person = {name : string; gender : gender; age : int} You can use the Gobject.Data.caml converter : it has type 'a Gobject.data_conv, so you can store an arbitrary caml value in it. Here is how to do it (roughly) : ,---- | let column_list = new GTree.column_list in | let column = column_list#add Gobject.Data.caml in | let model = GTree.list_store column_list in | List.iter | (fun p -> | let row = model#append () in | model#set ~row ~column p) | persons ; | let viewcol = GTree.view_column () in | let renderer = GTree.cell_renderer_text [] in | viewcol#pack renderer ; | viewcol#set_cell_data_func renderer | (fun m row -> | let p = m#get ~row ~column in | let text = string_of_person p in | renderer#set_properties [`TEXT text]) ; | let view = GTree.view ~model () in | ignore (view#append_column viewcol) ; | view `---- Richard Jones [Thu, 3 Mar 2005]: > Is it better to write my own tree model? (And if so, where on > earth do I start? I have no idea how to go about this.) You can't really write your tree model in caml, you'd need to write it in C. > Or is it better, as was suggested on one of the Gtk lists, to use a > Gobject.Data.pointer or Gobject.Data.boxed to "hang" my extra > structures on to a row, and just use the ordinary GtkTreeModel? > (I'm not exactly sure how to do this one either ...) Using Gobject.Data.caml is the way to go I'd say : this is the moral equivalent of G_TYPE_POINTER (or G_TYPE_BOXED), i.e. some data, opaque for the GtkTreeView, which is displayed using a GtkTreeCellDataFunc. HTH, -- Olivier