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 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2991 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2992 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2993 text_view->vadjustment->lower = 0;
2994 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2996 gtk_adjustment_changed (text_view->vadjustment);
2998 if (yoffset_changed)
2999 gtk_adjustment_value_changed (vadj);
3001 /* The GTK resize loop processes all the pending exposes right
3002 * after doing the resize stuff, so the idle sizer won't have a
3003 * chance to run. So we do the work here.
3005 gtk_text_view_flush_first_validate (text_view);
3007 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3010 if (size_changed && GTK_WIDGET_REALIZED (widget))
3011 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3015 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3018 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3019 text_view->first_para_mark);
3023 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3025 GtkWidget *widget = GTK_WIDGET (text_view);
3027 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3029 if (SCREEN_HEIGHT (widget) > 0)
3031 GtkTextIter first_para;
3033 /* Be sure we've validated the stuff onscreen; if we
3034 * scrolled, these calls won't have any effect, because
3035 * they were called in the recursive validate_onscreen
3037 gtk_text_view_get_first_para_iter (text_view, &first_para);
3039 gtk_text_layout_validate_yrange (text_view->layout,
3042 text_view->first_para_pixels +
3043 SCREEN_HEIGHT (widget));
3046 text_view->onscreen_validated = TRUE;
3048 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3050 /* This can have the odd side effect of triggering a scroll, which should
3051 * flip "onscreen_validated" back to FALSE, but should also get us
3052 * back into this function to turn it on again.
3054 gtk_text_view_update_adjustments (text_view);
3056 g_assert (text_view->onscreen_validated);
3060 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3062 if (text_view->first_validate_idle == 0)
3065 /* Do this first, which means that if an "invalidate"
3066 * occurs during any of this process, a new first_validate_callback
3067 * will be installed, and we'll start again.
3069 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3070 g_source_remove (text_view->first_validate_idle);
3071 text_view->first_validate_idle = 0;
3073 /* be sure we have up-to-date screen size set on the
3076 gtk_text_view_update_layout_width (text_view);
3078 /* Bail out if we invalidated stuff; scrolling right away will just
3079 * confuse the issue.
3081 if (text_view->first_validate_idle != 0)
3083 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3087 /* scroll to any marks, if that's pending. This can jump us to
3088 * the validation codepath used for scrolling onscreen, if so we
3089 * bail out. It won't jump if already in that codepath since
3090 * value_changed is not recursive, so also validate if
3093 if (!gtk_text_view_flush_scroll (text_view) ||
3094 !text_view->onscreen_validated)
3095 gtk_text_view_validate_onscreen (text_view);
3097 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3099 g_assert (text_view->onscreen_validated);
3104 first_validate_callback (gpointer data)
3106 GtkTextView *text_view = data;
3108 GDK_THREADS_ENTER ();
3110 /* Note that some of this code is duplicated at the end of size_allocate,
3111 * keep in sync with that.
3114 DV(g_print(G_STRLOC"\n"));
3116 gtk_text_view_flush_first_validate (text_view);
3118 GDK_THREADS_LEAVE ();
3124 incremental_validate_callback (gpointer data)
3126 GtkTextView *text_view = data;
3127 gboolean result = TRUE;
3129 GDK_THREADS_ENTER ();
3131 DV(g_print(G_STRLOC"\n"));
3133 gtk_text_layout_validate (text_view->layout, 2000);
3135 gtk_text_view_update_adjustments (text_view);
3137 if (gtk_text_layout_is_valid (text_view->layout))
3139 text_view->incremental_validate_idle = 0;
3143 GDK_THREADS_LEAVE ();
3149 gtk_text_view_invalidate (GtkTextView *text_view)
3151 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3152 text_view->onscreen_validated));
3154 text_view->onscreen_validated = FALSE;
3156 /* We'll invalidate when the layout is created */
3157 if (text_view->layout == NULL)
3160 if (!text_view->first_validate_idle)
3162 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3163 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3164 text_view->first_validate_idle));
3167 if (!text_view->incremental_validate_idle)
3169 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3170 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3171 text_view->incremental_validate_idle));
3176 invalidated_handler (GtkTextLayout *layout,
3179 GtkTextView *text_view;
3181 text_view = GTK_TEXT_VIEW (data);
3183 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3184 gtk_text_view_invalidate (text_view);
3188 changed_handler (GtkTextLayout *layout,
3194 GtkTextView *text_view;
3196 GdkRectangle visible_rect;
3197 GdkRectangle redraw_rect;
3199 text_view = GTK_TEXT_VIEW (data);
3200 widget = GTK_WIDGET (data);
3202 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3204 if (GTK_WIDGET_REALIZED (text_view))
3206 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3208 redraw_rect.x = visible_rect.x;
3209 redraw_rect.width = visible_rect.width;
3210 redraw_rect.y = start_y;
3212 if (old_height == new_height)
3213 redraw_rect.height = old_height;
3214 else if (start_y + old_height > visible_rect.y)
3215 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3217 redraw_rect.height = 0;
3219 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3221 /* text_window_invalidate_rect() takes buffer coordinates */
3222 text_window_invalidate_rect (text_view->text_window,
3225 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3229 redraw_rect.height));
3231 if (text_view->left_window)
3232 text_window_invalidate_rect (text_view->left_window,
3234 if (text_view->right_window)
3235 text_window_invalidate_rect (text_view->right_window,
3237 if (text_view->top_window)
3238 text_window_invalidate_rect (text_view->top_window,
3240 if (text_view->bottom_window)
3241 text_window_invalidate_rect (text_view->bottom_window,
3244 gtk_text_view_update_im_spot_location (text_view);
3248 if (old_height != new_height)
3250 gboolean yoffset_changed = FALSE;
3252 int new_first_para_top;
3253 int old_first_para_top;
3256 /* If the bottom of the old area was above the top of the
3257 * screen, we need to scroll to keep the current top of the
3258 * screen in place. Remember that first_para_pixels is the
3259 * position of the top of the screen in coordinates relative to
3260 * the first paragraph onscreen.
3262 * In short we are adding the height delta of the portion of the
3263 * changed region above first_para_mark to text_view->yoffset.
3265 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3266 text_view->first_para_mark);
3268 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3270 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3272 if (new_first_para_top != old_first_para_top)
3274 text_view->yoffset += new_first_para_top - old_first_para_top;
3276 get_vadjustment (text_view)->value = text_view->yoffset;
3277 yoffset_changed = TRUE;
3280 if (yoffset_changed)
3282 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3283 gtk_adjustment_value_changed (get_vadjustment (text_view));
3286 /* FIXME be smarter about which anchored widgets we update */
3288 tmp_list = text_view->children;
3289 while (tmp_list != NULL)
3291 GtkTextViewChild *child = tmp_list->data;
3294 gtk_text_view_update_child_allocation (text_view, child);
3296 tmp_list = g_slist_next (tmp_list);
3301 GtkRequisition old_req;
3302 GtkRequisition new_req;
3304 old_req = widget->requisition;
3306 /* Use this instead of gtk_widget_size_request wrapper
3307 * to avoid the optimization which just returns widget->requisition
3308 * if a resize hasn't been queued.
3310 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3312 if (old_req.width != new_req.width ||
3313 old_req.height != new_req.height)
3315 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3316 _gtk_size_group_queue_resize (widget);
3322 gtk_text_view_realize (GtkWidget *widget)
3324 GtkTextView *text_view;
3325 GdkWindowAttr attributes;
3326 gint attributes_mask;
3329 text_view = GTK_TEXT_VIEW (widget);
3330 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3332 attributes.window_type = GDK_WINDOW_CHILD;
3333 attributes.x = widget->allocation.x;
3334 attributes.y = widget->allocation.y;
3335 attributes.width = widget->allocation.width;
3336 attributes.height = widget->allocation.height;
3337 attributes.wclass = GDK_INPUT_OUTPUT;
3338 attributes.visual = gtk_widget_get_visual (widget);
3339 attributes.colormap = gtk_widget_get_colormap (widget);
3340 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3342 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3344 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3345 &attributes, attributes_mask);
3346 gdk_window_set_user_data (widget->window, widget);
3348 /* must come before text_window_realize calls */
3349 widget->style = gtk_style_attach (widget->style, widget->window);
3351 gdk_window_set_background (widget->window,
3352 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3354 text_window_realize (text_view->text_window, widget->window);
3356 if (text_view->left_window)
3357 text_window_realize (text_view->left_window,
3360 if (text_view->top_window)
3361 text_window_realize (text_view->top_window,
3364 if (text_view->right_window)
3365 text_window_realize (text_view->right_window,
3368 if (text_view->bottom_window)
3369 text_window_realize (text_view->bottom_window,
3372 gtk_text_view_ensure_layout (text_view);
3374 if (text_view->buffer)
3376 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3377 GDK_SELECTION_PRIMARY);
3378 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3381 tmp_list = text_view->children;
3382 while (tmp_list != NULL)
3384 GtkTextViewChild *vc = tmp_list->data;
3386 text_view_child_set_parent_window (text_view, vc);
3388 tmp_list = tmp_list->next;
3393 gtk_text_view_unrealize (GtkWidget *widget)
3395 GtkTextView *text_view;
3397 text_view = GTK_TEXT_VIEW (widget);
3399 if (text_view->buffer)
3401 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3402 GDK_SELECTION_PRIMARY);
3403 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3406 gtk_text_view_remove_validate_idles (text_view);
3408 if (text_view->popup_menu)
3410 gtk_widget_destroy (text_view->popup_menu);
3411 text_view->popup_menu = NULL;
3414 text_window_unrealize (text_view->text_window);
3416 if (text_view->left_window)
3417 text_window_unrealize (text_view->left_window);
3419 if (text_view->top_window)
3420 text_window_unrealize (text_view->top_window);
3422 if (text_view->right_window)
3423 text_window_unrealize (text_view->right_window);
3425 if (text_view->bottom_window)
3426 text_window_unrealize (text_view->bottom_window);
3428 gtk_text_view_destroy_layout (text_view);
3430 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3434 gtk_text_view_set_background (GtkTextView *text_view)
3436 GtkWidget *widget = GTK_WIDGET (text_view);
3438 gdk_window_set_background (widget->window,
3439 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3441 gdk_window_set_background (text_view->text_window->bin_window,
3442 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3444 if (text_view->left_window)
3445 gdk_window_set_background (text_view->left_window->bin_window,
3446 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3447 if (text_view->right_window)
3448 gdk_window_set_background (text_view->right_window->bin_window,
3449 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3451 if (text_view->top_window)
3452 gdk_window_set_background (text_view->top_window->bin_window,
3453 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3455 if (text_view->bottom_window)
3456 gdk_window_set_background (text_view->bottom_window->bin_window,
3457 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3461 gtk_text_view_style_set (GtkWidget *widget,
3462 GtkStyle *previous_style)
3464 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3466 if (GTK_WIDGET_REALIZED (widget))
3468 gtk_text_view_set_background (text_view);
3471 if (text_view->layout && previous_style)
3473 gtk_text_view_set_attributes_from_style (text_view,
3474 text_view->layout->default_style,
3476 gtk_text_layout_default_style_changed (text_view->layout);
3481 gtk_text_view_direction_changed (GtkWidget *widget,
3482 GtkTextDirection previous_direction)
3484 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3486 if (text_view->layout)
3488 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3489 gtk_text_layout_default_style_changed (text_view->layout);
3494 gtk_text_view_state_changed (GtkWidget *widget,
3495 GtkStateType previous_state)
3497 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3499 if (GTK_WIDGET_REALIZED (widget))
3501 gtk_text_view_set_background (text_view);
3504 if (!GTK_WIDGET_IS_SENSITIVE (widget))
3506 /* Clear any selection */
3507 gtk_text_view_unselect (text_view);
3510 gtk_widget_queue_draw (widget);
3514 set_invisible_cursor (GdkWindow *window)
3516 GdkBitmap *empty_bitmap;
3519 char invisible_cursor_bits[] = { 0x0 };
3521 useless.red = useless.green = useless.blue = 0;
3524 empty_bitmap = gdk_bitmap_create_from_data (window,
3525 invisible_cursor_bits,
3528 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3533 gdk_window_set_cursor (window, cursor);
3535 gdk_cursor_unref (cursor);
3537 g_object_unref (empty_bitmap);
3541 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3543 if (text_view->mouse_cursor_obscured)
3546 set_invisible_cursor (text_view->text_window->bin_window);
3548 text_view->mouse_cursor_obscured = TRUE;
3556 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3559 switch (event->type)
3561 case GDK_MOTION_NOTIFY:
3562 *x = event->motion.x;
3563 *y = event->motion.y;
3567 case GDK_BUTTON_PRESS:
3568 case GDK_2BUTTON_PRESS:
3569 case GDK_3BUTTON_PRESS:
3570 case GDK_BUTTON_RELEASE:
3571 *x = event->button.x;
3572 *y = event->button.y;
3577 case GDK_KEY_RELEASE:
3578 case GDK_ENTER_NOTIFY:
3579 case GDK_LEAVE_NOTIFY:
3580 case GDK_PROPERTY_NOTIFY:
3581 case GDK_SELECTION_CLEAR:
3582 case GDK_SELECTION_REQUEST:
3583 case GDK_SELECTION_NOTIFY:
3584 case GDK_PROXIMITY_IN:
3585 case GDK_PROXIMITY_OUT:
3586 case GDK_DRAG_ENTER:
3587 case GDK_DRAG_LEAVE:
3588 case GDK_DRAG_MOTION:
3589 case GDK_DRAG_STATUS:
3590 case GDK_DROP_START:
3591 case GDK_DROP_FINISHED:
3601 emit_event_on_tags (GtkWidget *widget,
3607 gboolean retval = FALSE;
3608 GtkTextView *text_view;
3610 text_view = GTK_TEXT_VIEW (widget);
3612 tags = gtk_text_iter_get_tags (iter);
3617 GtkTextTag *tag = tmp->data;
3619 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3625 tmp = g_slist_next (tmp);
3628 g_slist_free (tags);
3634 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3636 GtkTextView *text_view;
3639 text_view = GTK_TEXT_VIEW (widget);
3641 if (text_view->layout == NULL ||
3642 get_buffer (text_view) == NULL)
3645 if (event->any.window != text_view->text_window->bin_window)
3648 if (get_event_coordinates (event, &x, &y))
3652 x += text_view->xoffset;
3653 y += text_view->yoffset;
3655 /* FIXME this is slow and we do it twice per event.
3656 * My favorite solution is to have GtkTextLayout cache
3657 * the last couple lookups.
3659 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3663 return emit_event_on_tags (widget, event, &iter);
3665 else if (event->type == GDK_KEY_PRESS ||
3666 event->type == GDK_KEY_RELEASE)
3668 GtkTextMark *insert;
3671 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3674 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3676 return emit_event_on_tags (widget, event, &iter);
3683 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3685 gboolean retval = FALSE;
3686 gboolean obscure = FALSE;
3687 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3688 GtkTextMark *insert;
3690 gboolean can_insert;
3692 if (text_view->layout == NULL ||
3693 get_buffer (text_view) == NULL)
3696 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3697 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3698 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3700 gtk_im_context_filter_keypress (text_view->im_context, event))
3702 text_view->need_im_reset = TRUE;
3707 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3708 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3710 /* use overall editability not can_insert, more predictable for users */
3711 else if (text_view->editable &&
3712 (event->keyval == GDK_Return ||
3713 event->keyval == GDK_KP_Enter))
3715 /* this won't actually insert the newline if the cursor isn't
3718 gtk_text_view_commit_text (text_view, "\n");
3723 /* Pass through Tab as literal tab, unless Control is held down */
3724 else if ((event->keyval == GDK_Tab ||
3725 event->keyval == GDK_KP_Tab) &&
3726 !(event->state & GDK_CONTROL_MASK))
3728 /* If the text widget isn't editable overall, move the focus
3731 if (text_view->editable)
3733 gtk_text_view_commit_text (text_view, "\t");
3737 gtk_text_view_move_focus (text_view,
3738 (event->state & GDK_SHIFT_MASK) ?
3739 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3747 gtk_text_view_obscure_mouse_cursor (text_view);
3749 gtk_text_view_pend_cursor_blink (text_view);
3755 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3757 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3758 GtkTextMark *insert;
3761 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3764 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3765 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3766 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3767 gtk_im_context_filter_keypress (text_view->im_context, event))
3769 text_view->need_im_reset = TRUE;
3773 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3777 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3779 GtkTextView *text_view;
3781 text_view = GTK_TEXT_VIEW (widget);
3783 text_view->disable_scroll_on_focus = TRUE;
3784 gtk_widget_grab_focus (widget);
3785 text_view->disable_scroll_on_focus = FALSE;
3787 if (event->window != text_view->text_window->bin_window)
3789 /* Remove selection if any. */
3790 gtk_text_view_unselect (text_view);
3796 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3797 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3798 else if (event->button == 3)
3799 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3802 if (event->type == GDK_BUTTON_PRESS)
3804 gtk_text_view_reset_im_context (text_view);
3806 if (event->button == 1)
3808 /* If we're in the selection, start a drag copy/move of the
3809 * selection; otherwise, start creating a new selection.
3812 GtkTextIter start, end;
3814 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3816 event->x + text_view->xoffset,
3817 event->y + text_view->yoffset);
3819 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3821 gtk_text_iter_in_range (&iter, &start, &end))
3823 text_view->drag_start_x = event->x;
3824 text_view->drag_start_y = event->y;
3825 text_view->pending_place_cursor_button = event->button;
3829 gtk_text_view_start_selection_drag (text_view, &iter, event);
3834 else if (event->button == 2)
3838 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3840 event->x + text_view->xoffset,
3841 event->y + text_view->yoffset);
3843 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3844 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3846 text_view->editable);
3849 else if (event->button == 3)
3851 gtk_text_view_do_popup (text_view, event);
3855 else if ((event->type == GDK_2BUTTON_PRESS ||
3856 event->type == GDK_3BUTTON_PRESS) &&
3859 GtkTextIter start, end;
3861 /* End the selection drag, otherwise we'd clear the new
3862 * word/line selection on button release
3864 gtk_text_view_end_selection_drag (text_view, event);
3866 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3868 event->x + text_view->xoffset,
3869 event->y + text_view->yoffset);
3873 if (event->type == GDK_2BUTTON_PRESS)
3875 if (gtk_text_iter_inside_word (&start))
3877 if (!gtk_text_iter_starts_word (&start))
3878 gtk_text_iter_backward_word_start (&start);
3880 if (!gtk_text_iter_ends_word (&end))
3881 gtk_text_iter_forward_word_end (&end);
3884 else if (event->type == GDK_3BUTTON_PRESS)
3886 if (gtk_text_view_starts_display_line (text_view, &start))
3888 /* If on a display line boundary, we assume the user
3889 * clicked off the end of a line and we therefore select
3890 * the line before the boundary.
3892 gtk_text_view_backward_display_line_start (text_view, &start);
3896 /* start isn't on the start of a line, so we move it to the
3897 * start, and move end to the end unless it's already there.
3899 gtk_text_view_backward_display_line_start (text_view, &start);
3901 if (!gtk_text_view_starts_display_line (text_view, &end))
3902 gtk_text_view_forward_display_line_end (text_view, &end);
3906 if (event->state & GDK_SHIFT_MASK)
3908 /* Take union of old and new selection */
3909 GtkTextIter old_start, old_end;
3911 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3912 &old_start, &old_end);
3914 gtk_text_iter_order (&start, &old_start);
3915 gtk_text_iter_order (&old_end, &end);
3917 /* Now start is the first of the starts, and end is the
3922 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3925 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3929 text_view->just_selected_element = TRUE;
3938 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3940 GtkTextView *text_view;
3942 text_view = GTK_TEXT_VIEW (widget);
3944 if (event->window != text_view->text_window->bin_window)
3947 if (event->button == 1)
3949 if (text_view->drag_start_x >= 0)
3951 text_view->drag_start_x = -1;
3952 text_view->drag_start_y = -1;
3955 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3957 else if (text_view->just_selected_element)
3959 text_view->just_selected_element = FALSE;
3962 else if (text_view->pending_place_cursor_button == event->button)
3966 /* Unselect everything; we clicked inside selection, but
3967 * didn't move by the drag threshold, so just clear selection
3970 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3972 event->x + text_view->xoffset,
3973 event->y + text_view->yoffset);
3975 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3977 text_view->pending_place_cursor_button = 0;
3987 keymap_direction_changed (GdkKeymap *keymap,
3988 GtkTextView *text_view)
3990 gtk_text_view_check_keymap_direction (text_view);
3994 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3996 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3998 gtk_widget_queue_draw (widget);
4000 DV(g_print (G_STRLOC": focus_in_event\n"));
4002 if (text_view->cursor_visible && text_view->layout)
4004 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4005 gtk_text_view_check_cursor_blink (text_view);
4008 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4009 "direction_changed",
4010 G_CALLBACK (keymap_direction_changed), text_view);
4011 gtk_text_view_check_keymap_direction (text_view);
4013 text_view->need_im_reset = TRUE;
4014 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4020 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4022 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4024 gtk_widget_queue_draw (widget);
4026 DV(g_print (G_STRLOC": focus_out_event\n"));
4028 if (text_view->cursor_visible && text_view->layout)
4030 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4031 gtk_text_view_check_cursor_blink (text_view);
4034 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4035 keymap_direction_changed,
4038 text_view->need_im_reset = TRUE;
4039 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4045 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4047 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4049 if (text_view->mouse_cursor_obscured)
4053 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4055 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4056 gdk_cursor_unref (cursor);
4057 text_view->mouse_cursor_obscured = FALSE;
4060 if (event->window == text_view->text_window->bin_window &&
4061 text_view->drag_start_x >= 0)
4065 gdk_window_get_pointer (text_view->text_window->bin_window,
4068 if (gtk_drag_check_threshold (widget,
4069 text_view->drag_start_x,
4070 text_view->drag_start_y,
4074 gint buffer_x, buffer_y;
4076 gtk_text_view_window_to_buffer_coords (text_view,
4077 GTK_TEXT_WINDOW_TEXT,
4078 text_view->drag_start_x,
4079 text_view->drag_start_y,
4083 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4085 buffer_x, buffer_y);
4087 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4096 gtk_text_view_paint (GtkWidget *widget,
4098 GdkEventExpose *event)
4100 GtkTextView *text_view;
4101 GList *child_exposes;
4105 text_view = GTK_TEXT_VIEW (widget);
4107 g_return_if_fail (text_view->layout != NULL);
4108 g_return_if_fail (text_view->xoffset >= 0);
4109 g_return_if_fail (text_view->yoffset >= 0);
4111 while (text_view->first_validate_idle != 0)
4113 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4114 text_view->first_validate_idle));
4115 gtk_text_view_flush_first_validate (text_view);
4118 /* More regions could have become invalid in the above loop */
4119 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4124 gdk_region_get_clipbox (updates, &rect);
4126 gdk_rectangle_union (area, &rect, area);
4128 gdk_region_destroy (updates);
4131 if (!text_view->onscreen_validated)
4133 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.");
4134 g_assert_not_reached ();
4138 printf ("painting %d,%d %d x %d\n",
4140 area->width, area->height);
4143 child_exposes = NULL;
4144 gtk_text_layout_draw (text_view->layout,
4146 text_view->text_window->bin_window,
4151 area->width, area->height,
4154 tmp_list = child_exposes;
4155 while (tmp_list != NULL)
4157 GtkWidget *child = tmp_list->data;
4159 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4163 g_object_unref (child);
4165 tmp_list = tmp_list->next;
4168 g_list_free (child_exposes);
4172 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4176 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4177 GTK_TEXT_WINDOW_TEXT))
4179 DV(g_print (">Exposed ("G_STRLOC")\n"));
4180 gtk_text_view_paint (widget, &event->area, event);
4183 if (event->window == widget->window)
4184 gtk_text_view_draw_focus (widget);
4186 /* Propagate exposes to all children not in the buffer. */
4187 tmp_list = GTK_TEXT_VIEW (widget)->children;
4188 while (tmp_list != NULL)
4190 GtkTextViewChild *vc = tmp_list->data;
4192 /* propagate_expose checks that event->window matches
4195 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4196 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4200 tmp_list = tmp_list->next;
4207 gtk_text_view_draw_focus (GtkWidget *widget)
4209 gboolean interior_focus;
4211 /* We clear the focus if we are in interior focus mode. */
4212 gtk_widget_style_get (widget,
4213 "interior_focus", &interior_focus,
4216 if (GTK_WIDGET_DRAWABLE (widget))
4218 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4220 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4221 NULL, widget, "textview",
4223 widget->allocation.width,
4224 widget->allocation.height);
4228 gdk_window_clear (widget->window);
4234 gtk_text_view_grab_focus (GtkWidget *widget)
4236 GtkTextView *text_view;
4238 text_view = GTK_TEXT_VIEW (widget);
4240 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4242 if (!text_view->disable_scroll_on_focus)
4243 gtk_text_view_scroll_mark_onscreen (text_view,
4244 gtk_text_buffer_get_mark (get_buffer (text_view),
4249 gtk_text_view_focus (GtkWidget *widget,
4250 GtkDirectionType direction)
4252 GtkTextView *text_view;
4253 GtkContainer *container;
4256 text_view = GTK_TEXT_VIEW (widget);
4257 container = GTK_CONTAINER (widget);
4259 if (!gtk_widget_is_focus (widget) &&
4260 container->focus_child == NULL)
4262 gtk_widget_grab_focus (widget);
4268 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4269 * children to get the focus
4271 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4272 result = GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4273 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4284 gtk_text_view_add (GtkContainer *container,
4287 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4288 g_return_if_fail (GTK_IS_WIDGET (child));
4290 /* This is pretty random. */
4291 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4293 GTK_TEXT_WINDOW_WIDGET,
4298 gtk_text_view_remove (GtkContainer *container,
4302 GtkTextView *text_view;
4303 GtkTextViewChild *vc;
4305 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4306 g_return_if_fail (GTK_IS_WIDGET (child));
4307 g_return_if_fail (child->parent == (GtkWidget*) container);
4309 text_view = GTK_TEXT_VIEW (container);
4312 iter = text_view->children;
4314 while (iter != NULL)
4318 if (vc->widget == child)
4321 iter = g_slist_next (iter);
4324 g_assert (iter != NULL); /* be sure we had the child in the list */
4326 text_view->children = g_slist_remove (text_view->children, vc);
4328 gtk_widget_unparent (vc->widget);
4330 text_view_child_free (vc);
4334 gtk_text_view_forall (GtkContainer *container,
4335 gboolean include_internals,
4336 GtkCallback callback,
4337 gpointer callback_data)
4340 GtkTextView *text_view;
4343 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4344 g_return_if_fail (callback != NULL);
4346 text_view = GTK_TEXT_VIEW (container);
4348 copy = g_slist_copy (text_view->children);
4351 while (iter != NULL)
4353 GtkTextViewChild *vc = iter->data;
4355 (* callback) (vc->widget, callback_data);
4357 iter = g_slist_next (iter);
4360 g_slist_free (copy);
4363 #define CURSOR_ON_MULTIPLIER 0.66
4364 #define CURSOR_OFF_MULTIPLIER 0.34
4365 #define CURSOR_PEND_MULTIPLIER 1.0
4368 cursor_blinks (GtkTextView *text_view)
4370 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4373 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4376 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4379 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4384 get_cursor_time (GtkTextView *text_view)
4386 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4389 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4399 blink_cb (gpointer data)
4401 GtkTextView *text_view;
4404 GDK_THREADS_ENTER ();
4406 text_view = GTK_TEXT_VIEW (data);
4408 g_assert (text_view->layout);
4409 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4410 g_assert (text_view->cursor_visible);
4412 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4415 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4419 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4423 gtk_text_layout_set_cursor_visible (text_view->layout,
4426 GDK_THREADS_LEAVE ();
4428 /* Remove ourselves */
4434 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4436 if (text_view->blink_timeout)
4438 gtk_timeout_remove (text_view->blink_timeout);
4439 text_view->blink_timeout = 0;
4444 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4446 if (text_view->layout != NULL &&
4447 text_view->cursor_visible &&
4448 GTK_WIDGET_HAS_FOCUS (text_view))
4450 if (cursor_blinks (text_view))
4452 if (text_view->blink_timeout == 0)
4454 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4456 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4462 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4466 gtk_text_view_stop_cursor_blink (text_view);
4471 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4473 if (text_view->layout != NULL &&
4474 text_view->cursor_visible &&
4475 GTK_WIDGET_HAS_FOCUS (text_view) &&
4476 cursor_blinks (text_view))
4478 if (text_view->blink_timeout != 0)
4480 gtk_timeout_remove (text_view->blink_timeout);
4481 text_view->blink_timeout = 0;
4484 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4486 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4494 * Key binding handlers
4498 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4499 GtkTextIter *newplace,
4504 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4510 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4516 move_cursor (GtkTextView *text_view,
4517 const GtkTextIter *new_location,
4518 gboolean extend_selection)
4520 if (extend_selection)
4521 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4525 gtk_text_buffer_place_cursor (get_buffer (text_view),
4529 /* FIXME when we are unfrozen and can change GtkMovementStep,
4532 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4535 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4536 GtkMovementStep step,
4538 gboolean extend_selection)
4541 GtkTextIter newplace;
4543 gint cursor_x_pos = 0;
4545 gtk_text_view_reset_im_context (text_view);
4547 if (step == GTK_MOVEMENT_PAGES)
4549 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4550 gtk_text_view_pend_cursor_blink (text_view);
4553 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4555 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4556 gtk_text_view_pend_cursor_blink (text_view);
4560 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4561 gtk_text_buffer_get_mark (get_buffer (text_view),
4565 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4566 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4570 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4571 gtk_text_iter_forward_cursor_positions (&newplace, count);
4574 case GTK_MOVEMENT_VISUAL_POSITIONS:
4575 gtk_text_layout_move_iter_visually (text_view->layout,
4579 case GTK_MOVEMENT_WORDS:
4581 gtk_text_iter_backward_word_starts (&newplace, -count);
4583 gtk_text_iter_forward_word_ends (&newplace, count);
4586 case GTK_MOVEMENT_DISPLAY_LINES:
4587 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4588 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4591 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4593 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4594 else if (count < -1)
4595 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4598 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4601 case GTK_MOVEMENT_PARAGRAPHS:
4604 if (!gtk_text_iter_ends_line (&newplace))
4606 gtk_text_iter_forward_to_line_end (&newplace);
4612 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4614 gtk_text_iter_set_line_offset (&newplace, 0);
4621 gtk_text_iter_forward_lines (&newplace, count);
4622 gtk_text_iter_set_line_offset (&newplace, 0);
4626 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4629 if (!gtk_text_iter_ends_line (&newplace))
4630 gtk_text_iter_forward_to_line_end (&newplace);
4634 gtk_text_iter_set_line_offset (&newplace, 0);
4638 case GTK_MOVEMENT_BUFFER_ENDS:
4640 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4642 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4649 /* call move_cursor() even if the cursor hasn't moved, since it
4650 cancels the selection
4652 move_cursor (text_view, &newplace, extend_selection);
4654 if (!gtk_text_iter_equal (&insert, &newplace))
4656 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4657 gtk_text_view_scroll_mark_onscreen (text_view,
4658 gtk_text_buffer_get_mark (get_buffer (text_view),
4661 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4663 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4667 gtk_text_view_pend_cursor_blink (text_view);
4671 gtk_text_view_move_cursor (GtkTextView *text_view,
4672 GtkMovementStep step,
4674 gboolean extend_selection)
4676 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4680 gtk_text_view_page_horizontally (GtkTextView *text_view,
4682 gboolean extend_selection)
4684 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4685 count, extend_selection);
4689 gtk_text_view_set_anchor (GtkTextView *text_view)
4693 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4694 gtk_text_buffer_get_mark (get_buffer (text_view),
4697 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4701 gtk_text_view_scroll_pages (GtkTextView *text_view,
4703 gboolean extend_selection)
4708 gint cursor_x_pos, cursor_y_pos;
4709 GtkTextIter new_insert;
4713 g_return_if_fail (text_view->vadjustment != NULL);
4715 cancel_pending_scroll (text_view);
4717 adj = text_view->vadjustment;
4719 /* Validate the region that will be brought into view by the cursor motion
4723 gtk_text_view_get_first_para_iter (text_view, &anchor);
4724 y0 = adj->page_size;
4725 y1 = adj->page_size + count * adj->page_increment;
4729 gtk_text_view_get_first_para_iter (text_view, &anchor);
4730 y0 = count * adj->page_increment + adj->page_size;
4734 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4735 /* FIXME do we need to update the adjustment ranges here? */
4737 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4739 /* already at top, just be sure we are at offset 0 */
4740 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4741 move_cursor (text_view, &new_insert, extend_selection);
4743 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4745 /* already at bottom, just be sure we are at the end */
4746 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4747 move_cursor (text_view, &new_insert, extend_selection);
4751 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4753 newval = adj->value;
4754 oldval = adj->value;
4756 newval += count * adj->page_increment;
4758 set_adjustment_clamped (adj, newval);
4759 cursor_y_pos += adj->value - oldval;
4761 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4762 clamp_iter_onscreen (text_view, &new_insert);
4763 move_cursor (text_view, &new_insert, extend_selection);
4765 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4768 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4769 * only guarantees 1 pixel onscreen.
4771 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4772 gtk_text_view_scroll_mark_onscreen (text_view,
4773 gtk_text_buffer_get_mark (get_buffer (text_view),
4778 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4780 gboolean extend_selection)
4785 gint cursor_x_pos, cursor_y_pos;
4786 GtkTextIter new_insert;
4789 g_return_if_fail (text_view->hadjustment != NULL);
4791 cancel_pending_scroll (text_view);
4793 adj = text_view->hadjustment;
4795 /* Validate the line that we're moving within.
4797 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4799 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4800 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4801 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4802 /* FIXME do we need to update the adjustment ranges here? */
4804 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4806 /* already at far left, just be sure we are at offset 0 */
4807 gtk_text_iter_set_line_offset (&new_insert, 0);
4808 move_cursor (text_view, &new_insert, extend_selection);
4810 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4812 /* already at far right, just be sure we are at the end */
4813 gtk_text_iter_forward_to_line_end (&new_insert);
4814 move_cursor (text_view, &new_insert, extend_selection);
4818 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4820 newval = adj->value;
4821 oldval = adj->value;
4823 newval += count * adj->page_increment;
4825 set_adjustment_clamped (adj, newval);
4826 cursor_x_pos += adj->value - oldval;
4828 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4829 clamp_iter_onscreen (text_view, &new_insert);
4830 move_cursor (text_view, &new_insert, extend_selection);
4832 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4835 /* FIXME for lines shorter than the overall widget width, this results in a
4836 * "bounce" effect as we scroll to the right of the widget, then scroll
4837 * back to get the end of the line onscreen.
4838 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4841 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4842 * only guarantees 1 pixel onscreen.
4844 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4845 gtk_text_view_scroll_mark_onscreen (text_view,
4846 gtk_text_buffer_get_mark (get_buffer (text_view),
4851 whitespace (gunichar ch, gpointer user_data)
4853 return (ch == ' ' || ch == '\t');
4857 not_whitespace (gunichar ch, gpointer user_data)
4859 return !whitespace (ch, user_data);
4863 find_whitepace_region (const GtkTextIter *center,
4864 GtkTextIter *start, GtkTextIter *end)
4869 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4870 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4871 if (whitespace (gtk_text_iter_get_char (end), NULL))
4872 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4874 return !gtk_text_iter_equal (start, end);
4878 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4881 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4882 text_view->editable);
4886 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4893 gboolean leave_one = FALSE;
4895 gtk_text_view_reset_im_context (text_view);
4897 if (type == GTK_DELETE_CHARS)
4899 /* Char delete deletes the selection, if one exists */
4900 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4901 text_view->editable))
4905 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4907 gtk_text_buffer_get_mark (get_buffer (text_view),
4915 case GTK_DELETE_CHARS:
4916 gtk_text_iter_forward_cursor_positions (&end, count);
4919 case GTK_DELETE_WORD_ENDS:
4921 gtk_text_iter_forward_word_ends (&end, count);
4923 gtk_text_iter_backward_word_starts (&start, 0 - count);
4926 case GTK_DELETE_WORDS:
4929 case GTK_DELETE_DISPLAY_LINE_ENDS:
4932 case GTK_DELETE_DISPLAY_LINES:
4935 case GTK_DELETE_PARAGRAPH_ENDS:
4936 /* If we're already at a newline, we need to
4937 * simply delete that newline, instead of
4938 * moving to the next one.
4940 if (gtk_text_iter_ends_line (&end))
4942 gtk_text_iter_forward_line (&end);
4948 if (!gtk_text_iter_forward_to_line_end (&end))
4954 /* FIXME figure out what a negative count means
4958 case GTK_DELETE_PARAGRAPHS:
4961 gtk_text_iter_set_line_offset (&start, 0);
4962 gtk_text_iter_forward_to_line_end (&end);
4964 /* Do the lines beyond the first. */
4967 gtk_text_iter_forward_to_line_end (&end);
4973 /* FIXME negative count? */
4977 case GTK_DELETE_WHITESPACE:
4979 find_whitepace_region (&insert, &start, &end);
4987 if (!gtk_text_iter_equal (&start, &end))
4989 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4991 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4992 text_view->editable))
4995 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4997 text_view->editable);
5000 gtk_text_buffer_end_user_action (get_buffer (text_view));
5002 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5003 gtk_text_view_scroll_mark_onscreen (text_view,
5004 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5009 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5011 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5012 GDK_SELECTION_CLIPBOARD);
5014 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5016 text_view->editable);
5017 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5018 gtk_text_view_scroll_mark_onscreen (text_view,
5019 gtk_text_buffer_get_mark (get_buffer (text_view),
5024 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5026 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5027 GDK_SELECTION_CLIPBOARD);
5029 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5031 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5032 gtk_text_view_scroll_mark_onscreen (text_view,
5033 gtk_text_buffer_get_mark (get_buffer (text_view),
5038 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5040 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5041 GDK_SELECTION_CLIPBOARD);
5043 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5046 text_view->editable);
5047 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5048 gtk_text_view_scroll_mark_onscreen (text_view,
5049 gtk_text_buffer_get_mark (get_buffer (text_view),
5054 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5056 text_view->overwrite_mode = !text_view->overwrite_mode;
5060 gtk_text_view_move_focus (GtkTextView *text_view,
5061 GtkDirectionType direction_type)
5063 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5065 if (!GTK_WIDGET_TOPLEVEL (toplevel))
5068 /* Propagate to toplevel */
5069 g_signal_emit_by_name (toplevel, "move_focus", direction_type);
5077 gtk_text_view_unselect (GtkTextView *text_view)
5081 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5083 gtk_text_buffer_get_mark (get_buffer (text_view),
5086 gtk_text_buffer_move_mark (get_buffer (text_view),
5087 gtk_text_buffer_get_mark (get_buffer (text_view),
5093 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5094 const gchar *mark_name)
5097 GdkModifierType state;
5098 GtkTextIter newplace;
5100 /* DV(g_print (G_STRLOC": begin\n")); */
5102 gdk_window_get_pointer (text_view->text_window->bin_window,
5105 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5106 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5108 x + text_view->xoffset,
5109 y + text_view->yoffset);
5113 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5115 /* This may invalidate the layout */
5116 DV(g_print (G_STRLOC": move mark\n"));
5117 gtk_text_buffer_move_mark (get_buffer (text_view),
5121 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5122 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5125 DV (g_print ("first validate idle leaving %s is %d\n",
5126 G_STRLOC, text_view->first_validate_idle));
5130 selection_scan_timeout (gpointer data)
5132 GtkTextView *text_view;
5134 GDK_THREADS_ENTER ();
5136 text_view = GTK_TEXT_VIEW (data);
5138 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5139 move_mark_to_pointer_and_scroll (text_view, "insert");
5141 GDK_THREADS_LEAVE ();
5143 return TRUE; /* remain installed. */
5146 #define DND_SCROLL_MARGIN 0.20
5149 drag_scan_timeout (gpointer data)
5151 GtkTextView *text_view;
5153 GdkModifierType state;
5154 GtkTextIter newplace;
5156 GDK_THREADS_ENTER ();
5158 text_view = GTK_TEXT_VIEW (data);
5160 gdk_window_get_pointer (text_view->text_window->bin_window,
5163 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5165 x + text_view->xoffset,
5166 y + text_view->yoffset);
5168 gtk_text_buffer_move_mark (get_buffer (text_view),
5169 text_view->dnd_mark,
5172 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5173 gtk_text_view_scroll_to_mark (text_view,
5174 text_view->dnd_mark,
5175 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5177 GDK_THREADS_LEAVE ();
5183 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5185 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5186 move_mark_to_pointer_and_scroll (text_view, "insert");
5188 /* If we had to scroll offscreen, insert a timeout to do so
5189 * again. Note that in the timeout, even if the mouse doesn't
5190 * move, due to this scroll xoffset/yoffset will have changed
5191 * and we'll need to scroll again.
5193 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5194 gtk_timeout_remove (text_view->scroll_timeout);
5196 text_view->scroll_timeout =
5197 gtk_timeout_add (50, selection_scan_timeout, text_view);
5203 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5204 const GtkTextIter *iter,
5205 GdkEventButton *button)
5207 GtkTextIter newplace;
5208 GtkTextBuffer *buffer;
5210 g_return_if_fail (text_view->selection_drag_handler == 0);
5212 gtk_grab_add (GTK_WIDGET (text_view));
5214 buffer = get_buffer (text_view);
5218 if (button->state & GDK_SHIFT_MASK)
5220 /* Extend selection */
5221 GtkTextIter start, end;
5223 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5225 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5227 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5230 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5233 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5235 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5238 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5244 /* Replace selection */
5245 gtk_text_buffer_place_cursor (buffer, &newplace);
5248 text_view->selection_drag_handler = g_signal_connect (text_view,
5249 "motion_notify_event",
5250 G_CALLBACK (selection_motion_event_handler),
5254 /* returns whether we were really dragging */
5256 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5258 if (text_view->selection_drag_handler == 0)
5261 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
5262 text_view->selection_drag_handler = 0;
5264 if (text_view->scroll_timeout != 0)
5266 gtk_timeout_remove (text_view->scroll_timeout);
5267 text_view->scroll_timeout = 0;
5270 /* one last update to current position */
5271 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5272 move_mark_to_pointer_and_scroll (text_view, "insert");
5274 gtk_grab_remove (GTK_WIDGET (text_view));
5284 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5285 GtkTextAttributes *values,
5288 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5289 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5292 pango_font_description_free (values->font);
5294 values->font = pango_font_description_copy (style->font_desc);
5298 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5300 if (text_view->layout)
5302 gboolean split_cursor;
5303 GtkTextDirection new_dir;
5304 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5306 g_object_get (G_OBJECT (settings),
5307 "gtk-split-cursor", &split_cursor,
5311 new_dir = GTK_TEXT_DIR_NONE;
5315 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5316 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5317 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5320 if (text_view->layout->cursor_direction != new_dir)
5321 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5326 gtk_text_view_ensure_layout (GtkTextView *text_view)
5330 widget = GTK_WIDGET (text_view);
5332 if (text_view->layout == NULL)
5334 GtkTextAttributes *style;
5335 PangoContext *ltr_context, *rtl_context;
5338 DV(g_print(G_STRLOC"\n"));
5340 text_view->layout = gtk_text_layout_new ();
5342 g_signal_connect (text_view->layout,
5344 G_CALLBACK (invalidated_handler),
5347 g_signal_connect (text_view->layout,
5349 G_CALLBACK (changed_handler),
5352 g_signal_connect (text_view->layout,
5354 G_CALLBACK (gtk_text_view_child_allocated),
5357 if (get_buffer (text_view))
5358 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5360 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5361 gtk_text_view_pend_cursor_blink (text_view);
5363 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5365 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5366 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5367 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5368 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5370 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5372 g_object_unref (ltr_context);
5373 g_object_unref (rtl_context);
5375 gtk_text_view_check_keymap_direction (text_view);
5377 style = gtk_text_attributes_new ();
5379 gtk_widget_ensure_style (widget);
5380 gtk_text_view_set_attributes_from_style (text_view,
5381 style, widget->style);
5383 style->pixels_above_lines = text_view->pixels_above_lines;
5384 style->pixels_below_lines = text_view->pixels_below_lines;
5385 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5386 style->left_margin = text_view->left_margin;
5387 style->right_margin = text_view->right_margin;
5388 style->indent = text_view->indent;
5389 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5391 style->wrap_mode = text_view->wrap_mode;
5392 style->justification = text_view->justify;
5393 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5395 gtk_text_layout_set_default_style (text_view->layout, style);
5397 gtk_text_attributes_unref (style);
5399 /* Set layout for all anchored children */
5401 tmp_list = text_view->children;
5402 while (tmp_list != NULL)
5404 GtkTextViewChild *vc = tmp_list->data;
5408 gtk_text_anchored_child_set_layout (vc->widget,
5410 /* vc may now be invalid! */
5413 tmp_list = g_slist_next (tmp_list);
5416 gtk_text_view_invalidate (text_view);
5421 * gtk_text_view_get_default_attributes:
5422 * @text_view: a #GtkTextView
5424 * Obtains a copy of the default text attributes. These are the
5425 * attributes used for text unless a tag overrides them.
5426 * You'd typically pass the default attributes in to
5427 * gtk_text_iter_get_attributes() in order to get the
5428 * attributes in effect at a given text position.
5430 * The return value is a copy owned by the caller of this function,
5431 * and should be freed.
5433 * Return value: a new #GtkTextAttributes
5436 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5438 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5440 gtk_text_view_ensure_layout (text_view);
5442 return gtk_text_attributes_copy (text_view->layout->default_style);
5446 gtk_text_view_destroy_layout (GtkTextView *text_view)
5448 if (text_view->layout)
5452 gtk_text_view_remove_validate_idles (text_view);
5454 g_signal_handlers_disconnect_by_func (text_view->layout,
5455 invalidated_handler,
5457 g_signal_handlers_disconnect_by_func (text_view->layout,
5461 /* Remove layout from all anchored children */
5462 tmp_list = text_view->children;
5463 while (tmp_list != NULL)
5465 GtkTextViewChild *vc = tmp_list->data;
5469 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5470 /* vc may now be invalid! */
5473 tmp_list = g_slist_next (tmp_list);
5476 gtk_text_view_stop_cursor_blink (text_view);
5477 gtk_text_view_end_selection_drag (text_view, NULL);
5479 g_object_unref (text_view->layout);
5480 text_view->layout = NULL;
5485 gtk_text_view_reset_im_context (GtkTextView *text_view)
5487 if (text_view->need_im_reset)
5489 text_view->need_im_reset = FALSE;
5490 gtk_im_context_reset (text_view->im_context);
5499 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5500 const GtkTextIter *iter,
5501 GdkEventMotion *event)
5503 GdkDragContext *context;
5504 GtkTargetList *target_list;
5506 text_view->drag_start_x = -1;
5507 text_view->drag_start_y = -1;
5508 text_view->pending_place_cursor_button = 0;
5510 target_list = gtk_target_list_new (target_table,
5511 G_N_ELEMENTS (target_table));
5513 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5514 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5515 1, (GdkEvent*)event);
5517 gtk_target_list_unref (target_list);
5519 gtk_drag_set_icon_default (context);
5523 gtk_text_view_drag_begin (GtkWidget *widget,
5524 GdkDragContext *context)
5530 gtk_text_view_drag_end (GtkWidget *widget,
5531 GdkDragContext *context)
5533 GtkTextView *text_view;
5535 text_view = GTK_TEXT_VIEW (widget);
5539 gtk_text_view_drag_data_get (GtkWidget *widget,
5540 GdkDragContext *context,
5541 GtkSelectionData *selection_data,
5545 GtkTextView *text_view;
5547 text_view = GTK_TEXT_VIEW (widget);
5549 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5551 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5553 gtk_selection_data_set (selection_data,
5554 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5567 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5570 /* Extract the selected text */
5571 str = gtk_text_iter_get_visible_text (&start, &end);
5576 gtk_selection_data_set_text (selection_data, str, -1);
5583 gtk_text_view_drag_data_delete (GtkWidget *widget,
5584 GdkDragContext *context)
5586 GtkTextView *text_view;
5588 text_view = GTK_TEXT_VIEW (widget);
5590 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5591 TRUE, GTK_TEXT_VIEW (widget)->editable);
5595 gtk_text_view_drag_leave (GtkWidget *widget,
5596 GdkDragContext *context,
5599 GtkTextView *text_view;
5601 text_view = GTK_TEXT_VIEW (widget);
5603 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5605 if (text_view->scroll_timeout != 0)
5606 gtk_timeout_remove (text_view->scroll_timeout);
5608 text_view->scroll_timeout = 0;
5612 gtk_text_view_drag_motion (GtkWidget *widget,
5613 GdkDragContext *context,
5618 GtkTextIter newplace;
5619 GtkTextView *text_view;
5622 GdkRectangle target_rect;
5624 GdkDragAction suggested_action = 0;
5626 text_view = GTK_TEXT_VIEW (widget);
5628 target_rect = text_view->text_window->allocation;
5630 if (x < target_rect.x ||
5631 y < target_rect.y ||
5632 x > (target_rect.x + target_rect.width) ||
5633 y > (target_rect.y + target_rect.height))
5634 return FALSE; /* outside the text window, allow parent widgets to handle event */
5636 gtk_text_view_window_to_buffer_coords (text_view,
5637 GTK_TEXT_WINDOW_WIDGET,
5641 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5645 if (gtk_drag_dest_find_target (widget, context,
5646 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5648 /* can't accept any of the offered targets */
5650 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5652 gtk_text_iter_compare (&newplace, &start) >= 0 &&
5653 gtk_text_iter_compare (&newplace, &end) <= 0)
5655 /* We're inside the selection. */
5659 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5661 GtkWidget *source_widget;
5663 suggested_action = context->suggested_action;
5665 source_widget = gtk_drag_get_source_widget (context);
5667 if (source_widget == widget)
5669 /* Default to MOVE, unless the user has
5670 * pressed ctrl or alt to affect available actions
5672 if ((context->actions & GDK_ACTION_MOVE) != 0)
5673 suggested_action = GDK_ACTION_MOVE;
5678 /* Can't drop here. */
5682 if (suggested_action != 0)
5684 gtk_text_mark_set_visible (text_view->dnd_mark,
5685 text_view->cursor_visible);
5687 gdk_drag_status (context, suggested_action, time);
5691 gdk_drag_status (context, 0, time);
5692 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5695 gtk_text_buffer_move_mark (get_buffer (text_view),
5696 text_view->dnd_mark,
5699 DV(g_print (G_STRLOC": scrolling to mark\n"));
5700 gtk_text_view_scroll_to_mark (text_view,
5701 text_view->dnd_mark,
5702 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5704 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5705 gtk_timeout_remove (text_view->scroll_timeout);
5707 text_view->scroll_timeout =
5708 gtk_timeout_add (50, drag_scan_timeout, text_view);
5710 /* TRUE return means don't propagate the drag motion to parent
5711 * widgets that may also be drop sites.
5717 gtk_text_view_drag_drop (GtkWidget *widget,
5718 GdkDragContext *context,
5723 GtkTextView *text_view;
5724 GtkTextIter drop_point;
5725 GdkAtom target = GDK_NONE;
5727 text_view = GTK_TEXT_VIEW (widget);
5729 if (text_view->scroll_timeout != 0)
5730 gtk_timeout_remove (text_view->scroll_timeout);
5732 text_view->scroll_timeout = 0;
5734 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5736 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5738 text_view->dnd_mark);
5740 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5741 target = gtk_drag_dest_find_target (widget, context, NULL);
5743 if (target != GDK_NONE)
5744 gtk_drag_get_data (widget, context, target, time);
5746 gtk_drag_finish (context, FALSE, FALSE, time);
5752 insert_text_data (GtkTextView *text_view,
5753 GtkTextIter *drop_point,
5754 GtkSelectionData *selection_data)
5758 str = gtk_selection_data_get_text (selection_data);
5762 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5763 drop_point, str, -1,
5764 text_view->editable);
5770 gtk_text_view_drag_data_received (GtkWidget *widget,
5771 GdkDragContext *context,
5774 GtkSelectionData *selection_data,
5778 GtkTextIter drop_point;
5779 GtkTextView *text_view;
5780 gboolean success = FALSE;
5782 text_view = GTK_TEXT_VIEW (widget);
5784 if (!text_view->dnd_mark)
5787 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5789 text_view->dnd_mark);
5791 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5794 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5796 GtkTextBuffer *src_buffer = NULL;
5797 GtkTextIter start, end;
5798 gboolean copy_tags = TRUE;
5800 if (selection_data->length != sizeof (src_buffer))
5803 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5805 if (src_buffer == NULL)
5808 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5810 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5811 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5814 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5819 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5823 text_view->editable);
5828 str = gtk_text_iter_get_visible_text (&start, &end);
5829 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5830 &drop_point, str, -1,
5831 text_view->editable);
5837 insert_text_data (text_view, &drop_point, selection_data);
5842 gtk_drag_finish (context, success,
5843 success && context->action == GDK_ACTION_MOVE,
5847 static GtkAdjustment*
5848 get_hadjustment (GtkTextView *text_view)
5850 if (text_view->hadjustment == NULL)
5851 gtk_text_view_set_scroll_adjustments (text_view,
5852 NULL, /* forces creation */
5853 text_view->vadjustment);
5855 return text_view->hadjustment;
5858 static GtkAdjustment*
5859 get_vadjustment (GtkTextView *text_view)
5861 if (text_view->vadjustment == NULL)
5862 gtk_text_view_set_scroll_adjustments (text_view,
5863 text_view->hadjustment,
5864 NULL); /* forces creation */
5865 return text_view->vadjustment;
5870 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5871 GtkAdjustment *hadj,
5872 GtkAdjustment *vadj)
5874 gboolean need_adjust = FALSE;
5876 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5879 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5881 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5883 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5885 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5887 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5889 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
5890 gtk_text_view_value_changed,
5892 g_object_unref (text_view->hadjustment);
5895 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5897 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
5898 gtk_text_view_value_changed,
5900 g_object_unref (text_view->vadjustment);
5903 if (text_view->hadjustment != hadj)
5905 text_view->hadjustment = hadj;
5906 g_object_ref (text_view->hadjustment);
5907 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5909 g_signal_connect (text_view->hadjustment, "value_changed",
5910 G_CALLBACK (gtk_text_view_value_changed),
5915 if (text_view->vadjustment != vadj)
5917 text_view->vadjustment = vadj;
5918 g_object_ref (text_view->vadjustment);
5919 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5921 g_signal_connect (text_view->vadjustment, "value_changed",
5922 G_CALLBACK (gtk_text_view_value_changed),
5928 gtk_text_view_value_changed (NULL, text_view);
5931 /* FIXME this adjust_allocation is a big cut-and-paste from
5932 * GtkCList, needs to be some "official" way to do this
5942 /* The window to which widget->window is relative */
5943 #define ALLOCATION_WINDOW(widget) \
5944 (GTK_WIDGET_NO_WINDOW (widget) ? \
5945 (widget)->window : \
5946 gdk_window_get_parent ((widget)->window))
5949 adjust_allocation_recurse (GtkWidget *widget,
5952 ScrollData *scroll_data = data;
5954 /* Need to really size allocate instead of just poking
5955 * into widget->allocation if the widget is not realized.
5956 * FIXME someone figure out why this was.
5958 if (!GTK_WIDGET_REALIZED (widget))
5960 if (GTK_WIDGET_VISIBLE (widget))
5962 GdkRectangle tmp_rectangle = widget->allocation;
5963 tmp_rectangle.x += scroll_data->dx;
5964 tmp_rectangle.y += scroll_data->dy;
5966 gtk_widget_size_allocate (widget, &tmp_rectangle);
5971 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5973 widget->allocation.x += scroll_data->dx;
5974 widget->allocation.y += scroll_data->dy;
5976 if (GTK_IS_CONTAINER (widget))
5977 gtk_container_forall (GTK_CONTAINER (widget),
5978 adjust_allocation_recurse,
5985 adjust_allocation (GtkWidget *widget,
5989 ScrollData scroll_data;
5991 if (GTK_WIDGET_REALIZED (widget))
5992 scroll_data.window = ALLOCATION_WINDOW (widget);
5994 scroll_data.window = NULL;
5996 scroll_data.dx = dx;
5997 scroll_data.dy = dy;
5999 adjust_allocation_recurse (widget, &scroll_data);
6003 gtk_text_view_value_changed (GtkAdjustment *adj,
6004 GtkTextView *text_view)
6011 /* Note that we oddly call this function with adj == NULL
6015 text_view->onscreen_validated = FALSE;
6017 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
6018 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
6019 adj ? adj->value : 0.0));
6021 if (adj == text_view->hadjustment)
6023 dx = text_view->xoffset - (gint)adj->value;
6024 text_view->xoffset = adj->value;
6026 else if (adj == text_view->vadjustment)
6028 dy = text_view->yoffset - (gint)adj->value;
6029 text_view->yoffset = adj->value;
6031 if (text_view->layout)
6033 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
6035 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
6037 text_view->first_para_pixels = adj->value - line_top;
6041 if (dx != 0 || dy != 0)
6045 if (GTK_WIDGET_REALIZED (text_view))
6049 if (text_view->left_window)
6050 text_window_scroll (text_view->left_window, 0, dy);
6051 if (text_view->right_window)
6052 text_window_scroll (text_view->right_window, 0, dy);
6057 if (text_view->top_window)
6058 text_window_scroll (text_view->top_window, dx, 0);
6059 if (text_view->bottom_window)
6060 text_window_scroll (text_view->bottom_window, dx, 0);
6063 /* It looks nicer to scroll the main area last, because
6064 * it takes a while, and making the side areas update
6065 * afterward emphasizes the slowness of scrolling the
6068 text_window_scroll (text_view->text_window, dx, dy);
6071 /* Children are now "moved" in the text window, poke
6072 * into widget->allocation for each child
6074 tmp_list = text_view->children;
6075 while (tmp_list != NULL)
6077 GtkTextViewChild *child = tmp_list->data;
6080 adjust_allocation (child->widget, dx, dy);
6082 tmp_list = g_slist_next (tmp_list);
6086 /* This could result in invalidation, which would install the
6087 * first_validate_idle, which would validate onscreen;
6088 * but we're going to go ahead and validate here, so
6089 * first_validate_idle shouldn't have anything to do.
6091 gtk_text_view_update_layout_width (text_view);
6093 /* note that validation of onscreen could invoke this function
6094 * recursively, by scrolling to maintain first_para, or in response
6095 * to updating the layout width, however there is no problem with
6096 * that, or shouldn't be.
6098 gtk_text_view_validate_onscreen (text_view);
6100 /* process exposes */
6101 if (GTK_WIDGET_REALIZED (text_view))
6103 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6105 if (text_view->left_window)
6106 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6108 if (text_view->right_window)
6109 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6111 if (text_view->top_window)
6112 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6114 if (text_view->bottom_window)
6115 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6117 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6120 /* If this got installed, get rid of it, it's just a waste of time. */
6121 if (text_view->first_validate_idle != 0)
6123 g_source_remove (text_view->first_validate_idle);
6124 text_view->first_validate_idle = 0;
6127 gtk_text_view_update_im_spot_location (text_view);
6129 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6133 gtk_text_view_commit_handler (GtkIMContext *context,
6135 GtkTextView *text_view)
6137 gtk_text_view_commit_text (text_view, str);
6141 gtk_text_view_commit_text (GtkTextView *text_view,
6144 gboolean had_selection;
6146 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6148 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6151 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6152 text_view->editable);
6154 if (!strcmp (str, "\n"))
6156 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6157 text_view->editable);
6161 if (!had_selection && text_view->overwrite_mode)
6165 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6167 gtk_text_buffer_get_mark (get_buffer (text_view),
6169 if (!gtk_text_iter_ends_line (&insert))
6170 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6172 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6173 text_view->editable);
6176 gtk_text_buffer_end_user_action (get_buffer (text_view));
6178 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6179 gtk_text_view_scroll_mark_onscreen (text_view,
6180 gtk_text_buffer_get_mark (get_buffer (text_view),
6185 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6186 GtkTextView *text_view)
6189 PangoAttrList *attrs;
6192 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6193 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6195 pango_attr_list_unref (attrs);
6200 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6201 GtkTextView *text_view)
6208 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6209 gtk_text_buffer_get_insert (text_view->buffer));
6212 pos = gtk_text_iter_get_line_index (&start);
6213 gtk_text_iter_set_line_offset (&start, 0);
6214 gtk_text_iter_forward_to_line_end (&end);
6216 text = gtk_text_iter_get_slice (&start, &end);
6217 gtk_im_context_set_surrounding (context, text, -1, pos);
6224 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6227 GtkTextView *text_view)
6232 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6233 gtk_text_buffer_get_insert (text_view->buffer));
6236 gtk_text_iter_forward_chars (&start, offset);
6237 gtk_text_iter_forward_chars (&end, offset + n_chars);
6239 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6245 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6246 const GtkTextIter *location,
6250 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6251 gboolean need_reset = FALSE;
6253 if (mark == gtk_text_buffer_get_insert (buffer))
6255 text_view->virtual_cursor_x = -1;
6256 text_view->virtual_cursor_y = -1;
6257 gtk_text_view_update_im_spot_location (text_view);
6260 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6266 gtk_text_view_reset_im_context (text_view);
6270 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6274 GdkRectangle strong_pos;
6277 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6278 gtk_text_buffer_get_mark (get_buffer (text_view),
6281 if ((x && text_view->virtual_cursor_x == -1) ||
6282 (y && text_view->virtual_cursor_y == -1))
6283 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6287 if (text_view->virtual_cursor_x != -1)
6288 *x = text_view->virtual_cursor_x;
6295 if (text_view->virtual_cursor_x != -1)
6296 *y = text_view->virtual_cursor_y;
6298 *y = strong_pos.y + strong_pos.height / 2;
6303 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6307 GdkRectangle strong_pos;
6310 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6311 gtk_text_buffer_get_mark (get_buffer (text_view),
6314 if (x == -1 || y == -1)
6315 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6317 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6318 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6321 /* Quick hack of a popup menu
6324 activate_cb (GtkWidget *menuitem,
6325 GtkTextView *text_view)
6327 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6328 g_signal_emit_by_name (text_view, signal);
6332 append_action_signal (GtkTextView *text_view,
6334 const gchar *stock_id,
6335 const gchar *signal,
6338 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6340 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6341 g_signal_connect (menuitem, "activate",
6342 G_CALLBACK (activate_cb), text_view);
6344 gtk_widget_set_sensitive (menuitem, sensitive);
6346 gtk_widget_show (menuitem);
6347 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6351 popup_menu_detach (GtkWidget *attach_widget,
6354 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6358 popup_position_func (GtkMenu *menu,
6364 GtkTextView *text_view;
6366 GdkRectangle cursor_rect;
6367 GdkRectangle onscreen_rect;
6368 gint root_x, root_y;
6373 text_view = GTK_TEXT_VIEW (user_data);
6374 widget = GTK_WIDGET (text_view);
6376 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6378 screen = gtk_widget_get_screen (widget);
6380 gdk_window_get_origin (widget->window, &root_x, &root_y);
6382 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6384 gtk_text_buffer_get_insert (get_buffer (text_view)));
6386 gtk_text_view_get_iter_location (text_view,
6390 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6392 gtk_widget_size_request (text_view->popup_menu, &req);
6394 /* can't use rectangle_intersect since cursor rect can have 0 width */
6395 if (cursor_rect.x >= onscreen_rect.x &&
6396 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6397 cursor_rect.y >= onscreen_rect.y &&
6398 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6400 gtk_text_view_buffer_to_window_coords (text_view,
6401 GTK_TEXT_WINDOW_WIDGET,
6402 cursor_rect.x, cursor_rect.y,
6403 &cursor_rect.x, &cursor_rect.y);
6405 *x = root_x + cursor_rect.x + cursor_rect.width;
6406 *y = root_y + cursor_rect.y + cursor_rect.height;
6410 /* Just center the menu, since cursor is offscreen. */
6411 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6412 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6416 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6417 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6419 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6420 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6425 GtkTextView *text_view;
6431 range_contains_editable_text (const GtkTextIter *start,
6432 const GtkTextIter *end,
6433 gboolean default_editability)
6435 GtkTextIter iter = *start;
6437 while (gtk_text_iter_compare (&iter, end) < 0)
6439 if (gtk_text_iter_editable (&iter, default_editability))
6442 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6449 unichar_chosen_func (const char *text,
6452 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6454 gtk_text_view_commit_text (text_view, text);
6458 popup_targets_received (GtkClipboard *clipboard,
6459 GtkSelectionData *data,
6462 PopupInfo *info = user_data;
6463 GtkTextView *text_view = info->text_view;
6465 if (GTK_WIDGET_REALIZED (text_view))
6467 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6468 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6470 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6471 GtkWidget *menuitem;
6473 gboolean have_selection;
6474 gboolean can_insert;
6476 GtkTextIter sel_start, sel_end;
6478 if (text_view->popup_menu)
6479 gtk_widget_destroy (text_view->popup_menu);
6481 text_view->popup_menu = gtk_menu_new ();
6483 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6484 GTK_WIDGET (text_view),
6487 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6488 &sel_start, &sel_end);
6490 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6492 gtk_text_buffer_get_insert (get_buffer (text_view)));
6494 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6496 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6498 range_contains_editable_text (&sel_start, &sel_end,
6499 text_view->editable));
6500 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6502 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6503 can_insert && clipboard_contains_text);
6505 menuitem = gtk_separator_menu_item_new ();
6506 gtk_widget_show (menuitem);
6507 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6509 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6510 gtk_widget_show (menuitem);
6511 submenu = gtk_menu_new ();
6512 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6513 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6515 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6516 GTK_MENU_SHELL (submenu));
6518 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6519 gtk_widget_show (menuitem);
6520 gtk_widget_set_sensitive (menuitem, can_insert);
6522 submenu = gtk_menu_new ();
6523 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6524 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6526 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6527 unichar_chosen_func,
6530 g_signal_emit (text_view,
6531 signals[POPULATE_POPUP],
6533 text_view->popup_menu);
6536 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6538 info->button, info->time);
6541 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6542 popup_position_func, text_view,
6543 0, gtk_get_current_event_time ());
6544 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
6548 g_object_unref (text_view);
6553 gtk_text_view_do_popup (GtkTextView *text_view,
6554 GdkEventButton *event)
6556 PopupInfo *info = g_new (PopupInfo, 1);
6558 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6559 gtk_text_view_end_selection_drag (text_view, event);
6561 /* In order to know what entries we should make sensitive, we
6562 * ask for the current targets of the clipboard, and when
6563 * we get them, then we actually pop up the menu.
6565 info->text_view = g_object_ref (text_view);
6569 info->button = event->button;
6570 info->time = event->time;
6575 info->time = gtk_get_current_event_time ();
6578 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6579 GDK_SELECTION_CLIPBOARD),
6580 gdk_atom_intern ("TARGETS", FALSE),
6581 popup_targets_received,
6586 gtk_text_view_popup_menu (GtkWidget *widget)
6588 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6592 /* Child GdkWindows */
6595 static GtkTextWindow*
6596 text_window_new (GtkTextWindowType type,
6599 gint height_request)
6603 win = g_new (GtkTextWindow, 1);
6606 win->widget = widget;
6608 win->bin_window = NULL;
6609 win->requisition.width = width_request;
6610 win->requisition.height = height_request;
6611 win->allocation.width = width_request;
6612 win->allocation.height = height_request;
6613 win->allocation.x = 0;
6614 win->allocation.y = 0;
6620 text_window_free (GtkTextWindow *win)
6623 text_window_unrealize (win);
6629 text_window_realize (GtkTextWindow *win,
6632 GdkWindowAttr attributes;
6633 gint attributes_mask;
6636 attributes.window_type = GDK_WINDOW_CHILD;
6637 attributes.x = win->allocation.x;
6638 attributes.y = win->allocation.y;
6639 attributes.width = win->allocation.width;
6640 attributes.height = win->allocation.height;
6641 attributes.wclass = GDK_INPUT_OUTPUT;
6642 attributes.visual = gtk_widget_get_visual (win->widget);
6643 attributes.colormap = gtk_widget_get_colormap (win->widget);
6644 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6646 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6648 win->window = gdk_window_new (parent,
6652 gdk_window_show (win->window);
6653 gdk_window_set_user_data (win->window, win->widget);
6657 attributes.width = win->allocation.width;
6658 attributes.height = win->allocation.height;
6659 attributes.event_mask = (GDK_EXPOSURE_MASK |
6661 GDK_KEY_PRESS_MASK |
6662 GDK_BUTTON_PRESS_MASK |
6663 GDK_BUTTON_RELEASE_MASK |
6664 GDK_POINTER_MOTION_MASK |
6665 GDK_POINTER_MOTION_HINT_MASK |
6666 gtk_widget_get_events (win->widget));
6668 win->bin_window = gdk_window_new (win->window,
6672 gdk_window_show (win->bin_window);
6673 gdk_window_set_user_data (win->bin_window, win->widget);
6675 if (win->type == GTK_TEXT_WINDOW_TEXT)
6678 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
6680 gdk_window_set_cursor (win->bin_window, cursor);
6681 gdk_cursor_unref (cursor);
6683 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6687 gdk_window_set_background (win->bin_window,
6688 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6692 gdk_window_set_background (win->bin_window,
6693 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6696 g_object_set_qdata (G_OBJECT (win->window),
6697 g_quark_from_static_string ("gtk-text-view-text-window"),
6700 g_object_set_qdata (G_OBJECT (win->bin_window),
6701 g_quark_from_static_string ("gtk-text-view-text-window"),
6706 text_window_unrealize (GtkTextWindow *win)
6708 if (win->type == GTK_TEXT_WINDOW_TEXT)
6710 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6714 gdk_window_set_user_data (win->window, NULL);
6715 gdk_window_set_user_data (win->bin_window, NULL);
6716 gdk_window_destroy (win->bin_window);
6717 gdk_window_destroy (win->window);
6719 win->bin_window = NULL;
6723 text_window_size_allocate (GtkTextWindow *win,
6726 win->allocation = *rect;
6730 gdk_window_move_resize (win->window,
6732 rect->width, rect->height);
6734 gdk_window_resize (win->bin_window,
6735 rect->width, rect->height);
6740 text_window_scroll (GtkTextWindow *win,
6744 if (dx != 0 || dy != 0)
6746 gdk_window_scroll (win->bin_window, dx, dy);
6751 text_window_invalidate_rect (GtkTextWindow *win,
6754 GdkRectangle window_rect;
6756 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6763 window_rect.width = rect->width;
6764 window_rect.height = rect->height;
6766 /* Adjust the rect as appropriate */
6770 case GTK_TEXT_WINDOW_TEXT:
6773 case GTK_TEXT_WINDOW_LEFT:
6774 case GTK_TEXT_WINDOW_RIGHT:
6776 window_rect.width = win->allocation.width;
6779 case GTK_TEXT_WINDOW_TOP:
6780 case GTK_TEXT_WINDOW_BOTTOM:
6782 window_rect.height = win->allocation.height;
6786 g_warning ("%s: bug!", G_STRLOC);
6791 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6795 GdkColor color = { 0, 65535, 0, 0 };
6796 GdkGC *gc = gdk_gc_new (win->bin_window);
6797 gdk_gc_set_rgb_fg_color (gc, &color);
6798 gdk_draw_rectangle (win->bin_window,
6799 gc, TRUE, window_rect.x, window_rect.y,
6800 window_rect.width, window_rect.height);
6801 g_object_unref (gc);
6807 text_window_get_width (GtkTextWindow *win)
6809 return win->allocation.width;
6813 text_window_get_height (GtkTextWindow *win)
6815 return win->allocation.height;
6822 * gtk_text_view_get_window:
6823 * @text_view: a #GtkTextView
6824 * @win: window to get
6826 * Retrieves the #GdkWindow corresponding to an area of the text view;
6827 * possible windows include the overall widget window, child windows
6828 * on the left, right, top, bottom, and the window that displays the
6829 * text buffer. Windows are %NULL and nonexistent if their width or
6830 * height is 0, and are nonexistent before the widget has been
6833 * Return value: a #GdkWindow, or %NULL
6836 gtk_text_view_get_window (GtkTextView *text_view,
6837 GtkTextWindowType win)
6839 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6843 case GTK_TEXT_WINDOW_WIDGET:
6844 return GTK_WIDGET (text_view)->window;
6847 case GTK_TEXT_WINDOW_TEXT:
6848 return text_view->text_window->bin_window;
6851 case GTK_TEXT_WINDOW_LEFT:
6852 if (text_view->left_window)
6853 return text_view->left_window->bin_window;
6858 case GTK_TEXT_WINDOW_RIGHT:
6859 if (text_view->right_window)
6860 return text_view->right_window->bin_window;
6865 case GTK_TEXT_WINDOW_TOP:
6866 if (text_view->top_window)
6867 return text_view->top_window->bin_window;
6872 case GTK_TEXT_WINDOW_BOTTOM:
6873 if (text_view->bottom_window)
6874 return text_view->bottom_window->bin_window;
6879 case GTK_TEXT_WINDOW_PRIVATE:
6880 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6885 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6890 * gtk_text_view_get_window_type:
6891 * @text_view: a #GtkTextView
6892 * @window: a window type
6894 * Usually used to find out which window an event corresponds to.
6895 * If you connect to an event signal on @text_view, this function
6896 * should be called on <literal>event->window</literal> to
6897 * see which window it was.
6899 * Return value: the window type.
6902 gtk_text_view_get_window_type (GtkTextView *text_view,
6907 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6908 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6910 if (window == GTK_WIDGET (text_view)->window)
6911 return GTK_TEXT_WINDOW_WIDGET;
6913 win = g_object_get_qdata (G_OBJECT (window),
6914 g_quark_try_string ("gtk-text-view-text-window"));
6920 return GTK_TEXT_WINDOW_PRIVATE;
6925 buffer_to_widget (GtkTextView *text_view,
6933 *window_x = buffer_x - text_view->xoffset;
6934 *window_x += text_view->text_window->allocation.x;
6939 *window_y = buffer_y - text_view->yoffset;
6940 *window_y += text_view->text_window->allocation.y;
6945 widget_to_text_window (GtkTextWindow *win,
6952 *window_x = widget_x - win->allocation.x;
6955 *window_y = widget_y - win->allocation.y;
6959 buffer_to_text_window (GtkTextView *text_view,
6968 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6969 "for a nonexistent or private child window of GtkTextView");
6973 buffer_to_widget (text_view,
6975 window_x, window_y);
6977 widget_to_text_window (win,
6978 window_x ? *window_x : 0,
6979 window_y ? *window_y : 0,
6985 * gtk_text_view_buffer_to_window_coords:
6986 * @text_view: a #GtkTextView
6987 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6988 * @buffer_x: buffer x coordinate
6989 * @buffer_y: buffer y coordinate
6990 * @window_x: window x coordinate return location
6991 * @window_y: window y coordinate return location
6993 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6994 * @win, and stores the result in (@window_x, @window_y).
6996 * Note that you can't convert coordinates for a nonexisting window (see
6997 * gtk_text_view_set_border_window_size()).
7000 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
7001 GtkTextWindowType win,
7007 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7011 case GTK_TEXT_WINDOW_WIDGET:
7012 buffer_to_widget (text_view,
7014 window_x, window_y);
7017 case GTK_TEXT_WINDOW_TEXT:
7019 *window_x = buffer_x - text_view->xoffset;
7021 *window_y = buffer_y - text_view->yoffset;
7024 case GTK_TEXT_WINDOW_LEFT:
7025 buffer_to_text_window (text_view,
7026 text_view->left_window,
7028 window_x, window_y);
7031 case GTK_TEXT_WINDOW_RIGHT:
7032 buffer_to_text_window (text_view,
7033 text_view->right_window,
7035 window_x, window_y);
7038 case GTK_TEXT_WINDOW_TOP:
7039 buffer_to_text_window (text_view,
7040 text_view->top_window,
7042 window_x, window_y);
7045 case GTK_TEXT_WINDOW_BOTTOM:
7046 buffer_to_text_window (text_view,
7047 text_view->bottom_window,
7049 window_x, window_y);
7052 case GTK_TEXT_WINDOW_PRIVATE:
7053 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7057 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7063 widget_to_buffer (GtkTextView *text_view,
7071 *buffer_x = widget_x + text_view->xoffset;
7072 *buffer_x -= text_view->text_window->allocation.x;
7077 *buffer_y = widget_y + text_view->yoffset;
7078 *buffer_y -= text_view->text_window->allocation.y;
7083 text_window_to_widget (GtkTextWindow *win,
7090 *widget_x = window_x + win->allocation.x;
7093 *widget_y = window_y + win->allocation.y;
7097 text_window_to_buffer (GtkTextView *text_view,
7106 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7107 "coordinates for a nonexistent child window.");
7111 text_window_to_widget (win,
7117 widget_to_buffer (text_view,
7118 buffer_x ? *buffer_x : 0,
7119 buffer_y ? *buffer_y : 0,
7125 * gtk_text_view_window_to_buffer_coords:
7126 * @text_view: a #GtkTextView
7127 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7128 * @window_x: window x coordinate
7129 * @window_y: window y coordinate
7130 * @buffer_x: buffer x coordinate return location
7131 * @buffer_y: buffer y coordinate return location
7133 * Converts coordinates on the window identified by @win to buffer
7134 * coordinates, storing the result in (@buffer_x,@buffer_y).
7136 * Note that you can't convert coordinates for a nonexisting window (see
7137 * gtk_text_view_set_border_window_size()).
7140 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7141 GtkTextWindowType win,
7147 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7151 case GTK_TEXT_WINDOW_WIDGET:
7152 widget_to_buffer (text_view,
7154 buffer_x, buffer_y);
7157 case GTK_TEXT_WINDOW_TEXT:
7159 *buffer_x = window_x + text_view->xoffset;
7161 *buffer_y = window_y + text_view->yoffset;
7164 case GTK_TEXT_WINDOW_LEFT:
7165 text_window_to_buffer (text_view,
7166 text_view->left_window,
7168 buffer_x, buffer_y);
7171 case GTK_TEXT_WINDOW_RIGHT:
7172 text_window_to_buffer (text_view,
7173 text_view->right_window,
7175 buffer_x, buffer_y);
7178 case GTK_TEXT_WINDOW_TOP:
7179 text_window_to_buffer (text_view,
7180 text_view->top_window,
7182 buffer_x, buffer_y);
7185 case GTK_TEXT_WINDOW_BOTTOM:
7186 text_window_to_buffer (text_view,
7187 text_view->bottom_window,
7189 buffer_x, buffer_y);
7192 case GTK_TEXT_WINDOW_PRIVATE:
7193 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7197 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7203 set_window_width (GtkTextView *text_view,
7205 GtkTextWindowType type,
7206 GtkTextWindow **winp)
7212 text_window_free (*winp);
7214 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7221 *winp = text_window_new (type,
7222 GTK_WIDGET (text_view),
7224 /* if the widget is already realized we need to realize the child manually */
7225 if (GTK_WIDGET_REALIZED (text_view))
7226 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7230 if ((*winp)->requisition.width == width)
7233 (*winp)->requisition.width = width;
7236 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7242 set_window_height (GtkTextView *text_view,
7244 GtkTextWindowType type,
7245 GtkTextWindow **winp)
7251 text_window_free (*winp);
7253 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7260 *winp = text_window_new (type,
7261 GTK_WIDGET (text_view),
7264 /* if the widget is already realized we need to realize the child manually */
7265 if (GTK_WIDGET_REALIZED (text_view))
7266 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7270 if ((*winp)->requisition.height == height)
7273 (*winp)->requisition.height = height;
7276 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7281 * gtk_text_view_set_border_window_size:
7282 * @text_view: a #GtkTextView
7283 * @type: window to affect
7284 * @size: width or height of the window
7286 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7287 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7288 * Automatically destroys the corresponding window if the size is set
7289 * to 0, and creates the window if the size is set to non-zero. This
7290 * function can only be used for the "border windows," it doesn't work
7291 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7292 * #GTK_TEXT_WINDOW_PRIVATE.
7295 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7296 GtkTextWindowType type,
7300 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7301 g_return_if_fail (size >= 0);
7305 case GTK_TEXT_WINDOW_LEFT:
7306 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7307 &text_view->left_window);
7310 case GTK_TEXT_WINDOW_RIGHT:
7311 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7312 &text_view->right_window);
7315 case GTK_TEXT_WINDOW_TOP:
7316 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7317 &text_view->top_window);
7320 case GTK_TEXT_WINDOW_BOTTOM:
7321 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7322 &text_view->bottom_window);
7326 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7332 * gtk_text_view_get_border_window_size:
7333 * @text_view: a #GtkTextView
7334 * @type: window to return size from
7336 * Gets the width of the specified border window. See
7337 * gtk_text_view_set_border_window_size().
7339 * Return value: width of window
7342 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7343 GtkTextWindowType type)
7345 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7349 case GTK_TEXT_WINDOW_LEFT:
7350 if (text_view->left_window)
7351 return text_view->left_window->requisition.width;
7353 case GTK_TEXT_WINDOW_RIGHT:
7354 if (text_view->right_window)
7355 return text_view->right_window->requisition.width;
7357 case GTK_TEXT_WINDOW_TOP:
7358 if (text_view->top_window)
7359 return text_view->top_window->requisition.height;
7361 case GTK_TEXT_WINDOW_BOTTOM:
7362 if (text_view->bottom_window)
7363 return text_view->bottom_window->requisition.height;
7366 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7377 static GtkTextViewChild*
7378 text_view_child_new_anchored (GtkWidget *child,
7379 GtkTextChildAnchor *anchor,
7380 GtkTextLayout *layout)
7382 GtkTextViewChild *vc;
7384 vc = g_new (GtkTextViewChild, 1);
7386 vc->type = GTK_TEXT_WINDOW_PRIVATE;
7388 vc->anchor = anchor;
7390 vc->from_top_of_line = 0;
7391 vc->from_left_of_buffer = 0;
7393 g_object_ref (vc->widget);
7394 g_object_ref (vc->anchor);
7396 g_object_set_data (G_OBJECT (child),
7397 "gtk-text-view-child",
7400 gtk_text_child_anchor_register_child (anchor, child, layout);
7405 static GtkTextViewChild*
7406 text_view_child_new_window (GtkWidget *child,
7407 GtkTextWindowType type,
7411 GtkTextViewChild *vc;
7413 vc = g_new (GtkTextViewChild, 1);
7418 vc->from_top_of_line = 0;
7419 vc->from_left_of_buffer = 0;
7421 g_object_ref (vc->widget);
7427 g_object_set_data (G_OBJECT (child),
7428 "gtk-text-view-child",
7435 text_view_child_free (GtkTextViewChild *child)
7437 g_object_set_data (G_OBJECT (child->widget),
7438 "gtk-text-view-child", NULL);
7442 gtk_text_child_anchor_unregister_child (child->anchor,
7444 g_object_unref (child->anchor);
7447 g_object_unref (child->widget);
7453 text_view_child_set_parent_window (GtkTextView *text_view,
7454 GtkTextViewChild *vc)
7457 gtk_widget_set_parent_window (vc->widget,
7458 text_view->text_window->bin_window);
7462 window = gtk_text_view_get_window (text_view,
7464 gtk_widget_set_parent_window (vc->widget, window);
7469 add_child (GtkTextView *text_view,
7470 GtkTextViewChild *vc)
7472 text_view->children = g_slist_prepend (text_view->children,
7475 if (GTK_WIDGET_REALIZED (text_view))
7476 text_view_child_set_parent_window (text_view, vc);
7478 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7482 * gtk_text_view_add_child_at_anchor:
7483 * @text_view: a #GtkTextView
7484 * @child: a #GtkWidget
7485 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7487 * Adds a child widget in the text buffer, at the given @anchor.
7491 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7493 GtkTextChildAnchor *anchor)
7495 GtkTextViewChild *vc;
7497 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7498 g_return_if_fail (GTK_IS_WIDGET (child));
7499 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7500 g_return_if_fail (child->parent == NULL);
7502 gtk_text_view_ensure_layout (text_view);
7504 vc = text_view_child_new_anchored (child, anchor,
7507 add_child (text_view, vc);
7509 g_assert (vc->widget == child);
7510 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7514 * gtk_text_view_add_child_in_window:
7515 * @text_view: a #GtkTextView
7516 * @child: a #GtkWidget
7517 * @which_window: which window the child should appear in
7518 * @xpos: X position of child in window coordinates
7519 * @ypos: Y position of child in window coordinates
7521 * Adds a child at fixed coordinates in one of the text widget's
7522 * windows. The window must have nonzero size (see
7523 * gtk_text_view_set_border_window_size()). Note that the child
7524 * coordinates are given relative to the #GdkWindow in question, and
7525 * that these coordinates have no sane relationship to scrolling. When
7526 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7527 * irrelevant, the child floats above all scrollable areas. But when
7528 * placing a child in one of the scrollable windows (border windows or
7529 * text window), you'll need to compute the child's correct position
7530 * in buffer coordinates any time scrolling occurs or buffer changes
7531 * occur, and then call gtk_text_view_move_child() to update the
7532 * child's position. Unfortunately there's no good way to detect that
7533 * scrolling has occurred, using the current API; a possible hack
7534 * would be to update all child positions when the scroll adjustments
7535 * change or the text buffer changes. See bug 64518 on
7536 * bugzilla.gnome.org for status of fixing this issue.
7540 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7542 GtkTextWindowType which_window,
7546 GtkTextViewChild *vc;
7548 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7549 g_return_if_fail (GTK_IS_WIDGET (child));
7550 g_return_if_fail (child->parent == NULL);
7552 vc = text_view_child_new_window (child, which_window,
7555 add_child (text_view, vc);
7557 g_assert (vc->widget == child);
7558 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7562 * gtk_text_view_move_child:
7563 * @text_view: a #GtkTextView
7564 * @child: child widget already added to the text view
7565 * @xpos: new X position in window coordinates
7566 * @ypos: new Y position in window coordinates
7568 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7572 gtk_text_view_move_child (GtkTextView *text_view,
7577 GtkTextViewChild *vc;
7579 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7580 g_return_if_fail (GTK_IS_WIDGET (child));
7581 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7583 vc = g_object_get_data (G_OBJECT (child),
7584 "gtk-text-view-child");
7586 g_assert (vc != NULL);
7588 if (vc->x == xpos &&
7595 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7596 gtk_widget_queue_resize (child);
7600 /* Iterator operations */
7603 * gtk_text_view_forward_display_line:
7604 * @text_view: a #GtkTextView
7605 * @iter: a #GtkTextIter
7607 * Moves the given @iter forward by one display (wrapped) line. A
7608 * display line is different from a paragraph. Paragraphs are
7609 * separated by newlines or other paragraph separator characters.
7610 * Display lines are created by line-wrapping a paragraph. If
7611 * wrapping is turned off, display lines and paragraphs will be the
7612 * same. Display lines are divided differently for each view, since
7613 * they depend on the view's width; paragraphs are the same in all
7614 * views, since they depend on the contents of the #GtkTextBuffer.
7616 * Return value: %TRUE if @iter was moved and is not on the end iterator
7619 gtk_text_view_forward_display_line (GtkTextView *text_view,
7622 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7623 g_return_val_if_fail (iter != NULL, FALSE);
7625 gtk_text_view_ensure_layout (text_view);
7627 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7631 * gtk_text_view_backward_display_line:
7632 * @text_view: a #GtkTextView
7633 * @iter: a #GtkTextIter
7635 * Moves the given @iter backward by one display (wrapped) line. A
7636 * display line is different from a paragraph. Paragraphs are
7637 * separated by newlines or other paragraph separator characters.
7638 * Display lines are created by line-wrapping a paragraph. If
7639 * wrapping is turned off, display lines and paragraphs will be the
7640 * same. Display lines are divided differently for each view, since
7641 * they depend on the view's width; paragraphs are the same in all
7642 * views, since they depend on the contents of the #GtkTextBuffer.
7644 * Return value: %TRUE if @iter was moved and is not on the end iterator
7647 gtk_text_view_backward_display_line (GtkTextView *text_view,
7650 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7651 g_return_val_if_fail (iter != NULL, FALSE);
7653 gtk_text_view_ensure_layout (text_view);
7655 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7659 * gtk_text_view_forward_display_line_end:
7660 * @text_view: a #GtkTextView
7661 * @iter: a #GtkTextIter
7663 * Moves the given @iter forward to the next display line end. A
7664 * display line is different from a paragraph. Paragraphs are
7665 * separated by newlines or other paragraph separator characters.
7666 * Display lines are created by line-wrapping a paragraph. If
7667 * wrapping is turned off, display lines and paragraphs will be the
7668 * same. Display lines are divided differently for each view, since
7669 * they depend on the view's width; paragraphs are the same in all
7670 * views, since they depend on the contents of the #GtkTextBuffer.
7672 * Return value: %TRUE if @iter was moved and is not on the end iterator
7675 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7678 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7679 g_return_val_if_fail (iter != NULL, FALSE);
7681 gtk_text_view_ensure_layout (text_view);
7683 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7687 * gtk_text_view_backward_display_line_start:
7688 * @text_view: a #GtkTextView
7689 * @iter: a #GtkTextIter
7691 * Moves the given @iter backward to the next display line start. A
7692 * display line is different from a paragraph. Paragraphs are
7693 * separated by newlines or other paragraph separator characters.
7694 * Display lines are created by line-wrapping a paragraph. If
7695 * wrapping is turned off, display lines and paragraphs will be the
7696 * same. Display lines are divided differently for each view, since
7697 * they depend on the view's width; paragraphs are the same in all
7698 * views, since they depend on the contents of the #GtkTextBuffer.
7700 * Return value: %TRUE if @iter was moved and is not on the end iterator
7703 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7706 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7707 g_return_val_if_fail (iter != NULL, FALSE);
7709 gtk_text_view_ensure_layout (text_view);
7711 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7715 * gtk_text_view_starts_display_line:
7716 * @text_view: a #GtkTextView
7717 * @iter: a #GtkTextIter
7719 * Determines whether @iter is at the start of a display line.
7720 * See gtk_text_view_forward_display_line() for an explanation of
7721 * display lines vs. paragraphs.
7723 * Return value: %TRUE if @iter begins a wrapped line
7726 gtk_text_view_starts_display_line (GtkTextView *text_view,
7727 const GtkTextIter *iter)
7729 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7730 g_return_val_if_fail (iter != NULL, FALSE);
7732 gtk_text_view_ensure_layout (text_view);
7734 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7738 * gtk_text_view_move_visually:
7739 * @text_view: a #GtkTextView
7740 * @iter: a #GtkTextIter
7741 * @count: number of characters to move (negative moves left, positive moves right)
7743 * Move the iterator a given number of characters visually, treating
7744 * it as the strong cursor position. If @count is positive, then the
7745 * new strong cursor position will be @count positions to the right of
7746 * the old cursor position. If @count is negative then the new strong
7747 * cursor position will be @count positions to the left of the old
7750 * In the presence of bidirection text, the correspondence
7751 * between logical and visual order will depend on the direction
7752 * of the current run, and there may be jumps when the cursor
7753 * is moved off of the end of a run.
7755 * Return value: %TRUE if @iter moved and is not on the end iterator
7758 gtk_text_view_move_visually (GtkTextView *text_view,
7762 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7763 g_return_val_if_fail (iter != NULL, FALSE);
7765 gtk_text_view_ensure_layout (text_view);
7767 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);