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"
40 #include "gtksignal.h"
42 #include "gtktextdisplay.h"
43 #include "gtktextview.h"
44 #include "gtkimmulticontext.h"
45 #include "gdk/gdkkeysyms.h"
46 #include "gtksizegroup.h" /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
47 #include "gtktextutil.h"
48 #include "gtkwindow.h"
51 /* How scrolling, validation, exposes, etc. work.
53 * The expose_event handler has the invariant that the onscreen lines
54 * have been validated.
56 * There are two ways that onscreen lines can become invalid. The first
57 * is to change which lines are onscreen. This happens when the value
58 * of a scroll adjustment changes. So the code path begins in
59 * gtk_text_view_value_changed() and goes like this:
60 * - gdk_window_scroll() to reflect the new adjustment value
61 * - validate the lines that were moved onscreen
62 * - gdk_window_process_updates() to handle the exposes immediately
64 * The second way is that you get the "invalidated" signal from the layout,
65 * indicating that lines have become invalid. This code path begins in
66 * invalidated_handler() and goes like this:
67 * - install high-priority idle which does the rest of the steps
68 * - if a scroll is pending from scroll_to_mark(), do the scroll,
69 * jumping to the gtk_text_view_value_changed() code path
70 * - otherwise, validate the onscreen lines
71 * - DO NOT process updates
73 * In both cases, validating the onscreen lines can trigger a scroll
74 * due to maintaining the first_para on the top of the screen.
75 * If validation triggers a scroll, we jump to the top of the code path
76 * for value_changed, and bail out of the current code path.
78 * Also, in size_allocate, if we invalidate some lines from changing
79 * the layout width, we need to go ahead and run the high-priority idle,
80 * because GTK sends exposes right after doing the size allocates without
81 * returning to the main loop. This is also why the high-priority idle
82 * is at a higher priority than resizing.
87 #define DEBUG_VALIDATION_AND_SCROLLING
90 #ifdef DEBUG_VALIDATION_AND_SCROLLING
96 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
97 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
99 struct _GtkTextPendingScroll
102 gdouble within_margin;
110 SET_SCROLL_ADJUSTMENTS,
128 PROP_PIXELS_ABOVE_LINES,
129 PROP_PIXELS_BELOW_LINES,
130 PROP_PIXELS_INSIDE_WRAP,
142 static void gtk_text_view_init (GtkTextView *text_view);
143 static void gtk_text_view_class_init (GtkTextViewClass *klass);
144 static void gtk_text_view_destroy (GtkObject *object);
145 static void gtk_text_view_finalize (GObject *object);
146 static void gtk_text_view_set_property (GObject *object,
150 static void gtk_text_view_get_property (GObject *object,
154 static void gtk_text_view_size_request (GtkWidget *widget,
155 GtkRequisition *requisition);
156 static void gtk_text_view_size_allocate (GtkWidget *widget,
157 GtkAllocation *allocation);
158 static void gtk_text_view_realize (GtkWidget *widget);
159 static void gtk_text_view_unrealize (GtkWidget *widget);
160 static void gtk_text_view_style_set (GtkWidget *widget,
161 GtkStyle *previous_style);
162 static void gtk_text_view_direction_changed (GtkWidget *widget,
163 GtkTextDirection previous_direction);
164 static gint gtk_text_view_event (GtkWidget *widget,
166 static gint gtk_text_view_key_press_event (GtkWidget *widget,
168 static gint gtk_text_view_key_release_event (GtkWidget *widget,
170 static gint gtk_text_view_button_press_event (GtkWidget *widget,
171 GdkEventButton *event);
172 static gint gtk_text_view_button_release_event (GtkWidget *widget,
173 GdkEventButton *event);
174 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
175 GdkEventFocus *event);
176 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
177 GdkEventFocus *event);
178 static gint gtk_text_view_motion_event (GtkWidget *widget,
179 GdkEventMotion *event);
180 static gint gtk_text_view_expose_event (GtkWidget *widget,
181 GdkEventExpose *expose);
182 static void gtk_text_view_draw_focus (GtkWidget *widget);
183 static void gtk_text_view_grab_focus (GtkWidget *widget);
184 static gboolean gtk_text_view_focus (GtkWidget *widget,
185 GtkDirectionType direction);
188 /* Source side drag signals */
189 static void gtk_text_view_drag_begin (GtkWidget *widget,
190 GdkDragContext *context);
191 static void gtk_text_view_drag_end (GtkWidget *widget,
192 GdkDragContext *context);
193 static void gtk_text_view_drag_data_get (GtkWidget *widget,
194 GdkDragContext *context,
195 GtkSelectionData *selection_data,
198 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
199 GdkDragContext *context);
201 /* Target side drag signals */
202 static void gtk_text_view_drag_leave (GtkWidget *widget,
203 GdkDragContext *context,
205 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
206 GdkDragContext *context,
210 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
211 GdkDragContext *context,
215 static void gtk_text_view_drag_data_received (GtkWidget *widget,
216 GdkDragContext *context,
219 GtkSelectionData *selection_data,
223 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
225 GtkAdjustment *vadj);
226 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
228 static void gtk_text_view_move_cursor (GtkTextView *text_view,
229 GtkMovementStep step,
231 gboolean extend_selection);
232 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
234 gboolean extend_selection);
235 static void gtk_text_view_set_anchor (GtkTextView *text_view);
236 static void gtk_text_view_scroll_pages (GtkTextView *text_view,
238 gboolean extend_selection);
239 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
241 gboolean extend_selection);
242 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
244 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
247 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
248 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
249 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
250 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
251 static void gtk_text_view_move_focus (GtkTextView *text_view,
252 GtkDirectionType direction_type);
253 static void gtk_text_view_unselect (GtkTextView *text_view);
255 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
256 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
258 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
259 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
260 GtkTextAttributes *values,
262 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
263 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
264 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
265 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
266 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
267 const GtkTextIter *iter,
268 GdkEventButton *event);
269 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
270 GdkEventButton *event);
271 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
272 const GtkTextIter *iter,
273 GdkEventMotion *event);
274 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
275 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
276 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
278 static void gtk_text_view_value_changed (GtkAdjustment *adj,
280 static void gtk_text_view_commit_handler (GtkIMContext *context,
282 GtkTextView *text_view);
283 static void gtk_text_view_commit_text (GtkTextView *text_view,
285 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
286 GtkTextView *text_view);
287 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
288 GtkTextView *text_view);
289 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
292 GtkTextView *text_view);
294 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
295 const GtkTextIter *location,
298 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
301 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
305 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
306 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
308 static void gtk_text_view_do_popup (GtkTextView *text_view,
309 GdkEventButton *event);
311 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
313 gdouble within_margin,
318 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
319 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
320 static void gtk_text_view_invalidate (GtkTextView *text_view);
321 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
323 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
325 /* Container methods */
326 static void gtk_text_view_add (GtkContainer *container,
328 static void gtk_text_view_remove (GtkContainer *container,
330 static void gtk_text_view_forall (GtkContainer *container,
331 gboolean include_internals,
332 GtkCallback callback,
333 gpointer callback_data);
335 /* FIXME probably need the focus methods. */
337 typedef struct _GtkTextViewChild GtkTextViewChild;
339 struct _GtkTextViewChild
343 GtkTextChildAnchor *anchor;
345 gint from_top_of_line;
346 gint from_left_of_buffer;
348 /* These are ignored if anchor != NULL */
349 GtkTextWindowType type;
354 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
355 GtkTextChildAnchor *anchor,
356 GtkTextLayout *layout);
357 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
358 GtkTextWindowType type,
361 static void text_view_child_free (GtkTextViewChild *child);
362 static void text_view_child_set_parent_window (GtkTextView *text_view,
363 GtkTextViewChild *child);
365 struct _GtkTextWindow
367 GtkTextWindowType type;
370 GdkWindow *bin_window;
371 GtkRequisition requisition;
372 GdkRectangle allocation;
375 static GtkTextWindow *text_window_new (GtkTextWindowType type,
378 gint height_request);
379 static void text_window_free (GtkTextWindow *win);
380 static void text_window_realize (GtkTextWindow *win,
382 static void text_window_unrealize (GtkTextWindow *win);
383 static void text_window_size_allocate (GtkTextWindow *win,
385 static void text_window_scroll (GtkTextWindow *win,
388 static void text_window_invalidate_rect (GtkTextWindow *win,
391 static gint text_window_get_width (GtkTextWindow *win);
392 static gint text_window_get_height (GtkTextWindow *win);
393 static void text_window_get_allocation (GtkTextWindow *win,
401 TARGET_COMPOUND_TEXT,
403 TARGET_TEXT_BUFFER_CONTENTS
406 static GtkTargetEntry target_table[] = {
407 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
408 TARGET_TEXT_BUFFER_CONTENTS },
409 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
410 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
411 { "TEXT", 0, TARGET_TEXT },
412 { "STRING", 0, TARGET_STRING }
415 static GtkContainerClass *parent_class = NULL;
416 static guint signals[LAST_SIGNAL] = { 0 };
419 gtk_text_view_get_type (void)
421 static GtkType our_type = 0;
425 static const GtkTypeInfo our_info =
428 sizeof (GtkTextView),
429 sizeof (GtkTextViewClass),
430 (GtkClassInitFunc) gtk_text_view_class_init,
431 (GtkObjectInitFunc) gtk_text_view_init,
432 /* reserved_1 */ NULL,
433 /* reserved_2 */ NULL,
434 (GtkClassInitFunc) NULL
437 our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
444 add_move_binding (GtkBindingSet *binding_set,
447 GtkMovementStep step,
450 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
452 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
456 GTK_TYPE_BOOL, FALSE);
458 /* Selection-extending version */
459 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
463 GTK_TYPE_BOOL, TRUE);
467 gtk_text_view_class_init (GtkTextViewClass *klass)
469 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
470 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
471 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
472 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
473 GtkBindingSet *binding_set;
475 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
477 /* Default handlers and virtual methods
479 gobject_class->set_property = gtk_text_view_set_property;
480 gobject_class->get_property = gtk_text_view_get_property;
482 object_class->destroy = gtk_text_view_destroy;
483 gobject_class->finalize = gtk_text_view_finalize;
485 widget_class->realize = gtk_text_view_realize;
486 widget_class->unrealize = gtk_text_view_unrealize;
487 widget_class->style_set = gtk_text_view_style_set;
488 widget_class->direction_changed = gtk_text_view_direction_changed;
489 widget_class->size_request = gtk_text_view_size_request;
490 widget_class->size_allocate = gtk_text_view_size_allocate;
491 widget_class->event = gtk_text_view_event;
492 widget_class->key_press_event = gtk_text_view_key_press_event;
493 widget_class->key_release_event = gtk_text_view_key_release_event;
494 widget_class->button_press_event = gtk_text_view_button_press_event;
495 widget_class->button_release_event = gtk_text_view_button_release_event;
496 widget_class->focus_in_event = gtk_text_view_focus_in_event;
497 widget_class->focus_out_event = gtk_text_view_focus_out_event;
498 widget_class->motion_notify_event = gtk_text_view_motion_event;
499 widget_class->expose_event = gtk_text_view_expose_event;
500 widget_class->grab_focus = gtk_text_view_grab_focus;
501 widget_class->focus = gtk_text_view_focus;
503 widget_class->drag_begin = gtk_text_view_drag_begin;
504 widget_class->drag_end = gtk_text_view_drag_end;
505 widget_class->drag_data_get = gtk_text_view_drag_data_get;
506 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
508 widget_class->drag_leave = gtk_text_view_drag_leave;
509 widget_class->drag_motion = gtk_text_view_drag_motion;
510 widget_class->drag_drop = gtk_text_view_drag_drop;
511 widget_class->drag_data_received = gtk_text_view_drag_data_received;
513 widget_class->popup_menu = gtk_text_view_popup_menu;
515 container_class->add = gtk_text_view_add;
516 container_class->remove = gtk_text_view_remove;
517 container_class->forall = gtk_text_view_forall;
519 klass->move_cursor = gtk_text_view_move_cursor;
520 klass->page_horizontally = gtk_text_view_page_horizontally;
521 klass->set_anchor = gtk_text_view_set_anchor;
522 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
523 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
524 klass->cut_clipboard = gtk_text_view_cut_clipboard;
525 klass->copy_clipboard = gtk_text_view_copy_clipboard;
526 klass->paste_clipboard = gtk_text_view_paste_clipboard;
527 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
528 klass->move_focus = gtk_text_view_move_focus;
529 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
535 g_object_class_install_property (gobject_class,
536 PROP_PIXELS_ABOVE_LINES,
537 g_param_spec_int ("pixels_above_lines",
538 _("Pixels Above Lines"),
539 _("Pixels of blank space above paragraphs"),
545 g_object_class_install_property (gobject_class,
546 PROP_PIXELS_BELOW_LINES,
547 g_param_spec_int ("pixels_below_lines",
548 _("Pixels Below Lines"),
549 _("Pixels of blank space below paragraphs"),
555 g_object_class_install_property (gobject_class,
556 PROP_PIXELS_INSIDE_WRAP,
557 g_param_spec_int ("pixels_inside_wrap",
558 _("Pixels Inside Wrap"),
559 _("Pixels of blank space between wrapped lines in a paragraph"),
565 g_object_class_install_property (gobject_class,
567 g_param_spec_boolean ("editable",
569 _("Whether the text can be modified by the user"),
573 g_object_class_install_property (gobject_class,
575 g_param_spec_enum ("wrap_mode",
577 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
582 g_object_class_install_property (gobject_class,
584 g_param_spec_enum ("justification",
586 _("Left, right, or center justification"),
587 GTK_TYPE_JUSTIFICATION,
591 g_object_class_install_property (gobject_class,
593 g_param_spec_int ("left_margin",
595 _("Width of the left margin in pixels"),
601 g_object_class_install_property (gobject_class,
603 g_param_spec_int ("right_margin",
605 _("Width of the right margin in pixels"),
611 g_object_class_install_property (gobject_class,
613 g_param_spec_int ("indent",
615 _("Amount to indent the paragraph, in pixels"),
621 g_object_class_install_property (gobject_class,
623 g_param_spec_boxed ("tabs",
625 _("Custom tabs for this text"),
626 PANGO_TYPE_TAB_ARRAY,
629 g_object_class_install_property (gobject_class,
631 g_param_spec_boolean ("cursor_visible",
633 _("If the insertion cursor is shown"),
642 signals[MOVE_CURSOR] =
643 gtk_signal_new ("move_cursor",
644 GTK_RUN_LAST | GTK_RUN_ACTION,
645 GTK_CLASS_TYPE (object_class),
646 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
647 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
648 GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
650 signals[PAGE_HORIZONTALLY] =
651 gtk_signal_new ("page_horizontally",
652 GTK_RUN_LAST | GTK_RUN_ACTION,
653 GTK_CLASS_TYPE (object_class),
654 GTK_SIGNAL_OFFSET (GtkTextViewClass, page_horizontally),
655 _gtk_marshal_VOID__INT_BOOLEAN,
656 GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_BOOL);
658 signals[SET_ANCHOR] =
659 gtk_signal_new ("set_anchor",
660 GTK_RUN_LAST | GTK_RUN_ACTION,
661 GTK_CLASS_TYPE (object_class),
662 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
663 _gtk_marshal_VOID__VOID,
666 signals[INSERT_AT_CURSOR] =
667 gtk_signal_new ("insert_at_cursor",
668 GTK_RUN_LAST | GTK_RUN_ACTION,
669 GTK_CLASS_TYPE (object_class),
670 GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
671 _gtk_marshal_VOID__STRING,
672 GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
674 signals[DELETE_FROM_CURSOR] =
675 gtk_signal_new ("delete_from_cursor",
676 GTK_RUN_LAST | GTK_RUN_ACTION,
677 GTK_CLASS_TYPE (object_class),
678 GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
679 _gtk_marshal_VOID__ENUM_INT,
680 GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
682 signals[CUT_CLIPBOARD] =
683 gtk_signal_new ("cut_clipboard",
684 GTK_RUN_LAST | GTK_RUN_ACTION,
685 GTK_CLASS_TYPE (object_class),
686 GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
687 _gtk_marshal_VOID__VOID,
690 signals[COPY_CLIPBOARD] =
691 gtk_signal_new ("copy_clipboard",
692 GTK_RUN_LAST | GTK_RUN_ACTION,
693 GTK_CLASS_TYPE (object_class),
694 GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
695 _gtk_marshal_VOID__VOID,
698 signals[PASTE_CLIPBOARD] =
699 gtk_signal_new ("paste_clipboard",
700 GTK_RUN_LAST | GTK_RUN_ACTION,
701 GTK_CLASS_TYPE (object_class),
702 GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
703 _gtk_marshal_VOID__VOID,
706 signals[TOGGLE_OVERWRITE] =
707 gtk_signal_new ("toggle_overwrite",
708 GTK_RUN_LAST | GTK_RUN_ACTION,
709 GTK_CLASS_TYPE (object_class),
710 GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
711 _gtk_marshal_VOID__VOID,
714 signals[MOVE_FOCUS] =
715 gtk_signal_new ("move_focus",
716 GTK_RUN_LAST | GTK_RUN_ACTION,
717 GTK_CLASS_TYPE (object_class),
718 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_focus),
719 _gtk_marshal_VOID__ENUM,
720 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
722 signals[SET_SCROLL_ADJUSTMENTS] =
723 gtk_signal_new ("set_scroll_adjustments",
725 GTK_CLASS_TYPE (object_class),
726 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
727 _gtk_marshal_VOID__OBJECT_OBJECT,
728 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
729 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
731 signals[POPULATE_POPUP] =
732 gtk_signal_new ("populate_popup",
734 GTK_CLASS_TYPE (object_class),
735 GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
736 _gtk_marshal_VOID__OBJECT,
737 GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
743 binding_set = gtk_binding_set_by_class (klass);
745 /* Moving the insertion point */
746 add_move_binding (binding_set, GDK_Right, 0,
747 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
749 add_move_binding (binding_set, GDK_KP_Right, 0,
750 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
752 add_move_binding (binding_set, GDK_Left, 0,
753 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
755 add_move_binding (binding_set, GDK_KP_Left, 0,
756 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
758 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
759 GTK_MOVEMENT_WORDS, 1);
761 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
762 GTK_MOVEMENT_WORDS, 1);
764 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
765 GTK_MOVEMENT_WORDS, -1);
767 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
768 GTK_MOVEMENT_WORDS, 1);
770 add_move_binding (binding_set, GDK_Up, 0,
771 GTK_MOVEMENT_DISPLAY_LINES, -1);
773 add_move_binding (binding_set, GDK_KP_Up, 0,
774 GTK_MOVEMENT_DISPLAY_LINES, -1);
776 add_move_binding (binding_set, GDK_Down, 0,
777 GTK_MOVEMENT_DISPLAY_LINES, 1);
779 add_move_binding (binding_set, GDK_KP_Down, 0,
780 GTK_MOVEMENT_DISPLAY_LINES, 1);
782 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
783 GTK_MOVEMENT_PARAGRAPHS, -1);
785 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
786 GTK_MOVEMENT_PARAGRAPHS, -1);
788 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
789 GTK_MOVEMENT_PARAGRAPHS, 1);
791 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
792 GTK_MOVEMENT_PARAGRAPHS, 1);
794 add_move_binding (binding_set, GDK_Home, 0,
795 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
797 add_move_binding (binding_set, GDK_KP_Home, 0,
798 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
800 add_move_binding (binding_set, GDK_End, 0,
801 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
803 add_move_binding (binding_set, GDK_KP_End, 0,
804 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
806 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
807 GTK_MOVEMENT_BUFFER_ENDS, -1);
809 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
810 GTK_MOVEMENT_BUFFER_ENDS, -1);
812 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
813 GTK_MOVEMENT_BUFFER_ENDS, 1);
815 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
816 GTK_MOVEMENT_BUFFER_ENDS, 1);
818 add_move_binding (binding_set, GDK_Page_Up, 0,
819 GTK_MOVEMENT_PAGES, -1);
821 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
822 GTK_MOVEMENT_PAGES, -1);
824 add_move_binding (binding_set, GDK_Page_Down, 0,
825 GTK_MOVEMENT_PAGES, 1);
827 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
828 GTK_MOVEMENT_PAGES, 1);
832 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
834 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
836 GTK_TYPE_BOOL, FALSE);
837 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
839 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
841 GTK_TYPE_BOOL, TRUE);
844 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
845 "page_horizontally", 2,
847 GTK_TYPE_BOOL, FALSE);
849 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
850 "page_horizontally", 2,
852 GTK_TYPE_BOOL, TRUE);
854 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
855 "page_horizontally", 2,
857 GTK_TYPE_BOOL, FALSE);
859 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
860 "page_horizontally", 2,
862 GTK_TYPE_BOOL, TRUE);
864 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
865 "page_horizontally", 2,
867 GTK_TYPE_BOOL, FALSE);
869 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
870 "page_horizontally", 2,
872 GTK_TYPE_BOOL, TRUE);
874 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
875 "page_horizontally", 2,
877 GTK_TYPE_BOOL, FALSE);
879 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
880 "page_horizontally", 2,
882 GTK_TYPE_BOOL, TRUE);
885 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
886 "delete_from_cursor", 2,
887 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
890 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
891 "delete_from_cursor", 2,
892 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
895 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
896 "delete_from_cursor", 2,
897 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
900 /* Make this do the same as Backspace, to help with mis-typing */
901 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
902 "delete_from_cursor", 2,
903 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
906 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
907 "delete_from_cursor", 2,
908 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
911 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
912 "delete_from_cursor", 2,
913 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
916 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
917 "delete_from_cursor", 2,
918 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
923 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
925 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
926 "copy_clipboard", 0);
927 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
928 "paste_clipboard", 0);
930 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
932 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
933 "copy_clipboard", 0);
934 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
935 "paste_clipboard", 0);
938 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
939 "toggle_overwrite", 0);
940 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
941 "toggle_overwrite", 0);
943 /* Control-tab focus motion */
944 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
946 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
947 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
949 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
951 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
953 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
954 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
956 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
960 gtk_text_view_init (GtkTextView *text_view)
964 widget = GTK_WIDGET (text_view);
966 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
968 /* Set up default style */
969 text_view->wrap_mode = GTK_WRAP_NONE;
970 text_view->pixels_above_lines = 0;
971 text_view->pixels_below_lines = 0;
972 text_view->pixels_inside_wrap = 0;
973 text_view->justify = GTK_JUSTIFY_LEFT;
974 text_view->left_margin = 0;
975 text_view->right_margin = 0;
976 text_view->indent = 0;
977 text_view->tabs = NULL;
978 text_view->editable = TRUE;
980 gtk_drag_dest_set (widget,
982 target_table, G_N_ELEMENTS (target_table),
983 GDK_ACTION_COPY | GDK_ACTION_MOVE);
985 text_view->virtual_cursor_x = -1;
986 text_view->virtual_cursor_y = -1;
988 /* This object is completely private. No external entity can gain a reference
989 * to it; so we create it here and destroy it in finalize ().
991 text_view->im_context = gtk_im_multicontext_new ();
993 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
994 G_CALLBACK (gtk_text_view_commit_handler), text_view);
995 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
996 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
997 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
998 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
999 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
1000 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1002 text_view->cursor_visible = TRUE;
1004 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1007 text_view->drag_start_x = -1;
1008 text_view->drag_start_y = -1;
1010 text_view->pending_place_cursor_button = 0;
1012 /* We handle all our own redrawing */
1013 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1017 * gtk_text_view_new:
1019 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1020 * before using the text view, an empty default buffer will be created
1021 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1022 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1024 * Return value: a new #GtkTextView
1027 gtk_text_view_new (void)
1029 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1033 * gtk_text_view_new_with_buffer:
1034 * @buffer: a #GtkTextBuffer
1036 * Creates a new #GtkTextView widget displaying the buffer
1037 * @buffer. One buffer can be shared among many widgets.
1038 * @buffer may be NULL to create a default buffer, in which case
1039 * this function is equivalent to gtk_text_view_new(). The
1040 * text view adds its own reference count to the buffer; it does not
1041 * take over an existing reference.
1043 * Return value: a new #GtkTextView.
1046 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1048 GtkTextView *text_view;
1050 text_view = (GtkTextView*)gtk_text_view_new ();
1052 gtk_text_view_set_buffer (text_view, buffer);
1054 return GTK_WIDGET (text_view);
1058 * gtk_text_view_set_buffer:
1059 * @text_view: a #GtkTextView
1060 * @buffer: a #GtkTextBuffer
1062 * Sets @buffer as the buffer being displayed by @text_view. The previous
1063 * buffer displayed by the text view is unreferenced, and a reference is
1064 * added to @buffer. If you owned a reference to @buffer before passing it
1065 * to this function, you must remove that reference yourself; #GtkTextView
1066 * will not "adopt" it.
1070 gtk_text_view_set_buffer (GtkTextView *text_view,
1071 GtkTextBuffer *buffer)
1073 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1074 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1076 if (text_view->buffer == buffer)
1079 if (text_view->buffer != NULL)
1081 /* Destroy all anchored children */
1085 copy = g_slist_copy (text_view->children);
1087 while (tmp_list != NULL)
1089 GtkTextViewChild *vc = tmp_list->data;
1093 gtk_widget_destroy (vc->widget);
1094 /* vc may now be invalid! */
1097 tmp_list = g_slist_next (tmp_list);
1100 g_slist_free (copy);
1102 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1103 (gpointer) gtk_text_view_mark_set_handler, text_view);
1104 g_object_unref (G_OBJECT (text_view->buffer));
1105 text_view->dnd_mark = NULL;
1107 if (GTK_WIDGET_REALIZED (text_view))
1109 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1110 GDK_SELECTION_PRIMARY);
1111 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1115 text_view->buffer = buffer;
1121 g_object_ref (G_OBJECT (buffer));
1123 if (text_view->layout)
1124 gtk_text_layout_set_buffer (text_view->layout, buffer);
1126 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1128 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1132 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1136 text_view->first_para_pixels = 0;
1138 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1139 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1141 if (GTK_WIDGET_REALIZED (text_view))
1143 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1144 GDK_SELECTION_PRIMARY);
1145 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1149 if (GTK_WIDGET_VISIBLE (text_view))
1150 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1152 DV(g_print ("Invalidating due to set_buffer\n"));
1153 gtk_text_view_invalidate (text_view);
1156 static GtkTextBuffer*
1157 get_buffer (GtkTextView *text_view)
1159 if (text_view->buffer == NULL)
1162 b = gtk_text_buffer_new (NULL);
1163 gtk_text_view_set_buffer (text_view, b);
1164 g_object_unref (G_OBJECT (b));
1167 return text_view->buffer;
1171 * gtk_text_view_get_buffer:
1172 * @text_view: a #GtkTextView
1174 * Returns the #GtkTextBuffer being displayed by this text view.
1175 * The reference count on the buffer is not incremented; the caller
1176 * of this function won't own a new reference.
1178 * Return value: a #GtkTextBuffer
1181 gtk_text_view_get_buffer (GtkTextView *text_view)
1183 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1185 return get_buffer (text_view);
1189 * gtk_text_view_get_iter_at_location:
1190 * @text_view: a #GtkTextView
1191 * @iter: a #GtkTextIter
1192 * @x: x position, in buffer coordinates
1193 * @y: y position, in buffer coordinates
1195 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1196 * coordinates are coordinates for the entire buffer, not just the
1197 * currently-displayed portion. If you have coordinates from an
1198 * event, you have to convert those to buffer coordinates with
1199 * gtk_text_view_window_to_buffer_coords().
1203 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1208 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1209 g_return_if_fail (iter != NULL);
1211 gtk_text_view_ensure_layout (text_view);
1213 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1220 * gtk_text_view_get_iter_location:
1221 * @text_view: a #GtkTextView
1222 * @iter: a #GtkTextIter
1223 * @location: bounds of the character at @iter
1225 * Gets a rectangle which roughly contains the character at @iter.
1226 * The rectangle position is in buffer coordinates; use
1227 * gtk_text_view_buffer_to_window_coords() to convert these
1228 * coordinates to coordinates for one of the windows in the text view.
1232 gtk_text_view_get_iter_location (GtkTextView *text_view,
1233 const GtkTextIter *iter,
1234 GdkRectangle *location)
1236 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1237 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1239 gtk_text_view_ensure_layout (text_view);
1241 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1245 * gtk_text_view_get_line_yrange:
1246 * @text_view: a #GtkTextView
1247 * @iter: a #GtkTextIter
1248 * @y: return location for a y coordinate
1249 * @height: return location for a height
1251 * Gets the y coordinate of the top of the line containing @iter,
1252 * and the height of the line. The coordinate is a buffer coordinate;
1253 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1257 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1258 const GtkTextIter *iter,
1262 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1263 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1265 gtk_text_view_ensure_layout (text_view);
1267 gtk_text_layout_get_line_yrange (text_view->layout,
1274 * gtk_text_view_get_line_at_y:
1275 * @text_view: a #GtkTextView
1276 * @target_iter: a #GtkTextIter
1277 * @y: a y coordinate
1278 * @line_top: return location for top coordinate of the line
1280 * Gets the #GtkTextIter at the start of the line containing
1281 * the coordinate @y. @y is in buffer coordinates, convert from
1282 * window coordinates with gtk_text_view_window_to_buffer_coords().
1283 * If non-%NULL, @line_top will be filled with the coordinate of the top
1287 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1288 GtkTextIter *target_iter,
1292 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1294 gtk_text_view_ensure_layout (text_view);
1296 gtk_text_layout_get_line_at_y (text_view->layout,
1303 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1305 DV (g_print (" Setting adj to raw value %g\n", val));
1307 /* We don't really want to clamp to upper; we want to clamp to
1308 upper - page_size which is the highest value the scrollbar
1309 will let us reach. */
1310 if (val > (adj->upper - adj->page_size))
1311 val = adj->upper - adj->page_size;
1313 if (val < adj->lower)
1316 if (val != adj->value)
1318 DV (g_print (" Setting adj to clamped value %g\n", val));
1319 gtk_adjustment_set_value (adj, val);
1327 * gtk_text_view_scroll_to_iter:
1328 * @text_view: a #GtkTextView
1329 * @iter: a #GtkTextIter
1330 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1331 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1332 * @xalign: horizontal alignment of mark within visible area.
1333 * @yalign: vertical alignment of mark within visible area
1335 * Scrolls @text_view so that @iter is on the screen in the position
1336 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1337 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1338 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1339 * possibly not scrolling at all. The effective screen for purposes
1340 * of this function is reduced by a margin of size @within_margin.
1341 * NOTE: This function uses the currently-computed height of the
1342 * lines in the text buffer. Note that line heights are computed
1343 * in an idle handler; so this function may not have the desired effect
1344 * if it's called before the height computations. To avoid oddness,
1345 * consider using gtk_text_view_scroll_to_mark() which saves a point
1346 * to be scrolled to after line validation.
1348 * Return value: %TRUE if scrolling occurred
1351 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1353 gdouble within_margin,
1359 GdkRectangle screen;
1364 gboolean retval = FALSE;
1366 gint screen_xoffset, screen_yoffset;
1367 gint current_x_scroll, current_y_scroll;
1369 /* FIXME why don't we do the validate-at-scroll-destination thing
1370 * from flush_scroll in this function? I think it wasn't done before
1371 * because changed_handler was screwed up, but I could be wrong.
1374 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1375 g_return_val_if_fail (iter != NULL, FALSE);
1376 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1377 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1378 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1380 widget = GTK_WIDGET (text_view);
1382 DV(g_print(G_STRLOC"\n"));
1384 gtk_text_layout_get_iter_location (text_view->layout,
1388 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1390 current_x_scroll = text_view->xoffset;
1391 current_y_scroll = text_view->yoffset;
1393 screen.x = current_x_scroll;
1394 screen.y = current_y_scroll;
1395 screen.width = SCREEN_WIDTH (widget);
1396 screen.height = SCREEN_HEIGHT (widget);
1398 screen_xoffset = screen.width * within_margin;
1399 screen_yoffset = screen.height * within_margin;
1401 screen.x += screen_xoffset;
1402 screen.y += screen_yoffset;
1403 screen.width -= screen_xoffset * 2;
1404 screen.height -= screen_yoffset * 2;
1406 /* paranoia check */
1407 if (screen.width < 1)
1409 if (screen.height < 1)
1412 screen_right = screen.x + screen.width;
1413 screen_bottom = screen.y + screen.height;
1415 /* The alignment affects the point in the target character that we
1416 * choose to align. If we're doing right/bottom alignment, we align
1417 * the right/bottom edge of the character the mark is at; if we're
1418 * doing left/top we align the left/top edge of the character; if
1419 * we're doing center alignment we align the center of the
1423 /* Vertical scroll */
1426 scroll_dest = current_y_scroll;
1430 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1432 /* if scroll_dest < screen.y, we move a negative increment (up),
1433 * else a positive increment (down)
1435 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1439 /* move minimum to get onscreen */
1440 if (rect.y < screen.y)
1442 scroll_dest = rect.y;
1443 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1445 else if ((rect.y + rect.height) > screen_bottom)
1447 scroll_dest = rect.y + rect.height;
1448 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1452 if (scroll_inc != 0)
1454 retval = set_adjustment_clamped (get_vadjustment (text_view),
1455 current_y_scroll + scroll_inc);
1457 DV (g_print (" vert increment %d\n", scroll_inc));
1460 /* Horizontal scroll */
1463 scroll_dest = current_x_scroll;
1467 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1469 /* if scroll_dest < screen.y, we move a negative increment (left),
1470 * else a positive increment (right)
1472 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1476 /* move minimum to get onscreen */
1477 if (rect.x < screen.x)
1479 scroll_dest = rect.x;
1480 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1482 else if ((rect.x + rect.width) > screen_right)
1484 scroll_dest = rect.x + rect.width;
1485 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1489 if (scroll_inc != 0)
1491 retval = set_adjustment_clamped (get_hadjustment (text_view),
1492 current_x_scroll + scroll_inc);
1494 DV (g_print (" horiz increment %d\n", scroll_inc));
1498 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1500 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1506 free_pending_scroll (GtkTextPendingScroll *scroll)
1508 if (!gtk_text_mark_get_deleted (scroll->mark))
1509 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1511 g_object_unref (G_OBJECT (scroll->mark));
1516 cancel_pending_scroll (GtkTextView *text_view)
1518 if (text_view->pending_scroll)
1520 free_pending_scroll (text_view->pending_scroll);
1521 text_view->pending_scroll = NULL;
1526 gtk_text_view_queue_scroll (GtkTextView *text_view,
1528 gdouble within_margin,
1534 GtkTextPendingScroll *scroll;
1536 DV(g_print(G_STRLOC"\n"));
1538 scroll = g_new (GtkTextPendingScroll, 1);
1540 scroll->within_margin = within_margin;
1541 scroll->use_align = use_align;
1542 scroll->xalign = xalign;
1543 scroll->yalign = yalign;
1545 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1547 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1550 gtk_text_mark_get_left_gravity (mark));
1552 g_object_ref (G_OBJECT (scroll->mark));
1554 cancel_pending_scroll (text_view);
1556 text_view->pending_scroll = scroll;
1560 gtk_text_view_flush_scroll (GtkTextView *text_view)
1563 GtkTextPendingScroll *scroll;
1567 widget = GTK_WIDGET (text_view);
1569 DV(g_print(G_STRLOC"\n"));
1571 if (text_view->pending_scroll == NULL)
1573 DV (g_print ("in flush scroll, no pending scroll\n"));
1577 scroll = text_view->pending_scroll;
1579 /* avoid recursion */
1580 text_view->pending_scroll = NULL;
1582 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1584 /* Validate area around the scroll destination, so the adjustment
1585 * can meaningfully point into that area. We must validate
1586 * enough area to be sure that after we scroll, everything onscreen
1587 * is valid; otherwise, validation will maintain the first para
1588 * in one place, but may push the target iter off the bottom of
1591 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1592 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1593 - (widget->allocation.height * 2),
1594 widget->allocation.height * 2);
1596 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1598 /* Ensure we have updated width/height */
1599 gtk_text_view_update_adjustments (text_view);
1601 retval = gtk_text_view_scroll_to_iter (text_view,
1603 scroll->within_margin,
1608 free_pending_scroll (scroll);
1614 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1616 if (upper != adj->upper)
1618 gdouble min = MAX (0.0, upper - adj->page_size);
1619 gboolean value_changed = FALSE;
1623 if (adj->value > min)
1626 value_changed = TRUE;
1629 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1630 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1634 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1635 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1641 gtk_text_view_update_adjustments (GtkTextView *text_view)
1643 gint width = 0, height = 0;
1645 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1647 if (text_view->layout)
1648 gtk_text_layout_get_size (text_view->layout, &width, &height);
1650 if (text_view->width != width || text_view->height != height)
1652 text_view->width = width;
1653 text_view->height = height;
1655 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1656 MAX (SCREEN_WIDTH (text_view), width));
1657 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1658 MAX (SCREEN_HEIGHT (text_view), height));
1660 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1662 /* Set up the step sizes; we'll say that a page is
1663 our allocation minus one step, and a step is
1664 1/10 of our allocation. */
1665 text_view->hadjustment->step_increment =
1666 SCREEN_WIDTH (text_view) / 10.0;
1667 text_view->hadjustment->page_increment =
1668 SCREEN_WIDTH (text_view) * 0.9;
1670 text_view->vadjustment->step_increment =
1671 SCREEN_HEIGHT (text_view) / 10.0;
1672 text_view->vadjustment->page_increment =
1673 SCREEN_HEIGHT (text_view) * 0.9;
1675 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1676 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1681 gtk_text_view_update_layout_width (GtkTextView *text_view)
1683 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1685 gtk_text_view_ensure_layout (text_view);
1687 gtk_text_layout_set_screen_width (text_view->layout,
1688 SCREEN_WIDTH (text_view));
1692 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1695 gint cursor_x_pos, cursor_y_pos;
1697 if (text_view->layout == NULL)
1700 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1702 area.x = cursor_x_pos;
1703 area.y = cursor_y_pos;
1704 area.width = area.height = 0;
1706 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1710 * gtk_text_view_scroll_to_mark:
1711 * @text_view: a #GtkTextView
1712 * @mark: a #GtkTextMark
1713 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1714 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1715 * @xalign: horizontal alignment of mark within visible area.
1716 * @yalign: vertical alignment of mark within visible area
1718 * Scrolls @text_view so that @mark is on the screen in the position
1719 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1720 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1721 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1722 * possibly not scrolling at all. The effective screen for purposes
1723 * of this function is reduced by a margin of size @within_margin.
1727 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1729 gdouble within_margin,
1734 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1735 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1736 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1737 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1738 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1740 gtk_text_view_queue_scroll (text_view, mark,
1746 /* If no validation is pending, we need to go ahead and force an
1749 if (text_view->layout &&
1750 gtk_text_layout_is_valid (text_view->layout))
1751 gtk_text_view_flush_scroll (text_view);
1755 * gtk_text_view_scroll_mark_onscreen:
1756 * @text_view: a #GtkTextView
1757 * @mark: a mark in the buffer for @text_view
1759 * Scrolls @text_view the minimum distance such that @mark is contained
1760 * within the visible area of the widget.
1764 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1767 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1768 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1770 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1774 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1776 GdkRectangle visible_rect;
1777 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1779 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1781 visible_rect.y + visible_rect.height);
1785 * gtk_text_view_move_mark_onscreen:
1786 * @text_view: a #GtkTextView
1787 * @mark: a #GtkTextMark
1789 * Moves a mark within the buffer so that it's
1790 * located within the currently-visible text area.
1792 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1795 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1800 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1801 g_return_val_if_fail (mark != NULL, FALSE);
1803 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1805 if (clamp_iter_onscreen (text_view, &iter))
1807 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1815 * gtk_text_view_get_visible_rect:
1816 * @text_view: a #GtkTextView
1817 * @visible_rect: rectangle to fill
1819 * Fills @visible_rect with the currently-visible
1820 * region of the buffer, in buffer coordinates. Convert to window coordinates
1821 * with gtk_text_view_buffer_to_window_coords().
1824 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1825 GdkRectangle *visible_rect)
1829 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1831 widget = GTK_WIDGET (text_view);
1835 visible_rect->x = text_view->xoffset;
1836 visible_rect->y = text_view->yoffset;
1837 visible_rect->width = SCREEN_WIDTH (widget);
1838 visible_rect->height = SCREEN_HEIGHT (widget);
1840 DV(g_print(" visible rect: %d,%d %d x %d\n",
1843 visible_rect->width,
1844 visible_rect->height));
1849 * gtk_text_view_set_wrap_mode:
1850 * @text_view: a #GtkTextView
1851 * @wrap_mode: a #GtkWrapMode
1853 * Sets the line wrapping for the view.
1856 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1857 GtkWrapMode wrap_mode)
1859 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1861 if (text_view->wrap_mode != wrap_mode)
1863 text_view->wrap_mode = wrap_mode;
1865 if (text_view->layout)
1867 text_view->layout->default_style->wrap_mode = wrap_mode;
1868 gtk_text_layout_default_style_changed (text_view->layout);
1872 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1876 * gtk_text_view_get_wrap_mode:
1877 * @text_view: a #GtkTextView
1879 * Gets the line wrapping for the view.
1881 * Return value: the line wrap setting
1884 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1886 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1888 return text_view->wrap_mode;
1892 * gtk_text_view_set_editable:
1893 * @text_view: a #GtkTextView
1894 * @setting: whether it's editable
1896 * Sets the default editability of the #GtkTextView. You can override
1897 * this default setting with tags in the buffer, using the "editable"
1898 * attribute of tags.
1901 gtk_text_view_set_editable (GtkTextView *text_view,
1904 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1905 setting = setting != FALSE;
1907 if (text_view->editable != setting)
1909 text_view->editable = setting;
1911 if (text_view->layout)
1913 text_view->layout->default_style->editable = text_view->editable;
1914 gtk_text_layout_default_style_changed (text_view->layout);
1917 g_object_notify (G_OBJECT (text_view), "editable");
1922 * gtk_text_view_get_editable:
1923 * @text_view: a #GtkTextView
1925 * Returns the default editability of the #GtkTextView. Tags in the
1926 * buffer may override this setting for some ranges of text.
1928 * Return value: whether text is editable by default
1931 gtk_text_view_get_editable (GtkTextView *text_view)
1933 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1935 return text_view->editable;
1939 * gtk_text_view_set_pixels_above_lines:
1940 * @text_view: a #GtkTextView
1941 * @pixels_above_lines: pixels above paragraphs
1943 * Sets the default number of blank pixels above paragraphs in @text_view.
1944 * Tags in the buffer for @text_view may override the defaults.
1948 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1949 gint pixels_above_lines)
1951 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1953 if (text_view->pixels_above_lines != pixels_above_lines)
1955 text_view->pixels_above_lines = pixels_above_lines;
1957 if (text_view->layout)
1959 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1960 gtk_text_layout_default_style_changed (text_view->layout);
1963 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1968 * gtk_text_view_get_pixels_above_lines:
1969 * @text_view: a #GtkTextView
1971 * Gets the default number of pixels to put above paragraphs.
1973 * Return value: default number of pixels above paragraphs
1976 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1978 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1980 return text_view->pixels_above_lines;
1984 * gtk_text_view_set_pixels_below_lines:
1985 * @text_view: a #GtkTextView
1986 * @pixels_below_lines: pixels below paragraphs
1988 * Sets the default number of pixels of blank space
1989 * to put below paragraphs in @text_view. May be overridden
1990 * by tags applied to @text_view's buffer.
1994 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1995 gint pixels_below_lines)
1997 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1999 if (text_view->pixels_below_lines != pixels_below_lines)
2001 text_view->pixels_below_lines = pixels_below_lines;
2003 if (text_view->layout)
2005 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2006 gtk_text_layout_default_style_changed (text_view->layout);
2009 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
2014 * gtk_text_view_get_pixels_below_lines:
2015 * @text_view: a #GtkTextView
2017 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2019 * Return value: default number of blank pixels below paragraphs
2022 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2024 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2026 return text_view->pixels_below_lines;
2030 * gtk_text_view_set_pixels_inside_wrap:
2031 * @text_view: a #GtkTextView
2032 * @pixels_inside_wrap: default number of pixels between wrapped lines
2034 * Sets the default number of pixels of blank space to leave between
2035 * display/wrapped lines within a paragraph. May be overridden by
2036 * tags in @text_view's buffer.
2040 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2041 gint pixels_inside_wrap)
2043 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2045 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2047 text_view->pixels_inside_wrap = pixels_inside_wrap;
2049 if (text_view->layout)
2051 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2052 gtk_text_layout_default_style_changed (text_view->layout);
2055 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2060 * gtk_text_view_get_pixels_inside_wrap:
2061 * @text_view: a #GtkTextView
2063 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2065 * Return value: default number of pixels of blank space between wrapped lines
2068 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2070 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2072 return text_view->pixels_inside_wrap;
2076 * gtk_text_view_set_justification:
2077 * @text_view: a #GtkTextView
2078 * @justification: justification
2080 * Sets the default justification of text in @text_view.
2081 * Tags in the view's buffer may override the default.
2085 gtk_text_view_set_justification (GtkTextView *text_view,
2086 GtkJustification justification)
2088 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2090 if (text_view->justify != justification)
2092 text_view->justify = justification;
2094 if (text_view->layout)
2096 text_view->layout->default_style->justification = justification;
2097 gtk_text_layout_default_style_changed (text_view->layout);
2100 g_object_notify (G_OBJECT (text_view), "justification");
2105 * gtk_text_view_get_justification:
2106 * @text_view: a #GtkTextView
2108 * Gets the default justification of paragraphs in @text_view.
2109 * Tags in the buffer may override the default.
2111 * Return value: default justification
2114 gtk_text_view_get_justification (GtkTextView *text_view)
2116 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2118 return text_view->justify;
2122 * gtk_text_view_set_left_margin:
2123 * @text_view: a #GtkTextView
2124 * @left_margin: left margin in pixels
2126 * Sets the default left margin for text in @text_view.
2127 * Tags in the buffer may override the default.
2131 gtk_text_view_set_left_margin (GtkTextView *text_view,
2134 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2136 if (text_view->left_margin != left_margin)
2138 text_view->left_margin = left_margin;
2140 if (text_view->layout)
2142 text_view->layout->default_style->left_margin = left_margin;
2143 gtk_text_layout_default_style_changed (text_view->layout);
2146 g_object_notify (G_OBJECT (text_view), "left_margin");
2151 * gtk_text_view_get_left_margin:
2152 * @text_view: a #GtkTextView
2154 * Gets the default left margin size of paragraphs in the @text_view.
2155 * Tags in the buffer may override the default.
2157 * Return value: left margin in pixels
2160 gtk_text_view_get_left_margin (GtkTextView *text_view)
2162 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2164 return text_view->left_margin;
2168 * gtk_text_view_set_right_margin:
2169 * @text_view: a #GtkTextView
2170 * @right_margin: right margin in pixels
2172 * Sets the default right margin for text in the text view.
2173 * Tags in the buffer may override the default.
2177 gtk_text_view_set_right_margin (GtkTextView *text_view,
2180 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2182 if (text_view->right_margin != right_margin)
2184 text_view->right_margin = right_margin;
2186 if (text_view->layout)
2188 text_view->layout->default_style->right_margin = right_margin;
2189 gtk_text_layout_default_style_changed (text_view->layout);
2192 g_object_notify (G_OBJECT (text_view), "right_margin");
2197 * gtk_text_view_get_right_margin:
2198 * @text_view: a #GtkTextView
2200 * Gets the default right margin for text in @text_view. Tags
2201 * in the buffer may override the default.
2203 * Return value: right margin in pixels
2206 gtk_text_view_get_right_margin (GtkTextView *text_view)
2208 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2210 return text_view->right_margin;
2214 * gtk_text_view_set_indent:
2215 * @text_view: a #GtkTextView
2216 * @indent: indentation in pixels
2218 * Sets the default indentation for paragraphs in @text_view.
2219 * Tags in the buffer may override the default.
2223 gtk_text_view_set_indent (GtkTextView *text_view,
2226 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2228 if (text_view->indent != indent)
2230 text_view->indent = indent;
2232 if (text_view->layout)
2234 text_view->layout->default_style->indent = indent;
2235 gtk_text_layout_default_style_changed (text_view->layout);
2238 g_object_notify (G_OBJECT (text_view), "indent");
2243 * gtk_text_view_get_indent:
2244 * @text_view: a #GtkTextView
2246 * Gets the default indentation of paragraphs in @text_view.
2247 * Tags in the view's buffer may override the default.
2248 * The indentation may be negative.
2250 * Return value: number of pixels of indentation
2253 gtk_text_view_get_indent (GtkTextView *text_view)
2255 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2257 return text_view->indent;
2261 * gtk_text_view_set_tabs:
2262 * @text_view: a #GtkTextView
2263 * @tabs: tabs as a #PangoTabArray
2265 * Sets the default tab stops for paragraphs in @text_view.
2266 * Tags in the buffer may override the default.
2270 gtk_text_view_set_tabs (GtkTextView *text_view,
2271 PangoTabArray *tabs)
2273 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2275 if (text_view->tabs)
2276 pango_tab_array_free (text_view->tabs);
2278 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2280 if (text_view->layout)
2282 /* some unkosher futzing in internal struct details... */
2283 if (text_view->layout->default_style->tabs)
2284 pango_tab_array_free (text_view->layout->default_style->tabs);
2286 text_view->layout->default_style->tabs =
2287 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2289 gtk_text_layout_default_style_changed (text_view->layout);
2292 g_object_notify (G_OBJECT (text_view), "tabs");
2296 * gtk_text_view_get_tabs:
2297 * @text_view: a #GtkTextView
2299 * Gets the default tabs for @text_view. Tags in the buffer may
2300 * override the defaults. The returned array will be %NULL if
2301 * "standard" (8-space) tabs are used. Free the return value
2302 * with pango_tab_array_free().
2304 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2307 gtk_text_view_get_tabs (GtkTextView *text_view)
2309 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2311 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2315 * gtk_text_view_set_cursor_visible:
2316 * @text_view: a #GtkTextView
2317 * @setting: whether to show the insertion cursor
2319 * Toggles whether the insertion point is displayed. A buffer with no editable
2320 * text probably shouldn't have a visible cursor, so you may want to turn
2324 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2327 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2329 setting = (setting != FALSE);
2331 if (text_view->cursor_visible != setting)
2333 text_view->cursor_visible = setting;
2335 if (GTK_WIDGET_HAS_FOCUS (text_view))
2337 if (text_view->layout)
2339 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2340 gtk_text_view_check_cursor_blink (text_view);
2344 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2349 * gtk_text_view_get_cursor_visible:
2350 * @text_view: a #GtkTextView
2352 * Find out whether the cursor is being displayed.
2354 * Return value: whether the insertion mark is visible
2357 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2359 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2361 return text_view->cursor_visible;
2366 * gtk_text_view_place_cursor_onscreen:
2367 * @text_view: a #GtkTextView
2369 * Moves the cursor to the currently visible region of the
2370 * buffer, it it isn't there already.
2372 * Return value: TRUE if the cursor had to be moved.
2375 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2379 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2381 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2382 gtk_text_buffer_get_mark (get_buffer (text_view),
2385 if (clamp_iter_onscreen (text_view, &insert))
2387 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2395 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2397 if (text_view->first_validate_idle != 0)
2399 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2400 g_source_remove (text_view->first_validate_idle);
2401 text_view->first_validate_idle = 0;
2404 if (text_view->incremental_validate_idle != 0)
2406 g_source_remove (text_view->incremental_validate_idle);
2407 text_view->incremental_validate_idle = 0;
2412 gtk_text_view_destroy (GtkObject *object)
2414 GtkTextView *text_view;
2415 GtkTextLayout *layout;
2417 text_view = GTK_TEXT_VIEW (object);
2419 layout = text_view->layout;
2421 gtk_text_view_remove_validate_idles (text_view);
2422 gtk_text_view_set_buffer (text_view, NULL);
2423 gtk_text_view_destroy_layout (text_view);
2425 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2429 gtk_text_view_finalize (GObject *object)
2431 GtkTextView *text_view;
2433 text_view = GTK_TEXT_VIEW (object);
2435 g_return_if_fail (text_view->buffer == NULL);
2437 gtk_text_view_destroy_layout (text_view);
2438 gtk_text_view_set_buffer (text_view, NULL);
2440 cancel_pending_scroll (text_view);
2442 if (text_view->tabs)
2443 pango_tab_array_free (text_view->tabs);
2445 if (text_view->hadjustment)
2446 g_object_unref (G_OBJECT (text_view->hadjustment));
2447 if (text_view->vadjustment)
2448 g_object_unref (G_OBJECT (text_view->vadjustment));
2450 text_window_free (text_view->text_window);
2452 if (text_view->left_window)
2453 text_window_free (text_view->left_window);
2455 if (text_view->top_window)
2456 text_window_free (text_view->top_window);
2458 if (text_view->right_window)
2459 text_window_free (text_view->right_window);
2461 if (text_view->bottom_window)
2462 text_window_free (text_view->bottom_window);
2464 g_object_unref (G_OBJECT (text_view->im_context));
2466 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2470 gtk_text_view_set_property (GObject *object,
2472 const GValue *value,
2475 GtkTextView *text_view;
2477 text_view = GTK_TEXT_VIEW (object);
2481 case PROP_PIXELS_ABOVE_LINES:
2482 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2485 case PROP_PIXELS_BELOW_LINES:
2486 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2489 case PROP_PIXELS_INSIDE_WRAP:
2490 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2494 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2497 case PROP_WRAP_MODE:
2498 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2501 case PROP_JUSTIFICATION:
2502 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2505 case PROP_LEFT_MARGIN:
2506 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2509 case PROP_RIGHT_MARGIN:
2510 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2514 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2518 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2521 case PROP_CURSOR_VISIBLE:
2522 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2526 g_assert_not_reached ();
2532 gtk_text_view_get_property (GObject *object,
2537 GtkTextView *text_view;
2539 text_view = GTK_TEXT_VIEW (object);
2543 case PROP_PIXELS_ABOVE_LINES:
2544 g_value_set_int (value, text_view->pixels_above_lines);
2547 case PROP_PIXELS_BELOW_LINES:
2548 g_value_set_int (value, text_view->pixels_below_lines);
2551 case PROP_PIXELS_INSIDE_WRAP:
2552 g_value_set_int (value, text_view->pixels_inside_wrap);
2556 g_value_set_boolean (value, text_view->editable);
2559 case PROP_WRAP_MODE:
2560 g_value_set_enum (value, text_view->wrap_mode);
2563 case PROP_JUSTIFICATION:
2564 g_value_set_enum (value, text_view->justify);
2567 case PROP_LEFT_MARGIN:
2568 g_value_set_int (value, text_view->left_margin);
2571 case PROP_RIGHT_MARGIN:
2572 g_value_set_int (value, text_view->right_margin);
2576 g_value_set_int (value, text_view->indent);
2580 g_value_set_boxed (value, text_view->tabs);
2583 case PROP_CURSOR_VISIBLE:
2584 g_value_set_boolean (value, text_view->cursor_visible);
2588 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2594 gtk_text_view_size_request (GtkWidget *widget,
2595 GtkRequisition *requisition)
2597 GtkTextView *text_view;
2599 gint focus_edge_width;
2601 gboolean interior_focus;
2603 text_view = GTK_TEXT_VIEW (widget);
2605 gtk_widget_style_get (widget,
2606 "interior_focus", &interior_focus,
2607 "focus_line_width", &focus_width,
2611 focus_edge_width = 0;
2613 focus_edge_width = focus_width;
2615 if (text_view->layout)
2617 text_view->text_window->requisition.width = text_view->layout->width;
2618 text_view->text_window->requisition.height = text_view->layout->height;
2622 text_view->text_window->requisition.width = 0;
2623 text_view->text_window->requisition.height = 0;
2626 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2627 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2629 if (text_view->left_window)
2630 requisition->width += text_view->left_window->requisition.width;
2632 if (text_view->right_window)
2633 requisition->width += text_view->right_window->requisition.width;
2635 if (text_view->top_window)
2636 requisition->height += text_view->top_window->requisition.height;
2638 if (text_view->bottom_window)
2639 requisition->height += text_view->bottom_window->requisition.height;
2641 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2642 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2644 tmp_list = text_view->children;
2645 while (tmp_list != NULL)
2647 GtkTextViewChild *child = tmp_list->data;
2651 GtkRequisition child_req;
2652 GtkRequisition old_req;
2654 gtk_widget_get_child_requisition (child->widget, &old_req);
2656 gtk_widget_size_request (child->widget, &child_req);
2658 gtk_widget_get_child_requisition (child->widget, &child_req);
2660 /* Invalidate layout lines if required */
2661 if (text_view->layout &&
2662 (old_req.width != child_req.width ||
2663 old_req.height != child_req.height))
2664 gtk_text_child_anchor_queue_resize (child->anchor,
2669 GtkRequisition child_req;
2671 gtk_widget_size_request (child->widget, &child_req);
2674 tmp_list = g_slist_next (tmp_list);
2679 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2680 GtkTextViewChild *vc,
2681 GtkAllocation *allocation)
2687 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2691 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2694 buffer_y += vc->from_top_of_line;
2696 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2697 allocation->y = buffer_y - text_view->yoffset;
2699 gtk_widget_get_child_requisition (vc->widget, &req);
2700 allocation->width = req.width;
2701 allocation->height = req.height;
2705 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2706 GtkTextViewChild *vc)
2708 GtkAllocation allocation;
2710 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2712 gtk_widget_size_allocate (vc->widget, &allocation);
2715 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2717 vc->widget->allocation.x,
2718 vc->widget->allocation.y,
2719 text_view->yoffset);
2724 gtk_text_view_child_allocated (GtkTextLayout *layout,
2730 GtkTextViewChild *vc = NULL;
2731 GtkTextView *text_view = data;
2733 /* x,y is the position of the child from the top of the line, and
2734 * from the left of the buffer. We have to translate that into text
2735 * window coordinates, then size_allocate the child.
2738 vc = g_object_get_data (G_OBJECT (child),
2739 "gtk-text-view-child");
2741 g_assert (vc != NULL);
2743 DV (g_print ("child allocated at %d,%d\n", x, y));
2745 vc->from_left_of_buffer = x;
2746 vc->from_top_of_line = y;
2748 gtk_text_view_update_child_allocation (text_view, vc);
2752 gtk_text_view_allocate_children (GtkTextView *text_view)
2756 DV(g_print(G_STRLOC"\n"));
2758 tmp_list = text_view->children;
2759 while (tmp_list != NULL)
2761 GtkTextViewChild *child = tmp_list->data;
2765 /* We need to force-validate the regions containing
2768 GtkTextIter child_loc;
2769 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2773 gtk_text_layout_validate_yrange (text_view->layout,
2779 GtkAllocation allocation;
2780 GtkRequisition child_req;
2782 g_assert (child != NULL);
2784 allocation.x = child->x;
2785 allocation.y = child->y;
2787 gtk_widget_get_child_requisition (child->widget, &child_req);
2789 allocation.width = child_req.width;
2790 allocation.height = child_req.height;
2792 gtk_widget_size_allocate (child->widget, &allocation);
2795 tmp_list = g_slist_next (tmp_list);
2800 gtk_text_view_size_allocate (GtkWidget *widget,
2801 GtkAllocation *allocation)
2803 GtkTextView *text_view;
2804 GtkTextIter first_para;
2806 GtkAdjustment *vadj;
2807 gboolean yoffset_changed = FALSE;
2809 GdkRectangle text_rect;
2810 GdkRectangle left_rect;
2811 GdkRectangle right_rect;
2812 GdkRectangle top_rect;
2813 GdkRectangle bottom_rect;
2814 gint focus_edge_width;
2816 gboolean interior_focus;
2817 gboolean size_changed;
2819 text_view = GTK_TEXT_VIEW (widget);
2821 DV(g_print(G_STRLOC"\n"));
2824 widget->allocation.width != allocation->width ||
2825 widget->allocation.height != allocation->height;
2827 widget->allocation = *allocation;
2829 if (GTK_WIDGET_REALIZED (widget))
2831 gdk_window_move_resize (widget->window,
2832 allocation->x, allocation->y,
2833 allocation->width, allocation->height);
2836 /* distribute width/height among child windows. Ensure all
2837 * windows get at least a 1x1 allocation.
2840 gtk_widget_style_get (widget,
2841 "interior_focus", &interior_focus,
2842 "focus_line_width", &focus_width,
2846 focus_edge_width = 0;
2848 focus_edge_width = focus_width;
2850 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2852 if (text_view->left_window)
2853 left_rect.width = text_view->left_window->requisition.width;
2855 left_rect.width = 0;
2857 width -= left_rect.width;
2859 if (text_view->right_window)
2860 right_rect.width = text_view->right_window->requisition.width;
2862 right_rect.width = 0;
2864 width -= right_rect.width;
2866 text_rect.width = MAX (1, width);
2868 top_rect.width = text_rect.width;
2869 bottom_rect.width = text_rect.width;
2872 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2874 if (text_view->top_window)
2875 top_rect.height = text_view->top_window->requisition.height;
2877 top_rect.height = 0;
2879 height -= top_rect.height;
2881 if (text_view->bottom_window)
2882 bottom_rect.height = text_view->bottom_window->requisition.height;
2884 bottom_rect.height = 0;
2886 height -= bottom_rect.height;
2888 text_rect.height = MAX (1, height);
2890 left_rect.height = text_rect.height;
2891 right_rect.height = text_rect.height;
2894 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2895 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2897 text_rect.x = left_rect.x + left_rect.width;
2898 text_rect.y = top_rect.y + top_rect.height;
2900 left_rect.y = text_rect.y;
2901 right_rect.y = text_rect.y;
2903 top_rect.x = text_rect.x;
2904 bottom_rect.x = text_rect.x;
2906 right_rect.x = text_rect.x + text_rect.width;
2907 bottom_rect.y = text_rect.y + text_rect.height;
2909 text_window_size_allocate (text_view->text_window,
2912 if (text_view->left_window)
2913 text_window_size_allocate (text_view->left_window,
2916 if (text_view->right_window)
2917 text_window_size_allocate (text_view->right_window,
2920 if (text_view->top_window)
2921 text_window_size_allocate (text_view->top_window,
2924 if (text_view->bottom_window)
2925 text_window_size_allocate (text_view->bottom_window,
2928 gtk_text_view_update_layout_width (text_view);
2930 /* Note that this will do some layout validation */
2931 gtk_text_view_allocate_children (text_view);
2933 /* Now adjust the value of the adjustment to keep the cursor at the
2934 * same place in the buffer
2936 gtk_text_view_get_first_para_iter (text_view, &first_para);
2937 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2939 y += text_view->first_para_pixels;
2941 /* Ensure h/v adj exist */
2942 get_hadjustment (text_view);
2943 get_vadjustment (text_view);
2945 vadj = text_view->vadjustment;
2946 if (y > vadj->upper - vadj->page_size)
2947 y = MAX (0, vadj->upper - vadj->page_size);
2949 if (y != text_view->yoffset)
2952 yoffset_changed = TRUE;
2955 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2956 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
2957 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
2958 text_view->hadjustment->lower = 0;
2959 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2961 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2963 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2964 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2965 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2966 text_view->vadjustment->lower = 0;
2967 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2969 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2971 if (yoffset_changed)
2972 gtk_adjustment_value_changed (vadj);
2974 /* The GTK resize loop processes all the pending exposes right
2975 * after doing the resize stuff, so the idle sizer won't have a
2976 * chance to run. So we do the work here.
2978 gtk_text_view_flush_first_validate (text_view);
2980 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2983 if (size_changed && GTK_WIDGET_REALIZED (widget))
2984 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2988 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2991 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2992 text_view->first_para_mark);
2996 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2998 GtkWidget *widget = GTK_WIDGET (text_view);
3000 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3002 if (SCREEN_HEIGHT (widget) > 0)
3004 GtkTextIter first_para;
3006 /* Be sure we've validated the stuff onscreen; if we
3007 * scrolled, these calls won't have any effect, because
3008 * they were called in the recursive validate_onscreen
3010 gtk_text_view_get_first_para_iter (text_view, &first_para);
3012 gtk_text_layout_validate_yrange (text_view->layout,
3015 text_view->first_para_pixels +
3016 SCREEN_HEIGHT (widget));
3019 text_view->onscreen_validated = TRUE;
3021 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3023 /* This can have the odd side effect of triggering a scroll, which should
3024 * flip "onscreen_validated" back to FALSE, but should also get us
3025 * back into this function to turn it on again.
3027 gtk_text_view_update_adjustments (text_view);
3029 g_assert (text_view->onscreen_validated);
3033 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3035 if (text_view->first_validate_idle == 0)
3038 /* Do this first, which means that if an "invalidate"
3039 * occurs during any of this process, a new first_validate_callback
3040 * will be installed, and we'll start again.
3042 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3043 g_source_remove (text_view->first_validate_idle);
3044 text_view->first_validate_idle = 0;
3046 /* be sure we have up-to-date screen size set on the
3049 gtk_text_view_update_layout_width (text_view);
3051 /* Bail out if we invalidated stuff; scrolling right away will just
3052 * confuse the issue.
3054 if (text_view->first_validate_idle != 0)
3056 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3060 /* scroll to any marks, if that's pending. This can jump us to
3061 * the validation codepath used for scrolling onscreen, if so we
3062 * bail out. It won't jump if already in that codepath since
3063 * value_changed is not recursive, so also validate if
3066 if (!gtk_text_view_flush_scroll (text_view) ||
3067 !text_view->onscreen_validated)
3068 gtk_text_view_validate_onscreen (text_view);
3070 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3072 g_assert (text_view->onscreen_validated);
3077 first_validate_callback (gpointer data)
3079 GtkTextView *text_view = data;
3081 GDK_THREADS_ENTER ();
3083 /* Note that some of this code is duplicated at the end of size_allocate,
3084 * keep in sync with that.
3087 DV(g_print(G_STRLOC"\n"));
3089 gtk_text_view_flush_first_validate (text_view);
3091 GDK_THREADS_LEAVE ();
3097 incremental_validate_callback (gpointer data)
3099 GtkTextView *text_view = data;
3100 gboolean result = TRUE;
3102 GDK_THREADS_ENTER ();
3104 DV(g_print(G_STRLOC"\n"));
3106 gtk_text_layout_validate (text_view->layout, 2000);
3108 gtk_text_view_update_adjustments (text_view);
3110 if (gtk_text_layout_is_valid (text_view->layout))
3112 text_view->incremental_validate_idle = 0;
3116 GDK_THREADS_LEAVE ();
3122 gtk_text_view_invalidate (GtkTextView *text_view)
3124 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3125 text_view->onscreen_validated));
3127 text_view->onscreen_validated = FALSE;
3129 /* We'll invalidate when the layout is created */
3130 if (text_view->layout == NULL)
3133 if (!text_view->first_validate_idle)
3135 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3136 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3137 text_view->first_validate_idle));
3140 if (!text_view->incremental_validate_idle)
3142 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3143 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3144 text_view->incremental_validate_idle));
3149 invalidated_handler (GtkTextLayout *layout,
3152 GtkTextView *text_view;
3154 text_view = GTK_TEXT_VIEW (data);
3156 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3157 gtk_text_view_invalidate (text_view);
3161 changed_handler (GtkTextLayout *layout,
3167 GtkTextView *text_view;
3169 GdkRectangle visible_rect;
3170 GdkRectangle redraw_rect;
3172 text_view = GTK_TEXT_VIEW (data);
3173 widget = GTK_WIDGET (data);
3175 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3177 if (GTK_WIDGET_REALIZED (text_view))
3179 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3181 redraw_rect.x = visible_rect.x;
3182 redraw_rect.width = visible_rect.width;
3183 redraw_rect.y = start_y;
3185 if (old_height == new_height)
3186 redraw_rect.height = old_height;
3187 else if (start_y + old_height > visible_rect.y)
3188 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3190 redraw_rect.height = 0;
3192 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3194 /* text_window_invalidate_rect() takes buffer coordinates */
3195 text_window_invalidate_rect (text_view->text_window,
3198 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3202 redraw_rect.height));
3204 if (text_view->left_window)
3205 text_window_invalidate_rect (text_view->left_window,
3207 if (text_view->right_window)
3208 text_window_invalidate_rect (text_view->right_window,
3210 if (text_view->top_window)
3211 text_window_invalidate_rect (text_view->top_window,
3213 if (text_view->bottom_window)
3214 text_window_invalidate_rect (text_view->bottom_window,
3217 gtk_text_view_update_im_spot_location (text_view);
3221 if (old_height != new_height)
3223 gboolean yoffset_changed = FALSE;
3225 int new_first_para_top;
3226 int old_first_para_top;
3229 /* If the bottom of the old area was above the top of the
3230 * screen, we need to scroll to keep the current top of the
3231 * screen in place. Remember that first_para_pixels is the
3232 * position of the top of the screen in coordinates relative to
3233 * the first paragraph onscreen.
3235 * In short we are adding the height delta of the portion of the
3236 * changed region above first_para_mark to text_view->yoffset.
3238 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3239 text_view->first_para_mark);
3241 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3243 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3245 if (new_first_para_top != old_first_para_top)
3247 text_view->yoffset += new_first_para_top - old_first_para_top;
3249 get_vadjustment (text_view)->value = text_view->yoffset;
3250 yoffset_changed = TRUE;
3253 if (yoffset_changed)
3255 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3256 gtk_adjustment_value_changed (get_vadjustment (text_view));
3259 /* FIXME be smarter about which anchored widgets we update */
3261 tmp_list = text_view->children;
3262 while (tmp_list != NULL)
3264 GtkTextViewChild *child = tmp_list->data;
3267 gtk_text_view_update_child_allocation (text_view, child);
3269 tmp_list = g_slist_next (tmp_list);
3274 GtkRequisition old_req;
3275 GtkRequisition new_req;
3277 old_req = widget->requisition;
3279 /* Use this instead of gtk_widget_size_request wrapper
3280 * to avoid the optimization which just returns widget->requisition
3281 * if a resize hasn't been queued.
3283 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3285 if (old_req.width != new_req.width ||
3286 old_req.height != new_req.height)
3288 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3289 _gtk_size_group_queue_resize (widget);
3295 gtk_text_view_realize (GtkWidget *widget)
3297 GtkTextView *text_view;
3298 GdkWindowAttr attributes;
3299 gint attributes_mask;
3302 text_view = GTK_TEXT_VIEW (widget);
3303 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3305 attributes.window_type = GDK_WINDOW_CHILD;
3306 attributes.x = widget->allocation.x;
3307 attributes.y = widget->allocation.y;
3308 attributes.width = widget->allocation.width;
3309 attributes.height = widget->allocation.height;
3310 attributes.wclass = GDK_INPUT_OUTPUT;
3311 attributes.visual = gtk_widget_get_visual (widget);
3312 attributes.colormap = gtk_widget_get_colormap (widget);
3313 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3315 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3317 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3318 &attributes, attributes_mask);
3319 gdk_window_set_user_data (widget->window, widget);
3321 /* must come before text_window_realize calls */
3322 widget->style = gtk_style_attach (widget->style, widget->window);
3324 gdk_window_set_background (widget->window,
3325 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3327 text_window_realize (text_view->text_window, widget->window);
3329 if (text_view->left_window)
3330 text_window_realize (text_view->left_window,
3333 if (text_view->top_window)
3334 text_window_realize (text_view->top_window,
3337 if (text_view->right_window)
3338 text_window_realize (text_view->right_window,
3341 if (text_view->bottom_window)
3342 text_window_realize (text_view->bottom_window,
3345 gtk_text_view_ensure_layout (text_view);
3347 if (text_view->buffer)
3349 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3350 GDK_SELECTION_PRIMARY);
3351 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3354 tmp_list = text_view->children;
3355 while (tmp_list != NULL)
3357 GtkTextViewChild *vc = tmp_list->data;
3359 text_view_child_set_parent_window (text_view, vc);
3361 tmp_list = tmp_list->next;
3366 gtk_text_view_unrealize (GtkWidget *widget)
3368 GtkTextView *text_view;
3370 text_view = GTK_TEXT_VIEW (widget);
3372 if (text_view->buffer)
3374 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3375 GDK_SELECTION_PRIMARY);
3376 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3379 gtk_text_view_remove_validate_idles (text_view);
3381 if (text_view->popup_menu)
3383 gtk_widget_destroy (text_view->popup_menu);
3384 text_view->popup_menu = NULL;
3387 text_window_unrealize (text_view->text_window);
3389 if (text_view->left_window)
3390 text_window_unrealize (text_view->left_window);
3392 if (text_view->top_window)
3393 text_window_unrealize (text_view->top_window);
3395 if (text_view->right_window)
3396 text_window_unrealize (text_view->right_window);
3398 if (text_view->bottom_window)
3399 text_window_unrealize (text_view->bottom_window);
3401 gtk_text_view_destroy_layout (text_view);
3403 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3407 gtk_text_view_style_set (GtkWidget *widget,
3408 GtkStyle *previous_style)
3410 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3412 if (GTK_WIDGET_REALIZED (widget))
3414 gdk_window_set_background (widget->window,
3415 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3417 gdk_window_set_background (text_view->text_window->bin_window,
3418 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3420 if (text_view->left_window)
3421 gdk_window_set_background (text_view->left_window->bin_window,
3422 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3423 if (text_view->right_window)
3424 gdk_window_set_background (text_view->right_window->bin_window,
3425 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3427 if (text_view->top_window)
3428 gdk_window_set_background (text_view->top_window->bin_window,
3429 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3431 if (text_view->bottom_window)
3432 gdk_window_set_background (text_view->bottom_window->bin_window,
3433 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3436 if (text_view->layout && previous_style)
3438 gtk_text_view_set_attributes_from_style (text_view,
3439 text_view->layout->default_style,
3441 gtk_text_layout_default_style_changed (text_view->layout);
3446 gtk_text_view_direction_changed (GtkWidget *widget,
3447 GtkTextDirection previous_direction)
3449 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3451 if (text_view->layout)
3453 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3454 gtk_text_layout_default_style_changed (text_view->layout);
3460 set_invisible_cursor (GdkWindow *window)
3462 GdkBitmap *empty_bitmap;
3465 char invisible_cursor_bits[] = { 0x0 };
3467 useless.red = useless.green = useless.blue = 0;
3470 empty_bitmap = gdk_bitmap_create_from_data (window,
3471 invisible_cursor_bits,
3474 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3479 gdk_window_set_cursor (window, cursor);
3481 gdk_cursor_unref (cursor);
3483 g_object_unref (empty_bitmap);
3487 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3489 if (text_view->mouse_cursor_obscured)
3492 set_invisible_cursor (text_view->text_window->bin_window);
3494 text_view->mouse_cursor_obscured = TRUE;
3502 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3505 switch (event->type)
3507 case GDK_MOTION_NOTIFY:
3508 *x = event->motion.x;
3509 *y = event->motion.y;
3513 case GDK_BUTTON_PRESS:
3514 case GDK_2BUTTON_PRESS:
3515 case GDK_3BUTTON_PRESS:
3516 case GDK_BUTTON_RELEASE:
3517 *x = event->button.x;
3518 *y = event->button.y;
3523 case GDK_KEY_RELEASE:
3524 case GDK_ENTER_NOTIFY:
3525 case GDK_LEAVE_NOTIFY:
3526 case GDK_PROPERTY_NOTIFY:
3527 case GDK_SELECTION_CLEAR:
3528 case GDK_SELECTION_REQUEST:
3529 case GDK_SELECTION_NOTIFY:
3530 case GDK_PROXIMITY_IN:
3531 case GDK_PROXIMITY_OUT:
3532 case GDK_DRAG_ENTER:
3533 case GDK_DRAG_LEAVE:
3534 case GDK_DRAG_MOTION:
3535 case GDK_DRAG_STATUS:
3536 case GDK_DROP_START:
3537 case GDK_DROP_FINISHED:
3547 emit_event_on_tags (GtkWidget *widget,
3553 gboolean retval = FALSE;
3554 GtkTextView *text_view;
3556 text_view = GTK_TEXT_VIEW (widget);
3558 tags = gtk_text_iter_get_tags (iter);
3563 GtkTextTag *tag = tmp->data;
3565 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3571 tmp = g_slist_next (tmp);
3574 g_slist_free (tags);
3580 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3582 GtkTextView *text_view;
3585 text_view = GTK_TEXT_VIEW (widget);
3587 if (text_view->layout == NULL ||
3588 get_buffer (text_view) == NULL)
3591 if (event->any.window != text_view->text_window->bin_window)
3594 if (get_event_coordinates (event, &x, &y))
3598 x += text_view->xoffset;
3599 y += text_view->yoffset;
3601 /* FIXME this is slow and we do it twice per event.
3602 * My favorite solution is to have GtkTextLayout cache
3603 * the last couple lookups.
3605 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3609 return emit_event_on_tags (widget, event, &iter);
3611 else if (event->type == GDK_KEY_PRESS ||
3612 event->type == GDK_KEY_RELEASE)
3614 GtkTextMark *insert;
3617 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3620 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3622 return emit_event_on_tags (widget, event, &iter);
3629 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3631 gboolean retval = FALSE;
3632 gboolean obscure = FALSE;
3633 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3634 GtkTextMark *insert;
3636 gboolean can_insert;
3638 if (text_view->layout == NULL ||
3639 get_buffer (text_view) == NULL)
3642 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3643 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3644 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3646 gtk_im_context_filter_keypress (text_view->im_context, event))
3648 text_view->need_im_reset = TRUE;
3653 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3654 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3656 /* use overall editability not can_insert, more predictable for users */
3657 else if (text_view->editable &&
3658 (event->keyval == GDK_Return ||
3659 event->keyval == GDK_KP_Enter))
3661 /* this won't actually insert the newline if the cursor isn't
3664 gtk_text_view_commit_text (text_view, "\n");
3669 /* Pass through Tab as literal tab, unless Control is held down */
3670 else if ((event->keyval == GDK_Tab ||
3671 event->keyval == GDK_KP_Tab) &&
3672 !(event->state & GDK_CONTROL_MASK))
3674 /* If the text widget isn't editable overall, move the focus
3677 if (text_view->editable)
3679 gtk_text_view_commit_text (text_view, "\t");
3683 gtk_text_view_move_focus (text_view,
3684 (event->state & GDK_SHIFT_MASK) ?
3685 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3693 gtk_text_view_obscure_mouse_cursor (text_view);
3695 gtk_text_view_pend_cursor_blink (text_view);
3701 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3703 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3704 GtkTextMark *insert;
3707 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3710 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3711 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3712 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3713 gtk_im_context_filter_keypress (text_view->im_context, event))
3715 text_view->need_im_reset = TRUE;
3719 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3723 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3725 GtkTextView *text_view;
3727 text_view = GTK_TEXT_VIEW (widget);
3729 text_view->disable_scroll_on_focus = TRUE;
3730 gtk_widget_grab_focus (widget);
3731 text_view->disable_scroll_on_focus = FALSE;
3733 if (event->window != text_view->text_window->bin_window)
3735 /* Remove selection if any. */
3736 gtk_text_view_unselect (text_view);
3742 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3743 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3744 else if (event->button == 3)
3745 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3748 if (event->type == GDK_BUTTON_PRESS)
3750 gtk_text_view_reset_im_context (text_view);
3752 if (event->button == 1)
3754 /* If we're in the selection, start a drag copy/move of the
3755 * selection; otherwise, start creating a new selection.
3758 GtkTextIter start, end;
3760 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3762 event->x + text_view->xoffset,
3763 event->y + text_view->yoffset);
3765 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3767 gtk_text_iter_in_range (&iter, &start, &end))
3769 text_view->drag_start_x = event->x;
3770 text_view->drag_start_y = event->y;
3771 text_view->pending_place_cursor_button = event->button;
3775 gtk_text_view_start_selection_drag (text_view, &iter, event);
3780 else if (event->button == 2)
3784 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3786 event->x + text_view->xoffset,
3787 event->y + text_view->yoffset);
3789 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3790 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3792 text_view->editable);
3795 else if (event->button == 3)
3797 gtk_text_view_do_popup (text_view, event);
3800 else if ((event->type == GDK_2BUTTON_PRESS ||
3801 event->type == GDK_3BUTTON_PRESS) &&
3804 GtkTextIter start, end;
3806 /* End the selection drag, otherwise we'd clear the new
3807 * word/line selection on button release
3809 gtk_text_view_end_selection_drag (text_view, event);
3811 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3813 event->x + text_view->xoffset,
3814 event->y + text_view->yoffset);
3818 if (event->type == GDK_2BUTTON_PRESS)
3820 if (gtk_text_iter_inside_word (&start))
3822 if (!gtk_text_iter_starts_word (&start))
3823 gtk_text_iter_backward_word_start (&start);
3825 if (!gtk_text_iter_ends_word (&end))
3826 gtk_text_iter_forward_word_end (&end);
3829 else if (event->type == GDK_3BUTTON_PRESS)
3831 if (gtk_text_view_starts_display_line (text_view, &start))
3833 /* If on a display line boundary, we assume the user
3834 * clicked off the end of a line and we therefore select
3835 * the line before the boundary.
3837 gtk_text_view_backward_display_line_start (text_view, &start);
3841 /* start isn't on the start of a line, so we move it to the
3842 * start, and move end to the end unless it's already there.
3844 gtk_text_view_backward_display_line_start (text_view, &start);
3846 if (!gtk_text_view_starts_display_line (text_view, &end))
3847 gtk_text_view_forward_display_line_end (text_view, &end);
3851 if (event->state & GDK_SHIFT_MASK)
3853 /* Take union of old and new selection */
3854 GtkTextIter old_start, old_end;
3856 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3857 &old_start, &old_end);
3859 gtk_text_iter_order (&start, &old_start);
3860 gtk_text_iter_order (&old_end, &end);
3862 /* Now start is the first of the starts, and end is the
3867 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3870 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3874 text_view->just_selected_element = TRUE;
3883 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3885 GtkTextView *text_view;
3887 text_view = GTK_TEXT_VIEW (widget);
3889 if (event->window != text_view->text_window->bin_window)
3892 if (event->button == 1)
3894 if (text_view->drag_start_x >= 0)
3896 text_view->drag_start_x = -1;
3897 text_view->drag_start_y = -1;
3900 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3902 else if (text_view->just_selected_element)
3904 text_view->just_selected_element = FALSE;
3907 else if (text_view->pending_place_cursor_button == event->button)
3911 /* Unselect everything; we clicked inside selection, but
3912 * didn't move by the drag threshold, so just clear selection
3915 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3917 event->x + text_view->xoffset,
3918 event->y + text_view->yoffset);
3920 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3922 text_view->pending_place_cursor_button = 0;
3932 keymap_direction_changed (GdkKeymap *keymap,
3933 GtkTextView *text_view)
3935 gtk_text_view_check_keymap_direction (text_view);
3939 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3941 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3943 gtk_widget_queue_draw (widget);
3945 DV(g_print (G_STRLOC": focus_in_event\n"));
3947 if (text_view->cursor_visible && text_view->layout)
3949 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3950 gtk_text_view_check_cursor_blink (text_view);
3953 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3954 "direction_changed",
3955 G_CALLBACK (keymap_direction_changed), text_view);
3956 gtk_text_view_check_keymap_direction (text_view);
3958 text_view->need_im_reset = TRUE;
3959 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3965 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3967 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3969 gtk_widget_queue_draw (widget);
3971 DV(g_print (G_STRLOC": focus_out_event\n"));
3973 if (text_view->cursor_visible && text_view->layout)
3975 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3976 gtk_text_view_check_cursor_blink (text_view);
3979 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3980 (gpointer) keymap_direction_changed,
3983 text_view->need_im_reset = TRUE;
3984 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3990 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3992 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3994 if (text_view->mouse_cursor_obscured)
3998 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4000 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4001 gdk_cursor_unref (cursor);
4002 text_view->mouse_cursor_obscured = FALSE;
4005 if (event->window == text_view->text_window->bin_window &&
4006 text_view->drag_start_x >= 0)
4010 gdk_window_get_pointer (text_view->text_window->bin_window,
4013 if (gtk_drag_check_threshold (widget,
4014 text_view->drag_start_x,
4015 text_view->drag_start_y,
4019 gint buffer_x, buffer_y;
4021 gtk_text_view_window_to_buffer_coords (text_view,
4022 GTK_TEXT_WINDOW_TEXT,
4023 text_view->drag_start_x,
4024 text_view->drag_start_y,
4028 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4030 buffer_x, buffer_y);
4032 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4041 gtk_text_view_paint (GtkWidget *widget,
4043 GdkEventExpose *event)
4045 GtkTextView *text_view;
4046 GList *child_exposes;
4050 text_view = GTK_TEXT_VIEW (widget);
4052 g_return_if_fail (text_view->layout != NULL);
4053 g_return_if_fail (text_view->xoffset >= 0);
4054 g_return_if_fail (text_view->yoffset >= 0);
4056 while (text_view->first_validate_idle != 0)
4058 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4059 text_view->first_validate_idle));
4060 gtk_text_view_flush_first_validate (text_view);
4063 /* More regions could have become invalid in the above loop */
4064 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4069 gdk_region_get_clipbox (updates, &rect);
4071 gdk_rectangle_union (area, &rect, area);
4073 gdk_region_destroy (updates);
4076 if (!text_view->onscreen_validated)
4078 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.");
4079 g_assert_not_reached ();
4083 printf ("painting %d,%d %d x %d\n",
4085 area->width, area->height);
4088 child_exposes = NULL;
4089 gtk_text_layout_draw (text_view->layout,
4091 text_view->text_window->bin_window,
4096 area->width, area->height,
4099 tmp_list = child_exposes;
4100 while (tmp_list != NULL)
4102 GtkWidget *child = tmp_list->data;
4104 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4108 g_object_unref (G_OBJECT (child));
4110 tmp_list = tmp_list->next;
4113 g_list_free (child_exposes);
4117 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4121 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4122 GTK_TEXT_WINDOW_TEXT))
4124 DV(g_print (">Exposed ("G_STRLOC")\n"));
4125 gtk_text_view_paint (widget, &event->area, event);
4128 if (event->window == widget->window)
4129 gtk_text_view_draw_focus (widget);
4131 /* Propagate exposes to all children not in the buffer. */
4132 tmp_list = GTK_TEXT_VIEW (widget)->children;
4133 while (tmp_list != NULL)
4135 GtkTextViewChild *vc = tmp_list->data;
4137 /* propagate_expose checks that event->window matches
4140 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4141 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4145 tmp_list = tmp_list->next;
4152 gtk_text_view_draw_focus (GtkWidget *widget)
4154 gboolean interior_focus;
4156 /* We clear the focus if we are in interior focus mode. */
4157 gtk_widget_style_get (widget,
4158 "interior_focus", &interior_focus,
4161 if (GTK_WIDGET_DRAWABLE (widget))
4163 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4165 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4166 NULL, widget, "textview",
4168 widget->allocation.width,
4169 widget->allocation.height);
4173 gdk_window_clear (widget->window);
4179 gtk_text_view_grab_focus (GtkWidget *widget)
4181 GtkTextView *text_view;
4183 text_view = GTK_TEXT_VIEW (widget);
4185 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4187 if (!text_view->disable_scroll_on_focus)
4188 gtk_text_view_scroll_mark_onscreen (text_view,
4189 gtk_text_buffer_get_mark (get_buffer (text_view),
4194 gtk_text_view_focus (GtkWidget *widget,
4195 GtkDirectionType direction)
4197 GtkTextView *text_view;
4198 GtkContainer *container;
4201 text_view = GTK_TEXT_VIEW (widget);
4202 container = GTK_CONTAINER (widget);
4204 if (!gtk_widget_is_focus (widget) &&
4205 container->focus_child == NULL)
4207 gtk_widget_grab_focus (widget);
4213 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4214 * children to get the focus
4216 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4217 result = GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4218 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4229 gtk_text_view_add (GtkContainer *container,
4232 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4233 g_return_if_fail (GTK_IS_WIDGET (child));
4235 /* This is pretty random. */
4236 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4238 GTK_TEXT_WINDOW_WIDGET,
4243 gtk_text_view_remove (GtkContainer *container,
4247 GtkTextView *text_view;
4248 GtkTextViewChild *vc;
4250 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4251 g_return_if_fail (GTK_IS_WIDGET (child));
4252 g_return_if_fail (child->parent == (GtkWidget*) container);
4254 text_view = GTK_TEXT_VIEW (container);
4257 iter = text_view->children;
4259 while (iter != NULL)
4263 if (vc->widget == child)
4266 iter = g_slist_next (iter);
4269 g_assert (iter != NULL); /* be sure we had the child in the list */
4271 text_view->children = g_slist_remove (text_view->children, vc);
4273 gtk_widget_unparent (vc->widget);
4275 text_view_child_free (vc);
4279 gtk_text_view_forall (GtkContainer *container,
4280 gboolean include_internals,
4281 GtkCallback callback,
4282 gpointer callback_data)
4285 GtkTextView *text_view;
4288 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4289 g_return_if_fail (callback != NULL);
4291 text_view = GTK_TEXT_VIEW (container);
4293 copy = g_slist_copy (text_view->children);
4296 while (iter != NULL)
4298 GtkTextViewChild *vc = iter->data;
4300 (* callback) (vc->widget, callback_data);
4302 iter = g_slist_next (iter);
4305 g_slist_free (copy);
4308 #define CURSOR_ON_MULTIPLIER 0.66
4309 #define CURSOR_OFF_MULTIPLIER 0.34
4310 #define CURSOR_PEND_MULTIPLIER 1.0
4313 cursor_blinks (GtkTextView *text_view)
4315 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4318 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4321 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4324 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4329 get_cursor_time (GtkTextView *text_view)
4331 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4334 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4344 blink_cb (gpointer data)
4346 GtkTextView *text_view;
4349 GDK_THREADS_ENTER ();
4351 text_view = GTK_TEXT_VIEW (data);
4353 g_assert (text_view->layout);
4354 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4355 g_assert (text_view->cursor_visible);
4357 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4360 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4364 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4368 gtk_text_layout_set_cursor_visible (text_view->layout,
4371 GDK_THREADS_LEAVE ();
4373 /* Remove ourselves */
4379 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4381 if (text_view->blink_timeout)
4383 gtk_timeout_remove (text_view->blink_timeout);
4384 text_view->blink_timeout = 0;
4389 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4391 if (text_view->layout != NULL &&
4392 text_view->cursor_visible &&
4393 GTK_WIDGET_HAS_FOCUS (text_view))
4395 if (cursor_blinks (text_view))
4397 if (text_view->blink_timeout == 0)
4399 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4401 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4407 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4411 gtk_text_view_stop_cursor_blink (text_view);
4416 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4418 if (text_view->layout != NULL &&
4419 text_view->cursor_visible &&
4420 GTK_WIDGET_HAS_FOCUS (text_view) &&
4421 cursor_blinks (text_view))
4423 if (text_view->blink_timeout != 0)
4425 gtk_timeout_remove (text_view->blink_timeout);
4426 text_view->blink_timeout = 0;
4429 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4431 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4439 * Key binding handlers
4443 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4444 GtkTextIter *newplace,
4449 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4455 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4461 move_cursor (GtkTextView *text_view,
4462 const GtkTextIter *new_location,
4463 gboolean extend_selection)
4465 if (extend_selection)
4466 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4470 gtk_text_buffer_place_cursor (get_buffer (text_view),
4474 /* FIXME when we are unfrozen and can change GtkMovementStep,
4477 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4480 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4481 GtkMovementStep step,
4483 gboolean extend_selection)
4486 GtkTextIter newplace;
4488 gint cursor_x_pos = 0;
4490 gtk_text_view_reset_im_context (text_view);
4492 if (step == GTK_MOVEMENT_PAGES)
4494 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4495 gtk_text_view_pend_cursor_blink (text_view);
4498 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4500 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4501 gtk_text_view_pend_cursor_blink (text_view);
4505 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4506 gtk_text_buffer_get_mark (get_buffer (text_view),
4510 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4511 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4515 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4516 gtk_text_iter_forward_cursor_positions (&newplace, count);
4519 case GTK_MOVEMENT_VISUAL_POSITIONS:
4520 gtk_text_layout_move_iter_visually (text_view->layout,
4524 case GTK_MOVEMENT_WORDS:
4526 gtk_text_iter_backward_word_starts (&newplace, -count);
4528 gtk_text_iter_forward_word_ends (&newplace, count);
4531 case GTK_MOVEMENT_DISPLAY_LINES:
4532 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4533 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4536 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4538 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4539 else if (count < -1)
4540 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4543 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4546 case GTK_MOVEMENT_PARAGRAPHS:
4549 if (!gtk_text_iter_ends_line (&newplace))
4551 gtk_text_iter_forward_to_line_end (&newplace);
4557 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4559 gtk_text_iter_set_line_offset (&newplace, 0);
4566 gtk_text_iter_forward_lines (&newplace, count);
4567 gtk_text_iter_set_line_offset (&newplace, 0);
4571 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4574 if (!gtk_text_iter_ends_line (&newplace))
4575 gtk_text_iter_forward_to_line_end (&newplace);
4579 gtk_text_iter_set_line_offset (&newplace, 0);
4583 case GTK_MOVEMENT_BUFFER_ENDS:
4585 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4587 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4594 if (!gtk_text_iter_equal (&insert, &newplace))
4596 move_cursor (text_view, &newplace, extend_selection);
4598 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4599 gtk_text_view_scroll_mark_onscreen (text_view,
4600 gtk_text_buffer_get_mark (get_buffer (text_view),
4603 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4605 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4609 gtk_text_view_pend_cursor_blink (text_view);
4613 gtk_text_view_move_cursor (GtkTextView *text_view,
4614 GtkMovementStep step,
4616 gboolean extend_selection)
4618 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4622 gtk_text_view_page_horizontally (GtkTextView *text_view,
4624 gboolean extend_selection)
4626 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4627 count, extend_selection);
4631 gtk_text_view_set_anchor (GtkTextView *text_view)
4635 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4636 gtk_text_buffer_get_mark (get_buffer (text_view),
4639 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4643 gtk_text_view_scroll_pages (GtkTextView *text_view,
4645 gboolean extend_selection)
4650 gint cursor_x_pos, cursor_y_pos;
4651 GtkTextIter new_insert;
4655 g_return_if_fail (text_view->vadjustment != NULL);
4657 cancel_pending_scroll (text_view);
4659 adj = text_view->vadjustment;
4661 /* Validate the region that will be brought into view by the cursor motion
4665 gtk_text_view_get_first_para_iter (text_view, &anchor);
4666 y0 = adj->page_size;
4667 y1 = adj->page_size + count * adj->page_increment;
4671 gtk_text_view_get_first_para_iter (text_view, &anchor);
4672 y0 = count * adj->page_increment + adj->page_size;
4676 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4677 /* FIXME do we need to update the adjustment ranges here? */
4679 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4681 /* already at top, just be sure we are at offset 0 */
4682 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4683 move_cursor (text_view, &new_insert, extend_selection);
4685 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4687 /* already at bottom, just be sure we are at the end */
4688 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4689 move_cursor (text_view, &new_insert, extend_selection);
4693 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4695 newval = adj->value;
4696 oldval = adj->value;
4698 newval += count * adj->page_increment;
4700 set_adjustment_clamped (adj, newval);
4701 cursor_y_pos += adj->value - oldval;
4703 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4704 clamp_iter_onscreen (text_view, &new_insert);
4705 move_cursor (text_view, &new_insert, extend_selection);
4707 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4710 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4711 * only guarantees 1 pixel onscreen.
4713 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4714 gtk_text_view_scroll_mark_onscreen (text_view,
4715 gtk_text_buffer_get_mark (get_buffer (text_view),
4720 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4722 gboolean extend_selection)
4727 gint cursor_x_pos, cursor_y_pos;
4728 GtkTextIter new_insert;
4731 g_return_if_fail (text_view->hadjustment != NULL);
4733 cancel_pending_scroll (text_view);
4735 adj = text_view->hadjustment;
4737 /* Validate the line that we're moving within.
4739 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4741 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4742 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4743 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4744 /* FIXME do we need to update the adjustment ranges here? */
4746 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4748 /* already at far left, just be sure we are at offset 0 */
4749 gtk_text_iter_set_line_offset (&new_insert, 0);
4750 move_cursor (text_view, &new_insert, extend_selection);
4752 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4754 /* already at far right, just be sure we are at the end */
4755 gtk_text_iter_forward_to_line_end (&new_insert);
4756 move_cursor (text_view, &new_insert, extend_selection);
4760 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4762 newval = adj->value;
4763 oldval = adj->value;
4765 newval += count * adj->page_increment;
4767 set_adjustment_clamped (adj, newval);
4768 cursor_x_pos += adj->value - oldval;
4770 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4771 clamp_iter_onscreen (text_view, &new_insert);
4772 move_cursor (text_view, &new_insert, extend_selection);
4774 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4777 /* FIXME for lines shorter than the overall widget width, this results in a
4778 * "bounce" effect as we scroll to the right of the widget, then scroll
4779 * back to get the end of the line onscreen.
4780 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4783 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4784 * only guarantees 1 pixel onscreen.
4786 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4787 gtk_text_view_scroll_mark_onscreen (text_view,
4788 gtk_text_buffer_get_mark (get_buffer (text_view),
4793 whitespace (gunichar ch, gpointer user_data)
4795 return (ch == ' ' || ch == '\t');
4799 not_whitespace (gunichar ch, gpointer user_data)
4801 return !whitespace (ch, user_data);
4805 find_whitepace_region (const GtkTextIter *center,
4806 GtkTextIter *start, GtkTextIter *end)
4811 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4812 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4813 if (whitespace (gtk_text_iter_get_char (end), NULL))
4814 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4816 return !gtk_text_iter_equal (start, end);
4820 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4823 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4824 text_view->editable);
4828 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4835 gboolean leave_one = FALSE;
4837 gtk_text_view_reset_im_context (text_view);
4839 if (type == GTK_DELETE_CHARS)
4841 /* Char delete deletes the selection, if one exists */
4842 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4843 text_view->editable))
4847 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4849 gtk_text_buffer_get_mark (get_buffer (text_view),
4857 case GTK_DELETE_CHARS:
4858 gtk_text_iter_forward_cursor_positions (&end, count);
4861 case GTK_DELETE_WORD_ENDS:
4863 gtk_text_iter_forward_word_ends (&end, count);
4865 gtk_text_iter_backward_word_starts (&start, 0 - count);
4868 case GTK_DELETE_WORDS:
4871 case GTK_DELETE_DISPLAY_LINE_ENDS:
4874 case GTK_DELETE_DISPLAY_LINES:
4877 case GTK_DELETE_PARAGRAPH_ENDS:
4878 /* If we're already at a newline, we need to
4879 * simply delete that newline, instead of
4880 * moving to the next one.
4882 if (gtk_text_iter_ends_line (&end))
4884 gtk_text_iter_forward_line (&end);
4890 if (!gtk_text_iter_forward_to_line_end (&end))
4896 /* FIXME figure out what a negative count means
4900 case GTK_DELETE_PARAGRAPHS:
4903 gtk_text_iter_set_line_offset (&start, 0);
4904 gtk_text_iter_forward_to_line_end (&end);
4906 /* Do the lines beyond the first. */
4909 gtk_text_iter_forward_to_line_end (&end);
4915 /* FIXME negative count? */
4919 case GTK_DELETE_WHITESPACE:
4921 find_whitepace_region (&insert, &start, &end);
4929 if (!gtk_text_iter_equal (&start, &end))
4931 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4933 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4934 text_view->editable))
4937 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4939 text_view->editable);
4942 gtk_text_buffer_end_user_action (get_buffer (text_view));
4944 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4945 gtk_text_view_scroll_mark_onscreen (text_view,
4946 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4951 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4953 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4954 GDK_SELECTION_CLIPBOARD);
4956 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4958 text_view->editable);
4959 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4960 gtk_text_view_scroll_mark_onscreen (text_view,
4961 gtk_text_buffer_get_mark (get_buffer (text_view),
4966 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4968 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4969 GDK_SELECTION_CLIPBOARD);
4971 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4973 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4974 gtk_text_view_scroll_mark_onscreen (text_view,
4975 gtk_text_buffer_get_mark (get_buffer (text_view),
4980 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4982 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4983 GDK_SELECTION_CLIPBOARD);
4985 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4988 text_view->editable);
4989 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4990 gtk_text_view_scroll_mark_onscreen (text_view,
4991 gtk_text_buffer_get_mark (get_buffer (text_view),
4996 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4998 text_view->overwrite_mode = !text_view->overwrite_mode;
5002 gtk_text_view_move_focus (GtkTextView *text_view,
5003 GtkDirectionType direction_type)
5005 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5007 if (!GTK_WIDGET_TOPLEVEL (toplevel))
5010 /* Propagate to toplevel */
5011 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus", direction_type);
5019 gtk_text_view_unselect (GtkTextView *text_view)
5023 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5025 gtk_text_buffer_get_mark (get_buffer (text_view),
5028 gtk_text_buffer_move_mark (get_buffer (text_view),
5029 gtk_text_buffer_get_mark (get_buffer (text_view),
5035 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5036 const gchar *mark_name)
5039 GdkModifierType state;
5040 GtkTextIter newplace;
5042 /* DV(g_print (G_STRLOC": begin\n")); */
5044 gdk_window_get_pointer (text_view->text_window->bin_window,
5047 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5048 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5050 x + text_view->xoffset,
5051 y + text_view->yoffset);
5055 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5057 /* This may invalidate the layout */
5058 DV(g_print (G_STRLOC": move mark\n"));
5059 gtk_text_buffer_move_mark (get_buffer (text_view),
5063 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5064 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5067 DV (g_print ("first validate idle leaving %s is %d\n",
5068 G_STRLOC, text_view->first_validate_idle));
5072 selection_scan_timeout (gpointer data)
5074 GtkTextView *text_view;
5076 GDK_THREADS_ENTER ();
5078 text_view = GTK_TEXT_VIEW (data);
5080 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5081 move_mark_to_pointer_and_scroll (text_view, "insert");
5083 GDK_THREADS_LEAVE ();
5085 return TRUE; /* remain installed. */
5088 #define DND_SCROLL_MARGIN 0.20
5091 drag_scan_timeout (gpointer data)
5093 GtkTextView *text_view;
5095 GdkModifierType state;
5096 GtkTextIter newplace;
5098 GDK_THREADS_ENTER ();
5100 text_view = GTK_TEXT_VIEW (data);
5102 gdk_window_get_pointer (text_view->text_window->bin_window,
5105 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5107 x + text_view->xoffset,
5108 y + text_view->yoffset);
5110 gtk_text_buffer_move_mark (get_buffer (text_view),
5111 text_view->dnd_mark,
5114 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5115 gtk_text_view_scroll_to_mark (text_view,
5116 text_view->dnd_mark,
5117 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5119 GDK_THREADS_LEAVE ();
5125 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5127 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5128 move_mark_to_pointer_and_scroll (text_view, "insert");
5130 /* If we had to scroll offscreen, insert a timeout to do so
5131 * again. Note that in the timeout, even if the mouse doesn't
5132 * move, due to this scroll xoffset/yoffset will have changed
5133 * and we'll need to scroll again.
5135 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5136 gtk_timeout_remove (text_view->scroll_timeout);
5138 text_view->scroll_timeout =
5139 gtk_timeout_add (50, selection_scan_timeout, text_view);
5145 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5146 const GtkTextIter *iter,
5147 GdkEventButton *button)
5149 GtkTextIter newplace;
5150 GtkTextBuffer *buffer;
5152 g_return_if_fail (text_view->selection_drag_handler == 0);
5154 gtk_grab_add (GTK_WIDGET (text_view));
5156 buffer = get_buffer (text_view);
5160 if (button->state & GDK_SHIFT_MASK)
5162 /* Extend selection */
5163 GtkTextIter start, end;
5165 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5167 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5169 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5172 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5175 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5177 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5180 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5186 /* Replace selection */
5187 gtk_text_buffer_place_cursor (buffer, &newplace);
5190 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5191 "motion_notify_event",
5192 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5196 /* returns whether we were really dragging */
5198 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5200 if (text_view->selection_drag_handler == 0)
5203 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5204 text_view->selection_drag_handler = 0;
5206 if (text_view->scroll_timeout != 0)
5208 gtk_timeout_remove (text_view->scroll_timeout);
5209 text_view->scroll_timeout = 0;
5212 /* one last update to current position */
5213 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5214 move_mark_to_pointer_and_scroll (text_view, "insert");
5216 gtk_grab_remove (GTK_WIDGET (text_view));
5226 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5227 GtkTextAttributes *values,
5230 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5231 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5234 pango_font_description_free (values->font);
5236 values->font = pango_font_description_copy (style->font_desc);
5240 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5242 if (text_view->layout)
5244 gboolean split_cursor;
5245 GtkTextDirection new_dir;
5246 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5248 g_object_get (G_OBJECT (settings),
5249 "gtk-split-cursor", &split_cursor,
5253 new_dir = GTK_TEXT_DIR_NONE;
5257 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5258 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5259 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5262 if (text_view->layout->cursor_direction != new_dir)
5263 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5268 gtk_text_view_ensure_layout (GtkTextView *text_view)
5272 widget = GTK_WIDGET (text_view);
5274 if (text_view->layout == NULL)
5276 GtkTextAttributes *style;
5277 PangoContext *ltr_context, *rtl_context;
5280 DV(g_print(G_STRLOC"\n"));
5282 text_view->layout = gtk_text_layout_new ();
5284 g_signal_connect (G_OBJECT (text_view->layout),
5286 G_CALLBACK (invalidated_handler),
5289 g_signal_connect (G_OBJECT (text_view->layout),
5291 G_CALLBACK (changed_handler),
5294 g_signal_connect (G_OBJECT (text_view->layout),
5296 G_CALLBACK (gtk_text_view_child_allocated),
5299 if (get_buffer (text_view))
5300 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5302 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5303 gtk_text_view_pend_cursor_blink (text_view);
5305 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5307 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5308 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5309 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5310 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5312 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5314 g_object_unref (G_OBJECT (ltr_context));
5315 g_object_unref (G_OBJECT (rtl_context));
5317 gtk_text_view_check_keymap_direction (text_view);
5319 style = gtk_text_attributes_new ();
5321 gtk_widget_ensure_style (widget);
5322 gtk_text_view_set_attributes_from_style (text_view,
5323 style, widget->style);
5325 style->pixels_above_lines = text_view->pixels_above_lines;
5326 style->pixels_below_lines = text_view->pixels_below_lines;
5327 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5328 style->left_margin = text_view->left_margin;
5329 style->right_margin = text_view->right_margin;
5330 style->indent = text_view->indent;
5331 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5333 style->wrap_mode = text_view->wrap_mode;
5334 style->justification = text_view->justify;
5335 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5337 gtk_text_layout_set_default_style (text_view->layout, style);
5339 gtk_text_attributes_unref (style);
5341 /* Set layout for all anchored children */
5343 tmp_list = text_view->children;
5344 while (tmp_list != NULL)
5346 GtkTextViewChild *vc = tmp_list->data;
5350 gtk_text_anchored_child_set_layout (vc->widget,
5352 /* vc may now be invalid! */
5355 tmp_list = g_slist_next (tmp_list);
5358 gtk_text_view_invalidate (text_view);
5363 * gtk_text_view_get_default_attributes:
5364 * @text_view: a #GtkTextView
5366 * Obtains a copy of the default text attributes. These are the
5367 * attributes used for text unless a tag overrides them.
5368 * You'd typically pass the default attributes in to
5369 * gtk_text_iter_get_attributes() in order to get the
5370 * attributes in effect at a given text position.
5372 * The return value is a copy owned by the caller of this function,
5373 * and should be freed.
5375 * Return value: a new #GtkTextAttributes
5378 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5380 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5382 gtk_text_view_ensure_layout (text_view);
5384 return gtk_text_attributes_copy (text_view->layout->default_style);
5388 gtk_text_view_destroy_layout (GtkTextView *text_view)
5390 if (text_view->layout)
5394 gtk_text_view_remove_validate_idles (text_view);
5396 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5397 (gpointer) invalidated_handler,
5399 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5400 (gpointer) changed_handler,
5403 /* Remove layout from all anchored children */
5404 tmp_list = text_view->children;
5405 while (tmp_list != NULL)
5407 GtkTextViewChild *vc = tmp_list->data;
5411 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5412 /* vc may now be invalid! */
5415 tmp_list = g_slist_next (tmp_list);
5418 gtk_text_view_stop_cursor_blink (text_view);
5419 gtk_text_view_end_selection_drag (text_view, NULL);
5421 g_object_unref (G_OBJECT (text_view->layout));
5422 text_view->layout = NULL;
5427 gtk_text_view_reset_im_context (GtkTextView *text_view)
5429 if (text_view->need_im_reset)
5431 text_view->need_im_reset = FALSE;
5432 gtk_im_context_reset (text_view->im_context);
5441 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5442 const GtkTextIter *iter,
5443 GdkEventMotion *event)
5445 GdkDragContext *context;
5446 GtkTargetList *target_list;
5448 text_view->drag_start_x = -1;
5449 text_view->drag_start_y = -1;
5450 text_view->pending_place_cursor_button = 0;
5452 target_list = gtk_target_list_new (target_table,
5453 G_N_ELEMENTS (target_table));
5455 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5456 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5457 1, (GdkEvent*)event);
5459 gtk_target_list_unref (target_list);
5461 gtk_drag_set_icon_default (context);
5465 gtk_text_view_drag_begin (GtkWidget *widget,
5466 GdkDragContext *context)
5472 gtk_text_view_drag_end (GtkWidget *widget,
5473 GdkDragContext *context)
5475 GtkTextView *text_view;
5477 text_view = GTK_TEXT_VIEW (widget);
5481 gtk_text_view_drag_data_get (GtkWidget *widget,
5482 GdkDragContext *context,
5483 GtkSelectionData *selection_data,
5487 GtkTextView *text_view;
5489 text_view = GTK_TEXT_VIEW (widget);
5491 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5493 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5495 gtk_selection_data_set (selection_data,
5496 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5509 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5512 /* Extract the selected text */
5513 str = gtk_text_iter_get_visible_text (&start, &end);
5518 gtk_selection_data_set_text (selection_data, str, -1);
5525 gtk_text_view_drag_data_delete (GtkWidget *widget,
5526 GdkDragContext *context)
5528 GtkTextView *text_view;
5530 text_view = GTK_TEXT_VIEW (widget);
5532 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5533 TRUE, GTK_TEXT_VIEW (widget)->editable);
5537 gtk_text_view_drag_leave (GtkWidget *widget,
5538 GdkDragContext *context,
5541 GtkTextView *text_view;
5543 text_view = GTK_TEXT_VIEW (widget);
5545 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5547 if (text_view->scroll_timeout != 0)
5548 gtk_timeout_remove (text_view->scroll_timeout);
5550 text_view->scroll_timeout = 0;
5554 gtk_text_view_drag_motion (GtkWidget *widget,
5555 GdkDragContext *context,
5560 GtkTextIter newplace;
5561 GtkTextView *text_view;
5564 GdkRectangle target_rect;
5566 GdkDragAction suggested_action = 0;
5568 text_view = GTK_TEXT_VIEW (widget);
5570 target_rect = text_view->text_window->allocation;
5572 if (x < target_rect.x ||
5573 y < target_rect.y ||
5574 x > (target_rect.x + target_rect.width) ||
5575 y > (target_rect.y + target_rect.height))
5576 return FALSE; /* outside the text window, allow parent widgets to handle event */
5578 gtk_text_view_window_to_buffer_coords (text_view,
5579 GTK_TEXT_WINDOW_WIDGET,
5583 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5587 if (gtk_drag_dest_find_target (widget, context,
5588 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5590 /* can't accept any of the offered targets */
5592 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5594 gtk_text_iter_compare (&newplace, &start) >= 0 &&
5595 gtk_text_iter_compare (&newplace, &end) <= 0)
5597 /* We're inside the selection. */
5601 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5603 GtkWidget *source_widget;
5605 suggested_action = context->suggested_action;
5607 source_widget = gtk_drag_get_source_widget (context);
5609 if (source_widget == widget)
5611 /* Default to MOVE, unless the user has
5612 * pressed ctrl or alt to affect available actions
5614 if ((context->actions & GDK_ACTION_MOVE) != 0)
5615 suggested_action = GDK_ACTION_MOVE;
5620 /* Can't drop here. */
5624 if (suggested_action != 0)
5626 gtk_text_mark_set_visible (text_view->dnd_mark,
5627 text_view->cursor_visible);
5629 gdk_drag_status (context, suggested_action, time);
5633 gdk_drag_status (context, 0, time);
5634 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5637 gtk_text_buffer_move_mark (get_buffer (text_view),
5638 text_view->dnd_mark,
5641 DV(g_print (G_STRLOC": scrolling to mark\n"));
5642 gtk_text_view_scroll_to_mark (text_view,
5643 text_view->dnd_mark,
5644 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5646 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5647 gtk_timeout_remove (text_view->scroll_timeout);
5649 text_view->scroll_timeout =
5650 gtk_timeout_add (50, drag_scan_timeout, text_view);
5652 /* TRUE return means don't propagate the drag motion to parent
5653 * widgets that may also be drop sites.
5659 gtk_text_view_drag_drop (GtkWidget *widget,
5660 GdkDragContext *context,
5665 GtkTextView *text_view;
5666 GtkTextIter drop_point;
5667 GdkAtom target = GDK_NONE;
5669 text_view = GTK_TEXT_VIEW (widget);
5671 if (text_view->scroll_timeout != 0)
5672 gtk_timeout_remove (text_view->scroll_timeout);
5674 text_view->scroll_timeout = 0;
5676 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5678 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5680 text_view->dnd_mark);
5682 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5683 target = gtk_drag_dest_find_target (widget, context, NULL);
5685 if (target != GDK_NONE)
5686 gtk_drag_get_data (widget, context, target, time);
5688 gtk_drag_finish (context, FALSE, FALSE, time);
5694 insert_text_data (GtkTextView *text_view,
5695 GtkTextIter *drop_point,
5696 GtkSelectionData *selection_data)
5700 str = gtk_selection_data_get_text (selection_data);
5704 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5705 drop_point, str, -1,
5706 text_view->editable);
5712 gtk_text_view_drag_data_received (GtkWidget *widget,
5713 GdkDragContext *context,
5716 GtkSelectionData *selection_data,
5720 GtkTextIter drop_point;
5721 GtkTextView *text_view;
5722 gboolean success = FALSE;
5724 text_view = GTK_TEXT_VIEW (widget);
5726 if (!text_view->dnd_mark)
5729 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5731 text_view->dnd_mark);
5733 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5736 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5738 GtkTextBuffer *src_buffer = NULL;
5739 GtkTextIter start, end;
5740 gboolean copy_tags = TRUE;
5742 if (selection_data->length != sizeof (src_buffer))
5745 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5747 if (src_buffer == NULL)
5750 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5752 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5753 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5756 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5761 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5765 text_view->editable);
5770 str = gtk_text_iter_get_visible_text (&start, &end);
5771 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5772 &drop_point, str, -1,
5773 text_view->editable);
5779 insert_text_data (text_view, &drop_point, selection_data);
5784 gtk_drag_finish (context, success,
5785 success && context->action == GDK_ACTION_MOVE,
5789 static GtkAdjustment*
5790 get_hadjustment (GtkTextView *text_view)
5792 if (text_view->hadjustment == NULL)
5793 gtk_text_view_set_scroll_adjustments (text_view,
5794 NULL, /* forces creation */
5795 text_view->vadjustment);
5797 return text_view->hadjustment;
5800 static GtkAdjustment*
5801 get_vadjustment (GtkTextView *text_view)
5803 if (text_view->vadjustment == NULL)
5804 gtk_text_view_set_scroll_adjustments (text_view,
5805 text_view->hadjustment,
5806 NULL); /* forces creation */
5807 return text_view->vadjustment;
5812 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5813 GtkAdjustment *hadj,
5814 GtkAdjustment *vadj)
5816 gboolean need_adjust = FALSE;
5818 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5821 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5823 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5825 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5827 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5829 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5831 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5832 g_object_unref (G_OBJECT (text_view->hadjustment));
5835 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5837 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5838 g_object_unref (G_OBJECT (text_view->vadjustment));
5841 if (text_view->hadjustment != hadj)
5843 text_view->hadjustment = hadj;
5844 g_object_ref (G_OBJECT (text_view->hadjustment));
5845 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5847 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5848 (GtkSignalFunc) gtk_text_view_value_changed,
5853 if (text_view->vadjustment != vadj)
5855 text_view->vadjustment = vadj;
5856 g_object_ref (G_OBJECT (text_view->vadjustment));
5857 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5859 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5860 (GtkSignalFunc) gtk_text_view_value_changed,
5866 gtk_text_view_value_changed (NULL, text_view);
5869 /* FIXME this adjust_allocation is a big cut-and-paste from
5870 * GtkCList, needs to be some "official" way to do this
5880 /* The window to which widget->window is relative */
5881 #define ALLOCATION_WINDOW(widget) \
5882 (GTK_WIDGET_NO_WINDOW (widget) ? \
5883 (widget)->window : \
5884 gdk_window_get_parent ((widget)->window))
5887 adjust_allocation_recurse (GtkWidget *widget,
5890 ScrollData *scroll_data = data;
5892 /* Need to really size allocate instead of just poking
5893 * into widget->allocation if the widget is not realized.
5894 * FIXME someone figure out why this was.
5896 if (!GTK_WIDGET_REALIZED (widget))
5898 if (GTK_WIDGET_VISIBLE (widget))
5900 GdkRectangle tmp_rectangle = widget->allocation;
5901 tmp_rectangle.x += scroll_data->dx;
5902 tmp_rectangle.y += scroll_data->dy;
5904 gtk_widget_size_allocate (widget, &tmp_rectangle);
5909 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5911 widget->allocation.x += scroll_data->dx;
5912 widget->allocation.y += scroll_data->dy;
5914 if (GTK_IS_CONTAINER (widget))
5915 gtk_container_forall (GTK_CONTAINER (widget),
5916 adjust_allocation_recurse,
5923 adjust_allocation (GtkWidget *widget,
5927 ScrollData scroll_data;
5929 if (GTK_WIDGET_REALIZED (widget))
5930 scroll_data.window = ALLOCATION_WINDOW (widget);
5932 scroll_data.window = NULL;
5934 scroll_data.dx = dx;
5935 scroll_data.dy = dy;
5937 adjust_allocation_recurse (widget, &scroll_data);
5941 gtk_text_view_value_changed (GtkAdjustment *adj,
5942 GtkTextView *text_view)
5949 /* Note that we oddly call this function with adj == NULL
5953 text_view->onscreen_validated = FALSE;
5955 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5956 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5957 adj ? adj->value : 0.0));
5959 if (adj == text_view->hadjustment)
5961 dx = text_view->xoffset - (gint)adj->value;
5962 text_view->xoffset = adj->value;
5964 else if (adj == text_view->vadjustment)
5966 dy = text_view->yoffset - (gint)adj->value;
5967 text_view->yoffset = adj->value;
5969 if (text_view->layout)
5971 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5973 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5975 text_view->first_para_pixels = adj->value - line_top;
5979 if (dx != 0 || dy != 0)
5983 if (GTK_WIDGET_REALIZED (text_view))
5987 if (text_view->left_window)
5988 text_window_scroll (text_view->left_window, 0, dy);
5989 if (text_view->right_window)
5990 text_window_scroll (text_view->right_window, 0, dy);
5995 if (text_view->top_window)
5996 text_window_scroll (text_view->top_window, dx, 0);
5997 if (text_view->bottom_window)
5998 text_window_scroll (text_view->bottom_window, dx, 0);
6001 /* It looks nicer to scroll the main area last, because
6002 * it takes a while, and making the side areas update
6003 * afterward emphasizes the slowness of scrolling the
6006 text_window_scroll (text_view->text_window, dx, dy);
6009 /* Children are now "moved" in the text window, poke
6010 * into widget->allocation for each child
6012 tmp_list = text_view->children;
6013 while (tmp_list != NULL)
6015 GtkTextViewChild *child = tmp_list->data;
6018 adjust_allocation (child->widget, dx, dy);
6020 tmp_list = g_slist_next (tmp_list);
6024 /* This could result in invalidation, which would install the
6025 * first_validate_idle, which would validate onscreen;
6026 * but we're going to go ahead and validate here, so
6027 * first_validate_idle shouldn't have anything to do.
6029 gtk_text_view_update_layout_width (text_view);
6031 /* note that validation of onscreen could invoke this function
6032 * recursively, by scrolling to maintain first_para, or in response
6033 * to updating the layout width, however there is no problem with
6034 * that, or shouldn't be.
6036 gtk_text_view_validate_onscreen (text_view);
6038 /* process exposes */
6039 if (GTK_WIDGET_REALIZED (text_view))
6041 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6043 if (text_view->left_window)
6044 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6046 if (text_view->right_window)
6047 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6049 if (text_view->top_window)
6050 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6052 if (text_view->bottom_window)
6053 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6055 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6058 /* If this got installed, get rid of it, it's just a waste of time. */
6059 if (text_view->first_validate_idle != 0)
6061 g_source_remove (text_view->first_validate_idle);
6062 text_view->first_validate_idle = 0;
6065 gtk_text_view_update_im_spot_location (text_view);
6067 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6071 gtk_text_view_commit_handler (GtkIMContext *context,
6073 GtkTextView *text_view)
6075 gtk_text_view_commit_text (text_view, str);
6079 gtk_text_view_commit_text (GtkTextView *text_view,
6082 gboolean had_selection;
6084 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6086 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6089 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6090 text_view->editable);
6092 if (!strcmp (str, "\n"))
6094 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6095 text_view->editable);
6099 if (!had_selection && text_view->overwrite_mode)
6103 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6105 gtk_text_buffer_get_mark (get_buffer (text_view),
6107 if (!gtk_text_iter_ends_line (&insert))
6108 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6110 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6111 text_view->editable);
6114 gtk_text_buffer_end_user_action (get_buffer (text_view));
6116 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6117 gtk_text_view_scroll_mark_onscreen (text_view,
6118 gtk_text_buffer_get_mark (get_buffer (text_view),
6123 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6124 GtkTextView *text_view)
6127 PangoAttrList *attrs;
6130 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6131 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6133 pango_attr_list_unref (attrs);
6138 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6139 GtkTextView *text_view)
6146 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6147 gtk_text_buffer_get_insert (text_view->buffer));
6150 pos = gtk_text_iter_get_line_index (&start);
6151 gtk_text_iter_set_line_offset (&start, 0);
6152 gtk_text_iter_forward_to_line_end (&end);
6154 text = gtk_text_iter_get_slice (&start, &end);
6155 gtk_im_context_set_surrounding (context, text, -1, pos);
6162 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6165 GtkTextView *text_view)
6170 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6171 gtk_text_buffer_get_insert (text_view->buffer));
6174 gtk_text_iter_forward_chars (&start, offset);
6175 gtk_text_iter_forward_chars (&end, offset + n_chars);
6177 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6183 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6184 const GtkTextIter *location,
6188 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6189 gboolean need_reset = FALSE;
6191 if (mark == gtk_text_buffer_get_insert (buffer))
6193 text_view->virtual_cursor_x = -1;
6194 text_view->virtual_cursor_y = -1;
6195 gtk_text_view_update_im_spot_location (text_view);
6198 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6204 gtk_text_view_reset_im_context (text_view);
6208 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6212 GdkRectangle strong_pos;
6215 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6216 gtk_text_buffer_get_mark (get_buffer (text_view),
6219 if ((x && text_view->virtual_cursor_x == -1) ||
6220 (y && text_view->virtual_cursor_y == -1))
6221 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6225 if (text_view->virtual_cursor_x != -1)
6226 *x = text_view->virtual_cursor_x;
6233 if (text_view->virtual_cursor_x != -1)
6234 *y = text_view->virtual_cursor_y;
6236 *y = strong_pos.y + strong_pos.height / 2;
6241 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6245 GdkRectangle strong_pos;
6248 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6249 gtk_text_buffer_get_mark (get_buffer (text_view),
6252 if (x == -1 || y == -1)
6253 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6255 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6256 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6259 /* Quick hack of a popup menu
6262 activate_cb (GtkWidget *menuitem,
6263 GtkTextView *text_view)
6265 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6266 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6270 append_action_signal (GtkTextView *text_view,
6272 const gchar *stock_id,
6273 const gchar *signal,
6276 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6278 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6279 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6280 GTK_SIGNAL_FUNC (activate_cb), text_view);
6282 gtk_widget_set_sensitive (menuitem, sensitive);
6284 gtk_widget_show (menuitem);
6285 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6289 popup_menu_detach (GtkWidget *attach_widget,
6292 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6296 popup_position_func (GtkMenu *menu,
6302 GtkTextView *text_view;
6304 GdkRectangle cursor_rect;
6305 GdkRectangle onscreen_rect;
6306 gint root_x, root_y;
6311 text_view = GTK_TEXT_VIEW (user_data);
6312 widget = GTK_WIDGET (text_view);
6314 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6316 screen = gtk_widget_get_screen (widget);
6318 gdk_window_get_origin (widget->window, &root_x, &root_y);
6320 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6322 gtk_text_buffer_get_insert (get_buffer (text_view)));
6324 gtk_text_view_get_iter_location (text_view,
6328 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6330 gtk_widget_size_request (text_view->popup_menu, &req);
6332 /* can't use rectangle_intersect since cursor rect can have 0 width */
6333 if (cursor_rect.x >= onscreen_rect.x &&
6334 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6335 cursor_rect.y >= onscreen_rect.y &&
6336 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6338 gtk_text_view_buffer_to_window_coords (text_view,
6339 GTK_TEXT_WINDOW_WIDGET,
6340 cursor_rect.x, cursor_rect.y,
6341 &cursor_rect.x, &cursor_rect.y);
6343 *x = root_x + cursor_rect.x + cursor_rect.width;
6344 *y = root_y + cursor_rect.y + cursor_rect.height;
6348 /* Just center the menu, since cursor is offscreen. */
6349 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6350 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6354 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6355 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6357 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6358 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6363 GtkTextView *text_view;
6369 range_contains_editable_text (const GtkTextIter *start,
6370 const GtkTextIter *end,
6371 gboolean default_editability)
6373 GtkTextIter iter = *start;
6375 while (gtk_text_iter_compare (&iter, end) < 0)
6377 if (gtk_text_iter_editable (&iter, default_editability))
6380 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6387 unichar_chosen_func (const char *text,
6390 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6392 gtk_text_view_commit_text (text_view, text);
6396 popup_targets_received (GtkClipboard *clipboard,
6397 GtkSelectionData *data,
6400 PopupInfo *info = user_data;
6401 GtkTextView *text_view = info->text_view;
6403 if (GTK_WIDGET_REALIZED (text_view))
6405 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6406 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6408 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6409 GtkWidget *menuitem;
6411 gboolean have_selection;
6412 gboolean can_insert;
6414 GtkTextIter sel_start, sel_end;
6416 if (text_view->popup_menu)
6417 gtk_widget_destroy (text_view->popup_menu);
6419 text_view->popup_menu = gtk_menu_new ();
6421 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6422 GTK_WIDGET (text_view),
6425 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6426 &sel_start, &sel_end);
6428 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6430 gtk_text_buffer_get_insert (get_buffer (text_view)));
6432 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6434 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6436 range_contains_editable_text (&sel_start, &sel_end,
6437 text_view->editable));
6438 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6440 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6441 can_insert && clipboard_contains_text);
6443 menuitem = gtk_separator_menu_item_new ();
6444 gtk_widget_show (menuitem);
6445 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6447 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6448 gtk_widget_show (menuitem);
6449 submenu = gtk_menu_new ();
6450 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6451 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6453 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6454 GTK_MENU_SHELL (submenu));
6456 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6457 gtk_widget_show (menuitem);
6458 gtk_widget_set_sensitive (menuitem, can_insert);
6460 submenu = gtk_menu_new ();
6461 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6462 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6464 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6465 unichar_chosen_func,
6468 gtk_signal_emit (GTK_OBJECT (text_view),
6469 signals[POPULATE_POPUP],
6470 text_view->popup_menu);
6473 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6475 info->button, info->time);
6478 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6479 popup_position_func, text_view,
6480 0, gtk_get_current_event_time ());
6481 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
6485 g_object_unref (text_view);
6490 gtk_text_view_do_popup (GtkTextView *text_view,
6491 GdkEventButton *event)
6493 PopupInfo *info = g_new (PopupInfo, 1);
6495 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6496 gtk_text_view_end_selection_drag (text_view, event);
6498 /* In order to know what entries we should make sensitive, we
6499 * ask for the current targets of the clipboard, and when
6500 * we get them, then we actually pop up the menu.
6502 info->text_view = g_object_ref (text_view);
6506 info->button = event->button;
6507 info->time = event->time;
6512 info->time = gtk_get_current_event_time ();
6515 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6516 GDK_SELECTION_CLIPBOARD),
6517 gdk_atom_intern ("TARGETS", FALSE),
6518 popup_targets_received,
6523 gtk_text_view_popup_menu (GtkWidget *widget)
6525 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6529 /* Child GdkWindows */
6532 static GtkTextWindow*
6533 text_window_new (GtkTextWindowType type,
6536 gint height_request)
6540 win = g_new (GtkTextWindow, 1);
6543 win->widget = widget;
6545 win->bin_window = NULL;
6546 win->requisition.width = width_request;
6547 win->requisition.height = height_request;
6548 win->allocation.width = width_request;
6549 win->allocation.height = height_request;
6550 win->allocation.x = 0;
6551 win->allocation.y = 0;
6557 text_window_free (GtkTextWindow *win)
6560 text_window_unrealize (win);
6566 text_window_realize (GtkTextWindow *win,
6569 GdkWindowAttr attributes;
6570 gint attributes_mask;
6573 attributes.window_type = GDK_WINDOW_CHILD;
6574 attributes.x = win->allocation.x;
6575 attributes.y = win->allocation.y;
6576 attributes.width = win->allocation.width;
6577 attributes.height = win->allocation.height;
6578 attributes.wclass = GDK_INPUT_OUTPUT;
6579 attributes.visual = gtk_widget_get_visual (win->widget);
6580 attributes.colormap = gtk_widget_get_colormap (win->widget);
6581 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6583 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6585 win->window = gdk_window_new (parent,
6589 gdk_window_show (win->window);
6590 gdk_window_set_user_data (win->window, win->widget);
6594 attributes.width = win->allocation.width;
6595 attributes.height = win->allocation.height;
6596 attributes.event_mask = (GDK_EXPOSURE_MASK |
6598 GDK_KEY_PRESS_MASK |
6599 GDK_BUTTON_PRESS_MASK |
6600 GDK_BUTTON_RELEASE_MASK |
6601 GDK_POINTER_MOTION_MASK |
6602 GDK_POINTER_MOTION_HINT_MASK |
6603 gtk_widget_get_events (win->widget));
6605 win->bin_window = gdk_window_new (win->window,
6609 gdk_window_show (win->bin_window);
6610 gdk_window_set_user_data (win->bin_window, win->widget);
6612 if (win->type == GTK_TEXT_WINDOW_TEXT)
6615 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
6617 gdk_window_set_cursor (win->bin_window, cursor);
6618 gdk_cursor_unref (cursor);
6620 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6624 gdk_window_set_background (win->bin_window,
6625 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6629 gdk_window_set_background (win->bin_window,
6630 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6633 g_object_set_qdata (G_OBJECT (win->window),
6634 g_quark_from_static_string ("gtk-text-view-text-window"),
6637 g_object_set_qdata (G_OBJECT (win->bin_window),
6638 g_quark_from_static_string ("gtk-text-view-text-window"),
6643 text_window_unrealize (GtkTextWindow *win)
6645 if (win->type == GTK_TEXT_WINDOW_TEXT)
6647 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6651 gdk_window_set_user_data (win->window, NULL);
6652 gdk_window_set_user_data (win->bin_window, NULL);
6653 gdk_window_destroy (win->bin_window);
6654 gdk_window_destroy (win->window);
6656 win->bin_window = NULL;
6660 text_window_size_allocate (GtkTextWindow *win,
6663 win->allocation = *rect;
6667 gdk_window_move_resize (win->window,
6669 rect->width, rect->height);
6671 gdk_window_resize (win->bin_window,
6672 rect->width, rect->height);
6677 text_window_scroll (GtkTextWindow *win,
6681 if (dx != 0 || dy != 0)
6683 gdk_window_scroll (win->bin_window, dx, dy);
6688 text_window_invalidate_rect (GtkTextWindow *win,
6691 GdkRectangle window_rect;
6693 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6700 window_rect.width = rect->width;
6701 window_rect.height = rect->height;
6703 /* Adjust the rect as appropriate */
6707 case GTK_TEXT_WINDOW_TEXT:
6710 case GTK_TEXT_WINDOW_LEFT:
6711 case GTK_TEXT_WINDOW_RIGHT:
6713 window_rect.width = win->allocation.width;
6716 case GTK_TEXT_WINDOW_TOP:
6717 case GTK_TEXT_WINDOW_BOTTOM:
6719 window_rect.height = win->allocation.height;
6723 g_warning ("%s: bug!", G_STRLOC);
6728 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6732 GdkColor color = { 0, 65535, 0, 0 };
6733 GdkGC *gc = gdk_gc_new (win->bin_window);
6734 gdk_gc_set_rgb_fg_color (gc, &color);
6735 gdk_draw_rectangle (win->bin_window,
6736 gc, TRUE, window_rect.x, window_rect.y,
6737 window_rect.width, window_rect.height);
6744 text_window_get_width (GtkTextWindow *win)
6746 return win->allocation.width;
6750 text_window_get_height (GtkTextWindow *win)
6752 return win->allocation.height;
6756 text_window_get_allocation (GtkTextWindow *win,
6759 *rect = win->allocation;
6766 * gtk_text_view_get_window:
6767 * @text_view: a #GtkTextView
6768 * @win: window to get
6770 * Retrieves the #GdkWindow corresponding to an area of the text view;
6771 * possible windows include the overall widget window, child windows
6772 * on the left, right, top, bottom, and the window that displays the
6773 * text buffer. Windows are %NULL and nonexistent if their width or
6774 * height is 0, and are nonexistent before the widget has been
6777 * Return value: a #GdkWindow, or %NULL
6780 gtk_text_view_get_window (GtkTextView *text_view,
6781 GtkTextWindowType win)
6783 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6787 case GTK_TEXT_WINDOW_WIDGET:
6788 return GTK_WIDGET (text_view)->window;
6791 case GTK_TEXT_WINDOW_TEXT:
6792 return text_view->text_window->bin_window;
6795 case GTK_TEXT_WINDOW_LEFT:
6796 if (text_view->left_window)
6797 return text_view->left_window->bin_window;
6802 case GTK_TEXT_WINDOW_RIGHT:
6803 if (text_view->right_window)
6804 return text_view->right_window->bin_window;
6809 case GTK_TEXT_WINDOW_TOP:
6810 if (text_view->top_window)
6811 return text_view->top_window->bin_window;
6816 case GTK_TEXT_WINDOW_BOTTOM:
6817 if (text_view->bottom_window)
6818 return text_view->bottom_window->bin_window;
6823 case GTK_TEXT_WINDOW_PRIVATE:
6824 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6829 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6834 * gtk_text_view_get_window_type:
6835 * @text_view: a #GtkTextView
6836 * @window: a window type
6838 * Usually used to find out which window an event corresponds to.
6839 * If you connect to an event signal on @text_view, this function
6840 * should be called on <literal>event->window</literal> to
6841 * see which window it was.
6843 * Return value: the window type.
6846 gtk_text_view_get_window_type (GtkTextView *text_view,
6851 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6852 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6854 if (window == GTK_WIDGET (text_view)->window)
6855 return GTK_TEXT_WINDOW_WIDGET;
6857 win = g_object_get_qdata (G_OBJECT (window),
6858 g_quark_try_string ("gtk-text-view-text-window"));
6864 return GTK_TEXT_WINDOW_PRIVATE;
6869 buffer_to_widget (GtkTextView *text_view,
6877 *window_x = buffer_x - text_view->xoffset;
6878 *window_x += text_view->text_window->allocation.x;
6883 *window_y = buffer_y - text_view->yoffset;
6884 *window_y += text_view->text_window->allocation.y;
6889 widget_to_text_window (GtkTextWindow *win,
6896 *window_x = widget_x - win->allocation.x;
6899 *window_y = widget_y - win->allocation.y;
6903 buffer_to_text_window (GtkTextView *text_view,
6912 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6913 "for a nonexistent or private child window of GtkTextView");
6917 buffer_to_widget (text_view,
6919 window_x, window_y);
6921 widget_to_text_window (win,
6922 window_x ? *window_x : 0,
6923 window_y ? *window_y : 0,
6929 * gtk_text_view_buffer_to_window_coords:
6930 * @text_view: a #GtkTextView
6931 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6932 * @buffer_x: buffer x coordinate
6933 * @buffer_y: buffer y coordinate
6934 * @window_x: window x coordinate return location
6935 * @window_y: window y coordinate return location
6937 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6938 * @win, and stores the result in (@window_x, @window_y).
6940 * Note that you can't convert coordinates for a nonexisting window (see
6941 * gtk_text_view_set_border_window_size()).
6944 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6945 GtkTextWindowType win,
6951 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6955 case GTK_TEXT_WINDOW_WIDGET:
6956 buffer_to_widget (text_view,
6958 window_x, window_y);
6961 case GTK_TEXT_WINDOW_TEXT:
6963 *window_x = buffer_x - text_view->xoffset;
6965 *window_y = buffer_y - text_view->yoffset;
6968 case GTK_TEXT_WINDOW_LEFT:
6969 buffer_to_text_window (text_view,
6970 text_view->left_window,
6972 window_x, window_y);
6975 case GTK_TEXT_WINDOW_RIGHT:
6976 buffer_to_text_window (text_view,
6977 text_view->right_window,
6979 window_x, window_y);
6982 case GTK_TEXT_WINDOW_TOP:
6983 buffer_to_text_window (text_view,
6984 text_view->top_window,
6986 window_x, window_y);
6989 case GTK_TEXT_WINDOW_BOTTOM:
6990 buffer_to_text_window (text_view,
6991 text_view->bottom_window,
6993 window_x, window_y);
6996 case GTK_TEXT_WINDOW_PRIVATE:
6997 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7001 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7007 widget_to_buffer (GtkTextView *text_view,
7015 *buffer_x = widget_x + text_view->xoffset;
7016 *buffer_x -= text_view->text_window->allocation.x;
7021 *buffer_y = widget_y + text_view->yoffset;
7022 *buffer_y -= text_view->text_window->allocation.y;
7027 text_window_to_widget (GtkTextWindow *win,
7034 *widget_x = window_x + win->allocation.x;
7037 *widget_y = window_y + win->allocation.y;
7041 text_window_to_buffer (GtkTextView *text_view,
7050 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7051 "coordinates for a nonexistent child window.");
7055 text_window_to_widget (win,
7061 widget_to_buffer (text_view,
7062 buffer_x ? *buffer_x : 0,
7063 buffer_y ? *buffer_y : 0,
7069 * gtk_text_view_window_to_buffer_coords:
7070 * @text_view: a #GtkTextView
7071 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7072 * @window_x: window x coordinate
7073 * @window_y: window y coordinate
7074 * @buffer_x: buffer x coordinate return location
7075 * @buffer_y: buffer y coordinate return location
7077 * Converts coordinates on the window identified by @win to buffer
7078 * coordinates, storing the result in (@buffer_x,@buffer_y).
7080 * Note that you can't convert coordinates for a nonexisting window (see
7081 * gtk_text_view_set_border_window_size()).
7084 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7085 GtkTextWindowType win,
7091 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7095 case GTK_TEXT_WINDOW_WIDGET:
7096 widget_to_buffer (text_view,
7098 buffer_x, buffer_y);
7101 case GTK_TEXT_WINDOW_TEXT:
7103 *buffer_x = window_x + text_view->xoffset;
7105 *buffer_y = window_y + text_view->yoffset;
7108 case GTK_TEXT_WINDOW_LEFT:
7109 text_window_to_buffer (text_view,
7110 text_view->left_window,
7112 buffer_x, buffer_y);
7115 case GTK_TEXT_WINDOW_RIGHT:
7116 text_window_to_buffer (text_view,
7117 text_view->right_window,
7119 buffer_x, buffer_y);
7122 case GTK_TEXT_WINDOW_TOP:
7123 text_window_to_buffer (text_view,
7124 text_view->top_window,
7126 buffer_x, buffer_y);
7129 case GTK_TEXT_WINDOW_BOTTOM:
7130 text_window_to_buffer (text_view,
7131 text_view->bottom_window,
7133 buffer_x, buffer_y);
7136 case GTK_TEXT_WINDOW_PRIVATE:
7137 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7141 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7147 set_window_width (GtkTextView *text_view,
7149 GtkTextWindowType type,
7150 GtkTextWindow **winp)
7156 text_window_free (*winp);
7158 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7165 *winp = text_window_new (type,
7166 GTK_WIDGET (text_view),
7168 /* if the widget is already realized we need to realize the child manually */
7169 if (GTK_WIDGET_REALIZED (text_view))
7170 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7174 if ((*winp)->requisition.width == width)
7177 (*winp)->requisition.width = width;
7180 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7186 set_window_height (GtkTextView *text_view,
7188 GtkTextWindowType type,
7189 GtkTextWindow **winp)
7195 text_window_free (*winp);
7197 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7204 *winp = text_window_new (type,
7205 GTK_WIDGET (text_view),
7208 /* if the widget is already realized we need to realize the child manually */
7209 if (GTK_WIDGET_REALIZED (text_view))
7210 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7214 if ((*winp)->requisition.height == height)
7217 (*winp)->requisition.height = height;
7220 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7225 * gtk_text_view_set_border_window_size:
7226 * @text_view: a #GtkTextView
7227 * @type: window to affect
7228 * @size: width or height of the window
7230 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7231 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7232 * Automatically destroys the corresponding window if the size is set
7233 * to 0, and creates the window if the size is set to non-zero. This
7234 * function can only be used for the "border windows," it doesn't work
7235 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7236 * #GTK_TEXT_WINDOW_PRIVATE.
7239 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7240 GtkTextWindowType type,
7244 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7245 g_return_if_fail (size >= 0);
7249 case GTK_TEXT_WINDOW_LEFT:
7250 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7251 &text_view->left_window);
7254 case GTK_TEXT_WINDOW_RIGHT:
7255 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7256 &text_view->right_window);
7259 case GTK_TEXT_WINDOW_TOP:
7260 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7261 &text_view->top_window);
7264 case GTK_TEXT_WINDOW_BOTTOM:
7265 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7266 &text_view->bottom_window);
7270 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7276 * gtk_text_view_get_border_window_size:
7277 * @text_view: a #GtkTextView
7278 * @type: window to return size from
7280 * Gets the width of the specified border window. See
7281 * gtk_text_view_set_border_window_size().
7283 * Return value: width of window
7286 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7287 GtkTextWindowType type)
7289 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7293 case GTK_TEXT_WINDOW_LEFT:
7294 if (text_view->left_window)
7295 return text_view->left_window->requisition.width;
7297 case GTK_TEXT_WINDOW_RIGHT:
7298 if (text_view->right_window)
7299 return text_view->right_window->requisition.width;
7301 case GTK_TEXT_WINDOW_TOP:
7302 if (text_view->top_window)
7303 return text_view->top_window->requisition.height;
7305 case GTK_TEXT_WINDOW_BOTTOM:
7306 if (text_view->bottom_window)
7307 return text_view->bottom_window->requisition.height;
7310 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7321 static GtkTextViewChild*
7322 text_view_child_new_anchored (GtkWidget *child,
7323 GtkTextChildAnchor *anchor,
7324 GtkTextLayout *layout)
7326 GtkTextViewChild *vc;
7328 vc = g_new (GtkTextViewChild, 1);
7330 vc->type = GTK_TEXT_WINDOW_PRIVATE;
7332 vc->anchor = anchor;
7334 vc->from_top_of_line = 0;
7335 vc->from_left_of_buffer = 0;
7337 g_object_ref (G_OBJECT (vc->widget));
7338 g_object_ref (G_OBJECT (vc->anchor));
7340 g_object_set_data (G_OBJECT (child),
7341 "gtk-text-view-child",
7344 gtk_text_child_anchor_register_child (anchor, child, layout);
7349 static GtkTextViewChild*
7350 text_view_child_new_window (GtkWidget *child,
7351 GtkTextWindowType type,
7355 GtkTextViewChild *vc;
7357 vc = g_new (GtkTextViewChild, 1);
7362 vc->from_top_of_line = 0;
7363 vc->from_left_of_buffer = 0;
7365 g_object_ref (G_OBJECT (vc->widget));
7371 g_object_set_data (G_OBJECT (child),
7372 "gtk-text-view-child",
7379 text_view_child_free (GtkTextViewChild *child)
7381 g_object_set_data (G_OBJECT (child->widget),
7382 "gtk-text-view-child", NULL);
7386 gtk_text_child_anchor_unregister_child (child->anchor,
7388 g_object_unref (G_OBJECT (child->anchor));
7391 g_object_unref (G_OBJECT (child->widget));
7397 text_view_child_set_parent_window (GtkTextView *text_view,
7398 GtkTextViewChild *vc)
7401 gtk_widget_set_parent_window (vc->widget,
7402 text_view->text_window->bin_window);
7406 window = gtk_text_view_get_window (text_view,
7408 gtk_widget_set_parent_window (vc->widget, window);
7413 add_child (GtkTextView *text_view,
7414 GtkTextViewChild *vc)
7416 text_view->children = g_slist_prepend (text_view->children,
7419 if (GTK_WIDGET_REALIZED (text_view))
7420 text_view_child_set_parent_window (text_view, vc);
7422 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7426 * gtk_text_view_add_child_at_anchor:
7427 * @text_view: a #GtkTextView
7428 * @child: a #GtkWidget
7429 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7431 * Adds a child widget in the text buffer, at the given @anchor.
7435 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7437 GtkTextChildAnchor *anchor)
7439 GtkTextViewChild *vc;
7441 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7442 g_return_if_fail (GTK_IS_WIDGET (child));
7443 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7444 g_return_if_fail (child->parent == NULL);
7446 gtk_text_view_ensure_layout (text_view);
7448 vc = text_view_child_new_anchored (child, anchor,
7451 add_child (text_view, vc);
7453 g_assert (vc->widget == child);
7454 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7458 * gtk_text_view_add_child_in_window:
7459 * @text_view: a #GtkTextView
7460 * @child: a #GtkWidget
7461 * @which_window: which window the child should appear in
7462 * @xpos: X position of child in window coordinates
7463 * @ypos: Y position of child in window coordinates
7465 * Adds a child at fixed coordinates in one of the text widget's
7466 * windows. The window must have nonzero size (see
7467 * gtk_text_view_set_border_window_size()). Note that the child
7468 * coordinates are given relative to the #GdkWindow in question, and
7469 * that these coordinates have no sane relationship to scrolling. When
7470 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7471 * irrelevant, the child floats above all scrollable areas. But when
7472 * placing a child in one of the scrollable windows (border windows or
7473 * text window), you'll need to compute the child's correct position
7474 * in buffer coordinates any time scrolling occurs or buffer changes
7475 * occur, and then call gtk_text_view_move_child() to update the
7476 * child's position. Unfortunately there's no good way to detect that
7477 * scrolling has occurred, using the current API; a possible hack
7478 * would be to update all child positions when the scroll adjustments
7479 * change or the text buffer changes. See bug 64518 on
7480 * bugzilla.gnome.org for status of fixing this issue.
7484 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7486 GtkTextWindowType which_window,
7490 GtkTextViewChild *vc;
7492 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7493 g_return_if_fail (GTK_IS_WIDGET (child));
7494 g_return_if_fail (child->parent == NULL);
7496 vc = text_view_child_new_window (child, which_window,
7499 add_child (text_view, vc);
7501 g_assert (vc->widget == child);
7502 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7506 * gtk_text_view_move_child:
7507 * @text_view: a #GtkTextView
7508 * @child: child widget already added to the text view
7509 * @xpos: new X position in window coordinates
7510 * @ypos: new Y position in window coordinates
7512 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7516 gtk_text_view_move_child (GtkTextView *text_view,
7521 GtkTextViewChild *vc;
7523 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7524 g_return_if_fail (GTK_IS_WIDGET (child));
7525 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7527 vc = g_object_get_data (G_OBJECT (child),
7528 "gtk-text-view-child");
7530 g_assert (vc != NULL);
7532 if (vc->x == xpos &&
7539 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7540 gtk_widget_queue_resize (child);
7544 /* Iterator operations */
7547 * gtk_text_view_forward_display_line:
7548 * @text_view: a #GtkTextView
7549 * @iter: a #GtkTextIter
7551 * Moves the given @iter forward by one display (wrapped) line. A
7552 * display line is different from a paragraph. Paragraphs are
7553 * separated by newlines or other paragraph separator characters.
7554 * Display lines are created by line-wrapping a paragraph. If
7555 * wrapping is turned off, display lines and paragraphs will be the
7556 * same. Display lines are divided differently for each view, since
7557 * they depend on the view's width; paragraphs are the same in all
7558 * views, since they depend on the contents of the #GtkTextBuffer.
7560 * Return value: %TRUE if @iter was moved and is not on the end iterator
7563 gtk_text_view_forward_display_line (GtkTextView *text_view,
7566 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7567 g_return_val_if_fail (iter != NULL, FALSE);
7569 gtk_text_view_ensure_layout (text_view);
7571 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7575 * gtk_text_view_backward_display_line:
7576 * @text_view: a #GtkTextView
7577 * @iter: a #GtkTextIter
7579 * Moves the given @iter backward by one display (wrapped) line. A
7580 * display line is different from a paragraph. Paragraphs are
7581 * separated by newlines or other paragraph separator characters.
7582 * Display lines are created by line-wrapping a paragraph. If
7583 * wrapping is turned off, display lines and paragraphs will be the
7584 * same. Display lines are divided differently for each view, since
7585 * they depend on the view's width; paragraphs are the same in all
7586 * views, since they depend on the contents of the #GtkTextBuffer.
7588 * Return value: %TRUE if @iter was moved and is not on the end iterator
7591 gtk_text_view_backward_display_line (GtkTextView *text_view,
7594 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7595 g_return_val_if_fail (iter != NULL, FALSE);
7597 gtk_text_view_ensure_layout (text_view);
7599 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7603 * gtk_text_view_forward_display_line_end:
7604 * @text_view: a #GtkTextView
7605 * @iter: a #GtkTextIter
7607 * Moves the given @iter forward to the next display line end. 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_end (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_line_end (text_view->layout, iter, 1);
7631 * gtk_text_view_backward_display_line_start:
7632 * @text_view: a #GtkTextView
7633 * @iter: a #GtkTextIter
7635 * Moves the given @iter backward to the next display line start. 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_start (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_line_end (text_view->layout, iter, -1);
7659 * gtk_text_view_starts_display_line:
7660 * @text_view: a #GtkTextView
7661 * @iter: a #GtkTextIter
7663 * Determines whether @iter is at the start of a display line.
7664 * See gtk_text_view_forward_display_line() for an explanation of
7665 * display lines vs. paragraphs.
7667 * Return value: %TRUE if @iter begins a wrapped line
7670 gtk_text_view_starts_display_line (GtkTextView *text_view,
7671 const GtkTextIter *iter)
7673 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7674 g_return_val_if_fail (iter != NULL, FALSE);
7676 gtk_text_view_ensure_layout (text_view);
7678 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7682 * gtk_text_view_move_visually:
7683 * @text_view: a #GtkTextView
7684 * @iter: a #GtkTextIter
7685 * @count: number of lines to move
7687 * Moves @iter up or down by @count display (wrapped) lines.
7688 * See gtk_text_view_forward_display_line() for an explanation of
7689 * display lines vs. paragraphs.
7691 * Return value: %TRUE if @iter moved and is not on the end iterator
7694 gtk_text_view_move_visually (GtkTextView *text_view,
7698 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7699 g_return_val_if_fail (iter != NULL, FALSE);
7701 gtk_text_view_ensure_layout (text_view);
7703 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);