Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to build a simple indicator in C. This is my code:

void make_indicator(){
    //Making the indicator
    appindicator = app_indicator_new("My Indicator", "/home/alex/Qt/normal_tray_icon/a.png", APP_INDICATOR_CATEGORY_APPLICATION_STATUS);

    //the construction of the main menu
    GtkWidget* indicatormenu = gtk_menu_new();
    //adding a submenu
    GtkWidget* submenu1 = gtk_menu_item_new_with_label("SubMenu1");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(submenu1), indicatormenu);

    /* HERE */
    //here comes the error! Adding an option to the submenu    
    GtkWidget* submenu1_option;
    submenu1_option = gtk_menu_item_new_with_label("Submenu option!");
    gtk_menu_shell_append(GTK_MENU_SHELL(submenu1), submenu1_option);

    //adding an option to the main menu and connecting it to a slot
    GtkWidget* showapp_option;

    showapp_option = gtk_menu_item_new_with_label("Show App!");
    g_signal_connect(showapp_option, "activate", G_CALLBACK(show_app), this);
    gtk_menu_shell_append(GTK_MENU_SHELL(indicatormenu), showapp_option);

    //showing the indicator
    gtk_widget_show_all(indicatormenu);
    app_indicator_set_status(appindicator, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_attention_icon(appindicator, "dialog-warning");

    app_indicator_set_menu(appindicator, GTK_MENU (indicatormenu));
}

I fail to add an action to a submenu. I add the submenu as a simple menu item, thus I cannot append to it actions, or can I?

I don't get error messages on compilation, but on run time I get:

(normal_tray_icon:9203): GLib-GObject-WARNING **: invalid cast from `GtkMenuItem' to `GtkMenuShell'

(normal_tray_icon:9203): Gtk-CRITICAL **: IA__gtk_menu_shell_insert: assertion `GTK_IS_MENU_SHELL (menu_shell)' failed

which I can understand but I don't know how to solve.

Thanks for any answers.

share|improve this question

1 Answer 1

up vote 2 down vote accepted
+250

You created only item for submenu, not submenu. This can be done in this way:

//adding a submenu:
//creating submenu
GtkWidget *submenu1 = gtk_menu_new();
GtkWidget *submenu1_item = gtk_menu_item_new_with_label("SubMenu1");
gtk_menu_item_set_submenu(GTK_MENU_ITEM(submenu1_item), submenu1);

//adding submenu to main menu
gtk_menu_shell_append(GTK_MENU_SHELL(indicatormenu), submenu1_item);

//adding an option to the submenu    
GtkWidget* submenu1_option;
submenu1_option = gtk_menu_item_new_with_label("Submenu option!");
gtk_menu_shell_append(GTK_MENU_SHELL(submenu1), submenu1_option);
share|improve this answer
    
Thanks for the answer :) –  hakermania Jan 1 '13 at 13:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.