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 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
901 "delete_from_cursor", 2,
902 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
905 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
906 "delete_from_cursor", 2,
907 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
910 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
911 "delete_from_cursor", 2,
912 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
917 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
920 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
921 "copy_clipboard", 0);
923 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
924 "paste_clipboard", 0);
927 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
928 "toggle_overwrite", 0);
929 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
930 "toggle_overwrite", 0);
932 /* Control-tab focus motion */
933 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
935 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
936 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
938 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
940 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
942 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
943 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
945 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
949 gtk_text_view_init (GtkTextView *text_view)
953 widget = GTK_WIDGET (text_view);
955 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
957 /* Set up default style */
958 text_view->wrap_mode = GTK_WRAP_NONE;
959 text_view->pixels_above_lines = 0;
960 text_view->pixels_below_lines = 0;
961 text_view->pixels_inside_wrap = 0;
962 text_view->justify = GTK_JUSTIFY_LEFT;
963 text_view->left_margin = 0;
964 text_view->right_margin = 0;
965 text_view->indent = 0;
966 text_view->tabs = NULL;
967 text_view->editable = TRUE;
969 gtk_drag_dest_set (widget,
971 target_table, G_N_ELEMENTS (target_table),
972 GDK_ACTION_COPY | GDK_ACTION_MOVE);
974 text_view->virtual_cursor_x = -1;
975 text_view->virtual_cursor_y = -1;
977 /* This object is completely private. No external entity can gain a reference
978 * to it; so we create it here and destroy it in finalize ().
980 text_view->im_context = gtk_im_multicontext_new ();
982 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
983 G_CALLBACK (gtk_text_view_commit_handler), text_view);
984 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
985 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
986 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
987 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
988 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
989 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
991 text_view->cursor_visible = TRUE;
993 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
996 text_view->drag_start_x = -1;
997 text_view->drag_start_y = -1;
999 text_view->pending_place_cursor_button = 0;
1001 /* We handle all our own redrawing */
1002 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1006 * gtk_text_view_new:
1008 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1009 * before using the text view, an empty default buffer will be created
1010 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1011 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1013 * Return value: a new #GtkTextView
1016 gtk_text_view_new (void)
1018 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1022 * gtk_text_view_new_with_buffer:
1023 * @buffer: a #GtkTextBuffer
1025 * Creates a new #GtkTextView widget displaying the buffer
1026 * @buffer. One buffer can be shared among many widgets.
1027 * @buffer may be NULL to create a default buffer, in which case
1028 * this function is equivalent to gtk_text_view_new(). The
1029 * text view adds its own reference count to the buffer; it does not
1030 * take over an existing reference.
1032 * Return value: a new #GtkTextView.
1035 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1037 GtkTextView *text_view;
1039 text_view = (GtkTextView*)gtk_text_view_new ();
1041 gtk_text_view_set_buffer (text_view, buffer);
1043 return GTK_WIDGET (text_view);
1047 * gtk_text_view_set_buffer:
1048 * @text_view: a #GtkTextView
1049 * @buffer: a #GtkTextBuffer
1051 * Sets @buffer as the buffer being displayed by @text_view. The previous
1052 * buffer displayed by the text view is unreferenced, and a reference is
1053 * added to @buffer. If you owned a reference to @buffer before passing it
1054 * to this function, you must remove that reference yourself; #GtkTextView
1055 * will not "adopt" it.
1059 gtk_text_view_set_buffer (GtkTextView *text_view,
1060 GtkTextBuffer *buffer)
1062 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1063 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1065 if (text_view->buffer == buffer)
1068 if (text_view->buffer != NULL)
1070 /* Destroy all anchored children */
1074 copy = g_slist_copy (text_view->children);
1076 while (tmp_list != NULL)
1078 GtkTextViewChild *vc = tmp_list->data;
1082 gtk_widget_destroy (vc->widget);
1083 /* vc may now be invalid! */
1086 tmp_list = g_slist_next (tmp_list);
1089 g_slist_free (copy);
1091 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1092 (gpointer) gtk_text_view_mark_set_handler, text_view);
1093 g_object_unref (G_OBJECT (text_view->buffer));
1094 text_view->dnd_mark = NULL;
1096 if (GTK_WIDGET_REALIZED (text_view))
1098 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1099 GDK_SELECTION_PRIMARY);
1100 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1104 text_view->buffer = buffer;
1110 g_object_ref (G_OBJECT (buffer));
1112 if (text_view->layout)
1113 gtk_text_layout_set_buffer (text_view->layout, buffer);
1115 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1117 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1121 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1125 text_view->first_para_pixels = 0;
1127 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1128 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1130 if (GTK_WIDGET_REALIZED (text_view))
1132 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1133 GDK_SELECTION_PRIMARY);
1134 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1138 if (GTK_WIDGET_VISIBLE (text_view))
1139 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1141 DV(g_print ("Invalidating due to set_buffer\n"));
1142 gtk_text_view_invalidate (text_view);
1145 static GtkTextBuffer*
1146 get_buffer (GtkTextView *text_view)
1148 if (text_view->buffer == NULL)
1151 b = gtk_text_buffer_new (NULL);
1152 gtk_text_view_set_buffer (text_view, b);
1153 g_object_unref (G_OBJECT (b));
1156 return text_view->buffer;
1160 * gtk_text_view_get_buffer:
1161 * @text_view: a #GtkTextView
1163 * Returns the #GtkTextBuffer being displayed by this text view.
1164 * The reference count on the buffer is not incremented; the caller
1165 * of this function won't own a new reference.
1167 * Return value: a #GtkTextBuffer
1170 gtk_text_view_get_buffer (GtkTextView *text_view)
1172 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1174 return get_buffer (text_view);
1178 * gtk_text_view_get_iter_at_location:
1179 * @text_view: a #GtkTextView
1180 * @iter: a #GtkTextIter
1181 * @x: x position, in buffer coordinates
1182 * @y: y position, in buffer coordinates
1184 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1185 * coordinates are coordinates for the entire buffer, not just the
1186 * currently-displayed portion. If you have coordinates from an
1187 * event, you have to convert those to buffer coordinates with
1188 * gtk_text_view_window_to_buffer_coords().
1192 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1197 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1198 g_return_if_fail (iter != NULL);
1200 gtk_text_view_ensure_layout (text_view);
1202 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1209 * gtk_text_view_get_iter_location:
1210 * @text_view: a #GtkTextView
1211 * @iter: a #GtkTextIter
1212 * @location: bounds of the character at @iter
1214 * Gets a rectangle which roughly contains the character at @iter.
1215 * The rectangle position is in buffer coordinates; use
1216 * gtk_text_view_buffer_to_window_coords() to convert these
1217 * coordinates to coordinates for one of the windows in the text view.
1221 gtk_text_view_get_iter_location (GtkTextView *text_view,
1222 const GtkTextIter *iter,
1223 GdkRectangle *location)
1225 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1226 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1228 gtk_text_view_ensure_layout (text_view);
1230 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1234 * gtk_text_view_get_line_yrange:
1235 * @text_view: a #GtkTextView
1236 * @iter: a #GtkTextIter
1237 * @y: return location for a y coordinate
1238 * @height: return location for a height
1240 * Gets the y coordinate of the top of the line containing @iter,
1241 * and the height of the line. The coordinate is a buffer coordinate;
1242 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1246 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1247 const GtkTextIter *iter,
1251 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1252 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1254 gtk_text_view_ensure_layout (text_view);
1256 gtk_text_layout_get_line_yrange (text_view->layout,
1263 * gtk_text_view_get_line_at_y:
1264 * @text_view: a #GtkTextView
1265 * @target_iter: a #GtkTextIter
1266 * @y: a y coordinate
1267 * @line_top: return location for top coordinate of the line
1269 * Gets the #GtkTextIter at the start of the line containing
1270 * the coordinate @y. @y is in buffer coordinates, convert from
1271 * window coordinates with gtk_text_view_window_to_buffer_coords().
1272 * If non-%NULL, @line_top will be filled with the coordinate of the top
1276 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1277 GtkTextIter *target_iter,
1281 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1283 gtk_text_view_ensure_layout (text_view);
1285 gtk_text_layout_get_line_at_y (text_view->layout,
1292 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1294 DV (g_print (" Setting adj to raw value %g\n", val));
1296 /* We don't really want to clamp to upper; we want to clamp to
1297 upper - page_size which is the highest value the scrollbar
1298 will let us reach. */
1299 if (val > (adj->upper - adj->page_size))
1300 val = adj->upper - adj->page_size;
1302 if (val < adj->lower)
1305 if (val != adj->value)
1307 DV (g_print (" Setting adj to clamped value %g\n", val));
1308 gtk_adjustment_set_value (adj, val);
1316 * gtk_text_view_scroll_to_iter:
1317 * @text_view: a #GtkTextView
1318 * @iter: a #GtkTextIter
1319 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1320 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1321 * @xalign: horizontal alignment of mark within visible area.
1322 * @yalign: vertical alignment of mark within visible area
1324 * Scrolls @text_view so that @iter is on the screen in the position
1325 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1326 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1327 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1328 * possibly not scrolling at all. The effective screen for purposes
1329 * of this function is reduced by a margin of size @within_margin.
1330 * NOTE: This function uses the currently-computed height of the
1331 * lines in the text buffer. Note that line heights are computed
1332 * in an idle handler; so this function may not have the desired effect
1333 * if it's called before the height computations. To avoid oddness,
1334 * consider using gtk_text_view_scroll_to_mark() which saves a point
1335 * to be scrolled to after line validation.
1337 * Return value: %TRUE if scrolling occurred
1340 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1342 gdouble within_margin,
1348 GdkRectangle screen;
1353 gboolean retval = FALSE;
1355 gint screen_xoffset, screen_yoffset;
1356 gint current_x_scroll, current_y_scroll;
1358 /* FIXME why don't we do the validate-at-scroll-destination thing
1359 * from flush_scroll in this function? I think it wasn't done before
1360 * because changed_handler was screwed up, but I could be wrong.
1363 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1364 g_return_val_if_fail (iter != NULL, FALSE);
1365 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1366 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1367 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1369 widget = GTK_WIDGET (text_view);
1371 DV(g_print(G_STRLOC"\n"));
1373 gtk_text_layout_get_iter_location (text_view->layout,
1377 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1379 current_x_scroll = text_view->xoffset;
1380 current_y_scroll = text_view->yoffset;
1382 screen.x = current_x_scroll;
1383 screen.y = current_y_scroll;
1384 screen.width = SCREEN_WIDTH (widget);
1385 screen.height = SCREEN_HEIGHT (widget);
1387 screen_xoffset = screen.width * within_margin;
1388 screen_yoffset = screen.height * within_margin;
1390 screen.x += screen_xoffset;
1391 screen.y += screen_yoffset;
1392 screen.width -= screen_xoffset * 2;
1393 screen.height -= screen_yoffset * 2;
1395 /* paranoia check */
1396 if (screen.width < 1)
1398 if (screen.height < 1)
1401 screen_right = screen.x + screen.width;
1402 screen_bottom = screen.y + screen.height;
1404 /* The alignment affects the point in the target character that we
1405 * choose to align. If we're doing right/bottom alignment, we align
1406 * the right/bottom edge of the character the mark is at; if we're
1407 * doing left/top we align the left/top edge of the character; if
1408 * we're doing center alignment we align the center of the
1412 /* Vertical scroll */
1415 scroll_dest = current_y_scroll;
1419 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1421 /* if scroll_dest < screen.y, we move a negative increment (up),
1422 * else a positive increment (down)
1424 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1428 /* move minimum to get onscreen */
1429 if (rect.y < screen.y)
1431 scroll_dest = rect.y;
1432 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1434 else if ((rect.y + rect.height) > screen_bottom)
1436 scroll_dest = rect.y + rect.height;
1437 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1441 if (scroll_inc != 0)
1443 retval = set_adjustment_clamped (get_vadjustment (text_view),
1444 current_y_scroll + scroll_inc);
1446 DV (g_print (" vert increment %d\n", scroll_inc));
1449 /* Horizontal scroll */
1452 scroll_dest = current_x_scroll;
1456 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1458 /* if scroll_dest < screen.y, we move a negative increment (left),
1459 * else a positive increment (right)
1461 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1465 /* move minimum to get onscreen */
1466 if (rect.x < screen.x)
1468 scroll_dest = rect.x;
1469 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1471 else if ((rect.x + rect.width) > screen_right)
1473 scroll_dest = rect.x + rect.width;
1474 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1478 if (scroll_inc != 0)
1480 retval = set_adjustment_clamped (get_hadjustment (text_view),
1481 current_x_scroll + scroll_inc);
1483 DV (g_print (" horiz increment %d\n", scroll_inc));
1487 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1489 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1495 free_pending_scroll (GtkTextPendingScroll *scroll)
1497 if (!gtk_text_mark_get_deleted (scroll->mark))
1498 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1500 g_object_unref (G_OBJECT (scroll->mark));
1505 gtk_text_view_queue_scroll (GtkTextView *text_view,
1507 gdouble within_margin,
1513 GtkTextPendingScroll *scroll;
1515 DV(g_print(G_STRLOC"\n"));
1517 scroll = g_new (GtkTextPendingScroll, 1);
1519 scroll->within_margin = within_margin;
1520 scroll->use_align = use_align;
1521 scroll->xalign = xalign;
1522 scroll->yalign = yalign;
1524 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1526 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1529 gtk_text_mark_get_left_gravity (mark));
1531 g_object_ref (G_OBJECT (scroll->mark));
1533 if (text_view->pending_scroll)
1534 free_pending_scroll (text_view->pending_scroll);
1536 text_view->pending_scroll = scroll;
1540 gtk_text_view_flush_scroll (GtkTextView *text_view)
1543 GtkTextPendingScroll *scroll;
1547 widget = GTK_WIDGET (text_view);
1549 DV(g_print(G_STRLOC"\n"));
1551 if (text_view->pending_scroll == NULL)
1553 DV (g_print ("in flush scroll, no pending scroll\n"));
1557 scroll = text_view->pending_scroll;
1559 /* avoid recursion */
1560 text_view->pending_scroll = NULL;
1562 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1564 /* Validate area around the scroll destination, so the adjustment
1565 * can meaningfully point into that area. We must validate
1566 * enough area to be sure that after we scroll, everything onscreen
1567 * is valid; otherwise, validation will maintain the first para
1568 * in one place, but may push the target iter off the bottom of
1571 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1572 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1573 - (widget->allocation.height * 2),
1574 widget->allocation.height * 2);
1576 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1578 /* Ensure we have updated width/height */
1579 gtk_text_view_update_adjustments (text_view);
1581 retval = gtk_text_view_scroll_to_iter (text_view,
1583 scroll->within_margin,
1588 free_pending_scroll (scroll);
1594 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1596 if (upper != adj->upper)
1598 gdouble min = MAX (0.0, upper - adj->page_size);
1599 gboolean value_changed = FALSE;
1603 if (adj->value > min)
1606 value_changed = TRUE;
1609 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1610 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1614 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1615 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1621 gtk_text_view_update_adjustments (GtkTextView *text_view)
1623 gint width = 0, height = 0;
1625 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1627 if (text_view->layout)
1628 gtk_text_layout_get_size (text_view->layout, &width, &height);
1630 if (text_view->width != width || text_view->height != height)
1632 text_view->width = width;
1633 text_view->height = height;
1635 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1636 MAX (SCREEN_WIDTH (text_view), width));
1637 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1638 MAX (SCREEN_HEIGHT (text_view), height));
1640 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1642 /* Set up the step sizes; we'll say that a page is
1643 our allocation minus one step, and a step is
1644 1/10 of our allocation. */
1645 text_view->hadjustment->step_increment =
1646 SCREEN_WIDTH (text_view) / 10.0;
1647 text_view->hadjustment->page_increment =
1648 SCREEN_WIDTH (text_view) * 0.9;
1650 text_view->vadjustment->step_increment =
1651 SCREEN_HEIGHT (text_view) / 10.0;
1652 text_view->vadjustment->page_increment =
1653 SCREEN_HEIGHT (text_view) * 0.9;
1655 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1656 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1661 gtk_text_view_update_layout_width (GtkTextView *text_view)
1663 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1665 gtk_text_view_ensure_layout (text_view);
1667 gtk_text_layout_set_screen_width (text_view->layout,
1668 SCREEN_WIDTH (text_view));
1672 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1675 gint cursor_x_pos, cursor_y_pos;
1677 if (text_view->layout == NULL)
1680 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1682 area.x = cursor_x_pos;
1683 area.y = cursor_y_pos;
1684 area.width = area.height = 0;
1686 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1690 * gtk_text_view_scroll_to_mark:
1691 * @text_view: a #GtkTextView
1692 * @mark: a #GtkTextMark
1693 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1694 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1695 * @xalign: horizontal alignment of mark within visible area.
1696 * @yalign: vertical alignment of mark within visible area
1698 * Scrolls @text_view so that @mark is on the screen in the position
1699 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1700 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1701 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1702 * possibly not scrolling at all. The effective screen for purposes
1703 * of this function is reduced by a margin of size @within_margin.
1707 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1709 gdouble within_margin,
1714 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1715 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1716 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1717 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1718 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1720 gtk_text_view_queue_scroll (text_view, mark,
1726 /* If no validation is pending, we need to go ahead and force an
1729 if (text_view->layout &&
1730 gtk_text_layout_is_valid (text_view->layout))
1731 gtk_text_view_flush_scroll (text_view);
1735 * gtk_text_view_scroll_mark_onscreen:
1736 * @text_view: a #GtkTextView
1737 * @mark: a mark in the buffer for @text_view
1739 * Scrolls @text_view the minimum distance such that @mark is contained
1740 * within the visible area of the widget.
1744 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1747 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1748 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1750 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1754 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1756 GdkRectangle visible_rect;
1757 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1759 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1761 visible_rect.y + visible_rect.height);
1765 * gtk_text_view_move_mark_onscreen:
1766 * @text_view: a #GtkTextView
1767 * @mark: a #GtkTextMark
1769 * Moves a mark within the buffer so that it's
1770 * located within the currently-visible text area.
1772 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1775 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1780 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1781 g_return_val_if_fail (mark != NULL, FALSE);
1783 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1785 if (clamp_iter_onscreen (text_view, &iter))
1787 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1795 * gtk_text_view_get_visible_rect:
1796 * @text_view: a #GtkTextView
1797 * @visible_rect: rectangle to fill
1799 * Fills @visible_rect with the currently-visible
1800 * region of the buffer, in buffer coordinates. Convert to window coordinates
1801 * with gtk_text_view_buffer_to_window_coords().
1804 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1805 GdkRectangle *visible_rect)
1809 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1811 widget = GTK_WIDGET (text_view);
1815 visible_rect->x = text_view->xoffset;
1816 visible_rect->y = text_view->yoffset;
1817 visible_rect->width = SCREEN_WIDTH (widget);
1818 visible_rect->height = SCREEN_HEIGHT (widget);
1820 DV(g_print(" visible rect: %d,%d %d x %d\n",
1823 visible_rect->width,
1824 visible_rect->height));
1829 * gtk_text_view_set_wrap_mode:
1830 * @text_view: a #GtkTextView
1831 * @wrap_mode: a #GtkWrapMode
1833 * Sets the line wrapping for the view.
1836 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1837 GtkWrapMode wrap_mode)
1839 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1841 if (text_view->wrap_mode != wrap_mode)
1843 text_view->wrap_mode = wrap_mode;
1845 if (text_view->layout)
1847 text_view->layout->default_style->wrap_mode = wrap_mode;
1848 gtk_text_layout_default_style_changed (text_view->layout);
1852 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1856 * gtk_text_view_get_wrap_mode:
1857 * @text_view: a #GtkTextView
1859 * Gets the line wrapping for the view.
1861 * Return value: the line wrap setting
1864 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1866 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1868 return text_view->wrap_mode;
1872 * gtk_text_view_set_editable:
1873 * @text_view: a #GtkTextView
1874 * @setting: whether it's editable
1876 * Sets the default editability of the #GtkTextView. You can override
1877 * this default setting with tags in the buffer, using the "editable"
1878 * attribute of tags.
1881 gtk_text_view_set_editable (GtkTextView *text_view,
1884 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1885 setting = setting != FALSE;
1887 if (text_view->editable != setting)
1889 text_view->editable = setting;
1891 if (text_view->layout)
1893 text_view->layout->default_style->editable = text_view->editable;
1894 gtk_text_layout_default_style_changed (text_view->layout);
1897 g_object_notify (G_OBJECT (text_view), "editable");
1902 * gtk_text_view_get_editable:
1903 * @text_view: a #GtkTextView
1905 * Returns the default editability of the #GtkTextView. Tags in the
1906 * buffer may override this setting for some ranges of text.
1908 * Return value: whether text is editable by default
1911 gtk_text_view_get_editable (GtkTextView *text_view)
1913 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1915 return text_view->editable;
1919 * gtk_text_view_set_pixels_above_lines:
1920 * @text_view: a #GtkTextView
1921 * @pixels_above_lines: pixels above paragraphs
1923 * Sets the default number of blank pixels above paragraphs in @text_view.
1924 * Tags in the buffer for @text_view may override the defaults.
1928 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1929 gint pixels_above_lines)
1931 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1933 if (text_view->pixels_above_lines != pixels_above_lines)
1935 text_view->pixels_above_lines = pixels_above_lines;
1937 if (text_view->layout)
1939 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1940 gtk_text_layout_default_style_changed (text_view->layout);
1943 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1948 * gtk_text_view_get_pixels_above_lines:
1949 * @text_view: a #GtkTextView
1951 * Gets the default number of pixels to put above paragraphs.
1953 * Return value: default number of pixels above paragraphs
1956 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1958 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1960 return text_view->pixels_above_lines;
1964 * gtk_text_view_set_pixels_below_lines:
1965 * @text_view: a #GtkTextView
1966 * @pixels_below_lines: pixels below paragraphs
1968 * Sets the default number of pixels of blank space
1969 * to put below paragraphs in @text_view. May be overridden
1970 * by tags applied to @text_view's buffer.
1974 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1975 gint pixels_below_lines)
1977 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1979 if (text_view->pixels_below_lines != pixels_below_lines)
1981 text_view->pixels_below_lines = pixels_below_lines;
1983 if (text_view->layout)
1985 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
1986 gtk_text_layout_default_style_changed (text_view->layout);
1989 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
1994 * gtk_text_view_get_pixels_below_lines:
1995 * @text_view: a #GtkTextView
1997 * Gets the value set by gtk_text_view_set_pixels_below_lines().
1999 * Return value: default number of blank pixels below paragraphs
2002 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2004 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2006 return text_view->pixels_below_lines;
2010 * gtk_text_view_set_pixels_inside_wrap:
2011 * @text_view: a #GtkTextView
2012 * @pixels_inside_wrap: default number of pixels between wrapped lines
2014 * Sets the default number of pixels of blank space to leave between
2015 * display/wrapped lines within a paragraph. May be overridden by
2016 * tags in @text_view's buffer.
2020 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2021 gint pixels_inside_wrap)
2023 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2025 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2027 text_view->pixels_inside_wrap = pixels_inside_wrap;
2029 if (text_view->layout)
2031 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2032 gtk_text_layout_default_style_changed (text_view->layout);
2035 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2040 * gtk_text_view_get_pixels_inside_wrap:
2041 * @text_view: a #GtkTextView
2043 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2045 * Return value: default number of pixels of blank space between wrapped lines
2048 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2050 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2052 return text_view->pixels_inside_wrap;
2056 * gtk_text_view_set_justification:
2057 * @text_view: a #GtkTextView
2058 * @justification: justification
2060 * Sets the default justification of text in @text_view.
2061 * Tags in the view's buffer may override the default.
2065 gtk_text_view_set_justification (GtkTextView *text_view,
2066 GtkJustification justification)
2068 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2070 if (text_view->justify != justification)
2072 text_view->justify = justification;
2074 if (text_view->layout)
2076 text_view->layout->default_style->justification = justification;
2077 gtk_text_layout_default_style_changed (text_view->layout);
2080 g_object_notify (G_OBJECT (text_view), "justification");
2085 * gtk_text_view_get_justification:
2086 * @text_view: a #GtkTextView
2088 * Gets the default justification of paragraphs in @text_view.
2089 * Tags in the buffer may override the default.
2091 * Return value: default justification
2094 gtk_text_view_get_justification (GtkTextView *text_view)
2096 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2098 return text_view->justify;
2102 * gtk_text_view_set_left_margin:
2103 * @text_view: a #GtkTextView
2104 * @left_margin: left margin in pixels
2106 * Sets the default left margin for text in @text_view.
2107 * Tags in the buffer may override the default.
2111 gtk_text_view_set_left_margin (GtkTextView *text_view,
2114 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2116 if (text_view->left_margin != left_margin)
2118 text_view->left_margin = left_margin;
2120 if (text_view->layout)
2122 text_view->layout->default_style->left_margin = left_margin;
2123 gtk_text_layout_default_style_changed (text_view->layout);
2126 g_object_notify (G_OBJECT (text_view), "left_margin");
2131 * gtk_text_view_get_left_margin:
2132 * @text_view: a #GtkTextView
2134 * Gets the default left margin size of paragraphs in the @text_view.
2135 * Tags in the buffer may override the default.
2137 * Return value: left margin in pixels
2140 gtk_text_view_get_left_margin (GtkTextView *text_view)
2142 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2144 return text_view->left_margin;
2148 * gtk_text_view_set_right_margin:
2149 * @text_view: a #GtkTextView
2150 * @right_margin: right margin in pixels
2152 * Sets the default right margin for text in the text view.
2153 * Tags in the buffer may override the default.
2157 gtk_text_view_set_right_margin (GtkTextView *text_view,
2160 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2162 if (text_view->right_margin != right_margin)
2164 text_view->right_margin = right_margin;
2166 if (text_view->layout)
2168 text_view->layout->default_style->right_margin = right_margin;
2169 gtk_text_layout_default_style_changed (text_view->layout);
2172 g_object_notify (G_OBJECT (text_view), "right_margin");
2177 * gtk_text_view_get_right_margin:
2178 * @text_view: a #GtkTextView
2180 * Gets the default right margin for text in @text_view. Tags
2181 * in the buffer may override the default.
2183 * Return value: right margin in pixels
2186 gtk_text_view_get_right_margin (GtkTextView *text_view)
2188 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2190 return text_view->right_margin;
2194 * gtk_text_view_set_indent:
2195 * @text_view: a #GtkTextView
2196 * @indent: indentation in pixels
2198 * Sets the default indentation for paragraphs in @text_view.
2199 * Tags in the buffer may override the default.
2203 gtk_text_view_set_indent (GtkTextView *text_view,
2206 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2208 if (text_view->indent != indent)
2210 text_view->indent = indent;
2212 if (text_view->layout)
2214 text_view->layout->default_style->indent = indent;
2215 gtk_text_layout_default_style_changed (text_view->layout);
2218 g_object_notify (G_OBJECT (text_view), "indent");
2223 * gtk_text_view_get_indent:
2224 * @text_view: a #GtkTextView
2226 * Gets the default indentation of paragraphs in @text_view.
2227 * Tags in the view's buffer may override the default.
2228 * The indentation may be negative.
2230 * Return value: number of pixels of indentation
2233 gtk_text_view_get_indent (GtkTextView *text_view)
2235 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2237 return text_view->indent;
2241 * gtk_text_view_set_tabs:
2242 * @text_view: a #GtkTextView
2243 * @tabs: tabs as a #PangoTabArray
2245 * Sets the default tab stops for paragraphs in @text_view.
2246 * Tags in the buffer may override the default.
2250 gtk_text_view_set_tabs (GtkTextView *text_view,
2251 PangoTabArray *tabs)
2253 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2255 if (text_view->tabs)
2256 pango_tab_array_free (text_view->tabs);
2258 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2260 if (text_view->layout)
2262 /* some unkosher futzing in internal struct details... */
2263 if (text_view->layout->default_style->tabs)
2264 pango_tab_array_free (text_view->layout->default_style->tabs);
2266 text_view->layout->default_style->tabs =
2267 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2269 gtk_text_layout_default_style_changed (text_view->layout);
2272 g_object_notify (G_OBJECT (text_view), "tabs");
2276 * gtk_text_view_get_tabs:
2277 * @text_view: a #GtkTextView
2279 * Gets the default tabs for @text_view. Tags in the buffer may
2280 * override the defaults. The returned array will be %NULL if
2281 * "standard" (8-space) tabs are used. Free the return value
2282 * with pango_tab_array_free().
2284 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2287 gtk_text_view_get_tabs (GtkTextView *text_view)
2289 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2291 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2295 * gtk_text_view_set_cursor_visible:
2296 * @text_view: a #GtkTextView
2297 * @setting: whether to show the insertion cursor
2299 * Toggles whether the insertion point is displayed. A buffer with no editable
2300 * text probably shouldn't have a visible cursor, so you may want to turn
2304 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2307 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2309 setting = (setting != FALSE);
2311 if (text_view->cursor_visible != setting)
2313 text_view->cursor_visible = setting;
2315 if (GTK_WIDGET_HAS_FOCUS (text_view))
2317 if (text_view->layout)
2319 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2320 gtk_text_view_check_cursor_blink (text_view);
2324 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2329 * gtk_text_view_get_cursor_visible:
2330 * @text_view: a #GtkTextView
2332 * Find out whether the cursor is being displayed.
2334 * Return value: whether the insertion mark is visible
2337 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2339 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2341 return text_view->cursor_visible;
2346 * gtk_text_view_place_cursor_onscreen:
2347 * @text_view: a #GtkTextView
2349 * Moves the cursor to the currently visible region of the
2350 * buffer, it it isn't there already.
2352 * Return value: TRUE if the cursor had to be moved.
2355 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2359 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2361 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2362 gtk_text_buffer_get_mark (get_buffer (text_view),
2365 if (clamp_iter_onscreen (text_view, &insert))
2367 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2375 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2377 if (text_view->first_validate_idle != 0)
2379 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2380 g_source_remove (text_view->first_validate_idle);
2381 text_view->first_validate_idle = 0;
2384 if (text_view->incremental_validate_idle != 0)
2386 g_source_remove (text_view->incremental_validate_idle);
2387 text_view->incremental_validate_idle = 0;
2392 gtk_text_view_destroy (GtkObject *object)
2394 GtkTextView *text_view;
2395 GtkTextLayout *layout;
2397 text_view = GTK_TEXT_VIEW (object);
2399 layout = text_view->layout;
2401 gtk_text_view_remove_validate_idles (text_view);
2402 gtk_text_view_set_buffer (text_view, NULL);
2403 gtk_text_view_destroy_layout (text_view);
2405 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2409 gtk_text_view_finalize (GObject *object)
2411 GtkTextView *text_view;
2413 text_view = GTK_TEXT_VIEW (object);
2415 g_return_if_fail (text_view->buffer == NULL);
2417 gtk_text_view_destroy_layout (text_view);
2418 gtk_text_view_set_buffer (text_view, NULL);
2420 if (text_view->pending_scroll)
2422 free_pending_scroll (text_view->pending_scroll);
2423 text_view->pending_scroll = NULL;
2426 if (text_view->tabs)
2427 pango_tab_array_free (text_view->tabs);
2429 if (text_view->hadjustment)
2430 g_object_unref (G_OBJECT (text_view->hadjustment));
2431 if (text_view->vadjustment)
2432 g_object_unref (G_OBJECT (text_view->vadjustment));
2434 text_window_free (text_view->text_window);
2436 if (text_view->left_window)
2437 text_window_free (text_view->left_window);
2439 if (text_view->top_window)
2440 text_window_free (text_view->top_window);
2442 if (text_view->right_window)
2443 text_window_free (text_view->right_window);
2445 if (text_view->bottom_window)
2446 text_window_free (text_view->bottom_window);
2448 g_object_unref (G_OBJECT (text_view->im_context));
2450 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2454 gtk_text_view_set_property (GObject *object,
2456 const GValue *value,
2459 GtkTextView *text_view;
2461 text_view = GTK_TEXT_VIEW (object);
2465 case PROP_PIXELS_ABOVE_LINES:
2466 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2469 case PROP_PIXELS_BELOW_LINES:
2470 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2473 case PROP_PIXELS_INSIDE_WRAP:
2474 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2478 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2481 case PROP_WRAP_MODE:
2482 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2485 case PROP_JUSTIFICATION:
2486 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2489 case PROP_LEFT_MARGIN:
2490 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2493 case PROP_RIGHT_MARGIN:
2494 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2498 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2502 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2505 case PROP_CURSOR_VISIBLE:
2506 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2510 g_assert_not_reached ();
2516 gtk_text_view_get_property (GObject *object,
2521 GtkTextView *text_view;
2523 text_view = GTK_TEXT_VIEW (object);
2527 case PROP_PIXELS_ABOVE_LINES:
2528 g_value_set_int (value, text_view->pixels_above_lines);
2531 case PROP_PIXELS_BELOW_LINES:
2532 g_value_set_int (value, text_view->pixels_below_lines);
2535 case PROP_PIXELS_INSIDE_WRAP:
2536 g_value_set_int (value, text_view->pixels_inside_wrap);
2540 g_value_set_boolean (value, text_view->editable);
2543 case PROP_WRAP_MODE:
2544 g_value_set_enum (value, text_view->wrap_mode);
2547 case PROP_JUSTIFICATION:
2548 g_value_set_enum (value, text_view->justify);
2551 case PROP_LEFT_MARGIN:
2552 g_value_set_int (value, text_view->left_margin);
2555 case PROP_RIGHT_MARGIN:
2556 g_value_set_int (value, text_view->right_margin);
2560 g_value_set_int (value, text_view->indent);
2564 g_value_set_boxed (value, text_view->tabs);
2567 case PROP_CURSOR_VISIBLE:
2568 g_value_set_boolean (value, text_view->cursor_visible);
2572 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2578 gtk_text_view_size_request (GtkWidget *widget,
2579 GtkRequisition *requisition)
2581 GtkTextView *text_view;
2583 gint focus_edge_width;
2585 gboolean interior_focus;
2587 text_view = GTK_TEXT_VIEW (widget);
2589 gtk_widget_style_get (widget,
2590 "interior_focus", &interior_focus,
2591 "focus_line_width", &focus_width,
2595 focus_edge_width = 0;
2597 focus_edge_width = focus_width;
2599 if (text_view->layout)
2601 text_view->text_window->requisition.width = text_view->layout->width;
2602 text_view->text_window->requisition.height = text_view->layout->height;
2606 text_view->text_window->requisition.width = 0;
2607 text_view->text_window->requisition.height = 0;
2610 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2611 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2613 if (text_view->left_window)
2614 requisition->width += text_view->left_window->requisition.width;
2616 if (text_view->right_window)
2617 requisition->width += text_view->right_window->requisition.width;
2619 if (text_view->top_window)
2620 requisition->height += text_view->top_window->requisition.height;
2622 if (text_view->bottom_window)
2623 requisition->height += text_view->bottom_window->requisition.height;
2625 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2626 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2628 tmp_list = text_view->children;
2629 while (tmp_list != NULL)
2631 GtkTextViewChild *child = tmp_list->data;
2635 GtkRequisition child_req;
2636 GtkRequisition old_req;
2638 gtk_widget_get_child_requisition (child->widget, &old_req);
2640 gtk_widget_size_request (child->widget, &child_req);
2642 gtk_widget_get_child_requisition (child->widget, &child_req);
2644 /* Invalidate layout lines if required */
2645 if (text_view->layout &&
2646 (old_req.width != child_req.width ||
2647 old_req.height != child_req.height))
2648 gtk_text_child_anchor_queue_resize (child->anchor,
2653 GtkRequisition child_req;
2655 gtk_widget_size_request (child->widget, &child_req);
2658 tmp_list = g_slist_next (tmp_list);
2663 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2664 GtkTextViewChild *vc,
2665 GtkAllocation *allocation)
2671 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2675 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2678 buffer_y += vc->from_top_of_line;
2680 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2681 allocation->y = buffer_y - text_view->yoffset;
2683 gtk_widget_get_child_requisition (vc->widget, &req);
2684 allocation->width = req.width;
2685 allocation->height = req.height;
2689 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2690 GtkTextViewChild *vc)
2692 GtkAllocation allocation;
2694 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2696 gtk_widget_size_allocate (vc->widget, &allocation);
2699 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2701 vc->widget->allocation.x,
2702 vc->widget->allocation.y,
2703 text_view->yoffset);
2708 gtk_text_view_child_allocated (GtkTextLayout *layout,
2714 GtkTextViewChild *vc = NULL;
2715 GtkTextView *text_view = data;
2717 /* x,y is the position of the child from the top of the line, and
2718 * from the left of the buffer. We have to translate that into text
2719 * window coordinates, then size_allocate the child.
2722 vc = g_object_get_data (G_OBJECT (child),
2723 "gtk-text-view-child");
2725 g_assert (vc != NULL);
2727 DV (g_print ("child allocated at %d,%d\n", x, y));
2729 vc->from_left_of_buffer = x;
2730 vc->from_top_of_line = y;
2732 gtk_text_view_update_child_allocation (text_view, vc);
2736 gtk_text_view_allocate_children (GtkTextView *text_view)
2740 DV(g_print(G_STRLOC"\n"));
2742 tmp_list = text_view->children;
2743 while (tmp_list != NULL)
2745 GtkTextViewChild *child = tmp_list->data;
2749 /* We need to force-validate the regions containing
2752 GtkTextIter child_loc;
2753 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2757 gtk_text_layout_validate_yrange (text_view->layout,
2763 GtkAllocation allocation;
2764 GtkRequisition child_req;
2766 g_assert (child != NULL);
2768 allocation.x = child->x;
2769 allocation.y = child->y;
2771 gtk_widget_get_child_requisition (child->widget, &child_req);
2773 allocation.width = child_req.width;
2774 allocation.height = child_req.height;
2776 gtk_widget_size_allocate (child->widget, &allocation);
2779 tmp_list = g_slist_next (tmp_list);
2784 gtk_text_view_size_allocate (GtkWidget *widget,
2785 GtkAllocation *allocation)
2787 GtkTextView *text_view;
2788 GtkTextIter first_para;
2790 GtkAdjustment *vadj;
2791 gboolean yoffset_changed = FALSE;
2793 GdkRectangle text_rect;
2794 GdkRectangle left_rect;
2795 GdkRectangle right_rect;
2796 GdkRectangle top_rect;
2797 GdkRectangle bottom_rect;
2798 gint focus_edge_width;
2800 gboolean interior_focus;
2801 gboolean size_changed;
2803 text_view = GTK_TEXT_VIEW (widget);
2805 DV(g_print(G_STRLOC"\n"));
2808 widget->allocation.width != allocation->width ||
2809 widget->allocation.height != allocation->height;
2811 widget->allocation = *allocation;
2813 if (GTK_WIDGET_REALIZED (widget))
2815 gdk_window_move_resize (widget->window,
2816 allocation->x, allocation->y,
2817 allocation->width, allocation->height);
2820 /* distribute width/height among child windows. Ensure all
2821 * windows get at least a 1x1 allocation.
2824 gtk_widget_style_get (widget,
2825 "interior_focus", &interior_focus,
2826 "focus_line_width", &focus_width,
2830 focus_edge_width = 0;
2832 focus_edge_width = focus_width;
2834 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2836 if (text_view->left_window)
2837 left_rect.width = text_view->left_window->requisition.width;
2839 left_rect.width = 0;
2841 width -= left_rect.width;
2843 if (text_view->right_window)
2844 right_rect.width = text_view->right_window->requisition.width;
2846 right_rect.width = 0;
2848 width -= right_rect.width;
2850 text_rect.width = MAX (1, width);
2852 top_rect.width = text_rect.width;
2853 bottom_rect.width = text_rect.width;
2856 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2858 if (text_view->top_window)
2859 top_rect.height = text_view->top_window->requisition.height;
2861 top_rect.height = 0;
2863 height -= top_rect.height;
2865 if (text_view->bottom_window)
2866 bottom_rect.height = text_view->bottom_window->requisition.height;
2868 bottom_rect.height = 0;
2870 height -= bottom_rect.height;
2872 text_rect.height = MAX (1, height);
2874 left_rect.height = text_rect.height;
2875 right_rect.height = text_rect.height;
2878 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2879 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2881 text_rect.x = left_rect.x + left_rect.width;
2882 text_rect.y = top_rect.y + top_rect.height;
2884 left_rect.y = text_rect.y;
2885 right_rect.y = text_rect.y;
2887 top_rect.x = text_rect.x;
2888 bottom_rect.x = text_rect.x;
2890 right_rect.x = text_rect.x + text_rect.width;
2891 bottom_rect.y = text_rect.y + text_rect.height;
2893 text_window_size_allocate (text_view->text_window,
2896 if (text_view->left_window)
2897 text_window_size_allocate (text_view->left_window,
2900 if (text_view->right_window)
2901 text_window_size_allocate (text_view->right_window,
2904 if (text_view->top_window)
2905 text_window_size_allocate (text_view->top_window,
2908 if (text_view->bottom_window)
2909 text_window_size_allocate (text_view->bottom_window,
2912 gtk_text_view_update_layout_width (text_view);
2914 /* Note that this will do some layout validation */
2915 gtk_text_view_allocate_children (text_view);
2917 /* Now adjust the value of the adjustment to keep the cursor at the
2918 * same place in the buffer
2920 gtk_text_view_get_first_para_iter (text_view, &first_para);
2921 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2923 y += text_view->first_para_pixels;
2925 /* Ensure h/v adj exist */
2926 get_hadjustment (text_view);
2927 get_vadjustment (text_view);
2929 vadj = text_view->vadjustment;
2930 if (y > vadj->upper - vadj->page_size)
2931 y = MAX (0, vadj->upper - vadj->page_size);
2933 if (y != text_view->yoffset)
2936 yoffset_changed = TRUE;
2939 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2940 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
2941 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
2942 text_view->hadjustment->lower = 0;
2943 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2945 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2947 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2948 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2949 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2950 text_view->vadjustment->lower = 0;
2951 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2953 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2955 if (yoffset_changed)
2956 gtk_adjustment_value_changed (vadj);
2958 /* The GTK resize loop processes all the pending exposes right
2959 * after doing the resize stuff, so the idle sizer won't have a
2960 * chance to run. So we do the work here.
2962 gtk_text_view_flush_first_validate (text_view);
2964 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2967 if (size_changed && GTK_WIDGET_REALIZED (widget))
2968 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2972 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2975 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2976 text_view->first_para_mark);
2980 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2982 GtkWidget *widget = GTK_WIDGET (text_view);
2984 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
2986 if (SCREEN_HEIGHT (widget) > 0)
2988 GtkTextIter first_para;
2990 /* Be sure we've validated the stuff onscreen; if we
2991 * scrolled, these calls won't have any effect, because
2992 * they were called in the recursive validate_onscreen
2994 gtk_text_view_get_first_para_iter (text_view, &first_para);
2996 gtk_text_layout_validate_yrange (text_view->layout,
2999 text_view->first_para_pixels +
3000 SCREEN_HEIGHT (widget));
3003 text_view->onscreen_validated = TRUE;
3005 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3007 /* This can have the odd side effect of triggering a scroll, which should
3008 * flip "onscreen_validated" back to FALSE, but should also get us
3009 * back into this function to turn it on again.
3011 gtk_text_view_update_adjustments (text_view);
3013 g_assert (text_view->onscreen_validated);
3017 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3019 if (text_view->first_validate_idle == 0)
3022 /* Do this first, which means that if an "invalidate"
3023 * occurs during any of this process, a new first_validate_callback
3024 * will be installed, and we'll start again.
3026 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3027 g_source_remove (text_view->first_validate_idle);
3028 text_view->first_validate_idle = 0;
3030 /* be sure we have up-to-date screen size set on the
3033 gtk_text_view_update_layout_width (text_view);
3035 /* Bail out if we invalidated stuff; scrolling right away will just
3036 * confuse the issue.
3038 if (text_view->first_validate_idle != 0)
3040 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3044 /* scroll to any marks, if that's pending. This can
3045 * jump us to the validation codepath used for scrolling
3046 * onscreen, if so we bail out.
3048 if (!gtk_text_view_flush_scroll (text_view))
3049 gtk_text_view_validate_onscreen (text_view);
3051 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3053 g_assert (text_view->onscreen_validated);
3058 first_validate_callback (gpointer data)
3060 GtkTextView *text_view = data;
3062 GDK_THREADS_ENTER ();
3064 /* Note that some of this code is duplicated at the end of size_allocate,
3065 * keep in sync with that.
3068 DV(g_print(G_STRLOC"\n"));
3070 gtk_text_view_flush_first_validate (text_view);
3072 GDK_THREADS_LEAVE ();
3078 incremental_validate_callback (gpointer data)
3080 GtkTextView *text_view = data;
3081 gboolean result = TRUE;
3083 GDK_THREADS_ENTER ();
3085 DV(g_print(G_STRLOC"\n"));
3087 gtk_text_layout_validate (text_view->layout, 2000);
3089 gtk_text_view_update_adjustments (text_view);
3091 if (gtk_text_layout_is_valid (text_view->layout))
3093 text_view->incremental_validate_idle = 0;
3097 GDK_THREADS_LEAVE ();
3103 gtk_text_view_invalidate (GtkTextView *text_view)
3105 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3106 text_view->onscreen_validated));
3108 text_view->onscreen_validated = FALSE;
3110 /* We'll invalidate when the layout is created */
3111 if (text_view->layout == NULL)
3114 if (!text_view->first_validate_idle)
3116 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3117 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3118 text_view->first_validate_idle));
3121 if (!text_view->incremental_validate_idle)
3123 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3124 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3125 text_view->incremental_validate_idle));
3130 invalidated_handler (GtkTextLayout *layout,
3133 GtkTextView *text_view;
3135 text_view = GTK_TEXT_VIEW (data);
3137 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3138 gtk_text_view_invalidate (text_view);
3142 changed_handler (GtkTextLayout *layout,
3148 GtkTextView *text_view;
3150 GdkRectangle visible_rect;
3151 GdkRectangle redraw_rect;
3153 text_view = GTK_TEXT_VIEW (data);
3154 widget = GTK_WIDGET (data);
3156 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3158 if (GTK_WIDGET_REALIZED (text_view))
3160 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3162 redraw_rect.x = visible_rect.x;
3163 redraw_rect.width = visible_rect.width;
3164 redraw_rect.y = start_y;
3166 if (old_height == new_height)
3167 redraw_rect.height = old_height;
3168 else if (start_y + old_height > visible_rect.y)
3169 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3171 redraw_rect.height = 0;
3173 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3175 /* text_window_invalidate_rect() takes buffer coordinates */
3176 text_window_invalidate_rect (text_view->text_window,
3179 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3183 redraw_rect.height));
3185 if (text_view->left_window)
3186 text_window_invalidate_rect (text_view->left_window,
3188 if (text_view->right_window)
3189 text_window_invalidate_rect (text_view->right_window,
3191 if (text_view->top_window)
3192 text_window_invalidate_rect (text_view->top_window,
3194 if (text_view->bottom_window)
3195 text_window_invalidate_rect (text_view->bottom_window,
3198 gtk_text_view_update_im_spot_location (text_view);
3202 if (old_height != new_height)
3204 gboolean yoffset_changed = FALSE;
3206 int new_first_para_top;
3207 int old_first_para_top;
3210 /* If the bottom of the old area was above the top of the
3211 * screen, we need to scroll to keep the current top of the
3212 * screen in place. Remember that first_para_pixels is the
3213 * position of the top of the screen in coordinates relative to
3214 * the first paragraph onscreen.
3216 * In short we are adding the height delta of the portion of the
3217 * changed region above first_para_mark to text_view->yoffset.
3219 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3220 text_view->first_para_mark);
3222 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3224 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3226 if (new_first_para_top != old_first_para_top)
3228 text_view->yoffset += new_first_para_top - old_first_para_top;
3230 get_vadjustment (text_view)->value = text_view->yoffset;
3231 yoffset_changed = TRUE;
3234 if (yoffset_changed)
3236 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3237 gtk_adjustment_value_changed (get_vadjustment (text_view));
3240 /* FIXME be smarter about which anchored widgets we update */
3242 tmp_list = text_view->children;
3243 while (tmp_list != NULL)
3245 GtkTextViewChild *child = tmp_list->data;
3248 gtk_text_view_update_child_allocation (text_view, child);
3250 tmp_list = g_slist_next (tmp_list);
3255 GtkRequisition old_req;
3256 GtkRequisition new_req;
3258 old_req = widget->requisition;
3260 /* Use this instead of gtk_widget_size_request wrapper
3261 * to avoid the optimization which just returns widget->requisition
3262 * if a resize hasn't been queued.
3264 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3266 if (old_req.width != new_req.width ||
3267 old_req.height != new_req.height)
3269 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3270 _gtk_size_group_queue_resize (widget);
3276 gtk_text_view_realize (GtkWidget *widget)
3278 GtkTextView *text_view;
3279 GdkWindowAttr attributes;
3280 gint attributes_mask;
3283 text_view = GTK_TEXT_VIEW (widget);
3284 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3286 attributes.window_type = GDK_WINDOW_CHILD;
3287 attributes.x = widget->allocation.x;
3288 attributes.y = widget->allocation.y;
3289 attributes.width = widget->allocation.width;
3290 attributes.height = widget->allocation.height;
3291 attributes.wclass = GDK_INPUT_OUTPUT;
3292 attributes.visual = gtk_widget_get_visual (widget);
3293 attributes.colormap = gtk_widget_get_colormap (widget);
3294 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3296 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3298 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3299 &attributes, attributes_mask);
3300 gdk_window_set_user_data (widget->window, widget);
3302 /* must come before text_window_realize calls */
3303 widget->style = gtk_style_attach (widget->style, widget->window);
3305 gdk_window_set_background (widget->window,
3306 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3308 text_window_realize (text_view->text_window, widget->window);
3310 if (text_view->left_window)
3311 text_window_realize (text_view->left_window,
3314 if (text_view->top_window)
3315 text_window_realize (text_view->top_window,
3318 if (text_view->right_window)
3319 text_window_realize (text_view->right_window,
3322 if (text_view->bottom_window)
3323 text_window_realize (text_view->bottom_window,
3326 gtk_text_view_ensure_layout (text_view);
3328 if (text_view->buffer)
3330 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3331 GDK_SELECTION_PRIMARY);
3332 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3335 tmp_list = text_view->children;
3336 while (tmp_list != NULL)
3338 GtkTextViewChild *vc = tmp_list->data;
3340 text_view_child_set_parent_window (text_view, vc);
3342 tmp_list = tmp_list->next;
3347 gtk_text_view_unrealize (GtkWidget *widget)
3349 GtkTextView *text_view;
3351 text_view = GTK_TEXT_VIEW (widget);
3353 if (text_view->buffer)
3355 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3356 GDK_SELECTION_PRIMARY);
3357 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3360 gtk_text_view_remove_validate_idles (text_view);
3362 if (text_view->popup_menu)
3364 gtk_widget_destroy (text_view->popup_menu);
3365 text_view->popup_menu = NULL;
3368 text_window_unrealize (text_view->text_window);
3370 if (text_view->left_window)
3371 text_window_unrealize (text_view->left_window);
3373 if (text_view->top_window)
3374 text_window_unrealize (text_view->top_window);
3376 if (text_view->right_window)
3377 text_window_unrealize (text_view->right_window);
3379 if (text_view->bottom_window)
3380 text_window_unrealize (text_view->bottom_window);
3382 gtk_text_view_destroy_layout (text_view);
3384 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3388 gtk_text_view_style_set (GtkWidget *widget,
3389 GtkStyle *previous_style)
3391 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3393 if (GTK_WIDGET_REALIZED (widget))
3395 gdk_window_set_background (widget->window,
3396 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3398 gdk_window_set_background (text_view->text_window->bin_window,
3399 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3401 if (text_view->left_window)
3402 gdk_window_set_background (text_view->left_window->bin_window,
3403 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3404 if (text_view->right_window)
3405 gdk_window_set_background (text_view->right_window->bin_window,
3406 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3408 if (text_view->top_window)
3409 gdk_window_set_background (text_view->top_window->bin_window,
3410 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3412 if (text_view->bottom_window)
3413 gdk_window_set_background (text_view->bottom_window->bin_window,
3414 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3417 if (text_view->layout && previous_style)
3419 gtk_text_view_set_attributes_from_style (text_view,
3420 text_view->layout->default_style,
3422 gtk_text_layout_default_style_changed (text_view->layout);
3427 gtk_text_view_direction_changed (GtkWidget *widget,
3428 GtkTextDirection previous_direction)
3430 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3432 if (text_view->layout)
3434 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3435 gtk_text_layout_default_style_changed (text_view->layout);
3441 set_invisible_cursor (GdkWindow *window)
3443 GdkBitmap *empty_bitmap;
3446 char invisible_cursor_bits[] = { 0x0 };
3448 useless.red = useless.green = useless.blue = 0;
3451 empty_bitmap = gdk_bitmap_create_from_data (window,
3452 invisible_cursor_bits,
3455 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3460 gdk_window_set_cursor (window, cursor);
3462 gdk_cursor_unref (cursor);
3464 g_object_unref (empty_bitmap);
3468 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3470 if (text_view->mouse_cursor_obscured)
3473 set_invisible_cursor (text_view->text_window->bin_window);
3475 text_view->mouse_cursor_obscured = TRUE;
3483 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3486 switch (event->type)
3488 case GDK_MOTION_NOTIFY:
3489 *x = event->motion.x;
3490 *y = event->motion.y;
3494 case GDK_BUTTON_PRESS:
3495 case GDK_2BUTTON_PRESS:
3496 case GDK_3BUTTON_PRESS:
3497 case GDK_BUTTON_RELEASE:
3498 *x = event->button.x;
3499 *y = event->button.y;
3504 case GDK_KEY_RELEASE:
3505 case GDK_ENTER_NOTIFY:
3506 case GDK_LEAVE_NOTIFY:
3507 case GDK_PROPERTY_NOTIFY:
3508 case GDK_SELECTION_CLEAR:
3509 case GDK_SELECTION_REQUEST:
3510 case GDK_SELECTION_NOTIFY:
3511 case GDK_PROXIMITY_IN:
3512 case GDK_PROXIMITY_OUT:
3513 case GDK_DRAG_ENTER:
3514 case GDK_DRAG_LEAVE:
3515 case GDK_DRAG_MOTION:
3516 case GDK_DRAG_STATUS:
3517 case GDK_DROP_START:
3518 case GDK_DROP_FINISHED:
3528 emit_event_on_tags (GtkWidget *widget,
3534 gboolean retval = FALSE;
3535 GtkTextView *text_view;
3537 text_view = GTK_TEXT_VIEW (widget);
3539 tags = gtk_text_iter_get_tags (iter);
3544 GtkTextTag *tag = tmp->data;
3546 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3552 tmp = g_slist_next (tmp);
3555 g_slist_free (tags);
3561 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3563 GtkTextView *text_view;
3566 text_view = GTK_TEXT_VIEW (widget);
3568 if (text_view->layout == NULL ||
3569 get_buffer (text_view) == NULL)
3572 if (event->any.window != text_view->text_window->bin_window)
3575 if (get_event_coordinates (event, &x, &y))
3579 x += text_view->xoffset;
3580 y += text_view->yoffset;
3582 /* FIXME this is slow and we do it twice per event.
3583 * My favorite solution is to have GtkTextLayout cache
3584 * the last couple lookups.
3586 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3590 return emit_event_on_tags (widget, event, &iter);
3592 else if (event->type == GDK_KEY_PRESS ||
3593 event->type == GDK_KEY_RELEASE)
3595 GtkTextMark *insert;
3598 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3601 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3603 return emit_event_on_tags (widget, event, &iter);
3610 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3612 gboolean retval = FALSE;
3613 gboolean obscure = FALSE;
3614 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3615 GtkTextMark *insert;
3617 gboolean can_insert;
3619 if (text_view->layout == NULL ||
3620 get_buffer (text_view) == NULL)
3623 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3624 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3625 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3627 gtk_im_context_filter_keypress (text_view->im_context, event))
3629 text_view->need_im_reset = TRUE;
3634 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3635 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3637 /* use overall editability not can_insert, more predictable for users */
3638 else if (text_view->editable &&
3639 (event->keyval == GDK_Return ||
3640 event->keyval == GDK_KP_Enter))
3642 /* this won't actually insert the newline if the cursor isn't
3645 gtk_text_view_commit_text (text_view, "\n");
3650 /* Pass through Tab as literal tab, unless Control is held down */
3651 else if ((event->keyval == GDK_Tab ||
3652 event->keyval == GDK_KP_Tab) &&
3653 !(event->state & GDK_CONTROL_MASK))
3655 /* If the text widget isn't editable overall, move the focus
3658 if (text_view->editable)
3660 gtk_text_view_commit_text (text_view, "\t");
3664 gtk_text_view_move_focus (text_view,
3665 (event->state & GDK_SHIFT_MASK) ?
3666 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3674 gtk_text_view_obscure_mouse_cursor (text_view);
3676 gtk_text_view_pend_cursor_blink (text_view);
3682 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3684 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3685 GtkTextMark *insert;
3688 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3691 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3692 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3693 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3694 gtk_im_context_filter_keypress (text_view->im_context, event))
3696 text_view->need_im_reset = TRUE;
3700 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3704 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3706 GtkTextView *text_view;
3708 text_view = GTK_TEXT_VIEW (widget);
3710 text_view->disable_scroll_on_focus = TRUE;
3711 gtk_widget_grab_focus (widget);
3712 text_view->disable_scroll_on_focus = FALSE;
3714 if (event->window != text_view->text_window->bin_window)
3716 /* Remove selection if any. */
3717 gtk_text_view_unselect (text_view);
3723 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3724 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3725 else if (event->button == 3)
3726 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3729 if (event->type == GDK_BUTTON_PRESS)
3731 gtk_text_view_reset_im_context (text_view);
3733 if (event->button == 1)
3735 /* If we're in the selection, start a drag copy/move of the
3736 * selection; otherwise, start creating a new selection.
3739 GtkTextIter start, end;
3741 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3743 event->x + text_view->xoffset,
3744 event->y + text_view->yoffset);
3746 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3748 gtk_text_iter_in_range (&iter, &start, &end))
3750 text_view->drag_start_x = event->x;
3751 text_view->drag_start_y = event->y;
3752 text_view->pending_place_cursor_button = event->button;
3756 gtk_text_view_start_selection_drag (text_view, &iter, event);
3761 else if (event->button == 2)
3765 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3767 event->x + text_view->xoffset,
3768 event->y + text_view->yoffset);
3770 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3771 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3773 text_view->editable);
3776 else if (event->button == 3)
3778 gtk_text_view_do_popup (text_view, event);
3781 else if ((event->type == GDK_2BUTTON_PRESS ||
3782 event->type == GDK_3BUTTON_PRESS) &&
3785 GtkTextIter start, end;
3787 /* End the selection drag, otherwise we'd clear the new
3788 * word/line selection on button release
3790 gtk_text_view_end_selection_drag (text_view, event);
3792 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3794 event->x + text_view->xoffset,
3795 event->y + text_view->yoffset);
3799 if (event->type == GDK_2BUTTON_PRESS)
3801 if (gtk_text_iter_inside_word (&start))
3803 if (!gtk_text_iter_starts_word (&start))
3804 gtk_text_iter_backward_word_start (&start);
3806 if (!gtk_text_iter_ends_word (&end))
3807 gtk_text_iter_forward_word_end (&end);
3810 else if (event->type == GDK_3BUTTON_PRESS)
3812 if (gtk_text_view_starts_display_line (text_view, &start))
3814 /* If on a display line boundary, we assume the user
3815 * clicked off the end of a line and we therefore select
3816 * the line before the boundary.
3818 gtk_text_view_backward_display_line_start (text_view, &start);
3822 /* start isn't on the start of a line, so we move it to the
3823 * start, and move end to the end unless it's already there.
3825 gtk_text_view_backward_display_line_start (text_view, &start);
3827 if (!gtk_text_view_starts_display_line (text_view, &end))
3828 gtk_text_view_forward_display_line_end (text_view, &end);
3832 if (event->state & GDK_SHIFT_MASK)
3834 /* Take union of old and new selection */
3835 GtkTextIter old_start, old_end;
3837 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3838 &old_start, &old_end);
3840 gtk_text_iter_order (&start, &old_start);
3841 gtk_text_iter_order (&old_end, &end);
3843 /* Now start is the first of the starts, and end is the
3848 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3851 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3855 text_view->just_selected_element = TRUE;
3864 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3866 GtkTextView *text_view;
3868 text_view = GTK_TEXT_VIEW (widget);
3870 if (event->window != text_view->text_window->bin_window)
3873 if (event->button == 1)
3875 if (text_view->drag_start_x >= 0)
3877 text_view->drag_start_x = -1;
3878 text_view->drag_start_y = -1;
3881 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3883 else if (text_view->just_selected_element)
3885 text_view->just_selected_element = FALSE;
3888 else if (text_view->pending_place_cursor_button == event->button)
3892 /* Unselect everything; we clicked inside selection, but
3893 * didn't move by the drag threshold, so just clear selection
3896 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3898 event->x + text_view->xoffset,
3899 event->y + text_view->yoffset);
3901 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3903 text_view->pending_place_cursor_button = 0;
3913 keymap_direction_changed (GdkKeymap *keymap,
3914 GtkTextView *text_view)
3916 gtk_text_view_check_keymap_direction (text_view);
3920 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3922 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3924 gtk_widget_queue_draw (widget);
3926 DV(g_print (G_STRLOC": focus_in_event\n"));
3928 if (text_view->cursor_visible && text_view->layout)
3930 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3931 gtk_text_view_check_cursor_blink (text_view);
3934 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3935 "direction_changed",
3936 G_CALLBACK (keymap_direction_changed), text_view);
3937 gtk_text_view_check_keymap_direction (text_view);
3939 text_view->need_im_reset = TRUE;
3940 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3946 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3948 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3950 gtk_widget_queue_draw (widget);
3952 DV(g_print (G_STRLOC": focus_out_event\n"));
3954 if (text_view->cursor_visible && text_view->layout)
3956 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3957 gtk_text_view_check_cursor_blink (text_view);
3960 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3961 (gpointer) keymap_direction_changed,
3964 text_view->need_im_reset = TRUE;
3965 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3971 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3973 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3975 if (text_view->mouse_cursor_obscured)
3979 cursor = gdk_cursor_new_for_screen (gtk_widget_get_screen (widget),
3981 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3982 gdk_cursor_unref (cursor);
3983 text_view->mouse_cursor_obscured = FALSE;
3986 if (event->window == text_view->text_window->bin_window &&
3987 text_view->drag_start_x >= 0)
3991 gdk_window_get_pointer (text_view->text_window->bin_window,
3994 if (gtk_drag_check_threshold (widget,
3995 text_view->drag_start_x,
3996 text_view->drag_start_y,
4000 gint buffer_x, buffer_y;
4002 gtk_text_view_window_to_buffer_coords (text_view,
4003 GTK_TEXT_WINDOW_TEXT,
4004 text_view->drag_start_x,
4005 text_view->drag_start_y,
4009 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4011 buffer_x, buffer_y);
4013 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4022 gtk_text_view_paint (GtkWidget *widget,
4024 GdkEventExpose *event)
4026 GtkTextView *text_view;
4027 GList *child_exposes;
4031 text_view = GTK_TEXT_VIEW (widget);
4033 g_return_if_fail (text_view->layout != NULL);
4034 g_return_if_fail (text_view->xoffset >= 0);
4035 g_return_if_fail (text_view->yoffset >= 0);
4037 while (text_view->first_validate_idle != 0)
4039 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4040 text_view->first_validate_idle));
4041 gtk_text_view_flush_first_validate (text_view);
4044 /* More regions could have become invalid in the above loop */
4045 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4050 gdk_region_get_clipbox (updates, &rect);
4052 gdk_rectangle_union (area, &rect, area);
4054 gdk_region_destroy (updates);
4057 if (!text_view->onscreen_validated)
4059 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.");
4060 g_assert_not_reached ();
4064 printf ("painting %d,%d %d x %d\n",
4066 area->width, area->height);
4069 child_exposes = NULL;
4070 gtk_text_layout_draw (text_view->layout,
4072 text_view->text_window->bin_window,
4077 area->width, area->height,
4080 tmp_list = child_exposes;
4081 while (tmp_list != NULL)
4083 GtkWidget *child = tmp_list->data;
4085 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4089 g_object_unref (G_OBJECT (child));
4091 tmp_list = tmp_list->next;
4094 g_list_free (child_exposes);
4098 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4102 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4103 GTK_TEXT_WINDOW_TEXT))
4105 DV(g_print (">Exposed ("G_STRLOC")\n"));
4106 gtk_text_view_paint (widget, &event->area, event);
4109 if (event->window == widget->window)
4110 gtk_text_view_draw_focus (widget);
4112 /* Propagate exposes to all children not in the buffer. */
4113 tmp_list = GTK_TEXT_VIEW (widget)->children;
4114 while (tmp_list != NULL)
4116 GtkTextViewChild *vc = tmp_list->data;
4118 /* propagate_expose checks that event->window matches
4121 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4122 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4126 tmp_list = tmp_list->next;
4133 gtk_text_view_draw_focus (GtkWidget *widget)
4135 gboolean interior_focus;
4137 /* We clear the focus if we are in interior focus mode. */
4138 gtk_widget_style_get (widget,
4139 "interior_focus", &interior_focus,
4142 if (GTK_WIDGET_DRAWABLE (widget))
4144 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4146 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4147 NULL, widget, "textview",
4149 widget->allocation.width,
4150 widget->allocation.height);
4154 gdk_window_clear (widget->window);
4160 gtk_text_view_grab_focus (GtkWidget *widget)
4162 GtkTextView *text_view;
4164 text_view = GTK_TEXT_VIEW (widget);
4166 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4168 if (!text_view->disable_scroll_on_focus)
4169 gtk_text_view_scroll_mark_onscreen (text_view,
4170 gtk_text_buffer_get_mark (get_buffer (text_view),
4175 gtk_text_view_focus (GtkWidget *widget,
4176 GtkDirectionType direction)
4178 GtkTextView *text_view;
4179 GtkContainer *container;
4181 text_view = GTK_TEXT_VIEW (widget);
4182 container = GTK_CONTAINER (widget);
4184 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4192 gtk_text_view_add (GtkContainer *container,
4195 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4196 g_return_if_fail (GTK_IS_WIDGET (child));
4198 /* This is pretty random. */
4199 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4201 GTK_TEXT_WINDOW_WIDGET,
4206 gtk_text_view_remove (GtkContainer *container,
4210 GtkTextView *text_view;
4211 GtkTextViewChild *vc;
4213 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4214 g_return_if_fail (GTK_IS_WIDGET (child));
4215 g_return_if_fail (child->parent == (GtkWidget*) container);
4217 text_view = GTK_TEXT_VIEW (container);
4220 iter = text_view->children;
4222 while (iter != NULL)
4226 if (vc->widget == child)
4229 iter = g_slist_next (iter);
4232 g_assert (iter != NULL); /* be sure we had the child in the list */
4234 text_view->children = g_slist_remove (text_view->children, vc);
4236 gtk_widget_unparent (vc->widget);
4238 text_view_child_free (vc);
4242 gtk_text_view_forall (GtkContainer *container,
4243 gboolean include_internals,
4244 GtkCallback callback,
4245 gpointer callback_data)
4248 GtkTextView *text_view;
4251 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4252 g_return_if_fail (callback != NULL);
4254 text_view = GTK_TEXT_VIEW (container);
4256 copy = g_slist_copy (text_view->children);
4259 while (iter != NULL)
4261 GtkTextViewChild *vc = iter->data;
4263 (* callback) (vc->widget, callback_data);
4265 iter = g_slist_next (iter);
4268 g_slist_free (copy);
4271 #define CURSOR_ON_MULTIPLIER 0.66
4272 #define CURSOR_OFF_MULTIPLIER 0.34
4273 #define CURSOR_PEND_MULTIPLIER 1.0
4276 cursor_blinks (GtkTextView *text_view)
4278 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4281 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4284 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4287 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4292 get_cursor_time (GtkTextView *text_view)
4294 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4297 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4307 blink_cb (gpointer data)
4309 GtkTextView *text_view;
4312 GDK_THREADS_ENTER ();
4314 text_view = GTK_TEXT_VIEW (data);
4316 g_assert (text_view->layout);
4317 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4318 g_assert (text_view->cursor_visible);
4320 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4323 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4327 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4331 gtk_text_layout_set_cursor_visible (text_view->layout,
4334 GDK_THREADS_LEAVE ();
4336 /* Remove ourselves */
4342 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4344 if (text_view->blink_timeout)
4346 gtk_timeout_remove (text_view->blink_timeout);
4347 text_view->blink_timeout = 0;
4352 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4354 if (text_view->layout != NULL &&
4355 text_view->cursor_visible &&
4356 GTK_WIDGET_HAS_FOCUS (text_view))
4358 if (cursor_blinks (text_view))
4360 if (text_view->blink_timeout == 0)
4362 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4364 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4370 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4374 gtk_text_view_stop_cursor_blink (text_view);
4379 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4381 if (text_view->layout != NULL &&
4382 text_view->cursor_visible &&
4383 GTK_WIDGET_HAS_FOCUS (text_view) &&
4384 cursor_blinks (text_view))
4386 if (text_view->blink_timeout != 0)
4388 gtk_timeout_remove (text_view->blink_timeout);
4389 text_view->blink_timeout = 0;
4392 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4394 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4402 * Key binding handlers
4406 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4407 GtkTextIter *newplace,
4412 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4418 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4424 move_cursor (GtkTextView *text_view,
4425 const GtkTextIter *new_location,
4426 gboolean extend_selection)
4428 if (extend_selection)
4429 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4433 gtk_text_buffer_place_cursor (get_buffer (text_view),
4437 /* FIXME when we are unfrozen and can change GtkMovementStep,
4440 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4443 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4444 GtkMovementStep step,
4446 gboolean extend_selection)
4449 GtkTextIter newplace;
4451 gint cursor_x_pos = 0;
4453 gtk_text_view_reset_im_context (text_view);
4455 if (step == GTK_MOVEMENT_PAGES)
4457 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4458 gtk_text_view_pend_cursor_blink (text_view);
4461 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4463 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4464 gtk_text_view_pend_cursor_blink (text_view);
4468 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4469 gtk_text_buffer_get_mark (get_buffer (text_view),
4473 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4474 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4478 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4479 gtk_text_iter_forward_cursor_positions (&newplace, count);
4482 case GTK_MOVEMENT_VISUAL_POSITIONS:
4483 gtk_text_layout_move_iter_visually (text_view->layout,
4487 case GTK_MOVEMENT_WORDS:
4489 gtk_text_iter_backward_word_starts (&newplace, -count);
4491 gtk_text_iter_forward_word_ends (&newplace, count);
4494 case GTK_MOVEMENT_DISPLAY_LINES:
4495 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4496 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4499 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4501 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4502 else if (count < -1)
4503 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4506 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4509 case GTK_MOVEMENT_PARAGRAPHS:
4512 if (!gtk_text_iter_ends_line (&newplace))
4514 gtk_text_iter_forward_to_line_end (&newplace);
4520 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4522 gtk_text_iter_set_line_offset (&newplace, 0);
4529 gtk_text_iter_forward_lines (&newplace, count);
4530 gtk_text_iter_set_line_offset (&newplace, 0);
4534 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4537 if (!gtk_text_iter_ends_line (&newplace))
4538 gtk_text_iter_forward_to_line_end (&newplace);
4542 gtk_text_iter_set_line_offset (&newplace, 0);
4546 case GTK_MOVEMENT_BUFFER_ENDS:
4548 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4550 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4557 if (!gtk_text_iter_equal (&insert, &newplace))
4559 move_cursor (text_view, &newplace, extend_selection);
4561 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4562 gtk_text_view_scroll_mark_onscreen (text_view,
4563 gtk_text_buffer_get_mark (get_buffer (text_view),
4566 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4568 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4572 gtk_text_view_pend_cursor_blink (text_view);
4576 gtk_text_view_move_cursor (GtkTextView *text_view,
4577 GtkMovementStep step,
4579 gboolean extend_selection)
4581 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4585 gtk_text_view_page_horizontally (GtkTextView *text_view,
4587 gboolean extend_selection)
4589 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4590 count, extend_selection);
4594 gtk_text_view_set_anchor (GtkTextView *text_view)
4598 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4599 gtk_text_buffer_get_mark (get_buffer (text_view),
4602 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4606 gtk_text_view_scroll_pages (GtkTextView *text_view,
4608 gboolean extend_selection)
4613 gint cursor_x_pos, cursor_y_pos;
4614 GtkTextIter new_insert;
4618 g_return_if_fail (text_view->vadjustment != NULL);
4620 adj = text_view->vadjustment;
4622 /* Validate the region that will be brought into view by the cursor motion
4626 gtk_text_view_get_first_para_iter (text_view, &anchor);
4627 y0 = adj->page_size;
4628 y1 = adj->page_size + count * adj->page_increment;
4632 gtk_text_view_get_first_para_iter (text_view, &anchor);
4633 y0 = count * adj->page_increment + adj->page_size;
4637 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4638 /* FIXME do we need to update the adjustment ranges here? */
4640 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4642 /* already at top, just be sure we are at offset 0 */
4643 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4644 move_cursor (text_view, &new_insert, extend_selection);
4646 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4648 /* already at bottom, just be sure we are at the end */
4649 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4650 move_cursor (text_view, &new_insert, extend_selection);
4654 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4656 newval = adj->value;
4657 oldval = adj->value;
4659 newval += count * adj->page_increment;
4661 set_adjustment_clamped (adj, newval);
4662 cursor_y_pos += adj->value - oldval;
4664 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4665 clamp_iter_onscreen (text_view, &new_insert);
4666 move_cursor (text_view, &new_insert, extend_selection);
4668 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4671 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4672 * only guarantees 1 pixel onscreen.
4674 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4675 gtk_text_view_scroll_mark_onscreen (text_view,
4676 gtk_text_buffer_get_mark (get_buffer (text_view),
4681 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4683 gboolean extend_selection)
4688 gint cursor_x_pos, cursor_y_pos;
4689 GtkTextIter new_insert;
4692 g_return_if_fail (text_view->hadjustment != NULL);
4694 adj = text_view->hadjustment;
4696 /* Validate the line that we're moving within.
4698 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4700 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4701 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4702 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4703 /* FIXME do we need to update the adjustment ranges here? */
4705 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4707 /* already at far left, just be sure we are at offset 0 */
4708 gtk_text_iter_set_line_offset (&new_insert, 0);
4709 move_cursor (text_view, &new_insert, extend_selection);
4711 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4713 /* already at far right, just be sure we are at the end */
4714 gtk_text_iter_forward_to_line_end (&new_insert);
4715 move_cursor (text_view, &new_insert, extend_selection);
4719 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4721 newval = adj->value;
4722 oldval = adj->value;
4724 newval += count * adj->page_increment;
4726 set_adjustment_clamped (adj, newval);
4727 cursor_x_pos += adj->value - oldval;
4729 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4730 clamp_iter_onscreen (text_view, &new_insert);
4731 move_cursor (text_view, &new_insert, extend_selection);
4733 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4736 /* FIXME for lines shorter than the overall widget width, this results in a
4737 * "bounce" effect as we scroll to the right of the widget, then scroll
4738 * back to get the end of the line onscreen.
4739 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4742 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4743 * only guarantees 1 pixel onscreen.
4745 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4746 gtk_text_view_scroll_mark_onscreen (text_view,
4747 gtk_text_buffer_get_mark (get_buffer (text_view),
4752 whitespace (gunichar ch, gpointer user_data)
4754 return (ch == ' ' || ch == '\t');
4758 not_whitespace (gunichar ch, gpointer user_data)
4760 return !whitespace (ch, user_data);
4764 find_whitepace_region (const GtkTextIter *center,
4765 GtkTextIter *start, GtkTextIter *end)
4770 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4771 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4772 if (whitespace (gtk_text_iter_get_char (end), NULL))
4773 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4775 return !gtk_text_iter_equal (start, end);
4779 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4782 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4783 text_view->editable);
4787 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4794 gboolean leave_one = FALSE;
4796 gtk_text_view_reset_im_context (text_view);
4798 if (type == GTK_DELETE_CHARS)
4800 /* Char delete deletes the selection, if one exists */
4801 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4802 text_view->editable))
4806 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4808 gtk_text_buffer_get_mark (get_buffer (text_view),
4816 case GTK_DELETE_CHARS:
4817 gtk_text_iter_forward_cursor_positions (&end, count);
4820 case GTK_DELETE_WORD_ENDS:
4822 gtk_text_iter_forward_word_ends (&end, count);
4824 gtk_text_iter_backward_word_starts (&start, 0 - count);
4827 case GTK_DELETE_WORDS:
4830 case GTK_DELETE_DISPLAY_LINE_ENDS:
4833 case GTK_DELETE_DISPLAY_LINES:
4836 case GTK_DELETE_PARAGRAPH_ENDS:
4837 /* If we're already at a newline, we need to
4838 * simply delete that newline, instead of
4839 * moving to the next one.
4841 if (gtk_text_iter_ends_line (&end))
4843 gtk_text_iter_forward_line (&end);
4849 if (!gtk_text_iter_forward_to_line_end (&end))
4855 /* FIXME figure out what a negative count means
4859 case GTK_DELETE_PARAGRAPHS:
4862 gtk_text_iter_set_line_offset (&start, 0);
4863 gtk_text_iter_forward_to_line_end (&end);
4865 /* Do the lines beyond the first. */
4868 gtk_text_iter_forward_to_line_end (&end);
4874 /* FIXME negative count? */
4878 case GTK_DELETE_WHITESPACE:
4880 find_whitepace_region (&insert, &start, &end);
4888 if (!gtk_text_iter_equal (&start, &end))
4890 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4892 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4893 text_view->editable))
4896 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4898 text_view->editable);
4901 gtk_text_buffer_end_user_action (get_buffer (text_view));
4903 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4904 gtk_text_view_scroll_mark_onscreen (text_view,
4905 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4910 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4912 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4913 GDK_SELECTION_CLIPBOARD);
4915 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4917 text_view->editable);
4918 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4919 gtk_text_view_scroll_mark_onscreen (text_view,
4920 gtk_text_buffer_get_mark (get_buffer (text_view),
4925 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4927 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4928 GDK_SELECTION_CLIPBOARD);
4930 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4932 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4933 gtk_text_view_scroll_mark_onscreen (text_view,
4934 gtk_text_buffer_get_mark (get_buffer (text_view),
4939 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4941 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4942 GDK_SELECTION_CLIPBOARD);
4944 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4947 text_view->editable);
4948 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4949 gtk_text_view_scroll_mark_onscreen (text_view,
4950 gtk_text_buffer_get_mark (get_buffer (text_view),
4955 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4957 text_view->overwrite_mode = !text_view->overwrite_mode;
4961 gtk_text_view_move_focus (GtkTextView *text_view,
4962 GtkDirectionType direction_type)
4964 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
4966 if (!GTK_WIDGET_TOPLEVEL (toplevel))
4969 /* Propagate to toplevel */
4970 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus", direction_type);
4978 gtk_text_view_unselect (GtkTextView *text_view)
4982 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4984 gtk_text_buffer_get_mark (get_buffer (text_view),
4987 gtk_text_buffer_move_mark (get_buffer (text_view),
4988 gtk_text_buffer_get_mark (get_buffer (text_view),
4994 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
4995 const gchar *mark_name)
4998 GdkModifierType state;
4999 GtkTextIter newplace;
5001 /* DV(g_print (G_STRLOC": begin\n")); */
5003 gdk_window_get_pointer (text_view->text_window->bin_window,
5006 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5007 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5009 x + text_view->xoffset,
5010 y + text_view->yoffset);
5014 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5016 /* This may invalidate the layout */
5017 DV(g_print (G_STRLOC": move mark\n"));
5018 gtk_text_buffer_move_mark (get_buffer (text_view),
5022 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5023 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5026 DV (g_print ("first validate idle leaving %s is %d\n",
5027 G_STRLOC, text_view->first_validate_idle));
5031 selection_scan_timeout (gpointer data)
5033 GtkTextView *text_view;
5035 GDK_THREADS_ENTER ();
5037 text_view = GTK_TEXT_VIEW (data);
5039 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5040 move_mark_to_pointer_and_scroll (text_view, "insert");
5042 GDK_THREADS_LEAVE ();
5044 return TRUE; /* remain installed. */
5047 #define DND_SCROLL_MARGIN 0.20
5050 drag_scan_timeout (gpointer data)
5052 GtkTextView *text_view;
5054 GdkModifierType state;
5055 GtkTextIter newplace;
5057 GDK_THREADS_ENTER ();
5059 text_view = GTK_TEXT_VIEW (data);
5061 gdk_window_get_pointer (text_view->text_window->bin_window,
5064 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5066 x + text_view->xoffset,
5067 y + text_view->yoffset);
5069 gtk_text_buffer_move_mark (get_buffer (text_view),
5070 text_view->dnd_mark,
5073 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5074 gtk_text_view_scroll_to_mark (text_view,
5075 text_view->dnd_mark,
5076 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5078 GDK_THREADS_LEAVE ();
5084 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5086 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5087 move_mark_to_pointer_and_scroll (text_view, "insert");
5089 /* If we had to scroll offscreen, insert a timeout to do so
5090 * again. Note that in the timeout, even if the mouse doesn't
5091 * move, due to this scroll xoffset/yoffset will have changed
5092 * and we'll need to scroll again.
5094 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5095 gtk_timeout_remove (text_view->scroll_timeout);
5097 text_view->scroll_timeout =
5098 gtk_timeout_add (50, selection_scan_timeout, text_view);
5104 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5105 const GtkTextIter *iter,
5106 GdkEventButton *button)
5108 GtkTextIter newplace;
5109 GtkTextBuffer *buffer;
5111 g_return_if_fail (text_view->selection_drag_handler == 0);
5113 gtk_grab_add (GTK_WIDGET (text_view));
5115 buffer = get_buffer (text_view);
5119 if (button->state & GDK_SHIFT_MASK)
5121 /* Extend selection */
5122 GtkTextIter start, end;
5124 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5126 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5128 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5131 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5134 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5136 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5139 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5145 /* Replace selection */
5146 gtk_text_buffer_place_cursor (buffer, &newplace);
5149 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5150 "motion_notify_event",
5151 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5155 /* returns whether we were really dragging */
5157 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5159 if (text_view->selection_drag_handler == 0)
5162 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5163 text_view->selection_drag_handler = 0;
5165 if (text_view->scroll_timeout != 0)
5167 gtk_timeout_remove (text_view->scroll_timeout);
5168 text_view->scroll_timeout = 0;
5171 /* one last update to current position */
5172 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5173 move_mark_to_pointer_and_scroll (text_view, "insert");
5175 gtk_grab_remove (GTK_WIDGET (text_view));
5185 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5186 GtkTextAttributes *values,
5189 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5190 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5193 pango_font_description_free (values->font);
5195 values->font = pango_font_description_copy (style->font_desc);
5199 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5201 if (text_view->layout)
5203 gboolean split_cursor;
5204 GtkTextDirection new_dir;
5205 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5207 g_object_get (G_OBJECT (settings),
5208 "gtk-split-cursor", &split_cursor,
5212 new_dir = GTK_TEXT_DIR_NONE;
5216 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5217 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5218 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5221 if (text_view->layout->cursor_direction != new_dir)
5222 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5227 gtk_text_view_ensure_layout (GtkTextView *text_view)
5231 widget = GTK_WIDGET (text_view);
5233 if (text_view->layout == NULL)
5235 GtkTextAttributes *style;
5236 PangoContext *ltr_context, *rtl_context;
5239 DV(g_print(G_STRLOC"\n"));
5241 text_view->layout = gtk_text_layout_new ();
5243 g_signal_connect (G_OBJECT (text_view->layout),
5245 G_CALLBACK (invalidated_handler),
5248 g_signal_connect (G_OBJECT (text_view->layout),
5250 G_CALLBACK (changed_handler),
5253 g_signal_connect (G_OBJECT (text_view->layout),
5255 G_CALLBACK (gtk_text_view_child_allocated),
5258 if (get_buffer (text_view))
5259 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5261 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5262 gtk_text_view_pend_cursor_blink (text_view);
5264 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5266 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5267 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5268 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5269 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5271 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5273 g_object_unref (G_OBJECT (ltr_context));
5274 g_object_unref (G_OBJECT (rtl_context));
5276 gtk_text_view_check_keymap_direction (text_view);
5278 style = gtk_text_attributes_new ();
5280 gtk_widget_ensure_style (widget);
5281 gtk_text_view_set_attributes_from_style (text_view,
5282 style, widget->style);
5284 style->pixels_above_lines = text_view->pixels_above_lines;
5285 style->pixels_below_lines = text_view->pixels_below_lines;
5286 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5287 style->left_margin = text_view->left_margin;
5288 style->right_margin = text_view->right_margin;
5289 style->indent = text_view->indent;
5290 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5292 style->wrap_mode = text_view->wrap_mode;
5293 style->justification = text_view->justify;
5294 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5296 gtk_text_layout_set_default_style (text_view->layout, style);
5298 gtk_text_attributes_unref (style);
5300 /* Set layout for all anchored children */
5302 tmp_list = text_view->children;
5303 while (tmp_list != NULL)
5305 GtkTextViewChild *vc = tmp_list->data;
5309 gtk_text_anchored_child_set_layout (vc->widget,
5311 /* vc may now be invalid! */
5314 tmp_list = g_slist_next (tmp_list);
5317 gtk_text_view_invalidate (text_view);
5322 * gtk_text_view_get_default_attributes:
5323 * @text_view: a #GtkTextView
5325 * Obtains a copy of the default text attributes. These are the
5326 * attributes used for text unless a tag overrides them.
5327 * You'd typically pass the default attributes in to
5328 * gtk_text_iter_get_attributes() in order to get the
5329 * attributes in effect at a given text position.
5331 * The return value is a copy owned by the caller of this function,
5332 * and should be freed.
5334 * Return value: a new #GtkTextAttributes
5337 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5339 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5341 gtk_text_view_ensure_layout (text_view);
5343 return gtk_text_attributes_copy (text_view->layout->default_style);
5347 gtk_text_view_destroy_layout (GtkTextView *text_view)
5349 if (text_view->layout)
5353 gtk_text_view_remove_validate_idles (text_view);
5355 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5356 (gpointer) invalidated_handler,
5358 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5359 (gpointer) changed_handler,
5362 /* Remove layout from all anchored children */
5363 tmp_list = text_view->children;
5364 while (tmp_list != NULL)
5366 GtkTextViewChild *vc = tmp_list->data;
5370 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5371 /* vc may now be invalid! */
5374 tmp_list = g_slist_next (tmp_list);
5377 gtk_text_view_stop_cursor_blink (text_view);
5378 gtk_text_view_end_selection_drag (text_view, NULL);
5380 g_object_unref (G_OBJECT (text_view->layout));
5381 text_view->layout = NULL;
5386 gtk_text_view_reset_im_context (GtkTextView *text_view)
5388 if (text_view->need_im_reset)
5390 text_view->need_im_reset = FALSE;
5391 gtk_im_context_reset (text_view->im_context);
5400 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5401 const GtkTextIter *iter,
5402 GdkEventMotion *event)
5404 GdkDragContext *context;
5405 GtkTargetList *target_list;
5407 text_view->drag_start_x = -1;
5408 text_view->drag_start_y = -1;
5409 text_view->pending_place_cursor_button = 0;
5411 target_list = gtk_target_list_new (target_table,
5412 G_N_ELEMENTS (target_table));
5414 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5415 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5416 1, (GdkEvent*)event);
5418 gtk_target_list_unref (target_list);
5420 gtk_drag_set_icon_default (context);
5424 gtk_text_view_drag_begin (GtkWidget *widget,
5425 GdkDragContext *context)
5431 gtk_text_view_drag_end (GtkWidget *widget,
5432 GdkDragContext *context)
5434 GtkTextView *text_view;
5436 text_view = GTK_TEXT_VIEW (widget);
5440 gtk_text_view_drag_data_get (GtkWidget *widget,
5441 GdkDragContext *context,
5442 GtkSelectionData *selection_data,
5446 GtkTextView *text_view;
5448 text_view = GTK_TEXT_VIEW (widget);
5450 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5452 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5454 gtk_selection_data_set (selection_data,
5455 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5468 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5471 /* Extract the selected text */
5472 str = gtk_text_iter_get_visible_text (&start, &end);
5477 gtk_selection_data_set_text (selection_data, str, -1);
5484 gtk_text_view_drag_data_delete (GtkWidget *widget,
5485 GdkDragContext *context)
5487 GtkTextView *text_view;
5489 text_view = GTK_TEXT_VIEW (widget);
5491 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5492 TRUE, GTK_TEXT_VIEW (widget)->editable);
5496 gtk_text_view_drag_leave (GtkWidget *widget,
5497 GdkDragContext *context,
5500 GtkTextView *text_view;
5502 text_view = GTK_TEXT_VIEW (widget);
5504 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5506 if (text_view->scroll_timeout != 0)
5507 gtk_timeout_remove (text_view->scroll_timeout);
5509 text_view->scroll_timeout = 0;
5513 gtk_text_view_drag_motion (GtkWidget *widget,
5514 GdkDragContext *context,
5519 GtkTextIter newplace;
5520 GtkTextView *text_view;
5523 GdkRectangle target_rect;
5525 GdkDragAction suggested_action = 0;
5527 text_view = GTK_TEXT_VIEW (widget);
5529 target_rect = text_view->text_window->allocation;
5531 if (x < target_rect.x ||
5532 y < target_rect.y ||
5533 x > (target_rect.x + target_rect.width) ||
5534 y > (target_rect.y + target_rect.height))
5535 return FALSE; /* outside the text window, allow parent widgets to handle event */
5537 gtk_text_view_window_to_buffer_coords (text_view,
5538 GTK_TEXT_WINDOW_WIDGET,
5542 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5546 if (gtk_drag_dest_find_target (widget, context,
5547 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5549 /* can't accept any of the offered targets */
5551 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5553 gtk_text_iter_in_range (&newplace, &start, &end))
5555 /* We're inside the selection. */
5559 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5561 GtkWidget *source_widget;
5563 suggested_action = context->suggested_action;
5565 source_widget = gtk_drag_get_source_widget (context);
5567 if (source_widget == widget)
5569 /* Default to MOVE, unless the user has
5570 * pressed ctrl or alt to affect available actions
5572 if ((context->actions & GDK_ACTION_MOVE) != 0)
5573 suggested_action = GDK_ACTION_MOVE;
5578 /* Can't drop here. */
5582 if (suggested_action != 0)
5584 gtk_text_mark_set_visible (text_view->dnd_mark,
5585 text_view->cursor_visible);
5587 gdk_drag_status (context, suggested_action, time);
5591 gdk_drag_status (context, 0, time);
5592 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5595 gtk_text_buffer_move_mark (get_buffer (text_view),
5596 text_view->dnd_mark,
5599 DV(g_print (G_STRLOC": scrolling to mark\n"));
5600 gtk_text_view_scroll_to_mark (text_view,
5601 text_view->dnd_mark,
5602 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5604 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5605 gtk_timeout_remove (text_view->scroll_timeout);
5607 text_view->scroll_timeout =
5608 gtk_timeout_add (50, drag_scan_timeout, text_view);
5610 /* TRUE return means don't propagate the drag motion to parent
5611 * widgets that may also be drop sites.
5617 gtk_text_view_drag_drop (GtkWidget *widget,
5618 GdkDragContext *context,
5623 GtkTextView *text_view;
5624 GtkTextIter drop_point;
5625 GdkAtom target = GDK_NONE;
5627 text_view = GTK_TEXT_VIEW (widget);
5629 if (text_view->scroll_timeout != 0)
5630 gtk_timeout_remove (text_view->scroll_timeout);
5632 text_view->scroll_timeout = 0;
5634 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5636 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5638 text_view->dnd_mark);
5640 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5641 target = gtk_drag_dest_find_target (widget, context, NULL);
5643 if (target != GDK_NONE)
5644 gtk_drag_get_data (widget, context, target, time);
5646 gtk_drag_finish (context, FALSE, FALSE, time);
5652 insert_text_data (GtkTextView *text_view,
5653 GtkTextIter *drop_point,
5654 GtkSelectionData *selection_data)
5658 str = gtk_selection_data_get_text (selection_data);
5662 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5663 drop_point, str, -1,
5664 text_view->editable);
5670 gtk_text_view_drag_data_received (GtkWidget *widget,
5671 GdkDragContext *context,
5674 GtkSelectionData *selection_data,
5678 GtkTextIter drop_point;
5679 GtkTextView *text_view;
5680 gboolean success = FALSE;
5682 text_view = GTK_TEXT_VIEW (widget);
5684 if (!text_view->dnd_mark)
5687 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5689 text_view->dnd_mark);
5691 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5694 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5696 GtkTextBuffer *src_buffer = NULL;
5697 GtkTextIter start, end;
5698 gboolean copy_tags = TRUE;
5700 if (selection_data->length != sizeof (src_buffer))
5703 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5705 if (src_buffer == NULL)
5708 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5710 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5711 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5714 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5719 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5723 text_view->editable);
5728 str = gtk_text_iter_get_visible_text (&start, &end);
5729 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5730 &drop_point, str, -1,
5731 text_view->editable);
5737 insert_text_data (text_view, &drop_point, selection_data);
5742 gtk_drag_finish (context, success,
5743 success && context->action == GDK_ACTION_MOVE,
5747 static GtkAdjustment*
5748 get_hadjustment (GtkTextView *text_view)
5750 if (text_view->hadjustment == NULL)
5751 gtk_text_view_set_scroll_adjustments (text_view,
5752 NULL, /* forces creation */
5753 text_view->vadjustment);
5755 return text_view->hadjustment;
5758 static GtkAdjustment*
5759 get_vadjustment (GtkTextView *text_view)
5761 if (text_view->vadjustment == NULL)
5762 gtk_text_view_set_scroll_adjustments (text_view,
5763 text_view->hadjustment,
5764 NULL); /* forces creation */
5765 return text_view->vadjustment;
5770 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5771 GtkAdjustment *hadj,
5772 GtkAdjustment *vadj)
5774 gboolean need_adjust = FALSE;
5776 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5779 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5781 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5783 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5785 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5787 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5789 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5790 g_object_unref (G_OBJECT (text_view->hadjustment));
5793 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5795 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5796 g_object_unref (G_OBJECT (text_view->vadjustment));
5799 if (text_view->hadjustment != hadj)
5801 text_view->hadjustment = hadj;
5802 g_object_ref (G_OBJECT (text_view->hadjustment));
5803 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5805 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5806 (GtkSignalFunc) gtk_text_view_value_changed,
5811 if (text_view->vadjustment != vadj)
5813 text_view->vadjustment = vadj;
5814 g_object_ref (G_OBJECT (text_view->vadjustment));
5815 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5817 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5818 (GtkSignalFunc) gtk_text_view_value_changed,
5824 gtk_text_view_value_changed (NULL, text_view);
5827 /* FIXME this adjust_allocation is a big cut-and-paste from
5828 * GtkCList, needs to be some "official" way to do this
5838 /* The window to which widget->window is relative */
5839 #define ALLOCATION_WINDOW(widget) \
5840 (GTK_WIDGET_NO_WINDOW (widget) ? \
5841 (widget)->window : \
5842 gdk_window_get_parent ((widget)->window))
5845 adjust_allocation_recurse (GtkWidget *widget,
5848 ScrollData *scroll_data = data;
5850 /* Need to really size allocate instead of just poking
5851 * into widget->allocation if the widget is not realized.
5852 * FIXME someone figure out why this was.
5854 if (!GTK_WIDGET_REALIZED (widget))
5856 if (GTK_WIDGET_VISIBLE (widget))
5858 GdkRectangle tmp_rectangle = widget->allocation;
5859 tmp_rectangle.x += scroll_data->dx;
5860 tmp_rectangle.y += scroll_data->dy;
5862 gtk_widget_size_allocate (widget, &tmp_rectangle);
5867 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5869 widget->allocation.x += scroll_data->dx;
5870 widget->allocation.y += scroll_data->dy;
5872 if (GTK_IS_CONTAINER (widget))
5873 gtk_container_forall (GTK_CONTAINER (widget),
5874 adjust_allocation_recurse,
5881 adjust_allocation (GtkWidget *widget,
5885 ScrollData scroll_data;
5887 if (GTK_WIDGET_REALIZED (widget))
5888 scroll_data.window = ALLOCATION_WINDOW (widget);
5890 scroll_data.window = NULL;
5892 scroll_data.dx = dx;
5893 scroll_data.dy = dy;
5895 adjust_allocation_recurse (widget, &scroll_data);
5899 gtk_text_view_value_changed (GtkAdjustment *adj,
5900 GtkTextView *text_view)
5907 /* Note that we oddly call this function with adj == NULL
5911 text_view->onscreen_validated = FALSE;
5913 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5914 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5915 adj ? adj->value : 0.0));
5917 if (adj == text_view->hadjustment)
5919 dx = text_view->xoffset - (gint)adj->value;
5920 text_view->xoffset = adj->value;
5922 else if (adj == text_view->vadjustment)
5924 dy = text_view->yoffset - (gint)adj->value;
5925 text_view->yoffset = adj->value;
5927 if (text_view->layout)
5929 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5931 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5933 text_view->first_para_pixels = adj->value - line_top;
5937 if (dx != 0 || dy != 0)
5941 if (GTK_WIDGET_REALIZED (text_view))
5945 if (text_view->left_window)
5946 text_window_scroll (text_view->left_window, 0, dy);
5947 if (text_view->right_window)
5948 text_window_scroll (text_view->right_window, 0, dy);
5953 if (text_view->top_window)
5954 text_window_scroll (text_view->top_window, dx, 0);
5955 if (text_view->bottom_window)
5956 text_window_scroll (text_view->bottom_window, dx, 0);
5959 /* It looks nicer to scroll the main area last, because
5960 * it takes a while, and making the side areas update
5961 * afterward emphasizes the slowness of scrolling the
5964 text_window_scroll (text_view->text_window, dx, dy);
5967 /* Children are now "moved" in the text window, poke
5968 * into widget->allocation for each child
5970 tmp_list = text_view->children;
5971 while (tmp_list != NULL)
5973 GtkTextViewChild *child = tmp_list->data;
5976 adjust_allocation (child->widget, dx, dy);
5978 tmp_list = g_slist_next (tmp_list);
5982 /* This could result in invalidation, which would install the
5983 * first_validate_idle, which would validate onscreen;
5984 * but we're going to go ahead and validate here, so
5985 * first_validate_idle shouldn't have anything to do.
5987 gtk_text_view_update_layout_width (text_view);
5989 /* note that validation of onscreen could invoke this function
5990 * recursively, by scrolling to maintain first_para, or in response
5991 * to updating the layout width, however there is no problem with
5992 * that, or shouldn't be.
5994 gtk_text_view_validate_onscreen (text_view);
5996 /* process exposes */
5997 if (GTK_WIDGET_REALIZED (text_view))
5999 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6001 if (text_view->left_window)
6002 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6004 if (text_view->right_window)
6005 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6007 if (text_view->top_window)
6008 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6010 if (text_view->bottom_window)
6011 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6013 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6016 /* If this got installed, get rid of it, it's just a waste of time. */
6017 if (text_view->first_validate_idle != 0)
6019 g_source_remove (text_view->first_validate_idle);
6020 text_view->first_validate_idle = 0;
6023 gtk_text_view_update_im_spot_location (text_view);
6025 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6029 gtk_text_view_commit_handler (GtkIMContext *context,
6031 GtkTextView *text_view)
6033 gtk_text_view_commit_text (text_view, str);
6037 gtk_text_view_commit_text (GtkTextView *text_view,
6040 gboolean had_selection;
6042 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6044 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6047 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6048 text_view->editable);
6050 if (!strcmp (str, "\n"))
6052 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6053 text_view->editable);
6057 if (!had_selection && text_view->overwrite_mode)
6061 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6063 gtk_text_buffer_get_mark (get_buffer (text_view),
6065 if (!gtk_text_iter_ends_line (&insert))
6066 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6068 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6069 text_view->editable);
6072 gtk_text_buffer_end_user_action (get_buffer (text_view));
6074 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6075 gtk_text_view_scroll_mark_onscreen (text_view,
6076 gtk_text_buffer_get_mark (get_buffer (text_view),
6081 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6082 GtkTextView *text_view)
6085 PangoAttrList *attrs;
6088 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6089 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6091 pango_attr_list_unref (attrs);
6096 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6097 GtkTextView *text_view)
6104 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6105 gtk_text_buffer_get_insert (text_view->buffer));
6108 pos = gtk_text_iter_get_line_index (&start);
6109 gtk_text_iter_set_line_offset (&start, 0);
6110 gtk_text_iter_forward_to_line_end (&end);
6112 text = gtk_text_iter_get_slice (&start, &end);
6113 gtk_im_context_set_surrounding (context, text, -1, pos);
6120 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6123 GtkTextView *text_view)
6128 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6129 gtk_text_buffer_get_insert (text_view->buffer));
6132 gtk_text_iter_forward_chars (&start, offset);
6133 gtk_text_iter_forward_chars (&end, offset + n_chars);
6135 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6141 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6142 const GtkTextIter *location,
6146 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6147 gboolean need_reset = FALSE;
6149 if (mark == gtk_text_buffer_get_insert (buffer))
6151 text_view->virtual_cursor_x = -1;
6152 text_view->virtual_cursor_y = -1;
6153 gtk_text_view_update_im_spot_location (text_view);
6156 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6162 gtk_text_view_reset_im_context (text_view);
6166 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6170 GdkRectangle strong_pos;
6173 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6174 gtk_text_buffer_get_mark (get_buffer (text_view),
6177 if ((x && text_view->virtual_cursor_x == -1) ||
6178 (y && text_view->virtual_cursor_y == -1))
6179 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6183 if (text_view->virtual_cursor_x != -1)
6184 *x = text_view->virtual_cursor_x;
6191 if (text_view->virtual_cursor_x != -1)
6192 *y = text_view->virtual_cursor_y;
6194 *y = strong_pos.y + strong_pos.height / 2;
6199 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6203 GdkRectangle strong_pos;
6206 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6207 gtk_text_buffer_get_mark (get_buffer (text_view),
6210 if (x == -1 || y == -1)
6211 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6213 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6214 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6217 /* Quick hack of a popup menu
6220 activate_cb (GtkWidget *menuitem,
6221 GtkTextView *text_view)
6223 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6224 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6228 append_action_signal (GtkTextView *text_view,
6230 const gchar *stock_id,
6231 const gchar *signal,
6234 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6236 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6237 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6238 GTK_SIGNAL_FUNC (activate_cb), text_view);
6240 gtk_widget_set_sensitive (menuitem, sensitive);
6242 gtk_widget_show (menuitem);
6243 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6247 popup_menu_detach (GtkWidget *attach_widget,
6250 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6254 popup_position_func (GtkMenu *menu,
6260 GtkTextView *text_view;
6262 GdkRectangle cursor_rect;
6263 GdkRectangle onscreen_rect;
6264 gint root_x, root_y;
6269 text_view = GTK_TEXT_VIEW (user_data);
6270 widget = GTK_WIDGET (text_view);
6272 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6274 screen = gtk_widget_get_screen (widget);
6276 gdk_window_get_origin (widget->window, &root_x, &root_y);
6278 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6280 gtk_text_buffer_get_insert (get_buffer (text_view)));
6282 gtk_text_view_get_iter_location (text_view,
6286 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6288 gtk_widget_size_request (text_view->popup_menu, &req);
6290 /* can't use rectangle_intersect since cursor rect can have 0 width */
6291 if (cursor_rect.x >= onscreen_rect.x &&
6292 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6293 cursor_rect.y >= onscreen_rect.y &&
6294 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6296 gtk_text_view_buffer_to_window_coords (text_view,
6297 GTK_TEXT_WINDOW_WIDGET,
6298 cursor_rect.x, cursor_rect.y,
6299 &cursor_rect.x, &cursor_rect.y);
6301 *x = root_x + cursor_rect.x + cursor_rect.width;
6302 *y = root_y + cursor_rect.y + cursor_rect.height;
6306 /* Just center the menu, since cursor is offscreen. */
6307 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6308 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6312 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6313 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6315 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6316 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6321 GtkTextView *text_view;
6327 range_contains_editable_text (const GtkTextIter *start,
6328 const GtkTextIter *end,
6329 gboolean default_editability)
6331 GtkTextIter iter = *start;
6333 while (gtk_text_iter_compare (&iter, end) < 0)
6335 if (gtk_text_iter_editable (&iter, default_editability))
6338 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6345 unichar_chosen_func (const char *text,
6348 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6350 gtk_text_view_commit_text (text_view, text);
6354 popup_targets_received (GtkClipboard *clipboard,
6355 GtkSelectionData *data,
6358 PopupInfo *info = user_data;
6359 GtkTextView *text_view = info->text_view;
6361 if (GTK_WIDGET_REALIZED (text_view))
6363 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6364 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6366 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6367 GtkWidget *menuitem;
6369 gboolean have_selection;
6370 gboolean can_insert;
6372 GtkTextIter sel_start, sel_end;
6374 if (text_view->popup_menu)
6375 gtk_widget_destroy (text_view->popup_menu);
6377 text_view->popup_menu = gtk_menu_new ();
6379 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6380 GTK_WIDGET (text_view),
6383 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6384 &sel_start, &sel_end);
6386 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6388 gtk_text_buffer_get_insert (get_buffer (text_view)));
6390 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6392 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6394 range_contains_editable_text (&sel_start, &sel_end,
6395 text_view->editable));
6396 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6398 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6399 can_insert && clipboard_contains_text);
6401 menuitem = gtk_separator_menu_item_new ();
6402 gtk_widget_show (menuitem);
6403 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6405 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6406 gtk_widget_show (menuitem);
6407 submenu = gtk_menu_new ();
6408 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6409 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6411 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6412 GTK_MENU_SHELL (submenu));
6414 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6415 gtk_widget_show (menuitem);
6416 gtk_widget_set_sensitive (menuitem, can_insert);
6418 submenu = gtk_menu_new ();
6419 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6420 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6422 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6423 unichar_chosen_func,
6426 gtk_signal_emit (GTK_OBJECT (text_view),
6427 signals[POPULATE_POPUP],
6428 text_view->popup_menu);
6431 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6433 info->button, info->time);
6436 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6437 popup_position_func, text_view,
6438 0, gtk_get_current_event_time ());
6439 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu));
6443 g_object_unref (text_view);
6448 gtk_text_view_do_popup (GtkTextView *text_view,
6449 GdkEventButton *event)
6451 PopupInfo *info = g_new (PopupInfo, 1);
6453 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6454 gtk_text_view_end_selection_drag (text_view, event);
6456 /* In order to know what entries we should make sensitive, we
6457 * ask for the current targets of the clipboard, and when
6458 * we get them, then we actually pop up the menu.
6460 info->text_view = g_object_ref (text_view);
6464 info->button = event->button;
6465 info->time = event->time;
6470 info->time = gtk_get_current_event_time ();
6473 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6474 GDK_SELECTION_CLIPBOARD),
6475 gdk_atom_intern ("TARGETS", FALSE),
6476 popup_targets_received,
6481 gtk_text_view_popup_menu (GtkWidget *widget)
6483 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6487 /* Child GdkWindows */
6490 static GtkTextWindow*
6491 text_window_new (GtkTextWindowType type,
6494 gint height_request)
6498 win = g_new (GtkTextWindow, 1);
6501 win->widget = widget;
6503 win->bin_window = NULL;
6504 win->requisition.width = width_request;
6505 win->requisition.height = height_request;
6506 win->allocation.width = width_request;
6507 win->allocation.height = height_request;
6508 win->allocation.x = 0;
6509 win->allocation.y = 0;
6515 text_window_free (GtkTextWindow *win)
6518 text_window_unrealize (win);
6524 text_window_realize (GtkTextWindow *win,
6527 GdkWindowAttr attributes;
6528 gint attributes_mask;
6531 attributes.window_type = GDK_WINDOW_CHILD;
6532 attributes.x = win->allocation.x;
6533 attributes.y = win->allocation.y;
6534 attributes.width = win->allocation.width;
6535 attributes.height = win->allocation.height;
6536 attributes.wclass = GDK_INPUT_OUTPUT;
6537 attributes.visual = gtk_widget_get_visual (win->widget);
6538 attributes.colormap = gtk_widget_get_colormap (win->widget);
6539 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6541 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6543 win->window = gdk_window_new (parent,
6547 gdk_window_show (win->window);
6548 gdk_window_set_user_data (win->window, win->widget);
6552 attributes.width = win->allocation.width;
6553 attributes.height = win->allocation.height;
6554 attributes.event_mask = (GDK_EXPOSURE_MASK |
6556 GDK_KEY_PRESS_MASK |
6557 GDK_BUTTON_PRESS_MASK |
6558 GDK_BUTTON_RELEASE_MASK |
6559 GDK_POINTER_MOTION_MASK |
6560 GDK_POINTER_MOTION_HINT_MASK |
6561 gtk_widget_get_events (win->widget));
6563 win->bin_window = gdk_window_new (win->window,
6567 gdk_window_show (win->bin_window);
6568 gdk_window_set_user_data (win->bin_window, win->widget);
6570 if (win->type == GTK_TEXT_WINDOW_TEXT)
6573 cursor = gdk_cursor_new_for_screen (gdk_drawable_get_screen (parent),
6575 gdk_window_set_cursor (win->bin_window, cursor);
6576 gdk_cursor_unref (cursor);
6578 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6582 gdk_window_set_background (win->bin_window,
6583 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6587 gdk_window_set_background (win->bin_window,
6588 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6591 g_object_set_qdata (G_OBJECT (win->window),
6592 g_quark_from_static_string ("gtk-text-view-text-window"),
6595 g_object_set_qdata (G_OBJECT (win->bin_window),
6596 g_quark_from_static_string ("gtk-text-view-text-window"),
6601 text_window_unrealize (GtkTextWindow *win)
6603 if (win->type == GTK_TEXT_WINDOW_TEXT)
6605 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6609 gdk_window_set_user_data (win->window, NULL);
6610 gdk_window_set_user_data (win->bin_window, NULL);
6611 gdk_window_destroy (win->bin_window);
6612 gdk_window_destroy (win->window);
6614 win->bin_window = NULL;
6618 text_window_size_allocate (GtkTextWindow *win,
6621 win->allocation = *rect;
6625 gdk_window_move_resize (win->window,
6627 rect->width, rect->height);
6629 gdk_window_resize (win->bin_window,
6630 rect->width, rect->height);
6635 text_window_scroll (GtkTextWindow *win,
6639 if (dx != 0 || dy != 0)
6641 gdk_window_scroll (win->bin_window, dx, dy);
6646 text_window_invalidate_rect (GtkTextWindow *win,
6649 GdkRectangle window_rect;
6651 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6658 window_rect.width = rect->width;
6659 window_rect.height = rect->height;
6661 /* Adjust the rect as appropriate */
6665 case GTK_TEXT_WINDOW_TEXT:
6668 case GTK_TEXT_WINDOW_LEFT:
6669 case GTK_TEXT_WINDOW_RIGHT:
6671 window_rect.width = win->allocation.width;
6674 case GTK_TEXT_WINDOW_TOP:
6675 case GTK_TEXT_WINDOW_BOTTOM:
6677 window_rect.height = win->allocation.height;
6681 g_warning ("%s: bug!", G_STRLOC);
6686 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6690 GdkColor color = { 0, 65535, 0, 0 };
6691 GdkGC *gc = gdk_gc_new (win->bin_window);
6692 gdk_gc_set_rgb_fg_color (gc, &color);
6693 gdk_draw_rectangle (win->bin_window,
6694 gc, TRUE, window_rect.x, window_rect.y,
6695 window_rect.width, window_rect.height);
6702 text_window_get_width (GtkTextWindow *win)
6704 return win->allocation.width;
6708 text_window_get_height (GtkTextWindow *win)
6710 return win->allocation.height;
6714 text_window_get_allocation (GtkTextWindow *win,
6717 *rect = win->allocation;
6724 * gtk_text_view_get_window:
6725 * @text_view: a #GtkTextView
6726 * @win: window to get
6728 * Retrieves the #GdkWindow corresponding to an area of the text view;
6729 * possible windows include the overall widget window, child windows
6730 * on the left, right, top, bottom, and the window that displays the
6731 * text buffer. Windows are %NULL and nonexistent if their width or
6732 * height is 0, and are nonexistent before the widget has been
6735 * Return value: a #GdkWindow, or %NULL
6738 gtk_text_view_get_window (GtkTextView *text_view,
6739 GtkTextWindowType win)
6741 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6745 case GTK_TEXT_WINDOW_WIDGET:
6746 return GTK_WIDGET (text_view)->window;
6749 case GTK_TEXT_WINDOW_TEXT:
6750 return text_view->text_window->bin_window;
6753 case GTK_TEXT_WINDOW_LEFT:
6754 if (text_view->left_window)
6755 return text_view->left_window->bin_window;
6760 case GTK_TEXT_WINDOW_RIGHT:
6761 if (text_view->right_window)
6762 return text_view->right_window->bin_window;
6767 case GTK_TEXT_WINDOW_TOP:
6768 if (text_view->top_window)
6769 return text_view->top_window->bin_window;
6774 case GTK_TEXT_WINDOW_BOTTOM:
6775 if (text_view->bottom_window)
6776 return text_view->bottom_window->bin_window;
6781 case GTK_TEXT_WINDOW_PRIVATE:
6782 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6787 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6792 * gtk_text_view_get_window_type:
6793 * @text_view: a #GtkTextView
6794 * @window: a window type
6796 * Usually used to find out which window an event corresponds to.
6797 * If you connect to an event signal on @text_view, this function
6798 * should be called on <literal>event->window</literal> to
6799 * see which window it was.
6801 * Return value: the window type.
6804 gtk_text_view_get_window_type (GtkTextView *text_view,
6809 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6810 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6812 if (window == GTK_WIDGET (text_view)->window)
6813 return GTK_TEXT_WINDOW_WIDGET;
6815 win = g_object_get_qdata (G_OBJECT (window),
6816 g_quark_try_string ("gtk-text-view-text-window"));
6822 return GTK_TEXT_WINDOW_PRIVATE;
6827 buffer_to_widget (GtkTextView *text_view,
6835 *window_x = buffer_x - text_view->xoffset;
6836 *window_x += text_view->text_window->allocation.x;
6841 *window_y = buffer_y - text_view->yoffset;
6842 *window_y += text_view->text_window->allocation.y;
6847 widget_to_text_window (GtkTextWindow *win,
6854 *window_x = widget_x - win->allocation.x;
6857 *window_y = widget_y - win->allocation.y;
6861 buffer_to_text_window (GtkTextView *text_view,
6870 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6871 "for a nonexistent or private child window of GtkTextView");
6875 buffer_to_widget (text_view,
6877 window_x, window_y);
6879 widget_to_text_window (win,
6880 window_x ? *window_x : 0,
6881 window_y ? *window_y : 0,
6887 * gtk_text_view_buffer_to_window_coords:
6888 * @text_view: a #GtkTextView
6889 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6890 * @buffer_x: buffer x coordinate
6891 * @buffer_y: buffer y coordinate
6892 * @window_x: window x coordinate return location
6893 * @window_y: window y coordinate return location
6895 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6896 * @win, and stores the result in (@window_x, @window_y).
6898 * Note that you can't convert coordinates for a nonexisting window (see
6899 * gtk_text_view_set_border_window_size()).
6902 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6903 GtkTextWindowType win,
6909 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6913 case GTK_TEXT_WINDOW_WIDGET:
6914 buffer_to_widget (text_view,
6916 window_x, window_y);
6919 case GTK_TEXT_WINDOW_TEXT:
6921 *window_x = buffer_x - text_view->xoffset;
6923 *window_y = buffer_y - text_view->yoffset;
6926 case GTK_TEXT_WINDOW_LEFT:
6927 buffer_to_text_window (text_view,
6928 text_view->left_window,
6930 window_x, window_y);
6933 case GTK_TEXT_WINDOW_RIGHT:
6934 buffer_to_text_window (text_view,
6935 text_view->right_window,
6937 window_x, window_y);
6940 case GTK_TEXT_WINDOW_TOP:
6941 buffer_to_text_window (text_view,
6942 text_view->top_window,
6944 window_x, window_y);
6947 case GTK_TEXT_WINDOW_BOTTOM:
6948 buffer_to_text_window (text_view,
6949 text_view->bottom_window,
6951 window_x, window_y);
6954 case GTK_TEXT_WINDOW_PRIVATE:
6955 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6959 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6965 widget_to_buffer (GtkTextView *text_view,
6973 *buffer_x = widget_x + text_view->xoffset;
6974 *buffer_x -= text_view->text_window->allocation.x;
6979 *buffer_y = widget_y + text_view->yoffset;
6980 *buffer_y -= text_view->text_window->allocation.y;
6985 text_window_to_widget (GtkTextWindow *win,
6992 *widget_x = window_x + win->allocation.x;
6995 *widget_y = window_y + win->allocation.y;
6999 text_window_to_buffer (GtkTextView *text_view,
7008 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7009 "coordinates for a nonexistent child window.");
7013 text_window_to_widget (win,
7019 widget_to_buffer (text_view,
7020 buffer_x ? *buffer_x : 0,
7021 buffer_y ? *buffer_y : 0,
7027 * gtk_text_view_window_to_buffer_coords:
7028 * @text_view: a #GtkTextView
7029 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7030 * @window_x: window x coordinate
7031 * @window_y: window y coordinate
7032 * @buffer_x: buffer x coordinate return location
7033 * @buffer_y: buffer y coordinate return location
7035 * Converts coordinates on the window identified by @win to buffer
7036 * coordinates, storing the result in (@buffer_x,@buffer_y).
7038 * Note that you can't convert coordinates for a nonexisting window (see
7039 * gtk_text_view_set_border_window_size()).
7042 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7043 GtkTextWindowType win,
7049 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7053 case GTK_TEXT_WINDOW_WIDGET:
7054 widget_to_buffer (text_view,
7056 buffer_x, buffer_y);
7059 case GTK_TEXT_WINDOW_TEXT:
7061 *buffer_x = window_x + text_view->xoffset;
7063 *buffer_y = window_y + text_view->yoffset;
7066 case GTK_TEXT_WINDOW_LEFT:
7067 text_window_to_buffer (text_view,
7068 text_view->left_window,
7070 buffer_x, buffer_y);
7073 case GTK_TEXT_WINDOW_RIGHT:
7074 text_window_to_buffer (text_view,
7075 text_view->right_window,
7077 buffer_x, buffer_y);
7080 case GTK_TEXT_WINDOW_TOP:
7081 text_window_to_buffer (text_view,
7082 text_view->top_window,
7084 buffer_x, buffer_y);
7087 case GTK_TEXT_WINDOW_BOTTOM:
7088 text_window_to_buffer (text_view,
7089 text_view->bottom_window,
7091 buffer_x, buffer_y);
7094 case GTK_TEXT_WINDOW_PRIVATE:
7095 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7099 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7105 set_window_width (GtkTextView *text_view,
7107 GtkTextWindowType type,
7108 GtkTextWindow **winp)
7114 text_window_free (*winp);
7116 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7123 *winp = text_window_new (type,
7124 GTK_WIDGET (text_view),
7126 /* if the widget is already realized we need to realize the child manually */
7127 if (GTK_WIDGET_REALIZED (text_view))
7128 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7132 if ((*winp)->requisition.width == width)
7135 (*winp)->requisition.width = width;
7138 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7144 set_window_height (GtkTextView *text_view,
7146 GtkTextWindowType type,
7147 GtkTextWindow **winp)
7153 text_window_free (*winp);
7155 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7162 *winp = text_window_new (type,
7163 GTK_WIDGET (text_view),
7166 /* if the widget is already realized we need to realize the child manually */
7167 if (GTK_WIDGET_REALIZED (text_view))
7168 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7172 if ((*winp)->requisition.height == height)
7175 (*winp)->requisition.height = height;
7178 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7183 * gtk_text_view_set_border_window_size:
7184 * @text_view: a #GtkTextView
7185 * @type: window to affect
7186 * @size: width or height of the window
7188 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7189 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7190 * Automatically destroys the corresponding window if the size is set
7191 * to 0, and creates the window if the size is set to non-zero. This
7192 * function can only be used for the "border windows," it doesn't work
7193 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7194 * #GTK_TEXT_WINDOW_PRIVATE.
7197 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7198 GtkTextWindowType type,
7202 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7203 g_return_if_fail (size >= 0);
7207 case GTK_TEXT_WINDOW_LEFT:
7208 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7209 &text_view->left_window);
7212 case GTK_TEXT_WINDOW_RIGHT:
7213 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7214 &text_view->right_window);
7217 case GTK_TEXT_WINDOW_TOP:
7218 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7219 &text_view->top_window);
7222 case GTK_TEXT_WINDOW_BOTTOM:
7223 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7224 &text_view->bottom_window);
7228 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7234 * gtk_text_view_get_border_window_size:
7235 * @text_view: a #GtkTextView
7236 * @type: window to return size from
7238 * Gets the width of the specified border window. See
7239 * gtk_text_view_set_border_window_size().
7241 * Return value: width of window
7244 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7245 GtkTextWindowType type)
7247 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7251 case GTK_TEXT_WINDOW_LEFT:
7252 if (text_view->left_window)
7253 return text_view->left_window->requisition.width;
7255 case GTK_TEXT_WINDOW_RIGHT:
7256 if (text_view->right_window)
7257 return text_view->right_window->requisition.width;
7259 case GTK_TEXT_WINDOW_TOP:
7260 if (text_view->top_window)
7261 return text_view->top_window->requisition.height;
7263 case GTK_TEXT_WINDOW_BOTTOM:
7264 if (text_view->bottom_window)
7265 return text_view->bottom_window->requisition.height;
7268 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7279 static GtkTextViewChild*
7280 text_view_child_new_anchored (GtkWidget *child,
7281 GtkTextChildAnchor *anchor,
7282 GtkTextLayout *layout)
7284 GtkTextViewChild *vc;
7286 vc = g_new (GtkTextViewChild, 1);
7289 vc->anchor = anchor;
7291 vc->from_top_of_line = 0;
7292 vc->from_left_of_buffer = 0;
7294 g_object_ref (G_OBJECT (vc->widget));
7295 g_object_ref (G_OBJECT (vc->anchor));
7297 g_object_set_data (G_OBJECT (child),
7298 "gtk-text-view-child",
7301 gtk_text_child_anchor_register_child (anchor, child, layout);
7306 static GtkTextViewChild*
7307 text_view_child_new_window (GtkWidget *child,
7308 GtkTextWindowType type,
7312 GtkTextViewChild *vc;
7314 vc = g_new (GtkTextViewChild, 1);
7319 vc->from_top_of_line = 0;
7320 vc->from_left_of_buffer = 0;
7322 g_object_ref (G_OBJECT (vc->widget));
7328 g_object_set_data (G_OBJECT (child),
7329 "gtk-text-view-child",
7336 text_view_child_free (GtkTextViewChild *child)
7338 g_object_set_data (G_OBJECT (child->widget),
7339 "gtk-text-view-child", NULL);
7343 gtk_text_child_anchor_unregister_child (child->anchor,
7345 g_object_unref (G_OBJECT (child->anchor));
7348 g_object_unref (G_OBJECT (child->widget));
7354 text_view_child_set_parent_window (GtkTextView *text_view,
7355 GtkTextViewChild *vc)
7358 gtk_widget_set_parent_window (vc->widget,
7359 text_view->text_window->bin_window);
7363 window = gtk_text_view_get_window (text_view,
7365 gtk_widget_set_parent_window (vc->widget, window);
7370 add_child (GtkTextView *text_view,
7371 GtkTextViewChild *vc)
7373 text_view->children = g_slist_prepend (text_view->children,
7376 if (GTK_WIDGET_REALIZED (text_view))
7377 text_view_child_set_parent_window (text_view, vc);
7379 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7383 * gtk_text_view_add_child_at_anchor:
7384 * @text_view: a #GtkTextView
7385 * @child: a #GtkWidget
7386 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7388 * Adds a child widget in the text buffer, at the given @anchor.
7392 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7394 GtkTextChildAnchor *anchor)
7396 GtkTextViewChild *vc;
7398 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7399 g_return_if_fail (GTK_IS_WIDGET (child));
7400 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7401 g_return_if_fail (child->parent == NULL);
7403 gtk_text_view_ensure_layout (text_view);
7405 vc = text_view_child_new_anchored (child, anchor,
7408 add_child (text_view, vc);
7410 g_assert (vc->widget == child);
7411 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7415 * gtk_text_view_add_child_in_window:
7416 * @text_view: a #GtkTextView
7417 * @child: a #GtkWidget
7418 * @which_window: which window the child should appear in
7419 * @xpos: X position of child in window coordinates
7420 * @ypos: Y position of child in window coordinates
7422 * Adds a child at fixed coordinates in one of the text widget's
7423 * windows. The window must have nonzero size (see
7424 * gtk_text_view_set_border_window_size()). Note that the child
7425 * coordinates are given relative to the #GdkWindow in question, and
7426 * that these coordinates have no sane relationship to scrolling. When
7427 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7428 * irrelevant, the child floats above all scrollable areas. But when
7429 * placing a child in one of the scrollable windows (border windows or
7430 * text window), you'll need to compute the child's correct position
7431 * in buffer coordinates any time scrolling occurs or buffer changes
7432 * occur, and then call gtk_text_view_move_child() to update the
7433 * child's position. Unfortunately there's no good way to detect that
7434 * scrolling has occurred, using the current API; a possible hack
7435 * would be to update all child positions when the scroll adjustments
7436 * change or the text buffer changes. See bug 64518 on
7437 * bugzilla.gnome.org for status of fixing this issue.
7441 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7443 GtkTextWindowType which_window,
7447 GtkTextViewChild *vc;
7449 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7450 g_return_if_fail (GTK_IS_WIDGET (child));
7451 g_return_if_fail (child->parent == NULL);
7453 vc = text_view_child_new_window (child, which_window,
7456 add_child (text_view, vc);
7458 g_assert (vc->widget == child);
7459 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7463 * gtk_text_view_move_child:
7464 * @text_view: a #GtkTextView
7465 * @child: child widget already added to the text view
7466 * @xpos: new X position in window coordinates
7467 * @ypos: new Y position in window coordinates
7469 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7473 gtk_text_view_move_child (GtkTextView *text_view,
7478 GtkTextViewChild *vc;
7480 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7481 g_return_if_fail (GTK_IS_WIDGET (child));
7482 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7484 vc = g_object_get_data (G_OBJECT (child),
7485 "gtk-text-view-child");
7487 g_assert (vc != NULL);
7489 if (vc->x == xpos &&
7496 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7497 gtk_widget_queue_resize (child);
7501 /* Iterator operations */
7504 * gtk_text_view_forward_display_line:
7505 * @text_view: a #GtkTextView
7506 * @iter: a #GtkTextIter
7508 * Moves the given @iter forward by one display (wrapped) line. A
7509 * display line is different from a paragraph. Paragraphs are
7510 * separated by newlines or other paragraph separator characters.
7511 * Display lines are created by line-wrapping a paragraph. If
7512 * wrapping is turned off, display lines and paragraphs will be the
7513 * same. Display lines are divided differently for each view, since
7514 * they depend on the view's width; paragraphs are the same in all
7515 * views, since they depend on the contents of the #GtkTextBuffer.
7517 * Return value: %TRUE if @iter was moved and is not on the end iterator
7520 gtk_text_view_forward_display_line (GtkTextView *text_view,
7523 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7524 g_return_val_if_fail (iter != NULL, FALSE);
7526 gtk_text_view_ensure_layout (text_view);
7528 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7532 * gtk_text_view_backward_display_line:
7533 * @text_view: a #GtkTextView
7534 * @iter: a #GtkTextIter
7536 * Moves the given @iter backward by one display (wrapped) line. A
7537 * display line is different from a paragraph. Paragraphs are
7538 * separated by newlines or other paragraph separator characters.
7539 * Display lines are created by line-wrapping a paragraph. If
7540 * wrapping is turned off, display lines and paragraphs will be the
7541 * same. Display lines are divided differently for each view, since
7542 * they depend on the view's width; paragraphs are the same in all
7543 * views, since they depend on the contents of the #GtkTextBuffer.
7545 * Return value: %TRUE if @iter was moved and is not on the end iterator
7548 gtk_text_view_backward_display_line (GtkTextView *text_view,
7551 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7552 g_return_val_if_fail (iter != NULL, FALSE);
7554 gtk_text_view_ensure_layout (text_view);
7556 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7560 * gtk_text_view_forward_display_line_end:
7561 * @text_view: a #GtkTextView
7562 * @iter: a #GtkTextIter
7564 * Moves the given @iter forward to the next display line end. A
7565 * display line is different from a paragraph. Paragraphs are
7566 * separated by newlines or other paragraph separator characters.
7567 * Display lines are created by line-wrapping a paragraph. If
7568 * wrapping is turned off, display lines and paragraphs will be the
7569 * same. Display lines are divided differently for each view, since
7570 * they depend on the view's width; paragraphs are the same in all
7571 * views, since they depend on the contents of the #GtkTextBuffer.
7573 * Return value: %TRUE if @iter was moved and is not on the end iterator
7576 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7579 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7580 g_return_val_if_fail (iter != NULL, FALSE);
7582 gtk_text_view_ensure_layout (text_view);
7584 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7588 * gtk_text_view_backward_display_line_start:
7589 * @text_view: a #GtkTextView
7590 * @iter: a #GtkTextIter
7592 * Moves the given @iter backward to the next display line start. A
7593 * display line is different from a paragraph. Paragraphs are
7594 * separated by newlines or other paragraph separator characters.
7595 * Display lines are created by line-wrapping a paragraph. If
7596 * wrapping is turned off, display lines and paragraphs will be the
7597 * same. Display lines are divided differently for each view, since
7598 * they depend on the view's width; paragraphs are the same in all
7599 * views, since they depend on the contents of the #GtkTextBuffer.
7601 * Return value: %TRUE if @iter was moved and is not on the end iterator
7604 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7607 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7608 g_return_val_if_fail (iter != NULL, FALSE);
7610 gtk_text_view_ensure_layout (text_view);
7612 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7616 * gtk_text_view_starts_display_line:
7617 * @text_view: a #GtkTextView
7618 * @iter: a #GtkTextIter
7620 * Determines whether @iter is at the start of a display line.
7621 * See gtk_text_view_forward_display_line() for an explanation of
7622 * display lines vs. paragraphs.
7624 * Return value: %TRUE if @iter begins a wrapped line
7627 gtk_text_view_starts_display_line (GtkTextView *text_view,
7628 const GtkTextIter *iter)
7630 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7631 g_return_val_if_fail (iter != NULL, FALSE);
7633 gtk_text_view_ensure_layout (text_view);
7635 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7639 * gtk_text_view_move_visually:
7640 * @text_view: a #GtkTextView
7641 * @iter: a #GtkTextIter
7642 * @count: number of lines to move
7644 * Moves @iter up or down by @count display (wrapped) lines.
7645 * See gtk_text_view_forward_display_line() for an explanation of
7646 * display lines vs. paragraphs.
7648 * Return value: %TRUE if @iter moved and is not on the end iterator
7651 gtk_text_view_move_visually (GtkTextView *text_view,
7655 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7656 g_return_val_if_fail (iter != NULL, FALSE);
7658 gtk_text_view_ensure_layout (text_view);
7660 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);