1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
33 #include "gtkbindings.h"
35 #include "gtkimagemenuitem.h"
38 #include "gtkmarshalers.h"
40 #include "gtkmenuitem.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtksettings.h"
44 #include "gtktextbufferrichtext.h"
45 #include "gtktextdisplay.h"
46 #include "gtktextview.h"
47 #include "gtkimmulticontext.h"
48 #include "gtkprivate.h"
49 #include "gtktextutil.h"
50 #include "gtkwidgetprivate.h"
51 #include "gtkwindow.h"
52 #include "gtkscrollable.h"
53 #include "gtktypebuiltins.h"
58 * @Short_description: Widget that displays a GtkTextBuffer
60 * @See_also: #GtkTextBuffer, #GtkTextIter
62 * You may wish to begin by reading the <link linkend="TextWidget">text widget
63 * conceptual overview</link> which gives an overview of all the objects and data
64 * types related to the text widget and how they work together.
68 /* How scrolling, validation, exposes, etc. work.
70 * The expose_event handler has the invariant that the onscreen lines
71 * have been validated.
73 * There are two ways that onscreen lines can become invalid. The first
74 * is to change which lines are onscreen. This happens when the value
75 * of a scroll adjustment changes. So the code path begins in
76 * gtk_text_view_value_changed() and goes like this:
77 * - gdk_window_scroll() to reflect the new adjustment value
78 * - validate the lines that were moved onscreen
79 * - gdk_window_process_updates() to handle the exposes immediately
81 * The second way is that you get the "invalidated" signal from the layout,
82 * indicating that lines have become invalid. This code path begins in
83 * invalidated_handler() and goes like this:
84 * - install high-priority idle which does the rest of the steps
85 * - if a scroll is pending from scroll_to_mark(), do the scroll,
86 * jumping to the gtk_text_view_value_changed() code path
87 * - otherwise, validate the onscreen lines
88 * - DO NOT process updates
90 * In both cases, validating the onscreen lines can trigger a scroll
91 * due to maintaining the first_para on the top of the screen.
92 * If validation triggers a scroll, we jump to the top of the code path
93 * for value_changed, and bail out of the current code path.
95 * Also, in size_allocate, if we invalidate some lines from changing
96 * the layout width, we need to go ahead and run the high-priority idle,
97 * because GTK sends exposes right after doing the size allocates without
98 * returning to the main loop. This is also why the high-priority idle
99 * is at a higher priority than resizing.
104 #define DEBUG_VALIDATION_AND_SCROLLING
107 #ifdef DEBUG_VALIDATION_AND_SCROLLING
113 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->priv->text_window)
114 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->priv->text_window)
116 #define SPACE_FOR_CURSOR 1
118 #define GTK_TEXT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_VIEW, GtkTextViewPrivate))
120 typedef struct _GtkTextWindow GtkTextWindow;
121 typedef struct _GtkTextPendingScroll GtkTextPendingScroll;
123 struct _GtkTextViewPrivate
125 GtkTextLayout *layout;
126 GtkTextBuffer *buffer;
128 guint blink_time; /* time in msec the cursor has blinked since last user event */
131 GdkDevice *grab_device;
132 GdkDevice *dnd_device;
134 gulong selection_drag_handler;
135 guint scroll_timeout;
137 GtkTextWindow *text_window;
138 GtkTextWindow *left_window;
139 GtkTextWindow *right_window;
140 GtkTextWindow *top_window;
141 GtkTextWindow *bottom_window;
143 GtkAdjustment *hadjustment;
144 GtkAdjustment *vadjustment;
146 gint xoffset; /* Offsets between widget coordinates and buffer coordinates */
148 gint width; /* Width and height of the buffer */
151 /* This is used to monitor the overall size request
152 * and decide whether we need to queue resizes when
153 * the buffer content changes.
155 * FIXME: This could be done in a simpler way by
156 * consulting the above width/height of the buffer + some
157 * padding values, however all of this request code needs
158 * to be changed to use GtkWidget Iface and deserves
161 GtkRequisition cached_size_request;
163 /* The virtual cursor position is normally the same as the
164 * actual (strong) cursor position, except in two circumstances:
166 * a) When the cursor is moved vertically with the keyboard
167 * b) When the text view is scrolled with the keyboard
169 * In case a), virtual_cursor_x is preserved, but not virtual_cursor_y
170 * In case b), both virtual_cursor_x and virtual_cursor_y are preserved.
172 gint virtual_cursor_x; /* -1 means use actual cursor position */
173 gint virtual_cursor_y; /* -1 means use actual cursor position */
175 GtkTextMark *first_para_mark; /* Mark at the beginning of the first onscreen paragraph */
176 gint first_para_pixels; /* Offset of top of screen in the first onscreen paragraph */
178 GtkTextMark *dnd_mark;
181 guint first_validate_idle; /* Idle to revalidate onscreen portion, runs before resize */
182 guint incremental_validate_idle; /* Idle to revalidate offscreen portions, runs after redraw */
184 GtkIMContext *im_context;
185 GtkWidget *popup_menu;
192 GtkTextPendingScroll *pending_scroll;
194 gint pending_place_cursor_button;
196 /* Default style settings */
197 gint pixels_above_lines;
198 gint pixels_below_lines;
199 gint pixels_inside_wrap;
200 GtkWrapMode wrap_mode;
201 GtkJustification justify;
208 guint overwrite_mode : 1;
209 guint cursor_visible : 1;
211 /* if we have reset the IM since the last character entered */
212 guint need_im_reset : 1;
214 guint accepts_tab : 1;
216 guint width_changed : 1;
218 /* debug flag - means that we've validated onscreen since the
219 * last "invalidate" signal from the layout
221 guint onscreen_validated : 1;
223 guint mouse_cursor_obscured : 1;
225 guint scroll_after_paste : 1;
227 /* GtkScrollablePolicy needs to be checked when
228 * driving the scrollable adjustment values */
229 guint hscroll_policy : 1;
230 guint vscroll_policy : 1;
233 struct _GtkTextPendingScroll
236 gdouble within_margin;
257 TOGGLE_CURSOR_VISIBLE,
265 PROP_PIXELS_ABOVE_LINES,
266 PROP_PIXELS_BELOW_LINES,
267 PROP_PIXELS_INSIDE_WRAP,
286 static void gtk_text_view_finalize (GObject *object);
287 static void gtk_text_view_set_property (GObject *object,
291 static void gtk_text_view_get_property (GObject *object,
295 static void gtk_text_view_destroy (GtkWidget *widget);
296 static void gtk_text_view_size_request (GtkWidget *widget,
297 GtkRequisition *requisition);
298 static void gtk_text_view_get_preferred_width (GtkWidget *widget,
301 static void gtk_text_view_get_preferred_height (GtkWidget *widget,
304 static void gtk_text_view_size_allocate (GtkWidget *widget,
305 GtkAllocation *allocation);
306 static void gtk_text_view_realize (GtkWidget *widget);
307 static void gtk_text_view_unrealize (GtkWidget *widget);
308 static void gtk_text_view_style_updated (GtkWidget *widget);
309 static void gtk_text_view_direction_changed (GtkWidget *widget,
310 GtkTextDirection previous_direction);
311 static void gtk_text_view_grab_notify (GtkWidget *widget,
312 gboolean was_grabbed);
313 static void gtk_text_view_state_flags_changed (GtkWidget *widget,
314 GtkStateFlags previous_state);
316 static gint gtk_text_view_event (GtkWidget *widget,
318 static gint gtk_text_view_key_press_event (GtkWidget *widget,
320 static gint gtk_text_view_key_release_event (GtkWidget *widget,
322 static gint gtk_text_view_button_press_event (GtkWidget *widget,
323 GdkEventButton *event);
324 static gint gtk_text_view_button_release_event (GtkWidget *widget,
325 GdkEventButton *event);
326 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
327 GdkEventFocus *event);
328 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
329 GdkEventFocus *event);
330 static gint gtk_text_view_motion_event (GtkWidget *widget,
331 GdkEventMotion *event);
332 static gint gtk_text_view_draw (GtkWidget *widget,
334 static void gtk_text_view_draw_focus (GtkWidget *widget,
336 static gboolean gtk_text_view_focus (GtkWidget *widget,
337 GtkDirectionType direction);
338 static void gtk_text_view_select_all (GtkWidget *widget,
342 /* Source side drag signals */
343 static void gtk_text_view_drag_begin (GtkWidget *widget,
344 GdkDragContext *context);
345 static void gtk_text_view_drag_end (GtkWidget *widget,
346 GdkDragContext *context);
347 static void gtk_text_view_drag_data_get (GtkWidget *widget,
348 GdkDragContext *context,
349 GtkSelectionData *selection_data,
352 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
353 GdkDragContext *context);
355 /* Target side drag signals */
356 static void gtk_text_view_drag_leave (GtkWidget *widget,
357 GdkDragContext *context,
359 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
360 GdkDragContext *context,
364 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
365 GdkDragContext *context,
369 static void gtk_text_view_drag_data_received (GtkWidget *widget,
370 GdkDragContext *context,
373 GtkSelectionData *selection_data,
377 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
379 static void gtk_text_view_move_cursor (GtkTextView *text_view,
380 GtkMovementStep step,
382 gboolean extend_selection);
383 static void gtk_text_view_move_viewport (GtkTextView *text_view,
386 static void gtk_text_view_set_anchor (GtkTextView *text_view);
387 static gboolean gtk_text_view_scroll_pages (GtkTextView *text_view,
389 gboolean extend_selection);
390 static gboolean gtk_text_view_scroll_hpages(GtkTextView *text_view,
392 gboolean extend_selection);
393 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
395 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
398 static void gtk_text_view_backspace (GtkTextView *text_view);
399 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
400 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
401 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
402 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
403 static void gtk_text_view_toggle_cursor_visible (GtkTextView *text_view);
405 static void gtk_text_view_unselect (GtkTextView *text_view);
407 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
408 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
410 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
411 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
412 GtkTextAttributes *values);
413 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
414 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
415 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
416 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
417 const GtkTextIter *iter,
418 GdkEventButton *event);
419 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view);
420 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
421 const GtkTextIter *iter,
422 GdkEventMotion *event);
423 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
424 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
425 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
426 static void gtk_text_view_reset_blink_time (GtkTextView *text_view);
428 static void gtk_text_view_value_changed (GtkAdjustment *adjustment,
430 static void gtk_text_view_commit_handler (GtkIMContext *context,
432 GtkTextView *text_view);
433 static void gtk_text_view_commit_text (GtkTextView *text_view,
435 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
436 GtkTextView *text_view);
437 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
438 GtkTextView *text_view);
439 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
442 GtkTextView *text_view);
444 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
445 const GtkTextIter *location,
448 static void gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
449 const GParamSpec *pspec,
451 static void gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
452 GtkClipboard *clipboard,
454 static void gtk_text_view_get_cursor_location (GtkTextView *text_view,
456 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
460 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
464 static void gtk_text_view_do_popup (GtkTextView *text_view,
465 GdkEventButton *event);
467 static void cancel_pending_scroll (GtkTextView *text_view);
468 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
470 gdouble within_margin,
475 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
476 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
477 static void gtk_text_view_invalidate (GtkTextView *text_view);
478 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
480 static void gtk_text_view_set_hadjustment (GtkTextView *text_view,
481 GtkAdjustment *adjustment);
482 static void gtk_text_view_set_vadjustment (GtkTextView *text_view,
483 GtkAdjustment *adjustment);
484 static void gtk_text_view_set_hadjustment_values (GtkTextView *text_view);
485 static void gtk_text_view_set_vadjustment_values (GtkTextView *text_view);
487 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
489 /* Container methods */
490 static void gtk_text_view_add (GtkContainer *container,
492 static void gtk_text_view_remove (GtkContainer *container,
494 static void gtk_text_view_forall (GtkContainer *container,
495 gboolean include_internals,
496 GtkCallback callback,
497 gpointer callback_data);
499 /* FIXME probably need the focus methods. */
501 typedef struct _GtkTextViewChild GtkTextViewChild;
503 struct _GtkTextViewChild
507 GtkTextChildAnchor *anchor;
509 gint from_top_of_line;
510 gint from_left_of_buffer;
512 /* These are ignored if anchor != NULL */
513 GtkTextWindowType type;
518 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
519 GtkTextChildAnchor *anchor,
520 GtkTextLayout *layout);
521 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
522 GtkTextWindowType type,
525 static void text_view_child_free (GtkTextViewChild *child);
526 static void text_view_child_set_parent_window (GtkTextView *text_view,
527 GtkTextViewChild *child);
529 struct _GtkTextWindow
531 GtkTextWindowType type;
534 GdkWindow *bin_window;
535 GtkRequisition requisition;
536 GdkRectangle allocation;
539 static GtkTextWindow *text_window_new (GtkTextWindowType type,
542 gint height_request);
543 static void text_window_free (GtkTextWindow *win);
544 static void text_window_realize (GtkTextWindow *win,
546 static void text_window_unrealize (GtkTextWindow *win);
547 static void text_window_size_allocate (GtkTextWindow *win,
549 static void text_window_scroll (GtkTextWindow *win,
552 static void text_window_invalidate_rect (GtkTextWindow *win,
554 static void text_window_invalidate_cursors (GtkTextWindow *win);
556 static gint text_window_get_width (GtkTextWindow *win);
557 static gint text_window_get_height (GtkTextWindow *win);
560 static guint signals[LAST_SIGNAL] = { 0 };
562 G_DEFINE_TYPE_WITH_CODE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER,
563 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
566 add_move_binding (GtkBindingSet *binding_set,
569 GtkMovementStep step,
572 g_assert ((modmask & GDK_SHIFT_MASK) == 0);
574 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
578 G_TYPE_BOOLEAN, FALSE);
580 /* Selection-extending version */
581 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
585 G_TYPE_BOOLEAN, TRUE);
589 gtk_text_view_class_init (GtkTextViewClass *klass)
591 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
592 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
593 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
594 GtkBindingSet *binding_set;
596 /* Default handlers and virtual methods
598 gobject_class->set_property = gtk_text_view_set_property;
599 gobject_class->get_property = gtk_text_view_get_property;
600 gobject_class->finalize = gtk_text_view_finalize;
602 widget_class->destroy = gtk_text_view_destroy;
603 widget_class->realize = gtk_text_view_realize;
604 widget_class->unrealize = gtk_text_view_unrealize;
605 widget_class->style_updated = gtk_text_view_style_updated;
606 widget_class->direction_changed = gtk_text_view_direction_changed;
607 widget_class->grab_notify = gtk_text_view_grab_notify;
608 widget_class->state_flags_changed = gtk_text_view_state_flags_changed;
609 widget_class->get_preferred_width = gtk_text_view_get_preferred_width;
610 widget_class->get_preferred_height = gtk_text_view_get_preferred_height;
611 widget_class->size_allocate = gtk_text_view_size_allocate;
612 widget_class->event = gtk_text_view_event;
613 widget_class->key_press_event = gtk_text_view_key_press_event;
614 widget_class->key_release_event = gtk_text_view_key_release_event;
615 widget_class->button_press_event = gtk_text_view_button_press_event;
616 widget_class->button_release_event = gtk_text_view_button_release_event;
617 widget_class->focus_in_event = gtk_text_view_focus_in_event;
618 widget_class->focus_out_event = gtk_text_view_focus_out_event;
619 widget_class->motion_notify_event = gtk_text_view_motion_event;
620 widget_class->draw = gtk_text_view_draw;
621 widget_class->focus = gtk_text_view_focus;
622 widget_class->drag_begin = gtk_text_view_drag_begin;
623 widget_class->drag_end = gtk_text_view_drag_end;
624 widget_class->drag_data_get = gtk_text_view_drag_data_get;
625 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
627 widget_class->drag_leave = gtk_text_view_drag_leave;
628 widget_class->drag_motion = gtk_text_view_drag_motion;
629 widget_class->drag_drop = gtk_text_view_drag_drop;
630 widget_class->drag_data_received = gtk_text_view_drag_data_received;
632 widget_class->popup_menu = gtk_text_view_popup_menu;
634 container_class->add = gtk_text_view_add;
635 container_class->remove = gtk_text_view_remove;
636 container_class->forall = gtk_text_view_forall;
638 klass->move_cursor = gtk_text_view_move_cursor;
639 klass->set_anchor = gtk_text_view_set_anchor;
640 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
641 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
642 klass->backspace = gtk_text_view_backspace;
643 klass->cut_clipboard = gtk_text_view_cut_clipboard;
644 klass->copy_clipboard = gtk_text_view_copy_clipboard;
645 klass->paste_clipboard = gtk_text_view_paste_clipboard;
646 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
652 g_object_class_install_property (gobject_class,
653 PROP_PIXELS_ABOVE_LINES,
654 g_param_spec_int ("pixels-above-lines",
655 P_("Pixels Above Lines"),
656 P_("Pixels of blank space above paragraphs"),
660 GTK_PARAM_READWRITE));
662 g_object_class_install_property (gobject_class,
663 PROP_PIXELS_BELOW_LINES,
664 g_param_spec_int ("pixels-below-lines",
665 P_("Pixels Below Lines"),
666 P_("Pixels of blank space below paragraphs"),
670 GTK_PARAM_READWRITE));
672 g_object_class_install_property (gobject_class,
673 PROP_PIXELS_INSIDE_WRAP,
674 g_param_spec_int ("pixels-inside-wrap",
675 P_("Pixels Inside Wrap"),
676 P_("Pixels of blank space between wrapped lines in a paragraph"),
680 GTK_PARAM_READWRITE));
682 g_object_class_install_property (gobject_class,
684 g_param_spec_boolean ("editable",
686 P_("Whether the text can be modified by the user"),
688 GTK_PARAM_READWRITE));
690 g_object_class_install_property (gobject_class,
692 g_param_spec_enum ("wrap-mode",
694 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
697 GTK_PARAM_READWRITE));
699 g_object_class_install_property (gobject_class,
701 g_param_spec_enum ("justification",
703 P_("Left, right, or center justification"),
704 GTK_TYPE_JUSTIFICATION,
706 GTK_PARAM_READWRITE));
708 g_object_class_install_property (gobject_class,
710 g_param_spec_int ("left-margin",
712 P_("Width of the left margin in pixels"),
716 GTK_PARAM_READWRITE));
718 g_object_class_install_property (gobject_class,
720 g_param_spec_int ("right-margin",
722 P_("Width of the right margin in pixels"),
726 GTK_PARAM_READWRITE));
728 g_object_class_install_property (gobject_class,
730 g_param_spec_int ("indent",
732 P_("Amount to indent the paragraph, in pixels"),
736 GTK_PARAM_READWRITE));
738 g_object_class_install_property (gobject_class,
740 g_param_spec_boxed ("tabs",
742 P_("Custom tabs for this text"),
743 PANGO_TYPE_TAB_ARRAY,
744 GTK_PARAM_READWRITE));
746 g_object_class_install_property (gobject_class,
748 g_param_spec_boolean ("cursor-visible",
749 P_("Cursor Visible"),
750 P_("If the insertion cursor is shown"),
752 GTK_PARAM_READWRITE));
754 g_object_class_install_property (gobject_class,
756 g_param_spec_object ("buffer",
758 P_("The buffer which is displayed"),
759 GTK_TYPE_TEXT_BUFFER,
760 GTK_PARAM_READWRITE));
762 g_object_class_install_property (gobject_class,
764 g_param_spec_boolean ("overwrite",
765 P_("Overwrite mode"),
766 P_("Whether entered text overwrites existing contents"),
768 GTK_PARAM_READWRITE));
770 g_object_class_install_property (gobject_class,
772 g_param_spec_boolean ("accepts-tab",
774 P_("Whether Tab will result in a tab character being entered"),
776 GTK_PARAM_READWRITE));
779 * GtkTextView:im-module:
781 * Which IM (input method) module should be used for this entry.
784 * Setting this to a non-%NULL value overrides the
785 * system-wide IM module setting. See the GtkSettings
786 * #GtkSettings:gtk-im-module property.
790 g_object_class_install_property (gobject_class,
792 g_param_spec_string ("im-module",
794 P_("Which IM module should be used"),
796 GTK_PARAM_READWRITE));
798 /* GtkScrollable interface */
799 g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment");
800 g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment");
801 g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
802 g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
807 gtk_widget_class_install_style_property (widget_class,
808 g_param_spec_boxed ("error-underline-color",
809 P_("Error underline color"),
810 P_("Color with which to draw error-indication underlines"),
812 GTK_PARAM_READABLE));
819 * GtkTextView::move-cursor:
820 * @text_view: the object which received the signal
821 * @step: the granularity of the move, as a #GtkMovementStep
822 * @count: the number of @step units to move
823 * @extend_selection: %TRUE if the move should extend the selection
825 * The ::move-cursor signal is a
826 * <link linkend="keybinding-signals">keybinding signal</link>
827 * which gets emitted when the user initiates a cursor movement.
828 * If the cursor is not visible in @text_view, this signal causes
829 * the viewport to be moved instead.
831 * Applications should not connect to it, but may emit it with
832 * g_signal_emit_by_name() if they need to control the cursor
835 * The default bindings for this signal come in two variants,
836 * the variant with the Shift modifier extends the selection,
837 * the variant without the Shift modifer does not.
838 * There are too many key combinations to list them all here.
840 * <listitem>Arrow keys move by individual characters/lines</listitem>
841 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
842 * <listitem>Home/End keys move to the ends of the buffer</listitem>
843 * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
844 * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
847 signals[MOVE_CURSOR] =
848 g_signal_new (I_("move-cursor"),
849 G_OBJECT_CLASS_TYPE (gobject_class),
850 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
851 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
853 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
855 GTK_TYPE_MOVEMENT_STEP,
860 * GtkTextView::move-viewport:
861 * @text_view: the object which received the signal
862 * @step: the granularity of the move, as a #GtkMovementStep
863 * @count: the number of @step units to move
865 * The ::move-viewport signal is a
866 * <link linkend="keybinding-signals">keybinding signal</link>
867 * which can be bound to key combinations to allow the user
868 * to move the viewport, i.e. change what part of the text view
869 * is visible in a containing scrolled window.
871 * There are no default bindings for this signal.
873 signals[MOVE_VIEWPORT] =
874 g_signal_new_class_handler (I_("move-viewport"),
875 G_OBJECT_CLASS_TYPE (gobject_class),
876 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
877 G_CALLBACK (gtk_text_view_move_viewport),
879 _gtk_marshal_VOID__ENUM_INT,
881 GTK_TYPE_SCROLL_STEP,
885 * GtkTextView::set-anchor:
886 * @text_view: the object which received the signal
888 * The ::set-anchor signal is a
889 * <link linkend="keybinding-signals">keybinding signal</link>
890 * which gets emitted when the user initiates setting the "anchor"
891 * mark. The "anchor" mark gets placed at the same position as the
894 * This signal has no default bindings.
896 signals[SET_ANCHOR] =
897 g_signal_new (I_("set-anchor"),
898 G_OBJECT_CLASS_TYPE (gobject_class),
899 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
900 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
902 _gtk_marshal_VOID__VOID,
906 * GtkTextView::insert-at-cursor:
907 * @text_view: the object which received the signal
908 * @string: the string to insert
910 * The ::insert-at-cursor signal is a
911 * <link linkend="keybinding-signals">keybinding signal</link>
912 * which gets emitted when the user initiates the insertion of a
913 * fixed string at the cursor.
915 * This signal has no default bindings.
917 signals[INSERT_AT_CURSOR] =
918 g_signal_new (I_("insert-at-cursor"),
919 G_OBJECT_CLASS_TYPE (gobject_class),
920 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
921 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
923 _gtk_marshal_VOID__STRING,
928 * GtkTextView::delete-from-cursor:
929 * @text_view: the object which received the signal
930 * @type: the granularity of the deletion, as a #GtkDeleteType
931 * @count: the number of @type units to delete
933 * The ::delete-from-cursor signal is a
934 * <link linkend="keybinding-signals">keybinding signal</link>
935 * which gets emitted when the user initiates a text deletion.
937 * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
938 * if there is one, otherwise it deletes the requested number
941 * The default bindings for this signal are
942 * Delete for deleting a character, Ctrl-Delete for
943 * deleting a word and Ctrl-Backspace for deleting a word
946 signals[DELETE_FROM_CURSOR] =
947 g_signal_new (I_("delete-from-cursor"),
948 G_OBJECT_CLASS_TYPE (gobject_class),
949 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
950 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
952 _gtk_marshal_VOID__ENUM_INT,
954 GTK_TYPE_DELETE_TYPE,
958 * GtkTextView::backspace:
959 * @text_view: the object which received the signal
961 * The ::backspace signal is a
962 * <link linkend="keybinding-signals">keybinding signal</link>
963 * which gets emitted when the user asks for it.
965 * The default bindings for this signal are
966 * Backspace and Shift-Backspace.
969 g_signal_new (I_("backspace"),
970 G_OBJECT_CLASS_TYPE (gobject_class),
971 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
972 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
974 _gtk_marshal_VOID__VOID,
978 * GtkTextView::cut-clipboard:
979 * @text_view: the object which received the signal
981 * The ::cut-clipboard signal is a
982 * <link linkend="keybinding-signals">keybinding signal</link>
983 * which gets emitted to cut the selection to the clipboard.
985 * The default bindings for this signal are
986 * Ctrl-x and Shift-Delete.
988 signals[CUT_CLIPBOARD] =
989 g_signal_new (I_("cut-clipboard"),
990 G_OBJECT_CLASS_TYPE (gobject_class),
991 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
992 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
994 _gtk_marshal_VOID__VOID,
998 * GtkTextView::copy-clipboard:
999 * @text_view: the object which received the signal
1001 * The ::copy-clipboard signal is a
1002 * <link linkend="keybinding-signals">keybinding signal</link>
1003 * which gets emitted to copy the selection to the clipboard.
1005 * The default bindings for this signal are
1006 * Ctrl-c and Ctrl-Insert.
1008 signals[COPY_CLIPBOARD] =
1009 g_signal_new (I_("copy-clipboard"),
1010 G_OBJECT_CLASS_TYPE (gobject_class),
1011 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1012 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
1014 _gtk_marshal_VOID__VOID,
1018 * GtkTextView::paste-clipboard:
1019 * @text_view: the object which received the signal
1021 * The ::paste-clipboard signal is a
1022 * <link linkend="keybinding-signals">keybinding signal</link>
1023 * which gets emitted to paste the contents of the clipboard
1024 * into the text view.
1026 * The default bindings for this signal are
1027 * Ctrl-v and Shift-Insert.
1029 signals[PASTE_CLIPBOARD] =
1030 g_signal_new (I_("paste-clipboard"),
1031 G_OBJECT_CLASS_TYPE (gobject_class),
1032 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1033 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
1035 _gtk_marshal_VOID__VOID,
1039 * GtkTextView::toggle-overwrite:
1040 * @text_view: the object which received the signal
1042 * The ::toggle-overwrite signal is a
1043 * <link linkend="keybinding-signals">keybinding signal</link>
1044 * which gets emitted to toggle the overwrite mode of the text view.
1046 * The default bindings for this signal is Insert.
1048 signals[TOGGLE_OVERWRITE] =
1049 g_signal_new (I_("toggle-overwrite"),
1050 G_OBJECT_CLASS_TYPE (gobject_class),
1051 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1052 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
1054 _gtk_marshal_VOID__VOID,
1058 * GtkTextView::populate-popup:
1059 * @entry: The text view on which the signal is emitted
1060 * @menu: the menu that is being populated
1062 * The ::populate-popup signal gets emitted before showing the
1063 * context menu of the text view.
1065 * If you need to add items to the context menu, connect
1066 * to this signal and append your menuitems to the @menu.
1068 signals[POPULATE_POPUP] =
1069 g_signal_new (I_("populate-popup"),
1070 G_OBJECT_CLASS_TYPE (gobject_class),
1072 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
1074 _gtk_marshal_VOID__OBJECT,
1079 * GtkTextView::select-all:
1080 * @text_view: the object which received the signal
1081 * @select: %TRUE to select, %FALSE to unselect
1083 * The ::select-all signal is a
1084 * <link linkend="keybinding-signals">keybinding signal</link>
1085 * which gets emitted to select or unselect the complete
1086 * contents of the text view.
1088 * The default bindings for this signal are Ctrl-a and Ctrl-/
1089 * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
1091 signals[SELECT_ALL] =
1092 g_signal_new_class_handler (I_("select-all"),
1093 G_OBJECT_CLASS_TYPE (gobject_class),
1094 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1095 G_CALLBACK (gtk_text_view_select_all),
1097 _gtk_marshal_VOID__BOOLEAN,
1098 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
1101 * GtkTextView::toggle-cursor-visible:
1102 * @text_view: the object which received the signal
1104 * The ::toggle-cursor-visible signal is a
1105 * <link linkend="keybinding-signals">keybinding signal</link>
1106 * which gets emitted to toggle the visibility of the cursor.
1108 * The default binding for this signal is F7.
1110 signals[TOGGLE_CURSOR_VISIBLE] =
1111 g_signal_new_class_handler (I_("toggle-cursor-visible"),
1112 G_OBJECT_CLASS_TYPE (gobject_class),
1113 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1114 G_CALLBACK (gtk_text_view_toggle_cursor_visible),
1116 _gtk_marshal_VOID__VOID,
1120 * GtkTextView::preedit-changed:
1121 * @text_view: the object which received the signal
1122 * @preedit: the current preedit string
1124 * If an input method is used, the typed text will not immediately
1125 * be committed to the buffer. So if you are interested in the text,
1126 * connect to this signal.
1128 * This signal is only emitted if the text at the given position
1129 * is actually editable.
1133 signals[PREEDIT_CHANGED] =
1134 g_signal_new_class_handler (I_("preedit-changed"),
1135 G_OBJECT_CLASS_TYPE (gobject_class),
1136 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1139 _gtk_marshal_VOID__STRING,
1147 binding_set = gtk_binding_set_by_class (klass);
1149 /* Moving the insertion point */
1150 add_move_binding (binding_set, GDK_KEY_Right, 0,
1151 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1153 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
1154 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1156 add_move_binding (binding_set, GDK_KEY_Left, 0,
1157 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1159 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
1160 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1162 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1163 GTK_MOVEMENT_WORDS, 1);
1165 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1166 GTK_MOVEMENT_WORDS, 1);
1168 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1169 GTK_MOVEMENT_WORDS, -1);
1171 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1172 GTK_MOVEMENT_WORDS, -1);
1174 add_move_binding (binding_set, GDK_KEY_Up, 0,
1175 GTK_MOVEMENT_DISPLAY_LINES, -1);
1177 add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
1178 GTK_MOVEMENT_DISPLAY_LINES, -1);
1180 add_move_binding (binding_set, GDK_KEY_Down, 0,
1181 GTK_MOVEMENT_DISPLAY_LINES, 1);
1183 add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
1184 GTK_MOVEMENT_DISPLAY_LINES, 1);
1186 add_move_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
1187 GTK_MOVEMENT_PARAGRAPHS, -1);
1189 add_move_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
1190 GTK_MOVEMENT_PARAGRAPHS, -1);
1192 add_move_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
1193 GTK_MOVEMENT_PARAGRAPHS, 1);
1195 add_move_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
1196 GTK_MOVEMENT_PARAGRAPHS, 1);
1198 add_move_binding (binding_set, GDK_KEY_Home, 0,
1199 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1201 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1202 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1204 add_move_binding (binding_set, GDK_KEY_End, 0,
1205 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1207 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1208 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1210 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1211 GTK_MOVEMENT_BUFFER_ENDS, -1);
1213 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1214 GTK_MOVEMENT_BUFFER_ENDS, -1);
1216 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1217 GTK_MOVEMENT_BUFFER_ENDS, 1);
1219 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1220 GTK_MOVEMENT_BUFFER_ENDS, 1);
1222 add_move_binding (binding_set, GDK_KEY_Page_Up, 0,
1223 GTK_MOVEMENT_PAGES, -1);
1225 add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
1226 GTK_MOVEMENT_PAGES, -1);
1228 add_move_binding (binding_set, GDK_KEY_Page_Down, 0,
1229 GTK_MOVEMENT_PAGES, 1);
1231 add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
1232 GTK_MOVEMENT_PAGES, 1);
1234 add_move_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
1235 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1237 add_move_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
1238 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1240 add_move_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
1241 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1243 add_move_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
1244 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1247 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1249 G_TYPE_BOOLEAN, TRUE);
1251 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1253 G_TYPE_BOOLEAN, TRUE);
1256 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1258 G_TYPE_BOOLEAN, FALSE);
1260 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1262 G_TYPE_BOOLEAN, FALSE);
1265 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, 0,
1266 "delete-from-cursor", 2,
1267 G_TYPE_ENUM, GTK_DELETE_CHARS,
1270 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, 0,
1271 "delete-from-cursor", 2,
1272 G_TYPE_ENUM, GTK_DELETE_CHARS,
1275 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, 0,
1278 /* Make this do the same as Backspace, to help with mis-typing */
1279 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_SHIFT_MASK,
1282 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_CONTROL_MASK,
1283 "delete-from-cursor", 2,
1284 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1287 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, GDK_CONTROL_MASK,
1288 "delete-from-cursor", 2,
1289 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1292 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_CONTROL_MASK,
1293 "delete-from-cursor", 2,
1294 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1297 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1298 "delete-from-cursor", 2,
1299 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1302 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1303 "delete-from-cursor", 2,
1304 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1307 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1308 "delete-from-cursor", 2,
1309 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1312 /* Cut/copy/paste */
1314 gtk_binding_entry_add_signal (binding_set, GDK_KEY_x, GDK_CONTROL_MASK,
1315 "cut-clipboard", 0);
1316 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1317 "copy-clipboard", 0);
1318 gtk_binding_entry_add_signal (binding_set, GDK_KEY_v, GDK_CONTROL_MASK,
1319 "paste-clipboard", 0);
1321 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_SHIFT_MASK,
1322 "cut-clipboard", 0);
1323 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_CONTROL_MASK,
1324 "copy-clipboard", 0);
1325 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_SHIFT_MASK,
1326 "paste-clipboard", 0);
1329 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, 0,
1330 "toggle-overwrite", 0);
1331 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Insert, 0,
1332 "toggle-overwrite", 0);
1335 gtk_binding_entry_add_signal (binding_set, GDK_KEY_F7, 0,
1336 "toggle-cursor-visible", 0);
1338 /* Control-tab focus motion */
1339 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, GDK_CONTROL_MASK,
1341 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1342 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, GDK_CONTROL_MASK,
1344 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1346 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1348 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1349 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1351 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1353 g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1357 gtk_text_view_init (GtkTextView *text_view)
1359 GtkWidget *widget = GTK_WIDGET (text_view);
1360 GtkTargetList *target_list;
1361 GtkTextViewPrivate *priv;
1363 text_view->priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
1364 priv = text_view->priv;
1366 gtk_widget_set_can_focus (widget, TRUE);
1368 /* Set up default style */
1369 priv->wrap_mode = GTK_WRAP_NONE;
1370 priv->pixels_above_lines = 0;
1371 priv->pixels_below_lines = 0;
1372 priv->pixels_inside_wrap = 0;
1373 priv->justify = GTK_JUSTIFY_LEFT;
1374 priv->left_margin = 0;
1375 priv->right_margin = 0;
1378 priv->editable = TRUE;
1380 priv->scroll_after_paste = TRUE;
1382 gtk_drag_dest_set (widget, 0, NULL, 0,
1383 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1385 target_list = gtk_target_list_new (NULL, 0);
1386 gtk_drag_dest_set_target_list (widget, target_list);
1387 gtk_target_list_unref (target_list);
1389 priv->virtual_cursor_x = -1;
1390 priv->virtual_cursor_y = -1;
1392 /* This object is completely private. No external entity can gain a reference
1393 * to it; so we create it here and destroy it in finalize ().
1395 priv->im_context = gtk_im_multicontext_new ();
1397 g_signal_connect (priv->im_context, "commit",
1398 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1399 g_signal_connect (priv->im_context, "preedit-changed",
1400 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1401 g_signal_connect (priv->im_context, "retrieve-surrounding",
1402 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1403 g_signal_connect (priv->im_context, "delete-surrounding",
1404 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1406 priv->cursor_visible = TRUE;
1408 priv->accepts_tab = TRUE;
1410 priv->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1413 priv->drag_start_x = -1;
1414 priv->drag_start_y = -1;
1416 priv->pending_place_cursor_button = 0;
1418 /* We handle all our own redrawing */
1419 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1423 * gtk_text_view_new:
1425 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1426 * before using the text view, an empty default buffer will be created
1427 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1428 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1430 * Return value: a new #GtkTextView
1433 gtk_text_view_new (void)
1435 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1439 * gtk_text_view_new_with_buffer:
1440 * @buffer: a #GtkTextBuffer
1442 * Creates a new #GtkTextView widget displaying the buffer
1443 * @buffer. One buffer can be shared among many widgets.
1444 * @buffer may be %NULL to create a default buffer, in which case
1445 * this function is equivalent to gtk_text_view_new(). The
1446 * text view adds its own reference count to the buffer; it does not
1447 * take over an existing reference.
1449 * Return value: a new #GtkTextView.
1452 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1454 GtkTextView *text_view;
1456 text_view = (GtkTextView*)gtk_text_view_new ();
1458 gtk_text_view_set_buffer (text_view, buffer);
1460 return GTK_WIDGET (text_view);
1464 * gtk_text_view_set_buffer:
1465 * @text_view: a #GtkTextView
1466 * @buffer: (allow-none): a #GtkTextBuffer
1468 * Sets @buffer as the buffer being displayed by @text_view. The previous
1469 * buffer displayed by the text view is unreferenced, and a reference is
1470 * added to @buffer. If you owned a reference to @buffer before passing it
1471 * to this function, you must remove that reference yourself; #GtkTextView
1472 * will not "adopt" it.
1475 gtk_text_view_set_buffer (GtkTextView *text_view,
1476 GtkTextBuffer *buffer)
1478 GtkTextViewPrivate *priv;
1480 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1481 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1483 priv = text_view->priv;
1485 if (priv->buffer == buffer)
1488 if (priv->buffer != NULL)
1490 /* Destroy all anchored children */
1494 copy = g_slist_copy (priv->children);
1496 while (tmp_list != NULL)
1498 GtkTextViewChild *vc = tmp_list->data;
1502 gtk_widget_destroy (vc->widget);
1503 /* vc may now be invalid! */
1506 tmp_list = g_slist_next (tmp_list);
1509 g_slist_free (copy);
1511 g_signal_handlers_disconnect_by_func (priv->buffer,
1512 gtk_text_view_mark_set_handler,
1514 g_signal_handlers_disconnect_by_func (priv->buffer,
1515 gtk_text_view_target_list_notify,
1517 g_signal_handlers_disconnect_by_func (priv->buffer,
1518 gtk_text_view_paste_done_handler,
1521 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
1523 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1524 GDK_SELECTION_PRIMARY);
1525 gtk_text_buffer_remove_selection_clipboard (priv->buffer, clipboard);
1529 gtk_text_layout_set_buffer (priv->layout, NULL);
1531 g_object_unref (priv->buffer);
1532 priv->dnd_mark = NULL;
1533 priv->first_para_mark = NULL;
1534 cancel_pending_scroll (text_view);
1537 priv->buffer = buffer;
1540 gtk_text_layout_set_buffer (priv->layout, buffer);
1546 g_object_ref (buffer);
1548 gtk_text_buffer_get_iter_at_offset (priv->buffer, &start, 0);
1550 priv->dnd_mark = gtk_text_buffer_create_mark (priv->buffer,
1554 priv->first_para_mark = gtk_text_buffer_create_mark (priv->buffer,
1558 priv->first_para_pixels = 0;
1561 g_signal_connect (priv->buffer, "mark-set",
1562 G_CALLBACK (gtk_text_view_mark_set_handler),
1564 g_signal_connect (priv->buffer, "notify::paste-target-list",
1565 G_CALLBACK (gtk_text_view_target_list_notify),
1567 g_signal_connect (priv->buffer, "paste-done",
1568 G_CALLBACK (gtk_text_view_paste_done_handler),
1571 gtk_text_view_target_list_notify (priv->buffer, NULL, text_view);
1573 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
1575 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1576 GDK_SELECTION_PRIMARY);
1577 gtk_text_buffer_add_selection_clipboard (priv->buffer, clipboard);
1581 g_object_notify (G_OBJECT (text_view), "buffer");
1583 if (gtk_widget_get_visible (GTK_WIDGET (text_view)))
1584 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1586 DV(g_print ("Invalidating due to set_buffer\n"));
1587 gtk_text_view_invalidate (text_view);
1590 static GtkTextBuffer*
1591 get_buffer (GtkTextView *text_view)
1593 if (text_view->priv->buffer == NULL)
1596 b = gtk_text_buffer_new (NULL);
1597 gtk_text_view_set_buffer (text_view, b);
1601 return text_view->priv->buffer;
1605 * gtk_text_view_get_buffer:
1606 * @text_view: a #GtkTextView
1608 * Returns the #GtkTextBuffer being displayed by this text view.
1609 * The reference count on the buffer is not incremented; the caller
1610 * of this function won't own a new reference.
1612 * Return value: (transfer none): a #GtkTextBuffer
1615 gtk_text_view_get_buffer (GtkTextView *text_view)
1617 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1619 return get_buffer (text_view);
1623 * gtk_text_view_get_iter_at_location:
1624 * @text_view: a #GtkTextView
1625 * @iter: (out): a #GtkTextIter
1626 * @x: x position, in buffer coordinates
1627 * @y: y position, in buffer coordinates
1629 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1630 * coordinates are coordinates for the entire buffer, not just the
1631 * currently-displayed portion. If you have coordinates from an
1632 * event, you have to convert those to buffer coordinates with
1633 * gtk_text_view_window_to_buffer_coords().
1636 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1641 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1642 g_return_if_fail (iter != NULL);
1644 gtk_text_view_ensure_layout (text_view);
1646 gtk_text_layout_get_iter_at_pixel (text_view->priv->layout,
1651 * gtk_text_view_get_iter_at_position:
1652 * @text_view: a #GtkTextView
1653 * @iter: (out): a #GtkTextIter
1654 * @trailing: (out) (allow-none): if non-%NULL, location to store an integer indicating where
1655 * in the grapheme the user clicked. It will either be
1656 * zero, or the number of characters in the grapheme.
1657 * 0 represents the trailing edge of the grapheme.
1658 * @x: x position, in buffer coordinates
1659 * @y: y position, in buffer coordinates
1661 * Retrieves the iterator pointing to the character at buffer
1662 * coordinates @x and @y. Buffer coordinates are coordinates for
1663 * the entire buffer, not just the currently-displayed portion.
1664 * If you have coordinates from an event, you have to convert
1665 * those to buffer coordinates with
1666 * gtk_text_view_window_to_buffer_coords().
1668 * Note that this is different from gtk_text_view_get_iter_at_location(),
1669 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1675 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1681 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1682 g_return_if_fail (iter != NULL);
1684 gtk_text_view_ensure_layout (text_view);
1686 gtk_text_layout_get_iter_at_position (text_view->priv->layout,
1687 iter, trailing, x, y);
1691 * gtk_text_view_get_iter_location:
1692 * @text_view: a #GtkTextView
1693 * @iter: a #GtkTextIter
1694 * @location: (out): bounds of the character at @iter
1696 * Gets a rectangle which roughly contains the character at @iter.
1697 * The rectangle position is in buffer coordinates; use
1698 * gtk_text_view_buffer_to_window_coords() to convert these
1699 * coordinates to coordinates for one of the windows in the text view.
1702 gtk_text_view_get_iter_location (GtkTextView *text_view,
1703 const GtkTextIter *iter,
1704 GdkRectangle *location)
1706 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1707 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1709 gtk_text_view_ensure_layout (text_view);
1711 gtk_text_layout_get_iter_location (text_view->priv->layout, iter, location);
1715 * gtk_text_view_get_line_yrange:
1716 * @text_view: a #GtkTextView
1717 * @iter: a #GtkTextIter
1718 * @y: (out): return location for a y coordinate
1719 * @height: (out): return location for a height
1721 * Gets the y coordinate of the top of the line containing @iter,
1722 * and the height of the line. The coordinate is a buffer coordinate;
1723 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1726 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1727 const GtkTextIter *iter,
1731 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1732 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1734 gtk_text_view_ensure_layout (text_view);
1736 gtk_text_layout_get_line_yrange (text_view->priv->layout,
1743 * gtk_text_view_get_line_at_y:
1744 * @text_view: a #GtkTextView
1745 * @target_iter: (out): a #GtkTextIter
1746 * @y: a y coordinate
1747 * @line_top: (out): return location for top coordinate of the line
1749 * Gets the #GtkTextIter at the start of the line containing
1750 * the coordinate @y. @y is in buffer coordinates, convert from
1751 * window coordinates with gtk_text_view_window_to_buffer_coords().
1752 * If non-%NULL, @line_top will be filled with the coordinate of the top
1756 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1757 GtkTextIter *target_iter,
1761 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1763 gtk_text_view_ensure_layout (text_view);
1765 gtk_text_layout_get_line_at_y (text_view->priv->layout,
1772 * gtk_text_view_scroll_to_iter:
1773 * @text_view: a #GtkTextView
1774 * @iter: a #GtkTextIter
1775 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1776 * @use_align: whether to use alignment arguments (if %FALSE,
1777 * just get the mark onscreen)
1778 * @xalign: horizontal alignment of mark within visible area
1779 * @yalign: vertical alignment of mark within visible area
1781 * Scrolls @text_view so that @iter is on the screen in the position
1782 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1783 * left or top, 1.0 indicates right or bottom, 0.5 means center.
1784 * If @use_align is %FALSE, the text scrolls the minimal distance to
1785 * get the mark onscreen, possibly not scrolling at all. The effective
1786 * screen for purposes of this function is reduced by a margin of size
1789 * Note that this function uses the currently-computed height of the
1790 * lines in the text buffer. Line heights are computed in an idle
1791 * handler; so this function may not have the desired effect if it's
1792 * called before the height computations. To avoid oddness, consider
1793 * using gtk_text_view_scroll_to_mark() which saves a point to be
1794 * scrolled to after line validation.
1796 * Return value: %TRUE if scrolling occurred
1799 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1801 gdouble within_margin,
1807 GdkRectangle screen;
1812 gboolean retval = FALSE;
1814 gint screen_xoffset, screen_yoffset;
1815 gint current_x_scroll, current_y_scroll;
1817 /* FIXME why don't we do the validate-at-scroll-destination thing
1818 * from flush_scroll in this function? I think it wasn't done before
1819 * because changed_handler was screwed up, but I could be wrong.
1822 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1823 g_return_val_if_fail (iter != NULL, FALSE);
1824 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1825 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1826 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1828 widget = GTK_WIDGET (text_view);
1830 DV(g_print(G_STRLOC"\n"));
1832 gtk_text_layout_get_iter_location (text_view->priv->layout,
1836 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1838 current_x_scroll = text_view->priv->xoffset;
1839 current_y_scroll = text_view->priv->yoffset;
1841 screen.x = current_x_scroll;
1842 screen.y = current_y_scroll;
1843 screen.width = SCREEN_WIDTH (widget);
1844 screen.height = SCREEN_HEIGHT (widget);
1846 screen_xoffset = screen.width * within_margin;
1847 screen_yoffset = screen.height * within_margin;
1849 screen.x += screen_xoffset;
1850 screen.y += screen_yoffset;
1851 screen.width -= screen_xoffset * 2;
1852 screen.height -= screen_yoffset * 2;
1854 /* paranoia check */
1855 if (screen.width < 1)
1857 if (screen.height < 1)
1860 /* The -1 here ensures that we leave enough space to draw the cursor
1861 * when this function is used for horizontal scrolling.
1863 screen_right = screen.x + screen.width - 1;
1864 screen_bottom = screen.y + screen.height;
1866 /* The alignment affects the point in the target character that we
1867 * choose to align. If we're doing right/bottom alignment, we align
1868 * the right/bottom edge of the character the mark is at; if we're
1869 * doing left/top we align the left/top edge of the character; if
1870 * we're doing center alignment we align the center of the
1874 /* Vertical scroll */
1877 scroll_dest = current_y_scroll;
1881 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1883 /* if scroll_dest < screen.y, we move a negative increment (up),
1884 * else a positive increment (down)
1886 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1890 /* move minimum to get onscreen */
1891 if (rect.y < screen.y)
1893 scroll_dest = rect.y;
1894 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1896 else if ((rect.y + rect.height) > screen_bottom)
1898 scroll_dest = rect.y + rect.height;
1899 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1903 if (scroll_inc != 0)
1905 gtk_adjustment_set_value (text_view->priv->vadjustment,
1906 current_y_scroll + scroll_inc);
1908 DV (g_print (" vert increment %d\n", scroll_inc));
1911 /* Horizontal scroll */
1914 scroll_dest = current_x_scroll;
1918 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1920 /* if scroll_dest < screen.y, we move a negative increment (left),
1921 * else a positive increment (right)
1923 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1927 /* move minimum to get onscreen */
1928 if (rect.x < screen.x)
1930 scroll_dest = rect.x;
1931 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1933 else if ((rect.x + rect.width) > screen_right)
1935 scroll_dest = rect.x + rect.width;
1936 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1940 if (scroll_inc != 0)
1942 gtk_adjustment_set_value (text_view->priv->hadjustment,
1943 current_x_scroll + scroll_inc);
1945 DV (g_print (" horiz increment %d\n", scroll_inc));
1948 retval = (current_y_scroll != gtk_adjustment_get_value (text_view->priv->vadjustment))
1949 || (current_x_scroll != gtk_adjustment_get_value (text_view->priv->hadjustment));
1953 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1957 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1964 free_pending_scroll (GtkTextPendingScroll *scroll)
1966 if (!gtk_text_mark_get_deleted (scroll->mark))
1967 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1969 g_object_unref (scroll->mark);
1974 cancel_pending_scroll (GtkTextView *text_view)
1976 if (text_view->priv->pending_scroll)
1978 free_pending_scroll (text_view->priv->pending_scroll);
1979 text_view->priv->pending_scroll = NULL;
1984 gtk_text_view_queue_scroll (GtkTextView *text_view,
1986 gdouble within_margin,
1992 GtkTextPendingScroll *scroll;
1994 DV(g_print(G_STRLOC"\n"));
1996 scroll = g_new (GtkTextPendingScroll, 1);
1998 scroll->within_margin = within_margin;
1999 scroll->use_align = use_align;
2000 scroll->xalign = xalign;
2001 scroll->yalign = yalign;
2003 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2005 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
2008 gtk_text_mark_get_left_gravity (mark));
2010 g_object_ref (scroll->mark);
2012 cancel_pending_scroll (text_view);
2014 text_view->priv->pending_scroll = scroll;
2018 gtk_text_view_flush_scroll (GtkTextView *text_view)
2020 GtkAllocation allocation;
2022 GtkTextPendingScroll *scroll;
2026 widget = GTK_WIDGET (text_view);
2028 DV(g_print(G_STRLOC"\n"));
2030 if (text_view->priv->pending_scroll == NULL)
2032 DV (g_print ("in flush scroll, no pending scroll\n"));
2036 scroll = text_view->priv->pending_scroll;
2038 /* avoid recursion */
2039 text_view->priv->pending_scroll = NULL;
2041 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
2043 /* Validate area around the scroll destination, so the adjustment
2044 * can meaningfully point into that area. We must validate
2045 * enough area to be sure that after we scroll, everything onscreen
2046 * is valid; otherwise, validation will maintain the first para
2047 * in one place, but may push the target iter off the bottom of
2050 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
2051 gtk_widget_get_allocation (widget, &allocation);
2052 gtk_text_layout_validate_yrange (text_view->priv->layout, &iter,
2053 -(allocation.height * 2),
2054 allocation.height * 2);
2056 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
2058 /* Ensure we have updated width/height */
2059 gtk_text_view_update_adjustments (text_view);
2061 retval = gtk_text_view_scroll_to_iter (text_view,
2063 scroll->within_margin,
2068 free_pending_scroll (scroll);
2074 gtk_text_view_update_adjustments (GtkTextView *text_view)
2076 GtkTextViewPrivate *priv;
2077 gint width = 0, height = 0;
2079 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
2081 priv = text_view->priv;
2084 gtk_text_layout_get_size (priv->layout, &width, &height);
2086 /* Make room for the cursor after the last character in the widest line */
2087 width += SPACE_FOR_CURSOR;
2089 if (priv->width != width || priv->height != height)
2091 if (priv->width != width)
2092 priv->width_changed = TRUE;
2094 priv->width = width;
2095 priv->height = height;
2097 gtk_text_view_set_hadjustment_values (text_view);
2098 gtk_text_view_set_vadjustment_values (text_view);
2103 gtk_text_view_update_layout_width (GtkTextView *text_view)
2105 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
2107 gtk_text_view_ensure_layout (text_view);
2109 gtk_text_layout_set_screen_width (text_view->priv->layout,
2110 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
2114 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
2118 if (text_view->priv->layout == NULL)
2121 gtk_text_view_get_cursor_location (text_view, &area);
2123 area.x -= text_view->priv->xoffset;
2124 area.y -= text_view->priv->yoffset;
2126 /* Width returned by Pango indicates direction of cursor,
2127 * by it's sign more than the size of cursor.
2131 gtk_im_context_set_cursor_location (text_view->priv->im_context, &area);
2135 do_update_im_spot_location (gpointer text_view)
2137 GtkTextViewPrivate *priv;
2139 priv = GTK_TEXT_VIEW (text_view)->priv;
2140 priv->im_spot_idle = 0;
2142 gtk_text_view_update_im_spot_location (text_view);
2147 queue_update_im_spot_location (GtkTextView *text_view)
2149 GtkTextViewPrivate *priv;
2151 priv = text_view->priv;
2153 /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
2154 * so we don't wait until the entire buffer has been validated. */
2155 if (!priv->im_spot_idle)
2156 priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
2157 do_update_im_spot_location,
2163 flush_update_im_spot_location (GtkTextView *text_view)
2165 GtkTextViewPrivate *priv;
2167 priv = text_view->priv;
2169 if (priv->im_spot_idle)
2171 g_source_remove (priv->im_spot_idle);
2172 priv->im_spot_idle = 0;
2173 gtk_text_view_update_im_spot_location (text_view);
2178 * gtk_text_view_scroll_to_mark:
2179 * @text_view: a #GtkTextView
2180 * @mark: a #GtkTextMark
2181 * @within_margin: margin as a [0.0,0.5) fraction of screen size
2182 * @use_align: whether to use alignment arguments (if %FALSE, just
2183 * get the mark onscreen)
2184 * @xalign: horizontal alignment of mark within visible area
2185 * @yalign: vertical alignment of mark within visible area
2187 * Scrolls @text_view so that @mark is on the screen in the position
2188 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
2189 * left or top, 1.0 indicates right or bottom, 0.5 means center.
2190 * If @use_align is %FALSE, the text scrolls the minimal distance to
2191 * get the mark onscreen, possibly not scrolling at all. The effective
2192 * screen for purposes of this function is reduced by a margin of size
2196 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
2198 gdouble within_margin,
2203 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2204 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2205 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
2206 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
2207 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
2209 /* We need to verify that the buffer contains the mark, otherwise this
2210 * can lead to data structure corruption later on.
2212 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2214 gtk_text_view_queue_scroll (text_view, mark,
2220 /* If no validation is pending, we need to go ahead and force an
2223 if (text_view->priv->layout &&
2224 gtk_text_layout_is_valid (text_view->priv->layout))
2225 gtk_text_view_flush_scroll (text_view);
2229 * gtk_text_view_scroll_mark_onscreen:
2230 * @text_view: a #GtkTextView
2231 * @mark: a mark in the buffer for @text_view
2233 * Scrolls @text_view the minimum distance such that @mark is contained
2234 * within the visible area of the widget.
2237 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
2240 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2241 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2243 /* We need to verify that the buffer contains the mark, otherwise this
2244 * can lead to data structure corruption later on.
2246 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2248 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
2252 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
2254 GdkRectangle visible_rect;
2255 gtk_text_view_get_visible_rect (text_view, &visible_rect);
2257 return gtk_text_layout_clamp_iter_to_vrange (text_view->priv->layout, iter,
2259 visible_rect.y + visible_rect.height);
2263 * gtk_text_view_move_mark_onscreen:
2264 * @text_view: a #GtkTextView
2265 * @mark: a #GtkTextMark
2267 * Moves a mark within the buffer so that it's
2268 * located within the currently-visible text area.
2270 * Return value: %TRUE if the mark moved (wasn't already onscreen)
2273 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
2278 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2279 g_return_val_if_fail (mark != NULL, FALSE);
2281 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2283 if (clamp_iter_onscreen (text_view, &iter))
2285 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
2293 * gtk_text_view_get_visible_rect:
2294 * @text_view: a #GtkTextView
2295 * @visible_rect: rectangle to fill
2297 * Fills @visible_rect with the currently-visible
2298 * region of the buffer, in buffer coordinates. Convert to window coordinates
2299 * with gtk_text_view_buffer_to_window_coords().
2302 gtk_text_view_get_visible_rect (GtkTextView *text_view,
2303 GdkRectangle *visible_rect)
2307 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2309 widget = GTK_WIDGET (text_view);
2313 visible_rect->x = text_view->priv->xoffset;
2314 visible_rect->y = text_view->priv->yoffset;
2315 visible_rect->width = SCREEN_WIDTH (widget);
2316 visible_rect->height = SCREEN_HEIGHT (widget);
2318 DV(g_print(" visible rect: %d,%d %d x %d\n",
2321 visible_rect->width,
2322 visible_rect->height));
2327 * gtk_text_view_set_wrap_mode:
2328 * @text_view: a #GtkTextView
2329 * @wrap_mode: a #GtkWrapMode
2331 * Sets the line wrapping for the view.
2334 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
2335 GtkWrapMode wrap_mode)
2337 GtkTextViewPrivate *priv;
2339 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2341 priv = text_view->priv;
2343 if (priv->wrap_mode != wrap_mode)
2345 priv->wrap_mode = wrap_mode;
2349 priv->layout->default_style->wrap_mode = wrap_mode;
2350 gtk_text_layout_default_style_changed (priv->layout);
2354 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2358 * gtk_text_view_get_wrap_mode:
2359 * @text_view: a #GtkTextView
2361 * Gets the line wrapping for the view.
2363 * Return value: the line wrap setting
2366 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2368 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2370 return text_view->priv->wrap_mode;
2374 * gtk_text_view_set_editable:
2375 * @text_view: a #GtkTextView
2376 * @setting: whether it's editable
2378 * Sets the default editability of the #GtkTextView. You can override
2379 * this default setting with tags in the buffer, using the "editable"
2380 * attribute of tags.
2383 gtk_text_view_set_editable (GtkTextView *text_view,
2386 GtkTextViewPrivate *priv;
2388 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2390 priv = text_view->priv;
2391 setting = setting != FALSE;
2393 if (priv->editable != setting)
2397 gtk_text_view_reset_im_context(text_view);
2398 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2399 gtk_im_context_focus_out (priv->im_context);
2402 priv->editable = setting;
2404 if (setting && gtk_widget_has_focus (GTK_WIDGET (text_view)))
2405 gtk_im_context_focus_in (priv->im_context);
2409 gtk_text_layout_set_overwrite_mode (priv->layout,
2410 priv->overwrite_mode && priv->editable);
2411 priv->layout->default_style->editable = priv->editable;
2412 gtk_text_layout_default_style_changed (priv->layout);
2415 g_object_notify (G_OBJECT (text_view), "editable");
2420 * gtk_text_view_get_editable:
2421 * @text_view: a #GtkTextView
2423 * Returns the default editability of the #GtkTextView. Tags in the
2424 * buffer may override this setting for some ranges of text.
2426 * Return value: whether text is editable by default
2429 gtk_text_view_get_editable (GtkTextView *text_view)
2431 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2433 return text_view->priv->editable;
2437 * gtk_text_view_set_pixels_above_lines:
2438 * @text_view: a #GtkTextView
2439 * @pixels_above_lines: pixels above paragraphs
2441 * Sets the default number of blank pixels above paragraphs in @text_view.
2442 * Tags in the buffer for @text_view may override the defaults.
2445 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2446 gint pixels_above_lines)
2448 GtkTextViewPrivate *priv;
2450 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2452 priv = text_view->priv;
2454 if (priv->pixels_above_lines != pixels_above_lines)
2456 priv->pixels_above_lines = pixels_above_lines;
2460 priv->layout->default_style->pixels_above_lines = pixels_above_lines;
2461 gtk_text_layout_default_style_changed (priv->layout);
2464 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2469 * gtk_text_view_get_pixels_above_lines:
2470 * @text_view: a #GtkTextView
2472 * Gets the default number of pixels to put above paragraphs.
2474 * Return value: default number of pixels above paragraphs
2477 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2479 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2481 return text_view->priv->pixels_above_lines;
2485 * gtk_text_view_set_pixels_below_lines:
2486 * @text_view: a #GtkTextView
2487 * @pixels_below_lines: pixels below paragraphs
2489 * Sets the default number of pixels of blank space
2490 * to put below paragraphs in @text_view. May be overridden
2491 * by tags applied to @text_view's buffer.
2494 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2495 gint pixels_below_lines)
2497 GtkTextViewPrivate *priv;
2499 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2501 priv = text_view->priv;
2503 if (priv->pixels_below_lines != pixels_below_lines)
2505 priv->pixels_below_lines = pixels_below_lines;
2509 priv->layout->default_style->pixels_below_lines = pixels_below_lines;
2510 gtk_text_layout_default_style_changed (priv->layout);
2513 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2518 * gtk_text_view_get_pixels_below_lines:
2519 * @text_view: a #GtkTextView
2521 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2523 * Return value: default number of blank pixels below paragraphs
2526 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2528 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2530 return text_view->priv->pixels_below_lines;
2534 * gtk_text_view_set_pixels_inside_wrap:
2535 * @text_view: a #GtkTextView
2536 * @pixels_inside_wrap: default number of pixels between wrapped lines
2538 * Sets the default number of pixels of blank space to leave between
2539 * display/wrapped lines within a paragraph. May be overridden by
2540 * tags in @text_view's buffer.
2543 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2544 gint pixels_inside_wrap)
2546 GtkTextViewPrivate *priv;
2548 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2550 priv = text_view->priv;
2552 if (priv->pixels_inside_wrap != pixels_inside_wrap)
2554 priv->pixels_inside_wrap = pixels_inside_wrap;
2558 priv->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2559 gtk_text_layout_default_style_changed (priv->layout);
2562 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2567 * gtk_text_view_get_pixels_inside_wrap:
2568 * @text_view: a #GtkTextView
2570 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2572 * Return value: default number of pixels of blank space between wrapped lines
2575 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2577 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2579 return text_view->priv->pixels_inside_wrap;
2583 * gtk_text_view_set_justification:
2584 * @text_view: a #GtkTextView
2585 * @justification: justification
2587 * Sets the default justification of text in @text_view.
2588 * Tags in the view's buffer may override the default.
2592 gtk_text_view_set_justification (GtkTextView *text_view,
2593 GtkJustification justification)
2595 GtkTextViewPrivate *priv;
2597 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2599 priv = text_view->priv;
2601 if (priv->justify != justification)
2603 priv->justify = justification;
2607 priv->layout->default_style->justification = justification;
2608 gtk_text_layout_default_style_changed (priv->layout);
2611 g_object_notify (G_OBJECT (text_view), "justification");
2616 * gtk_text_view_get_justification:
2617 * @text_view: a #GtkTextView
2619 * Gets the default justification of paragraphs in @text_view.
2620 * Tags in the buffer may override the default.
2622 * Return value: default justification
2625 gtk_text_view_get_justification (GtkTextView *text_view)
2627 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2629 return text_view->priv->justify;
2633 * gtk_text_view_set_left_margin:
2634 * @text_view: a #GtkTextView
2635 * @left_margin: left margin in pixels
2637 * Sets the default left margin for text in @text_view.
2638 * Tags in the buffer may override the default.
2641 gtk_text_view_set_left_margin (GtkTextView *text_view,
2644 GtkTextViewPrivate *priv;
2646 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2648 priv = text_view->priv;
2650 if (priv->left_margin != left_margin)
2652 priv->left_margin = left_margin;
2656 priv->layout->default_style->left_margin = left_margin;
2657 gtk_text_layout_default_style_changed (priv->layout);
2660 g_object_notify (G_OBJECT (text_view), "left-margin");
2665 * gtk_text_view_get_left_margin:
2666 * @text_view: a #GtkTextView
2668 * Gets the default left margin size of paragraphs in the @text_view.
2669 * Tags in the buffer may override the default.
2671 * Return value: left margin in pixels
2674 gtk_text_view_get_left_margin (GtkTextView *text_view)
2676 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2678 return text_view->priv->left_margin;
2682 * gtk_text_view_set_right_margin:
2683 * @text_view: a #GtkTextView
2684 * @right_margin: right margin in pixels
2686 * Sets the default right margin for text in the text view.
2687 * Tags in the buffer may override the default.
2690 gtk_text_view_set_right_margin (GtkTextView *text_view,
2693 GtkTextViewPrivate *priv = text_view->priv;
2695 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2697 if (priv->right_margin != right_margin)
2699 priv->right_margin = right_margin;
2703 priv->layout->default_style->right_margin = right_margin;
2704 gtk_text_layout_default_style_changed (priv->layout);
2707 g_object_notify (G_OBJECT (text_view), "right-margin");
2712 * gtk_text_view_get_right_margin:
2713 * @text_view: a #GtkTextView
2715 * Gets the default right margin for text in @text_view. Tags
2716 * in the buffer may override the default.
2718 * Return value: right margin in pixels
2721 gtk_text_view_get_right_margin (GtkTextView *text_view)
2723 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2725 return text_view->priv->right_margin;
2729 * gtk_text_view_set_indent:
2730 * @text_view: a #GtkTextView
2731 * @indent: indentation in pixels
2733 * Sets the default indentation for paragraphs in @text_view.
2734 * Tags in the buffer may override the default.
2737 gtk_text_view_set_indent (GtkTextView *text_view,
2740 GtkTextViewPrivate *priv;
2742 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2744 priv = text_view->priv;
2746 if (priv->indent != indent)
2748 priv->indent = indent;
2752 priv->layout->default_style->indent = indent;
2753 gtk_text_layout_default_style_changed (priv->layout);
2756 g_object_notify (G_OBJECT (text_view), "indent");
2761 * gtk_text_view_get_indent:
2762 * @text_view: a #GtkTextView
2764 * Gets the default indentation of paragraphs in @text_view.
2765 * Tags in the view's buffer may override the default.
2766 * The indentation may be negative.
2768 * Return value: number of pixels of indentation
2771 gtk_text_view_get_indent (GtkTextView *text_view)
2773 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2775 return text_view->priv->indent;
2779 * gtk_text_view_set_tabs:
2780 * @text_view: a #GtkTextView
2781 * @tabs: tabs as a #PangoTabArray
2783 * Sets the default tab stops for paragraphs in @text_view.
2784 * Tags in the buffer may override the default.
2787 gtk_text_view_set_tabs (GtkTextView *text_view,
2788 PangoTabArray *tabs)
2790 GtkTextViewPrivate *priv;
2792 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2794 priv = text_view->priv;
2797 pango_tab_array_free (priv->tabs);
2799 priv->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2803 /* some unkosher futzing in internal struct details... */
2804 if (priv->layout->default_style->tabs)
2805 pango_tab_array_free (priv->layout->default_style->tabs);
2807 priv->layout->default_style->tabs =
2808 priv->tabs ? pango_tab_array_copy (priv->tabs) : NULL;
2810 gtk_text_layout_default_style_changed (priv->layout);
2813 g_object_notify (G_OBJECT (text_view), "tabs");
2817 * gtk_text_view_get_tabs:
2818 * @text_view: a #GtkTextView
2820 * Gets the default tabs for @text_view. Tags in the buffer may
2821 * override the defaults. The returned array will be %NULL if
2822 * "standard" (8-space) tabs are used. Free the return value
2823 * with pango_tab_array_free().
2825 * Return value: copy of default tab array, or %NULL if "standard"
2826 * tabs are used; must be freed with pango_tab_array_free().
2829 gtk_text_view_get_tabs (GtkTextView *text_view)
2831 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2833 return text_view->priv->tabs ? pango_tab_array_copy (text_view->priv->tabs) : NULL;
2837 gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
2839 gtk_text_view_set_cursor_visible (text_view, !text_view->priv->cursor_visible);
2843 * gtk_text_view_set_cursor_visible:
2844 * @text_view: a #GtkTextView
2845 * @setting: whether to show the insertion cursor
2847 * Toggles whether the insertion point is displayed. A buffer with no editable
2848 * text probably shouldn't have a visible cursor, so you may want to turn
2852 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2855 GtkTextViewPrivate *priv;
2857 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2859 priv = text_view->priv;
2860 setting = (setting != FALSE);
2862 if (priv->cursor_visible != setting)
2864 priv->cursor_visible = setting;
2866 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2870 gtk_text_layout_set_cursor_visible (priv->layout, setting);
2871 gtk_text_view_check_cursor_blink (text_view);
2875 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2880 * gtk_text_view_get_cursor_visible:
2881 * @text_view: a #GtkTextView
2883 * Find out whether the cursor is being displayed.
2885 * Return value: whether the insertion mark is visible
2888 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2890 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2892 return text_view->priv->cursor_visible;
2897 * gtk_text_view_place_cursor_onscreen:
2898 * @text_view: a #GtkTextView
2900 * Moves the cursor to the currently visible region of the
2901 * buffer, it it isn't there already.
2903 * Return value: %TRUE if the cursor had to be moved.
2906 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2910 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2912 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2913 gtk_text_buffer_get_insert (get_buffer (text_view)));
2915 if (clamp_iter_onscreen (text_view, &insert))
2917 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2925 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2927 GtkTextViewPrivate *priv = text_view->priv;
2929 if (priv->first_validate_idle != 0)
2931 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2932 g_source_remove (priv->first_validate_idle);
2933 priv->first_validate_idle = 0;
2936 if (priv->incremental_validate_idle != 0)
2938 g_source_remove (priv->incremental_validate_idle);
2939 priv->incremental_validate_idle = 0;
2944 gtk_text_view_destroy (GtkWidget *widget)
2946 GtkTextView *text_view;
2947 GtkTextViewPrivate *priv;
2949 text_view = GTK_TEXT_VIEW (widget);
2950 priv = text_view->priv;
2952 gtk_text_view_remove_validate_idles (text_view);
2953 gtk_text_view_set_buffer (text_view, NULL);
2954 gtk_text_view_destroy_layout (text_view);
2956 if (text_view->priv->scroll_timeout)
2958 g_source_remove (text_view->priv->scroll_timeout);
2959 text_view->priv->scroll_timeout = 0;
2962 if (priv->im_spot_idle)
2964 g_source_remove (priv->im_spot_idle);
2965 priv->im_spot_idle = 0;
2968 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->destroy (widget);
2972 gtk_text_view_finalize (GObject *object)
2974 GtkTextView *text_view;
2975 GtkTextViewPrivate *priv;
2977 text_view = GTK_TEXT_VIEW (object);
2978 priv = text_view->priv;
2980 g_assert (priv->buffer == NULL);
2982 gtk_text_view_destroy_layout (text_view);
2983 gtk_text_view_set_buffer (text_view, NULL);
2985 cancel_pending_scroll (text_view);
2988 pango_tab_array_free (priv->tabs);
2990 if (priv->hadjustment)
2991 g_object_unref (priv->hadjustment);
2992 if (priv->vadjustment)
2993 g_object_unref (priv->vadjustment);
2995 text_window_free (priv->text_window);
2997 if (priv->left_window)
2998 text_window_free (priv->left_window);
3000 if (priv->top_window)
3001 text_window_free (priv->top_window);
3003 if (priv->right_window)
3004 text_window_free (priv->right_window);
3006 if (priv->bottom_window)
3007 text_window_free (priv->bottom_window);
3009 g_object_unref (priv->im_context);
3011 g_free (priv->im_module);
3013 G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize (object);
3017 gtk_text_view_set_property (GObject *object,
3019 const GValue *value,
3022 GtkTextView *text_view;
3023 GtkTextViewPrivate *priv;
3025 text_view = GTK_TEXT_VIEW (object);
3026 priv = text_view->priv;
3030 case PROP_PIXELS_ABOVE_LINES:
3031 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
3034 case PROP_PIXELS_BELOW_LINES:
3035 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
3038 case PROP_PIXELS_INSIDE_WRAP:
3039 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
3043 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
3046 case PROP_WRAP_MODE:
3047 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
3050 case PROP_JUSTIFICATION:
3051 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
3054 case PROP_LEFT_MARGIN:
3055 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
3058 case PROP_RIGHT_MARGIN:
3059 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
3063 gtk_text_view_set_indent (text_view, g_value_get_int (value));
3067 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
3070 case PROP_CURSOR_VISIBLE:
3071 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
3074 case PROP_OVERWRITE:
3075 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
3079 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
3082 case PROP_ACCEPTS_TAB:
3083 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
3086 case PROP_IM_MODULE:
3087 g_free (priv->im_module);
3088 priv->im_module = g_value_dup_string (value);
3089 if (GTK_IS_IM_MULTICONTEXT (priv->im_context))
3090 gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (priv->im_context), priv->im_module);
3093 case PROP_HADJUSTMENT:
3094 gtk_text_view_set_hadjustment (text_view, g_value_get_object (value));
3097 case PROP_VADJUSTMENT:
3098 gtk_text_view_set_vadjustment (text_view, g_value_get_object (value));
3101 case PROP_HSCROLL_POLICY:
3102 priv->hscroll_policy = g_value_get_enum (value);
3103 gtk_widget_queue_resize (GTK_WIDGET (text_view));
3106 case PROP_VSCROLL_POLICY:
3107 priv->vscroll_policy = g_value_get_enum (value);
3108 gtk_widget_queue_resize (GTK_WIDGET (text_view));
3112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3118 gtk_text_view_get_property (GObject *object,
3123 GtkTextView *text_view;
3124 GtkTextViewPrivate *priv;
3126 text_view = GTK_TEXT_VIEW (object);
3127 priv = text_view->priv;
3131 case PROP_PIXELS_ABOVE_LINES:
3132 g_value_set_int (value, priv->pixels_above_lines);
3135 case PROP_PIXELS_BELOW_LINES:
3136 g_value_set_int (value, priv->pixels_below_lines);
3139 case PROP_PIXELS_INSIDE_WRAP:
3140 g_value_set_int (value, priv->pixels_inside_wrap);
3144 g_value_set_boolean (value, priv->editable);
3147 case PROP_WRAP_MODE:
3148 g_value_set_enum (value, priv->wrap_mode);
3151 case PROP_JUSTIFICATION:
3152 g_value_set_enum (value, priv->justify);
3155 case PROP_LEFT_MARGIN:
3156 g_value_set_int (value, priv->left_margin);
3159 case PROP_RIGHT_MARGIN:
3160 g_value_set_int (value, priv->right_margin);
3164 g_value_set_int (value, priv->indent);
3168 g_value_set_boxed (value, priv->tabs);
3171 case PROP_CURSOR_VISIBLE:
3172 g_value_set_boolean (value, priv->cursor_visible);
3176 g_value_set_object (value, get_buffer (text_view));
3179 case PROP_OVERWRITE:
3180 g_value_set_boolean (value, priv->overwrite_mode);
3183 case PROP_ACCEPTS_TAB:
3184 g_value_set_boolean (value, priv->accepts_tab);
3187 case PROP_IM_MODULE:
3188 g_value_set_string (value, priv->im_module);
3191 case PROP_HADJUSTMENT:
3192 g_value_set_object (value, priv->hadjustment);
3195 case PROP_VADJUSTMENT:
3196 g_value_set_object (value, priv->vadjustment);
3199 case PROP_HSCROLL_POLICY:
3200 g_value_set_enum (value, priv->hscroll_policy);
3203 case PROP_VSCROLL_POLICY:
3204 g_value_set_enum (value, priv->vscroll_policy);
3208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3214 gtk_text_view_size_request (GtkWidget *widget,
3215 GtkRequisition *requisition)
3217 GtkTextView *text_view;
3218 GtkTextViewPrivate *priv;
3220 gint focus_edge_width;
3223 gboolean interior_focus;
3225 text_view = GTK_TEXT_VIEW (widget);
3226 priv = text_view->priv;
3228 gtk_widget_style_get (widget,
3229 "interior-focus", &interior_focus,
3230 "focus-line-width", &focus_width,
3234 focus_edge_width = 0;
3236 focus_edge_width = focus_width;
3240 priv->text_window->requisition.width = priv->layout->width;
3241 priv->text_window->requisition.height = priv->layout->height;
3245 priv->text_window->requisition.width = 0;
3246 priv->text_window->requisition.height = 0;
3249 requisition->width = priv->text_window->requisition.width + focus_edge_width * 2;
3250 requisition->height = priv->text_window->requisition.height + focus_edge_width * 2;
3252 if (priv->left_window)
3253 requisition->width += priv->left_window->requisition.width;
3255 if (priv->right_window)
3256 requisition->width += priv->right_window->requisition.width;
3258 if (priv->top_window)
3259 requisition->height += priv->top_window->requisition.height;
3261 if (priv->bottom_window)
3262 requisition->height += priv->bottom_window->requisition.height;
3264 border_width = gtk_container_get_border_width (GTK_CONTAINER (text_view));
3265 requisition->width += border_width * 2;
3266 requisition->height += border_width * 2;
3268 tmp_list = priv->children;
3269 while (tmp_list != NULL)
3271 GtkTextViewChild *child = tmp_list->data;
3275 GtkRequisition child_req;
3276 GtkRequisition old_req;
3278 gtk_widget_get_preferred_size (child->widget, &old_req, NULL);
3280 gtk_widget_get_preferred_size (child->widget, &child_req, NULL);
3282 /* Invalidate layout lines if required */
3284 (old_req.width != child_req.width ||
3285 old_req.height != child_req.height))
3286 gtk_text_child_anchor_queue_resize (child->anchor,
3291 GtkRequisition child_req;
3293 gtk_widget_get_preferred_size (child->widget,
3297 tmp_list = g_slist_next (tmp_list);
3300 /* Cache the requested size of the text view so we can
3301 * compare it in the changed_handler() */
3302 priv->cached_size_request = *requisition;
3306 gtk_text_view_get_preferred_width (GtkWidget *widget,
3310 GtkRequisition requisition;
3312 gtk_text_view_size_request (widget, &requisition);
3314 *minimum = *natural = requisition.width;
3318 gtk_text_view_get_preferred_height (GtkWidget *widget,
3322 GtkRequisition requisition;
3324 gtk_text_view_size_request (widget, &requisition);
3326 *minimum = *natural = requisition.height;
3331 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
3332 GtkTextViewChild *vc,
3333 GtkAllocation *allocation)
3339 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3343 gtk_text_layout_get_line_yrange (text_view->priv->layout, &iter,
3346 buffer_y += vc->from_top_of_line;
3348 allocation->x = vc->from_left_of_buffer - text_view->priv->xoffset;
3349 allocation->y = buffer_y - text_view->priv->yoffset;
3351 gtk_widget_get_preferred_size (vc->widget, &req, NULL);
3352 allocation->width = req.width;
3353 allocation->height = req.height;
3357 gtk_text_view_update_child_allocation (GtkTextView *text_view,
3358 GtkTextViewChild *vc)
3360 GtkAllocation allocation;
3362 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
3364 gtk_widget_size_allocate (vc->widget, &allocation);
3367 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
3369 vc->widget->allocation.x,
3370 vc->widget->allocation.y,
3371 text_view->priv->yoffset);
3376 gtk_text_view_child_allocated (GtkTextLayout *layout,
3382 GtkTextViewChild *vc = NULL;
3383 GtkTextView *text_view = data;
3385 /* x,y is the position of the child from the top of the line, and
3386 * from the left of the buffer. We have to translate that into text
3387 * window coordinates, then size_allocate the child.
3390 vc = g_object_get_data (G_OBJECT (child),
3391 "gtk-text-view-child");
3393 g_assert (vc != NULL);
3395 DV (g_print ("child allocated at %d,%d\n", x, y));
3397 vc->from_left_of_buffer = x;
3398 vc->from_top_of_line = y;
3400 gtk_text_view_update_child_allocation (text_view, vc);
3404 gtk_text_view_allocate_children (GtkTextView *text_view)
3408 DV(g_print(G_STRLOC"\n"));
3410 tmp_list = text_view->priv->children;
3411 while (tmp_list != NULL)
3413 GtkTextViewChild *child = tmp_list->data;
3415 g_assert (child != NULL);
3419 /* We need to force-validate the regions containing
3422 GtkTextIter child_loc;
3423 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3427 /* Since anchored children are only ever allocated from
3428 * gtk_text_layout_get_line_display() we have to make sure
3429 * that the display line caching in the layout doesn't
3430 * get in the way. Invalidating the layout around the anchor
3433 if (_gtk_widget_get_alloc_needed (child->widget))
3435 GtkTextIter end = child_loc;
3436 gtk_text_iter_forward_char (&end);
3437 gtk_text_layout_invalidate (text_view->priv->layout, &child_loc, &end);
3440 gtk_text_layout_validate_yrange (text_view->priv->layout,
3446 GtkAllocation allocation;
3447 GtkRequisition child_req;
3449 allocation.x = child->x;
3450 allocation.y = child->y;
3452 gtk_widget_get_preferred_size (child->widget, &child_req, NULL);
3454 allocation.width = child_req.width;
3455 allocation.height = child_req.height;
3457 gtk_widget_size_allocate (child->widget, &allocation);
3460 tmp_list = g_slist_next (tmp_list);
3465 gtk_text_view_size_allocate (GtkWidget *widget,
3466 GtkAllocation *allocation)
3468 GtkAllocation widget_allocation;
3469 GtkTextView *text_view;
3470 GtkTextViewPrivate *priv;
3472 GdkRectangle text_rect;
3473 GdkRectangle left_rect;
3474 GdkRectangle right_rect;
3475 GdkRectangle top_rect;
3476 GdkRectangle bottom_rect;
3477 gint focus_edge_width;
3480 gboolean interior_focus;
3481 gboolean size_changed;
3483 text_view = GTK_TEXT_VIEW (widget);
3484 priv = text_view->priv;
3486 DV(g_print(G_STRLOC"\n"));
3488 gtk_widget_get_allocation (widget, &widget_allocation);
3490 widget_allocation.width != allocation->width ||
3491 widget_allocation.height != allocation->height;
3493 border_width = gtk_container_get_border_width (GTK_CONTAINER (text_view));
3495 gtk_widget_set_allocation (widget, allocation);
3497 if (gtk_widget_get_realized (widget))
3499 gdk_window_move_resize (gtk_widget_get_window (widget),
3500 allocation->x, allocation->y,
3501 allocation->width, allocation->height);
3504 /* distribute width/height among child windows. Ensure all
3505 * windows get at least a 1x1 allocation.
3508 gtk_widget_style_get (widget,
3509 "interior-focus", &interior_focus,
3510 "focus-line-width", &focus_width,
3514 focus_edge_width = 0;
3516 focus_edge_width = focus_width;
3518 width = allocation->width - focus_edge_width * 2 - border_width * 2;
3520 if (priv->left_window)
3521 left_rect.width = priv->left_window->requisition.width;
3523 left_rect.width = 0;
3525 width -= left_rect.width;
3527 if (priv->right_window)
3528 right_rect.width = priv->right_window->requisition.width;
3530 right_rect.width = 0;
3532 width -= right_rect.width;
3534 text_rect.width = MAX (1, width);
3536 top_rect.width = text_rect.width;
3537 bottom_rect.width = text_rect.width;
3540 height = allocation->height - focus_edge_width * 2 - border_width * 2;
3542 if (priv->top_window)
3543 top_rect.height = priv->top_window->requisition.height;
3545 top_rect.height = 0;
3547 height -= top_rect.height;
3549 if (priv->bottom_window)
3550 bottom_rect.height = priv->bottom_window->requisition.height;
3552 bottom_rect.height = 0;
3554 height -= bottom_rect.height;
3556 text_rect.height = MAX (1, height);
3558 left_rect.height = text_rect.height;
3559 right_rect.height = text_rect.height;
3562 left_rect.x = focus_edge_width + border_width;
3563 top_rect.y = focus_edge_width + border_width;
3565 text_rect.x = left_rect.x + left_rect.width;
3566 text_rect.y = top_rect.y + top_rect.height;
3568 left_rect.y = text_rect.y;
3569 right_rect.y = text_rect.y;
3571 top_rect.x = text_rect.x;
3572 bottom_rect.x = text_rect.x;
3574 right_rect.x = text_rect.x + text_rect.width;
3575 bottom_rect.y = text_rect.y + text_rect.height;
3577 text_window_size_allocate (priv->text_window,
3580 if (priv->left_window)
3581 text_window_size_allocate (priv->left_window,
3584 if (priv->right_window)
3585 text_window_size_allocate (priv->right_window,
3588 if (priv->top_window)
3589 text_window_size_allocate (priv->top_window,
3592 if (priv->bottom_window)
3593 text_window_size_allocate (priv->bottom_window,
3596 gtk_text_view_update_layout_width (text_view);
3598 /* Note that this will do some layout validation */
3599 gtk_text_view_allocate_children (text_view);
3601 /* Update adjustments */
3602 gtk_text_view_set_hadjustment_values (text_view);
3603 gtk_text_view_set_vadjustment_values (text_view);
3605 /* The GTK resize loop processes all the pending exposes right
3606 * after doing the resize stuff, so the idle sizer won't have a
3607 * chance to run. So we do the work here.
3609 gtk_text_view_flush_first_validate (text_view);
3611 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3614 if (size_changed && gtk_widget_get_realized (widget))
3615 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
3619 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3622 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3623 text_view->priv->first_para_mark);
3627 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3630 GtkTextViewPrivate *priv;
3632 widget = GTK_WIDGET (text_view);
3633 priv = text_view->priv;
3635 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3637 if (SCREEN_HEIGHT (widget) > 0)
3639 GtkTextIter first_para;
3641 /* Be sure we've validated the stuff onscreen; if we
3642 * scrolled, these calls won't have any effect, because
3643 * they were called in the recursive validate_onscreen
3645 gtk_text_view_get_first_para_iter (text_view, &first_para);
3647 gtk_text_layout_validate_yrange (priv->layout,
3650 priv->first_para_pixels +
3651 SCREEN_HEIGHT (widget));
3654 priv->onscreen_validated = TRUE;
3656 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3658 /* This can have the odd side effect of triggering a scroll, which should
3659 * flip "onscreen_validated" back to FALSE, but should also get us
3660 * back into this function to turn it on again.
3662 gtk_text_view_update_adjustments (text_view);
3664 g_assert (priv->onscreen_validated);
3668 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3670 GtkTextViewPrivate *priv = text_view->priv;
3672 if (priv->first_validate_idle == 0)
3675 /* Do this first, which means that if an "invalidate"
3676 * occurs during any of this process, a new first_validate_callback
3677 * will be installed, and we'll start again.
3679 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3680 g_source_remove (priv->first_validate_idle);
3681 priv->first_validate_idle = 0;
3683 /* be sure we have up-to-date screen size set on the
3686 gtk_text_view_update_layout_width (text_view);
3688 /* Bail out if we invalidated stuff; scrolling right away will just
3689 * confuse the issue.
3691 if (priv->first_validate_idle != 0)
3693 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3697 /* scroll to any marks, if that's pending. This can jump us to
3698 * the validation codepath used for scrolling onscreen, if so we
3699 * bail out. It won't jump if already in that codepath since
3700 * value_changed is not recursive, so also validate if
3703 if (!gtk_text_view_flush_scroll (text_view) ||
3704 !priv->onscreen_validated)
3705 gtk_text_view_validate_onscreen (text_view);
3707 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3709 g_assert (priv->onscreen_validated);
3714 first_validate_callback (gpointer data)
3716 GtkTextView *text_view = data;
3718 /* Note that some of this code is duplicated at the end of size_allocate,
3719 * keep in sync with that.
3722 DV(g_print(G_STRLOC"\n"));
3724 gtk_text_view_flush_first_validate (text_view);
3730 incremental_validate_callback (gpointer data)
3732 GtkTextView *text_view = data;
3733 gboolean result = TRUE;
3735 DV(g_print(G_STRLOC"\n"));
3737 gtk_text_layout_validate (text_view->priv->layout, 2000);
3739 gtk_text_view_update_adjustments (text_view);
3741 if (gtk_text_layout_is_valid (text_view->priv->layout))
3743 text_view->priv->incremental_validate_idle = 0;
3751 gtk_text_view_invalidate (GtkTextView *text_view)
3753 GtkTextViewPrivate *priv = text_view->priv;
3755 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3756 priv->onscreen_validated));
3758 priv->onscreen_validated = FALSE;
3760 /* We'll invalidate when the layout is created */
3761 if (priv->layout == NULL)
3764 if (!priv->first_validate_idle)
3766 priv->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3767 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3768 priv->first_validate_idle));
3771 if (!priv->incremental_validate_idle)
3773 priv->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3774 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3775 priv->incremental_validate_idle));
3780 invalidated_handler (GtkTextLayout *layout,
3783 GtkTextView *text_view;
3785 text_view = GTK_TEXT_VIEW (data);
3787 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3788 gtk_text_view_invalidate (text_view);
3792 changed_handler (GtkTextLayout *layout,
3798 GtkTextView *text_view;
3799 GtkTextViewPrivate *priv;
3801 GdkRectangle visible_rect;
3802 GdkRectangle redraw_rect;
3804 text_view = GTK_TEXT_VIEW (data);
3805 priv = text_view->priv;
3806 widget = GTK_WIDGET (data);
3808 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3810 if (gtk_widget_get_realized (widget))
3812 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3814 redraw_rect.x = visible_rect.x;
3815 redraw_rect.width = visible_rect.width;
3816 redraw_rect.y = start_y;
3818 if (old_height == new_height)
3819 redraw_rect.height = old_height;
3820 else if (start_y + old_height > visible_rect.y)
3821 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3823 redraw_rect.height = 0;
3825 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3827 /* text_window_invalidate_rect() takes buffer coordinates */
3828 text_window_invalidate_rect (priv->text_window,
3831 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3835 redraw_rect.height));
3837 if (priv->left_window)
3838 text_window_invalidate_rect (priv->left_window,
3840 if (priv->right_window)
3841 text_window_invalidate_rect (priv->right_window,
3843 if (priv->top_window)
3844 text_window_invalidate_rect (priv->top_window,
3846 if (priv->bottom_window)
3847 text_window_invalidate_rect (priv->bottom_window,
3850 queue_update_im_spot_location (text_view);
3854 if (old_height != new_height)
3857 int new_first_para_top;
3858 int old_first_para_top;
3861 /* If the bottom of the old area was above the top of the
3862 * screen, we need to scroll to keep the current top of the
3863 * screen in place. Remember that first_para_pixels is the
3864 * position of the top of the screen in coordinates relative to
3865 * the first paragraph onscreen.
3867 * In short we are adding the height delta of the portion of the
3868 * changed region above first_para_mark to priv->yoffset.
3870 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3871 priv->first_para_mark);
3873 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3875 old_first_para_top = priv->yoffset - priv->first_para_pixels;
3877 if (new_first_para_top != old_first_para_top)
3879 priv->yoffset += new_first_para_top - old_first_para_top;
3881 gtk_adjustment_set_value (text_view->priv->vadjustment, priv->yoffset);
3884 /* FIXME be smarter about which anchored widgets we update */
3886 tmp_list = priv->children;
3887 while (tmp_list != NULL)
3889 GtkTextViewChild *child = tmp_list->data;
3892 gtk_text_view_update_child_allocation (text_view, child);
3894 tmp_list = g_slist_next (tmp_list);
3899 GtkRequisition old_req = priv->cached_size_request;
3900 GtkRequisition new_req;
3902 /* Use this instead of gtk_widget_size_request wrapper
3903 * to avoid the optimization which just returns widget->requisition
3904 * if a resize hasn't been queued.
3906 gtk_text_view_size_request (widget, &new_req);
3908 if (old_req.width != new_req.width ||
3909 old_req.height != new_req.height)
3911 gtk_widget_queue_resize_no_redraw (widget);
3917 gtk_text_view_realize (GtkWidget *widget)
3919 GtkAllocation allocation;
3920 GtkTextView *text_view;
3921 GtkTextViewPrivate *priv;
3922 GtkStyleContext *context;
3923 GtkStateFlags state;
3925 GdkWindowAttr attributes;
3926 gint attributes_mask;
3930 text_view = GTK_TEXT_VIEW (widget);
3931 priv = text_view->priv;
3933 gtk_widget_set_realized (widget, TRUE);
3935 gtk_widget_get_allocation (widget, &allocation);
3937 attributes.window_type = GDK_WINDOW_CHILD;
3938 attributes.x = allocation.x;
3939 attributes.y = allocation.y;
3940 attributes.width = allocation.width;
3941 attributes.height = allocation.height;
3942 attributes.wclass = GDK_INPUT_OUTPUT;
3943 attributes.visual = gtk_widget_get_visual (widget);
3944 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3946 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
3948 window = gdk_window_new (gtk_widget_get_parent_window (widget),
3949 &attributes, attributes_mask);
3950 gtk_widget_set_window (widget, window);
3951 gdk_window_set_user_data (window, widget);
3953 context = gtk_widget_get_style_context (widget);
3954 state = gtk_widget_get_state_flags (widget);
3956 gtk_style_context_get_background_color (context, state, &color);
3957 gdk_window_set_background_rgba (window, &color);
3959 text_window_realize (priv->text_window, widget);
3961 if (priv->left_window)
3962 text_window_realize (priv->left_window, widget);
3964 if (priv->top_window)
3965 text_window_realize (priv->top_window, widget);
3967 if (priv->right_window)
3968 text_window_realize (priv->right_window, widget);
3970 if (priv->bottom_window)
3971 text_window_realize (priv->bottom_window, widget);
3973 gtk_text_view_ensure_layout (text_view);
3977 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3978 GDK_SELECTION_PRIMARY);
3979 gtk_text_buffer_add_selection_clipboard (priv->buffer, clipboard);
3982 tmp_list = priv->children;
3983 while (tmp_list != NULL)
3985 GtkTextViewChild *vc = tmp_list->data;
3987 text_view_child_set_parent_window (text_view, vc);
3989 tmp_list = tmp_list->next;
3992 /* Ensure updating the spot location. */
3993 gtk_text_view_update_im_spot_location (text_view);
3997 gtk_text_view_unrealize (GtkWidget *widget)
3999 GtkTextView *text_view;
4000 GtkTextViewPrivate *priv;
4002 text_view = GTK_TEXT_VIEW (widget);
4003 priv = text_view->priv;
4007 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4008 GDK_SELECTION_PRIMARY);
4009 gtk_text_buffer_remove_selection_clipboard (priv->buffer, clipboard);
4012 gtk_text_view_remove_validate_idles (text_view);
4014 if (priv->popup_menu)
4016 gtk_widget_destroy (priv->popup_menu);
4017 priv->popup_menu = NULL;
4020 text_window_unrealize (priv->text_window);
4022 if (priv->left_window)
4023 text_window_unrealize (priv->left_window);
4025 if (priv->top_window)
4026 text_window_unrealize (priv->top_window);
4028 if (priv->right_window)
4029 text_window_unrealize (priv->right_window);
4031 if (priv->bottom_window)
4032 text_window_unrealize (priv->bottom_window);
4034 gtk_text_view_destroy_layout (text_view);
4036 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize (widget);
4040 gtk_text_view_set_background (GtkTextView *text_view)
4042 GtkStyleContext *context;
4043 GtkStateFlags state;
4045 GtkTextViewPrivate *priv;
4048 widget = GTK_WIDGET (text_view);
4049 priv = text_view->priv;
4051 context = gtk_widget_get_style_context (widget);
4052 state = gtk_widget_get_state_flags (widget);
4054 /* Set bin window background */
4055 gtk_style_context_save (context);
4056 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
4058 gtk_style_context_get_background_color (context, state, &color);
4059 gdk_window_set_background_rgba (priv->text_window->bin_window, &color);
4061 gtk_style_context_restore (context);
4063 /* Set lateral panes background */
4064 gtk_style_context_get_background_color (context, state, &color);
4066 gdk_window_set_background_rgba (gtk_widget_get_window (widget), &color);
4068 if (priv->left_window)
4069 gdk_window_set_background_rgba (priv->left_window->bin_window, &color);
4071 if (priv->right_window)
4072 gdk_window_set_background_rgba (priv->right_window->bin_window, &color);
4074 if (priv->top_window)
4075 gdk_window_set_background_rgba (priv->top_window->bin_window, &color);
4077 if (priv->bottom_window)
4078 gdk_window_set_background_rgba (priv->bottom_window->bin_window, &color);
4082 gtk_text_view_style_updated (GtkWidget *widget)
4084 GtkTextView *text_view;
4085 GtkTextViewPrivate *priv;
4086 PangoContext *ltr_context, *rtl_context;
4088 text_view = GTK_TEXT_VIEW (widget);
4089 priv = text_view->priv;
4091 if (gtk_widget_get_realized (widget))
4093 gtk_text_view_set_background (text_view);
4096 if (priv->layout && priv->layout->default_style)
4098 gtk_text_view_set_attributes_from_style (text_view,
4099 priv->layout->default_style);
4101 ltr_context = gtk_widget_create_pango_context (widget);
4102 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
4103 rtl_context = gtk_widget_create_pango_context (widget);
4104 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
4106 gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
4108 g_object_unref (ltr_context);
4109 g_object_unref (rtl_context);
4114 gtk_text_view_direction_changed (GtkWidget *widget,
4115 GtkTextDirection previous_direction)
4117 GtkTextViewPrivate *priv = GTK_TEXT_VIEW (widget)->priv;
4121 priv->layout->default_style->direction = gtk_widget_get_direction (widget);
4123 gtk_text_layout_default_style_changed (priv->layout);
4128 gtk_text_view_state_flags_changed (GtkWidget *widget,
4129 GtkStateFlags previous_state)
4131 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4134 if (gtk_widget_get_realized (widget))
4136 gtk_text_view_set_background (text_view);
4138 if (gtk_widget_is_sensitive (widget))
4139 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4143 gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
4146 g_object_unref (cursor);
4148 text_view->priv->mouse_cursor_obscured = FALSE;
4151 if (!gtk_widget_is_sensitive (widget))
4153 /* Clear any selection */
4154 gtk_text_view_unselect (text_view);
4157 gtk_widget_queue_draw (widget);
4161 set_invisible_cursor (GdkWindow *window)
4163 GdkDisplay *display;
4166 display = gdk_window_get_display (window);
4167 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4169 gdk_window_set_cursor (window, cursor);
4171 g_object_unref (cursor);
4175 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
4177 if (text_view->priv->mouse_cursor_obscured)
4180 set_invisible_cursor (text_view->priv->text_window->bin_window);
4182 text_view->priv->mouse_cursor_obscured = TRUE;
4186 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
4188 if (text_view->priv->mouse_cursor_obscured)
4192 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
4194 gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
4195 g_object_unref (cursor);
4196 text_view->priv->mouse_cursor_obscured = FALSE;
4201 gtk_text_view_grab_notify (GtkWidget *widget,
4202 gboolean was_grabbed)
4204 GtkTextViewPrivate *priv;
4206 priv = GTK_TEXT_VIEW (widget)->priv;
4208 if (priv->grab_device &&
4209 gtk_widget_device_is_shadowed (widget, priv->grab_device))
4211 gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
4212 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
4222 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
4225 switch (event->type)
4227 case GDK_MOTION_NOTIFY:
4228 *x = event->motion.x;
4229 *y = event->motion.y;
4233 case GDK_BUTTON_PRESS:
4234 case GDK_2BUTTON_PRESS:
4235 case GDK_3BUTTON_PRESS:
4236 case GDK_BUTTON_RELEASE:
4237 *x = event->button.x;
4238 *y = event->button.y;
4243 case GDK_KEY_RELEASE:
4244 case GDK_ENTER_NOTIFY:
4245 case GDK_LEAVE_NOTIFY:
4246 case GDK_PROPERTY_NOTIFY:
4247 case GDK_SELECTION_CLEAR:
4248 case GDK_SELECTION_REQUEST:
4249 case GDK_SELECTION_NOTIFY:
4250 case GDK_PROXIMITY_IN:
4251 case GDK_PROXIMITY_OUT:
4252 case GDK_DRAG_ENTER:
4253 case GDK_DRAG_LEAVE:
4254 case GDK_DRAG_MOTION:
4255 case GDK_DRAG_STATUS:
4256 case GDK_DROP_START:
4257 case GDK_DROP_FINISHED:
4267 emit_event_on_tags (GtkWidget *widget,
4273 gboolean retval = FALSE;
4275 tags = gtk_text_iter_get_tags (iter);
4280 GtkTextTag *tag = tmp->data;
4282 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
4288 tmp = g_slist_next (tmp);
4291 g_slist_free (tags);
4297 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
4299 GtkTextView *text_view;
4300 GtkTextViewPrivate *priv;
4303 text_view = GTK_TEXT_VIEW (widget);
4304 priv = text_view->priv;
4306 if (priv->layout == NULL ||
4307 get_buffer (text_view) == NULL)
4310 if (event->any.window != priv->text_window->bin_window)
4313 if (get_event_coordinates (event, &x, &y))
4320 /* FIXME this is slow and we do it twice per event.
4321 * My favorite solution is to have GtkTextLayout cache
4322 * the last couple lookups.
4324 gtk_text_layout_get_iter_at_pixel (priv->layout,
4328 return emit_event_on_tags (widget, event, &iter);
4330 else if (event->type == GDK_KEY_PRESS ||
4331 event->type == GDK_KEY_RELEASE)
4335 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
4336 gtk_text_buffer_get_insert (get_buffer (text_view)));
4338 return emit_event_on_tags (widget, event, &iter);
4345 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
4347 GtkTextView *text_view;
4348 GtkTextViewPrivate *priv;
4349 GtkTextMark *insert;
4351 gboolean can_insert;
4352 gboolean retval = FALSE;
4353 gboolean obscure = FALSE;
4355 text_view = GTK_TEXT_VIEW (widget);
4356 priv = text_view->priv;
4358 if (priv->layout == NULL ||
4359 get_buffer (text_view) == NULL)
4362 /* Make sure input method knows where it is */
4363 flush_update_im_spot_location (text_view);
4365 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4366 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4367 can_insert = gtk_text_iter_can_insert (&iter, priv->editable);
4368 if (gtk_im_context_filter_keypress (priv->im_context, event))
4370 priv->need_im_reset = TRUE;
4372 gtk_text_view_reset_im_context (text_view);
4373 obscure = can_insert;
4377 else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4381 /* use overall editability not can_insert, more predictable for users */
4382 else if (priv->editable &&
4383 (event->keyval == GDK_KEY_Return ||
4384 event->keyval == GDK_KEY_ISO_Enter ||
4385 event->keyval == GDK_KEY_KP_Enter))
4387 /* this won't actually insert the newline if the cursor isn't
4390 gtk_text_view_reset_im_context (text_view);
4391 gtk_text_view_commit_text (text_view, "\n");
4396 /* Pass through Tab as literal tab, unless Control is held down */
4397 else if ((event->keyval == GDK_KEY_Tab ||
4398 event->keyval == GDK_KEY_KP_Tab ||
4399 event->keyval == GDK_KEY_ISO_Left_Tab) &&
4400 !(event->state & GDK_CONTROL_MASK))
4402 /* If the text widget isn't editable overall, or if the application
4403 * has turned off "accepts_tab", move the focus instead
4405 if (priv->accepts_tab && priv->editable)
4407 gtk_text_view_reset_im_context (text_view);
4408 gtk_text_view_commit_text (text_view, "\t");
4412 g_signal_emit_by_name (text_view, "move-focus",
4413 (event->state & GDK_SHIFT_MASK) ?
4414 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4422 gtk_text_view_obscure_mouse_cursor (text_view);
4424 gtk_text_view_reset_blink_time (text_view);
4425 gtk_text_view_pend_cursor_blink (text_view);
4431 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4433 GtkTextView *text_view;
4434 GtkTextViewPrivate *priv;
4435 GtkTextMark *insert;
4438 text_view = GTK_TEXT_VIEW (widget);
4439 priv = text_view->priv;
4441 if (priv->layout == NULL || get_buffer (text_view) == NULL)
4444 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4445 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4446 if (gtk_text_iter_can_insert (&iter, priv->editable) &&
4447 gtk_im_context_filter_keypress (priv->im_context, event))
4449 priv->need_im_reset = TRUE;
4453 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4457 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4459 GtkTextView *text_view;
4460 GtkTextViewPrivate *priv;
4462 text_view = GTK_TEXT_VIEW (widget);
4463 priv = text_view->priv;
4465 gtk_widget_grab_focus (widget);
4467 if (event->window != priv->text_window->bin_window)
4469 /* Remove selection if any. */
4470 gtk_text_view_unselect (text_view);
4474 gtk_text_view_reset_blink_time (text_view);
4478 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4479 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4480 else if (event->button == 3)
4481 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4484 if (event->type == GDK_BUTTON_PRESS)
4486 gtk_text_view_reset_im_context (text_view);
4488 if (event->button == 1)
4490 /* If we're in the selection, start a drag copy/move of the
4491 * selection; otherwise, start creating a new selection.
4494 GtkTextIter start, end;
4496 gtk_text_layout_get_iter_at_pixel (priv->layout,
4498 event->x + priv->xoffset,
4499 event->y + priv->yoffset);
4501 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4503 gtk_text_iter_in_range (&iter, &start, &end) &&
4504 !(event->state & GDK_SHIFT_MASK))
4506 priv->drag_start_x = event->x;
4507 priv->drag_start_y = event->y;
4508 priv->pending_place_cursor_button = event->button;
4512 gtk_text_view_start_selection_drag (text_view, &iter, event);
4517 else if (event->button == 2)
4521 /* We do not want to scroll back to the insert iter when we paste
4522 with the middle button */
4523 priv->scroll_after_paste = FALSE;
4525 gtk_text_layout_get_iter_at_pixel (priv->layout,
4527 event->x + priv->xoffset,
4528 event->y + priv->yoffset);
4530 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4531 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4536 else if (event->button == 3)
4538 gtk_text_view_do_popup (text_view, event);
4542 else if ((event->type == GDK_2BUTTON_PRESS ||
4543 event->type == GDK_3BUTTON_PRESS) &&
4548 gtk_text_view_end_selection_drag (text_view);
4550 gtk_text_layout_get_iter_at_pixel (priv->layout,
4552 event->x + priv->xoffset,
4553 event->y + priv->yoffset);
4555 gtk_text_view_start_selection_drag (text_view, &iter, event);
4563 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4565 GtkTextView *text_view;
4566 GtkTextViewPrivate *priv;
4568 text_view = GTK_TEXT_VIEW (widget);
4569 priv = text_view->priv;
4571 if (event->window != priv->text_window->bin_window)
4574 if (event->button == 1)
4576 if (priv->drag_start_x >= 0)
4578 priv->drag_start_x = -1;
4579 priv->drag_start_y = -1;
4582 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4584 else if (priv->pending_place_cursor_button == event->button)
4588 /* Unselect everything; we clicked inside selection, but
4589 * didn't move by the drag threshold, so just clear selection
4592 gtk_text_layout_get_iter_at_pixel (priv->layout,
4594 event->x + priv->xoffset,
4595 event->y + priv->yoffset);
4597 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4598 gtk_text_view_check_cursor_blink (text_view);
4600 priv->pending_place_cursor_button = 0;
4610 keymap_direction_changed (GdkKeymap *keymap,
4611 GtkTextView *text_view)
4613 gtk_text_view_check_keymap_direction (text_view);
4617 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4619 GtkTextView *text_view;
4620 GtkTextViewPrivate *priv;
4622 text_view = GTK_TEXT_VIEW (widget);
4623 priv = text_view->priv;
4625 gtk_widget_queue_draw (widget);
4627 DV(g_print (G_STRLOC": focus_in_event\n"));
4629 gtk_text_view_reset_blink_time (text_view);
4631 if (priv->cursor_visible && priv->layout)
4633 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
4634 gtk_text_view_check_cursor_blink (text_view);
4637 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4638 "direction-changed",
4639 G_CALLBACK (keymap_direction_changed), text_view);
4640 gtk_text_view_check_keymap_direction (text_view);
4644 priv->need_im_reset = TRUE;
4645 gtk_im_context_focus_in (priv->im_context);
4652 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4654 GtkTextView *text_view;
4655 GtkTextViewPrivate *priv;
4657 text_view = GTK_TEXT_VIEW (widget);
4658 priv = text_view->priv;
4660 gtk_text_view_end_selection_drag (text_view);
4662 gtk_widget_queue_draw (widget);
4664 DV(g_print (G_STRLOC": focus_out_event\n"));
4666 if (priv->cursor_visible && priv->layout)
4668 gtk_text_view_check_cursor_blink (text_view);
4669 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
4672 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4673 keymap_direction_changed,
4678 priv->need_im_reset = TRUE;
4679 gtk_im_context_focus_out (priv->im_context);
4686 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4688 GtkTextView *text_view;
4689 GtkTextViewPrivate *priv;
4691 text_view = GTK_TEXT_VIEW (widget);
4692 priv = text_view->priv;
4694 gtk_text_view_unobscure_mouse_cursor (text_view);
4696 if (event->window == priv->text_window->bin_window &&
4697 priv->drag_start_x >= 0)
4702 gdk_event_request_motions (event);
4704 if (gtk_drag_check_threshold (widget,
4710 gint buffer_x, buffer_y;
4712 gtk_text_view_window_to_buffer_coords (text_view,
4713 GTK_TEXT_WINDOW_TEXT,
4719 gtk_text_layout_get_iter_at_pixel (priv->layout,
4721 buffer_x, buffer_y);
4723 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4732 gtk_text_view_paint (GtkWidget *widget,
4735 GtkTextView *text_view;
4736 GtkTextViewPrivate *priv;
4737 GList *child_exposes;
4740 text_view = GTK_TEXT_VIEW (widget);
4741 priv = text_view->priv;
4743 g_return_if_fail (priv->layout != NULL);
4744 g_return_if_fail (priv->xoffset >= 0);
4745 g_return_if_fail (priv->yoffset >= 0);
4747 while (priv->first_validate_idle != 0)
4749 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4750 priv->first_validate_idle));
4751 gtk_text_view_flush_first_validate (text_view);
4754 if (!priv->onscreen_validated)
4756 g_warning (G_STRLOC ": somehow some text lines were modified or scrolling occurred since the last validation of lines on the screen - may be a text widget bug.");
4757 g_assert_not_reached ();
4761 printf ("painting %d,%d %d x %d\n",
4763 area->width, area->height);
4766 child_exposes = NULL;
4769 cairo_translate (cr, -priv->xoffset, -priv->yoffset);
4771 gtk_text_layout_draw (priv->layout,
4778 tmp_list = child_exposes;
4779 while (tmp_list != NULL)
4781 GtkWidget *child = tmp_list->data;
4783 gtk_container_propagate_draw (GTK_CONTAINER (text_view),
4787 g_object_unref (child);
4789 tmp_list = tmp_list->next;
4792 g_list_free (child_exposes);
4796 gtk_text_view_draw (GtkWidget *widget,
4802 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
4803 gtk_text_view_draw_focus (widget, cr);
4805 window = gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4806 GTK_TEXT_WINDOW_TEXT);
4807 if (gtk_cairo_should_draw_window (cr, window))
4809 DV(g_print (">Exposed ("G_STRLOC")\n"));
4811 gtk_cairo_transform_to_window (cr, widget, window);
4812 gtk_text_view_paint (widget, cr);
4816 /* Propagate exposes to all unanchored children.
4817 * Anchored children are handled in gtk_text_view_paint().
4819 tmp_list = GTK_TEXT_VIEW (widget)->priv->children;
4820 while (tmp_list != NULL)
4822 GtkTextViewChild *vc = tmp_list->data;
4824 /* propagate_draw checks that event->window matches
4828 gtk_container_propagate_draw (GTK_CONTAINER (widget),
4832 tmp_list = tmp_list->next;
4839 gtk_text_view_draw_focus (GtkWidget *widget,
4842 gboolean interior_focus;
4844 /* We clear the focus if we are in interior focus mode. */
4845 gtk_widget_style_get (widget,
4846 "interior-focus", &interior_focus,
4849 if (gtk_widget_has_focus (widget) && !interior_focus)
4851 GtkStyleContext *context;
4853 context = gtk_widget_get_style_context (widget);
4855 gtk_render_focus (context, cr, 0, 0,
4856 gtk_widget_get_allocated_width (widget),
4857 gtk_widget_get_allocated_height (widget));
4862 gtk_text_view_focus (GtkWidget *widget,
4863 GtkDirectionType direction)
4865 GtkContainer *container;
4868 container = GTK_CONTAINER (widget);
4870 if (!gtk_widget_is_focus (widget) &&
4871 gtk_container_get_focus_child (container) == NULL)
4873 gtk_widget_grab_focus (widget);
4879 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4880 * children to get the focus
4882 gtk_widget_set_can_focus (widget, FALSE);
4883 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4884 gtk_widget_set_can_focus (widget, TRUE);
4895 gtk_text_view_add (GtkContainer *container,
4898 /* This is pretty random. */
4899 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4901 GTK_TEXT_WINDOW_WIDGET,
4906 gtk_text_view_remove (GtkContainer *container,
4909 GtkTextView *text_view;
4910 GtkTextViewPrivate *priv;
4911 GtkTextViewChild *vc;
4914 text_view = GTK_TEXT_VIEW (container);
4915 priv = text_view->priv;
4918 iter = priv->children;
4920 while (iter != NULL)
4924 if (vc->widget == child)
4927 iter = g_slist_next (iter);
4930 g_assert (iter != NULL); /* be sure we had the child in the list */
4932 priv->children = g_slist_remove (priv->children, vc);
4934 gtk_widget_unparent (vc->widget);
4936 text_view_child_free (vc);
4940 gtk_text_view_forall (GtkContainer *container,
4941 gboolean include_internals,
4942 GtkCallback callback,
4943 gpointer callback_data)
4946 GtkTextView *text_view;
4949 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4950 g_return_if_fail (callback != NULL);
4952 text_view = GTK_TEXT_VIEW (container);
4954 copy = g_slist_copy (text_view->priv->children);
4957 while (iter != NULL)
4959 GtkTextViewChild *vc = iter->data;
4961 (* callback) (vc->widget, callback_data);
4963 iter = g_slist_next (iter);
4966 g_slist_free (copy);
4969 #define CURSOR_ON_MULTIPLIER 2
4970 #define CURSOR_OFF_MULTIPLIER 1
4971 #define CURSOR_PEND_MULTIPLIER 3
4972 #define CURSOR_DIVIDER 3
4975 cursor_blinks (GtkTextView *text_view)
4977 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4980 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4983 if (gtk_get_debug_flags () & GTK_DEBUG_UPDATES)
4986 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4991 if (text_view->priv->editable)
4993 GtkTextMark *insert;
4996 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4997 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4999 if (gtk_text_iter_editable (&iter, text_view->priv->editable))
5007 get_cursor_time (GtkTextView *text_view)
5009 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5012 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5018 get_cursor_blink_timeout (GtkTextView *text_view)
5020 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5023 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
5034 blink_cb (gpointer data)
5036 GtkTextView *text_view;
5037 GtkTextViewPrivate *priv;
5041 text_view = GTK_TEXT_VIEW (data);
5042 priv = text_view->priv;
5044 if (!gtk_widget_has_focus (GTK_WIDGET (text_view)))
5046 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
5047 "connect a handler to this signal, it must return\n"
5048 "FALSE so the text view gets the event as well");
5050 gtk_text_view_check_cursor_blink (text_view);
5055 g_assert (priv->layout);
5056 g_assert (priv->cursor_visible);
5058 visible = gtk_text_layout_get_cursor_visible (priv->layout);
5060 blink_timeout = get_cursor_blink_timeout (text_view);
5061 if (priv->blink_time > 1000 * blink_timeout &&
5062 blink_timeout < G_MAXINT/1000)
5064 /* we've blinked enough without the user doing anything, stop blinking */
5066 priv->blink_timeout = 0;
5069 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5074 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5077 priv->blink_time += get_cursor_time (text_view);
5080 /* Block changed_handler while changing the layout's cursor visibility
5081 * because it would expose the whole paragraph. Instead, we expose
5082 * the cursor's area(s) manually below.
5084 g_signal_handlers_block_by_func (priv->layout,
5087 gtk_text_layout_set_cursor_visible (priv->layout, !visible);
5088 g_signal_handlers_unblock_by_func (priv->layout,
5092 text_window_invalidate_cursors (priv->text_window);
5094 /* Remove ourselves */
5100 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
5102 if (text_view->priv->blink_timeout)
5104 g_source_remove (text_view->priv->blink_timeout);
5105 text_view->priv->blink_timeout = 0;
5110 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
5112 GtkTextViewPrivate *priv = text_view->priv;
5114 if (priv->layout != NULL &&
5115 priv->cursor_visible &&
5116 gtk_widget_has_focus (GTK_WIDGET (text_view)))
5118 if (cursor_blinks (text_view))
5120 if (priv->blink_timeout == 0)
5122 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5124 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5131 gtk_text_view_stop_cursor_blink (text_view);
5132 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5137 gtk_text_view_stop_cursor_blink (text_view);
5138 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
5143 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
5145 GtkTextViewPrivate *priv = text_view->priv;
5147 if (priv->layout != NULL &&
5148 priv->cursor_visible &&
5149 gtk_widget_has_focus (GTK_WIDGET (text_view)) &&
5150 cursor_blinks (text_view))
5152 gtk_text_view_stop_cursor_blink (text_view);
5153 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5155 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5162 gtk_text_view_reset_blink_time (GtkTextView *text_view)
5164 GtkTextViewPrivate *priv = text_view->priv;
5166 priv->blink_time = 0;
5171 * Key binding handlers
5175 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
5176 GtkTextIter *newplace,
5179 gboolean ret = TRUE;
5183 ret = gtk_text_layout_move_iter_to_previous_line (text_view->priv->layout, newplace);
5189 ret = gtk_text_layout_move_iter_to_next_line (text_view->priv->layout, newplace);
5197 move_cursor (GtkTextView *text_view,
5198 const GtkTextIter *new_location,
5199 gboolean extend_selection)
5201 if (extend_selection)
5202 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
5206 gtk_text_buffer_place_cursor (get_buffer (text_view),
5208 gtk_text_view_check_cursor_blink (text_view);
5212 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
5213 GtkMovementStep step,
5215 gboolean extend_selection)
5217 GtkTextViewPrivate *priv;
5219 GtkTextIter newplace;
5220 gboolean cancel_selection = FALSE;
5221 gint cursor_x_pos = 0;
5222 GtkDirectionType leave_direction = -1;
5224 priv = text_view->priv;
5226 if (!priv->cursor_visible)
5228 GtkScrollStep scroll_step;
5229 gdouble old_xpos, old_ypos;
5233 case GTK_MOVEMENT_VISUAL_POSITIONS:
5234 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
5236 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5237 case GTK_MOVEMENT_WORDS:
5238 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
5240 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5241 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
5243 case GTK_MOVEMENT_DISPLAY_LINES:
5244 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
5246 case GTK_MOVEMENT_PARAGRAPHS:
5247 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5248 scroll_step = GTK_SCROLL_STEPS;
5250 case GTK_MOVEMENT_PAGES:
5251 scroll_step = GTK_SCROLL_PAGES;
5253 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5254 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
5256 case GTK_MOVEMENT_BUFFER_ENDS:
5257 scroll_step = GTK_SCROLL_ENDS;
5260 scroll_step = GTK_SCROLL_PAGES;
5264 old_xpos = priv->xoffset;
5265 old_ypos = priv->yoffset;
5266 gtk_text_view_move_viewport (text_view, scroll_step, count);
5267 if ((old_xpos != priv->xoffset || old_ypos != priv->yoffset) &&
5268 leave_direction != -1 &&
5269 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5272 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5278 gtk_text_view_reset_im_context (text_view);
5280 if (step == GTK_MOVEMENT_PAGES)
5282 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
5283 gtk_widget_error_bell (GTK_WIDGET (text_view));
5285 gtk_text_view_check_cursor_blink (text_view);
5286 gtk_text_view_pend_cursor_blink (text_view);
5289 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
5291 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
5292 gtk_widget_error_bell (GTK_WIDGET (text_view));
5294 gtk_text_view_check_cursor_blink (text_view);
5295 gtk_text_view_pend_cursor_blink (text_view);
5299 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5300 gtk_text_buffer_get_insert (get_buffer (text_view)));
5302 if (! extend_selection)
5304 GtkTextIter sel_bound;
5306 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &sel_bound,
5307 gtk_text_buffer_get_selection_bound (get_buffer (text_view)));
5309 /* if we move forward, assume the cursor is at the end of the selection;
5310 * if we move backward, assume the cursor is at the start
5313 gtk_text_iter_order (&sel_bound, &insert);
5315 gtk_text_iter_order (&insert, &sel_bound);
5317 /* if we actually have a selection, just move *to* the beginning/end
5318 * of the selection and not *from* there on LOGICAL_POSITIONS
5319 * and VISUAL_POSITIONS movement
5321 if (! gtk_text_iter_equal (&sel_bound, &insert))
5322 cancel_selection = TRUE;
5327 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5328 gtk_text_view_get_virtual_cursor_pos (text_view, &insert, &cursor_x_pos, NULL);
5332 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5333 if (! cancel_selection)
5334 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5337 case GTK_MOVEMENT_VISUAL_POSITIONS:
5338 if (! cancel_selection)
5339 gtk_text_layout_move_iter_visually (priv->layout,
5343 case GTK_MOVEMENT_WORDS:
5345 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5348 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5349 gtk_text_iter_forward_to_line_end (&newplace);
5353 case GTK_MOVEMENT_DISPLAY_LINES:
5356 leave_direction = GTK_DIR_UP;
5358 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5359 gtk_text_layout_move_iter_to_x (priv->layout, &newplace, cursor_x_pos);
5361 gtk_text_iter_set_line_offset (&newplace, 0);
5365 leave_direction = GTK_DIR_DOWN;
5367 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5368 gtk_text_layout_move_iter_to_x (priv->layout, &newplace, cursor_x_pos);
5370 gtk_text_iter_forward_to_line_end (&newplace);
5374 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5376 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5377 else if (count < -1)
5378 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5381 gtk_text_layout_move_iter_to_line_end (priv->layout, &newplace, count);
5384 case GTK_MOVEMENT_PARAGRAPHS:
5387 if (!gtk_text_iter_ends_line (&newplace))
5389 gtk_text_iter_forward_to_line_end (&newplace);
5392 gtk_text_iter_forward_visible_lines (&newplace, count);
5393 gtk_text_iter_forward_to_line_end (&newplace);
5397 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5398 gtk_text_iter_set_line_offset (&newplace, 0);
5399 gtk_text_iter_forward_visible_lines (&newplace, count);
5400 gtk_text_iter_set_line_offset (&newplace, 0);
5404 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5407 if (!gtk_text_iter_ends_line (&newplace))
5408 gtk_text_iter_forward_to_line_end (&newplace);
5412 gtk_text_iter_set_line_offset (&newplace, 0);
5416 case GTK_MOVEMENT_BUFFER_ENDS:
5418 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5420 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5427 /* call move_cursor() even if the cursor hasn't moved, since it
5428 cancels the selection
5430 move_cursor (text_view, &newplace, extend_selection);
5432 if (!gtk_text_iter_equal (&insert, &newplace))
5434 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5435 gtk_text_view_scroll_mark_onscreen (text_view,
5436 gtk_text_buffer_get_insert (get_buffer (text_view)));
5438 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5439 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5441 else if (leave_direction != -1)
5443 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5446 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5449 else if (! cancel_selection)
5451 gtk_widget_error_bell (GTK_WIDGET (text_view));
5454 gtk_text_view_check_cursor_blink (text_view);
5455 gtk_text_view_pend_cursor_blink (text_view);
5459 gtk_text_view_move_cursor (GtkTextView *text_view,
5460 GtkMovementStep step,
5462 gboolean extend_selection)
5464 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5468 gtk_text_view_move_viewport (GtkTextView *text_view,
5472 GtkAdjustment *adjustment;
5477 case GTK_SCROLL_STEPS:
5478 case GTK_SCROLL_PAGES:
5479 case GTK_SCROLL_ENDS:
5480 adjustment = text_view->priv->vadjustment;
5482 case GTK_SCROLL_HORIZONTAL_STEPS:
5483 case GTK_SCROLL_HORIZONTAL_PAGES:
5484 case GTK_SCROLL_HORIZONTAL_ENDS:
5485 adjustment = text_view->priv->hadjustment;
5488 adjustment = text_view->priv->vadjustment;
5494 case GTK_SCROLL_STEPS:
5495 case GTK_SCROLL_HORIZONTAL_STEPS:
5496 increment = gtk_adjustment_get_step_increment (adjustment);
5498 case GTK_SCROLL_PAGES:
5499 case GTK_SCROLL_HORIZONTAL_PAGES:
5500 increment = gtk_adjustment_get_page_increment (adjustment);
5502 case GTK_SCROLL_ENDS:
5503 case GTK_SCROLL_HORIZONTAL_ENDS:
5504 increment = gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment);
5511 gtk_adjustment_set_value (adjustment, gtk_adjustment_get_value (adjustment) + count * increment);
5515 gtk_text_view_set_anchor (GtkTextView *text_view)
5519 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5520 gtk_text_buffer_get_insert (get_buffer (text_view)));
5522 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5526 gtk_text_view_scroll_pages (GtkTextView *text_view,
5528 gboolean extend_selection)
5530 GtkTextViewPrivate *priv;
5531 GtkAdjustment *adjustment;
5532 gint cursor_x_pos, cursor_y_pos;
5533 GtkTextMark *insert_mark;
5534 GtkTextIter old_insert;
5535 GtkTextIter new_insert;
5541 priv = text_view->priv;
5543 g_return_val_if_fail (priv->vadjustment != NULL, FALSE);
5545 adjustment = priv->vadjustment;
5547 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5549 /* Make sure we start from the current cursor position, even
5550 * if it was offscreen, but don't queue more scrolls if we're
5553 if (priv->pending_scroll)
5554 cancel_pending_scroll (text_view);
5556 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5558 /* Validate the region that will be brought into view by the cursor motion
5560 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5561 &old_insert, insert_mark);
5565 gtk_text_view_get_first_para_iter (text_view, &anchor);
5566 y0 = gtk_adjustment_get_page_size (adjustment);
5567 y1 = gtk_adjustment_get_page_size (adjustment) + count * gtk_adjustment_get_page_increment (adjustment);
5571 gtk_text_view_get_first_para_iter (text_view, &anchor);
5572 y0 = count * gtk_adjustment_get_page_increment (adjustment) + gtk_adjustment_get_page_size (adjustment);
5576 gtk_text_layout_validate_yrange (priv->layout, &anchor, y0, y1);
5577 /* FIXME do we need to update the adjustment ranges here? */
5579 new_insert = old_insert;
5581 if (count < 0 && gtk_adjustment_get_value (adjustment) <= (gtk_adjustment_get_lower (adjustment) + 1e-12))
5583 /* already at top, just be sure we are at offset 0 */
5584 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5585 move_cursor (text_view, &new_insert, extend_selection);
5587 else if (count > 0 && gtk_adjustment_get_value (adjustment) >= (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_page_size (adjustment) - 1e-12))
5589 /* already at bottom, just be sure we are at the end */
5590 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5591 move_cursor (text_view, &new_insert, extend_selection);
5595 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5597 oldval = gtk_adjustment_get_value (adjustment);
5598 newval = gtk_adjustment_get_value (adjustment);
5600 newval += count * gtk_adjustment_get_page_increment (adjustment);
5602 gtk_adjustment_set_value (adjustment, newval);
5603 cursor_y_pos += gtk_adjustment_get_value (adjustment) - oldval;
5605 gtk_text_layout_get_iter_at_pixel (priv->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5606 clamp_iter_onscreen (text_view, &new_insert);
5607 move_cursor (text_view, &new_insert, extend_selection);
5609 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5612 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5613 * only guarantees 1 pixel onscreen.
5615 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5616 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5618 return !gtk_text_iter_equal (&old_insert, &new_insert);
5622 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5624 gboolean extend_selection)
5626 GtkTextViewPrivate *priv;
5627 GtkAdjustment *adjustment;
5628 gint cursor_x_pos, cursor_y_pos;
5629 GtkTextMark *insert_mark;
5630 GtkTextIter old_insert;
5631 GtkTextIter new_insert;
5636 priv = text_view->priv;
5638 g_return_val_if_fail (priv->hadjustment != NULL, FALSE);
5640 adjustment = priv->hadjustment;
5642 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5644 /* Make sure we start from the current cursor position, even
5645 * if it was offscreen, but don't queue more scrolls if we're
5648 if (priv->pending_scroll)
5649 cancel_pending_scroll (text_view);
5651 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5653 /* Validate the line that we're moving within.
5655 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5656 &old_insert, insert_mark);
5658 gtk_text_layout_get_line_yrange (priv->layout, &old_insert, &y, &height);
5659 gtk_text_layout_validate_yrange (priv->layout, &old_insert, y, y + height);
5660 /* FIXME do we need to update the adjustment ranges here? */
5662 new_insert = old_insert;
5664 if (count < 0 && gtk_adjustment_get_value (adjustment) <= (gtk_adjustment_get_lower (adjustment) + 1e-12))
5666 /* already at far left, just be sure we are at offset 0 */
5667 gtk_text_iter_set_line_offset (&new_insert, 0);
5668 move_cursor (text_view, &new_insert, extend_selection);
5670 else if (count > 0 && gtk_adjustment_get_value (adjustment) >= (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_page_size (adjustment) - 1e-12))
5672 /* already at far right, just be sure we are at the end */
5673 if (!gtk_text_iter_ends_line (&new_insert))
5674 gtk_text_iter_forward_to_line_end (&new_insert);
5675 move_cursor (text_view, &new_insert, extend_selection);
5679 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5681 oldval = gtk_adjustment_get_value (adjustment);
5682 newval = gtk_adjustment_get_value (adjustment);
5684 newval += count * gtk_adjustment_get_page_increment (adjustment);
5686 gtk_adjustment_set_value (adjustment, newval);
5687 cursor_x_pos += gtk_adjustment_get_value (adjustment) - oldval;
5689 gtk_text_layout_get_iter_at_pixel (priv->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5690 clamp_iter_onscreen (text_view, &new_insert);
5691 move_cursor (text_view, &new_insert, extend_selection);
5693 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5696 /* FIXME for lines shorter than the overall widget width, this results in a
5697 * "bounce" effect as we scroll to the right of the widget, then scroll
5698 * back to get the end of the line onscreen.
5699 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5702 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5703 * only guarantees 1 pixel onscreen.
5705 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5706 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5708 return !gtk_text_iter_equal (&old_insert, &new_insert);
5712 whitespace (gunichar ch, gpointer user_data)
5714 return (ch == ' ' || ch == '\t');
5718 not_whitespace (gunichar ch, gpointer user_data)
5720 return !whitespace (ch, user_data);
5724 find_whitepace_region (const GtkTextIter *center,
5725 GtkTextIter *start, GtkTextIter *end)
5730 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5731 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5732 if (whitespace (gtk_text_iter_get_char (end), NULL))
5733 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5735 return !gtk_text_iter_equal (start, end);
5739 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5742 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5743 text_view->priv->editable))
5745 gtk_widget_error_bell (GTK_WIDGET (text_view));
5750 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5754 GtkTextViewPrivate *priv;
5758 gboolean leave_one = FALSE;
5760 priv = text_view->priv;
5762 gtk_text_view_reset_im_context (text_view);
5764 if (type == GTK_DELETE_CHARS)
5766 /* Char delete deletes the selection, if one exists */
5767 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5772 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5773 gtk_text_buffer_get_insert (get_buffer (text_view)));
5780 case GTK_DELETE_CHARS:
5781 gtk_text_iter_forward_cursor_positions (&end, count);
5784 case GTK_DELETE_WORD_ENDS:
5786 gtk_text_iter_forward_word_ends (&end, count);
5788 gtk_text_iter_backward_word_starts (&start, 0 - count);
5791 case GTK_DELETE_WORDS:
5794 case GTK_DELETE_DISPLAY_LINE_ENDS:
5797 case GTK_DELETE_DISPLAY_LINES:
5800 case GTK_DELETE_PARAGRAPH_ENDS:
5803 /* If we're already at a newline, we need to
5804 * simply delete that newline, instead of
5805 * moving to the next one.
5807 if (gtk_text_iter_ends_line (&end))
5809 gtk_text_iter_forward_line (&end);
5815 if (!gtk_text_iter_forward_to_line_end (&end))
5823 if (gtk_text_iter_starts_line (&start))
5825 gtk_text_iter_backward_line (&start);
5826 if (!gtk_text_iter_ends_line (&end))
5827 gtk_text_iter_forward_to_line_end (&start);
5831 gtk_text_iter_set_line_offset (&start, 0);
5835 gtk_text_iter_backward_lines (&start, -count);
5839 case GTK_DELETE_PARAGRAPHS:
5842 gtk_text_iter_set_line_offset (&start, 0);
5843 gtk_text_iter_forward_to_line_end (&end);
5845 /* Do the lines beyond the first. */
5848 gtk_text_iter_forward_to_line_end (&end);
5854 /* FIXME negative count? */
5858 case GTK_DELETE_WHITESPACE:
5860 find_whitepace_region (&insert, &start, &end);
5868 if (!gtk_text_iter_equal (&start, &end))
5870 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5872 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5876 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5882 gtk_widget_error_bell (GTK_WIDGET (text_view));
5885 gtk_text_buffer_end_user_action (get_buffer (text_view));
5886 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5888 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5889 gtk_text_view_scroll_mark_onscreen (text_view,
5890 gtk_text_buffer_get_insert (get_buffer (text_view)));
5894 gtk_widget_error_bell (GTK_WIDGET (text_view));
5899 gtk_text_view_backspace (GtkTextView *text_view)
5901 GtkTextViewPrivate *priv;
5904 priv = text_view->priv;
5906 gtk_text_view_reset_im_context (text_view);
5908 /* Backspace deletes the selection, if one exists */
5909 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5913 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5915 gtk_text_buffer_get_insert (get_buffer (text_view)));
5917 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5918 TRUE, priv->editable))
5920 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5921 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5922 gtk_text_view_scroll_mark_onscreen (text_view,
5923 gtk_text_buffer_get_insert (get_buffer (text_view)));
5927 gtk_widget_error_bell (GTK_WIDGET (text_view));
5932 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5934 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5935 GDK_SELECTION_CLIPBOARD);
5937 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5939 text_view->priv->editable);
5940 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5941 gtk_text_view_scroll_mark_onscreen (text_view,
5942 gtk_text_buffer_get_insert (get_buffer (text_view)));
5946 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5948 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5949 GDK_SELECTION_CLIPBOARD);
5951 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5954 /* on copy do not scroll, we are already onscreen */
5958 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5960 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5961 GDK_SELECTION_CLIPBOARD);
5963 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5966 text_view->priv->editable);
5970 gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
5971 GtkClipboard *clipboard,
5974 GtkTextView *text_view = data;
5975 GtkTextViewPrivate *priv;
5977 priv = text_view->priv;
5979 if (priv->scroll_after_paste)
5981 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5982 gtk_text_view_scroll_mark_onscreen (text_view, gtk_text_buffer_get_insert (buffer));
5985 priv->scroll_after_paste = TRUE;
5989 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5991 GtkTextViewPrivate *priv = text_view->priv;
5993 if (priv->text_window)
5994 text_window_invalidate_cursors (priv->text_window);
5996 priv->overwrite_mode = !priv->overwrite_mode;
5999 gtk_text_layout_set_overwrite_mode (priv->layout,
6000 priv->overwrite_mode && priv->editable);
6002 if (priv->text_window)
6003 text_window_invalidate_cursors (priv->text_window);
6005 gtk_text_view_pend_cursor_blink (text_view);
6007 g_object_notify (G_OBJECT (text_view), "overwrite");
6011 * gtk_text_view_get_overwrite:
6012 * @text_view: a #GtkTextView
6014 * Returns whether the #GtkTextView is in overwrite mode or not.
6016 * Return value: whether @text_view is in overwrite mode or not.
6021 gtk_text_view_get_overwrite (GtkTextView *text_view)
6023 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6025 return text_view->priv->overwrite_mode;
6029 * gtk_text_view_set_overwrite:
6030 * @text_view: a #GtkTextView
6031 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
6033 * Changes the #GtkTextView overwrite mode.
6038 gtk_text_view_set_overwrite (GtkTextView *text_view,
6041 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6042 overwrite = overwrite != FALSE;
6044 if (text_view->priv->overwrite_mode != overwrite)
6045 gtk_text_view_toggle_overwrite (text_view);
6049 * gtk_text_view_set_accepts_tab:
6050 * @text_view: A #GtkTextView
6051 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
6052 * character, %FALSE, if pressing the Tab key should move the
6055 * Sets the behavior of the text widget when the Tab key is pressed.
6056 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
6057 * is %FALSE the keyboard focus is moved to the next widget in the focus
6063 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
6064 gboolean accepts_tab)
6066 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6068 accepts_tab = accepts_tab != FALSE;
6070 if (text_view->priv->accepts_tab != accepts_tab)
6072 text_view->priv->accepts_tab = accepts_tab;
6074 g_object_notify (G_OBJECT (text_view), "accepts-tab");
6079 * gtk_text_view_get_accepts_tab:
6080 * @text_view: A #GtkTextView
6082 * Returns whether pressing the Tab key inserts a tab characters.
6083 * gtk_text_view_set_accepts_tab().
6085 * Return value: %TRUE if pressing the Tab key inserts a tab character,
6086 * %FALSE if pressing the Tab key moves the keyboard focus.
6091 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
6093 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6095 return text_view->priv->accepts_tab;
6103 gtk_text_view_unselect (GtkTextView *text_view)
6107 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6108 gtk_text_buffer_get_insert (get_buffer (text_view)));
6110 gtk_text_buffer_move_mark (get_buffer (text_view),
6111 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
6116 get_iter_at_pointer (GtkTextView *text_view,
6122 GtkTextViewPrivate *priv;
6123 gint xcoord, ycoord;
6124 GdkModifierType state;
6126 priv = text_view->priv;
6128 gdk_window_get_device_position (priv->text_window->bin_window,
6129 device, &xcoord, &ycoord, &state);
6131 gtk_text_layout_get_iter_at_pixel (priv->layout,
6133 xcoord + priv->xoffset,
6134 ycoord + priv->yoffset);
6143 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
6144 const gchar *mark_name,
6147 GtkTextIter newplace;
6150 get_iter_at_pointer (text_view, device, &newplace, NULL, NULL);
6152 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
6154 /* This may invalidate the layout */
6155 DV(g_print (G_STRLOC": move mark\n"));
6157 gtk_text_buffer_move_mark (get_buffer (text_view),
6161 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6162 gtk_text_view_scroll_mark_onscreen (text_view, mark);
6164 DV (g_print ("first validate idle leaving %s is %d\n",
6165 G_STRLOC, text_view->priv->first_validate_idle));
6169 selection_scan_timeout (gpointer data)
6171 GtkTextView *text_view;
6173 text_view = GTK_TEXT_VIEW (data);
6175 gtk_text_view_scroll_mark_onscreen (text_view,
6176 gtk_text_buffer_get_insert (get_buffer (text_view)));
6178 return TRUE; /* remain installed. */
6181 #define UPPER_OFFSET_ANCHOR 0.8
6182 #define LOWER_OFFSET_ANCHOR 0.2
6185 check_scroll (gdouble offset, GtkAdjustment *adjustment)
6187 if ((offset > UPPER_OFFSET_ANCHOR &&
6188 gtk_adjustment_get_value (adjustment) + gtk_adjustment_get_page_size (adjustment) < gtk_adjustment_get_upper (adjustment)) ||
6189 (offset < LOWER_OFFSET_ANCHOR &&
6190 gtk_adjustment_get_value (adjustment) > gtk_adjustment_get_lower (adjustment)))
6197 drag_scan_timeout (gpointer data)
6199 GtkTextView *text_view;
6200 GtkTextViewPrivate *priv;
6201 GtkTextIter newplace;
6203 gdouble pointer_xoffset, pointer_yoffset;
6205 text_view = GTK_TEXT_VIEW (data);
6206 priv = text_view->priv;
6208 get_iter_at_pointer (text_view, priv->dnd_device, &newplace, &x, &y);
6210 gtk_text_buffer_move_mark (get_buffer (text_view),
6214 pointer_xoffset = (gdouble) x / gdk_window_get_width (priv->text_window->bin_window);
6215 pointer_yoffset = (gdouble) y / gdk_window_get_height (priv->text_window->bin_window);
6217 if (check_scroll (pointer_xoffset, priv->hadjustment) ||
6218 check_scroll (pointer_yoffset, priv->vadjustment))
6220 /* do not make offsets surpass lower nor upper anchors, this makes
6221 * scrolling speed relative to the distance of the pointer to the
6222 * anchors when it moves beyond them.
6224 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6225 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6227 gtk_text_view_scroll_to_mark (text_view,
6229 0., TRUE, pointer_xoffset, pointer_yoffset);
6240 } SelectionGranularity;
6243 * Move @start and @end to the boundaries of the selection unit (indicated by
6244 * @granularity) which contained @start initially.
6245 * If the selction unit is SELECT_WORDS and @start is not contained in a word
6246 * the selection is extended to all the white spaces between the end of the
6247 * word preceding @start and the start of the one following.
6250 extend_selection (GtkTextView *text_view,
6251 SelectionGranularity granularity,
6257 if (granularity == SELECT_WORDS)
6259 if (gtk_text_iter_inside_word (start))
6261 if (!gtk_text_iter_starts_word (start))
6262 gtk_text_iter_backward_visible_word_start (start);
6264 if (!gtk_text_iter_ends_word (end))
6266 if (!gtk_text_iter_forward_visible_word_end (end))
6267 gtk_text_iter_forward_to_end (end);
6275 if (gtk_text_iter_backward_visible_word_start (&tmp))
6276 gtk_text_iter_forward_visible_word_end (&tmp);
6278 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
6281 gtk_text_iter_set_line_offset (start, 0);
6284 if (!gtk_text_iter_forward_visible_word_end (&tmp))
6285 gtk_text_iter_forward_to_end (&tmp);
6287 if (gtk_text_iter_ends_word (&tmp))
6288 gtk_text_iter_backward_visible_word_start (&tmp);
6290 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
6293 gtk_text_iter_forward_to_line_end (end);
6296 else if (granularity == SELECT_LINES)
6298 if (gtk_text_view_starts_display_line (text_view, start))
6300 /* If on a display line boundary, we assume the user
6301 * clicked off the end of a line and we therefore select
6302 * the line before the boundary.
6304 gtk_text_view_backward_display_line_start (text_view, start);
6308 /* start isn't on the start of a line, so we move it to the
6309 * start, and move end to the end unless it's already there.
6311 gtk_text_view_backward_display_line_start (text_view, start);
6313 if (!gtk_text_view_starts_display_line (text_view, end))
6314 gtk_text_view_forward_display_line_end (text_view, end);
6322 SelectionGranularity granularity;
6323 GtkTextMark *orig_start;
6324 GtkTextMark *orig_end;
6328 selection_data_free (SelectionData *data)
6330 if (data->orig_start != NULL)
6331 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6333 if (data->orig_end != NULL)
6334 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6340 selection_motion_event_handler (GtkTextView *text_view,
6341 GdkEventMotion *event,
6342 SelectionData *data)
6344 GtkTextViewPrivate *priv;
6346 priv = text_view->priv;
6347 gdk_event_request_motions (event);
6349 if (priv->grab_device != event->device)
6352 if (data->granularity == SELECT_CHARACTERS)
6354 move_mark_to_pointer_and_scroll (text_view, "insert", event->device);
6358 GtkTextIter cursor, start, end;
6359 GtkTextIter orig_start, orig_end;
6360 GtkTextBuffer *buffer;
6362 buffer = get_buffer (text_view);
6364 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6365 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6367 get_iter_at_pointer (text_view, event->device, &cursor, NULL, NULL);
6370 extend_selection (text_view, data->granularity, &start, &end);
6372 /* either the selection extends to the front, or end (or not) */
6373 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6374 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6376 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6378 gtk_text_view_scroll_mark_onscreen (text_view,
6379 gtk_text_buffer_get_insert (buffer));
6382 /* If we had to scroll offscreen, insert a timeout to do so
6383 * again. Note that in the timeout, even if the mouse doesn't
6384 * move, due to this scroll xoffset/yoffset will have changed
6385 * and we'll need to scroll again.
6387 if (text_view->priv->scroll_timeout != 0) /* reset on every motion event */
6388 g_source_remove (text_view->priv->scroll_timeout);
6390 text_view->priv->scroll_timeout =
6391 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6397 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6398 const GtkTextIter *iter,
6399 GdkEventButton *button)
6401 GtkTextViewPrivate *priv;
6402 GtkTextIter cursor, ins, bound;
6403 GtkTextIter orig_start, orig_end;
6404 GtkTextBuffer *buffer;
6405 SelectionData *data;
6407 if (text_view->priv->selection_drag_handler != 0)
6410 priv = text_view->priv;
6411 data = g_new0 (SelectionData, 1);
6413 if (button->type == GDK_2BUTTON_PRESS)
6414 data->granularity = SELECT_WORDS;
6415 else if (button->type == GDK_3BUTTON_PRESS)
6416 data->granularity = SELECT_LINES;
6418 data->granularity = SELECT_CHARACTERS;
6420 priv->grab_device = button->device;
6421 gtk_device_grab_add (GTK_WIDGET (text_view),
6425 buffer = get_buffer (text_view);
6430 extend_selection (text_view, data->granularity, &ins, &bound);
6434 if (button->state & GDK_SHIFT_MASK)
6436 /* Extend selection */
6437 GtkTextIter old_ins, old_bound;
6438 GtkTextIter old_start, old_end;
6440 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6441 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6442 old_start = old_ins;
6443 old_end = old_bound;
6444 gtk_text_iter_order (&old_start, &old_end);
6446 /* move the front cursor, if the mouse is in front of the selection. Should the
6447 * cursor however be inside the selection (this happens on tripple click) then we
6448 * move the side which was last moved (current insert mark) */
6449 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6450 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6451 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6454 orig_start = old_end;
6466 gtk_text_buffer_select_range (buffer, &ins, &bound);
6468 gtk_text_iter_order (&orig_start, &orig_end);
6469 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6471 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6473 gtk_text_view_check_cursor_blink (text_view);
6475 text_view->priv->selection_drag_handler = g_signal_connect_data (text_view,
6476 "motion-notify-event",
6477 G_CALLBACK (selection_motion_event_handler),
6479 (GClosureNotify) selection_data_free, 0);
6482 /* returns whether we were really dragging */
6484 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6486 GtkTextViewPrivate *priv;
6488 priv = text_view->priv;
6490 if (!priv->grab_device)
6493 if (priv->selection_drag_handler == 0)
6496 g_signal_handler_disconnect (text_view, priv->selection_drag_handler);
6497 priv->selection_drag_handler = 0;
6499 if (priv->scroll_timeout != 0)
6501 g_source_remove (priv->scroll_timeout);
6502 priv->scroll_timeout = 0;
6505 gtk_device_grab_remove (GTK_WIDGET (text_view),
6507 priv->grab_device = NULL;
6517 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6518 GtkTextAttributes *values)
6520 GtkStyleContext *context;
6521 GdkRGBA bg_color, fg_color;
6522 GtkStateFlags state;
6524 context = gtk_widget_get_style_context (GTK_WIDGET (text_view));
6525 state = gtk_widget_get_state_flags (GTK_WIDGET (text_view));
6527 gtk_style_context_save (context);
6528 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
6530 gtk_style_context_get_background_color (context, state, &bg_color);
6531 gtk_style_context_get_color (context, state, &fg_color);
6533 values->appearance.bg_color.red = CLAMP (bg_color.red * 65535. + 0.5, 0, 65535);
6534 values->appearance.bg_color.green = CLAMP (bg_color.green * 65535. + 0.5, 0, 65535);
6535 values->appearance.bg_color.blue = CLAMP (bg_color.blue * 65535. + 0.5, 0, 65535);
6537 values->appearance.fg_color.red = CLAMP (fg_color.red * 65535. + 0.5, 0, 65535);
6538 values->appearance.fg_color.green = CLAMP (fg_color.green * 65535. + 0.5, 0, 65535);
6539 values->appearance.fg_color.blue = CLAMP (fg_color.blue * 65535. + 0.5, 0, 65535);
6542 pango_font_description_free (values->font);
6544 values->font = pango_font_description_copy (gtk_style_context_get_font (context, state));
6546 gtk_style_context_restore (context);
6550 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6552 GtkTextViewPrivate *priv = text_view->priv;
6556 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6557 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6558 GtkTextDirection new_cursor_dir;
6559 GtkTextDirection new_keyboard_dir;
6560 gboolean split_cursor;
6562 g_object_get (settings,
6563 "gtk-split-cursor", &split_cursor,
6566 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6567 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6569 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6572 new_cursor_dir = GTK_TEXT_DIR_NONE;
6574 new_cursor_dir = new_keyboard_dir;
6576 gtk_text_layout_set_cursor_direction (priv->layout, new_cursor_dir);
6577 gtk_text_layout_set_keyboard_direction (priv->layout, new_keyboard_dir);
6582 gtk_text_view_ensure_layout (GtkTextView *text_view)
6585 GtkTextViewPrivate *priv;
6587 widget = GTK_WIDGET (text_view);
6588 priv = text_view->priv;
6590 if (priv->layout == NULL)
6592 GtkTextAttributes *style;
6593 PangoContext *ltr_context, *rtl_context;
6596 DV(g_print(G_STRLOC"\n"));
6598 priv->layout = gtk_text_layout_new ();
6600 g_signal_connect (priv->layout,
6602 G_CALLBACK (invalidated_handler),
6605 g_signal_connect (priv->layout,
6607 G_CALLBACK (changed_handler),
6610 g_signal_connect (priv->layout,
6612 G_CALLBACK (gtk_text_view_child_allocated),
6615 if (get_buffer (text_view))
6616 gtk_text_layout_set_buffer (priv->layout, get_buffer (text_view));
6618 if ((gtk_widget_has_focus (widget) && priv->cursor_visible))
6619 gtk_text_view_pend_cursor_blink (text_view);
6621 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
6623 gtk_text_layout_set_overwrite_mode (priv->layout,
6624 priv->overwrite_mode && priv->editable);
6626 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6627 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6628 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6629 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6631 gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
6633 g_object_unref (ltr_context);
6634 g_object_unref (rtl_context);
6636 gtk_text_view_check_keymap_direction (text_view);
6638 style = gtk_text_attributes_new ();
6640 gtk_text_view_set_attributes_from_style (text_view, style);
6642 style->pixels_above_lines = priv->pixels_above_lines;
6643 style->pixels_below_lines = priv->pixels_below_lines;
6644 style->pixels_inside_wrap = priv->pixels_inside_wrap;
6645 style->left_margin = priv->left_margin;
6646 style->right_margin = priv->right_margin;
6647 style->indent = priv->indent;
6648 style->tabs = priv->tabs ? pango_tab_array_copy (priv->tabs) : NULL;
6650 style->wrap_mode = priv->wrap_mode;
6651 style->justification = priv->justify;
6652 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6654 gtk_text_layout_set_default_style (priv->layout, style);
6656 gtk_text_attributes_unref (style);
6658 /* Set layout for all anchored children */
6660 tmp_list = priv->children;
6661 while (tmp_list != NULL)
6663 GtkTextViewChild *vc = tmp_list->data;
6667 gtk_text_anchored_child_set_layout (vc->widget,
6669 /* vc may now be invalid! */
6672 tmp_list = g_slist_next (tmp_list);
6675 gtk_text_view_invalidate (text_view);
6680 * gtk_text_view_get_default_attributes:
6681 * @text_view: a #GtkTextView
6683 * Obtains a copy of the default text attributes. These are the
6684 * attributes used for text unless a tag overrides them.
6685 * You'd typically pass the default attributes in to
6686 * gtk_text_iter_get_attributes() in order to get the
6687 * attributes in effect at a given text position.
6689 * The return value is a copy owned by the caller of this function,
6690 * and should be freed.
6692 * Return value: a new #GtkTextAttributes
6695 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6697 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6699 gtk_text_view_ensure_layout (text_view);
6701 return gtk_text_attributes_copy (text_view->priv->layout->default_style);
6705 gtk_text_view_destroy_layout (GtkTextView *text_view)
6707 GtkTextViewPrivate *priv = text_view->priv;
6713 gtk_text_view_remove_validate_idles (text_view);
6715 g_signal_handlers_disconnect_by_func (priv->layout,
6716 invalidated_handler,
6718 g_signal_handlers_disconnect_by_func (priv->layout,
6722 /* Remove layout from all anchored children */
6723 tmp_list = priv->children;
6724 while (tmp_list != NULL)
6726 GtkTextViewChild *vc = tmp_list->data;
6730 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6731 /* vc may now be invalid! */
6734 tmp_list = g_slist_next (tmp_list);
6737 gtk_text_view_stop_cursor_blink (text_view);
6738 gtk_text_view_end_selection_drag (text_view);
6740 g_object_unref (priv->layout);
6741 priv->layout = NULL;
6746 * gtk_text_view_reset_im_context:
6747 * @text_view: a #GtkTextView
6749 * Reset the input method context of the text view if needed.
6751 * This can be necessary in the case where modifying the buffer
6752 * would confuse on-going input method behavior.
6757 gtk_text_view_reset_im_context (GtkTextView *text_view)
6759 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6761 if (text_view->priv->need_im_reset)
6763 text_view->priv->need_im_reset = FALSE;
6764 gtk_im_context_reset (text_view->priv->im_context);
6769 * gtk_text_view_im_context_filter_keypress:
6770 * @text_view: a #GtkTextView
6771 * @event: the key event
6773 * Allow the #GtkTextView input method to internally handle key press
6774 * and release events. If this function returns %TRUE, then no further
6775 * processing should be done for this key event. See
6776 * gtk_im_context_filter_keypress().
6778 * Note that you are expected to call this function from your handler
6779 * when overriding key event handling. This is needed in the case when
6780 * you need to insert your own key handling between the input method
6781 * and the default key event handling of the #GtkTextView.
6785 * gtk_foo_bar_key_press_event (GtkWidget *widget,
6786 * GdkEventKey *event)
6788 * if ((key->keyval == GDK_KEY_Return || key->keyval == GDK_KEY_KP_Enter))
6790 * if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (view), event))
6794 * /* Do some stuff */
6796 * return GTK_WIDGET_CLASS (gtk_foo_bar_parent_class)->key_press_event (widget, event);
6800 * Return value: %TRUE if the input method handled the key event.
6805 gtk_text_view_im_context_filter_keypress (GtkTextView *text_view,
6808 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6810 return gtk_im_context_filter_keypress (text_view->priv->im_context, event);
6818 drag_begin_cb (GtkWidget *widget,
6819 GdkDragContext *context,
6822 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6823 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6826 cairo_surface_t *surface = NULL;
6828 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6830 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6831 surface = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6835 gtk_drag_set_icon_surface (context, surface);
6836 cairo_surface_destroy (surface);
6840 gtk_drag_set_icon_default (context);
6845 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6846 const GtkTextIter *iter,
6847 GdkEventMotion *event)
6849 GtkTargetList *target_list;
6851 text_view->priv->drag_start_x = -1;
6852 text_view->priv->drag_start_y = -1;
6853 text_view->priv->pending_place_cursor_button = 0;
6855 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6857 g_signal_connect (text_view, "drag-begin",
6858 G_CALLBACK (drag_begin_cb), NULL);
6859 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6860 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6861 1, (GdkEvent*)event);
6865 gtk_text_view_drag_begin (GtkWidget *widget,
6866 GdkDragContext *context)
6872 gtk_text_view_drag_end (GtkWidget *widget,
6873 GdkDragContext *context)
6878 gtk_text_view_drag_data_get (GtkWidget *widget,
6879 GdkDragContext *context,
6880 GtkSelectionData *selection_data,
6884 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6885 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6887 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6889 gtk_selection_data_set (selection_data,
6890 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6895 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6902 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6904 /* Extract the selected text */
6905 str = gtk_text_buffer_serialize (buffer, buffer,
6906 gtk_selection_data_get_target (selection_data),
6913 gtk_selection_data_set (selection_data,
6914 gtk_selection_data_get_target (selection_data),
6916 (guchar *) str, len);
6926 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6928 /* Extract the selected text */
6929 str = gtk_text_iter_get_visible_text (&start, &end);
6934 gtk_selection_data_set_text (selection_data, str, -1);
6941 gtk_text_view_drag_data_delete (GtkWidget *widget,
6942 GdkDragContext *context)
6944 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->priv->buffer,
6945 TRUE, GTK_TEXT_VIEW (widget)->priv->editable);
6949 gtk_text_view_drag_leave (GtkWidget *widget,
6950 GdkDragContext *context,
6953 GtkTextView *text_view;
6954 GtkTextViewPrivate *priv;
6956 text_view = GTK_TEXT_VIEW (widget);
6957 priv = text_view->priv;
6959 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
6961 if (priv->dnd_device)
6962 priv->dnd_device = NULL;
6964 if (priv->scroll_timeout != 0)
6965 g_source_remove (priv->scroll_timeout);
6967 priv->scroll_timeout = 0;
6971 gtk_text_view_drag_motion (GtkWidget *widget,
6972 GdkDragContext *context,
6977 GtkTextIter newplace;
6978 GtkTextView *text_view;
6979 GtkTextViewPrivate *priv;
6982 GdkRectangle target_rect;
6985 GdkDragAction suggested_action = 0;
6987 text_view = GTK_TEXT_VIEW (widget);
6988 priv = text_view->priv;
6990 target_rect = priv->text_window->allocation;
6992 if (x < target_rect.x ||
6993 y < target_rect.y ||
6994 x > (target_rect.x + target_rect.width) ||
6995 y > (target_rect.y + target_rect.height))
6996 return FALSE; /* outside the text window, allow parent widgets to handle event */
6998 gtk_text_view_window_to_buffer_coords (text_view,
6999 GTK_TEXT_WINDOW_WIDGET,
7003 gtk_text_layout_get_iter_at_pixel (priv->layout,
7007 target = gtk_drag_dest_find_target (widget, context,
7008 gtk_drag_dest_get_target_list (widget));
7010 if (target == GDK_NONE)
7012 /* can't accept any of the offered targets */
7014 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7016 gtk_text_iter_compare (&newplace, &start) >= 0 &&
7017 gtk_text_iter_compare (&newplace, &end) <= 0)
7019 /* We're inside the selection. */
7023 if (gtk_text_iter_can_insert (&newplace, priv->editable))
7025 GtkWidget *source_widget;
7027 suggested_action = gdk_drag_context_get_suggested_action (context);
7029 source_widget = gtk_drag_get_source_widget (context);
7031 if (source_widget == widget)
7033 /* Default to MOVE, unless the user has
7034 * pressed ctrl or alt to affect available actions
7036 if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
7037 suggested_action = GDK_ACTION_MOVE;
7042 /* Can't drop here. */
7046 if (suggested_action != 0)
7048 gtk_text_mark_set_visible (priv->dnd_mark,
7049 priv->cursor_visible);
7051 gdk_drag_status (context, suggested_action, time);
7055 gdk_drag_status (context, 0, time);
7056 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7059 priv->dnd_device = gdk_drag_context_get_device (context);
7061 if (!priv->scroll_timeout)
7062 priv->scroll_timeout =
7063 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
7065 /* TRUE return means don't propagate the drag motion to parent
7066 * widgets that may also be drop sites.
7072 gtk_text_view_drag_drop (GtkWidget *widget,
7073 GdkDragContext *context,
7078 GtkTextView *text_view;
7079 GtkTextViewPrivate *priv;
7080 GtkTextIter drop_point;
7081 GdkAtom target = GDK_NONE;
7083 text_view = GTK_TEXT_VIEW (widget);
7084 priv = text_view->priv;
7086 if (priv->scroll_timeout != 0)
7087 g_source_remove (priv->scroll_timeout);
7089 priv->scroll_timeout = 0;
7091 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7093 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7097 if (gtk_text_iter_can_insert (&drop_point, priv->editable))
7098 target = gtk_drag_dest_find_target (widget, context, NULL);
7100 if (target != GDK_NONE)
7101 gtk_drag_get_data (widget, context, target, time);
7103 gtk_drag_finish (context, FALSE, FALSE, time);
7109 insert_text_data (GtkTextView *text_view,
7110 GtkTextIter *drop_point,
7111 GtkSelectionData *selection_data)
7115 str = gtk_selection_data_get_text (selection_data);
7119 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
7120 drop_point, (gchar *) str, -1,
7121 text_view->priv->editable))
7123 gtk_widget_error_bell (GTK_WIDGET (text_view));
7131 gtk_text_view_drag_data_received (GtkWidget *widget,
7132 GdkDragContext *context,
7135 GtkSelectionData *selection_data,
7139 GtkTextIter drop_point;
7140 GtkTextView *text_view;
7141 GtkTextViewPrivate *priv;
7142 gboolean success = FALSE;
7143 GtkTextBuffer *buffer = NULL;
7145 text_view = GTK_TEXT_VIEW (widget);
7146 priv = text_view->priv;
7148 if (!priv->dnd_mark)
7151 buffer = get_buffer (text_view);
7153 gtk_text_buffer_get_iter_at_mark (buffer,
7157 if (!gtk_text_iter_can_insert (&drop_point, priv->editable))
7162 gtk_text_buffer_begin_user_action (buffer);
7164 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7166 GtkTextBuffer *src_buffer = NULL;
7167 GtkTextIter start, end;
7168 gboolean copy_tags = TRUE;
7170 if (gtk_selection_data_get_length (selection_data) != sizeof (src_buffer))
7173 memcpy (&src_buffer, gtk_selection_data_get_data (selection_data), sizeof (src_buffer));
7175 if (src_buffer == NULL)
7178 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
7180 if (gtk_text_buffer_get_tag_table (src_buffer) !=
7181 gtk_text_buffer_get_tag_table (buffer))
7183 /* try to find a suitable rich text target instead */
7187 GdkAtom target = GDK_NONE;
7191 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
7193 for (list = gdk_drag_context_list_targets (context); list; list = g_list_next (list))
7197 for (i = 0; i < n_atoms; i++)
7198 if (GUINT_TO_POINTER (atoms[i]) == list->data)
7207 if (target != GDK_NONE)
7209 gtk_drag_get_data (widget, context, target, time);
7210 gtk_text_buffer_end_user_action (buffer);
7215 if (gtk_text_buffer_get_selection_bounds (src_buffer,
7220 gtk_text_buffer_insert_range_interactive (buffer,
7229 str = gtk_text_iter_get_visible_text (&start, &end);
7230 gtk_text_buffer_insert_interactive (buffer,
7231 &drop_point, str, -1,
7237 else if (gtk_selection_data_get_length (selection_data) > 0 &&
7238 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
7241 GError *error = NULL;
7243 retval = gtk_text_buffer_deserialize (buffer, buffer,
7244 gtk_selection_data_get_target (selection_data),
7246 (guint8 *) gtk_selection_data_get_data (selection_data),
7247 gtk_selection_data_get_length (selection_data),
7252 g_warning ("error pasting: %s\n", error->message);
7253 g_clear_error (&error);
7257 insert_text_data (text_view, &drop_point, selection_data);
7260 gtk_drag_finish (context, success,
7261 success && gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE,
7266 gtk_text_buffer_get_iter_at_mark (buffer,
7269 gtk_text_buffer_place_cursor (buffer, &drop_point);
7271 gtk_text_buffer_end_user_action (buffer);
7276 * gtk_text_view_get_hadjustment:
7277 * @text_view: a #GtkTextView
7279 * Gets the horizontal-scrolling #GtkAdjustment.
7281 * Returns: (transfer none): pointer to the horizontal #GtkAdjustment
7285 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
7288 gtk_text_view_get_hadjustment (GtkTextView *text_view)
7290 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7292 return text_view->priv->hadjustment;
7296 gtk_text_view_set_hadjustment (GtkTextView *text_view,
7297 GtkAdjustment *adjustment)
7299 GtkTextViewPrivate *priv = text_view->priv;
7301 if (adjustment && priv->hadjustment == adjustment)
7304 if (priv->hadjustment != NULL)
7306 g_signal_handlers_disconnect_by_func (priv->hadjustment,
7307 gtk_text_view_value_changed,
7309 g_object_unref (priv->hadjustment);
7312 if (adjustment == NULL)
7313 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
7315 g_signal_connect (adjustment, "value-changed",
7316 G_CALLBACK (gtk_text_view_value_changed), text_view);
7317 priv->hadjustment = g_object_ref_sink (adjustment);
7318 gtk_text_view_set_hadjustment_values (text_view);
7320 g_object_notify (G_OBJECT (text_view), "hadjustment");
7324 * gtk_text_view_get_vadjustment:
7325 * @text_view: a #GtkTextView
7327 * Gets the vertical-scrolling #GtkAdjustment.
7329 * Returns: (transfer none): pointer to the vertical #GtkAdjustment
7333 * Deprecated: 3.0: Use gtk_scrollable_get_vadjustment()
7336 gtk_text_view_get_vadjustment (GtkTextView *text_view)
7338 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7340 return text_view->priv->vadjustment;
7344 gtk_text_view_set_vadjustment (GtkTextView *text_view,
7345 GtkAdjustment *adjustment)
7347 GtkTextViewPrivate *priv = text_view->priv;
7349 if (adjustment && priv->vadjustment == adjustment)
7352 if (priv->vadjustment != NULL)
7354 g_signal_handlers_disconnect_by_func (priv->vadjustment,
7355 gtk_text_view_value_changed,
7357 g_object_unref (priv->vadjustment);
7360 if (adjustment == NULL)
7361 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
7363 g_signal_connect (adjustment, "value-changed",
7364 G_CALLBACK (gtk_text_view_value_changed), text_view);
7365 priv->vadjustment = g_object_ref_sink (adjustment);
7366 gtk_text_view_set_vadjustment_values (text_view);
7368 g_object_notify (G_OBJECT (text_view), "vadjustment");
7372 gtk_text_view_set_hadjustment_values (GtkTextView *text_view)
7374 GtkTextViewPrivate *priv;
7380 priv = text_view->priv;
7382 screen_width = SCREEN_WIDTH (text_view);
7383 old_value = gtk_adjustment_get_value (priv->hadjustment);
7384 new_upper = MAX (screen_width, priv->width);
7386 g_object_set (priv->hadjustment,
7389 "page-size", (gdouble)screen_width,
7390 "step-increment", screen_width * 0.1,
7391 "page-increment", screen_width * 0.9,
7394 new_value = CLAMP (old_value, 0, new_upper - screen_width);
7395 if (new_value != old_value)
7396 gtk_adjustment_set_value (priv->hadjustment, new_value);
7400 gtk_text_view_set_vadjustment_values (GtkTextView *text_view)
7402 GtkTextViewPrivate *priv;
7403 GtkTextIter first_para;
7410 priv = text_view->priv;
7412 screen_height = SCREEN_HEIGHT (text_view);
7413 old_value = gtk_adjustment_get_value (priv->vadjustment);
7414 new_upper = MAX (screen_height, priv->height);
7416 g_object_set (priv->vadjustment,
7419 "page-size", (gdouble)screen_height,
7420 "step-increment", screen_height * 0.1,
7421 "page-increment", screen_height * 0.9,
7424 /* Now adjust the value of the adjustment to keep the cursor at the
7425 * same place in the buffer */
7426 gtk_text_view_ensure_layout (text_view);
7427 gtk_text_view_get_first_para_iter (text_view, &first_para);
7428 gtk_text_layout_get_line_yrange (priv->layout, &first_para, &y, NULL);
7430 y += priv->first_para_pixels;
7432 new_value = CLAMP (y, 0, new_upper - screen_height);
7433 if (new_value != old_value)
7434 gtk_adjustment_set_value (priv->vadjustment, new_value);
7438 /* FIXME this adjust_allocation is a big cut-and-paste from
7439 * GtkCList, needs to be some "official" way to do this
7449 /* The window to which widget->window is relative */
7450 #define ALLOCATION_WINDOW(widget) \
7451 (!gtk_widget_get_has_window (widget) ? \
7452 gtk_widget_get_window (widget) : \
7453 gdk_window_get_parent (gtk_widget_get_window (widget)))
7456 adjust_allocation_recurse (GtkWidget *widget,
7459 GtkAllocation allocation;
7460 ScrollData *scroll_data = data;
7462 /* Need to really size allocate instead of just poking
7463 * into widget->allocation if the widget is not realized.
7464 * FIXME someone figure out why this was.
7466 if (!gtk_widget_get_realized (widget))
7468 if (gtk_widget_get_visible (widget))
7470 GdkRectangle tmp_rectangle;
7472 tmp_rectangle = allocation;
7473 tmp_rectangle.x += scroll_data->dx;
7474 tmp_rectangle.y += scroll_data->dy;
7476 gtk_widget_size_allocate (widget, &tmp_rectangle);
7481 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
7483 allocation.x += scroll_data->dx;
7484 allocation.y += scroll_data->dy;
7485 gtk_widget_set_allocation (widget, &allocation);
7487 if (GTK_IS_CONTAINER (widget))
7488 gtk_container_forall (GTK_CONTAINER (widget),
7489 adjust_allocation_recurse,
7496 adjust_allocation (GtkWidget *widget,
7500 ScrollData scroll_data;
7502 if (gtk_widget_get_realized (widget))
7503 scroll_data.window = ALLOCATION_WINDOW (widget);
7505 scroll_data.window = NULL;
7507 scroll_data.dx = dx;
7508 scroll_data.dy = dy;
7510 adjust_allocation_recurse (widget, &scroll_data);
7514 gtk_text_view_value_changed (GtkAdjustment *adjustment,
7515 GtkTextView *text_view)
7517 GtkTextViewPrivate *priv;
7523 priv = text_view->priv;
7525 /* Note that we oddly call this function with adjustment == NULL
7529 priv->onscreen_validated = FALSE;
7531 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7532 adjustment == priv->hadjustment ? "hadjustment" : adjustment == priv->vadjustment ? "vadjustment" : "none",
7533 adjustment ? gtk_adjustment_get_value (adjustment) : 0.0));
7535 if (adjustment == priv->hadjustment)
7537 dx = priv->xoffset - (gint)gtk_adjustment_get_value (adjustment);
7538 priv->xoffset = gtk_adjustment_get_value (adjustment);
7540 /* If the change is due to a size change we need
7541 * to invalidate the entire text window because there might be
7542 * right-aligned or centered text
7544 if (priv->width_changed)
7546 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7547 gdk_window_invalidate_rect (priv->text_window->bin_window, NULL, FALSE);
7549 priv->width_changed = FALSE;
7552 else if (adjustment == priv->vadjustment)
7554 dy = priv->yoffset - (gint)gtk_adjustment_get_value (adjustment);
7555 priv->yoffset = gtk_adjustment_get_value (adjustment);
7559 gtk_text_layout_get_line_at_y (priv->layout, &iter, gtk_adjustment_get_value (adjustment), &line_top);
7561 gtk_text_buffer_move_mark (get_buffer (text_view), priv->first_para_mark, &iter);
7563 priv->first_para_pixels = gtk_adjustment_get_value (adjustment) - line_top;
7567 if (dx != 0 || dy != 0)
7571 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7575 if (priv->left_window)
7576 text_window_scroll (priv->left_window, 0, dy);
7577 if (priv->right_window)
7578 text_window_scroll (priv->right_window, 0, dy);
7583 if (priv->top_window)
7584 text_window_scroll (priv->top_window, dx, 0);
7585 if (priv->bottom_window)
7586 text_window_scroll (priv->bottom_window, dx, 0);
7589 /* It looks nicer to scroll the main area last, because
7590 * it takes a while, and making the side areas update
7591 * afterward emphasizes the slowness of scrolling the
7594 text_window_scroll (priv->text_window, dx, dy);
7597 /* Children are now "moved" in the text window, poke
7598 * into widget->allocation for each child
7600 tmp_list = priv->children;
7601 while (tmp_list != NULL)
7603 GtkTextViewChild *child = tmp_list->data;
7606 adjust_allocation (child->widget, dx, dy);
7608 tmp_list = g_slist_next (tmp_list);
7612 /* This could result in invalidation, which would install the
7613 * first_validate_idle, which would validate onscreen;
7614 * but we're going to go ahead and validate here, so
7615 * first_validate_idle shouldn't have anything to do.
7617 gtk_text_view_update_layout_width (text_view);
7619 /* We also update the IM spot location here, since the IM context
7620 * might do something that leads to validation.
7622 gtk_text_view_update_im_spot_location (text_view);
7624 /* note that validation of onscreen could invoke this function
7625 * recursively, by scrolling to maintain first_para, or in response
7626 * to updating the layout width, however there is no problem with
7627 * that, or shouldn't be.
7629 gtk_text_view_validate_onscreen (text_view);
7631 /* process exposes */
7632 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7634 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7636 if (priv->left_window)
7637 gdk_window_process_updates (priv->left_window->bin_window, TRUE);
7639 if (priv->right_window)
7640 gdk_window_process_updates (priv->right_window->bin_window, TRUE);
7642 if (priv->top_window)
7643 gdk_window_process_updates (priv->top_window->bin_window, TRUE);
7645 if (priv->bottom_window)
7646 gdk_window_process_updates (priv->bottom_window->bin_window, TRUE);
7648 gdk_window_process_updates (priv->text_window->bin_window, TRUE);
7651 /* If this got installed, get rid of it, it's just a waste of time. */
7652 if (priv->first_validate_idle != 0)
7654 g_source_remove (priv->first_validate_idle);
7655 priv->first_validate_idle = 0;
7658 /* Finally we update the IM cursor location again, to ensure any
7659 * changes made by the validation are pushed through.
7661 gtk_text_view_update_im_spot_location (text_view);
7663 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7667 gtk_text_view_commit_handler (GtkIMContext *context,
7669 GtkTextView *text_view)
7671 gtk_text_view_commit_text (text_view, str);
7675 gtk_text_view_commit_text (GtkTextView *text_view,
7678 GtkTextViewPrivate *priv;
7679 gboolean had_selection;
7681 priv = text_view->priv;
7683 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7685 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7688 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7691 if (!strcmp (str, "\n"))
7693 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7696 gtk_widget_error_bell (GTK_WIDGET (text_view));
7701 if (!had_selection && priv->overwrite_mode)
7705 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7707 gtk_text_buffer_get_insert (get_buffer (text_view)));
7708 if (!gtk_text_iter_ends_line (&insert))
7709 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7712 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7715 gtk_widget_error_bell (GTK_WIDGET (text_view));
7719 gtk_text_buffer_end_user_action (get_buffer (text_view));
7721 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7722 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7723 gtk_text_view_scroll_mark_onscreen (text_view,
7724 gtk_text_buffer_get_insert (get_buffer (text_view)));
7728 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7729 GtkTextView *text_view)
7731 GtkTextViewPrivate *priv;
7733 PangoAttrList *attrs;
7737 priv = text_view->priv;
7739 gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter,
7740 gtk_text_buffer_get_insert (priv->buffer));
7742 /* Keypress events are passed to input method even if cursor position is
7743 * not editable; so beep here if it's multi-key input sequence, input
7744 * method will be reset in key-press-event handler.
7746 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7748 if (str && str[0] && !gtk_text_iter_can_insert (&iter, priv->editable))
7750 gtk_widget_error_bell (GTK_WIDGET (text_view));
7754 g_signal_emit (text_view, signals[PREEDIT_CHANGED], 0, str);
7757 gtk_text_layout_set_preedit_string (priv->layout, str, attrs, cursor_pos);
7758 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
7759 gtk_text_view_scroll_mark_onscreen (text_view,
7760 gtk_text_buffer_get_insert (get_buffer (text_view)));
7763 pango_attr_list_unref (attrs);
7768 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7769 GtkTextView *text_view)
7776 gtk_text_buffer_get_iter_at_mark (text_view->priv->buffer, &start,
7777 gtk_text_buffer_get_insert (text_view->priv->buffer));
7780 pos = gtk_text_iter_get_line_index (&start);
7781 gtk_text_iter_set_line_offset (&start, 0);
7782 gtk_text_iter_forward_to_line_end (&end);
7784 text = gtk_text_iter_get_slice (&start, &end);
7785 gtk_im_context_set_surrounding (context, text, -1, pos);
7792 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7795 GtkTextView *text_view)
7797 GtkTextViewPrivate *priv;
7801 priv = text_view->priv;
7803 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start,
7804 gtk_text_buffer_get_insert (priv->buffer));
7807 gtk_text_iter_forward_chars (&start, offset);
7808 gtk_text_iter_forward_chars (&end, offset + n_chars);
7810 gtk_text_buffer_delete_interactive (priv->buffer, &start, &end,
7817 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7818 const GtkTextIter *location,
7822 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7823 gboolean need_reset = FALSE;
7825 if (mark == gtk_text_buffer_get_insert (buffer))
7827 text_view->priv->virtual_cursor_x = -1;
7828 text_view->priv->virtual_cursor_y = -1;
7829 gtk_text_view_update_im_spot_location (text_view);
7832 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7838 gtk_text_view_reset_im_context (text_view);
7842 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7843 const GParamSpec *pspec,
7846 GtkWidget *widget = GTK_WIDGET (data);
7847 GtkTargetList *view_list;
7848 GtkTargetList *buffer_list;
7851 view_list = gtk_drag_dest_get_target_list (widget);
7852 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7855 gtk_target_list_ref (view_list);
7857 view_list = gtk_target_list_new (NULL, 0);
7859 list = view_list->list;
7862 GtkTargetPair *pair = list->data;
7864 list = g_list_next (list); /* get next element before removing */
7866 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7867 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7869 gtk_target_list_remove (view_list, pair->target);
7873 for (list = buffer_list->list; list; list = g_list_next (list))
7875 GtkTargetPair *pair = list->data;
7877 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7880 gtk_drag_dest_set_target_list (widget, view_list);
7881 gtk_target_list_unref (view_list);
7885 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7890 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7891 gtk_text_buffer_get_insert (get_buffer (text_view)));
7893 gtk_text_layout_get_cursor_locations (text_view->priv->layout, &insert, pos, NULL);
7897 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7898 GtkTextIter *cursor,
7902 GtkTextViewPrivate *priv;
7906 priv = text_view->priv;
7911 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7912 gtk_text_buffer_get_insert (get_buffer (text_view)));
7914 if ((x && priv->virtual_cursor_x == -1) ||
7915 (y && priv->virtual_cursor_y == -1))
7916 gtk_text_layout_get_cursor_locations (priv->layout, &insert, &pos, NULL);
7920 if (priv->virtual_cursor_x != -1)
7921 *x = priv->virtual_cursor_x;
7928 if (priv->virtual_cursor_x != -1)
7929 *y = priv->virtual_cursor_y;
7931 *y = pos.y + pos.height / 2;
7936 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7942 if (!text_view->priv->layout)
7945 if (x == -1 || y == -1)
7946 gtk_text_view_get_cursor_location (text_view, &pos);
7948 text_view->priv->virtual_cursor_x = (x == -1) ? pos.x : x;
7949 text_view->priv->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7952 /* Quick hack of a popup menu
7955 activate_cb (GtkWidget *menuitem,
7956 GtkTextView *text_view)
7958 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7959 g_signal_emit_by_name (text_view, signal);
7963 append_action_signal (GtkTextView *text_view,
7965 const gchar *stock_id,
7966 const gchar *signal,
7969 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7971 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7972 g_signal_connect (menuitem, "activate",
7973 G_CALLBACK (activate_cb), text_view);
7975 gtk_widget_set_sensitive (menuitem, sensitive);
7977 gtk_widget_show (menuitem);
7978 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7982 gtk_text_view_select_all (GtkWidget *widget,
7985 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
7986 GtkTextBuffer *buffer;
7987 GtkTextIter start_iter, end_iter, insert;
7989 buffer = text_view->priv->buffer;
7992 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
7993 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
7997 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
7998 gtk_text_buffer_get_insert (buffer));
7999 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
8004 select_all_cb (GtkWidget *menuitem,
8005 GtkTextView *text_view)
8007 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
8011 delete_cb (GtkTextView *text_view)
8013 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
8014 text_view->priv->editable);
8018 popup_menu_detach (GtkWidget *attach_widget,
8021 GTK_TEXT_VIEW (attach_widget)->priv->popup_menu = NULL;
8025 popup_position_func (GtkMenu *menu,
8031 GtkAllocation allocation;
8032 GtkTextView *text_view;
8034 GdkRectangle cursor_rect;
8035 GdkRectangle onscreen_rect;
8036 gint root_x, root_y;
8041 GdkRectangle monitor;
8043 text_view = GTK_TEXT_VIEW (user_data);
8044 widget = GTK_WIDGET (text_view);
8046 g_return_if_fail (gtk_widget_get_realized (widget));
8048 screen = gtk_widget_get_screen (widget);
8050 gdk_window_get_origin (gtk_widget_get_window (widget),
8053 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
8055 gtk_text_buffer_get_insert (get_buffer (text_view)));
8057 gtk_text_view_get_iter_location (text_view,
8061 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
8063 gtk_widget_get_preferred_size (text_view->priv->popup_menu,
8066 gtk_widget_get_allocation (widget, &allocation);
8068 /* can't use rectangle_intersect since cursor rect can have 0 width */
8069 if (cursor_rect.x >= onscreen_rect.x &&
8070 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
8071 cursor_rect.y >= onscreen_rect.y &&
8072 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
8074 gtk_text_view_buffer_to_window_coords (text_view,
8075 GTK_TEXT_WINDOW_WIDGET,
8076 cursor_rect.x, cursor_rect.y,
8077 &cursor_rect.x, &cursor_rect.y);
8079 *x = root_x + cursor_rect.x + cursor_rect.width;
8080 *y = root_y + cursor_rect.y + cursor_rect.height;
8084 /* Just center the menu, since cursor is offscreen. */
8085 *x = root_x + (allocation.width / 2 - req.width / 2);
8086 *y = root_y + (allocation.height / 2 - req.height / 2);
8090 *x = CLAMP (*x, root_x, (root_x + allocation.width));
8091 *y = CLAMP (*y, root_y, (root_y + allocation.height));
8093 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
8094 gtk_menu_set_monitor (menu, monitor_num);
8095 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8097 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
8098 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
8105 GtkTextView *text_view;
8111 range_contains_editable_text (const GtkTextIter *start,
8112 const GtkTextIter *end,
8113 gboolean default_editability)
8115 GtkTextIter iter = *start;
8117 while (gtk_text_iter_compare (&iter, end) < 0)
8119 if (gtk_text_iter_editable (&iter, default_editability))
8122 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
8129 unichar_chosen_func (const char *text,
8132 GtkTextView *text_view = GTK_TEXT_VIEW (data);
8134 gtk_text_view_commit_text (text_view, text);
8138 popup_targets_received (GtkClipboard *clipboard,
8139 GtkSelectionData *data,
8142 PopupInfo *info = user_data;
8143 GtkTextView *text_view;
8144 GtkTextViewPrivate *priv;
8146 text_view = info->text_view;
8147 priv = text_view->priv;
8149 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
8151 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
8152 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
8154 gboolean clipboard_contains_text;
8155 GtkWidget *menuitem;
8157 gboolean have_selection;
8158 gboolean can_insert;
8160 GtkTextIter sel_start, sel_end;
8161 gboolean show_input_method_menu;
8162 gboolean show_unicode_menu;
8164 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8166 if (priv->popup_menu)
8167 gtk_widget_destroy (priv->popup_menu);
8169 priv->popup_menu = gtk_menu_new ();
8171 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
8172 GTK_WIDGET (text_view),
8175 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
8176 &sel_start, &sel_end);
8178 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
8180 gtk_text_buffer_get_insert (get_buffer (text_view)));
8182 can_insert = gtk_text_iter_can_insert (&iter, priv->editable);
8184 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8186 range_contains_editable_text (&sel_start, &sel_end,
8188 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8190 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8191 can_insert && clipboard_contains_text);
8193 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8194 gtk_widget_set_sensitive (menuitem,
8196 range_contains_editable_text (&sel_start, &sel_end,
8198 g_signal_connect_swapped (menuitem, "activate",
8199 G_CALLBACK (delete_cb), text_view);
8200 gtk_widget_show (menuitem);
8201 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8203 menuitem = gtk_separator_menu_item_new ();
8204 gtk_widget_show (menuitem);
8205 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8207 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8208 g_signal_connect (menuitem, "activate",
8209 G_CALLBACK (select_all_cb), text_view);
8210 gtk_widget_show (menuitem);
8211 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8213 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
8214 "gtk-show-input-method-menu", &show_input_method_menu,
8215 "gtk-show-unicode-menu", &show_unicode_menu,
8218 if (show_input_method_menu || show_unicode_menu)
8220 menuitem = gtk_separator_menu_item_new ();
8221 gtk_widget_show (menuitem);
8222 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8225 if (show_input_method_menu)
8227 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8228 gtk_widget_show (menuitem);
8229 gtk_widget_set_sensitive (menuitem, can_insert);
8231 submenu = gtk_menu_new ();
8232 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8233 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8235 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (priv->im_context),
8236 GTK_MENU_SHELL (submenu));
8239 if (show_unicode_menu)
8241 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8242 gtk_widget_show (menuitem);
8243 gtk_widget_set_sensitive (menuitem, can_insert);
8245 submenu = gtk_menu_new ();
8246 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8247 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8249 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8250 unichar_chosen_func,
8254 g_signal_emit (text_view,
8255 signals[POPULATE_POPUP],
8260 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
8262 info->button, info->time);
8265 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
8266 popup_position_func, text_view,
8267 0, gtk_get_current_event_time ());
8268 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
8272 g_object_unref (text_view);
8277 gtk_text_view_do_popup (GtkTextView *text_view,
8278 GdkEventButton *event)
8280 PopupInfo *info = g_new (PopupInfo, 1);
8282 /* In order to know what entries we should make sensitive, we
8283 * ask for the current targets of the clipboard, and when
8284 * we get them, then we actually pop up the menu.
8286 info->text_view = g_object_ref (text_view);
8290 info->button = event->button;
8291 info->time = event->time;
8296 info->time = gtk_get_current_event_time ();
8299 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
8300 GDK_SELECTION_CLIPBOARD),
8301 gdk_atom_intern_static_string ("TARGETS"),
8302 popup_targets_received,
8307 gtk_text_view_popup_menu (GtkWidget *widget)
8309 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
8313 /* Child GdkWindows */
8316 static GtkTextWindow*
8317 text_window_new (GtkTextWindowType type,
8320 gint height_request)
8324 win = g_new (GtkTextWindow, 1);
8327 win->widget = widget;
8329 win->bin_window = NULL;
8330 win->requisition.width = width_request;
8331 win->requisition.height = height_request;
8332 win->allocation.width = width_request;
8333 win->allocation.height = height_request;
8334 win->allocation.x = 0;
8335 win->allocation.y = 0;
8341 text_window_free (GtkTextWindow *win)
8344 text_window_unrealize (win);
8350 text_window_realize (GtkTextWindow *win,
8353 GtkStyleContext *context;
8354 GtkStateFlags state;
8356 GdkWindowAttr attributes;
8357 gint attributes_mask;
8361 attributes.window_type = GDK_WINDOW_CHILD;
8362 attributes.x = win->allocation.x;
8363 attributes.y = win->allocation.y;
8364 attributes.width = win->allocation.width;
8365 attributes.height = win->allocation.height;
8366 attributes.wclass = GDK_INPUT_OUTPUT;
8367 attributes.visual = gtk_widget_get_visual (win->widget);
8368 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
8370 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
8372 window = gtk_widget_get_window (widget);
8374 win->window = gdk_window_new (window,
8375 &attributes, attributes_mask);
8377 gdk_window_show (win->window);
8378 gdk_window_set_user_data (win->window, win->widget);
8379 gdk_window_lower (win->window);
8383 attributes.width = win->allocation.width;
8384 attributes.height = win->allocation.height;
8385 attributes.event_mask = (GDK_EXPOSURE_MASK |
8387 GDK_KEY_PRESS_MASK |
8388 GDK_BUTTON_PRESS_MASK |
8389 GDK_BUTTON_RELEASE_MASK |
8390 GDK_POINTER_MOTION_MASK |
8391 GDK_POINTER_MOTION_HINT_MASK |
8392 gtk_widget_get_events (win->widget));
8394 win->bin_window = gdk_window_new (win->window,
8398 gdk_window_show (win->bin_window);
8399 gdk_window_set_user_data (win->bin_window, win->widget);
8401 context = gtk_widget_get_style_context (widget);
8402 state = gtk_widget_get_state_flags (widget);
8404 if (win->type == GTK_TEXT_WINDOW_TEXT)
8406 if (gtk_widget_is_sensitive (widget))
8409 cursor = gdk_cursor_new_for_display (gdk_window_get_display (window),
8411 gdk_window_set_cursor (win->bin_window, cursor);
8412 g_object_unref (cursor);
8415 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->priv->im_context,
8418 gtk_style_context_save (context);
8419 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
8421 gtk_style_context_get_background_color (context, state, &color);
8422 gdk_window_set_background_rgba (win->bin_window, &color);
8424 gtk_style_context_restore (context);
8428 gtk_style_context_get_background_color (context, state, &color);
8429 gdk_window_set_background_rgba (win->bin_window, &color);
8432 g_object_set_qdata (G_OBJECT (win->window),
8433 g_quark_from_static_string ("gtk-text-view-text-window"),
8436 g_object_set_qdata (G_OBJECT (win->bin_window),
8437 g_quark_from_static_string ("gtk-text-view-text-window"),
8442 text_window_unrealize (GtkTextWindow *win)
8444 if (win->type == GTK_TEXT_WINDOW_TEXT)
8446 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->priv->im_context,
8450 gdk_window_set_user_data (win->window, NULL);
8451 gdk_window_set_user_data (win->bin_window, NULL);
8452 gdk_window_destroy (win->bin_window);
8453 gdk_window_destroy (win->window);
8455 win->bin_window = NULL;
8459 text_window_size_allocate (GtkTextWindow *win,
8462 win->allocation = *rect;
8466 gdk_window_move_resize (win->window,
8468 rect->width, rect->height);
8470 gdk_window_resize (win->bin_window,
8471 rect->width, rect->height);
8476 text_window_scroll (GtkTextWindow *win,
8480 if (dx != 0 || dy != 0)
8482 gdk_window_scroll (win->bin_window, dx, dy);
8487 text_window_invalidate_rect (GtkTextWindow *win,
8490 GdkRectangle window_rect;
8492 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
8499 window_rect.width = rect->width;
8500 window_rect.height = rect->height;
8502 /* Adjust the rect as appropriate */
8506 case GTK_TEXT_WINDOW_TEXT:
8509 case GTK_TEXT_WINDOW_LEFT:
8510 case GTK_TEXT_WINDOW_RIGHT:
8512 window_rect.width = win->allocation.width;
8515 case GTK_TEXT_WINDOW_TOP:
8516 case GTK_TEXT_WINDOW_BOTTOM:
8518 window_rect.height = win->allocation.height;
8522 g_warning ("%s: bug!", G_STRFUNC);
8527 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
8531 cairo_t *cr = gdk_cairo_create (win->bin_window);
8532 gdk_cairo_rectangle (cr, &window_rect);
8533 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
8541 text_window_invalidate_cursors (GtkTextWindow *win)
8543 GtkTextView *text_view;
8544 GtkTextViewPrivate *priv;
8546 GdkRectangle strong;
8548 gboolean draw_arrow;
8549 gfloat cursor_aspect_ratio;
8553 text_view = GTK_TEXT_VIEW (win->widget);
8554 priv = text_view->priv;
8556 gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter,
8557 gtk_text_buffer_get_insert (priv->buffer));
8559 if (_gtk_text_layout_get_block_cursor (priv->layout, &strong))
8561 text_window_invalidate_rect (win, &strong);
8565 gtk_text_layout_get_cursor_locations (priv->layout, &iter,
8568 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
8569 * ignoring the text direction be exposing both sides of the cursor
8572 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
8574 gtk_widget_style_get (win->widget,
8575 "cursor-aspect-ratio", &cursor_aspect_ratio,
8578 stem_width = strong.height * cursor_aspect_ratio + 1;
8579 arrow_width = stem_width + 1;
8581 strong.width = stem_width;
8583 /* round up to the next even number */
8587 strong.x -= stem_width / 2;
8588 strong.width += stem_width;
8592 strong.x -= arrow_width;
8593 strong.width += arrow_width * 2;
8596 text_window_invalidate_rect (win, &strong);
8598 if (draw_arrow) /* == have weak */
8600 stem_width = weak.height * cursor_aspect_ratio + 1;
8601 arrow_width = stem_width + 1;
8603 weak.width = stem_width;
8605 /* round up to the next even number */
8609 weak.x -= stem_width / 2;
8610 weak.width += stem_width;
8612 weak.x -= arrow_width;
8613 weak.width += arrow_width * 2;
8615 text_window_invalidate_rect (win, &weak);
8620 text_window_get_width (GtkTextWindow *win)
8622 return win->allocation.width;
8626 text_window_get_height (GtkTextWindow *win)
8628 return win->allocation.height;
8635 * gtk_text_view_get_window:
8636 * @text_view: a #GtkTextView
8637 * @win: window to get
8639 * Retrieves the #GdkWindow corresponding to an area of the text view;
8640 * possible windows include the overall widget window, child windows
8641 * on the left, right, top, bottom, and the window that displays the
8642 * text buffer. Windows are %NULL and nonexistent if their width or
8643 * height is 0, and are nonexistent before the widget has been
8646 * Return value: (transfer none): a #GdkWindow, or %NULL
8649 gtk_text_view_get_window (GtkTextView *text_view,
8650 GtkTextWindowType win)
8652 GtkTextViewPrivate *priv = text_view->priv;
8654 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8658 case GTK_TEXT_WINDOW_WIDGET:
8659 return gtk_widget_get_window (GTK_WIDGET (text_view));
8662 case GTK_TEXT_WINDOW_TEXT:
8663 return priv->text_window->bin_window;
8666 case GTK_TEXT_WINDOW_LEFT:
8667 if (priv->left_window)
8668 return priv->left_window->bin_window;
8673 case GTK_TEXT_WINDOW_RIGHT:
8674 if (priv->right_window)
8675 return priv->right_window->bin_window;
8680 case GTK_TEXT_WINDOW_TOP:
8681 if (priv->top_window)
8682 return priv->top_window->bin_window;
8687 case GTK_TEXT_WINDOW_BOTTOM:
8688 if (priv->bottom_window)
8689 return priv->bottom_window->bin_window;
8694 case GTK_TEXT_WINDOW_PRIVATE:
8695 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8700 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8705 * gtk_text_view_get_window_type:
8706 * @text_view: a #GtkTextView
8707 * @window: a window type
8709 * Usually used to find out which window an event corresponds to.
8710 * If you connect to an event signal on @text_view, this function
8711 * should be called on <literal>event->window</literal> to
8712 * see which window it was.
8714 * Return value: the window type.
8717 gtk_text_view_get_window_type (GtkTextView *text_view,
8722 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8723 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8725 if (window == gtk_widget_get_window (GTK_WIDGET (text_view)))
8726 return GTK_TEXT_WINDOW_WIDGET;
8728 win = g_object_get_qdata (G_OBJECT (window),
8729 g_quark_try_string ("gtk-text-view-text-window"));
8735 return GTK_TEXT_WINDOW_PRIVATE;
8740 buffer_to_widget (GtkTextView *text_view,
8746 GtkTextViewPrivate *priv = text_view->priv;
8750 *window_x = buffer_x - priv->xoffset;
8751 *window_x += priv->text_window->allocation.x;
8756 *window_y = buffer_y - priv->yoffset;
8757 *window_y += priv->text_window->allocation.y;
8762 widget_to_text_window (GtkTextWindow *win,
8769 *window_x = widget_x - win->allocation.x;
8772 *window_y = widget_y - win->allocation.y;
8776 buffer_to_text_window (GtkTextView *text_view,
8785 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8786 "for a nonexistent or private child window of GtkTextView");
8790 buffer_to_widget (text_view,
8792 window_x, window_y);
8794 widget_to_text_window (win,
8795 window_x ? *window_x : 0,
8796 window_y ? *window_y : 0,
8802 * gtk_text_view_buffer_to_window_coords:
8803 * @text_view: a #GtkTextView
8804 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8805 * @buffer_x: buffer x coordinate
8806 * @buffer_y: buffer y coordinate
8807 * @window_x: (out) (allow-none): window x coordinate return location or %NULL
8808 * @window_y: (out) (allow-none): window y coordinate return location or %NULL
8810 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8811 * @win, and stores the result in (@window_x, @window_y).
8813 * Note that you can't convert coordinates for a nonexisting window (see
8814 * gtk_text_view_set_border_window_size()).
8817 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8818 GtkTextWindowType win,
8824 GtkTextViewPrivate *priv = text_view->priv;
8826 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8830 case GTK_TEXT_WINDOW_WIDGET:
8831 buffer_to_widget (text_view,
8833 window_x, window_y);
8836 case GTK_TEXT_WINDOW_TEXT:
8838 *window_x = buffer_x - priv->xoffset;
8840 *window_y = buffer_y - priv->yoffset;
8843 case GTK_TEXT_WINDOW_LEFT:
8844 buffer_to_text_window (text_view,
8847 window_x, window_y);
8850 case GTK_TEXT_WINDOW_RIGHT:
8851 buffer_to_text_window (text_view,
8854 window_x, window_y);
8857 case GTK_TEXT_WINDOW_TOP:
8858 buffer_to_text_window (text_view,
8861 window_x, window_y);
8864 case GTK_TEXT_WINDOW_BOTTOM:
8865 buffer_to_text_window (text_view,
8866 priv->bottom_window,
8868 window_x, window_y);
8871 case GTK_TEXT_WINDOW_PRIVATE:
8872 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8876 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8882 widget_to_buffer (GtkTextView *text_view,
8888 GtkTextViewPrivate *priv = text_view->priv;
8892 *buffer_x = widget_x + priv->xoffset;
8893 *buffer_x -= priv->text_window->allocation.x;
8898 *buffer_y = widget_y + priv->yoffset;
8899 *buffer_y -= priv->text_window->allocation.y;
8904 text_window_to_widget (GtkTextWindow *win,
8911 *widget_x = window_x + win->allocation.x;
8914 *widget_y = window_y + win->allocation.y;
8918 text_window_to_buffer (GtkTextView *text_view,
8927 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8928 "coordinates for a nonexistent child window.");
8932 text_window_to_widget (win,
8938 widget_to_buffer (text_view,
8939 buffer_x ? *buffer_x : 0,
8940 buffer_y ? *buffer_y : 0,
8946 * gtk_text_view_window_to_buffer_coords:
8947 * @text_view: a #GtkTextView
8948 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8949 * @window_x: window x coordinate
8950 * @window_y: window y coordinate
8951 * @buffer_x: (out) (allow-none): buffer x coordinate return location or %NULL
8952 * @buffer_y: (out) (allow-none): buffer y coordinate return location or %NULL
8954 * Converts coordinates on the window identified by @win to buffer
8955 * coordinates, storing the result in (@buffer_x,@buffer_y).
8957 * Note that you can't convert coordinates for a nonexisting window (see
8958 * gtk_text_view_set_border_window_size()).
8961 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8962 GtkTextWindowType win,
8968 GtkTextViewPrivate *priv = text_view->priv;
8970 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8974 case GTK_TEXT_WINDOW_WIDGET:
8975 widget_to_buffer (text_view,
8977 buffer_x, buffer_y);
8980 case GTK_TEXT_WINDOW_TEXT:
8982 *buffer_x = window_x + priv->xoffset;
8984 *buffer_y = window_y + priv->yoffset;
8987 case GTK_TEXT_WINDOW_LEFT:
8988 text_window_to_buffer (text_view,
8991 buffer_x, buffer_y);
8994 case GTK_TEXT_WINDOW_RIGHT:
8995 text_window_to_buffer (text_view,
8998 buffer_x, buffer_y);
9001 case GTK_TEXT_WINDOW_TOP:
9002 text_window_to_buffer (text_view,
9005 buffer_x, buffer_y);
9008 case GTK_TEXT_WINDOW_BOTTOM:
9009 text_window_to_buffer (text_view,
9010 priv->bottom_window,
9012 buffer_x, buffer_y);
9015 case GTK_TEXT_WINDOW_PRIVATE:
9016 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
9020 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
9026 set_window_width (GtkTextView *text_view,
9028 GtkTextWindowType type,
9029 GtkTextWindow **winp)
9035 text_window_free (*winp);
9037 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9044 *winp = text_window_new (type,
9045 GTK_WIDGET (text_view),
9047 /* if the widget is already realized we need to realize the child manually */
9048 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9049 text_window_realize (*winp, GTK_WIDGET (text_view));
9053 if ((*winp)->requisition.width == width)
9056 (*winp)->requisition.width = width;
9059 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9065 set_window_height (GtkTextView *text_view,
9067 GtkTextWindowType type,
9068 GtkTextWindow **winp)
9074 text_window_free (*winp);
9076 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9083 *winp = text_window_new (type,
9084 GTK_WIDGET (text_view),
9087 /* if the widget is already realized we need to realize the child manually */
9088 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9089 text_window_realize (*winp, GTK_WIDGET (text_view));
9093 if ((*winp)->requisition.height == height)
9096 (*winp)->requisition.height = height;
9099 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9104 * gtk_text_view_set_border_window_size:
9105 * @text_view: a #GtkTextView
9106 * @type: window to affect
9107 * @size: width or height of the window
9109 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
9110 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
9111 * Automatically destroys the corresponding window if the size is set
9112 * to 0, and creates the window if the size is set to non-zero. This
9113 * function can only be used for the "border windows," it doesn't work
9114 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
9115 * #GTK_TEXT_WINDOW_PRIVATE.
9118 gtk_text_view_set_border_window_size (GtkTextView *text_view,
9119 GtkTextWindowType type,
9122 GtkTextViewPrivate *priv = text_view->priv;
9124 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9125 g_return_if_fail (size >= 0);
9129 case GTK_TEXT_WINDOW_LEFT:
9130 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
9131 &priv->left_window);
9134 case GTK_TEXT_WINDOW_RIGHT:
9135 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
9136 &priv->right_window);
9139 case GTK_TEXT_WINDOW_TOP:
9140 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
9144 case GTK_TEXT_WINDOW_BOTTOM:
9145 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
9146 &priv->bottom_window);
9150 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
9156 * gtk_text_view_get_border_window_size:
9157 * @text_view: a #GtkTextView
9158 * @type: window to return size from
9160 * Gets the width of the specified border window. See
9161 * gtk_text_view_set_border_window_size().
9163 * Return value: width of window
9166 gtk_text_view_get_border_window_size (GtkTextView *text_view,
9167 GtkTextWindowType type)
9169 GtkTextViewPrivate *priv = text_view->priv;
9171 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
9175 case GTK_TEXT_WINDOW_LEFT:
9176 if (priv->left_window)
9177 return priv->left_window->requisition.width;
9180 case GTK_TEXT_WINDOW_RIGHT:
9181 if (priv->right_window)
9182 return priv->right_window->requisition.width;
9185 case GTK_TEXT_WINDOW_TOP:
9186 if (priv->top_window)
9187 return priv->top_window->requisition.height;
9190 case GTK_TEXT_WINDOW_BOTTOM:
9191 if (priv->bottom_window)
9192 return priv->bottom_window->requisition.height;
9196 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
9207 static GtkTextViewChild*
9208 text_view_child_new_anchored (GtkWidget *child,
9209 GtkTextChildAnchor *anchor,
9210 GtkTextLayout *layout)
9212 GtkTextViewChild *vc;
9214 vc = g_new (GtkTextViewChild, 1);
9216 vc->type = GTK_TEXT_WINDOW_PRIVATE;
9218 vc->anchor = anchor;
9220 vc->from_top_of_line = 0;
9221 vc->from_left_of_buffer = 0;
9223 g_object_ref (vc->widget);
9224 g_object_ref (vc->anchor);
9226 g_object_set_data (G_OBJECT (child),
9227 I_("gtk-text-view-child"),
9230 gtk_text_child_anchor_register_child (anchor, child, layout);
9235 static GtkTextViewChild*
9236 text_view_child_new_window (GtkWidget *child,
9237 GtkTextWindowType type,
9241 GtkTextViewChild *vc;
9243 vc = g_new (GtkTextViewChild, 1);
9248 vc->from_top_of_line = 0;
9249 vc->from_left_of_buffer = 0;
9251 g_object_ref (vc->widget);
9257 g_object_set_data (G_OBJECT (child),
9258 I_("gtk-text-view-child"),
9265 text_view_child_free (GtkTextViewChild *child)
9267 g_object_set_data (G_OBJECT (child->widget),
9268 I_("gtk-text-view-child"), NULL);
9272 gtk_text_child_anchor_unregister_child (child->anchor,
9274 g_object_unref (child->anchor);
9277 g_object_unref (child->widget);
9283 text_view_child_set_parent_window (GtkTextView *text_view,
9284 GtkTextViewChild *vc)
9287 gtk_widget_set_parent_window (vc->widget,
9288 text_view->priv->text_window->bin_window);
9292 window = gtk_text_view_get_window (text_view,
9294 gtk_widget_set_parent_window (vc->widget, window);
9299 add_child (GtkTextView *text_view,
9300 GtkTextViewChild *vc)
9302 text_view->priv->children = g_slist_prepend (text_view->priv->children,
9305 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9306 text_view_child_set_parent_window (text_view, vc);
9308 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
9312 * gtk_text_view_add_child_at_anchor:
9313 * @text_view: a #GtkTextView
9314 * @child: a #GtkWidget
9315 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
9317 * Adds a child widget in the text buffer, at the given @anchor.
9320 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
9322 GtkTextChildAnchor *anchor)
9324 GtkTextViewChild *vc;
9326 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9327 g_return_if_fail (GTK_IS_WIDGET (child));
9328 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
9329 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
9331 gtk_text_view_ensure_layout (text_view);
9333 vc = text_view_child_new_anchored (child, anchor,
9334 text_view->priv->layout);
9336 add_child (text_view, vc);
9338 g_assert (vc->widget == child);
9339 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9343 * gtk_text_view_add_child_in_window:
9344 * @text_view: a #GtkTextView
9345 * @child: a #GtkWidget
9346 * @which_window: which window the child should appear in
9347 * @xpos: X position of child in window coordinates
9348 * @ypos: Y position of child in window coordinates
9350 * Adds a child at fixed coordinates in one of the text widget's
9353 * The window must have nonzero size (see
9354 * gtk_text_view_set_border_window_size()). Note that the child
9355 * coordinates are given relative to the #GdkWindow in question, and
9356 * that these coordinates have no sane relationship to scrolling. When
9357 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
9358 * irrelevant, the child floats above all scrollable areas. But when
9359 * placing a child in one of the scrollable windows (border windows or
9360 * text window), you'll need to compute the child's correct position
9361 * in buffer coordinates any time scrolling occurs or buffer changes
9362 * occur, and then call gtk_text_view_move_child() to update the
9366 gtk_text_view_add_child_in_window (GtkTextView *text_view,
9368 GtkTextWindowType which_window,
9372 GtkTextViewChild *vc;
9374 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9375 g_return_if_fail (GTK_IS_WIDGET (child));
9376 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
9378 vc = text_view_child_new_window (child, which_window,
9381 add_child (text_view, vc);
9383 g_assert (vc->widget == child);
9384 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9388 * gtk_text_view_move_child:
9389 * @text_view: a #GtkTextView
9390 * @child: child widget already added to the text view
9391 * @xpos: new X position in window coordinates
9392 * @ypos: new Y position in window coordinates
9394 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
9397 gtk_text_view_move_child (GtkTextView *text_view,
9402 GtkTextViewChild *vc;
9404 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9405 g_return_if_fail (GTK_IS_WIDGET (child));
9406 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9408 vc = g_object_get_data (G_OBJECT (child),
9409 "gtk-text-view-child");
9411 g_assert (vc != NULL);
9413 if (vc->x == xpos &&
9420 if (gtk_widget_get_visible (child) &&
9421 gtk_widget_get_visible (GTK_WIDGET (text_view)))
9422 gtk_widget_queue_resize (child);
9426 /* Iterator operations */
9429 * gtk_text_view_forward_display_line:
9430 * @text_view: a #GtkTextView
9431 * @iter: a #GtkTextIter
9433 * Moves the given @iter forward by one display (wrapped) line.
9434 * A display line is different from a paragraph. Paragraphs are
9435 * separated by newlines or other paragraph separator characters.
9436 * Display lines are created by line-wrapping a paragraph. If
9437 * wrapping is turned off, display lines and paragraphs will be the
9438 * same. Display lines are divided differently for each view, since
9439 * they depend on the view's width; paragraphs are the same in all
9440 * views, since they depend on the contents of the #GtkTextBuffer.
9442 * Return value: %TRUE if @iter was moved and is not on the end iterator
9445 gtk_text_view_forward_display_line (GtkTextView *text_view,
9448 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9449 g_return_val_if_fail (iter != NULL, FALSE);
9451 gtk_text_view_ensure_layout (text_view);
9453 return gtk_text_layout_move_iter_to_next_line (text_view->priv->layout, iter);
9457 * gtk_text_view_backward_display_line:
9458 * @text_view: a #GtkTextView
9459 * @iter: a #GtkTextIter
9461 * Moves the given @iter backward by one display (wrapped) line.
9462 * A display line is different from a paragraph. Paragraphs are
9463 * separated by newlines or other paragraph separator characters.
9464 * Display lines are created by line-wrapping a paragraph. If
9465 * wrapping is turned off, display lines and paragraphs will be the
9466 * same. Display lines are divided differently for each view, since
9467 * they depend on the view's width; paragraphs are the same in all
9468 * views, since they depend on the contents of the #GtkTextBuffer.
9470 * Return value: %TRUE if @iter was moved and is not on the end iterator
9473 gtk_text_view_backward_display_line (GtkTextView *text_view,
9476 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9477 g_return_val_if_fail (iter != NULL, FALSE);
9479 gtk_text_view_ensure_layout (text_view);
9481 return gtk_text_layout_move_iter_to_previous_line (text_view->priv->layout, iter);
9485 * gtk_text_view_forward_display_line_end:
9486 * @text_view: a #GtkTextView
9487 * @iter: a #GtkTextIter
9489 * Moves the given @iter forward to the next display line end.
9490 * A display line is different from a paragraph. Paragraphs are
9491 * separated by newlines or other paragraph separator characters.
9492 * Display lines are created by line-wrapping a paragraph. If
9493 * wrapping is turned off, display lines and paragraphs will be the
9494 * same. Display lines are divided differently for each view, since
9495 * they depend on the view's width; paragraphs are the same in all
9496 * views, since they depend on the contents of the #GtkTextBuffer.
9498 * Return value: %TRUE if @iter was moved and is not on the end iterator
9501 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
9504 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9505 g_return_val_if_fail (iter != NULL, FALSE);
9507 gtk_text_view_ensure_layout (text_view);
9509 return gtk_text_layout_move_iter_to_line_end (text_view->priv->layout, iter, 1);
9513 * gtk_text_view_backward_display_line_start:
9514 * @text_view: a #GtkTextView
9515 * @iter: a #GtkTextIter
9517 * Moves the given @iter backward to the next display line start.
9518 * A display line is different from a paragraph. Paragraphs are
9519 * separated by newlines or other paragraph separator characters.
9520 * Display lines are created by line-wrapping a paragraph. If
9521 * wrapping is turned off, display lines and paragraphs will be the
9522 * same. Display lines are divided differently for each view, since
9523 * they depend on the view's width; paragraphs are the same in all
9524 * views, since they depend on the contents of the #GtkTextBuffer.
9526 * Return value: %TRUE if @iter was moved and is not on the end iterator
9529 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
9532 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9533 g_return_val_if_fail (iter != NULL, FALSE);
9535 gtk_text_view_ensure_layout (text_view);
9537 return gtk_text_layout_move_iter_to_line_end (text_view->priv->layout, iter, -1);
9541 * gtk_text_view_starts_display_line:
9542 * @text_view: a #GtkTextView
9543 * @iter: a #GtkTextIter
9545 * Determines whether @iter is at the start of a display line.
9546 * See gtk_text_view_forward_display_line() for an explanation of
9547 * display lines vs. paragraphs.
9549 * Return value: %TRUE if @iter begins a wrapped line
9552 gtk_text_view_starts_display_line (GtkTextView *text_view,
9553 const GtkTextIter *iter)
9555 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9556 g_return_val_if_fail (iter != NULL, FALSE);
9558 gtk_text_view_ensure_layout (text_view);
9560 return gtk_text_layout_iter_starts_line (text_view->priv->layout, iter);
9564 * gtk_text_view_move_visually:
9565 * @text_view: a #GtkTextView
9566 * @iter: a #GtkTextIter
9567 * @count: number of characters to move (negative moves left,
9568 * positive moves right)
9570 * Move the iterator a given number of characters visually, treating
9571 * it as the strong cursor position. If @count is positive, then the
9572 * new strong cursor position will be @count positions to the right of
9573 * the old cursor position. If @count is negative then the new strong
9574 * cursor position will be @count positions to the left of the old
9577 * In the presence of bi-directional text, the correspondence
9578 * between logical and visual order will depend on the direction
9579 * of the current run, and there may be jumps when the cursor
9580 * is moved off of the end of a run.
9582 * Return value: %TRUE if @iter moved and is not on the end iterator
9585 gtk_text_view_move_visually (GtkTextView *text_view,
9589 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9590 g_return_val_if_fail (iter != NULL, FALSE);
9592 gtk_text_view_ensure_layout (text_view);
9594 return gtk_text_layout_move_iter_visually (text_view->priv->layout, iter, count);