(Clearly I've been neglecting my blog for the last couple of months. To try and change that, I'll start with this small post, just to get the ball rolling again.) If it is unclear from the title, the Firefox widget I'm talking about is the toolbar buttons you get in the bookmarks toolbar that pops up a menu when you click it. I just wanted to take its functionality a bit further for Virtaal: Creating a similar widget which is not limited to a toolbar. This is not the first time this has been attempted. A very intriguing approach (discussed in this thread on the PyGtk mailing list) sub-classes the gtk.MenuToolButton class. This seems to be more stable than my approach, but still limits the button to use in a toolbar. As with all Gtk+ deep hacking, I try and keep the code as re-usable as possible. So I present to you: PopupButton. PopupButton is a sub-class of gtk.ToggleButton, so it is nothing more than a plain button. It turns out that turning a toggle button into the widget I want only meant adding a menu member and managing its pop-up/pop-down events. It works quite well for something this simple. There is only one major issue left with this widget, and that is localised to the _calculate_popup_pos() method. Calculating the location of where the menu should be popped up proved to be a lot more difficult than I expected it to be. It makes the whole thing seem pretty ugly, but I'm sure it will eventually be fixed. So there it is! You have the code, so please let me know if you have any comments, improvements, flames, or whatever. |
|||
