[Glade-users] Using Actions + MenuItems + Accelerators + AccelLabels



On Mon, 2009-10-05 at 11:41 -0500, Manuel Alejandro Cer?n Estrada wrote:
2009/10/5 Alexey Kurochkin <alexey.kurochkin at pathfinderlwd.com>:

I'd like to see the answer too. Currently I create only the main menu
bar and empty sub menus with Glade, and all the menu items are created
and appended to the sub menus manually in the code. It results in
hundreds of lines of clutter which I'm looking forward to get rid of.

I've been looking information about it, and there are two problems:
First, Glade currently doesn't support GtkActionGroup well. Second
GTK+ doesn't have a property for accelerator in GtkAction, the only
way to assign accelerators to actions is using
GtkActionGroup.add_action_with_accel(). Thereby, we have for one of
those problems to be fixed.

My current approach for the problem, trying to avoid clutter code as
much as possible, is: Creating the main menu using GtkUIManager
instead of Glade. I manually wrote my main menu using a UIManager xml
description. Something like this:

<ui>
<menubar name="mainmenu">
<menu name="file_menu" action="file_action">
<menuitem name="open_recording_menuitem" action="open_recording_action"/>
<menuitem name="open_recording2_menuitem" action="open_recording2_action"/>
<separator/>
<menuitem name="quit_menuitem" action="quit_action"/>
</menu>
...
</menubar>
</ui>

The actions referred in the UIManager file are created in the Glade
file. Then, in code I do:

- Create an GtkActionGroup and add all the actions defined in the
Glade file to it.
- Create a UIManager and load the file with the menu defined.
- Insert the ActionGroup to the UIManager
- Assign the AccelGroup of the UIManager to the current window.
- Get the main menu from the UIManager and manually insert it in the window.

Manuel.


I used to do it before, but I've found it rather inconvenient for two
reasons. First, writing xml by hand is not that much different from
copy/paste five lines of code per menu item (get action from builder,
set accelerator group for it, add it to the group, create menu item from
the action, and append it to the menu shell). I can write GtkBuilder xml
too, but what I need Glade for then? Second, the changes to the ui file
have to be synchronized with code and/or glade anyway, so I figured it
is less hassle to keep it all in one source file despite the length. It
is easier to maintain this way. I'm not going back to ui manager unless
I'd really need to build dynamic (merge/unmerge) menus.






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