[Gtk-sharp-list] ButtonPressEvent on TreeView

John Luke jluke@users.sourceforge.net
Mon, 05 Apr 2004 15:01:26 -0400


On Mon, 2004-04-05 at 06:31 +0200, Mikael Hallendal wrote:
> On sön, 2004-04-04 at 23:47 +0200, Daniel Pecos wrote: 
> > Hello!
> 
> Hi,
> 
> > I'm trying to develop a simple app that uses a treeview and I want to
> > add context menus, so I need ButtonPressEvent, isn't it?
> > 
> > I have read FAQ 3.3, but I can't get it working. Could anyone show me a
> > working example?
> 
> something along the lines of this should do:
> 
> public class YourTree : Gtk.TreeView {
> 
>   static GLib.GType gtype = GLib.GType.Invalid;
>   public static new GLib.GType GType {
>     get {
>       if (gtype == GLib.GType.Invalid)
>         gtype = RegisterGType (typeof (YourTree));
>       return gtype;
>     }
>   }
>         
>   public YourTree () : base (GType) 
>   {
>     // Constructor code
>   }
> 
>   protected override bool OnButtonPressEvent (EventButton event)
>   {
>     switch (event.Button) {
>     case 1: 
>       // Let the treeview implementation handle left click
>       return base.OnButtonPressEvent (event);
>     case 3:
>       // Popup your context menu
>       return true; // Prevents other handlers from receiving the event.
>     }
>  
>     return true;
>   }
> }
> 

In addition you will probably also want to show the popup menu on the
Gtk.Widget.PopupMenu event that is emitted on shift + F10.  This is
mentioned in the GTK+ FAQ which I can't seem to get to right now, but it
is mostly for accessibility (keyboard navigation) reasons.