1 /* GTK - The GIMP Toolkit
2 * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
31 #include "gtkbindings.h"
33 #include "gtkimagemenuitem.h"
36 #include "gtkmarshalers.h"
38 #include "gtkmenuitem.h"
39 #include "gtkseparatormenuitem.h"
40 #include "gtksettings.h"
42 #include "gtktextdisplay.h"
43 #include "gtktextview.h"
44 #include "gtkimmulticontext.h"
45 #include "gdk/gdkkeysyms.h"
46 #include "gtkprivate.h"
47 #include "gtksizegroup.h" /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
48 #include "gtktextutil.h"
49 #include "gtkwindow.h"
52 /* How scrolling, validation, exposes, etc. work.
54 * The expose_event handler has the invariant that the onscreen lines
55 * have been validated.
57 * There are two ways that onscreen lines can become invalid. The first
58 * is to change which lines are onscreen. This happens when the value
59 * of a scroll adjustment changes. So the code path begins in
60 * gtk_text_view_value_changed() and goes like this:
61 * - gdk_window_scroll() to reflect the new adjustment value
62 * - validate the lines that were moved onscreen
63 * - gdk_window_process_updates() to handle the exposes immediately
65 * The second way is that you get the "invalidated" signal from the layout,
66 * indicating that lines have become invalid. This code path begins in
67 * invalidated_handler() and goes like this:
68 * - install high-priority idle which does the rest of the steps
69 * - if a scroll is pending from scroll_to_mark(), do the scroll,
70 * jumping to the gtk_text_view_value_changed() code path
71 * - otherwise, validate the onscreen lines
72 * - DO NOT process updates
74 * In both cases, validating the onscreen lines can trigger a scroll
75 * due to maintaining the first_para on the top of the screen.
76 * If validation triggers a scroll, we jump to the top of the code path
77 * for value_changed, and bail out of the current code path.
79 * Also, in size_allocate, if we invalidate some lines from changing
80 * the layout width, we need to go ahead and run the high-priority idle,
81 * because GTK sends exposes right after doing the size allocates without
82 * returning to the main loop. This is also why the high-priority idle
83 * is at a higher priority than resizing.
88 #define DEBUG_VALIDATION_AND_SCROLLING
91 #ifdef DEBUG_VALIDATION_AND_SCROLLING
97 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
98 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
100 #define SPACE_FOR_CURSOR 1
102 struct _GtkTextPendingScroll
105 gdouble within_margin;
113 SET_SCROLL_ADJUSTMENTS,
134 PROP_PIXELS_ABOVE_LINES,
135 PROP_PIXELS_BELOW_LINES,
136 PROP_PIXELS_INSIDE_WRAP,
151 static void gtk_text_view_init (GtkTextView *text_view);
152 static void gtk_text_view_class_init (GtkTextViewClass *klass);
153 static void gtk_text_view_destroy (GtkObject *object);
154 static void gtk_text_view_finalize (GObject *object);
155 static void gtk_text_view_set_property (GObject *object,
159 static void gtk_text_view_get_property (GObject *object,
163 static void gtk_text_view_size_request (GtkWidget *widget,
164 GtkRequisition *requisition);
165 static void gtk_text_view_size_allocate (GtkWidget *widget,
166 GtkAllocation *allocation);
167 static void gtk_text_view_realize (GtkWidget *widget);
168 static void gtk_text_view_unrealize (GtkWidget *widget);
169 static void gtk_text_view_style_set (GtkWidget *widget,
170 GtkStyle *previous_style);
171 static void gtk_text_view_direction_changed (GtkWidget *widget,
172 GtkTextDirection previous_direction);
173 static void gtk_text_view_grab_notify (GtkWidget *widget,
174 gboolean was_grabbed);
175 static void gtk_text_view_state_changed (GtkWidget *widget,
176 GtkStateType previous_state);
178 static gint gtk_text_view_event (GtkWidget *widget,
180 static gint gtk_text_view_key_press_event (GtkWidget *widget,
182 static gint gtk_text_view_key_release_event (GtkWidget *widget,
184 static gint gtk_text_view_button_press_event (GtkWidget *widget,
185 GdkEventButton *event);
186 static gint gtk_text_view_button_release_event (GtkWidget *widget,
187 GdkEventButton *event);
188 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
189 GdkEventFocus *event);
190 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
191 GdkEventFocus *event);
192 static gint gtk_text_view_motion_event (GtkWidget *widget,
193 GdkEventMotion *event);
194 static gint gtk_text_view_expose_event (GtkWidget *widget,
195 GdkEventExpose *expose);
196 static void gtk_text_view_draw_focus (GtkWidget *widget);
197 static gboolean gtk_text_view_focus (GtkWidget *widget,
198 GtkDirectionType direction);
199 static void gtk_text_view_select_all (GtkWidget *widget,
203 /* Source side drag signals */
204 static void gtk_text_view_drag_begin (GtkWidget *widget,
205 GdkDragContext *context);
206 static void gtk_text_view_drag_end (GtkWidget *widget,
207 GdkDragContext *context);
208 static void gtk_text_view_drag_data_get (GtkWidget *widget,
209 GdkDragContext *context,
210 GtkSelectionData *selection_data,
213 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
214 GdkDragContext *context);
216 /* Target side drag signals */
217 static void gtk_text_view_drag_leave (GtkWidget *widget,
218 GdkDragContext *context,
220 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
221 GdkDragContext *context,
225 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
226 GdkDragContext *context,
230 static void gtk_text_view_drag_data_received (GtkWidget *widget,
231 GdkDragContext *context,
234 GtkSelectionData *selection_data,
238 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
240 GtkAdjustment *vadj);
241 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
243 static void gtk_text_view_move_cursor (GtkTextView *text_view,
244 GtkMovementStep step,
246 gboolean extend_selection);
247 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
249 gboolean extend_selection);
250 static void gtk_text_view_move_viewport (GtkTextView *text_view,
253 static void gtk_text_view_set_anchor (GtkTextView *text_view);
254 static void gtk_text_view_scroll_pages (GtkTextView *text_view,
256 gboolean extend_selection);
257 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
259 gboolean extend_selection);
260 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
262 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
265 static void gtk_text_view_backspace (GtkTextView *text_view);
266 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
267 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
268 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
269 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
270 static void gtk_text_view_move_focus (GtkTextView *text_view,
271 GtkDirectionType direction_type);
272 static void gtk_text_view_unselect (GtkTextView *text_view);
274 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
275 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
277 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
278 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
279 GtkTextAttributes *values,
281 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
282 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
283 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
284 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
285 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
286 const GtkTextIter *iter,
287 GdkEventButton *event);
288 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
289 GdkEventButton *event);
290 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
291 const GtkTextIter *iter,
292 GdkEventMotion *event);
293 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
294 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
295 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
297 static void gtk_text_view_value_changed (GtkAdjustment *adj,
299 static void gtk_text_view_commit_handler (GtkIMContext *context,
301 GtkTextView *text_view);
302 static void gtk_text_view_commit_text (GtkTextView *text_view,
304 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
305 GtkTextView *text_view);
306 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
307 GtkTextView *text_view);
308 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
311 GtkTextView *text_view);
313 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
314 const GtkTextIter *location,
317 static void gtk_text_view_get_cursor_location (GtkTextView *text_view,
319 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
322 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
326 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
327 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
329 static void gtk_text_view_do_popup (GtkTextView *text_view,
330 GdkEventButton *event);
332 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
334 gdouble within_margin,
339 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
340 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
341 static void gtk_text_view_invalidate (GtkTextView *text_view);
342 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
344 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
346 /* Container methods */
347 static void gtk_text_view_add (GtkContainer *container,
349 static void gtk_text_view_remove (GtkContainer *container,
351 static void gtk_text_view_forall (GtkContainer *container,
352 gboolean include_internals,
353 GtkCallback callback,
354 gpointer callback_data);
356 /* FIXME probably need the focus methods. */
358 typedef struct _GtkTextViewChild GtkTextViewChild;
360 struct _GtkTextViewChild
364 GtkTextChildAnchor *anchor;
366 gint from_top_of_line;
367 gint from_left_of_buffer;
369 /* These are ignored if anchor != NULL */
370 GtkTextWindowType type;
375 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
376 GtkTextChildAnchor *anchor,
377 GtkTextLayout *layout);
378 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
379 GtkTextWindowType type,
382 static void text_view_child_free (GtkTextViewChild *child);
383 static void text_view_child_set_parent_window (GtkTextView *text_view,
384 GtkTextViewChild *child);
386 struct _GtkTextWindow
388 GtkTextWindowType type;
391 GdkWindow *bin_window;
392 GtkRequisition requisition;
393 GdkRectangle allocation;
396 static GtkTextWindow *text_window_new (GtkTextWindowType type,
399 gint height_request);
400 static void text_window_free (GtkTextWindow *win);
401 static void text_window_realize (GtkTextWindow *win,
403 static void text_window_unrealize (GtkTextWindow *win);
404 static void text_window_size_allocate (GtkTextWindow *win,
406 static void text_window_scroll (GtkTextWindow *win,
409 static void text_window_invalidate_rect (GtkTextWindow *win,
411 static void text_window_invalidate_cursors (GtkTextWindow *win);
413 static gint text_window_get_width (GtkTextWindow *win);
414 static gint text_window_get_height (GtkTextWindow *win);
417 static const GtkTargetEntry target_table[] = {
418 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP, 0 },
421 static GtkContainerClass *parent_class = NULL;
422 static guint signals[LAST_SIGNAL] = { 0 };
425 gtk_text_view_get_type (void)
427 static GType our_type = 0;
431 static const GTypeInfo our_info =
433 sizeof (GtkTextViewClass),
434 NULL, /* base_init */
435 NULL, /* base_finalize */
436 (GClassInitFunc) gtk_text_view_class_init,
437 NULL, /* class_finalize */
438 NULL, /* class_data */
439 sizeof (GtkTextView),
441 (GInstanceInitFunc) gtk_text_view_init,
444 our_type = g_type_register_static (GTK_TYPE_CONTAINER, g_intern_static_string ("GtkTextView"),
452 add_move_binding (GtkBindingSet *binding_set,
455 GtkMovementStep step,
458 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
460 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
464 G_TYPE_BOOLEAN, FALSE);
466 /* Selection-extending version */
467 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
471 G_TYPE_BOOLEAN, TRUE);
475 gtk_text_view_class_init (GtkTextViewClass *klass)
477 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
478 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
479 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
480 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
481 GtkBindingSet *binding_set;
483 parent_class = g_type_class_peek_parent (klass);
485 /* Default handlers and virtual methods
487 gobject_class->set_property = gtk_text_view_set_property;
488 gobject_class->get_property = gtk_text_view_get_property;
490 object_class->destroy = gtk_text_view_destroy;
491 gobject_class->finalize = gtk_text_view_finalize;
493 widget_class->realize = gtk_text_view_realize;
494 widget_class->unrealize = gtk_text_view_unrealize;
495 widget_class->style_set = gtk_text_view_style_set;
496 widget_class->direction_changed = gtk_text_view_direction_changed;
497 widget_class->grab_notify = gtk_text_view_grab_notify;
498 widget_class->state_changed = gtk_text_view_state_changed;
499 widget_class->size_request = gtk_text_view_size_request;
500 widget_class->size_allocate = gtk_text_view_size_allocate;
501 widget_class->event = gtk_text_view_event;
502 widget_class->key_press_event = gtk_text_view_key_press_event;
503 widget_class->key_release_event = gtk_text_view_key_release_event;
504 widget_class->button_press_event = gtk_text_view_button_press_event;
505 widget_class->button_release_event = gtk_text_view_button_release_event;
506 widget_class->focus_in_event = gtk_text_view_focus_in_event;
507 widget_class->focus_out_event = gtk_text_view_focus_out_event;
508 widget_class->motion_notify_event = gtk_text_view_motion_event;
509 widget_class->expose_event = gtk_text_view_expose_event;
510 widget_class->focus = gtk_text_view_focus;
512 widget_class->drag_begin = gtk_text_view_drag_begin;
513 widget_class->drag_end = gtk_text_view_drag_end;
514 widget_class->drag_data_get = gtk_text_view_drag_data_get;
515 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
517 widget_class->drag_leave = gtk_text_view_drag_leave;
518 widget_class->drag_motion = gtk_text_view_drag_motion;
519 widget_class->drag_drop = gtk_text_view_drag_drop;
520 widget_class->drag_data_received = gtk_text_view_drag_data_received;
522 widget_class->popup_menu = gtk_text_view_popup_menu;
524 container_class->add = gtk_text_view_add;
525 container_class->remove = gtk_text_view_remove;
526 container_class->forall = gtk_text_view_forall;
528 klass->move_cursor = gtk_text_view_move_cursor;
529 klass->page_horizontally = gtk_text_view_page_horizontally;
530 klass->set_anchor = gtk_text_view_set_anchor;
531 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
532 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
533 klass->backspace = gtk_text_view_backspace;
534 klass->cut_clipboard = gtk_text_view_cut_clipboard;
535 klass->copy_clipboard = gtk_text_view_copy_clipboard;
536 klass->paste_clipboard = gtk_text_view_paste_clipboard;
537 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
538 klass->move_focus = gtk_text_view_move_focus;
539 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
545 g_object_class_install_property (gobject_class,
546 PROP_PIXELS_ABOVE_LINES,
547 g_param_spec_int ("pixels-above-lines",
548 P_("Pixels Above Lines"),
549 P_("Pixels of blank space above paragraphs"),
553 GTK_PARAM_READWRITE));
555 g_object_class_install_property (gobject_class,
556 PROP_PIXELS_BELOW_LINES,
557 g_param_spec_int ("pixels-below-lines",
558 P_("Pixels Below Lines"),
559 P_("Pixels of blank space below paragraphs"),
563 GTK_PARAM_READWRITE));
565 g_object_class_install_property (gobject_class,
566 PROP_PIXELS_INSIDE_WRAP,
567 g_param_spec_int ("pixels-inside-wrap",
568 P_("Pixels Inside Wrap"),
569 P_("Pixels of blank space between wrapped lines in a paragraph"),
573 GTK_PARAM_READWRITE));
575 g_object_class_install_property (gobject_class,
577 g_param_spec_boolean ("editable",
579 P_("Whether the text can be modified by the user"),
581 GTK_PARAM_READWRITE));
583 g_object_class_install_property (gobject_class,
585 g_param_spec_enum ("wrap-mode",
587 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
590 GTK_PARAM_READWRITE));
592 g_object_class_install_property (gobject_class,
594 g_param_spec_enum ("justification",
596 P_("Left, right, or center justification"),
597 GTK_TYPE_JUSTIFICATION,
599 GTK_PARAM_READWRITE));
601 g_object_class_install_property (gobject_class,
603 g_param_spec_int ("left-margin",
605 P_("Width of the left margin in pixels"),
609 GTK_PARAM_READWRITE));
611 g_object_class_install_property (gobject_class,
613 g_param_spec_int ("right-margin",
615 P_("Width of the right margin in pixels"),
619 GTK_PARAM_READWRITE));
621 g_object_class_install_property (gobject_class,
623 g_param_spec_int ("indent",
625 P_("Amount to indent the paragraph, in pixels"),
629 GTK_PARAM_READWRITE));
631 g_object_class_install_property (gobject_class,
633 g_param_spec_boxed ("tabs",
635 P_("Custom tabs for this text"),
636 PANGO_TYPE_TAB_ARRAY,
637 GTK_PARAM_READWRITE));
639 g_object_class_install_property (gobject_class,
641 g_param_spec_boolean ("cursor-visible",
642 P_("Cursor Visible"),
643 P_("If the insertion cursor is shown"),
645 GTK_PARAM_READWRITE));
647 g_object_class_install_property (gobject_class,
649 g_param_spec_object ("buffer",
651 P_("The buffer which is displayed"),
652 GTK_TYPE_TEXT_BUFFER,
653 GTK_PARAM_READWRITE));
655 g_object_class_install_property (gobject_class,
657 g_param_spec_boolean ("overwrite",
658 P_("Overwrite mode"),
659 P_("Whether entered text overwrites existing contents"),
661 GTK_PARAM_READWRITE));
663 g_object_class_install_property (gobject_class,
665 g_param_spec_boolean ("accepts-tab",
667 P_("Whether Tab will result in a tab character being entered"),
669 GTK_PARAM_READWRITE));
674 gtk_widget_class_install_style_property (widget_class,
675 g_param_spec_boxed ("error-underline-color",
676 P_("Error underline color"),
677 P_("Color with which to draw error-indication underlines"),
679 GTK_PARAM_READABLE));
686 * GtkTextView::move-cursor:
687 * @widget: the object which received the signal
688 * @step: the granularity of the move, as a #GtkMovementStep
689 * @count: the number of @step units to move
690 * @extend_selection: %TRUE if the move should extend the selection
692 * The ::move-cursor signal is a keybinding signal which gets emitted
693 * when the user initiates a cursor movement.
695 * Applications should not connect to it, but may emit it with
696 * g_signal_emit_by_name() if they need to control scrolling
700 signals[MOVE_CURSOR] =
701 g_signal_new ("move_cursor",
702 G_OBJECT_CLASS_TYPE (gobject_class),
703 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
704 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
706 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
708 GTK_TYPE_MOVEMENT_STEP,
712 signals[PAGE_HORIZONTALLY] =
713 g_signal_new ("page_horizontally",
714 G_OBJECT_CLASS_TYPE (gobject_class),
715 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
716 G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
718 _gtk_marshal_VOID__INT_BOOLEAN,
723 signals[MOVE_VIEWPORT] =
724 _gtk_binding_signal_new ("move_viewport",
725 G_OBJECT_CLASS_TYPE (gobject_class),
726 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
727 G_CALLBACK (gtk_text_view_move_viewport),
729 _gtk_marshal_VOID__ENUM_INT,
731 GTK_TYPE_SCROLL_STEP,
734 signals[SET_ANCHOR] =
735 g_signal_new ("set_anchor",
736 G_OBJECT_CLASS_TYPE (gobject_class),
737 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
738 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
740 _gtk_marshal_VOID__VOID,
743 signals[INSERT_AT_CURSOR] =
744 g_signal_new ("insert_at_cursor",
745 G_OBJECT_CLASS_TYPE (gobject_class),
746 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
747 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
749 _gtk_marshal_VOID__STRING,
753 signals[DELETE_FROM_CURSOR] =
754 g_signal_new ("delete_from_cursor",
755 G_OBJECT_CLASS_TYPE (gobject_class),
756 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
757 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
759 _gtk_marshal_VOID__ENUM_INT,
761 GTK_TYPE_DELETE_TYPE,
765 g_signal_new ("backspace",
766 G_OBJECT_CLASS_TYPE (gobject_class),
767 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
768 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
770 _gtk_marshal_VOID__VOID,
773 signals[CUT_CLIPBOARD] =
774 g_signal_new ("cut_clipboard",
775 G_OBJECT_CLASS_TYPE (gobject_class),
776 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
777 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
779 _gtk_marshal_VOID__VOID,
782 signals[COPY_CLIPBOARD] =
783 g_signal_new ("copy_clipboard",
784 G_OBJECT_CLASS_TYPE (gobject_class),
785 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
786 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
788 _gtk_marshal_VOID__VOID,
791 signals[PASTE_CLIPBOARD] =
792 g_signal_new ("paste_clipboard",
793 G_OBJECT_CLASS_TYPE (gobject_class),
794 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
795 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
797 _gtk_marshal_VOID__VOID,
800 signals[TOGGLE_OVERWRITE] =
801 g_signal_new ("toggle_overwrite",
802 G_OBJECT_CLASS_TYPE (gobject_class),
803 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
804 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
806 _gtk_marshal_VOID__VOID,
809 signals[MOVE_FOCUS] =
810 g_signal_new ("move_focus",
811 G_OBJECT_CLASS_TYPE (gobject_class),
812 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
813 G_STRUCT_OFFSET (GtkTextViewClass, move_focus),
815 _gtk_marshal_VOID__ENUM,
817 GTK_TYPE_DIRECTION_TYPE);
819 signals[SET_SCROLL_ADJUSTMENTS] =
820 g_signal_new ("set_scroll_adjustments",
821 G_OBJECT_CLASS_TYPE (gobject_class),
822 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
823 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
825 _gtk_marshal_VOID__OBJECT_OBJECT,
828 GTK_TYPE_ADJUSTMENT);
829 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
831 signals[POPULATE_POPUP] =
832 g_signal_new ("populate_popup",
833 G_OBJECT_CLASS_TYPE (gobject_class),
835 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
837 _gtk_marshal_VOID__OBJECT,
841 signals[SELECT_ALL] =
842 _gtk_binding_signal_new ("select_all",
843 G_OBJECT_CLASS_TYPE (object_class),
844 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
845 G_CALLBACK (gtk_text_view_select_all),
847 _gtk_marshal_VOID__BOOLEAN,
849 G_TYPE_BOOLEAN, TRUE);
856 binding_set = gtk_binding_set_by_class (klass);
858 /* Moving the insertion point */
859 add_move_binding (binding_set, GDK_Right, 0,
860 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
862 add_move_binding (binding_set, GDK_KP_Right, 0,
863 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
865 add_move_binding (binding_set, GDK_Left, 0,
866 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
868 add_move_binding (binding_set, GDK_KP_Left, 0,
869 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
871 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
872 GTK_MOVEMENT_WORDS, 1);
874 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
875 GTK_MOVEMENT_WORDS, 1);
877 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
878 GTK_MOVEMENT_WORDS, -1);
880 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
881 GTK_MOVEMENT_WORDS, 1);
883 add_move_binding (binding_set, GDK_Up, 0,
884 GTK_MOVEMENT_DISPLAY_LINES, -1);
886 add_move_binding (binding_set, GDK_KP_Up, 0,
887 GTK_MOVEMENT_DISPLAY_LINES, -1);
889 add_move_binding (binding_set, GDK_Down, 0,
890 GTK_MOVEMENT_DISPLAY_LINES, 1);
892 add_move_binding (binding_set, GDK_KP_Down, 0,
893 GTK_MOVEMENT_DISPLAY_LINES, 1);
895 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
896 GTK_MOVEMENT_PARAGRAPHS, -1);
898 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
899 GTK_MOVEMENT_PARAGRAPHS, -1);
901 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
902 GTK_MOVEMENT_PARAGRAPHS, 1);
904 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
905 GTK_MOVEMENT_PARAGRAPHS, 1);
907 add_move_binding (binding_set, GDK_Home, 0,
908 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
910 add_move_binding (binding_set, GDK_KP_Home, 0,
911 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
913 add_move_binding (binding_set, GDK_End, 0,
914 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
916 add_move_binding (binding_set, GDK_KP_End, 0,
917 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
919 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
920 GTK_MOVEMENT_BUFFER_ENDS, -1);
922 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
923 GTK_MOVEMENT_BUFFER_ENDS, -1);
925 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
926 GTK_MOVEMENT_BUFFER_ENDS, 1);
928 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
929 GTK_MOVEMENT_BUFFER_ENDS, 1);
931 add_move_binding (binding_set, GDK_Page_Up, 0,
932 GTK_MOVEMENT_PAGES, -1);
934 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
935 GTK_MOVEMENT_PAGES, -1);
937 add_move_binding (binding_set, GDK_Page_Down, 0,
938 GTK_MOVEMENT_PAGES, 1);
940 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
941 GTK_MOVEMENT_PAGES, 1);
943 add_move_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
944 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
946 add_move_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
947 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
949 add_move_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
950 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
952 add_move_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
953 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
956 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
958 G_TYPE_BOOLEAN, TRUE);
960 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
962 G_TYPE_BOOLEAN, TRUE);
965 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
967 G_TYPE_BOOLEAN, FALSE);
970 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
971 "delete_from_cursor", 2,
972 G_TYPE_ENUM, GTK_DELETE_CHARS,
975 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
976 "delete_from_cursor", 2,
977 G_TYPE_ENUM, GTK_DELETE_CHARS,
980 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
983 /* Make this do the same as Backspace, to help with mis-typing */
984 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
987 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
988 "delete_from_cursor", 2,
989 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
992 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
993 "delete_from_cursor", 2,
994 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
997 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
998 "delete_from_cursor", 2,
999 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1002 /* Cut/copy/paste */
1004 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
1005 "cut_clipboard", 0);
1006 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
1007 "copy_clipboard", 0);
1008 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
1009 "paste_clipboard", 0);
1011 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
1012 "cut_clipboard", 0);
1013 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
1014 "copy_clipboard", 0);
1015 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
1016 "paste_clipboard", 0);
1019 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1020 "toggle_overwrite", 0);
1021 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1022 "toggle_overwrite", 0);
1024 /* Control-tab focus motion */
1025 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
1027 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1028 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
1030 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1032 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1034 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1035 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1037 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1041 gtk_text_view_init (GtkTextView *text_view)
1045 widget = GTK_WIDGET (text_view);
1047 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1049 /* Set up default style */
1050 text_view->wrap_mode = GTK_WRAP_NONE;
1051 text_view->pixels_above_lines = 0;
1052 text_view->pixels_below_lines = 0;
1053 text_view->pixels_inside_wrap = 0;
1054 text_view->justify = GTK_JUSTIFY_LEFT;
1055 text_view->left_margin = 0;
1056 text_view->right_margin = 0;
1057 text_view->indent = 0;
1058 text_view->tabs = NULL;
1059 text_view->editable = TRUE;
1061 gtk_drag_dest_set (widget,
1063 target_table, G_N_ELEMENTS (target_table),
1064 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1065 gtk_drag_dest_add_text_targets (widget);
1067 text_view->virtual_cursor_x = -1;
1068 text_view->virtual_cursor_y = -1;
1070 /* This object is completely private. No external entity can gain a reference
1071 * to it; so we create it here and destroy it in finalize ().
1073 text_view->im_context = gtk_im_multicontext_new ();
1075 g_signal_connect (text_view->im_context, "commit",
1076 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1077 g_signal_connect (text_view->im_context, "preedit_changed",
1078 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1079 g_signal_connect (text_view->im_context, "retrieve_surrounding",
1080 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1081 g_signal_connect (text_view->im_context, "delete_surrounding",
1082 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1084 text_view->cursor_visible = TRUE;
1086 text_view->accepts_tab = TRUE;
1088 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1091 text_view->drag_start_x = -1;
1092 text_view->drag_start_y = -1;
1094 text_view->pending_place_cursor_button = 0;
1096 /* We handle all our own redrawing */
1097 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1101 * gtk_text_view_new:
1103 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1104 * before using the text view, an empty default buffer will be created
1105 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1106 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1108 * Return value: a new #GtkTextView
1111 gtk_text_view_new (void)
1113 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1117 * gtk_text_view_new_with_buffer:
1118 * @buffer: a #GtkTextBuffer
1120 * Creates a new #GtkTextView widget displaying the buffer
1121 * @buffer. One buffer can be shared among many widgets.
1122 * @buffer may be NULL to create a default buffer, in which case
1123 * this function is equivalent to gtk_text_view_new(). The
1124 * text view adds its own reference count to the buffer; it does not
1125 * take over an existing reference.
1127 * Return value: a new #GtkTextView.
1130 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1132 GtkTextView *text_view;
1134 text_view = (GtkTextView*)gtk_text_view_new ();
1136 gtk_text_view_set_buffer (text_view, buffer);
1138 return GTK_WIDGET (text_view);
1142 * gtk_text_view_set_buffer:
1143 * @text_view: a #GtkTextView
1144 * @buffer: a #GtkTextBuffer
1146 * Sets @buffer as the buffer being displayed by @text_view. The previous
1147 * buffer displayed by the text view is unreferenced, and a reference is
1148 * added to @buffer. If you owned a reference to @buffer before passing it
1149 * to this function, you must remove that reference yourself; #GtkTextView
1150 * will not "adopt" it.
1154 gtk_text_view_set_buffer (GtkTextView *text_view,
1155 GtkTextBuffer *buffer)
1157 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1158 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1160 if (text_view->buffer == buffer)
1163 if (text_view->buffer != NULL)
1165 /* Destroy all anchored children */
1169 copy = g_slist_copy (text_view->children);
1171 while (tmp_list != NULL)
1173 GtkTextViewChild *vc = tmp_list->data;
1177 gtk_widget_destroy (vc->widget);
1178 /* vc may now be invalid! */
1181 tmp_list = g_slist_next (tmp_list);
1184 g_slist_free (copy);
1186 g_signal_handlers_disconnect_by_func (text_view->buffer,
1187 gtk_text_view_mark_set_handler,
1189 g_object_unref (text_view->buffer);
1190 text_view->dnd_mark = NULL;
1192 if (GTK_WIDGET_REALIZED (text_view))
1194 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1195 GDK_SELECTION_PRIMARY);
1196 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1200 text_view->buffer = buffer;
1206 g_object_ref (buffer);
1208 if (text_view->layout)
1209 gtk_text_layout_set_buffer (text_view->layout, buffer);
1211 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1213 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1217 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1221 text_view->first_para_pixels = 0;
1223 g_signal_connect (text_view->buffer, "mark_set",
1224 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1226 if (GTK_WIDGET_REALIZED (text_view))
1228 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1229 GDK_SELECTION_PRIMARY);
1230 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1234 g_object_notify (G_OBJECT (text_view), "buffer");
1236 if (GTK_WIDGET_VISIBLE (text_view))
1237 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1239 DV(g_print ("Invalidating due to set_buffer\n"));
1240 gtk_text_view_invalidate (text_view);
1243 static GtkTextBuffer*
1244 get_buffer (GtkTextView *text_view)
1246 if (text_view->buffer == NULL)
1249 b = gtk_text_buffer_new (NULL);
1250 gtk_text_view_set_buffer (text_view, b);
1254 return text_view->buffer;
1258 * gtk_text_view_get_buffer:
1259 * @text_view: a #GtkTextView
1261 * Returns the #GtkTextBuffer being displayed by this text view.
1262 * The reference count on the buffer is not incremented; the caller
1263 * of this function won't own a new reference.
1265 * Return value: a #GtkTextBuffer
1268 gtk_text_view_get_buffer (GtkTextView *text_view)
1270 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1272 return get_buffer (text_view);
1276 * gtk_text_view_get_iter_at_location:
1277 * @text_view: a #GtkTextView
1278 * @iter: a #GtkTextIter
1279 * @x: x position, in buffer coordinates
1280 * @y: y position, in buffer coordinates
1282 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1283 * coordinates are coordinates for the entire buffer, not just the
1284 * currently-displayed portion. If you have coordinates from an
1285 * event, you have to convert those to buffer coordinates with
1286 * gtk_text_view_window_to_buffer_coords().
1290 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1295 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1296 g_return_if_fail (iter != NULL);
1298 gtk_text_view_ensure_layout (text_view);
1300 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1305 * gtk_text_view_get_iter_at_position:
1306 * @text_view: a #GtkTextView
1307 * @iter: a #GtkTextIter
1308 * @trailing: location to store an integer indicating where
1309 * in the grapheme the user clicked. It will either be
1310 * zero, or the number of characters in the grapheme.
1311 * 0 represents the trailing edge of the grapheme.
1312 * @x: x position, in buffer coordinates
1313 * @y: y position, in buffer coordinates
1315 * Retrieves the iterator pointing to the character at buffer
1316 * coordinates @x and @y. Buffer coordinates are coordinates for
1317 * the entire buffer, not just the currently-displayed portion.
1318 * If you have coordinates from an event, you have to convert
1319 * those to buffer coordinates with
1320 * gtk_text_view_window_to_buffer_coords().
1322 * Note that this is different from gtk_text_view_get_iter_at_location(),
1323 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1329 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1335 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1336 g_return_if_fail (iter != NULL);
1338 gtk_text_view_ensure_layout (text_view);
1340 gtk_text_layout_get_iter_at_position (text_view->layout,
1341 iter, trailing, x, y);
1345 * gtk_text_view_get_iter_location:
1346 * @text_view: a #GtkTextView
1347 * @iter: a #GtkTextIter
1348 * @location: bounds of the character at @iter
1350 * Gets a rectangle which roughly contains the character at @iter.
1351 * The rectangle position is in buffer coordinates; use
1352 * gtk_text_view_buffer_to_window_coords() to convert these
1353 * coordinates to coordinates for one of the windows in the text view.
1357 gtk_text_view_get_iter_location (GtkTextView *text_view,
1358 const GtkTextIter *iter,
1359 GdkRectangle *location)
1361 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1362 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1364 gtk_text_view_ensure_layout (text_view);
1366 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1370 * gtk_text_view_get_line_yrange:
1371 * @text_view: a #GtkTextView
1372 * @iter: a #GtkTextIter
1373 * @y: return location for a y coordinate
1374 * @height: return location for a height
1376 * Gets the y coordinate of the top of the line containing @iter,
1377 * and the height of the line. The coordinate is a buffer coordinate;
1378 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1382 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1383 const GtkTextIter *iter,
1387 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1388 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1390 gtk_text_view_ensure_layout (text_view);
1392 gtk_text_layout_get_line_yrange (text_view->layout,
1399 * gtk_text_view_get_line_at_y:
1400 * @text_view: a #GtkTextView
1401 * @target_iter: a #GtkTextIter
1402 * @y: a y coordinate
1403 * @line_top: return location for top coordinate of the line
1405 * Gets the #GtkTextIter at the start of the line containing
1406 * the coordinate @y. @y is in buffer coordinates, convert from
1407 * window coordinates with gtk_text_view_window_to_buffer_coords().
1408 * If non-%NULL, @line_top will be filled with the coordinate of the top
1412 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1413 GtkTextIter *target_iter,
1417 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1419 gtk_text_view_ensure_layout (text_view);
1421 gtk_text_layout_get_line_at_y (text_view->layout,
1428 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1430 DV (g_print (" Setting adj to raw value %g\n", val));
1432 /* We don't really want to clamp to upper; we want to clamp to
1433 upper - page_size which is the highest value the scrollbar
1434 will let us reach. */
1435 if (val > (adj->upper - adj->page_size))
1436 val = adj->upper - adj->page_size;
1438 if (val < adj->lower)
1441 if (val != adj->value)
1443 DV (g_print (" Setting adj to clamped value %g\n", val));
1444 gtk_adjustment_set_value (adj, val);
1452 * gtk_text_view_scroll_to_iter:
1453 * @text_view: a #GtkTextView
1454 * @iter: a #GtkTextIter
1455 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1456 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1457 * @xalign: horizontal alignment of mark within visible area.
1458 * @yalign: vertical alignment of mark within visible area
1460 * Scrolls @text_view so that @iter is on the screen in the position
1461 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1462 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1463 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1464 * possibly not scrolling at all. The effective screen for purposes
1465 * of this function is reduced by a margin of size @within_margin.
1466 * NOTE: This function uses the currently-computed height of the
1467 * lines in the text buffer. Note that line heights are computed
1468 * in an idle handler; so this function may not have the desired effect
1469 * if it's called before the height computations. To avoid oddness,
1470 * consider using gtk_text_view_scroll_to_mark() which saves a point
1471 * to be scrolled to after line validation.
1473 * Return value: %TRUE if scrolling occurred
1476 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1478 gdouble within_margin,
1484 GdkRectangle screen;
1489 gboolean retval = FALSE;
1491 gint screen_xoffset, screen_yoffset;
1492 gint current_x_scroll, current_y_scroll;
1494 /* FIXME why don't we do the validate-at-scroll-destination thing
1495 * from flush_scroll in this function? I think it wasn't done before
1496 * because changed_handler was screwed up, but I could be wrong.
1499 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1500 g_return_val_if_fail (iter != NULL, FALSE);
1501 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1502 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1503 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1505 widget = GTK_WIDGET (text_view);
1507 DV(g_print(G_STRLOC"\n"));
1509 gtk_text_layout_get_iter_location (text_view->layout,
1513 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1515 current_x_scroll = text_view->xoffset;
1516 current_y_scroll = text_view->yoffset;
1518 screen.x = current_x_scroll;
1519 screen.y = current_y_scroll;
1520 screen.width = SCREEN_WIDTH (widget);
1521 screen.height = SCREEN_HEIGHT (widget);
1523 screen_xoffset = screen.width * within_margin;
1524 screen_yoffset = screen.height * within_margin;
1526 screen.x += screen_xoffset;
1527 screen.y += screen_yoffset;
1528 screen.width -= screen_xoffset * 2;
1529 screen.height -= screen_yoffset * 2;
1531 /* paranoia check */
1532 if (screen.width < 1)
1534 if (screen.height < 1)
1537 /* The -1 here ensures that we leave enough space to draw the cursor
1538 * when this function is used for horizontal scrolling.
1540 screen_right = screen.x + screen.width - 1;
1541 screen_bottom = screen.y + screen.height;
1543 /* The alignment affects the point in the target character that we
1544 * choose to align. If we're doing right/bottom alignment, we align
1545 * the right/bottom edge of the character the mark is at; if we're
1546 * doing left/top we align the left/top edge of the character; if
1547 * we're doing center alignment we align the center of the
1551 /* Vertical scroll */
1554 scroll_dest = current_y_scroll;
1558 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1560 /* if scroll_dest < screen.y, we move a negative increment (up),
1561 * else a positive increment (down)
1563 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1567 /* move minimum to get onscreen */
1568 if (rect.y < screen.y)
1570 scroll_dest = rect.y;
1571 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1573 else if ((rect.y + rect.height) > screen_bottom)
1575 scroll_dest = rect.y + rect.height;
1576 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1580 if (scroll_inc != 0)
1582 retval = set_adjustment_clamped (get_vadjustment (text_view),
1583 current_y_scroll + scroll_inc);
1585 DV (g_print (" vert increment %d\n", scroll_inc));
1588 /* Horizontal scroll */
1591 scroll_dest = current_x_scroll;
1595 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1597 /* if scroll_dest < screen.y, we move a negative increment (left),
1598 * else a positive increment (right)
1600 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1604 /* move minimum to get onscreen */
1605 if (rect.x < screen.x)
1607 scroll_dest = rect.x;
1608 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1610 else if ((rect.x + rect.width) > screen_right)
1612 scroll_dest = rect.x + rect.width;
1613 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1617 if (scroll_inc != 0)
1619 retval = set_adjustment_clamped (get_hadjustment (text_view),
1620 current_x_scroll + scroll_inc);
1622 DV (g_print (" horiz increment %d\n", scroll_inc));
1626 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1628 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1634 free_pending_scroll (GtkTextPendingScroll *scroll)
1636 if (!gtk_text_mark_get_deleted (scroll->mark))
1637 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1639 g_object_unref (scroll->mark);
1644 cancel_pending_scroll (GtkTextView *text_view)
1646 if (text_view->pending_scroll)
1648 free_pending_scroll (text_view->pending_scroll);
1649 text_view->pending_scroll = NULL;
1654 gtk_text_view_queue_scroll (GtkTextView *text_view,
1656 gdouble within_margin,
1662 GtkTextPendingScroll *scroll;
1664 DV(g_print(G_STRLOC"\n"));
1666 scroll = g_new (GtkTextPendingScroll, 1);
1668 scroll->within_margin = within_margin;
1669 scroll->use_align = use_align;
1670 scroll->xalign = xalign;
1671 scroll->yalign = yalign;
1673 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1675 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1678 gtk_text_mark_get_left_gravity (mark));
1680 g_object_ref (scroll->mark);
1682 cancel_pending_scroll (text_view);
1684 text_view->pending_scroll = scroll;
1688 gtk_text_view_flush_scroll (GtkTextView *text_view)
1691 GtkTextPendingScroll *scroll;
1695 widget = GTK_WIDGET (text_view);
1697 DV(g_print(G_STRLOC"\n"));
1699 if (text_view->pending_scroll == NULL)
1701 DV (g_print ("in flush scroll, no pending scroll\n"));
1705 scroll = text_view->pending_scroll;
1707 /* avoid recursion */
1708 text_view->pending_scroll = NULL;
1710 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1712 /* Validate area around the scroll destination, so the adjustment
1713 * can meaningfully point into that area. We must validate
1714 * enough area to be sure that after we scroll, everything onscreen
1715 * is valid; otherwise, validation will maintain the first para
1716 * in one place, but may push the target iter off the bottom of
1719 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1720 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1721 - (widget->allocation.height * 2),
1722 widget->allocation.height * 2);
1724 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1726 /* Ensure we have updated width/height */
1727 gtk_text_view_update_adjustments (text_view);
1729 retval = gtk_text_view_scroll_to_iter (text_view,
1731 scroll->within_margin,
1736 free_pending_scroll (scroll);
1742 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1744 if (upper != adj->upper)
1746 gdouble min = MAX (0.0, upper - adj->page_size);
1747 gboolean value_changed = FALSE;
1751 if (adj->value > min)
1754 value_changed = TRUE;
1757 gtk_adjustment_changed (adj);
1758 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1762 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1763 gtk_adjustment_value_changed (adj);
1769 gtk_text_view_update_adjustments (GtkTextView *text_view)
1771 gint width = 0, height = 0;
1773 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1775 if (text_view->layout)
1776 gtk_text_layout_get_size (text_view->layout, &width, &height);
1778 /* Make room for the cursor after the last character in the widest line */
1779 width += SPACE_FOR_CURSOR;
1781 if (text_view->width != width || text_view->height != height)
1783 if (text_view->width != width)
1784 text_view->width_changed = TRUE;
1786 text_view->width = width;
1787 text_view->height = height;
1789 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1790 MAX (SCREEN_WIDTH (text_view), width));
1791 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1792 MAX (SCREEN_HEIGHT (text_view), height));
1794 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1796 /* Set up the step sizes; we'll say that a page is
1797 our allocation minus one step, and a step is
1798 1/10 of our allocation. */
1799 text_view->hadjustment->step_increment =
1800 SCREEN_WIDTH (text_view) / 10.0;
1801 text_view->hadjustment->page_increment =
1802 SCREEN_WIDTH (text_view) * 0.9;
1804 text_view->vadjustment->step_increment =
1805 SCREEN_HEIGHT (text_view) / 10.0;
1806 text_view->vadjustment->page_increment =
1807 SCREEN_HEIGHT (text_view) * 0.9;
1809 gtk_adjustment_changed (get_hadjustment (text_view));
1810 gtk_adjustment_changed (get_vadjustment (text_view));
1815 gtk_text_view_update_layout_width (GtkTextView *text_view)
1817 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1819 gtk_text_view_ensure_layout (text_view);
1821 gtk_text_layout_set_screen_width (text_view->layout,
1822 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
1826 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1830 if (text_view->layout == NULL)
1833 gtk_text_view_get_cursor_location (text_view, &area);
1835 area.x -= text_view->xoffset;
1836 area.y -= text_view->yoffset;
1838 /* Width returned by Pango indicates direction of cursor,
1839 * by it's sign more than the size of cursor.
1843 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1847 * gtk_text_view_scroll_to_mark:
1848 * @text_view: a #GtkTextView
1849 * @mark: a #GtkTextMark
1850 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1851 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1852 * @xalign: horizontal alignment of mark within visible area.
1853 * @yalign: vertical alignment of mark within visible area
1855 * Scrolls @text_view so that @mark is on the screen in the position
1856 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1857 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1858 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1859 * possibly not scrolling at all. The effective screen for purposes
1860 * of this function is reduced by a margin of size @within_margin.
1864 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1866 gdouble within_margin,
1871 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1872 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1873 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1874 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1875 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1877 gtk_text_view_queue_scroll (text_view, mark,
1883 /* If no validation is pending, we need to go ahead and force an
1886 if (text_view->layout &&
1887 gtk_text_layout_is_valid (text_view->layout))
1888 gtk_text_view_flush_scroll (text_view);
1892 * gtk_text_view_scroll_mark_onscreen:
1893 * @text_view: a #GtkTextView
1894 * @mark: a mark in the buffer for @text_view
1896 * Scrolls @text_view the minimum distance such that @mark is contained
1897 * within the visible area of the widget.
1901 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1904 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1905 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1907 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1911 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1913 GdkRectangle visible_rect;
1914 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1916 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1918 visible_rect.y + visible_rect.height);
1922 * gtk_text_view_move_mark_onscreen:
1923 * @text_view: a #GtkTextView
1924 * @mark: a #GtkTextMark
1926 * Moves a mark within the buffer so that it's
1927 * located within the currently-visible text area.
1929 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1932 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1937 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1938 g_return_val_if_fail (mark != NULL, FALSE);
1940 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1942 if (clamp_iter_onscreen (text_view, &iter))
1944 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1952 * gtk_text_view_get_visible_rect:
1953 * @text_view: a #GtkTextView
1954 * @visible_rect: rectangle to fill
1956 * Fills @visible_rect with the currently-visible
1957 * region of the buffer, in buffer coordinates. Convert to window coordinates
1958 * with gtk_text_view_buffer_to_window_coords().
1961 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1962 GdkRectangle *visible_rect)
1966 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1968 widget = GTK_WIDGET (text_view);
1972 visible_rect->x = text_view->xoffset;
1973 visible_rect->y = text_view->yoffset;
1974 visible_rect->width = SCREEN_WIDTH (widget);
1975 visible_rect->height = SCREEN_HEIGHT (widget);
1977 DV(g_print(" visible rect: %d,%d %d x %d\n",
1980 visible_rect->width,
1981 visible_rect->height));
1986 * gtk_text_view_set_wrap_mode:
1987 * @text_view: a #GtkTextView
1988 * @wrap_mode: a #GtkWrapMode
1990 * Sets the line wrapping for the view.
1993 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1994 GtkWrapMode wrap_mode)
1996 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1998 if (text_view->wrap_mode != wrap_mode)
2000 text_view->wrap_mode = wrap_mode;
2002 if (text_view->layout)
2004 text_view->layout->default_style->wrap_mode = wrap_mode;
2005 gtk_text_layout_default_style_changed (text_view->layout);
2009 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2013 * gtk_text_view_get_wrap_mode:
2014 * @text_view: a #GtkTextView
2016 * Gets the line wrapping for the view.
2018 * Return value: the line wrap setting
2021 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2023 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2025 return text_view->wrap_mode;
2029 * gtk_text_view_set_editable:
2030 * @text_view: a #GtkTextView
2031 * @setting: whether it's editable
2033 * Sets the default editability of the #GtkTextView. You can override
2034 * this default setting with tags in the buffer, using the "editable"
2035 * attribute of tags.
2038 gtk_text_view_set_editable (GtkTextView *text_view,
2041 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2042 setting = setting != FALSE;
2044 if (text_view->editable != setting)
2046 text_view->editable = setting;
2048 if (text_view->layout)
2050 text_view->layout->default_style->editable = text_view->editable;
2051 gtk_text_layout_default_style_changed (text_view->layout);
2054 g_object_notify (G_OBJECT (text_view), "editable");
2059 * gtk_text_view_get_editable:
2060 * @text_view: a #GtkTextView
2062 * Returns the default editability of the #GtkTextView. Tags in the
2063 * buffer may override this setting for some ranges of text.
2065 * Return value: whether text is editable by default
2068 gtk_text_view_get_editable (GtkTextView *text_view)
2070 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2072 return text_view->editable;
2076 * gtk_text_view_set_pixels_above_lines:
2077 * @text_view: a #GtkTextView
2078 * @pixels_above_lines: pixels above paragraphs
2080 * Sets the default number of blank pixels above paragraphs in @text_view.
2081 * Tags in the buffer for @text_view may override the defaults.
2085 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2086 gint pixels_above_lines)
2088 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2090 if (text_view->pixels_above_lines != pixels_above_lines)
2092 text_view->pixels_above_lines = pixels_above_lines;
2094 if (text_view->layout)
2096 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
2097 gtk_text_layout_default_style_changed (text_view->layout);
2100 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2105 * gtk_text_view_get_pixels_above_lines:
2106 * @text_view: a #GtkTextView
2108 * Gets the default number of pixels to put above paragraphs.
2110 * Return value: default number of pixels above paragraphs
2113 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2115 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2117 return text_view->pixels_above_lines;
2121 * gtk_text_view_set_pixels_below_lines:
2122 * @text_view: a #GtkTextView
2123 * @pixels_below_lines: pixels below paragraphs
2125 * Sets the default number of pixels of blank space
2126 * to put below paragraphs in @text_view. May be overridden
2127 * by tags applied to @text_view's buffer.
2131 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2132 gint pixels_below_lines)
2134 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2136 if (text_view->pixels_below_lines != pixels_below_lines)
2138 text_view->pixels_below_lines = pixels_below_lines;
2140 if (text_view->layout)
2142 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2143 gtk_text_layout_default_style_changed (text_view->layout);
2146 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2151 * gtk_text_view_get_pixels_below_lines:
2152 * @text_view: a #GtkTextView
2154 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2156 * Return value: default number of blank pixels below paragraphs
2159 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2161 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2163 return text_view->pixels_below_lines;
2167 * gtk_text_view_set_pixels_inside_wrap:
2168 * @text_view: a #GtkTextView
2169 * @pixels_inside_wrap: default number of pixels between wrapped lines
2171 * Sets the default number of pixels of blank space to leave between
2172 * display/wrapped lines within a paragraph. May be overridden by
2173 * tags in @text_view's buffer.
2177 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2178 gint pixels_inside_wrap)
2180 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2182 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2184 text_view->pixels_inside_wrap = pixels_inside_wrap;
2186 if (text_view->layout)
2188 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2189 gtk_text_layout_default_style_changed (text_view->layout);
2192 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2197 * gtk_text_view_get_pixels_inside_wrap:
2198 * @text_view: a #GtkTextView
2200 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2202 * Return value: default number of pixels of blank space between wrapped lines
2205 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2207 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2209 return text_view->pixels_inside_wrap;
2213 * gtk_text_view_set_justification:
2214 * @text_view: a #GtkTextView
2215 * @justification: justification
2217 * Sets the default justification of text in @text_view.
2218 * Tags in the view's buffer may override the default.
2222 gtk_text_view_set_justification (GtkTextView *text_view,
2223 GtkJustification justification)
2225 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2227 if (text_view->justify != justification)
2229 text_view->justify = justification;
2231 if (text_view->layout)
2233 text_view->layout->default_style->justification = justification;
2234 gtk_text_layout_default_style_changed (text_view->layout);
2237 g_object_notify (G_OBJECT (text_view), "justification");
2242 * gtk_text_view_get_justification:
2243 * @text_view: a #GtkTextView
2245 * Gets the default justification of paragraphs in @text_view.
2246 * Tags in the buffer may override the default.
2248 * Return value: default justification
2251 gtk_text_view_get_justification (GtkTextView *text_view)
2253 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2255 return text_view->justify;
2259 * gtk_text_view_set_left_margin:
2260 * @text_view: a #GtkTextView
2261 * @left_margin: left margin in pixels
2263 * Sets the default left margin for text in @text_view.
2264 * Tags in the buffer may override the default.
2268 gtk_text_view_set_left_margin (GtkTextView *text_view,
2271 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2273 if (text_view->left_margin != left_margin)
2275 text_view->left_margin = left_margin;
2277 if (text_view->layout)
2279 text_view->layout->default_style->left_margin = left_margin;
2280 gtk_text_layout_default_style_changed (text_view->layout);
2283 g_object_notify (G_OBJECT (text_view), "left-margin");
2288 * gtk_text_view_get_left_margin:
2289 * @text_view: a #GtkTextView
2291 * Gets the default left margin size of paragraphs in the @text_view.
2292 * Tags in the buffer may override the default.
2294 * Return value: left margin in pixels
2297 gtk_text_view_get_left_margin (GtkTextView *text_view)
2299 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2301 return text_view->left_margin;
2305 * gtk_text_view_set_right_margin:
2306 * @text_view: a #GtkTextView
2307 * @right_margin: right margin in pixels
2309 * Sets the default right margin for text in the text view.
2310 * Tags in the buffer may override the default.
2314 gtk_text_view_set_right_margin (GtkTextView *text_view,
2317 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2319 if (text_view->right_margin != right_margin)
2321 text_view->right_margin = right_margin;
2323 if (text_view->layout)
2325 text_view->layout->default_style->right_margin = right_margin;
2326 gtk_text_layout_default_style_changed (text_view->layout);
2329 g_object_notify (G_OBJECT (text_view), "right-margin");
2334 * gtk_text_view_get_right_margin:
2335 * @text_view: a #GtkTextView
2337 * Gets the default right margin for text in @text_view. Tags
2338 * in the buffer may override the default.
2340 * Return value: right margin in pixels
2343 gtk_text_view_get_right_margin (GtkTextView *text_view)
2345 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2347 return text_view->right_margin;
2351 * gtk_text_view_set_indent:
2352 * @text_view: a #GtkTextView
2353 * @indent: indentation in pixels
2355 * Sets the default indentation for paragraphs in @text_view.
2356 * Tags in the buffer may override the default.
2360 gtk_text_view_set_indent (GtkTextView *text_view,
2363 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2365 if (text_view->indent != indent)
2367 text_view->indent = indent;
2369 if (text_view->layout)
2371 text_view->layout->default_style->indent = indent;
2372 gtk_text_layout_default_style_changed (text_view->layout);
2375 g_object_notify (G_OBJECT (text_view), "indent");
2380 * gtk_text_view_get_indent:
2381 * @text_view: a #GtkTextView
2383 * Gets the default indentation of paragraphs in @text_view.
2384 * Tags in the view's buffer may override the default.
2385 * The indentation may be negative.
2387 * Return value: number of pixels of indentation
2390 gtk_text_view_get_indent (GtkTextView *text_view)
2392 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2394 return text_view->indent;
2398 * gtk_text_view_set_tabs:
2399 * @text_view: a #GtkTextView
2400 * @tabs: tabs as a #PangoTabArray
2402 * Sets the default tab stops for paragraphs in @text_view.
2403 * Tags in the buffer may override the default.
2407 gtk_text_view_set_tabs (GtkTextView *text_view,
2408 PangoTabArray *tabs)
2410 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2412 if (text_view->tabs)
2413 pango_tab_array_free (text_view->tabs);
2415 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2417 if (text_view->layout)
2419 /* some unkosher futzing in internal struct details... */
2420 if (text_view->layout->default_style->tabs)
2421 pango_tab_array_free (text_view->layout->default_style->tabs);
2423 text_view->layout->default_style->tabs =
2424 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2426 gtk_text_layout_default_style_changed (text_view->layout);
2429 g_object_notify (G_OBJECT (text_view), "tabs");
2433 * gtk_text_view_get_tabs:
2434 * @text_view: a #GtkTextView
2436 * Gets the default tabs for @text_view. Tags in the buffer may
2437 * override the defaults. The returned array will be %NULL if
2438 * "standard" (8-space) tabs are used. Free the return value
2439 * with pango_tab_array_free().
2441 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2444 gtk_text_view_get_tabs (GtkTextView *text_view)
2446 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2448 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2452 * gtk_text_view_set_cursor_visible:
2453 * @text_view: a #GtkTextView
2454 * @setting: whether to show the insertion cursor
2456 * Toggles whether the insertion point is displayed. A buffer with no editable
2457 * text probably shouldn't have a visible cursor, so you may want to turn
2461 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2464 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2466 setting = (setting != FALSE);
2468 if (text_view->cursor_visible != setting)
2470 text_view->cursor_visible = setting;
2472 if (GTK_WIDGET_HAS_FOCUS (text_view))
2474 if (text_view->layout)
2476 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2477 gtk_text_view_check_cursor_blink (text_view);
2481 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2486 * gtk_text_view_get_cursor_visible:
2487 * @text_view: a #GtkTextView
2489 * Find out whether the cursor is being displayed.
2491 * Return value: whether the insertion mark is visible
2494 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2496 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2498 return text_view->cursor_visible;
2503 * gtk_text_view_place_cursor_onscreen:
2504 * @text_view: a #GtkTextView
2506 * Moves the cursor to the currently visible region of the
2507 * buffer, it it isn't there already.
2509 * Return value: TRUE if the cursor had to be moved.
2512 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2516 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2518 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2519 gtk_text_buffer_get_mark (get_buffer (text_view),
2522 if (clamp_iter_onscreen (text_view, &insert))
2524 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2532 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2534 if (text_view->first_validate_idle != 0)
2536 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2537 g_source_remove (text_view->first_validate_idle);
2538 text_view->first_validate_idle = 0;
2541 if (text_view->incremental_validate_idle != 0)
2543 g_source_remove (text_view->incremental_validate_idle);
2544 text_view->incremental_validate_idle = 0;
2549 gtk_text_view_destroy (GtkObject *object)
2551 GtkTextView *text_view;
2552 GtkTextLayout *layout;
2554 text_view = GTK_TEXT_VIEW (object);
2556 layout = text_view->layout;
2558 gtk_text_view_remove_validate_idles (text_view);
2559 gtk_text_view_set_buffer (text_view, NULL);
2560 gtk_text_view_destroy_layout (text_view);
2562 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2566 gtk_text_view_finalize (GObject *object)
2568 GtkTextView *text_view;
2570 text_view = GTK_TEXT_VIEW (object);
2572 g_return_if_fail (text_view->buffer == NULL);
2574 gtk_text_view_destroy_layout (text_view);
2575 gtk_text_view_set_buffer (text_view, NULL);
2577 cancel_pending_scroll (text_view);
2579 if (text_view->tabs)
2580 pango_tab_array_free (text_view->tabs);
2582 if (text_view->hadjustment)
2583 g_object_unref (text_view->hadjustment);
2584 if (text_view->vadjustment)
2585 g_object_unref (text_view->vadjustment);
2587 text_window_free (text_view->text_window);
2589 if (text_view->left_window)
2590 text_window_free (text_view->left_window);
2592 if (text_view->top_window)
2593 text_window_free (text_view->top_window);
2595 if (text_view->right_window)
2596 text_window_free (text_view->right_window);
2598 if (text_view->bottom_window)
2599 text_window_free (text_view->bottom_window);
2601 g_object_unref (text_view->im_context);
2603 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2607 gtk_text_view_set_property (GObject *object,
2609 const GValue *value,
2612 GtkTextView *text_view;
2614 text_view = GTK_TEXT_VIEW (object);
2618 case PROP_PIXELS_ABOVE_LINES:
2619 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2622 case PROP_PIXELS_BELOW_LINES:
2623 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2626 case PROP_PIXELS_INSIDE_WRAP:
2627 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2631 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2634 case PROP_WRAP_MODE:
2635 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2638 case PROP_JUSTIFICATION:
2639 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2642 case PROP_LEFT_MARGIN:
2643 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2646 case PROP_RIGHT_MARGIN:
2647 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2651 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2655 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2658 case PROP_CURSOR_VISIBLE:
2659 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2662 case PROP_OVERWRITE:
2663 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
2667 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
2670 case PROP_ACCEPTS_TAB:
2671 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
2675 g_assert_not_reached ();
2681 gtk_text_view_get_property (GObject *object,
2686 GtkTextView *text_view;
2688 text_view = GTK_TEXT_VIEW (object);
2692 case PROP_PIXELS_ABOVE_LINES:
2693 g_value_set_int (value, text_view->pixels_above_lines);
2696 case PROP_PIXELS_BELOW_LINES:
2697 g_value_set_int (value, text_view->pixels_below_lines);
2700 case PROP_PIXELS_INSIDE_WRAP:
2701 g_value_set_int (value, text_view->pixels_inside_wrap);
2705 g_value_set_boolean (value, text_view->editable);
2708 case PROP_WRAP_MODE:
2709 g_value_set_enum (value, text_view->wrap_mode);
2712 case PROP_JUSTIFICATION:
2713 g_value_set_enum (value, text_view->justify);
2716 case PROP_LEFT_MARGIN:
2717 g_value_set_int (value, text_view->left_margin);
2720 case PROP_RIGHT_MARGIN:
2721 g_value_set_int (value, text_view->right_margin);
2725 g_value_set_int (value, text_view->indent);
2729 g_value_set_boxed (value, text_view->tabs);
2732 case PROP_CURSOR_VISIBLE:
2733 g_value_set_boolean (value, text_view->cursor_visible);
2737 g_value_set_object (value, get_buffer (text_view));
2740 case PROP_OVERWRITE:
2741 g_value_set_boolean (value, text_view->overwrite_mode);
2744 case PROP_ACCEPTS_TAB:
2745 g_value_set_boolean (value, text_view->accepts_tab);
2749 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2755 gtk_text_view_size_request (GtkWidget *widget,
2756 GtkRequisition *requisition)
2758 GtkTextView *text_view;
2760 gint focus_edge_width;
2762 gboolean interior_focus;
2764 text_view = GTK_TEXT_VIEW (widget);
2766 gtk_widget_style_get (widget,
2767 "interior-focus", &interior_focus,
2768 "focus-line-width", &focus_width,
2772 focus_edge_width = 0;
2774 focus_edge_width = focus_width;
2776 if (text_view->layout)
2778 text_view->text_window->requisition.width = text_view->layout->width;
2779 text_view->text_window->requisition.height = text_view->layout->height;
2783 text_view->text_window->requisition.width = 0;
2784 text_view->text_window->requisition.height = 0;
2787 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2788 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2790 if (text_view->left_window)
2791 requisition->width += text_view->left_window->requisition.width;
2793 if (text_view->right_window)
2794 requisition->width += text_view->right_window->requisition.width;
2796 if (text_view->top_window)
2797 requisition->height += text_view->top_window->requisition.height;
2799 if (text_view->bottom_window)
2800 requisition->height += text_view->bottom_window->requisition.height;
2802 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2803 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2805 tmp_list = text_view->children;
2806 while (tmp_list != NULL)
2808 GtkTextViewChild *child = tmp_list->data;
2812 GtkRequisition child_req;
2813 GtkRequisition old_req;
2815 gtk_widget_get_child_requisition (child->widget, &old_req);
2817 gtk_widget_size_request (child->widget, &child_req);
2819 gtk_widget_get_child_requisition (child->widget, &child_req);
2821 /* Invalidate layout lines if required */
2822 if (text_view->layout &&
2823 (old_req.width != child_req.width ||
2824 old_req.height != child_req.height))
2825 gtk_text_child_anchor_queue_resize (child->anchor,
2830 GtkRequisition child_req;
2832 gtk_widget_size_request (child->widget, &child_req);
2835 tmp_list = g_slist_next (tmp_list);
2840 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2841 GtkTextViewChild *vc,
2842 GtkAllocation *allocation)
2848 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2852 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2855 buffer_y += vc->from_top_of_line;
2857 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2858 allocation->y = buffer_y - text_view->yoffset;
2860 gtk_widget_get_child_requisition (vc->widget, &req);
2861 allocation->width = req.width;
2862 allocation->height = req.height;
2866 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2867 GtkTextViewChild *vc)
2869 GtkAllocation allocation;
2871 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2873 gtk_widget_size_allocate (vc->widget, &allocation);
2876 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2878 vc->widget->allocation.x,
2879 vc->widget->allocation.y,
2880 text_view->yoffset);
2885 gtk_text_view_child_allocated (GtkTextLayout *layout,
2891 GtkTextViewChild *vc = NULL;
2892 GtkTextView *text_view = data;
2894 /* x,y is the position of the child from the top of the line, and
2895 * from the left of the buffer. We have to translate that into text
2896 * window coordinates, then size_allocate the child.
2899 vc = g_object_get_data (G_OBJECT (child),
2900 "gtk-text-view-child");
2902 g_assert (vc != NULL);
2904 DV (g_print ("child allocated at %d,%d\n", x, y));
2906 vc->from_left_of_buffer = x;
2907 vc->from_top_of_line = y;
2909 gtk_text_view_update_child_allocation (text_view, vc);
2913 gtk_text_view_allocate_children (GtkTextView *text_view)
2917 DV(g_print(G_STRLOC"\n"));
2919 tmp_list = text_view->children;
2920 while (tmp_list != NULL)
2922 GtkTextViewChild *child = tmp_list->data;
2926 /* We need to force-validate the regions containing
2929 GtkTextIter child_loc;
2930 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2934 /* Since anchored children are only ever allocated from
2935 * gtk_text_layout_get_line_display() we have to make sure
2936 * that the display line caching in the layout doesn't
2937 * get in the way. Invalidating the layout around the anchor
2940 if (GTK_WIDGET_ALLOC_NEEDED (child->widget))
2942 GtkTextIter end = child_loc;
2943 gtk_text_iter_forward_char (&end);
2944 gtk_text_layout_invalidate (text_view->layout, &child_loc, &end);
2947 gtk_text_layout_validate_yrange (text_view->layout,
2953 GtkAllocation allocation;
2954 GtkRequisition child_req;
2956 g_assert (child != NULL);
2958 allocation.x = child->x;
2959 allocation.y = child->y;
2961 gtk_widget_get_child_requisition (child->widget, &child_req);
2963 allocation.width = child_req.width;
2964 allocation.height = child_req.height;
2966 gtk_widget_size_allocate (child->widget, &allocation);
2969 tmp_list = g_slist_next (tmp_list);
2974 gtk_text_view_size_allocate (GtkWidget *widget,
2975 GtkAllocation *allocation)
2977 GtkTextView *text_view;
2978 GtkTextIter first_para;
2981 GdkRectangle text_rect;
2982 GdkRectangle left_rect;
2983 GdkRectangle right_rect;
2984 GdkRectangle top_rect;
2985 GdkRectangle bottom_rect;
2986 gint focus_edge_width;
2988 gboolean interior_focus;
2989 gboolean size_changed;
2991 text_view = GTK_TEXT_VIEW (widget);
2993 DV(g_print(G_STRLOC"\n"));
2996 widget->allocation.width != allocation->width ||
2997 widget->allocation.height != allocation->height;
2999 widget->allocation = *allocation;
3001 if (GTK_WIDGET_REALIZED (widget))
3003 gdk_window_move_resize (widget->window,
3004 allocation->x, allocation->y,
3005 allocation->width, allocation->height);
3008 /* distribute width/height among child windows. Ensure all
3009 * windows get at least a 1x1 allocation.
3012 gtk_widget_style_get (widget,
3013 "interior-focus", &interior_focus,
3014 "focus-line-width", &focus_width,
3018 focus_edge_width = 0;
3020 focus_edge_width = focus_width;
3022 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3024 if (text_view->left_window)
3025 left_rect.width = text_view->left_window->requisition.width;
3027 left_rect.width = 0;
3029 width -= left_rect.width;
3031 if (text_view->right_window)
3032 right_rect.width = text_view->right_window->requisition.width;
3034 right_rect.width = 0;
3036 width -= right_rect.width;
3038 text_rect.width = MAX (1, width);
3040 top_rect.width = text_rect.width;
3041 bottom_rect.width = text_rect.width;
3044 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3046 if (text_view->top_window)
3047 top_rect.height = text_view->top_window->requisition.height;
3049 top_rect.height = 0;
3051 height -= top_rect.height;
3053 if (text_view->bottom_window)
3054 bottom_rect.height = text_view->bottom_window->requisition.height;
3056 bottom_rect.height = 0;
3058 height -= bottom_rect.height;
3060 text_rect.height = MAX (1, height);
3062 left_rect.height = text_rect.height;
3063 right_rect.height = text_rect.height;
3066 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3067 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3069 text_rect.x = left_rect.x + left_rect.width;
3070 text_rect.y = top_rect.y + top_rect.height;
3072 left_rect.y = text_rect.y;
3073 right_rect.y = text_rect.y;
3075 top_rect.x = text_rect.x;
3076 bottom_rect.x = text_rect.x;
3078 right_rect.x = text_rect.x + text_rect.width;
3079 bottom_rect.y = text_rect.y + text_rect.height;
3081 text_window_size_allocate (text_view->text_window,
3084 if (text_view->left_window)
3085 text_window_size_allocate (text_view->left_window,
3088 if (text_view->right_window)
3089 text_window_size_allocate (text_view->right_window,
3092 if (text_view->top_window)
3093 text_window_size_allocate (text_view->top_window,
3096 if (text_view->bottom_window)
3097 text_window_size_allocate (text_view->bottom_window,
3100 gtk_text_view_update_layout_width (text_view);
3102 /* Note that this will do some layout validation */
3103 gtk_text_view_allocate_children (text_view);
3105 /* Ensure h/v adj exist */
3106 get_hadjustment (text_view);
3107 get_vadjustment (text_view);
3109 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
3110 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
3111 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
3112 text_view->hadjustment->lower = 0;
3113 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
3116 if (text_view->hadjustment->value > text_view->hadjustment->upper - text_view->hadjustment->page_size)
3117 gtk_adjustment_set_value (text_view->hadjustment, MAX (0, text_view->hadjustment->upper - text_view->hadjustment->page_size));
3119 gtk_adjustment_changed (text_view->hadjustment);
3121 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
3122 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
3123 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
3124 text_view->vadjustment->lower = 0;
3125 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
3128 /* Now adjust the value of the adjustment to keep the cursor at the
3129 * same place in the buffer
3131 gtk_text_view_get_first_para_iter (text_view, &first_para);
3132 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
3134 y += text_view->first_para_pixels;
3136 if (y > text_view->vadjustment->upper - text_view->vadjustment->page_size)
3137 y = MAX (0, text_view->vadjustment->upper - text_view->vadjustment->page_size);
3139 if (y != text_view->yoffset)
3140 gtk_adjustment_set_value (text_view->vadjustment, y);
3142 gtk_adjustment_changed (text_view->vadjustment);
3144 /* The GTK resize loop processes all the pending exposes right
3145 * after doing the resize stuff, so the idle sizer won't have a
3146 * chance to run. So we do the work here.
3148 gtk_text_view_flush_first_validate (text_view);
3150 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3153 if (size_changed && GTK_WIDGET_REALIZED (widget))
3154 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3158 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3161 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3162 text_view->first_para_mark);
3166 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3168 GtkWidget *widget = GTK_WIDGET (text_view);
3170 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3172 if (SCREEN_HEIGHT (widget) > 0)
3174 GtkTextIter first_para;
3176 /* Be sure we've validated the stuff onscreen; if we
3177 * scrolled, these calls won't have any effect, because
3178 * they were called in the recursive validate_onscreen
3180 gtk_text_view_get_first_para_iter (text_view, &first_para);
3182 gtk_text_layout_validate_yrange (text_view->layout,
3185 text_view->first_para_pixels +
3186 SCREEN_HEIGHT (widget));
3189 text_view->onscreen_validated = TRUE;
3191 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3193 /* This can have the odd side effect of triggering a scroll, which should
3194 * flip "onscreen_validated" back to FALSE, but should also get us
3195 * back into this function to turn it on again.
3197 gtk_text_view_update_adjustments (text_view);
3199 g_assert (text_view->onscreen_validated);
3203 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3205 if (text_view->first_validate_idle == 0)
3208 /* Do this first, which means that if an "invalidate"
3209 * occurs during any of this process, a new first_validate_callback
3210 * will be installed, and we'll start again.
3212 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3213 g_source_remove (text_view->first_validate_idle);
3214 text_view->first_validate_idle = 0;
3216 /* be sure we have up-to-date screen size set on the
3219 gtk_text_view_update_layout_width (text_view);
3221 /* Bail out if we invalidated stuff; scrolling right away will just
3222 * confuse the issue.
3224 if (text_view->first_validate_idle != 0)
3226 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3230 /* scroll to any marks, if that's pending. This can jump us to
3231 * the validation codepath used for scrolling onscreen, if so we
3232 * bail out. It won't jump if already in that codepath since
3233 * value_changed is not recursive, so also validate if
3236 if (!gtk_text_view_flush_scroll (text_view) ||
3237 !text_view->onscreen_validated)
3238 gtk_text_view_validate_onscreen (text_view);
3240 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3242 g_assert (text_view->onscreen_validated);
3247 first_validate_callback (gpointer data)
3249 GtkTextView *text_view = data;
3251 GDK_THREADS_ENTER ();
3253 /* Note that some of this code is duplicated at the end of size_allocate,
3254 * keep in sync with that.
3257 DV(g_print(G_STRLOC"\n"));
3259 gtk_text_view_flush_first_validate (text_view);
3261 GDK_THREADS_LEAVE ();
3267 incremental_validate_callback (gpointer data)
3269 GtkTextView *text_view = data;
3270 gboolean result = TRUE;
3272 GDK_THREADS_ENTER ();
3274 DV(g_print(G_STRLOC"\n"));
3276 gtk_text_layout_validate (text_view->layout, 2000);
3278 gtk_text_view_update_adjustments (text_view);
3280 if (gtk_text_layout_is_valid (text_view->layout))
3282 text_view->incremental_validate_idle = 0;
3286 GDK_THREADS_LEAVE ();
3292 gtk_text_view_invalidate (GtkTextView *text_view)
3294 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3295 text_view->onscreen_validated));
3297 text_view->onscreen_validated = FALSE;
3299 /* We'll invalidate when the layout is created */
3300 if (text_view->layout == NULL)
3303 if (!text_view->first_validate_idle)
3305 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3306 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3307 text_view->first_validate_idle));
3310 if (!text_view->incremental_validate_idle)
3312 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3313 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3314 text_view->incremental_validate_idle));
3319 invalidated_handler (GtkTextLayout *layout,
3322 GtkTextView *text_view;
3324 text_view = GTK_TEXT_VIEW (data);
3326 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3327 gtk_text_view_invalidate (text_view);
3331 changed_handler (GtkTextLayout *layout,
3337 GtkTextView *text_view;
3339 GdkRectangle visible_rect;
3340 GdkRectangle redraw_rect;
3342 text_view = GTK_TEXT_VIEW (data);
3343 widget = GTK_WIDGET (data);
3345 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3347 if (GTK_WIDGET_REALIZED (text_view))
3349 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3351 redraw_rect.x = visible_rect.x;
3352 redraw_rect.width = visible_rect.width;
3353 redraw_rect.y = start_y;
3355 if (old_height == new_height)
3356 redraw_rect.height = old_height;
3357 else if (start_y + old_height > visible_rect.y)
3358 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3360 redraw_rect.height = 0;
3362 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3364 /* text_window_invalidate_rect() takes buffer coordinates */
3365 text_window_invalidate_rect (text_view->text_window,
3368 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3372 redraw_rect.height));
3374 if (text_view->left_window)
3375 text_window_invalidate_rect (text_view->left_window,
3377 if (text_view->right_window)
3378 text_window_invalidate_rect (text_view->right_window,
3380 if (text_view->top_window)
3381 text_window_invalidate_rect (text_view->top_window,
3383 if (text_view->bottom_window)
3384 text_window_invalidate_rect (text_view->bottom_window,
3387 gtk_text_view_update_im_spot_location (text_view);
3391 if (old_height != new_height)
3393 gboolean yoffset_changed = FALSE;
3395 int new_first_para_top;
3396 int old_first_para_top;
3399 /* If the bottom of the old area was above the top of the
3400 * screen, we need to scroll to keep the current top of the
3401 * screen in place. Remember that first_para_pixels is the
3402 * position of the top of the screen in coordinates relative to
3403 * the first paragraph onscreen.
3405 * In short we are adding the height delta of the portion of the
3406 * changed region above first_para_mark to text_view->yoffset.
3408 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3409 text_view->first_para_mark);
3411 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3413 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3415 if (new_first_para_top != old_first_para_top)
3417 text_view->yoffset += new_first_para_top - old_first_para_top;
3419 get_vadjustment (text_view)->value = text_view->yoffset;
3420 yoffset_changed = TRUE;
3423 if (yoffset_changed)
3425 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3426 gtk_adjustment_value_changed (get_vadjustment (text_view));
3429 /* FIXME be smarter about which anchored widgets we update */
3431 tmp_list = text_view->children;
3432 while (tmp_list != NULL)
3434 GtkTextViewChild *child = tmp_list->data;
3437 gtk_text_view_update_child_allocation (text_view, child);
3439 tmp_list = g_slist_next (tmp_list);
3444 GtkRequisition old_req;
3445 GtkRequisition new_req;
3447 old_req = widget->requisition;
3449 /* Use this instead of gtk_widget_size_request wrapper
3450 * to avoid the optimization which just returns widget->requisition
3451 * if a resize hasn't been queued.
3453 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3455 if (old_req.width != new_req.width ||
3456 old_req.height != new_req.height)
3458 gtk_widget_queue_resize_no_redraw (widget);
3464 gtk_text_view_realize (GtkWidget *widget)
3466 GtkTextView *text_view;
3467 GdkWindowAttr attributes;
3468 gint attributes_mask;
3471 text_view = GTK_TEXT_VIEW (widget);
3472 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3474 attributes.window_type = GDK_WINDOW_CHILD;
3475 attributes.x = widget->allocation.x;
3476 attributes.y = widget->allocation.y;
3477 attributes.width = widget->allocation.width;
3478 attributes.height = widget->allocation.height;
3479 attributes.wclass = GDK_INPUT_OUTPUT;
3480 attributes.visual = gtk_widget_get_visual (widget);
3481 attributes.colormap = gtk_widget_get_colormap (widget);
3482 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3484 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3486 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3487 &attributes, attributes_mask);
3488 gdk_window_set_user_data (widget->window, widget);
3490 /* must come before text_window_realize calls */
3491 widget->style = gtk_style_attach (widget->style, widget->window);
3493 gdk_window_set_background (widget->window,
3494 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3496 text_window_realize (text_view->text_window, widget->window);
3498 if (text_view->left_window)
3499 text_window_realize (text_view->left_window,
3502 if (text_view->top_window)
3503 text_window_realize (text_view->top_window,
3506 if (text_view->right_window)
3507 text_window_realize (text_view->right_window,
3510 if (text_view->bottom_window)
3511 text_window_realize (text_view->bottom_window,
3514 gtk_text_view_ensure_layout (text_view);
3516 if (text_view->buffer)
3518 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3519 GDK_SELECTION_PRIMARY);
3520 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3523 tmp_list = text_view->children;
3524 while (tmp_list != NULL)
3526 GtkTextViewChild *vc = tmp_list->data;
3528 text_view_child_set_parent_window (text_view, vc);
3530 tmp_list = tmp_list->next;
3535 gtk_text_view_unrealize (GtkWidget *widget)
3537 GtkTextView *text_view;
3539 text_view = GTK_TEXT_VIEW (widget);
3541 if (text_view->buffer)
3543 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3544 GDK_SELECTION_PRIMARY);
3545 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3548 gtk_text_view_remove_validate_idles (text_view);
3550 if (text_view->popup_menu)
3552 gtk_widget_destroy (text_view->popup_menu);
3553 text_view->popup_menu = NULL;
3556 text_window_unrealize (text_view->text_window);
3558 if (text_view->left_window)
3559 text_window_unrealize (text_view->left_window);
3561 if (text_view->top_window)
3562 text_window_unrealize (text_view->top_window);
3564 if (text_view->right_window)
3565 text_window_unrealize (text_view->right_window);
3567 if (text_view->bottom_window)
3568 text_window_unrealize (text_view->bottom_window);
3570 gtk_text_view_destroy_layout (text_view);
3572 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3576 gtk_text_view_set_background (GtkTextView *text_view)
3578 GtkWidget *widget = GTK_WIDGET (text_view);
3580 gdk_window_set_background (widget->window,
3581 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3583 gdk_window_set_background (text_view->text_window->bin_window,
3584 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3586 if (text_view->left_window)
3587 gdk_window_set_background (text_view->left_window->bin_window,
3588 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3589 if (text_view->right_window)
3590 gdk_window_set_background (text_view->right_window->bin_window,
3591 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3593 if (text_view->top_window)
3594 gdk_window_set_background (text_view->top_window->bin_window,
3595 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3597 if (text_view->bottom_window)
3598 gdk_window_set_background (text_view->bottom_window->bin_window,
3599 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3603 gtk_text_view_style_set (GtkWidget *widget,
3604 GtkStyle *previous_style)
3606 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3608 if (GTK_WIDGET_REALIZED (widget))
3610 gtk_text_view_set_background (text_view);
3613 if (text_view->layout && previous_style)
3615 gtk_text_view_set_attributes_from_style (text_view,
3616 text_view->layout->default_style,
3618 gtk_text_layout_default_style_changed (text_view->layout);
3623 gtk_text_view_direction_changed (GtkWidget *widget,
3624 GtkTextDirection previous_direction)
3626 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3628 if (text_view->layout)
3630 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3631 gtk_text_layout_default_style_changed (text_view->layout);
3636 gtk_text_view_state_changed (GtkWidget *widget,
3637 GtkStateType previous_state)
3639 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3641 if (GTK_WIDGET_REALIZED (widget))
3643 gtk_text_view_set_background (text_view);
3646 if (!GTK_WIDGET_IS_SENSITIVE (widget))
3648 /* Clear any selection */
3649 gtk_text_view_unselect (text_view);
3652 gtk_widget_queue_draw (widget);
3656 set_invisible_cursor (GdkWindow *window)
3658 GdkBitmap *empty_bitmap;
3661 char invisible_cursor_bits[] = { 0x0 };
3663 useless.red = useless.green = useless.blue = 0;
3666 empty_bitmap = gdk_bitmap_create_from_data (window,
3667 invisible_cursor_bits,
3670 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3675 gdk_window_set_cursor (window, cursor);
3677 gdk_cursor_unref (cursor);
3679 g_object_unref (empty_bitmap);
3683 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3685 if (text_view->mouse_cursor_obscured)
3688 set_invisible_cursor (text_view->text_window->bin_window);
3690 text_view->mouse_cursor_obscured = TRUE;
3694 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
3696 if (text_view->mouse_cursor_obscured)
3700 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
3702 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3703 gdk_cursor_unref (cursor);
3704 text_view->mouse_cursor_obscured = FALSE;
3709 gtk_text_view_grab_notify (GtkWidget *widget,
3710 gboolean was_grabbed)
3713 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
3722 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3725 switch (event->type)
3727 case GDK_MOTION_NOTIFY:
3728 *x = event->motion.x;
3729 *y = event->motion.y;
3733 case GDK_BUTTON_PRESS:
3734 case GDK_2BUTTON_PRESS:
3735 case GDK_3BUTTON_PRESS:
3736 case GDK_BUTTON_RELEASE:
3737 *x = event->button.x;
3738 *y = event->button.y;
3743 case GDK_KEY_RELEASE:
3744 case GDK_ENTER_NOTIFY:
3745 case GDK_LEAVE_NOTIFY:
3746 case GDK_PROPERTY_NOTIFY:
3747 case GDK_SELECTION_CLEAR:
3748 case GDK_SELECTION_REQUEST:
3749 case GDK_SELECTION_NOTIFY:
3750 case GDK_PROXIMITY_IN:
3751 case GDK_PROXIMITY_OUT:
3752 case GDK_DRAG_ENTER:
3753 case GDK_DRAG_LEAVE:
3754 case GDK_DRAG_MOTION:
3755 case GDK_DRAG_STATUS:
3756 case GDK_DROP_START:
3757 case GDK_DROP_FINISHED:
3767 emit_event_on_tags (GtkWidget *widget,
3773 gboolean retval = FALSE;
3774 GtkTextView *text_view;
3776 text_view = GTK_TEXT_VIEW (widget);
3778 tags = gtk_text_iter_get_tags (iter);
3783 GtkTextTag *tag = tmp->data;
3785 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3791 tmp = g_slist_next (tmp);
3794 g_slist_free (tags);
3800 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3802 GtkTextView *text_view;
3805 text_view = GTK_TEXT_VIEW (widget);
3807 if (text_view->layout == NULL ||
3808 get_buffer (text_view) == NULL)
3811 if (event->any.window != text_view->text_window->bin_window)
3814 if (get_event_coordinates (event, &x, &y))
3818 x += text_view->xoffset;
3819 y += text_view->yoffset;
3821 /* FIXME this is slow and we do it twice per event.
3822 * My favorite solution is to have GtkTextLayout cache
3823 * the last couple lookups.
3825 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3829 return emit_event_on_tags (widget, event, &iter);
3831 else if (event->type == GDK_KEY_PRESS ||
3832 event->type == GDK_KEY_RELEASE)
3834 GtkTextMark *insert;
3837 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3840 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3842 return emit_event_on_tags (widget, event, &iter);
3849 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3851 gboolean retval = FALSE;
3852 gboolean obscure = FALSE;
3853 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3854 GtkTextMark *insert;
3856 gboolean can_insert;
3858 if (text_view->layout == NULL ||
3859 get_buffer (text_view) == NULL)
3862 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3863 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3864 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3866 gtk_im_context_filter_keypress (text_view->im_context, event))
3868 text_view->need_im_reset = TRUE;
3873 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3874 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3876 /* use overall editability not can_insert, more predictable for users */
3877 else if (text_view->editable &&
3878 (event->keyval == GDK_Return ||
3879 event->keyval == GDK_KP_Enter))
3881 /* this won't actually insert the newline if the cursor isn't
3884 gtk_text_view_commit_text (text_view, "\n");
3889 /* Pass through Tab as literal tab, unless Control is held down */
3890 else if ((event->keyval == GDK_Tab ||
3891 event->keyval == GDK_KP_Tab) &&
3892 !(event->state & GDK_CONTROL_MASK))
3894 /* If the text widget isn't editable overall, or if the application
3895 * has turned off "accepts_tab", move the focus instead
3897 if (text_view->accepts_tab && text_view->editable)
3899 gtk_text_view_commit_text (text_view, "\t");
3903 gtk_text_view_move_focus (text_view,
3904 (event->state & GDK_SHIFT_MASK) ?
3905 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3913 gtk_text_view_obscure_mouse_cursor (text_view);
3915 gtk_text_view_pend_cursor_blink (text_view);
3921 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3923 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3924 GtkTextMark *insert;
3927 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3930 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3931 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3932 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3933 gtk_im_context_filter_keypress (text_view->im_context, event))
3935 text_view->need_im_reset = TRUE;
3939 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3943 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3945 GtkTextView *text_view;
3947 text_view = GTK_TEXT_VIEW (widget);
3949 gtk_widget_grab_focus (widget);
3951 if (event->window != text_view->text_window->bin_window)
3953 /* Remove selection if any. */
3954 gtk_text_view_unselect (text_view);
3960 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3961 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3962 else if (event->button == 3)
3963 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3966 if (event->type == GDK_BUTTON_PRESS)
3968 gtk_text_view_reset_im_context (text_view);
3970 if (event->button == 1)
3972 /* If we're in the selection, start a drag copy/move of the
3973 * selection; otherwise, start creating a new selection.
3976 GtkTextIter start, end;
3978 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3980 event->x + text_view->xoffset,
3981 event->y + text_view->yoffset);
3983 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3985 gtk_text_iter_in_range (&iter, &start, &end))
3987 text_view->drag_start_x = event->x;
3988 text_view->drag_start_y = event->y;
3989 text_view->pending_place_cursor_button = event->button;
3993 gtk_text_view_start_selection_drag (text_view, &iter, event);
3998 else if (event->button == 2)
4002 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4004 event->x + text_view->xoffset,
4005 event->y + text_view->yoffset);
4007 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4008 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4010 text_view->editable);
4013 else if (event->button == 3)
4015 gtk_text_view_do_popup (text_view, event);
4019 else if ((event->type == GDK_2BUTTON_PRESS ||
4020 event->type == GDK_3BUTTON_PRESS) &&
4025 gtk_text_view_end_selection_drag (text_view, event);
4027 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4029 event->x + text_view->xoffset,
4030 event->y + text_view->yoffset);
4032 gtk_text_view_start_selection_drag (text_view, &iter, event);
4040 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4042 GtkTextView *text_view;
4044 text_view = GTK_TEXT_VIEW (widget);
4046 if (event->window != text_view->text_window->bin_window)
4049 if (event->button == 1)
4051 if (text_view->drag_start_x >= 0)
4053 text_view->drag_start_x = -1;
4054 text_view->drag_start_y = -1;
4057 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
4059 else if (text_view->pending_place_cursor_button == event->button)
4063 /* Unselect everything; we clicked inside selection, but
4064 * didn't move by the drag threshold, so just clear selection
4067 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4069 event->x + text_view->xoffset,
4070 event->y + text_view->yoffset);
4072 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4074 text_view->pending_place_cursor_button = 0;
4084 keymap_direction_changed (GdkKeymap *keymap,
4085 GtkTextView *text_view)
4087 gtk_text_view_check_keymap_direction (text_view);
4091 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4093 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4095 gtk_widget_queue_draw (widget);
4097 DV(g_print (G_STRLOC": focus_in_event\n"));
4099 if (text_view->cursor_visible && text_view->layout)
4101 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4102 gtk_text_view_check_cursor_blink (text_view);
4105 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4106 "direction_changed",
4107 G_CALLBACK (keymap_direction_changed), text_view);
4108 gtk_text_view_check_keymap_direction (text_view);
4110 text_view->need_im_reset = TRUE;
4111 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4117 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4119 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4121 gtk_widget_queue_draw (widget);
4123 DV(g_print (G_STRLOC": focus_out_event\n"));
4125 if (text_view->cursor_visible && text_view->layout)
4127 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4128 gtk_text_view_check_cursor_blink (text_view);
4131 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4132 keymap_direction_changed,
4135 text_view->need_im_reset = TRUE;
4136 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4142 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4144 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4146 gtk_text_view_unobscure_mouse_cursor (text_view);
4148 if (event->window == text_view->text_window->bin_window &&
4149 text_view->drag_start_x >= 0)
4153 gdk_window_get_pointer (text_view->text_window->bin_window,
4156 if (gtk_drag_check_threshold (widget,
4157 text_view->drag_start_x,
4158 text_view->drag_start_y,
4162 gint buffer_x, buffer_y;
4164 gtk_text_view_window_to_buffer_coords (text_view,
4165 GTK_TEXT_WINDOW_TEXT,
4166 text_view->drag_start_x,
4167 text_view->drag_start_y,
4171 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4173 buffer_x, buffer_y);
4175 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4184 gtk_text_view_paint (GtkWidget *widget,
4186 GdkEventExpose *event)
4188 GtkTextView *text_view;
4189 GList *child_exposes;
4193 text_view = GTK_TEXT_VIEW (widget);
4195 g_return_if_fail (text_view->layout != NULL);
4196 g_return_if_fail (text_view->xoffset >= 0);
4197 g_return_if_fail (text_view->yoffset >= 0);
4199 while (text_view->first_validate_idle != 0)
4201 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4202 text_view->first_validate_idle));
4203 gtk_text_view_flush_first_validate (text_view);
4206 /* More regions could have become invalid in the above loop */
4207 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4212 gdk_region_get_clipbox (updates, &rect);
4214 gdk_rectangle_union (area, &rect, area);
4216 gdk_region_destroy (updates);
4219 if (!text_view->onscreen_validated)
4221 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.");
4222 g_assert_not_reached ();
4226 printf ("painting %d,%d %d x %d\n",
4228 area->width, area->height);
4231 child_exposes = NULL;
4232 gtk_text_layout_draw (text_view->layout,
4234 text_view->text_window->bin_window,
4239 area->width, area->height,
4242 tmp_list = child_exposes;
4243 while (tmp_list != NULL)
4245 GtkWidget *child = tmp_list->data;
4247 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4251 g_object_unref (child);
4253 tmp_list = tmp_list->next;
4256 g_list_free (child_exposes);
4260 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4264 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4265 GTK_TEXT_WINDOW_TEXT))
4267 DV(g_print (">Exposed ("G_STRLOC")\n"));
4268 gtk_text_view_paint (widget, &event->area, event);
4271 if (event->window == widget->window)
4272 gtk_text_view_draw_focus (widget);
4274 /* Propagate exposes to all unanchored children.
4275 * Anchored children are handled in gtk_text_view_paint().
4277 tmp_list = GTK_TEXT_VIEW (widget)->children;
4278 while (tmp_list != NULL)
4280 GtkTextViewChild *vc = tmp_list->data;
4282 /* propagate_expose checks that event->window matches
4286 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4290 tmp_list = tmp_list->next;
4297 gtk_text_view_draw_focus (GtkWidget *widget)
4299 gboolean interior_focus;
4301 /* We clear the focus if we are in interior focus mode. */
4302 gtk_widget_style_get (widget,
4303 "interior-focus", &interior_focus,
4306 if (GTK_WIDGET_DRAWABLE (widget))
4308 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4310 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4311 NULL, widget, "textview",
4313 widget->allocation.width,
4314 widget->allocation.height);
4318 gdk_window_clear (widget->window);
4324 gtk_text_view_focus (GtkWidget *widget,
4325 GtkDirectionType direction)
4327 GtkTextView *text_view;
4328 GtkContainer *container;
4331 text_view = GTK_TEXT_VIEW (widget);
4332 container = GTK_CONTAINER (widget);
4334 if (!gtk_widget_is_focus (widget) &&
4335 container->focus_child == NULL)
4337 gtk_widget_grab_focus (widget);
4343 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4344 * children to get the focus
4346 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4347 result = GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4348 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4359 gtk_text_view_add (GtkContainer *container,
4362 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4363 g_return_if_fail (GTK_IS_WIDGET (child));
4365 /* This is pretty random. */
4366 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4368 GTK_TEXT_WINDOW_WIDGET,
4373 gtk_text_view_remove (GtkContainer *container,
4377 GtkTextView *text_view;
4378 GtkTextViewChild *vc;
4380 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4381 g_return_if_fail (GTK_IS_WIDGET (child));
4382 g_return_if_fail (child->parent == (GtkWidget*) container);
4384 text_view = GTK_TEXT_VIEW (container);
4387 iter = text_view->children;
4389 while (iter != NULL)
4393 if (vc->widget == child)
4396 iter = g_slist_next (iter);
4399 g_assert (iter != NULL); /* be sure we had the child in the list */
4401 text_view->children = g_slist_remove (text_view->children, vc);
4403 gtk_widget_unparent (vc->widget);
4405 text_view_child_free (vc);
4409 gtk_text_view_forall (GtkContainer *container,
4410 gboolean include_internals,
4411 GtkCallback callback,
4412 gpointer callback_data)
4415 GtkTextView *text_view;
4418 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4419 g_return_if_fail (callback != NULL);
4421 text_view = GTK_TEXT_VIEW (container);
4423 copy = g_slist_copy (text_view->children);
4426 while (iter != NULL)
4428 GtkTextViewChild *vc = iter->data;
4430 (* callback) (vc->widget, callback_data);
4432 iter = g_slist_next (iter);
4435 g_slist_free (copy);
4438 #define CURSOR_ON_MULTIPLIER 0.66
4439 #define CURSOR_OFF_MULTIPLIER 0.34
4440 #define CURSOR_PEND_MULTIPLIER 1.0
4443 cursor_blinks (GtkTextView *text_view)
4445 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4448 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4451 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4454 if (text_view->editable)
4456 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4464 get_cursor_time (GtkTextView *text_view)
4466 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4469 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
4479 blink_cb (gpointer data)
4481 GtkTextView *text_view;
4484 GDK_THREADS_ENTER ();
4486 text_view = GTK_TEXT_VIEW (data);
4488 if (!GTK_WIDGET_HAS_FOCUS (text_view))
4490 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
4491 "connect a handler to this signal, it must return\n"
4492 "FALSE so the entry gets the event as well");
4495 g_assert (text_view->layout);
4496 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4497 g_assert (text_view->cursor_visible);
4499 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4502 text_view->blink_timeout = g_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4506 text_view->blink_timeout = g_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4510 /* Block changed_handler while changing the layout's cursor visibility
4511 * because it would expose the whole paragraph. Instead, we expose
4512 * the cursor's area(s) manually below.
4514 g_signal_handlers_block_by_func (text_view->layout,
4518 gtk_text_layout_set_cursor_visible (text_view->layout, !visible);
4520 g_signal_handlers_unblock_by_func (text_view->layout,
4524 text_window_invalidate_cursors (text_view->text_window);
4526 GDK_THREADS_LEAVE ();
4528 /* Remove ourselves */
4534 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4536 if (text_view->blink_timeout)
4538 g_source_remove (text_view->blink_timeout);
4539 text_view->blink_timeout = 0;
4544 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4546 if (text_view->layout != NULL &&
4547 text_view->cursor_visible &&
4548 GTK_WIDGET_HAS_FOCUS (text_view))
4550 if (cursor_blinks (text_view))
4552 if (text_view->blink_timeout == 0)
4554 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4556 text_view->blink_timeout = g_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4562 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4566 gtk_text_view_stop_cursor_blink (text_view);
4571 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4573 if (text_view->layout != NULL &&
4574 text_view->cursor_visible &&
4575 GTK_WIDGET_HAS_FOCUS (text_view) &&
4576 cursor_blinks (text_view))
4578 if (text_view->blink_timeout != 0)
4580 g_source_remove (text_view->blink_timeout);
4581 text_view->blink_timeout = 0;
4584 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4586 text_view->blink_timeout = g_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4594 * Key binding handlers
4598 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4599 GtkTextIter *newplace,
4602 gboolean ret = TRUE;
4606 ret = gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4612 ret = gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4620 move_cursor (GtkTextView *text_view,
4621 const GtkTextIter *new_location,
4622 gboolean extend_selection)
4624 if (extend_selection)
4625 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4629 gtk_text_buffer_place_cursor (get_buffer (text_view),
4634 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4635 GtkMovementStep step,
4637 gboolean extend_selection)
4640 GtkTextIter newplace;
4642 gint cursor_x_pos = 0;
4644 if (!text_view->cursor_visible)
4646 GtkScrollStep scroll_step;
4650 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4651 case GTK_MOVEMENT_VISUAL_POSITIONS:
4652 case GTK_MOVEMENT_WORDS:
4653 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
4655 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4656 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
4658 case GTK_MOVEMENT_DISPLAY_LINES:
4659 case GTK_MOVEMENT_PARAGRAPHS:
4660 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4661 scroll_step = GTK_SCROLL_STEPS;
4663 case GTK_MOVEMENT_PAGES:
4664 scroll_step = GTK_SCROLL_PAGES;
4666 case GTK_MOVEMENT_HORIZONTAL_PAGES:
4667 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
4669 case GTK_MOVEMENT_BUFFER_ENDS:
4670 scroll_step = GTK_SCROLL_ENDS;
4673 scroll_step = GTK_SCROLL_PAGES;
4677 gtk_text_view_move_viewport (text_view, scroll_step, count);
4682 gtk_text_view_reset_im_context (text_view);
4684 if (step == GTK_MOVEMENT_PAGES)
4686 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4687 gtk_text_view_pend_cursor_blink (text_view);
4690 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
4692 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4693 gtk_text_view_pend_cursor_blink (text_view);
4697 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4698 gtk_text_buffer_get_mark (get_buffer (text_view),
4702 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4703 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4707 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4708 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
4711 case GTK_MOVEMENT_VISUAL_POSITIONS:
4712 gtk_text_layout_move_iter_visually (text_view->layout,
4716 case GTK_MOVEMENT_WORDS:
4718 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
4721 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
4722 gtk_text_iter_forward_to_line_end (&newplace);
4726 case GTK_MOVEMENT_DISPLAY_LINES:
4729 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
4730 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4732 gtk_text_iter_set_line_offset (&newplace, 0);
4736 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
4737 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4739 gtk_text_iter_forward_to_line_end (&newplace);
4743 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4745 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4746 else if (count < -1)
4747 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4750 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4753 case GTK_MOVEMENT_PARAGRAPHS:
4756 if (!gtk_text_iter_ends_line (&newplace))
4758 gtk_text_iter_forward_to_line_end (&newplace);
4761 gtk_text_iter_forward_visible_lines (&newplace, count);
4762 gtk_text_iter_forward_to_line_end (&newplace);
4766 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4767 gtk_text_iter_set_line_offset (&newplace, 0);
4768 gtk_text_iter_forward_visible_lines (&newplace, count);
4769 gtk_text_iter_set_line_offset (&newplace, 0);
4773 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4776 if (!gtk_text_iter_ends_line (&newplace))
4777 gtk_text_iter_forward_to_line_end (&newplace);
4781 gtk_text_iter_set_line_offset (&newplace, 0);
4785 case GTK_MOVEMENT_BUFFER_ENDS:
4787 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4789 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4796 /* call move_cursor() even if the cursor hasn't moved, since it
4797 cancels the selection
4799 move_cursor (text_view, &newplace, extend_selection);
4801 if (!gtk_text_iter_equal (&insert, &newplace))
4803 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4804 gtk_text_view_scroll_mark_onscreen (text_view,
4805 gtk_text_buffer_get_mark (get_buffer (text_view),
4808 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4809 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4812 gtk_text_view_pend_cursor_blink (text_view);
4816 gtk_text_view_move_cursor (GtkTextView *text_view,
4817 GtkMovementStep step,
4819 gboolean extend_selection)
4821 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4825 gtk_text_view_page_horizontally (GtkTextView *text_view,
4827 gboolean extend_selection)
4829 gtk_text_view_move_cursor_internal (text_view, GTK_MOVEMENT_HORIZONTAL_PAGES,
4830 count, extend_selection);
4835 gtk_text_view_move_viewport (GtkTextView *text_view,
4839 GtkAdjustment *adjustment;
4844 case GTK_SCROLL_STEPS:
4845 case GTK_SCROLL_PAGES:
4846 case GTK_SCROLL_ENDS:
4847 adjustment = get_vadjustment (text_view);
4849 case GTK_SCROLL_HORIZONTAL_STEPS:
4850 case GTK_SCROLL_HORIZONTAL_PAGES:
4851 case GTK_SCROLL_HORIZONTAL_ENDS:
4852 adjustment = get_hadjustment (text_view);
4855 adjustment = get_vadjustment (text_view);
4861 case GTK_SCROLL_STEPS:
4862 case GTK_SCROLL_HORIZONTAL_STEPS:
4863 increment = adjustment->step_increment;
4865 case GTK_SCROLL_PAGES:
4866 case GTK_SCROLL_HORIZONTAL_PAGES:
4867 increment = adjustment->page_increment;
4869 case GTK_SCROLL_ENDS:
4870 case GTK_SCROLL_HORIZONTAL_ENDS:
4871 increment = adjustment->upper - adjustment->lower;
4878 set_adjustment_clamped (adjustment, adjustment->value + count * increment);
4882 gtk_text_view_set_anchor (GtkTextView *text_view)
4886 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4887 gtk_text_buffer_get_mark (get_buffer (text_view),
4890 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4894 gtk_text_view_scroll_pages (GtkTextView *text_view,
4896 gboolean extend_selection)
4901 gint cursor_x_pos, cursor_y_pos;
4902 GtkTextIter new_insert;
4906 g_return_if_fail (text_view->vadjustment != NULL);
4908 adj = text_view->vadjustment;
4910 /* Make sure we start from the current cursor position, even
4911 * if it was offscreen, but don't queue more scrolls if we're
4914 if (text_view->pending_scroll)
4915 cancel_pending_scroll (text_view);
4917 gtk_text_view_scroll_mark_onscreen (text_view,
4918 gtk_text_buffer_get_mark (get_buffer (text_view),
4921 /* Validate the region that will be brought into view by the cursor motion
4925 gtk_text_view_get_first_para_iter (text_view, &anchor);
4926 y0 = adj->page_size;
4927 y1 = adj->page_size + count * adj->page_increment;
4931 gtk_text_view_get_first_para_iter (text_view, &anchor);
4932 y0 = count * adj->page_increment + adj->page_size;
4936 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4937 /* FIXME do we need to update the adjustment ranges here? */
4939 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4941 /* already at top, just be sure we are at offset 0 */
4942 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4943 move_cursor (text_view, &new_insert, extend_selection);
4945 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4947 /* already at bottom, just be sure we are at the end */
4948 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4949 move_cursor (text_view, &new_insert, extend_selection);
4953 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4955 newval = adj->value;
4956 oldval = adj->value;
4958 newval += count * adj->page_increment;
4960 set_adjustment_clamped (adj, newval);
4961 cursor_y_pos += adj->value - oldval;
4963 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4964 clamp_iter_onscreen (text_view, &new_insert);
4965 move_cursor (text_view, &new_insert, extend_selection);
4967 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4970 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4971 * only guarantees 1 pixel onscreen.
4973 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4974 gtk_text_view_scroll_mark_onscreen (text_view,
4975 gtk_text_buffer_get_mark (get_buffer (text_view),
4980 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4982 gboolean extend_selection)
4987 gint cursor_x_pos, cursor_y_pos;
4988 GtkTextIter new_insert;
4991 g_return_if_fail (text_view->hadjustment != NULL);
4993 adj = text_view->hadjustment;
4995 /* Make sure we start from the current cursor position, even
4996 * if it was offscreen, but don't queue more scrolls if we're
4999 if (text_view->pending_scroll)
5000 cancel_pending_scroll (text_view);
5002 gtk_text_view_scroll_mark_onscreen (text_view,
5003 gtk_text_buffer_get_mark (get_buffer (text_view),
5006 /* Validate the line that we're moving within.
5008 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5010 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5011 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
5012 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
5013 /* FIXME do we need to update the adjustment ranges here? */
5015 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5017 /* already at far left, just be sure we are at offset 0 */
5018 gtk_text_iter_set_line_offset (&new_insert, 0);
5019 move_cursor (text_view, &new_insert, extend_selection);
5021 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5023 /* already at far right, just be sure we are at the end */
5024 if (!gtk_text_iter_ends_line (&new_insert))
5025 gtk_text_iter_forward_to_line_end (&new_insert);
5026 move_cursor (text_view, &new_insert, extend_selection);
5030 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5032 newval = adj->value;
5033 oldval = adj->value;
5035 newval += count * adj->page_increment;
5037 set_adjustment_clamped (adj, newval);
5038 cursor_x_pos += adj->value - oldval;
5040 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5041 clamp_iter_onscreen (text_view, &new_insert);
5042 move_cursor (text_view, &new_insert, extend_selection);
5044 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5047 /* FIXME for lines shorter than the overall widget width, this results in a
5048 * "bounce" effect as we scroll to the right of the widget, then scroll
5049 * back to get the end of the line onscreen.
5050 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5053 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5054 * only guarantees 1 pixel onscreen.
5056 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5057 gtk_text_view_scroll_mark_onscreen (text_view,
5058 gtk_text_buffer_get_mark (get_buffer (text_view),
5063 whitespace (gunichar ch, gpointer user_data)
5065 return (ch == ' ' || ch == '\t');
5069 not_whitespace (gunichar ch, gpointer user_data)
5071 return !whitespace (ch, user_data);
5075 find_whitepace_region (const GtkTextIter *center,
5076 GtkTextIter *start, GtkTextIter *end)
5081 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5082 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5083 if (whitespace (gtk_text_iter_get_char (end), NULL))
5084 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5086 return !gtk_text_iter_equal (start, end);
5090 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5093 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5094 text_view->editable);
5098 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5105 gboolean leave_one = FALSE;
5107 gtk_text_view_reset_im_context (text_view);
5109 if (type == GTK_DELETE_CHARS)
5111 /* Char delete deletes the selection, if one exists */
5112 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5113 text_view->editable))
5117 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5119 gtk_text_buffer_get_mark (get_buffer (text_view),
5127 case GTK_DELETE_CHARS:
5128 gtk_text_iter_forward_cursor_positions (&end, count);
5131 case GTK_DELETE_WORD_ENDS:
5133 gtk_text_iter_forward_word_ends (&end, count);
5135 gtk_text_iter_backward_word_starts (&start, 0 - count);
5138 case GTK_DELETE_WORDS:
5141 case GTK_DELETE_DISPLAY_LINE_ENDS:
5144 case GTK_DELETE_DISPLAY_LINES:
5147 case GTK_DELETE_PARAGRAPH_ENDS:
5148 /* If we're already at a newline, we need to
5149 * simply delete that newline, instead of
5150 * moving to the next one.
5152 if (gtk_text_iter_ends_line (&end))
5154 gtk_text_iter_forward_line (&end);
5160 if (!gtk_text_iter_forward_to_line_end (&end))
5166 /* FIXME figure out what a negative count means
5170 case GTK_DELETE_PARAGRAPHS:
5173 gtk_text_iter_set_line_offset (&start, 0);
5174 gtk_text_iter_forward_to_line_end (&end);
5176 /* Do the lines beyond the first. */
5179 gtk_text_iter_forward_to_line_end (&end);
5185 /* FIXME negative count? */
5189 case GTK_DELETE_WHITESPACE:
5191 find_whitepace_region (&insert, &start, &end);
5199 if (!gtk_text_iter_equal (&start, &end))
5201 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5203 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5204 text_view->editable))
5207 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5209 text_view->editable);
5212 gtk_text_buffer_end_user_action (get_buffer (text_view));
5214 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5215 gtk_text_view_scroll_mark_onscreen (text_view,
5216 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5221 gtk_text_view_backspace (GtkTextView *text_view)
5225 gtk_text_view_reset_im_context (text_view);
5227 /* Backspace deletes the selection, if one exists */
5228 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5229 text_view->editable))
5232 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5234 gtk_text_buffer_get_mark (get_buffer (text_view),
5237 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5238 TRUE, text_view->editable))
5240 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5241 gtk_text_view_scroll_mark_onscreen (text_view,
5242 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5247 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5249 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5250 GDK_SELECTION_CLIPBOARD);
5252 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5254 text_view->editable);
5255 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5256 gtk_text_view_scroll_mark_onscreen (text_view,
5257 gtk_text_buffer_get_mark (get_buffer (text_view),
5262 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5264 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5265 GDK_SELECTION_CLIPBOARD);
5267 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5269 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5270 gtk_text_view_scroll_mark_onscreen (text_view,
5271 gtk_text_buffer_get_mark (get_buffer (text_view),
5276 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5278 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5279 GDK_SELECTION_CLIPBOARD);
5281 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5284 text_view->editable);
5285 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5286 gtk_text_view_scroll_mark_onscreen (text_view,
5287 gtk_text_buffer_get_mark (get_buffer (text_view),
5292 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5294 text_view->overwrite_mode = !text_view->overwrite_mode;
5295 g_object_notify (G_OBJECT (text_view), "overwrite");
5299 * gtk_text_view_get_overwrite:
5300 * @text_view: a #GtkTextView
5302 * Returns whether the #GtkTextView is in overwrite mode or not.
5304 * Return value: whether @text_view is in overwrite mode or not.
5309 gtk_text_view_get_overwrite (GtkTextView *text_view)
5311 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5313 return text_view->overwrite_mode;
5317 * gtk_text_view_set_overwrite:
5318 * @text_view: a #GtkTextView
5319 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
5321 * Changes the #GtkTextView overwrite mode.
5326 gtk_text_view_set_overwrite (GtkTextView *text_view,
5329 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5330 overwrite = overwrite != FALSE;
5332 if (text_view->overwrite_mode != overwrite)
5334 text_view->overwrite_mode = overwrite;
5336 g_object_notify (G_OBJECT (text_view), "overwrite");
5341 * gtk_text_view_set_accepts_tab:
5342 * @text_view: A #GtkTextView
5343 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab character, %FALSE, if pressing the Tab key should move the keyboard focus.
5345 * Sets the behavior of the text widget when the Tab key is pressed. If @accepts_tab
5346 * is %TRUE a tab character is inserted. If @accepts_tab is %FALSE the keyboard focus
5347 * is moved to the next widget in the focus chain.
5352 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
5353 gboolean accepts_tab)
5355 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5357 accepts_tab = accepts_tab != FALSE;
5359 if (text_view->accepts_tab != accepts_tab)
5361 text_view->accepts_tab = accepts_tab;
5363 g_object_notify (G_OBJECT (text_view), "accepts-tab");
5368 * gtk_text_view_get_accepts_tab:
5369 * @text_view: A #GtkTextView
5371 * Returns whether pressing the Tab key inserts a tab characters.
5372 * gtk_text_view_set_accepts_tab().
5374 * Return value: %TRUE if pressing the Tab key inserts a tab character, %FALSE if pressing the Tab key moves the keyboard focus.
5379 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
5381 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5383 return text_view->accepts_tab;
5387 gtk_text_view_move_focus (GtkTextView *text_view,
5388 GtkDirectionType direction_type)
5390 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5392 if (!GTK_WIDGET_TOPLEVEL (toplevel))
5395 /* Propagate to toplevel */
5396 g_signal_emit_by_name (toplevel, "move_focus", direction_type);
5404 gtk_text_view_unselect (GtkTextView *text_view)
5408 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5410 gtk_text_buffer_get_mark (get_buffer (text_view),
5413 gtk_text_buffer_move_mark (get_buffer (text_view),
5414 gtk_text_buffer_get_mark (get_buffer (text_view),
5420 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5421 const gchar *mark_name)
5424 GdkModifierType state;
5425 GtkTextIter newplace;
5427 /* DV(g_print (G_STRLOC": begin\n")); */
5429 gdk_window_get_pointer (text_view->text_window->bin_window,
5432 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5433 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5435 x + text_view->xoffset,
5436 y + text_view->yoffset);
5440 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5442 /* This may invalidate the layout */
5443 DV(g_print (G_STRLOC": move mark\n"));
5444 gtk_text_buffer_move_mark (get_buffer (text_view),
5448 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5449 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5452 DV (g_print ("first validate idle leaving %s is %d\n",
5453 G_STRLOC, text_view->first_validate_idle));
5457 selection_scan_timeout (gpointer data)
5459 GtkTextView *text_view;
5461 GDK_THREADS_ENTER ();
5463 text_view = GTK_TEXT_VIEW (data);
5465 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5466 gtk_text_view_scroll_mark_onscreen (text_view,
5467 gtk_text_buffer_get_mark (get_buffer (text_view),
5470 GDK_THREADS_LEAVE ();
5472 return TRUE; /* remain installed. */
5475 #define DND_SCROLL_MARGIN 0.20
5478 drag_scan_timeout (gpointer data)
5480 GtkTextView *text_view;
5482 GdkModifierType state;
5483 GtkTextIter newplace;
5485 GDK_THREADS_ENTER ();
5487 text_view = GTK_TEXT_VIEW (data);
5489 gdk_window_get_pointer (text_view->text_window->bin_window,
5492 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5494 x + text_view->xoffset,
5495 y + text_view->yoffset);
5497 gtk_text_buffer_move_mark (get_buffer (text_view),
5498 text_view->dnd_mark,
5501 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5502 gtk_text_view_scroll_to_mark (text_view,
5503 text_view->dnd_mark,
5504 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5506 GDK_THREADS_LEAVE ();
5516 } SelectionGranularity;
5519 * Move @start and @end to the boundaries of the selection unit (indicated by
5520 * @granularity) which contained @start initially.
5521 * If the selction unit is SELECT_WORDS and @start is not contained in a word
5522 * the selection is extended to all the white spaces between the end of the
5523 * word preceding @start and the start of the one following.
5526 extend_selection (GtkTextView *text_view,
5527 SelectionGranularity granularity,
5533 if (granularity == SELECT_WORDS)
5535 if (gtk_text_iter_inside_word (start))
5537 if (!gtk_text_iter_starts_word (start))
5538 gtk_text_iter_backward_visible_word_start (start);
5540 if (!gtk_text_iter_ends_word (end))
5542 if (!gtk_text_iter_forward_visible_word_end (end))
5543 gtk_text_iter_forward_to_end (end);
5551 if (gtk_text_iter_backward_visible_word_start (&tmp))
5552 gtk_text_iter_forward_visible_word_end (&tmp);
5554 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
5557 gtk_text_iter_set_line_offset (start, 0);
5560 if (!gtk_text_iter_forward_visible_word_end (&tmp))
5561 gtk_text_iter_forward_to_end (&tmp);
5563 if (gtk_text_iter_ends_word (&tmp))
5564 gtk_text_iter_backward_visible_word_start (&tmp);
5566 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
5569 gtk_text_iter_forward_to_line_end (end);
5572 else if (granularity == SELECT_LINES)
5574 if (gtk_text_view_starts_display_line (text_view, start))
5576 /* If on a display line boundary, we assume the user
5577 * clicked off the end of a line and we therefore select
5578 * the line before the boundary.
5580 gtk_text_view_backward_display_line_start (text_view, start);
5584 /* start isn't on the start of a line, so we move it to the
5585 * start, and move end to the end unless it's already there.
5587 gtk_text_view_backward_display_line_start (text_view, start);
5589 if (!gtk_text_view_starts_display_line (text_view, end))
5590 gtk_text_view_forward_display_line_end (text_view, end);
5596 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5598 SelectionGranularity granularity = GPOINTER_TO_INT (data);
5600 if (granularity == SELECT_CHARACTERS)
5602 move_mark_to_pointer_and_scroll (text_view, "insert");
5607 GdkModifierType state;
5608 GtkTextIter start, end;
5609 GtkTextIter old_start, old_end;
5610 GtkTextIter ins, bound;
5611 GtkTextBuffer *buffer;
5613 buffer = get_buffer (text_view);
5615 gdk_window_get_pointer (text_view->text_window->bin_window,
5618 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5620 event->x + text_view->xoffset,
5621 event->y + text_view->yoffset);
5623 extend_selection (text_view, granularity, &start, &end);
5625 /* Extend selection */
5626 gtk_text_buffer_get_iter_at_mark (buffer,
5628 gtk_text_buffer_get_insert (buffer));
5629 gtk_text_buffer_get_iter_at_mark (buffer,
5631 gtk_text_buffer_get_selection_bound (buffer));
5633 if (gtk_text_iter_compare (&ins, &bound) < 0)
5644 if (gtk_text_iter_compare (&start, &old_start) < 0)
5646 /* newly selected unit before the current selection */
5650 else if (gtk_text_iter_compare (&old_end, &end) < 0)
5652 /* newly selected unit after the current selection */
5656 else if (gtk_text_iter_equal (&ins, &old_start))
5658 /* newly selected unit inside the current selection at the start */
5659 if (!gtk_text_iter_equal (&ins, &start))
5664 /* newly selected unit inside the current selection at the end */
5665 if (!gtk_text_iter_equal (&ins, &end))
5669 gtk_text_buffer_select_range (buffer, &ins, &bound);
5671 gtk_text_view_scroll_mark_onscreen (text_view,
5672 gtk_text_buffer_get_mark (buffer,
5676 /* If we had to scroll offscreen, insert a timeout to do so
5677 * again. Note that in the timeout, even if the mouse doesn't
5678 * move, due to this scroll xoffset/yoffset will have changed
5679 * and we'll need to scroll again.
5681 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5682 g_source_remove (text_view->scroll_timeout);
5684 text_view->scroll_timeout =
5685 g_timeout_add (50, selection_scan_timeout, text_view);
5691 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5692 const GtkTextIter *iter,
5693 GdkEventButton *button)
5695 GtkTextIter start, end;
5696 GtkTextBuffer *buffer;
5697 SelectionGranularity granularity;
5699 g_return_if_fail (text_view->selection_drag_handler == 0);
5701 if (button->type == GDK_2BUTTON_PRESS)
5702 granularity = SELECT_WORDS;
5703 else if (button->type == GDK_3BUTTON_PRESS)
5704 granularity = SELECT_LINES;
5706 granularity = SELECT_CHARACTERS;
5708 gtk_grab_add (GTK_WIDGET (text_view));
5710 buffer = get_buffer (text_view);
5714 extend_selection (text_view, granularity, &start, &end);
5716 if (button->state & GDK_SHIFT_MASK)
5718 /* Extend selection */
5719 GtkTextIter old_start, old_end;
5721 gtk_text_buffer_get_selection_bounds (buffer, &old_start, &old_end);
5723 gtk_text_iter_order (&start, &old_start);
5724 gtk_text_iter_order (&old_end, &end);
5726 /* Now start is the first of the starts, and end is the
5731 gtk_text_buffer_select_range (buffer, &end, &start);
5733 text_view->selection_drag_handler = g_signal_connect (text_view,
5734 "motion_notify_event",
5735 G_CALLBACK (selection_motion_event_handler),
5736 GINT_TO_POINTER (granularity));
5739 /* returns whether we were really dragging */
5741 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5743 if (text_view->selection_drag_handler == 0)
5746 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
5747 text_view->selection_drag_handler = 0;
5749 if (text_view->scroll_timeout != 0)
5751 g_source_remove (text_view->scroll_timeout);
5752 text_view->scroll_timeout = 0;
5755 gtk_grab_remove (GTK_WIDGET (text_view));
5765 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5766 GtkTextAttributes *values,
5769 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5770 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5773 pango_font_description_free (values->font);
5775 values->font = pango_font_description_copy (style->font_desc);
5779 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5781 if (text_view->layout)
5783 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5784 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5785 GtkTextDirection new_cursor_dir;
5786 GtkTextDirection new_keyboard_dir;
5787 gboolean split_cursor;
5789 g_object_get (settings,
5790 "gtk-split-cursor", &split_cursor,
5793 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5794 new_keyboard_dir = GTK_TEXT_DIR_RTL;
5796 new_keyboard_dir = GTK_TEXT_DIR_LTR;
5799 new_cursor_dir = GTK_TEXT_DIR_NONE;
5801 new_cursor_dir = new_keyboard_dir;
5803 gtk_text_layout_set_cursor_direction (text_view->layout, new_cursor_dir);
5804 gtk_text_layout_set_keyboard_direction (text_view->layout, new_keyboard_dir);
5809 gtk_text_view_ensure_layout (GtkTextView *text_view)
5813 widget = GTK_WIDGET (text_view);
5815 if (text_view->layout == NULL)
5817 GtkTextAttributes *style;
5818 PangoContext *ltr_context, *rtl_context;
5821 DV(g_print(G_STRLOC"\n"));
5823 text_view->layout = gtk_text_layout_new ();
5825 g_signal_connect (text_view->layout,
5827 G_CALLBACK (invalidated_handler),
5830 g_signal_connect (text_view->layout,
5832 G_CALLBACK (changed_handler),
5835 g_signal_connect (text_view->layout,
5837 G_CALLBACK (gtk_text_view_child_allocated),
5840 if (get_buffer (text_view))
5841 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5843 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5844 gtk_text_view_pend_cursor_blink (text_view);
5846 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5848 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5849 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5850 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5851 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5853 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5855 g_object_unref (ltr_context);
5856 g_object_unref (rtl_context);
5858 gtk_text_view_check_keymap_direction (text_view);
5860 style = gtk_text_attributes_new ();
5862 gtk_widget_ensure_style (widget);
5863 gtk_text_view_set_attributes_from_style (text_view,
5864 style, widget->style);
5866 style->pixels_above_lines = text_view->pixels_above_lines;
5867 style->pixels_below_lines = text_view->pixels_below_lines;
5868 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5869 style->left_margin = text_view->left_margin;
5870 style->right_margin = text_view->right_margin;
5871 style->indent = text_view->indent;
5872 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5874 style->wrap_mode = text_view->wrap_mode;
5875 style->justification = text_view->justify;
5876 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5878 gtk_text_layout_set_default_style (text_view->layout, style);
5880 gtk_text_attributes_unref (style);
5882 /* Set layout for all anchored children */
5884 tmp_list = text_view->children;
5885 while (tmp_list != NULL)
5887 GtkTextViewChild *vc = tmp_list->data;
5891 gtk_text_anchored_child_set_layout (vc->widget,
5893 /* vc may now be invalid! */
5896 tmp_list = g_slist_next (tmp_list);
5899 gtk_text_view_invalidate (text_view);
5904 * gtk_text_view_get_default_attributes:
5905 * @text_view: a #GtkTextView
5907 * Obtains a copy of the default text attributes. These are the
5908 * attributes used for text unless a tag overrides them.
5909 * You'd typically pass the default attributes in to
5910 * gtk_text_iter_get_attributes() in order to get the
5911 * attributes in effect at a given text position.
5913 * The return value is a copy owned by the caller of this function,
5914 * and should be freed.
5916 * Return value: a new #GtkTextAttributes
5919 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5921 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5923 gtk_text_view_ensure_layout (text_view);
5925 return gtk_text_attributes_copy (text_view->layout->default_style);
5929 gtk_text_view_destroy_layout (GtkTextView *text_view)
5931 if (text_view->layout)
5935 gtk_text_view_remove_validate_idles (text_view);
5937 g_signal_handlers_disconnect_by_func (text_view->layout,
5938 invalidated_handler,
5940 g_signal_handlers_disconnect_by_func (text_view->layout,
5944 /* Remove layout from all anchored children */
5945 tmp_list = text_view->children;
5946 while (tmp_list != NULL)
5948 GtkTextViewChild *vc = tmp_list->data;
5952 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5953 /* vc may now be invalid! */
5956 tmp_list = g_slist_next (tmp_list);
5959 gtk_text_view_stop_cursor_blink (text_view);
5960 gtk_text_view_end_selection_drag (text_view, NULL);
5962 g_object_unref (text_view->layout);
5963 text_view->layout = NULL;
5968 gtk_text_view_reset_im_context (GtkTextView *text_view)
5970 if (text_view->need_im_reset)
5972 text_view->need_im_reset = FALSE;
5973 gtk_im_context_reset (text_view->im_context);
5978 _gtk_text_view_get_selected_text (GtkTextView *text_view)
5980 GtkTextBuffer *buffer;
5981 GtkTextIter start, end;
5984 buffer = gtk_text_view_get_buffer (text_view);
5986 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
5987 text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
5997 drag_begin_cb (GtkWidget *widget,
5998 GdkDragContext *context,
6001 GtkTextView *text_view;
6003 GdkPixmap *pixmap = NULL;
6005 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6007 text_view = GTK_TEXT_VIEW (widget);
6009 text = _gtk_text_view_get_selected_text (text_view);
6010 pixmap = _gtk_text_util_create_drag_icon (widget, text, -1);
6013 gtk_drag_set_icon_pixmap (context,
6014 gdk_drawable_get_colormap (pixmap),
6019 gtk_drag_set_icon_default (context);
6022 g_object_unref (pixmap);
6027 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6028 const GtkTextIter *iter,
6029 GdkEventMotion *event)
6031 GdkDragContext *context;
6032 GtkTargetList *target_list;
6034 text_view->drag_start_x = -1;
6035 text_view->drag_start_y = -1;
6036 text_view->pending_place_cursor_button = 0;
6038 target_list = gtk_target_list_new (target_table,
6039 G_N_ELEMENTS (target_table));
6040 gtk_target_list_add_text_targets (target_list, 0);
6042 g_signal_connect (text_view, "drag-begin",
6043 G_CALLBACK (drag_begin_cb), NULL);
6044 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6045 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6046 1, (GdkEvent*)event);
6048 gtk_target_list_unref (target_list);
6052 gtk_text_view_drag_begin (GtkWidget *widget,
6053 GdkDragContext *context)
6059 gtk_text_view_drag_end (GtkWidget *widget,
6060 GdkDragContext *context)
6062 GtkTextView *text_view;
6064 text_view = GTK_TEXT_VIEW (widget);
6068 gtk_text_view_drag_data_get (GtkWidget *widget,
6069 GdkDragContext *context,
6070 GtkSelectionData *selection_data,
6074 GtkTextView *text_view;
6076 text_view = GTK_TEXT_VIEW (widget);
6078 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
6080 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6082 gtk_selection_data_set (selection_data,
6083 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
6096 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6099 /* Extract the selected text */
6100 str = gtk_text_iter_get_visible_text (&start, &end);
6105 gtk_selection_data_set_text (selection_data, str, -1);
6112 gtk_text_view_drag_data_delete (GtkWidget *widget,
6113 GdkDragContext *context)
6115 GtkTextView *text_view;
6117 text_view = GTK_TEXT_VIEW (widget);
6119 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
6120 TRUE, GTK_TEXT_VIEW (widget)->editable);
6124 gtk_text_view_drag_leave (GtkWidget *widget,
6125 GdkDragContext *context,
6128 GtkTextView *text_view;
6130 text_view = GTK_TEXT_VIEW (widget);
6132 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6134 if (text_view->scroll_timeout != 0)
6135 g_source_remove (text_view->scroll_timeout);
6137 text_view->scroll_timeout = 0;
6141 gtk_text_view_drag_motion (GtkWidget *widget,
6142 GdkDragContext *context,
6147 GtkTextIter newplace;
6148 GtkTextView *text_view;
6151 GdkRectangle target_rect;
6153 GdkDragAction suggested_action = 0;
6155 text_view = GTK_TEXT_VIEW (widget);
6157 target_rect = text_view->text_window->allocation;
6159 if (x < target_rect.x ||
6160 y < target_rect.y ||
6161 x > (target_rect.x + target_rect.width) ||
6162 y > (target_rect.y + target_rect.height))
6163 return FALSE; /* outside the text window, allow parent widgets to handle event */
6165 gtk_text_view_window_to_buffer_coords (text_view,
6166 GTK_TEXT_WINDOW_WIDGET,
6170 gtk_text_layout_get_iter_at_pixel (text_view->layout,
6174 if (gtk_drag_dest_find_target (widget, context,
6175 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
6177 /* can't accept any of the offered targets */
6179 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6181 gtk_text_iter_compare (&newplace, &start) >= 0 &&
6182 gtk_text_iter_compare (&newplace, &end) <= 0)
6184 /* We're inside the selection. */
6188 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
6190 GtkWidget *source_widget;
6192 suggested_action = context->suggested_action;
6194 source_widget = gtk_drag_get_source_widget (context);
6196 if (source_widget == widget)
6198 /* Default to MOVE, unless the user has
6199 * pressed ctrl or alt to affect available actions
6201 if ((context->actions & GDK_ACTION_MOVE) != 0)
6202 suggested_action = GDK_ACTION_MOVE;
6207 /* Can't drop here. */
6211 if (suggested_action != 0)
6213 gtk_text_mark_set_visible (text_view->dnd_mark,
6214 text_view->cursor_visible);
6216 gdk_drag_status (context, suggested_action, time);
6220 gdk_drag_status (context, 0, time);
6221 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6224 gtk_text_buffer_move_mark (get_buffer (text_view),
6225 text_view->dnd_mark,
6228 DV(g_print (G_STRLOC": scrolling to mark\n"));
6229 gtk_text_view_scroll_to_mark (text_view,
6230 text_view->dnd_mark,
6231 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
6233 if (text_view->scroll_timeout != 0) /* reset on every motion event */
6234 g_source_remove (text_view->scroll_timeout);
6236 text_view->scroll_timeout =
6237 g_timeout_add (50, drag_scan_timeout, text_view);
6239 /* TRUE return means don't propagate the drag motion to parent
6240 * widgets that may also be drop sites.
6246 gtk_text_view_drag_drop (GtkWidget *widget,
6247 GdkDragContext *context,
6252 GtkTextView *text_view;
6253 GtkTextIter drop_point;
6254 GdkAtom target = GDK_NONE;
6256 text_view = GTK_TEXT_VIEW (widget);
6258 if (text_view->scroll_timeout != 0)
6259 g_source_remove (text_view->scroll_timeout);
6261 text_view->scroll_timeout = 0;
6263 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6265 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6267 text_view->dnd_mark);
6269 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
6270 target = gtk_drag_dest_find_target (widget, context, NULL);
6272 if (target != GDK_NONE)
6273 gtk_drag_get_data (widget, context, target, time);
6275 gtk_drag_finish (context, FALSE, FALSE, time);
6281 insert_text_data (GtkTextView *text_view,
6282 GtkTextIter *drop_point,
6283 GtkSelectionData *selection_data)
6287 str = gtk_selection_data_get_text (selection_data);
6291 gtk_text_buffer_insert_interactive (get_buffer (text_view),
6292 drop_point, str, -1,
6293 text_view->editable);
6299 gtk_text_view_drag_data_received (GtkWidget *widget,
6300 GdkDragContext *context,
6303 GtkSelectionData *selection_data,
6307 GtkTextIter drop_point;
6308 GtkTextView *text_view;
6309 gboolean success = FALSE;
6310 GtkTextBuffer *buffer = NULL;
6312 text_view = GTK_TEXT_VIEW (widget);
6314 if (!text_view->dnd_mark)
6317 buffer = get_buffer (text_view);
6319 gtk_text_buffer_get_iter_at_mark (buffer,
6321 text_view->dnd_mark);
6323 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
6328 gtk_text_buffer_begin_user_action (buffer);
6330 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
6332 GtkTextBuffer *src_buffer = NULL;
6333 GtkTextIter start, end;
6334 gboolean copy_tags = TRUE;
6336 if (selection_data->length != sizeof (src_buffer))
6339 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
6341 if (src_buffer == NULL)
6344 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
6346 if (gtk_text_buffer_get_tag_table (src_buffer) !=
6347 gtk_text_buffer_get_tag_table (buffer))
6350 if (gtk_text_buffer_get_selection_bounds (src_buffer,
6355 gtk_text_buffer_insert_range_interactive (buffer,
6359 text_view->editable);
6364 str = gtk_text_iter_get_visible_text (&start, &end);
6365 gtk_text_buffer_insert_interactive (buffer,
6366 &drop_point, str, -1,
6367 text_view->editable);
6373 insert_text_data (text_view, &drop_point, selection_data);
6376 gtk_drag_finish (context, success,
6377 success && context->action == GDK_ACTION_MOVE,
6382 gtk_text_buffer_get_iter_at_mark (buffer,
6384 text_view->dnd_mark);
6385 gtk_text_buffer_place_cursor (buffer, &drop_point);
6387 gtk_text_buffer_end_user_action (buffer);
6391 static GtkAdjustment*
6392 get_hadjustment (GtkTextView *text_view)
6394 if (text_view->hadjustment == NULL)
6395 gtk_text_view_set_scroll_adjustments (text_view,
6396 NULL, /* forces creation */
6397 text_view->vadjustment);
6399 return text_view->hadjustment;
6402 static GtkAdjustment*
6403 get_vadjustment (GtkTextView *text_view)
6405 if (text_view->vadjustment == NULL)
6406 gtk_text_view_set_scroll_adjustments (text_view,
6407 text_view->hadjustment,
6408 NULL); /* forces creation */
6409 return text_view->vadjustment;
6414 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
6415 GtkAdjustment *hadj,
6416 GtkAdjustment *vadj)
6418 gboolean need_adjust = FALSE;
6420 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6423 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
6425 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6427 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
6429 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6431 if (text_view->hadjustment && (text_view->hadjustment != hadj))
6433 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
6434 gtk_text_view_value_changed,
6436 g_object_unref (text_view->hadjustment);
6439 if (text_view->vadjustment && (text_view->vadjustment != vadj))
6441 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
6442 gtk_text_view_value_changed,
6444 g_object_unref (text_view->vadjustment);
6447 if (text_view->hadjustment != hadj)
6449 text_view->hadjustment = hadj;
6450 g_object_ref (text_view->hadjustment);
6451 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
6453 g_signal_connect (text_view->hadjustment, "value_changed",
6454 G_CALLBACK (gtk_text_view_value_changed),
6459 if (text_view->vadjustment != vadj)
6461 text_view->vadjustment = vadj;
6462 g_object_ref (text_view->vadjustment);
6463 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
6465 g_signal_connect (text_view->vadjustment, "value_changed",
6466 G_CALLBACK (gtk_text_view_value_changed),
6472 gtk_text_view_value_changed (NULL, text_view);
6475 /* FIXME this adjust_allocation is a big cut-and-paste from
6476 * GtkCList, needs to be some "official" way to do this
6486 /* The window to which widget->window is relative */
6487 #define ALLOCATION_WINDOW(widget) \
6488 (GTK_WIDGET_NO_WINDOW (widget) ? \
6489 (widget)->window : \
6490 gdk_window_get_parent ((widget)->window))
6493 adjust_allocation_recurse (GtkWidget *widget,
6496 ScrollData *scroll_data = data;
6498 /* Need to really size allocate instead of just poking
6499 * into widget->allocation if the widget is not realized.
6500 * FIXME someone figure out why this was.
6502 if (!GTK_WIDGET_REALIZED (widget))
6504 if (GTK_WIDGET_VISIBLE (widget))
6506 GdkRectangle tmp_rectangle = widget->allocation;
6507 tmp_rectangle.x += scroll_data->dx;
6508 tmp_rectangle.y += scroll_data->dy;
6510 gtk_widget_size_allocate (widget, &tmp_rectangle);
6515 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
6517 widget->allocation.x += scroll_data->dx;
6518 widget->allocation.y += scroll_data->dy;
6520 if (GTK_IS_CONTAINER (widget))
6521 gtk_container_forall (GTK_CONTAINER (widget),
6522 adjust_allocation_recurse,
6529 adjust_allocation (GtkWidget *widget,
6533 ScrollData scroll_data;
6535 if (GTK_WIDGET_REALIZED (widget))
6536 scroll_data.window = ALLOCATION_WINDOW (widget);
6538 scroll_data.window = NULL;
6540 scroll_data.dx = dx;
6541 scroll_data.dy = dy;
6543 adjust_allocation_recurse (widget, &scroll_data);
6547 gtk_text_view_value_changed (GtkAdjustment *adj,
6548 GtkTextView *text_view)
6555 /* Note that we oddly call this function with adj == NULL
6559 text_view->onscreen_validated = FALSE;
6561 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
6562 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
6563 adj ? adj->value : 0.0));
6565 if (adj == text_view->hadjustment)
6567 dx = text_view->xoffset - (gint)adj->value;
6568 text_view->xoffset = adj->value;
6570 /* If the change is due to a size change we need
6571 * to invalidate the entire text window because there might be
6572 * right-aligned or centered text
6574 if (text_view->width_changed)
6576 gdk_window_invalidate_rect (text_view->text_window->bin_window, NULL, FALSE);
6578 text_view->width_changed = FALSE;
6581 else if (adj == text_view->vadjustment)
6583 dy = text_view->yoffset - (gint)adj->value;
6584 text_view->yoffset = adj->value;
6586 if (text_view->layout)
6588 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
6590 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
6592 text_view->first_para_pixels = adj->value - line_top;
6596 if (dx != 0 || dy != 0)
6600 if (GTK_WIDGET_REALIZED (text_view))
6604 if (text_view->left_window)
6605 text_window_scroll (text_view->left_window, 0, dy);
6606 if (text_view->right_window)
6607 text_window_scroll (text_view->right_window, 0, dy);
6612 if (text_view->top_window)
6613 text_window_scroll (text_view->top_window, dx, 0);
6614 if (text_view->bottom_window)
6615 text_window_scroll (text_view->bottom_window, dx, 0);
6618 /* It looks nicer to scroll the main area last, because
6619 * it takes a while, and making the side areas update
6620 * afterward emphasizes the slowness of scrolling the
6623 text_window_scroll (text_view->text_window, dx, dy);
6626 /* Children are now "moved" in the text window, poke
6627 * into widget->allocation for each child
6629 tmp_list = text_view->children;
6630 while (tmp_list != NULL)
6632 GtkTextViewChild *child = tmp_list->data;
6635 adjust_allocation (child->widget, dx, dy);
6637 tmp_list = g_slist_next (tmp_list);
6641 /* This could result in invalidation, which would install the
6642 * first_validate_idle, which would validate onscreen;
6643 * but we're going to go ahead and validate here, so
6644 * first_validate_idle shouldn't have anything to do.
6646 gtk_text_view_update_layout_width (text_view);
6648 /* note that validation of onscreen could invoke this function
6649 * recursively, by scrolling to maintain first_para, or in response
6650 * to updating the layout width, however there is no problem with
6651 * that, or shouldn't be.
6653 gtk_text_view_validate_onscreen (text_view);
6655 /* process exposes */
6656 if (GTK_WIDGET_REALIZED (text_view))
6658 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6660 if (text_view->left_window)
6661 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6663 if (text_view->right_window)
6664 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6666 if (text_view->top_window)
6667 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6669 if (text_view->bottom_window)
6670 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6672 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6675 /* If this got installed, get rid of it, it's just a waste of time. */
6676 if (text_view->first_validate_idle != 0)
6678 g_source_remove (text_view->first_validate_idle);
6679 text_view->first_validate_idle = 0;
6682 gtk_text_view_update_im_spot_location (text_view);
6684 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6688 gtk_text_view_commit_handler (GtkIMContext *context,
6690 GtkTextView *text_view)
6692 gtk_text_view_commit_text (text_view, str);
6696 gtk_text_view_commit_text (GtkTextView *text_view,
6699 gboolean had_selection;
6701 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6703 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6706 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6707 text_view->editable);
6709 if (!strcmp (str, "\n"))
6711 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6712 text_view->editable);
6716 if (!had_selection && text_view->overwrite_mode)
6720 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6722 gtk_text_buffer_get_mark (get_buffer (text_view),
6724 if (!gtk_text_iter_ends_line (&insert))
6725 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6727 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6728 text_view->editable);
6731 gtk_text_buffer_end_user_action (get_buffer (text_view));
6733 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6734 gtk_text_view_scroll_mark_onscreen (text_view,
6735 gtk_text_buffer_get_mark (get_buffer (text_view),
6740 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6741 GtkTextView *text_view)
6744 PangoAttrList *attrs;
6747 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6748 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6749 pango_attr_list_unref (attrs);
6752 gtk_text_view_scroll_mark_onscreen (text_view,
6753 gtk_text_buffer_get_mark (get_buffer (text_view),
6758 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6759 GtkTextView *text_view)
6766 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6767 gtk_text_buffer_get_insert (text_view->buffer));
6770 pos = gtk_text_iter_get_line_index (&start);
6771 gtk_text_iter_set_line_offset (&start, 0);
6772 gtk_text_iter_forward_to_line_end (&end);
6774 text = gtk_text_iter_get_slice (&start, &end);
6775 gtk_im_context_set_surrounding (context, text, -1, pos);
6782 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6785 GtkTextView *text_view)
6790 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6791 gtk_text_buffer_get_insert (text_view->buffer));
6794 gtk_text_iter_forward_chars (&start, offset);
6795 gtk_text_iter_forward_chars (&end, offset + n_chars);
6797 gtk_text_buffer_delete_interactive (text_view->buffer, &start, &end,
6798 text_view->editable);
6804 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6805 const GtkTextIter *location,
6809 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6810 gboolean need_reset = FALSE;
6812 if (mark == gtk_text_buffer_get_insert (buffer))
6814 text_view->virtual_cursor_x = -1;
6815 text_view->virtual_cursor_y = -1;
6816 gtk_text_view_update_im_spot_location (text_view);
6819 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6825 gtk_text_view_reset_im_context (text_view);
6829 gtk_text_view_get_cursor_location (GtkTextView *text_view,
6834 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6835 gtk_text_buffer_get_mark (get_buffer (text_view),
6838 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, pos, NULL);
6842 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6848 if ((x && text_view->virtual_cursor_x == -1) ||
6849 (y && text_view->virtual_cursor_y == -1))
6850 gtk_text_view_get_cursor_location (text_view, &pos);
6854 if (text_view->virtual_cursor_x != -1)
6855 *x = text_view->virtual_cursor_x;
6862 if (text_view->virtual_cursor_x != -1)
6863 *y = text_view->virtual_cursor_y;
6865 *y = pos.y + pos.height / 2;
6870 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6876 if (x == -1 || y == -1)
6877 gtk_text_view_get_cursor_location (text_view, &pos);
6879 text_view->virtual_cursor_x = (x == -1) ? pos.x : x;
6880 text_view->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
6883 /* Quick hack of a popup menu
6886 activate_cb (GtkWidget *menuitem,
6887 GtkTextView *text_view)
6889 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6890 g_signal_emit_by_name (text_view, signal);
6894 append_action_signal (GtkTextView *text_view,
6896 const gchar *stock_id,
6897 const gchar *signal,
6900 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6902 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6903 g_signal_connect (menuitem, "activate",
6904 G_CALLBACK (activate_cb), text_view);
6906 gtk_widget_set_sensitive (menuitem, sensitive);
6908 gtk_widget_show (menuitem);
6909 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6913 gtk_text_view_select_all (GtkWidget *widget,
6916 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6917 GtkTextBuffer *buffer;
6918 GtkTextIter start_iter, end_iter, insert;
6920 buffer = text_view->buffer;
6923 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
6924 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
6928 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
6929 gtk_text_buffer_get_insert (buffer));
6930 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
6935 select_all_cb (GtkWidget *menuitem,
6936 GtkTextView *text_view)
6938 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
6942 delete_cb (GtkTextView *text_view)
6944 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6945 text_view->editable);
6949 popup_menu_detach (GtkWidget *attach_widget,
6952 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6956 popup_position_func (GtkMenu *menu,
6962 GtkTextView *text_view;
6964 GdkRectangle cursor_rect;
6965 GdkRectangle onscreen_rect;
6966 gint root_x, root_y;
6971 GdkRectangle monitor;
6973 text_view = GTK_TEXT_VIEW (user_data);
6974 widget = GTK_WIDGET (text_view);
6976 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6978 screen = gtk_widget_get_screen (widget);
6980 gdk_window_get_origin (widget->window, &root_x, &root_y);
6982 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6984 gtk_text_buffer_get_insert (get_buffer (text_view)));
6986 gtk_text_view_get_iter_location (text_view,
6990 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6992 gtk_widget_size_request (text_view->popup_menu, &req);
6994 /* can't use rectangle_intersect since cursor rect can have 0 width */
6995 if (cursor_rect.x >= onscreen_rect.x &&
6996 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6997 cursor_rect.y >= onscreen_rect.y &&
6998 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
7000 gtk_text_view_buffer_to_window_coords (text_view,
7001 GTK_TEXT_WINDOW_WIDGET,
7002 cursor_rect.x, cursor_rect.y,
7003 &cursor_rect.x, &cursor_rect.y);
7005 *x = root_x + cursor_rect.x + cursor_rect.width;
7006 *y = root_y + cursor_rect.y + cursor_rect.height;
7010 /* Just center the menu, since cursor is offscreen. */
7011 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
7012 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
7016 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
7017 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
7019 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
7020 gtk_menu_set_monitor (menu, monitor_num);
7021 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
7023 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
7024 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
7031 GtkTextView *text_view;
7037 range_contains_editable_text (const GtkTextIter *start,
7038 const GtkTextIter *end,
7039 gboolean default_editability)
7041 GtkTextIter iter = *start;
7043 while (gtk_text_iter_compare (&iter, end) < 0)
7045 if (gtk_text_iter_editable (&iter, default_editability))
7048 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
7055 unichar_chosen_func (const char *text,
7058 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7060 gtk_text_view_commit_text (text_view, text);
7064 popup_targets_received (GtkClipboard *clipboard,
7065 GtkSelectionData *data,
7068 PopupInfo *info = user_data;
7069 GtkTextView *text_view = info->text_view;
7071 if (GTK_WIDGET_REALIZED (text_view))
7073 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
7074 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
7076 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
7077 GtkWidget *menuitem;
7079 gboolean have_selection;
7080 gboolean can_insert;
7082 GtkTextIter sel_start, sel_end;
7084 if (text_view->popup_menu)
7085 gtk_widget_destroy (text_view->popup_menu);
7087 text_view->popup_menu = gtk_menu_new ();
7089 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
7090 GTK_WIDGET (text_view),
7093 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7094 &sel_start, &sel_end);
7096 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7098 gtk_text_buffer_get_insert (get_buffer (text_view)));
7100 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
7102 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
7104 range_contains_editable_text (&sel_start, &sel_end,
7105 text_view->editable));
7106 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
7108 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
7109 can_insert && clipboard_contains_text);
7111 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
7112 gtk_widget_set_sensitive (menuitem,
7114 range_contains_editable_text (&sel_start, &sel_end,
7115 text_view->editable));
7116 g_signal_connect_swapped (menuitem, "activate",
7117 G_CALLBACK (delete_cb), text_view);
7118 gtk_widget_show (menuitem);
7119 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7121 menuitem = gtk_separator_menu_item_new ();
7122 gtk_widget_show (menuitem);
7123 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7125 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
7126 g_signal_connect (menuitem, "activate",
7127 G_CALLBACK (select_all_cb), text_view);
7128 gtk_widget_show (menuitem);
7129 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7131 menuitem = gtk_separator_menu_item_new ();
7132 gtk_widget_show (menuitem);
7133 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7135 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
7136 gtk_widget_show (menuitem);
7137 gtk_widget_set_sensitive (menuitem, can_insert);
7139 submenu = gtk_menu_new ();
7140 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7141 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7143 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
7144 GTK_MENU_SHELL (submenu));
7146 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
7147 gtk_widget_show (menuitem);
7148 gtk_widget_set_sensitive (menuitem, can_insert);
7150 submenu = gtk_menu_new ();
7151 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7152 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7154 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
7155 unichar_chosen_func,
7158 g_signal_emit (text_view,
7159 signals[POPULATE_POPUP],
7161 text_view->popup_menu);
7164 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7166 info->button, info->time);
7169 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7170 popup_position_func, text_view,
7171 0, gtk_get_current_event_time ());
7172 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
7176 g_object_unref (text_view);
7181 gtk_text_view_do_popup (GtkTextView *text_view,
7182 GdkEventButton *event)
7184 PopupInfo *info = g_new (PopupInfo, 1);
7186 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
7187 gtk_text_view_end_selection_drag (text_view, event);
7189 /* In order to know what entries we should make sensitive, we
7190 * ask for the current targets of the clipboard, and when
7191 * we get them, then we actually pop up the menu.
7193 info->text_view = g_object_ref (text_view);
7197 info->button = event->button;
7198 info->time = event->time;
7203 info->time = gtk_get_current_event_time ();
7206 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
7207 GDK_SELECTION_CLIPBOARD),
7208 gdk_atom_intern ("TARGETS", FALSE),
7209 popup_targets_received,
7214 gtk_text_view_popup_menu (GtkWidget *widget)
7216 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
7220 /* Child GdkWindows */
7223 static GtkTextWindow*
7224 text_window_new (GtkTextWindowType type,
7227 gint height_request)
7231 win = g_new (GtkTextWindow, 1);
7234 win->widget = widget;
7236 win->bin_window = NULL;
7237 win->requisition.width = width_request;
7238 win->requisition.height = height_request;
7239 win->allocation.width = width_request;
7240 win->allocation.height = height_request;
7241 win->allocation.x = 0;
7242 win->allocation.y = 0;
7248 text_window_free (GtkTextWindow *win)
7251 text_window_unrealize (win);
7257 text_window_realize (GtkTextWindow *win,
7260 GdkWindowAttr attributes;
7261 gint attributes_mask;
7264 attributes.window_type = GDK_WINDOW_CHILD;
7265 attributes.x = win->allocation.x;
7266 attributes.y = win->allocation.y;
7267 attributes.width = win->allocation.width;
7268 attributes.height = win->allocation.height;
7269 attributes.wclass = GDK_INPUT_OUTPUT;
7270 attributes.visual = gtk_widget_get_visual (win->widget);
7271 attributes.colormap = gtk_widget_get_colormap (win->widget);
7272 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
7274 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
7276 win->window = gdk_window_new (parent,
7280 gdk_window_set_back_pixmap (win->window, NULL, FALSE);
7282 gdk_window_show (win->window);
7283 gdk_window_set_user_data (win->window, win->widget);
7287 attributes.width = win->allocation.width;
7288 attributes.height = win->allocation.height;
7289 attributes.event_mask = (GDK_EXPOSURE_MASK |
7291 GDK_KEY_PRESS_MASK |
7292 GDK_BUTTON_PRESS_MASK |
7293 GDK_BUTTON_RELEASE_MASK |
7294 GDK_POINTER_MOTION_MASK |
7295 GDK_POINTER_MOTION_HINT_MASK |
7296 gtk_widget_get_events (win->widget));
7298 win->bin_window = gdk_window_new (win->window,
7302 gdk_window_show (win->bin_window);
7303 gdk_window_set_user_data (win->bin_window, win->widget);
7305 if (win->type == GTK_TEXT_WINDOW_TEXT)
7308 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
7310 gdk_window_set_cursor (win->bin_window, cursor);
7311 gdk_cursor_unref (cursor);
7313 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
7317 gdk_window_set_background (win->bin_window,
7318 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
7322 gdk_window_set_background (win->bin_window,
7323 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
7326 g_object_set_qdata (G_OBJECT (win->window),
7327 g_quark_from_static_string ("gtk-text-view-text-window"),
7330 g_object_set_qdata (G_OBJECT (win->bin_window),
7331 g_quark_from_static_string ("gtk-text-view-text-window"),
7336 text_window_unrealize (GtkTextWindow *win)
7338 if (win->type == GTK_TEXT_WINDOW_TEXT)
7340 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
7344 gdk_window_set_user_data (win->window, NULL);
7345 gdk_window_set_user_data (win->bin_window, NULL);
7346 gdk_window_destroy (win->bin_window);
7347 gdk_window_destroy (win->window);
7349 win->bin_window = NULL;
7353 text_window_size_allocate (GtkTextWindow *win,
7356 win->allocation = *rect;
7360 gdk_window_move_resize (win->window,
7362 rect->width, rect->height);
7364 gdk_window_resize (win->bin_window,
7365 rect->width, rect->height);
7370 text_window_scroll (GtkTextWindow *win,
7374 if (dx != 0 || dy != 0)
7376 gdk_window_scroll (win->bin_window, dx, dy);
7381 text_window_invalidate_rect (GtkTextWindow *win,
7384 GdkRectangle window_rect;
7386 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
7393 window_rect.width = rect->width;
7394 window_rect.height = rect->height;
7396 /* Adjust the rect as appropriate */
7400 case GTK_TEXT_WINDOW_TEXT:
7403 case GTK_TEXT_WINDOW_LEFT:
7404 case GTK_TEXT_WINDOW_RIGHT:
7406 window_rect.width = win->allocation.width;
7409 case GTK_TEXT_WINDOW_TOP:
7410 case GTK_TEXT_WINDOW_BOTTOM:
7412 window_rect.height = win->allocation.height;
7416 g_warning ("%s: bug!", G_STRLOC);
7421 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
7425 cairo_t *cr = gdk_cairo_create (win->bin_window);
7426 gdk_cairo_rectangle (cr, &window_rect);
7427 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
7435 text_window_invalidate_cursors (GtkTextWindow *win)
7437 GtkTextView *text_view = GTK_TEXT_VIEW (win->widget);
7439 GdkRectangle strong;
7441 gboolean draw_arrow;
7442 gfloat cursor_aspect_ratio;
7446 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7447 gtk_text_buffer_get_insert (text_view->buffer));
7449 gtk_text_layout_get_cursor_locations (text_view->layout, &iter,
7452 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
7453 * ignoring the text direction be exposing both sides of the cursor
7456 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
7458 gtk_widget_style_get (win->widget,
7459 "cursor-aspect-ratio", &cursor_aspect_ratio,
7462 stem_width = strong.height * cursor_aspect_ratio + 1;
7463 arrow_width = stem_width + 1;
7465 /* round up to the next even number */
7469 strong.x -= stem_width / 2;
7470 strong.width += stem_width;
7474 strong.x -= arrow_width;
7475 strong.width += arrow_width * 2;
7478 text_window_invalidate_rect (win, &strong);
7480 if (draw_arrow) /* == have weak */
7482 stem_width = weak.height * cursor_aspect_ratio + 1;
7483 arrow_width = stem_width + 1;
7485 /* round up to the next even number */
7489 weak.x -= stem_width / 2;
7490 weak.width += stem_width;
7492 weak.x -= arrow_width;
7493 weak.width += arrow_width * 2;
7495 text_window_invalidate_rect (win, &weak);
7500 text_window_get_width (GtkTextWindow *win)
7502 return win->allocation.width;
7506 text_window_get_height (GtkTextWindow *win)
7508 return win->allocation.height;
7515 * gtk_text_view_get_window:
7516 * @text_view: a #GtkTextView
7517 * @win: window to get
7519 * Retrieves the #GdkWindow corresponding to an area of the text view;
7520 * possible windows include the overall widget window, child windows
7521 * on the left, right, top, bottom, and the window that displays the
7522 * text buffer. Windows are %NULL and nonexistent if their width or
7523 * height is 0, and are nonexistent before the widget has been
7526 * Return value: a #GdkWindow, or %NULL
7529 gtk_text_view_get_window (GtkTextView *text_view,
7530 GtkTextWindowType win)
7532 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7536 case GTK_TEXT_WINDOW_WIDGET:
7537 return GTK_WIDGET (text_view)->window;
7540 case GTK_TEXT_WINDOW_TEXT:
7541 return text_view->text_window->bin_window;
7544 case GTK_TEXT_WINDOW_LEFT:
7545 if (text_view->left_window)
7546 return text_view->left_window->bin_window;
7551 case GTK_TEXT_WINDOW_RIGHT:
7552 if (text_view->right_window)
7553 return text_view->right_window->bin_window;
7558 case GTK_TEXT_WINDOW_TOP:
7559 if (text_view->top_window)
7560 return text_view->top_window->bin_window;
7565 case GTK_TEXT_WINDOW_BOTTOM:
7566 if (text_view->bottom_window)
7567 return text_view->bottom_window->bin_window;
7572 case GTK_TEXT_WINDOW_PRIVATE:
7573 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
7578 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
7583 * gtk_text_view_get_window_type:
7584 * @text_view: a #GtkTextView
7585 * @window: a window type
7587 * Usually used to find out which window an event corresponds to.
7588 * If you connect to an event signal on @text_view, this function
7589 * should be called on <literal>event->window</literal> to
7590 * see which window it was.
7592 * Return value: the window type.
7595 gtk_text_view_get_window_type (GtkTextView *text_view,
7600 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7601 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
7603 if (window == GTK_WIDGET (text_view)->window)
7604 return GTK_TEXT_WINDOW_WIDGET;
7606 win = g_object_get_qdata (G_OBJECT (window),
7607 g_quark_try_string ("gtk-text-view-text-window"));
7613 return GTK_TEXT_WINDOW_PRIVATE;
7618 buffer_to_widget (GtkTextView *text_view,
7626 *window_x = buffer_x - text_view->xoffset;
7627 *window_x += text_view->text_window->allocation.x;
7632 *window_y = buffer_y - text_view->yoffset;
7633 *window_y += text_view->text_window->allocation.y;
7638 widget_to_text_window (GtkTextWindow *win,
7645 *window_x = widget_x - win->allocation.x;
7648 *window_y = widget_y - win->allocation.y;
7652 buffer_to_text_window (GtkTextView *text_view,
7661 g_warning ("Attempt to convert text buffer coordinates to coordinates "
7662 "for a nonexistent or private child window of GtkTextView");
7666 buffer_to_widget (text_view,
7668 window_x, window_y);
7670 widget_to_text_window (win,
7671 window_x ? *window_x : 0,
7672 window_y ? *window_y : 0,
7678 * gtk_text_view_buffer_to_window_coords:
7679 * @text_view: a #GtkTextView
7680 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7681 * @buffer_x: buffer x coordinate
7682 * @buffer_y: buffer y coordinate
7683 * @window_x: window x coordinate return location
7684 * @window_y: window y coordinate return location
7686 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
7687 * @win, and stores the result in (@window_x, @window_y).
7689 * Note that you can't convert coordinates for a nonexisting window (see
7690 * gtk_text_view_set_border_window_size()).
7693 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
7694 GtkTextWindowType win,
7700 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7704 case GTK_TEXT_WINDOW_WIDGET:
7705 buffer_to_widget (text_view,
7707 window_x, window_y);
7710 case GTK_TEXT_WINDOW_TEXT:
7712 *window_x = buffer_x - text_view->xoffset;
7714 *window_y = buffer_y - text_view->yoffset;
7717 case GTK_TEXT_WINDOW_LEFT:
7718 buffer_to_text_window (text_view,
7719 text_view->left_window,
7721 window_x, window_y);
7724 case GTK_TEXT_WINDOW_RIGHT:
7725 buffer_to_text_window (text_view,
7726 text_view->right_window,
7728 window_x, window_y);
7731 case GTK_TEXT_WINDOW_TOP:
7732 buffer_to_text_window (text_view,
7733 text_view->top_window,
7735 window_x, window_y);
7738 case GTK_TEXT_WINDOW_BOTTOM:
7739 buffer_to_text_window (text_view,
7740 text_view->bottom_window,
7742 window_x, window_y);
7745 case GTK_TEXT_WINDOW_PRIVATE:
7746 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7750 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7756 widget_to_buffer (GtkTextView *text_view,
7764 *buffer_x = widget_x + text_view->xoffset;
7765 *buffer_x -= text_view->text_window->allocation.x;
7770 *buffer_y = widget_y + text_view->yoffset;
7771 *buffer_y -= text_view->text_window->allocation.y;
7776 text_window_to_widget (GtkTextWindow *win,
7783 *widget_x = window_x + win->allocation.x;
7786 *widget_y = window_y + win->allocation.y;
7790 text_window_to_buffer (GtkTextView *text_view,
7799 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7800 "coordinates for a nonexistent child window.");
7804 text_window_to_widget (win,
7810 widget_to_buffer (text_view,
7811 buffer_x ? *buffer_x : 0,
7812 buffer_y ? *buffer_y : 0,
7818 * gtk_text_view_window_to_buffer_coords:
7819 * @text_view: a #GtkTextView
7820 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7821 * @window_x: window x coordinate
7822 * @window_y: window y coordinate
7823 * @buffer_x: buffer x coordinate return location
7824 * @buffer_y: buffer y coordinate return location
7826 * Converts coordinates on the window identified by @win to buffer
7827 * coordinates, storing the result in (@buffer_x,@buffer_y).
7829 * Note that you can't convert coordinates for a nonexisting window (see
7830 * gtk_text_view_set_border_window_size()).
7833 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7834 GtkTextWindowType win,
7840 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7844 case GTK_TEXT_WINDOW_WIDGET:
7845 widget_to_buffer (text_view,
7847 buffer_x, buffer_y);
7850 case GTK_TEXT_WINDOW_TEXT:
7852 *buffer_x = window_x + text_view->xoffset;
7854 *buffer_y = window_y + text_view->yoffset;
7857 case GTK_TEXT_WINDOW_LEFT:
7858 text_window_to_buffer (text_view,
7859 text_view->left_window,
7861 buffer_x, buffer_y);
7864 case GTK_TEXT_WINDOW_RIGHT:
7865 text_window_to_buffer (text_view,
7866 text_view->right_window,
7868 buffer_x, buffer_y);
7871 case GTK_TEXT_WINDOW_TOP:
7872 text_window_to_buffer (text_view,
7873 text_view->top_window,
7875 buffer_x, buffer_y);
7878 case GTK_TEXT_WINDOW_BOTTOM:
7879 text_window_to_buffer (text_view,
7880 text_view->bottom_window,
7882 buffer_x, buffer_y);
7885 case GTK_TEXT_WINDOW_PRIVATE:
7886 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7890 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7896 set_window_width (GtkTextView *text_view,
7898 GtkTextWindowType type,
7899 GtkTextWindow **winp)
7905 text_window_free (*winp);
7907 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7914 *winp = text_window_new (type,
7915 GTK_WIDGET (text_view),
7917 /* if the widget is already realized we need to realize the child manually */
7918 if (GTK_WIDGET_REALIZED (text_view))
7919 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7923 if ((*winp)->requisition.width == width)
7926 (*winp)->requisition.width = width;
7929 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7935 set_window_height (GtkTextView *text_view,
7937 GtkTextWindowType type,
7938 GtkTextWindow **winp)
7944 text_window_free (*winp);
7946 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7953 *winp = text_window_new (type,
7954 GTK_WIDGET (text_view),
7957 /* if the widget is already realized we need to realize the child manually */
7958 if (GTK_WIDGET_REALIZED (text_view))
7959 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7963 if ((*winp)->requisition.height == height)
7966 (*winp)->requisition.height = height;
7969 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7974 * gtk_text_view_set_border_window_size:
7975 * @text_view: a #GtkTextView
7976 * @type: window to affect
7977 * @size: width or height of the window
7979 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7980 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7981 * Automatically destroys the corresponding window if the size is set
7982 * to 0, and creates the window if the size is set to non-zero. This
7983 * function can only be used for the "border windows," it doesn't work
7984 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7985 * #GTK_TEXT_WINDOW_PRIVATE.
7988 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7989 GtkTextWindowType type,
7993 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7994 g_return_if_fail (size >= 0);
7998 case GTK_TEXT_WINDOW_LEFT:
7999 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
8000 &text_view->left_window);
8003 case GTK_TEXT_WINDOW_RIGHT:
8004 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
8005 &text_view->right_window);
8008 case GTK_TEXT_WINDOW_TOP:
8009 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
8010 &text_view->top_window);
8013 case GTK_TEXT_WINDOW_BOTTOM:
8014 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
8015 &text_view->bottom_window);
8019 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
8025 * gtk_text_view_get_border_window_size:
8026 * @text_view: a #GtkTextView
8027 * @type: window to return size from
8029 * Gets the width of the specified border window. See
8030 * gtk_text_view_set_border_window_size().
8032 * Return value: width of window
8035 gtk_text_view_get_border_window_size (GtkTextView *text_view,
8036 GtkTextWindowType type)
8038 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8042 case GTK_TEXT_WINDOW_LEFT:
8043 if (text_view->left_window)
8044 return text_view->left_window->requisition.width;
8046 case GTK_TEXT_WINDOW_RIGHT:
8047 if (text_view->right_window)
8048 return text_view->right_window->requisition.width;
8050 case GTK_TEXT_WINDOW_TOP:
8051 if (text_view->top_window)
8052 return text_view->top_window->requisition.height;
8054 case GTK_TEXT_WINDOW_BOTTOM:
8055 if (text_view->bottom_window)
8056 return text_view->bottom_window->requisition.height;
8059 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
8070 static GtkTextViewChild*
8071 text_view_child_new_anchored (GtkWidget *child,
8072 GtkTextChildAnchor *anchor,
8073 GtkTextLayout *layout)
8075 GtkTextViewChild *vc;
8077 vc = g_new (GtkTextViewChild, 1);
8079 vc->type = GTK_TEXT_WINDOW_PRIVATE;
8081 vc->anchor = anchor;
8083 vc->from_top_of_line = 0;
8084 vc->from_left_of_buffer = 0;
8086 g_object_ref (vc->widget);
8087 g_object_ref (vc->anchor);
8089 g_object_set_data (G_OBJECT (child),
8090 "gtk-text-view-child",
8093 gtk_text_child_anchor_register_child (anchor, child, layout);
8098 static GtkTextViewChild*
8099 text_view_child_new_window (GtkWidget *child,
8100 GtkTextWindowType type,
8104 GtkTextViewChild *vc;
8106 vc = g_new (GtkTextViewChild, 1);
8111 vc->from_top_of_line = 0;
8112 vc->from_left_of_buffer = 0;
8114 g_object_ref (vc->widget);
8120 g_object_set_data (G_OBJECT (child),
8121 "gtk-text-view-child",
8128 text_view_child_free (GtkTextViewChild *child)
8130 g_object_set_data (G_OBJECT (child->widget),
8131 "gtk-text-view-child", NULL);
8135 gtk_text_child_anchor_unregister_child (child->anchor,
8137 g_object_unref (child->anchor);
8140 g_object_unref (child->widget);
8146 text_view_child_set_parent_window (GtkTextView *text_view,
8147 GtkTextViewChild *vc)
8150 gtk_widget_set_parent_window (vc->widget,
8151 text_view->text_window->bin_window);
8155 window = gtk_text_view_get_window (text_view,
8157 gtk_widget_set_parent_window (vc->widget, window);
8162 add_child (GtkTextView *text_view,
8163 GtkTextViewChild *vc)
8165 text_view->children = g_slist_prepend (text_view->children,
8168 if (GTK_WIDGET_REALIZED (text_view))
8169 text_view_child_set_parent_window (text_view, vc);
8171 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
8175 * gtk_text_view_add_child_at_anchor:
8176 * @text_view: a #GtkTextView
8177 * @child: a #GtkWidget
8178 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
8180 * Adds a child widget in the text buffer, at the given @anchor.
8184 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
8186 GtkTextChildAnchor *anchor)
8188 GtkTextViewChild *vc;
8190 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8191 g_return_if_fail (GTK_IS_WIDGET (child));
8192 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
8193 g_return_if_fail (child->parent == NULL);
8195 gtk_text_view_ensure_layout (text_view);
8197 vc = text_view_child_new_anchored (child, anchor,
8200 add_child (text_view, vc);
8202 g_assert (vc->widget == child);
8203 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8207 * gtk_text_view_add_child_in_window:
8208 * @text_view: a #GtkTextView
8209 * @child: a #GtkWidget
8210 * @which_window: which window the child should appear in
8211 * @xpos: X position of child in window coordinates
8212 * @ypos: Y position of child in window coordinates
8214 * Adds a child at fixed coordinates in one of the text widget's
8215 * windows. The window must have nonzero size (see
8216 * gtk_text_view_set_border_window_size()). Note that the child
8217 * coordinates are given relative to the #GdkWindow in question, and
8218 * that these coordinates have no sane relationship to scrolling. When
8219 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
8220 * irrelevant, the child floats above all scrollable areas. But when
8221 * placing a child in one of the scrollable windows (border windows or
8222 * text window), you'll need to compute the child's correct position
8223 * in buffer coordinates any time scrolling occurs or buffer changes
8224 * occur, and then call gtk_text_view_move_child() to update the
8225 * child's position. Unfortunately there's no good way to detect that
8226 * scrolling has occurred, using the current API; a possible hack
8227 * would be to update all child positions when the scroll adjustments
8228 * change or the text buffer changes. See bug 64518 on
8229 * bugzilla.gnome.org for status of fixing this issue.
8233 gtk_text_view_add_child_in_window (GtkTextView *text_view,
8235 GtkTextWindowType which_window,
8239 GtkTextViewChild *vc;
8241 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8242 g_return_if_fail (GTK_IS_WIDGET (child));
8243 g_return_if_fail (child->parent == NULL);
8245 vc = text_view_child_new_window (child, which_window,
8248 add_child (text_view, vc);
8250 g_assert (vc->widget == child);
8251 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8255 * gtk_text_view_move_child:
8256 * @text_view: a #GtkTextView
8257 * @child: child widget already added to the text view
8258 * @xpos: new X position in window coordinates
8259 * @ypos: new Y position in window coordinates
8261 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
8265 gtk_text_view_move_child (GtkTextView *text_view,
8270 GtkTextViewChild *vc;
8272 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8273 g_return_if_fail (GTK_IS_WIDGET (child));
8274 g_return_if_fail (child->parent == (GtkWidget*) text_view);
8276 vc = g_object_get_data (G_OBJECT (child),
8277 "gtk-text-view-child");
8279 g_assert (vc != NULL);
8281 if (vc->x == xpos &&
8288 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
8289 gtk_widget_queue_resize (child);
8293 /* Iterator operations */
8296 * gtk_text_view_forward_display_line:
8297 * @text_view: a #GtkTextView
8298 * @iter: a #GtkTextIter
8300 * Moves the given @iter forward by one display (wrapped) line. A
8301 * display line is different from a paragraph. Paragraphs are
8302 * separated by newlines or other paragraph separator characters.
8303 * Display lines are created by line-wrapping a paragraph. If
8304 * wrapping is turned off, display lines and paragraphs will be the
8305 * same. Display lines are divided differently for each view, since
8306 * they depend on the view's width; paragraphs are the same in all
8307 * views, since they depend on the contents of the #GtkTextBuffer.
8309 * Return value: %TRUE if @iter was moved and is not on the end iterator
8312 gtk_text_view_forward_display_line (GtkTextView *text_view,
8315 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8316 g_return_val_if_fail (iter != NULL, FALSE);
8318 gtk_text_view_ensure_layout (text_view);
8320 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
8324 * gtk_text_view_backward_display_line:
8325 * @text_view: a #GtkTextView
8326 * @iter: a #GtkTextIter
8328 * Moves the given @iter backward by one display (wrapped) line. A
8329 * display line is different from a paragraph. Paragraphs are
8330 * separated by newlines or other paragraph separator characters.
8331 * Display lines are created by line-wrapping a paragraph. If
8332 * wrapping is turned off, display lines and paragraphs will be the
8333 * same. Display lines are divided differently for each view, since
8334 * they depend on the view's width; paragraphs are the same in all
8335 * views, since they depend on the contents of the #GtkTextBuffer.
8337 * Return value: %TRUE if @iter was moved and is not on the end iterator
8340 gtk_text_view_backward_display_line (GtkTextView *text_view,
8343 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8344 g_return_val_if_fail (iter != NULL, FALSE);
8346 gtk_text_view_ensure_layout (text_view);
8348 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
8352 * gtk_text_view_forward_display_line_end:
8353 * @text_view: a #GtkTextView
8354 * @iter: a #GtkTextIter
8356 * Moves the given @iter forward to the next display line end. A
8357 * display line is different from a paragraph. Paragraphs are
8358 * separated by newlines or other paragraph separator characters.
8359 * Display lines are created by line-wrapping a paragraph. If
8360 * wrapping is turned off, display lines and paragraphs will be the
8361 * same. Display lines are divided differently for each view, since
8362 * they depend on the view's width; paragraphs are the same in all
8363 * views, since they depend on the contents of the #GtkTextBuffer.
8365 * Return value: %TRUE if @iter was moved and is not on the end iterator
8368 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
8371 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8372 g_return_val_if_fail (iter != NULL, FALSE);
8374 gtk_text_view_ensure_layout (text_view);
8376 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
8380 * gtk_text_view_backward_display_line_start:
8381 * @text_view: a #GtkTextView
8382 * @iter: a #GtkTextIter
8384 * Moves the given @iter backward to the next display line start. A
8385 * display line is different from a paragraph. Paragraphs are
8386 * separated by newlines or other paragraph separator characters.
8387 * Display lines are created by line-wrapping a paragraph. If
8388 * wrapping is turned off, display lines and paragraphs will be the
8389 * same. Display lines are divided differently for each view, since
8390 * they depend on the view's width; paragraphs are the same in all
8391 * views, since they depend on the contents of the #GtkTextBuffer.
8393 * Return value: %TRUE if @iter was moved and is not on the end iterator
8396 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
8399 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8400 g_return_val_if_fail (iter != NULL, FALSE);
8402 gtk_text_view_ensure_layout (text_view);
8404 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
8408 * gtk_text_view_starts_display_line:
8409 * @text_view: a #GtkTextView
8410 * @iter: a #GtkTextIter
8412 * Determines whether @iter is at the start of a display line.
8413 * See gtk_text_view_forward_display_line() for an explanation of
8414 * display lines vs. paragraphs.
8416 * Return value: %TRUE if @iter begins a wrapped line
8419 gtk_text_view_starts_display_line (GtkTextView *text_view,
8420 const GtkTextIter *iter)
8422 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8423 g_return_val_if_fail (iter != NULL, FALSE);
8425 gtk_text_view_ensure_layout (text_view);
8427 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
8431 * gtk_text_view_move_visually:
8432 * @text_view: a #GtkTextView
8433 * @iter: a #GtkTextIter
8434 * @count: number of characters to move (negative moves left, positive moves right)
8436 * Move the iterator a given number of characters visually, treating
8437 * it as the strong cursor position. If @count is positive, then the
8438 * new strong cursor position will be @count positions to the right of
8439 * the old cursor position. If @count is negative then the new strong
8440 * cursor position will be @count positions to the left of the old
8443 * In the presence of bidirection text, the correspondence
8444 * between logical and visual order will depend on the direction
8445 * of the current run, and there may be jumps when the cursor
8446 * is moved off of the end of a run.
8448 * Return value: %TRUE if @iter moved and is not on the end iterator
8451 gtk_text_view_move_visually (GtkTextView *text_view,
8455 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8456 g_return_val_if_fail (iter != NULL, FALSE);
8458 gtk_text_view_ensure_layout (text_view);
8460 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
8463 #define __GTK_TEXT_VIEW_C__
8464 #include "gtkaliasdef.c"