Commit 822c600f authored by Gene Ragan's avatar Gene Ragan

New file and functions to handle undo in NautilusEntry and GtkEditables.



	* libnautilus-extensions/nautilus-undo-signal-handlers.c:
	* libnautilus-extensions/nautilus-undo-signal-handlers.h:
	(nautilus_entry_user_changed_callback),
	(nautilus_undo_setup_nautilus_entry_for_undo),
	(nautilus_undo_teardown_nautilus_entry_for_undo),
	(free_editable_undo_data), (free_editable_object_data),
	(editable_insert_text_callback), (editable_delete_text_callback),
	(editable_register_edit_undo),
	(nautilus_undo_setup_editable_for_undo),
	(nautilus_undo_teardown_editable_for_undo),
	(restore_editable_from_undo_snapshot_callback),
	(editable_key_press_event), (nautilus_undo_editable_set_undo_key):
	New file and functions to handle undo in NautilusEntry and
	GtkEditables.  This file is intended to contain signal handlers
	that enable undo for a variety of GTK widgets. Calling a
	setup_for_undo function will prepare the widget to handle
	undo functionality.

	* libnautilus-extensions/Makefile.am:
	Added entries for new files nautilus-undo-signal-handlers.c
	and nautilus-undo-signal-handlers.h

	* components/notes/nautilus-notes.c: (make_notes_view):
	Fixed bug #973.  Add undo to notes component.

	* components/websearch/ntl-web-search.c: (make_obj):
	Changed to use new undo signal handlers.

	* libnautilus-extensions/nautilus-entry.c:
	(nautilus_entry_initialize), (nautilus_entry_key_press),
	(nautilus_entry_set_text), (nautilus_entry_delete_text):
	Removed old undo code that had been rendered obsolete by the
	new signal handlers mentioned above.

	* src/nautilus-bookmarks-window.c: (create_bookmarks_window):
	Changed to use new undo signal handlers.

----------------------------------
parent 820f06eb
2000-06-12 Gene Z. Ragan <gzr@eazel.com>
* libnautilus-extensions/nautilus-undo-signal-handlers.c:
* libnautilus-extensions/nautilus-undo-signal-handlers.h:
(nautilus_entry_user_changed_callback),
(nautilus_undo_setup_nautilus_entry_for_undo),
(nautilus_undo_teardown_nautilus_entry_for_undo),
(free_editable_undo_data), (free_editable_object_data),
(editable_insert_text_callback), (editable_delete_text_callback),
(editable_register_edit_undo),
(nautilus_undo_setup_editable_for_undo),
(nautilus_undo_teardown_editable_for_undo),
(restore_editable_from_undo_snapshot_callback),
(editable_key_press_event), (nautilus_undo_editable_set_undo_key):
New file and functions to handle undo in NautilusEntry and
GtkEditables. This file is intended to contain signal handlers
that enable undo for a variety of GTK widgets. Calling a
setup_for_undo function will prepare the widget to handle
undo functionality.
* libnautilus-extensions/Makefile.am:
Added entries for new files nautilus-undo-signal-handlers.c
and nautilus-undo-signal-handlers.h
* components/notes/nautilus-notes.c: (make_notes_view):
Fixed bug #973. Add undo to notes component.
* components/websearch/ntl-web-search.c: (make_obj):
Changed to use new undo signal handlers.
* libnautilus-extensions/nautilus-entry.c:
(nautilus_entry_initialize), (nautilus_entry_key_press),
(nautilus_entry_set_text), (nautilus_entry_delete_text):
Removed old undo code that had been rendered obsolete by the
new signal handlers mentioned above.
* src/nautilus-bookmarks-window.c: (create_bookmarks_window):
Changed to use new undo signal handlers.
2000-06-12 John Sullivan <sullivan@eazel.com>
Fixed bug 692 (Bookmarks for deleted files don't do
......
......@@ -29,6 +29,7 @@
#include <libnautilus/libnautilus.h>
#include <libnautilus-extensions/nautilus-metadata.h>
#include <libnautilus-extensions/nautilus-undo-signal-handlers.h>
#include <gnome.h>
#include <libgnomevfs/gnome-vfs.h>
#include <liboaf/liboaf.h>
......@@ -149,23 +150,25 @@ make_notes_view (BonoboGenericFactory *Factory, const char *goad_id, gpointer cl
notes->uri = g_strdup ("");
/* allocate a vbox to hold all of the UI elements */
vbox = gtk_vbox_new (FALSE, 0);
/* create the text container */
/* create the text container */
notes->note_text_field = gtk_text_new (NULL, NULL);
gtk_text_set_editable (GTK_TEXT (notes->note_text_field), TRUE);
gtk_text_set_editable (GTK_TEXT (notes->note_text_field), TRUE);
gtk_box_pack_start (GTK_BOX (vbox), notes->note_text_field, TRUE, TRUE, 0);
background = nautilus_get_widget_background (notes->note_text_field);
nautilus_background_set_color (background, NOTES_DEFAULT_BACKGROUND_COLOR);
gtk_widget_show_all (vbox);
/* Create CORBA object. */
/* Create CORBA object. */
notes->view = nautilus_view_new (vbox);
gtk_signal_connect (GTK_OBJECT (notes->view), "destroy", do_destroy, notes);
/* Setup up text field for undo */
nautilus_undo_setup_editable_for_undo (GTK_EDITABLE (notes->note_text_field));
nautilus_undo_editable_set_undo_key (GTK_EDITABLE (notes->note_text_field), TRUE);
notes_object_count++;
/* handle events */
......
......@@ -25,13 +25,16 @@
/* ntl-web-search.c: Rewrite KWebSearch using Gtk+ and Nautilus */
#include <config.h>
#include <libnautilus/libnautilus.h>
#include <libnautilus-extensions/nautilus-entry.h>
#include <gnome.h>
#include <liboaf/liboaf.h>
#include <limits.h>
#include <ctype.h>
#include <libnautilus/libnautilus.h>
#include <libnautilus/nautilus-clipboard.h>
#include <libnautilus-extensions/nautilus-undo-signal-handlers.h>
typedef struct {
char *name, *url_head, *url_tail;
......@@ -182,7 +185,8 @@ make_obj(BonoboGenericFactory *Factory, const char *goad_id, gpointer closure)
web_search_populate_engines(hview);
hview->ent_params = nautilus_entry_new();
nautilus_undo_editable_set_undo_key ( GTK_EDITABLE (hview->ent_params), TRUE);
gtk_signal_connect(GTK_OBJECT(hview->ent_params), "activate", do_search, hview);
gtk_container_add(GTK_CONTAINER(vbox), hview->ent_params);
......
......@@ -86,6 +86,7 @@ libnautilus_extensions_la_SOURCES = \
nautilus-text-caption.c \
nautilus-undo-context.c \
nautilus-undo-manager.c \
nautilus-undo-signal-handlers.c \
nautilus-user-level-manager.c \
nautilus-view-identifier.c \
nautilus-xml-extensions.c \
......
......@@ -30,29 +30,24 @@
#include "nautilus-gtk-macros.h"
#include <gdk/gdkkeysyms.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkwidget.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libnautilus/nautilus-undo.h>
#include <libnautilus-extensions/nautilus-undo-signal-handlers.h>
#include <orb/orbit.h>
enum {
USER_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
typedef struct
{
char *undo_text;
gint position;
} NautilusEntryUndoData;
static void nautilus_entry_initialize (NautilusEntry *entry);
static void nautilus_entry_initialize_class (NautilusEntryClass *class);
static void nautilus_entry_destroy (GtkObject *object);
......@@ -66,13 +61,6 @@ static void nautilus_entry_delete_text (GtkEditable *editable,
gint start_pos,
gint end_pos);
static void user_changed_callback (NautilusEntry *entry);
/* Undo callbacks */
static void register_edit_undo (NautilusEntry *entry);
static void restore_from_undo_snapshot_callback (GtkObject *target, gpointer callback_data);
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusEntry, nautilus_entry, GTK_TYPE_ENTRY)
static void
......@@ -110,11 +98,7 @@ nautilus_entry_initialize (NautilusEntry *entry)
{
entry->user_edit = TRUE;
gtk_signal_connect (GTK_OBJECT (entry),
"user_changed",
GTK_SIGNAL_FUNC (user_changed_callback),
NULL);
nautilus_undo_setup_nautilus_entry_for_undo (entry);
}
GtkWidget*
......@@ -153,15 +137,6 @@ nautilus_entry_key_press (GtkWidget *widget, GdkEventKey *event)
case GDK_KP_Enter:
gtk_widget_activate (widget);
return TRUE;
/* Undo */
case 'z':
if ((event->state & GDK_CONTROL_MASK) != 0
&& entry->handle_undo_key) {
nautilus_undo (GTK_OBJECT (widget));
return FALSE;
}
break;
default:
break;
......@@ -230,17 +205,10 @@ nautilus_entry_select_all_at_idle (NautilusEntry *entry)
void
nautilus_entry_set_text (NautilusEntry *entry, const gchar *text)
{
gboolean val;
entry->user_edit = FALSE;
val = entry->undo_registered;
entry->undo_registered = TRUE;
gtk_entry_set_text ( GTK_ENTRY (entry), text);
entry->undo_registered = val;
entry->user_edit = TRUE;
}
......@@ -276,108 +244,4 @@ nautilus_entry_delete_text (GtkEditable *editable, gint start_pos, gint end_pos)
NAUTILUS_CALL_PARENT_CLASS (GTK_EDITABLE_CLASS, delete_text,
(editable, start_pos, end_pos));
}
/* free_undo_data
*
* Clean up routine to free entry undo data
*/
static void
free_undo_data (gpointer data)
{
NautilusEntryUndoData *undo_data;
undo_data = (NautilusEntryUndoData *) data;
g_free (undo_data->undo_text);
g_free (undo_data);
}
/* save_undo_snapshot_callback
*
* Get text at start of edit operation and store in undo data as
* string with a key of "undo_text".
*/
static void
register_edit_undo (NautilusEntry *entry)
{
NautilusEntryUndoData *undo_data;
if (entry->undo_registered) {
return;
}
undo_data = g_new (NautilusEntryUndoData, 1);
undo_data->undo_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
undo_data->position = gtk_editable_get_position (GTK_EDITABLE (entry));
entry->undo_registered = TRUE;
nautilus_undo_register
(GTK_OBJECT (entry),
restore_from_undo_snapshot_callback,
undo_data,
(GDestroyNotify) free_undo_data,
_("Edit"),
_("Undo Edit"),
_("Undo the edit"),
_("Redo Edit"),
_("Redo the edit"));
}
/* restore_from_undo_snapshot_callback
*
* Restore edited text.
*/
static void
restore_from_undo_snapshot_callback (GtkObject *target, gpointer callback_data)
{
NautilusEntry *entry;
GtkWindow *window;
NautilusEntryUndoData *undo_data;
entry = NAUTILUS_ENTRY (target);
undo_data = (NautilusEntryUndoData *) callback_data;
/* Reset the registered flag so we get a new item for future editing. */
entry->undo_registered = FALSE;
/* Register a new undo transaction for redo. */
register_edit_undo (entry);
/* Restore the text. */
gtk_entry_set_text (GTK_ENTRY (entry), undo_data->undo_text);
/* Set focus to widget */
window = GTK_WINDOW (gtk_widget_get_toplevel ( GTK_WIDGET (target)));
gtk_window_set_focus (window, GTK_WIDGET (entry));
/* We have to do this call, because th eprevious call selects all text */
gtk_editable_select_region (GTK_EDITABLE (entry), 0, 0);
/* Set the i-beam to the end of the text */
gtk_editable_set_position ( GTK_EDITABLE (target), undo_data->position);
/* Reset the registered flag so we get a new item for future editing. */
entry->undo_registered = FALSE;
}
/* nautilus_entry_enable_undo_key
*
* Allow the use of ctrl-z from within widget. This should only be
* set if there is no menu bar to use to undo the widget.
*/
void
nautilus_entry_set_undo_key (NautilusEntry *entry, gboolean value)
{
entry->handle_undo_key = value;
}
static void
user_changed_callback (NautilusEntry *entry)
{
/* Register undo transaction */
register_edit_undo (entry);
}
......@@ -49,8 +49,6 @@ typedef struct NautilusEntryClass NautilusEntryClass;
struct NautilusEntry {
GtkEntry parent;
gboolean undo_registered;
gboolean handle_undo_key;
gboolean user_edit;
};
......@@ -63,11 +61,10 @@ struct NautilusEntryClass {
GtkType nautilus_entry_get_type (void);
GtkWidget* nautilus_entry_new (void);
void nautilus_entry_set_text (NautilusEntry *entry, const gchar *text);
void nautilus_entry_set_text (NautilusEntry *entry,
const gchar *text);
void nautilus_entry_select_all (NautilusEntry *entry);
void nautilus_entry_select_all_at_idle (NautilusEntry *entry);
void nautilus_entry_set_undo_key (NautilusEntry *entry,
gboolean value);
END_GNOME_DECLS
......
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* Signal handlers to enable undo in Gtk Widgets.
*
* Copyright (C) 2000 Eazel, Inc.
*
* Author: Gene Z. Ragan <gzr@eazel.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <gtk/gtk.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libnautilus/nautilus-undo.h>
#include "nautilus-gtk-macros.h"
#include "nautilus-undo-signal-handlers.h"
typedef struct {
char *undo_text;
gint position;
guint selection_start;
guint selection_end;
} EditableUndoData;
typedef struct {
gboolean undo_registered;
} EditableUndoObjectData;
static void restore_editable_from_undo_snapshot_callback (GtkObject *target,
gpointer callback_data);
static void editable_register_edit_undo (GtkEditable *editable);
static void free_editable_object_data (gpointer data);
/* nautilus_undo_setup_nautilus_entry_for_undo
*
* Functions and callback methods to handle undo
* in a NautilusEntry
*/
static void
nautilus_entry_user_changed_callback (NautilusEntry *entry)
{
/* Register undo transaction */
editable_register_edit_undo (GTK_EDITABLE (entry));
}
void
nautilus_undo_setup_nautilus_entry_for_undo (NautilusEntry *entry)
{
EditableUndoObjectData *data;
if (!NAUTILUS_IS_ENTRY (entry) ) {
return;
}
data = g_new(EditableUndoObjectData, 1);
data->undo_registered = FALSE;
gtk_object_set_data_full (GTK_OBJECT (entry), "undo_registered",
data, free_editable_object_data);
/* Connect to entry signals */
gtk_signal_connect (GTK_OBJECT (entry),
"user_changed",
GTK_SIGNAL_FUNC (nautilus_entry_user_changed_callback),
NULL);
}
void
nautilus_undo_teardown_nautilus_entry_for_undo (NautilusEntry *entry)
{
if (!NAUTILUS_IS_ENTRY (entry) ) {
return;
}
/* Disconnect from entry signals */
gtk_signal_disconnect_by_func (GTK_OBJECT (entry),
GTK_SIGNAL_FUNC (nautilus_entry_user_changed_callback),
NULL);
}
/* nautilus_undo_setup_nautilus_entry_for_undo
*
* Functions and callback methods to handle undo
* in a NautilusEntry
*/
static void
free_editable_undo_data (gpointer data)
{
EditableUndoData *undo_data;
undo_data = (EditableUndoData *) data;
g_free (undo_data->undo_text);
g_free (undo_data);
}
static void
free_editable_object_data (gpointer data)
{
g_free (data);
}
static void
editable_insert_text_callback (GtkEditable *editable)
{
/* Register undo transaction */
editable_register_edit_undo (editable);
}
static void
editable_delete_text_callback (GtkEditable *editable)
{
/* Register undo transaction */
editable_register_edit_undo (editable);
}
static void
editable_register_edit_undo (GtkEditable *editable)
{
EditableUndoData *undo_data;
EditableUndoObjectData *undo_info;
gpointer data;
if (!GTK_IS_EDITABLE (editable) ) {
return;
}
/* Check our undo registered flag */
data = gtk_object_get_data (GTK_OBJECT (editable), "undo_registered");
if (data == NULL) {
return;
}
undo_info = (EditableUndoObjectData *)data;
if (undo_info->undo_registered == TRUE) {
return;
}
undo_data = g_new (EditableUndoData, 1);
undo_data->undo_text = g_strdup (gtk_editable_get_chars (editable, 0, -1));
undo_data->position = gtk_editable_get_position (editable);
undo_data->selection_start = editable->selection_start_pos;
undo_data->selection_end = editable->selection_end_pos;
nautilus_undo_register
(GTK_OBJECT (editable),
restore_editable_from_undo_snapshot_callback,
undo_data,
(GDestroyNotify) free_editable_undo_data,
_("Edit"),
_("Undo Edit"),
_("Undo the edit"),
_("Redo Edit"),
_("Redo the edit"));
undo_info->undo_registered = TRUE;
}
void
nautilus_undo_setup_editable_for_undo (GtkEditable *editable)
{
EditableUndoObjectData *data;
if (!GTK_IS_EDITABLE (editable) ) {
return;
}
/* Connect to editable signals */
gtk_signal_connect (GTK_OBJECT (editable),
"insert_text",
GTK_SIGNAL_FUNC (editable_insert_text_callback),
NULL);
gtk_signal_connect (GTK_OBJECT (editable),
"delete_text",
GTK_SIGNAL_FUNC (editable_delete_text_callback),
NULL);
data = g_new(EditableUndoObjectData, 1);
data->undo_registered = FALSE;
gtk_object_set_data_full (GTK_OBJECT (editable), "undo_registered",
data, free_editable_object_data);
}
void
nautilus_undo_teardown_editable_for_undo (GtkEditable *editable)
{
if (!GTK_IS_EDITABLE (editable) ) {
return;
}
/* Disconnect from entry signals */
gtk_signal_disconnect_by_func (GTK_OBJECT (editable),
GTK_SIGNAL_FUNC (editable_insert_text_callback),
NULL);
gtk_signal_disconnect_by_func (GTK_OBJECT (editable),
GTK_SIGNAL_FUNC (editable_delete_text_callback),
NULL);
}
/* restore_editable_from_undo_snapshot_callback
*
* Restore edited text.
*/
static void
restore_editable_from_undo_snapshot_callback (GtkObject *target, gpointer callback_data)
{
GtkEditable *editable;
GtkWindow *window;
EditableUndoData *undo_data;
EditableUndoObjectData *data;
gint position;
editable = GTK_EDITABLE (target);
undo_data = (EditableUndoData *) callback_data;
/* Check our undo registered flag */
data = gtk_object_get_data (target, "undo_registered");
if (data == NULL) {
return;
}
/* Reset the registered flag so we get a new item for future editing. */
data->undo_registered = FALSE;
/* Register a new undo transaction for redo. */
editable_register_edit_undo (editable);
/* Restore the text. */
position = 0;
gtk_editable_delete_text (editable, 0, -1);
gtk_editable_insert_text (editable, undo_data->undo_text,
strlen (undo_data->undo_text), &position);
/* Set focus to widget */
window = GTK_WINDOW (gtk_widget_get_toplevel ( GTK_WIDGET (target)));
gtk_window_set_focus (window, GTK_WIDGET (editable));
/* We have to do this call, because the previous call selects all text */
gtk_editable_select_region (editable, 0, 0);
/* Restore selection */
gtk_editable_select_region (editable, undo_data->selection_start,
undo_data->selection_end);
/* Set the i-beam to the saved position */
gtk_editable_set_position (editable, undo_data->position);
/* Reset the registered flag so we get a new item for future editing. */
data->undo_registered = FALSE;
}
/* editable_set_undo_key
*
* Allow the use of ctrl-z from within widget.
*/
static void
editable_key_press_event (GtkEditable *editable, GdkEventKey *event, gpointer user_data)
{
switch (event->keyval) {
/* Undo */
case 'z':
if ((event->state & GDK_CONTROL_MASK) != 0) {
nautilus_undo (GTK_OBJECT (editable));
return;
}
break;
default:
break;
}
}
/* editable_set_undo_key
*
* Allow the use of ctrl-z from within widget. This should only be
* set if there is no menu bar to use to undo the widget.
*/
void
nautilus_undo_editable_set_undo_key (GtkEditable *editable, gboolean value)
{
if (value) {
/* Connect to entry signals */
gtk_signal_connect (GTK_OBJECT (editable),
"key-press-event",
GTK_SIGNAL_FUNC (editable_key_press_event),
NULL);
}
else {
gtk_signal_disconnect_by_func (GTK_OBJECT (editable),
GTK_SIGNAL_FUNC (editable_key_press_event),
NULL);
}
}
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* Signal handlers to enable undo in Gtk Widgets.
*
* Copyright (C) 2000 Eazel, Inc.
*
* Author: Gene Z. Ragan <gzr@eazel.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef NAUTILUS_UNDO_SIGNAL_HANDLERS_H
#define NAUTILUS_UNDO_SIGNAL_HANDLERS_H
#include <libnautilus-extensions/nautilus-entry.h>
void nautilus_undo_setup_nautilus_entry_for_undo (NautilusEntry *entry);
void nautilus_undo_teardown_nautilus_entry_for_undo (NautilusEntry *entry);
void nautilus_undo_setup_editable_for_undo (GtkEditable *editable);
void nautilus_undo_teardown_editable_for_undo (GtkEditable *editable);
void nautilus_undo_editable_set_undo_key (GtkEditable *editable,
gboolean value);
#endif /* NAUTILUS_UNDO_SIGNAL_HANDLERS_H */
\ No newline at end of file
......@@ -86,6 +86,7 @@ libnautilus_extensions_la_SOURCES = \
nautilus-text-caption.c \
nautilus-undo-context.c \
nautilus-undo-manager.c \
nautilus-undo-signal-handlers.c \
nautilus-user-level-manager.c \
nautilus-view-identifier.c \
nautilus-xml-extensions.c \
......
......@@ -30,29 +30,24 @@
#include "nautilus-gtk-macros.h"
#include <gdk/gdkkeysyms.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkwidget.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>