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 &&
4099 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4101 /* use overall editability not can_insert, more predictable for users */
4102 else if (text_view->editable &&
4103 (event->keyval == GDK_Return ||
4104 event->keyval == GDK_ISO_Enter ||
4105 event->keyval == GDK_KP_Enter))
4107 /* this won't actually insert the newline if the cursor isn't
4110 gtk_text_view_reset_im_context (text_view);
4111 gtk_text_view_commit_text (text_view, "\n");
4116 /* Pass through Tab as literal tab, unless Control is held down */
4117 else if ((event->keyval == GDK_Tab ||
4118 event->keyval == GDK_KP_Tab ||
4119 event->keyval == GDK_ISO_Left_Tab) &&
4120 !(event->state & GDK_CONTROL_MASK))
4122 /* If the text widget isn't editable overall, or if the application
4123 * has turned off "accepts_tab", move the focus instead
4125 if (text_view->accepts_tab && text_view->editable)
4127 gtk_text_view_reset_im_context (text_view);
4128 gtk_text_view_commit_text (text_view, "\t");
4132 g_signal_emit_by_name (text_view, "move-focus",
4133 (event->state & GDK_SHIFT_MASK) ?
4134 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4142 gtk_text_view_obscure_mouse_cursor (text_view);
4144 gtk_text_view_reset_blink_time (text_view);
4145 gtk_text_view_pend_cursor_blink (text_view);
4151 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4153 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4154 GtkTextMark *insert;
4157 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
4160 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4161 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4162 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
4163 gtk_im_context_filter_keypress (text_view->im_context, event))
4165 text_view->need_im_reset = TRUE;
4169 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4173 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4175 GtkTextView *text_view;
4177 text_view = GTK_TEXT_VIEW (widget);
4179 gtk_widget_grab_focus (widget);
4181 if (event->window != text_view->text_window->bin_window)
4183 /* Remove selection if any. */
4184 gtk_text_view_unselect (text_view);
4188 gtk_text_view_reset_blink_time (text_view);
4192 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4193 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4194 else if (event->button == 3)
4195 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4198 if (event->type == GDK_BUTTON_PRESS)
4200 gtk_text_view_reset_im_context (text_view);
4202 if (event->button == 1)
4204 /* If we're in the selection, start a drag copy/move of the
4205 * selection; otherwise, start creating a new selection.
4208 GtkTextIter start, end;
4210 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4212 event->x + text_view->xoffset,
4213 event->y + text_view->yoffset);
4215 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4217 gtk_text_iter_in_range (&iter, &start, &end))
4219 text_view->drag_start_x = event->x;
4220 text_view->drag_start_y = event->y;
4221 text_view->pending_place_cursor_button = event->button;
4225 gtk_text_view_start_selection_drag (text_view, &iter, event);
4230 else if (event->button == 2)
4234 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4236 event->x + text_view->xoffset,
4237 event->y + text_view->yoffset);
4239 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4240 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4242 text_view->editable);
4245 else if (event->button == 3)
4247 gtk_text_view_do_popup (text_view, event);
4251 else if ((event->type == GDK_2BUTTON_PRESS ||
4252 event->type == GDK_3BUTTON_PRESS) &&
4257 gtk_text_view_end_selection_drag (text_view);
4259 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4261 event->x + text_view->xoffset,
4262 event->y + text_view->yoffset);
4264 gtk_text_view_start_selection_drag (text_view, &iter, event);
4272 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4274 GtkTextView *text_view;
4276 text_view = GTK_TEXT_VIEW (widget);
4278 if (event->window != text_view->text_window->bin_window)
4281 if (event->button == 1)
4283 if (text_view->drag_start_x >= 0)
4285 text_view->drag_start_x = -1;
4286 text_view->drag_start_y = -1;
4289 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4291 else if (text_view->pending_place_cursor_button == event->button)
4295 /* Unselect everything; we clicked inside selection, but
4296 * didn't move by the drag threshold, so just clear selection
4299 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4301 event->x + text_view->xoffset,
4302 event->y + text_view->yoffset);
4304 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4305 gtk_text_view_check_cursor_blink (text_view);
4307 text_view->pending_place_cursor_button = 0;
4317 keymap_direction_changed (GdkKeymap *keymap,
4318 GtkTextView *text_view)
4320 gtk_text_view_check_keymap_direction (text_view);
4324 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4326 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4328 gtk_widget_queue_draw (widget);
4330 DV(g_print (G_STRLOC": focus_in_event\n"));
4332 gtk_text_view_reset_blink_time (text_view);
4334 if (text_view->cursor_visible && text_view->layout)
4336 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4337 gtk_text_view_check_cursor_blink (text_view);
4340 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4341 "direction_changed",
4342 G_CALLBACK (keymap_direction_changed), text_view);
4343 gtk_text_view_check_keymap_direction (text_view);
4345 if (text_view->editable)
4347 text_view->need_im_reset = TRUE;
4348 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4355 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4357 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4359 gtk_text_view_end_selection_drag (text_view);
4361 gtk_widget_queue_draw (widget);
4363 DV(g_print (G_STRLOC": focus_out_event\n"));
4365 if (text_view->cursor_visible && text_view->layout)
4367 gtk_text_view_check_cursor_blink (text_view);
4368 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4371 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4372 keymap_direction_changed,
4375 if (text_view->editable)
4377 text_view->need_im_reset = TRUE;
4378 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4385 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4387 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4389 gtk_text_view_unobscure_mouse_cursor (text_view);
4391 if (event->window == text_view->text_window->bin_window &&
4392 text_view->drag_start_x >= 0)
4397 gdk_event_request_motions (event);
4399 if (gtk_drag_check_threshold (widget,
4400 text_view->drag_start_x,
4401 text_view->drag_start_y,
4405 gint buffer_x, buffer_y;
4407 gtk_text_view_window_to_buffer_coords (text_view,
4408 GTK_TEXT_WINDOW_TEXT,
4409 text_view->drag_start_x,
4410 text_view->drag_start_y,
4414 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4416 buffer_x, buffer_y);
4418 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4427 gtk_text_view_paint (GtkWidget *widget,
4429 GdkEventExpose *event)
4431 GtkTextView *text_view;
4432 GList *child_exposes;
4436 text_view = GTK_TEXT_VIEW (widget);
4438 g_return_if_fail (text_view->layout != NULL);
4439 g_return_if_fail (text_view->xoffset >= 0);
4440 g_return_if_fail (text_view->yoffset >= 0);
4442 while (text_view->first_validate_idle != 0)
4444 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4445 text_view->first_validate_idle));
4446 gtk_text_view_flush_first_validate (text_view);
4449 /* More regions could have become invalid in the above loop */
4450 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4455 gdk_region_get_clipbox (updates, &rect);
4457 gdk_rectangle_union (area, &rect, area);
4459 gdk_region_destroy (updates);
4462 if (!text_view->onscreen_validated)
4464 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.");
4465 g_assert_not_reached ();
4469 printf ("painting %d,%d %d x %d\n",
4471 area->width, area->height);
4474 child_exposes = NULL;
4475 gtk_text_layout_draw (text_view->layout,
4477 text_view->text_window->bin_window,
4482 area->width, area->height,
4485 tmp_list = child_exposes;
4486 while (tmp_list != NULL)
4488 GtkWidget *child = tmp_list->data;
4490 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4494 g_object_unref (child);
4496 tmp_list = tmp_list->next;
4499 g_list_free (child_exposes);
4503 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4507 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4508 GTK_TEXT_WINDOW_TEXT))
4510 DV(g_print (">Exposed ("G_STRLOC")\n"));
4511 gtk_text_view_paint (widget, &event->area, event);
4514 if (event->window == widget->window)
4515 gtk_text_view_draw_focus (widget);
4517 /* Propagate exposes to all unanchored children.
4518 * Anchored children are handled in gtk_text_view_paint().
4520 tmp_list = GTK_TEXT_VIEW (widget)->children;
4521 while (tmp_list != NULL)
4523 GtkTextViewChild *vc = tmp_list->data;
4525 /* propagate_expose checks that event->window matches
4529 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4533 tmp_list = tmp_list->next;
4540 gtk_text_view_draw_focus (GtkWidget *widget)
4542 gboolean interior_focus;
4544 /* We clear the focus if we are in interior focus mode. */
4545 gtk_widget_style_get (widget,
4546 "interior-focus", &interior_focus,
4549 if (GTK_WIDGET_DRAWABLE (widget))
4551 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4553 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4554 NULL, widget, "textview",
4556 widget->allocation.width,
4557 widget->allocation.height);
4561 gdk_window_clear (widget->window);
4567 gtk_text_view_focus (GtkWidget *widget,
4568 GtkDirectionType direction)
4570 GtkContainer *container;
4573 container = GTK_CONTAINER (widget);
4575 if (!gtk_widget_is_focus (widget) &&
4576 container->focus_child == NULL)
4578 gtk_widget_grab_focus (widget);
4584 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4585 * children to get the focus
4587 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4588 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4589 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4596 gtk_text_view_move_focus (GtkWidget *widget,
4597 GtkDirectionType direction_type)
4599 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4601 if (GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus)
4602 GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus (text_view,
4611 gtk_text_view_add (GtkContainer *container,
4614 /* This is pretty random. */
4615 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4617 GTK_TEXT_WINDOW_WIDGET,
4622 gtk_text_view_remove (GtkContainer *container,
4626 GtkTextView *text_view;
4627 GtkTextViewChild *vc;
4629 text_view = GTK_TEXT_VIEW (container);
4632 iter = text_view->children;
4634 while (iter != NULL)
4638 if (vc->widget == child)
4641 iter = g_slist_next (iter);
4644 g_assert (iter != NULL); /* be sure we had the child in the list */
4646 text_view->children = g_slist_remove (text_view->children, vc);
4648 gtk_widget_unparent (vc->widget);
4650 text_view_child_free (vc);
4654 gtk_text_view_forall (GtkContainer *container,
4655 gboolean include_internals,
4656 GtkCallback callback,
4657 gpointer callback_data)
4660 GtkTextView *text_view;
4663 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4664 g_return_if_fail (callback != NULL);
4666 text_view = GTK_TEXT_VIEW (container);
4668 copy = g_slist_copy (text_view->children);
4671 while (iter != NULL)
4673 GtkTextViewChild *vc = iter->data;
4675 (* callback) (vc->widget, callback_data);
4677 iter = g_slist_next (iter);
4680 g_slist_free (copy);
4683 #define CURSOR_ON_MULTIPLIER 2
4684 #define CURSOR_OFF_MULTIPLIER 1
4685 #define CURSOR_PEND_MULTIPLIER 3
4686 #define CURSOR_DIVIDER 3
4689 cursor_blinks (GtkTextView *text_view)
4691 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4694 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4697 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4700 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4705 if (text_view->editable)
4707 GtkTextMark *insert;
4710 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4711 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4713 if (gtk_text_iter_editable (&iter, text_view->editable))
4721 get_cursor_time (GtkTextView *text_view)
4723 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4726 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
4732 get_cursor_blink_timeout (GtkTextView *text_view)
4734 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4737 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
4748 blink_cb (gpointer data)
4750 GtkTextView *text_view;
4751 GtkTextViewPrivate *priv;
4755 text_view = GTK_TEXT_VIEW (data);
4756 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4758 if (!GTK_WIDGET_HAS_FOCUS (text_view))
4760 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
4761 "connect a handler to this signal, it must return\n"
4762 "FALSE so the text view gets the event as well");
4764 gtk_text_view_check_cursor_blink (text_view);
4769 g_assert (text_view->layout);
4770 g_assert (text_view->cursor_visible);
4772 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4774 blink_timeout = get_cursor_blink_timeout (text_view);
4775 if (priv->blink_time > 1000 * blink_timeout &&
4776 blink_timeout < G_MAXINT/1000)
4778 /* we've blinked enough without the user doing anything, stop blinking */
4780 text_view->blink_timeout = 0;
4783 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4788 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
4791 priv->blink_time += get_cursor_time (text_view);
4794 /* Block changed_handler while changing the layout's cursor visibility
4795 * because it would expose the whole paragraph. Instead, we expose
4796 * the cursor's area(s) manually below.
4798 g_signal_handlers_block_by_func (text_view->layout,
4801 gtk_text_layout_set_cursor_visible (text_view->layout, !visible);
4802 g_signal_handlers_unblock_by_func (text_view->layout,
4806 text_window_invalidate_cursors (text_view->text_window);
4808 /* Remove ourselves */
4814 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4816 if (text_view->blink_timeout)
4818 g_source_remove (text_view->blink_timeout);
4819 text_view->blink_timeout = 0;
4824 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4826 if (text_view->layout != NULL &&
4827 text_view->cursor_visible &&
4828 GTK_WIDGET_HAS_FOCUS (text_view))
4830 if (cursor_blinks (text_view))
4832 if (text_view->blink_timeout == 0)
4834 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4836 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4843 gtk_text_view_stop_cursor_blink (text_view);
4844 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4849 gtk_text_view_stop_cursor_blink (text_view);
4850 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4855 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
4857 if (text_view->layout != NULL &&
4858 text_view->cursor_visible &&
4859 GTK_WIDGET_HAS_FOCUS (text_view) &&
4860 cursor_blinks (text_view))
4862 gtk_text_view_stop_cursor_blink (text_view);
4863 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4865 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
4872 gtk_text_view_reset_blink_time (GtkTextView *text_view)
4874 GtkTextViewPrivate *priv;
4876 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4878 priv->blink_time = 0;
4883 * Key binding handlers
4887 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4888 GtkTextIter *newplace,
4891 gboolean ret = TRUE;
4895 ret = gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4901 ret = gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4909 move_cursor (GtkTextView *text_view,
4910 const GtkTextIter *new_location,
4911 gboolean extend_selection)
4913 if (extend_selection)
4914 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4918 gtk_text_buffer_place_cursor (get_buffer (text_view),
4920 gtk_text_view_check_cursor_blink (text_view);
4924 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4925 GtkMovementStep step,
4927 gboolean extend_selection)
4930 GtkTextIter newplace;
4931 gint cursor_x_pos = 0;
4932 GtkDirectionType leave_direction = -1;
4934 if (!text_view->cursor_visible)
4936 GtkScrollStep scroll_step;
4940 case GTK_MOVEMENT_VISUAL_POSITIONS:
4941 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
4943 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4944 case GTK_MOVEMENT_WORDS:
4945 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
4947 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4948 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
4950 case GTK_MOVEMENT_DISPLAY_LINES:
4951 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
4953 case GTK_MOVEMENT_PARAGRAPHS:
4954 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4955 scroll_step = GTK_SCROLL_STEPS;
4957 case GTK_MOVEMENT_PAGES:
4958 scroll_step = GTK_SCROLL_PAGES;
4960 case GTK_MOVEMENT_HORIZONTAL_PAGES:
4961 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
4963 case GTK_MOVEMENT_BUFFER_ENDS:
4964 scroll_step = GTK_SCROLL_ENDS;
4967 scroll_step = GTK_SCROLL_PAGES;
4971 if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
4973 if (leave_direction != -1 &&
4974 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
4977 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
4984 gtk_text_view_reset_im_context (text_view);
4986 if (step == GTK_MOVEMENT_PAGES)
4988 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
4989 gtk_widget_error_bell (GTK_WIDGET (text_view));
4991 gtk_text_view_check_cursor_blink (text_view);
4992 gtk_text_view_pend_cursor_blink (text_view);
4995 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
4997 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
4998 gtk_widget_error_bell (GTK_WIDGET (text_view));
5000 gtk_text_view_check_cursor_blink (text_view);
5001 gtk_text_view_pend_cursor_blink (text_view);
5005 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5006 gtk_text_buffer_get_insert (get_buffer (text_view)));
5009 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5010 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
5014 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5015 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5018 case GTK_MOVEMENT_VISUAL_POSITIONS:
5019 gtk_text_layout_move_iter_visually (text_view->layout,
5023 case GTK_MOVEMENT_WORDS:
5025 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5028 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5029 gtk_text_iter_forward_to_line_end (&newplace);
5033 case GTK_MOVEMENT_DISPLAY_LINES:
5036 leave_direction = GTK_DIR_UP;
5038 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5039 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5041 gtk_text_iter_set_line_offset (&newplace, 0);
5045 leave_direction = GTK_DIR_DOWN;
5047 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5048 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5050 gtk_text_iter_forward_to_line_end (&newplace);
5054 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5056 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5057 else if (count < -1)
5058 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5061 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
5064 case GTK_MOVEMENT_PARAGRAPHS:
5067 if (!gtk_text_iter_ends_line (&newplace))
5069 gtk_text_iter_forward_to_line_end (&newplace);
5072 gtk_text_iter_forward_visible_lines (&newplace, count);
5073 gtk_text_iter_forward_to_line_end (&newplace);
5077 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5078 gtk_text_iter_set_line_offset (&newplace, 0);
5079 gtk_text_iter_forward_visible_lines (&newplace, count);
5080 gtk_text_iter_set_line_offset (&newplace, 0);
5084 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5087 if (!gtk_text_iter_ends_line (&newplace))
5088 gtk_text_iter_forward_to_line_end (&newplace);
5092 gtk_text_iter_set_line_offset (&newplace, 0);
5096 case GTK_MOVEMENT_BUFFER_ENDS:
5098 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5100 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5107 /* call move_cursor() even if the cursor hasn't moved, since it
5108 cancels the selection
5110 move_cursor (text_view, &newplace, extend_selection);
5112 if (!gtk_text_iter_equal (&insert, &newplace))
5114 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5115 gtk_text_view_scroll_mark_onscreen (text_view,
5116 gtk_text_buffer_get_insert (get_buffer (text_view)));
5118 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5119 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5121 else if (leave_direction != -1)
5123 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5126 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5131 gtk_widget_error_bell (GTK_WIDGET (text_view));
5134 gtk_text_view_check_cursor_blink (text_view);
5135 gtk_text_view_pend_cursor_blink (text_view);
5139 gtk_text_view_move_cursor (GtkTextView *text_view,
5140 GtkMovementStep step,
5142 gboolean extend_selection)
5144 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5148 gtk_text_view_page_horizontally (GtkTextView *text_view,
5150 gboolean extend_selection)
5152 gtk_text_view_move_cursor_internal (text_view, GTK_MOVEMENT_HORIZONTAL_PAGES,
5153 count, extend_selection);
5158 gtk_text_view_move_viewport (GtkTextView *text_view,
5162 GtkAdjustment *adjustment;
5167 case GTK_SCROLL_STEPS:
5168 case GTK_SCROLL_PAGES:
5169 case GTK_SCROLL_ENDS:
5170 adjustment = get_vadjustment (text_view);
5172 case GTK_SCROLL_HORIZONTAL_STEPS:
5173 case GTK_SCROLL_HORIZONTAL_PAGES:
5174 case GTK_SCROLL_HORIZONTAL_ENDS:
5175 adjustment = get_hadjustment (text_view);
5178 adjustment = get_vadjustment (text_view);
5184 case GTK_SCROLL_STEPS:
5185 case GTK_SCROLL_HORIZONTAL_STEPS:
5186 increment = adjustment->step_increment;
5188 case GTK_SCROLL_PAGES:
5189 case GTK_SCROLL_HORIZONTAL_PAGES:
5190 increment = adjustment->page_increment;
5192 case GTK_SCROLL_ENDS:
5193 case GTK_SCROLL_HORIZONTAL_ENDS:
5194 increment = adjustment->upper - adjustment->lower;
5201 return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
5205 gtk_text_view_set_anchor (GtkTextView *text_view)
5209 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5210 gtk_text_buffer_get_insert (get_buffer (text_view)));
5212 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5216 gtk_text_view_scroll_pages (GtkTextView *text_view,
5218 gboolean extend_selection)
5223 gint cursor_x_pos, cursor_y_pos;
5224 GtkTextMark *insert_mark;
5225 GtkTextIter old_insert;
5226 GtkTextIter new_insert;
5230 g_return_val_if_fail (text_view->vadjustment != NULL, FALSE);
5232 adj = text_view->vadjustment;
5234 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5236 /* Make sure we start from the current cursor position, even
5237 * if it was offscreen, but don't queue more scrolls if we're
5240 if (text_view->pending_scroll)
5241 cancel_pending_scroll (text_view);
5243 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5245 /* Validate the region that will be brought into view by the cursor motion
5247 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5248 &old_insert, insert_mark);
5252 gtk_text_view_get_first_para_iter (text_view, &anchor);
5253 y0 = adj->page_size;
5254 y1 = adj->page_size + count * adj->page_increment;
5258 gtk_text_view_get_first_para_iter (text_view, &anchor);
5259 y0 = count * adj->page_increment + adj->page_size;
5263 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
5264 /* FIXME do we need to update the adjustment ranges here? */
5266 new_insert = old_insert;
5268 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5270 /* already at top, just be sure we are at offset 0 */
5271 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5272 move_cursor (text_view, &new_insert, extend_selection);
5274 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5276 /* already at bottom, just be sure we are at the end */
5277 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5278 move_cursor (text_view, &new_insert, extend_selection);
5282 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5284 oldval = adj->value;
5285 newval = adj->value;
5287 newval += count * adj->page_increment;
5289 set_adjustment_clamped (adj, newval);
5290 cursor_y_pos += adj->value - oldval;
5292 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5293 clamp_iter_onscreen (text_view, &new_insert);
5294 move_cursor (text_view, &new_insert, extend_selection);
5296 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5299 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5300 * only guarantees 1 pixel onscreen.
5302 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5303 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5305 return !gtk_text_iter_equal (&old_insert, &new_insert);
5309 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5311 gboolean extend_selection)
5316 gint cursor_x_pos, cursor_y_pos;
5317 GtkTextMark *insert_mark;
5318 GtkTextIter old_insert;
5319 GtkTextIter new_insert;
5322 g_return_val_if_fail (text_view->hadjustment != NULL, FALSE);
5324 adj = text_view->hadjustment;
5326 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5328 /* Make sure we start from the current cursor position, even
5329 * if it was offscreen, but don't queue more scrolls if we're
5332 if (text_view->pending_scroll)
5333 cancel_pending_scroll (text_view);
5335 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5337 /* Validate the line that we're moving within.
5339 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5340 &old_insert, insert_mark);
5342 gtk_text_layout_get_line_yrange (text_view->layout, &old_insert, &y, &height);
5343 gtk_text_layout_validate_yrange (text_view->layout, &old_insert, y, y + height);
5344 /* FIXME do we need to update the adjustment ranges here? */
5346 new_insert = old_insert;
5348 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5350 /* already at far left, just be sure we are at offset 0 */
5351 gtk_text_iter_set_line_offset (&new_insert, 0);
5352 move_cursor (text_view, &new_insert, extend_selection);
5354 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5356 /* already at far right, just be sure we are at the end */
5357 if (!gtk_text_iter_ends_line (&new_insert))
5358 gtk_text_iter_forward_to_line_end (&new_insert);
5359 move_cursor (text_view, &new_insert, extend_selection);
5363 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5365 oldval = adj->value;
5366 newval = adj->value;
5368 newval += count * adj->page_increment;
5370 set_adjustment_clamped (adj, newval);
5371 cursor_x_pos += adj->value - oldval;
5373 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5374 clamp_iter_onscreen (text_view, &new_insert);
5375 move_cursor (text_view, &new_insert, extend_selection);
5377 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5380 /* FIXME for lines shorter than the overall widget width, this results in a
5381 * "bounce" effect as we scroll to the right of the widget, then scroll
5382 * back to get the end of the line onscreen.
5383 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5386 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5387 * only guarantees 1 pixel onscreen.
5389 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5390 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5392 return !gtk_text_iter_equal (&old_insert, &new_insert);
5396 whitespace (gunichar ch, gpointer user_data)
5398 return (ch == ' ' || ch == '\t');
5402 not_whitespace (gunichar ch, gpointer user_data)
5404 return !whitespace (ch, user_data);
5408 find_whitepace_region (const GtkTextIter *center,
5409 GtkTextIter *start, GtkTextIter *end)
5414 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5415 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5416 if (whitespace (gtk_text_iter_get_char (end), NULL))
5417 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5419 return !gtk_text_iter_equal (start, end);
5423 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5426 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5427 text_view->editable))
5429 gtk_widget_error_bell (GTK_WIDGET (text_view));
5434 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5441 gboolean leave_one = FALSE;
5443 gtk_text_view_reset_im_context (text_view);
5445 if (type == GTK_DELETE_CHARS)
5447 /* Char delete deletes the selection, if one exists */
5448 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5449 text_view->editable))
5453 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5454 gtk_text_buffer_get_insert (get_buffer (text_view)));
5461 case GTK_DELETE_CHARS:
5462 gtk_text_iter_forward_cursor_positions (&end, count);
5465 case GTK_DELETE_WORD_ENDS:
5467 gtk_text_iter_forward_word_ends (&end, count);
5469 gtk_text_iter_backward_word_starts (&start, 0 - count);
5472 case GTK_DELETE_WORDS:
5475 case GTK_DELETE_DISPLAY_LINE_ENDS:
5478 case GTK_DELETE_DISPLAY_LINES:
5481 case GTK_DELETE_PARAGRAPH_ENDS:
5482 /* If we're already at a newline, we need to
5483 * simply delete that newline, instead of
5484 * moving to the next one.
5486 if (gtk_text_iter_ends_line (&end))
5488 gtk_text_iter_forward_line (&end);
5494 if (!gtk_text_iter_forward_to_line_end (&end))
5500 /* FIXME figure out what a negative count means
5504 case GTK_DELETE_PARAGRAPHS:
5507 gtk_text_iter_set_line_offset (&start, 0);
5508 gtk_text_iter_forward_to_line_end (&end);
5510 /* Do the lines beyond the first. */
5513 gtk_text_iter_forward_to_line_end (&end);
5519 /* FIXME negative count? */
5523 case GTK_DELETE_WHITESPACE:
5525 find_whitepace_region (&insert, &start, &end);
5533 if (!gtk_text_iter_equal (&start, &end))
5535 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5537 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5538 text_view->editable))
5541 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5543 text_view->editable);
5547 gtk_widget_error_bell (GTK_WIDGET (text_view));
5550 gtk_text_buffer_end_user_action (get_buffer (text_view));
5551 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5553 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5554 gtk_text_view_scroll_mark_onscreen (text_view,
5555 gtk_text_buffer_get_insert (get_buffer (text_view)));
5559 gtk_widget_error_bell (GTK_WIDGET (text_view));
5564 gtk_text_view_backspace (GtkTextView *text_view)
5568 gtk_text_view_reset_im_context (text_view);
5570 /* Backspace deletes the selection, if one exists */
5571 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5572 text_view->editable))
5575 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5577 gtk_text_buffer_get_insert (get_buffer (text_view)));
5579 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5580 TRUE, text_view->editable))
5582 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5583 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5584 gtk_text_view_scroll_mark_onscreen (text_view,
5585 gtk_text_buffer_get_insert (get_buffer (text_view)));
5589 gtk_widget_error_bell (GTK_WIDGET (text_view));
5594 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5596 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5597 GDK_SELECTION_CLIPBOARD);
5599 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5601 text_view->editable);
5602 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5603 gtk_text_view_scroll_mark_onscreen (text_view,
5604 gtk_text_buffer_get_insert (get_buffer (text_view)));
5608 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5610 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5611 GDK_SELECTION_CLIPBOARD);
5613 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5616 /* on copy do not scroll, we are already onscreen */
5620 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5622 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5623 GDK_SELECTION_CLIPBOARD);
5625 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5628 text_view->editable);
5629 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5630 gtk_text_view_scroll_mark_onscreen (text_view,
5631 gtk_text_buffer_get_insert (get_buffer (text_view)));
5635 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5637 if (text_view->text_window)
5638 text_window_invalidate_cursors (text_view->text_window);
5640 text_view->overwrite_mode = !text_view->overwrite_mode;
5642 if (text_view->layout)
5643 gtk_text_layout_set_overwrite_mode (text_view->layout,
5644 text_view->overwrite_mode && text_view->editable);
5646 if (text_view->text_window)
5647 text_window_invalidate_cursors (text_view->text_window);
5649 gtk_text_view_pend_cursor_blink (text_view);
5651 g_object_notify (G_OBJECT (text_view), "overwrite");
5655 * gtk_text_view_get_overwrite:
5656 * @text_view: a #GtkTextView
5658 * Returns whether the #GtkTextView is in overwrite mode or not.
5660 * Return value: whether @text_view is in overwrite mode or not.
5665 gtk_text_view_get_overwrite (GtkTextView *text_view)
5667 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5669 return text_view->overwrite_mode;
5673 * gtk_text_view_set_overwrite:
5674 * @text_view: a #GtkTextView
5675 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
5677 * Changes the #GtkTextView overwrite mode.
5682 gtk_text_view_set_overwrite (GtkTextView *text_view,
5685 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5686 overwrite = overwrite != FALSE;
5688 if (text_view->overwrite_mode != overwrite)
5689 gtk_text_view_toggle_overwrite (text_view);
5693 * gtk_text_view_set_accepts_tab:
5694 * @text_view: A #GtkTextView
5695 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
5696 * character, %FALSE, if pressing the Tab key should move the
5699 * Sets the behavior of the text widget when the Tab key is pressed.
5700 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
5701 * is %FALSE the keyboard focus is moved to the next widget in the focus
5707 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
5708 gboolean accepts_tab)
5710 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5712 accepts_tab = accepts_tab != FALSE;
5714 if (text_view->accepts_tab != accepts_tab)
5716 text_view->accepts_tab = accepts_tab;
5718 g_object_notify (G_OBJECT (text_view), "accepts-tab");
5723 * gtk_text_view_get_accepts_tab:
5724 * @text_view: A #GtkTextView
5726 * Returns whether pressing the Tab key inserts a tab characters.
5727 * gtk_text_view_set_accepts_tab().
5729 * Return value: %TRUE if pressing the Tab key inserts a tab character,
5730 * %FALSE if pressing the Tab key moves the keyboard focus.
5735 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
5737 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5739 return text_view->accepts_tab;
5743 gtk_text_view_compat_move_focus (GtkTextView *text_view,
5744 GtkDirectionType direction_type)
5746 GSignalInvocationHint *hint = g_signal_get_invocation_hint (text_view);
5748 /* as of GTK+ 2.12, the "move-focus" signal has been moved to GtkWidget,
5749 * the evil code below makes sure that both emitting the signal and
5750 * calling the virtual function directly continue to work as expetcted
5753 if (hint->signal_id == g_signal_lookup ("move-focus", GTK_TYPE_WIDGET))
5755 /* if this is a signal emission, chain up */
5759 g_signal_chain_from_overridden_handler (text_view,
5760 direction_type, &retval);
5764 /* otherwise emit the signal, since somebody called the virtual
5768 g_signal_emit_by_name (text_view, "move-focus", direction_type);
5777 gtk_text_view_unselect (GtkTextView *text_view)
5781 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5782 gtk_text_buffer_get_insert (get_buffer (text_view)));
5784 gtk_text_buffer_move_mark (get_buffer (text_view),
5785 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
5790 get_iter_at_pointer (GtkTextView *text_view,
5795 gint xcoord, ycoord;
5796 GdkModifierType state;
5798 gdk_window_get_pointer (text_view->text_window->bin_window,
5799 &xcoord, &ycoord, &state);
5801 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5803 xcoord + text_view->xoffset,
5804 ycoord + text_view->yoffset);
5813 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5814 const gchar *mark_name)
5816 GtkTextIter newplace;
5819 get_iter_at_pointer (text_view, &newplace, NULL, NULL);
5821 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5823 /* This may invalidate the layout */
5824 DV(g_print (G_STRLOC": move mark\n"));
5826 gtk_text_buffer_move_mark (get_buffer (text_view),
5830 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5831 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5833 DV (g_print ("first validate idle leaving %s is %d\n",
5834 G_STRLOC, text_view->first_validate_idle));
5838 selection_scan_timeout (gpointer data)
5840 GtkTextView *text_view;
5842 text_view = GTK_TEXT_VIEW (data);
5844 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5845 gtk_text_view_scroll_mark_onscreen (text_view,
5846 gtk_text_buffer_get_insert (get_buffer (text_view)));
5848 return TRUE; /* remain installed. */
5851 #define UPPER_OFFSET_ANCHOR 0.8
5852 #define LOWER_OFFSET_ANCHOR 0.2
5855 check_scroll (gdouble offset, GtkAdjustment *adj)
5857 if ((offset > UPPER_OFFSET_ANCHOR &&
5858 adj->value + adj->page_size < adj->upper) ||
5859 (offset < LOWER_OFFSET_ANCHOR &&
5860 adj->value > adj->lower))
5867 drag_scan_timeout (gpointer data)
5869 GtkTextView *text_view;
5870 GtkTextIter newplace;
5871 gint x, y, width, height;
5872 gdouble pointer_xoffset, pointer_yoffset;
5874 text_view = GTK_TEXT_VIEW (data);
5876 get_iter_at_pointer (text_view, &newplace, &x, &y);
5877 gdk_drawable_get_size (text_view->text_window->bin_window, &width, &height);
5879 gtk_text_buffer_move_mark (get_buffer (text_view),
5880 text_view->dnd_mark,
5883 pointer_xoffset = (gdouble) x / width;
5884 pointer_yoffset = (gdouble) y / height;
5886 if (check_scroll (pointer_xoffset, text_view->hadjustment) ||
5887 check_scroll (pointer_yoffset, text_view->vadjustment))
5889 /* do not make offsets surpass lower nor upper anchors, this makes
5890 * scrolling speed relative to the distance of the pointer to the
5891 * anchors when it moves beyond them.
5893 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
5894 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
5896 gtk_text_view_scroll_to_mark (text_view,
5897 text_view->dnd_mark,
5898 0., TRUE, pointer_xoffset, pointer_yoffset);
5909 } SelectionGranularity;
5912 * Move @start and @end to the boundaries of the selection unit (indicated by
5913 * @granularity) which contained @start initially.
5914 * If the selction unit is SELECT_WORDS and @start is not contained in a word
5915 * the selection is extended to all the white spaces between the end of the
5916 * word preceding @start and the start of the one following.
5919 extend_selection (GtkTextView *text_view,
5920 SelectionGranularity granularity,
5926 if (granularity == SELECT_WORDS)
5928 if (gtk_text_iter_inside_word (start))
5930 if (!gtk_text_iter_starts_word (start))
5931 gtk_text_iter_backward_visible_word_start (start);
5933 if (!gtk_text_iter_ends_word (end))
5935 if (!gtk_text_iter_forward_visible_word_end (end))
5936 gtk_text_iter_forward_to_end (end);
5944 if (gtk_text_iter_backward_visible_word_start (&tmp))
5945 gtk_text_iter_forward_visible_word_end (&tmp);
5947 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
5950 gtk_text_iter_set_line_offset (start, 0);
5953 if (!gtk_text_iter_forward_visible_word_end (&tmp))
5954 gtk_text_iter_forward_to_end (&tmp);
5956 if (gtk_text_iter_ends_word (&tmp))
5957 gtk_text_iter_backward_visible_word_start (&tmp);
5959 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
5962 gtk_text_iter_forward_to_line_end (end);
5965 else if (granularity == SELECT_LINES)
5967 if (gtk_text_view_starts_display_line (text_view, start))
5969 /* If on a display line boundary, we assume the user
5970 * clicked off the end of a line and we therefore select
5971 * the line before the boundary.
5973 gtk_text_view_backward_display_line_start (text_view, start);
5977 /* start isn't on the start of a line, so we move it to the
5978 * start, and move end to the end unless it's already there.
5980 gtk_text_view_backward_display_line_start (text_view, start);
5982 if (!gtk_text_view_starts_display_line (text_view, end))
5983 gtk_text_view_forward_display_line_end (text_view, end);
5991 SelectionGranularity granularity;
5992 GtkTextMark *orig_start;
5993 GtkTextMark *orig_end;
5997 selection_data_free (SelectionData *data)
5999 if (data->orig_start != NULL)
6000 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6002 if (data->orig_end != NULL)
6003 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6009 selection_motion_event_handler (GtkTextView *text_view,
6010 GdkEventMotion *event,
6011 SelectionData *data)
6013 gdk_event_request_motions (event);
6015 if (data->granularity == SELECT_CHARACTERS)
6017 move_mark_to_pointer_and_scroll (text_view, "insert");
6021 GtkTextIter cursor, start, end;
6022 GtkTextIter orig_start, orig_end;
6023 GtkTextBuffer *buffer;
6025 buffer = get_buffer (text_view);
6027 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6028 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6030 get_iter_at_pointer (text_view, &cursor, NULL, NULL);
6033 extend_selection (text_view, data->granularity, &start, &end);
6035 /* either the selection extends to the front, or end (or not) */
6036 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6037 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6039 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6041 gtk_text_view_scroll_mark_onscreen (text_view,
6042 gtk_text_buffer_get_insert (buffer));
6045 /* If we had to scroll offscreen, insert a timeout to do so
6046 * again. Note that in the timeout, even if the mouse doesn't
6047 * move, due to this scroll xoffset/yoffset will have changed
6048 * and we'll need to scroll again.
6050 if (text_view->scroll_timeout != 0) /* reset on every motion event */
6051 g_source_remove (text_view->scroll_timeout);
6053 text_view->scroll_timeout =
6054 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6060 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6061 const GtkTextIter *iter,
6062 GdkEventButton *button)
6064 GtkTextIter cursor, ins, bound;
6065 GtkTextIter orig_start, orig_end;
6066 GtkTextBuffer *buffer;
6067 SelectionData *data;
6069 if (text_view->selection_drag_handler != 0)
6072 data = g_new0 (SelectionData, 1);
6074 if (button->type == GDK_2BUTTON_PRESS)
6075 data->granularity = SELECT_WORDS;
6076 else if (button->type == GDK_3BUTTON_PRESS)
6077 data->granularity = SELECT_LINES;
6079 data->granularity = SELECT_CHARACTERS;
6081 gtk_grab_add (GTK_WIDGET (text_view));
6083 buffer = get_buffer (text_view);
6088 extend_selection (text_view, data->granularity, &ins, &bound);
6092 if (button->state & GDK_SHIFT_MASK)
6094 /* Extend selection */
6095 GtkTextIter old_ins, old_bound;
6096 GtkTextIter old_start, old_end;
6098 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6099 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6100 old_start = old_ins;
6101 old_end = old_bound;
6102 gtk_text_iter_order (&old_start, &old_end);
6104 /* move the front cursor, if the mouse is in front of the selection. Should the
6105 * cursor however be inside the selection (this happens on tripple click) then we
6106 * move the side which was last moved (current insert mark) */
6107 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6108 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6109 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6112 orig_start = old_end;
6124 gtk_text_buffer_select_range (buffer, &ins, &bound);
6126 gtk_text_iter_order (&orig_start, &orig_end);
6127 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6129 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6132 gtk_text_view_check_cursor_blink (text_view);
6134 text_view->selection_drag_handler = g_signal_connect_data (text_view,
6135 "motion_notify_event",
6136 G_CALLBACK (selection_motion_event_handler),
6138 (GClosureNotify) selection_data_free, 0);
6141 /* returns whether we were really dragging */
6143 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6145 if (text_view->selection_drag_handler == 0)
6148 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
6149 text_view->selection_drag_handler = 0;
6151 if (text_view->scroll_timeout != 0)
6153 g_source_remove (text_view->scroll_timeout);
6154 text_view->scroll_timeout = 0;
6157 gtk_grab_remove (GTK_WIDGET (text_view));
6167 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6168 GtkTextAttributes *values,
6171 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
6172 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
6175 pango_font_description_free (values->font);
6177 values->font = pango_font_description_copy (style->font_desc);
6181 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6183 if (text_view->layout)
6185 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6186 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6187 GtkTextDirection new_cursor_dir;
6188 GtkTextDirection new_keyboard_dir;
6189 gboolean split_cursor;
6191 g_object_get (settings,
6192 "gtk-split-cursor", &split_cursor,
6195 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6196 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6198 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6201 new_cursor_dir = GTK_TEXT_DIR_NONE;
6203 new_cursor_dir = new_keyboard_dir;
6205 gtk_text_layout_set_cursor_direction (text_view->layout, new_cursor_dir);
6206 gtk_text_layout_set_keyboard_direction (text_view->layout, new_keyboard_dir);
6211 gtk_text_view_ensure_layout (GtkTextView *text_view)
6215 widget = GTK_WIDGET (text_view);
6217 if (text_view->layout == NULL)
6219 GtkTextAttributes *style;
6220 PangoContext *ltr_context, *rtl_context;
6223 DV(g_print(G_STRLOC"\n"));
6225 text_view->layout = gtk_text_layout_new ();
6227 g_signal_connect (text_view->layout,
6229 G_CALLBACK (invalidated_handler),
6232 g_signal_connect (text_view->layout,
6234 G_CALLBACK (changed_handler),
6237 g_signal_connect (text_view->layout,
6239 G_CALLBACK (gtk_text_view_child_allocated),
6242 if (get_buffer (text_view))
6243 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
6245 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
6246 gtk_text_view_pend_cursor_blink (text_view);
6248 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
6250 gtk_text_layout_set_overwrite_mode (text_view->layout,
6251 text_view->overwrite_mode && text_view->editable);
6253 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6254 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6255 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6256 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6258 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
6260 g_object_unref (ltr_context);
6261 g_object_unref (rtl_context);
6263 gtk_text_view_check_keymap_direction (text_view);
6265 style = gtk_text_attributes_new ();
6267 gtk_widget_ensure_style (widget);
6268 gtk_text_view_set_attributes_from_style (text_view,
6269 style, widget->style);
6271 style->pixels_above_lines = text_view->pixels_above_lines;
6272 style->pixels_below_lines = text_view->pixels_below_lines;
6273 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
6274 style->left_margin = text_view->left_margin;
6275 style->right_margin = text_view->right_margin;
6276 style->indent = text_view->indent;
6277 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
6279 style->wrap_mode = text_view->wrap_mode;
6280 style->justification = text_view->justify;
6281 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6283 gtk_text_layout_set_default_style (text_view->layout, style);
6285 gtk_text_attributes_unref (style);
6287 /* Set layout for all anchored children */
6289 tmp_list = text_view->children;
6290 while (tmp_list != NULL)
6292 GtkTextViewChild *vc = tmp_list->data;
6296 gtk_text_anchored_child_set_layout (vc->widget,
6298 /* vc may now be invalid! */
6301 tmp_list = g_slist_next (tmp_list);
6304 gtk_text_view_invalidate (text_view);
6309 * gtk_text_view_get_default_attributes:
6310 * @text_view: a #GtkTextView
6312 * Obtains a copy of the default text attributes. These are the
6313 * attributes used for text unless a tag overrides them.
6314 * You'd typically pass the default attributes in to
6315 * gtk_text_iter_get_attributes() in order to get the
6316 * attributes in effect at a given text position.
6318 * The return value is a copy owned by the caller of this function,
6319 * and should be freed.
6321 * Return value: a new #GtkTextAttributes
6324 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6326 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6328 gtk_text_view_ensure_layout (text_view);
6330 return gtk_text_attributes_copy (text_view->layout->default_style);
6334 gtk_text_view_destroy_layout (GtkTextView *text_view)
6336 if (text_view->layout)
6340 gtk_text_view_remove_validate_idles (text_view);
6342 g_signal_handlers_disconnect_by_func (text_view->layout,
6343 invalidated_handler,
6345 g_signal_handlers_disconnect_by_func (text_view->layout,
6349 /* Remove layout from all anchored children */
6350 tmp_list = text_view->children;
6351 while (tmp_list != NULL)
6353 GtkTextViewChild *vc = tmp_list->data;
6357 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6358 /* vc may now be invalid! */
6361 tmp_list = g_slist_next (tmp_list);
6364 gtk_text_view_stop_cursor_blink (text_view);
6365 gtk_text_view_end_selection_drag (text_view);
6367 g_object_unref (text_view->layout);
6368 text_view->layout = NULL;
6373 gtk_text_view_reset_im_context (GtkTextView *text_view)
6375 if (text_view->need_im_reset)
6377 text_view->need_im_reset = FALSE;
6378 gtk_im_context_reset (text_view->im_context);
6387 drag_begin_cb (GtkWidget *widget,
6388 GdkDragContext *context,
6391 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6392 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6395 GdkPixmap *pixmap = NULL;
6397 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6399 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6400 pixmap = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6404 gtk_drag_set_icon_pixmap (context,
6405 gdk_drawable_get_colormap (pixmap),
6409 g_object_unref (pixmap);
6413 gtk_drag_set_icon_default (context);
6418 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6419 const GtkTextIter *iter,
6420 GdkEventMotion *event)
6422 GtkTargetList *target_list;
6424 text_view->drag_start_x = -1;
6425 text_view->drag_start_y = -1;
6426 text_view->pending_place_cursor_button = 0;
6428 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6430 g_signal_connect (text_view, "drag_begin",
6431 G_CALLBACK (drag_begin_cb), NULL);
6432 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6433 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6434 1, (GdkEvent*)event);
6438 gtk_text_view_drag_begin (GtkWidget *widget,
6439 GdkDragContext *context)
6445 gtk_text_view_drag_end (GtkWidget *widget,
6446 GdkDragContext *context)
6451 gtk_text_view_drag_data_get (GtkWidget *widget,
6452 GdkDragContext *context,
6453 GtkSelectionData *selection_data,
6457 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6458 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6460 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6462 gtk_selection_data_set (selection_data,
6463 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6468 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6475 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6477 /* Extract the selected text */
6478 str = gtk_text_buffer_serialize (buffer, buffer,
6479 selection_data->target,
6486 gtk_selection_data_set (selection_data,
6487 selection_data->target,
6489 (guchar *) str, len);
6499 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6501 /* Extract the selected text */
6502 str = gtk_text_iter_get_visible_text (&start, &end);
6507 gtk_selection_data_set_text (selection_data, str, -1);
6514 gtk_text_view_drag_data_delete (GtkWidget *widget,
6515 GdkDragContext *context)
6517 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
6518 TRUE, GTK_TEXT_VIEW (widget)->editable);
6522 gtk_text_view_drag_leave (GtkWidget *widget,
6523 GdkDragContext *context,
6526 GtkTextView *text_view;
6528 text_view = GTK_TEXT_VIEW (widget);
6530 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6532 if (text_view->scroll_timeout != 0)
6533 g_source_remove (text_view->scroll_timeout);
6535 text_view->scroll_timeout = 0;
6539 gtk_text_view_drag_motion (GtkWidget *widget,
6540 GdkDragContext *context,
6545 GtkTextIter newplace;
6546 GtkTextView *text_view;
6549 GdkRectangle target_rect;
6552 GdkDragAction suggested_action = 0;
6554 text_view = GTK_TEXT_VIEW (widget);
6556 target_rect = text_view->text_window->allocation;
6558 if (x < target_rect.x ||
6559 y < target_rect.y ||
6560 x > (target_rect.x + target_rect.width) ||
6561 y > (target_rect.y + target_rect.height))
6562 return FALSE; /* outside the text window, allow parent widgets to handle event */
6564 gtk_text_view_window_to_buffer_coords (text_view,
6565 GTK_TEXT_WINDOW_WIDGET,
6569 gtk_text_layout_get_iter_at_pixel (text_view->layout,
6573 target = gtk_drag_dest_find_target (widget, context,
6574 gtk_drag_dest_get_target_list (widget));
6576 if (target == GDK_NONE)
6578 /* can't accept any of the offered targets */
6580 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6582 gtk_text_iter_compare (&newplace, &start) >= 0 &&
6583 gtk_text_iter_compare (&newplace, &end) <= 0)
6585 /* We're inside the selection. */
6589 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
6591 GtkWidget *source_widget;
6593 suggested_action = context->suggested_action;
6595 source_widget = gtk_drag_get_source_widget (context);
6597 if (source_widget == widget)
6599 /* Default to MOVE, unless the user has
6600 * pressed ctrl or alt to affect available actions
6602 if ((context->actions & GDK_ACTION_MOVE) != 0)
6603 suggested_action = GDK_ACTION_MOVE;
6608 /* Can't drop here. */
6612 if (suggested_action != 0)
6614 gtk_text_mark_set_visible (text_view->dnd_mark,
6615 text_view->cursor_visible);
6617 gdk_drag_status (context, suggested_action, time);
6621 gdk_drag_status (context, 0, time);
6622 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6625 if (!text_view->scroll_timeout)
6626 text_view->scroll_timeout =
6627 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
6629 /* TRUE return means don't propagate the drag motion to parent
6630 * widgets that may also be drop sites.
6636 gtk_text_view_drag_drop (GtkWidget *widget,
6637 GdkDragContext *context,
6642 GtkTextView *text_view;
6643 GtkTextIter drop_point;
6644 GdkAtom target = GDK_NONE;
6646 text_view = GTK_TEXT_VIEW (widget);
6648 if (text_view->scroll_timeout != 0)
6649 g_source_remove (text_view->scroll_timeout);
6651 text_view->scroll_timeout = 0;
6653 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6655 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6657 text_view->dnd_mark);
6659 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
6660 target = gtk_drag_dest_find_target (widget, context, NULL);
6662 if (target != GDK_NONE)
6663 gtk_drag_get_data (widget, context, target, time);
6665 gtk_drag_finish (context, FALSE, FALSE, time);
6671 insert_text_data (GtkTextView *text_view,
6672 GtkTextIter *drop_point,
6673 GtkSelectionData *selection_data)
6677 str = gtk_selection_data_get_text (selection_data);
6681 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
6682 drop_point, (gchar *) str, -1,
6683 text_view->editable))
6685 gtk_widget_error_bell (GTK_WIDGET (text_view));
6693 gtk_text_view_drag_data_received (GtkWidget *widget,
6694 GdkDragContext *context,
6697 GtkSelectionData *selection_data,
6701 GtkTextIter drop_point;
6702 GtkTextView *text_view;
6703 gboolean success = FALSE;
6704 GtkTextBuffer *buffer = NULL;
6706 text_view = GTK_TEXT_VIEW (widget);
6708 if (!text_view->dnd_mark)
6711 buffer = get_buffer (text_view);
6713 gtk_text_buffer_get_iter_at_mark (buffer,
6715 text_view->dnd_mark);
6717 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
6722 gtk_text_buffer_begin_user_action (buffer);
6724 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6726 GtkTextBuffer *src_buffer = NULL;
6727 GtkTextIter start, end;
6728 gboolean copy_tags = TRUE;
6730 if (selection_data->length != sizeof (src_buffer))
6733 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
6735 if (src_buffer == NULL)
6738 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
6740 if (gtk_text_buffer_get_tag_table (src_buffer) !=
6741 gtk_text_buffer_get_tag_table (buffer))
6743 /* try to find a suitable rich text target instead */
6747 GdkAtom target = GDK_NONE;
6751 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
6753 for (list = context->targets; list; list = g_list_next (list))
6757 for (i = 0; i < n_atoms; i++)
6758 if (GUINT_TO_POINTER (atoms[i]) == list->data)
6767 if (target != GDK_NONE)
6769 gtk_drag_get_data (widget, context, target, time);
6770 gtk_text_buffer_end_user_action (buffer);
6775 if (gtk_text_buffer_get_selection_bounds (src_buffer,
6780 gtk_text_buffer_insert_range_interactive (buffer,
6784 text_view->editable);
6789 str = gtk_text_iter_get_visible_text (&start, &end);
6790 gtk_text_buffer_insert_interactive (buffer,
6791 &drop_point, str, -1,
6792 text_view->editable);
6797 else if (selection_data->length > 0 &&
6798 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6801 GError *error = NULL;
6803 retval = gtk_text_buffer_deserialize (buffer, buffer,
6804 selection_data->target,
6806 (guint8 *) selection_data->data,
6807 selection_data->length,
6812 g_warning ("error pasting: %s\n", error->message);
6813 g_clear_error (&error);
6817 insert_text_data (text_view, &drop_point, selection_data);
6820 gtk_drag_finish (context, success,
6821 success && context->action == GDK_ACTION_MOVE,
6826 gtk_text_buffer_get_iter_at_mark (buffer,
6828 text_view->dnd_mark);
6829 gtk_text_buffer_place_cursor (buffer, &drop_point);
6831 gtk_text_buffer_end_user_action (buffer);
6835 static GtkAdjustment*
6836 get_hadjustment (GtkTextView *text_view)
6838 if (text_view->hadjustment == NULL)
6839 gtk_text_view_set_scroll_adjustments (text_view,
6840 NULL, /* forces creation */
6841 text_view->vadjustment);
6843 return text_view->hadjustment;
6846 static GtkAdjustment*
6847 get_vadjustment (GtkTextView *text_view)
6849 if (text_view->vadjustment == NULL)
6850 gtk_text_view_set_scroll_adjustments (text_view,
6851 text_view->hadjustment,
6852 NULL); /* forces creation */
6853 return text_view->vadjustment;
6858 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
6859 GtkAdjustment *hadj,
6860 GtkAdjustment *vadj)
6862 gboolean need_adjust = FALSE;
6865 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
6867 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6869 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
6871 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6873 if (text_view->hadjustment && (text_view->hadjustment != hadj))
6875 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
6876 gtk_text_view_value_changed,
6878 g_object_unref (text_view->hadjustment);
6881 if (text_view->vadjustment && (text_view->vadjustment != vadj))
6883 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
6884 gtk_text_view_value_changed,
6886 g_object_unref (text_view->vadjustment);
6889 if (text_view->hadjustment != hadj)
6891 text_view->hadjustment = hadj;
6892 g_object_ref_sink (text_view->hadjustment);
6894 g_signal_connect (text_view->hadjustment, "value-changed",
6895 G_CALLBACK (gtk_text_view_value_changed),
6900 if (text_view->vadjustment != vadj)
6902 text_view->vadjustment = vadj;
6903 g_object_ref_sink (text_view->vadjustment);
6905 g_signal_connect (text_view->vadjustment, "value-changed",
6906 G_CALLBACK (gtk_text_view_value_changed),
6912 gtk_text_view_value_changed (NULL, text_view);
6915 /* FIXME this adjust_allocation is a big cut-and-paste from
6916 * GtkCList, needs to be some "official" way to do this
6926 /* The window to which widget->window is relative */
6927 #define ALLOCATION_WINDOW(widget) \
6928 (GTK_WIDGET_NO_WINDOW (widget) ? \
6929 (widget)->window : \
6930 gdk_window_get_parent ((widget)->window))
6933 adjust_allocation_recurse (GtkWidget *widget,
6936 ScrollData *scroll_data = data;
6938 /* Need to really size allocate instead of just poking
6939 * into widget->allocation if the widget is not realized.
6940 * FIXME someone figure out why this was.
6942 if (!GTK_WIDGET_REALIZED (widget))
6944 if (GTK_WIDGET_VISIBLE (widget))
6946 GdkRectangle tmp_rectangle = widget->allocation;
6947 tmp_rectangle.x += scroll_data->dx;
6948 tmp_rectangle.y += scroll_data->dy;
6950 gtk_widget_size_allocate (widget, &tmp_rectangle);
6955 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
6957 widget->allocation.x += scroll_data->dx;
6958 widget->allocation.y += scroll_data->dy;
6960 if (GTK_IS_CONTAINER (widget))
6961 gtk_container_forall (GTK_CONTAINER (widget),
6962 adjust_allocation_recurse,
6969 adjust_allocation (GtkWidget *widget,
6973 ScrollData scroll_data;
6975 if (GTK_WIDGET_REALIZED (widget))
6976 scroll_data.window = ALLOCATION_WINDOW (widget);
6978 scroll_data.window = NULL;
6980 scroll_data.dx = dx;
6981 scroll_data.dy = dy;
6983 adjust_allocation_recurse (widget, &scroll_data);
6987 gtk_text_view_value_changed (GtkAdjustment *adj,
6988 GtkTextView *text_view)
6995 /* Note that we oddly call this function with adj == NULL
6999 text_view->onscreen_validated = FALSE;
7001 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7002 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
7003 adj ? adj->value : 0.0));
7005 if (adj == text_view->hadjustment)
7007 dx = text_view->xoffset - (gint)adj->value;
7008 text_view->xoffset = adj->value;
7010 /* If the change is due to a size change we need
7011 * to invalidate the entire text window because there might be
7012 * right-aligned or centered text
7014 if (text_view->width_changed)
7016 if (GTK_WIDGET_REALIZED (text_view))
7017 gdk_window_invalidate_rect (text_view->text_window->bin_window, NULL, FALSE);
7019 text_view->width_changed = FALSE;
7022 else if (adj == text_view->vadjustment)
7024 dy = text_view->yoffset - (gint)adj->value;
7025 text_view->yoffset = adj->value;
7027 if (text_view->layout)
7029 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
7031 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
7033 text_view->first_para_pixels = adj->value - line_top;
7037 if (dx != 0 || dy != 0)
7041 if (GTK_WIDGET_REALIZED (text_view))
7045 if (text_view->left_window)
7046 text_window_scroll (text_view->left_window, 0, dy);
7047 if (text_view->right_window)
7048 text_window_scroll (text_view->right_window, 0, dy);
7053 if (text_view->top_window)
7054 text_window_scroll (text_view->top_window, dx, 0);
7055 if (text_view->bottom_window)
7056 text_window_scroll (text_view->bottom_window, dx, 0);
7059 /* It looks nicer to scroll the main area last, because
7060 * it takes a while, and making the side areas update
7061 * afterward emphasizes the slowness of scrolling the
7064 text_window_scroll (text_view->text_window, dx, dy);
7067 /* Children are now "moved" in the text window, poke
7068 * into widget->allocation for each child
7070 tmp_list = text_view->children;
7071 while (tmp_list != NULL)
7073 GtkTextViewChild *child = tmp_list->data;
7076 adjust_allocation (child->widget, dx, dy);
7078 tmp_list = g_slist_next (tmp_list);
7082 /* This could result in invalidation, which would install the
7083 * first_validate_idle, which would validate onscreen;
7084 * but we're going to go ahead and validate here, so
7085 * first_validate_idle shouldn't have anything to do.
7087 gtk_text_view_update_layout_width (text_view);
7089 /* note that validation of onscreen could invoke this function
7090 * recursively, by scrolling to maintain first_para, or in response
7091 * to updating the layout width, however there is no problem with
7092 * that, or shouldn't be.
7094 gtk_text_view_validate_onscreen (text_view);
7096 /* process exposes */
7097 if (GTK_WIDGET_REALIZED (text_view))
7099 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7101 if (text_view->left_window)
7102 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
7104 if (text_view->right_window)
7105 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
7107 if (text_view->top_window)
7108 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
7110 if (text_view->bottom_window)
7111 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
7113 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
7116 /* If this got installed, get rid of it, it's just a waste of time. */
7117 if (text_view->first_validate_idle != 0)
7119 g_source_remove (text_view->first_validate_idle);
7120 text_view->first_validate_idle = 0;
7123 gtk_text_view_update_im_spot_location (text_view);
7125 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7129 gtk_text_view_commit_handler (GtkIMContext *context,
7131 GtkTextView *text_view)
7133 gtk_text_view_commit_text (text_view, str);
7137 gtk_text_view_commit_text (GtkTextView *text_view,
7140 gboolean had_selection;
7142 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7144 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7147 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7148 text_view->editable);
7150 if (!strcmp (str, "\n"))
7152 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7153 text_view->editable))
7155 gtk_widget_error_bell (GTK_WIDGET (text_view));
7160 if (!had_selection && text_view->overwrite_mode)
7164 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7166 gtk_text_buffer_get_insert (get_buffer (text_view)));
7167 if (!gtk_text_iter_ends_line (&insert))
7168 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7171 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7172 text_view->editable))
7174 gtk_widget_error_bell (GTK_WIDGET (text_view));
7178 gtk_text_buffer_end_user_action (get_buffer (text_view));
7180 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7181 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7182 gtk_text_view_scroll_mark_onscreen (text_view,
7183 gtk_text_buffer_get_insert (get_buffer (text_view)));
7187 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7188 GtkTextView *text_view)
7191 PangoAttrList *attrs;
7195 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7196 gtk_text_buffer_get_insert (text_view->buffer));
7198 /* Keypress events are passed to input method even if cursor position is not editable;
7199 * so beep here if it's multi-key input sequence, input method will be reset in
7200 * key-press-event handler. */
7201 if (!gtk_text_iter_can_insert (&iter, text_view->editable))
7203 gtk_widget_error_bell (GTK_WIDGET (text_view));
7207 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7208 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
7209 pango_attr_list_unref (attrs);
7212 if (GTK_WIDGET_HAS_FOCUS (text_view))
7213 gtk_text_view_scroll_mark_onscreen (text_view,
7214 gtk_text_buffer_get_insert (get_buffer (text_view)));
7218 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7219 GtkTextView *text_view)
7226 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7227 gtk_text_buffer_get_insert (text_view->buffer));
7230 pos = gtk_text_iter_get_line_index (&start);
7231 gtk_text_iter_set_line_offset (&start, 0);
7232 gtk_text_iter_forward_to_line_end (&end);
7234 text = gtk_text_iter_get_slice (&start, &end);
7235 gtk_im_context_set_surrounding (context, text, -1, pos);
7242 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7245 GtkTextView *text_view)
7250 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7251 gtk_text_buffer_get_insert (text_view->buffer));
7254 gtk_text_iter_forward_chars (&start, offset);
7255 gtk_text_iter_forward_chars (&end, offset + n_chars);
7257 gtk_text_buffer_delete_interactive (text_view->buffer, &start, &end,
7258 text_view->editable);
7264 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7265 const GtkTextIter *location,
7269 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7270 gboolean need_reset = FALSE;
7272 if (mark == gtk_text_buffer_get_insert (buffer))
7274 text_view->virtual_cursor_x = -1;
7275 text_view->virtual_cursor_y = -1;
7276 gtk_text_view_update_im_spot_location (text_view);
7279 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7285 gtk_text_view_reset_im_context (text_view);
7289 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7290 const GParamSpec *pspec,
7293 GtkWidget *widget = GTK_WIDGET (data);
7294 GtkTargetList *view_list;
7295 GtkTargetList *buffer_list;
7298 view_list = gtk_drag_dest_get_target_list (widget);
7299 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7302 gtk_target_list_ref (view_list);
7304 view_list = gtk_target_list_new (NULL, 0);
7306 list = view_list->list;
7309 GtkTargetPair *pair = list->data;
7311 list = g_list_next (list); /* get next element before removing */
7313 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7314 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7316 gtk_target_list_remove (view_list, pair->target);
7320 for (list = buffer_list->list; list; list = g_list_next (list))
7322 GtkTargetPair *pair = list->data;
7324 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7327 gtk_drag_dest_set_target_list (widget, view_list);
7328 gtk_target_list_unref (view_list);
7332 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7337 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7338 gtk_text_buffer_get_insert (get_buffer (text_view)));
7340 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, pos, NULL);
7344 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7350 if ((x && text_view->virtual_cursor_x == -1) ||
7351 (y && text_view->virtual_cursor_y == -1))
7352 gtk_text_view_get_cursor_location (text_view, &pos);
7356 if (text_view->virtual_cursor_x != -1)
7357 *x = text_view->virtual_cursor_x;
7364 if (text_view->virtual_cursor_x != -1)
7365 *y = text_view->virtual_cursor_y;
7367 *y = pos.y + pos.height / 2;
7372 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7378 if (!text_view->layout)
7381 if (x == -1 || y == -1)
7382 gtk_text_view_get_cursor_location (text_view, &pos);
7384 text_view->virtual_cursor_x = (x == -1) ? pos.x : x;
7385 text_view->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7388 /* Quick hack of a popup menu
7391 activate_cb (GtkWidget *menuitem,
7392 GtkTextView *text_view)
7394 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7395 g_signal_emit_by_name (text_view, signal);
7399 append_action_signal (GtkTextView *text_view,
7401 const gchar *stock_id,
7402 const gchar *signal,
7405 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7407 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7408 g_signal_connect (menuitem, "activate",
7409 G_CALLBACK (activate_cb), text_view);
7411 gtk_widget_set_sensitive (menuitem, sensitive);
7413 gtk_widget_show (menuitem);
7414 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7418 gtk_text_view_select_all (GtkWidget *widget,
7421 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
7422 GtkTextBuffer *buffer;
7423 GtkTextIter start_iter, end_iter, insert;
7425 buffer = text_view->buffer;
7428 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
7429 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
7433 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
7434 gtk_text_buffer_get_insert (buffer));
7435 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
7440 select_all_cb (GtkWidget *menuitem,
7441 GtkTextView *text_view)
7443 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
7447 delete_cb (GtkTextView *text_view)
7449 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7450 text_view->editable);
7454 popup_menu_detach (GtkWidget *attach_widget,
7457 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
7461 popup_position_func (GtkMenu *menu,
7467 GtkTextView *text_view;
7469 GdkRectangle cursor_rect;
7470 GdkRectangle onscreen_rect;
7471 gint root_x, root_y;
7476 GdkRectangle monitor;
7478 text_view = GTK_TEXT_VIEW (user_data);
7479 widget = GTK_WIDGET (text_view);
7481 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
7483 screen = gtk_widget_get_screen (widget);
7485 gdk_window_get_origin (widget->window, &root_x, &root_y);
7487 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7489 gtk_text_buffer_get_insert (get_buffer (text_view)));
7491 gtk_text_view_get_iter_location (text_view,
7495 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
7497 gtk_widget_size_request (text_view->popup_menu, &req);
7499 /* can't use rectangle_intersect since cursor rect can have 0 width */
7500 if (cursor_rect.x >= onscreen_rect.x &&
7501 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
7502 cursor_rect.y >= onscreen_rect.y &&
7503 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
7505 gtk_text_view_buffer_to_window_coords (text_view,
7506 GTK_TEXT_WINDOW_WIDGET,
7507 cursor_rect.x, cursor_rect.y,
7508 &cursor_rect.x, &cursor_rect.y);
7510 *x = root_x + cursor_rect.x + cursor_rect.width;
7511 *y = root_y + cursor_rect.y + cursor_rect.height;
7515 /* Just center the menu, since cursor is offscreen. */
7516 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
7517 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
7521 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
7522 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
7524 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
7525 gtk_menu_set_monitor (menu, monitor_num);
7526 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
7528 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
7529 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
7536 GtkTextView *text_view;
7542 range_contains_editable_text (const GtkTextIter *start,
7543 const GtkTextIter *end,
7544 gboolean default_editability)
7546 GtkTextIter iter = *start;
7548 while (gtk_text_iter_compare (&iter, end) < 0)
7550 if (gtk_text_iter_editable (&iter, default_editability))
7553 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
7560 unichar_chosen_func (const char *text,
7563 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7565 gtk_text_view_commit_text (text_view, text);
7569 popup_targets_received (GtkClipboard *clipboard,
7570 GtkSelectionData *data,
7573 PopupInfo *info = user_data;
7574 GtkTextView *text_view = info->text_view;
7576 if (GTK_WIDGET_REALIZED (text_view))
7578 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
7579 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
7581 gboolean clipboard_contains_text;
7582 GtkWidget *menuitem;
7584 gboolean have_selection;
7585 gboolean can_insert;
7587 GtkTextIter sel_start, sel_end;
7588 gboolean show_input_method_menu;
7589 gboolean show_unicode_menu;
7591 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
7593 if (text_view->popup_menu)
7594 gtk_widget_destroy (text_view->popup_menu);
7596 text_view->popup_menu = gtk_menu_new ();
7598 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
7599 GTK_WIDGET (text_view),
7602 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7603 &sel_start, &sel_end);
7605 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7607 gtk_text_buffer_get_insert (get_buffer (text_view)));
7609 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
7611 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
7613 range_contains_editable_text (&sel_start, &sel_end,
7614 text_view->editable));
7615 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
7617 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
7618 can_insert && clipboard_contains_text);
7620 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
7621 gtk_widget_set_sensitive (menuitem,
7623 range_contains_editable_text (&sel_start, &sel_end,
7624 text_view->editable));
7625 g_signal_connect_swapped (menuitem, "activate",
7626 G_CALLBACK (delete_cb), text_view);
7627 gtk_widget_show (menuitem);
7628 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7630 menuitem = gtk_separator_menu_item_new ();
7631 gtk_widget_show (menuitem);
7632 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7634 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
7635 g_signal_connect (menuitem, "activate",
7636 G_CALLBACK (select_all_cb), text_view);
7637 gtk_widget_show (menuitem);
7638 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7640 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
7641 "gtk-show-input-method-menu", &show_input_method_menu,
7642 "gtk-show-unicode-menu", &show_unicode_menu,
7645 if (show_input_method_menu || show_unicode_menu)
7647 menuitem = gtk_separator_menu_item_new ();
7648 gtk_widget_show (menuitem);
7649 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7652 if (show_input_method_menu)
7654 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
7655 gtk_widget_show (menuitem);
7656 gtk_widget_set_sensitive (menuitem, can_insert);
7658 submenu = gtk_menu_new ();
7659 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7660 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7662 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
7663 GTK_MENU_SHELL (submenu));
7666 if (show_unicode_menu)
7668 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
7669 gtk_widget_show (menuitem);
7670 gtk_widget_set_sensitive (menuitem, can_insert);
7672 submenu = gtk_menu_new ();
7673 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7674 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7676 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
7677 unichar_chosen_func,
7681 g_signal_emit (text_view,
7682 signals[POPULATE_POPUP],
7684 text_view->popup_menu);
7687 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7689 info->button, info->time);
7692 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7693 popup_position_func, text_view,
7694 0, gtk_get_current_event_time ());
7695 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
7699 g_object_unref (text_view);
7704 gtk_text_view_do_popup (GtkTextView *text_view,
7705 GdkEventButton *event)
7707 PopupInfo *info = g_new (PopupInfo, 1);
7709 /* In order to know what entries we should make sensitive, we
7710 * ask for the current targets of the clipboard, and when
7711 * we get them, then we actually pop up the menu.
7713 info->text_view = g_object_ref (text_view);
7717 info->button = event->button;
7718 info->time = event->time;
7723 info->time = gtk_get_current_event_time ();
7726 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
7727 GDK_SELECTION_CLIPBOARD),
7728 gdk_atom_intern_static_string ("TARGETS"),
7729 popup_targets_received,
7734 gtk_text_view_popup_menu (GtkWidget *widget)
7736 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
7740 /* Child GdkWindows */
7743 static GtkTextWindow*
7744 text_window_new (GtkTextWindowType type,
7747 gint height_request)
7751 win = g_new (GtkTextWindow, 1);
7754 win->widget = widget;
7756 win->bin_window = NULL;
7757 win->requisition.width = width_request;
7758 win->requisition.height = height_request;
7759 win->allocation.width = width_request;
7760 win->allocation.height = height_request;
7761 win->allocation.x = 0;
7762 win->allocation.y = 0;
7768 text_window_free (GtkTextWindow *win)
7771 text_window_unrealize (win);
7777 text_window_realize (GtkTextWindow *win,
7780 GdkWindowAttr attributes;
7781 gint attributes_mask;
7784 attributes.window_type = GDK_WINDOW_CHILD;
7785 attributes.x = win->allocation.x;
7786 attributes.y = win->allocation.y;
7787 attributes.width = win->allocation.width;
7788 attributes.height = win->allocation.height;
7789 attributes.wclass = GDK_INPUT_OUTPUT;
7790 attributes.visual = gtk_widget_get_visual (win->widget);
7791 attributes.colormap = gtk_widget_get_colormap (win->widget);
7792 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
7794 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
7796 win->window = gdk_window_new (widget->window,
7800 gdk_window_set_back_pixmap (win->window, NULL, FALSE);
7802 gdk_window_show (win->window);
7803 gdk_window_set_user_data (win->window, win->widget);
7804 gdk_window_lower (win->window);
7808 attributes.width = win->allocation.width;
7809 attributes.height = win->allocation.height;
7810 attributes.event_mask = (GDK_EXPOSURE_MASK |
7812 GDK_KEY_PRESS_MASK |
7813 GDK_BUTTON_PRESS_MASK |
7814 GDK_BUTTON_RELEASE_MASK |
7815 GDK_POINTER_MOTION_MASK |
7816 GDK_POINTER_MOTION_HINT_MASK |
7817 gtk_widget_get_events (win->widget));
7819 win->bin_window = gdk_window_new (win->window,
7823 gdk_window_show (win->bin_window);
7824 gdk_window_set_user_data (win->bin_window, win->widget);
7826 if (win->type == GTK_TEXT_WINDOW_TEXT)
7828 if (GTK_WIDGET_IS_SENSITIVE (widget))
7831 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (widget->window),
7833 gdk_window_set_cursor (win->bin_window, cursor);
7834 gdk_cursor_unref (cursor);
7837 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->im_context,
7841 gdk_window_set_background (win->bin_window,
7842 &widget->style->base[GTK_WIDGET_STATE (widget)]);
7846 gdk_window_set_background (win->bin_window,
7847 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
7850 g_object_set_qdata (G_OBJECT (win->window),
7851 g_quark_from_static_string ("gtk-text-view-text-window"),
7854 g_object_set_qdata (G_OBJECT (win->bin_window),
7855 g_quark_from_static_string ("gtk-text-view-text-window"),
7860 text_window_unrealize (GtkTextWindow *win)
7862 if (win->type == GTK_TEXT_WINDOW_TEXT)
7864 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
7868 gdk_window_set_user_data (win->window, NULL);
7869 gdk_window_set_user_data (win->bin_window, NULL);
7870 gdk_window_destroy (win->bin_window);
7871 gdk_window_destroy (win->window);
7873 win->bin_window = NULL;
7877 text_window_size_allocate (GtkTextWindow *win,
7880 win->allocation = *rect;
7884 gdk_window_move_resize (win->window,
7886 rect->width, rect->height);
7888 gdk_window_resize (win->bin_window,
7889 rect->width, rect->height);
7894 text_window_scroll (GtkTextWindow *win,
7898 if (dx != 0 || dy != 0)
7900 gdk_window_scroll (win->bin_window, dx, dy);
7905 text_window_invalidate_rect (GtkTextWindow *win,
7908 GdkRectangle window_rect;
7910 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
7917 window_rect.width = rect->width;
7918 window_rect.height = rect->height;
7920 /* Adjust the rect as appropriate */
7924 case GTK_TEXT_WINDOW_TEXT:
7927 case GTK_TEXT_WINDOW_LEFT:
7928 case GTK_TEXT_WINDOW_RIGHT:
7930 window_rect.width = win->allocation.width;
7933 case GTK_TEXT_WINDOW_TOP:
7934 case GTK_TEXT_WINDOW_BOTTOM:
7936 window_rect.height = win->allocation.height;
7940 g_warning ("%s: bug!", G_STRFUNC);
7945 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
7949 cairo_t *cr = gdk_cairo_create (win->bin_window);
7950 gdk_cairo_rectangle (cr, &window_rect);
7951 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
7959 text_window_invalidate_cursors (GtkTextWindow *win)
7961 GtkTextView *text_view = GTK_TEXT_VIEW (win->widget);
7963 GdkRectangle strong;
7965 gboolean draw_arrow;
7966 gfloat cursor_aspect_ratio;
7970 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7971 gtk_text_buffer_get_insert (text_view->buffer));
7973 if (_gtk_text_layout_get_block_cursor (text_view->layout, &strong))
7975 text_window_invalidate_rect (win, &strong);
7979 gtk_text_layout_get_cursor_locations (text_view->layout, &iter,
7982 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
7983 * ignoring the text direction be exposing both sides of the cursor
7986 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
7988 gtk_widget_style_get (win->widget,
7989 "cursor-aspect-ratio", &cursor_aspect_ratio,
7992 stem_width = strong.height * cursor_aspect_ratio + 1;
7993 arrow_width = stem_width + 1;
7995 strong.width = stem_width;
7997 /* round up to the next even number */
8001 strong.x -= stem_width / 2;
8002 strong.width += stem_width;
8006 strong.x -= arrow_width;
8007 strong.width += arrow_width * 2;
8010 text_window_invalidate_rect (win, &strong);
8012 if (draw_arrow) /* == have weak */
8014 stem_width = weak.height * cursor_aspect_ratio + 1;
8015 arrow_width = stem_width + 1;
8017 weak.width = stem_width;
8019 /* round up to the next even number */
8023 weak.x -= stem_width / 2;
8024 weak.width += stem_width;
8026 weak.x -= arrow_width;
8027 weak.width += arrow_width * 2;
8029 text_window_invalidate_rect (win, &weak);
8034 text_window_get_width (GtkTextWindow *win)
8036 return win->allocation.width;
8040 text_window_get_height (GtkTextWindow *win)
8042 return win->allocation.height;
8049 * gtk_text_view_get_window:
8050 * @text_view: a #GtkTextView
8051 * @win: window to get
8053 * Retrieves the #GdkWindow corresponding to an area of the text view;
8054 * possible windows include the overall widget window, child windows
8055 * on the left, right, top, bottom, and the window that displays the
8056 * text buffer. Windows are %NULL and nonexistent if their width or
8057 * height is 0, and are nonexistent before the widget has been
8060 * Return value: a #GdkWindow, or %NULL
8063 gtk_text_view_get_window (GtkTextView *text_view,
8064 GtkTextWindowType win)
8066 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8070 case GTK_TEXT_WINDOW_WIDGET:
8071 return GTK_WIDGET (text_view)->window;
8074 case GTK_TEXT_WINDOW_TEXT:
8075 return text_view->text_window->bin_window;
8078 case GTK_TEXT_WINDOW_LEFT:
8079 if (text_view->left_window)
8080 return text_view->left_window->bin_window;
8085 case GTK_TEXT_WINDOW_RIGHT:
8086 if (text_view->right_window)
8087 return text_view->right_window->bin_window;
8092 case GTK_TEXT_WINDOW_TOP:
8093 if (text_view->top_window)
8094 return text_view->top_window->bin_window;
8099 case GTK_TEXT_WINDOW_BOTTOM:
8100 if (text_view->bottom_window)
8101 return text_view->bottom_window->bin_window;
8106 case GTK_TEXT_WINDOW_PRIVATE:
8107 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8112 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8117 * gtk_text_view_get_window_type:
8118 * @text_view: a #GtkTextView
8119 * @window: a window type
8121 * Usually used to find out which window an event corresponds to.
8122 * If you connect to an event signal on @text_view, this function
8123 * should be called on <literal>event->window</literal> to
8124 * see which window it was.
8126 * Return value: the window type.
8129 gtk_text_view_get_window_type (GtkTextView *text_view,
8134 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8135 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8137 if (window == GTK_WIDGET (text_view)->window)
8138 return GTK_TEXT_WINDOW_WIDGET;
8140 win = g_object_get_qdata (G_OBJECT (window),
8141 g_quark_try_string ("gtk-text-view-text-window"));
8147 return GTK_TEXT_WINDOW_PRIVATE;
8152 buffer_to_widget (GtkTextView *text_view,
8160 *window_x = buffer_x - text_view->xoffset;
8161 *window_x += text_view->text_window->allocation.x;
8166 *window_y = buffer_y - text_view->yoffset;
8167 *window_y += text_view->text_window->allocation.y;
8172 widget_to_text_window (GtkTextWindow *win,
8179 *window_x = widget_x - win->allocation.x;
8182 *window_y = widget_y - win->allocation.y;
8186 buffer_to_text_window (GtkTextView *text_view,
8195 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8196 "for a nonexistent or private child window of GtkTextView");
8200 buffer_to_widget (text_view,
8202 window_x, window_y);
8204 widget_to_text_window (win,
8205 window_x ? *window_x : 0,
8206 window_y ? *window_y : 0,
8212 * gtk_text_view_buffer_to_window_coords:
8213 * @text_view: a #GtkTextView
8214 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8215 * @buffer_x: buffer x coordinate
8216 * @buffer_y: buffer y coordinate
8217 * @window_x: window x coordinate return location
8218 * @window_y: window y coordinate return location
8220 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8221 * @win, and stores the result in (@window_x, @window_y).
8223 * Note that you can't convert coordinates for a nonexisting window (see
8224 * gtk_text_view_set_border_window_size()).
8227 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8228 GtkTextWindowType win,
8234 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8238 case GTK_TEXT_WINDOW_WIDGET:
8239 buffer_to_widget (text_view,
8241 window_x, window_y);
8244 case GTK_TEXT_WINDOW_TEXT:
8246 *window_x = buffer_x - text_view->xoffset;
8248 *window_y = buffer_y - text_view->yoffset;
8251 case GTK_TEXT_WINDOW_LEFT:
8252 buffer_to_text_window (text_view,
8253 text_view->left_window,
8255 window_x, window_y);
8258 case GTK_TEXT_WINDOW_RIGHT:
8259 buffer_to_text_window (text_view,
8260 text_view->right_window,
8262 window_x, window_y);
8265 case GTK_TEXT_WINDOW_TOP:
8266 buffer_to_text_window (text_view,
8267 text_view->top_window,
8269 window_x, window_y);
8272 case GTK_TEXT_WINDOW_BOTTOM:
8273 buffer_to_text_window (text_view,
8274 text_view->bottom_window,
8276 window_x, window_y);
8279 case GTK_TEXT_WINDOW_PRIVATE:
8280 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8284 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8290 widget_to_buffer (GtkTextView *text_view,
8298 *buffer_x = widget_x + text_view->xoffset;
8299 *buffer_x -= text_view->text_window->allocation.x;
8304 *buffer_y = widget_y + text_view->yoffset;
8305 *buffer_y -= text_view->text_window->allocation.y;
8310 text_window_to_widget (GtkTextWindow *win,
8317 *widget_x = window_x + win->allocation.x;
8320 *widget_y = window_y + win->allocation.y;
8324 text_window_to_buffer (GtkTextView *text_view,
8333 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8334 "coordinates for a nonexistent child window.");
8338 text_window_to_widget (win,
8344 widget_to_buffer (text_view,
8345 buffer_x ? *buffer_x : 0,
8346 buffer_y ? *buffer_y : 0,
8352 * gtk_text_view_window_to_buffer_coords:
8353 * @text_view: a #GtkTextView
8354 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8355 * @window_x: window x coordinate
8356 * @window_y: window y coordinate
8357 * @buffer_x: buffer x coordinate return location
8358 * @buffer_y: buffer y coordinate return location
8360 * Converts coordinates on the window identified by @win to buffer
8361 * coordinates, storing the result in (@buffer_x,@buffer_y).
8363 * Note that you can't convert coordinates for a nonexisting window (see
8364 * gtk_text_view_set_border_window_size()).
8367 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8368 GtkTextWindowType win,
8374 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8378 case GTK_TEXT_WINDOW_WIDGET:
8379 widget_to_buffer (text_view,
8381 buffer_x, buffer_y);
8384 case GTK_TEXT_WINDOW_TEXT:
8386 *buffer_x = window_x + text_view->xoffset;
8388 *buffer_y = window_y + text_view->yoffset;
8391 case GTK_TEXT_WINDOW_LEFT:
8392 text_window_to_buffer (text_view,
8393 text_view->left_window,
8395 buffer_x, buffer_y);
8398 case GTK_TEXT_WINDOW_RIGHT:
8399 text_window_to_buffer (text_view,
8400 text_view->right_window,
8402 buffer_x, buffer_y);
8405 case GTK_TEXT_WINDOW_TOP:
8406 text_window_to_buffer (text_view,
8407 text_view->top_window,
8409 buffer_x, buffer_y);
8412 case GTK_TEXT_WINDOW_BOTTOM:
8413 text_window_to_buffer (text_view,
8414 text_view->bottom_window,
8416 buffer_x, buffer_y);
8419 case GTK_TEXT_WINDOW_PRIVATE:
8420 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8424 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8430 set_window_width (GtkTextView *text_view,
8432 GtkTextWindowType type,
8433 GtkTextWindow **winp)
8439 text_window_free (*winp);
8441 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8448 *winp = text_window_new (type,
8449 GTK_WIDGET (text_view),
8451 /* if the widget is already realized we need to realize the child manually */
8452 if (GTK_WIDGET_REALIZED (text_view))
8453 text_window_realize (*winp, GTK_WIDGET (text_view));
8457 if ((*winp)->requisition.width == width)
8460 (*winp)->requisition.width = width;
8463 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8469 set_window_height (GtkTextView *text_view,
8471 GtkTextWindowType type,
8472 GtkTextWindow **winp)
8478 text_window_free (*winp);
8480 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8487 *winp = text_window_new (type,
8488 GTK_WIDGET (text_view),
8491 /* if the widget is already realized we need to realize the child manually */
8492 if (GTK_WIDGET_REALIZED (text_view))
8493 text_window_realize (*winp, GTK_WIDGET (text_view));
8497 if ((*winp)->requisition.height == height)
8500 (*winp)->requisition.height = height;
8503 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8508 * gtk_text_view_set_border_window_size:
8509 * @text_view: a #GtkTextView
8510 * @type: window to affect
8511 * @size: width or height of the window
8513 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
8514 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
8515 * Automatically destroys the corresponding window if the size is set
8516 * to 0, and creates the window if the size is set to non-zero. This
8517 * function can only be used for the "border windows," it doesn't work
8518 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
8519 * #GTK_TEXT_WINDOW_PRIVATE.
8522 gtk_text_view_set_border_window_size (GtkTextView *text_view,
8523 GtkTextWindowType type,
8527 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8528 g_return_if_fail (size >= 0);
8532 case GTK_TEXT_WINDOW_LEFT:
8533 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
8534 &text_view->left_window);
8537 case GTK_TEXT_WINDOW_RIGHT:
8538 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
8539 &text_view->right_window);
8542 case GTK_TEXT_WINDOW_TOP:
8543 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
8544 &text_view->top_window);
8547 case GTK_TEXT_WINDOW_BOTTOM:
8548 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
8549 &text_view->bottom_window);
8553 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
8559 * gtk_text_view_get_border_window_size:
8560 * @text_view: a #GtkTextView
8561 * @type: window to return size from
8563 * Gets the width of the specified border window. See
8564 * gtk_text_view_set_border_window_size().
8566 * Return value: width of window
8569 gtk_text_view_get_border_window_size (GtkTextView *text_view,
8570 GtkTextWindowType type)
8572 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8576 case GTK_TEXT_WINDOW_LEFT:
8577 if (text_view->left_window)
8578 return text_view->left_window->requisition.width;
8581 case GTK_TEXT_WINDOW_RIGHT:
8582 if (text_view->right_window)
8583 return text_view->right_window->requisition.width;
8586 case GTK_TEXT_WINDOW_TOP:
8587 if (text_view->top_window)
8588 return text_view->top_window->requisition.height;
8591 case GTK_TEXT_WINDOW_BOTTOM:
8592 if (text_view->bottom_window)
8593 return text_view->bottom_window->requisition.height;
8597 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
8608 static GtkTextViewChild*
8609 text_view_child_new_anchored (GtkWidget *child,
8610 GtkTextChildAnchor *anchor,
8611 GtkTextLayout *layout)
8613 GtkTextViewChild *vc;
8615 vc = g_new (GtkTextViewChild, 1);
8617 vc->type = GTK_TEXT_WINDOW_PRIVATE;
8619 vc->anchor = anchor;
8621 vc->from_top_of_line = 0;
8622 vc->from_left_of_buffer = 0;
8624 g_object_ref (vc->widget);
8625 g_object_ref (vc->anchor);
8627 g_object_set_data (G_OBJECT (child),
8628 I_("gtk-text-view-child"),
8631 gtk_text_child_anchor_register_child (anchor, child, layout);
8636 static GtkTextViewChild*
8637 text_view_child_new_window (GtkWidget *child,
8638 GtkTextWindowType type,
8642 GtkTextViewChild *vc;
8644 vc = g_new (GtkTextViewChild, 1);
8649 vc->from_top_of_line = 0;
8650 vc->from_left_of_buffer = 0;
8652 g_object_ref (vc->widget);
8658 g_object_set_data (G_OBJECT (child),
8659 I_("gtk-text-view-child"),
8666 text_view_child_free (GtkTextViewChild *child)
8668 g_object_set_data (G_OBJECT (child->widget),
8669 I_("gtk-text-view-child"), NULL);
8673 gtk_text_child_anchor_unregister_child (child->anchor,
8675 g_object_unref (child->anchor);
8678 g_object_unref (child->widget);
8684 text_view_child_set_parent_window (GtkTextView *text_view,
8685 GtkTextViewChild *vc)
8688 gtk_widget_set_parent_window (vc->widget,
8689 text_view->text_window->bin_window);
8693 window = gtk_text_view_get_window (text_view,
8695 gtk_widget_set_parent_window (vc->widget, window);
8700 add_child (GtkTextView *text_view,
8701 GtkTextViewChild *vc)
8703 text_view->children = g_slist_prepend (text_view->children,
8706 if (GTK_WIDGET_REALIZED (text_view))
8707 text_view_child_set_parent_window (text_view, vc);
8709 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
8713 * gtk_text_view_add_child_at_anchor:
8714 * @text_view: a #GtkTextView
8715 * @child: a #GtkWidget
8716 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
8718 * Adds a child widget in the text buffer, at the given @anchor.
8721 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
8723 GtkTextChildAnchor *anchor)
8725 GtkTextViewChild *vc;
8727 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8728 g_return_if_fail (GTK_IS_WIDGET (child));
8729 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
8730 g_return_if_fail (child->parent == NULL);
8732 gtk_text_view_ensure_layout (text_view);
8734 vc = text_view_child_new_anchored (child, anchor,
8737 add_child (text_view, vc);
8739 g_assert (vc->widget == child);
8740 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8744 * gtk_text_view_add_child_in_window:
8745 * @text_view: a #GtkTextView
8746 * @child: a #GtkWidget
8747 * @which_window: which window the child should appear in
8748 * @xpos: X position of child in window coordinates
8749 * @ypos: Y position of child in window coordinates
8751 * Adds a child at fixed coordinates in one of the text widget's
8752 * windows. The window must have nonzero size (see
8753 * gtk_text_view_set_border_window_size()). Note that the child
8754 * coordinates are given relative to the #GdkWindow in question, and
8755 * that these coordinates have no sane relationship to scrolling. When
8756 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
8757 * irrelevant, the child floats above all scrollable areas. But when
8758 * placing a child in one of the scrollable windows (border windows or
8759 * text window), you'll need to compute the child's correct position
8760 * in buffer coordinates any time scrolling occurs or buffer changes
8761 * occur, and then call gtk_text_view_move_child() to update the
8762 * child's position. Unfortunately there's no good way to detect that
8763 * scrolling has occurred, using the current API; a possible hack
8764 * would be to update all child positions when the scroll adjustments
8765 * change or the text buffer changes. See bug 64518 on
8766 * bugzilla.gnome.org for status of fixing this issue.
8769 gtk_text_view_add_child_in_window (GtkTextView *text_view,
8771 GtkTextWindowType which_window,
8775 GtkTextViewChild *vc;
8777 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8778 g_return_if_fail (GTK_IS_WIDGET (child));
8779 g_return_if_fail (child->parent == NULL);
8781 vc = text_view_child_new_window (child, which_window,
8784 add_child (text_view, vc);
8786 g_assert (vc->widget == child);
8787 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8791 * gtk_text_view_move_child:
8792 * @text_view: a #GtkTextView
8793 * @child: child widget already added to the text view
8794 * @xpos: new X position in window coordinates
8795 * @ypos: new Y position in window coordinates
8797 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
8800 gtk_text_view_move_child (GtkTextView *text_view,
8805 GtkTextViewChild *vc;
8807 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8808 g_return_if_fail (GTK_IS_WIDGET (child));
8809 g_return_if_fail (child->parent == (GtkWidget*) text_view);
8811 vc = g_object_get_data (G_OBJECT (child),
8812 "gtk-text-view-child");
8814 g_assert (vc != NULL);
8816 if (vc->x == xpos &&
8823 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
8824 gtk_widget_queue_resize (child);
8828 /* Iterator operations */
8831 * gtk_text_view_forward_display_line:
8832 * @text_view: a #GtkTextView
8833 * @iter: a #GtkTextIter
8835 * Moves the given @iter forward by one display (wrapped) line.
8836 * A display line is different from a paragraph. Paragraphs are
8837 * separated by newlines or other paragraph separator characters.
8838 * Display lines are created by line-wrapping a paragraph. If
8839 * wrapping is turned off, display lines and paragraphs will be the
8840 * same. Display lines are divided differently for each view, since
8841 * they depend on the view's width; paragraphs are the same in all
8842 * views, since they depend on the contents of the #GtkTextBuffer.
8844 * Return value: %TRUE if @iter was moved and is not on the end iterator
8847 gtk_text_view_forward_display_line (GtkTextView *text_view,
8850 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8851 g_return_val_if_fail (iter != NULL, FALSE);
8853 gtk_text_view_ensure_layout (text_view);
8855 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
8859 * gtk_text_view_backward_display_line:
8860 * @text_view: a #GtkTextView
8861 * @iter: a #GtkTextIter
8863 * Moves the given @iter backward by one display (wrapped) line.
8864 * A display line is different from a paragraph. Paragraphs are
8865 * separated by newlines or other paragraph separator characters.
8866 * Display lines are created by line-wrapping a paragraph. If
8867 * wrapping is turned off, display lines and paragraphs will be the
8868 * same. Display lines are divided differently for each view, since
8869 * they depend on the view's width; paragraphs are the same in all
8870 * views, since they depend on the contents of the #GtkTextBuffer.
8872 * Return value: %TRUE if @iter was moved and is not on the end iterator
8875 gtk_text_view_backward_display_line (GtkTextView *text_view,
8878 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8879 g_return_val_if_fail (iter != NULL, FALSE);
8881 gtk_text_view_ensure_layout (text_view);
8883 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
8887 * gtk_text_view_forward_display_line_end:
8888 * @text_view: a #GtkTextView
8889 * @iter: a #GtkTextIter
8891 * Moves the given @iter forward to the next display line end.
8892 * A display line is different from a paragraph. Paragraphs are
8893 * separated by newlines or other paragraph separator characters.
8894 * Display lines are created by line-wrapping a paragraph. If
8895 * wrapping is turned off, display lines and paragraphs will be the
8896 * same. Display lines are divided differently for each view, since
8897 * they depend on the view's width; paragraphs are the same in all
8898 * views, since they depend on the contents of the #GtkTextBuffer.
8900 * Return value: %TRUE if @iter was moved and is not on the end iterator
8903 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
8906 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8907 g_return_val_if_fail (iter != NULL, FALSE);
8909 gtk_text_view_ensure_layout (text_view);
8911 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
8915 * gtk_text_view_backward_display_line_start:
8916 * @text_view: a #GtkTextView
8917 * @iter: a #GtkTextIter
8919 * Moves the given @iter backward to the next display line start.
8920 * A display line is different from a paragraph. Paragraphs are
8921 * separated by newlines or other paragraph separator characters.
8922 * Display lines are created by line-wrapping a paragraph. If
8923 * wrapping is turned off, display lines and paragraphs will be the
8924 * same. Display lines are divided differently for each view, since
8925 * they depend on the view's width; paragraphs are the same in all
8926 * views, since they depend on the contents of the #GtkTextBuffer.
8928 * Return value: %TRUE if @iter was moved and is not on the end iterator
8931 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
8934 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8935 g_return_val_if_fail (iter != NULL, FALSE);
8937 gtk_text_view_ensure_layout (text_view);
8939 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
8943 * gtk_text_view_starts_display_line:
8944 * @text_view: a #GtkTextView
8945 * @iter: a #GtkTextIter
8947 * Determines whether @iter is at the start of a display line.
8948 * See gtk_text_view_forward_display_line() for an explanation of
8949 * display lines vs. paragraphs.
8951 * Return value: %TRUE if @iter begins a wrapped line
8954 gtk_text_view_starts_display_line (GtkTextView *text_view,
8955 const GtkTextIter *iter)
8957 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8958 g_return_val_if_fail (iter != NULL, FALSE);
8960 gtk_text_view_ensure_layout (text_view);
8962 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
8966 * gtk_text_view_move_visually:
8967 * @text_view: a #GtkTextView
8968 * @iter: a #GtkTextIter
8969 * @count: number of characters to move (negative moves left,
8970 * positive moves right)
8972 * Move the iterator a given number of characters visually, treating
8973 * it as the strong cursor position. If @count is positive, then the
8974 * new strong cursor position will be @count positions to the right of
8975 * the old cursor position. If @count is negative then the new strong
8976 * cursor position will be @count positions to the left of the old
8979 * In the presence of bi-directional text, the correspondence
8980 * between logical and visual order will depend on the direction
8981 * of the current run, and there may be jumps when the cursor
8982 * is moved off of the end of a run.
8984 * Return value: %TRUE if @iter moved and is not on the end iterator
8987 gtk_text_view_move_visually (GtkTextView *text_view,
8991 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8992 g_return_val_if_fail (iter != NULL, FALSE);
8994 gtk_text_view_ensure_layout (text_view);
8996 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
8999 #define __GTK_TEXT_VIEW_C__
9000 #include "gtkaliasdef.c"