summaryrefslogtreecommitdiff
path: root/libcaja-private/caja-undo-signal-handlers.c
blob: f1dfba3c421222061f2ec14fc087dcecfdeb9cd2 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* -*- 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */


#include <config.h>
#include <gtk/gtk.h>

#include <glib/gi18n.h>
#include <libcaja-private/caja-undo.h>

#include <eel/eel-gtk-macros.h>
#include <string.h>

#include "caja-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 (GObject 	*target,
        gpointer 	callback_data);
static void editable_register_edit_undo 		 (GtkEditable 	*editable);
static void free_editable_object_data 			 (gpointer 	data);

/* caja_undo_set_up_caja_entry_for_undo
 *
 * Functions and callback methods to handle undo
 * in a CajaEntry
 */

static void
caja_entry_user_changed_callback (CajaEntry *entry)
{
    /* Register undo transaction */
    editable_register_edit_undo (GTK_EDITABLE (entry));
}

void
caja_undo_set_up_caja_entry_for_undo (CajaEntry *entry)
{
    EditableUndoObjectData *data;

    if (!CAJA_IS_ENTRY (entry) )
    {
        return;
    }

    data = g_new(EditableUndoObjectData, 1);
    data->undo_registered = FALSE;
    g_object_set_data_full (G_OBJECT (entry), "undo_registered",
                            data, free_editable_object_data);

    /* Connect to entry signals */
    g_signal_connect (entry, "user_changed",
                      G_CALLBACK (caja_entry_user_changed_callback),
                      NULL);
}

void
caja_undo_tear_down_caja_entry_for_undo (CajaEntry *entry)
{
    if (!CAJA_IS_ENTRY (entry) )
    {
        return;
    }

    /* Disconnect from entry signals */
    g_signal_handlers_disconnect_by_func
    (entry, G_CALLBACK (caja_entry_user_changed_callback), NULL);

}

/* caja_undo_set_up_caja_entry_for_undo
 *
 * Functions and callback methods to handle undo
 * in a CajaEntry
 */

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 = g_object_get_data (G_OBJECT (editable), "undo_registered");
    if (data == NULL)
    {
        g_warning ("Undo data is NULL");
        return;
    }

    undo_info = (EditableUndoObjectData *)data;
    if (undo_info->undo_registered)
    {
        return;
    }

    undo_data = g_new0 (EditableUndoData, 1);
    undo_data->undo_text = gtk_editable_get_chars (editable, 0, -1);
    undo_data->position = gtk_editable_get_position (editable);
    gtk_editable_get_selection_bounds (editable,
                                       &undo_data->selection_start,
                                       &undo_data->selection_end);

    caja_undo_register
    (G_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
caja_undo_set_up_editable_for_undo (GtkEditable *editable)
{
    EditableUndoObjectData *data;

    if (!GTK_IS_EDITABLE (editable) )
    {
        return;
    }

    /* Connect to editable signals */
    g_signal_connect (editable, "insert_text",
                      G_CALLBACK (editable_insert_text_callback), NULL);
    g_signal_connect (editable, "delete_text",
                      G_CALLBACK (editable_delete_text_callback), NULL);


    data = g_new (EditableUndoObjectData, 1);
    data->undo_registered = FALSE;
    g_object_set_data_full (G_OBJECT (editable), "undo_registered",
                            data, free_editable_object_data);
}

void
caja_undo_tear_down_editable_for_undo (GtkEditable *editable)
{
    if (!GTK_IS_EDITABLE (editable) )
    {
        return;
    }

    /* Disconnect from entry signals */
    g_signal_handlers_disconnect_by_func
    (editable, G_CALLBACK (editable_insert_text_callback), NULL);
    g_signal_handlers_disconnect_by_func
    (editable, G_CALLBACK (editable_delete_text_callback), NULL);
}

/* restore_editable_from_undo_snapshot_callback
 *
 * Restore edited text.
 */
static void
restore_editable_from_undo_snapshot_callback (GObject *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 = g_object_get_data (target, "undo_registered");
    if (data == NULL)
    {
        g_warning ("Undo regisetred flag not found");
        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.
 */

/* Undo is disabled until we have a better implementation.
 * Both here and in caja-shell-ui.xml.
 */

/* FIXME bugzilla.gnome.org 43515: Undo doesn't work */
#ifdef UNDO_ENABLED

static gboolean
editable_key_press_event (GtkEditable *editable, GdkEventKey *event, gpointer user_data)
{
    switch (event->keyval)
    {
        /* Undo */
    case 'z':
        if ((event->state & GDK_CONTROL_MASK) != 0)
        {
            caja_undo (GTK_OBJECT (editable));
            return TRUE;
        }
        break;

    default:
        break;
    }

    return FALSE;
}

#endif

/* 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
caja_undo_editable_set_undo_key (GtkEditable *editable, gboolean value)
{
    /* FIXME bugzilla.gnome.org 43515: Undo doesn't work */
#ifdef UNDO_ENABLED
    if (value)
    {
        /* Connect to entry signals */
        g_signal_connect (editable, "key_press_event",
                          G_CALLBACK (editable_key_press_event), NULL);
    }
    else
    {
        /* FIXME bugzilla.gnome.org 45092: Warns if the handler
         * is not already connected. We could use object data
         * to prevent that little problem.
         */
        g_signal_disconnect_by_func (editable,
                                     G_CALLBACK (editable_key_press_event), NULL);
    }
#endif
}