GTK+ FAQ
<<< Previous | Development with GTK+: widget specific questions | Next >>>

# How do I add some underlined accelerators to menu items?

Damon Chaplin, the technical force behind the Glade project, provided the following code sample (this code is an output from Glade). It creates a small File menu item with only one child (New). The F in File and the N in New are underlined, and the relevant accelerators are created.

      menubar1 = gtk_menu_bar_new ();
      gtk_object_set_data (GTK_OBJECT (window1), "menubar1", menubar1);
      gtk_widget_show (menubar1);
      gtk_box_pack_start (GTK_BOX (vbox1), menubar1, FALSE, FALSE, 0);

      file1 = gtk_menu_item_new_with_label ("");
      tmp_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (file1)->child),
                                       _("_File"));
      gtk_widget_add_accelerator (file1, "activate_item", accel_group,
                                  tmp_key, GDK_MOD1_MASK, 0);
      gtk_object_set_data (GTK_OBJECT (window1), "file1", file1);
      gtk_widget_show (file1);
      gtk_container_add (GTK_CONTAINER (menubar1), file1);

      file1_menu = gtk_menu_new ();
      file1_menu_accels = gtk_menu_ensure_uline_accel_group (GTK_MENU (file1_menu));
      gtk_object_set_data (GTK_OBJECT (window1), "file1_menu", file1_menu);
      gtk_menu_item_set_submenu (GTK_MENU_ITEM (file1), file1_menu);

      new1 = gtk_menu_item_new_with_label ("");
      tmp_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (new1)->child),
                                       _("_New"));
      gtk_widget_add_accelerator (new1, "activate_item", file1_menu_accels,
                                  tmp_key, 0, 0);
      gtk_object_set_data (GTK_OBJECT (window1), "new1", new1);
      gtk_widget_show (new1);
      gtk_container_add (GTK_CONTAINER (file1_menu), new1);

<<< Previous | Home | Next >>>
How can I right justify a menu, such as Help? | Up | How can I retrieve the text from a GtkMenuItem?