1998-03-31 23:43:49 +00:00
|
|
|
|
2002-02-16 23:52:30 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <stdio.h>
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* This is our data identification string to store
|
1998-03-31 23:43:49 +00:00
|
|
|
* data in list items
|
|
|
|
*/
|
1998-12-13 22:25:07 +00:00
|
|
|
const gchar *list_item_data_key="list_item_data";
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* prototypes for signal handler that we are going to connect
|
1999-11-13 23:06:46 +00:00
|
|
|
* to the List widget
|
1998-03-31 23:43:49 +00:00
|
|
|
*/
|
1998-12-13 22:25:07 +00:00
|
|
|
static void sigh_print_selection( GtkWidget *gtklist,
|
|
|
|
gpointer func_data);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
static void sigh_button_event( GtkWidget *gtklist,
|
|
|
|
GdkEventButton *event,
|
|
|
|
GtkWidget *frame );
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Main function to set up the user interface */
|
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
gint main( int argc,
|
|
|
|
gchar *argv[] )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
1998-12-13 22:25:07 +00:00
|
|
|
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];
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* Initialize GTK (and subsequently GDK) */
|
1998-03-31 23:43:49 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_init (&argc, &argv);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Create a window to put all the widgets in
|
1998-03-31 23:43:49 +00:00
|
|
|
* connect gtk_main_quit() to the "destroy" event of
|
|
|
|
* the window to handle window manager close-window-events
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "GtkList Example");
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (window), "destroy",
|
|
|
|
G_CALLBACK (gtk_main_quit),
|
2002-02-19 01:25:26 +00:00
|
|
|
NULL);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Inside the window we need a box to arrange the widgets
|
1998-03-31 23:43:49 +00:00
|
|
|
* vertically */
|
2002-02-19 01:25:26 +00:00
|
|
|
vbox=gtk_vbox_new (FALSE, 5);
|
2002-02-16 23:52:30 +00:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
gtk_widget_show (vbox);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* This is the scrolled window to put the List widget inside */
|
2002-02-19 01:25:26 +00:00
|
|
|
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
|
2002-02-16 23:52:30 +00:00
|
|
|
gtk_widget_set_size_request (scrolled_window, 250, 150);
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), scrolled_window);
|
|
|
|
gtk_widget_show (scrolled_window);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* Create thekList widget.
|
1998-12-13 22:25:07 +00:00
|
|
|
* Connect the sigh_print_selection() signal handler
|
1999-11-13 23:06:46 +00:00
|
|
|
* function to the "selection_changed" signal of the List
|
1998-03-31 23:43:49 +00:00
|
|
|
* to print out the selected items each time the selection
|
|
|
|
* has changed */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtklist=gtk_list_new ();
|
|
|
|
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window),
|
1998-12-13 22:25:07 +00:00
|
|
|
gtklist);
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_widget_show (gtklist);
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (gtklist), "selection_changed",
|
|
|
|
G_CALLBACK (sigh_print_selection),
|
|
|
|
NULL);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* We create a "Prison" to put a list item in ;) */
|
2002-02-19 01:25:26 +00:00
|
|
|
frame=gtk_frame_new ("Prison");
|
2002-02-16 23:52:30 +00:00
|
|
|
gtk_widget_set_size_request (frame, 200, 50);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (frame), 5);
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), frame);
|
|
|
|
gtk_widget_show (frame);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* Connect the sigh_button_event() signal handler to the List
|
1998-12-10 17:31:04 +00:00
|
|
|
* which will handle the "arresting" of list items
|
1998-03-31 23:43:49 +00:00
|
|
|
*/
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (gtklist), "button_release_event",
|
|
|
|
G_CALLBACK (sigh_button_event),
|
|
|
|
frame);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Create a separator */
|
2002-02-19 01:25:26 +00:00
|
|
|
separator=gtk_hseparator_new ();
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), separator);
|
|
|
|
gtk_widget_show (separator);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* Finally create a button and connect its "clicked" signal
|
1998-12-13 22:25:07 +00:00
|
|
|
* to the destruction of the window */
|
2002-02-19 01:25:26 +00:00
|
|
|
button=gtk_button_new_with_label ("Close");
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbox), button);
|
|
|
|
gtk_widget_show (button);
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect_swapped (G_OBJECT (button), "clicked",
|
|
|
|
G_CALLBACK (gtk_widget_destroy),
|
|
|
|
window);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* Now we create 5 list items, each having its own
|
|
|
|
* label and add them to the List using gtk_container_add()
|
1998-12-13 22:25:07 +00:00
|
|
|
* Also we query the text string from the label and
|
1998-03-31 23:43:49 +00:00
|
|
|
* associate it with the list_item_data_key for each list item
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
for (i = 0; i < 5; i++) {
|
1998-03-31 23:43:49 +00:00
|
|
|
GtkWidget *label;
|
|
|
|
gchar *string;
|
|
|
|
|
|
|
|
sprintf(buffer, "ListItemContainer with Label #%d", i);
|
2002-02-19 01:25:26 +00:00
|
|
|
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);
|
2002-02-19 19:47:16 +00:00
|
|
|
g_object_set_data (G_OBJECT (list_item), list_item_data_key, string);
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Here, we are creating another 5 labels, this time
|
1998-03-31 23:43:49 +00:00
|
|
|
* we use gtk_list_item_new_with_label() for the creation
|
1998-12-13 22:25:07 +00:00
|
|
|
* we can't query the text string from the label because
|
|
|
|
* we don't have the labels pointer and therefore
|
1998-03-31 23:43:49 +00:00
|
|
|
* we just associate the list_item_data_key of each
|
1998-12-13 22:25:07 +00:00
|
|
|
* list item with the same text string.
|
|
|
|
* For adding of the list items we put them all into a doubly
|
1998-03-31 23:43:49 +00:00
|
|
|
* linked list (GList), and then add them by a single call to
|
1998-12-13 22:25:07 +00:00
|
|
|
* gtk_list_append_items().
|
|
|
|
* Because we use g_list_prepend() to put the items into the
|
1998-03-31 23:43:49 +00:00
|
|
|
* doubly linked list, their order will be descending (instead
|
|
|
|
* of ascending when using g_list_append())
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
dlist = NULL;
|
|
|
|
for (; i < 10; i++) {
|
1998-03-31 23:43:49 +00:00
|
|
|
sprintf(buffer, "List Item with Label %d", i);
|
2002-02-19 01:25:26 +00:00
|
|
|
list_item = gtk_list_item_new_with_label (buffer);
|
|
|
|
dlist = g_list_prepend (dlist, list_item);
|
|
|
|
gtk_widget_show (list_item);
|
2002-02-19 19:47:16 +00:00
|
|
|
g_object_set_data (G_OBJECT (list_item),
|
|
|
|
list_item_data_key,
|
|
|
|
"ListItem with integrated Label");
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_list_append_items (GTK_LIST (gtklist), dlist);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Finally we want to see the window, don't we? ;) */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_widget_show (window);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Fire up the main event loop of gtk */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_main ();
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* We get here after gtk_main_quit() has been called which
|
1998-03-31 23:43:49 +00:00
|
|
|
* happens if the main window gets destroyed
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
return 0;
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* This is the signal handler that got connected to button
|
1999-11-13 23:06:46 +00:00
|
|
|
* press/release events of the List
|
1998-03-31 23:43:49 +00:00
|
|
|
*/
|
1998-12-13 22:25:07 +00:00
|
|
|
void sigh_button_event( GtkWidget *gtklist,
|
|
|
|
GdkEventButton *event,
|
|
|
|
GtkWidget *frame )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
1998-12-13 22:25:07 +00:00
|
|
|
/* We only do something if the third (rightmost mouse button
|
1998-03-31 23:43:49 +00:00
|
|
|
* was released
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
if (event->type == GDK_BUTTON_RELEASE &&
|
|
|
|
event->button == 3) {
|
1998-03-31 23:43:49 +00:00
|
|
|
GList *dlist, *free_list;
|
|
|
|
GtkWidget *new_prisoner;
|
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Fetch the currently selected list item which
|
1998-03-31 23:43:49 +00:00
|
|
|
* will be our next prisoner ;)
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
dlist = GTK_LIST (gtklist)->selection;
|
1998-03-31 23:43:49 +00:00
|
|
|
if (dlist)
|
2002-02-19 01:25:26 +00:00
|
|
|
new_prisoner = GTK_WIDGET (dlist->data);
|
1998-03-31 23:43:49 +00:00
|
|
|
else
|
2002-02-19 01:25:26 +00:00
|
|
|
new_prisoner = NULL;
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Look for already imprisoned list items, we
|
|
|
|
* will put them back into the list.
|
|
|
|
* Remember to free the doubly linked list that
|
1998-03-31 23:43:49 +00:00
|
|
|
* gtk_container_children() returns
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
dlist = gtk_container_children (GTK_CONTAINER (frame));
|
|
|
|
free_list = dlist;
|
1998-03-31 23:43:49 +00:00
|
|
|
while (dlist) {
|
|
|
|
GtkWidget *list_item;
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
list_item = dlist->data;
|
1998-03-31 23:43:49 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_widget_reparent (list_item, gtklist);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
dlist = dlist->next;
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
2002-02-19 01:25:26 +00:00
|
|
|
g_list_free (free_list);
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* If we have a new prisoner, remove him from the
|
1999-11-13 23:06:46 +00:00
|
|
|
* List and put him into the frame "Prison".
|
1998-12-13 22:25:07 +00:00
|
|
|
* We need to unselect the item first.
|
1998-03-31 23:43:49 +00:00
|
|
|
*/
|
|
|
|
if (new_prisoner) {
|
|
|
|
GList static_dlist;
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
static_dlist.data = new_prisoner;
|
|
|
|
static_dlist.next = NULL;
|
|
|
|
static_dlist.prev = NULL;
|
1998-03-31 23:43:49 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_list_unselect_child (GTK_LIST (gtklist),
|
|
|
|
new_prisoner);
|
|
|
|
gtk_widget_reparent (new_prisoner, frame);
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-13 23:06:46 +00:00
|
|
|
/* This is the signal handler that gets called if List
|
1998-03-31 23:43:49 +00:00
|
|
|
* emits the "selection_changed" signal
|
|
|
|
*/
|
1998-12-13 22:25:07 +00:00
|
|
|
void sigh_print_selection( GtkWidget *gtklist,
|
1999-11-13 23:06:46 +00:00
|
|
|
gpointer func_data )
|
1998-03-31 23:43:49 +00:00
|
|
|
{
|
|
|
|
GList *dlist;
|
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Fetch the doubly linked list of selected items
|
1999-11-13 23:06:46 +00:00
|
|
|
* of the List, remember to treat this as read-only!
|
1998-03-31 23:43:49 +00:00
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
dlist = GTK_LIST (gtklist)->selection;
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* If there are no selected items there is nothing more
|
1998-03-31 23:43:49 +00:00
|
|
|
* to do than just telling the user so
|
|
|
|
*/
|
|
|
|
if (!dlist) {
|
2002-02-19 01:25:26 +00:00
|
|
|
g_print ("Selection cleared\n");
|
1998-03-31 23:43:49 +00:00
|
|
|
return;
|
|
|
|
}
|
1998-12-13 22:25:07 +00:00
|
|
|
/* Ok, we got a selection and so we print it
|
1998-03-31 23:43:49 +00:00
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
g_print ("The selection is a ");
|
1998-03-31 23:43:49 +00:00
|
|
|
|
1998-12-13 22:25:07 +00:00
|
|
|
/* 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 */
|
1998-03-31 23:43:49 +00:00
|
|
|
while (dlist) {
|
2002-02-19 19:47:16 +00:00
|
|
|
const gchar *item_data_string;
|
1998-03-31 23:43:49 +00:00
|
|
|
|
2002-02-19 19:47:16 +00:00
|
|
|
item_data_string = g_object_get_data (G_OBJECT (dlist->data),
|
2002-02-19 01:25:26 +00:00
|
|
|
list_item_data_key);
|
1998-03-31 23:43:49 +00:00
|
|
|
g_print("%s ", item_data_string);
|
2002-02-19 01:25:26 +00:00
|
|
|
|
|
|
|
dlist = dlist->next;
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|
2002-02-19 01:25:26 +00:00
|
|
|
g_print ("\n");
|
1998-03-31 23:43:49 +00:00
|
|
|
}
|