1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* CajaEntry: one-line text editing widget. This consists of bug fixes
4 * and other improvements to GtkEntry, and all the changes could be rolled
5 * into GtkEntry some day.
6 *
7 * Copyright (C) 2000 Eazel, Inc.
8 *
9 * Author: John Sullivan <sullivan@eazel.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
24 * Boston, MA 02110-1301, USA.
25 */
26
27 #include <config.h>
28 #include <string.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32
33 #include <eel/eel-gdk-extensions.h>
34 #include <eel/eel-gtk-macros.h>
35
36 #include "caja-entry.h"
37 #include "caja-global-preferences.h"
38
39 struct CajaEntryDetails
40 {
41 gboolean user_edit;
42 gboolean special_tab_handling;
43
44 guint select_idle_id;
45 };
46
47 enum
48 {
49 USER_CHANGED,
50 SELECTION_CHANGED,
51 LAST_SIGNAL
52 };
53 static guint signals[LAST_SIGNAL] = { 0 };
54
55 static void caja_entry_editable_init (GtkEditableInterface *iface);
56
57 G_DEFINE_TYPE_WITH_CODE (CajaEntry, caja_entry, GTK_TYPE_ENTRY,
58 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
59 caja_entry_editable_init));
60
61 static GtkEditableInterface *parent_editable_interface = NULL;
62
63 static void
caja_entry_init(CajaEntry * entry)64 caja_entry_init (CajaEntry *entry)
65 {
66 entry->details = g_new0 (CajaEntryDetails, 1);
67
68 entry->details->user_edit = TRUE;
69 }
70
71 GtkWidget *
caja_entry_new(void)72 caja_entry_new (void)
73 {
74 return gtk_widget_new (CAJA_TYPE_ENTRY, NULL);
75 }
76
77 static void
caja_entry_finalize(GObject * object)78 caja_entry_finalize (GObject *object)
79 {
80 CajaEntry *entry;
81
82 entry = CAJA_ENTRY (object);
83
84 if (entry->details->select_idle_id != 0)
85 {
86 g_source_remove (entry->details->select_idle_id);
87 }
88
89 g_free (entry->details);
90
91 G_OBJECT_CLASS (caja_entry_parent_class)->finalize (object);
92 }
93
94 static gboolean
caja_entry_key_press(GtkWidget * widget,GdkEventKey * event)95 caja_entry_key_press (GtkWidget *widget, GdkEventKey *event)
96 {
97 CajaEntry *entry;
98 GtkEditable *editable;
99 gboolean old_has, new_has;
100 gboolean result;
101
102 entry = CAJA_ENTRY (widget);
103 editable = GTK_EDITABLE (widget);
104
105 if (!gtk_editable_get_editable (editable))
106 {
107 return FALSE;
108 }
109
110 switch (event->keyval)
111 {
112 case GDK_KEY_Tab:
113 /* The location bar entry wants TAB to work kind of
114 * like it does in the shell for command completion,
115 * so if we get a tab and there's a selection, we
116 * should position the insertion point at the end of
117 * the selection.
118 */
119 if (entry->details->special_tab_handling && gtk_editable_get_selection_bounds (editable, NULL, NULL))
120 {
121 int position;
122
123 position = strlen (gtk_entry_get_text (GTK_ENTRY (editable)));
124 gtk_editable_select_region (editable, position, position);
125 return TRUE;
126 }
127 break;
128
129 default:
130 break;
131 }
132
133 old_has = gtk_editable_get_selection_bounds (editable, NULL, NULL);
134
135 result = GTK_WIDGET_CLASS (caja_entry_parent_class)->key_press_event (widget, event);
136
137 /* Pressing a key usually changes the selection if there is a selection.
138 * If there is not selection, we can save work by not emitting a signal.
139 */
140 if (result)
141 {
142 new_has = gtk_editable_get_selection_bounds (editable, NULL, NULL);
143 if (old_has || new_has)
144 {
145 g_signal_emit (widget, signals[SELECTION_CHANGED], 0);
146 }
147 }
148
149 return result;
150
151 }
152
153 static gboolean
caja_entry_motion_notify(GtkWidget * widget,GdkEventMotion * event)154 caja_entry_motion_notify (GtkWidget *widget, GdkEventMotion *event)
155 {
156 int result;
157 gboolean old_had, new_had;
158 int old_start, old_end, new_start, new_end;
159 GtkEditable *editable;
160
161 editable = GTK_EDITABLE (widget);
162
163 old_had = gtk_editable_get_selection_bounds (editable, &old_start, &old_end);
164
165 result = GTK_WIDGET_CLASS (caja_entry_parent_class)->motion_notify_event (widget, event);
166
167 /* Send a signal if dragging the mouse caused the selection to change. */
168 if (result)
169 {
170 new_had = gtk_editable_get_selection_bounds (editable, &new_start, &new_end);
171 if (old_had != new_had || (old_had && (old_start != new_start || old_end != new_end)))
172 {
173 g_signal_emit (widget, signals[SELECTION_CHANGED], 0);
174 }
175 }
176
177 return result;
178 }
179
180 /**
181 * caja_entry_select_all
182 *
183 * Select all text, leaving the text cursor position at the end.
184 *
185 * @entry: A CajaEntry
186 **/
187 void
caja_entry_select_all(CajaEntry * entry)188 caja_entry_select_all (CajaEntry *entry)
189 {
190 g_return_if_fail (CAJA_IS_ENTRY (entry));
191
192 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
193 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
194 }
195
196 static gboolean
select_all_at_idle(gpointer callback_data)197 select_all_at_idle (gpointer callback_data)
198 {
199 CajaEntry *entry;
200
201 entry = CAJA_ENTRY (callback_data);
202
203 caja_entry_select_all (entry);
204
205 entry->details->select_idle_id = 0;
206
207 return FALSE;
208 }
209
210 /**
211 * caja_entry_select_all_at_idle
212 *
213 * Select all text at the next idle, not immediately.
214 * This is useful when reacting to a key press, because
215 * changing the selection and the text cursor position doesn't
216 * work in a key_press signal handler.
217 *
218 * @entry: A CajaEntry
219 **/
220 void
caja_entry_select_all_at_idle(CajaEntry * entry)221 caja_entry_select_all_at_idle (CajaEntry *entry)
222 {
223 g_return_if_fail (CAJA_IS_ENTRY (entry));
224
225 /* If the text cursor position changes in this routine
226 * then gtk_entry_key_press will unselect (and we want
227 * to move the text cursor position to the end).
228 */
229
230 if (entry->details->select_idle_id == 0)
231 {
232 entry->details->select_idle_id = g_idle_add (select_all_at_idle, entry);
233 }
234 }
235
236 /**
237 * caja_entry_set_text
238 *
239 * This function wraps gtk_entry_set_text. It sets undo_registered
240 * to TRUE and preserves the old value for a later restore. This is
241 * done so the programmatic changes to the entry do not register
242 * with the undo manager.
243 *
244 * @entry: A CajaEntry
245 * @test: The text to set
246 **/
247
248 void
caja_entry_set_text(CajaEntry * entry,const gchar * text)249 caja_entry_set_text (CajaEntry *entry, const gchar *text)
250 {
251 g_return_if_fail (CAJA_IS_ENTRY (entry));
252
253 entry->details->user_edit = FALSE;
254 gtk_entry_set_text (GTK_ENTRY (entry), text);
255 entry->details->user_edit = TRUE;
256
257 g_signal_emit (entry, signals[SELECTION_CHANGED], 0);
258 }
259
260 static void
caja_entry_set_selection_bounds(GtkEditable * editable,int start_pos,int end_pos)261 caja_entry_set_selection_bounds (GtkEditable *editable,
262 int start_pos,
263 int end_pos)
264 {
265 parent_editable_interface->set_selection_bounds (editable, start_pos, end_pos);
266
267 g_signal_emit (editable, signals[SELECTION_CHANGED], 0);
268 }
269
270 static gboolean
caja_entry_button_press(GtkWidget * widget,GdkEventButton * event)271 caja_entry_button_press (GtkWidget *widget,
272 GdkEventButton *event)
273 {
274 gboolean result;
275
276 result = GTK_WIDGET_CLASS (caja_entry_parent_class)->button_press_event (widget, event);
277
278 if (result)
279 {
280 g_signal_emit (widget, signals[SELECTION_CHANGED], 0);
281 }
282
283 return result;
284 }
285
286 static gboolean
caja_entry_button_release(GtkWidget * widget,GdkEventButton * event)287 caja_entry_button_release (GtkWidget *widget,
288 GdkEventButton *event)
289 {
290 gboolean result;
291
292 result = GTK_WIDGET_CLASS (caja_entry_parent_class)->button_release_event (widget, event);
293
294 if (result)
295 {
296 g_signal_emit (widget, signals[SELECTION_CHANGED], 0);
297 }
298
299 return result;
300 }
301
302 static void
caja_entry_insert_text(GtkEditable * editable,const gchar * text,int length,int * position)303 caja_entry_insert_text (GtkEditable *editable, const gchar *text,
304 int length, int *position)
305 {
306 CajaEntry *entry;
307
308 entry = CAJA_ENTRY(editable);
309
310 /* Fire off user changed signals */
311 if (entry->details->user_edit)
312 {
313 g_signal_emit (editable, signals[USER_CHANGED], 0);
314 }
315
316 parent_editable_interface->insert_text (editable, text, length, position);
317
318 g_signal_emit (editable, signals[SELECTION_CHANGED], 0);
319 }
320
321 static void
caja_entry_delete_text(GtkEditable * editable,int start_pos,int end_pos)322 caja_entry_delete_text (GtkEditable *editable, int start_pos, int end_pos)
323 {
324 CajaEntry *entry;
325
326 entry = CAJA_ENTRY (editable);
327
328 /* Fire off user changed signals */
329 if (entry->details->user_edit)
330 {
331 g_signal_emit (editable, signals[USER_CHANGED], 0);
332 }
333
334 parent_editable_interface->delete_text (editable, start_pos, end_pos);
335
336 g_signal_emit (editable, signals[SELECTION_CHANGED], 0);
337 }
338
339 /* Overridden to work around GTK bug. The selection_clear_event is queued
340 * when the selection changes. Changing the selection to NULL and then
341 * back to the original selection owner still sends the event, so the
342 * selection owner then gets the selection ripped away from it. We ran into
343 * this with type-completion behavior in CajaLocationBar (see bug 5313).
344 * There's a FIXME comment that seems to be about this same issue in
345 * gtk+/gtkselection.c, gtk_selection_clear.
346 */
347 static gboolean
caja_entry_selection_clear(GtkWidget * widget,GdkEventSelection * event)348 caja_entry_selection_clear (GtkWidget *widget,
349 GdkEventSelection *event)
350 {
351 g_assert (CAJA_IS_ENTRY (widget));
352
353 if (gdk_selection_owner_get (event->selection) == gtk_widget_get_window (widget))
354 {
355 return FALSE;
356 }
357
358 return GTK_WIDGET_CLASS (caja_entry_parent_class)->selection_clear_event (widget, event);
359 }
360
361 static void
caja_entry_editable_init(GtkEditableInterface * iface)362 caja_entry_editable_init (GtkEditableInterface *iface)
363 {
364 parent_editable_interface = g_type_interface_peek_parent (iface);
365
366 iface->insert_text = caja_entry_insert_text;
367 iface->delete_text = caja_entry_delete_text;
368 iface->set_selection_bounds = caja_entry_set_selection_bounds;
369
370 /* Otherwise we might need some memcpy loving */
371 g_assert (iface->do_insert_text != NULL);
372 g_assert (iface->get_position != NULL);
373 g_assert (iface->get_chars != NULL);
374 }
375
376 static void
caja_entry_class_init(CajaEntryClass * class)377 caja_entry_class_init (CajaEntryClass *class)
378 {
379 GtkWidgetClass *widget_class;
380 GObjectClass *gobject_class;
381
382 widget_class = GTK_WIDGET_CLASS (class);
383 gobject_class = G_OBJECT_CLASS (class);
384
385 widget_class->button_press_event = caja_entry_button_press;
386 widget_class->button_release_event = caja_entry_button_release;
387 widget_class->key_press_event = caja_entry_key_press;
388 widget_class->motion_notify_event = caja_entry_motion_notify;
389 widget_class->selection_clear_event = caja_entry_selection_clear;
390
391 gobject_class->finalize = caja_entry_finalize;
392
393 /* Set up signals */
394 signals[USER_CHANGED] = g_signal_new
395 ("user_changed",
396 G_TYPE_FROM_CLASS (class),
397 G_SIGNAL_RUN_LAST,
398 G_STRUCT_OFFSET (CajaEntryClass, user_changed),
399 NULL, NULL,
400 g_cclosure_marshal_VOID__VOID,
401 G_TYPE_NONE, 0);
402 signals[SELECTION_CHANGED] = g_signal_new
403 ("selection_changed",
404 G_TYPE_FROM_CLASS (class),
405 G_SIGNAL_RUN_LAST,
406 G_STRUCT_OFFSET (CajaEntryClass, selection_changed),
407 NULL, NULL,
408 g_cclosure_marshal_VOID__VOID,
409 G_TYPE_NONE, 0);
410 }
411
412 void
caja_entry_set_special_tab_handling(CajaEntry * entry,gboolean special_tab_handling)413 caja_entry_set_special_tab_handling (CajaEntry *entry,
414 gboolean special_tab_handling)
415 {
416 g_return_if_fail (CAJA_IS_ENTRY (entry));
417
418 entry->details->special_tab_handling = special_tab_handling;
419 }
420
421
422