PageRenderTime 12ms CodeModel.GetById 6ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wrappers/gtk/examples/gtk-demo/stock_browser.e

http://github.com/tybor/Liberty
Specman e | 573 lines | 21 code | 109 blank | 443 comment | 2 complexity | db672d2a0e96312ce7ceefc981d1916b MD5 | raw file
  1indexing
  2	description: "."
  3	copyright: "[
  4					Copyright (C) 2006 Paolo Redaelli, GTK+ team
  5					
  6					This library is free software; you can redistribute it and/or
  7					modify it under the terms of the GNU Lesser General Public License
  8					as published by the Free Software Foundation; either version 2.1 of
  9					the License, or (at your option) any later version.
 10					
 11					This library is distributed in the hope that it will be useful, but
 12					WITHOUT ANY WARRANTY; without even the implied warranty of
 13					MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14					Lesser General Public License for more details.
 15
 16					You should have received a copy of the GNU Lesser General Public
 17					License along with this library; if not, write to the Free Software
 18					Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 19					02110-1301 USA
 20			]"
 21
 22class STOCK_BROWSER
 23
 24creation make
 25
 26feature 
 27-- /* Stock Item and Icon Browser
 28--  *
 29--  * This source code for this demo doesn't demonstrate anything
 30--  * particularly useful in applications. The purpose of the "demo" is
 31--  * just to provide a handy place to browse the available stock icons
 32--  * and stock items.
 33--  */
 34
 35-- #include <string.h>
 36
 37-- #include <gtk/gtk.h>
 38
 39-- static GtkWidget *window = NULL;
 40
 41-- typedef struct _StockItemInfo StockItemInfo;
 42-- struct _StockItemInfo
 43-- {
 44--   gchar *id;
 45--   GtkStockItem item;
 46--   GdkPixbuf *small_icon;
 47--   gchar *macro;
 48--   gchar *accel_str;
 49-- };
 50
 51-- /* Make StockItemInfo a boxed type so we can automatically
 52--  * manage memory
 53--  */
 54-- #define STOCK_ITEM_INFO_TYPE stock_item_info_get_type ()
 55
 56-- static void
 57-- stock_item_info_free (StockItemInfo *info)
 58-- {
 59--   g_free (info->id);
 60--   g_free (info->macro);
 61--   g_free (info->accel_str);
 62--   if (info->small_icon)
 63--     g_object_unref (info->small_icon);
 64  
 65--   g_free (info);
 66-- }
 67
 68-- static StockItemInfo*
 69-- stock_item_info_copy (StockItemInfo *src)
 70-- {
 71--   StockItemInfo *info;
 72
 73--   info = g_new (StockItemInfo, 1);
 74--   info->id = g_strdup (src->id);
 75--   info->macro = g_strdup (src->macro);
 76--   info->accel_str = g_strdup (src->accel_str);
 77  
 78--   info->item = src->item;
 79
 80--   info->small_icon = src->small_icon;
 81--   if (info->small_icon)
 82--     g_object_ref (info->small_icon);
 83
 84--   return info;
 85-- }
 86
 87-- static GType
 88-- stock_item_info_get_type (void)
 89-- {
 90--   static GType our_type = 0;
 91  
 92--   if (our_type == 0)
 93--     our_type = g_boxed_type_register_static ("StockItemInfo",
 94--                                              (GBoxedCopyFunc) stock_item_info_copy,
 95--                                              (GBoxedFreeFunc) stock_item_info_free);
 96
 97--   return our_type;
 98-- }
 99
100-- typedef struct _StockItemDisplay StockItemDisplay;
101-- struct _StockItemDisplay
102-- {
103--   GtkWidget *type_label;
104--   GtkWidget *macro_label;
105--   GtkWidget *id_label;
106--   GtkWidget *label_accel_label;
107--   GtkWidget *icon_image;
108-- };
109
110-- static gchar*
111-- id_to_macro (const gchar *id)
112-- {
113--   GString *macro = NULL;
114--   const gchar *cp;
115
116--   /* gtk-foo-bar -> GTK_STOCK_FOO_BAR */
117
118--   macro = g_string_new (NULL);
119  
120--   cp = id;
121  
122--   if (strncmp (cp, "gtk-", 4) == 0)
123--     {
124--       g_string_append (macro, "GTK_STOCK_");
125--       cp += 4;
126--     }
127
128--   while (*cp)
129--     {
130--       if (*cp == '-')
131-- 	g_string_append_c (macro, '_');
132--       else if (g_ascii_islower (*cp))
133-- 	g_string_append_c (macro, g_ascii_toupper (*cp));
134--       else
135-- 	g_string_append_c (macro, *cp);
136
137--       cp++;
138--     }
139
140--   return g_string_free (macro, FALSE);
141-- }
142
143-- static GtkTreeModel*
144-- create_model (void)
145-- {
146--   GtkListStore *store;
147--   GSList *ids;
148--   GSList *tmp_list;
149  
150--   store = gtk_list_store_new (2, STOCK_ITEM_INFO_TYPE, G_TYPE_STRING);
151
152--   ids = gtk_stock_list_ids ();
153--   ids = g_slist_sort (ids, (GCompareFunc) strcmp);
154--   tmp_list = ids;
155--   while (tmp_list != NULL)
156--     {
157--       StockItemInfo info;
158--       GtkStockItem item;
159--       GtkTreeIter iter;
160--       GtkIconSet *icon_set;
161		
162--       info.id = tmp_list->data;
163		
164--       if (gtk_stock_lookup (info.id, &item))
165--         {
166--           info.item = item;
167--         }
168--       else
169--         {
170--           info.item.label = NULL;
171--           info.item.stock_id = NULL;
172--           info.item.modifier = 0;
173--           info.item.keyval = 0;
174--           info.item.translation_domain = NULL;
175--         }
176
177--       /* only show icons for stock IDs that have default icons */
178--       icon_set = gtk_icon_factory_lookup_default (info.id);
179--       if (icon_set)
180--         {
181--           GtkIconSize *sizes = NULL;
182--           gint n_sizes = 0;
183--           gint i;
184--           GtkIconSize size;
185
186--           /* See what sizes this stock icon really exists at */
187--           gtk_icon_set_get_sizes (icon_set, &sizes, &n_sizes);
188
189--           /* Use menu size if it exists, otherwise first size found */
190--           size = sizes[0];
191--           i = 0;
192--           while (i < n_sizes)
193--             {
194--               if (sizes[i] == GTK_ICON_SIZE_MENU)
195--                 {
196--                   size = GTK_ICON_SIZE_MENU;
197--                   break;
198--                 }
199--               ++i;
200--             }
201--           g_free (sizes);
202			 
203--           info.small_icon = gtk_widget_render_icon (window, info.id,
204--                                                     size,
205--                                                     NULL);
206			 
207--           if (size != GTK_ICON_SIZE_MENU)
208--             {
209--               /* Make the result the proper size for our thumbnail */
210--               gint w, h;
211--               GdkPixbuf *scaled;
212				  
213--               gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &w, &h);
214				  
215--               scaled = gdk_pixbuf_scale_simple (info.small_icon,
216--                                                 w, h,
217--                                                 GDK_INTERP_BILINEAR);
218
219--               g_object_unref (info.small_icon);
220--               info.small_icon = scaled;
221--             }
222--         }
223--       else
224--         info.small_icon = NULL;
225
226--       if (info.item.keyval != 0)
227--         {
228--           info.accel_str = gtk_accelerator_name (info.item.keyval,
229--                                                  info.item.modifier);
230--         }
231--       else
232--         {
233--           info.accel_str = g_strdup ("");
234--         }
235
236--       info.macro = id_to_macro (info.id);
237		
238--       gtk_list_store_append (store, &iter);
239--       gtk_list_store_set (store, &iter, 0, &info, 1, info.id, -1);
240
241--       g_free (info.macro);
242--       g_free (info.accel_str);
243--       if (info.small_icon)
244--         g_object_unref (info.small_icon);
245		
246--       tmp_list = g_slist_next (tmp_list);
247--     }
248  
249--   g_slist_foreach (ids, (GFunc)g_free, NULL);
250--   g_slist_free (ids);
251
252--   return GTK_TREE_MODEL (store);
253-- }
254
255-- /* Finds the largest size at which the given image stock id is
256--  * available. This would not be useful for a normal application
257--  */
258-- static GtkIconSize
259-- get_largest_size (const char *id)
260-- {
261--   GtkIconSet *set = gtk_icon_factory_lookup_default (id);
262--   GtkIconSize *sizes;
263--   gint n_sizes, i;
264--   GtkIconSize best_size = GTK_ICON_SIZE_INVALID;
265--   gint best_pixels = 0;
266
267--   gtk_icon_set_get_sizes (set, &sizes, &n_sizes);
268
269--   for (i = 0; i < n_sizes; i++)
270--     {
271--       gint width, height;
272		
273--       gtk_icon_size_lookup (sizes[i], &width, &height);
274
275--       if (width * height > best_pixels)
276-- 	{
277-- 	  best_size = sizes[i];
278-- 	  best_pixels = width * height;
279-- 	}
280--     }
281  
282--   g_free (sizes);
283
284--   return best_size;
285-- }
286
287-- static void
288-- selection_changed (GtkTreeSelection *selection)
289-- {
290--   GtkTreeView *treeview;
291--   StockItemDisplay *display;
292--   GtkTreeModel *model;
293--   GtkTreeIter iter;
294  
295--   treeview = gtk_tree_selection_get_tree_view (selection);
296--   display = g_object_get_data (G_OBJECT (treeview), "stock-display");
297
298--   if (gtk_tree_selection_get_selected (selection, &model, &iter))
299--     {
300--       StockItemInfo *info;
301--       gchar *str;
302		
303--       gtk_tree_model_get (model, &iter,
304--                           0, &info,
305--                           -1);
306
307--       if (info->small_icon && info->item.label)
308--         gtk_label_set_text (GTK_LABEL (display->type_label), "Icon and Item");
309--       else if (info->small_icon)
310--         gtk_label_set_text (GTK_LABEL (display->type_label), "Icon Only");
311--       else if (info->item.label)
312--         gtk_label_set_text (GTK_LABEL (display->type_label), "Item Only");
313--       else
314--         gtk_label_set_text (GTK_LABEL (display->type_label), "???????");
315
316--       gtk_label_set_text (GTK_LABEL (display->macro_label), info->macro);
317--       gtk_label_set_text (GTK_LABEL (display->id_label), info->id);
318
319--       if (info->item.label)
320--         {
321--           str = g_strdup_printf ("%s %s", info->item.label, info->accel_str);
322--           gtk_label_set_text_with_mnemonic (GTK_LABEL (display->label_accel_label), str);
323--           g_free (str);
324--         }
325--       else
326--         {
327--           gtk_label_set_text (GTK_LABEL (display->label_accel_label), "");
328--         }
329
330--       if (info->small_icon)
331--         gtk_image_set_from_stock (GTK_IMAGE (display->icon_image), info->id,
332--                                   get_largest_size (info->id));
333--       else
334--         gtk_image_set_from_pixbuf (GTK_IMAGE (display->icon_image), NULL);
335
336--       stock_item_info_free (info);
337--     }
338--   else
339--     {
340--       gtk_label_set_text (GTK_LABEL (display->type_label), "No selected item");
341--       gtk_label_set_text (GTK_LABEL (display->macro_label), "");
342--       gtk_label_set_text (GTK_LABEL (display->id_label), "");
343--       gtk_label_set_text (GTK_LABEL (display->label_accel_label), "");
344--       gtk_image_set_from_pixbuf (GTK_IMAGE (display->icon_image), NULL);
345--     }
346-- }
347
348-- static void
349-- macro_set_func_text (GtkTreeViewColumn *tree_column,
350-- 		     GtkCellRenderer   *cell,
351-- 		     GtkTreeModel      *model,
352-- 		     GtkTreeIter       *iter,
353-- 		     gpointer           data)
354-- {
355--   StockItemInfo *info;
356  
357--   gtk_tree_model_get (model, iter,
358--                       0, &info,
359--                       -1);
360  
361--   g_object_set (GTK_CELL_RENDERER (cell),
362--                 "text", info->macro,
363--                 NULL);
364  
365--   stock_item_info_free (info);
366-- }
367
368-- static void
369-- id_set_func (GtkTreeViewColumn *tree_column,
370--              GtkCellRenderer   *cell,
371--              GtkTreeModel      *model,
372--              GtkTreeIter       *iter,
373--              gpointer           data)
374-- {
375--   StockItemInfo *info;
376  
377--   gtk_tree_model_get (model, iter,
378--                       0, &info,
379--                       -1);
380  
381--   g_object_set (GTK_CELL_RENDERER (cell),
382--                 "text", info->id,
383--                 NULL);
384  
385--   stock_item_info_free (info);
386-- }
387
388-- static void
389-- accel_set_func (GtkTreeViewColumn *tree_column,
390--                 GtkCellRenderer   *cell,
391--                 GtkTreeModel      *model,
392--                 GtkTreeIter       *iter,
393--                 gpointer           data)
394-- {
395--   StockItemInfo *info;
396  
397--   gtk_tree_model_get (model, iter,
398--                       0, &info,
399--                       -1);
400  
401--   g_object_set (GTK_CELL_RENDERER (cell),
402--                 "text", info->accel_str,
403--                 NULL);
404  
405--   stock_item_info_free (info);
406-- }
407
408-- static void
409-- label_set_func (GtkTreeViewColumn *tree_column,
410--                 GtkCellRenderer   *cell,
411--                 GtkTreeModel      *model,
412--                 GtkTreeIter       *iter,
413--                 gpointer           data)
414-- {
415--   StockItemInfo *info;
416  
417--   gtk_tree_model_get (model, iter,
418--                       0, &info,
419--                       -1);
420  
421--   g_object_set (GTK_CELL_RENDERER (cell),
422--                 "text", info->item.label,
423--                 NULL);
424  
425--   stock_item_info_free (info);
426-- }
427
428-- GtkWidget *
429-- do_stock_browser (GtkWidget *do_widget)
430-- {  
431--   if (!window)
432--     {
433--       GtkWidget *frame;
434--       GtkWidget *vbox;
435--       GtkWidget *hbox;
436--       GtkWidget *sw;
437--       GtkWidget *treeview;
438--       GtkWidget *align;
439--       GtkTreeModel *model;
440--       GtkCellRenderer *cell_renderer;
441--       StockItemDisplay *display;
442--       GtkTreeSelection *selection;
443--       GtkTreeViewColumn *column;
444
445--       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
446--       gtk_window_set_screen (GTK_WINDOW (window),
447-- 			     gtk_widget_get_screen (do_widget));
448--       gtk_window_set_title (GTK_WINDOW (window), "Stock Icons and Items");
449--       gtk_window_set_default_size (GTK_WINDOW (window), -1, 500);
450
451--       g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
452--       gtk_container_set_border_width (GTK_CONTAINER (window), 8);
453
454--       hbox = gtk_hbox_new (FALSE, 8);
455--       gtk_container_add (GTK_CONTAINER (window), hbox);
456
457--       sw = gtk_scrolled_window_new (NULL, NULL);
458--       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
459--                                       GTK_POLICY_NEVER,
460--                                       GTK_POLICY_AUTOMATIC);
461--       gtk_box_pack_start (GTK_BOX (hbox), sw, FALSE, FALSE, 0);
462
463--       model = create_model ();
464		
465--       treeview = gtk_tree_view_new_with_model (model);
466
467--       g_object_unref (model);
468
469--       gtk_container_add (GTK_CONTAINER (sw), treeview);
470		
471--       column = gtk_tree_view_column_new ();
472--       gtk_tree_view_column_set_title (column, "Macro");
473
474--       cell_renderer = gtk_cell_renderer_pixbuf_new ();
475--       gtk_tree_view_column_pack_start (column,
476-- 				       cell_renderer,
477-- 				       FALSE);
478--       gtk_tree_view_column_set_attributes (column, cell_renderer,
479-- 					   "stock_id", 1, NULL);
480--       cell_renderer = gtk_cell_renderer_text_new ();
481--       gtk_tree_view_column_pack_start (column,
482-- 				       cell_renderer,
483-- 				       TRUE);
484--       gtk_tree_view_column_set_cell_data_func (column, cell_renderer,
485-- 					       macro_set_func_text, NULL, NULL);
486
487--       gtk_tree_view_append_column (GTK_TREE_VIEW (treeview),
488-- 				   column);
489
490--       cell_renderer = gtk_cell_renderer_text_new ();
491--       gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (treeview),
492--                                                   -1,
493--                                                   "Label",
494--                                                   cell_renderer,
495--                                                   label_set_func,
496--                                                   NULL,
497--                                                   NULL);
498
499--       cell_renderer = gtk_cell_renderer_text_new ();
500--       gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (treeview),
501--                                                   -1,
502--                                                   "Accel",
503--                                                   cell_renderer,
504--                                                   accel_set_func,
505--                                                   NULL,
506--                                                   NULL);
507
508--       cell_renderer = gtk_cell_renderer_text_new ();
509--       gtk_tree_view_insert_column_with_data_func (GTK_TREE_VIEW (treeview),
510--                                                   -1,
511--                                                   "ID",
512--                                                   cell_renderer,
513--                                                   id_set_func,
514--                                                   NULL,
515--                                                   NULL);
516		
517--       align = gtk_alignment_new (0.5, 0.0, 0.0, 0.0);
518--       gtk_box_pack_end (GTK_BOX (hbox), align, FALSE, FALSE, 0);
519		
520--       frame = gtk_frame_new ("Selected Item");
521--       gtk_container_add (GTK_CONTAINER (align), frame);
522
523--       vbox = gtk_vbox_new (FALSE, 8);
524--       gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
525--       gtk_container_add (GTK_CONTAINER (frame), vbox);
526
527--       display = g_new (StockItemDisplay, 1);
528--       g_object_set_data_full (G_OBJECT (treeview),
529--                               "stock-display",
530--                               display,
531--                               g_free); /* free display with treeview */
532		
533--       display->type_label = gtk_label_new (NULL);
534--       display->macro_label = gtk_label_new (NULL);
535--       display->id_label = gtk_label_new (NULL);
536--       display->label_accel_label = gtk_label_new (NULL);
537--       display->icon_image = gtk_image_new_from_pixbuf (NULL); /* empty image */
538
539--       gtk_box_pack_start (GTK_BOX (vbox), display->type_label,
540--                           FALSE, FALSE, 0);
541
542--       gtk_box_pack_start (GTK_BOX (vbox), display->icon_image,
543--                           FALSE, FALSE, 0);
544		
545--       gtk_box_pack_start (GTK_BOX (vbox), display->label_accel_label,
546--                           FALSE, FALSE, 0);
547--       gtk_box_pack_start (GTK_BOX (vbox), display->macro_label,
548--                           FALSE, FALSE, 0);
549--       gtk_box_pack_start (GTK_BOX (vbox), display->id_label,
550--                           FALSE, FALSE, 0);
551
552--       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
553--       gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
554		
555--       g_signal_connect (selection,
556-- 			"changed",
557-- 			G_CALLBACK (selection_changed),
558-- 			NULL);
559--     }
560
561--   if (!GTK_WIDGET_VISIBLE (window))
562--     {
563--       gtk_widget_show_all (window);
564--     }
565--   else
566--     {	 
567--       gtk_widget_destroy (window);
568--       window = NULL;
569--     }
570
571--   return window;
572-- }
573end