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);
2990 if (text_view->hadjustment->value > text_view->hadjustment->upper - text_view->hadjustment->page_size)
2991 gtk_adjustment_set_value (text_view->hadjustment, MAX (0, text_view->hadjustment->upper - text_view->hadjustment->page_size));
2993 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2994 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2995 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2996 text_view->vadjustment->lower = 0;
2997 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2999 gtk_adjustment_changed (text_view->vadjustment);
3001 if (yoffset_changed)
3002 gtk_adjustment_value_changed (vadj);
3004 /* The GTK resize loop processes all the pending exposes right
3005 * after doing the resize stuff, so the idle sizer won't have a
3006 * chance to run. So we do the work here.
3008 gtk_text_view_flush_first_validate (text_view);
3010 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3013 if (size_changed && GTK_WIDGET_REALIZED (widget))
3014 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3018 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3021 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3022 text_view->first_para_mark);
3026 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3028 GtkWidget *widget = GTK_WIDGET (text_view);
3030 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3032 if (SCREEN_HEIGHT (widget) > 0)
3034 GtkTextIter first_para;
3036 /* Be sure we've validated the stuff onscreen; if we
3037 * scrolled, these calls won't have any effect, because
3038 * they were called in the recursive validate_onscreen
3040 gtk_text_view_get_first_para_iter (text_view, &first_para);
3042 gtk_text_layout_validate_yrange (text_view->layout,
3045 text_view->first_para_pixels +
3046 SCREEN_HEIGHT (widget));
3049 text_view->onscreen_validated = TRUE;
3051 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3053 /* This can have the odd side effect of triggering a scroll, which should
3054 * flip "onscreen_validated" back to FALSE, but should also get us
3055 * back into this function to turn it on again.
3057 gtk_text_view_update_adjustments (text_view);
3059 g_assert (text_view->onscreen_validated);
3063 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3065 if (text_view->first_validate_idle == 0)
3068 /* Do this first, which means that if an "invalidate"
3069 * occurs during any of this process, a new first_validate_callback
3070 * will be installed, and we'll start again.
3072 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3073 g_source_remove (text_view->first_validate_idle);
3074 text_view->first_validate_idle = 0;
3076 /* be sure we have up-to-date screen size set on the
3079 gtk_text_view_update_layout_width (text_view);
3081 /* Bail out if we invalidated stuff; scrolling right away will just
3082 * confuse the issue.
3084 if (text_view->first_validate_idle != 0)
3086 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3090 /* scroll to any marks, if that's pending. This can jump us to
3091 * the validation codepath used for scrolling onscreen, if so we
3092 * bail out. It won't jump if already in that codepath since
3093 * value_changed is not recursive, so also validate if
3096 if (!gtk_text_view_flush_scroll (text_view) ||
3097 !text_view->onscreen_validated)
3098 gtk_text_view_validate_onscreen (text_view);
3100 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3102 g_assert (text_view->onscreen_validated);
3107 first_validate_callback (gpointer data)
3109 GtkTextView *text_view = data;
3111 GDK_THREADS_ENTER ();
3113 /* Note that some of this code is duplicated at the end of size_allocate,
3114 * keep in sync with that.
3117 DV(g_print(G_STRLOC"\n"));
3119 gtk_text_view_flush_first_validate (text_view);
3121 GDK_THREADS_LEAVE ();
3127 incremental_validate_callback (gpointer data)
3129 GtkTextView *text_view = data;
3130 gboolean result = TRUE;
3132 GDK_THREADS_ENTER ();
3134 DV(g_print(G_STRLOC"\n"));
3136 gtk_text_layout_validate (text_view->layout, 2000);
3138 gtk_text_view_update_adjustments (text_view);
3140 if (gtk_text_layout_is_valid (text_view->layout))
3142 text_view->incremental_validate_idle = 0;
3146 GDK_THREADS_LEAVE ();
3152 gtk_text_view_invalidate (GtkTextView *text_view)
3154 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3155 text_view->onscreen_validated));
3157 text_view->onscreen_validated = FALSE;
3159 /* We'll invalidate when the layout is created */
3160 if (text_view->layout == NULL)
3163 if (!text_view->first_validate_idle)
3165 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3166 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3167 text_view->first_validate_idle));
3170 if (!text_view->incremental_validate_idle)
3172 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3173 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3174 text_view->incremental_validate_idle));
3179 invalidated_handler (GtkTextLayout *layout,
3182 GtkTextView *text_view;
3184 text_view = GTK_TEXT_VIEW (data);
3186 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3187 gtk_text_view_invalidate (text_view);
3191 changed_handler (GtkTextLayout *layout,
3197 GtkTextView *text_view;
3199 GdkRectangle visible_rect;
3200 GdkRectangle redraw_rect;
3202 text_view = GTK_TEXT_VIEW (data);
3203 widget = GTK_WIDGET (data);
3205 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3207 if (GTK_WIDGET_REALIZED (text_view))
3209 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3211 redraw_rect.x = visible_rect.x;
3212 redraw_rect.width = visible_rect.width;
3213 redraw_rect.y = start_y;
3215 if (old_height == new_height)
3216 redraw_rect.height = old_height;
3217 else if (start_y + old_height > visible_rect.y)
3218 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3220 redraw_rect.height = 0;
3222 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3224 /* text_window_invalidate_rect() takes buffer coordinates */
3225 text_window_invalidate_rect (text_view->text_window,
3228 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3232 redraw_rect.height));
3234 if (text_view->left_window)
3235 text_window_invalidate_rect (text_view->left_window,
3237 if (text_view->right_window)
3238 text_window_invalidate_rect (text_view->right_window,
3240 if (text_view->top_window)
3241 text_window_invalidate_rect (text_view->top_window,
3243 if (text_view->bottom_window)
3244 text_window_invalidate_rect (text_view->bottom_window,
3247 gtk_text_view_update_im_spot_location (text_view);
3251 if (old_height != new_height)
3253 gboolean yoffset_changed = FALSE;
3255 int new_first_para_top;
3256 int old_first_para_top;
3259 /* If the bottom of the old area was above the top of the
3260 * screen, we need to scroll to keep the current top of the
3261 * screen in place. Remember that first_para_pixels is the
3262 * position of the top of the screen in coordinates relative to
3263 * the first paragraph onscreen.
3265 * In short we are adding the height delta of the portion of the
3266 * changed region above first_para_mark to text_view->yoffset.
3268 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3269 text_view->first_para_mark);
3271 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3273 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3275 if (new_first_para_top != old_first_para_top)
3277 text_view->yoffset += new_first_para_top - old_first_para_top;
3279 get_vadjustment (text_view)->value = text_view->yoffset;
3280 yoffset_changed = TRUE;
3283 if (yoffset_changed)
3285 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3286 gtk_adjustment_value_changed (get_vadjustment (text_view));
3289 /* FIXME be smarter about which anchored widgets we update */
3291 tmp_list = text_view->children;
3292 while (tmp_list != NULL)
3294 GtkTextViewChild *child = tmp_list->data;
3297 gtk_text_view_update_child_allocation (text_view, child);
3299 tmp_list = g_slist_next (tmp_list);
3304 GtkRequisition old_req;
3305 GtkRequisition new_req;
3307 old_req = widget->requisition;
3309 /* Use this instead of gtk_widget_size_request wrapper
3310 * to avoid the optimization which just returns widget->requisition
3311 * if a resize hasn't been queued.
3313 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3315 if (old_req.width != new_req.width ||
3316 old_req.height != new_req.height)
3318 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3319 _gtk_size_group_queue_resize (widget);
3325 gtk_text_view_realize (GtkWidget *widget)
3327 GtkTextView *text_view;
3328 GdkWindowAttr attributes;
3329 gint attributes_mask;
3332 text_view = GTK_TEXT_VIEW (widget);
3333 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3335 attributes.window_type = GDK_WINDOW_CHILD;
3336 attributes.x = widget->allocation.x;
3337 attributes.y = widget->allocation.y;
3338 attributes.width = widget->allocation.width;
3339 attributes.height = widget->allocation.height;
3340 attributes.wclass = GDK_INPUT_OUTPUT;
3341 attributes.visual = gtk_widget_get_visual (widget);
3342 attributes.colormap = gtk_widget_get_colormap (widget);
3343 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3345 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3347 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3348 &attributes, attributes_mask);
3349 gdk_window_set_user_data (widget->window, widget);
3351 /* must come before text_window_realize calls */
3352 widget->style = gtk_style_attach (widget->style, widget->window);
3354 gdk_window_set_background (widget->window,
3355 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3357 text_window_realize (text_view->text_window, widget->window);
3359 if (text_view->left_window)
3360 text_window_realize (text_view->left_window,
3363 if (text_view->top_window)
3364 text_window_realize (text_view->top_window,
3367 if (text_view->right_window)
3368 text_window_realize (text_view->right_window,
3371 if (text_view->bottom_window)
3372 text_window_realize (text_view->bottom_window,
3375 gtk_text_view_ensure_layout (text_view);
3377 if (text_view->buffer)
3379 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3380 GDK_SELECTION_PRIMARY);
3381 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3384 tmp_list = text_view->children;
3385 while (tmp_list != NULL)
3387 GtkTextViewChild *vc = tmp_list->data;
3389 text_view_child_set_parent_window (text_view, vc);
3391 tmp_list = tmp_list->next;
3396 gtk_text_view_unrealize (GtkWidget *widget)
3398 GtkTextView *text_view;
3400 text_view = GTK_TEXT_VIEW (widget);
3402 if (text_view->buffer)
3404 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3405 GDK_SELECTION_PRIMARY);
3406 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3409 gtk_text_view_remove_validate_idles (text_view);
3411 if (text_view->popup_menu)
3413 gtk_widget_destroy (text_view->popup_menu);
3414 text_view->popup_menu = NULL;
3417 text_window_unrealize (text_view->text_window);
3419 if (text_view->left_window)
3420 text_window_unrealize (text_view->left_window);
3422 if (text_view->top_window)
3423 text_window_unrealize (text_view->top_window);
3425 if (text_view->right_window)
3426 text_window_unrealize (text_view->right_window);
3428 if (text_view->bottom_window)
3429 text_window_unrealize (text_view->bottom_window);
3431 gtk_text_view_destroy_layout (text_view);
3433 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3437 gtk_text_view_set_background (GtkTextView *text_view)
3439 GtkWidget *widget = GTK_WIDGET (text_view);
3441 gdk_window_set_background (widget->window,
3442 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3444 gdk_window_set_background (text_view->text_window->bin_window,
3445 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3447 if (text_view->left_window)
3448 gdk_window_set_background (text_view->left_window->bin_window,
3449 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3450 if (text_view->right_window)
3451 gdk_window_set_background (text_view->right_window->bin_window,
3452 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3454 if (text_view->top_window)
3455 gdk_window_set_background (text_view->top_window->bin_window,
3456 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3458 if (text_view->bottom_window)
3459 gdk_window_set_background (text_view->bottom_window->bin_window,
3460 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3464 gtk_text_view_style_set (GtkWidget *widget,
3465 GtkStyle *previous_style)
3467 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3469 if (GTK_WIDGET_REALIZED (widget))
3471 gtk_text_view_set_background (text_view);
3474 if (text_view->layout && previous_style)
3476 gtk_text_view_set_attributes_from_style (text_view,
3477 text_view->layout->default_style,
3479 gtk_text_layout_default_style_changed (text_view->layout);
3484 gtk_text_view_direction_changed (GtkWidget *widget,
3485 GtkTextDirection previous_direction)
3487 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3489 if (text_view->layout)
3491 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3492 gtk_text_layout_default_style_changed (text_view->layout);
3497 gtk_text_view_state_changed (GtkWidget *widget,
3498 GtkStateType previous_state)
3500 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3502 if (GTK_WIDGET_REALIZED (widget))
3504 gtk_text_view_set_background (text_view);
3507 if (!GTK_WIDGET_IS_SENSITIVE (widget))
3509 /* Clear any selection */
3510 gtk_text_view_unselect (text_view);
3513 gtk_widget_queue_draw (widget);
3517 set_invisible_cursor (GdkWindow *window)
3519 GdkBitmap *empty_bitmap;
3522 char invisible_cursor_bits[] = { 0x0 };
3524 useless.red = useless.green = useless.blue = 0;
3527 empty_bitmap = gdk_bitmap_create_from_data (window,
3528 invisible_cursor_bits,
3531 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3536 gdk_window_set_cursor (window, cursor);
3538 gdk_cursor_unref (cursor);
3540 g_object_unref (empty_bitmap);
3544 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3546 if (text_view->mouse_cursor_obscured)
3549 set_invisible_cursor (text_view->text_window->bin_window);
3551 text_view->mouse_cursor_obscured = TRUE;
3559 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3562 switch (event->type)
3564 case GDK_MOTION_NOTIFY:
3565 *x = event->motion.x;
3566 *y = event->motion.y;
3570 case GDK_BUTTON_PRESS:
3571 case GDK_2BUTTON_PRESS:
3572 case GDK_3BUTTON_PRESS:
3573 case GDK_BUTTON_RELEASE:
3574 *x = event->button.x;
3575 *y = event->button.y;
3580 case GDK_KEY_RELEASE:
3581 case GDK_ENTER_NOTIFY:
3582 case GDK_LEAVE_NOTIFY:
3583 case GDK_PROPERTY_NOTIFY:
3584 case GDK_SELECTION_CLEAR:
3585 case GDK_SELECTION_REQUEST:
3586 case GDK_SELECTION_NOTIFY:
3587 case GDK_PROXIMITY_IN:
3588 case GDK_PROXIMITY_OUT:
3589 case GDK_DRAG_ENTER:
3590 case GDK_DRAG_LEAVE:
3591 case GDK_DRAG_MOTION:
3592 case GDK_DRAG_STATUS:
3593 case GDK_DROP_START:
3594 case GDK_DROP_FINISHED:
3604 emit_event_on_tags (GtkWidget *widget,
3610 gboolean retval = FALSE;
3611 GtkTextView *text_view;
3613 text_view = GTK_TEXT_VIEW (widget);
3615 tags = gtk_text_iter_get_tags (iter);
3620 GtkTextTag *tag = tmp->data;
3622 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3628 tmp = g_slist_next (tmp);
3631 g_slist_free (tags);
3637 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3639 GtkTextView *text_view;
3642 text_view = GTK_TEXT_VIEW (widget);
3644 if (text_view->layout == NULL ||
3645 get_buffer (text_view) == NULL)
3648 if (event->any.window != text_view->text_window->bin_window)
3651 if (get_event_coordinates (event, &x, &y))
3655 x += text_view->xoffset;
3656 y += text_view->yoffset;
3658 /* FIXME this is slow and we do it twice per event.
3659 * My favorite solution is to have GtkTextLayout cache
3660 * the last couple lookups.
3662 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3666 return emit_event_on_tags (widget, event, &iter);
3668 else if (event->type == GDK_KEY_PRESS ||
3669 event->type == GDK_KEY_RELEASE)
3671 GtkTextMark *insert;
3674 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3677 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3679 return emit_event_on_tags (widget, event, &iter);
3686 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3688 gboolean retval = FALSE;
3689 gboolean obscure = FALSE;
3690 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3691 GtkTextMark *insert;
3693 gboolean can_insert;
3695 if (text_view->layout == NULL ||
3696 get_buffer (text_view) == NULL)
3699 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3700 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3701 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3703 gtk_im_context_filter_keypress (text_view->im_context, event))
3705 text_view->need_im_reset = TRUE;
3710 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3711 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3713 /* use overall editability not can_insert, more predictable for users */
3714 else if (text_view->editable &&
3715 (event->keyval == GDK_Return ||
3716 event->keyval == GDK_KP_Enter))
3718 /* this won't actually insert the newline if the cursor isn't
3721 gtk_text_view_commit_text (text_view, "\n");
3726 /* Pass through Tab as literal tab, unless Control is held down */
3727 else if ((event->keyval == GDK_Tab ||
3728 event->keyval == GDK_KP_Tab) &&
3729 !(event->state & GDK_CONTROL_MASK))
3731 /* If the text widget isn't editable overall, move the focus
3734 if (text_view->editable)
3736 gtk_text_view_commit_text (text_view, "\t");
3740 gtk_text_view_move_focus (text_view,
3741 (event->state & GDK_SHIFT_MASK) ?
3742 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3750 gtk_text_view_obscure_mouse_cursor (text_view);
3752 gtk_text_view_pend_cursor_blink (text_view);
3758 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3760 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3761 GtkTextMark *insert;
3764 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3767 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3768 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3769 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3770 gtk_im_context_filter_keypress (text_view->im_context, event))
3772 text_view->need_im_reset = TRUE;
3776 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3780 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3782 GtkTextView *text_view;
3784 text_view = GTK_TEXT_VIEW (widget);
3786 text_view->disable_scroll_on_focus = TRUE;
3787 gtk_widget_grab_focus (widget);
3788 text_view->disable_scroll_on_focus = FALSE;
3790 if (event->window != text_view->text_window->bin_window)
3792 /* Remove selection if any. */
3793 gtk_text_view_unselect (text_view);
3799 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3800 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3801 else if (event->button == 3)
3802 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3805 if (event->type == GDK_BUTTON_PRESS)
3807 gtk_text_view_reset_im_context (text_view);
3809 if (event->button == 1)
3811 /* If we're in the selection, start a drag copy/move of the
3812 * selection; otherwise, start creating a new selection.
3815 GtkTextIter start, end;
3817 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3819 event->x + text_view->xoffset,
3820 event->y + text_view->yoffset);
3822 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3824 gtk_text_iter_in_range (&iter, &start, &end))
3826 text_view->drag_start_x = event->x;
3827 text_view->drag_start_y = event->y;
3828 text_view->pending_place_cursor_button = event->button;
3832 gtk_text_view_start_selection_drag (text_view, &iter, event);
3837 else if (event->button == 2)
3841 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3843 event->x + text_view->xoffset,
3844 event->y + text_view->yoffset);
3846 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3847 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3849 text_view->editable);
3852 else if (event->button == 3)
3854 gtk_text_view_do_popup (text_view, event);
3858 else if ((event->type == GDK_2BUTTON_PRESS ||
3859 event->type == GDK_3BUTTON_PRESS) &&
3862 GtkTextIter start, end;
3864 /* End the selection drag, otherwise we'd clear the new
3865 * word/line selection on button release
3867 gtk_text_view_end_selection_drag (text_view, event);
3869 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3871 event->x + text_view->xoffset,
3872 event->y + text_view->yoffset);
3876 if (event->type == GDK_2BUTTON_PRESS)
3878 if (gtk_text_iter_inside_word (&start))
3880 if (!gtk_text_iter_starts_word (&start))
3881 gtk_text_iter_backward_word_start (&start);
3883 if (!gtk_text_iter_ends_word (&end))
3884 gtk_text_iter_forward_word_end (&end);
3887 else if (event->type == GDK_3BUTTON_PRESS)
3889 if (gtk_text_view_starts_display_line (text_view, &start))
3891 /* If on a display line boundary, we assume the user
3892 * clicked off the end of a line and we therefore select
3893 * the line before the boundary.
3895 gtk_text_view_backward_display_line_start (text_view, &start);
3899 /* start isn't on the start of a line, so we move it to the
3900 * start, and move end to the end unless it's already there.
3902 gtk_text_view_backward_display_line_start (text_view, &start);
3904 if (!gtk_text_view_starts_display_line (text_view, &end))
3905 gtk_text_view_forward_display_line_end (text_view, &end);
3909 if (event->state & GDK_SHIFT_MASK)
3911 /* Take union of old and new selection */
3912 GtkTextIter old_start, old_end;
3914 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3915 &old_start, &old_end);
3917 gtk_text_iter_order (&start, &old_start);
3918 gtk_text_iter_order (&old_end, &end);
3920 /* Now start is the first of the starts, and end is the
3925 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3928 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3932 text_view->just_selected_element = TRUE;
3941 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3943 GtkTextView *text_view;
3945 text_view = GTK_TEXT_VIEW (widget);
3947 if (event->window != text_view->text_window->bin_window)
3950 if (event->button == 1)
3952 if (text_view->drag_start_x >= 0)
3954 text_view->drag_start_x = -1;
3955 text_view->drag_start_y = -1;
3958 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3960 else if (text_view->just_selected_element)
3962 text_view->just_selected_element = FALSE;
3965 else if (text_view->pending_place_cursor_button == event->button)
3969 /* Unselect everything; we clicked inside selection, but
3970 * didn't move by the drag threshold, so just clear selection
3973 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3975 event->x + text_view->xoffset,
3976 event->y + text_view->yoffset);
3978 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3980 text_view->pending_place_cursor_button = 0;
3990 keymap_direction_changed (GdkKeymap *keymap,
3991 GtkTextView *text_view)
3993 gtk_text_view_check_keymap_direction (text_view);
3997 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3999 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4001 gtk_widget_queue_draw (widget);
4003 DV(g_print (G_STRLOC": focus_in_event\n"));
4005 if (text_view->cursor_visible && text_view->layout)
4007 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4008 gtk_text_view_check_cursor_blink (text_view);
4011 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4012 "direction_changed",
4013 G_CALLBACK (keymap_direction_changed), text_view);
4014 gtk_text_view_check_keymap_direction (text_view);
4016 text_view->need_im_reset = TRUE;
4017 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4023 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4025 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4027 gtk_widget_queue_draw (widget);
4029 DV(g_print (G_STRLOC": focus_out_event\n"));
4031 if (text_view->cursor_visible && text_view->layout)
4033 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4034 gtk_text_view_check_cursor_blink (text_view);
4037 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4038 keymap_direction_changed,
4041 text_view->need_im_reset = TRUE;
4042 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4048 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4050 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4052 if (text_view->mouse_cursor_obscured)
4056 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4058 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4059 gdk_cursor_unref (cursor);
4060 text_view->mouse_cursor_obscured = FALSE;
4063 if (event->window == text_view->text_window->bin_window &&
4064 text_view->drag_start_x >= 0)
4068 gdk_window_get_pointer (text_view->text_window->bin_window,
4071 if (gtk_drag_check_threshold (widget,
4072 text_view->drag_start_x,
4073 text_view->drag_start_y,
4077 gint buffer_x, buffer_y;
4079 gtk_text_view_window_to_buffer_coords (text_view,
4080 GTK_TEXT_WINDOW_TEXT,
4081 text_view->drag_start_x,
4082 text_view->drag_start_y,
4086 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4088 buffer_x, buffer_y);
4090 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4099 gtk_text_view_paint (GtkWidget *widget,
4101 GdkEventExpose *event)
4103 GtkTextView *text_view;
4104 GList *child_exposes;
4108 text_view = GTK_TEXT_VIEW (widget);
4110 g_return_if_fail (text_view->layout != NULL);
4111 g_return_if_fail (text_view->xoffset >= 0);
4112 g_return_if_fail (text_view->yoffset >= 0);
4114 while (text_view->first_validate_idle != 0)
4116 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4117 text_view->first_validate_idle));
4118 gtk_text_view_flush_first_validate (text_view);
4121 /* More regions could have become invalid in the above loop */
4122 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4127 gdk_region_get_clipbox (updates, &rect);
4129 gdk_rectangle_union (area, &rect, area);
4131 gdk_region_destroy (updates);
4134 if (!text_view->onscreen_validated)
4136 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.");
4137 g_assert_not_reached ();
4141 printf ("painting %d,%d %d x %d\n",
4143 area->width, area->height);
4146 child_exposes = NULL;
4147 gtk_text_layout_draw (text_view->layout,
4149 text_view->text_window->bin_window,
4154 area->width, area->height,
4157 tmp_list = child_exposes;
4158 while (tmp_list != NULL)
4160 GtkWidget *child = tmp_list->data;
4162 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4166 g_object_unref (child);
4168 tmp_list = tmp_list->next;
4171 g_list_free (child_exposes);
4175 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4179 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4180 GTK_TEXT_WINDOW_TEXT))
4182 DV(g_print (">Exposed ("G_STRLOC")\n"));
4183 gtk_text_view_paint (widget, &event->area, event);
4186 if (event->window == widget->window)
4187 gtk_text_view_draw_focus (widget);
4189 /* Propagate exposes to all children not in the buffer. */
4190 tmp_list = GTK_TEXT_VIEW (widget)->children;
4191 while (tmp_list != NULL)
4193 GtkTextViewChild *vc = tmp_list->data;
4195 /* propagate_expose checks that event->window matches
4198 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4199 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4203 tmp_list = tmp_list->next;
4210 gtk_text_view_draw_focus (GtkWidget *widget)
4212 gboolean interior_focus;
4214 /* We clear the focus if we are in interior focus mode. */
4215 gtk_widget_style_get (widget,
4216 "interior_focus", &interior_focus,
4219 if (GTK_WIDGET_DRAWABLE (widget))
4221 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4223 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4224 NULL, widget, "textview",
4226 widget->allocation.width,
4227 widget->allocation.height);
4231 gdk_window_clear (widget->window);
4237 gtk_text_view_grab_focus (GtkWidget *widget)
4239 GtkTextView *text_view;
4241 text_view = GTK_TEXT_VIEW (widget);
4243 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4245 if (!text_view->disable_scroll_on_focus)
4246 gtk_text_view_scroll_mark_onscreen (text_view,
4247 gtk_text_buffer_get_mark (get_buffer (text_view),
4252 gtk_text_view_focus (GtkWidget *widget,
4253 GtkDirectionType direction)
4255 GtkTextView *text_view;
4256 GtkContainer *container;
4259 text_view = GTK_TEXT_VIEW (widget);
4260 container = GTK_CONTAINER (widget);
4262 if (!gtk_widget_is_focus (widget) &&
4263 container->focus_child == NULL)
4265 gtk_widget_grab_focus (widget);
4271 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4272 * children to get the focus
4274 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4275 result = GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4276 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4287 gtk_text_view_add (GtkContainer *container,
4290 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4291 g_return_if_fail (GTK_IS_WIDGET (child));
4293 /* This is pretty random. */
4294 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4296 GTK_TEXT_WINDOW_WIDGET,
4301 gtk_text_view_remove (GtkContainer *container,
4305 GtkTextView *text_view;
4306 GtkTextViewChild *vc;
4308 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4309 g_return_if_fail (GTK_IS_WIDGET (child));
4310 g_return_if_fail (child->parent == (GtkWidget*) container);
4312 text_view = GTK_TEXT_VIEW (container);
4315 iter = text_view->children;
4317 while (iter != NULL)
4321 if (vc->widget == child)
4324 iter = g_slist_next (iter);
4327 g_assert (iter != NULL); /* be sure we had the child in the list */
4329 text_view->children = g_slist_remove (text_view->children, vc);
4331 gtk_widget_unparent (vc->widget);
4333 text_view_child_free (vc);
4337 gtk_text_view_forall (GtkContainer *container,
4338 gboolean include_internals,
4339 GtkCallback callback,
4340 gpointer callback_data)
4343 GtkTextView *text_view;
4346 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4347 g_return_if_fail (callback != NULL);
4349 text_view = GTK_TEXT_VIEW (container);
4351 copy = g_slist_copy (text_view->children);
4354 while (iter != NULL)
4356 GtkTextViewChild *vc = iter->data;
4358 (* callback) (vc->widget, callback_data);
4360 iter = g_slist_next (iter);
4363 g_slist_free (copy);
4366 #define CURSOR_ON_MULTIPLIER 0.66
4367 #define CURSOR_OFF_MULTIPLIER 0.34
4368 #define CURSOR_PEND_MULTIPLIER 1.0
4371 cursor_blinks (GtkTextView *text_view)
4373 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4376 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4379 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4382 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4387 get_cursor_time (GtkTextView *text_view)
4389 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4392 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4402 blink_cb (gpointer data)
4404 GtkTextView *text_view;
4407 GDK_THREADS_ENTER ();
4409 text_view = GTK_TEXT_VIEW (data);
4411 g_assert (text_view->layout);
4412 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4413 g_assert (text_view->cursor_visible);
4415 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4418 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4422 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4426 gtk_text_layout_set_cursor_visible (text_view->layout,
4429 GDK_THREADS_LEAVE ();
4431 /* Remove ourselves */
4437 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4439 if (text_view->blink_timeout)
4441 gtk_timeout_remove (text_view->blink_timeout);
4442 text_view->blink_timeout = 0;
4447 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4449 if (text_view->layout != NULL &&
4450 text_view->cursor_visible &&
4451 GTK_WIDGET_HAS_FOCUS (text_view))
4453 if (cursor_blinks (text_view))
4455 if (text_view->blink_timeout == 0)
4457 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4459 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4465 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4469 gtk_text_view_stop_cursor_blink (text_view);
4474 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4476 if (text_view->layout != NULL &&
4477 text_view->cursor_visible &&
4478 GTK_WIDGET_HAS_FOCUS (text_view) &&
4479 cursor_blinks (text_view))
4481 if (text_view->blink_timeout != 0)
4483 gtk_timeout_remove (text_view->blink_timeout);
4484 text_view->blink_timeout = 0;
4487 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4489 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4497 * Key binding handlers
4501 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4502 GtkTextIter *newplace,
4507 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4513 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4519 move_cursor (GtkTextView *text_view,
4520 const GtkTextIter *new_location,
4521 gboolean extend_selection)
4523 if (extend_selection)
4524 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4528 gtk_text_buffer_place_cursor (get_buffer (text_view),
4532 /* FIXME when we are unfrozen and can change GtkMovementStep,
4535 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4538 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4539 GtkMovementStep step,
4541 gboolean extend_selection)
4544 GtkTextIter newplace;
4546 gint cursor_x_pos = 0;
4548 gtk_text_view_reset_im_context (text_view);
4550 if (step == GTK_MOVEMENT_PAGES)
4552 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4553 gtk_text_view_pend_cursor_blink (text_view);
4556 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4558 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4559 gtk_text_view_pend_cursor_blink (text_view);
4563 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4564 gtk_text_buffer_get_mark (get_buffer (text_view),
4568 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4569 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4573 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4574 gtk_text_iter_forward_cursor_positions (&newplace, count);
4577 case GTK_MOVEMENT_VISUAL_POSITIONS:
4578 gtk_text_layout_move_iter_visually (text_view->layout,
4582 case GTK_MOVEMENT_WORDS:
4584 gtk_text_iter_backward_word_starts (&newplace, -count);
4586 gtk_text_iter_forward_word_ends (&newplace, count);
4589 case GTK_MOVEMENT_DISPLAY_LINES:
4590 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4591 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4594 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4596 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4597 else if (count < -1)
4598 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4601 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4604 case GTK_MOVEMENT_PARAGRAPHS:
4607 if (!gtk_text_iter_ends_line (&newplace))
4609 gtk_text_iter_forward_to_line_end (&newplace);
4615 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4617 gtk_text_iter_set_line_offset (&newplace, 0);
4624 gtk_text_iter_forward_lines (&newplace, count);
4625 gtk_text_iter_set_line_offset (&newplace, 0);
4629 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4632 if (!gtk_text_iter_ends_line (&newplace))
4633 gtk_text_iter_forward_to_line_end (&newplace);
4637 gtk_text_iter_set_line_offset (&newplace, 0);
4641 case GTK_MOVEMENT_BUFFER_ENDS:
4643 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4645 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4652 /* call move_cursor() even if the cursor hasn't moved, since it
4653 cancels the selection
4655 move_cursor (text_view, &newplace, extend_selection);
4657 if (!gtk_text_iter_equal (&insert, &newplace))
4659 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4660 gtk_text_view_scroll_mark_onscreen (text_view,
4661 gtk_text_buffer_get_mark (get_buffer (text_view),
4664 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4666 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4670 gtk_text_view_pend_cursor_blink (text_view);
4674 gtk_text_view_move_cursor (GtkTextView *text_view,
4675 GtkMovementStep step,
4677 gboolean extend_selection)
4679 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4683 gtk_text_view_page_horizontally (GtkTextView *text_view,
4685 gboolean extend_selection)
4687 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4688 count, extend_selection);
4692 gtk_text_view_set_anchor (GtkTextView *text_view)
4696 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4697 gtk_text_buffer_get_mark (get_buffer (text_view),
4700 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4704 gtk_text_view_scroll_pages (GtkTextView *text_view,
4706 gboolean extend_selection)
4711 gint cursor_x_pos, cursor_y_pos;
4712 GtkTextIter new_insert;
4716 g_return_if_fail (text_view->vadjustment != NULL);
4718 cancel_pending_scroll (text_view);
4720 adj = text_view->vadjustment;
4722 /* Validate the region that will be brought into view by the cursor motion
4726 gtk_text_view_get_first_para_iter (text_view, &anchor);
4727 y0 = adj->page_size;
4728 y1 = adj->page_size + count * adj->page_increment;
4732 gtk_text_view_get_first_para_iter (text_view, &anchor);
4733 y0 = count * adj->page_increment + adj->page_size;
4737 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4738 /* FIXME do we need to update the adjustment ranges here? */
4740 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4742 /* already at top, just be sure we are at offset 0 */
4743 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4744 move_cursor (text_view, &new_insert, extend_selection);
4746 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4748 /* already at bottom, just be sure we are at the end */
4749 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4750 move_cursor (text_view, &new_insert, extend_selection);
4754 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4756 newval = adj->value;
4757 oldval = adj->value;
4759 newval += count * adj->page_increment;
4761 set_adjustment_clamped (adj, newval);
4762 cursor_y_pos += adj->value - oldval;
4764 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4765 clamp_iter_onscreen (text_view, &new_insert);
4766 move_cursor (text_view, &new_insert, extend_selection);
4768 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4771 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4772 * only guarantees 1 pixel onscreen.
4774 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4775 gtk_text_view_scroll_mark_onscreen (text_view,
4776 gtk_text_buffer_get_mark (get_buffer (text_view),
4781 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4783 gboolean extend_selection)
4788 gint cursor_x_pos, cursor_y_pos;
4789 GtkTextIter new_insert;
4792 g_return_if_fail (text_view->hadjustment != NULL);
4794 cancel_pending_scroll (text_view);
4796 adj = text_view->hadjustment;
4798 /* Validate the line that we're moving within.
4800 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4802 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4803 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4804 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4805 /* FIXME do we need to update the adjustment ranges here? */
4807 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4809 /* already at far left, just be sure we are at offset 0 */
4810 gtk_text_iter_set_line_offset (&new_insert, 0);
4811 move_cursor (text_view, &new_insert, extend_selection);
4813 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4815 /* already at far right, just be sure we are at the end */
4816 gtk_text_iter_forward_to_line_end (&new_insert);
4817 move_cursor (text_view, &new_insert, extend_selection);
4821 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4823 newval = adj->value;
4824 oldval = adj->value;
4826 newval += count * adj->page_increment;
4828 set_adjustment_clamped (adj, newval);
4829 cursor_x_pos += adj->value - oldval;
4831 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4832 clamp_iter_onscreen (text_view, &new_insert);
4833 move_cursor (text_view, &new_insert, extend_selection);
4835 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4838 /* FIXME for lines shorter than the overall widget width, this results in a
4839 * "bounce" effect as we scroll to the right of the widget, then scroll
4840 * back to get the end of the line onscreen.
4841 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4844 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4845 * only guarantees 1 pixel onscreen.
4847 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4848 gtk_text_view_scroll_mark_onscreen (text_view,
4849 gtk_text_buffer_get_mark (get_buffer (text_view),
4854 whitespace (gunichar ch, gpointer user_data)
4856 return (ch == ' ' || ch == '\t');
4860 not_whitespace (gunichar ch, gpointer user_data)
4862 return !whitespace (ch, user_data);
4866 find_whitepace_region (const GtkTextIter *center,
4867 GtkTextIter *start, GtkTextIter *end)
4872 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4873 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4874 if (whitespace (gtk_text_iter_get_char (end), NULL))
4875 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4877 return !gtk_text_iter_equal (start, end);
4881 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4884 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4885 text_view->editable);
4889 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4896 gboolean leave_one = FALSE;
4898 gtk_text_view_reset_im_context (text_view);
4900 if (type == GTK_DELETE_CHARS)
4902 /* Char delete deletes the selection, if one exists */
4903 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4904 text_view->editable))
4908 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4910 gtk_text_buffer_get_mark (get_buffer (text_view),
4918 case GTK_DELETE_CHARS:
4919 gtk_text_iter_forward_cursor_positions (&end, count);
4922 case GTK_DELETE_WORD_ENDS:
4924 gtk_text_iter_forward_word_ends (&end, count);
4926 gtk_text_iter_backward_word_starts (&start, 0 - count);
4929 case GTK_DELETE_WORDS:
4932 case GTK_DELETE_DISPLAY_LINE_ENDS:
4935 case GTK_DELETE_DISPLAY_LINES:
4938 case GTK_DELETE_PARAGRAPH_ENDS:
4939 /* If we're already at a newline, we need to
4940 * simply delete that newline, instead of
4941 * moving to the next one.
4943 if (gtk_text_iter_ends_line (&end))
4945 gtk_text_iter_forward_line (&end);
4951 if (!gtk_text_iter_forward_to_line_end (&end))
4957 /* FIXME figure out what a negative count means
4961 case GTK_DELETE_PARAGRAPHS:
4964 gtk_text_iter_set_line_offset (&start, 0);
4965 gtk_text_iter_forward_to_line_end (&end);
4967 /* Do the lines beyond the first. */
4970 gtk_text_iter_forward_to_line_end (&end);
4976 /* FIXME negative count? */
4980 case GTK_DELETE_WHITESPACE:
4982 find_whitepace_region (&insert, &start, &end);
4990 if (!gtk_text_iter_equal (&start, &end))
4992 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4994 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4995 text_view->editable))
4998 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5000 text_view->editable);
5003 gtk_text_buffer_end_user_action (get_buffer (text_view));
5005 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5006 gtk_text_view_scroll_mark_onscreen (text_view,
5007 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5012 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5014 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5015 GDK_SELECTION_CLIPBOARD);
5017 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5019 text_view->editable);
5020 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5021 gtk_text_view_scroll_mark_onscreen (text_view,
5022 gtk_text_buffer_get_mark (get_buffer (text_view),
5027 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5029 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5030 GDK_SELECTION_CLIPBOARD);
5032 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5034 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5035 gtk_text_view_scroll_mark_onscreen (text_view,
5036 gtk_text_buffer_get_mark (get_buffer (text_view),
5041 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5043 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5044 GDK_SELECTION_CLIPBOARD);
5046 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5049 text_view->editable);
5050 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5051 gtk_text_view_scroll_mark_onscreen (text_view,
5052 gtk_text_buffer_get_mark (get_buffer (text_view),
5057 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5059 text_view->overwrite_mode = !text_view->overwrite_mode;
5063 gtk_text_view_move_focus (GtkTextView *text_view,
5064 GtkDirectionType direction_type)
5066 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5068 if (!GTK_WIDGET_TOPLEVEL (toplevel))
5071 /* Propagate to toplevel */
5072 g_signal_emit_by_name (toplevel, "move_focus", direction_type);
5080 gtk_text_view_unselect (GtkTextView *text_view)
5084 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5086 gtk_text_buffer_get_mark (get_buffer (text_view),
5089 gtk_text_buffer_move_mark (get_buffer (text_view),
5090 gtk_text_buffer_get_mark (get_buffer (text_view),
5096 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5097 const gchar *mark_name)
5100 GdkModifierType state;
5101 GtkTextIter newplace;
5103 /* DV(g_print (G_STRLOC": begin\n")); */
5105 gdk_window_get_pointer (text_view->text_window->bin_window,
5108 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5109 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5111 x + text_view->xoffset,
5112 y + text_view->yoffset);
5116 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5118 /* This may invalidate the layout */
5119 DV(g_print (G_STRLOC": move mark\n"));
5120 gtk_text_buffer_move_mark (get_buffer (text_view),
5124 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5125 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5128 DV (g_print ("first validate idle leaving %s is %d\n",
5129 G_STRLOC, text_view->first_validate_idle));
5133 selection_scan_timeout (gpointer data)
5135 GtkTextView *text_view;
5137 GDK_THREADS_ENTER ();
5139 text_view = GTK_TEXT_VIEW (data);
5141 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5142 move_mark_to_pointer_and_scroll (text_view, "insert");
5144 GDK_THREADS_LEAVE ();
5146 return TRUE; /* remain installed. */
5149 #define DND_SCROLL_MARGIN 0.20
5152 drag_scan_timeout (gpointer data)
5154 GtkTextView *text_view;
5156 GdkModifierType state;
5157 GtkTextIter newplace;
5159 GDK_THREADS_ENTER ();
5161 text_view = GTK_TEXT_VIEW (data);
5163 gdk_window_get_pointer (text_view->text_window->bin_window,
5166 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5168 x + text_view->xoffset,
5169 y + text_view->yoffset);
5171 gtk_text_buffer_move_mark (get_buffer (text_view),
5172 text_view->dnd_mark,
5175 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5176 gtk_text_view_scroll_to_mark (text_view,
5177 text_view->dnd_mark,
5178 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5180 GDK_THREADS_LEAVE ();
5186 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5188 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5189 move_mark_to_pointer_and_scroll (text_view, "insert");
5191 /* If we had to scroll offscreen, insert a timeout to do so
5192 * again. Note that in the timeout, even if the mouse doesn't
5193 * move, due to this scroll xoffset/yoffset will have changed
5194 * and we'll need to scroll again.
5196 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5197 gtk_timeout_remove (text_view->scroll_timeout);
5199 text_view->scroll_timeout =
5200 gtk_timeout_add (50, selection_scan_timeout, text_view);
5206 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5207 const GtkTextIter *iter,
5208 GdkEventButton *button)
5210 GtkTextIter newplace;
5211 GtkTextBuffer *buffer;
5213 g_return_if_fail (text_view->selection_drag_handler == 0);
5215 gtk_grab_add (GTK_WIDGET (text_view));
5217 buffer = get_buffer (text_view);
5221 if (button->state & GDK_SHIFT_MASK)
5223 /* Extend selection */
5224 GtkTextIter start, end;
5226 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5228 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5230 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5233 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5236 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5238 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5241 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5247 /* Replace selection */
5248 gtk_text_buffer_place_cursor (buffer, &newplace);
5251 text_view->selection_drag_handler = g_signal_connect (text_view,
5252 "motion_notify_event",
5253 G_CALLBACK (selection_motion_event_handler),
5257 /* returns whether we were really dragging */
5259 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5261 if (text_view->selection_drag_handler == 0)
5264 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
5265 text_view->selection_drag_handler = 0;
5267 if (text_view->scroll_timeout != 0)
5269 gtk_timeout_remove (text_view->scroll_timeout);
5270 text_view->scroll_timeout = 0;
5273 /* one last update to current position */
5274 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5275 move_mark_to_pointer_and_scroll (text_view, "insert");
5277 gtk_grab_remove (GTK_WIDGET (text_view));
5287 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5288 GtkTextAttributes *values,
5291 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5292 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5295 pango_font_description_free (values->font);
5297 values->font = pango_font_description_copy (style->font_desc);
5301 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5303 if (text_view->layout)
5305 gboolean split_cursor;
5306 GtkTextDirection new_dir;
5307 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5309 g_object_get (G_OBJECT (settings),
5310 "gtk-split-cursor", &split_cursor,
5314 new_dir = GTK_TEXT_DIR_NONE;
5318 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5319 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5320 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5323 if (text_view->layout->cursor_direction != new_dir)
5324 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5329 gtk_text_view_ensure_layout (GtkTextView *text_view)
5333 widget = GTK_WIDGET (text_view);
5335 if (text_view->layout == NULL)
5337 GtkTextAttributes *style;
5338 PangoContext *ltr_context, *rtl_context;
5341 DV(g_print(G_STRLOC"\n"));
5343 text_view->layout = gtk_text_layout_new ();
5345 g_signal_connect (text_view->layout,
5347 G_CALLBACK (invalidated_handler),
5350 g_signal_connect (text_view->layout,
5352 G_CALLBACK (changed_handler),
5355 g_signal_connect (text_view->layout,
5357 G_CALLBACK (gtk_text_view_child_allocated),
5360 if (get_buffer (text_view))
5361 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5363 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5364 gtk_text_view_pend_cursor_blink (text_view);
5366 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5368 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5369 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5370 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5371 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5373 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5375 g_object_unref (ltr_context);
5376 g_object_unref (rtl_context);
5378 gtk_text_view_check_keymap_direction (text_view);
5380 style = gtk_text_attributes_new ();
5382 gtk_widget_ensure_style (widget);
5383 gtk_text_view_set_attributes_from_style (text_view,
5384 style, widget->style);
5386 style->pixels_above_lines = text_view->pixels_above_lines;
5387 style->pixels_below_lines = text_view->pixels_below_lines;
5388 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5389 style->left_margin = text_view->left_margin;
5390 style->right_margin = text_view->right_margin;
5391 style->indent = text_view->indent;
5392 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5394 style->wrap_mode = text_view->wrap_mode;
5395 style->justification = text_view->justify;
5396 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5398 gtk_text_layout_set_default_style (text_view->layout, style);
5400 gtk_text_attributes_unref (style);
5402 /* Set layout for all anchored children */
5404 tmp_list = text_view->children;
5405 while (tmp_list != NULL)
5407 GtkTextViewChild *vc = tmp_list->data;
5411 gtk_text_anchored_child_set_layout (vc->widget,
5413 /* vc may now be invalid! */
5416 tmp_list = g_slist_next (tmp_list);
5419 gtk_text_view_invalidate (text_view);
5424 * gtk_text_view_get_default_attributes:
5425 * @text_view: a #GtkTextView
5427 * Obtains a copy of the default text attributes. These are the
5428 * attributes used for text unless a tag overrides them.
5429 * You'd typically pass the default attributes in to
5430 * gtk_text_iter_get_attributes() in order to get the
5431 * attributes in effect at a given text position.
5433 * The return value is a copy owned by the caller of this function,
5434 * and should be freed.
5436 * Return value: a new #GtkTextAttributes
5439 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5441 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5443 gtk_text_view_ensure_layout (text_view);
5445 return gtk_text_attributes_copy (text_view->layout->default_style);
5449 gtk_text_view_destroy_layout (GtkTextView *text_view)
5451 if (text_view->layout)
5455 gtk_text_view_remove_validate_idles (text_view);
5457 g_signal_handlers_disconnect_by_func (text_view->layout,
5458 invalidated_handler,
5460 g_signal_handlers_disconnect_by_func (text_view->layout,
5464 /* Remove layout from all anchored children */
5465 tmp_list = text_view->children;
5466 while (tmp_list != NULL)
5468 GtkTextViewChild *vc = tmp_list->data;
5472 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5473 /* vc may now be invalid! */
5476 tmp_list = g_slist_next (tmp_list);
5479 gtk_text_view_stop_cursor_blink (text_view);
5480 gtk_text_view_end_selection_drag (text_view, NULL);
5482 g_object_unref (text_view->layout);
5483 text_view->layout = NULL;
5488 gtk_text_view_reset_im_context (GtkTextView *text_view)
5490 if (text_view->need_im_reset)
5492 text_view->need_im_reset = FALSE;
5493 gtk_im_context_reset (text_view->im_context);
5502 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5503 const GtkTextIter *iter,
5504 GdkEventMotion *event)
5506 GdkDragContext *context;
5507 GtkTargetList *target_list;
5509 text_view->drag_start_x = -1;
5510 text_view->drag_start_y = -1;
5511 text_view->pending_place_cursor_button = 0;
5513 target_list = gtk_target_list_new (target_table,
5514 G_N_ELEMENTS (target_table));
5516 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5517 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5518 1, (GdkEvent*)event);
5520 gtk_target_list_unref (target_list);
5522 gtk_drag_set_icon_default (context);
5526 gtk_text_view_drag_begin (GtkWidget *widget,
5527 GdkDragContext *context)
5533 gtk_text_view_drag_end (GtkWidget *widget,
5534 GdkDragContext *context)
5536 GtkTextView *text_view;
5538 text_view = GTK_TEXT_VIEW (widget);
5542 gtk_text_view_drag_data_get (GtkWidget *widget,
5543 GdkDragContext *context,
5544 GtkSelectionData *selection_data,
5548 GtkTextView *text_view;
5550 text_view = GTK_TEXT_VIEW (widget);
5552 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5554 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5556 gtk_selection_data_set (selection_data,
5557 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5570 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5573 /* Extract the selected text */
5574 str = gtk_text_iter_get_visible_text (&start, &end);
5579 gtk_selection_data_set_text (selection_data, str, -1);
5586 gtk_text_view_drag_data_delete (GtkWidget *widget,
5587 GdkDragContext *context)
5589 GtkTextView *text_view;
5591 text_view = GTK_TEXT_VIEW (widget);
5593 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5594 TRUE, GTK_TEXT_VIEW (widget)->editable);
5598 gtk_text_view_drag_leave (GtkWidget *widget,
5599 GdkDragContext *context,
5602 GtkTextView *text_view;
5604 text_view = GTK_TEXT_VIEW (widget);
5606 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5608 if (text_view->scroll_timeout != 0)
5609 gtk_timeout_remove (text_view->scroll_timeout);
5611 text_view->scroll_timeout = 0;
5615 gtk_text_view_drag_motion (GtkWidget *widget,
5616 GdkDragContext *context,
5621 GtkTextIter newplace;
5622 GtkTextView *text_view;
5625 GdkRectangle target_rect;
5627 GdkDragAction suggested_action = 0;
5629 text_view = GTK_TEXT_VIEW (widget);
5631 target_rect = text_view->text_window->allocation;
5633 if (x < target_rect.x ||
5634 y < target_rect.y ||
5635 x > (target_rect.x + target_rect.width) ||
5636 y > (target_rect.y + target_rect.height))
5637 return FALSE; /* outside the text window, allow parent widgets to handle event */
5639 gtk_text_view_window_to_buffer_coords (text_view,
5640 GTK_TEXT_WINDOW_WIDGET,
5644 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5648 if (gtk_drag_dest_find_target (widget, context,
5649 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5651 /* can't accept any of the offered targets */
5653 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5655 gtk_text_iter_compare (&newplace, &start) >= 0 &&
5656 gtk_text_iter_compare (&newplace, &end) <= 0)
5658 /* We're inside the selection. */
5662 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5664 GtkWidget *source_widget;
5666 suggested_action = context->suggested_action;
5668 source_widget = gtk_drag_get_source_widget (context);
5670 if (source_widget == widget)
5672 /* Default to MOVE, unless the user has
5673 * pressed ctrl or alt to affect available actions
5675 if ((context->actions & GDK_ACTION_MOVE) != 0)
5676 suggested_action = GDK_ACTION_MOVE;
5681 /* Can't drop here. */
5685 if (suggested_action != 0)
5687 gtk_text_mark_set_visible (text_view->dnd_mark,
5688 text_view->cursor_visible);
5690 gdk_drag_status (context, suggested_action, time);
5694 gdk_drag_status (context, 0, time);
5695 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5698 gtk_text_buffer_move_mark (get_buffer (text_view),
5699 text_view->dnd_mark,
5702 DV(g_print (G_STRLOC": scrolling to mark\n"));
5703 gtk_text_view_scroll_to_mark (text_view,
5704 text_view->dnd_mark,
5705 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5707 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5708 gtk_timeout_remove (text_view->scroll_timeout);
5710 text_view->scroll_timeout =
5711 gtk_timeout_add (50, drag_scan_timeout, text_view);
5713 /* TRUE return means don't propagate the drag motion to parent
5714 * widgets that may also be drop sites.
5720 gtk_text_view_drag_drop (GtkWidget *widget,
5721 GdkDragContext *context,
5726 GtkTextView *text_view;
5727 GtkTextIter drop_point;
5728 GdkAtom target = GDK_NONE;
5730 text_view = GTK_TEXT_VIEW (widget);
5732 if (text_view->scroll_timeout != 0)
5733 gtk_timeout_remove (text_view->scroll_timeout);
5735 text_view->scroll_timeout = 0;
5737 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5739 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5741 text_view->dnd_mark);
5743 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5744 target = gtk_drag_dest_find_target (widget, context, NULL);
5746 if (target != GDK_NONE)
5747 gtk_drag_get_data (widget, context, target, time);
5749 gtk_drag_finish (context, FALSE, FALSE, time);
5755 insert_text_data (GtkTextView *text_view,
5756 GtkTextIter *drop_point,
5757 GtkSelectionData *selection_data)
5761 str = gtk_selection_data_get_text (selection_data);
5765 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5766 drop_point, str, -1,
5767 text_view->editable);
5773 gtk_text_view_drag_data_received (GtkWidget *widget,
5774 GdkDragContext *context,
5777 GtkSelectionData *selection_data,
5781 GtkTextIter drop_point;
5782 GtkTextView *text_view;
5783 gboolean success = FALSE;
5785 text_view = GTK_TEXT_VIEW (widget);
5787 if (!text_view->dnd_mark)
5790 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5792 text_view->dnd_mark);
5794 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5797 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5799 GtkTextBuffer *src_buffer = NULL;
5800 GtkTextIter start, end;
5801 gboolean copy_tags = TRUE;
5803 if (selection_data->length != sizeof (src_buffer))
5806 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5808 if (src_buffer == NULL)
5811 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5813 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5814 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5817 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5822 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5826 text_view->editable);
5831 str = gtk_text_iter_get_visible_text (&start, &end);
5832 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5833 &drop_point, str, -1,
5834 text_view->editable);
5840 insert_text_data (text_view, &drop_point, selection_data);
5842 gtk_text_buffer_place_cursor (get_buffer (text_view), &drop_point);
5847 gtk_drag_finish (context, success,
5848 success && context->action == GDK_ACTION_MOVE,
5852 static GtkAdjustment*
5853 get_hadjustment (GtkTextView *text_view)
5855 if (text_view->hadjustment == NULL)
5856 gtk_text_view_set_scroll_adjustments (text_view,
5857 NULL, /* forces creation */
5858 text_view->vadjustment);
5860 return text_view->hadjustment;
5863 static GtkAdjustment*
5864 get_vadjustment (GtkTextView *text_view)
5866 if (text_view->vadjustment == NULL)
5867 gtk_text_view_set_scroll_adjustments (text_view,
5868 text_view->hadjustment,
5869 NULL); /* forces creation */
5870 return text_view->vadjustment;
5875 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5876 GtkAdjustment *hadj,
5877 GtkAdjustment *vadj)
5879 gboolean need_adjust = FALSE;
5881 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5884 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5886 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5888 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5890 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5892 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5894 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
5895 gtk_text_view_value_changed,
5897 g_object_unref (text_view->hadjustment);
5900 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5902 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
5903 gtk_text_view_value_changed,
5905 g_object_unref (text_view->vadjustment);
5908 if (text_view->hadjustment != hadj)
5910 text_view->hadjustment = hadj;
5911 g_object_ref (text_view->hadjustment);
5912 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5914 g_signal_connect (text_view->hadjustment, "value_changed",
5915 G_CALLBACK (gtk_text_view_value_changed),
5920 if (text_view->vadjustment != vadj)
5922 text_view->vadjustment = vadj;
5923 g_object_ref (text_view->vadjustment);
5924 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5926 g_signal_connect (text_view->vadjustment, "value_changed",
5927 G_CALLBACK (gtk_text_view_value_changed),
5933 gtk_text_view_value_changed (NULL, text_view);
5936 /* FIXME this adjust_allocation is a big cut-and-paste from
5937 * GtkCList, needs to be some "official" way to do this
5947 /* The window to which widget->window is relative */
5948 #define ALLOCATION_WINDOW(widget) \
5949 (GTK_WIDGET_NO_WINDOW (widget) ? \
5950 (widget)->window : \
5951 gdk_window_get_parent ((widget)->window))
5954 adjust_allocation_recurse (GtkWidget *widget,
5957 ScrollData *scroll_data = data;
5959 /* Need to really size allocate instead of just poking
5960 * into widget->allocation if the widget is not realized.
5961 * FIXME someone figure out why this was.
5963 if (!GTK_WIDGET_REALIZED (widget))
5965 if (GTK_WIDGET_VISIBLE (widget))
5967 GdkRectangle tmp_rectangle = widget->allocation;
5968 tmp_rectangle.x += scroll_data->dx;
5969 tmp_rectangle.y += scroll_data->dy;
5971 gtk_widget_size_allocate (widget, &tmp_rectangle);
5976 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5978 widget->allocation.x += scroll_data->dx;
5979 widget->allocation.y += scroll_data->dy;
5981 if (GTK_IS_CONTAINER (widget))
5982 gtk_container_forall (GTK_CONTAINER (widget),
5983 adjust_allocation_recurse,
5990 adjust_allocation (GtkWidget *widget,
5994 ScrollData scroll_data;
5996 if (GTK_WIDGET_REALIZED (widget))
5997 scroll_data.window = ALLOCATION_WINDOW (widget);
5999 scroll_data.window = NULL;
6001 scroll_data.dx = dx;
6002 scroll_data.dy = dy;
6004 adjust_allocation_recurse (widget, &scroll_data);
6008 gtk_text_view_value_changed (GtkAdjustment *adj,
6009 GtkTextView *text_view)
6016 /* Note that we oddly call this function with adj == NULL
6020 text_view->onscreen_validated = FALSE;
6022 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
6023 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
6024 adj ? adj->value : 0.0));
6026 if (adj == text_view->hadjustment)
6028 dx = text_view->xoffset - (gint)adj->value;
6029 text_view->xoffset = adj->value;
6031 else if (adj == text_view->vadjustment)
6033 dy = text_view->yoffset - (gint)adj->value;
6034 text_view->yoffset = adj->value;
6036 if (text_view->layout)
6038 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
6040 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
6042 text_view->first_para_pixels = adj->value - line_top;
6046 if (dx != 0 || dy != 0)
6050 if (GTK_WIDGET_REALIZED (text_view))
6054 if (text_view->left_window)
6055 text_window_scroll (text_view->left_window, 0, dy);
6056 if (text_view->right_window)
6057 text_window_scroll (text_view->right_window, 0, dy);
6062 if (text_view->top_window)
6063 text_window_scroll (text_view->top_window, dx, 0);
6064 if (text_view->bottom_window)
6065 text_window_scroll (text_view->bottom_window, dx, 0);
6068 /* It looks nicer to scroll the main area last, because
6069 * it takes a while, and making the side areas update
6070 * afterward emphasizes the slowness of scrolling the
6073 text_window_scroll (text_view->text_window, dx, dy);
6076 /* Children are now "moved" in the text window, poke
6077 * into widget->allocation for each child
6079 tmp_list = text_view->children;
6080 while (tmp_list != NULL)
6082 GtkTextViewChild *child = tmp_list->data;
6085 adjust_allocation (child->widget, dx, dy);
6087 tmp_list = g_slist_next (tmp_list);
6091 /* This could result in invalidation, which would install the
6092 * first_validate_idle, which would validate onscreen;
6093 * but we're going to go ahead and validate here, so
6094 * first_validate_idle shouldn't have anything to do.
6096 gtk_text_view_update_layout_width (text_view);
6098 /* note that validation of onscreen could invoke this function
6099 * recursively, by scrolling to maintain first_para, or in response
6100 * to updating the layout width, however there is no problem with
6101 * that, or shouldn't be.
6103 gtk_text_view_validate_onscreen (text_view);
6105 /* process exposes */
6106 if (GTK_WIDGET_REALIZED (text_view))
6108 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6110 if (text_view->left_window)
6111 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6113 if (text_view->right_window)
6114 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6116 if (text_view->top_window)
6117 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6119 if (text_view->bottom_window)
6120 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6122 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6125 /* If this got installed, get rid of it, it's just a waste of time. */
6126 if (text_view->first_validate_idle != 0)
6128 g_source_remove (text_view->first_validate_idle);
6129 text_view->first_validate_idle = 0;
6132 gtk_text_view_update_im_spot_location (text_view);
6134 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6138 gtk_text_view_commit_handler (GtkIMContext *context,
6140 GtkTextView *text_view)
6142 gtk_text_view_commit_text (text_view, str);
6146 gtk_text_view_commit_text (GtkTextView *text_view,
6149 gboolean had_selection;
6151 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6153 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6156 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6157 text_view->editable);
6159 if (!strcmp (str, "\n"))
6161 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6162 text_view->editable);
6166 if (!had_selection && text_view->overwrite_mode)
6170 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6172 gtk_text_buffer_get_mark (get_buffer (text_view),
6174 if (!gtk_text_iter_ends_line (&insert))
6175 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6177 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6178 text_view->editable);
6181 gtk_text_buffer_end_user_action (get_buffer (text_view));
6183 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6184 gtk_text_view_scroll_mark_onscreen (text_view,
6185 gtk_text_buffer_get_mark (get_buffer (text_view),
6190 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6191 GtkTextView *text_view)
6194 PangoAttrList *attrs;
6197 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6198 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6199 pango_attr_list_unref (attrs);
6202 gtk_text_view_scroll_mark_onscreen (text_view,
6203 gtk_text_buffer_get_mark (get_buffer (text_view),
6208 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6209 GtkTextView *text_view)
6216 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6217 gtk_text_buffer_get_insert (text_view->buffer));
6220 pos = gtk_text_iter_get_line_index (&start);
6221 gtk_text_iter_set_line_offset (&start, 0);
6222 gtk_text_iter_forward_to_line_end (&end);
6224 text = gtk_text_iter_get_slice (&start, &end);
6225 gtk_im_context_set_surrounding (context, text, -1, pos);
6232 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6235 GtkTextView *text_view)
6240 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6241 gtk_text_buffer_get_insert (text_view->buffer));
6244 gtk_text_iter_forward_chars (&start, offset);
6245 gtk_text_iter_forward_chars (&end, offset + n_chars);
6247 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6253 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6254 const GtkTextIter *location,
6258 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6259 gboolean need_reset = FALSE;
6261 if (mark == gtk_text_buffer_get_insert (buffer))
6263 text_view->virtual_cursor_x = -1;
6264 text_view->virtual_cursor_y = -1;
6265 gtk_text_view_update_im_spot_location (text_view);
6268 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6274 gtk_text_view_reset_im_context (text_view);
6278 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6282 GdkRectangle strong_pos;
6285 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6286 gtk_text_buffer_get_mark (get_buffer (text_view),
6289 if ((x && text_view->virtual_cursor_x == -1) ||
6290 (y && text_view->virtual_cursor_y == -1))
6291 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6295 if (text_view->virtual_cursor_x != -1)
6296 *x = text_view->virtual_cursor_x;
6303 if (text_view->virtual_cursor_x != -1)
6304 *y = text_view->virtual_cursor_y;
6306 *y = strong_pos.y + strong_pos.height / 2;
6311 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6315 GdkRectangle strong_pos;
6318 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6319 gtk_text_buffer_get_mark (get_buffer (text_view),
6322 if (x == -1 || y == -1)
6323 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6325 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6326 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6329 /* Quick hack of a popup menu
6332 activate_cb (GtkWidget *menuitem,
6333 GtkTextView *text_view)
6335 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6336 g_signal_emit_by_name (text_view, signal);
6340 append_action_signal (GtkTextView *text_view,
6342 const gchar *stock_id,
6343 const gchar *signal,
6346 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6348 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6349 g_signal_connect (menuitem, "activate",
6350 G_CALLBACK (activate_cb), text_view);
6352 gtk_widget_set_sensitive (menuitem, sensitive);
6354 gtk_widget_show (menuitem);
6355 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6359 popup_menu_detach (GtkWidget *attach_widget,
6362 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6366 popup_position_func (GtkMenu *menu,
6372 GtkTextView *text_view;
6374 GdkRectangle cursor_rect;
6375 GdkRectangle onscreen_rect;
6376 gint root_x, root_y;
6381 text_view = GTK_TEXT_VIEW (user_data);
6382 widget = GTK_WIDGET (text_view);
6384 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6386 screen = gtk_widget_get_screen (widget);
6388 gdk_window_get_origin (widget->window, &root_x, &root_y);
6390 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6392 gtk_text_buffer_get_insert (get_buffer (text_view)));
6394 gtk_text_view_get_iter_location (text_view,
6398 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6400 gtk_widget_size_request (text_view->popup_menu, &req);
6402 /* can't use rectangle_intersect since cursor rect can have 0 width */
6403 if (cursor_rect.x >= onscreen_rect.x &&
6404 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6405 cursor_rect.y >= onscreen_rect.y &&
6406 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6408 gtk_text_view_buffer_to_window_coords (text_view,
6409 GTK_TEXT_WINDOW_WIDGET,
6410 cursor_rect.x, cursor_rect.y,
6411 &cursor_rect.x, &cursor_rect.y);
6413 *x = root_x + cursor_rect.x + cursor_rect.width;
6414 *y = root_y + cursor_rect.y + cursor_rect.height;
6418 /* Just center the menu, since cursor is offscreen. */
6419 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6420 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6424 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6425 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6427 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6428 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6433 GtkTextView *text_view;
6439 range_contains_editable_text (const GtkTextIter *start,
6440 const GtkTextIter *end,
6441 gboolean default_editability)
6443 GtkTextIter iter = *start;
6445 while (gtk_text_iter_compare (&iter, end) < 0)
6447 if (gtk_text_iter_editable (&iter, default_editability))
6450 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6457 unichar_chosen_func (const char *text,
6460 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6462 gtk_text_view_commit_text (text_view, text);
6466 popup_targets_received (GtkClipboard *clipboard,
6467 GtkSelectionData *data,
6470 PopupInfo *info = user_data;
6471 GtkTextView *text_view = info->text_view;
6473 if (GTK_WIDGET_REALIZED (text_view))
6475 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6476 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6478 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6479 GtkWidget *menuitem;
6481 gboolean have_selection;
6482 gboolean can_insert;
6484 GtkTextIter sel_start, sel_end;
6486 if (text_view->popup_menu)
6487 gtk_widget_destroy (text_view->popup_menu);
6489 text_view->popup_menu = gtk_menu_new ();
6491 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6492 GTK_WIDGET (text_view),
6495 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6496 &sel_start, &sel_end);
6498 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6500 gtk_text_buffer_get_insert (get_buffer (text_view)));
6502 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6504 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6506 range_contains_editable_text (&sel_start, &sel_end,
6507 text_view->editable));
6508 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6510 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6511 can_insert && clipboard_contains_text);
6513 menuitem = gtk_separator_menu_item_new ();
6514 gtk_widget_show (menuitem);
6515 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6517 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6518 gtk_widget_show (menuitem);
6519 submenu = gtk_menu_new ();
6520 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6521 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6523 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6524 GTK_MENU_SHELL (submenu));
6526 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6527 gtk_widget_show (menuitem);
6528 gtk_widget_set_sensitive (menuitem, can_insert);
6530 submenu = gtk_menu_new ();
6531 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6532 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6534 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6535 unichar_chosen_func,
6538 g_signal_emit (text_view,
6539 signals[POPULATE_POPUP],
6541 text_view->popup_menu);
6544 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6546 info->button, info->time);
6549 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6550 popup_position_func, text_view,
6551 0, gtk_get_current_event_time ());
6552 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
6556 g_object_unref (text_view);
6561 gtk_text_view_do_popup (GtkTextView *text_view,
6562 GdkEventButton *event)
6564 PopupInfo *info = g_new (PopupInfo, 1);
6566 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6567 gtk_text_view_end_selection_drag (text_view, event);
6569 /* In order to know what entries we should make sensitive, we
6570 * ask for the current targets of the clipboard, and when
6571 * we get them, then we actually pop up the menu.
6573 info->text_view = g_object_ref (text_view);
6577 info->button = event->button;
6578 info->time = event->time;
6583 info->time = gtk_get_current_event_time ();
6586 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6587 GDK_SELECTION_CLIPBOARD),
6588 gdk_atom_intern ("TARGETS", FALSE),
6589 popup_targets_received,
6594 gtk_text_view_popup_menu (GtkWidget *widget)
6596 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6600 /* Child GdkWindows */
6603 static GtkTextWindow*
6604 text_window_new (GtkTextWindowType type,
6607 gint height_request)
6611 win = g_new (GtkTextWindow, 1);
6614 win->widget = widget;
6616 win->bin_window = NULL;
6617 win->requisition.width = width_request;
6618 win->requisition.height = height_request;
6619 win->allocation.width = width_request;
6620 win->allocation.height = height_request;
6621 win->allocation.x = 0;
6622 win->allocation.y = 0;
6628 text_window_free (GtkTextWindow *win)
6631 text_window_unrealize (win);
6637 text_window_realize (GtkTextWindow *win,
6640 GdkWindowAttr attributes;
6641 gint attributes_mask;
6644 attributes.window_type = GDK_WINDOW_CHILD;
6645 attributes.x = win->allocation.x;
6646 attributes.y = win->allocation.y;
6647 attributes.width = win->allocation.width;
6648 attributes.height = win->allocation.height;
6649 attributes.wclass = GDK_INPUT_OUTPUT;
6650 attributes.visual = gtk_widget_get_visual (win->widget);
6651 attributes.colormap = gtk_widget_get_colormap (win->widget);
6652 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6654 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6656 win->window = gdk_window_new (parent,
6660 gdk_window_show (win->window);
6661 gdk_window_set_user_data (win->window, win->widget);
6665 attributes.width = win->allocation.width;
6666 attributes.height = win->allocation.height;
6667 attributes.event_mask = (GDK_EXPOSURE_MASK |
6669 GDK_KEY_PRESS_MASK |
6670 GDK_BUTTON_PRESS_MASK |
6671 GDK_BUTTON_RELEASE_MASK |
6672 GDK_POINTER_MOTION_MASK |
6673 GDK_POINTER_MOTION_HINT_MASK |
6674 gtk_widget_get_events (win->widget));
6676 win->bin_window = gdk_window_new (win->window,
6680 gdk_window_show (win->bin_window);
6681 gdk_window_set_user_data (win->bin_window, win->widget);
6683 if (win->type == GTK_TEXT_WINDOW_TEXT)
6686 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
6688 gdk_window_set_cursor (win->bin_window, cursor);
6689 gdk_cursor_unref (cursor);
6691 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6695 gdk_window_set_background (win->bin_window,
6696 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6700 gdk_window_set_background (win->bin_window,
6701 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6704 g_object_set_qdata (G_OBJECT (win->window),
6705 g_quark_from_static_string ("gtk-text-view-text-window"),
6708 g_object_set_qdata (G_OBJECT (win->bin_window),
6709 g_quark_from_static_string ("gtk-text-view-text-window"),
6714 text_window_unrealize (GtkTextWindow *win)
6716 if (win->type == GTK_TEXT_WINDOW_TEXT)
6718 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6722 gdk_window_set_user_data (win->window, NULL);
6723 gdk_window_set_user_data (win->bin_window, NULL);
6724 gdk_window_destroy (win->bin_window);
6725 gdk_window_destroy (win->window);
6727 win->bin_window = NULL;
6731 text_window_size_allocate (GtkTextWindow *win,
6734 win->allocation = *rect;
6738 gdk_window_move_resize (win->window,
6740 rect->width, rect->height);
6742 gdk_window_resize (win->bin_window,
6743 rect->width, rect->height);
6748 text_window_scroll (GtkTextWindow *win,
6752 if (dx != 0 || dy != 0)
6754 gdk_window_scroll (win->bin_window, dx, dy);
6759 text_window_invalidate_rect (GtkTextWindow *win,
6762 GdkRectangle window_rect;
6764 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6771 window_rect.width = rect->width;
6772 window_rect.height = rect->height;
6774 /* Adjust the rect as appropriate */
6778 case GTK_TEXT_WINDOW_TEXT:
6781 case GTK_TEXT_WINDOW_LEFT:
6782 case GTK_TEXT_WINDOW_RIGHT:
6784 window_rect.width = win->allocation.width;
6787 case GTK_TEXT_WINDOW_TOP:
6788 case GTK_TEXT_WINDOW_BOTTOM:
6790 window_rect.height = win->allocation.height;
6794 g_warning ("%s: bug!", G_STRLOC);
6799 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6803 GdkColor color = { 0, 65535, 0, 0 };
6804 GdkGC *gc = gdk_gc_new (win->bin_window);
6805 gdk_gc_set_rgb_fg_color (gc, &color);
6806 gdk_draw_rectangle (win->bin_window,
6807 gc, TRUE, window_rect.x, window_rect.y,
6808 window_rect.width, window_rect.height);
6809 g_object_unref (gc);
6815 text_window_get_width (GtkTextWindow *win)
6817 return win->allocation.width;
6821 text_window_get_height (GtkTextWindow *win)
6823 return win->allocation.height;
6830 * gtk_text_view_get_window:
6831 * @text_view: a #GtkTextView
6832 * @win: window to get
6834 * Retrieves the #GdkWindow corresponding to an area of the text view;
6835 * possible windows include the overall widget window, child windows
6836 * on the left, right, top, bottom, and the window that displays the
6837 * text buffer. Windows are %NULL and nonexistent if their width or
6838 * height is 0, and are nonexistent before the widget has been
6841 * Return value: a #GdkWindow, or %NULL
6844 gtk_text_view_get_window (GtkTextView *text_view,
6845 GtkTextWindowType win)
6847 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6851 case GTK_TEXT_WINDOW_WIDGET:
6852 return GTK_WIDGET (text_view)->window;
6855 case GTK_TEXT_WINDOW_TEXT:
6856 return text_view->text_window->bin_window;
6859 case GTK_TEXT_WINDOW_LEFT:
6860 if (text_view->left_window)
6861 return text_view->left_window->bin_window;
6866 case GTK_TEXT_WINDOW_RIGHT:
6867 if (text_view->right_window)
6868 return text_view->right_window->bin_window;
6873 case GTK_TEXT_WINDOW_TOP:
6874 if (text_view->top_window)
6875 return text_view->top_window->bin_window;
6880 case GTK_TEXT_WINDOW_BOTTOM:
6881 if (text_view->bottom_window)
6882 return text_view->bottom_window->bin_window;
6887 case GTK_TEXT_WINDOW_PRIVATE:
6888 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6893 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6898 * gtk_text_view_get_window_type:
6899 * @text_view: a #GtkTextView
6900 * @window: a window type
6902 * Usually used to find out which window an event corresponds to.
6903 * If you connect to an event signal on @text_view, this function
6904 * should be called on <literal>event->window</literal> to
6905 * see which window it was.
6907 * Return value: the window type.
6910 gtk_text_view_get_window_type (GtkTextView *text_view,
6915 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6916 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6918 if (window == GTK_WIDGET (text_view)->window)
6919 return GTK_TEXT_WINDOW_WIDGET;
6921 win = g_object_get_qdata (G_OBJECT (window),
6922 g_quark_try_string ("gtk-text-view-text-window"));
6928 return GTK_TEXT_WINDOW_PRIVATE;
6933 buffer_to_widget (GtkTextView *text_view,
6941 *window_x = buffer_x - text_view->xoffset;
6942 *window_x += text_view->text_window->allocation.x;
6947 *window_y = buffer_y - text_view->yoffset;
6948 *window_y += text_view->text_window->allocation.y;
6953 widget_to_text_window (GtkTextWindow *win,
6960 *window_x = widget_x - win->allocation.x;
6963 *window_y = widget_y - win->allocation.y;
6967 buffer_to_text_window (GtkTextView *text_view,
6976 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6977 "for a nonexistent or private child window of GtkTextView");
6981 buffer_to_widget (text_view,
6983 window_x, window_y);
6985 widget_to_text_window (win,
6986 window_x ? *window_x : 0,
6987 window_y ? *window_y : 0,
6993 * gtk_text_view_buffer_to_window_coords:
6994 * @text_view: a #GtkTextView
6995 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6996 * @buffer_x: buffer x coordinate
6997 * @buffer_y: buffer y coordinate
6998 * @window_x: window x coordinate return location
6999 * @window_y: window y coordinate return location
7001 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
7002 * @win, and stores the result in (@window_x, @window_y).
7004 * Note that you can't convert coordinates for a nonexisting window (see
7005 * gtk_text_view_set_border_window_size()).
7008 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
7009 GtkTextWindowType win,
7015 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7019 case GTK_TEXT_WINDOW_WIDGET:
7020 buffer_to_widget (text_view,
7022 window_x, window_y);
7025 case GTK_TEXT_WINDOW_TEXT:
7027 *window_x = buffer_x - text_view->xoffset;
7029 *window_y = buffer_y - text_view->yoffset;
7032 case GTK_TEXT_WINDOW_LEFT:
7033 buffer_to_text_window (text_view,
7034 text_view->left_window,
7036 window_x, window_y);
7039 case GTK_TEXT_WINDOW_RIGHT:
7040 buffer_to_text_window (text_view,
7041 text_view->right_window,
7043 window_x, window_y);
7046 case GTK_TEXT_WINDOW_TOP:
7047 buffer_to_text_window (text_view,
7048 text_view->top_window,
7050 window_x, window_y);
7053 case GTK_TEXT_WINDOW_BOTTOM:
7054 buffer_to_text_window (text_view,
7055 text_view->bottom_window,
7057 window_x, window_y);
7060 case GTK_TEXT_WINDOW_PRIVATE:
7061 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7065 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7071 widget_to_buffer (GtkTextView *text_view,
7079 *buffer_x = widget_x + text_view->xoffset;
7080 *buffer_x -= text_view->text_window->allocation.x;
7085 *buffer_y = widget_y + text_view->yoffset;
7086 *buffer_y -= text_view->text_window->allocation.y;
7091 text_window_to_widget (GtkTextWindow *win,
7098 *widget_x = window_x + win->allocation.x;
7101 *widget_y = window_y + win->allocation.y;
7105 text_window_to_buffer (GtkTextView *text_view,
7114 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7115 "coordinates for a nonexistent child window.");
7119 text_window_to_widget (win,
7125 widget_to_buffer (text_view,
7126 buffer_x ? *buffer_x : 0,
7127 buffer_y ? *buffer_y : 0,
7133 * gtk_text_view_window_to_buffer_coords:
7134 * @text_view: a #GtkTextView
7135 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7136 * @window_x: window x coordinate
7137 * @window_y: window y coordinate
7138 * @buffer_x: buffer x coordinate return location
7139 * @buffer_y: buffer y coordinate return location
7141 * Converts coordinates on the window identified by @win to buffer
7142 * coordinates, storing the result in (@buffer_x,@buffer_y).
7144 * Note that you can't convert coordinates for a nonexisting window (see
7145 * gtk_text_view_set_border_window_size()).
7148 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7149 GtkTextWindowType win,
7155 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7159 case GTK_TEXT_WINDOW_WIDGET:
7160 widget_to_buffer (text_view,
7162 buffer_x, buffer_y);
7165 case GTK_TEXT_WINDOW_TEXT:
7167 *buffer_x = window_x + text_view->xoffset;
7169 *buffer_y = window_y + text_view->yoffset;
7172 case GTK_TEXT_WINDOW_LEFT:
7173 text_window_to_buffer (text_view,
7174 text_view->left_window,
7176 buffer_x, buffer_y);
7179 case GTK_TEXT_WINDOW_RIGHT:
7180 text_window_to_buffer (text_view,
7181 text_view->right_window,
7183 buffer_x, buffer_y);
7186 case GTK_TEXT_WINDOW_TOP:
7187 text_window_to_buffer (text_view,
7188 text_view->top_window,
7190 buffer_x, buffer_y);
7193 case GTK_TEXT_WINDOW_BOTTOM:
7194 text_window_to_buffer (text_view,
7195 text_view->bottom_window,
7197 buffer_x, buffer_y);
7200 case GTK_TEXT_WINDOW_PRIVATE:
7201 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7205 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7211 set_window_width (GtkTextView *text_view,
7213 GtkTextWindowType type,
7214 GtkTextWindow **winp)
7220 text_window_free (*winp);
7222 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7229 *winp = text_window_new (type,
7230 GTK_WIDGET (text_view),
7232 /* if the widget is already realized we need to realize the child manually */
7233 if (GTK_WIDGET_REALIZED (text_view))
7234 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7238 if ((*winp)->requisition.width == width)
7241 (*winp)->requisition.width = width;
7244 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7250 set_window_height (GtkTextView *text_view,
7252 GtkTextWindowType type,
7253 GtkTextWindow **winp)
7259 text_window_free (*winp);
7261 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7268 *winp = text_window_new (type,
7269 GTK_WIDGET (text_view),
7272 /* if the widget is already realized we need to realize the child manually */
7273 if (GTK_WIDGET_REALIZED (text_view))
7274 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7278 if ((*winp)->requisition.height == height)
7281 (*winp)->requisition.height = height;
7284 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7289 * gtk_text_view_set_border_window_size:
7290 * @text_view: a #GtkTextView
7291 * @type: window to affect
7292 * @size: width or height of the window
7294 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7295 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7296 * Automatically destroys the corresponding window if the size is set
7297 * to 0, and creates the window if the size is set to non-zero. This
7298 * function can only be used for the "border windows," it doesn't work
7299 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7300 * #GTK_TEXT_WINDOW_PRIVATE.
7303 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7304 GtkTextWindowType type,
7308 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7309 g_return_if_fail (size >= 0);
7313 case GTK_TEXT_WINDOW_LEFT:
7314 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7315 &text_view->left_window);
7318 case GTK_TEXT_WINDOW_RIGHT:
7319 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7320 &text_view->right_window);
7323 case GTK_TEXT_WINDOW_TOP:
7324 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7325 &text_view->top_window);
7328 case GTK_TEXT_WINDOW_BOTTOM:
7329 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7330 &text_view->bottom_window);
7334 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7340 * gtk_text_view_get_border_window_size:
7341 * @text_view: a #GtkTextView
7342 * @type: window to return size from
7344 * Gets the width of the specified border window. See
7345 * gtk_text_view_set_border_window_size().
7347 * Return value: width of window
7350 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7351 GtkTextWindowType type)
7353 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7357 case GTK_TEXT_WINDOW_LEFT:
7358 if (text_view->left_window)
7359 return text_view->left_window->requisition.width;
7361 case GTK_TEXT_WINDOW_RIGHT:
7362 if (text_view->right_window)
7363 return text_view->right_window->requisition.width;
7365 case GTK_TEXT_WINDOW_TOP:
7366 if (text_view->top_window)
7367 return text_view->top_window->requisition.height;
7369 case GTK_TEXT_WINDOW_BOTTOM:
7370 if (text_view->bottom_window)
7371 return text_view->bottom_window->requisition.height;
7374 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7385 static GtkTextViewChild*
7386 text_view_child_new_anchored (GtkWidget *child,
7387 GtkTextChildAnchor *anchor,
7388 GtkTextLayout *layout)
7390 GtkTextViewChild *vc;
7392 vc = g_new (GtkTextViewChild, 1);
7394 vc->type = GTK_TEXT_WINDOW_PRIVATE;
7396 vc->anchor = anchor;
7398 vc->from_top_of_line = 0;
7399 vc->from_left_of_buffer = 0;
7401 g_object_ref (vc->widget);
7402 g_object_ref (vc->anchor);
7404 g_object_set_data (G_OBJECT (child),
7405 "gtk-text-view-child",
7408 gtk_text_child_anchor_register_child (anchor, child, layout);
7413 static GtkTextViewChild*
7414 text_view_child_new_window (GtkWidget *child,
7415 GtkTextWindowType type,
7419 GtkTextViewChild *vc;
7421 vc = g_new (GtkTextViewChild, 1);
7426 vc->from_top_of_line = 0;
7427 vc->from_left_of_buffer = 0;
7429 g_object_ref (vc->widget);
7435 g_object_set_data (G_OBJECT (child),
7436 "gtk-text-view-child",
7443 text_view_child_free (GtkTextViewChild *child)
7445 g_object_set_data (G_OBJECT (child->widget),
7446 "gtk-text-view-child", NULL);
7450 gtk_text_child_anchor_unregister_child (child->anchor,
7452 g_object_unref (child->anchor);
7455 g_object_unref (child->widget);
7461 text_view_child_set_parent_window (GtkTextView *text_view,
7462 GtkTextViewChild *vc)
7465 gtk_widget_set_parent_window (vc->widget,
7466 text_view->text_window->bin_window);
7470 window = gtk_text_view_get_window (text_view,
7472 gtk_widget_set_parent_window (vc->widget, window);
7477 add_child (GtkTextView *text_view,
7478 GtkTextViewChild *vc)
7480 text_view->children = g_slist_prepend (text_view->children,
7483 if (GTK_WIDGET_REALIZED (text_view))
7484 text_view_child_set_parent_window (text_view, vc);
7486 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7490 * gtk_text_view_add_child_at_anchor:
7491 * @text_view: a #GtkTextView
7492 * @child: a #GtkWidget
7493 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7495 * Adds a child widget in the text buffer, at the given @anchor.
7499 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7501 GtkTextChildAnchor *anchor)
7503 GtkTextViewChild *vc;
7505 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7506 g_return_if_fail (GTK_IS_WIDGET (child));
7507 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7508 g_return_if_fail (child->parent == NULL);
7510 gtk_text_view_ensure_layout (text_view);
7512 vc = text_view_child_new_anchored (child, anchor,
7515 add_child (text_view, vc);
7517 g_assert (vc->widget == child);
7518 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7522 * gtk_text_view_add_child_in_window:
7523 * @text_view: a #GtkTextView
7524 * @child: a #GtkWidget
7525 * @which_window: which window the child should appear in
7526 * @xpos: X position of child in window coordinates
7527 * @ypos: Y position of child in window coordinates
7529 * Adds a child at fixed coordinates in one of the text widget's
7530 * windows. The window must have nonzero size (see
7531 * gtk_text_view_set_border_window_size()). Note that the child
7532 * coordinates are given relative to the #GdkWindow in question, and
7533 * that these coordinates have no sane relationship to scrolling. When
7534 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7535 * irrelevant, the child floats above all scrollable areas. But when
7536 * placing a child in one of the scrollable windows (border windows or
7537 * text window), you'll need to compute the child's correct position
7538 * in buffer coordinates any time scrolling occurs or buffer changes
7539 * occur, and then call gtk_text_view_move_child() to update the
7540 * child's position. Unfortunately there's no good way to detect that
7541 * scrolling has occurred, using the current API; a possible hack
7542 * would be to update all child positions when the scroll adjustments
7543 * change or the text buffer changes. See bug 64518 on
7544 * bugzilla.gnome.org for status of fixing this issue.
7548 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7550 GtkTextWindowType which_window,
7554 GtkTextViewChild *vc;
7556 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7557 g_return_if_fail (GTK_IS_WIDGET (child));
7558 g_return_if_fail (child->parent == NULL);
7560 vc = text_view_child_new_window (child, which_window,
7563 add_child (text_view, vc);
7565 g_assert (vc->widget == child);
7566 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7570 * gtk_text_view_move_child:
7571 * @text_view: a #GtkTextView
7572 * @child: child widget already added to the text view
7573 * @xpos: new X position in window coordinates
7574 * @ypos: new Y position in window coordinates
7576 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7580 gtk_text_view_move_child (GtkTextView *text_view,
7585 GtkTextViewChild *vc;
7587 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7588 g_return_if_fail (GTK_IS_WIDGET (child));
7589 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7591 vc = g_object_get_data (G_OBJECT (child),
7592 "gtk-text-view-child");
7594 g_assert (vc != NULL);
7596 if (vc->x == xpos &&
7603 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7604 gtk_widget_queue_resize (child);
7608 /* Iterator operations */
7611 * gtk_text_view_forward_display_line:
7612 * @text_view: a #GtkTextView
7613 * @iter: a #GtkTextIter
7615 * Moves the given @iter forward by one display (wrapped) line. A
7616 * display line is different from a paragraph. Paragraphs are
7617 * separated by newlines or other paragraph separator characters.
7618 * Display lines are created by line-wrapping a paragraph. If
7619 * wrapping is turned off, display lines and paragraphs will be the
7620 * same. Display lines are divided differently for each view, since
7621 * they depend on the view's width; paragraphs are the same in all
7622 * views, since they depend on the contents of the #GtkTextBuffer.
7624 * Return value: %TRUE if @iter was moved and is not on the end iterator
7627 gtk_text_view_forward_display_line (GtkTextView *text_view,
7630 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7631 g_return_val_if_fail (iter != NULL, FALSE);
7633 gtk_text_view_ensure_layout (text_view);
7635 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7639 * gtk_text_view_backward_display_line:
7640 * @text_view: a #GtkTextView
7641 * @iter: a #GtkTextIter
7643 * Moves the given @iter backward by one display (wrapped) line. A
7644 * display line is different from a paragraph. Paragraphs are
7645 * separated by newlines or other paragraph separator characters.
7646 * Display lines are created by line-wrapping a paragraph. If
7647 * wrapping is turned off, display lines and paragraphs will be the
7648 * same. Display lines are divided differently for each view, since
7649 * they depend on the view's width; paragraphs are the same in all
7650 * views, since they depend on the contents of the #GtkTextBuffer.
7652 * Return value: %TRUE if @iter was moved and is not on the end iterator
7655 gtk_text_view_backward_display_line (GtkTextView *text_view,
7658 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7659 g_return_val_if_fail (iter != NULL, FALSE);
7661 gtk_text_view_ensure_layout (text_view);
7663 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7667 * gtk_text_view_forward_display_line_end:
7668 * @text_view: a #GtkTextView
7669 * @iter: a #GtkTextIter
7671 * Moves the given @iter forward to the next display line end. A
7672 * display line is different from a paragraph. Paragraphs are
7673 * separated by newlines or other paragraph separator characters.
7674 * Display lines are created by line-wrapping a paragraph. If
7675 * wrapping is turned off, display lines and paragraphs will be the
7676 * same. Display lines are divided differently for each view, since
7677 * they depend on the view's width; paragraphs are the same in all
7678 * views, since they depend on the contents of the #GtkTextBuffer.
7680 * Return value: %TRUE if @iter was moved and is not on the end iterator
7683 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7686 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7687 g_return_val_if_fail (iter != NULL, FALSE);
7689 gtk_text_view_ensure_layout (text_view);
7691 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7695 * gtk_text_view_backward_display_line_start:
7696 * @text_view: a #GtkTextView
7697 * @iter: a #GtkTextIter
7699 * Moves the given @iter backward to the next display line start. A
7700 * display line is different from a paragraph. Paragraphs are
7701 * separated by newlines or other paragraph separator characters.
7702 * Display lines are created by line-wrapping a paragraph. If
7703 * wrapping is turned off, display lines and paragraphs will be the
7704 * same. Display lines are divided differently for each view, since
7705 * they depend on the view's width; paragraphs are the same in all
7706 * views, since they depend on the contents of the #GtkTextBuffer.
7708 * Return value: %TRUE if @iter was moved and is not on the end iterator
7711 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7714 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7715 g_return_val_if_fail (iter != NULL, FALSE);
7717 gtk_text_view_ensure_layout (text_view);
7719 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7723 * gtk_text_view_starts_display_line:
7724 * @text_view: a #GtkTextView
7725 * @iter: a #GtkTextIter
7727 * Determines whether @iter is at the start of a display line.
7728 * See gtk_text_view_forward_display_line() for an explanation of
7729 * display lines vs. paragraphs.
7731 * Return value: %TRUE if @iter begins a wrapped line
7734 gtk_text_view_starts_display_line (GtkTextView *text_view,
7735 const GtkTextIter *iter)
7737 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7738 g_return_val_if_fail (iter != NULL, FALSE);
7740 gtk_text_view_ensure_layout (text_view);
7742 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7746 * gtk_text_view_move_visually:
7747 * @text_view: a #GtkTextView
7748 * @iter: a #GtkTextIter
7749 * @count: number of characters to move (negative moves left, positive moves right)
7751 * Move the iterator a given number of characters visually, treating
7752 * it as the strong cursor position. If @count is positive, then the
7753 * new strong cursor position will be @count positions to the right of
7754 * the old cursor position. If @count is negative then the new strong
7755 * cursor position will be @count positions to the left of the old
7758 * In the presence of bidirection text, the correspondence
7759 * between logical and visual order will depend on the direction
7760 * of the current run, and there may be jumps when the cursor
7761 * is moved off of the end of a run.
7763 * Return value: %TRUE if @iter moved and is not on the end iterator
7766 gtk_text_view_move_visually (GtkTextView *text_view,
7770 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7771 g_return_val_if_fail (iter != NULL, FALSE);
7773 gtk_text_view_ensure_layout (text_view);
7775 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);