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
|
|
|
|
* simple GtkListStore and displays it. See the Stock Browser
|
|
|
|
* demo for a more advanced example.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
static GtkWidget *window = NULL;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const gboolean fixed;
|
|
|
|
const guint number;
|
|
|
|
const gchar *severity;
|
|
|
|
const gchar *description;
|
|
|
|
}
|
|
|
|
Bug;
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
COLUMN_FIXED,
|
|
|
|
COLUMN_NUMBER,
|
|
|
|
COLUMN_SEVERITY,
|
|
|
|
COLUMN_DESCRIPTION,
|
|
|
|
NUM_COLUMNS
|
|
|
|
};
|
|
|
|
|
|
|
|
static Bug data[] =
|
|
|
|
{
|
|
|
|
{ FALSE, 60482, "Normal", "scrollable notebooks and hidden tabs" },
|
|
|
|
{ FALSE, 60620, "Critical", "gdk_window_clear_area (gdkwindow-win32.c) is not thread-safe" },
|
|
|
|
{ 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" },
|
|
|
|
{ FALSE, 6112, "Enhancement","netscape-like collapsable toolbars" },
|
|
|
|
{ FALSE, 1, "Normal", "First bug :=)" },
|
|
|
|
};
|
|
|
|
|
|
|
|
static GtkTreeModel *
|
|
|
|
create_model (void)
|
|
|
|
{
|
|
|
|
gint i = 0;
|
|
|
|
GtkListStore *store;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
/* create list store */
|
|
|
|
store = gtk_list_store_new (NUM_COLUMNS,
|
|
|
|
G_TYPE_BOOLEAN,
|
|
|
|
G_TYPE_UINT,
|
|
|
|
G_TYPE_STRING,
|
|
|
|
G_TYPE_STRING);
|
|
|
|
|
|
|
|
/* add data to the list store */
|
2001-10-26 16:31:01 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS (data); i++)
|
2001-10-02 18:54:05 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_TREE_MODEL (store);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fixed_toggled (GtkCellRendererToggle *cell,
|
|
|
|
gchar *path_str,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkTreeModel *model = (GtkTreeModel *)data;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
|
|
|
|
gboolean fixed;
|
|
|
|
|
|
|
|
/* get toggled iter */
|
|
|
|
gtk_tree_model_get_iter (model, &iter, path);
|
|
|
|
gtk_tree_model_get (model, &iter, COLUMN_FIXED, &fixed, -1);
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
gtk_list_store_set (GTK_LIST_STORE (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;
|
|
|
|
GtkTreeModel *model = gtk_tree_view_get_model (treeview);
|
|
|
|
|
|
|
|
/* column for fixed toggles */
|
|
|
|
renderer = gtk_cell_renderer_toggle_new ();
|
2002-09-29 21:24:24 +00:00
|
|
|
g_signal_connect (renderer, "toggled",
|
2001-10-02 18:54:05 +00:00
|
|
|
G_CALLBACK (fixed_toggled), model);
|
|
|
|
|
|
|
|
column = gtk_tree_view_column_new_with_attributes ("Fixed?",
|
|
|
|
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_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);
|
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",
|
|
|
|
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",
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
GtkTreeModel *model;
|
|
|
|
GtkWidget *treeview;
|
|
|
|
|
|
|
|
/* create window, etc */
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2003-11-08 22:08:05 +00:00
|
|
|
gtk_window_set_screen (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_screen (do_widget));
|
2001-10-02 18:54:05 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "GtkListStore demo");
|
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_signal_connect (window, "destroy",
|
2001-10-02 18:54:05 +00:00
|
|
|
G_CALLBACK (gtk_widget_destroyed), &window);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
|
|
|
|
|
|
|
|
vbox = gtk_vbox_new (FALSE, 8);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
|
|
|
|
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).");
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
sw = gtk_scrolled_window_new (NULL, NULL);
|
|
|
|
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_SHADOW_ETCHED_IN);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_NEVER,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
|
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);
|
|
|
|
gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
|
|
|
|
gtk_tree_view_set_search_column (GTK_TREE_VIEW (treeview),
|
|
|
|
COLUMN_DESCRIPTION);
|
|
|
|
|
2002-09-29 21:24:24 +00:00
|
|
|
g_object_unref (model);
|
2002-01-19 20:15:56 +00:00
|
|
|
|
2001-10-02 18:54:05 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GTK_WIDGET_VISIBLE (window))
|
|
|
|
gtk_widget_show_all (window);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtk_widget_destroy (window);
|
|
|
|
window = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|