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/.
29 #include <pango/pango.h>
31 #include <gdk/gdkkeysyms.h>
33 #include "gtkitementry.h"
35 #define MIN_ENTRY_WIDTH 150
36 #define DRAW_TIMEOUT 20
37 #define INNER_BORDER 0
39 /* Initial size of buffer, in bytes */
42 /* Maximum size of text buffer, in bytes */
43 #define MAX_SIZE G_MAXUSHORT
50 /* GObject, GtkObject methods
52 static void gtk_item_entry_class_init (GtkItemEntryClass *klass);
53 static void gtk_item_entry_init (GtkItemEntry *entry);
54 static void gtk_item_entry_editable_init (GtkEditableClass *iface);
58 static void gtk_entry_realize (GtkWidget *widget);
59 static void gtk_entry_size_request (GtkWidget *widget,
60 GtkRequisition *requisition);
61 static void gtk_entry_size_allocate (GtkWidget *widget,
62 GtkAllocation *allocation);
63 static void gtk_entry_draw_frame (GtkWidget *widget);
64 static gint gtk_entry_expose (GtkWidget *widget,
65 GdkEventExpose *event);
66 static void gtk_entry_grab_focus (GtkWidget *widget);
67 static void gtk_entry_style_set (GtkWidget *widget,
68 GtkStyle *previous_style);
69 static void gtk_entry_direction_changed (GtkWidget *widget,
70 GtkTextDirection previous_dir);
71 static void gtk_entry_state_changed (GtkWidget *widget,
72 GtkStateType previous_state);
74 /* GtkEditable method implementations
76 static void gtk_entry_insert_text (GtkEditable *editable,
77 const gchar *new_text,
80 static void gtk_entry_delete_text (GtkEditable *editable,
84 static void gtk_entry_real_set_position (GtkEditable *editable,
86 static gint gtk_entry_get_position (GtkEditable *editable);
88 /* Default signal handlers
90 static void gtk_entry_real_insert_text (GtkEditable *editable,
91 const gchar *new_text,
94 static void gtk_entry_real_delete_text (GtkEditable *editable,
97 static void gtk_entry_move_cursor (GtkEntry *entry,
100 gboolean extend_selection);
101 static void gtk_entry_insert_at_cursor (GtkEntry *entry,
103 static void gtk_entry_delete_from_cursor (GtkEntry *entry,
107 /* IM Context Callbacks
109 static void gtk_entry_commit_cb (GtkIMContext *context,
112 static void gtk_entry_preedit_changed_cb (GtkIMContext *context,
114 static gboolean gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
116 static gboolean gtk_entry_delete_surrounding_cb (GtkIMContext *context,
123 static void gtk_entry_enter_text (GtkEntry *entry,
125 static void gtk_entry_set_positions (GtkEntry *entry,
127 gint selection_bound);
128 static void gtk_entry_draw_text (GtkEntry *entry);
129 static void gtk_entry_draw_cursor (GtkEntry *entry,
131 static PangoLayout *gtk_entry_ensure_layout (GtkEntry *entry,
132 gboolean include_preedit);
133 static void gtk_entry_queue_draw (GtkEntry *entry);
134 static void gtk_entry_reset_im_context (GtkEntry *entry);
135 static void gtk_entry_recompute (GtkEntry *entry);
136 static void gtk_entry_get_cursor_locations (GtkEntry *entry,
140 static void gtk_entry_adjust_scroll (GtkEntry *entry);
141 static gint gtk_entry_move_visually (GtkEntry *editable,
144 static gint gtk_entry_move_logically (GtkEntry *entry,
147 static gint gtk_entry_move_forward_word (GtkEntry *entry,
149 static gint gtk_entry_move_backward_word (GtkEntry *entry,
151 static void gtk_entry_delete_whitespace (GtkEntry *entry);
152 static char * gtk_entry_get_public_chars (GtkEntry *entry,
155 static void gtk_entry_update_primary_selection (GtkEntry *entry);
156 static void gtk_entry_state_changed (GtkWidget *widget,
157 GtkStateType previous_state);
158 static void gtk_entry_check_cursor_blink (GtkEntry *entry);
159 static void gtk_entry_pend_cursor_blink (GtkEntry *entry);
160 static void get_text_area_size (GtkEntry *entry,
165 static void get_widget_window_size (GtkEntry *entry,
171 static GtkEntryClass *parent_class = NULL;
174 gtk_item_entry_get_type (void)
176 static GtkType item_entry_type = 0;
178 if (!item_entry_type)
180 static const GtkTypeInfo item_entry_info =
183 sizeof (GtkItemEntry),
184 sizeof (GtkItemEntryClass),
185 (GtkClassInitFunc) gtk_item_entry_class_init,
186 (GtkObjectInitFunc) gtk_item_entry_init,
187 /* reserved_1 */ NULL,
188 /* reserved_2 */ NULL,
189 (GtkClassInitFunc) NULL,
192 static const GInterfaceInfo item_editable_info =
194 (GInterfaceInitFunc) gtk_item_entry_editable_init, /* interface_init */
195 NULL, /* interface_finalize */
196 NULL /* interface_data */
200 item_entry_type = gtk_type_unique (GTK_TYPE_ENTRY, &item_entry_info);
202 g_type_add_interface_static (item_entry_type,
204 &item_editable_info);
208 return item_entry_type;
212 gtk_item_entry_class_init (GtkItemEntryClass *class)
214 GtkObjectClass *object_class;
215 GtkWidgetClass *widget_class;
216 GtkEntryClass *entry_class;
218 object_class = (GtkObjectClass*) class;
219 widget_class = (GtkWidgetClass*) class;
220 parent_class = gtk_type_class (GTK_TYPE_ENTRY);
221 entry_class = (GtkEntryClass *) class;
223 widget_class->realize = gtk_entry_realize;
224 widget_class->size_request = gtk_entry_size_request;
225 widget_class->size_allocate = gtk_entry_size_allocate;
226 widget_class->expose_event = gtk_entry_expose;
227 widget_class->grab_focus = gtk_entry_grab_focus;
228 widget_class->style_set = gtk_entry_style_set;
229 widget_class->direction_changed = gtk_entry_direction_changed;
230 widget_class->state_changed = gtk_entry_state_changed;
232 entry_class->move_cursor = gtk_entry_move_cursor;
233 entry_class->insert_at_cursor = gtk_entry_insert_at_cursor;
234 entry_class->delete_from_cursor = gtk_entry_delete_from_cursor;
239 gtk_item_entry_editable_init (GtkEditableClass *iface)
241 iface->do_insert_text = gtk_entry_insert_text;
242 iface->do_delete_text = gtk_entry_delete_text;
243 iface->insert_text = gtk_entry_real_insert_text;
244 iface->delete_text = gtk_entry_real_delete_text;
245 iface->set_position = gtk_entry_real_set_position;
246 iface->get_position = gtk_entry_get_position;
250 gtk_item_entry_init (GtkItemEntry *entry)
252 entry->justification = GTK_JUSTIFY_LEFT;
253 entry->text_max_size = 0;
254 GTK_ENTRY(entry)->has_frame = FALSE;
256 g_object_unref(G_OBJECT(GTK_ENTRY(entry)->im_context));
258 GTK_ENTRY(entry)->im_context = gtk_im_multicontext_new ();
260 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "commit",
261 G_CALLBACK (gtk_entry_commit_cb), entry);
262 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "preedit_changed",
263 G_CALLBACK (gtk_entry_preedit_changed_cb), entry);
264 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "retrieve_surrounding",
265 G_CALLBACK (gtk_entry_retrieve_surrounding_cb), entry);
266 g_signal_connect (G_OBJECT (GTK_ENTRY(entry)->im_context), "delete_surrounding",
267 G_CALLBACK (gtk_entry_delete_surrounding_cb), entry);
272 gtk_entry_realize (GtkWidget *widget)
275 GtkEditable *editable;
276 GdkWindowAttr attributes;
277 gint attributes_mask;
279 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
280 entry = GTK_ENTRY (widget);
281 editable = GTK_EDITABLE (widget);
283 attributes.window_type = GDK_WINDOW_CHILD;
285 get_widget_window_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
287 attributes.wclass = GDK_INPUT_OUTPUT;
288 attributes.visual = gtk_widget_get_visual (widget);
289 attributes.colormap = gtk_widget_get_colormap (widget);
290 attributes.event_mask = gtk_widget_get_events (widget);
291 attributes.event_mask |= (GDK_EXPOSURE_MASK |
292 GDK_BUTTON_PRESS_MASK |
293 GDK_BUTTON_RELEASE_MASK |
294 GDK_BUTTON1_MOTION_MASK |
295 GDK_BUTTON3_MOTION_MASK |
296 GDK_POINTER_MOTION_HINT_MASK |
297 GDK_POINTER_MOTION_MASK |
298 GDK_ENTER_NOTIFY_MASK |
299 GDK_LEAVE_NOTIFY_MASK);
300 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
302 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
303 gdk_window_set_user_data (widget->window, entry);
305 get_text_area_size (entry, &attributes.x, &attributes.y, &attributes.width, &attributes.height);
307 attributes.cursor = gdk_cursor_new (GDK_XTERM);
308 attributes_mask |= GDK_WA_CURSOR;
310 entry->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
311 gdk_window_set_user_data (entry->text_area, entry);
313 gdk_cursor_unref (attributes.cursor);
315 widget->style = gtk_style_attach (widget->style, widget->window);
317 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE(widget)]);
318 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
320 gdk_window_show (entry->text_area);
322 gtk_im_context_set_client_window (entry->im_context, entry->text_area);
324 gtk_entry_adjust_scroll (entry);
328 get_borders (GtkEntry *entry,
332 GtkWidget *widget = GTK_WIDGET (entry);
334 gboolean interior_focus;
336 gtk_widget_style_get (widget,
337 "interior-focus", &interior_focus,
338 "focus-line-width", &focus_width,
341 if (entry->has_frame)
343 *xborder = widget->style->xthickness;
344 *yborder = widget->style->ythickness;
354 *xborder += focus_width;
355 *yborder += focus_width;
361 gtk_entry_size_request (GtkWidget *widget,
362 GtkRequisition *requisition)
364 GtkEntry *entry = GTK_ENTRY (widget);
365 PangoFontMetrics *metrics;
366 gint xborder, yborder;
367 PangoContext *context;
369 context = gtk_widget_get_pango_context (widget);
370 metrics = pango_context_get_metrics (context,
371 widget->style->font_desc,
372 pango_context_get_language (context));
374 entry->ascent = pango_font_metrics_get_ascent (metrics);
375 entry->descent = pango_font_metrics_get_descent (metrics);
377 get_borders (entry, &xborder, &yborder);
379 xborder += INNER_BORDER;
380 yborder += INNER_BORDER;
382 if (entry->width_chars < 0)
383 requisition->width = MIN_ENTRY_WIDTH + xborder * 2;
386 gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
387 requisition->width = PANGO_PIXELS (char_width) * entry->width_chars + xborder * 2;
390 requisition->height = PANGO_PIXELS (entry->ascent + entry->descent) + yborder * 2;
392 pango_font_metrics_unref (metrics);
396 get_text_area_size (GtkEntry *entry,
402 gint xborder, yborder;
403 GtkRequisition requisition;
404 GtkWidget *widget = GTK_WIDGET (entry);
406 gtk_widget_get_child_requisition (widget, &requisition);
408 get_borders (entry, &xborder, &yborder);
417 *width = GTK_WIDGET (entry)->allocation.width - xborder * 2;
420 *height = requisition.height - yborder * 2;
424 get_widget_window_size (GtkEntry *entry,
430 GtkRequisition requisition;
431 GtkWidget *widget = GTK_WIDGET (entry);
433 gtk_widget_get_child_requisition (widget, &requisition);
436 *x = widget->allocation.x;
440 if (entry->is_cell_renderer)
441 *y = widget->allocation.y;
443 *y = widget->allocation.y + (widget->allocation.height - requisition.height) / 2;
447 *width = widget->allocation.width;
451 if (entry->is_cell_renderer)
452 *height = widget->allocation.height;
454 *height = requisition.height;
459 gtk_entry_size_allocate (GtkWidget *widget,
460 GtkAllocation *allocation)
462 GtkEntry *entry = GTK_ENTRY (widget);
463 GtkItemEntry *ientry = GTK_ITEM_ENTRY (widget);
465 if(ientry->text_max_size > 0)
466 allocation->width = MIN(ientry->text_max_size, allocation->width);
468 widget->allocation = *allocation;
470 if (GTK_WIDGET_REALIZED (widget))
472 /* We call gtk_widget_get_child_requisition, since we want (for
473 * backwards compatibility reasons) the realization here to
474 * be affected by the usize of the entry, if set
476 gint x, y, width, height;
478 get_widget_window_size (entry, &x, &y, &width, &height);
480 gdk_window_move_resize (widget->window,
481 allocation->x, allocation->y, allocation->width, allocation->height);
483 get_text_area_size (entry, &x, &y, &width, &height);
485 gdk_window_move_resize (entry->text_area,
486 0, allocation->height - height, allocation->width, height);
488 gtk_entry_recompute (entry);
493 gtk_entry_draw_frame (GtkWidget *widget)
498 gtk_entry_expose (GtkWidget *widget,
499 GdkEventExpose *event)
501 GtkEntry *entry = GTK_ENTRY (widget);
503 if (widget->window == event->window)
504 gtk_entry_draw_frame (widget);
505 else if (entry->text_area == event->window)
507 gint area_width, area_height;
509 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
511 gdk_draw_rectangle (entry->text_area,
512 widget->style->bg_gc[GTK_WIDGET_STATE(widget)],
514 0, 0, area_width, area_height);
516 if ((entry->visible || entry->invisible_char != 0) &&
517 GTK_WIDGET_HAS_FOCUS (widget) &&
518 entry->selection_bound == entry->current_pos && entry->cursor_visible)
519 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_STANDARD);
521 if (entry->dnd_position != -1)
522 gtk_entry_draw_cursor (GTK_ENTRY (widget), CURSOR_DND);
524 gtk_entry_draw_text (GTK_ENTRY (widget));
531 gtk_entry_grab_focus (GtkWidget *widget)
533 GtkEntry *entry = GTK_ENTRY (widget);
534 gboolean select_on_focus;
536 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
538 g_object_get (G_OBJECT (gtk_settings_get_default ()),
539 "gtk-entry-select-on-focus",
543 if (select_on_focus && entry->editable && !entry->in_click)
544 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
548 gtk_entry_direction_changed (GtkWidget *widget,
549 GtkTextDirection previous_dir)
551 GtkEntry *entry = GTK_ENTRY (widget);
553 gtk_entry_recompute (entry);
555 GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
559 gtk_entry_state_changed (GtkWidget *widget,
560 GtkStateType previous_state)
562 GtkEntry *entry = GTK_ENTRY (widget);
564 if (GTK_WIDGET_REALIZED (widget))
566 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
567 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
570 if (!GTK_WIDGET_IS_SENSITIVE (widget))
572 /* Clear any selection */
573 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
576 gtk_widget_queue_clear (widget);
579 /* GtkEditable method implementations
582 gtk_entry_insert_text (GtkEditable *editable,
583 const gchar *new_text,
584 gint new_text_length,
587 GtkEntry *entry = GTK_ENTRY (editable);
591 if (*position < 0 || *position > entry->text_length)
592 *position = entry->text_length;
594 g_object_ref (G_OBJECT (editable));
596 if (new_text_length <= 63)
599 text = g_new (gchar, new_text_length + 1);
601 text[new_text_length] = '\0';
602 strncpy (text, new_text, new_text_length);
604 g_signal_emit_by_name (editable, "insert_text", text, new_text_length, position);
606 if (new_text_length > 63)
609 g_object_unref (G_OBJECT (editable));
613 gtk_entry_delete_text (GtkEditable *editable,
617 GtkEntry *entry = GTK_ENTRY (editable);
619 if (end_pos < 0 || end_pos > entry->text_length)
620 end_pos = entry->text_length;
623 if (start_pos > end_pos)
626 g_object_ref (G_OBJECT (editable));
628 g_signal_emit_by_name (editable, "delete_text", start_pos, end_pos);
630 g_object_unref (G_OBJECT (editable));
634 gtk_entry_style_set (GtkWidget *widget,
635 GtkStyle *previous_style)
637 GtkEntry *entry = GTK_ENTRY (widget);
639 if (previous_style && GTK_WIDGET_REALIZED (widget))
641 gtk_entry_recompute (entry);
643 gdk_window_set_background (widget->window, &widget->style->bg[GTK_WIDGET_STATE(widget)]);
644 gdk_window_set_background (entry->text_area, &widget->style->bg[GTK_WIDGET_STATE (widget)]);
649 gtk_entry_real_set_position (GtkEditable *editable,
652 GtkEntry *entry = GTK_ENTRY (editable);
654 if (position < 0 || position > entry->text_length)
655 position = entry->text_length;
657 if (position != entry->current_pos ||
658 position != entry->selection_bound)
660 gtk_entry_reset_im_context (entry);
661 gtk_entry_set_positions (entry, position, position);
666 gtk_entry_get_position (GtkEditable *editable)
668 return GTK_ENTRY (editable)->current_pos;
672 /* Default signal handlers
675 gtk_entry_real_insert_text (GtkEditable *editable,
676 const gchar *new_text,
677 gint new_text_length,
683 GtkEntry *entry = GTK_ENTRY (editable);
685 if (new_text_length < 0)
686 new_text_length = strlen (new_text);
688 n_chars = g_utf8_strlen (new_text, new_text_length);
689 if (entry->text_max_length > 0 && n_chars + entry->text_length > entry->text_max_length)
692 n_chars = entry->text_max_length - entry->text_length;
693 new_text_length = g_utf8_offset_to_pointer (new_text, n_chars) - new_text;
696 if (new_text_length + entry->n_bytes + 1 > entry->text_size)
698 while (new_text_length + entry->n_bytes + 1 > entry->text_size)
700 if (entry->text_size == 0)
701 entry->text_size = MIN_SIZE;
704 if (2 * (guint)entry->text_size < MAX_SIZE &&
705 2 * (guint)entry->text_size > entry->text_size)
706 entry->text_size *= 2;
709 entry->text_size = MAX_SIZE;
710 if (new_text_length > (gint)entry->text_size - (gint)entry->n_bytes - 1)
712 new_text_length = (gint)entry->text_size - (gint)entry->n_bytes - 1;
713 new_text_length = g_utf8_find_prev_char (new_text, new_text + new_text_length + 1) - new_text;
714 n_chars = g_utf8_strlen (new_text, new_text_length);
721 entry->text = g_realloc (entry->text, entry->text_size);
724 index = g_utf8_offset_to_pointer (entry->text, *position) - entry->text;
726 g_memmove (entry->text + index + new_text_length, entry->text + index, entry->n_bytes - index);
727 memcpy (entry->text + index, new_text, new_text_length);
729 entry->n_bytes += new_text_length;
730 entry->text_length += n_chars;
732 /* NUL terminate for safety and convenience */
733 entry->text[entry->n_bytes] = '\0';
735 if (entry->current_pos > *position)
736 entry->current_pos += n_chars;
738 if (entry->selection_bound > *position)
739 entry->selection_bound += n_chars;
741 *position += n_chars;
743 gtk_entry_recompute (entry);
745 g_signal_emit_by_name (editable, "changed");
746 g_object_notify (G_OBJECT (editable), "text");
750 gtk_entry_real_delete_text (GtkEditable *editable,
754 GtkEntry *entry = GTK_ENTRY (editable);
758 if (end_pos < 0 || end_pos > entry->text_length)
759 end_pos = entry->text_length;
761 if (start_pos < end_pos)
763 gint start_index = g_utf8_offset_to_pointer (entry->text, start_pos) - entry->text;
764 gint end_index = g_utf8_offset_to_pointer (entry->text, end_pos) - entry->text;
766 g_memmove (entry->text + start_index, entry->text + end_index, entry->n_bytes + 1 - end_index);
767 entry->text_length -= (end_pos - start_pos);
768 entry->n_bytes -= (end_index - start_index);
770 if (entry->current_pos > start_pos)
771 entry->current_pos -= MIN (entry->current_pos, end_pos) - start_pos;
773 if (entry->selection_bound > start_pos)
774 entry->selection_bound -= MIN (entry->selection_bound, end_pos) - start_pos;
775 /* We might have deleted the selection
777 gtk_entry_update_primary_selection (entry);
779 gtk_entry_recompute (entry);
781 g_signal_emit_by_name (editable, "changed");
782 g_object_notify (G_OBJECT (editable), "text");
786 /* Compute the X position for an offset that corresponds to the "more important
787 * cursor position for that offset. We use this when trying to guess to which
788 * end of the selection we should go to when the user hits the left or
792 get_better_cursor_x (GtkEntry *entry,
795 GtkTextDirection keymap_direction =
796 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
797 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
798 GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
799 gboolean split_cursor;
801 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
802 gint index = g_utf8_offset_to_pointer (entry->text, offset) - entry->text;
804 PangoRectangle strong_pos, weak_pos;
806 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
807 "gtk-split-cursor", &split_cursor,
810 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
813 return strong_pos.x / PANGO_SCALE;
815 return (keymap_direction == widget_direction) ? strong_pos.x / PANGO_SCALE : weak_pos.x / PANGO_SCALE;
819 gtk_entry_move_cursor (GtkEntry *entry,
820 GtkMovementStep step,
822 gboolean extend_selection)
824 gint new_pos = entry->current_pos;
826 gtk_entry_reset_im_context (entry);
828 if (entry->current_pos != entry->selection_bound && !extend_selection)
830 /* If we have a current selection and aren't extending it, move to the
831 * start/or end of the selection as appropriate
835 case GTK_MOVEMENT_VISUAL_POSITIONS:
837 gint current_x = get_better_cursor_x (entry, entry->current_pos);
838 gint bound_x = get_better_cursor_x (entry, entry->selection_bound);
841 new_pos = current_x < bound_x ? entry->current_pos : entry->selection_bound;
843 new_pos = current_x > bound_x ? entry->current_pos : entry->selection_bound;
847 case GTK_MOVEMENT_LOGICAL_POSITIONS:
848 case GTK_MOVEMENT_WORDS:
850 new_pos = MIN (entry->current_pos, entry->selection_bound);
852 new_pos = MAX (entry->current_pos, entry->selection_bound);
854 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
855 case GTK_MOVEMENT_PARAGRAPH_ENDS:
856 case GTK_MOVEMENT_BUFFER_ENDS:
857 new_pos = count < 0 ? 0 : entry->text_length;
859 case GTK_MOVEMENT_DISPLAY_LINES:
860 case GTK_MOVEMENT_PARAGRAPHS:
861 case GTK_MOVEMENT_PAGES:
871 case GTK_MOVEMENT_LOGICAL_POSITIONS:
872 new_pos = gtk_entry_move_logically (entry, new_pos, count);
874 case GTK_MOVEMENT_VISUAL_POSITIONS:
875 new_pos = gtk_entry_move_visually (entry, new_pos, count);
877 case GTK_MOVEMENT_WORDS:
880 new_pos = gtk_entry_move_forward_word (entry, new_pos);
885 new_pos = gtk_entry_move_backward_word (entry, new_pos);
889 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
890 case GTK_MOVEMENT_PARAGRAPH_ENDS:
891 case GTK_MOVEMENT_BUFFER_ENDS:
892 new_pos = count < 0 ? 0 : entry->text_length;
894 case GTK_MOVEMENT_DISPLAY_LINES:
895 case GTK_MOVEMENT_PARAGRAPHS:
896 case GTK_MOVEMENT_PAGES:
903 if (extend_selection)
904 gtk_editable_select_region (GTK_EDITABLE (entry), entry->selection_bound, new_pos);
906 gtk_editable_set_position (GTK_EDITABLE (entry), new_pos);
908 gtk_entry_pend_cursor_blink (entry);
912 gtk_entry_insert_at_cursor (GtkEntry *entry,
915 GtkEditable *editable = GTK_EDITABLE (entry);
916 gint pos = entry->current_pos;
920 gtk_entry_reset_im_context (entry);
922 gtk_editable_insert_text (editable, str, -1, &pos);
923 gtk_editable_set_position (editable, pos);
928 gtk_entry_delete_from_cursor (GtkEntry *entry,
932 GtkEditable *editable = GTK_EDITABLE (entry);
933 gint start_pos = entry->current_pos;
934 gint end_pos = entry->current_pos;
936 gtk_entry_reset_im_context (entry);
938 if (!entry->editable)
941 if (entry->selection_bound != entry->current_pos)
943 gtk_editable_delete_selection (editable);
949 case GTK_DELETE_CHARS:
950 end_pos = gtk_entry_move_logically (entry, entry->current_pos, count);
951 gtk_editable_delete_text (editable, MIN (start_pos, end_pos), MAX (start_pos, end_pos));
953 case GTK_DELETE_WORDS:
956 /* Move to end of current word, or if not on a word, end of previous word */
957 end_pos = gtk_entry_move_backward_word (entry, end_pos);
958 end_pos = gtk_entry_move_forward_word (entry, end_pos);
962 /* Move to beginning of current word, or if not on a word, begining of next word */
963 start_pos = gtk_entry_move_forward_word (entry, start_pos);
964 start_pos = gtk_entry_move_backward_word (entry, start_pos);
968 case GTK_DELETE_WORD_ENDS:
971 start_pos = gtk_entry_move_backward_word (entry, start_pos);
976 end_pos = gtk_entry_move_forward_word (entry, end_pos);
979 gtk_editable_delete_text (editable, start_pos, end_pos);
981 case GTK_DELETE_DISPLAY_LINE_ENDS:
982 case GTK_DELETE_PARAGRAPH_ENDS:
984 gtk_editable_delete_text (editable, 0, entry->current_pos);
986 gtk_editable_delete_text (editable, entry->current_pos, -1);
988 case GTK_DELETE_DISPLAY_LINES:
989 case GTK_DELETE_PARAGRAPHS:
990 gtk_editable_delete_text (editable, 0, -1);
992 case GTK_DELETE_WHITESPACE:
993 gtk_entry_delete_whitespace (entry);
997 gtk_entry_pend_cursor_blink (entry);
1000 /* IM Context Callbacks
1004 gtk_entry_commit_cb (GtkIMContext *context,
1008 gtk_entry_enter_text (entry, str);
1012 gtk_entry_preedit_changed_cb (GtkIMContext *context,
1015 gchar *preedit_string;
1018 gtk_im_context_get_preedit_string (entry->im_context,
1019 &preedit_string, NULL,
1021 entry->preedit_length = strlen (preedit_string);
1022 cursor_pos = CLAMP (cursor_pos, 0, g_utf8_strlen (preedit_string, -1));
1023 entry->preedit_cursor = cursor_pos;
1024 g_free (preedit_string);
1026 gtk_entry_recompute (entry);
1030 gtk_entry_retrieve_surrounding_cb (GtkIMContext *context,
1033 gtk_im_context_set_surrounding (context,
1036 g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text);
1042 gtk_entry_delete_surrounding_cb (GtkIMContext *slave,
1047 gtk_editable_delete_text (GTK_EDITABLE (entry),
1048 entry->current_pos + offset,
1049 entry->current_pos + offset + n_chars);
1055 /* Internal functions
1058 /* Used for im_commit_cb and inserting Unicode chars */
1060 gtk_entry_enter_text (GtkEntry *entry,
1063 GtkEditable *editable = GTK_EDITABLE (entry);
1066 if (gtk_editable_get_selection_bounds (editable, NULL, NULL))
1067 gtk_editable_delete_selection (editable);
1070 if (entry->overwrite_mode)
1071 gtk_entry_delete_from_cursor (entry, GTK_DELETE_CHARS, 1);
1074 tmp_pos = entry->current_pos;
1075 gtk_editable_insert_text (editable, str, strlen (str), &tmp_pos);
1076 gtk_editable_set_position (editable, tmp_pos);
1079 /* All changes to entry->current_pos and entry->selection_bound
1080 * should go through this function.
1083 gtk_entry_set_positions (GtkEntry *entry,
1085 gint selection_bound)
1087 gboolean changed = FALSE;
1089 g_object_freeze_notify (G_OBJECT (entry));
1091 if (current_pos != -1 &&
1092 entry->current_pos != current_pos)
1094 entry->current_pos = current_pos;
1097 g_object_notify (G_OBJECT (entry), "cursor_position");
1100 if (selection_bound != -1 &&
1101 entry->selection_bound != selection_bound)
1103 entry->selection_bound = selection_bound;
1106 g_object_notify (G_OBJECT (entry), "selection_bound");
1109 g_object_thaw_notify (G_OBJECT (entry));
1112 gtk_entry_recompute (entry);
1116 gtk_entry_reset_layout (GtkEntry *entry)
1118 if (entry->cached_layout)
1120 g_object_unref (G_OBJECT (entry->cached_layout));
1121 entry->cached_layout = NULL;
1126 update_im_cursor_location (GtkEntry *entry)
1130 gint strong_xoffset;
1131 gint x, y, area_width, area_height;
1133 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL)
1135 get_text_area_size (entry, &x, &y, &area_width, &area_height);
1137 strong_xoffset = strong_x - entry->scroll_offset;
1138 if (strong_xoffset < 0)
1142 else if (strong_xoffset > area_width)
1144 strong_xoffset = area_width;
1146 area.x = x + strong_xoffset;
1147 area.y = y + area_height;
1148 area.width = area_width;
1149 area.height = area_height;
1151 gtk_im_context_set_cursor_location (entry->im_context, &area);
1155 recompute_idle_func (gpointer data)
1159 GDK_THREADS_ENTER ();
1161 entry = GTK_ENTRY (data);
1163 gtk_entry_adjust_scroll (entry);
1164 gtk_entry_queue_draw (entry);
1166 entry->recompute_idle = FALSE;
1168 update_im_cursor_location (entry);
1170 GDK_THREADS_LEAVE ();
1176 gtk_entry_recompute (GtkEntry *entry)
1178 gtk_entry_reset_layout (entry);
1179 gtk_entry_check_cursor_blink (entry);
1182 if (!entry->recompute_idle)
1184 entry->recompute_idle = g_idle_add_full (G_PRIORITY_HIGH_IDLE + 15, /* between resize and redraw */
1185 recompute_idle_func, entry, NULL);
1190 append_char (GString *str,
1198 char_len = g_unichar_to_utf8 (ch, buf);
1203 g_string_append_len (str, buf, char_len);
1208 static PangoLayout *
1209 gtk_entry_create_layout (GtkEntry *entry,
1210 gboolean include_preedit)
1212 PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (entry), NULL);
1213 PangoAttrList *tmp_attrs = pango_attr_list_new ();
1215 gchar *preedit_string = NULL;
1216 gint preedit_length = 0;
1217 PangoAttrList *preedit_attrs = NULL;
1219 pango_layout_set_single_paragraph_mode (layout, TRUE);
1221 if (include_preedit)
1223 gtk_im_context_get_preedit_string (entry->im_context,
1224 &preedit_string, &preedit_attrs, NULL);
1225 preedit_length = entry->preedit_length;
1230 GString *tmp_string = g_string_new (NULL);
1232 gint cursor_index = g_utf8_offset_to_pointer (entry->text, entry->current_pos) - entry->text;
1236 g_string_prepend_len (tmp_string, entry->text, entry->n_bytes);
1237 g_string_insert (tmp_string, cursor_index, preedit_string);
1242 gint preedit_len_chars;
1243 gunichar invisible_char;
1245 ch_len = g_utf8_strlen (entry->text, entry->n_bytes);
1246 preedit_len_chars = g_utf8_strlen (preedit_string, -1);
1247 ch_len += preedit_len_chars;
1249 if (entry->invisible_char != 0)
1250 invisible_char = entry->invisible_char;
1252 invisible_char = ' '; /* just pick a char */
1254 append_char (tmp_string, invisible_char, ch_len);
1256 /* Fix cursor index to point to invisible char corresponding
1257 * to the preedit, fix preedit_length to be the length of
1258 * the invisible chars representing the preedit
1261 g_utf8_offset_to_pointer (tmp_string->str, entry->current_pos) -
1265 g_unichar_to_utf8 (invisible_char, NULL);
1268 pango_layout_set_text (layout, tmp_string->str, tmp_string->len);
1270 pango_attr_list_splice (tmp_attrs, preedit_attrs,
1271 cursor_index, preedit_length);
1273 g_string_free (tmp_string, TRUE);
1279 pango_layout_set_text (layout, entry->text, entry->n_bytes);
1283 GString *str = g_string_new (NULL);
1284 gunichar invisible_char;
1286 if (entry->invisible_char != 0)
1287 invisible_char = entry->invisible_char;
1289 invisible_char = ' '; /* just pick a char */
1291 append_char (str, invisible_char, entry->text_length);
1292 pango_layout_set_text (layout, str->str, str->len);
1293 g_string_free (str, TRUE);
1297 pango_layout_set_attributes (layout, tmp_attrs);
1300 g_free (preedit_string);
1302 pango_attr_list_unref (preedit_attrs);
1304 pango_attr_list_unref (tmp_attrs);
1309 static PangoLayout *
1310 gtk_entry_ensure_layout (GtkEntry *entry,
1311 gboolean include_preedit)
1313 if (entry->preedit_length > 0 &&
1314 !include_preedit != !entry->cache_includes_preedit)
1315 gtk_entry_reset_layout (entry);
1317 if (!entry->cached_layout)
1319 entry->cached_layout = gtk_entry_create_layout (entry, include_preedit);
1320 entry->cache_includes_preedit = include_preedit;
1323 return entry->cached_layout;
1327 get_layout_position (GtkEntry *entry,
1331 PangoLayout *layout;
1332 PangoRectangle logical_rect;
1333 gint area_width, area_height;
1335 PangoLayoutLine *line;
1337 layout = gtk_entry_ensure_layout (entry, TRUE);
1339 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1341 area_height = PANGO_SCALE * (area_height);
1343 line = pango_layout_get_lines (layout)->data;
1344 pango_layout_line_get_extents (line, NULL, &logical_rect);
1346 /* Align primarily for locale's ascent/descent */
1348 y_pos = ((area_height - entry->ascent - entry->descent) / 2 +
1349 entry->ascent + logical_rect.y);
1352 /* Now see if we need to adjust to fit in actual drawn string */
1354 if (logical_rect.height > area_height)
1355 y_pos = (area_height - logical_rect.height) / 2;
1358 else if (y_pos + logical_rect.height > area_height)
1359 y_pos = area_height - logical_rect.height;
1361 y_pos = y_pos / PANGO_SCALE;
1364 *x = - entry->scroll_offset;
1371 gtk_entry_draw_text (GtkEntry *entry)
1374 PangoLayoutLine *line;
1376 if (!entry->visible && entry->invisible_char == 0)
1379 if (GTK_WIDGET_DRAWABLE (entry))
1381 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1382 gint area_width, area_height;
1385 gint start_pos, end_pos;
1387 widget = GTK_WIDGET (entry);
1389 get_layout_position (entry, &x, &y);
1391 get_text_area_size (entry, NULL, NULL, &area_width, &area_height);
1394 gdk_draw_layout (entry->text_area, widget->style->text_gc [widget->state],
1399 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start_pos, &end_pos))
1403 PangoRectangle logical_rect;
1404 const gchar *text = pango_layout_get_text (layout);
1405 gint start_index = g_utf8_offset_to_pointer (text, start_pos) - text;
1406 gint end_index = g_utf8_offset_to_pointer (text, end_pos) - text;
1407 GdkRegion *clip_region = gdk_region_new ();
1409 GdkGC *selection_gc;
1411 line = pango_layout_get_lines (layout)->data;
1413 pango_layout_line_get_x_ranges (line, start_index, end_index, &ranges, &n_ranges);
1415 pango_layout_get_extents (layout, NULL, &logical_rect);
1417 if (GTK_WIDGET_HAS_FOCUS (entry))
1419 selection_gc = widget->style->base_gc [GTK_STATE_SELECTED];
1420 text_gc = widget->style->text_gc [GTK_STATE_SELECTED];
1424 selection_gc = widget->style->base_gc [GTK_STATE_ACTIVE];
1425 text_gc = widget->style->text_gc [GTK_STATE_ACTIVE];
1428 for (i=0; i < n_ranges; i++)
1432 rect.x = INNER_BORDER - entry->scroll_offset + ranges[2*i] / PANGO_SCALE;
1434 rect.width = (ranges[2*i + 1] - ranges[2*i]) / PANGO_SCALE;
1435 rect.height = logical_rect.height / PANGO_SCALE;
1437 gdk_draw_rectangle (entry->text_area, selection_gc, TRUE,
1438 rect.x, rect.y, rect.width, rect.height);
1440 gdk_region_union_with_rect (clip_region, &rect);
1443 gdk_gc_set_clip_region (text_gc, clip_region);
1444 gdk_draw_layout (entry->text_area, text_gc,
1447 gdk_gc_set_clip_region (text_gc, NULL);
1449 gdk_region_destroy (clip_region);
1456 * From _gtk_get_insertion_cursor_gc
1459 typedef struct _CursorInfo CursorInfo;
1465 GdkGC *secondary_gc;
1469 make_cursor_gc (GtkWidget *widget,
1470 const gchar *property_name,
1473 GdkGCValues gc_values;
1474 GdkGCValuesMask gc_values_mask;
1475 GdkColor *cursor_color;
1477 gtk_widget_style_get (widget, property_name, &cursor_color, NULL);
1479 gc_values_mask = GDK_GC_FOREGROUND;
1482 gc_values.foreground = *cursor_color;
1483 gdk_color_free (cursor_color);
1486 gc_values.foreground = *fallback;
1488 gdk_rgb_find_color (widget->style->colormap, &gc_values.foreground);
1489 return gtk_gc_get (widget->style->depth, widget->style->colormap,
1490 &gc_values, gc_values_mask);
1494 _gtkextra_get_insertion_cursor_gc (GtkWidget *widget,
1495 gboolean is_primary)
1497 CursorInfo *cursor_info;
1499 cursor_info = g_object_get_data (G_OBJECT (widget->style), "gtk-style-cursor-info");
1502 cursor_info = g_new (CursorInfo, 1);
1503 g_object_set_data (G_OBJECT (widget->style), "gtk-style-cursor-info", cursor_info);
1504 cursor_info->primary_gc = NULL;
1505 cursor_info->secondary_gc = NULL;
1506 cursor_info->for_type = G_TYPE_INVALID;
1509 /* We have to keep track of the type because gtk_widget_style_get()
1510 * can return different results when called on the same property and
1511 * same style but for different widgets. :-(. That is,
1512 * GtkEntry::cursor-color = "red" in a style will modify the cursor
1513 * color for entries but not for text view.
1515 if (cursor_info->for_type != G_OBJECT_TYPE (widget))
1517 cursor_info->for_type = G_OBJECT_TYPE (widget);
1518 if (cursor_info->primary_gc)
1520 gtk_gc_release (cursor_info->primary_gc);
1521 cursor_info->primary_gc = NULL;
1523 if (cursor_info->secondary_gc)
1525 gtk_gc_release (cursor_info->secondary_gc);
1526 cursor_info->secondary_gc = NULL;
1532 if (!cursor_info->primary_gc)
1533 cursor_info->primary_gc = make_cursor_gc (widget,
1535 &widget->style->black);
1537 return g_object_ref (cursor_info->primary_gc);
1541 static GdkColor gray = { 0, 0x8888, 0x8888, 0x8888 };
1543 if (!cursor_info->secondary_gc)
1544 cursor_info->secondary_gc = make_cursor_gc (widget,
1545 "secondary-cursor-color",
1548 return g_object_ref (cursor_info->secondary_gc);
1553 * From _gtk_draw_insertion_cursor
1556 _gtkextra_draw_insertion_cursor (GtkWidget *widget,
1557 GdkDrawable *drawable,
1559 GdkRectangle *location,
1560 GtkTextDirection direction,
1561 gboolean draw_arrow)
1567 gfloat cursor_aspect_ratio;
1570 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
1572 gtk_widget_style_get (widget, "cursor-aspect-ratio", &cursor_aspect_ratio, NULL);
1574 stem_width = location->height * cursor_aspect_ratio + 1;
1575 arrow_width = stem_width + 1;
1577 /* put (stem_width % 2) on the proper side of the cursor */
1578 if (direction == GTK_TEXT_DIR_LTR)
1579 offset = stem_width / 2;
1581 offset = stem_width - stem_width / 2;
1583 for (i = 0; i < stem_width; i++)
1584 gdk_draw_line (drawable, gc,
1585 location->x + i - offset, location->y,
1586 location->x + i - offset, location->y + location->height - 1);
1590 if (direction == GTK_TEXT_DIR_RTL)
1592 x = location->x - offset - 1;
1593 y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
1595 for (i = 0; i < arrow_width; i++)
1597 gdk_draw_line (drawable, gc,
1599 x, y + 2 * arrow_width - i - 1);
1603 else if (direction == GTK_TEXT_DIR_LTR)
1605 x = location->x + stem_width - offset;
1606 y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
1608 for (i = 0; i < arrow_width; i++)
1610 gdk_draw_line (drawable, gc,
1612 x, y + 2 * arrow_width - i - 1);
1620 gtk_entry_draw_cursor (GtkEntry *entry,
1623 GtkTextDirection keymap_direction =
1624 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1625 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1626 GtkTextDirection widget_direction = gtk_widget_get_direction (GTK_WIDGET (entry));
1628 if (GTK_WIDGET_DRAWABLE (entry) && GTK_ENTRY(entry)->cursor_visible)
1630 GtkWidget *widget = GTK_WIDGET (entry);
1631 GdkRectangle cursor_location;
1632 gboolean split_cursor;
1634 gint xoffset = INNER_BORDER - entry->scroll_offset;
1635 gint strong_x, weak_x;
1636 gint text_area_height;
1637 GtkTextDirection dir1 = GTK_TEXT_DIR_NONE;
1638 GtkTextDirection dir2 = GTK_TEXT_DIR_NONE;
1643 gdk_window_get_size (entry->text_area, NULL, &text_area_height);
1645 gtk_entry_get_cursor_locations (entry, type, &strong_x, &weak_x);
1647 g_object_get (gtk_widget_get_settings (widget),
1648 "gtk-split-cursor", &split_cursor,
1651 dir1 = widget_direction;
1657 if (weak_x != strong_x)
1659 dir2 = (widget_direction == GTK_TEXT_DIR_LTR) ? GTK_TEXT_DIR_RTL : GTK_TEXT_DIR_LTR;
1665 if (keymap_direction == widget_direction)
1671 cursor_location.x = xoffset + x1;
1672 cursor_location.y = INNER_BORDER;
1673 cursor_location.width = 0;
1674 cursor_location.height = text_area_height - 2 * INNER_BORDER ;
1676 gc = _gtkextra_get_insertion_cursor_gc (widget, TRUE);
1677 _gtkextra_draw_insertion_cursor (widget, entry->text_area, gc,
1678 &cursor_location, dir1,
1679 dir2 != GTK_TEXT_DIR_NONE);
1680 g_object_unref (gc);
1682 if (dir2 != GTK_TEXT_DIR_NONE)
1684 cursor_location.x = xoffset + x2;
1685 gc = _gtkextra_get_insertion_cursor_gc (widget, FALSE);
1686 _gtkextra_draw_insertion_cursor (widget, entry->text_area, gc,
1687 &cursor_location, dir2,
1689 g_object_unref (gc);
1695 gtk_entry_queue_draw (GtkEntry *entry)
1697 if (GTK_WIDGET_REALIZED (entry))
1698 gdk_window_invalidate_rect (entry->text_area, NULL, FALSE);
1702 gtk_entry_reset_im_context (GtkEntry *entry)
1704 if (entry->need_im_reset)
1706 entry->need_im_reset = 0;
1707 gtk_im_context_reset (entry->im_context);
1712 gtk_entry_get_cursor_locations (GtkEntry *entry,
1717 PangoLayout *layout = gtk_entry_ensure_layout (entry, TRUE);
1719 PangoRectangle strong_pos, weak_pos;
1722 if (type == CURSOR_STANDARD)
1724 text = pango_layout_get_text (layout);
1725 index = g_utf8_offset_to_pointer (text, entry->current_pos + entry->preedit_cursor) - text;
1727 else /* type == CURSOR_DND */
1729 index = g_utf8_offset_to_pointer (entry->text, entry->dnd_position) - entry->text;
1730 if (entry->dnd_position > entry->current_pos)
1731 index += entry->preedit_length;
1734 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
1737 *strong_x = strong_pos.x / PANGO_SCALE;
1740 *weak_x = weak_pos.x / PANGO_SCALE;
1744 gtk_entry_adjust_scroll (GtkEntry *entry)
1746 gint min_offset, max_offset;
1747 gint text_area_width;
1748 gint strong_x, weak_x;
1749 PangoLayout *layout;
1750 PangoLayoutLine *line;
1751 PangoRectangle logical_rect;
1752 GtkItemEntry *item_entry;
1755 if (!GTK_WIDGET_REALIZED (entry))
1758 item_entry = GTK_ITEM_ENTRY(entry);
1760 gdk_window_get_size (entry->text_area, &text_area_width, NULL);
1761 text_area_width -= 2 * INNER_BORDER;
1763 layout = gtk_entry_ensure_layout (entry, TRUE);
1764 line = pango_layout_get_lines (layout)->data;
1766 pango_layout_line_get_extents (line, NULL, &logical_rect);
1767 text_width = logical_rect.width / PANGO_SCALE + 2; /* 2 for cursor */
1769 gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, &weak_x);
1771 /* Display as much text as we can */
1773 if (gtk_widget_get_direction (GTK_WIDGET (entry)) == GTK_TEXT_DIR_LTR)
1775 entry->scroll_offset = 0;
1776 switch(item_entry->justification){
1778 case GTK_JUSTIFY_FILL:
1779 case GTK_JUSTIFY_LEFT:
1781 /* LEFT JUSTIFICATION */
1783 strong_x -= entry->scroll_offset;
1785 entry->scroll_offset += strong_x;
1786 else if (strong_x > text_area_width){
1787 if(item_entry->text_max_size != 0 &&
1788 text_area_width + 2 <= item_entry->text_max_size){
1789 GtkAllocation allocation;
1790 allocation = GTK_WIDGET(entry)->allocation;
1791 allocation.width += text_width - text_area_width;
1792 entry->scroll_offset = 0;
1793 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1795 entry->scroll_offset += (strong_x - text_area_width) + 1;
1801 case GTK_JUSTIFY_RIGHT:
1803 /* RIGHT JUSTIFICATION FOR NUMBERS */
1806 entry->scroll_offset= -(text_area_width - text_width) + 1;
1807 if(entry->scroll_offset > 0){
1808 if(item_entry->text_max_size != 0 &&
1809 text_area_width + 2 <= item_entry->text_max_size){
1810 GtkAllocation allocation;
1811 allocation = GTK_WIDGET(entry)->allocation;
1812 allocation.x -= text_width - text_area_width;
1813 allocation.width += text_width - text_area_width;
1814 entry->scroll_offset = 0;
1815 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1819 entry->scroll_offset= -(text_area_width - strong_x) + 1;
1820 if(entry->scroll_offset < 0) entry->scroll_offset = 0;
1825 entry->scroll_offset=0;
1828 case GTK_JUSTIFY_CENTER:
1832 entry->scroll_offset= -(text_area_width - text_width)/2;
1833 if(entry->scroll_offset > 0){
1834 if(item_entry->text_max_size != 0 &&
1835 text_area_width+1<=item_entry->text_max_size){
1836 GtkAllocation allocation;
1837 allocation = GTK_WIDGET(entry)->allocation;
1838 allocation.x += (text_area_width/2 - text_width/2);
1839 allocation.width += text_width - text_area_width;
1840 entry->scroll_offset = 0;
1841 gtk_entry_size_allocate(GTK_WIDGET(entry), &allocation);
1845 entry->scroll_offset= -(text_area_width - strong_x) + 1;
1846 if(entry->scroll_offset < 0) entry->scroll_offset = 0;
1851 entry->scroll_offset=0;
1860 max_offset = text_width - text_area_width;
1861 min_offset = MIN (0, max_offset);
1862 entry->scroll_offset = CLAMP (entry->scroll_offset, min_offset, max_offset);
1865 g_object_notify (G_OBJECT (entry), "scroll_offset");
1869 gtk_entry_move_visually (GtkEntry *entry,
1874 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1877 text = pango_layout_get_text (layout);
1879 index = g_utf8_offset_to_pointer (text, start) - text;
1883 int new_index, new_trailing;
1884 gboolean split_cursor;
1887 g_object_get (gtk_widget_get_settings (GTK_WIDGET (entry)),
1888 "gtk-split-cursor", &split_cursor,
1895 GtkTextDirection keymap_direction =
1896 (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
1897 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
1899 strong = keymap_direction == gtk_widget_get_direction (GTK_WIDGET (entry));
1904 pango_layout_move_cursor_visually (layout, strong, index, 0, 1, &new_index, &new_trailing);
1909 pango_layout_move_cursor_visually (layout, strong, index, 0, -1, &new_index, &new_trailing);
1913 if (new_index < 0 || new_index == G_MAXINT)
1918 while (new_trailing--)
1919 index = g_utf8_next_char (entry->text + new_index) - entry->text;
1922 return g_utf8_pointer_to_offset (text, text + index);
1926 gtk_entry_move_logically (GtkEntry *entry,
1930 gint new_pos = start;
1932 /* Prevent any leak of information */
1933 if (!entry->visible)
1935 new_pos = CLAMP (start + count, 0, entry->text_length);
1937 else if (entry->text)
1939 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1940 PangoLogAttr *log_attrs;
1943 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
1945 while (count > 0 && new_pos < entry->text_length)
1949 while (new_pos < entry->text_length && !log_attrs[new_pos].is_cursor_position);
1953 while (count < 0 && new_pos > 0)
1957 while (new_pos > 0 && !log_attrs[new_pos].is_cursor_position);
1969 gtk_entry_move_forward_word (GtkEntry *entry,
1972 gint new_pos = start;
1974 /* Prevent any leak of information */
1975 if (!entry->visible)
1977 new_pos = entry->text_length;
1979 else if (entry->text && (new_pos < entry->text_length))
1981 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
1982 PangoLogAttr *log_attrs;
1985 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
1987 /* Find the next word end */
1989 while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
2000 gtk_entry_move_backward_word (GtkEntry *entry,
2003 gint new_pos = start;
2005 /* Prevent any leak of information */
2006 if (!entry->visible)
2010 else if (entry->text && start > 0)
2012 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2013 PangoLogAttr *log_attrs;
2016 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2018 new_pos = start - 1;
2020 /* Find the previous word beginning */
2021 while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
2031 gtk_entry_delete_whitespace (GtkEntry *entry)
2033 PangoLayout *layout = gtk_entry_ensure_layout (entry, FALSE);
2034 PangoLogAttr *log_attrs;
2038 pango_layout_get_log_attrs (layout, &log_attrs, &n_attrs);
2040 start = end = entry->current_pos;
2042 while (start > 0 && log_attrs[start-1].is_white)
2045 while (end < n_attrs && log_attrs[end].is_white)
2051 gtk_editable_delete_text (GTK_EDITABLE (entry), start, end);
2056 * Like gtk_editable_get_chars, but if the editable is not
2057 * visible, return asterisks; also convert result to UTF-8.
2060 gtk_entry_get_public_chars (GtkEntry *entry,
2065 end = entry->text_length;
2068 return gtk_editable_get_chars (GTK_EDITABLE (entry), start, end);
2073 gint n_chars = end - start;
2075 str = g_malloc (n_chars + 1);
2076 for (i = 0; i < n_chars; i++)
2086 primary_get_cb (GtkClipboard *clipboard,
2087 GtkSelectionData *selection_data,
2091 GtkEntry *entry = GTK_ENTRY (data);
2094 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
2096 gchar *str = gtk_entry_get_public_chars (entry, start, end);
2097 gtk_selection_data_set_text (selection_data, str, -1);
2103 primary_clear_cb (GtkClipboard *clipboard,
2106 GtkEntry *entry = GTK_ENTRY (data);
2108 gtk_editable_select_region (GTK_EDITABLE (entry), entry->current_pos, entry->current_pos);
2112 gtk_entry_update_primary_selection (GtkEntry *entry)
2114 static const GtkTargetEntry targets[] = {
2115 { "UTF8_STRING", 0, 0 },
2118 { "COMPOUND_TEXT", 0, 0 }
2121 GtkClipboard *clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
2124 if (gtk_editable_get_selection_bounds (GTK_EDITABLE (entry), &start, &end))
2126 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
2127 primary_get_cb, primary_clear_cb, G_OBJECT (entry)))
2128 primary_clear_cb (clipboard, entry);
2132 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (entry))
2133 gtk_clipboard_clear (clipboard);
2141 gtk_item_entry_new (void)
2145 widget = GTK_WIDGET (gtk_type_new (GTK_TYPE_ITEM_ENTRY));
2150 gtk_item_entry_new_with_max_length (gint max)
2152 GtkItemEntry *entry;
2154 entry = gtk_type_new (GTK_TYPE_ITEM_ENTRY);
2155 gtk_entry_set_max_length(GTK_ENTRY(entry), max);
2157 return GTK_WIDGET (entry);
2161 gtk_item_entry_set_text (GtkItemEntry *entry,
2163 GtkJustification justification)
2167 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2168 g_return_if_fail (text != NULL);
2170 entry->justification = justification;
2172 /* Actually setting the text will affect the cursor and selection;
2173 * if the contents don't actually change, this will look odd to the user.
2175 if (strcmp (GTK_ENTRY(entry)->text, text) == 0)
2178 if (GTK_ENTRY(entry)->recompute_idle){
2179 g_source_remove (GTK_ENTRY(entry)->recompute_idle);
2180 GTK_ENTRY(entry)->recompute_idle = 0;
2182 if (GTK_ENTRY(entry)->blink_timeout){
2183 g_source_remove (GTK_ENTRY(entry)->blink_timeout);
2184 GTK_ENTRY(entry)->blink_timeout = 0;
2187 gtk_editable_delete_text (GTK_EDITABLE (entry), 0, -1);
2190 gtk_editable_insert_text (GTK_EDITABLE (entry), text, strlen (text), &tmp_pos);
2194 * gtk_entry_get_layout_offsets:
2195 * @entry: a #GtkEntry
2196 * @x: location to store X offset of layout, or %NULL
2197 * @y: location to store Y offset of layout, or %NULL
2200 * Obtains the position of the #PangoLayout used to render text
2201 * in the entry, in widget coordinates. Useful if you want to line
2202 * up the text in an entry with some other text, e.g. when using the
2203 * entry to implement editable cells in a sheet widget.
2205 * Also useful to convert mouse events into coordinates inside the
2206 * #PangoLayout, e.g. to take some action if some part of the entry text
2209 * Note that as the user scrolls around in the entry the offsets will
2210 * change; you'll need to connect to the "notify::scroll_offset"
2211 * signal to track this. Remember when using the #PangoLayout
2212 * functions you need to convert to and from pixels using
2213 * PANGO_PIXELS() or #PANGO_SCALE.
2215 * Keep in mind that the layout text may contain a preedit string, so
2216 * gtk_entry_layout_index_to_text_index() and
2217 * gtk_entry_text_index_to_layout_index() are needed to convert byte
2218 * indices in the layout to byte indices in the entry contents.
2222 gtk_item_entry_get_layout_offsets (GtkItemEntry *entry,
2226 gint text_area_x, text_area_y;
2228 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2230 /* this gets coords relative to text area */
2231 get_layout_position (GTK_ENTRY(entry), x, y);
2233 /* convert to widget coords */
2234 get_text_area_size (GTK_ENTRY(entry), &text_area_x, &text_area_y, NULL, NULL);
2244 gtk_item_entry_set_justification(GtkItemEntry *entry, GtkJustification just)
2246 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2248 entry->justification = just;
2252 /* We display the cursor when
2254 * - the selection is empty, AND
2255 * - the widget has focus
2258 #define CURSOR_ON_MULTIPLIER 0.66
2259 #define CURSOR_OFF_MULTIPLIER 0.34
2260 #define CURSOR_PEND_MULTIPLIER 1.0
2263 cursor_blinks (GtkEntry *entry)
2265 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
2268 if (GTK_WIDGET_HAS_FOCUS (entry) &&
2269 entry->selection_bound == entry->current_pos)
2271 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
2279 get_cursor_time (GtkEntry *entry)
2281 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (entry));
2284 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
2290 show_cursor (GtkEntry *entry)
2292 if (!entry->cursor_visible)
2294 entry->cursor_visible = TRUE;
2296 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
2297 gtk_widget_queue_draw (GTK_WIDGET (entry));
2302 hide_cursor (GtkEntry *entry)
2304 if (entry->cursor_visible)
2306 entry->cursor_visible = FALSE;
2308 if (GTK_WIDGET_HAS_FOCUS (entry) && entry->selection_bound == entry->current_pos)
2309 gtk_widget_queue_draw (GTK_WIDGET (entry));
2317 blink_cb (gpointer data)
2321 GDK_THREADS_ENTER ();
2323 entry = GTK_ENTRY (data);
2325 g_assert (GTK_WIDGET_HAS_FOCUS (entry));
2326 g_assert (entry->selection_bound == entry->current_pos);
2328 if (entry->cursor_visible)
2330 hide_cursor (entry);
2331 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_OFF_MULTIPLIER,
2337 show_cursor (entry);
2338 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
2343 GDK_THREADS_LEAVE ();
2345 /* Remove ourselves */
2350 gtk_entry_check_cursor_blink (GtkEntry *entry)
2352 if (cursor_blinks (entry))
2354 if (!entry->blink_timeout)
2356 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_ON_MULTIPLIER,
2359 show_cursor (entry);
2364 if (entry->blink_timeout)
2366 gtk_timeout_remove (entry->blink_timeout);
2367 entry->blink_timeout = 0;
2370 entry->cursor_visible = TRUE;
2376 gtk_entry_pend_cursor_blink (GtkEntry *entry)
2378 if (cursor_blinks (entry))
2380 if (entry->blink_timeout != 0)
2381 gtk_timeout_remove (entry->blink_timeout);
2383 entry->blink_timeout = gtk_timeout_add (get_cursor_time (entry) * CURSOR_PEND_MULTIPLIER,
2386 show_cursor (entry);
2391 gtk_item_entry_set_cursor_visible(GtkItemEntry *entry, gboolean visible)
2393 g_return_if_fail (GTK_IS_ITEM_ENTRY (entry));
2395 GTK_ENTRY(entry)->cursor_visible = visible;
2399 gtk_item_entry_get_cursor_visible(GtkItemEntry *entry)
2401 g_return_val_if_fail (GTK_IS_ITEM_ENTRY (entry), FALSE);
2403 return(GTK_ENTRY(entry)->cursor_visible);