import System import Gtk from 'gtk-sharp' import GLib from 'glib-sharp' class MyWindow (Window): # Class reference to the menu we will want to pop up when we get a right click on the TreeViewColumn header menu = Menu () def constructor (title as string): # Chain the constructor call super (title) # add our menu item and show it menu.Append (MenuItem ("Item, yo!")) menu.ShowAll () # Create the TreeView, and make sure the headers are 1) visible, and 2) clickable view = TreeView () view.HeadersVisible = true view.HeadersClickable = true # Our simple store store = ListStore (typeof (string)) store.AppendValues ("value 1") store.AppendValues ("value 2") view.Model = store # Create our column, and add a simple renderer. col = TreeViewColumn () cell = CellRendererText () col.PackStart (cell, true) col.SetAttributes (cell, "text", 0) col.Clickable = true # NOTE: You *MUST* add the column to the view before trying to do the next magic to find the button to use for grabbing events. # This is thanks to some funkiness in how gtk+ handles creating the button for the header view.AppendColumn (col) # NOTE: TreeViewColumn.Widget only returns the widget being used in the header if a custom one is set, otherwise null. # As such, we add our own Label here, instead of using the internally supplied one. col.Widget = Label ("Right Click Me!") col.Widget.Show () # The following hack loops over the custom widget's parents, until finding a widget of type Gtk.Button. This will be the button # used for the header for the column. parent_widget = col.Widget.Parent parent_button = parent_widget as Button while (parent_widget != null and parent_button == null): parent_widget = parent_widget.Parent parent_button = parent_widget as Button # Hook up to the low leve ButtonPressEvent for our newly discovered TreeViewColumn header button. if parent_button is not null: parent_button.ButtonPressEvent += OnHeaderButtonPress Add (view) # NOTE: This has to have the ConnectBeforeAttribute, so the button press event isn't handled by TreeViewColumn, and never seen here. [GLib.ConnectBefore] def OnHeaderButtonPress (o as object, e as ButtonPressEventArgs): if e.Event.Button == 3: menu.Popup () # Make sure we stop propogation of the event by setting RetVal = false e.RetVal = false # Actually run the little sample Application.Init () w = MyWindow ("Right clickable TreeView headers!") w.DeleteEvent += { Application.Quit () } w.ShowAll () Application.Run ()