1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
32 #include "gtkbindings.h"
34 #include "gtkimagemenuitem.h"
37 #include "gtkmarshalers.h"
39 #include "gtkmenuitem.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtksettings.h"
43 #include "gtktextbufferrichtext.h"
44 #include "gtktextdisplay.h"
45 #include "gtktextview.h"
46 #include "gtkimmulticontext.h"
47 #include "gdk/gdkkeysyms.h"
48 #include "gtkprivate.h"
49 #include "gtktextutil.h"
50 #include "gtkwindow.h"
53 /* How scrolling, validation, exposes, etc. work.
55 * The expose_event handler has the invariant that the onscreen lines
56 * have been validated.
58 * There are two ways that onscreen lines can become invalid. The first
59 * is to change which lines are onscreen. This happens when the value
60 * of a scroll adjustment changes. So the code path begins in
61 * gtk_text_view_value_changed() and goes like this:
62 * - gdk_window_scroll() to reflect the new adjustment value
63 * - validate the lines that were moved onscreen
64 * - gdk_window_process_updates() to handle the exposes immediately
66 * The second way is that you get the "invalidated" signal from the layout,
67 * indicating that lines have become invalid. This code path begins in
68 * invalidated_handler() and goes like this:
69 * - install high-priority idle which does the rest of the steps
70 * - if a scroll is pending from scroll_to_mark(), do the scroll,
71 * jumping to the gtk_text_view_value_changed() code path
72 * - otherwise, validate the onscreen lines
73 * - DO NOT process updates
75 * In both cases, validating the onscreen lines can trigger a scroll
76 * due to maintaining the first_para on the top of the screen.
77 * If validation triggers a scroll, we jump to the top of the code path
78 * for value_changed, and bail out of the current code path.
80 * Also, in size_allocate, if we invalidate some lines from changing
81 * the layout width, we need to go ahead and run the high-priority idle,
82 * because GTK sends exposes right after doing the size allocates without
83 * returning to the main loop. This is also why the high-priority idle
84 * is at a higher priority than resizing.
89 #define DEBUG_VALIDATION_AND_SCROLLING
92 #ifdef DEBUG_VALIDATION_AND_SCROLLING
98 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
99 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
101 #define SPACE_FOR_CURSOR 1
103 typedef struct _GtkTextViewPrivate GtkTextViewPrivate;
105 #define GTK_TEXT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_VIEW, GtkTextViewPrivate))
107 struct _GtkTextViewPrivate
109 guint blink_time; /* time in msec the cursor has blinked since last user event */
115 struct _GtkTextPendingScroll
118 gdouble within_margin;
126 SET_SCROLL_ADJUSTMENTS,
140 TOGGLE_CURSOR_VISIBLE,
147 PROP_PIXELS_ABOVE_LINES,
148 PROP_PIXELS_BELOW_LINES,
149 PROP_PIXELS_INSIDE_WRAP,
164 static void gtk_text_view_destroy (GtkObject *object);
165 static void gtk_text_view_finalize (GObject *object);
166 static void gtk_text_view_set_property (GObject *object,
170 static void gtk_text_view_get_property (GObject *object,
174 static void gtk_text_view_size_request (GtkWidget *widget,
175 GtkRequisition *requisition);
176 static void gtk_text_view_size_allocate (GtkWidget *widget,
177 GtkAllocation *allocation);
178 static void gtk_text_view_realize (GtkWidget *widget);
179 static void gtk_text_view_unrealize (GtkWidget *widget);
180 static void gtk_text_view_style_set (GtkWidget *widget,
181 GtkStyle *previous_style);
182 static void gtk_text_view_direction_changed (GtkWidget *widget,
183 GtkTextDirection previous_direction);
184 static void gtk_text_view_grab_notify (GtkWidget *widget,
185 gboolean was_grabbed);
186 static void gtk_text_view_state_changed (GtkWidget *widget,
187 GtkStateType previous_state);
189 static gint gtk_text_view_event (GtkWidget *widget,
191 static gint gtk_text_view_key_press_event (GtkWidget *widget,
193 static gint gtk_text_view_key_release_event (GtkWidget *widget,
195 static gint gtk_text_view_button_press_event (GtkWidget *widget,
196 GdkEventButton *event);
197 static gint gtk_text_view_button_release_event (GtkWidget *widget,
198 GdkEventButton *event);
199 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
200 GdkEventFocus *event);
201 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
202 GdkEventFocus *event);
203 static gint gtk_text_view_motion_event (GtkWidget *widget,
204 GdkEventMotion *event);
205 static gint gtk_text_view_expose_event (GtkWidget *widget,
206 GdkEventExpose *expose);
207 static void gtk_text_view_draw_focus (GtkWidget *widget);
208 static gboolean gtk_text_view_focus (GtkWidget *widget,
209 GtkDirectionType direction);
210 static void gtk_text_view_move_focus (GtkWidget *widget,
211 GtkDirectionType direction_type);
212 static void gtk_text_view_select_all (GtkWidget *widget,
216 /* Source side drag signals */
217 static void gtk_text_view_drag_begin (GtkWidget *widget,
218 GdkDragContext *context);
219 static void gtk_text_view_drag_end (GtkWidget *widget,
220 GdkDragContext *context);
221 static void gtk_text_view_drag_data_get (GtkWidget *widget,
222 GdkDragContext *context,
223 GtkSelectionData *selection_data,
226 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
227 GdkDragContext *context);
229 /* Target side drag signals */
230 static void gtk_text_view_drag_leave (GtkWidget *widget,
231 GdkDragContext *context,
233 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
234 GdkDragContext *context,
238 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
239 GdkDragContext *context,
243 static void gtk_text_view_drag_data_received (GtkWidget *widget,
244 GdkDragContext *context,
247 GtkSelectionData *selection_data,
251 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
253 GtkAdjustment *vadj);
254 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
256 static void gtk_text_view_move_cursor (GtkTextView *text_view,
257 GtkMovementStep step,
259 gboolean extend_selection);
260 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
262 gboolean extend_selection);
263 static gboolean gtk_text_view_move_viewport (GtkTextView *text_view,
266 static void gtk_text_view_set_anchor (GtkTextView *text_view);
267 static gboolean gtk_text_view_scroll_pages (GtkTextView *text_view,
269 gboolean extend_selection);
270 static gboolean gtk_text_view_scroll_hpages(GtkTextView *text_view,
272 gboolean extend_selection);
273 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
275 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
278 static void gtk_text_view_backspace (GtkTextView *text_view);
279 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
280 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
281 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
282 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
283 static void gtk_text_view_toggle_cursor_visible (GtkTextView *text_view);
284 static void gtk_text_view_compat_move_focus(GtkTextView *text_view,
285 GtkDirectionType direction_type);
286 static void gtk_text_view_unselect (GtkTextView *text_view);
288 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
289 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
291 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
292 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
293 GtkTextAttributes *values,
295 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
296 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
297 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
298 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
299 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
300 const GtkTextIter *iter,
301 GdkEventButton *event);
302 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view);
303 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
304 const GtkTextIter *iter,
305 GdkEventMotion *event);
306 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
307 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
308 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
309 static void gtk_text_view_reset_blink_time (GtkTextView *text_view);
311 static void gtk_text_view_value_changed (GtkAdjustment *adj,
313 static void gtk_text_view_commit_handler (GtkIMContext *context,
315 GtkTextView *text_view);
316 static void gtk_text_view_commit_text (GtkTextView *text_view,
318 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
319 GtkTextView *text_view);
320 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
321 GtkTextView *text_view);
322 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
325 GtkTextView *text_view);
327 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
328 const GtkTextIter *location,
331 static void gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
332 const GParamSpec *pspec,
334 static void gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
335 GtkClipboard *clipboard,
337 static void gtk_text_view_get_cursor_location (GtkTextView *text_view,
339 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
342 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
346 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
347 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
349 static void gtk_text_view_do_popup (GtkTextView *text_view,
350 GdkEventButton *event);
352 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
354 gdouble within_margin,
359 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
360 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
361 static void gtk_text_view_invalidate (GtkTextView *text_view);
362 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
364 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
366 /* Container methods */
367 static void gtk_text_view_add (GtkContainer *container,
369 static void gtk_text_view_remove (GtkContainer *container,
371 static void gtk_text_view_forall (GtkContainer *container,
372 gboolean include_internals,
373 GtkCallback callback,
374 gpointer callback_data);
376 /* FIXME probably need the focus methods. */
378 typedef struct _GtkTextViewChild GtkTextViewChild;
380 struct _GtkTextViewChild
384 GtkTextChildAnchor *anchor;
386 gint from_top_of_line;
387 gint from_left_of_buffer;
389 /* These are ignored if anchor != NULL */
390 GtkTextWindowType type;
395 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
396 GtkTextChildAnchor *anchor,
397 GtkTextLayout *layout);
398 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
399 GtkTextWindowType type,
402 static void text_view_child_free (GtkTextViewChild *child);
403 static void text_view_child_set_parent_window (GtkTextView *text_view,
404 GtkTextViewChild *child);
406 struct _GtkTextWindow
408 GtkTextWindowType type;
411 GdkWindow *bin_window;
412 GtkRequisition requisition;
413 GdkRectangle allocation;
416 static GtkTextWindow *text_window_new (GtkTextWindowType type,
419 gint height_request);
420 static void text_window_free (GtkTextWindow *win);
421 static void text_window_realize (GtkTextWindow *win,
423 static void text_window_unrealize (GtkTextWindow *win);
424 static void text_window_size_allocate (GtkTextWindow *win,
426 static void text_window_scroll (GtkTextWindow *win,
429 static void text_window_invalidate_rect (GtkTextWindow *win,
431 static void text_window_invalidate_cursors (GtkTextWindow *win);
433 static gint text_window_get_width (GtkTextWindow *win);
434 static gint text_window_get_height (GtkTextWindow *win);
437 static guint signals[LAST_SIGNAL] = { 0 };
439 G_DEFINE_TYPE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER)
442 add_move_binding (GtkBindingSet *binding_set,
445 GtkMovementStep step,
448 g_assert ((modmask & GDK_SHIFT_MASK) == 0);
450 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
454 G_TYPE_BOOLEAN, FALSE);
456 /* Selection-extending version */
457 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
461 G_TYPE_BOOLEAN, TRUE);
465 gtk_text_view_class_init (GtkTextViewClass *klass)
467 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
468 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
469 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
470 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
471 GtkBindingSet *binding_set;
473 /* Default handlers and virtual methods
475 gobject_class->set_property = gtk_text_view_set_property;
476 gobject_class->get_property = gtk_text_view_get_property;
478 object_class->destroy = gtk_text_view_destroy;
479 gobject_class->finalize = gtk_text_view_finalize;
481 widget_class->realize = gtk_text_view_realize;
482 widget_class->unrealize = gtk_text_view_unrealize;
483 widget_class->style_set = gtk_text_view_style_set;
484 widget_class->direction_changed = gtk_text_view_direction_changed;
485 widget_class->grab_notify = gtk_text_view_grab_notify;
486 widget_class->state_changed = gtk_text_view_state_changed;
487 widget_class->size_request = gtk_text_view_size_request;
488 widget_class->size_allocate = gtk_text_view_size_allocate;
489 widget_class->event = gtk_text_view_event;
490 widget_class->key_press_event = gtk_text_view_key_press_event;
491 widget_class->key_release_event = gtk_text_view_key_release_event;
492 widget_class->button_press_event = gtk_text_view_button_press_event;
493 widget_class->button_release_event = gtk_text_view_button_release_event;
494 widget_class->focus_in_event = gtk_text_view_focus_in_event;
495 widget_class->focus_out_event = gtk_text_view_focus_out_event;
496 widget_class->motion_notify_event = gtk_text_view_motion_event;
497 widget_class->expose_event = gtk_text_view_expose_event;
498 widget_class->focus = gtk_text_view_focus;
500 /* need to override the base class function via override_class_handler,
501 * because the signal slot is not available in GtkWidgetCLass
503 g_signal_override_class_handler ("move-focus",
505 G_CALLBACK (gtk_text_view_move_focus));
507 widget_class->drag_begin = gtk_text_view_drag_begin;
508 widget_class->drag_end = gtk_text_view_drag_end;
509 widget_class->drag_data_get = gtk_text_view_drag_data_get;
510 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
512 widget_class->drag_leave = gtk_text_view_drag_leave;
513 widget_class->drag_motion = gtk_text_view_drag_motion;
514 widget_class->drag_drop = gtk_text_view_drag_drop;
515 widget_class->drag_data_received = gtk_text_view_drag_data_received;
517 widget_class->popup_menu = gtk_text_view_popup_menu;
519 container_class->add = gtk_text_view_add;
520 container_class->remove = gtk_text_view_remove;
521 container_class->forall = gtk_text_view_forall;
523 klass->move_cursor = gtk_text_view_move_cursor;
524 klass->page_horizontally = gtk_text_view_page_horizontally;
525 klass->set_anchor = gtk_text_view_set_anchor;
526 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
527 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
528 klass->backspace = gtk_text_view_backspace;
529 klass->cut_clipboard = gtk_text_view_cut_clipboard;
530 klass->copy_clipboard = gtk_text_view_copy_clipboard;
531 klass->paste_clipboard = gtk_text_view_paste_clipboard;
532 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
533 klass->move_focus = gtk_text_view_compat_move_focus;
534 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
540 g_object_class_install_property (gobject_class,
541 PROP_PIXELS_ABOVE_LINES,
542 g_param_spec_int ("pixels-above-lines",
543 P_("Pixels Above Lines"),
544 P_("Pixels of blank space above paragraphs"),
548 GTK_PARAM_READWRITE));
550 g_object_class_install_property (gobject_class,
551 PROP_PIXELS_BELOW_LINES,
552 g_param_spec_int ("pixels-below-lines",
553 P_("Pixels Below Lines"),
554 P_("Pixels of blank space below paragraphs"),
558 GTK_PARAM_READWRITE));
560 g_object_class_install_property (gobject_class,
561 PROP_PIXELS_INSIDE_WRAP,
562 g_param_spec_int ("pixels-inside-wrap",
563 P_("Pixels Inside Wrap"),
564 P_("Pixels of blank space between wrapped lines in a paragraph"),
568 GTK_PARAM_READWRITE));
570 g_object_class_install_property (gobject_class,
572 g_param_spec_boolean ("editable",
574 P_("Whether the text can be modified by the user"),
576 GTK_PARAM_READWRITE));
578 g_object_class_install_property (gobject_class,
580 g_param_spec_enum ("wrap-mode",
582 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
585 GTK_PARAM_READWRITE));
587 g_object_class_install_property (gobject_class,
589 g_param_spec_enum ("justification",
591 P_("Left, right, or center justification"),
592 GTK_TYPE_JUSTIFICATION,
594 GTK_PARAM_READWRITE));
596 g_object_class_install_property (gobject_class,
598 g_param_spec_int ("left-margin",
600 P_("Width of the left margin in pixels"),
604 GTK_PARAM_READWRITE));
606 g_object_class_install_property (gobject_class,
608 g_param_spec_int ("right-margin",
610 P_("Width of the right margin in pixels"),
614 GTK_PARAM_READWRITE));
616 g_object_class_install_property (gobject_class,
618 g_param_spec_int ("indent",
620 P_("Amount to indent the paragraph, in pixels"),
624 GTK_PARAM_READWRITE));
626 g_object_class_install_property (gobject_class,
628 g_param_spec_boxed ("tabs",
630 P_("Custom tabs for this text"),
631 PANGO_TYPE_TAB_ARRAY,
632 GTK_PARAM_READWRITE));
634 g_object_class_install_property (gobject_class,
636 g_param_spec_boolean ("cursor-visible",
637 P_("Cursor Visible"),
638 P_("If the insertion cursor is shown"),
640 GTK_PARAM_READWRITE));
642 g_object_class_install_property (gobject_class,
644 g_param_spec_object ("buffer",
646 P_("The buffer which is displayed"),
647 GTK_TYPE_TEXT_BUFFER,
648 GTK_PARAM_READWRITE));
650 g_object_class_install_property (gobject_class,
652 g_param_spec_boolean ("overwrite",
653 P_("Overwrite mode"),
654 P_("Whether entered text overwrites existing contents"),
656 GTK_PARAM_READWRITE));
658 g_object_class_install_property (gobject_class,
660 g_param_spec_boolean ("accepts-tab",
662 P_("Whether Tab will result in a tab character being entered"),
664 GTK_PARAM_READWRITE));
667 * GtkTextView:im-module:
669 * Which IM (input method) module should be used for this entry.
672 * Setting this to a non-%NULL value overrides the
673 * system-wide IM module setting. See the GtkSettings
674 * #GtkSettings:gtk-im-module property.
678 g_object_class_install_property (gobject_class,
680 g_param_spec_string ("im-module",
682 P_("Which IM module should be used"),
684 GTK_PARAM_READWRITE));
689 gtk_widget_class_install_style_property (widget_class,
690 g_param_spec_boxed ("error-underline-color",
691 P_("Error underline color"),
692 P_("Color with which to draw error-indication underlines"),
694 GTK_PARAM_READABLE));
701 * GtkTextView::move-cursor:
702 * @text_view: the object which received the signal
703 * @step: the granularity of the move, as a #GtkMovementStep
704 * @count: the number of @step units to move
705 * @extend_selection: %TRUE if the move should extend the selection
707 * The ::move-cursor signal is a
708 * <link linkend="keybinding-signals">keybinding signal</link>
709 * which gets emitted when the user initiates a cursor movement.
710 * If the cursor is not visible in @text_view, this signal causes
711 * the viewport to be moved instead.
713 * Applications should not connect to it, but may emit it with
714 * g_signal_emit_by_name() if they need to control the cursor
717 * The default bindings for this signal come in two variants,
718 * the variant with the Shift modifier extends the selection,
719 * the variant without the Shift modifer does not.
720 * There are too many key combinations to list them all here.
722 * <listitem>Arrow keys move by individual characters/lines</listitem>
723 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
724 * <listitem>Home/End keys move to the ends of the buffer</listitem>
725 * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
726 * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
729 signals[MOVE_CURSOR] =
730 g_signal_new (I_("move-cursor"),
731 G_OBJECT_CLASS_TYPE (gobject_class),
732 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
733 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
735 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
737 GTK_TYPE_MOVEMENT_STEP,
742 * GtkTextView::page-horizontally:
743 * @text_view: the object which received the signal
744 * @count: the number of @step units to move
745 * @extend_selection: %TRUE if the move should extend the selection
747 * The ::page-horizontally signal is a
748 * <link linkend="keybinding-signals">keybinding signal</link>
749 * which can be bound to key combinations to allow the user
750 * to initiate horizontal cursor movement by pages.
752 * This signal should not be used anymore, instead use the
753 * #GtkTextview::move-cursor signal with the #GTK_MOVEMENT_HORIZONTAL_PAGES
756 signals[PAGE_HORIZONTALLY] =
757 g_signal_new (I_("page-horizontally"),
758 G_OBJECT_CLASS_TYPE (gobject_class),
759 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
760 G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
762 _gtk_marshal_VOID__INT_BOOLEAN,
768 * GtkTextView::move-viewport:
769 * @text_view: the object which received the signal
770 * @step: the granularity of the move, as a #GtkMovementStep
771 * @count: the number of @step units to move
773 * The ::move-viewport signal is a
774 * <link linkend="keybinding-signals">keybinding signal</link>
775 * which can be bound to key combinations to allow the user
776 * to move the viewport, i.e. change what part of the text view
777 * is visible in a containing scrolled window.
779 * There are no default bindings for this signal.
781 signals[MOVE_VIEWPORT] =
782 g_signal_new_class_handler (I_("move-viewport"),
783 G_OBJECT_CLASS_TYPE (gobject_class),
784 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
785 G_CALLBACK (gtk_text_view_move_viewport),
787 _gtk_marshal_VOID__ENUM_INT,
789 GTK_TYPE_SCROLL_STEP,
793 * GtkTextView::set-anchor:
794 * @text_view: the object which received the signal
796 * The ::set-anchor signal is a
797 * <link linkend="keybinding-signals">keybinding signal</link>
798 * which gets emitted when the user initiates setting the "anchor"
799 * mark. The "anchor" mark gets placed at the same position as the
802 * This signal has no default bindings.
804 signals[SET_ANCHOR] =
805 g_signal_new (I_("set-anchor"),
806 G_OBJECT_CLASS_TYPE (gobject_class),
807 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
808 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
810 _gtk_marshal_VOID__VOID,
814 * GtkTextView::insert-at-cursor:
815 * @text_view: the object which received the signal
816 * @string: the string to insert
818 * The ::insert-at-cursor signal is a
819 * <link linkend="keybinding-signals">keybinding signal</link>
820 * which gets emitted when the user initiates the insertion of a
821 * fixed string at the cursor.
823 * This signal has no default bindings.
825 signals[INSERT_AT_CURSOR] =
826 g_signal_new (I_("insert-at-cursor"),
827 G_OBJECT_CLASS_TYPE (gobject_class),
828 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
829 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
831 _gtk_marshal_VOID__STRING,
836 * GtkTextView::delete-from-cursor:
837 * @text_view: the object which received the signal
838 * @type: the granularity of the deletion, as a #GtkDeleteType
839 * @count: the number of @type units to delete
841 * The ::delete-from-cursor signal is a
842 * <link linkend="keybinding-signals">keybinding signal</link>
843 * which gets emitted when the user initiates a text deletion.
845 * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
846 * if there is one, otherwise it deletes the requested number
849 * The default bindings for this signal are
850 * Delete for deleting a character, Ctrl-Delete for
851 * deleting a word and Ctrl-Backspace for deleting a word
854 signals[DELETE_FROM_CURSOR] =
855 g_signal_new (I_("delete-from-cursor"),
856 G_OBJECT_CLASS_TYPE (gobject_class),
857 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
858 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
860 _gtk_marshal_VOID__ENUM_INT,
862 GTK_TYPE_DELETE_TYPE,
866 * GtkTextView::backspace:
867 * @text_view: the object which received the signal
869 * The ::backspace signal is a
870 * <link linkend="keybinding-signals">keybinding signal</link>
871 * which gets emitted when the user asks for it.
873 * The default bindings for this signal are
874 * Backspace and Shift-Backspace.
877 g_signal_new (I_("backspace"),
878 G_OBJECT_CLASS_TYPE (gobject_class),
879 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
880 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
882 _gtk_marshal_VOID__VOID,
886 * GtkTextView::cut-clipboard:
887 * @text_view: the object which received the signal
889 * The ::cut-clipboard signal is a
890 * <link linkend="keybinding-signals">keybinding signal</link>
891 * which gets emitted to cut the selection to the clipboard.
893 * The default bindings for this signal are
894 * Ctrl-x and Shift-Delete.
896 signals[CUT_CLIPBOARD] =
897 g_signal_new (I_("cut-clipboard"),
898 G_OBJECT_CLASS_TYPE (gobject_class),
899 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
900 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
902 _gtk_marshal_VOID__VOID,
906 * GtkTextView::copy-clipboard:
907 * @text_view: the object which received the signal
909 * The ::copy-clipboard signal is a
910 * <link linkend="keybinding-signals">keybinding signal</link>
911 * which gets emitted to copy the selection to the clipboard.
913 * The default bindings for this signal are
914 * Ctrl-c and Ctrl-Insert.
916 signals[COPY_CLIPBOARD] =
917 g_signal_new (I_("copy-clipboard"),
918 G_OBJECT_CLASS_TYPE (gobject_class),
919 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
920 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
922 _gtk_marshal_VOID__VOID,
926 * GtkTextView::paste-clipboard:
927 * @text_view: the object which received the signal
929 * The ::paste-clipboard signal is a
930 * <link linkend="keybinding-signals">keybinding signal</link>
931 * which gets emitted to paste the contents of the clipboard
932 * into the text view.
934 * The default bindings for this signal are
935 * Ctrl-v and Shift-Insert.
937 signals[PASTE_CLIPBOARD] =
938 g_signal_new (I_("paste-clipboard"),
939 G_OBJECT_CLASS_TYPE (gobject_class),
940 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
941 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
943 _gtk_marshal_VOID__VOID,
947 * GtkTextView::toggle-overwrite:
948 * @text_view: the object which received the signal
950 * The ::toggle-overwrite signal is a
951 * <link linkend="keybinding-signals">keybinding signal</link>
952 * which gets emitted to toggle the overwrite mode of the text view.
954 * The default bindings for this signal is Insert.
956 signals[TOGGLE_OVERWRITE] =
957 g_signal_new (I_("toggle-overwrite"),
958 G_OBJECT_CLASS_TYPE (gobject_class),
959 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
960 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
962 _gtk_marshal_VOID__VOID,
966 * GtkTextView::set-scroll-adjustments
967 * @horizontal: the horizontal #GtkAdjustment
968 * @vertical: the vertical #GtkAdjustment
970 * Set the scroll adjustments for the text view. Usually scrolled containers
971 * like #GtkScrolledWindow will emit this signal to connect two instances
972 * of #GtkScrollbar to the scroll directions of the #GtkTextView.
974 signals[SET_SCROLL_ADJUSTMENTS] =
975 g_signal_new (I_("set-scroll-adjustments"),
976 G_OBJECT_CLASS_TYPE (gobject_class),
977 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
978 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
980 _gtk_marshal_VOID__OBJECT_OBJECT,
983 GTK_TYPE_ADJUSTMENT);
984 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
987 * GtkTextView::populate-popup:
988 * @entry: The text view on which the signal is emitted
989 * @menu: the menu that is being populated
991 * The ::populate-popup signal gets emitted before showing the
992 * context menu of the text view.
994 * If you need to add items to the context menu, connect
995 * to this signal and append your menuitems to the @menu.
997 signals[POPULATE_POPUP] =
998 g_signal_new (I_("populate-popup"),
999 G_OBJECT_CLASS_TYPE (gobject_class),
1001 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
1003 _gtk_marshal_VOID__OBJECT,
1008 * GtkTextView::select-all:
1009 * @text_view: the object which received the signal
1010 * @select: %TRUE to select, %FALSE to unselect
1012 * The ::select-all signal is a
1013 * <link linkend="keybinding-signals">keybinding signal</link>
1014 * which gets emitted to select or unselect the complete
1015 * contents of the text view.
1017 * The default bindings for this signal are Ctrl-a and Ctrl-/
1018 * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
1020 signals[SELECT_ALL] =
1021 g_signal_new_class_handler (I_("select-all"),
1022 G_OBJECT_CLASS_TYPE (object_class),
1023 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1024 G_CALLBACK (gtk_text_view_select_all),
1026 _gtk_marshal_VOID__BOOLEAN,
1027 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
1030 * GtkTextView::toggle-cursor-visible:
1031 * @text_view: the object which received the signal
1033 * The ::toggle-cursor-visible signal is a
1034 * <link linkend="keybinding-signals">keybinding signal</link>
1035 * which gets emitted to toggle the visibility of the cursor.
1037 * The default binding for this signal is F7.
1039 signals[TOGGLE_CURSOR_VISIBLE] =
1040 g_signal_new_class_handler (I_("toggle-cursor-visible"),
1041 G_OBJECT_CLASS_TYPE (object_class),
1042 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1043 G_CALLBACK (gtk_text_view_toggle_cursor_visible),
1045 _gtk_marshal_VOID__VOID,
1052 binding_set = gtk_binding_set_by_class (klass);
1054 /* Moving the insertion point */
1055 add_move_binding (binding_set, GDK_Right, 0,
1056 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1058 add_move_binding (binding_set, GDK_KP_Right, 0,
1059 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1061 add_move_binding (binding_set, GDK_Left, 0,
1062 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1064 add_move_binding (binding_set, GDK_KP_Left, 0,
1065 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1067 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
1068 GTK_MOVEMENT_WORDS, 1);
1070 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
1071 GTK_MOVEMENT_WORDS, 1);
1073 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
1074 GTK_MOVEMENT_WORDS, -1);
1076 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
1077 GTK_MOVEMENT_WORDS, -1);
1079 add_move_binding (binding_set, GDK_Up, 0,
1080 GTK_MOVEMENT_DISPLAY_LINES, -1);
1082 add_move_binding (binding_set, GDK_KP_Up, 0,
1083 GTK_MOVEMENT_DISPLAY_LINES, -1);
1085 add_move_binding (binding_set, GDK_Down, 0,
1086 GTK_MOVEMENT_DISPLAY_LINES, 1);
1088 add_move_binding (binding_set, GDK_KP_Down, 0,
1089 GTK_MOVEMENT_DISPLAY_LINES, 1);
1091 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
1092 GTK_MOVEMENT_PARAGRAPHS, -1);
1094 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
1095 GTK_MOVEMENT_PARAGRAPHS, -1);
1097 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
1098 GTK_MOVEMENT_PARAGRAPHS, 1);
1100 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
1101 GTK_MOVEMENT_PARAGRAPHS, 1);
1103 add_move_binding (binding_set, GDK_Home, 0,
1104 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1106 add_move_binding (binding_set, GDK_KP_Home, 0,
1107 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1109 add_move_binding (binding_set, GDK_End, 0,
1110 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1112 add_move_binding (binding_set, GDK_KP_End, 0,
1113 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1115 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
1116 GTK_MOVEMENT_BUFFER_ENDS, -1);
1118 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
1119 GTK_MOVEMENT_BUFFER_ENDS, -1);
1121 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
1122 GTK_MOVEMENT_BUFFER_ENDS, 1);
1124 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
1125 GTK_MOVEMENT_BUFFER_ENDS, 1);
1127 add_move_binding (binding_set, GDK_Page_Up, 0,
1128 GTK_MOVEMENT_PAGES, -1);
1130 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
1131 GTK_MOVEMENT_PAGES, -1);
1133 add_move_binding (binding_set, GDK_Page_Down, 0,
1134 GTK_MOVEMENT_PAGES, 1);
1136 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
1137 GTK_MOVEMENT_PAGES, 1);
1139 add_move_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
1140 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1142 add_move_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
1143 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1145 add_move_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
1146 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1148 add_move_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
1149 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1152 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
1154 G_TYPE_BOOLEAN, TRUE);
1156 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
1158 G_TYPE_BOOLEAN, TRUE);
1161 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
1163 G_TYPE_BOOLEAN, FALSE);
1165 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1167 G_TYPE_BOOLEAN, FALSE);
1170 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
1171 "delete-from-cursor", 2,
1172 G_TYPE_ENUM, GTK_DELETE_CHARS,
1175 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
1176 "delete-from-cursor", 2,
1177 G_TYPE_ENUM, GTK_DELETE_CHARS,
1180 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
1183 /* Make this do the same as Backspace, to help with mis-typing */
1184 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
1187 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
1188 "delete-from-cursor", 2,
1189 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1192 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
1193 "delete-from-cursor", 2,
1194 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1197 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
1198 "delete-from-cursor", 2,
1199 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1202 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1203 "delete-from-cursor", 2,
1204 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1207 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1208 "delete-from-cursor", 2,
1209 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1212 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1213 "delete-from-cursor", 2,
1214 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1217 /* Cut/copy/paste */
1219 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
1220 "cut-clipboard", 0);
1221 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
1222 "copy-clipboard", 0);
1223 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
1224 "paste-clipboard", 0);
1226 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
1227 "cut-clipboard", 0);
1228 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
1229 "copy-clipboard", 0);
1230 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
1231 "paste-clipboard", 0);
1234 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1235 "toggle-overwrite", 0);
1236 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1237 "toggle-overwrite", 0);
1240 gtk_binding_entry_add_signal (binding_set, GDK_F7, 0,
1241 "toggle-cursor-visible", 0);
1243 /* Control-tab focus motion */
1244 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
1246 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1247 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
1249 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1251 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1253 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1254 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1256 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1258 g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1262 gtk_text_view_init (GtkTextView *text_view)
1264 GtkWidget *widget = GTK_WIDGET (text_view);
1265 GtkTargetList *target_list;
1267 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1269 /* Set up default style */
1270 text_view->wrap_mode = GTK_WRAP_NONE;
1271 text_view->pixels_above_lines = 0;
1272 text_view->pixels_below_lines = 0;
1273 text_view->pixels_inside_wrap = 0;
1274 text_view->justify = GTK_JUSTIFY_LEFT;
1275 text_view->left_margin = 0;
1276 text_view->right_margin = 0;
1277 text_view->indent = 0;
1278 text_view->tabs = NULL;
1279 text_view->editable = TRUE;
1281 gtk_drag_dest_set (widget, 0, NULL, 0,
1282 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1284 target_list = gtk_target_list_new (NULL, 0);
1285 gtk_drag_dest_set_target_list (widget, target_list);
1286 gtk_target_list_unref (target_list);
1288 text_view->virtual_cursor_x = -1;
1289 text_view->virtual_cursor_y = -1;
1291 /* This object is completely private. No external entity can gain a reference
1292 * to it; so we create it here and destroy it in finalize ().
1294 text_view->im_context = gtk_im_multicontext_new ();
1296 g_signal_connect (text_view->im_context, "commit",
1297 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1298 g_signal_connect (text_view->im_context, "preedit-changed",
1299 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1300 g_signal_connect (text_view->im_context, "retrieve-surrounding",
1301 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1302 g_signal_connect (text_view->im_context, "delete-surrounding",
1303 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1305 text_view->cursor_visible = TRUE;
1307 text_view->accepts_tab = TRUE;
1309 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1312 text_view->drag_start_x = -1;
1313 text_view->drag_start_y = -1;
1315 text_view->pending_place_cursor_button = 0;
1317 /* We handle all our own redrawing */
1318 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1322 * gtk_text_view_new:
1324 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1325 * before using the text view, an empty default buffer will be created
1326 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1327 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1329 * Return value: a new #GtkTextView
1332 gtk_text_view_new (void)
1334 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1338 * gtk_text_view_new_with_buffer:
1339 * @buffer: a #GtkTextBuffer
1341 * Creates a new #GtkTextView widget displaying the buffer
1342 * @buffer. One buffer can be shared among many widgets.
1343 * @buffer may be %NULL to create a default buffer, in which case
1344 * this function is equivalent to gtk_text_view_new(). The
1345 * text view adds its own reference count to the buffer; it does not
1346 * take over an existing reference.
1348 * Return value: a new #GtkTextView.
1351 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1353 GtkTextView *text_view;
1355 text_view = (GtkTextView*)gtk_text_view_new ();
1357 gtk_text_view_set_buffer (text_view, buffer);
1359 return GTK_WIDGET (text_view);
1363 * gtk_text_view_set_buffer:
1364 * @text_view: a #GtkTextView
1365 * @buffer: a #GtkTextBuffer
1367 * Sets @buffer as the buffer being displayed by @text_view. The previous
1368 * buffer displayed by the text view is unreferenced, and a reference is
1369 * added to @buffer. If you owned a reference to @buffer before passing it
1370 * to this function, you must remove that reference yourself; #GtkTextView
1371 * will not "adopt" it.
1374 gtk_text_view_set_buffer (GtkTextView *text_view,
1375 GtkTextBuffer *buffer)
1377 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1378 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1380 if (text_view->buffer == buffer)
1383 if (text_view->buffer != NULL)
1385 /* Destroy all anchored children */
1389 copy = g_slist_copy (text_view->children);
1391 while (tmp_list != NULL)
1393 GtkTextViewChild *vc = tmp_list->data;
1397 gtk_widget_destroy (vc->widget);
1398 /* vc may now be invalid! */
1401 tmp_list = g_slist_next (tmp_list);
1404 g_slist_free (copy);
1406 g_signal_handlers_disconnect_by_func (text_view->buffer,
1407 gtk_text_view_mark_set_handler,
1409 g_signal_handlers_disconnect_by_func (text_view->buffer,
1410 gtk_text_view_target_list_notify,
1412 g_signal_handlers_disconnect_by_func (text_view->buffer,
1413 gtk_text_view_paste_done_handler,
1415 g_object_unref (text_view->buffer);
1416 text_view->dnd_mark = NULL;
1417 text_view->first_para_mark = NULL;
1419 if (GTK_WIDGET_REALIZED (text_view))
1421 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1422 GDK_SELECTION_PRIMARY);
1423 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1427 text_view->buffer = buffer;
1429 if (text_view->layout)
1430 gtk_text_layout_set_buffer (text_view->layout, buffer);
1436 g_object_ref (buffer);
1438 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1440 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1444 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1448 text_view->first_para_pixels = 0;
1450 g_signal_connect (text_view->buffer, "mark-set",
1451 G_CALLBACK (gtk_text_view_mark_set_handler),
1453 g_signal_connect (text_view->buffer, "notify::paste-target-list",
1454 G_CALLBACK (gtk_text_view_target_list_notify),
1456 g_signal_connect (text_view->buffer, "paste-done",
1457 G_CALLBACK (gtk_text_view_paste_done_handler),
1460 gtk_text_view_target_list_notify (text_view->buffer, NULL, text_view);
1462 if (GTK_WIDGET_REALIZED (text_view))
1464 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1465 GDK_SELECTION_PRIMARY);
1466 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1470 g_object_notify (G_OBJECT (text_view), "buffer");
1472 if (GTK_WIDGET_VISIBLE (text_view))
1473 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1475 DV(g_print ("Invalidating due to set_buffer\n"));
1476 gtk_text_view_invalidate (text_view);
1479 static GtkTextBuffer*
1480 get_buffer (GtkTextView *text_view)
1482 if (text_view->buffer == NULL)
1485 b = gtk_text_buffer_new (NULL);
1486 gtk_text_view_set_buffer (text_view, b);
1490 return text_view->buffer;
1494 * gtk_text_view_get_buffer:
1495 * @text_view: a #GtkTextView
1497 * Returns the #GtkTextBuffer being displayed by this text view.
1498 * The reference count on the buffer is not incremented; the caller
1499 * of this function won't own a new reference.
1501 * Return value: a #GtkTextBuffer
1504 gtk_text_view_get_buffer (GtkTextView *text_view)
1506 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1508 return get_buffer (text_view);
1512 * gtk_text_view_get_iter_at_location:
1513 * @text_view: a #GtkTextView
1514 * @iter: a #GtkTextIter
1515 * @x: x position, in buffer coordinates
1516 * @y: y position, in buffer coordinates
1518 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1519 * coordinates are coordinates for the entire buffer, not just the
1520 * currently-displayed portion. If you have coordinates from an
1521 * event, you have to convert those to buffer coordinates with
1522 * gtk_text_view_window_to_buffer_coords().
1525 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1530 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1531 g_return_if_fail (iter != NULL);
1533 gtk_text_view_ensure_layout (text_view);
1535 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1540 * gtk_text_view_get_iter_at_position:
1541 * @text_view: a #GtkTextView
1542 * @iter: a #GtkTextIter
1543 * @trailing: if non-%NULL, location to store an integer indicating where
1544 * in the grapheme the user clicked. It will either be
1545 * zero, or the number of characters in the grapheme.
1546 * 0 represents the trailing edge of the grapheme.
1547 * @x: x position, in buffer coordinates
1548 * @y: y position, in buffer coordinates
1550 * Retrieves the iterator pointing to the character at buffer
1551 * coordinates @x and @y. Buffer coordinates are coordinates for
1552 * the entire buffer, not just the currently-displayed portion.
1553 * If you have coordinates from an event, you have to convert
1554 * those to buffer coordinates with
1555 * gtk_text_view_window_to_buffer_coords().
1557 * Note that this is different from gtk_text_view_get_iter_at_location(),
1558 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1564 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1570 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1571 g_return_if_fail (iter != NULL);
1573 gtk_text_view_ensure_layout (text_view);
1575 gtk_text_layout_get_iter_at_position (text_view->layout,
1576 iter, trailing, x, y);
1580 * gtk_text_view_get_iter_location:
1581 * @text_view: a #GtkTextView
1582 * @iter: a #GtkTextIter
1583 * @location: bounds of the character at @iter
1585 * Gets a rectangle which roughly contains the character at @iter.
1586 * The rectangle position is in buffer coordinates; use
1587 * gtk_text_view_buffer_to_window_coords() to convert these
1588 * coordinates to coordinates for one of the windows in the text view.
1591 gtk_text_view_get_iter_location (GtkTextView *text_view,
1592 const GtkTextIter *iter,
1593 GdkRectangle *location)
1595 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1596 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1598 gtk_text_view_ensure_layout (text_view);
1600 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1604 * gtk_text_view_get_line_yrange:
1605 * @text_view: a #GtkTextView
1606 * @iter: a #GtkTextIter
1607 * @y: return location for a y coordinate
1608 * @height: return location for a height
1610 * Gets the y coordinate of the top of the line containing @iter,
1611 * and the height of the line. The coordinate is a buffer coordinate;
1612 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1615 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1616 const GtkTextIter *iter,
1620 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1621 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1623 gtk_text_view_ensure_layout (text_view);
1625 gtk_text_layout_get_line_yrange (text_view->layout,
1632 * gtk_text_view_get_line_at_y:
1633 * @text_view: a #GtkTextView
1634 * @target_iter: a #GtkTextIter
1635 * @y: a y coordinate
1636 * @line_top: return location for top coordinate of the line
1638 * Gets the #GtkTextIter at the start of the line containing
1639 * the coordinate @y. @y is in buffer coordinates, convert from
1640 * window coordinates with gtk_text_view_window_to_buffer_coords().
1641 * If non-%NULL, @line_top will be filled with the coordinate of the top
1645 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1646 GtkTextIter *target_iter,
1650 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1652 gtk_text_view_ensure_layout (text_view);
1654 gtk_text_layout_get_line_at_y (text_view->layout,
1661 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1663 DV (g_print (" Setting adj to raw value %g\n", val));
1665 /* We don't really want to clamp to upper; we want to clamp to
1666 upper - page_size which is the highest value the scrollbar
1667 will let us reach. */
1668 if (val > (adj->upper - adj->page_size))
1669 val = adj->upper - adj->page_size;
1671 if (val < adj->lower)
1674 if (val != adj->value)
1676 DV (g_print (" Setting adj to clamped value %g\n", val));
1677 gtk_adjustment_set_value (adj, val);
1685 * gtk_text_view_scroll_to_iter:
1686 * @text_view: a #GtkTextView
1687 * @iter: a #GtkTextIter
1688 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1689 * @use_align: whether to use alignment arguments (if %FALSE,
1690 * just get the mark onscreen)
1691 * @xalign: horizontal alignment of mark within visible area
1692 * @yalign: vertical alignment of mark within visible area
1694 * Scrolls @text_view so that @iter is on the screen in the position
1695 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1696 * left or top, 1.0 indicates right or bottom, 0.5 means center.
1697 * If @use_align is %FALSE, the text scrolls the minimal distance to
1698 * get the mark onscreen, possibly not scrolling at all. The effective
1699 * screen for purposes of this function is reduced by a margin of size
1702 * Note that this function uses the currently-computed height of the
1703 * lines in the text buffer. Line heights are computed in an idle
1704 * handler; so this function may not have the desired effect if it's
1705 * called before the height computations. To avoid oddness, consider
1706 * using gtk_text_view_scroll_to_mark() which saves a point to be
1707 * scrolled to after line validation.
1709 * Return value: %TRUE if scrolling occurred
1712 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1714 gdouble within_margin,
1720 GdkRectangle screen;
1725 gboolean retval = FALSE;
1727 gint screen_xoffset, screen_yoffset;
1728 gint current_x_scroll, current_y_scroll;
1730 /* FIXME why don't we do the validate-at-scroll-destination thing
1731 * from flush_scroll in this function? I think it wasn't done before
1732 * because changed_handler was screwed up, but I could be wrong.
1735 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1736 g_return_val_if_fail (iter != NULL, FALSE);
1737 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1738 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1739 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1741 widget = GTK_WIDGET (text_view);
1743 DV(g_print(G_STRLOC"\n"));
1745 gtk_text_layout_get_iter_location (text_view->layout,
1749 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1751 current_x_scroll = text_view->xoffset;
1752 current_y_scroll = text_view->yoffset;
1754 screen.x = current_x_scroll;
1755 screen.y = current_y_scroll;
1756 screen.width = SCREEN_WIDTH (widget);
1757 screen.height = SCREEN_HEIGHT (widget);
1759 screen_xoffset = screen.width * within_margin;
1760 screen_yoffset = screen.height * within_margin;
1762 screen.x += screen_xoffset;
1763 screen.y += screen_yoffset;
1764 screen.width -= screen_xoffset * 2;
1765 screen.height -= screen_yoffset * 2;
1767 /* paranoia check */
1768 if (screen.width < 1)
1770 if (screen.height < 1)
1773 /* The -1 here ensures that we leave enough space to draw the cursor
1774 * when this function is used for horizontal scrolling.
1776 screen_right = screen.x + screen.width - 1;
1777 screen_bottom = screen.y + screen.height;
1779 /* The alignment affects the point in the target character that we
1780 * choose to align. If we're doing right/bottom alignment, we align
1781 * the right/bottom edge of the character the mark is at; if we're
1782 * doing left/top we align the left/top edge of the character; if
1783 * we're doing center alignment we align the center of the
1787 /* Vertical scroll */
1790 scroll_dest = current_y_scroll;
1794 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1796 /* if scroll_dest < screen.y, we move a negative increment (up),
1797 * else a positive increment (down)
1799 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1803 /* move minimum to get onscreen */
1804 if (rect.y < screen.y)
1806 scroll_dest = rect.y;
1807 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1809 else if ((rect.y + rect.height) > screen_bottom)
1811 scroll_dest = rect.y + rect.height;
1812 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1816 if (scroll_inc != 0)
1818 retval = set_adjustment_clamped (get_vadjustment (text_view),
1819 current_y_scroll + scroll_inc);
1821 DV (g_print (" vert increment %d\n", scroll_inc));
1824 /* Horizontal scroll */
1827 scroll_dest = current_x_scroll;
1831 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1833 /* if scroll_dest < screen.y, we move a negative increment (left),
1834 * else a positive increment (right)
1836 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1840 /* move minimum to get onscreen */
1841 if (rect.x < screen.x)
1843 scroll_dest = rect.x;
1844 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1846 else if ((rect.x + rect.width) > screen_right)
1848 scroll_dest = rect.x + rect.width;
1849 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1853 if (scroll_inc != 0)
1855 retval = set_adjustment_clamped (get_hadjustment (text_view),
1856 current_x_scroll + scroll_inc);
1858 DV (g_print (" horiz increment %d\n", scroll_inc));
1862 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1864 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1870 free_pending_scroll (GtkTextPendingScroll *scroll)
1872 if (!gtk_text_mark_get_deleted (scroll->mark))
1873 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1875 g_object_unref (scroll->mark);
1880 cancel_pending_scroll (GtkTextView *text_view)
1882 if (text_view->pending_scroll)
1884 free_pending_scroll (text_view->pending_scroll);
1885 text_view->pending_scroll = NULL;
1890 gtk_text_view_queue_scroll (GtkTextView *text_view,
1892 gdouble within_margin,
1898 GtkTextPendingScroll *scroll;
1900 DV(g_print(G_STRLOC"\n"));
1902 scroll = g_new (GtkTextPendingScroll, 1);
1904 scroll->within_margin = within_margin;
1905 scroll->use_align = use_align;
1906 scroll->xalign = xalign;
1907 scroll->yalign = yalign;
1909 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1911 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1914 gtk_text_mark_get_left_gravity (mark));
1916 g_object_ref (scroll->mark);
1918 cancel_pending_scroll (text_view);
1920 text_view->pending_scroll = scroll;
1924 gtk_text_view_flush_scroll (GtkTextView *text_view)
1927 GtkTextPendingScroll *scroll;
1931 widget = GTK_WIDGET (text_view);
1933 DV(g_print(G_STRLOC"\n"));
1935 if (text_view->pending_scroll == NULL)
1937 DV (g_print ("in flush scroll, no pending scroll\n"));
1941 scroll = text_view->pending_scroll;
1943 /* avoid recursion */
1944 text_view->pending_scroll = NULL;
1946 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1948 /* Validate area around the scroll destination, so the adjustment
1949 * can meaningfully point into that area. We must validate
1950 * enough area to be sure that after we scroll, everything onscreen
1951 * is valid; otherwise, validation will maintain the first para
1952 * in one place, but may push the target iter off the bottom of
1955 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1956 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1957 - (widget->allocation.height * 2),
1958 widget->allocation.height * 2);
1960 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1962 /* Ensure we have updated width/height */
1963 gtk_text_view_update_adjustments (text_view);
1965 retval = gtk_text_view_scroll_to_iter (text_view,
1967 scroll->within_margin,
1972 free_pending_scroll (scroll);
1978 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1980 if (upper != adj->upper)
1982 gdouble min = MAX (0.0, upper - adj->page_size);
1983 gboolean value_changed = FALSE;
1987 if (adj->value > min)
1990 value_changed = TRUE;
1993 gtk_adjustment_changed (adj);
1994 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1998 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1999 gtk_adjustment_value_changed (adj);
2005 gtk_text_view_update_adjustments (GtkTextView *text_view)
2007 gint width = 0, height = 0;
2009 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
2011 if (text_view->layout)
2012 gtk_text_layout_get_size (text_view->layout, &width, &height);
2014 /* Make room for the cursor after the last character in the widest line */
2015 width += SPACE_FOR_CURSOR;
2017 if (text_view->width != width || text_view->height != height)
2019 if (text_view->width != width)
2020 text_view->width_changed = TRUE;
2022 text_view->width = width;
2023 text_view->height = height;
2025 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
2026 MAX (SCREEN_WIDTH (text_view), width));
2027 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
2028 MAX (SCREEN_HEIGHT (text_view), height));
2030 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
2032 /* Set up the step sizes; we'll say that a page is
2033 our allocation minus one step, and a step is
2034 1/10 of our allocation. */
2035 text_view->hadjustment->step_increment =
2036 SCREEN_WIDTH (text_view) / 10.0;
2037 text_view->hadjustment->page_increment =
2038 SCREEN_WIDTH (text_view) * 0.9;
2040 text_view->vadjustment->step_increment =
2041 SCREEN_HEIGHT (text_view) / 10.0;
2042 text_view->vadjustment->page_increment =
2043 SCREEN_HEIGHT (text_view) * 0.9;
2045 gtk_adjustment_changed (get_hadjustment (text_view));
2046 gtk_adjustment_changed (get_vadjustment (text_view));
2051 gtk_text_view_update_layout_width (GtkTextView *text_view)
2053 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
2055 gtk_text_view_ensure_layout (text_view);
2057 gtk_text_layout_set_screen_width (text_view->layout,
2058 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
2062 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
2066 if (text_view->layout == NULL)
2069 gtk_text_view_get_cursor_location (text_view, &area);
2071 area.x -= text_view->xoffset;
2072 area.y -= text_view->yoffset;
2074 /* Width returned by Pango indicates direction of cursor,
2075 * by it's sign more than the size of cursor.
2079 gtk_im_context_set_cursor_location (text_view->im_context, &area);
2083 do_update_im_spot_location (gpointer text_view)
2085 GtkTextViewPrivate *priv;
2087 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2088 priv->im_spot_idle = 0;
2090 gtk_text_view_update_im_spot_location (text_view);
2095 queue_update_im_spot_location (GtkTextView *text_view)
2097 GtkTextViewPrivate *priv;
2099 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2101 /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
2102 * so we don't wait until the entire buffer has been validated. */
2103 if (!priv->im_spot_idle)
2104 priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
2105 do_update_im_spot_location,
2111 flush_update_im_spot_location (GtkTextView *text_view)
2113 GtkTextViewPrivate *priv;
2115 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2117 if (priv->im_spot_idle)
2119 g_source_remove (priv->im_spot_idle);
2120 priv->im_spot_idle = 0;
2121 gtk_text_view_update_im_spot_location (text_view);
2126 * gtk_text_view_scroll_to_mark:
2127 * @text_view: a #GtkTextView
2128 * @mark: a #GtkTextMark
2129 * @within_margin: margin as a [0.0,0.5) fraction of screen size
2130 * @use_align: whether to use alignment arguments (if %FALSE, just
2131 * get the mark onscreen)
2132 * @xalign: horizontal alignment of mark within visible area
2133 * @yalign: vertical alignment of mark within visible area
2135 * Scrolls @text_view so that @mark is on the screen in the position
2136 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
2137 * left or top, 1.0 indicates right or bottom, 0.5 means center.
2138 * If @use_align is %FALSE, the text scrolls the minimal distance to
2139 * get the mark onscreen, possibly not scrolling at all. The effective
2140 * screen for purposes of this function is reduced by a margin of size
2144 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
2146 gdouble within_margin,
2151 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2152 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2153 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
2154 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
2155 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
2157 gtk_text_view_queue_scroll (text_view, mark,
2163 /* If no validation is pending, we need to go ahead and force an
2166 if (text_view->layout &&
2167 gtk_text_layout_is_valid (text_view->layout))
2168 gtk_text_view_flush_scroll (text_view);
2172 * gtk_text_view_scroll_mark_onscreen:
2173 * @text_view: a #GtkTextView
2174 * @mark: a mark in the buffer for @text_view
2176 * Scrolls @text_view the minimum distance such that @mark is contained
2177 * within the visible area of the widget.
2180 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
2183 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2184 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2186 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
2190 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
2192 GdkRectangle visible_rect;
2193 gtk_text_view_get_visible_rect (text_view, &visible_rect);
2195 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
2197 visible_rect.y + visible_rect.height);
2201 * gtk_text_view_move_mark_onscreen:
2202 * @text_view: a #GtkTextView
2203 * @mark: a #GtkTextMark
2205 * Moves a mark within the buffer so that it's
2206 * located within the currently-visible text area.
2208 * Return value: %TRUE if the mark moved (wasn't already onscreen)
2211 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
2216 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2217 g_return_val_if_fail (mark != NULL, FALSE);
2219 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2221 if (clamp_iter_onscreen (text_view, &iter))
2223 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
2231 * gtk_text_view_get_visible_rect:
2232 * @text_view: a #GtkTextView
2233 * @visible_rect: rectangle to fill
2235 * Fills @visible_rect with the currently-visible
2236 * region of the buffer, in buffer coordinates. Convert to window coordinates
2237 * with gtk_text_view_buffer_to_window_coords().
2240 gtk_text_view_get_visible_rect (GtkTextView *text_view,
2241 GdkRectangle *visible_rect)
2245 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2247 widget = GTK_WIDGET (text_view);
2251 visible_rect->x = text_view->xoffset;
2252 visible_rect->y = text_view->yoffset;
2253 visible_rect->width = SCREEN_WIDTH (widget);
2254 visible_rect->height = SCREEN_HEIGHT (widget);
2256 DV(g_print(" visible rect: %d,%d %d x %d\n",
2259 visible_rect->width,
2260 visible_rect->height));
2265 * gtk_text_view_set_wrap_mode:
2266 * @text_view: a #GtkTextView
2267 * @wrap_mode: a #GtkWrapMode
2269 * Sets the line wrapping for the view.
2272 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
2273 GtkWrapMode wrap_mode)
2275 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2277 if (text_view->wrap_mode != wrap_mode)
2279 text_view->wrap_mode = wrap_mode;
2281 if (text_view->layout)
2283 text_view->layout->default_style->wrap_mode = wrap_mode;
2284 gtk_text_layout_default_style_changed (text_view->layout);
2288 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2292 * gtk_text_view_get_wrap_mode:
2293 * @text_view: a #GtkTextView
2295 * Gets the line wrapping for the view.
2297 * Return value: the line wrap setting
2300 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2302 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2304 return text_view->wrap_mode;
2308 * gtk_text_view_set_editable:
2309 * @text_view: a #GtkTextView
2310 * @setting: whether it's editable
2312 * Sets the default editability of the #GtkTextView. You can override
2313 * this default setting with tags in the buffer, using the "editable"
2314 * attribute of tags.
2317 gtk_text_view_set_editable (GtkTextView *text_view,
2320 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2321 setting = setting != FALSE;
2323 if (text_view->editable != setting)
2327 gtk_text_view_reset_im_context(text_view);
2328 if (GTK_WIDGET_HAS_FOCUS (text_view))
2329 gtk_im_context_focus_out (text_view->im_context);
2332 text_view->editable = setting;
2334 if (setting && GTK_WIDGET_HAS_FOCUS (text_view))
2335 gtk_im_context_focus_in (text_view->im_context);
2337 if (text_view->layout)
2339 gtk_text_layout_set_overwrite_mode (text_view->layout,
2340 text_view->overwrite_mode && text_view->editable);
2341 text_view->layout->default_style->editable = text_view->editable;
2342 gtk_text_layout_default_style_changed (text_view->layout);
2345 g_object_notify (G_OBJECT (text_view), "editable");
2350 * gtk_text_view_get_editable:
2351 * @text_view: a #GtkTextView
2353 * Returns the default editability of the #GtkTextView. Tags in the
2354 * buffer may override this setting for some ranges of text.
2356 * Return value: whether text is editable by default
2359 gtk_text_view_get_editable (GtkTextView *text_view)
2361 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2363 return text_view->editable;
2367 * gtk_text_view_set_pixels_above_lines:
2368 * @text_view: a #GtkTextView
2369 * @pixels_above_lines: pixels above paragraphs
2371 * Sets the default number of blank pixels above paragraphs in @text_view.
2372 * Tags in the buffer for @text_view may override the defaults.
2375 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2376 gint pixels_above_lines)
2378 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2380 if (text_view->pixels_above_lines != pixels_above_lines)
2382 text_view->pixels_above_lines = pixels_above_lines;
2384 if (text_view->layout)
2386 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
2387 gtk_text_layout_default_style_changed (text_view->layout);
2390 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2395 * gtk_text_view_get_pixels_above_lines:
2396 * @text_view: a #GtkTextView
2398 * Gets the default number of pixels to put above paragraphs.
2400 * Return value: default number of pixels above paragraphs
2403 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2405 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2407 return text_view->pixels_above_lines;
2411 * gtk_text_view_set_pixels_below_lines:
2412 * @text_view: a #GtkTextView
2413 * @pixels_below_lines: pixels below paragraphs
2415 * Sets the default number of pixels of blank space
2416 * to put below paragraphs in @text_view. May be overridden
2417 * by tags applied to @text_view's buffer.
2420 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2421 gint pixels_below_lines)
2423 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2425 if (text_view->pixels_below_lines != pixels_below_lines)
2427 text_view->pixels_below_lines = pixels_below_lines;
2429 if (text_view->layout)
2431 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2432 gtk_text_layout_default_style_changed (text_view->layout);
2435 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2440 * gtk_text_view_get_pixels_below_lines:
2441 * @text_view: a #GtkTextView
2443 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2445 * Return value: default number of blank pixels below paragraphs
2448 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2450 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2452 return text_view->pixels_below_lines;
2456 * gtk_text_view_set_pixels_inside_wrap:
2457 * @text_view: a #GtkTextView
2458 * @pixels_inside_wrap: default number of pixels between wrapped lines
2460 * Sets the default number of pixels of blank space to leave between
2461 * display/wrapped lines within a paragraph. May be overridden by
2462 * tags in @text_view's buffer.
2465 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2466 gint pixels_inside_wrap)
2468 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2470 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2472 text_view->pixels_inside_wrap = pixels_inside_wrap;
2474 if (text_view->layout)
2476 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2477 gtk_text_layout_default_style_changed (text_view->layout);
2480 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2485 * gtk_text_view_get_pixels_inside_wrap:
2486 * @text_view: a #GtkTextView
2488 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2490 * Return value: default number of pixels of blank space between wrapped lines
2493 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2495 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2497 return text_view->pixels_inside_wrap;
2501 * gtk_text_view_set_justification:
2502 * @text_view: a #GtkTextView
2503 * @justification: justification
2505 * Sets the default justification of text in @text_view.
2506 * Tags in the view's buffer may override the default.
2510 gtk_text_view_set_justification (GtkTextView *text_view,
2511 GtkJustification justification)
2513 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2515 if (text_view->justify != justification)
2517 text_view->justify = justification;
2519 if (text_view->layout)
2521 text_view->layout->default_style->justification = justification;
2522 gtk_text_layout_default_style_changed (text_view->layout);
2525 g_object_notify (G_OBJECT (text_view), "justification");
2530 * gtk_text_view_get_justification:
2531 * @text_view: a #GtkTextView
2533 * Gets the default justification of paragraphs in @text_view.
2534 * Tags in the buffer may override the default.
2536 * Return value: default justification
2539 gtk_text_view_get_justification (GtkTextView *text_view)
2541 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2543 return text_view->justify;
2547 * gtk_text_view_set_left_margin:
2548 * @text_view: a #GtkTextView
2549 * @left_margin: left margin in pixels
2551 * Sets the default left margin for text in @text_view.
2552 * Tags in the buffer may override the default.
2555 gtk_text_view_set_left_margin (GtkTextView *text_view,
2558 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2560 if (text_view->left_margin != left_margin)
2562 text_view->left_margin = left_margin;
2564 if (text_view->layout)
2566 text_view->layout->default_style->left_margin = left_margin;
2567 gtk_text_layout_default_style_changed (text_view->layout);
2570 g_object_notify (G_OBJECT (text_view), "left-margin");
2575 * gtk_text_view_get_left_margin:
2576 * @text_view: a #GtkTextView
2578 * Gets the default left margin size of paragraphs in the @text_view.
2579 * Tags in the buffer may override the default.
2581 * Return value: left margin in pixels
2584 gtk_text_view_get_left_margin (GtkTextView *text_view)
2586 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2588 return text_view->left_margin;
2592 * gtk_text_view_set_right_margin:
2593 * @text_view: a #GtkTextView
2594 * @right_margin: right margin in pixels
2596 * Sets the default right margin for text in the text view.
2597 * Tags in the buffer may override the default.
2600 gtk_text_view_set_right_margin (GtkTextView *text_view,
2603 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2605 if (text_view->right_margin != right_margin)
2607 text_view->right_margin = right_margin;
2609 if (text_view->layout)
2611 text_view->layout->default_style->right_margin = right_margin;
2612 gtk_text_layout_default_style_changed (text_view->layout);
2615 g_object_notify (G_OBJECT (text_view), "right-margin");
2620 * gtk_text_view_get_right_margin:
2621 * @text_view: a #GtkTextView
2623 * Gets the default right margin for text in @text_view. Tags
2624 * in the buffer may override the default.
2626 * Return value: right margin in pixels
2629 gtk_text_view_get_right_margin (GtkTextView *text_view)
2631 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2633 return text_view->right_margin;
2637 * gtk_text_view_set_indent:
2638 * @text_view: a #GtkTextView
2639 * @indent: indentation in pixels
2641 * Sets the default indentation for paragraphs in @text_view.
2642 * Tags in the buffer may override the default.
2645 gtk_text_view_set_indent (GtkTextView *text_view,
2648 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2650 if (text_view->indent != indent)
2652 text_view->indent = indent;
2654 if (text_view->layout)
2656 text_view->layout->default_style->indent = indent;
2657 gtk_text_layout_default_style_changed (text_view->layout);
2660 g_object_notify (G_OBJECT (text_view), "indent");
2665 * gtk_text_view_get_indent:
2666 * @text_view: a #GtkTextView
2668 * Gets the default indentation of paragraphs in @text_view.
2669 * Tags in the view's buffer may override the default.
2670 * The indentation may be negative.
2672 * Return value: number of pixels of indentation
2675 gtk_text_view_get_indent (GtkTextView *text_view)
2677 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2679 return text_view->indent;
2683 * gtk_text_view_set_tabs:
2684 * @text_view: a #GtkTextView
2685 * @tabs: tabs as a #PangoTabArray
2687 * Sets the default tab stops for paragraphs in @text_view.
2688 * Tags in the buffer may override the default.
2691 gtk_text_view_set_tabs (GtkTextView *text_view,
2692 PangoTabArray *tabs)
2694 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2696 if (text_view->tabs)
2697 pango_tab_array_free (text_view->tabs);
2699 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2701 if (text_view->layout)
2703 /* some unkosher futzing in internal struct details... */
2704 if (text_view->layout->default_style->tabs)
2705 pango_tab_array_free (text_view->layout->default_style->tabs);
2707 text_view->layout->default_style->tabs =
2708 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2710 gtk_text_layout_default_style_changed (text_view->layout);
2713 g_object_notify (G_OBJECT (text_view), "tabs");
2717 * gtk_text_view_get_tabs:
2718 * @text_view: a #GtkTextView
2720 * Gets the default tabs for @text_view. Tags in the buffer may
2721 * override the defaults. The returned array will be %NULL if
2722 * "standard" (8-space) tabs are used. Free the return value
2723 * with pango_tab_array_free().
2725 * Return value: copy of default tab array, or %NULL if "standard"
2726 * tabs are used; must be freed with pango_tab_array_free().
2729 gtk_text_view_get_tabs (GtkTextView *text_view)
2731 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2733 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2737 gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
2739 gtk_text_view_set_cursor_visible (text_view, !text_view->cursor_visible);
2743 * gtk_text_view_set_cursor_visible:
2744 * @text_view: a #GtkTextView
2745 * @setting: whether to show the insertion cursor
2747 * Toggles whether the insertion point is displayed. A buffer with no editable
2748 * text probably shouldn't have a visible cursor, so you may want to turn
2752 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2755 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2757 setting = (setting != FALSE);
2759 if (text_view->cursor_visible != setting)
2761 text_view->cursor_visible = setting;
2763 if (GTK_WIDGET_HAS_FOCUS (text_view))
2765 if (text_view->layout)
2767 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2768 gtk_text_view_check_cursor_blink (text_view);
2772 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2777 * gtk_text_view_get_cursor_visible:
2778 * @text_view: a #GtkTextView
2780 * Find out whether the cursor is being displayed.
2782 * Return value: whether the insertion mark is visible
2785 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2787 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2789 return text_view->cursor_visible;
2794 * gtk_text_view_place_cursor_onscreen:
2795 * @text_view: a #GtkTextView
2797 * Moves the cursor to the currently visible region of the
2798 * buffer, it it isn't there already.
2800 * Return value: %TRUE if the cursor had to be moved.
2803 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2807 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2809 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2810 gtk_text_buffer_get_insert (get_buffer (text_view)));
2812 if (clamp_iter_onscreen (text_view, &insert))
2814 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2822 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2824 if (text_view->first_validate_idle != 0)
2826 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2827 g_source_remove (text_view->first_validate_idle);
2828 text_view->first_validate_idle = 0;
2831 if (text_view->incremental_validate_idle != 0)
2833 g_source_remove (text_view->incremental_validate_idle);
2834 text_view->incremental_validate_idle = 0;
2839 gtk_text_view_destroy (GtkObject *object)
2841 GtkTextView *text_view;
2842 GtkTextViewPrivate *priv;
2844 text_view = GTK_TEXT_VIEW (object);
2845 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2847 gtk_text_view_remove_validate_idles (text_view);
2848 gtk_text_view_set_buffer (text_view, NULL);
2849 gtk_text_view_destroy_layout (text_view);
2851 if (text_view->scroll_timeout)
2853 g_source_remove (text_view->scroll_timeout);
2854 text_view->scroll_timeout = 0;
2857 if (priv->im_spot_idle)
2859 g_source_remove (priv->im_spot_idle);
2860 priv->im_spot_idle = 0;
2863 GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy (object);
2867 gtk_text_view_finalize (GObject *object)
2869 GtkTextView *text_view;
2870 GtkTextViewPrivate *priv;
2872 text_view = GTK_TEXT_VIEW (object);
2873 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2875 g_assert (text_view->buffer == NULL);
2877 gtk_text_view_destroy_layout (text_view);
2878 gtk_text_view_set_buffer (text_view, NULL);
2880 cancel_pending_scroll (text_view);
2882 if (text_view->tabs)
2883 pango_tab_array_free (text_view->tabs);
2885 if (text_view->hadjustment)
2886 g_object_unref (text_view->hadjustment);
2887 if (text_view->vadjustment)
2888 g_object_unref (text_view->vadjustment);
2890 text_window_free (text_view->text_window);
2892 if (text_view->left_window)
2893 text_window_free (text_view->left_window);
2895 if (text_view->top_window)
2896 text_window_free (text_view->top_window);
2898 if (text_view->right_window)
2899 text_window_free (text_view->right_window);
2901 if (text_view->bottom_window)
2902 text_window_free (text_view->bottom_window);
2904 g_object_unref (text_view->im_context);
2906 g_free (priv->im_module);
2908 G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize (object);
2912 gtk_text_view_set_property (GObject *object,
2914 const GValue *value,
2917 GtkTextView *text_view;
2918 GtkTextViewPrivate *priv;
2920 text_view = GTK_TEXT_VIEW (object);
2921 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2925 case PROP_PIXELS_ABOVE_LINES:
2926 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2929 case PROP_PIXELS_BELOW_LINES:
2930 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2933 case PROP_PIXELS_INSIDE_WRAP:
2934 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2938 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2941 case PROP_WRAP_MODE:
2942 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2945 case PROP_JUSTIFICATION:
2946 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2949 case PROP_LEFT_MARGIN:
2950 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2953 case PROP_RIGHT_MARGIN:
2954 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2958 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2962 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2965 case PROP_CURSOR_VISIBLE:
2966 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2969 case PROP_OVERWRITE:
2970 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
2974 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
2977 case PROP_ACCEPTS_TAB:
2978 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
2981 case PROP_IM_MODULE:
2982 g_free (priv->im_module);
2983 priv->im_module = g_strdup (g_value_get_string (value));
2984 if (GTK_IS_IM_MULTICONTEXT (text_view->im_context))
2985 gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (text_view->im_context), priv->im_module);
2989 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2995 gtk_text_view_get_property (GObject *object,
3000 GtkTextView *text_view;
3001 GtkTextViewPrivate *priv;
3003 text_view = GTK_TEXT_VIEW (object);
3004 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
3008 case PROP_PIXELS_ABOVE_LINES:
3009 g_value_set_int (value, text_view->pixels_above_lines);
3012 case PROP_PIXELS_BELOW_LINES:
3013 g_value_set_int (value, text_view->pixels_below_lines);
3016 case PROP_PIXELS_INSIDE_WRAP:
3017 g_value_set_int (value, text_view->pixels_inside_wrap);
3021 g_value_set_boolean (value, text_view->editable);
3024 case PROP_WRAP_MODE:
3025 g_value_set_enum (value, text_view->wrap_mode);
3028 case PROP_JUSTIFICATION:
3029 g_value_set_enum (value, text_view->justify);
3032 case PROP_LEFT_MARGIN:
3033 g_value_set_int (value, text_view->left_margin);
3036 case PROP_RIGHT_MARGIN:
3037 g_value_set_int (value, text_view->right_margin);
3041 g_value_set_int (value, text_view->indent);
3045 g_value_set_boxed (value, text_view->tabs);
3048 case PROP_CURSOR_VISIBLE:
3049 g_value_set_boolean (value, text_view->cursor_visible);
3053 g_value_set_object (value, get_buffer (text_view));
3056 case PROP_OVERWRITE:
3057 g_value_set_boolean (value, text_view->overwrite_mode);
3060 case PROP_ACCEPTS_TAB:
3061 g_value_set_boolean (value, text_view->accepts_tab);
3064 case PROP_IM_MODULE:
3065 g_value_set_string (value, priv->im_module);
3069 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3075 gtk_text_view_size_request (GtkWidget *widget,
3076 GtkRequisition *requisition)
3078 GtkTextView *text_view;
3080 gint focus_edge_width;
3082 gboolean interior_focus;
3084 text_view = GTK_TEXT_VIEW (widget);
3086 gtk_widget_style_get (widget,
3087 "interior-focus", &interior_focus,
3088 "focus-line-width", &focus_width,
3092 focus_edge_width = 0;
3094 focus_edge_width = focus_width;
3096 if (text_view->layout)
3098 text_view->text_window->requisition.width = text_view->layout->width;
3099 text_view->text_window->requisition.height = text_view->layout->height;
3103 text_view->text_window->requisition.width = 0;
3104 text_view->text_window->requisition.height = 0;
3107 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
3108 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
3110 if (text_view->left_window)
3111 requisition->width += text_view->left_window->requisition.width;
3113 if (text_view->right_window)
3114 requisition->width += text_view->right_window->requisition.width;
3116 if (text_view->top_window)
3117 requisition->height += text_view->top_window->requisition.height;
3119 if (text_view->bottom_window)
3120 requisition->height += text_view->bottom_window->requisition.height;
3122 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
3123 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
3125 tmp_list = text_view->children;
3126 while (tmp_list != NULL)
3128 GtkTextViewChild *child = tmp_list->data;
3132 GtkRequisition child_req;
3133 GtkRequisition old_req;
3135 gtk_widget_get_child_requisition (child->widget, &old_req);
3137 gtk_widget_size_request (child->widget, &child_req);
3139 gtk_widget_get_child_requisition (child->widget, &child_req);
3141 /* Invalidate layout lines if required */
3142 if (text_view->layout &&
3143 (old_req.width != child_req.width ||
3144 old_req.height != child_req.height))
3145 gtk_text_child_anchor_queue_resize (child->anchor,
3150 GtkRequisition child_req;
3152 gtk_widget_size_request (child->widget, &child_req);
3155 tmp_list = g_slist_next (tmp_list);
3160 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
3161 GtkTextViewChild *vc,
3162 GtkAllocation *allocation)
3168 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3172 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
3175 buffer_y += vc->from_top_of_line;
3177 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
3178 allocation->y = buffer_y - text_view->yoffset;
3180 gtk_widget_get_child_requisition (vc->widget, &req);
3181 allocation->width = req.width;
3182 allocation->height = req.height;
3186 gtk_text_view_update_child_allocation (GtkTextView *text_view,
3187 GtkTextViewChild *vc)
3189 GtkAllocation allocation;
3191 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
3193 gtk_widget_size_allocate (vc->widget, &allocation);
3196 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
3198 vc->widget->allocation.x,
3199 vc->widget->allocation.y,
3200 text_view->yoffset);
3205 gtk_text_view_child_allocated (GtkTextLayout *layout,
3211 GtkTextViewChild *vc = NULL;
3212 GtkTextView *text_view = data;
3214 /* x,y is the position of the child from the top of the line, and
3215 * from the left of the buffer. We have to translate that into text
3216 * window coordinates, then size_allocate the child.
3219 vc = g_object_get_data (G_OBJECT (child),
3220 "gtk-text-view-child");
3222 g_assert (vc != NULL);
3224 DV (g_print ("child allocated at %d,%d\n", x, y));
3226 vc->from_left_of_buffer = x;
3227 vc->from_top_of_line = y;
3229 gtk_text_view_update_child_allocation (text_view, vc);
3233 gtk_text_view_allocate_children (GtkTextView *text_view)
3237 DV(g_print(G_STRLOC"\n"));
3239 tmp_list = text_view->children;
3240 while (tmp_list != NULL)
3242 GtkTextViewChild *child = tmp_list->data;
3244 g_assert (child != NULL);
3248 /* We need to force-validate the regions containing
3251 GtkTextIter child_loc;
3252 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3256 /* Since anchored children are only ever allocated from
3257 * gtk_text_layout_get_line_display() we have to make sure
3258 * that the display line caching in the layout doesn't
3259 * get in the way. Invalidating the layout around the anchor
3262 if (GTK_WIDGET_ALLOC_NEEDED (child->widget))
3264 GtkTextIter end = child_loc;
3265 gtk_text_iter_forward_char (&end);
3266 gtk_text_layout_invalidate (text_view->layout, &child_loc, &end);
3269 gtk_text_layout_validate_yrange (text_view->layout,
3275 GtkAllocation allocation;
3276 GtkRequisition child_req;
3278 allocation.x = child->x;
3279 allocation.y = child->y;
3281 gtk_widget_get_child_requisition (child->widget, &child_req);
3283 allocation.width = child_req.width;
3284 allocation.height = child_req.height;
3286 gtk_widget_size_allocate (child->widget, &allocation);
3289 tmp_list = g_slist_next (tmp_list);
3294 gtk_text_view_size_allocate (GtkWidget *widget,
3295 GtkAllocation *allocation)
3297 GtkTextView *text_view;
3298 GtkTextIter first_para;
3301 GdkRectangle text_rect;
3302 GdkRectangle left_rect;
3303 GdkRectangle right_rect;
3304 GdkRectangle top_rect;
3305 GdkRectangle bottom_rect;
3306 gint focus_edge_width;
3308 gboolean interior_focus;
3309 gboolean size_changed;
3311 text_view = GTK_TEXT_VIEW (widget);
3313 DV(g_print(G_STRLOC"\n"));
3316 widget->allocation.width != allocation->width ||
3317 widget->allocation.height != allocation->height;
3319 widget->allocation = *allocation;
3321 if (GTK_WIDGET_REALIZED (widget))
3323 gdk_window_move_resize (widget->window,
3324 allocation->x, allocation->y,
3325 allocation->width, allocation->height);
3328 /* distribute width/height among child windows. Ensure all
3329 * windows get at least a 1x1 allocation.
3332 gtk_widget_style_get (widget,
3333 "interior-focus", &interior_focus,
3334 "focus-line-width", &focus_width,
3338 focus_edge_width = 0;
3340 focus_edge_width = focus_width;
3342 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3344 if (text_view->left_window)
3345 left_rect.width = text_view->left_window->requisition.width;
3347 left_rect.width = 0;
3349 width -= left_rect.width;
3351 if (text_view->right_window)
3352 right_rect.width = text_view->right_window->requisition.width;
3354 right_rect.width = 0;
3356 width -= right_rect.width;
3358 text_rect.width = MAX (1, width);
3360 top_rect.width = text_rect.width;
3361 bottom_rect.width = text_rect.width;
3364 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3366 if (text_view->top_window)
3367 top_rect.height = text_view->top_window->requisition.height;
3369 top_rect.height = 0;
3371 height -= top_rect.height;
3373 if (text_view->bottom_window)
3374 bottom_rect.height = text_view->bottom_window->requisition.height;
3376 bottom_rect.height = 0;
3378 height -= bottom_rect.height;
3380 text_rect.height = MAX (1, height);
3382 left_rect.height = text_rect.height;
3383 right_rect.height = text_rect.height;
3386 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3387 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3389 text_rect.x = left_rect.x + left_rect.width;
3390 text_rect.y = top_rect.y + top_rect.height;
3392 left_rect.y = text_rect.y;
3393 right_rect.y = text_rect.y;
3395 top_rect.x = text_rect.x;
3396 bottom_rect.x = text_rect.x;
3398 right_rect.x = text_rect.x + text_rect.width;
3399 bottom_rect.y = text_rect.y + text_rect.height;
3401 text_window_size_allocate (text_view->text_window,
3404 if (text_view->left_window)
3405 text_window_size_allocate (text_view->left_window,
3408 if (text_view->right_window)
3409 text_window_size_allocate (text_view->right_window,
3412 if (text_view->top_window)
3413 text_window_size_allocate (text_view->top_window,
3416 if (text_view->bottom_window)
3417 text_window_size_allocate (text_view->bottom_window,
3420 gtk_text_view_update_layout_width (text_view);
3422 /* Note that this will do some layout validation */
3423 gtk_text_view_allocate_children (text_view);
3425 /* Ensure h/v adj exist */
3426 get_hadjustment (text_view);
3427 get_vadjustment (text_view);
3429 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
3430 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
3431 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
3432 text_view->hadjustment->lower = 0;
3433 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
3436 if (text_view->hadjustment->value > text_view->hadjustment->upper - text_view->hadjustment->page_size)
3437 gtk_adjustment_set_value (text_view->hadjustment, MAX (0, text_view->hadjustment->upper - text_view->hadjustment->page_size));
3439 gtk_adjustment_changed (text_view->hadjustment);
3441 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
3442 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
3443 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
3444 text_view->vadjustment->lower = 0;
3445 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
3448 /* Now adjust the value of the adjustment to keep the cursor at the
3449 * same place in the buffer
3451 gtk_text_view_get_first_para_iter (text_view, &first_para);
3452 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
3454 y += text_view->first_para_pixels;
3456 if (y > text_view->vadjustment->upper - text_view->vadjustment->page_size)
3457 y = MAX (0, text_view->vadjustment->upper - text_view->vadjustment->page_size);
3459 if (y != text_view->yoffset)
3460 gtk_adjustment_set_value (text_view->vadjustment, y);
3462 gtk_adjustment_changed (text_view->vadjustment);
3464 /* The GTK resize loop processes all the pending exposes right
3465 * after doing the resize stuff, so the idle sizer won't have a
3466 * chance to run. So we do the work here.
3468 gtk_text_view_flush_first_validate (text_view);
3470 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3473 if (size_changed && GTK_WIDGET_REALIZED (widget))
3474 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3478 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3481 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3482 text_view->first_para_mark);
3486 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3488 GtkWidget *widget = GTK_WIDGET (text_view);
3490 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3492 if (SCREEN_HEIGHT (widget) > 0)
3494 GtkTextIter first_para;
3496 /* Be sure we've validated the stuff onscreen; if we
3497 * scrolled, these calls won't have any effect, because
3498 * they were called in the recursive validate_onscreen
3500 gtk_text_view_get_first_para_iter (text_view, &first_para);
3502 gtk_text_layout_validate_yrange (text_view->layout,
3505 text_view->first_para_pixels +
3506 SCREEN_HEIGHT (widget));
3509 text_view->onscreen_validated = TRUE;
3511 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3513 /* This can have the odd side effect of triggering a scroll, which should
3514 * flip "onscreen_validated" back to FALSE, but should also get us
3515 * back into this function to turn it on again.
3517 gtk_text_view_update_adjustments (text_view);
3519 g_assert (text_view->onscreen_validated);
3523 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3525 if (text_view->first_validate_idle == 0)
3528 /* Do this first, which means that if an "invalidate"
3529 * occurs during any of this process, a new first_validate_callback
3530 * will be installed, and we'll start again.
3532 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3533 g_source_remove (text_view->first_validate_idle);
3534 text_view->first_validate_idle = 0;
3536 /* be sure we have up-to-date screen size set on the
3539 gtk_text_view_update_layout_width (text_view);
3541 /* Bail out if we invalidated stuff; scrolling right away will just
3542 * confuse the issue.
3544 if (text_view->first_validate_idle != 0)
3546 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3550 /* scroll to any marks, if that's pending. This can jump us to
3551 * the validation codepath used for scrolling onscreen, if so we
3552 * bail out. It won't jump if already in that codepath since
3553 * value_changed is not recursive, so also validate if
3556 if (!gtk_text_view_flush_scroll (text_view) ||
3557 !text_view->onscreen_validated)
3558 gtk_text_view_validate_onscreen (text_view);
3560 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3562 g_assert (text_view->onscreen_validated);
3567 first_validate_callback (gpointer data)
3569 GtkTextView *text_view = data;
3571 /* Note that some of this code is duplicated at the end of size_allocate,
3572 * keep in sync with that.
3575 DV(g_print(G_STRLOC"\n"));
3577 gtk_text_view_flush_first_validate (text_view);
3583 incremental_validate_callback (gpointer data)
3585 GtkTextView *text_view = data;
3586 gboolean result = TRUE;
3588 DV(g_print(G_STRLOC"\n"));
3590 gtk_text_layout_validate (text_view->layout, 2000);
3592 gtk_text_view_update_adjustments (text_view);
3594 if (gtk_text_layout_is_valid (text_view->layout))
3596 text_view->incremental_validate_idle = 0;
3604 gtk_text_view_invalidate (GtkTextView *text_view)
3606 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3607 text_view->onscreen_validated));
3609 text_view->onscreen_validated = FALSE;
3611 /* We'll invalidate when the layout is created */
3612 if (text_view->layout == NULL)
3615 if (!text_view->first_validate_idle)
3617 text_view->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3618 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3619 text_view->first_validate_idle));
3622 if (!text_view->incremental_validate_idle)
3624 text_view->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3625 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3626 text_view->incremental_validate_idle));
3631 invalidated_handler (GtkTextLayout *layout,
3634 GtkTextView *text_view;
3636 text_view = GTK_TEXT_VIEW (data);
3638 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3639 gtk_text_view_invalidate (text_view);
3643 changed_handler (GtkTextLayout *layout,
3649 GtkTextView *text_view;
3651 GdkRectangle visible_rect;
3652 GdkRectangle redraw_rect;
3654 text_view = GTK_TEXT_VIEW (data);
3655 widget = GTK_WIDGET (data);
3657 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3659 if (GTK_WIDGET_REALIZED (text_view))
3661 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3663 redraw_rect.x = visible_rect.x;
3664 redraw_rect.width = visible_rect.width;
3665 redraw_rect.y = start_y;
3667 if (old_height == new_height)
3668 redraw_rect.height = old_height;
3669 else if (start_y + old_height > visible_rect.y)
3670 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3672 redraw_rect.height = 0;
3674 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3676 /* text_window_invalidate_rect() takes buffer coordinates */
3677 text_window_invalidate_rect (text_view->text_window,
3680 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3684 redraw_rect.height));
3686 if (text_view->left_window)
3687 text_window_invalidate_rect (text_view->left_window,
3689 if (text_view->right_window)
3690 text_window_invalidate_rect (text_view->right_window,
3692 if (text_view->top_window)
3693 text_window_invalidate_rect (text_view->top_window,
3695 if (text_view->bottom_window)
3696 text_window_invalidate_rect (text_view->bottom_window,
3699 queue_update_im_spot_location (text_view);
3703 if (old_height != new_height)
3705 gboolean yoffset_changed = FALSE;
3707 int new_first_para_top;
3708 int old_first_para_top;
3711 /* If the bottom of the old area was above the top of the
3712 * screen, we need to scroll to keep the current top of the
3713 * screen in place. Remember that first_para_pixels is the
3714 * position of the top of the screen in coordinates relative to
3715 * the first paragraph onscreen.
3717 * In short we are adding the height delta of the portion of the
3718 * changed region above first_para_mark to text_view->yoffset.
3720 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3721 text_view->first_para_mark);
3723 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3725 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3727 if (new_first_para_top != old_first_para_top)
3729 text_view->yoffset += new_first_para_top - old_first_para_top;
3731 get_vadjustment (text_view)->value = text_view->yoffset;
3732 yoffset_changed = TRUE;
3735 if (yoffset_changed)
3737 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3738 gtk_adjustment_value_changed (get_vadjustment (text_view));
3741 /* FIXME be smarter about which anchored widgets we update */
3743 tmp_list = text_view->children;
3744 while (tmp_list != NULL)
3746 GtkTextViewChild *child = tmp_list->data;
3749 gtk_text_view_update_child_allocation (text_view, child);
3751 tmp_list = g_slist_next (tmp_list);
3756 GtkRequisition old_req;
3757 GtkRequisition new_req;
3759 old_req = widget->requisition;
3761 /* Use this instead of gtk_widget_size_request wrapper
3762 * to avoid the optimization which just returns widget->requisition
3763 * if a resize hasn't been queued.
3765 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3767 if (old_req.width != new_req.width ||
3768 old_req.height != new_req.height)
3770 gtk_widget_queue_resize_no_redraw (widget);
3776 gtk_text_view_realize (GtkWidget *widget)
3778 GtkTextView *text_view;
3779 GdkWindowAttr attributes;
3780 gint attributes_mask;
3783 text_view = GTK_TEXT_VIEW (widget);
3784 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3786 attributes.window_type = GDK_WINDOW_CHILD;
3787 attributes.x = widget->allocation.x;
3788 attributes.y = widget->allocation.y;
3789 attributes.width = widget->allocation.width;
3790 attributes.height = widget->allocation.height;
3791 attributes.wclass = GDK_INPUT_OUTPUT;
3792 attributes.visual = gtk_widget_get_visual (widget);
3793 attributes.colormap = gtk_widget_get_colormap (widget);
3794 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3796 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3798 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3799 &attributes, attributes_mask);
3800 gdk_window_set_user_data (widget->window, widget);
3802 /* must come before text_window_realize calls */
3803 widget->style = gtk_style_attach (widget->style, widget->window);
3805 gdk_window_set_background (widget->window,
3806 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3808 text_window_realize (text_view->text_window, widget);
3810 if (text_view->left_window)
3811 text_window_realize (text_view->left_window, widget);
3813 if (text_view->top_window)
3814 text_window_realize (text_view->top_window, widget);
3816 if (text_view->right_window)
3817 text_window_realize (text_view->right_window, widget);
3819 if (text_view->bottom_window)
3820 text_window_realize (text_view->bottom_window, widget);
3822 gtk_text_view_ensure_layout (text_view);
3824 if (text_view->buffer)
3826 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3827 GDK_SELECTION_PRIMARY);
3828 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3831 tmp_list = text_view->children;
3832 while (tmp_list != NULL)
3834 GtkTextViewChild *vc = tmp_list->data;
3836 text_view_child_set_parent_window (text_view, vc);
3838 tmp_list = tmp_list->next;
3841 /* Ensure updating the spot location. */
3842 gtk_text_view_update_im_spot_location (text_view);
3846 gtk_text_view_unrealize (GtkWidget *widget)
3848 GtkTextView *text_view;
3850 text_view = GTK_TEXT_VIEW (widget);
3852 if (text_view->buffer)
3854 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3855 GDK_SELECTION_PRIMARY);
3856 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3859 gtk_text_view_remove_validate_idles (text_view);
3861 if (text_view->popup_menu)
3863 gtk_widget_destroy (text_view->popup_menu);
3864 text_view->popup_menu = NULL;
3867 text_window_unrealize (text_view->text_window);
3869 if (text_view->left_window)
3870 text_window_unrealize (text_view->left_window);
3872 if (text_view->top_window)
3873 text_window_unrealize (text_view->top_window);
3875 if (text_view->right_window)
3876 text_window_unrealize (text_view->right_window);
3878 if (text_view->bottom_window)
3879 text_window_unrealize (text_view->bottom_window);
3881 gtk_text_view_destroy_layout (text_view);
3883 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize (widget);
3887 gtk_text_view_set_background (GtkTextView *text_view)
3889 GtkWidget *widget = GTK_WIDGET (text_view);
3891 gdk_window_set_background (widget->window,
3892 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3894 gdk_window_set_background (text_view->text_window->bin_window,
3895 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3897 if (text_view->left_window)
3898 gdk_window_set_background (text_view->left_window->bin_window,
3899 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3900 if (text_view->right_window)
3901 gdk_window_set_background (text_view->right_window->bin_window,
3902 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3904 if (text_view->top_window)
3905 gdk_window_set_background (text_view->top_window->bin_window,
3906 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3908 if (text_view->bottom_window)
3909 gdk_window_set_background (text_view->bottom_window->bin_window,
3910 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3914 gtk_text_view_style_set (GtkWidget *widget,
3915 GtkStyle *previous_style)
3917 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3918 PangoContext *ltr_context, *rtl_context;
3920 if (GTK_WIDGET_REALIZED (widget))
3922 gtk_text_view_set_background (text_view);
3925 if (text_view->layout && previous_style)
3927 gtk_text_view_set_attributes_from_style (text_view,
3928 text_view->layout->default_style,
3932 ltr_context = gtk_widget_create_pango_context (widget);
3933 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
3934 rtl_context = gtk_widget_create_pango_context (widget);
3935 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
3937 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
3939 g_object_unref (ltr_context);
3940 g_object_unref (rtl_context);
3945 gtk_text_view_direction_changed (GtkWidget *widget,
3946 GtkTextDirection previous_direction)
3948 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3950 if (text_view->layout)
3952 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3954 gtk_text_layout_default_style_changed (text_view->layout);
3959 gtk_text_view_state_changed (GtkWidget *widget,
3960 GtkStateType previous_state)
3962 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3965 if (GTK_WIDGET_REALIZED (widget))
3967 gtk_text_view_set_background (text_view);
3969 if (GTK_WIDGET_IS_SENSITIVE (widget))
3970 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3974 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3977 gdk_cursor_unref (cursor);
3979 text_view->mouse_cursor_obscured = FALSE;
3982 if (!GTK_WIDGET_IS_SENSITIVE (widget))
3984 /* Clear any selection */
3985 gtk_text_view_unselect (text_view);
3988 gtk_widget_queue_draw (widget);
3992 set_invisible_cursor (GdkWindow *window)
3994 GdkDisplay *display;
3997 display = gdk_drawable_get_display (window);
3998 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4000 gdk_window_set_cursor (window, cursor);
4002 gdk_cursor_unref (cursor);
4006 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
4008 if (text_view->mouse_cursor_obscured)
4011 set_invisible_cursor (text_view->text_window->bin_window);
4013 text_view->mouse_cursor_obscured = TRUE;
4017 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
4019 if (text_view->mouse_cursor_obscured)
4023 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
4025 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4026 gdk_cursor_unref (cursor);
4027 text_view->mouse_cursor_obscured = FALSE;
4032 gtk_text_view_grab_notify (GtkWidget *widget,
4033 gboolean was_grabbed)
4037 gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
4038 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
4048 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
4051 switch (event->type)
4053 case GDK_MOTION_NOTIFY:
4054 *x = event->motion.x;
4055 *y = event->motion.y;
4059 case GDK_BUTTON_PRESS:
4060 case GDK_2BUTTON_PRESS:
4061 case GDK_3BUTTON_PRESS:
4062 case GDK_BUTTON_RELEASE:
4063 *x = event->button.x;
4064 *y = event->button.y;
4069 case GDK_KEY_RELEASE:
4070 case GDK_ENTER_NOTIFY:
4071 case GDK_LEAVE_NOTIFY:
4072 case GDK_PROPERTY_NOTIFY:
4073 case GDK_SELECTION_CLEAR:
4074 case GDK_SELECTION_REQUEST:
4075 case GDK_SELECTION_NOTIFY:
4076 case GDK_PROXIMITY_IN:
4077 case GDK_PROXIMITY_OUT:
4078 case GDK_DRAG_ENTER:
4079 case GDK_DRAG_LEAVE:
4080 case GDK_DRAG_MOTION:
4081 case GDK_DRAG_STATUS:
4082 case GDK_DROP_START:
4083 case GDK_DROP_FINISHED:
4093 emit_event_on_tags (GtkWidget *widget,
4099 gboolean retval = FALSE;
4101 tags = gtk_text_iter_get_tags (iter);
4106 GtkTextTag *tag = tmp->data;
4108 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
4114 tmp = g_slist_next (tmp);
4117 g_slist_free (tags);
4123 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
4125 GtkTextView *text_view;
4128 text_view = GTK_TEXT_VIEW (widget);
4130 if (text_view->layout == NULL ||
4131 get_buffer (text_view) == NULL)
4134 if (event->any.window != text_view->text_window->bin_window)
4137 if (get_event_coordinates (event, &x, &y))
4141 x += text_view->xoffset;
4142 y += text_view->yoffset;
4144 /* FIXME this is slow and we do it twice per event.
4145 * My favorite solution is to have GtkTextLayout cache
4146 * the last couple lookups.
4148 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4152 return emit_event_on_tags (widget, event, &iter);
4154 else if (event->type == GDK_KEY_PRESS ||
4155 event->type == GDK_KEY_RELEASE)
4159 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
4160 gtk_text_buffer_get_insert (get_buffer (text_view)));
4162 return emit_event_on_tags (widget, event, &iter);
4169 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
4171 gboolean retval = FALSE;
4172 gboolean obscure = FALSE;
4173 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4174 GtkTextMark *insert;
4176 gboolean can_insert;
4178 if (text_view->layout == NULL ||
4179 get_buffer (text_view) == NULL)
4182 /* Make sure input method knows where it is */
4183 flush_update_im_spot_location (text_view);
4185 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4186 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4187 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
4188 if (gtk_im_context_filter_keypress (text_view->im_context, event))
4190 text_view->need_im_reset = TRUE;
4192 gtk_text_view_reset_im_context (text_view);
4193 obscure = can_insert;
4197 else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4201 /* use overall editability not can_insert, more predictable for users */
4202 else if (text_view->editable &&
4203 (event->keyval == GDK_Return ||
4204 event->keyval == GDK_ISO_Enter ||
4205 event->keyval == GDK_KP_Enter))
4207 /* this won't actually insert the newline if the cursor isn't
4210 gtk_text_view_reset_im_context (text_view);
4211 gtk_text_view_commit_text (text_view, "\n");
4216 /* Pass through Tab as literal tab, unless Control is held down */
4217 else if ((event->keyval == GDK_Tab ||
4218 event->keyval == GDK_KP_Tab ||
4219 event->keyval == GDK_ISO_Left_Tab) &&
4220 !(event->state & GDK_CONTROL_MASK))
4222 /* If the text widget isn't editable overall, or if the application
4223 * has turned off "accepts_tab", move the focus instead
4225 if (text_view->accepts_tab && text_view->editable)
4227 gtk_text_view_reset_im_context (text_view);
4228 gtk_text_view_commit_text (text_view, "\t");
4232 g_signal_emit_by_name (text_view, "move-focus",
4233 (event->state & GDK_SHIFT_MASK) ?
4234 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4242 gtk_text_view_obscure_mouse_cursor (text_view);
4244 gtk_text_view_reset_blink_time (text_view);
4245 gtk_text_view_pend_cursor_blink (text_view);
4251 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4253 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4254 GtkTextMark *insert;
4257 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
4260 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4261 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4262 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
4263 gtk_im_context_filter_keypress (text_view->im_context, event))
4265 text_view->need_im_reset = TRUE;
4269 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4273 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4275 GtkTextView *text_view;
4277 text_view = GTK_TEXT_VIEW (widget);
4279 gtk_widget_grab_focus (widget);
4281 if (event->window != text_view->text_window->bin_window)
4283 /* Remove selection if any. */
4284 gtk_text_view_unselect (text_view);
4288 gtk_text_view_reset_blink_time (text_view);
4292 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4293 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4294 else if (event->button == 3)
4295 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4298 if (event->type == GDK_BUTTON_PRESS)
4300 gtk_text_view_reset_im_context (text_view);
4302 if (event->button == 1)
4304 /* If we're in the selection, start a drag copy/move of the
4305 * selection; otherwise, start creating a new selection.
4308 GtkTextIter start, end;
4310 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4312 event->x + text_view->xoffset,
4313 event->y + text_view->yoffset);
4315 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4317 gtk_text_iter_in_range (&iter, &start, &end) &&
4318 !(event->state & GDK_SHIFT_MASK))
4320 text_view->drag_start_x = event->x;
4321 text_view->drag_start_y = event->y;
4322 text_view->pending_place_cursor_button = event->button;
4326 gtk_text_view_start_selection_drag (text_view, &iter, event);
4331 else if (event->button == 2)
4335 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4337 event->x + text_view->xoffset,
4338 event->y + text_view->yoffset);
4340 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4341 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4343 text_view->editable);
4346 else if (event->button == 3)
4348 gtk_text_view_do_popup (text_view, event);
4352 else if ((event->type == GDK_2BUTTON_PRESS ||
4353 event->type == GDK_3BUTTON_PRESS) &&
4358 gtk_text_view_end_selection_drag (text_view);
4360 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4362 event->x + text_view->xoffset,
4363 event->y + text_view->yoffset);
4365 gtk_text_view_start_selection_drag (text_view, &iter, event);
4373 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4375 GtkTextView *text_view;
4377 text_view = GTK_TEXT_VIEW (widget);
4379 if (event->window != text_view->text_window->bin_window)
4382 if (event->button == 1)
4384 if (text_view->drag_start_x >= 0)
4386 text_view->drag_start_x = -1;
4387 text_view->drag_start_y = -1;
4390 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4392 else if (text_view->pending_place_cursor_button == event->button)
4396 /* Unselect everything; we clicked inside selection, but
4397 * didn't move by the drag threshold, so just clear selection
4400 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4402 event->x + text_view->xoffset,
4403 event->y + text_view->yoffset);
4405 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4406 gtk_text_view_check_cursor_blink (text_view);
4408 text_view->pending_place_cursor_button = 0;
4418 keymap_direction_changed (GdkKeymap *keymap,
4419 GtkTextView *text_view)
4421 gtk_text_view_check_keymap_direction (text_view);
4425 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4427 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4429 gtk_widget_queue_draw (widget);
4431 DV(g_print (G_STRLOC": focus_in_event\n"));
4433 gtk_text_view_reset_blink_time (text_view);
4435 if (text_view->cursor_visible && text_view->layout)
4437 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4438 gtk_text_view_check_cursor_blink (text_view);
4441 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4442 "direction-changed",
4443 G_CALLBACK (keymap_direction_changed), text_view);
4444 gtk_text_view_check_keymap_direction (text_view);
4446 if (text_view->editable)
4448 text_view->need_im_reset = TRUE;
4449 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4456 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4458 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4460 gtk_text_view_end_selection_drag (text_view);
4462 gtk_widget_queue_draw (widget);
4464 DV(g_print (G_STRLOC": focus_out_event\n"));
4466 if (text_view->cursor_visible && text_view->layout)
4468 gtk_text_view_check_cursor_blink (text_view);
4469 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4472 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4473 keymap_direction_changed,
4476 if (text_view->editable)
4478 text_view->need_im_reset = TRUE;
4479 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4486 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4488 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4490 gtk_text_view_unobscure_mouse_cursor (text_view);
4492 if (event->window == text_view->text_window->bin_window &&
4493 text_view->drag_start_x >= 0)
4498 gdk_event_request_motions (event);
4500 if (gtk_drag_check_threshold (widget,
4501 text_view->drag_start_x,
4502 text_view->drag_start_y,
4506 gint buffer_x, buffer_y;
4508 gtk_text_view_window_to_buffer_coords (text_view,
4509 GTK_TEXT_WINDOW_TEXT,
4510 text_view->drag_start_x,
4511 text_view->drag_start_y,
4515 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4517 buffer_x, buffer_y);
4519 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4528 gtk_text_view_paint (GtkWidget *widget,
4530 GdkEventExpose *event)
4532 GtkTextView *text_view;
4533 GList *child_exposes;
4536 text_view = GTK_TEXT_VIEW (widget);
4538 g_return_if_fail (text_view->layout != NULL);
4539 g_return_if_fail (text_view->xoffset >= 0);
4540 g_return_if_fail (text_view->yoffset >= 0);
4542 while (text_view->first_validate_idle != 0)
4544 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4545 text_view->first_validate_idle));
4546 gtk_text_view_flush_first_validate (text_view);
4549 if (!text_view->onscreen_validated)
4551 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.");
4552 g_assert_not_reached ();
4556 printf ("painting %d,%d %d x %d\n",
4558 area->width, area->height);
4561 child_exposes = NULL;
4562 gtk_text_layout_draw (text_view->layout,
4564 text_view->text_window->bin_window,
4569 area->width, area->height,
4572 tmp_list = child_exposes;
4573 while (tmp_list != NULL)
4575 GtkWidget *child = tmp_list->data;
4577 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4581 g_object_unref (child);
4583 tmp_list = tmp_list->next;
4586 g_list_free (child_exposes);
4590 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4594 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4595 GTK_TEXT_WINDOW_TEXT))
4597 DV(g_print (">Exposed ("G_STRLOC")\n"));
4598 gtk_text_view_paint (widget, &event->area, event);
4601 if (event->window == widget->window)
4602 gtk_text_view_draw_focus (widget);
4604 /* Propagate exposes to all unanchored children.
4605 * Anchored children are handled in gtk_text_view_paint().
4607 tmp_list = GTK_TEXT_VIEW (widget)->children;
4608 while (tmp_list != NULL)
4610 GtkTextViewChild *vc = tmp_list->data;
4612 /* propagate_expose checks that event->window matches
4616 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4620 tmp_list = tmp_list->next;
4627 gtk_text_view_draw_focus (GtkWidget *widget)
4629 gboolean interior_focus;
4631 /* We clear the focus if we are in interior focus mode. */
4632 gtk_widget_style_get (widget,
4633 "interior-focus", &interior_focus,
4636 if (GTK_WIDGET_DRAWABLE (widget))
4638 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4640 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4641 NULL, widget, "textview",
4643 widget->allocation.width,
4644 widget->allocation.height);
4648 gdk_window_clear (widget->window);
4654 gtk_text_view_focus (GtkWidget *widget,
4655 GtkDirectionType direction)
4657 GtkContainer *container;
4660 container = GTK_CONTAINER (widget);
4662 if (!gtk_widget_is_focus (widget) &&
4663 container->focus_child == NULL)
4665 gtk_widget_grab_focus (widget);
4671 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4672 * children to get the focus
4674 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4675 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4676 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4683 gtk_text_view_move_focus (GtkWidget *widget,
4684 GtkDirectionType direction_type)
4686 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4688 if (GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus)
4689 GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus (text_view,
4698 gtk_text_view_add (GtkContainer *container,
4701 /* This is pretty random. */
4702 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4704 GTK_TEXT_WINDOW_WIDGET,
4709 gtk_text_view_remove (GtkContainer *container,
4713 GtkTextView *text_view;
4714 GtkTextViewChild *vc;
4716 text_view = GTK_TEXT_VIEW (container);
4719 iter = text_view->children;
4721 while (iter != NULL)
4725 if (vc->widget == child)
4728 iter = g_slist_next (iter);
4731 g_assert (iter != NULL); /* be sure we had the child in the list */
4733 text_view->children = g_slist_remove (text_view->children, vc);
4735 gtk_widget_unparent (vc->widget);
4737 text_view_child_free (vc);
4741 gtk_text_view_forall (GtkContainer *container,
4742 gboolean include_internals,
4743 GtkCallback callback,
4744 gpointer callback_data)
4747 GtkTextView *text_view;
4750 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4751 g_return_if_fail (callback != NULL);
4753 text_view = GTK_TEXT_VIEW (container);
4755 copy = g_slist_copy (text_view->children);
4758 while (iter != NULL)
4760 GtkTextViewChild *vc = iter->data;
4762 (* callback) (vc->widget, callback_data);
4764 iter = g_slist_next (iter);
4767 g_slist_free (copy);
4770 #define CURSOR_ON_MULTIPLIER 2
4771 #define CURSOR_OFF_MULTIPLIER 1
4772 #define CURSOR_PEND_MULTIPLIER 3
4773 #define CURSOR_DIVIDER 3
4776 cursor_blinks (GtkTextView *text_view)
4778 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4781 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4784 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4787 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4792 if (text_view->editable)
4794 GtkTextMark *insert;
4797 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4798 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4800 if (gtk_text_iter_editable (&iter, text_view->editable))
4808 get_cursor_time (GtkTextView *text_view)
4810 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4813 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
4819 get_cursor_blink_timeout (GtkTextView *text_view)
4821 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4824 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
4835 blink_cb (gpointer data)
4837 GtkTextView *text_view;
4838 GtkTextViewPrivate *priv;
4842 text_view = GTK_TEXT_VIEW (data);
4843 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4845 if (!GTK_WIDGET_HAS_FOCUS (text_view))
4847 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
4848 "connect a handler to this signal, it must return\n"
4849 "FALSE so the text view gets the event as well");
4851 gtk_text_view_check_cursor_blink (text_view);
4856 g_assert (text_view->layout);
4857 g_assert (text_view->cursor_visible);
4859 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4861 blink_timeout = get_cursor_blink_timeout (text_view);
4862 if (priv->blink_time > 1000 * blink_timeout &&
4863 blink_timeout < G_MAXINT/1000)
4865 /* we've blinked enough without the user doing anything, stop blinking */
4867 text_view->blink_timeout = 0;
4870 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4875 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
4878 priv->blink_time += get_cursor_time (text_view);
4881 /* Block changed_handler while changing the layout's cursor visibility
4882 * because it would expose the whole paragraph. Instead, we expose
4883 * the cursor's area(s) manually below.
4885 g_signal_handlers_block_by_func (text_view->layout,
4888 gtk_text_layout_set_cursor_visible (text_view->layout, !visible);
4889 g_signal_handlers_unblock_by_func (text_view->layout,
4893 text_window_invalidate_cursors (text_view->text_window);
4895 /* Remove ourselves */
4901 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4903 if (text_view->blink_timeout)
4905 g_source_remove (text_view->blink_timeout);
4906 text_view->blink_timeout = 0;
4911 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4913 if (text_view->layout != NULL &&
4914 text_view->cursor_visible &&
4915 GTK_WIDGET_HAS_FOCUS (text_view))
4917 if (cursor_blinks (text_view))
4919 if (text_view->blink_timeout == 0)
4921 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4923 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4930 gtk_text_view_stop_cursor_blink (text_view);
4931 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4936 gtk_text_view_stop_cursor_blink (text_view);
4937 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4942 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
4944 if (text_view->layout != NULL &&
4945 text_view->cursor_visible &&
4946 GTK_WIDGET_HAS_FOCUS (text_view) &&
4947 cursor_blinks (text_view))
4949 gtk_text_view_stop_cursor_blink (text_view);
4950 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4952 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
4959 gtk_text_view_reset_blink_time (GtkTextView *text_view)
4961 GtkTextViewPrivate *priv;
4963 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4965 priv->blink_time = 0;
4970 * Key binding handlers
4974 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4975 GtkTextIter *newplace,
4978 gboolean ret = TRUE;
4982 ret = gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4988 ret = gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4996 move_cursor (GtkTextView *text_view,
4997 const GtkTextIter *new_location,
4998 gboolean extend_selection)
5000 if (extend_selection)
5001 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
5005 gtk_text_buffer_place_cursor (get_buffer (text_view),
5007 gtk_text_view_check_cursor_blink (text_view);
5011 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
5012 GtkMovementStep step,
5014 gboolean extend_selection)
5017 GtkTextIter newplace;
5018 gint cursor_x_pos = 0;
5019 GtkDirectionType leave_direction = -1;
5021 if (!text_view->cursor_visible)
5023 GtkScrollStep scroll_step;
5027 case GTK_MOVEMENT_VISUAL_POSITIONS:
5028 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
5030 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5031 case GTK_MOVEMENT_WORDS:
5032 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
5034 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5035 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
5037 case GTK_MOVEMENT_DISPLAY_LINES:
5038 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
5040 case GTK_MOVEMENT_PARAGRAPHS:
5041 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5042 scroll_step = GTK_SCROLL_STEPS;
5044 case GTK_MOVEMENT_PAGES:
5045 scroll_step = GTK_SCROLL_PAGES;
5047 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5048 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
5050 case GTK_MOVEMENT_BUFFER_ENDS:
5051 scroll_step = GTK_SCROLL_ENDS;
5054 scroll_step = GTK_SCROLL_PAGES;
5058 if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
5060 if (leave_direction != -1 &&
5061 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5064 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5071 gtk_text_view_reset_im_context (text_view);
5073 if (step == GTK_MOVEMENT_PAGES)
5075 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
5076 gtk_widget_error_bell (GTK_WIDGET (text_view));
5078 gtk_text_view_check_cursor_blink (text_view);
5079 gtk_text_view_pend_cursor_blink (text_view);
5082 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
5084 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
5085 gtk_widget_error_bell (GTK_WIDGET (text_view));
5087 gtk_text_view_check_cursor_blink (text_view);
5088 gtk_text_view_pend_cursor_blink (text_view);
5092 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5093 gtk_text_buffer_get_insert (get_buffer (text_view)));
5096 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5097 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
5101 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5102 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5105 case GTK_MOVEMENT_VISUAL_POSITIONS:
5106 gtk_text_layout_move_iter_visually (text_view->layout,
5110 case GTK_MOVEMENT_WORDS:
5112 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5115 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5116 gtk_text_iter_forward_to_line_end (&newplace);
5120 case GTK_MOVEMENT_DISPLAY_LINES:
5123 leave_direction = GTK_DIR_UP;
5125 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5126 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5128 gtk_text_iter_set_line_offset (&newplace, 0);
5132 leave_direction = GTK_DIR_DOWN;
5134 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5135 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5137 gtk_text_iter_forward_to_line_end (&newplace);
5141 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5143 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5144 else if (count < -1)
5145 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5148 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
5151 case GTK_MOVEMENT_PARAGRAPHS:
5154 if (!gtk_text_iter_ends_line (&newplace))
5156 gtk_text_iter_forward_to_line_end (&newplace);
5159 gtk_text_iter_forward_visible_lines (&newplace, count);
5160 gtk_text_iter_forward_to_line_end (&newplace);
5164 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5165 gtk_text_iter_set_line_offset (&newplace, 0);
5166 gtk_text_iter_forward_visible_lines (&newplace, count);
5167 gtk_text_iter_set_line_offset (&newplace, 0);
5171 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5174 if (!gtk_text_iter_ends_line (&newplace))
5175 gtk_text_iter_forward_to_line_end (&newplace);
5179 gtk_text_iter_set_line_offset (&newplace, 0);
5183 case GTK_MOVEMENT_BUFFER_ENDS:
5185 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5187 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5194 /* call move_cursor() even if the cursor hasn't moved, since it
5195 cancels the selection
5197 move_cursor (text_view, &newplace, extend_selection);
5199 if (!gtk_text_iter_equal (&insert, &newplace))
5201 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5202 gtk_text_view_scroll_mark_onscreen (text_view,
5203 gtk_text_buffer_get_insert (get_buffer (text_view)));
5205 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5206 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5208 else if (leave_direction != -1)
5210 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5213 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5218 gtk_widget_error_bell (GTK_WIDGET (text_view));
5221 gtk_text_view_check_cursor_blink (text_view);
5222 gtk_text_view_pend_cursor_blink (text_view);
5226 gtk_text_view_move_cursor (GtkTextView *text_view,
5227 GtkMovementStep step,
5229 gboolean extend_selection)
5231 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5235 gtk_text_view_page_horizontally (GtkTextView *text_view,
5237 gboolean extend_selection)
5239 gtk_text_view_move_cursor_internal (text_view, GTK_MOVEMENT_HORIZONTAL_PAGES,
5240 count, extend_selection);
5245 gtk_text_view_move_viewport (GtkTextView *text_view,
5249 GtkAdjustment *adjustment;
5254 case GTK_SCROLL_STEPS:
5255 case GTK_SCROLL_PAGES:
5256 case GTK_SCROLL_ENDS:
5257 adjustment = get_vadjustment (text_view);
5259 case GTK_SCROLL_HORIZONTAL_STEPS:
5260 case GTK_SCROLL_HORIZONTAL_PAGES:
5261 case GTK_SCROLL_HORIZONTAL_ENDS:
5262 adjustment = get_hadjustment (text_view);
5265 adjustment = get_vadjustment (text_view);
5271 case GTK_SCROLL_STEPS:
5272 case GTK_SCROLL_HORIZONTAL_STEPS:
5273 increment = adjustment->step_increment;
5275 case GTK_SCROLL_PAGES:
5276 case GTK_SCROLL_HORIZONTAL_PAGES:
5277 increment = adjustment->page_increment;
5279 case GTK_SCROLL_ENDS:
5280 case GTK_SCROLL_HORIZONTAL_ENDS:
5281 increment = adjustment->upper - adjustment->lower;
5288 return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
5292 gtk_text_view_set_anchor (GtkTextView *text_view)
5296 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5297 gtk_text_buffer_get_insert (get_buffer (text_view)));
5299 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5303 gtk_text_view_scroll_pages (GtkTextView *text_view,
5305 gboolean extend_selection)
5310 gint cursor_x_pos, cursor_y_pos;
5311 GtkTextMark *insert_mark;
5312 GtkTextIter old_insert;
5313 GtkTextIter new_insert;
5317 g_return_val_if_fail (text_view->vadjustment != NULL, FALSE);
5319 adj = text_view->vadjustment;
5321 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5323 /* Make sure we start from the current cursor position, even
5324 * if it was offscreen, but don't queue more scrolls if we're
5327 if (text_view->pending_scroll)
5328 cancel_pending_scroll (text_view);
5330 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5332 /* Validate the region that will be brought into view by the cursor motion
5334 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5335 &old_insert, insert_mark);
5339 gtk_text_view_get_first_para_iter (text_view, &anchor);
5340 y0 = adj->page_size;
5341 y1 = adj->page_size + count * adj->page_increment;
5345 gtk_text_view_get_first_para_iter (text_view, &anchor);
5346 y0 = count * adj->page_increment + adj->page_size;
5350 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
5351 /* FIXME do we need to update the adjustment ranges here? */
5353 new_insert = old_insert;
5355 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5357 /* already at top, just be sure we are at offset 0 */
5358 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5359 move_cursor (text_view, &new_insert, extend_selection);
5361 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5363 /* already at bottom, just be sure we are at the end */
5364 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5365 move_cursor (text_view, &new_insert, extend_selection);
5369 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5371 oldval = adj->value;
5372 newval = adj->value;
5374 newval += count * adj->page_increment;
5376 set_adjustment_clamped (adj, newval);
5377 cursor_y_pos += adj->value - oldval;
5379 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5380 clamp_iter_onscreen (text_view, &new_insert);
5381 move_cursor (text_view, &new_insert, extend_selection);
5383 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5386 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5387 * only guarantees 1 pixel onscreen.
5389 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5390 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5392 return !gtk_text_iter_equal (&old_insert, &new_insert);
5396 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5398 gboolean extend_selection)
5403 gint cursor_x_pos, cursor_y_pos;
5404 GtkTextMark *insert_mark;
5405 GtkTextIter old_insert;
5406 GtkTextIter new_insert;
5409 g_return_val_if_fail (text_view->hadjustment != NULL, FALSE);
5411 adj = text_view->hadjustment;
5413 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5415 /* Make sure we start from the current cursor position, even
5416 * if it was offscreen, but don't queue more scrolls if we're
5419 if (text_view->pending_scroll)
5420 cancel_pending_scroll (text_view);
5422 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5424 /* Validate the line that we're moving within.
5426 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5427 &old_insert, insert_mark);
5429 gtk_text_layout_get_line_yrange (text_view->layout, &old_insert, &y, &height);
5430 gtk_text_layout_validate_yrange (text_view->layout, &old_insert, y, y + height);
5431 /* FIXME do we need to update the adjustment ranges here? */
5433 new_insert = old_insert;
5435 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5437 /* already at far left, just be sure we are at offset 0 */
5438 gtk_text_iter_set_line_offset (&new_insert, 0);
5439 move_cursor (text_view, &new_insert, extend_selection);
5441 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5443 /* already at far right, just be sure we are at the end */
5444 if (!gtk_text_iter_ends_line (&new_insert))
5445 gtk_text_iter_forward_to_line_end (&new_insert);
5446 move_cursor (text_view, &new_insert, extend_selection);
5450 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5452 oldval = adj->value;
5453 newval = adj->value;
5455 newval += count * adj->page_increment;
5457 set_adjustment_clamped (adj, newval);
5458 cursor_x_pos += adj->value - oldval;
5460 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5461 clamp_iter_onscreen (text_view, &new_insert);
5462 move_cursor (text_view, &new_insert, extend_selection);
5464 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5467 /* FIXME for lines shorter than the overall widget width, this results in a
5468 * "bounce" effect as we scroll to the right of the widget, then scroll
5469 * back to get the end of the line onscreen.
5470 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5473 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5474 * only guarantees 1 pixel onscreen.
5476 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5477 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5479 return !gtk_text_iter_equal (&old_insert, &new_insert);
5483 whitespace (gunichar ch, gpointer user_data)
5485 return (ch == ' ' || ch == '\t');
5489 not_whitespace (gunichar ch, gpointer user_data)
5491 return !whitespace (ch, user_data);
5495 find_whitepace_region (const GtkTextIter *center,
5496 GtkTextIter *start, GtkTextIter *end)
5501 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5502 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5503 if (whitespace (gtk_text_iter_get_char (end), NULL))
5504 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5506 return !gtk_text_iter_equal (start, end);
5510 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5513 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5514 text_view->editable))
5516 gtk_widget_error_bell (GTK_WIDGET (text_view));
5521 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5528 gboolean leave_one = FALSE;
5530 gtk_text_view_reset_im_context (text_view);
5532 if (type == GTK_DELETE_CHARS)
5534 /* Char delete deletes the selection, if one exists */
5535 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5536 text_view->editable))
5540 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5541 gtk_text_buffer_get_insert (get_buffer (text_view)));
5548 case GTK_DELETE_CHARS:
5549 gtk_text_iter_forward_cursor_positions (&end, count);
5552 case GTK_DELETE_WORD_ENDS:
5554 gtk_text_iter_forward_word_ends (&end, count);
5556 gtk_text_iter_backward_word_starts (&start, 0 - count);
5559 case GTK_DELETE_WORDS:
5562 case GTK_DELETE_DISPLAY_LINE_ENDS:
5565 case GTK_DELETE_DISPLAY_LINES:
5568 case GTK_DELETE_PARAGRAPH_ENDS:
5571 /* If we're already at a newline, we need to
5572 * simply delete that newline, instead of
5573 * moving to the next one.
5575 if (gtk_text_iter_ends_line (&end))
5577 gtk_text_iter_forward_line (&end);
5583 if (!gtk_text_iter_forward_to_line_end (&end))
5591 if (gtk_text_iter_starts_line (&start))
5593 gtk_text_iter_backward_line (&start);
5594 if (!gtk_text_iter_ends_line (&end))
5595 gtk_text_iter_forward_to_line_end (&start);
5599 gtk_text_iter_set_line_offset (&start, 0);
5603 gtk_text_iter_backward_lines (&start, -count);
5607 case GTK_DELETE_PARAGRAPHS:
5610 gtk_text_iter_set_line_offset (&start, 0);
5611 gtk_text_iter_forward_to_line_end (&end);
5613 /* Do the lines beyond the first. */
5616 gtk_text_iter_forward_to_line_end (&end);
5622 /* FIXME negative count? */
5626 case GTK_DELETE_WHITESPACE:
5628 find_whitepace_region (&insert, &start, &end);
5636 if (!gtk_text_iter_equal (&start, &end))
5638 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5640 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5641 text_view->editable))
5644 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5646 text_view->editable);
5650 gtk_widget_error_bell (GTK_WIDGET (text_view));
5653 gtk_text_buffer_end_user_action (get_buffer (text_view));
5654 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5656 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5657 gtk_text_view_scroll_mark_onscreen (text_view,
5658 gtk_text_buffer_get_insert (get_buffer (text_view)));
5662 gtk_widget_error_bell (GTK_WIDGET (text_view));
5667 gtk_text_view_backspace (GtkTextView *text_view)
5671 gtk_text_view_reset_im_context (text_view);
5673 /* Backspace deletes the selection, if one exists */
5674 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5675 text_view->editable))
5678 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5680 gtk_text_buffer_get_insert (get_buffer (text_view)));
5682 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5683 TRUE, text_view->editable))
5685 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5686 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5687 gtk_text_view_scroll_mark_onscreen (text_view,
5688 gtk_text_buffer_get_insert (get_buffer (text_view)));
5692 gtk_widget_error_bell (GTK_WIDGET (text_view));
5697 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5699 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5700 GDK_SELECTION_CLIPBOARD);
5702 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5704 text_view->editable);
5705 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5706 gtk_text_view_scroll_mark_onscreen (text_view,
5707 gtk_text_buffer_get_insert (get_buffer (text_view)));
5711 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5713 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5714 GDK_SELECTION_CLIPBOARD);
5716 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5719 /* on copy do not scroll, we are already onscreen */
5723 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5725 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5726 GDK_SELECTION_CLIPBOARD);
5728 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5731 text_view->editable);
5735 gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
5736 GtkClipboard *clipboard,
5739 GtkTextView *text_view = data;
5740 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5741 gtk_text_view_scroll_mark_onscreen (text_view, gtk_text_buffer_get_insert (buffer));
5745 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5747 if (text_view->text_window)
5748 text_window_invalidate_cursors (text_view->text_window);
5750 text_view->overwrite_mode = !text_view->overwrite_mode;
5752 if (text_view->layout)
5753 gtk_text_layout_set_overwrite_mode (text_view->layout,
5754 text_view->overwrite_mode && text_view->editable);
5756 if (text_view->text_window)
5757 text_window_invalidate_cursors (text_view->text_window);
5759 gtk_text_view_pend_cursor_blink (text_view);
5761 g_object_notify (G_OBJECT (text_view), "overwrite");
5765 * gtk_text_view_get_overwrite:
5766 * @text_view: a #GtkTextView
5768 * Returns whether the #GtkTextView is in overwrite mode or not.
5770 * Return value: whether @text_view is in overwrite mode or not.
5775 gtk_text_view_get_overwrite (GtkTextView *text_view)
5777 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5779 return text_view->overwrite_mode;
5783 * gtk_text_view_set_overwrite:
5784 * @text_view: a #GtkTextView
5785 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
5787 * Changes the #GtkTextView overwrite mode.
5792 gtk_text_view_set_overwrite (GtkTextView *text_view,
5795 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5796 overwrite = overwrite != FALSE;
5798 if (text_view->overwrite_mode != overwrite)
5799 gtk_text_view_toggle_overwrite (text_view);
5803 * gtk_text_view_set_accepts_tab:
5804 * @text_view: A #GtkTextView
5805 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
5806 * character, %FALSE, if pressing the Tab key should move the
5809 * Sets the behavior of the text widget when the Tab key is pressed.
5810 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
5811 * is %FALSE the keyboard focus is moved to the next widget in the focus
5817 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
5818 gboolean accepts_tab)
5820 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5822 accepts_tab = accepts_tab != FALSE;
5824 if (text_view->accepts_tab != accepts_tab)
5826 text_view->accepts_tab = accepts_tab;
5828 g_object_notify (G_OBJECT (text_view), "accepts-tab");
5833 * gtk_text_view_get_accepts_tab:
5834 * @text_view: A #GtkTextView
5836 * Returns whether pressing the Tab key inserts a tab characters.
5837 * gtk_text_view_set_accepts_tab().
5839 * Return value: %TRUE if pressing the Tab key inserts a tab character,
5840 * %FALSE if pressing the Tab key moves the keyboard focus.
5845 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
5847 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5849 return text_view->accepts_tab;
5853 gtk_text_view_compat_move_focus (GtkTextView *text_view,
5854 GtkDirectionType direction_type)
5856 GSignalInvocationHint *hint = g_signal_get_invocation_hint (text_view);
5858 /* as of GTK+ 2.12, the "move-focus" signal has been moved to GtkWidget,
5859 * the evil code below makes sure that both emitting the signal and
5860 * calling the virtual function directly continue to work as expetcted
5863 if (hint->signal_id == g_signal_lookup ("move-focus", GTK_TYPE_WIDGET))
5865 /* if this is a signal emission, chain up */
5869 g_signal_chain_from_overridden_handler (text_view,
5870 direction_type, &retval);
5874 /* otherwise emit the signal, since somebody called the virtual
5878 g_signal_emit_by_name (text_view, "move-focus", direction_type);
5887 gtk_text_view_unselect (GtkTextView *text_view)
5891 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5892 gtk_text_buffer_get_insert (get_buffer (text_view)));
5894 gtk_text_buffer_move_mark (get_buffer (text_view),
5895 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
5900 get_iter_at_pointer (GtkTextView *text_view,
5905 gint xcoord, ycoord;
5906 GdkModifierType state;
5908 gdk_window_get_pointer (text_view->text_window->bin_window,
5909 &xcoord, &ycoord, &state);
5911 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5913 xcoord + text_view->xoffset,
5914 ycoord + text_view->yoffset);
5923 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5924 const gchar *mark_name)
5926 GtkTextIter newplace;
5929 get_iter_at_pointer (text_view, &newplace, NULL, NULL);
5931 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5933 /* This may invalidate the layout */
5934 DV(g_print (G_STRLOC": move mark\n"));
5936 gtk_text_buffer_move_mark (get_buffer (text_view),
5940 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5941 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5943 DV (g_print ("first validate idle leaving %s is %d\n",
5944 G_STRLOC, text_view->first_validate_idle));
5948 selection_scan_timeout (gpointer data)
5950 GtkTextView *text_view;
5952 text_view = GTK_TEXT_VIEW (data);
5954 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5955 gtk_text_view_scroll_mark_onscreen (text_view,
5956 gtk_text_buffer_get_insert (get_buffer (text_view)));
5958 return TRUE; /* remain installed. */
5961 #define UPPER_OFFSET_ANCHOR 0.8
5962 #define LOWER_OFFSET_ANCHOR 0.2
5965 check_scroll (gdouble offset, GtkAdjustment *adj)
5967 if ((offset > UPPER_OFFSET_ANCHOR &&
5968 adj->value + adj->page_size < adj->upper) ||
5969 (offset < LOWER_OFFSET_ANCHOR &&
5970 adj->value > adj->lower))
5977 drag_scan_timeout (gpointer data)
5979 GtkTextView *text_view;
5980 GtkTextIter newplace;
5981 gint x, y, width, height;
5982 gdouble pointer_xoffset, pointer_yoffset;
5984 text_view = GTK_TEXT_VIEW (data);
5986 get_iter_at_pointer (text_view, &newplace, &x, &y);
5987 gdk_drawable_get_size (text_view->text_window->bin_window, &width, &height);
5989 gtk_text_buffer_move_mark (get_buffer (text_view),
5990 text_view->dnd_mark,
5993 pointer_xoffset = (gdouble) x / width;
5994 pointer_yoffset = (gdouble) y / height;
5996 if (check_scroll (pointer_xoffset, text_view->hadjustment) ||
5997 check_scroll (pointer_yoffset, text_view->vadjustment))
5999 /* do not make offsets surpass lower nor upper anchors, this makes
6000 * scrolling speed relative to the distance of the pointer to the
6001 * anchors when it moves beyond them.
6003 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6004 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6006 gtk_text_view_scroll_to_mark (text_view,
6007 text_view->dnd_mark,
6008 0., TRUE, pointer_xoffset, pointer_yoffset);
6019 } SelectionGranularity;
6022 * Move @start and @end to the boundaries of the selection unit (indicated by
6023 * @granularity) which contained @start initially.
6024 * If the selction unit is SELECT_WORDS and @start is not contained in a word
6025 * the selection is extended to all the white spaces between the end of the
6026 * word preceding @start and the start of the one following.
6029 extend_selection (GtkTextView *text_view,
6030 SelectionGranularity granularity,
6036 if (granularity == SELECT_WORDS)
6038 if (gtk_text_iter_inside_word (start))
6040 if (!gtk_text_iter_starts_word (start))
6041 gtk_text_iter_backward_visible_word_start (start);
6043 if (!gtk_text_iter_ends_word (end))
6045 if (!gtk_text_iter_forward_visible_word_end (end))
6046 gtk_text_iter_forward_to_end (end);
6054 if (gtk_text_iter_backward_visible_word_start (&tmp))
6055 gtk_text_iter_forward_visible_word_end (&tmp);
6057 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
6060 gtk_text_iter_set_line_offset (start, 0);
6063 if (!gtk_text_iter_forward_visible_word_end (&tmp))
6064 gtk_text_iter_forward_to_end (&tmp);
6066 if (gtk_text_iter_ends_word (&tmp))
6067 gtk_text_iter_backward_visible_word_start (&tmp);
6069 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
6072 gtk_text_iter_forward_to_line_end (end);
6075 else if (granularity == SELECT_LINES)
6077 if (gtk_text_view_starts_display_line (text_view, start))
6079 /* If on a display line boundary, we assume the user
6080 * clicked off the end of a line and we therefore select
6081 * the line before the boundary.
6083 gtk_text_view_backward_display_line_start (text_view, start);
6087 /* start isn't on the start of a line, so we move it to the
6088 * start, and move end to the end unless it's already there.
6090 gtk_text_view_backward_display_line_start (text_view, start);
6092 if (!gtk_text_view_starts_display_line (text_view, end))
6093 gtk_text_view_forward_display_line_end (text_view, end);
6101 SelectionGranularity granularity;
6102 GtkTextMark *orig_start;
6103 GtkTextMark *orig_end;
6107 selection_data_free (SelectionData *data)
6109 if (data->orig_start != NULL)
6110 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6112 if (data->orig_end != NULL)
6113 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6119 selection_motion_event_handler (GtkTextView *text_view,
6120 GdkEventMotion *event,
6121 SelectionData *data)
6123 gdk_event_request_motions (event);
6125 if (data->granularity == SELECT_CHARACTERS)
6127 move_mark_to_pointer_and_scroll (text_view, "insert");
6131 GtkTextIter cursor, start, end;
6132 GtkTextIter orig_start, orig_end;
6133 GtkTextBuffer *buffer;
6135 buffer = get_buffer (text_view);
6137 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6138 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6140 get_iter_at_pointer (text_view, &cursor, NULL, NULL);
6143 extend_selection (text_view, data->granularity, &start, &end);
6145 /* either the selection extends to the front, or end (or not) */
6146 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6147 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6149 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6151 gtk_text_view_scroll_mark_onscreen (text_view,
6152 gtk_text_buffer_get_insert (buffer));
6155 /* If we had to scroll offscreen, insert a timeout to do so
6156 * again. Note that in the timeout, even if the mouse doesn't
6157 * move, due to this scroll xoffset/yoffset will have changed
6158 * and we'll need to scroll again.
6160 if (text_view->scroll_timeout != 0) /* reset on every motion event */
6161 g_source_remove (text_view->scroll_timeout);
6163 text_view->scroll_timeout =
6164 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6170 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6171 const GtkTextIter *iter,
6172 GdkEventButton *button)
6174 GtkTextIter cursor, ins, bound;
6175 GtkTextIter orig_start, orig_end;
6176 GtkTextBuffer *buffer;
6177 SelectionData *data;
6179 if (text_view->selection_drag_handler != 0)
6182 data = g_new0 (SelectionData, 1);
6184 if (button->type == GDK_2BUTTON_PRESS)
6185 data->granularity = SELECT_WORDS;
6186 else if (button->type == GDK_3BUTTON_PRESS)
6187 data->granularity = SELECT_LINES;
6189 data->granularity = SELECT_CHARACTERS;
6191 gtk_grab_add (GTK_WIDGET (text_view));
6193 buffer = get_buffer (text_view);
6198 extend_selection (text_view, data->granularity, &ins, &bound);
6202 if (button->state & GDK_SHIFT_MASK)
6204 /* Extend selection */
6205 GtkTextIter old_ins, old_bound;
6206 GtkTextIter old_start, old_end;
6208 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6209 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6210 old_start = old_ins;
6211 old_end = old_bound;
6212 gtk_text_iter_order (&old_start, &old_end);
6214 /* move the front cursor, if the mouse is in front of the selection. Should the
6215 * cursor however be inside the selection (this happens on tripple click) then we
6216 * move the side which was last moved (current insert mark) */
6217 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6218 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6219 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6222 orig_start = old_end;
6234 gtk_text_buffer_select_range (buffer, &ins, &bound);
6236 gtk_text_iter_order (&orig_start, &orig_end);
6237 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6239 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6242 gtk_text_view_check_cursor_blink (text_view);
6244 text_view->selection_drag_handler = g_signal_connect_data (text_view,
6245 "motion-notify-event",
6246 G_CALLBACK (selection_motion_event_handler),
6248 (GClosureNotify) selection_data_free, 0);
6251 /* returns whether we were really dragging */
6253 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6255 if (text_view->selection_drag_handler == 0)
6258 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
6259 text_view->selection_drag_handler = 0;
6261 if (text_view->scroll_timeout != 0)
6263 g_source_remove (text_view->scroll_timeout);
6264 text_view->scroll_timeout = 0;
6267 gtk_grab_remove (GTK_WIDGET (text_view));
6277 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6278 GtkTextAttributes *values,
6281 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
6282 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
6285 pango_font_description_free (values->font);
6287 values->font = pango_font_description_copy (style->font_desc);
6291 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6293 if (text_view->layout)
6295 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6296 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6297 GtkTextDirection new_cursor_dir;
6298 GtkTextDirection new_keyboard_dir;
6299 gboolean split_cursor;
6301 g_object_get (settings,
6302 "gtk-split-cursor", &split_cursor,
6305 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6306 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6308 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6311 new_cursor_dir = GTK_TEXT_DIR_NONE;
6313 new_cursor_dir = new_keyboard_dir;
6315 gtk_text_layout_set_cursor_direction (text_view->layout, new_cursor_dir);
6316 gtk_text_layout_set_keyboard_direction (text_view->layout, new_keyboard_dir);
6321 gtk_text_view_ensure_layout (GtkTextView *text_view)
6325 widget = GTK_WIDGET (text_view);
6327 if (text_view->layout == NULL)
6329 GtkTextAttributes *style;
6330 PangoContext *ltr_context, *rtl_context;
6333 DV(g_print(G_STRLOC"\n"));
6335 text_view->layout = gtk_text_layout_new ();
6337 g_signal_connect (text_view->layout,
6339 G_CALLBACK (invalidated_handler),
6342 g_signal_connect (text_view->layout,
6344 G_CALLBACK (changed_handler),
6347 g_signal_connect (text_view->layout,
6349 G_CALLBACK (gtk_text_view_child_allocated),
6352 if (get_buffer (text_view))
6353 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
6355 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
6356 gtk_text_view_pend_cursor_blink (text_view);
6358 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
6360 gtk_text_layout_set_overwrite_mode (text_view->layout,
6361 text_view->overwrite_mode && text_view->editable);
6363 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6364 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6365 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6366 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6368 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
6370 g_object_unref (ltr_context);
6371 g_object_unref (rtl_context);
6373 gtk_text_view_check_keymap_direction (text_view);
6375 style = gtk_text_attributes_new ();
6377 gtk_widget_ensure_style (widget);
6378 gtk_text_view_set_attributes_from_style (text_view,
6379 style, widget->style);
6381 style->pixels_above_lines = text_view->pixels_above_lines;
6382 style->pixels_below_lines = text_view->pixels_below_lines;
6383 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
6384 style->left_margin = text_view->left_margin;
6385 style->right_margin = text_view->right_margin;
6386 style->indent = text_view->indent;
6387 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
6389 style->wrap_mode = text_view->wrap_mode;
6390 style->justification = text_view->justify;
6391 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6393 gtk_text_layout_set_default_style (text_view->layout, style);
6395 gtk_text_attributes_unref (style);
6397 /* Set layout for all anchored children */
6399 tmp_list = text_view->children;
6400 while (tmp_list != NULL)
6402 GtkTextViewChild *vc = tmp_list->data;
6406 gtk_text_anchored_child_set_layout (vc->widget,
6408 /* vc may now be invalid! */
6411 tmp_list = g_slist_next (tmp_list);
6414 gtk_text_view_invalidate (text_view);
6419 * gtk_text_view_get_default_attributes:
6420 * @text_view: a #GtkTextView
6422 * Obtains a copy of the default text attributes. These are the
6423 * attributes used for text unless a tag overrides them.
6424 * You'd typically pass the default attributes in to
6425 * gtk_text_iter_get_attributes() in order to get the
6426 * attributes in effect at a given text position.
6428 * The return value is a copy owned by the caller of this function,
6429 * and should be freed.
6431 * Return value: a new #GtkTextAttributes
6434 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6436 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6438 gtk_text_view_ensure_layout (text_view);
6440 return gtk_text_attributes_copy (text_view->layout->default_style);
6444 gtk_text_view_destroy_layout (GtkTextView *text_view)
6446 if (text_view->layout)
6450 gtk_text_view_remove_validate_idles (text_view);
6452 g_signal_handlers_disconnect_by_func (text_view->layout,
6453 invalidated_handler,
6455 g_signal_handlers_disconnect_by_func (text_view->layout,
6459 /* Remove layout from all anchored children */
6460 tmp_list = text_view->children;
6461 while (tmp_list != NULL)
6463 GtkTextViewChild *vc = tmp_list->data;
6467 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6468 /* vc may now be invalid! */
6471 tmp_list = g_slist_next (tmp_list);
6474 gtk_text_view_stop_cursor_blink (text_view);
6475 gtk_text_view_end_selection_drag (text_view);
6477 g_object_unref (text_view->layout);
6478 text_view->layout = NULL;
6483 gtk_text_view_reset_im_context (GtkTextView *text_view)
6485 if (text_view->need_im_reset)
6487 text_view->need_im_reset = FALSE;
6488 gtk_im_context_reset (text_view->im_context);
6497 drag_begin_cb (GtkWidget *widget,
6498 GdkDragContext *context,
6501 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6502 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6505 GdkPixmap *pixmap = NULL;
6507 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6509 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6510 pixmap = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6514 gtk_drag_set_icon_pixmap (context,
6515 gdk_drawable_get_colormap (pixmap),
6519 g_object_unref (pixmap);
6523 gtk_drag_set_icon_default (context);
6528 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6529 const GtkTextIter *iter,
6530 GdkEventMotion *event)
6532 GtkTargetList *target_list;
6534 text_view->drag_start_x = -1;
6535 text_view->drag_start_y = -1;
6536 text_view->pending_place_cursor_button = 0;
6538 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6540 g_signal_connect (text_view, "drag-begin",
6541 G_CALLBACK (drag_begin_cb), NULL);
6542 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6543 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6544 1, (GdkEvent*)event);
6548 gtk_text_view_drag_begin (GtkWidget *widget,
6549 GdkDragContext *context)
6555 gtk_text_view_drag_end (GtkWidget *widget,
6556 GdkDragContext *context)
6561 gtk_text_view_drag_data_get (GtkWidget *widget,
6562 GdkDragContext *context,
6563 GtkSelectionData *selection_data,
6567 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6568 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6570 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6572 gtk_selection_data_set (selection_data,
6573 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6578 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6585 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6587 /* Extract the selected text */
6588 str = gtk_text_buffer_serialize (buffer, buffer,
6589 selection_data->target,
6596 gtk_selection_data_set (selection_data,
6597 selection_data->target,
6599 (guchar *) str, len);
6609 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6611 /* Extract the selected text */
6612 str = gtk_text_iter_get_visible_text (&start, &end);
6617 gtk_selection_data_set_text (selection_data, str, -1);
6624 gtk_text_view_drag_data_delete (GtkWidget *widget,
6625 GdkDragContext *context)
6627 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
6628 TRUE, GTK_TEXT_VIEW (widget)->editable);
6632 gtk_text_view_drag_leave (GtkWidget *widget,
6633 GdkDragContext *context,
6636 GtkTextView *text_view;
6638 text_view = GTK_TEXT_VIEW (widget);
6640 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6642 if (text_view->scroll_timeout != 0)
6643 g_source_remove (text_view->scroll_timeout);
6645 text_view->scroll_timeout = 0;
6649 gtk_text_view_drag_motion (GtkWidget *widget,
6650 GdkDragContext *context,
6655 GtkTextIter newplace;
6656 GtkTextView *text_view;
6659 GdkRectangle target_rect;
6662 GdkDragAction suggested_action = 0;
6664 text_view = GTK_TEXT_VIEW (widget);
6666 target_rect = text_view->text_window->allocation;
6668 if (x < target_rect.x ||
6669 y < target_rect.y ||
6670 x > (target_rect.x + target_rect.width) ||
6671 y > (target_rect.y + target_rect.height))
6672 return FALSE; /* outside the text window, allow parent widgets to handle event */
6674 gtk_text_view_window_to_buffer_coords (text_view,
6675 GTK_TEXT_WINDOW_WIDGET,
6679 gtk_text_layout_get_iter_at_pixel (text_view->layout,
6683 target = gtk_drag_dest_find_target (widget, context,
6684 gtk_drag_dest_get_target_list (widget));
6686 if (target == GDK_NONE)
6688 /* can't accept any of the offered targets */
6690 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6692 gtk_text_iter_compare (&newplace, &start) >= 0 &&
6693 gtk_text_iter_compare (&newplace, &end) <= 0)
6695 /* We're inside the selection. */
6699 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
6701 GtkWidget *source_widget;
6703 suggested_action = context->suggested_action;
6705 source_widget = gtk_drag_get_source_widget (context);
6707 if (source_widget == widget)
6709 /* Default to MOVE, unless the user has
6710 * pressed ctrl or alt to affect available actions
6712 if ((context->actions & GDK_ACTION_MOVE) != 0)
6713 suggested_action = GDK_ACTION_MOVE;
6718 /* Can't drop here. */
6722 if (suggested_action != 0)
6724 gtk_text_mark_set_visible (text_view->dnd_mark,
6725 text_view->cursor_visible);
6727 gdk_drag_status (context, suggested_action, time);
6731 gdk_drag_status (context, 0, time);
6732 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6735 if (!text_view->scroll_timeout)
6736 text_view->scroll_timeout =
6737 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
6739 /* TRUE return means don't propagate the drag motion to parent
6740 * widgets that may also be drop sites.
6746 gtk_text_view_drag_drop (GtkWidget *widget,
6747 GdkDragContext *context,
6752 GtkTextView *text_view;
6753 GtkTextIter drop_point;
6754 GdkAtom target = GDK_NONE;
6756 text_view = GTK_TEXT_VIEW (widget);
6758 if (text_view->scroll_timeout != 0)
6759 g_source_remove (text_view->scroll_timeout);
6761 text_view->scroll_timeout = 0;
6763 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6765 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6767 text_view->dnd_mark);
6769 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
6770 target = gtk_drag_dest_find_target (widget, context, NULL);
6772 if (target != GDK_NONE)
6773 gtk_drag_get_data (widget, context, target, time);
6775 gtk_drag_finish (context, FALSE, FALSE, time);
6781 insert_text_data (GtkTextView *text_view,
6782 GtkTextIter *drop_point,
6783 GtkSelectionData *selection_data)
6787 str = gtk_selection_data_get_text (selection_data);
6791 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
6792 drop_point, (gchar *) str, -1,
6793 text_view->editable))
6795 gtk_widget_error_bell (GTK_WIDGET (text_view));
6803 gtk_text_view_drag_data_received (GtkWidget *widget,
6804 GdkDragContext *context,
6807 GtkSelectionData *selection_data,
6811 GtkTextIter drop_point;
6812 GtkTextView *text_view;
6813 gboolean success = FALSE;
6814 GtkTextBuffer *buffer = NULL;
6816 text_view = GTK_TEXT_VIEW (widget);
6818 if (!text_view->dnd_mark)
6821 buffer = get_buffer (text_view);
6823 gtk_text_buffer_get_iter_at_mark (buffer,
6825 text_view->dnd_mark);
6827 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
6832 gtk_text_buffer_begin_user_action (buffer);
6834 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6836 GtkTextBuffer *src_buffer = NULL;
6837 GtkTextIter start, end;
6838 gboolean copy_tags = TRUE;
6840 if (selection_data->length != sizeof (src_buffer))
6843 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
6845 if (src_buffer == NULL)
6848 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
6850 if (gtk_text_buffer_get_tag_table (src_buffer) !=
6851 gtk_text_buffer_get_tag_table (buffer))
6853 /* try to find a suitable rich text target instead */
6857 GdkAtom target = GDK_NONE;
6861 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
6863 for (list = context->targets; list; list = g_list_next (list))
6867 for (i = 0; i < n_atoms; i++)
6868 if (GUINT_TO_POINTER (atoms[i]) == list->data)
6877 if (target != GDK_NONE)
6879 gtk_drag_get_data (widget, context, target, time);
6880 gtk_text_buffer_end_user_action (buffer);
6885 if (gtk_text_buffer_get_selection_bounds (src_buffer,
6890 gtk_text_buffer_insert_range_interactive (buffer,
6894 text_view->editable);
6899 str = gtk_text_iter_get_visible_text (&start, &end);
6900 gtk_text_buffer_insert_interactive (buffer,
6901 &drop_point, str, -1,
6902 text_view->editable);
6907 else if (selection_data->length > 0 &&
6908 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6911 GError *error = NULL;
6913 retval = gtk_text_buffer_deserialize (buffer, buffer,
6914 selection_data->target,
6916 (guint8 *) selection_data->data,
6917 selection_data->length,
6922 g_warning ("error pasting: %s\n", error->message);
6923 g_clear_error (&error);
6927 insert_text_data (text_view, &drop_point, selection_data);
6930 gtk_drag_finish (context, success,
6931 success && context->action == GDK_ACTION_MOVE,
6936 gtk_text_buffer_get_iter_at_mark (buffer,
6938 text_view->dnd_mark);
6939 gtk_text_buffer_place_cursor (buffer, &drop_point);
6941 gtk_text_buffer_end_user_action (buffer);
6945 static GtkAdjustment*
6946 get_hadjustment (GtkTextView *text_view)
6948 if (text_view->hadjustment == NULL)
6949 gtk_text_view_set_scroll_adjustments (text_view,
6950 NULL, /* forces creation */
6951 text_view->vadjustment);
6953 return text_view->hadjustment;
6956 static GtkAdjustment*
6957 get_vadjustment (GtkTextView *text_view)
6959 if (text_view->vadjustment == NULL)
6960 gtk_text_view_set_scroll_adjustments (text_view,
6961 text_view->hadjustment,
6962 NULL); /* forces creation */
6963 return text_view->vadjustment;
6968 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
6969 GtkAdjustment *hadj,
6970 GtkAdjustment *vadj)
6972 gboolean need_adjust = FALSE;
6975 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
6977 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6979 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
6981 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6983 if (text_view->hadjustment && (text_view->hadjustment != hadj))
6985 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
6986 gtk_text_view_value_changed,
6988 g_object_unref (text_view->hadjustment);
6991 if (text_view->vadjustment && (text_view->vadjustment != vadj))
6993 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
6994 gtk_text_view_value_changed,
6996 g_object_unref (text_view->vadjustment);
6999 if (text_view->hadjustment != hadj)
7001 text_view->hadjustment = hadj;
7002 g_object_ref_sink (text_view->hadjustment);
7004 g_signal_connect (text_view->hadjustment, "value-changed",
7005 G_CALLBACK (gtk_text_view_value_changed),
7010 if (text_view->vadjustment != vadj)
7012 text_view->vadjustment = vadj;
7013 g_object_ref_sink (text_view->vadjustment);
7015 g_signal_connect (text_view->vadjustment, "value-changed",
7016 G_CALLBACK (gtk_text_view_value_changed),
7022 gtk_text_view_value_changed (NULL, text_view);
7025 /* FIXME this adjust_allocation is a big cut-and-paste from
7026 * GtkCList, needs to be some "official" way to do this
7036 /* The window to which widget->window is relative */
7037 #define ALLOCATION_WINDOW(widget) \
7038 (GTK_WIDGET_NO_WINDOW (widget) ? \
7039 (widget)->window : \
7040 gdk_window_get_parent ((widget)->window))
7043 adjust_allocation_recurse (GtkWidget *widget,
7046 ScrollData *scroll_data = data;
7048 /* Need to really size allocate instead of just poking
7049 * into widget->allocation if the widget is not realized.
7050 * FIXME someone figure out why this was.
7052 if (!GTK_WIDGET_REALIZED (widget))
7054 if (GTK_WIDGET_VISIBLE (widget))
7056 GdkRectangle tmp_rectangle = widget->allocation;
7057 tmp_rectangle.x += scroll_data->dx;
7058 tmp_rectangle.y += scroll_data->dy;
7060 gtk_widget_size_allocate (widget, &tmp_rectangle);
7065 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
7067 widget->allocation.x += scroll_data->dx;
7068 widget->allocation.y += scroll_data->dy;
7070 if (GTK_IS_CONTAINER (widget))
7071 gtk_container_forall (GTK_CONTAINER (widget),
7072 adjust_allocation_recurse,
7079 adjust_allocation (GtkWidget *widget,
7083 ScrollData scroll_data;
7085 if (GTK_WIDGET_REALIZED (widget))
7086 scroll_data.window = ALLOCATION_WINDOW (widget);
7088 scroll_data.window = NULL;
7090 scroll_data.dx = dx;
7091 scroll_data.dy = dy;
7093 adjust_allocation_recurse (widget, &scroll_data);
7097 gtk_text_view_value_changed (GtkAdjustment *adj,
7098 GtkTextView *text_view)
7105 /* Note that we oddly call this function with adj == NULL
7109 text_view->onscreen_validated = FALSE;
7111 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7112 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
7113 adj ? adj->value : 0.0));
7115 if (adj == text_view->hadjustment)
7117 dx = text_view->xoffset - (gint)adj->value;
7118 text_view->xoffset = adj->value;
7120 /* If the change is due to a size change we need
7121 * to invalidate the entire text window because there might be
7122 * right-aligned or centered text
7124 if (text_view->width_changed)
7126 if (GTK_WIDGET_REALIZED (text_view))
7127 gdk_window_invalidate_rect (text_view->text_window->bin_window, NULL, FALSE);
7129 text_view->width_changed = FALSE;
7132 else if (adj == text_view->vadjustment)
7134 dy = text_view->yoffset - (gint)adj->value;
7135 text_view->yoffset = adj->value;
7137 if (text_view->layout)
7139 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
7141 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
7143 text_view->first_para_pixels = adj->value - line_top;
7147 if (dx != 0 || dy != 0)
7151 if (GTK_WIDGET_REALIZED (text_view))
7155 if (text_view->left_window)
7156 text_window_scroll (text_view->left_window, 0, dy);
7157 if (text_view->right_window)
7158 text_window_scroll (text_view->right_window, 0, dy);
7163 if (text_view->top_window)
7164 text_window_scroll (text_view->top_window, dx, 0);
7165 if (text_view->bottom_window)
7166 text_window_scroll (text_view->bottom_window, dx, 0);
7169 /* It looks nicer to scroll the main area last, because
7170 * it takes a while, and making the side areas update
7171 * afterward emphasizes the slowness of scrolling the
7174 text_window_scroll (text_view->text_window, dx, dy);
7177 /* Children are now "moved" in the text window, poke
7178 * into widget->allocation for each child
7180 tmp_list = text_view->children;
7181 while (tmp_list != NULL)
7183 GtkTextViewChild *child = tmp_list->data;
7186 adjust_allocation (child->widget, dx, dy);
7188 tmp_list = g_slist_next (tmp_list);
7192 /* This could result in invalidation, which would install the
7193 * first_validate_idle, which would validate onscreen;
7194 * but we're going to go ahead and validate here, so
7195 * first_validate_idle shouldn't have anything to do.
7197 gtk_text_view_update_layout_width (text_view);
7199 /* We also update the IM spot location here, since the IM context
7200 * might do something that leads to validation.
7202 gtk_text_view_update_im_spot_location (text_view);
7204 /* note that validation of onscreen could invoke this function
7205 * recursively, by scrolling to maintain first_para, or in response
7206 * to updating the layout width, however there is no problem with
7207 * that, or shouldn't be.
7209 gtk_text_view_validate_onscreen (text_view);
7211 /* process exposes */
7212 if (GTK_WIDGET_REALIZED (text_view))
7214 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7216 if (text_view->left_window)
7217 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
7219 if (text_view->right_window)
7220 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
7222 if (text_view->top_window)
7223 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
7225 if (text_view->bottom_window)
7226 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
7228 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
7231 /* If this got installed, get rid of it, it's just a waste of time. */
7232 if (text_view->first_validate_idle != 0)
7234 g_source_remove (text_view->first_validate_idle);
7235 text_view->first_validate_idle = 0;
7238 /* Finally we update the IM cursor location again, to ensure any
7239 * changes made by the validation are pushed through.
7241 gtk_text_view_update_im_spot_location (text_view);
7243 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7247 gtk_text_view_commit_handler (GtkIMContext *context,
7249 GtkTextView *text_view)
7251 gtk_text_view_commit_text (text_view, str);
7255 gtk_text_view_commit_text (GtkTextView *text_view,
7258 gboolean had_selection;
7260 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7262 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7265 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7266 text_view->editable);
7268 if (!strcmp (str, "\n"))
7270 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7271 text_view->editable))
7273 gtk_widget_error_bell (GTK_WIDGET (text_view));
7278 if (!had_selection && text_view->overwrite_mode)
7282 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7284 gtk_text_buffer_get_insert (get_buffer (text_view)));
7285 if (!gtk_text_iter_ends_line (&insert))
7286 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7289 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7290 text_view->editable))
7292 gtk_widget_error_bell (GTK_WIDGET (text_view));
7296 gtk_text_buffer_end_user_action (get_buffer (text_view));
7298 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7299 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7300 gtk_text_view_scroll_mark_onscreen (text_view,
7301 gtk_text_buffer_get_insert (get_buffer (text_view)));
7305 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7306 GtkTextView *text_view)
7309 PangoAttrList *attrs;
7313 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7314 gtk_text_buffer_get_insert (text_view->buffer));
7316 /* Keypress events are passed to input method even if cursor position is
7317 * not editable; so beep here if it's multi-key input sequence, input
7318 * method will be reset in key-press-event handler.
7320 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7322 if (str && str[0] && !gtk_text_iter_can_insert (&iter, text_view->editable))
7324 gtk_widget_error_bell (GTK_WIDGET (text_view));
7328 if (text_view->layout)
7329 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
7330 if (GTK_WIDGET_HAS_FOCUS (text_view))
7331 gtk_text_view_scroll_mark_onscreen (text_view,
7332 gtk_text_buffer_get_insert (get_buffer (text_view)));
7335 pango_attr_list_unref (attrs);
7340 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7341 GtkTextView *text_view)
7348 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7349 gtk_text_buffer_get_insert (text_view->buffer));
7352 pos = gtk_text_iter_get_line_index (&start);
7353 gtk_text_iter_set_line_offset (&start, 0);
7354 gtk_text_iter_forward_to_line_end (&end);
7356 text = gtk_text_iter_get_slice (&start, &end);
7357 gtk_im_context_set_surrounding (context, text, -1, pos);
7364 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7367 GtkTextView *text_view)
7372 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7373 gtk_text_buffer_get_insert (text_view->buffer));
7376 gtk_text_iter_forward_chars (&start, offset);
7377 gtk_text_iter_forward_chars (&end, offset + n_chars);
7379 gtk_text_buffer_delete_interactive (text_view->buffer, &start, &end,
7380 text_view->editable);
7386 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7387 const GtkTextIter *location,
7391 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7392 gboolean need_reset = FALSE;
7394 if (mark == gtk_text_buffer_get_insert (buffer))
7396 text_view->virtual_cursor_x = -1;
7397 text_view->virtual_cursor_y = -1;
7398 gtk_text_view_update_im_spot_location (text_view);
7401 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7407 gtk_text_view_reset_im_context (text_view);
7411 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7412 const GParamSpec *pspec,
7415 GtkWidget *widget = GTK_WIDGET (data);
7416 GtkTargetList *view_list;
7417 GtkTargetList *buffer_list;
7420 view_list = gtk_drag_dest_get_target_list (widget);
7421 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7424 gtk_target_list_ref (view_list);
7426 view_list = gtk_target_list_new (NULL, 0);
7428 list = view_list->list;
7431 GtkTargetPair *pair = list->data;
7433 list = g_list_next (list); /* get next element before removing */
7435 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7436 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7438 gtk_target_list_remove (view_list, pair->target);
7442 for (list = buffer_list->list; list; list = g_list_next (list))
7444 GtkTargetPair *pair = list->data;
7446 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7449 gtk_drag_dest_set_target_list (widget, view_list);
7450 gtk_target_list_unref (view_list);
7454 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7459 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7460 gtk_text_buffer_get_insert (get_buffer (text_view)));
7462 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, pos, NULL);
7466 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7472 if ((x && text_view->virtual_cursor_x == -1) ||
7473 (y && text_view->virtual_cursor_y == -1))
7474 gtk_text_view_get_cursor_location (text_view, &pos);
7478 if (text_view->virtual_cursor_x != -1)
7479 *x = text_view->virtual_cursor_x;
7486 if (text_view->virtual_cursor_x != -1)
7487 *y = text_view->virtual_cursor_y;
7489 *y = pos.y + pos.height / 2;
7494 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7500 if (!text_view->layout)
7503 if (x == -1 || y == -1)
7504 gtk_text_view_get_cursor_location (text_view, &pos);
7506 text_view->virtual_cursor_x = (x == -1) ? pos.x : x;
7507 text_view->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7510 /* Quick hack of a popup menu
7513 activate_cb (GtkWidget *menuitem,
7514 GtkTextView *text_view)
7516 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7517 g_signal_emit_by_name (text_view, signal);
7521 append_action_signal (GtkTextView *text_view,
7523 const gchar *stock_id,
7524 const gchar *signal,
7527 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7529 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7530 g_signal_connect (menuitem, "activate",
7531 G_CALLBACK (activate_cb), text_view);
7533 gtk_widget_set_sensitive (menuitem, sensitive);
7535 gtk_widget_show (menuitem);
7536 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7540 gtk_text_view_select_all (GtkWidget *widget,
7543 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
7544 GtkTextBuffer *buffer;
7545 GtkTextIter start_iter, end_iter, insert;
7547 buffer = text_view->buffer;
7550 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
7551 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
7555 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
7556 gtk_text_buffer_get_insert (buffer));
7557 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
7562 select_all_cb (GtkWidget *menuitem,
7563 GtkTextView *text_view)
7565 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
7569 delete_cb (GtkTextView *text_view)
7571 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7572 text_view->editable);
7576 popup_menu_detach (GtkWidget *attach_widget,
7579 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
7583 popup_position_func (GtkMenu *menu,
7589 GtkTextView *text_view;
7591 GdkRectangle cursor_rect;
7592 GdkRectangle onscreen_rect;
7593 gint root_x, root_y;
7598 GdkRectangle monitor;
7600 text_view = GTK_TEXT_VIEW (user_data);
7601 widget = GTK_WIDGET (text_view);
7603 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
7605 screen = gtk_widget_get_screen (widget);
7607 gdk_window_get_origin (widget->window, &root_x, &root_y);
7609 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7611 gtk_text_buffer_get_insert (get_buffer (text_view)));
7613 gtk_text_view_get_iter_location (text_view,
7617 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
7619 gtk_widget_size_request (text_view->popup_menu, &req);
7621 /* can't use rectangle_intersect since cursor rect can have 0 width */
7622 if (cursor_rect.x >= onscreen_rect.x &&
7623 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
7624 cursor_rect.y >= onscreen_rect.y &&
7625 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
7627 gtk_text_view_buffer_to_window_coords (text_view,
7628 GTK_TEXT_WINDOW_WIDGET,
7629 cursor_rect.x, cursor_rect.y,
7630 &cursor_rect.x, &cursor_rect.y);
7632 *x = root_x + cursor_rect.x + cursor_rect.width;
7633 *y = root_y + cursor_rect.y + cursor_rect.height;
7637 /* Just center the menu, since cursor is offscreen. */
7638 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
7639 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
7643 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
7644 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
7646 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
7647 gtk_menu_set_monitor (menu, monitor_num);
7648 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
7650 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
7651 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
7658 GtkTextView *text_view;
7664 range_contains_editable_text (const GtkTextIter *start,
7665 const GtkTextIter *end,
7666 gboolean default_editability)
7668 GtkTextIter iter = *start;
7670 while (gtk_text_iter_compare (&iter, end) < 0)
7672 if (gtk_text_iter_editable (&iter, default_editability))
7675 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
7682 unichar_chosen_func (const char *text,
7685 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7687 gtk_text_view_commit_text (text_view, text);
7691 popup_targets_received (GtkClipboard *clipboard,
7692 GtkSelectionData *data,
7695 PopupInfo *info = user_data;
7696 GtkTextView *text_view = info->text_view;
7698 if (GTK_WIDGET_REALIZED (text_view))
7700 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
7701 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
7703 gboolean clipboard_contains_text;
7704 GtkWidget *menuitem;
7706 gboolean have_selection;
7707 gboolean can_insert;
7709 GtkTextIter sel_start, sel_end;
7710 gboolean show_input_method_menu;
7711 gboolean show_unicode_menu;
7713 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
7715 if (text_view->popup_menu)
7716 gtk_widget_destroy (text_view->popup_menu);
7718 text_view->popup_menu = gtk_menu_new ();
7720 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
7721 GTK_WIDGET (text_view),
7724 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7725 &sel_start, &sel_end);
7727 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7729 gtk_text_buffer_get_insert (get_buffer (text_view)));
7731 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
7733 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
7735 range_contains_editable_text (&sel_start, &sel_end,
7736 text_view->editable));
7737 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
7739 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
7740 can_insert && clipboard_contains_text);
7742 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
7743 gtk_widget_set_sensitive (menuitem,
7745 range_contains_editable_text (&sel_start, &sel_end,
7746 text_view->editable));
7747 g_signal_connect_swapped (menuitem, "activate",
7748 G_CALLBACK (delete_cb), text_view);
7749 gtk_widget_show (menuitem);
7750 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7752 menuitem = gtk_separator_menu_item_new ();
7753 gtk_widget_show (menuitem);
7754 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7756 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
7757 g_signal_connect (menuitem, "activate",
7758 G_CALLBACK (select_all_cb), text_view);
7759 gtk_widget_show (menuitem);
7760 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7762 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
7763 "gtk-show-input-method-menu", &show_input_method_menu,
7764 "gtk-show-unicode-menu", &show_unicode_menu,
7767 if (show_input_method_menu || show_unicode_menu)
7769 menuitem = gtk_separator_menu_item_new ();
7770 gtk_widget_show (menuitem);
7771 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7774 if (show_input_method_menu)
7776 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
7777 gtk_widget_show (menuitem);
7778 gtk_widget_set_sensitive (menuitem, can_insert);
7780 submenu = gtk_menu_new ();
7781 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7782 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7784 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
7785 GTK_MENU_SHELL (submenu));
7788 if (show_unicode_menu)
7790 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
7791 gtk_widget_show (menuitem);
7792 gtk_widget_set_sensitive (menuitem, can_insert);
7794 submenu = gtk_menu_new ();
7795 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7796 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7798 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
7799 unichar_chosen_func,
7803 g_signal_emit (text_view,
7804 signals[POPULATE_POPUP],
7806 text_view->popup_menu);
7809 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7811 info->button, info->time);
7814 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7815 popup_position_func, text_view,
7816 0, gtk_get_current_event_time ());
7817 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
7821 g_object_unref (text_view);
7826 gtk_text_view_do_popup (GtkTextView *text_view,
7827 GdkEventButton *event)
7829 PopupInfo *info = g_new (PopupInfo, 1);
7831 /* In order to know what entries we should make sensitive, we
7832 * ask for the current targets of the clipboard, and when
7833 * we get them, then we actually pop up the menu.
7835 info->text_view = g_object_ref (text_view);
7839 info->button = event->button;
7840 info->time = event->time;
7845 info->time = gtk_get_current_event_time ();
7848 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
7849 GDK_SELECTION_CLIPBOARD),
7850 gdk_atom_intern_static_string ("TARGETS"),
7851 popup_targets_received,
7856 gtk_text_view_popup_menu (GtkWidget *widget)
7858 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
7862 /* Child GdkWindows */
7865 static GtkTextWindow*
7866 text_window_new (GtkTextWindowType type,
7869 gint height_request)
7873 win = g_new (GtkTextWindow, 1);
7876 win->widget = widget;
7878 win->bin_window = NULL;
7879 win->requisition.width = width_request;
7880 win->requisition.height = height_request;
7881 win->allocation.width = width_request;
7882 win->allocation.height = height_request;
7883 win->allocation.x = 0;
7884 win->allocation.y = 0;
7890 text_window_free (GtkTextWindow *win)
7893 text_window_unrealize (win);
7899 text_window_realize (GtkTextWindow *win,
7902 GdkWindowAttr attributes;
7903 gint attributes_mask;
7906 attributes.window_type = GDK_WINDOW_CHILD;
7907 attributes.x = win->allocation.x;
7908 attributes.y = win->allocation.y;
7909 attributes.width = win->allocation.width;
7910 attributes.height = win->allocation.height;
7911 attributes.wclass = GDK_INPUT_OUTPUT;
7912 attributes.visual = gtk_widget_get_visual (win->widget);
7913 attributes.colormap = gtk_widget_get_colormap (win->widget);
7914 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
7916 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
7918 win->window = gdk_window_new (widget->window,
7922 gdk_window_set_back_pixmap (win->window, NULL, FALSE);
7924 gdk_window_show (win->window);
7925 gdk_window_set_user_data (win->window, win->widget);
7926 gdk_window_lower (win->window);
7930 attributes.width = win->allocation.width;
7931 attributes.height = win->allocation.height;
7932 attributes.event_mask = (GDK_EXPOSURE_MASK |
7934 GDK_KEY_PRESS_MASK |
7935 GDK_BUTTON_PRESS_MASK |
7936 GDK_BUTTON_RELEASE_MASK |
7937 GDK_POINTER_MOTION_MASK |
7938 GDK_POINTER_MOTION_HINT_MASK |
7939 gtk_widget_get_events (win->widget));
7941 win->bin_window = gdk_window_new (win->window,
7945 gdk_window_show (win->bin_window);
7946 gdk_window_set_user_data (win->bin_window, win->widget);
7948 if (win->type == GTK_TEXT_WINDOW_TEXT)
7950 if (GTK_WIDGET_IS_SENSITIVE (widget))
7953 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (widget->window),
7955 gdk_window_set_cursor (win->bin_window, cursor);
7956 gdk_cursor_unref (cursor);
7959 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->im_context,
7963 gdk_window_set_background (win->bin_window,
7964 &widget->style->base[GTK_WIDGET_STATE (widget)]);
7968 gdk_window_set_background (win->bin_window,
7969 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
7972 g_object_set_qdata (G_OBJECT (win->window),
7973 g_quark_from_static_string ("gtk-text-view-text-window"),
7976 g_object_set_qdata (G_OBJECT (win->bin_window),
7977 g_quark_from_static_string ("gtk-text-view-text-window"),
7982 text_window_unrealize (GtkTextWindow *win)
7984 if (win->type == GTK_TEXT_WINDOW_TEXT)
7986 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
7990 gdk_window_set_user_data (win->window, NULL);
7991 gdk_window_set_user_data (win->bin_window, NULL);
7992 gdk_window_destroy (win->bin_window);
7993 gdk_window_destroy (win->window);
7995 win->bin_window = NULL;
7999 text_window_size_allocate (GtkTextWindow *win,
8002 win->allocation = *rect;
8006 gdk_window_move_resize (win->window,
8008 rect->width, rect->height);
8010 gdk_window_resize (win->bin_window,
8011 rect->width, rect->height);
8016 text_window_scroll (GtkTextWindow *win,
8020 if (dx != 0 || dy != 0)
8022 gdk_window_scroll (win->bin_window, dx, dy);
8027 text_window_invalidate_rect (GtkTextWindow *win,
8030 GdkRectangle window_rect;
8032 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
8039 window_rect.width = rect->width;
8040 window_rect.height = rect->height;
8042 /* Adjust the rect as appropriate */
8046 case GTK_TEXT_WINDOW_TEXT:
8049 case GTK_TEXT_WINDOW_LEFT:
8050 case GTK_TEXT_WINDOW_RIGHT:
8052 window_rect.width = win->allocation.width;
8055 case GTK_TEXT_WINDOW_TOP:
8056 case GTK_TEXT_WINDOW_BOTTOM:
8058 window_rect.height = win->allocation.height;
8062 g_warning ("%s: bug!", G_STRFUNC);
8067 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
8071 cairo_t *cr = gdk_cairo_create (win->bin_window);
8072 gdk_cairo_rectangle (cr, &window_rect);
8073 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
8081 text_window_invalidate_cursors (GtkTextWindow *win)
8083 GtkTextView *text_view = GTK_TEXT_VIEW (win->widget);
8085 GdkRectangle strong;
8087 gboolean draw_arrow;
8088 gfloat cursor_aspect_ratio;
8092 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
8093 gtk_text_buffer_get_insert (text_view->buffer));
8095 if (_gtk_text_layout_get_block_cursor (text_view->layout, &strong))
8097 text_window_invalidate_rect (win, &strong);
8101 gtk_text_layout_get_cursor_locations (text_view->layout, &iter,
8104 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
8105 * ignoring the text direction be exposing both sides of the cursor
8108 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
8110 gtk_widget_style_get (win->widget,
8111 "cursor-aspect-ratio", &cursor_aspect_ratio,
8114 stem_width = strong.height * cursor_aspect_ratio + 1;
8115 arrow_width = stem_width + 1;
8117 strong.width = stem_width;
8119 /* round up to the next even number */
8123 strong.x -= stem_width / 2;
8124 strong.width += stem_width;
8128 strong.x -= arrow_width;
8129 strong.width += arrow_width * 2;
8132 text_window_invalidate_rect (win, &strong);
8134 if (draw_arrow) /* == have weak */
8136 stem_width = weak.height * cursor_aspect_ratio + 1;
8137 arrow_width = stem_width + 1;
8139 weak.width = stem_width;
8141 /* round up to the next even number */
8145 weak.x -= stem_width / 2;
8146 weak.width += stem_width;
8148 weak.x -= arrow_width;
8149 weak.width += arrow_width * 2;
8151 text_window_invalidate_rect (win, &weak);
8156 text_window_get_width (GtkTextWindow *win)
8158 return win->allocation.width;
8162 text_window_get_height (GtkTextWindow *win)
8164 return win->allocation.height;
8171 * gtk_text_view_get_window:
8172 * @text_view: a #GtkTextView
8173 * @win: window to get
8175 * Retrieves the #GdkWindow corresponding to an area of the text view;
8176 * possible windows include the overall widget window, child windows
8177 * on the left, right, top, bottom, and the window that displays the
8178 * text buffer. Windows are %NULL and nonexistent if their width or
8179 * height is 0, and are nonexistent before the widget has been
8182 * Return value: a #GdkWindow, or %NULL
8185 gtk_text_view_get_window (GtkTextView *text_view,
8186 GtkTextWindowType win)
8188 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8192 case GTK_TEXT_WINDOW_WIDGET:
8193 return GTK_WIDGET (text_view)->window;
8196 case GTK_TEXT_WINDOW_TEXT:
8197 return text_view->text_window->bin_window;
8200 case GTK_TEXT_WINDOW_LEFT:
8201 if (text_view->left_window)
8202 return text_view->left_window->bin_window;
8207 case GTK_TEXT_WINDOW_RIGHT:
8208 if (text_view->right_window)
8209 return text_view->right_window->bin_window;
8214 case GTK_TEXT_WINDOW_TOP:
8215 if (text_view->top_window)
8216 return text_view->top_window->bin_window;
8221 case GTK_TEXT_WINDOW_BOTTOM:
8222 if (text_view->bottom_window)
8223 return text_view->bottom_window->bin_window;
8228 case GTK_TEXT_WINDOW_PRIVATE:
8229 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8234 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8239 * gtk_text_view_get_window_type:
8240 * @text_view: a #GtkTextView
8241 * @window: a window type
8243 * Usually used to find out which window an event corresponds to.
8244 * If you connect to an event signal on @text_view, this function
8245 * should be called on <literal>event->window</literal> to
8246 * see which window it was.
8248 * Return value: the window type.
8251 gtk_text_view_get_window_type (GtkTextView *text_view,
8256 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8257 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8259 if (window == GTK_WIDGET (text_view)->window)
8260 return GTK_TEXT_WINDOW_WIDGET;
8262 win = g_object_get_qdata (G_OBJECT (window),
8263 g_quark_try_string ("gtk-text-view-text-window"));
8269 return GTK_TEXT_WINDOW_PRIVATE;
8274 buffer_to_widget (GtkTextView *text_view,
8282 *window_x = buffer_x - text_view->xoffset;
8283 *window_x += text_view->text_window->allocation.x;
8288 *window_y = buffer_y - text_view->yoffset;
8289 *window_y += text_view->text_window->allocation.y;
8294 widget_to_text_window (GtkTextWindow *win,
8301 *window_x = widget_x - win->allocation.x;
8304 *window_y = widget_y - win->allocation.y;
8308 buffer_to_text_window (GtkTextView *text_view,
8317 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8318 "for a nonexistent or private child window of GtkTextView");
8322 buffer_to_widget (text_view,
8324 window_x, window_y);
8326 widget_to_text_window (win,
8327 window_x ? *window_x : 0,
8328 window_y ? *window_y : 0,
8334 * gtk_text_view_buffer_to_window_coords:
8335 * @text_view: a #GtkTextView
8336 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8337 * @buffer_x: buffer x coordinate
8338 * @buffer_y: buffer y coordinate
8339 * @window_x: window x coordinate return location
8340 * @window_y: window y coordinate return location
8342 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8343 * @win, and stores the result in (@window_x, @window_y).
8345 * Note that you can't convert coordinates for a nonexisting window (see
8346 * gtk_text_view_set_border_window_size()).
8349 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8350 GtkTextWindowType win,
8356 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8360 case GTK_TEXT_WINDOW_WIDGET:
8361 buffer_to_widget (text_view,
8363 window_x, window_y);
8366 case GTK_TEXT_WINDOW_TEXT:
8368 *window_x = buffer_x - text_view->xoffset;
8370 *window_y = buffer_y - text_view->yoffset;
8373 case GTK_TEXT_WINDOW_LEFT:
8374 buffer_to_text_window (text_view,
8375 text_view->left_window,
8377 window_x, window_y);
8380 case GTK_TEXT_WINDOW_RIGHT:
8381 buffer_to_text_window (text_view,
8382 text_view->right_window,
8384 window_x, window_y);
8387 case GTK_TEXT_WINDOW_TOP:
8388 buffer_to_text_window (text_view,
8389 text_view->top_window,
8391 window_x, window_y);
8394 case GTK_TEXT_WINDOW_BOTTOM:
8395 buffer_to_text_window (text_view,
8396 text_view->bottom_window,
8398 window_x, window_y);
8401 case GTK_TEXT_WINDOW_PRIVATE:
8402 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8406 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8412 widget_to_buffer (GtkTextView *text_view,
8420 *buffer_x = widget_x + text_view->xoffset;
8421 *buffer_x -= text_view->text_window->allocation.x;
8426 *buffer_y = widget_y + text_view->yoffset;
8427 *buffer_y -= text_view->text_window->allocation.y;
8432 text_window_to_widget (GtkTextWindow *win,
8439 *widget_x = window_x + win->allocation.x;
8442 *widget_y = window_y + win->allocation.y;
8446 text_window_to_buffer (GtkTextView *text_view,
8455 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8456 "coordinates for a nonexistent child window.");
8460 text_window_to_widget (win,
8466 widget_to_buffer (text_view,
8467 buffer_x ? *buffer_x : 0,
8468 buffer_y ? *buffer_y : 0,
8474 * gtk_text_view_window_to_buffer_coords:
8475 * @text_view: a #GtkTextView
8476 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8477 * @window_x: window x coordinate
8478 * @window_y: window y coordinate
8479 * @buffer_x: buffer x coordinate return location
8480 * @buffer_y: buffer y coordinate return location
8482 * Converts coordinates on the window identified by @win to buffer
8483 * coordinates, storing the result in (@buffer_x,@buffer_y).
8485 * Note that you can't convert coordinates for a nonexisting window (see
8486 * gtk_text_view_set_border_window_size()).
8489 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8490 GtkTextWindowType win,
8496 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8500 case GTK_TEXT_WINDOW_WIDGET:
8501 widget_to_buffer (text_view,
8503 buffer_x, buffer_y);
8506 case GTK_TEXT_WINDOW_TEXT:
8508 *buffer_x = window_x + text_view->xoffset;
8510 *buffer_y = window_y + text_view->yoffset;
8513 case GTK_TEXT_WINDOW_LEFT:
8514 text_window_to_buffer (text_view,
8515 text_view->left_window,
8517 buffer_x, buffer_y);
8520 case GTK_TEXT_WINDOW_RIGHT:
8521 text_window_to_buffer (text_view,
8522 text_view->right_window,
8524 buffer_x, buffer_y);
8527 case GTK_TEXT_WINDOW_TOP:
8528 text_window_to_buffer (text_view,
8529 text_view->top_window,
8531 buffer_x, buffer_y);
8534 case GTK_TEXT_WINDOW_BOTTOM:
8535 text_window_to_buffer (text_view,
8536 text_view->bottom_window,
8538 buffer_x, buffer_y);
8541 case GTK_TEXT_WINDOW_PRIVATE:
8542 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8546 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8552 set_window_width (GtkTextView *text_view,
8554 GtkTextWindowType type,
8555 GtkTextWindow **winp)
8561 text_window_free (*winp);
8563 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8570 *winp = text_window_new (type,
8571 GTK_WIDGET (text_view),
8573 /* if the widget is already realized we need to realize the child manually */
8574 if (GTK_WIDGET_REALIZED (text_view))
8575 text_window_realize (*winp, GTK_WIDGET (text_view));
8579 if ((*winp)->requisition.width == width)
8582 (*winp)->requisition.width = width;
8585 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8591 set_window_height (GtkTextView *text_view,
8593 GtkTextWindowType type,
8594 GtkTextWindow **winp)
8600 text_window_free (*winp);
8602 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8609 *winp = text_window_new (type,
8610 GTK_WIDGET (text_view),
8613 /* if the widget is already realized we need to realize the child manually */
8614 if (GTK_WIDGET_REALIZED (text_view))
8615 text_window_realize (*winp, GTK_WIDGET (text_view));
8619 if ((*winp)->requisition.height == height)
8622 (*winp)->requisition.height = height;
8625 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8630 * gtk_text_view_set_border_window_size:
8631 * @text_view: a #GtkTextView
8632 * @type: window to affect
8633 * @size: width or height of the window
8635 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
8636 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
8637 * Automatically destroys the corresponding window if the size is set
8638 * to 0, and creates the window if the size is set to non-zero. This
8639 * function can only be used for the "border windows," it doesn't work
8640 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
8641 * #GTK_TEXT_WINDOW_PRIVATE.
8644 gtk_text_view_set_border_window_size (GtkTextView *text_view,
8645 GtkTextWindowType type,
8649 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8650 g_return_if_fail (size >= 0);
8654 case GTK_TEXT_WINDOW_LEFT:
8655 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
8656 &text_view->left_window);
8659 case GTK_TEXT_WINDOW_RIGHT:
8660 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
8661 &text_view->right_window);
8664 case GTK_TEXT_WINDOW_TOP:
8665 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
8666 &text_view->top_window);
8669 case GTK_TEXT_WINDOW_BOTTOM:
8670 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
8671 &text_view->bottom_window);
8675 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
8681 * gtk_text_view_get_border_window_size:
8682 * @text_view: a #GtkTextView
8683 * @type: window to return size from
8685 * Gets the width of the specified border window. See
8686 * gtk_text_view_set_border_window_size().
8688 * Return value: width of window
8691 gtk_text_view_get_border_window_size (GtkTextView *text_view,
8692 GtkTextWindowType type)
8694 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8698 case GTK_TEXT_WINDOW_LEFT:
8699 if (text_view->left_window)
8700 return text_view->left_window->requisition.width;
8703 case GTK_TEXT_WINDOW_RIGHT:
8704 if (text_view->right_window)
8705 return text_view->right_window->requisition.width;
8708 case GTK_TEXT_WINDOW_TOP:
8709 if (text_view->top_window)
8710 return text_view->top_window->requisition.height;
8713 case GTK_TEXT_WINDOW_BOTTOM:
8714 if (text_view->bottom_window)
8715 return text_view->bottom_window->requisition.height;
8719 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
8730 static GtkTextViewChild*
8731 text_view_child_new_anchored (GtkWidget *child,
8732 GtkTextChildAnchor *anchor,
8733 GtkTextLayout *layout)
8735 GtkTextViewChild *vc;
8737 vc = g_new (GtkTextViewChild, 1);
8739 vc->type = GTK_TEXT_WINDOW_PRIVATE;
8741 vc->anchor = anchor;
8743 vc->from_top_of_line = 0;
8744 vc->from_left_of_buffer = 0;
8746 g_object_ref (vc->widget);
8747 g_object_ref (vc->anchor);
8749 g_object_set_data (G_OBJECT (child),
8750 I_("gtk-text-view-child"),
8753 gtk_text_child_anchor_register_child (anchor, child, layout);
8758 static GtkTextViewChild*
8759 text_view_child_new_window (GtkWidget *child,
8760 GtkTextWindowType type,
8764 GtkTextViewChild *vc;
8766 vc = g_new (GtkTextViewChild, 1);
8771 vc->from_top_of_line = 0;
8772 vc->from_left_of_buffer = 0;
8774 g_object_ref (vc->widget);
8780 g_object_set_data (G_OBJECT (child),
8781 I_("gtk-text-view-child"),
8788 text_view_child_free (GtkTextViewChild *child)
8790 g_object_set_data (G_OBJECT (child->widget),
8791 I_("gtk-text-view-child"), NULL);
8795 gtk_text_child_anchor_unregister_child (child->anchor,
8797 g_object_unref (child->anchor);
8800 g_object_unref (child->widget);
8806 text_view_child_set_parent_window (GtkTextView *text_view,
8807 GtkTextViewChild *vc)
8810 gtk_widget_set_parent_window (vc->widget,
8811 text_view->text_window->bin_window);
8815 window = gtk_text_view_get_window (text_view,
8817 gtk_widget_set_parent_window (vc->widget, window);
8822 add_child (GtkTextView *text_view,
8823 GtkTextViewChild *vc)
8825 text_view->children = g_slist_prepend (text_view->children,
8828 if (GTK_WIDGET_REALIZED (text_view))
8829 text_view_child_set_parent_window (text_view, vc);
8831 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
8835 * gtk_text_view_add_child_at_anchor:
8836 * @text_view: a #GtkTextView
8837 * @child: a #GtkWidget
8838 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
8840 * Adds a child widget in the text buffer, at the given @anchor.
8843 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
8845 GtkTextChildAnchor *anchor)
8847 GtkTextViewChild *vc;
8849 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8850 g_return_if_fail (GTK_IS_WIDGET (child));
8851 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
8852 g_return_if_fail (child->parent == NULL);
8854 gtk_text_view_ensure_layout (text_view);
8856 vc = text_view_child_new_anchored (child, anchor,
8859 add_child (text_view, vc);
8861 g_assert (vc->widget == child);
8862 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8866 * gtk_text_view_add_child_in_window:
8867 * @text_view: a #GtkTextView
8868 * @child: a #GtkWidget
8869 * @which_window: which window the child should appear in
8870 * @xpos: X position of child in window coordinates
8871 * @ypos: Y position of child in window coordinates
8873 * Adds a child at fixed coordinates in one of the text widget's
8874 * windows. The window must have nonzero size (see
8875 * gtk_text_view_set_border_window_size()). Note that the child
8876 * coordinates are given relative to the #GdkWindow in question, and
8877 * that these coordinates have no sane relationship to scrolling. When
8878 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
8879 * irrelevant, the child floats above all scrollable areas. But when
8880 * placing a child in one of the scrollable windows (border windows or
8881 * text window), you'll need to compute the child's correct position
8882 * in buffer coordinates any time scrolling occurs or buffer changes
8883 * occur, and then call gtk_text_view_move_child() to update the
8884 * child's position. Unfortunately there's no good way to detect that
8885 * scrolling has occurred, using the current API; a possible hack
8886 * would be to update all child positions when the scroll adjustments
8887 * change or the text buffer changes. See bug 64518 on
8888 * bugzilla.gnome.org for status of fixing this issue.
8891 gtk_text_view_add_child_in_window (GtkTextView *text_view,
8893 GtkTextWindowType which_window,
8897 GtkTextViewChild *vc;
8899 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8900 g_return_if_fail (GTK_IS_WIDGET (child));
8901 g_return_if_fail (child->parent == NULL);
8903 vc = text_view_child_new_window (child, which_window,
8906 add_child (text_view, vc);
8908 g_assert (vc->widget == child);
8909 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8913 * gtk_text_view_move_child:
8914 * @text_view: a #GtkTextView
8915 * @child: child widget already added to the text view
8916 * @xpos: new X position in window coordinates
8917 * @ypos: new Y position in window coordinates
8919 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
8922 gtk_text_view_move_child (GtkTextView *text_view,
8927 GtkTextViewChild *vc;
8929 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8930 g_return_if_fail (GTK_IS_WIDGET (child));
8931 g_return_if_fail (child->parent == (GtkWidget*) text_view);
8933 vc = g_object_get_data (G_OBJECT (child),
8934 "gtk-text-view-child");
8936 g_assert (vc != NULL);
8938 if (vc->x == xpos &&
8945 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
8946 gtk_widget_queue_resize (child);
8950 /* Iterator operations */
8953 * gtk_text_view_forward_display_line:
8954 * @text_view: a #GtkTextView
8955 * @iter: a #GtkTextIter
8957 * Moves the given @iter forward by one display (wrapped) line.
8958 * A display line is different from a paragraph. Paragraphs are
8959 * separated by newlines or other paragraph separator characters.
8960 * Display lines are created by line-wrapping a paragraph. If
8961 * wrapping is turned off, display lines and paragraphs will be the
8962 * same. Display lines are divided differently for each view, since
8963 * they depend on the view's width; paragraphs are the same in all
8964 * views, since they depend on the contents of the #GtkTextBuffer.
8966 * Return value: %TRUE if @iter was moved and is not on the end iterator
8969 gtk_text_view_forward_display_line (GtkTextView *text_view,
8972 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8973 g_return_val_if_fail (iter != NULL, FALSE);
8975 gtk_text_view_ensure_layout (text_view);
8977 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
8981 * gtk_text_view_backward_display_line:
8982 * @text_view: a #GtkTextView
8983 * @iter: a #GtkTextIter
8985 * Moves the given @iter backward by one display (wrapped) line.
8986 * A display line is different from a paragraph. Paragraphs are
8987 * separated by newlines or other paragraph separator characters.
8988 * Display lines are created by line-wrapping a paragraph. If
8989 * wrapping is turned off, display lines and paragraphs will be the
8990 * same. Display lines are divided differently for each view, since
8991 * they depend on the view's width; paragraphs are the same in all
8992 * views, since they depend on the contents of the #GtkTextBuffer.
8994 * Return value: %TRUE if @iter was moved and is not on the end iterator
8997 gtk_text_view_backward_display_line (GtkTextView *text_view,
9000 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9001 g_return_val_if_fail (iter != NULL, FALSE);
9003 gtk_text_view_ensure_layout (text_view);
9005 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
9009 * gtk_text_view_forward_display_line_end:
9010 * @text_view: a #GtkTextView
9011 * @iter: a #GtkTextIter
9013 * Moves the given @iter forward to the next display line end.
9014 * A display line is different from a paragraph. Paragraphs are
9015 * separated by newlines or other paragraph separator characters.
9016 * Display lines are created by line-wrapping a paragraph. If
9017 * wrapping is turned off, display lines and paragraphs will be the
9018 * same. Display lines are divided differently for each view, since
9019 * they depend on the view's width; paragraphs are the same in all
9020 * views, since they depend on the contents of the #GtkTextBuffer.
9022 * Return value: %TRUE if @iter was moved and is not on the end iterator
9025 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
9028 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9029 g_return_val_if_fail (iter != NULL, FALSE);
9031 gtk_text_view_ensure_layout (text_view);
9033 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
9037 * gtk_text_view_backward_display_line_start:
9038 * @text_view: a #GtkTextView
9039 * @iter: a #GtkTextIter
9041 * Moves the given @iter backward to the next display line start.
9042 * A display line is different from a paragraph. Paragraphs are
9043 * separated by newlines or other paragraph separator characters.
9044 * Display lines are created by line-wrapping a paragraph. If
9045 * wrapping is turned off, display lines and paragraphs will be the
9046 * same. Display lines are divided differently for each view, since
9047 * they depend on the view's width; paragraphs are the same in all
9048 * views, since they depend on the contents of the #GtkTextBuffer.
9050 * Return value: %TRUE if @iter was moved and is not on the end iterator
9053 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
9056 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9057 g_return_val_if_fail (iter != NULL, FALSE);
9059 gtk_text_view_ensure_layout (text_view);
9061 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
9065 * gtk_text_view_starts_display_line:
9066 * @text_view: a #GtkTextView
9067 * @iter: a #GtkTextIter
9069 * Determines whether @iter is at the start of a display line.
9070 * See gtk_text_view_forward_display_line() for an explanation of
9071 * display lines vs. paragraphs.
9073 * Return value: %TRUE if @iter begins a wrapped line
9076 gtk_text_view_starts_display_line (GtkTextView *text_view,
9077 const GtkTextIter *iter)
9079 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9080 g_return_val_if_fail (iter != NULL, FALSE);
9082 gtk_text_view_ensure_layout (text_view);
9084 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
9088 * gtk_text_view_move_visually:
9089 * @text_view: a #GtkTextView
9090 * @iter: a #GtkTextIter
9091 * @count: number of characters to move (negative moves left,
9092 * positive moves right)
9094 * Move the iterator a given number of characters visually, treating
9095 * it as the strong cursor position. If @count is positive, then the
9096 * new strong cursor position will be @count positions to the right of
9097 * the old cursor position. If @count is negative then the new strong
9098 * cursor position will be @count positions to the left of the old
9101 * In the presence of bi-directional text, the correspondence
9102 * between logical and visual order will depend on the direction
9103 * of the current run, and there may be jumps when the cursor
9104 * is moved off of the end of a run.
9106 * Return value: %TRUE if @iter moved and is not on the end iterator
9109 gtk_text_view_move_visually (GtkTextView *text_view,
9113 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9114 g_return_val_if_fail (iter != NULL, FALSE);
9116 gtk_text_view_ensure_layout (text_view);
9118 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
9121 #define __GTK_TEXT_VIEW_C__
9122 #include "gtkaliasdef.c"