1 /* GTK - The GIMP Toolkit
2 * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
30 #include "gtkbindings.h"
32 #include "gtkimagemenuitem.h"
35 #include "gtkmarshalers.h"
37 #include "gtkmenuitem.h"
38 #include "gtkseparatormenuitem.h"
39 #include "gtksettings.h"
40 #include "gtksignal.h"
42 #include "gtktextdisplay.h"
43 #include "gtktextview.h"
44 #include "gtkimmulticontext.h"
45 #include "gdk/gdkkeysyms.h"
46 #include "gtksizegroup.h" /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
47 #include "gtktextutil.h"
48 #include "gtkwindow.h"
51 /* How scrolling, validation, exposes, etc. work.
53 * The expose_event handler has the invariant that the onscreen lines
54 * have been validated.
56 * There are two ways that onscreen lines can become invalid. The first
57 * is to change which lines are onscreen. This happens when the value
58 * of a scroll adjustment changes. So the code path begins in
59 * gtk_text_view_value_changed() and goes like this:
60 * - gdk_window_scroll() to reflect the new adjustment value
61 * - validate the lines that were moved onscreen
62 * - gdk_window_process_updates() to handle the exposes immediately
64 * The second way is that you get the "invalidated" signal from the layout,
65 * indicating that lines have become invalid. This code path begins in
66 * invalidated_handler() and goes like this:
67 * - install high-priority idle which does the rest of the steps
68 * - if a scroll is pending from scroll_to_mark(), do the scroll,
69 * jumping to the gtk_text_view_value_changed() code path
70 * - otherwise, validate the onscreen lines
71 * - DO NOT process updates
73 * In both cases, validating the onscreen lines can trigger a scroll
74 * due to maintaining the first_para on the top of the screen.
75 * If validation triggers a scroll, we jump to the top of the code path
76 * for value_changed, and bail out of the current code path.
78 * Also, in size_allocate, if we invalidate some lines from changing
79 * the layout width, we need to go ahead and run the high-priority idle,
80 * because GTK sends exposes right after doing the size allocates without
81 * returning to the main loop. This is also why the high-priority idle
82 * is at a higher priority than resizing.
87 #define DEBUG_VALIDATION_AND_SCROLLING
90 #ifdef DEBUG_VALIDATION_AND_SCROLLING
96 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
97 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
99 struct _GtkTextPendingScroll
102 gdouble within_margin;
110 SET_SCROLL_ADJUSTMENTS,
128 PROP_PIXELS_ABOVE_LINES,
129 PROP_PIXELS_BELOW_LINES,
130 PROP_PIXELS_INSIDE_WRAP,
142 static void gtk_text_view_init (GtkTextView *text_view);
143 static void gtk_text_view_class_init (GtkTextViewClass *klass);
144 static void gtk_text_view_destroy (GtkObject *object);
145 static void gtk_text_view_finalize (GObject *object);
146 static void gtk_text_view_set_property (GObject *object,
150 static void gtk_text_view_get_property (GObject *object,
154 static void gtk_text_view_size_request (GtkWidget *widget,
155 GtkRequisition *requisition);
156 static void gtk_text_view_size_allocate (GtkWidget *widget,
157 GtkAllocation *allocation);
158 static void gtk_text_view_realize (GtkWidget *widget);
159 static void gtk_text_view_unrealize (GtkWidget *widget);
160 static void gtk_text_view_style_set (GtkWidget *widget,
161 GtkStyle *previous_style);
162 static void gtk_text_view_direction_changed (GtkWidget *widget,
163 GtkTextDirection previous_direction);
164 static gint gtk_text_view_event (GtkWidget *widget,
166 static gint gtk_text_view_key_press_event (GtkWidget *widget,
168 static gint gtk_text_view_key_release_event (GtkWidget *widget,
170 static gint gtk_text_view_button_press_event (GtkWidget *widget,
171 GdkEventButton *event);
172 static gint gtk_text_view_button_release_event (GtkWidget *widget,
173 GdkEventButton *event);
174 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
175 GdkEventFocus *event);
176 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
177 GdkEventFocus *event);
178 static gint gtk_text_view_motion_event (GtkWidget *widget,
179 GdkEventMotion *event);
180 static gint gtk_text_view_expose_event (GtkWidget *widget,
181 GdkEventExpose *expose);
182 static void gtk_text_view_draw_focus (GtkWidget *widget);
183 static void gtk_text_view_grab_focus (GtkWidget *widget);
184 static gboolean gtk_text_view_focus (GtkWidget *widget,
185 GtkDirectionType direction);
188 /* Source side drag signals */
189 static void gtk_text_view_drag_begin (GtkWidget *widget,
190 GdkDragContext *context);
191 static void gtk_text_view_drag_end (GtkWidget *widget,
192 GdkDragContext *context);
193 static void gtk_text_view_drag_data_get (GtkWidget *widget,
194 GdkDragContext *context,
195 GtkSelectionData *selection_data,
198 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
199 GdkDragContext *context);
201 /* Target side drag signals */
202 static void gtk_text_view_drag_leave (GtkWidget *widget,
203 GdkDragContext *context,
205 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
206 GdkDragContext *context,
210 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
211 GdkDragContext *context,
215 static void gtk_text_view_drag_data_received (GtkWidget *widget,
216 GdkDragContext *context,
219 GtkSelectionData *selection_data,
223 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
225 GtkAdjustment *vadj);
226 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
228 static void gtk_text_view_move_cursor (GtkTextView *text_view,
229 GtkMovementStep step,
231 gboolean extend_selection);
232 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
234 gboolean extend_selection);
235 static void gtk_text_view_set_anchor (GtkTextView *text_view);
236 static void gtk_text_view_scroll_pages (GtkTextView *text_view,
238 gboolean extend_selection);
239 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
241 gboolean extend_selection);
242 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
244 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
247 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
248 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
249 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
250 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
251 static void gtk_text_view_move_focus (GtkTextView *text_view,
252 GtkDirectionType direction_type);
253 static void gtk_text_view_unselect (GtkTextView *text_view);
255 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
256 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
258 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
259 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
260 GtkTextAttributes *values,
262 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
263 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
264 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
265 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
266 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
267 const GtkTextIter *iter,
268 GdkEventButton *event);
269 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
270 GdkEventButton *event);
271 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
272 const GtkTextIter *iter,
273 GdkEventMotion *event);
274 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
275 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
276 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
278 static void gtk_text_view_value_changed (GtkAdjustment *adj,
280 static void gtk_text_view_commit_handler (GtkIMContext *context,
282 GtkTextView *text_view);
283 static void gtk_text_view_commit_text (GtkTextView *text_view,
285 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
286 GtkTextView *text_view);
287 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
288 GtkTextView *text_view);
289 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
292 GtkTextView *text_view);
294 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
295 const GtkTextIter *location,
298 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
301 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
305 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
306 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
308 static void gtk_text_view_do_popup (GtkTextView *text_view,
309 GdkEventButton *event);
311 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
313 gdouble within_margin,
318 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
319 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
320 static void gtk_text_view_invalidate (GtkTextView *text_view);
321 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
323 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
325 /* Container methods */
326 static void gtk_text_view_add (GtkContainer *container,
328 static void gtk_text_view_remove (GtkContainer *container,
330 static void gtk_text_view_forall (GtkContainer *container,
331 gboolean include_internals,
332 GtkCallback callback,
333 gpointer callback_data);
335 /* FIXME probably need the focus methods. */
337 typedef struct _GtkTextViewChild GtkTextViewChild;
339 struct _GtkTextViewChild
343 GtkTextChildAnchor *anchor;
345 gint from_top_of_line;
346 gint from_left_of_buffer;
348 /* These are ignored if anchor != NULL */
349 GtkTextWindowType type;
354 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
355 GtkTextChildAnchor *anchor,
356 GtkTextLayout *layout);
357 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
358 GtkTextWindowType type,
361 static void text_view_child_free (GtkTextViewChild *child);
362 static void text_view_child_set_parent_window (GtkTextView *text_view,
363 GtkTextViewChild *child);
365 struct _GtkTextWindow
367 GtkTextWindowType type;
370 GdkWindow *bin_window;
371 GtkRequisition requisition;
372 GdkRectangle allocation;
375 static GtkTextWindow *text_window_new (GtkTextWindowType type,
378 gint height_request);
379 static void text_window_free (GtkTextWindow *win);
380 static void text_window_realize (GtkTextWindow *win,
382 static void text_window_unrealize (GtkTextWindow *win);
383 static void text_window_size_allocate (GtkTextWindow *win,
385 static void text_window_scroll (GtkTextWindow *win,
388 static void text_window_invalidate_rect (GtkTextWindow *win,
391 static gint text_window_get_width (GtkTextWindow *win);
392 static gint text_window_get_height (GtkTextWindow *win);
393 static void text_window_get_allocation (GtkTextWindow *win,
401 TARGET_COMPOUND_TEXT,
403 TARGET_TEXT_BUFFER_CONTENTS
406 static GtkTargetEntry target_table[] = {
407 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
408 TARGET_TEXT_BUFFER_CONTENTS },
409 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
410 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
411 { "TEXT", 0, TARGET_TEXT },
412 { "STRING", 0, TARGET_STRING }
415 static GtkContainerClass *parent_class = NULL;
416 static guint signals[LAST_SIGNAL] = { 0 };
419 gtk_text_view_get_type (void)
421 static GtkType our_type = 0;
425 static const GtkTypeInfo our_info =
428 sizeof (GtkTextView),
429 sizeof (GtkTextViewClass),
430 (GtkClassInitFunc) gtk_text_view_class_init,
431 (GtkObjectInitFunc) gtk_text_view_init,
432 /* reserved_1 */ NULL,
433 /* reserved_2 */ NULL,
434 (GtkClassInitFunc) NULL
437 our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
444 add_move_binding (GtkBindingSet *binding_set,
447 GtkMovementStep step,
450 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
452 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
456 GTK_TYPE_BOOL, FALSE);
458 /* Selection-extending version */
459 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
463 GTK_TYPE_BOOL, TRUE);
467 gtk_text_view_class_init (GtkTextViewClass *klass)
469 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
470 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
471 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
472 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
473 GtkBindingSet *binding_set;
475 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
477 /* Default handlers and virtual methods
479 gobject_class->set_property = gtk_text_view_set_property;
480 gobject_class->get_property = gtk_text_view_get_property;
482 object_class->destroy = gtk_text_view_destroy;
483 gobject_class->finalize = gtk_text_view_finalize;
485 widget_class->realize = gtk_text_view_realize;
486 widget_class->unrealize = gtk_text_view_unrealize;
487 widget_class->style_set = gtk_text_view_style_set;
488 widget_class->direction_changed = gtk_text_view_direction_changed;
489 widget_class->size_request = gtk_text_view_size_request;
490 widget_class->size_allocate = gtk_text_view_size_allocate;
491 widget_class->event = gtk_text_view_event;
492 widget_class->key_press_event = gtk_text_view_key_press_event;
493 widget_class->key_release_event = gtk_text_view_key_release_event;
494 widget_class->button_press_event = gtk_text_view_button_press_event;
495 widget_class->button_release_event = gtk_text_view_button_release_event;
496 widget_class->focus_in_event = gtk_text_view_focus_in_event;
497 widget_class->focus_out_event = gtk_text_view_focus_out_event;
498 widget_class->motion_notify_event = gtk_text_view_motion_event;
499 widget_class->expose_event = gtk_text_view_expose_event;
500 widget_class->grab_focus = gtk_text_view_grab_focus;
501 widget_class->focus = gtk_text_view_focus;
503 widget_class->drag_begin = gtk_text_view_drag_begin;
504 widget_class->drag_end = gtk_text_view_drag_end;
505 widget_class->drag_data_get = gtk_text_view_drag_data_get;
506 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
508 widget_class->drag_leave = gtk_text_view_drag_leave;
509 widget_class->drag_motion = gtk_text_view_drag_motion;
510 widget_class->drag_drop = gtk_text_view_drag_drop;
511 widget_class->drag_data_received = gtk_text_view_drag_data_received;
513 widget_class->popup_menu = gtk_text_view_popup_menu;
515 container_class->add = gtk_text_view_add;
516 container_class->remove = gtk_text_view_remove;
517 container_class->forall = gtk_text_view_forall;
519 klass->move_cursor = gtk_text_view_move_cursor;
520 klass->page_horizontally = gtk_text_view_page_horizontally;
521 klass->set_anchor = gtk_text_view_set_anchor;
522 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
523 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
524 klass->cut_clipboard = gtk_text_view_cut_clipboard;
525 klass->copy_clipboard = gtk_text_view_copy_clipboard;
526 klass->paste_clipboard = gtk_text_view_paste_clipboard;
527 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
528 klass->move_focus = gtk_text_view_move_focus;
529 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
535 g_object_class_install_property (gobject_class,
536 PROP_PIXELS_ABOVE_LINES,
537 g_param_spec_int ("pixels_above_lines",
538 _("Pixels Above Lines"),
539 _("Pixels of blank space above paragraphs"),
545 g_object_class_install_property (gobject_class,
546 PROP_PIXELS_BELOW_LINES,
547 g_param_spec_int ("pixels_below_lines",
548 _("Pixels Below Lines"),
549 _("Pixels of blank space below paragraphs"),
555 g_object_class_install_property (gobject_class,
556 PROP_PIXELS_INSIDE_WRAP,
557 g_param_spec_int ("pixels_inside_wrap",
558 _("Pixels Inside Wrap"),
559 _("Pixels of blank space between wrapped lines in a paragraph"),
565 g_object_class_install_property (gobject_class,
567 g_param_spec_boolean ("editable",
569 _("Whether the text can be modified by the user"),
573 g_object_class_install_property (gobject_class,
575 g_param_spec_enum ("wrap_mode",
577 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
582 g_object_class_install_property (gobject_class,
584 g_param_spec_enum ("justification",
586 _("Left, right, or center justification"),
587 GTK_TYPE_JUSTIFICATION,
591 g_object_class_install_property (gobject_class,
593 g_param_spec_int ("left_margin",
595 _("Width of the left margin in pixels"),
601 g_object_class_install_property (gobject_class,
603 g_param_spec_int ("right_margin",
605 _("Width of the right margin in pixels"),
611 g_object_class_install_property (gobject_class,
613 g_param_spec_int ("indent",
615 _("Amount to indent the paragraph, in pixels"),
621 g_object_class_install_property (gobject_class,
623 g_param_spec_boxed ("tabs",
625 _("Custom tabs for this text"),
626 PANGO_TYPE_TAB_ARRAY,
629 g_object_class_install_property (gobject_class,
631 g_param_spec_boolean ("cursor_visible",
633 _("If the insertion cursor is shown"),
642 signals[MOVE_CURSOR] =
643 gtk_signal_new ("move_cursor",
644 GTK_RUN_LAST | GTK_RUN_ACTION,
645 GTK_CLASS_TYPE (object_class),
646 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
647 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
648 GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
650 signals[PAGE_HORIZONTALLY] =
651 gtk_signal_new ("page_horizontally",
652 GTK_RUN_LAST | GTK_RUN_ACTION,
653 GTK_CLASS_TYPE (object_class),
654 GTK_SIGNAL_OFFSET (GtkTextViewClass, page_horizontally),
655 _gtk_marshal_VOID__INT_BOOLEAN,
656 GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_BOOL);
658 signals[SET_ANCHOR] =
659 gtk_signal_new ("set_anchor",
660 GTK_RUN_LAST | GTK_RUN_ACTION,
661 GTK_CLASS_TYPE (object_class),
662 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
663 _gtk_marshal_VOID__VOID,
666 signals[INSERT_AT_CURSOR] =
667 gtk_signal_new ("insert_at_cursor",
668 GTK_RUN_LAST | GTK_RUN_ACTION,
669 GTK_CLASS_TYPE (object_class),
670 GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
671 _gtk_marshal_VOID__STRING,
672 GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
674 signals[DELETE_FROM_CURSOR] =
675 gtk_signal_new ("delete_from_cursor",
676 GTK_RUN_LAST | GTK_RUN_ACTION,
677 GTK_CLASS_TYPE (object_class),
678 GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
679 _gtk_marshal_VOID__ENUM_INT,
680 GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
682 signals[CUT_CLIPBOARD] =
683 gtk_signal_new ("cut_clipboard",
684 GTK_RUN_LAST | GTK_RUN_ACTION,
685 GTK_CLASS_TYPE (object_class),
686 GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
687 _gtk_marshal_VOID__VOID,
690 signals[COPY_CLIPBOARD] =
691 gtk_signal_new ("copy_clipboard",
692 GTK_RUN_LAST | GTK_RUN_ACTION,
693 GTK_CLASS_TYPE (object_class),
694 GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
695 _gtk_marshal_VOID__VOID,
698 signals[PASTE_CLIPBOARD] =
699 gtk_signal_new ("paste_clipboard",
700 GTK_RUN_LAST | GTK_RUN_ACTION,
701 GTK_CLASS_TYPE (object_class),
702 GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
703 _gtk_marshal_VOID__VOID,
706 signals[TOGGLE_OVERWRITE] =
707 gtk_signal_new ("toggle_overwrite",
708 GTK_RUN_LAST | GTK_RUN_ACTION,
709 GTK_CLASS_TYPE (object_class),
710 GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
711 _gtk_marshal_VOID__VOID,
714 signals[MOVE_FOCUS] =
715 gtk_signal_new ("move_focus",
716 GTK_RUN_LAST | GTK_RUN_ACTION,
717 GTK_CLASS_TYPE (object_class),
718 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_focus),
719 _gtk_marshal_VOID__ENUM,
720 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
722 signals[SET_SCROLL_ADJUSTMENTS] =
723 gtk_signal_new ("set_scroll_adjustments",
725 GTK_CLASS_TYPE (object_class),
726 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
727 _gtk_marshal_VOID__OBJECT_OBJECT,
728 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
729 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
731 signals[POPULATE_POPUP] =
732 gtk_signal_new ("populate_popup",
734 GTK_CLASS_TYPE (object_class),
735 GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
736 _gtk_marshal_VOID__OBJECT,
737 GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
743 binding_set = gtk_binding_set_by_class (klass);
745 /* Moving the insertion point */
746 add_move_binding (binding_set, GDK_Right, 0,
747 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
749 add_move_binding (binding_set, GDK_KP_Right, 0,
750 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
752 add_move_binding (binding_set, GDK_Left, 0,
753 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
755 add_move_binding (binding_set, GDK_KP_Left, 0,
756 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
758 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
759 GTK_MOVEMENT_WORDS, 1);
761 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
762 GTK_MOVEMENT_WORDS, 1);
764 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
765 GTK_MOVEMENT_WORDS, -1);
767 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
768 GTK_MOVEMENT_WORDS, 1);
770 add_move_binding (binding_set, GDK_Up, 0,
771 GTK_MOVEMENT_DISPLAY_LINES, -1);
773 add_move_binding (binding_set, GDK_KP_Up, 0,
774 GTK_MOVEMENT_DISPLAY_LINES, -1);
776 add_move_binding (binding_set, GDK_Down, 0,
777 GTK_MOVEMENT_DISPLAY_LINES, 1);
779 add_move_binding (binding_set, GDK_KP_Down, 0,
780 GTK_MOVEMENT_DISPLAY_LINES, 1);
782 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
783 GTK_MOVEMENT_PARAGRAPHS, -1);
785 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
786 GTK_MOVEMENT_PARAGRAPHS, -1);
788 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
789 GTK_MOVEMENT_PARAGRAPHS, 1);
791 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
792 GTK_MOVEMENT_PARAGRAPHS, 1);
794 add_move_binding (binding_set, GDK_Home, 0,
795 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
797 add_move_binding (binding_set, GDK_KP_Home, 0,
798 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
800 add_move_binding (binding_set, GDK_End, 0,
801 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
803 add_move_binding (binding_set, GDK_KP_End, 0,
804 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
806 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
807 GTK_MOVEMENT_BUFFER_ENDS, -1);
809 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
810 GTK_MOVEMENT_BUFFER_ENDS, -1);
812 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
813 GTK_MOVEMENT_BUFFER_ENDS, 1);
815 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
816 GTK_MOVEMENT_BUFFER_ENDS, 1);
818 add_move_binding (binding_set, GDK_Page_Up, 0,
819 GTK_MOVEMENT_PAGES, -1);
821 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
822 GTK_MOVEMENT_PAGES, -1);
824 add_move_binding (binding_set, GDK_Page_Down, 0,
825 GTK_MOVEMENT_PAGES, 1);
827 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
828 GTK_MOVEMENT_PAGES, 1);
832 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
834 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
836 GTK_TYPE_BOOL, FALSE);
837 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
839 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
841 GTK_TYPE_BOOL, TRUE);
844 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
845 "page_horizontally", 2,
847 GTK_TYPE_BOOL, FALSE);
849 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
850 "page_horizontally", 2,
852 GTK_TYPE_BOOL, TRUE);
854 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
855 "page_horizontally", 2,
857 GTK_TYPE_BOOL, FALSE);
859 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
860 "page_horizontally", 2,
862 GTK_TYPE_BOOL, TRUE);
864 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
865 "page_horizontally", 2,
867 GTK_TYPE_BOOL, FALSE);
869 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
870 "page_horizontally", 2,
872 GTK_TYPE_BOOL, TRUE);
874 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
875 "page_horizontally", 2,
877 GTK_TYPE_BOOL, FALSE);
879 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
880 "page_horizontally", 2,
882 GTK_TYPE_BOOL, TRUE);
885 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
886 "delete_from_cursor", 2,
887 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
890 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
891 "delete_from_cursor", 2,
892 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
895 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
896 "delete_from_cursor", 2,
897 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
900 /* Make this do the same as Backspace, to help with mis-typing */
901 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
902 "delete_from_cursor", 2,
903 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
906 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
907 "delete_from_cursor", 2,
908 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
911 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
912 "delete_from_cursor", 2,
913 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
916 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
917 "delete_from_cursor", 2,
918 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
923 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
925 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
926 "copy_clipboard", 0);
927 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
928 "paste_clipboard", 0);
930 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
932 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
933 "copy_clipboard", 0);
934 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
935 "paste_clipboard", 0);
938 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
939 "toggle_overwrite", 0);
940 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
941 "toggle_overwrite", 0);
943 /* Control-tab focus motion */
944 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
946 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
947 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
949 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
951 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
953 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
954 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
956 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
960 gtk_text_view_init (GtkTextView *text_view)
964 widget = GTK_WIDGET (text_view);
966 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
968 /* Set up default style */
969 text_view->wrap_mode = GTK_WRAP_NONE;
970 text_view->pixels_above_lines = 0;
971 text_view->pixels_below_lines = 0;
972 text_view->pixels_inside_wrap = 0;
973 text_view->justify = GTK_JUSTIFY_LEFT;
974 text_view->left_margin = 0;
975 text_view->right_margin = 0;
976 text_view->indent = 0;
977 text_view->tabs = NULL;
978 text_view->editable = TRUE;
980 gtk_drag_dest_set (widget,
982 target_table, G_N_ELEMENTS (target_table),
983 GDK_ACTION_COPY | GDK_ACTION_MOVE);
985 text_view->virtual_cursor_x = -1;
986 text_view->virtual_cursor_y = -1;
988 /* This object is completely private. No external entity can gain a reference
989 * to it; so we create it here and destroy it in finalize ().
991 text_view->im_context = gtk_im_multicontext_new ();
993 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
994 G_CALLBACK (gtk_text_view_commit_handler), text_view);
995 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
996 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
997 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
998 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
999 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
1000 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1002 text_view->cursor_visible = TRUE;
1004 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1007 text_view->drag_start_x = -1;
1008 text_view->drag_start_y = -1;
1010 text_view->pending_place_cursor_button = 0;
1012 /* We handle all our own redrawing */
1013 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1017 * gtk_text_view_new:
1019 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1020 * before using the text view, an empty default buffer will be created
1021 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1022 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1024 * Return value: a new #GtkTextView
1027 gtk_text_view_new (void)
1029 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1033 * gtk_text_view_new_with_buffer:
1034 * @buffer: a #GtkTextBuffer
1036 * Creates a new #GtkTextView widget displaying the buffer
1037 * @buffer. One buffer can be shared among many widgets.
1038 * @buffer may be NULL to create a default buffer, in which case
1039 * this function is equivalent to gtk_text_view_new(). The
1040 * text view adds its own reference count to the buffer; it does not
1041 * take over an existing reference.
1043 * Return value: a new #GtkTextView.
1046 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1048 GtkTextView *text_view;
1050 text_view = (GtkTextView*)gtk_text_view_new ();
1052 gtk_text_view_set_buffer (text_view, buffer);
1054 return GTK_WIDGET (text_view);
1058 * gtk_text_view_set_buffer:
1059 * @text_view: a #GtkTextView
1060 * @buffer: a #GtkTextBuffer
1062 * Sets @buffer as the buffer being displayed by @text_view. The previous
1063 * buffer displayed by the text view is unreferenced, and a reference is
1064 * added to @buffer. If you owned a reference to @buffer before passing it
1065 * to this function, you must remove that reference yourself; #GtkTextView
1066 * will not "adopt" it.
1070 gtk_text_view_set_buffer (GtkTextView *text_view,
1071 GtkTextBuffer *buffer)
1073 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1074 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1076 if (text_view->buffer == buffer)
1079 if (text_view->buffer != NULL)
1081 /* Destroy all anchored children */
1085 copy = g_slist_copy (text_view->children);
1087 while (tmp_list != NULL)
1089 GtkTextViewChild *vc = tmp_list->data;
1093 gtk_widget_destroy (vc->widget);
1094 /* vc may now be invalid! */
1097 tmp_list = g_slist_next (tmp_list);
1100 g_slist_free (copy);
1102 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1103 (gpointer) gtk_text_view_mark_set_handler, text_view);
1104 g_object_unref (G_OBJECT (text_view->buffer));
1105 text_view->dnd_mark = NULL;
1107 if (GTK_WIDGET_REALIZED (text_view))
1109 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1110 GDK_SELECTION_PRIMARY);
1111 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1115 text_view->buffer = buffer;
1121 g_object_ref (G_OBJECT (buffer));
1123 if (text_view->layout)
1124 gtk_text_layout_set_buffer (text_view->layout, buffer);
1126 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1128 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1132 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1136 text_view->first_para_pixels = 0;
1138 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1139 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1141 if (GTK_WIDGET_REALIZED (text_view))
1143 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1144 GDK_SELECTION_PRIMARY);
1145 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1149 if (GTK_WIDGET_VISIBLE (text_view))
1150 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1152 DV(g_print ("Invalidating due to set_buffer\n"));
1153 gtk_text_view_invalidate (text_view);
1156 static GtkTextBuffer*
1157 get_buffer (GtkTextView *text_view)
1159 if (text_view->buffer == NULL)
1162 b = gtk_text_buffer_new (NULL);
1163 gtk_text_view_set_buffer (text_view, b);
1164 g_object_unref (G_OBJECT (b));
1167 return text_view->buffer;
1171 * gtk_text_view_get_buffer:
1172 * @text_view: a #GtkTextView
1174 * Returns the #GtkTextBuffer being displayed by this text view.
1175 * The reference count on the buffer is not incremented; the caller
1176 * of this function won't own a new reference.
1178 * Return value: a #GtkTextBuffer
1181 gtk_text_view_get_buffer (GtkTextView *text_view)
1183 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1185 return get_buffer (text_view);
1189 * gtk_text_view_get_iter_at_location:
1190 * @text_view: a #GtkTextView
1191 * @iter: a #GtkTextIter
1192 * @x: x position, in buffer coordinates
1193 * @y: y position, in buffer coordinates
1195 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1196 * coordinates are coordinates for the entire buffer, not just the
1197 * currently-displayed portion. If you have coordinates from an
1198 * event, you have to convert those to buffer coordinates with
1199 * gtk_text_view_window_to_buffer_coords().
1203 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1208 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1209 g_return_if_fail (iter != NULL);
1211 gtk_text_view_ensure_layout (text_view);
1213 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1220 * gtk_text_view_get_iter_location:
1221 * @text_view: a #GtkTextView
1222 * @iter: a #GtkTextIter
1223 * @location: bounds of the character at @iter
1225 * Gets a rectangle which roughly contains the character at @iter.
1226 * The rectangle position is in buffer coordinates; use
1227 * gtk_text_view_buffer_to_window_coords() to convert these
1228 * coordinates to coordinates for one of the windows in the text view.
1232 gtk_text_view_get_iter_location (GtkTextView *text_view,
1233 const GtkTextIter *iter,
1234 GdkRectangle *location)
1236 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1237 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1239 gtk_text_view_ensure_layout (text_view);
1241 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1245 * gtk_text_view_get_line_yrange:
1246 * @text_view: a #GtkTextView
1247 * @iter: a #GtkTextIter
1248 * @y: return location for a y coordinate
1249 * @height: return location for a height
1251 * Gets the y coordinate of the top of the line containing @iter,
1252 * and the height of the line. The coordinate is a buffer coordinate;
1253 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1257 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1258 const GtkTextIter *iter,
1262 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1263 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1265 gtk_text_view_ensure_layout (text_view);
1267 gtk_text_layout_get_line_yrange (text_view->layout,
1274 * gtk_text_view_get_line_at_y:
1275 * @text_view: a #GtkTextView
1276 * @target_iter: a #GtkTextIter
1277 * @y: a y coordinate
1278 * @line_top: return location for top coordinate of the line
1280 * Gets the #GtkTextIter at the start of the line containing
1281 * the coordinate @y. @y is in buffer coordinates, convert from
1282 * window coordinates with gtk_text_view_window_to_buffer_coords().
1283 * If non-%NULL, @line_top will be filled with the coordinate of the top
1287 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1288 GtkTextIter *target_iter,
1292 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1294 gtk_text_view_ensure_layout (text_view);
1296 gtk_text_layout_get_line_at_y (text_view->layout,
1303 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1305 DV (g_print (" Setting adj to raw value %g\n", val));
1307 /* We don't really want to clamp to upper; we want to clamp to
1308 upper - page_size which is the highest value the scrollbar
1309 will let us reach. */
1310 if (val > (adj->upper - adj->page_size))
1311 val = adj->upper - adj->page_size;
1313 if (val < adj->lower)
1316 if (val != adj->value)
1318 DV (g_print (" Setting adj to clamped value %g\n", val));
1319 gtk_adjustment_set_value (adj, val);
1327 * gtk_text_view_scroll_to_iter:
1328 * @text_view: a #GtkTextView
1329 * @iter: a #GtkTextIter
1330 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1331 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1332 * @xalign: horizontal alignment of mark within visible area.
1333 * @yalign: vertical alignment of mark within visible area
1335 * Scrolls @text_view so that @iter is on the screen in the position
1336 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1337 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1338 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1339 * possibly not scrolling at all. The effective screen for purposes
1340 * of this function is reduced by a margin of size @within_margin.
1341 * NOTE: This function uses the currently-computed height of the
1342 * lines in the text buffer. Note that line heights are computed
1343 * in an idle handler; so this function may not have the desired effect
1344 * if it's called before the height computations. To avoid oddness,
1345 * consider using gtk_text_view_scroll_to_mark() which saves a point
1346 * to be scrolled to after line validation.
1348 * Return value: %TRUE if scrolling occurred
1351 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1353 gdouble within_margin,
1359 GdkRectangle screen;
1364 gboolean retval = FALSE;
1366 gint screen_xoffset, screen_yoffset;
1367 gint current_x_scroll, current_y_scroll;
1369 /* FIXME why don't we do the validate-at-scroll-destination thing
1370 * from flush_scroll in this function? I think it wasn't done before
1371 * because changed_handler was screwed up, but I could be wrong.
1374 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1375 g_return_val_if_fail (iter != NULL, FALSE);
1376 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1377 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1378 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1380 widget = GTK_WIDGET (text_view);
1382 DV(g_print(G_STRLOC"\n"));
1384 gtk_text_layout_get_iter_location (text_view->layout,
1388 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1390 current_x_scroll = text_view->xoffset;
1391 current_y_scroll = text_view->yoffset;
1393 screen.x = current_x_scroll;
1394 screen.y = current_y_scroll;
1395 screen.width = SCREEN_WIDTH (widget);
1396 screen.height = SCREEN_HEIGHT (widget);
1398 screen_xoffset = screen.width * within_margin;
1399 screen_yoffset = screen.height * within_margin;
1401 screen.x += screen_xoffset;
1402 screen.y += screen_yoffset;
1403 screen.width -= screen_xoffset * 2;
1404 screen.height -= screen_yoffset * 2;
1406 /* paranoia check */
1407 if (screen.width < 1)
1409 if (screen.height < 1)
1412 screen_right = screen.x + screen.width;
1413 screen_bottom = screen.y + screen.height;
1415 /* The alignment affects the point in the target character that we
1416 * choose to align. If we're doing right/bottom alignment, we align
1417 * the right/bottom edge of the character the mark is at; if we're
1418 * doing left/top we align the left/top edge of the character; if
1419 * we're doing center alignment we align the center of the
1423 /* Vertical scroll */
1426 scroll_dest = current_y_scroll;
1430 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1432 /* if scroll_dest < screen.y, we move a negative increment (up),
1433 * else a positive increment (down)
1435 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1439 /* move minimum to get onscreen */
1440 if (rect.y < screen.y)
1442 scroll_dest = rect.y;
1443 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1445 else if ((rect.y + rect.height) > screen_bottom)
1447 scroll_dest = rect.y + rect.height;
1448 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1452 if (scroll_inc != 0)
1454 retval = set_adjustment_clamped (get_vadjustment (text_view),
1455 current_y_scroll + scroll_inc);
1457 DV (g_print (" vert increment %d\n", scroll_inc));
1460 /* Horizontal scroll */
1463 scroll_dest = current_x_scroll;
1467 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1469 /* if scroll_dest < screen.y, we move a negative increment (left),
1470 * else a positive increment (right)
1472 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1476 /* move minimum to get onscreen */
1477 if (rect.x < screen.x)
1479 scroll_dest = rect.x;
1480 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1482 else if ((rect.x + rect.width) > screen_right)
1484 scroll_dest = rect.x + rect.width;
1485 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1489 if (scroll_inc != 0)
1491 retval = set_adjustment_clamped (get_hadjustment (text_view),
1492 current_x_scroll + scroll_inc);
1494 DV (g_print (" horiz increment %d\n", scroll_inc));
1498 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1500 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1506 free_pending_scroll (GtkTextPendingScroll *scroll)
1508 if (!gtk_text_mark_get_deleted (scroll->mark))
1509 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1511 g_object_unref (G_OBJECT (scroll->mark));
1516 gtk_text_view_queue_scroll (GtkTextView *text_view,
1518 gdouble within_margin,
1524 GtkTextPendingScroll *scroll;
1526 DV(g_print(G_STRLOC"\n"));
1528 scroll = g_new (GtkTextPendingScroll, 1);
1530 scroll->within_margin = within_margin;
1531 scroll->use_align = use_align;
1532 scroll->xalign = xalign;
1533 scroll->yalign = yalign;
1535 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1537 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1540 gtk_text_mark_get_left_gravity (mark));
1542 g_object_ref (G_OBJECT (scroll->mark));
1544 if (text_view->pending_scroll)
1545 free_pending_scroll (text_view->pending_scroll);
1547 text_view->pending_scroll = scroll;
1551 gtk_text_view_flush_scroll (GtkTextView *text_view)
1554 GtkTextPendingScroll *scroll;
1558 widget = GTK_WIDGET (text_view);
1560 DV(g_print(G_STRLOC"\n"));
1562 if (text_view->pending_scroll == NULL)
1564 DV (g_print ("in flush scroll, no pending scroll\n"));
1568 scroll = text_view->pending_scroll;
1570 /* avoid recursion */
1571 text_view->pending_scroll = NULL;
1573 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1575 /* Validate area around the scroll destination, so the adjustment
1576 * can meaningfully point into that area. We must validate
1577 * enough area to be sure that after we scroll, everything onscreen
1578 * is valid; otherwise, validation will maintain the first para
1579 * in one place, but may push the target iter off the bottom of
1582 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1583 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1584 - (widget->allocation.height * 2),
1585 widget->allocation.height * 2);
1587 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1589 /* Ensure we have updated width/height */
1590 gtk_text_view_update_adjustments (text_view);
1592 retval = gtk_text_view_scroll_to_iter (text_view,
1594 scroll->within_margin,
1599 free_pending_scroll (scroll);
1605 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1607 if (upper != adj->upper)
1609 gdouble min = MAX (0.0, upper - adj->page_size);
1610 gboolean value_changed = FALSE;
1614 if (adj->value > min)
1617 value_changed = TRUE;
1620 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1621 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1625 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1626 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1632 gtk_text_view_update_adjustments (GtkTextView *text_view)
1634 gint width = 0, height = 0;
1636 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1638 if (text_view->layout)
1639 gtk_text_layout_get_size (text_view->layout, &width, &height);
1641 if (text_view->width != width || text_view->height != height)
1643 text_view->width = width;
1644 text_view->height = height;
1646 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1647 MAX (SCREEN_WIDTH (text_view), width));
1648 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1649 MAX (SCREEN_HEIGHT (text_view), height));
1651 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1653 /* Set up the step sizes; we'll say that a page is
1654 our allocation minus one step, and a step is
1655 1/10 of our allocation. */
1656 text_view->hadjustment->step_increment =
1657 SCREEN_WIDTH (text_view) / 10.0;
1658 text_view->hadjustment->page_increment =
1659 SCREEN_WIDTH (text_view) * 0.9;
1661 text_view->vadjustment->step_increment =
1662 SCREEN_HEIGHT (text_view) / 10.0;
1663 text_view->vadjustment->page_increment =
1664 SCREEN_HEIGHT (text_view) * 0.9;
1666 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1667 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1672 gtk_text_view_update_layout_width (GtkTextView *text_view)
1674 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1676 gtk_text_view_ensure_layout (text_view);
1678 gtk_text_layout_set_screen_width (text_view->layout,
1679 SCREEN_WIDTH (text_view));
1683 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1686 gint cursor_x_pos, cursor_y_pos;
1688 if (text_view->layout == NULL)
1691 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1693 area.x = cursor_x_pos;
1694 area.y = cursor_y_pos;
1695 area.width = area.height = 0;
1697 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1701 * gtk_text_view_scroll_to_mark:
1702 * @text_view: a #GtkTextView
1703 * @mark: a #GtkTextMark
1704 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1705 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1706 * @xalign: horizontal alignment of mark within visible area.
1707 * @yalign: vertical alignment of mark within visible area
1709 * Scrolls @text_view so that @mark is on the screen in the position
1710 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1711 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1712 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1713 * possibly not scrolling at all. The effective screen for purposes
1714 * of this function is reduced by a margin of size @within_margin.
1718 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1720 gdouble within_margin,
1725 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1726 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1727 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1728 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1729 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1731 gtk_text_view_queue_scroll (text_view, mark,
1737 /* If no validation is pending, we need to go ahead and force an
1740 if (text_view->layout &&
1741 gtk_text_layout_is_valid (text_view->layout))
1742 gtk_text_view_flush_scroll (text_view);
1746 * gtk_text_view_scroll_mark_onscreen:
1747 * @text_view: a #GtkTextView
1748 * @mark: a mark in the buffer for @text_view
1750 * Scrolls @text_view the minimum distance such that @mark is contained
1751 * within the visible area of the widget.
1755 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1758 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1759 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1761 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1765 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1767 GdkRectangle visible_rect;
1768 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1770 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1772 visible_rect.y + visible_rect.height);
1776 * gtk_text_view_move_mark_onscreen:
1777 * @text_view: a #GtkTextView
1778 * @mark: a #GtkTextMark
1780 * Moves a mark within the buffer so that it's
1781 * located within the currently-visible text area.
1783 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1786 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1791 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1792 g_return_val_if_fail (mark != NULL, FALSE);
1794 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1796 if (clamp_iter_onscreen (text_view, &iter))
1798 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1806 * gtk_text_view_get_visible_rect:
1807 * @text_view: a #GtkTextView
1808 * @visible_rect: rectangle to fill
1810 * Fills @visible_rect with the currently-visible
1811 * region of the buffer, in buffer coordinates. Convert to window coordinates
1812 * with gtk_text_view_buffer_to_window_coords().
1815 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1816 GdkRectangle *visible_rect)
1820 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1822 widget = GTK_WIDGET (text_view);
1826 visible_rect->x = text_view->xoffset;
1827 visible_rect->y = text_view->yoffset;
1828 visible_rect->width = SCREEN_WIDTH (widget);
1829 visible_rect->height = SCREEN_HEIGHT (widget);
1831 DV(g_print(" visible rect: %d,%d %d x %d\n",
1834 visible_rect->width,
1835 visible_rect->height));
1840 * gtk_text_view_set_wrap_mode:
1841 * @text_view: a #GtkTextView
1842 * @wrap_mode: a #GtkWrapMode
1844 * Sets the line wrapping for the view.
1847 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1848 GtkWrapMode wrap_mode)
1850 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1852 if (text_view->wrap_mode != wrap_mode)
1854 text_view->wrap_mode = wrap_mode;
1856 if (text_view->layout)
1858 text_view->layout->default_style->wrap_mode = wrap_mode;
1859 gtk_text_layout_default_style_changed (text_view->layout);
1863 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1867 * gtk_text_view_get_wrap_mode:
1868 * @text_view: a #GtkTextView
1870 * Gets the line wrapping for the view.
1872 * Return value: the line wrap setting
1875 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1877 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1879 return text_view->wrap_mode;
1883 * gtk_text_view_set_editable:
1884 * @text_view: a #GtkTextView
1885 * @setting: whether it's editable
1887 * Sets the default editability of the #GtkTextView. You can override
1888 * this default setting with tags in the buffer, using the "editable"
1889 * attribute of tags.
1892 gtk_text_view_set_editable (GtkTextView *text_view,
1895 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1896 setting = setting != FALSE;
1898 if (text_view->editable != setting)
1900 text_view->editable = setting;
1902 if (text_view->layout)
1904 text_view->layout->default_style->editable = text_view->editable;
1905 gtk_text_layout_default_style_changed (text_view->layout);
1908 g_object_notify (G_OBJECT (text_view), "editable");
1913 * gtk_text_view_get_editable:
1914 * @text_view: a #GtkTextView
1916 * Returns the default editability of the #GtkTextView. Tags in the
1917 * buffer may override this setting for some ranges of text.
1919 * Return value: whether text is editable by default
1922 gtk_text_view_get_editable (GtkTextView *text_view)
1924 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1926 return text_view->editable;
1930 * gtk_text_view_set_pixels_above_lines:
1931 * @text_view: a #GtkTextView
1932 * @pixels_above_lines: pixels above paragraphs
1934 * Sets the default number of blank pixels above paragraphs in @text_view.
1935 * Tags in the buffer for @text_view may override the defaults.
1939 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1940 gint pixels_above_lines)
1942 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1944 if (text_view->pixels_above_lines != pixels_above_lines)
1946 text_view->pixels_above_lines = pixels_above_lines;
1948 if (text_view->layout)
1950 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1951 gtk_text_layout_default_style_changed (text_view->layout);
1954 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1959 * gtk_text_view_get_pixels_above_lines:
1960 * @text_view: a #GtkTextView
1962 * Gets the default number of pixels to put above paragraphs.
1964 * Return value: default number of pixels above paragraphs
1967 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1969 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1971 return text_view->pixels_above_lines;
1975 * gtk_text_view_set_pixels_below_lines:
1976 * @text_view: a #GtkTextView
1977 * @pixels_below_lines: pixels below paragraphs
1979 * Sets the default number of pixels of blank space
1980 * to put below paragraphs in @text_view. May be overridden
1981 * by tags applied to @text_view's buffer.
1985 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1986 gint pixels_below_lines)
1988 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1990 if (text_view->pixels_below_lines != pixels_below_lines)
1992 text_view->pixels_below_lines = pixels_below_lines;
1994 if (text_view->layout)
1996 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
1997 gtk_text_layout_default_style_changed (text_view->layout);
2000 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
2005 * gtk_text_view_get_pixels_below_lines:
2006 * @text_view: a #GtkTextView
2008 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2010 * Return value: default number of blank pixels below paragraphs
2013 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2015 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2017 return text_view->pixels_below_lines;
2021 * gtk_text_view_set_pixels_inside_wrap:
2022 * @text_view: a #GtkTextView
2023 * @pixels_inside_wrap: default number of pixels between wrapped lines
2025 * Sets the default number of pixels of blank space to leave between
2026 * display/wrapped lines within a paragraph. May be overridden by
2027 * tags in @text_view's buffer.
2031 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2032 gint pixels_inside_wrap)
2034 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2036 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2038 text_view->pixels_inside_wrap = pixels_inside_wrap;
2040 if (text_view->layout)
2042 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2043 gtk_text_layout_default_style_changed (text_view->layout);
2046 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2051 * gtk_text_view_get_pixels_inside_wrap:
2052 * @text_view: a #GtkTextView
2054 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2056 * Return value: default number of pixels of blank space between wrapped lines
2059 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2061 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2063 return text_view->pixels_inside_wrap;
2067 * gtk_text_view_set_justification:
2068 * @text_view: a #GtkTextView
2069 * @justification: justification
2071 * Sets the default justification of text in @text_view.
2072 * Tags in the view's buffer may override the default.
2076 gtk_text_view_set_justification (GtkTextView *text_view,
2077 GtkJustification justification)
2079 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2081 if (text_view->justify != justification)
2083 text_view->justify = justification;
2085 if (text_view->layout)
2087 text_view->layout->default_style->justification = justification;
2088 gtk_text_layout_default_style_changed (text_view->layout);
2091 g_object_notify (G_OBJECT (text_view), "justification");
2096 * gtk_text_view_get_justification:
2097 * @text_view: a #GtkTextView
2099 * Gets the default justification of paragraphs in @text_view.
2100 * Tags in the buffer may override the default.
2102 * Return value: default justification
2105 gtk_text_view_get_justification (GtkTextView *text_view)
2107 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2109 return text_view->justify;
2113 * gtk_text_view_set_left_margin:
2114 * @text_view: a #GtkTextView
2115 * @left_margin: left margin in pixels
2117 * Sets the default left margin for text in @text_view.
2118 * Tags in the buffer may override the default.
2122 gtk_text_view_set_left_margin (GtkTextView *text_view,
2125 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2127 if (text_view->left_margin != left_margin)
2129 text_view->left_margin = left_margin;
2131 if (text_view->layout)
2133 text_view->layout->default_style->left_margin = left_margin;
2134 gtk_text_layout_default_style_changed (text_view->layout);
2137 g_object_notify (G_OBJECT (text_view), "left_margin");
2142 * gtk_text_view_get_left_margin:
2143 * @text_view: a #GtkTextView
2145 * Gets the default left margin size of paragraphs in the @text_view.
2146 * Tags in the buffer may override the default.
2148 * Return value: left margin in pixels
2151 gtk_text_view_get_left_margin (GtkTextView *text_view)
2153 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2155 return text_view->left_margin;
2159 * gtk_text_view_set_right_margin:
2160 * @text_view: a #GtkTextView
2161 * @right_margin: right margin in pixels
2163 * Sets the default right margin for text in the text view.
2164 * Tags in the buffer may override the default.
2168 gtk_text_view_set_right_margin (GtkTextView *text_view,
2171 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2173 if (text_view->right_margin != right_margin)
2175 text_view->right_margin = right_margin;
2177 if (text_view->layout)
2179 text_view->layout->default_style->right_margin = right_margin;
2180 gtk_text_layout_default_style_changed (text_view->layout);
2183 g_object_notify (G_OBJECT (text_view), "right_margin");
2188 * gtk_text_view_get_right_margin:
2189 * @text_view: a #GtkTextView
2191 * Gets the default right margin for text in @text_view. Tags
2192 * in the buffer may override the default.
2194 * Return value: right margin in pixels
2197 gtk_text_view_get_right_margin (GtkTextView *text_view)
2199 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2201 return text_view->right_margin;
2205 * gtk_text_view_set_indent:
2206 * @text_view: a #GtkTextView
2207 * @indent: indentation in pixels
2209 * Sets the default indentation for paragraphs in @text_view.
2210 * Tags in the buffer may override the default.
2214 gtk_text_view_set_indent (GtkTextView *text_view,
2217 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2219 if (text_view->indent != indent)
2221 text_view->indent = indent;
2223 if (text_view->layout)
2225 text_view->layout->default_style->indent = indent;
2226 gtk_text_layout_default_style_changed (text_view->layout);
2229 g_object_notify (G_OBJECT (text_view), "indent");
2234 * gtk_text_view_get_indent:
2235 * @text_view: a #GtkTextView
2237 * Gets the default indentation of paragraphs in @text_view.
2238 * Tags in the view's buffer may override the default.
2239 * The indentation may be negative.
2241 * Return value: number of pixels of indentation
2244 gtk_text_view_get_indent (GtkTextView *text_view)
2246 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2248 return text_view->indent;
2252 * gtk_text_view_set_tabs:
2253 * @text_view: a #GtkTextView
2254 * @tabs: tabs as a #PangoTabArray
2256 * Sets the default tab stops for paragraphs in @text_view.
2257 * Tags in the buffer may override the default.
2261 gtk_text_view_set_tabs (GtkTextView *text_view,
2262 PangoTabArray *tabs)
2264 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2266 if (text_view->tabs)
2267 pango_tab_array_free (text_view->tabs);
2269 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2271 if (text_view->layout)
2273 /* some unkosher futzing in internal struct details... */
2274 if (text_view->layout->default_style->tabs)
2275 pango_tab_array_free (text_view->layout->default_style->tabs);
2277 text_view->layout->default_style->tabs =
2278 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2280 gtk_text_layout_default_style_changed (text_view->layout);
2283 g_object_notify (G_OBJECT (text_view), "tabs");
2287 * gtk_text_view_get_tabs:
2288 * @text_view: a #GtkTextView
2290 * Gets the default tabs for @text_view. Tags in the buffer may
2291 * override the defaults. The returned array will be %NULL if
2292 * "standard" (8-space) tabs are used. Free the return value
2293 * with pango_tab_array_free().
2295 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2298 gtk_text_view_get_tabs (GtkTextView *text_view)
2300 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2302 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2306 * gtk_text_view_set_cursor_visible:
2307 * @text_view: a #GtkTextView
2308 * @setting: whether to show the insertion cursor
2310 * Toggles whether the insertion point is displayed. A buffer with no editable
2311 * text probably shouldn't have a visible cursor, so you may want to turn
2315 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2318 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2320 setting = (setting != FALSE);
2322 if (text_view->cursor_visible != setting)
2324 text_view->cursor_visible = setting;
2326 if (GTK_WIDGET_HAS_FOCUS (text_view))
2328 if (text_view->layout)
2330 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2331 gtk_text_view_check_cursor_blink (text_view);
2335 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2340 * gtk_text_view_get_cursor_visible:
2341 * @text_view: a #GtkTextView
2343 * Find out whether the cursor is being displayed.
2345 * Return value: whether the insertion mark is visible
2348 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2350 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2352 return text_view->cursor_visible;
2357 * gtk_text_view_place_cursor_onscreen:
2358 * @text_view: a #GtkTextView
2360 * Moves the cursor to the currently visible region of the
2361 * buffer, it it isn't there already.
2363 * Return value: TRUE if the cursor had to be moved.
2366 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2370 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2372 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2373 gtk_text_buffer_get_mark (get_buffer (text_view),
2376 if (clamp_iter_onscreen (text_view, &insert))
2378 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2386 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2388 if (text_view->first_validate_idle != 0)
2390 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2391 g_source_remove (text_view->first_validate_idle);
2392 text_view->first_validate_idle = 0;
2395 if (text_view->incremental_validate_idle != 0)
2397 g_source_remove (text_view->incremental_validate_idle);
2398 text_view->incremental_validate_idle = 0;
2403 gtk_text_view_destroy (GtkObject *object)
2405 GtkTextView *text_view;
2406 GtkTextLayout *layout;
2408 text_view = GTK_TEXT_VIEW (object);
2410 layout = text_view->layout;
2412 gtk_text_view_remove_validate_idles (text_view);
2413 gtk_text_view_set_buffer (text_view, NULL);
2414 gtk_text_view_destroy_layout (text_view);
2416 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2420 gtk_text_view_finalize (GObject *object)
2422 GtkTextView *text_view;
2424 text_view = GTK_TEXT_VIEW (object);
2426 g_return_if_fail (text_view->buffer == NULL);
2428 gtk_text_view_destroy_layout (text_view);
2429 gtk_text_view_set_buffer (text_view, NULL);
2431 if (text_view->pending_scroll)
2433 free_pending_scroll (text_view->pending_scroll);
2434 text_view->pending_scroll = NULL;
2437 if (text_view->tabs)
2438 pango_tab_array_free (text_view->tabs);
2440 if (text_view->hadjustment)
2441 g_object_unref (G_OBJECT (text_view->hadjustment));
2442 if (text_view->vadjustment)
2443 g_object_unref (G_OBJECT (text_view->vadjustment));
2445 text_window_free (text_view->text_window);
2447 if (text_view->left_window)
2448 text_window_free (text_view->left_window);
2450 if (text_view->top_window)
2451 text_window_free (text_view->top_window);
2453 if (text_view->right_window)
2454 text_window_free (text_view->right_window);
2456 if (text_view->bottom_window)
2457 text_window_free (text_view->bottom_window);
2459 g_object_unref (G_OBJECT (text_view->im_context));
2461 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2465 gtk_text_view_set_property (GObject *object,
2467 const GValue *value,
2470 GtkTextView *text_view;
2472 text_view = GTK_TEXT_VIEW (object);
2476 case PROP_PIXELS_ABOVE_LINES:
2477 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2480 case PROP_PIXELS_BELOW_LINES:
2481 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2484 case PROP_PIXELS_INSIDE_WRAP:
2485 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2489 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2492 case PROP_WRAP_MODE:
2493 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2496 case PROP_JUSTIFICATION:
2497 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2500 case PROP_LEFT_MARGIN:
2501 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2504 case PROP_RIGHT_MARGIN:
2505 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2509 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2513 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2516 case PROP_CURSOR_VISIBLE:
2517 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2521 g_assert_not_reached ();
2527 gtk_text_view_get_property (GObject *object,
2532 GtkTextView *text_view;
2534 text_view = GTK_TEXT_VIEW (object);
2538 case PROP_PIXELS_ABOVE_LINES:
2539 g_value_set_int (value, text_view->pixels_above_lines);
2542 case PROP_PIXELS_BELOW_LINES:
2543 g_value_set_int (value, text_view->pixels_below_lines);
2546 case PROP_PIXELS_INSIDE_WRAP:
2547 g_value_set_int (value, text_view->pixels_inside_wrap);
2551 g_value_set_boolean (value, text_view->editable);
2554 case PROP_WRAP_MODE:
2555 g_value_set_enum (value, text_view->wrap_mode);
2558 case PROP_JUSTIFICATION:
2559 g_value_set_enum (value, text_view->justify);
2562 case PROP_LEFT_MARGIN:
2563 g_value_set_int (value, text_view->left_margin);
2566 case PROP_RIGHT_MARGIN:
2567 g_value_set_int (value, text_view->right_margin);
2571 g_value_set_int (value, text_view->indent);
2575 g_value_set_boxed (value, text_view->tabs);
2578 case PROP_CURSOR_VISIBLE:
2579 g_value_set_boolean (value, text_view->cursor_visible);
2583 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2589 gtk_text_view_size_request (GtkWidget *widget,
2590 GtkRequisition *requisition)
2592 GtkTextView *text_view;
2594 gint focus_edge_width;
2596 gboolean interior_focus;
2598 text_view = GTK_TEXT_VIEW (widget);
2600 gtk_widget_style_get (widget,
2601 "interior_focus", &interior_focus,
2602 "focus_line_width", &focus_width,
2606 focus_edge_width = 0;
2608 focus_edge_width = focus_width;
2610 if (text_view->layout)
2612 text_view->text_window->requisition.width = text_view->layout->width;
2613 text_view->text_window->requisition.height = text_view->layout->height;
2617 text_view->text_window->requisition.width = 0;
2618 text_view->text_window->requisition.height = 0;
2621 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2622 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2624 if (text_view->left_window)
2625 requisition->width += text_view->left_window->requisition.width;
2627 if (text_view->right_window)
2628 requisition->width += text_view->right_window->requisition.width;
2630 if (text_view->top_window)
2631 requisition->height += text_view->top_window->requisition.height;
2633 if (text_view->bottom_window)
2634 requisition->height += text_view->bottom_window->requisition.height;
2636 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2637 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2639 tmp_list = text_view->children;
2640 while (tmp_list != NULL)
2642 GtkTextViewChild *child = tmp_list->data;
2646 GtkRequisition child_req;
2647 GtkRequisition old_req;
2649 gtk_widget_get_child_requisition (child->widget, &old_req);
2651 gtk_widget_size_request (child->widget, &child_req);
2653 gtk_widget_get_child_requisition (child->widget, &child_req);
2655 /* Invalidate layout lines if required */
2656 if (text_view->layout &&
2657 (old_req.width != child_req.width ||
2658 old_req.height != child_req.height))
2659 gtk_text_child_anchor_queue_resize (child->anchor,
2664 GtkRequisition child_req;
2666 gtk_widget_size_request (child->widget, &child_req);
2669 tmp_list = g_slist_next (tmp_list);
2674 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2675 GtkTextViewChild *vc,
2676 GtkAllocation *allocation)
2682 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2686 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2689 buffer_y += vc->from_top_of_line;
2691 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2692 allocation->y = buffer_y - text_view->yoffset;
2694 gtk_widget_get_child_requisition (vc->widget, &req);
2695 allocation->width = req.width;
2696 allocation->height = req.height;
2700 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2701 GtkTextViewChild *vc)
2703 GtkAllocation allocation;
2705 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2707 gtk_widget_size_allocate (vc->widget, &allocation);
2710 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2712 vc->widget->allocation.x,
2713 vc->widget->allocation.y,
2714 text_view->yoffset);
2719 gtk_text_view_child_allocated (GtkTextLayout *layout,
2725 GtkTextViewChild *vc = NULL;
2726 GtkTextView *text_view = data;
2728 /* x,y is the position of the child from the top of the line, and
2729 * from the left of the buffer. We have to translate that into text
2730 * window coordinates, then size_allocate the child.
2733 vc = g_object_get_data (G_OBJECT (child),
2734 "gtk-text-view-child");
2736 g_assert (vc != NULL);
2738 DV (g_print ("child allocated at %d,%d\n", x, y));
2740 vc->from_left_of_buffer = x;
2741 vc->from_top_of_line = y;
2743 gtk_text_view_update_child_allocation (text_view, vc);
2747 gtk_text_view_allocate_children (GtkTextView *text_view)
2751 DV(g_print(G_STRLOC"\n"));
2753 tmp_list = text_view->children;
2754 while (tmp_list != NULL)
2756 GtkTextViewChild *child = tmp_list->data;
2760 /* We need to force-validate the regions containing
2763 GtkTextIter child_loc;
2764 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2768 gtk_text_layout_validate_yrange (text_view->layout,
2774 GtkAllocation allocation;
2775 GtkRequisition child_req;
2777 g_assert (child != NULL);
2779 allocation.x = child->x;
2780 allocation.y = child->y;
2782 gtk_widget_get_child_requisition (child->widget, &child_req);
2784 allocation.width = child_req.width;
2785 allocation.height = child_req.height;
2787 gtk_widget_size_allocate (child->widget, &allocation);
2790 tmp_list = g_slist_next (tmp_list);
2795 gtk_text_view_size_allocate (GtkWidget *widget,
2796 GtkAllocation *allocation)
2798 GtkTextView *text_view;
2799 GtkTextIter first_para;
2801 GtkAdjustment *vadj;
2802 gboolean yoffset_changed = FALSE;
2804 GdkRectangle text_rect;
2805 GdkRectangle left_rect;
2806 GdkRectangle right_rect;
2807 GdkRectangle top_rect;
2808 GdkRectangle bottom_rect;
2809 gint focus_edge_width;
2811 gboolean interior_focus;
2812 gboolean size_changed;
2814 text_view = GTK_TEXT_VIEW (widget);
2816 DV(g_print(G_STRLOC"\n"));
2819 widget->allocation.width != allocation->width ||
2820 widget->allocation.height != allocation->height;
2822 widget->allocation = *allocation;
2824 if (GTK_WIDGET_REALIZED (widget))
2826 gdk_window_move_resize (widget->window,
2827 allocation->x, allocation->y,
2828 allocation->width, allocation->height);
2831 /* distribute width/height among child windows. Ensure all
2832 * windows get at least a 1x1 allocation.
2835 gtk_widget_style_get (widget,
2836 "interior_focus", &interior_focus,
2837 "focus_line_width", &focus_width,
2841 focus_edge_width = 0;
2843 focus_edge_width = focus_width;
2845 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2847 if (text_view->left_window)
2848 left_rect.width = text_view->left_window->requisition.width;
2850 left_rect.width = 0;
2852 width -= left_rect.width;
2854 if (text_view->right_window)
2855 right_rect.width = text_view->right_window->requisition.width;
2857 right_rect.width = 0;
2859 width -= right_rect.width;
2861 text_rect.width = MAX (1, width);
2863 top_rect.width = text_rect.width;
2864 bottom_rect.width = text_rect.width;
2867 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2869 if (text_view->top_window)
2870 top_rect.height = text_view->top_window->requisition.height;
2872 top_rect.height = 0;
2874 height -= top_rect.height;
2876 if (text_view->bottom_window)
2877 bottom_rect.height = text_view->bottom_window->requisition.height;
2879 bottom_rect.height = 0;
2881 height -= bottom_rect.height;
2883 text_rect.height = MAX (1, height);
2885 left_rect.height = text_rect.height;
2886 right_rect.height = text_rect.height;
2889 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2890 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2892 text_rect.x = left_rect.x + left_rect.width;
2893 text_rect.y = top_rect.y + top_rect.height;
2895 left_rect.y = text_rect.y;
2896 right_rect.y = text_rect.y;
2898 top_rect.x = text_rect.x;
2899 bottom_rect.x = text_rect.x;
2901 right_rect.x = text_rect.x + text_rect.width;
2902 bottom_rect.y = text_rect.y + text_rect.height;
2904 text_window_size_allocate (text_view->text_window,
2907 if (text_view->left_window)
2908 text_window_size_allocate (text_view->left_window,
2911 if (text_view->right_window)
2912 text_window_size_allocate (text_view->right_window,
2915 if (text_view->top_window)
2916 text_window_size_allocate (text_view->top_window,
2919 if (text_view->bottom_window)
2920 text_window_size_allocate (text_view->bottom_window,
2923 gtk_text_view_update_layout_width (text_view);
2925 /* Note that this will do some layout validation */
2926 gtk_text_view_allocate_children (text_view);
2928 /* Now adjust the value of the adjustment to keep the cursor at the
2929 * same place in the buffer
2931 gtk_text_view_get_first_para_iter (text_view, &first_para);
2932 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2934 y += text_view->first_para_pixels;
2936 /* Ensure h/v adj exist */
2937 get_hadjustment (text_view);
2938 get_vadjustment (text_view);
2940 vadj = text_view->vadjustment;
2941 if (y > vadj->upper - vadj->page_size)
2942 y = MAX (0, vadj->upper - vadj->page_size);
2944 if (y != text_view->yoffset)
2947 yoffset_changed = TRUE;
2950 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2951 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
2952 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
2953 text_view->hadjustment->lower = 0;
2954 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2956 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2958 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2959 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2960 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2961 text_view->vadjustment->lower = 0;
2962 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2964 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2966 if (yoffset_changed)
2967 gtk_adjustment_value_changed (vadj);
2969 /* The GTK resize loop processes all the pending exposes right
2970 * after doing the resize stuff, so the idle sizer won't have a
2971 * chance to run. So we do the work here.
2973 gtk_text_view_flush_first_validate (text_view);
2975 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2978 if (size_changed && GTK_WIDGET_REALIZED (widget))
2979 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2983 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2986 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2987 text_view->first_para_mark);
2991 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2993 GtkWidget *widget = GTK_WIDGET (text_view);
2995 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
2997 if (SCREEN_HEIGHT (widget) > 0)
2999 GtkTextIter first_para;
3001 /* Be sure we've validated the stuff onscreen; if we
3002 * scrolled, these calls won't have any effect, because
3003 * they were called in the recursive validate_onscreen
3005 gtk_text_view_get_first_para_iter (text_view, &first_para);
3007 gtk_text_layout_validate_yrange (text_view->layout,
3010 text_view->first_para_pixels +
3011 SCREEN_HEIGHT (widget));
3014 text_view->onscreen_validated = TRUE;
3016 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3018 /* This can have the odd side effect of triggering a scroll, which should
3019 * flip "onscreen_validated" back to FALSE, but should also get us
3020 * back into this function to turn it on again.
3022 gtk_text_view_update_adjustments (text_view);
3024 g_assert (text_view->onscreen_validated);
3028 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3030 if (text_view->first_validate_idle == 0)
3033 /* Do this first, which means that if an "invalidate"
3034 * occurs during any of this process, a new first_validate_callback
3035 * will be installed, and we'll start again.
3037 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3038 g_source_remove (text_view->first_validate_idle);
3039 text_view->first_validate_idle = 0;
3041 /* be sure we have up-to-date screen size set on the
3044 gtk_text_view_update_layout_width (text_view);
3046 /* Bail out if we invalidated stuff; scrolling right away will just
3047 * confuse the issue.
3049 if (text_view->first_validate_idle != 0)
3051 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3055 /* scroll to any marks, if that's pending. This can
3056 * jump us to the validation codepath used for scrolling
3057 * onscreen, if so we bail out.
3059 if (!gtk_text_view_flush_scroll (text_view))
3060 gtk_text_view_validate_onscreen (text_view);
3062 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3064 g_assert (text_view->onscreen_validated);
3069 first_validate_callback (gpointer data)
3071 GtkTextView *text_view = data;
3073 GDK_THREADS_ENTER ();
3075 /* Note that some of this code is duplicated at the end of size_allocate,
3076 * keep in sync with that.
3079 DV(g_print(G_STRLOC"\n"));
3081 gtk_text_view_flush_first_validate (text_view);
3083 GDK_THREADS_LEAVE ();
3089 incremental_validate_callback (gpointer data)
3091 GtkTextView *text_view = data;
3092 gboolean result = TRUE;
3094 GDK_THREADS_ENTER ();
3096 DV(g_print(G_STRLOC"\n"));
3098 gtk_text_layout_validate (text_view->layout, 2000);
3100 gtk_text_view_update_adjustments (text_view);
3102 if (gtk_text_layout_is_valid (text_view->layout))
3104 text_view->incremental_validate_idle = 0;
3108 GDK_THREADS_LEAVE ();
3114 gtk_text_view_invalidate (GtkTextView *text_view)
3116 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3117 text_view->onscreen_validated));
3119 text_view->onscreen_validated = FALSE;
3121 /* We'll invalidate when the layout is created */
3122 if (text_view->layout == NULL)
3125 if (!text_view->first_validate_idle)
3127 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3128 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3129 text_view->first_validate_idle));
3132 if (!text_view->incremental_validate_idle)
3134 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3135 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3136 text_view->incremental_validate_idle));
3141 invalidated_handler (GtkTextLayout *layout,
3144 GtkTextView *text_view;
3146 text_view = GTK_TEXT_VIEW (data);
3148 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3149 gtk_text_view_invalidate (text_view);
3153 changed_handler (GtkTextLayout *layout,
3159 GtkTextView *text_view;
3161 GdkRectangle visible_rect;
3162 GdkRectangle redraw_rect;
3164 text_view = GTK_TEXT_VIEW (data);
3165 widget = GTK_WIDGET (data);
3167 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3169 if (GTK_WIDGET_REALIZED (text_view))
3171 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3173 redraw_rect.x = visible_rect.x;
3174 redraw_rect.width = visible_rect.width;
3175 redraw_rect.y = start_y;
3177 if (old_height == new_height)
3178 redraw_rect.height = old_height;
3179 else if (start_y + old_height > visible_rect.y)
3180 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3182 redraw_rect.height = 0;
3184 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3186 /* text_window_invalidate_rect() takes buffer coordinates */
3187 text_window_invalidate_rect (text_view->text_window,
3190 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3194 redraw_rect.height));
3196 if (text_view->left_window)
3197 text_window_invalidate_rect (text_view->left_window,
3199 if (text_view->right_window)
3200 text_window_invalidate_rect (text_view->right_window,
3202 if (text_view->top_window)
3203 text_window_invalidate_rect (text_view->top_window,
3205 if (text_view->bottom_window)
3206 text_window_invalidate_rect (text_view->bottom_window,
3209 gtk_text_view_update_im_spot_location (text_view);
3213 if (old_height != new_height)
3215 gboolean yoffset_changed = FALSE;
3217 int new_first_para_top;
3218 int old_first_para_top;
3221 /* If the bottom of the old area was above the top of the
3222 * screen, we need to scroll to keep the current top of the
3223 * screen in place. Remember that first_para_pixels is the
3224 * position of the top of the screen in coordinates relative to
3225 * the first paragraph onscreen.
3227 * In short we are adding the height delta of the portion of the
3228 * changed region above first_para_mark to text_view->yoffset.
3230 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3231 text_view->first_para_mark);
3233 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3235 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3237 if (new_first_para_top != old_first_para_top)
3239 text_view->yoffset += new_first_para_top - old_first_para_top;
3241 get_vadjustment (text_view)->value = text_view->yoffset;
3242 yoffset_changed = TRUE;
3245 if (yoffset_changed)
3247 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3248 gtk_adjustment_value_changed (get_vadjustment (text_view));
3251 /* FIXME be smarter about which anchored widgets we update */
3253 tmp_list = text_view->children;
3254 while (tmp_list != NULL)
3256 GtkTextViewChild *child = tmp_list->data;
3259 gtk_text_view_update_child_allocation (text_view, child);
3261 tmp_list = g_slist_next (tmp_list);
3266 GtkRequisition old_req;
3267 GtkRequisition new_req;
3269 old_req = widget->requisition;
3271 /* Use this instead of gtk_widget_size_request wrapper
3272 * to avoid the optimization which just returns widget->requisition
3273 * if a resize hasn't been queued.
3275 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3277 if (old_req.width != new_req.width ||
3278 old_req.height != new_req.height)
3280 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3281 _gtk_size_group_queue_resize (widget);
3287 gtk_text_view_realize (GtkWidget *widget)
3289 GtkTextView *text_view;
3290 GdkWindowAttr attributes;
3291 gint attributes_mask;
3294 text_view = GTK_TEXT_VIEW (widget);
3295 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3297 attributes.window_type = GDK_WINDOW_CHILD;
3298 attributes.x = widget->allocation.x;
3299 attributes.y = widget->allocation.y;
3300 attributes.width = widget->allocation.width;
3301 attributes.height = widget->allocation.height;
3302 attributes.wclass = GDK_INPUT_OUTPUT;
3303 attributes.visual = gtk_widget_get_visual (widget);
3304 attributes.colormap = gtk_widget_get_colormap (widget);
3305 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3307 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3309 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3310 &attributes, attributes_mask);
3311 gdk_window_set_user_data (widget->window, widget);
3313 /* must come before text_window_realize calls */
3314 widget->style = gtk_style_attach (widget->style, widget->window);
3316 gdk_window_set_background (widget->window,
3317 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3319 text_window_realize (text_view->text_window, widget->window);
3321 if (text_view->left_window)
3322 text_window_realize (text_view->left_window,
3325 if (text_view->top_window)
3326 text_window_realize (text_view->top_window,
3329 if (text_view->right_window)
3330 text_window_realize (text_view->right_window,
3333 if (text_view->bottom_window)
3334 text_window_realize (text_view->bottom_window,
3337 gtk_text_view_ensure_layout (text_view);
3339 if (text_view->buffer)
3341 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3342 GDK_SELECTION_PRIMARY);
3343 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3346 tmp_list = text_view->children;
3347 while (tmp_list != NULL)
3349 GtkTextViewChild *vc = tmp_list->data;
3351 text_view_child_set_parent_window (text_view, vc);
3353 tmp_list = tmp_list->next;
3358 gtk_text_view_unrealize (GtkWidget *widget)
3360 GtkTextView *text_view;
3362 text_view = GTK_TEXT_VIEW (widget);
3364 if (text_view->buffer)
3366 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3367 GDK_SELECTION_PRIMARY);
3368 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3371 gtk_text_view_remove_validate_idles (text_view);
3373 if (text_view->popup_menu)
3375 gtk_widget_destroy (text_view->popup_menu);
3376 text_view->popup_menu = NULL;
3379 text_window_unrealize (text_view->text_window);
3381 if (text_view->left_window)
3382 text_window_unrealize (text_view->left_window);
3384 if (text_view->top_window)
3385 text_window_unrealize (text_view->top_window);
3387 if (text_view->right_window)
3388 text_window_unrealize (text_view->right_window);
3390 if (text_view->bottom_window)
3391 text_window_unrealize (text_view->bottom_window);
3393 gtk_text_view_destroy_layout (text_view);
3395 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3399 gtk_text_view_style_set (GtkWidget *widget,
3400 GtkStyle *previous_style)
3402 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3404 if (GTK_WIDGET_REALIZED (widget))
3406 gdk_window_set_background (widget->window,
3407 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3409 gdk_window_set_background (text_view->text_window->bin_window,
3410 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3412 if (text_view->left_window)
3413 gdk_window_set_background (text_view->left_window->bin_window,
3414 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3415 if (text_view->right_window)
3416 gdk_window_set_background (text_view->right_window->bin_window,
3417 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3419 if (text_view->top_window)
3420 gdk_window_set_background (text_view->top_window->bin_window,
3421 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3423 if (text_view->bottom_window)
3424 gdk_window_set_background (text_view->bottom_window->bin_window,
3425 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3428 if (text_view->layout && previous_style)
3430 gtk_text_view_set_attributes_from_style (text_view,
3431 text_view->layout->default_style,
3433 gtk_text_layout_default_style_changed (text_view->layout);
3438 gtk_text_view_direction_changed (GtkWidget *widget,
3439 GtkTextDirection previous_direction)
3441 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3443 if (text_view->layout)
3445 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3446 gtk_text_layout_default_style_changed (text_view->layout);
3452 set_invisible_cursor (GdkWindow *window)
3454 GdkBitmap *empty_bitmap;
3457 char invisible_cursor_bits[] = { 0x0 };
3459 useless.red = useless.green = useless.blue = 0;
3462 empty_bitmap = gdk_bitmap_create_from_data (window,
3463 invisible_cursor_bits,
3466 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3471 gdk_window_set_cursor (window, cursor);
3473 gdk_cursor_unref (cursor);
3475 g_object_unref (empty_bitmap);
3479 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3481 if (text_view->mouse_cursor_obscured)
3484 set_invisible_cursor (text_view->text_window->bin_window);
3486 text_view->mouse_cursor_obscured = TRUE;
3494 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3497 switch (event->type)
3499 case GDK_MOTION_NOTIFY:
3500 *x = event->motion.x;
3501 *y = event->motion.y;
3505 case GDK_BUTTON_PRESS:
3506 case GDK_2BUTTON_PRESS:
3507 case GDK_3BUTTON_PRESS:
3508 case GDK_BUTTON_RELEASE:
3509 *x = event->button.x;
3510 *y = event->button.y;
3515 case GDK_KEY_RELEASE:
3516 case GDK_ENTER_NOTIFY:
3517 case GDK_LEAVE_NOTIFY:
3518 case GDK_PROPERTY_NOTIFY:
3519 case GDK_SELECTION_CLEAR:
3520 case GDK_SELECTION_REQUEST:
3521 case GDK_SELECTION_NOTIFY:
3522 case GDK_PROXIMITY_IN:
3523 case GDK_PROXIMITY_OUT:
3524 case GDK_DRAG_ENTER:
3525 case GDK_DRAG_LEAVE:
3526 case GDK_DRAG_MOTION:
3527 case GDK_DRAG_STATUS:
3528 case GDK_DROP_START:
3529 case GDK_DROP_FINISHED:
3539 emit_event_on_tags (GtkWidget *widget,
3545 gboolean retval = FALSE;
3546 GtkTextView *text_view;
3548 text_view = GTK_TEXT_VIEW (widget);
3550 tags = gtk_text_iter_get_tags (iter);
3555 GtkTextTag *tag = tmp->data;
3557 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3563 tmp = g_slist_next (tmp);
3566 g_slist_free (tags);
3572 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3574 GtkTextView *text_view;
3577 text_view = GTK_TEXT_VIEW (widget);
3579 if (text_view->layout == NULL ||
3580 get_buffer (text_view) == NULL)
3583 if (event->any.window != text_view->text_window->bin_window)
3586 if (get_event_coordinates (event, &x, &y))
3590 x += text_view->xoffset;
3591 y += text_view->yoffset;
3593 /* FIXME this is slow and we do it twice per event.
3594 * My favorite solution is to have GtkTextLayout cache
3595 * the last couple lookups.
3597 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3601 return emit_event_on_tags (widget, event, &iter);
3603 else if (event->type == GDK_KEY_PRESS ||
3604 event->type == GDK_KEY_RELEASE)
3606 GtkTextMark *insert;
3609 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3612 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3614 return emit_event_on_tags (widget, event, &iter);
3621 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3623 gboolean retval = FALSE;
3624 gboolean obscure = FALSE;
3625 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3626 GtkTextMark *insert;
3628 gboolean can_insert;
3630 if (text_view->layout == NULL ||
3631 get_buffer (text_view) == NULL)
3634 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3635 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3636 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3638 gtk_im_context_filter_keypress (text_view->im_context, event))
3640 text_view->need_im_reset = TRUE;
3645 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3646 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3648 /* use overall editability not can_insert, more predictable for users */
3649 else if (text_view->editable &&
3650 (event->keyval == GDK_Return ||
3651 event->keyval == GDK_KP_Enter))
3653 /* this won't actually insert the newline if the cursor isn't
3656 gtk_text_view_commit_text (text_view, "\n");
3661 /* Pass through Tab as literal tab, unless Control is held down */
3662 else if ((event->keyval == GDK_Tab ||
3663 event->keyval == GDK_KP_Tab) &&
3664 !(event->state & GDK_CONTROL_MASK))
3666 /* If the text widget isn't editable overall, move the focus
3669 if (text_view->editable)
3671 gtk_text_view_commit_text (text_view, "\t");
3675 gtk_text_view_move_focus (text_view,
3676 (event->state & GDK_SHIFT_MASK) ?
3677 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3685 gtk_text_view_obscure_mouse_cursor (text_view);
3687 gtk_text_view_pend_cursor_blink (text_view);
3693 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3695 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3696 GtkTextMark *insert;
3699 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3702 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3703 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3704 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3705 gtk_im_context_filter_keypress (text_view->im_context, event))
3707 text_view->need_im_reset = TRUE;
3711 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3715 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3717 GtkTextView *text_view;
3719 text_view = GTK_TEXT_VIEW (widget);
3721 text_view->disable_scroll_on_focus = TRUE;
3722 gtk_widget_grab_focus (widget);
3723 text_view->disable_scroll_on_focus = FALSE;
3725 if (event->window != text_view->text_window->bin_window)
3727 /* Remove selection if any. */
3728 gtk_text_view_unselect (text_view);
3734 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3735 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3736 else if (event->button == 3)
3737 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3740 if (event->type == GDK_BUTTON_PRESS)
3742 gtk_text_view_reset_im_context (text_view);
3744 if (event->button == 1)
3746 /* If we're in the selection, start a drag copy/move of the
3747 * selection; otherwise, start creating a new selection.
3750 GtkTextIter start, end;
3752 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3754 event->x + text_view->xoffset,
3755 event->y + text_view->yoffset);
3757 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3759 gtk_text_iter_in_range (&iter, &start, &end))
3761 text_view->drag_start_x = event->x;
3762 text_view->drag_start_y = event->y;
3763 text_view->pending_place_cursor_button = event->button;
3767 gtk_text_view_start_selection_drag (text_view, &iter, event);
3772 else if (event->button == 2)
3776 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3778 event->x + text_view->xoffset,
3779 event->y + text_view->yoffset);
3781 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3782 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3784 text_view->editable);
3787 else if (event->button == 3)
3789 gtk_text_view_do_popup (text_view, event);
3792 else if ((event->type == GDK_2BUTTON_PRESS ||
3793 event->type == GDK_3BUTTON_PRESS) &&
3796 GtkTextIter start, end;
3798 /* End the selection drag, otherwise we'd clear the new
3799 * word/line selection on button release
3801 gtk_text_view_end_selection_drag (text_view, event);
3803 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3805 event->x + text_view->xoffset,
3806 event->y + text_view->yoffset);
3810 if (event->type == GDK_2BUTTON_PRESS)
3812 if (gtk_text_iter_inside_word (&start))
3814 if (!gtk_text_iter_starts_word (&start))
3815 gtk_text_iter_backward_word_start (&start);
3817 if (!gtk_text_iter_ends_word (&end))
3818 gtk_text_iter_forward_word_end (&end);
3821 else if (event->type == GDK_3BUTTON_PRESS)
3823 if (gtk_text_view_starts_display_line (text_view, &start))
3825 /* If on a display line boundary, we assume the user
3826 * clicked off the end of a line and we therefore select
3827 * the line before the boundary.
3829 gtk_text_view_backward_display_line_start (text_view, &start);
3833 /* start isn't on the start of a line, so we move it to the
3834 * start, and move end to the end unless it's already there.
3836 gtk_text_view_backward_display_line_start (text_view, &start);
3838 if (!gtk_text_view_starts_display_line (text_view, &end))
3839 gtk_text_view_forward_display_line_end (text_view, &end);
3843 if (event->state & GDK_SHIFT_MASK)
3845 /* Take union of old and new selection */
3846 GtkTextIter old_start, old_end;
3848 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3849 &old_start, &old_end);
3851 gtk_text_iter_order (&start, &old_start);
3852 gtk_text_iter_order (&old_end, &end);
3854 /* Now start is the first of the starts, and end is the
3859 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3862 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3866 text_view->just_selected_element = TRUE;
3875 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3877 GtkTextView *text_view;
3879 text_view = GTK_TEXT_VIEW (widget);
3881 if (event->window != text_view->text_window->bin_window)
3884 if (event->button == 1)
3886 if (text_view->drag_start_x >= 0)
3888 text_view->drag_start_x = -1;
3889 text_view->drag_start_y = -1;
3892 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3894 else if (text_view->just_selected_element)
3896 text_view->just_selected_element = FALSE;
3899 else if (text_view->pending_place_cursor_button == event->button)
3903 /* Unselect everything; we clicked inside selection, but
3904 * didn't move by the drag threshold, so just clear selection
3907 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3909 event->x + text_view->xoffset,
3910 event->y + text_view->yoffset);
3912 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3914 text_view->pending_place_cursor_button = 0;
3924 keymap_direction_changed (GdkKeymap *keymap,
3925 GtkTextView *text_view)
3927 gtk_text_view_check_keymap_direction (text_view);
3931 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3933 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3935 gtk_widget_queue_draw (widget);
3937 DV(g_print (G_STRLOC": focus_in_event\n"));
3939 if (text_view->cursor_visible && text_view->layout)
3941 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3942 gtk_text_view_check_cursor_blink (text_view);
3945 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3946 "direction_changed",
3947 G_CALLBACK (keymap_direction_changed), text_view);
3948 gtk_text_view_check_keymap_direction (text_view);
3950 text_view->need_im_reset = TRUE;
3951 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3957 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3959 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3961 gtk_widget_queue_draw (widget);
3963 DV(g_print (G_STRLOC": focus_out_event\n"));
3965 if (text_view->cursor_visible && text_view->layout)
3967 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3968 gtk_text_view_check_cursor_blink (text_view);
3971 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3972 (gpointer) keymap_direction_changed,
3975 text_view->need_im_reset = TRUE;
3976 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3982 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3984 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3986 if (text_view->mouse_cursor_obscured)
3990 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
3992 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3993 gdk_cursor_unref (cursor);
3994 text_view->mouse_cursor_obscured = FALSE;
3997 if (event->window == text_view->text_window->bin_window &&
3998 text_view->drag_start_x >= 0)
4002 gdk_window_get_pointer (text_view->text_window->bin_window,
4005 if (gtk_drag_check_threshold (widget,
4006 text_view->drag_start_x,
4007 text_view->drag_start_y,
4011 gint buffer_x, buffer_y;
4013 gtk_text_view_window_to_buffer_coords (text_view,
4014 GTK_TEXT_WINDOW_TEXT,
4015 text_view->drag_start_x,
4016 text_view->drag_start_y,
4020 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4022 buffer_x, buffer_y);
4024 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4033 gtk_text_view_paint (GtkWidget *widget,
4035 GdkEventExpose *event)
4037 GtkTextView *text_view;
4038 GList *child_exposes;
4042 text_view = GTK_TEXT_VIEW (widget);
4044 g_return_if_fail (text_view->layout != NULL);
4045 g_return_if_fail (text_view->xoffset >= 0);
4046 g_return_if_fail (text_view->yoffset >= 0);
4048 while (text_view->first_validate_idle != 0)
4050 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4051 text_view->first_validate_idle));
4052 gtk_text_view_flush_first_validate (text_view);
4055 /* More regions could have become invalid in the above loop */
4056 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4061 gdk_region_get_clipbox (updates, &rect);
4063 gdk_rectangle_union (area, &rect, area);
4065 gdk_region_destroy (updates);
4068 if (!text_view->onscreen_validated)
4070 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.");
4071 g_assert_not_reached ();
4075 printf ("painting %d,%d %d x %d\n",
4077 area->width, area->height);
4080 child_exposes = NULL;
4081 gtk_text_layout_draw (text_view->layout,
4083 text_view->text_window->bin_window,
4088 area->width, area->height,
4091 tmp_list = child_exposes;
4092 while (tmp_list != NULL)
4094 GtkWidget *child = tmp_list->data;
4096 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4100 g_object_unref (G_OBJECT (child));
4102 tmp_list = tmp_list->next;
4105 g_list_free (child_exposes);
4109 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4113 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4114 GTK_TEXT_WINDOW_TEXT))
4116 DV(g_print (">Exposed ("G_STRLOC")\n"));
4117 gtk_text_view_paint (widget, &event->area, event);
4120 if (event->window == widget->window)
4121 gtk_text_view_draw_focus (widget);
4123 /* Propagate exposes to all children not in the buffer. */
4124 tmp_list = GTK_TEXT_VIEW (widget)->children;
4125 while (tmp_list != NULL)
4127 GtkTextViewChild *vc = tmp_list->data;
4129 /* propagate_expose checks that event->window matches
4132 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4133 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4137 tmp_list = tmp_list->next;
4144 gtk_text_view_draw_focus (GtkWidget *widget)
4146 gboolean interior_focus;
4148 /* We clear the focus if we are in interior focus mode. */
4149 gtk_widget_style_get (widget,
4150 "interior_focus", &interior_focus,
4153 if (GTK_WIDGET_DRAWABLE (widget))
4155 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4157 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4158 NULL, widget, "textview",
4160 widget->allocation.width,
4161 widget->allocation.height);
4165 gdk_window_clear (widget->window);
4171 gtk_text_view_grab_focus (GtkWidget *widget)
4173 GtkTextView *text_view;
4175 text_view = GTK_TEXT_VIEW (widget);
4177 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4179 if (!text_view->disable_scroll_on_focus)
4180 gtk_text_view_scroll_mark_onscreen (text_view,
4181 gtk_text_buffer_get_mark (get_buffer (text_view),
4186 gtk_text_view_focus (GtkWidget *widget,
4187 GtkDirectionType direction)
4189 GtkTextView *text_view;
4190 GtkContainer *container;
4192 text_view = GTK_TEXT_VIEW (widget);
4193 container = GTK_CONTAINER (widget);
4195 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4203 gtk_text_view_add (GtkContainer *container,
4206 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4207 g_return_if_fail (GTK_IS_WIDGET (child));
4209 /* This is pretty random. */
4210 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4212 GTK_TEXT_WINDOW_WIDGET,
4217 gtk_text_view_remove (GtkContainer *container,
4221 GtkTextView *text_view;
4222 GtkTextViewChild *vc;
4224 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4225 g_return_if_fail (GTK_IS_WIDGET (child));
4226 g_return_if_fail (child->parent == (GtkWidget*) container);
4228 text_view = GTK_TEXT_VIEW (container);
4231 iter = text_view->children;
4233 while (iter != NULL)
4237 if (vc->widget == child)
4240 iter = g_slist_next (iter);
4243 g_assert (iter != NULL); /* be sure we had the child in the list */
4245 text_view->children = g_slist_remove (text_view->children, vc);
4247 gtk_widget_unparent (vc->widget);
4249 text_view_child_free (vc);
4253 gtk_text_view_forall (GtkContainer *container,
4254 gboolean include_internals,
4255 GtkCallback callback,
4256 gpointer callback_data)
4259 GtkTextView *text_view;
4262 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4263 g_return_if_fail (callback != NULL);
4265 text_view = GTK_TEXT_VIEW (container);
4267 copy = g_slist_copy (text_view->children);
4270 while (iter != NULL)
4272 GtkTextViewChild *vc = iter->data;
4274 (* callback) (vc->widget, callback_data);
4276 iter = g_slist_next (iter);
4279 g_slist_free (copy);
4282 #define CURSOR_ON_MULTIPLIER 0.66
4283 #define CURSOR_OFF_MULTIPLIER 0.34
4284 #define CURSOR_PEND_MULTIPLIER 1.0
4287 cursor_blinks (GtkTextView *text_view)
4289 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4292 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4295 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4298 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4303 get_cursor_time (GtkTextView *text_view)
4305 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4308 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4318 blink_cb (gpointer data)
4320 GtkTextView *text_view;
4323 GDK_THREADS_ENTER ();
4325 text_view = GTK_TEXT_VIEW (data);
4327 g_assert (text_view->layout);
4328 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4329 g_assert (text_view->cursor_visible);
4331 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4334 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4338 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4342 gtk_text_layout_set_cursor_visible (text_view->layout,
4345 GDK_THREADS_LEAVE ();
4347 /* Remove ourselves */
4353 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4355 if (text_view->blink_timeout)
4357 gtk_timeout_remove (text_view->blink_timeout);
4358 text_view->blink_timeout = 0;
4363 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4365 if (text_view->layout != NULL &&
4366 text_view->cursor_visible &&
4367 GTK_WIDGET_HAS_FOCUS (text_view))
4369 if (cursor_blinks (text_view))
4371 if (text_view->blink_timeout == 0)
4373 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4375 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4381 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4385 gtk_text_view_stop_cursor_blink (text_view);
4390 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4392 if (text_view->layout != NULL &&
4393 text_view->cursor_visible &&
4394 GTK_WIDGET_HAS_FOCUS (text_view) &&
4395 cursor_blinks (text_view))
4397 if (text_view->blink_timeout != 0)
4399 gtk_timeout_remove (text_view->blink_timeout);
4400 text_view->blink_timeout = 0;
4403 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4405 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4413 * Key binding handlers
4417 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4418 GtkTextIter *newplace,
4423 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4429 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4435 move_cursor (GtkTextView *text_view,
4436 const GtkTextIter *new_location,
4437 gboolean extend_selection)
4439 if (extend_selection)
4440 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4444 gtk_text_buffer_place_cursor (get_buffer (text_view),
4448 /* FIXME when we are unfrozen and can change GtkMovementStep,
4451 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4454 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4455 GtkMovementStep step,
4457 gboolean extend_selection)
4460 GtkTextIter newplace;
4462 gint cursor_x_pos = 0;
4464 gtk_text_view_reset_im_context (text_view);
4466 if (step == GTK_MOVEMENT_PAGES)
4468 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4469 gtk_text_view_pend_cursor_blink (text_view);
4472 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4474 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4475 gtk_text_view_pend_cursor_blink (text_view);
4479 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4480 gtk_text_buffer_get_mark (get_buffer (text_view),
4484 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4485 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4489 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4490 gtk_text_iter_forward_cursor_positions (&newplace, count);
4493 case GTK_MOVEMENT_VISUAL_POSITIONS:
4494 gtk_text_layout_move_iter_visually (text_view->layout,
4498 case GTK_MOVEMENT_WORDS:
4500 gtk_text_iter_backward_word_starts (&newplace, -count);
4502 gtk_text_iter_forward_word_ends (&newplace, count);
4505 case GTK_MOVEMENT_DISPLAY_LINES:
4506 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4507 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4510 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4512 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4513 else if (count < -1)
4514 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4517 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4520 case GTK_MOVEMENT_PARAGRAPHS:
4523 if (!gtk_text_iter_ends_line (&newplace))
4525 gtk_text_iter_forward_to_line_end (&newplace);
4531 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4533 gtk_text_iter_set_line_offset (&newplace, 0);
4540 gtk_text_iter_forward_lines (&newplace, count);
4541 gtk_text_iter_set_line_offset (&newplace, 0);
4545 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4548 if (!gtk_text_iter_ends_line (&newplace))
4549 gtk_text_iter_forward_to_line_end (&newplace);
4553 gtk_text_iter_set_line_offset (&newplace, 0);
4557 case GTK_MOVEMENT_BUFFER_ENDS:
4559 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4561 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4568 if (!gtk_text_iter_equal (&insert, &newplace))
4570 move_cursor (text_view, &newplace, extend_selection);
4572 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4573 gtk_text_view_scroll_mark_onscreen (text_view,
4574 gtk_text_buffer_get_mark (get_buffer (text_view),
4577 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4579 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4583 gtk_text_view_pend_cursor_blink (text_view);
4587 gtk_text_view_move_cursor (GtkTextView *text_view,
4588 GtkMovementStep step,
4590 gboolean extend_selection)
4592 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4596 gtk_text_view_page_horizontally (GtkTextView *text_view,
4598 gboolean extend_selection)
4600 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4601 count, extend_selection);
4605 gtk_text_view_set_anchor (GtkTextView *text_view)
4609 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4610 gtk_text_buffer_get_mark (get_buffer (text_view),
4613 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4617 gtk_text_view_scroll_pages (GtkTextView *text_view,
4619 gboolean extend_selection)
4624 gint cursor_x_pos, cursor_y_pos;
4625 GtkTextIter new_insert;
4629 g_return_if_fail (text_view->vadjustment != NULL);
4631 adj = text_view->vadjustment;
4633 /* Validate the region that will be brought into view by the cursor motion
4637 gtk_text_view_get_first_para_iter (text_view, &anchor);
4638 y0 = adj->page_size;
4639 y1 = adj->page_size + count * adj->page_increment;
4643 gtk_text_view_get_first_para_iter (text_view, &anchor);
4644 y0 = count * adj->page_increment + adj->page_size;
4648 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4649 /* FIXME do we need to update the adjustment ranges here? */
4651 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4653 /* already at top, just be sure we are at offset 0 */
4654 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4655 move_cursor (text_view, &new_insert, extend_selection);
4657 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4659 /* already at bottom, just be sure we are at the end */
4660 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4661 move_cursor (text_view, &new_insert, extend_selection);
4665 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4667 newval = adj->value;
4668 oldval = adj->value;
4670 newval += count * adj->page_increment;
4672 set_adjustment_clamped (adj, newval);
4673 cursor_y_pos += adj->value - oldval;
4675 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4676 clamp_iter_onscreen (text_view, &new_insert);
4677 move_cursor (text_view, &new_insert, extend_selection);
4679 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4682 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4683 * only guarantees 1 pixel onscreen.
4685 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4686 gtk_text_view_scroll_mark_onscreen (text_view,
4687 gtk_text_buffer_get_mark (get_buffer (text_view),
4692 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4694 gboolean extend_selection)
4699 gint cursor_x_pos, cursor_y_pos;
4700 GtkTextIter new_insert;
4703 g_return_if_fail (text_view->hadjustment != NULL);
4705 adj = text_view->hadjustment;
4707 /* Validate the line that we're moving within.
4709 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4711 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4712 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4713 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4714 /* FIXME do we need to update the adjustment ranges here? */
4716 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4718 /* already at far left, just be sure we are at offset 0 */
4719 gtk_text_iter_set_line_offset (&new_insert, 0);
4720 move_cursor (text_view, &new_insert, extend_selection);
4722 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4724 /* already at far right, just be sure we are at the end */
4725 gtk_text_iter_forward_to_line_end (&new_insert);
4726 move_cursor (text_view, &new_insert, extend_selection);
4730 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4732 newval = adj->value;
4733 oldval = adj->value;
4735 newval += count * adj->page_increment;
4737 set_adjustment_clamped (adj, newval);
4738 cursor_x_pos += adj->value - oldval;
4740 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4741 clamp_iter_onscreen (text_view, &new_insert);
4742 move_cursor (text_view, &new_insert, extend_selection);
4744 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4747 /* FIXME for lines shorter than the overall widget width, this results in a
4748 * "bounce" effect as we scroll to the right of the widget, then scroll
4749 * back to get the end of the line onscreen.
4750 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4753 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4754 * only guarantees 1 pixel onscreen.
4756 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4757 gtk_text_view_scroll_mark_onscreen (text_view,
4758 gtk_text_buffer_get_mark (get_buffer (text_view),
4763 whitespace (gunichar ch, gpointer user_data)
4765 return (ch == ' ' || ch == '\t');
4769 not_whitespace (gunichar ch, gpointer user_data)
4771 return !whitespace (ch, user_data);
4775 find_whitepace_region (const GtkTextIter *center,
4776 GtkTextIter *start, GtkTextIter *end)
4781 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4782 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4783 if (whitespace (gtk_text_iter_get_char (end), NULL))
4784 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4786 return !gtk_text_iter_equal (start, end);
4790 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4793 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4794 text_view->editable);
4798 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4805 gboolean leave_one = FALSE;
4807 gtk_text_view_reset_im_context (text_view);
4809 if (type == GTK_DELETE_CHARS)
4811 /* Char delete deletes the selection, if one exists */
4812 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4813 text_view->editable))
4817 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4819 gtk_text_buffer_get_mark (get_buffer (text_view),
4827 case GTK_DELETE_CHARS:
4828 gtk_text_iter_forward_cursor_positions (&end, count);
4831 case GTK_DELETE_WORD_ENDS:
4833 gtk_text_iter_forward_word_ends (&end, count);
4835 gtk_text_iter_backward_word_starts (&start, 0 - count);
4838 case GTK_DELETE_WORDS:
4841 case GTK_DELETE_DISPLAY_LINE_ENDS:
4844 case GTK_DELETE_DISPLAY_LINES:
4847 case GTK_DELETE_PARAGRAPH_ENDS:
4848 /* If we're already at a newline, we need to
4849 * simply delete that newline, instead of
4850 * moving to the next one.
4852 if (gtk_text_iter_ends_line (&end))
4854 gtk_text_iter_forward_line (&end);
4860 if (!gtk_text_iter_forward_to_line_end (&end))
4866 /* FIXME figure out what a negative count means
4870 case GTK_DELETE_PARAGRAPHS:
4873 gtk_text_iter_set_line_offset (&start, 0);
4874 gtk_text_iter_forward_to_line_end (&end);
4876 /* Do the lines beyond the first. */
4879 gtk_text_iter_forward_to_line_end (&end);
4885 /* FIXME negative count? */
4889 case GTK_DELETE_WHITESPACE:
4891 find_whitepace_region (&insert, &start, &end);
4899 if (!gtk_text_iter_equal (&start, &end))
4901 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4903 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4904 text_view->editable))
4907 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4909 text_view->editable);
4912 gtk_text_buffer_end_user_action (get_buffer (text_view));
4914 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4915 gtk_text_view_scroll_mark_onscreen (text_view,
4916 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4921 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4923 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4924 GDK_SELECTION_CLIPBOARD);
4926 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4928 text_view->editable);
4929 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4930 gtk_text_view_scroll_mark_onscreen (text_view,
4931 gtk_text_buffer_get_mark (get_buffer (text_view),
4936 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4938 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4939 GDK_SELECTION_CLIPBOARD);
4941 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4943 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4944 gtk_text_view_scroll_mark_onscreen (text_view,
4945 gtk_text_buffer_get_mark (get_buffer (text_view),
4950 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4952 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4953 GDK_SELECTION_CLIPBOARD);
4955 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4958 text_view->editable);
4959 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4960 gtk_text_view_scroll_mark_onscreen (text_view,
4961 gtk_text_buffer_get_mark (get_buffer (text_view),
4966 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4968 text_view->overwrite_mode = !text_view->overwrite_mode;
4972 gtk_text_view_move_focus (GtkTextView *text_view,
4973 GtkDirectionType direction_type)
4975 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
4977 if (!GTK_WIDGET_TOPLEVEL (toplevel))
4980 /* Propagate to toplevel */
4981 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus", direction_type);
4989 gtk_text_view_unselect (GtkTextView *text_view)
4993 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4995 gtk_text_buffer_get_mark (get_buffer (text_view),
4998 gtk_text_buffer_move_mark (get_buffer (text_view),
4999 gtk_text_buffer_get_mark (get_buffer (text_view),
5005 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5006 const gchar *mark_name)
5009 GdkModifierType state;
5010 GtkTextIter newplace;
5012 /* DV(g_print (G_STRLOC": begin\n")); */
5014 gdk_window_get_pointer (text_view->text_window->bin_window,
5017 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5018 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5020 x + text_view->xoffset,
5021 y + text_view->yoffset);
5025 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5027 /* This may invalidate the layout */
5028 DV(g_print (G_STRLOC": move mark\n"));
5029 gtk_text_buffer_move_mark (get_buffer (text_view),
5033 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5034 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5037 DV (g_print ("first validate idle leaving %s is %d\n",
5038 G_STRLOC, text_view->first_validate_idle));
5042 selection_scan_timeout (gpointer data)
5044 GtkTextView *text_view;
5046 GDK_THREADS_ENTER ();
5048 text_view = GTK_TEXT_VIEW (data);
5050 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5051 move_mark_to_pointer_and_scroll (text_view, "insert");
5053 GDK_THREADS_LEAVE ();
5055 return TRUE; /* remain installed. */
5058 #define DND_SCROLL_MARGIN 0.20
5061 drag_scan_timeout (gpointer data)
5063 GtkTextView *text_view;
5065 GdkModifierType state;
5066 GtkTextIter newplace;
5068 GDK_THREADS_ENTER ();
5070 text_view = GTK_TEXT_VIEW (data);
5072 gdk_window_get_pointer (text_view->text_window->bin_window,
5075 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5077 x + text_view->xoffset,
5078 y + text_view->yoffset);
5080 gtk_text_buffer_move_mark (get_buffer (text_view),
5081 text_view->dnd_mark,
5084 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5085 gtk_text_view_scroll_to_mark (text_view,
5086 text_view->dnd_mark,
5087 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5089 GDK_THREADS_LEAVE ();
5095 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5097 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5098 move_mark_to_pointer_and_scroll (text_view, "insert");
5100 /* If we had to scroll offscreen, insert a timeout to do so
5101 * again. Note that in the timeout, even if the mouse doesn't
5102 * move, due to this scroll xoffset/yoffset will have changed
5103 * and we'll need to scroll again.
5105 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5106 gtk_timeout_remove (text_view->scroll_timeout);
5108 text_view->scroll_timeout =
5109 gtk_timeout_add (50, selection_scan_timeout, text_view);
5115 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5116 const GtkTextIter *iter,
5117 GdkEventButton *button)
5119 GtkTextIter newplace;
5120 GtkTextBuffer *buffer;
5122 g_return_if_fail (text_view->selection_drag_handler == 0);
5124 gtk_grab_add (GTK_WIDGET (text_view));
5126 buffer = get_buffer (text_view);
5130 if (button->state & GDK_SHIFT_MASK)
5132 /* Extend selection */
5133 GtkTextIter start, end;
5135 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5137 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5139 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5142 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5145 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5147 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5150 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5156 /* Replace selection */
5157 gtk_text_buffer_place_cursor (buffer, &newplace);
5160 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5161 "motion_notify_event",
5162 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5166 /* returns whether we were really dragging */
5168 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5170 if (text_view->selection_drag_handler == 0)
5173 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5174 text_view->selection_drag_handler = 0;
5176 if (text_view->scroll_timeout != 0)
5178 gtk_timeout_remove (text_view->scroll_timeout);
5179 text_view->scroll_timeout = 0;
5182 /* one last update to current position */
5183 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5184 move_mark_to_pointer_and_scroll (text_view, "insert");
5186 gtk_grab_remove (GTK_WIDGET (text_view));
5196 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5197 GtkTextAttributes *values,
5200 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5201 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5204 pango_font_description_free (values->font);
5206 values->font = pango_font_description_copy (style->font_desc);
5210 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5212 if (text_view->layout)
5214 gboolean split_cursor;
5215 GtkTextDirection new_dir;
5216 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5218 g_object_get (G_OBJECT (settings),
5219 "gtk-split-cursor", &split_cursor,
5223 new_dir = GTK_TEXT_DIR_NONE;
5227 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5228 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5229 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5232 if (text_view->layout->cursor_direction != new_dir)
5233 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5238 gtk_text_view_ensure_layout (GtkTextView *text_view)
5242 widget = GTK_WIDGET (text_view);
5244 if (text_view->layout == NULL)
5246 GtkTextAttributes *style;
5247 PangoContext *ltr_context, *rtl_context;
5250 DV(g_print(G_STRLOC"\n"));
5252 text_view->layout = gtk_text_layout_new ();
5254 g_signal_connect (G_OBJECT (text_view->layout),
5256 G_CALLBACK (invalidated_handler),
5259 g_signal_connect (G_OBJECT (text_view->layout),
5261 G_CALLBACK (changed_handler),
5264 g_signal_connect (G_OBJECT (text_view->layout),
5266 G_CALLBACK (gtk_text_view_child_allocated),
5269 if (get_buffer (text_view))
5270 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5272 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5273 gtk_text_view_pend_cursor_blink (text_view);
5275 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5277 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5278 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5279 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5280 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5282 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5284 g_object_unref (G_OBJECT (ltr_context));
5285 g_object_unref (G_OBJECT (rtl_context));
5287 gtk_text_view_check_keymap_direction (text_view);
5289 style = gtk_text_attributes_new ();
5291 gtk_widget_ensure_style (widget);
5292 gtk_text_view_set_attributes_from_style (text_view,
5293 style, widget->style);
5295 style->pixels_above_lines = text_view->pixels_above_lines;
5296 style->pixels_below_lines = text_view->pixels_below_lines;
5297 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5298 style->left_margin = text_view->left_margin;
5299 style->right_margin = text_view->right_margin;
5300 style->indent = text_view->indent;
5301 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5303 style->wrap_mode = text_view->wrap_mode;
5304 style->justification = text_view->justify;
5305 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5307 gtk_text_layout_set_default_style (text_view->layout, style);
5309 gtk_text_attributes_unref (style);
5311 /* Set layout for all anchored children */
5313 tmp_list = text_view->children;
5314 while (tmp_list != NULL)
5316 GtkTextViewChild *vc = tmp_list->data;
5320 gtk_text_anchored_child_set_layout (vc->widget,
5322 /* vc may now be invalid! */
5325 tmp_list = g_slist_next (tmp_list);
5328 gtk_text_view_invalidate (text_view);
5333 * gtk_text_view_get_default_attributes:
5334 * @text_view: a #GtkTextView
5336 * Obtains a copy of the default text attributes. These are the
5337 * attributes used for text unless a tag overrides them.
5338 * You'd typically pass the default attributes in to
5339 * gtk_text_iter_get_attributes() in order to get the
5340 * attributes in effect at a given text position.
5342 * The return value is a copy owned by the caller of this function,
5343 * and should be freed.
5345 * Return value: a new #GtkTextAttributes
5348 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5350 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5352 gtk_text_view_ensure_layout (text_view);
5354 return gtk_text_attributes_copy (text_view->layout->default_style);
5358 gtk_text_view_destroy_layout (GtkTextView *text_view)
5360 if (text_view->layout)
5364 gtk_text_view_remove_validate_idles (text_view);
5366 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5367 (gpointer) invalidated_handler,
5369 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5370 (gpointer) changed_handler,
5373 /* Remove layout from all anchored children */
5374 tmp_list = text_view->children;
5375 while (tmp_list != NULL)
5377 GtkTextViewChild *vc = tmp_list->data;
5381 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5382 /* vc may now be invalid! */
5385 tmp_list = g_slist_next (tmp_list);
5388 gtk_text_view_stop_cursor_blink (text_view);
5389 gtk_text_view_end_selection_drag (text_view, NULL);
5391 g_object_unref (G_OBJECT (text_view->layout));
5392 text_view->layout = NULL;
5397 gtk_text_view_reset_im_context (GtkTextView *text_view)
5399 if (text_view->need_im_reset)
5401 text_view->need_im_reset = FALSE;
5402 gtk_im_context_reset (text_view->im_context);
5411 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5412 const GtkTextIter *iter,
5413 GdkEventMotion *event)
5415 GdkDragContext *context;
5416 GtkTargetList *target_list;
5418 text_view->drag_start_x = -1;
5419 text_view->drag_start_y = -1;
5420 text_view->pending_place_cursor_button = 0;
5422 target_list = gtk_target_list_new (target_table,
5423 G_N_ELEMENTS (target_table));
5425 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5426 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5427 1, (GdkEvent*)event);
5429 gtk_target_list_unref (target_list);
5431 gtk_drag_set_icon_default (context);
5435 gtk_text_view_drag_begin (GtkWidget *widget,
5436 GdkDragContext *context)
5442 gtk_text_view_drag_end (GtkWidget *widget,
5443 GdkDragContext *context)
5445 GtkTextView *text_view;
5447 text_view = GTK_TEXT_VIEW (widget);
5451 gtk_text_view_drag_data_get (GtkWidget *widget,
5452 GdkDragContext *context,
5453 GtkSelectionData *selection_data,
5457 GtkTextView *text_view;
5459 text_view = GTK_TEXT_VIEW (widget);
5461 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5463 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5465 gtk_selection_data_set (selection_data,
5466 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5479 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5482 /* Extract the selected text */
5483 str = gtk_text_iter_get_visible_text (&start, &end);
5488 gtk_selection_data_set_text (selection_data, str, -1);
5495 gtk_text_view_drag_data_delete (GtkWidget *widget,
5496 GdkDragContext *context)
5498 GtkTextView *text_view;
5500 text_view = GTK_TEXT_VIEW (widget);
5502 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5503 TRUE, GTK_TEXT_VIEW (widget)->editable);
5507 gtk_text_view_drag_leave (GtkWidget *widget,
5508 GdkDragContext *context,
5511 GtkTextView *text_view;
5513 text_view = GTK_TEXT_VIEW (widget);
5515 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5517 if (text_view->scroll_timeout != 0)
5518 gtk_timeout_remove (text_view->scroll_timeout);
5520 text_view->scroll_timeout = 0;
5524 gtk_text_view_drag_motion (GtkWidget *widget,
5525 GdkDragContext *context,
5530 GtkTextIter newplace;
5531 GtkTextView *text_view;
5534 GdkRectangle target_rect;
5536 GdkDragAction suggested_action = 0;
5538 text_view = GTK_TEXT_VIEW (widget);
5540 target_rect = text_view->text_window->allocation;
5542 if (x < target_rect.x ||
5543 y < target_rect.y ||
5544 x > (target_rect.x + target_rect.width) ||
5545 y > (target_rect.y + target_rect.height))
5546 return FALSE; /* outside the text window, allow parent widgets to handle event */
5548 gtk_text_view_window_to_buffer_coords (text_view,
5549 GTK_TEXT_WINDOW_WIDGET,
5553 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5557 if (gtk_drag_dest_find_target (widget, context,
5558 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5560 /* can't accept any of the offered targets */
5562 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5564 gtk_text_iter_in_range (&newplace, &start, &end))
5566 /* We're inside the selection. */
5570 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5572 GtkWidget *source_widget;
5574 suggested_action = context->suggested_action;
5576 source_widget = gtk_drag_get_source_widget (context);
5578 if (source_widget == widget)
5580 /* Default to MOVE, unless the user has
5581 * pressed ctrl or alt to affect available actions
5583 if ((context->actions & GDK_ACTION_MOVE) != 0)
5584 suggested_action = GDK_ACTION_MOVE;
5589 /* Can't drop here. */
5593 if (suggested_action != 0)
5595 gtk_text_mark_set_visible (text_view->dnd_mark,
5596 text_view->cursor_visible);
5598 gdk_drag_status (context, suggested_action, time);
5602 gdk_drag_status (context, 0, time);
5603 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5606 gtk_text_buffer_move_mark (get_buffer (text_view),
5607 text_view->dnd_mark,
5610 DV(g_print (G_STRLOC": scrolling to mark\n"));
5611 gtk_text_view_scroll_to_mark (text_view,
5612 text_view->dnd_mark,
5613 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5615 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5616 gtk_timeout_remove (text_view->scroll_timeout);
5618 text_view->scroll_timeout =
5619 gtk_timeout_add (50, drag_scan_timeout, text_view);
5621 /* TRUE return means don't propagate the drag motion to parent
5622 * widgets that may also be drop sites.
5628 gtk_text_view_drag_drop (GtkWidget *widget,
5629 GdkDragContext *context,
5634 GtkTextView *text_view;
5635 GtkTextIter drop_point;
5636 GdkAtom target = GDK_NONE;
5638 text_view = GTK_TEXT_VIEW (widget);
5640 if (text_view->scroll_timeout != 0)
5641 gtk_timeout_remove (text_view->scroll_timeout);
5643 text_view->scroll_timeout = 0;
5645 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5647 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5649 text_view->dnd_mark);
5651 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5652 target = gtk_drag_dest_find_target (widget, context, NULL);
5654 if (target != GDK_NONE)
5655 gtk_drag_get_data (widget, context, target, time);
5657 gtk_drag_finish (context, FALSE, FALSE, time);
5663 insert_text_data (GtkTextView *text_view,
5664 GtkTextIter *drop_point,
5665 GtkSelectionData *selection_data)
5669 str = gtk_selection_data_get_text (selection_data);
5673 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5674 drop_point, str, -1,
5675 text_view->editable);
5681 gtk_text_view_drag_data_received (GtkWidget *widget,
5682 GdkDragContext *context,
5685 GtkSelectionData *selection_data,
5689 GtkTextIter drop_point;
5690 GtkTextView *text_view;
5691 gboolean success = FALSE;
5693 text_view = GTK_TEXT_VIEW (widget);
5695 if (!text_view->dnd_mark)
5698 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5700 text_view->dnd_mark);
5702 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5705 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5707 GtkTextBuffer *src_buffer = NULL;
5708 GtkTextIter start, end;
5709 gboolean copy_tags = TRUE;
5711 if (selection_data->length != sizeof (src_buffer))
5714 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5716 if (src_buffer == NULL)
5719 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5721 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5722 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5725 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5730 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5734 text_view->editable);
5739 str = gtk_text_iter_get_visible_text (&start, &end);
5740 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5741 &drop_point, str, -1,
5742 text_view->editable);
5748 insert_text_data (text_view, &drop_point, selection_data);
5753 gtk_drag_finish (context, success,
5754 success && context->action == GDK_ACTION_MOVE,
5758 static GtkAdjustment*
5759 get_hadjustment (GtkTextView *text_view)
5761 if (text_view->hadjustment == NULL)
5762 gtk_text_view_set_scroll_adjustments (text_view,
5763 NULL, /* forces creation */
5764 text_view->vadjustment);
5766 return text_view->hadjustment;
5769 static GtkAdjustment*
5770 get_vadjustment (GtkTextView *text_view)
5772 if (text_view->vadjustment == NULL)
5773 gtk_text_view_set_scroll_adjustments (text_view,
5774 text_view->hadjustment,
5775 NULL); /* forces creation */
5776 return text_view->vadjustment;
5781 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5782 GtkAdjustment *hadj,
5783 GtkAdjustment *vadj)
5785 gboolean need_adjust = FALSE;
5787 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5790 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5792 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5794 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5796 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5798 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5800 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5801 g_object_unref (G_OBJECT (text_view->hadjustment));
5804 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5806 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5807 g_object_unref (G_OBJECT (text_view->vadjustment));
5810 if (text_view->hadjustment != hadj)
5812 text_view->hadjustment = hadj;
5813 g_object_ref (G_OBJECT (text_view->hadjustment));
5814 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5816 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5817 (GtkSignalFunc) gtk_text_view_value_changed,
5822 if (text_view->vadjustment != vadj)
5824 text_view->vadjustment = vadj;
5825 g_object_ref (G_OBJECT (text_view->vadjustment));
5826 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5828 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5829 (GtkSignalFunc) gtk_text_view_value_changed,
5835 gtk_text_view_value_changed (NULL, text_view);
5838 /* FIXME this adjust_allocation is a big cut-and-paste from
5839 * GtkCList, needs to be some "official" way to do this
5849 /* The window to which widget->window is relative */
5850 #define ALLOCATION_WINDOW(widget) \
5851 (GTK_WIDGET_NO_WINDOW (widget) ? \
5852 (widget)->window : \
5853 gdk_window_get_parent ((widget)->window))
5856 adjust_allocation_recurse (GtkWidget *widget,
5859 ScrollData *scroll_data = data;
5861 /* Need to really size allocate instead of just poking
5862 * into widget->allocation if the widget is not realized.
5863 * FIXME someone figure out why this was.
5865 if (!GTK_WIDGET_REALIZED (widget))
5867 if (GTK_WIDGET_VISIBLE (widget))
5869 GdkRectangle tmp_rectangle = widget->allocation;
5870 tmp_rectangle.x += scroll_data->dx;
5871 tmp_rectangle.y += scroll_data->dy;
5873 gtk_widget_size_allocate (widget, &tmp_rectangle);
5878 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5880 widget->allocation.x += scroll_data->dx;
5881 widget->allocation.y += scroll_data->dy;
5883 if (GTK_IS_CONTAINER (widget))
5884 gtk_container_forall (GTK_CONTAINER (widget),
5885 adjust_allocation_recurse,
5892 adjust_allocation (GtkWidget *widget,
5896 ScrollData scroll_data;
5898 if (GTK_WIDGET_REALIZED (widget))
5899 scroll_data.window = ALLOCATION_WINDOW (widget);
5901 scroll_data.window = NULL;
5903 scroll_data.dx = dx;
5904 scroll_data.dy = dy;
5906 adjust_allocation_recurse (widget, &scroll_data);
5910 gtk_text_view_value_changed (GtkAdjustment *adj,
5911 GtkTextView *text_view)
5918 /* Note that we oddly call this function with adj == NULL
5922 text_view->onscreen_validated = FALSE;
5924 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5925 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5926 adj ? adj->value : 0.0));
5928 if (adj == text_view->hadjustment)
5930 dx = text_view->xoffset - (gint)adj->value;
5931 text_view->xoffset = adj->value;
5933 else if (adj == text_view->vadjustment)
5935 dy = text_view->yoffset - (gint)adj->value;
5936 text_view->yoffset = adj->value;
5938 if (text_view->layout)
5940 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5942 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5944 text_view->first_para_pixels = adj->value - line_top;
5948 if (dx != 0 || dy != 0)
5952 if (GTK_WIDGET_REALIZED (text_view))
5956 if (text_view->left_window)
5957 text_window_scroll (text_view->left_window, 0, dy);
5958 if (text_view->right_window)
5959 text_window_scroll (text_view->right_window, 0, dy);
5964 if (text_view->top_window)
5965 text_window_scroll (text_view->top_window, dx, 0);
5966 if (text_view->bottom_window)
5967 text_window_scroll (text_view->bottom_window, dx, 0);
5970 /* It looks nicer to scroll the main area last, because
5971 * it takes a while, and making the side areas update
5972 * afterward emphasizes the slowness of scrolling the
5975 text_window_scroll (text_view->text_window, dx, dy);
5978 /* Children are now "moved" in the text window, poke
5979 * into widget->allocation for each child
5981 tmp_list = text_view->children;
5982 while (tmp_list != NULL)
5984 GtkTextViewChild *child = tmp_list->data;
5987 adjust_allocation (child->widget, dx, dy);
5989 tmp_list = g_slist_next (tmp_list);
5993 /* This could result in invalidation, which would install the
5994 * first_validate_idle, which would validate onscreen;
5995 * but we're going to go ahead and validate here, so
5996 * first_validate_idle shouldn't have anything to do.
5998 gtk_text_view_update_layout_width (text_view);
6000 /* note that validation of onscreen could invoke this function
6001 * recursively, by scrolling to maintain first_para, or in response
6002 * to updating the layout width, however there is no problem with
6003 * that, or shouldn't be.
6005 gtk_text_view_validate_onscreen (text_view);
6007 /* process exposes */
6008 if (GTK_WIDGET_REALIZED (text_view))
6010 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6012 if (text_view->left_window)
6013 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6015 if (text_view->right_window)
6016 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6018 if (text_view->top_window)
6019 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6021 if (text_view->bottom_window)
6022 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6024 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6027 /* If this got installed, get rid of it, it's just a waste of time. */
6028 if (text_view->first_validate_idle != 0)
6030 g_source_remove (text_view->first_validate_idle);
6031 text_view->first_validate_idle = 0;
6034 gtk_text_view_update_im_spot_location (text_view);
6036 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6040 gtk_text_view_commit_handler (GtkIMContext *context,
6042 GtkTextView *text_view)
6044 gtk_text_view_commit_text (text_view, str);
6048 gtk_text_view_commit_text (GtkTextView *text_view,
6051 gboolean had_selection;
6053 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6055 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6058 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6059 text_view->editable);
6061 if (!strcmp (str, "\n"))
6063 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6064 text_view->editable);
6068 if (!had_selection && text_view->overwrite_mode)
6072 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6074 gtk_text_buffer_get_mark (get_buffer (text_view),
6076 if (!gtk_text_iter_ends_line (&insert))
6077 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6079 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6080 text_view->editable);
6083 gtk_text_buffer_end_user_action (get_buffer (text_view));
6085 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6086 gtk_text_view_scroll_mark_onscreen (text_view,
6087 gtk_text_buffer_get_mark (get_buffer (text_view),
6092 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6093 GtkTextView *text_view)
6096 PangoAttrList *attrs;
6099 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6100 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6102 pango_attr_list_unref (attrs);
6107 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6108 GtkTextView *text_view)
6115 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6116 gtk_text_buffer_get_insert (text_view->buffer));
6119 pos = gtk_text_iter_get_line_index (&start);
6120 gtk_text_iter_set_line_offset (&start, 0);
6121 gtk_text_iter_forward_to_line_end (&end);
6123 text = gtk_text_iter_get_slice (&start, &end);
6124 gtk_im_context_set_surrounding (context, text, -1, pos);
6131 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6134 GtkTextView *text_view)
6139 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6140 gtk_text_buffer_get_insert (text_view->buffer));
6143 gtk_text_iter_forward_chars (&start, offset);
6144 gtk_text_iter_forward_chars (&end, offset + n_chars);
6146 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6152 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6153 const GtkTextIter *location,
6157 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6158 gboolean need_reset = FALSE;
6160 if (mark == gtk_text_buffer_get_insert (buffer))
6162 text_view->virtual_cursor_x = -1;
6163 text_view->virtual_cursor_y = -1;
6164 gtk_text_view_update_im_spot_location (text_view);
6167 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6173 gtk_text_view_reset_im_context (text_view);
6177 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6181 GdkRectangle strong_pos;
6184 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6185 gtk_text_buffer_get_mark (get_buffer (text_view),
6188 if ((x && text_view->virtual_cursor_x == -1) ||
6189 (y && text_view->virtual_cursor_y == -1))
6190 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6194 if (text_view->virtual_cursor_x != -1)
6195 *x = text_view->virtual_cursor_x;
6202 if (text_view->virtual_cursor_x != -1)
6203 *y = text_view->virtual_cursor_y;
6205 *y = strong_pos.y + strong_pos.height / 2;
6210 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6214 GdkRectangle strong_pos;
6217 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6218 gtk_text_buffer_get_mark (get_buffer (text_view),
6221 if (x == -1 || y == -1)
6222 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6224 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6225 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6228 /* Quick hack of a popup menu
6231 activate_cb (GtkWidget *menuitem,
6232 GtkTextView *text_view)
6234 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6235 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6239 append_action_signal (GtkTextView *text_view,
6241 const gchar *stock_id,
6242 const gchar *signal,
6245 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6247 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6248 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6249 GTK_SIGNAL_FUNC (activate_cb), text_view);
6251 gtk_widget_set_sensitive (menuitem, sensitive);
6253 gtk_widget_show (menuitem);
6254 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6258 popup_menu_detach (GtkWidget *attach_widget,
6261 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6265 popup_position_func (GtkMenu *menu,
6271 GtkTextView *text_view;
6273 GdkRectangle cursor_rect;
6274 GdkRectangle onscreen_rect;
6275 gint root_x, root_y;
6280 text_view = GTK_TEXT_VIEW (user_data);
6281 widget = GTK_WIDGET (text_view);
6283 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6285 screen = gtk_widget_get_screen (widget);
6287 gdk_window_get_origin (widget->window, &root_x, &root_y);
6289 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6291 gtk_text_buffer_get_insert (get_buffer (text_view)));
6293 gtk_text_view_get_iter_location (text_view,
6297 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6299 gtk_widget_size_request (text_view->popup_menu, &req);
6301 /* can't use rectangle_intersect since cursor rect can have 0 width */
6302 if (cursor_rect.x >= onscreen_rect.x &&
6303 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6304 cursor_rect.y >= onscreen_rect.y &&
6305 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6307 gtk_text_view_buffer_to_window_coords (text_view,
6308 GTK_TEXT_WINDOW_WIDGET,
6309 cursor_rect.x, cursor_rect.y,
6310 &cursor_rect.x, &cursor_rect.y);
6312 *x = root_x + cursor_rect.x + cursor_rect.width;
6313 *y = root_y + cursor_rect.y + cursor_rect.height;
6317 /* Just center the menu, since cursor is offscreen. */
6318 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6319 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6323 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6324 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6326 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6327 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6332 GtkTextView *text_view;
6338 range_contains_editable_text (const GtkTextIter *start,
6339 const GtkTextIter *end,
6340 gboolean default_editability)
6342 GtkTextIter iter = *start;
6344 while (gtk_text_iter_compare (&iter, end) < 0)
6346 if (gtk_text_iter_editable (&iter, default_editability))
6349 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6356 unichar_chosen_func (const char *text,
6359 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6361 gtk_text_view_commit_text (text_view, text);
6365 popup_targets_received (GtkClipboard *clipboard,
6366 GtkSelectionData *data,
6369 PopupInfo *info = user_data;
6370 GtkTextView *text_view = info->text_view;
6372 if (GTK_WIDGET_REALIZED (text_view))
6374 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6375 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6377 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6378 GtkWidget *menuitem;
6380 gboolean have_selection;
6381 gboolean can_insert;
6383 GtkTextIter sel_start, sel_end;
6385 if (text_view->popup_menu)
6386 gtk_widget_destroy (text_view->popup_menu);
6388 text_view->popup_menu = gtk_menu_new ();
6390 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6391 GTK_WIDGET (text_view),
6394 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6395 &sel_start, &sel_end);
6397 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6399 gtk_text_buffer_get_insert (get_buffer (text_view)));
6401 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6403 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6405 range_contains_editable_text (&sel_start, &sel_end,
6406 text_view->editable));
6407 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6409 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6410 can_insert && clipboard_contains_text);
6412 menuitem = gtk_separator_menu_item_new ();
6413 gtk_widget_show (menuitem);
6414 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6416 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6417 gtk_widget_show (menuitem);
6418 submenu = gtk_menu_new ();
6419 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6420 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6422 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6423 GTK_MENU_SHELL (submenu));
6425 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6426 gtk_widget_show (menuitem);
6427 gtk_widget_set_sensitive (menuitem, can_insert);
6429 submenu = gtk_menu_new ();
6430 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6431 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6433 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6434 unichar_chosen_func,
6437 gtk_signal_emit (GTK_OBJECT (text_view),
6438 signals[POPULATE_POPUP],
6439 text_view->popup_menu);
6442 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6444 info->button, info->time);
6447 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6448 popup_position_func, text_view,
6449 0, gtk_get_current_event_time ());
6450 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu));
6454 g_object_unref (text_view);
6459 gtk_text_view_do_popup (GtkTextView *text_view,
6460 GdkEventButton *event)
6462 PopupInfo *info = g_new (PopupInfo, 1);
6464 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6465 gtk_text_view_end_selection_drag (text_view, event);
6467 /* In order to know what entries we should make sensitive, we
6468 * ask for the current targets of the clipboard, and when
6469 * we get them, then we actually pop up the menu.
6471 info->text_view = g_object_ref (text_view);
6475 info->button = event->button;
6476 info->time = event->time;
6481 info->time = gtk_get_current_event_time ();
6484 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6485 GDK_SELECTION_CLIPBOARD),
6486 gdk_atom_intern ("TARGETS", FALSE),
6487 popup_targets_received,
6492 gtk_text_view_popup_menu (GtkWidget *widget)
6494 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6498 /* Child GdkWindows */
6501 static GtkTextWindow*
6502 text_window_new (GtkTextWindowType type,
6505 gint height_request)
6509 win = g_new (GtkTextWindow, 1);
6512 win->widget = widget;
6514 win->bin_window = NULL;
6515 win->requisition.width = width_request;
6516 win->requisition.height = height_request;
6517 win->allocation.width = width_request;
6518 win->allocation.height = height_request;
6519 win->allocation.x = 0;
6520 win->allocation.y = 0;
6526 text_window_free (GtkTextWindow *win)
6529 text_window_unrealize (win);
6535 text_window_realize (GtkTextWindow *win,
6538 GdkWindowAttr attributes;
6539 gint attributes_mask;
6542 attributes.window_type = GDK_WINDOW_CHILD;
6543 attributes.x = win->allocation.x;
6544 attributes.y = win->allocation.y;
6545 attributes.width = win->allocation.width;
6546 attributes.height = win->allocation.height;
6547 attributes.wclass = GDK_INPUT_OUTPUT;
6548 attributes.visual = gtk_widget_get_visual (win->widget);
6549 attributes.colormap = gtk_widget_get_colormap (win->widget);
6550 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6552 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6554 win->window = gdk_window_new (parent,
6558 gdk_window_show (win->window);
6559 gdk_window_set_user_data (win->window, win->widget);
6563 attributes.width = win->allocation.width;
6564 attributes.height = win->allocation.height;
6565 attributes.event_mask = (GDK_EXPOSURE_MASK |
6567 GDK_KEY_PRESS_MASK |
6568 GDK_BUTTON_PRESS_MASK |
6569 GDK_BUTTON_RELEASE_MASK |
6570 GDK_POINTER_MOTION_MASK |
6571 GDK_POINTER_MOTION_HINT_MASK |
6572 gtk_widget_get_events (win->widget));
6574 win->bin_window = gdk_window_new (win->window,
6578 gdk_window_show (win->bin_window);
6579 gdk_window_set_user_data (win->bin_window, win->widget);
6581 if (win->type == GTK_TEXT_WINDOW_TEXT)
6584 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
6586 gdk_window_set_cursor (win->bin_window, cursor);
6587 gdk_cursor_unref (cursor);
6589 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6593 gdk_window_set_background (win->bin_window,
6594 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6598 gdk_window_set_background (win->bin_window,
6599 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6602 g_object_set_qdata (G_OBJECT (win->window),
6603 g_quark_from_static_string ("gtk-text-view-text-window"),
6606 g_object_set_qdata (G_OBJECT (win->bin_window),
6607 g_quark_from_static_string ("gtk-text-view-text-window"),
6612 text_window_unrealize (GtkTextWindow *win)
6614 if (win->type == GTK_TEXT_WINDOW_TEXT)
6616 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6620 gdk_window_set_user_data (win->window, NULL);
6621 gdk_window_set_user_data (win->bin_window, NULL);
6622 gdk_window_destroy (win->bin_window);
6623 gdk_window_destroy (win->window);
6625 win->bin_window = NULL;
6629 text_window_size_allocate (GtkTextWindow *win,
6632 win->allocation = *rect;
6636 gdk_window_move_resize (win->window,
6638 rect->width, rect->height);
6640 gdk_window_resize (win->bin_window,
6641 rect->width, rect->height);
6646 text_window_scroll (GtkTextWindow *win,
6650 if (dx != 0 || dy != 0)
6652 gdk_window_scroll (win->bin_window, dx, dy);
6657 text_window_invalidate_rect (GtkTextWindow *win,
6660 GdkRectangle window_rect;
6662 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6669 window_rect.width = rect->width;
6670 window_rect.height = rect->height;
6672 /* Adjust the rect as appropriate */
6676 case GTK_TEXT_WINDOW_TEXT:
6679 case GTK_TEXT_WINDOW_LEFT:
6680 case GTK_TEXT_WINDOW_RIGHT:
6682 window_rect.width = win->allocation.width;
6685 case GTK_TEXT_WINDOW_TOP:
6686 case GTK_TEXT_WINDOW_BOTTOM:
6688 window_rect.height = win->allocation.height;
6692 g_warning ("%s: bug!", G_STRLOC);
6697 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6701 GdkColor color = { 0, 65535, 0, 0 };
6702 GdkGC *gc = gdk_gc_new (win->bin_window);
6703 gdk_gc_set_rgb_fg_color (gc, &color);
6704 gdk_draw_rectangle (win->bin_window,
6705 gc, TRUE, window_rect.x, window_rect.y,
6706 window_rect.width, window_rect.height);
6713 text_window_get_width (GtkTextWindow *win)
6715 return win->allocation.width;
6719 text_window_get_height (GtkTextWindow *win)
6721 return win->allocation.height;
6725 text_window_get_allocation (GtkTextWindow *win,
6728 *rect = win->allocation;
6735 * gtk_text_view_get_window:
6736 * @text_view: a #GtkTextView
6737 * @win: window to get
6739 * Retrieves the #GdkWindow corresponding to an area of the text view;
6740 * possible windows include the overall widget window, child windows
6741 * on the left, right, top, bottom, and the window that displays the
6742 * text buffer. Windows are %NULL and nonexistent if their width or
6743 * height is 0, and are nonexistent before the widget has been
6746 * Return value: a #GdkWindow, or %NULL
6749 gtk_text_view_get_window (GtkTextView *text_view,
6750 GtkTextWindowType win)
6752 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6756 case GTK_TEXT_WINDOW_WIDGET:
6757 return GTK_WIDGET (text_view)->window;
6760 case GTK_TEXT_WINDOW_TEXT:
6761 return text_view->text_window->bin_window;
6764 case GTK_TEXT_WINDOW_LEFT:
6765 if (text_view->left_window)
6766 return text_view->left_window->bin_window;
6771 case GTK_TEXT_WINDOW_RIGHT:
6772 if (text_view->right_window)
6773 return text_view->right_window->bin_window;
6778 case GTK_TEXT_WINDOW_TOP:
6779 if (text_view->top_window)
6780 return text_view->top_window->bin_window;
6785 case GTK_TEXT_WINDOW_BOTTOM:
6786 if (text_view->bottom_window)
6787 return text_view->bottom_window->bin_window;
6792 case GTK_TEXT_WINDOW_PRIVATE:
6793 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6798 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6803 * gtk_text_view_get_window_type:
6804 * @text_view: a #GtkTextView
6805 * @window: a window type
6807 * Usually used to find out which window an event corresponds to.
6808 * If you connect to an event signal on @text_view, this function
6809 * should be called on <literal>event->window</literal> to
6810 * see which window it was.
6812 * Return value: the window type.
6815 gtk_text_view_get_window_type (GtkTextView *text_view,
6820 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6821 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6823 if (window == GTK_WIDGET (text_view)->window)
6824 return GTK_TEXT_WINDOW_WIDGET;
6826 win = g_object_get_qdata (G_OBJECT (window),
6827 g_quark_try_string ("gtk-text-view-text-window"));
6833 return GTK_TEXT_WINDOW_PRIVATE;
6838 buffer_to_widget (GtkTextView *text_view,
6846 *window_x = buffer_x - text_view->xoffset;
6847 *window_x += text_view->text_window->allocation.x;
6852 *window_y = buffer_y - text_view->yoffset;
6853 *window_y += text_view->text_window->allocation.y;
6858 widget_to_text_window (GtkTextWindow *win,
6865 *window_x = widget_x - win->allocation.x;
6868 *window_y = widget_y - win->allocation.y;
6872 buffer_to_text_window (GtkTextView *text_view,
6881 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6882 "for a nonexistent or private child window of GtkTextView");
6886 buffer_to_widget (text_view,
6888 window_x, window_y);
6890 widget_to_text_window (win,
6891 window_x ? *window_x : 0,
6892 window_y ? *window_y : 0,
6898 * gtk_text_view_buffer_to_window_coords:
6899 * @text_view: a #GtkTextView
6900 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6901 * @buffer_x: buffer x coordinate
6902 * @buffer_y: buffer y coordinate
6903 * @window_x: window x coordinate return location
6904 * @window_y: window y coordinate return location
6906 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6907 * @win, and stores the result in (@window_x, @window_y).
6909 * Note that you can't convert coordinates for a nonexisting window (see
6910 * gtk_text_view_set_border_window_size()).
6913 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6914 GtkTextWindowType win,
6920 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6924 case GTK_TEXT_WINDOW_WIDGET:
6925 buffer_to_widget (text_view,
6927 window_x, window_y);
6930 case GTK_TEXT_WINDOW_TEXT:
6932 *window_x = buffer_x - text_view->xoffset;
6934 *window_y = buffer_y - text_view->yoffset;
6937 case GTK_TEXT_WINDOW_LEFT:
6938 buffer_to_text_window (text_view,
6939 text_view->left_window,
6941 window_x, window_y);
6944 case GTK_TEXT_WINDOW_RIGHT:
6945 buffer_to_text_window (text_view,
6946 text_view->right_window,
6948 window_x, window_y);
6951 case GTK_TEXT_WINDOW_TOP:
6952 buffer_to_text_window (text_view,
6953 text_view->top_window,
6955 window_x, window_y);
6958 case GTK_TEXT_WINDOW_BOTTOM:
6959 buffer_to_text_window (text_view,
6960 text_view->bottom_window,
6962 window_x, window_y);
6965 case GTK_TEXT_WINDOW_PRIVATE:
6966 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6970 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6976 widget_to_buffer (GtkTextView *text_view,
6984 *buffer_x = widget_x + text_view->xoffset;
6985 *buffer_x -= text_view->text_window->allocation.x;
6990 *buffer_y = widget_y + text_view->yoffset;
6991 *buffer_y -= text_view->text_window->allocation.y;
6996 text_window_to_widget (GtkTextWindow *win,
7003 *widget_x = window_x + win->allocation.x;
7006 *widget_y = window_y + win->allocation.y;
7010 text_window_to_buffer (GtkTextView *text_view,
7019 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7020 "coordinates for a nonexistent child window.");
7024 text_window_to_widget (win,
7030 widget_to_buffer (text_view,
7031 buffer_x ? *buffer_x : 0,
7032 buffer_y ? *buffer_y : 0,
7038 * gtk_text_view_window_to_buffer_coords:
7039 * @text_view: a #GtkTextView
7040 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7041 * @window_x: window x coordinate
7042 * @window_y: window y coordinate
7043 * @buffer_x: buffer x coordinate return location
7044 * @buffer_y: buffer y coordinate return location
7046 * Converts coordinates on the window identified by @win to buffer
7047 * coordinates, storing the result in (@buffer_x,@buffer_y).
7049 * Note that you can't convert coordinates for a nonexisting window (see
7050 * gtk_text_view_set_border_window_size()).
7053 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7054 GtkTextWindowType win,
7060 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7064 case GTK_TEXT_WINDOW_WIDGET:
7065 widget_to_buffer (text_view,
7067 buffer_x, buffer_y);
7070 case GTK_TEXT_WINDOW_TEXT:
7072 *buffer_x = window_x + text_view->xoffset;
7074 *buffer_y = window_y + text_view->yoffset;
7077 case GTK_TEXT_WINDOW_LEFT:
7078 text_window_to_buffer (text_view,
7079 text_view->left_window,
7081 buffer_x, buffer_y);
7084 case GTK_TEXT_WINDOW_RIGHT:
7085 text_window_to_buffer (text_view,
7086 text_view->right_window,
7088 buffer_x, buffer_y);
7091 case GTK_TEXT_WINDOW_TOP:
7092 text_window_to_buffer (text_view,
7093 text_view->top_window,
7095 buffer_x, buffer_y);
7098 case GTK_TEXT_WINDOW_BOTTOM:
7099 text_window_to_buffer (text_view,
7100 text_view->bottom_window,
7102 buffer_x, buffer_y);
7105 case GTK_TEXT_WINDOW_PRIVATE:
7106 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7110 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7116 set_window_width (GtkTextView *text_view,
7118 GtkTextWindowType type,
7119 GtkTextWindow **winp)
7125 text_window_free (*winp);
7127 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7134 *winp = text_window_new (type,
7135 GTK_WIDGET (text_view),
7137 /* if the widget is already realized we need to realize the child manually */
7138 if (GTK_WIDGET_REALIZED (text_view))
7139 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7143 if ((*winp)->requisition.width == width)
7146 (*winp)->requisition.width = width;
7149 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7155 set_window_height (GtkTextView *text_view,
7157 GtkTextWindowType type,
7158 GtkTextWindow **winp)
7164 text_window_free (*winp);
7166 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7173 *winp = text_window_new (type,
7174 GTK_WIDGET (text_view),
7177 /* if the widget is already realized we need to realize the child manually */
7178 if (GTK_WIDGET_REALIZED (text_view))
7179 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7183 if ((*winp)->requisition.height == height)
7186 (*winp)->requisition.height = height;
7189 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7194 * gtk_text_view_set_border_window_size:
7195 * @text_view: a #GtkTextView
7196 * @type: window to affect
7197 * @size: width or height of the window
7199 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7200 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7201 * Automatically destroys the corresponding window if the size is set
7202 * to 0, and creates the window if the size is set to non-zero. This
7203 * function can only be used for the "border windows," it doesn't work
7204 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7205 * #GTK_TEXT_WINDOW_PRIVATE.
7208 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7209 GtkTextWindowType type,
7213 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7214 g_return_if_fail (size >= 0);
7218 case GTK_TEXT_WINDOW_LEFT:
7219 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7220 &text_view->left_window);
7223 case GTK_TEXT_WINDOW_RIGHT:
7224 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7225 &text_view->right_window);
7228 case GTK_TEXT_WINDOW_TOP:
7229 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7230 &text_view->top_window);
7233 case GTK_TEXT_WINDOW_BOTTOM:
7234 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7235 &text_view->bottom_window);
7239 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7245 * gtk_text_view_get_border_window_size:
7246 * @text_view: a #GtkTextView
7247 * @type: window to return size from
7249 * Gets the width of the specified border window. See
7250 * gtk_text_view_set_border_window_size().
7252 * Return value: width of window
7255 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7256 GtkTextWindowType type)
7258 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7262 case GTK_TEXT_WINDOW_LEFT:
7263 if (text_view->left_window)
7264 return text_view->left_window->requisition.width;
7266 case GTK_TEXT_WINDOW_RIGHT:
7267 if (text_view->right_window)
7268 return text_view->right_window->requisition.width;
7270 case GTK_TEXT_WINDOW_TOP:
7271 if (text_view->top_window)
7272 return text_view->top_window->requisition.height;
7274 case GTK_TEXT_WINDOW_BOTTOM:
7275 if (text_view->bottom_window)
7276 return text_view->bottom_window->requisition.height;
7279 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7290 static GtkTextViewChild*
7291 text_view_child_new_anchored (GtkWidget *child,
7292 GtkTextChildAnchor *anchor,
7293 GtkTextLayout *layout)
7295 GtkTextViewChild *vc;
7297 vc = g_new (GtkTextViewChild, 1);
7299 vc->type = GTK_TEXT_WINDOW_PRIVATE;
7301 vc->anchor = anchor;
7303 vc->from_top_of_line = 0;
7304 vc->from_left_of_buffer = 0;
7306 g_object_ref (G_OBJECT (vc->widget));
7307 g_object_ref (G_OBJECT (vc->anchor));
7309 g_object_set_data (G_OBJECT (child),
7310 "gtk-text-view-child",
7313 gtk_text_child_anchor_register_child (anchor, child, layout);
7318 static GtkTextViewChild*
7319 text_view_child_new_window (GtkWidget *child,
7320 GtkTextWindowType type,
7324 GtkTextViewChild *vc;
7326 vc = g_new (GtkTextViewChild, 1);
7331 vc->from_top_of_line = 0;
7332 vc->from_left_of_buffer = 0;
7334 g_object_ref (G_OBJECT (vc->widget));
7340 g_object_set_data (G_OBJECT (child),
7341 "gtk-text-view-child",
7348 text_view_child_free (GtkTextViewChild *child)
7350 g_object_set_data (G_OBJECT (child->widget),
7351 "gtk-text-view-child", NULL);
7355 gtk_text_child_anchor_unregister_child (child->anchor,
7357 g_object_unref (G_OBJECT (child->anchor));
7360 g_object_unref (G_OBJECT (child->widget));
7366 text_view_child_set_parent_window (GtkTextView *text_view,
7367 GtkTextViewChild *vc)
7370 gtk_widget_set_parent_window (vc->widget,
7371 text_view->text_window->bin_window);
7375 window = gtk_text_view_get_window (text_view,
7377 gtk_widget_set_parent_window (vc->widget, window);
7382 add_child (GtkTextView *text_view,
7383 GtkTextViewChild *vc)
7385 text_view->children = g_slist_prepend (text_view->children,
7388 if (GTK_WIDGET_REALIZED (text_view))
7389 text_view_child_set_parent_window (text_view, vc);
7391 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7395 * gtk_text_view_add_child_at_anchor:
7396 * @text_view: a #GtkTextView
7397 * @child: a #GtkWidget
7398 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7400 * Adds a child widget in the text buffer, at the given @anchor.
7404 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7406 GtkTextChildAnchor *anchor)
7408 GtkTextViewChild *vc;
7410 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7411 g_return_if_fail (GTK_IS_WIDGET (child));
7412 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7413 g_return_if_fail (child->parent == NULL);
7415 gtk_text_view_ensure_layout (text_view);
7417 vc = text_view_child_new_anchored (child, anchor,
7420 add_child (text_view, vc);
7422 g_assert (vc->widget == child);
7423 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7427 * gtk_text_view_add_child_in_window:
7428 * @text_view: a #GtkTextView
7429 * @child: a #GtkWidget
7430 * @which_window: which window the child should appear in
7431 * @xpos: X position of child in window coordinates
7432 * @ypos: Y position of child in window coordinates
7434 * Adds a child at fixed coordinates in one of the text widget's
7435 * windows. The window must have nonzero size (see
7436 * gtk_text_view_set_border_window_size()). Note that the child
7437 * coordinates are given relative to the #GdkWindow in question, and
7438 * that these coordinates have no sane relationship to scrolling. When
7439 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7440 * irrelevant, the child floats above all scrollable areas. But when
7441 * placing a child in one of the scrollable windows (border windows or
7442 * text window), you'll need to compute the child's correct position
7443 * in buffer coordinates any time scrolling occurs or buffer changes
7444 * occur, and then call gtk_text_view_move_child() to update the
7445 * child's position. Unfortunately there's no good way to detect that
7446 * scrolling has occurred, using the current API; a possible hack
7447 * would be to update all child positions when the scroll adjustments
7448 * change or the text buffer changes. See bug 64518 on
7449 * bugzilla.gnome.org for status of fixing this issue.
7453 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7455 GtkTextWindowType which_window,
7459 GtkTextViewChild *vc;
7461 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7462 g_return_if_fail (GTK_IS_WIDGET (child));
7463 g_return_if_fail (child->parent == NULL);
7465 vc = text_view_child_new_window (child, which_window,
7468 add_child (text_view, vc);
7470 g_assert (vc->widget == child);
7471 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7475 * gtk_text_view_move_child:
7476 * @text_view: a #GtkTextView
7477 * @child: child widget already added to the text view
7478 * @xpos: new X position in window coordinates
7479 * @ypos: new Y position in window coordinates
7481 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7485 gtk_text_view_move_child (GtkTextView *text_view,
7490 GtkTextViewChild *vc;
7492 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7493 g_return_if_fail (GTK_IS_WIDGET (child));
7494 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7496 vc = g_object_get_data (G_OBJECT (child),
7497 "gtk-text-view-child");
7499 g_assert (vc != NULL);
7501 if (vc->x == xpos &&
7508 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7509 gtk_widget_queue_resize (child);
7513 /* Iterator operations */
7516 * gtk_text_view_forward_display_line:
7517 * @text_view: a #GtkTextView
7518 * @iter: a #GtkTextIter
7520 * Moves the given @iter forward by one display (wrapped) line. A
7521 * display line is different from a paragraph. Paragraphs are
7522 * separated by newlines or other paragraph separator characters.
7523 * Display lines are created by line-wrapping a paragraph. If
7524 * wrapping is turned off, display lines and paragraphs will be the
7525 * same. Display lines are divided differently for each view, since
7526 * they depend on the view's width; paragraphs are the same in all
7527 * views, since they depend on the contents of the #GtkTextBuffer.
7529 * Return value: %TRUE if @iter was moved and is not on the end iterator
7532 gtk_text_view_forward_display_line (GtkTextView *text_view,
7535 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7536 g_return_val_if_fail (iter != NULL, FALSE);
7538 gtk_text_view_ensure_layout (text_view);
7540 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7544 * gtk_text_view_backward_display_line:
7545 * @text_view: a #GtkTextView
7546 * @iter: a #GtkTextIter
7548 * Moves the given @iter backward by one display (wrapped) line. A
7549 * display line is different from a paragraph. Paragraphs are
7550 * separated by newlines or other paragraph separator characters.
7551 * Display lines are created by line-wrapping a paragraph. If
7552 * wrapping is turned off, display lines and paragraphs will be the
7553 * same. Display lines are divided differently for each view, since
7554 * they depend on the view's width; paragraphs are the same in all
7555 * views, since they depend on the contents of the #GtkTextBuffer.
7557 * Return value: %TRUE if @iter was moved and is not on the end iterator
7560 gtk_text_view_backward_display_line (GtkTextView *text_view,
7563 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7564 g_return_val_if_fail (iter != NULL, FALSE);
7566 gtk_text_view_ensure_layout (text_view);
7568 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7572 * gtk_text_view_forward_display_line_end:
7573 * @text_view: a #GtkTextView
7574 * @iter: a #GtkTextIter
7576 * Moves the given @iter forward to the next display line end. A
7577 * display line is different from a paragraph. Paragraphs are
7578 * separated by newlines or other paragraph separator characters.
7579 * Display lines are created by line-wrapping a paragraph. If
7580 * wrapping is turned off, display lines and paragraphs will be the
7581 * same. Display lines are divided differently for each view, since
7582 * they depend on the view's width; paragraphs are the same in all
7583 * views, since they depend on the contents of the #GtkTextBuffer.
7585 * Return value: %TRUE if @iter was moved and is not on the end iterator
7588 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7591 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7592 g_return_val_if_fail (iter != NULL, FALSE);
7594 gtk_text_view_ensure_layout (text_view);
7596 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7600 * gtk_text_view_backward_display_line_start:
7601 * @text_view: a #GtkTextView
7602 * @iter: a #GtkTextIter
7604 * Moves the given @iter backward to the next display line start. A
7605 * display line is different from a paragraph. Paragraphs are
7606 * separated by newlines or other paragraph separator characters.
7607 * Display lines are created by line-wrapping a paragraph. If
7608 * wrapping is turned off, display lines and paragraphs will be the
7609 * same. Display lines are divided differently for each view, since
7610 * they depend on the view's width; paragraphs are the same in all
7611 * views, since they depend on the contents of the #GtkTextBuffer.
7613 * Return value: %TRUE if @iter was moved and is not on the end iterator
7616 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7619 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7620 g_return_val_if_fail (iter != NULL, FALSE);
7622 gtk_text_view_ensure_layout (text_view);
7624 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7628 * gtk_text_view_starts_display_line:
7629 * @text_view: a #GtkTextView
7630 * @iter: a #GtkTextIter
7632 * Determines whether @iter is at the start of a display line.
7633 * See gtk_text_view_forward_display_line() for an explanation of
7634 * display lines vs. paragraphs.
7636 * Return value: %TRUE if @iter begins a wrapped line
7639 gtk_text_view_starts_display_line (GtkTextView *text_view,
7640 const GtkTextIter *iter)
7642 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7643 g_return_val_if_fail (iter != NULL, FALSE);
7645 gtk_text_view_ensure_layout (text_view);
7647 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7651 * gtk_text_view_move_visually:
7652 * @text_view: a #GtkTextView
7653 * @iter: a #GtkTextIter
7654 * @count: number of lines to move
7656 * Moves @iter up or down by @count display (wrapped) lines.
7657 * See gtk_text_view_forward_display_line() for an explanation of
7658 * display lines vs. paragraphs.
7660 * Return value: %TRUE if @iter moved and is not on the end iterator
7663 gtk_text_view_move_visually (GtkTextView *text_view,
7667 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7668 g_return_val_if_fail (iter != NULL, FALSE);
7670 gtk_text_view_ensure_layout (text_view);
7672 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);