prior to 2.4, gtk+ offered two widgets for the task of selecting one item from a list of options. gtkoptionmenu presents the list of options as a menu while gtkcombo presents them in a windows-style list popup. the only difference between the two is that a gtkcombo allows to manually edit the selected value, while the gtkoptionmenu does not. in gtk+ 2.4, a unified api for list selection was introduced, with gtkcombobox for the non-editable case and gtkcomboboxentry for the editable case. the selection of the display style — menu or list — is no longer done at the api level, but has been made themeable via the style property gtkcombobox::appearance. here is an example of a simple, but typical use of gtkoptionmenu: gtkwidget *option_menu, *menu, *menu_item; option_menu = gtk_option_menu_new (); menu = gtk_menu_new (); menu_item = gtk_menu_item_new_with_label ("first item"); gtk_menu_shell_append (gtk_menu_shell (menu), menu_item); gtk_widget_show (menu_item); menu_item = gtk_menu_item_new_with_label ("second item"); gtk_menu_shell_append (gtk_menu_shell (menu), menu_item); gtk_widget_show (menu_item); menu_item = gtk_menu_item_new_with_label ("third item"); gtk_menu_shell_append (gtk_menu_shell (menu), menu_item); gtk_widget_show (menu_item); gtk_option_menu_set_menu (gtk_option_menu (option_menu), menu); in order to react to the user's selection, connect to the "changed" signal on the option menu and use gtk_option_menu_get_history() to retrieve the index of the selected item. and here is how it would be done with a gtkcombobox: gtkwidget *combo_box; combo_box = gtk_combo_box_new_text (); gtk_combo_box_append_text (gtk_combo_box (combo_box), "first item"); gtk_combo_box_append_text (gtk_combo_box (combo_box), "second item"); gtk_combo_box_append_text (gtk_combo_box (combo_box), "third item"); in order to react to the user's selection, connect to the "changed" signal on the combo box and use gtk_combo_box_get_active() to retrieve the index of the selected item. a slightly more complex example involving images: gtkwidget *option_menu, *menu, *menu_item; option_menu = gtk_option_menu_new (); menu = gtk_menu_new (); menu_item = gtk_image_menu_item_new_with_label ("first item"); gtk_image_menu_item_set_image (gtk_image_new_from_pixbuf (pixbuf1)); gtk_menu_shell_append (gtk_menu_shell (menu), menu_item); gtk_widget_show (menu_item); menu_item = gtk_image_menu_item_new_with_label ("second item"); gtk_image_menu_item_set_image (gtk_image_new_from_pixbuf (pixbuf2)); gtk_menu_shell_append (gtk_menu_shell (menu), menu_item); gtk_widget_show (menu_item); menu_item = gtk_image_menu_item_new_with_label ("third item"); gtk_image_menu_item_set_image (gtk_image_new_from_pixbuf (pixbuf3)); gtk_menu_shell_append (gtk_menu_shell (menu), menu_item); gtk_widget_show (menu_item); gtk_option_menu_set_menu (gtk_option_menu (option_menu), menu);
can be done using a gtkcombobox as follows: gtkliststore *store; gtktreeiter iter; gtkcellrenderer *renderer; gtkwidget *combo_box; store = gtk_list_store_new (2, gdk_type_pixbuf, g_type_string); gtk_list_store_append (store, &iter); gtk_list_store_set (store, &iter, 0, pixbuf1, 1, "first item", -1); gtk_list_store_append (store, &iter); gtk_list_store_set (store, &iter, 0, pixbuf2, 1, "second item", -1); gtk_list_store_append (store, &iter); gtk_list_store_set (store, &iter, 0, pixbuf3, 1, "third item", -1); combo_box = gtk_combo_box_new_with_model (gtk_tree_model (store)); renderer = gtk_cell_renderer_pixbuf_new (); gtk_cell_layout_pack_start (gtk_cell_layout (combo_box), renderer, false); gtk_cell_layout_set_attributes (gtk_cell_layout (combo_box), renderer, "pixbuf", 0, null); renderer = gtk_cell_renderer_text_new (); gtk_cell_layout_pack_start (gtk_cell_layout (combo_box), renderer, true); gtk_cell_layout_set_attributes (gtk_cell_layout (combo_box), renderer, "text", 1, null);
|