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