GTK + multi-level menu implementation
Study Notes
20:20:54
Read106
Comment0
Font Size:Large
Medium
Small
Subscription
GTK + allows you to create menus by hand. The process and method for creating a multi-level menu are the same as those for creating a common menu method. The key to implementing a hierarchical menu is to recreate a menu, then add it to the menu item of the Sub-menu. The followingCodeIs the code obtained from a netizen:
# Include <GTK/GTK. h>
Gtkwidget * form1;
Gtkwidget * box1;
Gtkwidget * menubar;
Gtkwidget * menuitem1;
Gtkwidget * menuitem2;
Gtkwidget * menuitem3;
Gtkwidget * menu1;
Gtkwidget * menu2;
Gtkwidget * menuitem1_1;
Gtkwidget * menuitem1_2;
Gtkwidget * menuitem1_3;
Gtkwidget * menuitem2_1;
Gtkwidget * menuitem2_2;
Gtkwidget * menu1_1;
Gtkwidget * menuitem1_1_1;
Gtkwidget * menuitem1_1_2;
Void Pt (gtkwidget * w, gpointer P );
Int main (INT argc, char * argv [])
{Gtk_init (& argc, & argv );
Form1 = gtk_window_new (gtk_window_toplevel );
Gtk_signal_connect (gtk_object (form1), "Destroy", gtk_signal_func (gtk_main_quit), null );
Box1 = gtk_vbox_new (false, 0 );
// Create a menu bar and place the first menu item
Menubar = gtk_menu_bar_new (); // create a menu bar
Menuitem1 = gtk_menu_item_new_with_label ("menu 1"); // create a menu item
Gtk_menu_bar_append (gtk_menu_bar (menubar), menuitem1); // Add the menu item to the menu bar
Menuitem2 = gtk_menu_item_new_with_label ("menu 2 ");
Gtk_menu_bar_append (gtk_menu_bar (menubar), menuitem2 );
Menuitem3 = gtk_menu_item_new_with_label ("menu 3 ");
Gtk_menu_bar_append (gtk_menu_bar (menubar), menuitem3 );
// Create a sub-menu of menuitem1
Menu1 = gtk_menu_new (); // create a menu
Menuitem1_1 = gtk_menu_item_new_with_label ("Level 1 menu 1_1 ");
Gtk_menu_shell_append (gtk_menu_shell (menu1), menuitem1_1); // Add the menu item to the menu
Menuitem1_2 = gtk_menu_item_new_with_label ("Level 1 menu 1_2 ");
Gtk_menu_shell_append (gtk_menu_shell (menu1), menuitem1_2 );
Menuitem1_3 = gtk_menu_item_new_with_label ("Level 1 menu 1_3 ");
Gtk_menu_shell_append (gtk_menu_shell (menu1), menuitem1_3 );
Gtk_menu_item_set_submenu (gtk_menu_item (menuitem1), menu1); // Finally, paste menu menu1 to menu item menuitem1
// As a sub-menu of menuitem2
Menu2 = gtk_menu_new ();
Menuitem2_1 = gtk_menu_item_new_with_label ("Level 1 menu 2_1 ");
Gtk_menu_shell_append (gtk_menu_shell (menu2), menuitem2_1 );
Menuitem2_2 = gtk_menu_item_new_with_label ("level-1 menu 2_2 ");
Gtk_menu_shell_append (gtk_menu_shell (menu2), menuitem2_2 );
Gtk_menu_item_set_submenu (gtk_menu_item (menuitem2), menu2 );
// Create a level-2 sub-menu. The key to the implementation of multi-level menus is the creation statement menu1_1 = gtk_menu_new (), which is the first point.
Menu1_1 = gtk_menu_new ();
Menuitem1_1_1 = gtk_menu_item_new_with_label ("level-2 menu 1_1_1 ");
Gtk_menu_shell_append (gtk_menu_shell (menu1_1), menuitem1_1_1 );
Menuitem1_1_2 = gtk_menu_item_new_with_label ("Level 2 menu 1_1_2 ");
Gtk_menu_shell_append (gtk_menu_shell (menu1_1), menuitem1_1_2 );
Gtk_menu_item_set_submenu (gtk_menu_item (menuitem1_1), menu1_1); // Add the sub-menu to the menu item of the Sub-menu
// Connection signals and functions
Gtk_signal_connect (gtk_object (menuitem1), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem1 "));
Gtk_signal_connect (gtk_object (menuitem2), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem2 "));
Gtk_signal_connect (gtk_object (menuitem3), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem3 "));
Gtk_signal_connect (gtk_object (menuitem1_1), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem1_1 "));
Gtk_signal_connect (gtk_object (menuitem1_2), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem1_2 "));
Gtk_signal_connect (gtk_object (menuitem1_3), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem1_3 "));
Gtk_signal_connect (gtk_object (menuitem2_1), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem2_1 "));
Gtk_signal_connect (gtk_object (menuitem2_2), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem2_2 "));
Gtk_signal_connect (gtk_object (menuitem1_1_1), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem1_1_1 "));
Gtk_signal_connect (gtk_object (menuitem1_1_2), "Activate", gtk_signal_func (PT), (gpointer) ("menuitem1_1_2 "));
Gtk_box_pack_start (gtk_box (box1), menubar, false, false, 0 );
Gtk_container_add (gtk_container (form1), box1 );
Gtk_window_set_default_size (gtk_window (form1), 400,200 );
Gtk_widget_show_all (form1 );
Gtk_main ();
Return (0 );
}
Void Pt (gtkwidget * w, gpointer P)
{G_print ("% s/n", (gchar *) P );
}