summaryrefslogtreecommitdiff
path: root/libnautilus/nautilus-undo.c
blob: b2d760a93a2fdcc37821e10bc9af889d2075bba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-undo.c - public interface for objects that implement
 *                   undoable actions -- works across components
 *
 * Copyright (C) 2000 Eazel, Inc.
 *
 * 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.
 *
 * Author: Darin Adler <darin@bentspoon.com>
 */

#include <config.h>
#include "nautilus-undo.h"

#include "nautilus-undo-private.h"
#include "nautilus-undo-transaction.h"
#include <gtk/gtksignal.h>
#include <gtk/gtkwindow.h>
#include <libgnomecanvas/gnome-canvas.h>

#define NAUTILUS_UNDO_MANAGER_DATA "Nautilus undo manager"

/* Register a simple undo action by calling nautilus_undo_register_full. */
void
nautilus_undo_register (GObject *target,
			NautilusUndoCallback callback,
			gpointer callback_data,
			GDestroyNotify callback_data_destroy_notify,
			const char *operation_name,
			const char *undo_menu_item_label,
			const char *undo_menu_item_hint,
			const char *redo_menu_item_label,
			const char *redo_menu_item_hint)
{
	NautilusUndoAtom atom;
	GList single_atom_list;

	g_return_if_fail (G_IS_OBJECT (target));
	g_return_if_fail (callback != NULL);

	/* Make an atom. */
	atom.target = target;
	atom.callback = callback;
	atom.callback_data = callback_data;
	atom.callback_data_destroy_notify = callback_data_destroy_notify;

	/* Make a single-atom list. */
	single_atom_list.data = &atom;
	single_atom_list.next = NULL;
	single_atom_list.prev = NULL;

	/* Call the full version of the registration function,
	 * using the undo target as the place to search for the
	 * undo manager.
	 */
	nautilus_undo_register_full (&single_atom_list,
				     target,
				     operation_name,
				     undo_menu_item_label,
				     undo_menu_item_hint,
				     redo_menu_item_label,
				     redo_menu_item_hint);
}

/* Register an undo action. */
void
nautilus_undo_register_full (GList *atoms,
			     GObject *undo_manager_search_start_object,
			     const char *operation_name,
			     const char *undo_menu_item_label,
			     const char *undo_menu_item_hint,
			     const char *redo_menu_item_label,
			     const char *redo_menu_item_hint)
{
	NautilusUndoTransaction *transaction;
	GList *p;

	g_return_if_fail (atoms != NULL);
	g_return_if_fail (G_IS_OBJECT (undo_manager_search_start_object));

	/* Create an undo transaction */
	transaction = nautilus_undo_transaction_new (operation_name,
						     undo_menu_item_label,
						     undo_menu_item_hint, 
						     redo_menu_item_label,
						     redo_menu_item_hint);
	for (p = atoms; p != NULL; p = p->next) {
		nautilus_undo_transaction_add_atom (transaction, p->data);
	}
	nautilus_undo_transaction_add_to_undo_manager
		(transaction,
		 nautilus_undo_get_undo_manager (undo_manager_search_start_object));

	/* Now we are done with the transaction.
	 * If the undo manager is holding it, then this will not destroy it.
	 */
	bonobo_object_unref (BONOBO_OBJECT (transaction));
}

/* Cover for forgetting about all undo relating to a particular target. */
void
nautilus_undo_unregister (GObject *target)
{
	/* Perhaps this should also unregister all children if called on a
	 * GtkContainer? That might be handy.
	 */
	nautilus_undo_transaction_unregister_object (target);
}

void
nautilus_undo (GObject *undo_manager_search_start_object)
{
	Nautilus_Undo_Manager manager;
	CORBA_Environment ev;

	g_return_if_fail (G_IS_OBJECT (undo_manager_search_start_object));

	CORBA_exception_init (&ev);

	manager = nautilus_undo_get_undo_manager (undo_manager_search_start_object);
	if (!CORBA_Object_is_nil (manager, &ev)) {
		Nautilus_Undo_Manager_undo (manager, &ev);
	}

	CORBA_exception_free (&ev);
}

Nautilus_Undo_Manager
nautilus_undo_get_undo_manager (GObject *start_object)
{
	Nautilus_Undo_Manager manager;
	GtkWidget *parent;
	GtkWindow *transient_parent;

	if (start_object == NULL) {
		return CORBA_OBJECT_NIL;
	}

	g_return_val_if_fail (G_IS_OBJECT (start_object), NULL);

	/* Check for an undo manager right here. */
	manager = g_object_get_data (start_object, NAUTILUS_UNDO_MANAGER_DATA);
	if (manager != NULL) {
		return manager;
	}

	/* Check for undo manager up the parent chain. */
	if (GTK_IS_WIDGET (start_object)) {
		parent = GTK_WIDGET (start_object)->parent;
		if (parent != NULL) {
			manager = nautilus_undo_get_undo_manager (G_OBJECT (parent));
			if (manager != NULL) {
				return manager;
			}
		}

		/* Check for undo manager in our window's parent. */
		if (GTK_IS_WINDOW (start_object)) {
			transient_parent = GTK_WINDOW (start_object)->transient_parent;
			if (transient_parent != NULL) {
				manager = nautilus_undo_get_undo_manager (G_OBJECT (transient_parent));
				if (manager != NULL) {
					return manager;
				}
			}
		}
	}
	
	/* In the case of a canvas item, try the canvas. */
	if (GNOME_IS_CANVAS_ITEM (start_object)) {
		manager = nautilus_undo_get_undo_manager (G_OBJECT (GNOME_CANVAS_ITEM (start_object)->canvas));
		if (manager != NULL) {
			return manager;
		}
	}
	
	/* Found nothing. I can live with that. */
	return CORBA_OBJECT_NIL;
}

static void
undo_manager_unref_cover (gpointer manager)
{
	bonobo_object_release_unref (manager, NULL);
}

void
nautilus_undo_attach_undo_manager (GObject *object,
				   Nautilus_Undo_Manager manager)
{
	g_return_if_fail (G_IS_OBJECT (object));

	if (manager == NULL) {
		g_object_set_data (object, NAUTILUS_UNDO_MANAGER_DATA, NULL);
	} else {
		bonobo_object_dup_ref (manager, NULL);
		g_object_set_data_full
			(object, NAUTILUS_UNDO_MANAGER_DATA,
			 manager, undo_manager_unref_cover);
	}
}

/* Copy a reference to the undo manager fromone object to another. */
void
nautilus_undo_share_undo_manager (GObject *destination_object,
				  GObject *source_object)
{
	Nautilus_Undo_Manager manager;
	CORBA_Environment ev;

	manager = nautilus_undo_get_undo_manager (source_object);

	nautilus_undo_attach_undo_manager
		(destination_object, manager);

	CORBA_exception_init (&ev);
	CORBA_Object_release (manager, &ev);
	CORBA_exception_free (&ev);
}

/* Locates an undo manager for this bonobo control.
 * The undo manager is supplied by an interface on
 * the control frame. The put that undo manager on
 * the Bonobo control's widget.
 */
static void
set_up_bonobo_control (BonoboControl *control)
{
	Nautilus_Undo_Manager manager;
	Bonobo_ControlFrame control_frame;
	CORBA_Environment ev;
	Nautilus_Undo_Context undo_context;
	GtkWidget *widget;

	g_assert (BONOBO_IS_CONTROL (control));

	manager = CORBA_OBJECT_NIL;

	CORBA_exception_init (&ev);

	/* Find the undo manager. */
	control_frame = bonobo_control_get_control_frame (control, &ev);
	if (!CORBA_Object_is_nil (control_frame, &ev)) {
		undo_context = Bonobo_Control_queryInterface
			(control_frame, "IDL:Nautilus/Undo/Context:1.0", &ev);
		if (!CORBA_Object_is_nil (undo_context, &ev)) {
			manager = Nautilus_Undo_Context__get_undo_manager (undo_context, &ev);
			Bonobo_Control_unref (undo_context, &ev);
		}
		CORBA_Object_release (undo_context, &ev);
	}
	CORBA_Object_release (control_frame, &ev);

	/* Attach the undo manager to the widget, or detach the old one. */
	widget = bonobo_control_get_widget (control);
	nautilus_undo_attach_undo_manager (G_OBJECT (widget), manager);
	CORBA_Object_release (manager, &ev);

	CORBA_exception_free (&ev);
}

void
nautilus_undo_set_up_bonobo_control (BonoboControl *control)
{
	g_return_if_fail (BONOBO_IS_CONTROL (control));

	set_up_bonobo_control (control);
	g_signal_connect (control, "set_frame",
			    G_CALLBACK (set_up_bonobo_control), NULL);
}