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 "gtktextutil.h"
47 #include "gtkwindow.h"
50 /* How scrolling, validation, exposes, etc. work.
52 * The expose_event handler has the invariant that the onscreen lines
53 * have been validated.
55 * There are two ways that onscreen lines can become invalid. The first
56 * is to change which lines are onscreen. This happens when the value
57 * of a scroll adjustment changes. So the code path begins in
58 * gtk_text_view_value_changed() and goes like this:
59 * - gdk_window_scroll() to reflect the new adjustment value
60 * - validate the lines that were moved onscreen
61 * - gdk_window_process_updates() to handle the exposes immediately
63 * The second way is that you get the "invalidated" signal from the layout,
64 * indicating that lines have become invalid. This code path begins in
65 * invalidated_handler() and goes like this:
66 * - install high-priority idle which does the rest of the steps
67 * - if a scroll is pending from scroll_to_mark(), do the scroll,
68 * jumping to the gtk_text_view_value_changed() code path
69 * - otherwise, validate the onscreen lines
70 * - DO NOT process updates
72 * In both cases, validating the onscreen lines can trigger a scroll
73 * due to maintaining the first_para on the top of the screen.
74 * If validation triggers a scroll, we jump to the top of the code path
75 * for value_changed, and bail out of the current code path.
77 * Also, in size_allocate, if we invalidate some lines from changing
78 * the layout width, we need to go ahead and run the high-priority idle,
79 * because GTK sends exposes right after doing the size allocates without
80 * returning to the main loop. This is also why the high-priority idle
81 * is at a higher priority than resizing.
86 #define DEBUG_VALIDATION_AND_SCROLLING
89 #ifdef DEBUG_VALIDATION_AND_SCROLLING
95 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
96 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
98 struct _GtkTextPendingScroll
101 gdouble within_margin;
109 SET_SCROLL_ADJUSTMENTS,
127 PROP_PIXELS_ABOVE_LINES,
128 PROP_PIXELS_BELOW_LINES,
129 PROP_PIXELS_INSIDE_WRAP,
141 static void gtk_text_view_init (GtkTextView *text_view);
142 static void gtk_text_view_class_init (GtkTextViewClass *klass);
143 static void gtk_text_view_destroy (GtkObject *object);
144 static void gtk_text_view_finalize (GObject *object);
145 static void gtk_text_view_set_property (GObject *object,
149 static void gtk_text_view_get_property (GObject *object,
153 static void gtk_text_view_size_request (GtkWidget *widget,
154 GtkRequisition *requisition);
155 static void gtk_text_view_size_allocate (GtkWidget *widget,
156 GtkAllocation *allocation);
157 static void gtk_text_view_realize (GtkWidget *widget);
158 static void gtk_text_view_unrealize (GtkWidget *widget);
159 static void gtk_text_view_style_set (GtkWidget *widget,
160 GtkStyle *previous_style);
161 static void gtk_text_view_direction_changed (GtkWidget *widget,
162 GtkTextDirection previous_direction);
163 static gint gtk_text_view_event (GtkWidget *widget,
165 static gint gtk_text_view_key_press_event (GtkWidget *widget,
167 static gint gtk_text_view_key_release_event (GtkWidget *widget,
169 static gint gtk_text_view_button_press_event (GtkWidget *widget,
170 GdkEventButton *event);
171 static gint gtk_text_view_button_release_event (GtkWidget *widget,
172 GdkEventButton *event);
173 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
174 GdkEventFocus *event);
175 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
176 GdkEventFocus *event);
177 static gint gtk_text_view_motion_event (GtkWidget *widget,
178 GdkEventMotion *event);
179 static gint gtk_text_view_expose_event (GtkWidget *widget,
180 GdkEventExpose *expose);
181 static void gtk_text_view_draw_focus (GtkWidget *widget);
182 static void gtk_text_view_grab_focus (GtkWidget *widget);
183 static gboolean gtk_text_view_focus (GtkWidget *widget,
184 GtkDirectionType direction);
187 /* Source side drag signals */
188 static void gtk_text_view_drag_begin (GtkWidget *widget,
189 GdkDragContext *context);
190 static void gtk_text_view_drag_end (GtkWidget *widget,
191 GdkDragContext *context);
192 static void gtk_text_view_drag_data_get (GtkWidget *widget,
193 GdkDragContext *context,
194 GtkSelectionData *selection_data,
197 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
198 GdkDragContext *context);
200 /* Target side drag signals */
201 static void gtk_text_view_drag_leave (GtkWidget *widget,
202 GdkDragContext *context,
204 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
205 GdkDragContext *context,
209 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
210 GdkDragContext *context,
214 static void gtk_text_view_drag_data_received (GtkWidget *widget,
215 GdkDragContext *context,
218 GtkSelectionData *selection_data,
222 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
224 GtkAdjustment *vadj);
225 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
227 static void gtk_text_view_move_cursor (GtkTextView *text_view,
228 GtkMovementStep step,
230 gboolean extend_selection);
231 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
233 gboolean extend_selection);
234 static void gtk_text_view_set_anchor (GtkTextView *text_view);
235 static void gtk_text_view_scroll_pages (GtkTextView *text_view,
237 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
239 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
241 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
244 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
245 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
246 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
247 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
248 static void gtk_text_view_move_focus (GtkTextView *text_view,
249 GtkDirectionType direction_type);
250 static void gtk_text_view_unselect (GtkTextView *text_view);
252 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
253 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
255 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
256 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
257 GtkTextAttributes *values,
259 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
260 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
261 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
262 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
263 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
264 const GtkTextIter *iter,
265 GdkEventButton *event);
266 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
267 GdkEventButton *event);
268 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
269 const GtkTextIter *iter,
270 GdkEventMotion *event);
271 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
272 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
273 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
275 static void gtk_text_view_value_changed (GtkAdjustment *adj,
277 static void gtk_text_view_commit_handler (GtkIMContext *context,
279 GtkTextView *text_view);
280 static void gtk_text_view_commit_text (GtkTextView *text_view,
282 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
283 GtkTextView *text_view);
284 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
285 GtkTextView *text_view);
286 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
289 GtkTextView *text_view);
291 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
292 const GtkTextIter *location,
295 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
298 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
302 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
303 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
305 static void gtk_text_view_do_popup (GtkTextView *text_view,
306 GdkEventButton *event);
308 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
310 gdouble within_margin,
315 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
316 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
317 static void gtk_text_view_invalidate (GtkTextView *text_view);
318 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
320 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
322 /* Container methods */
323 static void gtk_text_view_add (GtkContainer *container,
325 static void gtk_text_view_remove (GtkContainer *container,
327 static void gtk_text_view_forall (GtkContainer *container,
328 gboolean include_internals,
329 GtkCallback callback,
330 gpointer callback_data);
332 /* FIXME probably need the focus methods. */
334 typedef struct _GtkTextViewChild GtkTextViewChild;
336 struct _GtkTextViewChild
340 GtkTextChildAnchor *anchor;
342 gint from_top_of_line;
343 gint from_left_of_buffer;
345 /* These are ignored if anchor != NULL */
346 GtkTextWindowType type;
351 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
352 GtkTextChildAnchor *anchor,
353 GtkTextLayout *layout);
354 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
355 GtkTextWindowType type,
358 static void text_view_child_free (GtkTextViewChild *child);
359 static void text_view_child_set_parent_window (GtkTextView *text_view,
360 GtkTextViewChild *child);
362 struct _GtkTextWindow
364 GtkTextWindowType type;
367 GdkWindow *bin_window;
368 GtkRequisition requisition;
369 GdkRectangle allocation;
372 static GtkTextWindow *text_window_new (GtkTextWindowType type,
375 gint height_request);
376 static void text_window_free (GtkTextWindow *win);
377 static void text_window_realize (GtkTextWindow *win,
379 static void text_window_unrealize (GtkTextWindow *win);
380 static void text_window_size_allocate (GtkTextWindow *win,
382 static void text_window_scroll (GtkTextWindow *win,
385 static void text_window_invalidate_rect (GtkTextWindow *win,
388 static gint text_window_get_width (GtkTextWindow *win);
389 static gint text_window_get_height (GtkTextWindow *win);
390 static void text_window_get_allocation (GtkTextWindow *win,
398 TARGET_COMPOUND_TEXT,
400 TARGET_TEXT_BUFFER_CONTENTS
403 static GtkTargetEntry target_table[] = {
404 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
405 TARGET_TEXT_BUFFER_CONTENTS },
406 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
407 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
408 { "TEXT", 0, TARGET_TEXT },
409 { "STRING", 0, TARGET_STRING }
412 static GtkContainerClass *parent_class = NULL;
413 static guint signals[LAST_SIGNAL] = { 0 };
416 gtk_text_view_get_type (void)
418 static GtkType our_type = 0;
422 static const GtkTypeInfo our_info =
425 sizeof (GtkTextView),
426 sizeof (GtkTextViewClass),
427 (GtkClassInitFunc) gtk_text_view_class_init,
428 (GtkObjectInitFunc) gtk_text_view_init,
429 /* reserved_1 */ NULL,
430 /* reserved_2 */ NULL,
431 (GtkClassInitFunc) NULL
434 our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
441 add_move_binding (GtkBindingSet *binding_set,
444 GtkMovementStep step,
447 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
449 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
453 GTK_TYPE_BOOL, FALSE);
455 /* Selection-extending version */
456 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
460 GTK_TYPE_BOOL, TRUE);
464 gtk_text_view_class_init (GtkTextViewClass *klass)
466 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
467 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
468 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
469 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
470 GtkBindingSet *binding_set;
472 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
474 /* Default handlers and virtual methods
476 gobject_class->set_property = gtk_text_view_set_property;
477 gobject_class->get_property = gtk_text_view_get_property;
479 object_class->destroy = gtk_text_view_destroy;
480 gobject_class->finalize = gtk_text_view_finalize;
482 widget_class->realize = gtk_text_view_realize;
483 widget_class->unrealize = gtk_text_view_unrealize;
484 widget_class->style_set = gtk_text_view_style_set;
485 widget_class->direction_changed = gtk_text_view_direction_changed;
486 widget_class->size_request = gtk_text_view_size_request;
487 widget_class->size_allocate = gtk_text_view_size_allocate;
488 widget_class->event = gtk_text_view_event;
489 widget_class->key_press_event = gtk_text_view_key_press_event;
490 widget_class->key_release_event = gtk_text_view_key_release_event;
491 widget_class->button_press_event = gtk_text_view_button_press_event;
492 widget_class->button_release_event = gtk_text_view_button_release_event;
493 widget_class->focus_in_event = gtk_text_view_focus_in_event;
494 widget_class->focus_out_event = gtk_text_view_focus_out_event;
495 widget_class->motion_notify_event = gtk_text_view_motion_event;
496 widget_class->expose_event = gtk_text_view_expose_event;
497 widget_class->grab_focus = gtk_text_view_grab_focus;
498 widget_class->focus = gtk_text_view_focus;
500 widget_class->drag_begin = gtk_text_view_drag_begin;
501 widget_class->drag_end = gtk_text_view_drag_end;
502 widget_class->drag_data_get = gtk_text_view_drag_data_get;
503 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
505 widget_class->drag_leave = gtk_text_view_drag_leave;
506 widget_class->drag_motion = gtk_text_view_drag_motion;
507 widget_class->drag_drop = gtk_text_view_drag_drop;
508 widget_class->drag_data_received = gtk_text_view_drag_data_received;
510 widget_class->popup_menu = gtk_text_view_popup_menu;
512 container_class->add = gtk_text_view_add;
513 container_class->remove = gtk_text_view_remove;
514 container_class->forall = gtk_text_view_forall;
516 klass->move_cursor = gtk_text_view_move_cursor;
517 klass->page_horizontally = gtk_text_view_page_horizontally;
518 klass->set_anchor = gtk_text_view_set_anchor;
519 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
520 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
521 klass->cut_clipboard = gtk_text_view_cut_clipboard;
522 klass->copy_clipboard = gtk_text_view_copy_clipboard;
523 klass->paste_clipboard = gtk_text_view_paste_clipboard;
524 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
525 klass->move_focus = gtk_text_view_move_focus;
526 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
532 g_object_class_install_property (gobject_class,
533 PROP_PIXELS_ABOVE_LINES,
534 g_param_spec_int ("pixels_above_lines",
535 _("Pixels Above Lines"),
536 _("Pixels of blank space above paragraphs"),
542 g_object_class_install_property (gobject_class,
543 PROP_PIXELS_BELOW_LINES,
544 g_param_spec_int ("pixels_below_lines",
545 _("Pixels Below Lines"),
546 _("Pixels of blank space below paragraphs"),
552 g_object_class_install_property (gobject_class,
553 PROP_PIXELS_INSIDE_WRAP,
554 g_param_spec_int ("pixels_inside_wrap",
555 _("Pixels Inside Wrap"),
556 _("Pixels of blank space between wrapped lines in a paragraph"),
562 g_object_class_install_property (gobject_class,
564 g_param_spec_boolean ("editable",
566 _("Whether the text can be modified by the user"),
570 g_object_class_install_property (gobject_class,
572 g_param_spec_enum ("wrap_mode",
574 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
579 g_object_class_install_property (gobject_class,
581 g_param_spec_enum ("justification",
583 _("Left, right, or center justification"),
584 GTK_TYPE_JUSTIFICATION,
588 g_object_class_install_property (gobject_class,
590 g_param_spec_int ("left_margin",
592 _("Width of the left margin in pixels"),
598 g_object_class_install_property (gobject_class,
600 g_param_spec_int ("right_margin",
602 _("Width of the right margin in pixels"),
608 g_object_class_install_property (gobject_class,
610 g_param_spec_int ("indent",
612 _("Amount to indent the paragraph, in pixels"),
618 g_object_class_install_property (gobject_class,
620 g_param_spec_boxed ("tabs",
622 _("Custom tabs for this text"),
623 PANGO_TYPE_TAB_ARRAY,
626 g_object_class_install_property (gobject_class,
628 g_param_spec_boolean ("cursor_visible",
630 _("If the insertion cursor is shown"),
639 signals[MOVE_CURSOR] =
640 gtk_signal_new ("move_cursor",
641 GTK_RUN_LAST | GTK_RUN_ACTION,
642 GTK_CLASS_TYPE (object_class),
643 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
644 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
645 GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
647 signals[PAGE_HORIZONTALLY] =
648 gtk_signal_new ("page_horizontally",
649 GTK_RUN_LAST | GTK_RUN_ACTION,
650 GTK_CLASS_TYPE (object_class),
651 GTK_SIGNAL_OFFSET (GtkTextViewClass, page_horizontally),
652 _gtk_marshal_VOID__INT_BOOLEAN,
653 GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_BOOL);
655 signals[SET_ANCHOR] =
656 gtk_signal_new ("set_anchor",
657 GTK_RUN_LAST | GTK_RUN_ACTION,
658 GTK_CLASS_TYPE (object_class),
659 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
660 _gtk_marshal_VOID__VOID,
663 signals[INSERT_AT_CURSOR] =
664 gtk_signal_new ("insert_at_cursor",
665 GTK_RUN_LAST | GTK_RUN_ACTION,
666 GTK_CLASS_TYPE (object_class),
667 GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
668 _gtk_marshal_VOID__STRING,
669 GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
671 signals[DELETE_FROM_CURSOR] =
672 gtk_signal_new ("delete_from_cursor",
673 GTK_RUN_LAST | GTK_RUN_ACTION,
674 GTK_CLASS_TYPE (object_class),
675 GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
676 _gtk_marshal_VOID__ENUM_INT,
677 GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
679 signals[CUT_CLIPBOARD] =
680 gtk_signal_new ("cut_clipboard",
681 GTK_RUN_LAST | GTK_RUN_ACTION,
682 GTK_CLASS_TYPE (object_class),
683 GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
684 _gtk_marshal_VOID__VOID,
687 signals[COPY_CLIPBOARD] =
688 gtk_signal_new ("copy_clipboard",
689 GTK_RUN_LAST | GTK_RUN_ACTION,
690 GTK_CLASS_TYPE (object_class),
691 GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
692 _gtk_marshal_VOID__VOID,
695 signals[PASTE_CLIPBOARD] =
696 gtk_signal_new ("paste_clipboard",
697 GTK_RUN_LAST | GTK_RUN_ACTION,
698 GTK_CLASS_TYPE (object_class),
699 GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
700 _gtk_marshal_VOID__VOID,
703 signals[TOGGLE_OVERWRITE] =
704 gtk_signal_new ("toggle_overwrite",
705 GTK_RUN_LAST | GTK_RUN_ACTION,
706 GTK_CLASS_TYPE (object_class),
707 GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
708 _gtk_marshal_VOID__VOID,
711 signals[MOVE_FOCUS] =
712 gtk_signal_new ("move_focus",
713 GTK_RUN_LAST | GTK_RUN_ACTION,
714 GTK_CLASS_TYPE (object_class),
715 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_focus),
716 _gtk_marshal_VOID__ENUM,
717 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
719 signals[SET_SCROLL_ADJUSTMENTS] =
720 gtk_signal_new ("set_scroll_adjustments",
722 GTK_CLASS_TYPE (object_class),
723 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
724 _gtk_marshal_VOID__OBJECT_OBJECT,
725 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
726 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
728 signals[POPULATE_POPUP] =
729 gtk_signal_new ("populate_popup",
731 GTK_CLASS_TYPE (object_class),
732 GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
733 _gtk_marshal_VOID__OBJECT,
734 GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
740 binding_set = gtk_binding_set_by_class (klass);
742 /* Moving the insertion point */
743 add_move_binding (binding_set, GDK_Right, 0,
744 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
746 add_move_binding (binding_set, GDK_KP_Right, 0,
747 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
749 add_move_binding (binding_set, GDK_Left, 0,
750 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
752 add_move_binding (binding_set, GDK_KP_Left, 0,
753 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
755 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
756 GTK_MOVEMENT_WORDS, 1);
758 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
759 GTK_MOVEMENT_WORDS, 1);
761 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
762 GTK_MOVEMENT_WORDS, -1);
764 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
765 GTK_MOVEMENT_WORDS, 1);
767 add_move_binding (binding_set, GDK_Up, 0,
768 GTK_MOVEMENT_DISPLAY_LINES, -1);
770 add_move_binding (binding_set, GDK_KP_Up, 0,
771 GTK_MOVEMENT_DISPLAY_LINES, -1);
773 add_move_binding (binding_set, GDK_Down, 0,
774 GTK_MOVEMENT_DISPLAY_LINES, 1);
776 add_move_binding (binding_set, GDK_KP_Down, 0,
777 GTK_MOVEMENT_DISPLAY_LINES, 1);
779 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
780 GTK_MOVEMENT_PARAGRAPHS, -1);
782 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
783 GTK_MOVEMENT_PARAGRAPHS, -1);
785 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
786 GTK_MOVEMENT_PARAGRAPHS, 1);
788 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
789 GTK_MOVEMENT_PARAGRAPHS, 1);
791 add_move_binding (binding_set, GDK_Home, 0,
792 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
794 add_move_binding (binding_set, GDK_KP_Home, 0,
795 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
797 add_move_binding (binding_set, GDK_End, 0,
798 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
800 add_move_binding (binding_set, GDK_KP_End, 0,
801 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
803 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
804 GTK_MOVEMENT_BUFFER_ENDS, -1);
806 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
807 GTK_MOVEMENT_BUFFER_ENDS, -1);
809 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
810 GTK_MOVEMENT_BUFFER_ENDS, 1);
812 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
813 GTK_MOVEMENT_BUFFER_ENDS, 1);
815 add_move_binding (binding_set, GDK_Page_Up, 0,
816 GTK_MOVEMENT_PAGES, -1);
818 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
819 GTK_MOVEMENT_PAGES, -1);
821 add_move_binding (binding_set, GDK_Page_Down, 0,
822 GTK_MOVEMENT_PAGES, 1);
824 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
825 GTK_MOVEMENT_PAGES, 1);
829 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
831 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
833 GTK_TYPE_BOOL, FALSE);
834 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
836 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
838 GTK_TYPE_BOOL, TRUE);
841 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
842 "page_horizontally", 2,
844 GTK_TYPE_BOOL, FALSE);
846 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
847 "page_horizontally", 2,
849 GTK_TYPE_BOOL, TRUE);
851 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
852 "page_horizontally", 2,
854 GTK_TYPE_BOOL, FALSE);
856 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
857 "page_horizontally", 2,
859 GTK_TYPE_BOOL, TRUE);
861 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
862 "page_horizontally", 2,
864 GTK_TYPE_BOOL, FALSE);
866 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
867 "page_horizontally", 2,
869 GTK_TYPE_BOOL, TRUE);
871 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
872 "page_horizontally", 2,
874 GTK_TYPE_BOOL, FALSE);
876 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
877 "page_horizontally", 2,
879 GTK_TYPE_BOOL, TRUE);
882 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
883 "delete_from_cursor", 2,
884 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
887 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
888 "delete_from_cursor", 2,
889 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
892 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
893 "delete_from_cursor", 2,
894 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
897 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
898 "delete_from_cursor", 2,
899 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
902 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
903 "delete_from_cursor", 2,
904 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
907 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
908 "delete_from_cursor", 2,
909 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
914 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
917 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
918 "copy_clipboard", 0);
920 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
921 "paste_clipboard", 0);
924 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
925 "toggle_overwrite", 0);
926 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
927 "toggle_overwrite", 0);
929 /* Control-tab focus motion */
930 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
932 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
933 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
935 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
936 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, GDK_CONTROL_MASK,
938 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
940 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
942 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
943 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
945 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
946 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
948 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
952 gtk_text_view_init (GtkTextView *text_view)
956 widget = GTK_WIDGET (text_view);
958 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
960 /* Set up default style */
961 text_view->wrap_mode = GTK_WRAP_NONE;
962 text_view->pixels_above_lines = 0;
963 text_view->pixels_below_lines = 0;
964 text_view->pixels_inside_wrap = 0;
965 text_view->justify = GTK_JUSTIFY_LEFT;
966 text_view->left_margin = 0;
967 text_view->right_margin = 0;
968 text_view->indent = 0;
969 text_view->tabs = NULL;
970 text_view->editable = TRUE;
972 gtk_drag_dest_set (widget,
974 target_table, G_N_ELEMENTS (target_table),
975 GDK_ACTION_COPY | GDK_ACTION_MOVE);
977 text_view->virtual_cursor_x = -1;
978 text_view->virtual_cursor_y = -1;
980 /* This object is completely private. No external entity can gain a reference
981 * to it; so we create it here and destroy it in finalize ().
983 text_view->im_context = gtk_im_multicontext_new ();
985 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
986 G_CALLBACK (gtk_text_view_commit_handler), text_view);
987 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
988 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
989 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
990 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
991 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
992 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
994 text_view->cursor_visible = TRUE;
996 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
999 text_view->drag_start_x = -1;
1000 text_view->drag_start_y = -1;
1002 text_view->pending_place_cursor_button = 0;
1004 /* We handle all our own redrawing */
1005 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1009 * gtk_text_view_new:
1011 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1012 * before using the text view, an empty default buffer will be created
1013 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1014 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1016 * Return value: a new #GtkTextView
1019 gtk_text_view_new (void)
1021 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1025 * gtk_text_view_new_with_buffer:
1026 * @buffer: a #GtkTextBuffer
1028 * Creates a new #GtkTextView widget displaying the buffer
1029 * @buffer. One buffer can be shared among many widgets.
1030 * @buffer may be NULL to create a default buffer, in which case
1031 * this function is equivalent to gtk_text_view_new(). The
1032 * text view adds its own reference count to the buffer; it does not
1033 * take over an existing reference.
1035 * Return value: a new #GtkTextView.
1038 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1040 GtkTextView *text_view;
1042 text_view = (GtkTextView*)gtk_text_view_new ();
1044 gtk_text_view_set_buffer (text_view, buffer);
1046 return GTK_WIDGET (text_view);
1050 * gtk_text_view_set_buffer:
1051 * @text_view: a #GtkTextView
1052 * @buffer: a #GtkTextBuffer
1054 * Sets @buffer as the buffer being displayed by @text_view. The previous
1055 * buffer displayed by the text view is unreferenced, and a reference is
1056 * added to @buffer. If you owned a reference to @buffer before passing it
1057 * to this function, you must remove that reference yourself; #GtkTextView
1058 * will not "adopt" it.
1062 gtk_text_view_set_buffer (GtkTextView *text_view,
1063 GtkTextBuffer *buffer)
1065 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1066 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1068 if (text_view->buffer == buffer)
1071 if (text_view->buffer != NULL)
1073 /* Destroy all anchored children */
1077 copy = g_slist_copy (text_view->children);
1079 while (tmp_list != NULL)
1081 GtkTextViewChild *vc = tmp_list->data;
1085 gtk_widget_destroy (vc->widget);
1086 /* vc may now be invalid! */
1089 tmp_list = g_slist_next (tmp_list);
1092 g_slist_free (copy);
1094 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1095 gtk_text_view_mark_set_handler, text_view);
1096 g_object_unref (G_OBJECT (text_view->buffer));
1097 text_view->dnd_mark = NULL;
1099 if (GTK_WIDGET_REALIZED (text_view))
1100 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
1101 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1104 text_view->buffer = buffer;
1110 g_object_ref (G_OBJECT (buffer));
1112 if (text_view->layout)
1113 gtk_text_layout_set_buffer (text_view->layout, buffer);
1115 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1117 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1121 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1125 text_view->first_para_pixels = 0;
1127 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1128 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1130 if (GTK_WIDGET_REALIZED (text_view))
1131 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
1132 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1135 if (GTK_WIDGET_VISIBLE (text_view))
1136 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1138 DV(g_print ("Invalidating due to set_buffer\n"));
1139 gtk_text_view_invalidate (text_view);
1142 static GtkTextBuffer*
1143 get_buffer (GtkTextView *text_view)
1145 if (text_view->buffer == NULL)
1148 b = gtk_text_buffer_new (NULL);
1149 gtk_text_view_set_buffer (text_view, b);
1150 g_object_unref (G_OBJECT (b));
1153 return text_view->buffer;
1157 * gtk_text_view_get_buffer:
1158 * @text_view: a #GtkTextView
1160 * Returns the #GtkTextBuffer being displayed by this text view.
1161 * The reference count on the buffer is not incremented; the caller
1162 * of this function won't own a new reference.
1164 * Return value: a #GtkTextBuffer
1167 gtk_text_view_get_buffer (GtkTextView *text_view)
1169 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1171 return get_buffer (text_view);
1175 * gtk_text_view_get_iter_at_location:
1176 * @text_view: a #GtkTextView
1177 * @iter: a #GtkTextIter
1178 * @x: x position, in buffer coordinates
1179 * @y: y position, in buffer coordinates
1181 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1182 * coordinates are coordinates for the entire buffer, not just the
1183 * currently-displayed portion. If you have coordinates from an
1184 * event, you have to convert those to buffer coordinates with
1185 * gtk_text_view_window_to_buffer_coords().
1189 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1194 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1195 g_return_if_fail (iter != NULL);
1196 g_return_if_fail (text_view->layout != NULL);
1198 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1205 * gtk_text_view_get_iter_location:
1206 * @text_view: a #GtkTextView
1207 * @iter: a #GtkTextIter
1208 * @location: bounds of the character at @iter
1210 * Gets a rectangle which roughly contains the character at @iter.
1211 * The rectangle position is in buffer coordinates; use
1212 * gtk_text_view_buffer_to_window_coords() to convert these
1213 * coordinates to coordinates for one of the windows in the text view.
1217 gtk_text_view_get_iter_location (GtkTextView *text_view,
1218 const GtkTextIter *iter,
1219 GdkRectangle *location)
1221 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1222 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1224 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1228 * gtk_text_view_get_line_yrange:
1229 * @text_view: a #GtkTextView
1230 * @iter: a #GtkTextIter
1231 * @y: return location for a y coordinate
1232 * @height: return location for a height
1234 * Gets the y coordinate of the top of the line containing @iter,
1235 * and the height of the line. The coordinate is a buffer coordinate;
1236 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1240 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1241 const GtkTextIter *iter,
1245 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1246 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1248 gtk_text_layout_get_line_yrange (text_view->layout,
1255 * gtk_text_view_get_line_at_y:
1256 * @text_view: a #GtkTextView
1257 * @target_iter: a #GtkTextIter
1258 * @y: a y coordinate
1259 * @line_top: return location for top coordinate of the line
1261 * Gets the #GtkTextIter at the start of the line containing
1262 * the coordinate @y. @y is in buffer coordinates, convert from
1263 * window coordinates with gtk_text_view_window_to_buffer_coords().
1264 * If non-%NULL, @line_top will be filled with the coordinate of the top
1268 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1269 GtkTextIter *target_iter,
1273 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1275 gtk_text_layout_get_line_at_y (text_view->layout,
1282 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1284 DV (g_print (" Setting adj to raw value %g\n", val));
1286 /* We don't really want to clamp to upper; we want to clamp to
1287 upper - page_size which is the highest value the scrollbar
1288 will let us reach. */
1289 if (val > (adj->upper - adj->page_size))
1290 val = adj->upper - adj->page_size;
1292 if (val < adj->lower)
1295 if (val != adj->value)
1297 DV (g_print (" Setting adj to clamped value %g\n", val));
1298 gtk_adjustment_set_value (adj, val);
1306 * gtk_text_view_scroll_to_iter:
1307 * @text_view: a #GtkTextView
1308 * @iter: a #GtkTextIter
1309 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1310 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1311 * @xalign: horizontal alignment of mark within visible area.
1312 * @yalign: vertical alignment of mark within visible area
1314 * Scrolls @text_view so that @iter is on the screen in the position
1315 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1316 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1317 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1318 * possibly not scrolling at all. The effective screen for purposes
1319 * of this function is reduced by a margin of size @within_margin.
1320 * NOTE: This function uses the currently-computed height of the
1321 * lines in the text buffer. Note that line heights are computed
1322 * in an idle handler; so this function may not have the desired effect
1323 * if it's called before the height computations. To avoid oddness,
1324 * consider using gtk_text_view_scroll_to_mark() which saves a point
1325 * to be scrolled to after line validation.
1327 * Return value: %TRUE if scrolling occurred
1330 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1332 gdouble within_margin,
1338 GdkRectangle screen;
1343 gboolean retval = FALSE;
1345 gint screen_xoffset, screen_yoffset;
1346 gint current_x_scroll, current_y_scroll;
1348 /* FIXME why don't we do the validate-at-scroll-destination thing
1349 * from flush_scroll in this function? I think it wasn't done before
1350 * because changed_handler was screwed up, but I could be wrong.
1353 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1354 g_return_val_if_fail (iter != NULL, FALSE);
1355 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1356 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1357 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1359 widget = GTK_WIDGET (text_view);
1361 DV(g_print(G_STRLOC"\n"));
1363 gtk_text_layout_get_iter_location (text_view->layout,
1367 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1369 current_x_scroll = text_view->xoffset;
1370 current_y_scroll = text_view->yoffset;
1372 screen.x = current_x_scroll;
1373 screen.y = current_y_scroll;
1374 screen.width = SCREEN_WIDTH (widget);
1375 screen.height = SCREEN_HEIGHT (widget);
1377 screen_xoffset = screen.width * within_margin;
1378 screen_yoffset = screen.height * within_margin;
1380 screen.x += screen_xoffset;
1381 screen.y += screen_yoffset;
1382 screen.width -= screen_xoffset * 2;
1383 screen.height -= screen_yoffset * 2;
1385 /* paranoia check */
1386 if (screen.width < 1)
1388 if (screen.height < 1)
1391 screen_right = screen.x + screen.width;
1392 screen_bottom = screen.y + screen.height;
1394 /* The alignment affects the point in the target character that we
1395 * choose to align. If we're doing right/bottom alignment, we align
1396 * the right/bottom edge of the character the mark is at; if we're
1397 * doing left/top we align the left/top edge of the character; if
1398 * we're doing center alignment we align the center of the
1402 /* Vertical scroll */
1405 scroll_dest = current_y_scroll;
1409 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1411 /* if scroll_dest < screen.y, we move a negative increment (up),
1412 * else a positive increment (down)
1414 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1418 /* move minimum to get onscreen */
1419 if (rect.y < screen.y)
1421 scroll_dest = rect.y;
1422 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1424 else if ((rect.y + rect.height) > screen_bottom)
1426 scroll_dest = rect.y + rect.height;
1427 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1431 if (scroll_inc != 0)
1433 retval = set_adjustment_clamped (get_vadjustment (text_view),
1434 current_y_scroll + scroll_inc);
1436 DV (g_print (" vert increment %d\n", scroll_inc));
1439 /* Horizontal scroll */
1442 scroll_dest = current_x_scroll;
1446 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1448 /* if scroll_dest < screen.y, we move a negative increment (left),
1449 * else a positive increment (right)
1451 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1455 /* move minimum to get onscreen */
1456 if (rect.x < screen.x)
1458 scroll_dest = rect.x;
1459 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1461 else if ((rect.x + rect.width) > screen_right)
1463 scroll_dest = rect.x + rect.width;
1464 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1468 if (scroll_inc != 0)
1470 retval = set_adjustment_clamped (get_hadjustment (text_view),
1471 current_x_scroll + scroll_inc);
1473 DV (g_print (" horiz increment %d\n", scroll_inc));
1477 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1479 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1485 free_pending_scroll (GtkTextPendingScroll *scroll)
1487 if (!gtk_text_mark_get_deleted (scroll->mark))
1488 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1490 g_object_unref (G_OBJECT (scroll->mark));
1495 gtk_text_view_queue_scroll (GtkTextView *text_view,
1497 gdouble within_margin,
1503 GtkTextPendingScroll *scroll;
1505 DV(g_print(G_STRLOC"\n"));
1507 scroll = g_new (GtkTextPendingScroll, 1);
1509 scroll->within_margin = within_margin;
1510 scroll->use_align = use_align;
1511 scroll->xalign = xalign;
1512 scroll->yalign = yalign;
1514 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1516 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1519 gtk_text_mark_get_left_gravity (mark));
1521 g_object_ref (G_OBJECT (scroll->mark));
1523 if (text_view->pending_scroll)
1524 free_pending_scroll (text_view->pending_scroll);
1526 text_view->pending_scroll = scroll;
1530 gtk_text_view_flush_scroll (GtkTextView *text_view)
1533 GtkTextPendingScroll *scroll;
1537 widget = GTK_WIDGET (text_view);
1539 DV(g_print(G_STRLOC"\n"));
1541 if (text_view->pending_scroll == NULL)
1543 DV (g_print ("in flush scroll, no pending scroll\n"));
1547 scroll = text_view->pending_scroll;
1549 /* avoid recursion */
1550 text_view->pending_scroll = NULL;
1552 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1554 /* Validate area around the scroll destination, so the adjustment
1555 * can meaningfully point into that area. We must validate
1556 * enough area to be sure that after we scroll, everything onscreen
1557 * is valid; otherwise, validation will maintain the first para
1558 * in one place, but may push the target iter off the bottom of
1561 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1562 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1563 - (widget->allocation.height * 2),
1564 widget->allocation.height * 2);
1566 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1568 /* Ensure we have updated width/height */
1569 gtk_text_view_update_adjustments (text_view);
1571 retval = gtk_text_view_scroll_to_iter (text_view,
1573 scroll->within_margin,
1578 free_pending_scroll (scroll);
1584 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1586 if (upper != adj->upper)
1588 gdouble min = MAX (0.0, upper - adj->page_size);
1589 gboolean value_changed = FALSE;
1593 if (adj->value > min)
1596 value_changed = TRUE;
1599 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1600 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1604 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1605 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1611 gtk_text_view_update_adjustments (GtkTextView *text_view)
1613 gint width = 0, height = 0;
1615 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1617 if (text_view->layout)
1618 gtk_text_layout_get_size (text_view->layout, &width, &height);
1620 if (text_view->width != width || text_view->height != height)
1622 text_view->width = width;
1623 text_view->height = height;
1625 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1626 MAX (SCREEN_WIDTH (text_view), width));
1627 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1628 MAX (SCREEN_HEIGHT (text_view), height));
1630 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1632 /* Set up the step sizes; we'll say that a page is
1633 our allocation minus one step, and a step is
1634 1/10 of our allocation. */
1635 text_view->hadjustment->step_increment =
1636 SCREEN_WIDTH (text_view) / 10.0;
1637 text_view->hadjustment->page_increment =
1638 SCREEN_WIDTH (text_view) * 0.9;
1640 text_view->vadjustment->step_increment =
1641 SCREEN_HEIGHT (text_view) / 10.0;
1642 text_view->vadjustment->page_increment =
1643 SCREEN_HEIGHT (text_view) * 0.9;
1645 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1646 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1651 gtk_text_view_update_layout_width (GtkTextView *text_view)
1653 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1655 gtk_text_view_ensure_layout (text_view);
1657 gtk_text_layout_set_screen_width (text_view->layout,
1658 SCREEN_WIDTH (text_view));
1662 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1665 gint cursor_x_pos, cursor_y_pos;
1667 if (text_view->layout == NULL)
1670 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1672 area.x = cursor_x_pos;
1673 area.y = cursor_y_pos;
1674 area.width = area.height = 0;
1676 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1680 * gtk_text_view_scroll_to_mark:
1681 * @text_view: a #GtkTextView
1682 * @mark: a #GtkTextMark
1683 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1684 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1685 * @xalign: horizontal alignment of mark within visible area.
1686 * @yalign: vertical alignment of mark within visible area
1688 * Scrolls @text_view so that @mark is on the screen in the position
1689 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1690 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1691 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1692 * possibly not scrolling at all. The effective screen for purposes
1693 * of this function is reduced by a margin of size @within_margin.
1697 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1699 gdouble within_margin,
1704 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1705 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1706 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1707 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1708 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1710 gtk_text_view_queue_scroll (text_view, mark,
1716 /* If no validation is pending, we need to go ahead and force an
1719 if (text_view->layout &&
1720 gtk_text_layout_is_valid (text_view->layout))
1721 gtk_text_view_flush_scroll (text_view);
1725 * gtk_text_view_scroll_mark_onscreen:
1726 * @text_view: a #GtkTextView
1727 * @mark: a mark in the buffer for @text_view
1729 * Scrolls @text_view the minimum distance such that @mark is contained
1730 * within the visible area of the widget.
1734 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1737 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1738 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1740 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1744 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1746 GdkRectangle visible_rect;
1747 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1749 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1751 visible_rect.y + visible_rect.height);
1755 * gtk_text_view_move_mark_onscreen:
1756 * @text_view: a #GtkTextView
1757 * @mark: a #GtkTextMark
1759 * Moves a mark within the buffer so that it's
1760 * located within the currently-visible text area.
1762 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1765 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1770 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1771 g_return_val_if_fail (mark != NULL, FALSE);
1773 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1775 if (clamp_iter_onscreen (text_view, &iter))
1777 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1785 * gtk_text_view_get_visible_rect:
1786 * @text_view: a #GtkTextView
1787 * @visible_rect: rectangle to fill
1789 * Fills @visible_rect with the currently-visible
1790 * region of the buffer, in buffer coordinates. Convert to window coordinates
1791 * with gtk_text_view_buffer_to_window_coords().
1794 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1795 GdkRectangle *visible_rect)
1799 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1801 widget = GTK_WIDGET (text_view);
1805 visible_rect->x = text_view->xoffset;
1806 visible_rect->y = text_view->yoffset;
1807 visible_rect->width = SCREEN_WIDTH (widget);
1808 visible_rect->height = SCREEN_HEIGHT (widget);
1810 DV(g_print(" visible rect: %d,%d %d x %d\n",
1813 visible_rect->width,
1814 visible_rect->height));
1819 * gtk_text_view_set_wrap_mode:
1820 * @text_view: a #GtkTextView
1821 * @wrap_mode: a #GtkWrapMode
1823 * Sets the line wrapping for the view.
1826 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1827 GtkWrapMode wrap_mode)
1829 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1831 if (text_view->wrap_mode != wrap_mode)
1833 text_view->wrap_mode = wrap_mode;
1835 if (text_view->layout)
1837 text_view->layout->default_style->wrap_mode = wrap_mode;
1838 gtk_text_layout_default_style_changed (text_view->layout);
1842 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1846 * gtk_text_view_get_wrap_mode:
1847 * @text_view: a #GtkTextView
1849 * Gets the line wrapping for the view.
1851 * Return value: the line wrap setting
1854 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1856 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1858 return text_view->wrap_mode;
1862 * gtk_text_view_set_editable:
1863 * @text_view: a #GtkTextView
1864 * @setting: whether it's editable
1866 * Sets the default editability of the #GtkTextView. You can override
1867 * this default setting with tags in the buffer, using the "editable"
1868 * attribute of tags.
1871 gtk_text_view_set_editable (GtkTextView *text_view,
1874 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1875 setting = setting != FALSE;
1877 if (text_view->editable != setting)
1879 text_view->editable = setting;
1881 if (text_view->layout)
1883 text_view->layout->default_style->editable = text_view->editable;
1884 gtk_text_layout_default_style_changed (text_view->layout);
1887 g_object_notify (G_OBJECT (text_view), "editable");
1892 * gtk_text_view_get_editable:
1893 * @text_view: a #GtkTextView
1895 * Returns the default editability of the #GtkTextView. Tags in the
1896 * buffer may override this setting for some ranges of text.
1898 * Return value: whether text is editable by default
1901 gtk_text_view_get_editable (GtkTextView *text_view)
1903 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1905 return text_view->editable;
1909 * gtk_text_view_set_pixels_above_lines:
1910 * @text_view: a #GtkTextView
1911 * @pixels_above_lines: pixels above paragraphs
1913 * Sets the default number of blank pixels above paragraphs in @text_view.
1914 * Tags in the buffer for @text_view may override the defaults.
1918 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1919 gint pixels_above_lines)
1921 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1923 if (text_view->pixels_above_lines != pixels_above_lines)
1925 text_view->pixels_above_lines = pixels_above_lines;
1927 if (text_view->layout)
1929 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1930 gtk_text_layout_default_style_changed (text_view->layout);
1933 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1938 * gtk_text_view_get_pixels_above_lines:
1939 * @text_view: a #GtkTextView
1941 * Gets the default number of pixels to put above paragraphs.
1943 * Return value: default number of pixels above paragraphs
1946 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1948 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1950 return text_view->pixels_above_lines;
1954 * gtk_text_view_set_pixels_below_lines:
1955 * @text_view: a #GtkTextView
1956 * @pixels_below_lines: pixels below paragraphs
1958 * Sets the default number of pixels of blank space
1959 * to put below paragraphs in @text_view. May be overridden
1960 * by tags applied to @text_view's buffer.
1964 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1965 gint pixels_below_lines)
1967 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1969 if (text_view->pixels_below_lines != pixels_below_lines)
1971 text_view->pixels_below_lines = pixels_below_lines;
1973 if (text_view->layout)
1975 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
1976 gtk_text_layout_default_style_changed (text_view->layout);
1979 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
1984 * gtk_text_view_get_pixels_below_lines:
1985 * @text_view: a #GtkTextView
1987 * Gets the value set by gtk_text_view_set_pixels_below_lines().
1989 * Return value: default number of blank pixels below paragraphs
1992 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
1994 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1996 return text_view->pixels_below_lines;
2000 * gtk_text_view_set_pixels_inside_wrap:
2001 * @text_view: a #GtkTextView
2002 * @pixels_inside_wrap: default number of pixels between wrapped lines
2004 * Sets the default number of pixels of blank space to leave between
2005 * display/wrapped lines within a paragraph. May be overridden by
2006 * tags in @text_view's buffer.
2010 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2011 gint pixels_inside_wrap)
2013 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2015 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2017 text_view->pixels_inside_wrap = pixels_inside_wrap;
2019 if (text_view->layout)
2021 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2022 gtk_text_layout_default_style_changed (text_view->layout);
2025 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2030 * gtk_text_view_get_pixels_inside_wrap:
2031 * @text_view: a #GtkTextView
2033 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2035 * Return value: default number of pixels of blank space between wrapped lines
2038 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2040 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2042 return text_view->pixels_inside_wrap;
2046 * gtk_text_view_set_justification:
2047 * @text_view: a #GtkTextView
2048 * @justification: justification
2050 * Sets the default justification of text in @text_view.
2051 * Tags in the view's buffer may override the default.
2055 gtk_text_view_set_justification (GtkTextView *text_view,
2056 GtkJustification justification)
2058 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2060 if (text_view->justify != justification)
2062 text_view->justify = justification;
2064 if (text_view->layout)
2066 text_view->layout->default_style->justification = justification;
2067 gtk_text_layout_default_style_changed (text_view->layout);
2070 g_object_notify (G_OBJECT (text_view), "justification");
2075 * gtk_text_view_get_justification:
2076 * @text_view: a #GtkTextView
2078 * Gets the default justification of paragraphs in @text_view.
2079 * Tags in the buffer may override the default.
2081 * Return value: default justification
2084 gtk_text_view_get_justification (GtkTextView *text_view)
2086 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2088 return text_view->justify;
2092 * gtk_text_view_set_left_margin:
2093 * @text_view: a #GtkTextView
2094 * @left_margin: left margin in pixels
2096 * Sets the default left margin for text in @text_view.
2097 * Tags in the buffer may override the default.
2101 gtk_text_view_set_left_margin (GtkTextView *text_view,
2104 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2106 if (text_view->left_margin != left_margin)
2108 text_view->left_margin = left_margin;
2110 if (text_view->layout)
2112 text_view->layout->default_style->left_margin = left_margin;
2113 gtk_text_layout_default_style_changed (text_view->layout);
2116 g_object_notify (G_OBJECT (text_view), "left_margin");
2121 * gtk_text_view_get_left_margin:
2122 * @text_view: a #GtkTextView
2124 * Gets the default left margin size of paragraphs in the @text_view.
2125 * Tags in the buffer may override the default.
2127 * Return value: left margin in pixels
2130 gtk_text_view_get_left_margin (GtkTextView *text_view)
2132 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2134 return text_view->left_margin;
2138 * gtk_text_view_set_right_margin:
2139 * @text_view: a #GtkTextView
2140 * @right_margin: right margin in pixels
2142 * Sets the default right margin for text in the text view.
2143 * Tags in the buffer may override the default.
2147 gtk_text_view_set_right_margin (GtkTextView *text_view,
2150 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2152 if (text_view->right_margin != right_margin)
2154 text_view->right_margin = right_margin;
2156 if (text_view->layout)
2158 text_view->layout->default_style->right_margin = right_margin;
2159 gtk_text_layout_default_style_changed (text_view->layout);
2162 g_object_notify (G_OBJECT (text_view), "right_margin");
2167 * gtk_text_view_get_right_margin:
2168 * @text_view: a #GtkTextView
2170 * Gets the default right margin for text in @text_view. Tags
2171 * in the buffer may override the default.
2173 * Return value: right margin in pixels
2176 gtk_text_view_get_right_margin (GtkTextView *text_view)
2178 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2180 return text_view->right_margin;
2184 * gtk_text_view_set_indent:
2185 * @text_view: a #GtkTextView
2186 * @indent: indentation in pixels
2188 * Sets the default indentation for paragraphs in @text_view.
2189 * Tags in the buffer may override the default.
2193 gtk_text_view_set_indent (GtkTextView *text_view,
2196 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2198 if (text_view->indent != indent)
2200 text_view->indent = indent;
2202 if (text_view->layout)
2204 text_view->layout->default_style->indent = indent;
2205 gtk_text_layout_default_style_changed (text_view->layout);
2208 g_object_notify (G_OBJECT (text_view), "indent");
2213 * gtk_text_view_get_indent:
2214 * @text_view: a #GtkTextView
2216 * Gets the default indentation of paragraphs in @text_view.
2217 * Tags in the view's buffer may override the default.
2218 * The indentation may be negative.
2220 * Return value: number of pixels of indentation
2223 gtk_text_view_get_indent (GtkTextView *text_view)
2225 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2227 return text_view->indent;
2231 * gtk_text_view_set_tabs:
2232 * @text_view: a #GtkTextView
2233 * @tabs: tabs as a #PangoTabArray
2235 * Sets the default tab stops for paragraphs in @text_view.
2236 * Tags in the buffer may override the default.
2240 gtk_text_view_set_tabs (GtkTextView *text_view,
2241 PangoTabArray *tabs)
2243 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2245 if (text_view->tabs)
2246 pango_tab_array_free (text_view->tabs);
2248 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2250 if (text_view->layout)
2252 /* some unkosher futzing in internal struct details... */
2253 if (text_view->layout->default_style->tabs)
2254 pango_tab_array_free (text_view->layout->default_style->tabs);
2256 text_view->layout->default_style->tabs =
2257 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2259 gtk_text_layout_default_style_changed (text_view->layout);
2262 g_object_notify (G_OBJECT (text_view), "tabs");
2266 * gtk_text_view_get_tabs:
2267 * @text_view: a #GtkTextView
2269 * Gets the default tabs for @text_view. Tags in the buffer may
2270 * override the defaults. The returned array will be %NULL if
2271 * "standard" (8-space) tabs are used. Free the return value
2272 * with pango_tab_array_free().
2274 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2277 gtk_text_view_get_tabs (GtkTextView *text_view)
2279 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2281 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2285 * gtk_text_view_set_cursor_visible:
2286 * @text_view: a #GtkTextView
2287 * @setting: whether to show the insertion cursor
2289 * Toggles whether the insertion point is displayed. A buffer with no editable
2290 * text probably shouldn't have a visible cursor, so you may want to turn
2294 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2297 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2299 setting = (setting != FALSE);
2301 if (text_view->cursor_visible != setting)
2303 text_view->cursor_visible = setting;
2305 if (GTK_WIDGET_HAS_FOCUS (text_view))
2307 if (text_view->layout)
2309 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2310 gtk_text_view_check_cursor_blink (text_view);
2314 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2319 * gtk_text_view_get_cursor_visible:
2320 * @text_view: a #GtkTextView
2322 * Find out whether the cursor is being displayed.
2324 * Return value: whether the insertion mark is visible
2327 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2329 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2331 return text_view->cursor_visible;
2336 * gtk_text_view_place_cursor_onscreen:
2337 * @text_view: a #GtkTextView
2339 * Moves the cursor to the currently visible region of the
2340 * buffer, it it isn't there already.
2342 * Return value: TRUE if the cursor had to be moved.
2345 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2349 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2351 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2352 gtk_text_buffer_get_mark (get_buffer (text_view),
2355 if (clamp_iter_onscreen (text_view, &insert))
2357 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2365 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2367 if (text_view->first_validate_idle != 0)
2369 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2370 g_source_remove (text_view->first_validate_idle);
2371 text_view->first_validate_idle = 0;
2374 if (text_view->incremental_validate_idle != 0)
2376 g_source_remove (text_view->incremental_validate_idle);
2377 text_view->incremental_validate_idle = 0;
2382 gtk_text_view_destroy (GtkObject *object)
2384 GtkTextView *text_view;
2385 GtkTextLayout *layout;
2387 text_view = GTK_TEXT_VIEW (object);
2389 layout = text_view->layout;
2391 gtk_text_view_remove_validate_idles (text_view);
2392 gtk_text_view_set_buffer (text_view, NULL);
2393 gtk_text_view_destroy_layout (text_view);
2395 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2399 gtk_text_view_finalize (GObject *object)
2401 GtkTextView *text_view;
2403 text_view = GTK_TEXT_VIEW (object);
2405 g_return_if_fail (text_view->buffer == NULL);
2407 gtk_text_view_destroy_layout (text_view);
2408 gtk_text_view_set_buffer (text_view, NULL);
2410 if (text_view->pending_scroll)
2412 free_pending_scroll (text_view->pending_scroll);
2413 text_view->pending_scroll = NULL;
2416 if (text_view->hadjustment)
2417 g_object_unref (G_OBJECT (text_view->hadjustment));
2418 if (text_view->vadjustment)
2419 g_object_unref (G_OBJECT (text_view->vadjustment));
2421 text_window_free (text_view->text_window);
2423 if (text_view->left_window)
2424 text_window_free (text_view->left_window);
2426 if (text_view->top_window)
2427 text_window_free (text_view->top_window);
2429 if (text_view->right_window)
2430 text_window_free (text_view->right_window);
2432 if (text_view->bottom_window)
2433 text_window_free (text_view->bottom_window);
2435 g_object_unref (G_OBJECT (text_view->im_context));
2437 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2441 gtk_text_view_set_property (GObject *object,
2443 const GValue *value,
2446 GtkTextView *text_view;
2448 text_view = GTK_TEXT_VIEW (object);
2452 case PROP_PIXELS_ABOVE_LINES:
2453 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2456 case PROP_PIXELS_BELOW_LINES:
2457 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2460 case PROP_PIXELS_INSIDE_WRAP:
2461 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2465 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2468 case PROP_WRAP_MODE:
2469 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2472 case PROP_JUSTIFICATION:
2473 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2476 case PROP_LEFT_MARGIN:
2477 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2480 case PROP_RIGHT_MARGIN:
2481 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2485 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2489 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2492 case PROP_CURSOR_VISIBLE:
2493 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2497 g_assert_not_reached ();
2503 gtk_text_view_get_property (GObject *object,
2508 GtkTextView *text_view;
2510 text_view = GTK_TEXT_VIEW (object);
2514 case PROP_PIXELS_ABOVE_LINES:
2515 g_value_set_int (value, text_view->pixels_above_lines);
2518 case PROP_PIXELS_BELOW_LINES:
2519 g_value_set_int (value, text_view->pixels_below_lines);
2522 case PROP_PIXELS_INSIDE_WRAP:
2523 g_value_set_int (value, text_view->pixels_inside_wrap);
2527 g_value_set_boolean (value, text_view->editable);
2530 case PROP_WRAP_MODE:
2531 g_value_set_enum (value, text_view->wrap_mode);
2534 case PROP_JUSTIFICATION:
2535 g_value_set_enum (value, text_view->justify);
2538 case PROP_LEFT_MARGIN:
2539 g_value_set_int (value, text_view->left_margin);
2542 case PROP_RIGHT_MARGIN:
2543 g_value_set_int (value, text_view->right_margin);
2547 g_value_set_int (value, text_view->indent);
2551 g_value_set_boxed (value, text_view->tabs);
2554 case PROP_CURSOR_VISIBLE:
2555 g_value_set_boolean (value, text_view->cursor_visible);
2559 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2565 gtk_text_view_size_request (GtkWidget *widget,
2566 GtkRequisition *requisition)
2568 GtkTextView *text_view;
2570 gint focus_edge_width;
2572 gboolean interior_focus;
2574 text_view = GTK_TEXT_VIEW (widget);
2576 gtk_widget_style_get (widget,
2577 "interior_focus", &interior_focus,
2578 "focus_line_width", &focus_width,
2582 focus_edge_width = 0;
2584 focus_edge_width = focus_width;
2586 if (text_view->layout)
2588 text_view->text_window->requisition.width = text_view->layout->width;
2589 text_view->text_window->requisition.height = text_view->layout->height;
2593 text_view->text_window->requisition.width = 0;
2594 text_view->text_window->requisition.height = 0;
2597 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2598 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2600 if (text_view->left_window)
2601 requisition->width += text_view->left_window->requisition.width;
2603 if (text_view->right_window)
2604 requisition->width += text_view->right_window->requisition.width;
2606 if (text_view->top_window)
2607 requisition->height += text_view->top_window->requisition.height;
2609 if (text_view->bottom_window)
2610 requisition->height += text_view->bottom_window->requisition.height;
2612 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2613 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2615 tmp_list = text_view->children;
2616 while (tmp_list != NULL)
2618 GtkTextViewChild *child = tmp_list->data;
2622 GtkRequisition child_req;
2623 GtkRequisition old_req;
2625 gtk_widget_get_child_requisition (child->widget, &old_req);
2627 gtk_widget_size_request (child->widget, &child_req);
2629 gtk_widget_get_child_requisition (child->widget, &child_req);
2631 /* Invalidate layout lines if required */
2632 if (text_view->layout &&
2633 (old_req.width != child_req.width ||
2634 old_req.height != child_req.height))
2635 gtk_text_child_anchor_queue_resize (child->anchor,
2640 GtkRequisition child_req;
2642 gtk_widget_size_request (child->widget, &child_req);
2645 tmp_list = g_slist_next (tmp_list);
2650 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2651 GtkTextViewChild *vc,
2652 GtkAllocation *allocation)
2658 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2662 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2665 buffer_y += vc->from_top_of_line;
2667 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2668 allocation->y = buffer_y - text_view->yoffset;
2670 gtk_widget_get_child_requisition (vc->widget, &req);
2671 allocation->width = req.width;
2672 allocation->height = req.height;
2676 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2677 GtkTextViewChild *vc)
2679 GtkAllocation allocation;
2681 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2683 gtk_widget_size_allocate (vc->widget, &allocation);
2686 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2688 vc->widget->allocation.x,
2689 vc->widget->allocation.y,
2690 text_view->yoffset);
2695 gtk_text_view_child_allocated (GtkTextLayout *layout,
2701 GtkTextViewChild *vc = NULL;
2702 GtkTextView *text_view = data;
2704 /* x,y is the position of the child from the top of the line, and
2705 * from the left of the buffer. We have to translate that into text
2706 * window coordinates, then size_allocate the child.
2709 vc = g_object_get_data (G_OBJECT (child),
2710 "gtk-text-view-child");
2712 g_assert (vc != NULL);
2714 DV (g_print ("child allocated at %d,%d\n", x, y));
2716 vc->from_left_of_buffer = x;
2717 vc->from_top_of_line = y;
2719 gtk_text_view_update_child_allocation (text_view, vc);
2723 gtk_text_view_allocate_children (GtkTextView *text_view)
2727 DV(g_print(G_STRLOC"\n"));
2729 tmp_list = text_view->children;
2730 while (tmp_list != NULL)
2732 GtkTextViewChild *child = tmp_list->data;
2736 /* We need to force-validate the regions containing
2739 GtkTextIter child_loc;
2740 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2744 gtk_text_layout_validate_yrange (text_view->layout,
2750 GtkAllocation allocation;
2751 GtkRequisition child_req;
2753 g_assert (child != NULL);
2755 allocation.x = child->x;
2756 allocation.y = child->y;
2758 gtk_widget_get_child_requisition (child->widget, &child_req);
2760 allocation.width = child_req.width;
2761 allocation.height = child_req.height;
2763 gtk_widget_size_allocate (child->widget, &allocation);
2766 tmp_list = g_slist_next (tmp_list);
2771 gtk_text_view_size_allocate (GtkWidget *widget,
2772 GtkAllocation *allocation)
2774 GtkTextView *text_view;
2775 GtkTextIter first_para;
2777 GtkAdjustment *vadj;
2778 gboolean yoffset_changed = FALSE;
2780 GdkRectangle text_rect;
2781 GdkRectangle left_rect;
2782 GdkRectangle right_rect;
2783 GdkRectangle top_rect;
2784 GdkRectangle bottom_rect;
2785 gint focus_edge_width;
2787 gboolean interior_focus;
2788 gboolean size_changed;
2790 text_view = GTK_TEXT_VIEW (widget);
2792 DV(g_print(G_STRLOC"\n"));
2795 widget->allocation.width != allocation->width ||
2796 widget->allocation.height != allocation->height;
2798 widget->allocation = *allocation;
2800 if (GTK_WIDGET_REALIZED (widget))
2802 gdk_window_move_resize (widget->window,
2803 allocation->x, allocation->y,
2804 allocation->width, allocation->height);
2807 /* distribute width/height among child windows. Ensure all
2808 * windows get at least a 1x1 allocation.
2811 gtk_widget_style_get (widget,
2812 "interior_focus", &interior_focus,
2813 "focus_line_width", &focus_width,
2817 focus_edge_width = 0;
2819 focus_edge_width = focus_width;
2821 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2823 if (text_view->left_window)
2824 left_rect.width = text_view->left_window->requisition.width;
2826 left_rect.width = 0;
2828 width -= left_rect.width;
2830 if (text_view->right_window)
2831 right_rect.width = text_view->right_window->requisition.width;
2833 right_rect.width = 0;
2835 width -= right_rect.width;
2837 text_rect.width = MAX (1, width);
2839 top_rect.width = text_rect.width;
2840 bottom_rect.width = text_rect.width;
2843 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2845 if (text_view->top_window)
2846 top_rect.height = text_view->top_window->requisition.height;
2848 top_rect.height = 0;
2850 height -= top_rect.height;
2852 if (text_view->bottom_window)
2853 bottom_rect.height = text_view->bottom_window->requisition.height;
2855 bottom_rect.height = 0;
2857 height -= bottom_rect.height;
2859 text_rect.height = MAX (1, height);
2861 left_rect.height = text_rect.height;
2862 right_rect.height = text_rect.height;
2865 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2866 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2868 text_rect.x = left_rect.x + left_rect.width;
2869 text_rect.y = top_rect.y + top_rect.height;
2871 left_rect.y = text_rect.y;
2872 right_rect.y = text_rect.y;
2874 top_rect.x = text_rect.x;
2875 bottom_rect.x = text_rect.x;
2877 right_rect.x = text_rect.x + text_rect.width;
2878 bottom_rect.y = text_rect.y + text_rect.height;
2880 text_window_size_allocate (text_view->text_window,
2883 if (text_view->left_window)
2884 text_window_size_allocate (text_view->left_window,
2887 if (text_view->right_window)
2888 text_window_size_allocate (text_view->right_window,
2891 if (text_view->top_window)
2892 text_window_size_allocate (text_view->top_window,
2895 if (text_view->bottom_window)
2896 text_window_size_allocate (text_view->bottom_window,
2899 gtk_text_view_update_layout_width (text_view);
2901 /* Note that this will do some layout validation */
2902 gtk_text_view_allocate_children (text_view);
2904 /* Now adjust the value of the adjustment to keep the cursor at the
2905 * same place in the buffer
2907 gtk_text_view_get_first_para_iter (text_view, &first_para);
2908 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2910 y += text_view->first_para_pixels;
2912 /* Ensure h/v adj exist */
2913 get_hadjustment (text_view);
2914 get_vadjustment (text_view);
2916 vadj = text_view->vadjustment;
2917 if (y > vadj->upper - vadj->page_size)
2918 y = MAX (0, vadj->upper - vadj->page_size);
2920 if (y != text_view->yoffset)
2922 vadj->value = text_view->yoffset = y;
2923 yoffset_changed = TRUE;
2926 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2927 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
2928 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
2929 text_view->hadjustment->lower = 0;
2930 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2932 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2934 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2935 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2936 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2937 text_view->vadjustment->lower = 0;
2938 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2940 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2942 if (yoffset_changed)
2943 gtk_adjustment_value_changed (vadj);
2945 /* The GTK resize loop processes all the pending exposes right
2946 * after doing the resize stuff, so the idle sizer won't have a
2947 * chance to run. So we do the work here.
2949 gtk_text_view_flush_first_validate (text_view);
2951 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2954 if (size_changed && GTK_WIDGET_REALIZED (widget))
2955 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2959 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2962 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2963 text_view->first_para_mark);
2967 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2969 GtkWidget *widget = GTK_WIDGET (text_view);
2971 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
2973 if (SCREEN_HEIGHT (widget) > 0)
2975 GtkTextIter first_para;
2977 /* Be sure we've validated the stuff onscreen; if we
2978 * scrolled, these calls won't have any effect, because
2979 * they were called in the recursive validate_onscreen
2981 gtk_text_view_get_first_para_iter (text_view, &first_para);
2983 gtk_text_layout_validate_yrange (text_view->layout,
2986 text_view->first_para_pixels +
2987 SCREEN_HEIGHT (widget));
2990 text_view->onscreen_validated = TRUE;
2992 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
2994 /* This can have the odd side effect of triggering a scroll, which should
2995 * flip "onscreen_validated" back to FALSE, but should also get us
2996 * back into this function to turn it on again.
2998 gtk_text_view_update_adjustments (text_view);
3000 g_assert (text_view->onscreen_validated);
3004 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3006 if (text_view->first_validate_idle == 0)
3009 /* Do this first, which means that if an "invalidate"
3010 * occurs during any of this process, a new first_validate_callback
3011 * will be installed, and we'll start again.
3013 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3014 g_source_remove (text_view->first_validate_idle);
3015 text_view->first_validate_idle = 0;
3017 /* be sure we have up-to-date screen size set on the
3020 gtk_text_view_update_layout_width (text_view);
3022 /* Bail out if we invalidated stuff; scrolling right away will just
3023 * confuse the issue.
3025 if (text_view->first_validate_idle != 0)
3027 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3031 /* scroll to any marks, if that's pending. This can
3032 * jump us to the validation codepath used for scrolling
3033 * onscreen, if so we bail out.
3035 if (!gtk_text_view_flush_scroll (text_view))
3036 gtk_text_view_validate_onscreen (text_view);
3038 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3040 g_assert (text_view->onscreen_validated);
3045 first_validate_callback (gpointer data)
3047 GtkTextView *text_view = data;
3049 GDK_THREADS_ENTER ();
3051 /* Note that some of this code is duplicated at the end of size_allocate,
3052 * keep in sync with that.
3055 DV(g_print(G_STRLOC"\n"));
3057 gtk_text_view_flush_first_validate (text_view);
3059 GDK_THREADS_LEAVE ();
3065 incremental_validate_callback (gpointer data)
3067 GtkTextView *text_view = data;
3068 gboolean result = TRUE;
3070 GDK_THREADS_ENTER ();
3072 DV(g_print(G_STRLOC"\n"));
3074 gtk_text_layout_validate (text_view->layout, 2000);
3076 gtk_text_view_update_adjustments (text_view);
3078 if (gtk_text_layout_is_valid (text_view->layout))
3080 text_view->incremental_validate_idle = 0;
3084 GDK_THREADS_LEAVE ();
3090 gtk_text_view_invalidate (GtkTextView *text_view)
3092 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3093 text_view->onscreen_validated));
3095 text_view->onscreen_validated = FALSE;
3097 if (!text_view->first_validate_idle)
3099 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3100 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3101 text_view->first_validate_idle));
3104 if (!text_view->incremental_validate_idle)
3106 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3107 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3108 text_view->incremental_validate_idle));
3113 invalidated_handler (GtkTextLayout *layout,
3116 GtkTextView *text_view;
3118 text_view = GTK_TEXT_VIEW (data);
3120 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3121 gtk_text_view_invalidate (text_view);
3125 changed_handler (GtkTextLayout *layout,
3131 GtkTextView *text_view;
3133 GdkRectangle visible_rect;
3134 GdkRectangle redraw_rect;
3136 text_view = GTK_TEXT_VIEW (data);
3137 widget = GTK_WIDGET (data);
3139 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3141 if (GTK_WIDGET_REALIZED (text_view))
3143 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3145 redraw_rect.x = visible_rect.x;
3146 redraw_rect.width = visible_rect.width;
3147 redraw_rect.y = start_y;
3149 if (old_height == new_height)
3150 redraw_rect.height = old_height;
3152 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3154 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3156 /* text_window_invalidate_rect() takes buffer coordinates */
3157 text_window_invalidate_rect (text_view->text_window,
3160 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3164 redraw_rect.height));
3166 if (text_view->left_window)
3167 text_window_invalidate_rect (text_view->left_window,
3169 if (text_view->right_window)
3170 text_window_invalidate_rect (text_view->right_window,
3172 if (text_view->top_window)
3173 text_window_invalidate_rect (text_view->top_window,
3175 if (text_view->bottom_window)
3176 text_window_invalidate_rect (text_view->bottom_window,
3179 gtk_text_view_update_im_spot_location (text_view);
3183 if (old_height != new_height)
3185 gboolean yoffset_changed = FALSE;
3187 int new_first_para_top;
3188 int old_first_para_top;
3191 /* If the bottom of the old area was above the top of the
3192 * screen, we need to scroll to keep the current top of the
3193 * screen in place. Remember that first_para_pixels is the
3194 * position of the top of the screen in coordinates relative to
3195 * the first paragraph onscreen.
3197 * In short we are adding the height delta of the portion of the
3198 * changed region above first_para_mark to text_view->yoffset.
3200 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3201 text_view->first_para_mark);
3203 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3205 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3207 if (new_first_para_top != old_first_para_top)
3209 text_view->yoffset += new_first_para_top - old_first_para_top;
3211 get_vadjustment (text_view)->value = text_view->yoffset;
3212 yoffset_changed = TRUE;
3215 if (yoffset_changed)
3217 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3218 gtk_adjustment_value_changed (get_vadjustment (text_view));
3221 /* FIXME be smarter about which anchored widgets we update */
3223 tmp_list = text_view->children;
3224 while (tmp_list != NULL)
3226 GtkTextViewChild *child = tmp_list->data;
3229 gtk_text_view_update_child_allocation (text_view, child);
3231 tmp_list = g_slist_next (tmp_list);
3236 GtkRequisition old_req;
3237 GtkRequisition new_req;
3239 old_req = widget->requisition;
3241 /* Use this instead of gtk_widget_size_request wrapper
3242 * to avoid the optimization which just returns widget->requisition
3243 * if a resize hasn't been queued.
3245 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3247 if (old_req.width != new_req.width ||
3248 old_req.height != new_req.height)
3249 gtk_widget_queue_resize (widget);
3254 gtk_text_view_realize_cursor_gc (GtkTextView *text_view)
3256 GdkColor *cursor_color;
3257 GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
3259 if (text_view->cursor_gc)
3260 gdk_gc_unref (text_view->cursor_gc);
3262 gtk_widget_style_get (GTK_WIDGET (text_view), "cursor-color", &cursor_color, NULL);
3265 cursor_color = &red;
3267 text_view->cursor_gc = gdk_gc_new (text_view->text_window->bin_window);
3268 gdk_gc_set_rgb_fg_color (text_view->cursor_gc, cursor_color);
3272 gtk_text_view_realize (GtkWidget *widget)
3274 GtkTextView *text_view;
3275 GdkWindowAttr attributes;
3276 gint attributes_mask;
3279 text_view = GTK_TEXT_VIEW (widget);
3280 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3282 attributes.window_type = GDK_WINDOW_CHILD;
3283 attributes.x = widget->allocation.x;
3284 attributes.y = widget->allocation.y;
3285 attributes.width = widget->allocation.width;
3286 attributes.height = widget->allocation.height;
3287 attributes.wclass = GDK_INPUT_OUTPUT;
3288 attributes.visual = gtk_widget_get_visual (widget);
3289 attributes.colormap = gtk_widget_get_colormap (widget);
3290 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3292 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3294 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3295 &attributes, attributes_mask);
3296 gdk_window_set_user_data (widget->window, widget);
3298 /* must come before text_window_realize calls */
3299 widget->style = gtk_style_attach (widget->style, widget->window);
3301 gdk_window_set_background (widget->window,
3302 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3304 text_window_realize (text_view->text_window, widget->window);
3306 if (text_view->left_window)
3307 text_window_realize (text_view->left_window,
3310 if (text_view->top_window)
3311 text_window_realize (text_view->top_window,
3314 if (text_view->right_window)
3315 text_window_realize (text_view->right_window,
3318 if (text_view->bottom_window)
3319 text_window_realize (text_view->bottom_window,
3322 gtk_text_view_realize_cursor_gc (text_view);
3324 gtk_text_view_ensure_layout (text_view);
3326 if (text_view->buffer)
3327 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
3328 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3330 tmp_list = text_view->children;
3331 while (tmp_list != NULL)
3333 GtkTextViewChild *vc = tmp_list->data;
3335 text_view_child_set_parent_window (text_view, vc);
3337 tmp_list = tmp_list->next;
3342 gtk_text_view_unrealize (GtkWidget *widget)
3344 GtkTextView *text_view;
3346 text_view = GTK_TEXT_VIEW (widget);
3348 if (text_view->buffer)
3349 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
3350 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3352 if (text_view->cursor_gc)
3354 gdk_gc_unref (text_view->cursor_gc);
3355 text_view->cursor_gc = NULL;
3358 gtk_text_view_remove_validate_idles (text_view);
3360 if (text_view->popup_menu)
3362 gtk_widget_destroy (text_view->popup_menu);
3363 text_view->popup_menu = NULL;
3366 text_window_unrealize (text_view->text_window);
3368 if (text_view->left_window)
3369 text_window_unrealize (text_view->left_window);
3371 if (text_view->top_window)
3372 text_window_unrealize (text_view->top_window);
3374 if (text_view->right_window)
3375 text_window_unrealize (text_view->right_window);
3377 if (text_view->bottom_window)
3378 text_window_unrealize (text_view->bottom_window);
3380 gtk_text_view_destroy_layout (text_view);
3382 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3386 gtk_text_view_style_set (GtkWidget *widget,
3387 GtkStyle *previous_style)
3389 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3391 if (GTK_WIDGET_REALIZED (widget))
3393 gdk_window_set_background (widget->window,
3394 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3396 gdk_window_set_background (text_view->text_window->bin_window,
3397 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3399 if (text_view->left_window)
3400 gdk_window_set_background (text_view->left_window->bin_window,
3401 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3402 if (text_view->right_window)
3403 gdk_window_set_background (text_view->right_window->bin_window,
3404 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3406 if (text_view->top_window)
3407 gdk_window_set_background (text_view->top_window->bin_window,
3408 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3410 if (text_view->bottom_window)
3411 gdk_window_set_background (text_view->bottom_window->bin_window,
3412 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3414 gtk_text_view_realize_cursor_gc (text_view);
3417 if (text_view->layout && previous_style)
3419 gtk_text_view_set_attributes_from_style (text_view,
3420 text_view->layout->default_style,
3422 gtk_text_layout_default_style_changed (text_view->layout);
3427 gtk_text_view_direction_changed (GtkWidget *widget,
3428 GtkTextDirection previous_direction)
3430 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3432 if (text_view->layout)
3434 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3435 gtk_text_layout_default_style_changed (text_view->layout);
3441 set_invisible_cursor (GdkWindow *window)
3443 GdkBitmap *empty_bitmap;
3446 char invisible_cursor_bits[] = { 0x0 };
3448 useless.red = useless.green = useless.blue = 0;
3451 empty_bitmap = gdk_bitmap_create_from_data (window,
3452 invisible_cursor_bits,
3455 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3460 gdk_window_set_cursor (window, cursor);
3462 gdk_cursor_unref (cursor);
3464 g_object_unref (empty_bitmap);
3468 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3470 if (text_view->mouse_cursor_obscured)
3473 set_invisible_cursor (text_view->text_window->bin_window);
3475 text_view->mouse_cursor_obscured = TRUE;
3483 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3486 switch (event->type)
3488 case GDK_MOTION_NOTIFY:
3489 *x = event->motion.x;
3490 *y = event->motion.y;
3494 case GDK_BUTTON_PRESS:
3495 case GDK_2BUTTON_PRESS:
3496 case GDK_3BUTTON_PRESS:
3497 case GDK_BUTTON_RELEASE:
3498 *x = event->button.x;
3499 *y = event->button.y;
3504 case GDK_KEY_RELEASE:
3505 case GDK_ENTER_NOTIFY:
3506 case GDK_LEAVE_NOTIFY:
3507 case GDK_PROPERTY_NOTIFY:
3508 case GDK_SELECTION_CLEAR:
3509 case GDK_SELECTION_REQUEST:
3510 case GDK_SELECTION_NOTIFY:
3511 case GDK_PROXIMITY_IN:
3512 case GDK_PROXIMITY_OUT:
3513 case GDK_DRAG_ENTER:
3514 case GDK_DRAG_LEAVE:
3515 case GDK_DRAG_MOTION:
3516 case GDK_DRAG_STATUS:
3517 case GDK_DROP_START:
3518 case GDK_DROP_FINISHED:
3528 emit_event_on_tags (GtkWidget *widget,
3534 gboolean retval = FALSE;
3535 GtkTextView *text_view;
3537 text_view = GTK_TEXT_VIEW (widget);
3539 tags = gtk_text_iter_get_tags (iter);
3544 GtkTextTag *tag = tmp->data;
3546 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3552 tmp = g_slist_next (tmp);
3555 g_slist_free (tags);
3561 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3563 GtkTextView *text_view;
3566 text_view = GTK_TEXT_VIEW (widget);
3568 if (text_view->layout == NULL ||
3569 get_buffer (text_view) == NULL)
3572 if (event->any.window != text_view->text_window->bin_window)
3575 if (get_event_coordinates (event, &x, &y))
3579 x += text_view->xoffset;
3580 y += text_view->yoffset;
3582 /* FIXME this is slow and we do it twice per event.
3583 * My favorite solution is to have GtkTextLayout cache
3584 * the last couple lookups.
3586 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3590 return emit_event_on_tags (widget, event, &iter);
3592 else if (event->type == GDK_KEY_PRESS ||
3593 event->type == GDK_KEY_RELEASE)
3595 GtkTextMark *insert;
3598 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3601 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3603 return emit_event_on_tags (widget, event, &iter);
3610 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3612 gboolean retval = FALSE;
3613 gboolean obscure = FALSE;
3614 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3615 GtkTextMark *insert;
3618 if (text_view->layout == NULL ||
3619 get_buffer (text_view) == NULL)
3622 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3623 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3624 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3625 gtk_im_context_filter_keypress (text_view->im_context, event))
3627 text_view->need_im_reset = TRUE;
3631 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3632 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3634 else if (event->keyval == GDK_Return ||
3635 event->keyval == GDK_KP_Enter)
3637 gtk_text_view_commit_text (text_view, "\n");
3642 /* Pass through Tab as literal tab, unless Control is held down */
3643 else if ((event->keyval == GDK_Tab ||
3644 event->keyval == GDK_KP_Tab ||
3645 event->keyval == GDK_ISO_Left_Tab) &&
3646 !(event->state & GDK_CONTROL_MASK))
3648 /* If the text isn't editable, move the focus instead */
3649 if (text_view->editable)
3651 gtk_text_view_commit_text (text_view, "\t");
3655 gtk_text_view_move_focus (text_view,
3656 (event->state & GDK_SHIFT_MASK) ?
3657 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3665 gtk_text_view_obscure_mouse_cursor (text_view);
3667 gtk_text_view_pend_cursor_blink (text_view);
3673 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3675 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3676 GtkTextMark *insert;
3679 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3682 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3683 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3684 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3685 gtk_im_context_filter_keypress (text_view->im_context, event))
3687 text_view->need_im_reset = TRUE;
3691 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3695 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3697 GtkTextView *text_view;
3699 text_view = GTK_TEXT_VIEW (widget);
3701 text_view->disable_scroll_on_focus = TRUE;
3702 gtk_widget_grab_focus (widget);
3703 text_view->disable_scroll_on_focus = FALSE;
3705 if (event->window != text_view->text_window->bin_window)
3707 /* Remove selection if any. */
3708 gtk_text_view_unselect (text_view);
3714 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3715 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3716 else if (event->button == 3)
3717 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3720 if (event->type == GDK_BUTTON_PRESS)
3722 gtk_text_view_reset_im_context (text_view);
3724 if (event->button == 1)
3726 /* If we're in the selection, start a drag copy/move of the
3727 * selection; otherwise, start creating a new selection.
3730 GtkTextIter start, end;
3732 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3734 event->x + text_view->xoffset,
3735 event->y + text_view->yoffset);
3737 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3739 gtk_text_iter_in_range (&iter, &start, &end))
3741 text_view->drag_start_x = event->x;
3742 text_view->drag_start_y = event->y;
3743 text_view->pending_place_cursor_button = event->button;
3747 gtk_text_view_start_selection_drag (text_view, &iter, event);
3752 else if (event->button == 2)
3756 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3758 event->x + text_view->xoffset,
3759 event->y + text_view->yoffset);
3761 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3762 gtk_clipboard_get (GDK_SELECTION_PRIMARY),
3764 text_view->editable);
3767 else if (event->button == 3)
3769 gtk_text_view_do_popup (text_view, event);
3772 else if ((event->type == GDK_2BUTTON_PRESS ||
3773 event->type == GDK_3BUTTON_PRESS) &&
3776 GtkTextIter start, end;
3778 /* End the selection drag, otherwise we'd clear the new
3779 * word/line selection on button release
3781 gtk_text_view_end_selection_drag (text_view, event);
3783 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3785 event->x + text_view->xoffset,
3786 event->y + text_view->yoffset);
3790 if (event->type == GDK_2BUTTON_PRESS)
3792 if (gtk_text_iter_inside_word (&start))
3794 if (!gtk_text_iter_starts_word (&start))
3795 gtk_text_iter_backward_word_start (&start);
3797 if (!gtk_text_iter_ends_word (&end))
3798 gtk_text_iter_forward_word_end (&end);
3801 else if (event->type == GDK_3BUTTON_PRESS)
3803 if (gtk_text_view_starts_display_line (text_view, &start))
3805 /* If on a display line boundary, we assume the user
3806 * clicked off the end of a line and we therefore select
3807 * the line before the boundary.
3809 gtk_text_view_backward_display_line_start (text_view, &start);
3813 /* start isn't on the start of a line, so we move it to the
3814 * start, and move end to the end unless it's already there.
3816 gtk_text_view_backward_display_line_start (text_view, &start);
3818 if (!gtk_text_view_starts_display_line (text_view, &end))
3819 gtk_text_view_forward_display_line_end (text_view, &end);
3823 if (event->state & GDK_SHIFT_MASK)
3825 /* Take union of old and new selection */
3826 GtkTextIter old_start, old_end;
3828 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3829 &old_start, &old_end);
3831 gtk_text_iter_order (&start, &old_start);
3832 gtk_text_iter_order (&old_end, &end);
3834 /* Now start is the first of the starts, and end is the
3839 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3842 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3846 text_view->just_selected_element = TRUE;
3855 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3857 GtkTextView *text_view;
3859 text_view = GTK_TEXT_VIEW (widget);
3861 if (event->window != text_view->text_window->bin_window)
3864 if (event->button == 1)
3866 if (text_view->drag_start_x >= 0)
3868 text_view->drag_start_x = -1;
3869 text_view->drag_start_y = -1;
3872 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3874 else if (text_view->just_selected_element)
3876 text_view->just_selected_element = FALSE;
3879 else if (text_view->pending_place_cursor_button == event->button)
3883 /* Unselect everything; we clicked inside selection, but
3884 * didn't move by the drag threshold, so just clear selection
3887 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3889 event->x + text_view->xoffset,
3890 event->y + text_view->yoffset);
3892 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3894 text_view->pending_place_cursor_button = 0;
3904 keymap_direction_changed (GdkKeymap *keymap,
3905 GtkTextView *text_view)
3907 gtk_text_view_check_keymap_direction (text_view);
3911 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3913 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3915 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
3916 gtk_widget_queue_draw (widget);
3918 DV(g_print (G_STRLOC": focus_in_event\n"));
3920 if (text_view->cursor_visible && text_view->layout)
3922 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3923 gtk_text_view_check_cursor_blink (text_view);
3926 g_signal_connect (gdk_keymap_get_default (),
3927 "direction_changed",
3928 G_CALLBACK (keymap_direction_changed), text_view);
3929 gtk_text_view_check_keymap_direction (text_view);
3931 text_view->need_im_reset = TRUE;
3932 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3938 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3940 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3942 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
3943 gtk_widget_queue_draw (widget);
3945 DV(g_print (G_STRLOC": focus_out_event\n"));
3947 if (text_view->cursor_visible && text_view->layout)
3949 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3950 gtk_text_view_check_cursor_blink (text_view);
3953 g_signal_handlers_disconnect_by_func (gdk_keymap_get_default (),
3954 keymap_direction_changed,
3957 text_view->need_im_reset = TRUE;
3958 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3964 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3966 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3968 if (text_view->mouse_cursor_obscured)
3972 cursor = gdk_cursor_new (GDK_XTERM);
3973 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3974 gdk_cursor_unref (cursor);
3975 text_view->mouse_cursor_obscured = FALSE;
3978 if (event->window == text_view->text_window->bin_window &&
3979 text_view->drag_start_x >= 0)
3983 gdk_window_get_pointer (text_view->text_window->bin_window,
3986 if (gtk_drag_check_threshold (widget,
3987 text_view->drag_start_x,
3988 text_view->drag_start_y,
3992 gint buffer_x, buffer_y;
3994 gtk_text_view_window_to_buffer_coords (text_view,
3995 GTK_TEXT_WINDOW_TEXT,
3996 text_view->drag_start_x,
3997 text_view->drag_start_y,
4001 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4003 buffer_x, buffer_y);
4005 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4014 gtk_text_view_paint (GtkWidget *widget,
4016 GdkEventExpose *event)
4018 GtkTextView *text_view;
4019 GList *child_exposes;
4023 text_view = GTK_TEXT_VIEW (widget);
4025 g_return_if_fail (text_view->layout != NULL);
4026 g_return_if_fail (text_view->xoffset >= 0);
4027 g_return_if_fail (text_view->yoffset >= 0);
4029 while (text_view->first_validate_idle != 0)
4031 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4032 text_view->first_validate_idle));
4033 gtk_text_view_flush_first_validate (text_view);
4036 /* More regions could have become invalid in the above loop */
4037 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4042 gdk_region_get_clipbox (updates, &rect);
4044 gdk_rectangle_union (area, &rect, area);
4046 gdk_region_destroy (updates);
4049 if (!text_view->onscreen_validated)
4051 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.");
4056 printf ("painting %d,%d %d x %d\n",
4058 area->width, area->height);
4061 child_exposes = NULL;
4062 gtk_text_layout_draw (text_view->layout,
4064 text_view->text_window->bin_window,
4065 text_view->cursor_gc,
4069 area->width, area->height,
4072 tmp_list = child_exposes;
4073 while (tmp_list != NULL)
4075 GtkWidget *child = tmp_list->data;
4077 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4081 g_object_unref (G_OBJECT (child));
4083 tmp_list = tmp_list->next;
4086 g_list_free (child_exposes);
4090 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4094 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4095 GTK_TEXT_WINDOW_TEXT))
4097 DV(g_print (">Exposed ("G_STRLOC")\n"));
4098 gtk_text_view_paint (widget, &event->area, event);
4101 if (event->window == widget->window)
4102 gtk_text_view_draw_focus (widget);
4104 /* Propagate exposes to all children not in the buffer. */
4105 tmp_list = GTK_TEXT_VIEW (widget)->children;
4106 while (tmp_list != NULL)
4108 GtkTextViewChild *vc = tmp_list->data;
4110 /* propagate_expose checks that event->window matches
4113 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4114 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4118 tmp_list = tmp_list->next;
4125 gtk_text_view_draw_focus (GtkWidget *widget)
4127 gboolean interior_focus;
4129 /* We clear the focus if we are in interior focus mode. */
4130 gtk_widget_style_get (widget,
4131 "interior_focus", &interior_focus,
4134 if (GTK_WIDGET_DRAWABLE (widget))
4136 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4138 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4139 NULL, widget, "textview",
4141 widget->allocation.width,
4142 widget->allocation.height);
4146 gdk_window_clear (widget->window);
4152 gtk_text_view_grab_focus (GtkWidget *widget)
4154 GtkTextView *text_view;
4156 text_view = GTK_TEXT_VIEW (widget);
4158 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4160 if (!text_view->disable_scroll_on_focus)
4161 gtk_text_view_scroll_mark_onscreen (text_view,
4162 gtk_text_buffer_get_mark (get_buffer (text_view),
4167 gtk_text_view_focus (GtkWidget *widget,
4168 GtkDirectionType direction)
4170 GtkTextView *text_view;
4171 GtkContainer *container;
4173 text_view = GTK_TEXT_VIEW (widget);
4174 container = GTK_CONTAINER (widget);
4176 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4184 gtk_text_view_add (GtkContainer *container,
4187 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4188 g_return_if_fail (GTK_IS_WIDGET (child));
4190 /* This is pretty random. */
4191 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4193 GTK_TEXT_WINDOW_WIDGET,
4198 gtk_text_view_remove (GtkContainer *container,
4202 GtkTextView *text_view;
4203 GtkTextViewChild *vc;
4205 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4206 g_return_if_fail (GTK_IS_WIDGET (child));
4207 g_return_if_fail (child->parent == (GtkWidget*) container);
4209 text_view = GTK_TEXT_VIEW (container);
4212 iter = text_view->children;
4214 while (iter != NULL)
4218 if (vc->widget == child)
4221 iter = g_slist_next (iter);
4224 g_assert (iter != NULL); /* be sure we had the child in the list */
4226 text_view->children = g_slist_remove (text_view->children, vc);
4228 gtk_widget_unparent (vc->widget);
4230 text_view_child_free (vc);
4234 gtk_text_view_forall (GtkContainer *container,
4235 gboolean include_internals,
4236 GtkCallback callback,
4237 gpointer callback_data)
4240 GtkTextView *text_view;
4243 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4244 g_return_if_fail (callback != NULL);
4246 text_view = GTK_TEXT_VIEW (container);
4248 copy = g_slist_copy (text_view->children);
4251 while (iter != NULL)
4253 GtkTextViewChild *vc = iter->data;
4255 (* callback) (vc->widget, callback_data);
4257 iter = g_slist_next (iter);
4260 g_slist_free (copy);
4263 #define CURSOR_ON_MULTIPLIER 0.66
4264 #define CURSOR_OFF_MULTIPLIER 0.34
4265 #define CURSOR_PEND_MULTIPLIER 1.0
4268 cursor_blinks (GtkTextView *text_view)
4270 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4273 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4276 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4279 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4284 get_cursor_time (GtkTextView *text_view)
4286 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4289 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4299 blink_cb (gpointer data)
4301 GtkTextView *text_view;
4304 GDK_THREADS_ENTER ();
4306 text_view = GTK_TEXT_VIEW (data);
4308 g_assert (text_view->layout);
4309 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4310 g_assert (text_view->cursor_visible);
4312 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4315 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4319 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4323 gtk_text_layout_set_cursor_visible (text_view->layout,
4326 GDK_THREADS_LEAVE ();
4328 /* Remove ourselves */
4334 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4336 if (text_view->blink_timeout)
4338 gtk_timeout_remove (text_view->blink_timeout);
4339 text_view->blink_timeout = 0;
4344 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4346 if (text_view->layout != NULL &&
4347 text_view->cursor_visible &&
4348 GTK_WIDGET_HAS_FOCUS (text_view))
4350 if (cursor_blinks (text_view))
4352 if (text_view->blink_timeout == 0)
4354 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4356 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4362 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4366 gtk_text_view_stop_cursor_blink (text_view);
4371 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4373 if (text_view->layout != NULL &&
4374 text_view->cursor_visible &&
4375 GTK_WIDGET_HAS_FOCUS (text_view) &&
4376 cursor_blinks (text_view))
4378 if (text_view->blink_timeout != 0)
4380 gtk_timeout_remove (text_view->blink_timeout);
4381 text_view->blink_timeout = 0;
4384 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4386 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4394 * Key binding handlers
4398 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4399 GtkTextIter *newplace,
4404 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4410 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4415 /* FIXME when we are unfrozen and can change GtkMovementStep,
4418 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4421 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4422 GtkMovementStep step,
4424 gboolean extend_selection)
4427 GtkTextIter newplace;
4429 gint cursor_x_pos = 0;
4431 gtk_text_view_reset_im_context (text_view);
4433 if (step == GTK_MOVEMENT_PAGES)
4435 gtk_text_view_scroll_pages (text_view, count);
4436 gtk_text_view_pend_cursor_blink (text_view);
4439 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4441 gtk_text_view_scroll_hpages (text_view, count);
4442 gtk_text_view_pend_cursor_blink (text_view);
4446 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4447 gtk_text_buffer_get_mark (get_buffer (text_view),
4451 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4452 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4456 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4457 gtk_text_iter_forward_cursor_positions (&newplace, count);
4460 case GTK_MOVEMENT_VISUAL_POSITIONS:
4461 gtk_text_layout_move_iter_visually (text_view->layout,
4465 case GTK_MOVEMENT_WORDS:
4467 gtk_text_iter_backward_word_starts (&newplace, -count);
4469 gtk_text_iter_forward_word_ends (&newplace, count);
4472 case GTK_MOVEMENT_DISPLAY_LINES:
4473 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4474 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4477 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4479 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4480 else if (count < -1)
4481 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4484 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4487 case GTK_MOVEMENT_PARAGRAPHS:
4490 if (!gtk_text_iter_ends_line (&newplace))
4492 gtk_text_iter_forward_to_line_end (&newplace);
4498 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4500 gtk_text_iter_set_line_offset (&newplace, 0);
4507 gtk_text_iter_forward_lines (&newplace, count);
4508 gtk_text_iter_set_line_offset (&newplace, 0);
4512 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4515 if (!gtk_text_iter_ends_line (&newplace))
4516 gtk_text_iter_forward_to_line_end (&newplace);
4520 gtk_text_iter_set_line_offset (&newplace, 0);
4524 case GTK_MOVEMENT_BUFFER_ENDS:
4526 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4528 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4535 if (!gtk_text_iter_equal (&insert, &newplace))
4537 if (extend_selection)
4538 gtk_text_buffer_move_mark (get_buffer (text_view),
4539 gtk_text_buffer_get_mark (get_buffer (text_view),
4543 gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4545 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4546 gtk_text_view_scroll_mark_onscreen (text_view,
4547 gtk_text_buffer_get_mark (get_buffer (text_view),
4550 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4552 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4556 gtk_text_view_pend_cursor_blink (text_view);
4560 gtk_text_view_move_cursor (GtkTextView *text_view,
4561 GtkMovementStep step,
4563 gboolean extend_selection)
4565 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4569 gtk_text_view_page_horizontally (GtkTextView *text_view,
4571 gboolean extend_selection)
4573 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4574 count, extend_selection);
4578 gtk_text_view_set_anchor (GtkTextView *text_view)
4582 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4583 gtk_text_buffer_get_mark (get_buffer (text_view),
4586 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4590 gtk_text_view_scroll_pages (GtkTextView *text_view,
4596 gint cursor_x_pos, cursor_y_pos;
4597 GtkTextIter new_insert;
4601 g_return_if_fail (text_view->vadjustment != NULL);
4603 adj = text_view->vadjustment;
4605 /* Validate the region that will be brought into view by the cursor motion
4609 gtk_text_view_get_first_para_iter (text_view, &anchor);
4610 y0 = adj->page_size;
4611 y1 = adj->page_size + count * adj->page_increment;
4615 gtk_text_view_get_first_para_iter (text_view, &anchor);
4616 y0 = count * adj->page_increment + adj->page_size;
4620 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4621 /* FIXME do we need to update the adjustment ranges here? */
4623 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4625 /* already at top, just be sure we are at offset 0 */
4626 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4627 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4629 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4631 /* already at bottom, just be sure we are at the end */
4632 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4633 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4637 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4639 newval = adj->value;
4640 oldval = adj->value;
4642 newval += count * adj->page_increment;
4644 set_adjustment_clamped (adj, newval);
4645 cursor_y_pos += adj->value - oldval;
4647 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4648 clamp_iter_onscreen (text_view, &new_insert);
4649 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4651 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4654 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4655 * only guarantees 1 pixel onscreen.
4657 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4658 gtk_text_view_scroll_mark_onscreen (text_view,
4659 gtk_text_buffer_get_mark (get_buffer (text_view),
4664 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4670 gint cursor_x_pos, cursor_y_pos;
4671 GtkTextIter new_insert;
4674 g_return_if_fail (text_view->hadjustment != NULL);
4676 adj = text_view->hadjustment;
4678 /* Validate the line that we're moving within.
4680 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4682 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4683 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4684 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4685 /* FIXME do we need to update the adjustment ranges here? */
4687 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4689 /* already at far left, just be sure we are at offset 0 */
4690 gtk_text_iter_set_line_offset (&new_insert, 0);
4691 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4693 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4695 /* already at far right, just be sure we are at the end */
4696 gtk_text_iter_forward_to_line_end (&new_insert);
4697 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4701 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4703 newval = adj->value;
4704 oldval = adj->value;
4706 newval += count * adj->page_increment;
4708 set_adjustment_clamped (adj, newval);
4709 cursor_x_pos += adj->value - oldval;
4711 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4712 clamp_iter_onscreen (text_view, &new_insert);
4713 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4715 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4718 /* FIXME for lines shorter than the overall widget width, this results in a
4719 * "bounce" effect as we scroll to the right of the widget, then scroll
4720 * back to get the end of the line onscreen.
4721 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4724 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4725 * only guarantees 1 pixel onscreen.
4727 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4728 gtk_text_view_scroll_mark_onscreen (text_view,
4729 gtk_text_buffer_get_mark (get_buffer (text_view),
4734 whitespace (gunichar ch, gpointer user_data)
4736 return (ch == ' ' || ch == '\t');
4740 not_whitespace (gunichar ch, gpointer user_data)
4742 return !whitespace (ch, user_data);
4746 find_whitepace_region (const GtkTextIter *center,
4747 GtkTextIter *start, GtkTextIter *end)
4752 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4753 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4754 if (whitespace (gtk_text_iter_get_char (end), NULL))
4755 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4757 return !gtk_text_iter_equal (start, end);
4761 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4764 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4765 text_view->editable);
4769 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4776 gboolean leave_one = FALSE;
4778 gtk_text_view_reset_im_context (text_view);
4780 if (type == GTK_DELETE_CHARS)
4782 /* Char delete deletes the selection, if one exists */
4783 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4784 text_view->editable))
4788 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4790 gtk_text_buffer_get_mark (get_buffer (text_view),
4798 case GTK_DELETE_CHARS:
4799 gtk_text_iter_forward_cursor_positions (&end, count);
4802 case GTK_DELETE_WORD_ENDS:
4804 gtk_text_iter_forward_word_ends (&end, count);
4806 gtk_text_iter_backward_word_starts (&start, 0 - count);
4809 case GTK_DELETE_WORDS:
4812 case GTK_DELETE_DISPLAY_LINE_ENDS:
4815 case GTK_DELETE_DISPLAY_LINES:
4818 case GTK_DELETE_PARAGRAPH_ENDS:
4819 /* If we're already at a newline, we need to
4820 * simply delete that newline, instead of
4821 * moving to the next one.
4823 if (gtk_text_iter_ends_line (&end))
4825 gtk_text_iter_forward_line (&end);
4831 if (!gtk_text_iter_forward_to_line_end (&end))
4837 /* FIXME figure out what a negative count means
4841 case GTK_DELETE_PARAGRAPHS:
4844 gtk_text_iter_set_line_offset (&start, 0);
4845 gtk_text_iter_forward_to_line_end (&end);
4847 /* Do the lines beyond the first. */
4850 gtk_text_iter_forward_to_line_end (&end);
4856 /* FIXME negative count? */
4860 case GTK_DELETE_WHITESPACE:
4862 find_whitepace_region (&insert, &start, &end);
4870 if (!gtk_text_iter_equal (&start, &end))
4872 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4874 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4875 text_view->editable))
4878 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4880 text_view->editable);
4883 gtk_text_buffer_end_user_action (get_buffer (text_view));
4885 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4886 gtk_text_view_scroll_mark_onscreen (text_view,
4887 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4892 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4894 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4895 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4896 text_view->editable);
4897 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4898 gtk_text_view_scroll_mark_onscreen (text_view,
4899 gtk_text_buffer_get_mark (get_buffer (text_view),
4904 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4906 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4907 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
4908 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4909 gtk_text_view_scroll_mark_onscreen (text_view,
4910 gtk_text_buffer_get_mark (get_buffer (text_view),
4915 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4917 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4918 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4920 text_view->editable);
4921 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4922 gtk_text_view_scroll_mark_onscreen (text_view,
4923 gtk_text_buffer_get_mark (get_buffer (text_view),
4928 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4930 text_view->overwrite_mode = !text_view->overwrite_mode;
4934 gtk_text_view_move_focus (GtkTextView *text_view,
4935 GtkDirectionType direction_type)
4937 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
4939 if (!GTK_WIDGET_TOPLEVEL (toplevel))
4942 /* Propagate to toplevel */
4943 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus", direction_type);
4951 gtk_text_view_unselect (GtkTextView *text_view)
4955 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4957 gtk_text_buffer_get_mark (get_buffer (text_view),
4960 gtk_text_buffer_move_mark (get_buffer (text_view),
4961 gtk_text_buffer_get_mark (get_buffer (text_view),
4967 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
4968 const gchar *mark_name)
4971 GdkModifierType state;
4972 GtkTextIter newplace;
4974 /* DV(g_print (G_STRLOC": begin\n")); */
4976 gdk_window_get_pointer (text_view->text_window->bin_window,
4979 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
4980 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4982 x + text_view->xoffset,
4983 y + text_view->yoffset);
4987 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
4989 /* This may invalidate the layout */
4990 DV(g_print (G_STRLOC": move mark\n"));
4991 gtk_text_buffer_move_mark (get_buffer (text_view),
4995 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4996 gtk_text_view_scroll_mark_onscreen (text_view, mark);
4999 DV (g_print ("first validate idle leaving %s is %d\n",
5000 G_STRLOC, text_view->first_validate_idle));
5004 selection_scan_timeout (gpointer data)
5006 GtkTextView *text_view;
5008 GDK_THREADS_ENTER ();
5010 text_view = GTK_TEXT_VIEW (data);
5012 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5013 move_mark_to_pointer_and_scroll (text_view, "insert");
5015 GDK_THREADS_LEAVE ();
5017 return TRUE; /* remain installed. */
5020 #define DND_SCROLL_MARGIN 0.20
5023 drag_scan_timeout (gpointer data)
5025 GtkTextView *text_view;
5027 GdkModifierType state;
5028 GtkTextIter newplace;
5030 GDK_THREADS_ENTER ();
5032 text_view = GTK_TEXT_VIEW (data);
5034 gdk_window_get_pointer (text_view->text_window->bin_window,
5037 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5039 x + text_view->xoffset,
5040 y + text_view->yoffset);
5042 gtk_text_buffer_move_mark (get_buffer (text_view),
5043 text_view->dnd_mark,
5046 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5047 gtk_text_view_scroll_to_mark (text_view,
5048 text_view->dnd_mark,
5049 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5051 GDK_THREADS_LEAVE ();
5057 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5059 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5060 move_mark_to_pointer_and_scroll (text_view, "insert");
5062 /* If we had to scroll offscreen, insert a timeout to do so
5063 * again. Note that in the timeout, even if the mouse doesn't
5064 * move, due to this scroll xoffset/yoffset will have changed
5065 * and we'll need to scroll again.
5067 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5068 gtk_timeout_remove (text_view->scroll_timeout);
5070 text_view->scroll_timeout =
5071 gtk_timeout_add (50, selection_scan_timeout, text_view);
5077 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5078 const GtkTextIter *iter,
5079 GdkEventButton *button)
5081 GtkTextIter newplace;
5082 GtkTextBuffer *buffer;
5084 g_return_if_fail (text_view->selection_drag_handler == 0);
5086 gtk_grab_add (GTK_WIDGET (text_view));
5088 buffer = get_buffer (text_view);
5092 if (button->state & GDK_SHIFT_MASK)
5094 /* Extend selection */
5095 GtkTextIter start, end;
5097 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5099 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5101 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5104 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5107 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5109 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5112 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5118 /* Replace selection */
5119 gtk_text_buffer_place_cursor (buffer, &newplace);
5122 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5123 "motion_notify_event",
5124 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5128 /* returns whether we were really dragging */
5130 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5132 if (text_view->selection_drag_handler == 0)
5135 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5136 text_view->selection_drag_handler = 0;
5138 if (text_view->scroll_timeout != 0)
5140 gtk_timeout_remove (text_view->scroll_timeout);
5141 text_view->scroll_timeout = 0;
5144 /* one last update to current position */
5145 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5146 move_mark_to_pointer_and_scroll (text_view, "insert");
5148 gtk_grab_remove (GTK_WIDGET (text_view));
5158 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5159 GtkTextAttributes *values,
5162 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5163 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5166 pango_font_description_free (values->font);
5168 values->font = pango_font_description_copy (style->font_desc);
5172 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5174 if (text_view->layout)
5176 gboolean split_cursor;
5177 GtkTextDirection new_dir;
5178 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5180 g_object_get (G_OBJECT (settings),
5181 "gtk-split-cursor", &split_cursor,
5184 new_dir = GTK_TEXT_DIR_NONE;
5186 new_dir = (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
5187 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5189 if (text_view->layout->cursor_direction != new_dir)
5190 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5195 gtk_text_view_ensure_layout (GtkTextView *text_view)
5199 widget = GTK_WIDGET (text_view);
5201 if (text_view->layout == NULL)
5203 GtkTextAttributes *style;
5204 PangoContext *ltr_context, *rtl_context;
5207 DV(g_print(G_STRLOC"\n"));
5209 text_view->layout = gtk_text_layout_new ();
5211 g_signal_connect (G_OBJECT (text_view->layout),
5213 G_CALLBACK (invalidated_handler),
5216 g_signal_connect (G_OBJECT (text_view->layout),
5218 G_CALLBACK (changed_handler),
5221 g_signal_connect (G_OBJECT (text_view->layout),
5223 G_CALLBACK (gtk_text_view_child_allocated),
5226 if (get_buffer (text_view))
5227 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5229 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5230 gtk_text_view_pend_cursor_blink (text_view);
5232 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5234 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5235 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5236 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5237 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5239 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5241 g_object_unref (G_OBJECT (ltr_context));
5242 g_object_unref (G_OBJECT (rtl_context));
5244 gtk_text_view_check_keymap_direction (text_view);
5246 style = gtk_text_attributes_new ();
5248 gtk_widget_ensure_style (widget);
5249 gtk_text_view_set_attributes_from_style (text_view,
5250 style, widget->style);
5252 style->pixels_above_lines = text_view->pixels_above_lines;
5253 style->pixels_below_lines = text_view->pixels_below_lines;
5254 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5255 style->left_margin = text_view->left_margin;
5256 style->right_margin = text_view->right_margin;
5257 style->indent = text_view->indent;
5258 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5260 style->wrap_mode = text_view->wrap_mode;
5261 style->justification = text_view->justify;
5262 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5264 gtk_text_layout_set_default_style (text_view->layout, style);
5266 gtk_text_attributes_unref (style);
5268 /* Set layout for all anchored children */
5270 tmp_list = text_view->children;
5271 while (tmp_list != NULL)
5273 GtkTextViewChild *vc = tmp_list->data;
5277 gtk_text_anchored_child_set_layout (vc->widget,
5279 /* vc may now be invalid! */
5282 tmp_list = g_slist_next (tmp_list);
5288 * gtk_text_view_get_default_attributes:
5289 * @text_view: a #GtkTextView
5291 * Obtains a copy of the default text attributes. These are the
5292 * attributes used for text unless a tag overrides them.
5293 * You'd typically pass the default attributes in to
5294 * gtk_text_iter_get_attributes() in order to get the
5295 * attributes in effect at a given text position.
5297 * The return value is a copy owned by the caller of this function,
5298 * and should be freed.
5300 * Return value: a new #GtkTextAttributes
5303 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5305 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5307 gtk_text_view_ensure_layout (text_view);
5309 return gtk_text_attributes_copy (text_view->layout->default_style);
5313 gtk_text_view_destroy_layout (GtkTextView *text_view)
5315 if (text_view->layout)
5319 gtk_text_view_remove_validate_idles (text_view);
5321 /* Remove layout from all anchored children */
5322 tmp_list = text_view->children;
5323 while (tmp_list != NULL)
5325 GtkTextViewChild *vc = tmp_list->data;
5329 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5330 /* vc may now be invalid! */
5333 tmp_list = g_slist_next (tmp_list);
5336 gtk_text_view_stop_cursor_blink (text_view);
5337 gtk_text_view_end_selection_drag (text_view, NULL);
5339 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5340 invalidated_handler, text_view);
5341 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5342 changed_handler, text_view);
5343 g_object_unref (G_OBJECT (text_view->layout));
5344 text_view->layout = NULL;
5349 gtk_text_view_reset_im_context (GtkTextView *text_view)
5351 if (text_view->need_im_reset)
5353 text_view->need_im_reset = FALSE;
5354 gtk_im_context_reset (text_view->im_context);
5363 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5364 const GtkTextIter *iter,
5365 GdkEventMotion *event)
5367 GdkDragContext *context;
5368 GtkTargetList *target_list;
5370 text_view->drag_start_x = -1;
5371 text_view->drag_start_y = -1;
5372 text_view->pending_place_cursor_button = 0;
5374 target_list = gtk_target_list_new (target_table,
5375 G_N_ELEMENTS (target_table));
5377 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5378 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5379 1, (GdkEvent*)event);
5381 gtk_target_list_unref (target_list);
5383 gtk_drag_set_icon_default (context);
5387 gtk_text_view_drag_begin (GtkWidget *widget,
5388 GdkDragContext *context)
5394 gtk_text_view_drag_end (GtkWidget *widget,
5395 GdkDragContext *context)
5397 GtkTextView *text_view;
5399 text_view = GTK_TEXT_VIEW (widget);
5403 gtk_text_view_drag_data_get (GtkWidget *widget,
5404 GdkDragContext *context,
5405 GtkSelectionData *selection_data,
5409 GtkTextView *text_view;
5411 text_view = GTK_TEXT_VIEW (widget);
5413 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5415 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5417 gtk_selection_data_set (selection_data,
5418 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5431 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5434 /* Extract the selected text */
5435 str = gtk_text_iter_get_visible_text (&start, &end);
5440 gtk_selection_data_set_text (selection_data, str, -1);
5447 gtk_text_view_drag_data_delete (GtkWidget *widget,
5448 GdkDragContext *context)
5450 GtkTextView *text_view;
5452 text_view = GTK_TEXT_VIEW (widget);
5454 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5455 TRUE, GTK_TEXT_VIEW (widget)->editable);
5459 gtk_text_view_drag_leave (GtkWidget *widget,
5460 GdkDragContext *context,
5463 GtkTextView *text_view;
5465 text_view = GTK_TEXT_VIEW (widget);
5467 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5469 if (text_view->scroll_timeout != 0)
5470 gtk_timeout_remove (text_view->scroll_timeout);
5472 text_view->scroll_timeout = 0;
5476 gtk_text_view_drag_motion (GtkWidget *widget,
5477 GdkDragContext *context,
5482 GtkTextIter newplace;
5483 GtkTextView *text_view;
5486 GdkRectangle target_rect;
5488 GdkDragAction suggested_action = 0;
5490 text_view = GTK_TEXT_VIEW (widget);
5492 target_rect = text_view->text_window->allocation;
5494 if (x < target_rect.x ||
5495 y < target_rect.y ||
5496 x > (target_rect.x + target_rect.width) ||
5497 y > (target_rect.y + target_rect.height))
5498 return FALSE; /* outside the text window, allow parent widgets to handle event */
5500 gtk_text_view_window_to_buffer_coords (text_view,
5501 GTK_TEXT_WINDOW_WIDGET,
5505 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5509 if (gtk_drag_dest_find_target (widget, context,
5510 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5512 /* can't accept any of the offered targets */
5514 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5516 gtk_text_iter_in_range (&newplace, &start, &end))
5518 /* We're inside the selection. */
5522 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5524 GtkWidget *source_widget;
5526 suggested_action = context->suggested_action;
5528 source_widget = gtk_drag_get_source_widget (context);
5530 if (source_widget == widget)
5532 /* Default to MOVE, unless the user has
5533 * pressed ctrl or alt to affect available actions
5535 if ((context->actions & GDK_ACTION_MOVE) != 0)
5536 suggested_action = GDK_ACTION_MOVE;
5541 /* Can't drop here. */
5545 if (suggested_action != 0)
5547 gtk_text_mark_set_visible (text_view->dnd_mark,
5548 text_view->cursor_visible);
5550 gdk_drag_status (context, suggested_action, time);
5554 gdk_drag_status (context, 0, time);
5555 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5558 gtk_text_buffer_move_mark (get_buffer (text_view),
5559 text_view->dnd_mark,
5562 DV(g_print (G_STRLOC": scrolling to mark\n"));
5563 gtk_text_view_scroll_to_mark (text_view,
5564 text_view->dnd_mark,
5565 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5567 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5568 gtk_timeout_remove (text_view->scroll_timeout);
5570 text_view->scroll_timeout =
5571 gtk_timeout_add (50, drag_scan_timeout, text_view);
5573 /* TRUE return means don't propagate the drag motion to parent
5574 * widgets that may also be drop sites.
5580 gtk_text_view_drag_drop (GtkWidget *widget,
5581 GdkDragContext *context,
5586 GtkTextView *text_view;
5587 GtkTextIter drop_point;
5588 GdkAtom target = GDK_NONE;
5590 text_view = GTK_TEXT_VIEW (widget);
5592 if (text_view->scroll_timeout != 0)
5593 gtk_timeout_remove (text_view->scroll_timeout);
5595 text_view->scroll_timeout = 0;
5597 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5599 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5601 text_view->dnd_mark);
5603 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5604 target = gtk_drag_dest_find_target (widget, context, NULL);
5606 if (target != GDK_NONE)
5607 gtk_drag_get_data (widget, context, target, time);
5609 gtk_drag_finish (context, FALSE, FALSE, time);
5615 insert_text_data (GtkTextView *text_view,
5616 GtkTextIter *drop_point,
5617 GtkSelectionData *selection_data)
5621 str = gtk_selection_data_get_text (selection_data);
5625 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5626 drop_point, str, -1,
5627 text_view->editable);
5633 gtk_text_view_drag_data_received (GtkWidget *widget,
5634 GdkDragContext *context,
5637 GtkSelectionData *selection_data,
5641 GtkTextIter drop_point;
5642 GtkTextView *text_view;
5643 gboolean success = FALSE;
5645 text_view = GTK_TEXT_VIEW (widget);
5647 if (!text_view->dnd_mark)
5650 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5652 text_view->dnd_mark);
5654 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5657 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5659 GtkTextBuffer *src_buffer = NULL;
5660 GtkTextIter start, end;
5661 gboolean copy_tags = TRUE;
5663 if (selection_data->length != sizeof (src_buffer))
5666 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5668 if (src_buffer == NULL)
5671 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5673 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5674 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5677 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5682 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5686 text_view->editable);
5691 str = gtk_text_iter_get_visible_text (&start, &end);
5692 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5693 &drop_point, str, -1,
5694 text_view->editable);
5700 insert_text_data (text_view, &drop_point, selection_data);
5705 gtk_drag_finish (context, success,
5706 success && context->action == GDK_ACTION_MOVE,
5710 static GtkAdjustment*
5711 get_hadjustment (GtkTextView *text_view)
5713 if (text_view->hadjustment == NULL)
5714 gtk_text_view_set_scroll_adjustments (text_view,
5715 NULL, /* forces creation */
5716 text_view->vadjustment);
5718 return text_view->hadjustment;
5721 static GtkAdjustment*
5722 get_vadjustment (GtkTextView *text_view)
5724 if (text_view->vadjustment == NULL)
5725 gtk_text_view_set_scroll_adjustments (text_view,
5726 text_view->hadjustment,
5727 NULL); /* forces creation */
5728 return text_view->vadjustment;
5733 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5734 GtkAdjustment *hadj,
5735 GtkAdjustment *vadj)
5737 gboolean need_adjust = FALSE;
5739 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5742 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5744 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5746 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5748 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5750 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5752 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5753 g_object_unref (G_OBJECT (text_view->hadjustment));
5756 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5758 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5759 g_object_unref (G_OBJECT (text_view->vadjustment));
5762 if (text_view->hadjustment != hadj)
5764 text_view->hadjustment = hadj;
5765 g_object_ref (G_OBJECT (text_view->hadjustment));
5766 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5768 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5769 (GtkSignalFunc) gtk_text_view_value_changed,
5774 if (text_view->vadjustment != vadj)
5776 text_view->vadjustment = vadj;
5777 g_object_ref (G_OBJECT (text_view->vadjustment));
5778 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5780 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5781 (GtkSignalFunc) gtk_text_view_value_changed,
5787 gtk_text_view_value_changed (NULL, text_view);
5791 gtk_text_view_value_changed (GtkAdjustment *adj,
5792 GtkTextView *text_view)
5799 /* Note that we oddly call this function with adj == NULL
5803 text_view->onscreen_validated = FALSE;
5805 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5806 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5807 adj ? adj->value : 0.0));
5809 if (adj == text_view->hadjustment)
5811 dx = text_view->xoffset - (gint)adj->value;
5812 text_view->xoffset = adj->value;
5814 else if (adj == text_view->vadjustment)
5816 dy = text_view->yoffset - (gint)adj->value;
5817 text_view->yoffset = adj->value;
5819 if (text_view->layout)
5821 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5823 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5825 text_view->first_para_pixels = adj->value - line_top;
5829 if (dx != 0 || dy != 0)
5833 if (GTK_WIDGET_REALIZED (text_view))
5837 if (text_view->left_window)
5838 text_window_scroll (text_view->left_window, 0, dy);
5839 if (text_view->right_window)
5840 text_window_scroll (text_view->right_window, 0, dy);
5845 if (text_view->top_window)
5846 text_window_scroll (text_view->top_window, dx, 0);
5847 if (text_view->bottom_window)
5848 text_window_scroll (text_view->bottom_window, dx, 0);
5851 /* It looks nicer to scroll the main area last, because
5852 * it takes a while, and making the side areas update
5853 * afterward emphasizes the slowness of scrolling the
5856 text_window_scroll (text_view->text_window, dx, dy);
5859 /* Children are now "moved" in the text window, poke
5860 * into widget->allocation for each child
5862 tmp_list = text_view->children;
5863 while (tmp_list != NULL)
5865 GtkTextViewChild *child = tmp_list->data;
5869 child->widget->allocation.x -= dx;
5870 child->widget->allocation.y -= dy;
5873 g_print ("allocation for %p tweaked to %d,%d\n",
5875 child->widget->allocation.x,
5876 child->widget->allocation.y);
5880 tmp_list = g_slist_next (tmp_list);
5884 /* This could result in invalidation, which would install the
5885 * first_validate_idle, which would validate onscreen;
5886 * but we're going to go ahead and validate here, so
5887 * first_validate_idle shouldn't have anything to do.
5889 gtk_text_view_update_layout_width (text_view);
5891 /* note that validation of onscreen could invoke this function
5892 * recursively, by scrolling to maintain first_para, or in response
5893 * to updating the layout width, however there is no problem with
5894 * that, or shouldn't be.
5896 gtk_text_view_validate_onscreen (text_view);
5898 /* process exposes */
5899 if (GTK_WIDGET_REALIZED (text_view))
5901 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
5903 if (text_view->left_window)
5904 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
5906 if (text_view->right_window)
5907 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
5909 if (text_view->top_window)
5910 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
5912 if (text_view->bottom_window)
5913 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
5915 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
5918 /* If this got installed, get rid of it, it's just a waste of time. */
5919 if (text_view->first_validate_idle != 0)
5921 g_source_remove (text_view->first_validate_idle);
5922 text_view->first_validate_idle = 0;
5925 gtk_text_view_update_im_spot_location (text_view);
5927 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
5931 gtk_text_view_commit_handler (GtkIMContext *context,
5933 GtkTextView *text_view)
5935 gtk_text_view_commit_text (text_view, str);
5939 gtk_text_view_commit_text (GtkTextView *text_view,
5942 gboolean had_selection;
5944 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5946 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5949 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5950 text_view->editable);
5952 if (!strcmp (str, "\n"))
5954 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
5955 text_view->editable);
5959 if (!had_selection && text_view->overwrite_mode)
5960 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
5961 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5962 text_view->editable);
5965 gtk_text_buffer_end_user_action (get_buffer (text_view));
5967 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5968 gtk_text_view_scroll_mark_onscreen (text_view,
5969 gtk_text_buffer_get_mark (get_buffer (text_view),
5974 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
5975 GtkTextView *text_view)
5978 PangoAttrList *attrs;
5981 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
5982 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
5984 pango_attr_list_unref (attrs);
5989 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
5990 GtkTextView *text_view)
5997 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
5998 gtk_text_buffer_get_insert (text_view->buffer));
6001 pos = gtk_text_iter_get_line_index (&start);
6002 gtk_text_iter_set_line_offset (&start, 0);
6003 gtk_text_iter_forward_to_line_end (&end);
6005 text = gtk_text_iter_get_slice (&start, &end);
6006 gtk_im_context_set_surrounding (context, text, -1, pos);
6013 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6016 GtkTextView *text_view)
6021 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6022 gtk_text_buffer_get_insert (text_view->buffer));
6025 gtk_text_iter_forward_chars (&start, offset);
6026 gtk_text_iter_forward_chars (&end, offset + n_chars);
6028 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6034 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6035 const GtkTextIter *location,
6039 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6040 gboolean need_reset = FALSE;
6042 if (mark == gtk_text_buffer_get_insert (buffer))
6044 text_view->virtual_cursor_x = -1;
6045 text_view->virtual_cursor_y = -1;
6046 gtk_text_view_update_im_spot_location (text_view);
6049 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6055 gtk_text_view_reset_im_context (text_view);
6059 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6063 GdkRectangle strong_pos;
6066 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6067 gtk_text_buffer_get_mark (get_buffer (text_view),
6070 if ((x && text_view->virtual_cursor_x == -1) ||
6071 (y && text_view->virtual_cursor_y == -1))
6072 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6076 if (text_view->virtual_cursor_x != -1)
6077 *x = text_view->virtual_cursor_x;
6084 if (text_view->virtual_cursor_x != -1)
6085 *y = text_view->virtual_cursor_y;
6087 *y = strong_pos.y + strong_pos.height / 2;
6092 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6096 GdkRectangle strong_pos;
6099 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6100 gtk_text_buffer_get_mark (get_buffer (text_view),
6103 if (x == -1 || y == -1)
6104 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6106 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6107 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6110 /* Quick hack of a popup menu
6113 activate_cb (GtkWidget *menuitem,
6114 GtkTextView *text_view)
6116 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6117 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6121 append_action_signal (GtkTextView *text_view,
6123 const gchar *stock_id,
6124 const gchar *signal,
6127 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6129 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6130 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6131 GTK_SIGNAL_FUNC (activate_cb), text_view);
6133 gtk_widget_set_sensitive (menuitem, sensitive);
6135 gtk_widget_show (menuitem);
6136 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6140 popup_menu_detach (GtkWidget *attach_widget,
6143 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6147 popup_position_func (GtkMenu *menu,
6153 GtkTextView *text_view;
6155 GdkRectangle cursor_rect;
6156 GdkRectangle onscreen_rect;
6157 gint root_x, root_y;
6161 text_view = GTK_TEXT_VIEW (user_data);
6162 widget = GTK_WIDGET (text_view);
6164 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6166 gdk_window_get_origin (widget->window, &root_x, &root_y);
6168 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6170 gtk_text_buffer_get_insert (get_buffer (text_view)));
6172 gtk_text_view_get_iter_location (text_view,
6176 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6178 gtk_widget_size_request (text_view->popup_menu, &req);
6180 /* can't use rectangle_intersect since cursor rect can have 0 width */
6181 if (cursor_rect.x >= onscreen_rect.x &&
6182 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6183 cursor_rect.y >= onscreen_rect.y &&
6184 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6186 gtk_text_view_buffer_to_window_coords (text_view,
6187 GTK_TEXT_WINDOW_WIDGET,
6188 cursor_rect.x, cursor_rect.y,
6189 &cursor_rect.x, &cursor_rect.y);
6191 *x = root_x + cursor_rect.x + cursor_rect.width;
6192 *y = root_y + cursor_rect.y + cursor_rect.height;
6196 /* Just center the menu, since cursor is offscreen. */
6197 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6198 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6202 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6203 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6205 *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
6206 *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
6211 GtkTextView *text_view;
6217 range_contains_editable_text (const GtkTextIter *start,
6218 const GtkTextIter *end,
6219 gboolean default_editability)
6221 GtkTextIter iter = *start;
6223 while (gtk_text_iter_compare (&iter, end) < 0)
6225 if (gtk_text_iter_editable (&iter, default_editability))
6228 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6235 unichar_chosen_func (const char *text,
6238 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6240 gtk_text_view_commit_text (text_view, text);
6244 popup_targets_received (GtkClipboard *clipboard,
6245 GtkSelectionData *data,
6248 PopupInfo *info = user_data;
6249 GtkTextView *text_view = info->text_view;
6251 if (GTK_WIDGET_REALIZED (text_view))
6253 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6254 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6256 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6257 GtkWidget *menuitem;
6259 gboolean have_selection;
6260 gboolean can_insert;
6262 GtkTextIter sel_start, sel_end;
6264 if (text_view->popup_menu)
6265 gtk_widget_destroy (text_view->popup_menu);
6267 text_view->popup_menu = gtk_menu_new ();
6269 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6270 GTK_WIDGET (text_view),
6273 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6274 &sel_start, &sel_end);
6276 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6278 gtk_text_buffer_get_insert (get_buffer (text_view)));
6280 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6282 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6284 range_contains_editable_text (&sel_start, &sel_end,
6285 text_view->editable));
6286 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6288 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6289 can_insert && clipboard_contains_text);
6291 menuitem = gtk_separator_menu_item_new ();
6292 gtk_widget_show (menuitem);
6293 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6295 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6296 gtk_widget_show (menuitem);
6297 submenu = gtk_menu_new ();
6298 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6299 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6301 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6302 GTK_MENU_SHELL (submenu));
6304 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6305 gtk_widget_show (menuitem);
6306 gtk_widget_set_sensitive (menuitem, can_insert);
6308 submenu = gtk_menu_new ();
6309 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6310 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6312 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6313 unichar_chosen_func,
6316 gtk_signal_emit (GTK_OBJECT (text_view),
6317 signals[POPULATE_POPUP],
6318 text_view->popup_menu);
6321 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6323 info->button, info->time);
6326 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6327 popup_position_func, text_view,
6328 0, gtk_get_current_event_time ());
6329 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu));
6333 g_object_unref (text_view);
6338 gtk_text_view_do_popup (GtkTextView *text_view,
6339 GdkEventButton *event)
6341 PopupInfo *info = g_new (PopupInfo, 1);
6343 /* In order to know what entries we should make sensitive, we
6344 * ask for the current targets of the clipboard, and when
6345 * we get them, then we actually pop up the menu.
6347 info->text_view = g_object_ref (text_view);
6351 info->button = event->button;
6352 info->time = event->time;
6357 info->time = gtk_get_current_event_time ();
6360 gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
6361 gdk_atom_intern ("TARGETS", FALSE),
6362 popup_targets_received,
6367 gtk_text_view_popup_menu (GtkWidget *widget)
6369 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6373 /* Child GdkWindows */
6376 static GtkTextWindow*
6377 text_window_new (GtkTextWindowType type,
6380 gint height_request)
6384 win = g_new (GtkTextWindow, 1);
6387 win->widget = widget;
6389 win->bin_window = NULL;
6390 win->requisition.width = width_request;
6391 win->requisition.height = height_request;
6392 win->allocation.width = width_request;
6393 win->allocation.height = height_request;
6394 win->allocation.x = 0;
6395 win->allocation.y = 0;
6401 text_window_free (GtkTextWindow *win)
6404 text_window_unrealize (win);
6410 text_window_realize (GtkTextWindow *win,
6413 GdkWindowAttr attributes;
6414 gint attributes_mask;
6417 attributes.window_type = GDK_WINDOW_CHILD;
6418 attributes.x = win->allocation.x;
6419 attributes.y = win->allocation.y;
6420 attributes.width = win->allocation.width;
6421 attributes.height = win->allocation.height;
6422 attributes.wclass = GDK_INPUT_OUTPUT;
6423 attributes.visual = gtk_widget_get_visual (win->widget);
6424 attributes.colormap = gtk_widget_get_colormap (win->widget);
6425 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6427 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6429 win->window = gdk_window_new (parent,
6433 gdk_window_show (win->window);
6434 gdk_window_set_user_data (win->window, win->widget);
6438 attributes.width = win->allocation.width;
6439 attributes.height = win->allocation.height;
6440 attributes.event_mask = (GDK_EXPOSURE_MASK |
6442 GDK_KEY_PRESS_MASK |
6443 GDK_BUTTON_PRESS_MASK |
6444 GDK_BUTTON_RELEASE_MASK |
6445 GDK_POINTER_MOTION_MASK |
6446 GDK_POINTER_MOTION_HINT_MASK |
6447 gtk_widget_get_events (win->widget));
6449 win->bin_window = gdk_window_new (win->window,
6453 gdk_window_show (win->bin_window);
6454 gdk_window_set_user_data (win->bin_window, win->widget);
6456 if (win->type == GTK_TEXT_WINDOW_TEXT)
6459 cursor = gdk_cursor_new (GDK_XTERM);
6460 gdk_window_set_cursor (win->bin_window, cursor);
6461 gdk_cursor_unref (cursor);
6463 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6467 gdk_window_set_background (win->bin_window,
6468 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6472 gdk_window_set_background (win->bin_window,
6473 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6476 g_object_set_qdata (G_OBJECT (win->window),
6477 g_quark_from_static_string ("gtk-text-view-text-window"),
6480 g_object_set_qdata (G_OBJECT (win->bin_window),
6481 g_quark_from_static_string ("gtk-text-view-text-window"),
6486 text_window_unrealize (GtkTextWindow *win)
6488 if (win->type == GTK_TEXT_WINDOW_TEXT)
6490 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6494 gdk_window_set_user_data (win->window, NULL);
6495 gdk_window_set_user_data (win->bin_window, NULL);
6496 gdk_window_destroy (win->bin_window);
6497 gdk_window_destroy (win->window);
6499 win->bin_window = NULL;
6503 text_window_size_allocate (GtkTextWindow *win,
6506 win->allocation = *rect;
6510 gdk_window_move_resize (win->window,
6512 rect->width, rect->height);
6514 gdk_window_resize (win->bin_window,
6515 rect->width, rect->height);
6520 text_window_scroll (GtkTextWindow *win,
6524 if (dx != 0 || dy != 0)
6526 gdk_window_scroll (win->bin_window, dx, dy);
6531 text_window_invalidate_rect (GtkTextWindow *win,
6534 GdkRectangle window_rect;
6536 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6543 window_rect.width = rect->width;
6544 window_rect.height = rect->height;
6546 /* Adjust the rect as appropriate */
6550 case GTK_TEXT_WINDOW_TEXT:
6553 case GTK_TEXT_WINDOW_LEFT:
6554 case GTK_TEXT_WINDOW_RIGHT:
6556 window_rect.width = win->allocation.width;
6559 case GTK_TEXT_WINDOW_TOP:
6560 case GTK_TEXT_WINDOW_BOTTOM:
6562 window_rect.height = win->allocation.height;
6566 g_warning ("%s: bug!", G_STRLOC);
6571 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6575 GdkColor color = { 0, 65535, 0, 0 };
6576 GdkGC *gc = gdk_gc_new (win->bin_window);
6577 gdk_gc_set_rgb_fg_color (gc, &color);
6578 gdk_draw_rectangle (win->bin_window,
6579 gc, TRUE, window_rect.x, window_rect.y,
6580 window_rect.width, window_rect.height);
6587 text_window_get_width (GtkTextWindow *win)
6589 return win->allocation.width;
6593 text_window_get_height (GtkTextWindow *win)
6595 return win->allocation.height;
6599 text_window_get_allocation (GtkTextWindow *win,
6602 *rect = win->allocation;
6609 * gtk_text_view_get_window:
6610 * @text_view: a #GtkTextView
6611 * @win: window to get
6613 * Retrieves the #GdkWindow corresponding to an area of the text view;
6614 * possible windows include the overall widget window, child windows
6615 * on the left, right, top, bottom, and the window that displays the
6616 * text buffer. Windows are %NULL and nonexistent if their width or
6617 * height is 0, and are nonexistent before the widget has been
6620 * Return value: a #GdkWindow, or %NULL
6623 gtk_text_view_get_window (GtkTextView *text_view,
6624 GtkTextWindowType win)
6626 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6630 case GTK_TEXT_WINDOW_WIDGET:
6631 return GTK_WIDGET (text_view)->window;
6634 case GTK_TEXT_WINDOW_TEXT:
6635 return text_view->text_window->bin_window;
6638 case GTK_TEXT_WINDOW_LEFT:
6639 if (text_view->left_window)
6640 return text_view->left_window->bin_window;
6645 case GTK_TEXT_WINDOW_RIGHT:
6646 if (text_view->right_window)
6647 return text_view->right_window->bin_window;
6652 case GTK_TEXT_WINDOW_TOP:
6653 if (text_view->top_window)
6654 return text_view->top_window->bin_window;
6659 case GTK_TEXT_WINDOW_BOTTOM:
6660 if (text_view->bottom_window)
6661 return text_view->bottom_window->bin_window;
6666 case GTK_TEXT_WINDOW_PRIVATE:
6667 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6672 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6677 * gtk_text_view_get_window_type:
6678 * @text_view: a #GtkTextView
6679 * @window: a window type
6681 * Usually used to find out which window an event corresponds to.
6682 * If you connect to an event signal on @text_view, this function
6683 * should be called on <literal>event->window</literal> to
6684 * see which window it was.
6686 * Return value: the window type.
6689 gtk_text_view_get_window_type (GtkTextView *text_view,
6694 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6695 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6697 if (window == GTK_WIDGET (text_view)->window)
6698 return GTK_TEXT_WINDOW_WIDGET;
6700 win = g_object_get_qdata (G_OBJECT (window),
6701 g_quark_try_string ("gtk-text-view-text-window"));
6707 return GTK_TEXT_WINDOW_PRIVATE;
6712 buffer_to_widget (GtkTextView *text_view,
6718 gint focus_edge_width;
6719 gboolean interior_focus;
6722 gtk_widget_style_get (GTK_WIDGET (text_view),
6723 "interior_focus", &interior_focus,
6724 "focus_line_width", &focus_width,
6728 focus_edge_width = 0;
6730 focus_edge_width = focus_width;
6734 *window_x = buffer_x - text_view->xoffset + focus_edge_width;
6735 if (text_view->left_window)
6736 *window_x += text_view->left_window->allocation.width;
6741 *window_y = buffer_y - text_view->yoffset + focus_edge_width;
6742 if (text_view->top_window)
6743 *window_y += text_view->top_window->allocation.height;
6748 widget_to_text_window (GtkTextWindow *win,
6755 *window_x = widget_x - win->allocation.x;
6758 *window_y = widget_y - win->allocation.y;
6762 buffer_to_text_window (GtkTextView *text_view,
6771 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6772 "for a nonexistent or private child window of GtkTextView");
6776 buffer_to_widget (text_view,
6778 window_x, window_y);
6780 widget_to_text_window (win,
6781 window_x ? *window_x : 0,
6782 window_y ? *window_y : 0,
6788 * gtk_text_view_buffer_to_window_coords:
6789 * @text_view: a #GtkTextView
6790 * @win: a #GtkTextWindowType
6791 * @buffer_x: buffer x coordinate
6792 * @buffer_y: buffer y coordinate
6793 * @window_x: window x coordinate return location
6794 * @window_y: window y coordinate return location
6796 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6797 * @win, and stores the result in (@window_x, @window_y).
6800 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6801 GtkTextWindowType win,
6807 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6811 case GTK_TEXT_WINDOW_WIDGET:
6812 buffer_to_widget (text_view,
6814 window_x, window_y);
6817 case GTK_TEXT_WINDOW_TEXT:
6819 *window_x = buffer_x - text_view->xoffset;
6821 *window_y = buffer_y - text_view->yoffset;
6824 case GTK_TEXT_WINDOW_LEFT:
6825 buffer_to_text_window (text_view,
6826 text_view->left_window,
6828 window_x, window_y);
6831 case GTK_TEXT_WINDOW_RIGHT:
6832 buffer_to_text_window (text_view,
6833 text_view->right_window,
6835 window_x, window_y);
6838 case GTK_TEXT_WINDOW_TOP:
6839 buffer_to_text_window (text_view,
6840 text_view->top_window,
6842 window_x, window_y);
6845 case GTK_TEXT_WINDOW_BOTTOM:
6846 buffer_to_text_window (text_view,
6847 text_view->bottom_window,
6849 window_x, window_y);
6852 case GTK_TEXT_WINDOW_PRIVATE:
6853 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6857 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6863 widget_to_buffer (GtkTextView *text_view,
6869 gint focus_edge_width;
6870 gboolean interior_focus;
6873 gtk_widget_style_get (GTK_WIDGET (text_view),
6874 "interior_focus", &interior_focus,
6875 "focus_line_width", &focus_width,
6879 focus_edge_width = 0;
6881 focus_edge_width = focus_width;
6885 *buffer_x = widget_x - focus_edge_width + text_view->xoffset;
6886 if (text_view->left_window)
6887 *buffer_x -= text_view->left_window->allocation.width;
6892 *buffer_y = widget_y - focus_edge_width + text_view->yoffset;
6893 if (text_view->top_window)
6894 *buffer_y -= text_view->top_window->allocation.height;
6899 text_window_to_widget (GtkTextWindow *win,
6906 *widget_x = window_x + win->allocation.x;
6909 *widget_y = window_y + win->allocation.y;
6913 text_window_to_buffer (GtkTextView *text_view,
6922 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
6923 "coordinates for a nonexistent child window.");
6927 text_window_to_widget (win,
6933 widget_to_buffer (text_view,
6934 buffer_x ? *buffer_x : 0,
6935 buffer_y ? *buffer_y : 0,
6941 * gtk_text_view_window_to_buffer_coords:
6942 * @text_view: a #GtkTextView
6943 * @win: a #GtkTextWindowType
6944 * @window_x: window x coordinate
6945 * @window_y: window y coordinate
6946 * @buffer_x: buffer x coordinate return location
6947 * @buffer_y: buffer y coordinate return location
6949 * Converts coordinates on the window identified by @win to buffer
6950 * coordinates, storing the result in (@buffer_x,@buffer_y).
6953 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
6954 GtkTextWindowType win,
6960 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6964 case GTK_TEXT_WINDOW_WIDGET:
6965 widget_to_buffer (text_view,
6967 buffer_x, buffer_y);
6970 case GTK_TEXT_WINDOW_TEXT:
6972 *buffer_x = window_x + text_view->xoffset;
6974 *buffer_y = window_y + text_view->yoffset;
6977 case GTK_TEXT_WINDOW_LEFT:
6978 text_window_to_buffer (text_view,
6979 text_view->left_window,
6981 buffer_x, buffer_y);
6984 case GTK_TEXT_WINDOW_RIGHT:
6985 text_window_to_buffer (text_view,
6986 text_view->right_window,
6988 buffer_x, buffer_y);
6991 case GTK_TEXT_WINDOW_TOP:
6992 text_window_to_buffer (text_view,
6993 text_view->top_window,
6995 buffer_x, buffer_y);
6998 case GTK_TEXT_WINDOW_BOTTOM:
6999 text_window_to_buffer (text_view,
7000 text_view->bottom_window,
7002 buffer_x, buffer_y);
7005 case GTK_TEXT_WINDOW_PRIVATE:
7006 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7010 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7016 set_window_width (GtkTextView *text_view,
7018 GtkTextWindowType type,
7019 GtkTextWindow **winp)
7025 text_window_free (*winp);
7027 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7034 *winp = text_window_new (type,
7035 GTK_WIDGET (text_view),
7037 /* if the widget is already realized we need to realize the child manually */
7038 if (GTK_WIDGET_REALIZED (text_view))
7039 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7043 if ((*winp)->requisition.width == width)
7046 (*winp)->requisition.width = width;
7049 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7055 set_window_height (GtkTextView *text_view,
7057 GtkTextWindowType type,
7058 GtkTextWindow **winp)
7064 text_window_free (*winp);
7066 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7073 *winp = text_window_new (type,
7074 GTK_WIDGET (text_view),
7077 /* if the widget is already realized we need to realize the child manually */
7078 if (GTK_WIDGET_REALIZED (text_view))
7079 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7083 if ((*winp)->requisition.height == height)
7086 (*winp)->requisition.height = height;
7089 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7094 * gtk_text_view_set_border_window_size:
7095 * @text_view: a #GtkTextView
7096 * @type: window to affect
7097 * @size: width or height of the window
7099 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7100 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7101 * Automatically destroys the corresponding window if the size is set
7102 * to 0, and creates the window if the size is set to non-zero. This
7103 * function can only be used for the "border windows," it doesn't work
7104 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7105 * #GTK_TEXT_WINDOW_PRIVATE.
7108 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7109 GtkTextWindowType type,
7113 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7114 g_return_if_fail (size >= 0);
7118 case GTK_TEXT_WINDOW_LEFT:
7119 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7120 &text_view->left_window);
7123 case GTK_TEXT_WINDOW_RIGHT:
7124 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7125 &text_view->right_window);
7128 case GTK_TEXT_WINDOW_TOP:
7129 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7130 &text_view->top_window);
7133 case GTK_TEXT_WINDOW_BOTTOM:
7134 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7135 &text_view->bottom_window);
7139 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7145 * gtk_text_view_get_border_window_size:
7146 * @text_view: a #GtkTextView
7147 * @type: window to return size from
7149 * Gets the width of the specified border window. See
7150 * gtk_text_view_set_border_window_size().
7152 * Return value: width of window
7155 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7156 GtkTextWindowType type)
7158 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7162 case GTK_TEXT_WINDOW_LEFT:
7163 if (text_view->left_window)
7164 return text_view->left_window->requisition.width;
7166 case GTK_TEXT_WINDOW_RIGHT:
7167 if (text_view->right_window)
7168 return text_view->right_window->requisition.width;
7170 case GTK_TEXT_WINDOW_TOP:
7171 if (text_view->top_window)
7172 return text_view->top_window->requisition.height;
7174 case GTK_TEXT_WINDOW_BOTTOM:
7175 if (text_view->bottom_window)
7176 return text_view->bottom_window->requisition.height;
7179 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7190 static GtkTextViewChild*
7191 text_view_child_new_anchored (GtkWidget *child,
7192 GtkTextChildAnchor *anchor,
7193 GtkTextLayout *layout)
7195 GtkTextViewChild *vc;
7197 vc = g_new (GtkTextViewChild, 1);
7200 vc->anchor = anchor;
7202 vc->from_top_of_line = 0;
7203 vc->from_left_of_buffer = 0;
7205 g_object_ref (G_OBJECT (vc->widget));
7206 g_object_ref (G_OBJECT (vc->anchor));
7208 g_object_set_data (G_OBJECT (child),
7209 "gtk-text-view-child",
7212 gtk_text_child_anchor_register_child (anchor, child, layout);
7217 static GtkTextViewChild*
7218 text_view_child_new_window (GtkWidget *child,
7219 GtkTextWindowType type,
7223 GtkTextViewChild *vc;
7225 vc = g_new (GtkTextViewChild, 1);
7230 vc->from_top_of_line = 0;
7231 vc->from_left_of_buffer = 0;
7233 g_object_ref (G_OBJECT (vc->widget));
7239 g_object_set_data (G_OBJECT (child),
7240 "gtk-text-view-child",
7247 text_view_child_free (GtkTextViewChild *child)
7249 g_object_set_data (G_OBJECT (child->widget),
7250 "gtk-text-view-child", NULL);
7254 gtk_text_child_anchor_unregister_child (child->anchor,
7256 g_object_unref (G_OBJECT (child->anchor));
7259 g_object_unref (G_OBJECT (child->widget));
7265 text_view_child_set_parent_window (GtkTextView *text_view,
7266 GtkTextViewChild *vc)
7269 gtk_widget_set_parent_window (vc->widget,
7270 text_view->text_window->bin_window);
7274 window = gtk_text_view_get_window (text_view,
7276 gtk_widget_set_parent_window (vc->widget, window);
7281 add_child (GtkTextView *text_view,
7282 GtkTextViewChild *vc)
7284 text_view->children = g_slist_prepend (text_view->children,
7287 if (GTK_WIDGET_REALIZED (text_view))
7288 text_view_child_set_parent_window (text_view, vc);
7290 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7294 * gtk_text_view_add_child_at_anchor:
7295 * @text_view: a #GtkTextView
7296 * @child: a #GtkWidget
7297 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7299 * Adds a child widget in the text buffer, at the given @anchor.
7303 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7305 GtkTextChildAnchor *anchor)
7307 GtkTextViewChild *vc;
7309 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7310 g_return_if_fail (GTK_IS_WIDGET (child));
7311 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7312 g_return_if_fail (child->parent == NULL);
7314 gtk_text_view_ensure_layout (text_view);
7316 vc = text_view_child_new_anchored (child, anchor,
7319 add_child (text_view, vc);
7321 g_assert (vc->widget == child);
7322 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7326 * gtk_text_view_add_child_in_window:
7327 * @text_view: a #GtkTextView
7328 * @child: a #GtkWidget
7329 * @which_window: which window the child should appear in
7330 * @xpos: X position of child in window coordinates
7331 * @ypos: Y position of child in window coordinates
7333 * Adds a child at fixed coordinates in one of the text widget's
7334 * windows. The window must have nonzero size (see
7335 * gtk_text_view_set_border_window_size()). Note that the child
7336 * coordinates are given relative to the #GdkWindow in question, and
7337 * that these coordinates have no sane relationship to scrolling. When
7338 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7339 * irrelevant, the child floats above all scrollable areas. But when
7340 * placing a child in one of the scrollable windows (border windows or
7341 * text window), you'll need to compute the child's correct position
7342 * in buffer coordinates any time scrolling occurs or buffer changes
7343 * occur, and then call gtk_text_view_move_child() to update the
7344 * child's position. Unfortunately there's no good way to detect that
7345 * scrolling has occurred, using the current API; a possible hack
7346 * would be to update all child positions when the scroll adjustments
7347 * change or the text buffer changes. See bug 64518 on
7348 * bugzilla.gnome.org for status of fixing this issue.
7352 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7354 GtkTextWindowType which_window,
7358 GtkTextViewChild *vc;
7360 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7361 g_return_if_fail (GTK_IS_WIDGET (child));
7362 g_return_if_fail (child->parent == NULL);
7364 vc = text_view_child_new_window (child, which_window,
7367 add_child (text_view, vc);
7369 g_assert (vc->widget == child);
7370 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7374 * gtk_text_view_move_child:
7375 * @text_view: a #GtkTextView
7376 * @child: child widget already added to the text view
7377 * @xpos: new X position in window coordinates
7378 * @ypos: new Y position in window coordinates
7380 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7384 gtk_text_view_move_child (GtkTextView *text_view,
7389 GtkTextViewChild *vc;
7391 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7392 g_return_if_fail (GTK_IS_WIDGET (child));
7393 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7395 vc = g_object_get_data (G_OBJECT (child),
7396 "gtk-text-view-child");
7398 g_assert (vc != NULL);
7400 if (vc->x == xpos &&
7407 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7408 gtk_widget_queue_resize (child);
7412 /* Iterator operations */
7415 * gtk_text_view_forward_display_line:
7416 * @text_view: a #GtkTextView
7417 * @iter: a #GtkTextIter
7419 * Moves the given @iter forward by one display (wrapped) line. A
7420 * display line is different from a paragraph. Paragraphs are
7421 * separated by newlines or other paragraph separator characters.
7422 * Display lines are created by line-wrapping a paragraph. If
7423 * wrapping is turned off, display lines and paragraphs will be the
7424 * same. Display lines are divided differently for each view, since
7425 * they depend on the view's width; paragraphs are the same in all
7426 * views, since they depend on the contents of the #GtkTextBuffer.
7428 * Return value: %TRUE if @iter was moved and is not on the end iterator
7431 gtk_text_view_forward_display_line (GtkTextView *text_view,
7434 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7435 g_return_val_if_fail (iter != NULL, FALSE);
7437 gtk_text_view_ensure_layout (text_view);
7439 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7443 * gtk_text_view_backward_display_line:
7444 * @text_view: a #GtkTextView
7445 * @iter: a #GtkTextIter
7447 * Moves the given @iter backward by one display (wrapped) line. A
7448 * display line is different from a paragraph. Paragraphs are
7449 * separated by newlines or other paragraph separator characters.
7450 * Display lines are created by line-wrapping a paragraph. If
7451 * wrapping is turned off, display lines and paragraphs will be the
7452 * same. Display lines are divided differently for each view, since
7453 * they depend on the view's width; paragraphs are the same in all
7454 * views, since they depend on the contents of the #GtkTextBuffer.
7456 * Return value: %TRUE if @iter was moved and is not on the end iterator
7459 gtk_text_view_backward_display_line (GtkTextView *text_view,
7462 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7463 g_return_val_if_fail (iter != NULL, FALSE);
7465 gtk_text_view_ensure_layout (text_view);
7467 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7471 * gtk_text_view_forward_display_line_end:
7472 * @text_view: a #GtkTextView
7473 * @iter: a #GtkTextIter
7475 * Moves the given @iter forward to the next display line end. A
7476 * display line is different from a paragraph. Paragraphs are
7477 * separated by newlines or other paragraph separator characters.
7478 * Display lines are created by line-wrapping a paragraph. If
7479 * wrapping is turned off, display lines and paragraphs will be the
7480 * same. Display lines are divided differently for each view, since
7481 * they depend on the view's width; paragraphs are the same in all
7482 * views, since they depend on the contents of the #GtkTextBuffer.
7484 * Return value: %TRUE if @iter was moved and is not on the end iterator
7487 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7490 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7491 g_return_val_if_fail (iter != NULL, FALSE);
7493 gtk_text_view_ensure_layout (text_view);
7495 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7499 * gtk_text_view_backward_display_line_start:
7500 * @text_view: a #GtkTextView
7501 * @iter: a #GtkTextIter
7503 * Moves the given @iter backward to the next display line start. A
7504 * display line is different from a paragraph. Paragraphs are
7505 * separated by newlines or other paragraph separator characters.
7506 * Display lines are created by line-wrapping a paragraph. If
7507 * wrapping is turned off, display lines and paragraphs will be the
7508 * same. Display lines are divided differently for each view, since
7509 * they depend on the view's width; paragraphs are the same in all
7510 * views, since they depend on the contents of the #GtkTextBuffer.
7512 * Return value: %TRUE if @iter was moved and is not on the end iterator
7515 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7518 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7519 g_return_val_if_fail (iter != NULL, FALSE);
7521 gtk_text_view_ensure_layout (text_view);
7523 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7527 * gtk_text_view_starts_display_line:
7528 * @text_view: a #GtkTextView
7529 * @iter: a #GtkTextIter
7531 * Determines whether @iter is at the start of a display line.
7532 * See gtk_text_view_forward_display_line() for an explanation of
7533 * display lines vs. paragraphs.
7535 * Return value: %TRUE if @iter begins a wrapped line
7538 gtk_text_view_starts_display_line (GtkTextView *text_view,
7539 const GtkTextIter *iter)
7541 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7542 g_return_val_if_fail (iter != NULL, FALSE);
7544 gtk_text_view_ensure_layout (text_view);
7546 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7550 * gtk_text_view_move_visually:
7551 * @text_view: a #GtkTextView
7552 * @iter: a #GtkTextIter
7553 * @count: number of lines to move
7555 * Moves @iter up or down by @count display (wrapped) lines.
7556 * See gtk_text_view_forward_display_line() for an explanation of
7557 * display lines vs. paragraphs.
7559 * Return value: %TRUE if @iter moved and is not on the end iterator
7562 gtk_text_view_move_visually (GtkTextView *text_view,
7566 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7567 g_return_val_if_fail (iter != NULL, FALSE);
7569 gtk_text_view_ensure_layout (text_view);
7571 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);