To: lablgtk at math.nagoya-u.ac.jp Subject: Pixbuf combo box From: Dmitry Bely Date: Fri, 16 Dec 2005 10:27:07 +0300 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii I am quite new to both Gtk and lablgtk, so excuse me if my question is obvious to the local gurus :-) I am creating a simple image editor with some custom fuctions. I need to select a brush there. I think of using the combo box widget filled with pixbufs of the brushes available (squares/circles of different sizes). But how to do this? I am looking into combobox.ml demo, but don't understand how to fill empty pixbufs in (setup_combobox_demo_grid) with something usable. All these cell renderers etc. don't seems to be self-explaining and quite simple to use. Here is my first attempt (modified combo box example): let icon_width = 8 let icon_height = 8 let brush_pixbuf size = let x = (icon_width - size) / 2 in let y = (icon_height - size) / 2 in let pm = new GDraw.pixmap ~colormap:(Gdk.Rgb.get_cmap ()) (Gdk.Pixmap.create ~width:icon_width ~height:icon_height ~depth:32 ()) in pm#set_foreground `WHITE; pm#rectangle ~x:0 ~y:0 ~width:icon_width ~height:icon_height ~filled:true (); pm#set_foreground `BLACK; pm#rectangle ~x ~y ~width:size ~height:size ~filled:true (); let pb = GdkPixbuf.create ~width:icon_width ~height:icon_height () in GdkPixbuf.get_from_drawable ~dest:pb pm#pixmap; pb let changed_and_get_active (combo : #GEdit.combo_box) column cb = combo#connect#changed (fun () -> match combo#active_iter with | None -> () | Some row -> let data = combo#model#get ~row ~column in cb data) let cols = new GTree.column_list let col_pix: GdkPixbuf.pixbuf GTree.column = cols#add Gobject.Data.gobject let create_and_fill_model () = let liststore = GTree.list_store cols in (* Append a row, and fill it with some data *) let append pix = let row = liststore#append () in liststore#set ~row ~column:col_pix pix in List.iter append [ (brush_pixbuf 1); (brush_pixbuf 4); (brush_pixbuf 8); ]; liststore let setup_combobox packing = let tmp = GBin.frame ~label:"GtkComboBox (pixbuf)" ~packing () in let box = GPack.vbox ~border_width:5 ~packing:tmp#add () in let model = create_and_fill_model () in let combo = GEdit.combo_box ~model ~packing:box#pack () in let cell = GTree.cell_renderer_pixbuf [] in combo#pack cell ; combo#set_active 0 ; changed_and_get_active combo col_pix (fun id -> prerr_endline ""); () let main () = let window = GWindow.window ~border_width:5 () in window#connect#destroy GMain.quit ; let mainbox = GPack.vbox ~spacing:2 ~packing:window#add () in setup_combobox mainbox#pack ; window#show () ; GMain.main () let _ = main () (* Local Variables: *) (* compile-command: "ocamlc -I ../src -w s lablgtk.cma gtkInit.cmo combobox.ml" *) (* End: *) - don't work as expected. Maybe you recommend some docs on that? The tutorial seems to cover only text combo boxes. Or maybe another widget is more adequate for this purpose? - Dmitry Bely