1 /* GTK - The GIMP Toolkit
2 * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
30 #include "gtkbindings.h"
32 #include "gtkimagemenuitem.h"
35 #include "gtkmarshalers.h"
37 #include "gtkmenuitem.h"
38 #include "gtkseparatormenuitem.h"
39 #include "gtksettings.h"
40 #include "gtksignal.h"
42 #include "gtktextdisplay.h"
43 #include "gtktextview.h"
44 #include "gtkimmulticontext.h"
45 #include "gdk/gdkkeysyms.h"
46 #include "gtksizegroup.h" /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
47 #include "gtktextutil.h"
48 #include "gtkwindow.h"
51 /* How scrolling, validation, exposes, etc. work.
53 * The expose_event handler has the invariant that the onscreen lines
54 * have been validated.
56 * There are two ways that onscreen lines can become invalid. The first
57 * is to change which lines are onscreen. This happens when the value
58 * of a scroll adjustment changes. So the code path begins in
59 * gtk_text_view_value_changed() and goes like this:
60 * - gdk_window_scroll() to reflect the new adjustment value
61 * - validate the lines that were moved onscreen
62 * - gdk_window_process_updates() to handle the exposes immediately
64 * The second way is that you get the "invalidated" signal from the layout,
65 * indicating that lines have become invalid. This code path begins in
66 * invalidated_handler() and goes like this:
67 * - install high-priority idle which does the rest of the steps
68 * - if a scroll is pending from scroll_to_mark(), do the scroll,
69 * jumping to the gtk_text_view_value_changed() code path
70 * - otherwise, validate the onscreen lines
71 * - DO NOT process updates
73 * In both cases, validating the onscreen lines can trigger a scroll
74 * due to maintaining the first_para on the top of the screen.
75 * If validation triggers a scroll, we jump to the top of the code path
76 * for value_changed, and bail out of the current code path.
78 * Also, in size_allocate, if we invalidate some lines from changing
79 * the layout width, we need to go ahead and run the high-priority idle,
80 * because GTK sends exposes right after doing the size allocates without
81 * returning to the main loop. This is also why the high-priority idle
82 * is at a higher priority than resizing.
87 #define DEBUG_VALIDATION_AND_SCROLLING
90 #ifdef DEBUG_VALIDATION_AND_SCROLLING
96 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
97 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
99 struct _GtkTextPendingScroll
102 gdouble within_margin;
110 SET_SCROLL_ADJUSTMENTS,
128 PROP_PIXELS_ABOVE_LINES,
129 PROP_PIXELS_BELOW_LINES,
130 PROP_PIXELS_INSIDE_WRAP,
142 static void gtk_text_view_init (GtkTextView *text_view);
143 static void gtk_text_view_class_init (GtkTextViewClass *klass);
144 static void gtk_text_view_destroy (GtkObject *object);
145 static void gtk_text_view_finalize (GObject *object);
146 static void gtk_text_view_set_property (GObject *object,
150 static void gtk_text_view_get_property (GObject *object,
154 static void gtk_text_view_size_request (GtkWidget *widget,
155 GtkRequisition *requisition);
156 static void gtk_text_view_size_allocate (GtkWidget *widget,
157 GtkAllocation *allocation);
158 static void gtk_text_view_realize (GtkWidget *widget);
159 static void gtk_text_view_unrealize (GtkWidget *widget);
160 static void gtk_text_view_style_set (GtkWidget *widget,
161 GtkStyle *previous_style);
162 static void gtk_text_view_direction_changed (GtkWidget *widget,
163 GtkTextDirection previous_direction);
164 static gint gtk_text_view_event (GtkWidget *widget,
166 static gint gtk_text_view_key_press_event (GtkWidget *widget,
168 static gint gtk_text_view_key_release_event (GtkWidget *widget,
170 static gint gtk_text_view_button_press_event (GtkWidget *widget,
171 GdkEventButton *event);
172 static gint gtk_text_view_button_release_event (GtkWidget *widget,
173 GdkEventButton *event);
174 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
175 GdkEventFocus *event);
176 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
177 GdkEventFocus *event);
178 static gint gtk_text_view_motion_event (GtkWidget *widget,
179 GdkEventMotion *event);
180 static gint gtk_text_view_expose_event (GtkWidget *widget,
181 GdkEventExpose *expose);
182 static void gtk_text_view_draw_focus (GtkWidget *widget);
183 static void gtk_text_view_grab_focus (GtkWidget *widget);
184 static gboolean gtk_text_view_focus (GtkWidget *widget,
185 GtkDirectionType direction);
188 /* Source side drag signals */
189 static void gtk_text_view_drag_begin (GtkWidget *widget,
190 GdkDragContext *context);
191 static void gtk_text_view_drag_end (GtkWidget *widget,
192 GdkDragContext *context);
193 static void gtk_text_view_drag_data_get (GtkWidget *widget,
194 GdkDragContext *context,
195 GtkSelectionData *selection_data,
198 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
199 GdkDragContext *context);
201 /* Target side drag signals */
202 static void gtk_text_view_drag_leave (GtkWidget *widget,
203 GdkDragContext *context,
205 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
206 GdkDragContext *context,
210 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
211 GdkDragContext *context,
215 static void gtk_text_view_drag_data_received (GtkWidget *widget,
216 GdkDragContext *context,
219 GtkSelectionData *selection_data,
223 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
225 GtkAdjustment *vadj);
226 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
228 static void gtk_text_view_move_cursor (GtkTextView *text_view,
229 GtkMovementStep step,
231 gboolean extend_selection);
232 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
234 gboolean extend_selection);
235 static void gtk_text_view_set_anchor (GtkTextView *text_view);
236 static void gtk_text_view_scroll_pages (GtkTextView *text_view,
238 gboolean extend_selection);
239 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
241 gboolean extend_selection);
242 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
244 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
247 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
248 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
249 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
250 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
251 static void gtk_text_view_move_focus (GtkTextView *text_view,
252 GtkDirectionType direction_type);
253 static void gtk_text_view_unselect (GtkTextView *text_view);
255 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
256 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
258 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
259 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
260 GtkTextAttributes *values,
262 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
263 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
264 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
265 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
266 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
267 const GtkTextIter *iter,
268 GdkEventButton *event);
269 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
270 GdkEventButton *event);
271 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
272 const GtkTextIter *iter,
273 GdkEventMotion *event);
274 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
275 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
276 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
278 static void gtk_text_view_value_changed (GtkAdjustment *adj,
280 static void gtk_text_view_commit_handler (GtkIMContext *context,
282 GtkTextView *text_view);
283 static void gtk_text_view_commit_text (GtkTextView *text_view,
285 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
286 GtkTextView *text_view);
287 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
288 GtkTextView *text_view);
289 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
292 GtkTextView *text_view);
294 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
295 const GtkTextIter *location,
298 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
301 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
305 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
306 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
308 static void gtk_text_view_do_popup (GtkTextView *text_view,
309 GdkEventButton *event);
311 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
313 gdouble within_margin,
318 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
319 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
320 static void gtk_text_view_invalidate (GtkTextView *text_view);
321 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
323 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
325 /* Container methods */
326 static void gtk_text_view_add (GtkContainer *container,
328 static void gtk_text_view_remove (GtkContainer *container,
330 static void gtk_text_view_forall (GtkContainer *container,
331 gboolean include_internals,
332 GtkCallback callback,
333 gpointer callback_data);
335 /* FIXME probably need the focus methods. */
337 typedef struct _GtkTextViewChild GtkTextViewChild;
339 struct _GtkTextViewChild
343 GtkTextChildAnchor *anchor;
345 gint from_top_of_line;
346 gint from_left_of_buffer;
348 /* These are ignored if anchor != NULL */
349 GtkTextWindowType type;
354 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
355 GtkTextChildAnchor *anchor,
356 GtkTextLayout *layout);
357 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
358 GtkTextWindowType type,
361 static void text_view_child_free (GtkTextViewChild *child);
362 static void text_view_child_set_parent_window (GtkTextView *text_view,
363 GtkTextViewChild *child);
365 struct _GtkTextWindow
367 GtkTextWindowType type;
370 GdkWindow *bin_window;
371 GtkRequisition requisition;
372 GdkRectangle allocation;
375 static GtkTextWindow *text_window_new (GtkTextWindowType type,
378 gint height_request);
379 static void text_window_free (GtkTextWindow *win);
380 static void text_window_realize (GtkTextWindow *win,
382 static void text_window_unrealize (GtkTextWindow *win);
383 static void text_window_size_allocate (GtkTextWindow *win,
385 static void text_window_scroll (GtkTextWindow *win,
388 static void text_window_invalidate_rect (GtkTextWindow *win,
391 static gint text_window_get_width (GtkTextWindow *win);
392 static gint text_window_get_height (GtkTextWindow *win);
393 static void text_window_get_allocation (GtkTextWindow *win,
401 TARGET_COMPOUND_TEXT,
403 TARGET_TEXT_BUFFER_CONTENTS
406 static GtkTargetEntry target_table[] = {
407 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
408 TARGET_TEXT_BUFFER_CONTENTS },
409 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
410 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
411 { "TEXT", 0, TARGET_TEXT },
412 { "STRING", 0, TARGET_STRING }
415 static GtkContainerClass *parent_class = NULL;
416 static guint signals[LAST_SIGNAL] = { 0 };
419 gtk_text_view_get_type (void)
421 static GtkType our_type = 0;
425 static const GtkTypeInfo our_info =
428 sizeof (GtkTextView),
429 sizeof (GtkTextViewClass),
430 (GtkClassInitFunc) gtk_text_view_class_init,
431 (GtkObjectInitFunc) gtk_text_view_init,
432 /* reserved_1 */ NULL,
433 /* reserved_2 */ NULL,
434 (GtkClassInitFunc) NULL
437 our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
444 add_move_binding (GtkBindingSet *binding_set,
447 GtkMovementStep step,
450 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
452 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
456 GTK_TYPE_BOOL, FALSE);
458 /* Selection-extending version */
459 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
463 GTK_TYPE_BOOL, TRUE);
467 gtk_text_view_class_init (GtkTextViewClass *klass)
469 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
470 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
471 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
472 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
473 GtkBindingSet *binding_set;
475 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
477 /* Default handlers and virtual methods
479 gobject_class->set_property = gtk_text_view_set_property;
480 gobject_class->get_property = gtk_text_view_get_property;
482 object_class->destroy = gtk_text_view_destroy;
483 gobject_class->finalize = gtk_text_view_finalize;
485 widget_class->realize = gtk_text_view_realize;
486 widget_class->unrealize = gtk_text_view_unrealize;
487 widget_class->style_set = gtk_text_view_style_set;
488 widget_class->direction_changed = gtk_text_view_direction_changed;
489 widget_class->size_request = gtk_text_view_size_request;
490 widget_class->size_allocate = gtk_text_view_size_allocate;
491 widget_class->event = gtk_text_view_event;
492 widget_class->key_press_event = gtk_text_view_key_press_event;
493 widget_class->key_release_event = gtk_text_view_key_release_event;
494 widget_class->button_press_event = gtk_text_view_button_press_event;
495 widget_class->button_release_event = gtk_text_view_button_release_event;
496 widget_class->focus_in_event = gtk_text_view_focus_in_event;
497 widget_class->focus_out_event = gtk_text_view_focus_out_event;
498 widget_class->motion_notify_event = gtk_text_view_motion_event;
499 widget_class->expose_event = gtk_text_view_expose_event;
500 widget_class->grab_focus = gtk_text_view_grab_focus;
501 widget_class->focus = gtk_text_view_focus;
503 widget_class->drag_begin = gtk_text_view_drag_begin;
504 widget_class->drag_end = gtk_text_view_drag_end;
505 widget_class->drag_data_get = gtk_text_view_drag_data_get;
506 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
508 widget_class->drag_leave = gtk_text_view_drag_leave;
509 widget_class->drag_motion = gtk_text_view_drag_motion;
510 widget_class->drag_drop = gtk_text_view_drag_drop;
511 widget_class->drag_data_received = gtk_text_view_drag_data_received;
513 widget_class->popup_menu = gtk_text_view_popup_menu;
515 container_class->add = gtk_text_view_add;
516 container_class->remove = gtk_text_view_remove;
517 container_class->forall = gtk_text_view_forall;
519 klass->move_cursor = gtk_text_view_move_cursor;
520 klass->page_horizontally = gtk_text_view_page_horizontally;
521 klass->set_anchor = gtk_text_view_set_anchor;
522 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
523 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
524 klass->cut_clipboard = gtk_text_view_cut_clipboard;
525 klass->copy_clipboard = gtk_text_view_copy_clipboard;
526 klass->paste_clipboard = gtk_text_view_paste_clipboard;
527 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
528 klass->move_focus = gtk_text_view_move_focus;
529 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
535 g_object_class_install_property (gobject_class,
536 PROP_PIXELS_ABOVE_LINES,
537 g_param_spec_int ("pixels_above_lines",
538 _("Pixels Above Lines"),
539 _("Pixels of blank space above paragraphs"),
545 g_object_class_install_property (gobject_class,
546 PROP_PIXELS_BELOW_LINES,
547 g_param_spec_int ("pixels_below_lines",
548 _("Pixels Below Lines"),
549 _("Pixels of blank space below paragraphs"),
555 g_object_class_install_property (gobject_class,
556 PROP_PIXELS_INSIDE_WRAP,
557 g_param_spec_int ("pixels_inside_wrap",
558 _("Pixels Inside Wrap"),
559 _("Pixels of blank space between wrapped lines in a paragraph"),
565 g_object_class_install_property (gobject_class,
567 g_param_spec_boolean ("editable",
569 _("Whether the text can be modified by the user"),
573 g_object_class_install_property (gobject_class,
575 g_param_spec_enum ("wrap_mode",
577 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
582 g_object_class_install_property (gobject_class,
584 g_param_spec_enum ("justification",
586 _("Left, right, or center justification"),
587 GTK_TYPE_JUSTIFICATION,
591 g_object_class_install_property (gobject_class,
593 g_param_spec_int ("left_margin",
595 _("Width of the left margin in pixels"),
601 g_object_class_install_property (gobject_class,
603 g_param_spec_int ("right_margin",
605 _("Width of the right margin in pixels"),
611 g_object_class_install_property (gobject_class,
613 g_param_spec_int ("indent",
615 _("Amount to indent the paragraph, in pixels"),
621 g_object_class_install_property (gobject_class,
623 g_param_spec_boxed ("tabs",
625 _("Custom tabs for this text"),
626 PANGO_TYPE_TAB_ARRAY,
629 g_object_class_install_property (gobject_class,
631 g_param_spec_boolean ("cursor_visible",
633 _("If the insertion cursor is shown"),
642 signals[MOVE_CURSOR] =
643 gtk_signal_new ("move_cursor",
644 GTK_RUN_LAST | GTK_RUN_ACTION,
645 GTK_CLASS_TYPE (object_class),
646 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
647 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
648 GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
650 signals[PAGE_HORIZONTALLY] =
651 gtk_signal_new ("page_horizontally",
652 GTK_RUN_LAST | GTK_RUN_ACTION,
653 GTK_CLASS_TYPE (object_class),
654 GTK_SIGNAL_OFFSET (GtkTextViewClass, page_horizontally),
655 _gtk_marshal_VOID__INT_BOOLEAN,
656 GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_BOOL);
658 signals[SET_ANCHOR] =
659 gtk_signal_new ("set_anchor",
660 GTK_RUN_LAST | GTK_RUN_ACTION,
661 GTK_CLASS_TYPE (object_class),
662 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
663 _gtk_marshal_VOID__VOID,
666 signals[INSERT_AT_CURSOR] =
667 gtk_signal_new ("insert_at_cursor",
668 GTK_RUN_LAST | GTK_RUN_ACTION,
669 GTK_CLASS_TYPE (object_class),
670 GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
671 _gtk_marshal_VOID__STRING,
672 GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
674 signals[DELETE_FROM_CURSOR] =
675 gtk_signal_new ("delete_from_cursor",
676 GTK_RUN_LAST | GTK_RUN_ACTION,
677 GTK_CLASS_TYPE (object_class),
678 GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
679 _gtk_marshal_VOID__ENUM_INT,
680 GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
682 signals[CUT_CLIPBOARD] =
683 gtk_signal_new ("cut_clipboard",
684 GTK_RUN_LAST | GTK_RUN_ACTION,
685 GTK_CLASS_TYPE (object_class),
686 GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
687 _gtk_marshal_VOID__VOID,
690 signals[COPY_CLIPBOARD] =
691 gtk_signal_new ("copy_clipboard",
692 GTK_RUN_LAST | GTK_RUN_ACTION,
693 GTK_CLASS_TYPE (object_class),
694 GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
695 _gtk_marshal_VOID__VOID,
698 signals[PASTE_CLIPBOARD] =
699 gtk_signal_new ("paste_clipboard",
700 GTK_RUN_LAST | GTK_RUN_ACTION,
701 GTK_CLASS_TYPE (object_class),
702 GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
703 _gtk_marshal_VOID__VOID,
706 signals[TOGGLE_OVERWRITE] =
707 gtk_signal_new ("toggle_overwrite",
708 GTK_RUN_LAST | GTK_RUN_ACTION,
709 GTK_CLASS_TYPE (object_class),
710 GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
711 _gtk_marshal_VOID__VOID,
714 signals[MOVE_FOCUS] =
715 gtk_signal_new ("move_focus",
716 GTK_RUN_LAST | GTK_RUN_ACTION,
717 GTK_CLASS_TYPE (object_class),
718 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_focus),
719 _gtk_marshal_VOID__ENUM,
720 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
722 signals[SET_SCROLL_ADJUSTMENTS] =
723 gtk_signal_new ("set_scroll_adjustments",
725 GTK_CLASS_TYPE (object_class),
726 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
727 _gtk_marshal_VOID__OBJECT_OBJECT,
728 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
729 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
731 signals[POPULATE_POPUP] =
732 gtk_signal_new ("populate_popup",
734 GTK_CLASS_TYPE (object_class),
735 GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
736 _gtk_marshal_VOID__OBJECT,
737 GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
743 binding_set = gtk_binding_set_by_class (klass);
745 /* Moving the insertion point */
746 add_move_binding (binding_set, GDK_Right, 0,
747 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
749 add_move_binding (binding_set, GDK_KP_Right, 0,
750 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
752 add_move_binding (binding_set, GDK_Left, 0,
753 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
755 add_move_binding (binding_set, GDK_KP_Left, 0,
756 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
758 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
759 GTK_MOVEMENT_WORDS, 1);
761 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
762 GTK_MOVEMENT_WORDS, 1);
764 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
765 GTK_MOVEMENT_WORDS, -1);
767 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
768 GTK_MOVEMENT_WORDS, 1);
770 add_move_binding (binding_set, GDK_Up, 0,
771 GTK_MOVEMENT_DISPLAY_LINES, -1);
773 add_move_binding (binding_set, GDK_KP_Up, 0,
774 GTK_MOVEMENT_DISPLAY_LINES, -1);
776 add_move_binding (binding_set, GDK_Down, 0,
777 GTK_MOVEMENT_DISPLAY_LINES, 1);
779 add_move_binding (binding_set, GDK_KP_Down, 0,
780 GTK_MOVEMENT_DISPLAY_LINES, 1);
782 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
783 GTK_MOVEMENT_PARAGRAPHS, -1);
785 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
786 GTK_MOVEMENT_PARAGRAPHS, -1);
788 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
789 GTK_MOVEMENT_PARAGRAPHS, 1);
791 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
792 GTK_MOVEMENT_PARAGRAPHS, 1);
794 add_move_binding (binding_set, GDK_Home, 0,
795 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
797 add_move_binding (binding_set, GDK_KP_Home, 0,
798 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
800 add_move_binding (binding_set, GDK_End, 0,
801 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
803 add_move_binding (binding_set, GDK_KP_End, 0,
804 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
806 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
807 GTK_MOVEMENT_BUFFER_ENDS, -1);
809 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
810 GTK_MOVEMENT_BUFFER_ENDS, -1);
812 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
813 GTK_MOVEMENT_BUFFER_ENDS, 1);
815 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
816 GTK_MOVEMENT_BUFFER_ENDS, 1);
818 add_move_binding (binding_set, GDK_Page_Up, 0,
819 GTK_MOVEMENT_PAGES, -1);
821 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
822 GTK_MOVEMENT_PAGES, -1);
824 add_move_binding (binding_set, GDK_Page_Down, 0,
825 GTK_MOVEMENT_PAGES, 1);
827 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
828 GTK_MOVEMENT_PAGES, 1);
832 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
834 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
836 GTK_TYPE_BOOL, FALSE);
837 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
839 GTK_TYPE_MOVEMENT_STEP, GTK_MOVEMENT_BUFFER_ENDS,
841 GTK_TYPE_BOOL, TRUE);
844 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
845 "page_horizontally", 2,
847 GTK_TYPE_BOOL, FALSE);
849 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
850 "page_horizontally", 2,
852 GTK_TYPE_BOOL, TRUE);
854 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
855 "page_horizontally", 2,
857 GTK_TYPE_BOOL, FALSE);
859 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
860 "page_horizontally", 2,
862 GTK_TYPE_BOOL, TRUE);
864 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
865 "page_horizontally", 2,
867 GTK_TYPE_BOOL, FALSE);
869 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
870 "page_horizontally", 2,
872 GTK_TYPE_BOOL, TRUE);
874 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
875 "page_horizontally", 2,
877 GTK_TYPE_BOOL, FALSE);
879 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
880 "page_horizontally", 2,
882 GTK_TYPE_BOOL, TRUE);
885 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
886 "delete_from_cursor", 2,
887 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
890 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
891 "delete_from_cursor", 2,
892 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
895 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
896 "delete_from_cursor", 2,
897 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
900 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
901 "delete_from_cursor", 2,
902 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
905 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
906 "delete_from_cursor", 2,
907 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
910 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
911 "delete_from_cursor", 2,
912 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
917 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
920 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
921 "copy_clipboard", 0);
923 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
924 "paste_clipboard", 0);
927 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
928 "toggle_overwrite", 0);
929 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
930 "toggle_overwrite", 0);
932 /* Control-tab focus motion */
933 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
935 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
936 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
938 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
940 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
942 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
943 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
945 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
949 gtk_text_view_init (GtkTextView *text_view)
953 widget = GTK_WIDGET (text_view);
955 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
957 /* Set up default style */
958 text_view->wrap_mode = GTK_WRAP_NONE;
959 text_view->pixels_above_lines = 0;
960 text_view->pixels_below_lines = 0;
961 text_view->pixels_inside_wrap = 0;
962 text_view->justify = GTK_JUSTIFY_LEFT;
963 text_view->left_margin = 0;
964 text_view->right_margin = 0;
965 text_view->indent = 0;
966 text_view->tabs = NULL;
967 text_view->editable = TRUE;
969 gtk_drag_dest_set (widget,
971 target_table, G_N_ELEMENTS (target_table),
972 GDK_ACTION_COPY | GDK_ACTION_MOVE);
974 text_view->virtual_cursor_x = -1;
975 text_view->virtual_cursor_y = -1;
977 /* This object is completely private. No external entity can gain a reference
978 * to it; so we create it here and destroy it in finalize ().
980 text_view->im_context = gtk_im_multicontext_new ();
982 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
983 G_CALLBACK (gtk_text_view_commit_handler), text_view);
984 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
985 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
986 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
987 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
988 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
989 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
991 text_view->cursor_visible = TRUE;
993 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
996 text_view->drag_start_x = -1;
997 text_view->drag_start_y = -1;
999 text_view->pending_place_cursor_button = 0;
1001 /* We handle all our own redrawing */
1002 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1006 * gtk_text_view_new:
1008 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1009 * before using the text view, an empty default buffer will be created
1010 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1011 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1013 * Return value: a new #GtkTextView
1016 gtk_text_view_new (void)
1018 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1022 * gtk_text_view_new_with_buffer:
1023 * @buffer: a #GtkTextBuffer
1025 * Creates a new #GtkTextView widget displaying the buffer
1026 * @buffer. One buffer can be shared among many widgets.
1027 * @buffer may be NULL to create a default buffer, in which case
1028 * this function is equivalent to gtk_text_view_new(). The
1029 * text view adds its own reference count to the buffer; it does not
1030 * take over an existing reference.
1032 * Return value: a new #GtkTextView.
1035 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1037 GtkTextView *text_view;
1039 text_view = (GtkTextView*)gtk_text_view_new ();
1041 gtk_text_view_set_buffer (text_view, buffer);
1043 return GTK_WIDGET (text_view);
1047 * gtk_text_view_set_buffer:
1048 * @text_view: a #GtkTextView
1049 * @buffer: a #GtkTextBuffer
1051 * Sets @buffer as the buffer being displayed by @text_view. The previous
1052 * buffer displayed by the text view is unreferenced, and a reference is
1053 * added to @buffer. If you owned a reference to @buffer before passing it
1054 * to this function, you must remove that reference yourself; #GtkTextView
1055 * will not "adopt" it.
1059 gtk_text_view_set_buffer (GtkTextView *text_view,
1060 GtkTextBuffer *buffer)
1062 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1063 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1065 if (text_view->buffer == buffer)
1068 if (text_view->buffer != NULL)
1070 /* Destroy all anchored children */
1074 copy = g_slist_copy (text_view->children);
1076 while (tmp_list != NULL)
1078 GtkTextViewChild *vc = tmp_list->data;
1082 gtk_widget_destroy (vc->widget);
1083 /* vc may now be invalid! */
1086 tmp_list = g_slist_next (tmp_list);
1089 g_slist_free (copy);
1091 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1092 (gpointer) gtk_text_view_mark_set_handler, text_view);
1093 g_object_unref (G_OBJECT (text_view->buffer));
1094 text_view->dnd_mark = NULL;
1096 if (GTK_WIDGET_REALIZED (text_view))
1098 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1099 GDK_SELECTION_PRIMARY);
1100 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1104 text_view->buffer = buffer;
1110 g_object_ref (G_OBJECT (buffer));
1112 if (text_view->layout)
1113 gtk_text_layout_set_buffer (text_view->layout, buffer);
1115 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1117 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1121 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1125 text_view->first_para_pixels = 0;
1127 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1128 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1130 if (GTK_WIDGET_REALIZED (text_view))
1132 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1133 GDK_SELECTION_PRIMARY);
1134 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1138 if (GTK_WIDGET_VISIBLE (text_view))
1139 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1141 DV(g_print ("Invalidating due to set_buffer\n"));
1142 gtk_text_view_invalidate (text_view);
1145 static GtkTextBuffer*
1146 get_buffer (GtkTextView *text_view)
1148 if (text_view->buffer == NULL)
1151 b = gtk_text_buffer_new (NULL);
1152 gtk_text_view_set_buffer (text_view, b);
1153 g_object_unref (G_OBJECT (b));
1156 return text_view->buffer;
1160 * gtk_text_view_get_buffer:
1161 * @text_view: a #GtkTextView
1163 * Returns the #GtkTextBuffer being displayed by this text view.
1164 * The reference count on the buffer is not incremented; the caller
1165 * of this function won't own a new reference.
1167 * Return value: a #GtkTextBuffer
1170 gtk_text_view_get_buffer (GtkTextView *text_view)
1172 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1174 return get_buffer (text_view);
1178 * gtk_text_view_get_iter_at_location:
1179 * @text_view: a #GtkTextView
1180 * @iter: a #GtkTextIter
1181 * @x: x position, in buffer coordinates
1182 * @y: y position, in buffer coordinates
1184 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1185 * coordinates are coordinates for the entire buffer, not just the
1186 * currently-displayed portion. If you have coordinates from an
1187 * event, you have to convert those to buffer coordinates with
1188 * gtk_text_view_window_to_buffer_coords().
1192 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1197 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1198 g_return_if_fail (iter != NULL);
1200 gtk_text_view_ensure_layout (text_view);
1202 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1209 * gtk_text_view_get_iter_location:
1210 * @text_view: a #GtkTextView
1211 * @iter: a #GtkTextIter
1212 * @location: bounds of the character at @iter
1214 * Gets a rectangle which roughly contains the character at @iter.
1215 * The rectangle position is in buffer coordinates; use
1216 * gtk_text_view_buffer_to_window_coords() to convert these
1217 * coordinates to coordinates for one of the windows in the text view.
1221 gtk_text_view_get_iter_location (GtkTextView *text_view,
1222 const GtkTextIter *iter,
1223 GdkRectangle *location)
1225 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1226 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1228 gtk_text_view_ensure_layout (text_view);
1230 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1234 * gtk_text_view_get_line_yrange:
1235 * @text_view: a #GtkTextView
1236 * @iter: a #GtkTextIter
1237 * @y: return location for a y coordinate
1238 * @height: return location for a height
1240 * Gets the y coordinate of the top of the line containing @iter,
1241 * and the height of the line. The coordinate is a buffer coordinate;
1242 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1246 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1247 const GtkTextIter *iter,
1251 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1252 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1254 gtk_text_view_ensure_layout (text_view);
1256 gtk_text_layout_get_line_yrange (text_view->layout,
1263 * gtk_text_view_get_line_at_y:
1264 * @text_view: a #GtkTextView
1265 * @target_iter: a #GtkTextIter
1266 * @y: a y coordinate
1267 * @line_top: return location for top coordinate of the line
1269 * Gets the #GtkTextIter at the start of the line containing
1270 * the coordinate @y. @y is in buffer coordinates, convert from
1271 * window coordinates with gtk_text_view_window_to_buffer_coords().
1272 * If non-%NULL, @line_top will be filled with the coordinate of the top
1276 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1277 GtkTextIter *target_iter,
1281 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1283 gtk_text_view_ensure_layout (text_view);
1285 gtk_text_layout_get_line_at_y (text_view->layout,
1292 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1294 DV (g_print (" Setting adj to raw value %g\n", val));
1296 /* We don't really want to clamp to upper; we want to clamp to
1297 upper - page_size which is the highest value the scrollbar
1298 will let us reach. */
1299 if (val > (adj->upper - adj->page_size))
1300 val = adj->upper - adj->page_size;
1302 if (val < adj->lower)
1305 if (val != adj->value)
1307 DV (g_print (" Setting adj to clamped value %g\n", val));
1308 gtk_adjustment_set_value (adj, val);
1316 * gtk_text_view_scroll_to_iter:
1317 * @text_view: a #GtkTextView
1318 * @iter: a #GtkTextIter
1319 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1320 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1321 * @xalign: horizontal alignment of mark within visible area.
1322 * @yalign: vertical alignment of mark within visible area
1324 * Scrolls @text_view so that @iter is on the screen in the position
1325 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1326 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1327 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1328 * possibly not scrolling at all. The effective screen for purposes
1329 * of this function is reduced by a margin of size @within_margin.
1330 * NOTE: This function uses the currently-computed height of the
1331 * lines in the text buffer. Note that line heights are computed
1332 * in an idle handler; so this function may not have the desired effect
1333 * if it's called before the height computations. To avoid oddness,
1334 * consider using gtk_text_view_scroll_to_mark() which saves a point
1335 * to be scrolled to after line validation.
1337 * Return value: %TRUE if scrolling occurred
1340 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1342 gdouble within_margin,
1348 GdkRectangle screen;
1353 gboolean retval = FALSE;
1355 gint screen_xoffset, screen_yoffset;
1356 gint current_x_scroll, current_y_scroll;
1358 /* FIXME why don't we do the validate-at-scroll-destination thing
1359 * from flush_scroll in this function? I think it wasn't done before
1360 * because changed_handler was screwed up, but I could be wrong.
1363 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1364 g_return_val_if_fail (iter != NULL, FALSE);
1365 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1366 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1367 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1369 widget = GTK_WIDGET (text_view);
1371 DV(g_print(G_STRLOC"\n"));
1373 gtk_text_layout_get_iter_location (text_view->layout,
1377 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1379 current_x_scroll = text_view->xoffset;
1380 current_y_scroll = text_view->yoffset;
1382 screen.x = current_x_scroll;
1383 screen.y = current_y_scroll;
1384 screen.width = SCREEN_WIDTH (widget);
1385 screen.height = SCREEN_HEIGHT (widget);
1387 screen_xoffset = screen.width * within_margin;
1388 screen_yoffset = screen.height * within_margin;
1390 screen.x += screen_xoffset;
1391 screen.y += screen_yoffset;
1392 screen.width -= screen_xoffset * 2;
1393 screen.height -= screen_yoffset * 2;
1395 /* paranoia check */
1396 if (screen.width < 1)
1398 if (screen.height < 1)
1401 screen_right = screen.x + screen.width;
1402 screen_bottom = screen.y + screen.height;
1404 /* The alignment affects the point in the target character that we
1405 * choose to align. If we're doing right/bottom alignment, we align
1406 * the right/bottom edge of the character the mark is at; if we're
1407 * doing left/top we align the left/top edge of the character; if
1408 * we're doing center alignment we align the center of the
1412 /* Vertical scroll */
1415 scroll_dest = current_y_scroll;
1419 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1421 /* if scroll_dest < screen.y, we move a negative increment (up),
1422 * else a positive increment (down)
1424 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1428 /* move minimum to get onscreen */
1429 if (rect.y < screen.y)
1431 scroll_dest = rect.y;
1432 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1434 else if ((rect.y + rect.height) > screen_bottom)
1436 scroll_dest = rect.y + rect.height;
1437 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1441 if (scroll_inc != 0)
1443 retval = set_adjustment_clamped (get_vadjustment (text_view),
1444 current_y_scroll + scroll_inc);
1446 DV (g_print (" vert increment %d\n", scroll_inc));
1449 /* Horizontal scroll */
1452 scroll_dest = current_x_scroll;
1456 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1458 /* if scroll_dest < screen.y, we move a negative increment (left),
1459 * else a positive increment (right)
1461 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1465 /* move minimum to get onscreen */
1466 if (rect.x < screen.x)
1468 scroll_dest = rect.x;
1469 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1471 else if ((rect.x + rect.width) > screen_right)
1473 scroll_dest = rect.x + rect.width;
1474 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1478 if (scroll_inc != 0)
1480 retval = set_adjustment_clamped (get_hadjustment (text_view),
1481 current_x_scroll + scroll_inc);
1483 DV (g_print (" horiz increment %d\n", scroll_inc));
1487 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1489 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1495 free_pending_scroll (GtkTextPendingScroll *scroll)
1497 if (!gtk_text_mark_get_deleted (scroll->mark))
1498 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1500 g_object_unref (G_OBJECT (scroll->mark));
1505 gtk_text_view_queue_scroll (GtkTextView *text_view,
1507 gdouble within_margin,
1513 GtkTextPendingScroll *scroll;
1515 DV(g_print(G_STRLOC"\n"));
1517 scroll = g_new (GtkTextPendingScroll, 1);
1519 scroll->within_margin = within_margin;
1520 scroll->use_align = use_align;
1521 scroll->xalign = xalign;
1522 scroll->yalign = yalign;
1524 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1526 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1529 gtk_text_mark_get_left_gravity (mark));
1531 g_object_ref (G_OBJECT (scroll->mark));
1533 if (text_view->pending_scroll)
1534 free_pending_scroll (text_view->pending_scroll);
1536 text_view->pending_scroll = scroll;
1540 gtk_text_view_flush_scroll (GtkTextView *text_view)
1543 GtkTextPendingScroll *scroll;
1547 widget = GTK_WIDGET (text_view);
1549 DV(g_print(G_STRLOC"\n"));
1551 if (text_view->pending_scroll == NULL)
1553 DV (g_print ("in flush scroll, no pending scroll\n"));
1557 scroll = text_view->pending_scroll;
1559 /* avoid recursion */
1560 text_view->pending_scroll = NULL;
1562 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1564 /* Validate area around the scroll destination, so the adjustment
1565 * can meaningfully point into that area. We must validate
1566 * enough area to be sure that after we scroll, everything onscreen
1567 * is valid; otherwise, validation will maintain the first para
1568 * in one place, but may push the target iter off the bottom of
1571 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1572 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1573 - (widget->allocation.height * 2),
1574 widget->allocation.height * 2);
1576 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1578 /* Ensure we have updated width/height */
1579 gtk_text_view_update_adjustments (text_view);
1581 retval = gtk_text_view_scroll_to_iter (text_view,
1583 scroll->within_margin,
1588 free_pending_scroll (scroll);
1594 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1596 if (upper != adj->upper)
1598 gdouble min = MAX (0.0, upper - adj->page_size);
1599 gboolean value_changed = FALSE;
1603 if (adj->value > min)
1606 value_changed = TRUE;
1609 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1610 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1614 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1615 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1621 gtk_text_view_update_adjustments (GtkTextView *text_view)
1623 gint width = 0, height = 0;
1625 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1627 if (text_view->layout)
1628 gtk_text_layout_get_size (text_view->layout, &width, &height);
1630 if (text_view->width != width || text_view->height != height)
1632 text_view->width = width;
1633 text_view->height = height;
1635 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1636 MAX (SCREEN_WIDTH (text_view), width));
1637 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1638 MAX (SCREEN_HEIGHT (text_view), height));
1640 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1642 /* Set up the step sizes; we'll say that a page is
1643 our allocation minus one step, and a step is
1644 1/10 of our allocation. */
1645 text_view->hadjustment->step_increment =
1646 SCREEN_WIDTH (text_view) / 10.0;
1647 text_view->hadjustment->page_increment =
1648 SCREEN_WIDTH (text_view) * 0.9;
1650 text_view->vadjustment->step_increment =
1651 SCREEN_HEIGHT (text_view) / 10.0;
1652 text_view->vadjustment->page_increment =
1653 SCREEN_HEIGHT (text_view) * 0.9;
1655 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1656 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1661 gtk_text_view_update_layout_width (GtkTextView *text_view)
1663 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1665 gtk_text_view_ensure_layout (text_view);
1667 gtk_text_layout_set_screen_width (text_view->layout,
1668 SCREEN_WIDTH (text_view));
1672 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1675 gint cursor_x_pos, cursor_y_pos;
1677 if (text_view->layout == NULL)
1680 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1682 area.x = cursor_x_pos;
1683 area.y = cursor_y_pos;
1684 area.width = area.height = 0;
1686 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1690 * gtk_text_view_scroll_to_mark:
1691 * @text_view: a #GtkTextView
1692 * @mark: a #GtkTextMark
1693 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1694 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1695 * @xalign: horizontal alignment of mark within visible area.
1696 * @yalign: vertical alignment of mark within visible area
1698 * Scrolls @text_view so that @mark is on the screen in the position
1699 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1700 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1701 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1702 * possibly not scrolling at all. The effective screen for purposes
1703 * of this function is reduced by a margin of size @within_margin.
1707 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1709 gdouble within_margin,
1714 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1715 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1716 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1717 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1718 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1720 gtk_text_view_queue_scroll (text_view, mark,
1726 /* If no validation is pending, we need to go ahead and force an
1729 if (text_view->layout &&
1730 gtk_text_layout_is_valid (text_view->layout))
1731 gtk_text_view_flush_scroll (text_view);
1735 * gtk_text_view_scroll_mark_onscreen:
1736 * @text_view: a #GtkTextView
1737 * @mark: a mark in the buffer for @text_view
1739 * Scrolls @text_view the minimum distance such that @mark is contained
1740 * within the visible area of the widget.
1744 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1747 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1748 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1750 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1754 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1756 GdkRectangle visible_rect;
1757 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1759 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1761 visible_rect.y + visible_rect.height);
1765 * gtk_text_view_move_mark_onscreen:
1766 * @text_view: a #GtkTextView
1767 * @mark: a #GtkTextMark
1769 * Moves a mark within the buffer so that it's
1770 * located within the currently-visible text area.
1772 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1775 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1780 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1781 g_return_val_if_fail (mark != NULL, FALSE);
1783 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1785 if (clamp_iter_onscreen (text_view, &iter))
1787 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1795 * gtk_text_view_get_visible_rect:
1796 * @text_view: a #GtkTextView
1797 * @visible_rect: rectangle to fill
1799 * Fills @visible_rect with the currently-visible
1800 * region of the buffer, in buffer coordinates. Convert to window coordinates
1801 * with gtk_text_view_buffer_to_window_coords().
1804 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1805 GdkRectangle *visible_rect)
1809 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1811 widget = GTK_WIDGET (text_view);
1815 visible_rect->x = text_view->xoffset;
1816 visible_rect->y = text_view->yoffset;
1817 visible_rect->width = SCREEN_WIDTH (widget);
1818 visible_rect->height = SCREEN_HEIGHT (widget);
1820 DV(g_print(" visible rect: %d,%d %d x %d\n",
1823 visible_rect->width,
1824 visible_rect->height));
1829 * gtk_text_view_set_wrap_mode:
1830 * @text_view: a #GtkTextView
1831 * @wrap_mode: a #GtkWrapMode
1833 * Sets the line wrapping for the view.
1836 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1837 GtkWrapMode wrap_mode)
1839 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1841 if (text_view->wrap_mode != wrap_mode)
1843 text_view->wrap_mode = wrap_mode;
1845 if (text_view->layout)
1847 text_view->layout->default_style->wrap_mode = wrap_mode;
1848 gtk_text_layout_default_style_changed (text_view->layout);
1852 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1856 * gtk_text_view_get_wrap_mode:
1857 * @text_view: a #GtkTextView
1859 * Gets the line wrapping for the view.
1861 * Return value: the line wrap setting
1864 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1866 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1868 return text_view->wrap_mode;
1872 * gtk_text_view_set_editable:
1873 * @text_view: a #GtkTextView
1874 * @setting: whether it's editable
1876 * Sets the default editability of the #GtkTextView. You can override
1877 * this default setting with tags in the buffer, using the "editable"
1878 * attribute of tags.
1881 gtk_text_view_set_editable (GtkTextView *text_view,
1884 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1885 setting = setting != FALSE;
1887 if (text_view->editable != setting)
1889 text_view->editable = setting;
1891 if (text_view->layout)
1893 text_view->layout->default_style->editable = text_view->editable;
1894 gtk_text_layout_default_style_changed (text_view->layout);
1897 g_object_notify (G_OBJECT (text_view), "editable");
1902 * gtk_text_view_get_editable:
1903 * @text_view: a #GtkTextView
1905 * Returns the default editability of the #GtkTextView. Tags in the
1906 * buffer may override this setting for some ranges of text.
1908 * Return value: whether text is editable by default
1911 gtk_text_view_get_editable (GtkTextView *text_view)
1913 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1915 return text_view->editable;
1919 * gtk_text_view_set_pixels_above_lines:
1920 * @text_view: a #GtkTextView
1921 * @pixels_above_lines: pixels above paragraphs
1923 * Sets the default number of blank pixels above paragraphs in @text_view.
1924 * Tags in the buffer for @text_view may override the defaults.
1928 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1929 gint pixels_above_lines)
1931 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1933 if (text_view->pixels_above_lines != pixels_above_lines)
1935 text_view->pixels_above_lines = pixels_above_lines;
1937 if (text_view->layout)
1939 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1940 gtk_text_layout_default_style_changed (text_view->layout);
1943 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1948 * gtk_text_view_get_pixels_above_lines:
1949 * @text_view: a #GtkTextView
1951 * Gets the default number of pixels to put above paragraphs.
1953 * Return value: default number of pixels above paragraphs
1956 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1958 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1960 return text_view->pixels_above_lines;
1964 * gtk_text_view_set_pixels_below_lines:
1965 * @text_view: a #GtkTextView
1966 * @pixels_below_lines: pixels below paragraphs
1968 * Sets the default number of pixels of blank space
1969 * to put below paragraphs in @text_view. May be overridden
1970 * by tags applied to @text_view's buffer.
1974 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1975 gint pixels_below_lines)
1977 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1979 if (text_view->pixels_below_lines != pixels_below_lines)
1981 text_view->pixels_below_lines = pixels_below_lines;
1983 if (text_view->layout)
1985 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
1986 gtk_text_layout_default_style_changed (text_view->layout);
1989 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
1994 * gtk_text_view_get_pixels_below_lines:
1995 * @text_view: a #GtkTextView
1997 * Gets the value set by gtk_text_view_set_pixels_below_lines().
1999 * Return value: default number of blank pixels below paragraphs
2002 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2004 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2006 return text_view->pixels_below_lines;
2010 * gtk_text_view_set_pixels_inside_wrap:
2011 * @text_view: a #GtkTextView
2012 * @pixels_inside_wrap: default number of pixels between wrapped lines
2014 * Sets the default number of pixels of blank space to leave between
2015 * display/wrapped lines within a paragraph. May be overridden by
2016 * tags in @text_view's buffer.
2020 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2021 gint pixels_inside_wrap)
2023 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2025 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2027 text_view->pixels_inside_wrap = pixels_inside_wrap;
2029 if (text_view->layout)
2031 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2032 gtk_text_layout_default_style_changed (text_view->layout);
2035 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2040 * gtk_text_view_get_pixels_inside_wrap:
2041 * @text_view: a #GtkTextView
2043 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2045 * Return value: default number of pixels of blank space between wrapped lines
2048 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2050 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2052 return text_view->pixels_inside_wrap;
2056 * gtk_text_view_set_justification:
2057 * @text_view: a #GtkTextView
2058 * @justification: justification
2060 * Sets the default justification of text in @text_view.
2061 * Tags in the view's buffer may override the default.
2065 gtk_text_view_set_justification (GtkTextView *text_view,
2066 GtkJustification justification)
2068 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2070 if (text_view->justify != justification)
2072 text_view->justify = justification;
2074 if (text_view->layout)
2076 text_view->layout->default_style->justification = justification;
2077 gtk_text_layout_default_style_changed (text_view->layout);
2080 g_object_notify (G_OBJECT (text_view), "justification");
2085 * gtk_text_view_get_justification:
2086 * @text_view: a #GtkTextView
2088 * Gets the default justification of paragraphs in @text_view.
2089 * Tags in the buffer may override the default.
2091 * Return value: default justification
2094 gtk_text_view_get_justification (GtkTextView *text_view)
2096 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2098 return text_view->justify;
2102 * gtk_text_view_set_left_margin:
2103 * @text_view: a #GtkTextView
2104 * @left_margin: left margin in pixels
2106 * Sets the default left margin for text in @text_view.
2107 * Tags in the buffer may override the default.
2111 gtk_text_view_set_left_margin (GtkTextView *text_view,
2114 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2116 if (text_view->left_margin != left_margin)
2118 text_view->left_margin = left_margin;
2120 if (text_view->layout)
2122 text_view->layout->default_style->left_margin = left_margin;
2123 gtk_text_layout_default_style_changed (text_view->layout);
2126 g_object_notify (G_OBJECT (text_view), "left_margin");
2131 * gtk_text_view_get_left_margin:
2132 * @text_view: a #GtkTextView
2134 * Gets the default left margin size of paragraphs in the @text_view.
2135 * Tags in the buffer may override the default.
2137 * Return value: left margin in pixels
2140 gtk_text_view_get_left_margin (GtkTextView *text_view)
2142 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2144 return text_view->left_margin;
2148 * gtk_text_view_set_right_margin:
2149 * @text_view: a #GtkTextView
2150 * @right_margin: right margin in pixels
2152 * Sets the default right margin for text in the text view.
2153 * Tags in the buffer may override the default.
2157 gtk_text_view_set_right_margin (GtkTextView *text_view,
2160 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2162 if (text_view->right_margin != right_margin)
2164 text_view->right_margin = right_margin;
2166 if (text_view->layout)
2168 text_view->layout->default_style->right_margin = right_margin;
2169 gtk_text_layout_default_style_changed (text_view->layout);
2172 g_object_notify (G_OBJECT (text_view), "right_margin");
2177 * gtk_text_view_get_right_margin:
2178 * @text_view: a #GtkTextView
2180 * Gets the default right margin for text in @text_view. Tags
2181 * in the buffer may override the default.
2183 * Return value: right margin in pixels
2186 gtk_text_view_get_right_margin (GtkTextView *text_view)
2188 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2190 return text_view->right_margin;
2194 * gtk_text_view_set_indent:
2195 * @text_view: a #GtkTextView
2196 * @indent: indentation in pixels
2198 * Sets the default indentation for paragraphs in @text_view.
2199 * Tags in the buffer may override the default.
2203 gtk_text_view_set_indent (GtkTextView *text_view,
2206 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2208 if (text_view->indent != indent)
2210 text_view->indent = indent;
2212 if (text_view->layout)
2214 text_view->layout->default_style->indent = indent;
2215 gtk_text_layout_default_style_changed (text_view->layout);
2218 g_object_notify (G_OBJECT (text_view), "indent");
2223 * gtk_text_view_get_indent:
2224 * @text_view: a #GtkTextView
2226 * Gets the default indentation of paragraphs in @text_view.
2227 * Tags in the view's buffer may override the default.
2228 * The indentation may be negative.
2230 * Return value: number of pixels of indentation
2233 gtk_text_view_get_indent (GtkTextView *text_view)
2235 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2237 return text_view->indent;
2241 * gtk_text_view_set_tabs:
2242 * @text_view: a #GtkTextView
2243 * @tabs: tabs as a #PangoTabArray
2245 * Sets the default tab stops for paragraphs in @text_view.
2246 * Tags in the buffer may override the default.
2250 gtk_text_view_set_tabs (GtkTextView *text_view,
2251 PangoTabArray *tabs)
2253 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2255 if (text_view->tabs)
2256 pango_tab_array_free (text_view->tabs);
2258 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2260 if (text_view->layout)
2262 /* some unkosher futzing in internal struct details... */
2263 if (text_view->layout->default_style->tabs)
2264 pango_tab_array_free (text_view->layout->default_style->tabs);
2266 text_view->layout->default_style->tabs =
2267 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2269 gtk_text_layout_default_style_changed (text_view->layout);
2272 g_object_notify (G_OBJECT (text_view), "tabs");
2276 * gtk_text_view_get_tabs:
2277 * @text_view: a #GtkTextView
2279 * Gets the default tabs for @text_view. Tags in the buffer may
2280 * override the defaults. The returned array will be %NULL if
2281 * "standard" (8-space) tabs are used. Free the return value
2282 * with pango_tab_array_free().
2284 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2287 gtk_text_view_get_tabs (GtkTextView *text_view)
2289 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2291 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2295 * gtk_text_view_set_cursor_visible:
2296 * @text_view: a #GtkTextView
2297 * @setting: whether to show the insertion cursor
2299 * Toggles whether the insertion point is displayed. A buffer with no editable
2300 * text probably shouldn't have a visible cursor, so you may want to turn
2304 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2307 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2309 setting = (setting != FALSE);
2311 if (text_view->cursor_visible != setting)
2313 text_view->cursor_visible = setting;
2315 if (GTK_WIDGET_HAS_FOCUS (text_view))
2317 if (text_view->layout)
2319 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2320 gtk_text_view_check_cursor_blink (text_view);
2324 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2329 * gtk_text_view_get_cursor_visible:
2330 * @text_view: a #GtkTextView
2332 * Find out whether the cursor is being displayed.
2334 * Return value: whether the insertion mark is visible
2337 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2339 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2341 return text_view->cursor_visible;
2346 * gtk_text_view_place_cursor_onscreen:
2347 * @text_view: a #GtkTextView
2349 * Moves the cursor to the currently visible region of the
2350 * buffer, it it isn't there already.
2352 * Return value: TRUE if the cursor had to be moved.
2355 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2359 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2361 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2362 gtk_text_buffer_get_mark (get_buffer (text_view),
2365 if (clamp_iter_onscreen (text_view, &insert))
2367 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2375 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2377 if (text_view->first_validate_idle != 0)
2379 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2380 g_source_remove (text_view->first_validate_idle);
2381 text_view->first_validate_idle = 0;
2384 if (text_view->incremental_validate_idle != 0)
2386 g_source_remove (text_view->incremental_validate_idle);
2387 text_view->incremental_validate_idle = 0;
2392 gtk_text_view_destroy (GtkObject *object)
2394 GtkTextView *text_view;
2395 GtkTextLayout *layout;
2397 text_view = GTK_TEXT_VIEW (object);
2399 layout = text_view->layout;
2401 gtk_text_view_remove_validate_idles (text_view);
2402 gtk_text_view_set_buffer (text_view, NULL);
2403 gtk_text_view_destroy_layout (text_view);
2405 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2409 gtk_text_view_finalize (GObject *object)
2411 GtkTextView *text_view;
2413 text_view = GTK_TEXT_VIEW (object);
2415 g_return_if_fail (text_view->buffer == NULL);
2417 gtk_text_view_destroy_layout (text_view);
2418 gtk_text_view_set_buffer (text_view, NULL);
2420 if (text_view->pending_scroll)
2422 free_pending_scroll (text_view->pending_scroll);
2423 text_view->pending_scroll = NULL;
2426 if (text_view->tabs)
2427 pango_tab_array_free (text_view->tabs);
2429 if (text_view->hadjustment)
2430 g_object_unref (G_OBJECT (text_view->hadjustment));
2431 if (text_view->vadjustment)
2432 g_object_unref (G_OBJECT (text_view->vadjustment));
2434 text_window_free (text_view->text_window);
2436 if (text_view->left_window)
2437 text_window_free (text_view->left_window);
2439 if (text_view->top_window)
2440 text_window_free (text_view->top_window);
2442 if (text_view->right_window)
2443 text_window_free (text_view->right_window);
2445 if (text_view->bottom_window)
2446 text_window_free (text_view->bottom_window);
2448 g_object_unref (G_OBJECT (text_view->im_context));
2450 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2454 gtk_text_view_set_property (GObject *object,
2456 const GValue *value,
2459 GtkTextView *text_view;
2461 text_view = GTK_TEXT_VIEW (object);
2465 case PROP_PIXELS_ABOVE_LINES:
2466 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2469 case PROP_PIXELS_BELOW_LINES:
2470 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2473 case PROP_PIXELS_INSIDE_WRAP:
2474 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2478 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2481 case PROP_WRAP_MODE:
2482 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2485 case PROP_JUSTIFICATION:
2486 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2489 case PROP_LEFT_MARGIN:
2490 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2493 case PROP_RIGHT_MARGIN:
2494 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2498 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2502 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2505 case PROP_CURSOR_VISIBLE:
2506 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2510 g_assert_not_reached ();
2516 gtk_text_view_get_property (GObject *object,
2521 GtkTextView *text_view;
2523 text_view = GTK_TEXT_VIEW (object);
2527 case PROP_PIXELS_ABOVE_LINES:
2528 g_value_set_int (value, text_view->pixels_above_lines);
2531 case PROP_PIXELS_BELOW_LINES:
2532 g_value_set_int (value, text_view->pixels_below_lines);
2535 case PROP_PIXELS_INSIDE_WRAP:
2536 g_value_set_int (value, text_view->pixels_inside_wrap);
2540 g_value_set_boolean (value, text_view->editable);
2543 case PROP_WRAP_MODE:
2544 g_value_set_enum (value, text_view->wrap_mode);
2547 case PROP_JUSTIFICATION:
2548 g_value_set_enum (value, text_view->justify);
2551 case PROP_LEFT_MARGIN:
2552 g_value_set_int (value, text_view->left_margin);
2555 case PROP_RIGHT_MARGIN:
2556 g_value_set_int (value, text_view->right_margin);
2560 g_value_set_int (value, text_view->indent);
2564 g_value_set_boxed (value, text_view->tabs);
2567 case PROP_CURSOR_VISIBLE:
2568 g_value_set_boolean (value, text_view->cursor_visible);
2572 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2578 gtk_text_view_size_request (GtkWidget *widget,
2579 GtkRequisition *requisition)
2581 GtkTextView *text_view;
2583 gint focus_edge_width;
2585 gboolean interior_focus;
2587 text_view = GTK_TEXT_VIEW (widget);
2589 gtk_widget_style_get (widget,
2590 "interior_focus", &interior_focus,
2591 "focus_line_width", &focus_width,
2595 focus_edge_width = 0;
2597 focus_edge_width = focus_width;
2599 if (text_view->layout)
2601 text_view->text_window->requisition.width = text_view->layout->width;
2602 text_view->text_window->requisition.height = text_view->layout->height;
2606 text_view->text_window->requisition.width = 0;
2607 text_view->text_window->requisition.height = 0;
2610 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2611 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2613 if (text_view->left_window)
2614 requisition->width += text_view->left_window->requisition.width;
2616 if (text_view->right_window)
2617 requisition->width += text_view->right_window->requisition.width;
2619 if (text_view->top_window)
2620 requisition->height += text_view->top_window->requisition.height;
2622 if (text_view->bottom_window)
2623 requisition->height += text_view->bottom_window->requisition.height;
2625 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2626 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2628 tmp_list = text_view->children;
2629 while (tmp_list != NULL)
2631 GtkTextViewChild *child = tmp_list->data;
2635 GtkRequisition child_req;
2636 GtkRequisition old_req;
2638 gtk_widget_get_child_requisition (child->widget, &old_req);
2640 gtk_widget_size_request (child->widget, &child_req);
2642 gtk_widget_get_child_requisition (child->widget, &child_req);
2644 /* Invalidate layout lines if required */
2645 if (text_view->layout &&
2646 (old_req.width != child_req.width ||
2647 old_req.height != child_req.height))
2648 gtk_text_child_anchor_queue_resize (child->anchor,
2653 GtkRequisition child_req;
2655 gtk_widget_size_request (child->widget, &child_req);
2658 tmp_list = g_slist_next (tmp_list);
2663 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2664 GtkTextViewChild *vc,
2665 GtkAllocation *allocation)
2671 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2675 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2678 buffer_y += vc->from_top_of_line;
2680 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2681 allocation->y = buffer_y - text_view->yoffset;
2683 gtk_widget_get_child_requisition (vc->widget, &req);
2684 allocation->width = req.width;
2685 allocation->height = req.height;
2689 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2690 GtkTextViewChild *vc)
2692 GtkAllocation allocation;
2694 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2696 gtk_widget_size_allocate (vc->widget, &allocation);
2699 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2701 vc->widget->allocation.x,
2702 vc->widget->allocation.y,
2703 text_view->yoffset);
2708 gtk_text_view_child_allocated (GtkTextLayout *layout,
2714 GtkTextViewChild *vc = NULL;
2715 GtkTextView *text_view = data;
2717 /* x,y is the position of the child from the top of the line, and
2718 * from the left of the buffer. We have to translate that into text
2719 * window coordinates, then size_allocate the child.
2722 vc = g_object_get_data (G_OBJECT (child),
2723 "gtk-text-view-child");
2725 g_assert (vc != NULL);
2727 DV (g_print ("child allocated at %d,%d\n", x, y));
2729 vc->from_left_of_buffer = x;
2730 vc->from_top_of_line = y;
2732 gtk_text_view_update_child_allocation (text_view, vc);
2736 gtk_text_view_allocate_children (GtkTextView *text_view)
2740 DV(g_print(G_STRLOC"\n"));
2742 tmp_list = text_view->children;
2743 while (tmp_list != NULL)
2745 GtkTextViewChild *child = tmp_list->data;
2749 /* We need to force-validate the regions containing
2752 GtkTextIter child_loc;
2753 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2757 gtk_text_layout_validate_yrange (text_view->layout,
2763 GtkAllocation allocation;
2764 GtkRequisition child_req;
2766 g_assert (child != NULL);
2768 allocation.x = child->x;
2769 allocation.y = child->y;
2771 gtk_widget_get_child_requisition (child->widget, &child_req);
2773 allocation.width = child_req.width;
2774 allocation.height = child_req.height;
2776 gtk_widget_size_allocate (child->widget, &allocation);
2779 tmp_list = g_slist_next (tmp_list);
2784 gtk_text_view_size_allocate (GtkWidget *widget,
2785 GtkAllocation *allocation)
2787 GtkTextView *text_view;
2788 GtkTextIter first_para;
2790 GtkAdjustment *vadj;
2791 gboolean yoffset_changed = FALSE;
2793 GdkRectangle text_rect;
2794 GdkRectangle left_rect;
2795 GdkRectangle right_rect;
2796 GdkRectangle top_rect;
2797 GdkRectangle bottom_rect;
2798 gint focus_edge_width;
2800 gboolean interior_focus;
2801 gboolean size_changed;
2803 text_view = GTK_TEXT_VIEW (widget);
2805 DV(g_print(G_STRLOC"\n"));
2808 widget->allocation.width != allocation->width ||
2809 widget->allocation.height != allocation->height;
2811 widget->allocation = *allocation;
2813 if (GTK_WIDGET_REALIZED (widget))
2815 gdk_window_move_resize (widget->window,
2816 allocation->x, allocation->y,
2817 allocation->width, allocation->height);
2820 /* distribute width/height among child windows. Ensure all
2821 * windows get at least a 1x1 allocation.
2824 gtk_widget_style_get (widget,
2825 "interior_focus", &interior_focus,
2826 "focus_line_width", &focus_width,
2830 focus_edge_width = 0;
2832 focus_edge_width = focus_width;
2834 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2836 if (text_view->left_window)
2837 left_rect.width = text_view->left_window->requisition.width;
2839 left_rect.width = 0;
2841 width -= left_rect.width;
2843 if (text_view->right_window)
2844 right_rect.width = text_view->right_window->requisition.width;
2846 right_rect.width = 0;
2848 width -= right_rect.width;
2850 text_rect.width = MAX (1, width);
2852 top_rect.width = text_rect.width;
2853 bottom_rect.width = text_rect.width;
2856 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2858 if (text_view->top_window)
2859 top_rect.height = text_view->top_window->requisition.height;
2861 top_rect.height = 0;
2863 height -= top_rect.height;
2865 if (text_view->bottom_window)
2866 bottom_rect.height = text_view->bottom_window->requisition.height;
2868 bottom_rect.height = 0;
2870 height -= bottom_rect.height;
2872 text_rect.height = MAX (1, height);
2874 left_rect.height = text_rect.height;
2875 right_rect.height = text_rect.height;
2878 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2879 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2881 text_rect.x = left_rect.x + left_rect.width;
2882 text_rect.y = top_rect.y + top_rect.height;
2884 left_rect.y = text_rect.y;
2885 right_rect.y = text_rect.y;
2887 top_rect.x = text_rect.x;
2888 bottom_rect.x = text_rect.x;
2890 right_rect.x = text_rect.x + text_rect.width;
2891 bottom_rect.y = text_rect.y + text_rect.height;
2893 text_window_size_allocate (text_view->text_window,
2896 if (text_view->left_window)
2897 text_window_size_allocate (text_view->left_window,
2900 if (text_view->right_window)
2901 text_window_size_allocate (text_view->right_window,
2904 if (text_view->top_window)
2905 text_window_size_allocate (text_view->top_window,
2908 if (text_view->bottom_window)
2909 text_window_size_allocate (text_view->bottom_window,
2912 gtk_text_view_update_layout_width (text_view);
2914 /* Note that this will do some layout validation */
2915 gtk_text_view_allocate_children (text_view);
2917 /* Now adjust the value of the adjustment to keep the cursor at the
2918 * same place in the buffer
2920 gtk_text_view_get_first_para_iter (text_view, &first_para);
2921 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2923 y += text_view->first_para_pixels;
2925 /* Ensure h/v adj exist */
2926 get_hadjustment (text_view);
2927 get_vadjustment (text_view);
2929 vadj = text_view->vadjustment;
2930 if (y > vadj->upper - vadj->page_size)
2931 y = MAX (0, vadj->upper - vadj->page_size);
2933 if (y != text_view->yoffset)
2936 yoffset_changed = TRUE;
2939 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2940 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
2941 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
2942 text_view->hadjustment->lower = 0;
2943 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2945 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2947 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2948 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2949 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2950 text_view->vadjustment->lower = 0;
2951 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2953 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2955 if (yoffset_changed)
2956 gtk_adjustment_value_changed (vadj);
2958 /* The GTK resize loop processes all the pending exposes right
2959 * after doing the resize stuff, so the idle sizer won't have a
2960 * chance to run. So we do the work here.
2962 gtk_text_view_flush_first_validate (text_view);
2964 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2967 if (size_changed && GTK_WIDGET_REALIZED (widget))
2968 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2972 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2975 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2976 text_view->first_para_mark);
2980 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2982 GtkWidget *widget = GTK_WIDGET (text_view);
2984 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
2986 if (SCREEN_HEIGHT (widget) > 0)
2988 GtkTextIter first_para;
2990 /* Be sure we've validated the stuff onscreen; if we
2991 * scrolled, these calls won't have any effect, because
2992 * they were called in the recursive validate_onscreen
2994 gtk_text_view_get_first_para_iter (text_view, &first_para);
2996 gtk_text_layout_validate_yrange (text_view->layout,
2999 text_view->first_para_pixels +
3000 SCREEN_HEIGHT (widget));
3003 text_view->onscreen_validated = TRUE;
3005 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3007 /* This can have the odd side effect of triggering a scroll, which should
3008 * flip "onscreen_validated" back to FALSE, but should also get us
3009 * back into this function to turn it on again.
3011 gtk_text_view_update_adjustments (text_view);
3013 g_assert (text_view->onscreen_validated);
3017 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3019 if (text_view->first_validate_idle == 0)
3022 /* Do this first, which means that if an "invalidate"
3023 * occurs during any of this process, a new first_validate_callback
3024 * will be installed, and we'll start again.
3026 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3027 g_source_remove (text_view->first_validate_idle);
3028 text_view->first_validate_idle = 0;
3030 /* be sure we have up-to-date screen size set on the
3033 gtk_text_view_update_layout_width (text_view);
3035 /* Bail out if we invalidated stuff; scrolling right away will just
3036 * confuse the issue.
3038 if (text_view->first_validate_idle != 0)
3040 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3044 /* scroll to any marks, if that's pending. This can
3045 * jump us to the validation codepath used for scrolling
3046 * onscreen, if so we bail out.
3048 if (!gtk_text_view_flush_scroll (text_view))
3049 gtk_text_view_validate_onscreen (text_view);
3051 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3053 g_assert (text_view->onscreen_validated);
3058 first_validate_callback (gpointer data)
3060 GtkTextView *text_view = data;
3062 GDK_THREADS_ENTER ();
3064 /* Note that some of this code is duplicated at the end of size_allocate,
3065 * keep in sync with that.
3068 DV(g_print(G_STRLOC"\n"));
3070 gtk_text_view_flush_first_validate (text_view);
3072 GDK_THREADS_LEAVE ();
3078 incremental_validate_callback (gpointer data)
3080 GtkTextView *text_view = data;
3081 gboolean result = TRUE;
3083 GDK_THREADS_ENTER ();
3085 DV(g_print(G_STRLOC"\n"));
3087 gtk_text_layout_validate (text_view->layout, 2000);
3089 gtk_text_view_update_adjustments (text_view);
3091 if (gtk_text_layout_is_valid (text_view->layout))
3093 text_view->incremental_validate_idle = 0;
3097 GDK_THREADS_LEAVE ();
3103 gtk_text_view_invalidate (GtkTextView *text_view)
3105 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3106 text_view->onscreen_validated));
3108 text_view->onscreen_validated = FALSE;
3110 /* We'll invalidate when the layout is created */
3111 if (text_view->layout == NULL)
3114 if (!text_view->first_validate_idle)
3116 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3117 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3118 text_view->first_validate_idle));
3121 if (!text_view->incremental_validate_idle)
3123 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3124 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3125 text_view->incremental_validate_idle));
3130 invalidated_handler (GtkTextLayout *layout,
3133 GtkTextView *text_view;
3135 text_view = GTK_TEXT_VIEW (data);
3137 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3138 gtk_text_view_invalidate (text_view);
3142 changed_handler (GtkTextLayout *layout,
3148 GtkTextView *text_view;
3150 GdkRectangle visible_rect;
3151 GdkRectangle redraw_rect;
3153 text_view = GTK_TEXT_VIEW (data);
3154 widget = GTK_WIDGET (data);
3156 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3158 if (GTK_WIDGET_REALIZED (text_view))
3160 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3162 redraw_rect.x = visible_rect.x;
3163 redraw_rect.width = visible_rect.width;
3164 redraw_rect.y = start_y;
3166 if (old_height == new_height)
3167 redraw_rect.height = old_height;
3169 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3171 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3173 /* text_window_invalidate_rect() takes buffer coordinates */
3174 text_window_invalidate_rect (text_view->text_window,
3177 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3181 redraw_rect.height));
3183 if (text_view->left_window)
3184 text_window_invalidate_rect (text_view->left_window,
3186 if (text_view->right_window)
3187 text_window_invalidate_rect (text_view->right_window,
3189 if (text_view->top_window)
3190 text_window_invalidate_rect (text_view->top_window,
3192 if (text_view->bottom_window)
3193 text_window_invalidate_rect (text_view->bottom_window,
3196 gtk_text_view_update_im_spot_location (text_view);
3200 if (old_height != new_height)
3202 gboolean yoffset_changed = FALSE;
3204 int new_first_para_top;
3205 int old_first_para_top;
3208 /* If the bottom of the old area was above the top of the
3209 * screen, we need to scroll to keep the current top of the
3210 * screen in place. Remember that first_para_pixels is the
3211 * position of the top of the screen in coordinates relative to
3212 * the first paragraph onscreen.
3214 * In short we are adding the height delta of the portion of the
3215 * changed region above first_para_mark to text_view->yoffset.
3217 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3218 text_view->first_para_mark);
3220 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3222 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3224 if (new_first_para_top != old_first_para_top)
3226 text_view->yoffset += new_first_para_top - old_first_para_top;
3228 get_vadjustment (text_view)->value = text_view->yoffset;
3229 yoffset_changed = TRUE;
3232 if (yoffset_changed)
3234 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3235 gtk_adjustment_value_changed (get_vadjustment (text_view));
3238 /* FIXME be smarter about which anchored widgets we update */
3240 tmp_list = text_view->children;
3241 while (tmp_list != NULL)
3243 GtkTextViewChild *child = tmp_list->data;
3246 gtk_text_view_update_child_allocation (text_view, child);
3248 tmp_list = g_slist_next (tmp_list);
3253 GtkRequisition old_req;
3254 GtkRequisition new_req;
3256 old_req = widget->requisition;
3258 /* Use this instead of gtk_widget_size_request wrapper
3259 * to avoid the optimization which just returns widget->requisition
3260 * if a resize hasn't been queued.
3262 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3264 if (old_req.width != new_req.width ||
3265 old_req.height != new_req.height)
3267 /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3268 _gtk_size_group_queue_resize (widget);
3274 gtk_text_view_realize (GtkWidget *widget)
3276 GtkTextView *text_view;
3277 GdkWindowAttr attributes;
3278 gint attributes_mask;
3281 text_view = GTK_TEXT_VIEW (widget);
3282 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3284 attributes.window_type = GDK_WINDOW_CHILD;
3285 attributes.x = widget->allocation.x;
3286 attributes.y = widget->allocation.y;
3287 attributes.width = widget->allocation.width;
3288 attributes.height = widget->allocation.height;
3289 attributes.wclass = GDK_INPUT_OUTPUT;
3290 attributes.visual = gtk_widget_get_visual (widget);
3291 attributes.colormap = gtk_widget_get_colormap (widget);
3292 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3294 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3296 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3297 &attributes, attributes_mask);
3298 gdk_window_set_user_data (widget->window, widget);
3300 /* must come before text_window_realize calls */
3301 widget->style = gtk_style_attach (widget->style, widget->window);
3303 gdk_window_set_background (widget->window,
3304 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3306 text_window_realize (text_view->text_window, widget->window);
3308 if (text_view->left_window)
3309 text_window_realize (text_view->left_window,
3312 if (text_view->top_window)
3313 text_window_realize (text_view->top_window,
3316 if (text_view->right_window)
3317 text_window_realize (text_view->right_window,
3320 if (text_view->bottom_window)
3321 text_window_realize (text_view->bottom_window,
3324 gtk_text_view_ensure_layout (text_view);
3326 if (text_view->buffer)
3328 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3329 GDK_SELECTION_PRIMARY);
3330 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3333 tmp_list = text_view->children;
3334 while (tmp_list != NULL)
3336 GtkTextViewChild *vc = tmp_list->data;
3338 text_view_child_set_parent_window (text_view, vc);
3340 tmp_list = tmp_list->next;
3345 gtk_text_view_unrealize (GtkWidget *widget)
3347 GtkTextView *text_view;
3349 text_view = GTK_TEXT_VIEW (widget);
3351 if (text_view->buffer)
3353 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3354 GDK_SELECTION_PRIMARY);
3355 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3358 gtk_text_view_remove_validate_idles (text_view);
3360 if (text_view->popup_menu)
3362 gtk_widget_destroy (text_view->popup_menu);
3363 text_view->popup_menu = NULL;
3366 text_window_unrealize (text_view->text_window);
3368 if (text_view->left_window)
3369 text_window_unrealize (text_view->left_window);
3371 if (text_view->top_window)
3372 text_window_unrealize (text_view->top_window);
3374 if (text_view->right_window)
3375 text_window_unrealize (text_view->right_window);
3377 if (text_view->bottom_window)
3378 text_window_unrealize (text_view->bottom_window);
3380 gtk_text_view_destroy_layout (text_view);
3382 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3386 gtk_text_view_style_set (GtkWidget *widget,
3387 GtkStyle *previous_style)
3389 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3391 if (GTK_WIDGET_REALIZED (widget))
3393 gdk_window_set_background (widget->window,
3394 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3396 gdk_window_set_background (text_view->text_window->bin_window,
3397 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3399 if (text_view->left_window)
3400 gdk_window_set_background (text_view->left_window->bin_window,
3401 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3402 if (text_view->right_window)
3403 gdk_window_set_background (text_view->right_window->bin_window,
3404 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3406 if (text_view->top_window)
3407 gdk_window_set_background (text_view->top_window->bin_window,
3408 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3410 if (text_view->bottom_window)
3411 gdk_window_set_background (text_view->bottom_window->bin_window,
3412 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3415 if (text_view->layout && previous_style)
3417 gtk_text_view_set_attributes_from_style (text_view,
3418 text_view->layout->default_style,
3420 gtk_text_layout_default_style_changed (text_view->layout);
3425 gtk_text_view_direction_changed (GtkWidget *widget,
3426 GtkTextDirection previous_direction)
3428 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3430 if (text_view->layout)
3432 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3433 gtk_text_layout_default_style_changed (text_view->layout);
3439 set_invisible_cursor (GdkWindow *window)
3441 GdkBitmap *empty_bitmap;
3444 char invisible_cursor_bits[] = { 0x0 };
3446 useless.red = useless.green = useless.blue = 0;
3449 empty_bitmap = gdk_bitmap_create_from_data (window,
3450 invisible_cursor_bits,
3453 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3458 gdk_window_set_cursor (window, cursor);
3460 gdk_cursor_unref (cursor);
3462 g_object_unref (empty_bitmap);
3466 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3468 if (text_view->mouse_cursor_obscured)
3471 set_invisible_cursor (text_view->text_window->bin_window);
3473 text_view->mouse_cursor_obscured = TRUE;
3481 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3484 switch (event->type)
3486 case GDK_MOTION_NOTIFY:
3487 *x = event->motion.x;
3488 *y = event->motion.y;
3492 case GDK_BUTTON_PRESS:
3493 case GDK_2BUTTON_PRESS:
3494 case GDK_3BUTTON_PRESS:
3495 case GDK_BUTTON_RELEASE:
3496 *x = event->button.x;
3497 *y = event->button.y;
3502 case GDK_KEY_RELEASE:
3503 case GDK_ENTER_NOTIFY:
3504 case GDK_LEAVE_NOTIFY:
3505 case GDK_PROPERTY_NOTIFY:
3506 case GDK_SELECTION_CLEAR:
3507 case GDK_SELECTION_REQUEST:
3508 case GDK_SELECTION_NOTIFY:
3509 case GDK_PROXIMITY_IN:
3510 case GDK_PROXIMITY_OUT:
3511 case GDK_DRAG_ENTER:
3512 case GDK_DRAG_LEAVE:
3513 case GDK_DRAG_MOTION:
3514 case GDK_DRAG_STATUS:
3515 case GDK_DROP_START:
3516 case GDK_DROP_FINISHED:
3526 emit_event_on_tags (GtkWidget *widget,
3532 gboolean retval = FALSE;
3533 GtkTextView *text_view;
3535 text_view = GTK_TEXT_VIEW (widget);
3537 tags = gtk_text_iter_get_tags (iter);
3542 GtkTextTag *tag = tmp->data;
3544 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3550 tmp = g_slist_next (tmp);
3553 g_slist_free (tags);
3559 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3561 GtkTextView *text_view;
3564 text_view = GTK_TEXT_VIEW (widget);
3566 if (text_view->layout == NULL ||
3567 get_buffer (text_view) == NULL)
3570 if (event->any.window != text_view->text_window->bin_window)
3573 if (get_event_coordinates (event, &x, &y))
3577 x += text_view->xoffset;
3578 y += text_view->yoffset;
3580 /* FIXME this is slow and we do it twice per event.
3581 * My favorite solution is to have GtkTextLayout cache
3582 * the last couple lookups.
3584 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3588 return emit_event_on_tags (widget, event, &iter);
3590 else if (event->type == GDK_KEY_PRESS ||
3591 event->type == GDK_KEY_RELEASE)
3593 GtkTextMark *insert;
3596 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3599 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3601 return emit_event_on_tags (widget, event, &iter);
3608 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3610 gboolean retval = FALSE;
3611 gboolean obscure = FALSE;
3612 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3613 GtkTextMark *insert;
3615 gboolean can_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 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3625 gtk_im_context_filter_keypress (text_view->im_context, event))
3627 text_view->need_im_reset = TRUE;
3632 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3633 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3635 /* use overall editability not can_insert, more predictable for users */
3636 else if (text_view->editable &&
3637 (event->keyval == GDK_Return ||
3638 event->keyval == GDK_KP_Enter))
3640 /* this won't actually insert the newline if the cursor isn't
3643 gtk_text_view_commit_text (text_view, "\n");
3648 /* Pass through Tab as literal tab, unless Control is held down */
3649 else if ((event->keyval == GDK_Tab ||
3650 event->keyval == GDK_KP_Tab) &&
3651 !(event->state & GDK_CONTROL_MASK))
3653 /* If the text widget isn't editable overall, move the focus
3656 if (text_view->editable)
3658 gtk_text_view_commit_text (text_view, "\t");
3662 gtk_text_view_move_focus (text_view,
3663 (event->state & GDK_SHIFT_MASK) ?
3664 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3672 gtk_text_view_obscure_mouse_cursor (text_view);
3674 gtk_text_view_pend_cursor_blink (text_view);
3680 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3682 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3683 GtkTextMark *insert;
3686 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3689 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3690 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3691 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3692 gtk_im_context_filter_keypress (text_view->im_context, event))
3694 text_view->need_im_reset = TRUE;
3698 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3702 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3704 GtkTextView *text_view;
3706 text_view = GTK_TEXT_VIEW (widget);
3708 text_view->disable_scroll_on_focus = TRUE;
3709 gtk_widget_grab_focus (widget);
3710 text_view->disable_scroll_on_focus = FALSE;
3712 if (event->window != text_view->text_window->bin_window)
3714 /* Remove selection if any. */
3715 gtk_text_view_unselect (text_view);
3721 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3722 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3723 else if (event->button == 3)
3724 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3727 if (event->type == GDK_BUTTON_PRESS)
3729 gtk_text_view_reset_im_context (text_view);
3731 if (event->button == 1)
3733 /* If we're in the selection, start a drag copy/move of the
3734 * selection; otherwise, start creating a new selection.
3737 GtkTextIter start, end;
3739 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3741 event->x + text_view->xoffset,
3742 event->y + text_view->yoffset);
3744 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3746 gtk_text_iter_in_range (&iter, &start, &end))
3748 text_view->drag_start_x = event->x;
3749 text_view->drag_start_y = event->y;
3750 text_view->pending_place_cursor_button = event->button;
3754 gtk_text_view_start_selection_drag (text_view, &iter, event);
3759 else if (event->button == 2)
3763 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3765 event->x + text_view->xoffset,
3766 event->y + text_view->yoffset);
3768 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3769 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3771 text_view->editable);
3774 else if (event->button == 3)
3776 gtk_text_view_do_popup (text_view, event);
3779 else if ((event->type == GDK_2BUTTON_PRESS ||
3780 event->type == GDK_3BUTTON_PRESS) &&
3783 GtkTextIter start, end;
3785 /* End the selection drag, otherwise we'd clear the new
3786 * word/line selection on button release
3788 gtk_text_view_end_selection_drag (text_view, event);
3790 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3792 event->x + text_view->xoffset,
3793 event->y + text_view->yoffset);
3797 if (event->type == GDK_2BUTTON_PRESS)
3799 if (gtk_text_iter_inside_word (&start))
3801 if (!gtk_text_iter_starts_word (&start))
3802 gtk_text_iter_backward_word_start (&start);
3804 if (!gtk_text_iter_ends_word (&end))
3805 gtk_text_iter_forward_word_end (&end);
3808 else if (event->type == GDK_3BUTTON_PRESS)
3810 if (gtk_text_view_starts_display_line (text_view, &start))
3812 /* If on a display line boundary, we assume the user
3813 * clicked off the end of a line and we therefore select
3814 * the line before the boundary.
3816 gtk_text_view_backward_display_line_start (text_view, &start);
3820 /* start isn't on the start of a line, so we move it to the
3821 * start, and move end to the end unless it's already there.
3823 gtk_text_view_backward_display_line_start (text_view, &start);
3825 if (!gtk_text_view_starts_display_line (text_view, &end))
3826 gtk_text_view_forward_display_line_end (text_view, &end);
3830 if (event->state & GDK_SHIFT_MASK)
3832 /* Take union of old and new selection */
3833 GtkTextIter old_start, old_end;
3835 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3836 &old_start, &old_end);
3838 gtk_text_iter_order (&start, &old_start);
3839 gtk_text_iter_order (&old_end, &end);
3841 /* Now start is the first of the starts, and end is the
3846 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3849 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3853 text_view->just_selected_element = TRUE;
3862 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3864 GtkTextView *text_view;
3866 text_view = GTK_TEXT_VIEW (widget);
3868 if (event->window != text_view->text_window->bin_window)
3871 if (event->button == 1)
3873 if (text_view->drag_start_x >= 0)
3875 text_view->drag_start_x = -1;
3876 text_view->drag_start_y = -1;
3879 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3881 else if (text_view->just_selected_element)
3883 text_view->just_selected_element = FALSE;
3886 else if (text_view->pending_place_cursor_button == event->button)
3890 /* Unselect everything; we clicked inside selection, but
3891 * didn't move by the drag threshold, so just clear selection
3894 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3896 event->x + text_view->xoffset,
3897 event->y + text_view->yoffset);
3899 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3901 text_view->pending_place_cursor_button = 0;
3911 keymap_direction_changed (GdkKeymap *keymap,
3912 GtkTextView *text_view)
3914 gtk_text_view_check_keymap_direction (text_view);
3918 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3920 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3922 gtk_widget_queue_draw (widget);
3924 DV(g_print (G_STRLOC": focus_in_event\n"));
3926 if (text_view->cursor_visible && text_view->layout)
3928 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3929 gtk_text_view_check_cursor_blink (text_view);
3932 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3933 "direction_changed",
3934 G_CALLBACK (keymap_direction_changed), text_view);
3935 gtk_text_view_check_keymap_direction (text_view);
3937 text_view->need_im_reset = TRUE;
3938 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3944 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3946 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3948 gtk_widget_queue_draw (widget);
3950 DV(g_print (G_STRLOC": focus_out_event\n"));
3952 if (text_view->cursor_visible && text_view->layout)
3954 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3955 gtk_text_view_check_cursor_blink (text_view);
3958 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
3959 (gpointer) keymap_direction_changed,
3962 text_view->need_im_reset = TRUE;
3963 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3969 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3971 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3973 if (text_view->mouse_cursor_obscured)
3977 cursor = gdk_cursor_new_for_screen (gtk_widget_get_screen (widget),
3979 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3980 gdk_cursor_unref (cursor);
3981 text_view->mouse_cursor_obscured = FALSE;
3984 if (event->window == text_view->text_window->bin_window &&
3985 text_view->drag_start_x >= 0)
3989 gdk_window_get_pointer (text_view->text_window->bin_window,
3992 if (gtk_drag_check_threshold (widget,
3993 text_view->drag_start_x,
3994 text_view->drag_start_y,
3998 gint buffer_x, buffer_y;
4000 gtk_text_view_window_to_buffer_coords (text_view,
4001 GTK_TEXT_WINDOW_TEXT,
4002 text_view->drag_start_x,
4003 text_view->drag_start_y,
4007 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4009 buffer_x, buffer_y);
4011 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4020 gtk_text_view_paint (GtkWidget *widget,
4022 GdkEventExpose *event)
4024 GtkTextView *text_view;
4025 GList *child_exposes;
4029 text_view = GTK_TEXT_VIEW (widget);
4031 g_return_if_fail (text_view->layout != NULL);
4032 g_return_if_fail (text_view->xoffset >= 0);
4033 g_return_if_fail (text_view->yoffset >= 0);
4035 while (text_view->first_validate_idle != 0)
4037 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4038 text_view->first_validate_idle));
4039 gtk_text_view_flush_first_validate (text_view);
4042 /* More regions could have become invalid in the above loop */
4043 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4048 gdk_region_get_clipbox (updates, &rect);
4050 gdk_rectangle_union (area, &rect, area);
4052 gdk_region_destroy (updates);
4055 if (!text_view->onscreen_validated)
4057 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.");
4058 g_assert_not_reached ();
4062 printf ("painting %d,%d %d x %d\n",
4064 area->width, area->height);
4067 child_exposes = NULL;
4068 gtk_text_layout_draw (text_view->layout,
4070 text_view->text_window->bin_window,
4075 area->width, area->height,
4078 tmp_list = child_exposes;
4079 while (tmp_list != NULL)
4081 GtkWidget *child = tmp_list->data;
4083 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4087 g_object_unref (G_OBJECT (child));
4089 tmp_list = tmp_list->next;
4092 g_list_free (child_exposes);
4096 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4100 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4101 GTK_TEXT_WINDOW_TEXT))
4103 DV(g_print (">Exposed ("G_STRLOC")\n"));
4104 gtk_text_view_paint (widget, &event->area, event);
4107 if (event->window == widget->window)
4108 gtk_text_view_draw_focus (widget);
4110 /* Propagate exposes to all children not in the buffer. */
4111 tmp_list = GTK_TEXT_VIEW (widget)->children;
4112 while (tmp_list != NULL)
4114 GtkTextViewChild *vc = tmp_list->data;
4116 /* propagate_expose checks that event->window matches
4119 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4120 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4124 tmp_list = tmp_list->next;
4131 gtk_text_view_draw_focus (GtkWidget *widget)
4133 gboolean interior_focus;
4135 /* We clear the focus if we are in interior focus mode. */
4136 gtk_widget_style_get (widget,
4137 "interior_focus", &interior_focus,
4140 if (GTK_WIDGET_DRAWABLE (widget))
4142 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4144 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4145 NULL, widget, "textview",
4147 widget->allocation.width,
4148 widget->allocation.height);
4152 gdk_window_clear (widget->window);
4158 gtk_text_view_grab_focus (GtkWidget *widget)
4160 GtkTextView *text_view;
4162 text_view = GTK_TEXT_VIEW (widget);
4164 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4166 if (!text_view->disable_scroll_on_focus)
4167 gtk_text_view_scroll_mark_onscreen (text_view,
4168 gtk_text_buffer_get_mark (get_buffer (text_view),
4173 gtk_text_view_focus (GtkWidget *widget,
4174 GtkDirectionType direction)
4176 GtkTextView *text_view;
4177 GtkContainer *container;
4179 text_view = GTK_TEXT_VIEW (widget);
4180 container = GTK_CONTAINER (widget);
4182 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4190 gtk_text_view_add (GtkContainer *container,
4193 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4194 g_return_if_fail (GTK_IS_WIDGET (child));
4196 /* This is pretty random. */
4197 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4199 GTK_TEXT_WINDOW_WIDGET,
4204 gtk_text_view_remove (GtkContainer *container,
4208 GtkTextView *text_view;
4209 GtkTextViewChild *vc;
4211 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4212 g_return_if_fail (GTK_IS_WIDGET (child));
4213 g_return_if_fail (child->parent == (GtkWidget*) container);
4215 text_view = GTK_TEXT_VIEW (container);
4218 iter = text_view->children;
4220 while (iter != NULL)
4224 if (vc->widget == child)
4227 iter = g_slist_next (iter);
4230 g_assert (iter != NULL); /* be sure we had the child in the list */
4232 text_view->children = g_slist_remove (text_view->children, vc);
4234 gtk_widget_unparent (vc->widget);
4236 text_view_child_free (vc);
4240 gtk_text_view_forall (GtkContainer *container,
4241 gboolean include_internals,
4242 GtkCallback callback,
4243 gpointer callback_data)
4246 GtkTextView *text_view;
4249 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4250 g_return_if_fail (callback != NULL);
4252 text_view = GTK_TEXT_VIEW (container);
4254 copy = g_slist_copy (text_view->children);
4257 while (iter != NULL)
4259 GtkTextViewChild *vc = iter->data;
4261 (* callback) (vc->widget, callback_data);
4263 iter = g_slist_next (iter);
4266 g_slist_free (copy);
4269 #define CURSOR_ON_MULTIPLIER 0.66
4270 #define CURSOR_OFF_MULTIPLIER 0.34
4271 #define CURSOR_PEND_MULTIPLIER 1.0
4274 cursor_blinks (GtkTextView *text_view)
4276 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4279 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4282 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4285 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4290 get_cursor_time (GtkTextView *text_view)
4292 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4295 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4305 blink_cb (gpointer data)
4307 GtkTextView *text_view;
4310 GDK_THREADS_ENTER ();
4312 text_view = GTK_TEXT_VIEW (data);
4314 g_assert (text_view->layout);
4315 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4316 g_assert (text_view->cursor_visible);
4318 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4321 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4325 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4329 gtk_text_layout_set_cursor_visible (text_view->layout,
4332 GDK_THREADS_LEAVE ();
4334 /* Remove ourselves */
4340 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4342 if (text_view->blink_timeout)
4344 gtk_timeout_remove (text_view->blink_timeout);
4345 text_view->blink_timeout = 0;
4350 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4352 if (text_view->layout != NULL &&
4353 text_view->cursor_visible &&
4354 GTK_WIDGET_HAS_FOCUS (text_view))
4356 if (cursor_blinks (text_view))
4358 if (text_view->blink_timeout == 0)
4360 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4362 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4368 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4372 gtk_text_view_stop_cursor_blink (text_view);
4377 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4379 if (text_view->layout != NULL &&
4380 text_view->cursor_visible &&
4381 GTK_WIDGET_HAS_FOCUS (text_view) &&
4382 cursor_blinks (text_view))
4384 if (text_view->blink_timeout != 0)
4386 gtk_timeout_remove (text_view->blink_timeout);
4387 text_view->blink_timeout = 0;
4390 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4392 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4400 * Key binding handlers
4404 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4405 GtkTextIter *newplace,
4410 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4416 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4422 move_cursor (GtkTextView *text_view,
4423 const GtkTextIter *new_location,
4424 gboolean extend_selection)
4426 if (extend_selection)
4427 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4431 gtk_text_buffer_place_cursor (get_buffer (text_view),
4435 /* FIXME when we are unfrozen and can change GtkMovementStep,
4438 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4441 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4442 GtkMovementStep step,
4444 gboolean extend_selection)
4447 GtkTextIter newplace;
4449 gint cursor_x_pos = 0;
4451 gtk_text_view_reset_im_context (text_view);
4453 if (step == GTK_MOVEMENT_PAGES)
4455 gtk_text_view_scroll_pages (text_view, count, extend_selection);
4456 gtk_text_view_pend_cursor_blink (text_view);
4459 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4461 gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4462 gtk_text_view_pend_cursor_blink (text_view);
4466 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4467 gtk_text_buffer_get_mark (get_buffer (text_view),
4471 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4472 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4476 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4477 gtk_text_iter_forward_cursor_positions (&newplace, count);
4480 case GTK_MOVEMENT_VISUAL_POSITIONS:
4481 gtk_text_layout_move_iter_visually (text_view->layout,
4485 case GTK_MOVEMENT_WORDS:
4487 gtk_text_iter_backward_word_starts (&newplace, -count);
4489 gtk_text_iter_forward_word_ends (&newplace, count);
4492 case GTK_MOVEMENT_DISPLAY_LINES:
4493 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4494 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4497 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4499 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4500 else if (count < -1)
4501 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4504 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4507 case GTK_MOVEMENT_PARAGRAPHS:
4510 if (!gtk_text_iter_ends_line (&newplace))
4512 gtk_text_iter_forward_to_line_end (&newplace);
4518 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4520 gtk_text_iter_set_line_offset (&newplace, 0);
4527 gtk_text_iter_forward_lines (&newplace, count);
4528 gtk_text_iter_set_line_offset (&newplace, 0);
4532 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4535 if (!gtk_text_iter_ends_line (&newplace))
4536 gtk_text_iter_forward_to_line_end (&newplace);
4540 gtk_text_iter_set_line_offset (&newplace, 0);
4544 case GTK_MOVEMENT_BUFFER_ENDS:
4546 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4548 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4555 if (!gtk_text_iter_equal (&insert, &newplace))
4557 move_cursor (text_view, &newplace, extend_selection);
4559 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4560 gtk_text_view_scroll_mark_onscreen (text_view,
4561 gtk_text_buffer_get_mark (get_buffer (text_view),
4564 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4566 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4570 gtk_text_view_pend_cursor_blink (text_view);
4574 gtk_text_view_move_cursor (GtkTextView *text_view,
4575 GtkMovementStep step,
4577 gboolean extend_selection)
4579 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4583 gtk_text_view_page_horizontally (GtkTextView *text_view,
4585 gboolean extend_selection)
4587 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4588 count, extend_selection);
4592 gtk_text_view_set_anchor (GtkTextView *text_view)
4596 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4597 gtk_text_buffer_get_mark (get_buffer (text_view),
4600 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4604 gtk_text_view_scroll_pages (GtkTextView *text_view,
4606 gboolean extend_selection)
4611 gint cursor_x_pos, cursor_y_pos;
4612 GtkTextIter new_insert;
4616 g_return_if_fail (text_view->vadjustment != NULL);
4618 adj = text_view->vadjustment;
4620 /* Validate the region that will be brought into view by the cursor motion
4624 gtk_text_view_get_first_para_iter (text_view, &anchor);
4625 y0 = adj->page_size;
4626 y1 = adj->page_size + count * adj->page_increment;
4630 gtk_text_view_get_first_para_iter (text_view, &anchor);
4631 y0 = count * adj->page_increment + adj->page_size;
4635 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4636 /* FIXME do we need to update the adjustment ranges here? */
4638 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4640 /* already at top, just be sure we are at offset 0 */
4641 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4642 move_cursor (text_view, &new_insert, extend_selection);
4644 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4646 /* already at bottom, just be sure we are at the end */
4647 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4648 move_cursor (text_view, &new_insert, extend_selection);
4652 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4654 newval = adj->value;
4655 oldval = adj->value;
4657 newval += count * adj->page_increment;
4659 set_adjustment_clamped (adj, newval);
4660 cursor_y_pos += adj->value - oldval;
4662 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4663 clamp_iter_onscreen (text_view, &new_insert);
4664 move_cursor (text_view, &new_insert, extend_selection);
4666 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4669 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4670 * only guarantees 1 pixel onscreen.
4672 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4673 gtk_text_view_scroll_mark_onscreen (text_view,
4674 gtk_text_buffer_get_mark (get_buffer (text_view),
4679 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4681 gboolean extend_selection)
4686 gint cursor_x_pos, cursor_y_pos;
4687 GtkTextIter new_insert;
4690 g_return_if_fail (text_view->hadjustment != NULL);
4692 adj = text_view->hadjustment;
4694 /* Validate the line that we're moving within.
4696 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4698 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4699 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4700 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4701 /* FIXME do we need to update the adjustment ranges here? */
4703 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4705 /* already at far left, just be sure we are at offset 0 */
4706 gtk_text_iter_set_line_offset (&new_insert, 0);
4707 move_cursor (text_view, &new_insert, extend_selection);
4709 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4711 /* already at far right, just be sure we are at the end */
4712 gtk_text_iter_forward_to_line_end (&new_insert);
4713 move_cursor (text_view, &new_insert, extend_selection);
4717 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4719 newval = adj->value;
4720 oldval = adj->value;
4722 newval += count * adj->page_increment;
4724 set_adjustment_clamped (adj, newval);
4725 cursor_x_pos += adj->value - oldval;
4727 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4728 clamp_iter_onscreen (text_view, &new_insert);
4729 move_cursor (text_view, &new_insert, extend_selection);
4731 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4734 /* FIXME for lines shorter than the overall widget width, this results in a
4735 * "bounce" effect as we scroll to the right of the widget, then scroll
4736 * back to get the end of the line onscreen.
4737 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4740 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4741 * only guarantees 1 pixel onscreen.
4743 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4744 gtk_text_view_scroll_mark_onscreen (text_view,
4745 gtk_text_buffer_get_mark (get_buffer (text_view),
4750 whitespace (gunichar ch, gpointer user_data)
4752 return (ch == ' ' || ch == '\t');
4756 not_whitespace (gunichar ch, gpointer user_data)
4758 return !whitespace (ch, user_data);
4762 find_whitepace_region (const GtkTextIter *center,
4763 GtkTextIter *start, GtkTextIter *end)
4768 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4769 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4770 if (whitespace (gtk_text_iter_get_char (end), NULL))
4771 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4773 return !gtk_text_iter_equal (start, end);
4777 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4780 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4781 text_view->editable);
4785 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4792 gboolean leave_one = FALSE;
4794 gtk_text_view_reset_im_context (text_view);
4796 if (type == GTK_DELETE_CHARS)
4798 /* Char delete deletes the selection, if one exists */
4799 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4800 text_view->editable))
4804 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4806 gtk_text_buffer_get_mark (get_buffer (text_view),
4814 case GTK_DELETE_CHARS:
4815 gtk_text_iter_forward_cursor_positions (&end, count);
4818 case GTK_DELETE_WORD_ENDS:
4820 gtk_text_iter_forward_word_ends (&end, count);
4822 gtk_text_iter_backward_word_starts (&start, 0 - count);
4825 case GTK_DELETE_WORDS:
4828 case GTK_DELETE_DISPLAY_LINE_ENDS:
4831 case GTK_DELETE_DISPLAY_LINES:
4834 case GTK_DELETE_PARAGRAPH_ENDS:
4835 /* If we're already at a newline, we need to
4836 * simply delete that newline, instead of
4837 * moving to the next one.
4839 if (gtk_text_iter_ends_line (&end))
4841 gtk_text_iter_forward_line (&end);
4847 if (!gtk_text_iter_forward_to_line_end (&end))
4853 /* FIXME figure out what a negative count means
4857 case GTK_DELETE_PARAGRAPHS:
4860 gtk_text_iter_set_line_offset (&start, 0);
4861 gtk_text_iter_forward_to_line_end (&end);
4863 /* Do the lines beyond the first. */
4866 gtk_text_iter_forward_to_line_end (&end);
4872 /* FIXME negative count? */
4876 case GTK_DELETE_WHITESPACE:
4878 find_whitepace_region (&insert, &start, &end);
4886 if (!gtk_text_iter_equal (&start, &end))
4888 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4890 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4891 text_view->editable))
4894 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4896 text_view->editable);
4899 gtk_text_buffer_end_user_action (get_buffer (text_view));
4901 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4902 gtk_text_view_scroll_mark_onscreen (text_view,
4903 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4908 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4910 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4911 GDK_SELECTION_CLIPBOARD);
4913 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4915 text_view->editable);
4916 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4917 gtk_text_view_scroll_mark_onscreen (text_view,
4918 gtk_text_buffer_get_mark (get_buffer (text_view),
4923 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4925 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4926 GDK_SELECTION_CLIPBOARD);
4928 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4930 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4931 gtk_text_view_scroll_mark_onscreen (text_view,
4932 gtk_text_buffer_get_mark (get_buffer (text_view),
4937 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4939 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
4940 GDK_SELECTION_CLIPBOARD);
4942 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4945 text_view->editable);
4946 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4947 gtk_text_view_scroll_mark_onscreen (text_view,
4948 gtk_text_buffer_get_mark (get_buffer (text_view),
4953 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4955 text_view->overwrite_mode = !text_view->overwrite_mode;
4959 gtk_text_view_move_focus (GtkTextView *text_view,
4960 GtkDirectionType direction_type)
4962 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
4964 if (!GTK_WIDGET_TOPLEVEL (toplevel))
4967 /* Propagate to toplevel */
4968 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus", direction_type);
4976 gtk_text_view_unselect (GtkTextView *text_view)
4980 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4982 gtk_text_buffer_get_mark (get_buffer (text_view),
4985 gtk_text_buffer_move_mark (get_buffer (text_view),
4986 gtk_text_buffer_get_mark (get_buffer (text_view),
4992 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
4993 const gchar *mark_name)
4996 GdkModifierType state;
4997 GtkTextIter newplace;
4999 /* DV(g_print (G_STRLOC": begin\n")); */
5001 gdk_window_get_pointer (text_view->text_window->bin_window,
5004 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5005 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5007 x + text_view->xoffset,
5008 y + text_view->yoffset);
5012 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5014 /* This may invalidate the layout */
5015 DV(g_print (G_STRLOC": move mark\n"));
5016 gtk_text_buffer_move_mark (get_buffer (text_view),
5020 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5021 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5024 DV (g_print ("first validate idle leaving %s is %d\n",
5025 G_STRLOC, text_view->first_validate_idle));
5029 selection_scan_timeout (gpointer data)
5031 GtkTextView *text_view;
5033 GDK_THREADS_ENTER ();
5035 text_view = GTK_TEXT_VIEW (data);
5037 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5038 move_mark_to_pointer_and_scroll (text_view, "insert");
5040 GDK_THREADS_LEAVE ();
5042 return TRUE; /* remain installed. */
5045 #define DND_SCROLL_MARGIN 0.20
5048 drag_scan_timeout (gpointer data)
5050 GtkTextView *text_view;
5052 GdkModifierType state;
5053 GtkTextIter newplace;
5055 GDK_THREADS_ENTER ();
5057 text_view = GTK_TEXT_VIEW (data);
5059 gdk_window_get_pointer (text_view->text_window->bin_window,
5062 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5064 x + text_view->xoffset,
5065 y + text_view->yoffset);
5067 gtk_text_buffer_move_mark (get_buffer (text_view),
5068 text_view->dnd_mark,
5071 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5072 gtk_text_view_scroll_to_mark (text_view,
5073 text_view->dnd_mark,
5074 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5076 GDK_THREADS_LEAVE ();
5082 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5084 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5085 move_mark_to_pointer_and_scroll (text_view, "insert");
5087 /* If we had to scroll offscreen, insert a timeout to do so
5088 * again. Note that in the timeout, even if the mouse doesn't
5089 * move, due to this scroll xoffset/yoffset will have changed
5090 * and we'll need to scroll again.
5092 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5093 gtk_timeout_remove (text_view->scroll_timeout);
5095 text_view->scroll_timeout =
5096 gtk_timeout_add (50, selection_scan_timeout, text_view);
5102 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5103 const GtkTextIter *iter,
5104 GdkEventButton *button)
5106 GtkTextIter newplace;
5107 GtkTextBuffer *buffer;
5109 g_return_if_fail (text_view->selection_drag_handler == 0);
5111 gtk_grab_add (GTK_WIDGET (text_view));
5113 buffer = get_buffer (text_view);
5117 if (button->state & GDK_SHIFT_MASK)
5119 /* Extend selection */
5120 GtkTextIter start, end;
5122 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5124 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5126 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5129 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5132 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5134 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5137 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5143 /* Replace selection */
5144 gtk_text_buffer_place_cursor (buffer, &newplace);
5147 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5148 "motion_notify_event",
5149 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5153 /* returns whether we were really dragging */
5155 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5157 if (text_view->selection_drag_handler == 0)
5160 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5161 text_view->selection_drag_handler = 0;
5163 if (text_view->scroll_timeout != 0)
5165 gtk_timeout_remove (text_view->scroll_timeout);
5166 text_view->scroll_timeout = 0;
5169 /* one last update to current position */
5170 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5171 move_mark_to_pointer_and_scroll (text_view, "insert");
5173 gtk_grab_remove (GTK_WIDGET (text_view));
5183 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5184 GtkTextAttributes *values,
5187 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5188 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5191 pango_font_description_free (values->font);
5193 values->font = pango_font_description_copy (style->font_desc);
5197 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5199 if (text_view->layout)
5201 gboolean split_cursor;
5202 GtkTextDirection new_dir;
5203 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5205 g_object_get (G_OBJECT (settings),
5206 "gtk-split-cursor", &split_cursor,
5210 new_dir = GTK_TEXT_DIR_NONE;
5214 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5215 new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5216 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5219 if (text_view->layout->cursor_direction != new_dir)
5220 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5225 gtk_text_view_ensure_layout (GtkTextView *text_view)
5229 widget = GTK_WIDGET (text_view);
5231 if (text_view->layout == NULL)
5233 GtkTextAttributes *style;
5234 PangoContext *ltr_context, *rtl_context;
5237 DV(g_print(G_STRLOC"\n"));
5239 text_view->layout = gtk_text_layout_new ();
5241 g_signal_connect (G_OBJECT (text_view->layout),
5243 G_CALLBACK (invalidated_handler),
5246 g_signal_connect (G_OBJECT (text_view->layout),
5248 G_CALLBACK (changed_handler),
5251 g_signal_connect (G_OBJECT (text_view->layout),
5253 G_CALLBACK (gtk_text_view_child_allocated),
5256 if (get_buffer (text_view))
5257 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5259 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5260 gtk_text_view_pend_cursor_blink (text_view);
5262 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5264 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5265 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5266 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5267 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5269 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5271 g_object_unref (G_OBJECT (ltr_context));
5272 g_object_unref (G_OBJECT (rtl_context));
5274 gtk_text_view_check_keymap_direction (text_view);
5276 style = gtk_text_attributes_new ();
5278 gtk_widget_ensure_style (widget);
5279 gtk_text_view_set_attributes_from_style (text_view,
5280 style, widget->style);
5282 style->pixels_above_lines = text_view->pixels_above_lines;
5283 style->pixels_below_lines = text_view->pixels_below_lines;
5284 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5285 style->left_margin = text_view->left_margin;
5286 style->right_margin = text_view->right_margin;
5287 style->indent = text_view->indent;
5288 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5290 style->wrap_mode = text_view->wrap_mode;
5291 style->justification = text_view->justify;
5292 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5294 gtk_text_layout_set_default_style (text_view->layout, style);
5296 gtk_text_attributes_unref (style);
5298 /* Set layout for all anchored children */
5300 tmp_list = text_view->children;
5301 while (tmp_list != NULL)
5303 GtkTextViewChild *vc = tmp_list->data;
5307 gtk_text_anchored_child_set_layout (vc->widget,
5309 /* vc may now be invalid! */
5312 tmp_list = g_slist_next (tmp_list);
5315 gtk_text_view_invalidate (text_view);
5320 * gtk_text_view_get_default_attributes:
5321 * @text_view: a #GtkTextView
5323 * Obtains a copy of the default text attributes. These are the
5324 * attributes used for text unless a tag overrides them.
5325 * You'd typically pass the default attributes in to
5326 * gtk_text_iter_get_attributes() in order to get the
5327 * attributes in effect at a given text position.
5329 * The return value is a copy owned by the caller of this function,
5330 * and should be freed.
5332 * Return value: a new #GtkTextAttributes
5335 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5337 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5339 gtk_text_view_ensure_layout (text_view);
5341 return gtk_text_attributes_copy (text_view->layout->default_style);
5345 gtk_text_view_destroy_layout (GtkTextView *text_view)
5347 if (text_view->layout)
5351 gtk_text_view_remove_validate_idles (text_view);
5353 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5354 (gpointer) invalidated_handler,
5356 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5357 (gpointer) changed_handler,
5360 /* Remove layout from all anchored children */
5361 tmp_list = text_view->children;
5362 while (tmp_list != NULL)
5364 GtkTextViewChild *vc = tmp_list->data;
5368 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5369 /* vc may now be invalid! */
5372 tmp_list = g_slist_next (tmp_list);
5375 gtk_text_view_stop_cursor_blink (text_view);
5376 gtk_text_view_end_selection_drag (text_view, NULL);
5378 g_object_unref (G_OBJECT (text_view->layout));
5379 text_view->layout = NULL;
5384 gtk_text_view_reset_im_context (GtkTextView *text_view)
5386 if (text_view->need_im_reset)
5388 text_view->need_im_reset = FALSE;
5389 gtk_im_context_reset (text_view->im_context);
5398 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5399 const GtkTextIter *iter,
5400 GdkEventMotion *event)
5402 GdkDragContext *context;
5403 GtkTargetList *target_list;
5405 text_view->drag_start_x = -1;
5406 text_view->drag_start_y = -1;
5407 text_view->pending_place_cursor_button = 0;
5409 target_list = gtk_target_list_new (target_table,
5410 G_N_ELEMENTS (target_table));
5412 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5413 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5414 1, (GdkEvent*)event);
5416 gtk_target_list_unref (target_list);
5418 gtk_drag_set_icon_default (context);
5422 gtk_text_view_drag_begin (GtkWidget *widget,
5423 GdkDragContext *context)
5429 gtk_text_view_drag_end (GtkWidget *widget,
5430 GdkDragContext *context)
5432 GtkTextView *text_view;
5434 text_view = GTK_TEXT_VIEW (widget);
5438 gtk_text_view_drag_data_get (GtkWidget *widget,
5439 GdkDragContext *context,
5440 GtkSelectionData *selection_data,
5444 GtkTextView *text_view;
5446 text_view = GTK_TEXT_VIEW (widget);
5448 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5450 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5452 gtk_selection_data_set (selection_data,
5453 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5466 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5469 /* Extract the selected text */
5470 str = gtk_text_iter_get_visible_text (&start, &end);
5475 gtk_selection_data_set_text (selection_data, str, -1);
5482 gtk_text_view_drag_data_delete (GtkWidget *widget,
5483 GdkDragContext *context)
5485 GtkTextView *text_view;
5487 text_view = GTK_TEXT_VIEW (widget);
5489 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5490 TRUE, GTK_TEXT_VIEW (widget)->editable);
5494 gtk_text_view_drag_leave (GtkWidget *widget,
5495 GdkDragContext *context,
5498 GtkTextView *text_view;
5500 text_view = GTK_TEXT_VIEW (widget);
5502 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5504 if (text_view->scroll_timeout != 0)
5505 gtk_timeout_remove (text_view->scroll_timeout);
5507 text_view->scroll_timeout = 0;
5511 gtk_text_view_drag_motion (GtkWidget *widget,
5512 GdkDragContext *context,
5517 GtkTextIter newplace;
5518 GtkTextView *text_view;
5521 GdkRectangle target_rect;
5523 GdkDragAction suggested_action = 0;
5525 text_view = GTK_TEXT_VIEW (widget);
5527 target_rect = text_view->text_window->allocation;
5529 if (x < target_rect.x ||
5530 y < target_rect.y ||
5531 x > (target_rect.x + target_rect.width) ||
5532 y > (target_rect.y + target_rect.height))
5533 return FALSE; /* outside the text window, allow parent widgets to handle event */
5535 gtk_text_view_window_to_buffer_coords (text_view,
5536 GTK_TEXT_WINDOW_WIDGET,
5540 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5544 if (gtk_drag_dest_find_target (widget, context,
5545 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5547 /* can't accept any of the offered targets */
5549 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5551 gtk_text_iter_in_range (&newplace, &start, &end))
5553 /* We're inside the selection. */
5557 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5559 GtkWidget *source_widget;
5561 suggested_action = context->suggested_action;
5563 source_widget = gtk_drag_get_source_widget (context);
5565 if (source_widget == widget)
5567 /* Default to MOVE, unless the user has
5568 * pressed ctrl or alt to affect available actions
5570 if ((context->actions & GDK_ACTION_MOVE) != 0)
5571 suggested_action = GDK_ACTION_MOVE;
5576 /* Can't drop here. */
5580 if (suggested_action != 0)
5582 gtk_text_mark_set_visible (text_view->dnd_mark,
5583 text_view->cursor_visible);
5585 gdk_drag_status (context, suggested_action, time);
5589 gdk_drag_status (context, 0, time);
5590 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5593 gtk_text_buffer_move_mark (get_buffer (text_view),
5594 text_view->dnd_mark,
5597 DV(g_print (G_STRLOC": scrolling to mark\n"));
5598 gtk_text_view_scroll_to_mark (text_view,
5599 text_view->dnd_mark,
5600 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5602 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5603 gtk_timeout_remove (text_view->scroll_timeout);
5605 text_view->scroll_timeout =
5606 gtk_timeout_add (50, drag_scan_timeout, text_view);
5608 /* TRUE return means don't propagate the drag motion to parent
5609 * widgets that may also be drop sites.
5615 gtk_text_view_drag_drop (GtkWidget *widget,
5616 GdkDragContext *context,
5621 GtkTextView *text_view;
5622 GtkTextIter drop_point;
5623 GdkAtom target = GDK_NONE;
5625 text_view = GTK_TEXT_VIEW (widget);
5627 if (text_view->scroll_timeout != 0)
5628 gtk_timeout_remove (text_view->scroll_timeout);
5630 text_view->scroll_timeout = 0;
5632 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5634 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5636 text_view->dnd_mark);
5638 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5639 target = gtk_drag_dest_find_target (widget, context, NULL);
5641 if (target != GDK_NONE)
5642 gtk_drag_get_data (widget, context, target, time);
5644 gtk_drag_finish (context, FALSE, FALSE, time);
5650 insert_text_data (GtkTextView *text_view,
5651 GtkTextIter *drop_point,
5652 GtkSelectionData *selection_data)
5656 str = gtk_selection_data_get_text (selection_data);
5660 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5661 drop_point, str, -1,
5662 text_view->editable);
5668 gtk_text_view_drag_data_received (GtkWidget *widget,
5669 GdkDragContext *context,
5672 GtkSelectionData *selection_data,
5676 GtkTextIter drop_point;
5677 GtkTextView *text_view;
5678 gboolean success = FALSE;
5680 text_view = GTK_TEXT_VIEW (widget);
5682 if (!text_view->dnd_mark)
5685 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5687 text_view->dnd_mark);
5689 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5692 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5694 GtkTextBuffer *src_buffer = NULL;
5695 GtkTextIter start, end;
5696 gboolean copy_tags = TRUE;
5698 if (selection_data->length != sizeof (src_buffer))
5701 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5703 if (src_buffer == NULL)
5706 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5708 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5709 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5712 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5717 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5721 text_view->editable);
5726 str = gtk_text_iter_get_visible_text (&start, &end);
5727 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5728 &drop_point, str, -1,
5729 text_view->editable);
5735 insert_text_data (text_view, &drop_point, selection_data);
5740 gtk_drag_finish (context, success,
5741 success && context->action == GDK_ACTION_MOVE,
5745 static GtkAdjustment*
5746 get_hadjustment (GtkTextView *text_view)
5748 if (text_view->hadjustment == NULL)
5749 gtk_text_view_set_scroll_adjustments (text_view,
5750 NULL, /* forces creation */
5751 text_view->vadjustment);
5753 return text_view->hadjustment;
5756 static GtkAdjustment*
5757 get_vadjustment (GtkTextView *text_view)
5759 if (text_view->vadjustment == NULL)
5760 gtk_text_view_set_scroll_adjustments (text_view,
5761 text_view->hadjustment,
5762 NULL); /* forces creation */
5763 return text_view->vadjustment;
5768 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5769 GtkAdjustment *hadj,
5770 GtkAdjustment *vadj)
5772 gboolean need_adjust = FALSE;
5774 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5777 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5779 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5781 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5783 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5785 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5787 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5788 g_object_unref (G_OBJECT (text_view->hadjustment));
5791 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5793 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5794 g_object_unref (G_OBJECT (text_view->vadjustment));
5797 if (text_view->hadjustment != hadj)
5799 text_view->hadjustment = hadj;
5800 g_object_ref (G_OBJECT (text_view->hadjustment));
5801 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5803 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5804 (GtkSignalFunc) gtk_text_view_value_changed,
5809 if (text_view->vadjustment != vadj)
5811 text_view->vadjustment = vadj;
5812 g_object_ref (G_OBJECT (text_view->vadjustment));
5813 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5815 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5816 (GtkSignalFunc) gtk_text_view_value_changed,
5822 gtk_text_view_value_changed (NULL, text_view);
5825 /* FIXME this adjust_allocation is a big cut-and-paste from
5826 * GtkCList, needs to be some "official" way to do this
5836 /* The window to which widget->window is relative */
5837 #define ALLOCATION_WINDOW(widget) \
5838 (GTK_WIDGET_NO_WINDOW (widget) ? \
5839 (widget)->window : \
5840 gdk_window_get_parent ((widget)->window))
5843 adjust_allocation_recurse (GtkWidget *widget,
5846 ScrollData *scroll_data = data;
5848 /* Need to really size allocate instead of just poking
5849 * into widget->allocation if the widget is not realized.
5850 * FIXME someone figure out why this was.
5852 if (!GTK_WIDGET_REALIZED (widget))
5854 if (GTK_WIDGET_VISIBLE (widget))
5856 GdkRectangle tmp_rectangle = widget->allocation;
5857 tmp_rectangle.x += scroll_data->dx;
5858 tmp_rectangle.y += scroll_data->dy;
5860 gtk_widget_size_allocate (widget, &tmp_rectangle);
5865 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5867 widget->allocation.x += scroll_data->dx;
5868 widget->allocation.y += scroll_data->dy;
5870 if (GTK_IS_CONTAINER (widget))
5871 gtk_container_forall (GTK_CONTAINER (widget),
5872 adjust_allocation_recurse,
5879 adjust_allocation (GtkWidget *widget,
5883 ScrollData scroll_data;
5885 if (GTK_WIDGET_REALIZED (widget))
5886 scroll_data.window = ALLOCATION_WINDOW (widget);
5888 scroll_data.window = NULL;
5890 scroll_data.dx = dx;
5891 scroll_data.dy = dy;
5893 adjust_allocation_recurse (widget, &scroll_data);
5897 gtk_text_view_value_changed (GtkAdjustment *adj,
5898 GtkTextView *text_view)
5905 /* Note that we oddly call this function with adj == NULL
5909 text_view->onscreen_validated = FALSE;
5911 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5912 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5913 adj ? adj->value : 0.0));
5915 if (adj == text_view->hadjustment)
5917 dx = text_view->xoffset - (gint)adj->value;
5918 text_view->xoffset = adj->value;
5920 else if (adj == text_view->vadjustment)
5922 dy = text_view->yoffset - (gint)adj->value;
5923 text_view->yoffset = adj->value;
5925 if (text_view->layout)
5927 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5929 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5931 text_view->first_para_pixels = adj->value - line_top;
5935 if (dx != 0 || dy != 0)
5939 if (GTK_WIDGET_REALIZED (text_view))
5943 if (text_view->left_window)
5944 text_window_scroll (text_view->left_window, 0, dy);
5945 if (text_view->right_window)
5946 text_window_scroll (text_view->right_window, 0, dy);
5951 if (text_view->top_window)
5952 text_window_scroll (text_view->top_window, dx, 0);
5953 if (text_view->bottom_window)
5954 text_window_scroll (text_view->bottom_window, dx, 0);
5957 /* It looks nicer to scroll the main area last, because
5958 * it takes a while, and making the side areas update
5959 * afterward emphasizes the slowness of scrolling the
5962 text_window_scroll (text_view->text_window, dx, dy);
5965 /* Children are now "moved" in the text window, poke
5966 * into widget->allocation for each child
5968 tmp_list = text_view->children;
5969 while (tmp_list != NULL)
5971 GtkTextViewChild *child = tmp_list->data;
5974 adjust_allocation (child->widget, dx, dy);
5976 tmp_list = g_slist_next (tmp_list);
5980 /* This could result in invalidation, which would install the
5981 * first_validate_idle, which would validate onscreen;
5982 * but we're going to go ahead and validate here, so
5983 * first_validate_idle shouldn't have anything to do.
5985 gtk_text_view_update_layout_width (text_view);
5987 /* note that validation of onscreen could invoke this function
5988 * recursively, by scrolling to maintain first_para, or in response
5989 * to updating the layout width, however there is no problem with
5990 * that, or shouldn't be.
5992 gtk_text_view_validate_onscreen (text_view);
5994 /* process exposes */
5995 if (GTK_WIDGET_REALIZED (text_view))
5997 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
5999 if (text_view->left_window)
6000 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6002 if (text_view->right_window)
6003 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6005 if (text_view->top_window)
6006 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6008 if (text_view->bottom_window)
6009 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6011 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6014 /* If this got installed, get rid of it, it's just a waste of time. */
6015 if (text_view->first_validate_idle != 0)
6017 g_source_remove (text_view->first_validate_idle);
6018 text_view->first_validate_idle = 0;
6021 gtk_text_view_update_im_spot_location (text_view);
6023 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6027 gtk_text_view_commit_handler (GtkIMContext *context,
6029 GtkTextView *text_view)
6031 gtk_text_view_commit_text (text_view, str);
6035 gtk_text_view_commit_text (GtkTextView *text_view,
6038 gboolean had_selection;
6040 gtk_text_buffer_begin_user_action (get_buffer (text_view));
6042 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6045 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6046 text_view->editable);
6048 if (!strcmp (str, "\n"))
6050 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6051 text_view->editable);
6055 if (!had_selection && text_view->overwrite_mode)
6059 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6061 gtk_text_buffer_get_mark (get_buffer (text_view),
6063 if (!gtk_text_iter_ends_line (&insert))
6064 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6066 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6067 text_view->editable);
6070 gtk_text_buffer_end_user_action (get_buffer (text_view));
6072 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6073 gtk_text_view_scroll_mark_onscreen (text_view,
6074 gtk_text_buffer_get_mark (get_buffer (text_view),
6079 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6080 GtkTextView *text_view)
6083 PangoAttrList *attrs;
6086 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6087 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6089 pango_attr_list_unref (attrs);
6094 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6095 GtkTextView *text_view)
6102 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6103 gtk_text_buffer_get_insert (text_view->buffer));
6106 pos = gtk_text_iter_get_line_index (&start);
6107 gtk_text_iter_set_line_offset (&start, 0);
6108 gtk_text_iter_forward_to_line_end (&end);
6110 text = gtk_text_iter_get_slice (&start, &end);
6111 gtk_im_context_set_surrounding (context, text, -1, pos);
6118 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6121 GtkTextView *text_view)
6126 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6127 gtk_text_buffer_get_insert (text_view->buffer));
6130 gtk_text_iter_forward_chars (&start, offset);
6131 gtk_text_iter_forward_chars (&end, offset + n_chars);
6133 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6139 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6140 const GtkTextIter *location,
6144 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6145 gboolean need_reset = FALSE;
6147 if (mark == gtk_text_buffer_get_insert (buffer))
6149 text_view->virtual_cursor_x = -1;
6150 text_view->virtual_cursor_y = -1;
6151 gtk_text_view_update_im_spot_location (text_view);
6154 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6160 gtk_text_view_reset_im_context (text_view);
6164 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6168 GdkRectangle strong_pos;
6171 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6172 gtk_text_buffer_get_mark (get_buffer (text_view),
6175 if ((x && text_view->virtual_cursor_x == -1) ||
6176 (y && text_view->virtual_cursor_y == -1))
6177 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6181 if (text_view->virtual_cursor_x != -1)
6182 *x = text_view->virtual_cursor_x;
6189 if (text_view->virtual_cursor_x != -1)
6190 *y = text_view->virtual_cursor_y;
6192 *y = strong_pos.y + strong_pos.height / 2;
6197 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6201 GdkRectangle strong_pos;
6204 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6205 gtk_text_buffer_get_mark (get_buffer (text_view),
6208 if (x == -1 || y == -1)
6209 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6211 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6212 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6215 /* Quick hack of a popup menu
6218 activate_cb (GtkWidget *menuitem,
6219 GtkTextView *text_view)
6221 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6222 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6226 append_action_signal (GtkTextView *text_view,
6228 const gchar *stock_id,
6229 const gchar *signal,
6232 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6234 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6235 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6236 GTK_SIGNAL_FUNC (activate_cb), text_view);
6238 gtk_widget_set_sensitive (menuitem, sensitive);
6240 gtk_widget_show (menuitem);
6241 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6245 popup_menu_detach (GtkWidget *attach_widget,
6248 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6252 popup_position_func (GtkMenu *menu,
6258 GtkTextView *text_view;
6260 GdkRectangle cursor_rect;
6261 GdkRectangle onscreen_rect;
6262 gint root_x, root_y;
6267 text_view = GTK_TEXT_VIEW (user_data);
6268 widget = GTK_WIDGET (text_view);
6270 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6272 screen = gtk_widget_get_screen (widget);
6274 gdk_window_get_origin (widget->window, &root_x, &root_y);
6276 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6278 gtk_text_buffer_get_insert (get_buffer (text_view)));
6280 gtk_text_view_get_iter_location (text_view,
6284 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6286 gtk_widget_size_request (text_view->popup_menu, &req);
6288 /* can't use rectangle_intersect since cursor rect can have 0 width */
6289 if (cursor_rect.x >= onscreen_rect.x &&
6290 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6291 cursor_rect.y >= onscreen_rect.y &&
6292 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6294 gtk_text_view_buffer_to_window_coords (text_view,
6295 GTK_TEXT_WINDOW_WIDGET,
6296 cursor_rect.x, cursor_rect.y,
6297 &cursor_rect.x, &cursor_rect.y);
6299 *x = root_x + cursor_rect.x + cursor_rect.width;
6300 *y = root_y + cursor_rect.y + cursor_rect.height;
6304 /* Just center the menu, since cursor is offscreen. */
6305 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6306 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6310 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6311 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6313 *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6314 *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6319 GtkTextView *text_view;
6325 range_contains_editable_text (const GtkTextIter *start,
6326 const GtkTextIter *end,
6327 gboolean default_editability)
6329 GtkTextIter iter = *start;
6331 while (gtk_text_iter_compare (&iter, end) < 0)
6333 if (gtk_text_iter_editable (&iter, default_editability))
6336 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6343 unichar_chosen_func (const char *text,
6346 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6348 gtk_text_view_commit_text (text_view, text);
6352 popup_targets_received (GtkClipboard *clipboard,
6353 GtkSelectionData *data,
6356 PopupInfo *info = user_data;
6357 GtkTextView *text_view = info->text_view;
6359 if (GTK_WIDGET_REALIZED (text_view))
6361 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6362 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6364 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6365 GtkWidget *menuitem;
6367 gboolean have_selection;
6368 gboolean can_insert;
6370 GtkTextIter sel_start, sel_end;
6372 if (text_view->popup_menu)
6373 gtk_widget_destroy (text_view->popup_menu);
6375 text_view->popup_menu = gtk_menu_new ();
6377 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6378 GTK_WIDGET (text_view),
6381 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6382 &sel_start, &sel_end);
6384 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6386 gtk_text_buffer_get_insert (get_buffer (text_view)));
6388 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6390 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6392 range_contains_editable_text (&sel_start, &sel_end,
6393 text_view->editable));
6394 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6396 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6397 can_insert && clipboard_contains_text);
6399 menuitem = gtk_separator_menu_item_new ();
6400 gtk_widget_show (menuitem);
6401 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6403 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6404 gtk_widget_show (menuitem);
6405 submenu = gtk_menu_new ();
6406 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6407 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6409 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6410 GTK_MENU_SHELL (submenu));
6412 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6413 gtk_widget_show (menuitem);
6414 gtk_widget_set_sensitive (menuitem, can_insert);
6416 submenu = gtk_menu_new ();
6417 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6418 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6420 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6421 unichar_chosen_func,
6424 gtk_signal_emit (GTK_OBJECT (text_view),
6425 signals[POPULATE_POPUP],
6426 text_view->popup_menu);
6429 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6431 info->button, info->time);
6434 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6435 popup_position_func, text_view,
6436 0, gtk_get_current_event_time ());
6437 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu));
6441 g_object_unref (text_view);
6446 gtk_text_view_do_popup (GtkTextView *text_view,
6447 GdkEventButton *event)
6449 PopupInfo *info = g_new (PopupInfo, 1);
6451 /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6452 gtk_text_view_end_selection_drag (text_view, event);
6454 /* In order to know what entries we should make sensitive, we
6455 * ask for the current targets of the clipboard, and when
6456 * we get them, then we actually pop up the menu.
6458 info->text_view = g_object_ref (text_view);
6462 info->button = event->button;
6463 info->time = event->time;
6468 info->time = gtk_get_current_event_time ();
6471 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6472 GDK_SELECTION_CLIPBOARD),
6473 gdk_atom_intern ("TARGETS", FALSE),
6474 popup_targets_received,
6479 gtk_text_view_popup_menu (GtkWidget *widget)
6481 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6485 /* Child GdkWindows */
6488 static GtkTextWindow*
6489 text_window_new (GtkTextWindowType type,
6492 gint height_request)
6496 win = g_new (GtkTextWindow, 1);
6499 win->widget = widget;
6501 win->bin_window = NULL;
6502 win->requisition.width = width_request;
6503 win->requisition.height = height_request;
6504 win->allocation.width = width_request;
6505 win->allocation.height = height_request;
6506 win->allocation.x = 0;
6507 win->allocation.y = 0;
6513 text_window_free (GtkTextWindow *win)
6516 text_window_unrealize (win);
6522 text_window_realize (GtkTextWindow *win,
6525 GdkWindowAttr attributes;
6526 gint attributes_mask;
6529 attributes.window_type = GDK_WINDOW_CHILD;
6530 attributes.x = win->allocation.x;
6531 attributes.y = win->allocation.y;
6532 attributes.width = win->allocation.width;
6533 attributes.height = win->allocation.height;
6534 attributes.wclass = GDK_INPUT_OUTPUT;
6535 attributes.visual = gtk_widget_get_visual (win->widget);
6536 attributes.colormap = gtk_widget_get_colormap (win->widget);
6537 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6539 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6541 win->window = gdk_window_new (parent,
6545 gdk_window_show (win->window);
6546 gdk_window_set_user_data (win->window, win->widget);
6550 attributes.width = win->allocation.width;
6551 attributes.height = win->allocation.height;
6552 attributes.event_mask = (GDK_EXPOSURE_MASK |
6554 GDK_KEY_PRESS_MASK |
6555 GDK_BUTTON_PRESS_MASK |
6556 GDK_BUTTON_RELEASE_MASK |
6557 GDK_POINTER_MOTION_MASK |
6558 GDK_POINTER_MOTION_HINT_MASK |
6559 gtk_widget_get_events (win->widget));
6561 win->bin_window = gdk_window_new (win->window,
6565 gdk_window_show (win->bin_window);
6566 gdk_window_set_user_data (win->bin_window, win->widget);
6568 if (win->type == GTK_TEXT_WINDOW_TEXT)
6571 cursor = gdk_cursor_new_for_screen (gdk_drawable_get_screen (parent),
6573 gdk_window_set_cursor (win->bin_window, cursor);
6574 gdk_cursor_unref (cursor);
6576 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6580 gdk_window_set_background (win->bin_window,
6581 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6585 gdk_window_set_background (win->bin_window,
6586 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6589 g_object_set_qdata (G_OBJECT (win->window),
6590 g_quark_from_static_string ("gtk-text-view-text-window"),
6593 g_object_set_qdata (G_OBJECT (win->bin_window),
6594 g_quark_from_static_string ("gtk-text-view-text-window"),
6599 text_window_unrealize (GtkTextWindow *win)
6601 if (win->type == GTK_TEXT_WINDOW_TEXT)
6603 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6607 gdk_window_set_user_data (win->window, NULL);
6608 gdk_window_set_user_data (win->bin_window, NULL);
6609 gdk_window_destroy (win->bin_window);
6610 gdk_window_destroy (win->window);
6612 win->bin_window = NULL;
6616 text_window_size_allocate (GtkTextWindow *win,
6619 win->allocation = *rect;
6623 gdk_window_move_resize (win->window,
6625 rect->width, rect->height);
6627 gdk_window_resize (win->bin_window,
6628 rect->width, rect->height);
6633 text_window_scroll (GtkTextWindow *win,
6637 if (dx != 0 || dy != 0)
6639 gdk_window_scroll (win->bin_window, dx, dy);
6644 text_window_invalidate_rect (GtkTextWindow *win,
6647 GdkRectangle window_rect;
6649 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6656 window_rect.width = rect->width;
6657 window_rect.height = rect->height;
6659 /* Adjust the rect as appropriate */
6663 case GTK_TEXT_WINDOW_TEXT:
6666 case GTK_TEXT_WINDOW_LEFT:
6667 case GTK_TEXT_WINDOW_RIGHT:
6669 window_rect.width = win->allocation.width;
6672 case GTK_TEXT_WINDOW_TOP:
6673 case GTK_TEXT_WINDOW_BOTTOM:
6675 window_rect.height = win->allocation.height;
6679 g_warning ("%s: bug!", G_STRLOC);
6684 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6688 GdkColor color = { 0, 65535, 0, 0 };
6689 GdkGC *gc = gdk_gc_new (win->bin_window);
6690 gdk_gc_set_rgb_fg_color (gc, &color);
6691 gdk_draw_rectangle (win->bin_window,
6692 gc, TRUE, window_rect.x, window_rect.y,
6693 window_rect.width, window_rect.height);
6700 text_window_get_width (GtkTextWindow *win)
6702 return win->allocation.width;
6706 text_window_get_height (GtkTextWindow *win)
6708 return win->allocation.height;
6712 text_window_get_allocation (GtkTextWindow *win,
6715 *rect = win->allocation;
6722 * gtk_text_view_get_window:
6723 * @text_view: a #GtkTextView
6724 * @win: window to get
6726 * Retrieves the #GdkWindow corresponding to an area of the text view;
6727 * possible windows include the overall widget window, child windows
6728 * on the left, right, top, bottom, and the window that displays the
6729 * text buffer. Windows are %NULL and nonexistent if their width or
6730 * height is 0, and are nonexistent before the widget has been
6733 * Return value: a #GdkWindow, or %NULL
6736 gtk_text_view_get_window (GtkTextView *text_view,
6737 GtkTextWindowType win)
6739 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6743 case GTK_TEXT_WINDOW_WIDGET:
6744 return GTK_WIDGET (text_view)->window;
6747 case GTK_TEXT_WINDOW_TEXT:
6748 return text_view->text_window->bin_window;
6751 case GTK_TEXT_WINDOW_LEFT:
6752 if (text_view->left_window)
6753 return text_view->left_window->bin_window;
6758 case GTK_TEXT_WINDOW_RIGHT:
6759 if (text_view->right_window)
6760 return text_view->right_window->bin_window;
6765 case GTK_TEXT_WINDOW_TOP:
6766 if (text_view->top_window)
6767 return text_view->top_window->bin_window;
6772 case GTK_TEXT_WINDOW_BOTTOM:
6773 if (text_view->bottom_window)
6774 return text_view->bottom_window->bin_window;
6779 case GTK_TEXT_WINDOW_PRIVATE:
6780 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6785 g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6790 * gtk_text_view_get_window_type:
6791 * @text_view: a #GtkTextView
6792 * @window: a window type
6794 * Usually used to find out which window an event corresponds to.
6795 * If you connect to an event signal on @text_view, this function
6796 * should be called on <literal>event->window</literal> to
6797 * see which window it was.
6799 * Return value: the window type.
6802 gtk_text_view_get_window_type (GtkTextView *text_view,
6807 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6808 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6810 if (window == GTK_WIDGET (text_view)->window)
6811 return GTK_TEXT_WINDOW_WIDGET;
6813 win = g_object_get_qdata (G_OBJECT (window),
6814 g_quark_try_string ("gtk-text-view-text-window"));
6820 return GTK_TEXT_WINDOW_PRIVATE;
6825 buffer_to_widget (GtkTextView *text_view,
6833 *window_x = buffer_x - text_view->xoffset;
6834 *window_x += text_view->text_window->allocation.x;
6839 *window_y = buffer_y - text_view->yoffset;
6840 *window_y += text_view->text_window->allocation.y;
6845 widget_to_text_window (GtkTextWindow *win,
6852 *window_x = widget_x - win->allocation.x;
6855 *window_y = widget_y - win->allocation.y;
6859 buffer_to_text_window (GtkTextView *text_view,
6868 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6869 "for a nonexistent or private child window of GtkTextView");
6873 buffer_to_widget (text_view,
6875 window_x, window_y);
6877 widget_to_text_window (win,
6878 window_x ? *window_x : 0,
6879 window_y ? *window_y : 0,
6885 * gtk_text_view_buffer_to_window_coords:
6886 * @text_view: a #GtkTextView
6887 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6888 * @buffer_x: buffer x coordinate
6889 * @buffer_y: buffer y coordinate
6890 * @window_x: window x coordinate return location
6891 * @window_y: window y coordinate return location
6893 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6894 * @win, and stores the result in (@window_x, @window_y).
6896 * Note that you can't convert coordinates for a nonexisting window (see
6897 * gtk_text_view_set_border_window_size()).
6900 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6901 GtkTextWindowType win,
6907 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6911 case GTK_TEXT_WINDOW_WIDGET:
6912 buffer_to_widget (text_view,
6914 window_x, window_y);
6917 case GTK_TEXT_WINDOW_TEXT:
6919 *window_x = buffer_x - text_view->xoffset;
6921 *window_y = buffer_y - text_view->yoffset;
6924 case GTK_TEXT_WINDOW_LEFT:
6925 buffer_to_text_window (text_view,
6926 text_view->left_window,
6928 window_x, window_y);
6931 case GTK_TEXT_WINDOW_RIGHT:
6932 buffer_to_text_window (text_view,
6933 text_view->right_window,
6935 window_x, window_y);
6938 case GTK_TEXT_WINDOW_TOP:
6939 buffer_to_text_window (text_view,
6940 text_view->top_window,
6942 window_x, window_y);
6945 case GTK_TEXT_WINDOW_BOTTOM:
6946 buffer_to_text_window (text_view,
6947 text_view->bottom_window,
6949 window_x, window_y);
6952 case GTK_TEXT_WINDOW_PRIVATE:
6953 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6957 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6963 widget_to_buffer (GtkTextView *text_view,
6971 *buffer_x = widget_x + text_view->xoffset;
6972 *buffer_x -= text_view->text_window->allocation.x;
6977 *buffer_y = widget_y + text_view->yoffset;
6978 *buffer_y -= text_view->text_window->allocation.y;
6983 text_window_to_widget (GtkTextWindow *win,
6990 *widget_x = window_x + win->allocation.x;
6993 *widget_y = window_y + win->allocation.y;
6997 text_window_to_buffer (GtkTextView *text_view,
7006 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7007 "coordinates for a nonexistent child window.");
7011 text_window_to_widget (win,
7017 widget_to_buffer (text_view,
7018 buffer_x ? *buffer_x : 0,
7019 buffer_y ? *buffer_y : 0,
7025 * gtk_text_view_window_to_buffer_coords:
7026 * @text_view: a #GtkTextView
7027 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7028 * @window_x: window x coordinate
7029 * @window_y: window y coordinate
7030 * @buffer_x: buffer x coordinate return location
7031 * @buffer_y: buffer y coordinate return location
7033 * Converts coordinates on the window identified by @win to buffer
7034 * coordinates, storing the result in (@buffer_x,@buffer_y).
7036 * Note that you can't convert coordinates for a nonexisting window (see
7037 * gtk_text_view_set_border_window_size()).
7040 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7041 GtkTextWindowType win,
7047 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7051 case GTK_TEXT_WINDOW_WIDGET:
7052 widget_to_buffer (text_view,
7054 buffer_x, buffer_y);
7057 case GTK_TEXT_WINDOW_TEXT:
7059 *buffer_x = window_x + text_view->xoffset;
7061 *buffer_y = window_y + text_view->yoffset;
7064 case GTK_TEXT_WINDOW_LEFT:
7065 text_window_to_buffer (text_view,
7066 text_view->left_window,
7068 buffer_x, buffer_y);
7071 case GTK_TEXT_WINDOW_RIGHT:
7072 text_window_to_buffer (text_view,
7073 text_view->right_window,
7075 buffer_x, buffer_y);
7078 case GTK_TEXT_WINDOW_TOP:
7079 text_window_to_buffer (text_view,
7080 text_view->top_window,
7082 buffer_x, buffer_y);
7085 case GTK_TEXT_WINDOW_BOTTOM:
7086 text_window_to_buffer (text_view,
7087 text_view->bottom_window,
7089 buffer_x, buffer_y);
7092 case GTK_TEXT_WINDOW_PRIVATE:
7093 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7097 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7103 set_window_width (GtkTextView *text_view,
7105 GtkTextWindowType type,
7106 GtkTextWindow **winp)
7112 text_window_free (*winp);
7114 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7121 *winp = text_window_new (type,
7122 GTK_WIDGET (text_view),
7124 /* if the widget is already realized we need to realize the child manually */
7125 if (GTK_WIDGET_REALIZED (text_view))
7126 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7130 if ((*winp)->requisition.width == width)
7133 (*winp)->requisition.width = width;
7136 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7142 set_window_height (GtkTextView *text_view,
7144 GtkTextWindowType type,
7145 GtkTextWindow **winp)
7151 text_window_free (*winp);
7153 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7160 *winp = text_window_new (type,
7161 GTK_WIDGET (text_view),
7164 /* if the widget is already realized we need to realize the child manually */
7165 if (GTK_WIDGET_REALIZED (text_view))
7166 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7170 if ((*winp)->requisition.height == height)
7173 (*winp)->requisition.height = height;
7176 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7181 * gtk_text_view_set_border_window_size:
7182 * @text_view: a #GtkTextView
7183 * @type: window to affect
7184 * @size: width or height of the window
7186 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7187 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7188 * Automatically destroys the corresponding window if the size is set
7189 * to 0, and creates the window if the size is set to non-zero. This
7190 * function can only be used for the "border windows," it doesn't work
7191 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7192 * #GTK_TEXT_WINDOW_PRIVATE.
7195 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7196 GtkTextWindowType type,
7200 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7201 g_return_if_fail (size >= 0);
7205 case GTK_TEXT_WINDOW_LEFT:
7206 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7207 &text_view->left_window);
7210 case GTK_TEXT_WINDOW_RIGHT:
7211 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7212 &text_view->right_window);
7215 case GTK_TEXT_WINDOW_TOP:
7216 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7217 &text_view->top_window);
7220 case GTK_TEXT_WINDOW_BOTTOM:
7221 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7222 &text_view->bottom_window);
7226 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7232 * gtk_text_view_get_border_window_size:
7233 * @text_view: a #GtkTextView
7234 * @type: window to return size from
7236 * Gets the width of the specified border window. See
7237 * gtk_text_view_set_border_window_size().
7239 * Return value: width of window
7242 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7243 GtkTextWindowType type)
7245 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7249 case GTK_TEXT_WINDOW_LEFT:
7250 if (text_view->left_window)
7251 return text_view->left_window->requisition.width;
7253 case GTK_TEXT_WINDOW_RIGHT:
7254 if (text_view->right_window)
7255 return text_view->right_window->requisition.width;
7257 case GTK_TEXT_WINDOW_TOP:
7258 if (text_view->top_window)
7259 return text_view->top_window->requisition.height;
7261 case GTK_TEXT_WINDOW_BOTTOM:
7262 if (text_view->bottom_window)
7263 return text_view->bottom_window->requisition.height;
7266 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7277 static GtkTextViewChild*
7278 text_view_child_new_anchored (GtkWidget *child,
7279 GtkTextChildAnchor *anchor,
7280 GtkTextLayout *layout)
7282 GtkTextViewChild *vc;
7284 vc = g_new (GtkTextViewChild, 1);
7287 vc->anchor = anchor;
7289 vc->from_top_of_line = 0;
7290 vc->from_left_of_buffer = 0;
7292 g_object_ref (G_OBJECT (vc->widget));
7293 g_object_ref (G_OBJECT (vc->anchor));
7295 g_object_set_data (G_OBJECT (child),
7296 "gtk-text-view-child",
7299 gtk_text_child_anchor_register_child (anchor, child, layout);
7304 static GtkTextViewChild*
7305 text_view_child_new_window (GtkWidget *child,
7306 GtkTextWindowType type,
7310 GtkTextViewChild *vc;
7312 vc = g_new (GtkTextViewChild, 1);
7317 vc->from_top_of_line = 0;
7318 vc->from_left_of_buffer = 0;
7320 g_object_ref (G_OBJECT (vc->widget));
7326 g_object_set_data (G_OBJECT (child),
7327 "gtk-text-view-child",
7334 text_view_child_free (GtkTextViewChild *child)
7336 g_object_set_data (G_OBJECT (child->widget),
7337 "gtk-text-view-child", NULL);
7341 gtk_text_child_anchor_unregister_child (child->anchor,
7343 g_object_unref (G_OBJECT (child->anchor));
7346 g_object_unref (G_OBJECT (child->widget));
7352 text_view_child_set_parent_window (GtkTextView *text_view,
7353 GtkTextViewChild *vc)
7356 gtk_widget_set_parent_window (vc->widget,
7357 text_view->text_window->bin_window);
7361 window = gtk_text_view_get_window (text_view,
7363 gtk_widget_set_parent_window (vc->widget, window);
7368 add_child (GtkTextView *text_view,
7369 GtkTextViewChild *vc)
7371 text_view->children = g_slist_prepend (text_view->children,
7374 if (GTK_WIDGET_REALIZED (text_view))
7375 text_view_child_set_parent_window (text_view, vc);
7377 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7381 * gtk_text_view_add_child_at_anchor:
7382 * @text_view: a #GtkTextView
7383 * @child: a #GtkWidget
7384 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7386 * Adds a child widget in the text buffer, at the given @anchor.
7390 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7392 GtkTextChildAnchor *anchor)
7394 GtkTextViewChild *vc;
7396 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7397 g_return_if_fail (GTK_IS_WIDGET (child));
7398 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7399 g_return_if_fail (child->parent == NULL);
7401 gtk_text_view_ensure_layout (text_view);
7403 vc = text_view_child_new_anchored (child, anchor,
7406 add_child (text_view, vc);
7408 g_assert (vc->widget == child);
7409 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7413 * gtk_text_view_add_child_in_window:
7414 * @text_view: a #GtkTextView
7415 * @child: a #GtkWidget
7416 * @which_window: which window the child should appear in
7417 * @xpos: X position of child in window coordinates
7418 * @ypos: Y position of child in window coordinates
7420 * Adds a child at fixed coordinates in one of the text widget's
7421 * windows. The window must have nonzero size (see
7422 * gtk_text_view_set_border_window_size()). Note that the child
7423 * coordinates are given relative to the #GdkWindow in question, and
7424 * that these coordinates have no sane relationship to scrolling. When
7425 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7426 * irrelevant, the child floats above all scrollable areas. But when
7427 * placing a child in one of the scrollable windows (border windows or
7428 * text window), you'll need to compute the child's correct position
7429 * in buffer coordinates any time scrolling occurs or buffer changes
7430 * occur, and then call gtk_text_view_move_child() to update the
7431 * child's position. Unfortunately there's no good way to detect that
7432 * scrolling has occurred, using the current API; a possible hack
7433 * would be to update all child positions when the scroll adjustments
7434 * change or the text buffer changes. See bug 64518 on
7435 * bugzilla.gnome.org for status of fixing this issue.
7439 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7441 GtkTextWindowType which_window,
7445 GtkTextViewChild *vc;
7447 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7448 g_return_if_fail (GTK_IS_WIDGET (child));
7449 g_return_if_fail (child->parent == NULL);
7451 vc = text_view_child_new_window (child, which_window,
7454 add_child (text_view, vc);
7456 g_assert (vc->widget == child);
7457 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7461 * gtk_text_view_move_child:
7462 * @text_view: a #GtkTextView
7463 * @child: child widget already added to the text view
7464 * @xpos: new X position in window coordinates
7465 * @ypos: new Y position in window coordinates
7467 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7471 gtk_text_view_move_child (GtkTextView *text_view,
7476 GtkTextViewChild *vc;
7478 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7479 g_return_if_fail (GTK_IS_WIDGET (child));
7480 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7482 vc = g_object_get_data (G_OBJECT (child),
7483 "gtk-text-view-child");
7485 g_assert (vc != NULL);
7487 if (vc->x == xpos &&
7494 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7495 gtk_widget_queue_resize (child);
7499 /* Iterator operations */
7502 * gtk_text_view_forward_display_line:
7503 * @text_view: a #GtkTextView
7504 * @iter: a #GtkTextIter
7506 * Moves the given @iter forward by one display (wrapped) line. A
7507 * display line is different from a paragraph. Paragraphs are
7508 * separated by newlines or other paragraph separator characters.
7509 * Display lines are created by line-wrapping a paragraph. If
7510 * wrapping is turned off, display lines and paragraphs will be the
7511 * same. Display lines are divided differently for each view, since
7512 * they depend on the view's width; paragraphs are the same in all
7513 * views, since they depend on the contents of the #GtkTextBuffer.
7515 * Return value: %TRUE if @iter was moved and is not on the end iterator
7518 gtk_text_view_forward_display_line (GtkTextView *text_view,
7521 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7522 g_return_val_if_fail (iter != NULL, FALSE);
7524 gtk_text_view_ensure_layout (text_view);
7526 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7530 * gtk_text_view_backward_display_line:
7531 * @text_view: a #GtkTextView
7532 * @iter: a #GtkTextIter
7534 * Moves the given @iter backward by one display (wrapped) line. A
7535 * display line is different from a paragraph. Paragraphs are
7536 * separated by newlines or other paragraph separator characters.
7537 * Display lines are created by line-wrapping a paragraph. If
7538 * wrapping is turned off, display lines and paragraphs will be the
7539 * same. Display lines are divided differently for each view, since
7540 * they depend on the view's width; paragraphs are the same in all
7541 * views, since they depend on the contents of the #GtkTextBuffer.
7543 * Return value: %TRUE if @iter was moved and is not on the end iterator
7546 gtk_text_view_backward_display_line (GtkTextView *text_view,
7549 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7550 g_return_val_if_fail (iter != NULL, FALSE);
7552 gtk_text_view_ensure_layout (text_view);
7554 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7558 * gtk_text_view_forward_display_line_end:
7559 * @text_view: a #GtkTextView
7560 * @iter: a #GtkTextIter
7562 * Moves the given @iter forward to the next display line end. A
7563 * display line is different from a paragraph. Paragraphs are
7564 * separated by newlines or other paragraph separator characters.
7565 * Display lines are created by line-wrapping a paragraph. If
7566 * wrapping is turned off, display lines and paragraphs will be the
7567 * same. Display lines are divided differently for each view, since
7568 * they depend on the view's width; paragraphs are the same in all
7569 * views, since they depend on the contents of the #GtkTextBuffer.
7571 * Return value: %TRUE if @iter was moved and is not on the end iterator
7574 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7577 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7578 g_return_val_if_fail (iter != NULL, FALSE);
7580 gtk_text_view_ensure_layout (text_view);
7582 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7586 * gtk_text_view_backward_display_line_start:
7587 * @text_view: a #GtkTextView
7588 * @iter: a #GtkTextIter
7590 * Moves the given @iter backward to the next display line start. A
7591 * display line is different from a paragraph. Paragraphs are
7592 * separated by newlines or other paragraph separator characters.
7593 * Display lines are created by line-wrapping a paragraph. If
7594 * wrapping is turned off, display lines and paragraphs will be the
7595 * same. Display lines are divided differently for each view, since
7596 * they depend on the view's width; paragraphs are the same in all
7597 * views, since they depend on the contents of the #GtkTextBuffer.
7599 * Return value: %TRUE if @iter was moved and is not on the end iterator
7602 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7605 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7606 g_return_val_if_fail (iter != NULL, FALSE);
7608 gtk_text_view_ensure_layout (text_view);
7610 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7614 * gtk_text_view_starts_display_line:
7615 * @text_view: a #GtkTextView
7616 * @iter: a #GtkTextIter
7618 * Determines whether @iter is at the start of a display line.
7619 * See gtk_text_view_forward_display_line() for an explanation of
7620 * display lines vs. paragraphs.
7622 * Return value: %TRUE if @iter begins a wrapped line
7625 gtk_text_view_starts_display_line (GtkTextView *text_view,
7626 const GtkTextIter *iter)
7628 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7629 g_return_val_if_fail (iter != NULL, FALSE);
7631 gtk_text_view_ensure_layout (text_view);
7633 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7637 * gtk_text_view_move_visually:
7638 * @text_view: a #GtkTextView
7639 * @iter: a #GtkTextIter
7640 * @count: number of lines to move
7642 * Moves @iter up or down by @count display (wrapped) lines.
7643 * See gtk_text_view_forward_display_line() for an explanation of
7644 * display lines vs. paragraphs.
7646 * Return value: %TRUE if @iter moved and is not on the end iterator
7649 gtk_text_view_move_visually (GtkTextView *text_view,
7653 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7654 g_return_val_if_fail (iter != NULL, FALSE);
7656 gtk_text_view_ensure_layout (text_view);
7658 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);