Gtkcombobox and gtkliststore

Source: Internet
Author: User
Gtkcombobox allows you to create a drop-down list for users to select the category. gtkcombobox implements the Model-View Mode and provides a rich category type and table mode, however, this also improves the complexity of programming during preparation. For this reason, gtkcombobox provides gtk_combo_box_new_text () to create a commonly used drop-down list function named gtkcombobox, if you want to set the text targets in the drop-down list, you can use functions such as gtk_combo_box_append_text (), counts (), gtk_combo_box_prepend_text (), and counts. The following program is a simple display. In practice, only the gtkcombobox selected by the text is used as the starting part of the gtkcombobox, the selected text is displayed in the gtklabel below:

  • Gtk_combo_box_demo.c
#include <gtk/gtk.h>
gboolean combo_changed(GtkComboBox *comboBox, GtkLabel *label) {
gchar *active = gtk_combo_box_get_active_text(comboBox);
gtk_label_set_text(label, active);
}int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *comboBox; GtkWidget *label; GtkWidget *vbox; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "GtkComboBox"); gtk_window_set_default_size(GTK_WINDOW(window), 200, 50); comboBox = gtk_combo_box_new_text();
gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "caterpillar");
gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "momor");
gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "hamimi");
gtk_combo_box_append_text(GTK_COMBO_BOX(comboBox), "bush");
gtk_combo_box_set_active(GTK_COMBO_BOX(comboBox), 0);
label = gtk_label_new("caterpillar"); vbox = gtk_vbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), comboBox, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5); gtk_container_add(GTK_CONTAINER(window), vbox); g_signal_connect(GTK_OBJECT(comboBox), "changed",
G_CALLBACK(combo_changed), label); g_signal_connect(GTK_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_widget_show_all(window); gtk_main(); return 0;}

The following figure shows the details of a primary row:

 


In the example above, the most simple usage of gtkcombobox is to select only plain text. If you want to make gtkcombobox show more functions and appearances, you must understand the Model-View Design of gtkcombobox. The external component of the gtkcombobox is the part of the view object, and the part of the selected item is the part of the model object. The model object used by the gtkcombobox is actually the object of the gtktreemodel interface, for example, gtkliststore or gtktreestore implements the storage and access method of the model object. In this example, the use of gtkcombobox and gtkliststore is introduced first. Gtkliststore does not have any hierarchical data. When a drop-down list is required, you can directly list the selected data. You can set texts, tabs, and components in gtkliststore, to create a gtkliststore, you must specify that you want to create a unique bit and the type in the specific bit. For example:

Gtkliststore * store = gtk_list_store_new (2, gdk_type_pixbuf, g_type_string );

This program fragment will create a two-digit gtkliststore, a memory storage part, and use gdk_type_pixbuf to specify that the memory bit will be stored in gdkpixbuf, use g_type_string to specify another token. You can use gdk_pixbuf_new_from_file () to retrieve the vertex and restore it back to gdkpixbuf. The second vertex number is gerror. If you do not need it, you can set it to NULL:

Gdkpixbuf * pixbuf = gdk_pixbuf_new_from_file (files [I], null );

Gtkliststore uses gtktreeiter as the index of the content location. When you use gtk_list_store_append (), gtktreeiter is directed to the location of the next column of gtkliststore. Then, you can use gtk_list_store_set () set the location of the gtkliststore, for example:

Gtktreeiter ITER;
Gtk_list_store_append (store, & ITER );
Gtk_list_store_set (store, & ITER,
0, pixbuf,
1, filename,
-1 );

The first two parts of gtk_list_store_set () are the gtkliststore and gtktreeiter, and the last two parts are the specified index and resource, end with-1. With the gtlliststore model object, you can create a view, that is, gtkcombobox. You can use gtk_combo_box_new_with_model () to create a view:

Gtkwidget * ComboBox = gtk_combo_box_new_with_model (gtk_tree_model (store ));

How do you display your information? You need to configure the corresponding gtkcellrenderer, Which bit is used, which gtkcellrenderer, and some relevance of the corresponding bit, you need to inform gtkcelllayout that the gtkcombobox has the actually implemented gtkcelllayout interface. Therefore, you can use gtk_cell_layout_pack_start () to set the location of the gtkcellrenderer parameter and use the parameter () set the relevance:

Gtkcellrender * render; Renderer = gtk_cell_renderer_pixbuf_new ();
Gtk_cell_layout_pack_start (gtk_cell_layout (ComboBox), Renderer, false );
Gtk_cell_layout_set_attributes (gtk_cell_layout (ComboBox), Renderer,
"Pixbuf", 0, // "pixbuf" sets the image
Null); // end with null
Renderer = gtk_cell_renderer_text_new ();
Gtk_cell_layout_pack_start (gtk_cell_layout (ComboBox), Renderer, false );
Gtk_cell_layout_set_attributes (gtk_cell_layout (ComboBox), Renderer,
"Text", 1, // "text" set text
Null );

These are the basic processes for setting the model and view of the gtkcombobox. If you want to obtain the selection information from the user selection drop-down list, you must first obtain the model, that is, the gtkliststore in ctkcombobox, and obtain the selected gtktreeiter (remember? Gtktreeiter refers to a column of data in gtkliststore. Then, use gtk_tree_model_get () to obtain the desired vertex value. For example:

Gboolean combo_changed (gtkcombobox * ComboBox, gtklabel * label ){
Gtktreemodel * model = gtk_combo_box_get_model (ComboBox );
Gtktreeiter ITER;
Gchar * active;
Gtk_combo_box_get_active_iter (ComboBox, & ITER );
Gtk_tree_model_get (model, & ITER,
1, & active,
-1 );

Gtk_label_set_text (Label, active );
}

According to the above description, modify the example above so that the drop-down list can have a small example:

  • Gtk_combo_box_with_icon_demo.c
#include <gtk/gtk.h>enum {
PIXBUF_COL,
TEXT_COL
};

GtkTreeModel* createModel() {
const gchar *files[] = {"caterpillar.jpg", "momor.jpg",
"hamimi.jpg", "bush.jpg"};
GdkPixbuf *pixbuf;
GtkTreeIter iter;
GtkListStore *store;
gint i;

store = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);

for(i = 0; i < 4; i++) {
pixbuf = gdk_pixbuf_new_from_file(files[i], NULL);
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter,
PIXBUF_COL, pixbuf,
TEXT_COL, files[i],
-1);

}

return GTK_TREE_MODEL(store);
}

gboolean combo_changed(GtkComboBox *comboBox, GtkLabel *label) {
GtkTreeModel *model = gtk_combo_box_get_model(comboBox);
GtkTreeIter iter;
gchar *active;
gtk_combo_box_get_active_iter(comboBox, &iter);
gtk_tree_model_get(model, &iter,
1, &active,
-1);

gtk_label_set_text(label, active);
}int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *comboBox; GtkCellRenderer *renderer; GtkWidget *label; GtkWidget *vbox; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "GtkComboBox"); gtk_window_set_default_size(GTK_WINDOW(window), 200, 50); comboBox = gtk_combo_box_new_with_model(createModel());
gtk_combo_box_set_active(GTK_COMBO_BOX(comboBox), 0);
renderer = gtk_cell_renderer_pixbuf_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(comboBox), renderer, FALSE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(comboBox), renderer,
"pixbuf", PIXBUF_COL,
NULL);
renderer = gtk_cell_renderer_text_new();
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(comboBox), renderer, FALSE);
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(comboBox), renderer,
"text", TEXT_COL,
NULL);
label = gtk_label_new("caterpillar.jpg"); vbox = gtk_vbox_new(TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), comboBox, TRUE, TRUE, 5); gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5); gtk_container_add(GTK_CONTAINER(window), vbox);
g_signal_connect(GTK_OBJECT(comboBox), "changed",
G_CALLBACK(combo_changed), label); g_signal_connect(GTK_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_widget_show_all(window); gtk_main(); return 0;}

The results of a line are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.