Matthew Palmer wrote:
I'm trying to produce a menu effect in PyGTK similar to the Mozilla tab bar,
where a right-click on the tab produces a popup menu to perform various
actions on the tabs (and/or contents thereof). I can get most of the way
(sort of) with a button-press-event handler, but there's a couple of things
I can't manage to effect: (a) A right-click on the untabbed portion of the
tab list still gives the menu, and (b) I can't work out which tab was
clicked on.
Assuming the notebook is displaying tabs and not a popup menu, you need
to iterate through the tab label widgets and find the one that the click
event is in. You can get the tab label widgets via the get_tab_label()
method or by using one of the methods that let you specify the tab label
widget when you create the pages. Be careful of whether the tab widget
has a window or not when testing to see if the click was in a particular
top. Also, check to see if the tab label is MAPPED or not; tabs that
aren't displayed will not be MAPPED, but may have an allocation that's
on the screen.
Here's untested code (adapted from code I use) to do this:
def get_root_allocation(widget):
""" Returns allocation is root coordinates. """
gdk_window = w.window
origin_x, origin_y = gdk_window.get_origin()
x, y, width, height = w.get_allocation()
if w.flags() & gtk.NO_WINDOW:
origin_x += x
origin_y += y
return origin_x, origin_y, width, height
def get_clicked_child(notebook, event):
for child in notebook.get_children():
tab = notebook.get_tab_label(child)
if tab != None and tab.flags() & gtk.MAPPED:
x, y, w, h = get_root_allocation(tab)
if event.x_root >= x and event.x_root <= x + w and event.y_root
>= y and event.y_root <= y + h:
return child
return None
Hope this helps,
John
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/