]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/toolbar-editor/egg-toolbar-editor.c
cd5f38f2c48cf706586330696cf37b4d4e18e62f
[evince.git] / cut-n-paste / toolbar-editor / egg-toolbar-editor.c
1 /*
2  *  Copyright (C) 2003 Marco Pesenti Gritti
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program 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.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  *  $Id$
19  */
20
21 #include "config.h"
22
23 #include "egg-toolbar-editor.h"
24 #include "egg-editable-toolbar.h"
25
26 #include <string.h>
27 #include <libxml/tree.h>
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30
31 static const GtkTargetEntry dest_drag_types[] = {
32   {EGG_TOOLBAR_ITEM_TYPE, GTK_TARGET_SAME_APP, 0},
33 };
34
35 static const GtkTargetEntry source_drag_types[] = {
36   {EGG_TOOLBAR_ITEM_TYPE, GTK_TARGET_SAME_APP, 0},
37 };
38
39
40 static void egg_toolbar_editor_finalize         (GObject *object);
41 static void update_editor_sheet                 (EggToolbarEditor *editor);
42
43 enum
44 {
45   PROP_0,
46   PROP_UI_MANAGER,
47   PROP_TOOLBARS_MODEL
48 };
49
50 #define EGG_TOOLBAR_EDITOR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EGG_TYPE_TOOLBAR_EDITOR, EggToolbarEditorPrivate))
51
52 struct EggToolbarEditorPrivate
53 {
54   GtkUIManager *manager;
55   EggToolbarsModel *model;
56
57   GtkWidget *table;
58   GtkWidget *scrolled_window;
59   GList     *actions_list;
60   GList     *factory_list;
61 };
62
63 G_DEFINE_TYPE (EggToolbarEditor, egg_toolbar_editor, GTK_TYPE_VBOX);
64
65 static gint
66 compare_items (gconstpointer a,
67                gconstpointer b)
68 {
69   const GtkWidget *item1 = a;
70   const GtkWidget *item2 = b;
71
72   char *key1 = g_object_get_data (G_OBJECT (item1),
73                                   "egg-collate-key");
74   char *key2 = g_object_get_data (G_OBJECT (item2),
75                                   "egg-collate-key");
76   
77   return strcmp (key1, key2);
78 }
79
80 static GtkAction *
81 find_action (EggToolbarEditor *t,
82              const char       *name)
83 {
84   GList *l;
85   GtkAction *action = NULL;
86
87   l = gtk_ui_manager_get_action_groups (t->priv->manager);
88
89   g_return_val_if_fail (EGG_IS_TOOLBAR_EDITOR (t), NULL);
90   g_return_val_if_fail (name != NULL, NULL);
91
92   for (; l != NULL; l = l->next)
93     {
94       GtkAction *tmp;
95
96       tmp = gtk_action_group_get_action (GTK_ACTION_GROUP (l->data), name);
97       if (tmp)
98         action = tmp;
99     }
100
101   return action;
102 }
103
104 static void
105 egg_toolbar_editor_set_ui_manager (EggToolbarEditor *t,
106                                    GtkUIManager     *manager)
107 {
108   g_return_if_fail (GTK_IS_UI_MANAGER (manager));
109
110   t->priv->manager = g_object_ref (manager);
111 }
112
113 static void
114 item_added_or_removed_cb (EggToolbarsModel   *model,
115                           int                 tpos,
116                           int                 ipos,
117                           EggToolbarEditor   *editor)
118 {
119   update_editor_sheet (editor);
120 }
121
122 static void
123 toolbar_removed_cb (EggToolbarsModel   *model,
124                     int                 position,
125                     EggToolbarEditor   *editor)
126 {
127   update_editor_sheet (editor);
128 }
129
130 static void
131 egg_toolbar_editor_set_model (EggToolbarEditor *t,
132                               EggToolbarsModel *model)
133 {
134   g_return_if_fail (EGG_IS_TOOLBAR_EDITOR (t));
135
136   t->priv->model = g_object_ref (model);
137   
138   update_editor_sheet (t);
139
140   g_signal_connect_object (model, "item_added",
141                            G_CALLBACK (item_added_or_removed_cb), t, 0);
142   g_signal_connect_object (model, "item_removed",
143                            G_CALLBACK (item_added_or_removed_cb), t, 0);
144   g_signal_connect_object (model, "toolbar_removed",
145                            G_CALLBACK (toolbar_removed_cb), t, 0);
146 }
147
148 static void
149 egg_toolbar_editor_set_property (GObject      *object,
150                                  guint         prop_id,
151                                  const GValue *value,
152                                  GParamSpec   *pspec)
153 {
154   EggToolbarEditor *t = EGG_TOOLBAR_EDITOR (object);
155
156   switch (prop_id)
157     {
158     case PROP_UI_MANAGER:
159       egg_toolbar_editor_set_ui_manager (t, g_value_get_object (value));
160       break;
161     case PROP_TOOLBARS_MODEL:
162       egg_toolbar_editor_set_model (t, g_value_get_object (value));
163       break;
164     }
165 }
166
167 static void
168 egg_toolbar_editor_get_property (GObject    *object,
169                                  guint       prop_id,
170                                  GValue     *value,
171                                  GParamSpec *pspec)
172 {
173   EggToolbarEditor *t = EGG_TOOLBAR_EDITOR (object);
174
175   switch (prop_id)
176     {
177     case PROP_UI_MANAGER:
178       g_value_set_object (value, t->priv->manager);
179       break;
180     case PROP_TOOLBARS_MODEL:
181       g_value_set_object (value, t->priv->model);
182       break;
183     }
184 }
185
186 static void
187 egg_toolbar_editor_class_init (EggToolbarEditorClass *klass)
188 {
189   GObjectClass *object_class = G_OBJECT_CLASS (klass);
190
191   object_class->finalize = egg_toolbar_editor_finalize;
192   object_class->set_property = egg_toolbar_editor_set_property;
193   object_class->get_property = egg_toolbar_editor_get_property;
194
195   g_object_class_install_property (object_class,
196                                    PROP_UI_MANAGER,
197                                    g_param_spec_object ("ui-manager",
198                                                         "UI-Manager",
199                                                         "UI Manager",
200                                                         GTK_TYPE_UI_MANAGER,
201                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB |
202                                                         G_PARAM_CONSTRUCT_ONLY));
203  g_object_class_install_property (object_class,
204                                   PROP_TOOLBARS_MODEL,
205                                   g_param_spec_object ("model",
206                                                        "Model",
207                                                        "Toolbars Model",
208                                                        EGG_TYPE_TOOLBARS_MODEL,
209                                                        G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB |
210                                                        G_PARAM_CONSTRUCT_ONLY));
211
212   g_type_class_add_private (object_class, sizeof (EggToolbarEditorPrivate));
213 }
214
215 static void
216 egg_toolbar_editor_finalize (GObject *object)
217 {
218   EggToolbarEditor *editor = EGG_TOOLBAR_EDITOR (object);
219
220   if (editor->priv->manager)
221     {
222       g_object_unref (editor->priv->manager);
223     }
224
225   if (editor->priv->model)
226     {
227       g_object_unref (editor->priv->model);
228     }
229
230   g_list_free (editor->priv->actions_list);
231   g_list_free (editor->priv->factory_list);
232
233   G_OBJECT_CLASS (egg_toolbar_editor_parent_class)->finalize (object);
234 }
235
236 GtkWidget *
237 egg_toolbar_editor_new (GtkUIManager *manager,
238                         EggToolbarsModel *model)
239 {
240   return GTK_WIDGET (g_object_new (EGG_TYPE_TOOLBAR_EDITOR,
241                                    "ui-manager", manager,
242                                    "model", model,
243                                    NULL));
244 }
245
246 static void
247 drag_begin_cb (GtkWidget          *widget,
248                GdkDragContext     *context)
249 {
250   gtk_widget_hide (widget);
251 }
252
253 static void
254 drag_end_cb (GtkWidget          *widget,
255              GdkDragContext     *context)
256 {
257   gtk_widget_show (widget);
258 }
259
260 static void
261 drag_data_get_cb (GtkWidget          *widget,
262                   GdkDragContext     *context,
263                   GtkSelectionData   *selection_data,
264                   guint               info,
265                   guint32             time,
266                   EggToolbarEditor   *editor)
267 {
268   const char *target;
269
270   target = g_object_get_data (G_OBJECT (widget), "egg-item-name");
271   g_return_if_fail (target != NULL);
272   
273   gtk_selection_data_set (selection_data, selection_data->target, 8,
274                           (const guchar *) target, strlen (target));
275 }
276
277 static gchar *
278 elide_underscores (const gchar *original)
279 {
280   gchar *q, *result;
281   const gchar *p;
282   gboolean last_underscore;
283
284   q = result = g_malloc (strlen (original) + 1);
285   last_underscore = FALSE;
286
287   for (p = original; *p; p++)
288     {
289       if (!last_underscore && *p == '_')
290         last_underscore = TRUE;
291       else
292         {
293           last_underscore = FALSE;
294           *q++ = *p;
295         }
296     }
297
298   *q = '\0';
299
300   return result;
301 }
302
303 static void
304 set_drag_cursor (GtkWidget *widget)
305 {
306   GdkCursor *cursor;
307   GdkScreen *screen;
308   
309   screen = gtk_widget_get_screen (widget);
310   
311   cursor = gdk_cursor_new_for_display (gdk_screen_get_display (screen),
312                                        GDK_HAND2);
313   gdk_window_set_cursor (widget->window, cursor);
314   gdk_cursor_unref (cursor);
315 }
316
317 static void
318 event_box_realize_cb (GtkWidget *widget, GtkImage *icon)
319 {
320   GtkImageType type;
321
322   set_drag_cursor (widget);
323
324   type = gtk_image_get_storage_type (icon);
325   if (type == GTK_IMAGE_STOCK)
326     {
327       gchar *stock_id;
328       GdkPixbuf *pixbuf;
329
330       gtk_image_get_stock (icon, &stock_id, NULL);
331       pixbuf = gtk_widget_render_icon (widget, stock_id,
332                                        GTK_ICON_SIZE_LARGE_TOOLBAR, NULL);
333       gtk_drag_source_set_icon_pixbuf (widget, pixbuf);
334       g_object_unref (pixbuf);
335     }
336   else if (type == GTK_IMAGE_ICON_NAME)
337     {
338       const gchar *icon_name;
339       GdkScreen *screen;
340       GtkIconTheme *icon_theme;
341       GtkSettings *settings;
342       gint width, height;
343       GdkPixbuf *pixbuf;
344
345       gtk_image_get_icon_name (icon, &icon_name, NULL);
346       screen = gtk_widget_get_screen (widget);
347       icon_theme = gtk_icon_theme_get_for_screen (screen);
348       settings = gtk_settings_get_for_screen (screen);
349
350       if (!gtk_icon_size_lookup_for_settings (settings,
351                                               GTK_ICON_SIZE_LARGE_TOOLBAR,
352                                               &width, &height))
353         {
354           width = height = 24;
355         }
356
357       pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
358                                          MIN (width, height), 0, NULL);
359       if (G_UNLIKELY (!pixbuf))
360         return;
361
362       gtk_drag_source_set_icon_pixbuf (widget, pixbuf);
363       g_object_unref (pixbuf);
364
365     }
366   else if (type == GTK_IMAGE_PIXBUF)
367     {
368       GdkPixbuf *pixbuf = gtk_image_get_pixbuf (icon);
369       gtk_drag_source_set_icon_pixbuf (widget, pixbuf);
370     }
371 }
372
373 static GtkWidget *
374 editor_create_item (EggToolbarEditor *editor,
375                     GtkImage         *icon,
376                     const char       *label_text,
377                     GdkDragAction     action)
378 {
379   GtkWidget *event_box;
380   GtkWidget *vbox;
381   GtkWidget *label;
382   gchar *label_no_mnemonic = NULL;
383
384   event_box = gtk_event_box_new ();
385   gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
386   gtk_widget_show (event_box);
387   gtk_drag_source_set (event_box,
388                        GDK_BUTTON1_MASK,
389                        source_drag_types, G_N_ELEMENTS (source_drag_types), action);
390   g_signal_connect (event_box, "drag_data_get",
391                     G_CALLBACK (drag_data_get_cb), editor);
392   g_signal_connect_after (event_box, "realize",
393                           G_CALLBACK (event_box_realize_cb), icon);
394
395   if (action == GDK_ACTION_MOVE)
396     {
397       g_signal_connect (event_box, "drag_begin",
398                         G_CALLBACK (drag_begin_cb), NULL);
399       g_signal_connect (event_box, "drag_end",
400                         G_CALLBACK (drag_end_cb), NULL);
401     }
402
403   vbox = gtk_vbox_new (0, FALSE);
404   gtk_widget_show (vbox);
405   gtk_container_add (GTK_CONTAINER (event_box), vbox);
406
407   gtk_widget_show (GTK_WIDGET (icon));
408   gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (icon), FALSE, TRUE, 0);
409   label_no_mnemonic = elide_underscores (label_text);
410   label = gtk_label_new (label_no_mnemonic);
411   g_free (label_no_mnemonic);
412   gtk_widget_show (label);
413   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
414
415   return event_box;
416 }
417
418 static GtkWidget *
419 editor_create_item_from_name (EggToolbarEditor *editor,
420                               const char *      name,
421                               GdkDragAction     drag_action)
422 {
423   GtkWidget *item;
424   const char *item_name;
425   char *short_label;
426   const char *collate_key;
427   
428   if (strcmp (name, "_separator") == 0)
429     {
430       GtkWidget *icon;
431       
432       icon = _egg_editable_toolbar_new_separator_image ();
433       short_label = _("Separator");
434       item_name = g_strdup (name);
435       collate_key = g_utf8_collate_key (short_label, -1);
436       item = editor_create_item (editor, GTK_IMAGE (icon), 
437                                  short_label, drag_action);
438     }
439   else
440     {
441       GtkAction *action;
442       GtkWidget *icon;
443       char *stock_id, *icon_name = NULL;
444       
445       action = find_action (editor, name);
446       g_return_val_if_fail (action != NULL, NULL);
447
448       g_object_get (action,
449                     "icon-name", &icon_name,
450                     "stock-id", &stock_id,
451                     "short-label", &short_label,
452                     NULL);
453
454       /* This is a workaround to catch named icons. */
455       if (icon_name)
456         icon = gtk_image_new_from_icon_name (icon_name,
457                                              GTK_ICON_SIZE_LARGE_TOOLBAR);
458       else
459         icon = gtk_image_new_from_stock (stock_id ? stock_id : GTK_STOCK_DND,
460                                          GTK_ICON_SIZE_LARGE_TOOLBAR);
461
462       item_name = g_strdup (name);
463       collate_key = g_utf8_collate_key (short_label, -1);
464       item = editor_create_item (editor, GTK_IMAGE (icon),
465                                  short_label, drag_action);
466
467       g_free (short_label);
468       g_free (stock_id);
469       g_free (icon_name);
470     }
471   
472   g_object_set_data_full (G_OBJECT (item), "egg-collate-key",
473                           (gpointer) collate_key, g_free);
474   g_object_set_data_full (G_OBJECT (item), "egg-item-name",
475                           (gpointer) item_name, g_free);
476   
477   return item;
478 }
479
480 static gint
481 append_table (GtkTable *table, GList *items, gint y, gint width)
482 {
483   if (items != NULL)
484     {
485       gint x = 0, height;
486       GtkWidget *alignment;
487       GtkWidget *item;
488   
489       height = g_list_length (items) / width + 1;
490       gtk_table_resize (table, height, width);
491       
492       if (y > 0)
493         {
494           item = gtk_hseparator_new ();
495           alignment = gtk_alignment_new (0.5, 0.5, 1.0, 0.0);
496           gtk_container_add (GTK_CONTAINER (alignment), item);
497           gtk_widget_show (alignment);
498           gtk_widget_show (item);
499           
500           gtk_table_attach_defaults (table, alignment, 0, width, y-1, y+1);
501         }
502       
503       for (; items != NULL; items = items->next)
504         {
505           item = items->data;
506           alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
507           gtk_container_add (GTK_CONTAINER (alignment), item);
508           gtk_widget_show (alignment);
509           gtk_widget_show (item);
510           
511           if (x >= width)
512             {
513               x = 0;
514               y++;
515             }
516           gtk_table_attach_defaults (table, alignment, x, x+1, y, y+1);
517           x++;
518         }
519       
520       y++;
521     }
522   return y;
523 }
524
525 static void
526 update_editor_sheet (EggToolbarEditor *editor)
527 {
528   gint y;
529   GPtrArray *items;
530   GList *to_move = NULL, *to_copy = NULL;
531   GtkWidget *table;
532   GtkWidget *viewport;
533
534   g_return_if_fail (EGG_IS_TOOLBAR_EDITOR (editor));
535   
536   /* Create new table. */
537   table = gtk_table_new (0, 0, TRUE);
538   editor->priv->table = table;
539   gtk_container_set_border_width (GTK_CONTAINER (table), 12);
540   gtk_table_set_row_spacings (GTK_TABLE (table), 24);
541   gtk_widget_show (table);
542   gtk_drag_dest_set (table, GTK_DEST_DEFAULT_ALL,
543                      dest_drag_types, G_N_ELEMENTS (dest_drag_types),
544                      GDK_ACTION_MOVE | GDK_ACTION_COPY);
545   
546   /* Build two lists of items (one for copying, one for moving). */
547   items = egg_toolbars_model_get_name_avail (editor->priv->model);
548   while (items->len > 0)
549     {
550       GtkWidget *item;
551       const char *name;
552       gint flags;
553       
554       name = g_ptr_array_index (items, 0);
555       g_ptr_array_remove_index_fast (items, 0);
556       
557       flags = egg_toolbars_model_get_name_flags (editor->priv->model, name);
558       if ((flags & EGG_TB_MODEL_NAME_INFINITE) == 0)
559         {
560           item = editor_create_item_from_name (editor, name, GDK_ACTION_MOVE);
561           if (item != NULL)
562             to_move = g_list_insert_sorted (to_move, item, compare_items);
563         }
564       else
565         {
566           item = editor_create_item_from_name (editor, name, GDK_ACTION_COPY);
567           if (item != NULL)
568             to_copy = g_list_insert_sorted (to_copy, item, compare_items);
569         }
570     }
571
572   /* Add them to the sheet. */
573   y = 0;
574   y = append_table (GTK_TABLE (table), to_move, y, 4);
575   y = append_table (GTK_TABLE (table), to_copy, y, 4);
576   
577   g_list_free (to_move);
578   g_list_free (to_copy);
579   g_ptr_array_free (items, TRUE);
580   
581   /* Delete old table. */
582   viewport = GTK_BIN (editor->priv->scrolled_window)->child;
583   if (viewport)
584     {
585       gtk_container_remove (GTK_CONTAINER (viewport),
586                             GTK_BIN (viewport)->child);
587     }
588   
589   /* Add table to window. */
590   gtk_scrolled_window_add_with_viewport
591     (GTK_SCROLLED_WINDOW (editor->priv->scrolled_window), table);
592
593 }
594
595 static void
596 setup_editor (EggToolbarEditor *editor)
597 {
598   GtkWidget *scrolled_window;
599
600   gtk_container_set_border_width (GTK_CONTAINER (editor), 12);
601   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
602   editor->priv->scrolled_window = scrolled_window;
603   gtk_widget_show (scrolled_window);
604   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
605                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
606   gtk_box_pack_start (GTK_BOX (editor), scrolled_window, TRUE, TRUE, 0);
607 }
608
609 static void
610 egg_toolbar_editor_init (EggToolbarEditor *t)
611 {
612   t->priv = EGG_TOOLBAR_EDITOR_GET_PRIVATE (t);
613
614   t->priv->manager = NULL;
615   t->priv->actions_list = NULL;
616
617   setup_editor (t);
618 }
619