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