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,
148 PROP_PIXELS_ABOVE_LINES,
149 PROP_PIXELS_BELOW_LINES,
150 PROP_PIXELS_INSIDE_WRAP,
165 static void gtk_text_view_destroy (GtkObject *object);
166 static void gtk_text_view_finalize (GObject *object);
167 static void gtk_text_view_set_property (GObject *object,
171 static void gtk_text_view_get_property (GObject *object,
175 static void gtk_text_view_size_request (GtkWidget *widget,
176 GtkRequisition *requisition);
177 static void gtk_text_view_size_allocate (GtkWidget *widget,
178 GtkAllocation *allocation);
179 static void gtk_text_view_realize (GtkWidget *widget);
180 static void gtk_text_view_unrealize (GtkWidget *widget);
181 static void gtk_text_view_style_set (GtkWidget *widget,
182 GtkStyle *previous_style);
183 static void gtk_text_view_direction_changed (GtkWidget *widget,
184 GtkTextDirection previous_direction);
185 static void gtk_text_view_grab_notify (GtkWidget *widget,
186 gboolean was_grabbed);
187 static void gtk_text_view_state_changed (GtkWidget *widget,
188 GtkStateType previous_state);
190 static gint gtk_text_view_event (GtkWidget *widget,
192 static gint gtk_text_view_key_press_event (GtkWidget *widget,
194 static gint gtk_text_view_key_release_event (GtkWidget *widget,
196 static gint gtk_text_view_button_press_event (GtkWidget *widget,
197 GdkEventButton *event);
198 static gint gtk_text_view_button_release_event (GtkWidget *widget,
199 GdkEventButton *event);
200 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
201 GdkEventFocus *event);
202 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
203 GdkEventFocus *event);
204 static gint gtk_text_view_motion_event (GtkWidget *widget,
205 GdkEventMotion *event);
206 static gint gtk_text_view_expose_event (GtkWidget *widget,
207 GdkEventExpose *expose);
208 static void gtk_text_view_draw_focus (GtkWidget *widget);
209 static gboolean gtk_text_view_focus (GtkWidget *widget,
210 GtkDirectionType direction);
211 static void gtk_text_view_move_focus (GtkWidget *widget,
212 GtkDirectionType direction_type);
213 static void gtk_text_view_select_all (GtkWidget *widget,
217 /* Source side drag signals */
218 static void gtk_text_view_drag_begin (GtkWidget *widget,
219 GdkDragContext *context);
220 static void gtk_text_view_drag_end (GtkWidget *widget,
221 GdkDragContext *context);
222 static void gtk_text_view_drag_data_get (GtkWidget *widget,
223 GdkDragContext *context,
224 GtkSelectionData *selection_data,
227 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
228 GdkDragContext *context);
230 /* Target side drag signals */
231 static void gtk_text_view_drag_leave (GtkWidget *widget,
232 GdkDragContext *context,
234 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
235 GdkDragContext *context,
239 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
240 GdkDragContext *context,
244 static void gtk_text_view_drag_data_received (GtkWidget *widget,
245 GdkDragContext *context,
248 GtkSelectionData *selection_data,
252 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
254 GtkAdjustment *vadj);
255 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
257 static void gtk_text_view_move_cursor (GtkTextView *text_view,
258 GtkMovementStep step,
260 gboolean extend_selection);
261 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
263 gboolean extend_selection);
264 static gboolean gtk_text_view_move_viewport (GtkTextView *text_view,
267 static void gtk_text_view_set_anchor (GtkTextView *text_view);
268 static gboolean gtk_text_view_scroll_pages (GtkTextView *text_view,
270 gboolean extend_selection);
271 static gboolean gtk_text_view_scroll_hpages(GtkTextView *text_view,
273 gboolean extend_selection);
274 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
276 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
279 static void gtk_text_view_backspace (GtkTextView *text_view);
280 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
281 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
282 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
283 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
284 static void gtk_text_view_toggle_cursor_visible (GtkTextView *text_view);
285 static void gtk_text_view_compat_move_focus(GtkTextView *text_view,
286 GtkDirectionType direction_type);
287 static void gtk_text_view_unselect (GtkTextView *text_view);
289 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
290 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
292 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
293 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
294 GtkTextAttributes *values,
296 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
297 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
298 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
299 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
300 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
301 const GtkTextIter *iter,
302 GdkEventButton *event);
303 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view);
304 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
305 const GtkTextIter *iter,
306 GdkEventMotion *event);
307 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
308 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
309 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
310 static void gtk_text_view_reset_blink_time (GtkTextView *text_view);
312 static void gtk_text_view_value_changed (GtkAdjustment *adj,
314 static void gtk_text_view_commit_handler (GtkIMContext *context,
316 GtkTextView *text_view);
317 static void gtk_text_view_commit_text (GtkTextView *text_view,
319 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
320 GtkTextView *text_view);
321 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
322 GtkTextView *text_view);
323 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
326 GtkTextView *text_view);
328 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
329 const GtkTextIter *location,
332 static void gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
333 const GParamSpec *pspec,
335 static void gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
336 GtkClipboard *clipboard,
338 static void gtk_text_view_get_cursor_location (GtkTextView *text_view,
340 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
344 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
348 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
349 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
351 static void gtk_text_view_do_popup (GtkTextView *text_view,
352 GdkEventButton *event);
354 static void cancel_pending_scroll (GtkTextView *text_view);
355 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
357 gdouble within_margin,
362 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
363 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
364 static void gtk_text_view_invalidate (GtkTextView *text_view);
365 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
367 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
369 /* Container methods */
370 static void gtk_text_view_add (GtkContainer *container,
372 static void gtk_text_view_remove (GtkContainer *container,
374 static void gtk_text_view_forall (GtkContainer *container,
375 gboolean include_internals,
376 GtkCallback callback,
377 gpointer callback_data);
379 /* FIXME probably need the focus methods. */
381 typedef struct _GtkTextViewChild GtkTextViewChild;
383 struct _GtkTextViewChild
387 GtkTextChildAnchor *anchor;
389 gint from_top_of_line;
390 gint from_left_of_buffer;
392 /* These are ignored if anchor != NULL */
393 GtkTextWindowType type;
398 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
399 GtkTextChildAnchor *anchor,
400 GtkTextLayout *layout);
401 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
402 GtkTextWindowType type,
405 static void text_view_child_free (GtkTextViewChild *child);
406 static void text_view_child_set_parent_window (GtkTextView *text_view,
407 GtkTextViewChild *child);
409 struct _GtkTextWindow
411 GtkTextWindowType type;
414 GdkWindow *bin_window;
415 GtkRequisition requisition;
416 GdkRectangle allocation;
419 static GtkTextWindow *text_window_new (GtkTextWindowType type,
422 gint height_request);
423 static void text_window_free (GtkTextWindow *win);
424 static void text_window_realize (GtkTextWindow *win,
426 static void text_window_unrealize (GtkTextWindow *win);
427 static void text_window_size_allocate (GtkTextWindow *win,
429 static void text_window_scroll (GtkTextWindow *win,
432 static void text_window_invalidate_rect (GtkTextWindow *win,
434 static void text_window_invalidate_cursors (GtkTextWindow *win);
436 static gint text_window_get_width (GtkTextWindow *win);
437 static gint text_window_get_height (GtkTextWindow *win);
440 static guint signals[LAST_SIGNAL] = { 0 };
442 G_DEFINE_TYPE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER)
445 add_move_binding (GtkBindingSet *binding_set,
448 GtkMovementStep step,
451 g_assert ((modmask & GDK_SHIFT_MASK) == 0);
453 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
457 G_TYPE_BOOLEAN, FALSE);
459 /* Selection-extending version */
460 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
464 G_TYPE_BOOLEAN, TRUE);
468 gtk_text_view_class_init (GtkTextViewClass *klass)
470 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
471 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
472 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
473 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
474 GtkBindingSet *binding_set;
476 /* Default handlers and virtual methods
478 gobject_class->set_property = gtk_text_view_set_property;
479 gobject_class->get_property = gtk_text_view_get_property;
481 object_class->destroy = gtk_text_view_destroy;
482 gobject_class->finalize = gtk_text_view_finalize;
484 widget_class->realize = gtk_text_view_realize;
485 widget_class->unrealize = gtk_text_view_unrealize;
486 widget_class->style_set = gtk_text_view_style_set;
487 widget_class->direction_changed = gtk_text_view_direction_changed;
488 widget_class->grab_notify = gtk_text_view_grab_notify;
489 widget_class->state_changed = gtk_text_view_state_changed;
490 widget_class->size_request = gtk_text_view_size_request;
491 widget_class->size_allocate = gtk_text_view_size_allocate;
492 widget_class->event = gtk_text_view_event;
493 widget_class->key_press_event = gtk_text_view_key_press_event;
494 widget_class->key_release_event = gtk_text_view_key_release_event;
495 widget_class->button_press_event = gtk_text_view_button_press_event;
496 widget_class->button_release_event = gtk_text_view_button_release_event;
497 widget_class->focus_in_event = gtk_text_view_focus_in_event;
498 widget_class->focus_out_event = gtk_text_view_focus_out_event;
499 widget_class->motion_notify_event = gtk_text_view_motion_event;
500 widget_class->expose_event = gtk_text_view_expose_event;
501 widget_class->focus = gtk_text_view_focus;
503 /* need to override the base class function via override_class_handler,
504 * because the signal slot is not available in GtkWidgetCLass
506 g_signal_override_class_handler ("move-focus",
508 G_CALLBACK (gtk_text_view_move_focus));
510 widget_class->drag_begin = gtk_text_view_drag_begin;
511 widget_class->drag_end = gtk_text_view_drag_end;
512 widget_class->drag_data_get = gtk_text_view_drag_data_get;
513 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
515 widget_class->drag_leave = gtk_text_view_drag_leave;
516 widget_class->drag_motion = gtk_text_view_drag_motion;
517 widget_class->drag_drop = gtk_text_view_drag_drop;
518 widget_class->drag_data_received = gtk_text_view_drag_data_received;
520 widget_class->popup_menu = gtk_text_view_popup_menu;
522 container_class->add = gtk_text_view_add;
523 container_class->remove = gtk_text_view_remove;
524 container_class->forall = gtk_text_view_forall;
526 klass->move_cursor = gtk_text_view_move_cursor;
527 klass->page_horizontally = gtk_text_view_page_horizontally;
528 klass->set_anchor = gtk_text_view_set_anchor;
529 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
530 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
531 klass->backspace = gtk_text_view_backspace;
532 klass->cut_clipboard = gtk_text_view_cut_clipboard;
533 klass->copy_clipboard = gtk_text_view_copy_clipboard;
534 klass->paste_clipboard = gtk_text_view_paste_clipboard;
535 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
536 klass->move_focus = gtk_text_view_compat_move_focus;
537 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
543 g_object_class_install_property (gobject_class,
544 PROP_PIXELS_ABOVE_LINES,
545 g_param_spec_int ("pixels-above-lines",
546 P_("Pixels Above Lines"),
547 P_("Pixels of blank space above paragraphs"),
551 GTK_PARAM_READWRITE));
553 g_object_class_install_property (gobject_class,
554 PROP_PIXELS_BELOW_LINES,
555 g_param_spec_int ("pixels-below-lines",
556 P_("Pixels Below Lines"),
557 P_("Pixels of blank space below paragraphs"),
561 GTK_PARAM_READWRITE));
563 g_object_class_install_property (gobject_class,
564 PROP_PIXELS_INSIDE_WRAP,
565 g_param_spec_int ("pixels-inside-wrap",
566 P_("Pixels Inside Wrap"),
567 P_("Pixels of blank space between wrapped lines in a paragraph"),
571 GTK_PARAM_READWRITE));
573 g_object_class_install_property (gobject_class,
575 g_param_spec_boolean ("editable",
577 P_("Whether the text can be modified by the user"),
579 GTK_PARAM_READWRITE));
581 g_object_class_install_property (gobject_class,
583 g_param_spec_enum ("wrap-mode",
585 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
588 GTK_PARAM_READWRITE));
590 g_object_class_install_property (gobject_class,
592 g_param_spec_enum ("justification",
594 P_("Left, right, or center justification"),
595 GTK_TYPE_JUSTIFICATION,
597 GTK_PARAM_READWRITE));
599 g_object_class_install_property (gobject_class,
601 g_param_spec_int ("left-margin",
603 P_("Width of the left margin in pixels"),
607 GTK_PARAM_READWRITE));
609 g_object_class_install_property (gobject_class,
611 g_param_spec_int ("right-margin",
613 P_("Width of the right margin in pixels"),
617 GTK_PARAM_READWRITE));
619 g_object_class_install_property (gobject_class,
621 g_param_spec_int ("indent",
623 P_("Amount to indent the paragraph, in pixels"),
627 GTK_PARAM_READWRITE));
629 g_object_class_install_property (gobject_class,
631 g_param_spec_boxed ("tabs",
633 P_("Custom tabs for this text"),
634 PANGO_TYPE_TAB_ARRAY,
635 GTK_PARAM_READWRITE));
637 g_object_class_install_property (gobject_class,
639 g_param_spec_boolean ("cursor-visible",
640 P_("Cursor Visible"),
641 P_("If the insertion cursor is shown"),
643 GTK_PARAM_READWRITE));
645 g_object_class_install_property (gobject_class,
647 g_param_spec_object ("buffer",
649 P_("The buffer which is displayed"),
650 GTK_TYPE_TEXT_BUFFER,
651 GTK_PARAM_READWRITE));
653 g_object_class_install_property (gobject_class,
655 g_param_spec_boolean ("overwrite",
656 P_("Overwrite mode"),
657 P_("Whether entered text overwrites existing contents"),
659 GTK_PARAM_READWRITE));
661 g_object_class_install_property (gobject_class,
663 g_param_spec_boolean ("accepts-tab",
665 P_("Whether Tab will result in a tab character being entered"),
667 GTK_PARAM_READWRITE));
670 * GtkTextView:im-module:
672 * Which IM (input method) module should be used for this entry.
675 * Setting this to a non-%NULL value overrides the
676 * system-wide IM module setting. See the GtkSettings
677 * #GtkSettings:gtk-im-module property.
681 g_object_class_install_property (gobject_class,
683 g_param_spec_string ("im-module",
685 P_("Which IM module should be used"),
687 GTK_PARAM_READWRITE));
692 gtk_widget_class_install_style_property (widget_class,
693 g_param_spec_boxed ("error-underline-color",
694 P_("Error underline color"),
695 P_("Color with which to draw error-indication underlines"),
697 GTK_PARAM_READABLE));
704 * GtkTextView::move-cursor:
705 * @text_view: the object which received the signal
706 * @step: the granularity of the move, as a #GtkMovementStep
707 * @count: the number of @step units to move
708 * @extend_selection: %TRUE if the move should extend the selection
710 * The ::move-cursor signal is a
711 * <link linkend="keybinding-signals">keybinding signal</link>
712 * which gets emitted when the user initiates a cursor movement.
713 * If the cursor is not visible in @text_view, this signal causes
714 * the viewport to be moved instead.
716 * Applications should not connect to it, but may emit it with
717 * g_signal_emit_by_name() if they need to control the cursor
720 * The default bindings for this signal come in two variants,
721 * the variant with the Shift modifier extends the selection,
722 * the variant without the Shift modifer does not.
723 * There are too many key combinations to list them all here.
725 * <listitem>Arrow keys move by individual characters/lines</listitem>
726 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
727 * <listitem>Home/End keys move to the ends of the buffer</listitem>
728 * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
729 * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
732 signals[MOVE_CURSOR] =
733 g_signal_new (I_("move-cursor"),
734 G_OBJECT_CLASS_TYPE (gobject_class),
735 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
736 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
738 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
740 GTK_TYPE_MOVEMENT_STEP,
745 * GtkTextView::page-horizontally:
746 * @text_view: the object which received the signal
747 * @count: the number of @step units to move
748 * @extend_selection: %TRUE if the move should extend the selection
750 * The ::page-horizontally signal is a
751 * <link linkend="keybinding-signals">keybinding signal</link>
752 * which can be bound to key combinations to allow the user
753 * to initiate horizontal cursor movement by pages.
755 * This signal should not be used anymore, instead use the
756 * #GtkTextview::move-cursor signal with the #GTK_MOVEMENT_HORIZONTAL_PAGES
759 signals[PAGE_HORIZONTALLY] =
760 g_signal_new (I_("page-horizontally"),
761 G_OBJECT_CLASS_TYPE (gobject_class),
762 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
763 G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
765 _gtk_marshal_VOID__INT_BOOLEAN,
771 * GtkTextView::move-viewport:
772 * @text_view: the object which received the signal
773 * @step: the granularity of the move, as a #GtkMovementStep
774 * @count: the number of @step units to move
776 * The ::move-viewport signal is a
777 * <link linkend="keybinding-signals">keybinding signal</link>
778 * which can be bound to key combinations to allow the user
779 * to move the viewport, i.e. change what part of the text view
780 * is visible in a containing scrolled window.
782 * There are no default bindings for this signal.
784 signals[MOVE_VIEWPORT] =
785 g_signal_new_class_handler (I_("move-viewport"),
786 G_OBJECT_CLASS_TYPE (gobject_class),
787 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
788 G_CALLBACK (gtk_text_view_move_viewport),
790 _gtk_marshal_VOID__ENUM_INT,
792 GTK_TYPE_SCROLL_STEP,
796 * GtkTextView::set-anchor:
797 * @text_view: the object which received the signal
799 * The ::set-anchor signal is a
800 * <link linkend="keybinding-signals">keybinding signal</link>
801 * which gets emitted when the user initiates setting the "anchor"
802 * mark. The "anchor" mark gets placed at the same position as the
805 * This signal has no default bindings.
807 signals[SET_ANCHOR] =
808 g_signal_new (I_("set-anchor"),
809 G_OBJECT_CLASS_TYPE (gobject_class),
810 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
811 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
813 _gtk_marshal_VOID__VOID,
817 * GtkTextView::insert-at-cursor:
818 * @text_view: the object which received the signal
819 * @string: the string to insert
821 * The ::insert-at-cursor signal is a
822 * <link linkend="keybinding-signals">keybinding signal</link>
823 * which gets emitted when the user initiates the insertion of a
824 * fixed string at the cursor.
826 * This signal has no default bindings.
828 signals[INSERT_AT_CURSOR] =
829 g_signal_new (I_("insert-at-cursor"),
830 G_OBJECT_CLASS_TYPE (gobject_class),
831 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
832 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
834 _gtk_marshal_VOID__STRING,
839 * GtkTextView::delete-from-cursor:
840 * @text_view: the object which received the signal
841 * @type: the granularity of the deletion, as a #GtkDeleteType
842 * @count: the number of @type units to delete
844 * The ::delete-from-cursor signal is a
845 * <link linkend="keybinding-signals">keybinding signal</link>
846 * which gets emitted when the user initiates a text deletion.
848 * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
849 * if there is one, otherwise it deletes the requested number
852 * The default bindings for this signal are
853 * Delete for deleting a character, Ctrl-Delete for
854 * deleting a word and Ctrl-Backspace for deleting a word
857 signals[DELETE_FROM_CURSOR] =
858 g_signal_new (I_("delete-from-cursor"),
859 G_OBJECT_CLASS_TYPE (gobject_class),
860 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
861 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
863 _gtk_marshal_VOID__ENUM_INT,
865 GTK_TYPE_DELETE_TYPE,
869 * GtkTextView::backspace:
870 * @text_view: the object which received the signal
872 * The ::backspace signal is a
873 * <link linkend="keybinding-signals">keybinding signal</link>
874 * which gets emitted when the user asks for it.
876 * The default bindings for this signal are
877 * Backspace and Shift-Backspace.
880 g_signal_new (I_("backspace"),
881 G_OBJECT_CLASS_TYPE (gobject_class),
882 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
883 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
885 _gtk_marshal_VOID__VOID,
889 * GtkTextView::cut-clipboard:
890 * @text_view: the object which received the signal
892 * The ::cut-clipboard signal is a
893 * <link linkend="keybinding-signals">keybinding signal</link>
894 * which gets emitted to cut the selection to the clipboard.
896 * The default bindings for this signal are
897 * Ctrl-x and Shift-Delete.
899 signals[CUT_CLIPBOARD] =
900 g_signal_new (I_("cut-clipboard"),
901 G_OBJECT_CLASS_TYPE (gobject_class),
902 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
903 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
905 _gtk_marshal_VOID__VOID,
909 * GtkTextView::copy-clipboard:
910 * @text_view: the object which received the signal
912 * The ::copy-clipboard signal is a
913 * <link linkend="keybinding-signals">keybinding signal</link>
914 * which gets emitted to copy the selection to the clipboard.
916 * The default bindings for this signal are
917 * Ctrl-c and Ctrl-Insert.
919 signals[COPY_CLIPBOARD] =
920 g_signal_new (I_("copy-clipboard"),
921 G_OBJECT_CLASS_TYPE (gobject_class),
922 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
923 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
925 _gtk_marshal_VOID__VOID,
929 * GtkTextView::paste-clipboard:
930 * @text_view: the object which received the signal
932 * The ::paste-clipboard signal is a
933 * <link linkend="keybinding-signals">keybinding signal</link>
934 * which gets emitted to paste the contents of the clipboard
935 * into the text view.
937 * The default bindings for this signal are
938 * Ctrl-v and Shift-Insert.
940 signals[PASTE_CLIPBOARD] =
941 g_signal_new (I_("paste-clipboard"),
942 G_OBJECT_CLASS_TYPE (gobject_class),
943 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
944 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
946 _gtk_marshal_VOID__VOID,
950 * GtkTextView::toggle-overwrite:
951 * @text_view: the object which received the signal
953 * The ::toggle-overwrite signal is a
954 * <link linkend="keybinding-signals">keybinding signal</link>
955 * which gets emitted to toggle the overwrite mode of the text view.
957 * The default bindings for this signal is Insert.
959 signals[TOGGLE_OVERWRITE] =
960 g_signal_new (I_("toggle-overwrite"),
961 G_OBJECT_CLASS_TYPE (gobject_class),
962 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
963 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
965 _gtk_marshal_VOID__VOID,
969 * GtkTextView::set-scroll-adjustments
970 * @horizontal: the horizontal #GtkAdjustment
971 * @vertical: the vertical #GtkAdjustment
973 * Set the scroll adjustments for the text view. Usually scrolled containers
974 * like #GtkScrolledWindow will emit this signal to connect two instances
975 * of #GtkScrollbar to the scroll directions of the #GtkTextView.
977 signals[SET_SCROLL_ADJUSTMENTS] =
978 g_signal_new (I_("set-scroll-adjustments"),
979 G_OBJECT_CLASS_TYPE (gobject_class),
980 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
981 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
983 _gtk_marshal_VOID__OBJECT_OBJECT,
986 GTK_TYPE_ADJUSTMENT);
987 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
990 * GtkTextView::populate-popup:
991 * @entry: The text view on which the signal is emitted
992 * @menu: the menu that is being populated
994 * The ::populate-popup signal gets emitted before showing the
995 * context menu of the text view.
997 * If you need to add items to the context menu, connect
998 * to this signal and append your menuitems to the @menu.
1000 signals[POPULATE_POPUP] =
1001 g_signal_new (I_("populate-popup"),
1002 G_OBJECT_CLASS_TYPE (gobject_class),
1004 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
1006 _gtk_marshal_VOID__OBJECT,
1011 * GtkTextView::select-all:
1012 * @text_view: the object which received the signal
1013 * @select: %TRUE to select, %FALSE to unselect
1015 * The ::select-all signal is a
1016 * <link linkend="keybinding-signals">keybinding signal</link>
1017 * which gets emitted to select or unselect the complete
1018 * contents of the text view.
1020 * The default bindings for this signal are Ctrl-a and Ctrl-/
1021 * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
1023 signals[SELECT_ALL] =
1024 g_signal_new_class_handler (I_("select-all"),
1025 G_OBJECT_CLASS_TYPE (object_class),
1026 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1027 G_CALLBACK (gtk_text_view_select_all),
1029 _gtk_marshal_VOID__BOOLEAN,
1030 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
1033 * GtkTextView::toggle-cursor-visible:
1034 * @text_view: the object which received the signal
1036 * The ::toggle-cursor-visible signal is a
1037 * <link linkend="keybinding-signals">keybinding signal</link>
1038 * which gets emitted to toggle the visibility of the cursor.
1040 * The default binding for this signal is F7.
1042 signals[TOGGLE_CURSOR_VISIBLE] =
1043 g_signal_new_class_handler (I_("toggle-cursor-visible"),
1044 G_OBJECT_CLASS_TYPE (object_class),
1045 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1046 G_CALLBACK (gtk_text_view_toggle_cursor_visible),
1048 _gtk_marshal_VOID__VOID,
1052 * GtkTextView::preedit-changed:
1053 * @text_view: the object which received the signal
1054 * @preedit: the current preedit string
1056 * If an input method is used, the typed text will not immediately
1057 * be committed to the buffer. So if you are interested in the text,
1058 * connect to this signal.
1060 * This signal is only emitted if the text at the given position
1061 * is actually editable.
1065 signals[PREEDIT_CHANGED] =
1066 g_signal_new_class_handler (I_("preedit-changed"),
1067 G_OBJECT_CLASS_TYPE (object_class),
1068 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1071 _gtk_marshal_VOID__STRING,
1079 binding_set = gtk_binding_set_by_class (klass);
1081 /* Moving the insertion point */
1082 add_move_binding (binding_set, GDK_Right, 0,
1083 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1085 add_move_binding (binding_set, GDK_KP_Right, 0,
1086 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
1088 add_move_binding (binding_set, GDK_Left, 0,
1089 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1091 add_move_binding (binding_set, GDK_KP_Left, 0,
1092 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1094 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
1095 GTK_MOVEMENT_WORDS, 1);
1097 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
1098 GTK_MOVEMENT_WORDS, 1);
1100 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
1101 GTK_MOVEMENT_WORDS, -1);
1103 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
1104 GTK_MOVEMENT_WORDS, -1);
1106 add_move_binding (binding_set, GDK_Up, 0,
1107 GTK_MOVEMENT_DISPLAY_LINES, -1);
1109 add_move_binding (binding_set, GDK_KP_Up, 0,
1110 GTK_MOVEMENT_DISPLAY_LINES, -1);
1112 add_move_binding (binding_set, GDK_Down, 0,
1113 GTK_MOVEMENT_DISPLAY_LINES, 1);
1115 add_move_binding (binding_set, GDK_KP_Down, 0,
1116 GTK_MOVEMENT_DISPLAY_LINES, 1);
1118 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
1119 GTK_MOVEMENT_PARAGRAPHS, -1);
1121 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
1122 GTK_MOVEMENT_PARAGRAPHS, -1);
1124 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
1125 GTK_MOVEMENT_PARAGRAPHS, 1);
1127 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
1128 GTK_MOVEMENT_PARAGRAPHS, 1);
1130 add_move_binding (binding_set, GDK_Home, 0,
1131 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1133 add_move_binding (binding_set, GDK_KP_Home, 0,
1134 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1136 add_move_binding (binding_set, GDK_End, 0,
1137 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1139 add_move_binding (binding_set, GDK_KP_End, 0,
1140 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1142 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
1143 GTK_MOVEMENT_BUFFER_ENDS, -1);
1145 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
1146 GTK_MOVEMENT_BUFFER_ENDS, -1);
1148 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
1149 GTK_MOVEMENT_BUFFER_ENDS, 1);
1151 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
1152 GTK_MOVEMENT_BUFFER_ENDS, 1);
1154 add_move_binding (binding_set, GDK_Page_Up, 0,
1155 GTK_MOVEMENT_PAGES, -1);
1157 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
1158 GTK_MOVEMENT_PAGES, -1);
1160 add_move_binding (binding_set, GDK_Page_Down, 0,
1161 GTK_MOVEMENT_PAGES, 1);
1163 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
1164 GTK_MOVEMENT_PAGES, 1);
1166 add_move_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
1167 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1169 add_move_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
1170 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1172 add_move_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
1173 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1175 add_move_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
1176 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1179 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
1181 G_TYPE_BOOLEAN, TRUE);
1183 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
1185 G_TYPE_BOOLEAN, TRUE);
1188 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
1190 G_TYPE_BOOLEAN, FALSE);
1192 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1194 G_TYPE_BOOLEAN, FALSE);
1197 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
1198 "delete-from-cursor", 2,
1199 G_TYPE_ENUM, GTK_DELETE_CHARS,
1202 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
1203 "delete-from-cursor", 2,
1204 G_TYPE_ENUM, GTK_DELETE_CHARS,
1207 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
1210 /* Make this do the same as Backspace, to help with mis-typing */
1211 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
1214 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
1215 "delete-from-cursor", 2,
1216 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1219 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
1220 "delete-from-cursor", 2,
1221 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1224 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
1225 "delete-from-cursor", 2,
1226 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1229 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1230 "delete-from-cursor", 2,
1231 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1234 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1235 "delete-from-cursor", 2,
1236 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1239 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1240 "delete-from-cursor", 2,
1241 G_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
1244 /* Cut/copy/paste */
1246 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
1247 "cut-clipboard", 0);
1248 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
1249 "copy-clipboard", 0);
1250 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
1251 "paste-clipboard", 0);
1253 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
1254 "cut-clipboard", 0);
1255 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
1256 "copy-clipboard", 0);
1257 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
1258 "paste-clipboard", 0);
1261 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1262 "toggle-overwrite", 0);
1263 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1264 "toggle-overwrite", 0);
1267 gtk_binding_entry_add_signal (binding_set, GDK_F7, 0,
1268 "toggle-cursor-visible", 0);
1270 /* Control-tab focus motion */
1271 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
1273 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1274 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
1276 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1278 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1280 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1281 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1283 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1285 g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1289 gtk_text_view_init (GtkTextView *text_view)
1291 GtkWidget *widget = GTK_WIDGET (text_view);
1292 GtkTargetList *target_list;
1294 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1296 /* Set up default style */
1297 text_view->wrap_mode = GTK_WRAP_NONE;
1298 text_view->pixels_above_lines = 0;
1299 text_view->pixels_below_lines = 0;
1300 text_view->pixels_inside_wrap = 0;
1301 text_view->justify = GTK_JUSTIFY_LEFT;
1302 text_view->left_margin = 0;
1303 text_view->right_margin = 0;
1304 text_view->indent = 0;
1305 text_view->tabs = NULL;
1306 text_view->editable = TRUE;
1308 gtk_drag_dest_set (widget, 0, NULL, 0,
1309 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1311 target_list = gtk_target_list_new (NULL, 0);
1312 gtk_drag_dest_set_target_list (widget, target_list);
1313 gtk_target_list_unref (target_list);
1315 text_view->virtual_cursor_x = -1;
1316 text_view->virtual_cursor_y = -1;
1318 /* This object is completely private. No external entity can gain a reference
1319 * to it; so we create it here and destroy it in finalize ().
1321 text_view->im_context = gtk_im_multicontext_new ();
1323 g_signal_connect (text_view->im_context, "commit",
1324 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1325 g_signal_connect (text_view->im_context, "preedit-changed",
1326 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1327 g_signal_connect (text_view->im_context, "retrieve-surrounding",
1328 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1329 g_signal_connect (text_view->im_context, "delete-surrounding",
1330 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1332 text_view->cursor_visible = TRUE;
1334 text_view->accepts_tab = TRUE;
1336 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1339 text_view->drag_start_x = -1;
1340 text_view->drag_start_y = -1;
1342 text_view->pending_place_cursor_button = 0;
1344 /* We handle all our own redrawing */
1345 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1349 * gtk_text_view_new:
1351 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1352 * before using the text view, an empty default buffer will be created
1353 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1354 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1356 * Return value: a new #GtkTextView
1359 gtk_text_view_new (void)
1361 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1365 * gtk_text_view_new_with_buffer:
1366 * @buffer: a #GtkTextBuffer
1368 * Creates a new #GtkTextView widget displaying the buffer
1369 * @buffer. One buffer can be shared among many widgets.
1370 * @buffer may be %NULL to create a default buffer, in which case
1371 * this function is equivalent to gtk_text_view_new(). The
1372 * text view adds its own reference count to the buffer; it does not
1373 * take over an existing reference.
1375 * Return value: a new #GtkTextView.
1378 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1380 GtkTextView *text_view;
1382 text_view = (GtkTextView*)gtk_text_view_new ();
1384 gtk_text_view_set_buffer (text_view, buffer);
1386 return GTK_WIDGET (text_view);
1390 * gtk_text_view_set_buffer:
1391 * @text_view: a #GtkTextView
1392 * @buffer: (allow-none): a #GtkTextBuffer
1394 * Sets @buffer as the buffer being displayed by @text_view. The previous
1395 * buffer displayed by the text view is unreferenced, and a reference is
1396 * added to @buffer. If you owned a reference to @buffer before passing it
1397 * to this function, you must remove that reference yourself; #GtkTextView
1398 * will not "adopt" it.
1401 gtk_text_view_set_buffer (GtkTextView *text_view,
1402 GtkTextBuffer *buffer)
1404 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1405 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1407 if (text_view->buffer == buffer)
1410 if (text_view->buffer != NULL)
1412 /* Destroy all anchored children */
1416 copy = g_slist_copy (text_view->children);
1418 while (tmp_list != NULL)
1420 GtkTextViewChild *vc = tmp_list->data;
1424 gtk_widget_destroy (vc->widget);
1425 /* vc may now be invalid! */
1428 tmp_list = g_slist_next (tmp_list);
1431 g_slist_free (copy);
1433 g_signal_handlers_disconnect_by_func (text_view->buffer,
1434 gtk_text_view_mark_set_handler,
1436 g_signal_handlers_disconnect_by_func (text_view->buffer,
1437 gtk_text_view_target_list_notify,
1439 g_signal_handlers_disconnect_by_func (text_view->buffer,
1440 gtk_text_view_paste_done_handler,
1443 if (GTK_WIDGET_REALIZED (text_view))
1445 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1446 GDK_SELECTION_PRIMARY);
1447 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1450 if (text_view->layout)
1451 gtk_text_layout_set_buffer (text_view->layout, NULL);
1453 g_object_unref (text_view->buffer);
1454 text_view->dnd_mark = NULL;
1455 text_view->first_para_mark = NULL;
1456 cancel_pending_scroll (text_view);
1459 text_view->buffer = buffer;
1461 if (text_view->layout)
1462 gtk_text_layout_set_buffer (text_view->layout, buffer);
1468 g_object_ref (buffer);
1470 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1472 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1476 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1480 text_view->first_para_pixels = 0;
1482 g_signal_connect (text_view->buffer, "mark-set",
1483 G_CALLBACK (gtk_text_view_mark_set_handler),
1485 g_signal_connect (text_view->buffer, "notify::paste-target-list",
1486 G_CALLBACK (gtk_text_view_target_list_notify),
1488 g_signal_connect (text_view->buffer, "paste-done",
1489 G_CALLBACK (gtk_text_view_paste_done_handler),
1492 gtk_text_view_target_list_notify (text_view->buffer, NULL, text_view);
1494 if (GTK_WIDGET_REALIZED (text_view))
1496 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1497 GDK_SELECTION_PRIMARY);
1498 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1502 g_object_notify (G_OBJECT (text_view), "buffer");
1504 if (gtk_widget_get_visible (GTK_WIDGET (text_view)))
1505 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1507 DV(g_print ("Invalidating due to set_buffer\n"));
1508 gtk_text_view_invalidate (text_view);
1511 static GtkTextBuffer*
1512 get_buffer (GtkTextView *text_view)
1514 if (text_view->buffer == NULL)
1517 b = gtk_text_buffer_new (NULL);
1518 gtk_text_view_set_buffer (text_view, b);
1522 return text_view->buffer;
1526 * gtk_text_view_get_buffer:
1527 * @text_view: a #GtkTextView
1529 * Returns the #GtkTextBuffer being displayed by this text view.
1530 * The reference count on the buffer is not incremented; the caller
1531 * of this function won't own a new reference.
1533 * Return value: (transfer none): a #GtkTextBuffer
1536 gtk_text_view_get_buffer (GtkTextView *text_view)
1538 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1540 return get_buffer (text_view);
1544 * gtk_text_view_get_iter_at_location:
1545 * @text_view: a #GtkTextView
1546 * @iter: a #GtkTextIter
1547 * @x: x position, in buffer coordinates
1548 * @y: y position, in buffer coordinates
1550 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1551 * coordinates are coordinates for the entire buffer, not just the
1552 * currently-displayed portion. If you have coordinates from an
1553 * event, you have to convert those to buffer coordinates with
1554 * gtk_text_view_window_to_buffer_coords().
1557 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1562 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1563 g_return_if_fail (iter != NULL);
1565 gtk_text_view_ensure_layout (text_view);
1567 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1572 * gtk_text_view_get_iter_at_position:
1573 * @text_view: a #GtkTextView
1574 * @iter: a #GtkTextIter
1575 * @trailing: if non-%NULL, location to store an integer indicating where
1576 * in the grapheme the user clicked. It will either be
1577 * zero, or the number of characters in the grapheme.
1578 * 0 represents the trailing edge of the grapheme.
1579 * @x: x position, in buffer coordinates
1580 * @y: y position, in buffer coordinates
1582 * Retrieves the iterator pointing to the character at buffer
1583 * coordinates @x and @y. Buffer coordinates are coordinates for
1584 * the entire buffer, not just the currently-displayed portion.
1585 * If you have coordinates from an event, you have to convert
1586 * those to buffer coordinates with
1587 * gtk_text_view_window_to_buffer_coords().
1589 * Note that this is different from gtk_text_view_get_iter_at_location(),
1590 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1596 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1602 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1603 g_return_if_fail (iter != NULL);
1605 gtk_text_view_ensure_layout (text_view);
1607 gtk_text_layout_get_iter_at_position (text_view->layout,
1608 iter, trailing, x, y);
1612 * gtk_text_view_get_iter_location:
1613 * @text_view: a #GtkTextView
1614 * @iter: a #GtkTextIter
1615 * @location: bounds of the character at @iter
1617 * Gets a rectangle which roughly contains the character at @iter.
1618 * The rectangle position is in buffer coordinates; use
1619 * gtk_text_view_buffer_to_window_coords() to convert these
1620 * coordinates to coordinates for one of the windows in the text view.
1623 gtk_text_view_get_iter_location (GtkTextView *text_view,
1624 const GtkTextIter *iter,
1625 GdkRectangle *location)
1627 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1628 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1630 gtk_text_view_ensure_layout (text_view);
1632 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1636 * gtk_text_view_get_line_yrange:
1637 * @text_view: a #GtkTextView
1638 * @iter: a #GtkTextIter
1639 * @y: return location for a y coordinate
1640 * @height: return location for a height
1642 * Gets the y coordinate of the top of the line containing @iter,
1643 * and the height of the line. The coordinate is a buffer coordinate;
1644 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1647 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1648 const GtkTextIter *iter,
1652 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1653 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1655 gtk_text_view_ensure_layout (text_view);
1657 gtk_text_layout_get_line_yrange (text_view->layout,
1664 * gtk_text_view_get_line_at_y:
1665 * @text_view: a #GtkTextView
1666 * @target_iter: a #GtkTextIter
1667 * @y: a y coordinate
1668 * @line_top: return location for top coordinate of the line
1670 * Gets the #GtkTextIter at the start of the line containing
1671 * the coordinate @y. @y is in buffer coordinates, convert from
1672 * window coordinates with gtk_text_view_window_to_buffer_coords().
1673 * If non-%NULL, @line_top will be filled with the coordinate of the top
1677 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1678 GtkTextIter *target_iter,
1682 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1684 gtk_text_view_ensure_layout (text_view);
1686 gtk_text_layout_get_line_at_y (text_view->layout,
1693 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1695 DV (g_print (" Setting adj to raw value %g\n", val));
1697 /* We don't really want to clamp to upper; we want to clamp to
1698 upper - page_size which is the highest value the scrollbar
1699 will let us reach. */
1700 if (val > (adj->upper - adj->page_size))
1701 val = adj->upper - adj->page_size;
1703 if (val < adj->lower)
1706 if (val != adj->value)
1708 DV (g_print (" Setting adj to clamped value %g\n", val));
1709 gtk_adjustment_set_value (adj, val);
1717 * gtk_text_view_scroll_to_iter:
1718 * @text_view: a #GtkTextView
1719 * @iter: a #GtkTextIter
1720 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1721 * @use_align: whether to use alignment arguments (if %FALSE,
1722 * just get the mark onscreen)
1723 * @xalign: horizontal alignment of mark within visible area
1724 * @yalign: vertical alignment of mark within visible area
1726 * Scrolls @text_view so that @iter is on the screen in the position
1727 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1728 * left or top, 1.0 indicates right or bottom, 0.5 means center.
1729 * If @use_align is %FALSE, the text scrolls the minimal distance to
1730 * get the mark onscreen, possibly not scrolling at all. The effective
1731 * screen for purposes of this function is reduced by a margin of size
1734 * Note that this function uses the currently-computed height of the
1735 * lines in the text buffer. Line heights are computed in an idle
1736 * handler; so this function may not have the desired effect if it's
1737 * called before the height computations. To avoid oddness, consider
1738 * using gtk_text_view_scroll_to_mark() which saves a point to be
1739 * scrolled to after line validation.
1741 * Return value: %TRUE if scrolling occurred
1744 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1746 gdouble within_margin,
1752 GdkRectangle screen;
1757 gboolean retval = FALSE;
1759 gint screen_xoffset, screen_yoffset;
1760 gint current_x_scroll, current_y_scroll;
1762 /* FIXME why don't we do the validate-at-scroll-destination thing
1763 * from flush_scroll in this function? I think it wasn't done before
1764 * because changed_handler was screwed up, but I could be wrong.
1767 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1768 g_return_val_if_fail (iter != NULL, FALSE);
1769 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1770 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1771 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1773 widget = GTK_WIDGET (text_view);
1775 DV(g_print(G_STRLOC"\n"));
1777 gtk_text_layout_get_iter_location (text_view->layout,
1781 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1783 current_x_scroll = text_view->xoffset;
1784 current_y_scroll = text_view->yoffset;
1786 screen.x = current_x_scroll;
1787 screen.y = current_y_scroll;
1788 screen.width = SCREEN_WIDTH (widget);
1789 screen.height = SCREEN_HEIGHT (widget);
1791 screen_xoffset = screen.width * within_margin;
1792 screen_yoffset = screen.height * within_margin;
1794 screen.x += screen_xoffset;
1795 screen.y += screen_yoffset;
1796 screen.width -= screen_xoffset * 2;
1797 screen.height -= screen_yoffset * 2;
1799 /* paranoia check */
1800 if (screen.width < 1)
1802 if (screen.height < 1)
1805 /* The -1 here ensures that we leave enough space to draw the cursor
1806 * when this function is used for horizontal scrolling.
1808 screen_right = screen.x + screen.width - 1;
1809 screen_bottom = screen.y + screen.height;
1811 /* The alignment affects the point in the target character that we
1812 * choose to align. If we're doing right/bottom alignment, we align
1813 * the right/bottom edge of the character the mark is at; if we're
1814 * doing left/top we align the left/top edge of the character; if
1815 * we're doing center alignment we align the center of the
1819 /* Vertical scroll */
1822 scroll_dest = current_y_scroll;
1826 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1828 /* if scroll_dest < screen.y, we move a negative increment (up),
1829 * else a positive increment (down)
1831 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1835 /* move minimum to get onscreen */
1836 if (rect.y < screen.y)
1838 scroll_dest = rect.y;
1839 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1841 else if ((rect.y + rect.height) > screen_bottom)
1843 scroll_dest = rect.y + rect.height;
1844 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1848 if (scroll_inc != 0)
1850 retval = set_adjustment_clamped (get_vadjustment (text_view),
1851 current_y_scroll + scroll_inc);
1853 DV (g_print (" vert increment %d\n", scroll_inc));
1856 /* Horizontal scroll */
1859 scroll_dest = current_x_scroll;
1863 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1865 /* if scroll_dest < screen.y, we move a negative increment (left),
1866 * else a positive increment (right)
1868 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1872 /* move minimum to get onscreen */
1873 if (rect.x < screen.x)
1875 scroll_dest = rect.x;
1876 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1878 else if ((rect.x + rect.width) > screen_right)
1880 scroll_dest = rect.x + rect.width;
1881 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1885 if (scroll_inc != 0)
1887 retval = set_adjustment_clamped (get_hadjustment (text_view),
1888 current_x_scroll + scroll_inc);
1890 DV (g_print (" horiz increment %d\n", scroll_inc));
1895 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1899 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1906 free_pending_scroll (GtkTextPendingScroll *scroll)
1908 if (!gtk_text_mark_get_deleted (scroll->mark))
1909 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1911 g_object_unref (scroll->mark);
1916 cancel_pending_scroll (GtkTextView *text_view)
1918 if (text_view->pending_scroll)
1920 free_pending_scroll (text_view->pending_scroll);
1921 text_view->pending_scroll = NULL;
1926 gtk_text_view_queue_scroll (GtkTextView *text_view,
1928 gdouble within_margin,
1934 GtkTextPendingScroll *scroll;
1936 DV(g_print(G_STRLOC"\n"));
1938 scroll = g_new (GtkTextPendingScroll, 1);
1940 scroll->within_margin = within_margin;
1941 scroll->use_align = use_align;
1942 scroll->xalign = xalign;
1943 scroll->yalign = yalign;
1945 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1947 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1950 gtk_text_mark_get_left_gravity (mark));
1952 g_object_ref (scroll->mark);
1954 cancel_pending_scroll (text_view);
1956 text_view->pending_scroll = scroll;
1960 gtk_text_view_flush_scroll (GtkTextView *text_view)
1963 GtkTextPendingScroll *scroll;
1967 widget = GTK_WIDGET (text_view);
1969 DV(g_print(G_STRLOC"\n"));
1971 if (text_view->pending_scroll == NULL)
1973 DV (g_print ("in flush scroll, no pending scroll\n"));
1977 scroll = text_view->pending_scroll;
1979 /* avoid recursion */
1980 text_view->pending_scroll = NULL;
1982 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1984 /* Validate area around the scroll destination, so the adjustment
1985 * can meaningfully point into that area. We must validate
1986 * enough area to be sure that after we scroll, everything onscreen
1987 * is valid; otherwise, validation will maintain the first para
1988 * in one place, but may push the target iter off the bottom of
1991 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1992 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1993 - (widget->allocation.height * 2),
1994 widget->allocation.height * 2);
1996 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1998 /* Ensure we have updated width/height */
1999 gtk_text_view_update_adjustments (text_view);
2001 retval = gtk_text_view_scroll_to_iter (text_view,
2003 scroll->within_margin,
2008 free_pending_scroll (scroll);
2014 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
2016 if (upper != adj->upper)
2018 gdouble min = MAX (0.0, upper - adj->page_size);
2019 gboolean value_changed = FALSE;
2023 if (adj->value > min)
2026 value_changed = TRUE;
2029 gtk_adjustment_changed (adj);
2030 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
2034 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
2035 gtk_adjustment_value_changed (adj);
2041 gtk_text_view_update_adjustments (GtkTextView *text_view)
2043 gint width = 0, height = 0;
2045 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
2047 if (text_view->layout)
2048 gtk_text_layout_get_size (text_view->layout, &width, &height);
2050 /* Make room for the cursor after the last character in the widest line */
2051 width += SPACE_FOR_CURSOR;
2053 if (text_view->width != width || text_view->height != height)
2055 if (text_view->width != width)
2056 text_view->width_changed = TRUE;
2058 text_view->width = width;
2059 text_view->height = height;
2061 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
2062 MAX (SCREEN_WIDTH (text_view), width));
2063 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
2064 MAX (SCREEN_HEIGHT (text_view), height));
2066 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
2068 /* Set up the step sizes; we'll say that a page is
2069 our allocation minus one step, and a step is
2070 1/10 of our allocation. */
2071 text_view->hadjustment->step_increment =
2072 SCREEN_WIDTH (text_view) / 10.0;
2073 text_view->hadjustment->page_increment =
2074 SCREEN_WIDTH (text_view) * 0.9;
2076 text_view->vadjustment->step_increment =
2077 SCREEN_HEIGHT (text_view) / 10.0;
2078 text_view->vadjustment->page_increment =
2079 SCREEN_HEIGHT (text_view) * 0.9;
2081 gtk_adjustment_changed (get_hadjustment (text_view));
2082 gtk_adjustment_changed (get_vadjustment (text_view));
2087 gtk_text_view_update_layout_width (GtkTextView *text_view)
2089 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
2091 gtk_text_view_ensure_layout (text_view);
2093 gtk_text_layout_set_screen_width (text_view->layout,
2094 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
2098 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
2102 if (text_view->layout == NULL)
2105 gtk_text_view_get_cursor_location (text_view, &area);
2107 area.x -= text_view->xoffset;
2108 area.y -= text_view->yoffset;
2110 /* Width returned by Pango indicates direction of cursor,
2111 * by it's sign more than the size of cursor.
2115 gtk_im_context_set_cursor_location (text_view->im_context, &area);
2119 do_update_im_spot_location (gpointer text_view)
2121 GtkTextViewPrivate *priv;
2123 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2124 priv->im_spot_idle = 0;
2126 gtk_text_view_update_im_spot_location (text_view);
2131 queue_update_im_spot_location (GtkTextView *text_view)
2133 GtkTextViewPrivate *priv;
2135 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2137 /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
2138 * so we don't wait until the entire buffer has been validated. */
2139 if (!priv->im_spot_idle)
2140 priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
2141 do_update_im_spot_location,
2147 flush_update_im_spot_location (GtkTextView *text_view)
2149 GtkTextViewPrivate *priv;
2151 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2153 if (priv->im_spot_idle)
2155 g_source_remove (priv->im_spot_idle);
2156 priv->im_spot_idle = 0;
2157 gtk_text_view_update_im_spot_location (text_view);
2162 * gtk_text_view_scroll_to_mark:
2163 * @text_view: a #GtkTextView
2164 * @mark: a #GtkTextMark
2165 * @within_margin: margin as a [0.0,0.5) fraction of screen size
2166 * @use_align: whether to use alignment arguments (if %FALSE, just
2167 * get the mark onscreen)
2168 * @xalign: horizontal alignment of mark within visible area
2169 * @yalign: vertical alignment of mark within visible area
2171 * Scrolls @text_view so that @mark is on the screen in the position
2172 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
2173 * left or top, 1.0 indicates right or bottom, 0.5 means center.
2174 * If @use_align is %FALSE, the text scrolls the minimal distance to
2175 * get the mark onscreen, possibly not scrolling at all. The effective
2176 * screen for purposes of this function is reduced by a margin of size
2180 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
2182 gdouble within_margin,
2187 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2188 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2189 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
2190 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
2191 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
2193 /* We need to verify that the buffer contains the mark, otherwise this
2194 * can lead to data structure corruption later on.
2196 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2198 gtk_text_view_queue_scroll (text_view, mark,
2204 /* If no validation is pending, we need to go ahead and force an
2207 if (text_view->layout &&
2208 gtk_text_layout_is_valid (text_view->layout))
2209 gtk_text_view_flush_scroll (text_view);
2213 * gtk_text_view_scroll_mark_onscreen:
2214 * @text_view: a #GtkTextView
2215 * @mark: a mark in the buffer for @text_view
2217 * Scrolls @text_view the minimum distance such that @mark is contained
2218 * within the visible area of the widget.
2221 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
2224 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2225 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2227 /* We need to verify that the buffer contains the mark, otherwise this
2228 * can lead to data structure corruption later on.
2230 g_return_if_fail (get_buffer (text_view) == gtk_text_mark_get_buffer (mark));
2232 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
2236 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
2238 GdkRectangle visible_rect;
2239 gtk_text_view_get_visible_rect (text_view, &visible_rect);
2241 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
2243 visible_rect.y + visible_rect.height);
2247 * gtk_text_view_move_mark_onscreen:
2248 * @text_view: a #GtkTextView
2249 * @mark: a #GtkTextMark
2251 * Moves a mark within the buffer so that it's
2252 * located within the currently-visible text area.
2254 * Return value: %TRUE if the mark moved (wasn't already onscreen)
2257 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
2262 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2263 g_return_val_if_fail (mark != NULL, FALSE);
2265 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2267 if (clamp_iter_onscreen (text_view, &iter))
2269 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
2277 * gtk_text_view_get_visible_rect:
2278 * @text_view: a #GtkTextView
2279 * @visible_rect: rectangle to fill
2281 * Fills @visible_rect with the currently-visible
2282 * region of the buffer, in buffer coordinates. Convert to window coordinates
2283 * with gtk_text_view_buffer_to_window_coords().
2286 gtk_text_view_get_visible_rect (GtkTextView *text_view,
2287 GdkRectangle *visible_rect)
2291 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2293 widget = GTK_WIDGET (text_view);
2297 visible_rect->x = text_view->xoffset;
2298 visible_rect->y = text_view->yoffset;
2299 visible_rect->width = SCREEN_WIDTH (widget);
2300 visible_rect->height = SCREEN_HEIGHT (widget);
2302 DV(g_print(" visible rect: %d,%d %d x %d\n",
2305 visible_rect->width,
2306 visible_rect->height));
2311 * gtk_text_view_set_wrap_mode:
2312 * @text_view: a #GtkTextView
2313 * @wrap_mode: a #GtkWrapMode
2315 * Sets the line wrapping for the view.
2318 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
2319 GtkWrapMode wrap_mode)
2321 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2323 if (text_view->wrap_mode != wrap_mode)
2325 text_view->wrap_mode = wrap_mode;
2327 if (text_view->layout)
2329 text_view->layout->default_style->wrap_mode = wrap_mode;
2330 gtk_text_layout_default_style_changed (text_view->layout);
2334 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2338 * gtk_text_view_get_wrap_mode:
2339 * @text_view: a #GtkTextView
2341 * Gets the line wrapping for the view.
2343 * Return value: the line wrap setting
2346 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2348 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2350 return text_view->wrap_mode;
2354 * gtk_text_view_set_editable:
2355 * @text_view: a #GtkTextView
2356 * @setting: whether it's editable
2358 * Sets the default editability of the #GtkTextView. You can override
2359 * this default setting with tags in the buffer, using the "editable"
2360 * attribute of tags.
2363 gtk_text_view_set_editable (GtkTextView *text_view,
2366 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2367 setting = setting != FALSE;
2369 if (text_view->editable != setting)
2373 gtk_text_view_reset_im_context(text_view);
2374 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2375 gtk_im_context_focus_out (text_view->im_context);
2378 text_view->editable = setting;
2380 if (setting && gtk_widget_has_focus (GTK_WIDGET (text_view)))
2381 gtk_im_context_focus_in (text_view->im_context);
2383 if (text_view->layout)
2385 gtk_text_layout_set_overwrite_mode (text_view->layout,
2386 text_view->overwrite_mode && text_view->editable);
2387 text_view->layout->default_style->editable = text_view->editable;
2388 gtk_text_layout_default_style_changed (text_view->layout);
2391 g_object_notify (G_OBJECT (text_view), "editable");
2396 * gtk_text_view_get_editable:
2397 * @text_view: a #GtkTextView
2399 * Returns the default editability of the #GtkTextView. Tags in the
2400 * buffer may override this setting for some ranges of text.
2402 * Return value: whether text is editable by default
2405 gtk_text_view_get_editable (GtkTextView *text_view)
2407 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2409 return text_view->editable;
2413 * gtk_text_view_set_pixels_above_lines:
2414 * @text_view: a #GtkTextView
2415 * @pixels_above_lines: pixels above paragraphs
2417 * Sets the default number of blank pixels above paragraphs in @text_view.
2418 * Tags in the buffer for @text_view may override the defaults.
2421 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2422 gint pixels_above_lines)
2424 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2426 if (text_view->pixels_above_lines != pixels_above_lines)
2428 text_view->pixels_above_lines = pixels_above_lines;
2430 if (text_view->layout)
2432 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
2433 gtk_text_layout_default_style_changed (text_view->layout);
2436 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2441 * gtk_text_view_get_pixels_above_lines:
2442 * @text_view: a #GtkTextView
2444 * Gets the default number of pixels to put above paragraphs.
2446 * Return value: default number of pixels above paragraphs
2449 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2451 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2453 return text_view->pixels_above_lines;
2457 * gtk_text_view_set_pixels_below_lines:
2458 * @text_view: a #GtkTextView
2459 * @pixels_below_lines: pixels below paragraphs
2461 * Sets the default number of pixels of blank space
2462 * to put below paragraphs in @text_view. May be overridden
2463 * by tags applied to @text_view's buffer.
2466 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2467 gint pixels_below_lines)
2469 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2471 if (text_view->pixels_below_lines != pixels_below_lines)
2473 text_view->pixels_below_lines = pixels_below_lines;
2475 if (text_view->layout)
2477 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2478 gtk_text_layout_default_style_changed (text_view->layout);
2481 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2486 * gtk_text_view_get_pixels_below_lines:
2487 * @text_view: a #GtkTextView
2489 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2491 * Return value: default number of blank pixels below paragraphs
2494 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2496 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2498 return text_view->pixels_below_lines;
2502 * gtk_text_view_set_pixels_inside_wrap:
2503 * @text_view: a #GtkTextView
2504 * @pixels_inside_wrap: default number of pixels between wrapped lines
2506 * Sets the default number of pixels of blank space to leave between
2507 * display/wrapped lines within a paragraph. May be overridden by
2508 * tags in @text_view's buffer.
2511 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2512 gint pixels_inside_wrap)
2514 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2516 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2518 text_view->pixels_inside_wrap = pixels_inside_wrap;
2520 if (text_view->layout)
2522 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2523 gtk_text_layout_default_style_changed (text_view->layout);
2526 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2531 * gtk_text_view_get_pixels_inside_wrap:
2532 * @text_view: a #GtkTextView
2534 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2536 * Return value: default number of pixels of blank space between wrapped lines
2539 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2541 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2543 return text_view->pixels_inside_wrap;
2547 * gtk_text_view_set_justification:
2548 * @text_view: a #GtkTextView
2549 * @justification: justification
2551 * Sets the default justification of text in @text_view.
2552 * Tags in the view's buffer may override the default.
2556 gtk_text_view_set_justification (GtkTextView *text_view,
2557 GtkJustification justification)
2559 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2561 if (text_view->justify != justification)
2563 text_view->justify = justification;
2565 if (text_view->layout)
2567 text_view->layout->default_style->justification = justification;
2568 gtk_text_layout_default_style_changed (text_view->layout);
2571 g_object_notify (G_OBJECT (text_view), "justification");
2576 * gtk_text_view_get_justification:
2577 * @text_view: a #GtkTextView
2579 * Gets the default justification of paragraphs in @text_view.
2580 * Tags in the buffer may override the default.
2582 * Return value: default justification
2585 gtk_text_view_get_justification (GtkTextView *text_view)
2587 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2589 return text_view->justify;
2593 * gtk_text_view_set_left_margin:
2594 * @text_view: a #GtkTextView
2595 * @left_margin: left margin in pixels
2597 * Sets the default left margin for text in @text_view.
2598 * Tags in the buffer may override the default.
2601 gtk_text_view_set_left_margin (GtkTextView *text_view,
2604 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2606 if (text_view->left_margin != left_margin)
2608 text_view->left_margin = left_margin;
2610 if (text_view->layout)
2612 text_view->layout->default_style->left_margin = left_margin;
2613 gtk_text_layout_default_style_changed (text_view->layout);
2616 g_object_notify (G_OBJECT (text_view), "left-margin");
2621 * gtk_text_view_get_left_margin:
2622 * @text_view: a #GtkTextView
2624 * Gets the default left margin size of paragraphs in the @text_view.
2625 * Tags in the buffer may override the default.
2627 * Return value: left margin in pixels
2630 gtk_text_view_get_left_margin (GtkTextView *text_view)
2632 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2634 return text_view->left_margin;
2638 * gtk_text_view_set_right_margin:
2639 * @text_view: a #GtkTextView
2640 * @right_margin: right margin in pixels
2642 * Sets the default right margin for text in the text view.
2643 * Tags in the buffer may override the default.
2646 gtk_text_view_set_right_margin (GtkTextView *text_view,
2649 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2651 if (text_view->right_margin != right_margin)
2653 text_view->right_margin = right_margin;
2655 if (text_view->layout)
2657 text_view->layout->default_style->right_margin = right_margin;
2658 gtk_text_layout_default_style_changed (text_view->layout);
2661 g_object_notify (G_OBJECT (text_view), "right-margin");
2666 * gtk_text_view_get_right_margin:
2667 * @text_view: a #GtkTextView
2669 * Gets the default right margin for text in @text_view. Tags
2670 * in the buffer may override the default.
2672 * Return value: right margin in pixels
2675 gtk_text_view_get_right_margin (GtkTextView *text_view)
2677 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2679 return text_view->right_margin;
2683 * gtk_text_view_set_indent:
2684 * @text_view: a #GtkTextView
2685 * @indent: indentation in pixels
2687 * Sets the default indentation for paragraphs in @text_view.
2688 * Tags in the buffer may override the default.
2691 gtk_text_view_set_indent (GtkTextView *text_view,
2694 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2696 if (text_view->indent != indent)
2698 text_view->indent = indent;
2700 if (text_view->layout)
2702 text_view->layout->default_style->indent = indent;
2703 gtk_text_layout_default_style_changed (text_view->layout);
2706 g_object_notify (G_OBJECT (text_view), "indent");
2711 * gtk_text_view_get_indent:
2712 * @text_view: a #GtkTextView
2714 * Gets the default indentation of paragraphs in @text_view.
2715 * Tags in the view's buffer may override the default.
2716 * The indentation may be negative.
2718 * Return value: number of pixels of indentation
2721 gtk_text_view_get_indent (GtkTextView *text_view)
2723 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2725 return text_view->indent;
2729 * gtk_text_view_set_tabs:
2730 * @text_view: a #GtkTextView
2731 * @tabs: tabs as a #PangoTabArray
2733 * Sets the default tab stops for paragraphs in @text_view.
2734 * Tags in the buffer may override the default.
2737 gtk_text_view_set_tabs (GtkTextView *text_view,
2738 PangoTabArray *tabs)
2740 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2742 if (text_view->tabs)
2743 pango_tab_array_free (text_view->tabs);
2745 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2747 if (text_view->layout)
2749 /* some unkosher futzing in internal struct details... */
2750 if (text_view->layout->default_style->tabs)
2751 pango_tab_array_free (text_view->layout->default_style->tabs);
2753 text_view->layout->default_style->tabs =
2754 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2756 gtk_text_layout_default_style_changed (text_view->layout);
2759 g_object_notify (G_OBJECT (text_view), "tabs");
2763 * gtk_text_view_get_tabs:
2764 * @text_view: a #GtkTextView
2766 * Gets the default tabs for @text_view. Tags in the buffer may
2767 * override the defaults. The returned array will be %NULL if
2768 * "standard" (8-space) tabs are used. Free the return value
2769 * with pango_tab_array_free().
2771 * Return value: copy of default tab array, or %NULL if "standard"
2772 * tabs are used; must be freed with pango_tab_array_free().
2775 gtk_text_view_get_tabs (GtkTextView *text_view)
2777 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2779 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2783 gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
2785 gtk_text_view_set_cursor_visible (text_view, !text_view->cursor_visible);
2789 * gtk_text_view_set_cursor_visible:
2790 * @text_view: a #GtkTextView
2791 * @setting: whether to show the insertion cursor
2793 * Toggles whether the insertion point is displayed. A buffer with no editable
2794 * text probably shouldn't have a visible cursor, so you may want to turn
2798 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2801 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2803 setting = (setting != FALSE);
2805 if (text_view->cursor_visible != setting)
2807 text_view->cursor_visible = setting;
2809 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
2811 if (text_view->layout)
2813 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2814 gtk_text_view_check_cursor_blink (text_view);
2818 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2823 * gtk_text_view_get_cursor_visible:
2824 * @text_view: a #GtkTextView
2826 * Find out whether the cursor is being displayed.
2828 * Return value: whether the insertion mark is visible
2831 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2833 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2835 return text_view->cursor_visible;
2840 * gtk_text_view_place_cursor_onscreen:
2841 * @text_view: a #GtkTextView
2843 * Moves the cursor to the currently visible region of the
2844 * buffer, it it isn't there already.
2846 * Return value: %TRUE if the cursor had to be moved.
2849 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2853 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2855 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2856 gtk_text_buffer_get_insert (get_buffer (text_view)));
2858 if (clamp_iter_onscreen (text_view, &insert))
2860 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2868 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2870 if (text_view->first_validate_idle != 0)
2872 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2873 g_source_remove (text_view->first_validate_idle);
2874 text_view->first_validate_idle = 0;
2877 if (text_view->incremental_validate_idle != 0)
2879 g_source_remove (text_view->incremental_validate_idle);
2880 text_view->incremental_validate_idle = 0;
2885 gtk_text_view_destroy (GtkObject *object)
2887 GtkTextView *text_view;
2888 GtkTextViewPrivate *priv;
2890 text_view = GTK_TEXT_VIEW (object);
2891 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2893 gtk_text_view_remove_validate_idles (text_view);
2894 gtk_text_view_set_buffer (text_view, NULL);
2895 gtk_text_view_destroy_layout (text_view);
2897 if (text_view->scroll_timeout)
2899 g_source_remove (text_view->scroll_timeout);
2900 text_view->scroll_timeout = 0;
2903 if (priv->im_spot_idle)
2905 g_source_remove (priv->im_spot_idle);
2906 priv->im_spot_idle = 0;
2909 GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy (object);
2913 gtk_text_view_finalize (GObject *object)
2915 GtkTextView *text_view;
2916 GtkTextViewPrivate *priv;
2918 text_view = GTK_TEXT_VIEW (object);
2919 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2921 g_assert (text_view->buffer == NULL);
2923 gtk_text_view_destroy_layout (text_view);
2924 gtk_text_view_set_buffer (text_view, NULL);
2926 cancel_pending_scroll (text_view);
2928 if (text_view->tabs)
2929 pango_tab_array_free (text_view->tabs);
2931 if (text_view->hadjustment)
2932 g_object_unref (text_view->hadjustment);
2933 if (text_view->vadjustment)
2934 g_object_unref (text_view->vadjustment);
2936 text_window_free (text_view->text_window);
2938 if (text_view->left_window)
2939 text_window_free (text_view->left_window);
2941 if (text_view->top_window)
2942 text_window_free (text_view->top_window);
2944 if (text_view->right_window)
2945 text_window_free (text_view->right_window);
2947 if (text_view->bottom_window)
2948 text_window_free (text_view->bottom_window);
2950 g_object_unref (text_view->im_context);
2952 g_free (priv->im_module);
2954 G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize (object);
2958 gtk_text_view_set_property (GObject *object,
2960 const GValue *value,
2963 GtkTextView *text_view;
2964 GtkTextViewPrivate *priv;
2966 text_view = GTK_TEXT_VIEW (object);
2967 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2971 case PROP_PIXELS_ABOVE_LINES:
2972 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2975 case PROP_PIXELS_BELOW_LINES:
2976 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2979 case PROP_PIXELS_INSIDE_WRAP:
2980 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2984 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2987 case PROP_WRAP_MODE:
2988 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2991 case PROP_JUSTIFICATION:
2992 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2995 case PROP_LEFT_MARGIN:
2996 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2999 case PROP_RIGHT_MARGIN:
3000 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
3004 gtk_text_view_set_indent (text_view, g_value_get_int (value));
3008 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
3011 case PROP_CURSOR_VISIBLE:
3012 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
3015 case PROP_OVERWRITE:
3016 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
3020 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
3023 case PROP_ACCEPTS_TAB:
3024 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
3027 case PROP_IM_MODULE:
3028 g_free (priv->im_module);
3029 priv->im_module = g_value_dup_string (value);
3030 if (GTK_IS_IM_MULTICONTEXT (text_view->im_context))
3031 gtk_im_multicontext_set_context_id (GTK_IM_MULTICONTEXT (text_view->im_context), priv->im_module);
3035 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3041 gtk_text_view_get_property (GObject *object,
3046 GtkTextView *text_view;
3047 GtkTextViewPrivate *priv;
3049 text_view = GTK_TEXT_VIEW (object);
3050 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
3054 case PROP_PIXELS_ABOVE_LINES:
3055 g_value_set_int (value, text_view->pixels_above_lines);
3058 case PROP_PIXELS_BELOW_LINES:
3059 g_value_set_int (value, text_view->pixels_below_lines);
3062 case PROP_PIXELS_INSIDE_WRAP:
3063 g_value_set_int (value, text_view->pixels_inside_wrap);
3067 g_value_set_boolean (value, text_view->editable);
3070 case PROP_WRAP_MODE:
3071 g_value_set_enum (value, text_view->wrap_mode);
3074 case PROP_JUSTIFICATION:
3075 g_value_set_enum (value, text_view->justify);
3078 case PROP_LEFT_MARGIN:
3079 g_value_set_int (value, text_view->left_margin);
3082 case PROP_RIGHT_MARGIN:
3083 g_value_set_int (value, text_view->right_margin);
3087 g_value_set_int (value, text_view->indent);
3091 g_value_set_boxed (value, text_view->tabs);
3094 case PROP_CURSOR_VISIBLE:
3095 g_value_set_boolean (value, text_view->cursor_visible);
3099 g_value_set_object (value, get_buffer (text_view));
3102 case PROP_OVERWRITE:
3103 g_value_set_boolean (value, text_view->overwrite_mode);
3106 case PROP_ACCEPTS_TAB:
3107 g_value_set_boolean (value, text_view->accepts_tab);
3110 case PROP_IM_MODULE:
3111 g_value_set_string (value, priv->im_module);
3115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3121 gtk_text_view_size_request (GtkWidget *widget,
3122 GtkRequisition *requisition)
3124 GtkTextView *text_view;
3126 gint focus_edge_width;
3128 gboolean interior_focus;
3130 text_view = GTK_TEXT_VIEW (widget);
3132 gtk_widget_style_get (widget,
3133 "interior-focus", &interior_focus,
3134 "focus-line-width", &focus_width,
3138 focus_edge_width = 0;
3140 focus_edge_width = focus_width;
3142 if (text_view->layout)
3144 text_view->text_window->requisition.width = text_view->layout->width;
3145 text_view->text_window->requisition.height = text_view->layout->height;
3149 text_view->text_window->requisition.width = 0;
3150 text_view->text_window->requisition.height = 0;
3153 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
3154 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
3156 if (text_view->left_window)
3157 requisition->width += text_view->left_window->requisition.width;
3159 if (text_view->right_window)
3160 requisition->width += text_view->right_window->requisition.width;
3162 if (text_view->top_window)
3163 requisition->height += text_view->top_window->requisition.height;
3165 if (text_view->bottom_window)
3166 requisition->height += text_view->bottom_window->requisition.height;
3168 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
3169 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
3171 tmp_list = text_view->children;
3172 while (tmp_list != NULL)
3174 GtkTextViewChild *child = tmp_list->data;
3178 GtkRequisition child_req;
3179 GtkRequisition old_req;
3181 gtk_widget_get_child_requisition (child->widget, &old_req);
3183 gtk_widget_size_request (child->widget, &child_req);
3185 gtk_widget_get_child_requisition (child->widget, &child_req);
3187 /* Invalidate layout lines if required */
3188 if (text_view->layout &&
3189 (old_req.width != child_req.width ||
3190 old_req.height != child_req.height))
3191 gtk_text_child_anchor_queue_resize (child->anchor,
3196 GtkRequisition child_req;
3198 gtk_widget_size_request (child->widget, &child_req);
3201 tmp_list = g_slist_next (tmp_list);
3206 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
3207 GtkTextViewChild *vc,
3208 GtkAllocation *allocation)
3214 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3218 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
3221 buffer_y += vc->from_top_of_line;
3223 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
3224 allocation->y = buffer_y - text_view->yoffset;
3226 gtk_widget_get_child_requisition (vc->widget, &req);
3227 allocation->width = req.width;
3228 allocation->height = req.height;
3232 gtk_text_view_update_child_allocation (GtkTextView *text_view,
3233 GtkTextViewChild *vc)
3235 GtkAllocation allocation;
3237 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
3239 gtk_widget_size_allocate (vc->widget, &allocation);
3242 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
3244 vc->widget->allocation.x,
3245 vc->widget->allocation.y,
3246 text_view->yoffset);
3251 gtk_text_view_child_allocated (GtkTextLayout *layout,
3257 GtkTextViewChild *vc = NULL;
3258 GtkTextView *text_view = data;
3260 /* x,y is the position of the child from the top of the line, and
3261 * from the left of the buffer. We have to translate that into text
3262 * window coordinates, then size_allocate the child.
3265 vc = g_object_get_data (G_OBJECT (child),
3266 "gtk-text-view-child");
3268 g_assert (vc != NULL);
3270 DV (g_print ("child allocated at %d,%d\n", x, y));
3272 vc->from_left_of_buffer = x;
3273 vc->from_top_of_line = y;
3275 gtk_text_view_update_child_allocation (text_view, vc);
3279 gtk_text_view_allocate_children (GtkTextView *text_view)
3283 DV(g_print(G_STRLOC"\n"));
3285 tmp_list = text_view->children;
3286 while (tmp_list != NULL)
3288 GtkTextViewChild *child = tmp_list->data;
3290 g_assert (child != NULL);
3294 /* We need to force-validate the regions containing
3297 GtkTextIter child_loc;
3298 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3302 /* Since anchored children are only ever allocated from
3303 * gtk_text_layout_get_line_display() we have to make sure
3304 * that the display line caching in the layout doesn't
3305 * get in the way. Invalidating the layout around the anchor
3308 if (GTK_WIDGET_ALLOC_NEEDED (child->widget))
3310 GtkTextIter end = child_loc;
3311 gtk_text_iter_forward_char (&end);
3312 gtk_text_layout_invalidate (text_view->layout, &child_loc, &end);
3315 gtk_text_layout_validate_yrange (text_view->layout,
3321 GtkAllocation allocation;
3322 GtkRequisition child_req;
3324 allocation.x = child->x;
3325 allocation.y = child->y;
3327 gtk_widget_get_child_requisition (child->widget, &child_req);
3329 allocation.width = child_req.width;
3330 allocation.height = child_req.height;
3332 gtk_widget_size_allocate (child->widget, &allocation);
3335 tmp_list = g_slist_next (tmp_list);
3340 gtk_text_view_size_allocate (GtkWidget *widget,
3341 GtkAllocation *allocation)
3343 GtkTextView *text_view;
3344 GtkTextIter first_para;
3347 GdkRectangle text_rect;
3348 GdkRectangle left_rect;
3349 GdkRectangle right_rect;
3350 GdkRectangle top_rect;
3351 GdkRectangle bottom_rect;
3352 gint focus_edge_width;
3354 gboolean interior_focus;
3355 gboolean size_changed;
3357 text_view = GTK_TEXT_VIEW (widget);
3359 DV(g_print(G_STRLOC"\n"));
3362 widget->allocation.width != allocation->width ||
3363 widget->allocation.height != allocation->height;
3365 widget->allocation = *allocation;
3367 if (GTK_WIDGET_REALIZED (widget))
3369 gdk_window_move_resize (widget->window,
3370 allocation->x, allocation->y,
3371 allocation->width, allocation->height);
3374 /* distribute width/height among child windows. Ensure all
3375 * windows get at least a 1x1 allocation.
3378 gtk_widget_style_get (widget,
3379 "interior-focus", &interior_focus,
3380 "focus-line-width", &focus_width,
3384 focus_edge_width = 0;
3386 focus_edge_width = focus_width;
3388 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3390 if (text_view->left_window)
3391 left_rect.width = text_view->left_window->requisition.width;
3393 left_rect.width = 0;
3395 width -= left_rect.width;
3397 if (text_view->right_window)
3398 right_rect.width = text_view->right_window->requisition.width;
3400 right_rect.width = 0;
3402 width -= right_rect.width;
3404 text_rect.width = MAX (1, width);
3406 top_rect.width = text_rect.width;
3407 bottom_rect.width = text_rect.width;
3410 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3412 if (text_view->top_window)
3413 top_rect.height = text_view->top_window->requisition.height;
3415 top_rect.height = 0;
3417 height -= top_rect.height;
3419 if (text_view->bottom_window)
3420 bottom_rect.height = text_view->bottom_window->requisition.height;
3422 bottom_rect.height = 0;
3424 height -= bottom_rect.height;
3426 text_rect.height = MAX (1, height);
3428 left_rect.height = text_rect.height;
3429 right_rect.height = text_rect.height;
3432 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3433 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3435 text_rect.x = left_rect.x + left_rect.width;
3436 text_rect.y = top_rect.y + top_rect.height;
3438 left_rect.y = text_rect.y;
3439 right_rect.y = text_rect.y;
3441 top_rect.x = text_rect.x;
3442 bottom_rect.x = text_rect.x;
3444 right_rect.x = text_rect.x + text_rect.width;
3445 bottom_rect.y = text_rect.y + text_rect.height;
3447 text_window_size_allocate (text_view->text_window,
3450 if (text_view->left_window)
3451 text_window_size_allocate (text_view->left_window,
3454 if (text_view->right_window)
3455 text_window_size_allocate (text_view->right_window,
3458 if (text_view->top_window)
3459 text_window_size_allocate (text_view->top_window,
3462 if (text_view->bottom_window)
3463 text_window_size_allocate (text_view->bottom_window,
3466 gtk_text_view_update_layout_width (text_view);
3468 /* Note that this will do some layout validation */
3469 gtk_text_view_allocate_children (text_view);
3471 /* Ensure h/v adj exist */
3472 get_hadjustment (text_view);
3473 get_vadjustment (text_view);
3475 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
3476 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
3477 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
3478 text_view->hadjustment->lower = 0;
3479 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
3482 if (text_view->hadjustment->value > text_view->hadjustment->upper - text_view->hadjustment->page_size)
3483 gtk_adjustment_set_value (text_view->hadjustment, MAX (0, text_view->hadjustment->upper - text_view->hadjustment->page_size));
3485 gtk_adjustment_changed (text_view->hadjustment);
3487 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
3488 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
3489 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
3490 text_view->vadjustment->lower = 0;
3491 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
3494 /* Now adjust the value of the adjustment to keep the cursor at the
3495 * same place in the buffer
3497 gtk_text_view_get_first_para_iter (text_view, &first_para);
3498 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
3500 y += text_view->first_para_pixels;
3502 if (y > text_view->vadjustment->upper - text_view->vadjustment->page_size)
3503 y = MAX (0, text_view->vadjustment->upper - text_view->vadjustment->page_size);
3505 if (y != text_view->yoffset)
3506 gtk_adjustment_set_value (text_view->vadjustment, y);
3508 gtk_adjustment_changed (text_view->vadjustment);
3510 /* The GTK resize loop processes all the pending exposes right
3511 * after doing the resize stuff, so the idle sizer won't have a
3512 * chance to run. So we do the work here.
3514 gtk_text_view_flush_first_validate (text_view);
3516 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3519 if (size_changed && GTK_WIDGET_REALIZED (widget))
3520 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3524 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3527 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3528 text_view->first_para_mark);
3532 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3534 GtkWidget *widget = GTK_WIDGET (text_view);
3536 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3538 if (SCREEN_HEIGHT (widget) > 0)
3540 GtkTextIter first_para;
3542 /* Be sure we've validated the stuff onscreen; if we
3543 * scrolled, these calls won't have any effect, because
3544 * they were called in the recursive validate_onscreen
3546 gtk_text_view_get_first_para_iter (text_view, &first_para);
3548 gtk_text_layout_validate_yrange (text_view->layout,
3551 text_view->first_para_pixels +
3552 SCREEN_HEIGHT (widget));
3555 text_view->onscreen_validated = TRUE;
3557 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3559 /* This can have the odd side effect of triggering a scroll, which should
3560 * flip "onscreen_validated" back to FALSE, but should also get us
3561 * back into this function to turn it on again.
3563 gtk_text_view_update_adjustments (text_view);
3565 g_assert (text_view->onscreen_validated);
3569 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3571 if (text_view->first_validate_idle == 0)
3574 /* Do this first, which means that if an "invalidate"
3575 * occurs during any of this process, a new first_validate_callback
3576 * will be installed, and we'll start again.
3578 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3579 g_source_remove (text_view->first_validate_idle);
3580 text_view->first_validate_idle = 0;
3582 /* be sure we have up-to-date screen size set on the
3585 gtk_text_view_update_layout_width (text_view);
3587 /* Bail out if we invalidated stuff; scrolling right away will just
3588 * confuse the issue.
3590 if (text_view->first_validate_idle != 0)
3592 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3596 /* scroll to any marks, if that's pending. This can jump us to
3597 * the validation codepath used for scrolling onscreen, if so we
3598 * bail out. It won't jump if already in that codepath since
3599 * value_changed is not recursive, so also validate if
3602 if (!gtk_text_view_flush_scroll (text_view) ||
3603 !text_view->onscreen_validated)
3604 gtk_text_view_validate_onscreen (text_view);
3606 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3608 g_assert (text_view->onscreen_validated);
3613 first_validate_callback (gpointer data)
3615 GtkTextView *text_view = data;
3617 /* Note that some of this code is duplicated at the end of size_allocate,
3618 * keep in sync with that.
3621 DV(g_print(G_STRLOC"\n"));
3623 gtk_text_view_flush_first_validate (text_view);
3629 incremental_validate_callback (gpointer data)
3631 GtkTextView *text_view = data;
3632 gboolean result = TRUE;
3634 DV(g_print(G_STRLOC"\n"));
3636 gtk_text_layout_validate (text_view->layout, 2000);
3638 gtk_text_view_update_adjustments (text_view);
3640 if (gtk_text_layout_is_valid (text_view->layout))
3642 text_view->incremental_validate_idle = 0;
3650 gtk_text_view_invalidate (GtkTextView *text_view)
3652 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3653 text_view->onscreen_validated));
3655 text_view->onscreen_validated = FALSE;
3657 /* We'll invalidate when the layout is created */
3658 if (text_view->layout == NULL)
3661 if (!text_view->first_validate_idle)
3663 text_view->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3664 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3665 text_view->first_validate_idle));
3668 if (!text_view->incremental_validate_idle)
3670 text_view->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3671 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3672 text_view->incremental_validate_idle));
3677 invalidated_handler (GtkTextLayout *layout,
3680 GtkTextView *text_view;
3682 text_view = GTK_TEXT_VIEW (data);
3684 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3685 gtk_text_view_invalidate (text_view);
3689 changed_handler (GtkTextLayout *layout,
3695 GtkTextView *text_view;
3697 GdkRectangle visible_rect;
3698 GdkRectangle redraw_rect;
3700 text_view = GTK_TEXT_VIEW (data);
3701 widget = GTK_WIDGET (data);
3703 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3705 if (GTK_WIDGET_REALIZED (text_view))
3707 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3709 redraw_rect.x = visible_rect.x;
3710 redraw_rect.width = visible_rect.width;
3711 redraw_rect.y = start_y;
3713 if (old_height == new_height)
3714 redraw_rect.height = old_height;
3715 else if (start_y + old_height > visible_rect.y)
3716 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3718 redraw_rect.height = 0;
3720 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3722 /* text_window_invalidate_rect() takes buffer coordinates */
3723 text_window_invalidate_rect (text_view->text_window,
3726 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3730 redraw_rect.height));
3732 if (text_view->left_window)
3733 text_window_invalidate_rect (text_view->left_window,
3735 if (text_view->right_window)
3736 text_window_invalidate_rect (text_view->right_window,
3738 if (text_view->top_window)
3739 text_window_invalidate_rect (text_view->top_window,
3741 if (text_view->bottom_window)
3742 text_window_invalidate_rect (text_view->bottom_window,
3745 queue_update_im_spot_location (text_view);
3749 if (old_height != new_height)
3751 gboolean yoffset_changed = FALSE;
3753 int new_first_para_top;
3754 int old_first_para_top;
3757 /* If the bottom of the old area was above the top of the
3758 * screen, we need to scroll to keep the current top of the
3759 * screen in place. Remember that first_para_pixels is the
3760 * position of the top of the screen in coordinates relative to
3761 * the first paragraph onscreen.
3763 * In short we are adding the height delta of the portion of the
3764 * changed region above first_para_mark to text_view->yoffset.
3766 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3767 text_view->first_para_mark);
3769 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3771 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3773 if (new_first_para_top != old_first_para_top)
3775 text_view->yoffset += new_first_para_top - old_first_para_top;
3777 get_vadjustment (text_view)->value = text_view->yoffset;
3778 yoffset_changed = TRUE;
3781 if (yoffset_changed)
3783 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3784 gtk_adjustment_value_changed (get_vadjustment (text_view));
3787 /* FIXME be smarter about which anchored widgets we update */
3789 tmp_list = text_view->children;
3790 while (tmp_list != NULL)
3792 GtkTextViewChild *child = tmp_list->data;
3795 gtk_text_view_update_child_allocation (text_view, child);
3797 tmp_list = g_slist_next (tmp_list);
3802 GtkRequisition old_req;
3803 GtkRequisition new_req;
3805 old_req = widget->requisition;
3807 /* Use this instead of gtk_widget_size_request wrapper
3808 * to avoid the optimization which just returns widget->requisition
3809 * if a resize hasn't been queued.
3811 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3813 if (old_req.width != new_req.width ||
3814 old_req.height != new_req.height)
3816 gtk_widget_queue_resize_no_redraw (widget);
3822 gtk_text_view_realize (GtkWidget *widget)
3824 GtkTextView *text_view;
3825 GdkWindowAttr attributes;
3826 gint attributes_mask;
3829 text_view = GTK_TEXT_VIEW (widget);
3830 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3832 attributes.window_type = GDK_WINDOW_CHILD;
3833 attributes.x = widget->allocation.x;
3834 attributes.y = widget->allocation.y;
3835 attributes.width = widget->allocation.width;
3836 attributes.height = widget->allocation.height;
3837 attributes.wclass = GDK_INPUT_OUTPUT;
3838 attributes.visual = gtk_widget_get_visual (widget);
3839 attributes.colormap = gtk_widget_get_colormap (widget);
3840 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3842 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3844 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3845 &attributes, attributes_mask);
3846 gdk_window_set_user_data (widget->window, widget);
3848 /* must come before text_window_realize calls */
3849 widget->style = gtk_style_attach (widget->style, widget->window);
3851 gdk_window_set_background (widget->window,
3852 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3854 text_window_realize (text_view->text_window, widget);
3856 if (text_view->left_window)
3857 text_window_realize (text_view->left_window, widget);
3859 if (text_view->top_window)
3860 text_window_realize (text_view->top_window, widget);
3862 if (text_view->right_window)
3863 text_window_realize (text_view->right_window, widget);
3865 if (text_view->bottom_window)
3866 text_window_realize (text_view->bottom_window, widget);
3868 gtk_text_view_ensure_layout (text_view);
3870 if (text_view->buffer)
3872 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3873 GDK_SELECTION_PRIMARY);
3874 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3877 tmp_list = text_view->children;
3878 while (tmp_list != NULL)
3880 GtkTextViewChild *vc = tmp_list->data;
3882 text_view_child_set_parent_window (text_view, vc);
3884 tmp_list = tmp_list->next;
3887 /* Ensure updating the spot location. */
3888 gtk_text_view_update_im_spot_location (text_view);
3892 gtk_text_view_unrealize (GtkWidget *widget)
3894 GtkTextView *text_view;
3896 text_view = GTK_TEXT_VIEW (widget);
3898 if (text_view->buffer)
3900 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3901 GDK_SELECTION_PRIMARY);
3902 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3905 gtk_text_view_remove_validate_idles (text_view);
3907 if (text_view->popup_menu)
3909 gtk_widget_destroy (text_view->popup_menu);
3910 text_view->popup_menu = NULL;
3913 text_window_unrealize (text_view->text_window);
3915 if (text_view->left_window)
3916 text_window_unrealize (text_view->left_window);
3918 if (text_view->top_window)
3919 text_window_unrealize (text_view->top_window);
3921 if (text_view->right_window)
3922 text_window_unrealize (text_view->right_window);
3924 if (text_view->bottom_window)
3925 text_window_unrealize (text_view->bottom_window);
3927 gtk_text_view_destroy_layout (text_view);
3929 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize (widget);
3933 gtk_text_view_set_background (GtkTextView *text_view)
3935 GtkWidget *widget = GTK_WIDGET (text_view);
3937 gdk_window_set_background (widget->window,
3938 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3940 gdk_window_set_background (text_view->text_window->bin_window,
3941 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3943 if (text_view->left_window)
3944 gdk_window_set_background (text_view->left_window->bin_window,
3945 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3946 if (text_view->right_window)
3947 gdk_window_set_background (text_view->right_window->bin_window,
3948 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3950 if (text_view->top_window)
3951 gdk_window_set_background (text_view->top_window->bin_window,
3952 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3954 if (text_view->bottom_window)
3955 gdk_window_set_background (text_view->bottom_window->bin_window,
3956 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3960 gtk_text_view_style_set (GtkWidget *widget,
3961 GtkStyle *previous_style)
3963 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3964 PangoContext *ltr_context, *rtl_context;
3966 if (GTK_WIDGET_REALIZED (widget))
3968 gtk_text_view_set_background (text_view);
3971 if (text_view->layout && previous_style)
3973 gtk_text_view_set_attributes_from_style (text_view,
3974 text_view->layout->default_style,
3978 ltr_context = gtk_widget_create_pango_context (widget);
3979 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
3980 rtl_context = gtk_widget_create_pango_context (widget);
3981 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
3983 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
3985 g_object_unref (ltr_context);
3986 g_object_unref (rtl_context);
3991 gtk_text_view_direction_changed (GtkWidget *widget,
3992 GtkTextDirection previous_direction)
3994 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3996 if (text_view->layout)
3998 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
4000 gtk_text_layout_default_style_changed (text_view->layout);
4005 gtk_text_view_state_changed (GtkWidget *widget,
4006 GtkStateType previous_state)
4008 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4011 if (GTK_WIDGET_REALIZED (widget))
4013 gtk_text_view_set_background (text_view);
4015 if (gtk_widget_is_sensitive (widget))
4016 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
4020 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4023 gdk_cursor_unref (cursor);
4025 text_view->mouse_cursor_obscured = FALSE;
4028 if (!gtk_widget_is_sensitive (widget))
4030 /* Clear any selection */
4031 gtk_text_view_unselect (text_view);
4034 gtk_widget_queue_draw (widget);
4038 set_invisible_cursor (GdkWindow *window)
4040 GdkDisplay *display;
4043 display = gdk_drawable_get_display (window);
4044 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
4046 gdk_window_set_cursor (window, cursor);
4048 gdk_cursor_unref (cursor);
4052 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
4054 if (text_view->mouse_cursor_obscured)
4057 set_invisible_cursor (text_view->text_window->bin_window);
4059 text_view->mouse_cursor_obscured = TRUE;
4063 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
4065 if (text_view->mouse_cursor_obscured)
4069 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
4071 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4072 gdk_cursor_unref (cursor);
4073 text_view->mouse_cursor_obscured = FALSE;
4078 gtk_text_view_grab_notify (GtkWidget *widget,
4079 gboolean was_grabbed)
4083 gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
4084 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
4094 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
4097 switch (event->type)
4099 case GDK_MOTION_NOTIFY:
4100 *x = event->motion.x;
4101 *y = event->motion.y;
4105 case GDK_BUTTON_PRESS:
4106 case GDK_2BUTTON_PRESS:
4107 case GDK_3BUTTON_PRESS:
4108 case GDK_BUTTON_RELEASE:
4109 *x = event->button.x;
4110 *y = event->button.y;
4115 case GDK_KEY_RELEASE:
4116 case GDK_ENTER_NOTIFY:
4117 case GDK_LEAVE_NOTIFY:
4118 case GDK_PROPERTY_NOTIFY:
4119 case GDK_SELECTION_CLEAR:
4120 case GDK_SELECTION_REQUEST:
4121 case GDK_SELECTION_NOTIFY:
4122 case GDK_PROXIMITY_IN:
4123 case GDK_PROXIMITY_OUT:
4124 case GDK_DRAG_ENTER:
4125 case GDK_DRAG_LEAVE:
4126 case GDK_DRAG_MOTION:
4127 case GDK_DRAG_STATUS:
4128 case GDK_DROP_START:
4129 case GDK_DROP_FINISHED:
4139 emit_event_on_tags (GtkWidget *widget,
4145 gboolean retval = FALSE;
4147 tags = gtk_text_iter_get_tags (iter);
4152 GtkTextTag *tag = tmp->data;
4154 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
4160 tmp = g_slist_next (tmp);
4163 g_slist_free (tags);
4169 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
4171 GtkTextView *text_view;
4174 text_view = GTK_TEXT_VIEW (widget);
4176 if (text_view->layout == NULL ||
4177 get_buffer (text_view) == NULL)
4180 if (event->any.window != text_view->text_window->bin_window)
4183 if (get_event_coordinates (event, &x, &y))
4187 x += text_view->xoffset;
4188 y += text_view->yoffset;
4190 /* FIXME this is slow and we do it twice per event.
4191 * My favorite solution is to have GtkTextLayout cache
4192 * the last couple lookups.
4194 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4198 return emit_event_on_tags (widget, event, &iter);
4200 else if (event->type == GDK_KEY_PRESS ||
4201 event->type == GDK_KEY_RELEASE)
4205 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
4206 gtk_text_buffer_get_insert (get_buffer (text_view)));
4208 return emit_event_on_tags (widget, event, &iter);
4215 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
4217 gboolean retval = FALSE;
4218 gboolean obscure = FALSE;
4219 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4220 GtkTextMark *insert;
4222 gboolean can_insert;
4224 if (text_view->layout == NULL ||
4225 get_buffer (text_view) == NULL)
4228 /* Make sure input method knows where it is */
4229 flush_update_im_spot_location (text_view);
4231 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4232 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4233 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
4234 if (gtk_im_context_filter_keypress (text_view->im_context, event))
4236 text_view->need_im_reset = TRUE;
4238 gtk_text_view_reset_im_context (text_view);
4239 obscure = can_insert;
4243 else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4247 /* use overall editability not can_insert, more predictable for users */
4248 else if (text_view->editable &&
4249 (event->keyval == GDK_Return ||
4250 event->keyval == GDK_ISO_Enter ||
4251 event->keyval == GDK_KP_Enter))
4253 /* this won't actually insert the newline if the cursor isn't
4256 gtk_text_view_reset_im_context (text_view);
4257 gtk_text_view_commit_text (text_view, "\n");
4262 /* Pass through Tab as literal tab, unless Control is held down */
4263 else if ((event->keyval == GDK_Tab ||
4264 event->keyval == GDK_KP_Tab ||
4265 event->keyval == GDK_ISO_Left_Tab) &&
4266 !(event->state & GDK_CONTROL_MASK))
4268 /* If the text widget isn't editable overall, or if the application
4269 * has turned off "accepts_tab", move the focus instead
4271 if (text_view->accepts_tab && text_view->editable)
4273 gtk_text_view_reset_im_context (text_view);
4274 gtk_text_view_commit_text (text_view, "\t");
4278 g_signal_emit_by_name (text_view, "move-focus",
4279 (event->state & GDK_SHIFT_MASK) ?
4280 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4288 gtk_text_view_obscure_mouse_cursor (text_view);
4290 gtk_text_view_reset_blink_time (text_view);
4291 gtk_text_view_pend_cursor_blink (text_view);
4297 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4299 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4300 GtkTextMark *insert;
4303 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
4306 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4307 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4308 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
4309 gtk_im_context_filter_keypress (text_view->im_context, event))
4311 text_view->need_im_reset = TRUE;
4315 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4319 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4321 GtkTextView *text_view;
4323 text_view = GTK_TEXT_VIEW (widget);
4325 gtk_widget_grab_focus (widget);
4327 if (event->window != text_view->text_window->bin_window)
4329 /* Remove selection if any. */
4330 gtk_text_view_unselect (text_view);
4334 gtk_text_view_reset_blink_time (text_view);
4338 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4339 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4340 else if (event->button == 3)
4341 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4344 if (event->type == GDK_BUTTON_PRESS)
4346 gtk_text_view_reset_im_context (text_view);
4348 if (event->button == 1)
4350 /* If we're in the selection, start a drag copy/move of the
4351 * selection; otherwise, start creating a new selection.
4354 GtkTextIter start, end;
4356 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4358 event->x + text_view->xoffset,
4359 event->y + text_view->yoffset);
4361 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4363 gtk_text_iter_in_range (&iter, &start, &end) &&
4364 !(event->state & GDK_SHIFT_MASK))
4366 text_view->drag_start_x = event->x;
4367 text_view->drag_start_y = event->y;
4368 text_view->pending_place_cursor_button = event->button;
4372 gtk_text_view_start_selection_drag (text_view, &iter, event);
4377 else if (event->button == 2)
4381 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4383 event->x + text_view->xoffset,
4384 event->y + text_view->yoffset);
4386 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4387 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4389 text_view->editable);
4392 else if (event->button == 3)
4394 gtk_text_view_do_popup (text_view, event);
4398 else if ((event->type == GDK_2BUTTON_PRESS ||
4399 event->type == GDK_3BUTTON_PRESS) &&
4404 gtk_text_view_end_selection_drag (text_view);
4406 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4408 event->x + text_view->xoffset,
4409 event->y + text_view->yoffset);
4411 gtk_text_view_start_selection_drag (text_view, &iter, event);
4419 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4421 GtkTextView *text_view;
4423 text_view = GTK_TEXT_VIEW (widget);
4425 if (event->window != text_view->text_window->bin_window)
4428 if (event->button == 1)
4430 if (text_view->drag_start_x >= 0)
4432 text_view->drag_start_x = -1;
4433 text_view->drag_start_y = -1;
4436 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4438 else if (text_view->pending_place_cursor_button == event->button)
4442 /* Unselect everything; we clicked inside selection, but
4443 * didn't move by the drag threshold, so just clear selection
4446 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4448 event->x + text_view->xoffset,
4449 event->y + text_view->yoffset);
4451 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4452 gtk_text_view_check_cursor_blink (text_view);
4454 text_view->pending_place_cursor_button = 0;
4464 keymap_direction_changed (GdkKeymap *keymap,
4465 GtkTextView *text_view)
4467 gtk_text_view_check_keymap_direction (text_view);
4471 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4473 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4475 gtk_widget_queue_draw (widget);
4477 DV(g_print (G_STRLOC": focus_in_event\n"));
4479 gtk_text_view_reset_blink_time (text_view);
4481 if (text_view->cursor_visible && text_view->layout)
4483 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4484 gtk_text_view_check_cursor_blink (text_view);
4487 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4488 "direction-changed",
4489 G_CALLBACK (keymap_direction_changed), text_view);
4490 gtk_text_view_check_keymap_direction (text_view);
4492 if (text_view->editable)
4494 text_view->need_im_reset = TRUE;
4495 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4502 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4504 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4506 gtk_text_view_end_selection_drag (text_view);
4508 gtk_widget_queue_draw (widget);
4510 DV(g_print (G_STRLOC": focus_out_event\n"));
4512 if (text_view->cursor_visible && text_view->layout)
4514 gtk_text_view_check_cursor_blink (text_view);
4515 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4518 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4519 keymap_direction_changed,
4522 if (text_view->editable)
4524 text_view->need_im_reset = TRUE;
4525 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4532 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4534 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4536 gtk_text_view_unobscure_mouse_cursor (text_view);
4538 if (event->window == text_view->text_window->bin_window &&
4539 text_view->drag_start_x >= 0)
4544 gdk_event_request_motions (event);
4546 if (gtk_drag_check_threshold (widget,
4547 text_view->drag_start_x,
4548 text_view->drag_start_y,
4552 gint buffer_x, buffer_y;
4554 gtk_text_view_window_to_buffer_coords (text_view,
4555 GTK_TEXT_WINDOW_TEXT,
4556 text_view->drag_start_x,
4557 text_view->drag_start_y,
4561 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4563 buffer_x, buffer_y);
4565 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4574 gtk_text_view_paint (GtkWidget *widget,
4576 GdkEventExpose *event)
4578 GtkTextView *text_view;
4579 GList *child_exposes;
4582 text_view = GTK_TEXT_VIEW (widget);
4584 g_return_if_fail (text_view->layout != NULL);
4585 g_return_if_fail (text_view->xoffset >= 0);
4586 g_return_if_fail (text_view->yoffset >= 0);
4588 while (text_view->first_validate_idle != 0)
4590 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4591 text_view->first_validate_idle));
4592 gtk_text_view_flush_first_validate (text_view);
4595 if (!text_view->onscreen_validated)
4597 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.");
4598 g_assert_not_reached ();
4602 printf ("painting %d,%d %d x %d\n",
4604 area->width, area->height);
4607 child_exposes = NULL;
4608 gtk_text_layout_draw (text_view->layout,
4610 text_view->text_window->bin_window,
4615 area->width, area->height,
4618 tmp_list = child_exposes;
4619 while (tmp_list != NULL)
4621 GtkWidget *child = tmp_list->data;
4623 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4627 g_object_unref (child);
4629 tmp_list = tmp_list->next;
4632 g_list_free (child_exposes);
4636 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4640 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4641 GTK_TEXT_WINDOW_TEXT))
4643 DV(g_print (">Exposed ("G_STRLOC")\n"));
4644 gtk_text_view_paint (widget, &event->area, event);
4647 if (event->window == widget->window)
4648 gtk_text_view_draw_focus (widget);
4650 /* Propagate exposes to all unanchored children.
4651 * Anchored children are handled in gtk_text_view_paint().
4653 tmp_list = GTK_TEXT_VIEW (widget)->children;
4654 while (tmp_list != NULL)
4656 GtkTextViewChild *vc = tmp_list->data;
4658 /* propagate_expose checks that event->window matches
4662 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4666 tmp_list = tmp_list->next;
4673 gtk_text_view_draw_focus (GtkWidget *widget)
4675 gboolean interior_focus;
4677 /* We clear the focus if we are in interior focus mode. */
4678 gtk_widget_style_get (widget,
4679 "interior-focus", &interior_focus,
4682 if (gtk_widget_is_drawable (widget))
4684 if (gtk_widget_has_focus (widget) && !interior_focus)
4686 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4687 NULL, widget, "textview",
4689 widget->allocation.width,
4690 widget->allocation.height);
4694 gdk_window_clear (widget->window);
4700 gtk_text_view_focus (GtkWidget *widget,
4701 GtkDirectionType direction)
4703 GtkContainer *container;
4706 container = GTK_CONTAINER (widget);
4708 if (!gtk_widget_is_focus (widget) &&
4709 container->focus_child == NULL)
4711 gtk_widget_grab_focus (widget);
4717 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4718 * children to get the focus
4720 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4721 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4722 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4729 gtk_text_view_move_focus (GtkWidget *widget,
4730 GtkDirectionType direction_type)
4732 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4734 if (GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus)
4735 GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus (text_view,
4744 gtk_text_view_add (GtkContainer *container,
4747 /* This is pretty random. */
4748 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4750 GTK_TEXT_WINDOW_WIDGET,
4755 gtk_text_view_remove (GtkContainer *container,
4759 GtkTextView *text_view;
4760 GtkTextViewChild *vc;
4762 text_view = GTK_TEXT_VIEW (container);
4765 iter = text_view->children;
4767 while (iter != NULL)
4771 if (vc->widget == child)
4774 iter = g_slist_next (iter);
4777 g_assert (iter != NULL); /* be sure we had the child in the list */
4779 text_view->children = g_slist_remove (text_view->children, vc);
4781 gtk_widget_unparent (vc->widget);
4783 text_view_child_free (vc);
4787 gtk_text_view_forall (GtkContainer *container,
4788 gboolean include_internals,
4789 GtkCallback callback,
4790 gpointer callback_data)
4793 GtkTextView *text_view;
4796 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4797 g_return_if_fail (callback != NULL);
4799 text_view = GTK_TEXT_VIEW (container);
4801 copy = g_slist_copy (text_view->children);
4804 while (iter != NULL)
4806 GtkTextViewChild *vc = iter->data;
4808 (* callback) (vc->widget, callback_data);
4810 iter = g_slist_next (iter);
4813 g_slist_free (copy);
4816 #define CURSOR_ON_MULTIPLIER 2
4817 #define CURSOR_OFF_MULTIPLIER 1
4818 #define CURSOR_PEND_MULTIPLIER 3
4819 #define CURSOR_DIVIDER 3
4822 cursor_blinks (GtkTextView *text_view)
4824 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4827 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4830 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4833 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4838 if (text_view->editable)
4840 GtkTextMark *insert;
4843 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4844 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4846 if (gtk_text_iter_editable (&iter, text_view->editable))
4854 get_cursor_time (GtkTextView *text_view)
4856 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4859 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
4865 get_cursor_blink_timeout (GtkTextView *text_view)
4867 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4870 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
4881 blink_cb (gpointer data)
4883 GtkTextView *text_view;
4884 GtkTextViewPrivate *priv;
4888 text_view = GTK_TEXT_VIEW (data);
4889 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4891 if (!gtk_widget_has_focus (GTK_WIDGET (text_view)))
4893 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
4894 "connect a handler to this signal, it must return\n"
4895 "FALSE so the text view gets the event as well");
4897 gtk_text_view_check_cursor_blink (text_view);
4902 g_assert (text_view->layout);
4903 g_assert (text_view->cursor_visible);
4905 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4907 blink_timeout = get_cursor_blink_timeout (text_view);
4908 if (priv->blink_time > 1000 * blink_timeout &&
4909 blink_timeout < G_MAXINT/1000)
4911 /* we've blinked enough without the user doing anything, stop blinking */
4913 text_view->blink_timeout = 0;
4916 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4921 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
4924 priv->blink_time += get_cursor_time (text_view);
4927 /* Block changed_handler while changing the layout's cursor visibility
4928 * because it would expose the whole paragraph. Instead, we expose
4929 * the cursor's area(s) manually below.
4931 g_signal_handlers_block_by_func (text_view->layout,
4934 gtk_text_layout_set_cursor_visible (text_view->layout, !visible);
4935 g_signal_handlers_unblock_by_func (text_view->layout,
4939 text_window_invalidate_cursors (text_view->text_window);
4941 /* Remove ourselves */
4947 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4949 if (text_view->blink_timeout)
4951 g_source_remove (text_view->blink_timeout);
4952 text_view->blink_timeout = 0;
4957 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4959 if (text_view->layout != NULL &&
4960 text_view->cursor_visible &&
4961 gtk_widget_has_focus (GTK_WIDGET (text_view)))
4963 if (cursor_blinks (text_view))
4965 if (text_view->blink_timeout == 0)
4967 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4969 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4976 gtk_text_view_stop_cursor_blink (text_view);
4977 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4982 gtk_text_view_stop_cursor_blink (text_view);
4983 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4988 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
4990 if (text_view->layout != NULL &&
4991 text_view->cursor_visible &&
4992 gtk_widget_has_focus (GTK_WIDGET (text_view)) &&
4993 cursor_blinks (text_view))
4995 gtk_text_view_stop_cursor_blink (text_view);
4996 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4998 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
5005 gtk_text_view_reset_blink_time (GtkTextView *text_view)
5007 GtkTextViewPrivate *priv;
5009 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
5011 priv->blink_time = 0;
5016 * Key binding handlers
5020 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
5021 GtkTextIter *newplace,
5024 gboolean ret = TRUE;
5028 ret = gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
5034 ret = gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
5042 move_cursor (GtkTextView *text_view,
5043 const GtkTextIter *new_location,
5044 gboolean extend_selection)
5046 if (extend_selection)
5047 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
5051 gtk_text_buffer_place_cursor (get_buffer (text_view),
5053 gtk_text_view_check_cursor_blink (text_view);
5057 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
5058 GtkMovementStep step,
5060 gboolean extend_selection)
5063 GtkTextIter newplace;
5064 gboolean cancel_selection = FALSE;
5065 gint cursor_x_pos = 0;
5066 GtkDirectionType leave_direction = -1;
5068 if (!text_view->cursor_visible)
5070 GtkScrollStep scroll_step;
5074 case GTK_MOVEMENT_VISUAL_POSITIONS:
5075 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
5077 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5078 case GTK_MOVEMENT_WORDS:
5079 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
5081 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5082 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
5084 case GTK_MOVEMENT_DISPLAY_LINES:
5085 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
5087 case GTK_MOVEMENT_PARAGRAPHS:
5088 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5089 scroll_step = GTK_SCROLL_STEPS;
5091 case GTK_MOVEMENT_PAGES:
5092 scroll_step = GTK_SCROLL_PAGES;
5094 case GTK_MOVEMENT_HORIZONTAL_PAGES:
5095 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
5097 case GTK_MOVEMENT_BUFFER_ENDS:
5098 scroll_step = GTK_SCROLL_ENDS;
5101 scroll_step = GTK_SCROLL_PAGES;
5105 if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
5107 if (leave_direction != -1 &&
5108 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5111 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5118 gtk_text_view_reset_im_context (text_view);
5120 if (step == GTK_MOVEMENT_PAGES)
5122 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
5123 gtk_widget_error_bell (GTK_WIDGET (text_view));
5125 gtk_text_view_check_cursor_blink (text_view);
5126 gtk_text_view_pend_cursor_blink (text_view);
5129 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
5131 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
5132 gtk_widget_error_bell (GTK_WIDGET (text_view));
5134 gtk_text_view_check_cursor_blink (text_view);
5135 gtk_text_view_pend_cursor_blink (text_view);
5139 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5140 gtk_text_buffer_get_insert (get_buffer (text_view)));
5142 if (! extend_selection)
5144 GtkTextIter sel_bound;
5146 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &sel_bound,
5147 gtk_text_buffer_get_selection_bound (get_buffer (text_view)));
5149 /* if we move forward, assume the cursor is at the end of the selection;
5150 * if we move backward, assume the cursor is at the start
5153 gtk_text_iter_order (&sel_bound, &insert);
5155 gtk_text_iter_order (&insert, &sel_bound);
5157 /* if we actually have a selection, just move *to* the beginning/end
5158 * of the selection and not *from* there on LOGICAL_POSITIONS
5159 * and VISUAL_POSITIONS movement
5161 if (! gtk_text_iter_equal (&sel_bound, &insert))
5162 cancel_selection = TRUE;
5167 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5168 gtk_text_view_get_virtual_cursor_pos (text_view, &insert, &cursor_x_pos, NULL);
5172 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5173 if (! cancel_selection)
5174 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5177 case GTK_MOVEMENT_VISUAL_POSITIONS:
5178 if (! cancel_selection)
5179 gtk_text_layout_move_iter_visually (text_view->layout,
5183 case GTK_MOVEMENT_WORDS:
5185 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5188 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5189 gtk_text_iter_forward_to_line_end (&newplace);
5193 case GTK_MOVEMENT_DISPLAY_LINES:
5196 leave_direction = GTK_DIR_UP;
5198 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5199 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5201 gtk_text_iter_set_line_offset (&newplace, 0);
5205 leave_direction = GTK_DIR_DOWN;
5207 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5208 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5210 gtk_text_iter_forward_to_line_end (&newplace);
5214 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5216 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5217 else if (count < -1)
5218 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5221 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
5224 case GTK_MOVEMENT_PARAGRAPHS:
5227 if (!gtk_text_iter_ends_line (&newplace))
5229 gtk_text_iter_forward_to_line_end (&newplace);
5232 gtk_text_iter_forward_visible_lines (&newplace, count);
5233 gtk_text_iter_forward_to_line_end (&newplace);
5237 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5238 gtk_text_iter_set_line_offset (&newplace, 0);
5239 gtk_text_iter_forward_visible_lines (&newplace, count);
5240 gtk_text_iter_set_line_offset (&newplace, 0);
5244 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5247 if (!gtk_text_iter_ends_line (&newplace))
5248 gtk_text_iter_forward_to_line_end (&newplace);
5252 gtk_text_iter_set_line_offset (&newplace, 0);
5256 case GTK_MOVEMENT_BUFFER_ENDS:
5258 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5260 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5267 /* call move_cursor() even if the cursor hasn't moved, since it
5268 cancels the selection
5270 move_cursor (text_view, &newplace, extend_selection);
5272 if (!gtk_text_iter_equal (&insert, &newplace))
5274 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5275 gtk_text_view_scroll_mark_onscreen (text_view,
5276 gtk_text_buffer_get_insert (get_buffer (text_view)));
5278 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5279 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5281 else if (leave_direction != -1)
5283 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5286 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5289 else if (! cancel_selection)
5291 gtk_widget_error_bell (GTK_WIDGET (text_view));
5294 gtk_text_view_check_cursor_blink (text_view);
5295 gtk_text_view_pend_cursor_blink (text_view);
5299 gtk_text_view_move_cursor (GtkTextView *text_view,
5300 GtkMovementStep step,
5302 gboolean extend_selection)
5304 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5308 gtk_text_view_page_horizontally (GtkTextView *text_view,
5310 gboolean extend_selection)
5312 gtk_text_view_move_cursor_internal (text_view, GTK_MOVEMENT_HORIZONTAL_PAGES,
5313 count, extend_selection);
5318 gtk_text_view_move_viewport (GtkTextView *text_view,
5322 GtkAdjustment *adjustment;
5327 case GTK_SCROLL_STEPS:
5328 case GTK_SCROLL_PAGES:
5329 case GTK_SCROLL_ENDS:
5330 adjustment = get_vadjustment (text_view);
5332 case GTK_SCROLL_HORIZONTAL_STEPS:
5333 case GTK_SCROLL_HORIZONTAL_PAGES:
5334 case GTK_SCROLL_HORIZONTAL_ENDS:
5335 adjustment = get_hadjustment (text_view);
5338 adjustment = get_vadjustment (text_view);
5344 case GTK_SCROLL_STEPS:
5345 case GTK_SCROLL_HORIZONTAL_STEPS:
5346 increment = adjustment->step_increment;
5348 case GTK_SCROLL_PAGES:
5349 case GTK_SCROLL_HORIZONTAL_PAGES:
5350 increment = adjustment->page_increment;
5352 case GTK_SCROLL_ENDS:
5353 case GTK_SCROLL_HORIZONTAL_ENDS:
5354 increment = adjustment->upper - adjustment->lower;
5361 return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
5365 gtk_text_view_set_anchor (GtkTextView *text_view)
5369 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5370 gtk_text_buffer_get_insert (get_buffer (text_view)));
5372 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5376 gtk_text_view_scroll_pages (GtkTextView *text_view,
5378 gboolean extend_selection)
5383 gint cursor_x_pos, cursor_y_pos;
5384 GtkTextMark *insert_mark;
5385 GtkTextIter old_insert;
5386 GtkTextIter new_insert;
5390 g_return_val_if_fail (text_view->vadjustment != NULL, FALSE);
5392 adj = text_view->vadjustment;
5394 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5396 /* Make sure we start from the current cursor position, even
5397 * if it was offscreen, but don't queue more scrolls if we're
5400 if (text_view->pending_scroll)
5401 cancel_pending_scroll (text_view);
5403 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5405 /* Validate the region that will be brought into view by the cursor motion
5407 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5408 &old_insert, insert_mark);
5412 gtk_text_view_get_first_para_iter (text_view, &anchor);
5413 y0 = adj->page_size;
5414 y1 = adj->page_size + count * adj->page_increment;
5418 gtk_text_view_get_first_para_iter (text_view, &anchor);
5419 y0 = count * adj->page_increment + adj->page_size;
5423 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
5424 /* FIXME do we need to update the adjustment ranges here? */
5426 new_insert = old_insert;
5428 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5430 /* already at top, just be sure we are at offset 0 */
5431 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5432 move_cursor (text_view, &new_insert, extend_selection);
5434 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5436 /* already at bottom, just be sure we are at the end */
5437 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5438 move_cursor (text_view, &new_insert, extend_selection);
5442 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5444 oldval = adj->value;
5445 newval = adj->value;
5447 newval += count * adj->page_increment;
5449 set_adjustment_clamped (adj, newval);
5450 cursor_y_pos += adj->value - oldval;
5452 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5453 clamp_iter_onscreen (text_view, &new_insert);
5454 move_cursor (text_view, &new_insert, extend_selection);
5456 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5459 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5460 * only guarantees 1 pixel onscreen.
5462 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5463 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5465 return !gtk_text_iter_equal (&old_insert, &new_insert);
5469 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5471 gboolean extend_selection)
5476 gint cursor_x_pos, cursor_y_pos;
5477 GtkTextMark *insert_mark;
5478 GtkTextIter old_insert;
5479 GtkTextIter new_insert;
5482 g_return_val_if_fail (text_view->hadjustment != NULL, FALSE);
5484 adj = text_view->hadjustment;
5486 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5488 /* Make sure we start from the current cursor position, even
5489 * if it was offscreen, but don't queue more scrolls if we're
5492 if (text_view->pending_scroll)
5493 cancel_pending_scroll (text_view);
5495 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5497 /* Validate the line that we're moving within.
5499 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5500 &old_insert, insert_mark);
5502 gtk_text_layout_get_line_yrange (text_view->layout, &old_insert, &y, &height);
5503 gtk_text_layout_validate_yrange (text_view->layout, &old_insert, y, y + height);
5504 /* FIXME do we need to update the adjustment ranges here? */
5506 new_insert = old_insert;
5508 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5510 /* already at far left, just be sure we are at offset 0 */
5511 gtk_text_iter_set_line_offset (&new_insert, 0);
5512 move_cursor (text_view, &new_insert, extend_selection);
5514 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5516 /* already at far right, just be sure we are at the end */
5517 if (!gtk_text_iter_ends_line (&new_insert))
5518 gtk_text_iter_forward_to_line_end (&new_insert);
5519 move_cursor (text_view, &new_insert, extend_selection);
5523 gtk_text_view_get_virtual_cursor_pos (text_view, NULL, &cursor_x_pos, &cursor_y_pos);
5525 oldval = adj->value;
5526 newval = adj->value;
5528 newval += count * adj->page_increment;
5530 set_adjustment_clamped (adj, newval);
5531 cursor_x_pos += adj->value - oldval;
5533 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5534 clamp_iter_onscreen (text_view, &new_insert);
5535 move_cursor (text_view, &new_insert, extend_selection);
5537 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5540 /* FIXME for lines shorter than the overall widget width, this results in a
5541 * "bounce" effect as we scroll to the right of the widget, then scroll
5542 * back to get the end of the line onscreen.
5543 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5546 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5547 * only guarantees 1 pixel onscreen.
5549 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5550 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5552 return !gtk_text_iter_equal (&old_insert, &new_insert);
5556 whitespace (gunichar ch, gpointer user_data)
5558 return (ch == ' ' || ch == '\t');
5562 not_whitespace (gunichar ch, gpointer user_data)
5564 return !whitespace (ch, user_data);
5568 find_whitepace_region (const GtkTextIter *center,
5569 GtkTextIter *start, GtkTextIter *end)
5574 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5575 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5576 if (whitespace (gtk_text_iter_get_char (end), NULL))
5577 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5579 return !gtk_text_iter_equal (start, end);
5583 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5586 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5587 text_view->editable))
5589 gtk_widget_error_bell (GTK_WIDGET (text_view));
5594 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5601 gboolean leave_one = FALSE;
5603 gtk_text_view_reset_im_context (text_view);
5605 if (type == GTK_DELETE_CHARS)
5607 /* Char delete deletes the selection, if one exists */
5608 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5609 text_view->editable))
5613 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5614 gtk_text_buffer_get_insert (get_buffer (text_view)));
5621 case GTK_DELETE_CHARS:
5622 gtk_text_iter_forward_cursor_positions (&end, count);
5625 case GTK_DELETE_WORD_ENDS:
5627 gtk_text_iter_forward_word_ends (&end, count);
5629 gtk_text_iter_backward_word_starts (&start, 0 - count);
5632 case GTK_DELETE_WORDS:
5635 case GTK_DELETE_DISPLAY_LINE_ENDS:
5638 case GTK_DELETE_DISPLAY_LINES:
5641 case GTK_DELETE_PARAGRAPH_ENDS:
5644 /* If we're already at a newline, we need to
5645 * simply delete that newline, instead of
5646 * moving to the next one.
5648 if (gtk_text_iter_ends_line (&end))
5650 gtk_text_iter_forward_line (&end);
5656 if (!gtk_text_iter_forward_to_line_end (&end))
5664 if (gtk_text_iter_starts_line (&start))
5666 gtk_text_iter_backward_line (&start);
5667 if (!gtk_text_iter_ends_line (&end))
5668 gtk_text_iter_forward_to_line_end (&start);
5672 gtk_text_iter_set_line_offset (&start, 0);
5676 gtk_text_iter_backward_lines (&start, -count);
5680 case GTK_DELETE_PARAGRAPHS:
5683 gtk_text_iter_set_line_offset (&start, 0);
5684 gtk_text_iter_forward_to_line_end (&end);
5686 /* Do the lines beyond the first. */
5689 gtk_text_iter_forward_to_line_end (&end);
5695 /* FIXME negative count? */
5699 case GTK_DELETE_WHITESPACE:
5701 find_whitepace_region (&insert, &start, &end);
5709 if (!gtk_text_iter_equal (&start, &end))
5711 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5713 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5714 text_view->editable))
5717 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5719 text_view->editable);
5723 gtk_widget_error_bell (GTK_WIDGET (text_view));
5726 gtk_text_buffer_end_user_action (get_buffer (text_view));
5727 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5729 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5730 gtk_text_view_scroll_mark_onscreen (text_view,
5731 gtk_text_buffer_get_insert (get_buffer (text_view)));
5735 gtk_widget_error_bell (GTK_WIDGET (text_view));
5740 gtk_text_view_backspace (GtkTextView *text_view)
5744 gtk_text_view_reset_im_context (text_view);
5746 /* Backspace deletes the selection, if one exists */
5747 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5748 text_view->editable))
5751 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5753 gtk_text_buffer_get_insert (get_buffer (text_view)));
5755 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5756 TRUE, text_view->editable))
5758 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5759 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5760 gtk_text_view_scroll_mark_onscreen (text_view,
5761 gtk_text_buffer_get_insert (get_buffer (text_view)));
5765 gtk_widget_error_bell (GTK_WIDGET (text_view));
5770 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5772 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5773 GDK_SELECTION_CLIPBOARD);
5775 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5777 text_view->editable);
5778 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5779 gtk_text_view_scroll_mark_onscreen (text_view,
5780 gtk_text_buffer_get_insert (get_buffer (text_view)));
5784 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5786 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5787 GDK_SELECTION_CLIPBOARD);
5789 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5792 /* on copy do not scroll, we are already onscreen */
5796 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5798 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5799 GDK_SELECTION_CLIPBOARD);
5801 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5804 text_view->editable);
5808 gtk_text_view_paste_done_handler (GtkTextBuffer *buffer,
5809 GtkClipboard *clipboard,
5812 GtkTextView *text_view = data;
5813 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5814 gtk_text_view_scroll_mark_onscreen (text_view, gtk_text_buffer_get_insert (buffer));
5818 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5820 if (text_view->text_window)
5821 text_window_invalidate_cursors (text_view->text_window);
5823 text_view->overwrite_mode = !text_view->overwrite_mode;
5825 if (text_view->layout)
5826 gtk_text_layout_set_overwrite_mode (text_view->layout,
5827 text_view->overwrite_mode && text_view->editable);
5829 if (text_view->text_window)
5830 text_window_invalidate_cursors (text_view->text_window);
5832 gtk_text_view_pend_cursor_blink (text_view);
5834 g_object_notify (G_OBJECT (text_view), "overwrite");
5838 * gtk_text_view_get_overwrite:
5839 * @text_view: a #GtkTextView
5841 * Returns whether the #GtkTextView is in overwrite mode or not.
5843 * Return value: whether @text_view is in overwrite mode or not.
5848 gtk_text_view_get_overwrite (GtkTextView *text_view)
5850 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5852 return text_view->overwrite_mode;
5856 * gtk_text_view_set_overwrite:
5857 * @text_view: a #GtkTextView
5858 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
5860 * Changes the #GtkTextView overwrite mode.
5865 gtk_text_view_set_overwrite (GtkTextView *text_view,
5868 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5869 overwrite = overwrite != FALSE;
5871 if (text_view->overwrite_mode != overwrite)
5872 gtk_text_view_toggle_overwrite (text_view);
5876 * gtk_text_view_set_accepts_tab:
5877 * @text_view: A #GtkTextView
5878 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
5879 * character, %FALSE, if pressing the Tab key should move the
5882 * Sets the behavior of the text widget when the Tab key is pressed.
5883 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
5884 * is %FALSE the keyboard focus is moved to the next widget in the focus
5890 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
5891 gboolean accepts_tab)
5893 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5895 accepts_tab = accepts_tab != FALSE;
5897 if (text_view->accepts_tab != accepts_tab)
5899 text_view->accepts_tab = accepts_tab;
5901 g_object_notify (G_OBJECT (text_view), "accepts-tab");
5906 * gtk_text_view_get_accepts_tab:
5907 * @text_view: A #GtkTextView
5909 * Returns whether pressing the Tab key inserts a tab characters.
5910 * gtk_text_view_set_accepts_tab().
5912 * Return value: %TRUE if pressing the Tab key inserts a tab character,
5913 * %FALSE if pressing the Tab key moves the keyboard focus.
5918 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
5920 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5922 return text_view->accepts_tab;
5926 gtk_text_view_compat_move_focus (GtkTextView *text_view,
5927 GtkDirectionType direction_type)
5929 GSignalInvocationHint *hint = g_signal_get_invocation_hint (text_view);
5931 /* as of GTK+ 2.12, the "move-focus" signal has been moved to GtkWidget,
5932 * the evil code below makes sure that both emitting the signal and
5933 * calling the virtual function directly continue to work as expetcted
5936 if (hint->signal_id == g_signal_lookup ("move-focus", GTK_TYPE_WIDGET))
5938 /* if this is a signal emission, chain up */
5942 g_signal_chain_from_overridden_handler (text_view,
5943 direction_type, &retval);
5947 /* otherwise emit the signal, since somebody called the virtual
5951 g_signal_emit_by_name (text_view, "move-focus", direction_type);
5960 gtk_text_view_unselect (GtkTextView *text_view)
5964 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5965 gtk_text_buffer_get_insert (get_buffer (text_view)));
5967 gtk_text_buffer_move_mark (get_buffer (text_view),
5968 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
5973 get_iter_at_pointer (GtkTextView *text_view,
5978 gint xcoord, ycoord;
5979 GdkModifierType state;
5981 gdk_window_get_pointer (text_view->text_window->bin_window,
5982 &xcoord, &ycoord, &state);
5984 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5986 xcoord + text_view->xoffset,
5987 ycoord + text_view->yoffset);
5996 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5997 const gchar *mark_name)
5999 GtkTextIter newplace;
6002 get_iter_at_pointer (text_view, &newplace, NULL, NULL);
6004 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
6006 /* This may invalidate the layout */
6007 DV(g_print (G_STRLOC": move mark\n"));
6009 gtk_text_buffer_move_mark (get_buffer (text_view),
6013 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6014 gtk_text_view_scroll_mark_onscreen (text_view, mark);
6016 DV (g_print ("first validate idle leaving %s is %d\n",
6017 G_STRLOC, text_view->first_validate_idle));
6021 selection_scan_timeout (gpointer data)
6023 GtkTextView *text_view;
6025 text_view = GTK_TEXT_VIEW (data);
6027 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
6028 gtk_text_view_scroll_mark_onscreen (text_view,
6029 gtk_text_buffer_get_insert (get_buffer (text_view)));
6031 return TRUE; /* remain installed. */
6034 #define UPPER_OFFSET_ANCHOR 0.8
6035 #define LOWER_OFFSET_ANCHOR 0.2
6038 check_scroll (gdouble offset, GtkAdjustment *adj)
6040 if ((offset > UPPER_OFFSET_ANCHOR &&
6041 adj->value + adj->page_size < adj->upper) ||
6042 (offset < LOWER_OFFSET_ANCHOR &&
6043 adj->value > adj->lower))
6050 drag_scan_timeout (gpointer data)
6052 GtkTextView *text_view;
6053 GtkTextIter newplace;
6054 gint x, y, width, height;
6055 gdouble pointer_xoffset, pointer_yoffset;
6057 text_view = GTK_TEXT_VIEW (data);
6059 get_iter_at_pointer (text_view, &newplace, &x, &y);
6060 gdk_drawable_get_size (text_view->text_window->bin_window, &width, &height);
6062 gtk_text_buffer_move_mark (get_buffer (text_view),
6063 text_view->dnd_mark,
6066 pointer_xoffset = (gdouble) x / width;
6067 pointer_yoffset = (gdouble) y / height;
6069 if (check_scroll (pointer_xoffset, text_view->hadjustment) ||
6070 check_scroll (pointer_yoffset, text_view->vadjustment))
6072 /* do not make offsets surpass lower nor upper anchors, this makes
6073 * scrolling speed relative to the distance of the pointer to the
6074 * anchors when it moves beyond them.
6076 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6077 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
6079 gtk_text_view_scroll_to_mark (text_view,
6080 text_view->dnd_mark,
6081 0., TRUE, pointer_xoffset, pointer_yoffset);
6092 } SelectionGranularity;
6095 * Move @start and @end to the boundaries of the selection unit (indicated by
6096 * @granularity) which contained @start initially.
6097 * If the selction unit is SELECT_WORDS and @start is not contained in a word
6098 * the selection is extended to all the white spaces between the end of the
6099 * word preceding @start and the start of the one following.
6102 extend_selection (GtkTextView *text_view,
6103 SelectionGranularity granularity,
6109 if (granularity == SELECT_WORDS)
6111 if (gtk_text_iter_inside_word (start))
6113 if (!gtk_text_iter_starts_word (start))
6114 gtk_text_iter_backward_visible_word_start (start);
6116 if (!gtk_text_iter_ends_word (end))
6118 if (!gtk_text_iter_forward_visible_word_end (end))
6119 gtk_text_iter_forward_to_end (end);
6127 if (gtk_text_iter_backward_visible_word_start (&tmp))
6128 gtk_text_iter_forward_visible_word_end (&tmp);
6130 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
6133 gtk_text_iter_set_line_offset (start, 0);
6136 if (!gtk_text_iter_forward_visible_word_end (&tmp))
6137 gtk_text_iter_forward_to_end (&tmp);
6139 if (gtk_text_iter_ends_word (&tmp))
6140 gtk_text_iter_backward_visible_word_start (&tmp);
6142 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
6145 gtk_text_iter_forward_to_line_end (end);
6148 else if (granularity == SELECT_LINES)
6150 if (gtk_text_view_starts_display_line (text_view, start))
6152 /* If on a display line boundary, we assume the user
6153 * clicked off the end of a line and we therefore select
6154 * the line before the boundary.
6156 gtk_text_view_backward_display_line_start (text_view, start);
6160 /* start isn't on the start of a line, so we move it to the
6161 * start, and move end to the end unless it's already there.
6163 gtk_text_view_backward_display_line_start (text_view, start);
6165 if (!gtk_text_view_starts_display_line (text_view, end))
6166 gtk_text_view_forward_display_line_end (text_view, end);
6174 SelectionGranularity granularity;
6175 GtkTextMark *orig_start;
6176 GtkTextMark *orig_end;
6180 selection_data_free (SelectionData *data)
6182 if (data->orig_start != NULL)
6183 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6185 if (data->orig_end != NULL)
6186 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6192 selection_motion_event_handler (GtkTextView *text_view,
6193 GdkEventMotion *event,
6194 SelectionData *data)
6196 gdk_event_request_motions (event);
6198 if (data->granularity == SELECT_CHARACTERS)
6200 move_mark_to_pointer_and_scroll (text_view, "insert");
6204 GtkTextIter cursor, start, end;
6205 GtkTextIter orig_start, orig_end;
6206 GtkTextBuffer *buffer;
6208 buffer = get_buffer (text_view);
6210 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6211 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6213 get_iter_at_pointer (text_view, &cursor, NULL, NULL);
6216 extend_selection (text_view, data->granularity, &start, &end);
6218 /* either the selection extends to the front, or end (or not) */
6219 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6220 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6222 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6224 gtk_text_view_scroll_mark_onscreen (text_view,
6225 gtk_text_buffer_get_insert (buffer));
6228 /* If we had to scroll offscreen, insert a timeout to do so
6229 * again. Note that in the timeout, even if the mouse doesn't
6230 * move, due to this scroll xoffset/yoffset will have changed
6231 * and we'll need to scroll again.
6233 if (text_view->scroll_timeout != 0) /* reset on every motion event */
6234 g_source_remove (text_view->scroll_timeout);
6236 text_view->scroll_timeout =
6237 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6243 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6244 const GtkTextIter *iter,
6245 GdkEventButton *button)
6247 GtkTextIter cursor, ins, bound;
6248 GtkTextIter orig_start, orig_end;
6249 GtkTextBuffer *buffer;
6250 SelectionData *data;
6252 if (text_view->selection_drag_handler != 0)
6255 data = g_new0 (SelectionData, 1);
6257 if (button->type == GDK_2BUTTON_PRESS)
6258 data->granularity = SELECT_WORDS;
6259 else if (button->type == GDK_3BUTTON_PRESS)
6260 data->granularity = SELECT_LINES;
6262 data->granularity = SELECT_CHARACTERS;
6264 gtk_grab_add (GTK_WIDGET (text_view));
6266 buffer = get_buffer (text_view);
6271 extend_selection (text_view, data->granularity, &ins, &bound);
6275 if (button->state & GDK_SHIFT_MASK)
6277 /* Extend selection */
6278 GtkTextIter old_ins, old_bound;
6279 GtkTextIter old_start, old_end;
6281 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6282 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6283 old_start = old_ins;
6284 old_end = old_bound;
6285 gtk_text_iter_order (&old_start, &old_end);
6287 /* move the front cursor, if the mouse is in front of the selection. Should the
6288 * cursor however be inside the selection (this happens on tripple click) then we
6289 * move the side which was last moved (current insert mark) */
6290 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6291 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6292 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6295 orig_start = old_end;
6307 gtk_text_buffer_select_range (buffer, &ins, &bound);
6309 gtk_text_iter_order (&orig_start, &orig_end);
6310 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6312 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6315 gtk_text_view_check_cursor_blink (text_view);
6317 text_view->selection_drag_handler = g_signal_connect_data (text_view,
6318 "motion-notify-event",
6319 G_CALLBACK (selection_motion_event_handler),
6321 (GClosureNotify) selection_data_free, 0);
6324 /* returns whether we were really dragging */
6326 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6328 if (text_view->selection_drag_handler == 0)
6331 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
6332 text_view->selection_drag_handler = 0;
6334 if (text_view->scroll_timeout != 0)
6336 g_source_remove (text_view->scroll_timeout);
6337 text_view->scroll_timeout = 0;
6340 gtk_grab_remove (GTK_WIDGET (text_view));
6350 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6351 GtkTextAttributes *values,
6354 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
6355 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
6358 pango_font_description_free (values->font);
6360 values->font = pango_font_description_copy (style->font_desc);
6364 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6366 if (text_view->layout)
6368 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6369 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6370 GtkTextDirection new_cursor_dir;
6371 GtkTextDirection new_keyboard_dir;
6372 gboolean split_cursor;
6374 g_object_get (settings,
6375 "gtk-split-cursor", &split_cursor,
6378 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6379 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6381 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6384 new_cursor_dir = GTK_TEXT_DIR_NONE;
6386 new_cursor_dir = new_keyboard_dir;
6388 gtk_text_layout_set_cursor_direction (text_view->layout, new_cursor_dir);
6389 gtk_text_layout_set_keyboard_direction (text_view->layout, new_keyboard_dir);
6394 gtk_text_view_ensure_layout (GtkTextView *text_view)
6398 widget = GTK_WIDGET (text_view);
6400 if (text_view->layout == NULL)
6402 GtkTextAttributes *style;
6403 PangoContext *ltr_context, *rtl_context;
6406 DV(g_print(G_STRLOC"\n"));
6408 text_view->layout = gtk_text_layout_new ();
6410 g_signal_connect (text_view->layout,
6412 G_CALLBACK (invalidated_handler),
6415 g_signal_connect (text_view->layout,
6417 G_CALLBACK (changed_handler),
6420 g_signal_connect (text_view->layout,
6422 G_CALLBACK (gtk_text_view_child_allocated),
6425 if (get_buffer (text_view))
6426 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
6428 if ((gtk_widget_has_focus (widget) && text_view->cursor_visible))
6429 gtk_text_view_pend_cursor_blink (text_view);
6431 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
6433 gtk_text_layout_set_overwrite_mode (text_view->layout,
6434 text_view->overwrite_mode && text_view->editable);
6436 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6437 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6438 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6439 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6441 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
6443 g_object_unref (ltr_context);
6444 g_object_unref (rtl_context);
6446 gtk_text_view_check_keymap_direction (text_view);
6448 style = gtk_text_attributes_new ();
6450 gtk_widget_ensure_style (widget);
6451 gtk_text_view_set_attributes_from_style (text_view,
6452 style, widget->style);
6454 style->pixels_above_lines = text_view->pixels_above_lines;
6455 style->pixels_below_lines = text_view->pixels_below_lines;
6456 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
6457 style->left_margin = text_view->left_margin;
6458 style->right_margin = text_view->right_margin;
6459 style->indent = text_view->indent;
6460 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
6462 style->wrap_mode = text_view->wrap_mode;
6463 style->justification = text_view->justify;
6464 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6466 gtk_text_layout_set_default_style (text_view->layout, style);
6468 gtk_text_attributes_unref (style);
6470 /* Set layout for all anchored children */
6472 tmp_list = text_view->children;
6473 while (tmp_list != NULL)
6475 GtkTextViewChild *vc = tmp_list->data;
6479 gtk_text_anchored_child_set_layout (vc->widget,
6481 /* vc may now be invalid! */
6484 tmp_list = g_slist_next (tmp_list);
6487 gtk_text_view_invalidate (text_view);
6492 * gtk_text_view_get_default_attributes:
6493 * @text_view: a #GtkTextView
6495 * Obtains a copy of the default text attributes. These are the
6496 * attributes used for text unless a tag overrides them.
6497 * You'd typically pass the default attributes in to
6498 * gtk_text_iter_get_attributes() in order to get the
6499 * attributes in effect at a given text position.
6501 * The return value is a copy owned by the caller of this function,
6502 * and should be freed.
6504 * Return value: a new #GtkTextAttributes
6507 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6509 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6511 gtk_text_view_ensure_layout (text_view);
6513 return gtk_text_attributes_copy (text_view->layout->default_style);
6517 gtk_text_view_destroy_layout (GtkTextView *text_view)
6519 if (text_view->layout)
6523 gtk_text_view_remove_validate_idles (text_view);
6525 g_signal_handlers_disconnect_by_func (text_view->layout,
6526 invalidated_handler,
6528 g_signal_handlers_disconnect_by_func (text_view->layout,
6532 /* Remove layout from all anchored children */
6533 tmp_list = text_view->children;
6534 while (tmp_list != NULL)
6536 GtkTextViewChild *vc = tmp_list->data;
6540 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6541 /* vc may now be invalid! */
6544 tmp_list = g_slist_next (tmp_list);
6547 gtk_text_view_stop_cursor_blink (text_view);
6548 gtk_text_view_end_selection_drag (text_view);
6550 g_object_unref (text_view->layout);
6551 text_view->layout = NULL;
6556 gtk_text_view_reset_im_context (GtkTextView *text_view)
6558 if (text_view->need_im_reset)
6560 text_view->need_im_reset = FALSE;
6561 gtk_im_context_reset (text_view->im_context);
6570 drag_begin_cb (GtkWidget *widget,
6571 GdkDragContext *context,
6574 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6575 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6578 GdkPixmap *pixmap = NULL;
6580 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6582 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6583 pixmap = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6587 gtk_drag_set_icon_pixmap (context,
6588 gdk_drawable_get_colormap (pixmap),
6592 g_object_unref (pixmap);
6596 gtk_drag_set_icon_default (context);
6601 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6602 const GtkTextIter *iter,
6603 GdkEventMotion *event)
6605 GtkTargetList *target_list;
6607 text_view->drag_start_x = -1;
6608 text_view->drag_start_y = -1;
6609 text_view->pending_place_cursor_button = 0;
6611 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6613 g_signal_connect (text_view, "drag-begin",
6614 G_CALLBACK (drag_begin_cb), NULL);
6615 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6616 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6617 1, (GdkEvent*)event);
6621 gtk_text_view_drag_begin (GtkWidget *widget,
6622 GdkDragContext *context)
6628 gtk_text_view_drag_end (GtkWidget *widget,
6629 GdkDragContext *context)
6634 gtk_text_view_drag_data_get (GtkWidget *widget,
6635 GdkDragContext *context,
6636 GtkSelectionData *selection_data,
6640 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6641 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6643 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6645 gtk_selection_data_set (selection_data,
6646 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6651 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6658 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6660 /* Extract the selected text */
6661 str = gtk_text_buffer_serialize (buffer, buffer,
6662 selection_data->target,
6669 gtk_selection_data_set (selection_data,
6670 selection_data->target,
6672 (guchar *) str, len);
6682 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6684 /* Extract the selected text */
6685 str = gtk_text_iter_get_visible_text (&start, &end);
6690 gtk_selection_data_set_text (selection_data, str, -1);
6697 gtk_text_view_drag_data_delete (GtkWidget *widget,
6698 GdkDragContext *context)
6700 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
6701 TRUE, GTK_TEXT_VIEW (widget)->editable);
6705 gtk_text_view_drag_leave (GtkWidget *widget,
6706 GdkDragContext *context,
6709 GtkTextView *text_view;
6711 text_view = GTK_TEXT_VIEW (widget);
6713 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6715 if (text_view->scroll_timeout != 0)
6716 g_source_remove (text_view->scroll_timeout);
6718 text_view->scroll_timeout = 0;
6722 gtk_text_view_drag_motion (GtkWidget *widget,
6723 GdkDragContext *context,
6728 GtkTextIter newplace;
6729 GtkTextView *text_view;
6732 GdkRectangle target_rect;
6735 GdkDragAction suggested_action = 0;
6737 text_view = GTK_TEXT_VIEW (widget);
6739 target_rect = text_view->text_window->allocation;
6741 if (x < target_rect.x ||
6742 y < target_rect.y ||
6743 x > (target_rect.x + target_rect.width) ||
6744 y > (target_rect.y + target_rect.height))
6745 return FALSE; /* outside the text window, allow parent widgets to handle event */
6747 gtk_text_view_window_to_buffer_coords (text_view,
6748 GTK_TEXT_WINDOW_WIDGET,
6752 gtk_text_layout_get_iter_at_pixel (text_view->layout,
6756 target = gtk_drag_dest_find_target (widget, context,
6757 gtk_drag_dest_get_target_list (widget));
6759 if (target == GDK_NONE)
6761 /* can't accept any of the offered targets */
6763 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6765 gtk_text_iter_compare (&newplace, &start) >= 0 &&
6766 gtk_text_iter_compare (&newplace, &end) <= 0)
6768 /* We're inside the selection. */
6772 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
6774 GtkWidget *source_widget;
6776 suggested_action = context->suggested_action;
6778 source_widget = gtk_drag_get_source_widget (context);
6780 if (source_widget == widget)
6782 /* Default to MOVE, unless the user has
6783 * pressed ctrl or alt to affect available actions
6785 if ((context->actions & GDK_ACTION_MOVE) != 0)
6786 suggested_action = GDK_ACTION_MOVE;
6791 /* Can't drop here. */
6795 if (suggested_action != 0)
6797 gtk_text_mark_set_visible (text_view->dnd_mark,
6798 text_view->cursor_visible);
6800 gdk_drag_status (context, suggested_action, time);
6804 gdk_drag_status (context, 0, time);
6805 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6808 if (!text_view->scroll_timeout)
6809 text_view->scroll_timeout =
6810 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
6812 /* TRUE return means don't propagate the drag motion to parent
6813 * widgets that may also be drop sites.
6819 gtk_text_view_drag_drop (GtkWidget *widget,
6820 GdkDragContext *context,
6825 GtkTextView *text_view;
6826 GtkTextIter drop_point;
6827 GdkAtom target = GDK_NONE;
6829 text_view = GTK_TEXT_VIEW (widget);
6831 if (text_view->scroll_timeout != 0)
6832 g_source_remove (text_view->scroll_timeout);
6834 text_view->scroll_timeout = 0;
6836 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6838 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6840 text_view->dnd_mark);
6842 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
6843 target = gtk_drag_dest_find_target (widget, context, NULL);
6845 if (target != GDK_NONE)
6846 gtk_drag_get_data (widget, context, target, time);
6848 gtk_drag_finish (context, FALSE, FALSE, time);
6854 insert_text_data (GtkTextView *text_view,
6855 GtkTextIter *drop_point,
6856 GtkSelectionData *selection_data)
6860 str = gtk_selection_data_get_text (selection_data);
6864 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
6865 drop_point, (gchar *) str, -1,
6866 text_view->editable))
6868 gtk_widget_error_bell (GTK_WIDGET (text_view));
6876 gtk_text_view_drag_data_received (GtkWidget *widget,
6877 GdkDragContext *context,
6880 GtkSelectionData *selection_data,
6884 GtkTextIter drop_point;
6885 GtkTextView *text_view;
6886 gboolean success = FALSE;
6887 GtkTextBuffer *buffer = NULL;
6889 text_view = GTK_TEXT_VIEW (widget);
6891 if (!text_view->dnd_mark)
6894 buffer = get_buffer (text_view);
6896 gtk_text_buffer_get_iter_at_mark (buffer,
6898 text_view->dnd_mark);
6900 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
6905 gtk_text_buffer_begin_user_action (buffer);
6907 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6909 GtkTextBuffer *src_buffer = NULL;
6910 GtkTextIter start, end;
6911 gboolean copy_tags = TRUE;
6913 if (selection_data->length != sizeof (src_buffer))
6916 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
6918 if (src_buffer == NULL)
6921 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
6923 if (gtk_text_buffer_get_tag_table (src_buffer) !=
6924 gtk_text_buffer_get_tag_table (buffer))
6926 /* try to find a suitable rich text target instead */
6930 GdkAtom target = GDK_NONE;
6934 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
6936 for (list = context->targets; list; list = g_list_next (list))
6940 for (i = 0; i < n_atoms; i++)
6941 if (GUINT_TO_POINTER (atoms[i]) == list->data)
6950 if (target != GDK_NONE)
6952 gtk_drag_get_data (widget, context, target, time);
6953 gtk_text_buffer_end_user_action (buffer);
6958 if (gtk_text_buffer_get_selection_bounds (src_buffer,
6963 gtk_text_buffer_insert_range_interactive (buffer,
6967 text_view->editable);
6972 str = gtk_text_iter_get_visible_text (&start, &end);
6973 gtk_text_buffer_insert_interactive (buffer,
6974 &drop_point, str, -1,
6975 text_view->editable);
6980 else if (selection_data->length > 0 &&
6981 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6984 GError *error = NULL;
6986 retval = gtk_text_buffer_deserialize (buffer, buffer,
6987 selection_data->target,
6989 (guint8 *) selection_data->data,
6990 selection_data->length,
6995 g_warning ("error pasting: %s\n", error->message);
6996 g_clear_error (&error);
7000 insert_text_data (text_view, &drop_point, selection_data);
7003 gtk_drag_finish (context, success,
7004 success && context->action == GDK_ACTION_MOVE,
7009 gtk_text_buffer_get_iter_at_mark (buffer,
7011 text_view->dnd_mark);
7012 gtk_text_buffer_place_cursor (buffer, &drop_point);
7014 gtk_text_buffer_end_user_action (buffer);
7018 static GtkAdjustment*
7019 get_hadjustment (GtkTextView *text_view)
7021 if (text_view->hadjustment == NULL)
7022 gtk_text_view_set_scroll_adjustments (text_view,
7023 NULL, /* forces creation */
7024 text_view->vadjustment);
7026 return text_view->hadjustment;
7029 static GtkAdjustment*
7030 get_vadjustment (GtkTextView *text_view)
7032 if (text_view->vadjustment == NULL)
7033 gtk_text_view_set_scroll_adjustments (text_view,
7034 text_view->hadjustment,
7035 NULL); /* forces creation */
7036 return text_view->vadjustment;
7041 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
7042 GtkAdjustment *hadj,
7043 GtkAdjustment *vadj)
7045 gboolean need_adjust = FALSE;
7048 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
7050 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7052 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
7054 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
7056 if (text_view->hadjustment && (text_view->hadjustment != hadj))
7058 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
7059 gtk_text_view_value_changed,
7061 g_object_unref (text_view->hadjustment);
7064 if (text_view->vadjustment && (text_view->vadjustment != vadj))
7066 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
7067 gtk_text_view_value_changed,
7069 g_object_unref (text_view->vadjustment);
7072 if (text_view->hadjustment != hadj)
7074 text_view->hadjustment = hadj;
7075 g_object_ref_sink (text_view->hadjustment);
7077 g_signal_connect (text_view->hadjustment, "value-changed",
7078 G_CALLBACK (gtk_text_view_value_changed),
7083 if (text_view->vadjustment != vadj)
7085 text_view->vadjustment = vadj;
7086 g_object_ref_sink (text_view->vadjustment);
7088 g_signal_connect (text_view->vadjustment, "value-changed",
7089 G_CALLBACK (gtk_text_view_value_changed),
7095 gtk_text_view_value_changed (NULL, text_view);
7098 /* FIXME this adjust_allocation is a big cut-and-paste from
7099 * GtkCList, needs to be some "official" way to do this
7109 /* The window to which widget->window is relative */
7110 #define ALLOCATION_WINDOW(widget) \
7111 (!gtk_widget_get_has_window (widget) ? \
7112 (widget)->window : \
7113 gdk_window_get_parent ((widget)->window))
7116 adjust_allocation_recurse (GtkWidget *widget,
7119 ScrollData *scroll_data = data;
7121 /* Need to really size allocate instead of just poking
7122 * into widget->allocation if the widget is not realized.
7123 * FIXME someone figure out why this was.
7125 if (!GTK_WIDGET_REALIZED (widget))
7127 if (gtk_widget_get_visible (widget))
7129 GdkRectangle tmp_rectangle = widget->allocation;
7130 tmp_rectangle.x += scroll_data->dx;
7131 tmp_rectangle.y += scroll_data->dy;
7133 gtk_widget_size_allocate (widget, &tmp_rectangle);
7138 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
7140 widget->allocation.x += scroll_data->dx;
7141 widget->allocation.y += scroll_data->dy;
7143 if (GTK_IS_CONTAINER (widget))
7144 gtk_container_forall (GTK_CONTAINER (widget),
7145 adjust_allocation_recurse,
7152 adjust_allocation (GtkWidget *widget,
7156 ScrollData scroll_data;
7158 if (GTK_WIDGET_REALIZED (widget))
7159 scroll_data.window = ALLOCATION_WINDOW (widget);
7161 scroll_data.window = NULL;
7163 scroll_data.dx = dx;
7164 scroll_data.dy = dy;
7166 adjust_allocation_recurse (widget, &scroll_data);
7170 gtk_text_view_value_changed (GtkAdjustment *adj,
7171 GtkTextView *text_view)
7178 /* Note that we oddly call this function with adj == NULL
7182 text_view->onscreen_validated = FALSE;
7184 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7185 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
7186 adj ? adj->value : 0.0));
7188 if (adj == text_view->hadjustment)
7190 dx = text_view->xoffset - (gint)adj->value;
7191 text_view->xoffset = adj->value;
7193 /* If the change is due to a size change we need
7194 * to invalidate the entire text window because there might be
7195 * right-aligned or centered text
7197 if (text_view->width_changed)
7199 if (GTK_WIDGET_REALIZED (text_view))
7200 gdk_window_invalidate_rect (text_view->text_window->bin_window, NULL, FALSE);
7202 text_view->width_changed = FALSE;
7205 else if (adj == text_view->vadjustment)
7207 dy = text_view->yoffset - (gint)adj->value;
7208 text_view->yoffset = adj->value;
7210 if (text_view->layout)
7212 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
7214 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
7216 text_view->first_para_pixels = adj->value - line_top;
7220 if (dx != 0 || dy != 0)
7224 if (GTK_WIDGET_REALIZED (text_view))
7228 if (text_view->left_window)
7229 text_window_scroll (text_view->left_window, 0, dy);
7230 if (text_view->right_window)
7231 text_window_scroll (text_view->right_window, 0, dy);
7236 if (text_view->top_window)
7237 text_window_scroll (text_view->top_window, dx, 0);
7238 if (text_view->bottom_window)
7239 text_window_scroll (text_view->bottom_window, dx, 0);
7242 /* It looks nicer to scroll the main area last, because
7243 * it takes a while, and making the side areas update
7244 * afterward emphasizes the slowness of scrolling the
7247 text_window_scroll (text_view->text_window, dx, dy);
7250 /* Children are now "moved" in the text window, poke
7251 * into widget->allocation for each child
7253 tmp_list = text_view->children;
7254 while (tmp_list != NULL)
7256 GtkTextViewChild *child = tmp_list->data;
7259 adjust_allocation (child->widget, dx, dy);
7261 tmp_list = g_slist_next (tmp_list);
7265 /* This could result in invalidation, which would install the
7266 * first_validate_idle, which would validate onscreen;
7267 * but we're going to go ahead and validate here, so
7268 * first_validate_idle shouldn't have anything to do.
7270 gtk_text_view_update_layout_width (text_view);
7272 /* We also update the IM spot location here, since the IM context
7273 * might do something that leads to validation.
7275 gtk_text_view_update_im_spot_location (text_view);
7277 /* note that validation of onscreen could invoke this function
7278 * recursively, by scrolling to maintain first_para, or in response
7279 * to updating the layout width, however there is no problem with
7280 * that, or shouldn't be.
7282 gtk_text_view_validate_onscreen (text_view);
7284 /* process exposes */
7285 if (GTK_WIDGET_REALIZED (text_view))
7287 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7289 if (text_view->left_window)
7290 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
7292 if (text_view->right_window)
7293 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
7295 if (text_view->top_window)
7296 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
7298 if (text_view->bottom_window)
7299 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
7301 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
7304 /* If this got installed, get rid of it, it's just a waste of time. */
7305 if (text_view->first_validate_idle != 0)
7307 g_source_remove (text_view->first_validate_idle);
7308 text_view->first_validate_idle = 0;
7311 /* Finally we update the IM cursor location again, to ensure any
7312 * changes made by the validation are pushed through.
7314 gtk_text_view_update_im_spot_location (text_view);
7316 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7320 gtk_text_view_commit_handler (GtkIMContext *context,
7322 GtkTextView *text_view)
7324 gtk_text_view_commit_text (text_view, str);
7328 gtk_text_view_commit_text (GtkTextView *text_view,
7331 gboolean had_selection;
7333 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7335 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7338 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7339 text_view->editable);
7341 if (!strcmp (str, "\n"))
7343 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7344 text_view->editable))
7346 gtk_widget_error_bell (GTK_WIDGET (text_view));
7351 if (!had_selection && text_view->overwrite_mode)
7355 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7357 gtk_text_buffer_get_insert (get_buffer (text_view)));
7358 if (!gtk_text_iter_ends_line (&insert))
7359 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7362 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7363 text_view->editable))
7365 gtk_widget_error_bell (GTK_WIDGET (text_view));
7369 gtk_text_buffer_end_user_action (get_buffer (text_view));
7371 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7372 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7373 gtk_text_view_scroll_mark_onscreen (text_view,
7374 gtk_text_buffer_get_insert (get_buffer (text_view)));
7378 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7379 GtkTextView *text_view)
7382 PangoAttrList *attrs;
7386 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7387 gtk_text_buffer_get_insert (text_view->buffer));
7389 /* Keypress events are passed to input method even if cursor position is
7390 * not editable; so beep here if it's multi-key input sequence, input
7391 * method will be reset in key-press-event handler.
7393 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7395 if (str && str[0] && !gtk_text_iter_can_insert (&iter, text_view->editable))
7397 gtk_widget_error_bell (GTK_WIDGET (text_view));
7401 g_signal_emit (text_view, signals[PREEDIT_CHANGED], 0, str);
7403 if (text_view->layout)
7404 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
7405 if (gtk_widget_has_focus (GTK_WIDGET (text_view)))
7406 gtk_text_view_scroll_mark_onscreen (text_view,
7407 gtk_text_buffer_get_insert (get_buffer (text_view)));
7410 pango_attr_list_unref (attrs);
7415 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7416 GtkTextView *text_view)
7423 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7424 gtk_text_buffer_get_insert (text_view->buffer));
7427 pos = gtk_text_iter_get_line_index (&start);
7428 gtk_text_iter_set_line_offset (&start, 0);
7429 gtk_text_iter_forward_to_line_end (&end);
7431 text = gtk_text_iter_get_slice (&start, &end);
7432 gtk_im_context_set_surrounding (context, text, -1, pos);
7439 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7442 GtkTextView *text_view)
7447 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7448 gtk_text_buffer_get_insert (text_view->buffer));
7451 gtk_text_iter_forward_chars (&start, offset);
7452 gtk_text_iter_forward_chars (&end, offset + n_chars);
7454 gtk_text_buffer_delete_interactive (text_view->buffer, &start, &end,
7455 text_view->editable);
7461 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7462 const GtkTextIter *location,
7466 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7467 gboolean need_reset = FALSE;
7469 if (mark == gtk_text_buffer_get_insert (buffer))
7471 text_view->virtual_cursor_x = -1;
7472 text_view->virtual_cursor_y = -1;
7473 gtk_text_view_update_im_spot_location (text_view);
7476 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7482 gtk_text_view_reset_im_context (text_view);
7486 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7487 const GParamSpec *pspec,
7490 GtkWidget *widget = GTK_WIDGET (data);
7491 GtkTargetList *view_list;
7492 GtkTargetList *buffer_list;
7495 view_list = gtk_drag_dest_get_target_list (widget);
7496 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7499 gtk_target_list_ref (view_list);
7501 view_list = gtk_target_list_new (NULL, 0);
7503 list = view_list->list;
7506 GtkTargetPair *pair = list->data;
7508 list = g_list_next (list); /* get next element before removing */
7510 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7511 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7513 gtk_target_list_remove (view_list, pair->target);
7517 for (list = buffer_list->list; list; list = g_list_next (list))
7519 GtkTargetPair *pair = list->data;
7521 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7524 gtk_drag_dest_set_target_list (widget, view_list);
7525 gtk_target_list_unref (view_list);
7529 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7534 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7535 gtk_text_buffer_get_insert (get_buffer (text_view)));
7537 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, pos, NULL);
7541 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7542 GtkTextIter *cursor,
7552 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7553 gtk_text_buffer_get_insert (get_buffer (text_view)));
7555 if ((x && text_view->virtual_cursor_x == -1) ||
7556 (y && text_view->virtual_cursor_y == -1))
7557 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &pos, NULL);
7561 if (text_view->virtual_cursor_x != -1)
7562 *x = text_view->virtual_cursor_x;
7569 if (text_view->virtual_cursor_x != -1)
7570 *y = text_view->virtual_cursor_y;
7572 *y = pos.y + pos.height / 2;
7577 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7583 if (!text_view->layout)
7586 if (x == -1 || y == -1)
7587 gtk_text_view_get_cursor_location (text_view, &pos);
7589 text_view->virtual_cursor_x = (x == -1) ? pos.x : x;
7590 text_view->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7593 /* Quick hack of a popup menu
7596 activate_cb (GtkWidget *menuitem,
7597 GtkTextView *text_view)
7599 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7600 g_signal_emit_by_name (text_view, signal);
7604 append_action_signal (GtkTextView *text_view,
7606 const gchar *stock_id,
7607 const gchar *signal,
7610 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7612 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7613 g_signal_connect (menuitem, "activate",
7614 G_CALLBACK (activate_cb), text_view);
7616 gtk_widget_set_sensitive (menuitem, sensitive);
7618 gtk_widget_show (menuitem);
7619 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7623 gtk_text_view_select_all (GtkWidget *widget,
7626 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
7627 GtkTextBuffer *buffer;
7628 GtkTextIter start_iter, end_iter, insert;
7630 buffer = text_view->buffer;
7633 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
7634 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
7638 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
7639 gtk_text_buffer_get_insert (buffer));
7640 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
7645 select_all_cb (GtkWidget *menuitem,
7646 GtkTextView *text_view)
7648 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
7652 delete_cb (GtkTextView *text_view)
7654 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7655 text_view->editable);
7659 popup_menu_detach (GtkWidget *attach_widget,
7662 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
7666 popup_position_func (GtkMenu *menu,
7672 GtkTextView *text_view;
7674 GdkRectangle cursor_rect;
7675 GdkRectangle onscreen_rect;
7676 gint root_x, root_y;
7681 GdkRectangle monitor;
7683 text_view = GTK_TEXT_VIEW (user_data);
7684 widget = GTK_WIDGET (text_view);
7686 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
7688 screen = gtk_widget_get_screen (widget);
7690 gdk_window_get_origin (widget->window, &root_x, &root_y);
7692 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7694 gtk_text_buffer_get_insert (get_buffer (text_view)));
7696 gtk_text_view_get_iter_location (text_view,
7700 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
7702 gtk_widget_size_request (text_view->popup_menu, &req);
7704 /* can't use rectangle_intersect since cursor rect can have 0 width */
7705 if (cursor_rect.x >= onscreen_rect.x &&
7706 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
7707 cursor_rect.y >= onscreen_rect.y &&
7708 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
7710 gtk_text_view_buffer_to_window_coords (text_view,
7711 GTK_TEXT_WINDOW_WIDGET,
7712 cursor_rect.x, cursor_rect.y,
7713 &cursor_rect.x, &cursor_rect.y);
7715 *x = root_x + cursor_rect.x + cursor_rect.width;
7716 *y = root_y + cursor_rect.y + cursor_rect.height;
7720 /* Just center the menu, since cursor is offscreen. */
7721 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
7722 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
7726 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
7727 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
7729 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
7730 gtk_menu_set_monitor (menu, monitor_num);
7731 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
7733 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
7734 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
7741 GtkTextView *text_view;
7747 range_contains_editable_text (const GtkTextIter *start,
7748 const GtkTextIter *end,
7749 gboolean default_editability)
7751 GtkTextIter iter = *start;
7753 while (gtk_text_iter_compare (&iter, end) < 0)
7755 if (gtk_text_iter_editable (&iter, default_editability))
7758 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
7765 unichar_chosen_func (const char *text,
7768 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7770 gtk_text_view_commit_text (text_view, text);
7774 popup_targets_received (GtkClipboard *clipboard,
7775 GtkSelectionData *data,
7778 PopupInfo *info = user_data;
7779 GtkTextView *text_view = info->text_view;
7781 if (GTK_WIDGET_REALIZED (text_view))
7783 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
7784 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
7786 gboolean clipboard_contains_text;
7787 GtkWidget *menuitem;
7789 gboolean have_selection;
7790 gboolean can_insert;
7792 GtkTextIter sel_start, sel_end;
7793 gboolean show_input_method_menu;
7794 gboolean show_unicode_menu;
7796 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
7798 if (text_view->popup_menu)
7799 gtk_widget_destroy (text_view->popup_menu);
7801 text_view->popup_menu = gtk_menu_new ();
7803 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
7804 GTK_WIDGET (text_view),
7807 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7808 &sel_start, &sel_end);
7810 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7812 gtk_text_buffer_get_insert (get_buffer (text_view)));
7814 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
7816 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut-clipboard",
7818 range_contains_editable_text (&sel_start, &sel_end,
7819 text_view->editable));
7820 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy-clipboard",
7822 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste-clipboard",
7823 can_insert && clipboard_contains_text);
7825 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
7826 gtk_widget_set_sensitive (menuitem,
7828 range_contains_editable_text (&sel_start, &sel_end,
7829 text_view->editable));
7830 g_signal_connect_swapped (menuitem, "activate",
7831 G_CALLBACK (delete_cb), text_view);
7832 gtk_widget_show (menuitem);
7833 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7835 menuitem = gtk_separator_menu_item_new ();
7836 gtk_widget_show (menuitem);
7837 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7839 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
7840 g_signal_connect (menuitem, "activate",
7841 G_CALLBACK (select_all_cb), text_view);
7842 gtk_widget_show (menuitem);
7843 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7845 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
7846 "gtk-show-input-method-menu", &show_input_method_menu,
7847 "gtk-show-unicode-menu", &show_unicode_menu,
7850 if (show_input_method_menu || show_unicode_menu)
7852 menuitem = gtk_separator_menu_item_new ();
7853 gtk_widget_show (menuitem);
7854 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7857 if (show_input_method_menu)
7859 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
7860 gtk_widget_show (menuitem);
7861 gtk_widget_set_sensitive (menuitem, can_insert);
7863 submenu = gtk_menu_new ();
7864 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7865 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7867 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
7868 GTK_MENU_SHELL (submenu));
7871 if (show_unicode_menu)
7873 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
7874 gtk_widget_show (menuitem);
7875 gtk_widget_set_sensitive (menuitem, can_insert);
7877 submenu = gtk_menu_new ();
7878 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7879 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7881 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
7882 unichar_chosen_func,
7886 g_signal_emit (text_view,
7887 signals[POPULATE_POPUP],
7889 text_view->popup_menu);
7892 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7894 info->button, info->time);
7897 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7898 popup_position_func, text_view,
7899 0, gtk_get_current_event_time ());
7900 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
7904 g_object_unref (text_view);
7909 gtk_text_view_do_popup (GtkTextView *text_view,
7910 GdkEventButton *event)
7912 PopupInfo *info = g_new (PopupInfo, 1);
7914 /* In order to know what entries we should make sensitive, we
7915 * ask for the current targets of the clipboard, and when
7916 * we get them, then we actually pop up the menu.
7918 info->text_view = g_object_ref (text_view);
7922 info->button = event->button;
7923 info->time = event->time;
7928 info->time = gtk_get_current_event_time ();
7931 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
7932 GDK_SELECTION_CLIPBOARD),
7933 gdk_atom_intern_static_string ("TARGETS"),
7934 popup_targets_received,
7939 gtk_text_view_popup_menu (GtkWidget *widget)
7941 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
7945 /* Child GdkWindows */
7948 static GtkTextWindow*
7949 text_window_new (GtkTextWindowType type,
7952 gint height_request)
7956 win = g_new (GtkTextWindow, 1);
7959 win->widget = widget;
7961 win->bin_window = NULL;
7962 win->requisition.width = width_request;
7963 win->requisition.height = height_request;
7964 win->allocation.width = width_request;
7965 win->allocation.height = height_request;
7966 win->allocation.x = 0;
7967 win->allocation.y = 0;
7973 text_window_free (GtkTextWindow *win)
7976 text_window_unrealize (win);
7982 text_window_realize (GtkTextWindow *win,
7985 GdkWindowAttr attributes;
7986 gint attributes_mask;
7989 attributes.window_type = GDK_WINDOW_CHILD;
7990 attributes.x = win->allocation.x;
7991 attributes.y = win->allocation.y;
7992 attributes.width = win->allocation.width;
7993 attributes.height = win->allocation.height;
7994 attributes.wclass = GDK_INPUT_OUTPUT;
7995 attributes.visual = gtk_widget_get_visual (win->widget);
7996 attributes.colormap = gtk_widget_get_colormap (win->widget);
7997 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
7999 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
8001 win->window = gdk_window_new (widget->window,
8005 gdk_window_set_back_pixmap (win->window, NULL, FALSE);
8007 gdk_window_show (win->window);
8008 gdk_window_set_user_data (win->window, win->widget);
8009 gdk_window_lower (win->window);
8013 attributes.width = win->allocation.width;
8014 attributes.height = win->allocation.height;
8015 attributes.event_mask = (GDK_EXPOSURE_MASK |
8017 GDK_KEY_PRESS_MASK |
8018 GDK_BUTTON_PRESS_MASK |
8019 GDK_BUTTON_RELEASE_MASK |
8020 GDK_POINTER_MOTION_MASK |
8021 GDK_POINTER_MOTION_HINT_MASK |
8022 gtk_widget_get_events (win->widget));
8024 win->bin_window = gdk_window_new (win->window,
8028 gdk_window_show (win->bin_window);
8029 gdk_window_set_user_data (win->bin_window, win->widget);
8031 if (win->type == GTK_TEXT_WINDOW_TEXT)
8033 if (gtk_widget_is_sensitive (widget))
8036 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (widget->window),
8038 gdk_window_set_cursor (win->bin_window, cursor);
8039 gdk_cursor_unref (cursor);
8042 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->im_context,
8046 gdk_window_set_background (win->bin_window,
8047 &widget->style->base[GTK_WIDGET_STATE (widget)]);
8051 gdk_window_set_background (win->bin_window,
8052 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
8055 g_object_set_qdata (G_OBJECT (win->window),
8056 g_quark_from_static_string ("gtk-text-view-text-window"),
8059 g_object_set_qdata (G_OBJECT (win->bin_window),
8060 g_quark_from_static_string ("gtk-text-view-text-window"),
8065 text_window_unrealize (GtkTextWindow *win)
8067 if (win->type == GTK_TEXT_WINDOW_TEXT)
8069 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
8073 gdk_window_set_user_data (win->window, NULL);
8074 gdk_window_set_user_data (win->bin_window, NULL);
8075 gdk_window_destroy (win->bin_window);
8076 gdk_window_destroy (win->window);
8078 win->bin_window = NULL;
8082 text_window_size_allocate (GtkTextWindow *win,
8085 win->allocation = *rect;
8089 gdk_window_move_resize (win->window,
8091 rect->width, rect->height);
8093 gdk_window_resize (win->bin_window,
8094 rect->width, rect->height);
8099 text_window_scroll (GtkTextWindow *win,
8103 if (dx != 0 || dy != 0)
8105 gdk_window_scroll (win->bin_window, dx, dy);
8110 text_window_invalidate_rect (GtkTextWindow *win,
8113 GdkRectangle window_rect;
8115 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
8122 window_rect.width = rect->width;
8123 window_rect.height = rect->height;
8125 /* Adjust the rect as appropriate */
8129 case GTK_TEXT_WINDOW_TEXT:
8132 case GTK_TEXT_WINDOW_LEFT:
8133 case GTK_TEXT_WINDOW_RIGHT:
8135 window_rect.width = win->allocation.width;
8138 case GTK_TEXT_WINDOW_TOP:
8139 case GTK_TEXT_WINDOW_BOTTOM:
8141 window_rect.height = win->allocation.height;
8145 g_warning ("%s: bug!", G_STRFUNC);
8150 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
8154 cairo_t *cr = gdk_cairo_create (win->bin_window);
8155 gdk_cairo_rectangle (cr, &window_rect);
8156 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
8164 text_window_invalidate_cursors (GtkTextWindow *win)
8166 GtkTextView *text_view = GTK_TEXT_VIEW (win->widget);
8168 GdkRectangle strong;
8170 gboolean draw_arrow;
8171 gfloat cursor_aspect_ratio;
8175 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
8176 gtk_text_buffer_get_insert (text_view->buffer));
8178 if (_gtk_text_layout_get_block_cursor (text_view->layout, &strong))
8180 text_window_invalidate_rect (win, &strong);
8184 gtk_text_layout_get_cursor_locations (text_view->layout, &iter,
8187 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
8188 * ignoring the text direction be exposing both sides of the cursor
8191 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
8193 gtk_widget_style_get (win->widget,
8194 "cursor-aspect-ratio", &cursor_aspect_ratio,
8197 stem_width = strong.height * cursor_aspect_ratio + 1;
8198 arrow_width = stem_width + 1;
8200 strong.width = stem_width;
8202 /* round up to the next even number */
8206 strong.x -= stem_width / 2;
8207 strong.width += stem_width;
8211 strong.x -= arrow_width;
8212 strong.width += arrow_width * 2;
8215 text_window_invalidate_rect (win, &strong);
8217 if (draw_arrow) /* == have weak */
8219 stem_width = weak.height * cursor_aspect_ratio + 1;
8220 arrow_width = stem_width + 1;
8222 weak.width = stem_width;
8224 /* round up to the next even number */
8228 weak.x -= stem_width / 2;
8229 weak.width += stem_width;
8231 weak.x -= arrow_width;
8232 weak.width += arrow_width * 2;
8234 text_window_invalidate_rect (win, &weak);
8239 text_window_get_width (GtkTextWindow *win)
8241 return win->allocation.width;
8245 text_window_get_height (GtkTextWindow *win)
8247 return win->allocation.height;
8254 * gtk_text_view_get_window:
8255 * @text_view: a #GtkTextView
8256 * @win: window to get
8258 * Retrieves the #GdkWindow corresponding to an area of the text view;
8259 * possible windows include the overall widget window, child windows
8260 * on the left, right, top, bottom, and the window that displays the
8261 * text buffer. Windows are %NULL and nonexistent if their width or
8262 * height is 0, and are nonexistent before the widget has been
8265 * Return value: (transfer none): a #GdkWindow, or %NULL
8268 gtk_text_view_get_window (GtkTextView *text_view,
8269 GtkTextWindowType win)
8271 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8275 case GTK_TEXT_WINDOW_WIDGET:
8276 return GTK_WIDGET (text_view)->window;
8279 case GTK_TEXT_WINDOW_TEXT:
8280 return text_view->text_window->bin_window;
8283 case GTK_TEXT_WINDOW_LEFT:
8284 if (text_view->left_window)
8285 return text_view->left_window->bin_window;
8290 case GTK_TEXT_WINDOW_RIGHT:
8291 if (text_view->right_window)
8292 return text_view->right_window->bin_window;
8297 case GTK_TEXT_WINDOW_TOP:
8298 if (text_view->top_window)
8299 return text_view->top_window->bin_window;
8304 case GTK_TEXT_WINDOW_BOTTOM:
8305 if (text_view->bottom_window)
8306 return text_view->bottom_window->bin_window;
8311 case GTK_TEXT_WINDOW_PRIVATE:
8312 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8317 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8322 * gtk_text_view_get_window_type:
8323 * @text_view: a #GtkTextView
8324 * @window: a window type
8326 * Usually used to find out which window an event corresponds to.
8327 * If you connect to an event signal on @text_view, this function
8328 * should be called on <literal>event->window</literal> to
8329 * see which window it was.
8331 * Return value: the window type.
8334 gtk_text_view_get_window_type (GtkTextView *text_view,
8339 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8340 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8342 if (window == GTK_WIDGET (text_view)->window)
8343 return GTK_TEXT_WINDOW_WIDGET;
8345 win = g_object_get_qdata (G_OBJECT (window),
8346 g_quark_try_string ("gtk-text-view-text-window"));
8352 return GTK_TEXT_WINDOW_PRIVATE;
8357 buffer_to_widget (GtkTextView *text_view,
8365 *window_x = buffer_x - text_view->xoffset;
8366 *window_x += text_view->text_window->allocation.x;
8371 *window_y = buffer_y - text_view->yoffset;
8372 *window_y += text_view->text_window->allocation.y;
8377 widget_to_text_window (GtkTextWindow *win,
8384 *window_x = widget_x - win->allocation.x;
8387 *window_y = widget_y - win->allocation.y;
8391 buffer_to_text_window (GtkTextView *text_view,
8400 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8401 "for a nonexistent or private child window of GtkTextView");
8405 buffer_to_widget (text_view,
8407 window_x, window_y);
8409 widget_to_text_window (win,
8410 window_x ? *window_x : 0,
8411 window_y ? *window_y : 0,
8417 * gtk_text_view_buffer_to_window_coords:
8418 * @text_view: a #GtkTextView
8419 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8420 * @buffer_x: buffer x coordinate
8421 * @buffer_y: buffer y coordinate
8422 * @window_x: window x coordinate return location
8423 * @window_y: window y coordinate return location
8425 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8426 * @win, and stores the result in (@window_x, @window_y).
8428 * Note that you can't convert coordinates for a nonexisting window (see
8429 * gtk_text_view_set_border_window_size()).
8432 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8433 GtkTextWindowType win,
8439 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8443 case GTK_TEXT_WINDOW_WIDGET:
8444 buffer_to_widget (text_view,
8446 window_x, window_y);
8449 case GTK_TEXT_WINDOW_TEXT:
8451 *window_x = buffer_x - text_view->xoffset;
8453 *window_y = buffer_y - text_view->yoffset;
8456 case GTK_TEXT_WINDOW_LEFT:
8457 buffer_to_text_window (text_view,
8458 text_view->left_window,
8460 window_x, window_y);
8463 case GTK_TEXT_WINDOW_RIGHT:
8464 buffer_to_text_window (text_view,
8465 text_view->right_window,
8467 window_x, window_y);
8470 case GTK_TEXT_WINDOW_TOP:
8471 buffer_to_text_window (text_view,
8472 text_view->top_window,
8474 window_x, window_y);
8477 case GTK_TEXT_WINDOW_BOTTOM:
8478 buffer_to_text_window (text_view,
8479 text_view->bottom_window,
8481 window_x, window_y);
8484 case GTK_TEXT_WINDOW_PRIVATE:
8485 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8489 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8495 widget_to_buffer (GtkTextView *text_view,
8503 *buffer_x = widget_x + text_view->xoffset;
8504 *buffer_x -= text_view->text_window->allocation.x;
8509 *buffer_y = widget_y + text_view->yoffset;
8510 *buffer_y -= text_view->text_window->allocation.y;
8515 text_window_to_widget (GtkTextWindow *win,
8522 *widget_x = window_x + win->allocation.x;
8525 *widget_y = window_y + win->allocation.y;
8529 text_window_to_buffer (GtkTextView *text_view,
8538 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8539 "coordinates for a nonexistent child window.");
8543 text_window_to_widget (win,
8549 widget_to_buffer (text_view,
8550 buffer_x ? *buffer_x : 0,
8551 buffer_y ? *buffer_y : 0,
8557 * gtk_text_view_window_to_buffer_coords:
8558 * @text_view: a #GtkTextView
8559 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8560 * @window_x: window x coordinate
8561 * @window_y: window y coordinate
8562 * @buffer_x: buffer x coordinate return location
8563 * @buffer_y: buffer y coordinate return location
8565 * Converts coordinates on the window identified by @win to buffer
8566 * coordinates, storing the result in (@buffer_x,@buffer_y).
8568 * Note that you can't convert coordinates for a nonexisting window (see
8569 * gtk_text_view_set_border_window_size()).
8572 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8573 GtkTextWindowType win,
8579 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8583 case GTK_TEXT_WINDOW_WIDGET:
8584 widget_to_buffer (text_view,
8586 buffer_x, buffer_y);
8589 case GTK_TEXT_WINDOW_TEXT:
8591 *buffer_x = window_x + text_view->xoffset;
8593 *buffer_y = window_y + text_view->yoffset;
8596 case GTK_TEXT_WINDOW_LEFT:
8597 text_window_to_buffer (text_view,
8598 text_view->left_window,
8600 buffer_x, buffer_y);
8603 case GTK_TEXT_WINDOW_RIGHT:
8604 text_window_to_buffer (text_view,
8605 text_view->right_window,
8607 buffer_x, buffer_y);
8610 case GTK_TEXT_WINDOW_TOP:
8611 text_window_to_buffer (text_view,
8612 text_view->top_window,
8614 buffer_x, buffer_y);
8617 case GTK_TEXT_WINDOW_BOTTOM:
8618 text_window_to_buffer (text_view,
8619 text_view->bottom_window,
8621 buffer_x, buffer_y);
8624 case GTK_TEXT_WINDOW_PRIVATE:
8625 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8629 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8635 set_window_width (GtkTextView *text_view,
8637 GtkTextWindowType type,
8638 GtkTextWindow **winp)
8644 text_window_free (*winp);
8646 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8653 *winp = text_window_new (type,
8654 GTK_WIDGET (text_view),
8656 /* if the widget is already realized we need to realize the child manually */
8657 if (GTK_WIDGET_REALIZED (text_view))
8658 text_window_realize (*winp, GTK_WIDGET (text_view));
8662 if ((*winp)->requisition.width == width)
8665 (*winp)->requisition.width = width;
8668 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8674 set_window_height (GtkTextView *text_view,
8676 GtkTextWindowType type,
8677 GtkTextWindow **winp)
8683 text_window_free (*winp);
8685 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8692 *winp = text_window_new (type,
8693 GTK_WIDGET (text_view),
8696 /* if the widget is already realized we need to realize the child manually */
8697 if (GTK_WIDGET_REALIZED (text_view))
8698 text_window_realize (*winp, GTK_WIDGET (text_view));
8702 if ((*winp)->requisition.height == height)
8705 (*winp)->requisition.height = height;
8708 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8713 * gtk_text_view_set_border_window_size:
8714 * @text_view: a #GtkTextView
8715 * @type: window to affect
8716 * @size: width or height of the window
8718 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
8719 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
8720 * Automatically destroys the corresponding window if the size is set
8721 * to 0, and creates the window if the size is set to non-zero. This
8722 * function can only be used for the "border windows," it doesn't work
8723 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
8724 * #GTK_TEXT_WINDOW_PRIVATE.
8727 gtk_text_view_set_border_window_size (GtkTextView *text_view,
8728 GtkTextWindowType type,
8732 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8733 g_return_if_fail (size >= 0);
8737 case GTK_TEXT_WINDOW_LEFT:
8738 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
8739 &text_view->left_window);
8742 case GTK_TEXT_WINDOW_RIGHT:
8743 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
8744 &text_view->right_window);
8747 case GTK_TEXT_WINDOW_TOP:
8748 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
8749 &text_view->top_window);
8752 case GTK_TEXT_WINDOW_BOTTOM:
8753 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
8754 &text_view->bottom_window);
8758 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
8764 * gtk_text_view_get_border_window_size:
8765 * @text_view: a #GtkTextView
8766 * @type: window to return size from
8768 * Gets the width of the specified border window. See
8769 * gtk_text_view_set_border_window_size().
8771 * Return value: width of window
8774 gtk_text_view_get_border_window_size (GtkTextView *text_view,
8775 GtkTextWindowType type)
8777 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8781 case GTK_TEXT_WINDOW_LEFT:
8782 if (text_view->left_window)
8783 return text_view->left_window->requisition.width;
8786 case GTK_TEXT_WINDOW_RIGHT:
8787 if (text_view->right_window)
8788 return text_view->right_window->requisition.width;
8791 case GTK_TEXT_WINDOW_TOP:
8792 if (text_view->top_window)
8793 return text_view->top_window->requisition.height;
8796 case GTK_TEXT_WINDOW_BOTTOM:
8797 if (text_view->bottom_window)
8798 return text_view->bottom_window->requisition.height;
8802 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
8813 static GtkTextViewChild*
8814 text_view_child_new_anchored (GtkWidget *child,
8815 GtkTextChildAnchor *anchor,
8816 GtkTextLayout *layout)
8818 GtkTextViewChild *vc;
8820 vc = g_new (GtkTextViewChild, 1);
8822 vc->type = GTK_TEXT_WINDOW_PRIVATE;
8824 vc->anchor = anchor;
8826 vc->from_top_of_line = 0;
8827 vc->from_left_of_buffer = 0;
8829 g_object_ref (vc->widget);
8830 g_object_ref (vc->anchor);
8832 g_object_set_data (G_OBJECT (child),
8833 I_("gtk-text-view-child"),
8836 gtk_text_child_anchor_register_child (anchor, child, layout);
8841 static GtkTextViewChild*
8842 text_view_child_new_window (GtkWidget *child,
8843 GtkTextWindowType type,
8847 GtkTextViewChild *vc;
8849 vc = g_new (GtkTextViewChild, 1);
8854 vc->from_top_of_line = 0;
8855 vc->from_left_of_buffer = 0;
8857 g_object_ref (vc->widget);
8863 g_object_set_data (G_OBJECT (child),
8864 I_("gtk-text-view-child"),
8871 text_view_child_free (GtkTextViewChild *child)
8873 g_object_set_data (G_OBJECT (child->widget),
8874 I_("gtk-text-view-child"), NULL);
8878 gtk_text_child_anchor_unregister_child (child->anchor,
8880 g_object_unref (child->anchor);
8883 g_object_unref (child->widget);
8889 text_view_child_set_parent_window (GtkTextView *text_view,
8890 GtkTextViewChild *vc)
8893 gtk_widget_set_parent_window (vc->widget,
8894 text_view->text_window->bin_window);
8898 window = gtk_text_view_get_window (text_view,
8900 gtk_widget_set_parent_window (vc->widget, window);
8905 add_child (GtkTextView *text_view,
8906 GtkTextViewChild *vc)
8908 text_view->children = g_slist_prepend (text_view->children,
8911 if (GTK_WIDGET_REALIZED (text_view))
8912 text_view_child_set_parent_window (text_view, vc);
8914 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
8918 * gtk_text_view_add_child_at_anchor:
8919 * @text_view: a #GtkTextView
8920 * @child: a #GtkWidget
8921 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
8923 * Adds a child widget in the text buffer, at the given @anchor.
8926 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
8928 GtkTextChildAnchor *anchor)
8930 GtkTextViewChild *vc;
8932 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8933 g_return_if_fail (GTK_IS_WIDGET (child));
8934 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
8935 g_return_if_fail (child->parent == NULL);
8937 gtk_text_view_ensure_layout (text_view);
8939 vc = text_view_child_new_anchored (child, anchor,
8942 add_child (text_view, vc);
8944 g_assert (vc->widget == child);
8945 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8949 * gtk_text_view_add_child_in_window:
8950 * @text_view: a #GtkTextView
8951 * @child: a #GtkWidget
8952 * @which_window: which window the child should appear in
8953 * @xpos: X position of child in window coordinates
8954 * @ypos: Y position of child in window coordinates
8956 * Adds a child at fixed coordinates in one of the text widget's
8957 * windows. The window must have nonzero size (see
8958 * gtk_text_view_set_border_window_size()). Note that the child
8959 * coordinates are given relative to the #GdkWindow in question, and
8960 * that these coordinates have no sane relationship to scrolling. When
8961 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
8962 * irrelevant, the child floats above all scrollable areas. But when
8963 * placing a child in one of the scrollable windows (border windows or
8964 * text window), you'll need to compute the child's correct position
8965 * in buffer coordinates any time scrolling occurs or buffer changes
8966 * occur, and then call gtk_text_view_move_child() to update the
8967 * child's position. Unfortunately there's no good way to detect that
8968 * scrolling has occurred, using the current API; a possible hack
8969 * would be to update all child positions when the scroll adjustments
8970 * change or the text buffer changes. See bug 64518 on
8971 * bugzilla.gnome.org for status of fixing this issue.
8974 gtk_text_view_add_child_in_window (GtkTextView *text_view,
8976 GtkTextWindowType which_window,
8980 GtkTextViewChild *vc;
8982 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8983 g_return_if_fail (GTK_IS_WIDGET (child));
8984 g_return_if_fail (child->parent == NULL);
8986 vc = text_view_child_new_window (child, which_window,
8989 add_child (text_view, vc);
8991 g_assert (vc->widget == child);
8992 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8996 * gtk_text_view_move_child:
8997 * @text_view: a #GtkTextView
8998 * @child: child widget already added to the text view
8999 * @xpos: new X position in window coordinates
9000 * @ypos: new Y position in window coordinates
9002 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
9005 gtk_text_view_move_child (GtkTextView *text_view,
9010 GtkTextViewChild *vc;
9012 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
9013 g_return_if_fail (GTK_IS_WIDGET (child));
9014 g_return_if_fail (child->parent == (GtkWidget*) text_view);
9016 vc = g_object_get_data (G_OBJECT (child),
9017 "gtk-text-view-child");
9019 g_assert (vc != NULL);
9021 if (vc->x == xpos &&
9028 if (gtk_widget_get_visible (child) &&
9029 gtk_widget_get_visible (GTK_WIDGET (text_view)))
9030 gtk_widget_queue_resize (child);
9034 /* Iterator operations */
9037 * gtk_text_view_forward_display_line:
9038 * @text_view: a #GtkTextView
9039 * @iter: a #GtkTextIter
9041 * Moves the given @iter forward by one display (wrapped) line.
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_forward_display_line (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_next_line (text_view->layout, iter);
9065 * gtk_text_view_backward_display_line:
9066 * @text_view: a #GtkTextView
9067 * @iter: a #GtkTextIter
9069 * Moves the given @iter backward by one display (wrapped) line.
9070 * A display line is different from a paragraph. Paragraphs are
9071 * separated by newlines or other paragraph separator characters.
9072 * Display lines are created by line-wrapping a paragraph. If
9073 * wrapping is turned off, display lines and paragraphs will be the
9074 * same. Display lines are divided differently for each view, since
9075 * they depend on the view's width; paragraphs are the same in all
9076 * views, since they depend on the contents of the #GtkTextBuffer.
9078 * Return value: %TRUE if @iter was moved and is not on the end iterator
9081 gtk_text_view_backward_display_line (GtkTextView *text_view,
9084 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9085 g_return_val_if_fail (iter != NULL, FALSE);
9087 gtk_text_view_ensure_layout (text_view);
9089 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
9093 * gtk_text_view_forward_display_line_end:
9094 * @text_view: a #GtkTextView
9095 * @iter: a #GtkTextIter
9097 * Moves the given @iter forward to the next display line end.
9098 * A display line is different from a paragraph. Paragraphs are
9099 * separated by newlines or other paragraph separator characters.
9100 * Display lines are created by line-wrapping a paragraph. If
9101 * wrapping is turned off, display lines and paragraphs will be the
9102 * same. Display lines are divided differently for each view, since
9103 * they depend on the view's width; paragraphs are the same in all
9104 * views, since they depend on the contents of the #GtkTextBuffer.
9106 * Return value: %TRUE if @iter was moved and is not on the end iterator
9109 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
9112 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9113 g_return_val_if_fail (iter != NULL, FALSE);
9115 gtk_text_view_ensure_layout (text_view);
9117 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
9121 * gtk_text_view_backward_display_line_start:
9122 * @text_view: a #GtkTextView
9123 * @iter: a #GtkTextIter
9125 * Moves the given @iter backward to the next display line start.
9126 * A display line is different from a paragraph. Paragraphs are
9127 * separated by newlines or other paragraph separator characters.
9128 * Display lines are created by line-wrapping a paragraph. If
9129 * wrapping is turned off, display lines and paragraphs will be the
9130 * same. Display lines are divided differently for each view, since
9131 * they depend on the view's width; paragraphs are the same in all
9132 * views, since they depend on the contents of the #GtkTextBuffer.
9134 * Return value: %TRUE if @iter was moved and is not on the end iterator
9137 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
9140 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9141 g_return_val_if_fail (iter != NULL, FALSE);
9143 gtk_text_view_ensure_layout (text_view);
9145 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
9149 * gtk_text_view_starts_display_line:
9150 * @text_view: a #GtkTextView
9151 * @iter: a #GtkTextIter
9153 * Determines whether @iter is at the start of a display line.
9154 * See gtk_text_view_forward_display_line() for an explanation of
9155 * display lines vs. paragraphs.
9157 * Return value: %TRUE if @iter begins a wrapped line
9160 gtk_text_view_starts_display_line (GtkTextView *text_view,
9161 const GtkTextIter *iter)
9163 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9164 g_return_val_if_fail (iter != NULL, FALSE);
9166 gtk_text_view_ensure_layout (text_view);
9168 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
9172 * gtk_text_view_move_visually:
9173 * @text_view: a #GtkTextView
9174 * @iter: a #GtkTextIter
9175 * @count: number of characters to move (negative moves left,
9176 * positive moves right)
9178 * Move the iterator a given number of characters visually, treating
9179 * it as the strong cursor position. If @count is positive, then the
9180 * new strong cursor position will be @count positions to the right of
9181 * the old cursor position. If @count is negative then the new strong
9182 * cursor position will be @count positions to the left of the old
9185 * In the presence of bi-directional text, the correspondence
9186 * between logical and visual order will depend on the direction
9187 * of the current run, and there may be jumps when the cursor
9188 * is moved off of the end of a run.
9190 * Return value: %TRUE if @iter moved and is not on the end iterator
9193 gtk_text_view_move_visually (GtkTextView *text_view,
9197 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
9198 g_return_val_if_fail (iter != NULL, FALSE);
9200 gtk_text_view_ensure_layout (text_view);
9202 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
9205 #define __GTK_TEXT_VIEW_C__
9206 #include "gtkaliasdef.c"