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 { "text/plain", 0, TARGET_STRING },
410 { "STRING", 0, TARGET_STRING }
413 static GtkContainerClass *parent_class = NULL;
414 static guint signals[LAST_SIGNAL] = { 0 };
417 gtk_text_view_get_type (void)
419 static GtkType our_type = 0;
423 static const GtkTypeInfo our_info =
426 sizeof (GtkTextView),
427 sizeof (GtkTextViewClass),
428 (GtkClassInitFunc) gtk_text_view_class_init,
429 (GtkObjectInitFunc) gtk_text_view_init,
430 /* reserved_1 */ NULL,
431 /* reserved_2 */ NULL,
432 (GtkClassInitFunc) NULL
435 our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
442 add_move_binding (GtkBindingSet *binding_set,
445 GtkMovementStep step,
448 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
450 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
454 GTK_TYPE_BOOL, FALSE);
456 /* Selection-extending version */
457 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
461 GTK_TYPE_BOOL, TRUE);
465 gtk_text_view_class_init (GtkTextViewClass *klass)
467 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
468 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
469 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
470 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
471 GtkBindingSet *binding_set;
473 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
475 /* Default handlers and virtual methods
477 gobject_class->set_property = gtk_text_view_set_property;
478 gobject_class->get_property = gtk_text_view_get_property;
480 object_class->destroy = gtk_text_view_destroy;
481 gobject_class->finalize = gtk_text_view_finalize;
483 widget_class->realize = gtk_text_view_realize;
484 widget_class->unrealize = gtk_text_view_unrealize;
485 widget_class->style_set = gtk_text_view_style_set;
486 widget_class->direction_changed = gtk_text_view_direction_changed;
487 widget_class->size_request = gtk_text_view_size_request;
488 widget_class->size_allocate = gtk_text_view_size_allocate;
489 widget_class->event = gtk_text_view_event;
490 widget_class->key_press_event = gtk_text_view_key_press_event;
491 widget_class->key_release_event = gtk_text_view_key_release_event;
492 widget_class->button_press_event = gtk_text_view_button_press_event;
493 widget_class->button_release_event = gtk_text_view_button_release_event;
494 widget_class->focus_in_event = gtk_text_view_focus_in_event;
495 widget_class->focus_out_event = gtk_text_view_focus_out_event;
496 widget_class->motion_notify_event = gtk_text_view_motion_event;
497 widget_class->expose_event = gtk_text_view_expose_event;
498 widget_class->grab_focus = gtk_text_view_grab_focus;
499 widget_class->focus = gtk_text_view_focus;
501 widget_class->drag_begin = gtk_text_view_drag_begin;
502 widget_class->drag_end = gtk_text_view_drag_end;
503 widget_class->drag_data_get = gtk_text_view_drag_data_get;
504 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
506 widget_class->drag_leave = gtk_text_view_drag_leave;
507 widget_class->drag_motion = gtk_text_view_drag_motion;
508 widget_class->drag_drop = gtk_text_view_drag_drop;
509 widget_class->drag_data_received = gtk_text_view_drag_data_received;
511 widget_class->popup_menu = gtk_text_view_popup_menu;
513 container_class->add = gtk_text_view_add;
514 container_class->remove = gtk_text_view_remove;
515 container_class->forall = gtk_text_view_forall;
517 klass->move_cursor = gtk_text_view_move_cursor;
518 klass->page_horizontally = gtk_text_view_page_horizontally;
519 klass->set_anchor = gtk_text_view_set_anchor;
520 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
521 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
522 klass->cut_clipboard = gtk_text_view_cut_clipboard;
523 klass->copy_clipboard = gtk_text_view_copy_clipboard;
524 klass->paste_clipboard = gtk_text_view_paste_clipboard;
525 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
526 klass->move_focus = gtk_text_view_move_focus;
527 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
533 g_object_class_install_property (gobject_class,
534 PROP_PIXELS_ABOVE_LINES,
535 g_param_spec_int ("pixels_above_lines",
536 _("Pixels Above Lines"),
537 _("Pixels of blank space above paragraphs"),
543 g_object_class_install_property (gobject_class,
544 PROP_PIXELS_BELOW_LINES,
545 g_param_spec_int ("pixels_below_lines",
546 _("Pixels Below Lines"),
547 _("Pixels of blank space below paragraphs"),
553 g_object_class_install_property (gobject_class,
554 PROP_PIXELS_INSIDE_WRAP,
555 g_param_spec_int ("pixels_inside_wrap",
556 _("Pixels Inside Wrap"),
557 _("Pixels of blank space between wrapped lines in a paragraph"),
563 g_object_class_install_property (gobject_class,
565 g_param_spec_boolean ("editable",
567 _("Whether the text can be modified by the user"),
571 g_object_class_install_property (gobject_class,
573 g_param_spec_enum ("wrap_mode",
575 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
580 g_object_class_install_property (gobject_class,
582 g_param_spec_enum ("justification",
584 _("Left, right, or center justification"),
585 GTK_TYPE_JUSTIFICATION,
589 g_object_class_install_property (gobject_class,
591 g_param_spec_int ("left_margin",
593 _("Width of the left margin in pixels"),
599 g_object_class_install_property (gobject_class,
601 g_param_spec_int ("right_margin",
603 _("Width of the right margin in pixels"),
609 g_object_class_install_property (gobject_class,
611 g_param_spec_int ("indent",
613 _("Amount to indent the paragraph, in pixels"),
619 g_object_class_install_property (gobject_class,
621 g_param_spec_boxed ("tabs",
623 _("Custom tabs for this text"),
624 PANGO_TYPE_TAB_ARRAY,
627 g_object_class_install_property (gobject_class,
629 g_param_spec_boolean ("cursor_visible",
631 _("If the insertion cursor is shown"),
640 gtk_widget_class_install_style_property (widget_class,
641 g_param_spec_boxed ("cursor_color",
643 _("Color with which to draw insertion cursor"),
652 signals[MOVE_CURSOR] =
653 gtk_signal_new ("move_cursor",
654 GTK_RUN_LAST | GTK_RUN_ACTION,
655 GTK_CLASS_TYPE (object_class),
656 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
657 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
658 GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
660 signals[PAGE_HORIZONTALLY] =
661 gtk_signal_new ("page_horizontally",
662 GTK_RUN_LAST | GTK_RUN_ACTION,
663 GTK_CLASS_TYPE (object_class),
664 GTK_SIGNAL_OFFSET (GtkTextViewClass, page_horizontally),
665 _gtk_marshal_VOID__INT_BOOLEAN,
666 GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_BOOL);
668 signals[SET_ANCHOR] =
669 gtk_signal_new ("set_anchor",
670 GTK_RUN_LAST | GTK_RUN_ACTION,
671 GTK_CLASS_TYPE (object_class),
672 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
673 _gtk_marshal_VOID__VOID,
676 signals[INSERT_AT_CURSOR] =
677 gtk_signal_new ("insert_at_cursor",
678 GTK_RUN_LAST | GTK_RUN_ACTION,
679 GTK_CLASS_TYPE (object_class),
680 GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
681 _gtk_marshal_VOID__STRING,
682 GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
684 signals[DELETE_FROM_CURSOR] =
685 gtk_signal_new ("delete_from_cursor",
686 GTK_RUN_LAST | GTK_RUN_ACTION,
687 GTK_CLASS_TYPE (object_class),
688 GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
689 _gtk_marshal_VOID__ENUM_INT,
690 GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
692 signals[CUT_CLIPBOARD] =
693 gtk_signal_new ("cut_clipboard",
694 GTK_RUN_LAST | GTK_RUN_ACTION,
695 GTK_CLASS_TYPE (object_class),
696 GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
697 _gtk_marshal_VOID__VOID,
700 signals[COPY_CLIPBOARD] =
701 gtk_signal_new ("copy_clipboard",
702 GTK_RUN_LAST | GTK_RUN_ACTION,
703 GTK_CLASS_TYPE (object_class),
704 GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
705 _gtk_marshal_VOID__VOID,
708 signals[PASTE_CLIPBOARD] =
709 gtk_signal_new ("paste_clipboard",
710 GTK_RUN_LAST | GTK_RUN_ACTION,
711 GTK_CLASS_TYPE (object_class),
712 GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
713 _gtk_marshal_VOID__VOID,
716 signals[TOGGLE_OVERWRITE] =
717 gtk_signal_new ("toggle_overwrite",
718 GTK_RUN_LAST | GTK_RUN_ACTION,
719 GTK_CLASS_TYPE (object_class),
720 GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
721 _gtk_marshal_VOID__VOID,
724 signals[MOVE_FOCUS] =
725 gtk_signal_new ("move_focus",
726 GTK_RUN_LAST | GTK_RUN_ACTION,
727 GTK_CLASS_TYPE (object_class),
728 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_focus),
729 _gtk_marshal_VOID__ENUM,
730 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
732 signals[SET_SCROLL_ADJUSTMENTS] =
733 gtk_signal_new ("set_scroll_adjustments",
735 GTK_CLASS_TYPE (object_class),
736 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
737 _gtk_marshal_VOID__OBJECT_OBJECT,
738 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
739 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
741 signals[POPULATE_POPUP] =
742 gtk_signal_new ("populate_popup",
744 GTK_CLASS_TYPE (object_class),
745 GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
746 _gtk_marshal_VOID__OBJECT,
747 GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
753 binding_set = gtk_binding_set_by_class (klass);
755 /* Moving the insertion point */
756 add_move_binding (binding_set, GDK_Right, 0,
757 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
759 add_move_binding (binding_set, GDK_KP_Right, 0,
760 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
762 add_move_binding (binding_set, GDK_Left, 0,
763 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
765 add_move_binding (binding_set, GDK_KP_Left, 0,
766 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
768 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
769 GTK_MOVEMENT_WORDS, 1);
771 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
772 GTK_MOVEMENT_WORDS, 1);
774 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
775 GTK_MOVEMENT_WORDS, -1);
777 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
778 GTK_MOVEMENT_WORDS, 1);
780 add_move_binding (binding_set, GDK_Up, 0,
781 GTK_MOVEMENT_DISPLAY_LINES, -1);
783 add_move_binding (binding_set, GDK_KP_Up, 0,
784 GTK_MOVEMENT_DISPLAY_LINES, -1);
786 add_move_binding (binding_set, GDK_Down, 0,
787 GTK_MOVEMENT_DISPLAY_LINES, 1);
789 add_move_binding (binding_set, GDK_KP_Down, 0,
790 GTK_MOVEMENT_DISPLAY_LINES, 1);
792 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
793 GTK_MOVEMENT_PARAGRAPHS, -1);
795 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
796 GTK_MOVEMENT_PARAGRAPHS, -1);
798 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
799 GTK_MOVEMENT_PARAGRAPHS, 1);
801 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
802 GTK_MOVEMENT_PARAGRAPHS, 1);
804 add_move_binding (binding_set, GDK_Home, 0,
805 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
807 add_move_binding (binding_set, GDK_KP_Home, 0,
808 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
810 add_move_binding (binding_set, GDK_End, 0,
811 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
813 add_move_binding (binding_set, GDK_KP_End, 0,
814 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
816 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
817 GTK_MOVEMENT_BUFFER_ENDS, -1);
819 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
820 GTK_MOVEMENT_BUFFER_ENDS, -1);
822 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
823 GTK_MOVEMENT_BUFFER_ENDS, 1);
825 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
826 GTK_MOVEMENT_BUFFER_ENDS, 1);
828 add_move_binding (binding_set, GDK_Page_Up, 0,
829 GTK_MOVEMENT_PAGES, -1);
831 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
832 GTK_MOVEMENT_PAGES, -1);
834 add_move_binding (binding_set, GDK_Page_Down, 0,
835 GTK_MOVEMENT_PAGES, 1);
837 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
838 GTK_MOVEMENT_PAGES, 1);
842 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
844 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
846 GTK_TYPE_BOOL, FALSE);
847 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
849 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
851 GTK_TYPE_BOOL, TRUE);
854 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
855 "page_horizontally", 2,
857 GTK_TYPE_BOOL, FALSE);
859 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
860 "page_horizontally", 2,
862 GTK_TYPE_BOOL, TRUE);
864 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
865 "page_horizontally", 2,
867 GTK_TYPE_BOOL, FALSE);
869 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
870 "page_horizontally", 2,
872 GTK_TYPE_BOOL, TRUE);
874 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
875 "page_horizontally", 2,
877 GTK_TYPE_BOOL, FALSE);
879 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
880 "page_horizontally", 2,
882 GTK_TYPE_BOOL, TRUE);
884 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
885 "page_horizontally", 2,
887 GTK_TYPE_BOOL, FALSE);
889 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
890 "page_horizontally", 2,
892 GTK_TYPE_BOOL, TRUE);
895 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
896 "delete_from_cursor", 2,
897 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
900 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
901 "delete_from_cursor", 2,
902 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
905 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
906 "delete_from_cursor", 2,
907 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
910 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
911 "delete_from_cursor", 2,
912 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
915 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
916 "delete_from_cursor", 2,
917 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
920 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
921 "delete_from_cursor", 2,
922 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
927 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
930 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
931 "copy_clipboard", 0);
933 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
934 "paste_clipboard", 0);
937 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
938 "toggle_overwrite", 0);
939 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
940 "toggle_overwrite", 0);
942 /* Control-tab focus motion */
943 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
945 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
946 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
948 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
949 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, GDK_CONTROL_MASK,
951 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
953 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
955 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
956 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
958 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
959 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
961 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
965 gtk_text_view_init (GtkTextView *text_view)
969 widget = GTK_WIDGET (text_view);
971 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
973 /* Set up default style */
974 text_view->wrap_mode = GTK_WRAP_NONE;
975 text_view->pixels_above_lines = 0;
976 text_view->pixels_below_lines = 0;
977 text_view->pixels_inside_wrap = 0;
978 text_view->justify = GTK_JUSTIFY_LEFT;
979 text_view->left_margin = 0;
980 text_view->right_margin = 0;
981 text_view->indent = 0;
982 text_view->tabs = NULL;
983 text_view->editable = TRUE;
985 gtk_drag_dest_set (widget,
987 target_table, G_N_ELEMENTS (target_table),
988 GDK_ACTION_COPY | GDK_ACTION_MOVE);
990 text_view->virtual_cursor_x = -1;
991 text_view->virtual_cursor_y = -1;
993 /* This object is completely private. No external entity can gain a reference
994 * to it; so we create it here and destroy it in finalize ().
996 text_view->im_context = gtk_im_multicontext_new ();
998 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
999 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1000 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
1001 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1002 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
1003 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1004 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
1005 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1007 text_view->cursor_visible = TRUE;
1009 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1012 text_view->drag_start_x = -1;
1013 text_view->drag_start_y = -1;
1015 text_view->pending_place_cursor_button = 0;
1017 /* We handle all our own redrawing */
1018 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1022 * gtk_text_view_new:
1024 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1025 * before using the text view, an empty default buffer will be created
1026 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1027 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1029 * Return value: a new #GtkTextView
1032 gtk_text_view_new (void)
1034 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1038 * gtk_text_view_new_with_buffer:
1039 * @buffer: a #GtkTextBuffer
1041 * Creates a new #GtkTextView widget displaying the buffer
1042 * @buffer. One buffer can be shared among many widgets.
1043 * @buffer may be NULL to create a default buffer, in which case
1044 * this function is equivalent to gtk_text_view_new(). The
1045 * text view adds its own reference count to the buffer; it does not
1046 * take over an existing reference.
1048 * Return value: a new #GtkTextView.
1051 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1053 GtkTextView *text_view;
1055 text_view = (GtkTextView*)gtk_text_view_new ();
1057 gtk_text_view_set_buffer (text_view, buffer);
1059 return GTK_WIDGET (text_view);
1063 * gtk_text_view_set_buffer:
1064 * @text_view: a #GtkTextView
1065 * @buffer: a #GtkTextBuffer
1067 * Sets @buffer as the buffer being displayed by @text_view. The previous
1068 * buffer displayed by the text view is unreferenced, and a reference is
1069 * added to @buffer. If you owned a reference to @buffer before passing it
1070 * to this function, you must remove that reference yourself; #GtkTextView
1071 * will not "adopt" it.
1075 gtk_text_view_set_buffer (GtkTextView *text_view,
1076 GtkTextBuffer *buffer)
1078 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1079 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1081 if (text_view->buffer == buffer)
1084 if (text_view->buffer != NULL)
1086 /* Destroy all anchored children */
1090 copy = g_slist_copy (text_view->children);
1092 while (tmp_list != NULL)
1094 GtkTextViewChild *vc = tmp_list->data;
1098 gtk_widget_destroy (vc->widget);
1099 /* vc may now be invalid! */
1102 tmp_list = g_slist_next (tmp_list);
1105 g_slist_free (copy);
1107 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1108 gtk_text_view_mark_set_handler, text_view);
1109 g_object_unref (G_OBJECT (text_view->buffer));
1110 text_view->dnd_mark = NULL;
1112 if (GTK_WIDGET_REALIZED (text_view))
1113 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
1114 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1117 text_view->buffer = buffer;
1123 g_object_ref (G_OBJECT (buffer));
1125 if (text_view->layout)
1126 gtk_text_layout_set_buffer (text_view->layout, buffer);
1128 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1130 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1134 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1138 text_view->first_para_pixels = 0;
1140 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1141 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1143 if (GTK_WIDGET_REALIZED (text_view))
1144 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
1145 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1148 if (GTK_WIDGET_VISIBLE (text_view))
1149 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1151 DV(g_print ("Invalidating due to set_buffer\n"));
1152 gtk_text_view_invalidate (text_view);
1155 static GtkTextBuffer*
1156 get_buffer (GtkTextView *text_view)
1158 if (text_view->buffer == NULL)
1161 b = gtk_text_buffer_new (NULL);
1162 gtk_text_view_set_buffer (text_view, b);
1163 g_object_unref (G_OBJECT (b));
1166 return text_view->buffer;
1170 * gtk_text_view_get_buffer:
1171 * @text_view: a #GtkTextView
1173 * Returns the #GtkTextBuffer being displayed by this text view.
1174 * The reference count on the buffer is not incremented; the caller
1175 * of this function won't own a new reference.
1177 * Return value: a #GtkTextBuffer
1180 gtk_text_view_get_buffer (GtkTextView *text_view)
1182 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1184 return get_buffer (text_view);
1188 * gtk_text_view_get_iter_at_location:
1189 * @text_view: a #GtkTextView
1190 * @iter: a #GtkTextIter
1191 * @x: x position, in buffer coordinates
1192 * @y: y position, in buffer coordinates
1194 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1195 * coordinates are coordinates for the entire buffer, not just the
1196 * currently-displayed portion. If you have coordinates from an
1197 * event, you have to convert those to buffer coordinates with
1198 * gtk_text_view_window_to_buffer_coords().
1202 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1207 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1208 g_return_if_fail (iter != NULL);
1209 g_return_if_fail (text_view->layout != NULL);
1211 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1218 * gtk_text_view_get_iter_location:
1219 * @text_view: a #GtkTextView
1220 * @iter: a #GtkTextIter
1221 * @location: bounds of the character at @iter
1223 * Gets a rectangle which roughly contains the character at @iter.
1224 * The rectangle position is in buffer coordinates; use
1225 * gtk_text_view_buffer_to_window_coords() to convert these
1226 * coordinates to coordinates for one of the windows in the text view.
1230 gtk_text_view_get_iter_location (GtkTextView *text_view,
1231 const GtkTextIter *iter,
1232 GdkRectangle *location)
1234 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1235 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1237 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1241 * gtk_text_view_get_line_yrange:
1242 * @text_view: a #GtkTextView
1243 * @iter: a #GtkTextIter
1244 * @y: return location for a y coordinate
1245 * @height: return location for a height
1247 * Gets the y coordinate of the top of the line containing @iter,
1248 * and the height of the line. The coordinate is a buffer coordinate;
1249 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1253 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1254 const GtkTextIter *iter,
1258 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1259 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1261 gtk_text_layout_get_line_yrange (text_view->layout,
1268 * gtk_text_view_get_line_at_y:
1269 * @text_view: a #GtkTextView
1270 * @target_iter: a #GtkTextIter
1271 * @y: a y coordinate
1272 * @line_top: return location for top coordinate of the line
1274 * Gets the #GtkTextIter at the start of the line containing
1275 * the coordinate @y. @y is in buffer coordinates, convert from
1276 * window coordinates with gtk_text_view_window_to_buffer_coords().
1277 * If non-%NULL, @line_top will be filled with the coordinate of the top
1281 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1282 GtkTextIter *target_iter,
1286 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1288 gtk_text_layout_get_line_at_y (text_view->layout,
1295 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1297 DV (g_print (" Setting adj to raw value %g\n", val));
1299 /* We don't really want to clamp to upper; we want to clamp to
1300 upper - page_size which is the highest value the scrollbar
1301 will let us reach. */
1302 if (val > (adj->upper - adj->page_size))
1303 val = adj->upper - adj->page_size;
1305 if (val < adj->lower)
1308 if (val != adj->value)
1310 DV (g_print (" Setting adj to clamped value %g\n", val));
1311 gtk_adjustment_set_value (adj, val);
1319 * gtk_text_view_scroll_to_iter:
1320 * @text_view: a #GtkTextView
1321 * @iter: a #GtkTextIter
1322 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1323 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1324 * @xalign: horizontal alignment of mark within visible area.
1325 * @yalign: vertical alignment of mark within visible area
1327 * Scrolls @text_view so that @iter is on the screen in the position
1328 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1329 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1330 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1331 * possibly not scrolling at all. The effective screen for purposes
1332 * of this function is reduced by a margin of size @within_margin.
1333 * NOTE: This function uses the currently-computed height of the
1334 * lines in the text buffer. Note that line heights are computed
1335 * in an idle handler; so this function may not have the desired effect
1336 * if it's called before the height computations. To avoid oddness,
1337 * consider using gtk_text_view_scroll_to_mark() which saves a point
1338 * to be scrolled to after line validation.
1340 * Return value: %TRUE if scrolling occurred
1343 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1345 gdouble within_margin,
1351 GdkRectangle screen;
1356 gboolean retval = FALSE;
1358 gint screen_xoffset, screen_yoffset;
1359 gint current_x_scroll, current_y_scroll;
1361 /* FIXME why don't we do the validate-at-scroll-destination thing
1362 * from flush_scroll in this function? I think it wasn't done before
1363 * because changed_handler was screwed up, but I could be wrong.
1366 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1367 g_return_val_if_fail (iter != NULL, FALSE);
1368 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1369 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1370 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1372 widget = GTK_WIDGET (text_view);
1374 DV(g_print(G_STRLOC"\n"));
1376 gtk_text_layout_get_iter_location (text_view->layout,
1380 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1382 current_x_scroll = text_view->xoffset;
1383 current_y_scroll = text_view->yoffset;
1385 screen.x = current_x_scroll;
1386 screen.y = current_y_scroll;
1387 screen.width = SCREEN_WIDTH (widget);
1388 screen.height = SCREEN_HEIGHT (widget);
1390 screen_xoffset = screen.width * within_margin;
1391 screen_yoffset = screen.height * within_margin;
1393 screen.x += screen_xoffset;
1394 screen.y += screen_yoffset;
1395 screen.width -= screen_xoffset * 2;
1396 screen.height -= screen_yoffset * 2;
1398 /* paranoia check */
1399 if (screen.width < 1)
1401 if (screen.height < 1)
1404 screen_right = screen.x + screen.width;
1405 screen_bottom = screen.y + screen.height;
1407 /* The alignment affects the point in the target character that we
1408 * choose to align. If we're doing right/bottom alignment, we align
1409 * the right/bottom edge of the character the mark is at; if we're
1410 * doing left/top we align the left/top edge of the character; if
1411 * we're doing center alignment we align the center of the
1415 /* Vertical scroll */
1418 scroll_dest = current_y_scroll;
1422 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1424 /* if scroll_dest < screen.y, we move a negative increment (up),
1425 * else a positive increment (down)
1427 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1431 /* move minimum to get onscreen */
1432 if (rect.y < screen.y)
1434 scroll_dest = rect.y;
1435 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1437 else if ((rect.y + rect.height) > screen_bottom)
1439 scroll_dest = rect.y + rect.height;
1440 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1444 if (scroll_inc != 0)
1446 retval = set_adjustment_clamped (get_vadjustment (text_view),
1447 current_y_scroll + scroll_inc);
1449 DV (g_print (" vert increment %d\n", scroll_inc));
1452 /* Horizontal scroll */
1455 scroll_dest = current_x_scroll;
1459 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1461 /* if scroll_dest < screen.y, we move a negative increment (left),
1462 * else a positive increment (right)
1464 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1468 /* move minimum to get onscreen */
1469 if (rect.x < screen.x)
1471 scroll_dest = rect.x;
1472 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1474 else if ((rect.x + rect.width) > screen_right)
1476 scroll_dest = rect.x + rect.width;
1477 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1481 if (scroll_inc != 0)
1483 retval = set_adjustment_clamped (get_hadjustment (text_view),
1484 current_x_scroll + scroll_inc);
1486 DV (g_print (" horiz increment %d\n", scroll_inc));
1490 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1492 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1498 free_pending_scroll (GtkTextPendingScroll *scroll)
1500 if (!gtk_text_mark_get_deleted (scroll->mark))
1501 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1503 g_object_unref (G_OBJECT (scroll->mark));
1508 gtk_text_view_queue_scroll (GtkTextView *text_view,
1510 gdouble within_margin,
1516 GtkTextPendingScroll *scroll;
1518 DV(g_print(G_STRLOC"\n"));
1520 scroll = g_new (GtkTextPendingScroll, 1);
1522 scroll->within_margin = within_margin;
1523 scroll->use_align = use_align;
1524 scroll->xalign = xalign;
1525 scroll->yalign = yalign;
1527 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1529 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1532 gtk_text_mark_get_left_gravity (mark));
1534 g_object_ref (G_OBJECT (scroll->mark));
1536 if (text_view->pending_scroll)
1537 free_pending_scroll (text_view->pending_scroll);
1539 text_view->pending_scroll = scroll;
1543 gtk_text_view_flush_scroll (GtkTextView *text_view)
1546 GtkTextPendingScroll *scroll;
1550 widget = GTK_WIDGET (text_view);
1552 DV(g_print(G_STRLOC"\n"));
1554 if (text_view->pending_scroll == NULL)
1556 DV (g_print ("in flush scroll, no pending scroll\n"));
1560 scroll = text_view->pending_scroll;
1562 /* avoid recursion */
1563 text_view->pending_scroll = NULL;
1565 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1567 /* Validate area around the scroll destination, so the adjustment
1568 * can meaningfully point into that area. We must validate
1569 * enough area to be sure that after we scroll, everything onscreen
1570 * is valid; otherwise, validation will maintain the first para
1571 * in one place, but may push the target iter off the bottom of
1574 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1575 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1576 - (widget->allocation.height * 2),
1577 widget->allocation.height * 2);
1579 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1581 /* Ensure we have updated width/height */
1582 gtk_text_view_update_adjustments (text_view);
1584 retval = gtk_text_view_scroll_to_iter (text_view,
1586 scroll->within_margin,
1591 free_pending_scroll (scroll);
1597 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1599 if (upper != adj->upper)
1601 gdouble min = MAX (0.0, upper - adj->page_size);
1602 gboolean value_changed = FALSE;
1606 if (adj->value > min)
1609 value_changed = TRUE;
1612 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1613 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1617 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1618 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1624 gtk_text_view_update_adjustments (GtkTextView *text_view)
1626 gint width = 0, height = 0;
1628 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1630 if (text_view->layout)
1631 gtk_text_layout_get_size (text_view->layout, &width, &height);
1633 if (text_view->width != width || text_view->height != height)
1635 text_view->width = width;
1636 text_view->height = height;
1638 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1639 MAX (SCREEN_WIDTH (text_view), width));
1640 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1641 MAX (SCREEN_HEIGHT (text_view), height));
1643 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1645 /* Set up the step sizes; we'll say that a page is
1646 our allocation minus one step, and a step is
1647 1/10 of our allocation. */
1648 text_view->hadjustment->step_increment =
1649 SCREEN_WIDTH (text_view) / 10.0;
1650 text_view->hadjustment->page_increment =
1651 SCREEN_WIDTH (text_view) * 0.9;
1653 text_view->vadjustment->step_increment =
1654 SCREEN_HEIGHT (text_view) / 10.0;
1655 text_view->vadjustment->page_increment =
1656 SCREEN_HEIGHT (text_view) * 0.9;
1658 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1659 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1664 gtk_text_view_update_layout_width (GtkTextView *text_view)
1666 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1668 gtk_text_view_ensure_layout (text_view);
1670 gtk_text_layout_set_screen_width (text_view->layout,
1671 SCREEN_WIDTH (text_view));
1675 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1678 gint cursor_x_pos, cursor_y_pos;
1680 if (text_view->layout == NULL)
1683 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1685 area.x = cursor_x_pos;
1686 area.y = cursor_y_pos;
1687 area.width = area.height = 0;
1689 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1693 * gtk_text_view_scroll_to_mark:
1694 * @text_view: a #GtkTextView
1695 * @mark: a #GtkTextMark
1696 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1697 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1698 * @xalign: horizontal alignment of mark within visible area.
1699 * @yalign: vertical alignment of mark within visible area
1701 * Scrolls @text_view so that @mark is on the screen in the position
1702 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1703 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1704 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1705 * possibly not scrolling at all. The effective screen for purposes
1706 * of this function is reduced by a margin of size @within_margin.
1710 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1712 gdouble within_margin,
1717 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1718 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1719 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1720 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1721 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1723 gtk_text_view_queue_scroll (text_view, mark,
1729 /* If no validation is pending, we need to go ahead and force an
1732 if (text_view->layout &&
1733 gtk_text_layout_is_valid (text_view->layout))
1734 gtk_text_view_flush_scroll (text_view);
1738 * gtk_text_view_scroll_mark_onscreen:
1739 * @text_view: a #GtkTextView
1740 * @mark: a mark in the buffer for @text_view
1742 * Scrolls @text_view the minimum distance such that @mark is contained
1743 * within the visible area of the widget.
1747 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1750 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1751 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1753 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1757 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1759 GdkRectangle visible_rect;
1760 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1762 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1764 visible_rect.y + visible_rect.height);
1768 * gtk_text_view_move_mark_onscreen:
1769 * @text_view: a #GtkTextView
1770 * @mark: a #GtkTextMark
1772 * Moves a mark within the buffer so that it's
1773 * located within the currently-visible text area.
1775 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1778 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1783 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1784 g_return_val_if_fail (mark != NULL, FALSE);
1786 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1788 if (clamp_iter_onscreen (text_view, &iter))
1790 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1798 * gtk_text_view_get_visible_rect:
1799 * @text_view: a #GtkTextView
1800 * @visible_rect: rectangle to fill
1802 * Fills @visible_rect with the currently-visible
1803 * region of the buffer, in buffer coordinates. Convert to window coordinates
1804 * with gtk_text_view_buffer_to_window_coords().
1807 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1808 GdkRectangle *visible_rect)
1812 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1814 widget = GTK_WIDGET (text_view);
1818 visible_rect->x = text_view->xoffset;
1819 visible_rect->y = text_view->yoffset;
1820 visible_rect->width = SCREEN_WIDTH (widget);
1821 visible_rect->height = SCREEN_HEIGHT (widget);
1823 DV(g_print(" visible rect: %d,%d %d x %d\n",
1826 visible_rect->width,
1827 visible_rect->height));
1832 * gtk_text_view_set_wrap_mode:
1833 * @text_view: a #GtkTextView
1834 * @wrap_mode: a #GtkWrapMode
1836 * Sets the line wrapping for the view.
1839 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1840 GtkWrapMode wrap_mode)
1842 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1844 if (text_view->wrap_mode != wrap_mode)
1846 text_view->wrap_mode = wrap_mode;
1848 if (text_view->layout)
1850 text_view->layout->default_style->wrap_mode = wrap_mode;
1851 gtk_text_layout_default_style_changed (text_view->layout);
1855 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1859 * gtk_text_view_get_wrap_mode:
1860 * @text_view: a #GtkTextView
1862 * Gets the line wrapping for the view.
1864 * Return value: the line wrap setting
1867 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1869 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1871 return text_view->wrap_mode;
1875 * gtk_text_view_set_editable:
1876 * @text_view: a #GtkTextView
1877 * @setting: whether it's editable
1879 * Sets the default editability of the #GtkTextView. You can override
1880 * this default setting with tags in the buffer, using the "editable"
1881 * attribute of tags.
1884 gtk_text_view_set_editable (GtkTextView *text_view,
1887 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1888 setting = setting != FALSE;
1890 if (text_view->editable != setting)
1892 text_view->editable = setting;
1894 if (text_view->layout)
1896 text_view->layout->default_style->editable = text_view->editable;
1897 gtk_text_layout_default_style_changed (text_view->layout);
1900 g_object_notify (G_OBJECT (text_view), "editable");
1905 * gtk_text_view_get_editable:
1906 * @text_view: a #GtkTextView
1908 * Returns the default editability of the #GtkTextView. Tags in the
1909 * buffer may override this setting for some ranges of text.
1911 * Return value: whether text is editable by default
1914 gtk_text_view_get_editable (GtkTextView *text_view)
1916 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1918 return text_view->editable;
1922 * gtk_text_view_set_pixels_above_lines:
1923 * @text_view: a #GtkTextView
1924 * @pixels_above_lines: pixels above paragraphs
1926 * Sets the default number of blank pixels above paragraphs in @text_view.
1927 * Tags in the buffer for @text_view may override the defaults.
1931 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1932 gint pixels_above_lines)
1934 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1936 if (text_view->pixels_above_lines != pixels_above_lines)
1938 text_view->pixels_above_lines = pixels_above_lines;
1940 if (text_view->layout)
1942 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1943 gtk_text_layout_default_style_changed (text_view->layout);
1946 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1951 * gtk_text_view_get_pixels_above_lines:
1952 * @text_view: a #GtkTextView
1954 * Gets the default number of pixels to put above paragraphs.
1956 * Return value: default number of pixels above paragraphs
1959 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1961 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1963 return text_view->pixels_above_lines;
1967 * gtk_text_view_set_pixels_below_lines:
1968 * @text_view: a #GtkTextView
1969 * @pixels_below_lines: pixels below paragraphs
1971 * Sets the default number of pixels of blank space
1972 * to put below paragraphs in @text_view. May be overridden
1973 * by tags applied to @text_view's buffer.
1977 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1978 gint pixels_below_lines)
1980 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1982 if (text_view->pixels_below_lines != pixels_below_lines)
1984 text_view->pixels_below_lines = pixels_below_lines;
1986 if (text_view->layout)
1988 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
1989 gtk_text_layout_default_style_changed (text_view->layout);
1992 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
1997 * gtk_text_view_get_pixels_below_lines:
1998 * @text_view: a #GtkTextView
2000 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2002 * Return value: default number of blank pixels below paragraphs
2005 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2007 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2009 return text_view->pixels_below_lines;
2013 * gtk_text_view_set_pixels_inside_wrap:
2014 * @text_view: a #GtkTextView
2015 * @pixels_inside_wrap: default number of pixels between wrapped lines
2017 * Sets the default number of pixels of blank space to leave between
2018 * display/wrapped lines within a paragraph. May be overridden by
2019 * tags in @text_view's buffer.
2023 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2024 gint pixels_inside_wrap)
2026 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2028 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2030 text_view->pixels_inside_wrap = pixels_inside_wrap;
2032 if (text_view->layout)
2034 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2035 gtk_text_layout_default_style_changed (text_view->layout);
2038 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2043 * gtk_text_view_get_pixels_inside_wrap:
2044 * @text_view: a #GtkTextView
2046 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2048 * Return value: default number of pixels of blank space between wrapped lines
2051 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2053 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2055 return text_view->pixels_inside_wrap;
2059 * gtk_text_view_set_justification:
2060 * @text_view: a #GtkTextView
2061 * @justification: justification
2063 * Sets the default justification of text in @text_view.
2064 * Tags in the view's buffer may override the default.
2068 gtk_text_view_set_justification (GtkTextView *text_view,
2069 GtkJustification justification)
2071 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2073 if (text_view->justify != justification)
2075 text_view->justify = justification;
2077 if (text_view->layout)
2079 text_view->layout->default_style->justification = justification;
2080 gtk_text_layout_default_style_changed (text_view->layout);
2083 g_object_notify (G_OBJECT (text_view), "justification");
2088 * gtk_text_view_get_justification:
2089 * @text_view: a #GtkTextView
2091 * Gets the default justification of paragraphs in @text_view.
2092 * Tags in the buffer may override the default.
2094 * Return value: default justification
2097 gtk_text_view_get_justification (GtkTextView *text_view)
2099 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2101 return text_view->justify;
2105 * gtk_text_view_set_left_margin:
2106 * @text_view: a #GtkTextView
2107 * @left_margin: left margin in pixels
2109 * Sets the default left margin for text in @text_view.
2110 * Tags in the buffer may override the default.
2114 gtk_text_view_set_left_margin (GtkTextView *text_view,
2117 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2119 if (text_view->left_margin != left_margin)
2121 text_view->left_margin = left_margin;
2123 if (text_view->layout)
2125 text_view->layout->default_style->left_margin = left_margin;
2126 gtk_text_layout_default_style_changed (text_view->layout);
2129 g_object_notify (G_OBJECT (text_view), "left_margin");
2134 * gtk_text_view_get_left_margin:
2135 * @text_view: a #GtkTextView
2137 * Gets the default left margin size of paragraphs in the @text_view.
2138 * Tags in the buffer may override the default.
2140 * Return value: left margin in pixels
2143 gtk_text_view_get_left_margin (GtkTextView *text_view)
2145 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2147 return text_view->left_margin;
2151 * gtk_text_view_set_right_margin:
2152 * @text_view: a #GtkTextView
2153 * @right_margin: right margin in pixels
2155 * Sets the default right margin for text in the text view.
2156 * Tags in the buffer may override the default.
2160 gtk_text_view_set_right_margin (GtkTextView *text_view,
2163 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2165 if (text_view->right_margin != right_margin)
2167 text_view->right_margin = right_margin;
2169 if (text_view->layout)
2171 text_view->layout->default_style->right_margin = right_margin;
2172 gtk_text_layout_default_style_changed (text_view->layout);
2175 g_object_notify (G_OBJECT (text_view), "right_margin");
2180 * gtk_text_view_get_right_margin:
2181 * @text_view: a #GtkTextView
2183 * Gets the default right margin for text in @text_view. Tags
2184 * in the buffer may override the default.
2186 * Return value: right margin in pixels
2189 gtk_text_view_get_right_margin (GtkTextView *text_view)
2191 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2193 return text_view->right_margin;
2197 * gtk_text_view_set_indent:
2198 * @text_view: a #GtkTextView
2199 * @indent: indentation in pixels
2201 * Sets the default indentation for paragraphs in @text_view.
2202 * Tags in the buffer may override the default.
2206 gtk_text_view_set_indent (GtkTextView *text_view,
2209 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2211 if (text_view->indent != indent)
2213 text_view->indent = indent;
2215 if (text_view->layout)
2217 text_view->layout->default_style->indent = indent;
2218 gtk_text_layout_default_style_changed (text_view->layout);
2221 g_object_notify (G_OBJECT (text_view), "indent");
2226 * gtk_text_view_get_indent:
2227 * @text_view: a #GtkTextView
2229 * Gets the default indentation of paragraphs in @text_view.
2230 * Tags in the view's buffer may override the default.
2231 * The indentation may be negative.
2233 * Return value: number of pixels of indentation
2236 gtk_text_view_get_indent (GtkTextView *text_view)
2238 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2240 return text_view->indent;
2244 * gtk_text_view_set_tabs:
2245 * @text_view: a #GtkTextView
2246 * @tabs: tabs as a #PangoTabArray
2248 * Sets the default tab stops for paragraphs in @text_view.
2249 * Tags in the buffer may override the default.
2253 gtk_text_view_set_tabs (GtkTextView *text_view,
2254 PangoTabArray *tabs)
2256 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2258 if (text_view->tabs)
2259 pango_tab_array_free (text_view->tabs);
2261 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2263 if (text_view->layout)
2265 /* some unkosher futzing in internal struct details... */
2266 if (text_view->layout->default_style->tabs)
2267 pango_tab_array_free (text_view->layout->default_style->tabs);
2269 text_view->layout->default_style->tabs =
2270 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2272 gtk_text_layout_default_style_changed (text_view->layout);
2275 g_object_notify (G_OBJECT (text_view), "tabs");
2279 * gtk_text_view_get_tabs:
2280 * @text_view: a #GtkTextView
2282 * Gets the default tabs for @text_view. Tags in the buffer may
2283 * override the defaults. The returned array will be %NULL if
2284 * "standard" (8-space) tabs are used. Free the return value
2285 * with pango_tab_array_free().
2287 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2290 gtk_text_view_get_tabs (GtkTextView *text_view)
2292 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2294 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2298 * gtk_text_view_set_cursor_visible:
2299 * @text_view: a #GtkTextView
2300 * @setting: whether to show the insertion cursor
2302 * Toggles whether the insertion point is displayed. A buffer with no editable
2303 * text probably shouldn't have a visible cursor, so you may want to turn
2307 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2310 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2312 setting = (setting != FALSE);
2314 if (text_view->cursor_visible != setting)
2316 text_view->cursor_visible = setting;
2318 if (GTK_WIDGET_HAS_FOCUS (text_view))
2320 if (text_view->layout)
2322 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2323 gtk_text_view_check_cursor_blink (text_view);
2327 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2332 * gtk_text_view_get_cursor_visible:
2333 * @text_view: a #GtkTextView
2335 * Find out whether the cursor is being displayed.
2337 * Return value: whether the insertion mark is visible
2340 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2342 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2344 return text_view->cursor_visible;
2349 * gtk_text_view_place_cursor_onscreen:
2350 * @text_view: a #GtkTextView
2352 * Moves the cursor to the currently visible region of the
2353 * buffer, it it isn't there already.
2355 * Return value: TRUE if the cursor had to be moved.
2358 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2362 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2364 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2365 gtk_text_buffer_get_mark (get_buffer (text_view),
2368 if (clamp_iter_onscreen (text_view, &insert))
2370 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2378 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2380 if (text_view->first_validate_idle != 0)
2382 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2383 g_source_remove (text_view->first_validate_idle);
2384 text_view->first_validate_idle = 0;
2387 if (text_view->incremental_validate_idle != 0)
2389 g_source_remove (text_view->incremental_validate_idle);
2390 text_view->incremental_validate_idle = 0;
2395 gtk_text_view_destroy (GtkObject *object)
2397 GtkTextView *text_view;
2398 GtkTextLayout *layout;
2400 text_view = GTK_TEXT_VIEW (object);
2402 layout = text_view->layout;
2404 gtk_text_view_remove_validate_idles (text_view);
2405 gtk_text_view_set_buffer (text_view, NULL);
2406 gtk_text_view_destroy_layout (text_view);
2408 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2412 gtk_text_view_finalize (GObject *object)
2414 GtkTextView *text_view;
2416 text_view = GTK_TEXT_VIEW (object);
2418 g_return_if_fail (text_view->buffer == NULL);
2420 gtk_text_view_destroy_layout (text_view);
2421 gtk_text_view_set_buffer (text_view, NULL);
2423 if (text_view->pending_scroll)
2425 free_pending_scroll (text_view->pending_scroll);
2426 text_view->pending_scroll = NULL;
2429 if (text_view->hadjustment)
2430 g_object_unref (G_OBJECT (text_view->hadjustment));
2431 if (text_view->vadjustment)
2432 g_object_unref (G_OBJECT (text_view->vadjustment));
2434 text_window_free (text_view->text_window);
2436 if (text_view->left_window)
2437 text_window_free (text_view->left_window);
2439 if (text_view->top_window)
2440 text_window_free (text_view->top_window);
2442 if (text_view->right_window)
2443 text_window_free (text_view->right_window);
2445 if (text_view->bottom_window)
2446 text_window_free (text_view->bottom_window);
2448 g_object_unref (G_OBJECT (text_view->im_context));
2450 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2454 gtk_text_view_set_property (GObject *object,
2456 const GValue *value,
2459 GtkTextView *text_view;
2461 text_view = GTK_TEXT_VIEW (object);
2465 case PROP_PIXELS_ABOVE_LINES:
2466 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2469 case PROP_PIXELS_BELOW_LINES:
2470 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2473 case PROP_PIXELS_INSIDE_WRAP:
2474 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2478 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2481 case PROP_WRAP_MODE:
2482 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2485 case PROP_JUSTIFICATION:
2486 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2489 case PROP_LEFT_MARGIN:
2490 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2493 case PROP_RIGHT_MARGIN:
2494 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2498 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2502 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2505 case PROP_CURSOR_VISIBLE:
2506 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2510 g_assert_not_reached ();
2516 gtk_text_view_get_property (GObject *object,
2521 GtkTextView *text_view;
2523 text_view = GTK_TEXT_VIEW (object);
2527 case PROP_PIXELS_ABOVE_LINES:
2528 g_value_set_int (value, text_view->pixels_above_lines);
2531 case PROP_PIXELS_BELOW_LINES:
2532 g_value_set_int (value, text_view->pixels_below_lines);
2535 case PROP_PIXELS_INSIDE_WRAP:
2536 g_value_set_int (value, text_view->pixels_inside_wrap);
2540 g_value_set_boolean (value, text_view->editable);
2543 case PROP_WRAP_MODE:
2544 g_value_set_enum (value, text_view->wrap_mode);
2547 case PROP_JUSTIFICATION:
2548 g_value_set_enum (value, text_view->justify);
2551 case PROP_LEFT_MARGIN:
2552 g_value_set_int (value, text_view->left_margin);
2555 case PROP_RIGHT_MARGIN:
2556 g_value_set_int (value, text_view->right_margin);
2560 g_value_set_int (value, text_view->indent);
2564 g_value_set_boxed (value, text_view->tabs);
2567 case PROP_CURSOR_VISIBLE:
2568 g_value_set_boolean (value, text_view->cursor_visible);
2572 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2578 gtk_text_view_size_request (GtkWidget *widget,
2579 GtkRequisition *requisition)
2581 GtkTextView *text_view;
2583 gint focus_edge_width;
2585 gboolean interior_focus;
2587 text_view = GTK_TEXT_VIEW (widget);
2589 gtk_widget_style_get (widget,
2590 "interior_focus", &interior_focus,
2591 "focus_line_width", &focus_width,
2595 focus_edge_width = 0;
2597 focus_edge_width = focus_width;
2599 if (text_view->layout)
2601 text_view->text_window->requisition.width = text_view->layout->width;
2602 text_view->text_window->requisition.height = text_view->layout->height;
2606 text_view->text_window->requisition.width = 0;
2607 text_view->text_window->requisition.height = 0;
2610 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2611 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2613 if (text_view->left_window)
2614 requisition->width += text_view->left_window->requisition.width;
2616 if (text_view->right_window)
2617 requisition->width += text_view->right_window->requisition.width;
2619 if (text_view->top_window)
2620 requisition->height += text_view->top_window->requisition.height;
2622 if (text_view->bottom_window)
2623 requisition->height += text_view->bottom_window->requisition.height;
2625 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2626 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2628 tmp_list = text_view->children;
2629 while (tmp_list != NULL)
2631 GtkTextViewChild *child = tmp_list->data;
2635 GtkRequisition child_req;
2636 GtkRequisition old_req;
2638 gtk_widget_get_child_requisition (child->widget, &old_req);
2640 gtk_widget_size_request (child->widget, &child_req);
2642 gtk_widget_get_child_requisition (child->widget, &child_req);
2644 /* Invalidate layout lines if required */
2645 if (text_view->layout &&
2646 (old_req.width != child_req.width ||
2647 old_req.height != child_req.height))
2648 gtk_text_child_anchor_queue_resize (child->anchor,
2653 GtkRequisition child_req;
2655 gtk_widget_size_request (child->widget, &child_req);
2658 tmp_list = g_slist_next (tmp_list);
2663 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2664 GtkTextViewChild *vc,
2665 GtkAllocation *allocation)
2671 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2675 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2678 buffer_y += vc->from_top_of_line;
2680 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2681 allocation->y = buffer_y - text_view->yoffset;
2683 gtk_widget_get_child_requisition (vc->widget, &req);
2684 allocation->width = req.width;
2685 allocation->height = req.height;
2689 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2690 GtkTextViewChild *vc)
2692 GtkAllocation allocation;
2694 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2696 gtk_widget_size_allocate (vc->widget, &allocation);
2699 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2701 vc->widget->allocation.x,
2702 vc->widget->allocation.y,
2703 text_view->yoffset);
2708 gtk_text_view_child_allocated (GtkTextLayout *layout,
2714 GtkTextViewChild *vc = NULL;
2715 GtkTextView *text_view = data;
2717 /* x,y is the position of the child from the top of the line, and
2718 * from the left of the buffer. We have to translate that into text
2719 * window coordinates, then size_allocate the child.
2722 vc = g_object_get_data (G_OBJECT (child),
2723 "gtk-text-view-child");
2725 g_assert (vc != NULL);
2727 DV (g_print ("child allocated at %d,%d\n", x, y));
2729 vc->from_left_of_buffer = x;
2730 vc->from_top_of_line = y;
2732 gtk_text_view_update_child_allocation (text_view, vc);
2736 gtk_text_view_allocate_children (GtkTextView *text_view)
2740 DV(g_print(G_STRLOC"\n"));
2742 tmp_list = text_view->children;
2743 while (tmp_list != NULL)
2745 GtkTextViewChild *child = tmp_list->data;
2749 /* We need to force-validate the regions containing
2752 GtkTextIter child_loc;
2753 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2757 gtk_text_layout_validate_yrange (text_view->layout,
2763 GtkAllocation allocation;
2764 GtkRequisition child_req;
2766 g_assert (child != NULL);
2768 allocation.x = child->x;
2769 allocation.y = child->y;
2771 gtk_widget_get_child_requisition (child->widget, &child_req);
2773 allocation.width = child_req.width;
2774 allocation.height = child_req.height;
2776 gtk_widget_size_allocate (child->widget, &allocation);
2779 tmp_list = g_slist_next (tmp_list);
2784 gtk_text_view_size_allocate (GtkWidget *widget,
2785 GtkAllocation *allocation)
2787 GtkTextView *text_view;
2788 GtkTextIter first_para;
2790 GtkAdjustment *vadj;
2791 gboolean yoffset_changed = FALSE;
2793 GdkRectangle text_rect;
2794 GdkRectangle left_rect;
2795 GdkRectangle right_rect;
2796 GdkRectangle top_rect;
2797 GdkRectangle bottom_rect;
2798 gint focus_edge_width;
2800 gboolean interior_focus;
2801 gboolean size_changed;
2803 text_view = GTK_TEXT_VIEW (widget);
2805 DV(g_print(G_STRLOC"\n"));
2808 widget->allocation.width != allocation->width ||
2809 widget->allocation.height != allocation->height;
2811 widget->allocation = *allocation;
2813 if (GTK_WIDGET_REALIZED (widget))
2815 gdk_window_move_resize (widget->window,
2816 allocation->x, allocation->y,
2817 allocation->width, allocation->height);
2820 /* distribute width/height among child windows. Ensure all
2821 * windows get at least a 1x1 allocation.
2824 gtk_widget_style_get (widget,
2825 "interior_focus", &interior_focus,
2826 "focus_line_width", &focus_width,
2830 focus_edge_width = 0;
2832 focus_edge_width = focus_width;
2834 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2836 if (text_view->left_window)
2837 left_rect.width = text_view->left_window->requisition.width;
2839 left_rect.width = 0;
2841 width -= left_rect.width;
2843 if (text_view->right_window)
2844 right_rect.width = text_view->right_window->requisition.width;
2846 right_rect.width = 0;
2848 width -= right_rect.width;
2850 text_rect.width = MAX (1, width);
2852 top_rect.width = text_rect.width;
2853 bottom_rect.width = text_rect.width;
2856 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2858 if (text_view->top_window)
2859 top_rect.height = text_view->top_window->requisition.height;
2861 top_rect.height = 0;
2863 height -= top_rect.height;
2865 if (text_view->bottom_window)
2866 bottom_rect.height = text_view->bottom_window->requisition.height;
2868 bottom_rect.height = 0;
2870 height -= bottom_rect.height;
2872 text_rect.height = MAX (1, height);
2874 left_rect.height = text_rect.height;
2875 right_rect.height = text_rect.height;
2878 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2879 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2881 text_rect.x = left_rect.x + left_rect.width;
2882 text_rect.y = top_rect.y + top_rect.height;
2884 left_rect.y = text_rect.y;
2885 right_rect.y = text_rect.y;
2887 top_rect.x = text_rect.x;
2888 bottom_rect.x = text_rect.x;
2890 right_rect.x = text_rect.x + text_rect.width;
2891 bottom_rect.y = text_rect.y + text_rect.height;
2893 text_window_size_allocate (text_view->text_window,
2896 if (text_view->left_window)
2897 text_window_size_allocate (text_view->left_window,
2900 if (text_view->right_window)
2901 text_window_size_allocate (text_view->right_window,
2904 if (text_view->top_window)
2905 text_window_size_allocate (text_view->top_window,
2908 if (text_view->bottom_window)
2909 text_window_size_allocate (text_view->bottom_window,
2912 gtk_text_view_update_layout_width (text_view);
2914 /* Note that this will do some layout validation */
2915 gtk_text_view_allocate_children (text_view);
2917 /* Now adjust the value of the adjustment to keep the cursor at the
2918 * same place in the buffer
2920 gtk_text_view_get_first_para_iter (text_view, &first_para);
2921 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2923 y += text_view->first_para_pixels;
2925 /* Ensure h/v adj exist */
2926 get_hadjustment (text_view);
2927 get_vadjustment (text_view);
2929 vadj = text_view->vadjustment;
2930 if (y > vadj->upper - vadj->page_size)
2931 y = MAX (0, vadj->upper - vadj->page_size);
2933 if (y != text_view->yoffset)
2935 vadj->value = text_view->yoffset = y;
2936 yoffset_changed = TRUE;
2939 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2940 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) / 2;
2941 text_view->hadjustment->lower = 0;
2942 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2944 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2946 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2947 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) / 2;
2948 text_view->vadjustment->lower = 0;
2949 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2951 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2953 if (yoffset_changed)
2954 gtk_adjustment_value_changed (vadj);
2956 /* The GTK resize loop processes all the pending exposes right
2957 * after doing the resize stuff, so the idle sizer won't have a
2958 * chance to run. So we do the work here.
2960 gtk_text_view_flush_first_validate (text_view);
2962 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2965 if (size_changed && GTK_WIDGET_REALIZED (widget))
2966 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2970 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2973 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2974 text_view->first_para_mark);
2978 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2980 GtkWidget *widget = GTK_WIDGET (text_view);
2982 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
2984 if (SCREEN_HEIGHT (widget) > 0)
2986 GtkTextIter first_para;
2988 /* Be sure we've validated the stuff onscreen; if we
2989 * scrolled, these calls won't have any effect, because
2990 * they were called in the recursive validate_onscreen
2992 gtk_text_view_get_first_para_iter (text_view, &first_para);
2994 gtk_text_layout_validate_yrange (text_view->layout,
2997 text_view->first_para_pixels +
2998 SCREEN_HEIGHT (widget));
3001 text_view->onscreen_validated = TRUE;
3003 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3005 /* This can have the odd side effect of triggering a scroll, which should
3006 * flip "onscreen_validated" back to FALSE, but should also get us
3007 * back into this function to turn it on again.
3009 gtk_text_view_update_adjustments (text_view);
3011 g_assert (text_view->onscreen_validated);
3015 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3017 if (text_view->first_validate_idle == 0)
3020 /* Do this first, which means that if an "invalidate"
3021 * occurs during any of this process, a new first_validate_callback
3022 * will be installed, and we'll start again.
3024 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3025 g_source_remove (text_view->first_validate_idle);
3026 text_view->first_validate_idle = 0;
3028 /* be sure we have up-to-date screen size set on the
3031 gtk_text_view_update_layout_width (text_view);
3033 /* Bail out if we invalidated stuff; scrolling right away will just
3034 * confuse the issue.
3036 if (text_view->first_validate_idle != 0)
3038 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3042 /* scroll to any marks, if that's pending. This can
3043 * jump us to the validation codepath used for scrolling
3044 * onscreen, if so we bail out.
3046 if (!gtk_text_view_flush_scroll (text_view))
3047 gtk_text_view_validate_onscreen (text_view);
3049 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3051 g_assert (text_view->onscreen_validated);
3056 first_validate_callback (gpointer data)
3058 GtkTextView *text_view = data;
3060 GDK_THREADS_ENTER ();
3062 /* Note that some of this code is duplicated at the end of size_allocate,
3063 * keep in sync with that.
3066 DV(g_print(G_STRLOC"\n"));
3068 gtk_text_view_flush_first_validate (text_view);
3070 GDK_THREADS_LEAVE ();
3076 incremental_validate_callback (gpointer data)
3078 GtkTextView *text_view = data;
3079 gboolean result = TRUE;
3081 GDK_THREADS_ENTER ();
3083 DV(g_print(G_STRLOC"\n"));
3085 gtk_text_layout_validate (text_view->layout, 2000);
3087 gtk_text_view_update_adjustments (text_view);
3089 if (gtk_text_layout_is_valid (text_view->layout))
3091 text_view->incremental_validate_idle = 0;
3095 GDK_THREADS_LEAVE ();
3101 gtk_text_view_invalidate (GtkTextView *text_view)
3103 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3104 text_view->onscreen_validated));
3106 text_view->onscreen_validated = FALSE;
3108 if (!text_view->first_validate_idle)
3110 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3111 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3112 text_view->first_validate_idle));
3115 if (!text_view->incremental_validate_idle)
3117 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3118 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3119 text_view->incremental_validate_idle));
3124 invalidated_handler (GtkTextLayout *layout,
3127 GtkTextView *text_view;
3129 text_view = GTK_TEXT_VIEW (data);
3131 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3132 gtk_text_view_invalidate (text_view);
3136 changed_handler (GtkTextLayout *layout,
3142 GtkTextView *text_view;
3144 GdkRectangle visible_rect;
3145 GdkRectangle redraw_rect;
3147 text_view = GTK_TEXT_VIEW (data);
3148 widget = GTK_WIDGET (data);
3150 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3152 if (GTK_WIDGET_REALIZED (text_view))
3154 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3156 redraw_rect.x = visible_rect.x;
3157 redraw_rect.width = visible_rect.width;
3158 redraw_rect.y = start_y;
3160 if (old_height == new_height)
3161 redraw_rect.height = old_height;
3163 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3165 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3167 /* text_window_invalidate_rect() takes buffer coordinates */
3168 text_window_invalidate_rect (text_view->text_window,
3171 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3175 redraw_rect.height));
3177 if (text_view->left_window)
3178 text_window_invalidate_rect (text_view->left_window,
3180 if (text_view->right_window)
3181 text_window_invalidate_rect (text_view->right_window,
3183 if (text_view->top_window)
3184 text_window_invalidate_rect (text_view->top_window,
3186 if (text_view->bottom_window)
3187 text_window_invalidate_rect (text_view->bottom_window,
3190 gtk_text_view_update_im_spot_location (text_view);
3194 if (old_height != new_height)
3196 gboolean yoffset_changed = FALSE;
3198 int new_first_para_top;
3199 int old_first_para_top;
3202 /* If the bottom of the old area was above the top of the
3203 * screen, we need to scroll to keep the current top of the
3204 * screen in place. Remember that first_para_pixels is the
3205 * position of the top of the screen in coordinates relative to
3206 * the first paragraph onscreen.
3208 * In short we are adding the height delta of the portion of the
3209 * changed region above first_para_mark to text_view->yoffset.
3211 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3212 text_view->first_para_mark);
3214 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3216 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3218 if (new_first_para_top != old_first_para_top)
3220 text_view->yoffset += new_first_para_top - old_first_para_top;
3222 get_vadjustment (text_view)->value = text_view->yoffset;
3223 yoffset_changed = TRUE;
3226 if (yoffset_changed)
3228 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3229 gtk_adjustment_value_changed (get_vadjustment (text_view));
3232 /* FIXME be smarter about which anchored widgets we update */
3234 tmp_list = text_view->children;
3235 while (tmp_list != NULL)
3237 GtkTextViewChild *child = tmp_list->data;
3240 gtk_text_view_update_child_allocation (text_view, child);
3242 tmp_list = g_slist_next (tmp_list);
3247 GtkRequisition old_req;
3248 GtkRequisition new_req;
3250 old_req = widget->requisition;
3252 /* Use this instead of gtk_widget_size_request wrapper
3253 * to avoid the optimization which just returns widget->requisition
3254 * if a resize hasn't been queued.
3256 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3258 if (old_req.width != new_req.width ||
3259 old_req.height != new_req.height)
3260 gtk_widget_queue_resize (widget);
3265 gtk_text_view_realize_cursor_gc (GtkTextView *text_view)
3267 GdkColor *cursor_color;
3268 GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
3270 if (text_view->cursor_gc)
3271 gdk_gc_unref (text_view->cursor_gc);
3273 gtk_widget_style_get (GTK_WIDGET (text_view), "cursor_color", &cursor_color, NULL);
3276 cursor_color = &red;
3278 text_view->cursor_gc = gdk_gc_new (text_view->text_window->bin_window);
3279 gdk_gc_set_rgb_fg_color (text_view->cursor_gc, cursor_color);
3283 gtk_text_view_realize (GtkWidget *widget)
3285 GtkTextView *text_view;
3286 GdkWindowAttr attributes;
3287 gint attributes_mask;
3290 text_view = GTK_TEXT_VIEW (widget);
3291 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3293 attributes.window_type = GDK_WINDOW_CHILD;
3294 attributes.x = widget->allocation.x;
3295 attributes.y = widget->allocation.y;
3296 attributes.width = widget->allocation.width;
3297 attributes.height = widget->allocation.height;
3298 attributes.wclass = GDK_INPUT_OUTPUT;
3299 attributes.visual = gtk_widget_get_visual (widget);
3300 attributes.colormap = gtk_widget_get_colormap (widget);
3301 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3303 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3305 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3306 &attributes, attributes_mask);
3307 gdk_window_set_user_data (widget->window, widget);
3309 /* must come before text_window_realize calls */
3310 widget->style = gtk_style_attach (widget->style, widget->window);
3312 gdk_window_set_background (widget->window,
3313 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3315 text_window_realize (text_view->text_window, widget->window);
3317 if (text_view->left_window)
3318 text_window_realize (text_view->left_window,
3321 if (text_view->top_window)
3322 text_window_realize (text_view->top_window,
3325 if (text_view->right_window)
3326 text_window_realize (text_view->right_window,
3329 if (text_view->bottom_window)
3330 text_window_realize (text_view->bottom_window,
3333 gtk_text_view_realize_cursor_gc (text_view);
3335 gtk_text_view_ensure_layout (text_view);
3337 if (text_view->buffer)
3338 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
3339 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3341 tmp_list = text_view->children;
3342 while (tmp_list != NULL)
3344 GtkTextViewChild *vc = tmp_list->data;
3346 text_view_child_set_parent_window (text_view, vc);
3348 tmp_list = tmp_list->next;
3353 gtk_text_view_unrealize (GtkWidget *widget)
3355 GtkTextView *text_view;
3357 text_view = GTK_TEXT_VIEW (widget);
3359 if (text_view->buffer)
3360 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
3361 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3363 if (text_view->cursor_gc)
3365 gdk_gc_unref (text_view->cursor_gc);
3366 text_view->cursor_gc = NULL;
3369 gtk_text_view_remove_validate_idles (text_view);
3371 if (text_view->popup_menu)
3373 gtk_widget_destroy (text_view->popup_menu);
3374 text_view->popup_menu = NULL;
3377 text_window_unrealize (text_view->text_window);
3379 if (text_view->left_window)
3380 text_window_unrealize (text_view->left_window);
3382 if (text_view->top_window)
3383 text_window_unrealize (text_view->top_window);
3385 if (text_view->right_window)
3386 text_window_unrealize (text_view->right_window);
3388 if (text_view->bottom_window)
3389 text_window_unrealize (text_view->bottom_window);
3391 gtk_text_view_destroy_layout (text_view);
3393 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3397 gtk_text_view_style_set (GtkWidget *widget,
3398 GtkStyle *previous_style)
3400 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3402 if (GTK_WIDGET_REALIZED (widget))
3404 gdk_window_set_background (widget->window,
3405 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3407 gdk_window_set_background (text_view->text_window->bin_window,
3408 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3410 if (text_view->left_window)
3411 gdk_window_set_background (text_view->left_window->bin_window,
3412 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3413 if (text_view->right_window)
3414 gdk_window_set_background (text_view->right_window->bin_window,
3415 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3417 if (text_view->top_window)
3418 gdk_window_set_background (text_view->top_window->bin_window,
3419 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3421 if (text_view->bottom_window)
3422 gdk_window_set_background (text_view->bottom_window->bin_window,
3423 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3425 gtk_text_view_realize_cursor_gc (text_view);
3428 if (text_view->layout && previous_style)
3430 gtk_text_view_set_attributes_from_style (text_view,
3431 text_view->layout->default_style,
3433 gtk_text_layout_default_style_changed (text_view->layout);
3438 gtk_text_view_direction_changed (GtkWidget *widget,
3439 GtkTextDirection previous_direction)
3441 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3443 if (text_view->layout)
3445 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3446 gtk_text_layout_default_style_changed (text_view->layout);
3452 set_invisible_cursor (GdkWindow *window)
3454 GdkBitmap *empty_bitmap;
3457 char invisible_cursor_bits[] = { 0x0 };
3459 useless.red = useless.green = useless.blue = 0;
3462 empty_bitmap = gdk_bitmap_create_from_data (window,
3463 invisible_cursor_bits,
3466 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3471 gdk_window_set_cursor (window, cursor);
3473 gdk_cursor_unref (cursor);
3475 g_object_unref (empty_bitmap);
3479 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3481 if (text_view->mouse_cursor_obscured)
3484 set_invisible_cursor (text_view->text_window->bin_window);
3486 text_view->mouse_cursor_obscured = TRUE;
3494 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3497 switch (event->type)
3499 case GDK_MOTION_NOTIFY:
3500 *x = event->motion.x;
3501 *y = event->motion.y;
3505 case GDK_BUTTON_PRESS:
3506 case GDK_2BUTTON_PRESS:
3507 case GDK_3BUTTON_PRESS:
3508 case GDK_BUTTON_RELEASE:
3509 *x = event->button.x;
3510 *y = event->button.y;
3515 case GDK_KEY_RELEASE:
3516 case GDK_ENTER_NOTIFY:
3517 case GDK_LEAVE_NOTIFY:
3518 case GDK_PROPERTY_NOTIFY:
3519 case GDK_SELECTION_CLEAR:
3520 case GDK_SELECTION_REQUEST:
3521 case GDK_SELECTION_NOTIFY:
3522 case GDK_PROXIMITY_IN:
3523 case GDK_PROXIMITY_OUT:
3524 case GDK_DRAG_ENTER:
3525 case GDK_DRAG_LEAVE:
3526 case GDK_DRAG_MOTION:
3527 case GDK_DRAG_STATUS:
3528 case GDK_DROP_START:
3529 case GDK_DROP_FINISHED:
3539 emit_event_on_tags (GtkWidget *widget,
3545 gboolean retval = FALSE;
3546 GtkTextView *text_view;
3548 text_view = GTK_TEXT_VIEW (widget);
3550 tags = gtk_text_iter_get_tags (iter);
3555 GtkTextTag *tag = tmp->data;
3557 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3563 tmp = g_slist_next (tmp);
3566 g_slist_free (tags);
3572 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3574 GtkTextView *text_view;
3577 text_view = GTK_TEXT_VIEW (widget);
3579 if (text_view->layout == NULL ||
3580 get_buffer (text_view) == NULL)
3583 if (event->any.window != text_view->text_window->bin_window)
3586 if (get_event_coordinates (event, &x, &y))
3590 x += text_view->xoffset;
3591 y += text_view->yoffset;
3593 /* FIXME this is slow and we do it twice per event.
3594 * My favorite solution is to have GtkTextLayout cache
3595 * the last couple lookups.
3597 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3601 return emit_event_on_tags (widget, event, &iter);
3603 else if (event->type == GDK_KEY_PRESS ||
3604 event->type == GDK_KEY_RELEASE)
3606 GtkTextMark *insert;
3609 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3612 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3614 return emit_event_on_tags (widget, event, &iter);
3621 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3623 gboolean retval = FALSE;
3624 gboolean obscure = FALSE;
3625 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3626 GtkTextMark *insert;
3629 if (text_view->layout == NULL ||
3630 get_buffer (text_view) == NULL)
3633 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3634 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3635 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3636 gtk_im_context_filter_keypress (text_view->im_context, event))
3638 text_view->need_im_reset = TRUE;
3642 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3643 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3645 else if (event->keyval == GDK_Return ||
3646 event->keyval == GDK_KP_Enter)
3648 gtk_text_view_commit_text (text_view, "\n");
3653 /* Pass through Tab as literal tab, unless Control is held down */
3654 else if ((event->keyval == GDK_Tab ||
3655 event->keyval == GDK_KP_Tab ||
3656 event->keyval == GDK_ISO_Left_Tab) &&
3657 !(event->state & GDK_CONTROL_MASK))
3659 /* If the text isn't editable, move the focus instead */
3660 if (text_view->editable)
3662 gtk_text_view_commit_text (text_view, "\t");
3666 gtk_text_view_move_focus (text_view,
3667 (event->state & GDK_SHIFT_MASK) ?
3668 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3676 gtk_text_view_obscure_mouse_cursor (text_view);
3678 gtk_text_view_pend_cursor_blink (text_view);
3684 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3686 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3687 GtkTextMark *insert;
3690 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3693 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3694 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3695 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3696 gtk_im_context_filter_keypress (text_view->im_context, event))
3698 text_view->need_im_reset = TRUE;
3702 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3706 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3708 GtkTextView *text_view;
3710 text_view = GTK_TEXT_VIEW (widget);
3712 text_view->disable_scroll_on_focus = TRUE;
3713 gtk_widget_grab_focus (widget);
3714 text_view->disable_scroll_on_focus = FALSE;
3716 if (event->window != text_view->text_window->bin_window)
3718 /* Remove selection if any. */
3719 gtk_text_view_unselect (text_view);
3725 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3726 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3727 else if (event->button == 3)
3728 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3731 if (event->type == GDK_BUTTON_PRESS)
3733 gtk_text_view_reset_im_context (text_view);
3735 if (event->button == 1)
3737 /* If we're in the selection, start a drag copy/move of the
3738 * selection; otherwise, start creating a new selection.
3741 GtkTextIter start, end;
3743 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3745 event->x + text_view->xoffset,
3746 event->y + text_view->yoffset);
3748 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3750 gtk_text_iter_in_range (&iter, &start, &end))
3752 text_view->drag_start_x = event->x;
3753 text_view->drag_start_y = event->y;
3754 text_view->pending_place_cursor_button = event->button;
3758 gtk_text_view_start_selection_drag (text_view, &iter, event);
3763 else if (event->button == 2)
3767 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3769 event->x + text_view->xoffset,
3770 event->y + text_view->yoffset);
3772 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3773 gtk_clipboard_get (GDK_SELECTION_PRIMARY),
3775 text_view->editable);
3778 else if (event->button == 3)
3780 gtk_text_view_do_popup (text_view, event);
3783 else if ((event->type == GDK_2BUTTON_PRESS ||
3784 event->type == GDK_3BUTTON_PRESS) &&
3787 GtkTextIter start, end;
3789 /* End the selection drag, otherwise we'd clear the new
3790 * word/line selection on button release
3792 gtk_text_view_end_selection_drag (text_view, event);
3794 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3796 event->x + text_view->xoffset,
3797 event->y + text_view->yoffset);
3801 if (event->type == GDK_2BUTTON_PRESS)
3803 if (gtk_text_iter_inside_word (&start))
3805 if (!gtk_text_iter_starts_word (&start))
3806 gtk_text_iter_backward_word_start (&start);
3808 if (!gtk_text_iter_ends_word (&end))
3809 gtk_text_iter_forward_word_end (&end);
3812 else if (event->type == GDK_3BUTTON_PRESS)
3814 if (gtk_text_view_starts_display_line (text_view, &start))
3816 /* If on a display line boundary, we assume the user
3817 * clicked off the end of a line and we therefore select
3818 * the line before the boundary.
3820 gtk_text_view_backward_display_line_start (text_view, &start);
3824 /* start isn't on the start of a line, so we move it to the
3825 * start, and move end to the end unless it's already there.
3827 gtk_text_view_backward_display_line_start (text_view, &start);
3829 if (!gtk_text_view_starts_display_line (text_view, &end))
3830 gtk_text_view_forward_display_line_end (text_view, &end);
3834 if (event->state & GDK_SHIFT_MASK)
3836 /* Take union of old and new selection */
3837 GtkTextIter old_start, old_end;
3839 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3840 &old_start, &old_end);
3842 gtk_text_iter_order (&start, &old_start);
3843 gtk_text_iter_order (&old_end, &end);
3845 /* Now start is the first of the starts, and end is the
3850 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3853 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3857 text_view->just_selected_element = TRUE;
3866 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3868 GtkTextView *text_view;
3870 text_view = GTK_TEXT_VIEW (widget);
3872 if (event->window != text_view->text_window->bin_window)
3875 if (event->button == 1)
3877 if (text_view->drag_start_x >= 0)
3879 text_view->drag_start_x = -1;
3880 text_view->drag_start_y = -1;
3883 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3885 else if (text_view->just_selected_element)
3887 text_view->just_selected_element = FALSE;
3890 else if (text_view->pending_place_cursor_button == event->button)
3894 /* Unselect everything; we clicked inside selection, but
3895 * didn't move by the drag threshold, so just clear selection
3898 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3900 event->x + text_view->xoffset,
3901 event->y + text_view->yoffset);
3903 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3905 text_view->pending_place_cursor_button = 0;
3915 keymap_direction_changed (GdkKeymap *keymap,
3916 GtkTextView *text_view)
3918 gtk_text_view_check_keymap_direction (text_view);
3922 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3924 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3926 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
3927 gtk_widget_queue_draw (widget);
3929 DV(g_print (G_STRLOC": focus_in_event\n"));
3931 if (text_view->cursor_visible && text_view->layout)
3933 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3934 gtk_text_view_check_cursor_blink (text_view);
3937 g_signal_connect (gdk_keymap_get_default (),
3938 "direction_changed",
3939 G_CALLBACK (keymap_direction_changed), text_view);
3940 gtk_text_view_check_keymap_direction (text_view);
3942 text_view->need_im_reset = TRUE;
3943 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3949 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3951 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3953 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
3954 gtk_widget_queue_draw (widget);
3956 DV(g_print (G_STRLOC": focus_out_event\n"));
3958 if (text_view->cursor_visible && text_view->layout)
3960 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3961 gtk_text_view_check_cursor_blink (text_view);
3964 g_signal_handlers_disconnect_by_func (gdk_keymap_get_default (),
3965 keymap_direction_changed,
3968 text_view->need_im_reset = TRUE;
3969 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3975 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3977 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3979 if (text_view->mouse_cursor_obscured)
3983 cursor = gdk_cursor_new (GDK_XTERM);
3984 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3985 gdk_cursor_unref (cursor);
3986 text_view->mouse_cursor_obscured = FALSE;
3989 if (event->window == text_view->text_window->bin_window &&
3990 text_view->drag_start_x >= 0)
3994 gdk_window_get_pointer (text_view->text_window->bin_window,
3997 if (gtk_drag_check_threshold (widget,
3998 text_view->drag_start_x,
3999 text_view->drag_start_y,
4003 gint buffer_x, buffer_y;
4005 gtk_text_view_window_to_buffer_coords (text_view,
4006 GTK_TEXT_WINDOW_TEXT,
4007 text_view->drag_start_x,
4008 text_view->drag_start_y,
4012 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4014 buffer_x, buffer_y);
4016 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4025 gtk_text_view_paint (GtkWidget *widget,
4027 GdkEventExpose *event)
4029 GtkTextView *text_view;
4030 GList *child_exposes;
4034 text_view = GTK_TEXT_VIEW (widget);
4036 g_return_if_fail (text_view->layout != NULL);
4037 g_return_if_fail (text_view->xoffset >= 0);
4038 g_return_if_fail (text_view->yoffset >= 0);
4040 while (text_view->first_validate_idle != 0)
4042 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4043 text_view->first_validate_idle));
4044 gtk_text_view_flush_first_validate (text_view);
4047 /* More regions could have become invalid in the above loop */
4048 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4053 gdk_region_get_clipbox (updates, &rect);
4055 gdk_rectangle_union (area, &rect, area);
4057 gdk_region_destroy (updates);
4060 if (!text_view->onscreen_validated)
4062 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.");
4067 printf ("painting %d,%d %d x %d\n",
4069 area->width, area->height);
4072 child_exposes = NULL;
4073 gtk_text_layout_draw (text_view->layout,
4075 text_view->text_window->bin_window,
4076 text_view->cursor_gc,
4080 area->width, area->height,
4083 tmp_list = child_exposes;
4084 while (tmp_list != NULL)
4086 GtkWidget *child = tmp_list->data;
4088 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4092 g_object_unref (G_OBJECT (child));
4094 tmp_list = tmp_list->next;
4097 g_list_free (child_exposes);
4101 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4105 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4106 GTK_TEXT_WINDOW_TEXT))
4108 DV(g_print (">Exposed ("G_STRLOC")\n"));
4109 gtk_text_view_paint (widget, &event->area, event);
4112 if (event->window == widget->window)
4113 gtk_text_view_draw_focus (widget);
4115 /* Propagate exposes to all children not in the buffer. */
4116 tmp_list = GTK_TEXT_VIEW (widget)->children;
4117 while (tmp_list != NULL)
4119 GtkTextViewChild *vc = tmp_list->data;
4121 /* propagate_expose checks that event->window matches
4124 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4125 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4129 tmp_list = tmp_list->next;
4136 gtk_text_view_draw_focus (GtkWidget *widget)
4138 gboolean interior_focus;
4140 /* We clear the focus if we are in interior focus mode. */
4141 gtk_widget_style_get (widget,
4142 "interior_focus", &interior_focus,
4145 if (GTK_WIDGET_DRAWABLE (widget))
4147 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4149 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4150 NULL, widget, "textview",
4152 widget->allocation.width,
4153 widget->allocation.height);
4157 gdk_window_clear (widget->window);
4163 gtk_text_view_grab_focus (GtkWidget *widget)
4165 GtkTextView *text_view;
4167 text_view = GTK_TEXT_VIEW (widget);
4169 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4171 if (!text_view->disable_scroll_on_focus)
4172 gtk_text_view_scroll_mark_onscreen (text_view,
4173 gtk_text_buffer_get_mark (get_buffer (text_view),
4178 gtk_text_view_focus (GtkWidget *widget,
4179 GtkDirectionType direction)
4181 GtkTextView *text_view;
4182 GtkContainer *container;
4184 text_view = GTK_TEXT_VIEW (widget);
4185 container = GTK_CONTAINER (widget);
4187 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4195 gtk_text_view_add (GtkContainer *container,
4198 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4199 g_return_if_fail (GTK_IS_WIDGET (child));
4201 /* This is pretty random. */
4202 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4204 GTK_TEXT_WINDOW_WIDGET,
4209 gtk_text_view_remove (GtkContainer *container,
4213 GtkTextView *text_view;
4214 GtkTextViewChild *vc;
4216 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4217 g_return_if_fail (GTK_IS_WIDGET (child));
4218 g_return_if_fail (child->parent == (GtkWidget*) container);
4220 text_view = GTK_TEXT_VIEW (container);
4223 iter = text_view->children;
4225 while (iter != NULL)
4229 if (vc->widget == child)
4232 iter = g_slist_next (iter);
4235 g_assert (iter != NULL); /* be sure we had the child in the list */
4237 text_view->children = g_slist_remove (text_view->children, vc);
4239 gtk_widget_unparent (vc->widget);
4241 text_view_child_free (vc);
4245 gtk_text_view_forall (GtkContainer *container,
4246 gboolean include_internals,
4247 GtkCallback callback,
4248 gpointer callback_data)
4251 GtkTextView *text_view;
4254 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4255 g_return_if_fail (callback != NULL);
4257 text_view = GTK_TEXT_VIEW (container);
4259 copy = g_slist_copy (text_view->children);
4262 while (iter != NULL)
4264 GtkTextViewChild *vc = iter->data;
4266 (* callback) (vc->widget, callback_data);
4268 iter = g_slist_next (iter);
4271 g_slist_free (copy);
4274 #define CURSOR_ON_MULTIPLIER 0.66
4275 #define CURSOR_OFF_MULTIPLIER 0.34
4276 #define CURSOR_PEND_MULTIPLIER 1.0
4279 cursor_blinks (GtkTextView *text_view)
4281 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4284 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4287 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4290 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4295 get_cursor_time (GtkTextView *text_view)
4297 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4300 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4310 blink_cb (gpointer data)
4312 GtkTextView *text_view;
4315 GDK_THREADS_ENTER ();
4317 text_view = GTK_TEXT_VIEW (data);
4319 g_assert (text_view->layout);
4320 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4321 g_assert (text_view->cursor_visible);
4323 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4326 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4330 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4334 gtk_text_layout_set_cursor_visible (text_view->layout,
4337 GDK_THREADS_LEAVE ();
4339 /* Remove ourselves */
4345 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4347 if (text_view->blink_timeout)
4349 gtk_timeout_remove (text_view->blink_timeout);
4350 text_view->blink_timeout = 0;
4355 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4357 if (text_view->layout != NULL &&
4358 text_view->cursor_visible &&
4359 GTK_WIDGET_HAS_FOCUS (text_view))
4361 if (cursor_blinks (text_view))
4363 if (text_view->blink_timeout == 0)
4365 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4367 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4373 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4377 gtk_text_view_stop_cursor_blink (text_view);
4382 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4384 if (text_view->layout != NULL &&
4385 text_view->cursor_visible &&
4386 GTK_WIDGET_HAS_FOCUS (text_view) &&
4387 cursor_blinks (text_view))
4389 if (text_view->blink_timeout != 0)
4391 gtk_timeout_remove (text_view->blink_timeout);
4392 text_view->blink_timeout = 0;
4395 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4397 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4405 * Key binding handlers
4409 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4410 GtkTextIter *newplace,
4415 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4421 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4426 /* FIXME when we are unfrozen and can change GtkMovementStep,
4429 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4432 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4433 GtkMovementStep step,
4435 gboolean extend_selection)
4438 GtkTextIter newplace;
4440 gint cursor_x_pos = 0;
4442 gtk_text_view_reset_im_context (text_view);
4444 if (step == GTK_MOVEMENT_PAGES)
4446 gtk_text_view_scroll_pages (text_view, count);
4447 gtk_text_view_pend_cursor_blink (text_view);
4450 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4452 gtk_text_view_scroll_hpages (text_view, count);
4453 gtk_text_view_pend_cursor_blink (text_view);
4457 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4458 gtk_text_buffer_get_mark (get_buffer (text_view),
4462 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4463 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4467 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4468 gtk_text_iter_forward_cursor_positions (&newplace, count);
4471 case GTK_MOVEMENT_VISUAL_POSITIONS:
4472 gtk_text_layout_move_iter_visually (text_view->layout,
4476 case GTK_MOVEMENT_WORDS:
4478 gtk_text_iter_backward_word_starts (&newplace, -count);
4480 gtk_text_iter_forward_word_ends (&newplace, count);
4483 case GTK_MOVEMENT_DISPLAY_LINES:
4484 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4485 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4488 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4490 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4491 else if (count < -1)
4492 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4495 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4498 case GTK_MOVEMENT_PARAGRAPHS:
4501 if (!gtk_text_iter_ends_line (&newplace))
4503 gtk_text_iter_forward_to_line_end (&newplace);
4509 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4511 gtk_text_iter_set_line_offset (&newplace, 0);
4518 gtk_text_iter_forward_lines (&newplace, count);
4519 gtk_text_iter_set_line_offset (&newplace, 0);
4523 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4526 if (!gtk_text_iter_ends_line (&newplace))
4527 gtk_text_iter_forward_to_line_end (&newplace);
4531 gtk_text_iter_set_line_offset (&newplace, 0);
4535 case GTK_MOVEMENT_BUFFER_ENDS:
4537 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4539 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4546 if (!gtk_text_iter_equal (&insert, &newplace))
4548 if (extend_selection)
4549 gtk_text_buffer_move_mark (get_buffer (text_view),
4550 gtk_text_buffer_get_mark (get_buffer (text_view),
4554 gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4556 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4557 gtk_text_view_scroll_mark_onscreen (text_view,
4558 gtk_text_buffer_get_mark (get_buffer (text_view),
4561 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4563 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4567 gtk_text_view_pend_cursor_blink (text_view);
4571 gtk_text_view_move_cursor (GtkTextView *text_view,
4572 GtkMovementStep step,
4574 gboolean extend_selection)
4576 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4580 gtk_text_view_page_horizontally (GtkTextView *text_view,
4582 gboolean extend_selection)
4584 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4585 count, extend_selection);
4589 gtk_text_view_set_anchor (GtkTextView *text_view)
4593 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4594 gtk_text_buffer_get_mark (get_buffer (text_view),
4597 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4601 gtk_text_view_scroll_pages (GtkTextView *text_view,
4607 gint cursor_x_pos, cursor_y_pos;
4608 GtkTextIter new_insert;
4612 g_return_if_fail (text_view->vadjustment != NULL);
4614 adj = text_view->vadjustment;
4616 /* Validate the region that will be brought into view by the cursor motion
4620 gtk_text_view_get_first_para_iter (text_view, &anchor);
4621 y0 = adj->page_size;
4622 y1 = adj->page_size + count * adj->page_increment;
4626 gtk_text_view_get_first_para_iter (text_view, &anchor);
4627 y0 = count * adj->page_increment + adj->page_size;
4631 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4632 /* FIXME do we need to update the adjustment ranges here? */
4634 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4636 /* already at top, just be sure we are at offset 0 */
4637 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4638 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4640 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4642 /* already at bottom, just be sure we are at the end */
4643 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4644 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4648 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4650 newval = adj->value;
4651 oldval = adj->value;
4653 newval += count * adj->page_increment;
4655 set_adjustment_clamped (adj, newval);
4656 cursor_y_pos += adj->value - oldval;
4658 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4659 clamp_iter_onscreen (text_view, &new_insert);
4660 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4662 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4665 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4666 * only guarantees 1 pixel onscreen.
4668 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4669 gtk_text_view_scroll_mark_onscreen (text_view,
4670 gtk_text_buffer_get_mark (get_buffer (text_view),
4675 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4681 gint cursor_x_pos, cursor_y_pos;
4682 GtkTextIter new_insert;
4685 g_return_if_fail (text_view->hadjustment != NULL);
4687 adj = text_view->hadjustment;
4689 /* Validate the line that we're moving within.
4691 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4693 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4694 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4695 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4696 /* FIXME do we need to update the adjustment ranges here? */
4698 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4700 /* already at far left, just be sure we are at offset 0 */
4701 gtk_text_iter_set_line_offset (&new_insert, 0);
4702 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4704 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4706 /* already at far right, just be sure we are at the end */
4707 gtk_text_iter_forward_to_line_end (&new_insert);
4708 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4712 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4714 newval = adj->value;
4715 oldval = adj->value;
4717 newval += count * adj->page_increment;
4719 set_adjustment_clamped (adj, newval);
4720 cursor_x_pos += adj->value - oldval;
4722 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4723 clamp_iter_onscreen (text_view, &new_insert);
4724 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4726 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4729 /* FIXME for lines shorter than the overall widget width, this results in a
4730 * "bounce" effect as we scroll to the right of the widget, then scroll
4731 * back to get the end of the line onscreen.
4732 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4735 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4736 * only guarantees 1 pixel onscreen.
4738 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4739 gtk_text_view_scroll_mark_onscreen (text_view,
4740 gtk_text_buffer_get_mark (get_buffer (text_view),
4745 whitespace (gunichar ch, gpointer user_data)
4747 return (ch == ' ' || ch == '\t');
4751 not_whitespace (gunichar ch, gpointer user_data)
4753 return !whitespace (ch, user_data);
4757 find_whitepace_region (const GtkTextIter *center,
4758 GtkTextIter *start, GtkTextIter *end)
4763 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4764 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4765 if (whitespace (gtk_text_iter_get_char (end), NULL))
4766 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4768 return !gtk_text_iter_equal (start, end);
4772 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4775 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4776 text_view->editable);
4780 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4787 gboolean leave_one = FALSE;
4789 gtk_text_view_reset_im_context (text_view);
4791 if (type == GTK_DELETE_CHARS)
4793 /* Char delete deletes the selection, if one exists */
4794 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4795 text_view->editable))
4799 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4801 gtk_text_buffer_get_mark (get_buffer (text_view),
4809 case GTK_DELETE_CHARS:
4810 gtk_text_iter_forward_cursor_positions (&end, count);
4813 case GTK_DELETE_WORD_ENDS:
4815 gtk_text_iter_forward_word_ends (&end, count);
4817 gtk_text_iter_backward_word_starts (&start, 0 - count);
4820 case GTK_DELETE_WORDS:
4823 case GTK_DELETE_DISPLAY_LINE_ENDS:
4826 case GTK_DELETE_DISPLAY_LINES:
4829 case GTK_DELETE_PARAGRAPH_ENDS:
4830 /* If we're already at a newline, we need to
4831 * simply delete that newline, instead of
4832 * moving to the next one.
4834 if (gtk_text_iter_ends_line (&end))
4836 gtk_text_iter_forward_line (&end);
4842 if (!gtk_text_iter_forward_to_line_end (&end))
4848 /* FIXME figure out what a negative count means
4852 case GTK_DELETE_PARAGRAPHS:
4855 gtk_text_iter_set_line_offset (&start, 0);
4856 gtk_text_iter_forward_to_line_end (&end);
4858 /* Do the lines beyond the first. */
4861 gtk_text_iter_forward_to_line_end (&end);
4867 /* FIXME negative count? */
4871 case GTK_DELETE_WHITESPACE:
4873 find_whitepace_region (&insert, &start, &end);
4881 if (!gtk_text_iter_equal (&start, &end))
4883 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4885 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4886 text_view->editable))
4889 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4891 text_view->editable);
4894 gtk_text_buffer_end_user_action (get_buffer (text_view));
4896 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4897 gtk_text_view_scroll_mark_onscreen (text_view,
4898 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4903 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4905 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4906 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4907 text_view->editable);
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_copy_clipboard (GtkTextView *text_view)
4917 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4918 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
4919 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4920 gtk_text_view_scroll_mark_onscreen (text_view,
4921 gtk_text_buffer_get_mark (get_buffer (text_view),
4926 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4928 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4929 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4931 text_view->editable);
4932 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4933 gtk_text_view_scroll_mark_onscreen (text_view,
4934 gtk_text_buffer_get_mark (get_buffer (text_view),
4939 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4941 text_view->overwrite_mode = !text_view->overwrite_mode;
4945 gtk_text_view_move_focus (GtkTextView *text_view,
4946 GtkDirectionType direction_type)
4948 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
4950 if (!GTK_WIDGET_TOPLEVEL (toplevel))
4953 /* Propagate to toplevel */
4954 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus", direction_type);
4962 gtk_text_view_unselect (GtkTextView *text_view)
4966 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4968 gtk_text_buffer_get_mark (get_buffer (text_view),
4971 gtk_text_buffer_move_mark (get_buffer (text_view),
4972 gtk_text_buffer_get_mark (get_buffer (text_view),
4978 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
4979 const gchar *mark_name)
4982 GdkModifierType state;
4983 GtkTextIter newplace;
4985 /* DV(g_print (G_STRLOC": begin\n")); */
4987 gdk_window_get_pointer (text_view->text_window->bin_window,
4990 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
4991 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4993 x + text_view->xoffset,
4994 y + text_view->yoffset);
4998 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5000 /* This may invalidate the layout */
5001 DV(g_print (G_STRLOC": move mark\n"));
5002 gtk_text_buffer_move_mark (get_buffer (text_view),
5006 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5007 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5010 DV (g_print ("first validate idle leaving %s is %d\n",
5011 G_STRLOC, text_view->first_validate_idle));
5015 selection_scan_timeout (gpointer data)
5017 GtkTextView *text_view;
5019 GDK_THREADS_ENTER ();
5021 text_view = GTK_TEXT_VIEW (data);
5023 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5024 move_mark_to_pointer_and_scroll (text_view, "insert");
5026 GDK_THREADS_LEAVE ();
5028 return TRUE; /* remain installed. */
5031 #define DND_SCROLL_MARGIN 0.20
5034 drag_scan_timeout (gpointer data)
5036 GtkTextView *text_view;
5038 GdkModifierType state;
5039 GtkTextIter newplace;
5041 GDK_THREADS_ENTER ();
5043 text_view = GTK_TEXT_VIEW (data);
5045 gdk_window_get_pointer (text_view->text_window->bin_window,
5048 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5050 x + text_view->xoffset,
5051 y + text_view->yoffset);
5053 gtk_text_buffer_move_mark (get_buffer (text_view),
5054 text_view->dnd_mark,
5057 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5058 gtk_text_view_scroll_to_mark (text_view,
5059 text_view->dnd_mark,
5060 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5062 GDK_THREADS_LEAVE ();
5068 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5070 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5071 move_mark_to_pointer_and_scroll (text_view, "insert");
5073 /* If we had to scroll offscreen, insert a timeout to do so
5074 * again. Note that in the timeout, even if the mouse doesn't
5075 * move, due to this scroll xoffset/yoffset will have changed
5076 * and we'll need to scroll again.
5078 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5079 gtk_timeout_remove (text_view->scroll_timeout);
5081 text_view->scroll_timeout =
5082 gtk_timeout_add (50, selection_scan_timeout, text_view);
5088 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5089 const GtkTextIter *iter,
5090 GdkEventButton *button)
5092 GtkTextIter newplace;
5093 GtkTextBuffer *buffer;
5095 g_return_if_fail (text_view->selection_drag_handler == 0);
5097 gtk_grab_add (GTK_WIDGET (text_view));
5099 buffer = get_buffer (text_view);
5103 if (button->state & GDK_SHIFT_MASK)
5105 /* Extend selection */
5106 GtkTextIter start, end;
5108 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5110 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5112 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5115 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5118 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5120 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5123 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5129 /* Replace selection */
5130 gtk_text_buffer_place_cursor (buffer, &newplace);
5133 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5134 "motion_notify_event",
5135 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5139 /* returns whether we were really dragging */
5141 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5143 if (text_view->selection_drag_handler == 0)
5146 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5147 text_view->selection_drag_handler = 0;
5149 if (text_view->scroll_timeout != 0)
5151 gtk_timeout_remove (text_view->scroll_timeout);
5152 text_view->scroll_timeout = 0;
5155 /* one last update to current position */
5156 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5157 move_mark_to_pointer_and_scroll (text_view, "insert");
5159 gtk_grab_remove (GTK_WIDGET (text_view));
5169 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5170 GtkTextAttributes *values,
5173 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5174 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5177 pango_font_description_free (values->font);
5179 values->font = pango_font_description_copy (style->font_desc);
5183 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5185 if (text_view->layout)
5187 gboolean split_cursor;
5188 GtkTextDirection new_dir;
5189 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5191 g_object_get (G_OBJECT (settings),
5192 "gtk-split-cursor", &split_cursor,
5195 new_dir = GTK_TEXT_DIR_NONE;
5197 new_dir = (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
5198 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5200 if (text_view->layout->cursor_direction != new_dir)
5201 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5206 gtk_text_view_ensure_layout (GtkTextView *text_view)
5210 widget = GTK_WIDGET (text_view);
5212 if (text_view->layout == NULL)
5214 GtkTextAttributes *style;
5215 PangoContext *ltr_context, *rtl_context;
5218 DV(g_print(G_STRLOC"\n"));
5220 text_view->layout = gtk_text_layout_new ();
5222 g_signal_connect (G_OBJECT (text_view->layout),
5224 G_CALLBACK (invalidated_handler),
5227 g_signal_connect (G_OBJECT (text_view->layout),
5229 G_CALLBACK (changed_handler),
5232 g_signal_connect (G_OBJECT (text_view->layout),
5234 G_CALLBACK (gtk_text_view_child_allocated),
5237 if (get_buffer (text_view))
5238 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5240 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5241 gtk_text_view_pend_cursor_blink (text_view);
5243 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5245 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5246 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5247 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5248 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5250 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5252 g_object_unref (G_OBJECT (ltr_context));
5253 g_object_unref (G_OBJECT (rtl_context));
5255 gtk_text_view_check_keymap_direction (text_view);
5257 style = gtk_text_attributes_new ();
5259 gtk_widget_ensure_style (widget);
5260 gtk_text_view_set_attributes_from_style (text_view,
5261 style, widget->style);
5263 style->pixels_above_lines = text_view->pixels_above_lines;
5264 style->pixels_below_lines = text_view->pixels_below_lines;
5265 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5266 style->left_margin = text_view->left_margin;
5267 style->right_margin = text_view->right_margin;
5268 style->indent = text_view->indent;
5269 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5271 style->wrap_mode = text_view->wrap_mode;
5272 style->justification = text_view->justify;
5273 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5275 gtk_text_layout_set_default_style (text_view->layout, style);
5277 gtk_text_attributes_unref (style);
5279 /* Set layout for all anchored children */
5281 tmp_list = text_view->children;
5282 while (tmp_list != NULL)
5284 GtkTextViewChild *vc = tmp_list->data;
5288 gtk_text_anchored_child_set_layout (vc->widget,
5290 /* vc may now be invalid! */
5293 tmp_list = g_slist_next (tmp_list);
5299 * gtk_text_view_get_default_attributes:
5300 * @text_view: a #GtkTextView
5302 * Obtains a copy of the default text attributes. These are the
5303 * attributes used for text unless a tag overrides them.
5304 * You'd typically pass the default attributes in to
5305 * gtk_text_iter_get_attributes() in order to get the
5306 * attributes in effect at a given text position.
5308 * The return value is a copy owned by the caller of this function,
5309 * and should be freed.
5311 * Return value: a new #GtkTextAttributes
5314 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5316 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5318 gtk_text_view_ensure_layout (text_view);
5320 return gtk_text_attributes_copy (text_view->layout->default_style);
5324 gtk_text_view_destroy_layout (GtkTextView *text_view)
5326 if (text_view->layout)
5330 gtk_text_view_remove_validate_idles (text_view);
5332 /* Remove layout from all anchored children */
5333 tmp_list = text_view->children;
5334 while (tmp_list != NULL)
5336 GtkTextViewChild *vc = tmp_list->data;
5340 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5341 /* vc may now be invalid! */
5344 tmp_list = g_slist_next (tmp_list);
5347 gtk_text_view_stop_cursor_blink (text_view);
5348 gtk_text_view_end_selection_drag (text_view, NULL);
5350 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5351 invalidated_handler, text_view);
5352 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5353 changed_handler, text_view);
5354 g_object_unref (G_OBJECT (text_view->layout));
5355 text_view->layout = NULL;
5360 gtk_text_view_reset_im_context (GtkTextView *text_view)
5362 if (text_view->need_im_reset)
5364 text_view->need_im_reset = FALSE;
5365 gtk_im_context_reset (text_view->im_context);
5374 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5375 const GtkTextIter *iter,
5376 GdkEventMotion *event)
5378 GdkDragContext *context;
5379 GtkTargetList *target_list;
5381 text_view->drag_start_x = -1;
5382 text_view->drag_start_y = -1;
5383 text_view->pending_place_cursor_button = 0;
5385 target_list = gtk_target_list_new (target_table,
5386 G_N_ELEMENTS (target_table));
5388 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5389 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5390 1, (GdkEvent*)event);
5392 gtk_target_list_unref (target_list);
5394 gtk_drag_set_icon_default (context);
5398 gtk_text_view_drag_begin (GtkWidget *widget,
5399 GdkDragContext *context)
5405 gtk_text_view_drag_end (GtkWidget *widget,
5406 GdkDragContext *context)
5408 GtkTextView *text_view;
5410 text_view = GTK_TEXT_VIEW (widget);
5414 gtk_text_view_drag_data_get (GtkWidget *widget,
5415 GdkDragContext *context,
5416 GtkSelectionData *selection_data,
5420 GtkTextView *text_view;
5422 text_view = GTK_TEXT_VIEW (widget);
5424 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5426 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5428 gtk_selection_data_set (selection_data,
5429 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5442 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5445 /* Extract the selected text */
5446 str = gtk_text_iter_get_visible_text (&start, &end);
5451 gtk_selection_data_set_text (selection_data, str, -1);
5458 gtk_text_view_drag_data_delete (GtkWidget *widget,
5459 GdkDragContext *context)
5461 GtkTextView *text_view;
5463 text_view = GTK_TEXT_VIEW (widget);
5465 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5466 TRUE, GTK_TEXT_VIEW (widget)->editable);
5470 gtk_text_view_drag_leave (GtkWidget *widget,
5471 GdkDragContext *context,
5474 GtkTextView *text_view;
5476 text_view = GTK_TEXT_VIEW (widget);
5478 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5480 if (text_view->scroll_timeout != 0)
5481 gtk_timeout_remove (text_view->scroll_timeout);
5483 text_view->scroll_timeout = 0;
5487 gtk_text_view_drag_motion (GtkWidget *widget,
5488 GdkDragContext *context,
5493 GtkTextIter newplace;
5494 GtkTextView *text_view;
5497 GdkRectangle target_rect;
5499 GdkDragAction suggested_action = 0;
5501 text_view = GTK_TEXT_VIEW (widget);
5503 target_rect = text_view->text_window->allocation;
5505 if (x < target_rect.x ||
5506 y < target_rect.y ||
5507 x > (target_rect.x + target_rect.width) ||
5508 y > (target_rect.y + target_rect.height))
5509 return FALSE; /* outside the text window, allow parent widgets to handle event */
5511 gtk_text_view_window_to_buffer_coords (text_view,
5512 GTK_TEXT_WINDOW_WIDGET,
5516 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5520 if (gtk_drag_dest_find_target (widget, context,
5521 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5523 /* can't accept any of the offered targets */
5525 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5527 gtk_text_iter_in_range (&newplace, &start, &end))
5529 /* We're inside the selection. */
5533 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5535 GtkWidget *source_widget;
5537 suggested_action = context->suggested_action;
5539 source_widget = gtk_drag_get_source_widget (context);
5541 if (source_widget == widget)
5543 /* Default to MOVE, unless the user has
5544 * pressed ctrl or alt to affect available actions
5546 if ((context->actions & GDK_ACTION_MOVE) != 0)
5547 suggested_action = GDK_ACTION_MOVE;
5552 /* Can't drop here. */
5556 if (suggested_action != 0)
5558 gtk_text_mark_set_visible (text_view->dnd_mark,
5559 text_view->cursor_visible);
5561 gdk_drag_status (context, suggested_action, time);
5565 gdk_drag_status (context, 0, time);
5566 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5569 gtk_text_buffer_move_mark (get_buffer (text_view),
5570 text_view->dnd_mark,
5573 DV(g_print (G_STRLOC": scrolling to mark\n"));
5574 gtk_text_view_scroll_to_mark (text_view,
5575 text_view->dnd_mark,
5576 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5578 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5579 gtk_timeout_remove (text_view->scroll_timeout);
5581 text_view->scroll_timeout =
5582 gtk_timeout_add (50, drag_scan_timeout, text_view);
5584 /* TRUE return means don't propagate the drag motion to parent
5585 * widgets that may also be drop sites.
5591 gtk_text_view_drag_drop (GtkWidget *widget,
5592 GdkDragContext *context,
5597 GtkTextView *text_view;
5598 GtkTextIter drop_point;
5599 GdkAtom target = GDK_NONE;
5601 text_view = GTK_TEXT_VIEW (widget);
5603 if (text_view->scroll_timeout != 0)
5604 gtk_timeout_remove (text_view->scroll_timeout);
5606 text_view->scroll_timeout = 0;
5608 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5610 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5612 text_view->dnd_mark);
5614 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5615 target = gtk_drag_dest_find_target (widget, context, NULL);
5617 if (target != GDK_NONE)
5618 gtk_drag_get_data (widget, context, target, time);
5620 gtk_drag_finish (context, FALSE, FALSE, time);
5626 insert_text_data (GtkTextView *text_view,
5627 GtkTextIter *drop_point,
5628 GtkSelectionData *selection_data)
5632 str = gtk_selection_data_get_text (selection_data);
5636 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5637 drop_point, str, -1,
5638 text_view->editable);
5644 gtk_text_view_drag_data_received (GtkWidget *widget,
5645 GdkDragContext *context,
5648 GtkSelectionData *selection_data,
5652 GtkTextIter drop_point;
5653 GtkTextView *text_view;
5654 gboolean success = FALSE;
5656 text_view = GTK_TEXT_VIEW (widget);
5658 if (!text_view->dnd_mark)
5661 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5663 text_view->dnd_mark);
5665 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5668 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5670 GtkTextBuffer *src_buffer = NULL;
5671 GtkTextIter start, end;
5672 gboolean copy_tags = TRUE;
5674 if (selection_data->length != sizeof (src_buffer))
5677 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5679 if (src_buffer == NULL)
5682 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5684 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5685 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5688 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5693 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5697 text_view->editable);
5702 str = gtk_text_iter_get_visible_text (&start, &end);
5703 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5704 &drop_point, str, -1,
5705 text_view->editable);
5711 insert_text_data (text_view, &drop_point, selection_data);
5716 gtk_drag_finish (context, success,
5717 success && context->action == GDK_ACTION_MOVE,
5721 static GtkAdjustment*
5722 get_hadjustment (GtkTextView *text_view)
5724 if (text_view->hadjustment == NULL)
5725 gtk_text_view_set_scroll_adjustments (text_view,
5726 NULL, /* forces creation */
5727 text_view->vadjustment);
5729 return text_view->hadjustment;
5732 static GtkAdjustment*
5733 get_vadjustment (GtkTextView *text_view)
5735 if (text_view->vadjustment == NULL)
5736 gtk_text_view_set_scroll_adjustments (text_view,
5737 text_view->hadjustment,
5738 NULL); /* forces creation */
5739 return text_view->vadjustment;
5744 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5745 GtkAdjustment *hadj,
5746 GtkAdjustment *vadj)
5748 gboolean need_adjust = FALSE;
5750 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5753 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5755 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5757 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5759 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5761 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5763 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5764 g_object_unref (G_OBJECT (text_view->hadjustment));
5767 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5769 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5770 g_object_unref (G_OBJECT (text_view->vadjustment));
5773 if (text_view->hadjustment != hadj)
5775 text_view->hadjustment = hadj;
5776 g_object_ref (G_OBJECT (text_view->hadjustment));
5777 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5779 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5780 (GtkSignalFunc) gtk_text_view_value_changed,
5785 if (text_view->vadjustment != vadj)
5787 text_view->vadjustment = vadj;
5788 g_object_ref (G_OBJECT (text_view->vadjustment));
5789 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5791 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5792 (GtkSignalFunc) gtk_text_view_value_changed,
5798 gtk_text_view_value_changed (NULL, text_view);
5802 gtk_text_view_value_changed (GtkAdjustment *adj,
5803 GtkTextView *text_view)
5810 /* Note that we oddly call this function with adj == NULL
5814 text_view->onscreen_validated = FALSE;
5816 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5817 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5818 adj ? adj->value : 0.0));
5820 if (adj == text_view->hadjustment)
5822 dx = text_view->xoffset - (gint)adj->value;
5823 text_view->xoffset = adj->value;
5825 else if (adj == text_view->vadjustment)
5827 dy = text_view->yoffset - (gint)adj->value;
5828 text_view->yoffset = adj->value;
5830 if (text_view->layout)
5832 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5834 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5836 text_view->first_para_pixels = adj->value - line_top;
5840 if (dx != 0 || dy != 0)
5844 if (GTK_WIDGET_REALIZED (text_view))
5848 if (text_view->left_window)
5849 text_window_scroll (text_view->left_window, 0, dy);
5850 if (text_view->right_window)
5851 text_window_scroll (text_view->right_window, 0, dy);
5856 if (text_view->top_window)
5857 text_window_scroll (text_view->top_window, dx, 0);
5858 if (text_view->bottom_window)
5859 text_window_scroll (text_view->bottom_window, dx, 0);
5862 /* It looks nicer to scroll the main area last, because
5863 * it takes a while, and making the side areas update
5864 * afterward emphasizes the slowness of scrolling the
5867 text_window_scroll (text_view->text_window, dx, dy);
5870 /* Children are now "moved" in the text window, poke
5871 * into widget->allocation for each child
5873 tmp_list = text_view->children;
5874 while (tmp_list != NULL)
5876 GtkTextViewChild *child = tmp_list->data;
5880 child->widget->allocation.x -= dx;
5881 child->widget->allocation.y -= dy;
5884 g_print ("allocation for %p tweaked to %d,%d\n",
5886 child->widget->allocation.x,
5887 child->widget->allocation.y);
5891 tmp_list = g_slist_next (tmp_list);
5895 /* This could result in invalidation, which would install the
5896 * first_validate_idle, which would validate onscreen;
5897 * but we're going to go ahead and validate here, so
5898 * first_validate_idle shouldn't have anything to do.
5900 gtk_text_view_update_layout_width (text_view);
5902 /* note that validation of onscreen could invoke this function
5903 * recursively, by scrolling to maintain first_para, or in response
5904 * to updating the layout width, however there is no problem with
5905 * that, or shouldn't be.
5907 gtk_text_view_validate_onscreen (text_view);
5909 /* process exposes */
5910 if (GTK_WIDGET_REALIZED (text_view))
5912 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
5914 if (text_view->left_window)
5915 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
5917 if (text_view->right_window)
5918 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
5920 if (text_view->top_window)
5921 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
5923 if (text_view->bottom_window)
5924 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
5926 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
5929 /* If this got installed, get rid of it, it's just a waste of time. */
5930 if (text_view->first_validate_idle != 0)
5932 g_source_remove (text_view->first_validate_idle);
5933 text_view->first_validate_idle = 0;
5936 gtk_text_view_update_im_spot_location (text_view);
5938 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
5942 gtk_text_view_commit_handler (GtkIMContext *context,
5944 GtkTextView *text_view)
5946 gtk_text_view_commit_text (text_view, str);
5950 gtk_text_view_commit_text (GtkTextView *text_view,
5953 gboolean had_selection;
5955 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5957 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5960 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5961 text_view->editable);
5963 if (!strcmp (str, "\n"))
5965 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
5966 text_view->editable);
5970 if (!had_selection && text_view->overwrite_mode)
5971 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
5972 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5973 text_view->editable);
5976 gtk_text_buffer_end_user_action (get_buffer (text_view));
5978 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5979 gtk_text_view_scroll_mark_onscreen (text_view,
5980 gtk_text_buffer_get_mark (get_buffer (text_view),
5985 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
5986 GtkTextView *text_view)
5989 PangoAttrList *attrs;
5992 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
5993 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
5995 pango_attr_list_unref (attrs);
6000 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6001 GtkTextView *text_view)
6008 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6009 gtk_text_buffer_get_insert (text_view->buffer));
6012 pos = gtk_text_iter_get_line_index (&start);
6013 gtk_text_iter_set_line_offset (&start, 0);
6014 gtk_text_iter_forward_to_line_end (&end);
6016 text = gtk_text_iter_get_slice (&start, &end);
6017 gtk_im_context_set_surrounding (context, text, -1, pos);
6024 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6027 GtkTextView *text_view)
6032 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6033 gtk_text_buffer_get_insert (text_view->buffer));
6036 gtk_text_iter_forward_chars (&start, offset);
6037 gtk_text_iter_forward_chars (&end, offset + n_chars);
6039 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6045 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6046 const GtkTextIter *location,
6050 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6051 gboolean need_reset = FALSE;
6053 if (mark == gtk_text_buffer_get_insert (buffer))
6055 text_view->virtual_cursor_x = -1;
6056 text_view->virtual_cursor_y = -1;
6057 gtk_text_view_update_im_spot_location (text_view);
6060 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6066 gtk_text_view_reset_im_context (text_view);
6070 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6074 GdkRectangle strong_pos;
6077 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6078 gtk_text_buffer_get_mark (get_buffer (text_view),
6081 if ((x && text_view->virtual_cursor_x == -1) ||
6082 (y && text_view->virtual_cursor_y == -1))
6083 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6087 if (text_view->virtual_cursor_x != -1)
6088 *x = text_view->virtual_cursor_x;
6095 if (text_view->virtual_cursor_x != -1)
6096 *y = text_view->virtual_cursor_y;
6098 *y = strong_pos.y + strong_pos.height / 2;
6103 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6107 GdkRectangle strong_pos;
6110 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6111 gtk_text_buffer_get_mark (get_buffer (text_view),
6114 if (x == -1 || y == -1)
6115 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6117 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6118 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6121 /* Quick hack of a popup menu
6124 activate_cb (GtkWidget *menuitem,
6125 GtkTextView *text_view)
6127 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6128 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6132 append_action_signal (GtkTextView *text_view,
6134 const gchar *stock_id,
6135 const gchar *signal,
6138 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6140 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6141 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6142 GTK_SIGNAL_FUNC (activate_cb), text_view);
6144 gtk_widget_set_sensitive (menuitem, sensitive);
6146 gtk_widget_show (menuitem);
6147 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6151 popup_menu_detach (GtkWidget *attach_widget,
6154 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6158 popup_position_func (GtkMenu *menu,
6164 GtkTextView *text_view;
6166 GdkRectangle cursor_rect;
6167 GdkRectangle onscreen_rect;
6168 gint root_x, root_y;
6172 text_view = GTK_TEXT_VIEW (user_data);
6173 widget = GTK_WIDGET (text_view);
6175 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6177 gdk_window_get_origin (widget->window, &root_x, &root_y);
6179 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6181 gtk_text_buffer_get_insert (get_buffer (text_view)));
6183 gtk_text_view_get_iter_location (text_view,
6187 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6189 gtk_widget_size_request (text_view->popup_menu, &req);
6191 /* can't use rectangle_intersect since cursor rect can have 0 width */
6192 if (cursor_rect.x >= onscreen_rect.x &&
6193 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6194 cursor_rect.y >= onscreen_rect.y &&
6195 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6197 gtk_text_view_buffer_to_window_coords (text_view,
6198 GTK_TEXT_WINDOW_WIDGET,
6199 cursor_rect.x, cursor_rect.y,
6200 &cursor_rect.x, &cursor_rect.y);
6202 *x = root_x + cursor_rect.x + cursor_rect.width;
6203 *y = root_y + cursor_rect.y + cursor_rect.height;
6207 /* Just center the menu, since cursor is offscreen. */
6208 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6209 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6213 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6214 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6216 *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
6217 *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
6222 GtkTextView *text_view;
6228 range_contains_editable_text (const GtkTextIter *start,
6229 const GtkTextIter *end,
6230 gboolean default_editability)
6232 GtkTextIter iter = *start;
6234 while (gtk_text_iter_compare (&iter, end) < 0)
6236 if (gtk_text_iter_editable (&iter, default_editability))
6239 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6246 unichar_chosen_func (const char *text,
6249 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6251 gtk_text_view_commit_text (text_view, text);
6255 popup_targets_received (GtkClipboard *clipboard,
6256 GtkSelectionData *data,
6259 PopupInfo *info = user_data;
6260 GtkTextView *text_view = info->text_view;
6262 if (GTK_WIDGET_REALIZED (text_view))
6264 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6265 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6267 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6268 GtkWidget *menuitem;
6270 gboolean have_selection;
6271 gboolean can_insert;
6273 GtkTextIter sel_start, sel_end;
6275 if (text_view->popup_menu)
6276 gtk_widget_destroy (text_view->popup_menu);
6278 text_view->popup_menu = gtk_menu_new ();
6280 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6281 GTK_WIDGET (text_view),
6284 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6285 &sel_start, &sel_end);
6287 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6289 gtk_text_buffer_get_insert (get_buffer (text_view)));
6291 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6293 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6295 range_contains_editable_text (&sel_start, &sel_end,
6296 text_view->editable));
6297 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6299 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6300 can_insert && clipboard_contains_text);
6302 menuitem = gtk_separator_menu_item_new ();
6303 gtk_widget_show (menuitem);
6304 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6306 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6307 gtk_widget_show (menuitem);
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_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6313 GTK_MENU_SHELL (submenu));
6315 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6316 gtk_widget_show (menuitem);
6317 gtk_widget_set_sensitive (menuitem, can_insert);
6319 submenu = gtk_menu_new ();
6320 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6321 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6323 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6324 unichar_chosen_func,
6327 gtk_signal_emit (GTK_OBJECT (text_view),
6328 signals[POPULATE_POPUP],
6329 text_view->popup_menu);
6332 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6334 info->button, info->time);
6337 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6338 popup_position_func, text_view,
6339 0, gtk_get_current_event_time ());
6340 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu));
6344 g_object_unref (text_view);
6349 gtk_text_view_do_popup (GtkTextView *text_view,
6350 GdkEventButton *event)
6352 PopupInfo *info = g_new (PopupInfo, 1);
6354 /* In order to know what entries we should make sensitive, we
6355 * ask for the current targets of the clipboard, and when
6356 * we get them, then we actually pop up the menu.
6358 info->text_view = g_object_ref (text_view);
6362 info->button = event->button;
6363 info->time = event->time;
6368 info->time = gtk_get_current_event_time ();
6371 gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
6372 gdk_atom_intern ("TARGETS", FALSE),
6373 popup_targets_received,
6378 gtk_text_view_popup_menu (GtkWidget *widget)
6380 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6384 /* Child GdkWindows */
6387 static GtkTextWindow*
6388 text_window_new (GtkTextWindowType type,
6391 gint height_request)
6395 win = g_new (GtkTextWindow, 1);
6398 win->widget = widget;
6400 win->bin_window = NULL;
6401 win->requisition.width = width_request;
6402 win->requisition.height = height_request;
6403 win->allocation.width = width_request;
6404 win->allocation.height = height_request;
6405 win->allocation.x = 0;
6406 win->allocation.y = 0;
6412 text_window_free (GtkTextWindow *win)
6415 text_window_unrealize (win);
6421 text_window_realize (GtkTextWindow *win,
6424 GdkWindowAttr attributes;
6425 gint attributes_mask;
6428 attributes.window_type = GDK_WINDOW_CHILD;
6429 attributes.x = win->allocation.x;
6430 attributes.y = win->allocation.y;
6431 attributes.width = win->allocation.width;
6432 attributes.height = win->allocation.height;
6433 attributes.wclass = GDK_INPUT_OUTPUT;
6434 attributes.visual = gtk_widget_get_visual (win->widget);
6435 attributes.colormap = gtk_widget_get_colormap (win->widget);
6436 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6438 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6440 win->window = gdk_window_new (parent,
6444 gdk_window_show (win->window);
6445 gdk_window_set_user_data (win->window, win->widget);
6449 attributes.width = win->allocation.width;
6450 attributes.height = win->allocation.height;
6451 attributes.event_mask = (GDK_EXPOSURE_MASK |
6453 GDK_KEY_PRESS_MASK |
6454 GDK_BUTTON_PRESS_MASK |
6455 GDK_BUTTON_RELEASE_MASK |
6456 GDK_POINTER_MOTION_MASK |
6457 GDK_POINTER_MOTION_HINT_MASK |
6458 gtk_widget_get_events (win->widget));
6460 win->bin_window = gdk_window_new (win->window,
6464 gdk_window_show (win->bin_window);
6465 gdk_window_set_user_data (win->bin_window, win->widget);
6467 if (win->type == GTK_TEXT_WINDOW_TEXT)
6470 cursor = gdk_cursor_new (GDK_XTERM);
6471 gdk_window_set_cursor (win->bin_window, cursor);
6472 gdk_cursor_unref (cursor);
6474 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6478 gdk_window_set_background (win->bin_window,
6479 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6483 gdk_window_set_background (win->bin_window,
6484 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6487 g_object_set_qdata (G_OBJECT (win->window),
6488 g_quark_from_static_string ("gtk-text-view-text-window"),
6491 g_object_set_qdata (G_OBJECT (win->bin_window),
6492 g_quark_from_static_string ("gtk-text-view-text-window"),
6497 text_window_unrealize (GtkTextWindow *win)
6499 if (win->type == GTK_TEXT_WINDOW_TEXT)
6501 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6505 gdk_window_set_user_data (win->window, NULL);
6506 gdk_window_set_user_data (win->bin_window, NULL);
6507 gdk_window_destroy (win->bin_window);
6508 gdk_window_destroy (win->window);
6510 win->bin_window = NULL;
6514 text_window_size_allocate (GtkTextWindow *win,
6517 win->allocation = *rect;
6521 gdk_window_move_resize (win->window,
6523 rect->width, rect->height);
6525 gdk_window_resize (win->bin_window,
6526 rect->width, rect->height);
6531 text_window_scroll (GtkTextWindow *win,
6535 if (dx != 0 || dy != 0)
6537 gdk_window_scroll (win->bin_window, dx, dy);
6542 text_window_invalidate_rect (GtkTextWindow *win,
6545 GdkRectangle window_rect;
6547 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6554 window_rect.width = rect->width;
6555 window_rect.height = rect->height;
6557 /* Adjust the rect as appropriate */
6561 case GTK_TEXT_WINDOW_TEXT:
6564 case GTK_TEXT_WINDOW_LEFT:
6565 case GTK_TEXT_WINDOW_RIGHT:
6567 window_rect.width = win->allocation.width;
6570 case GTK_TEXT_WINDOW_TOP:
6571 case GTK_TEXT_WINDOW_BOTTOM:
6573 window_rect.height = win->allocation.height;
6577 g_warning ("%s: bug!", G_STRLOC);
6582 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6586 GdkColor color = { 0, 65535, 0, 0 };
6587 GdkGC *gc = gdk_gc_new (win->bin_window);
6588 gdk_gc_set_rgb_fg_color (gc, &color);
6589 gdk_draw_rectangle (win->bin_window,
6590 gc, TRUE, window_rect.x, window_rect.y,
6591 window_rect.width, window_rect.height);
6598 text_window_get_width (GtkTextWindow *win)
6600 return win->allocation.width;
6604 text_window_get_height (GtkTextWindow *win)
6606 return win->allocation.height;
6610 text_window_get_allocation (GtkTextWindow *win,
6613 *rect = win->allocation;
6620 * gtk_text_view_get_window:
6621 * @text_view: a #GtkTextView
6622 * @win: window to get
6624 * Retrieves the #GdkWindow corresponding to an area of the text view;
6625 * possible windows include the overall widget window, child windows
6626 * on the left, right, top, bottom, and the window that displays the
6627 * text buffer. Windows are %NULL and nonexistent if their width or
6628 * height is 0, and are nonexistent before the widget has been
6631 * Return value: a #GdkWindow, or %NULL
6634 gtk_text_view_get_window (GtkTextView *text_view,
6635 GtkTextWindowType win)
6637 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6641 case GTK_TEXT_WINDOW_WIDGET:
6642 return GTK_WIDGET (text_view)->window;
6645 case GTK_TEXT_WINDOW_TEXT:
6646 return text_view->text_window->bin_window;
6649 case GTK_TEXT_WINDOW_LEFT:
6650 if (text_view->left_window)
6651 return text_view->left_window->bin_window;
6656 case GTK_TEXT_WINDOW_RIGHT:
6657 if (text_view->right_window)
6658 return text_view->right_window->bin_window;
6663 case GTK_TEXT_WINDOW_TOP:
6664 if (text_view->top_window)
6665 return text_view->top_window->bin_window;
6670 case GTK_TEXT_WINDOW_BOTTOM:
6671 if (text_view->bottom_window)
6672 return text_view->bottom_window->bin_window;
6677 case GTK_TEXT_WINDOW_PRIVATE:
6678 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6683 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6688 * gtk_text_view_get_window_type:
6689 * @text_view: a #GtkTextView
6690 * @window: a window type
6692 * Usually used to find out which window an event corresponds to.
6693 * If you connect to an event signal on @text_view, this function
6694 * should be called on <literal>event->window</literal> to
6695 * see which window it was.
6697 * Return value: the window type.
6700 gtk_text_view_get_window_type (GtkTextView *text_view,
6705 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6706 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6708 if (window == GTK_WIDGET (text_view)->window)
6709 return GTK_TEXT_WINDOW_WIDGET;
6711 win = g_object_get_qdata (G_OBJECT (window),
6712 g_quark_try_string ("gtk-text-view-text-window"));
6718 return GTK_TEXT_WINDOW_PRIVATE;
6723 buffer_to_widget (GtkTextView *text_view,
6729 gint focus_edge_width;
6730 gboolean interior_focus;
6733 gtk_widget_style_get (GTK_WIDGET (text_view),
6734 "interior_focus", &interior_focus,
6735 "focus_line_width", &focus_width,
6739 focus_edge_width = 0;
6741 focus_edge_width = focus_width;
6745 *window_x = buffer_x - text_view->xoffset + focus_edge_width;
6746 if (text_view->left_window)
6747 *window_x += text_view->left_window->allocation.width;
6752 *window_y = buffer_y - text_view->yoffset + focus_edge_width;
6753 if (text_view->top_window)
6754 *window_y += text_view->top_window->allocation.height;
6759 widget_to_text_window (GtkTextWindow *win,
6766 *window_x = widget_x - win->allocation.x;
6769 *window_y = widget_y - win->allocation.y;
6773 buffer_to_text_window (GtkTextView *text_view,
6782 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6783 "for a nonexistent or private child window of GtkTextView");
6787 buffer_to_widget (text_view,
6789 window_x, window_y);
6791 widget_to_text_window (win,
6792 window_x ? *window_x : 0,
6793 window_y ? *window_y : 0,
6799 * gtk_text_view_buffer_to_window_coords:
6800 * @text_view: a #GtkTextView
6801 * @win: a #GtkTextWindowType
6802 * @buffer_x: buffer x coordinate
6803 * @buffer_y: buffer y coordinate
6804 * @window_x: window x coordinate return location
6805 * @window_y: window y coordinate return location
6807 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6808 * @win, and stores the result in (@window_x, @window_y).
6811 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6812 GtkTextWindowType win,
6818 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6822 case GTK_TEXT_WINDOW_WIDGET:
6823 buffer_to_widget (text_view,
6825 window_x, window_y);
6828 case GTK_TEXT_WINDOW_TEXT:
6830 *window_x = buffer_x - text_view->xoffset;
6832 *window_y = buffer_y - text_view->yoffset;
6835 case GTK_TEXT_WINDOW_LEFT:
6836 buffer_to_text_window (text_view,
6837 text_view->left_window,
6839 window_x, window_y);
6842 case GTK_TEXT_WINDOW_RIGHT:
6843 buffer_to_text_window (text_view,
6844 text_view->right_window,
6846 window_x, window_y);
6849 case GTK_TEXT_WINDOW_TOP:
6850 buffer_to_text_window (text_view,
6851 text_view->top_window,
6853 window_x, window_y);
6856 case GTK_TEXT_WINDOW_BOTTOM:
6857 buffer_to_text_window (text_view,
6858 text_view->bottom_window,
6860 window_x, window_y);
6863 case GTK_TEXT_WINDOW_PRIVATE:
6864 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6868 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6874 widget_to_buffer (GtkTextView *text_view,
6880 gint focus_edge_width;
6881 gboolean interior_focus;
6884 gtk_widget_style_get (GTK_WIDGET (text_view),
6885 "interior_focus", &interior_focus,
6886 "focus_line_width", &focus_width,
6890 focus_edge_width = 0;
6892 focus_edge_width = focus_width;
6896 *buffer_x = widget_x - focus_edge_width + text_view->xoffset;
6897 if (text_view->left_window)
6898 *buffer_x -= text_view->left_window->allocation.width;
6903 *buffer_y = widget_y - focus_edge_width + text_view->yoffset;
6904 if (text_view->top_window)
6905 *buffer_y -= text_view->top_window->allocation.height;
6910 text_window_to_widget (GtkTextWindow *win,
6917 *widget_x = window_x + win->allocation.x;
6920 *widget_y = window_y + win->allocation.y;
6924 text_window_to_buffer (GtkTextView *text_view,
6933 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
6934 "coordinates for a nonexistent child window.");
6938 text_window_to_widget (win,
6944 widget_to_buffer (text_view,
6945 buffer_x ? *buffer_x : 0,
6946 buffer_y ? *buffer_y : 0,
6952 * gtk_text_view_window_to_buffer_coords:
6953 * @text_view: a #GtkTextView
6954 * @win: a #GtkTextWindowType
6955 * @window_x: window x coordinate
6956 * @window_y: window y coordinate
6957 * @buffer_x: buffer x coordinate return location
6958 * @buffer_y: buffer y coordinate return location
6960 * Converts coordinates on the window identified by @win to buffer
6961 * coordinates, storing the result in (@buffer_x,@buffer_y).
6964 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
6965 GtkTextWindowType win,
6971 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6975 case GTK_TEXT_WINDOW_WIDGET:
6976 widget_to_buffer (text_view,
6978 buffer_x, buffer_y);
6981 case GTK_TEXT_WINDOW_TEXT:
6983 *buffer_x = window_x + text_view->xoffset;
6985 *buffer_y = window_y + text_view->yoffset;
6988 case GTK_TEXT_WINDOW_LEFT:
6989 text_window_to_buffer (text_view,
6990 text_view->left_window,
6992 buffer_x, buffer_y);
6995 case GTK_TEXT_WINDOW_RIGHT:
6996 text_window_to_buffer (text_view,
6997 text_view->right_window,
6999 buffer_x, buffer_y);
7002 case GTK_TEXT_WINDOW_TOP:
7003 text_window_to_buffer (text_view,
7004 text_view->top_window,
7006 buffer_x, buffer_y);
7009 case GTK_TEXT_WINDOW_BOTTOM:
7010 text_window_to_buffer (text_view,
7011 text_view->bottom_window,
7013 buffer_x, buffer_y);
7016 case GTK_TEXT_WINDOW_PRIVATE:
7017 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7021 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7027 set_window_width (GtkTextView *text_view,
7029 GtkTextWindowType type,
7030 GtkTextWindow **winp)
7036 text_window_free (*winp);
7038 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7045 *winp = text_window_new (type,
7046 GTK_WIDGET (text_view),
7048 /* if the widget is already realized we need to realize the child manually */
7049 if (GTK_WIDGET_REALIZED (text_view))
7050 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7054 if ((*winp)->requisition.width == width)
7057 (*winp)->requisition.width = width;
7060 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7066 set_window_height (GtkTextView *text_view,
7068 GtkTextWindowType type,
7069 GtkTextWindow **winp)
7075 text_window_free (*winp);
7077 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7084 *winp = text_window_new (type,
7085 GTK_WIDGET (text_view),
7088 /* if the widget is already realized we need to realize the child manually */
7089 if (GTK_WIDGET_REALIZED (text_view))
7090 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7094 if ((*winp)->requisition.height == height)
7097 (*winp)->requisition.height = height;
7100 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7105 * gtk_text_view_set_border_window_size:
7106 * @text_view: a #GtkTextView
7107 * @type: window to affect
7108 * @size: width or height of the window
7110 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7111 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7112 * Automatically destroys the corresponding window if the size is set
7113 * to 0, and creates the window if the size is set to non-zero. This
7114 * function can only be used for the "border windows," it doesn't work
7115 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7116 * #GTK_TEXT_WINDOW_PRIVATE.
7119 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7120 GtkTextWindowType type,
7124 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7125 g_return_if_fail (size >= 0);
7129 case GTK_TEXT_WINDOW_LEFT:
7130 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7131 &text_view->left_window);
7134 case GTK_TEXT_WINDOW_RIGHT:
7135 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7136 &text_view->right_window);
7139 case GTK_TEXT_WINDOW_TOP:
7140 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7141 &text_view->top_window);
7144 case GTK_TEXT_WINDOW_BOTTOM:
7145 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7146 &text_view->bottom_window);
7150 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7156 * gtk_text_view_get_border_window_size:
7157 * @text_view: a #GtkTextView
7158 * @type: window to return size from
7160 * Gets the width of the specified border window. See
7161 * gtk_text_view_set_border_window_size().
7163 * Return value: width of window
7166 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7167 GtkTextWindowType type)
7169 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7173 case GTK_TEXT_WINDOW_LEFT:
7174 if (text_view->left_window)
7175 return text_view->left_window->requisition.width;
7177 case GTK_TEXT_WINDOW_RIGHT:
7178 if (text_view->right_window)
7179 return text_view->right_window->requisition.width;
7181 case GTK_TEXT_WINDOW_TOP:
7182 if (text_view->top_window)
7183 return text_view->top_window->requisition.height;
7185 case GTK_TEXT_WINDOW_BOTTOM:
7186 if (text_view->bottom_window)
7187 return text_view->bottom_window->requisition.height;
7190 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7201 static GtkTextViewChild*
7202 text_view_child_new_anchored (GtkWidget *child,
7203 GtkTextChildAnchor *anchor,
7204 GtkTextLayout *layout)
7206 GtkTextViewChild *vc;
7208 vc = g_new (GtkTextViewChild, 1);
7211 vc->anchor = anchor;
7213 vc->from_top_of_line = 0;
7214 vc->from_left_of_buffer = 0;
7216 g_object_ref (G_OBJECT (vc->widget));
7217 g_object_ref (G_OBJECT (vc->anchor));
7219 g_object_set_data (G_OBJECT (child),
7220 "gtk-text-view-child",
7223 gtk_text_child_anchor_register_child (anchor, child, layout);
7228 static GtkTextViewChild*
7229 text_view_child_new_window (GtkWidget *child,
7230 GtkTextWindowType type,
7234 GtkTextViewChild *vc;
7236 vc = g_new (GtkTextViewChild, 1);
7241 vc->from_top_of_line = 0;
7242 vc->from_left_of_buffer = 0;
7244 g_object_ref (G_OBJECT (vc->widget));
7250 g_object_set_data (G_OBJECT (child),
7251 "gtk-text-view-child",
7258 text_view_child_free (GtkTextViewChild *child)
7260 g_object_set_data (G_OBJECT (child->widget),
7261 "gtk-text-view-child", NULL);
7265 gtk_text_child_anchor_unregister_child (child->anchor,
7267 g_object_unref (G_OBJECT (child->anchor));
7270 g_object_unref (G_OBJECT (child->widget));
7276 text_view_child_set_parent_window (GtkTextView *text_view,
7277 GtkTextViewChild *vc)
7280 gtk_widget_set_parent_window (vc->widget,
7281 text_view->text_window->bin_window);
7285 window = gtk_text_view_get_window (text_view,
7287 gtk_widget_set_parent_window (vc->widget, window);
7292 add_child (GtkTextView *text_view,
7293 GtkTextViewChild *vc)
7295 text_view->children = g_slist_prepend (text_view->children,
7298 if (GTK_WIDGET_REALIZED (text_view))
7299 text_view_child_set_parent_window (text_view, vc);
7301 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7305 * gtk_text_view_add_child_at_anchor:
7306 * @text_view: a #GtkTextView
7307 * @child: a #GtkWidget
7308 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7310 * Adds a child widget in the text buffer, at the given @anchor.
7314 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7316 GtkTextChildAnchor *anchor)
7318 GtkTextViewChild *vc;
7320 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7321 g_return_if_fail (GTK_IS_WIDGET (child));
7322 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7323 g_return_if_fail (child->parent == NULL);
7325 gtk_text_view_ensure_layout (text_view);
7327 vc = text_view_child_new_anchored (child, anchor,
7330 add_child (text_view, vc);
7332 g_assert (vc->widget == child);
7333 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7337 * gtk_text_view_add_child_in_window:
7338 * @text_view: a #GtkTextView
7339 * @child: a #GtkWidget
7340 * @which_window: which window the child should appear in
7341 * @xpos: X position of child in window coordinates
7342 * @ypos: Y position of child in window coordinates
7344 * Adds a child at fixed coordinates in one of the text widget's
7345 * windows. The window must have nonzero size (see
7346 * gtk_text_view_set_border_window_size()). Note that the child
7347 * coordinates are given relative to the #GdkWindow in question, and
7348 * that these coordinates have no sane relationship to scrolling. When
7349 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7350 * irrelevant, the child floats above all scrollable areas. But when
7351 * placing a child in one of the scrollable windows (border windows or
7352 * text window), you'll need to compute the child's correct position
7353 * in buffer coordinates any time scrolling occurs or buffer changes
7354 * occur, and then call gtk_text_view_move_child() to update the
7355 * child's position. Unfortunately there's no good way to detect that
7356 * scrolling has occurred, using the current API; a possible hack
7357 * would be to update all child positions when the scroll adjustments
7358 * change or the text buffer changes. See bug 64518 on
7359 * bugzilla.gnome.org for status of fixing this issue.
7363 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7365 GtkTextWindowType which_window,
7369 GtkTextViewChild *vc;
7371 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7372 g_return_if_fail (GTK_IS_WIDGET (child));
7373 g_return_if_fail (child->parent == NULL);
7375 vc = text_view_child_new_window (child, which_window,
7378 add_child (text_view, vc);
7380 g_assert (vc->widget == child);
7381 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7385 * gtk_text_view_move_child:
7386 * @text_view: a #GtkTextView
7387 * @child: child widget already added to the text view
7388 * @xpos: new X position in window coordinates
7389 * @ypos: new Y position in window coordinates
7391 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7395 gtk_text_view_move_child (GtkTextView *text_view,
7400 GtkTextViewChild *vc;
7402 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7403 g_return_if_fail (GTK_IS_WIDGET (child));
7404 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7406 vc = g_object_get_data (G_OBJECT (child),
7407 "gtk-text-view-child");
7409 g_assert (vc != NULL);
7411 if (vc->x == xpos &&
7418 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7419 gtk_widget_queue_resize (child);
7423 /* Iterator operations */
7426 * gtk_text_view_forward_display_line:
7427 * @text_view: a #GtkTextView
7428 * @iter: a #GtkTextIter
7430 * Moves the given @iter forward by one display (wrapped) line. A
7431 * display line is different from a paragraph. Paragraphs are
7432 * separated by newlines or other paragraph separator characters.
7433 * Display lines are created by line-wrapping a paragraph. If
7434 * wrapping is turned off, display lines and paragraphs will be the
7435 * same. Display lines are divided differently for each view, since
7436 * they depend on the view's width; paragraphs are the same in all
7437 * views, since they depend on the contents of the #GtkTextBuffer.
7439 * Return value: %TRUE if @iter was moved and is not on the end iterator
7442 gtk_text_view_forward_display_line (GtkTextView *text_view,
7445 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7446 g_return_val_if_fail (iter != NULL, FALSE);
7448 gtk_text_view_ensure_layout (text_view);
7450 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7454 * gtk_text_view_backward_display_line:
7455 * @text_view: a #GtkTextView
7456 * @iter: a #GtkTextIter
7458 * Moves the given @iter backward by one display (wrapped) line. A
7459 * display line is different from a paragraph. Paragraphs are
7460 * separated by newlines or other paragraph separator characters.
7461 * Display lines are created by line-wrapping a paragraph. If
7462 * wrapping is turned off, display lines and paragraphs will be the
7463 * same. Display lines are divided differently for each view, since
7464 * they depend on the view's width; paragraphs are the same in all
7465 * views, since they depend on the contents of the #GtkTextBuffer.
7467 * Return value: %TRUE if @iter was moved and is not on the end iterator
7470 gtk_text_view_backward_display_line (GtkTextView *text_view,
7473 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7474 g_return_val_if_fail (iter != NULL, FALSE);
7476 gtk_text_view_ensure_layout (text_view);
7478 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7482 * gtk_text_view_forward_display_line_end:
7483 * @text_view: a #GtkTextView
7484 * @iter: a #GtkTextIter
7486 * Moves the given @iter forward to the next display line end. A
7487 * display line is different from a paragraph. Paragraphs are
7488 * separated by newlines or other paragraph separator characters.
7489 * Display lines are created by line-wrapping a paragraph. If
7490 * wrapping is turned off, display lines and paragraphs will be the
7491 * same. Display lines are divided differently for each view, since
7492 * they depend on the view's width; paragraphs are the same in all
7493 * views, since they depend on the contents of the #GtkTextBuffer.
7495 * Return value: %TRUE if @iter was moved and is not on the end iterator
7498 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7501 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7502 g_return_val_if_fail (iter != NULL, FALSE);
7504 gtk_text_view_ensure_layout (text_view);
7506 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7510 * gtk_text_view_backward_display_line_start:
7511 * @text_view: a #GtkTextView
7512 * @iter: a #GtkTextIter
7514 * Moves the given @iter backward to the next display line start. A
7515 * display line is different from a paragraph. Paragraphs are
7516 * separated by newlines or other paragraph separator characters.
7517 * Display lines are created by line-wrapping a paragraph. If
7518 * wrapping is turned off, display lines and paragraphs will be the
7519 * same. Display lines are divided differently for each view, since
7520 * they depend on the view's width; paragraphs are the same in all
7521 * views, since they depend on the contents of the #GtkTextBuffer.
7523 * Return value: %TRUE if @iter was moved and is not on the end iterator
7526 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7529 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7530 g_return_val_if_fail (iter != NULL, FALSE);
7532 gtk_text_view_ensure_layout (text_view);
7534 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7538 * gtk_text_view_starts_display_line:
7539 * @text_view: a #GtkTextView
7540 * @iter: a #GtkTextIter
7542 * Determines whether @iter is at the start of a display line.
7543 * See gtk_text_view_forward_display_line() for an explanation of
7544 * display lines vs. paragraphs.
7546 * Return value: %TRUE if @iter begins a wrapped line
7549 gtk_text_view_starts_display_line (GtkTextView *text_view,
7550 const GtkTextIter *iter)
7552 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7553 g_return_val_if_fail (iter != NULL, FALSE);
7555 gtk_text_view_ensure_layout (text_view);
7557 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7561 * gtk_text_view_move_visually:
7562 * @text_view: a #GtkTextView
7563 * @iter: a #GtkTextIter
7564 * @count: number of lines to move
7566 * Moves @iter up or down by @count display (wrapped) lines.
7567 * See gtk_text_view_forward_display_line() for an explanation of
7568 * display lines vs. paragraphs.
7570 * Return value: %TRUE if @iter moved and is not on the end iterator
7573 gtk_text_view_move_visually (GtkTextView *text_view,
7577 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7578 g_return_val_if_fail (iter != NULL, FALSE);
7580 gtk_text_view_ensure_layout (text_view);
7582 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);