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 * |
37 | create_model (void) |
38 | { |
39 | int i; |
40 | GtkListStore *store; |
41 | |
42 | store = gtk_list_store_new (n_columns: 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 | char *str; |
52 | |
53 | str = g_strdup_printf (format: "Row %d" , i); |
54 | gtk_list_store_insert_with_values (list_store: store, NULL, position: 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 (mem: str); |
62 | } |
63 | |
64 | return GTK_TREE_MODEL (store); |
65 | } |
66 | |
67 | static void |
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 (tree_model: 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 (tree_model: model, iter: &iter, NULL, N_ROWS); |
89 | gtk_list_store_remove (GTK_LIST_STORE (model), iter: &iter); |
90 | } |
91 | } |
92 | |
93 | static void |
94 | combo_box_changed (GtkComboBox *combo_box, |
95 | gpointer user_data) |
96 | { |
97 | char *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 (s1: str, NO_EXPAND)) |
108 | { |
109 | for (list = columns; list; list = list->next) |
110 | gtk_tree_view_column_set_expand (tree_column: list->data, FALSE); |
111 | } |
112 | else if (!strcmp (s1: 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 (tree_column: list->data, TRUE); |
119 | else |
120 | gtk_tree_view_column_set_expand (tree_column: list->data, FALSE); |
121 | } |
122 | } |
123 | else if (!strcmp (s1: 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 (tree_column: 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 (tree_column: list->data, TRUE); |
133 | else |
134 | gtk_tree_view_column_set_expand (tree_column: list->data, FALSE); |
135 | } |
136 | } |
137 | else if (!strcmp (s1: str, LAST_EXPAND)) |
138 | { |
139 | for (list = columns; list->next; list = list->next) |
140 | gtk_tree_view_column_set_expand (tree_column: list->data, FALSE); |
141 | /* This is the last column */ |
142 | gtk_tree_view_column_set_expand (tree_column: list->data, TRUE); |
143 | } |
144 | else if (!strcmp (s1: str, BORDER_EXPAND)) |
145 | { |
146 | gtk_tree_view_column_set_expand (tree_column: columns->data, TRUE); |
147 | for (list = columns->next; list->next; list = list->next) |
148 | gtk_tree_view_column_set_expand (tree_column: list->data, FALSE); |
149 | /* This is the last column */ |
150 | gtk_tree_view_column_set_expand (tree_column: list->data, TRUE); |
151 | } |
152 | else if (!strcmp (s1: str, ALL_EXPAND)) |
153 | { |
154 | for (list = columns; list; list = list->next) |
155 | gtk_tree_view_column_set_expand (tree_column: list->data, TRUE); |
156 | } |
157 | |
158 | g_free (mem: str); |
159 | g_list_free (list: columns); |
160 | } |
161 | |
162 | static void |
163 | quit_cb (GtkWidget *widget, |
164 | gpointer data) |
165 | { |
166 | gboolean *done = data; |
167 | |
168 | *done = TRUE; |
169 | |
170 | g_main_context_wakeup (NULL); |
171 | } |
172 | |
173 | int |
174 | main (int argc, char **argv) |
175 | { |
176 | int i; |
177 | GtkWidget *window; |
178 | GtkWidget *vbox; |
179 | GtkWidget *combo_box; |
180 | GtkWidget *sw; |
181 | GtkWidget *tree_view; |
182 | GtkWidget *button; |
183 | gboolean done = FALSE; |
184 | |
185 | gtk_init (); |
186 | |
187 | /* Window and box */ |
188 | window = gtk_window_new (); |
189 | gtk_window_set_default_size (GTK_WINDOW (window), width: 640, height: 480); |
190 | g_signal_connect (window, "destroy" , G_CALLBACK (quit_cb), &done); |
191 | |
192 | vbox = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 5); |
193 | gtk_window_set_child (GTK_WINDOW (window), child: vbox); |
194 | |
195 | /* Option menu contents */ |
196 | combo_box = gtk_combo_box_text_new (); |
197 | |
198 | gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), NO_EXPAND); |
199 | gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), SINGLE_EXPAND); |
200 | gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), MULTI_EXPAND); |
201 | gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), LAST_EXPAND); |
202 | gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), BORDER_EXPAND); |
203 | gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), ALL_EXPAND); |
204 | |
205 | gtk_box_append (GTK_BOX (vbox), child: combo_box); |
206 | |
207 | /* Scrolled window and tree view */ |
208 | sw = gtk_scrolled_window_new (); |
209 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), |
210 | hscrollbar_policy: GTK_POLICY_AUTOMATIC, |
211 | vscrollbar_policy: GTK_POLICY_AUTOMATIC); |
212 | gtk_widget_set_vexpand (widget: sw, TRUE); |
213 | gtk_box_append (GTK_BOX (vbox), child: sw); |
214 | |
215 | tree_view = gtk_tree_view_new_with_model (model: create_model ()); |
216 | gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), child: tree_view); |
217 | |
218 | for (i = 0; i < 5; i++) |
219 | { |
220 | GtkTreeViewColumn *column; |
221 | |
222 | gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view), |
223 | position: i, title: "Header" , |
224 | cell: gtk_cell_renderer_text_new (), |
225 | "text" , i, |
226 | NULL); |
227 | |
228 | column = gtk_tree_view_get_column (GTK_TREE_VIEW (tree_view), n: i); |
229 | gtk_tree_view_column_set_resizable (tree_column: column, TRUE); |
230 | } |
231 | |
232 | /* Toggle button for long content row */ |
233 | button = gtk_toggle_button_new_with_label (label: "Toggle long content row" ); |
234 | g_signal_connect (button, "toggled" , |
235 | G_CALLBACK (toggle_long_content_row), tree_view); |
236 | gtk_box_append (GTK_BOX (vbox), child: button); |
237 | |
238 | /* Set up option menu callback and default item */ |
239 | g_signal_connect (combo_box, "changed" , |
240 | G_CALLBACK (combo_box_changed), tree_view); |
241 | gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), index_: 0); |
242 | |
243 | /* Done */ |
244 | gtk_widget_show (widget: window); |
245 | |
246 | while (!done) |
247 | g_main_context_iteration (NULL, TRUE); |
248 | |
249 | return 0; |
250 | } |
251 | |