Menus

Odysseus’s headerbar includes dropdown menus triggered by holdclick or rightclick on any toolbar button, thereby making this combined toolbar/titlebar also a menubar.

Though the “application menu” also shows this menu on left click, to show lesser used options.

This morning I want to describe how these menus are implemented in GTK.

The GtkMenu implements most of the logic, and can optionally add additional scrolling and “tear off” menu options neither of which I use. It’s rendered into it’s own GdkWindow (hence it can overflow the parent window) and largely focuses on:

  1. Computing where to show the window. There are multiple formulas you can call as methods.
  2. Laying out it’s children in a grid, mostly just assigning rows automatically.
  3. Dispatching events to it’s children.

These GtkMenu widgets subclass GtkMenuShell which performs the data modelling, and allows polymorphism between them and menubars.

Their children are almost always GtkMenuItem or GtkImageMenuItem widgets. GtkMenuItem doesn’t add much more (mostly just wrapping a GtkLabel), with most of it’s code dedicated to showing the associated submenu if any.

GtkImageMenuItem as it turns is now deprecated, but amounts to little more than also packing an image into the menuitem.