]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/toolbar-editor/egg-toolbar-editor.c
Fix multihead problems in toolbar editor. Fixes bug #382055.
[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   GdkScreen *screen;
316   
317   screen = gtk_widget_get_screen (widget);
318   
319   cursor = gdk_cursor_new_for_display (gdk_screen_get_display (screen),
320                                        GDK_HAND2);
321   gdk_window_set_cursor (widget->window, cursor);
322   gdk_cursor_unref (cursor);
323 }
324
325 static void
326 event_box_realize_cb (GtkWidget *widget, GtkImage *icon)
327 {
328   GtkImageType type;
329
330   set_drag_cursor (widget);
331
332   type = gtk_image_get_storage_type (icon);
333   if (type == GTK_IMAGE_STOCK)
334     {
335       gchar *stock_id;
336       GdkPixbuf *pixbuf;
337
338       gtk_image_get_stock (icon, &stock_id, NULL);
339       pixbuf = gtk_widget_render_icon (widget, stock_id,
340                                        GTK_ICON_SIZE_LARGE_TOOLBAR, NULL);
341       gtk_drag_source_set_icon_pixbuf (widget, pixbuf);
342       g_object_unref (pixbuf);
343     }
344   else if (type == GTK_IMAGE_ICON_NAME)
345     {
346       const gchar *icon_name;
347       GdkScreen *screen;
348       GtkIconTheme *icon_theme;
349       GtkSettings *settings;
350       gint width, height;
351       GdkPixbuf *pixbuf;
352
353       gtk_image_get_icon_name (icon, &icon_name, NULL);
354       screen = gtk_widget_get_screen (widget);
355       icon_theme = gtk_icon_theme_get_for_screen (screen);
356       settings = gtk_settings_get_for_screen (screen);
357
358       if (!gtk_icon_size_lookup_for_settings (settings,
359                                               GTK_ICON_SIZE_LARGE_TOOLBAR,
360                                               &width, &height))
361         {
362           width = height = 24;
363         }
364
365       pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
366                                          MIN (width, height), 0, NULL);
367       if (G_UNLIKELY (!pixbuf))
368         return;
369
370       gtk_drag_source_set_icon_pixbuf (widget, pixbuf);
371       g_object_unref (pixbuf);
372
373     }
374   else if (type == GTK_IMAGE_PIXBUF)
375     {
376       GdkPixbuf *pixbuf = gtk_image_get_pixbuf (icon);
377       gtk_drag_source_set_icon_pixbuf (widget, pixbuf);
378     }
379 }
380
381 static GtkWidget *
382 editor_create_item (EggToolbarEditor *editor,
383                     GtkImage         *icon,
384                     const char       *label_text,
385                     GdkDragAction     action)
386 {
387   GtkWidget *event_box;
388   GtkWidget *vbox;
389   GtkWidget *label;
390   gchar *label_no_mnemonic = NULL;
391
392   event_box = gtk_event_box_new ();
393   gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
394   gtk_widget_show (event_box);
395   gtk_drag_source_set (event_box,
396                        GDK_BUTTON1_MASK,
397                        source_drag_types, G_N_ELEMENTS (source_drag_types), action);
398   g_signal_connect (event_box, "drag_data_get",
399                     G_CALLBACK (drag_data_get_cb), editor);
400   g_signal_connect_after (event_box, "realize",
401                           G_CALLBACK (event_box_realize_cb), icon);
402
403   if (action == GDK_ACTION_MOVE)
404     {
405       g_signal_connect (event_box, "drag_begin",
406                         G_CALLBACK (drag_begin_cb), NULL);
407       g_signal_connect (event_box, "drag_end",
408                         G_CALLBACK (drag_end_cb), NULL);
409     }
410
411   vbox = gtk_vbox_new (0, FALSE);
412   gtk_widget_show (vbox);
413   gtk_container_add (GTK_CONTAINER (event_box), vbox);
414
415   gtk_widget_show (GTK_WIDGET (icon));
416   gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (icon), FALSE, TRUE, 0);
417   label_no_mnemonic = elide_underscores (label_text);
418   label = gtk_label_new (label_no_mnemonic);
419   g_free (label_no_mnemonic);
420   gtk_widget_show (label);
421   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
422
423   return event_box;
424 }
425
426 static GtkWidget *
427 editor_create_item_from_name (EggToolbarEditor *editor,
428                               const char *      name,
429                               GdkDragAction     drag_action)
430 {
431   GtkWidget *item;
432   const char *item_name;
433   char *short_label;
434   const char *collate_key;
435   
436   if (strcmp (name, "_separator") == 0)
437     {
438       GtkWidget *icon;
439       
440       icon = _egg_editable_toolbar_new_separator_image ();
441       short_label = _("Separator");
442       item_name = g_strdup (name);
443       collate_key = g_utf8_collate_key (short_label, -1);
444       item = editor_create_item (editor, GTK_IMAGE (icon), 
445                                  short_label, drag_action);
446     }
447   else
448     {
449       GtkAction *action;
450       GtkWidget *icon;
451       char *stock_id, *icon_name = NULL;
452       
453       action = find_action (editor, name);
454       g_return_val_if_fail (action != NULL, NULL);
455
456       g_object_get (action,
457                     "icon-name", &icon_name,
458                     "stock-id", &stock_id,
459                     "short-label", &short_label,
460                     NULL);
461
462       /* This is a workaround to catch named icons. */
463       if (icon_name)
464         icon = gtk_image_new_from_icon_name (icon_name,
465                                              GTK_ICON_SIZE_LARGE_TOOLBAR);
466       else
467         icon = gtk_image_new_from_stock (stock_id ? stock_id : GTK_STOCK_DND,
468                                          GTK_ICON_SIZE_LARGE_TOOLBAR);
469
470       item_name = g_strdup (name);
471       collate_key = g_utf8_collate_key (short_label, -1);
472       item = editor_create_item (editor, GTK_IMAGE (icon),
473                                  short_label, drag_action);
474
475       g_free (short_label);
476       g_free (stock_id);
477       g_free (icon_name);
478     }
479   
480   g_object_set_data_full (G_OBJECT (item), "egg-collate-key",
481                           (gpointer) collate_key, g_free);
482   g_object_set_data_full (G_OBJECT (item), "egg-item-name",
483                           (gpointer) item_name, g_free);
484   
485   return item;
486 }
487
488 static gint
489 append_table (GtkTable *table, GList *items, gint y, gint width)
490 {
491   if (items != NULL)
492     {
493       gint x = 0, height;
494       GtkWidget *alignment;
495       GtkWidget *item;
496   
497       height = g_list_length (items) / width + 1;
498       gtk_table_resize (table, height, width);
499       
500       if (y > 0)
501         {
502           item = gtk_hseparator_new ();
503           alignment = gtk_alignment_new (0.5, 0.5, 1.0, 0.0);
504           gtk_container_add (GTK_CONTAINER (alignment), item);
505           gtk_widget_show (alignment);
506           gtk_widget_show (item);
507           
508           gtk_table_attach_defaults (table, alignment, 0, width, y-1, y+1);
509         }
510       
511       for (; items != NULL; items = items->next)
512         {
513           item = items->data;
514           alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
515           gtk_container_add (GTK_CONTAINER (alignment), item);
516           gtk_widget_show (alignment);
517           gtk_widget_show (item);
518           
519           if (x >= width)
520             {
521               x = 0;
522               y++;
523             }
524           gtk_table_attach_defaults (table, alignment, x, x+1, y, y+1);
525           x++;
526         }
527       
528       y++;
529     }
530   return y;
531 }
532
533 static void
534 update_editor_sheet (EggToolbarEditor *editor)
535 {
536   gint y;
537   GPtrArray *items;
538   GList *to_move = NULL, *to_copy = NULL;
539   GtkWidget *table;
540   GtkWidget *viewport;
541
542   g_return_if_fail (EGG_IS_TOOLBAR_EDITOR (editor));
543   
544   /* Create new table. */
545   table = gtk_table_new (0, 0, TRUE);
546   editor->priv->table = table;
547   gtk_container_set_border_width (GTK_CONTAINER (table), 12);
548   gtk_table_set_row_spacings (GTK_TABLE (table), 24);
549   gtk_widget_show (table);
550   gtk_drag_dest_set (table, GTK_DEST_DEFAULT_ALL,
551                      dest_drag_types, G_N_ELEMENTS (dest_drag_types),
552                      GDK_ACTION_MOVE | GDK_ACTION_COPY);
553   
554   /* Build two lists of items (one for copying, one for moving). */
555   items = egg_toolbars_model_get_name_avail (editor->priv->model);
556   while (items->len > 0)
557     {
558       GtkWidget *item;
559       const char *name;
560       gint flags;
561       
562       name = g_ptr_array_index (items, 0);
563       g_ptr_array_remove_index_fast (items, 0);
564       
565       flags = egg_toolbars_model_get_name_flags (editor->priv->model, name);
566       if ((flags & EGG_TB_MODEL_NAME_INFINITE) == 0)
567         {
568           item = editor_create_item_from_name (editor, name, GDK_ACTION_MOVE);
569           if (item != NULL)
570             to_move = g_list_insert_sorted (to_move, item, compare_items);
571         }
572       else
573         {
574           item = editor_create_item_from_name (editor, name, GDK_ACTION_COPY);
575           if (item != NULL)
576             to_copy = g_list_insert_sorted (to_copy, item, compare_items);
577         }
578     }
579
580   /* Add them to the sheet. */
581   y = 0;
582   y = append_table (GTK_TABLE (table), to_move, y, 4);
583   y = append_table (GTK_TABLE (table), to_copy, y, 4);
584   
585   g_list_free (to_move);
586   g_list_free (to_copy);
587   g_ptr_array_free (items, TRUE);
588   
589   /* Delete old table. */
590   viewport = GTK_BIN (editor->priv->scrolled_window)->child;
591   if (viewport)
592     {
593       gtk_container_remove (GTK_CONTAINER (viewport),
594                             GTK_BIN (viewport)->child);
595     }
596   
597   /* Add table to window. */
598   gtk_scrolled_window_add_with_viewport
599     (GTK_SCROLLED_WINDOW (editor->priv->scrolled_window), table);
600
601 }
602
603 static void
604 setup_editor (EggToolbarEditor *editor)
605 {
606   GtkWidget *scrolled_window;
607
608   gtk_container_set_border_width (GTK_CONTAINER (editor), 12);
609   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
610   editor->priv->scrolled_window = scrolled_window;
611   gtk_widget_show (scrolled_window);
612   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
613                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
614   gtk_box_pack_start (GTK_BOX (editor), scrolled_window, TRUE, TRUE, 0);
615 }
616
617 static void
618 egg_toolbar_editor_init (EggToolbarEditor *t)
619 {
620   t->priv = EGG_TOOLBAR_EDITOR_GET_PRIVATE (t);
621
622   t->priv->manager = NULL;
623   t->priv->actions_list = NULL;
624
625   setup_editor (t);
626 }
627