2001-10-02 18:54:05 +00:00
|
|
|
/* Tree View/List Store
|
|
|
|
*
|
2002-01-19 20:15:56 +00:00
|
|
|
* The GtkListStore is used to store data in list form, to be used
|
2001-10-02 18:54:05 +00:00
|
|
|
* later on by a GtkTreeView to display it. This demo builds a
|
2013-06-21 17:46:54 +00:00
|
|
|
* simple GtkListStore and displays it.
|
2001-10-02 18:54:05 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static GtkWidget *window = NULL;
|
2009-07-21 01:16:56 +00:00
|
|
|
static GtkTreeModel *model = NULL;
|
|
|
|
static guint timeout = 0;
|
2001-10-02 18:54:05 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const gboolean fixed;
|
|
|
|
const guint number;
|
2020-03-07 14:01:21 +00:00
|
|
|
const char *severity;
|
|
|
|
const char *description;
|
|
|
|
} Bug;
|
2001-10-02 18:54:05 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
COLUMN_FIXED,
|
|
|
|
COLUMN_NUMBER,
|
|
|
|
COLUMN_SEVERITY,
|
|
|
|
COLUMN_DESCRIPTION,
|
2009-07-21 01:16:56 +00:00
|
|
|
COLUMN_PULSE,
|
2010-04-26 10:55:34 +00:00
|
|
|
COLUMN_ICON,
|
2009-07-21 01:16:56 +00:00
|
|
|
COLUMN_ACTIVE,
|
2010-04-26 10:55:34 +00:00
|
|
|
COLUMN_SENSITIVE,
|
2001-10-02 18:54:05 +00:00
|
|
|
NUM_COLUMNS
|
|
|
|
};
|
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
static Bug bugs[] =
|
2001-10-02 18:54:05 +00:00
|
|
|
{
|
|
|
|
{ FALSE, 60482, "Normal", "scrollable notebooks and hidden tabs" },
|
2018-03-20 10:46:11 +00:00
|
|
|
{ FALSE, 60620, "Critical", "gdk_surface_clear_area (gdksurface-win32.c) is not thread-safe" },
|
2001-10-02 18:54:05 +00:00
|
|
|
{ FALSE, 50214, "Major", "Xft support does not clean up correctly" },
|
|
|
|
{ TRUE, 52877, "Major", "GtkFileSelection needs a refresh method. " },
|
|
|
|
{ FALSE, 56070, "Normal", "Can't click button after setting in sensitive" },
|
|
|
|
{ TRUE, 56355, "Normal", "GtkLabel - Not all changes propagate correctly" },
|
|
|
|
{ FALSE, 50055, "Normal", "Rework width/height computations for TreeView" },
|
|
|
|
{ FALSE, 58278, "Normal", "gtk_dialog_set_response_sensitive () doesn't work" },
|
|
|
|
{ FALSE, 55767, "Normal", "Getters for all setters" },
|
|
|
|
{ FALSE, 56925, "Normal", "Gtkcalender size" },
|
|
|
|
{ FALSE, 56221, "Normal", "Selectable label needs right-click copy menu" },
|
|
|
|
{ TRUE, 50939, "Normal", "Add shift clicking to GtkTextView" },
|
2020-08-21 12:41:13 +00:00
|
|
|
{ FALSE, 6112, "Enhancement","netscape-like collapsible toolbars" },
|
2001-10-02 18:54:05 +00:00
|
|
|
{ FALSE, 1, "Normal", "First bug :=)" },
|
|
|
|
};
|
|
|
|
|
2009-07-21 01:16:56 +00:00
|
|
|
static gboolean
|
|
|
|
spinner_timeout (gpointer data)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
guint pulse;
|
|
|
|
|
|
|
|
if (model == NULL)
|
2012-01-31 00:12:27 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2009-07-21 01:16:56 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-01-31 00:12:27 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2009-07-21 01:16:56 +00:00
|
|
|
}
|
|
|
|
|
2001-10-02 18:54:05 +00:00
|
|
|
static GtkTreeModel *
|
|
|
|
create_model (void)
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int i = 0;
|
2001-10-02 18:54:05 +00:00
|
|
|
GtkListStore *store;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
/* create list store */
|
|
|
|
store = gtk_list_store_new (NUM_COLUMNS,
|
2009-07-21 01:16:56 +00:00
|
|
|
G_TYPE_BOOLEAN,
|
|
|
|
G_TYPE_UINT,
|
|
|
|
G_TYPE_STRING,
|
|
|
|
G_TYPE_STRING,
|
|
|
|
G_TYPE_UINT,
|
2010-04-26 10:55:34 +00:00
|
|
|
G_TYPE_STRING,
|
|
|
|
G_TYPE_BOOLEAN,
|
2009-07-21 01:16:56 +00:00
|
|
|
G_TYPE_BOOLEAN);
|
2001-10-02 18:54:05 +00:00
|
|
|
|
|
|
|
/* add data to the list store */
|
2020-03-07 14:01:21 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS (bugs); i++)
|
2001-10-02 18:54:05 +00:00
|
|
|
{
|
2020-03-07 14:01:21 +00:00
|
|
|
const char *icon_name;
|
2010-04-26 10:55:34 +00:00
|
|
|
gboolean sensitive;
|
|
|
|
|
|
|
|
if (i == 1 || i == 3)
|
2011-05-31 20:13:50 +00:00
|
|
|
icon_name = "battery-caution-charging-symbolic";
|
2010-04-26 10:55:34 +00:00
|
|
|
else
|
|
|
|
icon_name = NULL;
|
|
|
|
if (i == 3)
|
|
|
|
sensitive = FALSE;
|
|
|
|
else
|
|
|
|
sensitive = TRUE;
|
2001-10-02 18:54:05 +00:00
|
|
|
gtk_list_store_append (store, &iter);
|
|
|
|
gtk_list_store_set (store, &iter,
|
2020-03-07 14:01:21 +00:00
|
|
|
COLUMN_FIXED, bugs[i].fixed,
|
|
|
|
COLUMN_NUMBER, bugs[i].number,
|
|
|
|
COLUMN_SEVERITY, bugs[i].severity,
|
|
|
|
COLUMN_DESCRIPTION, bugs[i].description,
|
2009-07-21 01:16:56 +00:00
|
|
|
COLUMN_PULSE, 0,
|
2010-04-26 10:55:34 +00:00
|
|
|
COLUMN_ICON, icon_name,
|
2009-07-21 01:16:56 +00:00
|
|
|
COLUMN_ACTIVE, FALSE,
|
2010-04-26 10:55:34 +00:00
|
|
|
COLUMN_SENSITIVE, sensitive,
|
2009-07-21 01:16:56 +00:00
|
|
|
-1);
|
2001-10-02 18:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_TREE_MODEL (store);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fixed_toggled (GtkCellRendererToggle *cell,
|
2020-03-07 14:01:21 +00:00
|
|
|
char *path_str,
|
2009-07-21 01:16:56 +00:00
|
|
|
gpointer data)
|
2001-10-02 18:54:05 +00:00
|
|
|
{
|
2020-03-07 14:01:21 +00:00
|
|
|
GtkTreeModel *tree_model = (GtkTreeModel *)data;
|
2001-10-02 18:54:05 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
|
|
|
|
gboolean fixed;
|
|
|
|
|
|
|
|
/* get toggled iter */
|
2020-03-07 14:01:21 +00:00
|
|
|
gtk_tree_model_get_iter (tree_model, &iter, path);
|
|
|
|
gtk_tree_model_get (tree_model, &iter, COLUMN_FIXED, &fixed, -1);
|
2001-10-02 18:54:05 +00:00
|
|
|
|
|
|
|
/* do something with the value */
|
|
|
|
fixed ^= 1;
|
2002-01-19 20:15:56 +00:00
|
|
|
|
2001-10-02 18:54:05 +00:00
|
|
|
/* set new value */
|
2020-03-07 14:01:21 +00:00
|
|
|
gtk_list_store_set (GTK_LIST_STORE (tree_model), &iter, COLUMN_FIXED, fixed, -1);
|
2002-01-19 20:15:56 +00:00
|
|
|
|
2001-10-02 18:54:05 +00:00
|
|
|
/* clean up */
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_columns (GtkTreeView *treeview)
|
|
|
|
{
|
|
|
|
GtkCellRenderer *renderer;
|
|
|
|
GtkTreeViewColumn *column;
|
|
|
|
|
|
|
|
/* column for fixed toggles */
|
|
|
|
renderer = gtk_cell_renderer_toggle_new ();
|
2002-09-29 21:24:24 +00:00
|
|
|
g_signal_connect (renderer, "toggled",
|
2009-07-21 01:16:56 +00:00
|
|
|
G_CALLBACK (fixed_toggled), model);
|
2001-10-02 18:54:05 +00:00
|
|
|
|
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Fixed?",
|
2009-07-21 01:16:56 +00:00
|
|
|
renderer,
|
|
|
|
"active", COLUMN_FIXED,
|
|
|
|
NULL);
|
2001-10-02 18:54:05 +00:00
|
|
|
|
|
|
|
/* set this column to a fixed sizing (of 50 pixels) */
|
|
|
|
gtk_tree_view_column_set_sizing (GTK_TREE_VIEW_COLUMN (column),
|
2009-07-21 01:16:56 +00:00
|
|
|
GTK_TREE_VIEW_COLUMN_FIXED);
|
2001-10-02 18:54:05 +00:00
|
|
|
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",
|
2009-07-21 01:16:56 +00:00
|
|
|
renderer,
|
|
|
|
"text",
|
|
|
|
COLUMN_NUMBER,
|
|
|
|
NULL);
|
2002-01-19 20:15:56 +00:00
|
|
|
gtk_tree_view_column_set_sort_column_id (column, COLUMN_NUMBER);
|
2001-10-02 18:54:05 +00:00
|
|
|
gtk_tree_view_append_column (treeview, column);
|
2002-01-19 20:15:56 +00:00
|
|
|
|
2001-10-02 18:54:05 +00:00
|
|
|
/* column for severities */
|
|
|
|
renderer = gtk_cell_renderer_text_new ();
|
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Severity",
|
2009-07-21 01:16:56 +00:00
|
|
|
renderer,
|
|
|
|
"text",
|
|
|
|
COLUMN_SEVERITY,
|
|
|
|
NULL);
|
2002-01-19 20:15:56 +00:00
|
|
|
gtk_tree_view_column_set_sort_column_id (column, COLUMN_SEVERITY);
|
2001-10-02 18:54:05 +00:00
|
|
|
gtk_tree_view_append_column (treeview, column);
|
2002-01-19 20:15:56 +00:00
|
|
|
|
2001-10-02 18:54:05 +00:00
|
|
|
/* column for description */
|
|
|
|
renderer = gtk_cell_renderer_text_new ();
|
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Description",
|
2009-07-21 01:16:56 +00:00
|
|
|
renderer,
|
|
|
|
"text",
|
|
|
|
COLUMN_DESCRIPTION,
|
|
|
|
NULL);
|
2002-01-19 20:15:56 +00:00
|
|
|
gtk_tree_view_column_set_sort_column_id (column, COLUMN_DESCRIPTION);
|
2001-10-02 18:54:05 +00:00
|
|
|
gtk_tree_view_append_column (treeview, column);
|
2009-07-21 01:16:56 +00:00
|
|
|
|
|
|
|
/* 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);
|
2010-04-26 10:55:34 +00:00
|
|
|
|
|
|
|
/* column for symbolic icon */
|
|
|
|
renderer = gtk_cell_renderer_pixbuf_new ();
|
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Symbolic icon",
|
|
|
|
renderer,
|
|
|
|
"icon-name",
|
|
|
|
COLUMN_ICON,
|
|
|
|
"sensitive",
|
|
|
|
COLUMN_SENSITIVE,
|
|
|
|
NULL);
|
|
|
|
gtk_tree_view_column_set_sort_column_id (column, COLUMN_ICON);
|
|
|
|
gtk_tree_view_append_column (treeview, column);
|
2009-07-21 01:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2001-10-02 18:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
2003-11-08 22:08:05 +00:00
|
|
|
do_list_store (GtkWidget *do_widget)
|
2001-10-02 18:54:05 +00:00
|
|
|
{
|
|
|
|
if (!window)
|
|
|
|
{
|
|
|
|
GtkWidget *vbox;
|
|
|
|
GtkWidget *label;
|
|
|
|
GtkWidget *sw;
|
|
|
|
GtkWidget *treeview;
|
|
|
|
|
|
|
|
/* create window, etc */
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2015-06-20 01:59:15 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "List Store");
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2001-10-02 18:54:05 +00:00
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (vbox, 8);
|
|
|
|
gtk_widget_set_margin_end (vbox, 8);
|
|
|
|
gtk_widget_set_margin_top (vbox, 8);
|
|
|
|
gtk_widget_set_margin_bottom (vbox, 8);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), vbox);
|
2001-10-02 18:54:05 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("This is the bug list (note: not based on real data, it would be nice to have a nice ODBC interface to bugzilla or so, though).");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), label);
|
2001-10-02 18:54:05 +00:00
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2020-04-17 03:25:06 +00:00
|
|
|
gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);
|
2001-10-02 18:54:05 +00:00
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
2009-07-21 01:16:56 +00:00
|
|
|
GTK_POLICY_NEVER,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (vbox), sw);
|
2002-01-19 20:15:56 +00:00
|
|
|
|
2001-10-02 18:54:05 +00:00
|
|
|
/* create tree model */
|
|
|
|
model = create_model ();
|
|
|
|
|
|
|
|
/* create tree view */
|
|
|
|
treeview = gtk_tree_view_new_with_model (model);
|
2017-10-06 18:21:19 +00:00
|
|
|
gtk_widget_set_vexpand (treeview, TRUE);
|
2001-10-02 18:54:05 +00:00
|
|
|
gtk_tree_view_set_search_column (GTK_TREE_VIEW (treeview),
|
2009-07-21 01:16:56 +00:00
|
|
|
COLUMN_DESCRIPTION);
|
2001-10-02 18:54:05 +00:00
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_object_unref (model);
|
2002-01-19 20:15:56 +00:00
|
|
|
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), treeview);
|
2002-01-19 20:15:56 +00:00
|
|
|
|
2001-10-02 18:54:05 +00:00
|
|
|
/* add columns to the tree view */
|
|
|
|
add_columns (GTK_TREE_VIEW (treeview));
|
|
|
|
|
|
|
|
/* finish & show */
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 280, 250);
|
2017-12-31 17:54:42 +00:00
|
|
|
g_signal_connect (window, "destroy", G_CALLBACK (window_closed), NULL);
|
2001-10-02 18:54:05 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (window))
|
2009-07-21 01:16:56 +00:00
|
|
|
{
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2009-10-14 17:43:05 +00:00
|
|
|
if (timeout == 0) {
|
2009-10-15 11:25:21 +00:00
|
|
|
/* FIXME this should use the animation-duration instead */
|
2009-07-21 01:16:56 +00:00
|
|
|
timeout = g_timeout_add (80, spinner_timeout, NULL);
|
2009-10-14 17:43:05 +00:00
|
|
|
}
|
2009-07-21 01:16:56 +00:00
|
|
|
}
|
2001-10-02 18:54:05 +00:00
|
|
|
else
|
|
|
|
{
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2001-10-02 18:54:05 +00:00
|
|
|
window = NULL;
|
2009-07-21 01:16:56 +00:00
|
|
|
if (timeout != 0)
|
|
|
|
{
|
|
|
|
g_source_remove (timeout);
|
|
|
|
timeout = 0;
|
|
|
|
}
|
2001-10-02 18:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|