[Gtk-sharp-list] Adding a shortcut to menu or a button

Andy Selvig ajselvig at gmail.com
Tue Aug 26 15:48:30 EDT 2008


After studying the code generated by Stetic, I found a (round about)
way to do it.

Creating the action group is pretty straight forward:

Gtk.ActionGroup actionGroup = new Gtk.ActionGroup("Demo");
Gtk.ActionEntry fileAction = new Gtk.ActionEntry("file", null, "File",
null, null, null);
Gtk.ActionEntry newAction = new Gtk.ActionEntry("new", Gtk.Stock.New,
"New", "<Ctrl>n", "Create a new file.", OnNewFile);
Gtk.ActionEntry openAction = new Gtk.ActionEntry("open",
Gtk.Stock.Open, "Open", "<Ctrl>o", "Open a file.", OnOpenFile);
actionGroup.Add(new Gtk.ActionEntry[]{fileAction, newAction, openAction});

When I do this, then try to get a menu item with
actionGroup.GetMenuItem(), I get the following runtime assertion
failure:

Gtk-CRITICAL **: gtk_accel_label_set_accel_closure: assertion
`gtk_accel_group_from_accel_closure (accel_closure) != NULL' failed

The menu item works, but again the accelerator doesn't. In order to
get the accelerator working and displayed properly, I need to create a
UiManager and populate it with a string:

Gtk.UIManager uiManager = new Gtk.UIManager();
uiManager.InsertActionGroup(actionGroup, 0);
AddAccelGroup(uiManager.AccelGroup);			
uiManager.AddUiFromString("<ui><menubar name='menubar'><menu
action='file'><menuitem action='new'/><menuitem
action='open'/></menu></menubar></ui>");

Then I can get the fully functional menu bar with
uiManager.GetWidget("/menubar").

This seems to be a working solution, although creating the ui string
is hackish and undesirable. I understand that I could define the ui in
a file, but that doesn't suite my particular application as the ui is
already defined in a platform neutral xml file that I'm parsing
independently.



On Tue, Aug 26, 2008 at 1:11 PM, Michael Hutchinson
<m.j.hutchinson at gmail.com> wrote:
> On Tue, Aug 26, 2008 at 1:11 PM, Andy Selvig <ajselvig at gmail.com> wrote:
>> I've put together a very basic sample that tries to use accelerators.
>> Clicking on the menu item's call the methods appropriately, but the
>> accelerators don't work. I used AccelMap.AddEntry() to map the
>> accelerators to paths, then told the menu items what their accelerator
>> paths were. Is more needed? I still can't figure out how to use
>> AccelGroup, as I don't know how to make an IntPtr closure (notice the
>> ??? in the accelGroup.Connect() calls). The documentation for
>> AccelGroup says to use ItemFactory, but the documentation for
>> ItemFactory has "Obselete" and "Do not use" in it.
>>
>> The sample code is attached. Help on this would be great, it doesn't
>> seem like it should be this complicated.
>
> I'm not familar with Accelerators, but you could look at the code
> generated by Stetic.
>
> Another option is to use Action Groups, which can have accelerators,
> and bind those to your toolbars/menus/whatever.
>
> --
> Michael Hutchinson
> http://mjhutchinson.com
>


More information about the Gtk-sharp-list mailing list