Search code examples
c#monogdk

Why not getting `ButtonPressEvent`


I am experimenting a bit with monodevelop/c#/gdk and I was able to create a window with a DrawingArea correctly handling the expose event.

The mouse down events however are not dispatched and I don't understand why. The delegates have been set up in the code autogenerated by the gui designer:

this.da.ExposeEvent += new global::Gtk.ExposeEventHandler (this.OnDAExposeEvent);
this.da.ButtonPressEvent += new global::Gtk.ButtonPressEventHandler (this.OnDAButtonPressEvent);
this.da.MotionNotifyEvent += new global::Gtk.MotionNotifyEventHandler (this.OnDAMotionNotifyEvent);

and this is my initialization code:

public MainWindow () : base (Gtk.WindowType.Toplevel)
    {
        Build ();
        Gdk.Color col = new Gdk.Color();
        col.Red = col.Green = col.Blue = 0x8888;
        da.ModifyBg(StateType.Normal, col);
        var p = "wr/wn/wb/wq/wk/wp/br/bn/bb/bq/bk/bp".Split ('/');
        for (int i = 0; i < p.Length; i++) {
            pieces[p[i]] = new ImageSurface("/home/agriffini/x/chessboard/i" + p [i] + ".png");
        }
        da.Events |= (Gdk.EventMask.ButtonPressMask
                      | Gdk.EventMask.ButtonReleaseMask
                      | Gdk.EventMask.KeyPressMask
                      | Gdk.EventMask.PointerMotionMask);
    }

however the handler function OnDAButtonPressEvent never gets called (checked by placing a breakpoint there).

What is the part that is missing?


Solution

  • Found the problem by looking at console output.

    The issue is that event mask must be set before the widget is realized.

    This means that when using monodevelop and the gui designer you must set event handlers using the gui in widget->properties->events because the autogenerated Build method will set the event mask at the proper time before realization.

    Setting the event mask in the code before calling Build wouldn't work (widget have not been created yet); setting the mask after that call wouldn't work either (they've been already realized).