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 */
114 struct _GtkTextPendingScroll
117 gdouble within_margin;
125 SET_SCROLL_ADJUSTMENTS,
139 TOGGLE_CURSOR_VISIBLE,
146 PROP_PIXELS_ABOVE_LINES,
147 PROP_PIXELS_BELOW_LINES,
148 PROP_PIXELS_INSIDE_WRAP,
162 static void gtk_text_view_destroy (GtkObject *object);
163 static void gtk_text_view_finalize (GObject *object);
164 static void gtk_text_view_set_property (GObject *object,
168 static void gtk_text_view_get_property (GObject *object,
172 static void gtk_text_view_size_request (GtkWidget *widget,
173 GtkRequisition *requisition);
174 static void gtk_text_view_size_allocate (GtkWidget *widget,
175 GtkAllocation *allocation);
176 static void gtk_text_view_realize (GtkWidget *widget);
177 static void gtk_text_view_unrealize (GtkWidget *widget);
178 static void gtk_text_view_style_set (GtkWidget *widget,
179 GtkStyle *previous_style);
180 static void gtk_text_view_direction_changed (GtkWidget *widget,
181 GtkTextDirection previous_direction);
182 static void gtk_text_view_grab_notify (GtkWidget *widget,
183 gboolean was_grabbed);
184 static void gtk_text_view_state_changed (GtkWidget *widget,
185 GtkStateType previous_state);
187 static gint gtk_text_view_event (GtkWidget *widget,
189 static gint gtk_text_view_key_press_event (GtkWidget *widget,
191 static gint gtk_text_view_key_release_event (GtkWidget *widget,
193 static gint gtk_text_view_button_press_event (GtkWidget *widget,
194 GdkEventButton *event);
195 static gint gtk_text_view_button_release_event (GtkWidget *widget,
196 GdkEventButton *event);
197 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
198 GdkEventFocus *event);
199 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
200 GdkEventFocus *event);
201 static gint gtk_text_view_motion_event (GtkWidget *widget,
202 GdkEventMotion *event);
203 static gint gtk_text_view_expose_event (GtkWidget *widget,
204 GdkEventExpose *expose);
205 static void gtk_text_view_draw_focus (GtkWidget *widget);
206 static gboolean gtk_text_view_focus (GtkWidget *widget,
207 GtkDirectionType direction);
208 static void gtk_text_view_move_focus (GtkWidget *widget,
209 GtkDirectionType direction_type);
210 static void gtk_text_view_select_all (GtkWidget *widget,
214 /* Source side drag signals */
215 static void gtk_text_view_drag_begin (GtkWidget *widget,
216 GdkDragContext *context);
217 static void gtk_text_view_drag_end (GtkWidget *widget,
218 GdkDragContext *context);
219 static void gtk_text_view_drag_data_get (GtkWidget *widget,
220 GdkDragContext *context,
221 GtkSelectionData *selection_data,
224 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
225 GdkDragContext *context);
227 /* Target side drag signals */
228 static void gtk_text_view_drag_leave (GtkWidget *widget,
229 GdkDragContext *context,
231 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
232 GdkDragContext *context,
236 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
237 GdkDragContext *context,
241 static void gtk_text_view_drag_data_received (GtkWidget *widget,
242 GdkDragContext *context,
245 GtkSelectionData *selection_data,
249 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
251 GtkAdjustment *vadj);
252 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
254 static void gtk_text_view_move_cursor (GtkTextView *text_view,
255 GtkMovementStep step,
257 gboolean extend_selection);
258 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
260 gboolean extend_selection);
261 static gboolean gtk_text_view_move_viewport (GtkTextView *text_view,
264 static void gtk_text_view_set_anchor (GtkTextView *text_view);
265 static gboolean gtk_text_view_scroll_pages (GtkTextView *text_view,
267 gboolean extend_selection);
268 static gboolean gtk_text_view_scroll_hpages(GtkTextView *text_view,
270 gboolean extend_selection);
271 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
273 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
276 static void gtk_text_view_backspace (GtkTextView *text_view);
277 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
278 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
279 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
280 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
281 static void gtk_text_view_toggle_cursor_visible (GtkTextView *text_view);
282 static void gtk_text_view_compat_move_focus(GtkTextView *text_view,
283 GtkDirectionType direction_type);
284 static void gtk_text_view_unselect (GtkTextView *text_view);
286 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
287 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
289 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
290 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
291 GtkTextAttributes *values,
293 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
294 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
295 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
296 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
297 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
298 const GtkTextIter *iter,
299 GdkEventButton *event);
300 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view);
301 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
302 const GtkTextIter *iter,
303 GdkEventMotion *event);
304 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
305 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
306 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
307 static void gtk_text_view_reset_blink_time (GtkTextView *text_view);
309 static void gtk_text_view_value_changed (GtkAdjustment *adj,
311 static void gtk_text_view_commit_handler (GtkIMContext *context,
313 GtkTextView *text_view);
314 static void gtk_text_view_commit_text (GtkTextView *text_view,
316 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
317 GtkTextView *text_view);
318 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
319 GtkTextView *text_view);
320 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
323 GtkTextView *text_view);
325 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
326 const GtkTextIter *location,
329 static void gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
330 const GParamSpec *pspec,
332 static void gtk_text_view_get_cursor_location (GtkTextView *text_view,
334 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
337 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
341 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
342 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
344 static void gtk_text_view_do_popup (GtkTextView *text_view,
345 GdkEventButton *event);
347 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
349 gdouble within_margin,
354 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
355 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
356 static void gtk_text_view_invalidate (GtkTextView *text_view);
357 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
359 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
361 /* Container methods */
362 static void gtk_text_view_add (GtkContainer *container,
364 static void gtk_text_view_remove (GtkContainer *container,
366 static void gtk_text_view_forall (GtkContainer *container,
367 gboolean include_internals,
368 GtkCallback callback,
369 gpointer callback_data);
371 /* FIXME probably need the focus methods. */
373 typedef struct _GtkTextViewChild GtkTextViewChild;
375 struct _GtkTextViewChild
379 GtkTextChildAnchor *anchor;
381 gint from_top_of_line;
382 gint from_left_of_buffer;
384 /* These are ignored if anchor != NULL */
385 GtkTextWindowType type;
390 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
391 GtkTextChildAnchor *anchor,
392 GtkTextLayout *layout);
393 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
394 GtkTextWindowType type,
397 static void text_view_child_free (GtkTextViewChild *child);
398 static void text_view_child_set_parent_window (GtkTextView *text_view,
399 GtkTextViewChild *child);
401 struct _GtkTextWindow
403 GtkTextWindowType type;
406 GdkWindow *bin_window;
407 GtkRequisition requisition;
408 GdkRectangle allocation;
411 static GtkTextWindow *text_window_new (GtkTextWindowType type,
414 gint height_request);
415 static void text_window_free (GtkTextWindow *win);
416 static void text_window_realize (GtkTextWindow *win,
418 static void text_window_unrealize (GtkTextWindow *win);
419 static void text_window_size_allocate (GtkTextWindow *win,
421 static void text_window_scroll (GtkTextWindow *win,
424 static void text_window_invalidate_rect (GtkTextWindow *win,
426 static void text_window_invalidate_cursors (GtkTextWindow *win);
428 static gint text_window_get_width (GtkTextWindow *win);
429 static gint text_window_get_height (GtkTextWindow *win);
432 static guint signals[LAST_SIGNAL] = { 0 };
434 G_DEFINE_TYPE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER)
437 add_move_binding (GtkBindingSet *binding_set,
440 GtkMovementStep step,
443 g_assert ((modmask & GDK_SHIFT_MASK) == 0);
445 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
449 G_TYPE_BOOLEAN, FALSE);
451 /* Selection-extending version */
452 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
456 G_TYPE_BOOLEAN, TRUE);
460 gtk_text_view_class_init (GtkTextViewClass *klass)
462 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
463 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
464 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
465 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
466 GtkBindingSet *binding_set;
468 /* Default handlers and virtual methods
470 gobject_class->set_property = gtk_text_view_set_property;
471 gobject_class->get_property = gtk_text_view_get_property;
473 object_class->destroy = gtk_text_view_destroy;
474 gobject_class->finalize = gtk_text_view_finalize;
476 widget_class->realize = gtk_text_view_realize;
477 widget_class->unrealize = gtk_text_view_unrealize;
478 widget_class->style_set = gtk_text_view_style_set;
479 widget_class->direction_changed = gtk_text_view_direction_changed;
480 widget_class->grab_notify = gtk_text_view_grab_notify;
481 widget_class->state_changed = gtk_text_view_state_changed;
482 widget_class->size_request = gtk_text_view_size_request;
483 widget_class->size_allocate = gtk_text_view_size_allocate;
484 widget_class->event = gtk_text_view_event;
485 widget_class->key_press_event = gtk_text_view_key_press_event;
486 widget_class->key_release_event = gtk_text_view_key_release_event;
487 widget_class->button_press_event = gtk_text_view_button_press_event;
488 widget_class->button_release_event = gtk_text_view_button_release_event;
489 widget_class->focus_in_event = gtk_text_view_focus_in_event;
490 widget_class->focus_out_event = gtk_text_view_focus_out_event;
491 widget_class->motion_notify_event = gtk_text_view_motion_event;
492 widget_class->expose_event = gtk_text_view_expose_event;
493 widget_class->focus = gtk_text_view_focus;
495 /* need to override the base class function via override_class_handler,
496 * because the signal slot is not available in GtkWidgetCLass
498 g_signal_override_class_handler ("move-focus",
500 G_CALLBACK (gtk_text_view_move_focus));
502 widget_class->drag_begin = gtk_text_view_drag_begin;
503 widget_class->drag_end = gtk_text_view_drag_end;
504 widget_class->drag_data_get = gtk_text_view_drag_data_get;
505 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
507 widget_class->drag_leave = gtk_text_view_drag_leave;
508 widget_class->drag_motion = gtk_text_view_drag_motion;
509 widget_class->drag_drop = gtk_text_view_drag_drop;
510 widget_class->drag_data_received = gtk_text_view_drag_data_received;
512 widget_class->popup_menu = gtk_text_view_popup_menu;
514 container_class->add = gtk_text_view_add;
515 container_class->remove = gtk_text_view_remove;
516 container_class->forall = gtk_text_view_forall;
518 klass->move_cursor = gtk_text_view_move_cursor;
519 klass->page_horizontally = gtk_text_view_page_horizontally;
520 klass->set_anchor = gtk_text_view_set_anchor;
521 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
522 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
523 klass->backspace = gtk_text_view_backspace;
524 klass->cut_clipboard = gtk_text_view_cut_clipboard;
525 klass->copy_clipboard = gtk_text_view_copy_clipboard;
526 klass->paste_clipboard = gtk_text_view_paste_clipboard;
527 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
528 klass->move_focus = gtk_text_view_compat_move_focus;
529 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
535 g_object_class_install_property (gobject_class,
536 PROP_PIXELS_ABOVE_LINES,
537 g_param_spec_int ("pixels-above-lines",
538 P_("Pixels Above Lines"),
539 P_("Pixels of blank space above paragraphs"),
543 GTK_PARAM_READWRITE));
545 g_object_class_install_property (gobject_class,
546 PROP_PIXELS_BELOW_LINES,
547 g_param_spec_int ("pixels-below-lines",
548 P_("Pixels Below Lines"),
549 P_("Pixels of blank space below paragraphs"),
553 GTK_PARAM_READWRITE));
555 g_object_class_install_property (gobject_class,
556 PROP_PIXELS_INSIDE_WRAP,
557 g_param_spec_int ("pixels-inside-wrap",
558 P_("Pixels Inside Wrap"),
559 P_("Pixels of blank space between wrapped lines in a paragraph"),
563 GTK_PARAM_READWRITE));
565 g_object_class_install_property (gobject_class,
567 g_param_spec_boolean ("editable",
569 P_("Whether the text can be modified by the user"),
571 GTK_PARAM_READWRITE));
573 g_object_class_install_property (gobject_class,
575 g_param_spec_enum ("wrap-mode",
577 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
580 GTK_PARAM_READWRITE));
582 g_object_class_install_property (gobject_class,
584 g_param_spec_enum ("justification",
586 P_("Left, right, or center justification"),
587 GTK_TYPE_JUSTIFICATION,
589 GTK_PARAM_READWRITE));
591 g_object_class_install_property (gobject_class,
593 g_param_spec_int ("left-margin",
595 P_("Width of the left margin in pixels"),
599 GTK_PARAM_READWRITE));
601 g_object_class_install_property (gobject_class,
603 g_param_spec_int ("right-margin",
605 P_("Width of the right margin in pixels"),
609 GTK_PARAM_READWRITE));
611 g_object_class_install_property (gobject_class,
613 g_param_spec_int ("indent",
615 P_("Amount to indent the paragraph, in pixels"),
619 GTK_PARAM_READWRITE));
621 g_object_class_install_property (gobject_class,
623 g_param_spec_boxed ("tabs",
625 P_("Custom tabs for this text"),
626 PANGO_TYPE_TAB_ARRAY,
627 GTK_PARAM_READWRITE));
629 g_object_class_install_property (gobject_class,
631 g_param_spec_boolean ("cursor-visible",
632 P_("Cursor Visible"),
633 P_("If the insertion cursor is shown"),
635 GTK_PARAM_READWRITE));
637 g_object_class_install_property (gobject_class,
639 g_param_spec_object ("buffer",
641 P_("The buffer which is displayed"),
642 GTK_TYPE_TEXT_BUFFER,
643 GTK_PARAM_READWRITE));
645 g_object_class_install_property (gobject_class,
647 g_param_spec_boolean ("overwrite",
648 P_("Overwrite mode"),
649 P_("Whether entered text overwrites existing contents"),
651 GTK_PARAM_READWRITE));
653 g_object_class_install_property (gobject_class,
655 g_param_spec_boolean ("accepts-tab",
657 P_("Whether Tab will result in a tab character being entered"),
659 GTK_PARAM_READWRITE));
664 gtk_widget_class_install_style_property (widget_class,
665 g_param_spec_boxed ("error-underline-color",
666 P_("Error underline color"),
667 P_("Color with which to draw error-indication underlines"),
669 GTK_PARAM_READABLE));
676 * GtkTextView::move-cursor:
677 * @text_view: the object which received the signal
678 * @step: the granularity of the move, as a #GtkMovementStep
679 * @count: the number of @step units to move
680 * @extend_selection: %TRUE if the move should extend the selection
682 * The ::move-cursor signal is a
683 * <link linkend="keybinding-signals">keybinding signal</link>
684 * which gets emitted when the user initiates a cursor movement.
685 * If the cursor is not visible in @text_view, this signal causes
686 * the viewport to be moved instead.
688 * Applications should not connect to it, but may emit it with
689 * g_signal_emit_by_name() if they need to control scrolling
692 * The default bindings for this signal come in two variants,
693 * the variant with the Shift modifier extends the selection,
694 * the variant without the Shift modifer does not.
695 * There are too many key combinations to list them all here.
697 * <listitem>Arrow keys move by individual characters/lines</listitem>
698 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
699 * <listitem>Home/End keys move to the ends of the buffer</listitem>
700 * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
701 * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
704 signals[MOVE_CURSOR] =
705 g_signal_new (I_("move-cursor"),
706 G_OBJECT_CLASS_TYPE (gobject_class),
707 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
708 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
710 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
712 GTK_TYPE_MOVEMENT_STEP,
717 * GtkTextView::page-horizontally:
718 * @text_view: the object which received the signal
719 * @count: the number of @step units to move
720 * @extend_selection: %TRUE if the move should extend the selection
722 * The ::page-horizontally signal is a
723 * <link linkend="keybinding-signals">keybinding signal</link>
724 * which can be bound to key combinations to allow the user
725 * to initiate horizontal cursor movement by pages.
727 * This signal should not be used anymore, instead use the
728 * #GtkTextview::move-cursor signal with the #GTK_MOVEMENT_HORIZONTAL_PAGES
731 signals[PAGE_HORIZONTALLY] =
732 g_signal_new (I_("page-horizontally"),
733 G_OBJECT_CLASS_TYPE (gobject_class),
734 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
735 G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
737 _gtk_marshal_VOID__INT_BOOLEAN,
743 * GtkTextView::move-viewport:
744 * @text_view: the object which received the signal
745 * @step: the granularity of the move, as a #GtkMovementStep
746 * @count: the number of @step units to move
748 * The ::move-viewport signal is a
749 * <link linkend="keybinding-signals">keybinding signal</link>
750 * which can be bound to key combinations to allow the user
751 * to move the viewport, i.e. change what part of the text view
752 * is visible in a containing scrolled window.
754 * There are no default bindings for this signal.
756 signals[MOVE_VIEWPORT] =
757 g_signal_new_class_handler (I_("move-viewport"),
758 G_OBJECT_CLASS_TYPE (gobject_class),
759 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
760 G_CALLBACK (gtk_text_view_move_viewport),
762 _gtk_marshal_VOID__ENUM_INT,
764 GTK_TYPE_SCROLL_STEP,
767 signals[SET_ANCHOR] =
768 g_signal_new (I_("set-anchor"),
769 G_OBJECT_CLASS_TYPE (gobject_class),
770 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
771 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
773 _gtk_marshal_VOID__VOID,
776 signals[INSERT_AT_CURSOR] =
777 g_signal_new (I_("insert-at-cursor"),
778 G_OBJECT_CLASS_TYPE (gobject_class),
779 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
780 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
782 _gtk_marshal_VOID__STRING,
787 * GtkTextView::delete-from-cursor:
788 * @text_view: the object which received the signal
789 * @type: the granularity of the deletion, as a #GtkDeleteType
790 * @count: the number of @type units to delete
792 * The ::delete-from-cursor signal is a
793 * <link linkend="keybinding-signals">keybinding signal</link>
794 * which gets emitted when the user initiates a text deletion.
796 * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
797 * if there is one, otherwise it deletes the requested number
800 * The default bindings for this signal are
801 * Delete for deleting a character, Ctrl-Delete for
802 * deleting a word and Ctrl-Backspace for deleting a word
805 signals[DELETE_FROM_CURSOR] =
806 g_signal_new (I_("delete-from-cursor"),
807 G_OBJECT_CLASS_TYPE (gobject_class),
808 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
809 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
811 _gtk_marshal_VOID__ENUM_INT,
813 GTK_TYPE_DELETE_TYPE,
817 * GtkTextView::backspace:
818 * @text_view: the object which received the signal
820 * The ::backspace signal is a
821 * <link linkend="keybinding-signals">keybinding signal</link>
822 * which gets emitted when the user asks for it.
824 * The default bindings for this signal are
825 * Backspace and Shift-Backspace.
828 g_signal_new (I_("backspace"),
829 G_OBJECT_CLASS_TYPE (gobject_class),
830 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
831 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
833 _gtk_marshal_VOID__VOID,
837 * GtkTextView::cut-clipboard:
838 * @text_view: the object which received the signal
840 * The ::cut-clipboard signal is a
841 * <link linkend="keybinding-signals">keybinding signal</link>
842 * which gets emitted to cut the selection to the clipboard.
844 * The default bindings for this signal are
845 * Ctrl-x and Shift-Delete.
847 signals[CUT_CLIPBOARD] =
848 g_signal_new (I_("cut-clipboard"),
849 G_OBJECT_CLASS_TYPE (gobject_class),
850 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
851 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
853 _gtk_marshal_VOID__VOID,
857 * GtkTextView::copy-clipboard:
858 * @text_view: the object which received the signal
860 * The ::copy-clipboard signal is a
861 * <link linkend="keybinding-signals">keybinding signal</link>
862 * which gets emitted to copy the selection to the clipboard.
864 * The default bindings for this signal are
865 * Ctrl-c and Ctrl-Insert.
867 signals[COPY_CLIPBOARD] =
868 g_signal_new (I_("copy-clipboard"),
869 G_OBJECT_CLASS_TYPE (gobject_class),
870 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
871 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
873 _gtk_marshal_VOID__VOID,
877 * GtkTextView::paste-clipboard:
878 * @text_view: the object which received the signal
880 * The ::paste-clipboard signal is a
881 * <link linkend="keybinding-signals">keybinding signal</link>
882 * which gets emitted to paste the contents of the clipboard
883 * into the text view.
885 * The default bindings for this signal are
886 * Ctrl-v and Shift-Insert.
888 signals[PASTE_CLIPBOARD] =
889 g_signal_new (I_("paste-clipboard"),
890 G_OBJECT_CLASS_TYPE (gobject_class),
891 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
892 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
894 _gtk_marshal_VOID__VOID,
898 * GtkTextView::toggle-overwrite:
899 * @text_view: the object which received the signal
901 * The ::toggle-overwrite signal is a
902 * <link linkend="keybinding-signals">keybinding signal</link>
903 * which gets emitted to change the editability of the text view.
905 * The default bindings for this signal is Insert.
907 signals[TOGGLE_OVERWRITE] =
908 g_signal_new (I_("toggle-overwrite"),
909 G_OBJECT_CLASS_TYPE (gobject_class),
910 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
911 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
913 _gtk_marshal_VOID__VOID,
916 signals[SET_SCROLL_ADJUSTMENTS] =
917 g_signal_new (I_("set-scroll-adjustments"),
918 G_OBJECT_CLASS_TYPE (gobject_class),
919 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
920 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
922 _gtk_marshal_VOID__OBJECT_OBJECT,
925 GTK_TYPE_ADJUSTMENT);
926 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
928 signals[POPULATE_POPUP] =
929 g_signal_new (I_("populate-popup"),
930 G_OBJECT_CLASS_TYPE (gobject_class),
932 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
934 _gtk_marshal_VOID__OBJECT,
939 * GtkTextView::select-all:
940 * @text_view: the object which received the signal
941 * @select: %TRUE to select, %FALSE to unselect
943 * The ::select-all signal is a
944 * <link linkend="keybinding-signals">keybinding signal</link>
945 * which gets emitted to select or unselect the complete
946 * contents of the text view.
948 * The default bindings for this signal are Ctrl-a and Ctrl-/
949 * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
951 signals[SELECT_ALL] =
952 g_signal_new_class_handler (I_("select-all"),
953 G_OBJECT_CLASS_TYPE (object_class),
954 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
955 G_CALLBACK (gtk_text_view_select_all),
957 _gtk_marshal_VOID__BOOLEAN,
958 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
961 * GtkTextView::toggle-cursor-visible:
962 * @text_view: the object which received the signal
964 * The ::toggle-cursor-visible signal is a
965 * <link linkend="keybinding-signals">keybinding signal</link>
966 * which gets emitted to toggle the visibility of the cursor.
968 * The default binding for this signal is F7.
970 signals[TOGGLE_CURSOR_VISIBLE] =
971 g_signal_new_class_handler (I_("toggle-cursor-visible"),
972 G_OBJECT_CLASS_TYPE (object_class),
973 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
974 G_CALLBACK (gtk_text_view_toggle_cursor_visible),
976 _gtk_marshal_VOID__VOID,
983 binding_set = gtk_binding_set_by_class (klass);
985 /* Moving the insertion point */
986 add_move_binding (binding_set, GDK_Right, 0,
987 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
989 add_move_binding (binding_set, GDK_KP_Right, 0,
990 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
992 add_move_binding (binding_set, GDK_Left, 0,
993 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
995 add_move_binding (binding_set, GDK_KP_Left, 0,
996 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
998 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
999 GTK_MOVEMENT_WORDS, 1);
1001 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
1002 GTK_MOVEMENT_WORDS, 1);
1004 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
1005 GTK_MOVEMENT_WORDS, -1);
1007 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
1008 GTK_MOVEMENT_WORDS, -1);
1010 add_move_binding (binding_set, GDK_Up, 0,
1011 GTK_MOVEMENT_DISPLAY_LINES, -1);
1013 add_move_binding (binding_set, GDK_KP_Up, 0,
1014 GTK_MOVEMENT_DISPLAY_LINES, -1);
1016 add_move_binding (binding_set, GDK_Down, 0,
1017 GTK_MOVEMENT_DISPLAY_LINES, 1);
1019 add_move_binding (binding_set, GDK_KP_Down, 0,
1020 GTK_MOVEMENT_DISPLAY_LINES, 1);
1022 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
1023 GTK_MOVEMENT_PARAGRAPHS, -1);
1025 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
1026 GTK_MOVEMENT_PARAGRAPHS, -1);
1028 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
1029 GTK_MOVEMENT_PARAGRAPHS, 1);
1031 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
1032 GTK_MOVEMENT_PARAGRAPHS, 1);
1034 add_move_binding (binding_set, GDK_Home, 0,
1035 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1037 add_move_binding (binding_set, GDK_KP_Home, 0,
1038 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1040 add_move_binding (binding_set, GDK_End, 0,
1041 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1043 add_move_binding (binding_set, GDK_KP_End, 0,
1044 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1046 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
1047 GTK_MOVEMENT_BUFFER_ENDS, -1);
1049 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
1050 GTK_MOVEMENT_BUFFER_ENDS, -1);
1052 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
1053 GTK_MOVEMENT_BUFFER_ENDS, 1);
1055 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
1056 GTK_MOVEMENT_BUFFER_ENDS, 1);
1058 add_move_binding (binding_set, GDK_Page_Up, 0,
1059 GTK_MOVEMENT_PAGES, -1);
1061 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
1062 GTK_MOVEMENT_PAGES, -1);
1064 add_move_binding (binding_set, GDK_Page_Down, 0,
1065 GTK_MOVEMENT_PAGES, 1);
1067 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
1068 GTK_MOVEMENT_PAGES, 1);
1070 add_move_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
1071 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1073 add_move_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
1074 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1076 add_move_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
1077 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1079 add_move_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
1080 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1083 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
1085 G_TYPE_BOOLEAN, TRUE);
1087 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
1089 G_TYPE_BOOLEAN, TRUE);
1092 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
1094 G_TYPE_BOOLEAN, FALSE);
1096 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1098 G_TYPE_BOOLEAN, FALSE);
1101 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
1102 "delete-from-cursor", 2,
1103 G_TYPE_ENUM, GTK_DELETE_CHARS,
1106 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
1107 "delete-from-cursor", 2,
1108 G_TYPE_ENUM, GTK_DELETE_CHARS,
1111 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
1114 /* Make this do the same as Backspace, to help with mis-typing */
1115 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
1118 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
1119 "delete-from-cursor", 2,
1120 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1123 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
1124 "delete-from-cursor", 2,
1125 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1128 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
1129 "delete-from-cursor", 2,
1130 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1133 /* Cut/copy/paste */
1135 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
1136 "cut-clipboard", 0);
1137 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
1138 "copy-clipboard", 0);
1139 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
1140 "paste-clipboard", 0);
1142 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
1143 "cut-clipboard", 0);
1144 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
1145 "copy-clipboard", 0);
1146 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
1147 "paste-clipboard", 0);
1150 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1151 "toggle-overwrite", 0);
1152 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1153 "toggle-overwrite", 0);
1156 gtk_binding_entry_add_signal (binding_set, GDK_F7, 0,
1157 "toggle-cursor-visible", 0);
1159 /* Control-tab focus motion */
1160 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
1162 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1163 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
1165 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1167 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1169 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1170 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1172 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1174 g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1178 gtk_text_view_init (GtkTextView *text_view)
1180 GtkWidget *widget = GTK_WIDGET (text_view);
1181 GtkTargetList *target_list;
1183 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1185 /* Set up default style */
1186 text_view->wrap_mode = GTK_WRAP_NONE;
1187 text_view->pixels_above_lines = 0;
1188 text_view->pixels_below_lines = 0;
1189 text_view->pixels_inside_wrap = 0;
1190 text_view->justify = GTK_JUSTIFY_LEFT;
1191 text_view->left_margin = 0;
1192 text_view->right_margin = 0;
1193 text_view->indent = 0;
1194 text_view->tabs = NULL;
1195 text_view->editable = TRUE;
1197 gtk_drag_dest_set (widget, 0, NULL, 0,
1198 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1200 target_list = gtk_target_list_new (NULL, 0);
1201 gtk_drag_dest_set_target_list (widget, target_list);
1202 gtk_target_list_unref (target_list);
1204 text_view->virtual_cursor_x = -1;
1205 text_view->virtual_cursor_y = -1;
1207 /* This object is completely private. No external entity can gain a reference
1208 * to it; so we create it here and destroy it in finalize ().
1210 text_view->im_context = gtk_im_multicontext_new ();
1212 g_signal_connect (text_view->im_context, "commit",
1213 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1214 g_signal_connect (text_view->im_context, "preedit-changed",
1215 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1216 g_signal_connect (text_view->im_context, "retrieve-surrounding",
1217 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1218 g_signal_connect (text_view->im_context, "delete-surrounding",
1219 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1221 text_view->cursor_visible = TRUE;
1223 text_view->accepts_tab = TRUE;
1225 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1228 text_view->drag_start_x = -1;
1229 text_view->drag_start_y = -1;
1231 text_view->pending_place_cursor_button = 0;
1233 /* We handle all our own redrawing */
1234 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1238 * gtk_text_view_new:
1240 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1241 * before using the text view, an empty default buffer will be created
1242 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1243 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1245 * Return value: a new #GtkTextView
1248 gtk_text_view_new (void)
1250 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1254 * gtk_text_view_new_with_buffer:
1255 * @buffer: a #GtkTextBuffer
1257 * Creates a new #GtkTextView widget displaying the buffer
1258 * @buffer. One buffer can be shared among many widgets.
1259 * @buffer may be %NULL to create a default buffer, in which case
1260 * this function is equivalent to gtk_text_view_new(). The
1261 * text view adds its own reference count to the buffer; it does not
1262 * take over an existing reference.
1264 * Return value: a new #GtkTextView.
1267 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1269 GtkTextView *text_view;
1271 text_view = (GtkTextView*)gtk_text_view_new ();
1273 gtk_text_view_set_buffer (text_view, buffer);
1275 return GTK_WIDGET (text_view);
1279 * gtk_text_view_set_buffer:
1280 * @text_view: a #GtkTextView
1281 * @buffer: a #GtkTextBuffer
1283 * Sets @buffer as the buffer being displayed by @text_view. The previous
1284 * buffer displayed by the text view is unreferenced, and a reference is
1285 * added to @buffer. If you owned a reference to @buffer before passing it
1286 * to this function, you must remove that reference yourself; #GtkTextView
1287 * will not "adopt" it.
1290 gtk_text_view_set_buffer (GtkTextView *text_view,
1291 GtkTextBuffer *buffer)
1293 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1294 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1296 if (text_view->buffer == buffer)
1299 if (text_view->buffer != NULL)
1301 /* Destroy all anchored children */
1305 copy = g_slist_copy (text_view->children);
1307 while (tmp_list != NULL)
1309 GtkTextViewChild *vc = tmp_list->data;
1313 gtk_widget_destroy (vc->widget);
1314 /* vc may now be invalid! */
1317 tmp_list = g_slist_next (tmp_list);
1320 g_slist_free (copy);
1322 g_signal_handlers_disconnect_by_func (text_view->buffer,
1323 gtk_text_view_mark_set_handler,
1325 g_signal_handlers_disconnect_by_func (text_view->buffer,
1326 gtk_text_view_target_list_notify,
1328 g_object_unref (text_view->buffer);
1329 text_view->dnd_mark = NULL;
1330 text_view->first_para_mark = NULL;
1332 if (GTK_WIDGET_REALIZED (text_view))
1334 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1335 GDK_SELECTION_PRIMARY);
1336 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1340 text_view->buffer = buffer;
1346 g_object_ref (buffer);
1348 if (text_view->layout)
1349 gtk_text_layout_set_buffer (text_view->layout, buffer);
1351 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1353 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1357 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1361 text_view->first_para_pixels = 0;
1363 g_signal_connect (text_view->buffer, "mark-set",
1364 G_CALLBACK (gtk_text_view_mark_set_handler),
1366 g_signal_connect (text_view->buffer, "notify::paste-target-list",
1367 G_CALLBACK (gtk_text_view_target_list_notify),
1370 gtk_text_view_target_list_notify (text_view->buffer, NULL, text_view);
1372 if (GTK_WIDGET_REALIZED (text_view))
1374 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1375 GDK_SELECTION_PRIMARY);
1376 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1380 g_object_notify (G_OBJECT (text_view), "buffer");
1382 if (GTK_WIDGET_VISIBLE (text_view))
1383 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1385 DV(g_print ("Invalidating due to set_buffer\n"));
1386 gtk_text_view_invalidate (text_view);
1389 static GtkTextBuffer*
1390 get_buffer (GtkTextView *text_view)
1392 if (text_view->buffer == NULL)
1395 b = gtk_text_buffer_new (NULL);
1396 gtk_text_view_set_buffer (text_view, b);
1400 return text_view->buffer;
1404 * gtk_text_view_get_buffer:
1405 * @text_view: a #GtkTextView
1407 * Returns the #GtkTextBuffer being displayed by this text view.
1408 * The reference count on the buffer is not incremented; the caller
1409 * of this function won't own a new reference.
1411 * Return value: a #GtkTextBuffer
1414 gtk_text_view_get_buffer (GtkTextView *text_view)
1416 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1418 return get_buffer (text_view);
1422 * gtk_text_view_get_iter_at_location:
1423 * @text_view: a #GtkTextView
1424 * @iter: a #GtkTextIter
1425 * @x: x position, in buffer coordinates
1426 * @y: y position, in buffer coordinates
1428 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1429 * coordinates are coordinates for the entire buffer, not just the
1430 * currently-displayed portion. If you have coordinates from an
1431 * event, you have to convert those to buffer coordinates with
1432 * gtk_text_view_window_to_buffer_coords().
1435 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1440 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1441 g_return_if_fail (iter != NULL);
1443 gtk_text_view_ensure_layout (text_view);
1445 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1450 * gtk_text_view_get_iter_at_position:
1451 * @text_view: a #GtkTextView
1452 * @iter: a #GtkTextIter
1453 * @trailing: location to store an integer indicating where
1454 * in the grapheme the user clicked. It will either be
1455 * zero, or the number of characters in the grapheme.
1456 * 0 represents the trailing edge of the grapheme.
1457 * @x: x position, in buffer coordinates
1458 * @y: y position, in buffer coordinates
1460 * Retrieves the iterator pointing to the character at buffer
1461 * coordinates @x and @y. Buffer coordinates are coordinates for
1462 * the entire buffer, not just the currently-displayed portion.
1463 * If you have coordinates from an event, you have to convert
1464 * those to buffer coordinates with
1465 * gtk_text_view_window_to_buffer_coords().
1467 * Note that this is different from gtk_text_view_get_iter_at_location(),
1468 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1474 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1480 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1481 g_return_if_fail (iter != NULL);
1483 gtk_text_view_ensure_layout (text_view);
1485 gtk_text_layout_get_iter_at_position (text_view->layout,
1486 iter, trailing, x, y);
1490 * gtk_text_view_get_iter_location:
1491 * @text_view: a #GtkTextView
1492 * @iter: a #GtkTextIter
1493 * @location: bounds of the character at @iter
1495 * Gets a rectangle which roughly contains the character at @iter.
1496 * The rectangle position is in buffer coordinates; use
1497 * gtk_text_view_buffer_to_window_coords() to convert these
1498 * coordinates to coordinates for one of the windows in the text view.
1501 gtk_text_view_get_iter_location (GtkTextView *text_view,
1502 const GtkTextIter *iter,
1503 GdkRectangle *location)
1505 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1506 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1508 gtk_text_view_ensure_layout (text_view);
1510 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1514 * gtk_text_view_get_line_yrange:
1515 * @text_view: a #GtkTextView
1516 * @iter: a #GtkTextIter
1517 * @y: return location for a y coordinate
1518 * @height: return location for a height
1520 * Gets the y coordinate of the top of the line containing @iter,
1521 * and the height of the line. The coordinate is a buffer coordinate;
1522 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1525 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1526 const GtkTextIter *iter,
1530 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1531 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1533 gtk_text_view_ensure_layout (text_view);
1535 gtk_text_layout_get_line_yrange (text_view->layout,
1542 * gtk_text_view_get_line_at_y:
1543 * @text_view: a #GtkTextView
1544 * @target_iter: a #GtkTextIter
1545 * @y: a y coordinate
1546 * @line_top: return location for top coordinate of the line
1548 * Gets the #GtkTextIter at the start of the line containing
1549 * the coordinate @y. @y is in buffer coordinates, convert from
1550 * window coordinates with gtk_text_view_window_to_buffer_coords().
1551 * If non-%NULL, @line_top will be filled with the coordinate of the top
1555 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1556 GtkTextIter *target_iter,
1560 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1562 gtk_text_view_ensure_layout (text_view);
1564 gtk_text_layout_get_line_at_y (text_view->layout,
1571 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1573 DV (g_print (" Setting adj to raw value %g\n", val));
1575 /* We don't really want to clamp to upper; we want to clamp to
1576 upper - page_size which is the highest value the scrollbar
1577 will let us reach. */
1578 if (val > (adj->upper - adj->page_size))
1579 val = adj->upper - adj->page_size;
1581 if (val < adj->lower)
1584 if (val != adj->value)
1586 DV (g_print (" Setting adj to clamped value %g\n", val));
1587 gtk_adjustment_set_value (adj, val);
1595 * gtk_text_view_scroll_to_iter:
1596 * @text_view: a #GtkTextView
1597 * @iter: a #GtkTextIter
1598 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1599 * @use_align: whether to use alignment arguments (if %FALSE,
1600 * just get the mark onscreen)
1601 * @xalign: horizontal alignment of mark within visible area
1602 * @yalign: vertical alignment of mark within visible area
1604 * Scrolls @text_view so that @iter is on the screen in the position
1605 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1606 * left or top, 1.0 indicates right or bottom, 0.5 means center.
1607 * If @use_align is %FALSE, the text scrolls the minimal distance to
1608 * get the mark onscreen, possibly not scrolling at all. The effective
1609 * screen for purposes of this function is reduced by a margin of size
1612 * Note that this function uses the currently-computed height of the
1613 * lines in the text buffer. Line heights are computed in an idle
1614 * handler; so this function may not have the desired effect if it's
1615 * called before the height computations. To avoid oddness, consider
1616 * using gtk_text_view_scroll_to_mark() which saves a point to be
1617 * scrolled to after line validation.
1619 * Return value: %TRUE if scrolling occurred
1622 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1624 gdouble within_margin,
1630 GdkRectangle screen;
1635 gboolean retval = FALSE;
1637 gint screen_xoffset, screen_yoffset;
1638 gint current_x_scroll, current_y_scroll;
1640 /* FIXME why don't we do the validate-at-scroll-destination thing
1641 * from flush_scroll in this function? I think it wasn't done before
1642 * because changed_handler was screwed up, but I could be wrong.
1645 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1646 g_return_val_if_fail (iter != NULL, FALSE);
1647 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1648 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1649 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1651 widget = GTK_WIDGET (text_view);
1653 DV(g_print(G_STRLOC"\n"));
1655 gtk_text_layout_get_iter_location (text_view->layout,
1659 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1661 current_x_scroll = text_view->xoffset;
1662 current_y_scroll = text_view->yoffset;
1664 screen.x = current_x_scroll;
1665 screen.y = current_y_scroll;
1666 screen.width = SCREEN_WIDTH (widget);
1667 screen.height = SCREEN_HEIGHT (widget);
1669 screen_xoffset = screen.width * within_margin;
1670 screen_yoffset = screen.height * within_margin;
1672 screen.x += screen_xoffset;
1673 screen.y += screen_yoffset;
1674 screen.width -= screen_xoffset * 2;
1675 screen.height -= screen_yoffset * 2;
1677 /* paranoia check */
1678 if (screen.width < 1)
1680 if (screen.height < 1)
1683 /* The -1 here ensures that we leave enough space to draw the cursor
1684 * when this function is used for horizontal scrolling.
1686 screen_right = screen.x + screen.width - 1;
1687 screen_bottom = screen.y + screen.height;
1689 /* The alignment affects the point in the target character that we
1690 * choose to align. If we're doing right/bottom alignment, we align
1691 * the right/bottom edge of the character the mark is at; if we're
1692 * doing left/top we align the left/top edge of the character; if
1693 * we're doing center alignment we align the center of the
1697 /* Vertical scroll */
1700 scroll_dest = current_y_scroll;
1704 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1706 /* if scroll_dest < screen.y, we move a negative increment (up),
1707 * else a positive increment (down)
1709 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1713 /* move minimum to get onscreen */
1714 if (rect.y < screen.y)
1716 scroll_dest = rect.y;
1717 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1719 else if ((rect.y + rect.height) > screen_bottom)
1721 scroll_dest = rect.y + rect.height;
1722 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1726 if (scroll_inc != 0)
1728 retval = set_adjustment_clamped (get_vadjustment (text_view),
1729 current_y_scroll + scroll_inc);
1731 DV (g_print (" vert increment %d\n", scroll_inc));
1734 /* Horizontal scroll */
1737 scroll_dest = current_x_scroll;
1741 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1743 /* if scroll_dest < screen.y, we move a negative increment (left),
1744 * else a positive increment (right)
1746 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1750 /* move minimum to get onscreen */
1751 if (rect.x < screen.x)
1753 scroll_dest = rect.x;
1754 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1756 else if ((rect.x + rect.width) > screen_right)
1758 scroll_dest = rect.x + rect.width;
1759 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1763 if (scroll_inc != 0)
1765 retval = set_adjustment_clamped (get_hadjustment (text_view),
1766 current_x_scroll + scroll_inc);
1768 DV (g_print (" horiz increment %d\n", scroll_inc));
1772 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1774 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1780 free_pending_scroll (GtkTextPendingScroll *scroll)
1782 if (!gtk_text_mark_get_deleted (scroll->mark))
1783 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1785 g_object_unref (scroll->mark);
1790 cancel_pending_scroll (GtkTextView *text_view)
1792 if (text_view->pending_scroll)
1794 free_pending_scroll (text_view->pending_scroll);
1795 text_view->pending_scroll = NULL;
1800 gtk_text_view_queue_scroll (GtkTextView *text_view,
1802 gdouble within_margin,
1808 GtkTextPendingScroll *scroll;
1810 DV(g_print(G_STRLOC"\n"));
1812 scroll = g_new (GtkTextPendingScroll, 1);
1814 scroll->within_margin = within_margin;
1815 scroll->use_align = use_align;
1816 scroll->xalign = xalign;
1817 scroll->yalign = yalign;
1819 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1821 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1824 gtk_text_mark_get_left_gravity (mark));
1826 g_object_ref (scroll->mark);
1828 cancel_pending_scroll (text_view);
1830 text_view->pending_scroll = scroll;
1834 gtk_text_view_flush_scroll (GtkTextView *text_view)
1837 GtkTextPendingScroll *scroll;
1841 widget = GTK_WIDGET (text_view);
1843 DV(g_print(G_STRLOC"\n"));
1845 if (text_view->pending_scroll == NULL)
1847 DV (g_print ("in flush scroll, no pending scroll\n"));
1851 scroll = text_view->pending_scroll;
1853 /* avoid recursion */
1854 text_view->pending_scroll = NULL;
1856 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1858 /* Validate area around the scroll destination, so the adjustment
1859 * can meaningfully point into that area. We must validate
1860 * enough area to be sure that after we scroll, everything onscreen
1861 * is valid; otherwise, validation will maintain the first para
1862 * in one place, but may push the target iter off the bottom of
1865 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1866 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1867 - (widget->allocation.height * 2),
1868 widget->allocation.height * 2);
1870 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1872 /* Ensure we have updated width/height */
1873 gtk_text_view_update_adjustments (text_view);
1875 retval = gtk_text_view_scroll_to_iter (text_view,
1877 scroll->within_margin,
1882 free_pending_scroll (scroll);
1888 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1890 if (upper != adj->upper)
1892 gdouble min = MAX (0.0, upper - adj->page_size);
1893 gboolean value_changed = FALSE;
1897 if (adj->value > min)
1900 value_changed = TRUE;
1903 gtk_adjustment_changed (adj);
1904 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1908 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1909 gtk_adjustment_value_changed (adj);
1915 gtk_text_view_update_adjustments (GtkTextView *text_view)
1917 gint width = 0, height = 0;
1919 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1921 if (text_view->layout)
1922 gtk_text_layout_get_size (text_view->layout, &width, &height);
1924 /* Make room for the cursor after the last character in the widest line */
1925 width += SPACE_FOR_CURSOR;
1927 if (text_view->width != width || text_view->height != height)
1929 if (text_view->width != width)
1930 text_view->width_changed = TRUE;
1932 text_view->width = width;
1933 text_view->height = height;
1935 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1936 MAX (SCREEN_WIDTH (text_view), width));
1937 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1938 MAX (SCREEN_HEIGHT (text_view), height));
1940 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1942 /* Set up the step sizes; we'll say that a page is
1943 our allocation minus one step, and a step is
1944 1/10 of our allocation. */
1945 text_view->hadjustment->step_increment =
1946 SCREEN_WIDTH (text_view) / 10.0;
1947 text_view->hadjustment->page_increment =
1948 SCREEN_WIDTH (text_view) * 0.9;
1950 text_view->vadjustment->step_increment =
1951 SCREEN_HEIGHT (text_view) / 10.0;
1952 text_view->vadjustment->page_increment =
1953 SCREEN_HEIGHT (text_view) * 0.9;
1955 gtk_adjustment_changed (get_hadjustment (text_view));
1956 gtk_adjustment_changed (get_vadjustment (text_view));
1961 gtk_text_view_update_layout_width (GtkTextView *text_view)
1963 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1965 gtk_text_view_ensure_layout (text_view);
1967 gtk_text_layout_set_screen_width (text_view->layout,
1968 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
1972 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1976 if (text_view->layout == NULL)
1979 gtk_text_view_get_cursor_location (text_view, &area);
1981 area.x -= text_view->xoffset;
1982 area.y -= text_view->yoffset;
1984 /* Width returned by Pango indicates direction of cursor,
1985 * by it's sign more than the size of cursor.
1989 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1993 do_update_im_spot_location (gpointer text_view)
1995 GtkTextViewPrivate *priv;
1997 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
1998 priv->im_spot_idle = 0;
2000 gtk_text_view_update_im_spot_location (text_view);
2005 queue_update_im_spot_location (GtkTextView *text_view)
2007 GtkTextViewPrivate *priv;
2009 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2011 /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
2012 * so we don't wait until the entire buffer has been validated. */
2013 if (!priv->im_spot_idle)
2014 priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
2015 do_update_im_spot_location,
2021 flush_update_im_spot_location (GtkTextView *text_view)
2023 GtkTextViewPrivate *priv;
2025 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2027 if (priv->im_spot_idle)
2029 g_source_remove (priv->im_spot_idle);
2030 priv->im_spot_idle = 0;
2031 gtk_text_view_update_im_spot_location (text_view);
2036 * gtk_text_view_scroll_to_mark:
2037 * @text_view: a #GtkTextView
2038 * @mark: a #GtkTextMark
2039 * @within_margin: margin as a [0.0,0.5) fraction of screen size
2040 * @use_align: whether to use alignment arguments (if %FALSE, just
2041 * get the mark onscreen)
2042 * @xalign: horizontal alignment of mark within visible area
2043 * @yalign: vertical alignment of mark within visible area
2045 * Scrolls @text_view so that @mark is on the screen in the position
2046 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
2047 * left or top, 1.0 indicates right or bottom, 0.5 means center.
2048 * If @use_align is %FALSE, the text scrolls the minimal distance to
2049 * get the mark onscreen, possibly not scrolling at all. The effective
2050 * screen for purposes of this function is reduced by a margin of size
2054 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
2056 gdouble within_margin,
2061 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2062 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2063 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
2064 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
2065 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
2067 gtk_text_view_queue_scroll (text_view, mark,
2073 /* If no validation is pending, we need to go ahead and force an
2076 if (text_view->layout &&
2077 gtk_text_layout_is_valid (text_view->layout))
2078 gtk_text_view_flush_scroll (text_view);
2082 * gtk_text_view_scroll_mark_onscreen:
2083 * @text_view: a #GtkTextView
2084 * @mark: a mark in the buffer for @text_view
2086 * Scrolls @text_view the minimum distance such that @mark is contained
2087 * within the visible area of the widget.
2090 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
2093 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2094 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2096 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
2100 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
2102 GdkRectangle visible_rect;
2103 gtk_text_view_get_visible_rect (text_view, &visible_rect);
2105 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
2107 visible_rect.y + visible_rect.height);
2111 * gtk_text_view_move_mark_onscreen:
2112 * @text_view: a #GtkTextView
2113 * @mark: a #GtkTextMark
2115 * Moves a mark within the buffer so that it's
2116 * located within the currently-visible text area.
2118 * Return value: %TRUE if the mark moved (wasn't already onscreen)
2121 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
2126 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2127 g_return_val_if_fail (mark != NULL, FALSE);
2129 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2131 if (clamp_iter_onscreen (text_view, &iter))
2133 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
2141 * gtk_text_view_get_visible_rect:
2142 * @text_view: a #GtkTextView
2143 * @visible_rect: rectangle to fill
2145 * Fills @visible_rect with the currently-visible
2146 * region of the buffer, in buffer coordinates. Convert to window coordinates
2147 * with gtk_text_view_buffer_to_window_coords().
2150 gtk_text_view_get_visible_rect (GtkTextView *text_view,
2151 GdkRectangle *visible_rect)
2155 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2157 widget = GTK_WIDGET (text_view);
2161 visible_rect->x = text_view->xoffset;
2162 visible_rect->y = text_view->yoffset;
2163 visible_rect->width = SCREEN_WIDTH (widget);
2164 visible_rect->height = SCREEN_HEIGHT (widget);
2166 DV(g_print(" visible rect: %d,%d %d x %d\n",
2169 visible_rect->width,
2170 visible_rect->height));
2175 * gtk_text_view_set_wrap_mode:
2176 * @text_view: a #GtkTextView
2177 * @wrap_mode: a #GtkWrapMode
2179 * Sets the line wrapping for the view.
2182 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
2183 GtkWrapMode wrap_mode)
2185 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2187 if (text_view->wrap_mode != wrap_mode)
2189 text_view->wrap_mode = wrap_mode;
2191 if (text_view->layout)
2193 text_view->layout->default_style->wrap_mode = wrap_mode;
2194 gtk_text_layout_default_style_changed (text_view->layout);
2198 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2202 * gtk_text_view_get_wrap_mode:
2203 * @text_view: a #GtkTextView
2205 * Gets the line wrapping for the view.
2207 * Return value: the line wrap setting
2210 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2212 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2214 return text_view->wrap_mode;
2218 * gtk_text_view_set_editable:
2219 * @text_view: a #GtkTextView
2220 * @setting: whether it's editable
2222 * Sets the default editability of the #GtkTextView. You can override
2223 * this default setting with tags in the buffer, using the "editable"
2224 * attribute of tags.
2227 gtk_text_view_set_editable (GtkTextView *text_view,
2230 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2231 setting = setting != FALSE;
2233 if (text_view->editable != setting)
2237 gtk_text_view_reset_im_context(text_view);
2238 if (GTK_WIDGET_HAS_FOCUS (text_view))
2239 gtk_im_context_focus_out (text_view->im_context);
2242 text_view->editable = setting;
2244 if (setting && GTK_WIDGET_HAS_FOCUS (text_view))
2245 gtk_im_context_focus_in (text_view->im_context);
2247 if (text_view->layout)
2249 gtk_text_layout_set_overwrite_mode (text_view->layout,
2250 text_view->overwrite_mode && text_view->editable);
2251 text_view->layout->default_style->editable = text_view->editable;
2252 gtk_text_layout_default_style_changed (text_view->layout);
2255 g_object_notify (G_OBJECT (text_view), "editable");
2260 * gtk_text_view_get_editable:
2261 * @text_view: a #GtkTextView
2263 * Returns the default editability of the #GtkTextView. Tags in the
2264 * buffer may override this setting for some ranges of text.
2266 * Return value: whether text is editable by default
2269 gtk_text_view_get_editable (GtkTextView *text_view)
2271 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2273 return text_view->editable;
2277 * gtk_text_view_set_pixels_above_lines:
2278 * @text_view: a #GtkTextView
2279 * @pixels_above_lines: pixels above paragraphs
2281 * Sets the default number of blank pixels above paragraphs in @text_view.
2282 * Tags in the buffer for @text_view may override the defaults.
2285 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2286 gint pixels_above_lines)
2288 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2290 if (text_view->pixels_above_lines != pixels_above_lines)
2292 text_view->pixels_above_lines = pixels_above_lines;
2294 if (text_view->layout)
2296 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
2297 gtk_text_layout_default_style_changed (text_view->layout);
2300 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2305 * gtk_text_view_get_pixels_above_lines:
2306 * @text_view: a #GtkTextView
2308 * Gets the default number of pixels to put above paragraphs.
2310 * Return value: default number of pixels above paragraphs
2313 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2315 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2317 return text_view->pixels_above_lines;
2321 * gtk_text_view_set_pixels_below_lines:
2322 * @text_view: a #GtkTextView
2323 * @pixels_below_lines: pixels below paragraphs
2325 * Sets the default number of pixels of blank space
2326 * to put below paragraphs in @text_view. May be overridden
2327 * by tags applied to @text_view's buffer.
2330 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2331 gint pixels_below_lines)
2333 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2335 if (text_view->pixels_below_lines != pixels_below_lines)
2337 text_view->pixels_below_lines = pixels_below_lines;
2339 if (text_view->layout)
2341 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2342 gtk_text_layout_default_style_changed (text_view->layout);
2345 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2350 * gtk_text_view_get_pixels_below_lines:
2351 * @text_view: a #GtkTextView
2353 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2355 * Return value: default number of blank pixels below paragraphs
2358 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2360 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2362 return text_view->pixels_below_lines;
2366 * gtk_text_view_set_pixels_inside_wrap:
2367 * @text_view: a #GtkTextView
2368 * @pixels_inside_wrap: default number of pixels between wrapped lines
2370 * Sets the default number of pixels of blank space to leave between
2371 * display/wrapped lines within a paragraph. May be overridden by
2372 * tags in @text_view's buffer.
2375 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2376 gint pixels_inside_wrap)
2378 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2380 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2382 text_view->pixels_inside_wrap = pixels_inside_wrap;
2384 if (text_view->layout)
2386 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2387 gtk_text_layout_default_style_changed (text_view->layout);
2390 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2395 * gtk_text_view_get_pixels_inside_wrap:
2396 * @text_view: a #GtkTextView
2398 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2400 * Return value: default number of pixels of blank space between wrapped lines
2403 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2405 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2407 return text_view->pixels_inside_wrap;
2411 * gtk_text_view_set_justification:
2412 * @text_view: a #GtkTextView
2413 * @justification: justification
2415 * Sets the default justification of text in @text_view.
2416 * Tags in the view's buffer may override the default.
2420 gtk_text_view_set_justification (GtkTextView *text_view,
2421 GtkJustification justification)
2423 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2425 if (text_view->justify != justification)
2427 text_view->justify = justification;
2429 if (text_view->layout)
2431 text_view->layout->default_style->justification = justification;
2432 gtk_text_layout_default_style_changed (text_view->layout);
2435 g_object_notify (G_OBJECT (text_view), "justification");
2440 * gtk_text_view_get_justification:
2441 * @text_view: a #GtkTextView
2443 * Gets the default justification of paragraphs in @text_view.
2444 * Tags in the buffer may override the default.
2446 * Return value: default justification
2449 gtk_text_view_get_justification (GtkTextView *text_view)
2451 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2453 return text_view->justify;
2457 * gtk_text_view_set_left_margin:
2458 * @text_view: a #GtkTextView
2459 * @left_margin: left margin in pixels
2461 * Sets the default left margin for text in @text_view.
2462 * Tags in the buffer may override the default.
2465 gtk_text_view_set_left_margin (GtkTextView *text_view,
2468 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2470 if (text_view->left_margin != left_margin)
2472 text_view->left_margin = left_margin;
2474 if (text_view->layout)
2476 text_view->layout->default_style->left_margin = left_margin;
2477 gtk_text_layout_default_style_changed (text_view->layout);
2480 g_object_notify (G_OBJECT (text_view), "left-margin");
2485 * gtk_text_view_get_left_margin:
2486 * @text_view: a #GtkTextView
2488 * Gets the default left margin size of paragraphs in the @text_view.
2489 * Tags in the buffer may override the default.
2491 * Return value: left margin in pixels
2494 gtk_text_view_get_left_margin (GtkTextView *text_view)
2496 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2498 return text_view->left_margin;
2502 * gtk_text_view_set_right_margin:
2503 * @text_view: a #GtkTextView
2504 * @right_margin: right margin in pixels
2506 * Sets the default right margin for text in the text view.
2507 * Tags in the buffer may override the default.
2510 gtk_text_view_set_right_margin (GtkTextView *text_view,
2513 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2515 if (text_view->right_margin != right_margin)
2517 text_view->right_margin = right_margin;
2519 if (text_view->layout)
2521 text_view->layout->default_style->right_margin = right_margin;
2522 gtk_text_layout_default_style_changed (text_view->layout);
2525 g_object_notify (G_OBJECT (text_view), "right-margin");
2530 * gtk_text_view_get_right_margin:
2531 * @text_view: a #GtkTextView
2533 * Gets the default right margin for text in @text_view. Tags
2534 * in the buffer may override the default.
2536 * Return value: right margin in pixels
2539 gtk_text_view_get_right_margin (GtkTextView *text_view)
2541 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2543 return text_view->right_margin;
2547 * gtk_text_view_set_indent:
2548 * @text_view: a #GtkTextView
2549 * @indent: indentation in pixels
2551 * Sets the default indentation for paragraphs in @text_view.
2552 * Tags in the buffer may override the default.
2555 gtk_text_view_set_indent (GtkTextView *text_view,
2558 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2560 if (text_view->indent != indent)
2562 text_view->indent = indent;
2564 if (text_view->layout)
2566 text_view->layout->default_style->indent = indent;
2567 gtk_text_layout_default_style_changed (text_view->layout);
2570 g_object_notify (G_OBJECT (text_view), "indent");
2575 * gtk_text_view_get_indent:
2576 * @text_view: a #GtkTextView
2578 * Gets the default indentation of paragraphs in @text_view.
2579 * Tags in the view's buffer may override the default.
2580 * The indentation may be negative.
2582 * Return value: number of pixels of indentation
2585 gtk_text_view_get_indent (GtkTextView *text_view)
2587 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2589 return text_view->indent;
2593 * gtk_text_view_set_tabs:
2594 * @text_view: a #GtkTextView
2595 * @tabs: tabs as a #PangoTabArray
2597 * Sets the default tab stops for paragraphs in @text_view.
2598 * Tags in the buffer may override the default.
2601 gtk_text_view_set_tabs (GtkTextView *text_view,
2602 PangoTabArray *tabs)
2604 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2606 if (text_view->tabs)
2607 pango_tab_array_free (text_view->tabs);
2609 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2611 if (text_view->layout)
2613 /* some unkosher futzing in internal struct details... */
2614 if (text_view->layout->default_style->tabs)
2615 pango_tab_array_free (text_view->layout->default_style->tabs);
2617 text_view->layout->default_style->tabs =
2618 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2620 gtk_text_layout_default_style_changed (text_view->layout);
2623 g_object_notify (G_OBJECT (text_view), "tabs");
2627 * gtk_text_view_get_tabs:
2628 * @text_view: a #GtkTextView
2630 * Gets the default tabs for @text_view. Tags in the buffer may
2631 * override the defaults. The returned array will be %NULL if
2632 * "standard" (8-space) tabs are used. Free the return value
2633 * with pango_tab_array_free().
2635 * Return value: copy of default tab array, or %NULL if "standard"
2636 * tabs are used; must be freed with pango_tab_array_free().
2639 gtk_text_view_get_tabs (GtkTextView *text_view)
2641 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2643 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2647 gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
2649 gtk_text_view_set_cursor_visible (text_view, !text_view->cursor_visible);
2653 * gtk_text_view_set_cursor_visible:
2654 * @text_view: a #GtkTextView
2655 * @setting: whether to show the insertion cursor
2657 * Toggles whether the insertion point is displayed. A buffer with no editable
2658 * text probably shouldn't have a visible cursor, so you may want to turn
2662 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2665 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2667 setting = (setting != FALSE);
2669 if (text_view->cursor_visible != setting)
2671 text_view->cursor_visible = setting;
2673 if (GTK_WIDGET_HAS_FOCUS (text_view))
2675 if (text_view->layout)
2677 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2678 gtk_text_view_check_cursor_blink (text_view);
2682 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2687 * gtk_text_view_get_cursor_visible:
2688 * @text_view: a #GtkTextView
2690 * Find out whether the cursor is being displayed.
2692 * Return value: whether the insertion mark is visible
2695 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2697 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2699 return text_view->cursor_visible;
2704 * gtk_text_view_place_cursor_onscreen:
2705 * @text_view: a #GtkTextView
2707 * Moves the cursor to the currently visible region of the
2708 * buffer, it it isn't there already.
2710 * Return value: %TRUE if the cursor had to be moved.
2713 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2717 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2719 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2720 gtk_text_buffer_get_insert (get_buffer (text_view)));
2722 if (clamp_iter_onscreen (text_view, &insert))
2724 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2732 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2734 if (text_view->first_validate_idle != 0)
2736 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2737 g_source_remove (text_view->first_validate_idle);
2738 text_view->first_validate_idle = 0;
2741 if (text_view->incremental_validate_idle != 0)
2743 g_source_remove (text_view->incremental_validate_idle);
2744 text_view->incremental_validate_idle = 0;
2749 gtk_text_view_destroy (GtkObject *object)
2751 GtkTextView *text_view;
2752 GtkTextViewPrivate *priv;
2754 text_view = GTK_TEXT_VIEW (object);
2755 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2757 gtk_text_view_remove_validate_idles (text_view);
2758 gtk_text_view_set_buffer (text_view, NULL);
2759 gtk_text_view_destroy_layout (text_view);
2761 if (text_view->scroll_timeout)
2763 g_source_remove (text_view->scroll_timeout);
2764 text_view->scroll_timeout = 0;
2767 if (priv->im_spot_idle)
2769 g_source_remove (priv->im_spot_idle);
2770 priv->im_spot_idle = 0;
2773 GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy (object);
2777 gtk_text_view_finalize (GObject *object)
2779 GtkTextView *text_view;
2781 text_view = GTK_TEXT_VIEW (object);
2783 g_assert (text_view->buffer == NULL);
2785 gtk_text_view_destroy_layout (text_view);
2786 gtk_text_view_set_buffer (text_view, NULL);
2788 cancel_pending_scroll (text_view);
2790 if (text_view->tabs)
2791 pango_tab_array_free (text_view->tabs);
2793 if (text_view->hadjustment)
2794 g_object_unref (text_view->hadjustment);
2795 if (text_view->vadjustment)
2796 g_object_unref (text_view->vadjustment);
2798 text_window_free (text_view->text_window);
2800 if (text_view->left_window)
2801 text_window_free (text_view->left_window);
2803 if (text_view->top_window)
2804 text_window_free (text_view->top_window);
2806 if (text_view->right_window)
2807 text_window_free (text_view->right_window);
2809 if (text_view->bottom_window)
2810 text_window_free (text_view->bottom_window);
2812 g_object_unref (text_view->im_context);
2814 G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize (object);
2818 gtk_text_view_set_property (GObject *object,
2820 const GValue *value,
2823 GtkTextView *text_view;
2825 text_view = GTK_TEXT_VIEW (object);
2829 case PROP_PIXELS_ABOVE_LINES:
2830 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2833 case PROP_PIXELS_BELOW_LINES:
2834 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2837 case PROP_PIXELS_INSIDE_WRAP:
2838 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2842 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2845 case PROP_WRAP_MODE:
2846 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2849 case PROP_JUSTIFICATION:
2850 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2853 case PROP_LEFT_MARGIN:
2854 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2857 case PROP_RIGHT_MARGIN:
2858 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2862 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2866 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2869 case PROP_CURSOR_VISIBLE:
2870 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2873 case PROP_OVERWRITE:
2874 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
2878 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
2881 case PROP_ACCEPTS_TAB:
2882 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
2886 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2892 gtk_text_view_get_property (GObject *object,
2897 GtkTextView *text_view;
2899 text_view = GTK_TEXT_VIEW (object);
2903 case PROP_PIXELS_ABOVE_LINES:
2904 g_value_set_int (value, text_view->pixels_above_lines);
2907 case PROP_PIXELS_BELOW_LINES:
2908 g_value_set_int (value, text_view->pixels_below_lines);
2911 case PROP_PIXELS_INSIDE_WRAP:
2912 g_value_set_int (value, text_view->pixels_inside_wrap);
2916 g_value_set_boolean (value, text_view->editable);
2919 case PROP_WRAP_MODE:
2920 g_value_set_enum (value, text_view->wrap_mode);
2923 case PROP_JUSTIFICATION:
2924 g_value_set_enum (value, text_view->justify);
2927 case PROP_LEFT_MARGIN:
2928 g_value_set_int (value, text_view->left_margin);
2931 case PROP_RIGHT_MARGIN:
2932 g_value_set_int (value, text_view->right_margin);
2936 g_value_set_int (value, text_view->indent);
2940 g_value_set_boxed (value, text_view->tabs);
2943 case PROP_CURSOR_VISIBLE:
2944 g_value_set_boolean (value, text_view->cursor_visible);
2948 g_value_set_object (value, get_buffer (text_view));
2951 case PROP_OVERWRITE:
2952 g_value_set_boolean (value, text_view->overwrite_mode);
2955 case PROP_ACCEPTS_TAB:
2956 g_value_set_boolean (value, text_view->accepts_tab);
2960 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2966 gtk_text_view_size_request (GtkWidget *widget,
2967 GtkRequisition *requisition)
2969 GtkTextView *text_view;
2971 gint focus_edge_width;
2973 gboolean interior_focus;
2975 text_view = GTK_TEXT_VIEW (widget);
2977 gtk_widget_style_get (widget,
2978 "interior-focus", &interior_focus,
2979 "focus-line-width", &focus_width,
2983 focus_edge_width = 0;
2985 focus_edge_width = focus_width;
2987 if (text_view->layout)
2989 text_view->text_window->requisition.width = text_view->layout->width;
2990 text_view->text_window->requisition.height = text_view->layout->height;
2994 text_view->text_window->requisition.width = 0;
2995 text_view->text_window->requisition.height = 0;
2998 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2999 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
3001 if (text_view->left_window)
3002 requisition->width += text_view->left_window->requisition.width;
3004 if (text_view->right_window)
3005 requisition->width += text_view->right_window->requisition.width;
3007 if (text_view->top_window)
3008 requisition->height += text_view->top_window->requisition.height;
3010 if (text_view->bottom_window)
3011 requisition->height += text_view->bottom_window->requisition.height;
3013 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
3014 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
3016 tmp_list = text_view->children;
3017 while (tmp_list != NULL)
3019 GtkTextViewChild *child = tmp_list->data;
3023 GtkRequisition child_req;
3024 GtkRequisition old_req;
3026 gtk_widget_get_child_requisition (child->widget, &old_req);
3028 gtk_widget_size_request (child->widget, &child_req);
3030 gtk_widget_get_child_requisition (child->widget, &child_req);
3032 /* Invalidate layout lines if required */
3033 if (text_view->layout &&
3034 (old_req.width != child_req.width ||
3035 old_req.height != child_req.height))
3036 gtk_text_child_anchor_queue_resize (child->anchor,
3041 GtkRequisition child_req;
3043 gtk_widget_size_request (child->widget, &child_req);
3046 tmp_list = g_slist_next (tmp_list);
3051 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
3052 GtkTextViewChild *vc,
3053 GtkAllocation *allocation)
3059 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3063 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
3066 buffer_y += vc->from_top_of_line;
3068 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
3069 allocation->y = buffer_y - text_view->yoffset;
3071 gtk_widget_get_child_requisition (vc->widget, &req);
3072 allocation->width = req.width;
3073 allocation->height = req.height;
3077 gtk_text_view_update_child_allocation (GtkTextView *text_view,
3078 GtkTextViewChild *vc)
3080 GtkAllocation allocation;
3082 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
3084 gtk_widget_size_allocate (vc->widget, &allocation);
3087 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
3089 vc->widget->allocation.x,
3090 vc->widget->allocation.y,
3091 text_view->yoffset);
3096 gtk_text_view_child_allocated (GtkTextLayout *layout,
3102 GtkTextViewChild *vc = NULL;
3103 GtkTextView *text_view = data;
3105 /* x,y is the position of the child from the top of the line, and
3106 * from the left of the buffer. We have to translate that into text
3107 * window coordinates, then size_allocate the child.
3110 vc = g_object_get_data (G_OBJECT (child),
3111 "gtk-text-view-child");
3113 g_assert (vc != NULL);
3115 DV (g_print ("child allocated at %d,%d\n", x, y));
3117 vc->from_left_of_buffer = x;
3118 vc->from_top_of_line = y;
3120 gtk_text_view_update_child_allocation (text_view, vc);
3124 gtk_text_view_allocate_children (GtkTextView *text_view)
3128 DV(g_print(G_STRLOC"\n"));
3130 tmp_list = text_view->children;
3131 while (tmp_list != NULL)
3133 GtkTextViewChild *child = tmp_list->data;
3135 g_assert (child != NULL);
3139 /* We need to force-validate the regions containing
3142 GtkTextIter child_loc;
3143 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3147 /* Since anchored children are only ever allocated from
3148 * gtk_text_layout_get_line_display() we have to make sure
3149 * that the display line caching in the layout doesn't
3150 * get in the way. Invalidating the layout around the anchor
3153 if (GTK_WIDGET_ALLOC_NEEDED (child->widget))
3155 GtkTextIter end = child_loc;
3156 gtk_text_iter_forward_char (&end);
3157 gtk_text_layout_invalidate (text_view->layout, &child_loc, &end);
3160 gtk_text_layout_validate_yrange (text_view->layout,
3166 GtkAllocation allocation;
3167 GtkRequisition child_req;
3169 allocation.x = child->x;
3170 allocation.y = child->y;
3172 gtk_widget_get_child_requisition (child->widget, &child_req);
3174 allocation.width = child_req.width;
3175 allocation.height = child_req.height;
3177 gtk_widget_size_allocate (child->widget, &allocation);
3180 tmp_list = g_slist_next (tmp_list);
3185 gtk_text_view_size_allocate (GtkWidget *widget,
3186 GtkAllocation *allocation)
3188 GtkTextView *text_view;
3189 GtkTextIter first_para;
3192 GdkRectangle text_rect;
3193 GdkRectangle left_rect;
3194 GdkRectangle right_rect;
3195 GdkRectangle top_rect;
3196 GdkRectangle bottom_rect;
3197 gint focus_edge_width;
3199 gboolean interior_focus;
3200 gboolean size_changed;
3202 text_view = GTK_TEXT_VIEW (widget);
3204 DV(g_print(G_STRLOC"\n"));
3207 widget->allocation.width != allocation->width ||
3208 widget->allocation.height != allocation->height;
3210 widget->allocation = *allocation;
3212 if (GTK_WIDGET_REALIZED (widget))
3214 gdk_window_move_resize (widget->window,
3215 allocation->x, allocation->y,
3216 allocation->width, allocation->height);
3219 /* distribute width/height among child windows. Ensure all
3220 * windows get at least a 1x1 allocation.
3223 gtk_widget_style_get (widget,
3224 "interior-focus", &interior_focus,
3225 "focus-line-width", &focus_width,
3229 focus_edge_width = 0;
3231 focus_edge_width = focus_width;
3233 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3235 if (text_view->left_window)
3236 left_rect.width = text_view->left_window->requisition.width;
3238 left_rect.width = 0;
3240 width -= left_rect.width;
3242 if (text_view->right_window)
3243 right_rect.width = text_view->right_window->requisition.width;
3245 right_rect.width = 0;
3247 width -= right_rect.width;
3249 text_rect.width = MAX (1, width);
3251 top_rect.width = text_rect.width;
3252 bottom_rect.width = text_rect.width;
3255 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3257 if (text_view->top_window)
3258 top_rect.height = text_view->top_window->requisition.height;
3260 top_rect.height = 0;
3262 height -= top_rect.height;
3264 if (text_view->bottom_window)
3265 bottom_rect.height = text_view->bottom_window->requisition.height;
3267 bottom_rect.height = 0;
3269 height -= bottom_rect.height;
3271 text_rect.height = MAX (1, height);
3273 left_rect.height = text_rect.height;
3274 right_rect.height = text_rect.height;
3277 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3278 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3280 text_rect.x = left_rect.x + left_rect.width;
3281 text_rect.y = top_rect.y + top_rect.height;
3283 left_rect.y = text_rect.y;
3284 right_rect.y = text_rect.y;
3286 top_rect.x = text_rect.x;
3287 bottom_rect.x = text_rect.x;
3289 right_rect.x = text_rect.x + text_rect.width;
3290 bottom_rect.y = text_rect.y + text_rect.height;
3292 text_window_size_allocate (text_view->text_window,
3295 if (text_view->left_window)
3296 text_window_size_allocate (text_view->left_window,
3299 if (text_view->right_window)
3300 text_window_size_allocate (text_view->right_window,
3303 if (text_view->top_window)
3304 text_window_size_allocate (text_view->top_window,
3307 if (text_view->bottom_window)
3308 text_window_size_allocate (text_view->bottom_window,
3311 gtk_text_view_update_layout_width (text_view);
3313 /* Note that this will do some layout validation */
3314 gtk_text_view_allocate_children (text_view);
3316 /* Ensure h/v adj exist */
3317 get_hadjustment (text_view);
3318 get_vadjustment (text_view);
3320 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
3321 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
3322 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
3323 text_view->hadjustment->lower = 0;
3324 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
3327 if (text_view->hadjustment->value > text_view->hadjustment->upper - text_view->hadjustment->page_size)
3328 gtk_adjustment_set_value (text_view->hadjustment, MAX (0, text_view->hadjustment->upper - text_view->hadjustment->page_size));
3330 gtk_adjustment_changed (text_view->hadjustment);
3332 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
3333 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
3334 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
3335 text_view->vadjustment->lower = 0;
3336 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
3339 /* Now adjust the value of the adjustment to keep the cursor at the
3340 * same place in the buffer
3342 gtk_text_view_get_first_para_iter (text_view, &first_para);
3343 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
3345 y += text_view->first_para_pixels;
3347 if (y > text_view->vadjustment->upper - text_view->vadjustment->page_size)
3348 y = MAX (0, text_view->vadjustment->upper - text_view->vadjustment->page_size);
3350 if (y != text_view->yoffset)
3351 gtk_adjustment_set_value (text_view->vadjustment, y);
3353 gtk_adjustment_changed (text_view->vadjustment);
3355 /* The GTK resize loop processes all the pending exposes right
3356 * after doing the resize stuff, so the idle sizer won't have a
3357 * chance to run. So we do the work here.
3359 gtk_text_view_flush_first_validate (text_view);
3361 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3364 if (size_changed && GTK_WIDGET_REALIZED (widget))
3365 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3369 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3372 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3373 text_view->first_para_mark);
3377 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3379 GtkWidget *widget = GTK_WIDGET (text_view);
3381 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3383 if (SCREEN_HEIGHT (widget) > 0)
3385 GtkTextIter first_para;
3387 /* Be sure we've validated the stuff onscreen; if we
3388 * scrolled, these calls won't have any effect, because
3389 * they were called in the recursive validate_onscreen
3391 gtk_text_view_get_first_para_iter (text_view, &first_para);
3393 gtk_text_layout_validate_yrange (text_view->layout,
3396 text_view->first_para_pixels +
3397 SCREEN_HEIGHT (widget));
3400 text_view->onscreen_validated = TRUE;
3402 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3404 /* This can have the odd side effect of triggering a scroll, which should
3405 * flip "onscreen_validated" back to FALSE, but should also get us
3406 * back into this function to turn it on again.
3408 gtk_text_view_update_adjustments (text_view);
3410 g_assert (text_view->onscreen_validated);
3414 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3416 if (text_view->first_validate_idle == 0)
3419 /* Do this first, which means that if an "invalidate"
3420 * occurs during any of this process, a new first_validate_callback
3421 * will be installed, and we'll start again.
3423 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3424 g_source_remove (text_view->first_validate_idle);
3425 text_view->first_validate_idle = 0;
3427 /* be sure we have up-to-date screen size set on the
3430 gtk_text_view_update_layout_width (text_view);
3432 /* Bail out if we invalidated stuff; scrolling right away will just
3433 * confuse the issue.
3435 if (text_view->first_validate_idle != 0)
3437 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3441 /* scroll to any marks, if that's pending. This can jump us to
3442 * the validation codepath used for scrolling onscreen, if so we
3443 * bail out. It won't jump if already in that codepath since
3444 * value_changed is not recursive, so also validate if
3447 if (!gtk_text_view_flush_scroll (text_view) ||
3448 !text_view->onscreen_validated)
3449 gtk_text_view_validate_onscreen (text_view);
3451 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3453 g_assert (text_view->onscreen_validated);
3458 first_validate_callback (gpointer data)
3460 GtkTextView *text_view = data;
3462 /* Note that some of this code is duplicated at the end of size_allocate,
3463 * keep in sync with that.
3466 DV(g_print(G_STRLOC"\n"));
3468 gtk_text_view_flush_first_validate (text_view);
3474 incremental_validate_callback (gpointer data)
3476 GtkTextView *text_view = data;
3477 gboolean result = TRUE;
3479 DV(g_print(G_STRLOC"\n"));
3481 gtk_text_layout_validate (text_view->layout, 2000);
3483 gtk_text_view_update_adjustments (text_view);
3485 if (gtk_text_layout_is_valid (text_view->layout))
3487 text_view->incremental_validate_idle = 0;
3495 gtk_text_view_invalidate (GtkTextView *text_view)
3497 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3498 text_view->onscreen_validated));
3500 text_view->onscreen_validated = FALSE;
3502 /* We'll invalidate when the layout is created */
3503 if (text_view->layout == NULL)
3506 if (!text_view->first_validate_idle)
3508 text_view->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3509 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3510 text_view->first_validate_idle));
3513 if (!text_view->incremental_validate_idle)
3515 text_view->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3516 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3517 text_view->incremental_validate_idle));
3522 invalidated_handler (GtkTextLayout *layout,
3525 GtkTextView *text_view;
3527 text_view = GTK_TEXT_VIEW (data);
3529 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3530 gtk_text_view_invalidate (text_view);
3534 changed_handler (GtkTextLayout *layout,
3540 GtkTextView *text_view;
3542 GdkRectangle visible_rect;
3543 GdkRectangle redraw_rect;
3545 text_view = GTK_TEXT_VIEW (data);
3546 widget = GTK_WIDGET (data);
3548 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3550 if (GTK_WIDGET_REALIZED (text_view))
3552 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3554 redraw_rect.x = visible_rect.x;
3555 redraw_rect.width = visible_rect.width;
3556 redraw_rect.y = start_y;
3558 if (old_height == new_height)
3559 redraw_rect.height = old_height;
3560 else if (start_y + old_height > visible_rect.y)
3561 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3563 redraw_rect.height = 0;
3565 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3567 /* text_window_invalidate_rect() takes buffer coordinates */
3568 text_window_invalidate_rect (text_view->text_window,
3571 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3575 redraw_rect.height));
3577 if (text_view->left_window)
3578 text_window_invalidate_rect (text_view->left_window,
3580 if (text_view->right_window)
3581 text_window_invalidate_rect (text_view->right_window,
3583 if (text_view->top_window)
3584 text_window_invalidate_rect (text_view->top_window,
3586 if (text_view->bottom_window)
3587 text_window_invalidate_rect (text_view->bottom_window,
3590 queue_update_im_spot_location (text_view);
3594 if (old_height != new_height)
3596 gboolean yoffset_changed = FALSE;
3598 int new_first_para_top;
3599 int old_first_para_top;
3602 /* If the bottom of the old area was above the top of the
3603 * screen, we need to scroll to keep the current top of the
3604 * screen in place. Remember that first_para_pixels is the
3605 * position of the top of the screen in coordinates relative to
3606 * the first paragraph onscreen.
3608 * In short we are adding the height delta of the portion of the
3609 * changed region above first_para_mark to text_view->yoffset.
3611 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3612 text_view->first_para_mark);
3614 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3616 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3618 if (new_first_para_top != old_first_para_top)
3620 text_view->yoffset += new_first_para_top - old_first_para_top;
3622 get_vadjustment (text_view)->value = text_view->yoffset;
3623 yoffset_changed = TRUE;
3626 if (yoffset_changed)
3628 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3629 gtk_adjustment_value_changed (get_vadjustment (text_view));
3632 /* FIXME be smarter about which anchored widgets we update */
3634 tmp_list = text_view->children;
3635 while (tmp_list != NULL)
3637 GtkTextViewChild *child = tmp_list->data;
3640 gtk_text_view_update_child_allocation (text_view, child);
3642 tmp_list = g_slist_next (tmp_list);
3647 GtkRequisition old_req;
3648 GtkRequisition new_req;
3650 old_req = widget->requisition;
3652 /* Use this instead of gtk_widget_size_request wrapper
3653 * to avoid the optimization which just returns widget->requisition
3654 * if a resize hasn't been queued.
3656 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3658 if (old_req.width != new_req.width ||
3659 old_req.height != new_req.height)
3661 gtk_widget_queue_resize_no_redraw (widget);
3667 gtk_text_view_realize (GtkWidget *widget)
3669 GtkTextView *text_view;
3670 GdkWindowAttr attributes;
3671 gint attributes_mask;
3674 text_view = GTK_TEXT_VIEW (widget);
3675 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3677 attributes.window_type = GDK_WINDOW_CHILD;
3678 attributes.x = widget->allocation.x;
3679 attributes.y = widget->allocation.y;
3680 attributes.width = widget->allocation.width;
3681 attributes.height = widget->allocation.height;
3682 attributes.wclass = GDK_INPUT_OUTPUT;
3683 attributes.visual = gtk_widget_get_visual (widget);
3684 attributes.colormap = gtk_widget_get_colormap (widget);
3685 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3687 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3689 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3690 &attributes, attributes_mask);
3691 gdk_window_set_user_data (widget->window, widget);
3693 /* must come before text_window_realize calls */
3694 widget->style = gtk_style_attach (widget->style, widget->window);
3696 gdk_window_set_background (widget->window,
3697 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3699 text_window_realize (text_view->text_window, widget);
3701 if (text_view->left_window)
3702 text_window_realize (text_view->left_window, widget);
3704 if (text_view->top_window)
3705 text_window_realize (text_view->top_window, widget);
3707 if (text_view->right_window)
3708 text_window_realize (text_view->right_window, widget);
3710 if (text_view->bottom_window)
3711 text_window_realize (text_view->bottom_window, widget);
3713 gtk_text_view_ensure_layout (text_view);
3715 if (text_view->buffer)
3717 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3718 GDK_SELECTION_PRIMARY);
3719 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3722 tmp_list = text_view->children;
3723 while (tmp_list != NULL)
3725 GtkTextViewChild *vc = tmp_list->data;
3727 text_view_child_set_parent_window (text_view, vc);
3729 tmp_list = tmp_list->next;
3734 gtk_text_view_unrealize (GtkWidget *widget)
3736 GtkTextView *text_view;
3738 text_view = GTK_TEXT_VIEW (widget);
3740 if (text_view->buffer)
3742 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3743 GDK_SELECTION_PRIMARY);
3744 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3747 gtk_text_view_remove_validate_idles (text_view);
3749 if (text_view->popup_menu)
3751 gtk_widget_destroy (text_view->popup_menu);
3752 text_view->popup_menu = NULL;
3755 text_window_unrealize (text_view->text_window);
3757 if (text_view->left_window)
3758 text_window_unrealize (text_view->left_window);
3760 if (text_view->top_window)
3761 text_window_unrealize (text_view->top_window);
3763 if (text_view->right_window)
3764 text_window_unrealize (text_view->right_window);
3766 if (text_view->bottom_window)
3767 text_window_unrealize (text_view->bottom_window);
3769 gtk_text_view_destroy_layout (text_view);
3771 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize (widget);
3775 gtk_text_view_set_background (GtkTextView *text_view)
3777 GtkWidget *widget = GTK_WIDGET (text_view);
3779 gdk_window_set_background (widget->window,
3780 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3782 gdk_window_set_background (text_view->text_window->bin_window,
3783 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3785 if (text_view->left_window)
3786 gdk_window_set_background (text_view->left_window->bin_window,
3787 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3788 if (text_view->right_window)
3789 gdk_window_set_background (text_view->right_window->bin_window,
3790 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3792 if (text_view->top_window)
3793 gdk_window_set_background (text_view->top_window->bin_window,
3794 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3796 if (text_view->bottom_window)
3797 gdk_window_set_background (text_view->bottom_window->bin_window,
3798 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3802 gtk_text_view_style_set (GtkWidget *widget,
3803 GtkStyle *previous_style)
3805 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3806 PangoContext *ltr_context, *rtl_context;
3808 if (GTK_WIDGET_REALIZED (widget))
3810 gtk_text_view_set_background (text_view);
3813 if (text_view->layout && previous_style)
3815 gtk_text_view_set_attributes_from_style (text_view,
3816 text_view->layout->default_style,
3820 ltr_context = gtk_widget_create_pango_context (widget);
3821 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
3822 rtl_context = gtk_widget_create_pango_context (widget);
3823 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
3825 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
3827 g_object_unref (ltr_context);
3828 g_object_unref (rtl_context);
3833 gtk_text_view_direction_changed (GtkWidget *widget,
3834 GtkTextDirection previous_direction)
3836 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3838 if (text_view->layout)
3840 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3842 gtk_text_layout_default_style_changed (text_view->layout);
3847 gtk_text_view_state_changed (GtkWidget *widget,
3848 GtkStateType previous_state)
3850 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3853 if (GTK_WIDGET_REALIZED (widget))
3855 gtk_text_view_set_background (text_view);
3857 if (GTK_WIDGET_IS_SENSITIVE (widget))
3858 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3862 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3865 gdk_cursor_unref (cursor);
3867 text_view->mouse_cursor_obscured = FALSE;
3870 if (!GTK_WIDGET_IS_SENSITIVE (widget))
3872 /* Clear any selection */
3873 gtk_text_view_unselect (text_view);
3876 gtk_widget_queue_draw (widget);
3880 set_invisible_cursor (GdkWindow *window)
3882 GdkBitmap *empty_bitmap;
3885 char invisible_cursor_bits[] = { 0x0 };
3887 useless.red = useless.green = useless.blue = 0;
3890 empty_bitmap = gdk_bitmap_create_from_data (window,
3891 invisible_cursor_bits,
3894 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3899 gdk_window_set_cursor (window, cursor);
3901 gdk_cursor_unref (cursor);
3903 g_object_unref (empty_bitmap);
3907 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3909 if (text_view->mouse_cursor_obscured)
3912 set_invisible_cursor (text_view->text_window->bin_window);
3914 text_view->mouse_cursor_obscured = TRUE;
3918 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
3920 if (text_view->mouse_cursor_obscured)
3924 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
3926 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3927 gdk_cursor_unref (cursor);
3928 text_view->mouse_cursor_obscured = FALSE;
3933 gtk_text_view_grab_notify (GtkWidget *widget,
3934 gboolean was_grabbed)
3938 gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
3939 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
3949 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3952 switch (event->type)
3954 case GDK_MOTION_NOTIFY:
3955 *x = event->motion.x;
3956 *y = event->motion.y;
3960 case GDK_BUTTON_PRESS:
3961 case GDK_2BUTTON_PRESS:
3962 case GDK_3BUTTON_PRESS:
3963 case GDK_BUTTON_RELEASE:
3964 *x = event->button.x;
3965 *y = event->button.y;
3970 case GDK_KEY_RELEASE:
3971 case GDK_ENTER_NOTIFY:
3972 case GDK_LEAVE_NOTIFY:
3973 case GDK_PROPERTY_NOTIFY:
3974 case GDK_SELECTION_CLEAR:
3975 case GDK_SELECTION_REQUEST:
3976 case GDK_SELECTION_NOTIFY:
3977 case GDK_PROXIMITY_IN:
3978 case GDK_PROXIMITY_OUT:
3979 case GDK_DRAG_ENTER:
3980 case GDK_DRAG_LEAVE:
3981 case GDK_DRAG_MOTION:
3982 case GDK_DRAG_STATUS:
3983 case GDK_DROP_START:
3984 case GDK_DROP_FINISHED:
3994 emit_event_on_tags (GtkWidget *widget,
4000 gboolean retval = FALSE;
4002 tags = gtk_text_iter_get_tags (iter);
4007 GtkTextTag *tag = tmp->data;
4009 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
4015 tmp = g_slist_next (tmp);
4018 g_slist_free (tags);
4024 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
4026 GtkTextView *text_view;
4029 text_view = GTK_TEXT_VIEW (widget);
4031 if (text_view->layout == NULL ||
4032 get_buffer (text_view) == NULL)
4035 if (event->any.window != text_view->text_window->bin_window)
4038 if (get_event_coordinates (event, &x, &y))
4042 x += text_view->xoffset;
4043 y += text_view->yoffset;
4045 /* FIXME this is slow and we do it twice per event.
4046 * My favorite solution is to have GtkTextLayout cache
4047 * the last couple lookups.
4049 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4053 return emit_event_on_tags (widget, event, &iter);
4055 else if (event->type == GDK_KEY_PRESS ||
4056 event->type == GDK_KEY_RELEASE)
4060 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
4061 gtk_text_buffer_get_insert (get_buffer (text_view)));
4063 return emit_event_on_tags (widget, event, &iter);
4070 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
4072 gboolean retval = FALSE;
4073 gboolean obscure = FALSE;
4074 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4075 GtkTextMark *insert;
4077 gboolean can_insert;
4079 if (text_view->layout == NULL ||
4080 get_buffer (text_view) == NULL)
4083 /* Make sure input method knows where it is */
4084 flush_update_im_spot_location (text_view);
4086 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4087 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4088 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
4089 if (gtk_im_context_filter_keypress (text_view->im_context, event))
4091 text_view->need_im_reset = TRUE;
4093 gtk_text_view_reset_im_context (text_view);
4094 obscure = can_insert;
4098 else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4102 /* use overall editability not can_insert, more predictable for users */
4103 else if (text_view->editable &&
4104 (event->keyval == GDK_Return ||
4105 event->keyval == GDK_ISO_Enter ||
4106 event->keyval == GDK_KP_Enter))
4108 /* this won't actually insert the newline if the cursor isn't
4111 gtk_text_view_reset_im_context (text_view);
4112 gtk_text_view_commit_text (text_view, "\n");
4117 /* Pass through Tab as literal tab, unless Control is held down */
4118 else if ((event->keyval == GDK_Tab ||
4119 event->keyval == GDK_KP_Tab ||
4120 event->keyval == GDK_ISO_Left_Tab) &&
4121 !(event->state & GDK_CONTROL_MASK))
4123 /* If the text widget isn't editable overall, or if the application
4124 * has turned off "accepts_tab", move the focus instead
4126 if (text_view->accepts_tab && text_view->editable)
4128 gtk_text_view_reset_im_context (text_view);
4129 gtk_text_view_commit_text (text_view, "\t");
4133 g_signal_emit_by_name (text_view, "move-focus",
4134 (event->state & GDK_SHIFT_MASK) ?
4135 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4143 gtk_text_view_obscure_mouse_cursor (text_view);
4145 gtk_text_view_reset_blink_time (text_view);
4146 gtk_text_view_pend_cursor_blink (text_view);
4152 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4154 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4155 GtkTextMark *insert;
4158 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
4161 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4162 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4163 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
4164 gtk_im_context_filter_keypress (text_view->im_context, event))
4166 text_view->need_im_reset = TRUE;
4170 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4174 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4176 GtkTextView *text_view;
4178 text_view = GTK_TEXT_VIEW (widget);
4180 gtk_widget_grab_focus (widget);
4182 if (event->window != text_view->text_window->bin_window)
4184 /* Remove selection if any. */
4185 gtk_text_view_unselect (text_view);
4189 gtk_text_view_reset_blink_time (text_view);
4193 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4194 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4195 else if (event->button == 3)
4196 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4199 if (event->type == GDK_BUTTON_PRESS)
4201 gtk_text_view_reset_im_context (text_view);
4203 if (event->button == 1)
4205 /* If we're in the selection, start a drag copy/move of the
4206 * selection; otherwise, start creating a new selection.
4209 GtkTextIter start, end;
4211 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4213 event->x + text_view->xoffset,
4214 event->y + text_view->yoffset);
4216 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4218 gtk_text_iter_in_range (&iter, &start, &end))
4220 text_view->drag_start_x = event->x;
4221 text_view->drag_start_y = event->y;
4222 text_view->pending_place_cursor_button = event->button;
4226 gtk_text_view_start_selection_drag (text_view, &iter, event);
4231 else if (event->button == 2)
4235 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4237 event->x + text_view->xoffset,
4238 event->y + text_view->yoffset);
4240 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4241 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4243 text_view->editable);
4246 else if (event->button == 3)
4248 gtk_text_view_do_popup (text_view, event);
4252 else if ((event->type == GDK_2BUTTON_PRESS ||
4253 event->type == GDK_3BUTTON_PRESS) &&
4258 gtk_text_view_end_selection_drag (text_view);
4260 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4262 event->x + text_view->xoffset,
4263 event->y + text_view->yoffset);
4265 gtk_text_view_start_selection_drag (text_view, &iter, event);
4273 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4275 GtkTextView *text_view;
4277 text_view = GTK_TEXT_VIEW (widget);
4279 if (event->window != text_view->text_window->bin_window)
4282 if (event->button == 1)
4284 if (text_view->drag_start_x >= 0)
4286 text_view->drag_start_x = -1;
4287 text_view->drag_start_y = -1;
4290 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4292 else if (text_view->pending_place_cursor_button == event->button)
4296 /* Unselect everything; we clicked inside selection, but
4297 * didn't move by the drag threshold, so just clear selection
4300 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4302 event->x + text_view->xoffset,
4303 event->y + text_view->yoffset);
4305 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4306 gtk_text_view_check_cursor_blink (text_view);
4308 text_view->pending_place_cursor_button = 0;
4318 keymap_direction_changed (GdkKeymap *keymap,
4319 GtkTextView *text_view)
4321 gtk_text_view_check_keymap_direction (text_view);
4325 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4327 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4329 gtk_widget_queue_draw (widget);
4331 DV(g_print (G_STRLOC": focus_in_event\n"));
4333 gtk_text_view_reset_blink_time (text_view);
4335 if (text_view->cursor_visible && text_view->layout)
4337 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4338 gtk_text_view_check_cursor_blink (text_view);
4341 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4342 "direction-changed",
4343 G_CALLBACK (keymap_direction_changed), text_view);
4344 gtk_text_view_check_keymap_direction (text_view);
4346 if (text_view->editable)
4348 text_view->need_im_reset = TRUE;
4349 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4356 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4358 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4360 gtk_text_view_end_selection_drag (text_view);
4362 gtk_widget_queue_draw (widget);
4364 DV(g_print (G_STRLOC": focus_out_event\n"));
4366 if (text_view->cursor_visible && text_view->layout)
4368 gtk_text_view_check_cursor_blink (text_view);
4369 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4372 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4373 keymap_direction_changed,
4376 if (text_view->editable)
4378 text_view->need_im_reset = TRUE;
4379 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4386 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4388 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4390 gtk_text_view_unobscure_mouse_cursor (text_view);
4392 if (event->window == text_view->text_window->bin_window &&
4393 text_view->drag_start_x >= 0)
4398 gdk_event_request_motions (event);
4400 if (gtk_drag_check_threshold (widget,
4401 text_view->drag_start_x,
4402 text_view->drag_start_y,
4406 gint buffer_x, buffer_y;
4408 gtk_text_view_window_to_buffer_coords (text_view,
4409 GTK_TEXT_WINDOW_TEXT,
4410 text_view->drag_start_x,
4411 text_view->drag_start_y,
4415 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4417 buffer_x, buffer_y);
4419 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4428 gtk_text_view_paint (GtkWidget *widget,
4430 GdkEventExpose *event)
4432 GtkTextView *text_view;
4433 GList *child_exposes;
4437 text_view = GTK_TEXT_VIEW (widget);
4439 g_return_if_fail (text_view->layout != NULL);
4440 g_return_if_fail (text_view->xoffset >= 0);
4441 g_return_if_fail (text_view->yoffset >= 0);
4443 while (text_view->first_validate_idle != 0)
4445 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4446 text_view->first_validate_idle));
4447 gtk_text_view_flush_first_validate (text_view);
4450 /* More regions could have become invalid in the above loop */
4451 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4456 gdk_region_get_clipbox (updates, &rect);
4458 gdk_rectangle_union (area, &rect, area);
4460 gdk_region_destroy (updates);
4463 if (!text_view->onscreen_validated)
4465 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.");
4466 g_assert_not_reached ();
4470 printf ("painting %d,%d %d x %d\n",
4472 area->width, area->height);
4475 child_exposes = NULL;
4476 gtk_text_layout_draw (text_view->layout,
4478 text_view->text_window->bin_window,
4483 area->width, area->height,
4486 tmp_list = child_exposes;
4487 while (tmp_list != NULL)
4489 GtkWidget *child = tmp_list->data;
4491 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4495 g_object_unref (child);
4497 tmp_list = tmp_list->next;
4500 g_list_free (child_exposes);
4504 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4508 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4509 GTK_TEXT_WINDOW_TEXT))
4511 DV(g_print (">Exposed ("G_STRLOC")\n"));
4512 gtk_text_view_paint (widget, &event->area, event);
4515 if (event->window == widget->window)
4516 gtk_text_view_draw_focus (widget);
4518 /* Propagate exposes to all unanchored children.
4519 * Anchored children are handled in gtk_text_view_paint().
4521 tmp_list = GTK_TEXT_VIEW (widget)->children;
4522 while (tmp_list != NULL)
4524 GtkTextViewChild *vc = tmp_list->data;
4526 /* propagate_expose checks that event->window matches
4530 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4534 tmp_list = tmp_list->next;
4541 gtk_text_view_draw_focus (GtkWidget *widget)
4543 gboolean interior_focus;
4545 /* We clear the focus if we are in interior focus mode. */
4546 gtk_widget_style_get (widget,
4547 "interior-focus", &interior_focus,
4550 if (GTK_WIDGET_DRAWABLE (widget))
4552 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4554 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4555 NULL, widget, "textview",
4557 widget->allocation.width,
4558 widget->allocation.height);
4562 gdk_window_clear (widget->window);
4568 gtk_text_view_focus (GtkWidget *widget,
4569 GtkDirectionType direction)
4571 GtkContainer *container;
4574 container = GTK_CONTAINER (widget);
4576 if (!gtk_widget_is_focus (widget) &&
4577 container->focus_child == NULL)
4579 gtk_widget_grab_focus (widget);
4585 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4586 * children to get the focus
4588 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4589 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4590 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4597 gtk_text_view_move_focus (GtkWidget *widget,
4598 GtkDirectionType direction_type)
4600 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4602 if (GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus)
4603 GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus (text_view,
4612 gtk_text_view_add (GtkContainer *container,
4615 /* This is pretty random. */
4616 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4618 GTK_TEXT_WINDOW_WIDGET,
4623 gtk_text_view_remove (GtkContainer *container,
4627 GtkTextView *text_view;
4628 GtkTextViewChild *vc;
4630 text_view = GTK_TEXT_VIEW (container);
4633 iter = text_view->children;
4635 while (iter != NULL)
4639 if (vc->widget == child)
4642 iter = g_slist_next (iter);
4645 g_assert (iter != NULL); /* be sure we had the child in the list */
4647 text_view->children = g_slist_remove (text_view->children, vc);
4649 gtk_widget_unparent (vc->widget);
4651 text_view_child_free (vc);
4655 gtk_text_view_forall (GtkContainer *container,
4656 gboolean include_internals,
4657 GtkCallback callback,
4658 gpointer callback_data)
4661 GtkTextView *text_view;
4664 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4665 g_return_if_fail (callback != NULL);
4667 text_view = GTK_TEXT_VIEW (container);
4669 copy = g_slist_copy (text_view->children);
4672 while (iter != NULL)
4674 GtkTextViewChild *vc = iter->data;
4676 (* callback) (vc->widget, callback_data);
4678 iter = g_slist_next (iter);
4681 g_slist_free (copy);
4684 #define CURSOR_ON_MULTIPLIER 2
4685 #define CURSOR_OFF_MULTIPLIER 1
4686 #define CURSOR_PEND_MULTIPLIER 3
4687 #define CURSOR_DIVIDER 3
4690 cursor_blinks (GtkTextView *text_view)
4692 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4695 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4698 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4701 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4706 if (text_view->editable)
4708 GtkTextMark *insert;
4711 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4712 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4714 if (gtk_text_iter_editable (&iter, text_view->editable))
4722 get_cursor_time (GtkTextView *text_view)
4724 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4727 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
4733 get_cursor_blink_timeout (GtkTextView *text_view)
4735 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4738 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
4749 blink_cb (gpointer data)
4751 GtkTextView *text_view;
4752 GtkTextViewPrivate *priv;
4756 text_view = GTK_TEXT_VIEW (data);
4757 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4759 if (!GTK_WIDGET_HAS_FOCUS (text_view))
4761 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
4762 "connect a handler to this signal, it must return\n"
4763 "FALSE so the text view gets the event as well");
4765 gtk_text_view_check_cursor_blink (text_view);
4770 g_assert (text_view->layout);
4771 g_assert (text_view->cursor_visible);
4773 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4775 blink_timeout = get_cursor_blink_timeout (text_view);
4776 if (priv->blink_time > 1000 * blink_timeout &&
4777 blink_timeout < G_MAXINT/1000)
4779 /* we've blinked enough without the user doing anything, stop blinking */
4781 text_view->blink_timeout = 0;
4784 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4789 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
4792 priv->blink_time += get_cursor_time (text_view);
4795 /* Block changed_handler while changing the layout's cursor visibility
4796 * because it would expose the whole paragraph. Instead, we expose
4797 * the cursor's area(s) manually below.
4799 g_signal_handlers_block_by_func (text_view->layout,
4802 gtk_text_layout_set_cursor_visible (text_view->layout, !visible);
4803 g_signal_handlers_unblock_by_func (text_view->layout,
4807 text_window_invalidate_cursors (text_view->text_window);
4809 /* Remove ourselves */
4815 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4817 if (text_view->blink_timeout)
4819 g_source_remove (text_view->blink_timeout);
4820 text_view->blink_timeout = 0;
4825 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4827 if (text_view->layout != NULL &&
4828 text_view->cursor_visible &&
4829 GTK_WIDGET_HAS_FOCUS (text_view))
4831 if (cursor_blinks (text_view))
4833 if (text_view->blink_timeout == 0)
4835 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4837 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4844 gtk_text_view_stop_cursor_blink (text_view);
4845 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4850 gtk_text_view_stop_cursor_blink (text_view);
4851 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4856 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
4858 if (text_view->layout != NULL &&
4859 text_view->cursor_visible &&
4860 GTK_WIDGET_HAS_FOCUS (text_view) &&
4861 cursor_blinks (text_view))
4863 gtk_text_view_stop_cursor_blink (text_view);
4864 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4866 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
4873 gtk_text_view_reset_blink_time (GtkTextView *text_view)
4875 GtkTextViewPrivate *priv;
4877 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4879 priv->blink_time = 0;
4884 * Key binding handlers
4888 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4889 GtkTextIter *newplace,
4892 gboolean ret = TRUE;
4896 ret = gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4902 ret = gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4910 move_cursor (GtkTextView *text_view,
4911 const GtkTextIter *new_location,
4912 gboolean extend_selection)
4914 if (extend_selection)
4915 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4919 gtk_text_buffer_place_cursor (get_buffer (text_view),
4921 gtk_text_view_check_cursor_blink (text_view);
4925 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4926 GtkMovementStep step,
4928 gboolean extend_selection)
4931 GtkTextIter newplace;
4932 gint cursor_x_pos = 0;
4933 GtkDirectionType leave_direction = -1;
4935 if (!text_view->cursor_visible)
4937 GtkScrollStep scroll_step;
4941 case GTK_MOVEMENT_VISUAL_POSITIONS:
4942 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
4944 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4945 case GTK_MOVEMENT_WORDS:
4946 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
4948 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4949 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
4951 case GTK_MOVEMENT_DISPLAY_LINES:
4952 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
4954 case GTK_MOVEMENT_PARAGRAPHS:
4955 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4956 scroll_step = GTK_SCROLL_STEPS;
4958 case GTK_MOVEMENT_PAGES:
4959 scroll_step = GTK_SCROLL_PAGES;
4961 case GTK_MOVEMENT_HORIZONTAL_PAGES:
4962 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
4964 case GTK_MOVEMENT_BUFFER_ENDS:
4965 scroll_step = GTK_SCROLL_ENDS;
4968 scroll_step = GTK_SCROLL_PAGES;
4972 if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
4974 if (leave_direction != -1 &&
4975 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
4978 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
4985 gtk_text_view_reset_im_context (text_view);
4987 if (step == GTK_MOVEMENT_PAGES)
4989 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
4990 gtk_widget_error_bell (GTK_WIDGET (text_view));
4992 gtk_text_view_check_cursor_blink (text_view);
4993 gtk_text_view_pend_cursor_blink (text_view);
4996 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
4998 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
4999 gtk_widget_error_bell (GTK_WIDGET (text_view));
5001 gtk_text_view_check_cursor_blink (text_view);
5002 gtk_text_view_pend_cursor_blink (text_view);
5006 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5007 gtk_text_buffer_get_insert (get_buffer (text_view)));
5010 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5011 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
5015 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5016 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5019 case GTK_MOVEMENT_VISUAL_POSITIONS:
5020 gtk_text_layout_move_iter_visually (text_view->layout,
5024 case GTK_MOVEMENT_WORDS:
5026 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5029 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5030 gtk_text_iter_forward_to_line_end (&newplace);
5034 case GTK_MOVEMENT_DISPLAY_LINES:
5037 leave_direction = GTK_DIR_UP;
5039 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5040 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5042 gtk_text_iter_set_line_offset (&newplace, 0);
5046 leave_direction = GTK_DIR_DOWN;
5048 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5049 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5051 gtk_text_iter_forward_to_line_end (&newplace);
5055 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5057 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5058 else if (count < -1)
5059 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5062 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
5065 case GTK_MOVEMENT_PARAGRAPHS:
5068 if (!gtk_text_iter_ends_line (&newplace))
5070 gtk_text_iter_forward_to_line_end (&newplace);
5073 gtk_text_iter_forward_visible_lines (&newplace, count);
5074 gtk_text_iter_forward_to_line_end (&newplace);
5078 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5079 gtk_text_iter_set_line_offset (&newplace, 0);
5080 gtk_text_iter_forward_visible_lines (&newplace, count);
5081 gtk_text_iter_set_line_offset (&newplace, 0);
5085 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5088 if (!gtk_text_iter_ends_line (&newplace))
5089 gtk_text_iter_forward_to_line_end (&newplace);
5093 gtk_text_iter_set_line_offset (&newplace, 0);
5097 case GTK_MOVEMENT_BUFFER_ENDS:
5099 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5101 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5108 /* call move_cursor() even if the cursor hasn't moved, since it
5109 cancels the selection
5111 move_cursor (text_view, &newplace, extend_selection);
5113 if (!gtk_text_iter_equal (&insert, &newplace))
5115 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5116 gtk_text_view_scroll_mark_onscreen (text_view,
5117 gtk_text_buffer_get_insert (get_buffer (text_view)));
5119 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5120 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5122 else if (leave_direction != -1)
5124 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5127 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5132 gtk_widget_error_bell (GTK_WIDGET (text_view));
5135 gtk_text_view_check_cursor_blink (text_view);
5136 gtk_text_view_pend_cursor_blink (text_view);
5140 gtk_text_view_move_cursor (GtkTextView *text_view,
5141 GtkMovementStep step,
5143 gboolean extend_selection)
5145 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5149 gtk_text_view_page_horizontally (GtkTextView *text_view,
5151 gboolean extend_selection)
5153 gtk_text_view_move_cursor_internal (text_view, GTK_MOVEMENT_HORIZONTAL_PAGES,
5154 count, extend_selection);
5159 gtk_text_view_move_viewport (GtkTextView *text_view,
5163 GtkAdjustment *adjustment;
5168 case GTK_SCROLL_STEPS:
5169 case GTK_SCROLL_PAGES:
5170 case GTK_SCROLL_ENDS:
5171 adjustment = get_vadjustment (text_view);
5173 case GTK_SCROLL_HORIZONTAL_STEPS:
5174 case GTK_SCROLL_HORIZONTAL_PAGES:
5175 case GTK_SCROLL_HORIZONTAL_ENDS:
5176 adjustment = get_hadjustment (text_view);
5179 adjustment = get_vadjustment (text_view);
5185 case GTK_SCROLL_STEPS:
5186 case GTK_SCROLL_HORIZONTAL_STEPS:
5187 increment = adjustment->step_increment;
5189 case GTK_SCROLL_PAGES:
5190 case GTK_SCROLL_HORIZONTAL_PAGES:
5191 increment = adjustment->page_increment;
5193 case GTK_SCROLL_ENDS:
5194 case GTK_SCROLL_HORIZONTAL_ENDS:
5195 increment = adjustment->upper - adjustment->lower;
5202 return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
5206 gtk_text_view_set_anchor (GtkTextView *text_view)
5210 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5211 gtk_text_buffer_get_insert (get_buffer (text_view)));
5213 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5217 gtk_text_view_scroll_pages (GtkTextView *text_view,
5219 gboolean extend_selection)
5224 gint cursor_x_pos, cursor_y_pos;
5225 GtkTextMark *insert_mark;
5226 GtkTextIter old_insert;
5227 GtkTextIter new_insert;
5231 g_return_val_if_fail (text_view->vadjustment != NULL, FALSE);
5233 adj = text_view->vadjustment;
5235 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5237 /* Make sure we start from the current cursor position, even
5238 * if it was offscreen, but don't queue more scrolls if we're
5241 if (text_view->pending_scroll)
5242 cancel_pending_scroll (text_view);
5244 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5246 /* Validate the region that will be brought into view by the cursor motion
5248 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5249 &old_insert, insert_mark);
5253 gtk_text_view_get_first_para_iter (text_view, &anchor);
5254 y0 = adj->page_size;
5255 y1 = adj->page_size + count * adj->page_increment;
5259 gtk_text_view_get_first_para_iter (text_view, &anchor);
5260 y0 = count * adj->page_increment + adj->page_size;
5264 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
5265 /* FIXME do we need to update the adjustment ranges here? */
5267 new_insert = old_insert;
5269 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5271 /* already at top, just be sure we are at offset 0 */
5272 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5273 move_cursor (text_view, &new_insert, extend_selection);
5275 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5277 /* already at bottom, just be sure we are at the end */
5278 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5279 move_cursor (text_view, &new_insert, extend_selection);
5283 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5285 oldval = adj->value;
5286 newval = adj->value;
5288 newval += count * adj->page_increment;
5290 set_adjustment_clamped (adj, newval);
5291 cursor_y_pos += adj->value - oldval;
5293 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5294 clamp_iter_onscreen (text_view, &new_insert);
5295 move_cursor (text_view, &new_insert, extend_selection);
5297 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5300 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5301 * only guarantees 1 pixel onscreen.
5303 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5304 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5306 return !gtk_text_iter_equal (&old_insert, &new_insert);
5310 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5312 gboolean extend_selection)
5317 gint cursor_x_pos, cursor_y_pos;
5318 GtkTextMark *insert_mark;
5319 GtkTextIter old_insert;
5320 GtkTextIter new_insert;
5323 g_return_val_if_fail (text_view->hadjustment != NULL, FALSE);
5325 adj = text_view->hadjustment;
5327 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5329 /* Make sure we start from the current cursor position, even
5330 * if it was offscreen, but don't queue more scrolls if we're
5333 if (text_view->pending_scroll)
5334 cancel_pending_scroll (text_view);
5336 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5338 /* Validate the line that we're moving within.
5340 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5341 &old_insert, insert_mark);
5343 gtk_text_layout_get_line_yrange (text_view->layout, &old_insert, &y, &height);
5344 gtk_text_layout_validate_yrange (text_view->layout, &old_insert, y, y + height);
5345 /* FIXME do we need to update the adjustment ranges here? */
5347 new_insert = old_insert;
5349 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5351 /* already at far left, just be sure we are at offset 0 */
5352 gtk_text_iter_set_line_offset (&new_insert, 0);
5353 move_cursor (text_view, &new_insert, extend_selection);
5355 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5357 /* already at far right, just be sure we are at the end */
5358 if (!gtk_text_iter_ends_line (&new_insert))
5359 gtk_text_iter_forward_to_line_end (&new_insert);
5360 move_cursor (text_view, &new_insert, extend_selection);
5364 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5366 oldval = adj->value;
5367 newval = adj->value;
5369 newval += count * adj->page_increment;
5371 set_adjustment_clamped (adj, newval);
5372 cursor_x_pos += adj->value - oldval;
5374 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5375 clamp_iter_onscreen (text_view, &new_insert);
5376 move_cursor (text_view, &new_insert, extend_selection);
5378 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5381 /* FIXME for lines shorter than the overall widget width, this results in a
5382 * "bounce" effect as we scroll to the right of the widget, then scroll
5383 * back to get the end of the line onscreen.
5384 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5387 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5388 * only guarantees 1 pixel onscreen.
5390 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5391 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5393 return !gtk_text_iter_equal (&old_insert, &new_insert);
5397 whitespace (gunichar ch, gpointer user_data)
5399 return (ch == ' ' || ch == '\t');
5403 not_whitespace (gunichar ch, gpointer user_data)
5405 return !whitespace (ch, user_data);
5409 find_whitepace_region (const GtkTextIter *center,
5410 GtkTextIter *start, GtkTextIter *end)
5415 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5416 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5417 if (whitespace (gtk_text_iter_get_char (end), NULL))
5418 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5420 return !gtk_text_iter_equal (start, end);
5424 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5427 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5428 text_view->editable))
5430 gtk_widget_error_bell (GTK_WIDGET (text_view));
5435 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5442 gboolean leave_one = FALSE;
5444 gtk_text_view_reset_im_context (text_view);
5446 if (type == GTK_DELETE_CHARS)
5448 /* Char delete deletes the selection, if one exists */
5449 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5450 text_view->editable))
5454 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5455 gtk_text_buffer_get_insert (get_buffer (text_view)));
5462 case GTK_DELETE_CHARS:
5463 gtk_text_iter_forward_cursor_positions (&end, count);
5466 case GTK_DELETE_WORD_ENDS:
5468 gtk_text_iter_forward_word_ends (&end, count);
5470 gtk_text_iter_backward_word_starts (&start, 0 - count);
5473 case GTK_DELETE_WORDS:
5476 case GTK_DELETE_DISPLAY_LINE_ENDS:
5479 case GTK_DELETE_DISPLAY_LINES:
5482 case GTK_DELETE_PARAGRAPH_ENDS:
5483 /* If we're already at a newline, we need to
5484 * simply delete that newline, instead of
5485 * moving to the next one.
5487 if (gtk_text_iter_ends_line (&end))
5489 gtk_text_iter_forward_line (&end);
5495 if (!gtk_text_iter_forward_to_line_end (&end))
5501 /* FIXME figure out what a negative count means
5505 case GTK_DELETE_PARAGRAPHS:
5508 gtk_text_iter_set_line_offset (&start, 0);
5509 gtk_text_iter_forward_to_line_end (&end);
5511 /* Do the lines beyond the first. */
5514 gtk_text_iter_forward_to_line_end (&end);
5520 /* FIXME negative count? */
5524 case GTK_DELETE_WHITESPACE:
5526 find_whitepace_region (&insert, &start, &end);
5534 if (!gtk_text_iter_equal (&start, &end))
5536 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5538 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5539 text_view->editable))
5542 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5544 text_view->editable);
5548 gtk_widget_error_bell (GTK_WIDGET (text_view));
5551 gtk_text_buffer_end_user_action (get_buffer (text_view));
5552 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5554 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5555 gtk_text_view_scroll_mark_onscreen (text_view,
5556 gtk_text_buffer_get_insert (get_buffer (text_view)));
5560 gtk_widget_error_bell (GTK_WIDGET (text_view));
5565 gtk_text_view_backspace (GtkTextView *text_view)
5569 gtk_text_view_reset_im_context (text_view);
5571 /* Backspace deletes the selection, if one exists */
5572 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5573 text_view->editable))
5576 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5578 gtk_text_buffer_get_insert (get_buffer (text_view)));
5580 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5581 TRUE, text_view->editable))
5583 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5584 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5585 gtk_text_view_scroll_mark_onscreen (text_view,
5586 gtk_text_buffer_get_insert (get_buffer (text_view)));
5590 gtk_widget_error_bell (GTK_WIDGET (text_view));
5595 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5597 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5598 GDK_SELECTION_CLIPBOARD);
5600 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5602 text_view->editable);
5603 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5604 gtk_text_view_scroll_mark_onscreen (text_view,
5605 gtk_text_buffer_get_insert (get_buffer (text_view)));
5609 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5611 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5612 GDK_SELECTION_CLIPBOARD);
5614 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5617 /* on copy do not scroll, we are already onscreen */
5621 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5623 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5624 GDK_SELECTION_CLIPBOARD);
5626 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5629 text_view->editable);
5630 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5631 gtk_text_view_scroll_mark_onscreen (text_view,
5632 gtk_text_buffer_get_insert (get_buffer (text_view)));
5636 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5638 if (text_view->text_window)
5639 text_window_invalidate_cursors (text_view->text_window);
5641 text_view->overwrite_mode = !text_view->overwrite_mode;
5643 if (text_view->layout)
5644 gtk_text_layout_set_overwrite_mode (text_view->layout,
5645 text_view->overwrite_mode && text_view->editable);
5647 if (text_view->text_window)
5648 text_window_invalidate_cursors (text_view->text_window);
5650 gtk_text_view_pend_cursor_blink (text_view);
5652 g_object_notify (G_OBJECT (text_view), "overwrite");
5656 * gtk_text_view_get_overwrite:
5657 * @text_view: a #GtkTextView
5659 * Returns whether the #GtkTextView is in overwrite mode or not.
5661 * Return value: whether @text_view is in overwrite mode or not.
5666 gtk_text_view_get_overwrite (GtkTextView *text_view)
5668 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5670 return text_view->overwrite_mode;
5674 * gtk_text_view_set_overwrite:
5675 * @text_view: a #GtkTextView
5676 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
5678 * Changes the #GtkTextView overwrite mode.
5683 gtk_text_view_set_overwrite (GtkTextView *text_view,
5686 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5687 overwrite = overwrite != FALSE;
5689 if (text_view->overwrite_mode != overwrite)
5690 gtk_text_view_toggle_overwrite (text_view);
5694 * gtk_text_view_set_accepts_tab:
5695 * @text_view: A #GtkTextView
5696 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
5697 * character, %FALSE, if pressing the Tab key should move the
5700 * Sets the behavior of the text widget when the Tab key is pressed.
5701 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
5702 * is %FALSE the keyboard focus is moved to the next widget in the focus
5708 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
5709 gboolean accepts_tab)
5711 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5713 accepts_tab = accepts_tab != FALSE;
5715 if (text_view->accepts_tab != accepts_tab)
5717 text_view->accepts_tab = accepts_tab;
5719 g_object_notify (G_OBJECT (text_view), "accepts-tab");
5724 * gtk_text_view_get_accepts_tab:
5725 * @text_view: A #GtkTextView
5727 * Returns whether pressing the Tab key inserts a tab characters.
5728 * gtk_text_view_set_accepts_tab().
5730 * Return value: %TRUE if pressing the Tab key inserts a tab character,
5731 * %FALSE if pressing the Tab key moves the keyboard focus.
5736 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
5738 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5740 return text_view->accepts_tab;
5744 gtk_text_view_compat_move_focus (GtkTextView *text_view,
5745 GtkDirectionType direction_type)
5747 GSignalInvocationHint *hint = g_signal_get_invocation_hint (text_view);
5749 /* as of GTK+ 2.12, the "move-focus" signal has been moved to GtkWidget,
5750 * the evil code below makes sure that both emitting the signal and
5751 * calling the virtual function directly continue to work as expetcted
5754 if (hint->signal_id == g_signal_lookup ("move-focus", GTK_TYPE_WIDGET))
5756 /* if this is a signal emission, chain up */
5760 g_signal_chain_from_overridden_handler (text_view,
5761 direction_type, &retval);
5765 /* otherwise emit the signal, since somebody called the virtual
5769 g_signal_emit_by_name (text_view, "move-focus", direction_type);
5778 gtk_text_view_unselect (GtkTextView *text_view)
5782 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5783 gtk_text_buffer_get_insert (get_buffer (text_view)));
5785 gtk_text_buffer_move_mark (get_buffer (text_view),
5786 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
5791 get_iter_at_pointer (GtkTextView *text_view,
5796 gint xcoord, ycoord;
5797 GdkModifierType state;
5799 gdk_window_get_pointer (text_view->text_window->bin_window,
5800 &xcoord, &ycoord, &state);
5802 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5804 xcoord + text_view->xoffset,
5805 ycoord + text_view->yoffset);
5814 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5815 const gchar *mark_name)
5817 GtkTextIter newplace;
5820 get_iter_at_pointer (text_view, &newplace, NULL, NULL);
5822 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5824 /* This may invalidate the layout */
5825 DV(g_print (G_STRLOC": move mark\n"));
5827 gtk_text_buffer_move_mark (get_buffer (text_view),
5831 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5832 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5834 DV (g_print ("first validate idle leaving %s is %d\n",
5835 G_STRLOC, text_view->first_validate_idle));
5839 selection_scan_timeout (gpointer data)
5841 GtkTextView *text_view;
5843 text_view = GTK_TEXT_VIEW (data);
5845 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5846 gtk_text_view_scroll_mark_onscreen (text_view,
5847 gtk_text_buffer_get_insert (get_buffer (text_view)));
5849 return TRUE; /* remain installed. */
5852 #define UPPER_OFFSET_ANCHOR 0.8
5853 #define LOWER_OFFSET_ANCHOR 0.2
5856 check_scroll (gdouble offset, GtkAdjustment *adj)
5858 if ((offset > UPPER_OFFSET_ANCHOR &&
5859 adj->value + adj->page_size < adj->upper) ||
5860 (offset < LOWER_OFFSET_ANCHOR &&
5861 adj->value > adj->lower))
5868 drag_scan_timeout (gpointer data)
5870 GtkTextView *text_view;
5871 GtkTextIter newplace;
5872 gint x, y, width, height;
5873 gdouble pointer_xoffset, pointer_yoffset;
5875 text_view = GTK_TEXT_VIEW (data);
5877 get_iter_at_pointer (text_view, &newplace, &x, &y);
5878 gdk_drawable_get_size (text_view->text_window->bin_window, &width, &height);
5880 gtk_text_buffer_move_mark (get_buffer (text_view),
5881 text_view->dnd_mark,
5884 pointer_xoffset = (gdouble) x / width;
5885 pointer_yoffset = (gdouble) y / height;
5887 if (check_scroll (pointer_xoffset, text_view->hadjustment) ||
5888 check_scroll (pointer_yoffset, text_view->vadjustment))
5890 /* do not make offsets surpass lower nor upper anchors, this makes
5891 * scrolling speed relative to the distance of the pointer to the
5892 * anchors when it moves beyond them.
5894 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
5895 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
5897 gtk_text_view_scroll_to_mark (text_view,
5898 text_view->dnd_mark,
5899 0., TRUE, pointer_xoffset, pointer_yoffset);
5910 } SelectionGranularity;
5913 * Move @start and @end to the boundaries of the selection unit (indicated by
5914 * @granularity) which contained @start initially.
5915 * If the selction unit is SELECT_WORDS and @start is not contained in a word
5916 * the selection is extended to all the white spaces between the end of the
5917 * word preceding @start and the start of the one following.
5920 extend_selection (GtkTextView *text_view,
5921 SelectionGranularity granularity,
5927 if (granularity == SELECT_WORDS)
5929 if (gtk_text_iter_inside_word (start))
5931 if (!gtk_text_iter_starts_word (start))
5932 gtk_text_iter_backward_visible_word_start (start);
5934 if (!gtk_text_iter_ends_word (end))
5936 if (!gtk_text_iter_forward_visible_word_end (end))
5937 gtk_text_iter_forward_to_end (end);
5945 if (gtk_text_iter_backward_visible_word_start (&tmp))
5946 gtk_text_iter_forward_visible_word_end (&tmp);
5948 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
5951 gtk_text_iter_set_line_offset (start, 0);
5954 if (!gtk_text_iter_forward_visible_word_end (&tmp))
5955 gtk_text_iter_forward_to_end (&tmp);
5957 if (gtk_text_iter_ends_word (&tmp))
5958 gtk_text_iter_backward_visible_word_start (&tmp);
5960 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
5963 gtk_text_iter_forward_to_line_end (end);
5966 else if (granularity == SELECT_LINES)
5968 if (gtk_text_view_starts_display_line (text_view, start))
5970 /* If on a display line boundary, we assume the user
5971 * clicked off the end of a line and we therefore select
5972 * the line before the boundary.
5974 gtk_text_view_backward_display_line_start (text_view, start);
5978 /* start isn't on the start of a line, so we move it to the
5979 * start, and move end to the end unless it's already there.
5981 gtk_text_view_backward_display_line_start (text_view, start);
5983 if (!gtk_text_view_starts_display_line (text_view, end))
5984 gtk_text_view_forward_display_line_end (text_view, end);
5992 SelectionGranularity granularity;
5993 GtkTextMark *orig_start;
5994 GtkTextMark *orig_end;
5998 selection_data_free (SelectionData *data)
6000 if (data->orig_start != NULL)
6001 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6003 if (data->orig_end != NULL)
6004 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6010 selection_motion_event_handler (GtkTextView *text_view,
6011 GdkEventMotion *event,
6012 SelectionData *data)
6014 gdk_event_request_motions (event);
6016 if (data->granularity == SELECT_CHARACTERS)
6018 move_mark_to_pointer_and_scroll (text_view, "insert");
6022 GtkTextIter cursor, start, end;
6023 GtkTextIter orig_start, orig_end;
6024 GtkTextBuffer *buffer;
6026 buffer = get_buffer (text_view);
6028 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6029 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6031 get_iter_at_pointer (text_view, &cursor, NULL, NULL);
6034 extend_selection (text_view, data->granularity, &start, &end);
6036 /* either the selection extends to the front, or end (or not) */
6037 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6038 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6040 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6042 gtk_text_view_scroll_mark_onscreen (text_view,
6043 gtk_text_buffer_get_insert (buffer));
6046 /* If we had to scroll offscreen, insert a timeout to do so
6047 * again. Note that in the timeout, even if the mouse doesn't
6048 * move, due to this scroll xoffset/yoffset will have changed
6049 * and we'll need to scroll again.
6051 if (text_view->scroll_timeout != 0) /* reset on every motion event */
6052 g_source_remove (text_view->scroll_timeout);
6054 text_view->scroll_timeout =
6055 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6061 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6062 const GtkTextIter *iter,
6063 GdkEventButton *button)
6065 GtkTextIter cursor, ins, bound;
6066 GtkTextIter orig_start, orig_end;
6067 GtkTextBuffer *buffer;
6068 SelectionData *data;
6070 if (text_view->selection_drag_handler != 0)
6073 data = g_new0 (SelectionData, 1);
6075 if (button->type == GDK_2BUTTON_PRESS)
6076 data->granularity = SELECT_WORDS;
6077 else if (button->type == GDK_3BUTTON_PRESS)
6078 data->granularity = SELECT_LINES;
6080 data->granularity = SELECT_CHARACTERS;
6082 gtk_grab_add (GTK_WIDGET (text_view));
6084 buffer = get_buffer (text_view);
6089 extend_selection (text_view, data->granularity, &ins, &bound);
6093 if (button->state & GDK_SHIFT_MASK)
6095 /* Extend selection */
6096 GtkTextIter old_ins, old_bound;
6097 GtkTextIter old_start, old_end;
6099 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6100 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6101 old_start = old_ins;
6102 old_end = old_bound;
6103 gtk_text_iter_order (&old_start, &old_end);
6105 /* move the front cursor, if the mouse is in front of the selection. Should the
6106 * cursor however be inside the selection (this happens on tripple click) then we
6107 * move the side which was last moved (current insert mark) */
6108 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6109 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6110 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6113 orig_start = old_end;
6125 gtk_text_buffer_select_range (buffer, &ins, &bound);
6127 gtk_text_iter_order (&orig_start, &orig_end);
6128 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6130 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6133 gtk_text_view_check_cursor_blink (text_view);
6135 text_view->selection_drag_handler = g_signal_connect_data (text_view,
6136 "motion-notify-event",
6137 G_CALLBACK (selection_motion_event_handler),
6139 (GClosureNotify) selection_data_free, 0);
6142 /* returns whether we were really dragging */
6144 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6146 if (text_view->selection_drag_handler == 0)
6149 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
6150 text_view->selection_drag_handler = 0;
6152 if (text_view->scroll_timeout != 0)
6154 g_source_remove (text_view->scroll_timeout);
6155 text_view->scroll_timeout = 0;
6158 gtk_grab_remove (GTK_WIDGET (text_view));
6168 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6169 GtkTextAttributes *values,
6172 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
6173 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
6176 pango_font_description_free (values->font);
6178 values->font = pango_font_description_copy (style->font_desc);
6182 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6184 if (text_view->layout)
6186 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6187 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6188 GtkTextDirection new_cursor_dir;
6189 GtkTextDirection new_keyboard_dir;
6190 gboolean split_cursor;
6192 g_object_get (settings,
6193 "gtk-split-cursor", &split_cursor,
6196 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6197 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6199 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6202 new_cursor_dir = GTK_TEXT_DIR_NONE;
6204 new_cursor_dir = new_keyboard_dir;
6206 gtk_text_layout_set_cursor_direction (text_view->layout, new_cursor_dir);
6207 gtk_text_layout_set_keyboard_direction (text_view->layout, new_keyboard_dir);
6212 gtk_text_view_ensure_layout (GtkTextView *text_view)
6216 widget = GTK_WIDGET (text_view);
6218 if (text_view->layout == NULL)
6220 GtkTextAttributes *style;
6221 PangoContext *ltr_context, *rtl_context;
6224 DV(g_print(G_STRLOC"\n"));
6226 text_view->layout = gtk_text_layout_new ();
6228 g_signal_connect (text_view->layout,
6230 G_CALLBACK (invalidated_handler),
6233 g_signal_connect (text_view->layout,
6235 G_CALLBACK (changed_handler),
6238 g_signal_connect (text_view->layout,
6240 G_CALLBACK (gtk_text_view_child_allocated),
6243 if (get_buffer (text_view))
6244 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
6246 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
6247 gtk_text_view_pend_cursor_blink (text_view);
6249 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
6251 gtk_text_layout_set_overwrite_mode (text_view->layout,
6252 text_view->overwrite_mode && text_view->editable);
6254 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6255 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6256 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6257 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6259 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
6261 g_object_unref (ltr_context);
6262 g_object_unref (rtl_context);
6264 gtk_text_view_check_keymap_direction (text_view);
6266 style = gtk_text_attributes_new ();
6268 gtk_widget_ensure_style (widget);
6269 gtk_text_view_set_attributes_from_style (text_view,
6270 style, widget->style);
6272 style->pixels_above_lines = text_view->pixels_above_lines;
6273 style->pixels_below_lines = text_view->pixels_below_lines;
6274 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
6275 style->left_margin = text_view->left_margin;
6276 style->right_margin = text_view->right_margin;
6277 style->indent = text_view->indent;
6278 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
6280 style->wrap_mode = text_view->wrap_mode;
6281 style->justification = text_view->justify;
6282 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6284 gtk_text_layout_set_default_style (text_view->layout, style);
6286 gtk_text_attributes_unref (style);
6288 /* Set layout for all anchored children */
6290 tmp_list = text_view->children;
6291 while (tmp_list != NULL)
6293 GtkTextViewChild *vc = tmp_list->data;
6297 gtk_text_anchored_child_set_layout (vc->widget,
6299 /* vc may now be invalid! */
6302 tmp_list = g_slist_next (tmp_list);
6305 gtk_text_view_invalidate (text_view);
6310 * gtk_text_view_get_default_attributes:
6311 * @text_view: a #GtkTextView
6313 * Obtains a copy of the default text attributes. These are the
6314 * attributes used for text unless a tag overrides them.
6315 * You'd typically pass the default attributes in to
6316 * gtk_text_iter_get_attributes() in order to get the
6317 * attributes in effect at a given text position.
6319 * The return value is a copy owned by the caller of this function,
6320 * and should be freed.
6322 * Return value: a new #GtkTextAttributes
6325 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6327 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6329 gtk_text_view_ensure_layout (text_view);
6331 return gtk_text_attributes_copy (text_view->layout->default_style);
6335 gtk_text_view_destroy_layout (GtkTextView *text_view)
6337 if (text_view->layout)
6341 gtk_text_view_remove_validate_idles (text_view);
6343 g_signal_handlers_disconnect_by_func (text_view->layout,
6344 invalidated_handler,
6346 g_signal_handlers_disconnect_by_func (text_view->layout,
6350 /* Remove layout from all anchored children */
6351 tmp_list = text_view->children;
6352 while (tmp_list != NULL)
6354 GtkTextViewChild *vc = tmp_list->data;
6358 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6359 /* vc may now be invalid! */
6362 tmp_list = g_slist_next (tmp_list);
6365 gtk_text_view_stop_cursor_blink (text_view);
6366 gtk_text_view_end_selection_drag (text_view);
6368 g_object_unref (text_view->layout);
6369 text_view->layout = NULL;
6374 gtk_text_view_reset_im_context (GtkTextView *text_view)
6376 if (text_view->need_im_reset)
6378 text_view->need_im_reset = FALSE;
6379 gtk_im_context_reset (text_view->im_context);
6388 drag_begin_cb (GtkWidget *widget,
6389 GdkDragContext *context,
6392 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6393 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6396 GdkPixmap *pixmap = NULL;
6398 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6400 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6401 pixmap = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6405 gtk_drag_set_icon_pixmap (context,
6406 gdk_drawable_get_colormap (pixmap),
6410 g_object_unref (pixmap);
6414 gtk_drag_set_icon_default (context);
6419 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6420 const GtkTextIter *iter,
6421 GdkEventMotion *event)
6423 GtkTargetList *target_list;
6425 text_view->drag_start_x = -1;
6426 text_view->drag_start_y = -1;
6427 text_view->pending_place_cursor_button = 0;
6429 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6431 g_signal_connect (text_view, "drag-begin",
6432 G_CALLBACK (drag_begin_cb), NULL);
6433 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6434 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6435 1, (GdkEvent*)event);
6439 gtk_text_view_drag_begin (GtkWidget *widget,
6440 GdkDragContext *context)
6446 gtk_text_view_drag_end (GtkWidget *widget,
6447 GdkDragContext *context)
6452 gtk_text_view_drag_data_get (GtkWidget *widget,
6453 GdkDragContext *context,
6454 GtkSelectionData *selection_data,
6458 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6459 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6461 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6463 gtk_selection_data_set (selection_data,
6464 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6469 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6476 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6478 /* Extract the selected text */
6479 str = gtk_text_buffer_serialize (buffer, buffer,
6480 selection_data->target,
6487 gtk_selection_data_set (selection_data,
6488 selection_data->target,
6490 (guchar *) str, len);
6500 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6502 /* Extract the selected text */
6503 str = gtk_text_iter_get_visible_text (&start, &end);
6508 gtk_selection_data_set_text (selection_data, str, -1);
6515 gtk_text_view_drag_data_delete (GtkWidget *widget,
6516 GdkDragContext *context)
6518 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
6519 TRUE, GTK_TEXT_VIEW (widget)->editable);
6523 gtk_text_view_drag_leave (GtkWidget *widget,
6524 GdkDragContext *context,
6527 GtkTextView *text_view;
6529 text_view = GTK_TEXT_VIEW (widget);
6531 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6533 if (text_view->scroll_timeout != 0)
6534 g_source_remove (text_view->scroll_timeout);
6536 text_view->scroll_timeout = 0;
6540 gtk_text_view_drag_motion (GtkWidget *widget,
6541 GdkDragContext *context,
6546 GtkTextIter newplace;
6547 GtkTextView *text_view;
6550 GdkRectangle target_rect;
6553 GdkDragAction suggested_action = 0;
6555 text_view = GTK_TEXT_VIEW (widget);
6557 target_rect = text_view->text_window->allocation;
6559 if (x < target_rect.x ||
6560 y < target_rect.y ||
6561 x > (target_rect.x + target_rect.width) ||
6562 y > (target_rect.y + target_rect.height))
6563 return FALSE; /* outside the text window, allow parent widgets to handle event */
6565 gtk_text_view_window_to_buffer_coords (text_view,
6566 GTK_TEXT_WINDOW_WIDGET,
6570 gtk_text_layout_get_iter_at_pixel (text_view->layout,
6574 target = gtk_drag_dest_find_target (widget, context,
6575 gtk_drag_dest_get_target_list (widget));
6577 if (target == GDK_NONE)
6579 /* can't accept any of the offered targets */
6581 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6583 gtk_text_iter_compare (&newplace, &start) >= 0 &&
6584 gtk_text_iter_compare (&newplace, &end) <= 0)
6586 /* We're inside the selection. */
6590 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
6592 GtkWidget *source_widget;
6594 suggested_action = context->suggested_action;
6596 source_widget = gtk_drag_get_source_widget (context);
6598 if (source_widget == widget)
6600 /* Default to MOVE, unless the user has
6601 * pressed ctrl or alt to affect available actions
6603 if ((context->actions & GDK_ACTION_MOVE) != 0)
6604 suggested_action = GDK_ACTION_MOVE;
6609 /* Can't drop here. */
6613 if (suggested_action != 0)
6615 gtk_text_mark_set_visible (text_view->dnd_mark,
6616 text_view->cursor_visible);
6618 gdk_drag_status (context, suggested_action, time);
6622 gdk_drag_status (context, 0, time);
6623 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6626 if (!text_view->scroll_timeout)
6627 text_view->scroll_timeout =
6628 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
6630 /* TRUE return means don't propagate the drag motion to parent
6631 * widgets that may also be drop sites.
6637 gtk_text_view_drag_drop (GtkWidget *widget,
6638 GdkDragContext *context,
6643 GtkTextView *text_view;
6644 GtkTextIter drop_point;
6645 GdkAtom target = GDK_NONE;
6647 text_view = GTK_TEXT_VIEW (widget);
6649 if (text_view->scroll_timeout != 0)
6650 g_source_remove (text_view->scroll_timeout);
6652 text_view->scroll_timeout = 0;
6654 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6656 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6658 text_view->dnd_mark);
6660 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
6661 target = gtk_drag_dest_find_target (widget, context, NULL);
6663 if (target != GDK_NONE)
6664 gtk_drag_get_data (widget, context, target, time);
6666 gtk_drag_finish (context, FALSE, FALSE, time);
6672 insert_text_data (GtkTextView *text_view,
6673 GtkTextIter *drop_point,
6674 GtkSelectionData *selection_data)
6678 str = gtk_selection_data_get_text (selection_data);
6682 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
6683 drop_point, (gchar *) str, -1,
6684 text_view->editable))
6686 gtk_widget_error_bell (GTK_WIDGET (text_view));
6694 gtk_text_view_drag_data_received (GtkWidget *widget,
6695 GdkDragContext *context,
6698 GtkSelectionData *selection_data,
6702 GtkTextIter drop_point;
6703 GtkTextView *text_view;
6704 gboolean success = FALSE;
6705 GtkTextBuffer *buffer = NULL;
6707 text_view = GTK_TEXT_VIEW (widget);
6709 if (!text_view->dnd_mark)
6712 buffer = get_buffer (text_view);
6714 gtk_text_buffer_get_iter_at_mark (buffer,
6716 text_view->dnd_mark);
6718 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
6723 gtk_text_buffer_begin_user_action (buffer);
6725 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6727 GtkTextBuffer *src_buffer = NULL;
6728 GtkTextIter start, end;
6729 gboolean copy_tags = TRUE;
6731 if (selection_data->length != sizeof (src_buffer))
6734 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
6736 if (src_buffer == NULL)
6739 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
6741 if (gtk_text_buffer_get_tag_table (src_buffer) !=
6742 gtk_text_buffer_get_tag_table (buffer))
6744 /* try to find a suitable rich text target instead */
6748 GdkAtom target = GDK_NONE;
6752 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
6754 for (list = context->targets; list; list = g_list_next (list))
6758 for (i = 0; i < n_atoms; i++)
6759 if (GUINT_TO_POINTER (atoms[i]) == list->data)
6768 if (target != GDK_NONE)
6770 gtk_drag_get_data (widget, context, target, time);
6771 gtk_text_buffer_end_user_action (buffer);
6776 if (gtk_text_buffer_get_selection_bounds (src_buffer,
6781 gtk_text_buffer_insert_range_interactive (buffer,
6785 text_view->editable);
6790 str = gtk_text_iter_get_visible_text (&start, &end);
6791 gtk_text_buffer_insert_interactive (buffer,
6792 &drop_point, str, -1,
6793 text_view->editable);
6798 else if (selection_data->length > 0 &&
6799 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6802 GError *error = NULL;
6804 retval = gtk_text_buffer_deserialize (buffer, buffer,
6805 selection_data->target,
6807 (guint8 *) selection_data->data,
6808 selection_data->length,
6813 g_warning ("error pasting: %s\n", error->message);
6814 g_clear_error (&error);
6818 insert_text_data (text_view, &drop_point, selection_data);
6821 gtk_drag_finish (context, success,
6822 success && context->action == GDK_ACTION_MOVE,
6827 gtk_text_buffer_get_iter_at_mark (buffer,
6829 text_view->dnd_mark);
6830 gtk_text_buffer_place_cursor (buffer, &drop_point);
6832 gtk_text_buffer_end_user_action (buffer);
6836 static GtkAdjustment*
6837 get_hadjustment (GtkTextView *text_view)
6839 if (text_view->hadjustment == NULL)
6840 gtk_text_view_set_scroll_adjustments (text_view,
6841 NULL, /* forces creation */
6842 text_view->vadjustment);
6844 return text_view->hadjustment;
6847 static GtkAdjustment*
6848 get_vadjustment (GtkTextView *text_view)
6850 if (text_view->vadjustment == NULL)
6851 gtk_text_view_set_scroll_adjustments (text_view,
6852 text_view->hadjustment,
6853 NULL); /* forces creation */
6854 return text_view->vadjustment;
6859 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
6860 GtkAdjustment *hadj,
6861 GtkAdjustment *vadj)
6863 gboolean need_adjust = FALSE;
6866 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
6868 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6870 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
6872 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6874 if (text_view->hadjustment && (text_view->hadjustment != hadj))
6876 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
6877 gtk_text_view_value_changed,
6879 g_object_unref (text_view->hadjustment);
6882 if (text_view->vadjustment && (text_view->vadjustment != vadj))
6884 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
6885 gtk_text_view_value_changed,
6887 g_object_unref (text_view->vadjustment);
6890 if (text_view->hadjustment != hadj)
6892 text_view->hadjustment = hadj;
6893 g_object_ref_sink (text_view->hadjustment);
6895 g_signal_connect (text_view->hadjustment, "value-changed",
6896 G_CALLBACK (gtk_text_view_value_changed),
6901 if (text_view->vadjustment != vadj)
6903 text_view->vadjustment = vadj;
6904 g_object_ref_sink (text_view->vadjustment);
6906 g_signal_connect (text_view->vadjustment, "value-changed",
6907 G_CALLBACK (gtk_text_view_value_changed),
6913 gtk_text_view_value_changed (NULL, text_view);
6916 /* FIXME this adjust_allocation is a big cut-and-paste from
6917 * GtkCList, needs to be some "official" way to do this
6927 /* The window to which widget->window is relative */
6928 #define ALLOCATION_WINDOW(widget) \
6929 (GTK_WIDGET_NO_WINDOW (widget) ? \
6930 (widget)->window : \
6931 gdk_window_get_parent ((widget)->window))
6934 adjust_allocation_recurse (GtkWidget *widget,
6937 ScrollData *scroll_data = data;
6939 /* Need to really size allocate instead of just poking
6940 * into widget->allocation if the widget is not realized.
6941 * FIXME someone figure out why this was.
6943 if (!GTK_WIDGET_REALIZED (widget))
6945 if (GTK_WIDGET_VISIBLE (widget))
6947 GdkRectangle tmp_rectangle = widget->allocation;
6948 tmp_rectangle.x += scroll_data->dx;
6949 tmp_rectangle.y += scroll_data->dy;
6951 gtk_widget_size_allocate (widget, &tmp_rectangle);
6956 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
6958 widget->allocation.x += scroll_data->dx;
6959 widget->allocation.y += scroll_data->dy;
6961 if (GTK_IS_CONTAINER (widget))
6962 gtk_container_forall (GTK_CONTAINER (widget),
6963 adjust_allocation_recurse,
6970 adjust_allocation (GtkWidget *widget,
6974 ScrollData scroll_data;
6976 if (GTK_WIDGET_REALIZED (widget))
6977 scroll_data.window = ALLOCATION_WINDOW (widget);
6979 scroll_data.window = NULL;
6981 scroll_data.dx = dx;
6982 scroll_data.dy = dy;
6984 adjust_allocation_recurse (widget, &scroll_data);
6988 gtk_text_view_value_changed (GtkAdjustment *adj,
6989 GtkTextView *text_view)
6996 /* Note that we oddly call this function with adj == NULL
7000 text_view->onscreen_validated = FALSE;
7002 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7003 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
7004 adj ? adj->value : 0.0));
7006 if (adj == text_view->hadjustment)
7008 dx = text_view->xoffset - (gint)adj->value;
7009 text_view->xoffset = adj->value;
7011 /* If the change is due to a size change we need
7012 * to invalidate the entire text window because there might be
7013 * right-aligned or centered text
7015 if (text_view->width_changed)
7017 if (GTK_WIDGET_REALIZED (text_view))
7018 gdk_window_invalidate_rect (text_view->text_window->bin_window, NULL, FALSE);
7020 text_view->width_changed = FALSE;
7023 else if (adj == text_view->vadjustment)
7025 dy = text_view->yoffset - (gint)adj->value;
7026 text_view->yoffset = adj->value;
7028 if (text_view->layout)
7030 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
7032 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
7034 text_view->first_para_pixels = adj->value - line_top;
7038 if (dx != 0 || dy != 0)
7042 if (GTK_WIDGET_REALIZED (text_view))
7046 if (text_view->left_window)
7047 text_window_scroll (text_view->left_window, 0, dy);
7048 if (text_view->right_window)
7049 text_window_scroll (text_view->right_window, 0, dy);
7054 if (text_view->top_window)
7055 text_window_scroll (text_view->top_window, dx, 0);
7056 if (text_view->bottom_window)
7057 text_window_scroll (text_view->bottom_window, dx, 0);
7060 /* It looks nicer to scroll the main area last, because
7061 * it takes a while, and making the side areas update
7062 * afterward emphasizes the slowness of scrolling the
7065 text_window_scroll (text_view->text_window, dx, dy);
7068 /* Children are now "moved" in the text window, poke
7069 * into widget->allocation for each child
7071 tmp_list = text_view->children;
7072 while (tmp_list != NULL)
7074 GtkTextViewChild *child = tmp_list->data;
7077 adjust_allocation (child->widget, dx, dy);
7079 tmp_list = g_slist_next (tmp_list);
7083 /* This could result in invalidation, which would install the
7084 * first_validate_idle, which would validate onscreen;
7085 * but we're going to go ahead and validate here, so
7086 * first_validate_idle shouldn't have anything to do.
7088 gtk_text_view_update_layout_width (text_view);
7090 /* note that validation of onscreen could invoke this function
7091 * recursively, by scrolling to maintain first_para, or in response
7092 * to updating the layout width, however there is no problem with
7093 * that, or shouldn't be.
7095 gtk_text_view_validate_onscreen (text_view);
7097 /* process exposes */
7098 if (GTK_WIDGET_REALIZED (text_view))
7100 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7102 if (text_view->left_window)
7103 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
7105 if (text_view->right_window)
7106 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
7108 if (text_view->top_window)
7109 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
7111 if (text_view->bottom_window)
7112 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
7114 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
7117 /* If this got installed, get rid of it, it's just a waste of time. */
7118 if (text_view->first_validate_idle != 0)
7120 g_source_remove (text_view->first_validate_idle);
7121 text_view->first_validate_idle = 0;
7124 gtk_text_view_update_im_spot_location (text_view);
7126 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7130 gtk_text_view_commit_handler (GtkIMContext *context,
7132 GtkTextView *text_view)
7134 gtk_text_view_commit_text (text_view, str);
7138 gtk_text_view_commit_text (GtkTextView *text_view,
7141 gboolean had_selection;
7143 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7145 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7148 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7149 text_view->editable);
7151 if (!strcmp (str, "\n"))
7153 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7154 text_view->editable))
7156 gtk_widget_error_bell (GTK_WIDGET (text_view));
7161 if (!had_selection && text_view->overwrite_mode)
7165 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7167 gtk_text_buffer_get_insert (get_buffer (text_view)));
7168 if (!gtk_text_iter_ends_line (&insert))
7169 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7172 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7173 text_view->editable))
7175 gtk_widget_error_bell (GTK_WIDGET (text_view));
7179 gtk_text_buffer_end_user_action (get_buffer (text_view));
7181 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7182 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7183 gtk_text_view_scroll_mark_onscreen (text_view,
7184 gtk_text_buffer_get_insert (get_buffer (text_view)));
7188 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7189 GtkTextView *text_view)
7192 PangoAttrList *attrs;
7196 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7197 gtk_text_buffer_get_insert (text_view->buffer));
7199 /* Keypress events are passed to input method even if cursor position is not editable;
7200 * so beep here if it's multi-key input sequence, input method will be reset in
7201 * key-press-event handler. */
7202 if (!gtk_text_iter_can_insert (&iter, text_view->editable))
7204 gtk_widget_error_bell (GTK_WIDGET (text_view));
7208 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7209 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
7210 pango_attr_list_unref (attrs);
7213 if (GTK_WIDGET_HAS_FOCUS (text_view))
7214 gtk_text_view_scroll_mark_onscreen (text_view,
7215 gtk_text_buffer_get_insert (get_buffer (text_view)));
7219 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7220 GtkTextView *text_view)
7227 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7228 gtk_text_buffer_get_insert (text_view->buffer));
7231 pos = gtk_text_iter_get_line_index (&start);
7232 gtk_text_iter_set_line_offset (&start, 0);
7233 gtk_text_iter_forward_to_line_end (&end);
7235 text = gtk_text_iter_get_slice (&start, &end);
7236 gtk_im_context_set_surrounding (context, text, -1, pos);
7243 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7246 GtkTextView *text_view)
7251 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7252 gtk_text_buffer_get_insert (text_view->buffer));
7255 gtk_text_iter_forward_chars (&start, offset);
7256 gtk_text_iter_forward_chars (&end, offset + n_chars);
7258 gtk_text_buffer_delete_interactive (text_view->buffer, &start, &end,
7259 text_view->editable);
7265 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7266 const GtkTextIter *location,
7270 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7271 gboolean need_reset = FALSE;
7273 if (mark == gtk_text_buffer_get_insert (buffer))
7275 text_view->virtual_cursor_x = -1;
7276 text_view->virtual_cursor_y = -1;
7277 gtk_text_view_update_im_spot_location (text_view);
7280 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7286 gtk_text_view_reset_im_context (text_view);
7290 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7291 const GParamSpec *pspec,
7294 GtkWidget *widget = GTK_WIDGET (data);
7295 GtkTargetList *view_list;
7296 GtkTargetList *buffer_list;
7299 view_list = gtk_drag_dest_get_target_list (widget);
7300 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7303 gtk_target_list_ref (view_list);
7305 view_list = gtk_target_list_new (NULL, 0);
7307 list = view_list->list;
7310 GtkTargetPair *pair = list->data;
7312 list = g_list_next (list); /* get next element before removing */
7314 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7315 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7317 gtk_target_list_remove (view_list, pair->target);
7321 for (list = buffer_list->list; list; list = g_list_next (list))
7323 GtkTargetPair *pair = list->data;
7325 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7328 gtk_drag_dest_set_target_list (widget, view_list);
7329 gtk_target_list_unref (view_list);
7333 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7338 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7339 gtk_text_buffer_get_insert (get_buffer (text_view)));
7341 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, pos, NULL);
7345 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7351 if ((x && text_view->virtual_cursor_x == -1) ||
7352 (y && text_view->virtual_cursor_y == -1))
7353 gtk_text_view_get_cursor_location (text_view, &pos);
7357 if (text_view->virtual_cursor_x != -1)
7358 *x = text_view->virtual_cursor_x;
7365 if (text_view->virtual_cursor_x != -1)
7366 *y = text_view->virtual_cursor_y;
7368 *y = pos.y + pos.height / 2;
7373 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7379 if (!text_view->layout)
7382 if (x == -1 || y == -1)
7383 gtk_text_view_get_cursor_location (text_view, &pos);
7385 text_view->virtual_cursor_x = (x == -1) ? pos.x : x;
7386 text_view->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7389 /* Quick hack of a popup menu
7392 activate_cb (GtkWidget *menuitem,
7393 GtkTextView *text_view)
7395 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7396 g_signal_emit_by_name (text_view, signal);
7400 append_action_signal (GtkTextView *text_view,
7402 const gchar *stock_id,
7403 const gchar *signal,
7406 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7408 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7409 g_signal_connect (menuitem, "activate",
7410 G_CALLBACK (activate_cb), text_view);
7412 gtk_widget_set_sensitive (menuitem, sensitive);
7414 gtk_widget_show (menuitem);
7415 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7419 gtk_text_view_select_all (GtkWidget *widget,
7422 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
7423 GtkTextBuffer *buffer;
7424 GtkTextIter start_iter, end_iter, insert;
7426 buffer = text_view->buffer;
7429 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
7430 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
7434 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
7435 gtk_text_buffer_get_insert (buffer));
7436 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
7441 select_all_cb (GtkWidget *menuitem,
7442 GtkTextView *text_view)
7444 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
7448 delete_cb (GtkTextView *text_view)
7450 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7451 text_view->editable);
7455 popup_menu_detach (GtkWidget *attach_widget,
7458 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
7462 popup_position_func (GtkMenu *menu,
7468 GtkTextView *text_view;
7470 GdkRectangle cursor_rect;
7471 GdkRectangle onscreen_rect;
7472 gint root_x, root_y;
7477 GdkRectangle monitor;
7479 text_view = GTK_TEXT_VIEW (user_data);
7480 widget = GTK_WIDGET (text_view);
7482 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
7484 screen = gtk_widget_get_screen (widget);
7486 gdk_window_get_origin (widget->window, &root_x, &root_y);
7488 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7490 gtk_text_buffer_get_insert (get_buffer (text_view)));
7492 gtk_text_view_get_iter_location (text_view,
7496 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
7498 gtk_widget_size_request (text_view->popup_menu, &req);
7500 /* can't use rectangle_intersect since cursor rect can have 0 width */
7501 if (cursor_rect.x >= onscreen_rect.x &&
7502 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
7503 cursor_rect.y >= onscreen_rect.y &&
7504 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
7506 gtk_text_view_buffer_to_window_coords (text_view,
7507 GTK_TEXT_WINDOW_WIDGET,
7508 cursor_rect.x, cursor_rect.y,
7509 &cursor_rect.x, &cursor_rect.y);
7511 *x = root_x + cursor_rect.x + cursor_rect.width;
7512 *y = root_y + cursor_rect.y + cursor_rect.height;
7516 /* Just center the menu, since cursor is offscreen. */
7517 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
7518 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
7522 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
7523 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
7525 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
7526 gtk_menu_set_monitor (menu, monitor_num);
7527 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
7529 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
7530 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
7537 GtkTextView *text_view;
7543 range_contains_editable_text (const GtkTextIter *start,
7544 const GtkTextIter *end,
7545 gboolean default_editability)
7547 GtkTextIter iter = *start;
7549 while (gtk_text_iter_compare (&iter, end) < 0)
7551 if (gtk_text_iter_editable (&iter, default_editability))
7554 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
7561 unichar_chosen_func (const char *text,
7564 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7566 gtk_text_view_commit_text (text_view, text);
7570 popup_targets_received (GtkClipboard *clipboard,
7571 GtkSelectionData *data,
7574 PopupInfo *info = user_data;
7575 GtkTextView *text_view = info->text_view;
7577 if (GTK_WIDGET_REALIZED (text_view))
7579 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
7580 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
7582 gboolean clipboard_contains_text;
7583 GtkWidget *menuitem;
7585 gboolean have_selection;
7586 gboolean can_insert;
7588 GtkTextIter sel_start, sel_end;
7589 gboolean show_input_method_menu;
7590 gboolean show_unicode_menu;
7592 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
7594 if (text_view->popup_menu)
7595 gtk_widget_destroy (text_view->popup_menu);
7597 text_view->popup_menu = gtk_menu_new ();
7599 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
7600 GTK_WIDGET (text_view),
7603 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7604 &sel_start, &sel_end);
7606 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7608 gtk_text_buffer_get_insert (get_buffer (text_view)));
7610 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
7612 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
7614 range_contains_editable_text (&sel_start, &sel_end,
7615 text_view->editable));
7616 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
7618 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
7619 can_insert && clipboard_contains_text);
7621 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
7622 gtk_widget_set_sensitive (menuitem,
7624 range_contains_editable_text (&sel_start, &sel_end,
7625 text_view->editable));
7626 g_signal_connect_swapped (menuitem, "activate",
7627 G_CALLBACK (delete_cb), text_view);
7628 gtk_widget_show (menuitem);
7629 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7631 menuitem = gtk_separator_menu_item_new ();
7632 gtk_widget_show (menuitem);
7633 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7635 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
7636 g_signal_connect (menuitem, "activate",
7637 G_CALLBACK (select_all_cb), text_view);
7638 gtk_widget_show (menuitem);
7639 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7641 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
7642 "gtk-show-input-method-menu", &show_input_method_menu,
7643 "gtk-show-unicode-menu", &show_unicode_menu,
7646 if (show_input_method_menu || show_unicode_menu)
7648 menuitem = gtk_separator_menu_item_new ();
7649 gtk_widget_show (menuitem);
7650 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7653 if (show_input_method_menu)
7655 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
7656 gtk_widget_show (menuitem);
7657 gtk_widget_set_sensitive (menuitem, can_insert);
7659 submenu = gtk_menu_new ();
7660 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7661 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7663 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
7664 GTK_MENU_SHELL (submenu));
7667 if (show_unicode_menu)
7669 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
7670 gtk_widget_show (menuitem);
7671 gtk_widget_set_sensitive (menuitem, can_insert);
7673 submenu = gtk_menu_new ();
7674 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7675 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7677 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
7678 unichar_chosen_func,
7682 g_signal_emit (text_view,
7683 signals[POPULATE_POPUP],
7685 text_view->popup_menu);
7688 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7690 info->button, info->time);
7693 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7694 popup_position_func, text_view,
7695 0, gtk_get_current_event_time ());
7696 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
7700 g_object_unref (text_view);
7705 gtk_text_view_do_popup (GtkTextView *text_view,
7706 GdkEventButton *event)
7708 PopupInfo *info = g_new (PopupInfo, 1);
7710 /* In order to know what entries we should make sensitive, we
7711 * ask for the current targets of the clipboard, and when
7712 * we get them, then we actually pop up the menu.
7714 info->text_view = g_object_ref (text_view);
7718 info->button = event->button;
7719 info->time = event->time;
7724 info->time = gtk_get_current_event_time ();
7727 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
7728 GDK_SELECTION_CLIPBOARD),
7729 gdk_atom_intern_static_string ("TARGETS"),
7730 popup_targets_received,
7735 gtk_text_view_popup_menu (GtkWidget *widget)
7737 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
7741 /* Child GdkWindows */
7744 static GtkTextWindow*
7745 text_window_new (GtkTextWindowType type,
7748 gint height_request)
7752 win = g_new (GtkTextWindow, 1);
7755 win->widget = widget;
7757 win->bin_window = NULL;
7758 win->requisition.width = width_request;
7759 win->requisition.height = height_request;
7760 win->allocation.width = width_request;
7761 win->allocation.height = height_request;
7762 win->allocation.x = 0;
7763 win->allocation.y = 0;
7769 text_window_free (GtkTextWindow *win)
7772 text_window_unrealize (win);
7778 text_window_realize (GtkTextWindow *win,
7781 GdkWindowAttr attributes;
7782 gint attributes_mask;
7785 attributes.window_type = GDK_WINDOW_CHILD;
7786 attributes.x = win->allocation.x;
7787 attributes.y = win->allocation.y;
7788 attributes.width = win->allocation.width;
7789 attributes.height = win->allocation.height;
7790 attributes.wclass = GDK_INPUT_OUTPUT;
7791 attributes.visual = gtk_widget_get_visual (win->widget);
7792 attributes.colormap = gtk_widget_get_colormap (win->widget);
7793 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
7795 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
7797 win->window = gdk_window_new (widget->window,
7801 gdk_window_set_back_pixmap (win->window, NULL, FALSE);
7803 gdk_window_show (win->window);
7804 gdk_window_set_user_data (win->window, win->widget);
7805 gdk_window_lower (win->window);
7809 attributes.width = win->allocation.width;
7810 attributes.height = win->allocation.height;
7811 attributes.event_mask = (GDK_EXPOSURE_MASK |
7813 GDK_KEY_PRESS_MASK |
7814 GDK_BUTTON_PRESS_MASK |
7815 GDK_BUTTON_RELEASE_MASK |
7816 GDK_POINTER_MOTION_MASK |
7817 GDK_POINTER_MOTION_HINT_MASK |
7818 gtk_widget_get_events (win->widget));
7820 win->bin_window = gdk_window_new (win->window,
7824 gdk_window_show (win->bin_window);
7825 gdk_window_set_user_data (win->bin_window, win->widget);
7827 if (win->type == GTK_TEXT_WINDOW_TEXT)
7829 if (GTK_WIDGET_IS_SENSITIVE (widget))
7832 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (widget->window),
7834 gdk_window_set_cursor (win->bin_window, cursor);
7835 gdk_cursor_unref (cursor);
7838 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->im_context,
7842 gdk_window_set_background (win->bin_window,
7843 &widget->style->base[GTK_WIDGET_STATE (widget)]);
7847 gdk_window_set_background (win->bin_window,
7848 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
7851 g_object_set_qdata (G_OBJECT (win->window),
7852 g_quark_from_static_string ("gtk-text-view-text-window"),
7855 g_object_set_qdata (G_OBJECT (win->bin_window),
7856 g_quark_from_static_string ("gtk-text-view-text-window"),
7861 text_window_unrealize (GtkTextWindow *win)
7863 if (win->type == GTK_TEXT_WINDOW_TEXT)
7865 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
7869 gdk_window_set_user_data (win->window, NULL);
7870 gdk_window_set_user_data (win->bin_window, NULL);
7871 gdk_window_destroy (win->bin_window);
7872 gdk_window_destroy (win->window);
7874 win->bin_window = NULL;
7878 text_window_size_allocate (GtkTextWindow *win,
7881 win->allocation = *rect;
7885 gdk_window_move_resize (win->window,
7887 rect->width, rect->height);
7889 gdk_window_resize (win->bin_window,
7890 rect->width, rect->height);
7895 text_window_scroll (GtkTextWindow *win,
7899 if (dx != 0 || dy != 0)
7901 gdk_window_scroll (win->bin_window, dx, dy);
7906 text_window_invalidate_rect (GtkTextWindow *win,
7909 GdkRectangle window_rect;
7911 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
7918 window_rect.width = rect->width;
7919 window_rect.height = rect->height;
7921 /* Adjust the rect as appropriate */
7925 case GTK_TEXT_WINDOW_TEXT:
7928 case GTK_TEXT_WINDOW_LEFT:
7929 case GTK_TEXT_WINDOW_RIGHT:
7931 window_rect.width = win->allocation.width;
7934 case GTK_TEXT_WINDOW_TOP:
7935 case GTK_TEXT_WINDOW_BOTTOM:
7937 window_rect.height = win->allocation.height;
7941 g_warning ("%s: bug!", G_STRFUNC);
7946 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
7950 cairo_t *cr = gdk_cairo_create (win->bin_window);
7951 gdk_cairo_rectangle (cr, &window_rect);
7952 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
7960 text_window_invalidate_cursors (GtkTextWindow *win)
7962 GtkTextView *text_view = GTK_TEXT_VIEW (win->widget);
7964 GdkRectangle strong;
7966 gboolean draw_arrow;
7967 gfloat cursor_aspect_ratio;
7971 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7972 gtk_text_buffer_get_insert (text_view->buffer));
7974 if (_gtk_text_layout_get_block_cursor (text_view->layout, &strong))
7976 text_window_invalidate_rect (win, &strong);
7980 gtk_text_layout_get_cursor_locations (text_view->layout, &iter,
7983 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
7984 * ignoring the text direction be exposing both sides of the cursor
7987 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
7989 gtk_widget_style_get (win->widget,
7990 "cursor-aspect-ratio", &cursor_aspect_ratio,
7993 stem_width = strong.height * cursor_aspect_ratio + 1;
7994 arrow_width = stem_width + 1;
7996 strong.width = stem_width;
7998 /* round up to the next even number */
8002 strong.x -= stem_width / 2;
8003 strong.width += stem_width;
8007 strong.x -= arrow_width;
8008 strong.width += arrow_width * 2;
8011 text_window_invalidate_rect (win, &strong);
8013 if (draw_arrow) /* == have weak */
8015 stem_width = weak.height * cursor_aspect_ratio + 1;
8016 arrow_width = stem_width + 1;
8018 weak.width = stem_width;
8020 /* round up to the next even number */
8024 weak.x -= stem_width / 2;
8025 weak.width += stem_width;
8027 weak.x -= arrow_width;
8028 weak.width += arrow_width * 2;
8030 text_window_invalidate_rect (win, &weak);
8035 text_window_get_width (GtkTextWindow *win)
8037 return win->allocation.width;
8041 text_window_get_height (GtkTextWindow *win)
8043 return win->allocation.height;
8050 * gtk_text_view_get_window:
8051 * @text_view: a #GtkTextView
8052 * @win: window to get
8054 * Retrieves the #GdkWindow corresponding to an area of the text view;
8055 * possible windows include the overall widget window, child windows
8056 * on the left, right, top, bottom, and the window that displays the
8057 * text buffer. Windows are %NULL and nonexistent if their width or
8058 * height is 0, and are nonexistent before the widget has been
8061 * Return value: a #GdkWindow, or %NULL
8064 gtk_text_view_get_window (GtkTextView *text_view,
8065 GtkTextWindowType win)
8067 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8071 case GTK_TEXT_WINDOW_WIDGET:
8072 return GTK_WIDGET (text_view)->window;
8075 case GTK_TEXT_WINDOW_TEXT:
8076 return text_view->text_window->bin_window;
8079 case GTK_TEXT_WINDOW_LEFT:
8080 if (text_view->left_window)
8081 return text_view->left_window->bin_window;
8086 case GTK_TEXT_WINDOW_RIGHT:
8087 if (text_view->right_window)
8088 return text_view->right_window->bin_window;
8093 case GTK_TEXT_WINDOW_TOP:
8094 if (text_view->top_window)
8095 return text_view->top_window->bin_window;
8100 case GTK_TEXT_WINDOW_BOTTOM:
8101 if (text_view->bottom_window)
8102 return text_view->bottom_window->bin_window;
8107 case GTK_TEXT_WINDOW_PRIVATE:
8108 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8113 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8118 * gtk_text_view_get_window_type:
8119 * @text_view: a #GtkTextView
8120 * @window: a window type
8122 * Usually used to find out which window an event corresponds to.
8123 * If you connect to an event signal on @text_view, this function
8124 * should be called on <literal>event->window</literal> to
8125 * see which window it was.
8127 * Return value: the window type.
8130 gtk_text_view_get_window_type (GtkTextView *text_view,
8135 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8136 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8138 if (window == GTK_WIDGET (text_view)->window)
8139 return GTK_TEXT_WINDOW_WIDGET;
8141 win = g_object_get_qdata (G_OBJECT (window),
8142 g_quark_try_string ("gtk-text-view-text-window"));
8148 return GTK_TEXT_WINDOW_PRIVATE;
8153 buffer_to_widget (GtkTextView *text_view,
8161 *window_x = buffer_x - text_view->xoffset;
8162 *window_x += text_view->text_window->allocation.x;
8167 *window_y = buffer_y - text_view->yoffset;
8168 *window_y += text_view->text_window->allocation.y;
8173 widget_to_text_window (GtkTextWindow *win,
8180 *window_x = widget_x - win->allocation.x;
8183 *window_y = widget_y - win->allocation.y;
8187 buffer_to_text_window (GtkTextView *text_view,
8196 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8197 "for a nonexistent or private child window of GtkTextView");
8201 buffer_to_widget (text_view,
8203 window_x, window_y);
8205 widget_to_text_window (win,
8206 window_x ? *window_x : 0,
8207 window_y ? *window_y : 0,
8213 * gtk_text_view_buffer_to_window_coords:
8214 * @text_view: a #GtkTextView
8215 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8216 * @buffer_x: buffer x coordinate
8217 * @buffer_y: buffer y coordinate
8218 * @window_x: window x coordinate return location
8219 * @window_y: window y coordinate return location
8221 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8222 * @win, and stores the result in (@window_x, @window_y).
8224 * Note that you can't convert coordinates for a nonexisting window (see
8225 * gtk_text_view_set_border_window_size()).
8228 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8229 GtkTextWindowType win,
8235 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8239 case GTK_TEXT_WINDOW_WIDGET:
8240 buffer_to_widget (text_view,
8242 window_x, window_y);
8245 case GTK_TEXT_WINDOW_TEXT:
8247 *window_x = buffer_x - text_view->xoffset;
8249 *window_y = buffer_y - text_view->yoffset;
8252 case GTK_TEXT_WINDOW_LEFT:
8253 buffer_to_text_window (text_view,
8254 text_view->left_window,
8256 window_x, window_y);
8259 case GTK_TEXT_WINDOW_RIGHT:
8260 buffer_to_text_window (text_view,
8261 text_view->right_window,
8263 window_x, window_y);
8266 case GTK_TEXT_WINDOW_TOP:
8267 buffer_to_text_window (text_view,
8268 text_view->top_window,
8270 window_x, window_y);
8273 case GTK_TEXT_WINDOW_BOTTOM:
8274 buffer_to_text_window (text_view,
8275 text_view->bottom_window,
8277 window_x, window_y);
8280 case GTK_TEXT_WINDOW_PRIVATE:
8281 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8285 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8291 widget_to_buffer (GtkTextView *text_view,
8299 *buffer_x = widget_x + text_view->xoffset;
8300 *buffer_x -= text_view->text_window->allocation.x;
8305 *buffer_y = widget_y + text_view->yoffset;
8306 *buffer_y -= text_view->text_window->allocation.y;
8311 text_window_to_widget (GtkTextWindow *win,
8318 *widget_x = window_x + win->allocation.x;
8321 *widget_y = window_y + win->allocation.y;
8325 text_window_to_buffer (GtkTextView *text_view,
8334 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8335 "coordinates for a nonexistent child window.");
8339 text_window_to_widget (win,
8345 widget_to_buffer (text_view,
8346 buffer_x ? *buffer_x : 0,
8347 buffer_y ? *buffer_y : 0,
8353 * gtk_text_view_window_to_buffer_coords:
8354 * @text_view: a #GtkTextView
8355 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8356 * @window_x: window x coordinate
8357 * @window_y: window y coordinate
8358 * @buffer_x: buffer x coordinate return location
8359 * @buffer_y: buffer y coordinate return location
8361 * Converts coordinates on the window identified by @win to buffer
8362 * coordinates, storing the result in (@buffer_x,@buffer_y).
8364 * Note that you can't convert coordinates for a nonexisting window (see
8365 * gtk_text_view_set_border_window_size()).
8368 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8369 GtkTextWindowType win,
8375 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8379 case GTK_TEXT_WINDOW_WIDGET:
8380 widget_to_buffer (text_view,
8382 buffer_x, buffer_y);
8385 case GTK_TEXT_WINDOW_TEXT:
8387 *buffer_x = window_x + text_view->xoffset;
8389 *buffer_y = window_y + text_view->yoffset;
8392 case GTK_TEXT_WINDOW_LEFT:
8393 text_window_to_buffer (text_view,
8394 text_view->left_window,
8396 buffer_x, buffer_y);
8399 case GTK_TEXT_WINDOW_RIGHT:
8400 text_window_to_buffer (text_view,
8401 text_view->right_window,
8403 buffer_x, buffer_y);
8406 case GTK_TEXT_WINDOW_TOP:
8407 text_window_to_buffer (text_view,
8408 text_view->top_window,
8410 buffer_x, buffer_y);
8413 case GTK_TEXT_WINDOW_BOTTOM:
8414 text_window_to_buffer (text_view,
8415 text_view->bottom_window,
8417 buffer_x, buffer_y);
8420 case GTK_TEXT_WINDOW_PRIVATE:
8421 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8425 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8431 set_window_width (GtkTextView *text_view,
8433 GtkTextWindowType type,
8434 GtkTextWindow **winp)
8440 text_window_free (*winp);
8442 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8449 *winp = text_window_new (type,
8450 GTK_WIDGET (text_view),
8452 /* if the widget is already realized we need to realize the child manually */
8453 if (GTK_WIDGET_REALIZED (text_view))
8454 text_window_realize (*winp, GTK_WIDGET (text_view));
8458 if ((*winp)->requisition.width == width)
8461 (*winp)->requisition.width = width;
8464 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8470 set_window_height (GtkTextView *text_view,
8472 GtkTextWindowType type,
8473 GtkTextWindow **winp)
8479 text_window_free (*winp);
8481 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8488 *winp = text_window_new (type,
8489 GTK_WIDGET (text_view),
8492 /* if the widget is already realized we need to realize the child manually */
8493 if (GTK_WIDGET_REALIZED (text_view))
8494 text_window_realize (*winp, GTK_WIDGET (text_view));
8498 if ((*winp)->requisition.height == height)
8501 (*winp)->requisition.height = height;
8504 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8509 * gtk_text_view_set_border_window_size:
8510 * @text_view: a #GtkTextView
8511 * @type: window to affect
8512 * @size: width or height of the window
8514 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
8515 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
8516 * Automatically destroys the corresponding window if the size is set
8517 * to 0, and creates the window if the size is set to non-zero. This
8518 * function can only be used for the "border windows," it doesn't work
8519 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
8520 * #GTK_TEXT_WINDOW_PRIVATE.
8523 gtk_text_view_set_border_window_size (GtkTextView *text_view,
8524 GtkTextWindowType type,
8528 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8529 g_return_if_fail (size >= 0);
8533 case GTK_TEXT_WINDOW_LEFT:
8534 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
8535 &text_view->left_window);
8538 case GTK_TEXT_WINDOW_RIGHT:
8539 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
8540 &text_view->right_window);
8543 case GTK_TEXT_WINDOW_TOP:
8544 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
8545 &text_view->top_window);
8548 case GTK_TEXT_WINDOW_BOTTOM:
8549 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
8550 &text_view->bottom_window);
8554 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
8560 * gtk_text_view_get_border_window_size:
8561 * @text_view: a #GtkTextView
8562 * @type: window to return size from
8564 * Gets the width of the specified border window. See
8565 * gtk_text_view_set_border_window_size().
8567 * Return value: width of window
8570 gtk_text_view_get_border_window_size (GtkTextView *text_view,
8571 GtkTextWindowType type)
8573 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8577 case GTK_TEXT_WINDOW_LEFT:
8578 if (text_view->left_window)
8579 return text_view->left_window->requisition.width;
8582 case GTK_TEXT_WINDOW_RIGHT:
8583 if (text_view->right_window)
8584 return text_view->right_window->requisition.width;
8587 case GTK_TEXT_WINDOW_TOP:
8588 if (text_view->top_window)
8589 return text_view->top_window->requisition.height;
8592 case GTK_TEXT_WINDOW_BOTTOM:
8593 if (text_view->bottom_window)
8594 return text_view->bottom_window->requisition.height;
8598 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
8609 static GtkTextViewChild*
8610 text_view_child_new_anchored (GtkWidget *child,
8611 GtkTextChildAnchor *anchor,
8612 GtkTextLayout *layout)
8614 GtkTextViewChild *vc;
8616 vc = g_new (GtkTextViewChild, 1);
8618 vc->type = GTK_TEXT_WINDOW_PRIVATE;
8620 vc->anchor = anchor;
8622 vc->from_top_of_line = 0;
8623 vc->from_left_of_buffer = 0;
8625 g_object_ref (vc->widget);
8626 g_object_ref (vc->anchor);
8628 g_object_set_data (G_OBJECT (child),
8629 I_("gtk-text-view-child"),
8632 gtk_text_child_anchor_register_child (anchor, child, layout);
8637 static GtkTextViewChild*
8638 text_view_child_new_window (GtkWidget *child,
8639 GtkTextWindowType type,
8643 GtkTextViewChild *vc;
8645 vc = g_new (GtkTextViewChild, 1);
8650 vc->from_top_of_line = 0;
8651 vc->from_left_of_buffer = 0;
8653 g_object_ref (vc->widget);
8659 g_object_set_data (G_OBJECT (child),
8660 I_("gtk-text-view-child"),
8667 text_view_child_free (GtkTextViewChild *child)
8669 g_object_set_data (G_OBJECT (child->widget),
8670 I_("gtk-text-view-child"), NULL);
8674 gtk_text_child_anchor_unregister_child (child->anchor,
8676 g_object_unref (child->anchor);
8679 g_object_unref (child->widget);
8685 text_view_child_set_parent_window (GtkTextView *text_view,
8686 GtkTextViewChild *vc)
8689 gtk_widget_set_parent_window (vc->widget,
8690 text_view->text_window->bin_window);
8694 window = gtk_text_view_get_window (text_view,
8696 gtk_widget_set_parent_window (vc->widget, window);
8701 add_child (GtkTextView *text_view,
8702 GtkTextViewChild *vc)
8704 text_view->children = g_slist_prepend (text_view->children,
8707 if (GTK_WIDGET_REALIZED (text_view))
8708 text_view_child_set_parent_window (text_view, vc);
8710 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
8714 * gtk_text_view_add_child_at_anchor:
8715 * @text_view: a #GtkTextView
8716 * @child: a #GtkWidget
8717 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
8719 * Adds a child widget in the text buffer, at the given @anchor.
8722 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
8724 GtkTextChildAnchor *anchor)
8726 GtkTextViewChild *vc;
8728 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8729 g_return_if_fail (GTK_IS_WIDGET (child));
8730 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
8731 g_return_if_fail (child->parent == NULL);
8733 gtk_text_view_ensure_layout (text_view);
8735 vc = text_view_child_new_anchored (child, anchor,
8738 add_child (text_view, vc);
8740 g_assert (vc->widget == child);
8741 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8745 * gtk_text_view_add_child_in_window:
8746 * @text_view: a #GtkTextView
8747 * @child: a #GtkWidget
8748 * @which_window: which window the child should appear in
8749 * @xpos: X position of child in window coordinates
8750 * @ypos: Y position of child in window coordinates
8752 * Adds a child at fixed coordinates in one of the text widget's
8753 * windows. The window must have nonzero size (see
8754 * gtk_text_view_set_border_window_size()). Note that the child
8755 * coordinates are given relative to the #GdkWindow in question, and
8756 * that these coordinates have no sane relationship to scrolling. When
8757 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
8758 * irrelevant, the child floats above all scrollable areas. But when
8759 * placing a child in one of the scrollable windows (border windows or
8760 * text window), you'll need to compute the child's correct position
8761 * in buffer coordinates any time scrolling occurs or buffer changes
8762 * occur, and then call gtk_text_view_move_child() to update the
8763 * child's position. Unfortunately there's no good way to detect that
8764 * scrolling has occurred, using the current API; a possible hack
8765 * would be to update all child positions when the scroll adjustments
8766 * change or the text buffer changes. See bug 64518 on
8767 * bugzilla.gnome.org for status of fixing this issue.
8770 gtk_text_view_add_child_in_window (GtkTextView *text_view,
8772 GtkTextWindowType which_window,
8776 GtkTextViewChild *vc;
8778 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8779 g_return_if_fail (GTK_IS_WIDGET (child));
8780 g_return_if_fail (child->parent == NULL);
8782 vc = text_view_child_new_window (child, which_window,
8785 add_child (text_view, vc);
8787 g_assert (vc->widget == child);
8788 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8792 * gtk_text_view_move_child:
8793 * @text_view: a #GtkTextView
8794 * @child: child widget already added to the text view
8795 * @xpos: new X position in window coordinates
8796 * @ypos: new Y position in window coordinates
8798 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
8801 gtk_text_view_move_child (GtkTextView *text_view,
8806 GtkTextViewChild *vc;
8808 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8809 g_return_if_fail (GTK_IS_WIDGET (child));
8810 g_return_if_fail (child->parent == (GtkWidget*) text_view);
8812 vc = g_object_get_data (G_OBJECT (child),
8813 "gtk-text-view-child");
8815 g_assert (vc != NULL);
8817 if (vc->x == xpos &&
8824 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
8825 gtk_widget_queue_resize (child);
8829 /* Iterator operations */
8832 * gtk_text_view_forward_display_line:
8833 * @text_view: a #GtkTextView
8834 * @iter: a #GtkTextIter
8836 * Moves the given @iter forward by one display (wrapped) line.
8837 * A display line is different from a paragraph. Paragraphs are
8838 * separated by newlines or other paragraph separator characters.
8839 * Display lines are created by line-wrapping a paragraph. If
8840 * wrapping is turned off, display lines and paragraphs will be the
8841 * same. Display lines are divided differently for each view, since
8842 * they depend on the view's width; paragraphs are the same in all
8843 * views, since they depend on the contents of the #GtkTextBuffer.
8845 * Return value: %TRUE if @iter was moved and is not on the end iterator
8848 gtk_text_view_forward_display_line (GtkTextView *text_view,
8851 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8852 g_return_val_if_fail (iter != NULL, FALSE);
8854 gtk_text_view_ensure_layout (text_view);
8856 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
8860 * gtk_text_view_backward_display_line:
8861 * @text_view: a #GtkTextView
8862 * @iter: a #GtkTextIter
8864 * Moves the given @iter backward by one display (wrapped) line.
8865 * A display line is different from a paragraph. Paragraphs are
8866 * separated by newlines or other paragraph separator characters.
8867 * Display lines are created by line-wrapping a paragraph. If
8868 * wrapping is turned off, display lines and paragraphs will be the
8869 * same. Display lines are divided differently for each view, since
8870 * they depend on the view's width; paragraphs are the same in all
8871 * views, since they depend on the contents of the #GtkTextBuffer.
8873 * Return value: %TRUE if @iter was moved and is not on the end iterator
8876 gtk_text_view_backward_display_line (GtkTextView *text_view,
8879 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8880 g_return_val_if_fail (iter != NULL, FALSE);
8882 gtk_text_view_ensure_layout (text_view);
8884 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
8888 * gtk_text_view_forward_display_line_end:
8889 * @text_view: a #GtkTextView
8890 * @iter: a #GtkTextIter
8892 * Moves the given @iter forward to the next display line end.
8893 * A display line is different from a paragraph. Paragraphs are
8894 * separated by newlines or other paragraph separator characters.
8895 * Display lines are created by line-wrapping a paragraph. If
8896 * wrapping is turned off, display lines and paragraphs will be the
8897 * same. Display lines are divided differently for each view, since
8898 * they depend on the view's width; paragraphs are the same in all
8899 * views, since they depend on the contents of the #GtkTextBuffer.
8901 * Return value: %TRUE if @iter was moved and is not on the end iterator
8904 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
8907 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8908 g_return_val_if_fail (iter != NULL, FALSE);
8910 gtk_text_view_ensure_layout (text_view);
8912 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
8916 * gtk_text_view_backward_display_line_start:
8917 * @text_view: a #GtkTextView
8918 * @iter: a #GtkTextIter
8920 * Moves the given @iter backward to the next display line start.
8921 * A display line is different from a paragraph. Paragraphs are
8922 * separated by newlines or other paragraph separator characters.
8923 * Display lines are created by line-wrapping a paragraph. If
8924 * wrapping is turned off, display lines and paragraphs will be the
8925 * same. Display lines are divided differently for each view, since
8926 * they depend on the view's width; paragraphs are the same in all
8927 * views, since they depend on the contents of the #GtkTextBuffer.
8929 * Return value: %TRUE if @iter was moved and is not on the end iterator
8932 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
8935 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8936 g_return_val_if_fail (iter != NULL, FALSE);
8938 gtk_text_view_ensure_layout (text_view);
8940 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
8944 * gtk_text_view_starts_display_line:
8945 * @text_view: a #GtkTextView
8946 * @iter: a #GtkTextIter
8948 * Determines whether @iter is at the start of a display line.
8949 * See gtk_text_view_forward_display_line() for an explanation of
8950 * display lines vs. paragraphs.
8952 * Return value: %TRUE if @iter begins a wrapped line
8955 gtk_text_view_starts_display_line (GtkTextView *text_view,
8956 const GtkTextIter *iter)
8958 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8959 g_return_val_if_fail (iter != NULL, FALSE);
8961 gtk_text_view_ensure_layout (text_view);
8963 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
8967 * gtk_text_view_move_visually:
8968 * @text_view: a #GtkTextView
8969 * @iter: a #GtkTextIter
8970 * @count: number of characters to move (negative moves left,
8971 * positive moves right)
8973 * Move the iterator a given number of characters visually, treating
8974 * it as the strong cursor position. If @count is positive, then the
8975 * new strong cursor position will be @count positions to the right of
8976 * the old cursor position. If @count is negative then the new strong
8977 * cursor position will be @count positions to the left of the old
8980 * In the presence of bi-directional text, the correspondence
8981 * between logical and visual order will depend on the direction
8982 * of the current run, and there may be jumps when the cursor
8983 * is moved off of the end of a run.
8985 * Return value: %TRUE if @iter moved and is not on the end iterator
8988 gtk_text_view_move_visually (GtkTextView *text_view,
8992 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8993 g_return_val_if_fail (iter != NULL, FALSE);
8995 gtk_text_view_ensure_layout (text_view);
8997 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
9000 #define __GTK_TEXT_VIEW_C__
9001 #include "gtkaliasdef.c"