Assigning an accelerator to a button

Erkki Seppala <[email protected]> Sat, 22 Feb 2014 14:41:09 +0200
Newsgroups gmane.comp.lang.ocaml.lib.gtk
Message-ID <[email protected]>
Hello,

I'm having trouble with the following piece of code:

--8<--
(* ocamlfind ocamlc  -o foo -linkpkg -package lablgtk2,lablgtk2.auto-init foo.ml *)

open Gtk

let destroy () = GMain.Main.quit ()

let cast_button_signal_as_widget_signal
  (x : ([`button], unit -> unit) GtkSignal.t) :
  (Gtk.widget, unit -> unit) GtkSignal.t =
  Obj.magic x

let main () = 
  let main_window = GWindow.window () in
  let accel_group = GtkData.AccelGroup.create () in
  main_window#add_accel_group accel_group;
  let quit_button = GButton.button ~label:"Quit" ~packing:main_window#add () in
  quit_button#misc#add_accelerator
    ~sgn:(cast_button_signal_as_widget_signal GtkButtonProps.Button.S.activate)
    ~group:accel_group
    ~modi:[`CONTROL] GdkKeysyms._q;
  ignore (quit_button#connect#clicked ~callback:destroy);
  main_window#show ();
  GMain.Main.main ()

let _ = main ()

--8<--

The problem is in the binding of the Ctrl-q to the quit button (doing
the same with menu items has many examples on the net). This solution
works, but the function involving Obj.magic is far from optimal.

So, perhaps there's a better way? Or perhaps this is a bug?

Thanks!

-- 
  _____________________________________________________________________
     / __// /__ ____  __               http://www.modeemi.fi/~flux/\   \
    / /_ / // // /\ \/ /                                            \  /
   /_/  /_/ \___/ /_/\_\@modeemi.fi                                  \/