summaryrefslogtreecommitdiff
path: root/demos/gtk-demo
diff options
context:
space:
mode:
authorBastien Nocera <hadess@hadess.net>2009-07-21 02:16:56 +0100
committerBastien Nocera <hadess@hadess.net>2009-10-14 14:58:30 +0100
commitd21700f5105c2e4b215aa2b4d4f8c90121748cda (patch)
treeb03eb0b607309e6eb10b92c97bd333d0485f79b3 /demos/gtk-demo
parentdf53e6ad8b8939f1e7bb9a48987f972fec6cfa4c (diff)
downloadgtk+-d21700f5105c2e4b215aa2b4d4f8c90121748cda.tar.gz
Bug 319607 – Add a throbber (activity widget) to GTK+
Add GtkSpinner activity throbber, as well as a cell renderer.
Diffstat (limited to 'demos/gtk-demo')
-rw-r--r--demos/gtk-demo/Makefile.am1
-rw-r--r--demos/gtk-demo/list_store.c144
-rw-r--r--demos/gtk-demo/spinner.c90
3 files changed, 199 insertions, 36 deletions
diff --git a/demos/gtk-demo/Makefile.am b/demos/gtk-demo/Makefile.am
index b2d856856f..45d3ef3643 100644
--- a/demos/gtk-demo/Makefile.am
+++ b/demos/gtk-demo/Makefile.am
@@ -37,6 +37,7 @@ demos = \
rotated_text.c \
search_entry.c \
sizegroup.c \
+ spinner.c \
stock_browser.c \
textview.c \
textscroll.c \
diff --git a/demos/gtk-demo/list_store.c b/demos/gtk-demo/list_store.c
index 990e489921..33a7189559 100644
--- a/demos/gtk-demo/list_store.c
+++ b/demos/gtk-demo/list_store.c
@@ -10,6 +10,8 @@
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
+static GtkTreeModel *model = NULL;
+static guint timeout = 0;
typedef struct
{
@@ -26,6 +28,8 @@ enum
COLUMN_NUMBER,
COLUMN_SEVERITY,
COLUMN_DESCRIPTION,
+ COLUMN_PULSE,
+ COLUMN_ACTIVE,
NUM_COLUMNS
};
@@ -47,6 +51,33 @@ static Bug data[] =
{ FALSE, 1, "Normal", "First bug :=)" },
};
+static gboolean
+spinner_timeout (gpointer data)
+{
+ GtkTreeIter iter;
+ guint pulse;
+
+ if (model == NULL)
+ return FALSE;
+
+ gtk_tree_model_get_iter_first (model, &iter);
+ gtk_tree_model_get (model, &iter,
+ COLUMN_PULSE, &pulse,
+ -1);
+ if (pulse == G_MAXUINT)
+ pulse = 0;
+ else
+ pulse++;
+
+ gtk_list_store_set (GTK_LIST_STORE (model),
+ &iter,
+ COLUMN_PULSE, pulse,
+ COLUMN_ACTIVE, TRUE,
+ -1);
+
+ return TRUE;
+}
+
static GtkTreeModel *
create_model (void)
{
@@ -56,21 +87,25 @@ create_model (void)
/* create list store */
store = gtk_list_store_new (NUM_COLUMNS,
- G_TYPE_BOOLEAN,
- G_TYPE_UINT,
- G_TYPE_STRING,
- G_TYPE_STRING);
+ G_TYPE_BOOLEAN,
+ G_TYPE_UINT,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_UINT,
+ G_TYPE_BOOLEAN);
/* add data to the list store */
for (i = 0; i < G_N_ELEMENTS (data); i++)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
- COLUMN_FIXED, data[i].fixed,
- COLUMN_NUMBER, data[i].number,
- COLUMN_SEVERITY, data[i].severity,
- COLUMN_DESCRIPTION, data[i].description,
- -1);
+ COLUMN_FIXED, data[i].fixed,
+ COLUMN_NUMBER, data[i].number,
+ COLUMN_SEVERITY, data[i].severity,
+ COLUMN_DESCRIPTION, data[i].description,
+ COLUMN_PULSE, 0,
+ COLUMN_ACTIVE, FALSE,
+ -1);
}
return GTK_TREE_MODEL (store);
@@ -78,8 +113,8 @@ create_model (void)
static void
fixed_toggled (GtkCellRendererToggle *cell,
- gchar *path_str,
- gpointer data)
+ gchar *path_str,
+ gpointer data)
{
GtkTreeModel *model = (GtkTreeModel *)data;
GtkTreeIter iter;
@@ -110,48 +145,75 @@ add_columns (GtkTreeView *treeview)
/* column for fixed toggles */
renderer = gtk_cell_renderer_toggle_new ();
g_signal_connect (renderer, "toggled",
- G_CALLBACK (fixed_toggled), model);
+ G_CALLBACK (fixed_toggled), model);
column = gtk_tree_view_column_new_with_attributes ("Fixed?",
- renderer,
- "active", COLUMN_FIXED,
- NULL);
+ renderer,
+ "active", COLUMN_FIXED,
+ NULL);
/* set this column to a fixed sizing (of 50 pixels) */
gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column),
- GTK_TREE_VIEW_COLUMN_FIXED);
+ GTK_TREE_VIEW_COLUMN_FIXED);
gtk_tree_view_column_set_fixed_width (GTK_TREE_VIEW_COLUMN (column), 50);
gtk_tree_view_append_column (treeview, column);
/* column for bug numbers */
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Bug number",
- renderer,
- "text",
- COLUMN_NUMBER,
- NULL);
+ renderer,
+ "text",
+ COLUMN_NUMBER,
+ NULL);
gtk_tree_view_column_set_sort_column_id (column, COLUMN_NUMBER);
gtk_tree_view_append_column (treeview, column);
/* column for severities */
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Severity",
- renderer,
- "text",
- COLUMN_SEVERITY,
- NULL);
+ renderer,
+ "text",
+ COLUMN_SEVERITY,
+ NULL);
gtk_tree_view_column_set_sort_column_id (column, COLUMN_SEVERITY);
gtk_tree_view_append_column (treeview, column);
/* column for description */
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Description",
- renderer,
- "text",
- COLUMN_DESCRIPTION,
- NULL);
+ renderer,
+ "text",
+ COLUMN_DESCRIPTION,
+ NULL);
gtk_tree_view_column_set_sort_column_id (column, COLUMN_DESCRIPTION);
gtk_tree_view_append_column (treeview, column);
+
+ /* column for spinner */
+ renderer = gtk_cell_renderer_spinner_new ();
+ column = gtk_tree_view_column_new_with_attributes ("Spinning",
+ renderer,
+ "pulse",
+ COLUMN_PULSE,
+ "active",
+ COLUMN_ACTIVE,
+ NULL);
+ gtk_tree_view_column_set_sort_column_id (column, COLUMN_PULSE);
+ gtk_tree_view_append_column (treeview, column);
+}
+
+static gboolean
+window_closed (GtkWidget *widget,
+ GdkEvent *event,
+ gpointer user_data)
+{
+ model = NULL;
+ window = NULL;
+ if (timeout != 0)
+ {
+ g_source_remove (timeout);
+ timeout = 0;
+ }
+ return FALSE;
}
GtkWidget *
@@ -162,17 +224,16 @@ do_list_store (GtkWidget *do_widget)
GtkWidget *vbox;
GtkWidget *label;
GtkWidget *sw;
- GtkTreeModel *model;
GtkWidget *treeview;
/* create window, etc */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
- gtk_widget_get_screen (do_widget));
+ gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "GtkListStore demo");
g_signal_connect (window, "destroy",
- G_CALLBACK (gtk_widget_destroyed), &window);
+ G_CALLBACK (gtk_widget_destroyed), &window);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
vbox = gtk_vbox_new (FALSE, 8);
@@ -183,10 +244,10 @@ do_list_store (GtkWidget *do_widget)
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
- GTK_SHADOW_ETCHED_IN);
+ GTK_SHADOW_ETCHED_IN);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
- GTK_POLICY_NEVER,
- GTK_POLICY_AUTOMATIC);
+ GTK_POLICY_NEVER,
+ GTK_POLICY_AUTOMATIC);
gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
/* create tree model */
@@ -196,7 +257,7 @@ do_list_store (GtkWidget *do_widget)
treeview = gtk_tree_view_new_with_model (model);
gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
gtk_tree_view_set_search_column (GTK_TREE_VIEW (treeview),
- COLUMN_DESCRIPTION);
+ COLUMN_DESCRIPTION);
g_object_unref (model);
@@ -207,14 +268,25 @@ do_list_store (GtkWidget *do_widget)
/* finish & show */
gtk_window_set_default_size (GTK_WINDOW (window), 280, 250);
+ g_signal_connect (window, "delete-event",
+ G_CALLBACK (window_closed), NULL);
}
if (!GTK_WIDGET_VISIBLE (window))
- gtk_widget_show_all (window);
+ {
+ gtk_widget_show_all (window);
+ if (timeout == 0)
+ timeout = g_timeout_add (80, spinner_timeout, NULL);
+ }
else
{
gtk_widget_destroy (window);
window = NULL;
+ if (timeout != 0)
+ {
+ g_source_remove (timeout);
+ timeout = 0;
+ }
}
return window;
diff --git a/demos/gtk-demo/spinner.c b/demos/gtk-demo/spinner.c
new file mode 100644
index 0000000000..f18e1a17eb
--- /dev/null
+++ b/demos/gtk-demo/spinner.c
@@ -0,0 +1,90 @@
+/* Spinner
+ *
+ * GtkSpinner allows to show that background activity is on-going.
+ *
+ */
+
+#include <gtk/gtk.h>
+
+static GtkWidget *window = NULL;
+static GtkWidget *spinner_sensitive = NULL;
+static GtkWidget *spinner_unsensitive = NULL;
+
+static void
+on_play_clicked (GtkButton *button, gpointer user_data)
+{
+ gtk_spinner_start (GTK_SPINNER (spinner_sensitive));
+ gtk_spinner_start (GTK_SPINNER (spinner_unsensitive));
+}
+
+static void
+on_stop_clicked (GtkButton *button, gpointer user_data)
+{
+ gtk_spinner_stop (GTK_SPINNER (spinner_sensitive));
+ gtk_spinner_stop (GTK_SPINNER (spinner_unsensitive));
+}
+
+GtkWidget *
+do_spinner (GtkWidget *do_widget)
+{
+ GtkWidget *vbox;
+ GtkWidget *hbox;
+ GtkWidget *button;
+ GtkWidget *spinner;
+
+ if (!window)
+ {
+ window = gtk_dialog_new_with_buttons ("GtkSpinner",
+ GTK_WINDOW (do_widget),
+ 0,
+ GTK_STOCK_CLOSE,
+ GTK_RESPONSE_NONE,
+ NULL);
+ gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
+
+ g_signal_connect (window, "response",
+ G_CALLBACK (gtk_widget_destroy), NULL);
+ g_signal_connect (window, "destroy",
+ G_CALLBACK (gtk_widget_destroyed), &window);
+
+ vbox = gtk_vbox_new (FALSE, 5);
+ gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0);
+ gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
+
+ /* Sensitive */
+ hbox = gtk_hbox_new (FALSE, 5);
+ spinner = gtk_spinner_new ();
+ gtk_container_add (GTK_CONTAINER (hbox), spinner);
+ gtk_container_add (GTK_CONTAINER (hbox), gtk_entry_new ());
+ gtk_container_add (GTK_CONTAINER (vbox), hbox);
+ spinner_sensitive = spinner;
+
+ /* Disabled */
+ hbox = gtk_hbox_new (FALSE, 5);
+ spinner = gtk_spinner_new ();
+ gtk_container_add (GTK_CONTAINER (hbox), spinner);
+ gtk_container_add (GTK_CONTAINER (hbox), gtk_entry_new ());
+ gtk_container_add (GTK_CONTAINER (vbox), hbox);
+ spinner_unsensitive = spinner;
+ gtk_widget_set_sensitive (hbox, FALSE);
+
+ button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY);
+ g_signal_connect (G_OBJECT (button), "clicked",
+ G_CALLBACK (on_play_clicked), spinner);
+ gtk_container_add (GTK_CONTAINER (vbox), button);
+
+ button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP);
+ g_signal_connect (G_OBJECT (button), "clicked",
+ G_CALLBACK (on_stop_clicked), spinner);
+ gtk_container_add (GTK_CONTAINER (vbox), button);
+ }
+
+ if (!GTK_WIDGET_VISIBLE (window))
+ gtk_widget_show_all (window);
+ else
+ gtk_widget_destroy (window);
+
+ return window;
+}
+
+