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/.
31 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
32 #include "gtkbindings.h"
34 #include "gtkimagemenuitem.h"
37 #include "gtkmarshalers.h"
39 #include "gtkmenuitem.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtksettings.h"
43 #include "gtktextbufferrichtext.h"
44 #include "gtktextdisplay.h"
45 #include "gtktextview.h"
46 #include "gtkimmulticontext.h"
47 #include "gdk/gdkkeysyms.h"
48 #include "gtkprivate.h"
49 #include "gtktextutil.h"
50 #include "gtkwindow.h"
53 /* How scrolling, validation, exposes, etc. work.
55 * The expose_event handler has the invariant that the onscreen lines
56 * have been validated.
58 * There are two ways that onscreen lines can become invalid. The first
59 * is to change which lines are onscreen. This happens when the value
60 * of a scroll adjustment changes. So the code path begins in
61 * gtk_text_view_value_changed() and goes like this:
62 * - gdk_window_scroll() to reflect the new adjustment value
63 * - validate the lines that were moved onscreen
64 * - gdk_window_process_updates() to handle the exposes immediately
66 * The second way is that you get the "invalidated" signal from the layout,
67 * indicating that lines have become invalid. This code path begins in
68 * invalidated_handler() and goes like this:
69 * - install high-priority idle which does the rest of the steps
70 * - if a scroll is pending from scroll_to_mark(), do the scroll,
71 * jumping to the gtk_text_view_value_changed() code path
72 * - otherwise, validate the onscreen lines
73 * - DO NOT process updates
75 * In both cases, validating the onscreen lines can trigger a scroll
76 * due to maintaining the first_para on the top of the screen.
77 * If validation triggers a scroll, we jump to the top of the code path
78 * for value_changed, and bail out of the current code path.
80 * Also, in size_allocate, if we invalidate some lines from changing
81 * the layout width, we need to go ahead and run the high-priority idle,
82 * because GTK sends exposes right after doing the size allocates without
83 * returning to the main loop. This is also why the high-priority idle
84 * is at a higher priority than resizing.
89 #define DEBUG_VALIDATION_AND_SCROLLING
92 #ifdef DEBUG_VALIDATION_AND_SCROLLING
98 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->priv->text_window)
99 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->priv->text_window)
101 #define SPACE_FOR_CURSOR 1
103 #define GTK_TEXT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_VIEW, GtkTextViewPrivate))
105 typedef struct _GtkTextWindow GtkTextWindow;
106 typedef struct _GtkTextPendingScroll GtkTextPendingScroll;
108 struct _GtkTextViewPrivate
110 GtkTextLayout *layout;
111 GtkTextBuffer *buffer;
113 guint blink_time; /* time in msec the cursor has blinked since last user event */
116 GdkDevice *grab_device;
117 GdkDevice *dnd_device;
119 guint selection_drag_handler;
120 guint scroll_timeout;
122 GtkTextWindow *text_window;
123 GtkTextWindow *left_window;
124 GtkTextWindow *right_window;
125 GtkTextWindow *top_window;
126 GtkTextWindow *bottom_window;
128 GtkAdjustment *hadjustment;
129 GtkAdjustment *vadjustment;
131 gint xoffset; /* Offsets between widget coordinates and buffer coordinates */
133 gint width; /* Width and height of the buffer */
136 /* The virtual cursor position is normally the same as the
137 * actual (strong) cursor position, except in two circumstances:
139 * a) When the cursor is moved vertically with the keyboard
140 * b) When the text view is scrolled with the keyboard
142 * In case a), virtual_cursor_x is preserved, but not virtual_cursor_y
143 * In case b), both virtual_cursor_x and virtual_cursor_y are preserved.
145 gint virtual_cursor_x; /* -1 means use actual cursor position */
146 gint virtual_cursor_y; /* -1 means use actual cursor position */
148 GtkTextMark *first_para_mark; /* Mark at the beginning of the first onscreen paragraph */
149 gint first_para_pixels; /* Offset of top of screen in the first onscreen paragraph */
151 GtkTextMark *dnd_mark;
154 guint first_validate_idle; /* Idle to revalidate onscreen portion, runs before resize */
155 guint incremental_validate_idle; /* Idle to revalidate offscreen portions, runs after redraw */
157 GtkIMContext *im_context;
158 GtkWidget *popup_menu;
165 GtkTextPendingScroll *pending_scroll;
167 gint pending_place_cursor_button;
169 /* Default style settings */
170 gint pixels_above_lines;
171 gint pixels_below_lines;
172 gint pixels_inside_wrap;
173 GtkWrapMode wrap_mode;
174 GtkJustification justify;
181 guint overwrite_mode : 1;
182 guint cursor_visible : 1;
184 /* if we have reset the IM since the last character entered */
185 guint need_im_reset : 1;
187 guint accepts_tab : 1;
189 guint width_changed : 1;
191 /* debug flag - means that we've validated onscreen since the
192 * last "invalidate" signal from the layout
194 guint onscreen_validated : 1;
196 guint mouse_cursor_obscured : 1;
198 guint scroll_after_paste : 1;
201 struct _GtkTextPendingScroll
204 gdouble within_margin;
212 SET_SCROLL_ADJUSTMENTS,
226 TOGGLE_CURSOR_VISIBLE,
234 PROP_PIXELS_ABOVE_LINES,
235 PROP_PIXELS_BELOW_LINES,
236 PROP_PIXELS_INSIDE_WRAP,
251 static void gtk_text_view_destroy (GtkObject *object);
252 static void gtk_text_view_finalize (GObject *object);
253 static void gtk_text_view_set_property (GObject *object,
257 static void gtk_text_view_get_property (GObject *object,
261 static void gtk_text_view_size_request (GtkWidget *widget,
262 GtkRequisition *requisition);
263 static void gtk_text_view_size_allocate (GtkWidget *widget,
264 GtkAllocation *allocation);
265 static void gtk_text_view_realize (GtkWidget *widget);
266 static void gtk_text_view_unrealize (GtkWidget *widget);
267 static void gtk_text_view_style_set (GtkWidget *widget,
268 GtkStyle *previous_style);
269 static void gtk_text_view_direction_changed (GtkWidget *widget,
270 GtkTextDirection previous_direction);
271 static void gtk_text_view_grab_notify (GtkWidget *widget,
272 gboolean was_grabbed);
273 static void gtk_text_view_state_changed (GtkWidget *widget,
274 GtkStateType previous_state);
276 static gint gtk_text_view_event (GtkWidget *widget,
278 static gint gtk_text_view_key_press_event (GtkWidget *widget,
280 static gint gtk_text_view_key_release_event (GtkWidget *widget,
282 static gint gtk_text_view_button_press_event (GtkWidget *widget,
283 GdkEventButton *event);
284 static gint gtk_text_view_button_release_event (GtkWidget *widget,
285 GdkEventButton *event);
286 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
287 GdkEventFocus *event);
288 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
289 GdkEventFocus *event);
290 static gint gtk_text_view_motion_event (GtkWidget *widget,
291 GdkEventMotion *event);
292 static gint gtk_text_view_expose_event (GtkWidget *widget,
293 GdkEventExpose *expose);
294 static void gtk_text_view_draw_focus (GtkWidget *widget);
295 static gboolean gtk_text_view_focus (GtkWidget *widget,
296 GtkDirectionType direction);
297 static void gtk_text_view_move_focus (GtkWidget *widget,
298 GtkDirectionType direction_type);
299 static void gtk_text_view_select_all (GtkWidget *widget,
303 /* Source side drag signals */
304 static void gtk_text_view_drag_begin (GtkWidget *widget,
305 GdkDragContext *context);
306 static void gtk_text_view_drag_end (GtkWidget *widget,
307 GdkDragContext *context);
308 static void gtk_text_view_drag_data_get (GtkWidget *widget,
309 GdkDragContext *context,
310 GtkSelectionData *selection_data,
313 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
314 GdkDragContext *context);
316 /* Target side drag signals */
317 static void gtk_text_view_drag_leave (GtkWidget *widget,
318 GdkDragContext *context,
320 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
321 GdkDragContext *context,
325 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
326 GdkDragContext *context,
330 static void gtk_text_view_drag_data_received (GtkWidget *widget,
331 GdkDragContext *context,
334 GtkSelectionData *selection_data,
338 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
340 GtkAdjustment *vadj);
341 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
343 static void gtk_text_view_move_cursor (GtkTextView *text_view,
344 GtkMovementStep step,
346 gboolean extend_selection);
347 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
349 gboolean extend_selection);
350 static gboolean gtk_text_view_move_viewport (GtkTextView *text_view,
353 static void gtk_text_view_set_anchor (GtkTextView *text_view);
354 static gboolean gtk_text_view_scroll_pages (GtkTextView *text_view,
356 gboolean extend_selection);
357 static gboolean gtk_text_view_scroll_hpages(GtkTextView *text_view,
359 gboolean extend_selection);
360 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
362 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
365 static void gtk_text_view_backspace (GtkTextView *text_view);
366 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
367 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
368 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
369 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
370 static void gtk_text_view_toggle_cursor_visible (GtkTextView *text_view);
371 static void gtk_text_view_compat_move_focus(GtkTextView *text_view,
372 GtkDirectionType direction_type);
373 static void gtk_text_view_unselect (GtkTextView *text_view);
375 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
376 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
378 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
379 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
380 GtkTextAttributes *values,
382 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
383 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
384 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
385 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
386 const GtkTextIter *iter,
387 GdkEventButton *event);
388 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view);
389 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
390 const GtkTextIter *iter,
391 GdkEventMotion *event);
392 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
393 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
394 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
395 static void gtk_text_view_reset_blink_time (GtkTextView *text_view);
397 static void gtk_text_view_value_changed (GtkAdjustment *adj,
399 static void gtk_text_view_commit_handler (GtkIMContext *context,
401 GtkTextView *text_view);
402 static void gtk_text_view_commit_text (GtkTextView *text_view,
404 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
405 GtkTextView *text_view);
406 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
407 GtkTextView *text_view);
408 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
411 GtkTextView *text_view);
413 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
414 const GtkTextIter *location,
417 static void gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
418 const GParamSpec *pspec,
420 static void gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
421 GtkClipboard *clipboard,
423 static void gtk_text_view_get_cursor_location (GtkTextView *text_view,
425 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
429 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
433 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
434 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
436 static void gtk_text_view_do_popup (GtkTextView *text_view,
437 GdkEventButton *event);
439 static void cancel_pending_scroll (GtkTextView *text_view);
440 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
442 gdouble within_margin,
447 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
448 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
449 static void gtk_text_view_invalidate (GtkTextView *text_view);
450 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
452 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
454 /* Container methods */
455 static void gtk_text_view_add (GtkContainer *container,
457 static void gtk_text_view_remove (GtkContainer *container,
459 static void gtk_text_view_forall (GtkContainer *container,
460 gboolean include_internals,
461 GtkCallback callback,
462 gpointer callback_data);
464 /* FIXME probably need the focus methods. */
466 typedef struct _GtkTextViewChild GtkTextViewChild;
468 struct _GtkTextViewChild
472 GtkTextChildAnchor *anchor;
474 gint from_top_of_line;
475 gint from_left_of_buffer;
477 /* These are ignored if anchor != NULL */
478 GtkTextWindowType type;
483 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
484 GtkTextChildAnchor *anchor,
485 GtkTextLayout *layout);
486 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
487 GtkTextWindowType type,
490 static void text_view_child_free (GtkTextViewChild *child);
491 static void text_view_child_set_parent_window (GtkTextView *text_view,
492 GtkTextViewChild *child);
494 struct _GtkTextWindow
496 GtkTextWindowType type;
499 GdkWindow *bin_window;
500 GtkRequisition requisition;
501 GdkRectangle allocation;
504 static GtkTextWindow *text_window_new (GtkTextWindowType type,
507 gint height_request);
508 static void text_window_free (GtkTextWindow *win);
509 static void text_window_realize (GtkTextWindow *win,
511 static void text_window_unrealize (GtkTextWindow *win);
512 static void text_window_size_allocate (GtkTextWindow *win,
514 static void text_window_scroll (GtkTextWindow *win,
517 static void text_window_invalidate_rect (GtkTextWindow *win,
519 static void text_window_invalidate_cursors (GtkTextWindow *win);
521 static gint text_window_get_width (GtkTextWindow *win);
522 static gint text_window_get_height (GtkTextWindow *win);
525 static guint signals[LAST_SIGNAL] = { 0 };
527 G_DEFINE_TYPE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER)
530 add_move_binding (GtkBindingSet *binding_set,
533 GtkMovementStep step,
536 g_assert ((modmask & GDK_SHIFT_MASK) == 0);
538 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
542 G_TYPE_BOOLEAN, FALSE);
544 /* Selection-extending version */
545 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
549 G_TYPE_BOOLEAN, TRUE);
553 gtk_text_view_class_init (GtkTextViewClass *klass)
555 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
556 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
557 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
558 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
559 GtkBindingSet *binding_set;
561 /* Default handlers and virtual methods
563 gobject_class->set_property = gtk_text_view_set_property;
564 gobject_class->get_property = gtk_text_view_get_property;
566 object_class->destroy = gtk_text_view_destroy;
567 gobject_class->finalize = gtk_text_view_finalize;
569 widget_class->realize = gtk_text_view_realize;
570 widget_class->unrealize = gtk_text_view_unrealize;
571 widget_class->style_set = gtk_text_view_style_set;
572 widget_class->direction_changed = gtk_text_view_direction_changed;
573 widget_class->grab_notify = gtk_text_view_grab_notify;
574 widget_class->state_changed = gtk_text_view_state_changed;
575 widget_class->size_request = gtk_text_view_size_request;
576 widget_class->size_allocate = gtk_text_view_size_allocate;
577 widget_class->event = gtk_text_view_event;
578 widget_class->key_press_event = gtk_text_view_key_press_event;
579 widget_class->key_release_event = gtk_text_view_key_release_event;
580 widget_class->button_press_event = gtk_text_view_button_press_event;
581 widget_class->button_release_event = gtk_text_view_button_release_event;
582 widget_class->focus_in_event = gtk_text_view_focus_in_event;
583 widget_class->focus_out_event = gtk_text_view_focus_out_event;
584 widget_class->motion_notify_event = gtk_text_view_motion_event;
585 widget_class->expose_event = gtk_text_view_expose_event;
586 widget_class->focus = gtk_text_view_focus;
588 /* need to override the base class function via override_class_handler,
589 * because the signal slot is not available in GtkWidgetCLass
591 g_signal_override_class_handler ("move-focus",
593 G_CALLBACK (gtk_text_view_move_focus));
595 widget_class->drag_begin = gtk_text_view_drag_begin;
596 widget_class->drag_end = gtk_text_view_drag_end;
597 widget_class->drag_data_get = gtk_text_view_drag_data_get;
598 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
600 widget_class->drag_leave = gtk_text_view_drag_leave;
601 widget_class->drag_motion = gtk_text_view_drag_motion;
602 widget_class->drag_drop = gtk_text_view_drag_drop;
603 widget_class->drag_data_received = gtk_text_view_drag_data_received;
605 widget_class->popup_menu = gtk_text_view_popup_menu;
607 container_class->add = gtk_text_view_add;
608 container_class->remove = gtk_text_view_remove;
609 container_class->forall = gtk_text_view_forall;
611 klass->move_cursor = gtk_text_view_move_cursor;
612 klass->page_horizontally = gtk_text_view_page_horizontally;
613 klass->set_anchor = gtk_text_view_set_anchor;
614 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
615 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
616 klass->backspace = gtk_text_view_backspace;
617 klass->cut_clipboard = gtk_text_view_cut_clipboard;
618 klass->copy_clipboard = gtk_text_view_copy_clipboard;
619 klass->paste_clipboard = gtk_text_view_paste_clipboard;
620 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
621 klass->move_focus = gtk_text_view_compat_move_focus;
622 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
628 g_object_class_install_property (gobject_class,
629 PROP_PIXELS_ABOVE_LINES,
630 g_param_spec_int ("pixels-above-lines",
631 P_("Pixels Above Lines"),
632 P_("Pixels of blank space above paragraphs"),
636 GTK_PARAM_READWRITE));
638 g_object_class_install_property (gobject_class,
639 PROP_PIXELS_BELOW_LINES,
640 g_param_spec_int ("pixels-below-lines",
641 P_("Pixels Below Lines"),
642 P_("Pixels of blank space below paragraphs"),
646 GTK_PARAM_READWRITE));
648 g_object_class_install_property (gobject_class,
649 PROP_PIXELS_INSIDE_WRAP,
650 g_param_spec_int ("pixels-inside-wrap",
651 P_("Pixels Inside Wrap"),
652 P_("Pixels of blank space between wrapped lines in a paragraph"),
656 GTK_PARAM_READWRITE));
658 g_object_class_install_property (gobject_class,
660 g_param_spec_boolean ("editable",
662 P_("Whether the text can be modified by the user"),
664 GTK_PARAM_READWRITE));
666 g_object_class_install_property (gobject_class,
668 g_param_spec_enum ("wrap-mode",
670 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
673 GTK_PARAM_READWRITE));
675 g_object_class_install_property (gobject_class,
677 g_param_spec_enum ("justification",
679 P_("Left, right, or center justification"),
680 GTK_TYPE_JUSTIFICATION,
682 GTK_PARAM_READWRITE));
684 g_object_class_install_property (gobject_class,
686 g_param_spec_int ("left-margin",
688 P_("Width of the left margin in pixels"),
692 GTK_PARAM_READWRITE));
694 g_object_class_install_property (gobject_class,
696 g_param_spec_int ("right-margin",
698 P_("Width of the right margin in pixels"),
702 GTK_PARAM_READWRITE));
704 g_object_class_install_property (gobject_class,
706 g_param_spec_int ("indent",
708 P_("Amount to indent the paragraph, in pixels"),
712 GTK_PARAM_READWRITE));
714 g_object_class_install_property (gobject_class,
716 g_param_spec_boxed ("tabs",
718 P_("Custom tabs for this text"),
719 PANGO_TYPE_TAB_ARRAY,
720 GTK_PARAM_READWRITE));
722 g_object_class_install_property (gobject_class,
724 g_param_spec_boolean ("cursor-visible",
725 P_("Cursor Visible"),
726 P_("If the insertion cursor is shown"),
728 GTK_PARAM_READWRITE));
730 g_object_class_install_property (gobject_class,
732 g_param_spec_object ("buffer",
734 P_("The buffer which is displayed"),
735 GTK_TYPE_TEXT_BUFFER,
736 GTK_PARAM_READWRITE));
738 g_object_class_install_property (gobject_class,
740 g_param_spec_boolean ("overwrite",
741 P_("Overwrite mode"),
742 P_("Whether entered text overwrites existing contents"),
744 GTK_PARAM_READWRITE));
746 g_object_class_install_property (gobject_class,
748 g_param_spec_boolean ("accepts-tab",
750 P_("Whether Tab will result in a tab character being entered"),
752 GTK_PARAM_READWRITE));
755 * GtkTextView:im-module:
757 * Which IM (input method) module should be used for this entry.
760 * Setting this to a non-%NULL value overrides the
761 * system-wide IM module setting. See the GtkSettings
762 * #GtkSettings:gtk-im-module property.
766 g_object_class_install_property (gobject_class,
768 g_param_spec_string ("im-module",
770 P_("Which IM module should be used"),
772 GTK_PARAM_READWRITE));
777 gtk_widget_class_install_style_property (widget_class,
778 g_param_spec_boxed ("error-underline-color",
779 P_("Error underline color"),
780 P_("Color with which to draw error-indication underlines"),
782 GTK_PARAM_READABLE));
789 * GtkTextView::move-cursor:
790 * @text_view: the object which received the signal
791 * @step: the granularity of the move, as a #GtkMovementStep
792 * @count: the number of @step units to move
793 * @extend_selection: %TRUE if the move should extend the selection
795 * The ::move-cursor signal is a
796 * <link linkend="keybinding-signals">keybinding signal</link>
797 * which gets emitted when the user initiates a cursor movement.
798 * If the cursor is not visible in @text_view, this signal causes
799 * the viewport to be moved instead.
801 * Applications should not connect to it, but may emit it with
802 * g_signal_emit_by_name() if they need to control the cursor
805 * The default bindings for this signal come in two variants,
806 * the variant with the Shift modifier extends the selection,
807 * the variant without the Shift modifer does not.
808 * There are too many key combinations to list them all here.
810 * <listitem>Arrow keys move by individual characters/lines</listitem>
811 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
812 * <listitem>Home/End keys move to the ends of the buffer</listitem>
813 * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
814 * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
817 signals[MOVE_CURSOR] =
818 g_signal_new (I_("move-cursor"),
819 G_OBJECT_CLASS_TYPE (gobject_class),
820 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
821 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
823 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
825 GTK_TYPE_MOVEMENT_STEP,
830 * GtkTextView::page-horizontally:
831 * @text_view: the object which received the signal
832 * @count: the number of @step units to move
833 * @extend_selection: %TRUE if the move should extend the selection
835 * The ::page-horizontally signal is a
836 * <link linkend="keybinding-signals">keybinding signal</link>
837 * which can be bound to key combinations to allow the user
838 * to initiate horizontal cursor movement by pages.
840 * This signal should not be used anymore, instead use the
841 * #GtkTextview::move-cursor signal with the #GTK_MOVEMENT_HORIZONTAL_PAGES
844 signals[PAGE_HORIZONTALLY] =
845 g_signal_new (I_("page-horizontally"),
846 G_OBJECT_CLASS_TYPE (gobject_class),
847 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
848 G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
850 _gtk_marshal_VOID__INT_BOOLEAN,
856 * GtkTextView::move-viewport:
857 * @text_view: the object which received the signal
858 * @step: the granularity of the move, as a #GtkMovementStep
859 * @count: the number of @step units to move
861 * The ::move-viewport signal is a
862 * <link linkend="keybinding-signals">keybinding signal</link>
863 * which can be bound to key combinations to allow the user
864 * to move the viewport, i.e. change what part of the text view
865 * is visible in a containing scrolled window.
867 * There are no default bindings for this signal.
869 signals[MOVE_VIEWPORT] =
870 g_signal_new_class_handler (I_("move-viewport"),
871 G_OBJECT_CLASS_TYPE (gobject_class),
872 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
873 G_CALLBACK (gtk_text_view_move_viewport),
875 _gtk_marshal_VOID__ENUM_INT,
877 GTK_TYPE_SCROLL_STEP,
881 * GtkTextView::set-anchor:
882 * @text_view: the object which received the signal
884 * The ::set-anchor signal is a
885 * <link linkend="keybinding-signals">keybinding signal</link>
886 * which gets emitted when the user initiates setting the "anchor"
887 * mark. The "anchor" mark gets placed at the same position as the
890 * This signal has no default bindings.
892 signals[SET_ANCHOR] =
893 g_signal_new (I_("set-anchor"),
894 G_OBJECT_CLASS_TYPE (gobject_class),
895 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
896 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
898 _gtk_marshal_VOID__VOID,
902 * GtkTextView::insert-at-cursor:
903 * @text_view: the object which received the signal
904 * @string: the string to insert
906 * The ::insert-at-cursor signal is a
907 * <link linkend="keybinding-signals">keybinding signal</link>
908 * which gets emitted when the user initiates the insertion of a
909 * fixed string at the cursor.
911 * This signal has no default bindings.
913 signals[INSERT_AT_CURSOR] =
914 g_signal_new (I_("insert-at-cursor"),
915 G_OBJECT_CLASS_TYPE (gobject_class),
916 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
917 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
919 _gtk_marshal_VOID__STRING,
924 * GtkTextView::delete-from-cursor:
925 * @text_view: the object which received the signal
926 * @type: the granularity of the deletion, as a #GtkDeleteType
927 * @count: the number of @type units to delete
929 * The ::delete-from-cursor signal is a
930 * <link linkend="keybinding-signals">keybinding signal</link>
931 * which gets emitted when the user initiates a text deletion.
933 * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
934 * if there is one, otherwise it deletes the requested number
937 * The default bindings for this signal are
938 * Delete for deleting a character, Ctrl-Delete for
939 * deleting a word and Ctrl-Backspace for deleting a word
942 signals[DELETE_FROM_CURSOR] =
943 g_signal_new (I_("delete-from-cursor"),
944 G_OBJECT_CLASS_TYPE (gobject_class),
945 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
946 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
948 _gtk_marshal_VOID__ENUM_INT,
950 GTK_TYPE_DELETE_TYPE,
954 * GtkTextView::backspace:
955 * @text_view: the object which received the signal
957 * The ::backspace signal is a
958 * <link linkend="keybinding-signals">keybinding signal</link>
959 * which gets emitted when the user asks for it.
961 * The default bindings for this signal are
962 * Backspace and Shift-Backspace.
965 g_signal_new (I_("backspace"),
966 G_OBJECT_CLASS_TYPE (gobject_class),
967 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
968 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
970 _gtk_marshal_VOID__VOID,
974 * GtkTextView::cut-clipboard:
975 * @text_view: the object which received the signal
977 * The ::cut-clipboard signal is a
978 * <link linkend="keybinding-signals">keybinding signal</link>
979 * which gets emitted to cut the selection to the clipboard.
981 * The default bindings for this signal are
982 * Ctrl-x and Shift-Delete.
984 signals[CUT_CLIPBOARD] =
985 g_signal_new (I_("cut-clipboard"),
986 G_OBJECT_CLASS_TYPE (gobject_class),
987 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
988 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
990 _gtk_marshal_VOID__VOID,
994 * GtkTextView::copy-clipboard:
995 * @text_view: the object which received the signal
997 * The ::copy-clipboard signal is a
998 * <link linkend="keybinding-signals">keybinding signal</link>
999 * which gets emitted to copy the selection to the clipboard.
1001 * The default bindings for this signal are
1002 * Ctrl-c and Ctrl-Insert.
1004 signals[COPY_CLIPBOARD] =
1005 g_signal_new (I_("copy-clipboard"),
1006 G_OBJECT_CLASS_TYPE (gobject_class),
1007 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1008 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
1010 _gtk_marshal_VOID__VOID,
1014 * GtkTextView::paste-clipboard:
1015 * @text_view: the object which received the signal
1017 * The ::paste-clipboard signal is a
1018 * <link linkend="keybinding-signals">keybinding signal</link>
1019 * which gets emitted to paste the contents of the clipboard
1020 * into the text view.
1022 * The default bindings for this signal are
1023 * Ctrl-v and Shift-Insert.
1025 signals[PASTE_CLIPBOARD] =
1026 g_signal_new (I_("paste-clipboard"),
1027 G_OBJECT_CLASS_TYPE (gobject_class),
1028 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1029 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
1031 _gtk_marshal_VOID__VOID,
1035 * GtkTextView::toggle-overwrite:
1036 * @text_view: the object which received the signal
1038 * The ::toggle-overwrite signal is a
1039 * <link linkend="keybinding-signals">keybinding signal</link>
1040 * which gets emitted to toggle the overwrite mode of the text view.
1042 * The default bindings for this signal is Insert.
1044 signals[TOGGLE_OVERWRITE] =
1045 g_signal_new (I_("toggle-overwrite"),
1046 G_OBJECT_CLASS_TYPE (gobject_class),
1047 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1048 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
1050 _gtk_marshal_VOID__VOID,
1054 * GtkTextView::set-scroll-adjustments
1055 * @horizontal: the horizontal #GtkAdjustment
1056 * @vertical: the vertical #GtkAdjustment
1058 * Set the scroll adjustments for the text view. Usually scrolled containers
1059 * like #GtkScrolledWindow will emit this signal to connect two instances
1060 * of #GtkScrollbar to the scroll directions of the #GtkTextView.
1062 signals[SET_SCROLL_ADJUSTMENTS] =
1063 g_signal_new (I_("set-scroll-adjustments"),
1064 G_OBJECT_CLASS_TYPE (gobject_class),
1065 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1066 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
1068 _gtk_marshal_VOID__OBJECT_OBJECT,
1070 GTK_TYPE_ADJUSTMENT,
1071 GTK_TYPE_ADJUSTMENT);
1072 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
1075 * GtkTextView::populate-popup:
1076 * @entry: The text view on which the signal is emitted
1077 * @menu: the menu that is being populated
1079 * The ::populate-popup signal gets emitted before showing the
1080 * context menu of the text view.
1082 * If you need to add items to the context menu, connect
1083 * to this signal and append your menuitems to the @menu.
1085 signals[POPULATE_POPUP] =
1086 g_signal_new (I_("populate-popup"),
1087 G_OBJECT_CLASS_TYPE (gobject_class),
1089 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
1091 _gtk_marshal_VOID__OBJECT,
1096 * GtkTextView::select-all:
1097 * @text_view: the object which received the signal
1098 * @select: %TRUE to select, %FALSE to unselect
1100 * The ::select-all signal is a
1101 * <link linkend="keybinding-signals">keybinding signal</link>
1102 * which gets emitted to select or unselect the complete
1103 * contents of the text view.
1105 * The default bindings for this signal are Ctrl-a and Ctrl-/
1106 * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
1108 signals[SELECT_ALL] =
1109 g_signal_new_class_handler (I_("select-all"),
1110 G_OBJECT_CLASS_TYPE (object_class),
1111 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1112 G_CALLBACK (gtk_text_view_select_all),
1114 _gtk_marshal_VOID__BOOLEAN,
1115 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
1118 * GtkTextView::toggle-cursor-visible:
1119 * @text_view: the object which received the signal
1121 * The ::toggle-cursor-visible signal is a
1122 * <link linkend="keybinding-signals">keybinding signal</link>
1123 * which gets emitted to toggle the visibility of the cursor.
1125 * The default binding for this signal is F7.
1127 signals[TOGGLE_CURSOR_VISIBLE] =
1128 g_signal_new_class_handler (I_("toggle-cursor-visible"),
1129 G_OBJECT_CLASS_TYPE (object_class),
1130 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1131 G_CALLBACK (gtk_text_view_toggle_cursor_visible),
1133 _gtk_marshal_VOID__VOID,
1137 * GtkTextView::preedit-changed:
1138 * @text_view: the object which received the signal
1139 * @preedit: the current preedit string
1141 * If an input method is used, the typed text will not immediately
1142 * be committed to the buffer. So if you are interested in the text,
1143 * connect to this signal.
1145 * This signal is only emitted if the text at the given position
1146 * is actually editable.
1150 signals[PREEDIT_CHANGED] =
1151 g_signal_new_class_handler (I_("preedit-changed"),
1152 G_OBJECT_CLASS_TYPE (object_class),
1153 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1156 _gtk_marshal_VOID__STRING,
1164 binding_set = gtk_binding_set_by_class (klass);
1166 /* Moving the insertion point */
1167 add_move_binding (binding_set, GDK_Right, 0,
1168 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1170 add_move_binding (binding_set, GDK_KP_Right, 0,
1171 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1173 add_move_binding (binding_set, GDK_Left, 0,
1174 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1176 add_move_binding (binding_set, GDK_KP_Left, 0,
1177 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1179 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
1180 GTK_MOVEMENT_WORDS, 1);
1182 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
1183 GTK_MOVEMENT_WORDS, 1);
1185 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
1186 GTK_MOVEMENT_WORDS, -1);
1188 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
1189 GTK_MOVEMENT_WORDS, -1);
1191 add_move_binding (binding_set, GDK_Up, 0,
1192 GTK_MOVEMENT_DISPLAY_LINES, -1);
1194 add_move_binding (binding_set, GDK_KP_Up, 0,
1195 GTK_MOVEMENT_DISPLAY_LINES, -1);
1197 add_move_binding (binding_set, GDK_Down, 0,
1198 GTK_MOVEMENT_DISPLAY_LINES, 1);
1200 add_move_binding (binding_set, GDK_KP_Down, 0,
1201 GTK_MOVEMENT_DISPLAY_LINES, 1);
1203 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
1204 GTK_MOVEMENT_PARAGRAPHS, -1);
1206 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
1207 GTK_MOVEMENT_PARAGRAPHS, -1);
1209 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
1210 GTK_MOVEMENT_PARAGRAPHS, 1);
1212 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
1213 GTK_MOVEMENT_PARAGRAPHS, 1);
1215 add_move_binding (binding_set, GDK_Home, 0,
1216 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1218 add_move_binding (binding_set, GDK_KP_Home, 0,
1219 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1221 add_move_binding (binding_set, GDK_End, 0,
1222 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1224 add_move_binding (binding_set, GDK_KP_End, 0,
1225 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1227 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
1228 GTK_MOVEMENT_BUFFER_ENDS, -1);
1230 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
1231 GTK_MOVEMENT_BUFFER_ENDS, -1);
1233 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
1234 GTK_MOVEMENT_BUFFER_ENDS, 1);
1236 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
1237 GTK_MOVEMENT_BUFFER_ENDS, 1);
1239 add_move_binding (binding_set, GDK_Page_Up, 0,
1240 GTK_MOVEMENT_PAGES, -1);
1242 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
1243 GTK_MOVEMENT_PAGES, -1);
1245 add_move_binding (binding_set, GDK_Page_Down, 0,
1246 GTK_MOVEMENT_PAGES, 1);
1248 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
1249 GTK_MOVEMENT_PAGES, 1);
1251 add_move_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
1252 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1254 add_move_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
1255 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1257 add_move_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
1258 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1260 add_move_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
1261 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1264 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
1266 G_TYPE_BOOLEAN, TRUE);
1268 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
1270 G_TYPE_BOOLEAN, TRUE);
1273 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
1275 G_TYPE_BOOLEAN, FALSE);
1277 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1279 G_TYPE_BOOLEAN, FALSE);
1282 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
1283 "delete-from-cursor", 2,
1284 G_TYPE_ENUM, GTK_DELETE_CHARS,
1287 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
1288 "delete-from-cursor", 2,
1289 G_TYPE_ENUM, GTK_DELETE_CHARS,
1292 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
1295 /* Make this do the same as Backspace, to help with mis-typing */
1296 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
1299 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
1300 "delete-from-cursor", 2,
1301 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1304 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
1305 "delete-from-cursor", 2,
1306 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1309 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
1310 "delete-from-cursor", 2,
1311 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1314 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1315 "delete-from-cursor", 2,
1316 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1319 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1320 "delete-from-cursor", 2,
1321 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1324 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1325 "delete-from-cursor", 2,
1326 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1329 /* Cut/copy/paste */
1331 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
1332 "cut-clipboard", 0);
1333 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
1334 "copy-clipboard", 0);
1335 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
1336 "paste-clipboard", 0);
1338 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
1339 "cut-clipboard", 0);
1340 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
1341 "copy-clipboard", 0);
1342 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
1343 "paste-clipboard", 0);
1346 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1347 "toggle-overwrite", 0);
1348 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1349 "toggle-overwrite", 0);
1352 gtk_binding_entry_add_signal (binding_set, GDK_F7, 0,
1353 "toggle-cursor-visible", 0);
1355 /* Control-tab focus motion */
1356 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
1358 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1359 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
1361 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1363 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1365 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1366 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1368 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1370 g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1374 gtk_text_view_init (GtkTextView *text_view)
1376 GtkWidget *widget = GTK_WIDGET (text_view);
1377 GtkTargetList *target_list;
1378 GtkTextViewPrivate *priv;
1380 text_view->priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
1381 priv = text_view->priv;
1383 gtk_widget_set_can_focus (widget, TRUE);
1385 /* Set up default style */
1386 priv->wrap_mode = GTK_WRAP_NONE;
1387 priv->pixels_above_lines = 0;
1388 priv->pixels_below_lines = 0;
1389 priv->pixels_inside_wrap = 0;
1390 priv->justify = GTK_JUSTIFY_LEFT;
1391 priv->left_margin = 0;
1392 priv->right_margin = 0;
1395 priv->editable = TRUE;
1397 priv->scroll_after_paste = TRUE;
1399 gtk_drag_dest_set (widget, 0, NULL, 0,
1400 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1402 target_list = gtk_target_list_new (NULL, 0);
1403 gtk_drag_dest_set_target_list (widget, target_list);
1404 gtk_target_list_unref (target_list);
1406 priv->virtual_cursor_x = -1;
1407 priv->virtual_cursor_y = -1;
1409 /* This object is completely private. No external entity can gain a reference
1410 * to it; so we create it here and destroy it in finalize ().
1412 priv->im_context = gtk_im_multicontext_new ();
1414 g_signal_connect (priv->im_context, "commit",
1415 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1416 g_signal_connect (priv->im_context, "preedit-changed",
1417 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1418 g_signal_connect (priv->im_context, "retrieve-surrounding",
1419 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1420 g_signal_connect (priv->im_context, "delete-surrounding",
1421 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1423 priv->cursor_visible = TRUE;
1425 priv->accepts_tab = TRUE;
1427 priv->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1430 priv->drag_start_x = -1;
1431 priv->drag_start_y = -1;
1433 priv->pending_place_cursor_button = 0;
1435 /* We handle all our own redrawing */
1436 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1440 * gtk_text_view_new:
1442 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1443 * before using the text view, an empty default buffer will be created
1444 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1445 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1447 * Return value: a new #GtkTextView
1450 gtk_text_view_new (void)
1452 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1456 * gtk_text_view_new_with_buffer:
1457 * @buffer: a #GtkTextBuffer
1459 * Creates a new #GtkTextView widget displaying the buffer
1460 * @buffer. One buffer can be shared among many widgets.
1461 * @buffer may be %NULL to create a default buffer, in which case
1462 * this function is equivalent to gtk_text_view_new(). The
1463 * text view adds its own reference count to the buffer; it does not
1464 * take over an existing reference.
1466 * Return value: a new #GtkTextView.
1469 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1471 GtkTextView *text_view;
1473 text_view = (GtkTextView*)gtk_text_view_new ();
1475 gtk_text_view_set_buffer (text_view, buffer);
1477 return GTK_WIDGET (text_view);
1481 * gtk_text_view_set_buffer:
1482 * @text_view: a #GtkTextView
1483 * @buffer: (allow-none): a #GtkTextBuffer
1485 * Sets @buffer as the buffer being displayed by @text_view. The previous
1486 * buffer displayed by the text view is unreferenced, and a reference is
1487 * added to @buffer. If you owned a reference to @buffer before passing it
1488 * to this function, you must remove that reference yourself; #GtkTextView
1489 * will not "adopt" it.
1492 gtk_text_view_set_buffer (GtkTextView *text_view,
1493 GtkTextBuffer *buffer)
1495 GtkTextViewPrivate *priv;
1497 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1498 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1500 priv = text_view->priv;
1502 if (priv->buffer == buffer)
1505 if (priv->buffer != NULL)
1507 /* Destroy all anchored children */
1511 copy = g_slist_copy (priv->children);
1513 while (tmp_list != NULL)
1515 GtkTextViewChild *vc = tmp_list->data;
1519 gtk_widget_destroy (vc->widget);
1520 /* vc may now be invalid! */
1523 tmp_list = g_slist_next (tmp_list);
1526 g_slist_free (copy);
1528 g_signal_handlers_disconnect_by_func (priv->buffer,
1529 gtk_text_view_mark_set_handler,
1531 g_signal_handlers_disconnect_by_func (priv->buffer,
1532 gtk_text_view_target_list_notify,
1534 g_signal_handlers_disconnect_by_func (priv->buffer,
1535 gtk_text_view_paste_done_handler,
1538 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
1540 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1541 GDK_SELECTION_PRIMARY);
1542 gtk_text_buffer_remove_selection_clipboard (priv->buffer, clipboard);
1546 gtk_text_layout_set_buffer (priv->layout, NULL);
1548 g_object_unref (priv->buffer);
1549 priv->dnd_mark = NULL;
1550 priv->first_para_mark = NULL;
1551 cancel_pending_scroll (text_view);
1554 priv->buffer = buffer;
1557 gtk_text_layout_set_buffer (priv->layout, buffer);
1563 g_object_ref (buffer);
1565 gtk_text_buffer_get_iter_at_offset (priv->buffer, &start, 0);
1567 priv->dnd_mark = gtk_text_buffer_create_mark (priv->buffer,
1571 priv->first_para_mark = gtk_text_buffer_create_mark (priv->buffer,
1575 priv->first_para_pixels = 0;
1578 g_signal_connect (priv->buffer, "mark-set",
1579 G_CALLBACK (gtk_text_view_mark_set_handler),
1581 g_signal_connect (priv->buffer, "notify::paste-target-list",
1582 G_CALLBACK (gtk_text_view_target_list_notify),
1584 g_signal_connect (priv->buffer, "paste-done",
1585 G_CALLBACK (gtk_text_view_paste_done_handler),
1588 gtk_text_view_target_list_notify (priv->buffer, NULL, text_view);
1590 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
1592 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1593 GDK_SELECTION_PRIMARY);
1594 gtk_text_buffer_add_selection_clipboard (priv->buffer, clipboard);
1598 g_object_notify (G_OBJECT (text_view), "buffer");
1600 if (gtk_widget_get_visible (GTK_WIDGET (text_view)))
1601 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1603 DV(g_print ("Invalidating due to set_buffer\n"));
1604 gtk_text_view_invalidate (text_view);
1607 static GtkTextBuffer*
1608 get_buffer (GtkTextView *text_view)
1610 if (text_view->priv->buffer == NULL)
1613 b = gtk_text_buffer_new (NULL);
1614 gtk_text_view_set_buffer (text_view, b);
1618 return text_view->priv->buffer;
1622 * gtk_text_view_get_buffer:
1623 * @text_view: a #GtkTextView
1625 * Returns the #GtkTextBuffer being displayed by this text view.
1626 * The reference count on the buffer is not incremented; the caller
1627 * of this function won't own a new reference.
1629 * Return value: (transfer none): a #GtkTextBuffer
1632 gtk_text_view_get_buffer (GtkTextView *text_view)
1634 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1636 return get_buffer (text_view);
1640 * gtk_text_view_get_iter_at_location:
1641 * @text_view: a #GtkTextView
1642 * @iter: (out): a #GtkTextIter
1643 * @x: x position, in buffer coordinates
1644 * @y: y position, in buffer coordinates
1646 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1647 * coordinates are coordinates for the entire buffer, not just the
1648 * currently-displayed portion. If you have coordinates from an
1649 * event, you have to convert those to buffer coordinates with
1650 * gtk_text_view_window_to_buffer_coords().
1653 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1658 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1659 g_return_if_fail (iter != NULL);
1661 gtk_text_view_ensure_layout (text_view);
1663 gtk_text_layout_get_iter_at_pixel (text_view->priv->layout,
1668 * gtk_text_view_get_iter_at_position:
1669 * @text_view: a #GtkTextView
1670 * @iter: (out): a #GtkTextIter
1671 * @trailing: (out) (allow-none): if non-%NULL, location to store an integer indicating where
1672 * in the grapheme the user clicked. It will either be
1673 * zero, or the number of characters in the grapheme.
1674 * 0 represents the trailing edge of the grapheme.
1675 * @x: x position, in buffer coordinates
1676 * @y: y position, in buffer coordinates
1678 * Retrieves the iterator pointing to the character at buffer
1679 * coordinates @x and @y. Buffer coordinates are coordinates for
1680 * the entire buffer, not just the currently-displayed portion.
1681 * If you have coordinates from an event, you have to convert
1682 * those to buffer coordinates with
1683 * gtk_text_view_window_to_buffer_coords().
1685 * Note that this is different from gtk_text_view_get_iter_at_location(),
1686 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1692 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1698 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1699 g_return_if_fail (iter != NULL);
1701 gtk_text_view_ensure_layout (text_view);
1703 gtk_text_layout_get_iter_at_position (text_view->priv->layout,
1704 iter, trailing, x, y);
1708 * gtk_text_view_get_iter_location:
1709 * @text_view: a #GtkTextView
1710 * @iter: a #GtkTextIter
1711 * @location: (out): bounds of the character at @iter
1713 * Gets a rectangle which roughly contains the character at @iter.
1714 * The rectangle position is in buffer coordinates; use
1715 * gtk_text_view_buffer_to_window_coords() to convert these
1716 * coordinates to coordinates for one of the windows in the text view.
1719 gtk_text_view_get_iter_location (GtkTextView *text_view,
1720 const GtkTextIter *iter,
1721 GdkRectangle *location)
1723 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1724 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1726 gtk_text_view_ensure_layout (text_view);
1728 gtk_text_layout_get_iter_location (text_view->priv->layout, iter, location);
1732 * gtk_text_view_get_line_yrange:
1733 * @text_view: a #GtkTextView
1734 * @iter: a #GtkTextIter
1735 * @y: (out): return location for a y coordinate
1736 * @height: (out): return location for a height
1738 * Gets the y coordinate of the top of the line containing @iter,
1739 * and the height of the line. The coordinate is a buffer coordinate;
1740 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1743 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1744 const GtkTextIter *iter,
1748 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1749 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1751 gtk_text_view_ensure_layout (text_view);
1753 gtk_text_layout_get_line_yrange (text_view->priv->layout,
1760 * gtk_text_view_get_line_at_y:
1761 * @text_view: a #GtkTextView
1762 * @target_iter: (out): a #GtkTextIter
1763 * @y: a y coordinate
1764 * @line_top: (out): return location for top coordinate of the line
1766 * Gets the #GtkTextIter at the start of the line containing
1767 * the coordinate @y. @y is in buffer coordinates, convert from
1768 * window coordinates with gtk_text_view_window_to_buffer_coords().
1769 * If non-%NULL, @line_top will be filled with the coordinate of the top
1773 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1774 GtkTextIter *target_iter,
1778 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1780 gtk_text_view_ensure_layout (text_view);
1782 gtk_text_layout_get_line_at_y (text_view->priv->layout,
1789 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1791 DV (g_print (" Setting adj to raw value %g\n", val));
1793 /* We don't really want to clamp to upper; we want to clamp to
1794 upper - page_size which is the highest value the scrollbar
1795 will let us reach. */
1796 if (val > (adj->upper - adj->page_size))
1797 val = adj->upper - adj->page_size;
1799 if (val < adj->lower)
1802 if (val != adj->value)
1804 DV (g_print (" Setting adj to clamped value %g\n", val));
1805 gtk_adjustment_set_value (adj, val);
1813 * gtk_text_view_scroll_to_iter:
1814 * @text_view: a #GtkTextView
1815 * @iter: a #GtkTextIter
1816 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1817 * @use_align: whether to use alignment arguments (if %FALSE,
1818 * just get the mark onscreen)
1819 * @xalign: horizontal alignment of mark within visible area
1820 * @yalign: vertical alignment of mark within visible area
1822 * Scrolls @text_view so that @iter is on the screen in the position
1823 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1824 * left or top, 1.0 indicates right or bottom, 0.5 means center.
1825 * If @use_align is %FALSE, the text scrolls the minimal distance to
1826 * get the mark onscreen, possibly not scrolling at all. The effective
1827 * screen for purposes of this function is reduced by a margin of size
1830 * Note that this function uses the currently-computed height of the
1831 * lines in the text buffer. Line heights are computed in an idle
1832 * handler; so this function may not have the desired effect if it's
1833 * called before the height computations. To avoid oddness, consider
1834 * using gtk_text_view_scroll_to_mark() which saves a point to be
1835 * scrolled to after line validation.
1837 * Return value: %TRUE if scrolling occurred
1840 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1842 gdouble within_margin,
1848 GdkRectangle screen;
1853 gboolean retval = FALSE;
1855 gint screen_xoffset, screen_yoffset;
1856 gint current_x_scroll, current_y_scroll;
1858 /* FIXME why don't we do the validate-at-scroll-destination thing
1859 * from flush_scroll in this function? I think it wasn't done before
1860 * because changed_handler was screwed up, but I could be wrong.
1863 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1864 g_return_val_if_fail (iter != NULL, FALSE);
1865 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1866 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1867 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1869 widget = GTK_WIDGET (text_view);
1871 DV(g_print(G_STRLOC"\n"));
1873 gtk_text_layout_get_iter_location (text_view->priv->layout,
1877 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1879 current_x_scroll = text_view->priv->xoffset;
1880 current_y_scroll = text_view->priv->yoffset;
1882 screen.x = current_x_scroll;
1883 screen.y = current_y_scroll;
1884 screen.width = SCREEN_WIDTH (widget);
1885 screen.height = SCREEN_HEIGHT (widget);
1887 screen_xoffset = screen.width * within_margin;
1888 screen_yoffset = screen.height * within_margin;
1890 screen.x += screen_xoffset;
1891 screen.y += screen_yoffset;
1892 screen.width -= screen_xoffset * 2;
1893 screen.height -= screen_yoffset * 2;
1895 /* paranoia check */
1896 if (screen.width < 1)
1898 if (screen.height < 1)
1901 /* The -1 here ensures that we leave enough space to draw the cursor
1902 * when this function is used for horizontal scrolling.
1904 screen_right = screen.x + screen.width - 1;
1905 screen_bottom = screen.y + screen.height;
1907 /* The alignment affects the point in the target character that we
1908 * choose to align. If we're doing right/bottom alignment, we align
1909 * the right/bottom edge of the character the mark is at; if we're
1910 * doing left/top we align the left/top edge of the character; if
1911 * we're doing center alignment we align the center of the
1915 /* Vertical scroll */
1918 scroll_dest = current_y_scroll;
1922 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1924 /* if scroll_dest < screen.y, we move a negative increment (up),
1925 * else a positive increment (down)
1927 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1931 /* move minimum to get onscreen */
1932 if (rect.y < screen.y)
1934 scroll_dest = rect.y;
1935 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1937 else if ((rect.y + rect.height) > screen_bottom)
1939 scroll_dest = rect.y + rect.height;
1940 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1944 if (scroll_inc != 0)
1946 retval = set_adjustment_clamped (get_vadjustment (text_view),
1947 current_y_scroll + scroll_inc);
1949 DV (g_print (" vert increment %d\n", scroll_inc));
1952 /* Horizontal scroll */
1955 scroll_dest = current_x_scroll;
1959 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1961 /* if scroll_dest < screen.y, we move a negative increment (left),
1962 * else a positive increment (right)
1964 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1968 /* move minimum to get onscreen */
1969 if (rect.x < screen.x)
1971 scroll_dest = rect.x;
1972 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1974 else if ((rect.x + rect.width) > screen_right)
1976 scroll_dest = rect.x + rect.width;
1977 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1981 if (scroll_inc != 0)
1983 retval = set_adjustment_clamped (get_hadjustment (text_view),
1984 current_x_scroll + scroll_inc);
1986 DV (g_print (" horiz increment %d\n", scroll_inc));
1991 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1995 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
2002 free_pending_scroll (GtkTextPendingScroll *scroll)
2004 if (!gtk_text_mark_get_deleted (scroll->mark))
2005 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
2007 g_object_unref (scroll->mark);
2012 cancel_pending_scroll (GtkTextView *text_view)
2014 if (text_view->priv->pending_scroll)
2016 free_pending_scroll (text_view->priv->pending_scroll);
2017 text_view->priv->pending_scroll = NULL;
2022 gtk_text_view_queue_scroll (GtkTextView *text_view,
2024 gdouble within_margin,
2030 GtkTextPendingScroll *scroll;
2032 DV(g_print(G_STRLOC"\n"));
2034 scroll = g_new (GtkTextPendingScroll, 1);
2036 scroll->within_margin = within_margin;
2037 scroll->use_align = use_align;
2038 scroll->xalign = xalign;
2039 scroll->yalign = yalign;
2041 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2043 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
2046 gtk_text_mark_get_left_gravity (mark));
2048 g_object_ref (scroll->mark);
2050 cancel_pending_scroll (text_view);
2052 text_view->priv->pending_scroll = scroll;
2056 gtk_text_view_flush_scroll (GtkTextView *text_view)
2059 GtkTextPendingScroll *scroll;
2063 widget = GTK_WIDGET (text_view);
2065 DV(g_print(G_STRLOC"\n"));
2067 if (text_view->priv->pending_scroll == NULL)
2069 DV (g_print ("in flush scroll, no pending scroll\n"));
2073 scroll = text_view->priv->pending_scroll;
2075 /* avoid recursion */
2076 text_view->priv->pending_scroll = NULL;
2078 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
2080 /* Validate area around the scroll destination, so the adjustment
2081 * can meaningfully point into that area. We must validate
2082 * enough area to be sure that after we scroll, everything onscreen
2083 * is valid; otherwise, validation will maintain the first para
2084 * in one place, but may push the target iter off the bottom of
2087 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
2088 gtk_text_layout_validate_yrange (text_view->priv->layout, &iter,
2089 - (widget->allocation.height * 2),
2090 widget->allocation.height * 2);
2092 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
2094 /* Ensure we have updated width/height */
2095 gtk_text_view_update_adjustments (text_view);
2097 retval = gtk_text_view_scroll_to_iter (text_view,
2099 scroll->within_margin,
2104 free_pending_scroll (scroll);
2110 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
2112 if (upper != adj->upper)
2114 gdouble min = MAX (0.0, upper - adj->page_size);
2115 gboolean value_changed = FALSE;
2119 if (adj->value > min)
2122 value_changed = TRUE;
2125 gtk_adjustment_changed (adj);
2126 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
2130 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
2131 gtk_adjustment_value_changed (adj);
2137 gtk_text_view_update_adjustments (GtkTextView *text_view)
2139 GtkTextViewPrivate *priv;
2140 gint width = 0, height = 0;
2142 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
2144 priv = text_view->priv;
2147 gtk_text_layout_get_size (priv->layout, &width, &height);
2149 /* Make room for the cursor after the last character in the widest line */
2150 width += SPACE_FOR_CURSOR;
2152 if (priv->width != width || priv->height != height)
2154 if (priv->width != width)
2155 priv->width_changed = TRUE;
2157 priv->width = width;
2158 priv->height = height;
2160 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
2161 MAX (SCREEN_WIDTH (text_view), width));
2162 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
2163 MAX (SCREEN_HEIGHT (text_view), height));
2165 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
2167 /* Set up the step sizes; we'll say that a page is
2168 our allocation minus one step, and a step is
2169 1/10 of our allocation. */
2170 priv->hadjustment->step_increment =
2171 SCREEN_WIDTH (text_view) / 10.0;
2172 priv->hadjustment->page_increment =
2173 SCREEN_WIDTH (text_view) * 0.9;
2175 priv->vadjustment->step_increment =
2176 SCREEN_HEIGHT (text_view) / 10.0;
2177 priv->vadjustment->page_increment =
2178 SCREEN_HEIGHT (text_view) * 0.9;
2180 gtk_adjustment_changed (get_hadjustment (text_view));
2181 gtk_adjustment_changed (get_vadjustment (text_view));
2186 gtk_text_view_update_layout_width (GtkTextView *text_view)
2188 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
2190 gtk_text_view_ensure_layout (text_view);
2192 gtk_text_layout_set_screen_width (text_view->priv->layout,
2193 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
2197 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
2201 if (text_view->priv->layout == NULL)
2204 gtk_text_view_get_cursor_location (text_view, &area);
2206 area.x -= text_view->priv->xoffset;
2207 area.y -= text_view->priv->yoffset;
2209 /* Width returned by Pango indicates direction of cursor,
2210 * by it's sign more than the size of cursor.
2214 gtk_im_context_set_cursor_location (text_view->priv->im_context, &area);
2218 do_update_im_spot_location (gpointer text_view)
2220 GtkTextViewPrivate *priv;
2222 priv = GTK_TEXT_VIEW (text_view)->priv;
2223 priv->im_spot_idle = 0;
2225 gtk_text_view_update_im_spot_location (text_view);
2230 queue_update_im_spot_location (GtkTextView *text_view)
2232 GtkTextViewPrivate *priv;
2234 priv = text_view->priv;
2236 /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
2237 * so we don't wait until the entire buffer has been validated. */
2238 if (!priv->im_spot_idle)
2239 priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
2240 do_update_im_spot_location,
2246 flush_update_im_spot_location (GtkTextView *text_view)
2248 GtkTextViewPrivate *priv;
2250 priv = text_view->priv;
2252 if (priv->im_spot_idle)
2254 g_source_remove (priv->im_spot_idle);
2255 priv->im_spot_idle = 0;
2256 gtk_text_view_update_im_spot_location (text_view);
2261 * gtk_text_view_scroll_to_mark:
2262 * @text_view: a #GtkTextView
2263 * @mark: a #GtkTextMark
2264 * @within_margin: margin as a [0.0,0.5) fraction of screen size
2265 * @use_align: whether to use alignment arguments (if %FALSE, just
2266 * get the mark onscreen)
2267 * @xalign: horizontal alignment of mark within visible area
2268 * @yalign: vertical alignment of mark within visible area
2270 * Scrolls @text_view so that @mark is on the screen in the position
2271 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
2272 * left or top, 1.0 indicates right or bottom, 0.5 means center.
2273 * If @use_align is %FALSE, the text scrolls the minimal distance to
2274 * get the mark onscreen, possibly not scrolling at all. The effective
2275 * screen for purposes of this function is reduced by a margin of size
2279 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
2281 gdouble within_margin,
2286 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2287 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2288 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
2289 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
2290 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
2292 /* We need to verify that the buffer contains the mark, otherwise this
2293 * can lead to data structure corruption later on.
2295 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2297 gtk_text_view_queue_scroll (text_view, mark,
2303 /* If no validation is pending, we need to go ahead and force an
2306 if (text_view->priv->layout &&
2307 gtk_text_layout_is_valid (text_view->priv->layout))
2308 gtk_text_view_flush_scroll (text_view);
2312 * gtk_text_view_scroll_mark_onscreen:
2313 * @text_view: a #GtkTextView
2314 * @mark: a mark in the buffer for @text_view
2316 * Scrolls @text_view the minimum distance such that @mark is contained
2317 * within the visible area of the widget.
2320 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
2323 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2324 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2326 /* We need to verify that the buffer contains the mark, otherwise this
2327 * can lead to data structure corruption later on.
2329 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2331 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
2335 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
2337 GdkRectangle visible_rect;
2338 gtk_text_view_get_visible_rect (text_view, &visible_rect);
2340 return gtk_text_layout_clamp_iter_to_vrange (text_view->priv->layout, iter,
2342 visible_rect.y + visible_rect.height);
2346 * gtk_text_view_move_mark_onscreen:
2347 * @text_view: a #GtkTextView
2348 * @mark: a #GtkTextMark
2350 * Moves a mark within the buffer so that it's
2351 * located within the currently-visible text area.
2353 * Return value: %TRUE if the mark moved (wasn't already onscreen)
2356 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
2361 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2362 g_return_val_if_fail (mark != NULL, FALSE);
2364 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2366 if (clamp_iter_onscreen (text_view, &iter))
2368 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
2376 * gtk_text_view_get_visible_rect:
2377 * @text_view: a #GtkTextView
2378 * @visible_rect: rectangle to fill
2380 * Fills @visible_rect with the currently-visible
2381 * region of the buffer, in buffer coordinates. Convert to window coordinates
2382 * with gtk_text_view_buffer_to_window_coords().
2385 gtk_text_view_get_visible_rect (GtkTextView *text_view,
2386 GdkRectangle *visible_rect)
2390 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2392 widget = GTK_WIDGET (text_view);
2396 visible_rect->x = text_view->priv->xoffset;
2397 visible_rect->y = text_view->priv->yoffset;
2398 visible_rect->width = SCREEN_WIDTH (widget);
2399 visible_rect->height = SCREEN_HEIGHT (widget);
2401 DV(g_print(" visible rect: %d,%d %d x %d\n",
2404 visible_rect->width,
2405 visible_rect->height));
2410 * gtk_text_view_set_wrap_mode:
2411 * @text_view: a #GtkTextView
2412 * @wrap_mode: a #GtkWrapMode
2414 * Sets the line wrapping for the view.
2417 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
2418 GtkWrapMode wrap_mode)
2420 GtkTextViewPrivate *priv;
2422 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2424 priv = text_view->priv;
2426 if (priv->wrap_mode != wrap_mode)
2428 priv->wrap_mode = wrap_mode;
2432 priv->layout->default_style->wrap_mode = wrap_mode;
2433 gtk_text_layout_default_style_changed (priv->layout);
2437 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2441 * gtk_text_view_get_wrap_mode:
2442 * @text_view: a #GtkTextView
2444 * Gets the line wrapping for the view.
2446 * Return value: the line wrap setting
2449 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2451 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2453 return text_view->priv->wrap_mode;
2457 * gtk_text_view_set_editable:
2458 * @text_view: a #GtkTextView
2459 * @setting: whether it's editable
2461 * Sets the default editability of the #GtkTextView. You can override
2462 * this default setting with tags in the buffer, using the "editable"
2463 * attribute of tags.
2466 gtk_text_view_set_editable (GtkTextView *text_view,
2469 GtkTextViewPrivate *priv;
2471 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2473 priv = text_view->priv;
2474 setting = setting != FALSE;
2476 if (priv->editable != setting)
2480 gtk_text_view_reset_im_context(text_view);
2481 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2482 gtk_im_context_focus_out (priv->im_context);
2485 priv->editable = setting;
2487 if (setting && gtk_widget_has_focus (GTK_WIDGET (text_view)))
2488 gtk_im_context_focus_in (priv->im_context);
2492 gtk_text_layout_set_overwrite_mode (priv->layout,
2493 priv->overwrite_mode && priv->editable);
2494 priv->layout->default_style->editable = priv->editable;
2495 gtk_text_layout_default_style_changed (priv->layout);
2498 g_object_notify (G_OBJECT (text_view), "editable");
2503 * gtk_text_view_get_editable:
2504 * @text_view: a #GtkTextView
2506 * Returns the default editability of the #GtkTextView. Tags in the
2507 * buffer may override this setting for some ranges of text.
2509 * Return value: whether text is editable by default
2512 gtk_text_view_get_editable (GtkTextView *text_view)
2514 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2516 return text_view->priv->editable;
2520 * gtk_text_view_set_pixels_above_lines:
2521 * @text_view: a #GtkTextView
2522 * @pixels_above_lines: pixels above paragraphs
2524 * Sets the default number of blank pixels above paragraphs in @text_view.
2525 * Tags in the buffer for @text_view may override the defaults.
2528 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2529 gint pixels_above_lines)
2531 GtkTextViewPrivate *priv;
2533 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2535 priv = text_view->priv;
2537 if (priv->pixels_above_lines != pixels_above_lines)
2539 priv->pixels_above_lines = pixels_above_lines;
2543 priv->layout->default_style->pixels_above_lines = pixels_above_lines;
2544 gtk_text_layout_default_style_changed (priv->layout);
2547 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2552 * gtk_text_view_get_pixels_above_lines:
2553 * @text_view: a #GtkTextView
2555 * Gets the default number of pixels to put above paragraphs.
2557 * Return value: default number of pixels above paragraphs
2560 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2562 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2564 return text_view->priv->pixels_above_lines;
2568 * gtk_text_view_set_pixels_below_lines:
2569 * @text_view: a #GtkTextView
2570 * @pixels_below_lines: pixels below paragraphs
2572 * Sets the default number of pixels of blank space
2573 * to put below paragraphs in @text_view. May be overridden
2574 * by tags applied to @text_view's buffer.
2577 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2578 gint pixels_below_lines)
2580 GtkTextViewPrivate *priv;
2582 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2584 priv = text_view->priv;
2586 if (priv->pixels_below_lines != pixels_below_lines)
2588 priv->pixels_below_lines = pixels_below_lines;
2592 priv->layout->default_style->pixels_below_lines = pixels_below_lines;
2593 gtk_text_layout_default_style_changed (priv->layout);
2596 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2601 * gtk_text_view_get_pixels_below_lines:
2602 * @text_view: a #GtkTextView
2604 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2606 * Return value: default number of blank pixels below paragraphs
2609 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2611 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2613 return text_view->priv->pixels_below_lines;
2617 * gtk_text_view_set_pixels_inside_wrap:
2618 * @text_view: a #GtkTextView
2619 * @pixels_inside_wrap: default number of pixels between wrapped lines
2621 * Sets the default number of pixels of blank space to leave between
2622 * display/wrapped lines within a paragraph. May be overridden by
2623 * tags in @text_view's buffer.
2626 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2627 gint pixels_inside_wrap)
2629 GtkTextViewPrivate *priv;
2631 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2633 priv = text_view->priv;
2635 if (priv->pixels_inside_wrap != pixels_inside_wrap)
2637 priv->pixels_inside_wrap = pixels_inside_wrap;
2641 priv->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2642 gtk_text_layout_default_style_changed (priv->layout);
2645 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2650 * gtk_text_view_get_pixels_inside_wrap:
2651 * @text_view: a #GtkTextView
2653 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2655 * Return value: default number of pixels of blank space between wrapped lines
2658 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2660 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2662 return text_view->priv->pixels_inside_wrap;
2666 * gtk_text_view_set_justification:
2667 * @text_view: a #GtkTextView
2668 * @justification: justification
2670 * Sets the default justification of text in @text_view.
2671 * Tags in the view's buffer may override the default.
2675 gtk_text_view_set_justification (GtkTextView *text_view,
2676 GtkJustification justification)
2678 GtkTextViewPrivate *priv;
2680 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2682 priv = text_view->priv;
2684 if (priv->justify != justification)
2686 priv->justify = justification;
2690 priv->layout->default_style->justification = justification;
2691 gtk_text_layout_default_style_changed (priv->layout);
2694 g_object_notify (G_OBJECT (text_view), "justification");
2699 * gtk_text_view_get_justification:
2700 * @text_view: a #GtkTextView
2702 * Gets the default justification of paragraphs in @text_view.
2703 * Tags in the buffer may override the default.
2705 * Return value: default justification
2708 gtk_text_view_get_justification (GtkTextView *text_view)
2710 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2712 return text_view->priv->justify;
2716 * gtk_text_view_set_left_margin:
2717 * @text_view: a #GtkTextView
2718 * @left_margin: left margin in pixels
2720 * Sets the default left margin for text in @text_view.
2721 * Tags in the buffer may override the default.
2724 gtk_text_view_set_left_margin (GtkTextView *text_view,
2727 GtkTextViewPrivate *priv;
2729 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2731 priv = text_view->priv;
2733 if (priv->left_margin != left_margin)
2735 priv->left_margin = left_margin;
2739 priv->layout->default_style->left_margin = left_margin;
2740 gtk_text_layout_default_style_changed (priv->layout);
2743 g_object_notify (G_OBJECT (text_view), "left-margin");
2748 * gtk_text_view_get_left_margin:
2749 * @text_view: a #GtkTextView
2751 * Gets the default left margin size of paragraphs in the @text_view.
2752 * Tags in the buffer may override the default.
2754 * Return value: left margin in pixels
2757 gtk_text_view_get_left_margin (GtkTextView *text_view)
2759 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2761 return text_view->priv->left_margin;
2765 * gtk_text_view_set_right_margin:
2766 * @text_view: a #GtkTextView
2767 * @right_margin: right margin in pixels
2769 * Sets the default right margin for text in the text view.
2770 * Tags in the buffer may override the default.
2773 gtk_text_view_set_right_margin (GtkTextView *text_view,
2776 GtkTextViewPrivate *priv = text_view->priv;
2778 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2780 if (priv->right_margin != right_margin)
2782 priv->right_margin = right_margin;
2786 priv->layout->default_style->right_margin = right_margin;
2787 gtk_text_layout_default_style_changed (priv->layout);
2790 g_object_notify (G_OBJECT (text_view), "right-margin");
2795 * gtk_text_view_get_right_margin:
2796 * @text_view: a #GtkTextView
2798 * Gets the default right margin for text in @text_view. Tags
2799 * in the buffer may override the default.
2801 * Return value: right margin in pixels
2804 gtk_text_view_get_right_margin (GtkTextView *text_view)
2806 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2808 return text_view->priv->right_margin;
2812 * gtk_text_view_set_indent:
2813 * @text_view: a #GtkTextView
2814 * @indent: indentation in pixels
2816 * Sets the default indentation for paragraphs in @text_view.
2817 * Tags in the buffer may override the default.
2820 gtk_text_view_set_indent (GtkTextView *text_view,
2823 GtkTextViewPrivate *priv;
2825 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2827 priv = text_view->priv;
2829 if (priv->indent != indent)
2831 priv->indent = indent;
2835 priv->layout->default_style->indent = indent;
2836 gtk_text_layout_default_style_changed (priv->layout);
2839 g_object_notify (G_OBJECT (text_view), "indent");
2844 * gtk_text_view_get_indent:
2845 * @text_view: a #GtkTextView
2847 * Gets the default indentation of paragraphs in @text_view.
2848 * Tags in the view's buffer may override the default.
2849 * The indentation may be negative.
2851 * Return value: number of pixels of indentation
2854 gtk_text_view_get_indent (GtkTextView *text_view)
2856 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2858 return text_view->priv->indent;
2862 * gtk_text_view_set_tabs:
2863 * @text_view: a #GtkTextView
2864 * @tabs: tabs as a #PangoTabArray
2866 * Sets the default tab stops for paragraphs in @text_view.
2867 * Tags in the buffer may override the default.
2870 gtk_text_view_set_tabs (GtkTextView *text_view,
2871 PangoTabArray *tabs)
2873 GtkTextViewPrivate *priv;
2875 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2877 priv = text_view->priv;
2880 pango_tab_array_free (priv->tabs);
2882 priv->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2886 /* some unkosher futzing in internal struct details... */
2887 if (priv->layout->default_style->tabs)
2888 pango_tab_array_free (priv->layout->default_style->tabs);
2890 priv->layout->default_style->tabs =
2891 priv->tabs ? pango_tab_array_copy (priv->tabs) : NULL;
2893 gtk_text_layout_default_style_changed (priv->layout);
2896 g_object_notify (G_OBJECT (text_view), "tabs");
2900 * gtk_text_view_get_tabs:
2901 * @text_view: a #GtkTextView
2903 * Gets the default tabs for @text_view. Tags in the buffer may
2904 * override the defaults. The returned array will be %NULL if
2905 * "standard" (8-space) tabs are used. Free the return value
2906 * with pango_tab_array_free().
2908 * Return value: copy of default tab array, or %NULL if "standard"
2909 * tabs are used; must be freed with pango_tab_array_free().
2912 gtk_text_view_get_tabs (GtkTextView *text_view)
2914 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2916 return text_view->priv->tabs ? pango_tab_array_copy (text_view->priv->tabs) : NULL;
2920 gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
2922 gtk_text_view_set_cursor_visible (text_view, !text_view->priv->cursor_visible);
2926 * gtk_text_view_set_cursor_visible:
2927 * @text_view: a #GtkTextView
2928 * @setting: whether to show the insertion cursor
2930 * Toggles whether the insertion point is displayed. A buffer with no editable
2931 * text probably shouldn't have a visible cursor, so you may want to turn
2935 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2938 GtkTextViewPrivate *priv;
2940 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2942 priv = text_view->priv;
2943 setting = (setting != FALSE);
2945 if (priv->cursor_visible != setting)
2947 priv->cursor_visible = setting;
2949 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2953 gtk_text_layout_set_cursor_visible (priv->layout, setting);
2954 gtk_text_view_check_cursor_blink (text_view);
2958 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2963 * gtk_text_view_get_cursor_visible:
2964 * @text_view: a #GtkTextView
2966 * Find out whether the cursor is being displayed.
2968 * Return value: whether the insertion mark is visible
2971 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2973 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2975 return text_view->priv->cursor_visible;
2980 * gtk_text_view_place_cursor_onscreen:
2981 * @text_view: a #GtkTextView
2983 * Moves the cursor to the currently visible region of the
2984 * buffer, it it isn't there already.
2986 * Return value: %TRUE if the cursor had to be moved.
2989 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2993 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2995 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2996 gtk_text_buffer_get_insert (get_buffer (text_view)));
2998 if (clamp_iter_onscreen (text_view, &insert))
3000 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
3008 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
3010 GtkTextViewPrivate *priv = text_view->priv;
3012 if (priv->first_validate_idle != 0)
3014 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
3015 g_source_remove (priv->first_validate_idle);
3016 priv->first_validate_idle = 0;
3019 if (priv->incremental_validate_idle != 0)
3021 g_source_remove (priv->incremental_validate_idle);
3022 priv->incremental_validate_idle = 0;
3027 gtk_text_view_destroy (GtkObject *object)
3029 GtkTextView *text_view;
3030 GtkTextViewPrivate *priv;
3032 text_view = GTK_TEXT_VIEW (object);
3033 priv = text_view->priv;
3035 gtk_text_view_remove_validate_idles (text_view);
3036 gtk_text_view_set_buffer (text_view, NULL);
3037 gtk_text_view_destroy_layout (text_view);
3039 if (text_view->priv->scroll_timeout)
3041 g_source_remove (text_view->priv->scroll_timeout);
3042 text_view->priv->scroll_timeout = 0;
3045 if (priv->im_spot_idle)
3047 g_source_remove (priv->im_spot_idle);
3048 priv->im_spot_idle = 0;
3051 GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy (object);
3055 gtk_text_view_finalize (GObject *object)
3057 GtkTextView *text_view;
3058 GtkTextViewPrivate *priv;
3060 text_view = GTK_TEXT_VIEW (object);
3061 priv = text_view->priv;
3063 g_assert (priv->buffer == NULL);
3065 gtk_text_view_destroy_layout (text_view);
3066 gtk_text_view_set_buffer (text_view, NULL);
3068 cancel_pending_scroll (text_view);
3071 pango_tab_array_free (priv->tabs);
3073 if (priv->hadjustment)
3074 g_object_unref (priv->hadjustment);
3075 if (priv->vadjustment)
3076 g_object_unref (priv->vadjustment);
3078 text_window_free (priv->text_window);
3080 if (priv->left_window)
3081 text_window_free (priv->left_window);
3083 if (priv->top_window)
3084 text_window_free (priv->top_window);
3086 if (priv->right_window)
3087 text_window_free (priv->right_window);
3089 if (priv->bottom_window)
3090 text_window_free (priv->bottom_window);
3092 g_object_unref (priv->im_context);
3094 g_free (priv->im_module);
3096 G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize (object);
3100 gtk_text_view_set_property (GObject *object,
3102 const GValue *value,
3105 GtkTextView *text_view;
3106 GtkTextViewPrivate *priv;
3108 text_view = GTK_TEXT_VIEW (object);
3109 priv = text_view->priv;
3113 case PROP_PIXELS_ABOVE_LINES:
3114 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
3117 case PROP_PIXELS_BELOW_LINES:
3118 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
3121 case PROP_PIXELS_INSIDE_WRAP:
3122 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
3126 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
3129 case PROP_WRAP_MODE:
3130 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
3133 case PROP_JUSTIFICATION:
3134 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
3137 case PROP_LEFT_MARGIN:
3138 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
3141 case PROP_RIGHT_MARGIN:
3142 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
3146 gtk_text_view_set_indent (text_view, g_value_get_int (value));
3150 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
3153 case PROP_CURSOR_VISIBLE:
3154 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
3157 case PROP_OVERWRITE:
3158 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
3162 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
3165 case PROP_ACCEPTS_TAB:
3166 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
3169 case PROP_IM_MODULE:
3170 g_free (priv->im_module);
3171 priv->im_module = g_value_dup_string (value);
3172 if (GTK_IS_IM_MULTICONTEXT (priv->im_context))
3173 gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (priv->im_context), priv->im_module);
3177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3183 gtk_text_view_get_property (GObject *object,
3188 GtkTextView *text_view;
3189 GtkTextViewPrivate *priv;
3191 text_view = GTK_TEXT_VIEW (object);
3192 priv = text_view->priv;
3196 case PROP_PIXELS_ABOVE_LINES:
3197 g_value_set_int (value, priv->pixels_above_lines);
3200 case PROP_PIXELS_BELOW_LINES:
3201 g_value_set_int (value, priv->pixels_below_lines);
3204 case PROP_PIXELS_INSIDE_WRAP:
3205 g_value_set_int (value, priv->pixels_inside_wrap);
3209 g_value_set_boolean (value, priv->editable);
3212 case PROP_WRAP_MODE:
3213 g_value_set_enum (value, priv->wrap_mode);
3216 case PROP_JUSTIFICATION:
3217 g_value_set_enum (value, priv->justify);
3220 case PROP_LEFT_MARGIN:
3221 g_value_set_int (value, priv->left_margin);
3224 case PROP_RIGHT_MARGIN:
3225 g_value_set_int (value, priv->right_margin);
3229 g_value_set_int (value, priv->indent);
3233 g_value_set_boxed (value, priv->tabs);
3236 case PROP_CURSOR_VISIBLE:
3237 g_value_set_boolean (value, priv->cursor_visible);
3241 g_value_set_object (value, get_buffer (text_view));
3244 case PROP_OVERWRITE:
3245 g_value_set_boolean (value, priv->overwrite_mode);
3248 case PROP_ACCEPTS_TAB:
3249 g_value_set_boolean (value, priv->accepts_tab);
3252 case PROP_IM_MODULE:
3253 g_value_set_string (value, priv->im_module);
3257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3263 gtk_text_view_size_request (GtkWidget *widget,
3264 GtkRequisition *requisition)
3266 GtkTextView *text_view;
3267 GtkTextViewPrivate *priv;
3269 gint focus_edge_width;
3271 gboolean interior_focus;
3273 text_view = GTK_TEXT_VIEW (widget);
3274 priv = text_view->priv;
3276 gtk_widget_style_get (widget,
3277 "interior-focus", &interior_focus,
3278 "focus-line-width", &focus_width,
3282 focus_edge_width = 0;
3284 focus_edge_width = focus_width;
3288 priv->text_window->requisition.width = priv->layout->width;
3289 priv->text_window->requisition.height = priv->layout->height;
3293 priv->text_window->requisition.width = 0;
3294 priv->text_window->requisition.height = 0;
3297 requisition->width = priv->text_window->requisition.width + focus_edge_width * 2;
3298 requisition->height = priv->text_window->requisition.height + focus_edge_width * 2;
3300 if (priv->left_window)
3301 requisition->width += priv->left_window->requisition.width;
3303 if (priv->right_window)
3304 requisition->width += priv->right_window->requisition.width;
3306 if (priv->top_window)
3307 requisition->height += priv->top_window->requisition.height;
3309 if (priv->bottom_window)
3310 requisition->height += priv->bottom_window->requisition.height;
3312 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
3313 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
3315 tmp_list = priv->children;
3316 while (tmp_list != NULL)
3318 GtkTextViewChild *child = tmp_list->data;
3322 GtkRequisition child_req;
3323 GtkRequisition old_req;
3325 gtk_widget_get_child_requisition (child->widget, &old_req);
3327 gtk_widget_size_request (child->widget, &child_req);
3329 gtk_widget_get_child_requisition (child->widget, &child_req);
3331 /* Invalidate layout lines if required */
3333 (old_req.width != child_req.width ||
3334 old_req.height != child_req.height))
3335 gtk_text_child_anchor_queue_resize (child->anchor,
3340 GtkRequisition child_req;
3342 gtk_widget_size_request (child->widget, &child_req);
3345 tmp_list = g_slist_next (tmp_list);
3350 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
3351 GtkTextViewChild *vc,
3352 GtkAllocation *allocation)
3358 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3362 gtk_text_layout_get_line_yrange (text_view->priv->layout, &iter,
3365 buffer_y += vc->from_top_of_line;
3367 allocation->x = vc->from_left_of_buffer - text_view->priv->xoffset;
3368 allocation->y = buffer_y - text_view->priv->yoffset;
3370 gtk_widget_get_child_requisition (vc->widget, &req);
3371 allocation->width = req.width;
3372 allocation->height = req.height;
3376 gtk_text_view_update_child_allocation (GtkTextView *text_view,
3377 GtkTextViewChild *vc)
3379 GtkAllocation allocation;
3381 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
3383 gtk_widget_size_allocate (vc->widget, &allocation);
3386 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
3388 vc->widget->allocation.x,
3389 vc->widget->allocation.y,
3390 text_view->priv->yoffset);
3395 gtk_text_view_child_allocated (GtkTextLayout *layout,
3401 GtkTextViewChild *vc = NULL;
3402 GtkTextView *text_view = data;
3404 /* x,y is the position of the child from the top of the line, and
3405 * from the left of the buffer. We have to translate that into text
3406 * window coordinates, then size_allocate the child.
3409 vc = g_object_get_data (G_OBJECT (child),
3410 "gtk-text-view-child");
3412 g_assert (vc != NULL);
3414 DV (g_print ("child allocated at %d,%d\n", x, y));
3416 vc->from_left_of_buffer = x;
3417 vc->from_top_of_line = y;
3419 gtk_text_view_update_child_allocation (text_view, vc);
3423 gtk_text_view_allocate_children (GtkTextView *text_view)
3427 DV(g_print(G_STRLOC"\n"));
3429 tmp_list = text_view->priv->children;
3430 while (tmp_list != NULL)
3432 GtkTextViewChild *child = tmp_list->data;
3434 g_assert (child != NULL);
3438 /* We need to force-validate the regions containing
3441 GtkTextIter child_loc;
3442 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3446 /* Since anchored children are only ever allocated from
3447 * gtk_text_layout_get_line_display() we have to make sure
3448 * that the display line caching in the layout doesn't
3449 * get in the way. Invalidating the layout around the anchor
3452 if (GTK_WIDGET_ALLOC_NEEDED (child->widget))
3454 GtkTextIter end = child_loc;
3455 gtk_text_iter_forward_char (&end);
3456 gtk_text_layout_invalidate (text_view->priv->layout, &child_loc, &end);
3459 gtk_text_layout_validate_yrange (text_view->priv->layout,
3465 GtkAllocation allocation;
3466 GtkRequisition child_req;
3468 allocation.x = child->x;
3469 allocation.y = child->y;
3471 gtk_widget_get_child_requisition (child->widget, &child_req);
3473 allocation.width = child_req.width;
3474 allocation.height = child_req.height;
3476 gtk_widget_size_allocate (child->widget, &allocation);
3479 tmp_list = g_slist_next (tmp_list);
3484 gtk_text_view_size_allocate (GtkWidget *widget,
3485 GtkAllocation *allocation)
3487 GtkTextView *text_view;
3488 GtkTextViewPrivate *priv;
3489 GtkTextIter first_para;
3492 GdkRectangle text_rect;
3493 GdkRectangle left_rect;
3494 GdkRectangle right_rect;
3495 GdkRectangle top_rect;
3496 GdkRectangle bottom_rect;
3497 gint focus_edge_width;
3499 gboolean interior_focus;
3500 gboolean size_changed;
3502 text_view = GTK_TEXT_VIEW (widget);
3503 priv = text_view->priv;
3505 DV(g_print(G_STRLOC"\n"));
3508 widget->allocation.width != allocation->width ||
3509 widget->allocation.height != allocation->height;
3511 widget->allocation = *allocation;
3513 if (gtk_widget_get_realized (widget))
3515 gdk_window_move_resize (widget->window,
3516 allocation->x, allocation->y,
3517 allocation->width, allocation->height);
3520 /* distribute width/height among child windows. Ensure all
3521 * windows get at least a 1x1 allocation.
3524 gtk_widget_style_get (widget,
3525 "interior-focus", &interior_focus,
3526 "focus-line-width", &focus_width,
3530 focus_edge_width = 0;
3532 focus_edge_width = focus_width;
3534 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3536 if (priv->left_window)
3537 left_rect.width = priv->left_window->requisition.width;
3539 left_rect.width = 0;
3541 width -= left_rect.width;
3543 if (priv->right_window)
3544 right_rect.width = priv->right_window->requisition.width;
3546 right_rect.width = 0;
3548 width -= right_rect.width;
3550 text_rect.width = MAX (1, width);
3552 top_rect.width = text_rect.width;
3553 bottom_rect.width = text_rect.width;
3556 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3558 if (priv->top_window)
3559 top_rect.height = priv->top_window->requisition.height;
3561 top_rect.height = 0;
3563 height -= top_rect.height;
3565 if (priv->bottom_window)
3566 bottom_rect.height = priv->bottom_window->requisition.height;
3568 bottom_rect.height = 0;
3570 height -= bottom_rect.height;
3572 text_rect.height = MAX (1, height);
3574 left_rect.height = text_rect.height;
3575 right_rect.height = text_rect.height;
3578 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3579 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3581 text_rect.x = left_rect.x + left_rect.width;
3582 text_rect.y = top_rect.y + top_rect.height;
3584 left_rect.y = text_rect.y;
3585 right_rect.y = text_rect.y;
3587 top_rect.x = text_rect.x;
3588 bottom_rect.x = text_rect.x;
3590 right_rect.x = text_rect.x + text_rect.width;
3591 bottom_rect.y = text_rect.y + text_rect.height;
3593 text_window_size_allocate (priv->text_window,
3596 if (priv->left_window)
3597 text_window_size_allocate (priv->left_window,
3600 if (priv->right_window)
3601 text_window_size_allocate (priv->right_window,
3604 if (priv->top_window)
3605 text_window_size_allocate (priv->top_window,
3608 if (priv->bottom_window)
3609 text_window_size_allocate (priv->bottom_window,
3612 gtk_text_view_update_layout_width (text_view);
3614 /* Note that this will do some layout validation */
3615 gtk_text_view_allocate_children (text_view);
3617 /* Ensure h/v adj exist */
3618 get_hadjustment (text_view);
3619 get_vadjustment (text_view);
3621 priv->hadjustment->page_size = SCREEN_WIDTH (text_view);
3622 priv->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
3623 priv->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
3624 priv->hadjustment->lower = 0;
3625 priv->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
3628 if (priv->hadjustment->value > priv->hadjustment->upper - priv->hadjustment->page_size)
3629 gtk_adjustment_set_value (priv->hadjustment, MAX (0, priv->hadjustment->upper - priv->hadjustment->page_size));
3631 gtk_adjustment_changed (priv->hadjustment);
3633 priv->vadjustment->page_size = SCREEN_HEIGHT (text_view);
3634 priv->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
3635 priv->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
3636 priv->vadjustment->lower = 0;
3637 priv->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
3640 /* Now adjust the value of the adjustment to keep the cursor at the
3641 * same place in the buffer
3643 gtk_text_view_get_first_para_iter (text_view, &first_para);
3644 gtk_text_layout_get_line_yrange (priv->layout, &first_para, &y, NULL);
3646 y += priv->first_para_pixels;
3648 if (y > priv->vadjustment->upper - priv->vadjustment->page_size)
3649 y = MAX (0, priv->vadjustment->upper - priv->vadjustment->page_size);
3651 if (y != priv->yoffset)
3652 gtk_adjustment_set_value (priv->vadjustment, y);
3654 gtk_adjustment_changed (priv->vadjustment);
3656 /* The GTK resize loop processes all the pending exposes right
3657 * after doing the resize stuff, so the idle sizer won't have a
3658 * chance to run. So we do the work here.
3660 gtk_text_view_flush_first_validate (text_view);
3662 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3665 if (size_changed && gtk_widget_get_realized (widget))
3666 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3670 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3673 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3674 text_view->priv->first_para_mark);
3678 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3681 GtkTextViewPrivate *priv;
3683 widget = GTK_WIDGET (text_view);
3684 priv = text_view->priv;
3686 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3688 if (SCREEN_HEIGHT (widget) > 0)
3690 GtkTextIter first_para;
3692 /* Be sure we've validated the stuff onscreen; if we
3693 * scrolled, these calls won't have any effect, because
3694 * they were called in the recursive validate_onscreen
3696 gtk_text_view_get_first_para_iter (text_view, &first_para);
3698 gtk_text_layout_validate_yrange (priv->layout,
3701 priv->first_para_pixels +
3702 SCREEN_HEIGHT (widget));
3705 priv->onscreen_validated = TRUE;
3707 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3709 /* This can have the odd side effect of triggering a scroll, which should
3710 * flip "onscreen_validated" back to FALSE, but should also get us
3711 * back into this function to turn it on again.
3713 gtk_text_view_update_adjustments (text_view);
3715 g_assert (priv->onscreen_validated);
3719 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3721 GtkTextViewPrivate *priv = text_view->priv;
3723 if (priv->first_validate_idle == 0)
3726 /* Do this first, which means that if an "invalidate"
3727 * occurs during any of this process, a new first_validate_callback
3728 * will be installed, and we'll start again.
3730 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3731 g_source_remove (priv->first_validate_idle);
3732 priv->first_validate_idle = 0;
3734 /* be sure we have up-to-date screen size set on the
3737 gtk_text_view_update_layout_width (text_view);
3739 /* Bail out if we invalidated stuff; scrolling right away will just
3740 * confuse the issue.
3742 if (priv->first_validate_idle != 0)
3744 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3748 /* scroll to any marks, if that's pending. This can jump us to
3749 * the validation codepath used for scrolling onscreen, if so we
3750 * bail out. It won't jump if already in that codepath since
3751 * value_changed is not recursive, so also validate if
3754 if (!gtk_text_view_flush_scroll (text_view) ||
3755 !priv->onscreen_validated)
3756 gtk_text_view_validate_onscreen (text_view);
3758 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3760 g_assert (priv->onscreen_validated);
3765 first_validate_callback (gpointer data)
3767 GtkTextView *text_view = data;
3769 /* Note that some of this code is duplicated at the end of size_allocate,
3770 * keep in sync with that.
3773 DV(g_print(G_STRLOC"\n"));
3775 gtk_text_view_flush_first_validate (text_view);
3781 incremental_validate_callback (gpointer data)
3783 GtkTextView *text_view = data;
3784 gboolean result = TRUE;
3786 DV(g_print(G_STRLOC"\n"));
3788 gtk_text_layout_validate (text_view->priv->layout, 2000);
3790 gtk_text_view_update_adjustments (text_view);
3792 if (gtk_text_layout_is_valid (text_view->priv->layout))
3794 text_view->priv->incremental_validate_idle = 0;
3802 gtk_text_view_invalidate (GtkTextView *text_view)
3804 GtkTextViewPrivate *priv = text_view->priv;
3806 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3807 priv->onscreen_validated));
3809 priv->onscreen_validated = FALSE;
3811 /* We'll invalidate when the layout is created */
3812 if (priv->layout == NULL)
3815 if (!priv->first_validate_idle)
3817 priv->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3818 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3819 priv->first_validate_idle));
3822 if (!priv->incremental_validate_idle)
3824 priv->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3825 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3826 priv->incremental_validate_idle));
3831 invalidated_handler (GtkTextLayout *layout,
3834 GtkTextView *text_view;
3836 text_view = GTK_TEXT_VIEW (data);
3838 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3839 gtk_text_view_invalidate (text_view);
3843 changed_handler (GtkTextLayout *layout,
3849 GtkTextView *text_view;
3850 GtkTextViewPrivate *priv;
3852 GdkRectangle visible_rect;
3853 GdkRectangle redraw_rect;
3855 text_view = GTK_TEXT_VIEW (data);
3856 priv = text_view->priv;
3857 widget = GTK_WIDGET (data);
3859 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3861 if (gtk_widget_get_realized (widget))
3863 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3865 redraw_rect.x = visible_rect.x;
3866 redraw_rect.width = visible_rect.width;
3867 redraw_rect.y = start_y;
3869 if (old_height == new_height)
3870 redraw_rect.height = old_height;
3871 else if (start_y + old_height > visible_rect.y)
3872 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3874 redraw_rect.height = 0;
3876 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3878 /* text_window_invalidate_rect() takes buffer coordinates */
3879 text_window_invalidate_rect (priv->text_window,
3882 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3886 redraw_rect.height));
3888 if (priv->left_window)
3889 text_window_invalidate_rect (priv->left_window,
3891 if (priv->right_window)
3892 text_window_invalidate_rect (priv->right_window,
3894 if (priv->top_window)
3895 text_window_invalidate_rect (priv->top_window,
3897 if (priv->bottom_window)
3898 text_window_invalidate_rect (priv->bottom_window,
3901 queue_update_im_spot_location (text_view);
3905 if (old_height != new_height)
3907 gboolean yoffset_changed = FALSE;
3909 int new_first_para_top;
3910 int old_first_para_top;
3913 /* If the bottom of the old area was above the top of the
3914 * screen, we need to scroll to keep the current top of the
3915 * screen in place. Remember that first_para_pixels is the
3916 * position of the top of the screen in coordinates relative to
3917 * the first paragraph onscreen.
3919 * In short we are adding the height delta of the portion of the
3920 * changed region above first_para_mark to priv->yoffset.
3922 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3923 priv->first_para_mark);
3925 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3927 old_first_para_top = priv->yoffset - priv->first_para_pixels;
3929 if (new_first_para_top != old_first_para_top)
3931 priv->yoffset += new_first_para_top - old_first_para_top;
3933 get_vadjustment (text_view)->value = priv->yoffset;
3934 yoffset_changed = TRUE;
3937 if (yoffset_changed)
3939 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3940 gtk_adjustment_value_changed (get_vadjustment (text_view));
3943 /* FIXME be smarter about which anchored widgets we update */
3945 tmp_list = priv->children;
3946 while (tmp_list != NULL)
3948 GtkTextViewChild *child = tmp_list->data;
3951 gtk_text_view_update_child_allocation (text_view, child);
3953 tmp_list = g_slist_next (tmp_list);
3958 GtkRequisition old_req;
3959 GtkRequisition new_req;
3961 old_req = widget->requisition;
3963 /* Use this instead of gtk_widget_size_request wrapper
3964 * to avoid the optimization which just returns widget->requisition
3965 * if a resize hasn't been queued.
3967 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3969 if (old_req.width != new_req.width ||
3970 old_req.height != new_req.height)
3972 gtk_widget_queue_resize_no_redraw (widget);
3978 gtk_text_view_realize (GtkWidget *widget)
3980 GtkTextView *text_view;
3981 GtkTextViewPrivate *priv;
3982 GdkWindowAttr attributes;
3983 gint attributes_mask;
3986 text_view = GTK_TEXT_VIEW (widget);
3987 priv = text_view->priv;
3989 gtk_widget_set_realized (widget, TRUE);
3991 attributes.window_type = GDK_WINDOW_CHILD;
3992 attributes.x = widget->allocation.x;
3993 attributes.y = widget->allocation.y;
3994 attributes.width = widget->allocation.width;
3995 attributes.height = widget->allocation.height;
3996 attributes.wclass = GDK_INPUT_OUTPUT;
3997 attributes.visual = gtk_widget_get_visual (widget);
3998 attributes.colormap = gtk_widget_get_colormap (widget);
3999 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
4001 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
4003 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
4004 &attributes, attributes_mask);
4005 gdk_window_set_user_data (widget->window, widget);
4007 /* must come before text_window_realize calls */
4008 widget->style = gtk_style_attach (widget->style, widget->window);
4010 gdk_window_set_background (widget->window,
4011 &widget->style->bg[gtk_widget_get_state (widget)]);
4013 text_window_realize (priv->text_window, widget);
4015 if (priv->left_window)
4016 text_window_realize (priv->left_window, widget);
4018 if (priv->top_window)
4019 text_window_realize (priv->top_window, widget);
4021 if (priv->right_window)
4022 text_window_realize (priv->right_window, widget);
4024 if (priv->bottom_window)
4025 text_window_realize (priv->bottom_window, widget);
4027 gtk_text_view_ensure_layout (text_view);
4031 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4032 GDK_SELECTION_PRIMARY);
4033 gtk_text_buffer_add_selection_clipboard (priv->buffer, clipboard);
4036 tmp_list = priv->children;
4037 while (tmp_list != NULL)
4039 GtkTextViewChild *vc = tmp_list->data;
4041 text_view_child_set_parent_window (text_view, vc);
4043 tmp_list = tmp_list->next;
4046 /* Ensure updating the spot location. */
4047 gtk_text_view_update_im_spot_location (text_view);
4051 gtk_text_view_unrealize (GtkWidget *widget)
4053 GtkTextView *text_view;
4054 GtkTextViewPrivate *priv;
4056 text_view = GTK_TEXT_VIEW (widget);
4057 priv = text_view->priv;
4061 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4062 GDK_SELECTION_PRIMARY);
4063 gtk_text_buffer_remove_selection_clipboard (priv->buffer, clipboard);
4066 gtk_text_view_remove_validate_idles (text_view);
4068 if (priv->popup_menu)
4070 gtk_widget_destroy (priv->popup_menu);
4071 priv->popup_menu = NULL;
4074 text_window_unrealize (priv->text_window);
4076 if (priv->left_window)
4077 text_window_unrealize (priv->left_window);
4079 if (priv->top_window)
4080 text_window_unrealize (priv->top_window);
4082 if (priv->right_window)
4083 text_window_unrealize (priv->right_window);
4085 if (priv->bottom_window)
4086 text_window_unrealize (priv->bottom_window);
4088 gtk_text_view_destroy_layout (text_view);
4090 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize (widget);
4094 gtk_text_view_set_background (GtkTextView *text_view)
4097 GtkTextViewPrivate *priv;
4099 widget = GTK_WIDGET (text_view);
4100 priv = text_view->priv;
4102 gdk_window_set_background (widget->window,
4103 &widget->style->bg[gtk_widget_get_state (widget)]);
4105 gdk_window_set_background (priv->text_window->bin_window,
4106 &widget->style->base[gtk_widget_get_state (widget)]);
4108 if (priv->left_window)
4109 gdk_window_set_background (priv->left_window->bin_window,
4110 &widget->style->bg[gtk_widget_get_state (widget)]);
4111 if (priv->right_window)
4112 gdk_window_set_background (priv->right_window->bin_window,
4113 &widget->style->bg[gtk_widget_get_state (widget)]);
4115 if (priv->top_window)
4116 gdk_window_set_background (priv->top_window->bin_window,
4117 &widget->style->bg[gtk_widget_get_state (widget)]);
4119 if (priv->bottom_window)
4120 gdk_window_set_background (priv->bottom_window->bin_window,
4121 &widget->style->bg[gtk_widget_get_state (widget)]);
4125 gtk_text_view_style_set (GtkWidget *widget,
4126 GtkStyle *previous_style)
4128 GtkTextView *text_view;
4129 GtkTextViewPrivate *priv;
4130 PangoContext *ltr_context, *rtl_context;
4132 text_view = GTK_TEXT_VIEW (widget);
4133 priv = text_view->priv;
4135 if (gtk_widget_get_realized (widget))
4137 gtk_text_view_set_background (text_view);
4140 if (priv->layout && previous_style)
4142 gtk_text_view_set_attributes_from_style (text_view,
4143 priv->layout->default_style,
4147 ltr_context = gtk_widget_create_pango_context (widget);
4148 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
4149 rtl_context = gtk_widget_create_pango_context (widget);
4150 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
4152 gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
4154 g_object_unref (ltr_context);
4155 g_object_unref (rtl_context);
4160 gtk_text_view_direction_changed (GtkWidget *widget,
4161 GtkTextDirection previous_direction)
4163 GtkTextViewPrivate *priv = GTK_TEXT_VIEW (widget)->priv;
4167 priv->layout->default_style->direction = gtk_widget_get_direction (widget);
4169 gtk_text_layout_default_style_changed (priv->layout);
4174 gtk_text_view_state_changed (GtkWidget *widget,
4175 GtkStateType previous_state)
4177 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4180 if (gtk_widget_get_realized (widget))
4182 gtk_text_view_set_background (text_view);
4184 if (gtk_widget_is_sensitive (widget))
4185 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4189 gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
4192 gdk_cursor_unref (cursor);
4194 text_view->priv->mouse_cursor_obscured = FALSE;
4197 if (!gtk_widget_is_sensitive (widget))
4199 /* Clear any selection */
4200 gtk_text_view_unselect (text_view);
4203 gtk_widget_queue_draw (widget);
4207 set_invisible_cursor (GdkWindow *window)
4209 GdkDisplay *display;
4212 display = gdk_drawable_get_display (window);
4213 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4215 gdk_window_set_cursor (window, cursor);
4217 gdk_cursor_unref (cursor);
4221 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
4223 if (text_view->priv->mouse_cursor_obscured)
4226 set_invisible_cursor (text_view->priv->text_window->bin_window);
4228 text_view->priv->mouse_cursor_obscured = TRUE;
4232 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
4234 if (text_view->priv->mouse_cursor_obscured)
4238 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
4240 gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
4241 gdk_cursor_unref (cursor);
4242 text_view->priv->mouse_cursor_obscured = FALSE;
4247 gtk_text_view_grab_notify (GtkWidget *widget,
4248 gboolean was_grabbed)
4250 GtkTextViewPrivate *priv;
4252 priv = GTK_TEXT_VIEW (widget)->priv;
4254 if (priv->grab_device &&
4255 gtk_widget_device_is_shadowed (widget, priv->grab_device))
4257 gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
4258 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
4268 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
4271 switch (event->type)
4273 case GDK_MOTION_NOTIFY:
4274 *x = event->motion.x;
4275 *y = event->motion.y;
4279 case GDK_BUTTON_PRESS:
4280 case GDK_2BUTTON_PRESS:
4281 case GDK_3BUTTON_PRESS:
4282 case GDK_BUTTON_RELEASE:
4283 *x = event->button.x;
4284 *y = event->button.y;
4289 case GDK_KEY_RELEASE:
4290 case GDK_ENTER_NOTIFY:
4291 case GDK_LEAVE_NOTIFY:
4292 case GDK_PROPERTY_NOTIFY:
4293 case GDK_SELECTION_CLEAR:
4294 case GDK_SELECTION_REQUEST:
4295 case GDK_SELECTION_NOTIFY:
4296 case GDK_PROXIMITY_IN:
4297 case GDK_PROXIMITY_OUT:
4298 case GDK_DRAG_ENTER:
4299 case GDK_DRAG_LEAVE:
4300 case GDK_DRAG_MOTION:
4301 case GDK_DRAG_STATUS:
4302 case GDK_DROP_START:
4303 case GDK_DROP_FINISHED:
4313 emit_event_on_tags (GtkWidget *widget,
4319 gboolean retval = FALSE;
4321 tags = gtk_text_iter_get_tags (iter);
4326 GtkTextTag *tag = tmp->data;
4328 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
4334 tmp = g_slist_next (tmp);
4337 g_slist_free (tags);
4343 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
4345 GtkTextView *text_view;
4346 GtkTextViewPrivate *priv;
4349 text_view = GTK_TEXT_VIEW (widget);
4350 priv = text_view->priv;
4352 if (priv->layout == NULL ||
4353 get_buffer (text_view) == NULL)
4356 if (event->any.window != priv->text_window->bin_window)
4359 if (get_event_coordinates (event, &x, &y))
4366 /* FIXME this is slow and we do it twice per event.
4367 * My favorite solution is to have GtkTextLayout cache
4368 * the last couple lookups.
4370 gtk_text_layout_get_iter_at_pixel (priv->layout,
4374 return emit_event_on_tags (widget, event, &iter);
4376 else if (event->type == GDK_KEY_PRESS ||
4377 event->type == GDK_KEY_RELEASE)
4381 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
4382 gtk_text_buffer_get_insert (get_buffer (text_view)));
4384 return emit_event_on_tags (widget, event, &iter);
4391 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
4393 GtkTextView *text_view;
4394 GtkTextViewPrivate *priv;
4395 GtkTextMark *insert;
4397 gboolean can_insert;
4398 gboolean retval = FALSE;
4399 gboolean obscure = FALSE;
4401 text_view = GTK_TEXT_VIEW (widget);
4402 priv = text_view->priv;
4404 if (priv->layout == NULL ||
4405 get_buffer (text_view) == NULL)
4408 /* Make sure input method knows where it is */
4409 flush_update_im_spot_location (text_view);
4411 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4412 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4413 can_insert = gtk_text_iter_can_insert (&iter, priv->editable);
4414 if (gtk_im_context_filter_keypress (priv->im_context, event))
4416 priv->need_im_reset = TRUE;
4418 gtk_text_view_reset_im_context (text_view);
4419 obscure = can_insert;
4423 else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4427 /* use overall editability not can_insert, more predictable for users */
4428 else if (priv->editable &&
4429 (event->keyval == GDK_Return ||
4430 event->keyval == GDK_ISO_Enter ||
4431 event->keyval == GDK_KP_Enter))
4433 /* this won't actually insert the newline if the cursor isn't
4436 gtk_text_view_reset_im_context (text_view);
4437 gtk_text_view_commit_text (text_view, "\n");
4442 /* Pass through Tab as literal tab, unless Control is held down */
4443 else if ((event->keyval == GDK_Tab ||
4444 event->keyval == GDK_KP_Tab ||
4445 event->keyval == GDK_ISO_Left_Tab) &&
4446 !(event->state & GDK_CONTROL_MASK))
4448 /* If the text widget isn't editable overall, or if the application
4449 * has turned off "accepts_tab", move the focus instead
4451 if (priv->accepts_tab && priv->editable)
4453 gtk_text_view_reset_im_context (text_view);
4454 gtk_text_view_commit_text (text_view, "\t");
4458 g_signal_emit_by_name (text_view, "move-focus",
4459 (event->state & GDK_SHIFT_MASK) ?
4460 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4468 gtk_text_view_obscure_mouse_cursor (text_view);
4470 gtk_text_view_reset_blink_time (text_view);
4471 gtk_text_view_pend_cursor_blink (text_view);
4477 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4479 GtkTextView *text_view;
4480 GtkTextViewPrivate *priv;
4481 GtkTextMark *insert;
4484 text_view = GTK_TEXT_VIEW (widget);
4485 priv = text_view->priv;
4487 if (priv->layout == NULL || get_buffer (text_view) == NULL)
4490 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4491 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4492 if (gtk_text_iter_can_insert (&iter, priv->editable) &&
4493 gtk_im_context_filter_keypress (priv->im_context, event))
4495 priv->need_im_reset = TRUE;
4499 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4503 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4505 GtkTextView *text_view;
4506 GtkTextViewPrivate *priv;
4508 text_view = GTK_TEXT_VIEW (widget);
4509 priv = text_view->priv;
4511 gtk_widget_grab_focus (widget);
4513 if (event->window != priv->text_window->bin_window)
4515 /* Remove selection if any. */
4516 gtk_text_view_unselect (text_view);
4520 gtk_text_view_reset_blink_time (text_view);
4524 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4525 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4526 else if (event->button == 3)
4527 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4530 if (event->type == GDK_BUTTON_PRESS)
4532 gtk_text_view_reset_im_context (text_view);
4534 if (event->button == 1)
4536 /* If we're in the selection, start a drag copy/move of the
4537 * selection; otherwise, start creating a new selection.
4540 GtkTextIter start, end;
4542 gtk_text_layout_get_iter_at_pixel (priv->layout,
4544 event->x + priv->xoffset,
4545 event->y + priv->yoffset);
4547 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4549 gtk_text_iter_in_range (&iter, &start, &end) &&
4550 !(event->state & GDK_SHIFT_MASK))
4552 priv->drag_start_x = event->x;
4553 priv->drag_start_y = event->y;
4554 priv->pending_place_cursor_button = event->button;
4558 gtk_text_view_start_selection_drag (text_view, &iter, event);
4563 else if (event->button == 2)
4567 /* We do not want to scroll back to the insert iter when we paste
4568 with the middle button */
4569 priv->scroll_after_paste = FALSE;
4571 gtk_text_layout_get_iter_at_pixel (priv->layout,
4573 event->x + priv->xoffset,
4574 event->y + priv->yoffset);
4576 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4577 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4582 else if (event->button == 3)
4584 gtk_text_view_do_popup (text_view, event);
4588 else if ((event->type == GDK_2BUTTON_PRESS ||
4589 event->type == GDK_3BUTTON_PRESS) &&
4594 gtk_text_view_end_selection_drag (text_view);
4596 gtk_text_layout_get_iter_at_pixel (priv->layout,
4598 event->x + priv->xoffset,
4599 event->y + priv->yoffset);
4601 gtk_text_view_start_selection_drag (text_view, &iter, event);
4609 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4611 GtkTextView *text_view;
4612 GtkTextViewPrivate *priv;
4614 text_view = GTK_TEXT_VIEW (widget);
4615 priv = text_view->priv;
4617 if (event->window != priv->text_window->bin_window)
4620 if (event->button == 1)
4622 if (priv->drag_start_x >= 0)
4624 priv->drag_start_x = -1;
4625 priv->drag_start_y = -1;
4628 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4630 else if (priv->pending_place_cursor_button == event->button)
4634 /* Unselect everything; we clicked inside selection, but
4635 * didn't move by the drag threshold, so just clear selection
4638 gtk_text_layout_get_iter_at_pixel (priv->layout,
4640 event->x + priv->xoffset,
4641 event->y + priv->yoffset);
4643 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4644 gtk_text_view_check_cursor_blink (text_view);
4646 priv->pending_place_cursor_button = 0;
4656 keymap_direction_changed (GdkKeymap *keymap,
4657 GtkTextView *text_view)
4659 gtk_text_view_check_keymap_direction (text_view);
4663 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4665 GtkTextView *text_view;
4666 GtkTextViewPrivate *priv;
4668 text_view = GTK_TEXT_VIEW (widget);
4669 priv = text_view->priv;
4671 gtk_widget_queue_draw (widget);
4673 DV(g_print (G_STRLOC": focus_in_event\n"));
4675 gtk_text_view_reset_blink_time (text_view);
4677 if (priv->cursor_visible && priv->layout)
4679 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
4680 gtk_text_view_check_cursor_blink (text_view);
4683 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4684 "direction-changed",
4685 G_CALLBACK (keymap_direction_changed), text_view);
4686 gtk_text_view_check_keymap_direction (text_view);
4690 priv->need_im_reset = TRUE;
4691 gtk_im_context_focus_in (priv->im_context);
4698 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4700 GtkTextView *text_view;
4701 GtkTextViewPrivate *priv;
4703 text_view = GTK_TEXT_VIEW (widget);
4704 priv = text_view->priv;
4706 gtk_text_view_end_selection_drag (text_view);
4708 gtk_widget_queue_draw (widget);
4710 DV(g_print (G_STRLOC": focus_out_event\n"));
4712 if (priv->cursor_visible && priv->layout)
4714 gtk_text_view_check_cursor_blink (text_view);
4715 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
4718 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4719 keymap_direction_changed,
4724 priv->need_im_reset = TRUE;
4725 gtk_im_context_focus_out (priv->im_context);
4732 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4734 GtkTextView *text_view;
4735 GtkTextViewPrivate *priv;
4737 text_view = GTK_TEXT_VIEW (widget);
4738 priv = text_view->priv;
4740 gtk_text_view_unobscure_mouse_cursor (text_view);
4742 if (event->window == priv->text_window->bin_window &&
4743 priv->drag_start_x >= 0)
4748 gdk_event_request_motions (event);
4750 if (gtk_drag_check_threshold (widget,
4756 gint buffer_x, buffer_y;
4758 gtk_text_view_window_to_buffer_coords (text_view,
4759 GTK_TEXT_WINDOW_TEXT,
4765 gtk_text_layout_get_iter_at_pixel (priv->layout,
4767 buffer_x, buffer_y);
4769 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4778 gtk_text_view_paint (GtkWidget *widget,
4780 GdkEventExpose *event)
4782 GtkTextView *text_view;
4783 GtkTextViewPrivate *priv;
4784 GList *child_exposes;
4787 text_view = GTK_TEXT_VIEW (widget);
4788 priv = text_view->priv;
4790 g_return_if_fail (priv->layout != NULL);
4791 g_return_if_fail (priv->xoffset >= 0);
4792 g_return_if_fail (priv->yoffset >= 0);
4794 while (priv->first_validate_idle != 0)
4796 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4797 priv->first_validate_idle));
4798 gtk_text_view_flush_first_validate (text_view);
4801 if (!priv->onscreen_validated)
4803 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.");
4804 g_assert_not_reached ();
4808 printf ("painting %d,%d %d x %d\n",
4810 area->width, area->height);
4813 child_exposes = NULL;
4814 gtk_text_layout_draw (priv->layout,
4816 priv->text_window->bin_window,
4821 area->width, area->height,
4824 tmp_list = child_exposes;
4825 while (tmp_list != NULL)
4827 GtkWidget *child = tmp_list->data;
4829 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4833 g_object_unref (child);
4835 tmp_list = tmp_list->next;
4838 g_list_free (child_exposes);
4842 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4846 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4847 GTK_TEXT_WINDOW_TEXT))
4849 DV(g_print (">Exposed ("G_STRLOC")\n"));
4850 gtk_text_view_paint (widget, &event->area, event);
4853 if (event->window == widget->window)
4854 gtk_text_view_draw_focus (widget);
4856 /* Propagate exposes to all unanchored children.
4857 * Anchored children are handled in gtk_text_view_paint().
4859 tmp_list = GTK_TEXT_VIEW (widget)->priv->children;
4860 while (tmp_list != NULL)
4862 GtkTextViewChild *vc = tmp_list->data;
4864 /* propagate_expose checks that event->window matches
4868 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4872 tmp_list = tmp_list->next;
4879 gtk_text_view_draw_focus (GtkWidget *widget)
4881 gboolean interior_focus;
4883 /* We clear the focus if we are in interior focus mode. */
4884 gtk_widget_style_get (widget,
4885 "interior-focus", &interior_focus,
4888 if (gtk_widget_is_drawable (widget))
4890 if (gtk_widget_has_focus (widget) && !interior_focus)
4892 gtk_paint_focus (widget->style, widget->window, gtk_widget_get_state (widget),
4893 NULL, widget, "textview",
4895 widget->allocation.width,
4896 widget->allocation.height);
4900 gdk_window_clear (widget->window);
4906 gtk_text_view_focus (GtkWidget *widget,
4907 GtkDirectionType direction)
4909 GtkContainer *container;
4912 container = GTK_CONTAINER (widget);
4914 if (!gtk_widget_is_focus (widget) &&
4915 container->focus_child == NULL)
4917 gtk_widget_grab_focus (widget);
4923 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4924 * children to get the focus
4926 gtk_widget_set_can_focus (widget, FALSE);
4927 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4928 gtk_widget_set_can_focus (widget, TRUE);
4935 gtk_text_view_move_focus (GtkWidget *widget,
4936 GtkDirectionType direction_type)
4938 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4940 if (GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus)
4941 GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus (text_view,
4950 gtk_text_view_add (GtkContainer *container,
4953 /* This is pretty random. */
4954 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4956 GTK_TEXT_WINDOW_WIDGET,
4961 gtk_text_view_remove (GtkContainer *container,
4964 GtkTextView *text_view;
4965 GtkTextViewPrivate *priv;
4966 GtkTextViewChild *vc;
4969 text_view = GTK_TEXT_VIEW (container);
4970 priv = text_view->priv;
4973 iter = priv->children;
4975 while (iter != NULL)
4979 if (vc->widget == child)
4982 iter = g_slist_next (iter);
4985 g_assert (iter != NULL); /* be sure we had the child in the list */
4987 priv->children = g_slist_remove (priv->children, vc);
4989 gtk_widget_unparent (vc->widget);
4991 text_view_child_free (vc);
4995 gtk_text_view_forall (GtkContainer *container,
4996 gboolean include_internals,
4997 GtkCallback callback,
4998 gpointer callback_data)
5001 GtkTextView *text_view;
5004 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
5005 g_return_if_fail (callback != NULL);
5007 text_view = GTK_TEXT_VIEW (container);
5009 copy = g_slist_copy (text_view->priv->children);
5012 while (iter != NULL)
5014 GtkTextViewChild *vc = iter->data;
5016 (* callback) (vc->widget, callback_data);
5018 iter = g_slist_next (iter);
5021 g_slist_free (copy);
5024 #define CURSOR_ON_MULTIPLIER 2
5025 #define CURSOR_OFF_MULTIPLIER 1
5026 #define CURSOR_PEND_MULTIPLIER 3
5027 #define CURSOR_DIVIDER 3
5030 cursor_blinks (GtkTextView *text_view)
5032 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5035 #ifdef DEBUG_VALIDATION_AND_SCROLLING
5038 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
5041 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
5046 if (text_view->priv->editable)
5048 GtkTextMark *insert;
5051 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
5052 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
5054 if (gtk_text_iter_editable (&iter, text_view->priv->editable))
5062 get_cursor_time (GtkTextView *text_view)
5064 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5067 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5073 get_cursor_blink_timeout (GtkTextView *text_view)
5075 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5078 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
5089 blink_cb (gpointer data)
5091 GtkTextView *text_view;
5092 GtkTextViewPrivate *priv;
5096 text_view = GTK_TEXT_VIEW (data);
5097 priv = text_view->priv;
5099 if (!gtk_widget_has_focus (GTK_WIDGET (text_view)))
5101 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
5102 "connect a handler to this signal, it must return\n"
5103 "FALSE so the text view gets the event as well");
5105 gtk_text_view_check_cursor_blink (text_view);
5110 g_assert (priv->layout);
5111 g_assert (priv->cursor_visible);
5113 visible = gtk_text_layout_get_cursor_visible (priv->layout);
5115 blink_timeout = get_cursor_blink_timeout (text_view);
5116 if (priv->blink_time > 1000 * blink_timeout &&
5117 blink_timeout < G_MAXINT/1000)
5119 /* we've blinked enough without the user doing anything, stop blinking */
5121 priv->blink_timeout = 0;
5124 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5129 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5132 priv->blink_time += get_cursor_time (text_view);
5135 /* Block changed_handler while changing the layout's cursor visibility
5136 * because it would expose the whole paragraph. Instead, we expose
5137 * the cursor's area(s) manually below.
5139 g_signal_handlers_block_by_func (priv->layout,
5142 gtk_text_layout_set_cursor_visible (priv->layout, !visible);
5143 g_signal_handlers_unblock_by_func (priv->layout,
5147 text_window_invalidate_cursors (priv->text_window);
5149 /* Remove ourselves */
5155 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
5157 if (text_view->priv->blink_timeout)
5159 g_source_remove (text_view->priv->blink_timeout);
5160 text_view->priv->blink_timeout = 0;
5165 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
5167 GtkTextViewPrivate *priv = text_view->priv;
5169 if (priv->layout != NULL &&
5170 priv->cursor_visible &&
5171 gtk_widget_has_focus (GTK_WIDGET (text_view)))
5173 if (cursor_blinks (text_view))
5175 if (priv->blink_timeout == 0)
5177 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5179 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5186 gtk_text_view_stop_cursor_blink (text_view);
5187 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5192 gtk_text_view_stop_cursor_blink (text_view);
5193 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
5198 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
5200 GtkTextViewPrivate *priv = text_view->priv;
5202 if (priv->layout != NULL &&
5203 priv->cursor_visible &&
5204 gtk_widget_has_focus (GTK_WIDGET (text_view)) &&
5205 cursor_blinks (text_view))
5207 gtk_text_view_stop_cursor_blink (text_view);
5208 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5210 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5217 gtk_text_view_reset_blink_time (GtkTextView *text_view)
5219 GtkTextViewPrivate *priv = text_view->priv;
5221 priv->blink_time = 0;
5226 * Key binding handlers
5230 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
5231 GtkTextIter *newplace,
5234 gboolean ret = TRUE;
5238 ret = gtk_text_layout_move_iter_to_previous_line (text_view->priv->layout, newplace);
5244 ret = gtk_text_layout_move_iter_to_next_line (text_view->priv->layout, newplace);
5252 move_cursor (GtkTextView *text_view,
5253 const GtkTextIter *new_location,
5254 gboolean extend_selection)
5256 if (extend_selection)
5257 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
5261 gtk_text_buffer_place_cursor (get_buffer (text_view),
5263 gtk_text_view_check_cursor_blink (text_view);
5267 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
5268 GtkMovementStep step,
5270 gboolean extend_selection)
5272 GtkTextViewPrivate *priv;
5274 GtkTextIter newplace;
5275 gboolean cancel_selection = FALSE;
5276 gint cursor_x_pos = 0;
5277 GtkDirectionType leave_direction = -1;
5279 priv = text_view->priv;
5281 if (!priv->cursor_visible)
5283 GtkScrollStep scroll_step;
5287 case GTK_MOVEMENT_VISUAL_POSITIONS:
5288 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
5290 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5291 case GTK_MOVEMENT_WORDS:
5292 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
5294 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5295 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
5297 case GTK_MOVEMENT_DISPLAY_LINES:
5298 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
5300 case GTK_MOVEMENT_PARAGRAPHS:
5301 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5302 scroll_step = GTK_SCROLL_STEPS;
5304 case GTK_MOVEMENT_PAGES:
5305 scroll_step = GTK_SCROLL_PAGES;
5307 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5308 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
5310 case GTK_MOVEMENT_BUFFER_ENDS:
5311 scroll_step = GTK_SCROLL_ENDS;
5314 scroll_step = GTK_SCROLL_PAGES;
5318 if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
5320 if (leave_direction != -1 &&
5321 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5324 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5331 gtk_text_view_reset_im_context (text_view);
5333 if (step == GTK_MOVEMENT_PAGES)
5335 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
5336 gtk_widget_error_bell (GTK_WIDGET (text_view));
5338 gtk_text_view_check_cursor_blink (text_view);
5339 gtk_text_view_pend_cursor_blink (text_view);
5342 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
5344 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
5345 gtk_widget_error_bell (GTK_WIDGET (text_view));
5347 gtk_text_view_check_cursor_blink (text_view);
5348 gtk_text_view_pend_cursor_blink (text_view);
5352 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5353 gtk_text_buffer_get_insert (get_buffer (text_view)));
5355 if (! extend_selection)
5357 GtkTextIter sel_bound;
5359 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &sel_bound,
5360 gtk_text_buffer_get_selection_bound (get_buffer (text_view)));
5362 /* if we move forward, assume the cursor is at the end of the selection;
5363 * if we move backward, assume the cursor is at the start
5366 gtk_text_iter_order (&sel_bound, &insert);
5368 gtk_text_iter_order (&insert, &sel_bound);
5370 /* if we actually have a selection, just move *to* the beginning/end
5371 * of the selection and not *from* there on LOGICAL_POSITIONS
5372 * and VISUAL_POSITIONS movement
5374 if (! gtk_text_iter_equal (&sel_bound, &insert))
5375 cancel_selection = TRUE;
5380 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5381 gtk_text_view_get_virtual_cursor_pos (text_view, &insert, &cursor_x_pos, NULL);
5385 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5386 if (! cancel_selection)
5387 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5390 case GTK_MOVEMENT_VISUAL_POSITIONS:
5391 if (! cancel_selection)
5392 gtk_text_layout_move_iter_visually (priv->layout,
5396 case GTK_MOVEMENT_WORDS:
5398 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5401 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5402 gtk_text_iter_forward_to_line_end (&newplace);
5406 case GTK_MOVEMENT_DISPLAY_LINES:
5409 leave_direction = GTK_DIR_UP;
5411 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5412 gtk_text_layout_move_iter_to_x (priv->layout, &newplace, cursor_x_pos);
5414 gtk_text_iter_set_line_offset (&newplace, 0);
5418 leave_direction = GTK_DIR_DOWN;
5420 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5421 gtk_text_layout_move_iter_to_x (priv->layout, &newplace, cursor_x_pos);
5423 gtk_text_iter_forward_to_line_end (&newplace);
5427 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5429 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5430 else if (count < -1)
5431 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5434 gtk_text_layout_move_iter_to_line_end (priv->layout, &newplace, count);
5437 case GTK_MOVEMENT_PARAGRAPHS:
5440 if (!gtk_text_iter_ends_line (&newplace))
5442 gtk_text_iter_forward_to_line_end (&newplace);
5445 gtk_text_iter_forward_visible_lines (&newplace, count);
5446 gtk_text_iter_forward_to_line_end (&newplace);
5450 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5451 gtk_text_iter_set_line_offset (&newplace, 0);
5452 gtk_text_iter_forward_visible_lines (&newplace, count);
5453 gtk_text_iter_set_line_offset (&newplace, 0);
5457 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5460 if (!gtk_text_iter_ends_line (&newplace))
5461 gtk_text_iter_forward_to_line_end (&newplace);
5465 gtk_text_iter_set_line_offset (&newplace, 0);
5469 case GTK_MOVEMENT_BUFFER_ENDS:
5471 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5473 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5480 /* call move_cursor() even if the cursor hasn't moved, since it
5481 cancels the selection
5483 move_cursor (text_view, &newplace, extend_selection);
5485 if (!gtk_text_iter_equal (&insert, &newplace))
5487 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5488 gtk_text_view_scroll_mark_onscreen (text_view,
5489 gtk_text_buffer_get_insert (get_buffer (text_view)));
5491 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5492 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5494 else if (leave_direction != -1)
5496 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5499 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5502 else if (! cancel_selection)
5504 gtk_widget_error_bell (GTK_WIDGET (text_view));
5507 gtk_text_view_check_cursor_blink (text_view);
5508 gtk_text_view_pend_cursor_blink (text_view);
5512 gtk_text_view_move_cursor (GtkTextView *text_view,
5513 GtkMovementStep step,
5515 gboolean extend_selection)
5517 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5521 gtk_text_view_page_horizontally (GtkTextView *text_view,
5523 gboolean extend_selection)
5525 gtk_text_view_move_cursor_internal (text_view, GTK_MOVEMENT_HORIZONTAL_PAGES,
5526 count, extend_selection);
5531 gtk_text_view_move_viewport (GtkTextView *text_view,
5535 GtkAdjustment *adjustment;
5540 case GTK_SCROLL_STEPS:
5541 case GTK_SCROLL_PAGES:
5542 case GTK_SCROLL_ENDS:
5543 adjustment = get_vadjustment (text_view);
5545 case GTK_SCROLL_HORIZONTAL_STEPS:
5546 case GTK_SCROLL_HORIZONTAL_PAGES:
5547 case GTK_SCROLL_HORIZONTAL_ENDS:
5548 adjustment = get_hadjustment (text_view);
5551 adjustment = get_vadjustment (text_view);
5557 case GTK_SCROLL_STEPS:
5558 case GTK_SCROLL_HORIZONTAL_STEPS:
5559 increment = adjustment->step_increment;
5561 case GTK_SCROLL_PAGES:
5562 case GTK_SCROLL_HORIZONTAL_PAGES:
5563 increment = adjustment->page_increment;
5565 case GTK_SCROLL_ENDS:
5566 case GTK_SCROLL_HORIZONTAL_ENDS:
5567 increment = adjustment->upper - adjustment->lower;
5574 return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
5578 gtk_text_view_set_anchor (GtkTextView *text_view)
5582 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5583 gtk_text_buffer_get_insert (get_buffer (text_view)));
5585 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5589 gtk_text_view_scroll_pages (GtkTextView *text_view,
5591 gboolean extend_selection)
5593 GtkTextViewPrivate *priv;
5595 gint cursor_x_pos, cursor_y_pos;
5596 GtkTextMark *insert_mark;
5597 GtkTextIter old_insert;
5598 GtkTextIter new_insert;
5604 priv = text_view->priv;
5606 g_return_val_if_fail (priv->vadjustment != NULL, FALSE);
5608 adj = priv->vadjustment;
5610 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5612 /* Make sure we start from the current cursor position, even
5613 * if it was offscreen, but don't queue more scrolls if we're
5616 if (priv->pending_scroll)
5617 cancel_pending_scroll (text_view);
5619 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5621 /* Validate the region that will be brought into view by the cursor motion
5623 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5624 &old_insert, insert_mark);
5628 gtk_text_view_get_first_para_iter (text_view, &anchor);
5629 y0 = adj->page_size;
5630 y1 = adj->page_size + count * adj->page_increment;
5634 gtk_text_view_get_first_para_iter (text_view, &anchor);
5635 y0 = count * adj->page_increment + adj->page_size;
5639 gtk_text_layout_validate_yrange (priv->layout, &anchor, y0, y1);
5640 /* FIXME do we need to update the adjustment ranges here? */
5642 new_insert = old_insert;
5644 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5646 /* already at top, just be sure we are at offset 0 */
5647 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5648 move_cursor (text_view, &new_insert, extend_selection);
5650 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5652 /* already at bottom, just be sure we are at the end */
5653 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5654 move_cursor (text_view, &new_insert, extend_selection);
5658 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5660 oldval = adj->value;
5661 newval = adj->value;
5663 newval += count * adj->page_increment;
5665 set_adjustment_clamped (adj, newval);
5666 cursor_y_pos += adj->value - oldval;
5668 gtk_text_layout_get_iter_at_pixel (priv->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5669 clamp_iter_onscreen (text_view, &new_insert);
5670 move_cursor (text_view, &new_insert, extend_selection);
5672 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5675 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5676 * only guarantees 1 pixel onscreen.
5678 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5679 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5681 return !gtk_text_iter_equal (&old_insert, &new_insert);
5685 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5687 gboolean extend_selection)
5689 GtkTextViewPrivate *priv;
5691 gint cursor_x_pos, cursor_y_pos;
5692 GtkTextMark *insert_mark;
5693 GtkTextIter old_insert;
5694 GtkTextIter new_insert;
5699 priv = text_view->priv;
5701 g_return_val_if_fail (priv->hadjustment != NULL, FALSE);
5703 adj = priv->hadjustment;
5705 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5707 /* Make sure we start from the current cursor position, even
5708 * if it was offscreen, but don't queue more scrolls if we're
5711 if (priv->pending_scroll)
5712 cancel_pending_scroll (text_view);
5714 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5716 /* Validate the line that we're moving within.
5718 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5719 &old_insert, insert_mark);
5721 gtk_text_layout_get_line_yrange (priv->layout, &old_insert, &y, &height);
5722 gtk_text_layout_validate_yrange (priv->layout, &old_insert, y, y + height);
5723 /* FIXME do we need to update the adjustment ranges here? */
5725 new_insert = old_insert;
5727 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5729 /* already at far left, just be sure we are at offset 0 */
5730 gtk_text_iter_set_line_offset (&new_insert, 0);
5731 move_cursor (text_view, &new_insert, extend_selection);
5733 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5735 /* already at far right, just be sure we are at the end */
5736 if (!gtk_text_iter_ends_line (&new_insert))
5737 gtk_text_iter_forward_to_line_end (&new_insert);
5738 move_cursor (text_view, &new_insert, extend_selection);
5742 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5744 oldval = adj->value;
5745 newval = adj->value;
5747 newval += count * adj->page_increment;
5749 set_adjustment_clamped (adj, newval);
5750 cursor_x_pos += adj->value - oldval;
5752 gtk_text_layout_get_iter_at_pixel (priv->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5753 clamp_iter_onscreen (text_view, &new_insert);
5754 move_cursor (text_view, &new_insert, extend_selection);
5756 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5759 /* FIXME for lines shorter than the overall widget width, this results in a
5760 * "bounce" effect as we scroll to the right of the widget, then scroll
5761 * back to get the end of the line onscreen.
5762 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5765 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5766 * only guarantees 1 pixel onscreen.
5768 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5769 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5771 return !gtk_text_iter_equal (&old_insert, &new_insert);
5775 whitespace (gunichar ch, gpointer user_data)
5777 return (ch == ' ' || ch == '\t');
5781 not_whitespace (gunichar ch, gpointer user_data)
5783 return !whitespace (ch, user_data);
5787 find_whitepace_region (const GtkTextIter *center,
5788 GtkTextIter *start, GtkTextIter *end)
5793 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5794 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5795 if (whitespace (gtk_text_iter_get_char (end), NULL))
5796 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5798 return !gtk_text_iter_equal (start, end);
5802 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5805 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5806 text_view->priv->editable))
5808 gtk_widget_error_bell (GTK_WIDGET (text_view));
5813 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5817 GtkTextViewPrivate *priv;
5821 gboolean leave_one = FALSE;
5823 priv = text_view->priv;
5825 gtk_text_view_reset_im_context (text_view);
5827 if (type == GTK_DELETE_CHARS)
5829 /* Char delete deletes the selection, if one exists */
5830 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5835 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5836 gtk_text_buffer_get_insert (get_buffer (text_view)));
5843 case GTK_DELETE_CHARS:
5844 gtk_text_iter_forward_cursor_positions (&end, count);
5847 case GTK_DELETE_WORD_ENDS:
5849 gtk_text_iter_forward_word_ends (&end, count);
5851 gtk_text_iter_backward_word_starts (&start, 0 - count);
5854 case GTK_DELETE_WORDS:
5857 case GTK_DELETE_DISPLAY_LINE_ENDS:
5860 case GTK_DELETE_DISPLAY_LINES:
5863 case GTK_DELETE_PARAGRAPH_ENDS:
5866 /* If we're already at a newline, we need to
5867 * simply delete that newline, instead of
5868 * moving to the next one.
5870 if (gtk_text_iter_ends_line (&end))
5872 gtk_text_iter_forward_line (&end);
5878 if (!gtk_text_iter_forward_to_line_end (&end))
5886 if (gtk_text_iter_starts_line (&start))
5888 gtk_text_iter_backward_line (&start);
5889 if (!gtk_text_iter_ends_line (&end))
5890 gtk_text_iter_forward_to_line_end (&start);
5894 gtk_text_iter_set_line_offset (&start, 0);
5898 gtk_text_iter_backward_lines (&start, -count);
5902 case GTK_DELETE_PARAGRAPHS:
5905 gtk_text_iter_set_line_offset (&start, 0);
5906 gtk_text_iter_forward_to_line_end (&end);
5908 /* Do the lines beyond the first. */
5911 gtk_text_iter_forward_to_line_end (&end);
5917 /* FIXME negative count? */
5921 case GTK_DELETE_WHITESPACE:
5923 find_whitepace_region (&insert, &start, &end);
5931 if (!gtk_text_iter_equal (&start, &end))
5933 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5935 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5939 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5945 gtk_widget_error_bell (GTK_WIDGET (text_view));
5948 gtk_text_buffer_end_user_action (get_buffer (text_view));
5949 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5951 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5952 gtk_text_view_scroll_mark_onscreen (text_view,
5953 gtk_text_buffer_get_insert (get_buffer (text_view)));
5957 gtk_widget_error_bell (GTK_WIDGET (text_view));
5962 gtk_text_view_backspace (GtkTextView *text_view)
5964 GtkTextViewPrivate *priv;
5967 priv = text_view->priv;
5969 gtk_text_view_reset_im_context (text_view);
5971 /* Backspace deletes the selection, if one exists */
5972 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5976 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5978 gtk_text_buffer_get_insert (get_buffer (text_view)));
5980 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5981 TRUE, priv->editable))
5983 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5984 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5985 gtk_text_view_scroll_mark_onscreen (text_view,
5986 gtk_text_buffer_get_insert (get_buffer (text_view)));
5990 gtk_widget_error_bell (GTK_WIDGET (text_view));
5995 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5997 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5998 GDK_SELECTION_CLIPBOARD);
6000 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
6002 text_view->priv->editable);
6003 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6004 gtk_text_view_scroll_mark_onscreen (text_view,
6005 gtk_text_buffer_get_insert (get_buffer (text_view)));
6009 gtk_text_view_copy_clipboard (GtkTextView *text_view)
6011 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6012 GDK_SELECTION_CLIPBOARD);
6014 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
6017 /* on copy do not scroll, we are already onscreen */
6021 gtk_text_view_paste_clipboard (GtkTextView *text_view)
6023 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6024 GDK_SELECTION_CLIPBOARD);
6026 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
6029 text_view->priv->editable);
6033 gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
6034 GtkClipboard *clipboard,
6037 GtkTextView *text_view = data;
6038 GtkTextViewPrivate *priv;
6040 priv = text_view->priv;
6042 if (priv->scroll_after_paste)
6044 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6045 gtk_text_view_scroll_mark_onscreen (text_view, gtk_text_buffer_get_insert (buffer));
6048 priv->scroll_after_paste = TRUE;
6052 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
6054 GtkTextViewPrivate *priv = text_view->priv;
6056 if (priv->text_window)
6057 text_window_invalidate_cursors (priv->text_window);
6059 priv->overwrite_mode = !priv->overwrite_mode;
6062 gtk_text_layout_set_overwrite_mode (priv->layout,
6063 priv->overwrite_mode && priv->editable);
6065 if (priv->text_window)
6066 text_window_invalidate_cursors (priv->text_window);
6068 gtk_text_view_pend_cursor_blink (text_view);
6070 g_object_notify (G_OBJECT (text_view), "overwrite");
6074 * gtk_text_view_get_overwrite:
6075 * @text_view: a #GtkTextView
6077 * Returns whether the #GtkTextView is in overwrite mode or not.
6079 * Return value: whether @text_view is in overwrite mode or not.
6084 gtk_text_view_get_overwrite (GtkTextView *text_view)
6086 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6088 return text_view->priv->overwrite_mode;
6092 * gtk_text_view_set_overwrite:
6093 * @text_view: a #GtkTextView
6094 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
6096 * Changes the #GtkTextView overwrite mode.
6101 gtk_text_view_set_overwrite (GtkTextView *text_view,
6104 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6105 overwrite = overwrite != FALSE;
6107 if (text_view->priv->overwrite_mode != overwrite)
6108 gtk_text_view_toggle_overwrite (text_view);
6112 * gtk_text_view_set_accepts_tab:
6113 * @text_view: A #GtkTextView
6114 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
6115 * character, %FALSE, if pressing the Tab key should move the
6118 * Sets the behavior of the text widget when the Tab key is pressed.
6119 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
6120 * is %FALSE the keyboard focus is moved to the next widget in the focus
6126 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
6127 gboolean accepts_tab)
6129 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6131 accepts_tab = accepts_tab != FALSE;
6133 if (text_view->priv->accepts_tab != accepts_tab)
6135 text_view->priv->accepts_tab = accepts_tab;
6137 g_object_notify (G_OBJECT (text_view), "accepts-tab");
6142 * gtk_text_view_get_accepts_tab:
6143 * @text_view: A #GtkTextView
6145 * Returns whether pressing the Tab key inserts a tab characters.
6146 * gtk_text_view_set_accepts_tab().
6148 * Return value: %TRUE if pressing the Tab key inserts a tab character,
6149 * %FALSE if pressing the Tab key moves the keyboard focus.
6154 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
6156 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6158 return text_view->priv->accepts_tab;
6162 gtk_text_view_compat_move_focus (GtkTextView *text_view,
6163 GtkDirectionType direction_type)
6165 GSignalInvocationHint *hint = g_signal_get_invocation_hint (text_view);
6167 /* as of GTK+ 2.12, the "move-focus" signal has been moved to GtkWidget,
6168 * the evil code below makes sure that both emitting the signal and
6169 * calling the virtual function directly continue to work as expetcted
6172 if (hint->signal_id == g_signal_lookup ("move-focus", GTK_TYPE_WIDGET))
6174 /* if this is a signal emission, chain up */
6178 g_signal_chain_from_overridden_handler (text_view,
6179 direction_type, &retval);
6183 /* otherwise emit the signal, since somebody called the virtual
6187 g_signal_emit_by_name (text_view, "move-focus", direction_type);
6196 gtk_text_view_unselect (GtkTextView *text_view)
6200 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6201 gtk_text_buffer_get_insert (get_buffer (text_view)));
6203 gtk_text_buffer_move_mark (get_buffer (text_view),
6204 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
6209 get_iter_at_pointer (GtkTextView *text_view,
6215 GtkTextViewPrivate *priv;
6216 gint xcoord, ycoord;
6217 GdkModifierType state;
6219 priv = text_view->priv;
6221 gdk_window_get_device_position (priv->text_window->bin_window,
6222 device, &xcoord, &ycoord, &state);
6224 gtk_text_layout_get_iter_at_pixel (priv->layout,
6226 xcoord + priv->xoffset,
6227 ycoord + priv->yoffset);
6236 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
6237 const gchar *mark_name,
6240 GtkTextIter newplace;
6243 get_iter_at_pointer (text_view, device, &newplace, NULL, NULL);
6245 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
6247 /* This may invalidate the layout */
6248 DV(g_print (G_STRLOC": move mark\n"));
6250 gtk_text_buffer_move_mark (get_buffer (text_view),
6254 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6255 gtk_text_view_scroll_mark_onscreen (text_view, mark);
6257 DV (g_print ("first validate idle leaving %s is %d\n",
6258 G_STRLOC, text_view->priv->first_validate_idle));
6262 selection_scan_timeout (gpointer data)
6264 GtkTextView *text_view;
6266 text_view = GTK_TEXT_VIEW (data);
6268 gtk_text_view_scroll_mark_onscreen (text_view,
6269 gtk_text_buffer_get_insert (get_buffer (text_view)));
6271 return TRUE; /* remain installed. */
6274 #define UPPER_OFFSET_ANCHOR 0.8
6275 #define LOWER_OFFSET_ANCHOR 0.2
6278 check_scroll (gdouble offset, GtkAdjustment *adj)
6280 if ((offset > UPPER_OFFSET_ANCHOR &&
6281 adj->value + adj->page_size < adj->upper) ||
6282 (offset < LOWER_OFFSET_ANCHOR &&
6283 adj->value > adj->lower))
6290 drag_scan_timeout (gpointer data)
6292 GtkTextView *text_view;
6293 GtkTextViewPrivate *priv;
6294 GtkTextIter newplace;
6295 gint x, y, width, height;
6296 gdouble pointer_xoffset, pointer_yoffset;
6298 text_view = GTK_TEXT_VIEW (data);
6299 priv = text_view->priv;
6301 get_iter_at_pointer (text_view, priv->dnd_device, &newplace, &x, &y);
6302 gdk_drawable_get_size (priv->text_window->bin_window, &width, &height);
6304 gtk_text_buffer_move_mark (get_buffer (text_view),
6308 pointer_xoffset = (gdouble) x / width;
6309 pointer_yoffset = (gdouble) y / height;
6311 if (check_scroll (pointer_xoffset, priv->hadjustment) ||
6312 check_scroll (pointer_yoffset, priv->vadjustment))
6314 /* do not make offsets surpass lower nor upper anchors, this makes
6315 * scrolling speed relative to the distance of the pointer to the
6316 * anchors when it moves beyond them.
6318 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6319 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6321 gtk_text_view_scroll_to_mark (text_view,
6323 0., TRUE, pointer_xoffset, pointer_yoffset);
6334 } SelectionGranularity;
6337 * Move @start and @end to the boundaries of the selection unit (indicated by
6338 * @granularity) which contained @start initially.
6339 * If the selction unit is SELECT_WORDS and @start is not contained in a word
6340 * the selection is extended to all the white spaces between the end of the
6341 * word preceding @start and the start of the one following.
6344 extend_selection (GtkTextView *text_view,
6345 SelectionGranularity granularity,
6351 if (granularity == SELECT_WORDS)
6353 if (gtk_text_iter_inside_word (start))
6355 if (!gtk_text_iter_starts_word (start))
6356 gtk_text_iter_backward_visible_word_start (start);
6358 if (!gtk_text_iter_ends_word (end))
6360 if (!gtk_text_iter_forward_visible_word_end (end))
6361 gtk_text_iter_forward_to_end (end);
6369 if (gtk_text_iter_backward_visible_word_start (&tmp))
6370 gtk_text_iter_forward_visible_word_end (&tmp);
6372 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
6375 gtk_text_iter_set_line_offset (start, 0);
6378 if (!gtk_text_iter_forward_visible_word_end (&tmp))
6379 gtk_text_iter_forward_to_end (&tmp);
6381 if (gtk_text_iter_ends_word (&tmp))
6382 gtk_text_iter_backward_visible_word_start (&tmp);
6384 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
6387 gtk_text_iter_forward_to_line_end (end);
6390 else if (granularity == SELECT_LINES)
6392 if (gtk_text_view_starts_display_line (text_view, start))
6394 /* If on a display line boundary, we assume the user
6395 * clicked off the end of a line and we therefore select
6396 * the line before the boundary.
6398 gtk_text_view_backward_display_line_start (text_view, start);
6402 /* start isn't on the start of a line, so we move it to the
6403 * start, and move end to the end unless it's already there.
6405 gtk_text_view_backward_display_line_start (text_view, start);
6407 if (!gtk_text_view_starts_display_line (text_view, end))
6408 gtk_text_view_forward_display_line_end (text_view, end);
6416 SelectionGranularity granularity;
6417 GtkTextMark *orig_start;
6418 GtkTextMark *orig_end;
6422 selection_data_free (SelectionData *data)
6424 if (data->orig_start != NULL)
6425 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6427 if (data->orig_end != NULL)
6428 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6434 selection_motion_event_handler (GtkTextView *text_view,
6435 GdkEventMotion *event,
6436 SelectionData *data)
6438 GtkTextViewPrivate *priv;
6440 priv = text_view->priv;
6441 gdk_event_request_motions (event);
6443 if (priv->grab_device != event->device)
6446 if (data->granularity == SELECT_CHARACTERS)
6448 move_mark_to_pointer_and_scroll (text_view, "insert", event->device);
6452 GtkTextIter cursor, start, end;
6453 GtkTextIter orig_start, orig_end;
6454 GtkTextBuffer *buffer;
6456 buffer = get_buffer (text_view);
6458 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6459 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6461 get_iter_at_pointer (text_view, event->device, &cursor, NULL, NULL);
6464 extend_selection (text_view, data->granularity, &start, &end);
6466 /* either the selection extends to the front, or end (or not) */
6467 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6468 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6470 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6472 gtk_text_view_scroll_mark_onscreen (text_view,
6473 gtk_text_buffer_get_insert (buffer));
6476 /* If we had to scroll offscreen, insert a timeout to do so
6477 * again. Note that in the timeout, even if the mouse doesn't
6478 * move, due to this scroll xoffset/yoffset will have changed
6479 * and we'll need to scroll again.
6481 if (text_view->priv->scroll_timeout != 0) /* reset on every motion event */
6482 g_source_remove (text_view->priv->scroll_timeout);
6484 text_view->priv->scroll_timeout =
6485 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6491 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6492 const GtkTextIter *iter,
6493 GdkEventButton *button)
6495 GtkTextViewPrivate *priv;
6496 GtkTextIter cursor, ins, bound;
6497 GtkTextIter orig_start, orig_end;
6498 GtkTextBuffer *buffer;
6499 SelectionData *data;
6501 if (text_view->priv->selection_drag_handler != 0)
6504 priv = text_view->priv;
6505 data = g_new0 (SelectionData, 1);
6507 if (button->type == GDK_2BUTTON_PRESS)
6508 data->granularity = SELECT_WORDS;
6509 else if (button->type == GDK_3BUTTON_PRESS)
6510 data->granularity = SELECT_LINES;
6512 data->granularity = SELECT_CHARACTERS;
6514 priv->grab_device = button->device;
6515 gtk_device_grab_add (GTK_WIDGET (text_view),
6519 buffer = get_buffer (text_view);
6524 extend_selection (text_view, data->granularity, &ins, &bound);
6528 if (button->state & GDK_SHIFT_MASK)
6530 /* Extend selection */
6531 GtkTextIter old_ins, old_bound;
6532 GtkTextIter old_start, old_end;
6534 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6535 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6536 old_start = old_ins;
6537 old_end = old_bound;
6538 gtk_text_iter_order (&old_start, &old_end);
6540 /* move the front cursor, if the mouse is in front of the selection. Should the
6541 * cursor however be inside the selection (this happens on tripple click) then we
6542 * move the side which was last moved (current insert mark) */
6543 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6544 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6545 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6548 orig_start = old_end;
6560 gtk_text_buffer_select_range (buffer, &ins, &bound);
6562 gtk_text_iter_order (&orig_start, &orig_end);
6563 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6565 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6567 gtk_text_view_check_cursor_blink (text_view);
6569 text_view->priv->selection_drag_handler = g_signal_connect_data (text_view,
6570 "motion-notify-event",
6571 G_CALLBACK (selection_motion_event_handler),
6573 (GClosureNotify) selection_data_free, 0);
6576 /* returns whether we were really dragging */
6578 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6580 GtkTextViewPrivate *priv;
6582 priv = text_view->priv;
6584 if (!priv->grab_device)
6587 if (priv->selection_drag_handler == 0)
6590 g_signal_handler_disconnect (text_view, priv->selection_drag_handler);
6591 priv->selection_drag_handler = 0;
6593 if (priv->scroll_timeout != 0)
6595 g_source_remove (priv->scroll_timeout);
6596 priv->scroll_timeout = 0;
6599 gtk_device_grab_remove (GTK_WIDGET (text_view),
6601 priv->grab_device = NULL;
6611 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6612 GtkTextAttributes *values,
6615 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
6616 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
6619 pango_font_description_free (values->font);
6621 values->font = pango_font_description_copy (style->font_desc);
6625 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6627 GtkTextViewPrivate *priv = text_view->priv;
6631 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6632 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6633 GtkTextDirection new_cursor_dir;
6634 GtkTextDirection new_keyboard_dir;
6635 gboolean split_cursor;
6637 g_object_get (settings,
6638 "gtk-split-cursor", &split_cursor,
6641 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6642 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6644 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6647 new_cursor_dir = GTK_TEXT_DIR_NONE;
6649 new_cursor_dir = new_keyboard_dir;
6651 gtk_text_layout_set_cursor_direction (priv->layout, new_cursor_dir);
6652 gtk_text_layout_set_keyboard_direction (priv->layout, new_keyboard_dir);
6657 gtk_text_view_ensure_layout (GtkTextView *text_view)
6660 GtkTextViewPrivate *priv;
6662 widget = GTK_WIDGET (text_view);
6663 priv = text_view->priv;
6665 if (priv->layout == NULL)
6667 GtkTextAttributes *style;
6668 PangoContext *ltr_context, *rtl_context;
6671 DV(g_print(G_STRLOC"\n"));
6673 priv->layout = gtk_text_layout_new ();
6675 g_signal_connect (priv->layout,
6677 G_CALLBACK (invalidated_handler),
6680 g_signal_connect (priv->layout,
6682 G_CALLBACK (changed_handler),
6685 g_signal_connect (priv->layout,
6687 G_CALLBACK (gtk_text_view_child_allocated),
6690 if (get_buffer (text_view))
6691 gtk_text_layout_set_buffer (priv->layout, get_buffer (text_view));
6693 if ((gtk_widget_has_focus (widget) && priv->cursor_visible))
6694 gtk_text_view_pend_cursor_blink (text_view);
6696 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
6698 gtk_text_layout_set_overwrite_mode (priv->layout,
6699 priv->overwrite_mode && priv->editable);
6701 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6702 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6703 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6704 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6706 gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
6708 g_object_unref (ltr_context);
6709 g_object_unref (rtl_context);
6711 gtk_text_view_check_keymap_direction (text_view);
6713 style = gtk_text_attributes_new ();
6715 gtk_widget_ensure_style (widget);
6716 gtk_text_view_set_attributes_from_style (text_view,
6717 style, widget->style);
6719 style->pixels_above_lines = priv->pixels_above_lines;
6720 style->pixels_below_lines = priv->pixels_below_lines;
6721 style->pixels_inside_wrap = priv->pixels_inside_wrap;
6722 style->left_margin = priv->left_margin;
6723 style->right_margin = priv->right_margin;
6724 style->indent = priv->indent;
6725 style->tabs = priv->tabs ? pango_tab_array_copy (priv->tabs) : NULL;
6727 style->wrap_mode = priv->wrap_mode;
6728 style->justification = priv->justify;
6729 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6731 gtk_text_layout_set_default_style (priv->layout, style);
6733 gtk_text_attributes_unref (style);
6735 /* Set layout for all anchored children */
6737 tmp_list = priv->children;
6738 while (tmp_list != NULL)
6740 GtkTextViewChild *vc = tmp_list->data;
6744 gtk_text_anchored_child_set_layout (vc->widget,
6746 /* vc may now be invalid! */
6749 tmp_list = g_slist_next (tmp_list);
6752 gtk_text_view_invalidate (text_view);
6757 * gtk_text_view_get_default_attributes:
6758 * @text_view: a #GtkTextView
6760 * Obtains a copy of the default text attributes. These are the
6761 * attributes used for text unless a tag overrides them.
6762 * You'd typically pass the default attributes in to
6763 * gtk_text_iter_get_attributes() in order to get the
6764 * attributes in effect at a given text position.
6766 * The return value is a copy owned by the caller of this function,
6767 * and should be freed.
6769 * Return value: a new #GtkTextAttributes
6772 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6774 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6776 gtk_text_view_ensure_layout (text_view);
6778 return gtk_text_attributes_copy (text_view->priv->layout->default_style);
6782 gtk_text_view_destroy_layout (GtkTextView *text_view)
6784 GtkTextViewPrivate *priv = text_view->priv;
6790 gtk_text_view_remove_validate_idles (text_view);
6792 g_signal_handlers_disconnect_by_func (priv->layout,
6793 invalidated_handler,
6795 g_signal_handlers_disconnect_by_func (priv->layout,
6799 /* Remove layout from all anchored children */
6800 tmp_list = priv->children;
6801 while (tmp_list != NULL)
6803 GtkTextViewChild *vc = tmp_list->data;
6807 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6808 /* vc may now be invalid! */
6811 tmp_list = g_slist_next (tmp_list);
6814 gtk_text_view_stop_cursor_blink (text_view);
6815 gtk_text_view_end_selection_drag (text_view);
6817 g_object_unref (priv->layout);
6818 priv->layout = NULL;
6823 * gtk_text_view_reset_im_context:
6824 * @text_view: a #GtkTextView
6826 * Reset the input method context of the text view if needed.
6828 * This can be necessary in the case where modifying the buffer
6829 * would confuse on-going input method behavior.
6834 gtk_text_view_reset_im_context (GtkTextView *text_view)
6836 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6838 if (text_view->priv->need_im_reset)
6840 text_view->priv->need_im_reset = FALSE;
6841 gtk_im_context_reset (text_view->priv->im_context);
6846 * gtk_text_view_im_context_filter_keypress:
6847 * @text_view: a #GtkTextView
6848 * @event: the key event
6850 * Allow the #GtkTextView input method to internally handle key press
6851 * and release events. If this function returns %TRUE, then no further
6852 * processing should be done for this key event. See
6853 * gtk_im_context_filter_keypress().
6855 * Note that you are expected to call this function from your handler
6856 * when overriding key event handling. This is needed in the case when
6857 * you need to insert your own key handling between the input method
6858 * and the default key event handling of the #GtkTextView.
6862 * gtk_foo_bar_key_press_event (GtkWidget *widget,
6863 * GdkEventKey *event)
6865 * if ((key->keyval == GDK_Return || key->keyval == GDK_KP_Enter))
6867 * if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (view), event))
6871 * /* Do some stuff */
6873 * return GTK_WIDGET_CLASS (gtk_foo_bar_parent_class)->key_press_event (widget, event);
6877 * Return value: %TRUE if the input method handled the key event.
6882 gtk_text_view_im_context_filter_keypress (GtkTextView *text_view,
6885 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6887 return gtk_im_context_filter_keypress (text_view->priv->im_context, event);
6895 drag_begin_cb (GtkWidget *widget,
6896 GdkDragContext *context,
6899 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6900 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6903 GdkPixmap *pixmap = NULL;
6905 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6907 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6908 pixmap = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6912 gtk_drag_set_icon_pixmap (context,
6913 gdk_drawable_get_colormap (pixmap),
6917 g_object_unref (pixmap);
6921 gtk_drag_set_icon_default (context);
6926 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6927 const GtkTextIter *iter,
6928 GdkEventMotion *event)
6930 GtkTargetList *target_list;
6932 text_view->priv->drag_start_x = -1;
6933 text_view->priv->drag_start_y = -1;
6934 text_view->priv->pending_place_cursor_button = 0;
6936 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6938 g_signal_connect (text_view, "drag-begin",
6939 G_CALLBACK (drag_begin_cb), NULL);
6940 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6941 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6942 1, (GdkEvent*)event);
6946 gtk_text_view_drag_begin (GtkWidget *widget,
6947 GdkDragContext *context)
6953 gtk_text_view_drag_end (GtkWidget *widget,
6954 GdkDragContext *context)
6959 gtk_text_view_drag_data_get (GtkWidget *widget,
6960 GdkDragContext *context,
6961 GtkSelectionData *selection_data,
6965 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6966 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6968 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6970 gtk_selection_data_set (selection_data,
6971 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6976 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6983 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6985 /* Extract the selected text */
6986 str = gtk_text_buffer_serialize (buffer, buffer,
6987 selection_data->target,
6994 gtk_selection_data_set (selection_data,
6995 selection_data->target,
6997 (guchar *) str, len);
7007 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
7009 /* Extract the selected text */
7010 str = gtk_text_iter_get_visible_text (&start, &end);
7015 gtk_selection_data_set_text (selection_data, str, -1);
7022 gtk_text_view_drag_data_delete (GtkWidget *widget,
7023 GdkDragContext *context)
7025 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->priv->buffer,
7026 TRUE, GTK_TEXT_VIEW (widget)->priv->editable);
7030 gtk_text_view_drag_leave (GtkWidget *widget,
7031 GdkDragContext *context,
7034 GtkTextView *text_view;
7035 GtkTextViewPrivate *priv;
7037 text_view = GTK_TEXT_VIEW (widget);
7038 priv = text_view->priv;
7040 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7042 if (priv->dnd_device)
7043 priv->dnd_device = NULL;
7045 if (priv->scroll_timeout != 0)
7046 g_source_remove (priv->scroll_timeout);
7048 priv->scroll_timeout = 0;
7052 gtk_text_view_drag_motion (GtkWidget *widget,
7053 GdkDragContext *context,
7058 GtkTextIter newplace;
7059 GtkTextView *text_view;
7060 GtkTextViewPrivate *priv;
7063 GdkRectangle target_rect;
7066 GdkDragAction suggested_action = 0;
7068 text_view = GTK_TEXT_VIEW (widget);
7069 priv = text_view->priv;
7071 target_rect = priv->text_window->allocation;
7073 if (x < target_rect.x ||
7074 y < target_rect.y ||
7075 x > (target_rect.x + target_rect.width) ||
7076 y > (target_rect.y + target_rect.height))
7077 return FALSE; /* outside the text window, allow parent widgets to handle event */
7079 gtk_text_view_window_to_buffer_coords (text_view,
7080 GTK_TEXT_WINDOW_WIDGET,
7084 gtk_text_layout_get_iter_at_pixel (priv->layout,
7088 target = gtk_drag_dest_find_target (widget, context,
7089 gtk_drag_dest_get_target_list (widget));
7091 if (target == GDK_NONE)
7093 /* can't accept any of the offered targets */
7095 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7097 gtk_text_iter_compare (&newplace, &start) >= 0 &&
7098 gtk_text_iter_compare (&newplace, &end) <= 0)
7100 /* We're inside the selection. */
7104 if (gtk_text_iter_can_insert (&newplace, priv->editable))
7106 GtkWidget *source_widget;
7108 suggested_action = context->suggested_action;
7110 source_widget = gtk_drag_get_source_widget (context);
7112 if (source_widget == widget)
7114 /* Default to MOVE, unless the user has
7115 * pressed ctrl or alt to affect available actions
7117 if ((context->actions & GDK_ACTION_MOVE) != 0)
7118 suggested_action = GDK_ACTION_MOVE;
7123 /* Can't drop here. */
7127 if (suggested_action != 0)
7129 gtk_text_mark_set_visible (priv->dnd_mark,
7130 priv->cursor_visible);
7132 gdk_drag_status (context, suggested_action, time);
7136 gdk_drag_status (context, 0, time);
7137 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7140 priv->dnd_device = gdk_drag_context_get_device (context);
7142 if (!priv->scroll_timeout)
7143 priv->scroll_timeout =
7144 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
7146 /* TRUE return means don't propagate the drag motion to parent
7147 * widgets that may also be drop sites.
7153 gtk_text_view_drag_drop (GtkWidget *widget,
7154 GdkDragContext *context,
7159 GtkTextView *text_view;
7160 GtkTextViewPrivate *priv;
7161 GtkTextIter drop_point;
7162 GdkAtom target = GDK_NONE;
7164 text_view = GTK_TEXT_VIEW (widget);
7165 priv = text_view->priv;
7167 if (priv->scroll_timeout != 0)
7168 g_source_remove (priv->scroll_timeout);
7170 priv->scroll_timeout = 0;
7172 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7174 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7178 if (gtk_text_iter_can_insert (&drop_point, priv->editable))
7179 target = gtk_drag_dest_find_target (widget, context, NULL);
7181 if (target != GDK_NONE)
7182 gtk_drag_get_data (widget, context, target, time);
7184 gtk_drag_finish (context, FALSE, FALSE, time);
7190 insert_text_data (GtkTextView *text_view,
7191 GtkTextIter *drop_point,
7192 GtkSelectionData *selection_data)
7196 str = gtk_selection_data_get_text (selection_data);
7200 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
7201 drop_point, (gchar *) str, -1,
7202 text_view->priv->editable))
7204 gtk_widget_error_bell (GTK_WIDGET (text_view));
7212 gtk_text_view_drag_data_received (GtkWidget *widget,
7213 GdkDragContext *context,
7216 GtkSelectionData *selection_data,
7220 GtkTextIter drop_point;
7221 GtkTextView *text_view;
7222 GtkTextViewPrivate *priv;
7223 gboolean success = FALSE;
7224 GtkTextBuffer *buffer = NULL;
7226 text_view = GTK_TEXT_VIEW (widget);
7227 priv = text_view->priv;
7229 if (!priv->dnd_mark)
7232 buffer = get_buffer (text_view);
7234 gtk_text_buffer_get_iter_at_mark (buffer,
7238 if (!gtk_text_iter_can_insert (&drop_point, priv->editable))
7243 gtk_text_buffer_begin_user_action (buffer);
7245 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7247 GtkTextBuffer *src_buffer = NULL;
7248 GtkTextIter start, end;
7249 gboolean copy_tags = TRUE;
7251 if (selection_data->length != sizeof (src_buffer))
7254 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
7256 if (src_buffer == NULL)
7259 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
7261 if (gtk_text_buffer_get_tag_table (src_buffer) !=
7262 gtk_text_buffer_get_tag_table (buffer))
7264 /* try to find a suitable rich text target instead */
7268 GdkAtom target = GDK_NONE;
7272 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
7274 for (list = context->targets; list; list = g_list_next (list))
7278 for (i = 0; i < n_atoms; i++)
7279 if (GUINT_TO_POINTER (atoms[i]) == list->data)
7288 if (target != GDK_NONE)
7290 gtk_drag_get_data (widget, context, target, time);
7291 gtk_text_buffer_end_user_action (buffer);
7296 if (gtk_text_buffer_get_selection_bounds (src_buffer,
7301 gtk_text_buffer_insert_range_interactive (buffer,
7310 str = gtk_text_iter_get_visible_text (&start, &end);
7311 gtk_text_buffer_insert_interactive (buffer,
7312 &drop_point, str, -1,
7318 else if (selection_data->length > 0 &&
7319 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
7322 GError *error = NULL;
7324 retval = gtk_text_buffer_deserialize (buffer, buffer,
7325 selection_data->target,
7327 (guint8 *) selection_data->data,
7328 selection_data->length,
7333 g_warning ("error pasting: %s\n", error->message);
7334 g_clear_error (&error);
7338 insert_text_data (text_view, &drop_point, selection_data);
7341 gtk_drag_finish (context, success,
7342 success && context->action == GDK_ACTION_MOVE,
7347 gtk_text_buffer_get_iter_at_mark (buffer,
7350 gtk_text_buffer_place_cursor (buffer, &drop_point);
7352 gtk_text_buffer_end_user_action (buffer);
7357 * gtk_text_view_get_hadjustment:
7358 * @text_view: a #GtkTextView
7360 * Gets the horizontal-scrolling #GtkAdjustment.
7362 * Returns: (transfer none): pointer to the horizontal #GtkAdjustment
7367 gtk_text_view_get_hadjustment (GtkTextView *text_view)
7369 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7371 return get_hadjustment (text_view);
7375 * gtk_text_view_get_vadjustment:
7376 * @text_view: a #GtkTextView
7378 * Gets the vertical-scrolling #GtkAdjustment.
7380 * Returns: (transfer none): pointer to the vertical #GtkAdjustment
7385 gtk_text_view_get_vadjustment (GtkTextView *text_view)
7387 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7389 return get_vadjustment (text_view);
7392 static GtkAdjustment*
7393 get_hadjustment (GtkTextView *text_view)
7395 if (text_view->priv->hadjustment == NULL)
7396 gtk_text_view_set_scroll_adjustments (text_view,
7397 NULL, /* forces creation */
7398 text_view->priv->vadjustment);
7400 return text_view->priv->hadjustment;
7403 static GtkAdjustment*
7404 get_vadjustment (GtkTextView *text_view)
7406 if (text_view->priv->vadjustment == NULL)
7407 gtk_text_view_set_scroll_adjustments (text_view,
7408 text_view->priv->hadjustment,
7409 NULL); /* forces creation */
7410 return text_view->priv->vadjustment;
7415 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
7416 GtkAdjustment *hadj,
7417 GtkAdjustment *vadj)
7419 GtkTextViewPrivate *priv = text_view->priv;
7420 gboolean need_adjust = FALSE;
7423 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
7425 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7427 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
7429 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7431 if (priv->hadjustment && (priv->hadjustment != hadj))
7433 g_signal_handlers_disconnect_by_func (priv->hadjustment,
7434 gtk_text_view_value_changed,
7436 g_object_unref (priv->hadjustment);
7439 if (priv->vadjustment && (priv->vadjustment != vadj))
7441 g_signal_handlers_disconnect_by_func (priv->vadjustment,
7442 gtk_text_view_value_changed,
7444 g_object_unref (priv->vadjustment);
7447 if (priv->hadjustment != hadj)
7449 priv->hadjustment = hadj;
7450 g_object_ref_sink (priv->hadjustment);
7452 g_signal_connect (priv->hadjustment, "value-changed",
7453 G_CALLBACK (gtk_text_view_value_changed),
7458 if (priv->vadjustment != vadj)
7460 priv->vadjustment = vadj;
7461 g_object_ref_sink (priv->vadjustment);
7463 g_signal_connect (priv->vadjustment, "value-changed",
7464 G_CALLBACK (gtk_text_view_value_changed),
7470 gtk_text_view_value_changed (NULL, text_view);
7473 /* FIXME this adjust_allocation is a big cut-and-paste from
7474 * GtkCList, needs to be some "official" way to do this
7484 /* The window to which widget->window is relative */
7485 #define ALLOCATION_WINDOW(widget) \
7486 (!gtk_widget_get_has_window (widget) ? \
7487 (widget)->window : \
7488 gdk_window_get_parent ((widget)->window))
7491 adjust_allocation_recurse (GtkWidget *widget,
7494 ScrollData *scroll_data = data;
7496 /* Need to really size allocate instead of just poking
7497 * into widget->allocation if the widget is not realized.
7498 * FIXME someone figure out why this was.
7500 if (!gtk_widget_get_realized (widget))
7502 if (gtk_widget_get_visible (widget))
7504 GdkRectangle tmp_rectangle = widget->allocation;
7505 tmp_rectangle.x += scroll_data->dx;
7506 tmp_rectangle.y += scroll_data->dy;
7508 gtk_widget_size_allocate (widget, &tmp_rectangle);
7513 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
7515 widget->allocation.x += scroll_data->dx;
7516 widget->allocation.y += scroll_data->dy;
7518 if (GTK_IS_CONTAINER (widget))
7519 gtk_container_forall (GTK_CONTAINER (widget),
7520 adjust_allocation_recurse,
7527 adjust_allocation (GtkWidget *widget,
7531 ScrollData scroll_data;
7533 if (gtk_widget_get_realized (widget))
7534 scroll_data.window = ALLOCATION_WINDOW (widget);
7536 scroll_data.window = NULL;
7538 scroll_data.dx = dx;
7539 scroll_data.dy = dy;
7541 adjust_allocation_recurse (widget, &scroll_data);
7545 gtk_text_view_value_changed (GtkAdjustment *adj,
7546 GtkTextView *text_view)
7548 GtkTextViewPrivate *priv;
7554 priv = text_view->priv;
7556 /* Note that we oddly call this function with adj == NULL
7560 priv->onscreen_validated = FALSE;
7562 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7563 adj == priv->hadjustment ? "hadj" : adj == priv->vadjustment ? "vadj" : "none",
7564 adj ? adj->value : 0.0));
7566 if (adj == priv->hadjustment)
7568 dx = priv->xoffset - (gint)adj->value;
7569 priv->xoffset = adj->value;
7571 /* If the change is due to a size change we need
7572 * to invalidate the entire text window because there might be
7573 * right-aligned or centered text
7575 if (priv->width_changed)
7577 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7578 gdk_window_invalidate_rect (priv->text_window->bin_window, NULL, FALSE);
7580 priv->width_changed = FALSE;
7583 else if (adj == priv->vadjustment)
7585 dy = priv->yoffset - (gint)adj->value;
7586 priv->yoffset = adj->value;
7590 gtk_text_layout_get_line_at_y (priv->layout, &iter, adj->value, &line_top);
7592 gtk_text_buffer_move_mark (get_buffer (text_view), priv->first_para_mark, &iter);
7594 priv->first_para_pixels = adj->value - line_top;
7598 if (dx != 0 || dy != 0)
7602 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7606 if (priv->left_window)
7607 text_window_scroll (priv->left_window, 0, dy);
7608 if (priv->right_window)
7609 text_window_scroll (priv->right_window, 0, dy);
7614 if (priv->top_window)
7615 text_window_scroll (priv->top_window, dx, 0);
7616 if (priv->bottom_window)
7617 text_window_scroll (priv->bottom_window, dx, 0);
7620 /* It looks nicer to scroll the main area last, because
7621 * it takes a while, and making the side areas update
7622 * afterward emphasizes the slowness of scrolling the
7625 text_window_scroll (priv->text_window, dx, dy);
7628 /* Children are now "moved" in the text window, poke
7629 * into widget->allocation for each child
7631 tmp_list = priv->children;
7632 while (tmp_list != NULL)
7634 GtkTextViewChild *child = tmp_list->data;
7637 adjust_allocation (child->widget, dx, dy);
7639 tmp_list = g_slist_next (tmp_list);
7643 /* This could result in invalidation, which would install the
7644 * first_validate_idle, which would validate onscreen;
7645 * but we're going to go ahead and validate here, so
7646 * first_validate_idle shouldn't have anything to do.
7648 gtk_text_view_update_layout_width (text_view);
7650 /* We also update the IM spot location here, since the IM context
7651 * might do something that leads to validation.
7653 gtk_text_view_update_im_spot_location (text_view);
7655 /* note that validation of onscreen could invoke this function
7656 * recursively, by scrolling to maintain first_para, or in response
7657 * to updating the layout width, however there is no problem with
7658 * that, or shouldn't be.
7660 gtk_text_view_validate_onscreen (text_view);
7662 /* process exposes */
7663 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7665 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7667 if (priv->left_window)
7668 gdk_window_process_updates (priv->left_window->bin_window, TRUE);
7670 if (priv->right_window)
7671 gdk_window_process_updates (priv->right_window->bin_window, TRUE);
7673 if (priv->top_window)
7674 gdk_window_process_updates (priv->top_window->bin_window, TRUE);
7676 if (priv->bottom_window)
7677 gdk_window_process_updates (priv->bottom_window->bin_window, TRUE);
7679 gdk_window_process_updates (priv->text_window->bin_window, TRUE);
7682 /* If this got installed, get rid of it, it's just a waste of time. */
7683 if (priv->first_validate_idle != 0)
7685 g_source_remove (priv->first_validate_idle);
7686 priv->first_validate_idle = 0;
7689 /* Finally we update the IM cursor location again, to ensure any
7690 * changes made by the validation are pushed through.
7692 gtk_text_view_update_im_spot_location (text_view);
7694 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7698 gtk_text_view_commit_handler (GtkIMContext *context,
7700 GtkTextView *text_view)
7702 gtk_text_view_commit_text (text_view, str);
7706 gtk_text_view_commit_text (GtkTextView *text_view,
7709 GtkTextViewPrivate *priv;
7710 gboolean had_selection;
7712 priv = text_view->priv;
7714 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7716 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7719 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7722 if (!strcmp (str, "\n"))
7724 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7727 gtk_widget_error_bell (GTK_WIDGET (text_view));
7732 if (!had_selection && priv->overwrite_mode)
7736 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7738 gtk_text_buffer_get_insert (get_buffer (text_view)));
7739 if (!gtk_text_iter_ends_line (&insert))
7740 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7743 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7746 gtk_widget_error_bell (GTK_WIDGET (text_view));
7750 gtk_text_buffer_end_user_action (get_buffer (text_view));
7752 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7753 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7754 gtk_text_view_scroll_mark_onscreen (text_view,
7755 gtk_text_buffer_get_insert (get_buffer (text_view)));
7759 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7760 GtkTextView *text_view)
7762 GtkTextViewPrivate *priv;
7764 PangoAttrList *attrs;
7768 priv = text_view->priv;
7770 gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter,
7771 gtk_text_buffer_get_insert (priv->buffer));
7773 /* Keypress events are passed to input method even if cursor position is
7774 * not editable; so beep here if it's multi-key input sequence, input
7775 * method will be reset in key-press-event handler.
7777 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7779 if (str && str[0] && !gtk_text_iter_can_insert (&iter, priv->editable))
7781 gtk_widget_error_bell (GTK_WIDGET (text_view));
7785 g_signal_emit (text_view, signals[PREEDIT_CHANGED], 0, str);
7788 gtk_text_layout_set_preedit_string (priv->layout, str, attrs, cursor_pos);
7789 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
7790 gtk_text_view_scroll_mark_onscreen (text_view,
7791 gtk_text_buffer_get_insert (get_buffer (text_view)));
7794 pango_attr_list_unref (attrs);
7799 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7800 GtkTextView *text_view)
7807 gtk_text_buffer_get_iter_at_mark (text_view->priv->buffer, &start,
7808 gtk_text_buffer_get_insert (text_view->priv->buffer));
7811 pos = gtk_text_iter_get_line_index (&start);
7812 gtk_text_iter_set_line_offset (&start, 0);
7813 gtk_text_iter_forward_to_line_end (&end);
7815 text = gtk_text_iter_get_slice (&start, &end);
7816 gtk_im_context_set_surrounding (context, text, -1, pos);
7823 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7826 GtkTextView *text_view)
7828 GtkTextViewPrivate *priv;
7832 priv = text_view->priv;
7834 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start,
7835 gtk_text_buffer_get_insert (priv->buffer));
7838 gtk_text_iter_forward_chars (&start, offset);
7839 gtk_text_iter_forward_chars (&end, offset + n_chars);
7841 gtk_text_buffer_delete_interactive (priv->buffer, &start, &end,
7848 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7849 const GtkTextIter *location,
7853 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7854 gboolean need_reset = FALSE;
7856 if (mark == gtk_text_buffer_get_insert (buffer))
7858 text_view->priv->virtual_cursor_x = -1;
7859 text_view->priv->virtual_cursor_y = -1;
7860 gtk_text_view_update_im_spot_location (text_view);
7863 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7869 gtk_text_view_reset_im_context (text_view);
7873 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7874 const GParamSpec *pspec,
7877 GtkWidget *widget = GTK_WIDGET (data);
7878 GtkTargetList *view_list;
7879 GtkTargetList *buffer_list;
7882 view_list = gtk_drag_dest_get_target_list (widget);
7883 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7886 gtk_target_list_ref (view_list);
7888 view_list = gtk_target_list_new (NULL, 0);
7890 list = view_list->list;
7893 GtkTargetPair *pair = list->data;
7895 list = g_list_next (list); /* get next element before removing */
7897 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7898 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7900 gtk_target_list_remove (view_list, pair->target);
7904 for (list = buffer_list->list; list; list = g_list_next (list))
7906 GtkTargetPair *pair = list->data;
7908 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7911 gtk_drag_dest_set_target_list (widget, view_list);
7912 gtk_target_list_unref (view_list);
7916 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7921 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7922 gtk_text_buffer_get_insert (get_buffer (text_view)));
7924 gtk_text_layout_get_cursor_locations (text_view->priv->layout, &insert, pos, NULL);
7928 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7929 GtkTextIter *cursor,
7933 GtkTextViewPrivate *priv;
7937 priv = text_view->priv;
7942 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7943 gtk_text_buffer_get_insert (get_buffer (text_view)));
7945 if ((x && priv->virtual_cursor_x == -1) ||
7946 (y && priv->virtual_cursor_y == -1))
7947 gtk_text_layout_get_cursor_locations (priv->layout, &insert, &pos, NULL);
7951 if (priv->virtual_cursor_x != -1)
7952 *x = priv->virtual_cursor_x;
7959 if (priv->virtual_cursor_x != -1)
7960 *y = priv->virtual_cursor_y;
7962 *y = pos.y + pos.height / 2;
7967 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7973 if (!text_view->priv->layout)
7976 if (x == -1 || y == -1)
7977 gtk_text_view_get_cursor_location (text_view, &pos);
7979 text_view->priv->virtual_cursor_x = (x == -1) ? pos.x : x;
7980 text_view->priv->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7983 /* Quick hack of a popup menu
7986 activate_cb (GtkWidget *menuitem,
7987 GtkTextView *text_view)
7989 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7990 g_signal_emit_by_name (text_view, signal);
7994 append_action_signal (GtkTextView *text_view,
7996 const gchar *stock_id,
7997 const gchar *signal,
8000 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8002 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8003 g_signal_connect (menuitem, "activate",
8004 G_CALLBACK (activate_cb), text_view);
8006 gtk_widget_set_sensitive (menuitem, sensitive);
8008 gtk_widget_show (menuitem);
8009 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8013 gtk_text_view_select_all (GtkWidget *widget,
8016 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
8017 GtkTextBuffer *buffer;
8018 GtkTextIter start_iter, end_iter, insert;
8020 buffer = text_view->priv->buffer;
8023 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
8024 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
8028 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
8029 gtk_text_buffer_get_insert (buffer));
8030 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
8035 select_all_cb (GtkWidget *menuitem,
8036 GtkTextView *text_view)
8038 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
8042 delete_cb (GtkTextView *text_view)
8044 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
8045 text_view->priv->editable);
8049 popup_menu_detach (GtkWidget *attach_widget,
8052 GTK_TEXT_VIEW (attach_widget)->priv->popup_menu = NULL;
8056 popup_position_func (GtkMenu *menu,
8062 GtkTextView *text_view;
8064 GdkRectangle cursor_rect;
8065 GdkRectangle onscreen_rect;
8066 gint root_x, root_y;
8071 GdkRectangle monitor;
8073 text_view = GTK_TEXT_VIEW (user_data);
8074 widget = GTK_WIDGET (text_view);
8076 g_return_if_fail (gtk_widget_get_realized (widget));
8078 screen = gtk_widget_get_screen (widget);
8080 gdk_window_get_origin (widget->window, &root_x, &root_y);
8082 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
8084 gtk_text_buffer_get_insert (get_buffer (text_view)));
8086 gtk_text_view_get_iter_location (text_view,
8090 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
8092 gtk_widget_size_request (text_view->priv->popup_menu, &req);
8094 /* can't use rectangle_intersect since cursor rect can have 0 width */
8095 if (cursor_rect.x >= onscreen_rect.x &&
8096 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
8097 cursor_rect.y >= onscreen_rect.y &&
8098 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
8100 gtk_text_view_buffer_to_window_coords (text_view,
8101 GTK_TEXT_WINDOW_WIDGET,
8102 cursor_rect.x, cursor_rect.y,
8103 &cursor_rect.x, &cursor_rect.y);
8105 *x = root_x + cursor_rect.x + cursor_rect.width;
8106 *y = root_y + cursor_rect.y + cursor_rect.height;
8110 /* Just center the menu, since cursor is offscreen. */
8111 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
8112 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
8116 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
8117 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
8119 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
8120 gtk_menu_set_monitor (menu, monitor_num);
8121 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8123 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
8124 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
8131 GtkTextView *text_view;
8137 range_contains_editable_text (const GtkTextIter *start,
8138 const GtkTextIter *end,
8139 gboolean default_editability)
8141 GtkTextIter iter = *start;
8143 while (gtk_text_iter_compare (&iter, end) < 0)
8145 if (gtk_text_iter_editable (&iter, default_editability))
8148 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
8155 unichar_chosen_func (const char *text,
8158 GtkTextView *text_view = GTK_TEXT_VIEW (data);
8160 gtk_text_view_commit_text (text_view, text);
8164 popup_targets_received (GtkClipboard *clipboard,
8165 GtkSelectionData *data,
8168 PopupInfo *info = user_data;
8169 GtkTextView *text_view;
8170 GtkTextViewPrivate *priv;
8172 text_view = info->text_view;
8173 priv = text_view->priv;
8175 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
8177 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
8178 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
8180 gboolean clipboard_contains_text;
8181 GtkWidget *menuitem;
8183 gboolean have_selection;
8184 gboolean can_insert;
8186 GtkTextIter sel_start, sel_end;
8187 gboolean show_input_method_menu;
8188 gboolean show_unicode_menu;
8190 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8192 if (priv->popup_menu)
8193 gtk_widget_destroy (priv->popup_menu);
8195 priv->popup_menu = gtk_menu_new ();
8197 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
8198 GTK_WIDGET (text_view),
8201 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
8202 &sel_start, &sel_end);
8204 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
8206 gtk_text_buffer_get_insert (get_buffer (text_view)));
8208 can_insert = gtk_text_iter_can_insert (&iter, priv->editable);
8210 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8212 range_contains_editable_text (&sel_start, &sel_end,
8214 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8216 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8217 can_insert && clipboard_contains_text);
8219 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8220 gtk_widget_set_sensitive (menuitem,
8222 range_contains_editable_text (&sel_start, &sel_end,
8224 g_signal_connect_swapped (menuitem, "activate",
8225 G_CALLBACK (delete_cb), text_view);
8226 gtk_widget_show (menuitem);
8227 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8229 menuitem = gtk_separator_menu_item_new ();
8230 gtk_widget_show (menuitem);
8231 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8233 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8234 g_signal_connect (menuitem, "activate",
8235 G_CALLBACK (select_all_cb), text_view);
8236 gtk_widget_show (menuitem);
8237 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8239 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
8240 "gtk-show-input-method-menu", &show_input_method_menu,
8241 "gtk-show-unicode-menu", &show_unicode_menu,
8244 if (show_input_method_menu || show_unicode_menu)
8246 menuitem = gtk_separator_menu_item_new ();
8247 gtk_widget_show (menuitem);
8248 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8251 if (show_input_method_menu)
8253 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8254 gtk_widget_show (menuitem);
8255 gtk_widget_set_sensitive (menuitem, can_insert);
8257 submenu = gtk_menu_new ();
8258 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8259 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8261 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (priv->im_context),
8262 GTK_MENU_SHELL (submenu));
8265 if (show_unicode_menu)
8267 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8268 gtk_widget_show (menuitem);
8269 gtk_widget_set_sensitive (menuitem, can_insert);
8271 submenu = gtk_menu_new ();
8272 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8273 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8275 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8276 unichar_chosen_func,
8280 g_signal_emit (text_view,
8281 signals[POPULATE_POPUP],
8286 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
8288 info->button, info->time);
8291 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
8292 popup_position_func, text_view,
8293 0, gtk_get_current_event_time ());
8294 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
8298 g_object_unref (text_view);
8303 gtk_text_view_do_popup (GtkTextView *text_view,
8304 GdkEventButton *event)
8306 PopupInfo *info = g_new (PopupInfo, 1);
8308 /* In order to know what entries we should make sensitive, we
8309 * ask for the current targets of the clipboard, and when
8310 * we get them, then we actually pop up the menu.
8312 info->text_view = g_object_ref (text_view);
8316 info->button = event->button;
8317 info->time = event->time;
8322 info->time = gtk_get_current_event_time ();
8325 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
8326 GDK_SELECTION_CLIPBOARD),
8327 gdk_atom_intern_static_string ("TARGETS"),
8328 popup_targets_received,
8333 gtk_text_view_popup_menu (GtkWidget *widget)
8335 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
8339 /* Child GdkWindows */
8342 static GtkTextWindow*
8343 text_window_new (GtkTextWindowType type,
8346 gint height_request)
8350 win = g_new (GtkTextWindow, 1);
8353 win->widget = widget;
8355 win->bin_window = NULL;
8356 win->requisition.width = width_request;
8357 win->requisition.height = height_request;
8358 win->allocation.width = width_request;
8359 win->allocation.height = height_request;
8360 win->allocation.x = 0;
8361 win->allocation.y = 0;
8367 text_window_free (GtkTextWindow *win)
8370 text_window_unrealize (win);
8376 text_window_realize (GtkTextWindow *win,
8379 GdkWindowAttr attributes;
8380 gint attributes_mask;
8383 attributes.window_type = GDK_WINDOW_CHILD;
8384 attributes.x = win->allocation.x;
8385 attributes.y = win->allocation.y;
8386 attributes.width = win->allocation.width;
8387 attributes.height = win->allocation.height;
8388 attributes.wclass = GDK_INPUT_OUTPUT;
8389 attributes.visual = gtk_widget_get_visual (win->widget);
8390 attributes.colormap = gtk_widget_get_colormap (win->widget);
8391 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
8393 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
8395 win->window = gdk_window_new (widget->window,
8399 gdk_window_set_back_pixmap (win->window, NULL, FALSE);
8401 gdk_window_show (win->window);
8402 gdk_window_set_user_data (win->window, win->widget);
8403 gdk_window_lower (win->window);
8407 attributes.width = win->allocation.width;
8408 attributes.height = win->allocation.height;
8409 attributes.event_mask = (GDK_EXPOSURE_MASK |
8411 GDK_KEY_PRESS_MASK |
8412 GDK_BUTTON_PRESS_MASK |
8413 GDK_BUTTON_RELEASE_MASK |
8414 GDK_POINTER_MOTION_MASK |
8415 GDK_POINTER_MOTION_HINT_MASK |
8416 gtk_widget_get_events (win->widget));
8418 win->bin_window = gdk_window_new (win->window,
8422 gdk_window_show (win->bin_window);
8423 gdk_window_set_user_data (win->bin_window, win->widget);
8425 if (win->type == GTK_TEXT_WINDOW_TEXT)
8427 if (gtk_widget_is_sensitive (widget))
8430 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (widget->window),
8432 gdk_window_set_cursor (win->bin_window, cursor);
8433 gdk_cursor_unref (cursor);
8436 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->priv->im_context,
8440 gdk_window_set_background (win->bin_window,
8441 &widget->style->base[gtk_widget_get_state (widget)]);
8445 gdk_window_set_background (win->bin_window,
8446 &widget->style->bg[gtk_widget_get_state (widget)]);
8449 g_object_set_qdata (G_OBJECT (win->window),
8450 g_quark_from_static_string ("gtk-text-view-text-window"),
8453 g_object_set_qdata (G_OBJECT (win->bin_window),
8454 g_quark_from_static_string ("gtk-text-view-text-window"),
8459 text_window_unrealize (GtkTextWindow *win)
8461 if (win->type == GTK_TEXT_WINDOW_TEXT)
8463 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->priv->im_context,
8467 gdk_window_set_user_data (win->window, NULL);
8468 gdk_window_set_user_data (win->bin_window, NULL);
8469 gdk_window_destroy (win->bin_window);
8470 gdk_window_destroy (win->window);
8472 win->bin_window = NULL;
8476 text_window_size_allocate (GtkTextWindow *win,
8479 win->allocation = *rect;
8483 gdk_window_move_resize (win->window,
8485 rect->width, rect->height);
8487 gdk_window_resize (win->bin_window,
8488 rect->width, rect->height);
8493 text_window_scroll (GtkTextWindow *win,
8497 if (dx != 0 || dy != 0)
8499 gdk_window_scroll (win->bin_window, dx, dy);
8504 text_window_invalidate_rect (GtkTextWindow *win,
8507 GdkRectangle window_rect;
8509 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
8516 window_rect.width = rect->width;
8517 window_rect.height = rect->height;
8519 /* Adjust the rect as appropriate */
8523 case GTK_TEXT_WINDOW_TEXT:
8526 case GTK_TEXT_WINDOW_LEFT:
8527 case GTK_TEXT_WINDOW_RIGHT:
8529 window_rect.width = win->allocation.width;
8532 case GTK_TEXT_WINDOW_TOP:
8533 case GTK_TEXT_WINDOW_BOTTOM:
8535 window_rect.height = win->allocation.height;
8539 g_warning ("%s: bug!", G_STRFUNC);
8544 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
8548 cairo_t *cr = gdk_cairo_create (win->bin_window);
8549 gdk_cairo_rectangle (cr, &window_rect);
8550 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
8558 text_window_invalidate_cursors (GtkTextWindow *win)
8560 GtkTextView *text_view;
8561 GtkTextViewPrivate *priv;
8563 GdkRectangle strong;
8565 gboolean draw_arrow;
8566 gfloat cursor_aspect_ratio;
8570 text_view = GTK_TEXT_VIEW (win->widget);
8571 priv = text_view->priv;
8573 gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter,
8574 gtk_text_buffer_get_insert (priv->buffer));
8576 if (_gtk_text_layout_get_block_cursor (priv->layout, &strong))
8578 text_window_invalidate_rect (win, &strong);
8582 gtk_text_layout_get_cursor_locations (priv->layout, &iter,
8585 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
8586 * ignoring the text direction be exposing both sides of the cursor
8589 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
8591 gtk_widget_style_get (win->widget,
8592 "cursor-aspect-ratio", &cursor_aspect_ratio,
8595 stem_width = strong.height * cursor_aspect_ratio + 1;
8596 arrow_width = stem_width + 1;
8598 strong.width = stem_width;
8600 /* round up to the next even number */
8604 strong.x -= stem_width / 2;
8605 strong.width += stem_width;
8609 strong.x -= arrow_width;
8610 strong.width += arrow_width * 2;
8613 text_window_invalidate_rect (win, &strong);
8615 if (draw_arrow) /* == have weak */
8617 stem_width = weak.height * cursor_aspect_ratio + 1;
8618 arrow_width = stem_width + 1;
8620 weak.width = stem_width;
8622 /* round up to the next even number */
8626 weak.x -= stem_width / 2;
8627 weak.width += stem_width;
8629 weak.x -= arrow_width;
8630 weak.width += arrow_width * 2;
8632 text_window_invalidate_rect (win, &weak);
8637 text_window_get_width (GtkTextWindow *win)
8639 return win->allocation.width;
8643 text_window_get_height (GtkTextWindow *win)
8645 return win->allocation.height;
8652 * gtk_text_view_get_window:
8653 * @text_view: a #GtkTextView
8654 * @win: window to get
8656 * Retrieves the #GdkWindow corresponding to an area of the text view;
8657 * possible windows include the overall widget window, child windows
8658 * on the left, right, top, bottom, and the window that displays the
8659 * text buffer. Windows are %NULL and nonexistent if their width or
8660 * height is 0, and are nonexistent before the widget has been
8663 * Return value: (transfer none): a #GdkWindow, or %NULL
8666 gtk_text_view_get_window (GtkTextView *text_view,
8667 GtkTextWindowType win)
8669 GtkTextViewPrivate *priv = text_view->priv;
8671 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8675 case GTK_TEXT_WINDOW_WIDGET:
8676 return GTK_WIDGET (text_view)->window;
8679 case GTK_TEXT_WINDOW_TEXT:
8680 return priv->text_window->bin_window;
8683 case GTK_TEXT_WINDOW_LEFT:
8684 if (priv->left_window)
8685 return priv->left_window->bin_window;
8690 case GTK_TEXT_WINDOW_RIGHT:
8691 if (priv->right_window)
8692 return priv->right_window->bin_window;
8697 case GTK_TEXT_WINDOW_TOP:
8698 if (priv->top_window)
8699 return priv->top_window->bin_window;
8704 case GTK_TEXT_WINDOW_BOTTOM:
8705 if (priv->bottom_window)
8706 return priv->bottom_window->bin_window;
8711 case GTK_TEXT_WINDOW_PRIVATE:
8712 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8717 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8722 * gtk_text_view_get_window_type:
8723 * @text_view: a #GtkTextView
8724 * @window: a window type
8726 * Usually used to find out which window an event corresponds to.
8727 * If you connect to an event signal on @text_view, this function
8728 * should be called on <literal>event->window</literal> to
8729 * see which window it was.
8731 * Return value: the window type.
8734 gtk_text_view_get_window_type (GtkTextView *text_view,
8739 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8740 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8742 if (window == GTK_WIDGET (text_view)->window)
8743 return GTK_TEXT_WINDOW_WIDGET;
8745 win = g_object_get_qdata (G_OBJECT (window),
8746 g_quark_try_string ("gtk-text-view-text-window"));
8752 return GTK_TEXT_WINDOW_PRIVATE;
8757 buffer_to_widget (GtkTextView *text_view,
8763 GtkTextViewPrivate *priv = text_view->priv;
8767 *window_x = buffer_x - priv->xoffset;
8768 *window_x += priv->text_window->allocation.x;
8773 *window_y = buffer_y - priv->yoffset;
8774 *window_y += priv->text_window->allocation.y;
8779 widget_to_text_window (GtkTextWindow *win,
8786 *window_x = widget_x - win->allocation.x;
8789 *window_y = widget_y - win->allocation.y;
8793 buffer_to_text_window (GtkTextView *text_view,
8802 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8803 "for a nonexistent or private child window of GtkTextView");
8807 buffer_to_widget (text_view,
8809 window_x, window_y);
8811 widget_to_text_window (win,
8812 window_x ? *window_x : 0,
8813 window_y ? *window_y : 0,
8819 * gtk_text_view_buffer_to_window_coords:
8820 * @text_view: a #GtkTextView
8821 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8822 * @buffer_x: buffer x coordinate
8823 * @buffer_y: buffer y coordinate
8824 * @window_x: (out) (allow-none): window x coordinate return location or %NULL
8825 * @window_y: (out) (allow-none): window y coordinate return location or %NULL
8827 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8828 * @win, and stores the result in (@window_x, @window_y).
8830 * Note that you can't convert coordinates for a nonexisting window (see
8831 * gtk_text_view_set_border_window_size()).
8834 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8835 GtkTextWindowType win,
8841 GtkTextViewPrivate *priv = text_view->priv;
8843 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8847 case GTK_TEXT_WINDOW_WIDGET:
8848 buffer_to_widget (text_view,
8850 window_x, window_y);
8853 case GTK_TEXT_WINDOW_TEXT:
8855 *window_x = buffer_x - priv->xoffset;
8857 *window_y = buffer_y - priv->yoffset;
8860 case GTK_TEXT_WINDOW_LEFT:
8861 buffer_to_text_window (text_view,
8864 window_x, window_y);
8867 case GTK_TEXT_WINDOW_RIGHT:
8868 buffer_to_text_window (text_view,
8871 window_x, window_y);
8874 case GTK_TEXT_WINDOW_TOP:
8875 buffer_to_text_window (text_view,
8878 window_x, window_y);
8881 case GTK_TEXT_WINDOW_BOTTOM:
8882 buffer_to_text_window (text_view,
8883 priv->bottom_window,
8885 window_x, window_y);
8888 case GTK_TEXT_WINDOW_PRIVATE:
8889 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8893 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8899 widget_to_buffer (GtkTextView *text_view,
8905 GtkTextViewPrivate *priv = text_view->priv;
8909 *buffer_x = widget_x + priv->xoffset;
8910 *buffer_x -= priv->text_window->allocation.x;
8915 *buffer_y = widget_y + priv->yoffset;
8916 *buffer_y -= priv->text_window->allocation.y;
8921 text_window_to_widget (GtkTextWindow *win,
8928 *widget_x = window_x + win->allocation.x;
8931 *widget_y = window_y + win->allocation.y;
8935 text_window_to_buffer (GtkTextView *text_view,
8944 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8945 "coordinates for a nonexistent child window.");
8949 text_window_to_widget (win,
8955 widget_to_buffer (text_view,
8956 buffer_x ? *buffer_x : 0,
8957 buffer_y ? *buffer_y : 0,
8963 * gtk_text_view_window_to_buffer_coords:
8964 * @text_view: a #GtkTextView
8965 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8966 * @window_x: window x coordinate
8967 * @window_y: window y coordinate
8968 * @buffer_x: (out) (allow-none): buffer x coordinate return location or %NULL
8969 * @buffer_y: (out) (allow-none): buffer y coordinate return location or %NULL
8971 * Converts coordinates on the window identified by @win to buffer
8972 * coordinates, storing the result in (@buffer_x,@buffer_y).
8974 * Note that you can't convert coordinates for a nonexisting window (see
8975 * gtk_text_view_set_border_window_size()).
8978 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8979 GtkTextWindowType win,
8985 GtkTextViewPrivate *priv = text_view->priv;
8987 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8991 case GTK_TEXT_WINDOW_WIDGET:
8992 widget_to_buffer (text_view,
8994 buffer_x, buffer_y);
8997 case GTK_TEXT_WINDOW_TEXT:
8999 *buffer_x = window_x + priv->xoffset;
9001 *buffer_y = window_y + priv->yoffset;
9004 case GTK_TEXT_WINDOW_LEFT:
9005 text_window_to_buffer (text_view,
9008 buffer_x, buffer_y);
9011 case GTK_TEXT_WINDOW_RIGHT:
9012 text_window_to_buffer (text_view,
9015 buffer_x, buffer_y);
9018 case GTK_TEXT_WINDOW_TOP:
9019 text_window_to_buffer (text_view,
9022 buffer_x, buffer_y);
9025 case GTK_TEXT_WINDOW_BOTTOM:
9026 text_window_to_buffer (text_view,
9027 priv->bottom_window,
9029 buffer_x, buffer_y);
9032 case GTK_TEXT_WINDOW_PRIVATE:
9033 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
9037 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
9043 set_window_width (GtkTextView *text_view,
9045 GtkTextWindowType type,
9046 GtkTextWindow **winp)
9052 text_window_free (*winp);
9054 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9061 *winp = text_window_new (type,
9062 GTK_WIDGET (text_view),
9064 /* if the widget is already realized we need to realize the child manually */
9065 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9066 text_window_realize (*winp, GTK_WIDGET (text_view));
9070 if ((*winp)->requisition.width == width)
9073 (*winp)->requisition.width = width;
9076 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9082 set_window_height (GtkTextView *text_view,
9084 GtkTextWindowType type,
9085 GtkTextWindow **winp)
9091 text_window_free (*winp);
9093 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9100 *winp = text_window_new (type,
9101 GTK_WIDGET (text_view),
9104 /* if the widget is already realized we need to realize the child manually */
9105 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9106 text_window_realize (*winp, GTK_WIDGET (text_view));
9110 if ((*winp)->requisition.height == height)
9113 (*winp)->requisition.height = height;
9116 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9121 * gtk_text_view_set_border_window_size:
9122 * @text_view: a #GtkTextView
9123 * @type: window to affect
9124 * @size: width or height of the window
9126 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
9127 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
9128 * Automatically destroys the corresponding window if the size is set
9129 * to 0, and creates the window if the size is set to non-zero. This
9130 * function can only be used for the "border windows," it doesn't work
9131 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
9132 * #GTK_TEXT_WINDOW_PRIVATE.
9135 gtk_text_view_set_border_window_size (GtkTextView *text_view,
9136 GtkTextWindowType type,
9139 GtkTextViewPrivate *priv = text_view->priv;
9141 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9142 g_return_if_fail (size >= 0);
9146 case GTK_TEXT_WINDOW_LEFT:
9147 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
9148 &priv->left_window);
9151 case GTK_TEXT_WINDOW_RIGHT:
9152 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
9153 &priv->right_window);
9156 case GTK_TEXT_WINDOW_TOP:
9157 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
9161 case GTK_TEXT_WINDOW_BOTTOM:
9162 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
9163 &priv->bottom_window);
9167 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
9173 * gtk_text_view_get_border_window_size:
9174 * @text_view: a #GtkTextView
9175 * @type: window to return size from
9177 * Gets the width of the specified border window. See
9178 * gtk_text_view_set_border_window_size().
9180 * Return value: width of window
9183 gtk_text_view_get_border_window_size (GtkTextView *text_view,
9184 GtkTextWindowType type)
9186 GtkTextViewPrivate *priv = text_view->priv;
9188 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
9192 case GTK_TEXT_WINDOW_LEFT:
9193 if (priv->left_window)
9194 return priv->left_window->requisition.width;
9197 case GTK_TEXT_WINDOW_RIGHT:
9198 if (priv->right_window)
9199 return priv->right_window->requisition.width;
9202 case GTK_TEXT_WINDOW_TOP:
9203 if (priv->top_window)
9204 return priv->top_window->requisition.height;
9207 case GTK_TEXT_WINDOW_BOTTOM:
9208 if (priv->bottom_window)
9209 return priv->bottom_window->requisition.height;
9213 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
9224 static GtkTextViewChild*
9225 text_view_child_new_anchored (GtkWidget *child,
9226 GtkTextChildAnchor *anchor,
9227 GtkTextLayout *layout)
9229 GtkTextViewChild *vc;
9231 vc = g_new (GtkTextViewChild, 1);
9233 vc->type = GTK_TEXT_WINDOW_PRIVATE;
9235 vc->anchor = anchor;
9237 vc->from_top_of_line = 0;
9238 vc->from_left_of_buffer = 0;
9240 g_object_ref (vc->widget);
9241 g_object_ref (vc->anchor);
9243 g_object_set_data (G_OBJECT (child),
9244 I_("gtk-text-view-child"),
9247 gtk_text_child_anchor_register_child (anchor, child, layout);
9252 static GtkTextViewChild*
9253 text_view_child_new_window (GtkWidget *child,
9254 GtkTextWindowType type,
9258 GtkTextViewChild *vc;
9260 vc = g_new (GtkTextViewChild, 1);
9265 vc->from_top_of_line = 0;
9266 vc->from_left_of_buffer = 0;
9268 g_object_ref (vc->widget);
9274 g_object_set_data (G_OBJECT (child),
9275 I_("gtk-text-view-child"),
9282 text_view_child_free (GtkTextViewChild *child)
9284 g_object_set_data (G_OBJECT (child->widget),
9285 I_("gtk-text-view-child"), NULL);
9289 gtk_text_child_anchor_unregister_child (child->anchor,
9291 g_object_unref (child->anchor);
9294 g_object_unref (child->widget);
9300 text_view_child_set_parent_window (GtkTextView *text_view,
9301 GtkTextViewChild *vc)
9304 gtk_widget_set_parent_window (vc->widget,
9305 text_view->priv->text_window->bin_window);
9309 window = gtk_text_view_get_window (text_view,
9311 gtk_widget_set_parent_window (vc->widget, window);
9316 add_child (GtkTextView *text_view,
9317 GtkTextViewChild *vc)
9319 text_view->priv->children = g_slist_prepend (text_view->priv->children,
9322 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9323 text_view_child_set_parent_window (text_view, vc);
9325 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
9329 * gtk_text_view_add_child_at_anchor:
9330 * @text_view: a #GtkTextView
9331 * @child: a #GtkWidget
9332 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
9334 * Adds a child widget in the text buffer, at the given @anchor.
9337 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
9339 GtkTextChildAnchor *anchor)
9341 GtkTextViewChild *vc;
9343 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9344 g_return_if_fail (GTK_IS_WIDGET (child));
9345 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
9346 g_return_if_fail (child->parent == NULL);
9348 gtk_text_view_ensure_layout (text_view);
9350 vc = text_view_child_new_anchored (child, anchor,
9351 text_view->priv->layout);
9353 add_child (text_view, vc);
9355 g_assert (vc->widget == child);
9356 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9360 * gtk_text_view_add_child_in_window:
9361 * @text_view: a #GtkTextView
9362 * @child: a #GtkWidget
9363 * @which_window: which window the child should appear in
9364 * @xpos: X position of child in window coordinates
9365 * @ypos: Y position of child in window coordinates
9367 * Adds a child at fixed coordinates in one of the text widget's
9368 * windows. The window must have nonzero size (see
9369 * gtk_text_view_set_border_window_size()). Note that the child
9370 * coordinates are given relative to the #GdkWindow in question, and
9371 * that these coordinates have no sane relationship to scrolling. When
9372 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
9373 * irrelevant, the child floats above all scrollable areas. But when
9374 * placing a child in one of the scrollable windows (border windows or
9375 * text window), you'll need to compute the child's correct position
9376 * in buffer coordinates any time scrolling occurs or buffer changes
9377 * occur, and then call gtk_text_view_move_child() to update the
9378 * child's position. Unfortunately there's no good way to detect that
9379 * scrolling has occurred, using the current API; a possible hack
9380 * would be to update all child positions when the scroll adjustments
9381 * change or the text buffer changes. See bug 64518 on
9382 * bugzilla.gnome.org for status of fixing this issue.
9385 gtk_text_view_add_child_in_window (GtkTextView *text_view,
9387 GtkTextWindowType which_window,
9391 GtkTextViewChild *vc;
9393 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9394 g_return_if_fail (GTK_IS_WIDGET (child));
9395 g_return_if_fail (child->parent == NULL);
9397 vc = text_view_child_new_window (child, which_window,
9400 add_child (text_view, vc);
9402 g_assert (vc->widget == child);
9403 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9407 * gtk_text_view_move_child:
9408 * @text_view: a #GtkTextView
9409 * @child: child widget already added to the text view
9410 * @xpos: new X position in window coordinates
9411 * @ypos: new Y position in window coordinates
9413 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
9416 gtk_text_view_move_child (GtkTextView *text_view,
9421 GtkTextViewChild *vc;
9423 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9424 g_return_if_fail (GTK_IS_WIDGET (child));
9425 g_return_if_fail (child->parent == (GtkWidget*) text_view);
9427 vc = g_object_get_data (G_OBJECT (child),
9428 "gtk-text-view-child");
9430 g_assert (vc != NULL);
9432 if (vc->x == xpos &&
9439 if (gtk_widget_get_visible (child) &&
9440 gtk_widget_get_visible (GTK_WIDGET (text_view)))
9441 gtk_widget_queue_resize (child);
9445 /* Iterator operations */
9448 * gtk_text_view_forward_display_line:
9449 * @text_view: a #GtkTextView
9450 * @iter: a #GtkTextIter
9452 * Moves the given @iter forward by one display (wrapped) line.
9453 * A display line is different from a paragraph. Paragraphs are
9454 * separated by newlines or other paragraph separator characters.
9455 * Display lines are created by line-wrapping a paragraph. If
9456 * wrapping is turned off, display lines and paragraphs will be the
9457 * same. Display lines are divided differently for each view, since
9458 * they depend on the view's width; paragraphs are the same in all
9459 * views, since they depend on the contents of the #GtkTextBuffer.
9461 * Return value: %TRUE if @iter was moved and is not on the end iterator
9464 gtk_text_view_forward_display_line (GtkTextView *text_view,
9467 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9468 g_return_val_if_fail (iter != NULL, FALSE);
9470 gtk_text_view_ensure_layout (text_view);
9472 return gtk_text_layout_move_iter_to_next_line (text_view->priv->layout, iter);
9476 * gtk_text_view_backward_display_line:
9477 * @text_view: a #GtkTextView
9478 * @iter: a #GtkTextIter
9480 * Moves the given @iter backward by one display (wrapped) line.
9481 * A display line is different from a paragraph. Paragraphs are
9482 * separated by newlines or other paragraph separator characters.
9483 * Display lines are created by line-wrapping a paragraph. If
9484 * wrapping is turned off, display lines and paragraphs will be the
9485 * same. Display lines are divided differently for each view, since
9486 * they depend on the view's width; paragraphs are the same in all
9487 * views, since they depend on the contents of the #GtkTextBuffer.
9489 * Return value: %TRUE if @iter was moved and is not on the end iterator
9492 gtk_text_view_backward_display_line (GtkTextView *text_view,
9495 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9496 g_return_val_if_fail (iter != NULL, FALSE);
9498 gtk_text_view_ensure_layout (text_view);
9500 return gtk_text_layout_move_iter_to_previous_line (text_view->priv->layout, iter);
9504 * gtk_text_view_forward_display_line_end:
9505 * @text_view: a #GtkTextView
9506 * @iter: a #GtkTextIter
9508 * Moves the given @iter forward to the next display line end.
9509 * A display line is different from a paragraph. Paragraphs are
9510 * separated by newlines or other paragraph separator characters.
9511 * Display lines are created by line-wrapping a paragraph. If
9512 * wrapping is turned off, display lines and paragraphs will be the
9513 * same. Display lines are divided differently for each view, since
9514 * they depend on the view's width; paragraphs are the same in all
9515 * views, since they depend on the contents of the #GtkTextBuffer.
9517 * Return value: %TRUE if @iter was moved and is not on the end iterator
9520 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
9523 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9524 g_return_val_if_fail (iter != NULL, FALSE);
9526 gtk_text_view_ensure_layout (text_view);
9528 return gtk_text_layout_move_iter_to_line_end (text_view->priv->layout, iter, 1);
9532 * gtk_text_view_backward_display_line_start:
9533 * @text_view: a #GtkTextView
9534 * @iter: a #GtkTextIter
9536 * Moves the given @iter backward to the next display line start.
9537 * A display line is different from a paragraph. Paragraphs are
9538 * separated by newlines or other paragraph separator characters.
9539 * Display lines are created by line-wrapping a paragraph. If
9540 * wrapping is turned off, display lines and paragraphs will be the
9541 * same. Display lines are divided differently for each view, since
9542 * they depend on the view's width; paragraphs are the same in all
9543 * views, since they depend on the contents of the #GtkTextBuffer.
9545 * Return value: %TRUE if @iter was moved and is not on the end iterator
9548 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
9551 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9552 g_return_val_if_fail (iter != NULL, FALSE);
9554 gtk_text_view_ensure_layout (text_view);
9556 return gtk_text_layout_move_iter_to_line_end (text_view->priv->layout, iter, -1);
9560 * gtk_text_view_starts_display_line:
9561 * @text_view: a #GtkTextView
9562 * @iter: a #GtkTextIter
9564 * Determines whether @iter is at the start of a display line.
9565 * See gtk_text_view_forward_display_line() for an explanation of
9566 * display lines vs. paragraphs.
9568 * Return value: %TRUE if @iter begins a wrapped line
9571 gtk_text_view_starts_display_line (GtkTextView *text_view,
9572 const GtkTextIter *iter)
9574 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9575 g_return_val_if_fail (iter != NULL, FALSE);
9577 gtk_text_view_ensure_layout (text_view);
9579 return gtk_text_layout_iter_starts_line (text_view->priv->layout, iter);
9583 * gtk_text_view_move_visually:
9584 * @text_view: a #GtkTextView
9585 * @iter: a #GtkTextIter
9586 * @count: number of characters to move (negative moves left,
9587 * positive moves right)
9589 * Move the iterator a given number of characters visually, treating
9590 * it as the strong cursor position. If @count is positive, then the
9591 * new strong cursor position will be @count positions to the right of
9592 * the old cursor position. If @count is negative then the new strong
9593 * cursor position will be @count positions to the left of the old
9596 * In the presence of bi-directional text, the correspondence
9597 * between logical and visual order will depend on the direction
9598 * of the current run, and there may be jumps when the cursor
9599 * is moved off of the end of a run.
9601 * Return value: %TRUE if @iter moved and is not on the end iterator
9604 gtk_text_view_move_visually (GtkTextView *text_view,
9608 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9609 g_return_val_if_fail (iter != NULL, FALSE);
9611 gtk_text_view_ensure_layout (text_view);
9613 return gtk_text_layout_move_iter_visually (text_view->priv->layout, iter, count);