Dima
Dima

Reputation: 2072

How to make GtkMenuToolButton open the same menu when 'clicked' signal is emitted?

I am using GtkMenuToolButton and it has a button and a menu. When you click on the arrow the menu is opened. I'd like to make the button open that same menu as well. Simply emitting "show-menu" in the "clicked" callback did not work. Please help how to make this work.

Upvotes: 1

Views: 229

Answers (2)

ptomato
ptomato

Reputation: 57860

When you create the menu, save a reference to it as self.tool_button_menu or something; then in the clicked callback, call

self.tool_button_menu.popup(None, None, None, None, 0, Gtk.get_current_event_time())

The first two Nones are the parent menu and the parent menu item (not applicable). The second two Nones are a positioning callback function (more on that in a minute) and data to pass to it. 0 is the mouse button if the menu was initiated by a mouse button press (but you should pass 0, because I think in your case it's either a mouse button release or a key press.) And the last parameter is the timestamp to give to the menu popup event.

Now the positioning function. It takes two parameters and returns three:

def positioning_function(menu, data=None):
    # ...magic...
    return x, y, push_in

push_in should be True if you want the menu to be repositioned so that it always fits on the screen. Seems like a good idea. You can get good values for x and y by looking at the tool button's get_allocation(); read the x, y, width and height attributes of that object and calculate a nice place to put the menu.

Upvotes: 1

Dima
Dima

Reputation: 2072

I have currently ended up doing this:

  • Instead of GtkMenuToolButton I have GtkToolItem with custom content
  • In custom content I have GtkMenuButton
  • Inside that one, I delete the default GtkArrow and replace it with 1x2 GtkGrid which has a Label + GtkArrow

As a whole it does what I want =)

Upvotes: 1

Related Questions