As usual, you can cut-and-paste the code below to a file.ml and try it out with command
$ lablgtk2 file.ml
(*CUT HERE*)
(* Right-click menu example in Lablgtk 2.2*)
(* Thu Dec 18 10:31:24 CET 2003 stalkern at tiscalinet.it *)
open Gtk
open GObj
open GMain
let main () =
let testWindowWithMenu = GWindow.window title:"Test Window" () in
let aCallbackID = testWindowWithMenu#connect#destroy callback:Main.quit in
let vbox1 = GPack.vbox packing:(testWindowWithMenu#add) () in
(* Menus are the children of menu_shells and thus the sibling of menu_bars.
* See Description of GtkMenu at http://developer.gnome.org/doc/API/2.2/gtk/GtkMenu.html
*)
let menu = GMenu.menu () in
let menuItem =
GMenu.menu_item
label:"Print PERHAPS"
packing: menu#append
()
in
let aCallbackID =
menuItem#connect#activate callback:(fun () -> (print_endline "PERHAPS"; flush stdout))
in
let menuItem =
GMenu.menu_item
label:"Print MAYBE"
packing: menu#append
()
in
let aCallbackID =
menuItem#connect#activate callback:(fun () -> (print_endline "MAYBE"; flush stdout))
in
let button = GButton.button label:"Can You Right-Click Me?" packing:(vbox1#add) () in
let _ =
(*after a suggestion by Olivier Andrieu*)
button#event#connect#button_press
callback:
(fun event ->
let () =
(* Let’s filter the event: the "button" detected here is not a "Gtk button"
* but a _mouse_ button
*)
if ((GdkEvent.Button.button event) = 3) then
(GtkMenu.Menu.popup
button:3
time:(GdkEvent.get_time event)
(menu#as_menu))
(* You can also use:
menu#popup
button:3
time:(GdkEvent.get_time event)
*)
else
()
in
false
)
in
testWindowWithMenu#show ();
Main.main ()
;;
let _ = main ();;
(*CUT HERE*)
|