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 "gtkwidgetprivate.h"
51 #include "gtkwindow.h"
52 #include "gtkscrollable.h"
55 /* How scrolling, validation, exposes, etc. work.
57 * The expose_event handler has the invariant that the onscreen lines
58 * have been validated.
60 * There are two ways that onscreen lines can become invalid. The first
61 * is to change which lines are onscreen. This happens when the value
62 * of a scroll adjustment changes. So the code path begins in
63 * gtk_text_view_value_changed() and goes like this:
64 * - gdk_window_scroll() to reflect the new adjustment value
65 * - validate the lines that were moved onscreen
66 * - gdk_window_process_updates() to handle the exposes immediately
68 * The second way is that you get the "invalidated" signal from the layout,
69 * indicating that lines have become invalid. This code path begins in
70 * invalidated_handler() and goes like this:
71 * - install high-priority idle which does the rest of the steps
72 * - if a scroll is pending from scroll_to_mark(), do the scroll,
73 * jumping to the gtk_text_view_value_changed() code path
74 * - otherwise, validate the onscreen lines
75 * - DO NOT process updates
77 * In both cases, validating the onscreen lines can trigger a scroll
78 * due to maintaining the first_para on the top of the screen.
79 * If validation triggers a scroll, we jump to the top of the code path
80 * for value_changed, and bail out of the current code path.
82 * Also, in size_allocate, if we invalidate some lines from changing
83 * the layout width, we need to go ahead and run the high-priority idle,
84 * because GTK sends exposes right after doing the size allocates without
85 * returning to the main loop. This is also why the high-priority idle
86 * is at a higher priority than resizing.
91 #define DEBUG_VALIDATION_AND_SCROLLING
94 #ifdef DEBUG_VALIDATION_AND_SCROLLING
100 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->priv->text_window)
101 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->priv->text_window)
103 #define SPACE_FOR_CURSOR 1
105 #define GTK_TEXT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_VIEW, GtkTextViewPrivate))
107 typedef struct _GtkTextWindow GtkTextWindow;
108 typedef struct _GtkTextPendingScroll GtkTextPendingScroll;
110 struct _GtkTextViewPrivate
112 GtkTextLayout *layout;
113 GtkTextBuffer *buffer;
115 guint blink_time; /* time in msec the cursor has blinked since last user event */
118 GdkDevice *grab_device;
119 GdkDevice *dnd_device;
121 gulong selection_drag_handler;
122 guint scroll_timeout;
124 GtkTextWindow *text_window;
125 GtkTextWindow *left_window;
126 GtkTextWindow *right_window;
127 GtkTextWindow *top_window;
128 GtkTextWindow *bottom_window;
130 GtkAdjustment *hadjustment;
131 GtkAdjustment *vadjustment;
132 gint min_display_width;
133 gint min_display_height;
135 gint xoffset; /* Offsets between widget coordinates and buffer coordinates */
137 gint width; /* Width and height of the buffer */
140 /* This is used to monitor the overall size request
141 * and decide whether we need to queue resizes when
142 * the buffer content changes.
144 * FIXME: This could be done in a simpler way by
145 * consulting the above width/height of the buffer + some
146 * padding values, however all of this request code needs
147 * to be changed to use GtkWidget Iface and deserves
150 GtkRequisition cached_size_request;
152 /* The virtual cursor position is normally the same as the
153 * actual (strong) cursor position, except in two circumstances:
155 * a) When the cursor is moved vertically with the keyboard
156 * b) When the text view is scrolled with the keyboard
158 * In case a), virtual_cursor_x is preserved, but not virtual_cursor_y
159 * In case b), both virtual_cursor_x and virtual_cursor_y are preserved.
161 gint virtual_cursor_x; /* -1 means use actual cursor position */
162 gint virtual_cursor_y; /* -1 means use actual cursor position */
164 GtkTextMark *first_para_mark; /* Mark at the beginning of the first onscreen paragraph */
165 gint first_para_pixels; /* Offset of top of screen in the first onscreen paragraph */
167 GtkTextMark *dnd_mark;
170 guint first_validate_idle; /* Idle to revalidate onscreen portion, runs before resize */
171 guint incremental_validate_idle; /* Idle to revalidate offscreen portions, runs after redraw */
173 GtkIMContext *im_context;
174 GtkWidget *popup_menu;
181 GtkTextPendingScroll *pending_scroll;
183 gint pending_place_cursor_button;
185 /* Default style settings */
186 gint pixels_above_lines;
187 gint pixels_below_lines;
188 gint pixels_inside_wrap;
189 GtkWrapMode wrap_mode;
190 GtkJustification justify;
197 guint overwrite_mode : 1;
198 guint cursor_visible : 1;
200 /* if we have reset the IM since the last character entered */
201 guint need_im_reset : 1;
203 guint accepts_tab : 1;
205 guint width_changed : 1;
207 /* debug flag - means that we've validated onscreen since the
208 * last "invalidate" signal from the layout
210 guint onscreen_validated : 1;
212 guint mouse_cursor_obscured : 1;
214 guint scroll_after_paste : 1;
217 struct _GtkTextPendingScroll
220 gdouble within_margin;
241 TOGGLE_CURSOR_VISIBLE,
249 PROP_PIXELS_ABOVE_LINES,
250 PROP_PIXELS_BELOW_LINES,
251 PROP_PIXELS_INSIDE_WRAP,
266 PROP_MIN_DISPLAY_WIDTH,
267 PROP_MIN_DISPLAY_HEIGHT
270 static void gtk_text_view_finalize (GObject *object);
271 static void gtk_text_view_set_property (GObject *object,
275 static void gtk_text_view_get_property (GObject *object,
279 static void gtk_text_view_destroy (GtkWidget *widget);
280 static void gtk_text_view_size_request (GtkWidget *widget,
281 GtkRequisition *requisition);
282 static void gtk_text_view_size_allocate (GtkWidget *widget,
283 GtkAllocation *allocation);
284 static void gtk_text_view_realize (GtkWidget *widget);
285 static void gtk_text_view_unrealize (GtkWidget *widget);
286 static void gtk_text_view_style_set (GtkWidget *widget,
287 GtkStyle *previous_style);
288 static void gtk_text_view_direction_changed (GtkWidget *widget,
289 GtkTextDirection previous_direction);
290 static void gtk_text_view_grab_notify (GtkWidget *widget,
291 gboolean was_grabbed);
292 static void gtk_text_view_state_changed (GtkWidget *widget,
293 GtkStateType previous_state);
295 static gint gtk_text_view_event (GtkWidget *widget,
297 static gint gtk_text_view_key_press_event (GtkWidget *widget,
299 static gint gtk_text_view_key_release_event (GtkWidget *widget,
301 static gint gtk_text_view_button_press_event (GtkWidget *widget,
302 GdkEventButton *event);
303 static gint gtk_text_view_button_release_event (GtkWidget *widget,
304 GdkEventButton *event);
305 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
306 GdkEventFocus *event);
307 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
308 GdkEventFocus *event);
309 static gint gtk_text_view_motion_event (GtkWidget *widget,
310 GdkEventMotion *event);
311 static gint gtk_text_view_draw (GtkWidget *widget,
313 static void gtk_text_view_draw_focus (GtkWidget *widget,
315 static gboolean gtk_text_view_focus (GtkWidget *widget,
316 GtkDirectionType direction);
317 static void gtk_text_view_select_all (GtkWidget *widget,
321 /* Source side drag signals */
322 static void gtk_text_view_drag_begin (GtkWidget *widget,
323 GdkDragContext *context);
324 static void gtk_text_view_drag_end (GtkWidget *widget,
325 GdkDragContext *context);
326 static void gtk_text_view_drag_data_get (GtkWidget *widget,
327 GdkDragContext *context,
328 GtkSelectionData *selection_data,
331 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
332 GdkDragContext *context);
334 /* Target side drag signals */
335 static void gtk_text_view_drag_leave (GtkWidget *widget,
336 GdkDragContext *context,
338 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
339 GdkDragContext *context,
343 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
344 GdkDragContext *context,
348 static void gtk_text_view_drag_data_received (GtkWidget *widget,
349 GdkDragContext *context,
352 GtkSelectionData *selection_data,
356 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
358 static void gtk_text_view_move_cursor (GtkTextView *text_view,
359 GtkMovementStep step,
361 gboolean extend_selection);
362 static gboolean gtk_text_view_move_viewport (GtkTextView *text_view,
365 static void gtk_text_view_set_anchor (GtkTextView *text_view);
366 static gboolean gtk_text_view_scroll_pages (GtkTextView *text_view,
368 gboolean extend_selection);
369 static gboolean gtk_text_view_scroll_hpages(GtkTextView *text_view,
371 gboolean extend_selection);
372 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
374 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
377 static void gtk_text_view_backspace (GtkTextView *text_view);
378 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
379 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
380 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
381 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
382 static void gtk_text_view_toggle_cursor_visible (GtkTextView *text_view);
384 static void gtk_text_view_unselect (GtkTextView *text_view);
386 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
387 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
389 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
390 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
391 GtkTextAttributes *values,
393 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
394 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
395 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
396 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
397 const GtkTextIter *iter,
398 GdkEventButton *event);
399 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view);
400 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
401 const GtkTextIter *iter,
402 GdkEventMotion *event);
403 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
404 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
405 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
406 static void gtk_text_view_reset_blink_time (GtkTextView *text_view);
408 static void gtk_text_view_value_changed (GtkAdjustment *adj,
410 static void gtk_text_view_commit_handler (GtkIMContext *context,
412 GtkTextView *text_view);
413 static void gtk_text_view_commit_text (GtkTextView *text_view,
415 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
416 GtkTextView *text_view);
417 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
418 GtkTextView *text_view);
419 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
422 GtkTextView *text_view);
424 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
425 const GtkTextIter *location,
428 static void gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
429 const GParamSpec *pspec,
431 static void gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
432 GtkClipboard *clipboard,
434 static void gtk_text_view_get_cursor_location (GtkTextView *text_view,
436 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
440 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
444 static void gtk_text_view_do_popup (GtkTextView *text_view,
445 GdkEventButton *event);
447 static void cancel_pending_scroll (GtkTextView *text_view);
448 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
450 gdouble within_margin,
455 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
456 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
457 static void gtk_text_view_invalidate (GtkTextView *text_view);
458 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
460 static void gtk_text_view_set_hadjustment (GtkTextView *text_view,
461 GtkAdjustment *adjustment);
462 static void gtk_text_view_set_vadjustment (GtkTextView *text_view,
463 GtkAdjustment *adjustment);
464 static void gtk_text_view_set_hadjustment_values (GtkTextView *text_view);
465 static void gtk_text_view_set_vadjustment_values (GtkTextView *text_view);
467 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
469 /* Container methods */
470 static void gtk_text_view_add (GtkContainer *container,
472 static void gtk_text_view_remove (GtkContainer *container,
474 static void gtk_text_view_forall (GtkContainer *container,
475 gboolean include_internals,
476 GtkCallback callback,
477 gpointer callback_data);
479 /* FIXME probably need the focus methods. */
481 typedef struct _GtkTextViewChild GtkTextViewChild;
483 struct _GtkTextViewChild
487 GtkTextChildAnchor *anchor;
489 gint from_top_of_line;
490 gint from_left_of_buffer;
492 /* These are ignored if anchor != NULL */
493 GtkTextWindowType type;
498 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
499 GtkTextChildAnchor *anchor,
500 GtkTextLayout *layout);
501 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
502 GtkTextWindowType type,
505 static void text_view_child_free (GtkTextViewChild *child);
506 static void text_view_child_set_parent_window (GtkTextView *text_view,
507 GtkTextViewChild *child);
509 struct _GtkTextWindow
511 GtkTextWindowType type;
514 GdkWindow *bin_window;
515 GtkRequisition requisition;
516 GdkRectangle allocation;
519 static GtkTextWindow *text_window_new (GtkTextWindowType type,
522 gint height_request);
523 static void text_window_free (GtkTextWindow *win);
524 static void text_window_realize (GtkTextWindow *win,
526 static void text_window_unrealize (GtkTextWindow *win);
527 static void text_window_size_allocate (GtkTextWindow *win,
529 static void text_window_scroll (GtkTextWindow *win,
532 static void text_window_invalidate_rect (GtkTextWindow *win,
534 static void text_window_invalidate_cursors (GtkTextWindow *win);
536 static gint text_window_get_width (GtkTextWindow *win);
537 static gint text_window_get_height (GtkTextWindow *win);
540 static guint signals[LAST_SIGNAL] = { 0 };
542 G_DEFINE_TYPE_WITH_CODE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER,
543 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
546 add_move_binding (GtkBindingSet *binding_set,
549 GtkMovementStep step,
552 g_assert ((modmask & GDK_SHIFT_MASK) == 0);
554 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
558 G_TYPE_BOOLEAN, FALSE);
560 /* Selection-extending version */
561 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
565 G_TYPE_BOOLEAN, TRUE);
569 gtk_text_view_class_init (GtkTextViewClass *klass)
571 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
572 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
573 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
574 GtkBindingSet *binding_set;
576 /* Default handlers and virtual methods
578 gobject_class->set_property = gtk_text_view_set_property;
579 gobject_class->get_property = gtk_text_view_get_property;
580 gobject_class->finalize = gtk_text_view_finalize;
582 widget_class->destroy = gtk_text_view_destroy;
583 widget_class->realize = gtk_text_view_realize;
584 widget_class->unrealize = gtk_text_view_unrealize;
585 widget_class->style_set = gtk_text_view_style_set;
586 widget_class->direction_changed = gtk_text_view_direction_changed;
587 widget_class->grab_notify = gtk_text_view_grab_notify;
588 widget_class->state_changed = gtk_text_view_state_changed;
589 widget_class->size_request = gtk_text_view_size_request;
590 widget_class->size_allocate = gtk_text_view_size_allocate;
591 widget_class->event = gtk_text_view_event;
592 widget_class->key_press_event = gtk_text_view_key_press_event;
593 widget_class->key_release_event = gtk_text_view_key_release_event;
594 widget_class->button_press_event = gtk_text_view_button_press_event;
595 widget_class->button_release_event = gtk_text_view_button_release_event;
596 widget_class->focus_in_event = gtk_text_view_focus_in_event;
597 widget_class->focus_out_event = gtk_text_view_focus_out_event;
598 widget_class->motion_notify_event = gtk_text_view_motion_event;
599 widget_class->draw = gtk_text_view_draw;
600 widget_class->focus = gtk_text_view_focus;
601 widget_class->drag_begin = gtk_text_view_drag_begin;
602 widget_class->drag_end = gtk_text_view_drag_end;
603 widget_class->drag_data_get = gtk_text_view_drag_data_get;
604 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
606 widget_class->drag_leave = gtk_text_view_drag_leave;
607 widget_class->drag_motion = gtk_text_view_drag_motion;
608 widget_class->drag_drop = gtk_text_view_drag_drop;
609 widget_class->drag_data_received = gtk_text_view_drag_data_received;
611 widget_class->popup_menu = gtk_text_view_popup_menu;
613 container_class->add = gtk_text_view_add;
614 container_class->remove = gtk_text_view_remove;
615 container_class->forall = gtk_text_view_forall;
617 klass->move_cursor = gtk_text_view_move_cursor;
618 klass->set_anchor = gtk_text_view_set_anchor;
619 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
620 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
621 klass->backspace = gtk_text_view_backspace;
622 klass->cut_clipboard = gtk_text_view_cut_clipboard;
623 klass->copy_clipboard = gtk_text_view_copy_clipboard;
624 klass->paste_clipboard = gtk_text_view_paste_clipboard;
625 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
631 g_object_class_install_property (gobject_class,
632 PROP_PIXELS_ABOVE_LINES,
633 g_param_spec_int ("pixels-above-lines",
634 P_("Pixels Above Lines"),
635 P_("Pixels of blank space above paragraphs"),
639 GTK_PARAM_READWRITE));
641 g_object_class_install_property (gobject_class,
642 PROP_PIXELS_BELOW_LINES,
643 g_param_spec_int ("pixels-below-lines",
644 P_("Pixels Below Lines"),
645 P_("Pixels of blank space below paragraphs"),
649 GTK_PARAM_READWRITE));
651 g_object_class_install_property (gobject_class,
652 PROP_PIXELS_INSIDE_WRAP,
653 g_param_spec_int ("pixels-inside-wrap",
654 P_("Pixels Inside Wrap"),
655 P_("Pixels of blank space between wrapped lines in a paragraph"),
659 GTK_PARAM_READWRITE));
661 g_object_class_install_property (gobject_class,
663 g_param_spec_boolean ("editable",
665 P_("Whether the text can be modified by the user"),
667 GTK_PARAM_READWRITE));
669 g_object_class_install_property (gobject_class,
671 g_param_spec_enum ("wrap-mode",
673 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
676 GTK_PARAM_READWRITE));
678 g_object_class_install_property (gobject_class,
680 g_param_spec_enum ("justification",
682 P_("Left, right, or center justification"),
683 GTK_TYPE_JUSTIFICATION,
685 GTK_PARAM_READWRITE));
687 g_object_class_install_property (gobject_class,
689 g_param_spec_int ("left-margin",
691 P_("Width of the left margin in pixels"),
695 GTK_PARAM_READWRITE));
697 g_object_class_install_property (gobject_class,
699 g_param_spec_int ("right-margin",
701 P_("Width of the right margin in pixels"),
705 GTK_PARAM_READWRITE));
707 g_object_class_install_property (gobject_class,
709 g_param_spec_int ("indent",
711 P_("Amount to indent the paragraph, in pixels"),
715 GTK_PARAM_READWRITE));
717 g_object_class_install_property (gobject_class,
719 g_param_spec_boxed ("tabs",
721 P_("Custom tabs for this text"),
722 PANGO_TYPE_TAB_ARRAY,
723 GTK_PARAM_READWRITE));
725 g_object_class_install_property (gobject_class,
727 g_param_spec_boolean ("cursor-visible",
728 P_("Cursor Visible"),
729 P_("If the insertion cursor is shown"),
731 GTK_PARAM_READWRITE));
733 g_object_class_install_property (gobject_class,
735 g_param_spec_object ("buffer",
737 P_("The buffer which is displayed"),
738 GTK_TYPE_TEXT_BUFFER,
739 GTK_PARAM_READWRITE));
741 g_object_class_install_property (gobject_class,
743 g_param_spec_boolean ("overwrite",
744 P_("Overwrite mode"),
745 P_("Whether entered text overwrites existing contents"),
747 GTK_PARAM_READWRITE));
749 g_object_class_install_property (gobject_class,
751 g_param_spec_boolean ("accepts-tab",
753 P_("Whether Tab will result in a tab character being entered"),
755 GTK_PARAM_READWRITE));
758 * GtkTextView:im-module:
760 * Which IM (input method) module should be used for this entry.
763 * Setting this to a non-%NULL value overrides the
764 * system-wide IM module setting. See the GtkSettings
765 * #GtkSettings:gtk-im-module property.
769 g_object_class_install_property (gobject_class,
771 g_param_spec_string ("im-module",
773 P_("Which IM module should be used"),
775 GTK_PARAM_READWRITE));
777 /* GtkScrollable interface */
778 g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment");
779 g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment");
780 g_object_class_override_property (gobject_class, PROP_MIN_DISPLAY_WIDTH, "min-display-width");
781 g_object_class_override_property (gobject_class, PROP_MIN_DISPLAY_HEIGHT, "min-display-height");
786 gtk_widget_class_install_style_property (widget_class,
787 g_param_spec_boxed ("error-underline-color",
788 P_("Error underline color"),
789 P_("Color with which to draw error-indication underlines"),
791 GTK_PARAM_READABLE));
798 * GtkTextView::move-cursor:
799 * @text_view: the object which received the signal
800 * @step: the granularity of the move, as a #GtkMovementStep
801 * @count: the number of @step units to move
802 * @extend_selection: %TRUE if the move should extend the selection
804 * The ::move-cursor signal is a
805 * <link linkend="keybinding-signals">keybinding signal</link>
806 * which gets emitted when the user initiates a cursor movement.
807 * If the cursor is not visible in @text_view, this signal causes
808 * the viewport to be moved instead.
810 * Applications should not connect to it, but may emit it with
811 * g_signal_emit_by_name() if they need to control the cursor
814 * The default bindings for this signal come in two variants,
815 * the variant with the Shift modifier extends the selection,
816 * the variant without the Shift modifer does not.
817 * There are too many key combinations to list them all here.
819 * <listitem>Arrow keys move by individual characters/lines</listitem>
820 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
821 * <listitem>Home/End keys move to the ends of the buffer</listitem>
822 * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
823 * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
826 signals[MOVE_CURSOR] =
827 g_signal_new (I_("move-cursor"),
828 G_OBJECT_CLASS_TYPE (gobject_class),
829 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
830 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
832 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
834 GTK_TYPE_MOVEMENT_STEP,
839 * GtkTextView::move-viewport:
840 * @text_view: the object which received the signal
841 * @step: the granularity of the move, as a #GtkMovementStep
842 * @count: the number of @step units to move
844 * The ::move-viewport signal is a
845 * <link linkend="keybinding-signals">keybinding signal</link>
846 * which can be bound to key combinations to allow the user
847 * to move the viewport, i.e. change what part of the text view
848 * is visible in a containing scrolled window.
850 * There are no default bindings for this signal.
852 signals[MOVE_VIEWPORT] =
853 g_signal_new_class_handler (I_("move-viewport"),
854 G_OBJECT_CLASS_TYPE (gobject_class),
855 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
856 G_CALLBACK (gtk_text_view_move_viewport),
858 _gtk_marshal_VOID__ENUM_INT,
860 GTK_TYPE_SCROLL_STEP,
864 * GtkTextView::set-anchor:
865 * @text_view: the object which received the signal
867 * The ::set-anchor signal is a
868 * <link linkend="keybinding-signals">keybinding signal</link>
869 * which gets emitted when the user initiates setting the "anchor"
870 * mark. The "anchor" mark gets placed at the same position as the
873 * This signal has no default bindings.
875 signals[SET_ANCHOR] =
876 g_signal_new (I_("set-anchor"),
877 G_OBJECT_CLASS_TYPE (gobject_class),
878 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
879 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
881 _gtk_marshal_VOID__VOID,
885 * GtkTextView::insert-at-cursor:
886 * @text_view: the object which received the signal
887 * @string: the string to insert
889 * The ::insert-at-cursor signal is a
890 * <link linkend="keybinding-signals">keybinding signal</link>
891 * which gets emitted when the user initiates the insertion of a
892 * fixed string at the cursor.
894 * This signal has no default bindings.
896 signals[INSERT_AT_CURSOR] =
897 g_signal_new (I_("insert-at-cursor"),
898 G_OBJECT_CLASS_TYPE (gobject_class),
899 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
900 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
902 _gtk_marshal_VOID__STRING,
907 * GtkTextView::delete-from-cursor:
908 * @text_view: the object which received the signal
909 * @type: the granularity of the deletion, as a #GtkDeleteType
910 * @count: the number of @type units to delete
912 * The ::delete-from-cursor signal is a
913 * <link linkend="keybinding-signals">keybinding signal</link>
914 * which gets emitted when the user initiates a text deletion.
916 * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
917 * if there is one, otherwise it deletes the requested number
920 * The default bindings for this signal are
921 * Delete for deleting a character, Ctrl-Delete for
922 * deleting a word and Ctrl-Backspace for deleting a word
925 signals[DELETE_FROM_CURSOR] =
926 g_signal_new (I_("delete-from-cursor"),
927 G_OBJECT_CLASS_TYPE (gobject_class),
928 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
929 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
931 _gtk_marshal_VOID__ENUM_INT,
933 GTK_TYPE_DELETE_TYPE,
937 * GtkTextView::backspace:
938 * @text_view: the object which received the signal
940 * The ::backspace signal is a
941 * <link linkend="keybinding-signals">keybinding signal</link>
942 * which gets emitted when the user asks for it.
944 * The default bindings for this signal are
945 * Backspace and Shift-Backspace.
948 g_signal_new (I_("backspace"),
949 G_OBJECT_CLASS_TYPE (gobject_class),
950 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
951 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
953 _gtk_marshal_VOID__VOID,
957 * GtkTextView::cut-clipboard:
958 * @text_view: the object which received the signal
960 * The ::cut-clipboard signal is a
961 * <link linkend="keybinding-signals">keybinding signal</link>
962 * which gets emitted to cut the selection to the clipboard.
964 * The default bindings for this signal are
965 * Ctrl-x and Shift-Delete.
967 signals[CUT_CLIPBOARD] =
968 g_signal_new (I_("cut-clipboard"),
969 G_OBJECT_CLASS_TYPE (gobject_class),
970 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
971 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
973 _gtk_marshal_VOID__VOID,
977 * GtkTextView::copy-clipboard:
978 * @text_view: the object which received the signal
980 * The ::copy-clipboard signal is a
981 * <link linkend="keybinding-signals">keybinding signal</link>
982 * which gets emitted to copy the selection to the clipboard.
984 * The default bindings for this signal are
985 * Ctrl-c and Ctrl-Insert.
987 signals[COPY_CLIPBOARD] =
988 g_signal_new (I_("copy-clipboard"),
989 G_OBJECT_CLASS_TYPE (gobject_class),
990 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
991 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
993 _gtk_marshal_VOID__VOID,
997 * GtkTextView::paste-clipboard:
998 * @text_view: the object which received the signal
1000 * The ::paste-clipboard signal is a
1001 * <link linkend="keybinding-signals">keybinding signal</link>
1002 * which gets emitted to paste the contents of the clipboard
1003 * into the text view.
1005 * The default bindings for this signal are
1006 * Ctrl-v and Shift-Insert.
1008 signals[PASTE_CLIPBOARD] =
1009 g_signal_new (I_("paste-clipboard"),
1010 G_OBJECT_CLASS_TYPE (gobject_class),
1011 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1012 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
1014 _gtk_marshal_VOID__VOID,
1018 * GtkTextView::toggle-overwrite:
1019 * @text_view: the object which received the signal
1021 * The ::toggle-overwrite signal is a
1022 * <link linkend="keybinding-signals">keybinding signal</link>
1023 * which gets emitted to toggle the overwrite mode of the text view.
1025 * The default bindings for this signal is Insert.
1027 signals[TOGGLE_OVERWRITE] =
1028 g_signal_new (I_("toggle-overwrite"),
1029 G_OBJECT_CLASS_TYPE (gobject_class),
1030 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1031 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
1033 _gtk_marshal_VOID__VOID,
1037 * GtkTextView::populate-popup:
1038 * @entry: The text view on which the signal is emitted
1039 * @menu: the menu that is being populated
1041 * The ::populate-popup signal gets emitted before showing the
1042 * context menu of the text view.
1044 * If you need to add items to the context menu, connect
1045 * to this signal and append your menuitems to the @menu.
1047 signals[POPULATE_POPUP] =
1048 g_signal_new (I_("populate-popup"),
1049 G_OBJECT_CLASS_TYPE (gobject_class),
1051 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
1053 _gtk_marshal_VOID__OBJECT,
1058 * GtkTextView::select-all:
1059 * @text_view: the object which received the signal
1060 * @select: %TRUE to select, %FALSE to unselect
1062 * The ::select-all signal is a
1063 * <link linkend="keybinding-signals">keybinding signal</link>
1064 * which gets emitted to select or unselect the complete
1065 * contents of the text view.
1067 * The default bindings for this signal are Ctrl-a and Ctrl-/
1068 * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
1070 signals[SELECT_ALL] =
1071 g_signal_new_class_handler (I_("select-all"),
1072 G_OBJECT_CLASS_TYPE (gobject_class),
1073 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1074 G_CALLBACK (gtk_text_view_select_all),
1076 _gtk_marshal_VOID__BOOLEAN,
1077 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
1080 * GtkTextView::toggle-cursor-visible:
1081 * @text_view: the object which received the signal
1083 * The ::toggle-cursor-visible signal is a
1084 * <link linkend="keybinding-signals">keybinding signal</link>
1085 * which gets emitted to toggle the visibility of the cursor.
1087 * The default binding for this signal is F7.
1089 signals[TOGGLE_CURSOR_VISIBLE] =
1090 g_signal_new_class_handler (I_("toggle-cursor-visible"),
1091 G_OBJECT_CLASS_TYPE (gobject_class),
1092 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1093 G_CALLBACK (gtk_text_view_toggle_cursor_visible),
1095 _gtk_marshal_VOID__VOID,
1099 * GtkTextView::preedit-changed:
1100 * @text_view: the object which received the signal
1101 * @preedit: the current preedit string
1103 * If an input method is used, the typed text will not immediately
1104 * be committed to the buffer. So if you are interested in the text,
1105 * connect to this signal.
1107 * This signal is only emitted if the text at the given position
1108 * is actually editable.
1112 signals[PREEDIT_CHANGED] =
1113 g_signal_new_class_handler (I_("preedit-changed"),
1114 G_OBJECT_CLASS_TYPE (gobject_class),
1115 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1118 _gtk_marshal_VOID__STRING,
1126 binding_set = gtk_binding_set_by_class (klass);
1128 /* Moving the insertion point */
1129 add_move_binding (binding_set, GDK_KEY_Right, 0,
1130 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1132 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
1133 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1135 add_move_binding (binding_set, GDK_KEY_Left, 0,
1136 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1138 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
1139 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1141 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1142 GTK_MOVEMENT_WORDS, 1);
1144 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1145 GTK_MOVEMENT_WORDS, 1);
1147 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1148 GTK_MOVEMENT_WORDS, -1);
1150 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1151 GTK_MOVEMENT_WORDS, -1);
1153 add_move_binding (binding_set, GDK_KEY_Up, 0,
1154 GTK_MOVEMENT_DISPLAY_LINES, -1);
1156 add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
1157 GTK_MOVEMENT_DISPLAY_LINES, -1);
1159 add_move_binding (binding_set, GDK_KEY_Down, 0,
1160 GTK_MOVEMENT_DISPLAY_LINES, 1);
1162 add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
1163 GTK_MOVEMENT_DISPLAY_LINES, 1);
1165 add_move_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
1166 GTK_MOVEMENT_PARAGRAPHS, -1);
1168 add_move_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
1169 GTK_MOVEMENT_PARAGRAPHS, -1);
1171 add_move_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
1172 GTK_MOVEMENT_PARAGRAPHS, 1);
1174 add_move_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
1175 GTK_MOVEMENT_PARAGRAPHS, 1);
1177 add_move_binding (binding_set, GDK_KEY_Home, 0,
1178 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1180 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1181 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1183 add_move_binding (binding_set, GDK_KEY_End, 0,
1184 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1186 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1187 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1189 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1190 GTK_MOVEMENT_BUFFER_ENDS, -1);
1192 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1193 GTK_MOVEMENT_BUFFER_ENDS, -1);
1195 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1196 GTK_MOVEMENT_BUFFER_ENDS, 1);
1198 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1199 GTK_MOVEMENT_BUFFER_ENDS, 1);
1201 add_move_binding (binding_set, GDK_KEY_Page_Up, 0,
1202 GTK_MOVEMENT_PAGES, -1);
1204 add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
1205 GTK_MOVEMENT_PAGES, -1);
1207 add_move_binding (binding_set, GDK_KEY_Page_Down, 0,
1208 GTK_MOVEMENT_PAGES, 1);
1210 add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
1211 GTK_MOVEMENT_PAGES, 1);
1213 add_move_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
1214 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1216 add_move_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
1217 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1219 add_move_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
1220 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1222 add_move_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
1223 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1226 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1228 G_TYPE_BOOLEAN, TRUE);
1230 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1232 G_TYPE_BOOLEAN, TRUE);
1235 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1237 G_TYPE_BOOLEAN, FALSE);
1239 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1241 G_TYPE_BOOLEAN, FALSE);
1244 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, 0,
1245 "delete-from-cursor", 2,
1246 G_TYPE_ENUM, GTK_DELETE_CHARS,
1249 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, 0,
1250 "delete-from-cursor", 2,
1251 G_TYPE_ENUM, GTK_DELETE_CHARS,
1254 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, 0,
1257 /* Make this do the same as Backspace, to help with mis-typing */
1258 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_SHIFT_MASK,
1261 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_CONTROL_MASK,
1262 "delete-from-cursor", 2,
1263 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1266 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, GDK_CONTROL_MASK,
1267 "delete-from-cursor", 2,
1268 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1271 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_CONTROL_MASK,
1272 "delete-from-cursor", 2,
1273 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1276 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1277 "delete-from-cursor", 2,
1278 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1281 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1282 "delete-from-cursor", 2,
1283 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1286 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1287 "delete-from-cursor", 2,
1288 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1291 /* Cut/copy/paste */
1293 gtk_binding_entry_add_signal (binding_set, GDK_KEY_x, GDK_CONTROL_MASK,
1294 "cut-clipboard", 0);
1295 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1296 "copy-clipboard", 0);
1297 gtk_binding_entry_add_signal (binding_set, GDK_KEY_v, GDK_CONTROL_MASK,
1298 "paste-clipboard", 0);
1300 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_SHIFT_MASK,
1301 "cut-clipboard", 0);
1302 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_CONTROL_MASK,
1303 "copy-clipboard", 0);
1304 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_SHIFT_MASK,
1305 "paste-clipboard", 0);
1308 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, 0,
1309 "toggle-overwrite", 0);
1310 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Insert, 0,
1311 "toggle-overwrite", 0);
1314 gtk_binding_entry_add_signal (binding_set, GDK_KEY_F7, 0,
1315 "toggle-cursor-visible", 0);
1317 /* Control-tab focus motion */
1318 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, GDK_CONTROL_MASK,
1320 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1321 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, GDK_CONTROL_MASK,
1323 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1325 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1327 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1328 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1330 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1332 g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1336 gtk_text_view_init (GtkTextView *text_view)
1338 GtkWidget *widget = GTK_WIDGET (text_view);
1339 GtkTargetList *target_list;
1340 GtkTextViewPrivate *priv;
1342 text_view->priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
1343 priv = text_view->priv;
1345 gtk_widget_set_can_focus (widget, TRUE);
1347 /* Set up default style */
1348 priv->wrap_mode = GTK_WRAP_NONE;
1349 priv->pixels_above_lines = 0;
1350 priv->pixels_below_lines = 0;
1351 priv->pixels_inside_wrap = 0;
1352 priv->justify = GTK_JUSTIFY_LEFT;
1353 priv->left_margin = 0;
1354 priv->right_margin = 0;
1357 priv->editable = TRUE;
1359 priv->scroll_after_paste = TRUE;
1361 gtk_drag_dest_set (widget, 0, NULL, 0,
1362 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1364 target_list = gtk_target_list_new (NULL, 0);
1365 gtk_drag_dest_set_target_list (widget, target_list);
1366 gtk_target_list_unref (target_list);
1368 priv->virtual_cursor_x = -1;
1369 priv->virtual_cursor_y = -1;
1371 /* This object is completely private. No external entity can gain a reference
1372 * to it; so we create it here and destroy it in finalize ().
1374 priv->im_context = gtk_im_multicontext_new ();
1376 g_signal_connect (priv->im_context, "commit",
1377 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1378 g_signal_connect (priv->im_context, "preedit-changed",
1379 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1380 g_signal_connect (priv->im_context, "retrieve-surrounding",
1381 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1382 g_signal_connect (priv->im_context, "delete-surrounding",
1383 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1385 priv->cursor_visible = TRUE;
1387 priv->accepts_tab = TRUE;
1389 priv->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1392 priv->drag_start_x = -1;
1393 priv->drag_start_y = -1;
1395 priv->pending_place_cursor_button = 0;
1397 priv->min_display_width = -1;
1398 priv->min_display_height = -1;
1400 /* We handle all our own redrawing */
1401 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1405 * gtk_text_view_new:
1407 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1408 * before using the text view, an empty default buffer will be created
1409 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1410 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1412 * Return value: a new #GtkTextView
1415 gtk_text_view_new (void)
1417 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1421 * gtk_text_view_new_with_buffer:
1422 * @buffer: a #GtkTextBuffer
1424 * Creates a new #GtkTextView widget displaying the buffer
1425 * @buffer. One buffer can be shared among many widgets.
1426 * @buffer may be %NULL to create a default buffer, in which case
1427 * this function is equivalent to gtk_text_view_new(). The
1428 * text view adds its own reference count to the buffer; it does not
1429 * take over an existing reference.
1431 * Return value: a new #GtkTextView.
1434 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1436 GtkTextView *text_view;
1438 text_view = (GtkTextView*)gtk_text_view_new ();
1440 gtk_text_view_set_buffer (text_view, buffer);
1442 return GTK_WIDGET (text_view);
1446 * gtk_text_view_set_buffer:
1447 * @text_view: a #GtkTextView
1448 * @buffer: (allow-none): a #GtkTextBuffer
1450 * Sets @buffer as the buffer being displayed by @text_view. The previous
1451 * buffer displayed by the text view is unreferenced, and a reference is
1452 * added to @buffer. If you owned a reference to @buffer before passing it
1453 * to this function, you must remove that reference yourself; #GtkTextView
1454 * will not "adopt" it.
1457 gtk_text_view_set_buffer (GtkTextView *text_view,
1458 GtkTextBuffer *buffer)
1460 GtkTextViewPrivate *priv;
1462 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1463 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1465 priv = text_view->priv;
1467 if (priv->buffer == buffer)
1470 if (priv->buffer != NULL)
1472 /* Destroy all anchored children */
1476 copy = g_slist_copy (priv->children);
1478 while (tmp_list != NULL)
1480 GtkTextViewChild *vc = tmp_list->data;
1484 gtk_widget_destroy (vc->widget);
1485 /* vc may now be invalid! */
1488 tmp_list = g_slist_next (tmp_list);
1491 g_slist_free (copy);
1493 g_signal_handlers_disconnect_by_func (priv->buffer,
1494 gtk_text_view_mark_set_handler,
1496 g_signal_handlers_disconnect_by_func (priv->buffer,
1497 gtk_text_view_target_list_notify,
1499 g_signal_handlers_disconnect_by_func (priv->buffer,
1500 gtk_text_view_paste_done_handler,
1503 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
1505 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1506 GDK_SELECTION_PRIMARY);
1507 gtk_text_buffer_remove_selection_clipboard (priv->buffer, clipboard);
1511 gtk_text_layout_set_buffer (priv->layout, NULL);
1513 g_object_unref (priv->buffer);
1514 priv->dnd_mark = NULL;
1515 priv->first_para_mark = NULL;
1516 cancel_pending_scroll (text_view);
1519 priv->buffer = buffer;
1522 gtk_text_layout_set_buffer (priv->layout, buffer);
1528 g_object_ref (buffer);
1530 gtk_text_buffer_get_iter_at_offset (priv->buffer, &start, 0);
1532 priv->dnd_mark = gtk_text_buffer_create_mark (priv->buffer,
1536 priv->first_para_mark = gtk_text_buffer_create_mark (priv->buffer,
1540 priv->first_para_pixels = 0;
1543 g_signal_connect (priv->buffer, "mark-set",
1544 G_CALLBACK (gtk_text_view_mark_set_handler),
1546 g_signal_connect (priv->buffer, "notify::paste-target-list",
1547 G_CALLBACK (gtk_text_view_target_list_notify),
1549 g_signal_connect (priv->buffer, "paste-done",
1550 G_CALLBACK (gtk_text_view_paste_done_handler),
1553 gtk_text_view_target_list_notify (priv->buffer, NULL, text_view);
1555 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
1557 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1558 GDK_SELECTION_PRIMARY);
1559 gtk_text_buffer_add_selection_clipboard (priv->buffer, clipboard);
1563 g_object_notify (G_OBJECT (text_view), "buffer");
1565 if (gtk_widget_get_visible (GTK_WIDGET (text_view)))
1566 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1568 DV(g_print ("Invalidating due to set_buffer\n"));
1569 gtk_text_view_invalidate (text_view);
1572 static GtkTextBuffer*
1573 get_buffer (GtkTextView *text_view)
1575 if (text_view->priv->buffer == NULL)
1578 b = gtk_text_buffer_new (NULL);
1579 gtk_text_view_set_buffer (text_view, b);
1583 return text_view->priv->buffer;
1587 * gtk_text_view_get_buffer:
1588 * @text_view: a #GtkTextView
1590 * Returns the #GtkTextBuffer being displayed by this text view.
1591 * The reference count on the buffer is not incremented; the caller
1592 * of this function won't own a new reference.
1594 * Return value: (transfer none): a #GtkTextBuffer
1597 gtk_text_view_get_buffer (GtkTextView *text_view)
1599 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1601 return get_buffer (text_view);
1605 * gtk_text_view_get_iter_at_location:
1606 * @text_view: a #GtkTextView
1607 * @iter: (out): a #GtkTextIter
1608 * @x: x position, in buffer coordinates
1609 * @y: y position, in buffer coordinates
1611 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1612 * coordinates are coordinates for the entire buffer, not just the
1613 * currently-displayed portion. If you have coordinates from an
1614 * event, you have to convert those to buffer coordinates with
1615 * gtk_text_view_window_to_buffer_coords().
1618 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1623 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1624 g_return_if_fail (iter != NULL);
1626 gtk_text_view_ensure_layout (text_view);
1628 gtk_text_layout_get_iter_at_pixel (text_view->priv->layout,
1633 * gtk_text_view_get_iter_at_position:
1634 * @text_view: a #GtkTextView
1635 * @iter: (out): a #GtkTextIter
1636 * @trailing: (out) (allow-none): if non-%NULL, location to store an integer indicating where
1637 * in the grapheme the user clicked. It will either be
1638 * zero, or the number of characters in the grapheme.
1639 * 0 represents the trailing edge of the grapheme.
1640 * @x: x position, in buffer coordinates
1641 * @y: y position, in buffer coordinates
1643 * Retrieves the iterator pointing to the character at buffer
1644 * coordinates @x and @y. Buffer coordinates are coordinates for
1645 * the entire buffer, not just the currently-displayed portion.
1646 * If you have coordinates from an event, you have to convert
1647 * those to buffer coordinates with
1648 * gtk_text_view_window_to_buffer_coords().
1650 * Note that this is different from gtk_text_view_get_iter_at_location(),
1651 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1657 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1663 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1664 g_return_if_fail (iter != NULL);
1666 gtk_text_view_ensure_layout (text_view);
1668 gtk_text_layout_get_iter_at_position (text_view->priv->layout,
1669 iter, trailing, x, y);
1673 * gtk_text_view_get_iter_location:
1674 * @text_view: a #GtkTextView
1675 * @iter: a #GtkTextIter
1676 * @location: (out): bounds of the character at @iter
1678 * Gets a rectangle which roughly contains the character at @iter.
1679 * The rectangle position is in buffer coordinates; use
1680 * gtk_text_view_buffer_to_window_coords() to convert these
1681 * coordinates to coordinates for one of the windows in the text view.
1684 gtk_text_view_get_iter_location (GtkTextView *text_view,
1685 const GtkTextIter *iter,
1686 GdkRectangle *location)
1688 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1689 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1691 gtk_text_view_ensure_layout (text_view);
1693 gtk_text_layout_get_iter_location (text_view->priv->layout, iter, location);
1697 * gtk_text_view_get_line_yrange:
1698 * @text_view: a #GtkTextView
1699 * @iter: a #GtkTextIter
1700 * @y: (out): return location for a y coordinate
1701 * @height: (out): return location for a height
1703 * Gets the y coordinate of the top of the line containing @iter,
1704 * and the height of the line. The coordinate is a buffer coordinate;
1705 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1708 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1709 const GtkTextIter *iter,
1713 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1714 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1716 gtk_text_view_ensure_layout (text_view);
1718 gtk_text_layout_get_line_yrange (text_view->priv->layout,
1725 * gtk_text_view_get_line_at_y:
1726 * @text_view: a #GtkTextView
1727 * @target_iter: (out): a #GtkTextIter
1728 * @y: a y coordinate
1729 * @line_top: (out): return location for top coordinate of the line
1731 * Gets the #GtkTextIter at the start of the line containing
1732 * the coordinate @y. @y is in buffer coordinates, convert from
1733 * window coordinates with gtk_text_view_window_to_buffer_coords().
1734 * If non-%NULL, @line_top will be filled with the coordinate of the top
1738 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1739 GtkTextIter *target_iter,
1743 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1745 gtk_text_view_ensure_layout (text_view);
1747 gtk_text_layout_get_line_at_y (text_view->priv->layout,
1754 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1756 DV (g_print (" Setting adj to raw value %g\n", val));
1758 /* We don't really want to clamp to upper; we want to clamp to
1759 upper - page_size which is the highest value the scrollbar
1760 will let us reach. */
1761 if (val > (adj->upper - adj->page_size))
1762 val = adj->upper - adj->page_size;
1764 if (val < adj->lower)
1767 if (val != adj->value)
1769 DV (g_print (" Setting adj to clamped value %g\n", val));
1770 gtk_adjustment_set_value (adj, val);
1778 * gtk_text_view_scroll_to_iter:
1779 * @text_view: a #GtkTextView
1780 * @iter: a #GtkTextIter
1781 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1782 * @use_align: whether to use alignment arguments (if %FALSE,
1783 * just get the mark onscreen)
1784 * @xalign: horizontal alignment of mark within visible area
1785 * @yalign: vertical alignment of mark within visible area
1787 * Scrolls @text_view so that @iter is on the screen in the position
1788 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1789 * left or top, 1.0 indicates right or bottom, 0.5 means center.
1790 * If @use_align is %FALSE, the text scrolls the minimal distance to
1791 * get the mark onscreen, possibly not scrolling at all. The effective
1792 * screen for purposes of this function is reduced by a margin of size
1795 * Note that this function uses the currently-computed height of the
1796 * lines in the text buffer. Line heights are computed in an idle
1797 * handler; so this function may not have the desired effect if it's
1798 * called before the height computations. To avoid oddness, consider
1799 * using gtk_text_view_scroll_to_mark() which saves a point to be
1800 * scrolled to after line validation.
1802 * Return value: %TRUE if scrolling occurred
1805 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1807 gdouble within_margin,
1813 GdkRectangle screen;
1818 gboolean retval = FALSE;
1820 gint screen_xoffset, screen_yoffset;
1821 gint current_x_scroll, current_y_scroll;
1823 /* FIXME why don't we do the validate-at-scroll-destination thing
1824 * from flush_scroll in this function? I think it wasn't done before
1825 * because changed_handler was screwed up, but I could be wrong.
1828 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1829 g_return_val_if_fail (iter != NULL, FALSE);
1830 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1831 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1832 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1834 widget = GTK_WIDGET (text_view);
1836 DV(g_print(G_STRLOC"\n"));
1838 gtk_text_layout_get_iter_location (text_view->priv->layout,
1842 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1844 current_x_scroll = text_view->priv->xoffset;
1845 current_y_scroll = text_view->priv->yoffset;
1847 screen.x = current_x_scroll;
1848 screen.y = current_y_scroll;
1849 screen.width = SCREEN_WIDTH (widget);
1850 screen.height = SCREEN_HEIGHT (widget);
1852 screen_xoffset = screen.width * within_margin;
1853 screen_yoffset = screen.height * within_margin;
1855 screen.x += screen_xoffset;
1856 screen.y += screen_yoffset;
1857 screen.width -= screen_xoffset * 2;
1858 screen.height -= screen_yoffset * 2;
1860 /* paranoia check */
1861 if (screen.width < 1)
1863 if (screen.height < 1)
1866 /* The -1 here ensures that we leave enough space to draw the cursor
1867 * when this function is used for horizontal scrolling.
1869 screen_right = screen.x + screen.width - 1;
1870 screen_bottom = screen.y + screen.height;
1872 /* The alignment affects the point in the target character that we
1873 * choose to align. If we're doing right/bottom alignment, we align
1874 * the right/bottom edge of the character the mark is at; if we're
1875 * doing left/top we align the left/top edge of the character; if
1876 * we're doing center alignment we align the center of the
1880 /* Vertical scroll */
1883 scroll_dest = current_y_scroll;
1887 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1889 /* if scroll_dest < screen.y, we move a negative increment (up),
1890 * else a positive increment (down)
1892 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1896 /* move minimum to get onscreen */
1897 if (rect.y < screen.y)
1899 scroll_dest = rect.y;
1900 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1902 else if ((rect.y + rect.height) > screen_bottom)
1904 scroll_dest = rect.y + rect.height;
1905 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1909 if (scroll_inc != 0)
1911 retval = set_adjustment_clamped (text_view->priv->vadjustment,
1912 current_y_scroll + scroll_inc);
1914 DV (g_print (" vert increment %d\n", scroll_inc));
1917 /* Horizontal scroll */
1920 scroll_dest = current_x_scroll;
1924 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1926 /* if scroll_dest < screen.y, we move a negative increment (left),
1927 * else a positive increment (right)
1929 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1933 /* move minimum to get onscreen */
1934 if (rect.x < screen.x)
1936 scroll_dest = rect.x;
1937 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1939 else if ((rect.x + rect.width) > screen_right)
1941 scroll_dest = rect.x + rect.width;
1942 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1946 if (scroll_inc != 0)
1948 retval = set_adjustment_clamped (text_view->priv->hadjustment,
1949 current_x_scroll + scroll_inc);
1951 DV (g_print (" horiz increment %d\n", scroll_inc));
1956 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1960 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1967 free_pending_scroll (GtkTextPendingScroll *scroll)
1969 if (!gtk_text_mark_get_deleted (scroll->mark))
1970 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1972 g_object_unref (scroll->mark);
1977 cancel_pending_scroll (GtkTextView *text_view)
1979 if (text_view->priv->pending_scroll)
1981 free_pending_scroll (text_view->priv->pending_scroll);
1982 text_view->priv->pending_scroll = NULL;
1987 gtk_text_view_queue_scroll (GtkTextView *text_view,
1989 gdouble within_margin,
1995 GtkTextPendingScroll *scroll;
1997 DV(g_print(G_STRLOC"\n"));
1999 scroll = g_new (GtkTextPendingScroll, 1);
2001 scroll->within_margin = within_margin;
2002 scroll->use_align = use_align;
2003 scroll->xalign = xalign;
2004 scroll->yalign = yalign;
2006 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2008 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
2011 gtk_text_mark_get_left_gravity (mark));
2013 g_object_ref (scroll->mark);
2015 cancel_pending_scroll (text_view);
2017 text_view->priv->pending_scroll = scroll;
2021 gtk_text_view_flush_scroll (GtkTextView *text_view)
2023 GtkAllocation allocation;
2025 GtkTextPendingScroll *scroll;
2029 widget = GTK_WIDGET (text_view);
2031 DV(g_print(G_STRLOC"\n"));
2033 if (text_view->priv->pending_scroll == NULL)
2035 DV (g_print ("in flush scroll, no pending scroll\n"));
2039 scroll = text_view->priv->pending_scroll;
2041 /* avoid recursion */
2042 text_view->priv->pending_scroll = NULL;
2044 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
2046 /* Validate area around the scroll destination, so the adjustment
2047 * can meaningfully point into that area. We must validate
2048 * enough area to be sure that after we scroll, everything onscreen
2049 * is valid; otherwise, validation will maintain the first para
2050 * in one place, but may push the target iter off the bottom of
2053 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
2054 gtk_widget_get_allocation (widget, &allocation);
2055 gtk_text_layout_validate_yrange (text_view->priv->layout, &iter,
2056 -(allocation.height * 2),
2057 allocation.height * 2);
2059 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
2061 /* Ensure we have updated width/height */
2062 gtk_text_view_update_adjustments (text_view);
2064 retval = gtk_text_view_scroll_to_iter (text_view,
2066 scroll->within_margin,
2071 free_pending_scroll (scroll);
2077 gtk_text_view_update_adjustments (GtkTextView *text_view)
2079 GtkTextViewPrivate *priv;
2080 gint width = 0, height = 0;
2082 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
2084 priv = text_view->priv;
2087 gtk_text_layout_get_size (priv->layout, &width, &height);
2089 /* Make room for the cursor after the last character in the widest line */
2090 width += SPACE_FOR_CURSOR;
2092 if (priv->width != width || priv->height != height)
2094 if (priv->width != width)
2095 priv->width_changed = TRUE;
2097 priv->width = width;
2098 priv->height = height;
2100 gtk_text_view_set_hadjustment_values (text_view);
2101 gtk_text_view_set_vadjustment_values (text_view);
2106 gtk_text_view_update_layout_width (GtkTextView *text_view)
2108 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
2110 gtk_text_view_ensure_layout (text_view);
2112 gtk_text_layout_set_screen_width (text_view->priv->layout,
2113 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
2117 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
2121 if (text_view->priv->layout == NULL)
2124 gtk_text_view_get_cursor_location (text_view, &area);
2126 area.x -= text_view->priv->xoffset;
2127 area.y -= text_view->priv->yoffset;
2129 /* Width returned by Pango indicates direction of cursor,
2130 * by it's sign more than the size of cursor.
2134 gtk_im_context_set_cursor_location (text_view->priv->im_context, &area);
2138 do_update_im_spot_location (gpointer text_view)
2140 GtkTextViewPrivate *priv;
2142 priv = GTK_TEXT_VIEW (text_view)->priv;
2143 priv->im_spot_idle = 0;
2145 gtk_text_view_update_im_spot_location (text_view);
2150 queue_update_im_spot_location (GtkTextView *text_view)
2152 GtkTextViewPrivate *priv;
2154 priv = text_view->priv;
2156 /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
2157 * so we don't wait until the entire buffer has been validated. */
2158 if (!priv->im_spot_idle)
2159 priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
2160 do_update_im_spot_location,
2166 flush_update_im_spot_location (GtkTextView *text_view)
2168 GtkTextViewPrivate *priv;
2170 priv = text_view->priv;
2172 if (priv->im_spot_idle)
2174 g_source_remove (priv->im_spot_idle);
2175 priv->im_spot_idle = 0;
2176 gtk_text_view_update_im_spot_location (text_view);
2181 * gtk_text_view_scroll_to_mark:
2182 * @text_view: a #GtkTextView
2183 * @mark: a #GtkTextMark
2184 * @within_margin: margin as a [0.0,0.5) fraction of screen size
2185 * @use_align: whether to use alignment arguments (if %FALSE, just
2186 * get the mark onscreen)
2187 * @xalign: horizontal alignment of mark within visible area
2188 * @yalign: vertical alignment of mark within visible area
2190 * Scrolls @text_view so that @mark is on the screen in the position
2191 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
2192 * left or top, 1.0 indicates right or bottom, 0.5 means center.
2193 * If @use_align is %FALSE, the text scrolls the minimal distance to
2194 * get the mark onscreen, possibly not scrolling at all. The effective
2195 * screen for purposes of this function is reduced by a margin of size
2199 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
2201 gdouble within_margin,
2206 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2207 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2208 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
2209 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
2210 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
2212 /* We need to verify that the buffer contains the mark, otherwise this
2213 * can lead to data structure corruption later on.
2215 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2217 gtk_text_view_queue_scroll (text_view, mark,
2223 /* If no validation is pending, we need to go ahead and force an
2226 if (text_view->priv->layout &&
2227 gtk_text_layout_is_valid (text_view->priv->layout))
2228 gtk_text_view_flush_scroll (text_view);
2232 * gtk_text_view_scroll_mark_onscreen:
2233 * @text_view: a #GtkTextView
2234 * @mark: a mark in the buffer for @text_view
2236 * Scrolls @text_view the minimum distance such that @mark is contained
2237 * within the visible area of the widget.
2240 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
2243 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2244 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2246 /* We need to verify that the buffer contains the mark, otherwise this
2247 * can lead to data structure corruption later on.
2249 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2251 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
2255 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
2257 GdkRectangle visible_rect;
2258 gtk_text_view_get_visible_rect (text_view, &visible_rect);
2260 return gtk_text_layout_clamp_iter_to_vrange (text_view->priv->layout, iter,
2262 visible_rect.y + visible_rect.height);
2266 * gtk_text_view_move_mark_onscreen:
2267 * @text_view: a #GtkTextView
2268 * @mark: a #GtkTextMark
2270 * Moves a mark within the buffer so that it's
2271 * located within the currently-visible text area.
2273 * Return value: %TRUE if the mark moved (wasn't already onscreen)
2276 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
2281 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2282 g_return_val_if_fail (mark != NULL, FALSE);
2284 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2286 if (clamp_iter_onscreen (text_view, &iter))
2288 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
2296 * gtk_text_view_get_visible_rect:
2297 * @text_view: a #GtkTextView
2298 * @visible_rect: rectangle to fill
2300 * Fills @visible_rect with the currently-visible
2301 * region of the buffer, in buffer coordinates. Convert to window coordinates
2302 * with gtk_text_view_buffer_to_window_coords().
2305 gtk_text_view_get_visible_rect (GtkTextView *text_view,
2306 GdkRectangle *visible_rect)
2310 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2312 widget = GTK_WIDGET (text_view);
2316 visible_rect->x = text_view->priv->xoffset;
2317 visible_rect->y = text_view->priv->yoffset;
2318 visible_rect->width = SCREEN_WIDTH (widget);
2319 visible_rect->height = SCREEN_HEIGHT (widget);
2321 DV(g_print(" visible rect: %d,%d %d x %d\n",
2324 visible_rect->width,
2325 visible_rect->height));
2330 * gtk_text_view_set_wrap_mode:
2331 * @text_view: a #GtkTextView
2332 * @wrap_mode: a #GtkWrapMode
2334 * Sets the line wrapping for the view.
2337 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
2338 GtkWrapMode wrap_mode)
2340 GtkTextViewPrivate *priv;
2342 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2344 priv = text_view->priv;
2346 if (priv->wrap_mode != wrap_mode)
2348 priv->wrap_mode = wrap_mode;
2352 priv->layout->default_style->wrap_mode = wrap_mode;
2353 gtk_text_layout_default_style_changed (priv->layout);
2357 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2361 * gtk_text_view_get_wrap_mode:
2362 * @text_view: a #GtkTextView
2364 * Gets the line wrapping for the view.
2366 * Return value: the line wrap setting
2369 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2371 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2373 return text_view->priv->wrap_mode;
2377 * gtk_text_view_set_editable:
2378 * @text_view: a #GtkTextView
2379 * @setting: whether it's editable
2381 * Sets the default editability of the #GtkTextView. You can override
2382 * this default setting with tags in the buffer, using the "editable"
2383 * attribute of tags.
2386 gtk_text_view_set_editable (GtkTextView *text_view,
2389 GtkTextViewPrivate *priv;
2391 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2393 priv = text_view->priv;
2394 setting = setting != FALSE;
2396 if (priv->editable != setting)
2400 gtk_text_view_reset_im_context(text_view);
2401 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2402 gtk_im_context_focus_out (priv->im_context);
2405 priv->editable = setting;
2407 if (setting && gtk_widget_has_focus (GTK_WIDGET (text_view)))
2408 gtk_im_context_focus_in (priv->im_context);
2412 gtk_text_layout_set_overwrite_mode (priv->layout,
2413 priv->overwrite_mode && priv->editable);
2414 priv->layout->default_style->editable = priv->editable;
2415 gtk_text_layout_default_style_changed (priv->layout);
2418 g_object_notify (G_OBJECT (text_view), "editable");
2423 * gtk_text_view_get_editable:
2424 * @text_view: a #GtkTextView
2426 * Returns the default editability of the #GtkTextView. Tags in the
2427 * buffer may override this setting for some ranges of text.
2429 * Return value: whether text is editable by default
2432 gtk_text_view_get_editable (GtkTextView *text_view)
2434 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2436 return text_view->priv->editable;
2440 * gtk_text_view_set_pixels_above_lines:
2441 * @text_view: a #GtkTextView
2442 * @pixels_above_lines: pixels above paragraphs
2444 * Sets the default number of blank pixels above paragraphs in @text_view.
2445 * Tags in the buffer for @text_view may override the defaults.
2448 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2449 gint pixels_above_lines)
2451 GtkTextViewPrivate *priv;
2453 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2455 priv = text_view->priv;
2457 if (priv->pixels_above_lines != pixels_above_lines)
2459 priv->pixels_above_lines = pixels_above_lines;
2463 priv->layout->default_style->pixels_above_lines = pixels_above_lines;
2464 gtk_text_layout_default_style_changed (priv->layout);
2467 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2472 * gtk_text_view_get_pixels_above_lines:
2473 * @text_view: a #GtkTextView
2475 * Gets the default number of pixels to put above paragraphs.
2477 * Return value: default number of pixels above paragraphs
2480 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2482 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2484 return text_view->priv->pixels_above_lines;
2488 * gtk_text_view_set_pixels_below_lines:
2489 * @text_view: a #GtkTextView
2490 * @pixels_below_lines: pixels below paragraphs
2492 * Sets the default number of pixels of blank space
2493 * to put below paragraphs in @text_view. May be overridden
2494 * by tags applied to @text_view's buffer.
2497 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2498 gint pixels_below_lines)
2500 GtkTextViewPrivate *priv;
2502 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2504 priv = text_view->priv;
2506 if (priv->pixels_below_lines != pixels_below_lines)
2508 priv->pixels_below_lines = pixels_below_lines;
2512 priv->layout->default_style->pixels_below_lines = pixels_below_lines;
2513 gtk_text_layout_default_style_changed (priv->layout);
2516 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2521 * gtk_text_view_get_pixels_below_lines:
2522 * @text_view: a #GtkTextView
2524 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2526 * Return value: default number of blank pixels below paragraphs
2529 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2531 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2533 return text_view->priv->pixels_below_lines;
2537 * gtk_text_view_set_pixels_inside_wrap:
2538 * @text_view: a #GtkTextView
2539 * @pixels_inside_wrap: default number of pixels between wrapped lines
2541 * Sets the default number of pixels of blank space to leave between
2542 * display/wrapped lines within a paragraph. May be overridden by
2543 * tags in @text_view's buffer.
2546 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2547 gint pixels_inside_wrap)
2549 GtkTextViewPrivate *priv;
2551 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2553 priv = text_view->priv;
2555 if (priv->pixels_inside_wrap != pixels_inside_wrap)
2557 priv->pixels_inside_wrap = pixels_inside_wrap;
2561 priv->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2562 gtk_text_layout_default_style_changed (priv->layout);
2565 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2570 * gtk_text_view_get_pixels_inside_wrap:
2571 * @text_view: a #GtkTextView
2573 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2575 * Return value: default number of pixels of blank space between wrapped lines
2578 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2580 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2582 return text_view->priv->pixels_inside_wrap;
2586 * gtk_text_view_set_justification:
2587 * @text_view: a #GtkTextView
2588 * @justification: justification
2590 * Sets the default justification of text in @text_view.
2591 * Tags in the view's buffer may override the default.
2595 gtk_text_view_set_justification (GtkTextView *text_view,
2596 GtkJustification justification)
2598 GtkTextViewPrivate *priv;
2600 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2602 priv = text_view->priv;
2604 if (priv->justify != justification)
2606 priv->justify = justification;
2610 priv->layout->default_style->justification = justification;
2611 gtk_text_layout_default_style_changed (priv->layout);
2614 g_object_notify (G_OBJECT (text_view), "justification");
2619 * gtk_text_view_get_justification:
2620 * @text_view: a #GtkTextView
2622 * Gets the default justification of paragraphs in @text_view.
2623 * Tags in the buffer may override the default.
2625 * Return value: default justification
2628 gtk_text_view_get_justification (GtkTextView *text_view)
2630 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2632 return text_view->priv->justify;
2636 * gtk_text_view_set_left_margin:
2637 * @text_view: a #GtkTextView
2638 * @left_margin: left margin in pixels
2640 * Sets the default left margin for text in @text_view.
2641 * Tags in the buffer may override the default.
2644 gtk_text_view_set_left_margin (GtkTextView *text_view,
2647 GtkTextViewPrivate *priv;
2649 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2651 priv = text_view->priv;
2653 if (priv->left_margin != left_margin)
2655 priv->left_margin = left_margin;
2659 priv->layout->default_style->left_margin = left_margin;
2660 gtk_text_layout_default_style_changed (priv->layout);
2663 g_object_notify (G_OBJECT (text_view), "left-margin");
2668 * gtk_text_view_get_left_margin:
2669 * @text_view: a #GtkTextView
2671 * Gets the default left margin size of paragraphs in the @text_view.
2672 * Tags in the buffer may override the default.
2674 * Return value: left margin in pixels
2677 gtk_text_view_get_left_margin (GtkTextView *text_view)
2679 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2681 return text_view->priv->left_margin;
2685 * gtk_text_view_set_right_margin:
2686 * @text_view: a #GtkTextView
2687 * @right_margin: right margin in pixels
2689 * Sets the default right margin for text in the text view.
2690 * Tags in the buffer may override the default.
2693 gtk_text_view_set_right_margin (GtkTextView *text_view,
2696 GtkTextViewPrivate *priv = text_view->priv;
2698 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2700 if (priv->right_margin != right_margin)
2702 priv->right_margin = right_margin;
2706 priv->layout->default_style->right_margin = right_margin;
2707 gtk_text_layout_default_style_changed (priv->layout);
2710 g_object_notify (G_OBJECT (text_view), "right-margin");
2715 * gtk_text_view_get_right_margin:
2716 * @text_view: a #GtkTextView
2718 * Gets the default right margin for text in @text_view. Tags
2719 * in the buffer may override the default.
2721 * Return value: right margin in pixels
2724 gtk_text_view_get_right_margin (GtkTextView *text_view)
2726 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2728 return text_view->priv->right_margin;
2732 * gtk_text_view_set_indent:
2733 * @text_view: a #GtkTextView
2734 * @indent: indentation in pixels
2736 * Sets the default indentation for paragraphs in @text_view.
2737 * Tags in the buffer may override the default.
2740 gtk_text_view_set_indent (GtkTextView *text_view,
2743 GtkTextViewPrivate *priv;
2745 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2747 priv = text_view->priv;
2749 if (priv->indent != indent)
2751 priv->indent = indent;
2755 priv->layout->default_style->indent = indent;
2756 gtk_text_layout_default_style_changed (priv->layout);
2759 g_object_notify (G_OBJECT (text_view), "indent");
2764 * gtk_text_view_get_indent:
2765 * @text_view: a #GtkTextView
2767 * Gets the default indentation of paragraphs in @text_view.
2768 * Tags in the view's buffer may override the default.
2769 * The indentation may be negative.
2771 * Return value: number of pixels of indentation
2774 gtk_text_view_get_indent (GtkTextView *text_view)
2776 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2778 return text_view->priv->indent;
2782 * gtk_text_view_set_tabs:
2783 * @text_view: a #GtkTextView
2784 * @tabs: tabs as a #PangoTabArray
2786 * Sets the default tab stops for paragraphs in @text_view.
2787 * Tags in the buffer may override the default.
2790 gtk_text_view_set_tabs (GtkTextView *text_view,
2791 PangoTabArray *tabs)
2793 GtkTextViewPrivate *priv;
2795 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2797 priv = text_view->priv;
2800 pango_tab_array_free (priv->tabs);
2802 priv->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2806 /* some unkosher futzing in internal struct details... */
2807 if (priv->layout->default_style->tabs)
2808 pango_tab_array_free (priv->layout->default_style->tabs);
2810 priv->layout->default_style->tabs =
2811 priv->tabs ? pango_tab_array_copy (priv->tabs) : NULL;
2813 gtk_text_layout_default_style_changed (priv->layout);
2816 g_object_notify (G_OBJECT (text_view), "tabs");
2820 * gtk_text_view_get_tabs:
2821 * @text_view: a #GtkTextView
2823 * Gets the default tabs for @text_view. Tags in the buffer may
2824 * override the defaults. The returned array will be %NULL if
2825 * "standard" (8-space) tabs are used. Free the return value
2826 * with pango_tab_array_free().
2828 * Return value: copy of default tab array, or %NULL if "standard"
2829 * tabs are used; must be freed with pango_tab_array_free().
2832 gtk_text_view_get_tabs (GtkTextView *text_view)
2834 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2836 return text_view->priv->tabs ? pango_tab_array_copy (text_view->priv->tabs) : NULL;
2840 gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
2842 gtk_text_view_set_cursor_visible (text_view, !text_view->priv->cursor_visible);
2846 * gtk_text_view_set_cursor_visible:
2847 * @text_view: a #GtkTextView
2848 * @setting: whether to show the insertion cursor
2850 * Toggles whether the insertion point is displayed. A buffer with no editable
2851 * text probably shouldn't have a visible cursor, so you may want to turn
2855 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2858 GtkTextViewPrivate *priv;
2860 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2862 priv = text_view->priv;
2863 setting = (setting != FALSE);
2865 if (priv->cursor_visible != setting)
2867 priv->cursor_visible = setting;
2869 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2873 gtk_text_layout_set_cursor_visible (priv->layout, setting);
2874 gtk_text_view_check_cursor_blink (text_view);
2878 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2883 * gtk_text_view_get_cursor_visible:
2884 * @text_view: a #GtkTextView
2886 * Find out whether the cursor is being displayed.
2888 * Return value: whether the insertion mark is visible
2891 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2893 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2895 return text_view->priv->cursor_visible;
2900 * gtk_text_view_place_cursor_onscreen:
2901 * @text_view: a #GtkTextView
2903 * Moves the cursor to the currently visible region of the
2904 * buffer, it it isn't there already.
2906 * Return value: %TRUE if the cursor had to be moved.
2909 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2913 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2915 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2916 gtk_text_buffer_get_insert (get_buffer (text_view)));
2918 if (clamp_iter_onscreen (text_view, &insert))
2920 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2928 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2930 GtkTextViewPrivate *priv = text_view->priv;
2932 if (priv->first_validate_idle != 0)
2934 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2935 g_source_remove (priv->first_validate_idle);
2936 priv->first_validate_idle = 0;
2939 if (priv->incremental_validate_idle != 0)
2941 g_source_remove (priv->incremental_validate_idle);
2942 priv->incremental_validate_idle = 0;
2947 gtk_text_view_destroy (GtkWidget *widget)
2949 GtkTextView *text_view;
2950 GtkTextViewPrivate *priv;
2952 text_view = GTK_TEXT_VIEW (widget);
2953 priv = text_view->priv;
2955 gtk_text_view_remove_validate_idles (text_view);
2956 gtk_text_view_set_buffer (text_view, NULL);
2957 gtk_text_view_destroy_layout (text_view);
2959 if (text_view->priv->scroll_timeout)
2961 g_source_remove (text_view->priv->scroll_timeout);
2962 text_view->priv->scroll_timeout = 0;
2965 if (priv->im_spot_idle)
2967 g_source_remove (priv->im_spot_idle);
2968 priv->im_spot_idle = 0;
2971 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->destroy (widget);
2975 gtk_text_view_finalize (GObject *object)
2977 GtkTextView *text_view;
2978 GtkTextViewPrivate *priv;
2980 text_view = GTK_TEXT_VIEW (object);
2981 priv = text_view->priv;
2983 g_assert (priv->buffer == NULL);
2985 gtk_text_view_destroy_layout (text_view);
2986 gtk_text_view_set_buffer (text_view, NULL);
2988 cancel_pending_scroll (text_view);
2991 pango_tab_array_free (priv->tabs);
2993 if (priv->hadjustment)
2994 g_object_unref (priv->hadjustment);
2995 if (priv->vadjustment)
2996 g_object_unref (priv->vadjustment);
2998 text_window_free (priv->text_window);
3000 if (priv->left_window)
3001 text_window_free (priv->left_window);
3003 if (priv->top_window)
3004 text_window_free (priv->top_window);
3006 if (priv->right_window)
3007 text_window_free (priv->right_window);
3009 if (priv->bottom_window)
3010 text_window_free (priv->bottom_window);
3012 g_object_unref (priv->im_context);
3014 g_free (priv->im_module);
3016 G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize (object);
3020 gtk_text_view_set_property (GObject *object,
3022 const GValue *value,
3025 GtkTextView *text_view;
3026 GtkTextViewPrivate *priv;
3028 text_view = GTK_TEXT_VIEW (object);
3029 priv = text_view->priv;
3033 case PROP_PIXELS_ABOVE_LINES:
3034 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
3037 case PROP_PIXELS_BELOW_LINES:
3038 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
3041 case PROP_PIXELS_INSIDE_WRAP:
3042 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
3046 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
3049 case PROP_WRAP_MODE:
3050 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
3053 case PROP_JUSTIFICATION:
3054 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
3057 case PROP_LEFT_MARGIN:
3058 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
3061 case PROP_RIGHT_MARGIN:
3062 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
3066 gtk_text_view_set_indent (text_view, g_value_get_int (value));
3070 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
3073 case PROP_CURSOR_VISIBLE:
3074 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
3077 case PROP_OVERWRITE:
3078 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
3082 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
3085 case PROP_ACCEPTS_TAB:
3086 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
3089 case PROP_IM_MODULE:
3090 g_free (priv->im_module);
3091 priv->im_module = g_value_dup_string (value);
3092 if (GTK_IS_IM_MULTICONTEXT (priv->im_context))
3093 gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (priv->im_context), priv->im_module);
3096 case PROP_HADJUSTMENT:
3097 gtk_text_view_set_hadjustment (text_view, g_value_get_object (value));
3100 case PROP_VADJUSTMENT:
3101 gtk_text_view_set_vadjustment (text_view, g_value_get_object (value));
3104 case PROP_MIN_DISPLAY_WIDTH:
3105 priv->min_display_width = g_value_get_int (value);
3108 case PROP_MIN_DISPLAY_HEIGHT:
3109 priv->min_display_height = g_value_get_int (value);
3113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3119 gtk_text_view_get_property (GObject *object,
3124 GtkTextView *text_view;
3125 GtkTextViewPrivate *priv;
3127 text_view = GTK_TEXT_VIEW (object);
3128 priv = text_view->priv;
3132 case PROP_PIXELS_ABOVE_LINES:
3133 g_value_set_int (value, priv->pixels_above_lines);
3136 case PROP_PIXELS_BELOW_LINES:
3137 g_value_set_int (value, priv->pixels_below_lines);
3140 case PROP_PIXELS_INSIDE_WRAP:
3141 g_value_set_int (value, priv->pixels_inside_wrap);
3145 g_value_set_boolean (value, priv->editable);
3148 case PROP_WRAP_MODE:
3149 g_value_set_enum (value, priv->wrap_mode);
3152 case PROP_JUSTIFICATION:
3153 g_value_set_enum (value, priv->justify);
3156 case PROP_LEFT_MARGIN:
3157 g_value_set_int (value, priv->left_margin);
3160 case PROP_RIGHT_MARGIN:
3161 g_value_set_int (value, priv->right_margin);
3165 g_value_set_int (value, priv->indent);
3169 g_value_set_boxed (value, priv->tabs);
3172 case PROP_CURSOR_VISIBLE:
3173 g_value_set_boolean (value, priv->cursor_visible);
3177 g_value_set_object (value, get_buffer (text_view));
3180 case PROP_OVERWRITE:
3181 g_value_set_boolean (value, priv->overwrite_mode);
3184 case PROP_ACCEPTS_TAB:
3185 g_value_set_boolean (value, priv->accepts_tab);
3188 case PROP_IM_MODULE:
3189 g_value_set_string (value, priv->im_module);
3192 case PROP_HADJUSTMENT:
3193 g_value_set_object (value, priv->hadjustment);
3196 case PROP_VADJUSTMENT:
3197 g_value_set_object (value, priv->vadjustment);
3200 case PROP_MIN_DISPLAY_WIDTH:
3201 g_value_set_int (value, priv->min_display_width);
3204 case PROP_MIN_DISPLAY_HEIGHT:
3205 g_value_set_int (value, priv->min_display_height);
3209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3215 gtk_text_view_size_request (GtkWidget *widget,
3216 GtkRequisition *requisition)
3218 GtkTextView *text_view;
3219 GtkTextViewPrivate *priv;
3221 gint focus_edge_width;
3224 gboolean interior_focus;
3226 text_view = GTK_TEXT_VIEW (widget);
3227 priv = text_view->priv;
3229 gtk_widget_style_get (widget,
3230 "interior-focus", &interior_focus,
3231 "focus-line-width", &focus_width,
3235 focus_edge_width = 0;
3237 focus_edge_width = focus_width;
3241 priv->text_window->requisition.width = priv->layout->width;
3242 priv->text_window->requisition.height = priv->layout->height;
3246 priv->text_window->requisition.width = 0;
3247 priv->text_window->requisition.height = 0;
3250 requisition->width = priv->text_window->requisition.width + focus_edge_width * 2;
3251 requisition->height = priv->text_window->requisition.height + focus_edge_width * 2;
3253 if (priv->left_window)
3254 requisition->width += priv->left_window->requisition.width;
3256 if (priv->right_window)
3257 requisition->width += priv->right_window->requisition.width;
3259 if (priv->top_window)
3260 requisition->height += priv->top_window->requisition.height;
3262 if (priv->bottom_window)
3263 requisition->height += priv->bottom_window->requisition.height;
3265 border_width = gtk_container_get_border_width (GTK_CONTAINER (text_view));
3266 requisition->width += border_width * 2;
3267 requisition->height += border_width * 2;
3269 tmp_list = priv->children;
3270 while (tmp_list != NULL)
3272 GtkTextViewChild *child = tmp_list->data;
3276 GtkRequisition child_req;
3277 GtkRequisition old_req;
3279 gtk_widget_get_preferred_size (child->widget, &old_req, NULL);
3281 gtk_widget_get_preferred_size (child->widget, &child_req, NULL);
3283 /* Invalidate layout lines if required */
3285 (old_req.width != child_req.width ||
3286 old_req.height != child_req.height))
3287 gtk_text_child_anchor_queue_resize (child->anchor,
3292 GtkRequisition child_req;
3294 gtk_widget_get_preferred_size (child->widget,
3298 tmp_list = g_slist_next (tmp_list);
3301 /* Cache the requested size of the text view so we can
3302 * compare it in the changed_handler() */
3303 priv->cached_size_request = *requisition;
3307 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
3308 GtkTextViewChild *vc,
3309 GtkAllocation *allocation)
3315 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3319 gtk_text_layout_get_line_yrange (text_view->priv->layout, &iter,
3322 buffer_y += vc->from_top_of_line;
3324 allocation->x = vc->from_left_of_buffer - text_view->priv->xoffset;
3325 allocation->y = buffer_y - text_view->priv->yoffset;
3327 gtk_widget_get_preferred_size (vc->widget, &req, NULL);
3328 allocation->width = req.width;
3329 allocation->height = req.height;
3333 gtk_text_view_update_child_allocation (GtkTextView *text_view,
3334 GtkTextViewChild *vc)
3336 GtkAllocation allocation;
3338 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
3340 gtk_widget_size_allocate (vc->widget, &allocation);
3343 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
3345 vc->widget->allocation.x,
3346 vc->widget->allocation.y,
3347 text_view->priv->yoffset);
3352 gtk_text_view_child_allocated (GtkTextLayout *layout,
3358 GtkTextViewChild *vc = NULL;
3359 GtkTextView *text_view = data;
3361 /* x,y is the position of the child from the top of the line, and
3362 * from the left of the buffer. We have to translate that into text
3363 * window coordinates, then size_allocate the child.
3366 vc = g_object_get_data (G_OBJECT (child),
3367 "gtk-text-view-child");
3369 g_assert (vc != NULL);
3371 DV (g_print ("child allocated at %d,%d\n", x, y));
3373 vc->from_left_of_buffer = x;
3374 vc->from_top_of_line = y;
3376 gtk_text_view_update_child_allocation (text_view, vc);
3380 gtk_text_view_allocate_children (GtkTextView *text_view)
3384 DV(g_print(G_STRLOC"\n"));
3386 tmp_list = text_view->priv->children;
3387 while (tmp_list != NULL)
3389 GtkTextViewChild *child = tmp_list->data;
3391 g_assert (child != NULL);
3395 /* We need to force-validate the regions containing
3398 GtkTextIter child_loc;
3399 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3403 /* Since anchored children are only ever allocated from
3404 * gtk_text_layout_get_line_display() we have to make sure
3405 * that the display line caching in the layout doesn't
3406 * get in the way. Invalidating the layout around the anchor
3409 if (_gtk_widget_get_alloc_needed (child->widget))
3411 GtkTextIter end = child_loc;
3412 gtk_text_iter_forward_char (&end);
3413 gtk_text_layout_invalidate (text_view->priv->layout, &child_loc, &end);
3416 gtk_text_layout_validate_yrange (text_view->priv->layout,
3422 GtkAllocation allocation;
3423 GtkRequisition child_req;
3425 allocation.x = child->x;
3426 allocation.y = child->y;
3428 gtk_widget_get_preferred_size (child->widget, &child_req, NULL);
3430 allocation.width = child_req.width;
3431 allocation.height = child_req.height;
3433 gtk_widget_size_allocate (child->widget, &allocation);
3436 tmp_list = g_slist_next (tmp_list);
3441 gtk_text_view_size_allocate (GtkWidget *widget,
3442 GtkAllocation *allocation)
3444 GtkAllocation widget_allocation;
3445 GtkTextView *text_view;
3446 GtkTextViewPrivate *priv;
3448 GdkRectangle text_rect;
3449 GdkRectangle left_rect;
3450 GdkRectangle right_rect;
3451 GdkRectangle top_rect;
3452 GdkRectangle bottom_rect;
3453 gint focus_edge_width;
3456 gboolean interior_focus;
3457 gboolean size_changed;
3459 text_view = GTK_TEXT_VIEW (widget);
3460 priv = text_view->priv;
3462 DV(g_print(G_STRLOC"\n"));
3464 gtk_widget_get_allocation (widget, &widget_allocation);
3466 widget_allocation.width != allocation->width ||
3467 widget_allocation.height != allocation->height;
3469 border_width = gtk_container_get_border_width (GTK_CONTAINER (text_view));
3471 gtk_widget_set_allocation (widget, allocation);
3473 if (gtk_widget_get_realized (widget))
3475 gdk_window_move_resize (gtk_widget_get_window (widget),
3476 allocation->x, allocation->y,
3477 allocation->width, allocation->height);
3480 /* distribute width/height among child windows. Ensure all
3481 * windows get at least a 1x1 allocation.
3484 gtk_widget_style_get (widget,
3485 "interior-focus", &interior_focus,
3486 "focus-line-width", &focus_width,
3490 focus_edge_width = 0;
3492 focus_edge_width = focus_width;
3494 width = allocation->width - focus_edge_width * 2 - border_width * 2;
3496 if (priv->left_window)
3497 left_rect.width = priv->left_window->requisition.width;
3499 left_rect.width = 0;
3501 width -= left_rect.width;
3503 if (priv->right_window)
3504 right_rect.width = priv->right_window->requisition.width;
3506 right_rect.width = 0;
3508 width -= right_rect.width;
3510 text_rect.width = MAX (1, width);
3512 top_rect.width = text_rect.width;
3513 bottom_rect.width = text_rect.width;
3516 height = allocation->height - focus_edge_width * 2 - border_width * 2;
3518 if (priv->top_window)
3519 top_rect.height = priv->top_window->requisition.height;
3521 top_rect.height = 0;
3523 height -= top_rect.height;
3525 if (priv->bottom_window)
3526 bottom_rect.height = priv->bottom_window->requisition.height;
3528 bottom_rect.height = 0;
3530 height -= bottom_rect.height;
3532 text_rect.height = MAX (1, height);
3534 left_rect.height = text_rect.height;
3535 right_rect.height = text_rect.height;
3538 left_rect.x = focus_edge_width + border_width;
3539 top_rect.y = focus_edge_width + border_width;
3541 text_rect.x = left_rect.x + left_rect.width;
3542 text_rect.y = top_rect.y + top_rect.height;
3544 left_rect.y = text_rect.y;
3545 right_rect.y = text_rect.y;
3547 top_rect.x = text_rect.x;
3548 bottom_rect.x = text_rect.x;
3550 right_rect.x = text_rect.x + text_rect.width;
3551 bottom_rect.y = text_rect.y + text_rect.height;
3553 text_window_size_allocate (priv->text_window,
3556 if (priv->left_window)
3557 text_window_size_allocate (priv->left_window,
3560 if (priv->right_window)
3561 text_window_size_allocate (priv->right_window,
3564 if (priv->top_window)
3565 text_window_size_allocate (priv->top_window,
3568 if (priv->bottom_window)
3569 text_window_size_allocate (priv->bottom_window,
3572 gtk_text_view_update_layout_width (text_view);
3574 /* Note that this will do some layout validation */
3575 gtk_text_view_allocate_children (text_view);
3577 /* Update adjustments */
3578 gtk_text_view_set_hadjustment_values (text_view);
3579 gtk_text_view_set_vadjustment_values (text_view);
3581 /* The GTK resize loop processes all the pending exposes right
3582 * after doing the resize stuff, so the idle sizer won't have a
3583 * chance to run. So we do the work here.
3585 gtk_text_view_flush_first_validate (text_view);
3587 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3590 if (size_changed && gtk_widget_get_realized (widget))
3591 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
3595 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3598 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3599 text_view->priv->first_para_mark);
3603 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3606 GtkTextViewPrivate *priv;
3608 widget = GTK_WIDGET (text_view);
3609 priv = text_view->priv;
3611 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3613 if (SCREEN_HEIGHT (widget) > 0)
3615 GtkTextIter first_para;
3617 /* Be sure we've validated the stuff onscreen; if we
3618 * scrolled, these calls won't have any effect, because
3619 * they were called in the recursive validate_onscreen
3621 gtk_text_view_get_first_para_iter (text_view, &first_para);
3623 gtk_text_layout_validate_yrange (priv->layout,
3626 priv->first_para_pixels +
3627 SCREEN_HEIGHT (widget));
3630 priv->onscreen_validated = TRUE;
3632 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3634 /* This can have the odd side effect of triggering a scroll, which should
3635 * flip "onscreen_validated" back to FALSE, but should also get us
3636 * back into this function to turn it on again.
3638 gtk_text_view_update_adjustments (text_view);
3640 g_assert (priv->onscreen_validated);
3644 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3646 GtkTextViewPrivate *priv = text_view->priv;
3648 if (priv->first_validate_idle == 0)
3651 /* Do this first, which means that if an "invalidate"
3652 * occurs during any of this process, a new first_validate_callback
3653 * will be installed, and we'll start again.
3655 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3656 g_source_remove (priv->first_validate_idle);
3657 priv->first_validate_idle = 0;
3659 /* be sure we have up-to-date screen size set on the
3662 gtk_text_view_update_layout_width (text_view);
3664 /* Bail out if we invalidated stuff; scrolling right away will just
3665 * confuse the issue.
3667 if (priv->first_validate_idle != 0)
3669 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3673 /* scroll to any marks, if that's pending. This can jump us to
3674 * the validation codepath used for scrolling onscreen, if so we
3675 * bail out. It won't jump if already in that codepath since
3676 * value_changed is not recursive, so also validate if
3679 if (!gtk_text_view_flush_scroll (text_view) ||
3680 !priv->onscreen_validated)
3681 gtk_text_view_validate_onscreen (text_view);
3683 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3685 g_assert (priv->onscreen_validated);
3690 first_validate_callback (gpointer data)
3692 GtkTextView *text_view = data;
3694 /* Note that some of this code is duplicated at the end of size_allocate,
3695 * keep in sync with that.
3698 DV(g_print(G_STRLOC"\n"));
3700 gtk_text_view_flush_first_validate (text_view);
3706 incremental_validate_callback (gpointer data)
3708 GtkTextView *text_view = data;
3709 gboolean result = TRUE;
3711 DV(g_print(G_STRLOC"\n"));
3713 gtk_text_layout_validate (text_view->priv->layout, 2000);
3715 gtk_text_view_update_adjustments (text_view);
3717 if (gtk_text_layout_is_valid (text_view->priv->layout))
3719 text_view->priv->incremental_validate_idle = 0;
3727 gtk_text_view_invalidate (GtkTextView *text_view)
3729 GtkTextViewPrivate *priv = text_view->priv;
3731 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3732 priv->onscreen_validated));
3734 priv->onscreen_validated = FALSE;
3736 /* We'll invalidate when the layout is created */
3737 if (priv->layout == NULL)
3740 if (!priv->first_validate_idle)
3742 priv->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3743 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3744 priv->first_validate_idle));
3747 if (!priv->incremental_validate_idle)
3749 priv->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3750 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3751 priv->incremental_validate_idle));
3756 invalidated_handler (GtkTextLayout *layout,
3759 GtkTextView *text_view;
3761 text_view = GTK_TEXT_VIEW (data);
3763 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3764 gtk_text_view_invalidate (text_view);
3768 changed_handler (GtkTextLayout *layout,
3774 GtkTextView *text_view;
3775 GtkTextViewPrivate *priv;
3777 GdkRectangle visible_rect;
3778 GdkRectangle redraw_rect;
3780 text_view = GTK_TEXT_VIEW (data);
3781 priv = text_view->priv;
3782 widget = GTK_WIDGET (data);
3784 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3786 if (gtk_widget_get_realized (widget))
3788 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3790 redraw_rect.x = visible_rect.x;
3791 redraw_rect.width = visible_rect.width;
3792 redraw_rect.y = start_y;
3794 if (old_height == new_height)
3795 redraw_rect.height = old_height;
3796 else if (start_y + old_height > visible_rect.y)
3797 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3799 redraw_rect.height = 0;
3801 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3803 /* text_window_invalidate_rect() takes buffer coordinates */
3804 text_window_invalidate_rect (priv->text_window,
3807 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3811 redraw_rect.height));
3813 if (priv->left_window)
3814 text_window_invalidate_rect (priv->left_window,
3816 if (priv->right_window)
3817 text_window_invalidate_rect (priv->right_window,
3819 if (priv->top_window)
3820 text_window_invalidate_rect (priv->top_window,
3822 if (priv->bottom_window)
3823 text_window_invalidate_rect (priv->bottom_window,
3826 queue_update_im_spot_location (text_view);
3830 if (old_height != new_height)
3832 gboolean yoffset_changed = FALSE;
3834 int new_first_para_top;
3835 int old_first_para_top;
3838 /* If the bottom of the old area was above the top of the
3839 * screen, we need to scroll to keep the current top of the
3840 * screen in place. Remember that first_para_pixels is the
3841 * position of the top of the screen in coordinates relative to
3842 * the first paragraph onscreen.
3844 * In short we are adding the height delta of the portion of the
3845 * changed region above first_para_mark to priv->yoffset.
3847 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3848 priv->first_para_mark);
3850 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3852 old_first_para_top = priv->yoffset - priv->first_para_pixels;
3854 if (new_first_para_top != old_first_para_top)
3856 priv->yoffset += new_first_para_top - old_first_para_top;
3858 text_view->priv->vadjustment->value = priv->yoffset;
3859 yoffset_changed = TRUE;
3862 if (yoffset_changed)
3864 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3865 gtk_adjustment_value_changed (text_view->priv->vadjustment);
3868 /* FIXME be smarter about which anchored widgets we update */
3870 tmp_list = priv->children;
3871 while (tmp_list != NULL)
3873 GtkTextViewChild *child = tmp_list->data;
3876 gtk_text_view_update_child_allocation (text_view, child);
3878 tmp_list = g_slist_next (tmp_list);
3883 GtkRequisition old_req = priv->cached_size_request;
3884 GtkRequisition new_req;
3886 /* Use this instead of gtk_widget_size_request wrapper
3887 * to avoid the optimization which just returns widget->requisition
3888 * if a resize hasn't been queued.
3890 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3892 if (old_req.width != new_req.width ||
3893 old_req.height != new_req.height)
3895 gtk_widget_queue_resize_no_redraw (widget);
3901 gtk_text_view_realize (GtkWidget *widget)
3903 GtkAllocation allocation;
3904 GtkTextView *text_view;
3905 GtkTextViewPrivate *priv;
3907 GdkWindowAttr attributes;
3908 gint attributes_mask;
3911 text_view = GTK_TEXT_VIEW (widget);
3912 priv = text_view->priv;
3914 gtk_widget_set_realized (widget, TRUE);
3916 gtk_widget_get_allocation (widget, &allocation);
3918 attributes.window_type = GDK_WINDOW_CHILD;
3919 attributes.x = allocation.x;
3920 attributes.y = allocation.y;
3921 attributes.width = allocation.width;
3922 attributes.height = allocation.height;
3923 attributes.wclass = GDK_INPUT_OUTPUT;
3924 attributes.visual = gtk_widget_get_visual (widget);
3925 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3927 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
3929 window = gdk_window_new (gtk_widget_get_parent_window (widget),
3930 &attributes, attributes_mask);
3931 gtk_widget_set_window (widget, window);
3932 gdk_window_set_user_data (window, widget);
3934 /* must come before text_window_realize calls */
3935 gtk_widget_style_attach (widget);
3937 gdk_window_set_background (window,
3938 >k_widget_get_style (widget)->bg[gtk_widget_get_state (widget)]);
3940 text_window_realize (priv->text_window, widget);
3942 if (priv->left_window)
3943 text_window_realize (priv->left_window, widget);
3945 if (priv->top_window)
3946 text_window_realize (priv->top_window, widget);
3948 if (priv->right_window)
3949 text_window_realize (priv->right_window, widget);
3951 if (priv->bottom_window)
3952 text_window_realize (priv->bottom_window, widget);
3954 gtk_text_view_ensure_layout (text_view);
3958 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3959 GDK_SELECTION_PRIMARY);
3960 gtk_text_buffer_add_selection_clipboard (priv->buffer, clipboard);
3963 tmp_list = priv->children;
3964 while (tmp_list != NULL)
3966 GtkTextViewChild *vc = tmp_list->data;
3968 text_view_child_set_parent_window (text_view, vc);
3970 tmp_list = tmp_list->next;
3973 /* Ensure updating the spot location. */
3974 gtk_text_view_update_im_spot_location (text_view);
3978 gtk_text_view_unrealize (GtkWidget *widget)
3980 GtkTextView *text_view;
3981 GtkTextViewPrivate *priv;
3983 text_view = GTK_TEXT_VIEW (widget);
3984 priv = text_view->priv;
3988 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3989 GDK_SELECTION_PRIMARY);
3990 gtk_text_buffer_remove_selection_clipboard (priv->buffer, clipboard);
3993 gtk_text_view_remove_validate_idles (text_view);
3995 if (priv->popup_menu)
3997 gtk_widget_destroy (priv->popup_menu);
3998 priv->popup_menu = NULL;
4001 text_window_unrealize (priv->text_window);
4003 if (priv->left_window)
4004 text_window_unrealize (priv->left_window);
4006 if (priv->top_window)
4007 text_window_unrealize (priv->top_window);
4009 if (priv->right_window)
4010 text_window_unrealize (priv->right_window);
4012 if (priv->bottom_window)
4013 text_window_unrealize (priv->bottom_window);
4015 gtk_text_view_destroy_layout (text_view);
4017 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize (widget);
4021 gtk_text_view_set_background (GtkTextView *text_view)
4026 GtkTextViewPrivate *priv;
4028 widget = GTK_WIDGET (text_view);
4029 priv = text_view->priv;
4031 style = gtk_widget_get_style (widget);
4032 state = gtk_widget_get_state (widget);
4034 gdk_window_set_background (gtk_widget_get_window (widget),
4037 gdk_window_set_background (priv->text_window->bin_window,
4038 &style->base[state]);
4040 if (priv->left_window)
4041 gdk_window_set_background (priv->left_window->bin_window,
4043 if (priv->right_window)
4044 gdk_window_set_background (priv->right_window->bin_window,
4047 if (priv->top_window)
4048 gdk_window_set_background (priv->top_window->bin_window,
4051 if (priv->bottom_window)
4052 gdk_window_set_background (priv->bottom_window->bin_window,
4057 gtk_text_view_style_set (GtkWidget *widget,
4058 GtkStyle *previous_style)
4060 GtkTextView *text_view;
4061 GtkTextViewPrivate *priv;
4062 PangoContext *ltr_context, *rtl_context;
4064 text_view = GTK_TEXT_VIEW (widget);
4065 priv = text_view->priv;
4067 if (gtk_widget_get_realized (widget))
4069 gtk_text_view_set_background (text_view);
4072 if (priv->layout && previous_style)
4074 gtk_text_view_set_attributes_from_style (text_view,
4075 priv->layout->default_style,
4076 gtk_widget_get_style (widget));
4078 ltr_context = gtk_widget_create_pango_context (widget);
4079 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
4080 rtl_context = gtk_widget_create_pango_context (widget);
4081 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
4083 gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
4085 g_object_unref (ltr_context);
4086 g_object_unref (rtl_context);
4091 gtk_text_view_direction_changed (GtkWidget *widget,
4092 GtkTextDirection previous_direction)
4094 GtkTextViewPrivate *priv = GTK_TEXT_VIEW (widget)->priv;
4098 priv->layout->default_style->direction = gtk_widget_get_direction (widget);
4100 gtk_text_layout_default_style_changed (priv->layout);
4105 gtk_text_view_state_changed (GtkWidget *widget,
4106 GtkStateType previous_state)
4108 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4111 if (gtk_widget_get_realized (widget))
4113 gtk_text_view_set_background (text_view);
4115 if (gtk_widget_is_sensitive (widget))
4116 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4120 gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
4123 gdk_cursor_unref (cursor);
4125 text_view->priv->mouse_cursor_obscured = FALSE;
4128 if (!gtk_widget_is_sensitive (widget))
4130 /* Clear any selection */
4131 gtk_text_view_unselect (text_view);
4134 gtk_widget_queue_draw (widget);
4138 set_invisible_cursor (GdkWindow *window)
4140 GdkDisplay *display;
4143 display = gdk_window_get_display (window);
4144 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4146 gdk_window_set_cursor (window, cursor);
4148 gdk_cursor_unref (cursor);
4152 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
4154 if (text_view->priv->mouse_cursor_obscured)
4157 set_invisible_cursor (text_view->priv->text_window->bin_window);
4159 text_view->priv->mouse_cursor_obscured = TRUE;
4163 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
4165 if (text_view->priv->mouse_cursor_obscured)
4169 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
4171 gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
4172 gdk_cursor_unref (cursor);
4173 text_view->priv->mouse_cursor_obscured = FALSE;
4178 gtk_text_view_grab_notify (GtkWidget *widget,
4179 gboolean was_grabbed)
4181 GtkTextViewPrivate *priv;
4183 priv = GTK_TEXT_VIEW (widget)->priv;
4185 if (priv->grab_device &&
4186 gtk_widget_device_is_shadowed (widget, priv->grab_device))
4188 gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
4189 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
4199 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
4202 switch (event->type)
4204 case GDK_MOTION_NOTIFY:
4205 *x = event->motion.x;
4206 *y = event->motion.y;
4210 case GDK_BUTTON_PRESS:
4211 case GDK_2BUTTON_PRESS:
4212 case GDK_3BUTTON_PRESS:
4213 case GDK_BUTTON_RELEASE:
4214 *x = event->button.x;
4215 *y = event->button.y;
4220 case GDK_KEY_RELEASE:
4221 case GDK_ENTER_NOTIFY:
4222 case GDK_LEAVE_NOTIFY:
4223 case GDK_PROPERTY_NOTIFY:
4224 case GDK_SELECTION_CLEAR:
4225 case GDK_SELECTION_REQUEST:
4226 case GDK_SELECTION_NOTIFY:
4227 case GDK_PROXIMITY_IN:
4228 case GDK_PROXIMITY_OUT:
4229 case GDK_DRAG_ENTER:
4230 case GDK_DRAG_LEAVE:
4231 case GDK_DRAG_MOTION:
4232 case GDK_DRAG_STATUS:
4233 case GDK_DROP_START:
4234 case GDK_DROP_FINISHED:
4244 emit_event_on_tags (GtkWidget *widget,
4250 gboolean retval = FALSE;
4252 tags = gtk_text_iter_get_tags (iter);
4257 GtkTextTag *tag = tmp->data;
4259 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
4265 tmp = g_slist_next (tmp);
4268 g_slist_free (tags);
4274 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
4276 GtkTextView *text_view;
4277 GtkTextViewPrivate *priv;
4280 text_view = GTK_TEXT_VIEW (widget);
4281 priv = text_view->priv;
4283 if (priv->layout == NULL ||
4284 get_buffer (text_view) == NULL)
4287 if (event->any.window != priv->text_window->bin_window)
4290 if (get_event_coordinates (event, &x, &y))
4297 /* FIXME this is slow and we do it twice per event.
4298 * My favorite solution is to have GtkTextLayout cache
4299 * the last couple lookups.
4301 gtk_text_layout_get_iter_at_pixel (priv->layout,
4305 return emit_event_on_tags (widget, event, &iter);
4307 else if (event->type == GDK_KEY_PRESS ||
4308 event->type == GDK_KEY_RELEASE)
4312 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
4313 gtk_text_buffer_get_insert (get_buffer (text_view)));
4315 return emit_event_on_tags (widget, event, &iter);
4322 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
4324 GtkTextView *text_view;
4325 GtkTextViewPrivate *priv;
4326 GtkTextMark *insert;
4328 gboolean can_insert;
4329 gboolean retval = FALSE;
4330 gboolean obscure = FALSE;
4332 text_view = GTK_TEXT_VIEW (widget);
4333 priv = text_view->priv;
4335 if (priv->layout == NULL ||
4336 get_buffer (text_view) == NULL)
4339 /* Make sure input method knows where it is */
4340 flush_update_im_spot_location (text_view);
4342 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4343 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4344 can_insert = gtk_text_iter_can_insert (&iter, priv->editable);
4345 if (gtk_im_context_filter_keypress (priv->im_context, event))
4347 priv->need_im_reset = TRUE;
4349 gtk_text_view_reset_im_context (text_view);
4350 obscure = can_insert;
4354 else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4358 /* use overall editability not can_insert, more predictable for users */
4359 else if (priv->editable &&
4360 (event->keyval == GDK_KEY_Return ||
4361 event->keyval == GDK_KEY_ISO_Enter ||
4362 event->keyval == GDK_KEY_KP_Enter))
4364 /* this won't actually insert the newline if the cursor isn't
4367 gtk_text_view_reset_im_context (text_view);
4368 gtk_text_view_commit_text (text_view, "\n");
4373 /* Pass through Tab as literal tab, unless Control is held down */
4374 else if ((event->keyval == GDK_KEY_Tab ||
4375 event->keyval == GDK_KEY_KP_Tab ||
4376 event->keyval == GDK_KEY_ISO_Left_Tab) &&
4377 !(event->state & GDK_CONTROL_MASK))
4379 /* If the text widget isn't editable overall, or if the application
4380 * has turned off "accepts_tab", move the focus instead
4382 if (priv->accepts_tab && priv->editable)
4384 gtk_text_view_reset_im_context (text_view);
4385 gtk_text_view_commit_text (text_view, "\t");
4389 g_signal_emit_by_name (text_view, "move-focus",
4390 (event->state & GDK_SHIFT_MASK) ?
4391 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4399 gtk_text_view_obscure_mouse_cursor (text_view);
4401 gtk_text_view_reset_blink_time (text_view);
4402 gtk_text_view_pend_cursor_blink (text_view);
4408 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4410 GtkTextView *text_view;
4411 GtkTextViewPrivate *priv;
4412 GtkTextMark *insert;
4415 text_view = GTK_TEXT_VIEW (widget);
4416 priv = text_view->priv;
4418 if (priv->layout == NULL || get_buffer (text_view) == NULL)
4421 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4422 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4423 if (gtk_text_iter_can_insert (&iter, priv->editable) &&
4424 gtk_im_context_filter_keypress (priv->im_context, event))
4426 priv->need_im_reset = TRUE;
4430 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4434 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4436 GtkTextView *text_view;
4437 GtkTextViewPrivate *priv;
4439 text_view = GTK_TEXT_VIEW (widget);
4440 priv = text_view->priv;
4442 gtk_widget_grab_focus (widget);
4444 if (event->window != priv->text_window->bin_window)
4446 /* Remove selection if any. */
4447 gtk_text_view_unselect (text_view);
4451 gtk_text_view_reset_blink_time (text_view);
4455 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4456 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4457 else if (event->button == 3)
4458 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4461 if (event->type == GDK_BUTTON_PRESS)
4463 gtk_text_view_reset_im_context (text_view);
4465 if (event->button == 1)
4467 /* If we're in the selection, start a drag copy/move of the
4468 * selection; otherwise, start creating a new selection.
4471 GtkTextIter start, end;
4473 gtk_text_layout_get_iter_at_pixel (priv->layout,
4475 event->x + priv->xoffset,
4476 event->y + priv->yoffset);
4478 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4480 gtk_text_iter_in_range (&iter, &start, &end) &&
4481 !(event->state & GDK_SHIFT_MASK))
4483 priv->drag_start_x = event->x;
4484 priv->drag_start_y = event->y;
4485 priv->pending_place_cursor_button = event->button;
4489 gtk_text_view_start_selection_drag (text_view, &iter, event);
4494 else if (event->button == 2)
4498 /* We do not want to scroll back to the insert iter when we paste
4499 with the middle button */
4500 priv->scroll_after_paste = FALSE;
4502 gtk_text_layout_get_iter_at_pixel (priv->layout,
4504 event->x + priv->xoffset,
4505 event->y + priv->yoffset);
4507 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4508 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4513 else if (event->button == 3)
4515 gtk_text_view_do_popup (text_view, event);
4519 else if ((event->type == GDK_2BUTTON_PRESS ||
4520 event->type == GDK_3BUTTON_PRESS) &&
4525 gtk_text_view_end_selection_drag (text_view);
4527 gtk_text_layout_get_iter_at_pixel (priv->layout,
4529 event->x + priv->xoffset,
4530 event->y + priv->yoffset);
4532 gtk_text_view_start_selection_drag (text_view, &iter, event);
4540 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4542 GtkTextView *text_view;
4543 GtkTextViewPrivate *priv;
4545 text_view = GTK_TEXT_VIEW (widget);
4546 priv = text_view->priv;
4548 if (event->window != priv->text_window->bin_window)
4551 if (event->button == 1)
4553 if (priv->drag_start_x >= 0)
4555 priv->drag_start_x = -1;
4556 priv->drag_start_y = -1;
4559 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4561 else if (priv->pending_place_cursor_button == event->button)
4565 /* Unselect everything; we clicked inside selection, but
4566 * didn't move by the drag threshold, so just clear selection
4569 gtk_text_layout_get_iter_at_pixel (priv->layout,
4571 event->x + priv->xoffset,
4572 event->y + priv->yoffset);
4574 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4575 gtk_text_view_check_cursor_blink (text_view);
4577 priv->pending_place_cursor_button = 0;
4587 keymap_direction_changed (GdkKeymap *keymap,
4588 GtkTextView *text_view)
4590 gtk_text_view_check_keymap_direction (text_view);
4594 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4596 GtkTextView *text_view;
4597 GtkTextViewPrivate *priv;
4599 text_view = GTK_TEXT_VIEW (widget);
4600 priv = text_view->priv;
4602 gtk_widget_queue_draw (widget);
4604 DV(g_print (G_STRLOC": focus_in_event\n"));
4606 gtk_text_view_reset_blink_time (text_view);
4608 if (priv->cursor_visible && priv->layout)
4610 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
4611 gtk_text_view_check_cursor_blink (text_view);
4614 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4615 "direction-changed",
4616 G_CALLBACK (keymap_direction_changed), text_view);
4617 gtk_text_view_check_keymap_direction (text_view);
4621 priv->need_im_reset = TRUE;
4622 gtk_im_context_focus_in (priv->im_context);
4629 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4631 GtkTextView *text_view;
4632 GtkTextViewPrivate *priv;
4634 text_view = GTK_TEXT_VIEW (widget);
4635 priv = text_view->priv;
4637 gtk_text_view_end_selection_drag (text_view);
4639 gtk_widget_queue_draw (widget);
4641 DV(g_print (G_STRLOC": focus_out_event\n"));
4643 if (priv->cursor_visible && priv->layout)
4645 gtk_text_view_check_cursor_blink (text_view);
4646 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
4649 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4650 keymap_direction_changed,
4655 priv->need_im_reset = TRUE;
4656 gtk_im_context_focus_out (priv->im_context);
4663 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4665 GtkTextView *text_view;
4666 GtkTextViewPrivate *priv;
4668 text_view = GTK_TEXT_VIEW (widget);
4669 priv = text_view->priv;
4671 gtk_text_view_unobscure_mouse_cursor (text_view);
4673 if (event->window == priv->text_window->bin_window &&
4674 priv->drag_start_x >= 0)
4679 gdk_event_request_motions (event);
4681 if (gtk_drag_check_threshold (widget,
4687 gint buffer_x, buffer_y;
4689 gtk_text_view_window_to_buffer_coords (text_view,
4690 GTK_TEXT_WINDOW_TEXT,
4696 gtk_text_layout_get_iter_at_pixel (priv->layout,
4698 buffer_x, buffer_y);
4700 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4709 gtk_text_view_paint (GtkWidget *widget,
4712 GtkTextView *text_view;
4713 GtkTextViewPrivate *priv;
4714 GList *child_exposes;
4717 text_view = GTK_TEXT_VIEW (widget);
4718 priv = text_view->priv;
4720 g_return_if_fail (priv->layout != NULL);
4721 g_return_if_fail (priv->xoffset >= 0);
4722 g_return_if_fail (priv->yoffset >= 0);
4724 while (priv->first_validate_idle != 0)
4726 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4727 priv->first_validate_idle));
4728 gtk_text_view_flush_first_validate (text_view);
4731 if (!priv->onscreen_validated)
4733 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.");
4734 g_assert_not_reached ();
4738 printf ("painting %d,%d %d x %d\n",
4740 area->width, area->height);
4743 child_exposes = NULL;
4746 cairo_translate (cr, -priv->xoffset, -priv->yoffset);
4748 gtk_text_layout_draw (priv->layout,
4755 tmp_list = child_exposes;
4756 while (tmp_list != NULL)
4758 GtkWidget *child = tmp_list->data;
4760 gtk_container_propagate_draw (GTK_CONTAINER (text_view),
4764 g_object_unref (child);
4766 tmp_list = tmp_list->next;
4769 g_list_free (child_exposes);
4773 gtk_text_view_draw (GtkWidget *widget,
4779 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
4780 gtk_text_view_draw_focus (widget, cr);
4782 window = gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4783 GTK_TEXT_WINDOW_TEXT);
4784 if (gtk_cairo_should_draw_window (cr, window))
4786 DV(g_print (">Exposed ("G_STRLOC")\n"));
4788 gtk_cairo_transform_to_window (cr, widget, window);
4789 gtk_text_view_paint (widget, cr);
4793 /* Propagate exposes to all unanchored children.
4794 * Anchored children are handled in gtk_text_view_paint().
4796 tmp_list = GTK_TEXT_VIEW (widget)->priv->children;
4797 while (tmp_list != NULL)
4799 GtkTextViewChild *vc = tmp_list->data;
4801 /* propagate_draw checks that event->window matches
4805 gtk_container_propagate_draw (GTK_CONTAINER (widget),
4809 tmp_list = tmp_list->next;
4816 gtk_text_view_draw_focus (GtkWidget *widget,
4819 gboolean interior_focus;
4821 /* We clear the focus if we are in interior focus mode. */
4822 gtk_widget_style_get (widget,
4823 "interior-focus", &interior_focus,
4826 if (gtk_widget_has_focus (widget) && !interior_focus)
4828 gtk_paint_focus (gtk_widget_get_style (widget), cr,
4829 gtk_widget_get_state (widget),
4832 gtk_widget_get_allocated_width (widget),
4833 gtk_widget_get_allocated_height (widget));
4838 gtk_text_view_focus (GtkWidget *widget,
4839 GtkDirectionType direction)
4841 GtkContainer *container;
4844 container = GTK_CONTAINER (widget);
4846 if (!gtk_widget_is_focus (widget) &&
4847 gtk_container_get_focus_child (container) == NULL)
4849 gtk_widget_grab_focus (widget);
4855 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4856 * children to get the focus
4858 gtk_widget_set_can_focus (widget, FALSE);
4859 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4860 gtk_widget_set_can_focus (widget, TRUE);
4871 gtk_text_view_add (GtkContainer *container,
4874 /* This is pretty random. */
4875 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4877 GTK_TEXT_WINDOW_WIDGET,
4882 gtk_text_view_remove (GtkContainer *container,
4885 GtkTextView *text_view;
4886 GtkTextViewPrivate *priv;
4887 GtkTextViewChild *vc;
4890 text_view = GTK_TEXT_VIEW (container);
4891 priv = text_view->priv;
4894 iter = priv->children;
4896 while (iter != NULL)
4900 if (vc->widget == child)
4903 iter = g_slist_next (iter);
4906 g_assert (iter != NULL); /* be sure we had the child in the list */
4908 priv->children = g_slist_remove (priv->children, vc);
4910 gtk_widget_unparent (vc->widget);
4912 text_view_child_free (vc);
4916 gtk_text_view_forall (GtkContainer *container,
4917 gboolean include_internals,
4918 GtkCallback callback,
4919 gpointer callback_data)
4922 GtkTextView *text_view;
4925 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4926 g_return_if_fail (callback != NULL);
4928 text_view = GTK_TEXT_VIEW (container);
4930 copy = g_slist_copy (text_view->priv->children);
4933 while (iter != NULL)
4935 GtkTextViewChild *vc = iter->data;
4937 (* callback) (vc->widget, callback_data);
4939 iter = g_slist_next (iter);
4942 g_slist_free (copy);
4945 #define CURSOR_ON_MULTIPLIER 2
4946 #define CURSOR_OFF_MULTIPLIER 1
4947 #define CURSOR_PEND_MULTIPLIER 3
4948 #define CURSOR_DIVIDER 3
4951 cursor_blinks (GtkTextView *text_view)
4953 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4956 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4959 if (gtk_get_debug_flags () & GTK_DEBUG_UPDATES)
4962 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4967 if (text_view->priv->editable)
4969 GtkTextMark *insert;
4972 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4973 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4975 if (gtk_text_iter_editable (&iter, text_view->priv->editable))
4983 get_cursor_time (GtkTextView *text_view)
4985 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4988 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
4994 get_cursor_blink_timeout (GtkTextView *text_view)
4996 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4999 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
5010 blink_cb (gpointer data)
5012 GtkTextView *text_view;
5013 GtkTextViewPrivate *priv;
5017 text_view = GTK_TEXT_VIEW (data);
5018 priv = text_view->priv;
5020 if (!gtk_widget_has_focus (GTK_WIDGET (text_view)))
5022 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
5023 "connect a handler to this signal, it must return\n"
5024 "FALSE so the text view gets the event as well");
5026 gtk_text_view_check_cursor_blink (text_view);
5031 g_assert (priv->layout);
5032 g_assert (priv->cursor_visible);
5034 visible = gtk_text_layout_get_cursor_visible (priv->layout);
5036 blink_timeout = get_cursor_blink_timeout (text_view);
5037 if (priv->blink_time > 1000 * blink_timeout &&
5038 blink_timeout < G_MAXINT/1000)
5040 /* we've blinked enough without the user doing anything, stop blinking */
5042 priv->blink_timeout = 0;
5045 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5050 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5053 priv->blink_time += get_cursor_time (text_view);
5056 /* Block changed_handler while changing the layout's cursor visibility
5057 * because it would expose the whole paragraph. Instead, we expose
5058 * the cursor's area(s) manually below.
5060 g_signal_handlers_block_by_func (priv->layout,
5063 gtk_text_layout_set_cursor_visible (priv->layout, !visible);
5064 g_signal_handlers_unblock_by_func (priv->layout,
5068 text_window_invalidate_cursors (priv->text_window);
5070 /* Remove ourselves */
5076 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
5078 if (text_view->priv->blink_timeout)
5080 g_source_remove (text_view->priv->blink_timeout);
5081 text_view->priv->blink_timeout = 0;
5086 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
5088 GtkTextViewPrivate *priv = text_view->priv;
5090 if (priv->layout != NULL &&
5091 priv->cursor_visible &&
5092 gtk_widget_has_focus (GTK_WIDGET (text_view)))
5094 if (cursor_blinks (text_view))
5096 if (priv->blink_timeout == 0)
5098 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5100 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5107 gtk_text_view_stop_cursor_blink (text_view);
5108 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5113 gtk_text_view_stop_cursor_blink (text_view);
5114 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
5119 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
5121 GtkTextViewPrivate *priv = text_view->priv;
5123 if (priv->layout != NULL &&
5124 priv->cursor_visible &&
5125 gtk_widget_has_focus (GTK_WIDGET (text_view)) &&
5126 cursor_blinks (text_view))
5128 gtk_text_view_stop_cursor_blink (text_view);
5129 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5131 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5138 gtk_text_view_reset_blink_time (GtkTextView *text_view)
5140 GtkTextViewPrivate *priv = text_view->priv;
5142 priv->blink_time = 0;
5147 * Key binding handlers
5151 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
5152 GtkTextIter *newplace,
5155 gboolean ret = TRUE;
5159 ret = gtk_text_layout_move_iter_to_previous_line (text_view->priv->layout, newplace);
5165 ret = gtk_text_layout_move_iter_to_next_line (text_view->priv->layout, newplace);
5173 move_cursor (GtkTextView *text_view,
5174 const GtkTextIter *new_location,
5175 gboolean extend_selection)
5177 if (extend_selection)
5178 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
5182 gtk_text_buffer_place_cursor (get_buffer (text_view),
5184 gtk_text_view_check_cursor_blink (text_view);
5188 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
5189 GtkMovementStep step,
5191 gboolean extend_selection)
5193 GtkTextViewPrivate *priv;
5195 GtkTextIter newplace;
5196 gboolean cancel_selection = FALSE;
5197 gint cursor_x_pos = 0;
5198 GtkDirectionType leave_direction = -1;
5200 priv = text_view->priv;
5202 if (!priv->cursor_visible)
5204 GtkScrollStep scroll_step;
5208 case GTK_MOVEMENT_VISUAL_POSITIONS:
5209 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
5211 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5212 case GTK_MOVEMENT_WORDS:
5213 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
5215 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5216 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
5218 case GTK_MOVEMENT_DISPLAY_LINES:
5219 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
5221 case GTK_MOVEMENT_PARAGRAPHS:
5222 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5223 scroll_step = GTK_SCROLL_STEPS;
5225 case GTK_MOVEMENT_PAGES:
5226 scroll_step = GTK_SCROLL_PAGES;
5228 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5229 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
5231 case GTK_MOVEMENT_BUFFER_ENDS:
5232 scroll_step = GTK_SCROLL_ENDS;
5235 scroll_step = GTK_SCROLL_PAGES;
5239 if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
5241 if (leave_direction != -1 &&
5242 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5245 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5252 gtk_text_view_reset_im_context (text_view);
5254 if (step == GTK_MOVEMENT_PAGES)
5256 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
5257 gtk_widget_error_bell (GTK_WIDGET (text_view));
5259 gtk_text_view_check_cursor_blink (text_view);
5260 gtk_text_view_pend_cursor_blink (text_view);
5263 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
5265 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
5266 gtk_widget_error_bell (GTK_WIDGET (text_view));
5268 gtk_text_view_check_cursor_blink (text_view);
5269 gtk_text_view_pend_cursor_blink (text_view);
5273 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5274 gtk_text_buffer_get_insert (get_buffer (text_view)));
5276 if (! extend_selection)
5278 GtkTextIter sel_bound;
5280 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &sel_bound,
5281 gtk_text_buffer_get_selection_bound (get_buffer (text_view)));
5283 /* if we move forward, assume the cursor is at the end of the selection;
5284 * if we move backward, assume the cursor is at the start
5287 gtk_text_iter_order (&sel_bound, &insert);
5289 gtk_text_iter_order (&insert, &sel_bound);
5291 /* if we actually have a selection, just move *to* the beginning/end
5292 * of the selection and not *from* there on LOGICAL_POSITIONS
5293 * and VISUAL_POSITIONS movement
5295 if (! gtk_text_iter_equal (&sel_bound, &insert))
5296 cancel_selection = TRUE;
5301 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5302 gtk_text_view_get_virtual_cursor_pos (text_view, &insert, &cursor_x_pos, NULL);
5306 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5307 if (! cancel_selection)
5308 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5311 case GTK_MOVEMENT_VISUAL_POSITIONS:
5312 if (! cancel_selection)
5313 gtk_text_layout_move_iter_visually (priv->layout,
5317 case GTK_MOVEMENT_WORDS:
5319 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5322 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5323 gtk_text_iter_forward_to_line_end (&newplace);
5327 case GTK_MOVEMENT_DISPLAY_LINES:
5330 leave_direction = GTK_DIR_UP;
5332 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5333 gtk_text_layout_move_iter_to_x (priv->layout, &newplace, cursor_x_pos);
5335 gtk_text_iter_set_line_offset (&newplace, 0);
5339 leave_direction = GTK_DIR_DOWN;
5341 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5342 gtk_text_layout_move_iter_to_x (priv->layout, &newplace, cursor_x_pos);
5344 gtk_text_iter_forward_to_line_end (&newplace);
5348 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5350 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5351 else if (count < -1)
5352 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5355 gtk_text_layout_move_iter_to_line_end (priv->layout, &newplace, count);
5358 case GTK_MOVEMENT_PARAGRAPHS:
5361 if (!gtk_text_iter_ends_line (&newplace))
5363 gtk_text_iter_forward_to_line_end (&newplace);
5366 gtk_text_iter_forward_visible_lines (&newplace, count);
5367 gtk_text_iter_forward_to_line_end (&newplace);
5371 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5372 gtk_text_iter_set_line_offset (&newplace, 0);
5373 gtk_text_iter_forward_visible_lines (&newplace, count);
5374 gtk_text_iter_set_line_offset (&newplace, 0);
5378 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5381 if (!gtk_text_iter_ends_line (&newplace))
5382 gtk_text_iter_forward_to_line_end (&newplace);
5386 gtk_text_iter_set_line_offset (&newplace, 0);
5390 case GTK_MOVEMENT_BUFFER_ENDS:
5392 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5394 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5401 /* call move_cursor() even if the cursor hasn't moved, since it
5402 cancels the selection
5404 move_cursor (text_view, &newplace, extend_selection);
5406 if (!gtk_text_iter_equal (&insert, &newplace))
5408 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5409 gtk_text_view_scroll_mark_onscreen (text_view,
5410 gtk_text_buffer_get_insert (get_buffer (text_view)));
5412 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5413 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5415 else if (leave_direction != -1)
5417 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5420 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5423 else if (! cancel_selection)
5425 gtk_widget_error_bell (GTK_WIDGET (text_view));
5428 gtk_text_view_check_cursor_blink (text_view);
5429 gtk_text_view_pend_cursor_blink (text_view);
5433 gtk_text_view_move_cursor (GtkTextView *text_view,
5434 GtkMovementStep step,
5436 gboolean extend_selection)
5438 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5442 gtk_text_view_move_viewport (GtkTextView *text_view,
5446 GtkAdjustment *adjustment;
5451 case GTK_SCROLL_STEPS:
5452 case GTK_SCROLL_PAGES:
5453 case GTK_SCROLL_ENDS:
5454 adjustment = text_view->priv->vadjustment;
5456 case GTK_SCROLL_HORIZONTAL_STEPS:
5457 case GTK_SCROLL_HORIZONTAL_PAGES:
5458 case GTK_SCROLL_HORIZONTAL_ENDS:
5459 adjustment = text_view->priv->hadjustment;
5462 adjustment = text_view->priv->vadjustment;
5468 case GTK_SCROLL_STEPS:
5469 case GTK_SCROLL_HORIZONTAL_STEPS:
5470 increment = adjustment->step_increment;
5472 case GTK_SCROLL_PAGES:
5473 case GTK_SCROLL_HORIZONTAL_PAGES:
5474 increment = adjustment->page_increment;
5476 case GTK_SCROLL_ENDS:
5477 case GTK_SCROLL_HORIZONTAL_ENDS:
5478 increment = adjustment->upper - adjustment->lower;
5485 return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
5489 gtk_text_view_set_anchor (GtkTextView *text_view)
5493 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5494 gtk_text_buffer_get_insert (get_buffer (text_view)));
5496 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5500 gtk_text_view_scroll_pages (GtkTextView *text_view,
5502 gboolean extend_selection)
5504 GtkTextViewPrivate *priv;
5506 gint cursor_x_pos, cursor_y_pos;
5507 GtkTextMark *insert_mark;
5508 GtkTextIter old_insert;
5509 GtkTextIter new_insert;
5515 priv = text_view->priv;
5517 g_return_val_if_fail (priv->vadjustment != NULL, FALSE);
5519 adj = priv->vadjustment;
5521 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5523 /* Make sure we start from the current cursor position, even
5524 * if it was offscreen, but don't queue more scrolls if we're
5527 if (priv->pending_scroll)
5528 cancel_pending_scroll (text_view);
5530 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5532 /* Validate the region that will be brought into view by the cursor motion
5534 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5535 &old_insert, insert_mark);
5539 gtk_text_view_get_first_para_iter (text_view, &anchor);
5540 y0 = adj->page_size;
5541 y1 = adj->page_size + count * adj->page_increment;
5545 gtk_text_view_get_first_para_iter (text_view, &anchor);
5546 y0 = count * adj->page_increment + adj->page_size;
5550 gtk_text_layout_validate_yrange (priv->layout, &anchor, y0, y1);
5551 /* FIXME do we need to update the adjustment ranges here? */
5553 new_insert = old_insert;
5555 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5557 /* already at top, just be sure we are at offset 0 */
5558 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5559 move_cursor (text_view, &new_insert, extend_selection);
5561 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5563 /* already at bottom, just be sure we are at the end */
5564 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5565 move_cursor (text_view, &new_insert, extend_selection);
5569 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5571 oldval = adj->value;
5572 newval = adj->value;
5574 newval += count * adj->page_increment;
5576 set_adjustment_clamped (adj, newval);
5577 cursor_y_pos += adj->value - oldval;
5579 gtk_text_layout_get_iter_at_pixel (priv->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5580 clamp_iter_onscreen (text_view, &new_insert);
5581 move_cursor (text_view, &new_insert, extend_selection);
5583 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5586 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5587 * only guarantees 1 pixel onscreen.
5589 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5590 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5592 return !gtk_text_iter_equal (&old_insert, &new_insert);
5596 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5598 gboolean extend_selection)
5600 GtkTextViewPrivate *priv;
5602 gint cursor_x_pos, cursor_y_pos;
5603 GtkTextMark *insert_mark;
5604 GtkTextIter old_insert;
5605 GtkTextIter new_insert;
5610 priv = text_view->priv;
5612 g_return_val_if_fail (priv->hadjustment != NULL, FALSE);
5614 adj = priv->hadjustment;
5616 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5618 /* Make sure we start from the current cursor position, even
5619 * if it was offscreen, but don't queue more scrolls if we're
5622 if (priv->pending_scroll)
5623 cancel_pending_scroll (text_view);
5625 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5627 /* Validate the line that we're moving within.
5629 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5630 &old_insert, insert_mark);
5632 gtk_text_layout_get_line_yrange (priv->layout, &old_insert, &y, &height);
5633 gtk_text_layout_validate_yrange (priv->layout, &old_insert, y, y + height);
5634 /* FIXME do we need to update the adjustment ranges here? */
5636 new_insert = old_insert;
5638 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5640 /* already at far left, just be sure we are at offset 0 */
5641 gtk_text_iter_set_line_offset (&new_insert, 0);
5642 move_cursor (text_view, &new_insert, extend_selection);
5644 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5646 /* already at far right, just be sure we are at the end */
5647 if (!gtk_text_iter_ends_line (&new_insert))
5648 gtk_text_iter_forward_to_line_end (&new_insert);
5649 move_cursor (text_view, &new_insert, extend_selection);
5653 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5655 oldval = adj->value;
5656 newval = adj->value;
5658 newval += count * adj->page_increment;
5660 set_adjustment_clamped (adj, newval);
5661 cursor_x_pos += adj->value - oldval;
5663 gtk_text_layout_get_iter_at_pixel (priv->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5664 clamp_iter_onscreen (text_view, &new_insert);
5665 move_cursor (text_view, &new_insert, extend_selection);
5667 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5670 /* FIXME for lines shorter than the overall widget width, this results in a
5671 * "bounce" effect as we scroll to the right of the widget, then scroll
5672 * back to get the end of the line onscreen.
5673 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5676 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5677 * only guarantees 1 pixel onscreen.
5679 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5680 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5682 return !gtk_text_iter_equal (&old_insert, &new_insert);
5686 whitespace (gunichar ch, gpointer user_data)
5688 return (ch == ' ' || ch == '\t');
5692 not_whitespace (gunichar ch, gpointer user_data)
5694 return !whitespace (ch, user_data);
5698 find_whitepace_region (const GtkTextIter *center,
5699 GtkTextIter *start, GtkTextIter *end)
5704 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5705 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5706 if (whitespace (gtk_text_iter_get_char (end), NULL))
5707 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5709 return !gtk_text_iter_equal (start, end);
5713 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5716 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5717 text_view->priv->editable))
5719 gtk_widget_error_bell (GTK_WIDGET (text_view));
5724 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5728 GtkTextViewPrivate *priv;
5732 gboolean leave_one = FALSE;
5734 priv = text_view->priv;
5736 gtk_text_view_reset_im_context (text_view);
5738 if (type == GTK_DELETE_CHARS)
5740 /* Char delete deletes the selection, if one exists */
5741 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5746 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5747 gtk_text_buffer_get_insert (get_buffer (text_view)));
5754 case GTK_DELETE_CHARS:
5755 gtk_text_iter_forward_cursor_positions (&end, count);
5758 case GTK_DELETE_WORD_ENDS:
5760 gtk_text_iter_forward_word_ends (&end, count);
5762 gtk_text_iter_backward_word_starts (&start, 0 - count);
5765 case GTK_DELETE_WORDS:
5768 case GTK_DELETE_DISPLAY_LINE_ENDS:
5771 case GTK_DELETE_DISPLAY_LINES:
5774 case GTK_DELETE_PARAGRAPH_ENDS:
5777 /* If we're already at a newline, we need to
5778 * simply delete that newline, instead of
5779 * moving to the next one.
5781 if (gtk_text_iter_ends_line (&end))
5783 gtk_text_iter_forward_line (&end);
5789 if (!gtk_text_iter_forward_to_line_end (&end))
5797 if (gtk_text_iter_starts_line (&start))
5799 gtk_text_iter_backward_line (&start);
5800 if (!gtk_text_iter_ends_line (&end))
5801 gtk_text_iter_forward_to_line_end (&start);
5805 gtk_text_iter_set_line_offset (&start, 0);
5809 gtk_text_iter_backward_lines (&start, -count);
5813 case GTK_DELETE_PARAGRAPHS:
5816 gtk_text_iter_set_line_offset (&start, 0);
5817 gtk_text_iter_forward_to_line_end (&end);
5819 /* Do the lines beyond the first. */
5822 gtk_text_iter_forward_to_line_end (&end);
5828 /* FIXME negative count? */
5832 case GTK_DELETE_WHITESPACE:
5834 find_whitepace_region (&insert, &start, &end);
5842 if (!gtk_text_iter_equal (&start, &end))
5844 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5846 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5850 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5856 gtk_widget_error_bell (GTK_WIDGET (text_view));
5859 gtk_text_buffer_end_user_action (get_buffer (text_view));
5860 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5862 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5863 gtk_text_view_scroll_mark_onscreen (text_view,
5864 gtk_text_buffer_get_insert (get_buffer (text_view)));
5868 gtk_widget_error_bell (GTK_WIDGET (text_view));
5873 gtk_text_view_backspace (GtkTextView *text_view)
5875 GtkTextViewPrivate *priv;
5878 priv = text_view->priv;
5880 gtk_text_view_reset_im_context (text_view);
5882 /* Backspace deletes the selection, if one exists */
5883 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5887 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5889 gtk_text_buffer_get_insert (get_buffer (text_view)));
5891 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5892 TRUE, priv->editable))
5894 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5895 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5896 gtk_text_view_scroll_mark_onscreen (text_view,
5897 gtk_text_buffer_get_insert (get_buffer (text_view)));
5901 gtk_widget_error_bell (GTK_WIDGET (text_view));
5906 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5908 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5909 GDK_SELECTION_CLIPBOARD);
5911 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5913 text_view->priv->editable);
5914 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5915 gtk_text_view_scroll_mark_onscreen (text_view,
5916 gtk_text_buffer_get_insert (get_buffer (text_view)));
5920 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5922 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5923 GDK_SELECTION_CLIPBOARD);
5925 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5928 /* on copy do not scroll, we are already onscreen */
5932 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5934 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5935 GDK_SELECTION_CLIPBOARD);
5937 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5940 text_view->priv->editable);
5944 gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
5945 GtkClipboard *clipboard,
5948 GtkTextView *text_view = data;
5949 GtkTextViewPrivate *priv;
5951 priv = text_view->priv;
5953 if (priv->scroll_after_paste)
5955 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5956 gtk_text_view_scroll_mark_onscreen (text_view, gtk_text_buffer_get_insert (buffer));
5959 priv->scroll_after_paste = TRUE;
5963 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5965 GtkTextViewPrivate *priv = text_view->priv;
5967 if (priv->text_window)
5968 text_window_invalidate_cursors (priv->text_window);
5970 priv->overwrite_mode = !priv->overwrite_mode;
5973 gtk_text_layout_set_overwrite_mode (priv->layout,
5974 priv->overwrite_mode && priv->editable);
5976 if (priv->text_window)
5977 text_window_invalidate_cursors (priv->text_window);
5979 gtk_text_view_pend_cursor_blink (text_view);
5981 g_object_notify (G_OBJECT (text_view), "overwrite");
5985 * gtk_text_view_get_overwrite:
5986 * @text_view: a #GtkTextView
5988 * Returns whether the #GtkTextView is in overwrite mode or not.
5990 * Return value: whether @text_view is in overwrite mode or not.
5995 gtk_text_view_get_overwrite (GtkTextView *text_view)
5997 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5999 return text_view->priv->overwrite_mode;
6003 * gtk_text_view_set_overwrite:
6004 * @text_view: a #GtkTextView
6005 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
6007 * Changes the #GtkTextView overwrite mode.
6012 gtk_text_view_set_overwrite (GtkTextView *text_view,
6015 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6016 overwrite = overwrite != FALSE;
6018 if (text_view->priv->overwrite_mode != overwrite)
6019 gtk_text_view_toggle_overwrite (text_view);
6023 * gtk_text_view_set_accepts_tab:
6024 * @text_view: A #GtkTextView
6025 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
6026 * character, %FALSE, if pressing the Tab key should move the
6029 * Sets the behavior of the text widget when the Tab key is pressed.
6030 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
6031 * is %FALSE the keyboard focus is moved to the next widget in the focus
6037 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
6038 gboolean accepts_tab)
6040 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6042 accepts_tab = accepts_tab != FALSE;
6044 if (text_view->priv->accepts_tab != accepts_tab)
6046 text_view->priv->accepts_tab = accepts_tab;
6048 g_object_notify (G_OBJECT (text_view), "accepts-tab");
6053 * gtk_text_view_get_accepts_tab:
6054 * @text_view: A #GtkTextView
6056 * Returns whether pressing the Tab key inserts a tab characters.
6057 * gtk_text_view_set_accepts_tab().
6059 * Return value: %TRUE if pressing the Tab key inserts a tab character,
6060 * %FALSE if pressing the Tab key moves the keyboard focus.
6065 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
6067 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6069 return text_view->priv->accepts_tab;
6077 gtk_text_view_unselect (GtkTextView *text_view)
6081 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6082 gtk_text_buffer_get_insert (get_buffer (text_view)));
6084 gtk_text_buffer_move_mark (get_buffer (text_view),
6085 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
6090 get_iter_at_pointer (GtkTextView *text_view,
6096 GtkTextViewPrivate *priv;
6097 gint xcoord, ycoord;
6098 GdkModifierType state;
6100 priv = text_view->priv;
6102 gdk_window_get_device_position (priv->text_window->bin_window,
6103 device, &xcoord, &ycoord, &state);
6105 gtk_text_layout_get_iter_at_pixel (priv->layout,
6107 xcoord + priv->xoffset,
6108 ycoord + priv->yoffset);
6117 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
6118 const gchar *mark_name,
6121 GtkTextIter newplace;
6124 get_iter_at_pointer (text_view, device, &newplace, NULL, NULL);
6126 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
6128 /* This may invalidate the layout */
6129 DV(g_print (G_STRLOC": move mark\n"));
6131 gtk_text_buffer_move_mark (get_buffer (text_view),
6135 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6136 gtk_text_view_scroll_mark_onscreen (text_view, mark);
6138 DV (g_print ("first validate idle leaving %s is %d\n",
6139 G_STRLOC, text_view->priv->first_validate_idle));
6143 selection_scan_timeout (gpointer data)
6145 GtkTextView *text_view;
6147 text_view = GTK_TEXT_VIEW (data);
6149 gtk_text_view_scroll_mark_onscreen (text_view,
6150 gtk_text_buffer_get_insert (get_buffer (text_view)));
6152 return TRUE; /* remain installed. */
6155 #define UPPER_OFFSET_ANCHOR 0.8
6156 #define LOWER_OFFSET_ANCHOR 0.2
6159 check_scroll (gdouble offset, GtkAdjustment *adj)
6161 if ((offset > UPPER_OFFSET_ANCHOR &&
6162 adj->value + adj->page_size < adj->upper) ||
6163 (offset < LOWER_OFFSET_ANCHOR &&
6164 adj->value > adj->lower))
6171 drag_scan_timeout (gpointer data)
6173 GtkTextView *text_view;
6174 GtkTextViewPrivate *priv;
6175 GtkTextIter newplace;
6177 gdouble pointer_xoffset, pointer_yoffset;
6179 text_view = GTK_TEXT_VIEW (data);
6180 priv = text_view->priv;
6182 get_iter_at_pointer (text_view, priv->dnd_device, &newplace, &x, &y);
6184 gtk_text_buffer_move_mark (get_buffer (text_view),
6188 pointer_xoffset = (gdouble) x / gdk_window_get_width (priv->text_window->bin_window);
6189 pointer_yoffset = (gdouble) y / gdk_window_get_height (priv->text_window->bin_window);
6191 if (check_scroll (pointer_xoffset, priv->hadjustment) ||
6192 check_scroll (pointer_yoffset, priv->vadjustment))
6194 /* do not make offsets surpass lower nor upper anchors, this makes
6195 * scrolling speed relative to the distance of the pointer to the
6196 * anchors when it moves beyond them.
6198 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6199 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6201 gtk_text_view_scroll_to_mark (text_view,
6203 0., TRUE, pointer_xoffset, pointer_yoffset);
6214 } SelectionGranularity;
6217 * Move @start and @end to the boundaries of the selection unit (indicated by
6218 * @granularity) which contained @start initially.
6219 * If the selction unit is SELECT_WORDS and @start is not contained in a word
6220 * the selection is extended to all the white spaces between the end of the
6221 * word preceding @start and the start of the one following.
6224 extend_selection (GtkTextView *text_view,
6225 SelectionGranularity granularity,
6231 if (granularity == SELECT_WORDS)
6233 if (gtk_text_iter_inside_word (start))
6235 if (!gtk_text_iter_starts_word (start))
6236 gtk_text_iter_backward_visible_word_start (start);
6238 if (!gtk_text_iter_ends_word (end))
6240 if (!gtk_text_iter_forward_visible_word_end (end))
6241 gtk_text_iter_forward_to_end (end);
6249 if (gtk_text_iter_backward_visible_word_start (&tmp))
6250 gtk_text_iter_forward_visible_word_end (&tmp);
6252 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
6255 gtk_text_iter_set_line_offset (start, 0);
6258 if (!gtk_text_iter_forward_visible_word_end (&tmp))
6259 gtk_text_iter_forward_to_end (&tmp);
6261 if (gtk_text_iter_ends_word (&tmp))
6262 gtk_text_iter_backward_visible_word_start (&tmp);
6264 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
6267 gtk_text_iter_forward_to_line_end (end);
6270 else if (granularity == SELECT_LINES)
6272 if (gtk_text_view_starts_display_line (text_view, start))
6274 /* If on a display line boundary, we assume the user
6275 * clicked off the end of a line and we therefore select
6276 * the line before the boundary.
6278 gtk_text_view_backward_display_line_start (text_view, start);
6282 /* start isn't on the start of a line, so we move it to the
6283 * start, and move end to the end unless it's already there.
6285 gtk_text_view_backward_display_line_start (text_view, start);
6287 if (!gtk_text_view_starts_display_line (text_view, end))
6288 gtk_text_view_forward_display_line_end (text_view, end);
6296 SelectionGranularity granularity;
6297 GtkTextMark *orig_start;
6298 GtkTextMark *orig_end;
6302 selection_data_free (SelectionData *data)
6304 if (data->orig_start != NULL)
6305 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6307 if (data->orig_end != NULL)
6308 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6314 selection_motion_event_handler (GtkTextView *text_view,
6315 GdkEventMotion *event,
6316 SelectionData *data)
6318 GtkTextViewPrivate *priv;
6320 priv = text_view->priv;
6321 gdk_event_request_motions (event);
6323 if (priv->grab_device != event->device)
6326 if (data->granularity == SELECT_CHARACTERS)
6328 move_mark_to_pointer_and_scroll (text_view, "insert", event->device);
6332 GtkTextIter cursor, start, end;
6333 GtkTextIter orig_start, orig_end;
6334 GtkTextBuffer *buffer;
6336 buffer = get_buffer (text_view);
6338 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6339 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6341 get_iter_at_pointer (text_view, event->device, &cursor, NULL, NULL);
6344 extend_selection (text_view, data->granularity, &start, &end);
6346 /* either the selection extends to the front, or end (or not) */
6347 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6348 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6350 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6352 gtk_text_view_scroll_mark_onscreen (text_view,
6353 gtk_text_buffer_get_insert (buffer));
6356 /* If we had to scroll offscreen, insert a timeout to do so
6357 * again. Note that in the timeout, even if the mouse doesn't
6358 * move, due to this scroll xoffset/yoffset will have changed
6359 * and we'll need to scroll again.
6361 if (text_view->priv->scroll_timeout != 0) /* reset on every motion event */
6362 g_source_remove (text_view->priv->scroll_timeout);
6364 text_view->priv->scroll_timeout =
6365 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6371 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6372 const GtkTextIter *iter,
6373 GdkEventButton *button)
6375 GtkTextViewPrivate *priv;
6376 GtkTextIter cursor, ins, bound;
6377 GtkTextIter orig_start, orig_end;
6378 GtkTextBuffer *buffer;
6379 SelectionData *data;
6381 if (text_view->priv->selection_drag_handler != 0)
6384 priv = text_view->priv;
6385 data = g_new0 (SelectionData, 1);
6387 if (button->type == GDK_2BUTTON_PRESS)
6388 data->granularity = SELECT_WORDS;
6389 else if (button->type == GDK_3BUTTON_PRESS)
6390 data->granularity = SELECT_LINES;
6392 data->granularity = SELECT_CHARACTERS;
6394 priv->grab_device = button->device;
6395 gtk_device_grab_add (GTK_WIDGET (text_view),
6399 buffer = get_buffer (text_view);
6404 extend_selection (text_view, data->granularity, &ins, &bound);
6408 if (button->state & GDK_SHIFT_MASK)
6410 /* Extend selection */
6411 GtkTextIter old_ins, old_bound;
6412 GtkTextIter old_start, old_end;
6414 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6415 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6416 old_start = old_ins;
6417 old_end = old_bound;
6418 gtk_text_iter_order (&old_start, &old_end);
6420 /* move the front cursor, if the mouse is in front of the selection. Should the
6421 * cursor however be inside the selection (this happens on tripple click) then we
6422 * move the side which was last moved (current insert mark) */
6423 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6424 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6425 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6428 orig_start = old_end;
6440 gtk_text_buffer_select_range (buffer, &ins, &bound);
6442 gtk_text_iter_order (&orig_start, &orig_end);
6443 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6445 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6447 gtk_text_view_check_cursor_blink (text_view);
6449 text_view->priv->selection_drag_handler = g_signal_connect_data (text_view,
6450 "motion-notify-event",
6451 G_CALLBACK (selection_motion_event_handler),
6453 (GClosureNotify) selection_data_free, 0);
6456 /* returns whether we were really dragging */
6458 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6460 GtkTextViewPrivate *priv;
6462 priv = text_view->priv;
6464 if (!priv->grab_device)
6467 if (priv->selection_drag_handler == 0)
6470 g_signal_handler_disconnect (text_view, priv->selection_drag_handler);
6471 priv->selection_drag_handler = 0;
6473 if (priv->scroll_timeout != 0)
6475 g_source_remove (priv->scroll_timeout);
6476 priv->scroll_timeout = 0;
6479 gtk_device_grab_remove (GTK_WIDGET (text_view),
6481 priv->grab_device = NULL;
6491 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6492 GtkTextAttributes *values,
6495 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
6496 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
6499 pango_font_description_free (values->font);
6501 values->font = pango_font_description_copy (style->font_desc);
6505 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6507 GtkTextViewPrivate *priv = text_view->priv;
6511 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6512 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6513 GtkTextDirection new_cursor_dir;
6514 GtkTextDirection new_keyboard_dir;
6515 gboolean split_cursor;
6517 g_object_get (settings,
6518 "gtk-split-cursor", &split_cursor,
6521 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6522 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6524 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6527 new_cursor_dir = GTK_TEXT_DIR_NONE;
6529 new_cursor_dir = new_keyboard_dir;
6531 gtk_text_layout_set_cursor_direction (priv->layout, new_cursor_dir);
6532 gtk_text_layout_set_keyboard_direction (priv->layout, new_keyboard_dir);
6537 gtk_text_view_ensure_layout (GtkTextView *text_view)
6540 GtkTextViewPrivate *priv;
6542 widget = GTK_WIDGET (text_view);
6543 priv = text_view->priv;
6545 if (priv->layout == NULL)
6547 GtkTextAttributes *style;
6548 PangoContext *ltr_context, *rtl_context;
6551 DV(g_print(G_STRLOC"\n"));
6553 priv->layout = gtk_text_layout_new ();
6555 g_signal_connect (priv->layout,
6557 G_CALLBACK (invalidated_handler),
6560 g_signal_connect (priv->layout,
6562 G_CALLBACK (changed_handler),
6565 g_signal_connect (priv->layout,
6567 G_CALLBACK (gtk_text_view_child_allocated),
6570 if (get_buffer (text_view))
6571 gtk_text_layout_set_buffer (priv->layout, get_buffer (text_view));
6573 if ((gtk_widget_has_focus (widget) && priv->cursor_visible))
6574 gtk_text_view_pend_cursor_blink (text_view);
6576 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
6578 gtk_text_layout_set_overwrite_mode (priv->layout,
6579 priv->overwrite_mode && priv->editable);
6581 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6582 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6583 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6584 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6586 gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
6588 g_object_unref (ltr_context);
6589 g_object_unref (rtl_context);
6591 gtk_text_view_check_keymap_direction (text_view);
6593 style = gtk_text_attributes_new ();
6595 gtk_widget_ensure_style (widget);
6596 gtk_text_view_set_attributes_from_style (text_view,
6598 gtk_widget_get_style (widget));
6600 style->pixels_above_lines = priv->pixels_above_lines;
6601 style->pixels_below_lines = priv->pixels_below_lines;
6602 style->pixels_inside_wrap = priv->pixels_inside_wrap;
6603 style->left_margin = priv->left_margin;
6604 style->right_margin = priv->right_margin;
6605 style->indent = priv->indent;
6606 style->tabs = priv->tabs ? pango_tab_array_copy (priv->tabs) : NULL;
6608 style->wrap_mode = priv->wrap_mode;
6609 style->justification = priv->justify;
6610 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6612 gtk_text_layout_set_default_style (priv->layout, style);
6614 gtk_text_attributes_unref (style);
6616 /* Set layout for all anchored children */
6618 tmp_list = priv->children;
6619 while (tmp_list != NULL)
6621 GtkTextViewChild *vc = tmp_list->data;
6625 gtk_text_anchored_child_set_layout (vc->widget,
6627 /* vc may now be invalid! */
6630 tmp_list = g_slist_next (tmp_list);
6633 gtk_text_view_invalidate (text_view);
6638 * gtk_text_view_get_default_attributes:
6639 * @text_view: a #GtkTextView
6641 * Obtains a copy of the default text attributes. These are the
6642 * attributes used for text unless a tag overrides them.
6643 * You'd typically pass the default attributes in to
6644 * gtk_text_iter_get_attributes() in order to get the
6645 * attributes in effect at a given text position.
6647 * The return value is a copy owned by the caller of this function,
6648 * and should be freed.
6650 * Return value: a new #GtkTextAttributes
6653 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6655 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6657 gtk_text_view_ensure_layout (text_view);
6659 return gtk_text_attributes_copy (text_view->priv->layout->default_style);
6663 gtk_text_view_destroy_layout (GtkTextView *text_view)
6665 GtkTextViewPrivate *priv = text_view->priv;
6671 gtk_text_view_remove_validate_idles (text_view);
6673 g_signal_handlers_disconnect_by_func (priv->layout,
6674 invalidated_handler,
6676 g_signal_handlers_disconnect_by_func (priv->layout,
6680 /* Remove layout from all anchored children */
6681 tmp_list = priv->children;
6682 while (tmp_list != NULL)
6684 GtkTextViewChild *vc = tmp_list->data;
6688 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6689 /* vc may now be invalid! */
6692 tmp_list = g_slist_next (tmp_list);
6695 gtk_text_view_stop_cursor_blink (text_view);
6696 gtk_text_view_end_selection_drag (text_view);
6698 g_object_unref (priv->layout);
6699 priv->layout = NULL;
6704 * gtk_text_view_reset_im_context:
6705 * @text_view: a #GtkTextView
6707 * Reset the input method context of the text view if needed.
6709 * This can be necessary in the case where modifying the buffer
6710 * would confuse on-going input method behavior.
6715 gtk_text_view_reset_im_context (GtkTextView *text_view)
6717 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6719 if (text_view->priv->need_im_reset)
6721 text_view->priv->need_im_reset = FALSE;
6722 gtk_im_context_reset (text_view->priv->im_context);
6727 * gtk_text_view_im_context_filter_keypress:
6728 * @text_view: a #GtkTextView
6729 * @event: the key event
6731 * Allow the #GtkTextView input method to internally handle key press
6732 * and release events. If this function returns %TRUE, then no further
6733 * processing should be done for this key event. See
6734 * gtk_im_context_filter_keypress().
6736 * Note that you are expected to call this function from your handler
6737 * when overriding key event handling. This is needed in the case when
6738 * you need to insert your own key handling between the input method
6739 * and the default key event handling of the #GtkTextView.
6743 * gtk_foo_bar_key_press_event (GtkWidget *widget,
6744 * GdkEventKey *event)
6746 * if ((key->keyval == GDK_Return || key->keyval == GDK_KP_Enter))
6748 * if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (view), event))
6752 * /* Do some stuff */
6754 * return GTK_WIDGET_CLASS (gtk_foo_bar_parent_class)->key_press_event (widget, event);
6758 * Return value: %TRUE if the input method handled the key event.
6763 gtk_text_view_im_context_filter_keypress (GtkTextView *text_view,
6766 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6768 return gtk_im_context_filter_keypress (text_view->priv->im_context, event);
6776 drag_begin_cb (GtkWidget *widget,
6777 GdkDragContext *context,
6780 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6781 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6784 cairo_surface_t *surface = NULL;
6786 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6788 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6789 surface = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6793 gtk_drag_set_icon_surface (context, surface);
6794 cairo_surface_destroy (surface);
6798 gtk_drag_set_icon_default (context);
6803 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6804 const GtkTextIter *iter,
6805 GdkEventMotion *event)
6807 GtkTargetList *target_list;
6809 text_view->priv->drag_start_x = -1;
6810 text_view->priv->drag_start_y = -1;
6811 text_view->priv->pending_place_cursor_button = 0;
6813 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6815 g_signal_connect (text_view, "drag-begin",
6816 G_CALLBACK (drag_begin_cb), NULL);
6817 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6818 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6819 1, (GdkEvent*)event);
6823 gtk_text_view_drag_begin (GtkWidget *widget,
6824 GdkDragContext *context)
6830 gtk_text_view_drag_end (GtkWidget *widget,
6831 GdkDragContext *context)
6836 gtk_text_view_drag_data_get (GtkWidget *widget,
6837 GdkDragContext *context,
6838 GtkSelectionData *selection_data,
6842 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6843 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6845 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6847 gtk_selection_data_set (selection_data,
6848 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6853 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6860 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6862 /* Extract the selected text */
6863 str = gtk_text_buffer_serialize (buffer, buffer,
6864 selection_data->target,
6871 gtk_selection_data_set (selection_data,
6872 selection_data->target,
6874 (guchar *) str, len);
6884 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6886 /* Extract the selected text */
6887 str = gtk_text_iter_get_visible_text (&start, &end);
6892 gtk_selection_data_set_text (selection_data, str, -1);
6899 gtk_text_view_drag_data_delete (GtkWidget *widget,
6900 GdkDragContext *context)
6902 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->priv->buffer,
6903 TRUE, GTK_TEXT_VIEW (widget)->priv->editable);
6907 gtk_text_view_drag_leave (GtkWidget *widget,
6908 GdkDragContext *context,
6911 GtkTextView *text_view;
6912 GtkTextViewPrivate *priv;
6914 text_view = GTK_TEXT_VIEW (widget);
6915 priv = text_view->priv;
6917 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
6919 if (priv->dnd_device)
6920 priv->dnd_device = NULL;
6922 if (priv->scroll_timeout != 0)
6923 g_source_remove (priv->scroll_timeout);
6925 priv->scroll_timeout = 0;
6929 gtk_text_view_drag_motion (GtkWidget *widget,
6930 GdkDragContext *context,
6935 GtkTextIter newplace;
6936 GtkTextView *text_view;
6937 GtkTextViewPrivate *priv;
6940 GdkRectangle target_rect;
6943 GdkDragAction suggested_action = 0;
6945 text_view = GTK_TEXT_VIEW (widget);
6946 priv = text_view->priv;
6948 target_rect = priv->text_window->allocation;
6950 if (x < target_rect.x ||
6951 y < target_rect.y ||
6952 x > (target_rect.x + target_rect.width) ||
6953 y > (target_rect.y + target_rect.height))
6954 return FALSE; /* outside the text window, allow parent widgets to handle event */
6956 gtk_text_view_window_to_buffer_coords (text_view,
6957 GTK_TEXT_WINDOW_WIDGET,
6961 gtk_text_layout_get_iter_at_pixel (priv->layout,
6965 target = gtk_drag_dest_find_target (widget, context,
6966 gtk_drag_dest_get_target_list (widget));
6968 if (target == GDK_NONE)
6970 /* can't accept any of the offered targets */
6972 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6974 gtk_text_iter_compare (&newplace, &start) >= 0 &&
6975 gtk_text_iter_compare (&newplace, &end) <= 0)
6977 /* We're inside the selection. */
6981 if (gtk_text_iter_can_insert (&newplace, priv->editable))
6983 GtkWidget *source_widget;
6985 suggested_action = context->suggested_action;
6987 source_widget = gtk_drag_get_source_widget (context);
6989 if (source_widget == widget)
6991 /* Default to MOVE, unless the user has
6992 * pressed ctrl or alt to affect available actions
6994 if ((context->actions & GDK_ACTION_MOVE) != 0)
6995 suggested_action = GDK_ACTION_MOVE;
7000 /* Can't drop here. */
7004 if (suggested_action != 0)
7006 gtk_text_mark_set_visible (priv->dnd_mark,
7007 priv->cursor_visible);
7009 gdk_drag_status (context, suggested_action, time);
7013 gdk_drag_status (context, 0, time);
7014 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7017 priv->dnd_device = gdk_drag_context_get_device (context);
7019 if (!priv->scroll_timeout)
7020 priv->scroll_timeout =
7021 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
7023 /* TRUE return means don't propagate the drag motion to parent
7024 * widgets that may also be drop sites.
7030 gtk_text_view_drag_drop (GtkWidget *widget,
7031 GdkDragContext *context,
7036 GtkTextView *text_view;
7037 GtkTextViewPrivate *priv;
7038 GtkTextIter drop_point;
7039 GdkAtom target = GDK_NONE;
7041 text_view = GTK_TEXT_VIEW (widget);
7042 priv = text_view->priv;
7044 if (priv->scroll_timeout != 0)
7045 g_source_remove (priv->scroll_timeout);
7047 priv->scroll_timeout = 0;
7049 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7051 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7055 if (gtk_text_iter_can_insert (&drop_point, priv->editable))
7056 target = gtk_drag_dest_find_target (widget, context, NULL);
7058 if (target != GDK_NONE)
7059 gtk_drag_get_data (widget, context, target, time);
7061 gtk_drag_finish (context, FALSE, FALSE, time);
7067 insert_text_data (GtkTextView *text_view,
7068 GtkTextIter *drop_point,
7069 GtkSelectionData *selection_data)
7073 str = gtk_selection_data_get_text (selection_data);
7077 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
7078 drop_point, (gchar *) str, -1,
7079 text_view->priv->editable))
7081 gtk_widget_error_bell (GTK_WIDGET (text_view));
7089 gtk_text_view_drag_data_received (GtkWidget *widget,
7090 GdkDragContext *context,
7093 GtkSelectionData *selection_data,
7097 GtkTextIter drop_point;
7098 GtkTextView *text_view;
7099 GtkTextViewPrivate *priv;
7100 gboolean success = FALSE;
7101 GtkTextBuffer *buffer = NULL;
7103 text_view = GTK_TEXT_VIEW (widget);
7104 priv = text_view->priv;
7106 if (!priv->dnd_mark)
7109 buffer = get_buffer (text_view);
7111 gtk_text_buffer_get_iter_at_mark (buffer,
7115 if (!gtk_text_iter_can_insert (&drop_point, priv->editable))
7120 gtk_text_buffer_begin_user_action (buffer);
7122 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7124 GtkTextBuffer *src_buffer = NULL;
7125 GtkTextIter start, end;
7126 gboolean copy_tags = TRUE;
7128 if (selection_data->length != sizeof (src_buffer))
7131 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
7133 if (src_buffer == NULL)
7136 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
7138 if (gtk_text_buffer_get_tag_table (src_buffer) !=
7139 gtk_text_buffer_get_tag_table (buffer))
7141 /* try to find a suitable rich text target instead */
7145 GdkAtom target = GDK_NONE;
7149 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
7151 for (list = context->targets; list; list = g_list_next (list))
7155 for (i = 0; i < n_atoms; i++)
7156 if (GUINT_TO_POINTER (atoms[i]) == list->data)
7165 if (target != GDK_NONE)
7167 gtk_drag_get_data (widget, context, target, time);
7168 gtk_text_buffer_end_user_action (buffer);
7173 if (gtk_text_buffer_get_selection_bounds (src_buffer,
7178 gtk_text_buffer_insert_range_interactive (buffer,
7187 str = gtk_text_iter_get_visible_text (&start, &end);
7188 gtk_text_buffer_insert_interactive (buffer,
7189 &drop_point, str, -1,
7195 else if (selection_data->length > 0 &&
7196 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
7199 GError *error = NULL;
7201 retval = gtk_text_buffer_deserialize (buffer, buffer,
7202 selection_data->target,
7204 (guint8 *) selection_data->data,
7205 selection_data->length,
7210 g_warning ("error pasting: %s\n", error->message);
7211 g_clear_error (&error);
7215 insert_text_data (text_view, &drop_point, selection_data);
7218 gtk_drag_finish (context, success,
7219 success && context->action == GDK_ACTION_MOVE,
7224 gtk_text_buffer_get_iter_at_mark (buffer,
7227 gtk_text_buffer_place_cursor (buffer, &drop_point);
7229 gtk_text_buffer_end_user_action (buffer);
7234 * gtk_text_view_get_hadjustment:
7235 * @text_view: a #GtkTextView
7237 * Gets the horizontal-scrolling #GtkAdjustment.
7239 * Returns: (transfer none): pointer to the horizontal #GtkAdjustment
7243 * Deprecated: 3.0: Use gtk_scrollable_get_hadjustment()
7246 gtk_text_view_get_hadjustment (GtkTextView *text_view)
7248 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7250 return text_view->priv->hadjustment;
7254 gtk_text_view_set_hadjustment (GtkTextView *text_view,
7255 GtkAdjustment *adjustment)
7257 GtkTextViewPrivate *priv = text_view->priv;
7259 if (adjustment && priv->hadjustment == adjustment)
7262 if (priv->hadjustment != NULL)
7264 g_signal_handlers_disconnect_by_func (priv->hadjustment,
7265 gtk_text_view_value_changed,
7267 g_object_unref (priv->hadjustment);
7270 if (adjustment == NULL)
7271 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
7273 g_signal_connect (adjustment, "value-changed",
7274 G_CALLBACK (gtk_text_view_value_changed), text_view);
7275 priv->hadjustment = g_object_ref_sink (adjustment);
7276 gtk_text_view_set_hadjustment_values (text_view);
7278 g_object_notify (G_OBJECT (text_view), "hadjustment");
7282 * gtk_text_view_get_vadjustment:
7283 * @text_view: a #GtkTextView
7285 * Gets the vertical-scrolling #GtkAdjustment.
7287 * Returns: (transfer none): pointer to the vertical #GtkAdjustment
7292 gtk_text_view_get_vadjustment (GtkTextView *text_view)
7294 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7296 return text_view->priv->vadjustment;
7300 gtk_text_view_set_vadjustment (GtkTextView *text_view,
7301 GtkAdjustment *adjustment)
7303 GtkTextViewPrivate *priv = text_view->priv;
7305 if (adjustment && priv->vadjustment == adjustment)
7308 if (priv->vadjustment != NULL)
7310 g_signal_handlers_disconnect_by_func (priv->vadjustment,
7311 gtk_text_view_value_changed,
7313 g_object_unref (priv->vadjustment);
7316 if (adjustment == NULL)
7317 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
7319 g_signal_connect (adjustment, "value-changed",
7320 G_CALLBACK (gtk_text_view_value_changed), text_view);
7321 priv->vadjustment = g_object_ref_sink (adjustment);
7322 gtk_text_view_set_vadjustment_values (text_view);
7324 g_object_notify (G_OBJECT (text_view), "vadjustment");
7328 gtk_text_view_set_hadjustment_values (GtkTextView *text_view)
7330 GtkTextViewPrivate *priv;
7336 priv = text_view->priv;
7338 screen_width = SCREEN_WIDTH (text_view);
7339 old_value = gtk_adjustment_get_value (priv->hadjustment);
7340 new_upper = MAX (screen_width, priv->width);
7342 g_object_set (priv->hadjustment,
7345 "page-size", (gdouble)screen_width,
7346 "step-increment", screen_width * 0.1,
7347 "page-increment", screen_width * 0.9,
7350 new_value = CLAMP (old_value, 0, new_upper - screen_width);
7351 if (new_value != old_value)
7352 gtk_adjustment_set_value (priv->hadjustment, new_value);
7356 gtk_text_view_set_vadjustment_values (GtkTextView *text_view)
7358 GtkTextViewPrivate *priv;
7359 GtkTextIter first_para;
7366 priv = text_view->priv;
7368 screen_height = SCREEN_HEIGHT (text_view);
7369 old_value = gtk_adjustment_get_value (priv->vadjustment);
7370 new_upper = MAX (screen_height, priv->height);
7372 g_object_set (priv->vadjustment,
7375 "page-size", (gdouble)screen_height,
7376 "step-increment", screen_height * 0.1,
7377 "page-increment", screen_height * 0.9,
7380 /* Now adjust the value of the adjustment to keep the cursor at the
7381 * same place in the buffer */
7382 gtk_text_view_ensure_layout (text_view);
7383 gtk_text_view_get_first_para_iter (text_view, &first_para);
7384 gtk_text_layout_get_line_yrange (priv->layout, &first_para, &y, NULL);
7386 y += priv->first_para_pixels;
7388 new_value = CLAMP (y, 0, new_upper - screen_height);
7389 if (new_value != old_value)
7390 gtk_adjustment_set_value (priv->vadjustment, new_value);
7394 /* FIXME this adjust_allocation is a big cut-and-paste from
7395 * GtkCList, needs to be some "official" way to do this
7405 /* The window to which widget->window is relative */
7406 #define ALLOCATION_WINDOW(widget) \
7407 (!gtk_widget_get_has_window (widget) ? \
7408 gtk_widget_get_window (widget) : \
7409 gdk_window_get_parent (gtk_widget_get_window (widget)))
7412 adjust_allocation_recurse (GtkWidget *widget,
7415 GtkAllocation allocation;
7416 ScrollData *scroll_data = data;
7418 /* Need to really size allocate instead of just poking
7419 * into widget->allocation if the widget is not realized.
7420 * FIXME someone figure out why this was.
7422 if (!gtk_widget_get_realized (widget))
7424 if (gtk_widget_get_visible (widget))
7426 GdkRectangle tmp_rectangle;
7428 tmp_rectangle = allocation;
7429 tmp_rectangle.x += scroll_data->dx;
7430 tmp_rectangle.y += scroll_data->dy;
7432 gtk_widget_size_allocate (widget, &tmp_rectangle);
7437 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
7439 allocation.x += scroll_data->dx;
7440 allocation.y += scroll_data->dy;
7441 gtk_widget_set_allocation (widget, &allocation);
7443 if (GTK_IS_CONTAINER (widget))
7444 gtk_container_forall (GTK_CONTAINER (widget),
7445 adjust_allocation_recurse,
7452 adjust_allocation (GtkWidget *widget,
7456 ScrollData scroll_data;
7458 if (gtk_widget_get_realized (widget))
7459 scroll_data.window = ALLOCATION_WINDOW (widget);
7461 scroll_data.window = NULL;
7463 scroll_data.dx = dx;
7464 scroll_data.dy = dy;
7466 adjust_allocation_recurse (widget, &scroll_data);
7470 gtk_text_view_value_changed (GtkAdjustment *adj,
7471 GtkTextView *text_view)
7473 GtkTextViewPrivate *priv;
7479 priv = text_view->priv;
7481 /* Note that we oddly call this function with adj == NULL
7485 priv->onscreen_validated = FALSE;
7487 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7488 adj == priv->hadjustment ? "hadj" : adj == priv->vadjustment ? "vadj" : "none",
7489 adj ? adj->value : 0.0));
7491 if (adj == priv->hadjustment)
7493 dx = priv->xoffset - (gint)adj->value;
7494 priv->xoffset = adj->value;
7496 /* If the change is due to a size change we need
7497 * to invalidate the entire text window because there might be
7498 * right-aligned or centered text
7500 if (priv->width_changed)
7502 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7503 gdk_window_invalidate_rect (priv->text_window->bin_window, NULL, FALSE);
7505 priv->width_changed = FALSE;
7508 else if (adj == priv->vadjustment)
7510 dy = priv->yoffset - (gint)adj->value;
7511 priv->yoffset = adj->value;
7515 gtk_text_layout_get_line_at_y (priv->layout, &iter, adj->value, &line_top);
7517 gtk_text_buffer_move_mark (get_buffer (text_view), priv->first_para_mark, &iter);
7519 priv->first_para_pixels = adj->value - line_top;
7523 if (dx != 0 || dy != 0)
7527 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7531 if (priv->left_window)
7532 text_window_scroll (priv->left_window, 0, dy);
7533 if (priv->right_window)
7534 text_window_scroll (priv->right_window, 0, dy);
7539 if (priv->top_window)
7540 text_window_scroll (priv->top_window, dx, 0);
7541 if (priv->bottom_window)
7542 text_window_scroll (priv->bottom_window, dx, 0);
7545 /* It looks nicer to scroll the main area last, because
7546 * it takes a while, and making the side areas update
7547 * afterward emphasizes the slowness of scrolling the
7550 text_window_scroll (priv->text_window, dx, dy);
7553 /* Children are now "moved" in the text window, poke
7554 * into widget->allocation for each child
7556 tmp_list = priv->children;
7557 while (tmp_list != NULL)
7559 GtkTextViewChild *child = tmp_list->data;
7562 adjust_allocation (child->widget, dx, dy);
7564 tmp_list = g_slist_next (tmp_list);
7568 /* This could result in invalidation, which would install the
7569 * first_validate_idle, which would validate onscreen;
7570 * but we're going to go ahead and validate here, so
7571 * first_validate_idle shouldn't have anything to do.
7573 gtk_text_view_update_layout_width (text_view);
7575 /* We also update the IM spot location here, since the IM context
7576 * might do something that leads to validation.
7578 gtk_text_view_update_im_spot_location (text_view);
7580 /* note that validation of onscreen could invoke this function
7581 * recursively, by scrolling to maintain first_para, or in response
7582 * to updating the layout width, however there is no problem with
7583 * that, or shouldn't be.
7585 gtk_text_view_validate_onscreen (text_view);
7587 /* process exposes */
7588 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7590 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7592 if (priv->left_window)
7593 gdk_window_process_updates (priv->left_window->bin_window, TRUE);
7595 if (priv->right_window)
7596 gdk_window_process_updates (priv->right_window->bin_window, TRUE);
7598 if (priv->top_window)
7599 gdk_window_process_updates (priv->top_window->bin_window, TRUE);
7601 if (priv->bottom_window)
7602 gdk_window_process_updates (priv->bottom_window->bin_window, TRUE);
7604 gdk_window_process_updates (priv->text_window->bin_window, TRUE);
7607 /* If this got installed, get rid of it, it's just a waste of time. */
7608 if (priv->first_validate_idle != 0)
7610 g_source_remove (priv->first_validate_idle);
7611 priv->first_validate_idle = 0;
7614 /* Finally we update the IM cursor location again, to ensure any
7615 * changes made by the validation are pushed through.
7617 gtk_text_view_update_im_spot_location (text_view);
7619 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7623 gtk_text_view_commit_handler (GtkIMContext *context,
7625 GtkTextView *text_view)
7627 gtk_text_view_commit_text (text_view, str);
7631 gtk_text_view_commit_text (GtkTextView *text_view,
7634 GtkTextViewPrivate *priv;
7635 gboolean had_selection;
7637 priv = text_view->priv;
7639 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7641 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7644 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7647 if (!strcmp (str, "\n"))
7649 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7652 gtk_widget_error_bell (GTK_WIDGET (text_view));
7657 if (!had_selection && priv->overwrite_mode)
7661 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7663 gtk_text_buffer_get_insert (get_buffer (text_view)));
7664 if (!gtk_text_iter_ends_line (&insert))
7665 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7668 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7671 gtk_widget_error_bell (GTK_WIDGET (text_view));
7675 gtk_text_buffer_end_user_action (get_buffer (text_view));
7677 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7678 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7679 gtk_text_view_scroll_mark_onscreen (text_view,
7680 gtk_text_buffer_get_insert (get_buffer (text_view)));
7684 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7685 GtkTextView *text_view)
7687 GtkTextViewPrivate *priv;
7689 PangoAttrList *attrs;
7693 priv = text_view->priv;
7695 gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter,
7696 gtk_text_buffer_get_insert (priv->buffer));
7698 /* Keypress events are passed to input method even if cursor position is
7699 * not editable; so beep here if it's multi-key input sequence, input
7700 * method will be reset in key-press-event handler.
7702 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7704 if (str && str[0] && !gtk_text_iter_can_insert (&iter, priv->editable))
7706 gtk_widget_error_bell (GTK_WIDGET (text_view));
7710 g_signal_emit (text_view, signals[PREEDIT_CHANGED], 0, str);
7713 gtk_text_layout_set_preedit_string (priv->layout, str, attrs, cursor_pos);
7714 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
7715 gtk_text_view_scroll_mark_onscreen (text_view,
7716 gtk_text_buffer_get_insert (get_buffer (text_view)));
7719 pango_attr_list_unref (attrs);
7724 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7725 GtkTextView *text_view)
7732 gtk_text_buffer_get_iter_at_mark (text_view->priv->buffer, &start,
7733 gtk_text_buffer_get_insert (text_view->priv->buffer));
7736 pos = gtk_text_iter_get_line_index (&start);
7737 gtk_text_iter_set_line_offset (&start, 0);
7738 gtk_text_iter_forward_to_line_end (&end);
7740 text = gtk_text_iter_get_slice (&start, &end);
7741 gtk_im_context_set_surrounding (context, text, -1, pos);
7748 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7751 GtkTextView *text_view)
7753 GtkTextViewPrivate *priv;
7757 priv = text_view->priv;
7759 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start,
7760 gtk_text_buffer_get_insert (priv->buffer));
7763 gtk_text_iter_forward_chars (&start, offset);
7764 gtk_text_iter_forward_chars (&end, offset + n_chars);
7766 gtk_text_buffer_delete_interactive (priv->buffer, &start, &end,
7773 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7774 const GtkTextIter *location,
7778 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7779 gboolean need_reset = FALSE;
7781 if (mark == gtk_text_buffer_get_insert (buffer))
7783 text_view->priv->virtual_cursor_x = -1;
7784 text_view->priv->virtual_cursor_y = -1;
7785 gtk_text_view_update_im_spot_location (text_view);
7788 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7794 gtk_text_view_reset_im_context (text_view);
7798 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7799 const GParamSpec *pspec,
7802 GtkWidget *widget = GTK_WIDGET (data);
7803 GtkTargetList *view_list;
7804 GtkTargetList *buffer_list;
7807 view_list = gtk_drag_dest_get_target_list (widget);
7808 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7811 gtk_target_list_ref (view_list);
7813 view_list = gtk_target_list_new (NULL, 0);
7815 list = view_list->list;
7818 GtkTargetPair *pair = list->data;
7820 list = g_list_next (list); /* get next element before removing */
7822 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7823 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7825 gtk_target_list_remove (view_list, pair->target);
7829 for (list = buffer_list->list; list; list = g_list_next (list))
7831 GtkTargetPair *pair = list->data;
7833 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7836 gtk_drag_dest_set_target_list (widget, view_list);
7837 gtk_target_list_unref (view_list);
7841 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7846 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7847 gtk_text_buffer_get_insert (get_buffer (text_view)));
7849 gtk_text_layout_get_cursor_locations (text_view->priv->layout, &insert, pos, NULL);
7853 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7854 GtkTextIter *cursor,
7858 GtkTextViewPrivate *priv;
7862 priv = text_view->priv;
7867 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7868 gtk_text_buffer_get_insert (get_buffer (text_view)));
7870 if ((x && priv->virtual_cursor_x == -1) ||
7871 (y && priv->virtual_cursor_y == -1))
7872 gtk_text_layout_get_cursor_locations (priv->layout, &insert, &pos, NULL);
7876 if (priv->virtual_cursor_x != -1)
7877 *x = priv->virtual_cursor_x;
7884 if (priv->virtual_cursor_x != -1)
7885 *y = priv->virtual_cursor_y;
7887 *y = pos.y + pos.height / 2;
7892 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7898 if (!text_view->priv->layout)
7901 if (x == -1 || y == -1)
7902 gtk_text_view_get_cursor_location (text_view, &pos);
7904 text_view->priv->virtual_cursor_x = (x == -1) ? pos.x : x;
7905 text_view->priv->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7908 /* Quick hack of a popup menu
7911 activate_cb (GtkWidget *menuitem,
7912 GtkTextView *text_view)
7914 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7915 g_signal_emit_by_name (text_view, signal);
7919 append_action_signal (GtkTextView *text_view,
7921 const gchar *stock_id,
7922 const gchar *signal,
7925 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7927 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7928 g_signal_connect (menuitem, "activate",
7929 G_CALLBACK (activate_cb), text_view);
7931 gtk_widget_set_sensitive (menuitem, sensitive);
7933 gtk_widget_show (menuitem);
7934 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7938 gtk_text_view_select_all (GtkWidget *widget,
7941 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
7942 GtkTextBuffer *buffer;
7943 GtkTextIter start_iter, end_iter, insert;
7945 buffer = text_view->priv->buffer;
7948 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
7949 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
7953 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
7954 gtk_text_buffer_get_insert (buffer));
7955 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
7960 select_all_cb (GtkWidget *menuitem,
7961 GtkTextView *text_view)
7963 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
7967 delete_cb (GtkTextView *text_view)
7969 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7970 text_view->priv->editable);
7974 popup_menu_detach (GtkWidget *attach_widget,
7977 GTK_TEXT_VIEW (attach_widget)->priv->popup_menu = NULL;
7981 popup_position_func (GtkMenu *menu,
7987 GtkAllocation allocation;
7988 GtkTextView *text_view;
7990 GdkRectangle cursor_rect;
7991 GdkRectangle onscreen_rect;
7992 gint root_x, root_y;
7997 GdkRectangle monitor;
7999 text_view = GTK_TEXT_VIEW (user_data);
8000 widget = GTK_WIDGET (text_view);
8002 g_return_if_fail (gtk_widget_get_realized (widget));
8004 screen = gtk_widget_get_screen (widget);
8006 gdk_window_get_origin (gtk_widget_get_window (widget),
8009 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
8011 gtk_text_buffer_get_insert (get_buffer (text_view)));
8013 gtk_text_view_get_iter_location (text_view,
8017 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
8019 gtk_widget_get_preferred_size (text_view->priv->popup_menu,
8022 gtk_widget_get_allocation (widget, &allocation);
8024 /* can't use rectangle_intersect since cursor rect can have 0 width */
8025 if (cursor_rect.x >= onscreen_rect.x &&
8026 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
8027 cursor_rect.y >= onscreen_rect.y &&
8028 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
8030 gtk_text_view_buffer_to_window_coords (text_view,
8031 GTK_TEXT_WINDOW_WIDGET,
8032 cursor_rect.x, cursor_rect.y,
8033 &cursor_rect.x, &cursor_rect.y);
8035 *x = root_x + cursor_rect.x + cursor_rect.width;
8036 *y = root_y + cursor_rect.y + cursor_rect.height;
8040 /* Just center the menu, since cursor is offscreen. */
8041 *x = root_x + (allocation.width / 2 - req.width / 2);
8042 *y = root_y + (allocation.height / 2 - req.height / 2);
8046 *x = CLAMP (*x, root_x, (root_x + allocation.width));
8047 *y = CLAMP (*y, root_y, (root_y + allocation.height));
8049 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
8050 gtk_menu_set_monitor (menu, monitor_num);
8051 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8053 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
8054 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
8061 GtkTextView *text_view;
8067 range_contains_editable_text (const GtkTextIter *start,
8068 const GtkTextIter *end,
8069 gboolean default_editability)
8071 GtkTextIter iter = *start;
8073 while (gtk_text_iter_compare (&iter, end) < 0)
8075 if (gtk_text_iter_editable (&iter, default_editability))
8078 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
8085 unichar_chosen_func (const char *text,
8088 GtkTextView *text_view = GTK_TEXT_VIEW (data);
8090 gtk_text_view_commit_text (text_view, text);
8094 popup_targets_received (GtkClipboard *clipboard,
8095 GtkSelectionData *data,
8098 PopupInfo *info = user_data;
8099 GtkTextView *text_view;
8100 GtkTextViewPrivate *priv;
8102 text_view = info->text_view;
8103 priv = text_view->priv;
8105 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
8107 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
8108 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
8110 gboolean clipboard_contains_text;
8111 GtkWidget *menuitem;
8113 gboolean have_selection;
8114 gboolean can_insert;
8116 GtkTextIter sel_start, sel_end;
8117 gboolean show_input_method_menu;
8118 gboolean show_unicode_menu;
8120 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8122 if (priv->popup_menu)
8123 gtk_widget_destroy (priv->popup_menu);
8125 priv->popup_menu = gtk_menu_new ();
8127 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
8128 GTK_WIDGET (text_view),
8131 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
8132 &sel_start, &sel_end);
8134 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
8136 gtk_text_buffer_get_insert (get_buffer (text_view)));
8138 can_insert = gtk_text_iter_can_insert (&iter, priv->editable);
8140 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8142 range_contains_editable_text (&sel_start, &sel_end,
8144 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8146 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8147 can_insert && clipboard_contains_text);
8149 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8150 gtk_widget_set_sensitive (menuitem,
8152 range_contains_editable_text (&sel_start, &sel_end,
8154 g_signal_connect_swapped (menuitem, "activate",
8155 G_CALLBACK (delete_cb), text_view);
8156 gtk_widget_show (menuitem);
8157 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8159 menuitem = gtk_separator_menu_item_new ();
8160 gtk_widget_show (menuitem);
8161 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8163 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8164 g_signal_connect (menuitem, "activate",
8165 G_CALLBACK (select_all_cb), text_view);
8166 gtk_widget_show (menuitem);
8167 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8169 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
8170 "gtk-show-input-method-menu", &show_input_method_menu,
8171 "gtk-show-unicode-menu", &show_unicode_menu,
8174 if (show_input_method_menu || show_unicode_menu)
8176 menuitem = gtk_separator_menu_item_new ();
8177 gtk_widget_show (menuitem);
8178 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8181 if (show_input_method_menu)
8183 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8184 gtk_widget_show (menuitem);
8185 gtk_widget_set_sensitive (menuitem, can_insert);
8187 submenu = gtk_menu_new ();
8188 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8189 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8191 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (priv->im_context),
8192 GTK_MENU_SHELL (submenu));
8195 if (show_unicode_menu)
8197 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8198 gtk_widget_show (menuitem);
8199 gtk_widget_set_sensitive (menuitem, can_insert);
8201 submenu = gtk_menu_new ();
8202 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8203 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8205 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8206 unichar_chosen_func,
8210 g_signal_emit (text_view,
8211 signals[POPULATE_POPUP],
8216 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
8218 info->button, info->time);
8221 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
8222 popup_position_func, text_view,
8223 0, gtk_get_current_event_time ());
8224 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
8228 g_object_unref (text_view);
8233 gtk_text_view_do_popup (GtkTextView *text_view,
8234 GdkEventButton *event)
8236 PopupInfo *info = g_new (PopupInfo, 1);
8238 /* In order to know what entries we should make sensitive, we
8239 * ask for the current targets of the clipboard, and when
8240 * we get them, then we actually pop up the menu.
8242 info->text_view = g_object_ref (text_view);
8246 info->button = event->button;
8247 info->time = event->time;
8252 info->time = gtk_get_current_event_time ();
8255 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
8256 GDK_SELECTION_CLIPBOARD),
8257 gdk_atom_intern_static_string ("TARGETS"),
8258 popup_targets_received,
8263 gtk_text_view_popup_menu (GtkWidget *widget)
8265 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
8269 /* Child GdkWindows */
8272 static GtkTextWindow*
8273 text_window_new (GtkTextWindowType type,
8276 gint height_request)
8280 win = g_new (GtkTextWindow, 1);
8283 win->widget = widget;
8285 win->bin_window = NULL;
8286 win->requisition.width = width_request;
8287 win->requisition.height = height_request;
8288 win->allocation.width = width_request;
8289 win->allocation.height = height_request;
8290 win->allocation.x = 0;
8291 win->allocation.y = 0;
8297 text_window_free (GtkTextWindow *win)
8300 text_window_unrealize (win);
8306 text_window_realize (GtkTextWindow *win,
8310 GdkWindowAttr attributes;
8311 gint attributes_mask;
8314 attributes.window_type = GDK_WINDOW_CHILD;
8315 attributes.x = win->allocation.x;
8316 attributes.y = win->allocation.y;
8317 attributes.width = win->allocation.width;
8318 attributes.height = win->allocation.height;
8319 attributes.wclass = GDK_INPUT_OUTPUT;
8320 attributes.visual = gtk_widget_get_visual (win->widget);
8321 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
8323 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
8325 window = gtk_widget_get_window (widget);
8327 win->window = gdk_window_new (window,
8328 &attributes, attributes_mask);
8330 gdk_window_show (win->window);
8331 gdk_window_set_user_data (win->window, win->widget);
8332 gdk_window_lower (win->window);
8336 attributes.width = win->allocation.width;
8337 attributes.height = win->allocation.height;
8338 attributes.event_mask = (GDK_EXPOSURE_MASK |
8340 GDK_KEY_PRESS_MASK |
8341 GDK_BUTTON_PRESS_MASK |
8342 GDK_BUTTON_RELEASE_MASK |
8343 GDK_POINTER_MOTION_MASK |
8344 GDK_POINTER_MOTION_HINT_MASK |
8345 gtk_widget_get_events (win->widget));
8347 win->bin_window = gdk_window_new (win->window,
8351 gdk_window_show (win->bin_window);
8352 gdk_window_set_user_data (win->bin_window, win->widget);
8354 if (win->type == GTK_TEXT_WINDOW_TEXT)
8356 if (gtk_widget_is_sensitive (widget))
8359 cursor = gdk_cursor_new_for_display (gdk_window_get_display (window),
8361 gdk_window_set_cursor (win->bin_window, cursor);
8362 gdk_cursor_unref (cursor);
8365 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->priv->im_context,
8369 gdk_window_set_background (win->bin_window,
8370 >k_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
8374 gdk_window_set_background (win->bin_window,
8375 >k_widget_get_style (widget)->bg[gtk_widget_get_state (widget)]);
8378 g_object_set_qdata (G_OBJECT (win->window),
8379 g_quark_from_static_string ("gtk-text-view-text-window"),
8382 g_object_set_qdata (G_OBJECT (win->bin_window),
8383 g_quark_from_static_string ("gtk-text-view-text-window"),
8388 text_window_unrealize (GtkTextWindow *win)
8390 if (win->type == GTK_TEXT_WINDOW_TEXT)
8392 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->priv->im_context,
8396 gdk_window_set_user_data (win->window, NULL);
8397 gdk_window_set_user_data (win->bin_window, NULL);
8398 gdk_window_destroy (win->bin_window);
8399 gdk_window_destroy (win->window);
8401 win->bin_window = NULL;
8405 text_window_size_allocate (GtkTextWindow *win,
8408 win->allocation = *rect;
8412 gdk_window_move_resize (win->window,
8414 rect->width, rect->height);
8416 gdk_window_resize (win->bin_window,
8417 rect->width, rect->height);
8422 text_window_scroll (GtkTextWindow *win,
8426 if (dx != 0 || dy != 0)
8428 gdk_window_scroll (win->bin_window, dx, dy);
8433 text_window_invalidate_rect (GtkTextWindow *win,
8436 GdkRectangle window_rect;
8438 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
8445 window_rect.width = rect->width;
8446 window_rect.height = rect->height;
8448 /* Adjust the rect as appropriate */
8452 case GTK_TEXT_WINDOW_TEXT:
8455 case GTK_TEXT_WINDOW_LEFT:
8456 case GTK_TEXT_WINDOW_RIGHT:
8458 window_rect.width = win->allocation.width;
8461 case GTK_TEXT_WINDOW_TOP:
8462 case GTK_TEXT_WINDOW_BOTTOM:
8464 window_rect.height = win->allocation.height;
8468 g_warning ("%s: bug!", G_STRFUNC);
8473 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
8477 cairo_t *cr = gdk_cairo_create (win->bin_window);
8478 gdk_cairo_rectangle (cr, &window_rect);
8479 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
8487 text_window_invalidate_cursors (GtkTextWindow *win)
8489 GtkTextView *text_view;
8490 GtkTextViewPrivate *priv;
8492 GdkRectangle strong;
8494 gboolean draw_arrow;
8495 gfloat cursor_aspect_ratio;
8499 text_view = GTK_TEXT_VIEW (win->widget);
8500 priv = text_view->priv;
8502 gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter,
8503 gtk_text_buffer_get_insert (priv->buffer));
8505 if (_gtk_text_layout_get_block_cursor (priv->layout, &strong))
8507 text_window_invalidate_rect (win, &strong);
8511 gtk_text_layout_get_cursor_locations (priv->layout, &iter,
8514 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
8515 * ignoring the text direction be exposing both sides of the cursor
8518 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
8520 gtk_widget_style_get (win->widget,
8521 "cursor-aspect-ratio", &cursor_aspect_ratio,
8524 stem_width = strong.height * cursor_aspect_ratio + 1;
8525 arrow_width = stem_width + 1;
8527 strong.width = stem_width;
8529 /* round up to the next even number */
8533 strong.x -= stem_width / 2;
8534 strong.width += stem_width;
8538 strong.x -= arrow_width;
8539 strong.width += arrow_width * 2;
8542 text_window_invalidate_rect (win, &strong);
8544 if (draw_arrow) /* == have weak */
8546 stem_width = weak.height * cursor_aspect_ratio + 1;
8547 arrow_width = stem_width + 1;
8549 weak.width = stem_width;
8551 /* round up to the next even number */
8555 weak.x -= stem_width / 2;
8556 weak.width += stem_width;
8558 weak.x -= arrow_width;
8559 weak.width += arrow_width * 2;
8561 text_window_invalidate_rect (win, &weak);
8566 text_window_get_width (GtkTextWindow *win)
8568 return win->allocation.width;
8572 text_window_get_height (GtkTextWindow *win)
8574 return win->allocation.height;
8581 * gtk_text_view_get_window:
8582 * @text_view: a #GtkTextView
8583 * @win: window to get
8585 * Retrieves the #GdkWindow corresponding to an area of the text view;
8586 * possible windows include the overall widget window, child windows
8587 * on the left, right, top, bottom, and the window that displays the
8588 * text buffer. Windows are %NULL and nonexistent if their width or
8589 * height is 0, and are nonexistent before the widget has been
8592 * Return value: (transfer none): a #GdkWindow, or %NULL
8595 gtk_text_view_get_window (GtkTextView *text_view,
8596 GtkTextWindowType win)
8598 GtkTextViewPrivate *priv = text_view->priv;
8600 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8604 case GTK_TEXT_WINDOW_WIDGET:
8605 return gtk_widget_get_window (GTK_WIDGET (text_view));
8608 case GTK_TEXT_WINDOW_TEXT:
8609 return priv->text_window->bin_window;
8612 case GTK_TEXT_WINDOW_LEFT:
8613 if (priv->left_window)
8614 return priv->left_window->bin_window;
8619 case GTK_TEXT_WINDOW_RIGHT:
8620 if (priv->right_window)
8621 return priv->right_window->bin_window;
8626 case GTK_TEXT_WINDOW_TOP:
8627 if (priv->top_window)
8628 return priv->top_window->bin_window;
8633 case GTK_TEXT_WINDOW_BOTTOM:
8634 if (priv->bottom_window)
8635 return priv->bottom_window->bin_window;
8640 case GTK_TEXT_WINDOW_PRIVATE:
8641 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8646 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8651 * gtk_text_view_get_window_type:
8652 * @text_view: a #GtkTextView
8653 * @window: a window type
8655 * Usually used to find out which window an event corresponds to.
8656 * If you connect to an event signal on @text_view, this function
8657 * should be called on <literal>event->window</literal> to
8658 * see which window it was.
8660 * Return value: the window type.
8663 gtk_text_view_get_window_type (GtkTextView *text_view,
8668 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8669 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8671 if (window == gtk_widget_get_window (GTK_WIDGET (text_view)))
8672 return GTK_TEXT_WINDOW_WIDGET;
8674 win = g_object_get_qdata (G_OBJECT (window),
8675 g_quark_try_string ("gtk-text-view-text-window"));
8681 return GTK_TEXT_WINDOW_PRIVATE;
8686 buffer_to_widget (GtkTextView *text_view,
8692 GtkTextViewPrivate *priv = text_view->priv;
8696 *window_x = buffer_x - priv->xoffset;
8697 *window_x += priv->text_window->allocation.x;
8702 *window_y = buffer_y - priv->yoffset;
8703 *window_y += priv->text_window->allocation.y;
8708 widget_to_text_window (GtkTextWindow *win,
8715 *window_x = widget_x - win->allocation.x;
8718 *window_y = widget_y - win->allocation.y;
8722 buffer_to_text_window (GtkTextView *text_view,
8731 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8732 "for a nonexistent or private child window of GtkTextView");
8736 buffer_to_widget (text_view,
8738 window_x, window_y);
8740 widget_to_text_window (win,
8741 window_x ? *window_x : 0,
8742 window_y ? *window_y : 0,
8748 * gtk_text_view_buffer_to_window_coords:
8749 * @text_view: a #GtkTextView
8750 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8751 * @buffer_x: buffer x coordinate
8752 * @buffer_y: buffer y coordinate
8753 * @window_x: (out) (allow-none): window x coordinate return location or %NULL
8754 * @window_y: (out) (allow-none): window y coordinate return location or %NULL
8756 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8757 * @win, and stores the result in (@window_x, @window_y).
8759 * Note that you can't convert coordinates for a nonexisting window (see
8760 * gtk_text_view_set_border_window_size()).
8763 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8764 GtkTextWindowType win,
8770 GtkTextViewPrivate *priv = text_view->priv;
8772 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8776 case GTK_TEXT_WINDOW_WIDGET:
8777 buffer_to_widget (text_view,
8779 window_x, window_y);
8782 case GTK_TEXT_WINDOW_TEXT:
8784 *window_x = buffer_x - priv->xoffset;
8786 *window_y = buffer_y - priv->yoffset;
8789 case GTK_TEXT_WINDOW_LEFT:
8790 buffer_to_text_window (text_view,
8793 window_x, window_y);
8796 case GTK_TEXT_WINDOW_RIGHT:
8797 buffer_to_text_window (text_view,
8800 window_x, window_y);
8803 case GTK_TEXT_WINDOW_TOP:
8804 buffer_to_text_window (text_view,
8807 window_x, window_y);
8810 case GTK_TEXT_WINDOW_BOTTOM:
8811 buffer_to_text_window (text_view,
8812 priv->bottom_window,
8814 window_x, window_y);
8817 case GTK_TEXT_WINDOW_PRIVATE:
8818 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8822 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8828 widget_to_buffer (GtkTextView *text_view,
8834 GtkTextViewPrivate *priv = text_view->priv;
8838 *buffer_x = widget_x + priv->xoffset;
8839 *buffer_x -= priv->text_window->allocation.x;
8844 *buffer_y = widget_y + priv->yoffset;
8845 *buffer_y -= priv->text_window->allocation.y;
8850 text_window_to_widget (GtkTextWindow *win,
8857 *widget_x = window_x + win->allocation.x;
8860 *widget_y = window_y + win->allocation.y;
8864 text_window_to_buffer (GtkTextView *text_view,
8873 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8874 "coordinates for a nonexistent child window.");
8878 text_window_to_widget (win,
8884 widget_to_buffer (text_view,
8885 buffer_x ? *buffer_x : 0,
8886 buffer_y ? *buffer_y : 0,
8892 * gtk_text_view_window_to_buffer_coords:
8893 * @text_view: a #GtkTextView
8894 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8895 * @window_x: window x coordinate
8896 * @window_y: window y coordinate
8897 * @buffer_x: (out) (allow-none): buffer x coordinate return location or %NULL
8898 * @buffer_y: (out) (allow-none): buffer y coordinate return location or %NULL
8900 * Converts coordinates on the window identified by @win to buffer
8901 * coordinates, storing the result in (@buffer_x,@buffer_y).
8903 * Note that you can't convert coordinates for a nonexisting window (see
8904 * gtk_text_view_set_border_window_size()).
8907 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8908 GtkTextWindowType win,
8914 GtkTextViewPrivate *priv = text_view->priv;
8916 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8920 case GTK_TEXT_WINDOW_WIDGET:
8921 widget_to_buffer (text_view,
8923 buffer_x, buffer_y);
8926 case GTK_TEXT_WINDOW_TEXT:
8928 *buffer_x = window_x + priv->xoffset;
8930 *buffer_y = window_y + priv->yoffset;
8933 case GTK_TEXT_WINDOW_LEFT:
8934 text_window_to_buffer (text_view,
8937 buffer_x, buffer_y);
8940 case GTK_TEXT_WINDOW_RIGHT:
8941 text_window_to_buffer (text_view,
8944 buffer_x, buffer_y);
8947 case GTK_TEXT_WINDOW_TOP:
8948 text_window_to_buffer (text_view,
8951 buffer_x, buffer_y);
8954 case GTK_TEXT_WINDOW_BOTTOM:
8955 text_window_to_buffer (text_view,
8956 priv->bottom_window,
8958 buffer_x, buffer_y);
8961 case GTK_TEXT_WINDOW_PRIVATE:
8962 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8966 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8972 set_window_width (GtkTextView *text_view,
8974 GtkTextWindowType type,
8975 GtkTextWindow **winp)
8981 text_window_free (*winp);
8983 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8990 *winp = text_window_new (type,
8991 GTK_WIDGET (text_view),
8993 /* if the widget is already realized we need to realize the child manually */
8994 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
8995 text_window_realize (*winp, GTK_WIDGET (text_view));
8999 if ((*winp)->requisition.width == width)
9002 (*winp)->requisition.width = width;
9005 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9011 set_window_height (GtkTextView *text_view,
9013 GtkTextWindowType type,
9014 GtkTextWindow **winp)
9020 text_window_free (*winp);
9022 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9029 *winp = text_window_new (type,
9030 GTK_WIDGET (text_view),
9033 /* if the widget is already realized we need to realize the child manually */
9034 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9035 text_window_realize (*winp, GTK_WIDGET (text_view));
9039 if ((*winp)->requisition.height == height)
9042 (*winp)->requisition.height = height;
9045 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9050 * gtk_text_view_set_border_window_size:
9051 * @text_view: a #GtkTextView
9052 * @type: window to affect
9053 * @size: width or height of the window
9055 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
9056 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
9057 * Automatically destroys the corresponding window if the size is set
9058 * to 0, and creates the window if the size is set to non-zero. This
9059 * function can only be used for the "border windows," it doesn't work
9060 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
9061 * #GTK_TEXT_WINDOW_PRIVATE.
9064 gtk_text_view_set_border_window_size (GtkTextView *text_view,
9065 GtkTextWindowType type,
9068 GtkTextViewPrivate *priv = text_view->priv;
9070 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9071 g_return_if_fail (size >= 0);
9075 case GTK_TEXT_WINDOW_LEFT:
9076 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
9077 &priv->left_window);
9080 case GTK_TEXT_WINDOW_RIGHT:
9081 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
9082 &priv->right_window);
9085 case GTK_TEXT_WINDOW_TOP:
9086 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
9090 case GTK_TEXT_WINDOW_BOTTOM:
9091 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
9092 &priv->bottom_window);
9096 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
9102 * gtk_text_view_get_border_window_size:
9103 * @text_view: a #GtkTextView
9104 * @type: window to return size from
9106 * Gets the width of the specified border window. See
9107 * gtk_text_view_set_border_window_size().
9109 * Return value: width of window
9112 gtk_text_view_get_border_window_size (GtkTextView *text_view,
9113 GtkTextWindowType type)
9115 GtkTextViewPrivate *priv = text_view->priv;
9117 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
9121 case GTK_TEXT_WINDOW_LEFT:
9122 if (priv->left_window)
9123 return priv->left_window->requisition.width;
9126 case GTK_TEXT_WINDOW_RIGHT:
9127 if (priv->right_window)
9128 return priv->right_window->requisition.width;
9131 case GTK_TEXT_WINDOW_TOP:
9132 if (priv->top_window)
9133 return priv->top_window->requisition.height;
9136 case GTK_TEXT_WINDOW_BOTTOM:
9137 if (priv->bottom_window)
9138 return priv->bottom_window->requisition.height;
9142 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
9153 static GtkTextViewChild*
9154 text_view_child_new_anchored (GtkWidget *child,
9155 GtkTextChildAnchor *anchor,
9156 GtkTextLayout *layout)
9158 GtkTextViewChild *vc;
9160 vc = g_new (GtkTextViewChild, 1);
9162 vc->type = GTK_TEXT_WINDOW_PRIVATE;
9164 vc->anchor = anchor;
9166 vc->from_top_of_line = 0;
9167 vc->from_left_of_buffer = 0;
9169 g_object_ref (vc->widget);
9170 g_object_ref (vc->anchor);
9172 g_object_set_data (G_OBJECT (child),
9173 I_("gtk-text-view-child"),
9176 gtk_text_child_anchor_register_child (anchor, child, layout);
9181 static GtkTextViewChild*
9182 text_view_child_new_window (GtkWidget *child,
9183 GtkTextWindowType type,
9187 GtkTextViewChild *vc;
9189 vc = g_new (GtkTextViewChild, 1);
9194 vc->from_top_of_line = 0;
9195 vc->from_left_of_buffer = 0;
9197 g_object_ref (vc->widget);
9203 g_object_set_data (G_OBJECT (child),
9204 I_("gtk-text-view-child"),
9211 text_view_child_free (GtkTextViewChild *child)
9213 g_object_set_data (G_OBJECT (child->widget),
9214 I_("gtk-text-view-child"), NULL);
9218 gtk_text_child_anchor_unregister_child (child->anchor,
9220 g_object_unref (child->anchor);
9223 g_object_unref (child->widget);
9229 text_view_child_set_parent_window (GtkTextView *text_view,
9230 GtkTextViewChild *vc)
9233 gtk_widget_set_parent_window (vc->widget,
9234 text_view->priv->text_window->bin_window);
9238 window = gtk_text_view_get_window (text_view,
9240 gtk_widget_set_parent_window (vc->widget, window);
9245 add_child (GtkTextView *text_view,
9246 GtkTextViewChild *vc)
9248 text_view->priv->children = g_slist_prepend (text_view->priv->children,
9251 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9252 text_view_child_set_parent_window (text_view, vc);
9254 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
9258 * gtk_text_view_add_child_at_anchor:
9259 * @text_view: a #GtkTextView
9260 * @child: a #GtkWidget
9261 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
9263 * Adds a child widget in the text buffer, at the given @anchor.
9266 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
9268 GtkTextChildAnchor *anchor)
9270 GtkTextViewChild *vc;
9272 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9273 g_return_if_fail (GTK_IS_WIDGET (child));
9274 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
9275 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
9277 gtk_text_view_ensure_layout (text_view);
9279 vc = text_view_child_new_anchored (child, anchor,
9280 text_view->priv->layout);
9282 add_child (text_view, vc);
9284 g_assert (vc->widget == child);
9285 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9289 * gtk_text_view_add_child_in_window:
9290 * @text_view: a #GtkTextView
9291 * @child: a #GtkWidget
9292 * @which_window: which window the child should appear in
9293 * @xpos: X position of child in window coordinates
9294 * @ypos: Y position of child in window coordinates
9296 * Adds a child at fixed coordinates in one of the text widget's
9297 * windows. The window must have nonzero size (see
9298 * gtk_text_view_set_border_window_size()). Note that the child
9299 * coordinates are given relative to the #GdkWindow in question, and
9300 * that these coordinates have no sane relationship to scrolling. When
9301 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
9302 * irrelevant, the child floats above all scrollable areas. But when
9303 * placing a child in one of the scrollable windows (border windows or
9304 * text window), you'll need to compute the child's correct position
9305 * in buffer coordinates any time scrolling occurs or buffer changes
9306 * occur, and then call gtk_text_view_move_child() to update the
9307 * child's position. Unfortunately there's no good way to detect that
9308 * scrolling has occurred, using the current API; a possible hack
9309 * would be to update all child positions when the scroll adjustments
9310 * change or the text buffer changes. See bug 64518 on
9311 * bugzilla.gnome.org for status of fixing this issue.
9314 gtk_text_view_add_child_in_window (GtkTextView *text_view,
9316 GtkTextWindowType which_window,
9320 GtkTextViewChild *vc;
9322 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9323 g_return_if_fail (GTK_IS_WIDGET (child));
9324 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
9326 vc = text_view_child_new_window (child, which_window,
9329 add_child (text_view, vc);
9331 g_assert (vc->widget == child);
9332 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9336 * gtk_text_view_move_child:
9337 * @text_view: a #GtkTextView
9338 * @child: child widget already added to the text view
9339 * @xpos: new X position in window coordinates
9340 * @ypos: new Y position in window coordinates
9342 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
9345 gtk_text_view_move_child (GtkTextView *text_view,
9350 GtkTextViewChild *vc;
9352 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9353 g_return_if_fail (GTK_IS_WIDGET (child));
9354 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9356 vc = g_object_get_data (G_OBJECT (child),
9357 "gtk-text-view-child");
9359 g_assert (vc != NULL);
9361 if (vc->x == xpos &&
9368 if (gtk_widget_get_visible (child) &&
9369 gtk_widget_get_visible (GTK_WIDGET (text_view)))
9370 gtk_widget_queue_resize (child);
9374 /* Iterator operations */
9377 * gtk_text_view_forward_display_line:
9378 * @text_view: a #GtkTextView
9379 * @iter: a #GtkTextIter
9381 * Moves the given @iter forward by one display (wrapped) line.
9382 * A display line is different from a paragraph. Paragraphs are
9383 * separated by newlines or other paragraph separator characters.
9384 * Display lines are created by line-wrapping a paragraph. If
9385 * wrapping is turned off, display lines and paragraphs will be the
9386 * same. Display lines are divided differently for each view, since
9387 * they depend on the view's width; paragraphs are the same in all
9388 * views, since they depend on the contents of the #GtkTextBuffer.
9390 * Return value: %TRUE if @iter was moved and is not on the end iterator
9393 gtk_text_view_forward_display_line (GtkTextView *text_view,
9396 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9397 g_return_val_if_fail (iter != NULL, FALSE);
9399 gtk_text_view_ensure_layout (text_view);
9401 return gtk_text_layout_move_iter_to_next_line (text_view->priv->layout, iter);
9405 * gtk_text_view_backward_display_line:
9406 * @text_view: a #GtkTextView
9407 * @iter: a #GtkTextIter
9409 * Moves the given @iter backward by one display (wrapped) line.
9410 * A display line is different from a paragraph. Paragraphs are
9411 * separated by newlines or other paragraph separator characters.
9412 * Display lines are created by line-wrapping a paragraph. If
9413 * wrapping is turned off, display lines and paragraphs will be the
9414 * same. Display lines are divided differently for each view, since
9415 * they depend on the view's width; paragraphs are the same in all
9416 * views, since they depend on the contents of the #GtkTextBuffer.
9418 * Return value: %TRUE if @iter was moved and is not on the end iterator
9421 gtk_text_view_backward_display_line (GtkTextView *text_view,
9424 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9425 g_return_val_if_fail (iter != NULL, FALSE);
9427 gtk_text_view_ensure_layout (text_view);
9429 return gtk_text_layout_move_iter_to_previous_line (text_view->priv->layout, iter);
9433 * gtk_text_view_forward_display_line_end:
9434 * @text_view: a #GtkTextView
9435 * @iter: a #GtkTextIter
9437 * Moves the given @iter forward to the next display line end.
9438 * A display line is different from a paragraph. Paragraphs are
9439 * separated by newlines or other paragraph separator characters.
9440 * Display lines are created by line-wrapping a paragraph. If
9441 * wrapping is turned off, display lines and paragraphs will be the
9442 * same. Display lines are divided differently for each view, since
9443 * they depend on the view's width; paragraphs are the same in all
9444 * views, since they depend on the contents of the #GtkTextBuffer.
9446 * Return value: %TRUE if @iter was moved and is not on the end iterator
9449 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
9452 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9453 g_return_val_if_fail (iter != NULL, FALSE);
9455 gtk_text_view_ensure_layout (text_view);
9457 return gtk_text_layout_move_iter_to_line_end (text_view->priv->layout, iter, 1);
9461 * gtk_text_view_backward_display_line_start:
9462 * @text_view: a #GtkTextView
9463 * @iter: a #GtkTextIter
9465 * Moves the given @iter backward to the next display line start.
9466 * A display line is different from a paragraph. Paragraphs are
9467 * separated by newlines or other paragraph separator characters.
9468 * Display lines are created by line-wrapping a paragraph. If
9469 * wrapping is turned off, display lines and paragraphs will be the
9470 * same. Display lines are divided differently for each view, since
9471 * they depend on the view's width; paragraphs are the same in all
9472 * views, since they depend on the contents of the #GtkTextBuffer.
9474 * Return value: %TRUE if @iter was moved and is not on the end iterator
9477 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
9480 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9481 g_return_val_if_fail (iter != NULL, FALSE);
9483 gtk_text_view_ensure_layout (text_view);
9485 return gtk_text_layout_move_iter_to_line_end (text_view->priv->layout, iter, -1);
9489 * gtk_text_view_starts_display_line:
9490 * @text_view: a #GtkTextView
9491 * @iter: a #GtkTextIter
9493 * Determines whether @iter is at the start of a display line.
9494 * See gtk_text_view_forward_display_line() for an explanation of
9495 * display lines vs. paragraphs.
9497 * Return value: %TRUE if @iter begins a wrapped line
9500 gtk_text_view_starts_display_line (GtkTextView *text_view,
9501 const GtkTextIter *iter)
9503 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9504 g_return_val_if_fail (iter != NULL, FALSE);
9506 gtk_text_view_ensure_layout (text_view);
9508 return gtk_text_layout_iter_starts_line (text_view->priv->layout, iter);
9512 * gtk_text_view_move_visually:
9513 * @text_view: a #GtkTextView
9514 * @iter: a #GtkTextIter
9515 * @count: number of characters to move (negative moves left,
9516 * positive moves right)
9518 * Move the iterator a given number of characters visually, treating
9519 * it as the strong cursor position. If @count is positive, then the
9520 * new strong cursor position will be @count positions to the right of
9521 * the old cursor position. If @count is negative then the new strong
9522 * cursor position will be @count positions to the left of the old
9525 * In the presence of bi-directional text, the correspondence
9526 * between logical and visual order will depend on the direction
9527 * of the current run, and there may be jumps when the cursor
9528 * is moved off of the end of a run.
9530 * Return value: %TRUE if @iter moved and is not on the end iterator
9533 gtk_text_view_move_visually (GtkTextView *text_view,
9537 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9538 g_return_val_if_fail (iter != NULL, FALSE);
9540 gtk_text_view_ensure_layout (text_view);
9542 return gtk_text_layout_move_iter_visually (text_view->priv->layout, iter, count);