2000-10-05 01:04:57 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <stdlib.h>
|
2000-11-21 09:07:45 +00:00
|
|
|
#include <string.h>
|
2000-10-05 01:04:57 +00:00
|
|
|
|
2000-11-21 09:07:45 +00:00
|
|
|
GtkTreeStore *base_model;
|
2000-10-05 01:04:57 +00:00
|
|
|
|
|
|
|
static void
|
2000-10-06 00:06:39 +00:00
|
|
|
selection_changed (GtkTreeSelection *selection,
|
2000-10-26 00:36:47 +00:00
|
|
|
GtkWidget *button)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
2000-11-09 16:52:17 +00:00
|
|
|
if (gtk_tree_selection_get_selected (selection, NULL, NULL))
|
2000-10-06 00:06:39 +00:00
|
|
|
gtk_widget_set_sensitive (button, TRUE);
|
|
|
|
else
|
|
|
|
gtk_widget_set_sensitive (button, FALSE);
|
2000-10-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
2000-10-26 00:36:47 +00:00
|
|
|
static void
|
|
|
|
node_set (GtkTreeIter *iter)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
|
|
|
static gint i = 0;
|
2000-12-21 05:27:06 +00:00
|
|
|
gint n;
|
2000-10-05 01:04:57 +00:00
|
|
|
gchar *str;
|
|
|
|
|
2000-12-21 05:27:06 +00:00
|
|
|
str = g_strdup_printf ("Row (%d)", i++);
|
2000-11-21 09:07:45 +00:00
|
|
|
gtk_tree_store_set (base_model, iter, 0, str, -1);
|
2000-10-05 01:04:57 +00:00
|
|
|
g_free (str);
|
|
|
|
|
2000-12-21 05:27:06 +00:00
|
|
|
n = g_random_int ();
|
|
|
|
if (n < 0)
|
|
|
|
n *= -1;
|
|
|
|
str = g_strdup_printf ("%d", n);
|
|
|
|
|
2000-11-21 09:07:45 +00:00
|
|
|
gtk_tree_store_set (base_model, iter, 1, str, -1);
|
|
|
|
g_free (str);
|
2000-10-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-26 00:36:47 +00:00
|
|
|
iter_remove (GtkWidget *button, GtkTreeView *tree_view)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
2000-10-26 00:36:47 +00:00
|
|
|
GtkTreeIter selected;
|
2000-11-21 09:07:45 +00:00
|
|
|
GtkTreeIter s_selected;
|
|
|
|
GtkTreeModel *model;
|
|
|
|
|
|
|
|
model = gtk_tree_view_get_model (tree_view);
|
|
|
|
|
|
|
|
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (tree_view),
|
2000-11-09 16:52:17 +00:00
|
|
|
NULL,
|
2000-10-26 00:36:47 +00:00
|
|
|
&selected))
|
|
|
|
{
|
2000-11-21 09:07:45 +00:00
|
|
|
if (GTK_IS_TREE_STORE (model))
|
|
|
|
{
|
|
|
|
gtk_tree_store_remove (GTK_TREE_STORE (model), &selected);
|
|
|
|
}
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
2000-10-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-26 00:36:47 +00:00
|
|
|
iter_insert (GtkWidget *button, GtkTreeView *tree_view)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
|
|
|
GtkWidget *entry;
|
2000-10-26 00:36:47 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeIter selected;
|
2000-11-21 09:07:45 +00:00
|
|
|
GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
|
2000-10-05 01:04:57 +00:00
|
|
|
|
|
|
|
entry = gtk_object_get_user_data (GTK_OBJECT (button));
|
2000-10-26 00:36:47 +00:00
|
|
|
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
|
2000-11-09 16:52:17 +00:00
|
|
|
NULL,
|
2000-10-26 00:36:47 +00:00
|
|
|
&selected))
|
|
|
|
{
|
2000-10-27 23:34:58 +00:00
|
|
|
gtk_tree_store_insert (model,
|
|
|
|
&iter,
|
|
|
|
&selected,
|
|
|
|
atoi (gtk_entry_get_text (GTK_ENTRY (entry))));
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-27 23:34:58 +00:00
|
|
|
gtk_tree_store_insert (model,
|
|
|
|
&iter,
|
|
|
|
NULL,
|
|
|
|
atoi (gtk_entry_get_text (GTK_ENTRY (entry))));
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node_set (&iter);
|
2000-10-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-26 00:36:47 +00:00
|
|
|
iter_insert_before (GtkWidget *button, GtkTreeView *tree_view)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
2000-10-26 00:36:47 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeIter selected;
|
2000-11-21 09:07:45 +00:00
|
|
|
GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
|
2000-10-26 00:36:47 +00:00
|
|
|
|
|
|
|
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
|
2000-11-09 16:52:17 +00:00
|
|
|
NULL,
|
2000-10-26 00:36:47 +00:00
|
|
|
&selected))
|
|
|
|
{
|
2000-10-27 23:34:58 +00:00
|
|
|
gtk_tree_store_insert_before (model,
|
|
|
|
&iter,
|
|
|
|
NULL,
|
|
|
|
&selected);
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-10-27 23:34:58 +00:00
|
|
|
gtk_tree_store_insert_before (model,
|
|
|
|
&iter,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node_set (&iter);
|
2000-10-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-26 00:36:47 +00:00
|
|
|
iter_insert_after (GtkWidget *button, GtkTreeView *tree_view)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
2000-10-26 00:36:47 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeIter selected;
|
2000-11-21 09:07:45 +00:00
|
|
|
GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
|
2000-10-26 00:36:47 +00:00
|
|
|
|
|
|
|
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
|
2000-11-09 16:52:17 +00:00
|
|
|
NULL,
|
2000-10-26 00:36:47 +00:00
|
|
|
&selected))
|
|
|
|
{
|
2000-11-22 05:16:37 +00:00
|
|
|
if (GTK_IS_TREE_STORE (model))
|
|
|
|
{
|
|
|
|
gtk_tree_store_insert_after (model,
|
|
|
|
&iter,
|
|
|
|
NULL,
|
|
|
|
&selected);
|
|
|
|
node_set (&iter);
|
|
|
|
}
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-11-22 05:16:37 +00:00
|
|
|
if (GTK_IS_TREE_STORE (model))
|
|
|
|
{
|
|
|
|
gtk_tree_store_insert_after (model,
|
|
|
|
&iter,
|
|
|
|
NULL,
|
2000-12-21 05:27:06 +00:00
|
|
|
NULL);
|
2000-11-22 05:16:37 +00:00
|
|
|
node_set (&iter);
|
|
|
|
}
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
2000-10-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-26 00:36:47 +00:00
|
|
|
iter_prepend (GtkWidget *button, GtkTreeView *tree_view)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
2000-10-26 00:36:47 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeIter selected;
|
2000-11-21 09:07:45 +00:00
|
|
|
GtkTreeModel *model = gtk_tree_view_get_model (tree_view);
|
|
|
|
GtkTreeSelection *selection = gtk_tree_view_get_selection (tree_view);
|
2000-10-26 00:36:47 +00:00
|
|
|
|
2000-11-21 09:07:45 +00:00
|
|
|
if (gtk_tree_selection_get_selected (selection, NULL, &selected))
|
2000-10-26 00:36:47 +00:00
|
|
|
{
|
2000-11-21 09:07:45 +00:00
|
|
|
if (GTK_IS_TREE_STORE (model))
|
|
|
|
{
|
|
|
|
gtk_tree_store_prepend (GTK_TREE_STORE (model),
|
|
|
|
&iter,
|
|
|
|
&selected);
|
|
|
|
node_set (&iter);
|
|
|
|
}
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-11-21 09:07:45 +00:00
|
|
|
if (GTK_IS_TREE_STORE (model))
|
|
|
|
{
|
|
|
|
gtk_tree_store_prepend (GTK_TREE_STORE (model),
|
|
|
|
&iter,
|
|
|
|
NULL);
|
|
|
|
node_set (&iter);
|
|
|
|
}
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
2000-10-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-10-26 00:36:47 +00:00
|
|
|
iter_append (GtkWidget *button, GtkTreeView *tree_view)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
2000-10-26 00:36:47 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeIter selected;
|
2000-11-21 09:07:45 +00:00
|
|
|
GtkTreeStore *model = gtk_tree_view_get_model (tree_view);
|
2000-10-26 00:36:47 +00:00
|
|
|
|
|
|
|
if (gtk_tree_selection_get_selected (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)),
|
2000-11-09 16:52:17 +00:00
|
|
|
NULL,
|
2000-10-26 00:36:47 +00:00
|
|
|
&selected))
|
|
|
|
{
|
2000-11-21 09:07:45 +00:00
|
|
|
if (GTK_IS_TREE_STORE (model))
|
|
|
|
{
|
|
|
|
gtk_tree_store_append (model, &iter, &selected);
|
|
|
|
node_set (&iter);
|
|
|
|
}
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-11-21 09:07:45 +00:00
|
|
|
if (GTK_IS_TREE_STORE (model))
|
|
|
|
{
|
|
|
|
gtk_tree_store_append (model, &iter, NULL);
|
|
|
|
node_set (&iter);
|
|
|
|
}
|
2000-10-26 00:36:47 +00:00
|
|
|
}
|
2000-10-05 01:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-11-21 09:07:45 +00:00
|
|
|
make_window (gboolean use_sort)
|
2000-10-05 01:04:57 +00:00
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *hbox, *entry;
|
|
|
|
GtkWidget *button;
|
|
|
|
GtkWidget *scrolled_window;
|
|
|
|
GtkWidget *tree_view;
|
2000-11-09 16:52:17 +00:00
|
|
|
GtkTreeViewColumn *column;
|
2000-10-05 01:04:57 +00:00
|
|
|
GtkCellRenderer *cell;
|
|
|
|
GtkObject *selection;
|
|
|
|
|
|
|
|
/* Make the Widgets/Objects */
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2000-11-21 09:07:45 +00:00
|
|
|
if (use_sort)
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Sorted list");
|
|
|
|
else
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Unsorted list");
|
|
|
|
|
2000-10-05 01:04:57 +00:00
|
|
|
vbox = gtk_vbox_new (FALSE, 8);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 300, 350);
|
|
|
|
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
|
2000-11-21 09:07:45 +00:00
|
|
|
if (use_sort)
|
|
|
|
{
|
|
|
|
GtkTreeModel *sort_model;
|
|
|
|
|
|
|
|
sort_model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (base_model),
|
2000-11-22 05:16:37 +00:00
|
|
|
NULL, 1);
|
2000-11-21 09:07:45 +00:00
|
|
|
tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (sort_model));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (base_model));
|
|
|
|
}
|
|
|
|
|
2000-10-05 01:04:57 +00:00
|
|
|
selection = GTK_OBJECT (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)));
|
2001-01-04 21:33:24 +00:00
|
|
|
gtk_tree_selection_set_mode (GTK_TREE_SELECTION (selection), GTK_TREE_SELECTION_SINGLE);
|
2000-10-05 01:04:57 +00:00
|
|
|
|
|
|
|
/* Put them together */
|
|
|
|
gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_signal_connect (GTK_OBJECT (window), "destroy", gtk_main_quit, NULL);
|
|
|
|
|
|
|
|
/* buttons */
|
2000-10-27 23:34:58 +00:00
|
|
|
button = gtk_button_new_with_label ("gtk_tree_store_remove");
|
2000-10-05 01:04:57 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
|
|
|
gtk_signal_connect (GTK_OBJECT (selection),
|
2000-10-06 00:06:39 +00:00
|
|
|
"selection_changed",
|
|
|
|
selection_changed,
|
|
|
|
button);
|
2000-10-26 00:36:47 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_remove, tree_view);
|
2000-10-05 01:04:57 +00:00
|
|
|
gtk_widget_set_sensitive (button, FALSE);
|
|
|
|
|
2000-10-27 23:34:58 +00:00
|
|
|
button = gtk_button_new_with_label ("gtk_tree_store_insert");
|
2000-10-05 01:04:57 +00:00
|
|
|
hbox = gtk_hbox_new (FALSE, 8);
|
|
|
|
entry = gtk_entry_new ();
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0);
|
|
|
|
gtk_object_set_user_data (GTK_OBJECT (button), entry);
|
2000-10-26 00:36:47 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert, tree_view);
|
2000-10-05 01:04:57 +00:00
|
|
|
|
|
|
|
|
2000-10-27 23:34:58 +00:00
|
|
|
button = gtk_button_new_with_label ("gtk_tree_store_insert_before");
|
2000-10-05 01:04:57 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
2000-10-26 00:36:47 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert_before, tree_view);
|
2000-10-06 00:06:39 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (selection),
|
|
|
|
"selection_changed",
|
|
|
|
selection_changed,
|
|
|
|
button);
|
2000-10-05 01:04:57 +00:00
|
|
|
gtk_widget_set_sensitive (button, FALSE);
|
|
|
|
|
2000-10-27 23:34:58 +00:00
|
|
|
button = gtk_button_new_with_label ("gtk_tree_store_insert_after");
|
2000-10-05 01:04:57 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
2000-10-26 00:36:47 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_insert_after, tree_view);
|
2000-10-06 00:06:39 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (selection),
|
|
|
|
"selection_changed",
|
|
|
|
selection_changed,
|
|
|
|
button);
|
2000-10-05 01:04:57 +00:00
|
|
|
gtk_widget_set_sensitive (button, FALSE);
|
|
|
|
|
2000-10-27 23:34:58 +00:00
|
|
|
button = gtk_button_new_with_label ("gtk_tree_store_prepend");
|
2000-10-05 01:04:57 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
2000-10-26 00:36:47 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_prepend, tree_view);
|
2000-10-05 01:04:57 +00:00
|
|
|
|
2000-10-27 23:34:58 +00:00
|
|
|
button = gtk_button_new_with_label ("gtk_tree_store_append");
|
2000-10-05 01:04:57 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
|
2000-10-26 00:36:47 +00:00
|
|
|
gtk_signal_connect (GTK_OBJECT (button), "clicked", iter_append, tree_view);
|
2000-10-05 01:04:57 +00:00
|
|
|
|
|
|
|
/* The selected column */
|
|
|
|
cell = gtk_cell_renderer_text_new ();
|
2000-11-21 09:07:45 +00:00
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Node ID", cell, "text", 0, NULL);
|
2000-11-09 16:52:17 +00:00
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
|
2000-10-05 01:04:57 +00:00
|
|
|
|
2000-11-21 09:07:45 +00:00
|
|
|
cell = gtk_cell_renderer_text_new ();
|
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Random Number", cell, "text", 1, NULL);
|
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
|
2000-10-26 00:36:47 +00:00
|
|
|
|
2000-11-21 09:07:45 +00:00
|
|
|
/* A few to start */
|
|
|
|
if (!use_sort)
|
|
|
|
{
|
2000-12-21 05:27:06 +00:00
|
|
|
iter_append (NULL, GTK_TREE_VIEW (tree_view));
|
|
|
|
iter_append (NULL, GTK_TREE_VIEW (tree_view));
|
|
|
|
iter_append (NULL, GTK_TREE_VIEW (tree_view));
|
|
|
|
iter_append (NULL, GTK_TREE_VIEW (tree_view));
|
|
|
|
iter_append (NULL, GTK_TREE_VIEW (tree_view));
|
|
|
|
iter_append (NULL, GTK_TREE_VIEW (tree_view));
|
2000-11-21 09:07:45 +00:00
|
|
|
}
|
2000-10-05 01:04:57 +00:00
|
|
|
/* Show it all */
|
|
|
|
gtk_widget_show_all (window);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
2000-11-21 09:07:45 +00:00
|
|
|
base_model = gtk_tree_store_new_with_types (2, G_TYPE_STRING, G_TYPE_STRING);
|
|
|
|
|
|
|
|
/* FIXME: reverse this */
|
|
|
|
make_window (FALSE);
|
|
|
|
make_window (TRUE);
|
2000-10-05 01:04:57 +00:00
|
|
|
|
|
|
|
gtk_main ();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|