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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* NautilusEntry: one-line text editing widget. This consists of bug fixes
* and other improvements to GtkEntry, and all the changes could be rolled
* into GtkEntry some day.
*
* Copyright (C) 2000 Eazel, Inc.
*
* Author: John Sullivan <sullivan@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 "nautilus-entry.h"
#include <string.h>
#include "nautilus-global-preferences.h"
#include "nautilus-undo-signal-handlers.h"
#include <eel/eel-gdk-extensions.h>
#include <eel/eel-gtk-macros.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <libgnome/gnome-i18n.h>
struct NautilusEntryDetails {
gboolean use_emacs_shortcuts;
gboolean user_edit;
gboolean special_tab_handling;
gboolean cursor_obscured;
};
enum {
USER_CHANGED,
SELECTION_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
static void nautilus_entry_init (NautilusEntry *entry);
static void nautilus_entry_class_init (NautilusEntryClass *class);
EEL_DEFINE_CLASS_BOILERPLATE (NautilusEntry,
nautilus_entry,
GTK_TYPE_ENTRY)
static void
emacs_shortcuts_preference_changed_callback (gpointer callback_data)
{
NautilusEntry *entry;
entry = NAUTILUS_ENTRY (callback_data);
entry->details->use_emacs_shortcuts =
eel_preferences_get_boolean (NAUTILUS_PREFERENCES_USE_EMACS_SHORTCUTS);
}
static void
nautilus_entry_init (NautilusEntry *entry)
{
GtkWidget *widget;
widget = GTK_WIDGET (entry);
entry->details = g_new0 (NautilusEntryDetails, 1);
entry->details->user_edit = TRUE;
/* Allow pointer motion events so we can expose an obscured cursor if necessary */
gtk_widget_set_events (widget, gtk_widget_get_events (widget) | GDK_POINTER_MOTION_MASK);
nautilus_undo_set_up_nautilus_entry_for_undo (entry);
eel_preferences_add_callback (NAUTILUS_PREFERENCES_USE_EMACS_SHORTCUTS,
emacs_shortcuts_preference_changed_callback,
entry);
emacs_shortcuts_preference_changed_callback (entry);
}
GtkWidget *
nautilus_entry_new (void)
{
return gtk_widget_new (NAUTILUS_TYPE_ENTRY, NULL);
}
GtkWidget *
nautilus_entry_new_with_max_length (guint16 max)
{
GtkWidget *widget;
widget = gtk_widget_new (NAUTILUS_TYPE_ENTRY, NULL);
GTK_ENTRY (widget)->text_max_length = max;
return widget;
}
static void
nautilus_entry_destroy (GtkObject *object)
{
NautilusEntry *entry;
entry = NAUTILUS_ENTRY (object);
eel_preferences_remove_callback (NAUTILUS_PREFERENCES_USE_EMACS_SHORTCUTS,
emacs_shortcuts_preference_changed_callback,
entry);
g_free (entry->details);
EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}
static void
obscure_cursor (NautilusEntry *entry)
{
if (entry->details->cursor_obscured) {
return;
}
entry->details->cursor_obscured = TRUE;
eel_gdk_window_set_invisible_cursor (GTK_ENTRY (entry)->text_area);
}
static gboolean
nautilus_entry_key_press (GtkWidget *widget, GdkEventKey *event)
{
NautilusEntry *entry;
GtkEditable *editable;
int position;
gboolean old_has, new_has;
gboolean result;
entry = NAUTILUS_ENTRY (widget);
editable = GTK_EDITABLE (widget);
if (!gtk_editable_get_editable (editable)) {
return FALSE;
}
switch (event->keyval) {
case GDK_Tab:
/* The location bar entry wants TAB to work kind of
* like it does in the shell for command completion,
* so if we get a tab and there's a selection, we
* should position the insertion point at the end of
* the selection.
*/
if (entry->details->special_tab_handling && gtk_editable_get_selection_bounds (editable, NULL, NULL)) {
position = strlen (gtk_entry_get_text (GTK_ENTRY (editable)));
gtk_entry_select_region (GTK_ENTRY (editable), position, position);
return TRUE;
}
break;
case GDK_KP_Enter:
/* Work around bug in GtkEntry where keypad Enter key
* inserts a character rather than activating like the
* other Enter key.
*/
gtk_widget_activate (widget);
return TRUE;
default:
break;
}
if (!entry->details->use_emacs_shortcuts) {
/* Filter out the emacs-style keyboard shortcuts in
* GtkEntry for alt and control keys. They have
* numerous conflicts with menu keyboard shortcuts.
*/
if (event->state & GDK_CONTROL_MASK || event->state & GDK_MOD1_MASK) {
return FALSE;
}
}
obscure_cursor (entry);
old_has = gtk_editable_get_selection_bounds (editable, NULL, NULL);
result = EEL_CALL_PARENT_WITH_RETURN_VALUE
(GTK_WIDGET_CLASS, key_press_event, (widget, event));
/* Pressing a key usually changes the selection if there is a selection.
* If there is not selection, we can save work by not emitting a signal.
*/
if (result) {
new_has = gtk_editable_get_selection_bounds (editable, NULL, NULL);
if (old_has || new_has) {
gtk_signal_emit (GTK_OBJECT (widget), signals[SELECTION_CHANGED]);
}
}
return result;
}
static gboolean
nautilus_entry_motion_notify (GtkWidget *widget, GdkEventMotion *event)
{
int result;
gboolean old_had, new_had;
int old_start, old_end, new_start, new_end;
GdkCursor *cursor;
NautilusEntry *entry;
GtkEditable *editable;
entry = NAUTILUS_ENTRY (widget);
editable = GTK_EDITABLE (widget);
/* Reset cursor to I-Beam */
if (entry->details->cursor_obscured) {
cursor = gdk_cursor_new (GDK_XTERM);
gdk_window_set_cursor (GTK_ENTRY (entry)->text_area, cursor);
gdk_cursor_destroy (cursor);
entry->details->cursor_obscured = FALSE;
}
old_had = gtk_editable_get_selection_bounds (editable, &old_start, &old_end);
result = EEL_CALL_PARENT_WITH_RETURN_VALUE
(GTK_WIDGET_CLASS, motion_notify_event, (widget, event));
/* Send a signal if dragging the mouse caused the selection to change. */
if (result) {
new_had = gtk_editable_get_selection_bounds (editable, &new_start, &new_end);
if (old_had != new_had || (old_had && (old_start != new_start || old_end != new_end))) {
gtk_signal_emit (GTK_OBJECT (widget), signals[SELECTION_CHANGED]);
}
}
return result;
}
/**
* nautilus_entry_select_all
*
* Select all text, leaving the text cursor position at the end.
*
* @entry: A NautilusEntry
**/
void
nautilus_entry_select_all (NautilusEntry *entry)
{
g_return_if_fail (NAUTILUS_IS_ENTRY (entry));
gtk_editable_set_position (GTK_EDITABLE (entry), -1);
gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
}
static gboolean
select_all_at_idle (gpointer callback_data)
{
NautilusEntry *entry;
entry = NAUTILUS_ENTRY (callback_data);
if (!GTK_OBJECT_DESTROYED (entry)) {
nautilus_entry_select_all (entry);
}
gtk_object_unref (GTK_OBJECT (entry));
return FALSE;
}
/**
* nautilus_entry_select_all_at_idle
*
* Select all text at the next idle, not immediately.
* This is useful when reacting to a key press, because
* changing the selection and the text cursor position doesn't
* work in a key_press signal handler.
*
* @entry: A NautilusEntry
**/
void
nautilus_entry_select_all_at_idle (NautilusEntry *entry)
{
g_return_if_fail (NAUTILUS_IS_ENTRY (entry));
/* If the text cursor position changes in this routine
* then gtk_entry_key_press will unselect (and we want
* to move the text cursor position to the end).
*/
gtk_object_ref (GTK_OBJECT (entry));
gtk_idle_add (select_all_at_idle, entry);
}
/**
* nautilus_entry_set_text
*
* This function wraps gtk_entry_set_text. It sets undo_registered
* to TRUE and preserves the old value for a later restore. This is
* done so the programmatic changes to the entry do not register
* with the undo manager.
*
* @entry: A NautilusEntry
* @test: The text to set
**/
void
nautilus_entry_set_text (NautilusEntry *entry, const gchar *text)
{
g_return_if_fail (NAUTILUS_IS_ENTRY (entry));
entry->details->user_edit = FALSE;
gtk_entry_set_text (GTK_ENTRY (entry), text);
entry->details->user_edit = TRUE;
gtk_signal_emit (GTK_OBJECT (entry), signals[SELECTION_CHANGED]);
}
static void
nautilus_entry_set_selection_bounds (GtkEditable *editable,
int start_pos,
int end_pos)
{
EEL_CALL_PARENT (GTK_EDITABLE_CLASS, set_selection_bounds,
(editable, start_pos, end_pos));
gtk_signal_emit (GTK_OBJECT (editable), signals[SELECTION_CHANGED]);
}
static gboolean
nautilus_entry_button_press (GtkWidget *widget,
GdkEventButton *event)
{
gboolean result;
result = EEL_CALL_PARENT_WITH_RETURN_VALUE
(GTK_WIDGET_CLASS, button_press_event, (widget, event));
if (result) {
gtk_signal_emit (GTK_OBJECT (widget), signals[SELECTION_CHANGED]);
}
return result;
}
static gboolean
nautilus_entry_button_release (GtkWidget *widget,
GdkEventButton *event)
{
gboolean result;
result = EEL_CALL_PARENT_WITH_RETURN_VALUE
(GTK_WIDGET_CLASS, button_release_event, (widget, event));
if (result) {
gtk_signal_emit (GTK_OBJECT (widget), signals[SELECTION_CHANGED]);
}
return result;
}
static void
nautilus_entry_insert_text (GtkEditable *editable, const gchar *text,
int length, int *position)
{
NautilusEntry *entry;
entry = NAUTILUS_ENTRY(editable);
/* Fire off user changed signals */
if (entry->details->user_edit) {
gtk_signal_emit (GTK_OBJECT (editable), signals[USER_CHANGED]);
}
EEL_CALL_PARENT (GTK_EDITABLE_CLASS, insert_text,
(editable, text, length, position));
gtk_signal_emit (GTK_OBJECT (editable), signals[SELECTION_CHANGED]);
}
static void
nautilus_entry_delete_text (GtkEditable *editable, int start_pos, int end_pos)
{
NautilusEntry *entry;
entry = NAUTILUS_ENTRY (editable);
/* Fire off user changed signals */
if (entry->details->user_edit) {
gtk_signal_emit (GTK_OBJECT (editable), signals[USER_CHANGED]);
}
EEL_CALL_PARENT (GTK_EDITABLE_CLASS, delete_text,
(editable, start_pos, end_pos));
gtk_signal_emit (GTK_OBJECT (editable), signals[SELECTION_CHANGED]);
}
/* Overridden to work around GTK bug. The selection_clear_event is queued
* when the selection changes. Changing the selection to NULL and then
* back to the original selection owner still sends the event, so the
* selection owner then gets the selection ripped away from it. We ran into
* this with type-completion behavior in NautilusLocationBar (see bug 5313).
* There's a FIXME comment that seems to be about this same issue in
* gtk+/gtkselection.c, gtk_selection_clear.
*/
static gboolean
nautilus_entry_selection_clear (GtkWidget *widget,
GdkEventSelection *event)
{
g_return_val_if_fail (NAUTILUS_IS_ENTRY (widget), FALSE);
if (gdk_selection_owner_get (event->selection) == widget->window) {
return FALSE;
}
return EEL_CALL_PARENT_WITH_RETURN_VALUE
(GTK_WIDGET_CLASS, selection_clear_event, (widget, event));
}
static void
nautilus_entry_class_init (NautilusEntryClass *class)
{
GtkWidgetClass *widget_class;
GtkObjectClass *object_class;
GtkEditableClass *editable_class;
widget_class = GTK_WIDGET_CLASS (class);
object_class = GTK_OBJECT_CLASS (class);
editable_class = GTK_EDITABLE_CLASS (class);
widget_class->button_press_event = nautilus_entry_button_press;
widget_class->button_release_event = nautilus_entry_button_release;
widget_class->key_press_event = nautilus_entry_key_press;
widget_class->motion_notify_event = nautilus_entry_motion_notify;
widget_class->selection_clear_event = nautilus_entry_selection_clear;
object_class->destroy = nautilus_entry_destroy;
editable_class->insert_text = nautilus_entry_insert_text;
editable_class->delete_text = nautilus_entry_delete_text;
editable_class->set_selection_bounds = nautilus_entry_set_selection_bounds;
/* Set up signals */
signals[USER_CHANGED] = g_signal_new
("user_changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (NautilusEntryClass,
user_changed),
NULL, NULL,
gtk_marshal_NONE__NONE,
G_TYPE_NONE, 0);
signals[SELECTION_CHANGED] = g_signal_new
("selection_changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (NautilusEntryClass,
selection_changed),
NULL, NULL,
gtk_marshal_NONE__NONE,
G_TYPE_NONE, 0);
}
void
nautilus_entry_set_special_tab_handling (NautilusEntry *entry,
gboolean special_tab_handling)
{
g_return_if_fail (NAUTILUS_IS_ENTRY (entry));
entry->details->special_tab_handling = special_tab_handling;
}
|