Problem with icon_view#get_path_at_pos
Francesco Tovagliari <[email protected]> Fri, 4 Jun 2010 17:55:07 +0200
| Newsgroups | gmane.comp.lang.ocaml.lib.gtk |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I'm trying to set up a context menu when I press the right mouse
button on a cell in a GTree.icon_view but I'm having trouble getting
the tree path from the x, y position of the click event. The problem
is that the program crashes when I try to access the tree_path
obtained with get_path_at_pos.
Here is the code.
test_iconview.ml
(* ocamlc -o test_iconview -I +lablgtk2 lablgtk.cma gtkInit.cmo
test_iconview.ml && ./test_iconview *)
let _ = begin
let window = GWindow.window ~show:false () in
let cols = new GTree.column_list in
let col_text = cols#add Gobject.Data.string in
let col_pixbuf = cols#add (Gobject.Data.gobject_by_name "GdkPixbuf") in
let model = GTree.list_store cols in
let sw = GBin.scrolled_window ~packing:window#add () in
let view = GTree.icon_view ~model ~width:500 ~height:300 ~packing:sw#add () in
view#set_text_column col_text;
view#set_pixbuf_column col_pixbuf;
view#set_margin 0;
let row = model#append() in
model#set ~row ~column:col_text "First Element";
(* Image files from the Lablgtk examples *)
(*model#set ~row ~column:col_pixbuf (GdkPixbuf.from_file
"gnome-fs-directory.png");*)
let row = model#append() in
model#set ~row ~column:col_text "Second Element";
(*model#set ~row ~column:col_pixbuf (GdkPixbuf.from_file
"gnome-fs-regular.png");*)
(* let tpath = view#get_path_at_pos 0 0 in
Printf.fprintf stderr "tpath = %s;\n%!" (GTree.Path.to_string tpath);*)
ignore (view#event#connect#button_press ~callback:begin fun ev ->
let x, y = int_of_float (GdkEvent.Button.x ev), int_of_float
(GdkEvent.Button.y ev) in
let tpath = view#get_path_at_pos x y in
let row = model#get_iter tpath in
Printf.printf "%s\n%!" (model#get ~row ~column:col_text);
false
end);
ignore (window#connect#destroy ~callback:GMain.quit);
window#show();
GMain.main()
end
Does anyone have some idea what can be wrong?
Many thanks.
Francesco Tovagliari