1 | /* testtreesort.c |
2 | * Copyright (C) 2001 Red Hat, Inc |
3 | * Author: Jonathan Blandford |
4 | * |
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. |
9 | * |
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. |
14 | * |
15 | * You should have received a copy of the GNU Library General Public |
16 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | #include "config.h" |
20 | #include <gtk/gtk.h> |
21 | |
22 | |
23 | typedef struct _ListSort ListSort; |
24 | struct _ListSort |
25 | { |
26 | const char *word_1; |
27 | const char *word_2; |
28 | const char *word_3; |
29 | const char *word_4; |
30 | int number_1; |
31 | }; |
32 | |
33 | static ListSort data[] = |
34 | { |
35 | { "Apples" , "Transmogrify long word to demonstrate weirdness" , "Exculpatory" , "Gesundheit" , 30 }, |
36 | { "Oranges" , "Wicker" , "Adamantine" , "Convivial" , 10 }, |
37 | { "Bovine Spongiform Encephilopathy" , "Sleazebucket" , "Mountaineer" , "Pander" , 40 }, |
38 | { "Foot and Mouth" , "Lampshade" , "Skim Milk\nFull Milk" , "Viewless" , 20 }, |
39 | { "Blood,\nsweat,\ntears" , "The Man" , "Horses" , "Muckety-Muck" , 435 }, |
40 | { "Rare Steak" , "Siam" , "Watchdog" , "Xantippe" , 99999 }, |
41 | { "SIGINT" , "Rabbit Breath" , "Alligator" , "Bloodstained" , 4123 }, |
42 | { "Google" , "Chrysanthemums" , "Hobnob" , "Leapfrog" , 1 }, |
43 | { "Technology fibre optic" , "Turtle" , "Academe" , "Lonely" , 3 }, |
44 | { "Freon" , "Harpes" , "Quidditch" , "Reagan" , 6}, |
45 | { "Transposition" , "Fruit Basket" , "Monkey Wort" , "Glogg" , 54 }, |
46 | { "Fern" , "Glasnost and Perestroika" , "Latitude" , "Bomberman!!!" , 2 }, |
47 | {NULL, } |
48 | }; |
49 | |
50 | static ListSort childdata[] = |
51 | { |
52 | { "Heineken" , "Nederland" , "Wanda de vis" , "Electronische post" , 2}, |
53 | { "Hottentottententententoonstelling" , "Rotterdam" , "Ionentransport" , "Palm" , 45}, |
54 | { "Fruitvlieg" , "Eigenfrequentie" , "Supernoodles" , "Ramen" , 2002}, |
55 | { "Gereedschapskist" , "Stelsel van lineaire vergelijkingen" , "Tulpen" , "Badlaken" , 1311}, |
56 | { "Stereoinstallatie" , "Rood tapijt" , "Het periodieke systeem der elementen" , "Laaste woord" , 200}, |
57 | {NULL, } |
58 | }; |
59 | |
60 | |
61 | enum |
62 | { |
63 | WORD_COLUMN_1 = 0, |
64 | WORD_COLUMN_2, |
65 | WORD_COLUMN_3, |
66 | WORD_COLUMN_4, |
67 | NUMBER_COLUMN_1, |
68 | NUM_COLUMNS |
69 | }; |
70 | |
71 | static void |
72 | switch_search_method (GtkWidget *button, |
73 | gpointer tree_view) |
74 | { |
75 | if (!gtk_tree_view_get_search_entry (GTK_TREE_VIEW (tree_view))) |
76 | { |
77 | gpointer search_entry = g_object_get_data (object: tree_view, key: "my-search-entry" ); |
78 | gtk_tree_view_set_search_entry (GTK_TREE_VIEW (tree_view), GTK_EDITABLE (search_entry)); |
79 | } |
80 | else |
81 | gtk_tree_view_set_search_entry (GTK_TREE_VIEW (tree_view), NULL); |
82 | } |
83 | |
84 | static void |
85 | quit_cb (GtkWidget *widget, |
86 | gpointer user_data) |
87 | { |
88 | gboolean *done = user_data; |
89 | |
90 | *done = TRUE; |
91 | |
92 | g_main_context_wakeup (NULL); |
93 | } |
94 | |
95 | int |
96 | main (int argc, char *argv[]) |
97 | { |
98 | GtkWidget *window; |
99 | GtkWidget *vbox; |
100 | GtkWidget *scrolled_window; |
101 | GtkWidget *tree_view; |
102 | GtkTreeStore *model; |
103 | GtkTreeModel *smodel = NULL; |
104 | GtkTreeModel *ssmodel = NULL; |
105 | GtkCellRenderer *renderer; |
106 | GtkTreeViewColumn *column; |
107 | GtkTreeIter iter; |
108 | int i; |
109 | |
110 | GtkWidget *entry, *button; |
111 | GtkWidget *window2, *vbox2, *scrolled_window2, *tree_view2; |
112 | GtkWidget *window3, *vbox3, *scrolled_window3, *tree_view3; |
113 | gboolean done = FALSE; |
114 | |
115 | gtk_init (); |
116 | |
117 | /** |
118 | * First window - Just a GtkTreeStore |
119 | */ |
120 | |
121 | window = gtk_window_new (); |
122 | gtk_window_set_title (GTK_WINDOW (window), title: "Words, words, words - Window 1" ); |
123 | g_signal_connect (window, "destroy" , G_CALLBACK (quit_cb), &done); |
124 | vbox = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 8); |
125 | gtk_box_append (GTK_BOX (vbox), child: gtk_label_new (str: "Jonathan and Kristian's list of cool words. (And Anders' cool list of numbers) \n\nThis is just a GtkTreeStore" )); |
126 | gtk_window_set_child (GTK_WINDOW (window), child: vbox); |
127 | |
128 | entry = gtk_entry_new (); |
129 | gtk_box_append (GTK_BOX (vbox), child: entry); |
130 | |
131 | button = gtk_button_new_with_label (label: "Switch search method" ); |
132 | gtk_box_append (GTK_BOX (vbox), child: button); |
133 | |
134 | scrolled_window = gtk_scrolled_window_new (); |
135 | gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (scrolled_window), TRUE); |
136 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), hscrollbar_policy: GTK_POLICY_AUTOMATIC, vscrollbar_policy: GTK_POLICY_AUTOMATIC); |
137 | gtk_widget_set_vexpand (widget: scrolled_window, TRUE); |
138 | gtk_box_append (GTK_BOX (vbox), child: scrolled_window); |
139 | |
140 | model = gtk_tree_store_new (n_columns: NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT); |
141 | |
142 | /* |
143 | smodel = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (model)); |
144 | ssmodel = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (smodel)); |
145 | */ |
146 | tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model)); |
147 | |
148 | gtk_tree_view_set_search_entry (GTK_TREE_VIEW (tree_view), GTK_EDITABLE (entry)); |
149 | g_object_set_data (G_OBJECT (tree_view), key: "my-search-entry" , data: entry); |
150 | g_signal_connect (button, "clicked" , |
151 | G_CALLBACK (switch_search_method), tree_view); |
152 | |
153 | /* gtk_tree_selection_set_select_function (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)), select_func, NULL, NULL);*/ |
154 | |
155 | /* 12 iters now, 12 later... */ |
156 | for (i = 0; data[i].word_1 != NULL; i++) |
157 | { |
158 | int k; |
159 | GtkTreeIter child_iter; |
160 | |
161 | |
162 | gtk_tree_store_prepend (GTK_TREE_STORE (model), iter: &iter, NULL); |
163 | gtk_tree_store_set (GTK_TREE_STORE (model), iter: &iter, |
164 | WORD_COLUMN_1, data[i].word_1, |
165 | WORD_COLUMN_2, data[i].word_2, |
166 | WORD_COLUMN_3, data[i].word_3, |
167 | WORD_COLUMN_4, data[i].word_4, |
168 | NUMBER_COLUMN_1, data[i].number_1, |
169 | -1); |
170 | |
171 | gtk_tree_store_append (GTK_TREE_STORE (model), iter: &child_iter, parent: &iter); |
172 | gtk_tree_store_set (GTK_TREE_STORE (model), iter: &child_iter, |
173 | WORD_COLUMN_1, data[i].word_1, |
174 | WORD_COLUMN_2, data[i].word_2, |
175 | WORD_COLUMN_3, data[i].word_3, |
176 | WORD_COLUMN_4, data[i].word_4, |
177 | NUMBER_COLUMN_1, data[i].number_1, |
178 | -1); |
179 | |
180 | for (k = 0; childdata[k].word_1 != NULL; k++) |
181 | { |
182 | gtk_tree_store_append (GTK_TREE_STORE (model), iter: &child_iter, parent: &iter); |
183 | gtk_tree_store_set (GTK_TREE_STORE (model), iter: &child_iter, |
184 | WORD_COLUMN_1, childdata[k].word_1, |
185 | WORD_COLUMN_2, childdata[k].word_2, |
186 | WORD_COLUMN_3, childdata[k].word_3, |
187 | WORD_COLUMN_4, childdata[k].word_4, |
188 | NUMBER_COLUMN_1, childdata[k].number_1, |
189 | -1); |
190 | |
191 | } |
192 | |
193 | } |
194 | |
195 | smodel = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (model)); |
196 | ssmodel = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (smodel)); |
197 | g_object_unref (object: model); |
198 | |
199 | renderer = gtk_cell_renderer_text_new (); |
200 | column = gtk_tree_view_column_new_with_attributes (title: "First Word" , cell: renderer, |
201 | "text" , WORD_COLUMN_1, |
202 | NULL); |
203 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column); |
204 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_1); |
205 | |
206 | renderer = gtk_cell_renderer_text_new (); |
207 | column = gtk_tree_view_column_new_with_attributes (title: "Second Word" , cell: renderer, |
208 | "text" , WORD_COLUMN_2, |
209 | NULL); |
210 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_2); |
211 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column); |
212 | |
213 | renderer = gtk_cell_renderer_text_new (); |
214 | column = gtk_tree_view_column_new_with_attributes (title: "Third Word" , cell: renderer, |
215 | "text" , WORD_COLUMN_3, |
216 | NULL); |
217 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_3); |
218 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column); |
219 | |
220 | renderer = gtk_cell_renderer_text_new (); |
221 | column = gtk_tree_view_column_new_with_attributes (title: "Fourth Word" , cell: renderer, |
222 | "text" , WORD_COLUMN_4, |
223 | NULL); |
224 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_4); |
225 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column); |
226 | |
227 | renderer = gtk_cell_renderer_text_new (); |
228 | column = gtk_tree_view_column_new_with_attributes (title: "First Number" , cell: renderer, |
229 | "text" , NUMBER_COLUMN_1, |
230 | NULL); |
231 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: NUMBER_COLUMN_1); |
232 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column); |
233 | |
234 | /* gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (smodel), |
235 | WORD_COLUMN_1, |
236 | GTK_SORT_ASCENDING);*/ |
237 | |
238 | gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled_window), child: tree_view); |
239 | gtk_window_set_default_size (GTK_WINDOW (window), width: 400, height: 400); |
240 | gtk_widget_show (widget: window); |
241 | |
242 | /** |
243 | * Second window - GtkTreeModelSort wrapping the GtkTreeStore |
244 | */ |
245 | |
246 | if (smodel) |
247 | { |
248 | window2 = gtk_window_new (); |
249 | gtk_window_set_title (GTK_WINDOW (window2), |
250 | title: "Words, words, words - window 2" ); |
251 | g_signal_connect (window2, "destroy" , G_CALLBACK (quit_cb), &done); |
252 | vbox2 = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 8); |
253 | gtk_box_append (GTK_BOX (vbox2), |
254 | child: gtk_label_new (str: "Jonathan and Kristian's list of words.\n\nA GtkTreeModelSort wrapping the GtkTreeStore of window 1" )); |
255 | gtk_window_set_child (GTK_WINDOW (window2), child: vbox2); |
256 | |
257 | scrolled_window2 = gtk_scrolled_window_new (); |
258 | gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (scrolled_window2), TRUE); |
259 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window2), |
260 | hscrollbar_policy: GTK_POLICY_AUTOMATIC, |
261 | vscrollbar_policy: GTK_POLICY_AUTOMATIC); |
262 | gtk_widget_set_vexpand (widget: scrolled_window2, TRUE); |
263 | gtk_box_append (GTK_BOX (vbox2), child: scrolled_window2); |
264 | |
265 | |
266 | tree_view2 = gtk_tree_view_new_with_model (model: smodel); |
267 | |
268 | renderer = gtk_cell_renderer_text_new (); |
269 | column = gtk_tree_view_column_new_with_attributes (title: "First Word" , cell: renderer, |
270 | "text" , WORD_COLUMN_1, |
271 | NULL); |
272 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view2), column); |
273 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_1); |
274 | |
275 | renderer = gtk_cell_renderer_text_new (); |
276 | column = gtk_tree_view_column_new_with_attributes (title: "Second Word" , cell: renderer, |
277 | "text" , WORD_COLUMN_2, |
278 | NULL); |
279 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_2); |
280 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view2), column); |
281 | |
282 | renderer = gtk_cell_renderer_text_new (); |
283 | column = gtk_tree_view_column_new_with_attributes (title: "Third Word" , cell: renderer, |
284 | "text" , WORD_COLUMN_3, |
285 | NULL); |
286 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_3); |
287 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view2), column); |
288 | |
289 | renderer = gtk_cell_renderer_text_new (); |
290 | column = gtk_tree_view_column_new_with_attributes (title: "Fourth Word" , cell: renderer, |
291 | "text" , WORD_COLUMN_4, |
292 | NULL); |
293 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_4); |
294 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view2), column); |
295 | |
296 | /* gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (smodel), |
297 | (GtkTreeIterCompareFunc)gtk_tree_data_list_compare_func, |
298 | NULL, NULL); |
299 | gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (smodel), |
300 | WORD_COLUMN_1, |
301 | GTK_SORT_DESCENDING);*/ |
302 | |
303 | gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled_window2), child: tree_view2); |
304 | gtk_window_set_default_size (GTK_WINDOW (window2), width: 400, height: 400); |
305 | gtk_widget_show (widget: window2); |
306 | } |
307 | |
308 | /** |
309 | * Third window - GtkTreeModelSort wrapping the GtkTreeModelSort which |
310 | * is wrapping the GtkTreeStore. |
311 | */ |
312 | |
313 | if (ssmodel) |
314 | { |
315 | window3 = gtk_window_new (); |
316 | gtk_window_set_title (GTK_WINDOW (window3), |
317 | title: "Words, words, words - Window 3" ); |
318 | g_signal_connect (window3, "destroy" , G_CALLBACK (quit_cb), &done); |
319 | vbox3 = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 8); |
320 | gtk_box_append (GTK_BOX (vbox3), |
321 | child: gtk_label_new (str: "Jonathan and Kristian's list of words.\n\nA GtkTreeModelSort wrapping the GtkTreeModelSort of window 2" )); |
322 | gtk_window_set_child (GTK_WINDOW (window3), child: vbox3); |
323 | |
324 | scrolled_window3 = gtk_scrolled_window_new (); |
325 | gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (scrolled_window3), TRUE); |
326 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window3), |
327 | hscrollbar_policy: GTK_POLICY_AUTOMATIC, |
328 | vscrollbar_policy: GTK_POLICY_AUTOMATIC); |
329 | gtk_widget_set_vexpand (widget: scrolled_window3, TRUE); |
330 | gtk_box_append (GTK_BOX (vbox3), child: scrolled_window3); |
331 | |
332 | |
333 | tree_view3 = gtk_tree_view_new_with_model (model: ssmodel); |
334 | |
335 | renderer = gtk_cell_renderer_text_new (); |
336 | column = gtk_tree_view_column_new_with_attributes (title: "First Word" , cell: renderer, |
337 | "text" , WORD_COLUMN_1, |
338 | NULL); |
339 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view3), column); |
340 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_1); |
341 | |
342 | renderer = gtk_cell_renderer_text_new (); |
343 | column = gtk_tree_view_column_new_with_attributes (title: "Second Word" , cell: renderer, |
344 | "text" , WORD_COLUMN_2, |
345 | NULL); |
346 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_2); |
347 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view3), column); |
348 | |
349 | renderer = gtk_cell_renderer_text_new (); |
350 | column = gtk_tree_view_column_new_with_attributes (title: "Third Word" , cell: renderer, |
351 | "text" , WORD_COLUMN_3, |
352 | NULL); |
353 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_3); |
354 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view3), column); |
355 | |
356 | renderer = gtk_cell_renderer_text_new (); |
357 | column = gtk_tree_view_column_new_with_attributes (title: "Fourth Word" , cell: renderer, |
358 | "text" , WORD_COLUMN_4, |
359 | NULL); |
360 | gtk_tree_view_column_set_sort_column_id (tree_column: column, sort_column_id: WORD_COLUMN_4); |
361 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view3), column); |
362 | |
363 | /* gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (ssmodel), |
364 | (GtkTreeIterCompareFunc)gtk_tree_data_list_compare_func, |
365 | NULL, NULL); |
366 | gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (ssmodel), |
367 | WORD_COLUMN_1, |
368 | GTK_SORT_ASCENDING);*/ |
369 | |
370 | gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scrolled_window3), child: tree_view3); |
371 | gtk_window_set_default_size (GTK_WINDOW (window3), width: 400, height: 400); |
372 | gtk_widget_show (widget: window3); |
373 | } |
374 | |
375 | for (i = 0; data[i].word_1 != NULL; i++) |
376 | { |
377 | int k; |
378 | |
379 | gtk_tree_store_prepend (GTK_TREE_STORE (model), iter: &iter, NULL); |
380 | gtk_tree_store_set (GTK_TREE_STORE (model), iter: &iter, |
381 | WORD_COLUMN_1, data[i].word_1, |
382 | WORD_COLUMN_2, data[i].word_2, |
383 | WORD_COLUMN_3, data[i].word_3, |
384 | WORD_COLUMN_4, data[i].word_4, |
385 | -1); |
386 | for (k = 0; childdata[k].word_1 != NULL; k++) |
387 | { |
388 | GtkTreeIter child_iter; |
389 | |
390 | gtk_tree_store_append (GTK_TREE_STORE (model), iter: &child_iter, parent: &iter); |
391 | gtk_tree_store_set (GTK_TREE_STORE (model), iter: &child_iter, |
392 | WORD_COLUMN_1, childdata[k].word_1, |
393 | WORD_COLUMN_2, childdata[k].word_2, |
394 | WORD_COLUMN_3, childdata[k].word_3, |
395 | WORD_COLUMN_4, childdata[k].word_4, |
396 | -1); |
397 | } |
398 | } |
399 | |
400 | while (!done) |
401 | g_main_context_iteration (NULL, TRUE); |
402 | |
403 | return 0; |
404 | } |
405 | |