Add powerbox hook
[gtk-with-powerbox.git] / tests / testtreeedit.c
blob30d2a3fa548e4c8add47f68ebfef5643ec4b4b40
1 /* testtreeedit.c
2 * Copyright (C) 2001 Red Hat, Inc
3 * Author: Jonathan Blandford
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "config.h"
22 #include <gtk/gtk.h>
24 typedef struct {
25 const gchar *string;
26 gboolean is_editable;
27 gint progress;
28 } ListEntry;
30 enum {
31 STRING_COLUMN,
32 IS_EDITABLE_COLUMN,
33 PIXBUF_COLUMN,
34 PROGRESS_COLUMN,
35 NUM_COLUMNS
38 static ListEntry model_strings[] =
40 {"A simple string", TRUE, 0 },
41 {"Another string!", TRUE, 10 },
42 {"Guess what, a third string. This one can't be edited", FALSE, 47 },
43 {"And then a fourth string. Neither can this", FALSE, 48 },
44 {"Multiline\nFun!", TRUE, 75 },
45 { NULL }
48 static GtkTreeModel *
49 create_model (void)
51 GtkTreeStore *model;
52 GtkTreeIter iter;
53 gint i;
54 GdkPixbuf *foo;
55 GtkWidget *blah;
57 blah = gtk_window_new (GTK_WINDOW_TOPLEVEL);
58 foo = gtk_widget_render_icon (blah, GTK_STOCK_NEW, GTK_ICON_SIZE_MENU, NULL);
59 gtk_widget_destroy (blah);
61 model = gtk_tree_store_new (NUM_COLUMNS,
62 G_TYPE_STRING,
63 G_TYPE_BOOLEAN,
64 GDK_TYPE_PIXBUF,
65 G_TYPE_INT);
67 for (i = 0; model_strings[i].string != NULL; i++)
69 gtk_tree_store_append (model, &iter, NULL);
71 gtk_tree_store_set (model, &iter,
72 STRING_COLUMN, model_strings[i].string,
73 IS_EDITABLE_COLUMN, model_strings[i].is_editable,
74 PIXBUF_COLUMN, foo,
75 PROGRESS_COLUMN, model_strings[i].progress,
76 -1);
79 return GTK_TREE_MODEL (model);
82 static void
83 toggled (GtkCellRendererToggle *cell,
84 gchar *path_string,
85 gpointer data)
87 GtkTreeModel *model = GTK_TREE_MODEL (data);
88 GtkTreeIter iter;
89 GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
90 gboolean value;
92 gtk_tree_model_get_iter (model, &iter, path);
93 gtk_tree_model_get (model, &iter, IS_EDITABLE_COLUMN, &value, -1);
95 value = !value;
96 gtk_tree_store_set (GTK_TREE_STORE (model), &iter, IS_EDITABLE_COLUMN, value, -1);
98 gtk_tree_path_free (path);
101 static void
102 edited (GtkCellRendererText *cell,
103 gchar *path_string,
104 gchar *new_text,
105 gpointer data)
107 GtkTreeModel *model = GTK_TREE_MODEL (data);
108 GtkTreeIter iter;
109 GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
111 gtk_tree_model_get_iter (model, &iter, path);
112 gtk_tree_store_set (GTK_TREE_STORE (model), &iter, STRING_COLUMN, new_text, -1);
114 gtk_tree_path_free (path);
117 static gboolean
118 button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer callback_data)
120 /* Deselect if people click outside any row. */
121 if (event->window == gtk_tree_view_get_bin_window (GTK_TREE_VIEW (widget))
122 && !gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
123 event->x, event->y, NULL, NULL, NULL, NULL)) {
124 gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (widget)));
127 /* Let the default code run in any case; it won't reselect anything. */
128 return FALSE;
131 gint
132 main (gint argc, gchar **argv)
134 GtkWidget *window;
135 GtkWidget *scrolled_window;
136 GtkWidget *tree_view;
137 GtkTreeModel *tree_model;
138 GtkCellRenderer *renderer;
139 GtkTreeViewColumn *column;
141 gtk_init (&argc, &argv);
143 if (g_getenv ("RTL"))
144 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
146 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
147 gtk_window_set_title (GTK_WINDOW (window), "GtkTreeView editing sample");
148 g_signal_connect (window, "destroy", gtk_main_quit, NULL);
150 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
151 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_ETCHED_IN);
152 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
153 gtk_container_add (GTK_CONTAINER (window), scrolled_window);
155 tree_model = create_model ();
156 tree_view = gtk_tree_view_new_with_model (tree_model);
157 g_signal_connect (tree_view, "button_press_event", G_CALLBACK (button_press_event), NULL);
158 gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);
159 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), TRUE);
161 column = gtk_tree_view_column_new ();
162 gtk_tree_view_column_set_title (column, "String");
164 renderer = gtk_cell_renderer_pixbuf_new ();
165 gtk_tree_view_column_pack_start (column, renderer, TRUE);
166 gtk_tree_view_column_set_attributes (column, renderer,
167 "pixbuf", PIXBUF_COLUMN, NULL);
169 renderer = gtk_cell_renderer_text_new ();
170 gtk_tree_view_column_pack_start (column, renderer, TRUE);
171 gtk_tree_view_column_set_attributes (column, renderer,
172 "text", STRING_COLUMN,
173 "editable", IS_EDITABLE_COLUMN,
174 NULL);
175 g_signal_connect (renderer, "edited",
176 G_CALLBACK (edited), tree_model);
177 renderer = gtk_cell_renderer_text_new ();
178 gtk_tree_view_column_pack_start (column, renderer, TRUE);
179 gtk_tree_view_column_set_attributes (column, renderer,
180 "text", STRING_COLUMN,
181 "editable", IS_EDITABLE_COLUMN,
182 NULL);
183 g_signal_connect (renderer, "edited",
184 G_CALLBACK (edited), tree_model);
186 renderer = gtk_cell_renderer_pixbuf_new ();
187 gtk_tree_view_column_pack_start (column, renderer, TRUE);
188 gtk_tree_view_column_set_attributes (column, renderer,
189 "pixbuf", PIXBUF_COLUMN, NULL);
190 gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
192 renderer = gtk_cell_renderer_toggle_new ();
193 g_signal_connect (renderer, "toggled",
194 G_CALLBACK (toggled), tree_model);
196 g_object_set (renderer,
197 "xalign", 0.0,
198 NULL);
199 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
200 -1, "Editable",
201 renderer,
202 "active", IS_EDITABLE_COLUMN,
203 NULL);
205 renderer = gtk_cell_renderer_progress_new ();
206 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
207 -1, "Progress",
208 renderer,
209 "value", PROGRESS_COLUMN,
210 NULL);
212 gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
214 gtk_window_set_default_size (GTK_WINDOW (window),
215 800, 175);
217 gtk_widget_show_all (window);
218 gtk_main ();
220 return 0;