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 void gtk_text_view_state_changed (GtkWidget *widget,
163 GtkStateType previous_state);
165 static gint gtk_text_view_event (GtkWidget *widget,
167 static gint gtk_text_view_key_press_event (GtkWidget *widget,
169 static gint gtk_text_view_key_release_event (GtkWidget *widget,
171 static gint gtk_text_view_button_press_event (GtkWidget *widget,
172 GdkEventButton *event);
173 static gint gtk_text_view_button_release_event (GtkWidget *widget,
174 GdkEventButton *event);
175 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
176 GdkEventFocus *event);
177 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
178 GdkEventFocus *event);
179 static gint gtk_text_view_motion_event (GtkWidget *widget,
180 GdkEventMotion *event);
181 static gint gtk_text_view_expose_event (GtkWidget *widget,
182 GdkEventExpose *expose);
183 static void gtk_text_view_draw_focus (GtkWidget *widget);
184 static void gtk_text_view_grab_focus (GtkWidget *widget);
185 static gboolean gtk_text_view_focus (GtkWidget *widget,
186 GtkDirectionType direction);
189 /* Source side drag signals */
190 static void gtk_text_view_drag_begin (GtkWidget *widget,
191 GdkDragContext *context);
192 static void gtk_text_view_drag_end (GtkWidget *widget,
193 GdkDragContext *context);
194 static void gtk_text_view_drag_data_get (GtkWidget *widget,
195 GdkDragContext *context,
196 GtkSelectionData *selection_data,
199 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
200 GdkDragContext *context);
202 /* Target side drag signals */
203 static void gtk_text_view_drag_leave (GtkWidget *widget,
204 GdkDragContext *context,
206 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
207 GdkDragContext *context,
211 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
212 GdkDragContext *context,
216 static void gtk_text_view_drag_data_received (GtkWidget *widget,
217 GdkDragContext *context,
220 GtkSelectionData *selection_data,
224 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
226 GtkAdjustment *vadj);
227 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
229 static void gtk_text_view_move_cursor (GtkTextView *text_view,
230 GtkMovementStep step,
232 gboolean extend_selection);
233 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
235 gboolean extend_selection);
236 static void gtk_text_view_set_anchor (GtkTextView *text_view);
237 static void gtk_text_view_scroll_pages (GtkTextView *text_view,
239 gboolean extend_selection);
240 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
242 gboolean extend_selection);
243 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
245 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
248 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
249 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
250 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
251 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
252 static void gtk_text_view_move_focus (GtkTextView *text_view,
253 GtkDirectionType direction_type);
254 static void gtk_text_view_unselect (GtkTextView *text_view);
256 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
257 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
259 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
260 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
261 GtkTextAttributes *values,
263 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
264 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
265 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
266 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
267 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
268 const GtkTextIter *iter,
269 GdkEventButton *event);
270 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
271 GdkEventButton *event);
272 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
273 const GtkTextIter *iter,
274 GdkEventMotion *event);
275 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
276 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
277 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
279 static void gtk_text_view_value_changed (GtkAdjustment *adj,
281 static void gtk_text_view_commit_handler (GtkIMContext *context,
283 GtkTextView *text_view);
284 static void gtk_text_view_commit_text (GtkTextView *text_view,
286 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
287 GtkTextView *text_view);
288 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
289 GtkTextView *text_view);
290 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
293 GtkTextView *text_view);
295 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
296 const GtkTextIter *location,
299 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
302 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
306 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
307 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
309 static void gtk_text_view_do_popup (GtkTextView *text_view,
310 GdkEventButton *event);
312 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
314 gdouble within_margin,
319 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
320 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
321 static void gtk_text_view_invalidate (GtkTextView *text_view);
322 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
324 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
326 /* Container methods */
327 static void gtk_text_view_add (GtkContainer *container,
329 static void gtk_text_view_remove (GtkContainer *container,
331 static void gtk_text_view_forall (GtkContainer *container,
332 gboolean include_internals,
333 GtkCallback callback,
334 gpointer callback_data);
336 /* FIXME probably need the focus methods. */
338 typedef struct _GtkTextViewChild GtkTextViewChild;
340 struct _GtkTextViewChild
344 GtkTextChildAnchor *anchor;
346 gint from_top_of_line;
347 gint from_left_of_buffer;
349 /* These are ignored if anchor != NULL */
350 GtkTextWindowType type;
355 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
356 GtkTextChildAnchor *anchor,
357 GtkTextLayout *layout);
358 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
359 GtkTextWindowType type,
362 static void text_view_child_free (GtkTextViewChild *child);
363 static void text_view_child_set_parent_window (GtkTextView *text_view,
364 GtkTextViewChild *child);
366 struct _GtkTextWindow
368 GtkTextWindowType type;
371 GdkWindow *bin_window;
372 GtkRequisition requisition;
373 GdkRectangle allocation;
376 static GtkTextWindow *text_window_new (GtkTextWindowType type,
379 gint height_request);
380 static void text_window_free (GtkTextWindow *win);
381 static void text_window_realize (GtkTextWindow *win,
383 static void text_window_unrealize (GtkTextWindow *win);
384 static void text_window_size_allocate (GtkTextWindow *win,
386 static void text_window_scroll (GtkTextWindow *win,
389 static void text_window_invalidate_rect (GtkTextWindow *win,
392 static gint text_window_get_width (GtkTextWindow *win);
393 static gint text_window_get_height (GtkTextWindow *win);
400 TARGET_COMPOUND_TEXT,
402 TARGET_TEXT_BUFFER_CONTENTS
405 static const GtkTargetEntry target_table[] = {
406 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
407 TARGET_TEXT_BUFFER_CONTENTS },
408 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
409 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
410 { "TEXT", 0, TARGET_TEXT },
411 { "STRING", 0, TARGET_STRING }
414 static GtkContainerClass *parent_class = NULL;
415 static guint signals[LAST_SIGNAL] = { 0 };
418 gtk_text_view_get_type (void)
420 static GType our_type = 0;
424 static const GTypeInfo our_info =
426 sizeof (GtkTextViewClass),
427 NULL, /* base_init */
428 NULL, /* base_finalize */
429 (GClassInitFunc) gtk_text_view_class_init,
430 NULL, /* class_finalize */
431 NULL, /* class_data */
432 sizeof (GtkTextView),
434 (GInstanceInitFunc) gtk_text_view_init,
437 our_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkTextView",
445 add_move_binding (GtkBindingSet *binding_set,
448 GtkMovementStep step,
451 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
453 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
457 G_TYPE_BOOLEAN, FALSE);
459 /* Selection-extending version */
460 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
464 G_TYPE_BOOLEAN, TRUE);
468 gtk_text_view_class_init (GtkTextViewClass *klass)
470 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
471 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
472 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
473 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
474 GtkBindingSet *binding_set;
476 parent_class = g_type_class_peek_parent (klass);
478 /* Default handlers and virtual methods
480 gobject_class->set_property = gtk_text_view_set_property;
481 gobject_class->get_property = gtk_text_view_get_property;
483 object_class->destroy = gtk_text_view_destroy;
484 gobject_class->finalize = gtk_text_view_finalize;
486 widget_class->realize = gtk_text_view_realize;
487 widget_class->unrealize = gtk_text_view_unrealize;
488 widget_class->style_set = gtk_text_view_style_set;
489 widget_class->direction_changed = gtk_text_view_direction_changed;
490 widget_class->state_changed = gtk_text_view_state_changed;
491 widget_class->size_request = gtk_text_view_size_request;
492 widget_class->size_allocate = gtk_text_view_size_allocate;
493 widget_class->event = gtk_text_view_event;
494 widget_class->key_press_event = gtk_text_view_key_press_event;
495 widget_class->key_release_event = gtk_text_view_key_release_event;
496 widget_class->button_press_event = gtk_text_view_button_press_event;
497 widget_class->button_release_event = gtk_text_view_button_release_event;
498 widget_class->focus_in_event = gtk_text_view_focus_in_event;
499 widget_class->focus_out_event = gtk_text_view_focus_out_event;
500 widget_class->motion_notify_event = gtk_text_view_motion_event;
501 widget_class->expose_event = gtk_text_view_expose_event;
502 widget_class->grab_focus = gtk_text_view_grab_focus;
503 widget_class->focus = gtk_text_view_focus;
505 widget_class->drag_begin = gtk_text_view_drag_begin;
506 widget_class->drag_end = gtk_text_view_drag_end;
507 widget_class->drag_data_get = gtk_text_view_drag_data_get;
508 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
510 widget_class->drag_leave = gtk_text_view_drag_leave;
511 widget_class->drag_motion = gtk_text_view_drag_motion;
512 widget_class->drag_drop = gtk_text_view_drag_drop;
513 widget_class->drag_data_received = gtk_text_view_drag_data_received;
515 widget_class->popup_menu = gtk_text_view_popup_menu;
517 container_class->add = gtk_text_view_add;
518 container_class->remove = gtk_text_view_remove;
519 container_class->forall = gtk_text_view_forall;
521 klass->move_cursor = gtk_text_view_move_cursor;
522 klass->page_horizontally = gtk_text_view_page_horizontally;
523 klass->set_anchor = gtk_text_view_set_anchor;
524 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
525 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
526 klass->cut_clipboard = gtk_text_view_cut_clipboard;
527 klass->copy_clipboard = gtk_text_view_copy_clipboard;
528 klass->paste_clipboard = gtk_text_view_paste_clipboard;
529 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
530 klass->move_focus = gtk_text_view_move_focus;
531 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
537 g_object_class_install_property (gobject_class,
538 PROP_PIXELS_ABOVE_LINES,
539 g_param_spec_int ("pixels_above_lines",
540 _("Pixels Above Lines"),
541 _("Pixels of blank space above paragraphs"),
547 g_object_class_install_property (gobject_class,
548 PROP_PIXELS_BELOW_LINES,
549 g_param_spec_int ("pixels_below_lines",
550 _("Pixels Below Lines"),
551 _("Pixels of blank space below paragraphs"),
557 g_object_class_install_property (gobject_class,
558 PROP_PIXELS_INSIDE_WRAP,
559 g_param_spec_int ("pixels_inside_wrap",
560 _("Pixels Inside Wrap"),
561 _("Pixels of blank space between wrapped lines in a paragraph"),
567 g_object_class_install_property (gobject_class,
569 g_param_spec_boolean ("editable",
571 _("Whether the text can be modified by the user"),
575 g_object_class_install_property (gobject_class,
577 g_param_spec_enum ("wrap_mode",
579 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
584 g_object_class_install_property (gobject_class,
586 g_param_spec_enum ("justification",
588 _("Left, right, or center justification"),
589 GTK_TYPE_JUSTIFICATION,
593 g_object_class_install_property (gobject_class,
595 g_param_spec_int ("left_margin",
597 _("Width of the left margin in pixels"),
603 g_object_class_install_property (gobject_class,
605 g_param_spec_int ("right_margin",
607 _("Width of the right margin in pixels"),
613 g_object_class_install_property (gobject_class,
615 g_param_spec_int ("indent",
617 _("Amount to indent the paragraph, in pixels"),
623 g_object_class_install_property (gobject_class,
625 g_param_spec_boxed ("tabs",
627 _("Custom tabs for this text"),
628 PANGO_TYPE_TAB_ARRAY,
631 g_object_class_install_property (gobject_class,
633 g_param_spec_boolean ("cursor_visible",
635 _("If the insertion cursor is shown"),
644 signals[MOVE_CURSOR] =
645 g_signal_new ("move_cursor",
646 G_OBJECT_CLASS_TYPE (gobject_class),
647 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
648 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
650 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
652 GTK_TYPE_MOVEMENT_STEP,
656 signals[PAGE_HORIZONTALLY] =
657 g_signal_new ("page_horizontally",
658 G_OBJECT_CLASS_TYPE (gobject_class),
659 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
660 G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
662 _gtk_marshal_VOID__INT_BOOLEAN,
667 signals[SET_ANCHOR] =
668 g_signal_new ("set_anchor",
669 G_OBJECT_CLASS_TYPE (gobject_class),
670 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
671 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
673 _gtk_marshal_VOID__VOID,
676 signals[INSERT_AT_CURSOR] =
677 g_signal_new ("insert_at_cursor",
678 G_OBJECT_CLASS_TYPE (gobject_class),
679 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
680 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
682 _gtk_marshal_VOID__STRING,
686 signals[DELETE_FROM_CURSOR] =
687 g_signal_new ("delete_from_cursor",
688 G_OBJECT_CLASS_TYPE (gobject_class),
689 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
690 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
692 _gtk_marshal_VOID__ENUM_INT,
694 GTK_TYPE_DELETE_TYPE,
697 signals[CUT_CLIPBOARD] =
698 g_signal_new ("cut_clipboard",
699 G_OBJECT_CLASS_TYPE (gobject_class),
700 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
701 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
703 _gtk_marshal_VOID__VOID,
706 signals[COPY_CLIPBOARD] =
707 g_signal_new ("copy_clipboard",
708 G_OBJECT_CLASS_TYPE (gobject_class),
709 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
710 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
712 _gtk_marshal_VOID__VOID,
715 signals[PASTE_CLIPBOARD] =
716 g_signal_new ("paste_clipboard",
717 G_OBJECT_CLASS_TYPE (gobject_class),
718 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
719 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
721 _gtk_marshal_VOID__VOID,
724 signals[TOGGLE_OVERWRITE] =
725 g_signal_new ("toggle_overwrite",
726 G_OBJECT_CLASS_TYPE (gobject_class),
727 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
728 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
730 _gtk_marshal_VOID__VOID,
733 signals[MOVE_FOCUS] =
734 g_signal_new ("move_focus",
735 G_OBJECT_CLASS_TYPE (gobject_class),
736 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
737 G_STRUCT_OFFSET (GtkTextViewClass, move_focus),
739 _gtk_marshal_VOID__ENUM,
741 GTK_TYPE_DIRECTION_TYPE);
743 signals[SET_SCROLL_ADJUSTMENTS] =
744 g_signal_new ("set_scroll_adjustments",
745 G_OBJECT_CLASS_TYPE (gobject_class),
746 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
747 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
749 _gtk_marshal_VOID__OBJECT_OBJECT,
752 GTK_TYPE_ADJUSTMENT);
753 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
755 signals[POPULATE_POPUP] =
756 g_signal_new ("populate_popup",
757 G_OBJECT_CLASS_TYPE (gobject_class),
759 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
761 _gtk_marshal_VOID__OBJECT,
769 binding_set = gtk_binding_set_by_class (klass);
771 /* Moving the insertion point */
772 add_move_binding (binding_set, GDK_Right, 0,
773 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
775 add_move_binding (binding_set, GDK_KP_Right, 0,
776 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
778 add_move_binding (binding_set, GDK_Left, 0,
779 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
781 add_move_binding (binding_set, GDK_KP_Left, 0,
782 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
784 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
785 GTK_MOVEMENT_WORDS, 1);
787 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
788 GTK_MOVEMENT_WORDS, 1);
790 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
791 GTK_MOVEMENT_WORDS, -1);
793 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
794 GTK_MOVEMENT_WORDS, 1);
796 add_move_binding (binding_set, GDK_Up, 0,
797 GTK_MOVEMENT_DISPLAY_LINES, -1);
799 add_move_binding (binding_set, GDK_KP_Up, 0,
800 GTK_MOVEMENT_DISPLAY_LINES, -1);
802 add_move_binding (binding_set, GDK_Down, 0,
803 GTK_MOVEMENT_DISPLAY_LINES, 1);
805 add_move_binding (binding_set, GDK_KP_Down, 0,
806 GTK_MOVEMENT_DISPLAY_LINES, 1);
808 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
809 GTK_MOVEMENT_PARAGRAPHS, -1);
811 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
812 GTK_MOVEMENT_PARAGRAPHS, -1);
814 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
815 GTK_MOVEMENT_PARAGRAPHS, 1);
817 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
818 GTK_MOVEMENT_PARAGRAPHS, 1);
820 add_move_binding (binding_set, GDK_Home, 0,
821 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
823 add_move_binding (binding_set, GDK_KP_Home, 0,
824 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
826 add_move_binding (binding_set, GDK_End, 0,
827 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
829 add_move_binding (binding_set, GDK_KP_End, 0,
830 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
832 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
833 GTK_MOVEMENT_BUFFER_ENDS, -1);
835 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
836 GTK_MOVEMENT_BUFFER_ENDS, -1);
838 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
839 GTK_MOVEMENT_BUFFER_ENDS, 1);
841 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
842 GTK_MOVEMENT_BUFFER_ENDS, 1);
844 add_move_binding (binding_set, GDK_Page_Up, 0,
845 GTK_MOVEMENT_PAGES, -1);
847 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
848 GTK_MOVEMENT_PAGES, -1);
850 add_move_binding (binding_set, GDK_Page_Down, 0,
851 GTK_MOVEMENT_PAGES, 1);
853 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
854 GTK_MOVEMENT_PAGES, 1);
858 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
860 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
862 G_TYPE_BOOLEAN, FALSE);
863 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
865 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
867 G_TYPE_BOOLEAN, TRUE);
870 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
871 "page_horizontally", 2,
873 G_TYPE_BOOLEAN, FALSE);
875 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
876 "page_horizontally", 2,
878 G_TYPE_BOOLEAN, TRUE);
880 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
881 "page_horizontally", 2,
883 G_TYPE_BOOLEAN, FALSE);
885 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
886 "page_horizontally", 2,
888 G_TYPE_BOOLEAN, TRUE);
890 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
891 "page_horizontally", 2,
893 G_TYPE_BOOLEAN, FALSE);
895 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
896 "page_horizontally", 2,
898 G_TYPE_BOOLEAN, TRUE);
900 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
901 "page_horizontally", 2,
903 G_TYPE_BOOLEAN, FALSE);
905 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
906 "page_horizontally", 2,
908 G_TYPE_BOOLEAN, TRUE);
911 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
912 "delete_from_cursor", 2,
913 G_TYPE_ENUM, GTK_DELETE_CHARS,
916 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
917 "delete_from_cursor", 2,
918 G_TYPE_ENUM, GTK_DELETE_CHARS,
921 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
922 "delete_from_cursor", 2,
923 G_TYPE_ENUM, GTK_DELETE_CHARS,
926 /* Make this do the same as Backspace, to help with mis-typing */
927 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
928 "delete_from_cursor", 2,
929 G_TYPE_ENUM, GTK_DELETE_CHARS,
932 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
933 "delete_from_cursor", 2,
934 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
937 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
938 "delete_from_cursor", 2,
939 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
942 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
943 "delete_from_cursor", 2,
944 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
949 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
951 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
952 "copy_clipboard", 0);
953 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
954 "paste_clipboard", 0);
956 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
958 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
959 "copy_clipboard", 0);
960 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
961 "paste_clipboard", 0);
964 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
965 "toggle_overwrite", 0);
966 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
967 "toggle_overwrite", 0);
969 /* Control-tab focus motion */
970 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
972 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
973 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
975 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
977 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
979 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
980 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
982 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
986 gtk_text_view_init (GtkTextView *text_view)
990 widget = GTK_WIDGET (text_view);
992 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
994 /* Set up default style */
995 text_view->wrap_mode = GTK_WRAP_NONE;
996 text_view->pixels_above_lines = 0;
997 text_view->pixels_below_lines = 0;
998 text_view->pixels_inside_wrap = 0;
999 text_view->justify = GTK_JUSTIFY_LEFT;
1000 text_view->left_margin = 0;
1001 text_view->right_margin = 0;
1002 text_view->indent = 0;
1003 text_view->tabs = NULL;
1004 text_view->editable = TRUE;
1006 gtk_drag_dest_set (widget,
1008 target_table, G_N_ELEMENTS (target_table),
1009 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1011 text_view->virtual_cursor_x = -1;
1012 text_view->virtual_cursor_y = -1;
1014 /* This object is completely private. No external entity can gain a reference
1015 * to it; so we create it here and destroy it in finalize ().
1017 text_view->im_context = gtk_im_multicontext_new ();
1019 g_signal_connect (text_view->im_context, "commit",
1020 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1021 g_signal_connect (text_view->im_context, "preedit_changed",
1022 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1023 g_signal_connect (text_view->im_context, "retrieve_surrounding",
1024 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1025 g_signal_connect (text_view->im_context, "delete_surrounding",
1026 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1028 text_view->cursor_visible = TRUE;
1030 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1033 text_view->drag_start_x = -1;
1034 text_view->drag_start_y = -1;
1036 text_view->pending_place_cursor_button = 0;
1038 /* We handle all our own redrawing */
1039 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1043 * gtk_text_view_new:
1045 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1046 * before using the text view, an empty default buffer will be created
1047 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1048 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1050 * Return value: a new #GtkTextView
1053 gtk_text_view_new (void)
1055 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1059 * gtk_text_view_new_with_buffer:
1060 * @buffer: a #GtkTextBuffer
1062 * Creates a new #GtkTextView widget displaying the buffer
1063 * @buffer. One buffer can be shared among many widgets.
1064 * @buffer may be NULL to create a default buffer, in which case
1065 * this function is equivalent to gtk_text_view_new(). The
1066 * text view adds its own reference count to the buffer; it does not
1067 * take over an existing reference.
1069 * Return value: a new #GtkTextView.
1072 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1074 GtkTextView *text_view;
1076 text_view = (GtkTextView*)gtk_text_view_new ();
1078 gtk_text_view_set_buffer (text_view, buffer);
1080 return GTK_WIDGET (text_view);
1084 * gtk_text_view_set_buffer:
1085 * @text_view: a #GtkTextView
1086 * @buffer: a #GtkTextBuffer
1088 * Sets @buffer as the buffer being displayed by @text_view. The previous
1089 * buffer displayed by the text view is unreferenced, and a reference is
1090 * added to @buffer. If you owned a reference to @buffer before passing it
1091 * to this function, you must remove that reference yourself; #GtkTextView
1092 * will not "adopt" it.
1096 gtk_text_view_set_buffer (GtkTextView *text_view,
1097 GtkTextBuffer *buffer)
1099 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1100 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1102 if (text_view->buffer == buffer)
1105 if (text_view->buffer != NULL)
1107 /* Destroy all anchored children */
1111 copy = g_slist_copy (text_view->children);
1113 while (tmp_list != NULL)
1115 GtkTextViewChild *vc = tmp_list->data;
1119 gtk_widget_destroy (vc->widget);
1120 /* vc may now be invalid! */
1123 tmp_list = g_slist_next (tmp_list);
1126 g_slist_free (copy);
1128 g_signal_handlers_disconnect_by_func (text_view->buffer,
1129 gtk_text_view_mark_set_handler,
1131 g_object_unref (text_view->buffer);
1132 text_view->dnd_mark = NULL;
1134 if (GTK_WIDGET_REALIZED (text_view))
1136 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1137 GDK_SELECTION_PRIMARY);
1138 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1142 text_view->buffer = buffer;
1148 g_object_ref (buffer);
1150 if (text_view->layout)
1151 gtk_text_layout_set_buffer (text_view->layout, buffer);
1153 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1155 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1159 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1163 text_view->first_para_pixels = 0;
1165 g_signal_connect (text_view->buffer, "mark_set",
1166 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1168 if (GTK_WIDGET_REALIZED (text_view))
1170 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1171 GDK_SELECTION_PRIMARY);
1172 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1176 if (GTK_WIDGET_VISIBLE (text_view))
1177 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1179 DV(g_print ("Invalidating due to set_buffer\n"));
1180 gtk_text_view_invalidate (text_view);
1183 static GtkTextBuffer*
1184 get_buffer (GtkTextView *text_view)
1186 if (text_view->buffer == NULL)
1189 b = gtk_text_buffer_new (NULL);
1190 gtk_text_view_set_buffer (text_view, b);
1194 return text_view->buffer;
1198 * gtk_text_view_get_buffer:
1199 * @text_view: a #GtkTextView
1201 * Returns the #GtkTextBuffer being displayed by this text view.
1202 * The reference count on the buffer is not incremented; the caller
1203 * of this function won't own a new reference.
1205 * Return value: a #GtkTextBuffer
1208 gtk_text_view_get_buffer (GtkTextView *text_view)
1210 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1212 return get_buffer (text_view);
1216 * gtk_text_view_get_iter_at_location:
1217 * @text_view: a #GtkTextView
1218 * @iter: a #GtkTextIter
1219 * @x: x position, in buffer coordinates
1220 * @y: y position, in buffer coordinates
1222 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1223 * coordinates are coordinates for the entire buffer, not just the
1224 * currently-displayed portion. If you have coordinates from an
1225 * event, you have to convert those to buffer coordinates with
1226 * gtk_text_view_window_to_buffer_coords().
1230 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1235 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1236 g_return_if_fail (iter != NULL);
1238 gtk_text_view_ensure_layout (text_view);
1240 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1247 * gtk_text_view_get_iter_location:
1248 * @text_view: a #GtkTextView
1249 * @iter: a #GtkTextIter
1250 * @location: bounds of the character at @iter
1252 * Gets a rectangle which roughly contains the character at @iter.
1253 * The rectangle position is in buffer coordinates; use
1254 * gtk_text_view_buffer_to_window_coords() to convert these
1255 * coordinates to coordinates for one of the windows in the text view.
1259 gtk_text_view_get_iter_location (GtkTextView *text_view,
1260 const GtkTextIter *iter,
1261 GdkRectangle *location)
1263 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1264 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1266 gtk_text_view_ensure_layout (text_view);
1268 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1272 * gtk_text_view_get_line_yrange:
1273 * @text_view: a #GtkTextView
1274 * @iter: a #GtkTextIter
1275 * @y: return location for a y coordinate
1276 * @height: return location for a height
1278 * Gets the y coordinate of the top of the line containing @iter,
1279 * and the height of the line. The coordinate is a buffer coordinate;
1280 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1284 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1285 const GtkTextIter *iter,
1289 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1290 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1292 gtk_text_view_ensure_layout (text_view);
1294 gtk_text_layout_get_line_yrange (text_view->layout,
1301 * gtk_text_view_get_line_at_y:
1302 * @text_view: a #GtkTextView
1303 * @target_iter: a #GtkTextIter
1304 * @y: a y coordinate
1305 * @line_top: return location for top coordinate of the line
1307 * Gets the #GtkTextIter at the start of the line containing
1308 * the coordinate @y. @y is in buffer coordinates, convert from
1309 * window coordinates with gtk_text_view_window_to_buffer_coords().
1310 * If non-%NULL, @line_top will be filled with the coordinate of the top
1314 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1315 GtkTextIter *target_iter,
1319 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1321 gtk_text_view_ensure_layout (text_view);
1323 gtk_text_layout_get_line_at_y (text_view->layout,
1330 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1332 DV (g_print (" Setting adj to raw value %g\n", val));
1334 /* We don't really want to clamp to upper; we want to clamp to
1335 upper - page_size which is the highest value the scrollbar
1336 will let us reach. */
1337 if (val > (adj->upper - adj->page_size))
1338 val = adj->upper - adj->page_size;
1340 if (val < adj->lower)
1343 if (val != adj->value)
1345 DV (g_print (" Setting adj to clamped value %g\n", val));
1346 gtk_adjustment_set_value (adj, val);
1354 * gtk_text_view_scroll_to_iter:
1355 * @text_view: a #GtkTextView
1356 * @iter: a #GtkTextIter
1357 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1358 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1359 * @xalign: horizontal alignment of mark within visible area.
1360 * @yalign: vertical alignment of mark within visible area
1362 * Scrolls @text_view so that @iter is on the screen in the position
1363 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1364 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1365 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1366 * possibly not scrolling at all. The effective screen for purposes
1367 * of this function is reduced by a margin of size @within_margin.
1368 * NOTE: This function uses the currently-computed height of the
1369 * lines in the text buffer. Note that line heights are computed
1370 * in an idle handler; so this function may not have the desired effect
1371 * if it's called before the height computations. To avoid oddness,
1372 * consider using gtk_text_view_scroll_to_mark() which saves a point
1373 * to be scrolled to after line validation.
1375 * Return value: %TRUE if scrolling occurred
1378 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1380 gdouble within_margin,
1386 GdkRectangle screen;
1391 gboolean retval = FALSE;
1393 gint screen_xoffset, screen_yoffset;
1394 gint current_x_scroll, current_y_scroll;
1396 /* FIXME why don't we do the validate-at-scroll-destination thing
1397 * from flush_scroll in this function? I think it wasn't done before
1398 * because changed_handler was screwed up, but I could be wrong.
1401 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1402 g_return_val_if_fail (iter != NULL, FALSE);
1403 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1404 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1405 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1407 widget = GTK_WIDGET (text_view);
1409 DV(g_print(G_STRLOC"\n"));
1411 gtk_text_layout_get_iter_location (text_view->layout,
1415 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1417 current_x_scroll = text_view->xoffset;
1418 current_y_scroll = text_view->yoffset;
1420 screen.x = current_x_scroll;
1421 screen.y = current_y_scroll;
1422 screen.width = SCREEN_WIDTH (widget);
1423 screen.height = SCREEN_HEIGHT (widget);
1425 screen_xoffset = screen.width * within_margin;
1426 screen_yoffset = screen.height * within_margin;
1428 screen.x += screen_xoffset;
1429 screen.y += screen_yoffset;
1430 screen.width -= screen_xoffset * 2;
1431 screen.height -= screen_yoffset * 2;
1433 /* paranoia check */
1434 if (screen.width < 1)
1436 if (screen.height < 1)
1439 screen_right = screen.x + screen.width;
1440 screen_bottom = screen.y + screen.height;
1442 /* The alignment affects the point in the target character that we
1443 * choose to align. If we're doing right/bottom alignment, we align
1444 * the right/bottom edge of the character the mark is at; if we're
1445 * doing left/top we align the left/top edge of the character; if
1446 * we're doing center alignment we align the center of the
1450 /* Vertical scroll */
1453 scroll_dest = current_y_scroll;
1457 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1459 /* if scroll_dest < screen.y, we move a negative increment (up),
1460 * else a positive increment (down)
1462 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1466 /* move minimum to get onscreen */
1467 if (rect.y < screen.y)
1469 scroll_dest = rect.y;
1470 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1472 else if ((rect.y + rect.height) > screen_bottom)
1474 scroll_dest = rect.y + rect.height;
1475 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1479 if (scroll_inc != 0)
1481 retval = set_adjustment_clamped (get_vadjustment (text_view),
1482 current_y_scroll + scroll_inc);
1484 DV (g_print (" vert increment %d\n", scroll_inc));
1487 /* Horizontal scroll */
1490 scroll_dest = current_x_scroll;
1494 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1496 /* if scroll_dest < screen.y, we move a negative increment (left),
1497 * else a positive increment (right)
1499 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1503 /* move minimum to get onscreen */
1504 if (rect.x < screen.x)
1506 scroll_dest = rect.x;
1507 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1509 else if ((rect.x + rect.width) > screen_right)
1511 scroll_dest = rect.x + rect.width;
1512 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1516 if (scroll_inc != 0)
1518 retval = set_adjustment_clamped (get_hadjustment (text_view),
1519 current_x_scroll + scroll_inc);
1521 DV (g_print (" horiz increment %d\n", scroll_inc));
1525 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1527 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1533 free_pending_scroll (GtkTextPendingScroll *scroll)
1535 if (!gtk_text_mark_get_deleted (scroll->mark))
1536 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1538 g_object_unref (scroll->mark);
1543 cancel_pending_scroll (GtkTextView *text_view)
1545 if (text_view->pending_scroll)
1547 free_pending_scroll (text_view->pending_scroll);
1548 text_view->pending_scroll = NULL;
1553 gtk_text_view_queue_scroll (GtkTextView *text_view,
1555 gdouble within_margin,
1561 GtkTextPendingScroll *scroll;
1563 DV(g_print(G_STRLOC"\n"));
1565 scroll = g_new (GtkTextPendingScroll, 1);
1567 scroll->within_margin = within_margin;
1568 scroll->use_align = use_align;
1569 scroll->xalign = xalign;
1570 scroll->yalign = yalign;
1572 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1574 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1577 gtk_text_mark_get_left_gravity (mark));
1579 g_object_ref (scroll->mark);
1581 cancel_pending_scroll (text_view);
1583 text_view->pending_scroll = scroll;
1587 gtk_text_view_flush_scroll (GtkTextView *text_view)
1590 GtkTextPendingScroll *scroll;
1594 widget = GTK_WIDGET (text_view);
1596 DV(g_print(G_STRLOC"\n"));
1598 if (text_view->pending_scroll == NULL)
1600 DV (g_print ("in flush scroll, no pending scroll\n"));
1604 scroll = text_view->pending_scroll;
1606 /* avoid recursion */
1607 text_view->pending_scroll = NULL;
1609 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1611 /* Validate area around the scroll destination, so the adjustment
1612 * can meaningfully point into that area. We must validate
1613 * enough area to be sure that after we scroll, everything onscreen
1614 * is valid; otherwise, validation will maintain the first para
1615 * in one place, but may push the target iter off the bottom of
1618 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1619 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1620 - (widget->allocation.height * 2),
1621 widget->allocation.height * 2);
1623 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1625 /* Ensure we have updated width/height */
1626 gtk_text_view_update_adjustments (text_view);
1628 retval = gtk_text_view_scroll_to_iter (text_view,
1630 scroll->within_margin,
1635 free_pending_scroll (scroll);
1641 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1643 if (upper != adj->upper)
1645 gdouble min = MAX (0.0, upper - adj->page_size);
1646 gboolean value_changed = FALSE;
1650 if (adj->value > min)
1653 value_changed = TRUE;
1656 gtk_adjustment_changed (adj);
1657 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1661 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1662 gtk_adjustment_value_changed (adj);
1668 gtk_text_view_update_adjustments (GtkTextView *text_view)
1670 gint width = 0, height = 0;
1672 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1674 if (text_view->layout)
1675 gtk_text_layout_get_size (text_view->layout, &width, &height);
1677 if (text_view->width != width || text_view->height != height)
1679 text_view->width = width;
1680 text_view->height = height;
1682 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1683 MAX (SCREEN_WIDTH (text_view), width));
1684 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1685 MAX (SCREEN_HEIGHT (text_view), height));
1687 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1689 /* Set up the step sizes; we'll say that a page is
1690 our allocation minus one step, and a step is
1691 1/10 of our allocation. */
1692 text_view->hadjustment->step_increment =
1693 SCREEN_WIDTH (text_view) / 10.0;
1694 text_view->hadjustment->page_increment =
1695 SCREEN_WIDTH (text_view) * 0.9;
1697 text_view->vadjustment->step_increment =
1698 SCREEN_HEIGHT (text_view) / 10.0;
1699 text_view->vadjustment->page_increment =
1700 SCREEN_HEIGHT (text_view) * 0.9;
1702 gtk_adjustment_changed (get_hadjustment (text_view));
1703 gtk_adjustment_changed (get_vadjustment (text_view));
1708 gtk_text_view_update_layout_width (GtkTextView *text_view)
1710 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1712 gtk_text_view_ensure_layout (text_view);
1714 gtk_text_layout_set_screen_width (text_view->layout,
1715 SCREEN_WIDTH (text_view));
1719 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1722 gint cursor_x_pos, cursor_y_pos;
1724 if (text_view->layout == NULL)
1727 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1729 area.x = cursor_x_pos;
1730 area.y = cursor_y_pos;
1731 area.width = area.height = 0;
1733 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1737 * gtk_text_view_scroll_to_mark:
1738 * @text_view: a #GtkTextView
1739 * @mark: a #GtkTextMark
1740 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1741 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1742 * @xalign: horizontal alignment of mark within visible area.
1743 * @yalign: vertical alignment of mark within visible area
1745 * Scrolls @text_view so that @mark is on the screen in the position
1746 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1747 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1748 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1749 * possibly not scrolling at all. The effective screen for purposes
1750 * of this function is reduced by a margin of size @within_margin.
1754 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1756 gdouble within_margin,
1761 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1762 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1763 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1764 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1765 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1767 gtk_text_view_queue_scroll (text_view, mark,
1773 /* If no validation is pending, we need to go ahead and force an
1776 if (text_view->layout &&
1777 gtk_text_layout_is_valid (text_view->layout))
1778 gtk_text_view_flush_scroll (text_view);
1782 * gtk_text_view_scroll_mark_onscreen:
1783 * @text_view: a #GtkTextView
1784 * @mark: a mark in the buffer for @text_view
1786 * Scrolls @text_view the minimum distance such that @mark is contained
1787 * within the visible area of the widget.
1791 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1794 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1795 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1797 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1801 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1803 GdkRectangle visible_rect;
1804 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1806 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1808 visible_rect.y + visible_rect.height);
1812 * gtk_text_view_move_mark_onscreen:
1813 * @text_view: a #GtkTextView
1814 * @mark: a #GtkTextMark
1816 * Moves a mark within the buffer so that it's
1817 * located within the currently-visible text area.
1819 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1822 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1827 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1828 g_return_val_if_fail (mark != NULL, FALSE);
1830 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1832 if (clamp_iter_onscreen (text_view, &iter))
1834 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1842 * gtk_text_view_get_visible_rect:
1843 * @text_view: a #GtkTextView
1844 * @visible_rect: rectangle to fill
1846 * Fills @visible_rect with the currently-visible
1847 * region of the buffer, in buffer coordinates. Convert to window coordinates
1848 * with gtk_text_view_buffer_to_window_coords().
1851 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1852 GdkRectangle *visible_rect)
1856 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1858 widget = GTK_WIDGET (text_view);
1862 visible_rect->x = text_view->xoffset;
1863 visible_rect->y = text_view->yoffset;
1864 visible_rect->width = SCREEN_WIDTH (widget);
1865 visible_rect->height = SCREEN_HEIGHT (widget);
1867 DV(g_print(" visible rect: %d,%d %d x %d\n",
1870 visible_rect->width,
1871 visible_rect->height));
1876 * gtk_text_view_set_wrap_mode:
1877 * @text_view: a #GtkTextView
1878 * @wrap_mode: a #GtkWrapMode
1880 * Sets the line wrapping for the view.
1883 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1884 GtkWrapMode wrap_mode)
1886 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1888 if (text_view->wrap_mode != wrap_mode)
1890 text_view->wrap_mode = wrap_mode;
1892 if (text_view->layout)
1894 text_view->layout->default_style->wrap_mode = wrap_mode;
1895 gtk_text_layout_default_style_changed (text_view->layout);
1899 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1903 * gtk_text_view_get_wrap_mode:
1904 * @text_view: a #GtkTextView
1906 * Gets the line wrapping for the view.
1908 * Return value: the line wrap setting
1911 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1913 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1915 return text_view->wrap_mode;
1919 * gtk_text_view_set_editable:
1920 * @text_view: a #GtkTextView
1921 * @setting: whether it's editable
1923 * Sets the default editability of the #GtkTextView. You can override
1924 * this default setting with tags in the buffer, using the "editable"
1925 * attribute of tags.
1928 gtk_text_view_set_editable (GtkTextView *text_view,
1931 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1932 setting = setting != FALSE;
1934 if (text_view->editable != setting)
1936 text_view->editable = setting;
1938 if (text_view->layout)
1940 text_view->layout->default_style->editable = text_view->editable;
1941 gtk_text_layout_default_style_changed (text_view->layout);
1944 g_object_notify (G_OBJECT (text_view), "editable");
1949 * gtk_text_view_get_editable:
1950 * @text_view: a #GtkTextView
1952 * Returns the default editability of the #GtkTextView. Tags in the
1953 * buffer may override this setting for some ranges of text.
1955 * Return value: whether text is editable by default
1958 gtk_text_view_get_editable (GtkTextView *text_view)
1960 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1962 return text_view->editable;
1966 * gtk_text_view_set_pixels_above_lines:
1967 * @text_view: a #GtkTextView
1968 * @pixels_above_lines: pixels above paragraphs
1970 * Sets the default number of blank pixels above paragraphs in @text_view.
1971 * Tags in the buffer for @text_view may override the defaults.
1975 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1976 gint pixels_above_lines)
1978 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1980 if (text_view->pixels_above_lines != pixels_above_lines)
1982 text_view->pixels_above_lines = pixels_above_lines;
1984 if (text_view->layout)
1986 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1987 gtk_text_layout_default_style_changed (text_view->layout);
1990 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1995 * gtk_text_view_get_pixels_above_lines:
1996 * @text_view: a #GtkTextView
1998 * Gets the default number of pixels to put above paragraphs.
2000 * Return value: default number of pixels above paragraphs
2003 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2005 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2007 return text_view->pixels_above_lines;
2011 * gtk_text_view_set_pixels_below_lines:
2012 * @text_view: a #GtkTextView
2013 * @pixels_below_lines: pixels below paragraphs
2015 * Sets the default number of pixels of blank space
2016 * to put below paragraphs in @text_view. May be overridden
2017 * by tags applied to @text_view's buffer.
2021 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2022 gint pixels_below_lines)
2024 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2026 if (text_view->pixels_below_lines != pixels_below_lines)
2028 text_view->pixels_below_lines = pixels_below_lines;
2030 if (text_view->layout)
2032 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2033 gtk_text_layout_default_style_changed (text_view->layout);
2036 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
2041 * gtk_text_view_get_pixels_below_lines:
2042 * @text_view: a #GtkTextView
2044 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2046 * Return value: default number of blank pixels below paragraphs
2049 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2051 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2053 return text_view->pixels_below_lines;
2057 * gtk_text_view_set_pixels_inside_wrap:
2058 * @text_view: a #GtkTextView
2059 * @pixels_inside_wrap: default number of pixels between wrapped lines
2061 * Sets the default number of pixels of blank space to leave between
2062 * display/wrapped lines within a paragraph. May be overridden by
2063 * tags in @text_view's buffer.
2067 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2068 gint pixels_inside_wrap)
2070 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2072 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2074 text_view->pixels_inside_wrap = pixels_inside_wrap;
2076 if (text_view->layout)
2078 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2079 gtk_text_layout_default_style_changed (text_view->layout);
2082 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2087 * gtk_text_view_get_pixels_inside_wrap:
2088 * @text_view: a #GtkTextView
2090 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2092 * Return value: default number of pixels of blank space between wrapped lines
2095 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2097 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2099 return text_view->pixels_inside_wrap;
2103 * gtk_text_view_set_justification:
2104 * @text_view: a #GtkTextView
2105 * @justification: justification
2107 * Sets the default justification of text in @text_view.
2108 * Tags in the view's buffer may override the default.
2112 gtk_text_view_set_justification (GtkTextView *text_view,
2113 GtkJustification justification)
2115 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2117 if (text_view->justify != justification)
2119 text_view->justify = justification;
2121 if (text_view->layout)
2123 text_view->layout->default_style->justification = justification;
2124 gtk_text_layout_default_style_changed (text_view->layout);
2127 g_object_notify (G_OBJECT (text_view), "justification");
2132 * gtk_text_view_get_justification:
2133 * @text_view: a #GtkTextView
2135 * Gets the default justification of paragraphs in @text_view.
2136 * Tags in the buffer may override the default.
2138 * Return value: default justification
2141 gtk_text_view_get_justification (GtkTextView *text_view)
2143 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2145 return text_view->justify;
2149 * gtk_text_view_set_left_margin:
2150 * @text_view: a #GtkTextView
2151 * @left_margin: left margin in pixels
2153 * Sets the default left margin for text in @text_view.
2154 * Tags in the buffer may override the default.
2158 gtk_text_view_set_left_margin (GtkTextView *text_view,
2161 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2163 if (text_view->left_margin != left_margin)
2165 text_view->left_margin = left_margin;
2167 if (text_view->layout)
2169 text_view->layout->default_style->left_margin = left_margin;
2170 gtk_text_layout_default_style_changed (text_view->layout);
2173 g_object_notify (G_OBJECT (text_view), "left_margin");
2178 * gtk_text_view_get_left_margin:
2179 * @text_view: a #GtkTextView
2181 * Gets the default left margin size of paragraphs in the @text_view.
2182 * Tags in the buffer may override the default.
2184 * Return value: left margin in pixels
2187 gtk_text_view_get_left_margin (GtkTextView *text_view)
2189 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2191 return text_view->left_margin;
2195 * gtk_text_view_set_right_margin:
2196 * @text_view: a #GtkTextView
2197 * @right_margin: right margin in pixels
2199 * Sets the default right margin for text in the text view.
2200 * Tags in the buffer may override the default.
2204 gtk_text_view_set_right_margin (GtkTextView *text_view,
2207 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2209 if (text_view->right_margin != right_margin)
2211 text_view->right_margin = right_margin;
2213 if (text_view->layout)
2215 text_view->layout->default_style->right_margin = right_margin;
2216 gtk_text_layout_default_style_changed (text_view->layout);
2219 g_object_notify (G_OBJECT (text_view), "right_margin");
2224 * gtk_text_view_get_right_margin:
2225 * @text_view: a #GtkTextView
2227 * Gets the default right margin for text in @text_view. Tags
2228 * in the buffer may override the default.
2230 * Return value: right margin in pixels
2233 gtk_text_view_get_right_margin (GtkTextView *text_view)
2235 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2237 return text_view->right_margin;
2241 * gtk_text_view_set_indent:
2242 * @text_view: a #GtkTextView
2243 * @indent: indentation in pixels
2245 * Sets the default indentation for paragraphs in @text_view.
2246 * Tags in the buffer may override the default.
2250 gtk_text_view_set_indent (GtkTextView *text_view,
2253 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2255 if (text_view->indent != indent)
2257 text_view->indent = indent;
2259 if (text_view->layout)
2261 text_view->layout->default_style->indent = indent;
2262 gtk_text_layout_default_style_changed (text_view->layout);
2265 g_object_notify (G_OBJECT (text_view), "indent");
2270 * gtk_text_view_get_indent:
2271 * @text_view: a #GtkTextView
2273 * Gets the default indentation of paragraphs in @text_view.
2274 * Tags in the view's buffer may override the default.
2275 * The indentation may be negative.
2277 * Return value: number of pixels of indentation
2280 gtk_text_view_get_indent (GtkTextView *text_view)
2282 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2284 return text_view->indent;
2288 * gtk_text_view_set_tabs:
2289 * @text_view: a #GtkTextView
2290 * @tabs: tabs as a #PangoTabArray
2292 * Sets the default tab stops for paragraphs in @text_view.
2293 * Tags in the buffer may override the default.
2297 gtk_text_view_set_tabs (GtkTextView *text_view,
2298 PangoTabArray *tabs)
2300 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2302 if (text_view->tabs)
2303 pango_tab_array_free (text_view->tabs);
2305 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2307 if (text_view->layout)
2309 /* some unkosher futzing in internal struct details... */
2310 if (text_view->layout->default_style->tabs)
2311 pango_tab_array_free (text_view->layout->default_style->tabs);
2313 text_view->layout->default_style->tabs =
2314 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2316 gtk_text_layout_default_style_changed (text_view->layout);
2319 g_object_notify (G_OBJECT (text_view), "tabs");
2323 * gtk_text_view_get_tabs:
2324 * @text_view: a #GtkTextView
2326 * Gets the default tabs for @text_view. Tags in the buffer may
2327 * override the defaults. The returned array will be %NULL if
2328 * "standard" (8-space) tabs are used. Free the return value
2329 * with pango_tab_array_free().
2331 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2334 gtk_text_view_get_tabs (GtkTextView *text_view)
2336 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2338 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2342 * gtk_text_view_set_cursor_visible:
2343 * @text_view: a #GtkTextView
2344 * @setting: whether to show the insertion cursor
2346 * Toggles whether the insertion point is displayed. A buffer with no editable
2347 * text probably shouldn't have a visible cursor, so you may want to turn
2351 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2354 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2356 setting = (setting != FALSE);
2358 if (text_view->cursor_visible != setting)
2360 text_view->cursor_visible = setting;
2362 if (GTK_WIDGET_HAS_FOCUS (text_view))
2364 if (text_view->layout)
2366 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2367 gtk_text_view_check_cursor_blink (text_view);
2371 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2376 * gtk_text_view_get_cursor_visible:
2377 * @text_view: a #GtkTextView
2379 * Find out whether the cursor is being displayed.
2381 * Return value: whether the insertion mark is visible
2384 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2386 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2388 return text_view->cursor_visible;
2393 * gtk_text_view_place_cursor_onscreen:
2394 * @text_view: a #GtkTextView
2396 * Moves the cursor to the currently visible region of the
2397 * buffer, it it isn't there already.
2399 * Return value: TRUE if the cursor had to be moved.
2402 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2406 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2408 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2409 gtk_text_buffer_get_mark (get_buffer (text_view),
2412 if (clamp_iter_onscreen (text_view, &insert))
2414 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2422 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2424 if (text_view->first_validate_idle != 0)
2426 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2427 g_source_remove (text_view->first_validate_idle);
2428 text_view->first_validate_idle = 0;
2431 if (text_view->incremental_validate_idle != 0)
2433 g_source_remove (text_view->incremental_validate_idle);
2434 text_view->incremental_validate_idle = 0;
2439 gtk_text_view_destroy (GtkObject *object)
2441 GtkTextView *text_view;
2442 GtkTextLayout *layout;
2444 text_view = GTK_TEXT_VIEW (object);
2446 layout = text_view->layout;
2448 gtk_text_view_remove_validate_idles (text_view);
2449 gtk_text_view_set_buffer (text_view, NULL);
2450 gtk_text_view_destroy_layout (text_view);
2452 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2456 gtk_text_view_finalize (GObject *object)
2458 GtkTextView *text_view;
2460 text_view = GTK_TEXT_VIEW (object);
2462 g_return_if_fail (text_view->buffer == NULL);
2464 gtk_text_view_destroy_layout (text_view);
2465 gtk_text_view_set_buffer (text_view, NULL);
2467 cancel_pending_scroll (text_view);
2469 if (text_view->tabs)
2470 pango_tab_array_free (text_view->tabs);
2472 if (text_view->hadjustment)
2473 g_object_unref (text_view->hadjustment);
2474 if (text_view->vadjustment)
2475 g_object_unref (text_view->vadjustment);
2477 text_window_free (text_view->text_window);
2479 if (text_view->left_window)
2480 text_window_free (text_view->left_window);
2482 if (text_view->top_window)
2483 text_window_free (text_view->top_window);
2485 if (text_view->right_window)
2486 text_window_free (text_view->right_window);
2488 if (text_view->bottom_window)
2489 text_window_free (text_view->bottom_window);
2491 g_object_unref (text_view->im_context);
2493 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2497 gtk_text_view_set_property (GObject *object,
2499 const GValue *value,
2502 GtkTextView *text_view;
2504 text_view = GTK_TEXT_VIEW (object);
2508 case PROP_PIXELS_ABOVE_LINES:
2509 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2512 case PROP_PIXELS_BELOW_LINES:
2513 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2516 case PROP_PIXELS_INSIDE_WRAP:
2517 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2521 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2524 case PROP_WRAP_MODE:
2525 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2528 case PROP_JUSTIFICATION:
2529 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2532 case PROP_LEFT_MARGIN:
2533 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2536 case PROP_RIGHT_MARGIN:
2537 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2541 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2545 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2548 case PROP_CURSOR_VISIBLE:
2549 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2553 g_assert_not_reached ();
2559 gtk_text_view_get_property (GObject *object,
2564 GtkTextView *text_view;
2566 text_view = GTK_TEXT_VIEW (object);
2570 case PROP_PIXELS_ABOVE_LINES:
2571 g_value_set_int (value, text_view->pixels_above_lines);
2574 case PROP_PIXELS_BELOW_LINES:
2575 g_value_set_int (value, text_view->pixels_below_lines);
2578 case PROP_PIXELS_INSIDE_WRAP:
2579 g_value_set_int (value, text_view->pixels_inside_wrap);
2583 g_value_set_boolean (value, text_view->editable);
2586 case PROP_WRAP_MODE:
2587 g_value_set_enum (value, text_view->wrap_mode);
2590 case PROP_JUSTIFICATION:
2591 g_value_set_enum (value, text_view->justify);
2594 case PROP_LEFT_MARGIN:
2595 g_value_set_int (value, text_view->left_margin);
2598 case PROP_RIGHT_MARGIN:
2599 g_value_set_int (value, text_view->right_margin);
2603 g_value_set_int (value, text_view->indent);
2607 g_value_set_boxed (value, text_view->tabs);
2610 case PROP_CURSOR_VISIBLE:
2611 g_value_set_boolean (value, text_view->cursor_visible);
2615 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2621 gtk_text_view_size_request (GtkWidget *widget,
2622 GtkRequisition *requisition)
2624 GtkTextView *text_view;
2626 gint focus_edge_width;
2628 gboolean interior_focus;
2630 text_view = GTK_TEXT_VIEW (widget);
2632 gtk_widget_style_get (widget,
2633 "interior_focus", &interior_focus,
2634 "focus_line_width", &focus_width,
2638 focus_edge_width = 0;
2640 focus_edge_width = focus_width;
2642 if (text_view->layout)
2644 text_view->text_window->requisition.width = text_view->layout->width;
2645 text_view->text_window->requisition.height = text_view->layout->height;
2649 text_view->text_window->requisition.width = 0;
2650 text_view->text_window->requisition.height = 0;
2653 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2654 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2656 if (text_view->left_window)
2657 requisition->width += text_view->left_window->requisition.width;
2659 if (text_view->right_window)
2660 requisition->width += text_view->right_window->requisition.width;
2662 if (text_view->top_window)
2663 requisition->height += text_view->top_window->requisition.height;
2665 if (text_view->bottom_window)
2666 requisition->height += text_view->bottom_window->requisition.height;
2668 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2669 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2671 tmp_list = text_view->children;
2672 while (tmp_list != NULL)
2674 GtkTextViewChild *child = tmp_list->data;
2678 GtkRequisition child_req;
2679 GtkRequisition old_req;
2681 gtk_widget_get_child_requisition (child->widget, &old_req);
2683 gtk_widget_size_request (child->widget, &child_req);
2685 gtk_widget_get_child_requisition (child->widget, &child_req);
2687 /* Invalidate layout lines if required */
2688 if (text_view->layout &&
2689 (old_req.width != child_req.width ||
2690 old_req.height != child_req.height))
2691 gtk_text_child_anchor_queue_resize (child->anchor,
2696 GtkRequisition child_req;
2698 gtk_widget_size_request (child->widget, &child_req);
2701 tmp_list = g_slist_next (tmp_list);
2706 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2707 GtkTextViewChild *vc,
2708 GtkAllocation *allocation)
2714 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2718 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2721 buffer_y += vc->from_top_of_line;
2723 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2724 allocation->y = buffer_y - text_view->yoffset;
2726 gtk_widget_get_child_requisition (vc->widget, &req);
2727 allocation->width = req.width;
2728 allocation->height = req.height;
2732 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2733 GtkTextViewChild *vc)
2735 GtkAllocation allocation;
2737 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2739 gtk_widget_size_allocate (vc->widget, &allocation);
2742 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2744 vc->widget->allocation.x,
2745 vc->widget->allocation.y,
2746 text_view->yoffset);
2751 gtk_text_view_child_allocated (GtkTextLayout *layout,
2757 GtkTextViewChild *vc = NULL;
2758 GtkTextView *text_view = data;
2760 /* x,y is the position of the child from the top of the line, and
2761 * from the left of the buffer. We have to translate that into text
2762 * window coordinates, then size_allocate the child.
2765 vc = g_object_get_data (G_OBJECT (child),
2766 "gtk-text-view-child");
2768 g_assert (vc != NULL);
2770 DV (g_print ("child allocated at %d,%d\n", x, y));
2772 vc->from_left_of_buffer = x;
2773 vc->from_top_of_line = y;
2775 gtk_text_view_update_child_allocation (text_view, vc);
2779 gtk_text_view_allocate_children (GtkTextView *text_view)
2783 DV(g_print(G_STRLOC"\n"));
2785 tmp_list = text_view->children;
2786 while (tmp_list != NULL)
2788 GtkTextViewChild *child = tmp_list->data;
2792 /* We need to force-validate the regions containing
2795 GtkTextIter child_loc;
2796 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2800 gtk_text_layout_validate_yrange (text_view->layout,
2806 GtkAllocation allocation;
2807 GtkRequisition child_req;
2809 g_assert (child != NULL);
2811 allocation.x = child->x;
2812 allocation.y = child->y;
2814 gtk_widget_get_child_requisition (child->widget, &child_req);
2816 allocation.width = child_req.width;
2817 allocation.height = child_req.height;
2819 gtk_widget_size_allocate (child->widget, &allocation);
2822 tmp_list = g_slist_next (tmp_list);
2827 gtk_text_view_size_allocate (GtkWidget *widget,
2828 GtkAllocation *allocation)
2830 GtkTextView *text_view;
2831 GtkTextIter first_para;
2833 GtkAdjustment *vadj;
2834 gboolean yoffset_changed = FALSE;
2836 GdkRectangle text_rect;
2837 GdkRectangle left_rect;
2838 GdkRectangle right_rect;
2839 GdkRectangle top_rect;
2840 GdkRectangle bottom_rect;
2841 gint focus_edge_width;
2843 gboolean interior_focus;
2844 gboolean size_changed;
2846 text_view = GTK_TEXT_VIEW (widget);
2848 DV(g_print(G_STRLOC"\n"));
2851 widget->allocation.width != allocation->width ||
2852 widget->allocation.height != allocation->height;
2854 widget->allocation = *allocation;
2856 if (GTK_WIDGET_REALIZED (widget))
2858 gdk_window_move_resize (widget->window,
2859 allocation->x, allocation->y,
2860 allocation->width, allocation->height);
2863 /* distribute width/height among child windows. Ensure all
2864 * windows get at least a 1x1 allocation.
2867 gtk_widget_style_get (widget,
2868 "interior_focus", &interior_focus,
2869 "focus_line_width", &focus_width,
2873 focus_edge_width = 0;
2875 focus_edge_width = focus_width;
2877 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2879 if (text_view->left_window)
2880 left_rect.width = text_view->left_window->requisition.width;
2882 left_rect.width = 0;
2884 width -= left_rect.width;
2886 if (text_view->right_window)
2887 right_rect.width = text_view->right_window->requisition.width;
2889 right_rect.width = 0;
2891 width -= right_rect.width;
2893 text_rect.width = MAX (1, width);
2895 top_rect.width = text_rect.width;
2896 bottom_rect.width = text_rect.width;
2899 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2901 if (text_view->top_window)
2902 top_rect.height = text_view->top_window->requisition.height;
2904 top_rect.height = 0;
2906 height -= top_rect.height;
2908 if (text_view->bottom_window)
2909 bottom_rect.height = text_view->bottom_window->requisition.height;
2911 bottom_rect.height = 0;
2913 height -= bottom_rect.height;
2915 text_rect.height = MAX (1, height);
2917 left_rect.height = text_rect.height;
2918 right_rect.height = text_rect.height;
2921 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2922 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2924 text_rect.x = left_rect.x + left_rect.width;
2925 text_rect.y = top_rect.y + top_rect.height;
2927 left_rect.y = text_rect.y;
2928 right_rect.y = text_rect.y;
2930 top_rect.x = text_rect.x;
2931 bottom_rect.x = text_rect.x;
2933 right_rect.x = text_rect.x + text_rect.width;
2934 bottom_rect.y = text_rect.y + text_rect.height;
2936 text_window_size_allocate (text_view->text_window,
2939 if (text_view->left_window)
2940 text_window_size_allocate (text_view->left_window,
2943 if (text_view->right_window)
2944 text_window_size_allocate (text_view->right_window,
2947 if (text_view->top_window)
2948 text_window_size_allocate (text_view->top_window,
2951 if (text_view->bottom_window)
2952 text_window_size_allocate (text_view->bottom_window,
2955 gtk_text_view_update_layout_width (text_view);
2957 /* Note that this will do some layout validation */
2958 gtk_text_view_allocate_children (text_view);
2960 /* Now adjust the value of the adjustment to keep the cursor at the
2961 * same place in the buffer
2963 gtk_text_view_get_first_para_iter (text_view, &first_para);
2964 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2966 y += text_view->first_para_pixels;
2968 /* Ensure h/v adj exist */
2969 get_hadjustment (text_view);
2970 get_vadjustment (text_view);
2972 vadj = text_view->vadjustment;
2973 if (y > vadj->upper - vadj->page_size)
2974 y = MAX (0, vadj->upper - vadj->page_size);
2976 if (y != text_view->yoffset)
2979 yoffset_changed = TRUE;
2982 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2983 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
2984 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
2985 text_view->hadjustment->lower = 0;
2986 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2988 gtk_adjustment_changed (text_view->hadjustment);
2989 set_adjustment_clamped (text_view->hadjustment,
2990 text_view->hadjustment->upper - text_view->hadjustment->page_size);
2992 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2993 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2994 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2995 text_view->vadjustment->lower = 0;
2996 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2998 gtk_adjustment_changed (text_view->vadjustment);
3000 if (yoffset_changed)
3001 gtk_adjustment_value_changed (vadj);
3003 /* The GTK resize loop processes all the pending exposes right
3004 * after doing the resize stuff, so the idle sizer won't have a
3005 * chance to run. So we do the work here.
3007 gtk_text_view_flush_first_validate (text_view);
3009 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3012 if (size_changed && GTK_WIDGET_REALIZED (widget))
3013 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3017 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3020 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3021 text_view->first_para_mark);
3025 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3027 GtkWidget *widget = GTK_WIDGET (text_view);
3029 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3031 if (SCREEN_HEIGHT (widget) > 0)
3033 GtkTextIter first_para;
3035 /* Be sure we've validated the stuff onscreen; if we
3036 * scrolled, these calls won't have any effect, because
3037 * they were called in the recursive validate_onscreen
3039 gtk_text_view_get_first_para_iter (text_view, &first_para);
3041 gtk_text_layout_validate_yrange (text_view->layout,
3044 text_view->first_para_pixels +
3045 SCREEN_HEIGHT (widget));
3048 text_view->onscreen_validated = TRUE;
3050 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3052 /* This can have the odd side effect of triggering a scroll, which should
3053 * flip "onscreen_validated" back to FALSE, but should also get us
3054 * back into this function to turn it on again.
3056 gtk_text_view_update_adjustments (text_view);
3058 g_assert (text_view->onscreen_validated);
3062 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3064 if (text_view->first_validate_idle == 0)
3067 /* Do this first, which means that if an "invalidate"
3068 * occurs during any of this process, a new first_validate_callback
3069 * will be installed, and we'll start again.
3071 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3072 g_source_remove (text_view->first_validate_idle);
3073 text_view->first_validate_idle = 0;
3075 /* be sure we have up-to-date screen size set on the
3078 gtk_text_view_update_layout_width (text_view);
3080 /* Bail out if we invalidated stuff; scrolling right away will just
3081 * confuse the issue.
3083 if (text_view->first_validate_idle != 0)
3085 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3089 /* scroll to any marks, if that's pending. This can jump us to
3090 * the validation codepath used for scrolling onscreen, if so we
3091 * bail out. It won't jump if already in that codepath since
3092 * value_changed is not recursive, so also validate if
3095 if (!gtk_text_view_flush_scroll (text_view) ||
3096 !text_view->onscreen_validated)
3097 gtk_text_view_validate_onscreen (text_view);
3099 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3101 g_assert (text_view->onscreen_validated);
3106 first_validate_callback (gpointer data)
3108 GtkTextView *text_view = data;
3110 GDK_THREADS_ENTER ();
3112 /* Note that some of this code is duplicated at the end of size_allocate,
3113 * keep in sync with that.
3116 DV(g_print(G_STRLOC"\n"));
3118 gtk_text_view_flush_first_validate (text_view);
3120 GDK_THREADS_LEAVE ();
3126 incremental_validate_callback (gpointer data)
3128 GtkTextView *text_view = data;
3129 gboolean result = TRUE;
3131 GDK_THREADS_ENTER ();
3133 DV(g_print(G_STRLOC"\n"));
3135 gtk_text_layout_validate (text_view->layout, 2000);
3137 gtk_text_view_update_adjustments (text_view);
3139 if (gtk_text_layout_is_valid (text_view->layout))
3141 text_view->incremental_validate_idle = 0;
3145 GDK_THREADS_LEAVE ();
3151 gtk_text_view_invalidate (GtkTextView *text_view)
3153 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3154 text_view->onscreen_validated));
3156 text_view->onscreen_validated = FALSE;
3158 /* We'll invalidate when the layout is created */
3159 if (text_view->layout == NULL)
3162 if (!text_view->first_validate_idle)
3164 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3165 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3166 text_view->first_validate_idle));
3169 if (!text_view->incremental_validate_idle)
3171 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3172 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3173 text_view->incremental_validate_idle));
3178 invalidated_handler (GtkTextLayout *layout,
3181 GtkTextView *text_view;
3183 text_view = GTK_TEXT_VIEW (data);
3185 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3186 gtk_text_view_invalidate (text_view);
3190 changed_handler (GtkTextLayout *layout,
3196 GtkTextView *text_view;
3198 GdkRectangle visible_rect;
3199 GdkRectangle redraw_rect;
3201 text_view = GTK_TEXT_VIEW (data);
3202 widget = GTK_WIDGET (data);
3204 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3206 if (GTK_WIDGET_REALIZED (text_view))
3208 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3210 redraw_rect.x = visible_rect.x;
3211 redraw_rect.width = visible_rect.width;
3212 redraw_rect.y = start_y;
3214 if (old_height == new_height)
3215 redraw_rect.height = old_height;
3216 else if (start_y + old_height > visible_rect.y)
3217 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3219 redraw_rect.height = 0;
3221 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3223 /* text_window_invalidate_rect() takes buffer coordinates */
3224 text_window_invalidate_rect (text_view->text_window,
3227 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3231 redraw_rect.height));
3233 if (text_view->left_window)
3234 text_window_invalidate_rect (text_view->left_window,
3236 if (text_view->right_window)
3237 text_window_invalidate_rect (text_view->right_window,
3239 if (text_view->top_window)
3240 text_window_invalidate_rect (text_view->top_window,
3242 if (text_view->bottom_window)
3243 text_window_invalidate_rect (text_view->bottom_window,
3246 gtk_text_view_update_im_spot_location (text_view);
3250 if (old_height != new_height)
3252 gboolean yoffset_changed = FALSE;
3254 int new_first_para_top;
3255 int old_first_para_top;
3258 /* If the bottom of the old area was above the top of the
3259 * screen, we need to scroll to keep the current top of the
3260 * screen in place. Remember that first_para_pixels is the
3261 * position of the top of the screen in coordinates relative to
3262 * the first paragraph onscreen.
3264 * In short we are adding the height delta of the portion of the
3265 * changed region above first_para_mark to text_view->yoffset.
3267 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3268 text_view->first_para_mark);
3270 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3272 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3274 if (new_first_para_top != old_first_para_top)
3276 text_view->yoffset += new_first_para_top - old_first_para_top;
3278 get_vadjustment (text_view)->value = text_view->yoffset;
3279 yoffset_changed = TRUE;
3282 if (yoffset_changed)
3284 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3285 gtk_adjustment_value_changed (get_vadjustment (text_view));
3288 /* FIXME be smarter about which anchored widgets we update */
3290 tmp_list = text_view->children;
3291 while (tmp_list != NULL)
3293 GtkTextViewChild *child = tmp_list->data;
3296 gtk_text_view_update_child_allocation (text_view, child);
3298 tmp_list = g_slist_next (tmp_list);
3303 GtkRequisition old_req;
3304 GtkRequisition new_req;
3306 old_req = widget->requisition;
3308 /* Use this instead of gtk_widget_size_request wrapper
3309 * to avoid the optimization which just returns widget->requisition
3310 * if a resize hasn't been queued.
3312 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3314 if (old_req.width != new_req.width ||
3315 old_req.height != new_req.height)
3317 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3318 _gtk_size_group_queue_resize (widget);
3324 gtk_text_view_realize (GtkWidget *widget)
3326 GtkTextView *text_view;
3327 GdkWindowAttr attributes;
3328 gint attributes_mask;
3331 text_view = GTK_TEXT_VIEW (widget);
3332 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3334 attributes.window_type = GDK_WINDOW_CHILD;
3335 attributes.x = widget->allocation.x;
3336 attributes.y = widget->allocation.y;
3337 attributes.width = widget->allocation.width;
3338 attributes.height = widget->allocation.height;
3339 attributes.wclass = GDK_INPUT_OUTPUT;
3340 attributes.visual = gtk_widget_get_visual (widget);
3341 attributes.colormap = gtk_widget_get_colormap (widget);
3342 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3344 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3346 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3347 &attributes, attributes_mask);
3348 gdk_window_set_user_data (widget->window, widget);
3350 /* must come before text_window_realize calls */
3351 widget->style = gtk_style_attach (widget->style, widget->window);
3353 gdk_window_set_background (widget->window,
3354 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3356 text_window_realize (text_view->text_window, widget->window);
3358 if (text_view->left_window)
3359 text_window_realize (text_view->left_window,
3362 if (text_view->top_window)
3363 text_window_realize (text_view->top_window,
3366 if (text_view->right_window)
3367 text_window_realize (text_view->right_window,
3370 if (text_view->bottom_window)
3371 text_window_realize (text_view->bottom_window,
3374 gtk_text_view_ensure_layout (text_view);
3376 if (text_view->buffer)
3378 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3379 GDK_SELECTION_PRIMARY);
3380 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3383 tmp_list = text_view->children;
3384 while (tmp_list != NULL)
3386 GtkTextViewChild *vc = tmp_list->data;
3388 text_view_child_set_parent_window (text_view, vc);
3390 tmp_list = tmp_list->next;
3395 gtk_text_view_unrealize (GtkWidget *widget)
3397 GtkTextView *text_view;
3399 text_view = GTK_TEXT_VIEW (widget);
3401 if (text_view->buffer)
3403 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3404 GDK_SELECTION_PRIMARY);
3405 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3408 gtk_text_view_remove_validate_idles (text_view);
3410 if (text_view->popup_menu)
3412 gtk_widget_destroy (text_view->popup_menu);
3413 text_view->popup_menu = NULL;
3416 text_window_unrealize (text_view->text_window);
3418 if (text_view->left_window)
3419 text_window_unrealize (text_view->left_window);
3421 if (text_view->top_window)
3422 text_window_unrealize (text_view->top_window);
3424 if (text_view->right_window)
3425 text_window_unrealize (text_view->right_window);
3427 if (text_view->bottom_window)
3428 text_window_unrealize (text_view->bottom_window);
3430 gtk_text_view_destroy_layout (text_view);
3432 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3436 gtk_text_view_set_background (GtkTextView *text_view)
3438 GtkWidget *widget = GTK_WIDGET (text_view);
3440 gdk_window_set_background (widget->window,
3441 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3443 gdk_window_set_background (text_view->text_window->bin_window,
3444 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3446 if (text_view->left_window)
3447 gdk_window_set_background (text_view->left_window->bin_window,
3448 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3449 if (text_view->right_window)
3450 gdk_window_set_background (text_view->right_window->bin_window,
3451 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3453 if (text_view->top_window)
3454 gdk_window_set_background (text_view->top_window->bin_window,
3455 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3457 if (text_view->bottom_window)
3458 gdk_window_set_background (text_view->bottom_window->bin_window,
3459 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3463 gtk_text_view_style_set (GtkWidget *widget,
3464 GtkStyle *previous_style)
3466 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3468 if (GTK_WIDGET_REALIZED (widget))
3470 gtk_text_view_set_background (text_view);
3473 if (text_view->layout && previous_style)
3475 gtk_text_view_set_attributes_from_style (text_view,
3476 text_view->layout->default_style,
3478 gtk_text_layout_default_style_changed (text_view->layout);
3483 gtk_text_view_direction_changed (GtkWidget *widget,
3484 GtkTextDirection previous_direction)
3486 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3488 if (text_view->layout)
3490 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3491 gtk_text_layout_default_style_changed (text_view->layout);
3496 gtk_text_view_state_changed (GtkWidget *widget,
3497 GtkStateType previous_state)
3499 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3501 if (GTK_WIDGET_REALIZED (widget))
3503 gtk_text_view_set_background (text_view);
3506 if (!GTK_WIDGET_IS_SENSITIVE (widget))
3508 /* Clear any selection */
3509 gtk_text_view_unselect (text_view);
3512 gtk_widget_queue_draw (widget);
3516 set_invisible_cursor (GdkWindow *window)
3518 GdkBitmap *empty_bitmap;
3521 char invisible_cursor_bits[] = { 0x0 };
3523 useless.red = useless.green = useless.blue = 0;
3526 empty_bitmap = gdk_bitmap_create_from_data (window,
3527 invisible_cursor_bits,
3530 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3535 gdk_window_set_cursor (window, cursor);
3537 gdk_cursor_unref (cursor);
3539 g_object_unref (empty_bitmap);
3543 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3545 if (text_view->mouse_cursor_obscured)
3548 set_invisible_cursor (text_view->text_window->bin_window);
3550 text_view->mouse_cursor_obscured = TRUE;
3558 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3561 switch (event->type)
3563 case GDK_MOTION_NOTIFY:
3564 *x = event->motion.x;
3565 *y = event->motion.y;
3569 case GDK_BUTTON_PRESS:
3570 case GDK_2BUTTON_PRESS:
3571 case GDK_3BUTTON_PRESS:
3572 case GDK_BUTTON_RELEASE:
3573 *x = event->button.x;
3574 *y = event->button.y;
3579 case GDK_KEY_RELEASE:
3580 case GDK_ENTER_NOTIFY:
3581 case GDK_LEAVE_NOTIFY:
3582 case GDK_PROPERTY_NOTIFY:
3583 case GDK_SELECTION_CLEAR:
3584 case GDK_SELECTION_REQUEST:
3585 case GDK_SELECTION_NOTIFY:
3586 case GDK_PROXIMITY_IN:
3587 case GDK_PROXIMITY_OUT:
3588 case GDK_DRAG_ENTER:
3589 case GDK_DRAG_LEAVE:
3590 case GDK_DRAG_MOTION:
3591 case GDK_DRAG_STATUS:
3592 case GDK_DROP_START:
3593 case GDK_DROP_FINISHED:
3603 emit_event_on_tags (GtkWidget *widget,
3609 gboolean retval = FALSE;
3610 GtkTextView *text_view;
3612 text_view = GTK_TEXT_VIEW (widget);
3614 tags = gtk_text_iter_get_tags (iter);
3619 GtkTextTag *tag = tmp->data;
3621 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3627 tmp = g_slist_next (tmp);
3630 g_slist_free (tags);
3636 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3638 GtkTextView *text_view;
3641 text_view = GTK_TEXT_VIEW (widget);
3643 if (text_view->layout == NULL ||
3644 get_buffer (text_view) == NULL)
3647 if (event->any.window != text_view->text_window->bin_window)
3650 if (get_event_coordinates (event, &x, &y))
3654 x += text_view->xoffset;
3655 y += text_view->yoffset;
3657 /* FIXME this is slow and we do it twice per event.
3658 * My favorite solution is to have GtkTextLayout cache
3659 * the last couple lookups.
3661 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3665 return emit_event_on_tags (widget, event, &iter);
3667 else if (event->type == GDK_KEY_PRESS ||
3668 event->type == GDK_KEY_RELEASE)
3670 GtkTextMark *insert;
3673 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3676 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3678 return emit_event_on_tags (widget, event, &iter);
3685 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3687 gboolean retval = FALSE;
3688 gboolean obscure = FALSE;
3689 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3690 GtkTextMark *insert;
3692 gboolean can_insert;
3694 if (text_view->layout == NULL ||
3695 get_buffer (text_view) == NULL)
3698 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3699 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3700 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3702 gtk_im_context_filter_keypress (text_view->im_context, event))
3704 text_view->need_im_reset = TRUE;
3709 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3710 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3712 /* use overall editability not can_insert, more predictable for users */
3713 else if (text_view->editable &&
3714 (event->keyval == GDK_Return ||
3715 event->keyval == GDK_KP_Enter))
3717 /* this won't actually insert the newline if the cursor isn't
3720 gtk_text_view_commit_text (text_view, "\n");
3725 /* Pass through Tab as literal tab, unless Control is held down */
3726 else if ((event->keyval == GDK_Tab ||
3727 event->keyval == GDK_KP_Tab) &&
3728 !(event->state & GDK_CONTROL_MASK))
3730 /* If the text widget isn't editable overall, move the focus
3733 if (text_view->editable)
3735 gtk_text_view_commit_text (text_view, "\t");
3739 gtk_text_view_move_focus (text_view,
3740 (event->state & GDK_SHIFT_MASK) ?
3741 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3749 gtk_text_view_obscure_mouse_cursor (text_view);
3751 gtk_text_view_pend_cursor_blink (text_view);
3757 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3759 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3760 GtkTextMark *insert;
3763 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3766 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3767 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3768 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3769 gtk_im_context_filter_keypress (text_view->im_context, event))
3771 text_view->need_im_reset = TRUE;
3775 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3779 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3781 GtkTextView *text_view;
3783 text_view = GTK_TEXT_VIEW (widget);
3785 text_view->disable_scroll_on_focus = TRUE;
3786 gtk_widget_grab_focus (widget);
3787 text_view->disable_scroll_on_focus = FALSE;
3789 if (event->window != text_view->text_window->bin_window)
3791 /* Remove selection if any. */
3792 gtk_text_view_unselect (text_view);
3798 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3799 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3800 else if (event->button == 3)
3801 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3804 if (event->type == GDK_BUTTON_PRESS)
3806 gtk_text_view_reset_im_context (text_view);
3808 if (event->button == 1)
3810 /* If we're in the selection, start a drag copy/move of the
3811 * selection; otherwise, start creating a new selection.
3814 GtkTextIter start, end;
3816 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3818 event->x + text_view->xoffset,
3819 event->y + text_view->yoffset);
3821 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3823 gtk_text_iter_in_range (&iter, &start, &end))
3825 text_view->drag_start_x = event->x;
3826 text_view->drag_start_y = event->y;
3827 text_view->pending_place_cursor_button = event->button;
3831 gtk_text_view_start_selection_drag (text_view, &iter, event);
3836 else if (event->button == 2)
3840 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3842 event->x + text_view->xoffset,
3843 event->y + text_view->yoffset);
3845 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3846 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3848 text_view->editable);
3851 else if (event->button == 3)
3853 gtk_text_view_do_popup (text_view, event);
3857 else if ((event->type == GDK_2BUTTON_PRESS ||
3858 event->type == GDK_3BUTTON_PRESS) &&
3861 GtkTextIter start, end;
3863 /* End the selection drag, otherwise we'd clear the new
3864 * word/line selection on button release
3866 gtk_text_view_end_selection_drag (text_view, event);
3868 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3870 event->x + text_view->xoffset,
3871 event->y + text_view->yoffset);
3875 if (event->type == GDK_2BUTTON_PRESS)
3877 if (gtk_text_iter_inside_word (&start))
3879 if (!gtk_text_iter_starts_word (&start))
3880 gtk_text_iter_backward_word_start (&start);
3882 if (!gtk_text_iter_ends_word (&end))
3883 gtk_text_iter_forward_word_end (&end);
3886 else if (event->type == GDK_3BUTTON_PRESS)
3888 if (gtk_text_view_starts_display_line (text_view, &start))
3890 /* If on a display line boundary, we assume the user
3891 * clicked off the end of a line and we therefore select
3892 * the line before the boundary.
3894 gtk_text_view_backward_display_line_start (text_view, &start);
3898 /* start isn't on the start of a line, so we move it to the
3899 * start, and move end to the end unless it's already there.
3901 gtk_text_view_backward_display_line_start (text_view, &start);
3903 if (!gtk_text_view_starts_display_line (text_view, &end))
3904 gtk_text_view_forward_display_line_end (text_view, &end);
3908 if (event->state & GDK_SHIFT_MASK)
3910 /* Take union of old and new selection */
3911 GtkTextIter old_start, old_end;
3913 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3914 &old_start, &old_end);
3916 gtk_text_iter_order (&start, &old_start);
3917 gtk_text_iter_order (&old_end, &end);
3919 /* Now start is the first of the starts, and end is the
3924 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3927 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3931 text_view->just_selected_element = TRUE;
3940 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3942 GtkTextView *text_view;
3944 text_view = GTK_TEXT_VIEW (widget);
3946 if (event->window != text_view->text_window->bin_window)
3949 if (event->button == 1)
3951 if (text_view->drag_start_x >= 0)
3953 text_view->drag_start_x = -1;
3954 text_view->drag_start_y = -1;
3957 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3959 else if (text_view->just_selected_element)
3961 text_view->just_selected_element = FALSE;
3964 else if (text_view->pending_place_cursor_button == event->button)
3968 /* Unselect everything; we clicked inside selection, but
3969 * didn't move by the drag threshold, so just clear selection
3972 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3974 event->x + text_view->xoffset,
3975 event->y + text_view->yoffset);
3977 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3979 text_view->pending_place_cursor_button = 0;
3989 keymap_direction_changed (GdkKeymap *keymap,
3990 GtkTextView *text_view)
3992 gtk_text_view_check_keymap_direction (text_view);
3996 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3998 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4000 gtk_widget_queue_draw (widget);
4002 DV(g_print (G_STRLOC": focus_in_event\n"));
4004 if (text_view->cursor_visible && text_view->layout)
4006 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4007 gtk_text_view_check_cursor_blink (text_view);
4010 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4011 "direction_changed",
4012 G_CALLBACK (keymap_direction_changed), text_view);
4013 gtk_text_view_check_keymap_direction (text_view);
4015 text_view->need_im_reset = TRUE;
4016 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4022 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4024 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4026 gtk_widget_queue_draw (widget);
4028 DV(g_print (G_STRLOC": focus_out_event\n"));
4030 if (text_view->cursor_visible && text_view->layout)
4032 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4033 gtk_text_view_check_cursor_blink (text_view);
4036 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4037 keymap_direction_changed,
4040 text_view->need_im_reset = TRUE;
4041 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4047 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4049 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4051 if (text_view->mouse_cursor_obscured)
4055 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4057 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4058 gdk_cursor_unref (cursor);
4059 text_view->mouse_cursor_obscured = FALSE;
4062 if (event->window == text_view->text_window->bin_window &&
4063 text_view->drag_start_x >= 0)
4067 gdk_window_get_pointer (text_view->text_window->bin_window,
4070 if (gtk_drag_check_threshold (widget,
4071 text_view->drag_start_x,
4072 text_view->drag_start_y,
4076 gint buffer_x, buffer_y;
4078 gtk_text_view_window_to_buffer_coords (text_view,
4079 GTK_TEXT_WINDOW_TEXT,
4080 text_view->drag_start_x,
4081 text_view->drag_start_y,
4085 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4087 buffer_x, buffer_y);
4089 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4098 gtk_text_view_paint (GtkWidget *widget,
4100 GdkEventExpose *event)
4102 GtkTextView *text_view;
4103 GList *child_exposes;
4107 text_view = GTK_TEXT_VIEW (widget);
4109 g_return_if_fail (text_view->layout != NULL);
4110 g_return_if_fail (text_view->xoffset >= 0);
4111 g_return_if_fail (text_view->yoffset >= 0);
4113 while (text_view->first_validate_idle != 0)
4115 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4116 text_view->first_validate_idle));
4117 gtk_text_view_flush_first_validate (text_view);
4120 /* More regions could have become invalid in the above loop */
4121 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4126 gdk_region_get_clipbox (updates, &rect);
4128 gdk_rectangle_union (area, &rect, area);
4130 gdk_region_destroy (updates);
4133 if (!text_view->onscreen_validated)
4135 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.");
4136 g_assert_not_reached ();
4140 printf ("painting %d,%d %d x %d\n",
4142 area->width, area->height);
4145 child_exposes = NULL;
4146 gtk_text_layout_draw (text_view->layout,
4148 text_view->text_window->bin_window,
4153 area->width, area->height,
4156 tmp_list = child_exposes;
4157 while (tmp_list != NULL)
4159 GtkWidget *child = tmp_list->data;
4161 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4165 g_object_unref (child);
4167 tmp_list = tmp_list->next;
4170 g_list_free (child_exposes);
4174 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4178 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4179 GTK_TEXT_WINDOW_TEXT))
4181 DV(g_print (">Exposed ("G_STRLOC")\n"));
4182 gtk_text_view_paint (widget, &event->area, event);
4185 if (event->window == widget->window)
4186 gtk_text_view_draw_focus (widget);
4188 /* Propagate exposes to all children not in the buffer. */
4189 tmp_list = GTK_TEXT_VIEW (widget)->children;
4190 while (tmp_list != NULL)
4192 GtkTextViewChild *vc = tmp_list->data;
4194 /* propagate_expose checks that event->window matches
4197 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4198 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4202 tmp_list = tmp_list->next;
4209 gtk_text_view_draw_focus (GtkWidget *widget)
4211 gboolean interior_focus;
4213 /* We clear the focus if we are in interior focus mode. */
4214 gtk_widget_style_get (widget,
4215 "interior_focus", &interior_focus,
4218 if (GTK_WIDGET_DRAWABLE (widget))
4220 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4222 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4223 NULL, widget, "textview",
4225 widget->allocation.width,
4226 widget->allocation.height);
4230 gdk_window_clear (widget->window);
4236 gtk_text_view_grab_focus (GtkWidget *widget)
4238 GtkTextView *text_view;
4240 text_view = GTK_TEXT_VIEW (widget);
4242 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4244 if (!text_view->disable_scroll_on_focus)
4245 gtk_text_view_scroll_mark_onscreen (text_view,
4246 gtk_text_buffer_get_mark (get_buffer (text_view),
4251 gtk_text_view_focus (GtkWidget *widget,
4252 GtkDirectionType direction)
4254 GtkTextView *text_view;
4255 GtkContainer *container;
4258 text_view = GTK_TEXT_VIEW (widget);
4259 container = GTK_CONTAINER (widget);
4261 if (!gtk_widget_is_focus (widget) &&
4262 container->focus_child == NULL)
4264 gtk_widget_grab_focus (widget);
4270 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4271 * children to get the focus
4273 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4274 result = GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4275 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4286 gtk_text_view_add (GtkContainer *container,
4289 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4290 g_return_if_fail (GTK_IS_WIDGET (child));
4292 /* This is pretty random. */
4293 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4295 GTK_TEXT_WINDOW_WIDGET,
4300 gtk_text_view_remove (GtkContainer *container,
4304 GtkTextView *text_view;
4305 GtkTextViewChild *vc;
4307 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4308 g_return_if_fail (GTK_IS_WIDGET (child));
4309 g_return_if_fail (child->parent == (GtkWidget*) container);
4311 text_view = GTK_TEXT_VIEW (container);
4314 iter = text_view->children;
4316 while (iter != NULL)
4320 if (vc->widget == child)
4323 iter = g_slist_next (iter);
4326 g_assert (iter != NULL); /* be sure we had the child in the list */
4328 text_view->children = g_slist_remove (text_view->children, vc);
4330 gtk_widget_unparent (vc->widget);
4332 text_view_child_free (vc);
4336 gtk_text_view_forall (GtkContainer *container,
4337 gboolean include_internals,
4338 GtkCallback callback,
4339 gpointer callback_data)
4342 GtkTextView *text_view;
4345 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4346 g_return_if_fail (callback != NULL);
4348 text_view = GTK_TEXT_VIEW (container);
4350 copy = g_slist_copy (text_view->children);
4353 while (iter != NULL)
4355 GtkTextViewChild *vc = iter->data;
4357 (* callback) (vc->widget, callback_data);
4359 iter = g_slist_next (iter);
4362 g_slist_free (copy);
4365 #define CURSOR_ON_MULTIPLIER 0.66
4366 #define CURSOR_OFF_MULTIPLIER 0.34
4367 #define CURSOR_PEND_MULTIPLIER 1.0
4370 cursor_blinks (GtkTextView *text_view)
4372 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4375 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4378 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4381 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4386 get_cursor_time (GtkTextView *text_view)
4388 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4391 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4401 blink_cb (gpointer data)
4403 GtkTextView *text_view;
4406 GDK_THREADS_ENTER ();
4408 text_view = GTK_TEXT_VIEW (data);
4410 g_assert (text_view->layout);
4411 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4412 g_assert (text_view->cursor_visible);
4414 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4417 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4421 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4425 gtk_text_layout_set_cursor_visible (text_view->layout,
4428 GDK_THREADS_LEAVE ();
4430 /* Remove ourselves */
4436 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4438 if (text_view->blink_timeout)
4440 gtk_timeout_remove (text_view->blink_timeout);
4441 text_view->blink_timeout = 0;
4446 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4448 if (text_view->layout != NULL &&
4449 text_view->cursor_visible &&
4450 GTK_WIDGET_HAS_FOCUS (text_view))
4452 if (cursor_blinks (text_view))
4454 if (text_view->blink_timeout == 0)
4456 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4458 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4464 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4468 gtk_text_view_stop_cursor_blink (text_view);
4473 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4475 if (text_view->layout != NULL &&
4476 text_view->cursor_visible &&
4477 GTK_WIDGET_HAS_FOCUS (text_view) &&
4478 cursor_blinks (text_view))
4480 if (text_view->blink_timeout != 0)
4482 gtk_timeout_remove (text_view->blink_timeout);
4483 text_view->blink_timeout = 0;
4486 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4488 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4496 * Key binding handlers
4500 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4501 GtkTextIter *newplace,
4506 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4512 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4518 move_cursor (GtkTextView *text_view,
4519 const GtkTextIter *new_location,
4520 gboolean extend_selection)
4522 if (extend_selection)
4523 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4527 gtk_text_buffer_place_cursor (get_buffer (text_view),
4531 /* FIXME when we are unfrozen and can change GtkMovementStep,
4534 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4537 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4538 GtkMovementStep step,
4540 gboolean extend_selection)
4543 GtkTextIter newplace;
4545 gint cursor_x_pos = 0;
4547 gtk_text_view_reset_im_context (text_view);
4549 if (step == GTK_MOVEMENT_PAGES)
4551 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4552 gtk_text_view_pend_cursor_blink (text_view);
4555 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4557 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4558 gtk_text_view_pend_cursor_blink (text_view);
4562 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4563 gtk_text_buffer_get_mark (get_buffer (text_view),
4567 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4568 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4572 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4573 gtk_text_iter_forward_cursor_positions (&newplace, count);
4576 case GTK_MOVEMENT_VISUAL_POSITIONS:
4577 gtk_text_layout_move_iter_visually (text_view->layout,
4581 case GTK_MOVEMENT_WORDS:
4583 gtk_text_iter_backward_word_starts (&newplace, -count);
4585 gtk_text_iter_forward_word_ends (&newplace, count);
4588 case GTK_MOVEMENT_DISPLAY_LINES:
4589 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4590 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4593 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4595 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4596 else if (count < -1)
4597 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4600 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4603 case GTK_MOVEMENT_PARAGRAPHS:
4606 if (!gtk_text_iter_ends_line (&newplace))
4608 gtk_text_iter_forward_to_line_end (&newplace);
4614 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4616 gtk_text_iter_set_line_offset (&newplace, 0);
4623 gtk_text_iter_forward_lines (&newplace, count);
4624 gtk_text_iter_set_line_offset (&newplace, 0);
4628 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4631 if (!gtk_text_iter_ends_line (&newplace))
4632 gtk_text_iter_forward_to_line_end (&newplace);
4636 gtk_text_iter_set_line_offset (&newplace, 0);
4640 case GTK_MOVEMENT_BUFFER_ENDS:
4642 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4644 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4651 /* call move_cursor() even if the cursor hasn't moved, since it
4652 cancels the selection
4654 move_cursor (text_view, &newplace, extend_selection);
4656 if (!gtk_text_iter_equal (&insert, &newplace))
4658 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4659 gtk_text_view_scroll_mark_onscreen (text_view,
4660 gtk_text_buffer_get_mark (get_buffer (text_view),
4663 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4665 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4669 gtk_text_view_pend_cursor_blink (text_view);
4673 gtk_text_view_move_cursor (GtkTextView *text_view,
4674 GtkMovementStep step,
4676 gboolean extend_selection)
4678 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4682 gtk_text_view_page_horizontally (GtkTextView *text_view,
4684 gboolean extend_selection)
4686 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4687 count, extend_selection);
4691 gtk_text_view_set_anchor (GtkTextView *text_view)
4695 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4696 gtk_text_buffer_get_mark (get_buffer (text_view),
4699 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4703 gtk_text_view_scroll_pages (GtkTextView *text_view,
4705 gboolean extend_selection)
4710 gint cursor_x_pos, cursor_y_pos;
4711 GtkTextIter new_insert;
4715 g_return_if_fail (text_view->vadjustment != NULL);
4717 cancel_pending_scroll (text_view);
4719 adj = text_view->vadjustment;
4721 /* Validate the region that will be brought into view by the cursor motion
4725 gtk_text_view_get_first_para_iter (text_view, &anchor);
4726 y0 = adj->page_size;
4727 y1 = adj->page_size + count * adj->page_increment;
4731 gtk_text_view_get_first_para_iter (text_view, &anchor);
4732 y0 = count * adj->page_increment + adj->page_size;
4736 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4737 /* FIXME do we need to update the adjustment ranges here? */
4739 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4741 /* already at top, just be sure we are at offset 0 */
4742 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4743 move_cursor (text_view, &new_insert, extend_selection);
4745 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4747 /* already at bottom, just be sure we are at the end */
4748 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4749 move_cursor (text_view, &new_insert, extend_selection);
4753 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4755 newval = adj->value;
4756 oldval = adj->value;
4758 newval += count * adj->page_increment;
4760 set_adjustment_clamped (adj, newval);
4761 cursor_y_pos += adj->value - oldval;
4763 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4764 clamp_iter_onscreen (text_view, &new_insert);
4765 move_cursor (text_view, &new_insert, extend_selection);
4767 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4770 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4771 * only guarantees 1 pixel onscreen.
4773 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4774 gtk_text_view_scroll_mark_onscreen (text_view,
4775 gtk_text_buffer_get_mark (get_buffer (text_view),
4780 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4782 gboolean extend_selection)
4787 gint cursor_x_pos, cursor_y_pos;
4788 GtkTextIter new_insert;
4791 g_return_if_fail (text_view->hadjustment != NULL);
4793 cancel_pending_scroll (text_view);
4795 adj = text_view->hadjustment;
4797 /* Validate the line that we're moving within.
4799 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4801 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4802 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4803 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4804 /* FIXME do we need to update the adjustment ranges here? */
4806 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4808 /* already at far left, just be sure we are at offset 0 */
4809 gtk_text_iter_set_line_offset (&new_insert, 0);
4810 move_cursor (text_view, &new_insert, extend_selection);
4812 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4814 /* already at far right, just be sure we are at the end */
4815 gtk_text_iter_forward_to_line_end (&new_insert);
4816 move_cursor (text_view, &new_insert, extend_selection);
4820 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4822 newval = adj->value;
4823 oldval = adj->value;
4825 newval += count * adj->page_increment;
4827 set_adjustment_clamped (adj, newval);
4828 cursor_x_pos += adj->value - oldval;
4830 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4831 clamp_iter_onscreen (text_view, &new_insert);
4832 move_cursor (text_view, &new_insert, extend_selection);
4834 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4837 /* FIXME for lines shorter than the overall widget width, this results in a
4838 * "bounce" effect as we scroll to the right of the widget, then scroll
4839 * back to get the end of the line onscreen.
4840 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4843 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4844 * only guarantees 1 pixel onscreen.
4846 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4847 gtk_text_view_scroll_mark_onscreen (text_view,
4848 gtk_text_buffer_get_mark (get_buffer (text_view),
4853 whitespace (gunichar ch, gpointer user_data)
4855 return (ch == ' ' || ch == '\t');
4859 not_whitespace (gunichar ch, gpointer user_data)
4861 return !whitespace (ch, user_data);
4865 find_whitepace_region (const GtkTextIter *center,
4866 GtkTextIter *start, GtkTextIter *end)
4871 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4872 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4873 if (whitespace (gtk_text_iter_get_char (end), NULL))
4874 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4876 return !gtk_text_iter_equal (start, end);
4880 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4883 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4884 text_view->editable);
4888 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4895 gboolean leave_one = FALSE;
4897 gtk_text_view_reset_im_context (text_view);
4899 if (type == GTK_DELETE_CHARS)
4901 /* Char delete deletes the selection, if one exists */
4902 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4903 text_view->editable))
4907 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4909 gtk_text_buffer_get_mark (get_buffer (text_view),
4917 case GTK_DELETE_CHARS:
4918 gtk_text_iter_forward_cursor_positions (&end, count);
4921 case GTK_DELETE_WORD_ENDS:
4923 gtk_text_iter_forward_word_ends (&end, count);
4925 gtk_text_iter_backward_word_starts (&start, 0 - count);
4928 case GTK_DELETE_WORDS:
4931 case GTK_DELETE_DISPLAY_LINE_ENDS:
4934 case GTK_DELETE_DISPLAY_LINES:
4937 case GTK_DELETE_PARAGRAPH_ENDS:
4938 /* If we're already at a newline, we need to
4939 * simply delete that newline, instead of
4940 * moving to the next one.
4942 if (gtk_text_iter_ends_line (&end))
4944 gtk_text_iter_forward_line (&end);
4950 if (!gtk_text_iter_forward_to_line_end (&end))
4956 /* FIXME figure out what a negative count means
4960 case GTK_DELETE_PARAGRAPHS:
4963 gtk_text_iter_set_line_offset (&start, 0);
4964 gtk_text_iter_forward_to_line_end (&end);
4966 /* Do the lines beyond the first. */
4969 gtk_text_iter_forward_to_line_end (&end);
4975 /* FIXME negative count? */
4979 case GTK_DELETE_WHITESPACE:
4981 find_whitepace_region (&insert, &start, &end);
4989 if (!gtk_text_iter_equal (&start, &end))
4991 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4993 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4994 text_view->editable))
4997 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4999 text_view->editable);
5002 gtk_text_buffer_end_user_action (get_buffer (text_view));
5004 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5005 gtk_text_view_scroll_mark_onscreen (text_view,
5006 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5011 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5013 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5014 GDK_SELECTION_CLIPBOARD);
5016 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5018 text_view->editable);
5019 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5020 gtk_text_view_scroll_mark_onscreen (text_view,
5021 gtk_text_buffer_get_mark (get_buffer (text_view),
5026 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5028 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5029 GDK_SELECTION_CLIPBOARD);
5031 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5033 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5034 gtk_text_view_scroll_mark_onscreen (text_view,
5035 gtk_text_buffer_get_mark (get_buffer (text_view),
5040 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5042 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5043 GDK_SELECTION_CLIPBOARD);
5045 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5048 text_view->editable);
5049 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5050 gtk_text_view_scroll_mark_onscreen (text_view,
5051 gtk_text_buffer_get_mark (get_buffer (text_view),
5056 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5058 text_view->overwrite_mode = !text_view->overwrite_mode;
5062 gtk_text_view_move_focus (GtkTextView *text_view,
5063 GtkDirectionType direction_type)
5065 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5067 if (!GTK_WIDGET_TOPLEVEL (toplevel))
5070 /* Propagate to toplevel */
5071 g_signal_emit_by_name (toplevel, "move_focus", direction_type);
5079 gtk_text_view_unselect (GtkTextView *text_view)
5083 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5085 gtk_text_buffer_get_mark (get_buffer (text_view),
5088 gtk_text_buffer_move_mark (get_buffer (text_view),
5089 gtk_text_buffer_get_mark (get_buffer (text_view),
5095 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5096 const gchar *mark_name)
5099 GdkModifierType state;
5100 GtkTextIter newplace;
5102 /* DV(g_print (G_STRLOC": begin\n")); */
5104 gdk_window_get_pointer (text_view->text_window->bin_window,
5107 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5108 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5110 x + text_view->xoffset,
5111 y + text_view->yoffset);
5115 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5117 /* This may invalidate the layout */
5118 DV(g_print (G_STRLOC": move mark\n"));
5119 gtk_text_buffer_move_mark (get_buffer (text_view),
5123 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5124 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5127 DV (g_print ("first validate idle leaving %s is %d\n",
5128 G_STRLOC, text_view->first_validate_idle));
5132 selection_scan_timeout (gpointer data)
5134 GtkTextView *text_view;
5136 GDK_THREADS_ENTER ();
5138 text_view = GTK_TEXT_VIEW (data);
5140 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5141 move_mark_to_pointer_and_scroll (text_view, "insert");
5143 GDK_THREADS_LEAVE ();
5145 return TRUE; /* remain installed. */
5148 #define DND_SCROLL_MARGIN 0.20
5151 drag_scan_timeout (gpointer data)
5153 GtkTextView *text_view;
5155 GdkModifierType state;
5156 GtkTextIter newplace;
5158 GDK_THREADS_ENTER ();
5160 text_view = GTK_TEXT_VIEW (data);
5162 gdk_window_get_pointer (text_view->text_window->bin_window,
5165 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5167 x + text_view->xoffset,
5168 y + text_view->yoffset);
5170 gtk_text_buffer_move_mark (get_buffer (text_view),
5171 text_view->dnd_mark,
5174 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5175 gtk_text_view_scroll_to_mark (text_view,
5176 text_view->dnd_mark,
5177 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5179 GDK_THREADS_LEAVE ();
5185 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5187 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5188 move_mark_to_pointer_and_scroll (text_view, "insert");
5190 /* If we had to scroll offscreen, insert a timeout to do so
5191 * again. Note that in the timeout, even if the mouse doesn't
5192 * move, due to this scroll xoffset/yoffset will have changed
5193 * and we'll need to scroll again.
5195 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5196 gtk_timeout_remove (text_view->scroll_timeout);
5198 text_view->scroll_timeout =
5199 gtk_timeout_add (50, selection_scan_timeout, text_view);
5205 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5206 const GtkTextIter *iter,
5207 GdkEventButton *button)
5209 GtkTextIter newplace;
5210 GtkTextBuffer *buffer;
5212 g_return_if_fail (text_view->selection_drag_handler == 0);
5214 gtk_grab_add (GTK_WIDGET (text_view));
5216 buffer = get_buffer (text_view);
5220 if (button->state & GDK_SHIFT_MASK)
5222 /* Extend selection */
5223 GtkTextIter start, end;
5225 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5227 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5229 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5232 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5235 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5237 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5240 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5246 /* Replace selection */
5247 gtk_text_buffer_place_cursor (buffer, &newplace);
5250 text_view->selection_drag_handler = g_signal_connect (text_view,
5251 "motion_notify_event",
5252 G_CALLBACK (selection_motion_event_handler),
5256 /* returns whether we were really dragging */
5258 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5260 if (text_view->selection_drag_handler == 0)
5263 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
5264 text_view->selection_drag_handler = 0;
5266 if (text_view->scroll_timeout != 0)
5268 gtk_timeout_remove (text_view->scroll_timeout);
5269 text_view->scroll_timeout = 0;
5272 /* one last update to current position */
5273 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5274 move_mark_to_pointer_and_scroll (text_view, "insert");
5276 gtk_grab_remove (GTK_WIDGET (text_view));
5286 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5287 GtkTextAttributes *values,
5290 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5291 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5294 pango_font_description_free (values->font);
5296 values->font = pango_font_description_copy (style->font_desc);
5300 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5302 if (text_view->layout)
5304 gboolean split_cursor;
5305 GtkTextDirection new_dir;
5306 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5308 g_object_get (G_OBJECT (settings),
5309 "gtk-split-cursor", &split_cursor,
5313 new_dir = GTK_TEXT_DIR_NONE;
5317 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5318 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5319 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5322 if (text_view->layout->cursor_direction != new_dir)
5323 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5328 gtk_text_view_ensure_layout (GtkTextView *text_view)
5332 widget = GTK_WIDGET (text_view);
5334 if (text_view->layout == NULL)
5336 GtkTextAttributes *style;
5337 PangoContext *ltr_context, *rtl_context;
5340 DV(g_print(G_STRLOC"\n"));
5342 text_view->layout = gtk_text_layout_new ();
5344 g_signal_connect (text_view->layout,
5346 G_CALLBACK (invalidated_handler),
5349 g_signal_connect (text_view->layout,
5351 G_CALLBACK (changed_handler),
5354 g_signal_connect (text_view->layout,
5356 G_CALLBACK (gtk_text_view_child_allocated),
5359 if (get_buffer (text_view))
5360 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5362 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5363 gtk_text_view_pend_cursor_blink (text_view);
5365 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5367 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5368 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5369 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5370 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5372 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5374 g_object_unref (ltr_context);
5375 g_object_unref (rtl_context);
5377 gtk_text_view_check_keymap_direction (text_view);
5379 style = gtk_text_attributes_new ();
5381 gtk_widget_ensure_style (widget);
5382 gtk_text_view_set_attributes_from_style (text_view,
5383 style, widget->style);
5385 style->pixels_above_lines = text_view->pixels_above_lines;
5386 style->pixels_below_lines = text_view->pixels_below_lines;
5387 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5388 style->left_margin = text_view->left_margin;
5389 style->right_margin = text_view->right_margin;
5390 style->indent = text_view->indent;
5391 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5393 style->wrap_mode = text_view->wrap_mode;
5394 style->justification = text_view->justify;
5395 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5397 gtk_text_layout_set_default_style (text_view->layout, style);
5399 gtk_text_attributes_unref (style);
5401 /* Set layout for all anchored children */
5403 tmp_list = text_view->children;
5404 while (tmp_list != NULL)
5406 GtkTextViewChild *vc = tmp_list->data;
5410 gtk_text_anchored_child_set_layout (vc->widget,
5412 /* vc may now be invalid! */
5415 tmp_list = g_slist_next (tmp_list);
5418 gtk_text_view_invalidate (text_view);
5423 * gtk_text_view_get_default_attributes:
5424 * @text_view: a #GtkTextView
5426 * Obtains a copy of the default text attributes. These are the
5427 * attributes used for text unless a tag overrides them.
5428 * You'd typically pass the default attributes in to
5429 * gtk_text_iter_get_attributes() in order to get the
5430 * attributes in effect at a given text position.
5432 * The return value is a copy owned by the caller of this function,
5433 * and should be freed.
5435 * Return value: a new #GtkTextAttributes
5438 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5440 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5442 gtk_text_view_ensure_layout (text_view);
5444 return gtk_text_attributes_copy (text_view->layout->default_style);
5448 gtk_text_view_destroy_layout (GtkTextView *text_view)
5450 if (text_view->layout)
5454 gtk_text_view_remove_validate_idles (text_view);
5456 g_signal_handlers_disconnect_by_func (text_view->layout,
5457 invalidated_handler,
5459 g_signal_handlers_disconnect_by_func (text_view->layout,
5463 /* Remove layout from all anchored children */
5464 tmp_list = text_view->children;
5465 while (tmp_list != NULL)
5467 GtkTextViewChild *vc = tmp_list->data;
5471 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5472 /* vc may now be invalid! */
5475 tmp_list = g_slist_next (tmp_list);
5478 gtk_text_view_stop_cursor_blink (text_view);
5479 gtk_text_view_end_selection_drag (text_view, NULL);
5481 g_object_unref (text_view->layout);
5482 text_view->layout = NULL;
5487 gtk_text_view_reset_im_context (GtkTextView *text_view)
5489 if (text_view->need_im_reset)
5491 text_view->need_im_reset = FALSE;
5492 gtk_im_context_reset (text_view->im_context);
5501 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5502 const GtkTextIter *iter,
5503 GdkEventMotion *event)
5505 GdkDragContext *context;
5506 GtkTargetList *target_list;
5508 text_view->drag_start_x = -1;
5509 text_view->drag_start_y = -1;
5510 text_view->pending_place_cursor_button = 0;
5512 target_list = gtk_target_list_new (target_table,
5513 G_N_ELEMENTS (target_table));
5515 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5516 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5517 1, (GdkEvent*)event);
5519 gtk_target_list_unref (target_list);
5521 gtk_drag_set_icon_default (context);
5525 gtk_text_view_drag_begin (GtkWidget *widget,
5526 GdkDragContext *context)
5532 gtk_text_view_drag_end (GtkWidget *widget,
5533 GdkDragContext *context)
5535 GtkTextView *text_view;
5537 text_view = GTK_TEXT_VIEW (widget);
5541 gtk_text_view_drag_data_get (GtkWidget *widget,
5542 GdkDragContext *context,
5543 GtkSelectionData *selection_data,
5547 GtkTextView *text_view;
5549 text_view = GTK_TEXT_VIEW (widget);
5551 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5553 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5555 gtk_selection_data_set (selection_data,
5556 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5569 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5572 /* Extract the selected text */
5573 str = gtk_text_iter_get_visible_text (&start, &end);
5578 gtk_selection_data_set_text (selection_data, str, -1);
5585 gtk_text_view_drag_data_delete (GtkWidget *widget,
5586 GdkDragContext *context)
5588 GtkTextView *text_view;
5590 text_view = GTK_TEXT_VIEW (widget);
5592 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5593 TRUE, GTK_TEXT_VIEW (widget)->editable);
5597 gtk_text_view_drag_leave (GtkWidget *widget,
5598 GdkDragContext *context,
5601 GtkTextView *text_view;
5603 text_view = GTK_TEXT_VIEW (widget);
5605 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5607 if (text_view->scroll_timeout != 0)
5608 gtk_timeout_remove (text_view->scroll_timeout);
5610 text_view->scroll_timeout = 0;
5614 gtk_text_view_drag_motion (GtkWidget *widget,
5615 GdkDragContext *context,
5620 GtkTextIter newplace;
5621 GtkTextView *text_view;
5624 GdkRectangle target_rect;
5626 GdkDragAction suggested_action = 0;
5628 text_view = GTK_TEXT_VIEW (widget);
5630 target_rect = text_view->text_window->allocation;
5632 if (x < target_rect.x ||
5633 y < target_rect.y ||
5634 x > (target_rect.x + target_rect.width) ||
5635 y > (target_rect.y + target_rect.height))
5636 return FALSE; /* outside the text window, allow parent widgets to handle event */
5638 gtk_text_view_window_to_buffer_coords (text_view,
5639 GTK_TEXT_WINDOW_WIDGET,
5643 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5647 if (gtk_drag_dest_find_target (widget, context,
5648 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5650 /* can't accept any of the offered targets */
5652 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5654 gtk_text_iter_compare (&newplace, &start) >= 0 &&
5655 gtk_text_iter_compare (&newplace, &end) <= 0)
5657 /* We're inside the selection. */
5661 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5663 GtkWidget *source_widget;
5665 suggested_action = context->suggested_action;
5667 source_widget = gtk_drag_get_source_widget (context);
5669 if (source_widget == widget)
5671 /* Default to MOVE, unless the user has
5672 * pressed ctrl or alt to affect available actions
5674 if ((context->actions & GDK_ACTION_MOVE) != 0)
5675 suggested_action = GDK_ACTION_MOVE;
5680 /* Can't drop here. */
5684 if (suggested_action != 0)
5686 gtk_text_mark_set_visible (text_view->dnd_mark,
5687 text_view->cursor_visible);
5689 gdk_drag_status (context, suggested_action, time);
5693 gdk_drag_status (context, 0, time);
5694 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5697 gtk_text_buffer_move_mark (get_buffer (text_view),
5698 text_view->dnd_mark,
5701 DV(g_print (G_STRLOC": scrolling to mark\n"));
5702 gtk_text_view_scroll_to_mark (text_view,
5703 text_view->dnd_mark,
5704 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5706 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5707 gtk_timeout_remove (text_view->scroll_timeout);
5709 text_view->scroll_timeout =
5710 gtk_timeout_add (50, drag_scan_timeout, text_view);
5712 /* TRUE return means don't propagate the drag motion to parent
5713 * widgets that may also be drop sites.
5719 gtk_text_view_drag_drop (GtkWidget *widget,
5720 GdkDragContext *context,
5725 GtkTextView *text_view;
5726 GtkTextIter drop_point;
5727 GdkAtom target = GDK_NONE;
5729 text_view = GTK_TEXT_VIEW (widget);
5731 if (text_view->scroll_timeout != 0)
5732 gtk_timeout_remove (text_view->scroll_timeout);
5734 text_view->scroll_timeout = 0;
5736 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5738 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5740 text_view->dnd_mark);
5742 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5743 target = gtk_drag_dest_find_target (widget, context, NULL);
5745 if (target != GDK_NONE)
5746 gtk_drag_get_data (widget, context, target, time);
5748 gtk_drag_finish (context, FALSE, FALSE, time);
5754 insert_text_data (GtkTextView *text_view,
5755 GtkTextIter *drop_point,
5756 GtkSelectionData *selection_data)
5760 str = gtk_selection_data_get_text (selection_data);
5764 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5765 drop_point, str, -1,
5766 text_view->editable);
5772 gtk_text_view_drag_data_received (GtkWidget *widget,
5773 GdkDragContext *context,
5776 GtkSelectionData *selection_data,
5780 GtkTextIter drop_point;
5781 GtkTextView *text_view;
5782 gboolean success = FALSE;
5784 text_view = GTK_TEXT_VIEW (widget);
5786 if (!text_view->dnd_mark)
5789 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5791 text_view->dnd_mark);
5793 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5796 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5798 GtkTextBuffer *src_buffer = NULL;
5799 GtkTextIter start, end;
5800 gboolean copy_tags = TRUE;
5802 if (selection_data->length != sizeof (src_buffer))
5805 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5807 if (src_buffer == NULL)
5810 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5812 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5813 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5816 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5821 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5825 text_view->editable);
5830 str = gtk_text_iter_get_visible_text (&start, &end);
5831 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5832 &drop_point, str, -1,
5833 text_view->editable);
5839 insert_text_data (text_view, &drop_point, selection_data);
5841 gtk_text_buffer_place_cursor (get_buffer (text_view), &drop_point);
5846 gtk_drag_finish (context, success,
5847 success && context->action == GDK_ACTION_MOVE,
5851 static GtkAdjustment*
5852 get_hadjustment (GtkTextView *text_view)
5854 if (text_view->hadjustment == NULL)
5855 gtk_text_view_set_scroll_adjustments (text_view,
5856 NULL, /* forces creation */
5857 text_view->vadjustment);
5859 return text_view->hadjustment;
5862 static GtkAdjustment*
5863 get_vadjustment (GtkTextView *text_view)
5865 if (text_view->vadjustment == NULL)
5866 gtk_text_view_set_scroll_adjustments (text_view,
5867 text_view->hadjustment,
5868 NULL); /* forces creation */
5869 return text_view->vadjustment;
5874 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5875 GtkAdjustment *hadj,
5876 GtkAdjustment *vadj)
5878 gboolean need_adjust = FALSE;
5880 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5883 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5885 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5887 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5889 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5891 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5893 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
5894 gtk_text_view_value_changed,
5896 g_object_unref (text_view->hadjustment);
5899 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5901 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
5902 gtk_text_view_value_changed,
5904 g_object_unref (text_view->vadjustment);
5907 if (text_view->hadjustment != hadj)
5909 text_view->hadjustment = hadj;
5910 g_object_ref (text_view->hadjustment);
5911 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5913 g_signal_connect (text_view->hadjustment, "value_changed",
5914 G_CALLBACK (gtk_text_view_value_changed),
5919 if (text_view->vadjustment != vadj)
5921 text_view->vadjustment = vadj;
5922 g_object_ref (text_view->vadjustment);
5923 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5925 g_signal_connect (text_view->vadjustment, "value_changed",
5926 G_CALLBACK (gtk_text_view_value_changed),
5932 gtk_text_view_value_changed (NULL, text_view);
5935 /* FIXME this adjust_allocation is a big cut-and-paste from
5936 * GtkCList, needs to be some "official" way to do this
5946 /* The window to which widget->window is relative */
5947 #define ALLOCATION_WINDOW(widget) \
5948 (GTK_WIDGET_NO_WINDOW (widget) ? \
5949 (widget)->window : \
5950 gdk_window_get_parent ((widget)->window))
5953 adjust_allocation_recurse (GtkWidget *widget,
5956 ScrollData *scroll_data = data;
5958 /* Need to really size allocate instead of just poking
5959 * into widget->allocation if the widget is not realized.
5960 * FIXME someone figure out why this was.
5962 if (!GTK_WIDGET_REALIZED (widget))
5964 if (GTK_WIDGET_VISIBLE (widget))
5966 GdkRectangle tmp_rectangle = widget->allocation;
5967 tmp_rectangle.x += scroll_data->dx;
5968 tmp_rectangle.y += scroll_data->dy;
5970 gtk_widget_size_allocate (widget, &tmp_rectangle);
5975 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5977 widget->allocation.x += scroll_data->dx;
5978 widget->allocation.y += scroll_data->dy;
5980 if (GTK_IS_CONTAINER (widget))
5981 gtk_container_forall (GTK_CONTAINER (widget),
5982 adjust_allocation_recurse,
5989 adjust_allocation (GtkWidget *widget,
5993 ScrollData scroll_data;
5995 if (GTK_WIDGET_REALIZED (widget))
5996 scroll_data.window = ALLOCATION_WINDOW (widget);
5998 scroll_data.window = NULL;
6000 scroll_data.dx = dx;
6001 scroll_data.dy = dy;
6003 adjust_allocation_recurse (widget, &scroll_data);
6007 gtk_text_view_value_changed (GtkAdjustment *adj,
6008 GtkTextView *text_view)
6015 /* Note that we oddly call this function with adj == NULL
6019 text_view->onscreen_validated = FALSE;
6021 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
6022 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
6023 adj ? adj->value : 0.0));
6025 if (adj == text_view->hadjustment)
6027 dx = text_view->xoffset - (gint)adj->value;
6028 text_view->xoffset = adj->value;
6030 else if (adj == text_view->vadjustment)
6032 dy = text_view->yoffset - (gint)adj->value;
6033 text_view->yoffset = adj->value;
6035 if (text_view->layout)
6037 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
6039 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
6041 text_view->first_para_pixels = adj->value - line_top;
6045 if (dx != 0 || dy != 0)
6049 if (GTK_WIDGET_REALIZED (text_view))
6053 if (text_view->left_window)
6054 text_window_scroll (text_view->left_window, 0, dy);
6055 if (text_view->right_window)
6056 text_window_scroll (text_view->right_window, 0, dy);
6061 if (text_view->top_window)
6062 text_window_scroll (text_view->top_window, dx, 0);
6063 if (text_view->bottom_window)
6064 text_window_scroll (text_view->bottom_window, dx, 0);
6067 /* It looks nicer to scroll the main area last, because
6068 * it takes a while, and making the side areas update
6069 * afterward emphasizes the slowness of scrolling the
6072 text_window_scroll (text_view->text_window, dx, dy);
6075 /* Children are now "moved" in the text window, poke
6076 * into widget->allocation for each child
6078 tmp_list = text_view->children;
6079 while (tmp_list != NULL)
6081 GtkTextViewChild *child = tmp_list->data;
6084 adjust_allocation (child->widget, dx, dy);
6086 tmp_list = g_slist_next (tmp_list);
6090 /* This could result in invalidation, which would install the
6091 * first_validate_idle, which would validate onscreen;
6092 * but we're going to go ahead and validate here, so
6093 * first_validate_idle shouldn't have anything to do.
6095 gtk_text_view_update_layout_width (text_view);
6097 /* note that validation of onscreen could invoke this function
6098 * recursively, by scrolling to maintain first_para, or in response
6099 * to updating the layout width, however there is no problem with
6100 * that, or shouldn't be.
6102 gtk_text_view_validate_onscreen (text_view);
6104 /* process exposes */
6105 if (GTK_WIDGET_REALIZED (text_view))
6107 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6109 if (text_view->left_window)
6110 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6112 if (text_view->right_window)
6113 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6115 if (text_view->top_window)
6116 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6118 if (text_view->bottom_window)
6119 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6121 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6124 /* If this got installed, get rid of it, it's just a waste of time. */
6125 if (text_view->first_validate_idle != 0)
6127 g_source_remove (text_view->first_validate_idle);
6128 text_view->first_validate_idle = 0;
6131 gtk_text_view_update_im_spot_location (text_view);
6133 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6137 gtk_text_view_commit_handler (GtkIMContext *context,
6139 GtkTextView *text_view)
6141 gtk_text_view_commit_text (text_view, str);
6145 gtk_text_view_commit_text (GtkTextView *text_view,
6148 gboolean had_selection;
6150 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6152 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6155 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6156 text_view->editable);
6158 if (!strcmp (str, "\n"))
6160 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6161 text_view->editable);
6165 if (!had_selection && text_view->overwrite_mode)
6169 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6171 gtk_text_buffer_get_mark (get_buffer (text_view),
6173 if (!gtk_text_iter_ends_line (&insert))
6174 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6176 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6177 text_view->editable);
6180 gtk_text_buffer_end_user_action (get_buffer (text_view));
6182 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6183 gtk_text_view_scroll_mark_onscreen (text_view,
6184 gtk_text_buffer_get_mark (get_buffer (text_view),
6189 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6190 GtkTextView *text_view)
6193 PangoAttrList *attrs;
6196 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6197 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6198 pango_attr_list_unref (attrs);
6201 gtk_text_view_scroll_mark_onscreen (text_view,
6202 gtk_text_buffer_get_mark (get_buffer (text_view),
6207 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6208 GtkTextView *text_view)
6215 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6216 gtk_text_buffer_get_insert (text_view->buffer));
6219 pos = gtk_text_iter_get_line_index (&start);
6220 gtk_text_iter_set_line_offset (&start, 0);
6221 gtk_text_iter_forward_to_line_end (&end);
6223 text = gtk_text_iter_get_slice (&start, &end);
6224 gtk_im_context_set_surrounding (context, text, -1, pos);
6231 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6234 GtkTextView *text_view)
6239 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6240 gtk_text_buffer_get_insert (text_view->buffer));
6243 gtk_text_iter_forward_chars (&start, offset);
6244 gtk_text_iter_forward_chars (&end, offset + n_chars);
6246 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6252 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6253 const GtkTextIter *location,
6257 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6258 gboolean need_reset = FALSE;
6260 if (mark == gtk_text_buffer_get_insert (buffer))
6262 text_view->virtual_cursor_x = -1;
6263 text_view->virtual_cursor_y = -1;
6264 gtk_text_view_update_im_spot_location (text_view);
6267 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6273 gtk_text_view_reset_im_context (text_view);
6277 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6281 GdkRectangle strong_pos;
6284 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6285 gtk_text_buffer_get_mark (get_buffer (text_view),
6288 if ((x && text_view->virtual_cursor_x == -1) ||
6289 (y && text_view->virtual_cursor_y == -1))
6290 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6294 if (text_view->virtual_cursor_x != -1)
6295 *x = text_view->virtual_cursor_x;
6302 if (text_view->virtual_cursor_x != -1)
6303 *y = text_view->virtual_cursor_y;
6305 *y = strong_pos.y + strong_pos.height / 2;
6310 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6314 GdkRectangle strong_pos;
6317 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6318 gtk_text_buffer_get_mark (get_buffer (text_view),
6321 if (x == -1 || y == -1)
6322 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6324 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6325 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6328 /* Quick hack of a popup menu
6331 activate_cb (GtkWidget *menuitem,
6332 GtkTextView *text_view)
6334 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6335 g_signal_emit_by_name (text_view, signal);
6339 append_action_signal (GtkTextView *text_view,
6341 const gchar *stock_id,
6342 const gchar *signal,
6345 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6347 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6348 g_signal_connect (menuitem, "activate",
6349 G_CALLBACK (activate_cb), text_view);
6351 gtk_widget_set_sensitive (menuitem, sensitive);
6353 gtk_widget_show (menuitem);
6354 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6358 popup_menu_detach (GtkWidget *attach_widget,
6361 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6365 popup_position_func (GtkMenu *menu,
6371 GtkTextView *text_view;
6373 GdkRectangle cursor_rect;
6374 GdkRectangle onscreen_rect;
6375 gint root_x, root_y;
6380 text_view = GTK_TEXT_VIEW (user_data);
6381 widget = GTK_WIDGET (text_view);
6383 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6385 screen = gtk_widget_get_screen (widget);
6387 gdk_window_get_origin (widget->window, &root_x, &root_y);
6389 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6391 gtk_text_buffer_get_insert (get_buffer (text_view)));
6393 gtk_text_view_get_iter_location (text_view,
6397 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6399 gtk_widget_size_request (text_view->popup_menu, &req);
6401 /* can't use rectangle_intersect since cursor rect can have 0 width */
6402 if (cursor_rect.x >= onscreen_rect.x &&
6403 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6404 cursor_rect.y >= onscreen_rect.y &&
6405 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6407 gtk_text_view_buffer_to_window_coords (text_view,
6408 GTK_TEXT_WINDOW_WIDGET,
6409 cursor_rect.x, cursor_rect.y,
6410 &cursor_rect.x, &cursor_rect.y);
6412 *x = root_x + cursor_rect.x + cursor_rect.width;
6413 *y = root_y + cursor_rect.y + cursor_rect.height;
6417 /* Just center the menu, since cursor is offscreen. */
6418 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6419 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6423 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6424 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6426 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6427 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6432 GtkTextView *text_view;
6438 range_contains_editable_text (const GtkTextIter *start,
6439 const GtkTextIter *end,
6440 gboolean default_editability)
6442 GtkTextIter iter = *start;
6444 while (gtk_text_iter_compare (&iter, end) < 0)
6446 if (gtk_text_iter_editable (&iter, default_editability))
6449 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6456 unichar_chosen_func (const char *text,
6459 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6461 gtk_text_view_commit_text (text_view, text);
6465 popup_targets_received (GtkClipboard *clipboard,
6466 GtkSelectionData *data,
6469 PopupInfo *info = user_data;
6470 GtkTextView *text_view = info->text_view;
6472 if (GTK_WIDGET_REALIZED (text_view))
6474 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6475 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6477 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6478 GtkWidget *menuitem;
6480 gboolean have_selection;
6481 gboolean can_insert;
6483 GtkTextIter sel_start, sel_end;
6485 if (text_view->popup_menu)
6486 gtk_widget_destroy (text_view->popup_menu);
6488 text_view->popup_menu = gtk_menu_new ();
6490 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6491 GTK_WIDGET (text_view),
6494 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6495 &sel_start, &sel_end);
6497 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6499 gtk_text_buffer_get_insert (get_buffer (text_view)));
6501 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6503 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6505 range_contains_editable_text (&sel_start, &sel_end,
6506 text_view->editable));
6507 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6509 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6510 can_insert && clipboard_contains_text);
6512 menuitem = gtk_separator_menu_item_new ();
6513 gtk_widget_show (menuitem);
6514 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6516 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6517 gtk_widget_show (menuitem);
6518 submenu = gtk_menu_new ();
6519 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6520 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6522 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6523 GTK_MENU_SHELL (submenu));
6525 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6526 gtk_widget_show (menuitem);
6527 gtk_widget_set_sensitive (menuitem, can_insert);
6529 submenu = gtk_menu_new ();
6530 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6531 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6533 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6534 unichar_chosen_func,
6537 g_signal_emit (text_view,
6538 signals[POPULATE_POPUP],
6540 text_view->popup_menu);
6543 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6545 info->button, info->time);
6548 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6549 popup_position_func, text_view,
6550 0, gtk_get_current_event_time ());
6551 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
6555 g_object_unref (text_view);
6560 gtk_text_view_do_popup (GtkTextView *text_view,
6561 GdkEventButton *event)
6563 PopupInfo *info = g_new (PopupInfo, 1);
6565 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6566 gtk_text_view_end_selection_drag (text_view, event);
6568 /* In order to know what entries we should make sensitive, we
6569 * ask for the current targets of the clipboard, and when
6570 * we get them, then we actually pop up the menu.
6572 info->text_view = g_object_ref (text_view);
6576 info->button = event->button;
6577 info->time = event->time;
6582 info->time = gtk_get_current_event_time ();
6585 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6586 GDK_SELECTION_CLIPBOARD),
6587 gdk_atom_intern ("TARGETS", FALSE),
6588 popup_targets_received,
6593 gtk_text_view_popup_menu (GtkWidget *widget)
6595 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6599 /* Child GdkWindows */
6602 static GtkTextWindow*
6603 text_window_new (GtkTextWindowType type,
6606 gint height_request)
6610 win = g_new (GtkTextWindow, 1);
6613 win->widget = widget;
6615 win->bin_window = NULL;
6616 win->requisition.width = width_request;
6617 win->requisition.height = height_request;
6618 win->allocation.width = width_request;
6619 win->allocation.height = height_request;
6620 win->allocation.x = 0;
6621 win->allocation.y = 0;
6627 text_window_free (GtkTextWindow *win)
6630 text_window_unrealize (win);
6636 text_window_realize (GtkTextWindow *win,
6639 GdkWindowAttr attributes;
6640 gint attributes_mask;
6643 attributes.window_type = GDK_WINDOW_CHILD;
6644 attributes.x = win->allocation.x;
6645 attributes.y = win->allocation.y;
6646 attributes.width = win->allocation.width;
6647 attributes.height = win->allocation.height;
6648 attributes.wclass = GDK_INPUT_OUTPUT;
6649 attributes.visual = gtk_widget_get_visual (win->widget);
6650 attributes.colormap = gtk_widget_get_colormap (win->widget);
6651 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6653 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6655 win->window = gdk_window_new (parent,
6659 gdk_window_show (win->window);
6660 gdk_window_set_user_data (win->window, win->widget);
6664 attributes.width = win->allocation.width;
6665 attributes.height = win->allocation.height;
6666 attributes.event_mask = (GDK_EXPOSURE_MASK |
6668 GDK_KEY_PRESS_MASK |
6669 GDK_BUTTON_PRESS_MASK |
6670 GDK_BUTTON_RELEASE_MASK |
6671 GDK_POINTER_MOTION_MASK |
6672 GDK_POINTER_MOTION_HINT_MASK |
6673 gtk_widget_get_events (win->widget));
6675 win->bin_window = gdk_window_new (win->window,
6679 gdk_window_show (win->bin_window);
6680 gdk_window_set_user_data (win->bin_window, win->widget);
6682 if (win->type == GTK_TEXT_WINDOW_TEXT)
6685 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
6687 gdk_window_set_cursor (win->bin_window, cursor);
6688 gdk_cursor_unref (cursor);
6690 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6694 gdk_window_set_background (win->bin_window,
6695 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6699 gdk_window_set_background (win->bin_window,
6700 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6703 g_object_set_qdata (G_OBJECT (win->window),
6704 g_quark_from_static_string ("gtk-text-view-text-window"),
6707 g_object_set_qdata (G_OBJECT (win->bin_window),
6708 g_quark_from_static_string ("gtk-text-view-text-window"),
6713 text_window_unrealize (GtkTextWindow *win)
6715 if (win->type == GTK_TEXT_WINDOW_TEXT)
6717 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6721 gdk_window_set_user_data (win->window, NULL);
6722 gdk_window_set_user_data (win->bin_window, NULL);
6723 gdk_window_destroy (win->bin_window);
6724 gdk_window_destroy (win->window);
6726 win->bin_window = NULL;
6730 text_window_size_allocate (GtkTextWindow *win,
6733 win->allocation = *rect;
6737 gdk_window_move_resize (win->window,
6739 rect->width, rect->height);
6741 gdk_window_resize (win->bin_window,
6742 rect->width, rect->height);
6747 text_window_scroll (GtkTextWindow *win,
6751 if (dx != 0 || dy != 0)
6753 gdk_window_scroll (win->bin_window, dx, dy);
6758 text_window_invalidate_rect (GtkTextWindow *win,
6761 GdkRectangle window_rect;
6763 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6770 window_rect.width = rect->width;
6771 window_rect.height = rect->height;
6773 /* Adjust the rect as appropriate */
6777 case GTK_TEXT_WINDOW_TEXT:
6780 case GTK_TEXT_WINDOW_LEFT:
6781 case GTK_TEXT_WINDOW_RIGHT:
6783 window_rect.width = win->allocation.width;
6786 case GTK_TEXT_WINDOW_TOP:
6787 case GTK_TEXT_WINDOW_BOTTOM:
6789 window_rect.height = win->allocation.height;
6793 g_warning ("%s: bug!", G_STRLOC);
6798 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6802 GdkColor color = { 0, 65535, 0, 0 };
6803 GdkGC *gc = gdk_gc_new (win->bin_window);
6804 gdk_gc_set_rgb_fg_color (gc, &color);
6805 gdk_draw_rectangle (win->bin_window,
6806 gc, TRUE, window_rect.x, window_rect.y,
6807 window_rect.width, window_rect.height);
6808 g_object_unref (gc);
6814 text_window_get_width (GtkTextWindow *win)
6816 return win->allocation.width;
6820 text_window_get_height (GtkTextWindow *win)
6822 return win->allocation.height;
6829 * gtk_text_view_get_window:
6830 * @text_view: a #GtkTextView
6831 * @win: window to get
6833 * Retrieves the #GdkWindow corresponding to an area of the text view;
6834 * possible windows include the overall widget window, child windows
6835 * on the left, right, top, bottom, and the window that displays the
6836 * text buffer. Windows are %NULL and nonexistent if their width or
6837 * height is 0, and are nonexistent before the widget has been
6840 * Return value: a #GdkWindow, or %NULL
6843 gtk_text_view_get_window (GtkTextView *text_view,
6844 GtkTextWindowType win)
6846 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6850 case GTK_TEXT_WINDOW_WIDGET:
6851 return GTK_WIDGET (text_view)->window;
6854 case GTK_TEXT_WINDOW_TEXT:
6855 return text_view->text_window->bin_window;
6858 case GTK_TEXT_WINDOW_LEFT:
6859 if (text_view->left_window)
6860 return text_view->left_window->bin_window;
6865 case GTK_TEXT_WINDOW_RIGHT:
6866 if (text_view->right_window)
6867 return text_view->right_window->bin_window;
6872 case GTK_TEXT_WINDOW_TOP:
6873 if (text_view->top_window)
6874 return text_view->top_window->bin_window;
6879 case GTK_TEXT_WINDOW_BOTTOM:
6880 if (text_view->bottom_window)
6881 return text_view->bottom_window->bin_window;
6886 case GTK_TEXT_WINDOW_PRIVATE:
6887 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6892 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6897 * gtk_text_view_get_window_type:
6898 * @text_view: a #GtkTextView
6899 * @window: a window type
6901 * Usually used to find out which window an event corresponds to.
6902 * If you connect to an event signal on @text_view, this function
6903 * should be called on <literal>event->window</literal> to
6904 * see which window it was.
6906 * Return value: the window type.
6909 gtk_text_view_get_window_type (GtkTextView *text_view,
6914 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6915 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6917 if (window == GTK_WIDGET (text_view)->window)
6918 return GTK_TEXT_WINDOW_WIDGET;
6920 win = g_object_get_qdata (G_OBJECT (window),
6921 g_quark_try_string ("gtk-text-view-text-window"));
6927 return GTK_TEXT_WINDOW_PRIVATE;
6932 buffer_to_widget (GtkTextView *text_view,
6940 *window_x = buffer_x - text_view->xoffset;
6941 *window_x += text_view->text_window->allocation.x;
6946 *window_y = buffer_y - text_view->yoffset;
6947 *window_y += text_view->text_window->allocation.y;
6952 widget_to_text_window (GtkTextWindow *win,
6959 *window_x = widget_x - win->allocation.x;
6962 *window_y = widget_y - win->allocation.y;
6966 buffer_to_text_window (GtkTextView *text_view,
6975 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6976 "for a nonexistent or private child window of GtkTextView");
6980 buffer_to_widget (text_view,
6982 window_x, window_y);
6984 widget_to_text_window (win,
6985 window_x ? *window_x : 0,
6986 window_y ? *window_y : 0,
6992 * gtk_text_view_buffer_to_window_coords:
6993 * @text_view: a #GtkTextView
6994 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6995 * @buffer_x: buffer x coordinate
6996 * @buffer_y: buffer y coordinate
6997 * @window_x: window x coordinate return location
6998 * @window_y: window y coordinate return location
7000 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
7001 * @win, and stores the result in (@window_x, @window_y).
7003 * Note that you can't convert coordinates for a nonexisting window (see
7004 * gtk_text_view_set_border_window_size()).
7007 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
7008 GtkTextWindowType win,
7014 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7018 case GTK_TEXT_WINDOW_WIDGET:
7019 buffer_to_widget (text_view,
7021 window_x, window_y);
7024 case GTK_TEXT_WINDOW_TEXT:
7026 *window_x = buffer_x - text_view->xoffset;
7028 *window_y = buffer_y - text_view->yoffset;
7031 case GTK_TEXT_WINDOW_LEFT:
7032 buffer_to_text_window (text_view,
7033 text_view->left_window,
7035 window_x, window_y);
7038 case GTK_TEXT_WINDOW_RIGHT:
7039 buffer_to_text_window (text_view,
7040 text_view->right_window,
7042 window_x, window_y);
7045 case GTK_TEXT_WINDOW_TOP:
7046 buffer_to_text_window (text_view,
7047 text_view->top_window,
7049 window_x, window_y);
7052 case GTK_TEXT_WINDOW_BOTTOM:
7053 buffer_to_text_window (text_view,
7054 text_view->bottom_window,
7056 window_x, window_y);
7059 case GTK_TEXT_WINDOW_PRIVATE:
7060 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7064 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7070 widget_to_buffer (GtkTextView *text_view,
7078 *buffer_x = widget_x + text_view->xoffset;
7079 *buffer_x -= text_view->text_window->allocation.x;
7084 *buffer_y = widget_y + text_view->yoffset;
7085 *buffer_y -= text_view->text_window->allocation.y;
7090 text_window_to_widget (GtkTextWindow *win,
7097 *widget_x = window_x + win->allocation.x;
7100 *widget_y = window_y + win->allocation.y;
7104 text_window_to_buffer (GtkTextView *text_view,
7113 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7114 "coordinates for a nonexistent child window.");
7118 text_window_to_widget (win,
7124 widget_to_buffer (text_view,
7125 buffer_x ? *buffer_x : 0,
7126 buffer_y ? *buffer_y : 0,
7132 * gtk_text_view_window_to_buffer_coords:
7133 * @text_view: a #GtkTextView
7134 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7135 * @window_x: window x coordinate
7136 * @window_y: window y coordinate
7137 * @buffer_x: buffer x coordinate return location
7138 * @buffer_y: buffer y coordinate return location
7140 * Converts coordinates on the window identified by @win to buffer
7141 * coordinates, storing the result in (@buffer_x,@buffer_y).
7143 * Note that you can't convert coordinates for a nonexisting window (see
7144 * gtk_text_view_set_border_window_size()).
7147 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7148 GtkTextWindowType win,
7154 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7158 case GTK_TEXT_WINDOW_WIDGET:
7159 widget_to_buffer (text_view,
7161 buffer_x, buffer_y);
7164 case GTK_TEXT_WINDOW_TEXT:
7166 *buffer_x = window_x + text_view->xoffset;
7168 *buffer_y = window_y + text_view->yoffset;
7171 case GTK_TEXT_WINDOW_LEFT:
7172 text_window_to_buffer (text_view,
7173 text_view->left_window,
7175 buffer_x, buffer_y);
7178 case GTK_TEXT_WINDOW_RIGHT:
7179 text_window_to_buffer (text_view,
7180 text_view->right_window,
7182 buffer_x, buffer_y);
7185 case GTK_TEXT_WINDOW_TOP:
7186 text_window_to_buffer (text_view,
7187 text_view->top_window,
7189 buffer_x, buffer_y);
7192 case GTK_TEXT_WINDOW_BOTTOM:
7193 text_window_to_buffer (text_view,
7194 text_view->bottom_window,
7196 buffer_x, buffer_y);
7199 case GTK_TEXT_WINDOW_PRIVATE:
7200 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7204 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7210 set_window_width (GtkTextView *text_view,
7212 GtkTextWindowType type,
7213 GtkTextWindow **winp)
7219 text_window_free (*winp);
7221 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7228 *winp = text_window_new (type,
7229 GTK_WIDGET (text_view),
7231 /* if the widget is already realized we need to realize the child manually */
7232 if (GTK_WIDGET_REALIZED (text_view))
7233 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7237 if ((*winp)->requisition.width == width)
7240 (*winp)->requisition.width = width;
7243 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7249 set_window_height (GtkTextView *text_view,
7251 GtkTextWindowType type,
7252 GtkTextWindow **winp)
7258 text_window_free (*winp);
7260 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7267 *winp = text_window_new (type,
7268 GTK_WIDGET (text_view),
7271 /* if the widget is already realized we need to realize the child manually */
7272 if (GTK_WIDGET_REALIZED (text_view))
7273 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7277 if ((*winp)->requisition.height == height)
7280 (*winp)->requisition.height = height;
7283 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7288 * gtk_text_view_set_border_window_size:
7289 * @text_view: a #GtkTextView
7290 * @type: window to affect
7291 * @size: width or height of the window
7293 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7294 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7295 * Automatically destroys the corresponding window if the size is set
7296 * to 0, and creates the window if the size is set to non-zero. This
7297 * function can only be used for the "border windows," it doesn't work
7298 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7299 * #GTK_TEXT_WINDOW_PRIVATE.
7302 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7303 GtkTextWindowType type,
7307 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7308 g_return_if_fail (size >= 0);
7312 case GTK_TEXT_WINDOW_LEFT:
7313 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7314 &text_view->left_window);
7317 case GTK_TEXT_WINDOW_RIGHT:
7318 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7319 &text_view->right_window);
7322 case GTK_TEXT_WINDOW_TOP:
7323 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7324 &text_view->top_window);
7327 case GTK_TEXT_WINDOW_BOTTOM:
7328 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7329 &text_view->bottom_window);
7333 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7339 * gtk_text_view_get_border_window_size:
7340 * @text_view: a #GtkTextView
7341 * @type: window to return size from
7343 * Gets the width of the specified border window. See
7344 * gtk_text_view_set_border_window_size().
7346 * Return value: width of window
7349 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7350 GtkTextWindowType type)
7352 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7356 case GTK_TEXT_WINDOW_LEFT:
7357 if (text_view->left_window)
7358 return text_view->left_window->requisition.width;
7360 case GTK_TEXT_WINDOW_RIGHT:
7361 if (text_view->right_window)
7362 return text_view->right_window->requisition.width;
7364 case GTK_TEXT_WINDOW_TOP:
7365 if (text_view->top_window)
7366 return text_view->top_window->requisition.height;
7368 case GTK_TEXT_WINDOW_BOTTOM:
7369 if (text_view->bottom_window)
7370 return text_view->bottom_window->requisition.height;
7373 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7384 static GtkTextViewChild*
7385 text_view_child_new_anchored (GtkWidget *child,
7386 GtkTextChildAnchor *anchor,
7387 GtkTextLayout *layout)
7389 GtkTextViewChild *vc;
7391 vc = g_new (GtkTextViewChild, 1);
7393 vc->type = GTK_TEXT_WINDOW_PRIVATE;
7395 vc->anchor = anchor;
7397 vc->from_top_of_line = 0;
7398 vc->from_left_of_buffer = 0;
7400 g_object_ref (vc->widget);
7401 g_object_ref (vc->anchor);
7403 g_object_set_data (G_OBJECT (child),
7404 "gtk-text-view-child",
7407 gtk_text_child_anchor_register_child (anchor, child, layout);
7412 static GtkTextViewChild*
7413 text_view_child_new_window (GtkWidget *child,
7414 GtkTextWindowType type,
7418 GtkTextViewChild *vc;
7420 vc = g_new (GtkTextViewChild, 1);
7425 vc->from_top_of_line = 0;
7426 vc->from_left_of_buffer = 0;
7428 g_object_ref (vc->widget);
7434 g_object_set_data (G_OBJECT (child),
7435 "gtk-text-view-child",
7442 text_view_child_free (GtkTextViewChild *child)
7444 g_object_set_data (G_OBJECT (child->widget),
7445 "gtk-text-view-child", NULL);
7449 gtk_text_child_anchor_unregister_child (child->anchor,
7451 g_object_unref (child->anchor);
7454 g_object_unref (child->widget);
7460 text_view_child_set_parent_window (GtkTextView *text_view,
7461 GtkTextViewChild *vc)
7464 gtk_widget_set_parent_window (vc->widget,
7465 text_view->text_window->bin_window);
7469 window = gtk_text_view_get_window (text_view,
7471 gtk_widget_set_parent_window (vc->widget, window);
7476 add_child (GtkTextView *text_view,
7477 GtkTextViewChild *vc)
7479 text_view->children = g_slist_prepend (text_view->children,
7482 if (GTK_WIDGET_REALIZED (text_view))
7483 text_view_child_set_parent_window (text_view, vc);
7485 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7489 * gtk_text_view_add_child_at_anchor:
7490 * @text_view: a #GtkTextView
7491 * @child: a #GtkWidget
7492 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7494 * Adds a child widget in the text buffer, at the given @anchor.
7498 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7500 GtkTextChildAnchor *anchor)
7502 GtkTextViewChild *vc;
7504 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7505 g_return_if_fail (GTK_IS_WIDGET (child));
7506 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7507 g_return_if_fail (child->parent == NULL);
7509 gtk_text_view_ensure_layout (text_view);
7511 vc = text_view_child_new_anchored (child, anchor,
7514 add_child (text_view, vc);
7516 g_assert (vc->widget == child);
7517 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7521 * gtk_text_view_add_child_in_window:
7522 * @text_view: a #GtkTextView
7523 * @child: a #GtkWidget
7524 * @which_window: which window the child should appear in
7525 * @xpos: X position of child in window coordinates
7526 * @ypos: Y position of child in window coordinates
7528 * Adds a child at fixed coordinates in one of the text widget's
7529 * windows. The window must have nonzero size (see
7530 * gtk_text_view_set_border_window_size()). Note that the child
7531 * coordinates are given relative to the #GdkWindow in question, and
7532 * that these coordinates have no sane relationship to scrolling. When
7533 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7534 * irrelevant, the child floats above all scrollable areas. But when
7535 * placing a child in one of the scrollable windows (border windows or
7536 * text window), you'll need to compute the child's correct position
7537 * in buffer coordinates any time scrolling occurs or buffer changes
7538 * occur, and then call gtk_text_view_move_child() to update the
7539 * child's position. Unfortunately there's no good way to detect that
7540 * scrolling has occurred, using the current API; a possible hack
7541 * would be to update all child positions when the scroll adjustments
7542 * change or the text buffer changes. See bug 64518 on
7543 * bugzilla.gnome.org for status of fixing this issue.
7547 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7549 GtkTextWindowType which_window,
7553 GtkTextViewChild *vc;
7555 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7556 g_return_if_fail (GTK_IS_WIDGET (child));
7557 g_return_if_fail (child->parent == NULL);
7559 vc = text_view_child_new_window (child, which_window,
7562 add_child (text_view, vc);
7564 g_assert (vc->widget == child);
7565 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7569 * gtk_text_view_move_child:
7570 * @text_view: a #GtkTextView
7571 * @child: child widget already added to the text view
7572 * @xpos: new X position in window coordinates
7573 * @ypos: new Y position in window coordinates
7575 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7579 gtk_text_view_move_child (GtkTextView *text_view,
7584 GtkTextViewChild *vc;
7586 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7587 g_return_if_fail (GTK_IS_WIDGET (child));
7588 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7590 vc = g_object_get_data (G_OBJECT (child),
7591 "gtk-text-view-child");
7593 g_assert (vc != NULL);
7595 if (vc->x == xpos &&
7602 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7603 gtk_widget_queue_resize (child);
7607 /* Iterator operations */
7610 * gtk_text_view_forward_display_line:
7611 * @text_view: a #GtkTextView
7612 * @iter: a #GtkTextIter
7614 * Moves the given @iter forward by one display (wrapped) line. A
7615 * display line is different from a paragraph. Paragraphs are
7616 * separated by newlines or other paragraph separator characters.
7617 * Display lines are created by line-wrapping a paragraph. If
7618 * wrapping is turned off, display lines and paragraphs will be the
7619 * same. Display lines are divided differently for each view, since
7620 * they depend on the view's width; paragraphs are the same in all
7621 * views, since they depend on the contents of the #GtkTextBuffer.
7623 * Return value: %TRUE if @iter was moved and is not on the end iterator
7626 gtk_text_view_forward_display_line (GtkTextView *text_view,
7629 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7630 g_return_val_if_fail (iter != NULL, FALSE);
7632 gtk_text_view_ensure_layout (text_view);
7634 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7638 * gtk_text_view_backward_display_line:
7639 * @text_view: a #GtkTextView
7640 * @iter: a #GtkTextIter
7642 * Moves the given @iter backward by one display (wrapped) line. A
7643 * display line is different from a paragraph. Paragraphs are
7644 * separated by newlines or other paragraph separator characters.
7645 * Display lines are created by line-wrapping a paragraph. If
7646 * wrapping is turned off, display lines and paragraphs will be the
7647 * same. Display lines are divided differently for each view, since
7648 * they depend on the view's width; paragraphs are the same in all
7649 * views, since they depend on the contents of the #GtkTextBuffer.
7651 * Return value: %TRUE if @iter was moved and is not on the end iterator
7654 gtk_text_view_backward_display_line (GtkTextView *text_view,
7657 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7658 g_return_val_if_fail (iter != NULL, FALSE);
7660 gtk_text_view_ensure_layout (text_view);
7662 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7666 * gtk_text_view_forward_display_line_end:
7667 * @text_view: a #GtkTextView
7668 * @iter: a #GtkTextIter
7670 * Moves the given @iter forward to the next display line end. A
7671 * display line is different from a paragraph. Paragraphs are
7672 * separated by newlines or other paragraph separator characters.
7673 * Display lines are created by line-wrapping a paragraph. If
7674 * wrapping is turned off, display lines and paragraphs will be the
7675 * same. Display lines are divided differently for each view, since
7676 * they depend on the view's width; paragraphs are the same in all
7677 * views, since they depend on the contents of the #GtkTextBuffer.
7679 * Return value: %TRUE if @iter was moved and is not on the end iterator
7682 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7685 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7686 g_return_val_if_fail (iter != NULL, FALSE);
7688 gtk_text_view_ensure_layout (text_view);
7690 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7694 * gtk_text_view_backward_display_line_start:
7695 * @text_view: a #GtkTextView
7696 * @iter: a #GtkTextIter
7698 * Moves the given @iter backward to the next display line start. A
7699 * display line is different from a paragraph. Paragraphs are
7700 * separated by newlines or other paragraph separator characters.
7701 * Display lines are created by line-wrapping a paragraph. If
7702 * wrapping is turned off, display lines and paragraphs will be the
7703 * same. Display lines are divided differently for each view, since
7704 * they depend on the view's width; paragraphs are the same in all
7705 * views, since they depend on the contents of the #GtkTextBuffer.
7707 * Return value: %TRUE if @iter was moved and is not on the end iterator
7710 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7713 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7714 g_return_val_if_fail (iter != NULL, FALSE);
7716 gtk_text_view_ensure_layout (text_view);
7718 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7722 * gtk_text_view_starts_display_line:
7723 * @text_view: a #GtkTextView
7724 * @iter: a #GtkTextIter
7726 * Determines whether @iter is at the start of a display line.
7727 * See gtk_text_view_forward_display_line() for an explanation of
7728 * display lines vs. paragraphs.
7730 * Return value: %TRUE if @iter begins a wrapped line
7733 gtk_text_view_starts_display_line (GtkTextView *text_view,
7734 const GtkTextIter *iter)
7736 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7737 g_return_val_if_fail (iter != NULL, FALSE);
7739 gtk_text_view_ensure_layout (text_view);
7741 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7745 * gtk_text_view_move_visually:
7746 * @text_view: a #GtkTextView
7747 * @iter: a #GtkTextIter
7748 * @count: number of characters to move (negative moves left, positive moves right)
7750 * Move the iterator a given number of characters visually, treating
7751 * it as the strong cursor position. If @count is positive, then the
7752 * new strong cursor position will be @count positions to the right of
7753 * the old cursor position. If @count is negative then the new strong
7754 * cursor position will be @count positions to the left of the old
7757 * In the presence of bidirection text, the correspondence
7758 * between logical and visual order will depend on the direction
7759 * of the current run, and there may be jumps when the cursor
7760 * is moved off of the end of a run.
7762 * Return value: %TRUE if @iter moved and is not on the end iterator
7765 gtk_text_view_move_visually (GtkTextView *text_view,
7769 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7770 g_return_val_if_fail (iter != NULL, FALSE);
7772 gtk_text_view_ensure_layout (text_view);
7774 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);