2 * Copyright (C) 2003, 2004 Marco Pesenti Gritti
3 * Copyright (C) 2003, 2004, 2005 Christian Persch
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "egg-editable-toolbar.h"
25 #include "egg-toolbars-model.h"
26 #include "egg-toolbar-editor.h"
28 #include <gtk/gtkvseparator.h>
29 #include <gtk/gtkiconfactory.h>
30 #include <gtk/gtkwindow.h>
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkdnd.h>
33 #include <gtk/gtkhbox.h>
34 #include <gtk/gtkimage.h>
35 #include <gtk/gtktoggleaction.h>
36 #include <gtk/gtkcheckmenuitem.h>
37 #include <gtk/gtkimagemenuitem.h>
38 #include <gtk/gtkseparatormenuitem.h>
39 #include <gtk/gtkmenu.h>
40 #include <gtk/gtkstock.h>
41 #include <gtk/gtklabel.h>
42 #include <gtk/gtkbutton.h>
43 #include <gtk/gtktoolbar.h>
44 #include <gtk/gtktoolitem.h>
45 #include <gtk/gtktoolbutton.h>
46 #include <gtk/gtkseparatortoolitem.h>
47 #include <gtk/gtkicontheme.h>
48 #include <glib/gi18n.h>
51 static GdkPixbuf * new_separator_pixbuf (void);
53 #define MIN_TOOLBAR_HEIGHT 20
54 #define EGG_ITEM_NAME "egg-item-name"
55 #define STOCK_DRAG_MODE "stock_drag-mode"
57 static const GtkTargetEntry dest_drag_types[] = {
58 {EGG_TOOLBAR_ITEM_TYPE, GTK_TARGET_SAME_APP, 0},
77 static guint egg_editable_toolbar_signals[LAST_SIGNAL] = { 0 };
79 #define EGG_EDITABLE_TOOLBAR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EGG_TYPE_EDITABLE_TOOLBAR, EggEditableToolbarPrivate))
81 struct _EggEditableToolbarPrivate
83 GtkUIManager *manager;
84 EggToolbarsModel *model;
87 GtkWidget *fixed_toolbar;
90 GtkActionGroup *actions;
93 GList *visibility_paths;
94 GPtrArray *visibility_actions;
99 GtkToolbar *dnd_toolbar;
100 GtkToolItem *dnd_toolitem;
103 G_DEFINE_TYPE (EggEditableToolbar, egg_editable_toolbar, GTK_TYPE_VBOX);
106 get_dock_position (EggEditableToolbar *etoolbar,
112 l = gtk_container_get_children (GTK_CONTAINER (etoolbar));
113 result = g_list_index (l, dock);
120 get_toolbar_position (EggEditableToolbar *etoolbar, GtkWidget *toolbar)
122 return get_dock_position (etoolbar, toolbar->parent);
126 get_n_toolbars (EggEditableToolbar *etoolbar)
131 l = gtk_container_get_children (GTK_CONTAINER (etoolbar));
132 result = g_list_length (l);
139 get_dock_nth (EggEditableToolbar *etoolbar,
145 l = gtk_container_get_children (GTK_CONTAINER (etoolbar));
146 result = g_list_nth_data (l, position);
153 get_toolbar_nth (EggEditableToolbar *etoolbar,
160 dock = get_dock_nth (etoolbar, position);
161 g_return_val_if_fail (dock != NULL, NULL);
163 l = gtk_container_get_children (GTK_CONTAINER (dock));
164 result = GTK_WIDGET (l->data);
171 find_action (EggEditableToolbar *etoolbar,
175 GtkAction *action = NULL;
177 l = gtk_ui_manager_get_action_groups (etoolbar->priv->manager);
179 g_return_val_if_fail (name != NULL, NULL);
181 for (; l != NULL; l = l->next)
185 tmp = gtk_action_group_get_action (GTK_ACTION_GROUP (l->data), name);
194 drag_data_delete_cb (GtkWidget *widget,
195 GdkDragContext *context,
196 EggEditableToolbar *etoolbar)
198 int pos, toolbar_pos;
200 widget = gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM);
201 g_return_if_fail (widget != NULL);
202 g_return_if_fail (EGG_IS_EDITABLE_TOOLBAR (etoolbar));
204 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (widget->parent),
205 GTK_TOOL_ITEM (widget));
206 toolbar_pos = get_toolbar_position (etoolbar, widget->parent);
208 egg_toolbars_model_remove_item (etoolbar->priv->model,
213 drag_begin_cb (GtkWidget *widget,
214 GdkDragContext *context,
215 EggEditableToolbar *etoolbar)
220 gtk_widget_hide (widget);
222 action = g_object_get_data (G_OBJECT (widget), "gtk-action");
223 if (action == NULL) return;
225 flags = egg_toolbars_model_get_name_flags (etoolbar->priv->model,
226 gtk_action_get_name (action));
227 if (!(flags & EGG_TB_MODEL_NAME_INFINITE))
229 flags &= ~EGG_TB_MODEL_NAME_USED;
230 egg_toolbars_model_set_name_flags (etoolbar->priv->model,
231 gtk_action_get_name (action),
237 drag_end_cb (GtkWidget *widget,
238 GdkDragContext *context,
239 EggEditableToolbar *etoolbar)
244 if (gtk_widget_get_parent (widget) != NULL)
246 gtk_widget_show (widget);
248 action = g_object_get_data (G_OBJECT (widget), "gtk-action");
249 if (action == NULL) return;
251 flags = egg_toolbars_model_get_name_flags (etoolbar->priv->model,
252 gtk_action_get_name (action));
253 if (!(flags & EGG_TB_MODEL_NAME_INFINITE))
255 flags |= EGG_TB_MODEL_NAME_USED;
256 egg_toolbars_model_set_name_flags (etoolbar->priv->model,
257 gtk_action_get_name (action),
264 drag_data_get_cb (GtkWidget *widget,
265 GdkDragContext *context,
266 GtkSelectionData *selection_data,
269 EggEditableToolbar *etoolbar)
271 EggToolbarsModel *model;
275 g_return_if_fail (EGG_IS_EDITABLE_TOOLBAR (etoolbar));
276 model = egg_editable_toolbar_get_model (etoolbar);
278 name = g_object_get_data (G_OBJECT (widget), EGG_ITEM_NAME);
281 name = g_object_get_data (G_OBJECT (gtk_widget_get_parent (widget)), EGG_ITEM_NAME);
282 g_return_if_fail (name != NULL);
285 data = egg_toolbars_model_get_data (model, selection_data->target, name);
288 gtk_selection_data_set (selection_data, selection_data->target, 8, (unsigned char *)data, strlen (data));
294 move_item_cb (GtkAction *action,
295 EggEditableToolbar *etoolbar)
297 GtkWidget *toolitem = gtk_widget_get_ancestor (egg_editable_toolbar_get_selected (etoolbar), GTK_TYPE_TOOL_ITEM);
298 GtkTargetList *list = gtk_target_list_new (dest_drag_types, G_N_ELEMENTS (dest_drag_types));
300 GdkEvent *realevent = gtk_get_current_event();
301 GdkEventMotion event;
302 event.type = GDK_MOTION_NOTIFY;
303 event.window = realevent->any.window;
304 event.send_event = FALSE;
306 event.time = gdk_event_get_time (realevent);
307 gdk_event_get_state (realevent, &event.state);
308 gdk_event_get_coords (realevent, &event.x, &event.y);
309 gdk_event_get_root_coords (realevent, &event.x_root, &event.y_root);
311 gtk_drag_begin (toolitem, list, GDK_ACTION_MOVE, 1, (GdkEvent *)&event);
312 gtk_target_list_unref (list);
316 remove_item_cb (GtkAction *action,
317 EggEditableToolbar *etoolbar)
319 GtkWidget *toolitem = gtk_widget_get_ancestor (egg_editable_toolbar_get_selected (etoolbar), GTK_TYPE_TOOL_ITEM);
320 int pos, toolbar_pos;
322 toolbar_pos = get_toolbar_position (etoolbar, toolitem->parent);
323 pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolitem->parent),
324 GTK_TOOL_ITEM (toolitem));
326 egg_toolbars_model_remove_item (etoolbar->priv->model,
329 if (egg_toolbars_model_n_items (etoolbar->priv->model, toolbar_pos) == 0)
331 egg_toolbars_model_remove_toolbar (etoolbar->priv->model, toolbar_pos);
336 remove_toolbar_cb (GtkAction *action,
337 EggEditableToolbar *etoolbar)
339 GtkWidget *selected = egg_editable_toolbar_get_selected (etoolbar);
340 GtkWidget *toolbar = gtk_widget_get_ancestor (selected, GTK_TYPE_TOOLBAR);
343 toolbar_pos = get_toolbar_position (etoolbar, toolbar);
344 egg_toolbars_model_remove_toolbar (etoolbar->priv->model, toolbar_pos);
348 popup_context_deactivate (GtkMenuShell *menu,
349 EggEditableToolbar *etoolbar)
351 egg_editable_toolbar_set_selected (etoolbar, NULL);
352 g_object_notify (G_OBJECT (etoolbar), "selected");
356 popup_context_menu_cb (GtkWidget *toolbar,
360 EggEditableToolbar *etoolbar)
362 if (etoolbar->priv->popup_path != NULL)
366 egg_editable_toolbar_set_selected (etoolbar, toolbar);
367 g_object_notify (G_OBJECT (etoolbar), "selected");
369 menu = GTK_MENU (gtk_ui_manager_get_widget (etoolbar->priv->manager,
370 etoolbar->priv->popup_path));
371 g_return_if_fail (menu != NULL);
372 gtk_menu_popup (menu, NULL, NULL, NULL, NULL, button_number, gtk_get_current_event_time ());
373 g_signal_connect_object (menu, "selection-done",
374 G_CALLBACK (popup_context_deactivate),
380 button_press_event_cb (GtkWidget *widget,
381 GdkEventButton *event,
382 EggEditableToolbar *etoolbar)
384 if (event->button == 3 && etoolbar->priv->popup_path != NULL)
388 egg_editable_toolbar_set_selected (etoolbar, widget);
389 g_object_notify (G_OBJECT (etoolbar), "selected");
391 menu = GTK_MENU (gtk_ui_manager_get_widget (etoolbar->priv->manager,
392 etoolbar->priv->popup_path));
393 g_return_val_if_fail (menu != NULL, FALSE);
394 gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button, event->time);
395 g_signal_connect_object (menu, "selection-done",
396 G_CALLBACK (popup_context_deactivate),
406 configure_item_sensitivity (GtkToolItem *item, EggEditableToolbar *etoolbar)
411 name = g_object_get_data (G_OBJECT (item), EGG_ITEM_NAME);
412 action = name ? find_action (etoolbar, name) : NULL;
416 g_object_notify (G_OBJECT (action), "sensitive");
419 gtk_tool_item_set_use_drag_window (item,
420 (etoolbar->priv->edit_mode > 0) ||
421 GTK_IS_SEPARATOR_TOOL_ITEM (item));
426 configure_item_cursor (GtkToolItem *item,
427 EggEditableToolbar *etoolbar)
429 EggEditableToolbarPrivate *priv = etoolbar->priv;
430 GtkWidget *widget = GTK_WIDGET (item);
432 if (widget->window != NULL)
434 if (priv->edit_mode > 0)
438 GdkPixbuf *pixbuf = NULL;
440 screen = gtk_widget_get_screen (GTK_WIDGET (etoolbar));
442 cursor = gdk_cursor_new_for_display (gdk_screen_get_display (screen),
444 gdk_window_set_cursor (widget->window, cursor);
445 gdk_cursor_unref (cursor);
447 gtk_drag_source_set (widget, GDK_BUTTON1_MASK, dest_drag_types,
448 G_N_ELEMENTS (dest_drag_types), GDK_ACTION_MOVE);
449 if (GTK_IS_SEPARATOR_TOOL_ITEM (item))
451 pixbuf = new_separator_pixbuf ();
455 char *icon_name=NULL;
460 name = g_object_get_data (G_OBJECT (widget), EGG_ITEM_NAME);
461 action = name ? find_action (etoolbar, name) : NULL;
465 g_object_get (action,
466 "icon-name", &icon_name,
467 "stock-id", &stock_id,
473 GtkIconTheme *icon_theme;
474 GtkSettings *settings;
477 screen = gtk_widget_get_screen (widget);
478 icon_theme = gtk_icon_theme_get_for_screen (screen);
479 settings = gtk_settings_get_for_screen (screen);
481 if (!gtk_icon_size_lookup_for_settings (settings,
482 GTK_ICON_SIZE_LARGE_TOOLBAR,
488 pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
489 MIN (width, height), 0, NULL);
493 pixbuf = gtk_widget_render_icon (widget, stock_id,
494 GTK_ICON_SIZE_LARGE_TOOLBAR, NULL);
500 if (G_UNLIKELY (!pixbuf))
504 gtk_drag_source_set_icon_pixbuf (widget, pixbuf);
505 g_object_unref (pixbuf);
510 gdk_window_set_cursor (GTK_WIDGET(item)->window, NULL);
517 configure_item_tooltip (GtkToolItem *item)
519 GtkAction *action = g_object_get_data (G_OBJECT (item),
524 g_object_notify (G_OBJECT (action), "tooltip");
530 connect_widget_signals (GtkWidget *proxy, EggEditableToolbar *etoolbar)
532 if (GTK_IS_CONTAINER (proxy))
534 gtk_container_forall (GTK_CONTAINER (proxy),
535 (GtkCallback) connect_widget_signals,
536 (gpointer) etoolbar);
539 if (GTK_IS_TOOL_ITEM (proxy))
541 g_signal_connect_object (proxy, "drag_begin",
542 G_CALLBACK (drag_begin_cb),
544 g_signal_connect_object (proxy, "drag_end",
545 G_CALLBACK (drag_end_cb),
547 g_signal_connect_object (proxy, "drag_data_get",
548 G_CALLBACK (drag_data_get_cb),
550 g_signal_connect_object (proxy, "drag_data_delete",
551 G_CALLBACK (drag_data_delete_cb),
555 if (GTK_IS_BUTTON (proxy) || GTK_IS_TOOL_ITEM (proxy))
557 g_signal_connect_object (proxy, "button-press-event",
558 G_CALLBACK (button_press_event_cb),
564 action_sensitive_cb (GtkAction *action,
568 EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR
569 (gtk_widget_get_ancestor (GTK_WIDGET (item), EGG_TYPE_EDITABLE_TOOLBAR));
571 if (etoolbar->priv->edit_mode > 0)
573 gtk_widget_set_sensitive (GTK_WIDGET (item), TRUE);
578 create_item_from_action (EggEditableToolbar *etoolbar,
583 g_return_val_if_fail (name != NULL, NULL);
585 if (strcmp (name, "_separator") == 0)
587 item = gtk_separator_tool_item_new ();
591 GtkAction *action = find_action (etoolbar, name);
592 if (action == NULL) return NULL;
594 item = GTK_TOOL_ITEM (gtk_action_create_tool_item (action));
596 /* Normally done on-demand by the GtkUIManager, but no
597 * such demand may have been made yet, so do it ourselves.
599 gtk_action_set_accel_group
600 (action, gtk_ui_manager_get_accel_group(etoolbar->priv->manager));
602 g_signal_connect_object (action, "notify::sensitive",
603 G_CALLBACK (action_sensitive_cb), item, 0);
606 gtk_widget_show (GTK_WIDGET (item));
608 g_object_set_data_full (G_OBJECT (item), EGG_ITEM_NAME,
609 g_strdup (name), g_free);
615 create_item_from_position (EggEditableToolbar *etoolbar,
616 int toolbar_position,
622 name = egg_toolbars_model_item_nth (etoolbar->priv->model, toolbar_position, position);
623 item = create_item_from_action (etoolbar, name);
629 toolbar_drag_data_received_cb (GtkToolbar *toolbar,
630 GdkDragContext *context,
633 GtkSelectionData *selection_data,
636 EggEditableToolbar *etoolbar)
638 /* This function can be called for two reasons
640 * (1) drag_motion() needs an item to pass to
641 * gtk_toolbar_set_drop_highlight_item(). We can
642 * recognize this case by etoolbar->priv->pending being TRUE
643 * We should just create an item and return.
645 * (2) The drag has finished, and drag_drop() wants us to
646 * actually add a new item to the toolbar.
649 GdkAtom type = selection_data->type;
650 const char *data = (char *)selection_data->data;
654 gboolean used = FALSE;
656 /* Find out where the drop is occuring, and the name of what is being dropped. */
657 if (selection_data->length >= 0)
659 ipos = gtk_toolbar_get_drop_index (toolbar, x, y);
660 name = egg_toolbars_model_get_name (etoolbar->priv->model, type, data, FALSE);
663 used = ((egg_toolbars_model_get_name_flags (etoolbar->priv->model, name) & EGG_TB_MODEL_NAME_USED) != 0);
667 /* If we just want a highlight item, then . */
668 if (etoolbar->priv->dnd_pending > 0)
670 etoolbar->priv->dnd_pending--;
672 if (name != NULL && etoolbar->priv->dnd_toolbar == toolbar && !used)
674 etoolbar->priv->dnd_toolitem = create_item_from_action (etoolbar, name);
675 gtk_toolbar_set_drop_highlight_item (etoolbar->priv->dnd_toolbar,
676 etoolbar->priv->dnd_toolitem, ipos);
681 gtk_toolbar_set_drop_highlight_item (toolbar, NULL, 0);
682 etoolbar->priv->dnd_toolbar = NULL;
683 etoolbar->priv->dnd_toolitem = NULL;
685 /* If we don't have a name to use yet, try to create one. */
686 if (name == NULL && selection_data->length >= 0)
688 name = egg_toolbars_model_get_name (etoolbar->priv->model, type, data, TRUE);
691 if (name != NULL && !used)
693 gint tpos = get_toolbar_position (etoolbar, GTK_WIDGET (toolbar));
694 egg_toolbars_model_add_item (etoolbar->priv->model, tpos, ipos, name);
695 gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time);
699 gtk_drag_finish (context, FALSE, context->action == GDK_ACTION_MOVE, time);
707 toolbar_drag_drop_cb (GtkToolbar *toolbar,
708 GdkDragContext *context,
712 EggEditableToolbar *etoolbar)
716 target = gtk_drag_dest_find_target (GTK_WIDGET (toolbar), context, NULL);
717 if (target != GDK_NONE)
719 gtk_drag_get_data (GTK_WIDGET (toolbar), context, target, time);
727 toolbar_drag_motion_cb (GtkToolbar *toolbar,
728 GdkDragContext *context,
732 EggEditableToolbar *etoolbar)
734 GdkAtom target = gtk_drag_dest_find_target (GTK_WIDGET (toolbar), context, NULL);
735 if (target == GDK_NONE)
737 gdk_drag_status (context, 0, time);
741 /* Make ourselves the current dnd toolbar, and request a highlight item. */
742 if (etoolbar->priv->dnd_toolbar != toolbar)
744 etoolbar->priv->dnd_toolbar = toolbar;
745 etoolbar->priv->dnd_toolitem = NULL;
746 etoolbar->priv->dnd_pending++;
747 gtk_drag_get_data (GTK_WIDGET (toolbar), context, target, time);
750 /* If a highlight item is available, use it. */
751 else if (etoolbar->priv->dnd_toolitem)
753 gint ipos = gtk_toolbar_get_drop_index (etoolbar->priv->dnd_toolbar, x, y);
754 gtk_toolbar_set_drop_highlight_item (etoolbar->priv->dnd_toolbar,
755 etoolbar->priv->dnd_toolitem, ipos);
758 gdk_drag_status (context, context->suggested_action, time);
764 toolbar_drag_leave_cb (GtkToolbar *toolbar,
765 GdkDragContext *context,
767 EggEditableToolbar *etoolbar)
769 gtk_toolbar_set_drop_highlight_item (toolbar, NULL, 0);
771 /* If we were the current dnd toolbar target, remove the item. */
772 if (etoolbar->priv->dnd_toolbar == toolbar)
774 etoolbar->priv->dnd_toolbar = NULL;
775 etoolbar->priv->dnd_toolitem = NULL;
780 configure_drag_dest (EggEditableToolbar *etoolbar,
783 EggToolbarsItemType *type;
784 GtkTargetList *targets;
787 /* Make every toolbar able to receive drag-drops. */
788 gtk_drag_dest_set (GTK_WIDGET (toolbar), 0,
789 dest_drag_types, G_N_ELEMENTS (dest_drag_types),
790 GDK_ACTION_MOVE | GDK_ACTION_COPY);
792 /* Add any specialist drag-drop abilities. */
793 targets = gtk_drag_dest_get_target_list (GTK_WIDGET (toolbar));
794 list = egg_toolbars_model_get_types (etoolbar->priv->model);
798 if (type->new_name != NULL || type->get_name != NULL)
799 gtk_target_list_add (targets, type->type, 0, 0);
805 toggled_visibility_cb (GtkToggleAction *action,
806 EggEditableToolbar *etoolbar)
808 EggEditableToolbarPrivate *priv = etoolbar->priv;
810 EggTbModelFlags flags;
814 visible = gtk_toggle_action_get_active (action);
815 for (i = 0; i < priv->visibility_actions->len; i++)
816 if (g_ptr_array_index (priv->visibility_actions, i) == action)
819 g_return_if_fail (i < priv->visibility_actions->len);
821 dock = get_dock_nth (etoolbar, i);
824 gtk_widget_show (dock);
828 gtk_widget_hide (dock);
831 if (priv->save_hidden)
833 flags = egg_toolbars_model_get_flags (priv->model, i);
837 flags &= ~(EGG_TB_MODEL_HIDDEN);
841 flags |= (EGG_TB_MODEL_HIDDEN);
844 egg_toolbars_model_set_flags (priv->model, i, flags);
849 toolbar_visibility_refresh (EggEditableToolbar *etoolbar)
851 EggEditableToolbarPrivate *priv = etoolbar->priv;
852 gint n_toolbars, n_items, i, j, k;
853 GtkToggleAction *action;
857 char action_name[40];
861 if (priv == NULL || priv->model == NULL || priv->manager == NULL ||
862 priv->visibility_paths == NULL || priv->actions == NULL)
867 if (priv->visibility_actions == NULL)
869 priv->visibility_actions = g_ptr_array_new ();
872 if (priv->visibility_id != 0)
874 gtk_ui_manager_remove_ui (priv->manager, priv->visibility_id);
877 priv->visibility_id = gtk_ui_manager_new_merge_id (priv->manager);
879 showing = GTK_WIDGET_VISIBLE (etoolbar);
881 n_toolbars = egg_toolbars_model_n_toolbars (priv->model);
882 for (i = 0; i < n_toolbars; i++)
884 string = g_string_sized_new (0);
885 n_items = egg_toolbars_model_n_items (priv->model, i);
886 for (k = 0, j = 0; j < n_items; j++)
888 GValue value = { 0, };
892 name = egg_toolbars_model_item_nth (priv->model, i, j);
893 if (name == NULL) continue;
894 action = find_action (etoolbar, name);
895 if (action == NULL) continue;
897 g_value_init (&value, G_TYPE_STRING);
898 g_object_get_property (G_OBJECT (action), "label", &value);
899 name = g_value_get_string (&value);
902 g_value_unset (&value);
905 k += g_utf8_strlen (name, -1) + 2;
908 g_string_append (string, ", ");
911 g_value_unset (&value);
915 g_string_append (string, name);
916 g_value_unset (&value);
920 g_string_append (string, " ...");
923 tmp = g_string_free (string, FALSE);
924 for (j = 0, k = 0; tmp[j]; j++)
926 if (tmp[j] == '_') continue;
931 /* Translaters: This string is for a toggle to display a toolbar.
932 * The name of the toolbar is automatically computed from the widgets
933 * on the toolbar, and is placed at the %s. Note the _ before the %s
934 * which is used to add mnemonics. We know that this is likely to
935 * produce duplicates, but don't worry about it. If your language
936 * normally has a mnemonic at the start, please use the _. If not,
938 action_label = g_strdup_printf (_("Show “_%s”"), tmp);
941 sprintf(action_name, "ToolbarToggle%d", i);
943 if (i >= priv->visibility_actions->len)
945 action = gtk_toggle_action_new (action_name, action_label, NULL, NULL);
946 g_ptr_array_add (priv->visibility_actions, action);
947 g_signal_connect_object (action, "toggled",
948 G_CALLBACK (toggled_visibility_cb),
950 gtk_action_group_add_action (priv->actions, GTK_ACTION (action));
954 action = g_ptr_array_index (priv->visibility_actions, i);
955 g_object_set (action, "label", action_label, NULL);
958 gtk_action_set_visible (GTK_ACTION (action), (egg_toolbars_model_get_flags (priv->model, i)
959 & EGG_TB_MODEL_NOT_REMOVABLE) == 0);
960 gtk_action_set_sensitive (GTK_ACTION (action), showing);
961 gtk_toggle_action_set_active (action, GTK_WIDGET_VISIBLE
962 (get_dock_nth (etoolbar, i)));
964 for (list = priv->visibility_paths; list != NULL; list = g_list_next (list))
966 gtk_ui_manager_add_ui (priv->manager, priv->visibility_id,
967 (const char *)list->data, action_name, action_name,
968 GTK_UI_MANAGER_MENUITEM, FALSE);
971 g_free (action_label);
974 gtk_ui_manager_ensure_update (priv->manager);
976 while (i < priv->visibility_actions->len)
978 action = g_ptr_array_index (priv->visibility_actions, i);
979 g_ptr_array_remove_index_fast (priv->visibility_actions, i);
980 gtk_action_group_remove_action (priv->actions, GTK_ACTION (action));
986 create_dock (EggEditableToolbar *etoolbar)
988 GtkWidget *toolbar, *hbox;
990 hbox = gtk_hbox_new (0, FALSE);
992 toolbar = gtk_toolbar_new ();
993 gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
994 gtk_widget_show (toolbar);
995 gtk_box_pack_start (GTK_BOX (hbox), toolbar, TRUE, TRUE, 0);
997 g_signal_connect (toolbar, "drag_drop",
998 G_CALLBACK (toolbar_drag_drop_cb), etoolbar);
999 g_signal_connect (toolbar, "drag_motion",
1000 G_CALLBACK (toolbar_drag_motion_cb), etoolbar);
1001 g_signal_connect (toolbar, "drag_leave",
1002 G_CALLBACK (toolbar_drag_leave_cb), etoolbar);
1004 g_signal_connect (toolbar, "drag_data_received",
1005 G_CALLBACK (toolbar_drag_data_received_cb), etoolbar);
1006 g_signal_connect (toolbar, "popup_context_menu",
1007 G_CALLBACK (popup_context_menu_cb), etoolbar);
1009 configure_drag_dest (etoolbar, GTK_TOOLBAR (toolbar));
1015 set_fixed_style (EggEditableToolbar *t, GtkToolbarStyle style)
1017 g_return_if_fail (GTK_IS_TOOLBAR (t->priv->fixed_toolbar));
1018 gtk_toolbar_set_style (GTK_TOOLBAR (t->priv->fixed_toolbar),
1019 style == GTK_TOOLBAR_ICONS ? GTK_TOOLBAR_BOTH_HORIZ : style);
1023 unset_fixed_style (EggEditableToolbar *t)
1025 g_return_if_fail (GTK_IS_TOOLBAR (t->priv->fixed_toolbar));
1026 gtk_toolbar_unset_style (GTK_TOOLBAR (t->priv->fixed_toolbar));
1030 toolbar_changed_cb (EggToolbarsModel *model,
1032 EggEditableToolbar *etoolbar)
1035 EggTbModelFlags flags;
1036 GtkToolbarStyle style;
1038 flags = egg_toolbars_model_get_flags (model, position);
1039 toolbar = get_toolbar_nth (etoolbar, position);
1041 if (flags & EGG_TB_MODEL_ICONS)
1043 style = GTK_TOOLBAR_ICONS;
1045 else if (flags & EGG_TB_MODEL_TEXT)
1047 style = GTK_TOOLBAR_TEXT;
1049 else if (flags & EGG_TB_MODEL_BOTH)
1051 style = GTK_TOOLBAR_BOTH;
1053 else if (flags & EGG_TB_MODEL_BOTH_HORIZ)
1055 style = GTK_TOOLBAR_BOTH_HORIZ;
1059 gtk_toolbar_unset_style (GTK_TOOLBAR (toolbar));
1060 if (position == 0 && etoolbar->priv->fixed_toolbar)
1062 unset_fixed_style (etoolbar);
1067 gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), style);
1068 if (position == 0 && etoolbar->priv->fixed_toolbar)
1070 set_fixed_style (etoolbar, style);
1073 toolbar_visibility_refresh (etoolbar);
1077 unparent_fixed (EggEditableToolbar *etoolbar)
1079 GtkWidget *toolbar, *dock;
1080 g_return_if_fail (GTK_IS_TOOLBAR (etoolbar->priv->fixed_toolbar));
1082 toolbar = etoolbar->priv->fixed_toolbar;
1083 dock = get_dock_nth (etoolbar, 0);
1085 if (dock && toolbar->parent != NULL)
1087 gtk_container_remove (GTK_CONTAINER (dock), toolbar);
1092 update_fixed (EggEditableToolbar *etoolbar)
1094 GtkWidget *toolbar, *dock;
1095 if (!etoolbar->priv->fixed_toolbar) return;
1097 toolbar = etoolbar->priv->fixed_toolbar;
1098 dock = get_dock_nth (etoolbar, 0);
1100 if (dock && toolbar && toolbar->parent == NULL)
1102 gtk_box_pack_end (GTK_BOX (dock), toolbar, FALSE, TRUE, 0);
1104 gtk_widget_show (toolbar);
1106 gtk_widget_set_size_request (dock, -1, -1);
1107 gtk_widget_queue_resize_no_redraw (dock);
1112 toolbar_added_cb (EggToolbarsModel *model,
1114 EggEditableToolbar *etoolbar)
1118 dock = create_dock (etoolbar);
1119 if ((egg_toolbars_model_get_flags (model, position) & EGG_TB_MODEL_HIDDEN) == 0)
1120 gtk_widget_show (dock);
1122 gtk_widget_set_size_request (dock, -1, MIN_TOOLBAR_HEIGHT);
1124 gtk_box_pack_start (GTK_BOX (etoolbar), dock, TRUE, TRUE, 0);
1126 gtk_box_reorder_child (GTK_BOX (etoolbar), dock, position);
1128 gtk_widget_show_all (dock);
1130 update_fixed (etoolbar);
1132 toolbar_visibility_refresh (etoolbar);
1136 toolbar_removed_cb (EggToolbarsModel *model,
1138 EggEditableToolbar *etoolbar)
1142 if (position == 0 && etoolbar->priv->fixed_toolbar != NULL)
1144 unparent_fixed (etoolbar);
1147 dock = get_dock_nth (etoolbar, position);
1148 gtk_widget_destroy (dock);
1150 update_fixed (etoolbar);
1152 toolbar_visibility_refresh (etoolbar);
1156 item_added_cb (EggToolbarsModel *model,
1159 EggEditableToolbar *etoolbar)
1165 toolbar = get_toolbar_nth (etoolbar, tpos);
1166 item = create_item_from_position (etoolbar, tpos, ipos);
1167 if (item == NULL) return;
1169 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, ipos);
1171 connect_widget_signals (GTK_WIDGET (item), etoolbar);
1172 configure_item_tooltip (item);
1173 configure_item_cursor (item, etoolbar);
1174 configure_item_sensitivity (item, etoolbar);
1176 dock = get_dock_nth (etoolbar, tpos);
1177 gtk_widget_set_size_request (dock, -1, -1);
1178 gtk_widget_queue_resize_no_redraw (dock);
1180 toolbar_visibility_refresh (etoolbar);
1184 item_removed_cb (EggToolbarsModel *model,
1185 int toolbar_position,
1187 EggEditableToolbar *etoolbar)
1189 EggEditableToolbarPrivate *priv = etoolbar->priv;
1194 toolbar = get_toolbar_nth (etoolbar, toolbar_position);
1195 item = GTK_WIDGET (gtk_toolbar_get_nth_item
1196 (GTK_TOOLBAR (toolbar), position));
1197 g_return_if_fail (item != NULL);
1199 if (item == priv->selected)
1204 gtk_container_remove (GTK_CONTAINER (toolbar), item);
1206 toolbar_visibility_refresh (etoolbar);
1210 egg_editable_toolbar_build (EggEditableToolbar *etoolbar)
1212 int i, l, n_items, n_toolbars;
1213 EggToolbarsModel *model = etoolbar->priv->model;
1215 g_return_if_fail (model != NULL);
1216 g_return_if_fail (etoolbar->priv->manager != NULL);
1218 n_toolbars = egg_toolbars_model_n_toolbars (model);
1220 for (i = 0; i < n_toolbars; i++)
1222 GtkWidget *toolbar, *dock;
1224 dock = create_dock (etoolbar);
1225 if ((egg_toolbars_model_get_flags (model, i) & EGG_TB_MODEL_HIDDEN) == 0)
1226 gtk_widget_show (dock);
1227 gtk_box_pack_start (GTK_BOX (etoolbar), dock, TRUE, TRUE, 0);
1228 toolbar = get_toolbar_nth (etoolbar, i);
1230 n_items = egg_toolbars_model_n_items (model, i);
1231 for (l = 0; l < n_items; l++)
1235 item = create_item_from_position (etoolbar, i, l);
1238 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, l);
1240 connect_widget_signals (GTK_WIDGET (item), etoolbar);
1241 configure_item_tooltip (item);
1242 configure_item_sensitivity (item, etoolbar);
1246 egg_toolbars_model_remove_item (model, i, l);
1254 gtk_widget_set_size_request (dock, -1, MIN_TOOLBAR_HEIGHT);
1258 update_fixed (etoolbar);
1261 for (i = 0; i < n_toolbars; i ++)
1263 toolbar_changed_cb (model, i, etoolbar);
1268 egg_editable_toolbar_disconnect_model (EggEditableToolbar *toolbar)
1270 EggToolbarsModel *model = toolbar->priv->model;
1272 g_signal_handlers_disconnect_by_func
1273 (model, G_CALLBACK (item_added_cb), toolbar);
1274 g_signal_handlers_disconnect_by_func
1275 (model, G_CALLBACK (item_removed_cb), toolbar);
1276 g_signal_handlers_disconnect_by_func
1277 (model, G_CALLBACK (toolbar_added_cb), toolbar);
1278 g_signal_handlers_disconnect_by_func
1279 (model, G_CALLBACK (toolbar_removed_cb), toolbar);
1280 g_signal_handlers_disconnect_by_func
1281 (model, G_CALLBACK (toolbar_changed_cb), toolbar);
1285 egg_editable_toolbar_deconstruct (EggEditableToolbar *toolbar)
1287 EggToolbarsModel *model = toolbar->priv->model;
1290 g_return_if_fail (model != NULL);
1292 if (toolbar->priv->fixed_toolbar)
1294 unset_fixed_style (toolbar);
1295 unparent_fixed (toolbar);
1298 children = gtk_container_get_children (GTK_CONTAINER (toolbar));
1299 g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL);
1300 g_list_free (children);
1304 egg_editable_toolbar_set_model (EggEditableToolbar *etoolbar,
1305 EggToolbarsModel *model)
1307 EggEditableToolbarPrivate *priv = etoolbar->priv;
1309 if (priv->model == model) return;
1313 egg_editable_toolbar_disconnect_model (etoolbar);
1314 egg_editable_toolbar_deconstruct (etoolbar);
1316 g_object_unref (priv->model);
1319 priv->model = g_object_ref (model);
1321 egg_editable_toolbar_build (etoolbar);
1323 toolbar_visibility_refresh (etoolbar);
1325 g_signal_connect (model, "item_added",
1326 G_CALLBACK (item_added_cb), etoolbar);
1327 g_signal_connect (model, "item_removed",
1328 G_CALLBACK (item_removed_cb), etoolbar);
1329 g_signal_connect (model, "toolbar_added",
1330 G_CALLBACK (toolbar_added_cb), etoolbar);
1331 g_signal_connect (model, "toolbar_removed",
1332 G_CALLBACK (toolbar_removed_cb), etoolbar);
1333 g_signal_connect (model, "toolbar_changed",
1334 G_CALLBACK (toolbar_changed_cb), etoolbar);
1338 egg_editable_toolbar_init (EggEditableToolbar *etoolbar)
1340 EggEditableToolbarPrivate *priv;
1342 priv = etoolbar->priv = EGG_EDITABLE_TOOLBAR_GET_PRIVATE (etoolbar);
1344 priv->save_hidden = TRUE;
1346 g_signal_connect (etoolbar, "notify::visible",
1347 G_CALLBACK (toolbar_visibility_refresh), NULL);
1351 egg_editable_toolbar_dispose (GObject *object)
1353 EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1354 EggEditableToolbarPrivate *priv = etoolbar->priv;
1357 if (priv->fixed_toolbar != NULL)
1359 g_object_unref (priv->fixed_toolbar);
1360 priv->fixed_toolbar = NULL;
1363 if (priv->visibility_paths)
1365 children = priv->visibility_paths;
1366 g_list_foreach (children, (GFunc) g_free, NULL);
1367 g_list_free (children);
1368 priv->visibility_paths = NULL;
1371 g_free (priv->popup_path);
1372 priv->popup_path = NULL;
1374 if (priv->manager != NULL)
1376 if (priv->visibility_id)
1378 gtk_ui_manager_remove_ui (priv->manager, priv->visibility_id);
1379 priv->visibility_id = 0;
1382 g_object_unref (priv->manager);
1383 priv->manager = NULL;
1388 egg_editable_toolbar_disconnect_model (etoolbar);
1389 g_object_unref (priv->model);
1393 G_OBJECT_CLASS (egg_editable_toolbar_parent_class)->dispose (object);
1397 egg_editable_toolbar_set_ui_manager (EggEditableToolbar *etoolbar,
1398 GtkUIManager *manager)
1400 static const GtkActionEntry actions[] = {
1401 { "MoveToolItem", STOCK_DRAG_MODE, N_("_Move on Toolbar"), NULL,
1402 N_("Move the selected item on the toolbar"), G_CALLBACK (move_item_cb) },
1403 { "RemoveToolItem", GTK_STOCK_REMOVE, N_("_Remove from Toolbar"), NULL,
1404 N_("Remove the selected item from the toolbar"), G_CALLBACK (remove_item_cb) },
1405 { "RemoveToolbar", GTK_STOCK_DELETE, N_("_Delete Toolbar"), NULL,
1406 N_("Remove the selected toolbar"), G_CALLBACK (remove_toolbar_cb) },
1409 etoolbar->priv->manager = g_object_ref (manager);
1411 etoolbar->priv->actions = gtk_action_group_new ("ToolbarActions");
1412 gtk_action_group_set_translation_domain (etoolbar->priv->actions, GETTEXT_PACKAGE);
1413 gtk_action_group_add_actions (etoolbar->priv->actions, actions,
1414 G_N_ELEMENTS (actions), etoolbar);
1415 gtk_ui_manager_insert_action_group (manager, etoolbar->priv->actions, -1);
1416 g_object_unref (etoolbar->priv->actions);
1418 toolbar_visibility_refresh (etoolbar);
1421 GtkWidget * egg_editable_toolbar_get_selected (EggEditableToolbar *etoolbar)
1423 return etoolbar->priv->selected;
1427 egg_editable_toolbar_set_selected (EggEditableToolbar *etoolbar,
1430 GtkWidget *toolbar, *toolitem;
1433 etoolbar->priv->selected = widget;
1435 toolbar = (widget != NULL) ? gtk_widget_get_ancestor (widget, GTK_TYPE_TOOLBAR) : NULL;
1436 toolitem = (widget != NULL) ? gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM) : NULL;
1440 gint tpos = get_toolbar_position (etoolbar, toolbar);
1441 editable = ((egg_toolbars_model_get_flags (etoolbar->priv->model, tpos) & EGG_TB_MODEL_NOT_EDITABLE) == 0);
1448 gtk_action_set_visible (find_action (etoolbar, "RemoveToolbar"), (toolbar != NULL) && (etoolbar->priv->edit_mode > 0));
1449 gtk_action_set_visible (find_action (etoolbar, "RemoveToolItem"), (toolitem != NULL) && editable);
1450 gtk_action_set_visible (find_action (etoolbar, "MoveToolItem"), (toolitem != NULL) && editable);
1454 set_edit_mode (EggEditableToolbar *etoolbar,
1457 EggEditableToolbarPrivate *priv = etoolbar->priv;
1460 i = priv->edit_mode;
1467 g_return_if_fail (priv->edit_mode > 0);
1470 i *= priv->edit_mode;
1474 for (i = get_n_toolbars (etoolbar)-1; i >= 0; i--)
1478 toolbar = get_toolbar_nth (etoolbar, i);
1479 n_items = gtk_toolbar_get_n_items (GTK_TOOLBAR (toolbar));
1481 if (n_items == 0 && priv->edit_mode == 0)
1483 egg_toolbars_model_remove_toolbar (priv->model, i);
1487 for (l = 0; l < n_items; l++)
1491 item = gtk_toolbar_get_nth_item (GTK_TOOLBAR (toolbar), l);
1493 configure_item_cursor (item, etoolbar);
1494 configure_item_sensitivity (item, etoolbar);
1502 egg_editable_toolbar_set_property (GObject *object,
1504 const GValue *value,
1507 EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1511 case PROP_UI_MANAGER:
1512 egg_editable_toolbar_set_ui_manager (etoolbar, g_value_get_object (value));
1514 case PROP_TOOLBARS_MODEL:
1515 egg_editable_toolbar_set_model (etoolbar, g_value_get_object (value));
1518 egg_editable_toolbar_set_selected (etoolbar, g_value_get_object (value));
1520 case PROP_POPUP_PATH:
1521 etoolbar->priv->popup_path = g_strdup (g_value_get_string (value));
1523 case PROP_EDIT_MODE:
1524 set_edit_mode (etoolbar, g_value_get_boolean (value));
1527 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1533 egg_editable_toolbar_get_property (GObject *object,
1538 EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1542 case PROP_UI_MANAGER:
1543 g_value_set_object (value, etoolbar->priv->manager);
1545 case PROP_TOOLBARS_MODEL:
1546 g_value_set_object (value, etoolbar->priv->model);
1549 g_value_set_object (value, etoolbar->priv->selected);
1551 case PROP_EDIT_MODE:
1552 g_value_set_boolean (value, etoolbar->priv->edit_mode>0);
1555 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1561 egg_editable_toolbar_class_init (EggEditableToolbarClass *klass)
1563 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1565 object_class->dispose = egg_editable_toolbar_dispose;
1566 object_class->set_property = egg_editable_toolbar_set_property;
1567 object_class->get_property = egg_editable_toolbar_get_property;
1569 egg_editable_toolbar_signals[ACTION_REQUEST] =
1570 g_signal_new ("action_request",
1571 G_OBJECT_CLASS_TYPE (object_class),
1573 G_STRUCT_OFFSET (EggEditableToolbarClass, action_request),
1574 NULL, NULL, g_cclosure_marshal_VOID__STRING,
1575 G_TYPE_NONE, 1, G_TYPE_STRING);
1577 g_object_class_install_property (object_class,
1579 g_param_spec_object ("ui-manager",
1582 GTK_TYPE_UI_MANAGER,
1583 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1584 g_object_class_install_property (object_class,
1585 PROP_TOOLBARS_MODEL,
1586 g_param_spec_object ("model",
1589 EGG_TYPE_TOOLBARS_MODEL,
1590 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1591 g_object_class_install_property (object_class,
1593 g_param_spec_object ("selected",
1595 "Selected toolitem",
1597 G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1599 g_object_class_install_property (object_class,
1601 g_param_spec_string ("popup-path",
1605 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1607 g_object_class_install_property (object_class,
1609 g_param_spec_boolean ("edit-mode",
1613 G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1615 g_type_class_add_private (object_class, sizeof (EggEditableToolbarPrivate));
1619 egg_editable_toolbar_new (GtkUIManager *manager,
1620 const char *popup_path)
1622 return GTK_WIDGET (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
1623 "ui-manager", manager,
1624 "popup-path", popup_path,
1629 egg_editable_toolbar_new_with_model (GtkUIManager *manager,
1630 EggToolbarsModel *model,
1631 const char *popup_path)
1633 return GTK_WIDGET (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
1634 "ui-manager", manager,
1636 "popup-path", popup_path,
1641 egg_editable_toolbar_get_edit_mode (EggEditableToolbar *etoolbar)
1643 EggEditableToolbarPrivate *priv = etoolbar->priv;
1645 return priv->edit_mode > 0;
1649 egg_editable_toolbar_set_edit_mode (EggEditableToolbar *etoolbar,
1652 set_edit_mode (etoolbar, mode);
1653 g_object_notify (G_OBJECT (etoolbar), "edit-mode");
1657 egg_editable_toolbar_add_visibility (EggEditableToolbar *etoolbar,
1660 etoolbar->priv->visibility_paths = g_list_prepend
1661 (etoolbar->priv->visibility_paths, g_strdup (path));
1665 egg_editable_toolbar_show (EggEditableToolbar *etoolbar,
1668 EggEditableToolbarPrivate *priv = etoolbar->priv;
1669 EggToolbarsModel *model = priv->model;
1672 n_toolbars = egg_toolbars_model_n_toolbars (model);
1673 for (i = 0; i < n_toolbars; i++)
1675 const char *toolbar_name;
1677 toolbar_name = egg_toolbars_model_toolbar_nth (model, i);
1678 if (strcmp (toolbar_name, name) == 0)
1680 gtk_widget_show (get_dock_nth (etoolbar, i));
1686 egg_editable_toolbar_hide (EggEditableToolbar *etoolbar,
1689 EggEditableToolbarPrivate *priv = etoolbar->priv;
1690 EggToolbarsModel *model = priv->model;
1693 n_toolbars = egg_toolbars_model_n_toolbars (model);
1694 for (i = 0; i < n_toolbars; i++)
1696 const char *toolbar_name;
1698 toolbar_name = egg_toolbars_model_toolbar_nth (model, i);
1699 if (strcmp (toolbar_name, name) == 0)
1701 gtk_widget_hide (get_dock_nth (etoolbar, i));
1707 egg_editable_toolbar_set_fixed (EggEditableToolbar *etoolbar,
1708 GtkToolbar *toolbar)
1710 EggEditableToolbarPrivate *priv = etoolbar->priv;
1712 g_return_if_fail (!toolbar || GTK_IS_TOOLBAR (toolbar));
1714 if (priv->fixed_toolbar)
1716 unparent_fixed (etoolbar);
1717 g_object_unref (priv->fixed_toolbar);
1718 priv->fixed_toolbar = NULL;
1723 priv->fixed_toolbar = GTK_WIDGET (toolbar);
1724 gtk_toolbar_set_show_arrow (toolbar, FALSE);
1725 g_object_ref_sink (toolbar);
1728 update_fixed (etoolbar);
1731 #define DEFAULT_ICON_HEIGHT 20
1732 #define DEFAULT_ICON_WIDTH 0
1735 fake_expose_widget (GtkWidget *widget,
1738 GdkWindow *tmp_window;
1739 GdkEventExpose event;
1741 event.type = GDK_EXPOSE;
1742 event.window = pixmap;
1743 event.send_event = FALSE;
1744 event.area = widget->allocation;
1745 event.region = NULL;
1748 tmp_window = widget->window;
1749 widget->window = pixmap;
1750 gtk_widget_send_expose (widget, (GdkEvent *) &event);
1751 widget->window = tmp_window;
1754 /* We should probably experiment some more with this.
1755 * Right now the rendered icon is pretty good for most
1756 * themes. However, the icon is slightly large for themes
1757 * with large toolbar icons.
1760 new_pixbuf_from_widget (GtkWidget *widget)
1764 GtkRequisition requisition;
1765 GtkAllocation allocation;
1772 icon_width = DEFAULT_ICON_WIDTH;
1774 screen = gtk_widget_get_screen (widget);
1776 if (!gtk_icon_size_lookup_for_settings (gtk_settings_get_for_screen (screen),
1777 GTK_ICON_SIZE_LARGE_TOOLBAR,
1781 icon_height = DEFAULT_ICON_HEIGHT;
1784 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1786 gtk_container_add (GTK_CONTAINER (window), widget);
1787 gtk_widget_realize (window);
1788 gtk_widget_show (widget);
1789 gtk_widget_realize (widget);
1790 gtk_widget_map (widget);
1792 /* Gtk will never set the width or height of a window to 0. So setting the width to
1793 * 0 and than getting it will provide us with the minimum width needed to render
1794 * the icon correctly, without any additional window background noise.
1795 * This is needed mostly for pixmap based themes.
1797 gtk_window_set_default_size (GTK_WINDOW (window), icon_width, icon_height);
1798 gtk_window_get_size (GTK_WINDOW (window),&icon_width, &icon_height);
1800 gtk_widget_size_request (window, &requisition);
1803 allocation.width = icon_width;
1804 allocation.height = icon_height;
1805 gtk_widget_size_allocate (window, &allocation);
1806 gtk_widget_size_request (window, &requisition);
1808 /* Create a pixmap */
1809 visual = gtk_widget_get_visual (window);
1810 pixmap = gdk_pixmap_new (NULL, icon_width, icon_height, visual->depth);
1811 gdk_drawable_set_colormap (GDK_DRAWABLE (pixmap), gtk_widget_get_colormap (window));
1813 /* Draw the window */
1814 gtk_widget_ensure_style (window);
1815 g_assert (window->style);
1816 g_assert (window->style->font_desc);
1818 fake_expose_widget (window, pixmap);
1819 fake_expose_widget (widget, pixmap);
1821 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, icon_width, icon_height);
1822 gdk_pixbuf_get_from_drawable (pixbuf, pixmap, NULL, 0, 0, 0, 0, icon_width, icon_height);
1824 gtk_widget_destroy (window);
1830 new_separator_pixbuf ()
1832 GtkWidget *separator;
1835 separator = gtk_vseparator_new ();
1836 pixbuf = new_pixbuf_from_widget (separator);
1841 update_separator_image (GtkImage *image)
1843 GdkPixbuf *pixbuf = new_separator_pixbuf ();
1844 gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
1845 g_object_unref (pixbuf);
1849 style_set_cb (GtkWidget *widget,
1850 GtkStyle *previous_style,
1854 update_separator_image (image);
1859 _egg_editable_toolbar_new_separator_image (void)
1861 GtkWidget *image = gtk_image_new ();
1862 update_separator_image (GTK_IMAGE (image));
1863 g_signal_connect (G_OBJECT (image), "style_set",
1864 G_CALLBACK (style_set_cb), GTK_IMAGE (image));
1870 egg_editable_toolbar_get_model (EggEditableToolbar *etoolbar)
1872 return etoolbar->priv->model;