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);
3824 else if ((event->type == GDK_2BUTTON_PRESS ||
3825 event->type == GDK_3BUTTON_PRESS) &&
3828 GtkTextIter start, end;
3830 /* End the selection drag, otherwise we'd clear the new
3831 * word/line selection on button release
3833 gtk_text_view_end_selection_drag (text_view, event);
3835 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3837 event->x + text_view->xoffset,
3838 event->y + text_view->yoffset);
3842 if (event->type == GDK_2BUTTON_PRESS)
3844 if (gtk_text_iter_inside_word (&start))
3846 if (!gtk_text_iter_starts_word (&start))
3847 gtk_text_iter_backward_word_start (&start);
3849 if (!gtk_text_iter_ends_word (&end))
3850 gtk_text_iter_forward_word_end (&end);
3853 else if (event->type == GDK_3BUTTON_PRESS)
3855 if (gtk_text_view_starts_display_line (text_view, &start))
3857 /* If on a display line boundary, we assume the user
3858 * clicked off the end of a line and we therefore select
3859 * the line before the boundary.
3861 gtk_text_view_backward_display_line_start (text_view, &start);
3865 /* start isn't on the start of a line, so we move it to the
3866 * start, and move end to the end unless it's already there.
3868 gtk_text_view_backward_display_line_start (text_view, &start);
3870 if (!gtk_text_view_starts_display_line (text_view, &end))
3871 gtk_text_view_forward_display_line_end (text_view, &end);
3875 if (event->state & GDK_SHIFT_MASK)
3877 /* Take union of old and new selection */
3878 GtkTextIter old_start, old_end;
3880 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3881 &old_start, &old_end);
3883 gtk_text_iter_order (&start, &old_start);
3884 gtk_text_iter_order (&old_end, &end);
3886 /* Now start is the first of the starts, and end is the
3891 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3894 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3898 text_view->just_selected_element = TRUE;
3907 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3909 GtkTextView *text_view;
3911 text_view = GTK_TEXT_VIEW (widget);
3913 if (event->window != text_view->text_window->bin_window)
3916 if (event->button == 1)
3918 if (text_view->drag_start_x >= 0)
3920 text_view->drag_start_x = -1;
3921 text_view->drag_start_y = -1;
3924 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3926 else if (text_view->just_selected_element)
3928 text_view->just_selected_element = FALSE;
3931 else if (text_view->pending_place_cursor_button == event->button)
3935 /* Unselect everything; we clicked inside selection, but
3936 * didn't move by the drag threshold, so just clear selection
3939 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3941 event->x + text_view->xoffset,
3942 event->y + text_view->yoffset);
3944 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3946 text_view->pending_place_cursor_button = 0;
3956 keymap_direction_changed (GdkKeymap *keymap,
3957 GtkTextView *text_view)
3959 gtk_text_view_check_keymap_direction (text_view);
3963 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3965 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3967 gtk_widget_queue_draw (widget);
3969 DV(g_print (G_STRLOC": focus_in_event\n"));
3971 if (text_view->cursor_visible && text_view->layout)
3973 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3974 gtk_text_view_check_cursor_blink (text_view);
3977 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3978 "direction_changed",
3979 G_CALLBACK (keymap_direction_changed), text_view);
3980 gtk_text_view_check_keymap_direction (text_view);
3982 text_view->need_im_reset = TRUE;
3983 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3989 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3991 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3993 gtk_widget_queue_draw (widget);
3995 DV(g_print (G_STRLOC": focus_out_event\n"));
3997 if (text_view->cursor_visible && text_view->layout)
3999 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4000 gtk_text_view_check_cursor_blink (text_view);
4003 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4004 keymap_direction_changed,
4007 text_view->need_im_reset = TRUE;
4008 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4014 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4016 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4018 if (text_view->mouse_cursor_obscured)
4022 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4024 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4025 gdk_cursor_unref (cursor);
4026 text_view->mouse_cursor_obscured = FALSE;
4029 if (event->window == text_view->text_window->bin_window &&
4030 text_view->drag_start_x >= 0)
4034 gdk_window_get_pointer (text_view->text_window->bin_window,
4037 if (gtk_drag_check_threshold (widget,
4038 text_view->drag_start_x,
4039 text_view->drag_start_y,
4043 gint buffer_x, buffer_y;
4045 gtk_text_view_window_to_buffer_coords (text_view,
4046 GTK_TEXT_WINDOW_TEXT,
4047 text_view->drag_start_x,
4048 text_view->drag_start_y,
4052 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4054 buffer_x, buffer_y);
4056 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4065 gtk_text_view_paint (GtkWidget *widget,
4067 GdkEventExpose *event)
4069 GtkTextView *text_view;
4070 GList *child_exposes;
4074 text_view = GTK_TEXT_VIEW (widget);
4076 g_return_if_fail (text_view->layout != NULL);
4077 g_return_if_fail (text_view->xoffset >= 0);
4078 g_return_if_fail (text_view->yoffset >= 0);
4080 while (text_view->first_validate_idle != 0)
4082 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4083 text_view->first_validate_idle));
4084 gtk_text_view_flush_first_validate (text_view);
4087 /* More regions could have become invalid in the above loop */
4088 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4093 gdk_region_get_clipbox (updates, &rect);
4095 gdk_rectangle_union (area, &rect, area);
4097 gdk_region_destroy (updates);
4100 if (!text_view->onscreen_validated)
4102 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.");
4103 g_assert_not_reached ();
4107 printf ("painting %d,%d %d x %d\n",
4109 area->width, area->height);
4112 child_exposes = NULL;
4113 gtk_text_layout_draw (text_view->layout,
4115 text_view->text_window->bin_window,
4120 area->width, area->height,
4123 tmp_list = child_exposes;
4124 while (tmp_list != NULL)
4126 GtkWidget *child = tmp_list->data;
4128 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4132 g_object_unref (child);
4134 tmp_list = tmp_list->next;
4137 g_list_free (child_exposes);
4141 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4145 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4146 GTK_TEXT_WINDOW_TEXT))
4148 DV(g_print (">Exposed ("G_STRLOC")\n"));
4149 gtk_text_view_paint (widget, &event->area, event);
4152 if (event->window == widget->window)
4153 gtk_text_view_draw_focus (widget);
4155 /* Propagate exposes to all children not in the buffer. */
4156 tmp_list = GTK_TEXT_VIEW (widget)->children;
4157 while (tmp_list != NULL)
4159 GtkTextViewChild *vc = tmp_list->data;
4161 /* propagate_expose checks that event->window matches
4164 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4165 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4169 tmp_list = tmp_list->next;
4176 gtk_text_view_draw_focus (GtkWidget *widget)
4178 gboolean interior_focus;
4180 /* We clear the focus if we are in interior focus mode. */
4181 gtk_widget_style_get (widget,
4182 "interior_focus", &interior_focus,
4185 if (GTK_WIDGET_DRAWABLE (widget))
4187 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4189 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4190 NULL, widget, "textview",
4192 widget->allocation.width,
4193 widget->allocation.height);
4197 gdk_window_clear (widget->window);
4203 gtk_text_view_grab_focus (GtkWidget *widget)
4205 GtkTextView *text_view;
4207 text_view = GTK_TEXT_VIEW (widget);
4209 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4211 if (!text_view->disable_scroll_on_focus)
4212 gtk_text_view_scroll_mark_onscreen (text_view,
4213 gtk_text_buffer_get_mark (get_buffer (text_view),
4218 gtk_text_view_focus (GtkWidget *widget,
4219 GtkDirectionType direction)
4221 GtkTextView *text_view;
4222 GtkContainer *container;
4225 text_view = GTK_TEXT_VIEW (widget);
4226 container = GTK_CONTAINER (widget);
4228 if (!gtk_widget_is_focus (widget) &&
4229 container->focus_child == NULL)
4231 gtk_widget_grab_focus (widget);
4237 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4238 * children to get the focus
4240 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4241 result = GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4242 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4253 gtk_text_view_add (GtkContainer *container,
4256 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4257 g_return_if_fail (GTK_IS_WIDGET (child));
4259 /* This is pretty random. */
4260 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4262 GTK_TEXT_WINDOW_WIDGET,
4267 gtk_text_view_remove (GtkContainer *container,
4271 GtkTextView *text_view;
4272 GtkTextViewChild *vc;
4274 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4275 g_return_if_fail (GTK_IS_WIDGET (child));
4276 g_return_if_fail (child->parent == (GtkWidget*) container);
4278 text_view = GTK_TEXT_VIEW (container);
4281 iter = text_view->children;
4283 while (iter != NULL)
4287 if (vc->widget == child)
4290 iter = g_slist_next (iter);
4293 g_assert (iter != NULL); /* be sure we had the child in the list */
4295 text_view->children = g_slist_remove (text_view->children, vc);
4297 gtk_widget_unparent (vc->widget);
4299 text_view_child_free (vc);
4303 gtk_text_view_forall (GtkContainer *container,
4304 gboolean include_internals,
4305 GtkCallback callback,
4306 gpointer callback_data)
4309 GtkTextView *text_view;
4312 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4313 g_return_if_fail (callback != NULL);
4315 text_view = GTK_TEXT_VIEW (container);
4317 copy = g_slist_copy (text_view->children);
4320 while (iter != NULL)
4322 GtkTextViewChild *vc = iter->data;
4324 (* callback) (vc->widget, callback_data);
4326 iter = g_slist_next (iter);
4329 g_slist_free (copy);
4332 #define CURSOR_ON_MULTIPLIER 0.66
4333 #define CURSOR_OFF_MULTIPLIER 0.34
4334 #define CURSOR_PEND_MULTIPLIER 1.0
4337 cursor_blinks (GtkTextView *text_view)
4339 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4342 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4345 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4348 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4353 get_cursor_time (GtkTextView *text_view)
4355 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4358 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4368 blink_cb (gpointer data)
4370 GtkTextView *text_view;
4373 GDK_THREADS_ENTER ();
4375 text_view = GTK_TEXT_VIEW (data);
4377 g_assert (text_view->layout);
4378 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4379 g_assert (text_view->cursor_visible);
4381 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4384 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4388 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4392 gtk_text_layout_set_cursor_visible (text_view->layout,
4395 GDK_THREADS_LEAVE ();
4397 /* Remove ourselves */
4403 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4405 if (text_view->blink_timeout)
4407 gtk_timeout_remove (text_view->blink_timeout);
4408 text_view->blink_timeout = 0;
4413 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4415 if (text_view->layout != NULL &&
4416 text_view->cursor_visible &&
4417 GTK_WIDGET_HAS_FOCUS (text_view))
4419 if (cursor_blinks (text_view))
4421 if (text_view->blink_timeout == 0)
4423 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4425 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4431 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4435 gtk_text_view_stop_cursor_blink (text_view);
4440 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4442 if (text_view->layout != NULL &&
4443 text_view->cursor_visible &&
4444 GTK_WIDGET_HAS_FOCUS (text_view) &&
4445 cursor_blinks (text_view))
4447 if (text_view->blink_timeout != 0)
4449 gtk_timeout_remove (text_view->blink_timeout);
4450 text_view->blink_timeout = 0;
4453 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4455 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4463 * Key binding handlers
4467 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4468 GtkTextIter *newplace,
4473 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4479 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4485 move_cursor (GtkTextView *text_view,
4486 const GtkTextIter *new_location,
4487 gboolean extend_selection)
4489 if (extend_selection)
4490 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4494 gtk_text_buffer_place_cursor (get_buffer (text_view),
4498 /* FIXME when we are unfrozen and can change GtkMovementStep,
4501 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4504 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4505 GtkMovementStep step,
4507 gboolean extend_selection)
4510 GtkTextIter newplace;
4512 gint cursor_x_pos = 0;
4514 gtk_text_view_reset_im_context (text_view);
4516 if (step == GTK_MOVEMENT_PAGES)
4518 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4519 gtk_text_view_pend_cursor_blink (text_view);
4522 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4524 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4525 gtk_text_view_pend_cursor_blink (text_view);
4529 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4530 gtk_text_buffer_get_mark (get_buffer (text_view),
4534 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4535 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4539 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4540 gtk_text_iter_forward_cursor_positions (&newplace, count);
4543 case GTK_MOVEMENT_VISUAL_POSITIONS:
4544 gtk_text_layout_move_iter_visually (text_view->layout,
4548 case GTK_MOVEMENT_WORDS:
4550 gtk_text_iter_backward_word_starts (&newplace, -count);
4552 gtk_text_iter_forward_word_ends (&newplace, count);
4555 case GTK_MOVEMENT_DISPLAY_LINES:
4556 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4557 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4560 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4562 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4563 else if (count < -1)
4564 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4567 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4570 case GTK_MOVEMENT_PARAGRAPHS:
4573 if (!gtk_text_iter_ends_line (&newplace))
4575 gtk_text_iter_forward_to_line_end (&newplace);
4581 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4583 gtk_text_iter_set_line_offset (&newplace, 0);
4590 gtk_text_iter_forward_lines (&newplace, count);
4591 gtk_text_iter_set_line_offset (&newplace, 0);
4595 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4598 if (!gtk_text_iter_ends_line (&newplace))
4599 gtk_text_iter_forward_to_line_end (&newplace);
4603 gtk_text_iter_set_line_offset (&newplace, 0);
4607 case GTK_MOVEMENT_BUFFER_ENDS:
4609 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4611 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4618 if (!gtk_text_iter_equal (&insert, &newplace))
4620 move_cursor (text_view, &newplace, extend_selection);
4622 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4623 gtk_text_view_scroll_mark_onscreen (text_view,
4624 gtk_text_buffer_get_mark (get_buffer (text_view),
4627 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4629 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4633 gtk_text_view_pend_cursor_blink (text_view);
4637 gtk_text_view_move_cursor (GtkTextView *text_view,
4638 GtkMovementStep step,
4640 gboolean extend_selection)
4642 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4646 gtk_text_view_page_horizontally (GtkTextView *text_view,
4648 gboolean extend_selection)
4650 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4651 count, extend_selection);
4655 gtk_text_view_set_anchor (GtkTextView *text_view)
4659 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4660 gtk_text_buffer_get_mark (get_buffer (text_view),
4663 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4667 gtk_text_view_scroll_pages (GtkTextView *text_view,
4669 gboolean extend_selection)
4674 gint cursor_x_pos, cursor_y_pos;
4675 GtkTextIter new_insert;
4679 g_return_if_fail (text_view->vadjustment != NULL);
4681 cancel_pending_scroll (text_view);
4683 adj = text_view->vadjustment;
4685 /* Validate the region that will be brought into view by the cursor motion
4689 gtk_text_view_get_first_para_iter (text_view, &anchor);
4690 y0 = adj->page_size;
4691 y1 = adj->page_size + count * adj->page_increment;
4695 gtk_text_view_get_first_para_iter (text_view, &anchor);
4696 y0 = count * adj->page_increment + adj->page_size;
4700 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4701 /* FIXME do we need to update the adjustment ranges here? */
4703 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4705 /* already at top, just be sure we are at offset 0 */
4706 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4707 move_cursor (text_view, &new_insert, extend_selection);
4709 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4711 /* already at bottom, just be sure we are at the end */
4712 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4713 move_cursor (text_view, &new_insert, extend_selection);
4717 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4719 newval = adj->value;
4720 oldval = adj->value;
4722 newval += count * adj->page_increment;
4724 set_adjustment_clamped (adj, newval);
4725 cursor_y_pos += adj->value - oldval;
4727 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4728 clamp_iter_onscreen (text_view, &new_insert);
4729 move_cursor (text_view, &new_insert, extend_selection);
4731 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4734 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4735 * only guarantees 1 pixel onscreen.
4737 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4738 gtk_text_view_scroll_mark_onscreen (text_view,
4739 gtk_text_buffer_get_mark (get_buffer (text_view),
4744 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4746 gboolean extend_selection)
4751 gint cursor_x_pos, cursor_y_pos;
4752 GtkTextIter new_insert;
4755 g_return_if_fail (text_view->hadjustment != NULL);
4757 cancel_pending_scroll (text_view);
4759 adj = text_view->hadjustment;
4761 /* Validate the line that we're moving within.
4763 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4765 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4766 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4767 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4768 /* FIXME do we need to update the adjustment ranges here? */
4770 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4772 /* already at far left, just be sure we are at offset 0 */
4773 gtk_text_iter_set_line_offset (&new_insert, 0);
4774 move_cursor (text_view, &new_insert, extend_selection);
4776 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4778 /* already at far right, just be sure we are at the end */
4779 gtk_text_iter_forward_to_line_end (&new_insert);
4780 move_cursor (text_view, &new_insert, extend_selection);
4784 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4786 newval = adj->value;
4787 oldval = adj->value;
4789 newval += count * adj->page_increment;
4791 set_adjustment_clamped (adj, newval);
4792 cursor_x_pos += adj->value - oldval;
4794 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4795 clamp_iter_onscreen (text_view, &new_insert);
4796 move_cursor (text_view, &new_insert, extend_selection);
4798 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4801 /* FIXME for lines shorter than the overall widget width, this results in a
4802 * "bounce" effect as we scroll to the right of the widget, then scroll
4803 * back to get the end of the line onscreen.
4804 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4807 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4808 * only guarantees 1 pixel onscreen.
4810 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4811 gtk_text_view_scroll_mark_onscreen (text_view,
4812 gtk_text_buffer_get_mark (get_buffer (text_view),
4817 whitespace (gunichar ch, gpointer user_data)
4819 return (ch == ' ' || ch == '\t');
4823 not_whitespace (gunichar ch, gpointer user_data)
4825 return !whitespace (ch, user_data);
4829 find_whitepace_region (const GtkTextIter *center,
4830 GtkTextIter *start, GtkTextIter *end)
4835 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4836 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4837 if (whitespace (gtk_text_iter_get_char (end), NULL))
4838 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4840 return !gtk_text_iter_equal (start, end);
4844 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4847 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4848 text_view->editable);
4852 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4859 gboolean leave_one = FALSE;
4861 gtk_text_view_reset_im_context (text_view);
4863 if (type == GTK_DELETE_CHARS)
4865 /* Char delete deletes the selection, if one exists */
4866 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4867 text_view->editable))
4871 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4873 gtk_text_buffer_get_mark (get_buffer (text_view),
4881 case GTK_DELETE_CHARS:
4882 gtk_text_iter_forward_cursor_positions (&end, count);
4885 case GTK_DELETE_WORD_ENDS:
4887 gtk_text_iter_forward_word_ends (&end, count);
4889 gtk_text_iter_backward_word_starts (&start, 0 - count);
4892 case GTK_DELETE_WORDS:
4895 case GTK_DELETE_DISPLAY_LINE_ENDS:
4898 case GTK_DELETE_DISPLAY_LINES:
4901 case GTK_DELETE_PARAGRAPH_ENDS:
4902 /* If we're already at a newline, we need to
4903 * simply delete that newline, instead of
4904 * moving to the next one.
4906 if (gtk_text_iter_ends_line (&end))
4908 gtk_text_iter_forward_line (&end);
4914 if (!gtk_text_iter_forward_to_line_end (&end))
4920 /* FIXME figure out what a negative count means
4924 case GTK_DELETE_PARAGRAPHS:
4927 gtk_text_iter_set_line_offset (&start, 0);
4928 gtk_text_iter_forward_to_line_end (&end);
4930 /* Do the lines beyond the first. */
4933 gtk_text_iter_forward_to_line_end (&end);
4939 /* FIXME negative count? */
4943 case GTK_DELETE_WHITESPACE:
4945 find_whitepace_region (&insert, &start, &end);
4953 if (!gtk_text_iter_equal (&start, &end))
4955 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4957 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4958 text_view->editable))
4961 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4963 text_view->editable);
4966 gtk_text_buffer_end_user_action (get_buffer (text_view));
4968 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4969 gtk_text_view_scroll_mark_onscreen (text_view,
4970 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4975 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4977 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4978 GDK_SELECTION_CLIPBOARD);
4980 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4982 text_view->editable);
4983 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4984 gtk_text_view_scroll_mark_onscreen (text_view,
4985 gtk_text_buffer_get_mark (get_buffer (text_view),
4990 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4992 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4993 GDK_SELECTION_CLIPBOARD);
4995 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4997 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4998 gtk_text_view_scroll_mark_onscreen (text_view,
4999 gtk_text_buffer_get_mark (get_buffer (text_view),
5004 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5006 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5007 GDK_SELECTION_CLIPBOARD);
5009 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5012 text_view->editable);
5013 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5014 gtk_text_view_scroll_mark_onscreen (text_view,
5015 gtk_text_buffer_get_mark (get_buffer (text_view),
5020 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5022 text_view->overwrite_mode = !text_view->overwrite_mode;
5026 gtk_text_view_move_focus (GtkTextView *text_view,
5027 GtkDirectionType direction_type)
5029 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5031 if (!GTK_WIDGET_TOPLEVEL (toplevel))
5034 /* Propagate to toplevel */
5035 g_signal_emit_by_name (toplevel, "move_focus", direction_type);
5043 gtk_text_view_unselect (GtkTextView *text_view)
5047 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5049 gtk_text_buffer_get_mark (get_buffer (text_view),
5052 gtk_text_buffer_move_mark (get_buffer (text_view),
5053 gtk_text_buffer_get_mark (get_buffer (text_view),
5059 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5060 const gchar *mark_name)
5063 GdkModifierType state;
5064 GtkTextIter newplace;
5066 /* DV(g_print (G_STRLOC": begin\n")); */
5068 gdk_window_get_pointer (text_view->text_window->bin_window,
5071 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5072 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5074 x + text_view->xoffset,
5075 y + text_view->yoffset);
5079 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5081 /* This may invalidate the layout */
5082 DV(g_print (G_STRLOC": move mark\n"));
5083 gtk_text_buffer_move_mark (get_buffer (text_view),
5087 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5088 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5091 DV (g_print ("first validate idle leaving %s is %d\n",
5092 G_STRLOC, text_view->first_validate_idle));
5096 selection_scan_timeout (gpointer data)
5098 GtkTextView *text_view;
5100 GDK_THREADS_ENTER ();
5102 text_view = GTK_TEXT_VIEW (data);
5104 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5105 move_mark_to_pointer_and_scroll (text_view, "insert");
5107 GDK_THREADS_LEAVE ();
5109 return TRUE; /* remain installed. */
5112 #define DND_SCROLL_MARGIN 0.20
5115 drag_scan_timeout (gpointer data)
5117 GtkTextView *text_view;
5119 GdkModifierType state;
5120 GtkTextIter newplace;
5122 GDK_THREADS_ENTER ();
5124 text_view = GTK_TEXT_VIEW (data);
5126 gdk_window_get_pointer (text_view->text_window->bin_window,
5129 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5131 x + text_view->xoffset,
5132 y + text_view->yoffset);
5134 gtk_text_buffer_move_mark (get_buffer (text_view),
5135 text_view->dnd_mark,
5138 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5139 gtk_text_view_scroll_to_mark (text_view,
5140 text_view->dnd_mark,
5141 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5143 GDK_THREADS_LEAVE ();
5149 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5151 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5152 move_mark_to_pointer_and_scroll (text_view, "insert");
5154 /* If we had to scroll offscreen, insert a timeout to do so
5155 * again. Note that in the timeout, even if the mouse doesn't
5156 * move, due to this scroll xoffset/yoffset will have changed
5157 * and we'll need to scroll again.
5159 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5160 gtk_timeout_remove (text_view->scroll_timeout);
5162 text_view->scroll_timeout =
5163 gtk_timeout_add (50, selection_scan_timeout, text_view);
5169 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5170 const GtkTextIter *iter,
5171 GdkEventButton *button)
5173 GtkTextIter newplace;
5174 GtkTextBuffer *buffer;
5176 g_return_if_fail (text_view->selection_drag_handler == 0);
5178 gtk_grab_add (GTK_WIDGET (text_view));
5180 buffer = get_buffer (text_view);
5184 if (button->state & GDK_SHIFT_MASK)
5186 /* Extend selection */
5187 GtkTextIter start, end;
5189 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5191 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5193 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5196 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5199 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5201 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5204 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5210 /* Replace selection */
5211 gtk_text_buffer_place_cursor (buffer, &newplace);
5214 text_view->selection_drag_handler = g_signal_connect (text_view,
5215 "motion_notify_event",
5216 G_CALLBACK (selection_motion_event_handler),
5220 /* returns whether we were really dragging */
5222 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5224 if (text_view->selection_drag_handler == 0)
5227 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
5228 text_view->selection_drag_handler = 0;
5230 if (text_view->scroll_timeout != 0)
5232 gtk_timeout_remove (text_view->scroll_timeout);
5233 text_view->scroll_timeout = 0;
5236 /* one last update to current position */
5237 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5238 move_mark_to_pointer_and_scroll (text_view, "insert");
5240 gtk_grab_remove (GTK_WIDGET (text_view));
5250 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5251 GtkTextAttributes *values,
5254 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5255 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5258 pango_font_description_free (values->font);
5260 values->font = pango_font_description_copy (style->font_desc);
5264 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5266 if (text_view->layout)
5268 gboolean split_cursor;
5269 GtkTextDirection new_dir;
5270 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5272 g_object_get (G_OBJECT (settings),
5273 "gtk-split-cursor", &split_cursor,
5277 new_dir = GTK_TEXT_DIR_NONE;
5281 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5282 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5283 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5286 if (text_view->layout->cursor_direction != new_dir)
5287 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5292 gtk_text_view_ensure_layout (GtkTextView *text_view)
5296 widget = GTK_WIDGET (text_view);
5298 if (text_view->layout == NULL)
5300 GtkTextAttributes *style;
5301 PangoContext *ltr_context, *rtl_context;
5304 DV(g_print(G_STRLOC"\n"));
5306 text_view->layout = gtk_text_layout_new ();
5308 g_signal_connect (text_view->layout,
5310 G_CALLBACK (invalidated_handler),
5313 g_signal_connect (text_view->layout,
5315 G_CALLBACK (changed_handler),
5318 g_signal_connect (text_view->layout,
5320 G_CALLBACK (gtk_text_view_child_allocated),
5323 if (get_buffer (text_view))
5324 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5326 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5327 gtk_text_view_pend_cursor_blink (text_view);
5329 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5331 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5332 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5333 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5334 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5336 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5338 g_object_unref (ltr_context);
5339 g_object_unref (rtl_context);
5341 gtk_text_view_check_keymap_direction (text_view);
5343 style = gtk_text_attributes_new ();
5345 gtk_widget_ensure_style (widget);
5346 gtk_text_view_set_attributes_from_style (text_view,
5347 style, widget->style);
5349 style->pixels_above_lines = text_view->pixels_above_lines;
5350 style->pixels_below_lines = text_view->pixels_below_lines;
5351 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5352 style->left_margin = text_view->left_margin;
5353 style->right_margin = text_view->right_margin;
5354 style->indent = text_view->indent;
5355 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5357 style->wrap_mode = text_view->wrap_mode;
5358 style->justification = text_view->justify;
5359 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5361 gtk_text_layout_set_default_style (text_view->layout, style);
5363 gtk_text_attributes_unref (style);
5365 /* Set layout for all anchored children */
5367 tmp_list = text_view->children;
5368 while (tmp_list != NULL)
5370 GtkTextViewChild *vc = tmp_list->data;
5374 gtk_text_anchored_child_set_layout (vc->widget,
5376 /* vc may now be invalid! */
5379 tmp_list = g_slist_next (tmp_list);
5382 gtk_text_view_invalidate (text_view);
5387 * gtk_text_view_get_default_attributes:
5388 * @text_view: a #GtkTextView
5390 * Obtains a copy of the default text attributes. These are the
5391 * attributes used for text unless a tag overrides them.
5392 * You'd typically pass the default attributes in to
5393 * gtk_text_iter_get_attributes() in order to get the
5394 * attributes in effect at a given text position.
5396 * The return value is a copy owned by the caller of this function,
5397 * and should be freed.
5399 * Return value: a new #GtkTextAttributes
5402 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5404 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5406 gtk_text_view_ensure_layout (text_view);
5408 return gtk_text_attributes_copy (text_view->layout->default_style);
5412 gtk_text_view_destroy_layout (GtkTextView *text_view)
5414 if (text_view->layout)
5418 gtk_text_view_remove_validate_idles (text_view);
5420 g_signal_handlers_disconnect_by_func (text_view->layout,
5421 invalidated_handler,
5423 g_signal_handlers_disconnect_by_func (text_view->layout,
5427 /* Remove layout from all anchored children */
5428 tmp_list = text_view->children;
5429 while (tmp_list != NULL)
5431 GtkTextViewChild *vc = tmp_list->data;
5435 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5436 /* vc may now be invalid! */
5439 tmp_list = g_slist_next (tmp_list);
5442 gtk_text_view_stop_cursor_blink (text_view);
5443 gtk_text_view_end_selection_drag (text_view, NULL);
5445 g_object_unref (text_view->layout);
5446 text_view->layout = NULL;
5451 gtk_text_view_reset_im_context (GtkTextView *text_view)
5453 if (text_view->need_im_reset)
5455 text_view->need_im_reset = FALSE;
5456 gtk_im_context_reset (text_view->im_context);
5465 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5466 const GtkTextIter *iter,
5467 GdkEventMotion *event)
5469 GdkDragContext *context;
5470 GtkTargetList *target_list;
5472 text_view->drag_start_x = -1;
5473 text_view->drag_start_y = -1;
5474 text_view->pending_place_cursor_button = 0;
5476 target_list = gtk_target_list_new (target_table,
5477 G_N_ELEMENTS (target_table));
5479 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5480 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5481 1, (GdkEvent*)event);
5483 gtk_target_list_unref (target_list);
5485 gtk_drag_set_icon_default (context);
5489 gtk_text_view_drag_begin (GtkWidget *widget,
5490 GdkDragContext *context)
5496 gtk_text_view_drag_end (GtkWidget *widget,
5497 GdkDragContext *context)
5499 GtkTextView *text_view;
5501 text_view = GTK_TEXT_VIEW (widget);
5505 gtk_text_view_drag_data_get (GtkWidget *widget,
5506 GdkDragContext *context,
5507 GtkSelectionData *selection_data,
5511 GtkTextView *text_view;
5513 text_view = GTK_TEXT_VIEW (widget);
5515 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5517 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5519 gtk_selection_data_set (selection_data,
5520 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5533 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5536 /* Extract the selected text */
5537 str = gtk_text_iter_get_visible_text (&start, &end);
5542 gtk_selection_data_set_text (selection_data, str, -1);
5549 gtk_text_view_drag_data_delete (GtkWidget *widget,
5550 GdkDragContext *context)
5552 GtkTextView *text_view;
5554 text_view = GTK_TEXT_VIEW (widget);
5556 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5557 TRUE, GTK_TEXT_VIEW (widget)->editable);
5561 gtk_text_view_drag_leave (GtkWidget *widget,
5562 GdkDragContext *context,
5565 GtkTextView *text_view;
5567 text_view = GTK_TEXT_VIEW (widget);
5569 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5571 if (text_view->scroll_timeout != 0)
5572 gtk_timeout_remove (text_view->scroll_timeout);
5574 text_view->scroll_timeout = 0;
5578 gtk_text_view_drag_motion (GtkWidget *widget,
5579 GdkDragContext *context,
5584 GtkTextIter newplace;
5585 GtkTextView *text_view;
5588 GdkRectangle target_rect;
5590 GdkDragAction suggested_action = 0;
5592 text_view = GTK_TEXT_VIEW (widget);
5594 target_rect = text_view->text_window->allocation;
5596 if (x < target_rect.x ||
5597 y < target_rect.y ||
5598 x > (target_rect.x + target_rect.width) ||
5599 y > (target_rect.y + target_rect.height))
5600 return FALSE; /* outside the text window, allow parent widgets to handle event */
5602 gtk_text_view_window_to_buffer_coords (text_view,
5603 GTK_TEXT_WINDOW_WIDGET,
5607 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5611 if (gtk_drag_dest_find_target (widget, context,
5612 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5614 /* can't accept any of the offered targets */
5616 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5618 gtk_text_iter_compare (&newplace, &start) >= 0 &&
5619 gtk_text_iter_compare (&newplace, &end) <= 0)
5621 /* We're inside the selection. */
5625 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5627 GtkWidget *source_widget;
5629 suggested_action = context->suggested_action;
5631 source_widget = gtk_drag_get_source_widget (context);
5633 if (source_widget == widget)
5635 /* Default to MOVE, unless the user has
5636 * pressed ctrl or alt to affect available actions
5638 if ((context->actions & GDK_ACTION_MOVE) != 0)
5639 suggested_action = GDK_ACTION_MOVE;
5644 /* Can't drop here. */
5648 if (suggested_action != 0)
5650 gtk_text_mark_set_visible (text_view->dnd_mark,
5651 text_view->cursor_visible);
5653 gdk_drag_status (context, suggested_action, time);
5657 gdk_drag_status (context, 0, time);
5658 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5661 gtk_text_buffer_move_mark (get_buffer (text_view),
5662 text_view->dnd_mark,
5665 DV(g_print (G_STRLOC": scrolling to mark\n"));
5666 gtk_text_view_scroll_to_mark (text_view,
5667 text_view->dnd_mark,
5668 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5670 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5671 gtk_timeout_remove (text_view->scroll_timeout);
5673 text_view->scroll_timeout =
5674 gtk_timeout_add (50, drag_scan_timeout, text_view);
5676 /* TRUE return means don't propagate the drag motion to parent
5677 * widgets that may also be drop sites.
5683 gtk_text_view_drag_drop (GtkWidget *widget,
5684 GdkDragContext *context,
5689 GtkTextView *text_view;
5690 GtkTextIter drop_point;
5691 GdkAtom target = GDK_NONE;
5693 text_view = GTK_TEXT_VIEW (widget);
5695 if (text_view->scroll_timeout != 0)
5696 gtk_timeout_remove (text_view->scroll_timeout);
5698 text_view->scroll_timeout = 0;
5700 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5702 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5704 text_view->dnd_mark);
5706 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5707 target = gtk_drag_dest_find_target (widget, context, NULL);
5709 if (target != GDK_NONE)
5710 gtk_drag_get_data (widget, context, target, time);
5712 gtk_drag_finish (context, FALSE, FALSE, time);
5718 insert_text_data (GtkTextView *text_view,
5719 GtkTextIter *drop_point,
5720 GtkSelectionData *selection_data)
5724 str = gtk_selection_data_get_text (selection_data);
5728 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5729 drop_point, str, -1,
5730 text_view->editable);
5736 gtk_text_view_drag_data_received (GtkWidget *widget,
5737 GdkDragContext *context,
5740 GtkSelectionData *selection_data,
5744 GtkTextIter drop_point;
5745 GtkTextView *text_view;
5746 gboolean success = FALSE;
5748 text_view = GTK_TEXT_VIEW (widget);
5750 if (!text_view->dnd_mark)
5753 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5755 text_view->dnd_mark);
5757 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5760 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5762 GtkTextBuffer *src_buffer = NULL;
5763 GtkTextIter start, end;
5764 gboolean copy_tags = TRUE;
5766 if (selection_data->length != sizeof (src_buffer))
5769 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5771 if (src_buffer == NULL)
5774 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5776 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5777 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5780 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5785 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5789 text_view->editable);
5794 str = gtk_text_iter_get_visible_text (&start, &end);
5795 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5796 &drop_point, str, -1,
5797 text_view->editable);
5803 insert_text_data (text_view, &drop_point, selection_data);
5808 gtk_drag_finish (context, success,
5809 success && context->action == GDK_ACTION_MOVE,
5813 static GtkAdjustment*
5814 get_hadjustment (GtkTextView *text_view)
5816 if (text_view->hadjustment == NULL)
5817 gtk_text_view_set_scroll_adjustments (text_view,
5818 NULL, /* forces creation */
5819 text_view->vadjustment);
5821 return text_view->hadjustment;
5824 static GtkAdjustment*
5825 get_vadjustment (GtkTextView *text_view)
5827 if (text_view->vadjustment == NULL)
5828 gtk_text_view_set_scroll_adjustments (text_view,
5829 text_view->hadjustment,
5830 NULL); /* forces creation */
5831 return text_view->vadjustment;
5836 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5837 GtkAdjustment *hadj,
5838 GtkAdjustment *vadj)
5840 gboolean need_adjust = FALSE;
5842 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5845 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5847 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5849 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5851 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5853 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5855 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
5856 gtk_text_view_value_changed,
5858 g_object_unref (text_view->hadjustment);
5861 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5863 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
5864 gtk_text_view_value_changed,
5866 g_object_unref (text_view->vadjustment);
5869 if (text_view->hadjustment != hadj)
5871 text_view->hadjustment = hadj;
5872 g_object_ref (text_view->hadjustment);
5873 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5875 g_signal_connect (text_view->hadjustment, "value_changed",
5876 G_CALLBACK (gtk_text_view_value_changed),
5881 if (text_view->vadjustment != vadj)
5883 text_view->vadjustment = vadj;
5884 g_object_ref (text_view->vadjustment);
5885 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5887 g_signal_connect (text_view->vadjustment, "value_changed",
5888 G_CALLBACK (gtk_text_view_value_changed),
5894 gtk_text_view_value_changed (NULL, text_view);
5897 /* FIXME this adjust_allocation is a big cut-and-paste from
5898 * GtkCList, needs to be some "official" way to do this
5908 /* The window to which widget->window is relative */
5909 #define ALLOCATION_WINDOW(widget) \
5910 (GTK_WIDGET_NO_WINDOW (widget) ? \
5911 (widget)->window : \
5912 gdk_window_get_parent ((widget)->window))
5915 adjust_allocation_recurse (GtkWidget *widget,
5918 ScrollData *scroll_data = data;
5920 /* Need to really size allocate instead of just poking
5921 * into widget->allocation if the widget is not realized.
5922 * FIXME someone figure out why this was.
5924 if (!GTK_WIDGET_REALIZED (widget))
5926 if (GTK_WIDGET_VISIBLE (widget))
5928 GdkRectangle tmp_rectangle = widget->allocation;
5929 tmp_rectangle.x += scroll_data->dx;
5930 tmp_rectangle.y += scroll_data->dy;
5932 gtk_widget_size_allocate (widget, &tmp_rectangle);
5937 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5939 widget->allocation.x += scroll_data->dx;
5940 widget->allocation.y += scroll_data->dy;
5942 if (GTK_IS_CONTAINER (widget))
5943 gtk_container_forall (GTK_CONTAINER (widget),
5944 adjust_allocation_recurse,
5951 adjust_allocation (GtkWidget *widget,
5955 ScrollData scroll_data;
5957 if (GTK_WIDGET_REALIZED (widget))
5958 scroll_data.window = ALLOCATION_WINDOW (widget);
5960 scroll_data.window = NULL;
5962 scroll_data.dx = dx;
5963 scroll_data.dy = dy;
5965 adjust_allocation_recurse (widget, &scroll_data);
5969 gtk_text_view_value_changed (GtkAdjustment *adj,
5970 GtkTextView *text_view)
5977 /* Note that we oddly call this function with adj == NULL
5981 text_view->onscreen_validated = FALSE;
5983 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5984 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5985 adj ? adj->value : 0.0));
5987 if (adj == text_view->hadjustment)
5989 dx = text_view->xoffset - (gint)adj->value;
5990 text_view->xoffset = adj->value;
5992 else if (adj == text_view->vadjustment)
5994 dy = text_view->yoffset - (gint)adj->value;
5995 text_view->yoffset = adj->value;
5997 if (text_view->layout)
5999 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
6001 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
6003 text_view->first_para_pixels = adj->value - line_top;
6007 if (dx != 0 || dy != 0)
6011 if (GTK_WIDGET_REALIZED (text_view))
6015 if (text_view->left_window)
6016 text_window_scroll (text_view->left_window, 0, dy);
6017 if (text_view->right_window)
6018 text_window_scroll (text_view->right_window, 0, dy);
6023 if (text_view->top_window)
6024 text_window_scroll (text_view->top_window, dx, 0);
6025 if (text_view->bottom_window)
6026 text_window_scroll (text_view->bottom_window, dx, 0);
6029 /* It looks nicer to scroll the main area last, because
6030 * it takes a while, and making the side areas update
6031 * afterward emphasizes the slowness of scrolling the
6034 text_window_scroll (text_view->text_window, dx, dy);
6037 /* Children are now "moved" in the text window, poke
6038 * into widget->allocation for each child
6040 tmp_list = text_view->children;
6041 while (tmp_list != NULL)
6043 GtkTextViewChild *child = tmp_list->data;
6046 adjust_allocation (child->widget, dx, dy);
6048 tmp_list = g_slist_next (tmp_list);
6052 /* This could result in invalidation, which would install the
6053 * first_validate_idle, which would validate onscreen;
6054 * but we're going to go ahead and validate here, so
6055 * first_validate_idle shouldn't have anything to do.
6057 gtk_text_view_update_layout_width (text_view);
6059 /* note that validation of onscreen could invoke this function
6060 * recursively, by scrolling to maintain first_para, or in response
6061 * to updating the layout width, however there is no problem with
6062 * that, or shouldn't be.
6064 gtk_text_view_validate_onscreen (text_view);
6066 /* process exposes */
6067 if (GTK_WIDGET_REALIZED (text_view))
6069 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6071 if (text_view->left_window)
6072 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6074 if (text_view->right_window)
6075 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6077 if (text_view->top_window)
6078 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6080 if (text_view->bottom_window)
6081 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6083 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6086 /* If this got installed, get rid of it, it's just a waste of time. */
6087 if (text_view->first_validate_idle != 0)
6089 g_source_remove (text_view->first_validate_idle);
6090 text_view->first_validate_idle = 0;
6093 gtk_text_view_update_im_spot_location (text_view);
6095 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6099 gtk_text_view_commit_handler (GtkIMContext *context,
6101 GtkTextView *text_view)
6103 gtk_text_view_commit_text (text_view, str);
6107 gtk_text_view_commit_text (GtkTextView *text_view,
6110 gboolean had_selection;
6112 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6114 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6117 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6118 text_view->editable);
6120 if (!strcmp (str, "\n"))
6122 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6123 text_view->editable);
6127 if (!had_selection && text_view->overwrite_mode)
6131 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6133 gtk_text_buffer_get_mark (get_buffer (text_view),
6135 if (!gtk_text_iter_ends_line (&insert))
6136 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6138 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6139 text_view->editable);
6142 gtk_text_buffer_end_user_action (get_buffer (text_view));
6144 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6145 gtk_text_view_scroll_mark_onscreen (text_view,
6146 gtk_text_buffer_get_mark (get_buffer (text_view),
6151 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6152 GtkTextView *text_view)
6155 PangoAttrList *attrs;
6158 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6159 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6161 pango_attr_list_unref (attrs);
6166 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6167 GtkTextView *text_view)
6174 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6175 gtk_text_buffer_get_insert (text_view->buffer));
6178 pos = gtk_text_iter_get_line_index (&start);
6179 gtk_text_iter_set_line_offset (&start, 0);
6180 gtk_text_iter_forward_to_line_end (&end);
6182 text = gtk_text_iter_get_slice (&start, &end);
6183 gtk_im_context_set_surrounding (context, text, -1, pos);
6190 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6193 GtkTextView *text_view)
6198 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6199 gtk_text_buffer_get_insert (text_view->buffer));
6202 gtk_text_iter_forward_chars (&start, offset);
6203 gtk_text_iter_forward_chars (&end, offset + n_chars);
6205 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6211 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6212 const GtkTextIter *location,
6216 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6217 gboolean need_reset = FALSE;
6219 if (mark == gtk_text_buffer_get_insert (buffer))
6221 text_view->virtual_cursor_x = -1;
6222 text_view->virtual_cursor_y = -1;
6223 gtk_text_view_update_im_spot_location (text_view);
6226 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6232 gtk_text_view_reset_im_context (text_view);
6236 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6240 GdkRectangle strong_pos;
6243 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6244 gtk_text_buffer_get_mark (get_buffer (text_view),
6247 if ((x && text_view->virtual_cursor_x == -1) ||
6248 (y && text_view->virtual_cursor_y == -1))
6249 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6253 if (text_view->virtual_cursor_x != -1)
6254 *x = text_view->virtual_cursor_x;
6261 if (text_view->virtual_cursor_x != -1)
6262 *y = text_view->virtual_cursor_y;
6264 *y = strong_pos.y + strong_pos.height / 2;
6269 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6273 GdkRectangle strong_pos;
6276 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6277 gtk_text_buffer_get_mark (get_buffer (text_view),
6280 if (x == -1 || y == -1)
6281 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6283 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6284 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6287 /* Quick hack of a popup menu
6290 activate_cb (GtkWidget *menuitem,
6291 GtkTextView *text_view)
6293 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6294 g_signal_emit_by_name (text_view, signal);
6298 append_action_signal (GtkTextView *text_view,
6300 const gchar *stock_id,
6301 const gchar *signal,
6304 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6306 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6307 g_signal_connect (menuitem, "activate",
6308 G_CALLBACK (activate_cb), text_view);
6310 gtk_widget_set_sensitive (menuitem, sensitive);
6312 gtk_widget_show (menuitem);
6313 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6317 popup_menu_detach (GtkWidget *attach_widget,
6320 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6324 popup_position_func (GtkMenu *menu,
6330 GtkTextView *text_view;
6332 GdkRectangle cursor_rect;
6333 GdkRectangle onscreen_rect;
6334 gint root_x, root_y;
6339 text_view = GTK_TEXT_VIEW (user_data);
6340 widget = GTK_WIDGET (text_view);
6342 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6344 screen = gtk_widget_get_screen (widget);
6346 gdk_window_get_origin (widget->window, &root_x, &root_y);
6348 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6350 gtk_text_buffer_get_insert (get_buffer (text_view)));
6352 gtk_text_view_get_iter_location (text_view,
6356 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6358 gtk_widget_size_request (text_view->popup_menu, &req);
6360 /* can't use rectangle_intersect since cursor rect can have 0 width */
6361 if (cursor_rect.x >= onscreen_rect.x &&
6362 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6363 cursor_rect.y >= onscreen_rect.y &&
6364 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6366 gtk_text_view_buffer_to_window_coords (text_view,
6367 GTK_TEXT_WINDOW_WIDGET,
6368 cursor_rect.x, cursor_rect.y,
6369 &cursor_rect.x, &cursor_rect.y);
6371 *x = root_x + cursor_rect.x + cursor_rect.width;
6372 *y = root_y + cursor_rect.y + cursor_rect.height;
6376 /* Just center the menu, since cursor is offscreen. */
6377 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6378 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6382 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6383 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6385 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6386 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6391 GtkTextView *text_view;
6397 range_contains_editable_text (const GtkTextIter *start,
6398 const GtkTextIter *end,
6399 gboolean default_editability)
6401 GtkTextIter iter = *start;
6403 while (gtk_text_iter_compare (&iter, end) < 0)
6405 if (gtk_text_iter_editable (&iter, default_editability))
6408 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6415 unichar_chosen_func (const char *text,
6418 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6420 gtk_text_view_commit_text (text_view, text);
6424 popup_targets_received (GtkClipboard *clipboard,
6425 GtkSelectionData *data,
6428 PopupInfo *info = user_data;
6429 GtkTextView *text_view = info->text_view;
6431 if (GTK_WIDGET_REALIZED (text_view))
6433 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6434 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6436 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6437 GtkWidget *menuitem;
6439 gboolean have_selection;
6440 gboolean can_insert;
6442 GtkTextIter sel_start, sel_end;
6444 if (text_view->popup_menu)
6445 gtk_widget_destroy (text_view->popup_menu);
6447 text_view->popup_menu = gtk_menu_new ();
6449 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6450 GTK_WIDGET (text_view),
6453 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6454 &sel_start, &sel_end);
6456 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6458 gtk_text_buffer_get_insert (get_buffer (text_view)));
6460 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6462 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6464 range_contains_editable_text (&sel_start, &sel_end,
6465 text_view->editable));
6466 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6468 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6469 can_insert && clipboard_contains_text);
6471 menuitem = gtk_separator_menu_item_new ();
6472 gtk_widget_show (menuitem);
6473 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6475 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6476 gtk_widget_show (menuitem);
6477 submenu = gtk_menu_new ();
6478 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6479 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6481 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6482 GTK_MENU_SHELL (submenu));
6484 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6485 gtk_widget_show (menuitem);
6486 gtk_widget_set_sensitive (menuitem, can_insert);
6488 submenu = gtk_menu_new ();
6489 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6490 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6492 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6493 unichar_chosen_func,
6496 g_signal_emit (text_view,
6497 signals[POPULATE_POPUP],
6499 text_view->popup_menu);
6502 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6504 info->button, info->time);
6507 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6508 popup_position_func, text_view,
6509 0, gtk_get_current_event_time ());
6510 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
6514 g_object_unref (text_view);
6519 gtk_text_view_do_popup (GtkTextView *text_view,
6520 GdkEventButton *event)
6522 PopupInfo *info = g_new (PopupInfo, 1);
6524 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6525 gtk_text_view_end_selection_drag (text_view, event);
6527 /* In order to know what entries we should make sensitive, we
6528 * ask for the current targets of the clipboard, and when
6529 * we get them, then we actually pop up the menu.
6531 info->text_view = g_object_ref (text_view);
6535 info->button = event->button;
6536 info->time = event->time;
6541 info->time = gtk_get_current_event_time ();
6544 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6545 GDK_SELECTION_CLIPBOARD),
6546 gdk_atom_intern ("TARGETS", FALSE),
6547 popup_targets_received,
6552 gtk_text_view_popup_menu (GtkWidget *widget)
6554 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6558 /* Child GdkWindows */
6561 static GtkTextWindow*
6562 text_window_new (GtkTextWindowType type,
6565 gint height_request)
6569 win = g_new (GtkTextWindow, 1);
6572 win->widget = widget;
6574 win->bin_window = NULL;
6575 win->requisition.width = width_request;
6576 win->requisition.height = height_request;
6577 win->allocation.width = width_request;
6578 win->allocation.height = height_request;
6579 win->allocation.x = 0;
6580 win->allocation.y = 0;
6586 text_window_free (GtkTextWindow *win)
6589 text_window_unrealize (win);
6595 text_window_realize (GtkTextWindow *win,
6598 GdkWindowAttr attributes;
6599 gint attributes_mask;
6602 attributes.window_type = GDK_WINDOW_CHILD;
6603 attributes.x = win->allocation.x;
6604 attributes.y = win->allocation.y;
6605 attributes.width = win->allocation.width;
6606 attributes.height = win->allocation.height;
6607 attributes.wclass = GDK_INPUT_OUTPUT;
6608 attributes.visual = gtk_widget_get_visual (win->widget);
6609 attributes.colormap = gtk_widget_get_colormap (win->widget);
6610 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6612 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6614 win->window = gdk_window_new (parent,
6618 gdk_window_show (win->window);
6619 gdk_window_set_user_data (win->window, win->widget);
6623 attributes.width = win->allocation.width;
6624 attributes.height = win->allocation.height;
6625 attributes.event_mask = (GDK_EXPOSURE_MASK |
6627 GDK_KEY_PRESS_MASK |
6628 GDK_BUTTON_PRESS_MASK |
6629 GDK_BUTTON_RELEASE_MASK |
6630 GDK_POINTER_MOTION_MASK |
6631 GDK_POINTER_MOTION_HINT_MASK |
6632 gtk_widget_get_events (win->widget));
6634 win->bin_window = gdk_window_new (win->window,
6638 gdk_window_show (win->bin_window);
6639 gdk_window_set_user_data (win->bin_window, win->widget);
6641 if (win->type == GTK_TEXT_WINDOW_TEXT)
6644 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
6646 gdk_window_set_cursor (win->bin_window, cursor);
6647 gdk_cursor_unref (cursor);
6649 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6653 gdk_window_set_background (win->bin_window,
6654 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6658 gdk_window_set_background (win->bin_window,
6659 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6662 g_object_set_qdata (G_OBJECT (win->window),
6663 g_quark_from_static_string ("gtk-text-view-text-window"),
6666 g_object_set_qdata (G_OBJECT (win->bin_window),
6667 g_quark_from_static_string ("gtk-text-view-text-window"),
6672 text_window_unrealize (GtkTextWindow *win)
6674 if (win->type == GTK_TEXT_WINDOW_TEXT)
6676 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6680 gdk_window_set_user_data (win->window, NULL);
6681 gdk_window_set_user_data (win->bin_window, NULL);
6682 gdk_window_destroy (win->bin_window);
6683 gdk_window_destroy (win->window);
6685 win->bin_window = NULL;
6689 text_window_size_allocate (GtkTextWindow *win,
6692 win->allocation = *rect;
6696 gdk_window_move_resize (win->window,
6698 rect->width, rect->height);
6700 gdk_window_resize (win->bin_window,
6701 rect->width, rect->height);
6706 text_window_scroll (GtkTextWindow *win,
6710 if (dx != 0 || dy != 0)
6712 gdk_window_scroll (win->bin_window, dx, dy);
6717 text_window_invalidate_rect (GtkTextWindow *win,
6720 GdkRectangle window_rect;
6722 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6729 window_rect.width = rect->width;
6730 window_rect.height = rect->height;
6732 /* Adjust the rect as appropriate */
6736 case GTK_TEXT_WINDOW_TEXT:
6739 case GTK_TEXT_WINDOW_LEFT:
6740 case GTK_TEXT_WINDOW_RIGHT:
6742 window_rect.width = win->allocation.width;
6745 case GTK_TEXT_WINDOW_TOP:
6746 case GTK_TEXT_WINDOW_BOTTOM:
6748 window_rect.height = win->allocation.height;
6752 g_warning ("%s: bug!", G_STRLOC);
6757 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6761 GdkColor color = { 0, 65535, 0, 0 };
6762 GdkGC *gc = gdk_gc_new (win->bin_window);
6763 gdk_gc_set_rgb_fg_color (gc, &color);
6764 gdk_draw_rectangle (win->bin_window,
6765 gc, TRUE, window_rect.x, window_rect.y,
6766 window_rect.width, window_rect.height);
6767 g_object_unref (gc);
6773 text_window_get_width (GtkTextWindow *win)
6775 return win->allocation.width;
6779 text_window_get_height (GtkTextWindow *win)
6781 return win->allocation.height;
6788 * gtk_text_view_get_window:
6789 * @text_view: a #GtkTextView
6790 * @win: window to get
6792 * Retrieves the #GdkWindow corresponding to an area of the text view;
6793 * possible windows include the overall widget window, child windows
6794 * on the left, right, top, bottom, and the window that displays the
6795 * text buffer. Windows are %NULL and nonexistent if their width or
6796 * height is 0, and are nonexistent before the widget has been
6799 * Return value: a #GdkWindow, or %NULL
6802 gtk_text_view_get_window (GtkTextView *text_view,
6803 GtkTextWindowType win)
6805 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6809 case GTK_TEXT_WINDOW_WIDGET:
6810 return GTK_WIDGET (text_view)->window;
6813 case GTK_TEXT_WINDOW_TEXT:
6814 return text_view->text_window->bin_window;
6817 case GTK_TEXT_WINDOW_LEFT:
6818 if (text_view->left_window)
6819 return text_view->left_window->bin_window;
6824 case GTK_TEXT_WINDOW_RIGHT:
6825 if (text_view->right_window)
6826 return text_view->right_window->bin_window;
6831 case GTK_TEXT_WINDOW_TOP:
6832 if (text_view->top_window)
6833 return text_view->top_window->bin_window;
6838 case GTK_TEXT_WINDOW_BOTTOM:
6839 if (text_view->bottom_window)
6840 return text_view->bottom_window->bin_window;
6845 case GTK_TEXT_WINDOW_PRIVATE:
6846 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6851 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6856 * gtk_text_view_get_window_type:
6857 * @text_view: a #GtkTextView
6858 * @window: a window type
6860 * Usually used to find out which window an event corresponds to.
6861 * If you connect to an event signal on @text_view, this function
6862 * should be called on <literal>event->window</literal> to
6863 * see which window it was.
6865 * Return value: the window type.
6868 gtk_text_view_get_window_type (GtkTextView *text_view,
6873 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6874 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6876 if (window == GTK_WIDGET (text_view)->window)
6877 return GTK_TEXT_WINDOW_WIDGET;
6879 win = g_object_get_qdata (G_OBJECT (window),
6880 g_quark_try_string ("gtk-text-view-text-window"));
6886 return GTK_TEXT_WINDOW_PRIVATE;
6891 buffer_to_widget (GtkTextView *text_view,
6899 *window_x = buffer_x - text_view->xoffset;
6900 *window_x += text_view->text_window->allocation.x;
6905 *window_y = buffer_y - text_view->yoffset;
6906 *window_y += text_view->text_window->allocation.y;
6911 widget_to_text_window (GtkTextWindow *win,
6918 *window_x = widget_x - win->allocation.x;
6921 *window_y = widget_y - win->allocation.y;
6925 buffer_to_text_window (GtkTextView *text_view,
6934 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6935 "for a nonexistent or private child window of GtkTextView");
6939 buffer_to_widget (text_view,
6941 window_x, window_y);
6943 widget_to_text_window (win,
6944 window_x ? *window_x : 0,
6945 window_y ? *window_y : 0,
6951 * gtk_text_view_buffer_to_window_coords:
6952 * @text_view: a #GtkTextView
6953 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6954 * @buffer_x: buffer x coordinate
6955 * @buffer_y: buffer y coordinate
6956 * @window_x: window x coordinate return location
6957 * @window_y: window y coordinate return location
6959 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6960 * @win, and stores the result in (@window_x, @window_y).
6962 * Note that you can't convert coordinates for a nonexisting window (see
6963 * gtk_text_view_set_border_window_size()).
6966 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6967 GtkTextWindowType win,
6973 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6977 case GTK_TEXT_WINDOW_WIDGET:
6978 buffer_to_widget (text_view,
6980 window_x, window_y);
6983 case GTK_TEXT_WINDOW_TEXT:
6985 *window_x = buffer_x - text_view->xoffset;
6987 *window_y = buffer_y - text_view->yoffset;
6990 case GTK_TEXT_WINDOW_LEFT:
6991 buffer_to_text_window (text_view,
6992 text_view->left_window,
6994 window_x, window_y);
6997 case GTK_TEXT_WINDOW_RIGHT:
6998 buffer_to_text_window (text_view,
6999 text_view->right_window,
7001 window_x, window_y);
7004 case GTK_TEXT_WINDOW_TOP:
7005 buffer_to_text_window (text_view,
7006 text_view->top_window,
7008 window_x, window_y);
7011 case GTK_TEXT_WINDOW_BOTTOM:
7012 buffer_to_text_window (text_view,
7013 text_view->bottom_window,
7015 window_x, window_y);
7018 case GTK_TEXT_WINDOW_PRIVATE:
7019 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7023 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7029 widget_to_buffer (GtkTextView *text_view,
7037 *buffer_x = widget_x + text_view->xoffset;
7038 *buffer_x -= text_view->text_window->allocation.x;
7043 *buffer_y = widget_y + text_view->yoffset;
7044 *buffer_y -= text_view->text_window->allocation.y;
7049 text_window_to_widget (GtkTextWindow *win,
7056 *widget_x = window_x + win->allocation.x;
7059 *widget_y = window_y + win->allocation.y;
7063 text_window_to_buffer (GtkTextView *text_view,
7072 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7073 "coordinates for a nonexistent child window.");
7077 text_window_to_widget (win,
7083 widget_to_buffer (text_view,
7084 buffer_x ? *buffer_x : 0,
7085 buffer_y ? *buffer_y : 0,
7091 * gtk_text_view_window_to_buffer_coords:
7092 * @text_view: a #GtkTextView
7093 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7094 * @window_x: window x coordinate
7095 * @window_y: window y coordinate
7096 * @buffer_x: buffer x coordinate return location
7097 * @buffer_y: buffer y coordinate return location
7099 * Converts coordinates on the window identified by @win to buffer
7100 * coordinates, storing the result in (@buffer_x,@buffer_y).
7102 * Note that you can't convert coordinates for a nonexisting window (see
7103 * gtk_text_view_set_border_window_size()).
7106 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7107 GtkTextWindowType win,
7113 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7117 case GTK_TEXT_WINDOW_WIDGET:
7118 widget_to_buffer (text_view,
7120 buffer_x, buffer_y);
7123 case GTK_TEXT_WINDOW_TEXT:
7125 *buffer_x = window_x + text_view->xoffset;
7127 *buffer_y = window_y + text_view->yoffset;
7130 case GTK_TEXT_WINDOW_LEFT:
7131 text_window_to_buffer (text_view,
7132 text_view->left_window,
7134 buffer_x, buffer_y);
7137 case GTK_TEXT_WINDOW_RIGHT:
7138 text_window_to_buffer (text_view,
7139 text_view->right_window,
7141 buffer_x, buffer_y);
7144 case GTK_TEXT_WINDOW_TOP:
7145 text_window_to_buffer (text_view,
7146 text_view->top_window,
7148 buffer_x, buffer_y);
7151 case GTK_TEXT_WINDOW_BOTTOM:
7152 text_window_to_buffer (text_view,
7153 text_view->bottom_window,
7155 buffer_x, buffer_y);
7158 case GTK_TEXT_WINDOW_PRIVATE:
7159 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7163 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7169 set_window_width (GtkTextView *text_view,
7171 GtkTextWindowType type,
7172 GtkTextWindow **winp)
7178 text_window_free (*winp);
7180 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7187 *winp = text_window_new (type,
7188 GTK_WIDGET (text_view),
7190 /* if the widget is already realized we need to realize the child manually */
7191 if (GTK_WIDGET_REALIZED (text_view))
7192 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7196 if ((*winp)->requisition.width == width)
7199 (*winp)->requisition.width = width;
7202 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7208 set_window_height (GtkTextView *text_view,
7210 GtkTextWindowType type,
7211 GtkTextWindow **winp)
7217 text_window_free (*winp);
7219 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7226 *winp = text_window_new (type,
7227 GTK_WIDGET (text_view),
7230 /* if the widget is already realized we need to realize the child manually */
7231 if (GTK_WIDGET_REALIZED (text_view))
7232 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7236 if ((*winp)->requisition.height == height)
7239 (*winp)->requisition.height = height;
7242 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7247 * gtk_text_view_set_border_window_size:
7248 * @text_view: a #GtkTextView
7249 * @type: window to affect
7250 * @size: width or height of the window
7252 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7253 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7254 * Automatically destroys the corresponding window if the size is set
7255 * to 0, and creates the window if the size is set to non-zero. This
7256 * function can only be used for the "border windows," it doesn't work
7257 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7258 * #GTK_TEXT_WINDOW_PRIVATE.
7261 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7262 GtkTextWindowType type,
7266 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7267 g_return_if_fail (size >= 0);
7271 case GTK_TEXT_WINDOW_LEFT:
7272 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7273 &text_view->left_window);
7276 case GTK_TEXT_WINDOW_RIGHT:
7277 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7278 &text_view->right_window);
7281 case GTK_TEXT_WINDOW_TOP:
7282 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7283 &text_view->top_window);
7286 case GTK_TEXT_WINDOW_BOTTOM:
7287 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7288 &text_view->bottom_window);
7292 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7298 * gtk_text_view_get_border_window_size:
7299 * @text_view: a #GtkTextView
7300 * @type: window to return size from
7302 * Gets the width of the specified border window. See
7303 * gtk_text_view_set_border_window_size().
7305 * Return value: width of window
7308 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7309 GtkTextWindowType type)
7311 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7315 case GTK_TEXT_WINDOW_LEFT:
7316 if (text_view->left_window)
7317 return text_view->left_window->requisition.width;
7319 case GTK_TEXT_WINDOW_RIGHT:
7320 if (text_view->right_window)
7321 return text_view->right_window->requisition.width;
7323 case GTK_TEXT_WINDOW_TOP:
7324 if (text_view->top_window)
7325 return text_view->top_window->requisition.height;
7327 case GTK_TEXT_WINDOW_BOTTOM:
7328 if (text_view->bottom_window)
7329 return text_view->bottom_window->requisition.height;
7332 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7343 static GtkTextViewChild*
7344 text_view_child_new_anchored (GtkWidget *child,
7345 GtkTextChildAnchor *anchor,
7346 GtkTextLayout *layout)
7348 GtkTextViewChild *vc;
7350 vc = g_new (GtkTextViewChild, 1);
7352 vc->type = GTK_TEXT_WINDOW_PRIVATE;
7354 vc->anchor = anchor;
7356 vc->from_top_of_line = 0;
7357 vc->from_left_of_buffer = 0;
7359 g_object_ref (vc->widget);
7360 g_object_ref (vc->anchor);
7362 g_object_set_data (G_OBJECT (child),
7363 "gtk-text-view-child",
7366 gtk_text_child_anchor_register_child (anchor, child, layout);
7371 static GtkTextViewChild*
7372 text_view_child_new_window (GtkWidget *child,
7373 GtkTextWindowType type,
7377 GtkTextViewChild *vc;
7379 vc = g_new (GtkTextViewChild, 1);
7384 vc->from_top_of_line = 0;
7385 vc->from_left_of_buffer = 0;
7387 g_object_ref (vc->widget);
7393 g_object_set_data (G_OBJECT (child),
7394 "gtk-text-view-child",
7401 text_view_child_free (GtkTextViewChild *child)
7403 g_object_set_data (G_OBJECT (child->widget),
7404 "gtk-text-view-child", NULL);
7408 gtk_text_child_anchor_unregister_child (child->anchor,
7410 g_object_unref (child->anchor);
7413 g_object_unref (child->widget);
7419 text_view_child_set_parent_window (GtkTextView *text_view,
7420 GtkTextViewChild *vc)
7423 gtk_widget_set_parent_window (vc->widget,
7424 text_view->text_window->bin_window);
7428 window = gtk_text_view_get_window (text_view,
7430 gtk_widget_set_parent_window (vc->widget, window);
7435 add_child (GtkTextView *text_view,
7436 GtkTextViewChild *vc)
7438 text_view->children = g_slist_prepend (text_view->children,
7441 if (GTK_WIDGET_REALIZED (text_view))
7442 text_view_child_set_parent_window (text_view, vc);
7444 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7448 * gtk_text_view_add_child_at_anchor:
7449 * @text_view: a #GtkTextView
7450 * @child: a #GtkWidget
7451 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7453 * Adds a child widget in the text buffer, at the given @anchor.
7457 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7459 GtkTextChildAnchor *anchor)
7461 GtkTextViewChild *vc;
7463 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7464 g_return_if_fail (GTK_IS_WIDGET (child));
7465 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7466 g_return_if_fail (child->parent == NULL);
7468 gtk_text_view_ensure_layout (text_view);
7470 vc = text_view_child_new_anchored (child, anchor,
7473 add_child (text_view, vc);
7475 g_assert (vc->widget == child);
7476 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7480 * gtk_text_view_add_child_in_window:
7481 * @text_view: a #GtkTextView
7482 * @child: a #GtkWidget
7483 * @which_window: which window the child should appear in
7484 * @xpos: X position of child in window coordinates
7485 * @ypos: Y position of child in window coordinates
7487 * Adds a child at fixed coordinates in one of the text widget's
7488 * windows. The window must have nonzero size (see
7489 * gtk_text_view_set_border_window_size()). Note that the child
7490 * coordinates are given relative to the #GdkWindow in question, and
7491 * that these coordinates have no sane relationship to scrolling. When
7492 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7493 * irrelevant, the child floats above all scrollable areas. But when
7494 * placing a child in one of the scrollable windows (border windows or
7495 * text window), you'll need to compute the child's correct position
7496 * in buffer coordinates any time scrolling occurs or buffer changes
7497 * occur, and then call gtk_text_view_move_child() to update the
7498 * child's position. Unfortunately there's no good way to detect that
7499 * scrolling has occurred, using the current API; a possible hack
7500 * would be to update all child positions when the scroll adjustments
7501 * change or the text buffer changes. See bug 64518 on
7502 * bugzilla.gnome.org for status of fixing this issue.
7506 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7508 GtkTextWindowType which_window,
7512 GtkTextViewChild *vc;
7514 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7515 g_return_if_fail (GTK_IS_WIDGET (child));
7516 g_return_if_fail (child->parent == NULL);
7518 vc = text_view_child_new_window (child, which_window,
7521 add_child (text_view, vc);
7523 g_assert (vc->widget == child);
7524 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7528 * gtk_text_view_move_child:
7529 * @text_view: a #GtkTextView
7530 * @child: child widget already added to the text view
7531 * @xpos: new X position in window coordinates
7532 * @ypos: new Y position in window coordinates
7534 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7538 gtk_text_view_move_child (GtkTextView *text_view,
7543 GtkTextViewChild *vc;
7545 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7546 g_return_if_fail (GTK_IS_WIDGET (child));
7547 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7549 vc = g_object_get_data (G_OBJECT (child),
7550 "gtk-text-view-child");
7552 g_assert (vc != NULL);
7554 if (vc->x == xpos &&
7561 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7562 gtk_widget_queue_resize (child);
7566 /* Iterator operations */
7569 * gtk_text_view_forward_display_line:
7570 * @text_view: a #GtkTextView
7571 * @iter: a #GtkTextIter
7573 * Moves the given @iter forward by one display (wrapped) line. A
7574 * display line is different from a paragraph. Paragraphs are
7575 * separated by newlines or other paragraph separator characters.
7576 * Display lines are created by line-wrapping a paragraph. If
7577 * wrapping is turned off, display lines and paragraphs will be the
7578 * same. Display lines are divided differently for each view, since
7579 * they depend on the view's width; paragraphs are the same in all
7580 * views, since they depend on the contents of the #GtkTextBuffer.
7582 * Return value: %TRUE if @iter was moved and is not on the end iterator
7585 gtk_text_view_forward_display_line (GtkTextView *text_view,
7588 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7589 g_return_val_if_fail (iter != NULL, FALSE);
7591 gtk_text_view_ensure_layout (text_view);
7593 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7597 * gtk_text_view_backward_display_line:
7598 * @text_view: a #GtkTextView
7599 * @iter: a #GtkTextIter
7601 * Moves the given @iter backward by one display (wrapped) line. A
7602 * display line is different from a paragraph. Paragraphs are
7603 * separated by newlines or other paragraph separator characters.
7604 * Display lines are created by line-wrapping a paragraph. If
7605 * wrapping is turned off, display lines and paragraphs will be the
7606 * same. Display lines are divided differently for each view, since
7607 * they depend on the view's width; paragraphs are the same in all
7608 * views, since they depend on the contents of the #GtkTextBuffer.
7610 * Return value: %TRUE if @iter was moved and is not on the end iterator
7613 gtk_text_view_backward_display_line (GtkTextView *text_view,
7616 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7617 g_return_val_if_fail (iter != NULL, FALSE);
7619 gtk_text_view_ensure_layout (text_view);
7621 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7625 * gtk_text_view_forward_display_line_end:
7626 * @text_view: a #GtkTextView
7627 * @iter: a #GtkTextIter
7629 * Moves the given @iter forward to the next display line end. A
7630 * display line is different from a paragraph. Paragraphs are
7631 * separated by newlines or other paragraph separator characters.
7632 * Display lines are created by line-wrapping a paragraph. If
7633 * wrapping is turned off, display lines and paragraphs will be the
7634 * same. Display lines are divided differently for each view, since
7635 * they depend on the view's width; paragraphs are the same in all
7636 * views, since they depend on the contents of the #GtkTextBuffer.
7638 * Return value: %TRUE if @iter was moved and is not on the end iterator
7641 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7644 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7645 g_return_val_if_fail (iter != NULL, FALSE);
7647 gtk_text_view_ensure_layout (text_view);
7649 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7653 * gtk_text_view_backward_display_line_start:
7654 * @text_view: a #GtkTextView
7655 * @iter: a #GtkTextIter
7657 * Moves the given @iter backward to the next display line start. A
7658 * display line is different from a paragraph. Paragraphs are
7659 * separated by newlines or other paragraph separator characters.
7660 * Display lines are created by line-wrapping a paragraph. If
7661 * wrapping is turned off, display lines and paragraphs will be the
7662 * same. Display lines are divided differently for each view, since
7663 * they depend on the view's width; paragraphs are the same in all
7664 * views, since they depend on the contents of the #GtkTextBuffer.
7666 * Return value: %TRUE if @iter was moved and is not on the end iterator
7669 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7672 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7673 g_return_val_if_fail (iter != NULL, FALSE);
7675 gtk_text_view_ensure_layout (text_view);
7677 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7681 * gtk_text_view_starts_display_line:
7682 * @text_view: a #GtkTextView
7683 * @iter: a #GtkTextIter
7685 * Determines whether @iter is at the start of a display line.
7686 * See gtk_text_view_forward_display_line() for an explanation of
7687 * display lines vs. paragraphs.
7689 * Return value: %TRUE if @iter begins a wrapped line
7692 gtk_text_view_starts_display_line (GtkTextView *text_view,
7693 const GtkTextIter *iter)
7695 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7696 g_return_val_if_fail (iter != NULL, FALSE);
7698 gtk_text_view_ensure_layout (text_view);
7700 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7704 * gtk_text_view_move_visually:
7705 * @text_view: a #GtkTextView
7706 * @iter: a #GtkTextIter
7707 * @count: number of characters to move (negative moves left, positive moves right)
7709 * Move the iterator a given number of characters visually, treating
7710 * it as the strong cursor position. If @count is positive, then the
7711 * new strong cursor position will be @count positions to the right of
7712 * the old cursor position. If @count is negative then the new strong
7713 * cursor position will be @count positions to the left of the old
7716 * In the presence of bidirection text, the correspondence
7717 * between logical and visual order will depend on the direction
7718 * of the current run, and there may be jumps when the cursor
7719 * is moved off of the end of a run.
7721 * Return value: %TRUE if @iter moved and is not on the end iterator
7724 gtk_text_view_move_visually (GtkTextView *text_view,
7728 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7729 g_return_val_if_fail (iter != NULL, FALSE);
7731 gtk_text_view_ensure_layout (text_view);
7733 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);