list.c 8.42 KB
Newer Older
1

2
#include "config.h"
3 4
#include <gtk/gtk.h>
#include <stdio.h>
5

6
/* This is our data identification string to store
7 8
 * data in list items
 */
9
const gchar *list_item_data_key="list_item_data";
10 11 12


/* prototypes for signal handler that we are going to connect
13
 * to the List widget
14
 */
15 16
static void  sigh_print_selection( GtkWidget *gtklist,
                                   gpointer   func_data);
17

18 19 20
static void  sigh_button_event( GtkWidget      *gtklist,
                                GdkEventButton *event,
                                GtkWidget      *frame );
21 22


23 24
/* Main function to set up the user interface */

25 26
gint main( int    argc,
           gchar *argv[] )
27
{                                  
28 29 30 31 32 33 34 35 36 37 38
    GtkWidget *separator;
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *scrolled_window;
    GtkWidget *frame;
    GtkWidget *gtklist;
    GtkWidget *button;
    GtkWidget *list_item;
    GList *dlist;
    guint i;
    gchar buffer[64];
39 40
    
    
41
    /* Initialize GTK (and subsequently GDK) */
42

43
    gtk_init (&argc, &argv);
44 45
    
    
46
    /* Create a window to put all the widgets in
47 48 49
     * connect gtk_main_quit() to the "destroy" event of
     * the window to handle window manager close-window-events
     */
50 51
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "GtkList Example");
52 53
    g_signal_connect (G_OBJECT (window), "destroy",
		      G_CALLBACK (gtk_main_quit),
54
		      NULL);
55 56
    
    
57
    /* Inside the window we need a box to arrange the widgets
58
     * vertically */
59
    vbox=gtk_vbox_new (FALSE, 5);
60
    gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
61 62
    gtk_container_add (GTK_CONTAINER (window), vbox);
    gtk_widget_show (vbox);
63
    
64
    /* This is the scrolled window to put the List widget inside */
65
    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
66
    gtk_widget_set_size_request (scrolled_window, 250, 150);
67 68
    gtk_container_add (GTK_CONTAINER (vbox), scrolled_window);
    gtk_widget_show (scrolled_window);
69
    
70
    /* Create thekList widget.
71
     * Connect the sigh_print_selection() signal handler
72
     * function to the "selection_changed" signal of the List
73 74
     * to print out the selected items each time the selection
     * has changed */
75 76
    gtklist=gtk_list_new ();
    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),
77
                                           gtklist);
78
    gtk_widget_show (gtklist);
79 80 81
    g_signal_connect (G_OBJECT (gtklist), "selection_changed",
                      G_CALLBACK (sigh_print_selection),
                      NULL);
82
    
83
    /* We create a "Prison" to put a list item in ;) */
84
    frame=gtk_frame_new ("Prison");
85 86
    gtk_widget_set_size_request (frame, 200, 50);
    gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
87 88 89
    gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
    gtk_container_add (GTK_CONTAINER (vbox), frame);
    gtk_widget_show (frame);
90
    
91
    /* Connect the sigh_button_event() signal handler to the List
92
     * which will handle the "arresting" of list items
93
     */
94 95 96
    g_signal_connect (G_OBJECT (gtklist), "button_release_event",
                      G_CALLBACK (sigh_button_event),
                      frame);
97
    
98
    /* Create a separator */
99 100 101
    separator=gtk_hseparator_new ();
    gtk_container_add (GTK_CONTAINER (vbox), separator);
    gtk_widget_show (separator);
102
    
103
    /* Finally create a button and connect its "clicked" signal
104
     * to the destruction of the window */
105 106 107
    button=gtk_button_new_with_label ("Close");
    gtk_container_add (GTK_CONTAINER (vbox), button);
    gtk_widget_show (button);
108 109 110
    g_signal_connect_swapped (G_OBJECT (button), "clicked",
                              G_CALLBACK (gtk_widget_destroy),
                              window);
111 112
    
    
113 114
    /* Now we create 5 list items, each having its own
     * label and add them to the List using gtk_container_add()
115
     * Also we query the text string from the label and
116 117
     * associate it with the list_item_data_key for each list item
     */
118
    for (i = 0; i < 5; i++) {
119 120 121 122
	GtkWidget       *label;
	gchar           *string;
	
	sprintf(buffer, "ListItemContainer with Label #%d", i);
123 124 125 126 127 128 129
	label=gtk_label_new (buffer);
	list_item=gtk_list_item_new ();
	gtk_container_add (GTK_CONTAINER (list_item), label);
	gtk_widget_show (label);
	gtk_container_add (GTK_CONTAINER (gtklist), list_item);
	gtk_widget_show (list_item);
	gtk_label_get (GTK_LABEL (label), &string);
130
	g_object_set_data (G_OBJECT (list_item), list_item_data_key, string);
131
    }
132
    /* Here, we are creating another 5 labels, this time
133
     * we use gtk_list_item_new_with_label() for the creation
134 135
     * we can't query the text string from the label because
     * we don't have the labels pointer and therefore
136
     * we just associate the list_item_data_key of each
137 138
     * list item with the same text string.
     * For adding of the list items we put them all into a doubly
139
     * linked list (GList), and then add them by a single call to
140 141
     * gtk_list_append_items().
     * Because we use g_list_prepend() to put the items into the
142 143 144
     * doubly linked list, their order will be descending (instead
     * of ascending when using g_list_append())
     */
145 146
    dlist = NULL;
    for (; i < 10; i++) {
147
	sprintf(buffer, "List Item with Label %d", i);
148 149 150
	list_item = gtk_list_item_new_with_label (buffer);
	dlist = g_list_prepend (dlist, list_item);
	gtk_widget_show (list_item);
151 152 153
	g_object_set_data (G_OBJECT (list_item),
                           list_item_data_key,
                           "ListItem with integrated Label");
154
    }
155
    gtk_list_append_items (GTK_LIST (gtklist), dlist);
156
    
157
    /* Finally we want to see the window, don't we? ;) */
158
    gtk_widget_show (window);
159
    
160
    /* Fire up the main event loop of gtk */
161
    gtk_main ();
162
    
163
    /* We get here after gtk_main_quit() has been called which
164 165
     * happens if the main window gets destroyed
     */
166
    return 0;
167 168
}

169
/* This is the signal handler that got connected to button
170
 * press/release events of the List
171
 */
172 173 174
void sigh_button_event( GtkWidget      *gtklist,
                        GdkEventButton *event,
                        GtkWidget      *frame )
175
{
176
    /* We only do something if the third (rightmost mouse button
177 178
     * was released
     */
179 180
    if (event->type == GDK_BUTTON_RELEASE &&
	event->button == 3) {
181 182 183
	GList           *dlist, *free_list;
	GtkWidget       *new_prisoner;
	
184
	/* Fetch the currently selected list item which
185 186
	 * will be our next prisoner ;)
	 */
187
	dlist = GTK_LIST (gtklist)->selection;
188
	if (dlist)
189
		new_prisoner = GTK_WIDGET (dlist->data);
190
	else
191
		new_prisoner = NULL;
192
	
193 194 195
	/* Look for already imprisoned list items, we
	 * will put them back into the list.
	 * Remember to free the doubly linked list that
196 197
	 * gtk_container_children() returns
	 */
198 199
	dlist = gtk_container_children (GTK_CONTAINER (frame));
	free_list = dlist;
200 201 202
	while (dlist) {
	    GtkWidget       *list_item;
	    
203
	    list_item = dlist->data;
204
	    
205
	    gtk_widget_reparent (list_item, gtklist);
206
	    
207
	    dlist = dlist->next;
208
	}
209
	g_list_free (free_list);
210
	
211
	/* If we have a new prisoner, remove him from the
212
	 * List and put him into the frame "Prison".
213
	 * We need to unselect the item first.
214 215 216 217
	 */
	if (new_prisoner) {
	    GList   static_dlist;
	    
218 219 220
	    static_dlist.data = new_prisoner;
	    static_dlist.next = NULL;
	    static_dlist.prev = NULL;
221
	    
222 223 224
	    gtk_list_unselect_child (GTK_LIST (gtklist),
				     new_prisoner);
	    gtk_widget_reparent (new_prisoner, frame);
225 226 227 228
	}
    }
}

229
/* This is the signal handler that gets called if List
230 231
 * emits the "selection_changed" signal
 */
232
void sigh_print_selection( GtkWidget *gtklist,
233
                           gpointer   func_data )
234 235 236
{
    GList   *dlist;
    
237
    /* Fetch the doubly linked list of selected items
238
     * of the List, remember to treat this as read-only!
239
     */
240
    dlist = GTK_LIST (gtklist)->selection;
241
    
242
    /* If there are no selected items there is nothing more
243 244 245
     * to do than just telling the user so
     */
    if (!dlist) {
246
	g_print ("Selection cleared\n");
247 248
	return;
    }
249
    /* Ok, we got a selection and so we print it
250
     */
251
    g_print ("The selection is a ");
252
    
253 254 255
    /* Get the list item from the doubly linked list
     * and then query the data associated with list_item_data_key.
     * We then just print it */
256
    while (dlist) {
257
	const gchar *item_data_string;
258
	
259
	item_data_string = g_object_get_data (G_OBJECT (dlist->data),
260
	 				      list_item_data_key);
261
	g_print("%s ", item_data_string);
262 263
 	
	dlist = dlist->next;
264
    }
265
    g_print ("\n");
266
}