testtreesort.c 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* testtreesort.c
 * Copyright (C) 2001 Red Hat, Inc
 * Author: Jonathan Blandford
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18
 */

19
#include "config.h"
20 21 22 23 24 25 26 27 28 29
#include <gtk/gtk.h>


typedef struct _ListSort ListSort;
struct _ListSort
{
  const gchar *word_1;
  const gchar *word_2;
  const gchar *word_3;
  const gchar *word_4;
30
  gint number_1;
31 32 33 34
};

static ListSort data[] =
{
35 36 37 38 39 40 41 42 43 44 45 46
  { "Apples", "Transmogrify long word to demonstrate weirdness", "Exculpatory", "Gesundheit", 30 },
  { "Oranges", "Wicker", "Adamantine", "Convivial", 10 },
  { "Bovine Spongiform Encephilopathy", "Sleazebucket", "Mountaineer", "Pander", 40 },
  { "Foot and Mouth", "Lampshade", "Skim Milk\nFull Milk", "Viewless", 20 },
  { "Blood,\nsweat,\ntears", "The Man", "Horses", "Muckety-Muck", 435 },
  { "Rare Steak", "Siam", "Watchdog", "Xantippe" , 99999 },
  { "SIGINT", "Rabbit Breath", "Alligator", "Bloodstained", 4123 },
  { "Google", "Chrysanthemums", "Hobnob", "Leapfrog", 1 },
  { "Technology fibre optic", "Turtle", "Academe", "Lonely", 3 },
  { "Freon", "Harpes", "Quidditch", "Reagan", 6},
  { "Transposition", "Fruit Basket", "Monkey Wort", "Glogg", 54 },
  { "Fern", "Glasnost and Perestroika", "Latitude", "Bomberman!!!", 2 },
47 48
  {NULL, }
};
49 50 51

static ListSort childdata[] =
{
52 53 54 55 56
  { "Heineken", "Nederland", "Wanda de vis", "Electronische post", 2},
  { "Hottentottententententoonstelling", "Rotterdam", "Ionentransport", "Palm", 45},
  { "Fruitvlieg", "Eigenfrequentie", "Supernoodles", "Ramen", 2002},
  { "Gereedschapskist", "Stelsel van lineaire vergelijkingen", "Tulpen", "Badlaken", 1311},
  { "Stereoinstallatie", "Rood tapijt", "Het periodieke systeem der elementen", "Laaste woord", 200},
57 58
  {NULL, }
};
59 60 61 62
  

enum
{
63
  WORD_COLUMN_1 = 0,
64 65 66
  WORD_COLUMN_2,
  WORD_COLUMN_3,
  WORD_COLUMN_4,
67
  NUMBER_COLUMN_1,
68 69 70
  NUM_COLUMNS
};

71 72 73 74 75 76 77 78 79 80 81 82
gboolean
select_func (GtkTreeSelection  *selection,
	     GtkTreeModel      *model,
	     GtkTreePath       *path,
	     gboolean           path_currently_selected,
	     gpointer           data)
{
  if (gtk_tree_path_get_depth (path) > 1)
    return TRUE;
  return FALSE;
}

83 84 85 86 87 88 89 90 91 92 93 94 95
static void
switch_search_method (GtkWidget *button,
		      gpointer   tree_view)
{
  if (!gtk_tree_view_get_search_entry (GTK_TREE_VIEW (tree_view)))
    {
      gpointer data = g_object_get_data (tree_view, "my-search-entry");
      gtk_tree_view_set_search_entry (GTK_TREE_VIEW (tree_view), GTK_ENTRY (data));
    }
  else
    gtk_tree_view_set_search_entry (GTK_TREE_VIEW (tree_view), NULL);
}

96 97 98 99 100 101 102
int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *scrolled_window;
  GtkWidget *tree_view;
103
  GtkTreeStore *model;
104
  GtkTreeModel *smodel = NULL;
105
  GtkTreeModel *ssmodel = NULL;
106 107 108
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkTreeIter iter;
109
  gint i;
110

111
  GtkWidget *entry, *button;
112 113 114
  GtkWidget *window2, *vbox2, *scrolled_window2, *tree_view2;
  GtkWidget *window3, *vbox3, *scrolled_window3, *tree_view3;

115 116
  gtk_init (&argc, &argv);

117 118 119 120
  /**
   * First window - Just a GtkTreeStore
   */

121
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
122
  gtk_window_set_title (GTK_WINDOW (window), "Words, words, words - Window 1");
Manish Singh's avatar
Manish Singh committed
123
  g_signal_connect (window, "destroy", gtk_main_quit, NULL);
124
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
125
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
126
  gtk_box_pack_start (GTK_BOX (vbox), gtk_label_new ("Jonathan and Kristian's list of cool words. (And Anders' cool list of numbers) \n\nThis is just a GtkTreeStore"), FALSE, FALSE, 0);
127 128
  gtk_container_add (GTK_CONTAINER (window), vbox);

129 130 131 132 133 134
  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);

  button = gtk_button_new_with_label ("Switch search method");
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

135 136 137 138 139
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_ETCHED_IN);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);

140
  model = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
141 142

/*
143 144
  smodel = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (model));
  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 149 150 151 152 153 154

  gtk_tree_view_set_search_entry (GTK_TREE_VIEW (tree_view), GTK_ENTRY (entry));
  g_object_set_data (G_OBJECT (tree_view), "my-search-entry", entry);
  g_signal_connect (button, "clicked",
		    G_CALLBACK (switch_search_method), tree_view);

 /* gtk_tree_selection_set_select_function (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)), select_func, NULL, NULL);*/

155 156 157 158
  /* 12 iters now, 12 later... */
  for (i = 0; data[i].word_1 != NULL; i++)
    {
      gint k;
Jonathan Blandford's avatar
Jonathan Blandford committed
159 160 161
      GtkTreeIter child_iter;


162 163 164 165 166 167
      gtk_tree_store_prepend (GTK_TREE_STORE (model), &iter, NULL);
      gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
			  WORD_COLUMN_1, data[i].word_1,
			  WORD_COLUMN_2, data[i].word_2,
			  WORD_COLUMN_3, data[i].word_3,
			  WORD_COLUMN_4, data[i].word_4,
168
			  NUMBER_COLUMN_1, data[i].number_1,
169
			  -1);
Jonathan Blandford's avatar
Jonathan Blandford committed
170 171 172 173 174 175 176

      gtk_tree_store_append (GTK_TREE_STORE (model), &child_iter, &iter);
      gtk_tree_store_set (GTK_TREE_STORE (model), &child_iter,
			  WORD_COLUMN_1, data[i].word_1,
			  WORD_COLUMN_2, data[i].word_2,
			  WORD_COLUMN_3, data[i].word_3,
			  WORD_COLUMN_4, data[i].word_4,
177
			  NUMBER_COLUMN_1, data[i].number_1,
Jonathan Blandford's avatar
Jonathan Blandford committed
178
			  -1);
179

180 181 182 183 184 185 186 187
      for (k = 0; childdata[k].word_1 != NULL; k++)
	{
	  gtk_tree_store_append (GTK_TREE_STORE (model), &child_iter, &iter);
	  gtk_tree_store_set (GTK_TREE_STORE (model), &child_iter,
			      WORD_COLUMN_1, childdata[k].word_1,
			      WORD_COLUMN_2, childdata[k].word_2,
			      WORD_COLUMN_3, childdata[k].word_3,
			      WORD_COLUMN_4, childdata[k].word_4,
188
			      NUMBER_COLUMN_1, childdata[k].number_1,
189
			      -1);
190

191
	}
192

193 194
    }
  
195 196
  smodel = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (model));
  ssmodel = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (smodel));
Manish Singh's avatar
Manish Singh committed
197
  g_object_unref (model);
198

199 200 201 202
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view), TRUE);

  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("First Word", renderer,
203
						     "text", WORD_COLUMN_1,
204 205
						     NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
206 207
  gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_1);

208 209 210 211 212 213 214 215 216 217 218
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Second Word", renderer,
						     "text", WORD_COLUMN_2,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_2);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);

  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Third Word", renderer,
						     "text", WORD_COLUMN_3,
						     NULL);
Jonathan Blandford's avatar
Jonathan Blandford committed
219
  gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_3);
220 221 222 223 224 225 226
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);

  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Fourth Word", renderer,
						     "text", WORD_COLUMN_4,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_4);
227 228 229 230 231 232 233
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
  
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("First Number", renderer,
						     "text", NUMBER_COLUMN_1,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, NUMBER_COLUMN_1);
234
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
235

236
  /*  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (smodel),
237
					WORD_COLUMN_1,
238
					GTK_SORT_ASCENDING);*/
239

240
  gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
241
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
242
  gtk_widget_show_all (window);
243 244 245 246 247

  /**
   * Second window - GtkTreeModelSort wrapping the GtkTreeStore
   */

248 249 250 251 252
  if (smodel)
    {
      window2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window2), 
			    "Words, words, words - window 2");
Manish Singh's avatar
Manish Singh committed
253
      g_signal_connect (window2, "destroy", gtk_main_quit, NULL);
254
      vbox2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
      gtk_container_set_border_width (GTK_CONTAINER (vbox2), 8);
      gtk_box_pack_start (GTK_BOX (vbox2), 
			  gtk_label_new ("Jonathan and Kristian's list of words.\n\nA GtkTreeModelSort wrapping the GtkTreeStore of window 1"),
			  FALSE, FALSE, 0);
      gtk_container_add (GTK_CONTAINER (window2), vbox2);
      
      scrolled_window2 = gtk_scrolled_window_new (NULL, NULL);
      gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window2),
					   GTK_SHADOW_ETCHED_IN);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window2),
				      GTK_POLICY_AUTOMATIC,
				      GTK_POLICY_AUTOMATIC);
      gtk_box_pack_start (GTK_BOX (vbox2), scrolled_window2, TRUE, TRUE, 0);
      
      
      tree_view2 = gtk_tree_view_new_with_model (smodel);
      gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view2), TRUE);
      
      
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes ("First Word", renderer,
							 "text", WORD_COLUMN_1,
							 NULL);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view2), column);
      gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_1);
      
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes ("Second Word", renderer,
							 "text", WORD_COLUMN_2,
							 NULL);
      gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_2);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view2), column);
      
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes ("Third Word", renderer,
							 "text", WORD_COLUMN_3,
							 NULL);
      gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_3);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view2), column);
      
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes ("Fourth Word", renderer,
							 "text", WORD_COLUMN_4,
							 NULL);
      gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_4);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view2), column);
      
302
      /*      gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (smodel),
303 304 305 306
					       (GtkTreeIterCompareFunc)gtk_tree_data_list_compare_func,
					       NULL, NULL);
      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (smodel),
					    WORD_COLUMN_1,
307 308
					    GTK_SORT_DESCENDING);*/
      
309 310 311 312 313 314
      
      gtk_container_add (GTK_CONTAINER (scrolled_window2), tree_view2);
      gtk_window_set_default_size (GTK_WINDOW (window2), 400, 400);
      gtk_widget_show_all (window2);
    }
  
315 316 317 318
  /**
   * Third window - GtkTreeModelSort wrapping the GtkTreeModelSort which
   * is wrapping the GtkTreeStore.
   */
319 320 321 322 323 324
  
  if (ssmodel)
    {
      window3 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (window3), 
			    "Words, words, words - Window 3");
Manish Singh's avatar
Manish Singh committed
325
      g_signal_connect (window3, "destroy", gtk_main_quit, NULL);
326
      vbox3 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
      gtk_container_set_border_width (GTK_CONTAINER (vbox3), 8);
      gtk_box_pack_start (GTK_BOX (vbox3), 
			  gtk_label_new ("Jonathan and Kristian's list of words.\n\nA GtkTreeModelSort wrapping the GtkTreeModelSort of window 2"),
			  FALSE, FALSE, 0);
      gtk_container_add (GTK_CONTAINER (window3), vbox3);
      
      scrolled_window3 = gtk_scrolled_window_new (NULL, NULL);
      gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window3),
					   GTK_SHADOW_ETCHED_IN);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window3),
				      GTK_POLICY_AUTOMATIC,
				      GTK_POLICY_AUTOMATIC);
      gtk_box_pack_start (GTK_BOX (vbox3), scrolled_window3, TRUE, TRUE, 0);
      
      
      tree_view3 = gtk_tree_view_new_with_model (ssmodel);
      gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (tree_view3), TRUE);
      
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes ("First Word", renderer,
							 "text", WORD_COLUMN_1,
							 NULL);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view3), column);
      gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_1);
      
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes ("Second Word", renderer,
							 "text", WORD_COLUMN_2,
							 NULL);
      gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_2);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view3), column);
      
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes ("Third Word", renderer,
							 "text", WORD_COLUMN_3,
							 NULL);
      gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_3);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view3), column);
      
      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes ("Fourth Word", renderer,
							 "text", WORD_COLUMN_4,
							 NULL);
      gtk_tree_view_column_set_sort_column_id (column, WORD_COLUMN_4);
      gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view3), column);
      
373
      /*      gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (ssmodel),
374 375 376 377
					       (GtkTreeIterCompareFunc)gtk_tree_data_list_compare_func,
					       NULL, NULL);
      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (ssmodel),
					    WORD_COLUMN_1,
378
					    GTK_SORT_ASCENDING);*/
379 380 381 382 383
      
      gtk_container_add (GTK_CONTAINER (scrolled_window3), tree_view3);
      gtk_window_set_default_size (GTK_WINDOW (window3), 400, 400);
      gtk_widget_show_all (window3);
    }
384

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
  for (i = 0; data[i].word_1 != NULL; i++)
    {
      gint k;
      
      gtk_tree_store_prepend (GTK_TREE_STORE (model), &iter, NULL);
      gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
			  WORD_COLUMN_1, data[i].word_1,
			  WORD_COLUMN_2, data[i].word_2,
			  WORD_COLUMN_3, data[i].word_3,
			  WORD_COLUMN_4, data[i].word_4,
			  -1);
      for (k = 0; childdata[k].word_1 != NULL; k++)
	{
	  GtkTreeIter child_iter;
	  
	  gtk_tree_store_append (GTK_TREE_STORE (model), &child_iter, &iter);
	  gtk_tree_store_set (GTK_TREE_STORE (model), &child_iter,
			      WORD_COLUMN_1, childdata[k].word_1,
			      WORD_COLUMN_2, childdata[k].word_2,
			      WORD_COLUMN_3, childdata[k].word_3,
			      WORD_COLUMN_4, childdata[k].word_4,
			      -1);
	}
    }
409

410
  gtk_main ();
411
  
412 413
  return 0;
}