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 signals[MOVE_CURSOR] =
641 gtk_signal_new ("move_cursor",
642 GTK_RUN_LAST | GTK_RUN_ACTION,
643 GTK_CLASS_TYPE (object_class),
644 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
645 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
646 GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
648 signals[PAGE_HORIZONTALLY] =
649 gtk_signal_new ("page_horizontally",
650 GTK_RUN_LAST | GTK_RUN_ACTION,
651 GTK_CLASS_TYPE (object_class),
652 GTK_SIGNAL_OFFSET (GtkTextViewClass, page_horizontally),
653 _gtk_marshal_VOID__INT_BOOLEAN,
654 GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_BOOL);
656 signals[SET_ANCHOR] =
657 gtk_signal_new ("set_anchor",
658 GTK_RUN_LAST | GTK_RUN_ACTION,
659 GTK_CLASS_TYPE (object_class),
660 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
661 _gtk_marshal_VOID__VOID,
664 signals[INSERT_AT_CURSOR] =
665 gtk_signal_new ("insert_at_cursor",
666 GTK_RUN_LAST | GTK_RUN_ACTION,
667 GTK_CLASS_TYPE (object_class),
668 GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
669 _gtk_marshal_VOID__STRING,
670 GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
672 signals[DELETE_FROM_CURSOR] =
673 gtk_signal_new ("delete_from_cursor",
674 GTK_RUN_LAST | GTK_RUN_ACTION,
675 GTK_CLASS_TYPE (object_class),
676 GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
677 _gtk_marshal_VOID__ENUM_INT,
678 GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
680 signals[CUT_CLIPBOARD] =
681 gtk_signal_new ("cut_clipboard",
682 GTK_RUN_LAST | GTK_RUN_ACTION,
683 GTK_CLASS_TYPE (object_class),
684 GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
685 _gtk_marshal_VOID__VOID,
688 signals[COPY_CLIPBOARD] =
689 gtk_signal_new ("copy_clipboard",
690 GTK_RUN_LAST | GTK_RUN_ACTION,
691 GTK_CLASS_TYPE (object_class),
692 GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
693 _gtk_marshal_VOID__VOID,
696 signals[PASTE_CLIPBOARD] =
697 gtk_signal_new ("paste_clipboard",
698 GTK_RUN_LAST | GTK_RUN_ACTION,
699 GTK_CLASS_TYPE (object_class),
700 GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
701 _gtk_marshal_VOID__VOID,
704 signals[TOGGLE_OVERWRITE] =
705 gtk_signal_new ("toggle_overwrite",
706 GTK_RUN_LAST | GTK_RUN_ACTION,
707 GTK_CLASS_TYPE (object_class),
708 GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
709 _gtk_marshal_VOID__VOID,
712 signals[MOVE_FOCUS] =
713 gtk_signal_new ("move_focus",
714 GTK_RUN_LAST | GTK_RUN_ACTION,
715 GTK_CLASS_TYPE (object_class),
716 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_focus),
717 _gtk_marshal_VOID__ENUM,
718 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
720 signals[SET_SCROLL_ADJUSTMENTS] =
721 gtk_signal_new ("set_scroll_adjustments",
723 GTK_CLASS_TYPE (object_class),
724 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
725 _gtk_marshal_VOID__OBJECT_OBJECT,
726 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
727 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
729 signals[POPULATE_POPUP] =
730 gtk_signal_new ("populate_popup",
732 GTK_CLASS_TYPE (object_class),
733 GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
734 _gtk_marshal_VOID__OBJECT,
735 GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
741 binding_set = gtk_binding_set_by_class (klass);
743 /* Moving the insertion point */
744 add_move_binding (binding_set, GDK_Right, 0,
745 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
747 add_move_binding (binding_set, GDK_KP_Right, 0,
748 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
750 add_move_binding (binding_set, GDK_Left, 0,
751 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
753 add_move_binding (binding_set, GDK_KP_Left, 0,
754 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
756 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
757 GTK_MOVEMENT_WORDS, 1);
759 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
760 GTK_MOVEMENT_WORDS, 1);
762 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
763 GTK_MOVEMENT_WORDS, -1);
765 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
766 GTK_MOVEMENT_WORDS, 1);
768 add_move_binding (binding_set, GDK_Up, 0,
769 GTK_MOVEMENT_DISPLAY_LINES, -1);
771 add_move_binding (binding_set, GDK_KP_Up, 0,
772 GTK_MOVEMENT_DISPLAY_LINES, -1);
774 add_move_binding (binding_set, GDK_Down, 0,
775 GTK_MOVEMENT_DISPLAY_LINES, 1);
777 add_move_binding (binding_set, GDK_KP_Down, 0,
778 GTK_MOVEMENT_DISPLAY_LINES, 1);
780 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
781 GTK_MOVEMENT_PARAGRAPHS, -1);
783 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
784 GTK_MOVEMENT_PARAGRAPHS, -1);
786 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
787 GTK_MOVEMENT_PARAGRAPHS, 1);
789 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
790 GTK_MOVEMENT_PARAGRAPHS, 1);
792 add_move_binding (binding_set, GDK_Home, 0,
793 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
795 add_move_binding (binding_set, GDK_KP_Home, 0,
796 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
798 add_move_binding (binding_set, GDK_End, 0,
799 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
801 add_move_binding (binding_set, GDK_KP_End, 0,
802 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
804 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
805 GTK_MOVEMENT_BUFFER_ENDS, -1);
807 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
808 GTK_MOVEMENT_BUFFER_ENDS, -1);
810 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
811 GTK_MOVEMENT_BUFFER_ENDS, 1);
813 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
814 GTK_MOVEMENT_BUFFER_ENDS, 1);
816 add_move_binding (binding_set, GDK_Page_Up, 0,
817 GTK_MOVEMENT_PAGES, -1);
819 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
820 GTK_MOVEMENT_PAGES, -1);
822 add_move_binding (binding_set, GDK_Page_Down, 0,
823 GTK_MOVEMENT_PAGES, 1);
825 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
826 GTK_MOVEMENT_PAGES, 1);
830 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
832 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
834 GTK_TYPE_BOOL, FALSE);
835 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
837 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
839 GTK_TYPE_BOOL, TRUE);
842 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
843 "page_horizontally", 2,
845 GTK_TYPE_BOOL, FALSE);
847 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
848 "page_horizontally", 2,
850 GTK_TYPE_BOOL, TRUE);
852 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
853 "page_horizontally", 2,
855 GTK_TYPE_BOOL, FALSE);
857 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
858 "page_horizontally", 2,
860 GTK_TYPE_BOOL, TRUE);
862 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
863 "page_horizontally", 2,
865 GTK_TYPE_BOOL, FALSE);
867 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
868 "page_horizontally", 2,
870 GTK_TYPE_BOOL, TRUE);
872 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
873 "page_horizontally", 2,
875 GTK_TYPE_BOOL, FALSE);
877 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
878 "page_horizontally", 2,
880 GTK_TYPE_BOOL, TRUE);
883 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
884 "delete_from_cursor", 2,
885 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
888 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
889 "delete_from_cursor", 2,
890 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
893 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
894 "delete_from_cursor", 2,
895 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
898 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
899 "delete_from_cursor", 2,
900 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
903 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
904 "delete_from_cursor", 2,
905 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
908 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
909 "delete_from_cursor", 2,
910 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
915 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
918 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
919 "copy_clipboard", 0);
921 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
922 "paste_clipboard", 0);
925 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
926 "toggle_overwrite", 0);
927 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
928 "toggle_overwrite", 0);
930 /* Control-tab focus motion */
931 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
933 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
934 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
936 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
937 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, GDK_CONTROL_MASK,
939 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
941 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
943 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
944 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
946 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
947 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
949 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
953 gtk_text_view_init (GtkTextView *text_view)
957 widget = GTK_WIDGET (text_view);
959 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
961 /* Set up default style */
962 text_view->wrap_mode = GTK_WRAP_NONE;
963 text_view->pixels_above_lines = 0;
964 text_view->pixels_below_lines = 0;
965 text_view->pixels_inside_wrap = 0;
966 text_view->justify = GTK_JUSTIFY_LEFT;
967 text_view->left_margin = 0;
968 text_view->right_margin = 0;
969 text_view->indent = 0;
970 text_view->tabs = NULL;
971 text_view->editable = TRUE;
973 gtk_drag_dest_set (widget,
975 target_table, G_N_ELEMENTS (target_table),
976 GDK_ACTION_COPY | GDK_ACTION_MOVE);
978 text_view->virtual_cursor_x = -1;
979 text_view->virtual_cursor_y = -1;
981 /* This object is completely private. No external entity can gain a reference
982 * to it; so we create it here and destroy it in finalize ().
984 text_view->im_context = gtk_im_multicontext_new ();
986 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
987 G_CALLBACK (gtk_text_view_commit_handler), text_view);
988 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
989 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
990 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
991 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
992 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
993 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
995 text_view->cursor_visible = TRUE;
997 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1000 text_view->drag_start_x = -1;
1001 text_view->drag_start_y = -1;
1003 text_view->pending_place_cursor_button = 0;
1005 /* We handle all our own redrawing */
1006 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1010 * gtk_text_view_new:
1012 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1013 * before using the text view, an empty default buffer will be created
1014 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1015 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1017 * Return value: a new #GtkTextView
1020 gtk_text_view_new (void)
1022 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1026 * gtk_text_view_new_with_buffer:
1027 * @buffer: a #GtkTextBuffer
1029 * Creates a new #GtkTextView widget displaying the buffer
1030 * @buffer. One buffer can be shared among many widgets.
1031 * @buffer may be NULL to create a default buffer, in which case
1032 * this function is equivalent to gtk_text_view_new(). The
1033 * text view adds its own reference count to the buffer; it does not
1034 * take over an existing reference.
1036 * Return value: a new #GtkTextView.
1039 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1041 GtkTextView *text_view;
1043 text_view = (GtkTextView*)gtk_text_view_new ();
1045 gtk_text_view_set_buffer (text_view, buffer);
1047 return GTK_WIDGET (text_view);
1051 * gtk_text_view_set_buffer:
1052 * @text_view: a #GtkTextView
1053 * @buffer: a #GtkTextBuffer
1055 * Sets @buffer as the buffer being displayed by @text_view. The previous
1056 * buffer displayed by the text view is unreferenced, and a reference is
1057 * added to @buffer. If you owned a reference to @buffer before passing it
1058 * to this function, you must remove that reference yourself; #GtkTextView
1059 * will not "adopt" it.
1063 gtk_text_view_set_buffer (GtkTextView *text_view,
1064 GtkTextBuffer *buffer)
1066 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1067 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1069 if (text_view->buffer == buffer)
1072 if (text_view->buffer != NULL)
1074 /* Destroy all anchored children */
1078 copy = g_slist_copy (text_view->children);
1080 while (tmp_list != NULL)
1082 GtkTextViewChild *vc = tmp_list->data;
1086 gtk_widget_destroy (vc->widget);
1087 /* vc may now be invalid! */
1090 tmp_list = g_slist_next (tmp_list);
1093 g_slist_free (copy);
1095 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1096 gtk_text_view_mark_set_handler, text_view);
1097 g_object_unref (G_OBJECT (text_view->buffer));
1098 text_view->dnd_mark = NULL;
1100 if (GTK_WIDGET_REALIZED (text_view))
1101 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
1102 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1105 text_view->buffer = buffer;
1111 g_object_ref (G_OBJECT (buffer));
1113 if (text_view->layout)
1114 gtk_text_layout_set_buffer (text_view->layout, buffer);
1116 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1118 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1122 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1126 text_view->first_para_pixels = 0;
1128 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1129 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1131 if (GTK_WIDGET_REALIZED (text_view))
1132 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
1133 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1136 if (GTK_WIDGET_VISIBLE (text_view))
1137 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1139 DV(g_print ("Invalidating due to set_buffer\n"));
1140 gtk_text_view_invalidate (text_view);
1143 static GtkTextBuffer*
1144 get_buffer (GtkTextView *text_view)
1146 if (text_view->buffer == NULL)
1149 b = gtk_text_buffer_new (NULL);
1150 gtk_text_view_set_buffer (text_view, b);
1151 g_object_unref (G_OBJECT (b));
1154 return text_view->buffer;
1158 * gtk_text_view_get_buffer:
1159 * @text_view: a #GtkTextView
1161 * Returns the #GtkTextBuffer being displayed by this text view.
1162 * The reference count on the buffer is not incremented; the caller
1163 * of this function won't own a new reference.
1165 * Return value: a #GtkTextBuffer
1168 gtk_text_view_get_buffer (GtkTextView *text_view)
1170 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1172 return get_buffer (text_view);
1176 * gtk_text_view_get_iter_at_location:
1177 * @text_view: a #GtkTextView
1178 * @iter: a #GtkTextIter
1179 * @x: x position, in buffer coordinates
1180 * @y: y position, in buffer coordinates
1182 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1183 * coordinates are coordinates for the entire buffer, not just the
1184 * currently-displayed portion. If you have coordinates from an
1185 * event, you have to convert those to buffer coordinates with
1186 * gtk_text_view_window_to_buffer_coords().
1190 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1195 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1196 g_return_if_fail (iter != NULL);
1197 g_return_if_fail (text_view->layout != NULL);
1199 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1206 * gtk_text_view_get_iter_location:
1207 * @text_view: a #GtkTextView
1208 * @iter: a #GtkTextIter
1209 * @location: bounds of the character at @iter
1211 * Gets a rectangle which roughly contains the character at @iter.
1212 * The rectangle position is in buffer coordinates; use
1213 * gtk_text_view_buffer_to_window_coords() to convert these
1214 * coordinates to coordinates for one of the windows in the text view.
1218 gtk_text_view_get_iter_location (GtkTextView *text_view,
1219 const GtkTextIter *iter,
1220 GdkRectangle *location)
1222 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1223 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1225 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1229 * gtk_text_view_get_line_yrange:
1230 * @text_view: a #GtkTextView
1231 * @iter: a #GtkTextIter
1232 * @y: return location for a y coordinate
1233 * @height: return location for a height
1235 * Gets the y coordinate of the top of the line containing @iter,
1236 * and the height of the line. The coordinate is a buffer coordinate;
1237 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1241 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1242 const GtkTextIter *iter,
1246 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1247 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1249 gtk_text_layout_get_line_yrange (text_view->layout,
1256 * gtk_text_view_get_line_at_y:
1257 * @text_view: a #GtkTextView
1258 * @target_iter: a #GtkTextIter
1259 * @y: a y coordinate
1260 * @line_top: return location for top coordinate of the line
1262 * Gets the #GtkTextIter at the start of the line containing
1263 * the coordinate @y. @y is in buffer coordinates, convert from
1264 * window coordinates with gtk_text_view_window_to_buffer_coords().
1265 * If non-%NULL, @line_top will be filled with the coordinate of the top
1269 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1270 GtkTextIter *target_iter,
1274 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1276 gtk_text_layout_get_line_at_y (text_view->layout,
1283 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1285 DV (g_print (" Setting adj to raw value %g\n", val));
1287 /* We don't really want to clamp to upper; we want to clamp to
1288 upper - page_size which is the highest value the scrollbar
1289 will let us reach. */
1290 if (val > (adj->upper - adj->page_size))
1291 val = adj->upper - adj->page_size;
1293 if (val < adj->lower)
1296 if (val != adj->value)
1298 DV (g_print (" Setting adj to clamped value %g\n", val));
1299 gtk_adjustment_set_value (adj, val);
1307 * gtk_text_view_scroll_to_iter:
1308 * @text_view: a #GtkTextView
1309 * @iter: a #GtkTextIter
1310 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1311 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1312 * @xalign: horizontal alignment of mark within visible area.
1313 * @yalign: vertical alignment of mark within visible area
1315 * Scrolls @text_view so that @iter is on the screen in the position
1316 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1317 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1318 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1319 * possibly not scrolling at all. The effective screen for purposes
1320 * of this function is reduced by a margin of size @within_margin.
1321 * NOTE: This function uses the currently-computed height of the
1322 * lines in the text buffer. Note that line heights are computed
1323 * in an idle handler; so this function may not have the desired effect
1324 * if it's called before the height computations. To avoid oddness,
1325 * consider using gtk_text_view_scroll_to_mark() which saves a point
1326 * to be scrolled to after line validation.
1328 * Return value: %TRUE if scrolling occurred
1331 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1333 gdouble within_margin,
1339 GdkRectangle screen;
1344 gboolean retval = FALSE;
1346 gint screen_xoffset, screen_yoffset;
1347 gint current_x_scroll, current_y_scroll;
1349 /* FIXME why don't we do the validate-at-scroll-destination thing
1350 * from flush_scroll in this function? I think it wasn't done before
1351 * because changed_handler was screwed up, but I could be wrong.
1354 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1355 g_return_val_if_fail (iter != NULL, FALSE);
1356 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1357 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1358 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1360 widget = GTK_WIDGET (text_view);
1362 DV(g_print(G_STRLOC"\n"));
1364 gtk_text_layout_get_iter_location (text_view->layout,
1368 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1370 current_x_scroll = text_view->xoffset;
1371 current_y_scroll = text_view->yoffset;
1373 screen.x = current_x_scroll;
1374 screen.y = current_y_scroll;
1375 screen.width = SCREEN_WIDTH (widget);
1376 screen.height = SCREEN_HEIGHT (widget);
1378 screen_xoffset = screen.width * within_margin;
1379 screen_yoffset = screen.height * within_margin;
1381 screen.x += screen_xoffset;
1382 screen.y += screen_yoffset;
1383 screen.width -= screen_xoffset * 2;
1384 screen.height -= screen_yoffset * 2;
1386 /* paranoia check */
1387 if (screen.width < 1)
1389 if (screen.height < 1)
1392 screen_right = screen.x + screen.width;
1393 screen_bottom = screen.y + screen.height;
1395 /* The alignment affects the point in the target character that we
1396 * choose to align. If we're doing right/bottom alignment, we align
1397 * the right/bottom edge of the character the mark is at; if we're
1398 * doing left/top we align the left/top edge of the character; if
1399 * we're doing center alignment we align the center of the
1403 /* Vertical scroll */
1406 scroll_dest = current_y_scroll;
1410 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1412 /* if scroll_dest < screen.y, we move a negative increment (up),
1413 * else a positive increment (down)
1415 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1419 /* move minimum to get onscreen */
1420 if (rect.y < screen.y)
1422 scroll_dest = rect.y;
1423 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1425 else if ((rect.y + rect.height) > screen_bottom)
1427 scroll_dest = rect.y + rect.height;
1428 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1432 if (scroll_inc != 0)
1434 retval = set_adjustment_clamped (get_vadjustment (text_view),
1435 current_y_scroll + scroll_inc);
1437 DV (g_print (" vert increment %d\n", scroll_inc));
1440 /* Horizontal scroll */
1443 scroll_dest = current_x_scroll;
1447 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1449 /* if scroll_dest < screen.y, we move a negative increment (left),
1450 * else a positive increment (right)
1452 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1456 /* move minimum to get onscreen */
1457 if (rect.x < screen.x)
1459 scroll_dest = rect.x;
1460 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1462 else if ((rect.x + rect.width) > screen_right)
1464 scroll_dest = rect.x + rect.width;
1465 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1469 if (scroll_inc != 0)
1471 retval = set_adjustment_clamped (get_hadjustment (text_view),
1472 current_x_scroll + scroll_inc);
1474 DV (g_print (" horiz increment %d\n", scroll_inc));
1478 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1480 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1486 free_pending_scroll (GtkTextPendingScroll *scroll)
1488 if (!gtk_text_mark_get_deleted (scroll->mark))
1489 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1491 g_object_unref (G_OBJECT (scroll->mark));
1496 gtk_text_view_queue_scroll (GtkTextView *text_view,
1498 gdouble within_margin,
1504 GtkTextPendingScroll *scroll;
1506 DV(g_print(G_STRLOC"\n"));
1508 scroll = g_new (GtkTextPendingScroll, 1);
1510 scroll->within_margin = within_margin;
1511 scroll->use_align = use_align;
1512 scroll->xalign = xalign;
1513 scroll->yalign = yalign;
1515 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1517 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1520 gtk_text_mark_get_left_gravity (mark));
1522 g_object_ref (G_OBJECT (scroll->mark));
1524 if (text_view->pending_scroll)
1525 free_pending_scroll (text_view->pending_scroll);
1527 text_view->pending_scroll = scroll;
1531 gtk_text_view_flush_scroll (GtkTextView *text_view)
1534 GtkTextPendingScroll *scroll;
1538 widget = GTK_WIDGET (text_view);
1540 DV(g_print(G_STRLOC"\n"));
1542 if (text_view->pending_scroll == NULL)
1544 DV (g_print ("in flush scroll, no pending scroll\n"));
1548 scroll = text_view->pending_scroll;
1550 /* avoid recursion */
1551 text_view->pending_scroll = NULL;
1553 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1555 /* Validate area around the scroll destination, so the adjustment
1556 * can meaningfully point into that area. We must validate
1557 * enough area to be sure that after we scroll, everything onscreen
1558 * is valid; otherwise, validation will maintain the first para
1559 * in one place, but may push the target iter off the bottom of
1562 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1563 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1564 - (widget->allocation.height * 2),
1565 widget->allocation.height * 2);
1567 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1569 /* Ensure we have updated width/height */
1570 gtk_text_view_update_adjustments (text_view);
1572 retval = gtk_text_view_scroll_to_iter (text_view,
1574 scroll->within_margin,
1579 free_pending_scroll (scroll);
1585 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1587 if (upper != adj->upper)
1589 gdouble min = MAX (0.0, upper - adj->page_size);
1590 gboolean value_changed = FALSE;
1594 if (adj->value > min)
1597 value_changed = TRUE;
1600 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1601 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1605 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1606 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1612 gtk_text_view_update_adjustments (GtkTextView *text_view)
1614 gint width = 0, height = 0;
1616 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1618 if (text_view->layout)
1619 gtk_text_layout_get_size (text_view->layout, &width, &height);
1621 if (text_view->width != width || text_view->height != height)
1623 text_view->width = width;
1624 text_view->height = height;
1626 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1627 MAX (SCREEN_WIDTH (text_view), width));
1628 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1629 MAX (SCREEN_HEIGHT (text_view), height));
1631 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1633 /* Set up the step sizes; we'll say that a page is
1634 our allocation minus one step, and a step is
1635 1/10 of our allocation. */
1636 text_view->hadjustment->step_increment =
1637 SCREEN_WIDTH (text_view) / 10.0;
1638 text_view->hadjustment->page_increment =
1639 SCREEN_WIDTH (text_view) * 0.9;
1641 text_view->vadjustment->step_increment =
1642 SCREEN_HEIGHT (text_view) / 10.0;
1643 text_view->vadjustment->page_increment =
1644 SCREEN_HEIGHT (text_view) * 0.9;
1646 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1647 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1652 gtk_text_view_update_layout_width (GtkTextView *text_view)
1654 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1656 gtk_text_view_ensure_layout (text_view);
1658 gtk_text_layout_set_screen_width (text_view->layout,
1659 SCREEN_WIDTH (text_view));
1663 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1666 gint cursor_x_pos, cursor_y_pos;
1668 if (text_view->layout == NULL)
1671 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1673 area.x = cursor_x_pos;
1674 area.y = cursor_y_pos;
1675 area.width = area.height = 0;
1677 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1681 * gtk_text_view_scroll_to_mark:
1682 * @text_view: a #GtkTextView
1683 * @mark: a #GtkTextMark
1684 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1685 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1686 * @xalign: horizontal alignment of mark within visible area.
1687 * @yalign: vertical alignment of mark within visible area
1689 * Scrolls @text_view so that @mark is on the screen in the position
1690 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1691 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1692 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1693 * possibly not scrolling at all. The effective screen for purposes
1694 * of this function is reduced by a margin of size @within_margin.
1698 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1700 gdouble within_margin,
1705 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1706 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1707 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1708 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1709 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1711 gtk_text_view_queue_scroll (text_view, mark,
1717 /* If no validation is pending, we need to go ahead and force an
1720 if (text_view->layout &&
1721 gtk_text_layout_is_valid (text_view->layout))
1722 gtk_text_view_flush_scroll (text_view);
1726 * gtk_text_view_scroll_mark_onscreen:
1727 * @text_view: a #GtkTextView
1728 * @mark: a mark in the buffer for @text_view
1730 * Scrolls @text_view the minimum distance such that @mark is contained
1731 * within the visible area of the widget.
1735 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1738 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1739 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1741 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1745 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1747 GdkRectangle visible_rect;
1748 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1750 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1752 visible_rect.y + visible_rect.height);
1756 * gtk_text_view_move_mark_onscreen:
1757 * @text_view: a #GtkTextView
1758 * @mark: a #GtkTextMark
1760 * Moves a mark within the buffer so that it's
1761 * located within the currently-visible text area.
1763 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1766 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1771 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1772 g_return_val_if_fail (mark != NULL, FALSE);
1774 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1776 if (clamp_iter_onscreen (text_view, &iter))
1778 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1786 * gtk_text_view_get_visible_rect:
1787 * @text_view: a #GtkTextView
1788 * @visible_rect: rectangle to fill
1790 * Fills @visible_rect with the currently-visible
1791 * region of the buffer, in buffer coordinates. Convert to window coordinates
1792 * with gtk_text_view_buffer_to_window_coords().
1795 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1796 GdkRectangle *visible_rect)
1800 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1802 widget = GTK_WIDGET (text_view);
1806 visible_rect->x = text_view->xoffset;
1807 visible_rect->y = text_view->yoffset;
1808 visible_rect->width = SCREEN_WIDTH (widget);
1809 visible_rect->height = SCREEN_HEIGHT (widget);
1811 DV(g_print(" visible rect: %d,%d %d x %d\n",
1814 visible_rect->width,
1815 visible_rect->height));
1820 * gtk_text_view_set_wrap_mode:
1821 * @text_view: a #GtkTextView
1822 * @wrap_mode: a #GtkWrapMode
1824 * Sets the line wrapping for the view.
1827 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1828 GtkWrapMode wrap_mode)
1830 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1832 if (text_view->wrap_mode != wrap_mode)
1834 text_view->wrap_mode = wrap_mode;
1836 if (text_view->layout)
1838 text_view->layout->default_style->wrap_mode = wrap_mode;
1839 gtk_text_layout_default_style_changed (text_view->layout);
1843 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1847 * gtk_text_view_get_wrap_mode:
1848 * @text_view: a #GtkTextView
1850 * Gets the line wrapping for the view.
1852 * Return value: the line wrap setting
1855 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1857 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1859 return text_view->wrap_mode;
1863 * gtk_text_view_set_editable:
1864 * @text_view: a #GtkTextView
1865 * @setting: whether it's editable
1867 * Sets the default editability of the #GtkTextView. You can override
1868 * this default setting with tags in the buffer, using the "editable"
1869 * attribute of tags.
1872 gtk_text_view_set_editable (GtkTextView *text_view,
1875 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1876 setting = setting != FALSE;
1878 if (text_view->editable != setting)
1880 text_view->editable = setting;
1882 if (text_view->layout)
1884 text_view->layout->default_style->editable = text_view->editable;
1885 gtk_text_layout_default_style_changed (text_view->layout);
1888 g_object_notify (G_OBJECT (text_view), "editable");
1893 * gtk_text_view_get_editable:
1894 * @text_view: a #GtkTextView
1896 * Returns the default editability of the #GtkTextView. Tags in the
1897 * buffer may override this setting for some ranges of text.
1899 * Return value: whether text is editable by default
1902 gtk_text_view_get_editable (GtkTextView *text_view)
1904 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1906 return text_view->editable;
1910 * gtk_text_view_set_pixels_above_lines:
1911 * @text_view: a #GtkTextView
1912 * @pixels_above_lines: pixels above paragraphs
1914 * Sets the default number of blank pixels above paragraphs in @text_view.
1915 * Tags in the buffer for @text_view may override the defaults.
1919 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1920 gint pixels_above_lines)
1922 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1924 if (text_view->pixels_above_lines != pixels_above_lines)
1926 text_view->pixels_above_lines = pixels_above_lines;
1928 if (text_view->layout)
1930 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1931 gtk_text_layout_default_style_changed (text_view->layout);
1934 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1939 * gtk_text_view_get_pixels_above_lines:
1940 * @text_view: a #GtkTextView
1942 * Gets the default number of pixels to put above paragraphs.
1944 * Return value: default number of pixels above paragraphs
1947 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1949 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1951 return text_view->pixels_above_lines;
1955 * gtk_text_view_set_pixels_below_lines:
1956 * @text_view: a #GtkTextView
1957 * @pixels_below_lines: pixels below paragraphs
1959 * Sets the default number of pixels of blank space
1960 * to put below paragraphs in @text_view. May be overridden
1961 * by tags applied to @text_view's buffer.
1965 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1966 gint pixels_below_lines)
1968 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1970 if (text_view->pixels_below_lines != pixels_below_lines)
1972 text_view->pixels_below_lines = pixels_below_lines;
1974 if (text_view->layout)
1976 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
1977 gtk_text_layout_default_style_changed (text_view->layout);
1980 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
1985 * gtk_text_view_get_pixels_below_lines:
1986 * @text_view: a #GtkTextView
1988 * Gets the value set by gtk_text_view_set_pixels_below_lines().
1990 * Return value: default number of blank pixels below paragraphs
1993 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
1995 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1997 return text_view->pixels_below_lines;
2001 * gtk_text_view_set_pixels_inside_wrap:
2002 * @text_view: a #GtkTextView
2003 * @pixels_inside_wrap: default number of pixels between wrapped lines
2005 * Sets the default number of pixels of blank space to leave between
2006 * display/wrapped lines within a paragraph. May be overridden by
2007 * tags in @text_view's buffer.
2011 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2012 gint pixels_inside_wrap)
2014 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2016 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2018 text_view->pixels_inside_wrap = pixels_inside_wrap;
2020 if (text_view->layout)
2022 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2023 gtk_text_layout_default_style_changed (text_view->layout);
2026 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2031 * gtk_text_view_get_pixels_inside_wrap:
2032 * @text_view: a #GtkTextView
2034 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2036 * Return value: default number of pixels of blank space between wrapped lines
2039 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2041 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2043 return text_view->pixels_inside_wrap;
2047 * gtk_text_view_set_justification:
2048 * @text_view: a #GtkTextView
2049 * @justification: justification
2051 * Sets the default justification of text in @text_view.
2052 * Tags in the view's buffer may override the default.
2056 gtk_text_view_set_justification (GtkTextView *text_view,
2057 GtkJustification justification)
2059 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2061 if (text_view->justify != justification)
2063 text_view->justify = justification;
2065 if (text_view->layout)
2067 text_view->layout->default_style->justification = justification;
2068 gtk_text_layout_default_style_changed (text_view->layout);
2071 g_object_notify (G_OBJECT (text_view), "justification");
2076 * gtk_text_view_get_justification:
2077 * @text_view: a #GtkTextView
2079 * Gets the default justification of paragraphs in @text_view.
2080 * Tags in the buffer may override the default.
2082 * Return value: default justification
2085 gtk_text_view_get_justification (GtkTextView *text_view)
2087 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2089 return text_view->justify;
2093 * gtk_text_view_set_left_margin:
2094 * @text_view: a #GtkTextView
2095 * @left_margin: left margin in pixels
2097 * Sets the default left margin for text in @text_view.
2098 * Tags in the buffer may override the default.
2102 gtk_text_view_set_left_margin (GtkTextView *text_view,
2105 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2107 if (text_view->left_margin != left_margin)
2109 text_view->left_margin = left_margin;
2111 if (text_view->layout)
2113 text_view->layout->default_style->left_margin = left_margin;
2114 gtk_text_layout_default_style_changed (text_view->layout);
2117 g_object_notify (G_OBJECT (text_view), "left_margin");
2122 * gtk_text_view_get_left_margin:
2123 * @text_view: a #GtkTextView
2125 * Gets the default left margin size of paragraphs in the @text_view.
2126 * Tags in the buffer may override the default.
2128 * Return value: left margin in pixels
2131 gtk_text_view_get_left_margin (GtkTextView *text_view)
2133 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2135 return text_view->left_margin;
2139 * gtk_text_view_set_right_margin:
2140 * @text_view: a #GtkTextView
2141 * @right_margin: right margin in pixels
2143 * Sets the default right margin for text in the text view.
2144 * Tags in the buffer may override the default.
2148 gtk_text_view_set_right_margin (GtkTextView *text_view,
2151 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2153 if (text_view->right_margin != right_margin)
2155 text_view->right_margin = right_margin;
2157 if (text_view->layout)
2159 text_view->layout->default_style->right_margin = right_margin;
2160 gtk_text_layout_default_style_changed (text_view->layout);
2163 g_object_notify (G_OBJECT (text_view), "right_margin");
2168 * gtk_text_view_get_right_margin:
2169 * @text_view: a #GtkTextView
2171 * Gets the default right margin for text in @text_view. Tags
2172 * in the buffer may override the default.
2174 * Return value: right margin in pixels
2177 gtk_text_view_get_right_margin (GtkTextView *text_view)
2179 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2181 return text_view->right_margin;
2185 * gtk_text_view_set_indent:
2186 * @text_view: a #GtkTextView
2187 * @indent: indentation in pixels
2189 * Sets the default indentation for paragraphs in @text_view.
2190 * Tags in the buffer may override the default.
2194 gtk_text_view_set_indent (GtkTextView *text_view,
2197 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2199 if (text_view->indent != indent)
2201 text_view->indent = indent;
2203 if (text_view->layout)
2205 text_view->layout->default_style->indent = indent;
2206 gtk_text_layout_default_style_changed (text_view->layout);
2209 g_object_notify (G_OBJECT (text_view), "indent");
2214 * gtk_text_view_get_indent:
2215 * @text_view: a #GtkTextView
2217 * Gets the default indentation of paragraphs in @text_view.
2218 * Tags in the view's buffer may override the default.
2219 * The indentation may be negative.
2221 * Return value: number of pixels of indentation
2224 gtk_text_view_get_indent (GtkTextView *text_view)
2226 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2228 return text_view->indent;
2232 * gtk_text_view_set_tabs:
2233 * @text_view: a #GtkTextView
2234 * @tabs: tabs as a #PangoTabArray
2236 * Sets the default tab stops for paragraphs in @text_view.
2237 * Tags in the buffer may override the default.
2241 gtk_text_view_set_tabs (GtkTextView *text_view,
2242 PangoTabArray *tabs)
2244 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2246 if (text_view->tabs)
2247 pango_tab_array_free (text_view->tabs);
2249 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2251 if (text_view->layout)
2253 /* some unkosher futzing in internal struct details... */
2254 if (text_view->layout->default_style->tabs)
2255 pango_tab_array_free (text_view->layout->default_style->tabs);
2257 text_view->layout->default_style->tabs =
2258 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2260 gtk_text_layout_default_style_changed (text_view->layout);
2263 g_object_notify (G_OBJECT (text_view), "tabs");
2267 * gtk_text_view_get_tabs:
2268 * @text_view: a #GtkTextView
2270 * Gets the default tabs for @text_view. Tags in the buffer may
2271 * override the defaults. The returned array will be %NULL if
2272 * "standard" (8-space) tabs are used. Free the return value
2273 * with pango_tab_array_free().
2275 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2278 gtk_text_view_get_tabs (GtkTextView *text_view)
2280 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2282 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2286 * gtk_text_view_set_cursor_visible:
2287 * @text_view: a #GtkTextView
2288 * @setting: whether to show the insertion cursor
2290 * Toggles whether the insertion point is displayed. A buffer with no editable
2291 * text probably shouldn't have a visible cursor, so you may want to turn
2295 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2298 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2300 setting = (setting != FALSE);
2302 if (text_view->cursor_visible != setting)
2304 text_view->cursor_visible = setting;
2306 if (GTK_WIDGET_HAS_FOCUS (text_view))
2308 if (text_view->layout)
2310 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2311 gtk_text_view_check_cursor_blink (text_view);
2315 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2320 * gtk_text_view_get_cursor_visible:
2321 * @text_view: a #GtkTextView
2323 * Find out whether the cursor is being displayed.
2325 * Return value: whether the insertion mark is visible
2328 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2330 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2332 return text_view->cursor_visible;
2337 * gtk_text_view_place_cursor_onscreen:
2338 * @text_view: a #GtkTextView
2340 * Moves the cursor to the currently visible region of the
2341 * buffer, it it isn't there already.
2343 * Return value: TRUE if the cursor had to be moved.
2346 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2350 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2352 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2353 gtk_text_buffer_get_mark (get_buffer (text_view),
2356 if (clamp_iter_onscreen (text_view, &insert))
2358 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2366 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2368 if (text_view->first_validate_idle != 0)
2370 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2371 g_source_remove (text_view->first_validate_idle);
2372 text_view->first_validate_idle = 0;
2375 if (text_view->incremental_validate_idle != 0)
2377 g_source_remove (text_view->incremental_validate_idle);
2378 text_view->incremental_validate_idle = 0;
2383 gtk_text_view_destroy (GtkObject *object)
2385 GtkTextView *text_view;
2386 GtkTextLayout *layout;
2388 text_view = GTK_TEXT_VIEW (object);
2390 layout = text_view->layout;
2392 gtk_text_view_remove_validate_idles (text_view);
2393 gtk_text_view_set_buffer (text_view, NULL);
2394 gtk_text_view_destroy_layout (text_view);
2396 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2400 gtk_text_view_finalize (GObject *object)
2402 GtkTextView *text_view;
2404 text_view = GTK_TEXT_VIEW (object);
2406 g_return_if_fail (text_view->buffer == NULL);
2408 gtk_text_view_destroy_layout (text_view);
2409 gtk_text_view_set_buffer (text_view, NULL);
2411 if (text_view->pending_scroll)
2413 free_pending_scroll (text_view->pending_scroll);
2414 text_view->pending_scroll = NULL;
2417 if (text_view->hadjustment)
2418 g_object_unref (G_OBJECT (text_view->hadjustment));
2419 if (text_view->vadjustment)
2420 g_object_unref (G_OBJECT (text_view->vadjustment));
2422 text_window_free (text_view->text_window);
2424 if (text_view->left_window)
2425 text_window_free (text_view->left_window);
2427 if (text_view->top_window)
2428 text_window_free (text_view->top_window);
2430 if (text_view->right_window)
2431 text_window_free (text_view->right_window);
2433 if (text_view->bottom_window)
2434 text_window_free (text_view->bottom_window);
2436 g_object_unref (G_OBJECT (text_view->im_context));
2438 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2442 gtk_text_view_set_property (GObject *object,
2444 const GValue *value,
2447 GtkTextView *text_view;
2449 text_view = GTK_TEXT_VIEW (object);
2453 case PROP_PIXELS_ABOVE_LINES:
2454 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2457 case PROP_PIXELS_BELOW_LINES:
2458 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2461 case PROP_PIXELS_INSIDE_WRAP:
2462 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2466 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2469 case PROP_WRAP_MODE:
2470 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2473 case PROP_JUSTIFICATION:
2474 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2477 case PROP_LEFT_MARGIN:
2478 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2481 case PROP_RIGHT_MARGIN:
2482 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2486 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2490 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2493 case PROP_CURSOR_VISIBLE:
2494 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2498 g_assert_not_reached ();
2504 gtk_text_view_get_property (GObject *object,
2509 GtkTextView *text_view;
2511 text_view = GTK_TEXT_VIEW (object);
2515 case PROP_PIXELS_ABOVE_LINES:
2516 g_value_set_int (value, text_view->pixels_above_lines);
2519 case PROP_PIXELS_BELOW_LINES:
2520 g_value_set_int (value, text_view->pixels_below_lines);
2523 case PROP_PIXELS_INSIDE_WRAP:
2524 g_value_set_int (value, text_view->pixels_inside_wrap);
2528 g_value_set_boolean (value, text_view->editable);
2531 case PROP_WRAP_MODE:
2532 g_value_set_enum (value, text_view->wrap_mode);
2535 case PROP_JUSTIFICATION:
2536 g_value_set_enum (value, text_view->justify);
2539 case PROP_LEFT_MARGIN:
2540 g_value_set_int (value, text_view->left_margin);
2543 case PROP_RIGHT_MARGIN:
2544 g_value_set_int (value, text_view->right_margin);
2548 g_value_set_int (value, text_view->indent);
2552 g_value_set_boxed (value, text_view->tabs);
2555 case PROP_CURSOR_VISIBLE:
2556 g_value_set_boolean (value, text_view->cursor_visible);
2560 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2566 gtk_text_view_size_request (GtkWidget *widget,
2567 GtkRequisition *requisition)
2569 GtkTextView *text_view;
2571 gint focus_edge_width;
2573 gboolean interior_focus;
2575 text_view = GTK_TEXT_VIEW (widget);
2577 gtk_widget_style_get (widget,
2578 "interior_focus", &interior_focus,
2579 "focus_line_width", &focus_width,
2583 focus_edge_width = 0;
2585 focus_edge_width = focus_width;
2587 if (text_view->layout)
2589 text_view->text_window->requisition.width = text_view->layout->width;
2590 text_view->text_window->requisition.height = text_view->layout->height;
2594 text_view->text_window->requisition.width = 0;
2595 text_view->text_window->requisition.height = 0;
2598 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2599 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2601 if (text_view->left_window)
2602 requisition->width += text_view->left_window->requisition.width;
2604 if (text_view->right_window)
2605 requisition->width += text_view->right_window->requisition.width;
2607 if (text_view->top_window)
2608 requisition->height += text_view->top_window->requisition.height;
2610 if (text_view->bottom_window)
2611 requisition->height += text_view->bottom_window->requisition.height;
2613 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2614 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2616 tmp_list = text_view->children;
2617 while (tmp_list != NULL)
2619 GtkTextViewChild *child = tmp_list->data;
2623 GtkRequisition child_req;
2624 GtkRequisition old_req;
2626 gtk_widget_get_child_requisition (child->widget, &old_req);
2628 gtk_widget_size_request (child->widget, &child_req);
2630 gtk_widget_get_child_requisition (child->widget, &child_req);
2632 /* Invalidate layout lines if required */
2633 if (text_view->layout &&
2634 (old_req.width != child_req.width ||
2635 old_req.height != child_req.height))
2636 gtk_text_child_anchor_queue_resize (child->anchor,
2641 GtkRequisition child_req;
2643 gtk_widget_size_request (child->widget, &child_req);
2646 tmp_list = g_slist_next (tmp_list);
2651 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2652 GtkTextViewChild *vc,
2653 GtkAllocation *allocation)
2659 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2663 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2666 buffer_y += vc->from_top_of_line;
2668 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2669 allocation->y = buffer_y - text_view->yoffset;
2671 gtk_widget_get_child_requisition (vc->widget, &req);
2672 allocation->width = req.width;
2673 allocation->height = req.height;
2677 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2678 GtkTextViewChild *vc)
2680 GtkAllocation allocation;
2682 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2684 gtk_widget_size_allocate (vc->widget, &allocation);
2687 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2689 vc->widget->allocation.x,
2690 vc->widget->allocation.y,
2691 text_view->yoffset);
2696 gtk_text_view_child_allocated (GtkTextLayout *layout,
2702 GtkTextViewChild *vc = NULL;
2703 GtkTextView *text_view = data;
2705 /* x,y is the position of the child from the top of the line, and
2706 * from the left of the buffer. We have to translate that into text
2707 * window coordinates, then size_allocate the child.
2710 vc = g_object_get_data (G_OBJECT (child),
2711 "gtk-text-view-child");
2713 g_assert (vc != NULL);
2715 DV (g_print ("child allocated at %d,%d\n", x, y));
2717 vc->from_left_of_buffer = x;
2718 vc->from_top_of_line = y;
2720 gtk_text_view_update_child_allocation (text_view, vc);
2724 gtk_text_view_allocate_children (GtkTextView *text_view)
2728 DV(g_print(G_STRLOC"\n"));
2730 tmp_list = text_view->children;
2731 while (tmp_list != NULL)
2733 GtkTextViewChild *child = tmp_list->data;
2737 /* We need to force-validate the regions containing
2740 GtkTextIter child_loc;
2741 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2745 gtk_text_layout_validate_yrange (text_view->layout,
2751 GtkAllocation allocation;
2752 GtkRequisition child_req;
2754 g_assert (child != NULL);
2756 allocation.x = child->x;
2757 allocation.y = child->y;
2759 gtk_widget_get_child_requisition (child->widget, &child_req);
2761 allocation.width = child_req.width;
2762 allocation.height = child_req.height;
2764 gtk_widget_size_allocate (child->widget, &allocation);
2767 tmp_list = g_slist_next (tmp_list);
2772 gtk_text_view_size_allocate (GtkWidget *widget,
2773 GtkAllocation *allocation)
2775 GtkTextView *text_view;
2776 GtkTextIter first_para;
2778 GtkAdjustment *vadj;
2779 gboolean yoffset_changed = FALSE;
2781 GdkRectangle text_rect;
2782 GdkRectangle left_rect;
2783 GdkRectangle right_rect;
2784 GdkRectangle top_rect;
2785 GdkRectangle bottom_rect;
2786 gint focus_edge_width;
2788 gboolean interior_focus;
2789 gboolean size_changed;
2791 text_view = GTK_TEXT_VIEW (widget);
2793 DV(g_print(G_STRLOC"\n"));
2796 widget->allocation.width != allocation->width ||
2797 widget->allocation.height != allocation->height;
2799 widget->allocation = *allocation;
2801 if (GTK_WIDGET_REALIZED (widget))
2803 gdk_window_move_resize (widget->window,
2804 allocation->x, allocation->y,
2805 allocation->width, allocation->height);
2808 /* distribute width/height among child windows. Ensure all
2809 * windows get at least a 1x1 allocation.
2812 gtk_widget_style_get (widget,
2813 "interior_focus", &interior_focus,
2814 "focus_line_width", &focus_width,
2818 focus_edge_width = 0;
2820 focus_edge_width = focus_width;
2822 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2824 if (text_view->left_window)
2825 left_rect.width = text_view->left_window->requisition.width;
2827 left_rect.width = 0;
2829 width -= left_rect.width;
2831 if (text_view->right_window)
2832 right_rect.width = text_view->right_window->requisition.width;
2834 right_rect.width = 0;
2836 width -= right_rect.width;
2838 text_rect.width = MAX (1, width);
2840 top_rect.width = text_rect.width;
2841 bottom_rect.width = text_rect.width;
2844 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2846 if (text_view->top_window)
2847 top_rect.height = text_view->top_window->requisition.height;
2849 top_rect.height = 0;
2851 height -= top_rect.height;
2853 if (text_view->bottom_window)
2854 bottom_rect.height = text_view->bottom_window->requisition.height;
2856 bottom_rect.height = 0;
2858 height -= bottom_rect.height;
2860 text_rect.height = MAX (1, height);
2862 left_rect.height = text_rect.height;
2863 right_rect.height = text_rect.height;
2866 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2867 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2869 text_rect.x = left_rect.x + left_rect.width;
2870 text_rect.y = top_rect.y + top_rect.height;
2872 left_rect.y = text_rect.y;
2873 right_rect.y = text_rect.y;
2875 top_rect.x = text_rect.x;
2876 bottom_rect.x = text_rect.x;
2878 right_rect.x = text_rect.x + text_rect.width;
2879 bottom_rect.y = text_rect.y + text_rect.height;
2881 text_window_size_allocate (text_view->text_window,
2884 if (text_view->left_window)
2885 text_window_size_allocate (text_view->left_window,
2888 if (text_view->right_window)
2889 text_window_size_allocate (text_view->right_window,
2892 if (text_view->top_window)
2893 text_window_size_allocate (text_view->top_window,
2896 if (text_view->bottom_window)
2897 text_window_size_allocate (text_view->bottom_window,
2900 gtk_text_view_update_layout_width (text_view);
2902 /* Note that this will do some layout validation */
2903 gtk_text_view_allocate_children (text_view);
2905 /* Now adjust the value of the adjustment to keep the cursor at the
2906 * same place in the buffer
2908 gtk_text_view_get_first_para_iter (text_view, &first_para);
2909 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2911 y += text_view->first_para_pixels;
2913 /* Ensure h/v adj exist */
2914 get_hadjustment (text_view);
2915 get_vadjustment (text_view);
2917 vadj = text_view->vadjustment;
2918 if (y > vadj->upper - vadj->page_size)
2919 y = MAX (0, vadj->upper - vadj->page_size);
2921 if (y != text_view->yoffset)
2923 vadj->value = text_view->yoffset = y;
2924 yoffset_changed = TRUE;
2927 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2928 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) / 2;
2929 text_view->hadjustment->lower = 0;
2930 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2932 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2934 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2935 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) / 2;
2936 text_view->vadjustment->lower = 0;
2937 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2939 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2941 if (yoffset_changed)
2942 gtk_adjustment_value_changed (vadj);
2944 /* The GTK resize loop processes all the pending exposes right
2945 * after doing the resize stuff, so the idle sizer won't have a
2946 * chance to run. So we do the work here.
2948 gtk_text_view_flush_first_validate (text_view);
2950 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2953 if (size_changed && GTK_WIDGET_REALIZED (widget))
2954 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2958 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2961 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2962 text_view->first_para_mark);
2966 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2968 GtkWidget *widget = GTK_WIDGET (text_view);
2970 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
2972 if (SCREEN_HEIGHT (widget) > 0)
2974 GtkTextIter first_para;
2976 /* Be sure we've validated the stuff onscreen; if we
2977 * scrolled, these calls won't have any effect, because
2978 * they were called in the recursive validate_onscreen
2980 gtk_text_view_get_first_para_iter (text_view, &first_para);
2982 gtk_text_layout_validate_yrange (text_view->layout,
2985 text_view->first_para_pixels +
2986 SCREEN_HEIGHT (widget));
2989 text_view->onscreen_validated = TRUE;
2991 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
2993 /* This can have the odd side effect of triggering a scroll, which should
2994 * flip "onscreen_validated" back to FALSE, but should also get us
2995 * back into this function to turn it on again.
2997 gtk_text_view_update_adjustments (text_view);
2999 g_assert (text_view->onscreen_validated);
3003 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3005 if (text_view->first_validate_idle == 0)
3008 /* Do this first, which means that if an "invalidate"
3009 * occurs during any of this process, a new first_validate_callback
3010 * will be installed, and we'll start again.
3012 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3013 g_source_remove (text_view->first_validate_idle);
3014 text_view->first_validate_idle = 0;
3016 /* be sure we have up-to-date screen size set on the
3019 gtk_text_view_update_layout_width (text_view);
3021 /* Bail out if we invalidated stuff; scrolling right away will just
3022 * confuse the issue.
3024 if (text_view->first_validate_idle != 0)
3026 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3030 /* scroll to any marks, if that's pending. This can
3031 * jump us to the validation codepath used for scrolling
3032 * onscreen, if so we bail out.
3034 if (!gtk_text_view_flush_scroll (text_view))
3035 gtk_text_view_validate_onscreen (text_view);
3037 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3039 g_assert (text_view->onscreen_validated);
3044 first_validate_callback (gpointer data)
3046 GtkTextView *text_view = data;
3048 GDK_THREADS_ENTER ();
3050 /* Note that some of this code is duplicated at the end of size_allocate,
3051 * keep in sync with that.
3054 DV(g_print(G_STRLOC"\n"));
3056 gtk_text_view_flush_first_validate (text_view);
3058 GDK_THREADS_LEAVE ();
3064 incremental_validate_callback (gpointer data)
3066 GtkTextView *text_view = data;
3067 gboolean result = TRUE;
3069 GDK_THREADS_ENTER ();
3071 DV(g_print(G_STRLOC"\n"));
3073 gtk_text_layout_validate (text_view->layout, 2000);
3075 gtk_text_view_update_adjustments (text_view);
3077 if (gtk_text_layout_is_valid (text_view->layout))
3079 text_view->incremental_validate_idle = 0;
3083 GDK_THREADS_LEAVE ();
3089 gtk_text_view_invalidate (GtkTextView *text_view)
3091 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3092 text_view->onscreen_validated));
3094 text_view->onscreen_validated = FALSE;
3096 if (!text_view->first_validate_idle)
3098 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3099 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3100 text_view->first_validate_idle));
3103 if (!text_view->incremental_validate_idle)
3105 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3106 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3107 text_view->incremental_validate_idle));
3112 invalidated_handler (GtkTextLayout *layout,
3115 GtkTextView *text_view;
3117 text_view = GTK_TEXT_VIEW (data);
3119 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3120 gtk_text_view_invalidate (text_view);
3124 changed_handler (GtkTextLayout *layout,
3130 GtkTextView *text_view;
3132 GdkRectangle visible_rect;
3133 GdkRectangle redraw_rect;
3135 text_view = GTK_TEXT_VIEW (data);
3136 widget = GTK_WIDGET (data);
3138 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3140 if (GTK_WIDGET_REALIZED (text_view))
3142 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3144 redraw_rect.x = visible_rect.x;
3145 redraw_rect.width = visible_rect.width;
3146 redraw_rect.y = start_y;
3148 if (old_height == new_height)
3149 redraw_rect.height = old_height;
3151 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3153 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3155 /* text_window_invalidate_rect() takes buffer coordinates */
3156 text_window_invalidate_rect (text_view->text_window,
3159 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3163 redraw_rect.height));
3165 if (text_view->left_window)
3166 text_window_invalidate_rect (text_view->left_window,
3168 if (text_view->right_window)
3169 text_window_invalidate_rect (text_view->right_window,
3171 if (text_view->top_window)
3172 text_window_invalidate_rect (text_view->top_window,
3174 if (text_view->bottom_window)
3175 text_window_invalidate_rect (text_view->bottom_window,
3178 gtk_text_view_update_im_spot_location (text_view);
3182 if (old_height != new_height)
3184 gboolean yoffset_changed = FALSE;
3186 int new_first_para_top;
3187 int old_first_para_top;
3190 /* If the bottom of the old area was above the top of the
3191 * screen, we need to scroll to keep the current top of the
3192 * screen in place. Remember that first_para_pixels is the
3193 * position of the top of the screen in coordinates relative to
3194 * the first paragraph onscreen.
3196 * In short we are adding the height delta of the portion of the
3197 * changed region above first_para_mark to text_view->yoffset.
3199 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3200 text_view->first_para_mark);
3202 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3204 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3206 if (new_first_para_top != old_first_para_top)
3208 text_view->yoffset += new_first_para_top - old_first_para_top;
3210 get_vadjustment (text_view)->value = text_view->yoffset;
3211 yoffset_changed = TRUE;
3214 if (yoffset_changed)
3216 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3217 gtk_adjustment_value_changed (get_vadjustment (text_view));
3220 /* FIXME be smarter about which anchored widgets we update */
3222 tmp_list = text_view->children;
3223 while (tmp_list != NULL)
3225 GtkTextViewChild *child = tmp_list->data;
3228 gtk_text_view_update_child_allocation (text_view, child);
3230 tmp_list = g_slist_next (tmp_list);
3235 GtkRequisition old_req;
3236 GtkRequisition new_req;
3238 old_req = widget->requisition;
3240 /* Use this instead of gtk_widget_size_request wrapper
3241 * to avoid the optimization which just returns widget->requisition
3242 * if a resize hasn't been queued.
3244 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3246 if (old_req.width != new_req.width ||
3247 old_req.height != new_req.height)
3248 gtk_widget_queue_resize (widget);
3253 gtk_text_view_realize_cursor_gc (GtkTextView *text_view)
3255 GdkColor *cursor_color;
3256 GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
3258 if (text_view->cursor_gc)
3259 gdk_gc_unref (text_view->cursor_gc);
3261 gtk_widget_style_get (GTK_WIDGET (text_view), "cursor-color", &cursor_color, NULL);
3264 cursor_color = &red;
3266 text_view->cursor_gc = gdk_gc_new (text_view->text_window->bin_window);
3267 gdk_gc_set_rgb_fg_color (text_view->cursor_gc, cursor_color);
3271 gtk_text_view_realize (GtkWidget *widget)
3273 GtkTextView *text_view;
3274 GdkWindowAttr attributes;
3275 gint attributes_mask;
3278 text_view = GTK_TEXT_VIEW (widget);
3279 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3281 attributes.window_type = GDK_WINDOW_CHILD;
3282 attributes.x = widget->allocation.x;
3283 attributes.y = widget->allocation.y;
3284 attributes.width = widget->allocation.width;
3285 attributes.height = widget->allocation.height;
3286 attributes.wclass = GDK_INPUT_OUTPUT;
3287 attributes.visual = gtk_widget_get_visual (widget);
3288 attributes.colormap = gtk_widget_get_colormap (widget);
3289 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3291 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3293 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3294 &attributes, attributes_mask);
3295 gdk_window_set_user_data (widget->window, widget);
3297 /* must come before text_window_realize calls */
3298 widget->style = gtk_style_attach (widget->style, widget->window);
3300 gdk_window_set_background (widget->window,
3301 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3303 text_window_realize (text_view->text_window, widget->window);
3305 if (text_view->left_window)
3306 text_window_realize (text_view->left_window,
3309 if (text_view->top_window)
3310 text_window_realize (text_view->top_window,
3313 if (text_view->right_window)
3314 text_window_realize (text_view->right_window,
3317 if (text_view->bottom_window)
3318 text_window_realize (text_view->bottom_window,
3321 gtk_text_view_realize_cursor_gc (text_view);
3323 gtk_text_view_ensure_layout (text_view);
3325 if (text_view->buffer)
3326 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
3327 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3329 tmp_list = text_view->children;
3330 while (tmp_list != NULL)
3332 GtkTextViewChild *vc = tmp_list->data;
3334 text_view_child_set_parent_window (text_view, vc);
3336 tmp_list = tmp_list->next;
3341 gtk_text_view_unrealize (GtkWidget *widget)
3343 GtkTextView *text_view;
3345 text_view = GTK_TEXT_VIEW (widget);
3347 if (text_view->buffer)
3348 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
3349 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3351 if (text_view->cursor_gc)
3353 gdk_gc_unref (text_view->cursor_gc);
3354 text_view->cursor_gc = NULL;
3357 gtk_text_view_remove_validate_idles (text_view);
3359 if (text_view->popup_menu)
3361 gtk_widget_destroy (text_view->popup_menu);
3362 text_view->popup_menu = NULL;
3365 text_window_unrealize (text_view->text_window);
3367 if (text_view->left_window)
3368 text_window_unrealize (text_view->left_window);
3370 if (text_view->top_window)
3371 text_window_unrealize (text_view->top_window);
3373 if (text_view->right_window)
3374 text_window_unrealize (text_view->right_window);
3376 if (text_view->bottom_window)
3377 text_window_unrealize (text_view->bottom_window);
3379 gtk_text_view_destroy_layout (text_view);
3381 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3385 gtk_text_view_style_set (GtkWidget *widget,
3386 GtkStyle *previous_style)
3388 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3390 if (GTK_WIDGET_REALIZED (widget))
3392 gdk_window_set_background (widget->window,
3393 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3395 gdk_window_set_background (text_view->text_window->bin_window,
3396 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3398 if (text_view->left_window)
3399 gdk_window_set_background (text_view->left_window->bin_window,
3400 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3401 if (text_view->right_window)
3402 gdk_window_set_background (text_view->right_window->bin_window,
3403 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3405 if (text_view->top_window)
3406 gdk_window_set_background (text_view->top_window->bin_window,
3407 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3409 if (text_view->bottom_window)
3410 gdk_window_set_background (text_view->bottom_window->bin_window,
3411 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3413 gtk_text_view_realize_cursor_gc (text_view);
3416 if (text_view->layout && previous_style)
3418 gtk_text_view_set_attributes_from_style (text_view,
3419 text_view->layout->default_style,
3421 gtk_text_layout_default_style_changed (text_view->layout);
3426 gtk_text_view_direction_changed (GtkWidget *widget,
3427 GtkTextDirection previous_direction)
3429 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3431 if (text_view->layout)
3433 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3434 gtk_text_layout_default_style_changed (text_view->layout);
3440 set_invisible_cursor (GdkWindow *window)
3442 GdkBitmap *empty_bitmap;
3445 char invisible_cursor_bits[] = { 0x0 };
3447 useless.red = useless.green = useless.blue = 0;
3450 empty_bitmap = gdk_bitmap_create_from_data (window,
3451 invisible_cursor_bits,
3454 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3459 gdk_window_set_cursor (window, cursor);
3461 gdk_cursor_unref (cursor);
3463 g_object_unref (empty_bitmap);
3467 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3469 if (text_view->mouse_cursor_obscured)
3472 set_invisible_cursor (text_view->text_window->bin_window);
3474 text_view->mouse_cursor_obscured = TRUE;
3482 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3485 switch (event->type)
3487 case GDK_MOTION_NOTIFY:
3488 *x = event->motion.x;
3489 *y = event->motion.y;
3493 case GDK_BUTTON_PRESS:
3494 case GDK_2BUTTON_PRESS:
3495 case GDK_3BUTTON_PRESS:
3496 case GDK_BUTTON_RELEASE:
3497 *x = event->button.x;
3498 *y = event->button.y;
3503 case GDK_KEY_RELEASE:
3504 case GDK_ENTER_NOTIFY:
3505 case GDK_LEAVE_NOTIFY:
3506 case GDK_PROPERTY_NOTIFY:
3507 case GDK_SELECTION_CLEAR:
3508 case GDK_SELECTION_REQUEST:
3509 case GDK_SELECTION_NOTIFY:
3510 case GDK_PROXIMITY_IN:
3511 case GDK_PROXIMITY_OUT:
3512 case GDK_DRAG_ENTER:
3513 case GDK_DRAG_LEAVE:
3514 case GDK_DRAG_MOTION:
3515 case GDK_DRAG_STATUS:
3516 case GDK_DROP_START:
3517 case GDK_DROP_FINISHED:
3527 emit_event_on_tags (GtkWidget *widget,
3533 gboolean retval = FALSE;
3534 GtkTextView *text_view;
3536 text_view = GTK_TEXT_VIEW (widget);
3538 tags = gtk_text_iter_get_tags (iter);
3543 GtkTextTag *tag = tmp->data;
3545 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3551 tmp = g_slist_next (tmp);
3554 g_slist_free (tags);
3560 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3562 GtkTextView *text_view;
3565 text_view = GTK_TEXT_VIEW (widget);
3567 if (text_view->layout == NULL ||
3568 get_buffer (text_view) == NULL)
3571 if (event->any.window != text_view->text_window->bin_window)
3574 if (get_event_coordinates (event, &x, &y))
3578 x += text_view->xoffset;
3579 y += text_view->yoffset;
3581 /* FIXME this is slow and we do it twice per event.
3582 * My favorite solution is to have GtkTextLayout cache
3583 * the last couple lookups.
3585 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3589 return emit_event_on_tags (widget, event, &iter);
3591 else if (event->type == GDK_KEY_PRESS ||
3592 event->type == GDK_KEY_RELEASE)
3594 GtkTextMark *insert;
3597 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3600 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3602 return emit_event_on_tags (widget, event, &iter);
3609 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3611 gboolean retval = FALSE;
3612 gboolean obscure = FALSE;
3613 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3614 GtkTextMark *insert;
3617 if (text_view->layout == NULL ||
3618 get_buffer (text_view) == NULL)
3621 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3622 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3623 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3624 gtk_im_context_filter_keypress (text_view->im_context, event))
3626 text_view->need_im_reset = TRUE;
3630 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3631 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3633 else if (event->keyval == GDK_Return ||
3634 event->keyval == GDK_KP_Enter)
3636 gtk_text_view_commit_text (text_view, "\n");
3641 /* Pass through Tab as literal tab, unless Control is held down */
3642 else if ((event->keyval == GDK_Tab ||
3643 event->keyval == GDK_KP_Tab ||
3644 event->keyval == GDK_ISO_Left_Tab) &&
3645 !(event->state & GDK_CONTROL_MASK))
3647 /* If the text isn't editable, move the focus instead */
3648 if (text_view->editable)
3650 gtk_text_view_commit_text (text_view, "\t");
3654 gtk_text_view_move_focus (text_view,
3655 (event->state & GDK_SHIFT_MASK) ?
3656 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3664 gtk_text_view_obscure_mouse_cursor (text_view);
3666 gtk_text_view_pend_cursor_blink (text_view);
3672 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3674 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3675 GtkTextMark *insert;
3678 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3681 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3682 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3683 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3684 gtk_im_context_filter_keypress (text_view->im_context, event))
3686 text_view->need_im_reset = TRUE;
3690 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3694 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3696 GtkTextView *text_view;
3698 text_view = GTK_TEXT_VIEW (widget);
3700 text_view->disable_scroll_on_focus = TRUE;
3701 gtk_widget_grab_focus (widget);
3702 text_view->disable_scroll_on_focus = FALSE;
3704 if (event->window != text_view->text_window->bin_window)
3706 /* Remove selection if any. */
3707 gtk_text_view_unselect (text_view);
3713 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3714 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3715 else if (event->button == 3)
3716 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3719 if (event->type == GDK_BUTTON_PRESS)
3721 gtk_text_view_reset_im_context (text_view);
3723 if (event->button == 1)
3725 /* If we're in the selection, start a drag copy/move of the
3726 * selection; otherwise, start creating a new selection.
3729 GtkTextIter start, end;
3731 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3733 event->x + text_view->xoffset,
3734 event->y + text_view->yoffset);
3736 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3738 gtk_text_iter_in_range (&iter, &start, &end))
3740 text_view->drag_start_x = event->x;
3741 text_view->drag_start_y = event->y;
3742 text_view->pending_place_cursor_button = event->button;
3746 gtk_text_view_start_selection_drag (text_view, &iter, event);
3751 else if (event->button == 2)
3755 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3757 event->x + text_view->xoffset,
3758 event->y + text_view->yoffset);
3760 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3761 gtk_clipboard_get (GDK_SELECTION_PRIMARY),
3763 text_view->editable);
3766 else if (event->button == 3)
3768 gtk_text_view_do_popup (text_view, event);
3771 else if ((event->type == GDK_2BUTTON_PRESS ||
3772 event->type == GDK_3BUTTON_PRESS) &&
3775 GtkTextIter start, end;
3777 /* End the selection drag, otherwise we'd clear the new
3778 * word/line selection on button release
3780 gtk_text_view_end_selection_drag (text_view, event);
3782 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3784 event->x + text_view->xoffset,
3785 event->y + text_view->yoffset);
3789 if (event->type == GDK_2BUTTON_PRESS)
3791 if (gtk_text_iter_inside_word (&start))
3793 if (!gtk_text_iter_starts_word (&start))
3794 gtk_text_iter_backward_word_start (&start);
3796 if (!gtk_text_iter_ends_word (&end))
3797 gtk_text_iter_forward_word_end (&end);
3800 else if (event->type == GDK_3BUTTON_PRESS)
3802 if (gtk_text_view_starts_display_line (text_view, &start))
3804 /* If on a display line boundary, we assume the user
3805 * clicked off the end of a line and we therefore select
3806 * the line before the boundary.
3808 gtk_text_view_backward_display_line_start (text_view, &start);
3812 /* start isn't on the start of a line, so we move it to the
3813 * start, and move end to the end unless it's already there.
3815 gtk_text_view_backward_display_line_start (text_view, &start);
3817 if (!gtk_text_view_starts_display_line (text_view, &end))
3818 gtk_text_view_forward_display_line_end (text_view, &end);
3822 if (event->state & GDK_SHIFT_MASK)
3824 /* Take union of old and new selection */
3825 GtkTextIter old_start, old_end;
3827 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3828 &old_start, &old_end);
3830 gtk_text_iter_order (&start, &old_start);
3831 gtk_text_iter_order (&old_end, &end);
3833 /* Now start is the first of the starts, and end is the
3838 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3841 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3845 text_view->just_selected_element = TRUE;
3854 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3856 GtkTextView *text_view;
3858 text_view = GTK_TEXT_VIEW (widget);
3860 if (event->window != text_view->text_window->bin_window)
3863 if (event->button == 1)
3865 if (text_view->drag_start_x >= 0)
3867 text_view->drag_start_x = -1;
3868 text_view->drag_start_y = -1;
3871 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3873 else if (text_view->just_selected_element)
3875 text_view->just_selected_element = FALSE;
3878 else if (text_view->pending_place_cursor_button == event->button)
3882 /* Unselect everything; we clicked inside selection, but
3883 * didn't move by the drag threshold, so just clear selection
3886 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3888 event->x + text_view->xoffset,
3889 event->y + text_view->yoffset);
3891 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3893 text_view->pending_place_cursor_button = 0;
3903 keymap_direction_changed (GdkKeymap *keymap,
3904 GtkTextView *text_view)
3906 gtk_text_view_check_keymap_direction (text_view);
3910 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3912 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3914 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
3915 gtk_widget_queue_draw (widget);
3917 DV(g_print (G_STRLOC": focus_in_event\n"));
3919 if (text_view->cursor_visible && text_view->layout)
3921 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3922 gtk_text_view_check_cursor_blink (text_view);
3925 g_signal_connect (gdk_keymap_get_default (),
3926 "direction_changed",
3927 G_CALLBACK (keymap_direction_changed), text_view);
3928 gtk_text_view_check_keymap_direction (text_view);
3930 text_view->need_im_reset = TRUE;
3931 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3937 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3939 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3941 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
3942 gtk_widget_queue_draw (widget);
3944 DV(g_print (G_STRLOC": focus_out_event\n"));
3946 if (text_view->cursor_visible && text_view->layout)
3948 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3949 gtk_text_view_check_cursor_blink (text_view);
3952 g_signal_handlers_disconnect_by_func (gdk_keymap_get_default (),
3953 keymap_direction_changed,
3956 text_view->need_im_reset = TRUE;
3957 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3963 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3965 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3967 if (text_view->mouse_cursor_obscured)
3971 cursor = gdk_cursor_new (GDK_XTERM);
3972 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3973 gdk_cursor_unref (cursor);
3974 text_view->mouse_cursor_obscured = FALSE;
3977 if (event->window == text_view->text_window->bin_window &&
3978 text_view->drag_start_x >= 0)
3982 gdk_window_get_pointer (text_view->text_window->bin_window,
3985 if (gtk_drag_check_threshold (widget,
3986 text_view->drag_start_x,
3987 text_view->drag_start_y,
3991 gint buffer_x, buffer_y;
3993 gtk_text_view_window_to_buffer_coords (text_view,
3994 GTK_TEXT_WINDOW_TEXT,
3995 text_view->drag_start_x,
3996 text_view->drag_start_y,
4000 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4002 buffer_x, buffer_y);
4004 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4013 gtk_text_view_paint (GtkWidget *widget,
4015 GdkEventExpose *event)
4017 GtkTextView *text_view;
4018 GList *child_exposes;
4022 text_view = GTK_TEXT_VIEW (widget);
4024 g_return_if_fail (text_view->layout != NULL);
4025 g_return_if_fail (text_view->xoffset >= 0);
4026 g_return_if_fail (text_view->yoffset >= 0);
4028 while (text_view->first_validate_idle != 0)
4030 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4031 text_view->first_validate_idle));
4032 gtk_text_view_flush_first_validate (text_view);
4035 /* More regions could have become invalid in the above loop */
4036 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4041 gdk_region_get_clipbox (updates, &rect);
4043 gdk_rectangle_union (area, &rect, area);
4045 gdk_region_destroy (updates);
4048 if (!text_view->onscreen_validated)
4050 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.");
4055 printf ("painting %d,%d %d x %d\n",
4057 area->width, area->height);
4060 child_exposes = NULL;
4061 gtk_text_layout_draw (text_view->layout,
4063 text_view->text_window->bin_window,
4064 text_view->cursor_gc,
4068 area->width, area->height,
4071 tmp_list = child_exposes;
4072 while (tmp_list != NULL)
4074 GtkWidget *child = tmp_list->data;
4076 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4080 g_object_unref (G_OBJECT (child));
4082 tmp_list = tmp_list->next;
4085 g_list_free (child_exposes);
4089 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4093 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4094 GTK_TEXT_WINDOW_TEXT))
4096 DV(g_print (">Exposed ("G_STRLOC")\n"));
4097 gtk_text_view_paint (widget, &event->area, event);
4100 if (event->window == widget->window)
4101 gtk_text_view_draw_focus (widget);
4103 /* Propagate exposes to all children not in the buffer. */
4104 tmp_list = GTK_TEXT_VIEW (widget)->children;
4105 while (tmp_list != NULL)
4107 GtkTextViewChild *vc = tmp_list->data;
4109 /* propagate_expose checks that event->window matches
4112 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4113 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4117 tmp_list = tmp_list->next;
4124 gtk_text_view_draw_focus (GtkWidget *widget)
4126 gboolean interior_focus;
4128 /* We clear the focus if we are in interior focus mode. */
4129 gtk_widget_style_get (widget,
4130 "interior_focus", &interior_focus,
4133 if (GTK_WIDGET_DRAWABLE (widget))
4135 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4137 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4138 NULL, widget, "textview",
4140 widget->allocation.width,
4141 widget->allocation.height);
4145 gdk_window_clear (widget->window);
4151 gtk_text_view_grab_focus (GtkWidget *widget)
4153 GtkTextView *text_view;
4155 text_view = GTK_TEXT_VIEW (widget);
4157 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4159 if (!text_view->disable_scroll_on_focus)
4160 gtk_text_view_scroll_mark_onscreen (text_view,
4161 gtk_text_buffer_get_mark (get_buffer (text_view),
4166 gtk_text_view_focus (GtkWidget *widget,
4167 GtkDirectionType direction)
4169 GtkTextView *text_view;
4170 GtkContainer *container;
4172 text_view = GTK_TEXT_VIEW (widget);
4173 container = GTK_CONTAINER (widget);
4175 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4183 gtk_text_view_add (GtkContainer *container,
4186 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4187 g_return_if_fail (GTK_IS_WIDGET (child));
4189 /* This is pretty random. */
4190 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4192 GTK_TEXT_WINDOW_WIDGET,
4197 gtk_text_view_remove (GtkContainer *container,
4201 GtkTextView *text_view;
4202 GtkTextViewChild *vc;
4204 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4205 g_return_if_fail (GTK_IS_WIDGET (child));
4206 g_return_if_fail (child->parent == (GtkWidget*) container);
4208 text_view = GTK_TEXT_VIEW (container);
4211 iter = text_view->children;
4213 while (iter != NULL)
4217 if (vc->widget == child)
4220 iter = g_slist_next (iter);
4223 g_assert (iter != NULL); /* be sure we had the child in the list */
4225 text_view->children = g_slist_remove (text_view->children, vc);
4227 gtk_widget_unparent (vc->widget);
4229 text_view_child_free (vc);
4233 gtk_text_view_forall (GtkContainer *container,
4234 gboolean include_internals,
4235 GtkCallback callback,
4236 gpointer callback_data)
4239 GtkTextView *text_view;
4242 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4243 g_return_if_fail (callback != NULL);
4245 text_view = GTK_TEXT_VIEW (container);
4247 copy = g_slist_copy (text_view->children);
4250 while (iter != NULL)
4252 GtkTextViewChild *vc = iter->data;
4254 (* callback) (vc->widget, callback_data);
4256 iter = g_slist_next (iter);
4259 g_slist_free (copy);
4262 #define CURSOR_ON_MULTIPLIER 0.66
4263 #define CURSOR_OFF_MULTIPLIER 0.34
4264 #define CURSOR_PEND_MULTIPLIER 1.0
4267 cursor_blinks (GtkTextView *text_view)
4269 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4272 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4275 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4278 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4283 get_cursor_time (GtkTextView *text_view)
4285 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4288 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4298 blink_cb (gpointer data)
4300 GtkTextView *text_view;
4303 GDK_THREADS_ENTER ();
4305 text_view = GTK_TEXT_VIEW (data);
4307 g_assert (text_view->layout);
4308 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4309 g_assert (text_view->cursor_visible);
4311 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4314 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4318 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4322 gtk_text_layout_set_cursor_visible (text_view->layout,
4325 GDK_THREADS_LEAVE ();
4327 /* Remove ourselves */
4333 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4335 if (text_view->blink_timeout)
4337 gtk_timeout_remove (text_view->blink_timeout);
4338 text_view->blink_timeout = 0;
4343 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4345 if (text_view->layout != NULL &&
4346 text_view->cursor_visible &&
4347 GTK_WIDGET_HAS_FOCUS (text_view))
4349 if (cursor_blinks (text_view))
4351 if (text_view->blink_timeout == 0)
4353 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4355 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4361 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4365 gtk_text_view_stop_cursor_blink (text_view);
4370 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4372 if (text_view->layout != NULL &&
4373 text_view->cursor_visible &&
4374 GTK_WIDGET_HAS_FOCUS (text_view) &&
4375 cursor_blinks (text_view))
4377 if (text_view->blink_timeout != 0)
4379 gtk_timeout_remove (text_view->blink_timeout);
4380 text_view->blink_timeout = 0;
4383 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4385 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4393 * Key binding handlers
4397 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4398 GtkTextIter *newplace,
4403 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4409 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4414 /* FIXME when we are unfrozen and can change GtkMovementStep,
4417 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4420 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4421 GtkMovementStep step,
4423 gboolean extend_selection)
4426 GtkTextIter newplace;
4428 gint cursor_x_pos = 0;
4430 gtk_text_view_reset_im_context (text_view);
4432 if (step == GTK_MOVEMENT_PAGES)
4434 gtk_text_view_scroll_pages (text_view, count);
4435 gtk_text_view_pend_cursor_blink (text_view);
4438 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4440 gtk_text_view_scroll_hpages (text_view, count);
4441 gtk_text_view_pend_cursor_blink (text_view);
4445 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4446 gtk_text_buffer_get_mark (get_buffer (text_view),
4450 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4451 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4455 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4456 gtk_text_iter_forward_cursor_positions (&newplace, count);
4459 case GTK_MOVEMENT_VISUAL_POSITIONS:
4460 gtk_text_layout_move_iter_visually (text_view->layout,
4464 case GTK_MOVEMENT_WORDS:
4466 gtk_text_iter_backward_word_starts (&newplace, -count);
4468 gtk_text_iter_forward_word_ends (&newplace, count);
4471 case GTK_MOVEMENT_DISPLAY_LINES:
4472 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4473 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4476 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4478 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4479 else if (count < -1)
4480 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4483 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4486 case GTK_MOVEMENT_PARAGRAPHS:
4489 if (!gtk_text_iter_ends_line (&newplace))
4491 gtk_text_iter_forward_to_line_end (&newplace);
4497 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4499 gtk_text_iter_set_line_offset (&newplace, 0);
4506 gtk_text_iter_forward_lines (&newplace, count);
4507 gtk_text_iter_set_line_offset (&newplace, 0);
4511 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4514 if (!gtk_text_iter_ends_line (&newplace))
4515 gtk_text_iter_forward_to_line_end (&newplace);
4519 gtk_text_iter_set_line_offset (&newplace, 0);
4523 case GTK_MOVEMENT_BUFFER_ENDS:
4525 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4527 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4534 if (!gtk_text_iter_equal (&insert, &newplace))
4536 if (extend_selection)
4537 gtk_text_buffer_move_mark (get_buffer (text_view),
4538 gtk_text_buffer_get_mark (get_buffer (text_view),
4542 gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4544 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4545 gtk_text_view_scroll_mark_onscreen (text_view,
4546 gtk_text_buffer_get_mark (get_buffer (text_view),
4549 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4551 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4555 gtk_text_view_pend_cursor_blink (text_view);
4559 gtk_text_view_move_cursor (GtkTextView *text_view,
4560 GtkMovementStep step,
4562 gboolean extend_selection)
4564 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4568 gtk_text_view_page_horizontally (GtkTextView *text_view,
4570 gboolean extend_selection)
4572 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4573 count, extend_selection);
4577 gtk_text_view_set_anchor (GtkTextView *text_view)
4581 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4582 gtk_text_buffer_get_mark (get_buffer (text_view),
4585 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4589 gtk_text_view_scroll_pages (GtkTextView *text_view,
4595 gint cursor_x_pos, cursor_y_pos;
4596 GtkTextIter new_insert;
4600 g_return_if_fail (text_view->vadjustment != NULL);
4602 adj = text_view->vadjustment;
4604 /* Validate the region that will be brought into view by the cursor motion
4608 gtk_text_view_get_first_para_iter (text_view, &anchor);
4609 y0 = adj->page_size;
4610 y1 = adj->page_size + count * adj->page_increment;
4614 gtk_text_view_get_first_para_iter (text_view, &anchor);
4615 y0 = count * adj->page_increment + adj->page_size;
4619 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4620 /* FIXME do we need to update the adjustment ranges here? */
4622 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4624 /* already at top, just be sure we are at offset 0 */
4625 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4626 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4628 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4630 /* already at bottom, just be sure we are at the end */
4631 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4632 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4636 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4638 newval = adj->value;
4639 oldval = adj->value;
4641 newval += count * adj->page_increment;
4643 set_adjustment_clamped (adj, newval);
4644 cursor_y_pos += adj->value - oldval;
4646 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4647 clamp_iter_onscreen (text_view, &new_insert);
4648 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4650 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4653 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4654 * only guarantees 1 pixel onscreen.
4656 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4657 gtk_text_view_scroll_mark_onscreen (text_view,
4658 gtk_text_buffer_get_mark (get_buffer (text_view),
4663 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4669 gint cursor_x_pos, cursor_y_pos;
4670 GtkTextIter new_insert;
4673 g_return_if_fail (text_view->hadjustment != NULL);
4675 adj = text_view->hadjustment;
4677 /* Validate the line that we're moving within.
4679 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4681 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4682 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4683 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4684 /* FIXME do we need to update the adjustment ranges here? */
4686 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4688 /* already at far left, just be sure we are at offset 0 */
4689 gtk_text_iter_set_line_offset (&new_insert, 0);
4690 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4692 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4694 /* already at far right, just be sure we are at the end */
4695 gtk_text_iter_forward_to_line_end (&new_insert);
4696 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4700 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4702 newval = adj->value;
4703 oldval = adj->value;
4705 newval += count * adj->page_increment;
4707 set_adjustment_clamped (adj, newval);
4708 cursor_x_pos += adj->value - oldval;
4710 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4711 clamp_iter_onscreen (text_view, &new_insert);
4712 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4714 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4717 /* FIXME for lines shorter than the overall widget width, this results in a
4718 * "bounce" effect as we scroll to the right of the widget, then scroll
4719 * back to get the end of the line onscreen.
4720 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4723 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4724 * only guarantees 1 pixel onscreen.
4726 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4727 gtk_text_view_scroll_mark_onscreen (text_view,
4728 gtk_text_buffer_get_mark (get_buffer (text_view),
4733 whitespace (gunichar ch, gpointer user_data)
4735 return (ch == ' ' || ch == '\t');
4739 not_whitespace (gunichar ch, gpointer user_data)
4741 return !whitespace (ch, user_data);
4745 find_whitepace_region (const GtkTextIter *center,
4746 GtkTextIter *start, GtkTextIter *end)
4751 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4752 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4753 if (whitespace (gtk_text_iter_get_char (end), NULL))
4754 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4756 return !gtk_text_iter_equal (start, end);
4760 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4763 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4764 text_view->editable);
4768 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4775 gboolean leave_one = FALSE;
4777 gtk_text_view_reset_im_context (text_view);
4779 if (type == GTK_DELETE_CHARS)
4781 /* Char delete deletes the selection, if one exists */
4782 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4783 text_view->editable))
4787 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4789 gtk_text_buffer_get_mark (get_buffer (text_view),
4797 case GTK_DELETE_CHARS:
4798 gtk_text_iter_forward_cursor_positions (&end, count);
4801 case GTK_DELETE_WORD_ENDS:
4803 gtk_text_iter_forward_word_ends (&end, count);
4805 gtk_text_iter_backward_word_starts (&start, 0 - count);
4808 case GTK_DELETE_WORDS:
4811 case GTK_DELETE_DISPLAY_LINE_ENDS:
4814 case GTK_DELETE_DISPLAY_LINES:
4817 case GTK_DELETE_PARAGRAPH_ENDS:
4818 /* If we're already at a newline, we need to
4819 * simply delete that newline, instead of
4820 * moving to the next one.
4822 if (gtk_text_iter_ends_line (&end))
4824 gtk_text_iter_forward_line (&end);
4830 if (!gtk_text_iter_forward_to_line_end (&end))
4836 /* FIXME figure out what a negative count means
4840 case GTK_DELETE_PARAGRAPHS:
4843 gtk_text_iter_set_line_offset (&start, 0);
4844 gtk_text_iter_forward_to_line_end (&end);
4846 /* Do the lines beyond the first. */
4849 gtk_text_iter_forward_to_line_end (&end);
4855 /* FIXME negative count? */
4859 case GTK_DELETE_WHITESPACE:
4861 find_whitepace_region (&insert, &start, &end);
4869 if (!gtk_text_iter_equal (&start, &end))
4871 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4873 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4874 text_view->editable))
4877 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4879 text_view->editable);
4882 gtk_text_buffer_end_user_action (get_buffer (text_view));
4884 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4885 gtk_text_view_scroll_mark_onscreen (text_view,
4886 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4891 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4893 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4894 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4895 text_view->editable);
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),
4903 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4905 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4906 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
4907 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4908 gtk_text_view_scroll_mark_onscreen (text_view,
4909 gtk_text_buffer_get_mark (get_buffer (text_view),
4914 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4916 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4917 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4919 text_view->editable);
4920 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4921 gtk_text_view_scroll_mark_onscreen (text_view,
4922 gtk_text_buffer_get_mark (get_buffer (text_view),
4927 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4929 text_view->overwrite_mode = !text_view->overwrite_mode;
4933 gtk_text_view_move_focus (GtkTextView *text_view,
4934 GtkDirectionType direction_type)
4936 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
4938 if (!GTK_WIDGET_TOPLEVEL (toplevel))
4941 /* Propagate to toplevel */
4942 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus", direction_type);
4950 gtk_text_view_unselect (GtkTextView *text_view)
4954 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4956 gtk_text_buffer_get_mark (get_buffer (text_view),
4959 gtk_text_buffer_move_mark (get_buffer (text_view),
4960 gtk_text_buffer_get_mark (get_buffer (text_view),
4966 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
4967 const gchar *mark_name)
4970 GdkModifierType state;
4971 GtkTextIter newplace;
4973 /* DV(g_print (G_STRLOC": begin\n")); */
4975 gdk_window_get_pointer (text_view->text_window->bin_window,
4978 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
4979 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4981 x + text_view->xoffset,
4982 y + text_view->yoffset);
4986 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
4988 /* This may invalidate the layout */
4989 DV(g_print (G_STRLOC": move mark\n"));
4990 gtk_text_buffer_move_mark (get_buffer (text_view),
4994 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4995 gtk_text_view_scroll_mark_onscreen (text_view, mark);
4998 DV (g_print ("first validate idle leaving %s is %d\n",
4999 G_STRLOC, text_view->first_validate_idle));
5003 selection_scan_timeout (gpointer data)
5005 GtkTextView *text_view;
5007 GDK_THREADS_ENTER ();
5009 text_view = GTK_TEXT_VIEW (data);
5011 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5012 move_mark_to_pointer_and_scroll (text_view, "insert");
5014 GDK_THREADS_LEAVE ();
5016 return TRUE; /* remain installed. */
5019 #define DND_SCROLL_MARGIN 0.20
5022 drag_scan_timeout (gpointer data)
5024 GtkTextView *text_view;
5026 GdkModifierType state;
5027 GtkTextIter newplace;
5029 GDK_THREADS_ENTER ();
5031 text_view = GTK_TEXT_VIEW (data);
5033 gdk_window_get_pointer (text_view->text_window->bin_window,
5036 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5038 x + text_view->xoffset,
5039 y + text_view->yoffset);
5041 gtk_text_buffer_move_mark (get_buffer (text_view),
5042 text_view->dnd_mark,
5045 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5046 gtk_text_view_scroll_to_mark (text_view,
5047 text_view->dnd_mark,
5048 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5050 GDK_THREADS_LEAVE ();
5056 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5058 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5059 move_mark_to_pointer_and_scroll (text_view, "insert");
5061 /* If we had to scroll offscreen, insert a timeout to do so
5062 * again. Note that in the timeout, even if the mouse doesn't
5063 * move, due to this scroll xoffset/yoffset will have changed
5064 * and we'll need to scroll again.
5066 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5067 gtk_timeout_remove (text_view->scroll_timeout);
5069 text_view->scroll_timeout =
5070 gtk_timeout_add (50, selection_scan_timeout, text_view);
5076 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5077 const GtkTextIter *iter,
5078 GdkEventButton *button)
5080 GtkTextIter newplace;
5081 GtkTextBuffer *buffer;
5083 g_return_if_fail (text_view->selection_drag_handler == 0);
5085 gtk_grab_add (GTK_WIDGET (text_view));
5087 buffer = get_buffer (text_view);
5091 if (button->state & GDK_SHIFT_MASK)
5093 /* Extend selection */
5094 GtkTextIter start, end;
5096 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5098 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5100 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5103 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5106 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5108 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5111 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5117 /* Replace selection */
5118 gtk_text_buffer_place_cursor (buffer, &newplace);
5121 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5122 "motion_notify_event",
5123 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5127 /* returns whether we were really dragging */
5129 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5131 if (text_view->selection_drag_handler == 0)
5134 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5135 text_view->selection_drag_handler = 0;
5137 if (text_view->scroll_timeout != 0)
5139 gtk_timeout_remove (text_view->scroll_timeout);
5140 text_view->scroll_timeout = 0;
5143 /* one last update to current position */
5144 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5145 move_mark_to_pointer_and_scroll (text_view, "insert");
5147 gtk_grab_remove (GTK_WIDGET (text_view));
5157 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5158 GtkTextAttributes *values,
5161 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5162 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5165 pango_font_description_free (values->font);
5167 values->font = pango_font_description_copy (style->font_desc);
5171 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5173 if (text_view->layout)
5175 gboolean split_cursor;
5176 GtkTextDirection new_dir;
5177 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5179 g_object_get (G_OBJECT (settings),
5180 "gtk-split-cursor", &split_cursor,
5183 new_dir = GTK_TEXT_DIR_NONE;
5185 new_dir = (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
5186 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5188 if (text_view->layout->cursor_direction != new_dir)
5189 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5194 gtk_text_view_ensure_layout (GtkTextView *text_view)
5198 widget = GTK_WIDGET (text_view);
5200 if (text_view->layout == NULL)
5202 GtkTextAttributes *style;
5203 PangoContext *ltr_context, *rtl_context;
5206 DV(g_print(G_STRLOC"\n"));
5208 text_view->layout = gtk_text_layout_new ();
5210 g_signal_connect (G_OBJECT (text_view->layout),
5212 G_CALLBACK (invalidated_handler),
5215 g_signal_connect (G_OBJECT (text_view->layout),
5217 G_CALLBACK (changed_handler),
5220 g_signal_connect (G_OBJECT (text_view->layout),
5222 G_CALLBACK (gtk_text_view_child_allocated),
5225 if (get_buffer (text_view))
5226 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5228 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5229 gtk_text_view_pend_cursor_blink (text_view);
5231 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5233 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5234 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5235 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5236 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5238 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5240 g_object_unref (G_OBJECT (ltr_context));
5241 g_object_unref (G_OBJECT (rtl_context));
5243 gtk_text_view_check_keymap_direction (text_view);
5245 style = gtk_text_attributes_new ();
5247 gtk_widget_ensure_style (widget);
5248 gtk_text_view_set_attributes_from_style (text_view,
5249 style, widget->style);
5251 style->pixels_above_lines = text_view->pixels_above_lines;
5252 style->pixels_below_lines = text_view->pixels_below_lines;
5253 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5254 style->left_margin = text_view->left_margin;
5255 style->right_margin = text_view->right_margin;
5256 style->indent = text_view->indent;
5257 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5259 style->wrap_mode = text_view->wrap_mode;
5260 style->justification = text_view->justify;
5261 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5263 gtk_text_layout_set_default_style (text_view->layout, style);
5265 gtk_text_attributes_unref (style);
5267 /* Set layout for all anchored children */
5269 tmp_list = text_view->children;
5270 while (tmp_list != NULL)
5272 GtkTextViewChild *vc = tmp_list->data;
5276 gtk_text_anchored_child_set_layout (vc->widget,
5278 /* vc may now be invalid! */
5281 tmp_list = g_slist_next (tmp_list);
5287 * gtk_text_view_get_default_attributes:
5288 * @text_view: a #GtkTextView
5290 * Obtains a copy of the default text attributes. These are the
5291 * attributes used for text unless a tag overrides them.
5292 * You'd typically pass the default attributes in to
5293 * gtk_text_iter_get_attributes() in order to get the
5294 * attributes in effect at a given text position.
5296 * The return value is a copy owned by the caller of this function,
5297 * and should be freed.
5299 * Return value: a new #GtkTextAttributes
5302 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5304 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5306 gtk_text_view_ensure_layout (text_view);
5308 return gtk_text_attributes_copy (text_view->layout->default_style);
5312 gtk_text_view_destroy_layout (GtkTextView *text_view)
5314 if (text_view->layout)
5318 gtk_text_view_remove_validate_idles (text_view);
5320 /* Remove layout from all anchored children */
5321 tmp_list = text_view->children;
5322 while (tmp_list != NULL)
5324 GtkTextViewChild *vc = tmp_list->data;
5328 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5329 /* vc may now be invalid! */
5332 tmp_list = g_slist_next (tmp_list);
5335 gtk_text_view_stop_cursor_blink (text_view);
5336 gtk_text_view_end_selection_drag (text_view, NULL);
5338 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5339 invalidated_handler, text_view);
5340 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5341 changed_handler, text_view);
5342 g_object_unref (G_OBJECT (text_view->layout));
5343 text_view->layout = NULL;
5348 gtk_text_view_reset_im_context (GtkTextView *text_view)
5350 if (text_view->need_im_reset)
5352 text_view->need_im_reset = FALSE;
5353 gtk_im_context_reset (text_view->im_context);
5362 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5363 const GtkTextIter *iter,
5364 GdkEventMotion *event)
5366 GdkDragContext *context;
5367 GtkTargetList *target_list;
5369 text_view->drag_start_x = -1;
5370 text_view->drag_start_y = -1;
5371 text_view->pending_place_cursor_button = 0;
5373 target_list = gtk_target_list_new (target_table,
5374 G_N_ELEMENTS (target_table));
5376 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5377 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5378 1, (GdkEvent*)event);
5380 gtk_target_list_unref (target_list);
5382 gtk_drag_set_icon_default (context);
5386 gtk_text_view_drag_begin (GtkWidget *widget,
5387 GdkDragContext *context)
5393 gtk_text_view_drag_end (GtkWidget *widget,
5394 GdkDragContext *context)
5396 GtkTextView *text_view;
5398 text_view = GTK_TEXT_VIEW (widget);
5402 gtk_text_view_drag_data_get (GtkWidget *widget,
5403 GdkDragContext *context,
5404 GtkSelectionData *selection_data,
5408 GtkTextView *text_view;
5410 text_view = GTK_TEXT_VIEW (widget);
5412 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5414 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5416 gtk_selection_data_set (selection_data,
5417 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5430 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5433 /* Extract the selected text */
5434 str = gtk_text_iter_get_visible_text (&start, &end);
5439 gtk_selection_data_set_text (selection_data, str, -1);
5446 gtk_text_view_drag_data_delete (GtkWidget *widget,
5447 GdkDragContext *context)
5449 GtkTextView *text_view;
5451 text_view = GTK_TEXT_VIEW (widget);
5453 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5454 TRUE, GTK_TEXT_VIEW (widget)->editable);
5458 gtk_text_view_drag_leave (GtkWidget *widget,
5459 GdkDragContext *context,
5462 GtkTextView *text_view;
5464 text_view = GTK_TEXT_VIEW (widget);
5466 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5468 if (text_view->scroll_timeout != 0)
5469 gtk_timeout_remove (text_view->scroll_timeout);
5471 text_view->scroll_timeout = 0;
5475 gtk_text_view_drag_motion (GtkWidget *widget,
5476 GdkDragContext *context,
5481 GtkTextIter newplace;
5482 GtkTextView *text_view;
5485 GdkRectangle target_rect;
5487 GdkDragAction suggested_action = 0;
5489 text_view = GTK_TEXT_VIEW (widget);
5491 target_rect = text_view->text_window->allocation;
5493 if (x < target_rect.x ||
5494 y < target_rect.y ||
5495 x > (target_rect.x + target_rect.width) ||
5496 y > (target_rect.y + target_rect.height))
5497 return FALSE; /* outside the text window, allow parent widgets to handle event */
5499 gtk_text_view_window_to_buffer_coords (text_view,
5500 GTK_TEXT_WINDOW_WIDGET,
5504 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5508 if (gtk_drag_dest_find_target (widget, context,
5509 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5511 /* can't accept any of the offered targets */
5513 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5515 gtk_text_iter_in_range (&newplace, &start, &end))
5517 /* We're inside the selection. */
5521 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5523 GtkWidget *source_widget;
5525 suggested_action = context->suggested_action;
5527 source_widget = gtk_drag_get_source_widget (context);
5529 if (source_widget == widget)
5531 /* Default to MOVE, unless the user has
5532 * pressed ctrl or alt to affect available actions
5534 if ((context->actions & GDK_ACTION_MOVE) != 0)
5535 suggested_action = GDK_ACTION_MOVE;
5540 /* Can't drop here. */
5544 if (suggested_action != 0)
5546 gtk_text_mark_set_visible (text_view->dnd_mark,
5547 text_view->cursor_visible);
5549 gdk_drag_status (context, suggested_action, time);
5553 gdk_drag_status (context, 0, time);
5554 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5557 gtk_text_buffer_move_mark (get_buffer (text_view),
5558 text_view->dnd_mark,
5561 DV(g_print (G_STRLOC": scrolling to mark\n"));
5562 gtk_text_view_scroll_to_mark (text_view,
5563 text_view->dnd_mark,
5564 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5566 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5567 gtk_timeout_remove (text_view->scroll_timeout);
5569 text_view->scroll_timeout =
5570 gtk_timeout_add (50, drag_scan_timeout, text_view);
5572 /* TRUE return means don't propagate the drag motion to parent
5573 * widgets that may also be drop sites.
5579 gtk_text_view_drag_drop (GtkWidget *widget,
5580 GdkDragContext *context,
5585 GtkTextView *text_view;
5586 GtkTextIter drop_point;
5587 GdkAtom target = GDK_NONE;
5589 text_view = GTK_TEXT_VIEW (widget);
5591 if (text_view->scroll_timeout != 0)
5592 gtk_timeout_remove (text_view->scroll_timeout);
5594 text_view->scroll_timeout = 0;
5596 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5598 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5600 text_view->dnd_mark);
5602 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5603 target = gtk_drag_dest_find_target (widget, context, NULL);
5605 if (target != GDK_NONE)
5606 gtk_drag_get_data (widget, context, target, time);
5608 gtk_drag_finish (context, FALSE, FALSE, time);
5614 insert_text_data (GtkTextView *text_view,
5615 GtkTextIter *drop_point,
5616 GtkSelectionData *selection_data)
5620 str = gtk_selection_data_get_text (selection_data);
5624 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5625 drop_point, str, -1,
5626 text_view->editable);
5632 gtk_text_view_drag_data_received (GtkWidget *widget,
5633 GdkDragContext *context,
5636 GtkSelectionData *selection_data,
5640 GtkTextIter drop_point;
5641 GtkTextView *text_view;
5642 gboolean success = FALSE;
5644 text_view = GTK_TEXT_VIEW (widget);
5646 if (!text_view->dnd_mark)
5649 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5651 text_view->dnd_mark);
5653 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5656 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5658 GtkTextBuffer *src_buffer = NULL;
5659 GtkTextIter start, end;
5660 gboolean copy_tags = TRUE;
5662 if (selection_data->length != sizeof (src_buffer))
5665 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5667 if (src_buffer == NULL)
5670 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5672 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5673 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5676 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5681 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5685 text_view->editable);
5690 str = gtk_text_iter_get_visible_text (&start, &end);
5691 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5692 &drop_point, str, -1,
5693 text_view->editable);
5699 insert_text_data (text_view, &drop_point, selection_data);
5704 gtk_drag_finish (context, success,
5705 success && context->action == GDK_ACTION_MOVE,
5709 static GtkAdjustment*
5710 get_hadjustment (GtkTextView *text_view)
5712 if (text_view->hadjustment == NULL)
5713 gtk_text_view_set_scroll_adjustments (text_view,
5714 NULL, /* forces creation */
5715 text_view->vadjustment);
5717 return text_view->hadjustment;
5720 static GtkAdjustment*
5721 get_vadjustment (GtkTextView *text_view)
5723 if (text_view->vadjustment == NULL)
5724 gtk_text_view_set_scroll_adjustments (text_view,
5725 text_view->hadjustment,
5726 NULL); /* forces creation */
5727 return text_view->vadjustment;
5732 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5733 GtkAdjustment *hadj,
5734 GtkAdjustment *vadj)
5736 gboolean need_adjust = FALSE;
5738 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5741 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5743 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5745 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5747 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5749 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5751 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5752 g_object_unref (G_OBJECT (text_view->hadjustment));
5755 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5757 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5758 g_object_unref (G_OBJECT (text_view->vadjustment));
5761 if (text_view->hadjustment != hadj)
5763 text_view->hadjustment = hadj;
5764 g_object_ref (G_OBJECT (text_view->hadjustment));
5765 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5767 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5768 (GtkSignalFunc) gtk_text_view_value_changed,
5773 if (text_view->vadjustment != vadj)
5775 text_view->vadjustment = vadj;
5776 g_object_ref (G_OBJECT (text_view->vadjustment));
5777 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5779 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5780 (GtkSignalFunc) gtk_text_view_value_changed,
5786 gtk_text_view_value_changed (NULL, text_view);
5790 gtk_text_view_value_changed (GtkAdjustment *adj,
5791 GtkTextView *text_view)
5798 /* Note that we oddly call this function with adj == NULL
5802 text_view->onscreen_validated = FALSE;
5804 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5805 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5806 adj ? adj->value : 0.0));
5808 if (adj == text_view->hadjustment)
5810 dx = text_view->xoffset - (gint)adj->value;
5811 text_view->xoffset = adj->value;
5813 else if (adj == text_view->vadjustment)
5815 dy = text_view->yoffset - (gint)adj->value;
5816 text_view->yoffset = adj->value;
5818 if (text_view->layout)
5820 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5822 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5824 text_view->first_para_pixels = adj->value - line_top;
5828 if (dx != 0 || dy != 0)
5832 if (GTK_WIDGET_REALIZED (text_view))
5836 if (text_view->left_window)
5837 text_window_scroll (text_view->left_window, 0, dy);
5838 if (text_view->right_window)
5839 text_window_scroll (text_view->right_window, 0, dy);
5844 if (text_view->top_window)
5845 text_window_scroll (text_view->top_window, dx, 0);
5846 if (text_view->bottom_window)
5847 text_window_scroll (text_view->bottom_window, dx, 0);
5850 /* It looks nicer to scroll the main area last, because
5851 * it takes a while, and making the side areas update
5852 * afterward emphasizes the slowness of scrolling the
5855 text_window_scroll (text_view->text_window, dx, dy);
5858 /* Children are now "moved" in the text window, poke
5859 * into widget->allocation for each child
5861 tmp_list = text_view->children;
5862 while (tmp_list != NULL)
5864 GtkTextViewChild *child = tmp_list->data;
5868 child->widget->allocation.x -= dx;
5869 child->widget->allocation.y -= dy;
5872 g_print ("allocation for %p tweaked to %d,%d\n",
5874 child->widget->allocation.x,
5875 child->widget->allocation.y);
5879 tmp_list = g_slist_next (tmp_list);
5883 /* This could result in invalidation, which would install the
5884 * first_validate_idle, which would validate onscreen;
5885 * but we're going to go ahead and validate here, so
5886 * first_validate_idle shouldn't have anything to do.
5888 gtk_text_view_update_layout_width (text_view);
5890 /* note that validation of onscreen could invoke this function
5891 * recursively, by scrolling to maintain first_para, or in response
5892 * to updating the layout width, however there is no problem with
5893 * that, or shouldn't be.
5895 gtk_text_view_validate_onscreen (text_view);
5897 /* process exposes */
5898 if (GTK_WIDGET_REALIZED (text_view))
5900 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
5902 if (text_view->left_window)
5903 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
5905 if (text_view->right_window)
5906 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
5908 if (text_view->top_window)
5909 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
5911 if (text_view->bottom_window)
5912 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
5914 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
5917 /* If this got installed, get rid of it, it's just a waste of time. */
5918 if (text_view->first_validate_idle != 0)
5920 g_source_remove (text_view->first_validate_idle);
5921 text_view->first_validate_idle = 0;
5924 gtk_text_view_update_im_spot_location (text_view);
5926 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
5930 gtk_text_view_commit_handler (GtkIMContext *context,
5932 GtkTextView *text_view)
5934 gtk_text_view_commit_text (text_view, str);
5938 gtk_text_view_commit_text (GtkTextView *text_view,
5941 gboolean had_selection;
5943 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5945 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5948 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5949 text_view->editable);
5951 if (!strcmp (str, "\n"))
5953 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
5954 text_view->editable);
5958 if (!had_selection && text_view->overwrite_mode)
5959 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
5960 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5961 text_view->editable);
5964 gtk_text_buffer_end_user_action (get_buffer (text_view));
5966 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5967 gtk_text_view_scroll_mark_onscreen (text_view,
5968 gtk_text_buffer_get_mark (get_buffer (text_view),
5973 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
5974 GtkTextView *text_view)
5977 PangoAttrList *attrs;
5980 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
5981 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
5983 pango_attr_list_unref (attrs);
5988 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
5989 GtkTextView *text_view)
5996 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
5997 gtk_text_buffer_get_insert (text_view->buffer));
6000 pos = gtk_text_iter_get_line_index (&start);
6001 gtk_text_iter_set_line_offset (&start, 0);
6002 gtk_text_iter_forward_to_line_end (&end);
6004 text = gtk_text_iter_get_slice (&start, &end);
6005 gtk_im_context_set_surrounding (context, text, -1, pos);
6012 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6015 GtkTextView *text_view)
6020 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6021 gtk_text_buffer_get_insert (text_view->buffer));
6024 gtk_text_iter_forward_chars (&start, offset);
6025 gtk_text_iter_forward_chars (&end, offset + n_chars);
6027 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6033 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6034 const GtkTextIter *location,
6038 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6039 gboolean need_reset = FALSE;
6041 if (mark == gtk_text_buffer_get_insert (buffer))
6043 text_view->virtual_cursor_x = -1;
6044 text_view->virtual_cursor_y = -1;
6045 gtk_text_view_update_im_spot_location (text_view);
6048 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6054 gtk_text_view_reset_im_context (text_view);
6058 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6062 GdkRectangle strong_pos;
6065 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6066 gtk_text_buffer_get_mark (get_buffer (text_view),
6069 if ((x && text_view->virtual_cursor_x == -1) ||
6070 (y && text_view->virtual_cursor_y == -1))
6071 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6075 if (text_view->virtual_cursor_x != -1)
6076 *x = text_view->virtual_cursor_x;
6083 if (text_view->virtual_cursor_x != -1)
6084 *y = text_view->virtual_cursor_y;
6086 *y = strong_pos.y + strong_pos.height / 2;
6091 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6095 GdkRectangle strong_pos;
6098 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6099 gtk_text_buffer_get_mark (get_buffer (text_view),
6102 if (x == -1 || y == -1)
6103 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6105 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6106 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6109 /* Quick hack of a popup menu
6112 activate_cb (GtkWidget *menuitem,
6113 GtkTextView *text_view)
6115 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6116 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6120 append_action_signal (GtkTextView *text_view,
6122 const gchar *stock_id,
6123 const gchar *signal,
6126 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6128 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6129 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6130 GTK_SIGNAL_FUNC (activate_cb), text_view);
6132 gtk_widget_set_sensitive (menuitem, sensitive);
6134 gtk_widget_show (menuitem);
6135 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6139 popup_menu_detach (GtkWidget *attach_widget,
6142 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6146 popup_position_func (GtkMenu *menu,
6152 GtkTextView *text_view;
6154 GdkRectangle cursor_rect;
6155 GdkRectangle onscreen_rect;
6156 gint root_x, root_y;
6160 text_view = GTK_TEXT_VIEW (user_data);
6161 widget = GTK_WIDGET (text_view);
6163 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6165 gdk_window_get_origin (widget->window, &root_x, &root_y);
6167 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6169 gtk_text_buffer_get_insert (get_buffer (text_view)));
6171 gtk_text_view_get_iter_location (text_view,
6175 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6177 gtk_widget_size_request (text_view->popup_menu, &req);
6179 /* can't use rectangle_intersect since cursor rect can have 0 width */
6180 if (cursor_rect.x >= onscreen_rect.x &&
6181 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6182 cursor_rect.y >= onscreen_rect.y &&
6183 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6185 gtk_text_view_buffer_to_window_coords (text_view,
6186 GTK_TEXT_WINDOW_WIDGET,
6187 cursor_rect.x, cursor_rect.y,
6188 &cursor_rect.x, &cursor_rect.y);
6190 *x = root_x + cursor_rect.x + cursor_rect.width;
6191 *y = root_y + cursor_rect.y + cursor_rect.height;
6195 /* Just center the menu, since cursor is offscreen. */
6196 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6197 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6201 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6202 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6204 *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
6205 *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
6210 GtkTextView *text_view;
6216 range_contains_editable_text (const GtkTextIter *start,
6217 const GtkTextIter *end,
6218 gboolean default_editability)
6220 GtkTextIter iter = *start;
6222 while (gtk_text_iter_compare (&iter, end) < 0)
6224 if (gtk_text_iter_editable (&iter, default_editability))
6227 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6234 unichar_chosen_func (const char *text,
6237 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6239 gtk_text_view_commit_text (text_view, text);
6243 popup_targets_received (GtkClipboard *clipboard,
6244 GtkSelectionData *data,
6247 PopupInfo *info = user_data;
6248 GtkTextView *text_view = info->text_view;
6250 if (GTK_WIDGET_REALIZED (text_view))
6252 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6253 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6255 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6256 GtkWidget *menuitem;
6258 gboolean have_selection;
6259 gboolean can_insert;
6261 GtkTextIter sel_start, sel_end;
6263 if (text_view->popup_menu)
6264 gtk_widget_destroy (text_view->popup_menu);
6266 text_view->popup_menu = gtk_menu_new ();
6268 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6269 GTK_WIDGET (text_view),
6272 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6273 &sel_start, &sel_end);
6275 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6277 gtk_text_buffer_get_insert (get_buffer (text_view)));
6279 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6281 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6283 range_contains_editable_text (&sel_start, &sel_end,
6284 text_view->editable));
6285 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6287 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6288 can_insert && clipboard_contains_text);
6290 menuitem = gtk_separator_menu_item_new ();
6291 gtk_widget_show (menuitem);
6292 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6294 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6295 gtk_widget_show (menuitem);
6296 submenu = gtk_menu_new ();
6297 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6298 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6300 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6301 GTK_MENU_SHELL (submenu));
6303 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6304 gtk_widget_show (menuitem);
6305 gtk_widget_set_sensitive (menuitem, can_insert);
6307 submenu = gtk_menu_new ();
6308 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6309 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6311 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6312 unichar_chosen_func,
6315 gtk_signal_emit (GTK_OBJECT (text_view),
6316 signals[POPULATE_POPUP],
6317 text_view->popup_menu);
6320 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6322 info->button, info->time);
6325 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6326 popup_position_func, text_view,
6327 0, gtk_get_current_event_time ());
6328 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu));
6332 g_object_unref (text_view);
6337 gtk_text_view_do_popup (GtkTextView *text_view,
6338 GdkEventButton *event)
6340 PopupInfo *info = g_new (PopupInfo, 1);
6342 /* In order to know what entries we should make sensitive, we
6343 * ask for the current targets of the clipboard, and when
6344 * we get them, then we actually pop up the menu.
6346 info->text_view = g_object_ref (text_view);
6350 info->button = event->button;
6351 info->time = event->time;
6356 info->time = gtk_get_current_event_time ();
6359 gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
6360 gdk_atom_intern ("TARGETS", FALSE),
6361 popup_targets_received,
6366 gtk_text_view_popup_menu (GtkWidget *widget)
6368 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6372 /* Child GdkWindows */
6375 static GtkTextWindow*
6376 text_window_new (GtkTextWindowType type,
6379 gint height_request)
6383 win = g_new (GtkTextWindow, 1);
6386 win->widget = widget;
6388 win->bin_window = NULL;
6389 win->requisition.width = width_request;
6390 win->requisition.height = height_request;
6391 win->allocation.width = width_request;
6392 win->allocation.height = height_request;
6393 win->allocation.x = 0;
6394 win->allocation.y = 0;
6400 text_window_free (GtkTextWindow *win)
6403 text_window_unrealize (win);
6409 text_window_realize (GtkTextWindow *win,
6412 GdkWindowAttr attributes;
6413 gint attributes_mask;
6416 attributes.window_type = GDK_WINDOW_CHILD;
6417 attributes.x = win->allocation.x;
6418 attributes.y = win->allocation.y;
6419 attributes.width = win->allocation.width;
6420 attributes.height = win->allocation.height;
6421 attributes.wclass = GDK_INPUT_OUTPUT;
6422 attributes.visual = gtk_widget_get_visual (win->widget);
6423 attributes.colormap = gtk_widget_get_colormap (win->widget);
6424 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6426 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6428 win->window = gdk_window_new (parent,
6432 gdk_window_show (win->window);
6433 gdk_window_set_user_data (win->window, win->widget);
6437 attributes.width = win->allocation.width;
6438 attributes.height = win->allocation.height;
6439 attributes.event_mask = (GDK_EXPOSURE_MASK |
6441 GDK_KEY_PRESS_MASK |
6442 GDK_BUTTON_PRESS_MASK |
6443 GDK_BUTTON_RELEASE_MASK |
6444 GDK_POINTER_MOTION_MASK |
6445 GDK_POINTER_MOTION_HINT_MASK |
6446 gtk_widget_get_events (win->widget));
6448 win->bin_window = gdk_window_new (win->window,
6452 gdk_window_show (win->bin_window);
6453 gdk_window_set_user_data (win->bin_window, win->widget);
6455 if (win->type == GTK_TEXT_WINDOW_TEXT)
6458 cursor = gdk_cursor_new (GDK_XTERM);
6459 gdk_window_set_cursor (win->bin_window, cursor);
6460 gdk_cursor_unref (cursor);
6462 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6466 gdk_window_set_background (win->bin_window,
6467 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6471 gdk_window_set_background (win->bin_window,
6472 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6475 g_object_set_qdata (G_OBJECT (win->window),
6476 g_quark_from_static_string ("gtk-text-view-text-window"),
6479 g_object_set_qdata (G_OBJECT (win->bin_window),
6480 g_quark_from_static_string ("gtk-text-view-text-window"),
6485 text_window_unrealize (GtkTextWindow *win)
6487 if (win->type == GTK_TEXT_WINDOW_TEXT)
6489 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6493 gdk_window_set_user_data (win->window, NULL);
6494 gdk_window_set_user_data (win->bin_window, NULL);
6495 gdk_window_destroy (win->bin_window);
6496 gdk_window_destroy (win->window);
6498 win->bin_window = NULL;
6502 text_window_size_allocate (GtkTextWindow *win,
6505 win->allocation = *rect;
6509 gdk_window_move_resize (win->window,
6511 rect->width, rect->height);
6513 gdk_window_resize (win->bin_window,
6514 rect->width, rect->height);
6519 text_window_scroll (GtkTextWindow *win,
6523 if (dx != 0 || dy != 0)
6525 gdk_window_scroll (win->bin_window, dx, dy);
6530 text_window_invalidate_rect (GtkTextWindow *win,
6533 GdkRectangle window_rect;
6535 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6542 window_rect.width = rect->width;
6543 window_rect.height = rect->height;
6545 /* Adjust the rect as appropriate */
6549 case GTK_TEXT_WINDOW_TEXT:
6552 case GTK_TEXT_WINDOW_LEFT:
6553 case GTK_TEXT_WINDOW_RIGHT:
6555 window_rect.width = win->allocation.width;
6558 case GTK_TEXT_WINDOW_TOP:
6559 case GTK_TEXT_WINDOW_BOTTOM:
6561 window_rect.height = win->allocation.height;
6565 g_warning ("%s: bug!", G_STRLOC);
6570 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6574 GdkColor color = { 0, 65535, 0, 0 };
6575 GdkGC *gc = gdk_gc_new (win->bin_window);
6576 gdk_gc_set_rgb_fg_color (gc, &color);
6577 gdk_draw_rectangle (win->bin_window,
6578 gc, TRUE, window_rect.x, window_rect.y,
6579 window_rect.width, window_rect.height);
6586 text_window_get_width (GtkTextWindow *win)
6588 return win->allocation.width;
6592 text_window_get_height (GtkTextWindow *win)
6594 return win->allocation.height;
6598 text_window_get_allocation (GtkTextWindow *win,
6601 *rect = win->allocation;
6608 * gtk_text_view_get_window:
6609 * @text_view: a #GtkTextView
6610 * @win: window to get
6612 * Retrieves the #GdkWindow corresponding to an area of the text view;
6613 * possible windows include the overall widget window, child windows
6614 * on the left, right, top, bottom, and the window that displays the
6615 * text buffer. Windows are %NULL and nonexistent if their width or
6616 * height is 0, and are nonexistent before the widget has been
6619 * Return value: a #GdkWindow, or %NULL
6622 gtk_text_view_get_window (GtkTextView *text_view,
6623 GtkTextWindowType win)
6625 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6629 case GTK_TEXT_WINDOW_WIDGET:
6630 return GTK_WIDGET (text_view)->window;
6633 case GTK_TEXT_WINDOW_TEXT:
6634 return text_view->text_window->bin_window;
6637 case GTK_TEXT_WINDOW_LEFT:
6638 if (text_view->left_window)
6639 return text_view->left_window->bin_window;
6644 case GTK_TEXT_WINDOW_RIGHT:
6645 if (text_view->right_window)
6646 return text_view->right_window->bin_window;
6651 case GTK_TEXT_WINDOW_TOP:
6652 if (text_view->top_window)
6653 return text_view->top_window->bin_window;
6658 case GTK_TEXT_WINDOW_BOTTOM:
6659 if (text_view->bottom_window)
6660 return text_view->bottom_window->bin_window;
6665 case GTK_TEXT_WINDOW_PRIVATE:
6666 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6671 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6676 * gtk_text_view_get_window_type:
6677 * @text_view: a #GtkTextView
6678 * @window: a window type
6680 * Usually used to find out which window an event corresponds to.
6681 * If you connect to an event signal on @text_view, this function
6682 * should be called on <literal>event->window</literal> to
6683 * see which window it was.
6685 * Return value: the window type.
6688 gtk_text_view_get_window_type (GtkTextView *text_view,
6693 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6694 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6696 if (window == GTK_WIDGET (text_view)->window)
6697 return GTK_TEXT_WINDOW_WIDGET;
6699 win = g_object_get_qdata (G_OBJECT (window),
6700 g_quark_try_string ("gtk-text-view-text-window"));
6706 return GTK_TEXT_WINDOW_PRIVATE;
6711 buffer_to_widget (GtkTextView *text_view,
6717 gint focus_edge_width;
6718 gboolean interior_focus;
6721 gtk_widget_style_get (GTK_WIDGET (text_view),
6722 "interior_focus", &interior_focus,
6723 "focus_line_width", &focus_width,
6727 focus_edge_width = 0;
6729 focus_edge_width = focus_width;
6733 *window_x = buffer_x - text_view->xoffset + focus_edge_width;
6734 if (text_view->left_window)
6735 *window_x += text_view->left_window->allocation.width;
6740 *window_y = buffer_y - text_view->yoffset + focus_edge_width;
6741 if (text_view->top_window)
6742 *window_y += text_view->top_window->allocation.height;
6747 widget_to_text_window (GtkTextWindow *win,
6754 *window_x = widget_x - win->allocation.x;
6757 *window_y = widget_y - win->allocation.y;
6761 buffer_to_text_window (GtkTextView *text_view,
6770 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6771 "for a nonexistent or private child window of GtkTextView");
6775 buffer_to_widget (text_view,
6777 window_x, window_y);
6779 widget_to_text_window (win,
6780 window_x ? *window_x : 0,
6781 window_y ? *window_y : 0,
6787 * gtk_text_view_buffer_to_window_coords:
6788 * @text_view: a #GtkTextView
6789 * @win: a #GtkTextWindowType
6790 * @buffer_x: buffer x coordinate
6791 * @buffer_y: buffer y coordinate
6792 * @window_x: window x coordinate return location
6793 * @window_y: window y coordinate return location
6795 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6796 * @win, and stores the result in (@window_x, @window_y).
6799 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6800 GtkTextWindowType win,
6806 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6810 case GTK_TEXT_WINDOW_WIDGET:
6811 buffer_to_widget (text_view,
6813 window_x, window_y);
6816 case GTK_TEXT_WINDOW_TEXT:
6818 *window_x = buffer_x - text_view->xoffset;
6820 *window_y = buffer_y - text_view->yoffset;
6823 case GTK_TEXT_WINDOW_LEFT:
6824 buffer_to_text_window (text_view,
6825 text_view->left_window,
6827 window_x, window_y);
6830 case GTK_TEXT_WINDOW_RIGHT:
6831 buffer_to_text_window (text_view,
6832 text_view->right_window,
6834 window_x, window_y);
6837 case GTK_TEXT_WINDOW_TOP:
6838 buffer_to_text_window (text_view,
6839 text_view->top_window,
6841 window_x, window_y);
6844 case GTK_TEXT_WINDOW_BOTTOM:
6845 buffer_to_text_window (text_view,
6846 text_view->bottom_window,
6848 window_x, window_y);
6851 case GTK_TEXT_WINDOW_PRIVATE:
6852 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6856 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6862 widget_to_buffer (GtkTextView *text_view,
6868 gint focus_edge_width;
6869 gboolean interior_focus;
6872 gtk_widget_style_get (GTK_WIDGET (text_view),
6873 "interior_focus", &interior_focus,
6874 "focus_line_width", &focus_width,
6878 focus_edge_width = 0;
6880 focus_edge_width = focus_width;
6884 *buffer_x = widget_x - focus_edge_width + text_view->xoffset;
6885 if (text_view->left_window)
6886 *buffer_x -= text_view->left_window->allocation.width;
6891 *buffer_y = widget_y - focus_edge_width + text_view->yoffset;
6892 if (text_view->top_window)
6893 *buffer_y -= text_view->top_window->allocation.height;
6898 text_window_to_widget (GtkTextWindow *win,
6905 *widget_x = window_x + win->allocation.x;
6908 *widget_y = window_y + win->allocation.y;
6912 text_window_to_buffer (GtkTextView *text_view,
6921 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
6922 "coordinates for a nonexistent child window.");
6926 text_window_to_widget (win,
6932 widget_to_buffer (text_view,
6933 buffer_x ? *buffer_x : 0,
6934 buffer_y ? *buffer_y : 0,
6940 * gtk_text_view_window_to_buffer_coords:
6941 * @text_view: a #GtkTextView
6942 * @win: a #GtkTextWindowType
6943 * @window_x: window x coordinate
6944 * @window_y: window y coordinate
6945 * @buffer_x: buffer x coordinate return location
6946 * @buffer_y: buffer y coordinate return location
6948 * Converts coordinates on the window identified by @win to buffer
6949 * coordinates, storing the result in (@buffer_x,@buffer_y).
6952 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
6953 GtkTextWindowType win,
6959 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6963 case GTK_TEXT_WINDOW_WIDGET:
6964 widget_to_buffer (text_view,
6966 buffer_x, buffer_y);
6969 case GTK_TEXT_WINDOW_TEXT:
6971 *buffer_x = window_x + text_view->xoffset;
6973 *buffer_y = window_y + text_view->yoffset;
6976 case GTK_TEXT_WINDOW_LEFT:
6977 text_window_to_buffer (text_view,
6978 text_view->left_window,
6980 buffer_x, buffer_y);
6983 case GTK_TEXT_WINDOW_RIGHT:
6984 text_window_to_buffer (text_view,
6985 text_view->right_window,
6987 buffer_x, buffer_y);
6990 case GTK_TEXT_WINDOW_TOP:
6991 text_window_to_buffer (text_view,
6992 text_view->top_window,
6994 buffer_x, buffer_y);
6997 case GTK_TEXT_WINDOW_BOTTOM:
6998 text_window_to_buffer (text_view,
6999 text_view->bottom_window,
7001 buffer_x, buffer_y);
7004 case GTK_TEXT_WINDOW_PRIVATE:
7005 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7009 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7015 set_window_width (GtkTextView *text_view,
7017 GtkTextWindowType type,
7018 GtkTextWindow **winp)
7024 text_window_free (*winp);
7026 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7033 *winp = text_window_new (type,
7034 GTK_WIDGET (text_view),
7036 /* if the widget is already realized we need to realize the child manually */
7037 if (GTK_WIDGET_REALIZED (text_view))
7038 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7042 if ((*winp)->requisition.width == width)
7045 (*winp)->requisition.width = width;
7048 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7054 set_window_height (GtkTextView *text_view,
7056 GtkTextWindowType type,
7057 GtkTextWindow **winp)
7063 text_window_free (*winp);
7065 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7072 *winp = text_window_new (type,
7073 GTK_WIDGET (text_view),
7076 /* if the widget is already realized we need to realize the child manually */
7077 if (GTK_WIDGET_REALIZED (text_view))
7078 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7082 if ((*winp)->requisition.height == height)
7085 (*winp)->requisition.height = height;
7088 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7093 * gtk_text_view_set_border_window_size:
7094 * @text_view: a #GtkTextView
7095 * @type: window to affect
7096 * @size: width or height of the window
7098 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7099 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7100 * Automatically destroys the corresponding window if the size is set
7101 * to 0, and creates the window if the size is set to non-zero. This
7102 * function can only be used for the "border windows," it doesn't work
7103 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7104 * #GTK_TEXT_WINDOW_PRIVATE.
7107 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7108 GtkTextWindowType type,
7112 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7113 g_return_if_fail (size >= 0);
7117 case GTK_TEXT_WINDOW_LEFT:
7118 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7119 &text_view->left_window);
7122 case GTK_TEXT_WINDOW_RIGHT:
7123 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7124 &text_view->right_window);
7127 case GTK_TEXT_WINDOW_TOP:
7128 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7129 &text_view->top_window);
7132 case GTK_TEXT_WINDOW_BOTTOM:
7133 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7134 &text_view->bottom_window);
7138 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7144 * gtk_text_view_get_border_window_size:
7145 * @text_view: a #GtkTextView
7146 * @type: window to return size from
7148 * Gets the width of the specified border window. See
7149 * gtk_text_view_set_border_window_size().
7151 * Return value: width of window
7154 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7155 GtkTextWindowType type)
7157 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7161 case GTK_TEXT_WINDOW_LEFT:
7162 if (text_view->left_window)
7163 return text_view->left_window->requisition.width;
7165 case GTK_TEXT_WINDOW_RIGHT:
7166 if (text_view->right_window)
7167 return text_view->right_window->requisition.width;
7169 case GTK_TEXT_WINDOW_TOP:
7170 if (text_view->top_window)
7171 return text_view->top_window->requisition.height;
7173 case GTK_TEXT_WINDOW_BOTTOM:
7174 if (text_view->bottom_window)
7175 return text_view->bottom_window->requisition.height;
7178 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7189 static GtkTextViewChild*
7190 text_view_child_new_anchored (GtkWidget *child,
7191 GtkTextChildAnchor *anchor,
7192 GtkTextLayout *layout)
7194 GtkTextViewChild *vc;
7196 vc = g_new (GtkTextViewChild, 1);
7199 vc->anchor = anchor;
7201 vc->from_top_of_line = 0;
7202 vc->from_left_of_buffer = 0;
7204 g_object_ref (G_OBJECT (vc->widget));
7205 g_object_ref (G_OBJECT (vc->anchor));
7207 g_object_set_data (G_OBJECT (child),
7208 "gtk-text-view-child",
7211 gtk_text_child_anchor_register_child (anchor, child, layout);
7216 static GtkTextViewChild*
7217 text_view_child_new_window (GtkWidget *child,
7218 GtkTextWindowType type,
7222 GtkTextViewChild *vc;
7224 vc = g_new (GtkTextViewChild, 1);
7229 vc->from_top_of_line = 0;
7230 vc->from_left_of_buffer = 0;
7232 g_object_ref (G_OBJECT (vc->widget));
7238 g_object_set_data (G_OBJECT (child),
7239 "gtk-text-view-child",
7246 text_view_child_free (GtkTextViewChild *child)
7248 g_object_set_data (G_OBJECT (child->widget),
7249 "gtk-text-view-child", NULL);
7253 gtk_text_child_anchor_unregister_child (child->anchor,
7255 g_object_unref (G_OBJECT (child->anchor));
7258 g_object_unref (G_OBJECT (child->widget));
7264 text_view_child_set_parent_window (GtkTextView *text_view,
7265 GtkTextViewChild *vc)
7268 gtk_widget_set_parent_window (vc->widget,
7269 text_view->text_window->bin_window);
7273 window = gtk_text_view_get_window (text_view,
7275 gtk_widget_set_parent_window (vc->widget, window);
7280 add_child (GtkTextView *text_view,
7281 GtkTextViewChild *vc)
7283 text_view->children = g_slist_prepend (text_view->children,
7286 if (GTK_WIDGET_REALIZED (text_view))
7287 text_view_child_set_parent_window (text_view, vc);
7289 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7293 * gtk_text_view_add_child_at_anchor:
7294 * @text_view: a #GtkTextView
7295 * @child: a #GtkWidget
7296 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7298 * Adds a child widget in the text buffer, at the given @anchor.
7302 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7304 GtkTextChildAnchor *anchor)
7306 GtkTextViewChild *vc;
7308 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7309 g_return_if_fail (GTK_IS_WIDGET (child));
7310 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7311 g_return_if_fail (child->parent == NULL);
7313 gtk_text_view_ensure_layout (text_view);
7315 vc = text_view_child_new_anchored (child, anchor,
7318 add_child (text_view, vc);
7320 g_assert (vc->widget == child);
7321 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7325 * gtk_text_view_add_child_in_window:
7326 * @text_view: a #GtkTextView
7327 * @child: a #GtkWidget
7328 * @which_window: which window the child should appear in
7329 * @xpos: X position of child in window coordinates
7330 * @ypos: Y position of child in window coordinates
7332 * Adds a child at fixed coordinates in one of the text widget's
7333 * windows. The window must have nonzero size (see
7334 * gtk_text_view_set_border_window_size()). Note that the child
7335 * coordinates are given relative to the #GdkWindow in question, and
7336 * that these coordinates have no sane relationship to scrolling. When
7337 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7338 * irrelevant, the child floats above all scrollable areas. But when
7339 * placing a child in one of the scrollable windows (border windows or
7340 * text window), you'll need to compute the child's correct position
7341 * in buffer coordinates any time scrolling occurs or buffer changes
7342 * occur, and then call gtk_text_view_move_child() to update the
7343 * child's position. Unfortunately there's no good way to detect that
7344 * scrolling has occurred, using the current API; a possible hack
7345 * would be to update all child positions when the scroll adjustments
7346 * change or the text buffer changes. See bug 64518 on
7347 * bugzilla.gnome.org for status of fixing this issue.
7351 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7353 GtkTextWindowType which_window,
7357 GtkTextViewChild *vc;
7359 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7360 g_return_if_fail (GTK_IS_WIDGET (child));
7361 g_return_if_fail (child->parent == NULL);
7363 vc = text_view_child_new_window (child, which_window,
7366 add_child (text_view, vc);
7368 g_assert (vc->widget == child);
7369 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7373 * gtk_text_view_move_child:
7374 * @text_view: a #GtkTextView
7375 * @child: child widget already added to the text view
7376 * @xpos: new X position in window coordinates
7377 * @ypos: new Y position in window coordinates
7379 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7383 gtk_text_view_move_child (GtkTextView *text_view,
7388 GtkTextViewChild *vc;
7390 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7391 g_return_if_fail (GTK_IS_WIDGET (child));
7392 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7394 vc = g_object_get_data (G_OBJECT (child),
7395 "gtk-text-view-child");
7397 g_assert (vc != NULL);
7399 if (vc->x == xpos &&
7406 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7407 gtk_widget_queue_resize (child);
7411 /* Iterator operations */
7414 * gtk_text_view_forward_display_line:
7415 * @text_view: a #GtkTextView
7416 * @iter: a #GtkTextIter
7418 * Moves the given @iter forward by one display (wrapped) line. A
7419 * display line is different from a paragraph. Paragraphs are
7420 * separated by newlines or other paragraph separator characters.
7421 * Display lines are created by line-wrapping a paragraph. If
7422 * wrapping is turned off, display lines and paragraphs will be the
7423 * same. Display lines are divided differently for each view, since
7424 * they depend on the view's width; paragraphs are the same in all
7425 * views, since they depend on the contents of the #GtkTextBuffer.
7427 * Return value: %TRUE if @iter was moved and is not on the end iterator
7430 gtk_text_view_forward_display_line (GtkTextView *text_view,
7433 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7434 g_return_val_if_fail (iter != NULL, FALSE);
7436 gtk_text_view_ensure_layout (text_view);
7438 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7442 * gtk_text_view_backward_display_line:
7443 * @text_view: a #GtkTextView
7444 * @iter: a #GtkTextIter
7446 * Moves the given @iter backward by one display (wrapped) line. A
7447 * display line is different from a paragraph. Paragraphs are
7448 * separated by newlines or other paragraph separator characters.
7449 * Display lines are created by line-wrapping a paragraph. If
7450 * wrapping is turned off, display lines and paragraphs will be the
7451 * same. Display lines are divided differently for each view, since
7452 * they depend on the view's width; paragraphs are the same in all
7453 * views, since they depend on the contents of the #GtkTextBuffer.
7455 * Return value: %TRUE if @iter was moved and is not on the end iterator
7458 gtk_text_view_backward_display_line (GtkTextView *text_view,
7461 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7462 g_return_val_if_fail (iter != NULL, FALSE);
7464 gtk_text_view_ensure_layout (text_view);
7466 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7470 * gtk_text_view_forward_display_line_end:
7471 * @text_view: a #GtkTextView
7472 * @iter: a #GtkTextIter
7474 * Moves the given @iter forward to the next display line end. A
7475 * display line is different from a paragraph. Paragraphs are
7476 * separated by newlines or other paragraph separator characters.
7477 * Display lines are created by line-wrapping a paragraph. If
7478 * wrapping is turned off, display lines and paragraphs will be the
7479 * same. Display lines are divided differently for each view, since
7480 * they depend on the view's width; paragraphs are the same in all
7481 * views, since they depend on the contents of the #GtkTextBuffer.
7483 * Return value: %TRUE if @iter was moved and is not on the end iterator
7486 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7489 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7490 g_return_val_if_fail (iter != NULL, FALSE);
7492 gtk_text_view_ensure_layout (text_view);
7494 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7498 * gtk_text_view_backward_display_line_start:
7499 * @text_view: a #GtkTextView
7500 * @iter: a #GtkTextIter
7502 * Moves the given @iter backward to the next display line start. A
7503 * display line is different from a paragraph. Paragraphs are
7504 * separated by newlines or other paragraph separator characters.
7505 * Display lines are created by line-wrapping a paragraph. If
7506 * wrapping is turned off, display lines and paragraphs will be the
7507 * same. Display lines are divided differently for each view, since
7508 * they depend on the view's width; paragraphs are the same in all
7509 * views, since they depend on the contents of the #GtkTextBuffer.
7511 * Return value: %TRUE if @iter was moved and is not on the end iterator
7514 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7517 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7518 g_return_val_if_fail (iter != NULL, FALSE);
7520 gtk_text_view_ensure_layout (text_view);
7522 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7526 * gtk_text_view_starts_display_line:
7527 * @text_view: a #GtkTextView
7528 * @iter: a #GtkTextIter
7530 * Determines whether @iter is at the start of a display line.
7531 * See gtk_text_view_forward_display_line() for an explanation of
7532 * display lines vs. paragraphs.
7534 * Return value: %TRUE if @iter begins a wrapped line
7537 gtk_text_view_starts_display_line (GtkTextView *text_view,
7538 const GtkTextIter *iter)
7540 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7541 g_return_val_if_fail (iter != NULL, FALSE);
7543 gtk_text_view_ensure_layout (text_view);
7545 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7549 * gtk_text_view_move_visually:
7550 * @text_view: a #GtkTextView
7551 * @iter: a #GtkTextIter
7552 * @count: number of lines to move
7554 * Moves @iter up or down by @count display (wrapped) lines.
7555 * See gtk_text_view_forward_display_line() for an explanation of
7556 * display lines vs. paragraphs.
7558 * Return value: %TRUE if @iter moved and is not on the end iterator
7561 gtk_text_view_move_visually (GtkTextView *text_view,
7565 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7566 g_return_val_if_fail (iter != NULL, FALSE);
7568 gtk_text_view_ensure_layout (text_view);
7570 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);