Tuesday, June 12, 2012

GTK3: How to create menu button?

In Gtk3, we are provided with GtkMenuToolButton GtkMenuToolButton to show a dropdown menu from a button, but this toolbutton consists of two parts: a button and an additional button that popups a menu when toggled. The first button is useless when we plan to use GtkMenuToolButton as a menu button, thus requires us to find new solution.

There are several methods to create menu button:
  1. Connect the "clicked" or "toggled" signal of a button or toggle button to a callback that pops up a menu. For this method, we need to create a custom positioning function, which is to me a tedious work.
  2. Customize the GtkMenuToolButton by getting it's child and remove the button part as well as replacing the GtkArrow with another widget.
  3. Using GtkMenuBar.
This article only covers the 3rd solution.

Convert GtkMenuBar into Menu Button

This is the simplest way to create a menu button. Before this, I used the second method to come up with a menu button until I updated my Gnome installation to version 3.4 and found out Epiphany 3.4['s] way of implementing this. I don't know why am I so stupid to come up with that method? :(

What the developer did, is just changing the style class of the GtkMenuBar into button style. That's all. As easy as that! >.<

Code (in python)
menub = Gtk.MenuBar()
onlyitem = Gtk.MenuItem()
onlyitem_child = Gtk.Button(label = "Menu")
onlyitem.add(onlyitem_child)
 
menu = Gtk.Menu()
mitem1 = Gtk.MenuItem(label = "Item 1")
mitem2 = Gtk.MenuItem(label = "Item 2")

menu.insert(mitem1, 0)
menu.insert(mitem2, 1)
onlyitem.set_submenu(menu)
menub.insert(onlyitem, 0)

menub.get_style_context().add_class("button") #this is the key line.

**UPDATE**
Since Gtk+ 3.6, a native menu button widget, GtkMenuButton has been added.

Saturday, June 9, 2012

GtkGrid example

I've just upgraded my Fedora 16 installation to Fedora 17 which includes gtk3-3.4. I was working on some hobby project—written in genie—of mine before the upgrade. After upgrading, I tried to compile the source code, but got deprecation errors for GtkHBox and GtkVBox. So I checked devhelp, and was suggested to use GtkBox instead. So I read GtkBox documentation, then I found out that it's also will face the same fate as it's brothers. GtkGrid became the new way. I kinda don't like this change. But anyhow, life must goes on.

Continuing on, GtkGrid is similar to GtkTable, but unlike GtkTable, it stores the child's expand and margin property instead of having its container to give allocation to it. Seems to me to be GtkBuilder or GtkUIManager (I never used this before) friendly.
 
In genie, If you want to create a box with 3 buttons with the following layout
[<button1:!expand><<<<button2:expand>>>>><button3:!expand>]

the old way to do this is
...
var
     box = new HBox(false, 0)
     b1 = new Button.with_label("button1")
     b2 = new Button.with_label("button2")
     b3 = new Button.with_label("button3")

box.pack_start(b1, false, true, 0)
box.pack_start(b2, true, false, 0)
box.pack_start(b3, false, true, 0)
...

now, with GtkGrid, you had to do like this

...
var
     grid = new Grid
     b1 = new Button.with_label("button1")
     b2 = new Button.with_label("button2")
     b3 = new Button.with_label("button3")

b2.expand = true
grid.attach(b1, 0, 0, 1, 1)
grid.attach(b2, 0, 1, 1, 1)
grid.attach(b3, 0, 2, 1, 1)
...