action based menu code



I was working on a prototype for a MVC menu system where there is a group
of action objects, and you can create menu items or toolbar buttons as
proxies for the action.

A snapshot of the source is available at:
  http://www.daa.com.au/~james/files/gmenu.tar.gz
(run make, then run the tester program that gets built).

None of the interfaces are that great (especially the ones for creating
the menu items/toolbar buttons -- this would probably be replaced by code
that parsed an XML file using libxml or gmarkup or something).  But it
does demonstrate the principles.

Some of the features of the code include:

- Three types of actions -- normal, toggle and radio actions.  These
  correspond to the equivalent menuitem/button types.

- Actions include a name (not translated), label, tooltip and stock image.

- User callbacks can be attached to the action's activate signal.  Toggle
  and radio actions also have a toggled signal.

- Actions can have accelerators associated with them, as they implement
  the same accelerator interfaces that GtkWidget does.

- Menu item proxies set the accel label to watch accelerators on the
  action, so if you have two menu items corresponding to the same action,
  they will display the same accelerator.

- Toggle and radio actions synchronise the toggled state of all associated
  proxies.  So if you check a menu item and there is a toolbar button
  associated with the same action, it will toggle.

- attempts to dynamically set accelerators on a menu item proxy get
  redirected to add accelerators on the action, so show up on all menu
  items associated with the action.  This has some problems though, noted
  below.

The problem with dynamically changing accels occurs because of the way
these changes are performed.  In gtk_menu_key_press, it removes any accels
on the menu item that call the activate signal, then adds the new
accelerator.

Unfortunately with my code, the menu item itself doesn't have any accels
set on it ever, so none get removed.

To get the accels to work correctly, there would need to be some new API
added to allow setting an "accel proxy" property on the menu item.  If
this was set, the code in gtk_menu_key_press would change accelerators on
that object.  The code would also need to be changed to not assume that it
was setting accelerators on a GtkWidget (in my code, the actions are
GObjects).

One other bug I noticed was that a GtkAccelLabel doesn't queue an
accelerator refetch when gtk_accel_label_set_accel_object is called.  This
means that if the accelerators are set on the object before
set_accel_object() is called, they won't get displayed.  This should be
trivial to fix.

James.

-- 
Email: james daa com au
WWW:   http://www.daa.com.au/~james/






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]