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 "gtksizerequest.h"
44 #include "gtktextbufferrichtext.h"
45 #include "gtktextdisplay.h"
46 #include "gtktextview.h"
47 #include "gtkimmulticontext.h"
48 #include "gdk/gdkkeysyms.h"
49 #include "gtkprivate.h"
50 #include "gtktextutil.h"
51 #include "gtkwindow.h"
54 /* How scrolling, validation, exposes, etc. work.
56 * The expose_event handler has the invariant that the onscreen lines
57 * have been validated.
59 * There are two ways that onscreen lines can become invalid. The first
60 * is to change which lines are onscreen. This happens when the value
61 * of a scroll adjustment changes. So the code path begins in
62 * gtk_text_view_value_changed() and goes like this:
63 * - gdk_window_scroll() to reflect the new adjustment value
64 * - validate the lines that were moved onscreen
65 * - gdk_window_process_updates() to handle the exposes immediately
67 * The second way is that you get the "invalidated" signal from the layout,
68 * indicating that lines have become invalid. This code path begins in
69 * invalidated_handler() and goes like this:
70 * - install high-priority idle which does the rest of the steps
71 * - if a scroll is pending from scroll_to_mark(), do the scroll,
72 * jumping to the gtk_text_view_value_changed() code path
73 * - otherwise, validate the onscreen lines
74 * - DO NOT process updates
76 * In both cases, validating the onscreen lines can trigger a scroll
77 * due to maintaining the first_para on the top of the screen.
78 * If validation triggers a scroll, we jump to the top of the code path
79 * for value_changed, and bail out of the current code path.
81 * Also, in size_allocate, if we invalidate some lines from changing
82 * the layout width, we need to go ahead and run the high-priority idle,
83 * because GTK sends exposes right after doing the size allocates without
84 * returning to the main loop. This is also why the high-priority idle
85 * is at a higher priority than resizing.
90 #define DEBUG_VALIDATION_AND_SCROLLING
93 #ifdef DEBUG_VALIDATION_AND_SCROLLING
99 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->priv->text_window)
100 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->priv->text_window)
102 #define SPACE_FOR_CURSOR 1
104 #define GTK_TEXT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_VIEW, GtkTextViewPrivate))
106 typedef struct _GtkTextWindow GtkTextWindow;
107 typedef struct _GtkTextPendingScroll GtkTextPendingScroll;
109 struct _GtkTextViewPrivate
111 GtkTextLayout *layout;
112 GtkTextBuffer *buffer;
114 guint blink_time; /* time in msec the cursor has blinked since last user event */
117 GdkDevice *grab_device;
118 GdkDevice *dnd_device;
120 guint selection_drag_handler;
121 guint scroll_timeout;
123 GtkTextWindow *text_window;
124 GtkTextWindow *left_window;
125 GtkTextWindow *right_window;
126 GtkTextWindow *top_window;
127 GtkTextWindow *bottom_window;
129 GtkAdjustment *hadjustment;
130 GtkAdjustment *vadjustment;
132 gint xoffset; /* Offsets between widget coordinates and buffer coordinates */
134 gint width; /* Width and height of the buffer */
137 /* This is used to monitor the overall size request
138 * and decide whether we need to queue resizes when
139 * the buffer content changes.
141 * FIXME: This could be done in a simpler way by
142 * consulting the above width/height of the buffer + some
143 * padding values, however all of this request code needs
144 * to be changed to use GtkSizeRequestIface and deserves
147 GtkRequisition cached_size_request;
149 /* The virtual cursor position is normally the same as the
150 * actual (strong) cursor position, except in two circumstances:
152 * a) When the cursor is moved vertically with the keyboard
153 * b) When the text view is scrolled with the keyboard
155 * In case a), virtual_cursor_x is preserved, but not virtual_cursor_y
156 * In case b), both virtual_cursor_x and virtual_cursor_y are preserved.
158 gint virtual_cursor_x; /* -1 means use actual cursor position */
159 gint virtual_cursor_y; /* -1 means use actual cursor position */
161 GtkTextMark *first_para_mark; /* Mark at the beginning of the first onscreen paragraph */
162 gint first_para_pixels; /* Offset of top of screen in the first onscreen paragraph */
164 GtkTextMark *dnd_mark;
167 guint first_validate_idle; /* Idle to revalidate onscreen portion, runs before resize */
168 guint incremental_validate_idle; /* Idle to revalidate offscreen portions, runs after redraw */
170 GtkIMContext *im_context;
171 GtkWidget *popup_menu;
178 GtkTextPendingScroll *pending_scroll;
180 gint pending_place_cursor_button;
182 /* Default style settings */
183 gint pixels_above_lines;
184 gint pixels_below_lines;
185 gint pixels_inside_wrap;
186 GtkWrapMode wrap_mode;
187 GtkJustification justify;
194 guint overwrite_mode : 1;
195 guint cursor_visible : 1;
197 /* if we have reset the IM since the last character entered */
198 guint need_im_reset : 1;
200 guint accepts_tab : 1;
202 guint width_changed : 1;
204 /* debug flag - means that we've validated onscreen since the
205 * last "invalidate" signal from the layout
207 guint onscreen_validated : 1;
209 guint mouse_cursor_obscured : 1;
211 guint scroll_after_paste : 1;
214 struct _GtkTextPendingScroll
217 gdouble within_margin;
225 SET_SCROLL_ADJUSTMENTS,
239 TOGGLE_CURSOR_VISIBLE,
247 PROP_PIXELS_ABOVE_LINES,
248 PROP_PIXELS_BELOW_LINES,
249 PROP_PIXELS_INSIDE_WRAP,
264 static void gtk_text_view_destroy (GtkObject *object);
265 static void gtk_text_view_finalize (GObject *object);
266 static void gtk_text_view_set_property (GObject *object,
270 static void gtk_text_view_get_property (GObject *object,
274 static void gtk_text_view_size_request (GtkWidget *widget,
275 GtkRequisition *requisition);
276 static void gtk_text_view_size_allocate (GtkWidget *widget,
277 GtkAllocation *allocation);
278 static void gtk_text_view_realize (GtkWidget *widget);
279 static void gtk_text_view_unrealize (GtkWidget *widget);
280 static void gtk_text_view_style_set (GtkWidget *widget,
281 GtkStyle *previous_style);
282 static void gtk_text_view_direction_changed (GtkWidget *widget,
283 GtkTextDirection previous_direction);
284 static void gtk_text_view_grab_notify (GtkWidget *widget,
285 gboolean was_grabbed);
286 static void gtk_text_view_state_changed (GtkWidget *widget,
287 GtkStateType previous_state);
289 static gint gtk_text_view_event (GtkWidget *widget,
291 static gint gtk_text_view_key_press_event (GtkWidget *widget,
293 static gint gtk_text_view_key_release_event (GtkWidget *widget,
295 static gint gtk_text_view_button_press_event (GtkWidget *widget,
296 GdkEventButton *event);
297 static gint gtk_text_view_button_release_event (GtkWidget *widget,
298 GdkEventButton *event);
299 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
300 GdkEventFocus *event);
301 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
302 GdkEventFocus *event);
303 static gint gtk_text_view_motion_event (GtkWidget *widget,
304 GdkEventMotion *event);
305 static gint gtk_text_view_draw (GtkWidget *widget,
307 static void gtk_text_view_draw_focus (GtkWidget *widget,
309 static gboolean gtk_text_view_focus (GtkWidget *widget,
310 GtkDirectionType direction);
311 static void gtk_text_view_move_focus (GtkWidget *widget,
312 GtkDirectionType direction_type);
313 static void gtk_text_view_select_all (GtkWidget *widget,
317 /* Source side drag signals */
318 static void gtk_text_view_drag_begin (GtkWidget *widget,
319 GdkDragContext *context);
320 static void gtk_text_view_drag_end (GtkWidget *widget,
321 GdkDragContext *context);
322 static void gtk_text_view_drag_data_get (GtkWidget *widget,
323 GdkDragContext *context,
324 GtkSelectionData *selection_data,
327 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
328 GdkDragContext *context);
330 /* Target side drag signals */
331 static void gtk_text_view_drag_leave (GtkWidget *widget,
332 GdkDragContext *context,
334 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
335 GdkDragContext *context,
339 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
340 GdkDragContext *context,
344 static void gtk_text_view_drag_data_received (GtkWidget *widget,
345 GdkDragContext *context,
348 GtkSelectionData *selection_data,
352 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
354 GtkAdjustment *vadj);
355 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
357 static void gtk_text_view_move_cursor (GtkTextView *text_view,
358 GtkMovementStep step,
360 gboolean extend_selection);
361 static gboolean gtk_text_view_move_viewport (GtkTextView *text_view,
364 static void gtk_text_view_set_anchor (GtkTextView *text_view);
365 static gboolean gtk_text_view_scroll_pages (GtkTextView *text_view,
367 gboolean extend_selection);
368 static gboolean gtk_text_view_scroll_hpages(GtkTextView *text_view,
370 gboolean extend_selection);
371 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
373 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
376 static void gtk_text_view_backspace (GtkTextView *text_view);
377 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
378 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
379 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
380 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
381 static void gtk_text_view_toggle_cursor_visible (GtkTextView *text_view);
382 static void gtk_text_view_compat_move_focus(GtkTextView *text_view,
383 GtkDirectionType direction_type);
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 GtkAdjustment* get_hadjustment (GtkTextView *text_view);
445 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
447 static void gtk_text_view_do_popup (GtkTextView *text_view,
448 GdkEventButton *event);
450 static void cancel_pending_scroll (GtkTextView *text_view);
451 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
453 gdouble within_margin,
458 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
459 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
460 static void gtk_text_view_invalidate (GtkTextView *text_view);
461 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
463 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
465 /* Container methods */
466 static void gtk_text_view_add (GtkContainer *container,
468 static void gtk_text_view_remove (GtkContainer *container,
470 static void gtk_text_view_forall (GtkContainer *container,
471 gboolean include_internals,
472 GtkCallback callback,
473 gpointer callback_data);
475 /* FIXME probably need the focus methods. */
477 typedef struct _GtkTextViewChild GtkTextViewChild;
479 struct _GtkTextViewChild
483 GtkTextChildAnchor *anchor;
485 gint from_top_of_line;
486 gint from_left_of_buffer;
488 /* These are ignored if anchor != NULL */
489 GtkTextWindowType type;
494 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
495 GtkTextChildAnchor *anchor,
496 GtkTextLayout *layout);
497 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
498 GtkTextWindowType type,
501 static void text_view_child_free (GtkTextViewChild *child);
502 static void text_view_child_set_parent_window (GtkTextView *text_view,
503 GtkTextViewChild *child);
505 struct _GtkTextWindow
507 GtkTextWindowType type;
510 GdkWindow *bin_window;
511 GtkRequisition requisition;
512 GdkRectangle allocation;
515 static GtkTextWindow *text_window_new (GtkTextWindowType type,
518 gint height_request);
519 static void text_window_free (GtkTextWindow *win);
520 static void text_window_realize (GtkTextWindow *win,
522 static void text_window_unrealize (GtkTextWindow *win);
523 static void text_window_size_allocate (GtkTextWindow *win,
525 static void text_window_scroll (GtkTextWindow *win,
528 static void text_window_invalidate_rect (GtkTextWindow *win,
530 static void text_window_invalidate_cursors (GtkTextWindow *win);
532 static gint text_window_get_width (GtkTextWindow *win);
533 static gint text_window_get_height (GtkTextWindow *win);
536 static guint signals[LAST_SIGNAL] = { 0 };
538 G_DEFINE_TYPE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER)
541 add_move_binding (GtkBindingSet *binding_set,
544 GtkMovementStep step,
547 g_assert ((modmask & GDK_SHIFT_MASK) == 0);
549 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
553 G_TYPE_BOOLEAN, FALSE);
555 /* Selection-extending version */
556 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
560 G_TYPE_BOOLEAN, TRUE);
564 gtk_text_view_class_init (GtkTextViewClass *klass)
566 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
567 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
568 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
569 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
570 GtkBindingSet *binding_set;
572 /* Default handlers and virtual methods
574 gobject_class->set_property = gtk_text_view_set_property;
575 gobject_class->get_property = gtk_text_view_get_property;
577 object_class->destroy = gtk_text_view_destroy;
578 gobject_class->finalize = gtk_text_view_finalize;
580 widget_class->realize = gtk_text_view_realize;
581 widget_class->unrealize = gtk_text_view_unrealize;
582 widget_class->style_set = gtk_text_view_style_set;
583 widget_class->direction_changed = gtk_text_view_direction_changed;
584 widget_class->grab_notify = gtk_text_view_grab_notify;
585 widget_class->state_changed = gtk_text_view_state_changed;
586 widget_class->size_request = gtk_text_view_size_request;
587 widget_class->size_allocate = gtk_text_view_size_allocate;
588 widget_class->event = gtk_text_view_event;
589 widget_class->key_press_event = gtk_text_view_key_press_event;
590 widget_class->key_release_event = gtk_text_view_key_release_event;
591 widget_class->button_press_event = gtk_text_view_button_press_event;
592 widget_class->button_release_event = gtk_text_view_button_release_event;
593 widget_class->focus_in_event = gtk_text_view_focus_in_event;
594 widget_class->focus_out_event = gtk_text_view_focus_out_event;
595 widget_class->motion_notify_event = gtk_text_view_motion_event;
596 widget_class->draw = gtk_text_view_draw;
597 widget_class->focus = gtk_text_view_focus;
599 /* need to override the base class function via override_class_handler,
600 * because the signal slot is not available in GtkWidgetCLass
602 g_signal_override_class_handler ("move-focus",
604 G_CALLBACK (gtk_text_view_move_focus));
606 widget_class->drag_begin = gtk_text_view_drag_begin;
607 widget_class->drag_end = gtk_text_view_drag_end;
608 widget_class->drag_data_get = gtk_text_view_drag_data_get;
609 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
611 widget_class->drag_leave = gtk_text_view_drag_leave;
612 widget_class->drag_motion = gtk_text_view_drag_motion;
613 widget_class->drag_drop = gtk_text_view_drag_drop;
614 widget_class->drag_data_received = gtk_text_view_drag_data_received;
616 widget_class->popup_menu = gtk_text_view_popup_menu;
618 container_class->add = gtk_text_view_add;
619 container_class->remove = gtk_text_view_remove;
620 container_class->forall = gtk_text_view_forall;
622 klass->move_cursor = gtk_text_view_move_cursor;
623 klass->set_anchor = gtk_text_view_set_anchor;
624 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
625 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
626 klass->backspace = gtk_text_view_backspace;
627 klass->cut_clipboard = gtk_text_view_cut_clipboard;
628 klass->copy_clipboard = gtk_text_view_copy_clipboard;
629 klass->paste_clipboard = gtk_text_view_paste_clipboard;
630 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
631 klass->move_focus = gtk_text_view_compat_move_focus;
632 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
638 g_object_class_install_property (gobject_class,
639 PROP_PIXELS_ABOVE_LINES,
640 g_param_spec_int ("pixels-above-lines",
641 P_("Pixels Above Lines"),
642 P_("Pixels of blank space above paragraphs"),
646 GTK_PARAM_READWRITE));
648 g_object_class_install_property (gobject_class,
649 PROP_PIXELS_BELOW_LINES,
650 g_param_spec_int ("pixels-below-lines",
651 P_("Pixels Below Lines"),
652 P_("Pixels of blank space below paragraphs"),
656 GTK_PARAM_READWRITE));
658 g_object_class_install_property (gobject_class,
659 PROP_PIXELS_INSIDE_WRAP,
660 g_param_spec_int ("pixels-inside-wrap",
661 P_("Pixels Inside Wrap"),
662 P_("Pixels of blank space between wrapped lines in a paragraph"),
666 GTK_PARAM_READWRITE));
668 g_object_class_install_property (gobject_class,
670 g_param_spec_boolean ("editable",
672 P_("Whether the text can be modified by the user"),
674 GTK_PARAM_READWRITE));
676 g_object_class_install_property (gobject_class,
678 g_param_spec_enum ("wrap-mode",
680 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
683 GTK_PARAM_READWRITE));
685 g_object_class_install_property (gobject_class,
687 g_param_spec_enum ("justification",
689 P_("Left, right, or center justification"),
690 GTK_TYPE_JUSTIFICATION,
692 GTK_PARAM_READWRITE));
694 g_object_class_install_property (gobject_class,
696 g_param_spec_int ("left-margin",
698 P_("Width of the left margin in pixels"),
702 GTK_PARAM_READWRITE));
704 g_object_class_install_property (gobject_class,
706 g_param_spec_int ("right-margin",
708 P_("Width of the right margin in pixels"),
712 GTK_PARAM_READWRITE));
714 g_object_class_install_property (gobject_class,
716 g_param_spec_int ("indent",
718 P_("Amount to indent the paragraph, in pixels"),
722 GTK_PARAM_READWRITE));
724 g_object_class_install_property (gobject_class,
726 g_param_spec_boxed ("tabs",
728 P_("Custom tabs for this text"),
729 PANGO_TYPE_TAB_ARRAY,
730 GTK_PARAM_READWRITE));
732 g_object_class_install_property (gobject_class,
734 g_param_spec_boolean ("cursor-visible",
735 P_("Cursor Visible"),
736 P_("If the insertion cursor is shown"),
738 GTK_PARAM_READWRITE));
740 g_object_class_install_property (gobject_class,
742 g_param_spec_object ("buffer",
744 P_("The buffer which is displayed"),
745 GTK_TYPE_TEXT_BUFFER,
746 GTK_PARAM_READWRITE));
748 g_object_class_install_property (gobject_class,
750 g_param_spec_boolean ("overwrite",
751 P_("Overwrite mode"),
752 P_("Whether entered text overwrites existing contents"),
754 GTK_PARAM_READWRITE));
756 g_object_class_install_property (gobject_class,
758 g_param_spec_boolean ("accepts-tab",
760 P_("Whether Tab will result in a tab character being entered"),
762 GTK_PARAM_READWRITE));
765 * GtkTextView:im-module:
767 * Which IM (input method) module should be used for this entry.
770 * Setting this to a non-%NULL value overrides the
771 * system-wide IM module setting. See the GtkSettings
772 * #GtkSettings:gtk-im-module property.
776 g_object_class_install_property (gobject_class,
778 g_param_spec_string ("im-module",
780 P_("Which IM module should be used"),
782 GTK_PARAM_READWRITE));
787 gtk_widget_class_install_style_property (widget_class,
788 g_param_spec_boxed ("error-underline-color",
789 P_("Error underline color"),
790 P_("Color with which to draw error-indication underlines"),
792 GTK_PARAM_READABLE));
799 * GtkTextView::move-cursor:
800 * @text_view: the object which received the signal
801 * @step: the granularity of the move, as a #GtkMovementStep
802 * @count: the number of @step units to move
803 * @extend_selection: %TRUE if the move should extend the selection
805 * The ::move-cursor signal is a
806 * <link linkend="keybinding-signals">keybinding signal</link>
807 * which gets emitted when the user initiates a cursor movement.
808 * If the cursor is not visible in @text_view, this signal causes
809 * the viewport to be moved instead.
811 * Applications should not connect to it, but may emit it with
812 * g_signal_emit_by_name() if they need to control the cursor
815 * The default bindings for this signal come in two variants,
816 * the variant with the Shift modifier extends the selection,
817 * the variant without the Shift modifer does not.
818 * There are too many key combinations to list them all here.
820 * <listitem>Arrow keys move by individual characters/lines</listitem>
821 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
822 * <listitem>Home/End keys move to the ends of the buffer</listitem>
823 * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
824 * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
827 signals[MOVE_CURSOR] =
828 g_signal_new (I_("move-cursor"),
829 G_OBJECT_CLASS_TYPE (gobject_class),
830 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
831 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
833 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
835 GTK_TYPE_MOVEMENT_STEP,
840 * GtkTextView::move-viewport:
841 * @text_view: the object which received the signal
842 * @step: the granularity of the move, as a #GtkMovementStep
843 * @count: the number of @step units to move
845 * The ::move-viewport signal is a
846 * <link linkend="keybinding-signals">keybinding signal</link>
847 * which can be bound to key combinations to allow the user
848 * to move the viewport, i.e. change what part of the text view
849 * is visible in a containing scrolled window.
851 * There are no default bindings for this signal.
853 signals[MOVE_VIEWPORT] =
854 g_signal_new_class_handler (I_("move-viewport"),
855 G_OBJECT_CLASS_TYPE (gobject_class),
856 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
857 G_CALLBACK (gtk_text_view_move_viewport),
859 _gtk_marshal_VOID__ENUM_INT,
861 GTK_TYPE_SCROLL_STEP,
865 * GtkTextView::set-anchor:
866 * @text_view: the object which received the signal
868 * The ::set-anchor signal is a
869 * <link linkend="keybinding-signals">keybinding signal</link>
870 * which gets emitted when the user initiates setting the "anchor"
871 * mark. The "anchor" mark gets placed at the same position as the
874 * This signal has no default bindings.
876 signals[SET_ANCHOR] =
877 g_signal_new (I_("set-anchor"),
878 G_OBJECT_CLASS_TYPE (gobject_class),
879 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
880 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
882 _gtk_marshal_VOID__VOID,
886 * GtkTextView::insert-at-cursor:
887 * @text_view: the object which received the signal
888 * @string: the string to insert
890 * The ::insert-at-cursor signal is a
891 * <link linkend="keybinding-signals">keybinding signal</link>
892 * which gets emitted when the user initiates the insertion of a
893 * fixed string at the cursor.
895 * This signal has no default bindings.
897 signals[INSERT_AT_CURSOR] =
898 g_signal_new (I_("insert-at-cursor"),
899 G_OBJECT_CLASS_TYPE (gobject_class),
900 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
901 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
903 _gtk_marshal_VOID__STRING,
908 * GtkTextView::delete-from-cursor:
909 * @text_view: the object which received the signal
910 * @type: the granularity of the deletion, as a #GtkDeleteType
911 * @count: the number of @type units to delete
913 * The ::delete-from-cursor signal is a
914 * <link linkend="keybinding-signals">keybinding signal</link>
915 * which gets emitted when the user initiates a text deletion.
917 * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
918 * if there is one, otherwise it deletes the requested number
921 * The default bindings for this signal are
922 * Delete for deleting a character, Ctrl-Delete for
923 * deleting a word and Ctrl-Backspace for deleting a word
926 signals[DELETE_FROM_CURSOR] =
927 g_signal_new (I_("delete-from-cursor"),
928 G_OBJECT_CLASS_TYPE (gobject_class),
929 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
930 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
932 _gtk_marshal_VOID__ENUM_INT,
934 GTK_TYPE_DELETE_TYPE,
938 * GtkTextView::backspace:
939 * @text_view: the object which received the signal
941 * The ::backspace signal is a
942 * <link linkend="keybinding-signals">keybinding signal</link>
943 * which gets emitted when the user asks for it.
945 * The default bindings for this signal are
946 * Backspace and Shift-Backspace.
949 g_signal_new (I_("backspace"),
950 G_OBJECT_CLASS_TYPE (gobject_class),
951 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
952 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
954 _gtk_marshal_VOID__VOID,
958 * GtkTextView::cut-clipboard:
959 * @text_view: the object which received the signal
961 * The ::cut-clipboard signal is a
962 * <link linkend="keybinding-signals">keybinding signal</link>
963 * which gets emitted to cut the selection to the clipboard.
965 * The default bindings for this signal are
966 * Ctrl-x and Shift-Delete.
968 signals[CUT_CLIPBOARD] =
969 g_signal_new (I_("cut-clipboard"),
970 G_OBJECT_CLASS_TYPE (gobject_class),
971 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
972 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
974 _gtk_marshal_VOID__VOID,
978 * GtkTextView::copy-clipboard:
979 * @text_view: the object which received the signal
981 * The ::copy-clipboard signal is a
982 * <link linkend="keybinding-signals">keybinding signal</link>
983 * which gets emitted to copy the selection to the clipboard.
985 * The default bindings for this signal are
986 * Ctrl-c and Ctrl-Insert.
988 signals[COPY_CLIPBOARD] =
989 g_signal_new (I_("copy-clipboard"),
990 G_OBJECT_CLASS_TYPE (gobject_class),
991 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
992 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
994 _gtk_marshal_VOID__VOID,
998 * GtkTextView::paste-clipboard:
999 * @text_view: the object which received the signal
1001 * The ::paste-clipboard signal is a
1002 * <link linkend="keybinding-signals">keybinding signal</link>
1003 * which gets emitted to paste the contents of the clipboard
1004 * into the text view.
1006 * The default bindings for this signal are
1007 * Ctrl-v and Shift-Insert.
1009 signals[PASTE_CLIPBOARD] =
1010 g_signal_new (I_("paste-clipboard"),
1011 G_OBJECT_CLASS_TYPE (gobject_class),
1012 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1013 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
1015 _gtk_marshal_VOID__VOID,
1019 * GtkTextView::toggle-overwrite:
1020 * @text_view: the object which received the signal
1022 * The ::toggle-overwrite signal is a
1023 * <link linkend="keybinding-signals">keybinding signal</link>
1024 * which gets emitted to toggle the overwrite mode of the text view.
1026 * The default bindings for this signal is Insert.
1028 signals[TOGGLE_OVERWRITE] =
1029 g_signal_new (I_("toggle-overwrite"),
1030 G_OBJECT_CLASS_TYPE (gobject_class),
1031 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1032 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
1034 _gtk_marshal_VOID__VOID,
1038 * GtkTextView::set-scroll-adjustments
1039 * @horizontal: the horizontal #GtkAdjustment
1040 * @vertical: the vertical #GtkAdjustment
1042 * Set the scroll adjustments for the text view. Usually scrolled containers
1043 * like #GtkScrolledWindow will emit this signal to connect two instances
1044 * of #GtkScrollbar to the scroll directions of the #GtkTextView.
1046 signals[SET_SCROLL_ADJUSTMENTS] =
1047 g_signal_new (I_("set-scroll-adjustments"),
1048 G_OBJECT_CLASS_TYPE (gobject_class),
1049 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1050 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
1052 _gtk_marshal_VOID__OBJECT_OBJECT,
1054 GTK_TYPE_ADJUSTMENT,
1055 GTK_TYPE_ADJUSTMENT);
1056 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
1059 * GtkTextView::populate-popup:
1060 * @entry: The text view on which the signal is emitted
1061 * @menu: the menu that is being populated
1063 * The ::populate-popup signal gets emitted before showing the
1064 * context menu of the text view.
1066 * If you need to add items to the context menu, connect
1067 * to this signal and append your menuitems to the @menu.
1069 signals[POPULATE_POPUP] =
1070 g_signal_new (I_("populate-popup"),
1071 G_OBJECT_CLASS_TYPE (gobject_class),
1073 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
1075 _gtk_marshal_VOID__OBJECT,
1080 * GtkTextView::select-all:
1081 * @text_view: the object which received the signal
1082 * @select: %TRUE to select, %FALSE to unselect
1084 * The ::select-all signal is a
1085 * <link linkend="keybinding-signals">keybinding signal</link>
1086 * which gets emitted to select or unselect the complete
1087 * contents of the text view.
1089 * The default bindings for this signal are Ctrl-a and Ctrl-/
1090 * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
1092 signals[SELECT_ALL] =
1093 g_signal_new_class_handler (I_("select-all"),
1094 G_OBJECT_CLASS_TYPE (object_class),
1095 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1096 G_CALLBACK (gtk_text_view_select_all),
1098 _gtk_marshal_VOID__BOOLEAN,
1099 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
1102 * GtkTextView::toggle-cursor-visible:
1103 * @text_view: the object which received the signal
1105 * The ::toggle-cursor-visible signal is a
1106 * <link linkend="keybinding-signals">keybinding signal</link>
1107 * which gets emitted to toggle the visibility of the cursor.
1109 * The default binding for this signal is F7.
1111 signals[TOGGLE_CURSOR_VISIBLE] =
1112 g_signal_new_class_handler (I_("toggle-cursor-visible"),
1113 G_OBJECT_CLASS_TYPE (object_class),
1114 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1115 G_CALLBACK (gtk_text_view_toggle_cursor_visible),
1117 _gtk_marshal_VOID__VOID,
1121 * GtkTextView::preedit-changed:
1122 * @text_view: the object which received the signal
1123 * @preedit: the current preedit string
1125 * If an input method is used, the typed text will not immediately
1126 * be committed to the buffer. So if you are interested in the text,
1127 * connect to this signal.
1129 * This signal is only emitted if the text at the given position
1130 * is actually editable.
1134 signals[PREEDIT_CHANGED] =
1135 g_signal_new_class_handler (I_("preedit-changed"),
1136 G_OBJECT_CLASS_TYPE (object_class),
1137 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1140 _gtk_marshal_VOID__STRING,
1148 binding_set = gtk_binding_set_by_class (klass);
1150 /* Moving the insertion point */
1151 add_move_binding (binding_set, GDK_KEY_Right, 0,
1152 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1154 add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
1155 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1157 add_move_binding (binding_set, GDK_KEY_Left, 0,
1158 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1160 add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
1161 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1163 add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
1164 GTK_MOVEMENT_WORDS, 1);
1166 add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
1167 GTK_MOVEMENT_WORDS, 1);
1169 add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
1170 GTK_MOVEMENT_WORDS, -1);
1172 add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
1173 GTK_MOVEMENT_WORDS, -1);
1175 add_move_binding (binding_set, GDK_KEY_Up, 0,
1176 GTK_MOVEMENT_DISPLAY_LINES, -1);
1178 add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
1179 GTK_MOVEMENT_DISPLAY_LINES, -1);
1181 add_move_binding (binding_set, GDK_KEY_Down, 0,
1182 GTK_MOVEMENT_DISPLAY_LINES, 1);
1184 add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
1185 GTK_MOVEMENT_DISPLAY_LINES, 1);
1187 add_move_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
1188 GTK_MOVEMENT_PARAGRAPHS, -1);
1190 add_move_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
1191 GTK_MOVEMENT_PARAGRAPHS, -1);
1193 add_move_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
1194 GTK_MOVEMENT_PARAGRAPHS, 1);
1196 add_move_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
1197 GTK_MOVEMENT_PARAGRAPHS, 1);
1199 add_move_binding (binding_set, GDK_KEY_Home, 0,
1200 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1202 add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
1203 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1205 add_move_binding (binding_set, GDK_KEY_End, 0,
1206 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1208 add_move_binding (binding_set, GDK_KEY_KP_End, 0,
1209 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1211 add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
1212 GTK_MOVEMENT_BUFFER_ENDS, -1);
1214 add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
1215 GTK_MOVEMENT_BUFFER_ENDS, -1);
1217 add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
1218 GTK_MOVEMENT_BUFFER_ENDS, 1);
1220 add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
1221 GTK_MOVEMENT_BUFFER_ENDS, 1);
1223 add_move_binding (binding_set, GDK_KEY_Page_Up, 0,
1224 GTK_MOVEMENT_PAGES, -1);
1226 add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
1227 GTK_MOVEMENT_PAGES, -1);
1229 add_move_binding (binding_set, GDK_KEY_Page_Down, 0,
1230 GTK_MOVEMENT_PAGES, 1);
1232 add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
1233 GTK_MOVEMENT_PAGES, 1);
1235 add_move_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK,
1236 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1238 add_move_binding (binding_set, GDK_KEY_KP_Page_Up, GDK_CONTROL_MASK,
1239 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1241 add_move_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK,
1242 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1244 add_move_binding (binding_set, GDK_KEY_KP_Page_Down, GDK_CONTROL_MASK,
1245 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1248 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
1250 G_TYPE_BOOLEAN, TRUE);
1252 gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
1254 G_TYPE_BOOLEAN, TRUE);
1257 gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
1259 G_TYPE_BOOLEAN, FALSE);
1261 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1263 G_TYPE_BOOLEAN, FALSE);
1266 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, 0,
1267 "delete-from-cursor", 2,
1268 G_TYPE_ENUM, GTK_DELETE_CHARS,
1271 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, 0,
1272 "delete-from-cursor", 2,
1273 G_TYPE_ENUM, GTK_DELETE_CHARS,
1276 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, 0,
1279 /* Make this do the same as Backspace, to help with mis-typing */
1280 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_SHIFT_MASK,
1283 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_CONTROL_MASK,
1284 "delete-from-cursor", 2,
1285 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1288 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, GDK_CONTROL_MASK,
1289 "delete-from-cursor", 2,
1290 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1293 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_CONTROL_MASK,
1294 "delete-from-cursor", 2,
1295 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1298 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1299 "delete-from-cursor", 2,
1300 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1303 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1304 "delete-from-cursor", 2,
1305 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1308 gtk_binding_entry_add_signal (binding_set, GDK_KEY_BackSpace, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1309 "delete-from-cursor", 2,
1310 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1313 /* Cut/copy/paste */
1315 gtk_binding_entry_add_signal (binding_set, GDK_KEY_x, GDK_CONTROL_MASK,
1316 "cut-clipboard", 0);
1317 gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
1318 "copy-clipboard", 0);
1319 gtk_binding_entry_add_signal (binding_set, GDK_KEY_v, GDK_CONTROL_MASK,
1320 "paste-clipboard", 0);
1322 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Delete, GDK_SHIFT_MASK,
1323 "cut-clipboard", 0);
1324 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_CONTROL_MASK,
1325 "copy-clipboard", 0);
1326 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, GDK_SHIFT_MASK,
1327 "paste-clipboard", 0);
1330 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Insert, 0,
1331 "toggle-overwrite", 0);
1332 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Insert, 0,
1333 "toggle-overwrite", 0);
1336 gtk_binding_entry_add_signal (binding_set, GDK_KEY_F7, 0,
1337 "toggle-cursor-visible", 0);
1339 /* Control-tab focus motion */
1340 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, GDK_CONTROL_MASK,
1342 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1343 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, GDK_CONTROL_MASK,
1345 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1347 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1349 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1350 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1352 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1354 g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1358 gtk_text_view_init (GtkTextView *text_view)
1360 GtkWidget *widget = GTK_WIDGET (text_view);
1361 GtkTargetList *target_list;
1362 GtkTextViewPrivate *priv;
1364 text_view->priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
1365 priv = text_view->priv;
1367 gtk_widget_set_can_focus (widget, TRUE);
1369 /* Set up default style */
1370 priv->wrap_mode = GTK_WRAP_NONE;
1371 priv->pixels_above_lines = 0;
1372 priv->pixels_below_lines = 0;
1373 priv->pixels_inside_wrap = 0;
1374 priv->justify = GTK_JUSTIFY_LEFT;
1375 priv->left_margin = 0;
1376 priv->right_margin = 0;
1379 priv->editable = TRUE;
1381 priv->scroll_after_paste = TRUE;
1383 gtk_drag_dest_set (widget, 0, NULL, 0,
1384 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1386 target_list = gtk_target_list_new (NULL, 0);
1387 gtk_drag_dest_set_target_list (widget, target_list);
1388 gtk_target_list_unref (target_list);
1390 priv->virtual_cursor_x = -1;
1391 priv->virtual_cursor_y = -1;
1393 /* This object is completely private. No external entity can gain a reference
1394 * to it; so we create it here and destroy it in finalize ().
1396 priv->im_context = gtk_im_multicontext_new ();
1398 g_signal_connect (priv->im_context, "commit",
1399 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1400 g_signal_connect (priv->im_context, "preedit-changed",
1401 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1402 g_signal_connect (priv->im_context, "retrieve-surrounding",
1403 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1404 g_signal_connect (priv->im_context, "delete-surrounding",
1405 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1407 priv->cursor_visible = TRUE;
1409 priv->accepts_tab = TRUE;
1411 priv->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1414 priv->drag_start_x = -1;
1415 priv->drag_start_y = -1;
1417 priv->pending_place_cursor_button = 0;
1419 /* We handle all our own redrawing */
1420 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1424 * gtk_text_view_new:
1426 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1427 * before using the text view, an empty default buffer will be created
1428 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1429 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1431 * Return value: a new #GtkTextView
1434 gtk_text_view_new (void)
1436 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1440 * gtk_text_view_new_with_buffer:
1441 * @buffer: a #GtkTextBuffer
1443 * Creates a new #GtkTextView widget displaying the buffer
1444 * @buffer. One buffer can be shared among many widgets.
1445 * @buffer may be %NULL to create a default buffer, in which case
1446 * this function is equivalent to gtk_text_view_new(). The
1447 * text view adds its own reference count to the buffer; it does not
1448 * take over an existing reference.
1450 * Return value: a new #GtkTextView.
1453 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1455 GtkTextView *text_view;
1457 text_view = (GtkTextView*)gtk_text_view_new ();
1459 gtk_text_view_set_buffer (text_view, buffer);
1461 return GTK_WIDGET (text_view);
1465 * gtk_text_view_set_buffer:
1466 * @text_view: a #GtkTextView
1467 * @buffer: (allow-none): a #GtkTextBuffer
1469 * Sets @buffer as the buffer being displayed by @text_view. The previous
1470 * buffer displayed by the text view is unreferenced, and a reference is
1471 * added to @buffer. If you owned a reference to @buffer before passing it
1472 * to this function, you must remove that reference yourself; #GtkTextView
1473 * will not "adopt" it.
1476 gtk_text_view_set_buffer (GtkTextView *text_view,
1477 GtkTextBuffer *buffer)
1479 GtkTextViewPrivate *priv;
1481 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1482 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1484 priv = text_view->priv;
1486 if (priv->buffer == buffer)
1489 if (priv->buffer != NULL)
1491 /* Destroy all anchored children */
1495 copy = g_slist_copy (priv->children);
1497 while (tmp_list != NULL)
1499 GtkTextViewChild *vc = tmp_list->data;
1503 gtk_widget_destroy (vc->widget);
1504 /* vc may now be invalid! */
1507 tmp_list = g_slist_next (tmp_list);
1510 g_slist_free (copy);
1512 g_signal_handlers_disconnect_by_func (priv->buffer,
1513 gtk_text_view_mark_set_handler,
1515 g_signal_handlers_disconnect_by_func (priv->buffer,
1516 gtk_text_view_target_list_notify,
1518 g_signal_handlers_disconnect_by_func (priv->buffer,
1519 gtk_text_view_paste_done_handler,
1522 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
1524 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1525 GDK_SELECTION_PRIMARY);
1526 gtk_text_buffer_remove_selection_clipboard (priv->buffer, clipboard);
1530 gtk_text_layout_set_buffer (priv->layout, NULL);
1532 g_object_unref (priv->buffer);
1533 priv->dnd_mark = NULL;
1534 priv->first_para_mark = NULL;
1535 cancel_pending_scroll (text_view);
1538 priv->buffer = buffer;
1541 gtk_text_layout_set_buffer (priv->layout, buffer);
1547 g_object_ref (buffer);
1549 gtk_text_buffer_get_iter_at_offset (priv->buffer, &start, 0);
1551 priv->dnd_mark = gtk_text_buffer_create_mark (priv->buffer,
1555 priv->first_para_mark = gtk_text_buffer_create_mark (priv->buffer,
1559 priv->first_para_pixels = 0;
1562 g_signal_connect (priv->buffer, "mark-set",
1563 G_CALLBACK (gtk_text_view_mark_set_handler),
1565 g_signal_connect (priv->buffer, "notify::paste-target-list",
1566 G_CALLBACK (gtk_text_view_target_list_notify),
1568 g_signal_connect (priv->buffer, "paste-done",
1569 G_CALLBACK (gtk_text_view_paste_done_handler),
1572 gtk_text_view_target_list_notify (priv->buffer, NULL, text_view);
1574 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
1576 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1577 GDK_SELECTION_PRIMARY);
1578 gtk_text_buffer_add_selection_clipboard (priv->buffer, clipboard);
1582 g_object_notify (G_OBJECT (text_view), "buffer");
1584 if (gtk_widget_get_visible (GTK_WIDGET (text_view)))
1585 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1587 DV(g_print ("Invalidating due to set_buffer\n"));
1588 gtk_text_view_invalidate (text_view);
1591 static GtkTextBuffer*
1592 get_buffer (GtkTextView *text_view)
1594 if (text_view->priv->buffer == NULL)
1597 b = gtk_text_buffer_new (NULL);
1598 gtk_text_view_set_buffer (text_view, b);
1602 return text_view->priv->buffer;
1606 * gtk_text_view_get_buffer:
1607 * @text_view: a #GtkTextView
1609 * Returns the #GtkTextBuffer being displayed by this text view.
1610 * The reference count on the buffer is not incremented; the caller
1611 * of this function won't own a new reference.
1613 * Return value: (transfer none): a #GtkTextBuffer
1616 gtk_text_view_get_buffer (GtkTextView *text_view)
1618 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1620 return get_buffer (text_view);
1624 * gtk_text_view_get_iter_at_location:
1625 * @text_view: a #GtkTextView
1626 * @iter: (out): a #GtkTextIter
1627 * @x: x position, in buffer coordinates
1628 * @y: y position, in buffer coordinates
1630 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1631 * coordinates are coordinates for the entire buffer, not just the
1632 * currently-displayed portion. If you have coordinates from an
1633 * event, you have to convert those to buffer coordinates with
1634 * gtk_text_view_window_to_buffer_coords().
1637 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1642 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1643 g_return_if_fail (iter != NULL);
1645 gtk_text_view_ensure_layout (text_view);
1647 gtk_text_layout_get_iter_at_pixel (text_view->priv->layout,
1652 * gtk_text_view_get_iter_at_position:
1653 * @text_view: a #GtkTextView
1654 * @iter: (out): a #GtkTextIter
1655 * @trailing: (out) (allow-none): if non-%NULL, location to store an integer indicating where
1656 * in the grapheme the user clicked. It will either be
1657 * zero, or the number of characters in the grapheme.
1658 * 0 represents the trailing edge of the grapheme.
1659 * @x: x position, in buffer coordinates
1660 * @y: y position, in buffer coordinates
1662 * Retrieves the iterator pointing to the character at buffer
1663 * coordinates @x and @y. Buffer coordinates are coordinates for
1664 * the entire buffer, not just the currently-displayed portion.
1665 * If you have coordinates from an event, you have to convert
1666 * those to buffer coordinates with
1667 * gtk_text_view_window_to_buffer_coords().
1669 * Note that this is different from gtk_text_view_get_iter_at_location(),
1670 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1676 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1682 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1683 g_return_if_fail (iter != NULL);
1685 gtk_text_view_ensure_layout (text_view);
1687 gtk_text_layout_get_iter_at_position (text_view->priv->layout,
1688 iter, trailing, x, y);
1692 * gtk_text_view_get_iter_location:
1693 * @text_view: a #GtkTextView
1694 * @iter: a #GtkTextIter
1695 * @location: (out): bounds of the character at @iter
1697 * Gets a rectangle which roughly contains the character at @iter.
1698 * The rectangle position is in buffer coordinates; use
1699 * gtk_text_view_buffer_to_window_coords() to convert these
1700 * coordinates to coordinates for one of the windows in the text view.
1703 gtk_text_view_get_iter_location (GtkTextView *text_view,
1704 const GtkTextIter *iter,
1705 GdkRectangle *location)
1707 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1708 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1710 gtk_text_view_ensure_layout (text_view);
1712 gtk_text_layout_get_iter_location (text_view->priv->layout, iter, location);
1716 * gtk_text_view_get_line_yrange:
1717 * @text_view: a #GtkTextView
1718 * @iter: a #GtkTextIter
1719 * @y: (out): return location for a y coordinate
1720 * @height: (out): return location for a height
1722 * Gets the y coordinate of the top of the line containing @iter,
1723 * and the height of the line. The coordinate is a buffer coordinate;
1724 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1727 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1728 const GtkTextIter *iter,
1732 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1733 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1735 gtk_text_view_ensure_layout (text_view);
1737 gtk_text_layout_get_line_yrange (text_view->priv->layout,
1744 * gtk_text_view_get_line_at_y:
1745 * @text_view: a #GtkTextView
1746 * @target_iter: (out): a #GtkTextIter
1747 * @y: a y coordinate
1748 * @line_top: (out): return location for top coordinate of the line
1750 * Gets the #GtkTextIter at the start of the line containing
1751 * the coordinate @y. @y is in buffer coordinates, convert from
1752 * window coordinates with gtk_text_view_window_to_buffer_coords().
1753 * If non-%NULL, @line_top will be filled with the coordinate of the top
1757 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1758 GtkTextIter *target_iter,
1762 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1764 gtk_text_view_ensure_layout (text_view);
1766 gtk_text_layout_get_line_at_y (text_view->priv->layout,
1773 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1775 DV (g_print (" Setting adj to raw value %g\n", val));
1777 /* We don't really want to clamp to upper; we want to clamp to
1778 upper - page_size which is the highest value the scrollbar
1779 will let us reach. */
1780 if (val > (adj->upper - adj->page_size))
1781 val = adj->upper - adj->page_size;
1783 if (val < adj->lower)
1786 if (val != adj->value)
1788 DV (g_print (" Setting adj to clamped value %g\n", val));
1789 gtk_adjustment_set_value (adj, val);
1797 * gtk_text_view_scroll_to_iter:
1798 * @text_view: a #GtkTextView
1799 * @iter: a #GtkTextIter
1800 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1801 * @use_align: whether to use alignment arguments (if %FALSE,
1802 * just get the mark onscreen)
1803 * @xalign: horizontal alignment of mark within visible area
1804 * @yalign: vertical alignment of mark within visible area
1806 * Scrolls @text_view so that @iter is on the screen in the position
1807 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1808 * left or top, 1.0 indicates right or bottom, 0.5 means center.
1809 * If @use_align is %FALSE, the text scrolls the minimal distance to
1810 * get the mark onscreen, possibly not scrolling at all. The effective
1811 * screen for purposes of this function is reduced by a margin of size
1814 * Note that this function uses the currently-computed height of the
1815 * lines in the text buffer. Line heights are computed in an idle
1816 * handler; so this function may not have the desired effect if it's
1817 * called before the height computations. To avoid oddness, consider
1818 * using gtk_text_view_scroll_to_mark() which saves a point to be
1819 * scrolled to after line validation.
1821 * Return value: %TRUE if scrolling occurred
1824 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1826 gdouble within_margin,
1832 GdkRectangle screen;
1837 gboolean retval = FALSE;
1839 gint screen_xoffset, screen_yoffset;
1840 gint current_x_scroll, current_y_scroll;
1842 /* FIXME why don't we do the validate-at-scroll-destination thing
1843 * from flush_scroll in this function? I think it wasn't done before
1844 * because changed_handler was screwed up, but I could be wrong.
1847 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1848 g_return_val_if_fail (iter != NULL, FALSE);
1849 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1850 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1851 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1853 widget = GTK_WIDGET (text_view);
1855 DV(g_print(G_STRLOC"\n"));
1857 gtk_text_layout_get_iter_location (text_view->priv->layout,
1861 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1863 current_x_scroll = text_view->priv->xoffset;
1864 current_y_scroll = text_view->priv->yoffset;
1866 screen.x = current_x_scroll;
1867 screen.y = current_y_scroll;
1868 screen.width = SCREEN_WIDTH (widget);
1869 screen.height = SCREEN_HEIGHT (widget);
1871 screen_xoffset = screen.width * within_margin;
1872 screen_yoffset = screen.height * within_margin;
1874 screen.x += screen_xoffset;
1875 screen.y += screen_yoffset;
1876 screen.width -= screen_xoffset * 2;
1877 screen.height -= screen_yoffset * 2;
1879 /* paranoia check */
1880 if (screen.width < 1)
1882 if (screen.height < 1)
1885 /* The -1 here ensures that we leave enough space to draw the cursor
1886 * when this function is used for horizontal scrolling.
1888 screen_right = screen.x + screen.width - 1;
1889 screen_bottom = screen.y + screen.height;
1891 /* The alignment affects the point in the target character that we
1892 * choose to align. If we're doing right/bottom alignment, we align
1893 * the right/bottom edge of the character the mark is at; if we're
1894 * doing left/top we align the left/top edge of the character; if
1895 * we're doing center alignment we align the center of the
1899 /* Vertical scroll */
1902 scroll_dest = current_y_scroll;
1906 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1908 /* if scroll_dest < screen.y, we move a negative increment (up),
1909 * else a positive increment (down)
1911 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1915 /* move minimum to get onscreen */
1916 if (rect.y < screen.y)
1918 scroll_dest = rect.y;
1919 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1921 else if ((rect.y + rect.height) > screen_bottom)
1923 scroll_dest = rect.y + rect.height;
1924 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1928 if (scroll_inc != 0)
1930 retval = set_adjustment_clamped (get_vadjustment (text_view),
1931 current_y_scroll + scroll_inc);
1933 DV (g_print (" vert increment %d\n", scroll_inc));
1936 /* Horizontal scroll */
1939 scroll_dest = current_x_scroll;
1943 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1945 /* if scroll_dest < screen.y, we move a negative increment (left),
1946 * else a positive increment (right)
1948 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1952 /* move minimum to get onscreen */
1953 if (rect.x < screen.x)
1955 scroll_dest = rect.x;
1956 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1958 else if ((rect.x + rect.width) > screen_right)
1960 scroll_dest = rect.x + rect.width;
1961 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1965 if (scroll_inc != 0)
1967 retval = set_adjustment_clamped (get_hadjustment (text_view),
1968 current_x_scroll + scroll_inc);
1970 DV (g_print (" horiz increment %d\n", scroll_inc));
1975 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1979 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1986 free_pending_scroll (GtkTextPendingScroll *scroll)
1988 if (!gtk_text_mark_get_deleted (scroll->mark))
1989 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1991 g_object_unref (scroll->mark);
1996 cancel_pending_scroll (GtkTextView *text_view)
1998 if (text_view->priv->pending_scroll)
2000 free_pending_scroll (text_view->priv->pending_scroll);
2001 text_view->priv->pending_scroll = NULL;
2006 gtk_text_view_queue_scroll (GtkTextView *text_view,
2008 gdouble within_margin,
2014 GtkTextPendingScroll *scroll;
2016 DV(g_print(G_STRLOC"\n"));
2018 scroll = g_new (GtkTextPendingScroll, 1);
2020 scroll->within_margin = within_margin;
2021 scroll->use_align = use_align;
2022 scroll->xalign = xalign;
2023 scroll->yalign = yalign;
2025 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2027 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
2030 gtk_text_mark_get_left_gravity (mark));
2032 g_object_ref (scroll->mark);
2034 cancel_pending_scroll (text_view);
2036 text_view->priv->pending_scroll = scroll;
2040 gtk_text_view_flush_scroll (GtkTextView *text_view)
2042 GtkAllocation allocation;
2044 GtkTextPendingScroll *scroll;
2048 widget = GTK_WIDGET (text_view);
2050 DV(g_print(G_STRLOC"\n"));
2052 if (text_view->priv->pending_scroll == NULL)
2054 DV (g_print ("in flush scroll, no pending scroll\n"));
2058 scroll = text_view->priv->pending_scroll;
2060 /* avoid recursion */
2061 text_view->priv->pending_scroll = NULL;
2063 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
2065 /* Validate area around the scroll destination, so the adjustment
2066 * can meaningfully point into that area. We must validate
2067 * enough area to be sure that after we scroll, everything onscreen
2068 * is valid; otherwise, validation will maintain the first para
2069 * in one place, but may push the target iter off the bottom of
2072 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
2073 gtk_widget_get_allocation (widget, &allocation);
2074 gtk_text_layout_validate_yrange (text_view->priv->layout, &iter,
2075 -(allocation.height * 2),
2076 allocation.height * 2);
2078 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
2080 /* Ensure we have updated width/height */
2081 gtk_text_view_update_adjustments (text_view);
2083 retval = gtk_text_view_scroll_to_iter (text_view,
2085 scroll->within_margin,
2090 free_pending_scroll (scroll);
2096 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
2098 if (upper != adj->upper)
2100 gdouble min = MAX (0.0, upper - adj->page_size);
2101 gboolean value_changed = FALSE;
2105 if (adj->value > min)
2108 value_changed = TRUE;
2111 gtk_adjustment_changed (adj);
2112 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
2116 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
2117 gtk_adjustment_value_changed (adj);
2123 gtk_text_view_update_adjustments (GtkTextView *text_view)
2125 GtkTextViewPrivate *priv;
2126 gint width = 0, height = 0;
2128 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
2130 priv = text_view->priv;
2133 gtk_text_layout_get_size (priv->layout, &width, &height);
2135 /* Make room for the cursor after the last character in the widest line */
2136 width += SPACE_FOR_CURSOR;
2138 if (priv->width != width || priv->height != height)
2140 if (priv->width != width)
2141 priv->width_changed = TRUE;
2143 priv->width = width;
2144 priv->height = height;
2146 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
2147 MAX (SCREEN_WIDTH (text_view), width));
2148 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
2149 MAX (SCREEN_HEIGHT (text_view), height));
2151 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
2153 /* Set up the step sizes; we'll say that a page is
2154 our allocation minus one step, and a step is
2155 1/10 of our allocation. */
2156 priv->hadjustment->step_increment =
2157 SCREEN_WIDTH (text_view) / 10.0;
2158 priv->hadjustment->page_increment =
2159 SCREEN_WIDTH (text_view) * 0.9;
2161 priv->vadjustment->step_increment =
2162 SCREEN_HEIGHT (text_view) / 10.0;
2163 priv->vadjustment->page_increment =
2164 SCREEN_HEIGHT (text_view) * 0.9;
2166 gtk_adjustment_changed (get_hadjustment (text_view));
2167 gtk_adjustment_changed (get_vadjustment (text_view));
2172 gtk_text_view_update_layout_width (GtkTextView *text_view)
2174 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
2176 gtk_text_view_ensure_layout (text_view);
2178 gtk_text_layout_set_screen_width (text_view->priv->layout,
2179 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
2183 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
2187 if (text_view->priv->layout == NULL)
2190 gtk_text_view_get_cursor_location (text_view, &area);
2192 area.x -= text_view->priv->xoffset;
2193 area.y -= text_view->priv->yoffset;
2195 /* Width returned by Pango indicates direction of cursor,
2196 * by it's sign more than the size of cursor.
2200 gtk_im_context_set_cursor_location (text_view->priv->im_context, &area);
2204 do_update_im_spot_location (gpointer text_view)
2206 GtkTextViewPrivate *priv;
2208 priv = GTK_TEXT_VIEW (text_view)->priv;
2209 priv->im_spot_idle = 0;
2211 gtk_text_view_update_im_spot_location (text_view);
2216 queue_update_im_spot_location (GtkTextView *text_view)
2218 GtkTextViewPrivate *priv;
2220 priv = text_view->priv;
2222 /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
2223 * so we don't wait until the entire buffer has been validated. */
2224 if (!priv->im_spot_idle)
2225 priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
2226 do_update_im_spot_location,
2232 flush_update_im_spot_location (GtkTextView *text_view)
2234 GtkTextViewPrivate *priv;
2236 priv = text_view->priv;
2238 if (priv->im_spot_idle)
2240 g_source_remove (priv->im_spot_idle);
2241 priv->im_spot_idle = 0;
2242 gtk_text_view_update_im_spot_location (text_view);
2247 * gtk_text_view_scroll_to_mark:
2248 * @text_view: a #GtkTextView
2249 * @mark: a #GtkTextMark
2250 * @within_margin: margin as a [0.0,0.5) fraction of screen size
2251 * @use_align: whether to use alignment arguments (if %FALSE, just
2252 * get the mark onscreen)
2253 * @xalign: horizontal alignment of mark within visible area
2254 * @yalign: vertical alignment of mark within visible area
2256 * Scrolls @text_view so that @mark is on the screen in the position
2257 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
2258 * left or top, 1.0 indicates right or bottom, 0.5 means center.
2259 * If @use_align is %FALSE, the text scrolls the minimal distance to
2260 * get the mark onscreen, possibly not scrolling at all. The effective
2261 * screen for purposes of this function is reduced by a margin of size
2265 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
2267 gdouble within_margin,
2272 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2273 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2274 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
2275 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
2276 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
2278 /* We need to verify that the buffer contains the mark, otherwise this
2279 * can lead to data structure corruption later on.
2281 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2283 gtk_text_view_queue_scroll (text_view, mark,
2289 /* If no validation is pending, we need to go ahead and force an
2292 if (text_view->priv->layout &&
2293 gtk_text_layout_is_valid (text_view->priv->layout))
2294 gtk_text_view_flush_scroll (text_view);
2298 * gtk_text_view_scroll_mark_onscreen:
2299 * @text_view: a #GtkTextView
2300 * @mark: a mark in the buffer for @text_view
2302 * Scrolls @text_view the minimum distance such that @mark is contained
2303 * within the visible area of the widget.
2306 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
2309 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2310 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2312 /* We need to verify that the buffer contains the mark, otherwise this
2313 * can lead to data structure corruption later on.
2315 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2317 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
2321 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
2323 GdkRectangle visible_rect;
2324 gtk_text_view_get_visible_rect (text_view, &visible_rect);
2326 return gtk_text_layout_clamp_iter_to_vrange (text_view->priv->layout, iter,
2328 visible_rect.y + visible_rect.height);
2332 * gtk_text_view_move_mark_onscreen:
2333 * @text_view: a #GtkTextView
2334 * @mark: a #GtkTextMark
2336 * Moves a mark within the buffer so that it's
2337 * located within the currently-visible text area.
2339 * Return value: %TRUE if the mark moved (wasn't already onscreen)
2342 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
2347 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2348 g_return_val_if_fail (mark != NULL, FALSE);
2350 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2352 if (clamp_iter_onscreen (text_view, &iter))
2354 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
2362 * gtk_text_view_get_visible_rect:
2363 * @text_view: a #GtkTextView
2364 * @visible_rect: rectangle to fill
2366 * Fills @visible_rect with the currently-visible
2367 * region of the buffer, in buffer coordinates. Convert to window coordinates
2368 * with gtk_text_view_buffer_to_window_coords().
2371 gtk_text_view_get_visible_rect (GtkTextView *text_view,
2372 GdkRectangle *visible_rect)
2376 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2378 widget = GTK_WIDGET (text_view);
2382 visible_rect->x = text_view->priv->xoffset;
2383 visible_rect->y = text_view->priv->yoffset;
2384 visible_rect->width = SCREEN_WIDTH (widget);
2385 visible_rect->height = SCREEN_HEIGHT (widget);
2387 DV(g_print(" visible rect: %d,%d %d x %d\n",
2390 visible_rect->width,
2391 visible_rect->height));
2396 * gtk_text_view_set_wrap_mode:
2397 * @text_view: a #GtkTextView
2398 * @wrap_mode: a #GtkWrapMode
2400 * Sets the line wrapping for the view.
2403 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
2404 GtkWrapMode wrap_mode)
2406 GtkTextViewPrivate *priv;
2408 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2410 priv = text_view->priv;
2412 if (priv->wrap_mode != wrap_mode)
2414 priv->wrap_mode = wrap_mode;
2418 priv->layout->default_style->wrap_mode = wrap_mode;
2419 gtk_text_layout_default_style_changed (priv->layout);
2423 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2427 * gtk_text_view_get_wrap_mode:
2428 * @text_view: a #GtkTextView
2430 * Gets the line wrapping for the view.
2432 * Return value: the line wrap setting
2435 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2437 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2439 return text_view->priv->wrap_mode;
2443 * gtk_text_view_set_editable:
2444 * @text_view: a #GtkTextView
2445 * @setting: whether it's editable
2447 * Sets the default editability of the #GtkTextView. You can override
2448 * this default setting with tags in the buffer, using the "editable"
2449 * attribute of tags.
2452 gtk_text_view_set_editable (GtkTextView *text_view,
2455 GtkTextViewPrivate *priv;
2457 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2459 priv = text_view->priv;
2460 setting = setting != FALSE;
2462 if (priv->editable != setting)
2466 gtk_text_view_reset_im_context(text_view);
2467 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2468 gtk_im_context_focus_out (priv->im_context);
2471 priv->editable = setting;
2473 if (setting && gtk_widget_has_focus (GTK_WIDGET (text_view)))
2474 gtk_im_context_focus_in (priv->im_context);
2478 gtk_text_layout_set_overwrite_mode (priv->layout,
2479 priv->overwrite_mode && priv->editable);
2480 priv->layout->default_style->editable = priv->editable;
2481 gtk_text_layout_default_style_changed (priv->layout);
2484 g_object_notify (G_OBJECT (text_view), "editable");
2489 * gtk_text_view_get_editable:
2490 * @text_view: a #GtkTextView
2492 * Returns the default editability of the #GtkTextView. Tags in the
2493 * buffer may override this setting for some ranges of text.
2495 * Return value: whether text is editable by default
2498 gtk_text_view_get_editable (GtkTextView *text_view)
2500 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2502 return text_view->priv->editable;
2506 * gtk_text_view_set_pixels_above_lines:
2507 * @text_view: a #GtkTextView
2508 * @pixels_above_lines: pixels above paragraphs
2510 * Sets the default number of blank pixels above paragraphs in @text_view.
2511 * Tags in the buffer for @text_view may override the defaults.
2514 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2515 gint pixels_above_lines)
2517 GtkTextViewPrivate *priv;
2519 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2521 priv = text_view->priv;
2523 if (priv->pixels_above_lines != pixels_above_lines)
2525 priv->pixels_above_lines = pixels_above_lines;
2529 priv->layout->default_style->pixels_above_lines = pixels_above_lines;
2530 gtk_text_layout_default_style_changed (priv->layout);
2533 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2538 * gtk_text_view_get_pixels_above_lines:
2539 * @text_view: a #GtkTextView
2541 * Gets the default number of pixels to put above paragraphs.
2543 * Return value: default number of pixels above paragraphs
2546 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2548 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2550 return text_view->priv->pixels_above_lines;
2554 * gtk_text_view_set_pixels_below_lines:
2555 * @text_view: a #GtkTextView
2556 * @pixels_below_lines: pixels below paragraphs
2558 * Sets the default number of pixels of blank space
2559 * to put below paragraphs in @text_view. May be overridden
2560 * by tags applied to @text_view's buffer.
2563 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2564 gint pixels_below_lines)
2566 GtkTextViewPrivate *priv;
2568 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2570 priv = text_view->priv;
2572 if (priv->pixels_below_lines != pixels_below_lines)
2574 priv->pixels_below_lines = pixels_below_lines;
2578 priv->layout->default_style->pixels_below_lines = pixels_below_lines;
2579 gtk_text_layout_default_style_changed (priv->layout);
2582 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2587 * gtk_text_view_get_pixels_below_lines:
2588 * @text_view: a #GtkTextView
2590 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2592 * Return value: default number of blank pixels below paragraphs
2595 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2597 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2599 return text_view->priv->pixels_below_lines;
2603 * gtk_text_view_set_pixels_inside_wrap:
2604 * @text_view: a #GtkTextView
2605 * @pixels_inside_wrap: default number of pixels between wrapped lines
2607 * Sets the default number of pixels of blank space to leave between
2608 * display/wrapped lines within a paragraph. May be overridden by
2609 * tags in @text_view's buffer.
2612 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2613 gint pixels_inside_wrap)
2615 GtkTextViewPrivate *priv;
2617 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2619 priv = text_view->priv;
2621 if (priv->pixels_inside_wrap != pixels_inside_wrap)
2623 priv->pixels_inside_wrap = pixels_inside_wrap;
2627 priv->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2628 gtk_text_layout_default_style_changed (priv->layout);
2631 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2636 * gtk_text_view_get_pixels_inside_wrap:
2637 * @text_view: a #GtkTextView
2639 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2641 * Return value: default number of pixels of blank space between wrapped lines
2644 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2646 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2648 return text_view->priv->pixels_inside_wrap;
2652 * gtk_text_view_set_justification:
2653 * @text_view: a #GtkTextView
2654 * @justification: justification
2656 * Sets the default justification of text in @text_view.
2657 * Tags in the view's buffer may override the default.
2661 gtk_text_view_set_justification (GtkTextView *text_view,
2662 GtkJustification justification)
2664 GtkTextViewPrivate *priv;
2666 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2668 priv = text_view->priv;
2670 if (priv->justify != justification)
2672 priv->justify = justification;
2676 priv->layout->default_style->justification = justification;
2677 gtk_text_layout_default_style_changed (priv->layout);
2680 g_object_notify (G_OBJECT (text_view), "justification");
2685 * gtk_text_view_get_justification:
2686 * @text_view: a #GtkTextView
2688 * Gets the default justification of paragraphs in @text_view.
2689 * Tags in the buffer may override the default.
2691 * Return value: default justification
2694 gtk_text_view_get_justification (GtkTextView *text_view)
2696 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2698 return text_view->priv->justify;
2702 * gtk_text_view_set_left_margin:
2703 * @text_view: a #GtkTextView
2704 * @left_margin: left margin in pixels
2706 * Sets the default left margin for text in @text_view.
2707 * Tags in the buffer may override the default.
2710 gtk_text_view_set_left_margin (GtkTextView *text_view,
2713 GtkTextViewPrivate *priv;
2715 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2717 priv = text_view->priv;
2719 if (priv->left_margin != left_margin)
2721 priv->left_margin = left_margin;
2725 priv->layout->default_style->left_margin = left_margin;
2726 gtk_text_layout_default_style_changed (priv->layout);
2729 g_object_notify (G_OBJECT (text_view), "left-margin");
2734 * gtk_text_view_get_left_margin:
2735 * @text_view: a #GtkTextView
2737 * Gets the default left margin size of paragraphs in the @text_view.
2738 * Tags in the buffer may override the default.
2740 * Return value: left margin in pixels
2743 gtk_text_view_get_left_margin (GtkTextView *text_view)
2745 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2747 return text_view->priv->left_margin;
2751 * gtk_text_view_set_right_margin:
2752 * @text_view: a #GtkTextView
2753 * @right_margin: right margin in pixels
2755 * Sets the default right margin for text in the text view.
2756 * Tags in the buffer may override the default.
2759 gtk_text_view_set_right_margin (GtkTextView *text_view,
2762 GtkTextViewPrivate *priv = text_view->priv;
2764 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2766 if (priv->right_margin != right_margin)
2768 priv->right_margin = right_margin;
2772 priv->layout->default_style->right_margin = right_margin;
2773 gtk_text_layout_default_style_changed (priv->layout);
2776 g_object_notify (G_OBJECT (text_view), "right-margin");
2781 * gtk_text_view_get_right_margin:
2782 * @text_view: a #GtkTextView
2784 * Gets the default right margin for text in @text_view. Tags
2785 * in the buffer may override the default.
2787 * Return value: right margin in pixels
2790 gtk_text_view_get_right_margin (GtkTextView *text_view)
2792 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2794 return text_view->priv->right_margin;
2798 * gtk_text_view_set_indent:
2799 * @text_view: a #GtkTextView
2800 * @indent: indentation in pixels
2802 * Sets the default indentation for paragraphs in @text_view.
2803 * Tags in the buffer may override the default.
2806 gtk_text_view_set_indent (GtkTextView *text_view,
2809 GtkTextViewPrivate *priv;
2811 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2813 priv = text_view->priv;
2815 if (priv->indent != indent)
2817 priv->indent = indent;
2821 priv->layout->default_style->indent = indent;
2822 gtk_text_layout_default_style_changed (priv->layout);
2825 g_object_notify (G_OBJECT (text_view), "indent");
2830 * gtk_text_view_get_indent:
2831 * @text_view: a #GtkTextView
2833 * Gets the default indentation of paragraphs in @text_view.
2834 * Tags in the view's buffer may override the default.
2835 * The indentation may be negative.
2837 * Return value: number of pixels of indentation
2840 gtk_text_view_get_indent (GtkTextView *text_view)
2842 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2844 return text_view->priv->indent;
2848 * gtk_text_view_set_tabs:
2849 * @text_view: a #GtkTextView
2850 * @tabs: tabs as a #PangoTabArray
2852 * Sets the default tab stops for paragraphs in @text_view.
2853 * Tags in the buffer may override the default.
2856 gtk_text_view_set_tabs (GtkTextView *text_view,
2857 PangoTabArray *tabs)
2859 GtkTextViewPrivate *priv;
2861 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2863 priv = text_view->priv;
2866 pango_tab_array_free (priv->tabs);
2868 priv->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2872 /* some unkosher futzing in internal struct details... */
2873 if (priv->layout->default_style->tabs)
2874 pango_tab_array_free (priv->layout->default_style->tabs);
2876 priv->layout->default_style->tabs =
2877 priv->tabs ? pango_tab_array_copy (priv->tabs) : NULL;
2879 gtk_text_layout_default_style_changed (priv->layout);
2882 g_object_notify (G_OBJECT (text_view), "tabs");
2886 * gtk_text_view_get_tabs:
2887 * @text_view: a #GtkTextView
2889 * Gets the default tabs for @text_view. Tags in the buffer may
2890 * override the defaults. The returned array will be %NULL if
2891 * "standard" (8-space) tabs are used. Free the return value
2892 * with pango_tab_array_free().
2894 * Return value: copy of default tab array, or %NULL if "standard"
2895 * tabs are used; must be freed with pango_tab_array_free().
2898 gtk_text_view_get_tabs (GtkTextView *text_view)
2900 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2902 return text_view->priv->tabs ? pango_tab_array_copy (text_view->priv->tabs) : NULL;
2906 gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
2908 gtk_text_view_set_cursor_visible (text_view, !text_view->priv->cursor_visible);
2912 * gtk_text_view_set_cursor_visible:
2913 * @text_view: a #GtkTextView
2914 * @setting: whether to show the insertion cursor
2916 * Toggles whether the insertion point is displayed. A buffer with no editable
2917 * text probably shouldn't have a visible cursor, so you may want to turn
2921 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2924 GtkTextViewPrivate *priv;
2926 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2928 priv = text_view->priv;
2929 setting = (setting != FALSE);
2931 if (priv->cursor_visible != setting)
2933 priv->cursor_visible = setting;
2935 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2939 gtk_text_layout_set_cursor_visible (priv->layout, setting);
2940 gtk_text_view_check_cursor_blink (text_view);
2944 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2949 * gtk_text_view_get_cursor_visible:
2950 * @text_view: a #GtkTextView
2952 * Find out whether the cursor is being displayed.
2954 * Return value: whether the insertion mark is visible
2957 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2959 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2961 return text_view->priv->cursor_visible;
2966 * gtk_text_view_place_cursor_onscreen:
2967 * @text_view: a #GtkTextView
2969 * Moves the cursor to the currently visible region of the
2970 * buffer, it it isn't there already.
2972 * Return value: %TRUE if the cursor had to be moved.
2975 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2979 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2981 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2982 gtk_text_buffer_get_insert (get_buffer (text_view)));
2984 if (clamp_iter_onscreen (text_view, &insert))
2986 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2994 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2996 GtkTextViewPrivate *priv = text_view->priv;
2998 if (priv->first_validate_idle != 0)
3000 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
3001 g_source_remove (priv->first_validate_idle);
3002 priv->first_validate_idle = 0;
3005 if (priv->incremental_validate_idle != 0)
3007 g_source_remove (priv->incremental_validate_idle);
3008 priv->incremental_validate_idle = 0;
3013 gtk_text_view_destroy (GtkObject *object)
3015 GtkTextView *text_view;
3016 GtkTextViewPrivate *priv;
3018 text_view = GTK_TEXT_VIEW (object);
3019 priv = text_view->priv;
3021 gtk_text_view_remove_validate_idles (text_view);
3022 gtk_text_view_set_buffer (text_view, NULL);
3023 gtk_text_view_destroy_layout (text_view);
3025 if (text_view->priv->scroll_timeout)
3027 g_source_remove (text_view->priv->scroll_timeout);
3028 text_view->priv->scroll_timeout = 0;
3031 if (priv->im_spot_idle)
3033 g_source_remove (priv->im_spot_idle);
3034 priv->im_spot_idle = 0;
3037 GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy (object);
3041 gtk_text_view_finalize (GObject *object)
3043 GtkTextView *text_view;
3044 GtkTextViewPrivate *priv;
3046 text_view = GTK_TEXT_VIEW (object);
3047 priv = text_view->priv;
3049 g_assert (priv->buffer == NULL);
3051 gtk_text_view_destroy_layout (text_view);
3052 gtk_text_view_set_buffer (text_view, NULL);
3054 cancel_pending_scroll (text_view);
3057 pango_tab_array_free (priv->tabs);
3059 if (priv->hadjustment)
3060 g_object_unref (priv->hadjustment);
3061 if (priv->vadjustment)
3062 g_object_unref (priv->vadjustment);
3064 text_window_free (priv->text_window);
3066 if (priv->left_window)
3067 text_window_free (priv->left_window);
3069 if (priv->top_window)
3070 text_window_free (priv->top_window);
3072 if (priv->right_window)
3073 text_window_free (priv->right_window);
3075 if (priv->bottom_window)
3076 text_window_free (priv->bottom_window);
3078 g_object_unref (priv->im_context);
3080 g_free (priv->im_module);
3082 G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize (object);
3086 gtk_text_view_set_property (GObject *object,
3088 const GValue *value,
3091 GtkTextView *text_view;
3092 GtkTextViewPrivate *priv;
3094 text_view = GTK_TEXT_VIEW (object);
3095 priv = text_view->priv;
3099 case PROP_PIXELS_ABOVE_LINES:
3100 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
3103 case PROP_PIXELS_BELOW_LINES:
3104 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
3107 case PROP_PIXELS_INSIDE_WRAP:
3108 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
3112 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
3115 case PROP_WRAP_MODE:
3116 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
3119 case PROP_JUSTIFICATION:
3120 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
3123 case PROP_LEFT_MARGIN:
3124 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
3127 case PROP_RIGHT_MARGIN:
3128 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
3132 gtk_text_view_set_indent (text_view, g_value_get_int (value));
3136 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
3139 case PROP_CURSOR_VISIBLE:
3140 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
3143 case PROP_OVERWRITE:
3144 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
3148 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
3151 case PROP_ACCEPTS_TAB:
3152 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
3155 case PROP_IM_MODULE:
3156 g_free (priv->im_module);
3157 priv->im_module = g_value_dup_string (value);
3158 if (GTK_IS_IM_MULTICONTEXT (priv->im_context))
3159 gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (priv->im_context), priv->im_module);
3163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3169 gtk_text_view_get_property (GObject *object,
3174 GtkTextView *text_view;
3175 GtkTextViewPrivate *priv;
3177 text_view = GTK_TEXT_VIEW (object);
3178 priv = text_view->priv;
3182 case PROP_PIXELS_ABOVE_LINES:
3183 g_value_set_int (value, priv->pixels_above_lines);
3186 case PROP_PIXELS_BELOW_LINES:
3187 g_value_set_int (value, priv->pixels_below_lines);
3190 case PROP_PIXELS_INSIDE_WRAP:
3191 g_value_set_int (value, priv->pixels_inside_wrap);
3195 g_value_set_boolean (value, priv->editable);
3198 case PROP_WRAP_MODE:
3199 g_value_set_enum (value, priv->wrap_mode);
3202 case PROP_JUSTIFICATION:
3203 g_value_set_enum (value, priv->justify);
3206 case PROP_LEFT_MARGIN:
3207 g_value_set_int (value, priv->left_margin);
3210 case PROP_RIGHT_MARGIN:
3211 g_value_set_int (value, priv->right_margin);
3215 g_value_set_int (value, priv->indent);
3219 g_value_set_boxed (value, priv->tabs);
3222 case PROP_CURSOR_VISIBLE:
3223 g_value_set_boolean (value, priv->cursor_visible);
3227 g_value_set_object (value, get_buffer (text_view));
3230 case PROP_OVERWRITE:
3231 g_value_set_boolean (value, priv->overwrite_mode);
3234 case PROP_ACCEPTS_TAB:
3235 g_value_set_boolean (value, priv->accepts_tab);
3238 case PROP_IM_MODULE:
3239 g_value_set_string (value, priv->im_module);
3243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3249 gtk_text_view_size_request (GtkWidget *widget,
3250 GtkRequisition *requisition)
3252 GtkTextView *text_view;
3253 GtkTextViewPrivate *priv;
3255 gint focus_edge_width;
3258 gboolean interior_focus;
3260 text_view = GTK_TEXT_VIEW (widget);
3261 priv = text_view->priv;
3263 gtk_widget_style_get (widget,
3264 "interior-focus", &interior_focus,
3265 "focus-line-width", &focus_width,
3269 focus_edge_width = 0;
3271 focus_edge_width = focus_width;
3275 priv->text_window->requisition.width = priv->layout->width;
3276 priv->text_window->requisition.height = priv->layout->height;
3280 priv->text_window->requisition.width = 0;
3281 priv->text_window->requisition.height = 0;
3284 requisition->width = priv->text_window->requisition.width + focus_edge_width * 2;
3285 requisition->height = priv->text_window->requisition.height + focus_edge_width * 2;
3287 if (priv->left_window)
3288 requisition->width += priv->left_window->requisition.width;
3290 if (priv->right_window)
3291 requisition->width += priv->right_window->requisition.width;
3293 if (priv->top_window)
3294 requisition->height += priv->top_window->requisition.height;
3296 if (priv->bottom_window)
3297 requisition->height += priv->bottom_window->requisition.height;
3299 border_width = gtk_container_get_border_width (GTK_CONTAINER (text_view));
3300 requisition->width += border_width * 2;
3301 requisition->height += border_width * 2;
3303 tmp_list = priv->children;
3304 while (tmp_list != NULL)
3306 GtkTextViewChild *child = tmp_list->data;
3310 GtkRequisition child_req;
3311 GtkRequisition old_req;
3313 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->widget),
3316 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->widget),
3319 /* Invalidate layout lines if required */
3321 (old_req.width != child_req.width ||
3322 old_req.height != child_req.height))
3323 gtk_text_child_anchor_queue_resize (child->anchor,
3328 GtkRequisition child_req;
3330 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->widget),
3334 tmp_list = g_slist_next (tmp_list);
3337 /* Cache the requested size of the text view so we can
3338 * compare it in the changed_handler() */
3339 priv->cached_size_request = *requisition;
3343 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
3344 GtkTextViewChild *vc,
3345 GtkAllocation *allocation)
3351 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3355 gtk_text_layout_get_line_yrange (text_view->priv->layout, &iter,
3358 buffer_y += vc->from_top_of_line;
3360 allocation->x = vc->from_left_of_buffer - text_view->priv->xoffset;
3361 allocation->y = buffer_y - text_view->priv->yoffset;
3363 gtk_size_request_get_size (GTK_SIZE_REQUEST (vc->widget),
3365 allocation->width = req.width;
3366 allocation->height = req.height;
3370 gtk_text_view_update_child_allocation (GtkTextView *text_view,
3371 GtkTextViewChild *vc)
3373 GtkAllocation allocation;
3375 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
3377 gtk_widget_size_allocate (vc->widget, &allocation);
3380 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
3382 vc->widget->allocation.x,
3383 vc->widget->allocation.y,
3384 text_view->priv->yoffset);
3389 gtk_text_view_child_allocated (GtkTextLayout *layout,
3395 GtkTextViewChild *vc = NULL;
3396 GtkTextView *text_view = data;
3398 /* x,y is the position of the child from the top of the line, and
3399 * from the left of the buffer. We have to translate that into text
3400 * window coordinates, then size_allocate the child.
3403 vc = g_object_get_data (G_OBJECT (child),
3404 "gtk-text-view-child");
3406 g_assert (vc != NULL);
3408 DV (g_print ("child allocated at %d,%d\n", x, y));
3410 vc->from_left_of_buffer = x;
3411 vc->from_top_of_line = y;
3413 gtk_text_view_update_child_allocation (text_view, vc);
3417 gtk_text_view_allocate_children (GtkTextView *text_view)
3421 DV(g_print(G_STRLOC"\n"));
3423 tmp_list = text_view->priv->children;
3424 while (tmp_list != NULL)
3426 GtkTextViewChild *child = tmp_list->data;
3428 g_assert (child != NULL);
3432 /* We need to force-validate the regions containing
3435 GtkTextIter child_loc;
3436 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3440 /* Since anchored children are only ever allocated from
3441 * gtk_text_layout_get_line_display() we have to make sure
3442 * that the display line caching in the layout doesn't
3443 * get in the way. Invalidating the layout around the anchor
3446 if (GTK_WIDGET_ALLOC_NEEDED (child->widget))
3448 GtkTextIter end = child_loc;
3449 gtk_text_iter_forward_char (&end);
3450 gtk_text_layout_invalidate (text_view->priv->layout, &child_loc, &end);
3453 gtk_text_layout_validate_yrange (text_view->priv->layout,
3459 GtkAllocation allocation;
3460 GtkRequisition child_req;
3462 allocation.x = child->x;
3463 allocation.y = child->y;
3465 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->widget),
3468 allocation.width = child_req.width;
3469 allocation.height = child_req.height;
3471 gtk_widget_size_allocate (child->widget, &allocation);
3474 tmp_list = g_slist_next (tmp_list);
3479 gtk_text_view_size_allocate (GtkWidget *widget,
3480 GtkAllocation *allocation)
3482 GtkAllocation widget_allocation;
3483 GtkTextView *text_view;
3484 GtkTextViewPrivate *priv;
3485 GtkTextIter first_para;
3488 GdkRectangle text_rect;
3489 GdkRectangle left_rect;
3490 GdkRectangle right_rect;
3491 GdkRectangle top_rect;
3492 GdkRectangle bottom_rect;
3493 gint focus_edge_width;
3496 gboolean interior_focus;
3497 gboolean size_changed;
3499 text_view = GTK_TEXT_VIEW (widget);
3500 priv = text_view->priv;
3502 DV(g_print(G_STRLOC"\n"));
3504 gtk_widget_get_allocation (widget, &widget_allocation);
3506 widget_allocation.width != allocation->width ||
3507 widget_allocation.height != allocation->height;
3509 border_width = gtk_container_get_border_width (GTK_CONTAINER (text_view));
3511 gtk_widget_set_allocation (widget, allocation);
3513 if (gtk_widget_get_realized (widget))
3515 gdk_window_move_resize (gtk_widget_get_window (widget),
3516 allocation->x, allocation->y,
3517 allocation->width, allocation->height);
3520 /* distribute width/height among child windows. Ensure all
3521 * windows get at least a 1x1 allocation.
3524 gtk_widget_style_get (widget,
3525 "interior-focus", &interior_focus,
3526 "focus-line-width", &focus_width,
3530 focus_edge_width = 0;
3532 focus_edge_width = focus_width;
3534 width = allocation->width - focus_edge_width * 2 - border_width * 2;
3536 if (priv->left_window)
3537 left_rect.width = priv->left_window->requisition.width;
3539 left_rect.width = 0;
3541 width -= left_rect.width;
3543 if (priv->right_window)
3544 right_rect.width = priv->right_window->requisition.width;
3546 right_rect.width = 0;
3548 width -= right_rect.width;
3550 text_rect.width = MAX (1, width);
3552 top_rect.width = text_rect.width;
3553 bottom_rect.width = text_rect.width;
3556 height = allocation->height - focus_edge_width * 2 - border_width * 2;
3558 if (priv->top_window)
3559 top_rect.height = priv->top_window->requisition.height;
3561 top_rect.height = 0;
3563 height -= top_rect.height;
3565 if (priv->bottom_window)
3566 bottom_rect.height = priv->bottom_window->requisition.height;
3568 bottom_rect.height = 0;
3570 height -= bottom_rect.height;
3572 text_rect.height = MAX (1, height);
3574 left_rect.height = text_rect.height;
3575 right_rect.height = text_rect.height;
3578 left_rect.x = focus_edge_width + border_width;
3579 top_rect.y = focus_edge_width + border_width;
3581 text_rect.x = left_rect.x + left_rect.width;
3582 text_rect.y = top_rect.y + top_rect.height;
3584 left_rect.y = text_rect.y;
3585 right_rect.y = text_rect.y;
3587 top_rect.x = text_rect.x;
3588 bottom_rect.x = text_rect.x;
3590 right_rect.x = text_rect.x + text_rect.width;
3591 bottom_rect.y = text_rect.y + text_rect.height;
3593 text_window_size_allocate (priv->text_window,
3596 if (priv->left_window)
3597 text_window_size_allocate (priv->left_window,
3600 if (priv->right_window)
3601 text_window_size_allocate (priv->right_window,
3604 if (priv->top_window)
3605 text_window_size_allocate (priv->top_window,
3608 if (priv->bottom_window)
3609 text_window_size_allocate (priv->bottom_window,
3612 gtk_text_view_update_layout_width (text_view);
3614 /* Note that this will do some layout validation */
3615 gtk_text_view_allocate_children (text_view);
3617 /* Ensure h/v adj exist */
3618 get_hadjustment (text_view);
3619 get_vadjustment (text_view);
3621 priv->hadjustment->page_size = SCREEN_WIDTH (text_view);
3622 priv->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
3623 priv->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
3624 priv->hadjustment->lower = 0;
3625 priv->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
3628 if (priv->hadjustment->value > priv->hadjustment->upper - priv->hadjustment->page_size)
3629 gtk_adjustment_set_value (priv->hadjustment, MAX (0, priv->hadjustment->upper - priv->hadjustment->page_size));
3631 gtk_adjustment_changed (priv->hadjustment);
3633 priv->vadjustment->page_size = SCREEN_HEIGHT (text_view);
3634 priv->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
3635 priv->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
3636 priv->vadjustment->lower = 0;
3637 priv->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
3640 /* Now adjust the value of the adjustment to keep the cursor at the
3641 * same place in the buffer
3643 gtk_text_view_get_first_para_iter (text_view, &first_para);
3644 gtk_text_layout_get_line_yrange (priv->layout, &first_para, &y, NULL);
3646 y += priv->first_para_pixels;
3648 if (y > priv->vadjustment->upper - priv->vadjustment->page_size)
3649 y = MAX (0, priv->vadjustment->upper - priv->vadjustment->page_size);
3651 if (y != priv->yoffset)
3652 gtk_adjustment_set_value (priv->vadjustment, y);
3654 gtk_adjustment_changed (priv->vadjustment);
3656 /* The GTK resize loop processes all the pending exposes right
3657 * after doing the resize stuff, so the idle sizer won't have a
3658 * chance to run. So we do the work here.
3660 gtk_text_view_flush_first_validate (text_view);
3662 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3665 if (size_changed && gtk_widget_get_realized (widget))
3666 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
3670 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3673 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3674 text_view->priv->first_para_mark);
3678 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3681 GtkTextViewPrivate *priv;
3683 widget = GTK_WIDGET (text_view);
3684 priv = text_view->priv;
3686 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3688 if (SCREEN_HEIGHT (widget) > 0)
3690 GtkTextIter first_para;
3692 /* Be sure we've validated the stuff onscreen; if we
3693 * scrolled, these calls won't have any effect, because
3694 * they were called in the recursive validate_onscreen
3696 gtk_text_view_get_first_para_iter (text_view, &first_para);
3698 gtk_text_layout_validate_yrange (priv->layout,
3701 priv->first_para_pixels +
3702 SCREEN_HEIGHT (widget));
3705 priv->onscreen_validated = TRUE;
3707 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3709 /* This can have the odd side effect of triggering a scroll, which should
3710 * flip "onscreen_validated" back to FALSE, but should also get us
3711 * back into this function to turn it on again.
3713 gtk_text_view_update_adjustments (text_view);
3715 g_assert (priv->onscreen_validated);
3719 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3721 GtkTextViewPrivate *priv = text_view->priv;
3723 if (priv->first_validate_idle == 0)
3726 /* Do this first, which means that if an "invalidate"
3727 * occurs during any of this process, a new first_validate_callback
3728 * will be installed, and we'll start again.
3730 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3731 g_source_remove (priv->first_validate_idle);
3732 priv->first_validate_idle = 0;
3734 /* be sure we have up-to-date screen size set on the
3737 gtk_text_view_update_layout_width (text_view);
3739 /* Bail out if we invalidated stuff; scrolling right away will just
3740 * confuse the issue.
3742 if (priv->first_validate_idle != 0)
3744 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3748 /* scroll to any marks, if that's pending. This can jump us to
3749 * the validation codepath used for scrolling onscreen, if so we
3750 * bail out. It won't jump if already in that codepath since
3751 * value_changed is not recursive, so also validate if
3754 if (!gtk_text_view_flush_scroll (text_view) ||
3755 !priv->onscreen_validated)
3756 gtk_text_view_validate_onscreen (text_view);
3758 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3760 g_assert (priv->onscreen_validated);
3765 first_validate_callback (gpointer data)
3767 GtkTextView *text_view = data;
3769 /* Note that some of this code is duplicated at the end of size_allocate,
3770 * keep in sync with that.
3773 DV(g_print(G_STRLOC"\n"));
3775 gtk_text_view_flush_first_validate (text_view);
3781 incremental_validate_callback (gpointer data)
3783 GtkTextView *text_view = data;
3784 gboolean result = TRUE;
3786 DV(g_print(G_STRLOC"\n"));
3788 gtk_text_layout_validate (text_view->priv->layout, 2000);
3790 gtk_text_view_update_adjustments (text_view);
3792 if (gtk_text_layout_is_valid (text_view->priv->layout))
3794 text_view->priv->incremental_validate_idle = 0;
3802 gtk_text_view_invalidate (GtkTextView *text_view)
3804 GtkTextViewPrivate *priv = text_view->priv;
3806 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3807 priv->onscreen_validated));
3809 priv->onscreen_validated = FALSE;
3811 /* We'll invalidate when the layout is created */
3812 if (priv->layout == NULL)
3815 if (!priv->first_validate_idle)
3817 priv->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3818 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3819 priv->first_validate_idle));
3822 if (!priv->incremental_validate_idle)
3824 priv->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3825 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3826 priv->incremental_validate_idle));
3831 invalidated_handler (GtkTextLayout *layout,
3834 GtkTextView *text_view;
3836 text_view = GTK_TEXT_VIEW (data);
3838 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3839 gtk_text_view_invalidate (text_view);
3843 changed_handler (GtkTextLayout *layout,
3849 GtkTextView *text_view;
3850 GtkTextViewPrivate *priv;
3852 GdkRectangle visible_rect;
3853 GdkRectangle redraw_rect;
3855 text_view = GTK_TEXT_VIEW (data);
3856 priv = text_view->priv;
3857 widget = GTK_WIDGET (data);
3859 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3861 if (gtk_widget_get_realized (widget))
3863 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3865 redraw_rect.x = visible_rect.x;
3866 redraw_rect.width = visible_rect.width;
3867 redraw_rect.y = start_y;
3869 if (old_height == new_height)
3870 redraw_rect.height = old_height;
3871 else if (start_y + old_height > visible_rect.y)
3872 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3874 redraw_rect.height = 0;
3876 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3878 /* text_window_invalidate_rect() takes buffer coordinates */
3879 text_window_invalidate_rect (priv->text_window,
3882 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3886 redraw_rect.height));
3888 if (priv->left_window)
3889 text_window_invalidate_rect (priv->left_window,
3891 if (priv->right_window)
3892 text_window_invalidate_rect (priv->right_window,
3894 if (priv->top_window)
3895 text_window_invalidate_rect (priv->top_window,
3897 if (priv->bottom_window)
3898 text_window_invalidate_rect (priv->bottom_window,
3901 queue_update_im_spot_location (text_view);
3905 if (old_height != new_height)
3907 gboolean yoffset_changed = FALSE;
3909 int new_first_para_top;
3910 int old_first_para_top;
3913 /* If the bottom of the old area was above the top of the
3914 * screen, we need to scroll to keep the current top of the
3915 * screen in place. Remember that first_para_pixels is the
3916 * position of the top of the screen in coordinates relative to
3917 * the first paragraph onscreen.
3919 * In short we are adding the height delta of the portion of the
3920 * changed region above first_para_mark to priv->yoffset.
3922 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3923 priv->first_para_mark);
3925 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3927 old_first_para_top = priv->yoffset - priv->first_para_pixels;
3929 if (new_first_para_top != old_first_para_top)
3931 priv->yoffset += new_first_para_top - old_first_para_top;
3933 get_vadjustment (text_view)->value = priv->yoffset;
3934 yoffset_changed = TRUE;
3937 if (yoffset_changed)
3939 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3940 gtk_adjustment_value_changed (get_vadjustment (text_view));
3943 /* FIXME be smarter about which anchored widgets we update */
3945 tmp_list = priv->children;
3946 while (tmp_list != NULL)
3948 GtkTextViewChild *child = tmp_list->data;
3951 gtk_text_view_update_child_allocation (text_view, child);
3953 tmp_list = g_slist_next (tmp_list);
3958 GtkRequisition old_req = priv->cached_size_request;
3959 GtkRequisition new_req;
3961 /* Use this instead of gtk_widget_size_request wrapper
3962 * to avoid the optimization which just returns widget->requisition
3963 * if a resize hasn't been queued.
3965 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3967 if (old_req.width != new_req.width ||
3968 old_req.height != new_req.height)
3970 gtk_widget_queue_resize_no_redraw (widget);
3976 gtk_text_view_realize (GtkWidget *widget)
3978 GtkAllocation allocation;
3979 GtkTextView *text_view;
3980 GtkTextViewPrivate *priv;
3982 GdkWindowAttr attributes;
3983 gint attributes_mask;
3986 text_view = GTK_TEXT_VIEW (widget);
3987 priv = text_view->priv;
3989 gtk_widget_set_realized (widget, TRUE);
3991 gtk_widget_get_allocation (widget, &allocation);
3993 attributes.window_type = GDK_WINDOW_CHILD;
3994 attributes.x = allocation.x;
3995 attributes.y = allocation.y;
3996 attributes.width = allocation.width;
3997 attributes.height = allocation.height;
3998 attributes.wclass = GDK_INPUT_OUTPUT;
3999 attributes.visual = gtk_widget_get_visual (widget);
4000 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
4002 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
4004 window = gdk_window_new (gtk_widget_get_parent_window (widget),
4005 &attributes, attributes_mask);
4006 gtk_widget_set_window (widget, window);
4007 gdk_window_set_user_data (window, widget);
4009 /* must come before text_window_realize calls */
4010 gtk_widget_style_attach (widget);
4012 gdk_window_set_background (window,
4013 >k_widget_get_style (widget)->bg[gtk_widget_get_state (widget)]);
4015 text_window_realize (priv->text_window, widget);
4017 if (priv->left_window)
4018 text_window_realize (priv->left_window, widget);
4020 if (priv->top_window)
4021 text_window_realize (priv->top_window, widget);
4023 if (priv->right_window)
4024 text_window_realize (priv->right_window, widget);
4026 if (priv->bottom_window)
4027 text_window_realize (priv->bottom_window, widget);
4029 gtk_text_view_ensure_layout (text_view);
4033 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4034 GDK_SELECTION_PRIMARY);
4035 gtk_text_buffer_add_selection_clipboard (priv->buffer, clipboard);
4038 tmp_list = priv->children;
4039 while (tmp_list != NULL)
4041 GtkTextViewChild *vc = tmp_list->data;
4043 text_view_child_set_parent_window (text_view, vc);
4045 tmp_list = tmp_list->next;
4048 /* Ensure updating the spot location. */
4049 gtk_text_view_update_im_spot_location (text_view);
4053 gtk_text_view_unrealize (GtkWidget *widget)
4055 GtkTextView *text_view;
4056 GtkTextViewPrivate *priv;
4058 text_view = GTK_TEXT_VIEW (widget);
4059 priv = text_view->priv;
4063 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4064 GDK_SELECTION_PRIMARY);
4065 gtk_text_buffer_remove_selection_clipboard (priv->buffer, clipboard);
4068 gtk_text_view_remove_validate_idles (text_view);
4070 if (priv->popup_menu)
4072 gtk_widget_destroy (priv->popup_menu);
4073 priv->popup_menu = NULL;
4076 text_window_unrealize (priv->text_window);
4078 if (priv->left_window)
4079 text_window_unrealize (priv->left_window);
4081 if (priv->top_window)
4082 text_window_unrealize (priv->top_window);
4084 if (priv->right_window)
4085 text_window_unrealize (priv->right_window);
4087 if (priv->bottom_window)
4088 text_window_unrealize (priv->bottom_window);
4090 gtk_text_view_destroy_layout (text_view);
4092 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize (widget);
4096 gtk_text_view_set_background (GtkTextView *text_view)
4101 GtkTextViewPrivate *priv;
4103 widget = GTK_WIDGET (text_view);
4104 priv = text_view->priv;
4106 style = gtk_widget_get_style (widget);
4107 state = gtk_widget_get_state (widget);
4109 gdk_window_set_background (gtk_widget_get_window (widget),
4112 gdk_window_set_background (priv->text_window->bin_window,
4113 &style->base[state]);
4115 if (priv->left_window)
4116 gdk_window_set_background (priv->left_window->bin_window,
4118 if (priv->right_window)
4119 gdk_window_set_background (priv->right_window->bin_window,
4122 if (priv->top_window)
4123 gdk_window_set_background (priv->top_window->bin_window,
4126 if (priv->bottom_window)
4127 gdk_window_set_background (priv->bottom_window->bin_window,
4132 gtk_text_view_style_set (GtkWidget *widget,
4133 GtkStyle *previous_style)
4135 GtkTextView *text_view;
4136 GtkTextViewPrivate *priv;
4137 PangoContext *ltr_context, *rtl_context;
4139 text_view = GTK_TEXT_VIEW (widget);
4140 priv = text_view->priv;
4142 if (gtk_widget_get_realized (widget))
4144 gtk_text_view_set_background (text_view);
4147 if (priv->layout && previous_style)
4149 gtk_text_view_set_attributes_from_style (text_view,
4150 priv->layout->default_style,
4151 gtk_widget_get_style (widget));
4153 ltr_context = gtk_widget_create_pango_context (widget);
4154 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
4155 rtl_context = gtk_widget_create_pango_context (widget);
4156 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
4158 gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
4160 g_object_unref (ltr_context);
4161 g_object_unref (rtl_context);
4166 gtk_text_view_direction_changed (GtkWidget *widget,
4167 GtkTextDirection previous_direction)
4169 GtkTextViewPrivate *priv = GTK_TEXT_VIEW (widget)->priv;
4173 priv->layout->default_style->direction = gtk_widget_get_direction (widget);
4175 gtk_text_layout_default_style_changed (priv->layout);
4180 gtk_text_view_state_changed (GtkWidget *widget,
4181 GtkStateType previous_state)
4183 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4186 if (gtk_widget_get_realized (widget))
4188 gtk_text_view_set_background (text_view);
4190 if (gtk_widget_is_sensitive (widget))
4191 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4195 gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
4198 gdk_cursor_unref (cursor);
4200 text_view->priv->mouse_cursor_obscured = FALSE;
4203 if (!gtk_widget_is_sensitive (widget))
4205 /* Clear any selection */
4206 gtk_text_view_unselect (text_view);
4209 gtk_widget_queue_draw (widget);
4213 set_invisible_cursor (GdkWindow *window)
4215 GdkDisplay *display;
4218 display = gdk_window_get_display (window);
4219 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4221 gdk_window_set_cursor (window, cursor);
4223 gdk_cursor_unref (cursor);
4227 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
4229 if (text_view->priv->mouse_cursor_obscured)
4232 set_invisible_cursor (text_view->priv->text_window->bin_window);
4234 text_view->priv->mouse_cursor_obscured = TRUE;
4238 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
4240 if (text_view->priv->mouse_cursor_obscured)
4244 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
4246 gdk_window_set_cursor (text_view->priv->text_window->bin_window, cursor);
4247 gdk_cursor_unref (cursor);
4248 text_view->priv->mouse_cursor_obscured = FALSE;
4253 gtk_text_view_grab_notify (GtkWidget *widget,
4254 gboolean was_grabbed)
4256 GtkTextViewPrivate *priv;
4258 priv = GTK_TEXT_VIEW (widget)->priv;
4260 if (priv->grab_device &&
4261 gtk_widget_device_is_shadowed (widget, priv->grab_device))
4263 gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
4264 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
4274 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
4277 switch (event->type)
4279 case GDK_MOTION_NOTIFY:
4280 *x = event->motion.x;
4281 *y = event->motion.y;
4285 case GDK_BUTTON_PRESS:
4286 case GDK_2BUTTON_PRESS:
4287 case GDK_3BUTTON_PRESS:
4288 case GDK_BUTTON_RELEASE:
4289 *x = event->button.x;
4290 *y = event->button.y;
4295 case GDK_KEY_RELEASE:
4296 case GDK_ENTER_NOTIFY:
4297 case GDK_LEAVE_NOTIFY:
4298 case GDK_PROPERTY_NOTIFY:
4299 case GDK_SELECTION_CLEAR:
4300 case GDK_SELECTION_REQUEST:
4301 case GDK_SELECTION_NOTIFY:
4302 case GDK_PROXIMITY_IN:
4303 case GDK_PROXIMITY_OUT:
4304 case GDK_DRAG_ENTER:
4305 case GDK_DRAG_LEAVE:
4306 case GDK_DRAG_MOTION:
4307 case GDK_DRAG_STATUS:
4308 case GDK_DROP_START:
4309 case GDK_DROP_FINISHED:
4319 emit_event_on_tags (GtkWidget *widget,
4325 gboolean retval = FALSE;
4327 tags = gtk_text_iter_get_tags (iter);
4332 GtkTextTag *tag = tmp->data;
4334 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
4340 tmp = g_slist_next (tmp);
4343 g_slist_free (tags);
4349 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
4351 GtkTextView *text_view;
4352 GtkTextViewPrivate *priv;
4355 text_view = GTK_TEXT_VIEW (widget);
4356 priv = text_view->priv;
4358 if (priv->layout == NULL ||
4359 get_buffer (text_view) == NULL)
4362 if (event->any.window != priv->text_window->bin_window)
4365 if (get_event_coordinates (event, &x, &y))
4372 /* FIXME this is slow and we do it twice per event.
4373 * My favorite solution is to have GtkTextLayout cache
4374 * the last couple lookups.
4376 gtk_text_layout_get_iter_at_pixel (priv->layout,
4380 return emit_event_on_tags (widget, event, &iter);
4382 else if (event->type == GDK_KEY_PRESS ||
4383 event->type == GDK_KEY_RELEASE)
4387 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
4388 gtk_text_buffer_get_insert (get_buffer (text_view)));
4390 return emit_event_on_tags (widget, event, &iter);
4397 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
4399 GtkTextView *text_view;
4400 GtkTextViewPrivate *priv;
4401 GtkTextMark *insert;
4403 gboolean can_insert;
4404 gboolean retval = FALSE;
4405 gboolean obscure = FALSE;
4407 text_view = GTK_TEXT_VIEW (widget);
4408 priv = text_view->priv;
4410 if (priv->layout == NULL ||
4411 get_buffer (text_view) == NULL)
4414 /* Make sure input method knows where it is */
4415 flush_update_im_spot_location (text_view);
4417 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4418 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4419 can_insert = gtk_text_iter_can_insert (&iter, priv->editable);
4420 if (gtk_im_context_filter_keypress (priv->im_context, event))
4422 priv->need_im_reset = TRUE;
4424 gtk_text_view_reset_im_context (text_view);
4425 obscure = can_insert;
4429 else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4433 /* use overall editability not can_insert, more predictable for users */
4434 else if (priv->editable &&
4435 (event->keyval == GDK_KEY_Return ||
4436 event->keyval == GDK_KEY_ISO_Enter ||
4437 event->keyval == GDK_KEY_KP_Enter))
4439 /* this won't actually insert the newline if the cursor isn't
4442 gtk_text_view_reset_im_context (text_view);
4443 gtk_text_view_commit_text (text_view, "\n");
4448 /* Pass through Tab as literal tab, unless Control is held down */
4449 else if ((event->keyval == GDK_KEY_Tab ||
4450 event->keyval == GDK_KEY_KP_Tab ||
4451 event->keyval == GDK_KEY_ISO_Left_Tab) &&
4452 !(event->state & GDK_CONTROL_MASK))
4454 /* If the text widget isn't editable overall, or if the application
4455 * has turned off "accepts_tab", move the focus instead
4457 if (priv->accepts_tab && priv->editable)
4459 gtk_text_view_reset_im_context (text_view);
4460 gtk_text_view_commit_text (text_view, "\t");
4464 g_signal_emit_by_name (text_view, "move-focus",
4465 (event->state & GDK_SHIFT_MASK) ?
4466 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4474 gtk_text_view_obscure_mouse_cursor (text_view);
4476 gtk_text_view_reset_blink_time (text_view);
4477 gtk_text_view_pend_cursor_blink (text_view);
4483 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4485 GtkTextView *text_view;
4486 GtkTextViewPrivate *priv;
4487 GtkTextMark *insert;
4490 text_view = GTK_TEXT_VIEW (widget);
4491 priv = text_view->priv;
4493 if (priv->layout == NULL || get_buffer (text_view) == NULL)
4496 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4497 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4498 if (gtk_text_iter_can_insert (&iter, priv->editable) &&
4499 gtk_im_context_filter_keypress (priv->im_context, event))
4501 priv->need_im_reset = TRUE;
4505 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4509 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4511 GtkTextView *text_view;
4512 GtkTextViewPrivate *priv;
4514 text_view = GTK_TEXT_VIEW (widget);
4515 priv = text_view->priv;
4517 gtk_widget_grab_focus (widget);
4519 if (event->window != priv->text_window->bin_window)
4521 /* Remove selection if any. */
4522 gtk_text_view_unselect (text_view);
4526 gtk_text_view_reset_blink_time (text_view);
4530 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4531 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4532 else if (event->button == 3)
4533 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4536 if (event->type == GDK_BUTTON_PRESS)
4538 gtk_text_view_reset_im_context (text_view);
4540 if (event->button == 1)
4542 /* If we're in the selection, start a drag copy/move of the
4543 * selection; otherwise, start creating a new selection.
4546 GtkTextIter start, end;
4548 gtk_text_layout_get_iter_at_pixel (priv->layout,
4550 event->x + priv->xoffset,
4551 event->y + priv->yoffset);
4553 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4555 gtk_text_iter_in_range (&iter, &start, &end) &&
4556 !(event->state & GDK_SHIFT_MASK))
4558 priv->drag_start_x = event->x;
4559 priv->drag_start_y = event->y;
4560 priv->pending_place_cursor_button = event->button;
4564 gtk_text_view_start_selection_drag (text_view, &iter, event);
4569 else if (event->button == 2)
4573 /* We do not want to scroll back to the insert iter when we paste
4574 with the middle button */
4575 priv->scroll_after_paste = FALSE;
4577 gtk_text_layout_get_iter_at_pixel (priv->layout,
4579 event->x + priv->xoffset,
4580 event->y + priv->yoffset);
4582 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4583 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4588 else if (event->button == 3)
4590 gtk_text_view_do_popup (text_view, event);
4594 else if ((event->type == GDK_2BUTTON_PRESS ||
4595 event->type == GDK_3BUTTON_PRESS) &&
4600 gtk_text_view_end_selection_drag (text_view);
4602 gtk_text_layout_get_iter_at_pixel (priv->layout,
4604 event->x + priv->xoffset,
4605 event->y + priv->yoffset);
4607 gtk_text_view_start_selection_drag (text_view, &iter, event);
4615 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4617 GtkTextView *text_view;
4618 GtkTextViewPrivate *priv;
4620 text_view = GTK_TEXT_VIEW (widget);
4621 priv = text_view->priv;
4623 if (event->window != priv->text_window->bin_window)
4626 if (event->button == 1)
4628 if (priv->drag_start_x >= 0)
4630 priv->drag_start_x = -1;
4631 priv->drag_start_y = -1;
4634 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4636 else if (priv->pending_place_cursor_button == event->button)
4640 /* Unselect everything; we clicked inside selection, but
4641 * didn't move by the drag threshold, so just clear selection
4644 gtk_text_layout_get_iter_at_pixel (priv->layout,
4646 event->x + priv->xoffset,
4647 event->y + priv->yoffset);
4649 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4650 gtk_text_view_check_cursor_blink (text_view);
4652 priv->pending_place_cursor_button = 0;
4662 keymap_direction_changed (GdkKeymap *keymap,
4663 GtkTextView *text_view)
4665 gtk_text_view_check_keymap_direction (text_view);
4669 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4671 GtkTextView *text_view;
4672 GtkTextViewPrivate *priv;
4674 text_view = GTK_TEXT_VIEW (widget);
4675 priv = text_view->priv;
4677 gtk_widget_queue_draw (widget);
4679 DV(g_print (G_STRLOC": focus_in_event\n"));
4681 gtk_text_view_reset_blink_time (text_view);
4683 if (priv->cursor_visible && priv->layout)
4685 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
4686 gtk_text_view_check_cursor_blink (text_view);
4689 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4690 "direction-changed",
4691 G_CALLBACK (keymap_direction_changed), text_view);
4692 gtk_text_view_check_keymap_direction (text_view);
4696 priv->need_im_reset = TRUE;
4697 gtk_im_context_focus_in (priv->im_context);
4704 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4706 GtkTextView *text_view;
4707 GtkTextViewPrivate *priv;
4709 text_view = GTK_TEXT_VIEW (widget);
4710 priv = text_view->priv;
4712 gtk_text_view_end_selection_drag (text_view);
4714 gtk_widget_queue_draw (widget);
4716 DV(g_print (G_STRLOC": focus_out_event\n"));
4718 if (priv->cursor_visible && priv->layout)
4720 gtk_text_view_check_cursor_blink (text_view);
4721 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
4724 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4725 keymap_direction_changed,
4730 priv->need_im_reset = TRUE;
4731 gtk_im_context_focus_out (priv->im_context);
4738 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4740 GtkTextView *text_view;
4741 GtkTextViewPrivate *priv;
4743 text_view = GTK_TEXT_VIEW (widget);
4744 priv = text_view->priv;
4746 gtk_text_view_unobscure_mouse_cursor (text_view);
4748 if (event->window == priv->text_window->bin_window &&
4749 priv->drag_start_x >= 0)
4754 gdk_event_request_motions (event);
4756 if (gtk_drag_check_threshold (widget,
4762 gint buffer_x, buffer_y;
4764 gtk_text_view_window_to_buffer_coords (text_view,
4765 GTK_TEXT_WINDOW_TEXT,
4771 gtk_text_layout_get_iter_at_pixel (priv->layout,
4773 buffer_x, buffer_y);
4775 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4784 gtk_text_view_paint (GtkWidget *widget,
4787 GtkTextView *text_view;
4788 GtkTextViewPrivate *priv;
4789 GList *child_exposes;
4792 text_view = GTK_TEXT_VIEW (widget);
4793 priv = text_view->priv;
4795 g_return_if_fail (priv->layout != NULL);
4796 g_return_if_fail (priv->xoffset >= 0);
4797 g_return_if_fail (priv->yoffset >= 0);
4799 while (priv->first_validate_idle != 0)
4801 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4802 priv->first_validate_idle));
4803 gtk_text_view_flush_first_validate (text_view);
4806 if (!priv->onscreen_validated)
4808 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.");
4809 g_assert_not_reached ();
4813 printf ("painting %d,%d %d x %d\n",
4815 area->width, area->height);
4818 child_exposes = NULL;
4821 cairo_translate (cr, -priv->xoffset, -priv->yoffset);
4823 gtk_text_layout_draw (priv->layout,
4830 tmp_list = child_exposes;
4831 while (tmp_list != NULL)
4833 GtkWidget *child = tmp_list->data;
4835 gtk_container_propagate_draw (GTK_CONTAINER (text_view),
4839 g_object_unref (child);
4841 tmp_list = tmp_list->next;
4844 g_list_free (child_exposes);
4848 gtk_text_view_draw (GtkWidget *widget,
4854 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
4855 gtk_text_view_draw_focus (widget, cr);
4857 window = gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4858 GTK_TEXT_WINDOW_TEXT);
4859 if (gtk_cairo_should_draw_window (cr, window))
4863 DV(g_print (">Exposed ("G_STRLOC")\n"));
4865 gdk_window_get_position (window, &x, &y);
4866 cairo_translate (cr, x, y);
4867 gtk_text_view_paint (widget, cr);
4871 /* Propagate exposes to all unanchored children.
4872 * Anchored children are handled in gtk_text_view_paint().
4874 tmp_list = GTK_TEXT_VIEW (widget)->priv->children;
4875 while (tmp_list != NULL)
4877 GtkTextViewChild *vc = tmp_list->data;
4879 /* propagate_expose checks that event->window matches
4883 gtk_container_propagate_draw (GTK_CONTAINER (widget),
4887 tmp_list = tmp_list->next;
4894 gtk_text_view_draw_focus (GtkWidget *widget,
4897 gboolean interior_focus;
4899 /* We clear the focus if we are in interior focus mode. */
4900 gtk_widget_style_get (widget,
4901 "interior-focus", &interior_focus,
4904 if (gtk_widget_has_focus (widget) && !interior_focus)
4906 gtk_paint_focus (gtk_widget_get_style (widget), cr,
4907 gtk_widget_get_state (widget),
4910 gtk_widget_get_allocated_width (widget),
4911 gtk_widget_get_allocated_height (widget));
4916 gtk_text_view_focus (GtkWidget *widget,
4917 GtkDirectionType direction)
4919 GtkContainer *container;
4922 container = GTK_CONTAINER (widget);
4924 if (!gtk_widget_is_focus (widget) &&
4925 gtk_container_get_focus_child (container) == NULL)
4927 gtk_widget_grab_focus (widget);
4933 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4934 * children to get the focus
4936 gtk_widget_set_can_focus (widget, FALSE);
4937 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4938 gtk_widget_set_can_focus (widget, TRUE);
4945 gtk_text_view_move_focus (GtkWidget *widget,
4946 GtkDirectionType direction_type)
4948 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4950 if (GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus)
4951 GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus (text_view,
4960 gtk_text_view_add (GtkContainer *container,
4963 /* This is pretty random. */
4964 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4966 GTK_TEXT_WINDOW_WIDGET,
4971 gtk_text_view_remove (GtkContainer *container,
4974 GtkTextView *text_view;
4975 GtkTextViewPrivate *priv;
4976 GtkTextViewChild *vc;
4979 text_view = GTK_TEXT_VIEW (container);
4980 priv = text_view->priv;
4983 iter = priv->children;
4985 while (iter != NULL)
4989 if (vc->widget == child)
4992 iter = g_slist_next (iter);
4995 g_assert (iter != NULL); /* be sure we had the child in the list */
4997 priv->children = g_slist_remove (priv->children, vc);
4999 gtk_widget_unparent (vc->widget);
5001 text_view_child_free (vc);
5005 gtk_text_view_forall (GtkContainer *container,
5006 gboolean include_internals,
5007 GtkCallback callback,
5008 gpointer callback_data)
5011 GtkTextView *text_view;
5014 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
5015 g_return_if_fail (callback != NULL);
5017 text_view = GTK_TEXT_VIEW (container);
5019 copy = g_slist_copy (text_view->priv->children);
5022 while (iter != NULL)
5024 GtkTextViewChild *vc = iter->data;
5026 (* callback) (vc->widget, callback_data);
5028 iter = g_slist_next (iter);
5031 g_slist_free (copy);
5034 #define CURSOR_ON_MULTIPLIER 2
5035 #define CURSOR_OFF_MULTIPLIER 1
5036 #define CURSOR_PEND_MULTIPLIER 3
5037 #define CURSOR_DIVIDER 3
5040 cursor_blinks (GtkTextView *text_view)
5042 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5045 #ifdef DEBUG_VALIDATION_AND_SCROLLING
5048 if (gtk_get_debug_flags () & GTK_DEBUG_UPDATES)
5051 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
5056 if (text_view->priv->editable)
5058 GtkTextMark *insert;
5061 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
5062 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
5064 if (gtk_text_iter_editable (&iter, text_view->priv->editable))
5072 get_cursor_time (GtkTextView *text_view)
5074 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5077 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
5083 get_cursor_blink_timeout (GtkTextView *text_view)
5085 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5088 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
5099 blink_cb (gpointer data)
5101 GtkTextView *text_view;
5102 GtkTextViewPrivate *priv;
5106 text_view = GTK_TEXT_VIEW (data);
5107 priv = text_view->priv;
5109 if (!gtk_widget_has_focus (GTK_WIDGET (text_view)))
5111 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
5112 "connect a handler to this signal, it must return\n"
5113 "FALSE so the text view gets the event as well");
5115 gtk_text_view_check_cursor_blink (text_view);
5120 g_assert (priv->layout);
5121 g_assert (priv->cursor_visible);
5123 visible = gtk_text_layout_get_cursor_visible (priv->layout);
5125 blink_timeout = get_cursor_blink_timeout (text_view);
5126 if (priv->blink_time > 1000 * blink_timeout &&
5127 blink_timeout < G_MAXINT/1000)
5129 /* we've blinked enough without the user doing anything, stop blinking */
5131 priv->blink_timeout = 0;
5134 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5139 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
5142 priv->blink_time += get_cursor_time (text_view);
5145 /* Block changed_handler while changing the layout's cursor visibility
5146 * because it would expose the whole paragraph. Instead, we expose
5147 * the cursor's area(s) manually below.
5149 g_signal_handlers_block_by_func (priv->layout,
5152 gtk_text_layout_set_cursor_visible (priv->layout, !visible);
5153 g_signal_handlers_unblock_by_func (priv->layout,
5157 text_window_invalidate_cursors (priv->text_window);
5159 /* Remove ourselves */
5165 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
5167 if (text_view->priv->blink_timeout)
5169 g_source_remove (text_view->priv->blink_timeout);
5170 text_view->priv->blink_timeout = 0;
5175 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
5177 GtkTextViewPrivate *priv = text_view->priv;
5179 if (priv->layout != NULL &&
5180 priv->cursor_visible &&
5181 gtk_widget_has_focus (GTK_WIDGET (text_view)))
5183 if (cursor_blinks (text_view))
5185 if (priv->blink_timeout == 0)
5187 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5189 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
5196 gtk_text_view_stop_cursor_blink (text_view);
5197 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5202 gtk_text_view_stop_cursor_blink (text_view);
5203 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
5208 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
5210 GtkTextViewPrivate *priv = text_view->priv;
5212 if (priv->layout != NULL &&
5213 priv->cursor_visible &&
5214 gtk_widget_has_focus (GTK_WIDGET (text_view)) &&
5215 cursor_blinks (text_view))
5217 gtk_text_view_stop_cursor_blink (text_view);
5218 gtk_text_layout_set_cursor_visible (priv->layout, TRUE);
5220 priv->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5227 gtk_text_view_reset_blink_time (GtkTextView *text_view)
5229 GtkTextViewPrivate *priv = text_view->priv;
5231 priv->blink_time = 0;
5236 * Key binding handlers
5240 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
5241 GtkTextIter *newplace,
5244 gboolean ret = TRUE;
5248 ret = gtk_text_layout_move_iter_to_previous_line (text_view->priv->layout, newplace);
5254 ret = gtk_text_layout_move_iter_to_next_line (text_view->priv->layout, newplace);
5262 move_cursor (GtkTextView *text_view,
5263 const GtkTextIter *new_location,
5264 gboolean extend_selection)
5266 if (extend_selection)
5267 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
5271 gtk_text_buffer_place_cursor (get_buffer (text_view),
5273 gtk_text_view_check_cursor_blink (text_view);
5277 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
5278 GtkMovementStep step,
5280 gboolean extend_selection)
5282 GtkTextViewPrivate *priv;
5284 GtkTextIter newplace;
5285 gboolean cancel_selection = FALSE;
5286 gint cursor_x_pos = 0;
5287 GtkDirectionType leave_direction = -1;
5289 priv = text_view->priv;
5291 if (!priv->cursor_visible)
5293 GtkScrollStep scroll_step;
5297 case GTK_MOVEMENT_VISUAL_POSITIONS:
5298 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
5300 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5301 case GTK_MOVEMENT_WORDS:
5302 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
5304 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5305 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
5307 case GTK_MOVEMENT_DISPLAY_LINES:
5308 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
5310 case GTK_MOVEMENT_PARAGRAPHS:
5311 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5312 scroll_step = GTK_SCROLL_STEPS;
5314 case GTK_MOVEMENT_PAGES:
5315 scroll_step = GTK_SCROLL_PAGES;
5317 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5318 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
5320 case GTK_MOVEMENT_BUFFER_ENDS:
5321 scroll_step = GTK_SCROLL_ENDS;
5324 scroll_step = GTK_SCROLL_PAGES;
5328 if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
5330 if (leave_direction != -1 &&
5331 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5334 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5341 gtk_text_view_reset_im_context (text_view);
5343 if (step == GTK_MOVEMENT_PAGES)
5345 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
5346 gtk_widget_error_bell (GTK_WIDGET (text_view));
5348 gtk_text_view_check_cursor_blink (text_view);
5349 gtk_text_view_pend_cursor_blink (text_view);
5352 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
5354 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
5355 gtk_widget_error_bell (GTK_WIDGET (text_view));
5357 gtk_text_view_check_cursor_blink (text_view);
5358 gtk_text_view_pend_cursor_blink (text_view);
5362 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5363 gtk_text_buffer_get_insert (get_buffer (text_view)));
5365 if (! extend_selection)
5367 GtkTextIter sel_bound;
5369 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &sel_bound,
5370 gtk_text_buffer_get_selection_bound (get_buffer (text_view)));
5372 /* if we move forward, assume the cursor is at the end of the selection;
5373 * if we move backward, assume the cursor is at the start
5376 gtk_text_iter_order (&sel_bound, &insert);
5378 gtk_text_iter_order (&insert, &sel_bound);
5380 /* if we actually have a selection, just move *to* the beginning/end
5381 * of the selection and not *from* there on LOGICAL_POSITIONS
5382 * and VISUAL_POSITIONS movement
5384 if (! gtk_text_iter_equal (&sel_bound, &insert))
5385 cancel_selection = TRUE;
5390 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5391 gtk_text_view_get_virtual_cursor_pos (text_view, &insert, &cursor_x_pos, NULL);
5395 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5396 if (! cancel_selection)
5397 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5400 case GTK_MOVEMENT_VISUAL_POSITIONS:
5401 if (! cancel_selection)
5402 gtk_text_layout_move_iter_visually (priv->layout,
5406 case GTK_MOVEMENT_WORDS:
5408 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5411 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5412 gtk_text_iter_forward_to_line_end (&newplace);
5416 case GTK_MOVEMENT_DISPLAY_LINES:
5419 leave_direction = GTK_DIR_UP;
5421 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5422 gtk_text_layout_move_iter_to_x (priv->layout, &newplace, cursor_x_pos);
5424 gtk_text_iter_set_line_offset (&newplace, 0);
5428 leave_direction = GTK_DIR_DOWN;
5430 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5431 gtk_text_layout_move_iter_to_x (priv->layout, &newplace, cursor_x_pos);
5433 gtk_text_iter_forward_to_line_end (&newplace);
5437 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5439 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5440 else if (count < -1)
5441 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5444 gtk_text_layout_move_iter_to_line_end (priv->layout, &newplace, count);
5447 case GTK_MOVEMENT_PARAGRAPHS:
5450 if (!gtk_text_iter_ends_line (&newplace))
5452 gtk_text_iter_forward_to_line_end (&newplace);
5455 gtk_text_iter_forward_visible_lines (&newplace, count);
5456 gtk_text_iter_forward_to_line_end (&newplace);
5460 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5461 gtk_text_iter_set_line_offset (&newplace, 0);
5462 gtk_text_iter_forward_visible_lines (&newplace, count);
5463 gtk_text_iter_set_line_offset (&newplace, 0);
5467 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5470 if (!gtk_text_iter_ends_line (&newplace))
5471 gtk_text_iter_forward_to_line_end (&newplace);
5475 gtk_text_iter_set_line_offset (&newplace, 0);
5479 case GTK_MOVEMENT_BUFFER_ENDS:
5481 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5483 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5490 /* call move_cursor() even if the cursor hasn't moved, since it
5491 cancels the selection
5493 move_cursor (text_view, &newplace, extend_selection);
5495 if (!gtk_text_iter_equal (&insert, &newplace))
5497 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5498 gtk_text_view_scroll_mark_onscreen (text_view,
5499 gtk_text_buffer_get_insert (get_buffer (text_view)));
5501 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5502 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5504 else if (leave_direction != -1)
5506 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5509 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5512 else if (! cancel_selection)
5514 gtk_widget_error_bell (GTK_WIDGET (text_view));
5517 gtk_text_view_check_cursor_blink (text_view);
5518 gtk_text_view_pend_cursor_blink (text_view);
5522 gtk_text_view_move_cursor (GtkTextView *text_view,
5523 GtkMovementStep step,
5525 gboolean extend_selection)
5527 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5531 gtk_text_view_move_viewport (GtkTextView *text_view,
5535 GtkAdjustment *adjustment;
5540 case GTK_SCROLL_STEPS:
5541 case GTK_SCROLL_PAGES:
5542 case GTK_SCROLL_ENDS:
5543 adjustment = get_vadjustment (text_view);
5545 case GTK_SCROLL_HORIZONTAL_STEPS:
5546 case GTK_SCROLL_HORIZONTAL_PAGES:
5547 case GTK_SCROLL_HORIZONTAL_ENDS:
5548 adjustment = get_hadjustment (text_view);
5551 adjustment = get_vadjustment (text_view);
5557 case GTK_SCROLL_STEPS:
5558 case GTK_SCROLL_HORIZONTAL_STEPS:
5559 increment = adjustment->step_increment;
5561 case GTK_SCROLL_PAGES:
5562 case GTK_SCROLL_HORIZONTAL_PAGES:
5563 increment = adjustment->page_increment;
5565 case GTK_SCROLL_ENDS:
5566 case GTK_SCROLL_HORIZONTAL_ENDS:
5567 increment = adjustment->upper - adjustment->lower;
5574 return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
5578 gtk_text_view_set_anchor (GtkTextView *text_view)
5582 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5583 gtk_text_buffer_get_insert (get_buffer (text_view)));
5585 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5589 gtk_text_view_scroll_pages (GtkTextView *text_view,
5591 gboolean extend_selection)
5593 GtkTextViewPrivate *priv;
5595 gint cursor_x_pos, cursor_y_pos;
5596 GtkTextMark *insert_mark;
5597 GtkTextIter old_insert;
5598 GtkTextIter new_insert;
5604 priv = text_view->priv;
5606 g_return_val_if_fail (priv->vadjustment != NULL, FALSE);
5608 adj = priv->vadjustment;
5610 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5612 /* Make sure we start from the current cursor position, even
5613 * if it was offscreen, but don't queue more scrolls if we're
5616 if (priv->pending_scroll)
5617 cancel_pending_scroll (text_view);
5619 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5621 /* Validate the region that will be brought into view by the cursor motion
5623 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5624 &old_insert, insert_mark);
5628 gtk_text_view_get_first_para_iter (text_view, &anchor);
5629 y0 = adj->page_size;
5630 y1 = adj->page_size + count * adj->page_increment;
5634 gtk_text_view_get_first_para_iter (text_view, &anchor);
5635 y0 = count * adj->page_increment + adj->page_size;
5639 gtk_text_layout_validate_yrange (priv->layout, &anchor, y0, y1);
5640 /* FIXME do we need to update the adjustment ranges here? */
5642 new_insert = old_insert;
5644 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5646 /* already at top, just be sure we are at offset 0 */
5647 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5648 move_cursor (text_view, &new_insert, extend_selection);
5650 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5652 /* already at bottom, just be sure we are at the end */
5653 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5654 move_cursor (text_view, &new_insert, extend_selection);
5658 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5660 oldval = adj->value;
5661 newval = adj->value;
5663 newval += count * adj->page_increment;
5665 set_adjustment_clamped (adj, newval);
5666 cursor_y_pos += adj->value - oldval;
5668 gtk_text_layout_get_iter_at_pixel (priv->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5669 clamp_iter_onscreen (text_view, &new_insert);
5670 move_cursor (text_view, &new_insert, extend_selection);
5672 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5675 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5676 * only guarantees 1 pixel onscreen.
5678 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5679 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5681 return !gtk_text_iter_equal (&old_insert, &new_insert);
5685 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5687 gboolean extend_selection)
5689 GtkTextViewPrivate *priv;
5691 gint cursor_x_pos, cursor_y_pos;
5692 GtkTextMark *insert_mark;
5693 GtkTextIter old_insert;
5694 GtkTextIter new_insert;
5699 priv = text_view->priv;
5701 g_return_val_if_fail (priv->hadjustment != NULL, FALSE);
5703 adj = priv->hadjustment;
5705 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5707 /* Make sure we start from the current cursor position, even
5708 * if it was offscreen, but don't queue more scrolls if we're
5711 if (priv->pending_scroll)
5712 cancel_pending_scroll (text_view);
5714 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5716 /* Validate the line that we're moving within.
5718 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5719 &old_insert, insert_mark);
5721 gtk_text_layout_get_line_yrange (priv->layout, &old_insert, &y, &height);
5722 gtk_text_layout_validate_yrange (priv->layout, &old_insert, y, y + height);
5723 /* FIXME do we need to update the adjustment ranges here? */
5725 new_insert = old_insert;
5727 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5729 /* already at far left, just be sure we are at offset 0 */
5730 gtk_text_iter_set_line_offset (&new_insert, 0);
5731 move_cursor (text_view, &new_insert, extend_selection);
5733 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5735 /* already at far right, just be sure we are at the end */
5736 if (!gtk_text_iter_ends_line (&new_insert))
5737 gtk_text_iter_forward_to_line_end (&new_insert);
5738 move_cursor (text_view, &new_insert, extend_selection);
5742 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5744 oldval = adj->value;
5745 newval = adj->value;
5747 newval += count * adj->page_increment;
5749 set_adjustment_clamped (adj, newval);
5750 cursor_x_pos += adj->value - oldval;
5752 gtk_text_layout_get_iter_at_pixel (priv->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5753 clamp_iter_onscreen (text_view, &new_insert);
5754 move_cursor (text_view, &new_insert, extend_selection);
5756 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5759 /* FIXME for lines shorter than the overall widget width, this results in a
5760 * "bounce" effect as we scroll to the right of the widget, then scroll
5761 * back to get the end of the line onscreen.
5762 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5765 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5766 * only guarantees 1 pixel onscreen.
5768 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5769 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5771 return !gtk_text_iter_equal (&old_insert, &new_insert);
5775 whitespace (gunichar ch, gpointer user_data)
5777 return (ch == ' ' || ch == '\t');
5781 not_whitespace (gunichar ch, gpointer user_data)
5783 return !whitespace (ch, user_data);
5787 find_whitepace_region (const GtkTextIter *center,
5788 GtkTextIter *start, GtkTextIter *end)
5793 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5794 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5795 if (whitespace (gtk_text_iter_get_char (end), NULL))
5796 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5798 return !gtk_text_iter_equal (start, end);
5802 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5805 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5806 text_view->priv->editable))
5808 gtk_widget_error_bell (GTK_WIDGET (text_view));
5813 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5817 GtkTextViewPrivate *priv;
5821 gboolean leave_one = FALSE;
5823 priv = text_view->priv;
5825 gtk_text_view_reset_im_context (text_view);
5827 if (type == GTK_DELETE_CHARS)
5829 /* Char delete deletes the selection, if one exists */
5830 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5835 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5836 gtk_text_buffer_get_insert (get_buffer (text_view)));
5843 case GTK_DELETE_CHARS:
5844 gtk_text_iter_forward_cursor_positions (&end, count);
5847 case GTK_DELETE_WORD_ENDS:
5849 gtk_text_iter_forward_word_ends (&end, count);
5851 gtk_text_iter_backward_word_starts (&start, 0 - count);
5854 case GTK_DELETE_WORDS:
5857 case GTK_DELETE_DISPLAY_LINE_ENDS:
5860 case GTK_DELETE_DISPLAY_LINES:
5863 case GTK_DELETE_PARAGRAPH_ENDS:
5866 /* If we're already at a newline, we need to
5867 * simply delete that newline, instead of
5868 * moving to the next one.
5870 if (gtk_text_iter_ends_line (&end))
5872 gtk_text_iter_forward_line (&end);
5878 if (!gtk_text_iter_forward_to_line_end (&end))
5886 if (gtk_text_iter_starts_line (&start))
5888 gtk_text_iter_backward_line (&start);
5889 if (!gtk_text_iter_ends_line (&end))
5890 gtk_text_iter_forward_to_line_end (&start);
5894 gtk_text_iter_set_line_offset (&start, 0);
5898 gtk_text_iter_backward_lines (&start, -count);
5902 case GTK_DELETE_PARAGRAPHS:
5905 gtk_text_iter_set_line_offset (&start, 0);
5906 gtk_text_iter_forward_to_line_end (&end);
5908 /* Do the lines beyond the first. */
5911 gtk_text_iter_forward_to_line_end (&end);
5917 /* FIXME negative count? */
5921 case GTK_DELETE_WHITESPACE:
5923 find_whitepace_region (&insert, &start, &end);
5931 if (!gtk_text_iter_equal (&start, &end))
5933 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5935 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5939 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5945 gtk_widget_error_bell (GTK_WIDGET (text_view));
5948 gtk_text_buffer_end_user_action (get_buffer (text_view));
5949 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5951 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5952 gtk_text_view_scroll_mark_onscreen (text_view,
5953 gtk_text_buffer_get_insert (get_buffer (text_view)));
5957 gtk_widget_error_bell (GTK_WIDGET (text_view));
5962 gtk_text_view_backspace (GtkTextView *text_view)
5964 GtkTextViewPrivate *priv;
5967 priv = text_view->priv;
5969 gtk_text_view_reset_im_context (text_view);
5971 /* Backspace deletes the selection, if one exists */
5972 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5976 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5978 gtk_text_buffer_get_insert (get_buffer (text_view)));
5980 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5981 TRUE, priv->editable))
5983 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5984 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5985 gtk_text_view_scroll_mark_onscreen (text_view,
5986 gtk_text_buffer_get_insert (get_buffer (text_view)));
5990 gtk_widget_error_bell (GTK_WIDGET (text_view));
5995 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5997 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5998 GDK_SELECTION_CLIPBOARD);
6000 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
6002 text_view->priv->editable);
6003 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6004 gtk_text_view_scroll_mark_onscreen (text_view,
6005 gtk_text_buffer_get_insert (get_buffer (text_view)));
6009 gtk_text_view_copy_clipboard (GtkTextView *text_view)
6011 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6012 GDK_SELECTION_CLIPBOARD);
6014 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
6017 /* on copy do not scroll, we are already onscreen */
6021 gtk_text_view_paste_clipboard (GtkTextView *text_view)
6023 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6024 GDK_SELECTION_CLIPBOARD);
6026 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
6029 text_view->priv->editable);
6033 gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
6034 GtkClipboard *clipboard,
6037 GtkTextView *text_view = data;
6038 GtkTextViewPrivate *priv;
6040 priv = text_view->priv;
6042 if (priv->scroll_after_paste)
6044 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6045 gtk_text_view_scroll_mark_onscreen (text_view, gtk_text_buffer_get_insert (buffer));
6048 priv->scroll_after_paste = TRUE;
6052 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
6054 GtkTextViewPrivate *priv = text_view->priv;
6056 if (priv->text_window)
6057 text_window_invalidate_cursors (priv->text_window);
6059 priv->overwrite_mode = !priv->overwrite_mode;
6062 gtk_text_layout_set_overwrite_mode (priv->layout,
6063 priv->overwrite_mode && priv->editable);
6065 if (priv->text_window)
6066 text_window_invalidate_cursors (priv->text_window);
6068 gtk_text_view_pend_cursor_blink (text_view);
6070 g_object_notify (G_OBJECT (text_view), "overwrite");
6074 * gtk_text_view_get_overwrite:
6075 * @text_view: a #GtkTextView
6077 * Returns whether the #GtkTextView is in overwrite mode or not.
6079 * Return value: whether @text_view is in overwrite mode or not.
6084 gtk_text_view_get_overwrite (GtkTextView *text_view)
6086 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6088 return text_view->priv->overwrite_mode;
6092 * gtk_text_view_set_overwrite:
6093 * @text_view: a #GtkTextView
6094 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
6096 * Changes the #GtkTextView overwrite mode.
6101 gtk_text_view_set_overwrite (GtkTextView *text_view,
6104 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6105 overwrite = overwrite != FALSE;
6107 if (text_view->priv->overwrite_mode != overwrite)
6108 gtk_text_view_toggle_overwrite (text_view);
6112 * gtk_text_view_set_accepts_tab:
6113 * @text_view: A #GtkTextView
6114 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
6115 * character, %FALSE, if pressing the Tab key should move the
6118 * Sets the behavior of the text widget when the Tab key is pressed.
6119 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
6120 * is %FALSE the keyboard focus is moved to the next widget in the focus
6126 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
6127 gboolean accepts_tab)
6129 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6131 accepts_tab = accepts_tab != FALSE;
6133 if (text_view->priv->accepts_tab != accepts_tab)
6135 text_view->priv->accepts_tab = accepts_tab;
6137 g_object_notify (G_OBJECT (text_view), "accepts-tab");
6142 * gtk_text_view_get_accepts_tab:
6143 * @text_view: A #GtkTextView
6145 * Returns whether pressing the Tab key inserts a tab characters.
6146 * gtk_text_view_set_accepts_tab().
6148 * Return value: %TRUE if pressing the Tab key inserts a tab character,
6149 * %FALSE if pressing the Tab key moves the keyboard focus.
6154 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
6156 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6158 return text_view->priv->accepts_tab;
6162 gtk_text_view_compat_move_focus (GtkTextView *text_view,
6163 GtkDirectionType direction_type)
6165 GSignalInvocationHint *hint = g_signal_get_invocation_hint (text_view);
6167 /* as of GTK+ 2.12, the "move-focus" signal has been moved to GtkWidget,
6168 * the evil code below makes sure that both emitting the signal and
6169 * calling the virtual function directly continue to work as expetcted
6172 if (hint->signal_id == g_signal_lookup ("move-focus", GTK_TYPE_WIDGET))
6174 /* if this is a signal emission, chain up */
6178 g_signal_chain_from_overridden_handler (text_view,
6179 direction_type, &retval);
6183 /* otherwise emit the signal, since somebody called the virtual
6187 g_signal_emit_by_name (text_view, "move-focus", direction_type);
6196 gtk_text_view_unselect (GtkTextView *text_view)
6200 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6201 gtk_text_buffer_get_insert (get_buffer (text_view)));
6203 gtk_text_buffer_move_mark (get_buffer (text_view),
6204 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
6209 get_iter_at_pointer (GtkTextView *text_view,
6215 GtkTextViewPrivate *priv;
6216 gint xcoord, ycoord;
6217 GdkModifierType state;
6219 priv = text_view->priv;
6221 gdk_window_get_device_position (priv->text_window->bin_window,
6222 device, &xcoord, &ycoord, &state);
6224 gtk_text_layout_get_iter_at_pixel (priv->layout,
6226 xcoord + priv->xoffset,
6227 ycoord + priv->yoffset);
6236 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
6237 const gchar *mark_name,
6240 GtkTextIter newplace;
6243 get_iter_at_pointer (text_view, device, &newplace, NULL, NULL);
6245 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
6247 /* This may invalidate the layout */
6248 DV(g_print (G_STRLOC": move mark\n"));
6250 gtk_text_buffer_move_mark (get_buffer (text_view),
6254 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6255 gtk_text_view_scroll_mark_onscreen (text_view, mark);
6257 DV (g_print ("first validate idle leaving %s is %d\n",
6258 G_STRLOC, text_view->priv->first_validate_idle));
6262 selection_scan_timeout (gpointer data)
6264 GtkTextView *text_view;
6266 text_view = GTK_TEXT_VIEW (data);
6268 gtk_text_view_scroll_mark_onscreen (text_view,
6269 gtk_text_buffer_get_insert (get_buffer (text_view)));
6271 return TRUE; /* remain installed. */
6274 #define UPPER_OFFSET_ANCHOR 0.8
6275 #define LOWER_OFFSET_ANCHOR 0.2
6278 check_scroll (gdouble offset, GtkAdjustment *adj)
6280 if ((offset > UPPER_OFFSET_ANCHOR &&
6281 adj->value + adj->page_size < adj->upper) ||
6282 (offset < LOWER_OFFSET_ANCHOR &&
6283 adj->value > adj->lower))
6290 drag_scan_timeout (gpointer data)
6292 GtkTextView *text_view;
6293 GtkTextViewPrivate *priv;
6294 GtkTextIter newplace;
6296 gdouble pointer_xoffset, pointer_yoffset;
6298 text_view = GTK_TEXT_VIEW (data);
6299 priv = text_view->priv;
6301 get_iter_at_pointer (text_view, priv->dnd_device, &newplace, &x, &y);
6303 gtk_text_buffer_move_mark (get_buffer (text_view),
6307 pointer_xoffset = (gdouble) x / gdk_window_get_width (priv->text_window->bin_window);
6308 pointer_yoffset = (gdouble) y / gdk_window_get_height (priv->text_window->bin_window);
6310 if (check_scroll (pointer_xoffset, priv->hadjustment) ||
6311 check_scroll (pointer_yoffset, priv->vadjustment))
6313 /* do not make offsets surpass lower nor upper anchors, this makes
6314 * scrolling speed relative to the distance of the pointer to the
6315 * anchors when it moves beyond them.
6317 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6318 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6320 gtk_text_view_scroll_to_mark (text_view,
6322 0., TRUE, pointer_xoffset, pointer_yoffset);
6333 } SelectionGranularity;
6336 * Move @start and @end to the boundaries of the selection unit (indicated by
6337 * @granularity) which contained @start initially.
6338 * If the selction unit is SELECT_WORDS and @start is not contained in a word
6339 * the selection is extended to all the white spaces between the end of the
6340 * word preceding @start and the start of the one following.
6343 extend_selection (GtkTextView *text_view,
6344 SelectionGranularity granularity,
6350 if (granularity == SELECT_WORDS)
6352 if (gtk_text_iter_inside_word (start))
6354 if (!gtk_text_iter_starts_word (start))
6355 gtk_text_iter_backward_visible_word_start (start);
6357 if (!gtk_text_iter_ends_word (end))
6359 if (!gtk_text_iter_forward_visible_word_end (end))
6360 gtk_text_iter_forward_to_end (end);
6368 if (gtk_text_iter_backward_visible_word_start (&tmp))
6369 gtk_text_iter_forward_visible_word_end (&tmp);
6371 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
6374 gtk_text_iter_set_line_offset (start, 0);
6377 if (!gtk_text_iter_forward_visible_word_end (&tmp))
6378 gtk_text_iter_forward_to_end (&tmp);
6380 if (gtk_text_iter_ends_word (&tmp))
6381 gtk_text_iter_backward_visible_word_start (&tmp);
6383 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
6386 gtk_text_iter_forward_to_line_end (end);
6389 else if (granularity == SELECT_LINES)
6391 if (gtk_text_view_starts_display_line (text_view, start))
6393 /* If on a display line boundary, we assume the user
6394 * clicked off the end of a line and we therefore select
6395 * the line before the boundary.
6397 gtk_text_view_backward_display_line_start (text_view, start);
6401 /* start isn't on the start of a line, so we move it to the
6402 * start, and move end to the end unless it's already there.
6404 gtk_text_view_backward_display_line_start (text_view, start);
6406 if (!gtk_text_view_starts_display_line (text_view, end))
6407 gtk_text_view_forward_display_line_end (text_view, end);
6415 SelectionGranularity granularity;
6416 GtkTextMark *orig_start;
6417 GtkTextMark *orig_end;
6421 selection_data_free (SelectionData *data)
6423 if (data->orig_start != NULL)
6424 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6426 if (data->orig_end != NULL)
6427 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6433 selection_motion_event_handler (GtkTextView *text_view,
6434 GdkEventMotion *event,
6435 SelectionData *data)
6437 GtkTextViewPrivate *priv;
6439 priv = text_view->priv;
6440 gdk_event_request_motions (event);
6442 if (priv->grab_device != event->device)
6445 if (data->granularity == SELECT_CHARACTERS)
6447 move_mark_to_pointer_and_scroll (text_view, "insert", event->device);
6451 GtkTextIter cursor, start, end;
6452 GtkTextIter orig_start, orig_end;
6453 GtkTextBuffer *buffer;
6455 buffer = get_buffer (text_view);
6457 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6458 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6460 get_iter_at_pointer (text_view, event->device, &cursor, NULL, NULL);
6463 extend_selection (text_view, data->granularity, &start, &end);
6465 /* either the selection extends to the front, or end (or not) */
6466 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6467 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6469 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6471 gtk_text_view_scroll_mark_onscreen (text_view,
6472 gtk_text_buffer_get_insert (buffer));
6475 /* If we had to scroll offscreen, insert a timeout to do so
6476 * again. Note that in the timeout, even if the mouse doesn't
6477 * move, due to this scroll xoffset/yoffset will have changed
6478 * and we'll need to scroll again.
6480 if (text_view->priv->scroll_timeout != 0) /* reset on every motion event */
6481 g_source_remove (text_view->priv->scroll_timeout);
6483 text_view->priv->scroll_timeout =
6484 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6490 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6491 const GtkTextIter *iter,
6492 GdkEventButton *button)
6494 GtkTextViewPrivate *priv;
6495 GtkTextIter cursor, ins, bound;
6496 GtkTextIter orig_start, orig_end;
6497 GtkTextBuffer *buffer;
6498 SelectionData *data;
6500 if (text_view->priv->selection_drag_handler != 0)
6503 priv = text_view->priv;
6504 data = g_new0 (SelectionData, 1);
6506 if (button->type == GDK_2BUTTON_PRESS)
6507 data->granularity = SELECT_WORDS;
6508 else if (button->type == GDK_3BUTTON_PRESS)
6509 data->granularity = SELECT_LINES;
6511 data->granularity = SELECT_CHARACTERS;
6513 priv->grab_device = button->device;
6514 gtk_device_grab_add (GTK_WIDGET (text_view),
6518 buffer = get_buffer (text_view);
6523 extend_selection (text_view, data->granularity, &ins, &bound);
6527 if (button->state & GDK_SHIFT_MASK)
6529 /* Extend selection */
6530 GtkTextIter old_ins, old_bound;
6531 GtkTextIter old_start, old_end;
6533 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6534 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6535 old_start = old_ins;
6536 old_end = old_bound;
6537 gtk_text_iter_order (&old_start, &old_end);
6539 /* move the front cursor, if the mouse is in front of the selection. Should the
6540 * cursor however be inside the selection (this happens on tripple click) then we
6541 * move the side which was last moved (current insert mark) */
6542 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6543 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6544 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6547 orig_start = old_end;
6559 gtk_text_buffer_select_range (buffer, &ins, &bound);
6561 gtk_text_iter_order (&orig_start, &orig_end);
6562 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6564 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6566 gtk_text_view_check_cursor_blink (text_view);
6568 text_view->priv->selection_drag_handler = g_signal_connect_data (text_view,
6569 "motion-notify-event",
6570 G_CALLBACK (selection_motion_event_handler),
6572 (GClosureNotify) selection_data_free, 0);
6575 /* returns whether we were really dragging */
6577 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6579 GtkTextViewPrivate *priv;
6581 priv = text_view->priv;
6583 if (!priv->grab_device)
6586 if (priv->selection_drag_handler == 0)
6589 g_signal_handler_disconnect (text_view, priv->selection_drag_handler);
6590 priv->selection_drag_handler = 0;
6592 if (priv->scroll_timeout != 0)
6594 g_source_remove (priv->scroll_timeout);
6595 priv->scroll_timeout = 0;
6598 gtk_device_grab_remove (GTK_WIDGET (text_view),
6600 priv->grab_device = NULL;
6610 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6611 GtkTextAttributes *values,
6614 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
6615 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
6618 pango_font_description_free (values->font);
6620 values->font = pango_font_description_copy (style->font_desc);
6624 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6626 GtkTextViewPrivate *priv = text_view->priv;
6630 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6631 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6632 GtkTextDirection new_cursor_dir;
6633 GtkTextDirection new_keyboard_dir;
6634 gboolean split_cursor;
6636 g_object_get (settings,
6637 "gtk-split-cursor", &split_cursor,
6640 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6641 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6643 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6646 new_cursor_dir = GTK_TEXT_DIR_NONE;
6648 new_cursor_dir = new_keyboard_dir;
6650 gtk_text_layout_set_cursor_direction (priv->layout, new_cursor_dir);
6651 gtk_text_layout_set_keyboard_direction (priv->layout, new_keyboard_dir);
6656 gtk_text_view_ensure_layout (GtkTextView *text_view)
6659 GtkTextViewPrivate *priv;
6661 widget = GTK_WIDGET (text_view);
6662 priv = text_view->priv;
6664 if (priv->layout == NULL)
6666 GtkTextAttributes *style;
6667 PangoContext *ltr_context, *rtl_context;
6670 DV(g_print(G_STRLOC"\n"));
6672 priv->layout = gtk_text_layout_new ();
6674 g_signal_connect (priv->layout,
6676 G_CALLBACK (invalidated_handler),
6679 g_signal_connect (priv->layout,
6681 G_CALLBACK (changed_handler),
6684 g_signal_connect (priv->layout,
6686 G_CALLBACK (gtk_text_view_child_allocated),
6689 if (get_buffer (text_view))
6690 gtk_text_layout_set_buffer (priv->layout, get_buffer (text_view));
6692 if ((gtk_widget_has_focus (widget) && priv->cursor_visible))
6693 gtk_text_view_pend_cursor_blink (text_view);
6695 gtk_text_layout_set_cursor_visible (priv->layout, FALSE);
6697 gtk_text_layout_set_overwrite_mode (priv->layout,
6698 priv->overwrite_mode && priv->editable);
6700 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6701 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6702 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6703 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6705 gtk_text_layout_set_contexts (priv->layout, ltr_context, rtl_context);
6707 g_object_unref (ltr_context);
6708 g_object_unref (rtl_context);
6710 gtk_text_view_check_keymap_direction (text_view);
6712 style = gtk_text_attributes_new ();
6714 gtk_widget_ensure_style (widget);
6715 gtk_text_view_set_attributes_from_style (text_view,
6717 gtk_widget_get_style (widget));
6719 style->pixels_above_lines = priv->pixels_above_lines;
6720 style->pixels_below_lines = priv->pixels_below_lines;
6721 style->pixels_inside_wrap = priv->pixels_inside_wrap;
6722 style->left_margin = priv->left_margin;
6723 style->right_margin = priv->right_margin;
6724 style->indent = priv->indent;
6725 style->tabs = priv->tabs ? pango_tab_array_copy (priv->tabs) : NULL;
6727 style->wrap_mode = priv->wrap_mode;
6728 style->justification = priv->justify;
6729 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6731 gtk_text_layout_set_default_style (priv->layout, style);
6733 gtk_text_attributes_unref (style);
6735 /* Set layout for all anchored children */
6737 tmp_list = priv->children;
6738 while (tmp_list != NULL)
6740 GtkTextViewChild *vc = tmp_list->data;
6744 gtk_text_anchored_child_set_layout (vc->widget,
6746 /* vc may now be invalid! */
6749 tmp_list = g_slist_next (tmp_list);
6752 gtk_text_view_invalidate (text_view);
6757 * gtk_text_view_get_default_attributes:
6758 * @text_view: a #GtkTextView
6760 * Obtains a copy of the default text attributes. These are the
6761 * attributes used for text unless a tag overrides them.
6762 * You'd typically pass the default attributes in to
6763 * gtk_text_iter_get_attributes() in order to get the
6764 * attributes in effect at a given text position.
6766 * The return value is a copy owned by the caller of this function,
6767 * and should be freed.
6769 * Return value: a new #GtkTextAttributes
6772 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6774 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6776 gtk_text_view_ensure_layout (text_view);
6778 return gtk_text_attributes_copy (text_view->priv->layout->default_style);
6782 gtk_text_view_destroy_layout (GtkTextView *text_view)
6784 GtkTextViewPrivate *priv = text_view->priv;
6790 gtk_text_view_remove_validate_idles (text_view);
6792 g_signal_handlers_disconnect_by_func (priv->layout,
6793 invalidated_handler,
6795 g_signal_handlers_disconnect_by_func (priv->layout,
6799 /* Remove layout from all anchored children */
6800 tmp_list = priv->children;
6801 while (tmp_list != NULL)
6803 GtkTextViewChild *vc = tmp_list->data;
6807 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6808 /* vc may now be invalid! */
6811 tmp_list = g_slist_next (tmp_list);
6814 gtk_text_view_stop_cursor_blink (text_view);
6815 gtk_text_view_end_selection_drag (text_view);
6817 g_object_unref (priv->layout);
6818 priv->layout = NULL;
6823 * gtk_text_view_reset_im_context:
6824 * @text_view: a #GtkTextView
6826 * Reset the input method context of the text view if needed.
6828 * This can be necessary in the case where modifying the buffer
6829 * would confuse on-going input method behavior.
6834 gtk_text_view_reset_im_context (GtkTextView *text_view)
6836 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6838 if (text_view->priv->need_im_reset)
6840 text_view->priv->need_im_reset = FALSE;
6841 gtk_im_context_reset (text_view->priv->im_context);
6846 * gtk_text_view_im_context_filter_keypress:
6847 * @text_view: a #GtkTextView
6848 * @event: the key event
6850 * Allow the #GtkTextView input method to internally handle key press
6851 * and release events. If this function returns %TRUE, then no further
6852 * processing should be done for this key event. See
6853 * gtk_im_context_filter_keypress().
6855 * Note that you are expected to call this function from your handler
6856 * when overriding key event handling. This is needed in the case when
6857 * you need to insert your own key handling between the input method
6858 * and the default key event handling of the #GtkTextView.
6862 * gtk_foo_bar_key_press_event (GtkWidget *widget,
6863 * GdkEventKey *event)
6865 * if ((key->keyval == GDK_Return || key->keyval == GDK_KP_Enter))
6867 * if (gtk_text_view_im_context_filter_keypress (GTK_TEXT_VIEW (view), event))
6871 * /* Do some stuff */
6873 * return GTK_WIDGET_CLASS (gtk_foo_bar_parent_class)->key_press_event (widget, event);
6877 * Return value: %TRUE if the input method handled the key event.
6882 gtk_text_view_im_context_filter_keypress (GtkTextView *text_view,
6885 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6887 return gtk_im_context_filter_keypress (text_view->priv->im_context, event);
6895 drag_begin_cb (GtkWidget *widget,
6896 GdkDragContext *context,
6899 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6900 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6903 cairo_surface_t *surface = NULL;
6905 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6907 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6908 surface = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6912 gtk_drag_set_icon_surface (context, surface);
6913 cairo_surface_destroy (surface);
6917 gtk_drag_set_icon_default (context);
6922 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6923 const GtkTextIter *iter,
6924 GdkEventMotion *event)
6926 GtkTargetList *target_list;
6928 text_view->priv->drag_start_x = -1;
6929 text_view->priv->drag_start_y = -1;
6930 text_view->priv->pending_place_cursor_button = 0;
6932 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6934 g_signal_connect (text_view, "drag-begin",
6935 G_CALLBACK (drag_begin_cb), NULL);
6936 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6937 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6938 1, (GdkEvent*)event);
6942 gtk_text_view_drag_begin (GtkWidget *widget,
6943 GdkDragContext *context)
6949 gtk_text_view_drag_end (GtkWidget *widget,
6950 GdkDragContext *context)
6955 gtk_text_view_drag_data_get (GtkWidget *widget,
6956 GdkDragContext *context,
6957 GtkSelectionData *selection_data,
6961 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6962 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6964 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6966 gtk_selection_data_set (selection_data,
6967 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6972 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6979 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6981 /* Extract the selected text */
6982 str = gtk_text_buffer_serialize (buffer, buffer,
6983 selection_data->target,
6990 gtk_selection_data_set (selection_data,
6991 selection_data->target,
6993 (guchar *) str, len);
7003 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
7005 /* Extract the selected text */
7006 str = gtk_text_iter_get_visible_text (&start, &end);
7011 gtk_selection_data_set_text (selection_data, str, -1);
7018 gtk_text_view_drag_data_delete (GtkWidget *widget,
7019 GdkDragContext *context)
7021 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->priv->buffer,
7022 TRUE, GTK_TEXT_VIEW (widget)->priv->editable);
7026 gtk_text_view_drag_leave (GtkWidget *widget,
7027 GdkDragContext *context,
7030 GtkTextView *text_view;
7031 GtkTextViewPrivate *priv;
7033 text_view = GTK_TEXT_VIEW (widget);
7034 priv = text_view->priv;
7036 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7038 if (priv->dnd_device)
7039 priv->dnd_device = NULL;
7041 if (priv->scroll_timeout != 0)
7042 g_source_remove (priv->scroll_timeout);
7044 priv->scroll_timeout = 0;
7048 gtk_text_view_drag_motion (GtkWidget *widget,
7049 GdkDragContext *context,
7054 GtkTextIter newplace;
7055 GtkTextView *text_view;
7056 GtkTextViewPrivate *priv;
7059 GdkRectangle target_rect;
7062 GdkDragAction suggested_action = 0;
7064 text_view = GTK_TEXT_VIEW (widget);
7065 priv = text_view->priv;
7067 target_rect = priv->text_window->allocation;
7069 if (x < target_rect.x ||
7070 y < target_rect.y ||
7071 x > (target_rect.x + target_rect.width) ||
7072 y > (target_rect.y + target_rect.height))
7073 return FALSE; /* outside the text window, allow parent widgets to handle event */
7075 gtk_text_view_window_to_buffer_coords (text_view,
7076 GTK_TEXT_WINDOW_WIDGET,
7080 gtk_text_layout_get_iter_at_pixel (priv->layout,
7084 target = gtk_drag_dest_find_target (widget, context,
7085 gtk_drag_dest_get_target_list (widget));
7087 if (target == GDK_NONE)
7089 /* can't accept any of the offered targets */
7091 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7093 gtk_text_iter_compare (&newplace, &start) >= 0 &&
7094 gtk_text_iter_compare (&newplace, &end) <= 0)
7096 /* We're inside the selection. */
7100 if (gtk_text_iter_can_insert (&newplace, priv->editable))
7102 GtkWidget *source_widget;
7104 suggested_action = context->suggested_action;
7106 source_widget = gtk_drag_get_source_widget (context);
7108 if (source_widget == widget)
7110 /* Default to MOVE, unless the user has
7111 * pressed ctrl or alt to affect available actions
7113 if ((context->actions & GDK_ACTION_MOVE) != 0)
7114 suggested_action = GDK_ACTION_MOVE;
7119 /* Can't drop here. */
7123 if (suggested_action != 0)
7125 gtk_text_mark_set_visible (priv->dnd_mark,
7126 priv->cursor_visible);
7128 gdk_drag_status (context, suggested_action, time);
7132 gdk_drag_status (context, 0, time);
7133 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7136 priv->dnd_device = gdk_drag_context_get_device (context);
7138 if (!priv->scroll_timeout)
7139 priv->scroll_timeout =
7140 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
7142 /* TRUE return means don't propagate the drag motion to parent
7143 * widgets that may also be drop sites.
7149 gtk_text_view_drag_drop (GtkWidget *widget,
7150 GdkDragContext *context,
7155 GtkTextView *text_view;
7156 GtkTextViewPrivate *priv;
7157 GtkTextIter drop_point;
7158 GdkAtom target = GDK_NONE;
7160 text_view = GTK_TEXT_VIEW (widget);
7161 priv = text_view->priv;
7163 if (priv->scroll_timeout != 0)
7164 g_source_remove (priv->scroll_timeout);
7166 priv->scroll_timeout = 0;
7168 gtk_text_mark_set_visible (priv->dnd_mark, FALSE);
7170 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7174 if (gtk_text_iter_can_insert (&drop_point, priv->editable))
7175 target = gtk_drag_dest_find_target (widget, context, NULL);
7177 if (target != GDK_NONE)
7178 gtk_drag_get_data (widget, context, target, time);
7180 gtk_drag_finish (context, FALSE, FALSE, time);
7186 insert_text_data (GtkTextView *text_view,
7187 GtkTextIter *drop_point,
7188 GtkSelectionData *selection_data)
7192 str = gtk_selection_data_get_text (selection_data);
7196 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
7197 drop_point, (gchar *) str, -1,
7198 text_view->priv->editable))
7200 gtk_widget_error_bell (GTK_WIDGET (text_view));
7208 gtk_text_view_drag_data_received (GtkWidget *widget,
7209 GdkDragContext *context,
7212 GtkSelectionData *selection_data,
7216 GtkTextIter drop_point;
7217 GtkTextView *text_view;
7218 GtkTextViewPrivate *priv;
7219 gboolean success = FALSE;
7220 GtkTextBuffer *buffer = NULL;
7222 text_view = GTK_TEXT_VIEW (widget);
7223 priv = text_view->priv;
7225 if (!priv->dnd_mark)
7228 buffer = get_buffer (text_view);
7230 gtk_text_buffer_get_iter_at_mark (buffer,
7234 if (!gtk_text_iter_can_insert (&drop_point, priv->editable))
7239 gtk_text_buffer_begin_user_action (buffer);
7241 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7243 GtkTextBuffer *src_buffer = NULL;
7244 GtkTextIter start, end;
7245 gboolean copy_tags = TRUE;
7247 if (selection_data->length != sizeof (src_buffer))
7250 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
7252 if (src_buffer == NULL)
7255 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
7257 if (gtk_text_buffer_get_tag_table (src_buffer) !=
7258 gtk_text_buffer_get_tag_table (buffer))
7260 /* try to find a suitable rich text target instead */
7264 GdkAtom target = GDK_NONE;
7268 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
7270 for (list = context->targets; list; list = g_list_next (list))
7274 for (i = 0; i < n_atoms; i++)
7275 if (GUINT_TO_POINTER (atoms[i]) == list->data)
7284 if (target != GDK_NONE)
7286 gtk_drag_get_data (widget, context, target, time);
7287 gtk_text_buffer_end_user_action (buffer);
7292 if (gtk_text_buffer_get_selection_bounds (src_buffer,
7297 gtk_text_buffer_insert_range_interactive (buffer,
7306 str = gtk_text_iter_get_visible_text (&start, &end);
7307 gtk_text_buffer_insert_interactive (buffer,
7308 &drop_point, str, -1,
7314 else if (selection_data->length > 0 &&
7315 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
7318 GError *error = NULL;
7320 retval = gtk_text_buffer_deserialize (buffer, buffer,
7321 selection_data->target,
7323 (guint8 *) selection_data->data,
7324 selection_data->length,
7329 g_warning ("error pasting: %s\n", error->message);
7330 g_clear_error (&error);
7334 insert_text_data (text_view, &drop_point, selection_data);
7337 gtk_drag_finish (context, success,
7338 success && context->action == GDK_ACTION_MOVE,
7343 gtk_text_buffer_get_iter_at_mark (buffer,
7346 gtk_text_buffer_place_cursor (buffer, &drop_point);
7348 gtk_text_buffer_end_user_action (buffer);
7353 * gtk_text_view_get_hadjustment:
7354 * @text_view: a #GtkTextView
7356 * Gets the horizontal-scrolling #GtkAdjustment.
7358 * Returns: (transfer none): pointer to the horizontal #GtkAdjustment
7363 gtk_text_view_get_hadjustment (GtkTextView *text_view)
7365 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7367 return get_hadjustment (text_view);
7371 * gtk_text_view_get_vadjustment:
7372 * @text_view: a #GtkTextView
7374 * Gets the vertical-scrolling #GtkAdjustment.
7376 * Returns: (transfer none): pointer to the vertical #GtkAdjustment
7381 gtk_text_view_get_vadjustment (GtkTextView *text_view)
7383 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7385 return get_vadjustment (text_view);
7388 static GtkAdjustment*
7389 get_hadjustment (GtkTextView *text_view)
7391 if (text_view->priv->hadjustment == NULL)
7392 gtk_text_view_set_scroll_adjustments (text_view,
7393 NULL, /* forces creation */
7394 text_view->priv->vadjustment);
7396 return text_view->priv->hadjustment;
7399 static GtkAdjustment*
7400 get_vadjustment (GtkTextView *text_view)
7402 if (text_view->priv->vadjustment == NULL)
7403 gtk_text_view_set_scroll_adjustments (text_view,
7404 text_view->priv->hadjustment,
7405 NULL); /* forces creation */
7406 return text_view->priv->vadjustment;
7411 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
7412 GtkAdjustment *hadj,
7413 GtkAdjustment *vadj)
7415 GtkTextViewPrivate *priv = text_view->priv;
7416 gboolean need_adjust = FALSE;
7419 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
7421 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7423 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
7425 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7427 if (priv->hadjustment && (priv->hadjustment != hadj))
7429 g_signal_handlers_disconnect_by_func (priv->hadjustment,
7430 gtk_text_view_value_changed,
7432 g_object_unref (priv->hadjustment);
7435 if (priv->vadjustment && (priv->vadjustment != vadj))
7437 g_signal_handlers_disconnect_by_func (priv->vadjustment,
7438 gtk_text_view_value_changed,
7440 g_object_unref (priv->vadjustment);
7443 if (priv->hadjustment != hadj)
7445 priv->hadjustment = hadj;
7446 g_object_ref_sink (priv->hadjustment);
7448 g_signal_connect (priv->hadjustment, "value-changed",
7449 G_CALLBACK (gtk_text_view_value_changed),
7454 if (priv->vadjustment != vadj)
7456 priv->vadjustment = vadj;
7457 g_object_ref_sink (priv->vadjustment);
7459 g_signal_connect (priv->vadjustment, "value-changed",
7460 G_CALLBACK (gtk_text_view_value_changed),
7466 gtk_text_view_value_changed (NULL, text_view);
7469 /* FIXME this adjust_allocation is a big cut-and-paste from
7470 * GtkCList, needs to be some "official" way to do this
7480 /* The window to which widget->window is relative */
7481 #define ALLOCATION_WINDOW(widget) \
7482 (!gtk_widget_get_has_window (widget) ? \
7483 gtk_widget_get_window (widget) : \
7484 gdk_window_get_parent (gtk_widget_get_window (widget)))
7487 adjust_allocation_recurse (GtkWidget *widget,
7490 GtkAllocation allocation;
7491 ScrollData *scroll_data = data;
7493 /* Need to really size allocate instead of just poking
7494 * into widget->allocation if the widget is not realized.
7495 * FIXME someone figure out why this was.
7497 if (!gtk_widget_get_realized (widget))
7499 if (gtk_widget_get_visible (widget))
7501 GdkRectangle tmp_rectangle;
7503 tmp_rectangle = allocation;
7504 tmp_rectangle.x += scroll_data->dx;
7505 tmp_rectangle.y += scroll_data->dy;
7507 gtk_widget_size_allocate (widget, &tmp_rectangle);
7512 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
7514 allocation.x += scroll_data->dx;
7515 allocation.y += scroll_data->dy;
7516 gtk_widget_set_allocation (widget, &allocation);
7518 if (GTK_IS_CONTAINER (widget))
7519 gtk_container_forall (GTK_CONTAINER (widget),
7520 adjust_allocation_recurse,
7527 adjust_allocation (GtkWidget *widget,
7531 ScrollData scroll_data;
7533 if (gtk_widget_get_realized (widget))
7534 scroll_data.window = ALLOCATION_WINDOW (widget);
7536 scroll_data.window = NULL;
7538 scroll_data.dx = dx;
7539 scroll_data.dy = dy;
7541 adjust_allocation_recurse (widget, &scroll_data);
7545 gtk_text_view_value_changed (GtkAdjustment *adj,
7546 GtkTextView *text_view)
7548 GtkTextViewPrivate *priv;
7554 priv = text_view->priv;
7556 /* Note that we oddly call this function with adj == NULL
7560 priv->onscreen_validated = FALSE;
7562 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7563 adj == priv->hadjustment ? "hadj" : adj == priv->vadjustment ? "vadj" : "none",
7564 adj ? adj->value : 0.0));
7566 if (adj == priv->hadjustment)
7568 dx = priv->xoffset - (gint)adj->value;
7569 priv->xoffset = adj->value;
7571 /* If the change is due to a size change we need
7572 * to invalidate the entire text window because there might be
7573 * right-aligned or centered text
7575 if (priv->width_changed)
7577 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7578 gdk_window_invalidate_rect (priv->text_window->bin_window, NULL, FALSE);
7580 priv->width_changed = FALSE;
7583 else if (adj == priv->vadjustment)
7585 dy = priv->yoffset - (gint)adj->value;
7586 priv->yoffset = adj->value;
7590 gtk_text_layout_get_line_at_y (priv->layout, &iter, adj->value, &line_top);
7592 gtk_text_buffer_move_mark (get_buffer (text_view), priv->first_para_mark, &iter);
7594 priv->first_para_pixels = adj->value - line_top;
7598 if (dx != 0 || dy != 0)
7602 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7606 if (priv->left_window)
7607 text_window_scroll (priv->left_window, 0, dy);
7608 if (priv->right_window)
7609 text_window_scroll (priv->right_window, 0, dy);
7614 if (priv->top_window)
7615 text_window_scroll (priv->top_window, dx, 0);
7616 if (priv->bottom_window)
7617 text_window_scroll (priv->bottom_window, dx, 0);
7620 /* It looks nicer to scroll the main area last, because
7621 * it takes a while, and making the side areas update
7622 * afterward emphasizes the slowness of scrolling the
7625 text_window_scroll (priv->text_window, dx, dy);
7628 /* Children are now "moved" in the text window, poke
7629 * into widget->allocation for each child
7631 tmp_list = priv->children;
7632 while (tmp_list != NULL)
7634 GtkTextViewChild *child = tmp_list->data;
7637 adjust_allocation (child->widget, dx, dy);
7639 tmp_list = g_slist_next (tmp_list);
7643 /* This could result in invalidation, which would install the
7644 * first_validate_idle, which would validate onscreen;
7645 * but we're going to go ahead and validate here, so
7646 * first_validate_idle shouldn't have anything to do.
7648 gtk_text_view_update_layout_width (text_view);
7650 /* We also update the IM spot location here, since the IM context
7651 * might do something that leads to validation.
7653 gtk_text_view_update_im_spot_location (text_view);
7655 /* note that validation of onscreen could invoke this function
7656 * recursively, by scrolling to maintain first_para, or in response
7657 * to updating the layout width, however there is no problem with
7658 * that, or shouldn't be.
7660 gtk_text_view_validate_onscreen (text_view);
7662 /* process exposes */
7663 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
7665 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7667 if (priv->left_window)
7668 gdk_window_process_updates (priv->left_window->bin_window, TRUE);
7670 if (priv->right_window)
7671 gdk_window_process_updates (priv->right_window->bin_window, TRUE);
7673 if (priv->top_window)
7674 gdk_window_process_updates (priv->top_window->bin_window, TRUE);
7676 if (priv->bottom_window)
7677 gdk_window_process_updates (priv->bottom_window->bin_window, TRUE);
7679 gdk_window_process_updates (priv->text_window->bin_window, TRUE);
7682 /* If this got installed, get rid of it, it's just a waste of time. */
7683 if (priv->first_validate_idle != 0)
7685 g_source_remove (priv->first_validate_idle);
7686 priv->first_validate_idle = 0;
7689 /* Finally we update the IM cursor location again, to ensure any
7690 * changes made by the validation are pushed through.
7692 gtk_text_view_update_im_spot_location (text_view);
7694 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7698 gtk_text_view_commit_handler (GtkIMContext *context,
7700 GtkTextView *text_view)
7702 gtk_text_view_commit_text (text_view, str);
7706 gtk_text_view_commit_text (GtkTextView *text_view,
7709 GtkTextViewPrivate *priv;
7710 gboolean had_selection;
7712 priv = text_view->priv;
7714 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7716 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7719 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7722 if (!strcmp (str, "\n"))
7724 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7727 gtk_widget_error_bell (GTK_WIDGET (text_view));
7732 if (!had_selection && priv->overwrite_mode)
7736 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7738 gtk_text_buffer_get_insert (get_buffer (text_view)));
7739 if (!gtk_text_iter_ends_line (&insert))
7740 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7743 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7746 gtk_widget_error_bell (GTK_WIDGET (text_view));
7750 gtk_text_buffer_end_user_action (get_buffer (text_view));
7752 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7753 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7754 gtk_text_view_scroll_mark_onscreen (text_view,
7755 gtk_text_buffer_get_insert (get_buffer (text_view)));
7759 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7760 GtkTextView *text_view)
7762 GtkTextViewPrivate *priv;
7764 PangoAttrList *attrs;
7768 priv = text_view->priv;
7770 gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter,
7771 gtk_text_buffer_get_insert (priv->buffer));
7773 /* Keypress events are passed to input method even if cursor position is
7774 * not editable; so beep here if it's multi-key input sequence, input
7775 * method will be reset in key-press-event handler.
7777 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7779 if (str && str[0] && !gtk_text_iter_can_insert (&iter, priv->editable))
7781 gtk_widget_error_bell (GTK_WIDGET (text_view));
7785 g_signal_emit (text_view, signals[PREEDIT_CHANGED], 0, str);
7788 gtk_text_layout_set_preedit_string (priv->layout, str, attrs, cursor_pos);
7789 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
7790 gtk_text_view_scroll_mark_onscreen (text_view,
7791 gtk_text_buffer_get_insert (get_buffer (text_view)));
7794 pango_attr_list_unref (attrs);
7799 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7800 GtkTextView *text_view)
7807 gtk_text_buffer_get_iter_at_mark (text_view->priv->buffer, &start,
7808 gtk_text_buffer_get_insert (text_view->priv->buffer));
7811 pos = gtk_text_iter_get_line_index (&start);
7812 gtk_text_iter_set_line_offset (&start, 0);
7813 gtk_text_iter_forward_to_line_end (&end);
7815 text = gtk_text_iter_get_slice (&start, &end);
7816 gtk_im_context_set_surrounding (context, text, -1, pos);
7823 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7826 GtkTextView *text_view)
7828 GtkTextViewPrivate *priv;
7832 priv = text_view->priv;
7834 gtk_text_buffer_get_iter_at_mark (priv->buffer, &start,
7835 gtk_text_buffer_get_insert (priv->buffer));
7838 gtk_text_iter_forward_chars (&start, offset);
7839 gtk_text_iter_forward_chars (&end, offset + n_chars);
7841 gtk_text_buffer_delete_interactive (priv->buffer, &start, &end,
7848 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7849 const GtkTextIter *location,
7853 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7854 gboolean need_reset = FALSE;
7856 if (mark == gtk_text_buffer_get_insert (buffer))
7858 text_view->priv->virtual_cursor_x = -1;
7859 text_view->priv->virtual_cursor_y = -1;
7860 gtk_text_view_update_im_spot_location (text_view);
7863 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7869 gtk_text_view_reset_im_context (text_view);
7873 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7874 const GParamSpec *pspec,
7877 GtkWidget *widget = GTK_WIDGET (data);
7878 GtkTargetList *view_list;
7879 GtkTargetList *buffer_list;
7882 view_list = gtk_drag_dest_get_target_list (widget);
7883 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7886 gtk_target_list_ref (view_list);
7888 view_list = gtk_target_list_new (NULL, 0);
7890 list = view_list->list;
7893 GtkTargetPair *pair = list->data;
7895 list = g_list_next (list); /* get next element before removing */
7897 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7898 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7900 gtk_target_list_remove (view_list, pair->target);
7904 for (list = buffer_list->list; list; list = g_list_next (list))
7906 GtkTargetPair *pair = list->data;
7908 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7911 gtk_drag_dest_set_target_list (widget, view_list);
7912 gtk_target_list_unref (view_list);
7916 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7921 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7922 gtk_text_buffer_get_insert (get_buffer (text_view)));
7924 gtk_text_layout_get_cursor_locations (text_view->priv->layout, &insert, pos, NULL);
7928 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7929 GtkTextIter *cursor,
7933 GtkTextViewPrivate *priv;
7937 priv = text_view->priv;
7942 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7943 gtk_text_buffer_get_insert (get_buffer (text_view)));
7945 if ((x && priv->virtual_cursor_x == -1) ||
7946 (y && priv->virtual_cursor_y == -1))
7947 gtk_text_layout_get_cursor_locations (priv->layout, &insert, &pos, NULL);
7951 if (priv->virtual_cursor_x != -1)
7952 *x = priv->virtual_cursor_x;
7959 if (priv->virtual_cursor_x != -1)
7960 *y = priv->virtual_cursor_y;
7962 *y = pos.y + pos.height / 2;
7967 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7973 if (!text_view->priv->layout)
7976 if (x == -1 || y == -1)
7977 gtk_text_view_get_cursor_location (text_view, &pos);
7979 text_view->priv->virtual_cursor_x = (x == -1) ? pos.x : x;
7980 text_view->priv->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7983 /* Quick hack of a popup menu
7986 activate_cb (GtkWidget *menuitem,
7987 GtkTextView *text_view)
7989 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7990 g_signal_emit_by_name (text_view, signal);
7994 append_action_signal (GtkTextView *text_view,
7996 const gchar *stock_id,
7997 const gchar *signal,
8000 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
8002 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
8003 g_signal_connect (menuitem, "activate",
8004 G_CALLBACK (activate_cb), text_view);
8006 gtk_widget_set_sensitive (menuitem, sensitive);
8008 gtk_widget_show (menuitem);
8009 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
8013 gtk_text_view_select_all (GtkWidget *widget,
8016 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
8017 GtkTextBuffer *buffer;
8018 GtkTextIter start_iter, end_iter, insert;
8020 buffer = text_view->priv->buffer;
8023 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
8024 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
8028 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
8029 gtk_text_buffer_get_insert (buffer));
8030 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
8035 select_all_cb (GtkWidget *menuitem,
8036 GtkTextView *text_view)
8038 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
8042 delete_cb (GtkTextView *text_view)
8044 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
8045 text_view->priv->editable);
8049 popup_menu_detach (GtkWidget *attach_widget,
8052 GTK_TEXT_VIEW (attach_widget)->priv->popup_menu = NULL;
8056 popup_position_func (GtkMenu *menu,
8062 GtkAllocation allocation;
8063 GtkTextView *text_view;
8065 GdkRectangle cursor_rect;
8066 GdkRectangle onscreen_rect;
8067 gint root_x, root_y;
8072 GdkRectangle monitor;
8074 text_view = GTK_TEXT_VIEW (user_data);
8075 widget = GTK_WIDGET (text_view);
8077 g_return_if_fail (gtk_widget_get_realized (widget));
8079 screen = gtk_widget_get_screen (widget);
8081 gdk_window_get_origin (gtk_widget_get_window (widget),
8084 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
8086 gtk_text_buffer_get_insert (get_buffer (text_view)));
8088 gtk_text_view_get_iter_location (text_view,
8092 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
8094 gtk_size_request_get_size (GTK_SIZE_REQUEST (text_view->priv->popup_menu),
8097 gtk_widget_get_allocation (widget, &allocation);
8099 /* can't use rectangle_intersect since cursor rect can have 0 width */
8100 if (cursor_rect.x >= onscreen_rect.x &&
8101 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
8102 cursor_rect.y >= onscreen_rect.y &&
8103 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
8105 gtk_text_view_buffer_to_window_coords (text_view,
8106 GTK_TEXT_WINDOW_WIDGET,
8107 cursor_rect.x, cursor_rect.y,
8108 &cursor_rect.x, &cursor_rect.y);
8110 *x = root_x + cursor_rect.x + cursor_rect.width;
8111 *y = root_y + cursor_rect.y + cursor_rect.height;
8115 /* Just center the menu, since cursor is offscreen. */
8116 *x = root_x + (allocation.width / 2 - req.width / 2);
8117 *y = root_y + (allocation.height / 2 - req.height / 2);
8121 *x = CLAMP (*x, root_x, (root_x + allocation.width));
8122 *y = CLAMP (*y, root_y, (root_y + allocation.height));
8124 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
8125 gtk_menu_set_monitor (menu, monitor_num);
8126 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
8128 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
8129 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
8136 GtkTextView *text_view;
8142 range_contains_editable_text (const GtkTextIter *start,
8143 const GtkTextIter *end,
8144 gboolean default_editability)
8146 GtkTextIter iter = *start;
8148 while (gtk_text_iter_compare (&iter, end) < 0)
8150 if (gtk_text_iter_editable (&iter, default_editability))
8153 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
8160 unichar_chosen_func (const char *text,
8163 GtkTextView *text_view = GTK_TEXT_VIEW (data);
8165 gtk_text_view_commit_text (text_view, text);
8169 popup_targets_received (GtkClipboard *clipboard,
8170 GtkSelectionData *data,
8173 PopupInfo *info = user_data;
8174 GtkTextView *text_view;
8175 GtkTextViewPrivate *priv;
8177 text_view = info->text_view;
8178 priv = text_view->priv;
8180 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
8182 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
8183 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
8185 gboolean clipboard_contains_text;
8186 GtkWidget *menuitem;
8188 gboolean have_selection;
8189 gboolean can_insert;
8191 GtkTextIter sel_start, sel_end;
8192 gboolean show_input_method_menu;
8193 gboolean show_unicode_menu;
8195 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
8197 if (priv->popup_menu)
8198 gtk_widget_destroy (priv->popup_menu);
8200 priv->popup_menu = gtk_menu_new ();
8202 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
8203 GTK_WIDGET (text_view),
8206 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
8207 &sel_start, &sel_end);
8209 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
8211 gtk_text_buffer_get_insert (get_buffer (text_view)));
8213 can_insert = gtk_text_iter_can_insert (&iter, priv->editable);
8215 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
8217 range_contains_editable_text (&sel_start, &sel_end,
8219 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
8221 append_action_signal (text_view, priv->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
8222 can_insert && clipboard_contains_text);
8224 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
8225 gtk_widget_set_sensitive (menuitem,
8227 range_contains_editable_text (&sel_start, &sel_end,
8229 g_signal_connect_swapped (menuitem, "activate",
8230 G_CALLBACK (delete_cb), text_view);
8231 gtk_widget_show (menuitem);
8232 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8234 menuitem = gtk_separator_menu_item_new ();
8235 gtk_widget_show (menuitem);
8236 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8238 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
8239 g_signal_connect (menuitem, "activate",
8240 G_CALLBACK (select_all_cb), text_view);
8241 gtk_widget_show (menuitem);
8242 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8244 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
8245 "gtk-show-input-method-menu", &show_input_method_menu,
8246 "gtk-show-unicode-menu", &show_unicode_menu,
8249 if (show_input_method_menu || show_unicode_menu)
8251 menuitem = gtk_separator_menu_item_new ();
8252 gtk_widget_show (menuitem);
8253 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8256 if (show_input_method_menu)
8258 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
8259 gtk_widget_show (menuitem);
8260 gtk_widget_set_sensitive (menuitem, can_insert);
8262 submenu = gtk_menu_new ();
8263 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8264 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8266 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (priv->im_context),
8267 GTK_MENU_SHELL (submenu));
8270 if (show_unicode_menu)
8272 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
8273 gtk_widget_show (menuitem);
8274 gtk_widget_set_sensitive (menuitem, can_insert);
8276 submenu = gtk_menu_new ();
8277 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
8278 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
8280 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
8281 unichar_chosen_func,
8285 g_signal_emit (text_view,
8286 signals[POPULATE_POPUP],
8291 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
8293 info->button, info->time);
8296 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
8297 popup_position_func, text_view,
8298 0, gtk_get_current_event_time ());
8299 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
8303 g_object_unref (text_view);
8308 gtk_text_view_do_popup (GtkTextView *text_view,
8309 GdkEventButton *event)
8311 PopupInfo *info = g_new (PopupInfo, 1);
8313 /* In order to know what entries we should make sensitive, we
8314 * ask for the current targets of the clipboard, and when
8315 * we get them, then we actually pop up the menu.
8317 info->text_view = g_object_ref (text_view);
8321 info->button = event->button;
8322 info->time = event->time;
8327 info->time = gtk_get_current_event_time ();
8330 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
8331 GDK_SELECTION_CLIPBOARD),
8332 gdk_atom_intern_static_string ("TARGETS"),
8333 popup_targets_received,
8338 gtk_text_view_popup_menu (GtkWidget *widget)
8340 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
8344 /* Child GdkWindows */
8347 static GtkTextWindow*
8348 text_window_new (GtkTextWindowType type,
8351 gint height_request)
8355 win = g_new (GtkTextWindow, 1);
8358 win->widget = widget;
8360 win->bin_window = NULL;
8361 win->requisition.width = width_request;
8362 win->requisition.height = height_request;
8363 win->allocation.width = width_request;
8364 win->allocation.height = height_request;
8365 win->allocation.x = 0;
8366 win->allocation.y = 0;
8372 text_window_free (GtkTextWindow *win)
8375 text_window_unrealize (win);
8381 text_window_realize (GtkTextWindow *win,
8385 GdkWindowAttr attributes;
8386 gint attributes_mask;
8389 attributes.window_type = GDK_WINDOW_CHILD;
8390 attributes.x = win->allocation.x;
8391 attributes.y = win->allocation.y;
8392 attributes.width = win->allocation.width;
8393 attributes.height = win->allocation.height;
8394 attributes.wclass = GDK_INPUT_OUTPUT;
8395 attributes.visual = gtk_widget_get_visual (win->widget);
8396 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
8398 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
8400 window = gtk_widget_get_window (widget);
8402 win->window = gdk_window_new (window,
8403 &attributes, attributes_mask);
8405 gdk_window_show (win->window);
8406 gdk_window_set_user_data (win->window, win->widget);
8407 gdk_window_lower (win->window);
8411 attributes.width = win->allocation.width;
8412 attributes.height = win->allocation.height;
8413 attributes.event_mask = (GDK_EXPOSURE_MASK |
8415 GDK_KEY_PRESS_MASK |
8416 GDK_BUTTON_PRESS_MASK |
8417 GDK_BUTTON_RELEASE_MASK |
8418 GDK_POINTER_MOTION_MASK |
8419 GDK_POINTER_MOTION_HINT_MASK |
8420 gtk_widget_get_events (win->widget));
8422 win->bin_window = gdk_window_new (win->window,
8426 gdk_window_show (win->bin_window);
8427 gdk_window_set_user_data (win->bin_window, win->widget);
8429 if (win->type == GTK_TEXT_WINDOW_TEXT)
8431 if (gtk_widget_is_sensitive (widget))
8434 cursor = gdk_cursor_new_for_display (gdk_window_get_display (window),
8436 gdk_window_set_cursor (win->bin_window, cursor);
8437 gdk_cursor_unref (cursor);
8440 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->priv->im_context,
8444 gdk_window_set_background (win->bin_window,
8445 >k_widget_get_style (widget)->base[gtk_widget_get_state (widget)]);
8449 gdk_window_set_background (win->bin_window,
8450 >k_widget_get_style (widget)->bg[gtk_widget_get_state (widget)]);
8453 g_object_set_qdata (G_OBJECT (win->window),
8454 g_quark_from_static_string ("gtk-text-view-text-window"),
8457 g_object_set_qdata (G_OBJECT (win->bin_window),
8458 g_quark_from_static_string ("gtk-text-view-text-window"),
8463 text_window_unrealize (GtkTextWindow *win)
8465 if (win->type == GTK_TEXT_WINDOW_TEXT)
8467 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->priv->im_context,
8471 gdk_window_set_user_data (win->window, NULL);
8472 gdk_window_set_user_data (win->bin_window, NULL);
8473 gdk_window_destroy (win->bin_window);
8474 gdk_window_destroy (win->window);
8476 win->bin_window = NULL;
8480 text_window_size_allocate (GtkTextWindow *win,
8483 win->allocation = *rect;
8487 gdk_window_move_resize (win->window,
8489 rect->width, rect->height);
8491 gdk_window_resize (win->bin_window,
8492 rect->width, rect->height);
8497 text_window_scroll (GtkTextWindow *win,
8501 if (dx != 0 || dy != 0)
8503 gdk_window_scroll (win->bin_window, dx, dy);
8508 text_window_invalidate_rect (GtkTextWindow *win,
8511 GdkRectangle window_rect;
8513 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
8520 window_rect.width = rect->width;
8521 window_rect.height = rect->height;
8523 /* Adjust the rect as appropriate */
8527 case GTK_TEXT_WINDOW_TEXT:
8530 case GTK_TEXT_WINDOW_LEFT:
8531 case GTK_TEXT_WINDOW_RIGHT:
8533 window_rect.width = win->allocation.width;
8536 case GTK_TEXT_WINDOW_TOP:
8537 case GTK_TEXT_WINDOW_BOTTOM:
8539 window_rect.height = win->allocation.height;
8543 g_warning ("%s: bug!", G_STRFUNC);
8548 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
8552 cairo_t *cr = gdk_cairo_create (win->bin_window);
8553 gdk_cairo_rectangle (cr, &window_rect);
8554 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
8562 text_window_invalidate_cursors (GtkTextWindow *win)
8564 GtkTextView *text_view;
8565 GtkTextViewPrivate *priv;
8567 GdkRectangle strong;
8569 gboolean draw_arrow;
8570 gfloat cursor_aspect_ratio;
8574 text_view = GTK_TEXT_VIEW (win->widget);
8575 priv = text_view->priv;
8577 gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter,
8578 gtk_text_buffer_get_insert (priv->buffer));
8580 if (_gtk_text_layout_get_block_cursor (priv->layout, &strong))
8582 text_window_invalidate_rect (win, &strong);
8586 gtk_text_layout_get_cursor_locations (priv->layout, &iter,
8589 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
8590 * ignoring the text direction be exposing both sides of the cursor
8593 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
8595 gtk_widget_style_get (win->widget,
8596 "cursor-aspect-ratio", &cursor_aspect_ratio,
8599 stem_width = strong.height * cursor_aspect_ratio + 1;
8600 arrow_width = stem_width + 1;
8602 strong.width = stem_width;
8604 /* round up to the next even number */
8608 strong.x -= stem_width / 2;
8609 strong.width += stem_width;
8613 strong.x -= arrow_width;
8614 strong.width += arrow_width * 2;
8617 text_window_invalidate_rect (win, &strong);
8619 if (draw_arrow) /* == have weak */
8621 stem_width = weak.height * cursor_aspect_ratio + 1;
8622 arrow_width = stem_width + 1;
8624 weak.width = stem_width;
8626 /* round up to the next even number */
8630 weak.x -= stem_width / 2;
8631 weak.width += stem_width;
8633 weak.x -= arrow_width;
8634 weak.width += arrow_width * 2;
8636 text_window_invalidate_rect (win, &weak);
8641 text_window_get_width (GtkTextWindow *win)
8643 return win->allocation.width;
8647 text_window_get_height (GtkTextWindow *win)
8649 return win->allocation.height;
8656 * gtk_text_view_get_window:
8657 * @text_view: a #GtkTextView
8658 * @win: window to get
8660 * Retrieves the #GdkWindow corresponding to an area of the text view;
8661 * possible windows include the overall widget window, child windows
8662 * on the left, right, top, bottom, and the window that displays the
8663 * text buffer. Windows are %NULL and nonexistent if their width or
8664 * height is 0, and are nonexistent before the widget has been
8667 * Return value: (transfer none): a #GdkWindow, or %NULL
8670 gtk_text_view_get_window (GtkTextView *text_view,
8671 GtkTextWindowType win)
8673 GtkTextViewPrivate *priv = text_view->priv;
8675 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8679 case GTK_TEXT_WINDOW_WIDGET:
8680 return gtk_widget_get_window (GTK_WIDGET (text_view));
8683 case GTK_TEXT_WINDOW_TEXT:
8684 return priv->text_window->bin_window;
8687 case GTK_TEXT_WINDOW_LEFT:
8688 if (priv->left_window)
8689 return priv->left_window->bin_window;
8694 case GTK_TEXT_WINDOW_RIGHT:
8695 if (priv->right_window)
8696 return priv->right_window->bin_window;
8701 case GTK_TEXT_WINDOW_TOP:
8702 if (priv->top_window)
8703 return priv->top_window->bin_window;
8708 case GTK_TEXT_WINDOW_BOTTOM:
8709 if (priv->bottom_window)
8710 return priv->bottom_window->bin_window;
8715 case GTK_TEXT_WINDOW_PRIVATE:
8716 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8721 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8726 * gtk_text_view_get_window_type:
8727 * @text_view: a #GtkTextView
8728 * @window: a window type
8730 * Usually used to find out which window an event corresponds to.
8731 * If you connect to an event signal on @text_view, this function
8732 * should be called on <literal>event->window</literal> to
8733 * see which window it was.
8735 * Return value: the window type.
8738 gtk_text_view_get_window_type (GtkTextView *text_view,
8743 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8744 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8746 if (window == gtk_widget_get_window (GTK_WIDGET (text_view)))
8747 return GTK_TEXT_WINDOW_WIDGET;
8749 win = g_object_get_qdata (G_OBJECT (window),
8750 g_quark_try_string ("gtk-text-view-text-window"));
8756 return GTK_TEXT_WINDOW_PRIVATE;
8761 buffer_to_widget (GtkTextView *text_view,
8767 GtkTextViewPrivate *priv = text_view->priv;
8771 *window_x = buffer_x - priv->xoffset;
8772 *window_x += priv->text_window->allocation.x;
8777 *window_y = buffer_y - priv->yoffset;
8778 *window_y += priv->text_window->allocation.y;
8783 widget_to_text_window (GtkTextWindow *win,
8790 *window_x = widget_x - win->allocation.x;
8793 *window_y = widget_y - win->allocation.y;
8797 buffer_to_text_window (GtkTextView *text_view,
8806 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8807 "for a nonexistent or private child window of GtkTextView");
8811 buffer_to_widget (text_view,
8813 window_x, window_y);
8815 widget_to_text_window (win,
8816 window_x ? *window_x : 0,
8817 window_y ? *window_y : 0,
8823 * gtk_text_view_buffer_to_window_coords:
8824 * @text_view: a #GtkTextView
8825 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8826 * @buffer_x: buffer x coordinate
8827 * @buffer_y: buffer y coordinate
8828 * @window_x: (out) (allow-none): window x coordinate return location or %NULL
8829 * @window_y: (out) (allow-none): window y coordinate return location or %NULL
8831 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8832 * @win, and stores the result in (@window_x, @window_y).
8834 * Note that you can't convert coordinates for a nonexisting window (see
8835 * gtk_text_view_set_border_window_size()).
8838 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8839 GtkTextWindowType win,
8845 GtkTextViewPrivate *priv = text_view->priv;
8847 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8851 case GTK_TEXT_WINDOW_WIDGET:
8852 buffer_to_widget (text_view,
8854 window_x, window_y);
8857 case GTK_TEXT_WINDOW_TEXT:
8859 *window_x = buffer_x - priv->xoffset;
8861 *window_y = buffer_y - priv->yoffset;
8864 case GTK_TEXT_WINDOW_LEFT:
8865 buffer_to_text_window (text_view,
8868 window_x, window_y);
8871 case GTK_TEXT_WINDOW_RIGHT:
8872 buffer_to_text_window (text_view,
8875 window_x, window_y);
8878 case GTK_TEXT_WINDOW_TOP:
8879 buffer_to_text_window (text_view,
8882 window_x, window_y);
8885 case GTK_TEXT_WINDOW_BOTTOM:
8886 buffer_to_text_window (text_view,
8887 priv->bottom_window,
8889 window_x, window_y);
8892 case GTK_TEXT_WINDOW_PRIVATE:
8893 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8897 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8903 widget_to_buffer (GtkTextView *text_view,
8909 GtkTextViewPrivate *priv = text_view->priv;
8913 *buffer_x = widget_x + priv->xoffset;
8914 *buffer_x -= priv->text_window->allocation.x;
8919 *buffer_y = widget_y + priv->yoffset;
8920 *buffer_y -= priv->text_window->allocation.y;
8925 text_window_to_widget (GtkTextWindow *win,
8932 *widget_x = window_x + win->allocation.x;
8935 *widget_y = window_y + win->allocation.y;
8939 text_window_to_buffer (GtkTextView *text_view,
8948 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8949 "coordinates for a nonexistent child window.");
8953 text_window_to_widget (win,
8959 widget_to_buffer (text_view,
8960 buffer_x ? *buffer_x : 0,
8961 buffer_y ? *buffer_y : 0,
8967 * gtk_text_view_window_to_buffer_coords:
8968 * @text_view: a #GtkTextView
8969 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8970 * @window_x: window x coordinate
8971 * @window_y: window y coordinate
8972 * @buffer_x: (out) (allow-none): buffer x coordinate return location or %NULL
8973 * @buffer_y: (out) (allow-none): buffer y coordinate return location or %NULL
8975 * Converts coordinates on the window identified by @win to buffer
8976 * coordinates, storing the result in (@buffer_x,@buffer_y).
8978 * Note that you can't convert coordinates for a nonexisting window (see
8979 * gtk_text_view_set_border_window_size()).
8982 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8983 GtkTextWindowType win,
8989 GtkTextViewPrivate *priv = text_view->priv;
8991 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8995 case GTK_TEXT_WINDOW_WIDGET:
8996 widget_to_buffer (text_view,
8998 buffer_x, buffer_y);
9001 case GTK_TEXT_WINDOW_TEXT:
9003 *buffer_x = window_x + priv->xoffset;
9005 *buffer_y = window_y + priv->yoffset;
9008 case GTK_TEXT_WINDOW_LEFT:
9009 text_window_to_buffer (text_view,
9012 buffer_x, buffer_y);
9015 case GTK_TEXT_WINDOW_RIGHT:
9016 text_window_to_buffer (text_view,
9019 buffer_x, buffer_y);
9022 case GTK_TEXT_WINDOW_TOP:
9023 text_window_to_buffer (text_view,
9026 buffer_x, buffer_y);
9029 case GTK_TEXT_WINDOW_BOTTOM:
9030 text_window_to_buffer (text_view,
9031 priv->bottom_window,
9033 buffer_x, buffer_y);
9036 case GTK_TEXT_WINDOW_PRIVATE:
9037 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
9041 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
9047 set_window_width (GtkTextView *text_view,
9049 GtkTextWindowType type,
9050 GtkTextWindow **winp)
9056 text_window_free (*winp);
9058 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9065 *winp = text_window_new (type,
9066 GTK_WIDGET (text_view),
9068 /* if the widget is already realized we need to realize the child manually */
9069 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9070 text_window_realize (*winp, GTK_WIDGET (text_view));
9074 if ((*winp)->requisition.width == width)
9077 (*winp)->requisition.width = width;
9080 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9086 set_window_height (GtkTextView *text_view,
9088 GtkTextWindowType type,
9089 GtkTextWindow **winp)
9095 text_window_free (*winp);
9097 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9104 *winp = text_window_new (type,
9105 GTK_WIDGET (text_view),
9108 /* if the widget is already realized we need to realize the child manually */
9109 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9110 text_window_realize (*winp, GTK_WIDGET (text_view));
9114 if ((*winp)->requisition.height == height)
9117 (*winp)->requisition.height = height;
9120 gtk_widget_queue_resize (GTK_WIDGET (text_view));
9125 * gtk_text_view_set_border_window_size:
9126 * @text_view: a #GtkTextView
9127 * @type: window to affect
9128 * @size: width or height of the window
9130 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
9131 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
9132 * Automatically destroys the corresponding window if the size is set
9133 * to 0, and creates the window if the size is set to non-zero. This
9134 * function can only be used for the "border windows," it doesn't work
9135 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
9136 * #GTK_TEXT_WINDOW_PRIVATE.
9139 gtk_text_view_set_border_window_size (GtkTextView *text_view,
9140 GtkTextWindowType type,
9143 GtkTextViewPrivate *priv = text_view->priv;
9145 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9146 g_return_if_fail (size >= 0);
9150 case GTK_TEXT_WINDOW_LEFT:
9151 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
9152 &priv->left_window);
9155 case GTK_TEXT_WINDOW_RIGHT:
9156 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
9157 &priv->right_window);
9160 case GTK_TEXT_WINDOW_TOP:
9161 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
9165 case GTK_TEXT_WINDOW_BOTTOM:
9166 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
9167 &priv->bottom_window);
9171 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
9177 * gtk_text_view_get_border_window_size:
9178 * @text_view: a #GtkTextView
9179 * @type: window to return size from
9181 * Gets the width of the specified border window. See
9182 * gtk_text_view_set_border_window_size().
9184 * Return value: width of window
9187 gtk_text_view_get_border_window_size (GtkTextView *text_view,
9188 GtkTextWindowType type)
9190 GtkTextViewPrivate *priv = text_view->priv;
9192 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
9196 case GTK_TEXT_WINDOW_LEFT:
9197 if (priv->left_window)
9198 return priv->left_window->requisition.width;
9201 case GTK_TEXT_WINDOW_RIGHT:
9202 if (priv->right_window)
9203 return priv->right_window->requisition.width;
9206 case GTK_TEXT_WINDOW_TOP:
9207 if (priv->top_window)
9208 return priv->top_window->requisition.height;
9211 case GTK_TEXT_WINDOW_BOTTOM:
9212 if (priv->bottom_window)
9213 return priv->bottom_window->requisition.height;
9217 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
9228 static GtkTextViewChild*
9229 text_view_child_new_anchored (GtkWidget *child,
9230 GtkTextChildAnchor *anchor,
9231 GtkTextLayout *layout)
9233 GtkTextViewChild *vc;
9235 vc = g_new (GtkTextViewChild, 1);
9237 vc->type = GTK_TEXT_WINDOW_PRIVATE;
9239 vc->anchor = anchor;
9241 vc->from_top_of_line = 0;
9242 vc->from_left_of_buffer = 0;
9244 g_object_ref (vc->widget);
9245 g_object_ref (vc->anchor);
9247 g_object_set_data (G_OBJECT (child),
9248 I_("gtk-text-view-child"),
9251 gtk_text_child_anchor_register_child (anchor, child, layout);
9256 static GtkTextViewChild*
9257 text_view_child_new_window (GtkWidget *child,
9258 GtkTextWindowType type,
9262 GtkTextViewChild *vc;
9264 vc = g_new (GtkTextViewChild, 1);
9269 vc->from_top_of_line = 0;
9270 vc->from_left_of_buffer = 0;
9272 g_object_ref (vc->widget);
9278 g_object_set_data (G_OBJECT (child),
9279 I_("gtk-text-view-child"),
9286 text_view_child_free (GtkTextViewChild *child)
9288 g_object_set_data (G_OBJECT (child->widget),
9289 I_("gtk-text-view-child"), NULL);
9293 gtk_text_child_anchor_unregister_child (child->anchor,
9295 g_object_unref (child->anchor);
9298 g_object_unref (child->widget);
9304 text_view_child_set_parent_window (GtkTextView *text_view,
9305 GtkTextViewChild *vc)
9308 gtk_widget_set_parent_window (vc->widget,
9309 text_view->priv->text_window->bin_window);
9313 window = gtk_text_view_get_window (text_view,
9315 gtk_widget_set_parent_window (vc->widget, window);
9320 add_child (GtkTextView *text_view,
9321 GtkTextViewChild *vc)
9323 text_view->priv->children = g_slist_prepend (text_view->priv->children,
9326 if (gtk_widget_get_realized (GTK_WIDGET (text_view)))
9327 text_view_child_set_parent_window (text_view, vc);
9329 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
9333 * gtk_text_view_add_child_at_anchor:
9334 * @text_view: a #GtkTextView
9335 * @child: a #GtkWidget
9336 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
9338 * Adds a child widget in the text buffer, at the given @anchor.
9341 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
9343 GtkTextChildAnchor *anchor)
9345 GtkTextViewChild *vc;
9347 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9348 g_return_if_fail (GTK_IS_WIDGET (child));
9349 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
9350 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
9352 gtk_text_view_ensure_layout (text_view);
9354 vc = text_view_child_new_anchored (child, anchor,
9355 text_view->priv->layout);
9357 add_child (text_view, vc);
9359 g_assert (vc->widget == child);
9360 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9364 * gtk_text_view_add_child_in_window:
9365 * @text_view: a #GtkTextView
9366 * @child: a #GtkWidget
9367 * @which_window: which window the child should appear in
9368 * @xpos: X position of child in window coordinates
9369 * @ypos: Y position of child in window coordinates
9371 * Adds a child at fixed coordinates in one of the text widget's
9372 * windows. The window must have nonzero size (see
9373 * gtk_text_view_set_border_window_size()). Note that the child
9374 * coordinates are given relative to the #GdkWindow in question, and
9375 * that these coordinates have no sane relationship to scrolling. When
9376 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
9377 * irrelevant, the child floats above all scrollable areas. But when
9378 * placing a child in one of the scrollable windows (border windows or
9379 * text window), you'll need to compute the child's correct position
9380 * in buffer coordinates any time scrolling occurs or buffer changes
9381 * occur, and then call gtk_text_view_move_child() to update the
9382 * child's position. Unfortunately there's no good way to detect that
9383 * scrolling has occurred, using the current API; a possible hack
9384 * would be to update all child positions when the scroll adjustments
9385 * change or the text buffer changes. See bug 64518 on
9386 * bugzilla.gnome.org for status of fixing this issue.
9389 gtk_text_view_add_child_in_window (GtkTextView *text_view,
9391 GtkTextWindowType which_window,
9395 GtkTextViewChild *vc;
9397 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9398 g_return_if_fail (GTK_IS_WIDGET (child));
9399 g_return_if_fail (gtk_widget_get_parent (child) == NULL);
9401 vc = text_view_child_new_window (child, which_window,
9404 add_child (text_view, vc);
9406 g_assert (vc->widget == child);
9407 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9411 * gtk_text_view_move_child:
9412 * @text_view: a #GtkTextView
9413 * @child: child widget already added to the text view
9414 * @xpos: new X position in window coordinates
9415 * @ypos: new Y position in window coordinates
9417 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
9420 gtk_text_view_move_child (GtkTextView *text_view,
9425 GtkTextViewChild *vc;
9427 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9428 g_return_if_fail (GTK_IS_WIDGET (child));
9429 g_return_if_fail (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
9431 vc = g_object_get_data (G_OBJECT (child),
9432 "gtk-text-view-child");
9434 g_assert (vc != NULL);
9436 if (vc->x == xpos &&
9443 if (gtk_widget_get_visible (child) &&
9444 gtk_widget_get_visible (GTK_WIDGET (text_view)))
9445 gtk_widget_queue_resize (child);
9449 /* Iterator operations */
9452 * gtk_text_view_forward_display_line:
9453 * @text_view: a #GtkTextView
9454 * @iter: a #GtkTextIter
9456 * Moves the given @iter forward by one display (wrapped) line.
9457 * A display line is different from a paragraph. Paragraphs are
9458 * separated by newlines or other paragraph separator characters.
9459 * Display lines are created by line-wrapping a paragraph. If
9460 * wrapping is turned off, display lines and paragraphs will be the
9461 * same. Display lines are divided differently for each view, since
9462 * they depend on the view's width; paragraphs are the same in all
9463 * views, since they depend on the contents of the #GtkTextBuffer.
9465 * Return value: %TRUE if @iter was moved and is not on the end iterator
9468 gtk_text_view_forward_display_line (GtkTextView *text_view,
9471 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9472 g_return_val_if_fail (iter != NULL, FALSE);
9474 gtk_text_view_ensure_layout (text_view);
9476 return gtk_text_layout_move_iter_to_next_line (text_view->priv->layout, iter);
9480 * gtk_text_view_backward_display_line:
9481 * @text_view: a #GtkTextView
9482 * @iter: a #GtkTextIter
9484 * Moves the given @iter backward by one display (wrapped) line.
9485 * A display line is different from a paragraph. Paragraphs are
9486 * separated by newlines or other paragraph separator characters.
9487 * Display lines are created by line-wrapping a paragraph. If
9488 * wrapping is turned off, display lines and paragraphs will be the
9489 * same. Display lines are divided differently for each view, since
9490 * they depend on the view's width; paragraphs are the same in all
9491 * views, since they depend on the contents of the #GtkTextBuffer.
9493 * Return value: %TRUE if @iter was moved and is not on the end iterator
9496 gtk_text_view_backward_display_line (GtkTextView *text_view,
9499 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9500 g_return_val_if_fail (iter != NULL, FALSE);
9502 gtk_text_view_ensure_layout (text_view);
9504 return gtk_text_layout_move_iter_to_previous_line (text_view->priv->layout, iter);
9508 * gtk_text_view_forward_display_line_end:
9509 * @text_view: a #GtkTextView
9510 * @iter: a #GtkTextIter
9512 * Moves the given @iter forward to the next display line end.
9513 * A display line is different from a paragraph. Paragraphs are
9514 * separated by newlines or other paragraph separator characters.
9515 * Display lines are created by line-wrapping a paragraph. If
9516 * wrapping is turned off, display lines and paragraphs will be the
9517 * same. Display lines are divided differently for each view, since
9518 * they depend on the view's width; paragraphs are the same in all
9519 * views, since they depend on the contents of the #GtkTextBuffer.
9521 * Return value: %TRUE if @iter was moved and is not on the end iterator
9524 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
9527 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9528 g_return_val_if_fail (iter != NULL, FALSE);
9530 gtk_text_view_ensure_layout (text_view);
9532 return gtk_text_layout_move_iter_to_line_end (text_view->priv->layout, iter, 1);
9536 * gtk_text_view_backward_display_line_start:
9537 * @text_view: a #GtkTextView
9538 * @iter: a #GtkTextIter
9540 * Moves the given @iter backward to the next display line start.
9541 * A display line is different from a paragraph. Paragraphs are
9542 * separated by newlines or other paragraph separator characters.
9543 * Display lines are created by line-wrapping a paragraph. If
9544 * wrapping is turned off, display lines and paragraphs will be the
9545 * same. Display lines are divided differently for each view, since
9546 * they depend on the view's width; paragraphs are the same in all
9547 * views, since they depend on the contents of the #GtkTextBuffer.
9549 * Return value: %TRUE if @iter was moved and is not on the end iterator
9552 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
9555 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9556 g_return_val_if_fail (iter != NULL, FALSE);
9558 gtk_text_view_ensure_layout (text_view);
9560 return gtk_text_layout_move_iter_to_line_end (text_view->priv->layout, iter, -1);
9564 * gtk_text_view_starts_display_line:
9565 * @text_view: a #GtkTextView
9566 * @iter: a #GtkTextIter
9568 * Determines whether @iter is at the start of a display line.
9569 * See gtk_text_view_forward_display_line() for an explanation of
9570 * display lines vs. paragraphs.
9572 * Return value: %TRUE if @iter begins a wrapped line
9575 gtk_text_view_starts_display_line (GtkTextView *text_view,
9576 const GtkTextIter *iter)
9578 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9579 g_return_val_if_fail (iter != NULL, FALSE);
9581 gtk_text_view_ensure_layout (text_view);
9583 return gtk_text_layout_iter_starts_line (text_view->priv->layout, iter);
9587 * gtk_text_view_move_visually:
9588 * @text_view: a #GtkTextView
9589 * @iter: a #GtkTextIter
9590 * @count: number of characters to move (negative moves left,
9591 * positive moves right)
9593 * Move the iterator a given number of characters visually, treating
9594 * it as the strong cursor position. If @count is positive, then the
9595 * new strong cursor position will be @count positions to the right of
9596 * the old cursor position. If @count is negative then the new strong
9597 * cursor position will be @count positions to the left of the old
9600 * In the presence of bi-directional text, the correspondence
9601 * between logical and visual order will depend on the direction
9602 * of the current run, and there may be jumps when the cursor
9603 * is moved off of the end of a run.
9605 * Return value: %TRUE if @iter moved and is not on the end iterator
9608 gtk_text_view_move_visually (GtkTextView *text_view,
9612 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9613 g_return_val_if_fail (iter != NULL, FALSE);
9615 gtk_text_view_ensure_layout (text_view);
9617 return gtk_text_layout_move_iter_visually (text_view->priv->layout, iter, count);