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/.
29 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
30 #include "gtkbindings.h"
32 #include "gtkimagemenuitem.h"
35 #include "gtkmarshalers.h"
37 #include "gtkmenuitem.h"
38 #include "gtkseparatormenuitem.h"
39 #include "gtksettings.h"
41 #include "gtktextdisplay.h"
42 #include "gtktextview.h"
43 #include "gtkimmulticontext.h"
44 #include "gdk/gdkkeysyms.h"
45 #include "gtksizegroup.h" /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
46 #include "gtktextutil.h"
47 #include "gtkwindow.h"
49 /* How scrolling, validation, exposes, etc. work.
51 * The expose_event handler has the invariant that the onscreen lines
52 * have been validated.
54 * There are two ways that onscreen lines can become invalid. The first
55 * is to change which lines are onscreen. This happens when the value
56 * of a scroll adjustment changes. So the code path begins in
57 * gtk_text_view_value_changed() and goes like this:
58 * - gdk_window_scroll() to reflect the new adjustment value
59 * - validate the lines that were moved onscreen
60 * - gdk_window_process_updates() to handle the exposes immediately
62 * The second way is that you get the "invalidated" signal from the layout,
63 * indicating that lines have become invalid. This code path begins in
64 * invalidated_handler() and goes like this:
65 * - install high-priority idle which does the rest of the steps
66 * - if a scroll is pending from scroll_to_mark(), do the scroll,
67 * jumping to the gtk_text_view_value_changed() code path
68 * - otherwise, validate the onscreen lines
69 * - DO NOT process updates
71 * In both cases, validating the onscreen lines can trigger a scroll
72 * due to maintaining the first_para on the top of the screen.
73 * If validation triggers a scroll, we jump to the top of the code path
74 * for value_changed, and bail out of the current code path.
76 * Also, in size_allocate, if we invalidate some lines from changing
77 * the layout width, we need to go ahead and run the high-priority idle,
78 * because GTK sends exposes right after doing the size allocates without
79 * returning to the main loop. This is also why the high-priority idle
80 * is at a higher priority than resizing.
85 #define DEBUG_VALIDATION_AND_SCROLLING
88 #ifdef DEBUG_VALIDATION_AND_SCROLLING
94 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
95 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
97 struct _GtkTextPendingScroll
100 gdouble within_margin;
108 SET_SCROLL_ADJUSTMENTS,
126 PROP_PIXELS_ABOVE_LINES,
127 PROP_PIXELS_BELOW_LINES,
128 PROP_PIXELS_INSIDE_WRAP,
140 static void gtk_text_view_init (GtkTextView *text_view);
141 static void gtk_text_view_class_init (GtkTextViewClass *klass);
142 static void gtk_text_view_destroy (GtkObject *object);
143 static void gtk_text_view_finalize (GObject *object);
144 static void gtk_text_view_set_property (GObject *object,
148 static void gtk_text_view_get_property (GObject *object,
152 static void gtk_text_view_size_request (GtkWidget *widget,
153 GtkRequisition *requisition);
154 static void gtk_text_view_size_allocate (GtkWidget *widget,
155 GtkAllocation *allocation);
156 static void gtk_text_view_realize (GtkWidget *widget);
157 static void gtk_text_view_unrealize (GtkWidget *widget);
158 static void gtk_text_view_style_set (GtkWidget *widget,
159 GtkStyle *previous_style);
160 static void gtk_text_view_direction_changed (GtkWidget *widget,
161 GtkTextDirection previous_direction);
162 static gint gtk_text_view_event (GtkWidget *widget,
164 static gint gtk_text_view_key_press_event (GtkWidget *widget,
166 static gint gtk_text_view_key_release_event (GtkWidget *widget,
168 static gint gtk_text_view_button_press_event (GtkWidget *widget,
169 GdkEventButton *event);
170 static gint gtk_text_view_button_release_event (GtkWidget *widget,
171 GdkEventButton *event);
172 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
173 GdkEventFocus *event);
174 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
175 GdkEventFocus *event);
176 static gint gtk_text_view_motion_event (GtkWidget *widget,
177 GdkEventMotion *event);
178 static gint gtk_text_view_expose_event (GtkWidget *widget,
179 GdkEventExpose *expose);
180 static void gtk_text_view_draw_focus (GtkWidget *widget);
181 static void gtk_text_view_grab_focus (GtkWidget *widget);
182 static gboolean gtk_text_view_focus (GtkWidget *widget,
183 GtkDirectionType direction);
186 /* Source side drag signals */
187 static void gtk_text_view_drag_begin (GtkWidget *widget,
188 GdkDragContext *context);
189 static void gtk_text_view_drag_end (GtkWidget *widget,
190 GdkDragContext *context);
191 static void gtk_text_view_drag_data_get (GtkWidget *widget,
192 GdkDragContext *context,
193 GtkSelectionData *selection_data,
196 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
197 GdkDragContext *context);
199 /* Target side drag signals */
200 static void gtk_text_view_drag_leave (GtkWidget *widget,
201 GdkDragContext *context,
203 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
204 GdkDragContext *context,
208 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
209 GdkDragContext *context,
213 static void gtk_text_view_drag_data_received (GtkWidget *widget,
214 GdkDragContext *context,
217 GtkSelectionData *selection_data,
221 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
223 GtkAdjustment *vadj);
224 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
226 static void gtk_text_view_move_cursor (GtkTextView *text_view,
227 GtkMovementStep step,
229 gboolean extend_selection);
230 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
232 gboolean extend_selection);
233 static void gtk_text_view_set_anchor (GtkTextView *text_view);
234 static void gtk_text_view_scroll_pages (GtkTextView *text_view,
236 gboolean extend_selection);
237 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
239 gboolean extend_selection);
240 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
242 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
245 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
246 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
247 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
248 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
249 static void gtk_text_view_move_focus (GtkTextView *text_view,
250 GtkDirectionType direction_type);
251 static void gtk_text_view_unselect (GtkTextView *text_view);
253 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
254 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
256 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
257 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
258 GtkTextAttributes *values,
260 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
261 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
262 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
263 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
264 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
265 const GtkTextIter *iter,
266 GdkEventButton *event);
267 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
268 GdkEventButton *event);
269 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
270 const GtkTextIter *iter,
271 GdkEventMotion *event);
272 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
273 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
274 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
276 static void gtk_text_view_value_changed (GtkAdjustment *adj,
278 static void gtk_text_view_commit_handler (GtkIMContext *context,
280 GtkTextView *text_view);
281 static void gtk_text_view_commit_text (GtkTextView *text_view,
283 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
284 GtkTextView *text_view);
285 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
286 GtkTextView *text_view);
287 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
290 GtkTextView *text_view);
292 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
293 const GtkTextIter *location,
296 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
299 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
303 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
304 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
306 static void gtk_text_view_do_popup (GtkTextView *text_view,
307 GdkEventButton *event);
309 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
311 gdouble within_margin,
316 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
317 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
318 static void gtk_text_view_invalidate (GtkTextView *text_view);
319 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
321 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
323 /* Container methods */
324 static void gtk_text_view_add (GtkContainer *container,
326 static void gtk_text_view_remove (GtkContainer *container,
328 static void gtk_text_view_forall (GtkContainer *container,
329 gboolean include_internals,
330 GtkCallback callback,
331 gpointer callback_data);
333 /* FIXME probably need the focus methods. */
335 typedef struct _GtkTextViewChild GtkTextViewChild;
337 struct _GtkTextViewChild
341 GtkTextChildAnchor *anchor;
343 gint from_top_of_line;
344 gint from_left_of_buffer;
346 /* These are ignored if anchor != NULL */
347 GtkTextWindowType type;
352 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
353 GtkTextChildAnchor *anchor,
354 GtkTextLayout *layout);
355 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
356 GtkTextWindowType type,
359 static void text_view_child_free (GtkTextViewChild *child);
360 static void text_view_child_set_parent_window (GtkTextView *text_view,
361 GtkTextViewChild *child);
363 struct _GtkTextWindow
365 GtkTextWindowType type;
368 GdkWindow *bin_window;
369 GtkRequisition requisition;
370 GdkRectangle allocation;
373 static GtkTextWindow *text_window_new (GtkTextWindowType type,
376 gint height_request);
377 static void text_window_free (GtkTextWindow *win);
378 static void text_window_realize (GtkTextWindow *win,
380 static void text_window_unrealize (GtkTextWindow *win);
381 static void text_window_size_allocate (GtkTextWindow *win,
383 static void text_window_scroll (GtkTextWindow *win,
386 static void text_window_invalidate_rect (GtkTextWindow *win,
389 static gint text_window_get_width (GtkTextWindow *win);
390 static gint text_window_get_height (GtkTextWindow *win);
397 TARGET_COMPOUND_TEXT,
399 TARGET_TEXT_BUFFER_CONTENTS
402 static const GtkTargetEntry target_table[] = {
403 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
404 TARGET_TEXT_BUFFER_CONTENTS },
405 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
406 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
407 { "TEXT", 0, TARGET_TEXT },
408 { "STRING", 0, TARGET_STRING }
411 static GtkContainerClass *parent_class = NULL;
412 static guint signals[LAST_SIGNAL] = { 0 };
415 gtk_text_view_get_type (void)
417 static GType our_type = 0;
421 static const GTypeInfo our_info =
423 sizeof (GtkTextViewClass),
424 NULL, /* base_init */
425 NULL, /* base_finalize */
426 (GClassInitFunc) gtk_text_view_class_init,
427 NULL, /* class_finalize */
428 NULL, /* class_data */
429 sizeof (GtkTextView),
431 (GInstanceInitFunc) gtk_text_view_init,
434 our_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkTextView",
442 add_move_binding (GtkBindingSet *binding_set,
445 GtkMovementStep step,
448 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
450 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
454 G_TYPE_BOOLEAN, FALSE);
456 /* Selection-extending version */
457 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
461 G_TYPE_BOOLEAN, TRUE);
465 gtk_text_view_class_init (GtkTextViewClass *klass)
467 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
468 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
469 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
470 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
471 GtkBindingSet *binding_set;
473 parent_class = g_type_class_peek_parent (klass);
475 /* Default handlers and virtual methods
477 gobject_class->set_property = gtk_text_view_set_property;
478 gobject_class->get_property = gtk_text_view_get_property;
480 object_class->destroy = gtk_text_view_destroy;
481 gobject_class->finalize = gtk_text_view_finalize;
483 widget_class->realize = gtk_text_view_realize;
484 widget_class->unrealize = gtk_text_view_unrealize;
485 widget_class->style_set = gtk_text_view_style_set;
486 widget_class->direction_changed = gtk_text_view_direction_changed;
487 widget_class->size_request = gtk_text_view_size_request;
488 widget_class->size_allocate = gtk_text_view_size_allocate;
489 widget_class->event = gtk_text_view_event;
490 widget_class->key_press_event = gtk_text_view_key_press_event;
491 widget_class->key_release_event = gtk_text_view_key_release_event;
492 widget_class->button_press_event = gtk_text_view_button_press_event;
493 widget_class->button_release_event = gtk_text_view_button_release_event;
494 widget_class->focus_in_event = gtk_text_view_focus_in_event;
495 widget_class->focus_out_event = gtk_text_view_focus_out_event;
496 widget_class->motion_notify_event = gtk_text_view_motion_event;
497 widget_class->expose_event = gtk_text_view_expose_event;
498 widget_class->grab_focus = gtk_text_view_grab_focus;
499 widget_class->focus = gtk_text_view_focus;
501 widget_class->drag_begin = gtk_text_view_drag_begin;
502 widget_class->drag_end = gtk_text_view_drag_end;
503 widget_class->drag_data_get = gtk_text_view_drag_data_get;
504 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
506 widget_class->drag_leave = gtk_text_view_drag_leave;
507 widget_class->drag_motion = gtk_text_view_drag_motion;
508 widget_class->drag_drop = gtk_text_view_drag_drop;
509 widget_class->drag_data_received = gtk_text_view_drag_data_received;
511 widget_class->popup_menu = gtk_text_view_popup_menu;
513 container_class->add = gtk_text_view_add;
514 container_class->remove = gtk_text_view_remove;
515 container_class->forall = gtk_text_view_forall;
517 klass->move_cursor = gtk_text_view_move_cursor;
518 klass->page_horizontally = gtk_text_view_page_horizontally;
519 klass->set_anchor = gtk_text_view_set_anchor;
520 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
521 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
522 klass->cut_clipboard = gtk_text_view_cut_clipboard;
523 klass->copy_clipboard = gtk_text_view_copy_clipboard;
524 klass->paste_clipboard = gtk_text_view_paste_clipboard;
525 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
526 klass->move_focus = gtk_text_view_move_focus;
527 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
533 g_object_class_install_property (gobject_class,
534 PROP_PIXELS_ABOVE_LINES,
535 g_param_spec_int ("pixels_above_lines",
536 _("Pixels Above Lines"),
537 _("Pixels of blank space above paragraphs"),
543 g_object_class_install_property (gobject_class,
544 PROP_PIXELS_BELOW_LINES,
545 g_param_spec_int ("pixels_below_lines",
546 _("Pixels Below Lines"),
547 _("Pixels of blank space below paragraphs"),
553 g_object_class_install_property (gobject_class,
554 PROP_PIXELS_INSIDE_WRAP,
555 g_param_spec_int ("pixels_inside_wrap",
556 _("Pixels Inside Wrap"),
557 _("Pixels of blank space between wrapped lines in a paragraph"),
563 g_object_class_install_property (gobject_class,
565 g_param_spec_boolean ("editable",
567 _("Whether the text can be modified by the user"),
571 g_object_class_install_property (gobject_class,
573 g_param_spec_enum ("wrap_mode",
575 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
580 g_object_class_install_property (gobject_class,
582 g_param_spec_enum ("justification",
584 _("Left, right, or center justification"),
585 GTK_TYPE_JUSTIFICATION,
589 g_object_class_install_property (gobject_class,
591 g_param_spec_int ("left_margin",
593 _("Width of the left margin in pixels"),
599 g_object_class_install_property (gobject_class,
601 g_param_spec_int ("right_margin",
603 _("Width of the right margin in pixels"),
609 g_object_class_install_property (gobject_class,
611 g_param_spec_int ("indent",
613 _("Amount to indent the paragraph, in pixels"),
619 g_object_class_install_property (gobject_class,
621 g_param_spec_boxed ("tabs",
623 _("Custom tabs for this text"),
624 PANGO_TYPE_TAB_ARRAY,
627 g_object_class_install_property (gobject_class,
629 g_param_spec_boolean ("cursor_visible",
631 _("If the insertion cursor is shown"),
640 signals[MOVE_CURSOR] =
641 g_signal_new ("move_cursor",
642 G_OBJECT_CLASS_TYPE (gobject_class),
643 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
644 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
646 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
648 GTK_TYPE_MOVEMENT_STEP,
652 signals[PAGE_HORIZONTALLY] =
653 g_signal_new ("page_horizontally",
654 G_OBJECT_CLASS_TYPE (gobject_class),
655 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
656 G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
658 _gtk_marshal_VOID__INT_BOOLEAN,
663 signals[SET_ANCHOR] =
664 g_signal_new ("set_anchor",
665 G_OBJECT_CLASS_TYPE (gobject_class),
666 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
667 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
669 _gtk_marshal_VOID__VOID,
672 signals[INSERT_AT_CURSOR] =
673 g_signal_new ("insert_at_cursor",
674 G_OBJECT_CLASS_TYPE (gobject_class),
675 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
676 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
678 _gtk_marshal_VOID__STRING,
682 signals[DELETE_FROM_CURSOR] =
683 g_signal_new ("delete_from_cursor",
684 G_OBJECT_CLASS_TYPE (gobject_class),
685 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
686 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
688 _gtk_marshal_VOID__ENUM_INT,
690 GTK_TYPE_DELETE_TYPE,
693 signals[CUT_CLIPBOARD] =
694 g_signal_new ("cut_clipboard",
695 G_OBJECT_CLASS_TYPE (gobject_class),
696 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
697 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
699 _gtk_marshal_VOID__VOID,
702 signals[COPY_CLIPBOARD] =
703 g_signal_new ("copy_clipboard",
704 G_OBJECT_CLASS_TYPE (gobject_class),
705 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
706 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
708 _gtk_marshal_VOID__VOID,
711 signals[PASTE_CLIPBOARD] =
712 g_signal_new ("paste_clipboard",
713 G_OBJECT_CLASS_TYPE (gobject_class),
714 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
715 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
717 _gtk_marshal_VOID__VOID,
720 signals[TOGGLE_OVERWRITE] =
721 g_signal_new ("toggle_overwrite",
722 G_OBJECT_CLASS_TYPE (gobject_class),
723 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
724 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
726 _gtk_marshal_VOID__VOID,
729 signals[MOVE_FOCUS] =
730 g_signal_new ("move_focus",
731 G_OBJECT_CLASS_TYPE (gobject_class),
732 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
733 G_STRUCT_OFFSET (GtkTextViewClass, move_focus),
735 _gtk_marshal_VOID__ENUM,
737 GTK_TYPE_DIRECTION_TYPE);
739 signals[SET_SCROLL_ADJUSTMENTS] =
740 g_signal_new ("set_scroll_adjustments",
741 G_OBJECT_CLASS_TYPE (gobject_class),
742 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
743 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
745 _gtk_marshal_VOID__OBJECT_OBJECT,
748 GTK_TYPE_ADJUSTMENT);
749 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
751 signals[POPULATE_POPUP] =
752 g_signal_new ("populate_popup",
753 G_OBJECT_CLASS_TYPE (gobject_class),
755 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
757 _gtk_marshal_VOID__OBJECT,
765 binding_set = gtk_binding_set_by_class (klass);
767 /* Moving the insertion point */
768 add_move_binding (binding_set, GDK_Right, 0,
769 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
771 add_move_binding (binding_set, GDK_KP_Right, 0,
772 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
774 add_move_binding (binding_set, GDK_Left, 0,
775 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
777 add_move_binding (binding_set, GDK_KP_Left, 0,
778 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
780 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
781 GTK_MOVEMENT_WORDS, 1);
783 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
784 GTK_MOVEMENT_WORDS, 1);
786 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
787 GTK_MOVEMENT_WORDS, -1);
789 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
790 GTK_MOVEMENT_WORDS, 1);
792 add_move_binding (binding_set, GDK_Up, 0,
793 GTK_MOVEMENT_DISPLAY_LINES, -1);
795 add_move_binding (binding_set, GDK_KP_Up, 0,
796 GTK_MOVEMENT_DISPLAY_LINES, -1);
798 add_move_binding (binding_set, GDK_Down, 0,
799 GTK_MOVEMENT_DISPLAY_LINES, 1);
801 add_move_binding (binding_set, GDK_KP_Down, 0,
802 GTK_MOVEMENT_DISPLAY_LINES, 1);
804 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
805 GTK_MOVEMENT_PARAGRAPHS, -1);
807 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
808 GTK_MOVEMENT_PARAGRAPHS, -1);
810 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
811 GTK_MOVEMENT_PARAGRAPHS, 1);
813 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
814 GTK_MOVEMENT_PARAGRAPHS, 1);
816 add_move_binding (binding_set, GDK_Home, 0,
817 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
819 add_move_binding (binding_set, GDK_KP_Home, 0,
820 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
822 add_move_binding (binding_set, GDK_End, 0,
823 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
825 add_move_binding (binding_set, GDK_KP_End, 0,
826 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
828 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
829 GTK_MOVEMENT_BUFFER_ENDS, -1);
831 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
832 GTK_MOVEMENT_BUFFER_ENDS, -1);
834 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
835 GTK_MOVEMENT_BUFFER_ENDS, 1);
837 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
838 GTK_MOVEMENT_BUFFER_ENDS, 1);
840 add_move_binding (binding_set, GDK_Page_Up, 0,
841 GTK_MOVEMENT_PAGES, -1);
843 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
844 GTK_MOVEMENT_PAGES, -1);
846 add_move_binding (binding_set, GDK_Page_Down, 0,
847 GTK_MOVEMENT_PAGES, 1);
849 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
850 GTK_MOVEMENT_PAGES, 1);
854 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
856 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
858 G_TYPE_BOOLEAN, FALSE);
859 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
861 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
863 G_TYPE_BOOLEAN, TRUE);
866 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
867 "page_horizontally", 2,
869 G_TYPE_BOOLEAN, FALSE);
871 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
872 "page_horizontally", 2,
874 G_TYPE_BOOLEAN, TRUE);
876 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
877 "page_horizontally", 2,
879 G_TYPE_BOOLEAN, FALSE);
881 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
882 "page_horizontally", 2,
884 G_TYPE_BOOLEAN, TRUE);
886 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
887 "page_horizontally", 2,
889 G_TYPE_BOOLEAN, FALSE);
891 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
892 "page_horizontally", 2,
894 G_TYPE_BOOLEAN, TRUE);
896 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
897 "page_horizontally", 2,
899 G_TYPE_BOOLEAN, FALSE);
901 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
902 "page_horizontally", 2,
904 G_TYPE_BOOLEAN, TRUE);
907 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
908 "delete_from_cursor", 2,
909 G_TYPE_ENUM, GTK_DELETE_CHARS,
912 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
913 "delete_from_cursor", 2,
914 G_TYPE_ENUM, GTK_DELETE_CHARS,
917 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
918 "delete_from_cursor", 2,
919 G_TYPE_ENUM, GTK_DELETE_CHARS,
922 /* Make this do the same as Backspace, to help with mis-typing */
923 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
924 "delete_from_cursor", 2,
925 G_TYPE_ENUM, GTK_DELETE_CHARS,
928 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
929 "delete_from_cursor", 2,
930 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
933 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
934 "delete_from_cursor", 2,
935 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
938 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
939 "delete_from_cursor", 2,
940 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
945 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
947 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
948 "copy_clipboard", 0);
949 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
950 "paste_clipboard", 0);
952 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
954 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
955 "copy_clipboard", 0);
956 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
957 "paste_clipboard", 0);
960 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
961 "toggle_overwrite", 0);
962 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
963 "toggle_overwrite", 0);
965 /* Control-tab focus motion */
966 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
968 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
969 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
971 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
973 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
975 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
976 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
978 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
982 gtk_text_view_init (GtkTextView *text_view)
986 widget = GTK_WIDGET (text_view);
988 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
990 /* Set up default style */
991 text_view->wrap_mode = GTK_WRAP_NONE;
992 text_view->pixels_above_lines = 0;
993 text_view->pixels_below_lines = 0;
994 text_view->pixels_inside_wrap = 0;
995 text_view->justify = GTK_JUSTIFY_LEFT;
996 text_view->left_margin = 0;
997 text_view->right_margin = 0;
998 text_view->indent = 0;
999 text_view->tabs = NULL;
1000 text_view->editable = TRUE;
1002 gtk_drag_dest_set (widget,
1004 target_table, G_N_ELEMENTS (target_table),
1005 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1007 text_view->virtual_cursor_x = -1;
1008 text_view->virtual_cursor_y = -1;
1010 /* This object is completely private. No external entity can gain a reference
1011 * to it; so we create it here and destroy it in finalize ().
1013 text_view->im_context = gtk_im_multicontext_new ();
1015 g_signal_connect (text_view->im_context, "commit",
1016 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1017 g_signal_connect (text_view->im_context, "preedit_changed",
1018 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1019 g_signal_connect (text_view->im_context, "retrieve_surrounding",
1020 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1021 g_signal_connect (text_view->im_context, "delete_surrounding",
1022 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1024 text_view->cursor_visible = TRUE;
1026 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1029 text_view->drag_start_x = -1;
1030 text_view->drag_start_y = -1;
1032 text_view->pending_place_cursor_button = 0;
1034 /* We handle all our own redrawing */
1035 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1039 * gtk_text_view_new:
1041 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1042 * before using the text view, an empty default buffer will be created
1043 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1044 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1046 * Return value: a new #GtkTextView
1049 gtk_text_view_new (void)
1051 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1055 * gtk_text_view_new_with_buffer:
1056 * @buffer: a #GtkTextBuffer
1058 * Creates a new #GtkTextView widget displaying the buffer
1059 * @buffer. One buffer can be shared among many widgets.
1060 * @buffer may be NULL to create a default buffer, in which case
1061 * this function is equivalent to gtk_text_view_new(). The
1062 * text view adds its own reference count to the buffer; it does not
1063 * take over an existing reference.
1065 * Return value: a new #GtkTextView.
1068 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1070 GtkTextView *text_view;
1072 text_view = (GtkTextView*)gtk_text_view_new ();
1074 gtk_text_view_set_buffer (text_view, buffer);
1076 return GTK_WIDGET (text_view);
1080 * gtk_text_view_set_buffer:
1081 * @text_view: a #GtkTextView
1082 * @buffer: a #GtkTextBuffer
1084 * Sets @buffer as the buffer being displayed by @text_view. The previous
1085 * buffer displayed by the text view is unreferenced, and a reference is
1086 * added to @buffer. If you owned a reference to @buffer before passing it
1087 * to this function, you must remove that reference yourself; #GtkTextView
1088 * will not "adopt" it.
1092 gtk_text_view_set_buffer (GtkTextView *text_view,
1093 GtkTextBuffer *buffer)
1095 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1096 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1098 if (text_view->buffer == buffer)
1101 if (text_view->buffer != NULL)
1103 /* Destroy all anchored children */
1107 copy = g_slist_copy (text_view->children);
1109 while (tmp_list != NULL)
1111 GtkTextViewChild *vc = tmp_list->data;
1115 gtk_widget_destroy (vc->widget);
1116 /* vc may now be invalid! */
1119 tmp_list = g_slist_next (tmp_list);
1122 g_slist_free (copy);
1124 g_signal_handlers_disconnect_by_func (text_view->buffer,
1125 gtk_text_view_mark_set_handler,
1127 g_object_unref (text_view->buffer);
1128 text_view->dnd_mark = NULL;
1130 if (GTK_WIDGET_REALIZED (text_view))
1132 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1133 GDK_SELECTION_PRIMARY);
1134 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1138 text_view->buffer = buffer;
1144 g_object_ref (buffer);
1146 if (text_view->layout)
1147 gtk_text_layout_set_buffer (text_view->layout, buffer);
1149 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1151 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1155 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1159 text_view->first_para_pixels = 0;
1161 g_signal_connect (text_view->buffer, "mark_set",
1162 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1164 if (GTK_WIDGET_REALIZED (text_view))
1166 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1167 GDK_SELECTION_PRIMARY);
1168 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1172 if (GTK_WIDGET_VISIBLE (text_view))
1173 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1175 DV(g_print ("Invalidating due to set_buffer\n"));
1176 gtk_text_view_invalidate (text_view);
1179 static GtkTextBuffer*
1180 get_buffer (GtkTextView *text_view)
1182 if (text_view->buffer == NULL)
1185 b = gtk_text_buffer_new (NULL);
1186 gtk_text_view_set_buffer (text_view, b);
1190 return text_view->buffer;
1194 * gtk_text_view_get_buffer:
1195 * @text_view: a #GtkTextView
1197 * Returns the #GtkTextBuffer being displayed by this text view.
1198 * The reference count on the buffer is not incremented; the caller
1199 * of this function won't own a new reference.
1201 * Return value: a #GtkTextBuffer
1204 gtk_text_view_get_buffer (GtkTextView *text_view)
1206 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1208 return get_buffer (text_view);
1212 * gtk_text_view_get_iter_at_location:
1213 * @text_view: a #GtkTextView
1214 * @iter: a #GtkTextIter
1215 * @x: x position, in buffer coordinates
1216 * @y: y position, in buffer coordinates
1218 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1219 * coordinates are coordinates for the entire buffer, not just the
1220 * currently-displayed portion. If you have coordinates from an
1221 * event, you have to convert those to buffer coordinates with
1222 * gtk_text_view_window_to_buffer_coords().
1226 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1231 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1232 g_return_if_fail (iter != NULL);
1234 gtk_text_view_ensure_layout (text_view);
1236 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1243 * gtk_text_view_get_iter_location:
1244 * @text_view: a #GtkTextView
1245 * @iter: a #GtkTextIter
1246 * @location: bounds of the character at @iter
1248 * Gets a rectangle which roughly contains the character at @iter.
1249 * The rectangle position is in buffer coordinates; use
1250 * gtk_text_view_buffer_to_window_coords() to convert these
1251 * coordinates to coordinates for one of the windows in the text view.
1255 gtk_text_view_get_iter_location (GtkTextView *text_view,
1256 const GtkTextIter *iter,
1257 GdkRectangle *location)
1259 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1260 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1262 gtk_text_view_ensure_layout (text_view);
1264 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1268 * gtk_text_view_get_line_yrange:
1269 * @text_view: a #GtkTextView
1270 * @iter: a #GtkTextIter
1271 * @y: return location for a y coordinate
1272 * @height: return location for a height
1274 * Gets the y coordinate of the top of the line containing @iter,
1275 * and the height of the line. The coordinate is a buffer coordinate;
1276 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1280 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1281 const GtkTextIter *iter,
1285 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1286 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1288 gtk_text_view_ensure_layout (text_view);
1290 gtk_text_layout_get_line_yrange (text_view->layout,
1297 * gtk_text_view_get_line_at_y:
1298 * @text_view: a #GtkTextView
1299 * @target_iter: a #GtkTextIter
1300 * @y: a y coordinate
1301 * @line_top: return location for top coordinate of the line
1303 * Gets the #GtkTextIter at the start of the line containing
1304 * the coordinate @y. @y is in buffer coordinates, convert from
1305 * window coordinates with gtk_text_view_window_to_buffer_coords().
1306 * If non-%NULL, @line_top will be filled with the coordinate of the top
1310 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1311 GtkTextIter *target_iter,
1315 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1317 gtk_text_view_ensure_layout (text_view);
1319 gtk_text_layout_get_line_at_y (text_view->layout,
1326 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1328 DV (g_print (" Setting adj to raw value %g\n", val));
1330 /* We don't really want to clamp to upper; we want to clamp to
1331 upper - page_size which is the highest value the scrollbar
1332 will let us reach. */
1333 if (val > (adj->upper - adj->page_size))
1334 val = adj->upper - adj->page_size;
1336 if (val < adj->lower)
1339 if (val != adj->value)
1341 DV (g_print (" Setting adj to clamped value %g\n", val));
1342 gtk_adjustment_set_value (adj, val);
1350 * gtk_text_view_scroll_to_iter:
1351 * @text_view: a #GtkTextView
1352 * @iter: a #GtkTextIter
1353 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1354 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1355 * @xalign: horizontal alignment of mark within visible area.
1356 * @yalign: vertical alignment of mark within visible area
1358 * Scrolls @text_view so that @iter is on the screen in the position
1359 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1360 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1361 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1362 * possibly not scrolling at all. The effective screen for purposes
1363 * of this function is reduced by a margin of size @within_margin.
1364 * NOTE: This function uses the currently-computed height of the
1365 * lines in the text buffer. Note that line heights are computed
1366 * in an idle handler; so this function may not have the desired effect
1367 * if it's called before the height computations. To avoid oddness,
1368 * consider using gtk_text_view_scroll_to_mark() which saves a point
1369 * to be scrolled to after line validation.
1371 * Return value: %TRUE if scrolling occurred
1374 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1376 gdouble within_margin,
1382 GdkRectangle screen;
1387 gboolean retval = FALSE;
1389 gint screen_xoffset, screen_yoffset;
1390 gint current_x_scroll, current_y_scroll;
1392 /* FIXME why don't we do the validate-at-scroll-destination thing
1393 * from flush_scroll in this function? I think it wasn't done before
1394 * because changed_handler was screwed up, but I could be wrong.
1397 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1398 g_return_val_if_fail (iter != NULL, FALSE);
1399 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1400 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1401 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1403 widget = GTK_WIDGET (text_view);
1405 DV(g_print(G_STRLOC"\n"));
1407 gtk_text_layout_get_iter_location (text_view->layout,
1411 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1413 current_x_scroll = text_view->xoffset;
1414 current_y_scroll = text_view->yoffset;
1416 screen.x = current_x_scroll;
1417 screen.y = current_y_scroll;
1418 screen.width = SCREEN_WIDTH (widget);
1419 screen.height = SCREEN_HEIGHT (widget);
1421 screen_xoffset = screen.width * within_margin;
1422 screen_yoffset = screen.height * within_margin;
1424 screen.x += screen_xoffset;
1425 screen.y += screen_yoffset;
1426 screen.width -= screen_xoffset * 2;
1427 screen.height -= screen_yoffset * 2;
1429 /* paranoia check */
1430 if (screen.width < 1)
1432 if (screen.height < 1)
1435 screen_right = screen.x + screen.width;
1436 screen_bottom = screen.y + screen.height;
1438 /* The alignment affects the point in the target character that we
1439 * choose to align. If we're doing right/bottom alignment, we align
1440 * the right/bottom edge of the character the mark is at; if we're
1441 * doing left/top we align the left/top edge of the character; if
1442 * we're doing center alignment we align the center of the
1446 /* Vertical scroll */
1449 scroll_dest = current_y_scroll;
1453 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1455 /* if scroll_dest < screen.y, we move a negative increment (up),
1456 * else a positive increment (down)
1458 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1462 /* move minimum to get onscreen */
1463 if (rect.y < screen.y)
1465 scroll_dest = rect.y;
1466 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1468 else if ((rect.y + rect.height) > screen_bottom)
1470 scroll_dest = rect.y + rect.height;
1471 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1475 if (scroll_inc != 0)
1477 retval = set_adjustment_clamped (get_vadjustment (text_view),
1478 current_y_scroll + scroll_inc);
1480 DV (g_print (" vert increment %d\n", scroll_inc));
1483 /* Horizontal scroll */
1486 scroll_dest = current_x_scroll;
1490 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1492 /* if scroll_dest < screen.y, we move a negative increment (left),
1493 * else a positive increment (right)
1495 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1499 /* move minimum to get onscreen */
1500 if (rect.x < screen.x)
1502 scroll_dest = rect.x;
1503 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1505 else if ((rect.x + rect.width) > screen_right)
1507 scroll_dest = rect.x + rect.width;
1508 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1512 if (scroll_inc != 0)
1514 retval = set_adjustment_clamped (get_hadjustment (text_view),
1515 current_x_scroll + scroll_inc);
1517 DV (g_print (" horiz increment %d\n", scroll_inc));
1521 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1523 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1529 free_pending_scroll (GtkTextPendingScroll *scroll)
1531 if (!gtk_text_mark_get_deleted (scroll->mark))
1532 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1534 g_object_unref (scroll->mark);
1539 cancel_pending_scroll (GtkTextView *text_view)
1541 if (text_view->pending_scroll)
1543 free_pending_scroll (text_view->pending_scroll);
1544 text_view->pending_scroll = NULL;
1549 gtk_text_view_queue_scroll (GtkTextView *text_view,
1551 gdouble within_margin,
1557 GtkTextPendingScroll *scroll;
1559 DV(g_print(G_STRLOC"\n"));
1561 scroll = g_new (GtkTextPendingScroll, 1);
1563 scroll->within_margin = within_margin;
1564 scroll->use_align = use_align;
1565 scroll->xalign = xalign;
1566 scroll->yalign = yalign;
1568 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1570 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1573 gtk_text_mark_get_left_gravity (mark));
1575 g_object_ref (scroll->mark);
1577 cancel_pending_scroll (text_view);
1579 text_view->pending_scroll = scroll;
1583 gtk_text_view_flush_scroll (GtkTextView *text_view)
1586 GtkTextPendingScroll *scroll;
1590 widget = GTK_WIDGET (text_view);
1592 DV(g_print(G_STRLOC"\n"));
1594 if (text_view->pending_scroll == NULL)
1596 DV (g_print ("in flush scroll, no pending scroll\n"));
1600 scroll = text_view->pending_scroll;
1602 /* avoid recursion */
1603 text_view->pending_scroll = NULL;
1605 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1607 /* Validate area around the scroll destination, so the adjustment
1608 * can meaningfully point into that area. We must validate
1609 * enough area to be sure that after we scroll, everything onscreen
1610 * is valid; otherwise, validation will maintain the first para
1611 * in one place, but may push the target iter off the bottom of
1614 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1615 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1616 - (widget->allocation.height * 2),
1617 widget->allocation.height * 2);
1619 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1621 /* Ensure we have updated width/height */
1622 gtk_text_view_update_adjustments (text_view);
1624 retval = gtk_text_view_scroll_to_iter (text_view,
1626 scroll->within_margin,
1631 free_pending_scroll (scroll);
1637 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1639 if (upper != adj->upper)
1641 gdouble min = MAX (0.0, upper - adj->page_size);
1642 gboolean value_changed = FALSE;
1646 if (adj->value > min)
1649 value_changed = TRUE;
1652 gtk_adjustment_changed (adj);
1653 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1657 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1658 gtk_adjustment_value_changed (adj);
1664 gtk_text_view_update_adjustments (GtkTextView *text_view)
1666 gint width = 0, height = 0;
1668 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1670 if (text_view->layout)
1671 gtk_text_layout_get_size (text_view->layout, &width, &height);
1673 if (text_view->width != width || text_view->height != height)
1675 text_view->width = width;
1676 text_view->height = height;
1678 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1679 MAX (SCREEN_WIDTH (text_view), width));
1680 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1681 MAX (SCREEN_HEIGHT (text_view), height));
1683 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1685 /* Set up the step sizes; we'll say that a page is
1686 our allocation minus one step, and a step is
1687 1/10 of our allocation. */
1688 text_view->hadjustment->step_increment =
1689 SCREEN_WIDTH (text_view) / 10.0;
1690 text_view->hadjustment->page_increment =
1691 SCREEN_WIDTH (text_view) * 0.9;
1693 text_view->vadjustment->step_increment =
1694 SCREEN_HEIGHT (text_view) / 10.0;
1695 text_view->vadjustment->page_increment =
1696 SCREEN_HEIGHT (text_view) * 0.9;
1698 gtk_adjustment_changed (get_hadjustment (text_view));
1699 gtk_adjustment_changed (get_vadjustment (text_view));
1704 gtk_text_view_update_layout_width (GtkTextView *text_view)
1706 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1708 gtk_text_view_ensure_layout (text_view);
1710 gtk_text_layout_set_screen_width (text_view->layout,
1711 SCREEN_WIDTH (text_view));
1715 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1718 gint cursor_x_pos, cursor_y_pos;
1720 if (text_view->layout == NULL)
1723 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1725 area.x = cursor_x_pos;
1726 area.y = cursor_y_pos;
1727 area.width = area.height = 0;
1729 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1733 * gtk_text_view_scroll_to_mark:
1734 * @text_view: a #GtkTextView
1735 * @mark: a #GtkTextMark
1736 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1737 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1738 * @xalign: horizontal alignment of mark within visible area.
1739 * @yalign: vertical alignment of mark within visible area
1741 * Scrolls @text_view so that @mark is on the screen in the position
1742 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1743 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1744 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1745 * possibly not scrolling at all. The effective screen for purposes
1746 * of this function is reduced by a margin of size @within_margin.
1750 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1752 gdouble within_margin,
1757 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1758 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1759 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1760 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1761 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1763 gtk_text_view_queue_scroll (text_view, mark,
1769 /* If no validation is pending, we need to go ahead and force an
1772 if (text_view->layout &&
1773 gtk_text_layout_is_valid (text_view->layout))
1774 gtk_text_view_flush_scroll (text_view);
1778 * gtk_text_view_scroll_mark_onscreen:
1779 * @text_view: a #GtkTextView
1780 * @mark: a mark in the buffer for @text_view
1782 * Scrolls @text_view the minimum distance such that @mark is contained
1783 * within the visible area of the widget.
1787 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1790 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1791 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1793 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1797 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1799 GdkRectangle visible_rect;
1800 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1802 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1804 visible_rect.y + visible_rect.height);
1808 * gtk_text_view_move_mark_onscreen:
1809 * @text_view: a #GtkTextView
1810 * @mark: a #GtkTextMark
1812 * Moves a mark within the buffer so that it's
1813 * located within the currently-visible text area.
1815 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1818 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1823 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1824 g_return_val_if_fail (mark != NULL, FALSE);
1826 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1828 if (clamp_iter_onscreen (text_view, &iter))
1830 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1838 * gtk_text_view_get_visible_rect:
1839 * @text_view: a #GtkTextView
1840 * @visible_rect: rectangle to fill
1842 * Fills @visible_rect with the currently-visible
1843 * region of the buffer, in buffer coordinates. Convert to window coordinates
1844 * with gtk_text_view_buffer_to_window_coords().
1847 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1848 GdkRectangle *visible_rect)
1852 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1854 widget = GTK_WIDGET (text_view);
1858 visible_rect->x = text_view->xoffset;
1859 visible_rect->y = text_view->yoffset;
1860 visible_rect->width = SCREEN_WIDTH (widget);
1861 visible_rect->height = SCREEN_HEIGHT (widget);
1863 DV(g_print(" visible rect: %d,%d %d x %d\n",
1866 visible_rect->width,
1867 visible_rect->height));
1872 * gtk_text_view_set_wrap_mode:
1873 * @text_view: a #GtkTextView
1874 * @wrap_mode: a #GtkWrapMode
1876 * Sets the line wrapping for the view.
1879 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1880 GtkWrapMode wrap_mode)
1882 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1884 if (text_view->wrap_mode != wrap_mode)
1886 text_view->wrap_mode = wrap_mode;
1888 if (text_view->layout)
1890 text_view->layout->default_style->wrap_mode = wrap_mode;
1891 gtk_text_layout_default_style_changed (text_view->layout);
1895 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1899 * gtk_text_view_get_wrap_mode:
1900 * @text_view: a #GtkTextView
1902 * Gets the line wrapping for the view.
1904 * Return value: the line wrap setting
1907 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1909 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1911 return text_view->wrap_mode;
1915 * gtk_text_view_set_editable:
1916 * @text_view: a #GtkTextView
1917 * @setting: whether it's editable
1919 * Sets the default editability of the #GtkTextView. You can override
1920 * this default setting with tags in the buffer, using the "editable"
1921 * attribute of tags.
1924 gtk_text_view_set_editable (GtkTextView *text_view,
1927 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1928 setting = setting != FALSE;
1930 if (text_view->editable != setting)
1932 text_view->editable = setting;
1934 if (text_view->layout)
1936 text_view->layout->default_style->editable = text_view->editable;
1937 gtk_text_layout_default_style_changed (text_view->layout);
1940 g_object_notify (G_OBJECT (text_view), "editable");
1945 * gtk_text_view_get_editable:
1946 * @text_view: a #GtkTextView
1948 * Returns the default editability of the #GtkTextView. Tags in the
1949 * buffer may override this setting for some ranges of text.
1951 * Return value: whether text is editable by default
1954 gtk_text_view_get_editable (GtkTextView *text_view)
1956 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1958 return text_view->editable;
1962 * gtk_text_view_set_pixels_above_lines:
1963 * @text_view: a #GtkTextView
1964 * @pixels_above_lines: pixels above paragraphs
1966 * Sets the default number of blank pixels above paragraphs in @text_view.
1967 * Tags in the buffer for @text_view may override the defaults.
1971 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1972 gint pixels_above_lines)
1974 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1976 if (text_view->pixels_above_lines != pixels_above_lines)
1978 text_view->pixels_above_lines = pixels_above_lines;
1980 if (text_view->layout)
1982 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1983 gtk_text_layout_default_style_changed (text_view->layout);
1986 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1991 * gtk_text_view_get_pixels_above_lines:
1992 * @text_view: a #GtkTextView
1994 * Gets the default number of pixels to put above paragraphs.
1996 * Return value: default number of pixels above paragraphs
1999 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2001 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2003 return text_view->pixels_above_lines;
2007 * gtk_text_view_set_pixels_below_lines:
2008 * @text_view: a #GtkTextView
2009 * @pixels_below_lines: pixels below paragraphs
2011 * Sets the default number of pixels of blank space
2012 * to put below paragraphs in @text_view. May be overridden
2013 * by tags applied to @text_view's buffer.
2017 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2018 gint pixels_below_lines)
2020 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2022 if (text_view->pixels_below_lines != pixels_below_lines)
2024 text_view->pixels_below_lines = pixels_below_lines;
2026 if (text_view->layout)
2028 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2029 gtk_text_layout_default_style_changed (text_view->layout);
2032 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
2037 * gtk_text_view_get_pixels_below_lines:
2038 * @text_view: a #GtkTextView
2040 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2042 * Return value: default number of blank pixels below paragraphs
2045 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2047 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2049 return text_view->pixels_below_lines;
2053 * gtk_text_view_set_pixels_inside_wrap:
2054 * @text_view: a #GtkTextView
2055 * @pixels_inside_wrap: default number of pixels between wrapped lines
2057 * Sets the default number of pixels of blank space to leave between
2058 * display/wrapped lines within a paragraph. May be overridden by
2059 * tags in @text_view's buffer.
2063 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2064 gint pixels_inside_wrap)
2066 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2068 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2070 text_view->pixels_inside_wrap = pixels_inside_wrap;
2072 if (text_view->layout)
2074 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2075 gtk_text_layout_default_style_changed (text_view->layout);
2078 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2083 * gtk_text_view_get_pixels_inside_wrap:
2084 * @text_view: a #GtkTextView
2086 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2088 * Return value: default number of pixels of blank space between wrapped lines
2091 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2093 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2095 return text_view->pixels_inside_wrap;
2099 * gtk_text_view_set_justification:
2100 * @text_view: a #GtkTextView
2101 * @justification: justification
2103 * Sets the default justification of text in @text_view.
2104 * Tags in the view's buffer may override the default.
2108 gtk_text_view_set_justification (GtkTextView *text_view,
2109 GtkJustification justification)
2111 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2113 if (text_view->justify != justification)
2115 text_view->justify = justification;
2117 if (text_view->layout)
2119 text_view->layout->default_style->justification = justification;
2120 gtk_text_layout_default_style_changed (text_view->layout);
2123 g_object_notify (G_OBJECT (text_view), "justification");
2128 * gtk_text_view_get_justification:
2129 * @text_view: a #GtkTextView
2131 * Gets the default justification of paragraphs in @text_view.
2132 * Tags in the buffer may override the default.
2134 * Return value: default justification
2137 gtk_text_view_get_justification (GtkTextView *text_view)
2139 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2141 return text_view->justify;
2145 * gtk_text_view_set_left_margin:
2146 * @text_view: a #GtkTextView
2147 * @left_margin: left margin in pixels
2149 * Sets the default left margin for text in @text_view.
2150 * Tags in the buffer may override the default.
2154 gtk_text_view_set_left_margin (GtkTextView *text_view,
2157 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2159 if (text_view->left_margin != left_margin)
2161 text_view->left_margin = left_margin;
2163 if (text_view->layout)
2165 text_view->layout->default_style->left_margin = left_margin;
2166 gtk_text_layout_default_style_changed (text_view->layout);
2169 g_object_notify (G_OBJECT (text_view), "left_margin");
2174 * gtk_text_view_get_left_margin:
2175 * @text_view: a #GtkTextView
2177 * Gets the default left margin size of paragraphs in the @text_view.
2178 * Tags in the buffer may override the default.
2180 * Return value: left margin in pixels
2183 gtk_text_view_get_left_margin (GtkTextView *text_view)
2185 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2187 return text_view->left_margin;
2191 * gtk_text_view_set_right_margin:
2192 * @text_view: a #GtkTextView
2193 * @right_margin: right margin in pixels
2195 * Sets the default right margin for text in the text view.
2196 * Tags in the buffer may override the default.
2200 gtk_text_view_set_right_margin (GtkTextView *text_view,
2203 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2205 if (text_view->right_margin != right_margin)
2207 text_view->right_margin = right_margin;
2209 if (text_view->layout)
2211 text_view->layout->default_style->right_margin = right_margin;
2212 gtk_text_layout_default_style_changed (text_view->layout);
2215 g_object_notify (G_OBJECT (text_view), "right_margin");
2220 * gtk_text_view_get_right_margin:
2221 * @text_view: a #GtkTextView
2223 * Gets the default right margin for text in @text_view. Tags
2224 * in the buffer may override the default.
2226 * Return value: right margin in pixels
2229 gtk_text_view_get_right_margin (GtkTextView *text_view)
2231 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2233 return text_view->right_margin;
2237 * gtk_text_view_set_indent:
2238 * @text_view: a #GtkTextView
2239 * @indent: indentation in pixels
2241 * Sets the default indentation for paragraphs in @text_view.
2242 * Tags in the buffer may override the default.
2246 gtk_text_view_set_indent (GtkTextView *text_view,
2249 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2251 if (text_view->indent != indent)
2253 text_view->indent = indent;
2255 if (text_view->layout)
2257 text_view->layout->default_style->indent = indent;
2258 gtk_text_layout_default_style_changed (text_view->layout);
2261 g_object_notify (G_OBJECT (text_view), "indent");
2266 * gtk_text_view_get_indent:
2267 * @text_view: a #GtkTextView
2269 * Gets the default indentation of paragraphs in @text_view.
2270 * Tags in the view's buffer may override the default.
2271 * The indentation may be negative.
2273 * Return value: number of pixels of indentation
2276 gtk_text_view_get_indent (GtkTextView *text_view)
2278 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2280 return text_view->indent;
2284 * gtk_text_view_set_tabs:
2285 * @text_view: a #GtkTextView
2286 * @tabs: tabs as a #PangoTabArray
2288 * Sets the default tab stops for paragraphs in @text_view.
2289 * Tags in the buffer may override the default.
2293 gtk_text_view_set_tabs (GtkTextView *text_view,
2294 PangoTabArray *tabs)
2296 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2298 if (text_view->tabs)
2299 pango_tab_array_free (text_view->tabs);
2301 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2303 if (text_view->layout)
2305 /* some unkosher futzing in internal struct details... */
2306 if (text_view->layout->default_style->tabs)
2307 pango_tab_array_free (text_view->layout->default_style->tabs);
2309 text_view->layout->default_style->tabs =
2310 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2312 gtk_text_layout_default_style_changed (text_view->layout);
2315 g_object_notify (G_OBJECT (text_view), "tabs");
2319 * gtk_text_view_get_tabs:
2320 * @text_view: a #GtkTextView
2322 * Gets the default tabs for @text_view. Tags in the buffer may
2323 * override the defaults. The returned array will be %NULL if
2324 * "standard" (8-space) tabs are used. Free the return value
2325 * with pango_tab_array_free().
2327 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2330 gtk_text_view_get_tabs (GtkTextView *text_view)
2332 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2334 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2338 * gtk_text_view_set_cursor_visible:
2339 * @text_view: a #GtkTextView
2340 * @setting: whether to show the insertion cursor
2342 * Toggles whether the insertion point is displayed. A buffer with no editable
2343 * text probably shouldn't have a visible cursor, so you may want to turn
2347 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2350 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2352 setting = (setting != FALSE);
2354 if (text_view->cursor_visible != setting)
2356 text_view->cursor_visible = setting;
2358 if (GTK_WIDGET_HAS_FOCUS (text_view))
2360 if (text_view->layout)
2362 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2363 gtk_text_view_check_cursor_blink (text_view);
2367 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2372 * gtk_text_view_get_cursor_visible:
2373 * @text_view: a #GtkTextView
2375 * Find out whether the cursor is being displayed.
2377 * Return value: whether the insertion mark is visible
2380 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2382 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2384 return text_view->cursor_visible;
2389 * gtk_text_view_place_cursor_onscreen:
2390 * @text_view: a #GtkTextView
2392 * Moves the cursor to the currently visible region of the
2393 * buffer, it it isn't there already.
2395 * Return value: TRUE if the cursor had to be moved.
2398 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2402 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2404 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2405 gtk_text_buffer_get_mark (get_buffer (text_view),
2408 if (clamp_iter_onscreen (text_view, &insert))
2410 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2418 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2420 if (text_view->first_validate_idle != 0)
2422 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2423 g_source_remove (text_view->first_validate_idle);
2424 text_view->first_validate_idle = 0;
2427 if (text_view->incremental_validate_idle != 0)
2429 g_source_remove (text_view->incremental_validate_idle);
2430 text_view->incremental_validate_idle = 0;
2435 gtk_text_view_destroy (GtkObject *object)
2437 GtkTextView *text_view;
2438 GtkTextLayout *layout;
2440 text_view = GTK_TEXT_VIEW (object);
2442 layout = text_view->layout;
2444 gtk_text_view_remove_validate_idles (text_view);
2445 gtk_text_view_set_buffer (text_view, NULL);
2446 gtk_text_view_destroy_layout (text_view);
2448 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2452 gtk_text_view_finalize (GObject *object)
2454 GtkTextView *text_view;
2456 text_view = GTK_TEXT_VIEW (object);
2458 g_return_if_fail (text_view->buffer == NULL);
2460 gtk_text_view_destroy_layout (text_view);
2461 gtk_text_view_set_buffer (text_view, NULL);
2463 cancel_pending_scroll (text_view);
2465 if (text_view->tabs)
2466 pango_tab_array_free (text_view->tabs);
2468 if (text_view->hadjustment)
2469 g_object_unref (text_view->hadjustment);
2470 if (text_view->vadjustment)
2471 g_object_unref (text_view->vadjustment);
2473 text_window_free (text_view->text_window);
2475 if (text_view->left_window)
2476 text_window_free (text_view->left_window);
2478 if (text_view->top_window)
2479 text_window_free (text_view->top_window);
2481 if (text_view->right_window)
2482 text_window_free (text_view->right_window);
2484 if (text_view->bottom_window)
2485 text_window_free (text_view->bottom_window);
2487 g_object_unref (text_view->im_context);
2489 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2493 gtk_text_view_set_property (GObject *object,
2495 const GValue *value,
2498 GtkTextView *text_view;
2500 text_view = GTK_TEXT_VIEW (object);
2504 case PROP_PIXELS_ABOVE_LINES:
2505 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2508 case PROP_PIXELS_BELOW_LINES:
2509 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2512 case PROP_PIXELS_INSIDE_WRAP:
2513 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2517 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2520 case PROP_WRAP_MODE:
2521 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2524 case PROP_JUSTIFICATION:
2525 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2528 case PROP_LEFT_MARGIN:
2529 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2532 case PROP_RIGHT_MARGIN:
2533 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2537 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2541 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2544 case PROP_CURSOR_VISIBLE:
2545 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2549 g_assert_not_reached ();
2555 gtk_text_view_get_property (GObject *object,
2560 GtkTextView *text_view;
2562 text_view = GTK_TEXT_VIEW (object);
2566 case PROP_PIXELS_ABOVE_LINES:
2567 g_value_set_int (value, text_view->pixels_above_lines);
2570 case PROP_PIXELS_BELOW_LINES:
2571 g_value_set_int (value, text_view->pixels_below_lines);
2574 case PROP_PIXELS_INSIDE_WRAP:
2575 g_value_set_int (value, text_view->pixels_inside_wrap);
2579 g_value_set_boolean (value, text_view->editable);
2582 case PROP_WRAP_MODE:
2583 g_value_set_enum (value, text_view->wrap_mode);
2586 case PROP_JUSTIFICATION:
2587 g_value_set_enum (value, text_view->justify);
2590 case PROP_LEFT_MARGIN:
2591 g_value_set_int (value, text_view->left_margin);
2594 case PROP_RIGHT_MARGIN:
2595 g_value_set_int (value, text_view->right_margin);
2599 g_value_set_int (value, text_view->indent);
2603 g_value_set_boxed (value, text_view->tabs);
2606 case PROP_CURSOR_VISIBLE:
2607 g_value_set_boolean (value, text_view->cursor_visible);
2611 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2617 gtk_text_view_size_request (GtkWidget *widget,
2618 GtkRequisition *requisition)
2620 GtkTextView *text_view;
2622 gint focus_edge_width;
2624 gboolean interior_focus;
2626 text_view = GTK_TEXT_VIEW (widget);
2628 gtk_widget_style_get (widget,
2629 "interior_focus", &interior_focus,
2630 "focus_line_width", &focus_width,
2634 focus_edge_width = 0;
2636 focus_edge_width = focus_width;
2638 if (text_view->layout)
2640 text_view->text_window->requisition.width = text_view->layout->width;
2641 text_view->text_window->requisition.height = text_view->layout->height;
2645 text_view->text_window->requisition.width = 0;
2646 text_view->text_window->requisition.height = 0;
2649 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2650 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2652 if (text_view->left_window)
2653 requisition->width += text_view->left_window->requisition.width;
2655 if (text_view->right_window)
2656 requisition->width += text_view->right_window->requisition.width;
2658 if (text_view->top_window)
2659 requisition->height += text_view->top_window->requisition.height;
2661 if (text_view->bottom_window)
2662 requisition->height += text_view->bottom_window->requisition.height;
2664 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2665 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2667 tmp_list = text_view->children;
2668 while (tmp_list != NULL)
2670 GtkTextViewChild *child = tmp_list->data;
2674 GtkRequisition child_req;
2675 GtkRequisition old_req;
2677 gtk_widget_get_child_requisition (child->widget, &old_req);
2679 gtk_widget_size_request (child->widget, &child_req);
2681 gtk_widget_get_child_requisition (child->widget, &child_req);
2683 /* Invalidate layout lines if required */
2684 if (text_view->layout &&
2685 (old_req.width != child_req.width ||
2686 old_req.height != child_req.height))
2687 gtk_text_child_anchor_queue_resize (child->anchor,
2692 GtkRequisition child_req;
2694 gtk_widget_size_request (child->widget, &child_req);
2697 tmp_list = g_slist_next (tmp_list);
2702 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2703 GtkTextViewChild *vc,
2704 GtkAllocation *allocation)
2710 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2714 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2717 buffer_y += vc->from_top_of_line;
2719 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2720 allocation->y = buffer_y - text_view->yoffset;
2722 gtk_widget_get_child_requisition (vc->widget, &req);
2723 allocation->width = req.width;
2724 allocation->height = req.height;
2728 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2729 GtkTextViewChild *vc)
2731 GtkAllocation allocation;
2733 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2735 gtk_widget_size_allocate (vc->widget, &allocation);
2738 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2740 vc->widget->allocation.x,
2741 vc->widget->allocation.y,
2742 text_view->yoffset);
2747 gtk_text_view_child_allocated (GtkTextLayout *layout,
2753 GtkTextViewChild *vc = NULL;
2754 GtkTextView *text_view = data;
2756 /* x,y is the position of the child from the top of the line, and
2757 * from the left of the buffer. We have to translate that into text
2758 * window coordinates, then size_allocate the child.
2761 vc = g_object_get_data (G_OBJECT (child),
2762 "gtk-text-view-child");
2764 g_assert (vc != NULL);
2766 DV (g_print ("child allocated at %d,%d\n", x, y));
2768 vc->from_left_of_buffer = x;
2769 vc->from_top_of_line = y;
2771 gtk_text_view_update_child_allocation (text_view, vc);
2775 gtk_text_view_allocate_children (GtkTextView *text_view)
2779 DV(g_print(G_STRLOC"\n"));
2781 tmp_list = text_view->children;
2782 while (tmp_list != NULL)
2784 GtkTextViewChild *child = tmp_list->data;
2788 /* We need to force-validate the regions containing
2791 GtkTextIter child_loc;
2792 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2796 gtk_text_layout_validate_yrange (text_view->layout,
2802 GtkAllocation allocation;
2803 GtkRequisition child_req;
2805 g_assert (child != NULL);
2807 allocation.x = child->x;
2808 allocation.y = child->y;
2810 gtk_widget_get_child_requisition (child->widget, &child_req);
2812 allocation.width = child_req.width;
2813 allocation.height = child_req.height;
2815 gtk_widget_size_allocate (child->widget, &allocation);
2818 tmp_list = g_slist_next (tmp_list);
2823 gtk_text_view_size_allocate (GtkWidget *widget,
2824 GtkAllocation *allocation)
2826 GtkTextView *text_view;
2827 GtkTextIter first_para;
2829 GtkAdjustment *vadj;
2830 gboolean yoffset_changed = FALSE;
2832 GdkRectangle text_rect;
2833 GdkRectangle left_rect;
2834 GdkRectangle right_rect;
2835 GdkRectangle top_rect;
2836 GdkRectangle bottom_rect;
2837 gint focus_edge_width;
2839 gboolean interior_focus;
2840 gboolean size_changed;
2842 text_view = GTK_TEXT_VIEW (widget);
2844 DV(g_print(G_STRLOC"\n"));
2847 widget->allocation.width != allocation->width ||
2848 widget->allocation.height != allocation->height;
2850 widget->allocation = *allocation;
2852 if (GTK_WIDGET_REALIZED (widget))
2854 gdk_window_move_resize (widget->window,
2855 allocation->x, allocation->y,
2856 allocation->width, allocation->height);
2859 /* distribute width/height among child windows. Ensure all
2860 * windows get at least a 1x1 allocation.
2863 gtk_widget_style_get (widget,
2864 "interior_focus", &interior_focus,
2865 "focus_line_width", &focus_width,
2869 focus_edge_width = 0;
2871 focus_edge_width = focus_width;
2873 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2875 if (text_view->left_window)
2876 left_rect.width = text_view->left_window->requisition.width;
2878 left_rect.width = 0;
2880 width -= left_rect.width;
2882 if (text_view->right_window)
2883 right_rect.width = text_view->right_window->requisition.width;
2885 right_rect.width = 0;
2887 width -= right_rect.width;
2889 text_rect.width = MAX (1, width);
2891 top_rect.width = text_rect.width;
2892 bottom_rect.width = text_rect.width;
2895 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2897 if (text_view->top_window)
2898 top_rect.height = text_view->top_window->requisition.height;
2900 top_rect.height = 0;
2902 height -= top_rect.height;
2904 if (text_view->bottom_window)
2905 bottom_rect.height = text_view->bottom_window->requisition.height;
2907 bottom_rect.height = 0;
2909 height -= bottom_rect.height;
2911 text_rect.height = MAX (1, height);
2913 left_rect.height = text_rect.height;
2914 right_rect.height = text_rect.height;
2917 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2918 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2920 text_rect.x = left_rect.x + left_rect.width;
2921 text_rect.y = top_rect.y + top_rect.height;
2923 left_rect.y = text_rect.y;
2924 right_rect.y = text_rect.y;
2926 top_rect.x = text_rect.x;
2927 bottom_rect.x = text_rect.x;
2929 right_rect.x = text_rect.x + text_rect.width;
2930 bottom_rect.y = text_rect.y + text_rect.height;
2932 text_window_size_allocate (text_view->text_window,
2935 if (text_view->left_window)
2936 text_window_size_allocate (text_view->left_window,
2939 if (text_view->right_window)
2940 text_window_size_allocate (text_view->right_window,
2943 if (text_view->top_window)
2944 text_window_size_allocate (text_view->top_window,
2947 if (text_view->bottom_window)
2948 text_window_size_allocate (text_view->bottom_window,
2951 gtk_text_view_update_layout_width (text_view);
2953 /* Note that this will do some layout validation */
2954 gtk_text_view_allocate_children (text_view);
2956 /* Now adjust the value of the adjustment to keep the cursor at the
2957 * same place in the buffer
2959 gtk_text_view_get_first_para_iter (text_view, &first_para);
2960 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2962 y += text_view->first_para_pixels;
2964 /* Ensure h/v adj exist */
2965 get_hadjustment (text_view);
2966 get_vadjustment (text_view);
2968 vadj = text_view->vadjustment;
2969 if (y > vadj->upper - vadj->page_size)
2970 y = MAX (0, vadj->upper - vadj->page_size);
2972 if (y != text_view->yoffset)
2975 yoffset_changed = TRUE;
2978 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2979 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
2980 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
2981 text_view->hadjustment->lower = 0;
2982 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2984 gtk_adjustment_changed (text_view->hadjustment);
2986 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2987 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2988 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2989 text_view->vadjustment->lower = 0;
2990 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2992 gtk_adjustment_changed (text_view->vadjustment);
2994 if (yoffset_changed)
2995 gtk_adjustment_value_changed (vadj);
2997 /* The GTK resize loop processes all the pending exposes right
2998 * after doing the resize stuff, so the idle sizer won't have a
2999 * chance to run. So we do the work here.
3001 gtk_text_view_flush_first_validate (text_view);
3003 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3006 if (size_changed && GTK_WIDGET_REALIZED (widget))
3007 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3011 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3014 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3015 text_view->first_para_mark);
3019 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3021 GtkWidget *widget = GTK_WIDGET (text_view);
3023 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3025 if (SCREEN_HEIGHT (widget) > 0)
3027 GtkTextIter first_para;
3029 /* Be sure we've validated the stuff onscreen; if we
3030 * scrolled, these calls won't have any effect, because
3031 * they were called in the recursive validate_onscreen
3033 gtk_text_view_get_first_para_iter (text_view, &first_para);
3035 gtk_text_layout_validate_yrange (text_view->layout,
3038 text_view->first_para_pixels +
3039 SCREEN_HEIGHT (widget));
3042 text_view->onscreen_validated = TRUE;
3044 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3046 /* This can have the odd side effect of triggering a scroll, which should
3047 * flip "onscreen_validated" back to FALSE, but should also get us
3048 * back into this function to turn it on again.
3050 gtk_text_view_update_adjustments (text_view);
3052 g_assert (text_view->onscreen_validated);
3056 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3058 if (text_view->first_validate_idle == 0)
3061 /* Do this first, which means that if an "invalidate"
3062 * occurs during any of this process, a new first_validate_callback
3063 * will be installed, and we'll start again.
3065 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3066 g_source_remove (text_view->first_validate_idle);
3067 text_view->first_validate_idle = 0;
3069 /* be sure we have up-to-date screen size set on the
3072 gtk_text_view_update_layout_width (text_view);
3074 /* Bail out if we invalidated stuff; scrolling right away will just
3075 * confuse the issue.
3077 if (text_view->first_validate_idle != 0)
3079 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3083 /* scroll to any marks, if that's pending. This can jump us to
3084 * the validation codepath used for scrolling onscreen, if so we
3085 * bail out. It won't jump if already in that codepath since
3086 * value_changed is not recursive, so also validate if
3089 if (!gtk_text_view_flush_scroll (text_view) ||
3090 !text_view->onscreen_validated)
3091 gtk_text_view_validate_onscreen (text_view);
3093 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3095 g_assert (text_view->onscreen_validated);
3100 first_validate_callback (gpointer data)
3102 GtkTextView *text_view = data;
3104 GDK_THREADS_ENTER ();
3106 /* Note that some of this code is duplicated at the end of size_allocate,
3107 * keep in sync with that.
3110 DV(g_print(G_STRLOC"\n"));
3112 gtk_text_view_flush_first_validate (text_view);
3114 GDK_THREADS_LEAVE ();
3120 incremental_validate_callback (gpointer data)
3122 GtkTextView *text_view = data;
3123 gboolean result = TRUE;
3125 GDK_THREADS_ENTER ();
3127 DV(g_print(G_STRLOC"\n"));
3129 gtk_text_layout_validate (text_view->layout, 2000);
3131 gtk_text_view_update_adjustments (text_view);
3133 if (gtk_text_layout_is_valid (text_view->layout))
3135 text_view->incremental_validate_idle = 0;
3139 GDK_THREADS_LEAVE ();
3145 gtk_text_view_invalidate (GtkTextView *text_view)
3147 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3148 text_view->onscreen_validated));
3150 text_view->onscreen_validated = FALSE;
3152 /* We'll invalidate when the layout is created */
3153 if (text_view->layout == NULL)
3156 if (!text_view->first_validate_idle)
3158 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3159 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3160 text_view->first_validate_idle));
3163 if (!text_view->incremental_validate_idle)
3165 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3166 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3167 text_view->incremental_validate_idle));
3172 invalidated_handler (GtkTextLayout *layout,
3175 GtkTextView *text_view;
3177 text_view = GTK_TEXT_VIEW (data);
3179 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3180 gtk_text_view_invalidate (text_view);
3184 changed_handler (GtkTextLayout *layout,
3190 GtkTextView *text_view;
3192 GdkRectangle visible_rect;
3193 GdkRectangle redraw_rect;
3195 text_view = GTK_TEXT_VIEW (data);
3196 widget = GTK_WIDGET (data);
3198 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3200 if (GTK_WIDGET_REALIZED (text_view))
3202 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3204 redraw_rect.x = visible_rect.x;
3205 redraw_rect.width = visible_rect.width;
3206 redraw_rect.y = start_y;
3208 if (old_height == new_height)
3209 redraw_rect.height = old_height;
3210 else if (start_y + old_height > visible_rect.y)
3211 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3213 redraw_rect.height = 0;
3215 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3217 /* text_window_invalidate_rect() takes buffer coordinates */
3218 text_window_invalidate_rect (text_view->text_window,
3221 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3225 redraw_rect.height));
3227 if (text_view->left_window)
3228 text_window_invalidate_rect (text_view->left_window,
3230 if (text_view->right_window)
3231 text_window_invalidate_rect (text_view->right_window,
3233 if (text_view->top_window)
3234 text_window_invalidate_rect (text_view->top_window,
3236 if (text_view->bottom_window)
3237 text_window_invalidate_rect (text_view->bottom_window,
3240 gtk_text_view_update_im_spot_location (text_view);
3244 if (old_height != new_height)
3246 gboolean yoffset_changed = FALSE;
3248 int new_first_para_top;
3249 int old_first_para_top;
3252 /* If the bottom of the old area was above the top of the
3253 * screen, we need to scroll to keep the current top of the
3254 * screen in place. Remember that first_para_pixels is the
3255 * position of the top of the screen in coordinates relative to
3256 * the first paragraph onscreen.
3258 * In short we are adding the height delta of the portion of the
3259 * changed region above first_para_mark to text_view->yoffset.
3261 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3262 text_view->first_para_mark);
3264 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3266 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3268 if (new_first_para_top != old_first_para_top)
3270 text_view->yoffset += new_first_para_top - old_first_para_top;
3272 get_vadjustment (text_view)->value = text_view->yoffset;
3273 yoffset_changed = TRUE;
3276 if (yoffset_changed)
3278 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3279 gtk_adjustment_value_changed (get_vadjustment (text_view));
3282 /* FIXME be smarter about which anchored widgets we update */
3284 tmp_list = text_view->children;
3285 while (tmp_list != NULL)
3287 GtkTextViewChild *child = tmp_list->data;
3290 gtk_text_view_update_child_allocation (text_view, child);
3292 tmp_list = g_slist_next (tmp_list);
3297 GtkRequisition old_req;
3298 GtkRequisition new_req;
3300 old_req = widget->requisition;
3302 /* Use this instead of gtk_widget_size_request wrapper
3303 * to avoid the optimization which just returns widget->requisition
3304 * if a resize hasn't been queued.
3306 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3308 if (old_req.width != new_req.width ||
3309 old_req.height != new_req.height)
3311 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3312 _gtk_size_group_queue_resize (widget);
3318 gtk_text_view_realize (GtkWidget *widget)
3320 GtkTextView *text_view;
3321 GdkWindowAttr attributes;
3322 gint attributes_mask;
3325 text_view = GTK_TEXT_VIEW (widget);
3326 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3328 attributes.window_type = GDK_WINDOW_CHILD;
3329 attributes.x = widget->allocation.x;
3330 attributes.y = widget->allocation.y;
3331 attributes.width = widget->allocation.width;
3332 attributes.height = widget->allocation.height;
3333 attributes.wclass = GDK_INPUT_OUTPUT;
3334 attributes.visual = gtk_widget_get_visual (widget);
3335 attributes.colormap = gtk_widget_get_colormap (widget);
3336 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3338 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3340 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3341 &attributes, attributes_mask);
3342 gdk_window_set_user_data (widget->window, widget);
3344 /* must come before text_window_realize calls */
3345 widget->style = gtk_style_attach (widget->style, widget->window);
3347 gdk_window_set_background (widget->window,
3348 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3350 text_window_realize (text_view->text_window, widget->window);
3352 if (text_view->left_window)
3353 text_window_realize (text_view->left_window,
3356 if (text_view->top_window)
3357 text_window_realize (text_view->top_window,
3360 if (text_view->right_window)
3361 text_window_realize (text_view->right_window,
3364 if (text_view->bottom_window)
3365 text_window_realize (text_view->bottom_window,
3368 gtk_text_view_ensure_layout (text_view);
3370 if (text_view->buffer)
3372 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3373 GDK_SELECTION_PRIMARY);
3374 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3377 tmp_list = text_view->children;
3378 while (tmp_list != NULL)
3380 GtkTextViewChild *vc = tmp_list->data;
3382 text_view_child_set_parent_window (text_view, vc);
3384 tmp_list = tmp_list->next;
3389 gtk_text_view_unrealize (GtkWidget *widget)
3391 GtkTextView *text_view;
3393 text_view = GTK_TEXT_VIEW (widget);
3395 if (text_view->buffer)
3397 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3398 GDK_SELECTION_PRIMARY);
3399 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3402 gtk_text_view_remove_validate_idles (text_view);
3404 if (text_view->popup_menu)
3406 gtk_widget_destroy (text_view->popup_menu);
3407 text_view->popup_menu = NULL;
3410 text_window_unrealize (text_view->text_window);
3412 if (text_view->left_window)
3413 text_window_unrealize (text_view->left_window);
3415 if (text_view->top_window)
3416 text_window_unrealize (text_view->top_window);
3418 if (text_view->right_window)
3419 text_window_unrealize (text_view->right_window);
3421 if (text_view->bottom_window)
3422 text_window_unrealize (text_view->bottom_window);
3424 gtk_text_view_destroy_layout (text_view);
3426 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3430 gtk_text_view_style_set (GtkWidget *widget,
3431 GtkStyle *previous_style)
3433 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3435 if (GTK_WIDGET_REALIZED (widget))
3437 gdk_window_set_background (widget->window,
3438 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3440 gdk_window_set_background (text_view->text_window->bin_window,
3441 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3443 if (text_view->left_window)
3444 gdk_window_set_background (text_view->left_window->bin_window,
3445 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3446 if (text_view->right_window)
3447 gdk_window_set_background (text_view->right_window->bin_window,
3448 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3450 if (text_view->top_window)
3451 gdk_window_set_background (text_view->top_window->bin_window,
3452 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3454 if (text_view->bottom_window)
3455 gdk_window_set_background (text_view->bottom_window->bin_window,
3456 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3459 if (text_view->layout && previous_style)
3461 gtk_text_view_set_attributes_from_style (text_view,
3462 text_view->layout->default_style,
3464 gtk_text_layout_default_style_changed (text_view->layout);
3469 gtk_text_view_direction_changed (GtkWidget *widget,
3470 GtkTextDirection previous_direction)
3472 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3474 if (text_view->layout)
3476 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3477 gtk_text_layout_default_style_changed (text_view->layout);
3483 set_invisible_cursor (GdkWindow *window)
3485 GdkBitmap *empty_bitmap;
3488 char invisible_cursor_bits[] = { 0x0 };
3490 useless.red = useless.green = useless.blue = 0;
3493 empty_bitmap = gdk_bitmap_create_from_data (window,
3494 invisible_cursor_bits,
3497 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3502 gdk_window_set_cursor (window, cursor);
3504 gdk_cursor_unref (cursor);
3506 g_object_unref (empty_bitmap);
3510 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3512 if (text_view->mouse_cursor_obscured)
3515 set_invisible_cursor (text_view->text_window->bin_window);
3517 text_view->mouse_cursor_obscured = TRUE;
3525 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3528 switch (event->type)
3530 case GDK_MOTION_NOTIFY:
3531 *x = event->motion.x;
3532 *y = event->motion.y;
3536 case GDK_BUTTON_PRESS:
3537 case GDK_2BUTTON_PRESS:
3538 case GDK_3BUTTON_PRESS:
3539 case GDK_BUTTON_RELEASE:
3540 *x = event->button.x;
3541 *y = event->button.y;
3546 case GDK_KEY_RELEASE:
3547 case GDK_ENTER_NOTIFY:
3548 case GDK_LEAVE_NOTIFY:
3549 case GDK_PROPERTY_NOTIFY:
3550 case GDK_SELECTION_CLEAR:
3551 case GDK_SELECTION_REQUEST:
3552 case GDK_SELECTION_NOTIFY:
3553 case GDK_PROXIMITY_IN:
3554 case GDK_PROXIMITY_OUT:
3555 case GDK_DRAG_ENTER:
3556 case GDK_DRAG_LEAVE:
3557 case GDK_DRAG_MOTION:
3558 case GDK_DRAG_STATUS:
3559 case GDK_DROP_START:
3560 case GDK_DROP_FINISHED:
3570 emit_event_on_tags (GtkWidget *widget,
3576 gboolean retval = FALSE;
3577 GtkTextView *text_view;
3579 text_view = GTK_TEXT_VIEW (widget);
3581 tags = gtk_text_iter_get_tags (iter);
3586 GtkTextTag *tag = tmp->data;
3588 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3594 tmp = g_slist_next (tmp);
3597 g_slist_free (tags);
3603 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3605 GtkTextView *text_view;
3608 text_view = GTK_TEXT_VIEW (widget);
3610 if (text_view->layout == NULL ||
3611 get_buffer (text_view) == NULL)
3614 if (event->any.window != text_view->text_window->bin_window)
3617 if (get_event_coordinates (event, &x, &y))
3621 x += text_view->xoffset;
3622 y += text_view->yoffset;
3624 /* FIXME this is slow and we do it twice per event.
3625 * My favorite solution is to have GtkTextLayout cache
3626 * the last couple lookups.
3628 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3632 return emit_event_on_tags (widget, event, &iter);
3634 else if (event->type == GDK_KEY_PRESS ||
3635 event->type == GDK_KEY_RELEASE)
3637 GtkTextMark *insert;
3640 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3643 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3645 return emit_event_on_tags (widget, event, &iter);
3652 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3654 gboolean retval = FALSE;
3655 gboolean obscure = FALSE;
3656 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3657 GtkTextMark *insert;
3659 gboolean can_insert;
3661 if (text_view->layout == NULL ||
3662 get_buffer (text_view) == NULL)
3665 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3666 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3667 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3669 gtk_im_context_filter_keypress (text_view->im_context, event))
3671 text_view->need_im_reset = TRUE;
3676 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3677 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3679 /* use overall editability not can_insert, more predictable for users */
3680 else if (text_view->editable &&
3681 (event->keyval == GDK_Return ||
3682 event->keyval == GDK_KP_Enter))
3684 /* this won't actually insert the newline if the cursor isn't
3687 gtk_text_view_commit_text (text_view, "\n");
3692 /* Pass through Tab as literal tab, unless Control is held down */
3693 else if ((event->keyval == GDK_Tab ||
3694 event->keyval == GDK_KP_Tab) &&
3695 !(event->state & GDK_CONTROL_MASK))
3697 /* If the text widget isn't editable overall, move the focus
3700 if (text_view->editable)
3702 gtk_text_view_commit_text (text_view, "\t");
3706 gtk_text_view_move_focus (text_view,
3707 (event->state & GDK_SHIFT_MASK) ?
3708 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3716 gtk_text_view_obscure_mouse_cursor (text_view);
3718 gtk_text_view_pend_cursor_blink (text_view);
3724 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3726 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3727 GtkTextMark *insert;
3730 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3733 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3734 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3735 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3736 gtk_im_context_filter_keypress (text_view->im_context, event))
3738 text_view->need_im_reset = TRUE;
3742 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3746 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3748 GtkTextView *text_view;
3750 text_view = GTK_TEXT_VIEW (widget);
3752 text_view->disable_scroll_on_focus = TRUE;
3753 gtk_widget_grab_focus (widget);
3754 text_view->disable_scroll_on_focus = FALSE;
3756 if (event->window != text_view->text_window->bin_window)
3758 /* Remove selection if any. */
3759 gtk_text_view_unselect (text_view);
3765 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3766 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3767 else if (event->button == 3)
3768 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3771 if (event->type == GDK_BUTTON_PRESS)
3773 gtk_text_view_reset_im_context (text_view);
3775 if (event->button == 1)
3777 /* If we're in the selection, start a drag copy/move of the
3778 * selection; otherwise, start creating a new selection.
3781 GtkTextIter start, end;
3783 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3785 event->x + text_view->xoffset,
3786 event->y + text_view->yoffset);
3788 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3790 gtk_text_iter_in_range (&iter, &start, &end))
3792 text_view->drag_start_x = event->x;
3793 text_view->drag_start_y = event->y;
3794 text_view->pending_place_cursor_button = event->button;
3798 gtk_text_view_start_selection_drag (text_view, &iter, event);
3803 else if (event->button == 2)
3807 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3809 event->x + text_view->xoffset,
3810 event->y + text_view->yoffset);
3812 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3813 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3815 text_view->editable);
3818 else if (event->button == 3)
3820 gtk_text_view_do_popup (text_view, event);
3823 else if ((event->type == GDK_2BUTTON_PRESS ||
3824 event->type == GDK_3BUTTON_PRESS) &&
3827 GtkTextIter start, end;
3829 /* End the selection drag, otherwise we'd clear the new
3830 * word/line selection on button release
3832 gtk_text_view_end_selection_drag (text_view, event);
3834 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3836 event->x + text_view->xoffset,
3837 event->y + text_view->yoffset);
3841 if (event->type == GDK_2BUTTON_PRESS)
3843 if (gtk_text_iter_inside_word (&start))
3845 if (!gtk_text_iter_starts_word (&start))
3846 gtk_text_iter_backward_word_start (&start);
3848 if (!gtk_text_iter_ends_word (&end))
3849 gtk_text_iter_forward_word_end (&end);
3852 else if (event->type == GDK_3BUTTON_PRESS)
3854 if (gtk_text_view_starts_display_line (text_view, &start))
3856 /* If on a display line boundary, we assume the user
3857 * clicked off the end of a line and we therefore select
3858 * the line before the boundary.
3860 gtk_text_view_backward_display_line_start (text_view, &start);
3864 /* start isn't on the start of a line, so we move it to the
3865 * start, and move end to the end unless it's already there.
3867 gtk_text_view_backward_display_line_start (text_view, &start);
3869 if (!gtk_text_view_starts_display_line (text_view, &end))
3870 gtk_text_view_forward_display_line_end (text_view, &end);
3874 if (event->state & GDK_SHIFT_MASK)
3876 /* Take union of old and new selection */
3877 GtkTextIter old_start, old_end;
3879 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3880 &old_start, &old_end);
3882 gtk_text_iter_order (&start, &old_start);
3883 gtk_text_iter_order (&old_end, &end);
3885 /* Now start is the first of the starts, and end is the
3890 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3893 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3897 text_view->just_selected_element = TRUE;
3906 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3908 GtkTextView *text_view;
3910 text_view = GTK_TEXT_VIEW (widget);
3912 if (event->window != text_view->text_window->bin_window)
3915 if (event->button == 1)
3917 if (text_view->drag_start_x >= 0)
3919 text_view->drag_start_x = -1;
3920 text_view->drag_start_y = -1;
3923 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3925 else if (text_view->just_selected_element)
3927 text_view->just_selected_element = FALSE;
3930 else if (text_view->pending_place_cursor_button == event->button)
3934 /* Unselect everything; we clicked inside selection, but
3935 * didn't move by the drag threshold, so just clear selection
3938 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3940 event->x + text_view->xoffset,
3941 event->y + text_view->yoffset);
3943 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3945 text_view->pending_place_cursor_button = 0;
3955 keymap_direction_changed (GdkKeymap *keymap,
3956 GtkTextView *text_view)
3958 gtk_text_view_check_keymap_direction (text_view);
3962 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3964 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3966 gtk_widget_queue_draw (widget);
3968 DV(g_print (G_STRLOC": focus_in_event\n"));
3970 if (text_view->cursor_visible && text_view->layout)
3972 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3973 gtk_text_view_check_cursor_blink (text_view);
3976 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3977 "direction_changed",
3978 G_CALLBACK (keymap_direction_changed), text_view);
3979 gtk_text_view_check_keymap_direction (text_view);
3981 text_view->need_im_reset = TRUE;
3982 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3988 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3990 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3992 gtk_widget_queue_draw (widget);
3994 DV(g_print (G_STRLOC": focus_out_event\n"));
3996 if (text_view->cursor_visible && text_view->layout)
3998 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3999 gtk_text_view_check_cursor_blink (text_view);
4002 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4003 keymap_direction_changed,
4006 text_view->need_im_reset = TRUE;
4007 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4013 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4015 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4017 if (text_view->mouse_cursor_obscured)
4021 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4023 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4024 gdk_cursor_unref (cursor);
4025 text_view->mouse_cursor_obscured = FALSE;
4028 if (event->window == text_view->text_window->bin_window &&
4029 text_view->drag_start_x >= 0)
4033 gdk_window_get_pointer (text_view->text_window->bin_window,
4036 if (gtk_drag_check_threshold (widget,
4037 text_view->drag_start_x,
4038 text_view->drag_start_y,
4042 gint buffer_x, buffer_y;
4044 gtk_text_view_window_to_buffer_coords (text_view,
4045 GTK_TEXT_WINDOW_TEXT,
4046 text_view->drag_start_x,
4047 text_view->drag_start_y,
4051 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4053 buffer_x, buffer_y);
4055 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4064 gtk_text_view_paint (GtkWidget *widget,
4066 GdkEventExpose *event)
4068 GtkTextView *text_view;
4069 GList *child_exposes;
4073 text_view = GTK_TEXT_VIEW (widget);
4075 g_return_if_fail (text_view->layout != NULL);
4076 g_return_if_fail (text_view->xoffset >= 0);
4077 g_return_if_fail (text_view->yoffset >= 0);
4079 while (text_view->first_validate_idle != 0)
4081 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4082 text_view->first_validate_idle));
4083 gtk_text_view_flush_first_validate (text_view);
4086 /* More regions could have become invalid in the above loop */
4087 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4092 gdk_region_get_clipbox (updates, &rect);
4094 gdk_rectangle_union (area, &rect, area);
4096 gdk_region_destroy (updates);
4099 if (!text_view->onscreen_validated)
4101 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.");
4102 g_assert_not_reached ();
4106 printf ("painting %d,%d %d x %d\n",
4108 area->width, area->height);
4111 child_exposes = NULL;
4112 gtk_text_layout_draw (text_view->layout,
4114 text_view->text_window->bin_window,
4119 area->width, area->height,
4122 tmp_list = child_exposes;
4123 while (tmp_list != NULL)
4125 GtkWidget *child = tmp_list->data;
4127 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4131 g_object_unref (child);
4133 tmp_list = tmp_list->next;
4136 g_list_free (child_exposes);
4140 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4144 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4145 GTK_TEXT_WINDOW_TEXT))
4147 DV(g_print (">Exposed ("G_STRLOC")\n"));
4148 gtk_text_view_paint (widget, &event->area, event);
4151 if (event->window == widget->window)
4152 gtk_text_view_draw_focus (widget);
4154 /* Propagate exposes to all children not in the buffer. */
4155 tmp_list = GTK_TEXT_VIEW (widget)->children;
4156 while (tmp_list != NULL)
4158 GtkTextViewChild *vc = tmp_list->data;
4160 /* propagate_expose checks that event->window matches
4163 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4164 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4168 tmp_list = tmp_list->next;
4175 gtk_text_view_draw_focus (GtkWidget *widget)
4177 gboolean interior_focus;
4179 /* We clear the focus if we are in interior focus mode. */
4180 gtk_widget_style_get (widget,
4181 "interior_focus", &interior_focus,
4184 if (GTK_WIDGET_DRAWABLE (widget))
4186 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4188 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4189 NULL, widget, "textview",
4191 widget->allocation.width,
4192 widget->allocation.height);
4196 gdk_window_clear (widget->window);
4202 gtk_text_view_grab_focus (GtkWidget *widget)
4204 GtkTextView *text_view;
4206 text_view = GTK_TEXT_VIEW (widget);
4208 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4210 if (!text_view->disable_scroll_on_focus)
4211 gtk_text_view_scroll_mark_onscreen (text_view,
4212 gtk_text_buffer_get_mark (get_buffer (text_view),
4217 gtk_text_view_focus (GtkWidget *widget,
4218 GtkDirectionType direction)
4220 GtkTextView *text_view;
4221 GtkContainer *container;
4224 text_view = GTK_TEXT_VIEW (widget);
4225 container = GTK_CONTAINER (widget);
4227 if (!gtk_widget_is_focus (widget) &&
4228 container->focus_child == NULL)
4230 gtk_widget_grab_focus (widget);
4236 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4237 * children to get the focus
4239 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4240 result = GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4241 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4252 gtk_text_view_add (GtkContainer *container,
4255 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4256 g_return_if_fail (GTK_IS_WIDGET (child));
4258 /* This is pretty random. */
4259 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4261 GTK_TEXT_WINDOW_WIDGET,
4266 gtk_text_view_remove (GtkContainer *container,
4270 GtkTextView *text_view;
4271 GtkTextViewChild *vc;
4273 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4274 g_return_if_fail (GTK_IS_WIDGET (child));
4275 g_return_if_fail (child->parent == (GtkWidget*) container);
4277 text_view = GTK_TEXT_VIEW (container);
4280 iter = text_view->children;
4282 while (iter != NULL)
4286 if (vc->widget == child)
4289 iter = g_slist_next (iter);
4292 g_assert (iter != NULL); /* be sure we had the child in the list */
4294 text_view->children = g_slist_remove (text_view->children, vc);
4296 gtk_widget_unparent (vc->widget);
4298 text_view_child_free (vc);
4302 gtk_text_view_forall (GtkContainer *container,
4303 gboolean include_internals,
4304 GtkCallback callback,
4305 gpointer callback_data)
4308 GtkTextView *text_view;
4311 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4312 g_return_if_fail (callback != NULL);
4314 text_view = GTK_TEXT_VIEW (container);
4316 copy = g_slist_copy (text_view->children);
4319 while (iter != NULL)
4321 GtkTextViewChild *vc = iter->data;
4323 (* callback) (vc->widget, callback_data);
4325 iter = g_slist_next (iter);
4328 g_slist_free (copy);
4331 #define CURSOR_ON_MULTIPLIER 0.66
4332 #define CURSOR_OFF_MULTIPLIER 0.34
4333 #define CURSOR_PEND_MULTIPLIER 1.0
4336 cursor_blinks (GtkTextView *text_view)
4338 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4341 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4344 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4347 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4352 get_cursor_time (GtkTextView *text_view)
4354 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4357 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4367 blink_cb (gpointer data)
4369 GtkTextView *text_view;
4372 GDK_THREADS_ENTER ();
4374 text_view = GTK_TEXT_VIEW (data);
4376 g_assert (text_view->layout);
4377 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4378 g_assert (text_view->cursor_visible);
4380 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4383 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4387 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4391 gtk_text_layout_set_cursor_visible (text_view->layout,
4394 GDK_THREADS_LEAVE ();
4396 /* Remove ourselves */
4402 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4404 if (text_view->blink_timeout)
4406 gtk_timeout_remove (text_view->blink_timeout);
4407 text_view->blink_timeout = 0;
4412 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4414 if (text_view->layout != NULL &&
4415 text_view->cursor_visible &&
4416 GTK_WIDGET_HAS_FOCUS (text_view))
4418 if (cursor_blinks (text_view))
4420 if (text_view->blink_timeout == 0)
4422 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4424 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4430 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4434 gtk_text_view_stop_cursor_blink (text_view);
4439 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4441 if (text_view->layout != NULL &&
4442 text_view->cursor_visible &&
4443 GTK_WIDGET_HAS_FOCUS (text_view) &&
4444 cursor_blinks (text_view))
4446 if (text_view->blink_timeout != 0)
4448 gtk_timeout_remove (text_view->blink_timeout);
4449 text_view->blink_timeout = 0;
4452 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4454 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4462 * Key binding handlers
4466 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4467 GtkTextIter *newplace,
4472 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4478 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4484 move_cursor (GtkTextView *text_view,
4485 const GtkTextIter *new_location,
4486 gboolean extend_selection)
4488 if (extend_selection)
4489 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4493 gtk_text_buffer_place_cursor (get_buffer (text_view),
4497 /* FIXME when we are unfrozen and can change GtkMovementStep,
4500 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4503 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4504 GtkMovementStep step,
4506 gboolean extend_selection)
4509 GtkTextIter newplace;
4511 gint cursor_x_pos = 0;
4513 gtk_text_view_reset_im_context (text_view);
4515 if (step == GTK_MOVEMENT_PAGES)
4517 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4518 gtk_text_view_pend_cursor_blink (text_view);
4521 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4523 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4524 gtk_text_view_pend_cursor_blink (text_view);
4528 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4529 gtk_text_buffer_get_mark (get_buffer (text_view),
4533 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4534 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4538 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4539 gtk_text_iter_forward_cursor_positions (&newplace, count);
4542 case GTK_MOVEMENT_VISUAL_POSITIONS:
4543 gtk_text_layout_move_iter_visually (text_view->layout,
4547 case GTK_MOVEMENT_WORDS:
4549 gtk_text_iter_backward_word_starts (&newplace, -count);
4551 gtk_text_iter_forward_word_ends (&newplace, count);
4554 case GTK_MOVEMENT_DISPLAY_LINES:
4555 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4556 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4559 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4561 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4562 else if (count < -1)
4563 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4566 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4569 case GTK_MOVEMENT_PARAGRAPHS:
4572 if (!gtk_text_iter_ends_line (&newplace))
4574 gtk_text_iter_forward_to_line_end (&newplace);
4580 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4582 gtk_text_iter_set_line_offset (&newplace, 0);
4589 gtk_text_iter_forward_lines (&newplace, count);
4590 gtk_text_iter_set_line_offset (&newplace, 0);
4594 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4597 if (!gtk_text_iter_ends_line (&newplace))
4598 gtk_text_iter_forward_to_line_end (&newplace);
4602 gtk_text_iter_set_line_offset (&newplace, 0);
4606 case GTK_MOVEMENT_BUFFER_ENDS:
4608 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4610 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4617 if (!gtk_text_iter_equal (&insert, &newplace))
4619 move_cursor (text_view, &newplace, extend_selection);
4621 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4622 gtk_text_view_scroll_mark_onscreen (text_view,
4623 gtk_text_buffer_get_mark (get_buffer (text_view),
4626 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4628 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4632 gtk_text_view_pend_cursor_blink (text_view);
4636 gtk_text_view_move_cursor (GtkTextView *text_view,
4637 GtkMovementStep step,
4639 gboolean extend_selection)
4641 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4645 gtk_text_view_page_horizontally (GtkTextView *text_view,
4647 gboolean extend_selection)
4649 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4650 count, extend_selection);
4654 gtk_text_view_set_anchor (GtkTextView *text_view)
4658 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4659 gtk_text_buffer_get_mark (get_buffer (text_view),
4662 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4666 gtk_text_view_scroll_pages (GtkTextView *text_view,
4668 gboolean extend_selection)
4673 gint cursor_x_pos, cursor_y_pos;
4674 GtkTextIter new_insert;
4678 g_return_if_fail (text_view->vadjustment != NULL);
4680 cancel_pending_scroll (text_view);
4682 adj = text_view->vadjustment;
4684 /* Validate the region that will be brought into view by the cursor motion
4688 gtk_text_view_get_first_para_iter (text_view, &anchor);
4689 y0 = adj->page_size;
4690 y1 = adj->page_size + count * adj->page_increment;
4694 gtk_text_view_get_first_para_iter (text_view, &anchor);
4695 y0 = count * adj->page_increment + adj->page_size;
4699 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4700 /* FIXME do we need to update the adjustment ranges here? */
4702 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4704 /* already at top, just be sure we are at offset 0 */
4705 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4706 move_cursor (text_view, &new_insert, extend_selection);
4708 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4710 /* already at bottom, just be sure we are at the end */
4711 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4712 move_cursor (text_view, &new_insert, extend_selection);
4716 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4718 newval = adj->value;
4719 oldval = adj->value;
4721 newval += count * adj->page_increment;
4723 set_adjustment_clamped (adj, newval);
4724 cursor_y_pos += adj->value - oldval;
4726 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4727 clamp_iter_onscreen (text_view, &new_insert);
4728 move_cursor (text_view, &new_insert, extend_selection);
4730 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4733 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4734 * only guarantees 1 pixel onscreen.
4736 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4737 gtk_text_view_scroll_mark_onscreen (text_view,
4738 gtk_text_buffer_get_mark (get_buffer (text_view),
4743 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4745 gboolean extend_selection)
4750 gint cursor_x_pos, cursor_y_pos;
4751 GtkTextIter new_insert;
4754 g_return_if_fail (text_view->hadjustment != NULL);
4756 cancel_pending_scroll (text_view);
4758 adj = text_view->hadjustment;
4760 /* Validate the line that we're moving within.
4762 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4764 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4765 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4766 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4767 /* FIXME do we need to update the adjustment ranges here? */
4769 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4771 /* already at far left, just be sure we are at offset 0 */
4772 gtk_text_iter_set_line_offset (&new_insert, 0);
4773 move_cursor (text_view, &new_insert, extend_selection);
4775 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4777 /* already at far right, just be sure we are at the end */
4778 gtk_text_iter_forward_to_line_end (&new_insert);
4779 move_cursor (text_view, &new_insert, extend_selection);
4783 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4785 newval = adj->value;
4786 oldval = adj->value;
4788 newval += count * adj->page_increment;
4790 set_adjustment_clamped (adj, newval);
4791 cursor_x_pos += adj->value - oldval;
4793 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4794 clamp_iter_onscreen (text_view, &new_insert);
4795 move_cursor (text_view, &new_insert, extend_selection);
4797 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4800 /* FIXME for lines shorter than the overall widget width, this results in a
4801 * "bounce" effect as we scroll to the right of the widget, then scroll
4802 * back to get the end of the line onscreen.
4803 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4806 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4807 * only guarantees 1 pixel onscreen.
4809 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4810 gtk_text_view_scroll_mark_onscreen (text_view,
4811 gtk_text_buffer_get_mark (get_buffer (text_view),
4816 whitespace (gunichar ch, gpointer user_data)
4818 return (ch == ' ' || ch == '\t');
4822 not_whitespace (gunichar ch, gpointer user_data)
4824 return !whitespace (ch, user_data);
4828 find_whitepace_region (const GtkTextIter *center,
4829 GtkTextIter *start, GtkTextIter *end)
4834 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4835 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4836 if (whitespace (gtk_text_iter_get_char (end), NULL))
4837 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4839 return !gtk_text_iter_equal (start, end);
4843 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4846 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4847 text_view->editable);
4851 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4858 gboolean leave_one = FALSE;
4860 gtk_text_view_reset_im_context (text_view);
4862 if (type == GTK_DELETE_CHARS)
4864 /* Char delete deletes the selection, if one exists */
4865 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4866 text_view->editable))
4870 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4872 gtk_text_buffer_get_mark (get_buffer (text_view),
4880 case GTK_DELETE_CHARS:
4881 gtk_text_iter_forward_cursor_positions (&end, count);
4884 case GTK_DELETE_WORD_ENDS:
4886 gtk_text_iter_forward_word_ends (&end, count);
4888 gtk_text_iter_backward_word_starts (&start, 0 - count);
4891 case GTK_DELETE_WORDS:
4894 case GTK_DELETE_DISPLAY_LINE_ENDS:
4897 case GTK_DELETE_DISPLAY_LINES:
4900 case GTK_DELETE_PARAGRAPH_ENDS:
4901 /* If we're already at a newline, we need to
4902 * simply delete that newline, instead of
4903 * moving to the next one.
4905 if (gtk_text_iter_ends_line (&end))
4907 gtk_text_iter_forward_line (&end);
4913 if (!gtk_text_iter_forward_to_line_end (&end))
4919 /* FIXME figure out what a negative count means
4923 case GTK_DELETE_PARAGRAPHS:
4926 gtk_text_iter_set_line_offset (&start, 0);
4927 gtk_text_iter_forward_to_line_end (&end);
4929 /* Do the lines beyond the first. */
4932 gtk_text_iter_forward_to_line_end (&end);
4938 /* FIXME negative count? */
4942 case GTK_DELETE_WHITESPACE:
4944 find_whitepace_region (&insert, &start, &end);
4952 if (!gtk_text_iter_equal (&start, &end))
4954 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4956 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4957 text_view->editable))
4960 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4962 text_view->editable);
4965 gtk_text_buffer_end_user_action (get_buffer (text_view));
4967 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4968 gtk_text_view_scroll_mark_onscreen (text_view,
4969 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4974 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4976 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4977 GDK_SELECTION_CLIPBOARD);
4979 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4981 text_view->editable);
4982 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4983 gtk_text_view_scroll_mark_onscreen (text_view,
4984 gtk_text_buffer_get_mark (get_buffer (text_view),
4989 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4991 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4992 GDK_SELECTION_CLIPBOARD);
4994 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4996 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4997 gtk_text_view_scroll_mark_onscreen (text_view,
4998 gtk_text_buffer_get_mark (get_buffer (text_view),
5003 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5005 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5006 GDK_SELECTION_CLIPBOARD);
5008 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5011 text_view->editable);
5012 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5013 gtk_text_view_scroll_mark_onscreen (text_view,
5014 gtk_text_buffer_get_mark (get_buffer (text_view),
5019 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5021 text_view->overwrite_mode = !text_view->overwrite_mode;
5025 gtk_text_view_move_focus (GtkTextView *text_view,
5026 GtkDirectionType direction_type)
5028 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5030 if (!GTK_WIDGET_TOPLEVEL (toplevel))
5033 /* Propagate to toplevel */
5034 g_signal_emit_by_name (toplevel, "move_focus", direction_type);
5042 gtk_text_view_unselect (GtkTextView *text_view)
5046 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5048 gtk_text_buffer_get_mark (get_buffer (text_view),
5051 gtk_text_buffer_move_mark (get_buffer (text_view),
5052 gtk_text_buffer_get_mark (get_buffer (text_view),
5058 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5059 const gchar *mark_name)
5062 GdkModifierType state;
5063 GtkTextIter newplace;
5065 /* DV(g_print (G_STRLOC": begin\n")); */
5067 gdk_window_get_pointer (text_view->text_window->bin_window,
5070 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5071 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5073 x + text_view->xoffset,
5074 y + text_view->yoffset);
5078 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5080 /* This may invalidate the layout */
5081 DV(g_print (G_STRLOC": move mark\n"));
5082 gtk_text_buffer_move_mark (get_buffer (text_view),
5086 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5087 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5090 DV (g_print ("first validate idle leaving %s is %d\n",
5091 G_STRLOC, text_view->first_validate_idle));
5095 selection_scan_timeout (gpointer data)
5097 GtkTextView *text_view;
5099 GDK_THREADS_ENTER ();
5101 text_view = GTK_TEXT_VIEW (data);
5103 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5104 move_mark_to_pointer_and_scroll (text_view, "insert");
5106 GDK_THREADS_LEAVE ();
5108 return TRUE; /* remain installed. */
5111 #define DND_SCROLL_MARGIN 0.20
5114 drag_scan_timeout (gpointer data)
5116 GtkTextView *text_view;
5118 GdkModifierType state;
5119 GtkTextIter newplace;
5121 GDK_THREADS_ENTER ();
5123 text_view = GTK_TEXT_VIEW (data);
5125 gdk_window_get_pointer (text_view->text_window->bin_window,
5128 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5130 x + text_view->xoffset,
5131 y + text_view->yoffset);
5133 gtk_text_buffer_move_mark (get_buffer (text_view),
5134 text_view->dnd_mark,
5137 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5138 gtk_text_view_scroll_to_mark (text_view,
5139 text_view->dnd_mark,
5140 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5142 GDK_THREADS_LEAVE ();
5148 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5150 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5151 move_mark_to_pointer_and_scroll (text_view, "insert");
5153 /* If we had to scroll offscreen, insert a timeout to do so
5154 * again. Note that in the timeout, even if the mouse doesn't
5155 * move, due to this scroll xoffset/yoffset will have changed
5156 * and we'll need to scroll again.
5158 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5159 gtk_timeout_remove (text_view->scroll_timeout);
5161 text_view->scroll_timeout =
5162 gtk_timeout_add (50, selection_scan_timeout, text_view);
5168 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5169 const GtkTextIter *iter,
5170 GdkEventButton *button)
5172 GtkTextIter newplace;
5173 GtkTextBuffer *buffer;
5175 g_return_if_fail (text_view->selection_drag_handler == 0);
5177 gtk_grab_add (GTK_WIDGET (text_view));
5179 buffer = get_buffer (text_view);
5183 if (button->state & GDK_SHIFT_MASK)
5185 /* Extend selection */
5186 GtkTextIter start, end;
5188 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5190 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5192 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5195 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5198 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5200 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5203 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5209 /* Replace selection */
5210 gtk_text_buffer_place_cursor (buffer, &newplace);
5213 text_view->selection_drag_handler = g_signal_connect (text_view,
5214 "motion_notify_event",
5215 G_CALLBACK (selection_motion_event_handler),
5219 /* returns whether we were really dragging */
5221 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5223 if (text_view->selection_drag_handler == 0)
5226 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
5227 text_view->selection_drag_handler = 0;
5229 if (text_view->scroll_timeout != 0)
5231 gtk_timeout_remove (text_view->scroll_timeout);
5232 text_view->scroll_timeout = 0;
5235 /* one last update to current position */
5236 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5237 move_mark_to_pointer_and_scroll (text_view, "insert");
5239 gtk_grab_remove (GTK_WIDGET (text_view));
5249 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5250 GtkTextAttributes *values,
5253 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5254 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5257 pango_font_description_free (values->font);
5259 values->font = pango_font_description_copy (style->font_desc);
5263 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5265 if (text_view->layout)
5267 gboolean split_cursor;
5268 GtkTextDirection new_dir;
5269 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5271 g_object_get (G_OBJECT (settings),
5272 "gtk-split-cursor", &split_cursor,
5276 new_dir = GTK_TEXT_DIR_NONE;
5280 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5281 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5282 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5285 if (text_view->layout->cursor_direction != new_dir)
5286 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5291 gtk_text_view_ensure_layout (GtkTextView *text_view)
5295 widget = GTK_WIDGET (text_view);
5297 if (text_view->layout == NULL)
5299 GtkTextAttributes *style;
5300 PangoContext *ltr_context, *rtl_context;
5303 DV(g_print(G_STRLOC"\n"));
5305 text_view->layout = gtk_text_layout_new ();
5307 g_signal_connect (text_view->layout,
5309 G_CALLBACK (invalidated_handler),
5312 g_signal_connect (text_view->layout,
5314 G_CALLBACK (changed_handler),
5317 g_signal_connect (text_view->layout,
5319 G_CALLBACK (gtk_text_view_child_allocated),
5322 if (get_buffer (text_view))
5323 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5325 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5326 gtk_text_view_pend_cursor_blink (text_view);
5328 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5330 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5331 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5332 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5333 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5335 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5337 g_object_unref (ltr_context);
5338 g_object_unref (rtl_context);
5340 gtk_text_view_check_keymap_direction (text_view);
5342 style = gtk_text_attributes_new ();
5344 gtk_widget_ensure_style (widget);
5345 gtk_text_view_set_attributes_from_style (text_view,
5346 style, widget->style);
5348 style->pixels_above_lines = text_view->pixels_above_lines;
5349 style->pixels_below_lines = text_view->pixels_below_lines;
5350 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5351 style->left_margin = text_view->left_margin;
5352 style->right_margin = text_view->right_margin;
5353 style->indent = text_view->indent;
5354 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5356 style->wrap_mode = text_view->wrap_mode;
5357 style->justification = text_view->justify;
5358 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5360 gtk_text_layout_set_default_style (text_view->layout, style);
5362 gtk_text_attributes_unref (style);
5364 /* Set layout for all anchored children */
5366 tmp_list = text_view->children;
5367 while (tmp_list != NULL)
5369 GtkTextViewChild *vc = tmp_list->data;
5373 gtk_text_anchored_child_set_layout (vc->widget,
5375 /* vc may now be invalid! */
5378 tmp_list = g_slist_next (tmp_list);
5381 gtk_text_view_invalidate (text_view);
5386 * gtk_text_view_get_default_attributes:
5387 * @text_view: a #GtkTextView
5389 * Obtains a copy of the default text attributes. These are the
5390 * attributes used for text unless a tag overrides them.
5391 * You'd typically pass the default attributes in to
5392 * gtk_text_iter_get_attributes() in order to get the
5393 * attributes in effect at a given text position.
5395 * The return value is a copy owned by the caller of this function,
5396 * and should be freed.
5398 * Return value: a new #GtkTextAttributes
5401 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5403 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5405 gtk_text_view_ensure_layout (text_view);
5407 return gtk_text_attributes_copy (text_view->layout->default_style);
5411 gtk_text_view_destroy_layout (GtkTextView *text_view)
5413 if (text_view->layout)
5417 gtk_text_view_remove_validate_idles (text_view);
5419 g_signal_handlers_disconnect_by_func (text_view->layout,
5420 invalidated_handler,
5422 g_signal_handlers_disconnect_by_func (text_view->layout,
5426 /* Remove layout from all anchored children */
5427 tmp_list = text_view->children;
5428 while (tmp_list != NULL)
5430 GtkTextViewChild *vc = tmp_list->data;
5434 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5435 /* vc may now be invalid! */
5438 tmp_list = g_slist_next (tmp_list);
5441 gtk_text_view_stop_cursor_blink (text_view);
5442 gtk_text_view_end_selection_drag (text_view, NULL);
5444 g_object_unref (text_view->layout);
5445 text_view->layout = NULL;
5450 gtk_text_view_reset_im_context (GtkTextView *text_view)
5452 if (text_view->need_im_reset)
5454 text_view->need_im_reset = FALSE;
5455 gtk_im_context_reset (text_view->im_context);
5464 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5465 const GtkTextIter *iter,
5466 GdkEventMotion *event)
5468 GdkDragContext *context;
5469 GtkTargetList *target_list;
5471 text_view->drag_start_x = -1;
5472 text_view->drag_start_y = -1;
5473 text_view->pending_place_cursor_button = 0;
5475 target_list = gtk_target_list_new (target_table,
5476 G_N_ELEMENTS (target_table));
5478 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5479 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5480 1, (GdkEvent*)event);
5482 gtk_target_list_unref (target_list);
5484 gtk_drag_set_icon_default (context);
5488 gtk_text_view_drag_begin (GtkWidget *widget,
5489 GdkDragContext *context)
5495 gtk_text_view_drag_end (GtkWidget *widget,
5496 GdkDragContext *context)
5498 GtkTextView *text_view;
5500 text_view = GTK_TEXT_VIEW (widget);
5504 gtk_text_view_drag_data_get (GtkWidget *widget,
5505 GdkDragContext *context,
5506 GtkSelectionData *selection_data,
5510 GtkTextView *text_view;
5512 text_view = GTK_TEXT_VIEW (widget);
5514 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5516 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5518 gtk_selection_data_set (selection_data,
5519 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5532 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5535 /* Extract the selected text */
5536 str = gtk_text_iter_get_visible_text (&start, &end);
5541 gtk_selection_data_set_text (selection_data, str, -1);
5548 gtk_text_view_drag_data_delete (GtkWidget *widget,
5549 GdkDragContext *context)
5551 GtkTextView *text_view;
5553 text_view = GTK_TEXT_VIEW (widget);
5555 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5556 TRUE, GTK_TEXT_VIEW (widget)->editable);
5560 gtk_text_view_drag_leave (GtkWidget *widget,
5561 GdkDragContext *context,
5564 GtkTextView *text_view;
5566 text_view = GTK_TEXT_VIEW (widget);
5568 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5570 if (text_view->scroll_timeout != 0)
5571 gtk_timeout_remove (text_view->scroll_timeout);
5573 text_view->scroll_timeout = 0;
5577 gtk_text_view_drag_motion (GtkWidget *widget,
5578 GdkDragContext *context,
5583 GtkTextIter newplace;
5584 GtkTextView *text_view;
5587 GdkRectangle target_rect;
5589 GdkDragAction suggested_action = 0;
5591 text_view = GTK_TEXT_VIEW (widget);
5593 target_rect = text_view->text_window->allocation;
5595 if (x < target_rect.x ||
5596 y < target_rect.y ||
5597 x > (target_rect.x + target_rect.width) ||
5598 y > (target_rect.y + target_rect.height))
5599 return FALSE; /* outside the text window, allow parent widgets to handle event */
5601 gtk_text_view_window_to_buffer_coords (text_view,
5602 GTK_TEXT_WINDOW_WIDGET,
5606 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5610 if (gtk_drag_dest_find_target (widget, context,
5611 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5613 /* can't accept any of the offered targets */
5615 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5617 gtk_text_iter_compare (&newplace, &start) >= 0 &&
5618 gtk_text_iter_compare (&newplace, &end) <= 0)
5620 /* We're inside the selection. */
5624 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5626 GtkWidget *source_widget;
5628 suggested_action = context->suggested_action;
5630 source_widget = gtk_drag_get_source_widget (context);
5632 if (source_widget == widget)
5634 /* Default to MOVE, unless the user has
5635 * pressed ctrl or alt to affect available actions
5637 if ((context->actions & GDK_ACTION_MOVE) != 0)
5638 suggested_action = GDK_ACTION_MOVE;
5643 /* Can't drop here. */
5647 if (suggested_action != 0)
5649 gtk_text_mark_set_visible (text_view->dnd_mark,
5650 text_view->cursor_visible);
5652 gdk_drag_status (context, suggested_action, time);
5656 gdk_drag_status (context, 0, time);
5657 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5660 gtk_text_buffer_move_mark (get_buffer (text_view),
5661 text_view->dnd_mark,
5664 DV(g_print (G_STRLOC": scrolling to mark\n"));
5665 gtk_text_view_scroll_to_mark (text_view,
5666 text_view->dnd_mark,
5667 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5669 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5670 gtk_timeout_remove (text_view->scroll_timeout);
5672 text_view->scroll_timeout =
5673 gtk_timeout_add (50, drag_scan_timeout, text_view);
5675 /* TRUE return means don't propagate the drag motion to parent
5676 * widgets that may also be drop sites.
5682 gtk_text_view_drag_drop (GtkWidget *widget,
5683 GdkDragContext *context,
5688 GtkTextView *text_view;
5689 GtkTextIter drop_point;
5690 GdkAtom target = GDK_NONE;
5692 text_view = GTK_TEXT_VIEW (widget);
5694 if (text_view->scroll_timeout != 0)
5695 gtk_timeout_remove (text_view->scroll_timeout);
5697 text_view->scroll_timeout = 0;
5699 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5701 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5703 text_view->dnd_mark);
5705 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5706 target = gtk_drag_dest_find_target (widget, context, NULL);
5708 if (target != GDK_NONE)
5709 gtk_drag_get_data (widget, context, target, time);
5711 gtk_drag_finish (context, FALSE, FALSE, time);
5717 insert_text_data (GtkTextView *text_view,
5718 GtkTextIter *drop_point,
5719 GtkSelectionData *selection_data)
5723 str = gtk_selection_data_get_text (selection_data);
5727 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5728 drop_point, str, -1,
5729 text_view->editable);
5735 gtk_text_view_drag_data_received (GtkWidget *widget,
5736 GdkDragContext *context,
5739 GtkSelectionData *selection_data,
5743 GtkTextIter drop_point;
5744 GtkTextView *text_view;
5745 gboolean success = FALSE;
5747 text_view = GTK_TEXT_VIEW (widget);
5749 if (!text_view->dnd_mark)
5752 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5754 text_view->dnd_mark);
5756 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5759 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5761 GtkTextBuffer *src_buffer = NULL;
5762 GtkTextIter start, end;
5763 gboolean copy_tags = TRUE;
5765 if (selection_data->length != sizeof (src_buffer))
5768 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5770 if (src_buffer == NULL)
5773 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5775 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5776 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5779 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5784 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5788 text_view->editable);
5793 str = gtk_text_iter_get_visible_text (&start, &end);
5794 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5795 &drop_point, str, -1,
5796 text_view->editable);
5802 insert_text_data (text_view, &drop_point, selection_data);
5807 gtk_drag_finish (context, success,
5808 success && context->action == GDK_ACTION_MOVE,
5812 static GtkAdjustment*
5813 get_hadjustment (GtkTextView *text_view)
5815 if (text_view->hadjustment == NULL)
5816 gtk_text_view_set_scroll_adjustments (text_view,
5817 NULL, /* forces creation */
5818 text_view->vadjustment);
5820 return text_view->hadjustment;
5823 static GtkAdjustment*
5824 get_vadjustment (GtkTextView *text_view)
5826 if (text_view->vadjustment == NULL)
5827 gtk_text_view_set_scroll_adjustments (text_view,
5828 text_view->hadjustment,
5829 NULL); /* forces creation */
5830 return text_view->vadjustment;
5835 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5836 GtkAdjustment *hadj,
5837 GtkAdjustment *vadj)
5839 gboolean need_adjust = FALSE;
5841 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5844 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5846 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5848 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5850 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5852 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5854 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
5855 gtk_text_view_value_changed,
5857 g_object_unref (text_view->hadjustment);
5860 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5862 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
5863 gtk_text_view_value_changed,
5865 g_object_unref (text_view->vadjustment);
5868 if (text_view->hadjustment != hadj)
5870 text_view->hadjustment = hadj;
5871 g_object_ref (text_view->hadjustment);
5872 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5874 g_signal_connect (text_view->hadjustment, "value_changed",
5875 G_CALLBACK (gtk_text_view_value_changed),
5880 if (text_view->vadjustment != vadj)
5882 text_view->vadjustment = vadj;
5883 g_object_ref (text_view->vadjustment);
5884 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5886 g_signal_connect (text_view->vadjustment, "value_changed",
5887 G_CALLBACK (gtk_text_view_value_changed),
5893 gtk_text_view_value_changed (NULL, text_view);
5896 /* FIXME this adjust_allocation is a big cut-and-paste from
5897 * GtkCList, needs to be some "official" way to do this
5907 /* The window to which widget->window is relative */
5908 #define ALLOCATION_WINDOW(widget) \
5909 (GTK_WIDGET_NO_WINDOW (widget) ? \
5910 (widget)->window : \
5911 gdk_window_get_parent ((widget)->window))
5914 adjust_allocation_recurse (GtkWidget *widget,
5917 ScrollData *scroll_data = data;
5919 /* Need to really size allocate instead of just poking
5920 * into widget->allocation if the widget is not realized.
5921 * FIXME someone figure out why this was.
5923 if (!GTK_WIDGET_REALIZED (widget))
5925 if (GTK_WIDGET_VISIBLE (widget))
5927 GdkRectangle tmp_rectangle = widget->allocation;
5928 tmp_rectangle.x += scroll_data->dx;
5929 tmp_rectangle.y += scroll_data->dy;
5931 gtk_widget_size_allocate (widget, &tmp_rectangle);
5936 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5938 widget->allocation.x += scroll_data->dx;
5939 widget->allocation.y += scroll_data->dy;
5941 if (GTK_IS_CONTAINER (widget))
5942 gtk_container_forall (GTK_CONTAINER (widget),
5943 adjust_allocation_recurse,
5950 adjust_allocation (GtkWidget *widget,
5954 ScrollData scroll_data;
5956 if (GTK_WIDGET_REALIZED (widget))
5957 scroll_data.window = ALLOCATION_WINDOW (widget);
5959 scroll_data.window = NULL;
5961 scroll_data.dx = dx;
5962 scroll_data.dy = dy;
5964 adjust_allocation_recurse (widget, &scroll_data);
5968 gtk_text_view_value_changed (GtkAdjustment *adj,
5969 GtkTextView *text_view)
5976 /* Note that we oddly call this function with adj == NULL
5980 text_view->onscreen_validated = FALSE;
5982 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5983 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5984 adj ? adj->value : 0.0));
5986 if (adj == text_view->hadjustment)
5988 dx = text_view->xoffset - (gint)adj->value;
5989 text_view->xoffset = adj->value;
5991 else if (adj == text_view->vadjustment)
5993 dy = text_view->yoffset - (gint)adj->value;
5994 text_view->yoffset = adj->value;
5996 if (text_view->layout)
5998 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
6000 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
6002 text_view->first_para_pixels = adj->value - line_top;
6006 if (dx != 0 || dy != 0)
6010 if (GTK_WIDGET_REALIZED (text_view))
6014 if (text_view->left_window)
6015 text_window_scroll (text_view->left_window, 0, dy);
6016 if (text_view->right_window)
6017 text_window_scroll (text_view->right_window, 0, dy);
6022 if (text_view->top_window)
6023 text_window_scroll (text_view->top_window, dx, 0);
6024 if (text_view->bottom_window)
6025 text_window_scroll (text_view->bottom_window, dx, 0);
6028 /* It looks nicer to scroll the main area last, because
6029 * it takes a while, and making the side areas update
6030 * afterward emphasizes the slowness of scrolling the
6033 text_window_scroll (text_view->text_window, dx, dy);
6036 /* Children are now "moved" in the text window, poke
6037 * into widget->allocation for each child
6039 tmp_list = text_view->children;
6040 while (tmp_list != NULL)
6042 GtkTextViewChild *child = tmp_list->data;
6045 adjust_allocation (child->widget, dx, dy);
6047 tmp_list = g_slist_next (tmp_list);
6051 /* This could result in invalidation, which would install the
6052 * first_validate_idle, which would validate onscreen;
6053 * but we're going to go ahead and validate here, so
6054 * first_validate_idle shouldn't have anything to do.
6056 gtk_text_view_update_layout_width (text_view);
6058 /* note that validation of onscreen could invoke this function
6059 * recursively, by scrolling to maintain first_para, or in response
6060 * to updating the layout width, however there is no problem with
6061 * that, or shouldn't be.
6063 gtk_text_view_validate_onscreen (text_view);
6065 /* process exposes */
6066 if (GTK_WIDGET_REALIZED (text_view))
6068 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6070 if (text_view->left_window)
6071 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6073 if (text_view->right_window)
6074 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6076 if (text_view->top_window)
6077 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6079 if (text_view->bottom_window)
6080 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6082 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6085 /* If this got installed, get rid of it, it's just a waste of time. */
6086 if (text_view->first_validate_idle != 0)
6088 g_source_remove (text_view->first_validate_idle);
6089 text_view->first_validate_idle = 0;
6092 gtk_text_view_update_im_spot_location (text_view);
6094 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6098 gtk_text_view_commit_handler (GtkIMContext *context,
6100 GtkTextView *text_view)
6102 gtk_text_view_commit_text (text_view, str);
6106 gtk_text_view_commit_text (GtkTextView *text_view,
6109 gboolean had_selection;
6111 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6113 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6116 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6117 text_view->editable);
6119 if (!strcmp (str, "\n"))
6121 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6122 text_view->editable);
6126 if (!had_selection && text_view->overwrite_mode)
6130 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6132 gtk_text_buffer_get_mark (get_buffer (text_view),
6134 if (!gtk_text_iter_ends_line (&insert))
6135 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6137 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6138 text_view->editable);
6141 gtk_text_buffer_end_user_action (get_buffer (text_view));
6143 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6144 gtk_text_view_scroll_mark_onscreen (text_view,
6145 gtk_text_buffer_get_mark (get_buffer (text_view),
6150 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6151 GtkTextView *text_view)
6154 PangoAttrList *attrs;
6157 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6158 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6160 pango_attr_list_unref (attrs);
6165 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6166 GtkTextView *text_view)
6173 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6174 gtk_text_buffer_get_insert (text_view->buffer));
6177 pos = gtk_text_iter_get_line_index (&start);
6178 gtk_text_iter_set_line_offset (&start, 0);
6179 gtk_text_iter_forward_to_line_end (&end);
6181 text = gtk_text_iter_get_slice (&start, &end);
6182 gtk_im_context_set_surrounding (context, text, -1, pos);
6189 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6192 GtkTextView *text_view)
6197 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6198 gtk_text_buffer_get_insert (text_view->buffer));
6201 gtk_text_iter_forward_chars (&start, offset);
6202 gtk_text_iter_forward_chars (&end, offset + n_chars);
6204 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6210 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6211 const GtkTextIter *location,
6215 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6216 gboolean need_reset = FALSE;
6218 if (mark == gtk_text_buffer_get_insert (buffer))
6220 text_view->virtual_cursor_x = -1;
6221 text_view->virtual_cursor_y = -1;
6222 gtk_text_view_update_im_spot_location (text_view);
6225 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6231 gtk_text_view_reset_im_context (text_view);
6235 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6239 GdkRectangle strong_pos;
6242 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6243 gtk_text_buffer_get_mark (get_buffer (text_view),
6246 if ((x && text_view->virtual_cursor_x == -1) ||
6247 (y && text_view->virtual_cursor_y == -1))
6248 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6252 if (text_view->virtual_cursor_x != -1)
6253 *x = text_view->virtual_cursor_x;
6260 if (text_view->virtual_cursor_x != -1)
6261 *y = text_view->virtual_cursor_y;
6263 *y = strong_pos.y + strong_pos.height / 2;
6268 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6272 GdkRectangle strong_pos;
6275 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6276 gtk_text_buffer_get_mark (get_buffer (text_view),
6279 if (x == -1 || y == -1)
6280 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6282 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6283 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6286 /* Quick hack of a popup menu
6289 activate_cb (GtkWidget *menuitem,
6290 GtkTextView *text_view)
6292 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6293 g_signal_emit_by_name (text_view, signal);
6297 append_action_signal (GtkTextView *text_view,
6299 const gchar *stock_id,
6300 const gchar *signal,
6303 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6305 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6306 g_signal_connect (menuitem, "activate",
6307 G_CALLBACK (activate_cb), text_view);
6309 gtk_widget_set_sensitive (menuitem, sensitive);
6311 gtk_widget_show (menuitem);
6312 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6316 popup_menu_detach (GtkWidget *attach_widget,
6319 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6323 popup_position_func (GtkMenu *menu,
6329 GtkTextView *text_view;
6331 GdkRectangle cursor_rect;
6332 GdkRectangle onscreen_rect;
6333 gint root_x, root_y;
6338 text_view = GTK_TEXT_VIEW (user_data);
6339 widget = GTK_WIDGET (text_view);
6341 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6343 screen = gtk_widget_get_screen (widget);
6345 gdk_window_get_origin (widget->window, &root_x, &root_y);
6347 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6349 gtk_text_buffer_get_insert (get_buffer (text_view)));
6351 gtk_text_view_get_iter_location (text_view,
6355 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6357 gtk_widget_size_request (text_view->popup_menu, &req);
6359 /* can't use rectangle_intersect since cursor rect can have 0 width */
6360 if (cursor_rect.x >= onscreen_rect.x &&
6361 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6362 cursor_rect.y >= onscreen_rect.y &&
6363 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6365 gtk_text_view_buffer_to_window_coords (text_view,
6366 GTK_TEXT_WINDOW_WIDGET,
6367 cursor_rect.x, cursor_rect.y,
6368 &cursor_rect.x, &cursor_rect.y);
6370 *x = root_x + cursor_rect.x + cursor_rect.width;
6371 *y = root_y + cursor_rect.y + cursor_rect.height;
6375 /* Just center the menu, since cursor is offscreen. */
6376 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6377 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6381 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6382 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6384 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6385 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6390 GtkTextView *text_view;
6396 range_contains_editable_text (const GtkTextIter *start,
6397 const GtkTextIter *end,
6398 gboolean default_editability)
6400 GtkTextIter iter = *start;
6402 while (gtk_text_iter_compare (&iter, end) < 0)
6404 if (gtk_text_iter_editable (&iter, default_editability))
6407 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6414 unichar_chosen_func (const char *text,
6417 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6419 gtk_text_view_commit_text (text_view, text);
6423 popup_targets_received (GtkClipboard *clipboard,
6424 GtkSelectionData *data,
6427 PopupInfo *info = user_data;
6428 GtkTextView *text_view = info->text_view;
6430 if (GTK_WIDGET_REALIZED (text_view))
6432 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6433 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6435 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6436 GtkWidget *menuitem;
6438 gboolean have_selection;
6439 gboolean can_insert;
6441 GtkTextIter sel_start, sel_end;
6443 if (text_view->popup_menu)
6444 gtk_widget_destroy (text_view->popup_menu);
6446 text_view->popup_menu = gtk_menu_new ();
6448 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6449 GTK_WIDGET (text_view),
6452 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6453 &sel_start, &sel_end);
6455 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6457 gtk_text_buffer_get_insert (get_buffer (text_view)));
6459 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6461 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6463 range_contains_editable_text (&sel_start, &sel_end,
6464 text_view->editable));
6465 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6467 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6468 can_insert && clipboard_contains_text);
6470 menuitem = gtk_separator_menu_item_new ();
6471 gtk_widget_show (menuitem);
6472 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6474 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6475 gtk_widget_show (menuitem);
6476 submenu = gtk_menu_new ();
6477 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6478 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6480 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6481 GTK_MENU_SHELL (submenu));
6483 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6484 gtk_widget_show (menuitem);
6485 gtk_widget_set_sensitive (menuitem, can_insert);
6487 submenu = gtk_menu_new ();
6488 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6489 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6491 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6492 unichar_chosen_func,
6495 g_signal_emit (text_view,
6496 signals[POPULATE_POPUP],
6498 text_view->popup_menu);
6501 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6503 info->button, info->time);
6506 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6507 popup_position_func, text_view,
6508 0, gtk_get_current_event_time ());
6509 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
6513 g_object_unref (text_view);
6518 gtk_text_view_do_popup (GtkTextView *text_view,
6519 GdkEventButton *event)
6521 PopupInfo *info = g_new (PopupInfo, 1);
6523 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6524 gtk_text_view_end_selection_drag (text_view, event);
6526 /* In order to know what entries we should make sensitive, we
6527 * ask for the current targets of the clipboard, and when
6528 * we get them, then we actually pop up the menu.
6530 info->text_view = g_object_ref (text_view);
6534 info->button = event->button;
6535 info->time = event->time;
6540 info->time = gtk_get_current_event_time ();
6543 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6544 GDK_SELECTION_CLIPBOARD),
6545 gdk_atom_intern ("TARGETS", FALSE),
6546 popup_targets_received,
6551 gtk_text_view_popup_menu (GtkWidget *widget)
6553 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6557 /* Child GdkWindows */
6560 static GtkTextWindow*
6561 text_window_new (GtkTextWindowType type,
6564 gint height_request)
6568 win = g_new (GtkTextWindow, 1);
6571 win->widget = widget;
6573 win->bin_window = NULL;
6574 win->requisition.width = width_request;
6575 win->requisition.height = height_request;
6576 win->allocation.width = width_request;
6577 win->allocation.height = height_request;
6578 win->allocation.x = 0;
6579 win->allocation.y = 0;
6585 text_window_free (GtkTextWindow *win)
6588 text_window_unrealize (win);
6594 text_window_realize (GtkTextWindow *win,
6597 GdkWindowAttr attributes;
6598 gint attributes_mask;
6601 attributes.window_type = GDK_WINDOW_CHILD;
6602 attributes.x = win->allocation.x;
6603 attributes.y = win->allocation.y;
6604 attributes.width = win->allocation.width;
6605 attributes.height = win->allocation.height;
6606 attributes.wclass = GDK_INPUT_OUTPUT;
6607 attributes.visual = gtk_widget_get_visual (win->widget);
6608 attributes.colormap = gtk_widget_get_colormap (win->widget);
6609 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6611 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6613 win->window = gdk_window_new (parent,
6617 gdk_window_show (win->window);
6618 gdk_window_set_user_data (win->window, win->widget);
6622 attributes.width = win->allocation.width;
6623 attributes.height = win->allocation.height;
6624 attributes.event_mask = (GDK_EXPOSURE_MASK |
6626 GDK_KEY_PRESS_MASK |
6627 GDK_BUTTON_PRESS_MASK |
6628 GDK_BUTTON_RELEASE_MASK |
6629 GDK_POINTER_MOTION_MASK |
6630 GDK_POINTER_MOTION_HINT_MASK |
6631 gtk_widget_get_events (win->widget));
6633 win->bin_window = gdk_window_new (win->window,
6637 gdk_window_show (win->bin_window);
6638 gdk_window_set_user_data (win->bin_window, win->widget);
6640 if (win->type == GTK_TEXT_WINDOW_TEXT)
6643 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
6645 gdk_window_set_cursor (win->bin_window, cursor);
6646 gdk_cursor_unref (cursor);
6648 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6652 gdk_window_set_background (win->bin_window,
6653 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6657 gdk_window_set_background (win->bin_window,
6658 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6661 g_object_set_qdata (G_OBJECT (win->window),
6662 g_quark_from_static_string ("gtk-text-view-text-window"),
6665 g_object_set_qdata (G_OBJECT (win->bin_window),
6666 g_quark_from_static_string ("gtk-text-view-text-window"),
6671 text_window_unrealize (GtkTextWindow *win)
6673 if (win->type == GTK_TEXT_WINDOW_TEXT)
6675 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6679 gdk_window_set_user_data (win->window, NULL);
6680 gdk_window_set_user_data (win->bin_window, NULL);
6681 gdk_window_destroy (win->bin_window);
6682 gdk_window_destroy (win->window);
6684 win->bin_window = NULL;
6688 text_window_size_allocate (GtkTextWindow *win,
6691 win->allocation = *rect;
6695 gdk_window_move_resize (win->window,
6697 rect->width, rect->height);
6699 gdk_window_resize (win->bin_window,
6700 rect->width, rect->height);
6705 text_window_scroll (GtkTextWindow *win,
6709 if (dx != 0 || dy != 0)
6711 gdk_window_scroll (win->bin_window, dx, dy);
6716 text_window_invalidate_rect (GtkTextWindow *win,
6719 GdkRectangle window_rect;
6721 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6728 window_rect.width = rect->width;
6729 window_rect.height = rect->height;
6731 /* Adjust the rect as appropriate */
6735 case GTK_TEXT_WINDOW_TEXT:
6738 case GTK_TEXT_WINDOW_LEFT:
6739 case GTK_TEXT_WINDOW_RIGHT:
6741 window_rect.width = win->allocation.width;
6744 case GTK_TEXT_WINDOW_TOP:
6745 case GTK_TEXT_WINDOW_BOTTOM:
6747 window_rect.height = win->allocation.height;
6751 g_warning ("%s: bug!", G_STRLOC);
6756 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6760 GdkColor color = { 0, 65535, 0, 0 };
6761 GdkGC *gc = gdk_gc_new (win->bin_window);
6762 gdk_gc_set_rgb_fg_color (gc, &color);
6763 gdk_draw_rectangle (win->bin_window,
6764 gc, TRUE, window_rect.x, window_rect.y,
6765 window_rect.width, window_rect.height);
6766 g_object_unref (gc);
6772 text_window_get_width (GtkTextWindow *win)
6774 return win->allocation.width;
6778 text_window_get_height (GtkTextWindow *win)
6780 return win->allocation.height;
6787 * gtk_text_view_get_window:
6788 * @text_view: a #GtkTextView
6789 * @win: window to get
6791 * Retrieves the #GdkWindow corresponding to an area of the text view;
6792 * possible windows include the overall widget window, child windows
6793 * on the left, right, top, bottom, and the window that displays the
6794 * text buffer. Windows are %NULL and nonexistent if their width or
6795 * height is 0, and are nonexistent before the widget has been
6798 * Return value: a #GdkWindow, or %NULL
6801 gtk_text_view_get_window (GtkTextView *text_view,
6802 GtkTextWindowType win)
6804 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6808 case GTK_TEXT_WINDOW_WIDGET:
6809 return GTK_WIDGET (text_view)->window;
6812 case GTK_TEXT_WINDOW_TEXT:
6813 return text_view->text_window->bin_window;
6816 case GTK_TEXT_WINDOW_LEFT:
6817 if (text_view->left_window)
6818 return text_view->left_window->bin_window;
6823 case GTK_TEXT_WINDOW_RIGHT:
6824 if (text_view->right_window)
6825 return text_view->right_window->bin_window;
6830 case GTK_TEXT_WINDOW_TOP:
6831 if (text_view->top_window)
6832 return text_view->top_window->bin_window;
6837 case GTK_TEXT_WINDOW_BOTTOM:
6838 if (text_view->bottom_window)
6839 return text_view->bottom_window->bin_window;
6844 case GTK_TEXT_WINDOW_PRIVATE:
6845 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6850 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6855 * gtk_text_view_get_window_type:
6856 * @text_view: a #GtkTextView
6857 * @window: a window type
6859 * Usually used to find out which window an event corresponds to.
6860 * If you connect to an event signal on @text_view, this function
6861 * should be called on <literal>event->window</literal> to
6862 * see which window it was.
6864 * Return value: the window type.
6867 gtk_text_view_get_window_type (GtkTextView *text_view,
6872 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6873 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6875 if (window == GTK_WIDGET (text_view)->window)
6876 return GTK_TEXT_WINDOW_WIDGET;
6878 win = g_object_get_qdata (G_OBJECT (window),
6879 g_quark_try_string ("gtk-text-view-text-window"));
6885 return GTK_TEXT_WINDOW_PRIVATE;
6890 buffer_to_widget (GtkTextView *text_view,
6898 *window_x = buffer_x - text_view->xoffset;
6899 *window_x += text_view->text_window->allocation.x;
6904 *window_y = buffer_y - text_view->yoffset;
6905 *window_y += text_view->text_window->allocation.y;
6910 widget_to_text_window (GtkTextWindow *win,
6917 *window_x = widget_x - win->allocation.x;
6920 *window_y = widget_y - win->allocation.y;
6924 buffer_to_text_window (GtkTextView *text_view,
6933 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6934 "for a nonexistent or private child window of GtkTextView");
6938 buffer_to_widget (text_view,
6940 window_x, window_y);
6942 widget_to_text_window (win,
6943 window_x ? *window_x : 0,
6944 window_y ? *window_y : 0,
6950 * gtk_text_view_buffer_to_window_coords:
6951 * @text_view: a #GtkTextView
6952 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6953 * @buffer_x: buffer x coordinate
6954 * @buffer_y: buffer y coordinate
6955 * @window_x: window x coordinate return location
6956 * @window_y: window y coordinate return location
6958 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6959 * @win, and stores the result in (@window_x, @window_y).
6961 * Note that you can't convert coordinates for a nonexisting window (see
6962 * gtk_text_view_set_border_window_size()).
6965 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6966 GtkTextWindowType win,
6972 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6976 case GTK_TEXT_WINDOW_WIDGET:
6977 buffer_to_widget (text_view,
6979 window_x, window_y);
6982 case GTK_TEXT_WINDOW_TEXT:
6984 *window_x = buffer_x - text_view->xoffset;
6986 *window_y = buffer_y - text_view->yoffset;
6989 case GTK_TEXT_WINDOW_LEFT:
6990 buffer_to_text_window (text_view,
6991 text_view->left_window,
6993 window_x, window_y);
6996 case GTK_TEXT_WINDOW_RIGHT:
6997 buffer_to_text_window (text_view,
6998 text_view->right_window,
7000 window_x, window_y);
7003 case GTK_TEXT_WINDOW_TOP:
7004 buffer_to_text_window (text_view,
7005 text_view->top_window,
7007 window_x, window_y);
7010 case GTK_TEXT_WINDOW_BOTTOM:
7011 buffer_to_text_window (text_view,
7012 text_view->bottom_window,
7014 window_x, window_y);
7017 case GTK_TEXT_WINDOW_PRIVATE:
7018 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7022 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7028 widget_to_buffer (GtkTextView *text_view,
7036 *buffer_x = widget_x + text_view->xoffset;
7037 *buffer_x -= text_view->text_window->allocation.x;
7042 *buffer_y = widget_y + text_view->yoffset;
7043 *buffer_y -= text_view->text_window->allocation.y;
7048 text_window_to_widget (GtkTextWindow *win,
7055 *widget_x = window_x + win->allocation.x;
7058 *widget_y = window_y + win->allocation.y;
7062 text_window_to_buffer (GtkTextView *text_view,
7071 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7072 "coordinates for a nonexistent child window.");
7076 text_window_to_widget (win,
7082 widget_to_buffer (text_view,
7083 buffer_x ? *buffer_x : 0,
7084 buffer_y ? *buffer_y : 0,
7090 * gtk_text_view_window_to_buffer_coords:
7091 * @text_view: a #GtkTextView
7092 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7093 * @window_x: window x coordinate
7094 * @window_y: window y coordinate
7095 * @buffer_x: buffer x coordinate return location
7096 * @buffer_y: buffer y coordinate return location
7098 * Converts coordinates on the window identified by @win to buffer
7099 * coordinates, storing the result in (@buffer_x,@buffer_y).
7101 * Note that you can't convert coordinates for a nonexisting window (see
7102 * gtk_text_view_set_border_window_size()).
7105 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7106 GtkTextWindowType win,
7112 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7116 case GTK_TEXT_WINDOW_WIDGET:
7117 widget_to_buffer (text_view,
7119 buffer_x, buffer_y);
7122 case GTK_TEXT_WINDOW_TEXT:
7124 *buffer_x = window_x + text_view->xoffset;
7126 *buffer_y = window_y + text_view->yoffset;
7129 case GTK_TEXT_WINDOW_LEFT:
7130 text_window_to_buffer (text_view,
7131 text_view->left_window,
7133 buffer_x, buffer_y);
7136 case GTK_TEXT_WINDOW_RIGHT:
7137 text_window_to_buffer (text_view,
7138 text_view->right_window,
7140 buffer_x, buffer_y);
7143 case GTK_TEXT_WINDOW_TOP:
7144 text_window_to_buffer (text_view,
7145 text_view->top_window,
7147 buffer_x, buffer_y);
7150 case GTK_TEXT_WINDOW_BOTTOM:
7151 text_window_to_buffer (text_view,
7152 text_view->bottom_window,
7154 buffer_x, buffer_y);
7157 case GTK_TEXT_WINDOW_PRIVATE:
7158 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7162 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7168 set_window_width (GtkTextView *text_view,
7170 GtkTextWindowType type,
7171 GtkTextWindow **winp)
7177 text_window_free (*winp);
7179 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7186 *winp = text_window_new (type,
7187 GTK_WIDGET (text_view),
7189 /* if the widget is already realized we need to realize the child manually */
7190 if (GTK_WIDGET_REALIZED (text_view))
7191 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7195 if ((*winp)->requisition.width == width)
7198 (*winp)->requisition.width = width;
7201 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7207 set_window_height (GtkTextView *text_view,
7209 GtkTextWindowType type,
7210 GtkTextWindow **winp)
7216 text_window_free (*winp);
7218 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7225 *winp = text_window_new (type,
7226 GTK_WIDGET (text_view),
7229 /* if the widget is already realized we need to realize the child manually */
7230 if (GTK_WIDGET_REALIZED (text_view))
7231 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7235 if ((*winp)->requisition.height == height)
7238 (*winp)->requisition.height = height;
7241 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7246 * gtk_text_view_set_border_window_size:
7247 * @text_view: a #GtkTextView
7248 * @type: window to affect
7249 * @size: width or height of the window
7251 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7252 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7253 * Automatically destroys the corresponding window if the size is set
7254 * to 0, and creates the window if the size is set to non-zero. This
7255 * function can only be used for the "border windows," it doesn't work
7256 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7257 * #GTK_TEXT_WINDOW_PRIVATE.
7260 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7261 GtkTextWindowType type,
7265 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7266 g_return_if_fail (size >= 0);
7270 case GTK_TEXT_WINDOW_LEFT:
7271 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7272 &text_view->left_window);
7275 case GTK_TEXT_WINDOW_RIGHT:
7276 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7277 &text_view->right_window);
7280 case GTK_TEXT_WINDOW_TOP:
7281 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7282 &text_view->top_window);
7285 case GTK_TEXT_WINDOW_BOTTOM:
7286 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7287 &text_view->bottom_window);
7291 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7297 * gtk_text_view_get_border_window_size:
7298 * @text_view: a #GtkTextView
7299 * @type: window to return size from
7301 * Gets the width of the specified border window. See
7302 * gtk_text_view_set_border_window_size().
7304 * Return value: width of window
7307 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7308 GtkTextWindowType type)
7310 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7314 case GTK_TEXT_WINDOW_LEFT:
7315 if (text_view->left_window)
7316 return text_view->left_window->requisition.width;
7318 case GTK_TEXT_WINDOW_RIGHT:
7319 if (text_view->right_window)
7320 return text_view->right_window->requisition.width;
7322 case GTK_TEXT_WINDOW_TOP:
7323 if (text_view->top_window)
7324 return text_view->top_window->requisition.height;
7326 case GTK_TEXT_WINDOW_BOTTOM:
7327 if (text_view->bottom_window)
7328 return text_view->bottom_window->requisition.height;
7331 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7342 static GtkTextViewChild*
7343 text_view_child_new_anchored (GtkWidget *child,
7344 GtkTextChildAnchor *anchor,
7345 GtkTextLayout *layout)
7347 GtkTextViewChild *vc;
7349 vc = g_new (GtkTextViewChild, 1);
7351 vc->type = GTK_TEXT_WINDOW_PRIVATE;
7353 vc->anchor = anchor;
7355 vc->from_top_of_line = 0;
7356 vc->from_left_of_buffer = 0;
7358 g_object_ref (vc->widget);
7359 g_object_ref (vc->anchor);
7361 g_object_set_data (G_OBJECT (child),
7362 "gtk-text-view-child",
7365 gtk_text_child_anchor_register_child (anchor, child, layout);
7370 static GtkTextViewChild*
7371 text_view_child_new_window (GtkWidget *child,
7372 GtkTextWindowType type,
7376 GtkTextViewChild *vc;
7378 vc = g_new (GtkTextViewChild, 1);
7383 vc->from_top_of_line = 0;
7384 vc->from_left_of_buffer = 0;
7386 g_object_ref (vc->widget);
7392 g_object_set_data (G_OBJECT (child),
7393 "gtk-text-view-child",
7400 text_view_child_free (GtkTextViewChild *child)
7402 g_object_set_data (G_OBJECT (child->widget),
7403 "gtk-text-view-child", NULL);
7407 gtk_text_child_anchor_unregister_child (child->anchor,
7409 g_object_unref (child->anchor);
7412 g_object_unref (child->widget);
7418 text_view_child_set_parent_window (GtkTextView *text_view,
7419 GtkTextViewChild *vc)
7422 gtk_widget_set_parent_window (vc->widget,
7423 text_view->text_window->bin_window);
7427 window = gtk_text_view_get_window (text_view,
7429 gtk_widget_set_parent_window (vc->widget, window);
7434 add_child (GtkTextView *text_view,
7435 GtkTextViewChild *vc)
7437 text_view->children = g_slist_prepend (text_view->children,
7440 if (GTK_WIDGET_REALIZED (text_view))
7441 text_view_child_set_parent_window (text_view, vc);
7443 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7447 * gtk_text_view_add_child_at_anchor:
7448 * @text_view: a #GtkTextView
7449 * @child: a #GtkWidget
7450 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7452 * Adds a child widget in the text buffer, at the given @anchor.
7456 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7458 GtkTextChildAnchor *anchor)
7460 GtkTextViewChild *vc;
7462 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7463 g_return_if_fail (GTK_IS_WIDGET (child));
7464 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7465 g_return_if_fail (child->parent == NULL);
7467 gtk_text_view_ensure_layout (text_view);
7469 vc = text_view_child_new_anchored (child, anchor,
7472 add_child (text_view, vc);
7474 g_assert (vc->widget == child);
7475 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7479 * gtk_text_view_add_child_in_window:
7480 * @text_view: a #GtkTextView
7481 * @child: a #GtkWidget
7482 * @which_window: which window the child should appear in
7483 * @xpos: X position of child in window coordinates
7484 * @ypos: Y position of child in window coordinates
7486 * Adds a child at fixed coordinates in one of the text widget's
7487 * windows. The window must have nonzero size (see
7488 * gtk_text_view_set_border_window_size()). Note that the child
7489 * coordinates are given relative to the #GdkWindow in question, and
7490 * that these coordinates have no sane relationship to scrolling. When
7491 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7492 * irrelevant, the child floats above all scrollable areas. But when
7493 * placing a child in one of the scrollable windows (border windows or
7494 * text window), you'll need to compute the child's correct position
7495 * in buffer coordinates any time scrolling occurs or buffer changes
7496 * occur, and then call gtk_text_view_move_child() to update the
7497 * child's position. Unfortunately there's no good way to detect that
7498 * scrolling has occurred, using the current API; a possible hack
7499 * would be to update all child positions when the scroll adjustments
7500 * change or the text buffer changes. See bug 64518 on
7501 * bugzilla.gnome.org for status of fixing this issue.
7505 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7507 GtkTextWindowType which_window,
7511 GtkTextViewChild *vc;
7513 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7514 g_return_if_fail (GTK_IS_WIDGET (child));
7515 g_return_if_fail (child->parent == NULL);
7517 vc = text_view_child_new_window (child, which_window,
7520 add_child (text_view, vc);
7522 g_assert (vc->widget == child);
7523 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7527 * gtk_text_view_move_child:
7528 * @text_view: a #GtkTextView
7529 * @child: child widget already added to the text view
7530 * @xpos: new X position in window coordinates
7531 * @ypos: new Y position in window coordinates
7533 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7537 gtk_text_view_move_child (GtkTextView *text_view,
7542 GtkTextViewChild *vc;
7544 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7545 g_return_if_fail (GTK_IS_WIDGET (child));
7546 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7548 vc = g_object_get_data (G_OBJECT (child),
7549 "gtk-text-view-child");
7551 g_assert (vc != NULL);
7553 if (vc->x == xpos &&
7560 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7561 gtk_widget_queue_resize (child);
7565 /* Iterator operations */
7568 * gtk_text_view_forward_display_line:
7569 * @text_view: a #GtkTextView
7570 * @iter: a #GtkTextIter
7572 * Moves the given @iter forward by one display (wrapped) line. A
7573 * display line is different from a paragraph. Paragraphs are
7574 * separated by newlines or other paragraph separator characters.
7575 * Display lines are created by line-wrapping a paragraph. If
7576 * wrapping is turned off, display lines and paragraphs will be the
7577 * same. Display lines are divided differently for each view, since
7578 * they depend on the view's width; paragraphs are the same in all
7579 * views, since they depend on the contents of the #GtkTextBuffer.
7581 * Return value: %TRUE if @iter was moved and is not on the end iterator
7584 gtk_text_view_forward_display_line (GtkTextView *text_view,
7587 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7588 g_return_val_if_fail (iter != NULL, FALSE);
7590 gtk_text_view_ensure_layout (text_view);
7592 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7596 * gtk_text_view_backward_display_line:
7597 * @text_view: a #GtkTextView
7598 * @iter: a #GtkTextIter
7600 * Moves the given @iter backward by one display (wrapped) line. A
7601 * display line is different from a paragraph. Paragraphs are
7602 * separated by newlines or other paragraph separator characters.
7603 * Display lines are created by line-wrapping a paragraph. If
7604 * wrapping is turned off, display lines and paragraphs will be the
7605 * same. Display lines are divided differently for each view, since
7606 * they depend on the view's width; paragraphs are the same in all
7607 * views, since they depend on the contents of the #GtkTextBuffer.
7609 * Return value: %TRUE if @iter was moved and is not on the end iterator
7612 gtk_text_view_backward_display_line (GtkTextView *text_view,
7615 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7616 g_return_val_if_fail (iter != NULL, FALSE);
7618 gtk_text_view_ensure_layout (text_view);
7620 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7624 * gtk_text_view_forward_display_line_end:
7625 * @text_view: a #GtkTextView
7626 * @iter: a #GtkTextIter
7628 * Moves the given @iter forward to the next display line end. A
7629 * display line is different from a paragraph. Paragraphs are
7630 * separated by newlines or other paragraph separator characters.
7631 * Display lines are created by line-wrapping a paragraph. If
7632 * wrapping is turned off, display lines and paragraphs will be the
7633 * same. Display lines are divided differently for each view, since
7634 * they depend on the view's width; paragraphs are the same in all
7635 * views, since they depend on the contents of the #GtkTextBuffer.
7637 * Return value: %TRUE if @iter was moved and is not on the end iterator
7640 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7643 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7644 g_return_val_if_fail (iter != NULL, FALSE);
7646 gtk_text_view_ensure_layout (text_view);
7648 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7652 * gtk_text_view_backward_display_line_start:
7653 * @text_view: a #GtkTextView
7654 * @iter: a #GtkTextIter
7656 * Moves the given @iter backward to the next display line start. A
7657 * display line is different from a paragraph. Paragraphs are
7658 * separated by newlines or other paragraph separator characters.
7659 * Display lines are created by line-wrapping a paragraph. If
7660 * wrapping is turned off, display lines and paragraphs will be the
7661 * same. Display lines are divided differently for each view, since
7662 * they depend on the view's width; paragraphs are the same in all
7663 * views, since they depend on the contents of the #GtkTextBuffer.
7665 * Return value: %TRUE if @iter was moved and is not on the end iterator
7668 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7671 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7672 g_return_val_if_fail (iter != NULL, FALSE);
7674 gtk_text_view_ensure_layout (text_view);
7676 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7680 * gtk_text_view_starts_display_line:
7681 * @text_view: a #GtkTextView
7682 * @iter: a #GtkTextIter
7684 * Determines whether @iter is at the start of a display line.
7685 * See gtk_text_view_forward_display_line() for an explanation of
7686 * display lines vs. paragraphs.
7688 * Return value: %TRUE if @iter begins a wrapped line
7691 gtk_text_view_starts_display_line (GtkTextView *text_view,
7692 const GtkTextIter *iter)
7694 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7695 g_return_val_if_fail (iter != NULL, FALSE);
7697 gtk_text_view_ensure_layout (text_view);
7699 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7703 * gtk_text_view_move_visually:
7704 * @text_view: a #GtkTextView
7705 * @iter: a #GtkTextIter
7706 * @count: number of lines to move
7708 * Moves @iter up or down by @count display (wrapped) lines.
7709 * See gtk_text_view_forward_display_line() for an explanation of
7710 * display lines vs. paragraphs.
7712 * Return value: %TRUE if @iter moved and is not on the end iterator
7715 gtk_text_view_move_visually (GtkTextView *text_view,
7719 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7720 g_return_val_if_fail (iter != NULL, FALSE);
7722 gtk_text_view_ensure_layout (text_view);
7724 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);