/* * Copyright (C) 2003, 2004 Marco Pesenti Gritti * Copyright (C) 2003, 2004, 2005 Christian Persch * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: egg-editable-toolbar.c,v 1.78 2005/12/16 16:18:08 csaavedr Exp $ */ #include "config.h" #include "egg-editable-toolbar.h" #include "egg-toolbars-model.h" #include "egg-toolbar-editor.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MIN_TOOLBAR_HEIGHT 20 #define EGG_ITEM_NAME "egg-item-name" #define ACTION_NAME_FORMAT_PREFIX "EETViewToolbar" #define ACTION_NAME_FORMAT ACTION_NAME_FORMAT_PREFIX "%u" #define ACTION_NAME_FORMAT_LENGTH strlen (ACTION_NAME_FORMAT) + 14 #define LABEL_WIDTH_CHARS 32 #define EGG_EDITABLE_TOOLBAR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EGG_TYPE_EDITABLE_TOOLBAR, EggEditableToolbarPrivate)) struct _EggEditableToolbarPrivate { GtkUIManager *manager; char *ui_path; GtkActionGroup *action_group; GtkActionGroup *toggles_action_group; guint ui_id; guint toggles_ui_id; EggToolbarsModel *model; guint edit_mode; GtkWidget *fixed_toolbar; GtkWidget *selected; GtkWidget *popup_menu; guint dnd_pending; GtkToolbar *dnd_toolbar; GtkToolItem *dnd_toolitem; guint save_hidden : 1; guint updating_view_toggles : 1; }; static const GtkTargetEntry dest_drag_types[] = { {EGG_TOOLBAR_ITEM_TYPE, GTK_TARGET_SAME_APP, 0}, }; enum { PROP_0, PROP_TOOLBARS_MODEL, PROP_UI_MANAGER, PROP_UI_PATH, PROP_SELECTED }; enum { ACTION_REQUEST, PREPARE_POPUP, LAST_SIGNAL }; static guint signals[LAST_SIGNAL]; static GObjectClass *parent_class = NULL; /* Helper functions */ static GtkAction * find_action (EggEditableToolbar *toolbar, const char *name) { EggEditableToolbarPrivate *priv = toolbar->priv; GList *l; GtkAction *action = NULL, *act; g_return_val_if_fail (name != NULL, NULL); for (l = gtk_ui_manager_get_action_groups (priv->manager); l != NULL; l = l->next) { act = gtk_action_group_get_action ((GtkActionGroup*) (l->data), name); if (act) { action = act; break; } } return action; } /* Dock and toolbar helper functions */ static int get_dock_position (EggEditableToolbar *toolbar, GtkWidget *dock) { GList *l; int result; l = gtk_container_get_children (GTK_CONTAINER (toolbar)); result = g_list_index (l, dock); g_list_free (l); return result; } static int get_toolbar_position (EggEditableToolbar *toolbar, GtkWidget *toolbar) { return get_dock_position (toolbar, toolbar->parent); } static int get_n_toolbars (EggEditableToolbar *toolbar) { GList *l; int result; l = gtk_container_get_children (GTK_CONTAINER (toolbar)); result = g_list_length (l); g_list_free (l); return result; } static GtkWidget * get_dock_nth (EggEditableToolbar *etoolbar, int position) { GList *l; GtkWidget *result; l = gtk_container_get_children (GTK_CONTAINER (etoolbar)); result = g_list_nth_data (l, position); g_list_free (l); return result; } static void set_dock_visible (EggEditableToolbar *toolbar, GtkWidget *dock, gboolean visible) { EggEditableToolbarPrivate *priv = toolbar->priv; g_object_set (dock, "visible", visible, NULL); if (priv->save_hidden) { EggTbModelFlags flags; int position; position = get_dock_position (toolbar, dock); flags = egg_toolbars_model_get_flags (priv->model, position); if (visible) { flags &= ~(EGG_TB_MODEL_HIDDEN); } else { flags |= (EGG_TB_MODEL_HIDDEN); } egg_toolbars_model_set_flags (etoolbar->priv->model, position, flags); } } static GtkWidget * get_toolbar_nth (EggEditableToolbar *toolbar, int position) { GList *l; GtkWidget *dock; GtkWidget *result; dock = get_dock_nth (toolbar, position); g_return_val_if_fail (dock != NULL, NULL); l = gtk_container_get_children (GTK_CONTAINER (dock)); result = GTK_WIDGET (l->data); g_list_free (l); return result; } /* View toggles menu management */ static int get_toolbar_position_from_action (GtkAction *action) { const char *name; int position; name = gtk_action_get_name (action); position = (int) g_ascii_strtoull (name + strlen (ACTION_NAME_FORMAT_PREFIX), NULL, 10); return position; } static void toolbar_view_menu_clean (EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; if (priv->toggles_ui_id != 0) { gtk_ui_manager_remove_ui (priv->manager, priv->toggles_ui_id); gtk_ui_manager_ensure_update (priv->manager); priv->toggles_ui_id = 0; } } static void toolbar_view_menu_rebuild (EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; char name[ACTION_NAME_FORMAT_LENGTH]; int n_toolbars, i; if (priv->ui_path == NULL) return; toolbar_view_menu_clean (toolbar); priv->toggles_ui_id = gtk_ui_manager_new_merge_id (priv->manager); n_toolbars = egg_toolbars_model_n_toolbars (priv->model); for (i = 0; i < n_toolbars; i++) { g_snprintf (name, sizeof (name), ACTION_NAME_FORMAT, i); gtk_ui_manager_add_ui (priv->manager, priv->toggles_ui_id, priv->ui_path, name, name, GTK_UI_MANAGER_MENUITEM, FALSE); } } static void view_action_toggled_cb (GtkAction *action, EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; EggTbModelFlags flags; GtkWidget *dock; int position; gboolean active; active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)); position = get_toolbar_position_from_action (action); flags = egg_toolbars_model_get_flags (priv->model, position); if (active) { flags &= ~EGG_TB_MODEL_HIDDEN; } else { flags |= EGG_TB_MODEL_HIDDEN; } egg_toolbars_model_set_flags (priv->model, position, flags); dock = get_dock_nth (toolbar, position); set_dock_visible (toolbar, dock, active); } static void add_view_action_for_toolbar (EggEditableToolbar *toolbar, int position) { EggEditableToolbarPrivate *priv = toolbar->priv; GtkToggleAction *action; char name[ACTION_NAME_FORMAT_LENGTH]; EggTbModelFlags flags; flags = egg_toolbars_model_get_flags (priv->model, position); g_snprintf (name, sizeof (name), ACTION_NAME_FORMAT, position); action = gtk_toggle_action_new (name, name, "STATUSBAR FIXME", NULL); gtk_toggle_action_set_active (action, (flags & EGG_TB_MODEL_HIDDEN) == 0); g_signal_connect (action, "toggled", G_CALLBACK (view_action_toggled_cb), toolbar); gtk_action_group_add_action (priv->toggles_action_group, GTK_ACTION (action)); g_object_unref (action); // sync_toolbar_name (toolbar, i); FIXME } static void toolbar_action_group_clean (EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; if (priv->toggles_action_group != NULL) { gtk_ui_manager_remove_action_group (priv->manager, priv->toggles_action_group); priv->toggles_action_group = NULL; } } static void connect_proxy_cb (GtkActionGroup *action_group, GtkAction *action, GtkWidget *proxy, gpointer dummy) { if (GTK_IS_MENU_ITEM (proxy)) { GtkLabel *label; label = GTK_LABEL (GTK_BIN (proxy)->child); gtk_label_set_use_underline (label, FALSE); gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_END); gtk_label_set_max_width_chars (label, LABEL_WIDTH_CHARS); } } static void toolbar_action_group_rebuild (EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; int n_toolbars, i; if (priv->ui_path == NULL) return; priv->toggles_action_group = gtk_action_group_new ("EETViewToolbarGroup"); g_signal_connect (priv->toggles_action_group, "connect-proxy", G_CALLBACK (connect_proxy_cb), NULL); gtk_ui_manager_insert_action_group (priv->manager, priv->toggles_action_group, -1); g_object_unref (priv->toggles_action_group); n_toolbars = egg_toolbars_model_n_toolbars (priv->model); for (i = 0; i < n_toolbars; i++) { add_view_action_for_toolbar (toolbar, i); } } /* DND helper functions */ static void drag_data_delete_cb (GtkWidget *widget, GdkDragContext *context, EggEditableToolbar *etoolbar) { int pos, toolbar_pos; widget = gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM); g_return_if_fail (widget != NULL); g_return_if_fail (EGG_IS_EDITABLE_TOOLBAR (etoolbar)); pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (widget->parent), GTK_TOOL_ITEM (widget)); toolbar_pos = get_toolbar_position (etoolbar, widget->parent); egg_toolbars_model_remove_item (etoolbar->priv->model, toolbar_pos, pos); } static void drag_begin_cb (GtkWidget *widget, GdkDragContext *context, EggEditableToolbar *etoolbar) { gtk_widget_hide (widget); } static void drag_end_cb (GtkWidget *widget, GdkDragContext *context, EggEditableToolbar *etoolbar) { gtk_widget_show (widget); } static void drag_data_get_cb (GtkWidget *widget, GdkDragContext *context, GtkSelectionData *selection_data, guint info, guint32 time, EggEditableToolbar *etoolbar) { EggToolbarsModel *model; const char *name; char *data; g_return_if_fail (EGG_IS_EDITABLE_TOOLBAR (etoolbar)); model = egg_editable_toolbar_get_model (etoolbar); name = g_object_get_data (G_OBJECT (widget), EGG_ITEM_NAME); if (name == NULL) { name = g_object_get_data (G_OBJECT (gtk_widget_get_parent (widget)), EGG_ITEM_NAME); g_return_if_fail (name != NULL); } data = egg_toolbars_model_get_data (model, selection_data->target, name); if (data != NULL) { gtk_selection_data_set (selection_data, selection_data->target, 8, (unsigned char *)data, strlen (data)); g_free (data); } } #if 0 static void egg_editable_toolbar_add_visibility_items (EggEditableToolbar *etoolbar, GtkMenu *popup) { EggToolbarsModel *model = etoolbar->priv->model; GtkCheckMenuItem *item; GtkWidget *dock; char buffer[40]; int n_toolbars, n_items, n_visible = 0; int i, j, k, l; g_return_if_fail (model != NULL); g_return_if_fail (etoolbar->priv->manager != NULL); n_toolbars = egg_toolbars_model_n_toolbars (model); for (i = 0; i < n_toolbars; i++) { dock = get_dock_nth (etoolbar, i); if (GTK_WIDGET_VISIBLE (dock)) n_visible++; } if (GTK_MENU_SHELL(popup)->children != NULL) { GtkWidget *separator = gtk_separator_menu_item_new (); gtk_widget_show (separator); gtk_menu_shell_append (GTK_MENU_SHELL (popup), separator); } for (i = 0; i < n_toolbars; i++) { n_items = egg_toolbars_model_n_items (model, i); for (j = 0; j < n_items; j++) { GValue value = { 0, }; GtkAction *action; const char *name; name = egg_toolbars_model_item_nth (model, i, j); if (name == NULL) continue; action = find_action (etoolbar, name); if (action == NULL) continue; g_value_init (&value, G_TYPE_STRING); g_object_get_property (G_OBJECT (action), "short-label", &value); name = g_value_get_string (&value); if (name == NULL) continue; /* FIXME FIXME eek! check if this is UTF-8 clean! */ if (j > 0) { if(k0 && buffer[l] != ',') l--; if(buffer[l] == ',') k = l + 2; else k = k-3; buffer[k++] = '.'; buffer[k++] = '.'; buffer[k++] = '.'; buffer[k] = 0; break; } buffer[k] = 0; g_value_unset (&value); } dock = get_dock_nth (etoolbar, i); item = GTK_CHECK_MENU_ITEM (gtk_check_menu_item_new_with_label (buffer)); gtk_check_menu_item_set_active (item, GTK_WIDGET_VISIBLE (dock)); gtk_widget_set_sensitive (GTK_WIDGET (item), (n_visible > 1 || !GTK_WIDGET_VISIBLE (dock))); gtk_widget_show (GTK_WIDGET (item)); gtk_menu_shell_append (GTK_MENU_SHELL (popup), GTK_WIDGET (item)); g_object_set_data (G_OBJECT (item), "egg-dock", dock); g_signal_connect (item, "toggled", G_CALLBACK (toggle_visibility_cb), etoolbar); } } #endif /* Context menu helper functions */ static void get_positions_from_selected (EggEditableToolbar *etoolbar, int *toolbar_position, int *item_position) { EggEditableToolbarPrivate *priv = etoolbar->priv; GtkWidget *item, *toolbar; item = gtk_widget_get_ancestor (priv->selected, GTK_TYPE_TOOL_ITEM); toolbar = gtk_widget_get_ancestor (item, GTK_TYPE_TOOLBAR); g_return_if_fail (toolbar); if (item_position) { if (item) { *item_position = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (item)); } else { *item_position = -1; } } if (toolbar_position) { *toolbar_position = get_toolbar_position (etoolbar, toolbar); } } static void prepare_popup (EggEditableToolbar *toolbar, int toolbar, int item) { EggEditableToolbarPrivate *priv = etoolbar->priv; g_return_if_fail (priv->popup_ui_id == 0); priv->popup_ui_id = gtk_ui_manager_new_merge_id (priv->manager); g_signal_emit (etoolbar, signals[PREPARE_POPUP], 0, priv->popup_ui_id, toolbar, item); } static void popup_menu_deactivate_cb (GtkWidgt *menu, EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv = etoolbar->priv; g_signal_handlers_disconnect_by_func (menu, G_CALLBACK (popup_menu_deactivate_cb), toolbar); priv->popup_menu = NULL; gtk_ui_manager_remove_ui (priv->popup_ui_id); priv->popup_ui_id = 0; } static void toolbar_popup_context_menu_cb (GtkWidget *widget, int x, int y, int button, EggEditableToolbar *etoolbar) { EggEditableToolbarPrivate *priv = etoolbar->priv; int i; i = get_toolbar_position (etoolbar, widget); prepare_popup (etoolbar, i, -1); priv->popup_menu = gtk_ui_manager_get_widget (priv->manager, "/EEToolbarPopup"); g_signal_connect (priv->popup_menu, "deactivate", G_CALLBACK (popup_menu_deactivate_cb), etoolbar); gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL, NULL /* FIXME ephy_gui_position_menu_on_toolbar */, NULL, button, gtk_get_current_event_time ()); } static gboolean item_button_press_event_cb (GtkWidget *widget, GdkEventButton *event, EggEditableToolbar *etoolbar) { EggEditableToolbarPrivate *priv = etoolbar->priv; GtkWidget *item, *toolbar; int tpos, ipos; if (event->button == 3) { item = gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM); g_return_val_if_fail (item, FALSE); toolbar = gtk_widget_get_ancestor (item, GTK_TYPE_TOOLBAR); g_return_val_if_fail (toolbar, FALSE); tpos = get_toolbar_position (etoolbar, toolbar); ipos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolbar), GTK_TOOL_ITEM (item)); prepare_popup (etoolbar, tpos, ipos); priv->popup_menu = gtk_ui_manager_get_widget (priv->manager, "/EEToolbarPopup"); g_signal_connect (priv->popup_menu, "deactivate", G_CALLBACK (popup_menu_deactivate_cb), etoolbar); gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL, NULL /* FIXME ephy_gui_position_menu_on_toolbar */, NULL, event->button, event->time); return TRUE; } return FALSE; } /* FIXME keyboard popups!!! */ /* Widget helper functions */ static void configure_item_sensitivity (GtkToolItem *item, EggEditableToolbar *etoolbar) { GtkAction *action; char *name; name = g_object_get_data (G_OBJECT (item), EGG_ITEM_NAME); action = name ? find_action (etoolbar, name) : NULL; if (action) { g_object_notify (G_OBJECT (action), "sensitive"); } gtk_tool_item_set_use_drag_window (item, (etoolbar->priv->edit_mode > 0) || GTK_IS_SEPARATOR_TOOL_ITEM (item)); } static void configure_item_cursor (GtkToolItem *item, EggEditableToolbar *etoolbar) { g_return_if_fail (etoolbar != NULL); g_return_if_fail (GTK_WIDGET(item)->window != NULL); if (etoolbar->priv->edit_mode > 0) { GdkCursor *cursor; cursor = gdk_cursor_new (GDK_HAND2); gdk_window_set_cursor (GTK_WIDGET(item)->window, cursor); gdk_cursor_unref (cursor); gtk_drag_source_set (GTK_WIDGET (item), GDK_BUTTON1_MASK, dest_drag_types, G_N_ELEMENTS (dest_drag_types), GDK_ACTION_MOVE); } else { gdk_window_set_cursor (GTK_WIDGET(item)->window, NULL); } } static void connect_widget_signals (GtkWidget *proxy, EggEditableToolbar *etoolbar) { if (GTK_IS_CONTAINER (proxy)) { gtk_container_foreach (GTK_CONTAINER (proxy), (GtkCallback) connect_widget_signals, (gpointer) etoolbar); } if (GTK_IS_TOOL_ITEM (proxy)) { g_signal_connect (proxy, "drag_begin", G_CALLBACK (drag_begin_cb), etoolbar); g_signal_connect (proxy, "drag_end", G_CALLBACK (drag_end_cb), etoolbar); g_signal_connect (proxy, "drag_data_get", G_CALLBACK (drag_data_get_cb), etoolbar); g_signal_connect (proxy, "drag_data_delete", G_CALLBACK (drag_data_delete_cb), etoolbar); } if (GTK_IS_BUTTON (proxy) || GTK_IS_TOOL_ITEM (proxy)) { g_signal_connect (proxy, "button-press-event", G_CALLBACK (item_button_press_event_cb), etoolbar); } } static void action_sensitive_cb (GtkAction *action, GParamSpec *pspec, GtkToolItem *item) { EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (gtk_widget_get_ancestor (GTK_WIDGET (item), EGG_TYPE_EDITABLE_TOOLBAR)); if (etoolbar->priv->edit_mode > 0) { gtk_widget_set_sensitive (GTK_WIDGET (item), TRUE); } } static GtkToolItem * create_item_from_action (EggEditableToolbar *etoolbar, const char *name) { GtkToolItem *item; g_return_val_if_fail (name != NULL, NULL); if (strcmp (name, "_separator") == 0) { item = gtk_separator_tool_item_new (); } else { GtkAction *action = find_action (etoolbar, name); if (action == NULL) return NULL; item = GTK_TOOL_ITEM (gtk_action_create_tool_item (action)); /* Normally done on-demand by the GtkUIManager, but no * such demand may have been made yet, so do it ourselves. */ gtk_action_set_accel_group (action, gtk_ui_manager_get_accel_group(etoolbar->priv->manager)); g_signal_connect_object (action, "notify::sensitive", G_CALLBACK (action_sensitive_cb), item, 0); } gtk_widget_show (GTK_WIDGET (item)); g_object_set_data_full (G_OBJECT (item), EGG_ITEM_NAME, g_strdup (name), g_free); return item; } static GtkToolItem * create_item_from_position (EggEditableToolbar *etoolbar, int toolbar_position, int position) { GtkToolItem *item; const char *name; name = egg_toolbars_model_item_nth (etoolbar->priv->model, toolbar_position, position); item = create_item_from_action (etoolbar, name); return item; } static void toolbar_drag_data_received_cb (GtkToolbar *toolbar, GdkDragContext *context, gint x, gint y, GtkSelectionData *selection_data, guint info, guint time, EggEditableToolbar *etoolbar) { /* This function can be called for two reasons * * (1) drag_motion() needs an item to pass to * gtk_toolbar_set_drop_highlight_item(). We can * recognize this case by etoolbar->priv->pending being TRUE * We should just create an item and return. * * (2) The drag has finished, and drag_drop() wants us to * actually add a new item to the toolbar. */ GdkAtom type = selection_data->type; const char *data = (char *)selection_data->data; int ipos = -1; char *name = NULL; /* Find out where the drop is occuring, and the name of what is being dropped. */ if (selection_data->length >= 0) { ipos = gtk_toolbar_get_drop_index (toolbar, x, y); name = egg_toolbars_model_get_name (etoolbar->priv->model, type, data, FALSE); } /* If we just want a highlight item, then . */ if (etoolbar->priv->dnd_pending > 0) { etoolbar->priv->dnd_pending--; if (name != NULL && etoolbar->priv->dnd_toolbar == toolbar) { etoolbar->priv->dnd_toolitem = create_item_from_action (etoolbar, name); gtk_toolbar_set_drop_highlight_item (etoolbar->priv->dnd_toolbar, etoolbar->priv->dnd_toolitem, ipos); } } else { gtk_toolbar_set_drop_highlight_item (toolbar, NULL, 0); etoolbar->priv->dnd_toolbar = NULL; etoolbar->priv->dnd_toolitem = NULL; /* If we don't have a name to use yet, try to create one. */ if (name == NULL && selection_data->length >= 0) { name = egg_toolbars_model_get_name (etoolbar->priv->model, type, data, TRUE); } if (name != NULL) { gint tpos = get_toolbar_position (etoolbar, GTK_WIDGET (toolbar)); egg_toolbars_model_add_item (etoolbar->priv->model, tpos, ipos, name); gtk_drag_finish (context, TRUE, context->action == GDK_ACTION_MOVE, time); } else { gtk_drag_finish (context, FALSE, context->action == GDK_ACTION_MOVE, time); } } g_free (name); } static gboolean toolbar_drag_drop_cb (GtkToolbar *toolbar, GdkDragContext *context, gint x, gint y, guint time, EggEditableToolbar *etoolbar) { GdkAtom target; target = gtk_drag_dest_find_target (GTK_WIDGET (toolbar), context, NULL); if (target != GDK_NONE) { gtk_drag_get_data (GTK_WIDGET (toolbar), context, target, time); return TRUE; } return FALSE; } static gboolean toolbar_drag_motion_cb (GtkToolbar *toolbar, GdkDragContext *context, gint x, gint y, guint time, EggEditableToolbar *etoolbar) { GdkAtom target = gtk_drag_dest_find_target (GTK_WIDGET (toolbar), context, NULL); if (target == GDK_NONE) { gdk_drag_status (context, 0, time); return FALSE; } /* Make ourselves the current dnd toolbar, and request a highlight item. */ if (etoolbar->priv->dnd_toolbar != toolbar) { etoolbar->priv->dnd_toolbar = toolbar; etoolbar->priv->dnd_toolitem = NULL; etoolbar->priv->dnd_pending++; gtk_drag_get_data (GTK_WIDGET (toolbar), context, target, time); } /* If a highlight item is available, use it. */ else if (etoolbar->priv->dnd_toolitem) { gint ipos = gtk_toolbar_get_drop_index (etoolbar->priv->dnd_toolbar, x, y); gtk_toolbar_set_drop_highlight_item (etoolbar->priv->dnd_toolbar, etoolbar->priv->dnd_toolitem, ipos); } gdk_drag_status (context, context->suggested_action, time); return TRUE; } static void toolbar_drag_leave_cb (GtkToolbar *toolbar, GdkDragContext *context, guint time, EggEditableToolbar *etoolbar) { gtk_toolbar_set_drop_highlight_item (toolbar, NULL, 0); /* If we were the current dnd toolbar target, remove the item. */ if (etoolbar->priv->dnd_toolbar == toolbar) { etoolbar->priv->dnd_toolbar = NULL; etoolbar->priv->dnd_toolitem = NULL; } } static void configure_drag_dest (EggEditableToolbar *etoolbar, GtkToolbar *toolbar) { EggToolbarsItemType *type; GtkTargetList *targets; GList *list; /* Make every toolbar able to receive drag-drops. */ gtk_drag_dest_set (GTK_WIDGET (toolbar), 0, dest_drag_types, G_N_ELEMENTS (dest_drag_types), GDK_ACTION_MOVE | GDK_ACTION_COPY); /* Add any specialist drag-drop abilities. */ targets = gtk_drag_dest_get_target_list (GTK_WIDGET (toolbar)); list = egg_toolbars_model_get_types (etoolbar->priv->model); while (list) { type = list->data; if (type->new_name != NULL || type->get_name != NULL) gtk_target_list_add (targets, type->type, 0, 0); list = list->next; } } static GtkWidget * create_dock (EggEditableToolbar *etoolbar) { GtkWidget *toolbar, *hbox; hbox = gtk_hbox_new (0, FALSE); toolbar = gtk_toolbar_new (); gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE); gtk_widget_show (toolbar); gtk_box_pack_start (GTK_BOX (hbox), toolbar, TRUE, TRUE, 0); g_signal_connect (toolbar, "drag_drop", G_CALLBACK (toolbar_drag_drop_cb), etoolbar); g_signal_connect (toolbar, "drag_motion", G_CALLBACK (toolbar_drag_motion_cb), etoolbar); g_signal_connect (toolbar, "drag_leave", G_CALLBACK (toolbar_drag_leave_cb), etoolbar); g_signal_connect (toolbar, "drag_data_received", G_CALLBACK (toolbar_drag_data_received_cb), etoolbar); g_signal_connect (toolbar, "popup-context-menu", G_CALLBACK (toolbar_popup_context_menu_cb), etoolbar); configure_drag_dest (etoolbar, GTK_TOOLBAR (toolbar)); return hbox; } static void set_fixed_style (EggEditableToolbar *t, GtkToolbarStyle style) { g_return_if_fail (GTK_IS_TOOLBAR (t->priv->fixed_toolbar)); gtk_toolbar_set_style (GTK_TOOLBAR (t->priv->fixed_toolbar), style == GTK_TOOLBAR_ICONS ? GTK_TOOLBAR_BOTH_HORIZ : style); } static void unset_fixed_style (EggEditableToolbar *t) { g_return_if_fail (GTK_IS_TOOLBAR (t->priv->fixed_toolbar)); gtk_toolbar_unset_style (GTK_TOOLBAR (t->priv->fixed_toolbar)); } static void toolbar_changed_cb (EggToolbarsModel *model, int position, EggEditableToolbar *etoolbar) { GtkWidget *toolbar; EggTbModelFlags flags; GtkToolbarStyle style; flags = egg_toolbars_model_get_flags (model, position); toolbar = get_toolbar_nth (etoolbar, position); if (flags & EGG_TB_MODEL_ICONS) { style = GTK_TOOLBAR_ICONS; } else if (flags & EGG_TB_MODEL_TEXT) { style = GTK_TOOLBAR_TEXT; } else if (flags & EGG_TB_MODEL_BOTH) { style = GTK_TOOLBAR_BOTH; } else if (flags & EGG_TB_MODEL_BOTH_HORIZ) { style = GTK_TOOLBAR_BOTH_HORIZ; } else { gtk_toolbar_unset_style (GTK_TOOLBAR (toolbar)); if (position == 0 && etoolbar->priv->fixed_toolbar) { unset_fixed_style (etoolbar); } return; } gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), style); if (position == 0 && etoolbar->priv->fixed_toolbar) { set_fixed_style (etoolbar, style); } /* FIXME: update the view action label! */ } static void unparent_fixed (EggEditableToolbar *etoolbar) { GtkWidget *toolbar, *dock; g_return_if_fail (GTK_IS_TOOLBAR (etoolbar->priv->fixed_toolbar)); toolbar = etoolbar->priv->fixed_toolbar; dock = get_dock_nth (etoolbar, 0); if (dock && toolbar->parent != NULL) { gtk_container_remove (GTK_CONTAINER (dock), toolbar); } } static void update_fixed (EggEditableToolbar *etoolbar) { GtkWidget *toolbar, *dock; if (!etoolbar->priv->fixed_toolbar) return; toolbar = etoolbar->priv->fixed_toolbar; dock = get_dock_nth (etoolbar, 0); if (dock && toolbar && toolbar->parent == NULL) { gtk_box_pack_end (GTK_BOX (dock), toolbar, FALSE, TRUE, 0); gtk_widget_show (toolbar); gtk_widget_set_size_request (dock, -1, -1); gtk_widget_queue_resize_no_redraw (dock); } } static void toolbar_added_cb (EggToolbarsModel *model, int position, EggEditableToolbar *etoolbar) { GtkWidget *dock; dock = create_dock (etoolbar); if ((egg_toolbars_model_get_flags (model, position) & EGG_TB_MODEL_HIDDEN) == 0) gtk_widget_show (dock); gtk_widget_set_size_request (dock, -1, MIN_TOOLBAR_HEIGHT); gtk_box_pack_start (GTK_BOX (etoolbar), dock, TRUE, TRUE, 0); gtk_box_reorder_child (GTK_BOX (etoolbar), dock, position); gtk_widget_show_all (dock); update_fixed (etoolbar); /* this relies on the fact that position == n_toolbars here!! */ add_view_action_for_toolbar (etoolbar, position); toolbar_view_menu_clean (etoolbar); toolbar_view_menu_rebuild (etoolbar); } static void toolbar_removed_cb (EggToolbarsModel *model, int position, EggEditableToolbar *etoolbar) { GtkWidget *dock; int i; if (position == 0 && etoolbar->priv->fixed_toolbar != NULL) { unparent_fixed (etoolbar); } dock = get_dock_nth (etoolbar, position); gtk_widget_destroy (dock); dock = NULL; for (i = egg_toolbars_model_n_toolbars (model)-1; i >= 0; i--) { dock = get_dock_nth (etoolbar, i); if (GTK_WIDGET_VISIBLE (dock)) break; } if (i < 0 && dock != NULL) { set_dock_visible (etoolbar, dock, TRUE); } update_fixed (etoolbar); } static void item_added_cb (EggToolbarsModel *model, int tpos, int ipos, EggEditableToolbar *etoolbar) { GtkWidget *dock; GtkWidget *toolbar; GtkToolItem *item; toolbar = get_toolbar_nth (etoolbar, tpos); item = create_item_from_position (etoolbar, tpos, ipos); if (item == NULL) return; gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, ipos); connect_widget_signals (GTK_WIDGET (item), etoolbar); configure_item_cursor (item, etoolbar); configure_item_sensitivity (item, etoolbar); dock = get_dock_nth (etoolbar, tpos); gtk_widget_set_size_request (dock, -1, -1); gtk_widget_queue_resize_no_redraw (dock); } static void item_removed_cb (EggToolbarsModel *model, int toolbar_position, int position, EggEditableToolbar *etoolbar) { GtkWidget *toolbar; GtkWidget *item; toolbar = get_toolbar_nth (etoolbar, toolbar_position); item = GTK_WIDGET (gtk_toolbar_get_nth_item (GTK_TOOLBAR (toolbar), position)); g_return_if_fail (item != NULL); if (item == priv->selected) { /* FIXME */ } gtk_container_remove (GTK_CONTAINER (toolbar), item); } static void egg_editable_toolbar_build (EggEditableToolbar *etoolbar) { int i, l, n_items, n_toolbars; EggToolbarsModel *model = etoolbar->priv->model; g_return_if_fail (model != NULL); g_return_if_fail (etoolbar->priv->manager != NULL); n_toolbars = egg_toolbars_model_n_toolbars (model); for (i = 0; i < n_toolbars; i++) { GtkWidget *toolbar, *dock; dock = create_dock (etoolbar); if ((egg_toolbars_model_get_flags (model, i) & EGG_TB_MODEL_HIDDEN) == 0) gtk_widget_show (dock); gtk_box_pack_start (GTK_BOX (etoolbar), dock, TRUE, TRUE, 0); toolbar = get_toolbar_nth (etoolbar, i); n_items = egg_toolbars_model_n_items (model, i); for (l = 0; l < n_items; l++) { GtkToolItem *item; item = create_item_from_position (etoolbar, i, l); if (item) { gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, l); connect_widget_signals (GTK_WIDGET (item), etoolbar); configure_item_sensitivity (item, etoolbar); } else { egg_toolbars_model_remove_item (model, i, l); l--; n_items--; } } if (n_items == 0) { gtk_widget_set_size_request (dock, -1, MIN_TOOLBAR_HEIGHT); } } update_fixed (etoolbar); /* apply styles */ for (i = 0; i < n_toolbars; i ++) { toolbar_changed_cb (model, i, etoolbar); } } static void egg_editable_toolbar_disconnect_model (EggEditableToolbar *toolbar) { EggToolbarsModel *model = toolbar->priv->model; g_signal_handlers_disconnect_by_func (model, G_CALLBACK (item_added_cb), toolbar); g_signal_handlers_disconnect_by_func (model, G_CALLBACK (item_removed_cb), toolbar); g_signal_handlers_disconnect_by_func (model, G_CALLBACK (toolbar_added_cb), toolbar); g_signal_handlers_disconnect_by_func (model, G_CALLBACK (toolbar_removed_cb), toolbar); g_signal_handlers_disconnect_by_func (model, G_CALLBACK (toolbar_changed_cb), toolbar); } static void egg_editable_toolbar_deconstruct (EggEditableToolbar *toolbar) { EggToolbarsModel *model = toolbar->priv->model; GList *children; g_return_if_fail (model != NULL); if (toolbar->priv->fixed_toolbar) { unset_fixed_style (toolbar); unparent_fixed (toolbar); } children = gtk_container_get_children (GTK_CONTAINER (toolbar)); g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL); g_list_free (children); } static const char popup_ui_string[] = "" " "" "" "" "" ""; #define DEFAULT_ICON_HEIGHT 20 #define DEFAULT_ICON_WIDTH 0 static void fake_expose_widget (GtkWidget *widget, GdkPixmap *pixmap) { GdkWindow *tmp_window; GdkEventExpose event; event.type = GDK_EXPOSE; event.window = pixmap; event.send_event = FALSE; event.area = widget->allocation; event.region = NULL; event.count = 0; tmp_window = widget->window; widget->window = pixmap; gtk_widget_send_expose (widget, (GdkEvent *) &event); widget->window = tmp_window; } /* We should probably experiment some more with this. * Right now the rendered icon is pretty good for most * themes. However, the icon is slightly large for themes * with large toolbar icons. */ static GdkPixbuf * new_pixbuf_from_widget (GtkWidget *widget) { GtkWidget *window; GdkPixbuf *pixbuf; GtkRequisition requisition; GtkAllocation allocation; GdkPixmap *pixmap; GdkVisual *visual; gint icon_width; gint icon_height; icon_width = DEFAULT_ICON_WIDTH; if (!gtk_icon_size_lookup_for_settings (gtk_settings_get_default (), GTK_ICON_SIZE_LARGE_TOOLBAR, NULL, &icon_height)) { icon_height = DEFAULT_ICON_HEIGHT; } window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_container_add (GTK_CONTAINER (window), widget); gtk_widget_realize (window); gtk_widget_show (widget); gtk_widget_realize (widget); gtk_widget_map (widget); /* Gtk will never set the width or height of a window to 0. So setting the width to * 0 and than getting it will provide us with the minimum width needed to render * the icon correctly, without any additional window background noise. * This is needed mostly for pixmap based themes. */ gtk_window_set_default_size (GTK_WINDOW (window), icon_width, icon_height); gtk_window_get_size (GTK_WINDOW (window),&icon_width, &icon_height); gtk_widget_size_request (window, &requisition); allocation.x = 0; allocation.y = 0; allocation.width = icon_width; allocation.height = icon_height; gtk_widget_size_allocate (window, &allocation); gtk_widget_size_request (window, &requisition); /* Create a pixmap */ visual = gtk_widget_get_visual (window); pixmap = gdk_pixmap_new (NULL, icon_width, icon_height, visual->depth); gdk_drawable_set_colormap (GDK_DRAWABLE (pixmap), gtk_widget_get_colormap (window)); /* Draw the window */ gtk_widget_ensure_style (window); g_assert (window->style); g_assert (window->style->font_desc); fake_expose_widget (window, pixmap); fake_expose_widget (widget, pixmap); pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, icon_width, icon_height); gdk_pixbuf_get_from_drawable (pixbuf, pixmap, NULL, 0, 0, 0, 0, icon_width, icon_height); gtk_widget_destroy (window); return pixbuf; } static GdkPixbuf * new_separator_pixbuf () { GtkWidget *separator; GdkPixbuf *pixbuf; separator = gtk_vseparator_new (); pixbuf = new_pixbuf_from_widget (separator); return pixbuf; } static void update_separator_image (GtkImage *image) { GdkPixbuf *pixbuf = new_separator_pixbuf (); gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf); g_object_unref (pixbuf); } static gboolean style_set_cb (GtkWidget *widget, GtkStyle *previous_style, GtkImage *image) { update_separator_image (image); return FALSE; } GtkWidget * _egg_editable_toolbar_new_separator_image (void) { GtkWidget *image = gtk_image_new (); update_separator_image (GTK_IMAGE (image)); g_signal_connect (G_OBJECT (image), "style_set", G_CALLBACK (style_set_cb), GTK_IMAGE (image)); return image; } /* Action helper functions */ static void move_item_cb (GtkAction *action, EggEditableToolbar *toolbar) { GtkWidget *toolitem; GtkTargetList *list; toolitem = gtk_widget_get_ancestor (egg_editable_toolbar_get_selected (toolbar), GTK_TYPE_TOOL_ITEM); list = gtk_target_list_new (dest_drag_types, G_N_ELEMENTS (dest_drag_types)); /* FIXME remove this crap now that the gtk+ bug is fixed */ GdkEvent *realevent = gtk_get_current_event(); GdkEventMotion event; event.type = GDK_MOTION_NOTIFY; event.window = realevent->any.window; event.send_event = FALSE; event.axes = NULL; event.time = gdk_event_get_time (realevent); gdk_event_get_state (realevent, &event.state); gdk_event_get_coords (realevent, &event.x, &event.y); gdk_event_get_root_coords (realevent, &event.x_root, &event.y_root); gtk_drag_begin (toolitem, list, GDK_ACTION_MOVE, 1, (GdkEvent *)&event); gtk_target_list_unref (list); } static void remove_item_cb (GtkAction *action, EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; GtkWidget *toolitem; int pos, toolbar_pos; toolitem = gtk_widget_get_ancestor (egg_editable_toolbar_get_selected (toolbar), GTK_TYPE_TOOL_ITEM); toolbar_pos = get_toolbar_position (etoolbar, toolitem->parent); pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (toolitem->parent), GTK_TOOL_ITEM (toolitem)); egg_toolbars_model_remove_item (toolbar->priv->model, toolbar_pos, pos); if (egg_toolbars_model_n_items (etoolbar->priv->model, toolbar_pos) == 0) { egg_toolbars_model_remove_toolbar (etoolbar->priv->model, toolbar_pos); } } static void remove_toolbar_cb (GtkAction *action, EggEditableToolbar *etoolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; GtkWidget *selected, *toolbar; int toolbar_pos; /* FIXME confirmation?? */ selected = egg_editable_toolbar_get_selected (toolbar); toolbar = gtk_widget_get_ancestor (selected, GTK_TYPE_TOOLBAR); toolbar_pos = get_toolbar_position (toolbar, toolbar); egg_toolbars_model_remove_toolbar (priv->model, toolbar_pos); } static const GtkActionEntry popup_actions[] = { { "EETMoveItem", NULL, N_("_Move on Toolbar"), NULL, N_("Move the selected item on the toolbar"), G_CALLBACK (move_item_cb) }, { "EETRemoveItem", GTK_STOCK_REMOVE, N_("_Remove from Toolbar"), NULL, N_("Remove the selected item from the toolbar"), G_CALLBACK (remove_item_cb) }, { "EETRemoveToolbar", GTK_STOCK_REMOVE, N_("_Remove Toolbar"), NULL, N_("Remove the selected toolbar"), G_CALLBACK (remove_toolbar_cb) } }; /* Public API */ EggToolbarsModel * egg_editable_toolbar_get_model (EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; return priv->model; } void egg_editable_toolbar_set_model (EggEditableToolbar *toolbar, EggToolbarsModel *model) { EggEditableToolbarPrivate *priv = toolbar->priv; if (priv->model == model) return; if (priv->model) { toolbar_view_menu_clean (toolbar); toolbar_action_group_clean (toolbar); egg_editable_toolbar_disconnect_model (toolbar); egg_editable_toolbar_deconstruct (toolbar); g_object_unref (priv->model); } priv->model = g_object_ref (model); toolbar_action_group_rebuild (toolbar); toolbar_view_menu_rebuild (toolbar); egg_editable_toolbar_build (toolbar); g_signal_connect (model, "item_added", G_CALLBACK (item_added_cb), toolbar); g_signal_connect (model, "item_removed", G_CALLBACK (item_removed_cb), toolbar); g_signal_connect (model, "toolbar_added", G_CALLBACK (toolbar_added_cb), toolbar); g_signal_connect (model, "toolbar_removed", G_CALLBACK (toolbar_removed_cb), toolbar); g_signal_connect (model, "toolbar_changed", G_CALLBACK (toolbar_changed_cb), toolbar); } /* Helpers */ /* Class implementation */ static void egg_editable_toolbar_init (EggEditableToolbar *toolbar) { EggEditableToolbarPrivate *priv; priv = toolbar->priv = EGG_EDITABLE_TOOLBAR_GET_PRIVATE (etoolbar); priv->save_hidden = TRUE; } static GObject * egg_editable_toolbar_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_params) { GObject *object; EggEditableToolbar *toolbar; EggEditableToolbarPrivate *priv; object = parent_class->constructor (type, n_construct_properties, construct_params); toolbar = EGG_EDITABLE_TOOLBAR (toolbar); priv = toolbar->priv; g_assert (priv->model); g_assert (priv->manager); priv->action_group = gtk_action_group_new ("ETTPopupActions"); gtk_action_group_add_actions (priv->action_group, popup_actions, G_N_ELEMENTS (popup_actions), toolbar); gtk_ui_manager_insert_action_group (manager, priv->action_group, -1); g_object_unref (priv->action_group); priv->ui_id = gtk_ui_manager_add_ui_from_string (priv->manager, popup_ui_string, -1, NULL); return object; } static void egg_editable_toolbar_dispose (GObject *object) { EggEditableToolbar *toolbar = EGG_EDITABLE_TOOLBAR (object); EggEditableToolbarPrivate *priv = toolbar->priv; if (priv->fixed_toolbar != NULL) { g_object_unref (priv->fixed_toolbar); priv->fixed_toolbar = NULL; } if (priv->popup_menu) { gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->popup_menu)); priv->popup_menu = NULL; g_assert (priv->popup_ui_id == 0); } if (priv->manager != NULL) { toolbar_view_menu_clean (toolbar); toolbar_action_group_clean (toolbar); if (priv->ui_id != 0) { gtk_ui_manager_remove_ui (priv->manager, priv->ui_id); priv->ui_id = 0; } if (priv->action_group != NULL) { gtk_ui_manager_remove_action_group (priv->manager, priv->action_group); priv->action_group = NULL; } g_object_unref (priv->manager); priv->manager = NULL; } if (priv->ui_path != NULL) { g_free (priv->ui_path); priv->ui_path = NULL; } if (priv->model != NULL) { egg_editable_toolbar_disconnect_model (toolbar); g_object_unref (priv->model); priv->model = NULL; } G_OBJECT_CLASS (parent_class)->dispose (object); } static void egg_editable_toolbar_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { EggEditableToolbar *toolbar = EGG_EDITABLE_TOOLBAR (object); EggEditableToolbarPrivate *priv = toolbar->priv; switch (prop_id) { case PROP_UI_MANAGER: priv->manager = g_value_dup_object (value); break; case PROP_UI_PATH: priv->ui_path = g_value_dup_string (value); break; case PROP_TOOLBARS_MODEL: egg_editable_toolbar_set_model (toolbar, g_value_get_object (value)); break; case PROP_SELECTED: egg_editable_toolbar_set_selected (etoolbar, g_value_get_object (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } } static void egg_editable_toolbar_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { EggEditableToolbar *toolbar = EGG_EDITABLE_TOOLBAR (object); EggEditableToolbarPrivate *priv = toolbar->priv; switch (prop_id) { case PROP_UI_MANAGER: g_value_set_object (value, priv->manager); break; case PROP_UI_PATH: g_value_set_string (value, priv->ui_path); case PROP_TOOLBARS_MODEL: g_value_set_object (value, priv->model); break; case PROP_SELECTED: g_value_set_object (value, etoolbar->priv->selected); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } } static void egg_editable_toolbar_class_init (EggEditableToolbarClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); parent_class = g_type_class_peek_parent (klass); object_class->constructor = egg_editable_toolbar_constructor; object_class->dispose = egg_editable_toolbar_dispose; object_class->set_property = egg_editable_toolbar_set_property; object_class->get_property = egg_editable_toolbar_get_property; signals[ACTION_REQUEST] = g_signal_new ("action-request", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (EggEditableToolbarClass, action_request), NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING); signals[PREPARE_POPUP] = g_signal_new ("prepare-popup", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (EggEditableToolbarClass, prepare_popup), NULL, NULL, egg_marshal_VOID__UINT_INT_INT, G_TYPE_NONE, 3, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INT); g_object_class_install_property (object_class, PROP_UI_MANAGER, g_param_spec_object ("ui-manager", "UI-Mmanager", "UI Manager", GTK_TYPE_UI_MANAGER, G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); g_object_class_install_property (object_class, PROP_TOOLBARS_MODEL, g_param_spec_object ("model", "Model", "Toolbars Model", EGG_TYPE_TOOLBARS_MODEL, G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); g_object_class_install_property (object_class, PROP_SELECTED, g_param_spec_object ("selected", "Selected", "Selected toolitem", GTK_TYPE_TOOL_ITEM, G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); g_object_class_install_property (object_class, PROP_UI_PATH, g_param_spec_string ("ui-path", "ui-path", "ui-path", NULL, G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); g_type_class_add_private (object_class, sizeof (EggEditableToolbarPrivate)); } /* public API */ GType egg_editable_toolbar_get_type (void) { static GType type = 0; if (G_UNLIKELY (type == 0)) { static const GTypeInfo our_info = { sizeof (EggEditableToolbarClass), NULL, /* base_init */ NULL, /* base_finalize */ (GClassInitFunc) egg_editable_toolbar_class_init, NULL, NULL, /* class_data */ sizeof (EggEditableToolbar), 0, /* n_preallocs */ (GInstanceInitFunc) egg_editable_toolbar_init }; type = g_type_register_static (GTK_TYPE_VBOX, "EggEditableToolbar", &our_info, 0); } return type; } GtkWidget * egg_editable_toolbar_new (GtkUIManager *manager, const char *ui_path) { return GTK_WIDGET (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR, "ui-manager", manager, "ui-path", ui_path, NULL)); } GtkWidget * egg_editable_toolbar_new_with_model (GtkUIManager *manager, EggToolbarsModel *model, const char *ui_path) { return GTK_WIDGET (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR, "ui-manager", manager, "model", model, "ui-path", ui_path, NULL)); } gboolean egg_editable_toolbar_get_edit_mode (EggEditableToolbar *etoolbar) { EggEditableToolbarPrivate *priv = etoolbar->priv; return priv->edit_mode > 0; } void egg_editable_toolbar_set_edit_mode (EggEditableToolbar *etoolbar, gboolean mode) { EggEditableToolbarPrivate *priv = etoolbar->priv; int i, l, n_items; i = priv->edit_mode; if (mode) { priv->edit_mode++; } else { g_return_if_fail (priv->edit_mode > 0); priv->edit_mode--; } i *= priv->edit_mode; if (i == 0) { for (i = get_n_toolbars (toolbar)-1; i >= 0; i--) { GtkWidget *toolbar; toolbar = get_toolbar_nth (toolbar, i); n_items = gtk_toolbar_get_n_items (GTK_TOOLBAR (toolbar)); if (n_items == 0 && priv->edit_mode == 0) { egg_toolbars_model_remove_toolbar (priv->model, i); } else { for (l = 0; l < n_items; l++) { GtkToolItem *item; item = gtk_toolbar_get_nth_item (GTK_TOOLBAR (toolbar), l); configure_item_cursor (item, etoolbar); configure_item_sensitivity (item, etoolbar); } } } } } void egg_editable_toolbar_show (EggEditableToolbar *etoolbar, const char *name) { EggEditableToolbarPrivate *priv = etoolbar->priv; EggToolbarsModel *model = priv->model; int i, n_toolbars; n_toolbars = egg_toolbars_model_n_toolbars (model); for (i = 0; i < n_toolbars; i++) { const char *toolbar_name; toolbar_name = egg_toolbars_model_toolbar_nth (model, i); if (strcmp (toolbar_name, name) == 0) { gtk_widget_show (get_dock_nth (etoolbar, i)); } } } void egg_editable_toolbar_hide (EggEditableToolbar *etoolbar, const char *name) { EggEditableToolbarPrivate *priv = etoolbar->priv; EggToolbarsModel *model = priv->model; int i, n_toolbars; n_toolbars = egg_toolbars_model_n_toolbars (model); for (i = 0; i < n_toolbars; i++) { const char *toolbar_name; toolbar_name = egg_toolbars_model_toolbar_nth (model, i); if (strcmp (toolbar_name, name) == 0) { gtk_widget_hide (get_dock_nth (etoolbar, i)); } } } void egg_editable_toolbar_set_fixed (EggEditableToolbar *toolbar, GtkToolbar *fixed_toolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; g_return_if_fail (!fixed_toolbar || GTK_IS_TOOLBAR (fixed_toolbar)); if (toolbar->priv->fixed_toolbar) { unparent_fixed (toolbar); g_object_unref (toolbar->priv->fixed_toolbar); toolbar->priv->fixed_toolbar = NULL; } if (fixed_toolbar) { toolbar->priv->fixed_toolbar = GTK_WIDGET (fixed_toolbar); gtk_toolbar_set_show_arrow (fixed_toolbar, FALSE); g_object_ref (fixed_toolbar); gtk_object_sink (GTK_OBJECT (fixed_toolbar)); } update_fixed (toolbar); } GtkWidget * egg_editable_toolbar_get_selected (EggEditableToolbar *etoolbar) { EggEditableToolbarPrivate *priv = toolbar->priv; return priv->selected; } void egg_editable_toolbar_set_selected (EggEditableToolbar *toolbar, GtkWidget *widget) { EggEditableToolbarPrivate *priv = toolbar->priv; GtkActionGroup *action_group = priv->action_group; GtkAction *action; gboolean is_toolitem, is_toolbar; priv->selected = widget; is_toolitem = gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM) != 0; is_toolbar = gtk_widget_get_ancestor (widget, GTK_TYPE_TOOLBAR) != 0; // FIXME this is so wrong! action = gtk_action_group_get_action (action_group, "EETMoveItem"); gtk_action_set_visible (action, is_toolitem); action = gtk_action_group_get_action (action_group, "EETRemoveItem"); gtk_action_set_visible (action, is_toolitem); action = gtk_action_group_get_action (action_group, "EETRemoveToolbar"); gtk_action_set_visible (action, is_toolbar && (priv->edit_mode > 0)); }