[Glade-users] Dynamically adding menu items to menu bar

Jonathan Lane lanej at horizon.com
Thu Feb 19 21:42:35 EST 2009


On Feb 19, 2009, at 9:19 PM, John Coppens wrote:

> On Thu, 19 Feb 2009 20:19:46 -0500
> Jonathan Lane <lanej at horizon.com> wrote:
>
>> GtkWidget *cmds_menu;
>> GtkAction *action;
>> GtkWidget *w;
>>
>> cmds_menu = gtk_menu_new();
>> action = GTK_ACTION(gtk_builder_get_object(builder,
>> "commands_menuitem"));
>> w = gtk_action_create_menu_item(action);
>> gtk_menu_item_set_submenu(GTK_MENU_ITEM(w), cmds_menu);
>>
>> /* Applications from cfg file
>> */
>> for (i = 0; i < g_list_length(app_cfg->procs); i++) {
>>    struct app_proc *proc = g_list_nth_data(app_cfg->procs, i);
>>    assert(proc != NULL);
>>
>>    action = gtk_action_new(proc->name, proc->desc, NULL, NULL);
>>    w = gtk_action_create_menu_item(action);
>>    gtk_menu_shell_insert(GTK_MENU_SHELL(cmds_menu), w, -1);
>>    gtk_widget_show(w);
>> }
>>
>
> This is about the same I tried to do. The cast  
> GTK_MENU_SHELL(cmds_menu)
> gave a runtime problem, because the type of cmds_menu wasn't  
> MENUSHELL.

Actually, it is a GtkMenuShell -- at least it certainly should be --  
because it came from gtk_menu_new().  What you're thinking of I think  
is the "commands_menuitem" from glade being a GtkAction instead of a  
GtkMenuItem, and that's why I first grabbed the GtkAction and then  
used gtk_action_create_menu_item() to get at a GtkMenuItem to which I  
could add a submenu.  Somehow I think this is my problem though -- the  
proxy isn't working.. but I don't know enough about the inner-workings  
of these widgets to know for sure.

> if I recall rightly. If you look it up inside the .xml, you'll see  
> that
> the menu structure is somewhat separate from the rest.

It is, but it assigns the Commands menu with:

<child>
   <object class="GtkAction" id="commands_menuitem">
     <property name="name">commands_menuitem</property>
     <property name="label" translatable="yes">_Commands</property>
   </object>
</child>

...

<menuitem action="commands_menuitem"/>

So I'd think I ought to be able to get at the Action and then convert  
it to a MenuItem.  But I don't know.  Documentation on all of this  
seems non-existant.  Google turns up several questions but no answers :(

>>> I did find an example where the menu is directly built in XML inside
>>> the
>>> source file, then read in.
>>
>> Do you have a link for that?  I'd really appreciate it.
>
> It's a bit longish:
>
> http://www.delorie.com/pcb/cvs/src/hid/gtk/gui-top-window.c

Thanks much for this.

> I have a couple of toolbars which work fine, but I believe I can't  
> define
> new ones. Tried, and gave up. A buttonbar did the same for me. Teh
> advantages of 3.5.7 (which I compiled myself) are enough to take a few
> other problems along.

My problem was that gtk_builder_get_object() couldn't find the  
toolbars I'd defined in glade.  Then I tried doing it programatically  
and it couldn't find the VBox I wanted to pack it into.  I'm sure it's  
something stupid, but I really don't want to do something tonight that  
requires that I re-test everything at the last minute.

-Jonathan


More information about the Glade-users mailing list