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