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