1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include <pango/pango.h>
33 #include <gdk/gdkkeysyms.h>
35 #include "gtkitementry.h"
37 #define MIN_ENTRY_WIDTH 150
38 #define DRAW_TIMEOUT 20
39 #define INNER_BORDER 0
41 /* Initial size of buffer, in bytes */
44 /* Maximum size of text buffer, in bytes */
45 #define MAX_SIZE G_MAXUSHORT
52 /* GObject, GtkObject methods
54 static void gtk_item_entry_class_init (GtkItemEntryClass *klass);
55 static void gtk_item_entry_init (GtkItemEntry *entry);
56 static void gtk_item_entry_editable_init (GtkEditableClass *iface);
60 static void gtk_entry_realize (GtkWidget *widget);
61 static void gtk_entry_size_request (GtkWidget *widget,
62 GtkRequisition *requisition);
63 static void gtk_entry_size_allocate (GtkWidget *widget,
64 GtkAllocation *allocation);
65 static void gtk_entry_draw_frame (GtkWidget *widget);
66 static gint gtk_entry_expose (GtkWidget *widget,
67 GdkEventExpose *event);
68 static void gtk_entry_grab_focus (GtkWidget *widget);
69 static void gtk_entry_style_set (GtkWidget *widget,
70 GtkStyle *previous_style);
71 static void gtk_entry_direction_changed (GtkWidget *widget,
72 GtkTextDirection previous_dir);
73 static void gtk_entry_state_changed (GtkWidget *widget,
74 GtkStateType previous_state);
76 /* GtkEditable method implementations
78 static void gtk_entry_insert_text (GtkEditable *editable,
79 const gchar *new_text,
82 static void gtk_entry_delete_text (GtkEditable *editable,
86 static void gtk_entry_real_set_position (GtkEditable *editable,
88 static gint gtk_entry_get_position (GtkEditable *editable);
90 /* Default signal handlers
92 static void gtk_entry_real_insert_text (GtkEditable *editable,
93 const gchar *new_text,
96 static void gtk_entry_real_delete_text (GtkEditable *editable,
99 static void gtk_entry_move_cursor (GtkEntry *entry,
100 GtkMovementStep step,
102 gboolean extend_selection);
103 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
105 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
109 /* IM Context Callbacks
111 static void gtk_entry_commit_cb (GtkIMContext *context,
114 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
116 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
118 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
125 static void gtk_entry_enter_text (GtkEntry *entry,
127 static void gtk_entry_set_positions (GtkEntry *entry,
129 gint selection_bound);
130 static void gtk_entry_draw_text (GtkEntry *entry);
131 static void gtk_entry_draw_cursor (GtkEntry *entry,
133 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
134 gboolean include_preedit);
135 static void gtk_entry_queue_draw (GtkEntry *entry);
136 static void gtk_entry_reset_im_context (GtkEntry *entry);
137 static void gtk_entry_recompute (GtkEntry *entry);
138 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
142 static void gtk_entry_adjust_scroll (GtkEntry *entry);
143 static gint gtk_entry_move_visually (GtkEntry *editable,
146 static gint gtk_entry_move_logically (GtkEntry *entry,
149 static gint gtk_entry_move_forward_word (GtkEntry *entry,
151 static gint gtk_entry_move_backward_word (GtkEntry *entry,
153 static void gtk_entry_delete_whitespace (GtkEntry *entry);
154 static char * gtk_entry_get_public_chars (GtkEntry *entry,
157 static void gtk_entry_update_primary_selection (GtkEntry *entry);
158 static void gtk_entry_state_changed (GtkWidget *widget,
159 GtkStateType previous_state);
160 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
161 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
162 static void get_text_area_size (GtkEntry *entry,
167 static void get_widget_window_size (GtkEntry *entry,
173 static GtkEntryClass *parent_class = NULL;
176 gtk_item_entry_get_type (void)
178 static GtkType item_entry_type = 0;
180 if (!item_entry_type)
182 static const GtkTypeInfo item_entry_info =
185 sizeof (GtkItemEntry),
186 sizeof (GtkItemEntryClass),
187 (GtkClassInitFunc) gtk_item_entry_class_init,
188 (GtkObjectInitFunc) gtk_item_entry_init,
189 /* reserved_1 */ NULL,
190 /* reserved_2 */ NULL,
191 (GtkClassInitFunc) NULL,
194 static const GInterfaceInfo item_editable_info =
196 (GInterfaceInitFunc) gtk_item_entry_editable_init, /* interface_init */
197 NULL, /* interface_finalize */
198 NULL /* interface_data */
202 item_entry_type = gtk_type_unique (GTK_TYPE_ENTRY, &item_entry_info);
204 g_type_add_interface_static (item_entry_type,
206 &item_editable_info);
210 return item_entry_type;
214 gtk_item_entry_class_init (GtkItemEntryClass *class)
216 GtkObjectClass *object_class;
217 GtkWidgetClass *widget_class;
218 GtkEntryClass *entry_class;
220 object_class = (GtkObjectClass*) class;
221 widget_class = (GtkWidgetClass*) class;
222 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
223 entry_class = (GtkEntryClass *) class;
225 widget_class->realize = gtk_entry_realize;
226 widget_class->size_request = gtk_entry_size_request;
227 widget_class->size_allocate = gtk_entry_size_allocate;
228 widget_class->expose_event = gtk_entry_expose;
229 widget_class->grab_focus = gtk_entry_grab_focus;
230 widget_class->style_set = gtk_entry_style_set;
231 widget_class->direction_changed = gtk_entry_direction_changed;
232 widget_class->state_changed = gtk_entry_state_changed;
234 entry_class->move_cursor = gtk_entry_move_cursor;
235 entry_class->insert_at_cursor = gtk_entry_insert_at_cursor;
236 entry_class->delete_from_cursor = gtk_entry_delete_from_cursor;
241 gtk_item_entry_editable_init (GtkEditableClass *iface)
243 iface->do_insert_text = gtk_entry_insert_text;
244 iface->do_delete_text = gtk_entry_delete_text;
245 iface->insert_text = gtk_entry_real_insert_text;
246 iface->delete_text = gtk_entry_real_delete_text;
247 iface->set_position = gtk_entry_real_set_position;
248 iface->get_position = gtk_entry_get_position;
252 gtk_item_entry_init (GtkItemEntry *entry)
254 entry->justification = GTK_JUSTIFY_LEFT;
255 entry->text_max_size = 0;
256 GTK_ENTRY(entry)->has_frame = FALSE;
258 g_object_unref(G_OBJECT(GTK_ENTRY(entry)->im_context));
260 GTK_ENTRY(entry)->im_context = gtk_im_multicontext_new ();
262 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "commit",
263 G_CALLBACK (gtk_entry_commit_cb), entry);
264 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "preedit_changed",
265 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
266 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "retrieve_surrounding",
267 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
268 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "delete_surrounding",
269 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
274 gtk_entry_realize (GtkWidget *widget)
277 GtkEditable *editable;
278 GdkWindowAttr attributes;
279 gint attributes_mask;
281 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
282 entry = GTK_ENTRY (widget);
283 editable = GTK_EDITABLE (widget);
285 attributes.window_type = GDK_WINDOW_CHILD;
287 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
289 attributes.wclass = GDK_INPUT_OUTPUT;
290 attributes.visual = gtk_widget_get_visual (widget);
291 attributes.colormap = gtk_widget_get_colormap (widget);
292 attributes.event_mask = gtk_widget_get_events (widget);
293 attributes.event_mask |= (GDK_EXPOSURE_MASK |
294 GDK_BUTTON_PRESS_MASK |
295 GDK_BUTTON_RELEASE_MASK |
296 GDK_BUTTON1_MOTION_MASK |
297 GDK_BUTTON3_MOTION_MASK |
298 GDK_POINTER_MOTION_HINT_MASK |
299 GDK_POINTER_MOTION_MASK |
300 GDK_ENTER_NOTIFY_MASK |
301 GDK_LEAVE_NOTIFY_MASK);
302 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
304 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
305 gdk_window_set_user_data (widget->window, entry);
307 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
309 attributes.cursor = gdk_cursor_new (GDK_XTERM);
310 attributes_mask |= GDK_WA_CURSOR;
312 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
313 gdk_window_set_user_data (entry->text_area, entry);
315 gdk_cursor_unref (attributes.cursor);
317 widget->style = gtk_style_attach (widget->style, widget->window);
319 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE(widget)]);
320 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
322 gdk_window_show (entry->text_area);
324 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
326 gtk_entry_adjust_scroll (entry);
330 get_borders (GtkEntry *entry,
334 GtkWidget *widget = GTK_WIDGET (entry);
336 gboolean interior_focus;
338 gtk_widget_style_get (widget,
339 "interior-focus", &interior_focus,
340 "focus-line-width", &focus_width,
343 if (entry->has_frame)
345 *xborder = widget->style->xthickness;
346 *yborder = widget->style->ythickness;
356 *xborder += focus_width;
357 *yborder += focus_width;
363 gtk_entry_size_request (GtkWidget *widget,
364 GtkRequisition *requisition)
366 GtkEntry *entry = GTK_ENTRY (widget);
367 PangoFontMetrics *metrics;
368 gint xborder, yborder;
369 PangoContext *context;
371 context = gtk_widget_get_pango_context (widget);
372 metrics = pango_context_get_metrics (context,
373 widget->style->font_desc,
374 pango_context_get_language (context));
376 entry->ascent = pango_font_metrics_get_ascent (metrics);
377 entry->descent = pango_font_metrics_get_descent (metrics);
379 get_borders (entry, &xborder, &yborder);
381 xborder += INNER_BORDER;
382 yborder += INNER_BORDER;
384 if (entry->width_chars < 0)
385 requisition->width = MIN_ENTRY_WIDTH + xborder * 2;
388 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
389 requisition->width = PANGO_PIXELS (char_width) * entry->width_chars + xborder * 2;
392 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2;
394 pango_font_metrics_unref (metrics);
398 get_text_area_size (GtkEntry *entry,
404 gint xborder, yborder;
405 GtkRequisition requisition;
406 GtkWidget *widget = GTK_WIDGET (entry);
408 gtk_widget_get_child_requisition (widget, &requisition);
410 get_borders (entry, &xborder, &yborder);
419 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
422 *height = requisition.height - yborder * 2;
426 get_widget_window_size (GtkEntry *entry,
432 GtkRequisition requisition;
433 GtkWidget *widget = GTK_WIDGET (entry);
435 gtk_widget_get_child_requisition (widget, &requisition);
438 *x = widget->allocation.x;
442 if (entry->is_cell_renderer)
443 *y = widget->allocation.y;
445 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
449 *width = widget->allocation.width;
453 if (entry->is_cell_renderer)
454 *height = widget->allocation.height;
456 *height = requisition.height;
461 gtk_entry_size_allocate (GtkWidget *widget,
462 GtkAllocation *allocation)
464 GtkEntry *entry = GTK_ENTRY (widget);
465 GtkItemEntry *ientry = GTK_ITEM_ENTRY (widget);
467 if(ientry->text_max_size > 0)
468 allocation->width = MIN(ientry->text_max_size, allocation->width);
470 widget->allocation = *allocation;
472 if (GTK_WIDGET_REALIZED (widget))
474 /* We call gtk_widget_get_child_requisition, since we want (for
475 * backwards compatibility reasons) the realization here to
476 * be affected by the usize of the entry, if set
478 gint x, y, width, height;
480 get_widget_window_size (entry, &x, &y, &width, &height);
482 gdk_window_move_resize (widget->window,
483 allocation->x, allocation->y, allocation->width, allocation->height);
485 get_text_area_size (entry, &x, &y, &width, &height);
487 gdk_window_move_resize (entry->text_area,
488 0, allocation->height - height, allocation->width, height);
490 gtk_entry_recompute (entry);
495 gtk_entry_draw_frame (GtkWidget *widget)
500 gtk_entry_expose (GtkWidget *widget,
501 GdkEventExpose *event)
503 GtkEntry *entry = GTK_ENTRY (widget);
505 if (widget->window == event->window)
506 gtk_entry_draw_frame (widget);
507 else if (entry->text_area == event->window)
509 gint area_width, area_height;
511 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
513 gdk_draw_rectangle (entry->text_area,
514 widget->style->bg_gc[GTK_WIDGET_STATE(widget)],
516 0, 0, area_width, area_height);
518 if ((entry->visible || entry->invisible_char != 0) &&
519 GTK_WIDGET_HAS_FOCUS (widget) &&
520 entry->selection_bound == entry->current_pos && entry->cursor_visible)
521 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
523 if (entry->dnd_position != -1)
524 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
526 gtk_entry_draw_text (GTK_ENTRY (widget));
533 gtk_entry_grab_focus (GtkWidget *widget)
535 GtkEntry *entry = GTK_ENTRY (widget);
536 gboolean select_on_focus;
538 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
540 g_object_get (G_OBJECT (gtk_settings_get_default ()),
541 "gtk-entry-select-on-focus",
545 if (select_on_focus && entry->editable && !entry->in_click)
546 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
550 gtk_entry_direction_changed (GtkWidget *widget,
551 GtkTextDirection previous_dir)
553 GtkEntry *entry = GTK_ENTRY (widget);
555 gtk_entry_recompute (entry);
557 GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
561 gtk_entry_state_changed (GtkWidget *widget,
562 GtkStateType previous_state)
564 GtkEntry *entry = GTK_ENTRY (widget);
566 if (GTK_WIDGET_REALIZED (widget))
568 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
569 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
572 if (!GTK_WIDGET_IS_SENSITIVE (widget))
574 /* Clear any selection */
575 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
578 gtk_widget_queue_clear (widget);
581 /* GtkEditable method implementations
584 gtk_entry_insert_text (GtkEditable *editable,
585 const gchar *new_text,
586 gint new_text_length,
589 GtkEntry *entry = GTK_ENTRY (editable);
593 if (*position < 0 || *position > entry->text_length)
594 *position = entry->text_length;
596 g_object_ref (G_OBJECT (editable));
598 if (new_text_length <= 63)
601 text = g_new (gchar, new_text_length + 1);
603 text[new_text_length] = '\0';
604 strncpy (text, new_text, new_text_length);
606 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
608 if (new_text_length > 63)
611 g_object_unref (G_OBJECT (editable));
615 gtk_entry_delete_text (GtkEditable *editable,
619 GtkEntry *entry = GTK_ENTRY (editable);
621 if (end_pos < 0 || end_pos > entry->text_length)
622 end_pos = entry->text_length;
625 if (start_pos > end_pos)
628 g_object_ref (G_OBJECT (editable));
630 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
632 g_object_unref (G_OBJECT (editable));
636 gtk_entry_style_set (GtkWidget *widget,
637 GtkStyle *previous_style)
639 GtkEntry *entry = GTK_ENTRY (widget);
641 if (previous_style && GTK_WIDGET_REALIZED (widget))
643 gtk_entry_recompute (entry);
645 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE(widget)]);
646 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
651 gtk_entry_real_set_position (GtkEditable *editable,
654 GtkEntry *entry = GTK_ENTRY (editable);
656 if (position < 0 || position > entry->text_length)
657 position = entry->text_length;
659 if (position != entry->current_pos ||
660 position != entry->selection_bound)
662 gtk_entry_reset_im_context (entry);
663 gtk_entry_set_positions (entry, position, position);
668 gtk_entry_get_position (GtkEditable *editable)
670 return GTK_ENTRY (editable)->current_pos;
674 /* Default signal handlers
677 gtk_entry_real_insert_text (GtkEditable *editable,
678 const gchar *new_text,
679 gint new_text_length,
685 GtkEntry *entry = GTK_ENTRY (editable);
687 if (new_text_length < 0)
688 new_text_length = strlen (new_text);
690 n_chars = g_utf8_strlen (new_text, new_text_length);
691 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
694 n_chars = entry->text_max_length - entry->text_length;
695 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
698 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
700 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
702 if (entry->text_size == 0)
703 entry->text_size = MIN_SIZE;
706 if (2 * (guint)entry->text_size < MAX_SIZE &&
707 2 * (guint)entry->text_size > entry->text_size)
708 entry->text_size *= 2;
711 entry->text_size = MAX_SIZE;
712 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
714 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
715 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
716 n_chars = g_utf8_strlen (new_text, new_text_length);
723 entry->text = g_realloc (entry->text, entry->text_size);
726 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
728 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
729 memcpy (entry->text + index, new_text, new_text_length);
731 entry->n_bytes += new_text_length;
732 entry->text_length += n_chars;
734 /* NUL terminate for safety and convenience */
735 entry->text[entry->n_bytes] = '\0';
737 if (entry->current_pos > *position)
738 entry->current_pos += n_chars;
740 if (entry->selection_bound > *position)
741 entry->selection_bound += n_chars;
743 *position += n_chars;
745 gtk_entry_recompute (entry);
747 g_signal_emit_by_name (editable, "changed");
748 g_object_notify (G_OBJECT (editable), "text");
752 gtk_entry_real_delete_text (GtkEditable *editable,
756 GtkEntry *entry = GTK_ENTRY (editable);
760 if (end_pos < 0 || end_pos > entry->text_length)
761 end_pos = entry->text_length;
763 if (start_pos < end_pos)
765 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
766 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
768 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
769 entry->text_length -= (end_pos - start_pos);
770 entry->n_bytes -= (end_index - start_index);
772 if (entry->current_pos > start_pos)
773 entry->current_pos -= MIN (entry->current_pos, end_pos) - start_pos;
775 if (entry->selection_bound > start_pos)
776 entry->selection_bound -= MIN (entry->selection_bound, end_pos) - start_pos;
777 /* We might have deleted the selection
779 gtk_entry_update_primary_selection (entry);
781 gtk_entry_recompute (entry);
783 g_signal_emit_by_name (editable, "changed");
784 g_object_notify (G_OBJECT (editable), "text");
788 /* Compute the X position for an offset that corresponds to the "more important
789 * cursor position for that offset. We use this when trying to guess to which
790 * end of the selection we should go to when the user hits the left or
794 get_better_cursor_x (GtkEntry *entry,
797 GtkTextDirection keymap_direction =
798 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
799 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
800 GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
801 gboolean split_cursor;
803 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
804 gint index = g_utf8_offset_to_pointer (entry->text, offset) - entry->text;
806 PangoRectangle strong_pos, weak_pos;
808 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
809 "gtk-split-cursor", &split_cursor,
812 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
815 return strong_pos.x / PANGO_SCALE;
817 return (keymap_direction == widget_direction) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
821 gtk_entry_move_cursor (GtkEntry *entry,
822 GtkMovementStep step,
824 gboolean extend_selection)
826 gint new_pos = entry->current_pos;
828 gtk_entry_reset_im_context (entry);
830 if (entry->current_pos != entry->selection_bound && !extend_selection)
832 /* If we have a current selection and aren't extending it, move to the
833 * start/or end of the selection as appropriate
837 case GTK_MOVEMENT_VISUAL_POSITIONS:
839 gint current_x = get_better_cursor_x (entry, entry->current_pos);
840 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
843 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
845 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
849 case GTK_MOVEMENT_LOGICAL_POSITIONS:
850 case GTK_MOVEMENT_WORDS:
852 new_pos = MIN (entry->current_pos, entry->selection_bound);
854 new_pos = MAX (entry->current_pos, entry->selection_bound);
856 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
857 case GTK_MOVEMENT_PARAGRAPH_ENDS:
858 case GTK_MOVEMENT_BUFFER_ENDS:
859 new_pos = count < 0 ? 0 : entry->text_length;
861 case GTK_MOVEMENT_DISPLAY_LINES:
862 case GTK_MOVEMENT_PARAGRAPHS:
863 case GTK_MOVEMENT_PAGES:
873 case GTK_MOVEMENT_LOGICAL_POSITIONS:
874 new_pos = gtk_entry_move_logically (entry, new_pos, count);
876 case GTK_MOVEMENT_VISUAL_POSITIONS:
877 new_pos = gtk_entry_move_visually (entry, new_pos, count);
879 case GTK_MOVEMENT_WORDS:
882 new_pos = gtk_entry_move_forward_word (entry, new_pos);
887 new_pos = gtk_entry_move_backward_word (entry, new_pos);
891 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
892 case GTK_MOVEMENT_PARAGRAPH_ENDS:
893 case GTK_MOVEMENT_BUFFER_ENDS:
894 new_pos = count < 0 ? 0 : entry->text_length;
896 case GTK_MOVEMENT_DISPLAY_LINES:
897 case GTK_MOVEMENT_PARAGRAPHS:
898 case GTK_MOVEMENT_PAGES:
905 if (extend_selection)
906 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
908 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
910 gtk_entry_pend_cursor_blink (entry);
914 gtk_entry_insert_at_cursor (GtkEntry *entry,
917 GtkEditable *editable = GTK_EDITABLE (entry);
918 gint pos = entry->current_pos;
922 gtk_entry_reset_im_context (entry);
924 gtk_editable_insert_text (editable, str, -1, &pos);
925 gtk_editable_set_position (editable, pos);
930 gtk_entry_delete_from_cursor (GtkEntry *entry,
934 GtkEditable *editable = GTK_EDITABLE (entry);
935 gint start_pos = entry->current_pos;
936 gint end_pos = entry->current_pos;
938 gtk_entry_reset_im_context (entry);
940 if (!entry->editable)
943 if (entry->selection_bound != entry->current_pos)
945 gtk_editable_delete_selection (editable);
951 case GTK_DELETE_CHARS:
952 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
953 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
955 case GTK_DELETE_WORDS:
958 /* Move to end of current word, or if not on a word, end of previous word */
959 end_pos = gtk_entry_move_backward_word (entry, end_pos);
960 end_pos = gtk_entry_move_forward_word (entry, end_pos);
964 /* Move to beginning of current word, or if not on a word, begining of next word */
965 start_pos = gtk_entry_move_forward_word (entry, start_pos);
966 start_pos = gtk_entry_move_backward_word (entry, start_pos);
970 case GTK_DELETE_WORD_ENDS:
973 start_pos = gtk_entry_move_backward_word (entry, start_pos);
978 end_pos = gtk_entry_move_forward_word (entry, end_pos);
981 gtk_editable_delete_text (editable, start_pos, end_pos);
983 case GTK_DELETE_DISPLAY_LINE_ENDS:
984 case GTK_DELETE_PARAGRAPH_ENDS:
986 gtk_editable_delete_text (editable, 0, entry->current_pos);
988 gtk_editable_delete_text (editable, entry->current_pos, -1);
990 case GTK_DELETE_DISPLAY_LINES:
991 case GTK_DELETE_PARAGRAPHS:
992 gtk_editable_delete_text (editable, 0, -1);
994 case GTK_DELETE_WHITESPACE:
995 gtk_entry_delete_whitespace (entry);
999 gtk_entry_pend_cursor_blink (entry);
1002 /* IM Context Callbacks
1006 gtk_entry_commit_cb (GtkIMContext *context,
1010 gtk_entry_enter_text (entry, str);
1014 gtk_entry_preedit_changed_cb (GtkIMContext *context,
1017 gchar *preedit_string;
1020 gtk_im_context_get_preedit_string (entry->im_context,
1021 &preedit_string, NULL,
1023 entry->preedit_length = strlen (preedit_string);
1024 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
1025 entry->preedit_cursor = cursor_pos;
1026 g_free (preedit_string);
1028 gtk_entry_recompute (entry);
1032 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
1035 gtk_im_context_set_surrounding (context,
1038 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
1044 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
1049 gtk_editable_delete_text (GTK_EDITABLE (entry),
1050 entry->current_pos + offset,
1051 entry->current_pos + offset + n_chars);
1057 /* Internal functions
1060 /* Used for im_commit_cb and inserting Unicode chars */
1062 gtk_entry_enter_text (GtkEntry *entry,
1065 GtkEditable *editable = GTK_EDITABLE (entry);
1068 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
1069 gtk_editable_delete_selection (editable);
1072 if (entry->overwrite_mode)
1073 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
1076 tmp_pos = entry->current_pos;
1077 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
1078 gtk_editable_set_position (editable, tmp_pos);
1081 /* All changes to entry->current_pos and entry->selection_bound
1082 * should go through this function.
1085 gtk_entry_set_positions (GtkEntry *entry,
1087 gint selection_bound)
1089 gboolean changed = FALSE;
1091 g_object_freeze_notify (G_OBJECT (entry));
1093 if (current_pos != -1 &&
1094 entry->current_pos != current_pos)
1096 entry->current_pos = current_pos;
1099 g_object_notify (G_OBJECT (entry), "cursor_position");
1102 if (selection_bound != -1 &&
1103 entry->selection_bound != selection_bound)
1105 entry->selection_bound = selection_bound;
1108 g_object_notify (G_OBJECT (entry), "selection_bound");
1111 g_object_thaw_notify (G_OBJECT (entry));
1114 gtk_entry_recompute (entry);
1118 gtk_entry_reset_layout (GtkEntry *entry)
1120 if (entry->cached_layout)
1122 g_object_unref (G_OBJECT (entry->cached_layout));
1123 entry->cached_layout = NULL;
1128 update_im_cursor_location (GtkEntry *entry)
1132 gint strong_xoffset;
1133 gint x, y, area_width, area_height;
1135 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
1137 get_text_area_size (entry, &x, &y, &area_width, &area_height);
1139 strong_xoffset = strong_x - entry->scroll_offset;
1140 if (strong_xoffset < 0)
1144 else if (strong_xoffset > area_width)
1146 strong_xoffset = area_width;
1148 area.x = x + strong_xoffset;
1149 area.y = y + area_height;
1150 area.width = area_width;
1151 area.height = area_height;
1153 gtk_im_context_set_cursor_location (entry->im_context, &area);
1157 recompute_idle_func (gpointer data)
1161 GDK_THREADS_ENTER ();
1163 entry = GTK_ENTRY (data);
1165 gtk_entry_adjust_scroll (entry);
1166 gtk_entry_queue_draw (entry);
1168 entry->recompute_idle = FALSE;
1170 update_im_cursor_location (entry);
1172 GDK_THREADS_LEAVE ();
1178 gtk_entry_recompute (GtkEntry *entry)
1180 gtk_entry_reset_layout (entry);
1181 gtk_entry_check_cursor_blink (entry);
1184 if (!entry->recompute_idle)
1186 entry->recompute_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
1187 recompute_idle_func, entry, NULL);
1192 append_char (GString *str,
1200 char_len = g_unichar_to_utf8 (ch, buf);
1205 g_string_append_len (str, buf, char_len);
1210 static PangoLayout *
1211 gtk_entry_create_layout (GtkEntry *entry,
1212 gboolean include_preedit)
1214 PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (entry), NULL);
1215 PangoAttrList *tmp_attrs = pango_attr_list_new ();
1217 gchar *preedit_string = NULL;
1218 gint preedit_length = 0;
1219 PangoAttrList *preedit_attrs = NULL;
1221 pango_layout_set_single_paragraph_mode (layout, TRUE);
1223 if (include_preedit)
1225 gtk_im_context_get_preedit_string (entry->im_context,
1226 &preedit_string, &preedit_attrs, NULL);
1227 preedit_length = entry->preedit_length;
1232 GString *tmp_string = g_string_new (NULL);
1234 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
1238 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
1239 g_string_insert (tmp_string, cursor_index, preedit_string);
1244 gint preedit_len_chars;
1245 gunichar invisible_char;
1247 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
1248 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
1249 ch_len += preedit_len_chars;
1251 if (entry->invisible_char != 0)
1252 invisible_char = entry->invisible_char;
1254 invisible_char = ' '; /* just pick a char */
1256 append_char (tmp_string, invisible_char, ch_len);
1258 /* Fix cursor index to point to invisible char corresponding
1259 * to the preedit, fix preedit_length to be the length of
1260 * the invisible chars representing the preedit
1263 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
1267 g_unichar_to_utf8 (invisible_char, NULL);
1270 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
1272 pango_attr_list_splice (tmp_attrs, preedit_attrs,
1273 cursor_index, preedit_length);
1275 g_string_free (tmp_string, TRUE);
1281 pango_layout_set_text (layout, entry->text, entry->n_bytes);
1285 GString *str = g_string_new (NULL);
1286 gunichar invisible_char;
1288 if (entry->invisible_char != 0)
1289 invisible_char = entry->invisible_char;
1291 invisible_char = ' '; /* just pick a char */
1293 append_char (str, invisible_char, entry->text_length);
1294 pango_layout_set_text (layout, str->str, str->len);
1295 g_string_free (str, TRUE);
1299 pango_layout_set_attributes (layout, tmp_attrs);
1302 g_free (preedit_string);
1304 pango_attr_list_unref (preedit_attrs);
1306 pango_attr_list_unref (tmp_attrs);
1311 static PangoLayout *
1312 gtk_entry_ensure_layout (GtkEntry *entry,
1313 gboolean include_preedit)
1315 if (entry->preedit_length > 0 &&
1316 !include_preedit != !entry->cache_includes_preedit)
1317 gtk_entry_reset_layout (entry);
1319 if (!entry->cached_layout)
1321 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
1322 entry->cache_includes_preedit = include_preedit;
1325 return entry->cached_layout;
1329 get_layout_position (GtkEntry *entry,
1333 PangoLayout *layout;
1334 PangoRectangle logical_rect;
1335 gint area_width, area_height;
1337 PangoLayoutLine *line;
1339 layout = gtk_entry_ensure_layout (entry, TRUE);
1341 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1343 area_height = PANGO_SCALE * (area_height);
1345 line = pango_layout_get_lines (layout)->data;
1346 pango_layout_line_get_extents (line, NULL, &logical_rect);
1348 /* Align primarily for locale's ascent/descent */
1350 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
1351 entry->ascent + logical_rect.y);
1354 /* Now see if we need to adjust to fit in actual drawn string */
1356 if (logical_rect.height > area_height)
1357 y_pos = (area_height - logical_rect.height) / 2;
1360 else if (y_pos + logical_rect.height > area_height)
1361 y_pos = area_height - logical_rect.height;
1363 y_pos = y_pos / PANGO_SCALE;
1366 *x = - entry->scroll_offset;
1373 gtk_entry_draw_text (GtkEntry *entry)
1376 PangoLayoutLine *line;
1378 if (!entry->visible && entry->invisible_char == 0)
1381 if (GTK_WIDGET_DRAWABLE (entry))
1383 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1384 gint area_width, area_height;
1387 gint start_pos, end_pos;
1389 widget = GTK_WIDGET (entry);
1391 get_layout_position (entry, &x, &y);
1393 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1396 gdk_draw_layout (entry->text_area, widget->style->text_gc [widget->state],
1401 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
1405 PangoRectangle logical_rect;
1406 const gchar *text = pango_layout_get_text (layout);
1407 gint start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
1408 gint end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
1409 GdkRegion *clip_region = gdk_region_new ();
1411 GdkGC *selection_gc;
1413 line = pango_layout_get_lines (layout)->data;
1415 pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
1417 pango_layout_get_extents (layout, NULL, &logical_rect);
1419 if (GTK_WIDGET_HAS_FOCUS (entry))
1421 selection_gc = widget->style->base_gc [GTK_STATE_SELECTED];
1422 text_gc = widget->style->text_gc [GTK_STATE_SELECTED];
1426 selection_gc = widget->style->base_gc [GTK_STATE_ACTIVE];
1427 text_gc = widget->style->text_gc [GTK_STATE_ACTIVE];
1430 for (i=0; i < n_ranges; i++)
1434 rect.x = INNER_BORDER - entry->scroll_offset + ranges[2*i] / PANGO_SCALE;
1436 rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
1437 rect.height = logical_rect.height / PANGO_SCALE;
1439 gdk_draw_rectangle (entry->text_area, selection_gc, TRUE,
1440 rect.x, rect.y, rect.width, rect.height);
1442 gdk_region_union_with_rect (clip_region, &rect);
1445 gdk_gc_set_clip_region (text_gc, clip_region);
1446 gdk_draw_layout (entry->text_area, text_gc,
1449 gdk_gc_set_clip_region (text_gc, NULL);
1451 gdk_region_destroy (clip_region);
1458 * From _gtk_get_insertion_cursor_gc
1461 typedef struct _CursorInfo CursorInfo;
1467 GdkGC *secondary_gc;
1471 make_cursor_gc (GtkWidget *widget,
1472 const gchar *property_name,
1475 GdkGCValues gc_values;
1476 GdkGCValuesMask gc_values_mask;
1477 GdkColor *cursor_color;
1479 gtk_widget_style_get (widget, property_name, &cursor_color, NULL);
1481 gc_values_mask = GDK_GC_FOREGROUND;
1484 gc_values.foreground = *cursor_color;
1485 gdk_color_free (cursor_color);
1488 gc_values.foreground = *fallback;
1490 gdk_rgb_find_color (widget->style->colormap, &gc_values.foreground);
1491 return gtk_gc_get (widget->style->depth, widget->style->colormap,
1492 &gc_values, gc_values_mask);
1496 _gtkextra_get_insertion_cursor_gc (GtkWidget *widget,
1497 gboolean is_primary)
1499 CursorInfo *cursor_info;
1501 cursor_info = g_object_get_data (G_OBJECT (widget->style), "gtk-style-cursor-info");
1504 cursor_info = g_new (CursorInfo, 1);
1505 g_object_set_data (G_OBJECT (widget->style), "gtk-style-cursor-info", cursor_info);
1506 cursor_info->primary_gc = NULL;
1507 cursor_info->secondary_gc = NULL;
1508 cursor_info->for_type = G_TYPE_INVALID;
1511 /* We have to keep track of the type because gtk_widget_style_get()
1512 * can return different results when called on the same property and
1513 * same style but for different widgets. :-(. That is,
1514 * GtkEntry::cursor-color = "red" in a style will modify the cursor
1515 * color for entries but not for text view.
1517 if (cursor_info->for_type != G_OBJECT_TYPE (widget))
1519 cursor_info->for_type = G_OBJECT_TYPE (widget);
1520 if (cursor_info->primary_gc)
1522 gtk_gc_release (cursor_info->primary_gc);
1523 cursor_info->primary_gc = NULL;
1525 if (cursor_info->secondary_gc)
1527 gtk_gc_release (cursor_info->secondary_gc);
1528 cursor_info->secondary_gc = NULL;
1534 if (!cursor_info->primary_gc)
1535 cursor_info->primary_gc = make_cursor_gc (widget,
1537 &widget->style->black);
1539 return g_object_ref (cursor_info->primary_gc);
1543 static GdkColor gray = { 0, 0x8888, 0x8888, 0x8888 };
1545 if (!cursor_info->secondary_gc)
1546 cursor_info->secondary_gc = make_cursor_gc (widget,
1547 "secondary-cursor-color",
1550 return g_object_ref (cursor_info->secondary_gc);
1555 * From _gtk_draw_insertion_cursor
1558 _gtkextra_draw_insertion_cursor (GtkWidget *widget,
1559 GdkDrawable *drawable,
1561 GdkRectangle *location,
1562 GtkTextDirection direction,
1563 gboolean draw_arrow)
1569 gfloat cursor_aspect_ratio;
1572 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
1574 gtk_widget_style_get (widget, "cursor-aspect-ratio", &cursor_aspect_ratio, NULL);
1576 stem_width = location->height * cursor_aspect_ratio + 1;
1577 arrow_width = stem_width + 1;
1579 /* put (stem_width % 2) on the proper side of the cursor */
1580 if (direction == GTK_TEXT_DIR_LTR)
1581 offset = stem_width / 2;
1583 offset = stem_width - stem_width / 2;
1585 for (i = 0; i < stem_width; i++)
1586 gdk_draw_line (drawable, gc,
1587 location->x + i - offset, location->y,
1588 location->x + i - offset, location->y + location->height - 1);
1592 if (direction == GTK_TEXT_DIR_RTL)
1594 x = location->x - offset - 1;
1595 y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
1597 for (i = 0; i < arrow_width; i++)
1599 gdk_draw_line (drawable, gc,
1601 x, y + 2 * arrow_width - i - 1);
1605 else if (direction == GTK_TEXT_DIR_LTR)
1607 x = location->x + stem_width - offset;
1608 y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
1610 for (i = 0; i < arrow_width; i++)
1612 gdk_draw_line (drawable, gc,
1614 x, y + 2 * arrow_width - i - 1);
1622 gtk_entry_draw_cursor (GtkEntry *entry,
1625 GtkTextDirection keymap_direction =
1626 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1627 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1628 GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
1630 if (GTK_WIDGET_DRAWABLE (entry) && GTK_ENTRY(entry)->cursor_visible)
1632 GtkWidget *widget = GTK_WIDGET (entry);
1633 GdkRectangle cursor_location;
1634 gboolean split_cursor;
1636 gint xoffset = INNER_BORDER - entry->scroll_offset;
1637 gint strong_x, weak_x;
1638 gint text_area_height;
1639 GtkTextDirection dir1 = GTK_TEXT_DIR_NONE;
1640 GtkTextDirection dir2 = GTK_TEXT_DIR_NONE;
1645 gdk_window_get_size (entry->text_area, NULL, &text_area_height);
1647 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
1649 g_object_get (gtk_widget_get_settings (widget),
1650 "gtk-split-cursor", &split_cursor,
1653 dir1 = widget_direction;
1659 if (weak_x != strong_x)
1661 dir2 = (widget_direction == GTK_TEXT_DIR_LTR) ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
1667 if (keymap_direction == widget_direction)
1673 cursor_location.x = xoffset + x1;
1674 cursor_location.y = INNER_BORDER;
1675 cursor_location.width = 0;
1676 cursor_location.height = text_area_height - 2 * INNER_BORDER ;
1678 gc = _gtkextra_get_insertion_cursor_gc (widget, TRUE);
1679 _gtkextra_draw_insertion_cursor (widget, entry->text_area, gc,
1680 &cursor_location, dir1,
1681 dir2 != GTK_TEXT_DIR_NONE);
1682 g_object_unref (gc);
1684 if (dir2 != GTK_TEXT_DIR_NONE)
1686 cursor_location.x = xoffset + x2;
1687 gc = _gtkextra_get_insertion_cursor_gc (widget, FALSE);
1688 _gtkextra_draw_insertion_cursor (widget, entry->text_area, gc,
1689 &cursor_location, dir2,
1691 g_object_unref (gc);
1697 gtk_entry_queue_draw (GtkEntry *entry)
1699 if (GTK_WIDGET_REALIZED (entry))
1700 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
1704 gtk_entry_reset_im_context (GtkEntry *entry)
1706 if (entry->need_im_reset)
1708 entry->need_im_reset = 0;
1709 gtk_im_context_reset (entry->im_context);
1714 gtk_entry_get_cursor_locations (GtkEntry *entry,
1719 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1721 PangoRectangle strong_pos, weak_pos;
1724 if (type == CURSOR_STANDARD)
1726 text = pango_layout_get_text (layout);
1727 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
1729 else /* type == CURSOR_DND */
1731 index = g_utf8_offset_to_pointer (entry->text, entry->dnd_position) - entry->text;
1732 if (entry->dnd_position > entry->current_pos)
1733 index += entry->preedit_length;
1736 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
1739 *strong_x = strong_pos.x / PANGO_SCALE;
1742 *weak_x = weak_pos.x / PANGO_SCALE;
1746 gtk_entry_adjust_scroll (GtkEntry *entry)
1748 gint min_offset, max_offset;
1749 gint text_area_width;
1750 gint strong_x, weak_x;
1751 PangoLayout *layout;
1752 PangoLayoutLine *line;
1753 PangoRectangle logical_rect;
1754 GtkItemEntry *item_entry;
1757 if (!GTK_WIDGET_REALIZED (entry))
1760 item_entry = GTK_ITEM_ENTRY(entry);
1762 gdk_window_get_size (entry->text_area, &text_area_width, NULL);
1763 text_area_width -= 2 * INNER_BORDER;
1765 layout = gtk_entry_ensure_layout (entry, TRUE);
1766 line = pango_layout_get_lines (layout)->data;
1768 pango_layout_line_get_extents (line, NULL, &logical_rect);
1769 text_width = logical_rect.width / PANGO_SCALE + 2; /* 2 for cursor */
1771 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
1773 /* Display as much text as we can */
1775 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_LTR)
1777 entry->scroll_offset = 0;
1778 switch(item_entry->justification){
1780 case GTK_JUSTIFY_FILL:
1781 case GTK_JUSTIFY_LEFT:
1783 /* LEFT JUSTIFICATION */
1785 strong_x -= entry->scroll_offset;
1787 entry->scroll_offset += strong_x;
1788 else if (strong_x > text_area_width){
1789 if(item_entry->text_max_size != 0 &&
1790 text_area_width + 2 <= item_entry->text_max_size){
1791 GtkAllocation allocation;
1792 allocation = GTK_WIDGET(entry)->allocation;
1793 allocation.width += text_width - text_area_width;
1794 entry->scroll_offset = 0;
1795 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1797 entry->scroll_offset += (strong_x - text_area_width) + 1;
1803 case GTK_JUSTIFY_RIGHT:
1805 /* RIGHT JUSTIFICATION FOR NUMBERS */
1808 entry->scroll_offset= -(text_area_width - text_width) + 1;
1809 if(entry->scroll_offset > 0){
1810 if(item_entry->text_max_size != 0 &&
1811 text_area_width + 2 <= item_entry->text_max_size){
1812 GtkAllocation allocation;
1813 allocation = GTK_WIDGET(entry)->allocation;
1814 allocation.x -= text_width - text_area_width;
1815 allocation.width += text_width - text_area_width;
1816 entry->scroll_offset = 0;
1817 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1821 entry->scroll_offset= -(text_area_width - strong_x) + 1;
1822 if(entry->scroll_offset < 0) entry->scroll_offset = 0;
1827 entry->scroll_offset=0;
1830 case GTK_JUSTIFY_CENTER:
1834 entry->scroll_offset= -(text_area_width - text_width)/2;
1835 if(entry->scroll_offset > 0){
1836 if(item_entry->text_max_size != 0 &&
1837 text_area_width+1<=item_entry->text_max_size){
1838 GtkAllocation allocation;
1839 allocation = GTK_WIDGET(entry)->allocation;
1840 allocation.x += (text_area_width/2 - text_width/2);
1841 allocation.width += text_width - text_area_width;
1842 entry->scroll_offset = 0;
1843 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1847 entry->scroll_offset= -(text_area_width - strong_x) + 1;
1848 if(entry->scroll_offset < 0) entry->scroll_offset = 0;
1853 entry->scroll_offset=0;
1862 max_offset = text_width - text_area_width;
1863 min_offset = MIN (0, max_offset);
1864 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
1867 g_object_notify (G_OBJECT (entry), "scroll_offset");
1871 gtk_entry_move_visually (GtkEntry *entry,
1876 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1879 text = pango_layout_get_text (layout);
1881 index = g_utf8_offset_to_pointer (text, start) - text;
1885 int new_index, new_trailing;
1886 gboolean split_cursor;
1889 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
1890 "gtk-split-cursor", &split_cursor,
1897 GtkTextDirection keymap_direction =
1898 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1899 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1901 strong = keymap_direction == gtk_widget_get_direction (GTK_WIDGET (entry));
1906 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
1911 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
1915 if (new_index < 0 || new_index == G_MAXINT)
1920 while (new_trailing--)
1921 index = g_utf8_next_char (entry->text + new_index) - entry->text;
1924 return g_utf8_pointer_to_offset (text, text + index);
1928 gtk_entry_move_logically (GtkEntry *entry,
1932 gint new_pos = start;
1934 /* Prevent any leak of information */
1935 if (!entry->visible)
1937 new_pos = CLAMP (start + count, 0, entry->text_length);
1939 else if (entry->text)
1941 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1942 PangoLogAttr *log_attrs;
1945 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
1947 while (count > 0 && new_pos < entry->text_length)
1951 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
1955 while (count < 0 && new_pos > 0)
1959 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
1971 gtk_entry_move_forward_word (GtkEntry *entry,
1974 gint new_pos = start;
1976 /* Prevent any leak of information */
1977 if (!entry->visible)
1979 new_pos = entry->text_length;
1981 else if (entry->text && (new_pos < entry->text_length))
1983 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1984 PangoLogAttr *log_attrs;
1987 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
1989 /* Find the next word end */
1991 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
2002 gtk_entry_move_backward_word (GtkEntry *entry,
2005 gint new_pos = start;
2007 /* Prevent any leak of information */
2008 if (!entry->visible)
2012 else if (entry->text && start > 0)
2014 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2015 PangoLogAttr *log_attrs;
2018 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2020 new_pos = start - 1;
2022 /* Find the previous word beginning */
2023 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
2033 gtk_entry_delete_whitespace (GtkEntry *entry)
2035 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2036 PangoLogAttr *log_attrs;
2040 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2042 start = end = entry->current_pos;
2044 while (start > 0 && log_attrs[start-1].is_white)
2047 while (end < n_attrs && log_attrs[end].is_white)
2053 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
2058 * Like gtk_editable_get_chars, but if the editable is not
2059 * visible, return asterisks; also convert result to UTF-8.
2062 gtk_entry_get_public_chars (GtkEntry *entry,
2067 end = entry->text_length;
2070 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
2075 gint n_chars = end - start;
2077 str = g_malloc (n_chars + 1);
2078 for (i = 0; i < n_chars; i++)
2088 primary_get_cb (GtkClipboard *clipboard,
2089 GtkSelectionData *selection_data,
2093 GtkEntry *entry = GTK_ENTRY (data);
2096 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
2098 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2099 gtk_selection_data_set_text (selection_data, str, -1);
2105 primary_clear_cb (GtkClipboard *clipboard,
2108 GtkEntry *entry = GTK_ENTRY (data);
2110 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2114 gtk_entry_update_primary_selection (GtkEntry *entry)
2116 static const GtkTargetEntry targets[] = {
2117 { "UTF8_STRING", 0, 0 },
2120 { "COMPOUND_TEXT", 0, 0 }
2123 GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
2126 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
2128 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
2129 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
2130 primary_clear_cb (clipboard, entry);
2134 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2135 gtk_clipboard_clear (clipboard);
2143 gtk_item_entry_new (void)
2147 widget = GTK_WIDGET (gtk_type_new (GTK_TYPE_ITEM_ENTRY));
2152 gtk_item_entry_new_with_max_length (gint max)
2154 GtkItemEntry *entry;
2156 entry = gtk_type_new (GTK_TYPE_ITEM_ENTRY);
2157 gtk_entry_set_max_length(GTK_ENTRY(entry), max);
2159 return GTK_WIDGET (entry);
2163 gtk_item_entry_set_text (GtkItemEntry *entry,
2165 GtkJustification justification)
2169 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2170 g_return_if_fail (text != NULL);
2172 entry->justification = justification;
2174 /* Actually setting the text will affect the cursor and selection;
2175 * if the contents don't actually change, this will look odd to the user.
2177 if (strcmp (GTK_ENTRY(entry)->text, text) == 0)
2180 if (GTK_ENTRY(entry)->recompute_idle){
2181 g_source_remove (GTK_ENTRY(entry)->recompute_idle);
2182 GTK_ENTRY(entry)->recompute_idle = 0;
2184 if (GTK_ENTRY(entry)->blink_timeout){
2185 g_source_remove (GTK_ENTRY(entry)->blink_timeout);
2186 GTK_ENTRY(entry)->blink_timeout = 0;
2189 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
2192 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
2196 * gtk_entry_get_layout_offsets:
2197 * @entry: a #GtkEntry
2198 * @x: location to store X offset of layout, or %NULL
2199 * @y: location to store Y offset of layout, or %NULL
2202 * Obtains the position of the #PangoLayout used to render text
2203 * in the entry, in widget coordinates. Useful if you want to line
2204 * up the text in an entry with some other text, e.g. when using the
2205 * entry to implement editable cells in a sheet widget.
2207 * Also useful to convert mouse events into coordinates inside the
2208 * #PangoLayout, e.g. to take some action if some part of the entry text
2211 * Note that as the user scrolls around in the entry the offsets will
2212 * change; you'll need to connect to the "notify::scroll_offset"
2213 * signal to track this. Remember when using the #PangoLayout
2214 * functions you need to convert to and from pixels using
2215 * PANGO_PIXELS() or #PANGO_SCALE.
2217 * Keep in mind that the layout text may contain a preedit string, so
2218 * gtk_entry_layout_index_to_text_index() and
2219 * gtk_entry_text_index_to_layout_index() are needed to convert byte
2220 * indices in the layout to byte indices in the entry contents.
2224 gtk_item_entry_get_layout_offsets (GtkItemEntry *entry,
2228 gint text_area_x, text_area_y;
2230 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2232 /* this gets coords relative to text area */
2233 get_layout_position (GTK_ENTRY(entry), x, y);
2235 /* convert to widget coords */
2236 get_text_area_size (GTK_ENTRY(entry), &text_area_x, &text_area_y, NULL, NULL);
2246 gtk_item_entry_set_justification(GtkItemEntry *entry, GtkJustification just)
2248 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2250 entry->justification = just;
2254 /* We display the cursor when
2256 * - the selection is empty, AND
2257 * - the widget has focus
2260 #define CURSOR_ON_MULTIPLIER 0.66
2261 #define CURSOR_OFF_MULTIPLIER 0.34
2262 #define CURSOR_PEND_MULTIPLIER 1.0
2265 cursor_blinks (GtkEntry *entry)
2267 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
2270 if (GTK_WIDGET_HAS_FOCUS (entry) &&
2271 entry->selection_bound == entry->current_pos)
2273 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
2281 get_cursor_time (GtkEntry *entry)
2283 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
2286 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
2292 show_cursor (GtkEntry *entry)
2294 if (!entry->cursor_visible)
2296 entry->cursor_visible = TRUE;
2298 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
2299 gtk_widget_queue_draw (GTK_WIDGET (entry));
2304 hide_cursor (GtkEntry *entry)
2306 if (entry->cursor_visible)
2308 entry->cursor_visible = FALSE;
2310 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
2311 gtk_widget_queue_draw (GTK_WIDGET (entry));
2319 blink_cb (gpointer data)
2323 GDK_THREADS_ENTER ();
2325 entry = GTK_ENTRY (data);
2327 g_assert (GTK_WIDGET_HAS_FOCUS (entry));
2328 g_assert (entry->selection_bound == entry->current_pos);
2330 if (entry->cursor_visible)
2332 hide_cursor (entry);
2333 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER,
2339 show_cursor (entry);
2340 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
2345 GDK_THREADS_LEAVE ();
2347 /* Remove ourselves */
2352 gtk_entry_check_cursor_blink (GtkEntry *entry)
2354 if (cursor_blinks (entry))
2356 if (!entry->blink_timeout)
2358 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
2361 show_cursor (entry);
2366 if (entry->blink_timeout)
2368 gtk_timeout_remove (entry->blink_timeout);
2369 entry->blink_timeout = 0;
2372 entry->cursor_visible = TRUE;
2378 gtk_entry_pend_cursor_blink (GtkEntry *entry)
2380 if (cursor_blinks (entry))
2382 if (entry->blink_timeout != 0)
2383 gtk_timeout_remove (entry->blink_timeout);
2385 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER,
2388 show_cursor (entry);
2393 gtk_item_entry_set_cursor_visible(GtkItemEntry *entry, gboolean visible)
2395 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2397 GTK_ENTRY(entry)->cursor_visible = visible;
2401 gtk_item_entry_get_cursor_visible(GtkItemEntry *entry)
2403 g_return_val_if_fail (GTK_IS_ITEM_ENTRY (entry), FALSE);
2405 return(GTK_ENTRY(entry)->cursor_visible);