diff options
Diffstat (limited to 'libnautilus/nautilus-clipboard.c')
-rw-r--r-- | libnautilus/nautilus-clipboard.c | 405 |
1 files changed, 119 insertions, 286 deletions
diff --git a/libnautilus/nautilus-clipboard.c b/libnautilus/nautilus-clipboard.c index 8a5ebc667..551253624 100644 --- a/libnautilus/nautilus-clipboard.c +++ b/libnautilus/nautilus-clipboard.c @@ -1,5 +1,7 @@ /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ + /* nautilus-clipboard.c + * * Nautilus Clipboard support. For now, routines to support component cut * and paste. * @@ -21,329 +23,160 @@ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * - * Author: Rebecca Schulman <rebecka@eazel.com> + * Authors: Rebecca Schulman <rebecka@eazel.com>, + * Darin Adler <darin@eazel.com> */ -#include <config.h> -#include <gtk/gtksignal.h> -#include <gtk/gtkwidget.h> -#include <gtk/gtkeditable.h> -#include <gtk/gtkscrolledwindow.h> -#include <gnome.h> -#include <bonobo/bonobo-control.h> +#include <config.h> #include "nautilus-clipboard.h" - -#define MENU_PATH_CUT "/Edit/Cut" -#define MENU_PATH_COPY "/Edit/Copy" -#define MENU_PATH_PASTE "/Edit/Paste" -#define MENU_PATH_CLEAR "/Edit/Clear" - - +#include <libgnome/gnome-defs.h> #include <libgnome/gnome-i18n.h> -#include <libgnomeui/gnome-stock.h> -#include <libnautilus-extensions/nautilus-gtk-macros.h> - - -/* forward declarations */ - -static void nautilus_clipboard_info_initialize_class (NautilusClipboardInfoClass *klass); -void nautilus_clipboard_info_initialize (NautilusClipboardInfo *info); -void nautilus_clipboard_info_destroy (NautilusClipboardInfo *info); - -void nautilus_clipboard_info_destroy_cb (GtkObject *object, gpointer user_data); - -NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusClipboardInfo, nautilus_clipboard_info, GTK_TYPE_SCROLLED_WINDOW) - -NautilusClipboardInfo* nautilus_clipboard_info_new (void); - -static BonoboControl * nautilus_clipboard_info_get_bonobo_control (NautilusClipboardInfo *info); -/* static BonoboUIHandler* nautilus_clipboard_info_get_bonobo_ui_handler (NautilusClipboardInfo *info); */ -static void nautilus_component_bonobo_menu_cut_cb (BonoboUIHandler *uihandler, - gpointer data, const char *path); -static void nautilus_component_bonobo_menu_copy_cb (BonoboUIHandler *uihandler, - gpointer data, const char *path); -static void nautilus_component_bonobo_menu_paste_cb (BonoboUIHandler *uihandler, - gpointer data, const char *path); -static void nautilus_component_bonobo_menu_clear_cb (BonoboUIHandler *uihandler, - gpointer data, const char *path); - -struct NautilusClipboardDetails { - char *component_name; - GtkWidget *clipboard_owner; - NautilusView *view; -}; - - - -NautilusClipboardInfo* -nautilus_clipboard_info_new () -{ - return NAUTILUS_CLIPBOARD_INFO (gtk_type_new (nautilus_clipboard_info_get_type ())); -} - - - -void -nautilus_clipboard_info_initialize (NautilusClipboardInfo *info) -{ - - info->details = g_new0 (NautilusClipboardDetails, 1); - info->details->component_name = NULL; - info->details->clipboard_owner = NULL; - info->details->view = NULL; -} - - - - +#include "nautilus-bonobo-ui.h" static void -nautilus_clipboard_info_initialize_class (NautilusClipboardInfoClass *klass) +cut_callback (BonoboUIHandler *ui_handler, + gpointer callback_data, + const char *path) { + g_assert (BONOBO_IS_UI_HANDLER (ui_handler)); + g_assert (strcmp (path, NAUTILUS_MENU_PATH_CUT_ITEM) == 0); - klass->destroy = nautilus_clipboard_info_destroy; - -} - - -void -nautilus_clipboard_info_destroy (NautilusClipboardInfo *info) -{ - - g_free (info->details->component_name); - gtk_widget_unref (info->details->clipboard_owner); - - if (info->details->view != NULL) { - bonobo_object_unref (BONOBO_OBJECT (info->details->view)); - } - - g_free (info->details); - - NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (GTK_OBJECT (info))); -} - -void -nautilus_clipboard_info_destroy_cb (GtkObject *object, gpointer user_data) -{ - NautilusClipboardInfo *view; - - g_assert (NAUTILUS_IS_CLIPBOARD_INFO (user_data)); - view = NAUTILUS_CLIPBOARD_INFO (user_data); - - nautilus_clipboard_info_destroy (view); - -} - - -void -nautilus_clipboard_info_set_component_name (NautilusClipboardInfo *info, char *component_name) -{ - info->details->component_name = g_strdup(component_name); -} - -char* -nautilus_clipboard_info_get_component_name (NautilusClipboardInfo *info) -{ - - return info->details->component_name; -} - - - -void -nautilus_clipboard_info_set_clipboard_owner (NautilusClipboardInfo *info, GtkWidget *clipboard_owner) -{ - g_assert (GTK_IS_EDITABLE (clipboard_owner)); - info->details->clipboard_owner = clipboard_owner; - gtk_widget_ref (clipboard_owner); -} - -GtkWidget * -nautilus_clipboard_info_get_clipboard_owner (NautilusClipboardInfo *info) -{ - return info->details->clipboard_owner; + gtk_editable_cut_clipboard (GTK_EDITABLE (callback_data)); } -void -nautilus_clipboard_info_set_view (NautilusClipboardInfo *info, NautilusView *view) +static void +copy_callback (BonoboUIHandler *ui_handler, + gpointer callback_data, + const char *path) { - g_return_if_fail (info != NULL); - g_return_if_fail (NAUTILUS_CLIPBOARD_INFO (info)); - g_return_if_fail (view != NULL); - g_return_if_fail (NAUTILUS_IS_VIEW (view)); - - bonobo_object_ref (BONOBO_OBJECT (view)); + g_assert (BONOBO_IS_UI_HANDLER (ui_handler)); + g_assert (strcmp (path, NAUTILUS_MENU_PATH_COPY_ITEM) == 0); - info->details->view = view; - -} - -NautilusView * -nautilus_clipboard_info_get_view (NautilusClipboardInfo *info) -{ - return info->details->view; + gtk_editable_copy_clipboard (GTK_EDITABLE (callback_data)); } - -/* -static BonoboUIHandler * -nautilus_clipboard_info_get_bonobo_ui_handler (NautilusClipboardInfo *info) -{ - return bonobo_control_get_ui_handler (nautilus_clipboard_info_get_bonobo_control (info)); -} -*/ - -static BonoboControl * -nautilus_clipboard_info_get_bonobo_control (NautilusClipboardInfo *info) -{ - return nautilus_view_get_bonobo_control (info->details->view); -} - - -void -nautilus_component_merge_bonobo_items_cb (GtkWidget *widget, GdkEventAny *event, gpointer user_data) +static void +paste_callback (BonoboUIHandler *ui_handler, + gpointer callback_data, + const char *path) { - NautilusClipboardInfo *info; - BonoboUIHandler *local_ui_handler; - - - g_assert (NAUTILUS_IS_CLIPBOARD_INFO(user_data)); - info = NAUTILUS_CLIPBOARD_INFO(user_data); - g_assert (BONOBO_IS_CONTROL (nautilus_clipboard_info_get_bonobo_control (info))); - - local_ui_handler = bonobo_control_get_ui_handler (nautilus_clipboard_info_get_bonobo_control (info)); - - bonobo_ui_handler_set_container (local_ui_handler, - bonobo_control_get_remote_ui_handler (nautilus_clipboard_info_get_bonobo_control (info))); + g_assert (BONOBO_IS_UI_HANDLER (ui_handler)); + g_assert (strcmp (path, NAUTILUS_MENU_PATH_PASTE_ITEM) == 0); - bonobo_ui_handler_menu_new_item (local_ui_handler, - MENU_PATH_CUT, - _("_Cut"), - _("Remove selected text from selection"), - bonobo_ui_handler_menu_get_pos (local_ui_handler , MENU_PATH_CUT), - BONOBO_UI_HANDLER_PIXMAP_NONE, - NULL, - 0, - 0, - nautilus_component_bonobo_menu_cut_cb, - info); - bonobo_ui_handler_menu_new_item (local_ui_handler, - MENU_PATH_COPY, - _("_Copy"), - _("Copy selected text to the clipboard"), - bonobo_ui_handler_menu_get_pos (local_ui_handler , MENU_PATH_COPY), - BONOBO_UI_HANDLER_PIXMAP_NONE, - NULL, - 0, - 0, - nautilus_component_bonobo_menu_copy_cb, - info); - bonobo_ui_handler_menu_new_item (local_ui_handler, - MENU_PATH_PASTE, - _("_Paste"), - _("Paste text from clipboard into text box"), - bonobo_ui_handler_menu_get_pos (local_ui_handler , MENU_PATH_PASTE), - BONOBO_UI_HANDLER_PIXMAP_NONE, - NULL, - 0, - 0, - nautilus_component_bonobo_menu_paste_cb, - info); - bonobo_ui_handler_menu_new_item (local_ui_handler, - MENU_PATH_CLEAR, - _("_Clear"), - _("Clear the current selection"), - bonobo_ui_handler_menu_get_pos (local_ui_handler , MENU_PATH_CLEAR), - BONOBO_UI_HANDLER_PIXMAP_NONE, - NULL, - 0, - 0, - nautilus_component_bonobo_menu_clear_cb, - info); - - + gtk_editable_paste_clipboard (GTK_EDITABLE (callback_data)); } - -void -nautilus_component_unmerge_bonobo_items_cb (GtkWidget *widget, GdkEventAny *event, gpointer user_data) +static void +clear_callback (BonoboUIHandler *ui_handler, + gpointer callback_data, + const char *path) { - NautilusClipboardInfo *info; - BonoboUIHandler *local_ui_handler; + g_assert (BONOBO_IS_UI_HANDLER (ui_handler)); + g_assert (strcmp (path, NAUTILUS_MENU_PATH_CLEAR_ITEM) == 0); - - g_assert (NAUTILUS_IS_CLIPBOARD_INFO(user_data)); - info = NAUTILUS_CLIPBOARD_INFO(user_data); - - g_assert (BONOBO_IS_CONTROL (nautilus_clipboard_info_get_bonobo_control (info))); - local_ui_handler = bonobo_control_get_ui_handler (nautilus_clipboard_info_get_bonobo_control (info)); - bonobo_ui_handler_unset_container (local_ui_handler); - - + /* A negative index deletes until the end of the string */ + gtk_editable_delete_text (GTK_EDITABLE (callback_data), 0, -1); } static void -nautilus_component_bonobo_menu_cut_cb (BonoboUIHandler *uihandler, - gpointer data, const char *path) +add_menu_item (BonoboUIHandler *ui_handler, + const char *path, + const char *title, + const char *description, + BonoboUIHandlerCallbackFunc callback, + gpointer callback_data) { - NautilusClipboardInfo *info; - - g_assert (NAUTILUS_IS_CLIPBOARD_INFO (data)); - info = NAUTILUS_CLIPBOARD_INFO (data); - - g_assert (GTK_IS_EDITABLE (nautilus_clipboard_info_get_clipboard_owner (info))); - gtk_editable_cut_clipboard (GTK_EDITABLE (nautilus_clipboard_info_get_clipboard_owner (info))); - + bonobo_ui_handler_menu_new_item + (ui_handler, path, title, description, + bonobo_ui_handler_menu_get_pos (ui_handler, path), + BONOBO_UI_HANDLER_PIXMAP_NONE, NULL, 0, 0, + callback, callback_data); } - - static void -nautilus_component_bonobo_menu_copy_cb (BonoboUIHandler *uihandler, - gpointer data, const char *path) +add_menu_items_callback (GtkWidget *widget, + GdkEventAny *event, + gpointer callback_data) { - NautilusClipboardInfo *info; - - g_assert (NAUTILUS_IS_CLIPBOARD_INFO (data)); - info = NAUTILUS_CLIPBOARD_INFO (data); - - g_assert (GTK_IS_EDITABLE (nautilus_clipboard_info_get_clipboard_owner (info))); - gtk_editable_copy_clipboard (GTK_EDITABLE (nautilus_clipboard_info_get_clipboard_owner (info))); + BonoboUIHandler *ui_handler; + g_assert (GTK_IS_EDITABLE (widget)); + + ui_handler = bonobo_control_get_ui_handler (BONOBO_CONTROL (callback_data)); + + /* I wish I understsood better why we have to do this. */ + /* FIXME: Doesn't the remote UI handler leak here? */ + bonobo_ui_handler_set_container + (ui_handler, + bonobo_control_get_remote_ui_handler (BONOBO_CONTROL (callback_data))); + + /* FIXME: We never mark any of these items insensitive. */ + add_menu_item (ui_handler, + NAUTILUS_MENU_PATH_CUT_ITEM, + _("_Cut"), + _("Remove selected text from selection"), + cut_callback, widget); + add_menu_item (ui_handler, + NAUTILUS_MENU_PATH_COPY_ITEM, + _("_Copy"), + _("Copy selected text to the clipboard"), + copy_callback, widget); + add_menu_item (ui_handler, + NAUTILUS_MENU_PATH_PASTE_ITEM, + _("_Paste"), + _("Paste text from clipboard into text box"), + paste_callback, widget); + add_menu_item (ui_handler, + NAUTILUS_MENU_PATH_CLEAR_ITEM, + _("_Clear"), + _("Clear the current selection"), + clear_callback, widget); } - static void -nautilus_component_bonobo_menu_paste_cb (BonoboUIHandler *uihandler, - gpointer data, const char *path) +remove_menu_items_callback (GtkWidget *widget, + GdkEventAny *event, + gpointer callback_data) { - NautilusClipboardInfo *info; - - g_assert (NAUTILUS_IS_CLIPBOARD_INFO (data)); - info = NAUTILUS_CLIPBOARD_INFO (data); - - g_assert (GTK_IS_EDITABLE (nautilus_clipboard_info_get_clipboard_owner (info))); - gtk_editable_paste_clipboard (GTK_EDITABLE (nautilus_clipboard_info_get_clipboard_owner (info))); + BonoboUIHandler *ui_handler; + g_assert (GTK_IS_EDITABLE (widget)); + ui_handler = bonobo_control_get_ui_handler (BONOBO_CONTROL (callback_data)); + bonobo_ui_handler_menu_remove (ui_handler, + NAUTILUS_MENU_PATH_CUT_ITEM); + bonobo_ui_handler_menu_remove (ui_handler, + NAUTILUS_MENU_PATH_COPY_ITEM); + bonobo_ui_handler_menu_remove (ui_handler, + NAUTILUS_MENU_PATH_PASTE_ITEM); + bonobo_ui_handler_menu_remove (ui_handler, + NAUTILUS_MENU_PATH_CLEAR_ITEM); } - - -static void -nautilus_component_bonobo_menu_clear_cb (BonoboUIHandler *uihandler, - gpointer data, const char *path) -{ - NautilusClipboardInfo *info; - - g_assert (NAUTILUS_IS_CLIPBOARD_INFO (data)); - info = NAUTILUS_CLIPBOARD_INFO (data); - - g_assert (GTK_IS_EDITABLE (nautilus_clipboard_info_get_clipboard_owner (info))); - /* A negative index deletes until the end of the string */ - gtk_editable_delete_text (GTK_EDITABLE (nautilus_clipboard_info_get_clipboard_owner (info)), - 0, -1); - +void +nautilus_clipboard_set_up_editable (GtkEditable *target, + BonoboControl *control) +{ + g_return_if_fail (GTK_IS_EDITABLE (target)); + g_return_if_fail (BONOBO_IS_CONTROL (control)); + + /* Attach code to add menus when it gets the focus. */ + bonobo_object_ref (BONOBO_OBJECT (control)); + gtk_signal_connect_full + (GTK_OBJECT (target), "focus_in_event", + GTK_SIGNAL_FUNC (add_menu_items_callback), + NULL, control, + (GtkDestroyNotify) bonobo_object_unref, + FALSE, FALSE); + + /* Attach code to remove menus when it loses the focus. */ + bonobo_object_ref (BONOBO_OBJECT (control)); + gtk_signal_connect_full + (GTK_OBJECT (target), "focus_out_event", + GTK_SIGNAL_FUNC (remove_menu_items_callback), + NULL, control, + (GtkDestroyNotify) bonobo_object_unref, + FALSE, FALSE); } |