1 /* testtreecolumnsizing.c: Test case for tree view column resizing.
2 *
3 * Copyright (C) 2008 Kristian Rietveld <kris@gtk.org>
4 *
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
8 *
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
21 */
22
23 #include <gtk/gtk.h>
24 #include <string.h>
25
26 #define NO_EXPAND "No expandable columns"
27 #define SINGLE_EXPAND "One expandable column"
28 #define MULTI_EXPAND "Multiple expandable columns"
29 #define LAST_EXPAND "Last column is expandable"
30 #define BORDER_EXPAND "First and last columns are expandable"
31 #define ALL_EXPAND "All columns are expandable"
32
33 #define N_ROWS 10
34
35
36 static GtkTreeModel *
create_model(void)37 create_model (void)
38 {
39 int i;
40 GtkListStore *store;
41
42 store = gtk_list_store_new (5,
43 G_TYPE_STRING,
44 G_TYPE_STRING,
45 G_TYPE_STRING,
46 G_TYPE_STRING,
47 G_TYPE_STRING);
48
49 for (i = 0; i < N_ROWS; i++)
50 {
51 gchar *str;
52
53 str = g_strdup_printf ("Row %d", i);
54 gtk_list_store_insert_with_values (store, NULL, i,
55 0, str,
56 1, "Blah blah blah blah blah",
57 2, "Less blah",
58 3, "Medium length",
59 4, "Eek",
60 -1);
61 g_free (str);
62 }
63
64 return GTK_TREE_MODEL (store);
65 }
66
67 static void
toggle_long_content_row(GtkToggleButton * button,gpointer user_data)68 toggle_long_content_row (GtkToggleButton *button,
69 gpointer user_data)
70 {
71 GtkTreeModel *model;
72
73 model = gtk_tree_view_get_model (GTK_TREE_VIEW (user_data));
74 if (gtk_tree_model_iter_n_children (model, NULL) == N_ROWS)
75 {
76 gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, N_ROWS,
77 0, "Very very very very longggggg",
78 1, "Blah blah blah blah blah",
79 2, "Less blah",
80 3, "Medium length",
81 4, "Eek we make the scrollbar appear",
82 -1);
83 }
84 else
85 {
86 GtkTreeIter iter;
87
88 gtk_tree_model_iter_nth_child (model, &iter, NULL, N_ROWS);
89 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
90 }
91 }
92
93 static void
combo_box_changed(GtkComboBox * combo_box,gpointer user_data)94 combo_box_changed (GtkComboBox *combo_box,
95 gpointer user_data)
96 {
97 gchar *str;
98 GList *list;
99 GList *columns;
100
101 str = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (combo_box));
102 if (!str)
103 return;
104
105 columns = gtk_tree_view_get_columns (GTK_TREE_VIEW (user_data));
106
107 if (!strcmp (str, NO_EXPAND))
108 {
109 for (list = columns; list; list = list->next)
110 gtk_tree_view_column_set_expand (list->data, FALSE);
111 }
112 else if (!strcmp (str, SINGLE_EXPAND))
113 {
114 for (list = columns; list; list = list->next)
115 {
116 if (list->prev && !list->prev->prev)
117 /* This is the second column */
118 gtk_tree_view_column_set_expand (list->data, TRUE);
119 else
120 gtk_tree_view_column_set_expand (list->data, FALSE);
121 }
122 }
123 else if (!strcmp (str, MULTI_EXPAND))
124 {
125 for (list = columns; list; list = list->next)
126 {
127 if (list->prev && !list->prev->prev)
128 /* This is the second column */
129 gtk_tree_view_column_set_expand (list->data, TRUE);
130 else if (list->prev && !list->prev->prev->prev)
131 /* This is the third column */
132 gtk_tree_view_column_set_expand (list->data, TRUE);
133 else
134 gtk_tree_view_column_set_expand (list->data, FALSE);
135 }
136 }
137 else if (!strcmp (str, LAST_EXPAND))
138 {
139 for (list = columns; list->next; list = list->next)
140 gtk_tree_view_column_set_expand (list->data, FALSE);
141 /* This is the last column */
142 gtk_tree_view_column_set_expand (list->data, TRUE);
143 }
144 else if (!strcmp (str, BORDER_EXPAND))
145 {
146 gtk_tree_view_column_set_expand (columns->data, TRUE);
147 for (list = columns->next; list->next; list = list->next)
148 gtk_tree_view_column_set_expand (list->data, FALSE);
149 /* This is the last column */
150 gtk_tree_view_column_set_expand (list->data, TRUE);
151 }
152 else if (!strcmp (str, ALL_EXPAND))
153 {
154 for (list = columns; list; list = list->next)
155 gtk_tree_view_column_set_expand (list->data, TRUE);
156 }
157
158 g_free (str);
159 g_list_free (columns);
160 }
161
162 int
main(int argc,char ** argv)163 main (int argc, char **argv)
164 {
165 int i;
166 GtkWidget *window;
167 GtkWidget *vbox;
168 GtkWidget *combo_box;
169 GtkWidget *sw;
170 GtkWidget *tree_view;
171 GtkWidget *button;
172
173 gtk_init (&argc, &argv);
174
175 /* Window and box */
176 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
177 gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);
178 g_signal_connect (window, "delete-event", G_CALLBACK (gtk_main_quit), NULL);
179 gtk_container_set_border_width (GTK_CONTAINER (window), 5);
180
181 vbox = gtk_vbox_new (FALSE, 5);
182 gtk_container_add (GTK_CONTAINER (window), vbox);
183
184 /* Option menu contents */
185 combo_box = gtk_combo_box_text_new ();
186
187 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), NO_EXPAND);
188 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), SINGLE_EXPAND);
189 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), MULTI_EXPAND);
190 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), LAST_EXPAND);
191 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), BORDER_EXPAND);
192 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), ALL_EXPAND);
193
194 gtk_box_pack_start (GTK_BOX (vbox), combo_box, FALSE, FALSE, 0);
195
196 /* Scrolled window and tree view */
197 sw = gtk_scrolled_window_new (NULL, NULL);
198 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
199 GTK_POLICY_AUTOMATIC,
200 GTK_POLICY_AUTOMATIC);
201 gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
202
203 tree_view = gtk_tree_view_new_with_model (create_model ());
204 gtk_container_add (GTK_CONTAINER (sw), tree_view);
205
206 for (i = 0; i < 5; i++)
207 {
208 GtkTreeViewColumn *column;
209
210 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view),
211 i, "Header",
212 gtk_cell_renderer_text_new (),
213 "text", i,
214 NULL);
215
216 column = gtk_tree_view_get_column (GTK_TREE_VIEW (tree_view), i);
217 gtk_tree_view_column_set_resizable (column, TRUE);
218 }
219
220 /* Toggle button for long content row */
221 button = gtk_toggle_button_new_with_label ("Toggle long content row");
222 g_signal_connect (button, "toggled",
223 G_CALLBACK (toggle_long_content_row), tree_view);
224 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
225
226 /* Set up option menu callback and default item */
227 g_signal_connect (combo_box, "changed",
228 G_CALLBACK (combo_box_changed), tree_view);
229 gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
230
231 /* Done */
232 gtk_widget_show_all (window);
233
234 gtk_main ();
235
236 return 0;
237 }
238