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 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
240 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
242 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
245 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
246 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
247 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
248 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
249 static void gtk_text_view_move_focus (GtkTextView *text_view,
250 GtkDirectionType direction_type);
251 static void gtk_text_view_unselect (GtkTextView *text_view);
253 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
254 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
256 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
257 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
258 GtkTextAttributes *values,
260 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
261 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
262 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
263 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
264 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
265 const GtkTextIter *iter,
266 GdkEventButton *event);
267 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
268 GdkEventButton *event);
269 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
270 const GtkTextIter *iter,
271 GdkEventMotion *event);
272 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
273 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
274 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
276 static void gtk_text_view_value_changed (GtkAdjustment *adj,
278 static void gtk_text_view_commit_handler (GtkIMContext *context,
280 GtkTextView *text_view);
281 static void gtk_text_view_commit_text (GtkTextView *text_view,
283 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
284 GtkTextView *text_view);
285 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
286 GtkTextView *text_view);
287 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
290 GtkTextView *text_view);
292 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
293 const GtkTextIter *location,
296 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
299 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
303 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
304 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
306 static void gtk_text_view_do_popup (GtkTextView *text_view,
307 GdkEventButton *event);
309 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
311 gdouble within_margin,
316 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
317 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
318 static void gtk_text_view_invalidate (GtkTextView *text_view);
319 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
321 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
323 /* Container methods */
324 static void gtk_text_view_add (GtkContainer *container,
326 static void gtk_text_view_remove (GtkContainer *container,
328 static void gtk_text_view_forall (GtkContainer *container,
329 gboolean include_internals,
330 GtkCallback callback,
331 gpointer callback_data);
333 /* FIXME probably need the focus methods. */
335 typedef struct _GtkTextViewChild GtkTextViewChild;
337 struct _GtkTextViewChild
341 GtkTextChildAnchor *anchor;
343 gint from_top_of_line;
344 gint from_left_of_buffer;
346 /* These are ignored if anchor != NULL */
347 GtkTextWindowType type;
352 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
353 GtkTextChildAnchor *anchor,
354 GtkTextLayout *layout);
355 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
356 GtkTextWindowType type,
359 static void text_view_child_free (GtkTextViewChild *child);
360 static void text_view_child_set_parent_window (GtkTextView *text_view,
361 GtkTextViewChild *child);
363 struct _GtkTextWindow
365 GtkTextWindowType type;
368 GdkWindow *bin_window;
369 GtkRequisition requisition;
370 GdkRectangle allocation;
373 static GtkTextWindow *text_window_new (GtkTextWindowType type,
376 gint height_request);
377 static void text_window_free (GtkTextWindow *win);
378 static void text_window_realize (GtkTextWindow *win,
380 static void text_window_unrealize (GtkTextWindow *win);
381 static void text_window_size_allocate (GtkTextWindow *win,
383 static void text_window_scroll (GtkTextWindow *win,
386 static void text_window_invalidate_rect (GtkTextWindow *win,
389 static gint text_window_get_width (GtkTextWindow *win);
390 static gint text_window_get_height (GtkTextWindow *win);
391 static void text_window_get_allocation (GtkTextWindow *win,
399 TARGET_COMPOUND_TEXT,
401 TARGET_TEXT_BUFFER_CONTENTS
404 static GtkTargetEntry target_table[] = {
405 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
406 TARGET_TEXT_BUFFER_CONTENTS },
407 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
408 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
409 { "TEXT", 0, TARGET_TEXT },
410 { "STRING", 0, TARGET_STRING }
413 static GtkContainerClass *parent_class = NULL;
414 static guint signals[LAST_SIGNAL] = { 0 };
417 gtk_text_view_get_type (void)
419 static GtkType our_type = 0;
423 static const GtkTypeInfo our_info =
426 sizeof (GtkTextView),
427 sizeof (GtkTextViewClass),
428 (GtkClassInitFunc) gtk_text_view_class_init,
429 (GtkObjectInitFunc) gtk_text_view_init,
430 /* reserved_1 */ NULL,
431 /* reserved_2 */ NULL,
432 (GtkClassInitFunc) NULL
435 our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
442 add_move_binding (GtkBindingSet *binding_set,
445 GtkMovementStep step,
448 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
450 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
454 GTK_TYPE_BOOL, FALSE);
456 /* Selection-extending version */
457 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
461 GTK_TYPE_BOOL, TRUE);
465 gtk_text_view_class_init (GtkTextViewClass *klass)
467 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
468 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
469 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
470 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
471 GtkBindingSet *binding_set;
473 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
475 /* Default handlers and virtual methods
477 gobject_class->set_property = gtk_text_view_set_property;
478 gobject_class->get_property = gtk_text_view_get_property;
480 object_class->destroy = gtk_text_view_destroy;
481 gobject_class->finalize = gtk_text_view_finalize;
483 widget_class->realize = gtk_text_view_realize;
484 widget_class->unrealize = gtk_text_view_unrealize;
485 widget_class->style_set = gtk_text_view_style_set;
486 widget_class->direction_changed = gtk_text_view_direction_changed;
487 widget_class->size_request = gtk_text_view_size_request;
488 widget_class->size_allocate = gtk_text_view_size_allocate;
489 widget_class->event = gtk_text_view_event;
490 widget_class->key_press_event = gtk_text_view_key_press_event;
491 widget_class->key_release_event = gtk_text_view_key_release_event;
492 widget_class->button_press_event = gtk_text_view_button_press_event;
493 widget_class->button_release_event = gtk_text_view_button_release_event;
494 widget_class->focus_in_event = gtk_text_view_focus_in_event;
495 widget_class->focus_out_event = gtk_text_view_focus_out_event;
496 widget_class->motion_notify_event = gtk_text_view_motion_event;
497 widget_class->expose_event = gtk_text_view_expose_event;
498 widget_class->grab_focus = gtk_text_view_grab_focus;
499 widget_class->focus = gtk_text_view_focus;
501 widget_class->drag_begin = gtk_text_view_drag_begin;
502 widget_class->drag_end = gtk_text_view_drag_end;
503 widget_class->drag_data_get = gtk_text_view_drag_data_get;
504 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
506 widget_class->drag_leave = gtk_text_view_drag_leave;
507 widget_class->drag_motion = gtk_text_view_drag_motion;
508 widget_class->drag_drop = gtk_text_view_drag_drop;
509 widget_class->drag_data_received = gtk_text_view_drag_data_received;
511 widget_class->popup_menu = gtk_text_view_popup_menu;
513 container_class->add = gtk_text_view_add;
514 container_class->remove = gtk_text_view_remove;
515 container_class->forall = gtk_text_view_forall;
517 klass->move_cursor = gtk_text_view_move_cursor;
518 klass->page_horizontally = gtk_text_view_page_horizontally;
519 klass->set_anchor = gtk_text_view_set_anchor;
520 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
521 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
522 klass->cut_clipboard = gtk_text_view_cut_clipboard;
523 klass->copy_clipboard = gtk_text_view_copy_clipboard;
524 klass->paste_clipboard = gtk_text_view_paste_clipboard;
525 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
526 klass->move_focus = gtk_text_view_move_focus;
527 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
533 g_object_class_install_property (gobject_class,
534 PROP_PIXELS_ABOVE_LINES,
535 g_param_spec_int ("pixels_above_lines",
536 _("Pixels Above Lines"),
537 _("Pixels of blank space above paragraphs"),
543 g_object_class_install_property (gobject_class,
544 PROP_PIXELS_BELOW_LINES,
545 g_param_spec_int ("pixels_below_lines",
546 _("Pixels Below Lines"),
547 _("Pixels of blank space below paragraphs"),
553 g_object_class_install_property (gobject_class,
554 PROP_PIXELS_INSIDE_WRAP,
555 g_param_spec_int ("pixels_inside_wrap",
556 _("Pixels Inside Wrap"),
557 _("Pixels of blank space between wrapped lines in a paragraph"),
563 g_object_class_install_property (gobject_class,
565 g_param_spec_boolean ("editable",
567 _("Whether the text can be modified by the user"),
571 g_object_class_install_property (gobject_class,
573 g_param_spec_enum ("wrap_mode",
575 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
580 g_object_class_install_property (gobject_class,
582 g_param_spec_enum ("justification",
584 _("Left, right, or center justification"),
585 GTK_TYPE_JUSTIFICATION,
589 g_object_class_install_property (gobject_class,
591 g_param_spec_int ("left_margin",
593 _("Width of the left margin in pixels"),
599 g_object_class_install_property (gobject_class,
601 g_param_spec_int ("right_margin",
603 _("Width of the right margin in pixels"),
609 g_object_class_install_property (gobject_class,
611 g_param_spec_int ("indent",
613 _("Amount to indent the paragraph, in pixels"),
619 g_object_class_install_property (gobject_class,
621 g_param_spec_boxed ("tabs",
623 _("Custom tabs for this text"),
624 PANGO_TYPE_TAB_ARRAY,
627 g_object_class_install_property (gobject_class,
629 g_param_spec_boolean ("cursor_visible",
631 _("If the insertion cursor is shown"),
640 signals[MOVE_CURSOR] =
641 gtk_signal_new ("move_cursor",
642 GTK_RUN_LAST | GTK_RUN_ACTION,
643 GTK_CLASS_TYPE (object_class),
644 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
645 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
646 GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
648 signals[PAGE_HORIZONTALLY] =
649 gtk_signal_new ("page_horizontally",
650 GTK_RUN_LAST | GTK_RUN_ACTION,
651 GTK_CLASS_TYPE (object_class),
652 GTK_SIGNAL_OFFSET (GtkTextViewClass, page_horizontally),
653 _gtk_marshal_VOID__INT_BOOLEAN,
654 GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_BOOL);
656 signals[SET_ANCHOR] =
657 gtk_signal_new ("set_anchor",
658 GTK_RUN_LAST | GTK_RUN_ACTION,
659 GTK_CLASS_TYPE (object_class),
660 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
661 _gtk_marshal_VOID__VOID,
664 signals[INSERT_AT_CURSOR] =
665 gtk_signal_new ("insert_at_cursor",
666 GTK_RUN_LAST | GTK_RUN_ACTION,
667 GTK_CLASS_TYPE (object_class),
668 GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
669 _gtk_marshal_VOID__STRING,
670 GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
672 signals[DELETE_FROM_CURSOR] =
673 gtk_signal_new ("delete_from_cursor",
674 GTK_RUN_LAST | GTK_RUN_ACTION,
675 GTK_CLASS_TYPE (object_class),
676 GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
677 _gtk_marshal_VOID__ENUM_INT,
678 GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
680 signals[CUT_CLIPBOARD] =
681 gtk_signal_new ("cut_clipboard",
682 GTK_RUN_LAST | GTK_RUN_ACTION,
683 GTK_CLASS_TYPE (object_class),
684 GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
685 _gtk_marshal_VOID__VOID,
688 signals[COPY_CLIPBOARD] =
689 gtk_signal_new ("copy_clipboard",
690 GTK_RUN_LAST | GTK_RUN_ACTION,
691 GTK_CLASS_TYPE (object_class),
692 GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
693 _gtk_marshal_VOID__VOID,
696 signals[PASTE_CLIPBOARD] =
697 gtk_signal_new ("paste_clipboard",
698 GTK_RUN_LAST | GTK_RUN_ACTION,
699 GTK_CLASS_TYPE (object_class),
700 GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
701 _gtk_marshal_VOID__VOID,
704 signals[TOGGLE_OVERWRITE] =
705 gtk_signal_new ("toggle_overwrite",
706 GTK_RUN_LAST | GTK_RUN_ACTION,
707 GTK_CLASS_TYPE (object_class),
708 GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
709 _gtk_marshal_VOID__VOID,
712 signals[MOVE_FOCUS] =
713 gtk_signal_new ("move_focus",
714 GTK_RUN_LAST | GTK_RUN_ACTION,
715 GTK_CLASS_TYPE (object_class),
716 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_focus),
717 _gtk_marshal_VOID__ENUM,
718 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
720 signals[SET_SCROLL_ADJUSTMENTS] =
721 gtk_signal_new ("set_scroll_adjustments",
723 GTK_CLASS_TYPE (object_class),
724 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
725 _gtk_marshal_VOID__OBJECT_OBJECT,
726 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
727 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
729 signals[POPULATE_POPUP] =
730 gtk_signal_new ("populate_popup",
732 GTK_CLASS_TYPE (object_class),
733 GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
734 _gtk_marshal_VOID__OBJECT,
735 GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
741 binding_set = gtk_binding_set_by_class (klass);
743 /* Moving the insertion point */
744 add_move_binding (binding_set, GDK_Right, 0,
745 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
747 add_move_binding (binding_set, GDK_KP_Right, 0,
748 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
750 add_move_binding (binding_set, GDK_Left, 0,
751 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
753 add_move_binding (binding_set, GDK_KP_Left, 0,
754 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
756 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
757 GTK_MOVEMENT_WORDS, 1);
759 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
760 GTK_MOVEMENT_WORDS, 1);
762 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
763 GTK_MOVEMENT_WORDS, -1);
765 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
766 GTK_MOVEMENT_WORDS, 1);
768 add_move_binding (binding_set, GDK_Up, 0,
769 GTK_MOVEMENT_DISPLAY_LINES, -1);
771 add_move_binding (binding_set, GDK_KP_Up, 0,
772 GTK_MOVEMENT_DISPLAY_LINES, -1);
774 add_move_binding (binding_set, GDK_Down, 0,
775 GTK_MOVEMENT_DISPLAY_LINES, 1);
777 add_move_binding (binding_set, GDK_KP_Down, 0,
778 GTK_MOVEMENT_DISPLAY_LINES, 1);
780 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
781 GTK_MOVEMENT_PARAGRAPHS, -1);
783 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
784 GTK_MOVEMENT_PARAGRAPHS, -1);
786 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
787 GTK_MOVEMENT_PARAGRAPHS, 1);
789 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
790 GTK_MOVEMENT_PARAGRAPHS, 1);
792 add_move_binding (binding_set, GDK_Home, 0,
793 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
795 add_move_binding (binding_set, GDK_KP_Home, 0,
796 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
798 add_move_binding (binding_set, GDK_End, 0,
799 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
801 add_move_binding (binding_set, GDK_KP_End, 0,
802 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
804 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
805 GTK_MOVEMENT_BUFFER_ENDS, -1);
807 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
808 GTK_MOVEMENT_BUFFER_ENDS, -1);
810 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
811 GTK_MOVEMENT_BUFFER_ENDS, 1);
813 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
814 GTK_MOVEMENT_BUFFER_ENDS, 1);
816 add_move_binding (binding_set, GDK_Page_Up, 0,
817 GTK_MOVEMENT_PAGES, -1);
819 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
820 GTK_MOVEMENT_PAGES, -1);
822 add_move_binding (binding_set, GDK_Page_Down, 0,
823 GTK_MOVEMENT_PAGES, 1);
825 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
826 GTK_MOVEMENT_PAGES, 1);
830 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
832 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
834 GTK_TYPE_BOOL, FALSE);
835 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
837 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
839 GTK_TYPE_BOOL, TRUE);
842 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
843 "page_horizontally", 2,
845 GTK_TYPE_BOOL, FALSE);
847 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
848 "page_horizontally", 2,
850 GTK_TYPE_BOOL, TRUE);
852 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
853 "page_horizontally", 2,
855 GTK_TYPE_BOOL, FALSE);
857 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
858 "page_horizontally", 2,
860 GTK_TYPE_BOOL, TRUE);
862 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
863 "page_horizontally", 2,
865 GTK_TYPE_BOOL, FALSE);
867 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
868 "page_horizontally", 2,
870 GTK_TYPE_BOOL, TRUE);
872 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
873 "page_horizontally", 2,
875 GTK_TYPE_BOOL, FALSE);
877 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
878 "page_horizontally", 2,
880 GTK_TYPE_BOOL, TRUE);
883 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
884 "delete_from_cursor", 2,
885 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
888 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
889 "delete_from_cursor", 2,
890 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
893 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
894 "delete_from_cursor", 2,
895 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
898 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
899 "delete_from_cursor", 2,
900 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
903 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
904 "delete_from_cursor", 2,
905 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
908 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
909 "delete_from_cursor", 2,
910 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
915 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
918 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
919 "copy_clipboard", 0);
921 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
922 "paste_clipboard", 0);
925 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
926 "toggle_overwrite", 0);
927 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
928 "toggle_overwrite", 0);
930 /* Control-tab focus motion */
931 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
933 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
934 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
936 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
938 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
940 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
941 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
943 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
947 gtk_text_view_init (GtkTextView *text_view)
951 widget = GTK_WIDGET (text_view);
953 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
955 /* Set up default style */
956 text_view->wrap_mode = GTK_WRAP_NONE;
957 text_view->pixels_above_lines = 0;
958 text_view->pixels_below_lines = 0;
959 text_view->pixels_inside_wrap = 0;
960 text_view->justify = GTK_JUSTIFY_LEFT;
961 text_view->left_margin = 0;
962 text_view->right_margin = 0;
963 text_view->indent = 0;
964 text_view->tabs = NULL;
965 text_view->editable = TRUE;
967 gtk_drag_dest_set (widget,
969 target_table, G_N_ELEMENTS (target_table),
970 GDK_ACTION_COPY | GDK_ACTION_MOVE);
972 text_view->virtual_cursor_x = -1;
973 text_view->virtual_cursor_y = -1;
975 /* This object is completely private. No external entity can gain a reference
976 * to it; so we create it here and destroy it in finalize ().
978 text_view->im_context = gtk_im_multicontext_new ();
980 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
981 G_CALLBACK (gtk_text_view_commit_handler), text_view);
982 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
983 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
984 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
985 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
986 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
987 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
989 text_view->cursor_visible = TRUE;
991 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
994 text_view->drag_start_x = -1;
995 text_view->drag_start_y = -1;
997 text_view->pending_place_cursor_button = 0;
999 /* We handle all our own redrawing */
1000 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1004 * gtk_text_view_new:
1006 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1007 * before using the text view, an empty default buffer will be created
1008 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1009 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1011 * Return value: a new #GtkTextView
1014 gtk_text_view_new (void)
1016 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1020 * gtk_text_view_new_with_buffer:
1021 * @buffer: a #GtkTextBuffer
1023 * Creates a new #GtkTextView widget displaying the buffer
1024 * @buffer. One buffer can be shared among many widgets.
1025 * @buffer may be NULL to create a default buffer, in which case
1026 * this function is equivalent to gtk_text_view_new(). The
1027 * text view adds its own reference count to the buffer; it does not
1028 * take over an existing reference.
1030 * Return value: a new #GtkTextView.
1033 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1035 GtkTextView *text_view;
1037 text_view = (GtkTextView*)gtk_text_view_new ();
1039 gtk_text_view_set_buffer (text_view, buffer);
1041 return GTK_WIDGET (text_view);
1045 * gtk_text_view_set_buffer:
1046 * @text_view: a #GtkTextView
1047 * @buffer: a #GtkTextBuffer
1049 * Sets @buffer as the buffer being displayed by @text_view. The previous
1050 * buffer displayed by the text view is unreferenced, and a reference is
1051 * added to @buffer. If you owned a reference to @buffer before passing it
1052 * to this function, you must remove that reference yourself; #GtkTextView
1053 * will not "adopt" it.
1057 gtk_text_view_set_buffer (GtkTextView *text_view,
1058 GtkTextBuffer *buffer)
1060 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1061 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1063 if (text_view->buffer == buffer)
1066 if (text_view->buffer != NULL)
1068 /* Destroy all anchored children */
1072 copy = g_slist_copy (text_view->children);
1074 while (tmp_list != NULL)
1076 GtkTextViewChild *vc = tmp_list->data;
1080 gtk_widget_destroy (vc->widget);
1081 /* vc may now be invalid! */
1084 tmp_list = g_slist_next (tmp_list);
1087 g_slist_free (copy);
1089 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1090 gtk_text_view_mark_set_handler, text_view);
1091 g_object_unref (G_OBJECT (text_view->buffer));
1092 text_view->dnd_mark = NULL;
1094 if (GTK_WIDGET_REALIZED (text_view))
1095 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
1096 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1099 text_view->buffer = buffer;
1105 g_object_ref (G_OBJECT (buffer));
1107 if (text_view->layout)
1108 gtk_text_layout_set_buffer (text_view->layout, buffer);
1110 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1112 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1116 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1120 text_view->first_para_pixels = 0;
1122 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1123 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1125 if (GTK_WIDGET_REALIZED (text_view))
1126 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
1127 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1130 if (GTK_WIDGET_VISIBLE (text_view))
1131 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1133 DV(g_print ("Invalidating due to set_buffer\n"));
1134 gtk_text_view_invalidate (text_view);
1137 static GtkTextBuffer*
1138 get_buffer (GtkTextView *text_view)
1140 if (text_view->buffer == NULL)
1143 b = gtk_text_buffer_new (NULL);
1144 gtk_text_view_set_buffer (text_view, b);
1145 g_object_unref (G_OBJECT (b));
1148 return text_view->buffer;
1152 * gtk_text_view_get_buffer:
1153 * @text_view: a #GtkTextView
1155 * Returns the #GtkTextBuffer being displayed by this text view.
1156 * The reference count on the buffer is not incremented; the caller
1157 * of this function won't own a new reference.
1159 * Return value: a #GtkTextBuffer
1162 gtk_text_view_get_buffer (GtkTextView *text_view)
1164 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1166 return get_buffer (text_view);
1170 * gtk_text_view_get_iter_at_location:
1171 * @text_view: a #GtkTextView
1172 * @iter: a #GtkTextIter
1173 * @x: x position, in buffer coordinates
1174 * @y: y position, in buffer coordinates
1176 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1177 * coordinates are coordinates for the entire buffer, not just the
1178 * currently-displayed portion. If you have coordinates from an
1179 * event, you have to convert those to buffer coordinates with
1180 * gtk_text_view_window_to_buffer_coords().
1184 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1189 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1190 g_return_if_fail (iter != NULL);
1191 g_return_if_fail (text_view->layout != NULL);
1193 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1200 * gtk_text_view_get_iter_location:
1201 * @text_view: a #GtkTextView
1202 * @iter: a #GtkTextIter
1203 * @location: bounds of the character at @iter
1205 * Gets a rectangle which roughly contains the character at @iter.
1206 * The rectangle position is in buffer coordinates; use
1207 * gtk_text_view_buffer_to_window_coords() to convert these
1208 * coordinates to coordinates for one of the windows in the text view.
1212 gtk_text_view_get_iter_location (GtkTextView *text_view,
1213 const GtkTextIter *iter,
1214 GdkRectangle *location)
1216 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1217 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1219 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1223 * gtk_text_view_get_line_yrange:
1224 * @text_view: a #GtkTextView
1225 * @iter: a #GtkTextIter
1226 * @y: return location for a y coordinate
1227 * @height: return location for a height
1229 * Gets the y coordinate of the top of the line containing @iter,
1230 * and the height of the line. The coordinate is a buffer coordinate;
1231 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1235 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1236 const GtkTextIter *iter,
1240 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1241 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1243 gtk_text_layout_get_line_yrange (text_view->layout,
1250 * gtk_text_view_get_line_at_y:
1251 * @text_view: a #GtkTextView
1252 * @target_iter: a #GtkTextIter
1253 * @y: a y coordinate
1254 * @line_top: return location for top coordinate of the line
1256 * Gets the #GtkTextIter at the start of the line containing
1257 * the coordinate @y. @y is in buffer coordinates, convert from
1258 * window coordinates with gtk_text_view_window_to_buffer_coords().
1259 * If non-%NULL, @line_top will be filled with the coordinate of the top
1263 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1264 GtkTextIter *target_iter,
1268 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1270 gtk_text_layout_get_line_at_y (text_view->layout,
1277 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1279 DV (g_print (" Setting adj to raw value %g\n", val));
1281 /* We don't really want to clamp to upper; we want to clamp to
1282 upper - page_size which is the highest value the scrollbar
1283 will let us reach. */
1284 if (val > (adj->upper - adj->page_size))
1285 val = adj->upper - adj->page_size;
1287 if (val < adj->lower)
1290 if (val != adj->value)
1292 DV (g_print (" Setting adj to clamped value %g\n", val));
1293 gtk_adjustment_set_value (adj, val);
1301 * gtk_text_view_scroll_to_iter:
1302 * @text_view: a #GtkTextView
1303 * @iter: a #GtkTextIter
1304 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1305 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1306 * @xalign: horizontal alignment of mark within visible area.
1307 * @yalign: vertical alignment of mark within visible area
1309 * Scrolls @text_view so that @iter is on the screen in the position
1310 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1311 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1312 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1313 * possibly not scrolling at all. The effective screen for purposes
1314 * of this function is reduced by a margin of size @within_margin.
1315 * NOTE: This function uses the currently-computed height of the
1316 * lines in the text buffer. Note that line heights are computed
1317 * in an idle handler; so this function may not have the desired effect
1318 * if it's called before the height computations. To avoid oddness,
1319 * consider using gtk_text_view_scroll_to_mark() which saves a point
1320 * to be scrolled to after line validation.
1322 * Return value: %TRUE if scrolling occurred
1325 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1327 gdouble within_margin,
1333 GdkRectangle screen;
1338 gboolean retval = FALSE;
1340 gint screen_xoffset, screen_yoffset;
1341 gint current_x_scroll, current_y_scroll;
1343 /* FIXME why don't we do the validate-at-scroll-destination thing
1344 * from flush_scroll in this function? I think it wasn't done before
1345 * because changed_handler was screwed up, but I could be wrong.
1348 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1349 g_return_val_if_fail (iter != NULL, FALSE);
1350 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1351 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1352 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1354 widget = GTK_WIDGET (text_view);
1356 DV(g_print(G_STRLOC"\n"));
1358 gtk_text_layout_get_iter_location (text_view->layout,
1362 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1364 current_x_scroll = text_view->xoffset;
1365 current_y_scroll = text_view->yoffset;
1367 screen.x = current_x_scroll;
1368 screen.y = current_y_scroll;
1369 screen.width = SCREEN_WIDTH (widget);
1370 screen.height = SCREEN_HEIGHT (widget);
1372 screen_xoffset = screen.width * within_margin;
1373 screen_yoffset = screen.height * within_margin;
1375 screen.x += screen_xoffset;
1376 screen.y += screen_yoffset;
1377 screen.width -= screen_xoffset * 2;
1378 screen.height -= screen_yoffset * 2;
1380 /* paranoia check */
1381 if (screen.width < 1)
1383 if (screen.height < 1)
1386 screen_right = screen.x + screen.width;
1387 screen_bottom = screen.y + screen.height;
1389 /* The alignment affects the point in the target character that we
1390 * choose to align. If we're doing right/bottom alignment, we align
1391 * the right/bottom edge of the character the mark is at; if we're
1392 * doing left/top we align the left/top edge of the character; if
1393 * we're doing center alignment we align the center of the
1397 /* Vertical scroll */
1400 scroll_dest = current_y_scroll;
1404 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1406 /* if scroll_dest < screen.y, we move a negative increment (up),
1407 * else a positive increment (down)
1409 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1413 /* move minimum to get onscreen */
1414 if (rect.y < screen.y)
1416 scroll_dest = rect.y;
1417 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1419 else if ((rect.y + rect.height) > screen_bottom)
1421 scroll_dest = rect.y + rect.height;
1422 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1426 if (scroll_inc != 0)
1428 retval = set_adjustment_clamped (get_vadjustment (text_view),
1429 current_y_scroll + scroll_inc);
1431 DV (g_print (" vert increment %d\n", scroll_inc));
1434 /* Horizontal scroll */
1437 scroll_dest = current_x_scroll;
1441 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1443 /* if scroll_dest < screen.y, we move a negative increment (left),
1444 * else a positive increment (right)
1446 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1450 /* move minimum to get onscreen */
1451 if (rect.x < screen.x)
1453 scroll_dest = rect.x;
1454 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1456 else if ((rect.x + rect.width) > screen_right)
1458 scroll_dest = rect.x + rect.width;
1459 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1463 if (scroll_inc != 0)
1465 retval = set_adjustment_clamped (get_hadjustment (text_view),
1466 current_x_scroll + scroll_inc);
1468 DV (g_print (" horiz increment %d\n", scroll_inc));
1472 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1474 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1480 free_pending_scroll (GtkTextPendingScroll *scroll)
1482 if (!gtk_text_mark_get_deleted (scroll->mark))
1483 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1485 g_object_unref (G_OBJECT (scroll->mark));
1490 gtk_text_view_queue_scroll (GtkTextView *text_view,
1492 gdouble within_margin,
1498 GtkTextPendingScroll *scroll;
1500 DV(g_print(G_STRLOC"\n"));
1502 scroll = g_new (GtkTextPendingScroll, 1);
1504 scroll->within_margin = within_margin;
1505 scroll->use_align = use_align;
1506 scroll->xalign = xalign;
1507 scroll->yalign = yalign;
1509 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1511 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1514 gtk_text_mark_get_left_gravity (mark));
1516 g_object_ref (G_OBJECT (scroll->mark));
1518 if (text_view->pending_scroll)
1519 free_pending_scroll (text_view->pending_scroll);
1521 text_view->pending_scroll = scroll;
1525 gtk_text_view_flush_scroll (GtkTextView *text_view)
1528 GtkTextPendingScroll *scroll;
1532 widget = GTK_WIDGET (text_view);
1534 DV(g_print(G_STRLOC"\n"));
1536 if (text_view->pending_scroll == NULL)
1538 DV (g_print ("in flush scroll, no pending scroll\n"));
1542 scroll = text_view->pending_scroll;
1544 /* avoid recursion */
1545 text_view->pending_scroll = NULL;
1547 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1549 /* Validate area around the scroll destination, so the adjustment
1550 * can meaningfully point into that area. We must validate
1551 * enough area to be sure that after we scroll, everything onscreen
1552 * is valid; otherwise, validation will maintain the first para
1553 * in one place, but may push the target iter off the bottom of
1556 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1557 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1558 - (widget->allocation.height * 2),
1559 widget->allocation.height * 2);
1561 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1563 /* Ensure we have updated width/height */
1564 gtk_text_view_update_adjustments (text_view);
1566 retval = gtk_text_view_scroll_to_iter (text_view,
1568 scroll->within_margin,
1573 free_pending_scroll (scroll);
1579 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1581 if (upper != adj->upper)
1583 gdouble min = MAX (0.0, upper - adj->page_size);
1584 gboolean value_changed = FALSE;
1588 if (adj->value > min)
1591 value_changed = TRUE;
1594 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1595 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1599 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1600 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1606 gtk_text_view_update_adjustments (GtkTextView *text_view)
1608 gint width = 0, height = 0;
1610 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1612 if (text_view->layout)
1613 gtk_text_layout_get_size (text_view->layout, &width, &height);
1615 if (text_view->width != width || text_view->height != height)
1617 text_view->width = width;
1618 text_view->height = height;
1620 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1621 MAX (SCREEN_WIDTH (text_view), width));
1622 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1623 MAX (SCREEN_HEIGHT (text_view), height));
1625 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1627 /* Set up the step sizes; we'll say that a page is
1628 our allocation minus one step, and a step is
1629 1/10 of our allocation. */
1630 text_view->hadjustment->step_increment =
1631 SCREEN_WIDTH (text_view) / 10.0;
1632 text_view->hadjustment->page_increment =
1633 SCREEN_WIDTH (text_view) * 0.9;
1635 text_view->vadjustment->step_increment =
1636 SCREEN_HEIGHT (text_view) / 10.0;
1637 text_view->vadjustment->page_increment =
1638 SCREEN_HEIGHT (text_view) * 0.9;
1640 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1641 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1646 gtk_text_view_update_layout_width (GtkTextView *text_view)
1648 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1650 gtk_text_view_ensure_layout (text_view);
1652 gtk_text_layout_set_screen_width (text_view->layout,
1653 SCREEN_WIDTH (text_view));
1657 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1660 gint cursor_x_pos, cursor_y_pos;
1662 if (text_view->layout == NULL)
1665 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1667 area.x = cursor_x_pos;
1668 area.y = cursor_y_pos;
1669 area.width = area.height = 0;
1671 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1675 * gtk_text_view_scroll_to_mark:
1676 * @text_view: a #GtkTextView
1677 * @mark: a #GtkTextMark
1678 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1679 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1680 * @xalign: horizontal alignment of mark within visible area.
1681 * @yalign: vertical alignment of mark within visible area
1683 * Scrolls @text_view so that @mark is on the screen in the position
1684 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1685 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1686 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1687 * possibly not scrolling at all. The effective screen for purposes
1688 * of this function is reduced by a margin of size @within_margin.
1692 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1694 gdouble within_margin,
1699 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1700 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1701 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1702 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1703 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1705 gtk_text_view_queue_scroll (text_view, mark,
1711 /* If no validation is pending, we need to go ahead and force an
1714 if (text_view->layout &&
1715 gtk_text_layout_is_valid (text_view->layout))
1716 gtk_text_view_flush_scroll (text_view);
1720 * gtk_text_view_scroll_mark_onscreen:
1721 * @text_view: a #GtkTextView
1722 * @mark: a mark in the buffer for @text_view
1724 * Scrolls @text_view the minimum distance such that @mark is contained
1725 * within the visible area of the widget.
1729 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1732 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1733 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1735 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1739 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1741 GdkRectangle visible_rect;
1742 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1744 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1746 visible_rect.y + visible_rect.height);
1750 * gtk_text_view_move_mark_onscreen:
1751 * @text_view: a #GtkTextView
1752 * @mark: a #GtkTextMark
1754 * Moves a mark within the buffer so that it's
1755 * located within the currently-visible text area.
1757 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1760 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1765 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1766 g_return_val_if_fail (mark != NULL, FALSE);
1768 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1770 if (clamp_iter_onscreen (text_view, &iter))
1772 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1780 * gtk_text_view_get_visible_rect:
1781 * @text_view: a #GtkTextView
1782 * @visible_rect: rectangle to fill
1784 * Fills @visible_rect with the currently-visible
1785 * region of the buffer, in buffer coordinates. Convert to window coordinates
1786 * with gtk_text_view_buffer_to_window_coords().
1789 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1790 GdkRectangle *visible_rect)
1794 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1796 widget = GTK_WIDGET (text_view);
1800 visible_rect->x = text_view->xoffset;
1801 visible_rect->y = text_view->yoffset;
1802 visible_rect->width = SCREEN_WIDTH (widget);
1803 visible_rect->height = SCREEN_HEIGHT (widget);
1805 DV(g_print(" visible rect: %d,%d %d x %d\n",
1808 visible_rect->width,
1809 visible_rect->height));
1814 * gtk_text_view_set_wrap_mode:
1815 * @text_view: a #GtkTextView
1816 * @wrap_mode: a #GtkWrapMode
1818 * Sets the line wrapping for the view.
1821 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1822 GtkWrapMode wrap_mode)
1824 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1826 if (text_view->wrap_mode != wrap_mode)
1828 text_view->wrap_mode = wrap_mode;
1830 if (text_view->layout)
1832 text_view->layout->default_style->wrap_mode = wrap_mode;
1833 gtk_text_layout_default_style_changed (text_view->layout);
1837 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1841 * gtk_text_view_get_wrap_mode:
1842 * @text_view: a #GtkTextView
1844 * Gets the line wrapping for the view.
1846 * Return value: the line wrap setting
1849 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1851 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1853 return text_view->wrap_mode;
1857 * gtk_text_view_set_editable:
1858 * @text_view: a #GtkTextView
1859 * @setting: whether it's editable
1861 * Sets the default editability of the #GtkTextView. You can override
1862 * this default setting with tags in the buffer, using the "editable"
1863 * attribute of tags.
1866 gtk_text_view_set_editable (GtkTextView *text_view,
1869 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1870 setting = setting != FALSE;
1872 if (text_view->editable != setting)
1874 text_view->editable = setting;
1876 if (text_view->layout)
1878 text_view->layout->default_style->editable = text_view->editable;
1879 gtk_text_layout_default_style_changed (text_view->layout);
1882 g_object_notify (G_OBJECT (text_view), "editable");
1887 * gtk_text_view_get_editable:
1888 * @text_view: a #GtkTextView
1890 * Returns the default editability of the #GtkTextView. Tags in the
1891 * buffer may override this setting for some ranges of text.
1893 * Return value: whether text is editable by default
1896 gtk_text_view_get_editable (GtkTextView *text_view)
1898 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1900 return text_view->editable;
1904 * gtk_text_view_set_pixels_above_lines:
1905 * @text_view: a #GtkTextView
1906 * @pixels_above_lines: pixels above paragraphs
1908 * Sets the default number of blank pixels above paragraphs in @text_view.
1909 * Tags in the buffer for @text_view may override the defaults.
1913 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1914 gint pixels_above_lines)
1916 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1918 if (text_view->pixels_above_lines != pixels_above_lines)
1920 text_view->pixels_above_lines = pixels_above_lines;
1922 if (text_view->layout)
1924 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1925 gtk_text_layout_default_style_changed (text_view->layout);
1928 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1933 * gtk_text_view_get_pixels_above_lines:
1934 * @text_view: a #GtkTextView
1936 * Gets the default number of pixels to put above paragraphs.
1938 * Return value: default number of pixels above paragraphs
1941 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1943 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1945 return text_view->pixels_above_lines;
1949 * gtk_text_view_set_pixels_below_lines:
1950 * @text_view: a #GtkTextView
1951 * @pixels_below_lines: pixels below paragraphs
1953 * Sets the default number of pixels of blank space
1954 * to put below paragraphs in @text_view. May be overridden
1955 * by tags applied to @text_view's buffer.
1959 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1960 gint pixels_below_lines)
1962 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1964 if (text_view->pixels_below_lines != pixels_below_lines)
1966 text_view->pixels_below_lines = pixels_below_lines;
1968 if (text_view->layout)
1970 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
1971 gtk_text_layout_default_style_changed (text_view->layout);
1974 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
1979 * gtk_text_view_get_pixels_below_lines:
1980 * @text_view: a #GtkTextView
1982 * Gets the value set by gtk_text_view_set_pixels_below_lines().
1984 * Return value: default number of blank pixels below paragraphs
1987 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
1989 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1991 return text_view->pixels_below_lines;
1995 * gtk_text_view_set_pixels_inside_wrap:
1996 * @text_view: a #GtkTextView
1997 * @pixels_inside_wrap: default number of pixels between wrapped lines
1999 * Sets the default number of pixels of blank space to leave between
2000 * display/wrapped lines within a paragraph. May be overridden by
2001 * tags in @text_view's buffer.
2005 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2006 gint pixels_inside_wrap)
2008 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2010 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2012 text_view->pixels_inside_wrap = pixels_inside_wrap;
2014 if (text_view->layout)
2016 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2017 gtk_text_layout_default_style_changed (text_view->layout);
2020 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2025 * gtk_text_view_get_pixels_inside_wrap:
2026 * @text_view: a #GtkTextView
2028 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2030 * Return value: default number of pixels of blank space between wrapped lines
2033 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2035 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2037 return text_view->pixels_inside_wrap;
2041 * gtk_text_view_set_justification:
2042 * @text_view: a #GtkTextView
2043 * @justification: justification
2045 * Sets the default justification of text in @text_view.
2046 * Tags in the view's buffer may override the default.
2050 gtk_text_view_set_justification (GtkTextView *text_view,
2051 GtkJustification justification)
2053 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2055 if (text_view->justify != justification)
2057 text_view->justify = justification;
2059 if (text_view->layout)
2061 text_view->layout->default_style->justification = justification;
2062 gtk_text_layout_default_style_changed (text_view->layout);
2065 g_object_notify (G_OBJECT (text_view), "justification");
2070 * gtk_text_view_get_justification:
2071 * @text_view: a #GtkTextView
2073 * Gets the default justification of paragraphs in @text_view.
2074 * Tags in the buffer may override the default.
2076 * Return value: default justification
2079 gtk_text_view_get_justification (GtkTextView *text_view)
2081 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2083 return text_view->justify;
2087 * gtk_text_view_set_left_margin:
2088 * @text_view: a #GtkTextView
2089 * @left_margin: left margin in pixels
2091 * Sets the default left margin for text in @text_view.
2092 * Tags in the buffer may override the default.
2096 gtk_text_view_set_left_margin (GtkTextView *text_view,
2099 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2101 if (text_view->left_margin != left_margin)
2103 text_view->left_margin = left_margin;
2105 if (text_view->layout)
2107 text_view->layout->default_style->left_margin = left_margin;
2108 gtk_text_layout_default_style_changed (text_view->layout);
2111 g_object_notify (G_OBJECT (text_view), "left_margin");
2116 * gtk_text_view_get_left_margin:
2117 * @text_view: a #GtkTextView
2119 * Gets the default left margin size of paragraphs in the @text_view.
2120 * Tags in the buffer may override the default.
2122 * Return value: left margin in pixels
2125 gtk_text_view_get_left_margin (GtkTextView *text_view)
2127 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2129 return text_view->left_margin;
2133 * gtk_text_view_set_right_margin:
2134 * @text_view: a #GtkTextView
2135 * @right_margin: right margin in pixels
2137 * Sets the default right margin for text in the text view.
2138 * Tags in the buffer may override the default.
2142 gtk_text_view_set_right_margin (GtkTextView *text_view,
2145 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2147 if (text_view->right_margin != right_margin)
2149 text_view->right_margin = right_margin;
2151 if (text_view->layout)
2153 text_view->layout->default_style->right_margin = right_margin;
2154 gtk_text_layout_default_style_changed (text_view->layout);
2157 g_object_notify (G_OBJECT (text_view), "right_margin");
2162 * gtk_text_view_get_right_margin:
2163 * @text_view: a #GtkTextView
2165 * Gets the default right margin for text in @text_view. Tags
2166 * in the buffer may override the default.
2168 * Return value: right margin in pixels
2171 gtk_text_view_get_right_margin (GtkTextView *text_view)
2173 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2175 return text_view->right_margin;
2179 * gtk_text_view_set_indent:
2180 * @text_view: a #GtkTextView
2181 * @indent: indentation in pixels
2183 * Sets the default indentation for paragraphs in @text_view.
2184 * Tags in the buffer may override the default.
2188 gtk_text_view_set_indent (GtkTextView *text_view,
2191 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2193 if (text_view->indent != indent)
2195 text_view->indent = indent;
2197 if (text_view->layout)
2199 text_view->layout->default_style->indent = indent;
2200 gtk_text_layout_default_style_changed (text_view->layout);
2203 g_object_notify (G_OBJECT (text_view), "indent");
2208 * gtk_text_view_get_indent:
2209 * @text_view: a #GtkTextView
2211 * Gets the default indentation of paragraphs in @text_view.
2212 * Tags in the view's buffer may override the default.
2213 * The indentation may be negative.
2215 * Return value: number of pixels of indentation
2218 gtk_text_view_get_indent (GtkTextView *text_view)
2220 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2222 return text_view->indent;
2226 * gtk_text_view_set_tabs:
2227 * @text_view: a #GtkTextView
2228 * @tabs: tabs as a #PangoTabArray
2230 * Sets the default tab stops for paragraphs in @text_view.
2231 * Tags in the buffer may override the default.
2235 gtk_text_view_set_tabs (GtkTextView *text_view,
2236 PangoTabArray *tabs)
2238 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2240 if (text_view->tabs)
2241 pango_tab_array_free (text_view->tabs);
2243 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2245 if (text_view->layout)
2247 /* some unkosher futzing in internal struct details... */
2248 if (text_view->layout->default_style->tabs)
2249 pango_tab_array_free (text_view->layout->default_style->tabs);
2251 text_view->layout->default_style->tabs =
2252 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2254 gtk_text_layout_default_style_changed (text_view->layout);
2257 g_object_notify (G_OBJECT (text_view), "tabs");
2261 * gtk_text_view_get_tabs:
2262 * @text_view: a #GtkTextView
2264 * Gets the default tabs for @text_view. Tags in the buffer may
2265 * override the defaults. The returned array will be %NULL if
2266 * "standard" (8-space) tabs are used. Free the return value
2267 * with pango_tab_array_free().
2269 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2272 gtk_text_view_get_tabs (GtkTextView *text_view)
2274 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2276 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2280 * gtk_text_view_set_cursor_visible:
2281 * @text_view: a #GtkTextView
2282 * @setting: whether to show the insertion cursor
2284 * Toggles whether the insertion point is displayed. A buffer with no editable
2285 * text probably shouldn't have a visible cursor, so you may want to turn
2289 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2292 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2294 setting = (setting != FALSE);
2296 if (text_view->cursor_visible != setting)
2298 text_view->cursor_visible = setting;
2300 if (GTK_WIDGET_HAS_FOCUS (text_view))
2302 if (text_view->layout)
2304 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2305 gtk_text_view_check_cursor_blink (text_view);
2309 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2314 * gtk_text_view_get_cursor_visible:
2315 * @text_view: a #GtkTextView
2317 * Find out whether the cursor is being displayed.
2319 * Return value: whether the insertion mark is visible
2322 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2324 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2326 return text_view->cursor_visible;
2331 * gtk_text_view_place_cursor_onscreen:
2332 * @text_view: a #GtkTextView
2334 * Moves the cursor to the currently visible region of the
2335 * buffer, it it isn't there already.
2337 * Return value: TRUE if the cursor had to be moved.
2340 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2344 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2346 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2347 gtk_text_buffer_get_mark (get_buffer (text_view),
2350 if (clamp_iter_onscreen (text_view, &insert))
2352 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2360 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2362 if (text_view->first_validate_idle != 0)
2364 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2365 g_source_remove (text_view->first_validate_idle);
2366 text_view->first_validate_idle = 0;
2369 if (text_view->incremental_validate_idle != 0)
2371 g_source_remove (text_view->incremental_validate_idle);
2372 text_view->incremental_validate_idle = 0;
2377 gtk_text_view_destroy (GtkObject *object)
2379 GtkTextView *text_view;
2380 GtkTextLayout *layout;
2382 text_view = GTK_TEXT_VIEW (object);
2384 layout = text_view->layout;
2386 gtk_text_view_remove_validate_idles (text_view);
2387 gtk_text_view_set_buffer (text_view, NULL);
2388 gtk_text_view_destroy_layout (text_view);
2390 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2394 gtk_text_view_finalize (GObject *object)
2396 GtkTextView *text_view;
2398 text_view = GTK_TEXT_VIEW (object);
2400 g_return_if_fail (text_view->buffer == NULL);
2402 gtk_text_view_destroy_layout (text_view);
2403 gtk_text_view_set_buffer (text_view, NULL);
2405 if (text_view->pending_scroll)
2407 free_pending_scroll (text_view->pending_scroll);
2408 text_view->pending_scroll = NULL;
2411 if (text_view->tabs)
2412 pango_tab_array_free (text_view->tabs);
2414 if (text_view->hadjustment)
2415 g_object_unref (G_OBJECT (text_view->hadjustment));
2416 if (text_view->vadjustment)
2417 g_object_unref (G_OBJECT (text_view->vadjustment));
2419 text_window_free (text_view->text_window);
2421 if (text_view->left_window)
2422 text_window_free (text_view->left_window);
2424 if (text_view->top_window)
2425 text_window_free (text_view->top_window);
2427 if (text_view->right_window)
2428 text_window_free (text_view->right_window);
2430 if (text_view->bottom_window)
2431 text_window_free (text_view->bottom_window);
2433 g_object_unref (G_OBJECT (text_view->im_context));
2435 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2439 gtk_text_view_set_property (GObject *object,
2441 const GValue *value,
2444 GtkTextView *text_view;
2446 text_view = GTK_TEXT_VIEW (object);
2450 case PROP_PIXELS_ABOVE_LINES:
2451 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2454 case PROP_PIXELS_BELOW_LINES:
2455 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2458 case PROP_PIXELS_INSIDE_WRAP:
2459 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2463 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2466 case PROP_WRAP_MODE:
2467 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2470 case PROP_JUSTIFICATION:
2471 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2474 case PROP_LEFT_MARGIN:
2475 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2478 case PROP_RIGHT_MARGIN:
2479 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2483 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2487 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2490 case PROP_CURSOR_VISIBLE:
2491 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2495 g_assert_not_reached ();
2501 gtk_text_view_get_property (GObject *object,
2506 GtkTextView *text_view;
2508 text_view = GTK_TEXT_VIEW (object);
2512 case PROP_PIXELS_ABOVE_LINES:
2513 g_value_set_int (value, text_view->pixels_above_lines);
2516 case PROP_PIXELS_BELOW_LINES:
2517 g_value_set_int (value, text_view->pixels_below_lines);
2520 case PROP_PIXELS_INSIDE_WRAP:
2521 g_value_set_int (value, text_view->pixels_inside_wrap);
2525 g_value_set_boolean (value, text_view->editable);
2528 case PROP_WRAP_MODE:
2529 g_value_set_enum (value, text_view->wrap_mode);
2532 case PROP_JUSTIFICATION:
2533 g_value_set_enum (value, text_view->justify);
2536 case PROP_LEFT_MARGIN:
2537 g_value_set_int (value, text_view->left_margin);
2540 case PROP_RIGHT_MARGIN:
2541 g_value_set_int (value, text_view->right_margin);
2545 g_value_set_int (value, text_view->indent);
2549 g_value_set_boxed (value, text_view->tabs);
2552 case PROP_CURSOR_VISIBLE:
2553 g_value_set_boolean (value, text_view->cursor_visible);
2557 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2563 gtk_text_view_size_request (GtkWidget *widget,
2564 GtkRequisition *requisition)
2566 GtkTextView *text_view;
2568 gint focus_edge_width;
2570 gboolean interior_focus;
2572 text_view = GTK_TEXT_VIEW (widget);
2574 gtk_widget_style_get (widget,
2575 "interior_focus", &interior_focus,
2576 "focus_line_width", &focus_width,
2580 focus_edge_width = 0;
2582 focus_edge_width = focus_width;
2584 if (text_view->layout)
2586 text_view->text_window->requisition.width = text_view->layout->width;
2587 text_view->text_window->requisition.height = text_view->layout->height;
2591 text_view->text_window->requisition.width = 0;
2592 text_view->text_window->requisition.height = 0;
2595 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2596 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2598 if (text_view->left_window)
2599 requisition->width += text_view->left_window->requisition.width;
2601 if (text_view->right_window)
2602 requisition->width += text_view->right_window->requisition.width;
2604 if (text_view->top_window)
2605 requisition->height += text_view->top_window->requisition.height;
2607 if (text_view->bottom_window)
2608 requisition->height += text_view->bottom_window->requisition.height;
2610 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2611 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2613 tmp_list = text_view->children;
2614 while (tmp_list != NULL)
2616 GtkTextViewChild *child = tmp_list->data;
2620 GtkRequisition child_req;
2621 GtkRequisition old_req;
2623 gtk_widget_get_child_requisition (child->widget, &old_req);
2625 gtk_widget_size_request (child->widget, &child_req);
2627 gtk_widget_get_child_requisition (child->widget, &child_req);
2629 /* Invalidate layout lines if required */
2630 if (text_view->layout &&
2631 (old_req.width != child_req.width ||
2632 old_req.height != child_req.height))
2633 gtk_text_child_anchor_queue_resize (child->anchor,
2638 GtkRequisition child_req;
2640 gtk_widget_size_request (child->widget, &child_req);
2643 tmp_list = g_slist_next (tmp_list);
2648 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2649 GtkTextViewChild *vc,
2650 GtkAllocation *allocation)
2656 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2660 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2663 buffer_y += vc->from_top_of_line;
2665 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2666 allocation->y = buffer_y - text_view->yoffset;
2668 gtk_widget_get_child_requisition (vc->widget, &req);
2669 allocation->width = req.width;
2670 allocation->height = req.height;
2674 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2675 GtkTextViewChild *vc)
2677 GtkAllocation allocation;
2679 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2681 gtk_widget_size_allocate (vc->widget, &allocation);
2684 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2686 vc->widget->allocation.x,
2687 vc->widget->allocation.y,
2688 text_view->yoffset);
2693 gtk_text_view_child_allocated (GtkTextLayout *layout,
2699 GtkTextViewChild *vc = NULL;
2700 GtkTextView *text_view = data;
2702 /* x,y is the position of the child from the top of the line, and
2703 * from the left of the buffer. We have to translate that into text
2704 * window coordinates, then size_allocate the child.
2707 vc = g_object_get_data (G_OBJECT (child),
2708 "gtk-text-view-child");
2710 g_assert (vc != NULL);
2712 DV (g_print ("child allocated at %d,%d\n", x, y));
2714 vc->from_left_of_buffer = x;
2715 vc->from_top_of_line = y;
2717 gtk_text_view_update_child_allocation (text_view, vc);
2721 gtk_text_view_allocate_children (GtkTextView *text_view)
2725 DV(g_print(G_STRLOC"\n"));
2727 tmp_list = text_view->children;
2728 while (tmp_list != NULL)
2730 GtkTextViewChild *child = tmp_list->data;
2734 /* We need to force-validate the regions containing
2737 GtkTextIter child_loc;
2738 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2742 gtk_text_layout_validate_yrange (text_view->layout,
2748 GtkAllocation allocation;
2749 GtkRequisition child_req;
2751 g_assert (child != NULL);
2753 allocation.x = child->x;
2754 allocation.y = child->y;
2756 gtk_widget_get_child_requisition (child->widget, &child_req);
2758 allocation.width = child_req.width;
2759 allocation.height = child_req.height;
2761 gtk_widget_size_allocate (child->widget, &allocation);
2764 tmp_list = g_slist_next (tmp_list);
2769 gtk_text_view_size_allocate (GtkWidget *widget,
2770 GtkAllocation *allocation)
2772 GtkTextView *text_view;
2773 GtkTextIter first_para;
2775 GtkAdjustment *vadj;
2776 gboolean yoffset_changed = FALSE;
2778 GdkRectangle text_rect;
2779 GdkRectangle left_rect;
2780 GdkRectangle right_rect;
2781 GdkRectangle top_rect;
2782 GdkRectangle bottom_rect;
2783 gint focus_edge_width;
2785 gboolean interior_focus;
2786 gboolean size_changed;
2788 text_view = GTK_TEXT_VIEW (widget);
2790 DV(g_print(G_STRLOC"\n"));
2793 widget->allocation.width != allocation->width ||
2794 widget->allocation.height != allocation->height;
2796 widget->allocation = *allocation;
2798 if (GTK_WIDGET_REALIZED (widget))
2800 gdk_window_move_resize (widget->window,
2801 allocation->x, allocation->y,
2802 allocation->width, allocation->height);
2805 /* distribute width/height among child windows. Ensure all
2806 * windows get at least a 1x1 allocation.
2809 gtk_widget_style_get (widget,
2810 "interior_focus", &interior_focus,
2811 "focus_line_width", &focus_width,
2815 focus_edge_width = 0;
2817 focus_edge_width = focus_width;
2819 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2821 if (text_view->left_window)
2822 left_rect.width = text_view->left_window->requisition.width;
2824 left_rect.width = 0;
2826 width -= left_rect.width;
2828 if (text_view->right_window)
2829 right_rect.width = text_view->right_window->requisition.width;
2831 right_rect.width = 0;
2833 width -= right_rect.width;
2835 text_rect.width = MAX (1, width);
2837 top_rect.width = text_rect.width;
2838 bottom_rect.width = text_rect.width;
2841 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2843 if (text_view->top_window)
2844 top_rect.height = text_view->top_window->requisition.height;
2846 top_rect.height = 0;
2848 height -= top_rect.height;
2850 if (text_view->bottom_window)
2851 bottom_rect.height = text_view->bottom_window->requisition.height;
2853 bottom_rect.height = 0;
2855 height -= bottom_rect.height;
2857 text_rect.height = MAX (1, height);
2859 left_rect.height = text_rect.height;
2860 right_rect.height = text_rect.height;
2863 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2864 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2866 text_rect.x = left_rect.x + left_rect.width;
2867 text_rect.y = top_rect.y + top_rect.height;
2869 left_rect.y = text_rect.y;
2870 right_rect.y = text_rect.y;
2872 top_rect.x = text_rect.x;
2873 bottom_rect.x = text_rect.x;
2875 right_rect.x = text_rect.x + text_rect.width;
2876 bottom_rect.y = text_rect.y + text_rect.height;
2878 text_window_size_allocate (text_view->text_window,
2881 if (text_view->left_window)
2882 text_window_size_allocate (text_view->left_window,
2885 if (text_view->right_window)
2886 text_window_size_allocate (text_view->right_window,
2889 if (text_view->top_window)
2890 text_window_size_allocate (text_view->top_window,
2893 if (text_view->bottom_window)
2894 text_window_size_allocate (text_view->bottom_window,
2897 gtk_text_view_update_layout_width (text_view);
2899 /* Note that this will do some layout validation */
2900 gtk_text_view_allocate_children (text_view);
2902 /* Now adjust the value of the adjustment to keep the cursor at the
2903 * same place in the buffer
2905 gtk_text_view_get_first_para_iter (text_view, &first_para);
2906 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2908 y += text_view->first_para_pixels;
2910 /* Ensure h/v adj exist */
2911 get_hadjustment (text_view);
2912 get_vadjustment (text_view);
2914 vadj = text_view->vadjustment;
2915 if (y > vadj->upper - vadj->page_size)
2916 y = MAX (0, vadj->upper - vadj->page_size);
2918 if (y != text_view->yoffset)
2920 vadj->value = text_view->yoffset = y;
2921 yoffset_changed = TRUE;
2924 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2925 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
2926 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
2927 text_view->hadjustment->lower = 0;
2928 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2930 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2932 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2933 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2934 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2935 text_view->vadjustment->lower = 0;
2936 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2938 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2940 if (yoffset_changed)
2941 gtk_adjustment_value_changed (vadj);
2943 /* The GTK resize loop processes all the pending exposes right
2944 * after doing the resize stuff, so the idle sizer won't have a
2945 * chance to run. So we do the work here.
2947 gtk_text_view_flush_first_validate (text_view);
2949 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2952 if (size_changed && GTK_WIDGET_REALIZED (widget))
2953 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2957 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2960 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2961 text_view->first_para_mark);
2965 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2967 GtkWidget *widget = GTK_WIDGET (text_view);
2969 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
2971 if (SCREEN_HEIGHT (widget) > 0)
2973 GtkTextIter first_para;
2975 /* Be sure we've validated the stuff onscreen; if we
2976 * scrolled, these calls won't have any effect, because
2977 * they were called in the recursive validate_onscreen
2979 gtk_text_view_get_first_para_iter (text_view, &first_para);
2981 gtk_text_layout_validate_yrange (text_view->layout,
2984 text_view->first_para_pixels +
2985 SCREEN_HEIGHT (widget));
2988 text_view->onscreen_validated = TRUE;
2990 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
2992 /* This can have the odd side effect of triggering a scroll, which should
2993 * flip "onscreen_validated" back to FALSE, but should also get us
2994 * back into this function to turn it on again.
2996 gtk_text_view_update_adjustments (text_view);
2998 g_assert (text_view->onscreen_validated);
3002 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3004 if (text_view->first_validate_idle == 0)
3007 /* Do this first, which means that if an "invalidate"
3008 * occurs during any of this process, a new first_validate_callback
3009 * will be installed, and we'll start again.
3011 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3012 g_source_remove (text_view->first_validate_idle);
3013 text_view->first_validate_idle = 0;
3015 /* be sure we have up-to-date screen size set on the
3018 gtk_text_view_update_layout_width (text_view);
3020 /* Bail out if we invalidated stuff; scrolling right away will just
3021 * confuse the issue.
3023 if (text_view->first_validate_idle != 0)
3025 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3029 /* scroll to any marks, if that's pending. This can
3030 * jump us to the validation codepath used for scrolling
3031 * onscreen, if so we bail out.
3033 if (!gtk_text_view_flush_scroll (text_view))
3034 gtk_text_view_validate_onscreen (text_view);
3036 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3038 g_assert (text_view->onscreen_validated);
3043 first_validate_callback (gpointer data)
3045 GtkTextView *text_view = data;
3047 GDK_THREADS_ENTER ();
3049 /* Note that some of this code is duplicated at the end of size_allocate,
3050 * keep in sync with that.
3053 DV(g_print(G_STRLOC"\n"));
3055 gtk_text_view_flush_first_validate (text_view);
3057 GDK_THREADS_LEAVE ();
3063 incremental_validate_callback (gpointer data)
3065 GtkTextView *text_view = data;
3066 gboolean result = TRUE;
3068 GDK_THREADS_ENTER ();
3070 DV(g_print(G_STRLOC"\n"));
3072 gtk_text_layout_validate (text_view->layout, 2000);
3074 gtk_text_view_update_adjustments (text_view);
3076 if (gtk_text_layout_is_valid (text_view->layout))
3078 text_view->incremental_validate_idle = 0;
3082 GDK_THREADS_LEAVE ();
3088 gtk_text_view_invalidate (GtkTextView *text_view)
3090 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3091 text_view->onscreen_validated));
3093 text_view->onscreen_validated = FALSE;
3095 if (!text_view->first_validate_idle)
3097 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3098 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3099 text_view->first_validate_idle));
3102 if (!text_view->incremental_validate_idle)
3104 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3105 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3106 text_view->incremental_validate_idle));
3111 invalidated_handler (GtkTextLayout *layout,
3114 GtkTextView *text_view;
3116 text_view = GTK_TEXT_VIEW (data);
3118 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3119 gtk_text_view_invalidate (text_view);
3123 changed_handler (GtkTextLayout *layout,
3129 GtkTextView *text_view;
3131 GdkRectangle visible_rect;
3132 GdkRectangle redraw_rect;
3134 text_view = GTK_TEXT_VIEW (data);
3135 widget = GTK_WIDGET (data);
3137 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3139 if (GTK_WIDGET_REALIZED (text_view))
3141 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3143 redraw_rect.x = visible_rect.x;
3144 redraw_rect.width = visible_rect.width;
3145 redraw_rect.y = start_y;
3147 if (old_height == new_height)
3148 redraw_rect.height = old_height;
3150 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3152 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3154 /* text_window_invalidate_rect() takes buffer coordinates */
3155 text_window_invalidate_rect (text_view->text_window,
3158 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3162 redraw_rect.height));
3164 if (text_view->left_window)
3165 text_window_invalidate_rect (text_view->left_window,
3167 if (text_view->right_window)
3168 text_window_invalidate_rect (text_view->right_window,
3170 if (text_view->top_window)
3171 text_window_invalidate_rect (text_view->top_window,
3173 if (text_view->bottom_window)
3174 text_window_invalidate_rect (text_view->bottom_window,
3177 gtk_text_view_update_im_spot_location (text_view);
3181 if (old_height != new_height)
3183 gboolean yoffset_changed = FALSE;
3185 int new_first_para_top;
3186 int old_first_para_top;
3189 /* If the bottom of the old area was above the top of the
3190 * screen, we need to scroll to keep the current top of the
3191 * screen in place. Remember that first_para_pixels is the
3192 * position of the top of the screen in coordinates relative to
3193 * the first paragraph onscreen.
3195 * In short we are adding the height delta of the portion of the
3196 * changed region above first_para_mark to text_view->yoffset.
3198 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3199 text_view->first_para_mark);
3201 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3203 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3205 if (new_first_para_top != old_first_para_top)
3207 text_view->yoffset += new_first_para_top - old_first_para_top;
3209 get_vadjustment (text_view)->value = text_view->yoffset;
3210 yoffset_changed = TRUE;
3213 if (yoffset_changed)
3215 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3216 gtk_adjustment_value_changed (get_vadjustment (text_view));
3219 /* FIXME be smarter about which anchored widgets we update */
3221 tmp_list = text_view->children;
3222 while (tmp_list != NULL)
3224 GtkTextViewChild *child = tmp_list->data;
3227 gtk_text_view_update_child_allocation (text_view, child);
3229 tmp_list = g_slist_next (tmp_list);
3234 GtkRequisition old_req;
3235 GtkRequisition new_req;
3237 old_req = widget->requisition;
3239 /* Use this instead of gtk_widget_size_request wrapper
3240 * to avoid the optimization which just returns widget->requisition
3241 * if a resize hasn't been queued.
3243 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3245 if (old_req.width != new_req.width ||
3246 old_req.height != new_req.height)
3248 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3249 _gtk_size_group_queue_resize (widget);
3255 gtk_text_view_realize_cursor_gc (GtkTextView *text_view)
3257 GdkColor *cursor_color;
3258 GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
3260 if (text_view->cursor_gc)
3261 gdk_gc_unref (text_view->cursor_gc);
3263 gtk_widget_style_get (GTK_WIDGET (text_view), "cursor-color", &cursor_color, NULL);
3266 cursor_color = &red;
3268 text_view->cursor_gc = gdk_gc_new (text_view->text_window->bin_window);
3269 gdk_gc_set_rgb_fg_color (text_view->cursor_gc, cursor_color);
3273 gtk_text_view_realize (GtkWidget *widget)
3275 GtkTextView *text_view;
3276 GdkWindowAttr attributes;
3277 gint attributes_mask;
3280 text_view = GTK_TEXT_VIEW (widget);
3281 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3283 attributes.window_type = GDK_WINDOW_CHILD;
3284 attributes.x = widget->allocation.x;
3285 attributes.y = widget->allocation.y;
3286 attributes.width = widget->allocation.width;
3287 attributes.height = widget->allocation.height;
3288 attributes.wclass = GDK_INPUT_OUTPUT;
3289 attributes.visual = gtk_widget_get_visual (widget);
3290 attributes.colormap = gtk_widget_get_colormap (widget);
3291 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3293 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3295 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3296 &attributes, attributes_mask);
3297 gdk_window_set_user_data (widget->window, widget);
3299 /* must come before text_window_realize calls */
3300 widget->style = gtk_style_attach (widget->style, widget->window);
3302 gdk_window_set_background (widget->window,
3303 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3305 text_window_realize (text_view->text_window, widget->window);
3307 if (text_view->left_window)
3308 text_window_realize (text_view->left_window,
3311 if (text_view->top_window)
3312 text_window_realize (text_view->top_window,
3315 if (text_view->right_window)
3316 text_window_realize (text_view->right_window,
3319 if (text_view->bottom_window)
3320 text_window_realize (text_view->bottom_window,
3323 gtk_text_view_realize_cursor_gc (text_view);
3325 gtk_text_view_ensure_layout (text_view);
3327 if (text_view->buffer)
3328 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
3329 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3331 tmp_list = text_view->children;
3332 while (tmp_list != NULL)
3334 GtkTextViewChild *vc = tmp_list->data;
3336 text_view_child_set_parent_window (text_view, vc);
3338 tmp_list = tmp_list->next;
3343 gtk_text_view_unrealize (GtkWidget *widget)
3345 GtkTextView *text_view;
3347 text_view = GTK_TEXT_VIEW (widget);
3349 if (text_view->buffer)
3350 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
3351 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3353 if (text_view->cursor_gc)
3355 gdk_gc_unref (text_view->cursor_gc);
3356 text_view->cursor_gc = NULL;
3359 gtk_text_view_remove_validate_idles (text_view);
3361 if (text_view->popup_menu)
3363 gtk_widget_destroy (text_view->popup_menu);
3364 text_view->popup_menu = NULL;
3367 text_window_unrealize (text_view->text_window);
3369 if (text_view->left_window)
3370 text_window_unrealize (text_view->left_window);
3372 if (text_view->top_window)
3373 text_window_unrealize (text_view->top_window);
3375 if (text_view->right_window)
3376 text_window_unrealize (text_view->right_window);
3378 if (text_view->bottom_window)
3379 text_window_unrealize (text_view->bottom_window);
3381 gtk_text_view_destroy_layout (text_view);
3383 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3387 gtk_text_view_style_set (GtkWidget *widget,
3388 GtkStyle *previous_style)
3390 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3392 if (GTK_WIDGET_REALIZED (widget))
3394 gdk_window_set_background (widget->window,
3395 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3397 gdk_window_set_background (text_view->text_window->bin_window,
3398 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3400 if (text_view->left_window)
3401 gdk_window_set_background (text_view->left_window->bin_window,
3402 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3403 if (text_view->right_window)
3404 gdk_window_set_background (text_view->right_window->bin_window,
3405 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3407 if (text_view->top_window)
3408 gdk_window_set_background (text_view->top_window->bin_window,
3409 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3411 if (text_view->bottom_window)
3412 gdk_window_set_background (text_view->bottom_window->bin_window,
3413 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3415 gtk_text_view_realize_cursor_gc (text_view);
3418 if (text_view->layout && previous_style)
3420 gtk_text_view_set_attributes_from_style (text_view,
3421 text_view->layout->default_style,
3423 gtk_text_layout_default_style_changed (text_view->layout);
3428 gtk_text_view_direction_changed (GtkWidget *widget,
3429 GtkTextDirection previous_direction)
3431 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3433 if (text_view->layout)
3435 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3436 gtk_text_layout_default_style_changed (text_view->layout);
3442 set_invisible_cursor (GdkWindow *window)
3444 GdkBitmap *empty_bitmap;
3447 char invisible_cursor_bits[] = { 0x0 };
3449 useless.red = useless.green = useless.blue = 0;
3452 empty_bitmap = gdk_bitmap_create_from_data (window,
3453 invisible_cursor_bits,
3456 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3461 gdk_window_set_cursor (window, cursor);
3463 gdk_cursor_unref (cursor);
3465 g_object_unref (empty_bitmap);
3469 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3471 if (text_view->mouse_cursor_obscured)
3474 set_invisible_cursor (text_view->text_window->bin_window);
3476 text_view->mouse_cursor_obscured = TRUE;
3484 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3487 switch (event->type)
3489 case GDK_MOTION_NOTIFY:
3490 *x = event->motion.x;
3491 *y = event->motion.y;
3495 case GDK_BUTTON_PRESS:
3496 case GDK_2BUTTON_PRESS:
3497 case GDK_3BUTTON_PRESS:
3498 case GDK_BUTTON_RELEASE:
3499 *x = event->button.x;
3500 *y = event->button.y;
3505 case GDK_KEY_RELEASE:
3506 case GDK_ENTER_NOTIFY:
3507 case GDK_LEAVE_NOTIFY:
3508 case GDK_PROPERTY_NOTIFY:
3509 case GDK_SELECTION_CLEAR:
3510 case GDK_SELECTION_REQUEST:
3511 case GDK_SELECTION_NOTIFY:
3512 case GDK_PROXIMITY_IN:
3513 case GDK_PROXIMITY_OUT:
3514 case GDK_DRAG_ENTER:
3515 case GDK_DRAG_LEAVE:
3516 case GDK_DRAG_MOTION:
3517 case GDK_DRAG_STATUS:
3518 case GDK_DROP_START:
3519 case GDK_DROP_FINISHED:
3529 emit_event_on_tags (GtkWidget *widget,
3535 gboolean retval = FALSE;
3536 GtkTextView *text_view;
3538 text_view = GTK_TEXT_VIEW (widget);
3540 tags = gtk_text_iter_get_tags (iter);
3545 GtkTextTag *tag = tmp->data;
3547 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3553 tmp = g_slist_next (tmp);
3556 g_slist_free (tags);
3562 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3564 GtkTextView *text_view;
3567 text_view = GTK_TEXT_VIEW (widget);
3569 if (text_view->layout == NULL ||
3570 get_buffer (text_view) == NULL)
3573 if (event->any.window != text_view->text_window->bin_window)
3576 if (get_event_coordinates (event, &x, &y))
3580 x += text_view->xoffset;
3581 y += text_view->yoffset;
3583 /* FIXME this is slow and we do it twice per event.
3584 * My favorite solution is to have GtkTextLayout cache
3585 * the last couple lookups.
3587 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3591 return emit_event_on_tags (widget, event, &iter);
3593 else if (event->type == GDK_KEY_PRESS ||
3594 event->type == GDK_KEY_RELEASE)
3596 GtkTextMark *insert;
3599 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3602 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3604 return emit_event_on_tags (widget, event, &iter);
3611 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3613 gboolean retval = FALSE;
3614 gboolean obscure = FALSE;
3615 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3616 GtkTextMark *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 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3626 gtk_im_context_filter_keypress (text_view->im_context, event))
3628 text_view->need_im_reset = TRUE;
3632 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3633 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3635 else if (event->keyval == GDK_Return ||
3636 event->keyval == GDK_KP_Enter)
3638 gtk_text_view_commit_text (text_view, "\n");
3643 /* Pass through Tab as literal tab, unless Control is held down */
3644 else if ((event->keyval == GDK_Tab ||
3645 event->keyval == GDK_KP_Tab) &&
3646 !(event->state & GDK_CONTROL_MASK))
3648 /* If the text isn't editable, move the focus instead */
3649 if (text_view->editable)
3651 gtk_text_view_commit_text (text_view, "\t");
3655 gtk_text_view_move_focus (text_view,
3656 (event->state & GDK_SHIFT_MASK) ?
3657 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3665 gtk_text_view_obscure_mouse_cursor (text_view);
3667 gtk_text_view_pend_cursor_blink (text_view);
3673 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3675 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3676 GtkTextMark *insert;
3679 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3682 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3683 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3684 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3685 gtk_im_context_filter_keypress (text_view->im_context, event))
3687 text_view->need_im_reset = TRUE;
3691 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3695 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3697 GtkTextView *text_view;
3699 text_view = GTK_TEXT_VIEW (widget);
3701 text_view->disable_scroll_on_focus = TRUE;
3702 gtk_widget_grab_focus (widget);
3703 text_view->disable_scroll_on_focus = FALSE;
3705 if (event->window != text_view->text_window->bin_window)
3707 /* Remove selection if any. */
3708 gtk_text_view_unselect (text_view);
3714 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3715 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3716 else if (event->button == 3)
3717 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3720 if (event->type == GDK_BUTTON_PRESS)
3722 gtk_text_view_reset_im_context (text_view);
3724 if (event->button == 1)
3726 /* If we're in the selection, start a drag copy/move of the
3727 * selection; otherwise, start creating a new selection.
3730 GtkTextIter start, end;
3732 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3734 event->x + text_view->xoffset,
3735 event->y + text_view->yoffset);
3737 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3739 gtk_text_iter_in_range (&iter, &start, &end))
3741 text_view->drag_start_x = event->x;
3742 text_view->drag_start_y = event->y;
3743 text_view->pending_place_cursor_button = event->button;
3747 gtk_text_view_start_selection_drag (text_view, &iter, event);
3752 else if (event->button == 2)
3756 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3758 event->x + text_view->xoffset,
3759 event->y + text_view->yoffset);
3761 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3762 gtk_clipboard_get (GDK_SELECTION_PRIMARY),
3764 text_view->editable);
3767 else if (event->button == 3)
3769 gtk_text_view_do_popup (text_view, event);
3772 else if ((event->type == GDK_2BUTTON_PRESS ||
3773 event->type == GDK_3BUTTON_PRESS) &&
3776 GtkTextIter start, end;
3778 /* End the selection drag, otherwise we'd clear the new
3779 * word/line selection on button release
3781 gtk_text_view_end_selection_drag (text_view, event);
3783 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3785 event->x + text_view->xoffset,
3786 event->y + text_view->yoffset);
3790 if (event->type == GDK_2BUTTON_PRESS)
3792 if (gtk_text_iter_inside_word (&start))
3794 if (!gtk_text_iter_starts_word (&start))
3795 gtk_text_iter_backward_word_start (&start);
3797 if (!gtk_text_iter_ends_word (&end))
3798 gtk_text_iter_forward_word_end (&end);
3801 else if (event->type == GDK_3BUTTON_PRESS)
3803 if (gtk_text_view_starts_display_line (text_view, &start))
3805 /* If on a display line boundary, we assume the user
3806 * clicked off the end of a line and we therefore select
3807 * the line before the boundary.
3809 gtk_text_view_backward_display_line_start (text_view, &start);
3813 /* start isn't on the start of a line, so we move it to the
3814 * start, and move end to the end unless it's already there.
3816 gtk_text_view_backward_display_line_start (text_view, &start);
3818 if (!gtk_text_view_starts_display_line (text_view, &end))
3819 gtk_text_view_forward_display_line_end (text_view, &end);
3823 if (event->state & GDK_SHIFT_MASK)
3825 /* Take union of old and new selection */
3826 GtkTextIter old_start, old_end;
3828 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3829 &old_start, &old_end);
3831 gtk_text_iter_order (&start, &old_start);
3832 gtk_text_iter_order (&old_end, &end);
3834 /* Now start is the first of the starts, and end is the
3839 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3842 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3846 text_view->just_selected_element = TRUE;
3855 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3857 GtkTextView *text_view;
3859 text_view = GTK_TEXT_VIEW (widget);
3861 if (event->window != text_view->text_window->bin_window)
3864 if (event->button == 1)
3866 if (text_view->drag_start_x >= 0)
3868 text_view->drag_start_x = -1;
3869 text_view->drag_start_y = -1;
3872 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3874 else if (text_view->just_selected_element)
3876 text_view->just_selected_element = FALSE;
3879 else if (text_view->pending_place_cursor_button == event->button)
3883 /* Unselect everything; we clicked inside selection, but
3884 * didn't move by the drag threshold, so just clear selection
3887 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3889 event->x + text_view->xoffset,
3890 event->y + text_view->yoffset);
3892 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3894 text_view->pending_place_cursor_button = 0;
3904 keymap_direction_changed (GdkKeymap *keymap,
3905 GtkTextView *text_view)
3907 gtk_text_view_check_keymap_direction (text_view);
3911 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3913 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3915 gtk_widget_queue_draw (widget);
3917 DV(g_print (G_STRLOC": focus_in_event\n"));
3919 if (text_view->cursor_visible && text_view->layout)
3921 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3922 gtk_text_view_check_cursor_blink (text_view);
3925 g_signal_connect (gdk_keymap_get_default (),
3926 "direction_changed",
3927 G_CALLBACK (keymap_direction_changed), text_view);
3928 gtk_text_view_check_keymap_direction (text_view);
3930 text_view->need_im_reset = TRUE;
3931 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3937 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3939 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3941 gtk_widget_queue_draw (widget);
3943 DV(g_print (G_STRLOC": focus_out_event\n"));
3945 if (text_view->cursor_visible && text_view->layout)
3947 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3948 gtk_text_view_check_cursor_blink (text_view);
3951 g_signal_handlers_disconnect_by_func (gdk_keymap_get_default (),
3952 keymap_direction_changed,
3955 text_view->need_im_reset = TRUE;
3956 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3962 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3964 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3966 if (text_view->mouse_cursor_obscured)
3970 cursor = gdk_cursor_new (GDK_XTERM);
3971 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3972 gdk_cursor_unref (cursor);
3973 text_view->mouse_cursor_obscured = FALSE;
3976 if (event->window == text_view->text_window->bin_window &&
3977 text_view->drag_start_x >= 0)
3981 gdk_window_get_pointer (text_view->text_window->bin_window,
3984 if (gtk_drag_check_threshold (widget,
3985 text_view->drag_start_x,
3986 text_view->drag_start_y,
3990 gint buffer_x, buffer_y;
3992 gtk_text_view_window_to_buffer_coords (text_view,
3993 GTK_TEXT_WINDOW_TEXT,
3994 text_view->drag_start_x,
3995 text_view->drag_start_y,
3999 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4001 buffer_x, buffer_y);
4003 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4012 gtk_text_view_paint (GtkWidget *widget,
4014 GdkEventExpose *event)
4016 GtkTextView *text_view;
4017 GList *child_exposes;
4021 text_view = GTK_TEXT_VIEW (widget);
4023 g_return_if_fail (text_view->layout != NULL);
4024 g_return_if_fail (text_view->xoffset >= 0);
4025 g_return_if_fail (text_view->yoffset >= 0);
4027 while (text_view->first_validate_idle != 0)
4029 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4030 text_view->first_validate_idle));
4031 gtk_text_view_flush_first_validate (text_view);
4034 /* More regions could have become invalid in the above loop */
4035 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4040 gdk_region_get_clipbox (updates, &rect);
4042 gdk_rectangle_union (area, &rect, area);
4044 gdk_region_destroy (updates);
4047 if (!text_view->onscreen_validated)
4049 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.");
4054 printf ("painting %d,%d %d x %d\n",
4056 area->width, area->height);
4059 child_exposes = NULL;
4060 gtk_text_layout_draw (text_view->layout,
4062 text_view->text_window->bin_window,
4063 text_view->cursor_gc,
4067 area->width, area->height,
4070 tmp_list = child_exposes;
4071 while (tmp_list != NULL)
4073 GtkWidget *child = tmp_list->data;
4075 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4079 g_object_unref (G_OBJECT (child));
4081 tmp_list = tmp_list->next;
4084 g_list_free (child_exposes);
4088 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4092 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4093 GTK_TEXT_WINDOW_TEXT))
4095 DV(g_print (">Exposed ("G_STRLOC")\n"));
4096 gtk_text_view_paint (widget, &event->area, event);
4099 if (event->window == widget->window)
4100 gtk_text_view_draw_focus (widget);
4102 /* Propagate exposes to all children not in the buffer. */
4103 tmp_list = GTK_TEXT_VIEW (widget)->children;
4104 while (tmp_list != NULL)
4106 GtkTextViewChild *vc = tmp_list->data;
4108 /* propagate_expose checks that event->window matches
4111 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4112 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4116 tmp_list = tmp_list->next;
4123 gtk_text_view_draw_focus (GtkWidget *widget)
4125 gboolean interior_focus;
4127 /* We clear the focus if we are in interior focus mode. */
4128 gtk_widget_style_get (widget,
4129 "interior_focus", &interior_focus,
4132 if (GTK_WIDGET_DRAWABLE (widget))
4134 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4136 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4137 NULL, widget, "textview",
4139 widget->allocation.width,
4140 widget->allocation.height);
4144 gdk_window_clear (widget->window);
4150 gtk_text_view_grab_focus (GtkWidget *widget)
4152 GtkTextView *text_view;
4154 text_view = GTK_TEXT_VIEW (widget);
4156 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4158 if (!text_view->disable_scroll_on_focus)
4159 gtk_text_view_scroll_mark_onscreen (text_view,
4160 gtk_text_buffer_get_mark (get_buffer (text_view),
4165 gtk_text_view_focus (GtkWidget *widget,
4166 GtkDirectionType direction)
4168 GtkTextView *text_view;
4169 GtkContainer *container;
4171 text_view = GTK_TEXT_VIEW (widget);
4172 container = GTK_CONTAINER (widget);
4174 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4182 gtk_text_view_add (GtkContainer *container,
4185 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4186 g_return_if_fail (GTK_IS_WIDGET (child));
4188 /* This is pretty random. */
4189 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4191 GTK_TEXT_WINDOW_WIDGET,
4196 gtk_text_view_remove (GtkContainer *container,
4200 GtkTextView *text_view;
4201 GtkTextViewChild *vc;
4203 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4204 g_return_if_fail (GTK_IS_WIDGET (child));
4205 g_return_if_fail (child->parent == (GtkWidget*) container);
4207 text_view = GTK_TEXT_VIEW (container);
4210 iter = text_view->children;
4212 while (iter != NULL)
4216 if (vc->widget == child)
4219 iter = g_slist_next (iter);
4222 g_assert (iter != NULL); /* be sure we had the child in the list */
4224 text_view->children = g_slist_remove (text_view->children, vc);
4226 gtk_widget_unparent (vc->widget);
4228 text_view_child_free (vc);
4232 gtk_text_view_forall (GtkContainer *container,
4233 gboolean include_internals,
4234 GtkCallback callback,
4235 gpointer callback_data)
4238 GtkTextView *text_view;
4241 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4242 g_return_if_fail (callback != NULL);
4244 text_view = GTK_TEXT_VIEW (container);
4246 copy = g_slist_copy (text_view->children);
4249 while (iter != NULL)
4251 GtkTextViewChild *vc = iter->data;
4253 (* callback) (vc->widget, callback_data);
4255 iter = g_slist_next (iter);
4258 g_slist_free (copy);
4261 #define CURSOR_ON_MULTIPLIER 0.66
4262 #define CURSOR_OFF_MULTIPLIER 0.34
4263 #define CURSOR_PEND_MULTIPLIER 1.0
4266 cursor_blinks (GtkTextView *text_view)
4268 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4271 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4274 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4277 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4282 get_cursor_time (GtkTextView *text_view)
4284 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4287 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4297 blink_cb (gpointer data)
4299 GtkTextView *text_view;
4302 GDK_THREADS_ENTER ();
4304 text_view = GTK_TEXT_VIEW (data);
4306 g_assert (text_view->layout);
4307 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4308 g_assert (text_view->cursor_visible);
4310 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4313 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4317 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4321 gtk_text_layout_set_cursor_visible (text_view->layout,
4324 GDK_THREADS_LEAVE ();
4326 /* Remove ourselves */
4332 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4334 if (text_view->blink_timeout)
4336 gtk_timeout_remove (text_view->blink_timeout);
4337 text_view->blink_timeout = 0;
4342 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4344 if (text_view->layout != NULL &&
4345 text_view->cursor_visible &&
4346 GTK_WIDGET_HAS_FOCUS (text_view))
4348 if (cursor_blinks (text_view))
4350 if (text_view->blink_timeout == 0)
4352 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4354 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4360 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4364 gtk_text_view_stop_cursor_blink (text_view);
4369 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4371 if (text_view->layout != NULL &&
4372 text_view->cursor_visible &&
4373 GTK_WIDGET_HAS_FOCUS (text_view) &&
4374 cursor_blinks (text_view))
4376 if (text_view->blink_timeout != 0)
4378 gtk_timeout_remove (text_view->blink_timeout);
4379 text_view->blink_timeout = 0;
4382 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4384 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4392 * Key binding handlers
4396 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4397 GtkTextIter *newplace,
4402 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4408 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4413 /* FIXME when we are unfrozen and can change GtkMovementStep,
4416 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4419 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4420 GtkMovementStep step,
4422 gboolean extend_selection)
4425 GtkTextIter newplace;
4427 gint cursor_x_pos = 0;
4429 gtk_text_view_reset_im_context (text_view);
4431 if (step == GTK_MOVEMENT_PAGES)
4433 gtk_text_view_scroll_pages (text_view, count);
4434 gtk_text_view_pend_cursor_blink (text_view);
4437 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4439 gtk_text_view_scroll_hpages (text_view, count);
4440 gtk_text_view_pend_cursor_blink (text_view);
4444 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4445 gtk_text_buffer_get_mark (get_buffer (text_view),
4449 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4450 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4454 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4455 gtk_text_iter_forward_cursor_positions (&newplace, count);
4458 case GTK_MOVEMENT_VISUAL_POSITIONS:
4459 gtk_text_layout_move_iter_visually (text_view->layout,
4463 case GTK_MOVEMENT_WORDS:
4465 gtk_text_iter_backward_word_starts (&newplace, -count);
4467 gtk_text_iter_forward_word_ends (&newplace, count);
4470 case GTK_MOVEMENT_DISPLAY_LINES:
4471 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4472 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4475 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4477 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4478 else if (count < -1)
4479 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4482 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4485 case GTK_MOVEMENT_PARAGRAPHS:
4488 if (!gtk_text_iter_ends_line (&newplace))
4490 gtk_text_iter_forward_to_line_end (&newplace);
4496 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4498 gtk_text_iter_set_line_offset (&newplace, 0);
4505 gtk_text_iter_forward_lines (&newplace, count);
4506 gtk_text_iter_set_line_offset (&newplace, 0);
4510 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4513 if (!gtk_text_iter_ends_line (&newplace))
4514 gtk_text_iter_forward_to_line_end (&newplace);
4518 gtk_text_iter_set_line_offset (&newplace, 0);
4522 case GTK_MOVEMENT_BUFFER_ENDS:
4524 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4526 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4533 if (!gtk_text_iter_equal (&insert, &newplace))
4535 if (extend_selection)
4536 gtk_text_buffer_move_mark (get_buffer (text_view),
4537 gtk_text_buffer_get_mark (get_buffer (text_view),
4541 gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4543 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4544 gtk_text_view_scroll_mark_onscreen (text_view,
4545 gtk_text_buffer_get_mark (get_buffer (text_view),
4548 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4550 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4554 gtk_text_view_pend_cursor_blink (text_view);
4558 gtk_text_view_move_cursor (GtkTextView *text_view,
4559 GtkMovementStep step,
4561 gboolean extend_selection)
4563 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4567 gtk_text_view_page_horizontally (GtkTextView *text_view,
4569 gboolean extend_selection)
4571 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4572 count, extend_selection);
4576 gtk_text_view_set_anchor (GtkTextView *text_view)
4580 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4581 gtk_text_buffer_get_mark (get_buffer (text_view),
4584 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4588 gtk_text_view_scroll_pages (GtkTextView *text_view,
4594 gint cursor_x_pos, cursor_y_pos;
4595 GtkTextIter new_insert;
4599 g_return_if_fail (text_view->vadjustment != NULL);
4601 adj = text_view->vadjustment;
4603 /* Validate the region that will be brought into view by the cursor motion
4607 gtk_text_view_get_first_para_iter (text_view, &anchor);
4608 y0 = adj->page_size;
4609 y1 = adj->page_size + count * adj->page_increment;
4613 gtk_text_view_get_first_para_iter (text_view, &anchor);
4614 y0 = count * adj->page_increment + adj->page_size;
4618 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4619 /* FIXME do we need to update the adjustment ranges here? */
4621 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4623 /* already at top, just be sure we are at offset 0 */
4624 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4625 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4627 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4629 /* already at bottom, just be sure we are at the end */
4630 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4631 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4635 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4637 newval = adj->value;
4638 oldval = adj->value;
4640 newval += count * adj->page_increment;
4642 set_adjustment_clamped (adj, newval);
4643 cursor_y_pos += adj->value - oldval;
4645 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4646 clamp_iter_onscreen (text_view, &new_insert);
4647 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4649 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4652 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4653 * only guarantees 1 pixel onscreen.
4655 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4656 gtk_text_view_scroll_mark_onscreen (text_view,
4657 gtk_text_buffer_get_mark (get_buffer (text_view),
4662 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4668 gint cursor_x_pos, cursor_y_pos;
4669 GtkTextIter new_insert;
4672 g_return_if_fail (text_view->hadjustment != NULL);
4674 adj = text_view->hadjustment;
4676 /* Validate the line that we're moving within.
4678 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4680 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4681 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4682 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4683 /* FIXME do we need to update the adjustment ranges here? */
4685 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4687 /* already at far left, just be sure we are at offset 0 */
4688 gtk_text_iter_set_line_offset (&new_insert, 0);
4689 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4691 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4693 /* already at far right, just be sure we are at the end */
4694 gtk_text_iter_forward_to_line_end (&new_insert);
4695 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4699 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4701 newval = adj->value;
4702 oldval = adj->value;
4704 newval += count * adj->page_increment;
4706 set_adjustment_clamped (adj, newval);
4707 cursor_x_pos += adj->value - oldval;
4709 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4710 clamp_iter_onscreen (text_view, &new_insert);
4711 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4713 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4716 /* FIXME for lines shorter than the overall widget width, this results in a
4717 * "bounce" effect as we scroll to the right of the widget, then scroll
4718 * back to get the end of the line onscreen.
4719 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4722 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4723 * only guarantees 1 pixel onscreen.
4725 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4726 gtk_text_view_scroll_mark_onscreen (text_view,
4727 gtk_text_buffer_get_mark (get_buffer (text_view),
4732 whitespace (gunichar ch, gpointer user_data)
4734 return (ch == ' ' || ch == '\t');
4738 not_whitespace (gunichar ch, gpointer user_data)
4740 return !whitespace (ch, user_data);
4744 find_whitepace_region (const GtkTextIter *center,
4745 GtkTextIter *start, GtkTextIter *end)
4750 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4751 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4752 if (whitespace (gtk_text_iter_get_char (end), NULL))
4753 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4755 return !gtk_text_iter_equal (start, end);
4759 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4762 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4763 text_view->editable);
4767 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4774 gboolean leave_one = FALSE;
4776 gtk_text_view_reset_im_context (text_view);
4778 if (type == GTK_DELETE_CHARS)
4780 /* Char delete deletes the selection, if one exists */
4781 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4782 text_view->editable))
4786 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4788 gtk_text_buffer_get_mark (get_buffer (text_view),
4796 case GTK_DELETE_CHARS:
4797 gtk_text_iter_forward_cursor_positions (&end, count);
4800 case GTK_DELETE_WORD_ENDS:
4802 gtk_text_iter_forward_word_ends (&end, count);
4804 gtk_text_iter_backward_word_starts (&start, 0 - count);
4807 case GTK_DELETE_WORDS:
4810 case GTK_DELETE_DISPLAY_LINE_ENDS:
4813 case GTK_DELETE_DISPLAY_LINES:
4816 case GTK_DELETE_PARAGRAPH_ENDS:
4817 /* If we're already at a newline, we need to
4818 * simply delete that newline, instead of
4819 * moving to the next one.
4821 if (gtk_text_iter_ends_line (&end))
4823 gtk_text_iter_forward_line (&end);
4829 if (!gtk_text_iter_forward_to_line_end (&end))
4835 /* FIXME figure out what a negative count means
4839 case GTK_DELETE_PARAGRAPHS:
4842 gtk_text_iter_set_line_offset (&start, 0);
4843 gtk_text_iter_forward_to_line_end (&end);
4845 /* Do the lines beyond the first. */
4848 gtk_text_iter_forward_to_line_end (&end);
4854 /* FIXME negative count? */
4858 case GTK_DELETE_WHITESPACE:
4860 find_whitepace_region (&insert, &start, &end);
4868 if (!gtk_text_iter_equal (&start, &end))
4870 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4872 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4873 text_view->editable))
4876 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4878 text_view->editable);
4881 gtk_text_buffer_end_user_action (get_buffer (text_view));
4883 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4884 gtk_text_view_scroll_mark_onscreen (text_view,
4885 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4890 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4892 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4893 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4894 text_view->editable);
4895 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4896 gtk_text_view_scroll_mark_onscreen (text_view,
4897 gtk_text_buffer_get_mark (get_buffer (text_view),
4902 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4904 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4905 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
4906 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4907 gtk_text_view_scroll_mark_onscreen (text_view,
4908 gtk_text_buffer_get_mark (get_buffer (text_view),
4913 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4915 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4916 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4918 text_view->editable);
4919 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4920 gtk_text_view_scroll_mark_onscreen (text_view,
4921 gtk_text_buffer_get_mark (get_buffer (text_view),
4926 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4928 text_view->overwrite_mode = !text_view->overwrite_mode;
4932 gtk_text_view_move_focus (GtkTextView *text_view,
4933 GtkDirectionType direction_type)
4935 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
4937 if (!GTK_WIDGET_TOPLEVEL (toplevel))
4940 /* Propagate to toplevel */
4941 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus", direction_type);
4949 gtk_text_view_unselect (GtkTextView *text_view)
4953 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4955 gtk_text_buffer_get_mark (get_buffer (text_view),
4958 gtk_text_buffer_move_mark (get_buffer (text_view),
4959 gtk_text_buffer_get_mark (get_buffer (text_view),
4965 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
4966 const gchar *mark_name)
4969 GdkModifierType state;
4970 GtkTextIter newplace;
4972 /* DV(g_print (G_STRLOC": begin\n")); */
4974 gdk_window_get_pointer (text_view->text_window->bin_window,
4977 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
4978 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4980 x + text_view->xoffset,
4981 y + text_view->yoffset);
4985 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
4987 /* This may invalidate the layout */
4988 DV(g_print (G_STRLOC": move mark\n"));
4989 gtk_text_buffer_move_mark (get_buffer (text_view),
4993 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4994 gtk_text_view_scroll_mark_onscreen (text_view, mark);
4997 DV (g_print ("first validate idle leaving %s is %d\n",
4998 G_STRLOC, text_view->first_validate_idle));
5002 selection_scan_timeout (gpointer data)
5004 GtkTextView *text_view;
5006 GDK_THREADS_ENTER ();
5008 text_view = GTK_TEXT_VIEW (data);
5010 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5011 move_mark_to_pointer_and_scroll (text_view, "insert");
5013 GDK_THREADS_LEAVE ();
5015 return TRUE; /* remain installed. */
5018 #define DND_SCROLL_MARGIN 0.20
5021 drag_scan_timeout (gpointer data)
5023 GtkTextView *text_view;
5025 GdkModifierType state;
5026 GtkTextIter newplace;
5028 GDK_THREADS_ENTER ();
5030 text_view = GTK_TEXT_VIEW (data);
5032 gdk_window_get_pointer (text_view->text_window->bin_window,
5035 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5037 x + text_view->xoffset,
5038 y + text_view->yoffset);
5040 gtk_text_buffer_move_mark (get_buffer (text_view),
5041 text_view->dnd_mark,
5044 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5045 gtk_text_view_scroll_to_mark (text_view,
5046 text_view->dnd_mark,
5047 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5049 GDK_THREADS_LEAVE ();
5055 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5057 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5058 move_mark_to_pointer_and_scroll (text_view, "insert");
5060 /* If we had to scroll offscreen, insert a timeout to do so
5061 * again. Note that in the timeout, even if the mouse doesn't
5062 * move, due to this scroll xoffset/yoffset will have changed
5063 * and we'll need to scroll again.
5065 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5066 gtk_timeout_remove (text_view->scroll_timeout);
5068 text_view->scroll_timeout =
5069 gtk_timeout_add (50, selection_scan_timeout, text_view);
5075 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5076 const GtkTextIter *iter,
5077 GdkEventButton *button)
5079 GtkTextIter newplace;
5080 GtkTextBuffer *buffer;
5082 g_return_if_fail (text_view->selection_drag_handler == 0);
5084 gtk_grab_add (GTK_WIDGET (text_view));
5086 buffer = get_buffer (text_view);
5090 if (button->state & GDK_SHIFT_MASK)
5092 /* Extend selection */
5093 GtkTextIter start, end;
5095 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5097 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5099 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5102 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5105 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5107 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5110 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5116 /* Replace selection */
5117 gtk_text_buffer_place_cursor (buffer, &newplace);
5120 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5121 "motion_notify_event",
5122 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5126 /* returns whether we were really dragging */
5128 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5130 if (text_view->selection_drag_handler == 0)
5133 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5134 text_view->selection_drag_handler = 0;
5136 if (text_view->scroll_timeout != 0)
5138 gtk_timeout_remove (text_view->scroll_timeout);
5139 text_view->scroll_timeout = 0;
5142 /* one last update to current position */
5143 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5144 move_mark_to_pointer_and_scroll (text_view, "insert");
5146 gtk_grab_remove (GTK_WIDGET (text_view));
5156 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5157 GtkTextAttributes *values,
5160 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5161 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5164 pango_font_description_free (values->font);
5166 values->font = pango_font_description_copy (style->font_desc);
5170 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5172 if (text_view->layout)
5174 gboolean split_cursor;
5175 GtkTextDirection new_dir;
5176 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5178 g_object_get (G_OBJECT (settings),
5179 "gtk-split-cursor", &split_cursor,
5182 new_dir = GTK_TEXT_DIR_NONE;
5184 new_dir = (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
5185 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5187 if (text_view->layout->cursor_direction != new_dir)
5188 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5193 gtk_text_view_ensure_layout (GtkTextView *text_view)
5197 widget = GTK_WIDGET (text_view);
5199 if (text_view->layout == NULL)
5201 GtkTextAttributes *style;
5202 PangoContext *ltr_context, *rtl_context;
5205 DV(g_print(G_STRLOC"\n"));
5207 text_view->layout = gtk_text_layout_new ();
5209 g_signal_connect (G_OBJECT (text_view->layout),
5211 G_CALLBACK (invalidated_handler),
5214 g_signal_connect (G_OBJECT (text_view->layout),
5216 G_CALLBACK (changed_handler),
5219 g_signal_connect (G_OBJECT (text_view->layout),
5221 G_CALLBACK (gtk_text_view_child_allocated),
5224 if (get_buffer (text_view))
5225 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5227 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5228 gtk_text_view_pend_cursor_blink (text_view);
5230 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5232 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5233 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5234 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5235 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5237 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5239 g_object_unref (G_OBJECT (ltr_context));
5240 g_object_unref (G_OBJECT (rtl_context));
5242 gtk_text_view_check_keymap_direction (text_view);
5244 style = gtk_text_attributes_new ();
5246 gtk_widget_ensure_style (widget);
5247 gtk_text_view_set_attributes_from_style (text_view,
5248 style, widget->style);
5250 style->pixels_above_lines = text_view->pixels_above_lines;
5251 style->pixels_below_lines = text_view->pixels_below_lines;
5252 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5253 style->left_margin = text_view->left_margin;
5254 style->right_margin = text_view->right_margin;
5255 style->indent = text_view->indent;
5256 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5258 style->wrap_mode = text_view->wrap_mode;
5259 style->justification = text_view->justify;
5260 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5262 gtk_text_layout_set_default_style (text_view->layout, style);
5264 gtk_text_attributes_unref (style);
5266 /* Set layout for all anchored children */
5268 tmp_list = text_view->children;
5269 while (tmp_list != NULL)
5271 GtkTextViewChild *vc = tmp_list->data;
5275 gtk_text_anchored_child_set_layout (vc->widget,
5277 /* vc may now be invalid! */
5280 tmp_list = g_slist_next (tmp_list);
5286 * gtk_text_view_get_default_attributes:
5287 * @text_view: a #GtkTextView
5289 * Obtains a copy of the default text attributes. These are the
5290 * attributes used for text unless a tag overrides them.
5291 * You'd typically pass the default attributes in to
5292 * gtk_text_iter_get_attributes() in order to get the
5293 * attributes in effect at a given text position.
5295 * The return value is a copy owned by the caller of this function,
5296 * and should be freed.
5298 * Return value: a new #GtkTextAttributes
5301 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5303 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5305 gtk_text_view_ensure_layout (text_view);
5307 return gtk_text_attributes_copy (text_view->layout->default_style);
5311 gtk_text_view_destroy_layout (GtkTextView *text_view)
5313 if (text_view->layout)
5317 gtk_text_view_remove_validate_idles (text_view);
5319 /* Remove layout from all anchored children */
5320 tmp_list = text_view->children;
5321 while (tmp_list != NULL)
5323 GtkTextViewChild *vc = tmp_list->data;
5327 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5328 /* vc may now be invalid! */
5331 tmp_list = g_slist_next (tmp_list);
5334 gtk_text_view_stop_cursor_blink (text_view);
5335 gtk_text_view_end_selection_drag (text_view, NULL);
5337 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5338 invalidated_handler, text_view);
5339 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5340 changed_handler, text_view);
5341 g_object_unref (G_OBJECT (text_view->layout));
5342 text_view->layout = NULL;
5347 gtk_text_view_reset_im_context (GtkTextView *text_view)
5349 if (text_view->need_im_reset)
5351 text_view->need_im_reset = FALSE;
5352 gtk_im_context_reset (text_view->im_context);
5361 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5362 const GtkTextIter *iter,
5363 GdkEventMotion *event)
5365 GdkDragContext *context;
5366 GtkTargetList *target_list;
5368 text_view->drag_start_x = -1;
5369 text_view->drag_start_y = -1;
5370 text_view->pending_place_cursor_button = 0;
5372 target_list = gtk_target_list_new (target_table,
5373 G_N_ELEMENTS (target_table));
5375 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5376 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5377 1, (GdkEvent*)event);
5379 gtk_target_list_unref (target_list);
5381 gtk_drag_set_icon_default (context);
5385 gtk_text_view_drag_begin (GtkWidget *widget,
5386 GdkDragContext *context)
5392 gtk_text_view_drag_end (GtkWidget *widget,
5393 GdkDragContext *context)
5395 GtkTextView *text_view;
5397 text_view = GTK_TEXT_VIEW (widget);
5401 gtk_text_view_drag_data_get (GtkWidget *widget,
5402 GdkDragContext *context,
5403 GtkSelectionData *selection_data,
5407 GtkTextView *text_view;
5409 text_view = GTK_TEXT_VIEW (widget);
5411 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5413 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5415 gtk_selection_data_set (selection_data,
5416 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5429 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5432 /* Extract the selected text */
5433 str = gtk_text_iter_get_visible_text (&start, &end);
5438 gtk_selection_data_set_text (selection_data, str, -1);
5445 gtk_text_view_drag_data_delete (GtkWidget *widget,
5446 GdkDragContext *context)
5448 GtkTextView *text_view;
5450 text_view = GTK_TEXT_VIEW (widget);
5452 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5453 TRUE, GTK_TEXT_VIEW (widget)->editable);
5457 gtk_text_view_drag_leave (GtkWidget *widget,
5458 GdkDragContext *context,
5461 GtkTextView *text_view;
5463 text_view = GTK_TEXT_VIEW (widget);
5465 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5467 if (text_view->scroll_timeout != 0)
5468 gtk_timeout_remove (text_view->scroll_timeout);
5470 text_view->scroll_timeout = 0;
5474 gtk_text_view_drag_motion (GtkWidget *widget,
5475 GdkDragContext *context,
5480 GtkTextIter newplace;
5481 GtkTextView *text_view;
5484 GdkRectangle target_rect;
5486 GdkDragAction suggested_action = 0;
5488 text_view = GTK_TEXT_VIEW (widget);
5490 target_rect = text_view->text_window->allocation;
5492 if (x < target_rect.x ||
5493 y < target_rect.y ||
5494 x > (target_rect.x + target_rect.width) ||
5495 y > (target_rect.y + target_rect.height))
5496 return FALSE; /* outside the text window, allow parent widgets to handle event */
5498 gtk_text_view_window_to_buffer_coords (text_view,
5499 GTK_TEXT_WINDOW_WIDGET,
5503 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5507 if (gtk_drag_dest_find_target (widget, context,
5508 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5510 /* can't accept any of the offered targets */
5512 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5514 gtk_text_iter_in_range (&newplace, &start, &end))
5516 /* We're inside the selection. */
5520 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5522 GtkWidget *source_widget;
5524 suggested_action = context->suggested_action;
5526 source_widget = gtk_drag_get_source_widget (context);
5528 if (source_widget == widget)
5530 /* Default to MOVE, unless the user has
5531 * pressed ctrl or alt to affect available actions
5533 if ((context->actions & GDK_ACTION_MOVE) != 0)
5534 suggested_action = GDK_ACTION_MOVE;
5539 /* Can't drop here. */
5543 if (suggested_action != 0)
5545 gtk_text_mark_set_visible (text_view->dnd_mark,
5546 text_view->cursor_visible);
5548 gdk_drag_status (context, suggested_action, time);
5552 gdk_drag_status (context, 0, time);
5553 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5556 gtk_text_buffer_move_mark (get_buffer (text_view),
5557 text_view->dnd_mark,
5560 DV(g_print (G_STRLOC": scrolling to mark\n"));
5561 gtk_text_view_scroll_to_mark (text_view,
5562 text_view->dnd_mark,
5563 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5565 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5566 gtk_timeout_remove (text_view->scroll_timeout);
5568 text_view->scroll_timeout =
5569 gtk_timeout_add (50, drag_scan_timeout, text_view);
5571 /* TRUE return means don't propagate the drag motion to parent
5572 * widgets that may also be drop sites.
5578 gtk_text_view_drag_drop (GtkWidget *widget,
5579 GdkDragContext *context,
5584 GtkTextView *text_view;
5585 GtkTextIter drop_point;
5586 GdkAtom target = GDK_NONE;
5588 text_view = GTK_TEXT_VIEW (widget);
5590 if (text_view->scroll_timeout != 0)
5591 gtk_timeout_remove (text_view->scroll_timeout);
5593 text_view->scroll_timeout = 0;
5595 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5597 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5599 text_view->dnd_mark);
5601 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5602 target = gtk_drag_dest_find_target (widget, context, NULL);
5604 if (target != GDK_NONE)
5605 gtk_drag_get_data (widget, context, target, time);
5607 gtk_drag_finish (context, FALSE, FALSE, time);
5613 insert_text_data (GtkTextView *text_view,
5614 GtkTextIter *drop_point,
5615 GtkSelectionData *selection_data)
5619 str = gtk_selection_data_get_text (selection_data);
5623 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5624 drop_point, str, -1,
5625 text_view->editable);
5631 gtk_text_view_drag_data_received (GtkWidget *widget,
5632 GdkDragContext *context,
5635 GtkSelectionData *selection_data,
5639 GtkTextIter drop_point;
5640 GtkTextView *text_view;
5641 gboolean success = FALSE;
5643 text_view = GTK_TEXT_VIEW (widget);
5645 if (!text_view->dnd_mark)
5648 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5650 text_view->dnd_mark);
5652 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5655 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5657 GtkTextBuffer *src_buffer = NULL;
5658 GtkTextIter start, end;
5659 gboolean copy_tags = TRUE;
5661 if (selection_data->length != sizeof (src_buffer))
5664 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5666 if (src_buffer == NULL)
5669 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5671 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5672 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5675 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5680 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5684 text_view->editable);
5689 str = gtk_text_iter_get_visible_text (&start, &end);
5690 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5691 &drop_point, str, -1,
5692 text_view->editable);
5698 insert_text_data (text_view, &drop_point, selection_data);
5703 gtk_drag_finish (context, success,
5704 success && context->action == GDK_ACTION_MOVE,
5708 static GtkAdjustment*
5709 get_hadjustment (GtkTextView *text_view)
5711 if (text_view->hadjustment == NULL)
5712 gtk_text_view_set_scroll_adjustments (text_view,
5713 NULL, /* forces creation */
5714 text_view->vadjustment);
5716 return text_view->hadjustment;
5719 static GtkAdjustment*
5720 get_vadjustment (GtkTextView *text_view)
5722 if (text_view->vadjustment == NULL)
5723 gtk_text_view_set_scroll_adjustments (text_view,
5724 text_view->hadjustment,
5725 NULL); /* forces creation */
5726 return text_view->vadjustment;
5731 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5732 GtkAdjustment *hadj,
5733 GtkAdjustment *vadj)
5735 gboolean need_adjust = FALSE;
5737 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5740 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5742 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5744 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5746 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5748 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5750 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5751 g_object_unref (G_OBJECT (text_view->hadjustment));
5754 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5756 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5757 g_object_unref (G_OBJECT (text_view->vadjustment));
5760 if (text_view->hadjustment != hadj)
5762 text_view->hadjustment = hadj;
5763 g_object_ref (G_OBJECT (text_view->hadjustment));
5764 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5766 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5767 (GtkSignalFunc) gtk_text_view_value_changed,
5772 if (text_view->vadjustment != vadj)
5774 text_view->vadjustment = vadj;
5775 g_object_ref (G_OBJECT (text_view->vadjustment));
5776 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5778 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5779 (GtkSignalFunc) gtk_text_view_value_changed,
5785 gtk_text_view_value_changed (NULL, text_view);
5788 /* FIXME this adjust_allocation is a big cut-and-paste from
5789 * GtkCList, needs to be some "official" way to do this
5799 /* The window to which widget->window is relative */
5800 #define ALLOCATION_WINDOW(widget) \
5801 (GTK_WIDGET_NO_WINDOW (widget) ? \
5802 (widget)->window : \
5803 gdk_window_get_parent ((widget)->window))
5806 adjust_allocation_recurse (GtkWidget *widget,
5809 ScrollData *scroll_data = data;
5811 /* Need to really size allocate instead of just poking
5812 * into widget->allocation if the widget is not realized.
5813 * FIXME someone figure out why this was.
5815 if (!GTK_WIDGET_REALIZED (widget))
5817 if (GTK_WIDGET_VISIBLE (widget))
5819 GdkRectangle tmp_rectangle = widget->allocation;
5820 tmp_rectangle.x += scroll_data->dx;
5821 tmp_rectangle.y += scroll_data->dy;
5823 gtk_widget_size_allocate (widget, &tmp_rectangle);
5828 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5830 widget->allocation.x += scroll_data->dx;
5831 widget->allocation.y += scroll_data->dy;
5833 if (GTK_IS_CONTAINER (widget))
5834 gtk_container_forall (GTK_CONTAINER (widget),
5835 adjust_allocation_recurse,
5842 adjust_allocation (GtkWidget *widget,
5846 ScrollData scroll_data;
5848 if (GTK_WIDGET_REALIZED (widget))
5849 scroll_data.window = ALLOCATION_WINDOW (widget);
5851 scroll_data.window = NULL;
5853 scroll_data.dx = dx;
5854 scroll_data.dy = dy;
5856 adjust_allocation_recurse (widget, &scroll_data);
5860 gtk_text_view_value_changed (GtkAdjustment *adj,
5861 GtkTextView *text_view)
5868 /* Note that we oddly call this function with adj == NULL
5872 text_view->onscreen_validated = FALSE;
5874 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5875 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5876 adj ? adj->value : 0.0));
5878 if (adj == text_view->hadjustment)
5880 dx = text_view->xoffset - (gint)adj->value;
5881 text_view->xoffset = adj->value;
5883 else if (adj == text_view->vadjustment)
5885 dy = text_view->yoffset - (gint)adj->value;
5886 text_view->yoffset = adj->value;
5888 if (text_view->layout)
5890 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5892 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5894 text_view->first_para_pixels = adj->value - line_top;
5898 if (dx != 0 || dy != 0)
5902 if (GTK_WIDGET_REALIZED (text_view))
5906 if (text_view->left_window)
5907 text_window_scroll (text_view->left_window, 0, dy);
5908 if (text_view->right_window)
5909 text_window_scroll (text_view->right_window, 0, dy);
5914 if (text_view->top_window)
5915 text_window_scroll (text_view->top_window, dx, 0);
5916 if (text_view->bottom_window)
5917 text_window_scroll (text_view->bottom_window, dx, 0);
5920 /* It looks nicer to scroll the main area last, because
5921 * it takes a while, and making the side areas update
5922 * afterward emphasizes the slowness of scrolling the
5925 text_window_scroll (text_view->text_window, dx, dy);
5928 /* Children are now "moved" in the text window, poke
5929 * into widget->allocation for each child
5931 tmp_list = text_view->children;
5932 while (tmp_list != NULL)
5934 GtkTextViewChild *child = tmp_list->data;
5937 adjust_allocation (child->widget, dx, dy);
5939 tmp_list = g_slist_next (tmp_list);
5943 /* This could result in invalidation, which would install the
5944 * first_validate_idle, which would validate onscreen;
5945 * but we're going to go ahead and validate here, so
5946 * first_validate_idle shouldn't have anything to do.
5948 gtk_text_view_update_layout_width (text_view);
5950 /* note that validation of onscreen could invoke this function
5951 * recursively, by scrolling to maintain first_para, or in response
5952 * to updating the layout width, however there is no problem with
5953 * that, or shouldn't be.
5955 gtk_text_view_validate_onscreen (text_view);
5957 /* process exposes */
5958 if (GTK_WIDGET_REALIZED (text_view))
5960 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
5962 if (text_view->left_window)
5963 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
5965 if (text_view->right_window)
5966 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
5968 if (text_view->top_window)
5969 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
5971 if (text_view->bottom_window)
5972 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
5974 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
5977 /* If this got installed, get rid of it, it's just a waste of time. */
5978 if (text_view->first_validate_idle != 0)
5980 g_source_remove (text_view->first_validate_idle);
5981 text_view->first_validate_idle = 0;
5984 gtk_text_view_update_im_spot_location (text_view);
5986 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
5990 gtk_text_view_commit_handler (GtkIMContext *context,
5992 GtkTextView *text_view)
5994 gtk_text_view_commit_text (text_view, str);
5998 gtk_text_view_commit_text (GtkTextView *text_view,
6001 gboolean had_selection;
6003 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6005 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6008 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6009 text_view->editable);
6011 if (!strcmp (str, "\n"))
6013 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6014 text_view->editable);
6018 if (!had_selection && text_view->overwrite_mode)
6019 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6020 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6021 text_view->editable);
6024 gtk_text_buffer_end_user_action (get_buffer (text_view));
6026 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6027 gtk_text_view_scroll_mark_onscreen (text_view,
6028 gtk_text_buffer_get_mark (get_buffer (text_view),
6033 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6034 GtkTextView *text_view)
6037 PangoAttrList *attrs;
6040 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6041 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6043 pango_attr_list_unref (attrs);
6048 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6049 GtkTextView *text_view)
6056 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6057 gtk_text_buffer_get_insert (text_view->buffer));
6060 pos = gtk_text_iter_get_line_index (&start);
6061 gtk_text_iter_set_line_offset (&start, 0);
6062 gtk_text_iter_forward_to_line_end (&end);
6064 text = gtk_text_iter_get_slice (&start, &end);
6065 gtk_im_context_set_surrounding (context, text, -1, pos);
6072 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6075 GtkTextView *text_view)
6080 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6081 gtk_text_buffer_get_insert (text_view->buffer));
6084 gtk_text_iter_forward_chars (&start, offset);
6085 gtk_text_iter_forward_chars (&end, offset + n_chars);
6087 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6093 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6094 const GtkTextIter *location,
6098 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6099 gboolean need_reset = FALSE;
6101 if (mark == gtk_text_buffer_get_insert (buffer))
6103 text_view->virtual_cursor_x = -1;
6104 text_view->virtual_cursor_y = -1;
6105 gtk_text_view_update_im_spot_location (text_view);
6108 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6114 gtk_text_view_reset_im_context (text_view);
6118 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6122 GdkRectangle strong_pos;
6125 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6126 gtk_text_buffer_get_mark (get_buffer (text_view),
6129 if ((x && text_view->virtual_cursor_x == -1) ||
6130 (y && text_view->virtual_cursor_y == -1))
6131 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6135 if (text_view->virtual_cursor_x != -1)
6136 *x = text_view->virtual_cursor_x;
6143 if (text_view->virtual_cursor_x != -1)
6144 *y = text_view->virtual_cursor_y;
6146 *y = strong_pos.y + strong_pos.height / 2;
6151 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6155 GdkRectangle strong_pos;
6158 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6159 gtk_text_buffer_get_mark (get_buffer (text_view),
6162 if (x == -1 || y == -1)
6163 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6165 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6166 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6169 /* Quick hack of a popup menu
6172 activate_cb (GtkWidget *menuitem,
6173 GtkTextView *text_view)
6175 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6176 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6180 append_action_signal (GtkTextView *text_view,
6182 const gchar *stock_id,
6183 const gchar *signal,
6186 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6188 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6189 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6190 GTK_SIGNAL_FUNC (activate_cb), text_view);
6192 gtk_widget_set_sensitive (menuitem, sensitive);
6194 gtk_widget_show (menuitem);
6195 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6199 popup_menu_detach (GtkWidget *attach_widget,
6202 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6206 popup_position_func (GtkMenu *menu,
6212 GtkTextView *text_view;
6214 GdkRectangle cursor_rect;
6215 GdkRectangle onscreen_rect;
6216 gint root_x, root_y;
6220 text_view = GTK_TEXT_VIEW (user_data);
6221 widget = GTK_WIDGET (text_view);
6223 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6225 gdk_window_get_origin (widget->window, &root_x, &root_y);
6227 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6229 gtk_text_buffer_get_insert (get_buffer (text_view)));
6231 gtk_text_view_get_iter_location (text_view,
6235 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6237 gtk_widget_size_request (text_view->popup_menu, &req);
6239 /* can't use rectangle_intersect since cursor rect can have 0 width */
6240 if (cursor_rect.x >= onscreen_rect.x &&
6241 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6242 cursor_rect.y >= onscreen_rect.y &&
6243 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6245 gtk_text_view_buffer_to_window_coords (text_view,
6246 GTK_TEXT_WINDOW_WIDGET,
6247 cursor_rect.x, cursor_rect.y,
6248 &cursor_rect.x, &cursor_rect.y);
6250 *x = root_x + cursor_rect.x + cursor_rect.width;
6251 *y = root_y + cursor_rect.y + cursor_rect.height;
6255 /* Just center the menu, since cursor is offscreen. */
6256 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6257 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6261 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6262 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6264 *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
6265 *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
6270 GtkTextView *text_view;
6276 range_contains_editable_text (const GtkTextIter *start,
6277 const GtkTextIter *end,
6278 gboolean default_editability)
6280 GtkTextIter iter = *start;
6282 while (gtk_text_iter_compare (&iter, end) < 0)
6284 if (gtk_text_iter_editable (&iter, default_editability))
6287 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6294 unichar_chosen_func (const char *text,
6297 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6299 gtk_text_view_commit_text (text_view, text);
6303 popup_targets_received (GtkClipboard *clipboard,
6304 GtkSelectionData *data,
6307 PopupInfo *info = user_data;
6308 GtkTextView *text_view = info->text_view;
6310 if (GTK_WIDGET_REALIZED (text_view))
6312 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6313 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6315 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6316 GtkWidget *menuitem;
6318 gboolean have_selection;
6319 gboolean can_insert;
6321 GtkTextIter sel_start, sel_end;
6323 if (text_view->popup_menu)
6324 gtk_widget_destroy (text_view->popup_menu);
6326 text_view->popup_menu = gtk_menu_new ();
6328 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6329 GTK_WIDGET (text_view),
6332 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6333 &sel_start, &sel_end);
6335 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6337 gtk_text_buffer_get_insert (get_buffer (text_view)));
6339 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6341 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6343 range_contains_editable_text (&sel_start, &sel_end,
6344 text_view->editable));
6345 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6347 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6348 can_insert && clipboard_contains_text);
6350 menuitem = gtk_separator_menu_item_new ();
6351 gtk_widget_show (menuitem);
6352 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6354 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6355 gtk_widget_show (menuitem);
6356 submenu = gtk_menu_new ();
6357 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6358 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6360 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6361 GTK_MENU_SHELL (submenu));
6363 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6364 gtk_widget_show (menuitem);
6365 gtk_widget_set_sensitive (menuitem, can_insert);
6367 submenu = gtk_menu_new ();
6368 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6369 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6371 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6372 unichar_chosen_func,
6375 gtk_signal_emit (GTK_OBJECT (text_view),
6376 signals[POPULATE_POPUP],
6377 text_view->popup_menu);
6380 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6382 info->button, info->time);
6385 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6386 popup_position_func, text_view,
6387 0, gtk_get_current_event_time ());
6388 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu));
6392 g_object_unref (text_view);
6397 gtk_text_view_do_popup (GtkTextView *text_view,
6398 GdkEventButton *event)
6400 PopupInfo *info = g_new (PopupInfo, 1);
6402 /* In order to know what entries we should make sensitive, we
6403 * ask for the current targets of the clipboard, and when
6404 * we get them, then we actually pop up the menu.
6406 info->text_view = g_object_ref (text_view);
6410 info->button = event->button;
6411 info->time = event->time;
6416 info->time = gtk_get_current_event_time ();
6419 gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
6420 gdk_atom_intern ("TARGETS", FALSE),
6421 popup_targets_received,
6426 gtk_text_view_popup_menu (GtkWidget *widget)
6428 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6432 /* Child GdkWindows */
6435 static GtkTextWindow*
6436 text_window_new (GtkTextWindowType type,
6439 gint height_request)
6443 win = g_new (GtkTextWindow, 1);
6446 win->widget = widget;
6448 win->bin_window = NULL;
6449 win->requisition.width = width_request;
6450 win->requisition.height = height_request;
6451 win->allocation.width = width_request;
6452 win->allocation.height = height_request;
6453 win->allocation.x = 0;
6454 win->allocation.y = 0;
6460 text_window_free (GtkTextWindow *win)
6463 text_window_unrealize (win);
6469 text_window_realize (GtkTextWindow *win,
6472 GdkWindowAttr attributes;
6473 gint attributes_mask;
6476 attributes.window_type = GDK_WINDOW_CHILD;
6477 attributes.x = win->allocation.x;
6478 attributes.y = win->allocation.y;
6479 attributes.width = win->allocation.width;
6480 attributes.height = win->allocation.height;
6481 attributes.wclass = GDK_INPUT_OUTPUT;
6482 attributes.visual = gtk_widget_get_visual (win->widget);
6483 attributes.colormap = gtk_widget_get_colormap (win->widget);
6484 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6486 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6488 win->window = gdk_window_new (parent,
6492 gdk_window_show (win->window);
6493 gdk_window_set_user_data (win->window, win->widget);
6497 attributes.width = win->allocation.width;
6498 attributes.height = win->allocation.height;
6499 attributes.event_mask = (GDK_EXPOSURE_MASK |
6501 GDK_KEY_PRESS_MASK |
6502 GDK_BUTTON_PRESS_MASK |
6503 GDK_BUTTON_RELEASE_MASK |
6504 GDK_POINTER_MOTION_MASK |
6505 GDK_POINTER_MOTION_HINT_MASK |
6506 gtk_widget_get_events (win->widget));
6508 win->bin_window = gdk_window_new (win->window,
6512 gdk_window_show (win->bin_window);
6513 gdk_window_set_user_data (win->bin_window, win->widget);
6515 if (win->type == GTK_TEXT_WINDOW_TEXT)
6518 cursor = gdk_cursor_new (GDK_XTERM);
6519 gdk_window_set_cursor (win->bin_window, cursor);
6520 gdk_cursor_unref (cursor);
6522 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6526 gdk_window_set_background (win->bin_window,
6527 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6531 gdk_window_set_background (win->bin_window,
6532 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6535 g_object_set_qdata (G_OBJECT (win->window),
6536 g_quark_from_static_string ("gtk-text-view-text-window"),
6539 g_object_set_qdata (G_OBJECT (win->bin_window),
6540 g_quark_from_static_string ("gtk-text-view-text-window"),
6545 text_window_unrealize (GtkTextWindow *win)
6547 if (win->type == GTK_TEXT_WINDOW_TEXT)
6549 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6553 gdk_window_set_user_data (win->window, NULL);
6554 gdk_window_set_user_data (win->bin_window, NULL);
6555 gdk_window_destroy (win->bin_window);
6556 gdk_window_destroy (win->window);
6558 win->bin_window = NULL;
6562 text_window_size_allocate (GtkTextWindow *win,
6565 win->allocation = *rect;
6569 gdk_window_move_resize (win->window,
6571 rect->width, rect->height);
6573 gdk_window_resize (win->bin_window,
6574 rect->width, rect->height);
6579 text_window_scroll (GtkTextWindow *win,
6583 if (dx != 0 || dy != 0)
6585 gdk_window_scroll (win->bin_window, dx, dy);
6590 text_window_invalidate_rect (GtkTextWindow *win,
6593 GdkRectangle window_rect;
6595 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6602 window_rect.width = rect->width;
6603 window_rect.height = rect->height;
6605 /* Adjust the rect as appropriate */
6609 case GTK_TEXT_WINDOW_TEXT:
6612 case GTK_TEXT_WINDOW_LEFT:
6613 case GTK_TEXT_WINDOW_RIGHT:
6615 window_rect.width = win->allocation.width;
6618 case GTK_TEXT_WINDOW_TOP:
6619 case GTK_TEXT_WINDOW_BOTTOM:
6621 window_rect.height = win->allocation.height;
6625 g_warning ("%s: bug!", G_STRLOC);
6630 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6634 GdkColor color = { 0, 65535, 0, 0 };
6635 GdkGC *gc = gdk_gc_new (win->bin_window);
6636 gdk_gc_set_rgb_fg_color (gc, &color);
6637 gdk_draw_rectangle (win->bin_window,
6638 gc, TRUE, window_rect.x, window_rect.y,
6639 window_rect.width, window_rect.height);
6646 text_window_get_width (GtkTextWindow *win)
6648 return win->allocation.width;
6652 text_window_get_height (GtkTextWindow *win)
6654 return win->allocation.height;
6658 text_window_get_allocation (GtkTextWindow *win,
6661 *rect = win->allocation;
6668 * gtk_text_view_get_window:
6669 * @text_view: a #GtkTextView
6670 * @win: window to get
6672 * Retrieves the #GdkWindow corresponding to an area of the text view;
6673 * possible windows include the overall widget window, child windows
6674 * on the left, right, top, bottom, and the window that displays the
6675 * text buffer. Windows are %NULL and nonexistent if their width or
6676 * height is 0, and are nonexistent before the widget has been
6679 * Return value: a #GdkWindow, or %NULL
6682 gtk_text_view_get_window (GtkTextView *text_view,
6683 GtkTextWindowType win)
6685 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6689 case GTK_TEXT_WINDOW_WIDGET:
6690 return GTK_WIDGET (text_view)->window;
6693 case GTK_TEXT_WINDOW_TEXT:
6694 return text_view->text_window->bin_window;
6697 case GTK_TEXT_WINDOW_LEFT:
6698 if (text_view->left_window)
6699 return text_view->left_window->bin_window;
6704 case GTK_TEXT_WINDOW_RIGHT:
6705 if (text_view->right_window)
6706 return text_view->right_window->bin_window;
6711 case GTK_TEXT_WINDOW_TOP:
6712 if (text_view->top_window)
6713 return text_view->top_window->bin_window;
6718 case GTK_TEXT_WINDOW_BOTTOM:
6719 if (text_view->bottom_window)
6720 return text_view->bottom_window->bin_window;
6725 case GTK_TEXT_WINDOW_PRIVATE:
6726 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6731 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6736 * gtk_text_view_get_window_type:
6737 * @text_view: a #GtkTextView
6738 * @window: a window type
6740 * Usually used to find out which window an event corresponds to.
6741 * If you connect to an event signal on @text_view, this function
6742 * should be called on <literal>event->window</literal> to
6743 * see which window it was.
6745 * Return value: the window type.
6748 gtk_text_view_get_window_type (GtkTextView *text_view,
6753 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6754 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6756 if (window == GTK_WIDGET (text_view)->window)
6757 return GTK_TEXT_WINDOW_WIDGET;
6759 win = g_object_get_qdata (G_OBJECT (window),
6760 g_quark_try_string ("gtk-text-view-text-window"));
6766 return GTK_TEXT_WINDOW_PRIVATE;
6771 buffer_to_widget (GtkTextView *text_view,
6779 *window_x = buffer_x - text_view->xoffset;
6780 *window_x += text_view->text_window->allocation.x;
6785 *window_y = buffer_y - text_view->yoffset;
6786 *window_y += text_view->text_window->allocation.y;
6791 widget_to_text_window (GtkTextWindow *win,
6798 *window_x = widget_x - win->allocation.x;
6801 *window_y = widget_y - win->allocation.y;
6805 buffer_to_text_window (GtkTextView *text_view,
6814 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6815 "for a nonexistent or private child window of GtkTextView");
6819 buffer_to_widget (text_view,
6821 window_x, window_y);
6823 widget_to_text_window (win,
6824 window_x ? *window_x : 0,
6825 window_y ? *window_y : 0,
6831 * gtk_text_view_buffer_to_window_coords:
6832 * @text_view: a #GtkTextView
6833 * @win: a #GtkTextWindowType
6834 * @buffer_x: buffer x coordinate
6835 * @buffer_y: buffer y coordinate
6836 * @window_x: window x coordinate return location
6837 * @window_y: window y coordinate return location
6839 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6840 * @win, and stores the result in (@window_x, @window_y).
6843 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6844 GtkTextWindowType win,
6850 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6854 case GTK_TEXT_WINDOW_WIDGET:
6855 buffer_to_widget (text_view,
6857 window_x, window_y);
6860 case GTK_TEXT_WINDOW_TEXT:
6862 *window_x = buffer_x - text_view->xoffset;
6864 *window_y = buffer_y - text_view->yoffset;
6867 case GTK_TEXT_WINDOW_LEFT:
6868 buffer_to_text_window (text_view,
6869 text_view->left_window,
6871 window_x, window_y);
6874 case GTK_TEXT_WINDOW_RIGHT:
6875 buffer_to_text_window (text_view,
6876 text_view->right_window,
6878 window_x, window_y);
6881 case GTK_TEXT_WINDOW_TOP:
6882 buffer_to_text_window (text_view,
6883 text_view->top_window,
6885 window_x, window_y);
6888 case GTK_TEXT_WINDOW_BOTTOM:
6889 buffer_to_text_window (text_view,
6890 text_view->bottom_window,
6892 window_x, window_y);
6895 case GTK_TEXT_WINDOW_PRIVATE:
6896 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6900 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6906 widget_to_buffer (GtkTextView *text_view,
6914 *buffer_x = widget_x + text_view->xoffset;
6915 *buffer_x -= text_view->text_window->allocation.x;
6920 *buffer_y = widget_y + text_view->yoffset;
6921 *buffer_y -= text_view->text_window->allocation.y;
6926 text_window_to_widget (GtkTextWindow *win,
6933 *widget_x = window_x + win->allocation.x;
6936 *widget_y = window_y + win->allocation.y;
6940 text_window_to_buffer (GtkTextView *text_view,
6949 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
6950 "coordinates for a nonexistent child window.");
6954 text_window_to_widget (win,
6960 widget_to_buffer (text_view,
6961 buffer_x ? *buffer_x : 0,
6962 buffer_y ? *buffer_y : 0,
6968 * gtk_text_view_window_to_buffer_coords:
6969 * @text_view: a #GtkTextView
6970 * @win: a #GtkTextWindowType
6971 * @window_x: window x coordinate
6972 * @window_y: window y coordinate
6973 * @buffer_x: buffer x coordinate return location
6974 * @buffer_y: buffer y coordinate return location
6976 * Converts coordinates on the window identified by @win to buffer
6977 * coordinates, storing the result in (@buffer_x,@buffer_y).
6980 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
6981 GtkTextWindowType win,
6987 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6991 case GTK_TEXT_WINDOW_WIDGET:
6992 widget_to_buffer (text_view,
6994 buffer_x, buffer_y);
6997 case GTK_TEXT_WINDOW_TEXT:
6999 *buffer_x = window_x + text_view->xoffset;
7001 *buffer_y = window_y + text_view->yoffset;
7004 case GTK_TEXT_WINDOW_LEFT:
7005 text_window_to_buffer (text_view,
7006 text_view->left_window,
7008 buffer_x, buffer_y);
7011 case GTK_TEXT_WINDOW_RIGHT:
7012 text_window_to_buffer (text_view,
7013 text_view->right_window,
7015 buffer_x, buffer_y);
7018 case GTK_TEXT_WINDOW_TOP:
7019 text_window_to_buffer (text_view,
7020 text_view->top_window,
7022 buffer_x, buffer_y);
7025 case GTK_TEXT_WINDOW_BOTTOM:
7026 text_window_to_buffer (text_view,
7027 text_view->bottom_window,
7029 buffer_x, buffer_y);
7032 case GTK_TEXT_WINDOW_PRIVATE:
7033 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7037 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7043 set_window_width (GtkTextView *text_view,
7045 GtkTextWindowType type,
7046 GtkTextWindow **winp)
7052 text_window_free (*winp);
7054 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7061 *winp = text_window_new (type,
7062 GTK_WIDGET (text_view),
7064 /* if the widget is already realized we need to realize the child manually */
7065 if (GTK_WIDGET_REALIZED (text_view))
7066 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7070 if ((*winp)->requisition.width == width)
7073 (*winp)->requisition.width = width;
7076 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7082 set_window_height (GtkTextView *text_view,
7084 GtkTextWindowType type,
7085 GtkTextWindow **winp)
7091 text_window_free (*winp);
7093 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7100 *winp = text_window_new (type,
7101 GTK_WIDGET (text_view),
7104 /* if the widget is already realized we need to realize the child manually */
7105 if (GTK_WIDGET_REALIZED (text_view))
7106 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7110 if ((*winp)->requisition.height == height)
7113 (*winp)->requisition.height = height;
7116 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7121 * gtk_text_view_set_border_window_size:
7122 * @text_view: a #GtkTextView
7123 * @type: window to affect
7124 * @size: width or height of the window
7126 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7127 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7128 * Automatically destroys the corresponding window if the size is set
7129 * to 0, and creates the window if the size is set to non-zero. This
7130 * function can only be used for the "border windows," it doesn't work
7131 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7132 * #GTK_TEXT_WINDOW_PRIVATE.
7135 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7136 GtkTextWindowType type,
7140 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7141 g_return_if_fail (size >= 0);
7145 case GTK_TEXT_WINDOW_LEFT:
7146 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7147 &text_view->left_window);
7150 case GTK_TEXT_WINDOW_RIGHT:
7151 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7152 &text_view->right_window);
7155 case GTK_TEXT_WINDOW_TOP:
7156 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7157 &text_view->top_window);
7160 case GTK_TEXT_WINDOW_BOTTOM:
7161 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7162 &text_view->bottom_window);
7166 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7172 * gtk_text_view_get_border_window_size:
7173 * @text_view: a #GtkTextView
7174 * @type: window to return size from
7176 * Gets the width of the specified border window. See
7177 * gtk_text_view_set_border_window_size().
7179 * Return value: width of window
7182 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7183 GtkTextWindowType type)
7185 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7189 case GTK_TEXT_WINDOW_LEFT:
7190 if (text_view->left_window)
7191 return text_view->left_window->requisition.width;
7193 case GTK_TEXT_WINDOW_RIGHT:
7194 if (text_view->right_window)
7195 return text_view->right_window->requisition.width;
7197 case GTK_TEXT_WINDOW_TOP:
7198 if (text_view->top_window)
7199 return text_view->top_window->requisition.height;
7201 case GTK_TEXT_WINDOW_BOTTOM:
7202 if (text_view->bottom_window)
7203 return text_view->bottom_window->requisition.height;
7206 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7217 static GtkTextViewChild*
7218 text_view_child_new_anchored (GtkWidget *child,
7219 GtkTextChildAnchor *anchor,
7220 GtkTextLayout *layout)
7222 GtkTextViewChild *vc;
7224 vc = g_new (GtkTextViewChild, 1);
7227 vc->anchor = anchor;
7229 vc->from_top_of_line = 0;
7230 vc->from_left_of_buffer = 0;
7232 g_object_ref (G_OBJECT (vc->widget));
7233 g_object_ref (G_OBJECT (vc->anchor));
7235 g_object_set_data (G_OBJECT (child),
7236 "gtk-text-view-child",
7239 gtk_text_child_anchor_register_child (anchor, child, layout);
7244 static GtkTextViewChild*
7245 text_view_child_new_window (GtkWidget *child,
7246 GtkTextWindowType type,
7250 GtkTextViewChild *vc;
7252 vc = g_new (GtkTextViewChild, 1);
7257 vc->from_top_of_line = 0;
7258 vc->from_left_of_buffer = 0;
7260 g_object_ref (G_OBJECT (vc->widget));
7266 g_object_set_data (G_OBJECT (child),
7267 "gtk-text-view-child",
7274 text_view_child_free (GtkTextViewChild *child)
7276 g_object_set_data (G_OBJECT (child->widget),
7277 "gtk-text-view-child", NULL);
7281 gtk_text_child_anchor_unregister_child (child->anchor,
7283 g_object_unref (G_OBJECT (child->anchor));
7286 g_object_unref (G_OBJECT (child->widget));
7292 text_view_child_set_parent_window (GtkTextView *text_view,
7293 GtkTextViewChild *vc)
7296 gtk_widget_set_parent_window (vc->widget,
7297 text_view->text_window->bin_window);
7301 window = gtk_text_view_get_window (text_view,
7303 gtk_widget_set_parent_window (vc->widget, window);
7308 add_child (GtkTextView *text_view,
7309 GtkTextViewChild *vc)
7311 text_view->children = g_slist_prepend (text_view->children,
7314 if (GTK_WIDGET_REALIZED (text_view))
7315 text_view_child_set_parent_window (text_view, vc);
7317 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7321 * gtk_text_view_add_child_at_anchor:
7322 * @text_view: a #GtkTextView
7323 * @child: a #GtkWidget
7324 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7326 * Adds a child widget in the text buffer, at the given @anchor.
7330 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7332 GtkTextChildAnchor *anchor)
7334 GtkTextViewChild *vc;
7336 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7337 g_return_if_fail (GTK_IS_WIDGET (child));
7338 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7339 g_return_if_fail (child->parent == NULL);
7341 gtk_text_view_ensure_layout (text_view);
7343 vc = text_view_child_new_anchored (child, anchor,
7346 add_child (text_view, vc);
7348 g_assert (vc->widget == child);
7349 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7353 * gtk_text_view_add_child_in_window:
7354 * @text_view: a #GtkTextView
7355 * @child: a #GtkWidget
7356 * @which_window: which window the child should appear in
7357 * @xpos: X position of child in window coordinates
7358 * @ypos: Y position of child in window coordinates
7360 * Adds a child at fixed coordinates in one of the text widget's
7361 * windows. The window must have nonzero size (see
7362 * gtk_text_view_set_border_window_size()). Note that the child
7363 * coordinates are given relative to the #GdkWindow in question, and
7364 * that these coordinates have no sane relationship to scrolling. When
7365 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7366 * irrelevant, the child floats above all scrollable areas. But when
7367 * placing a child in one of the scrollable windows (border windows or
7368 * text window), you'll need to compute the child's correct position
7369 * in buffer coordinates any time scrolling occurs or buffer changes
7370 * occur, and then call gtk_text_view_move_child() to update the
7371 * child's position. Unfortunately there's no good way to detect that
7372 * scrolling has occurred, using the current API; a possible hack
7373 * would be to update all child positions when the scroll adjustments
7374 * change or the text buffer changes. See bug 64518 on
7375 * bugzilla.gnome.org for status of fixing this issue.
7379 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7381 GtkTextWindowType which_window,
7385 GtkTextViewChild *vc;
7387 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7388 g_return_if_fail (GTK_IS_WIDGET (child));
7389 g_return_if_fail (child->parent == NULL);
7391 vc = text_view_child_new_window (child, which_window,
7394 add_child (text_view, vc);
7396 g_assert (vc->widget == child);
7397 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7401 * gtk_text_view_move_child:
7402 * @text_view: a #GtkTextView
7403 * @child: child widget already added to the text view
7404 * @xpos: new X position in window coordinates
7405 * @ypos: new Y position in window coordinates
7407 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7411 gtk_text_view_move_child (GtkTextView *text_view,
7416 GtkTextViewChild *vc;
7418 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7419 g_return_if_fail (GTK_IS_WIDGET (child));
7420 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7422 vc = g_object_get_data (G_OBJECT (child),
7423 "gtk-text-view-child");
7425 g_assert (vc != NULL);
7427 if (vc->x == xpos &&
7434 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7435 gtk_widget_queue_resize (child);
7439 /* Iterator operations */
7442 * gtk_text_view_forward_display_line:
7443 * @text_view: a #GtkTextView
7444 * @iter: a #GtkTextIter
7446 * Moves the given @iter forward by one display (wrapped) line. A
7447 * display line is different from a paragraph. Paragraphs are
7448 * separated by newlines or other paragraph separator characters.
7449 * Display lines are created by line-wrapping a paragraph. If
7450 * wrapping is turned off, display lines and paragraphs will be the
7451 * same. Display lines are divided differently for each view, since
7452 * they depend on the view's width; paragraphs are the same in all
7453 * views, since they depend on the contents of the #GtkTextBuffer.
7455 * Return value: %TRUE if @iter was moved and is not on the end iterator
7458 gtk_text_view_forward_display_line (GtkTextView *text_view,
7461 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7462 g_return_val_if_fail (iter != NULL, FALSE);
7464 gtk_text_view_ensure_layout (text_view);
7466 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7470 * gtk_text_view_backward_display_line:
7471 * @text_view: a #GtkTextView
7472 * @iter: a #GtkTextIter
7474 * Moves the given @iter backward by one display (wrapped) line. A
7475 * display line is different from a paragraph. Paragraphs are
7476 * separated by newlines or other paragraph separator characters.
7477 * Display lines are created by line-wrapping a paragraph. If
7478 * wrapping is turned off, display lines and paragraphs will be the
7479 * same. Display lines are divided differently for each view, since
7480 * they depend on the view's width; paragraphs are the same in all
7481 * views, since they depend on the contents of the #GtkTextBuffer.
7483 * Return value: %TRUE if @iter was moved and is not on the end iterator
7486 gtk_text_view_backward_display_line (GtkTextView *text_view,
7489 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7490 g_return_val_if_fail (iter != NULL, FALSE);
7492 gtk_text_view_ensure_layout (text_view);
7494 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7498 * gtk_text_view_forward_display_line_end:
7499 * @text_view: a #GtkTextView
7500 * @iter: a #GtkTextIter
7502 * Moves the given @iter forward to the next display line end. A
7503 * display line is different from a paragraph. Paragraphs are
7504 * separated by newlines or other paragraph separator characters.
7505 * Display lines are created by line-wrapping a paragraph. If
7506 * wrapping is turned off, display lines and paragraphs will be the
7507 * same. Display lines are divided differently for each view, since
7508 * they depend on the view's width; paragraphs are the same in all
7509 * views, since they depend on the contents of the #GtkTextBuffer.
7511 * Return value: %TRUE if @iter was moved and is not on the end iterator
7514 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7517 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7518 g_return_val_if_fail (iter != NULL, FALSE);
7520 gtk_text_view_ensure_layout (text_view);
7522 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7526 * gtk_text_view_backward_display_line_start:
7527 * @text_view: a #GtkTextView
7528 * @iter: a #GtkTextIter
7530 * Moves the given @iter backward to the next display line start. A
7531 * display line is different from a paragraph. Paragraphs are
7532 * separated by newlines or other paragraph separator characters.
7533 * Display lines are created by line-wrapping a paragraph. If
7534 * wrapping is turned off, display lines and paragraphs will be the
7535 * same. Display lines are divided differently for each view, since
7536 * they depend on the view's width; paragraphs are the same in all
7537 * views, since they depend on the contents of the #GtkTextBuffer.
7539 * Return value: %TRUE if @iter was moved and is not on the end iterator
7542 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7545 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7546 g_return_val_if_fail (iter != NULL, FALSE);
7548 gtk_text_view_ensure_layout (text_view);
7550 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7554 * gtk_text_view_starts_display_line:
7555 * @text_view: a #GtkTextView
7556 * @iter: a #GtkTextIter
7558 * Determines whether @iter is at the start of a display line.
7559 * See gtk_text_view_forward_display_line() for an explanation of
7560 * display lines vs. paragraphs.
7562 * Return value: %TRUE if @iter begins a wrapped line
7565 gtk_text_view_starts_display_line (GtkTextView *text_view,
7566 const GtkTextIter *iter)
7568 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7569 g_return_val_if_fail (iter != NULL, FALSE);
7571 gtk_text_view_ensure_layout (text_view);
7573 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7577 * gtk_text_view_move_visually:
7578 * @text_view: a #GtkTextView
7579 * @iter: a #GtkTextIter
7580 * @count: number of lines to move
7582 * Moves @iter up or down by @count display (wrapped) lines.
7583 * See gtk_text_view_forward_display_line() for an explanation of
7584 * display lines vs. paragraphs.
7586 * Return value: %TRUE if @iter moved and is not on the end iterator
7589 gtk_text_view_move_visually (GtkTextView *text_view,
7593 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7594 g_return_val_if_fail (iter != NULL, FALSE);
7596 gtk_text_view_ensure_layout (text_view);
7598 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);