Ticket #4447: combo.c

File combo.c, 1.5 KB (added by erikos, 10 years ago)

Example in C, compile with "gcc pkg-config --cflags gtk+-3.0 -o combo combo.c pkg-config --libs gtk+-3.0"

Line 
1#include <gtk/gtk.h>
2
3
4static gboolean
5on_delete_event (GtkWidget *widget,
6                 GdkEvent  *event,
7                 gpointer   data)
8{
9    gtk_main_quit ();
10
11    return TRUE;
12}
13
14int
15main (int   argc,
16      char *argv[])
17{
18    GtkWidget *window;
19    GtkWidget *combo;
20    GtkListStore *store;
21    GtkTreePath *path;
22    GtkTreeIter iter;
23    GtkCellRenderer *cell;
24    gint i;
25
26    gtk_init (&argc, &argv);
27
28    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
29    gtk_window_set_title (GTK_WINDOW (window), "Combo");
30
31    g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
32
33    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
34
35    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
36
37    store = gtk_list_store_new (1, G_TYPE_STRING);
38
39    for (i = 0; i < 30; i++)
40    {
41        // Add a new row to the model
42        gtk_list_store_append (store, &iter);
43        gtk_list_store_set (store, &iter,
44                            0, "Hello",
45                            -1);
46    }
47
48    combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
49
50    gtk_container_add (GTK_CONTAINER (window), combo);
51
52    /* Remove our reference from store to avoid memory leak. */
53    g_object_unref( G_OBJECT( store ) );
54
55    /* Create cell renderer. */
56    cell = gtk_cell_renderer_text_new();
57
58    /* Pack it into the combo box. */
59    gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( combo ), cell, TRUE );
60
61    /* Connect renderer to data source. */
62    gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( combo ), cell, "text", 0, NULL );
63
64    gtk_widget_show_all( window );
65
66    gtk_main ();
67    return 0;
68}