1 /* GTK - The GIMP Toolkit
2 * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
30 #include "gtkbindings.h"
32 #include "gtkimagemenuitem.h"
35 #include "gtkmarshalers.h"
37 #include "gtkmenuitem.h"
38 #include "gtkseparatormenuitem.h"
39 #include "gtksettings.h"
40 #include "gtksignal.h"
42 #include "gtktextdisplay.h"
43 #include "gtktextview.h"
44 #include "gtkimmulticontext.h"
45 #include "gdk/gdkkeysyms.h"
46 #include "gtktextutil.h"
47 #include "gtkwindow.h"
50 /* How scrolling, validation, exposes, etc. work.
52 * The expose_event handler has the invariant that the onscreen lines
53 * have been validated.
55 * There are two ways that onscreen lines can become invalid. The first
56 * is to change which lines are onscreen. This happens when the value
57 * of a scroll adjustment changes. So the code path begins in
58 * gtk_text_view_value_changed() and goes like this:
59 * - gdk_window_scroll() to reflect the new adjustment value
60 * - validate the lines that were moved onscreen
61 * - gdk_window_process_updates() to handle the exposes immediately
63 * The second way is that you get the "invalidated" signal from the layout,
64 * indicating that lines have become invalid. This code path begins in
65 * invalidated_handler() and goes like this:
66 * - install high-priority idle which does the rest of the steps
67 * - if a scroll is pending from scroll_to_mark(), do the scroll,
68 * jumping to the gtk_text_view_value_changed() code path
69 * - otherwise, validate the onscreen lines
70 * - DO NOT process updates
72 * In both cases, validating the onscreen lines can trigger a scroll
73 * due to maintaining the first_para on the top of the screen.
74 * If validation triggers a scroll, we jump to the top of the code path
75 * for value_changed, and bail out of the current code path.
77 * Also, in size_allocate, if we invalidate some lines from changing
78 * the layout width, we need to go ahead and run the high-priority idle,
79 * because GTK sends exposes right after doing the size allocates without
80 * returning to the main loop. This is also why the high-priority idle
81 * is at a higher priority than resizing.
85 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
86 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
89 #define DEBUG_VALIDATION_AND_SCROLLING
92 #ifdef DEBUG_VALIDATION_AND_SCROLLING
98 struct _GtkTextPendingScroll
101 gdouble within_margin;
109 SET_SCROLL_ADJUSTMENTS,
127 PROP_PIXELS_ABOVE_LINES,
128 PROP_PIXELS_BELOW_LINES,
129 PROP_PIXELS_INSIDE_WRAP,
141 static void gtk_text_view_init (GtkTextView *text_view);
142 static void gtk_text_view_class_init (GtkTextViewClass *klass);
143 static void gtk_text_view_destroy (GtkObject *object);
144 static void gtk_text_view_finalize (GObject *object);
145 static void gtk_text_view_set_property (GObject *object,
149 static void gtk_text_view_get_property (GObject *object,
153 static void gtk_text_view_size_request (GtkWidget *widget,
154 GtkRequisition *requisition);
155 static void gtk_text_view_size_allocate (GtkWidget *widget,
156 GtkAllocation *allocation);
157 static void gtk_text_view_realize (GtkWidget *widget);
158 static void gtk_text_view_unrealize (GtkWidget *widget);
159 static void gtk_text_view_style_set (GtkWidget *widget,
160 GtkStyle *previous_style);
161 static void gtk_text_view_direction_changed (GtkWidget *widget,
162 GtkTextDirection previous_direction);
163 static gint gtk_text_view_event (GtkWidget *widget,
165 static gint gtk_text_view_key_press_event (GtkWidget *widget,
167 static gint gtk_text_view_key_release_event (GtkWidget *widget,
169 static gint gtk_text_view_button_press_event (GtkWidget *widget,
170 GdkEventButton *event);
171 static gint gtk_text_view_button_release_event (GtkWidget *widget,
172 GdkEventButton *event);
173 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
174 GdkEventFocus *event);
175 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
176 GdkEventFocus *event);
177 static gint gtk_text_view_motion_event (GtkWidget *widget,
178 GdkEventMotion *event);
179 static gint gtk_text_view_expose_event (GtkWidget *widget,
180 GdkEventExpose *expose);
181 static void gtk_text_view_draw_focus (GtkWidget *widget);
182 static void gtk_text_view_grab_focus (GtkWidget *widget);
183 static gboolean gtk_text_view_focus (GtkWidget *widget,
184 GtkDirectionType direction);
187 /* Source side drag signals */
188 static void gtk_text_view_drag_begin (GtkWidget *widget,
189 GdkDragContext *context);
190 static void gtk_text_view_drag_end (GtkWidget *widget,
191 GdkDragContext *context);
192 static void gtk_text_view_drag_data_get (GtkWidget *widget,
193 GdkDragContext *context,
194 GtkSelectionData *selection_data,
197 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
198 GdkDragContext *context);
200 /* Target side drag signals */
201 static void gtk_text_view_drag_leave (GtkWidget *widget,
202 GdkDragContext *context,
204 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
205 GdkDragContext *context,
209 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
210 GdkDragContext *context,
214 static void gtk_text_view_drag_data_received (GtkWidget *widget,
215 GdkDragContext *context,
218 GtkSelectionData *selection_data,
222 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
224 GtkAdjustment *vadj);
225 static void gtk_text_view_popup_menu (GtkWidget *widget);
227 static void gtk_text_view_move_cursor (GtkTextView *text_view,
228 GtkMovementStep step,
230 gboolean extend_selection);
231 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
233 gboolean extend_selection);
234 static void gtk_text_view_set_anchor (GtkTextView *text_view);
235 static void gtk_text_view_scroll_pages (GtkTextView *text_view,
237 static void gtk_text_view_scroll_hpages (GtkTextView *text_view,
239 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
241 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
244 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
245 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
246 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
247 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
248 static void gtk_text_view_move_focus (GtkTextView *text_view,
249 GtkDirectionType direction_type);
250 static void gtk_text_view_unselect (GtkTextView *text_view);
252 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
253 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
255 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
256 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
257 GtkTextAttributes *values,
259 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
260 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
261 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
262 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
263 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
264 const GtkTextIter *iter,
265 GdkEventButton *event);
266 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view,
267 GdkEventButton *event);
268 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
269 const GtkTextIter *iter,
270 GdkEventMotion *event);
271 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
272 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
273 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
275 static void gtk_text_view_value_changed (GtkAdjustment *adj,
277 static void gtk_text_view_commit_handler (GtkIMContext *context,
279 GtkTextView *text_view);
280 static void gtk_text_view_commit_text (GtkTextView *text_view,
282 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
283 GtkTextView *text_view);
284 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
285 GtkTextView *text_view);
286 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
289 GtkTextView *text_view);
291 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
292 const GtkTextIter *location,
295 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
298 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
302 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
303 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
305 static void gtk_text_view_do_popup (GtkTextView *text_view,
306 GdkEventButton *event);
308 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
310 gdouble within_margin,
315 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
316 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
317 static void gtk_text_view_invalidate (GtkTextView *text_view);
319 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
321 /* Container methods */
322 static void gtk_text_view_add (GtkContainer *container,
324 static void gtk_text_view_remove (GtkContainer *container,
326 static void gtk_text_view_forall (GtkContainer *container,
327 gboolean include_internals,
328 GtkCallback callback,
329 gpointer callback_data);
331 /* FIXME probably need the focus methods. */
333 typedef struct _GtkTextViewChild GtkTextViewChild;
335 struct _GtkTextViewChild
339 GtkTextChildAnchor *anchor;
341 gint from_top_of_line;
342 gint from_left_of_buffer;
344 /* These are ignored if anchor != NULL */
345 GtkTextWindowType type;
350 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
351 GtkTextChildAnchor *anchor,
352 GtkTextLayout *layout);
353 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
354 GtkTextWindowType type,
357 static void text_view_child_free (GtkTextViewChild *child);
358 static void text_view_child_set_parent_window (GtkTextView *text_view,
359 GtkTextViewChild *child);
361 struct _GtkTextWindow
363 GtkTextWindowType type;
366 GdkWindow *bin_window;
367 GtkRequisition requisition;
368 GdkRectangle allocation;
371 static GtkTextWindow *text_window_new (GtkTextWindowType type,
374 gint height_request);
375 static void text_window_free (GtkTextWindow *win);
376 static void text_window_realize (GtkTextWindow *win,
378 static void text_window_unrealize (GtkTextWindow *win);
379 static void text_window_size_allocate (GtkTextWindow *win,
381 static void text_window_scroll (GtkTextWindow *win,
384 static void text_window_invalidate_rect (GtkTextWindow *win,
387 static gint text_window_get_width (GtkTextWindow *win);
388 static gint text_window_get_height (GtkTextWindow *win);
389 static void text_window_get_allocation (GtkTextWindow *win,
397 TARGET_COMPOUND_TEXT,
399 TARGET_TEXT_BUFFER_CONTENTS
402 static GtkTargetEntry target_table[] = {
403 { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
404 TARGET_TEXT_BUFFER_CONTENTS },
405 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
406 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
407 { "TEXT", 0, TARGET_TEXT },
408 { "text/plain", 0, TARGET_STRING },
409 { "STRING", 0, TARGET_STRING }
412 static GtkContainerClass *parent_class = NULL;
413 static guint signals[LAST_SIGNAL] = { 0 };
416 gtk_text_view_get_type (void)
418 static GtkType our_type = 0;
422 static const GtkTypeInfo our_info =
425 sizeof (GtkTextView),
426 sizeof (GtkTextViewClass),
427 (GtkClassInitFunc) gtk_text_view_class_init,
428 (GtkObjectInitFunc) gtk_text_view_init,
429 /* reserved_1 */ NULL,
430 /* reserved_2 */ NULL,
431 (GtkClassInitFunc) NULL
434 our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
441 add_move_binding (GtkBindingSet *binding_set,
444 GtkMovementStep step,
447 g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
449 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
453 GTK_TYPE_BOOL, FALSE);
455 /* Selection-extending version */
456 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
460 GTK_TYPE_BOOL, TRUE);
464 gtk_text_view_class_init (GtkTextViewClass *klass)
466 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
467 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
468 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
469 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
470 GtkBindingSet *binding_set;
472 parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
474 /* Default handlers and virtual methods
476 gobject_class->set_property = gtk_text_view_set_property;
477 gobject_class->get_property = gtk_text_view_get_property;
479 object_class->destroy = gtk_text_view_destroy;
480 gobject_class->finalize = gtk_text_view_finalize;
482 widget_class->realize = gtk_text_view_realize;
483 widget_class->unrealize = gtk_text_view_unrealize;
484 widget_class->style_set = gtk_text_view_style_set;
485 widget_class->direction_changed = gtk_text_view_direction_changed;
486 widget_class->size_request = gtk_text_view_size_request;
487 widget_class->size_allocate = gtk_text_view_size_allocate;
488 widget_class->event = gtk_text_view_event;
489 widget_class->key_press_event = gtk_text_view_key_press_event;
490 widget_class->key_release_event = gtk_text_view_key_release_event;
491 widget_class->button_press_event = gtk_text_view_button_press_event;
492 widget_class->button_release_event = gtk_text_view_button_release_event;
493 widget_class->focus_in_event = gtk_text_view_focus_in_event;
494 widget_class->focus_out_event = gtk_text_view_focus_out_event;
495 widget_class->motion_notify_event = gtk_text_view_motion_event;
496 widget_class->expose_event = gtk_text_view_expose_event;
497 widget_class->grab_focus = gtk_text_view_grab_focus;
498 widget_class->focus = gtk_text_view_focus;
500 widget_class->drag_begin = gtk_text_view_drag_begin;
501 widget_class->drag_end = gtk_text_view_drag_end;
502 widget_class->drag_data_get = gtk_text_view_drag_data_get;
503 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
505 widget_class->drag_leave = gtk_text_view_drag_leave;
506 widget_class->drag_motion = gtk_text_view_drag_motion;
507 widget_class->drag_drop = gtk_text_view_drag_drop;
508 widget_class->drag_data_received = gtk_text_view_drag_data_received;
510 widget_class->popup_menu = gtk_text_view_popup_menu;
512 container_class->add = gtk_text_view_add;
513 container_class->remove = gtk_text_view_remove;
514 container_class->forall = gtk_text_view_forall;
516 klass->move_cursor = gtk_text_view_move_cursor;
517 klass->page_horizontally = gtk_text_view_page_horizontally;
518 klass->set_anchor = gtk_text_view_set_anchor;
519 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
520 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
521 klass->cut_clipboard = gtk_text_view_cut_clipboard;
522 klass->copy_clipboard = gtk_text_view_copy_clipboard;
523 klass->paste_clipboard = gtk_text_view_paste_clipboard;
524 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
525 klass->move_focus = gtk_text_view_move_focus;
526 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
532 g_object_class_install_property (gobject_class,
533 PROP_PIXELS_ABOVE_LINES,
534 g_param_spec_int ("pixels_above_lines",
535 _("Pixels Above Lines"),
536 _("Pixels of blank space above paragraphs"),
542 g_object_class_install_property (gobject_class,
543 PROP_PIXELS_BELOW_LINES,
544 g_param_spec_int ("pixels_below_lines",
545 _("Pixels Below Lines"),
546 _("Pixels of blank space below paragraphs"),
552 g_object_class_install_property (gobject_class,
553 PROP_PIXELS_INSIDE_WRAP,
554 g_param_spec_int ("pixels_inside_wrap",
555 _("Pixels Inside Wrap"),
556 _("Pixels of blank space between wrapped lines in a paragraph"),
562 g_object_class_install_property (gobject_class,
564 g_param_spec_boolean ("editable",
566 _("Whether the text can be modified by the user"),
570 g_object_class_install_property (gobject_class,
572 g_param_spec_enum ("wrap_mode",
574 _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
579 g_object_class_install_property (gobject_class,
581 g_param_spec_enum ("justification",
583 _("Left, right, or center justification"),
584 GTK_TYPE_JUSTIFICATION,
588 g_object_class_install_property (gobject_class,
590 g_param_spec_int ("left_margin",
592 _("Width of the left margin in pixels"),
598 g_object_class_install_property (gobject_class,
600 g_param_spec_int ("right_margin",
602 _("Width of the right margin in pixels"),
608 g_object_class_install_property (gobject_class,
610 g_param_spec_int ("indent",
612 _("Amount to indent the paragraph, in pixels"),
618 g_object_class_install_property (gobject_class,
620 g_param_spec_boxed ("tabs",
622 _("Custom tabs for this text"),
623 PANGO_TYPE_TAB_ARRAY,
626 g_object_class_install_property (gobject_class,
628 g_param_spec_boolean ("cursor_visible",
630 _("If the insertion cursor is shown"),
639 gtk_widget_class_install_style_property (widget_class,
640 g_param_spec_boxed ("cursor_color",
642 _("Color with which to draw insertion cursor"),
651 signals[MOVE_CURSOR] =
652 gtk_signal_new ("move_cursor",
653 GTK_RUN_LAST | GTK_RUN_ACTION,
654 GTK_CLASS_TYPE (object_class),
655 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
656 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
657 GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
659 signals[PAGE_HORIZONTALLY] =
660 gtk_signal_new ("page_horizontally",
661 GTK_RUN_LAST | GTK_RUN_ACTION,
662 GTK_CLASS_TYPE (object_class),
663 GTK_SIGNAL_OFFSET (GtkTextViewClass, page_horizontally),
664 _gtk_marshal_VOID__INT_BOOLEAN,
665 GTK_TYPE_NONE, 2, GTK_TYPE_INT, GTK_TYPE_BOOL);
667 signals[SET_ANCHOR] =
668 gtk_signal_new ("set_anchor",
669 GTK_RUN_LAST | GTK_RUN_ACTION,
670 GTK_CLASS_TYPE (object_class),
671 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
672 _gtk_marshal_VOID__VOID,
675 signals[INSERT_AT_CURSOR] =
676 gtk_signal_new ("insert_at_cursor",
677 GTK_RUN_LAST | GTK_RUN_ACTION,
678 GTK_CLASS_TYPE (object_class),
679 GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
680 _gtk_marshal_VOID__STRING,
681 GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
683 signals[DELETE_FROM_CURSOR] =
684 gtk_signal_new ("delete_from_cursor",
685 GTK_RUN_LAST | GTK_RUN_ACTION,
686 GTK_CLASS_TYPE (object_class),
687 GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
688 _gtk_marshal_VOID__ENUM_INT,
689 GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
691 signals[CUT_CLIPBOARD] =
692 gtk_signal_new ("cut_clipboard",
693 GTK_RUN_LAST | GTK_RUN_ACTION,
694 GTK_CLASS_TYPE (object_class),
695 GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
696 _gtk_marshal_VOID__VOID,
699 signals[COPY_CLIPBOARD] =
700 gtk_signal_new ("copy_clipboard",
701 GTK_RUN_LAST | GTK_RUN_ACTION,
702 GTK_CLASS_TYPE (object_class),
703 GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
704 _gtk_marshal_VOID__VOID,
707 signals[PASTE_CLIPBOARD] =
708 gtk_signal_new ("paste_clipboard",
709 GTK_RUN_LAST | GTK_RUN_ACTION,
710 GTK_CLASS_TYPE (object_class),
711 GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
712 _gtk_marshal_VOID__VOID,
715 signals[TOGGLE_OVERWRITE] =
716 gtk_signal_new ("toggle_overwrite",
717 GTK_RUN_LAST | GTK_RUN_ACTION,
718 GTK_CLASS_TYPE (object_class),
719 GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
720 _gtk_marshal_VOID__VOID,
723 signals[MOVE_FOCUS] =
724 gtk_signal_new ("move_focus",
725 GTK_RUN_LAST | GTK_RUN_ACTION,
726 GTK_CLASS_TYPE (object_class),
727 GTK_SIGNAL_OFFSET (GtkTextViewClass, move_focus),
728 _gtk_marshal_VOID__ENUM,
729 GTK_TYPE_NONE, 1, GTK_TYPE_DIRECTION_TYPE);
731 signals[SET_SCROLL_ADJUSTMENTS] =
732 gtk_signal_new ("set_scroll_adjustments",
734 GTK_CLASS_TYPE (object_class),
735 GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
736 _gtk_marshal_VOID__OBJECT_OBJECT,
737 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
738 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
740 signals[POPULATE_POPUP] =
741 gtk_signal_new ("populate_popup",
743 GTK_CLASS_TYPE (object_class),
744 GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
745 _gtk_marshal_VOID__OBJECT,
746 GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
752 binding_set = gtk_binding_set_by_class (klass);
754 /* Moving the insertion point */
755 add_move_binding (binding_set, GDK_Right, 0,
756 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
758 add_move_binding (binding_set, GDK_KP_Right, 0,
759 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
761 add_move_binding (binding_set, GDK_Left, 0,
762 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
764 add_move_binding (binding_set, GDK_KP_Left, 0,
765 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
767 add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
768 GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
770 add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
771 GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
773 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
774 GTK_MOVEMENT_WORDS, 1);
776 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
777 GTK_MOVEMENT_WORDS, 1);
779 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
780 GTK_MOVEMENT_WORDS, -1);
782 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
783 GTK_MOVEMENT_WORDS, 1);
785 /* Eventually we want to move by display lines, not paragraphs */
786 add_move_binding (binding_set, GDK_Up, 0,
787 GTK_MOVEMENT_DISPLAY_LINES, -1);
789 add_move_binding (binding_set, GDK_KP_Up, 0,
790 GTK_MOVEMENT_DISPLAY_LINES, -1);
792 add_move_binding (binding_set, GDK_Down, 0,
793 GTK_MOVEMENT_DISPLAY_LINES, 1);
795 add_move_binding (binding_set, GDK_KP_Down, 0,
796 GTK_MOVEMENT_DISPLAY_LINES, 1);
798 add_move_binding (binding_set, GDK_p, GDK_CONTROL_MASK,
799 GTK_MOVEMENT_DISPLAY_LINES, -1);
801 add_move_binding (binding_set, GDK_n, GDK_CONTROL_MASK,
802 GTK_MOVEMENT_DISPLAY_LINES, 1);
804 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
805 GTK_MOVEMENT_PARAGRAPHS, -1);
807 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
808 GTK_MOVEMENT_PARAGRAPHS, -1);
810 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
811 GTK_MOVEMENT_PARAGRAPHS, 1);
813 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
814 GTK_MOVEMENT_PARAGRAPHS, 1);
816 add_move_binding (binding_set, GDK_a, GDK_CONTROL_MASK,
817 GTK_MOVEMENT_PARAGRAPH_ENDS, -1);
819 add_move_binding (binding_set, GDK_e, GDK_CONTROL_MASK,
820 GTK_MOVEMENT_PARAGRAPH_ENDS, 1);
822 add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
823 GTK_MOVEMENT_WORDS, 1);
825 add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
826 GTK_MOVEMENT_WORDS, -1);
828 add_move_binding (binding_set, GDK_Home, 0,
829 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
831 add_move_binding (binding_set, GDK_KP_Home, 0,
832 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
834 add_move_binding (binding_set, GDK_End, 0,
835 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
837 add_move_binding (binding_set, GDK_KP_End, 0,
838 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
840 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
841 GTK_MOVEMENT_BUFFER_ENDS, -1);
843 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
844 GTK_MOVEMENT_BUFFER_ENDS, -1);
846 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
847 GTK_MOVEMENT_BUFFER_ENDS, 1);
849 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
850 GTK_MOVEMENT_BUFFER_ENDS, 1);
852 add_move_binding (binding_set, GDK_Page_Up, 0,
853 GTK_MOVEMENT_PAGES, -1);
855 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
856 GTK_MOVEMENT_PAGES, -1);
858 add_move_binding (binding_set, GDK_Page_Down, 0,
859 GTK_MOVEMENT_PAGES, 1);
861 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
862 GTK_MOVEMENT_PAGES, 1);
864 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
865 "page_horizontally", 2,
867 GTK_TYPE_BOOL, FALSE);
869 gtk_binding_entry_add_signal (binding_set, GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
870 "page_horizontally", 2,
872 GTK_TYPE_BOOL, TRUE);
874 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
875 "page_horizontally", 2,
877 GTK_TYPE_BOOL, FALSE);
879 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
880 "page_horizontally", 2,
882 GTK_TYPE_BOOL, TRUE);
884 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
885 "page_horizontally", 2,
887 GTK_TYPE_BOOL, FALSE);
889 gtk_binding_entry_add_signal (binding_set, GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
890 "page_horizontally", 2,
892 GTK_TYPE_BOOL, TRUE);
894 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
895 "page_horizontally", 2,
897 GTK_TYPE_BOOL, FALSE);
899 gtk_binding_entry_add_signal (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
900 "page_horizontally", 2,
902 GTK_TYPE_BOOL, TRUE);
904 /* Setting the cut/paste/copy anchor */
905 gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK,
907 gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_CONTROL_MASK,
911 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
912 "delete_from_cursor", 2,
913 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
916 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
917 "delete_from_cursor", 2,
918 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
921 gtk_binding_entry_add_signal (binding_set, GDK_d, GDK_CONTROL_MASK,
922 "delete_from_cursor", 2,
923 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
926 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
927 "delete_from_cursor", 2,
928 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
931 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
932 "delete_from_cursor", 2,
933 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
936 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
937 "delete_from_cursor", 2,
938 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
941 gtk_binding_entry_add_signal (binding_set, GDK_d, GDK_MOD1_MASK,
942 "delete_from_cursor", 2,
943 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
946 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
947 "delete_from_cursor", 2,
948 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
951 gtk_binding_entry_add_signal (binding_set, GDK_k, GDK_CONTROL_MASK,
952 "delete_from_cursor", 2,
953 GTK_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
956 gtk_binding_entry_add_signal (binding_set, GDK_u, GDK_CONTROL_MASK,
957 "delete_from_cursor", 2,
958 GTK_TYPE_ENUM, GTK_DELETE_PARAGRAPHS,
961 gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
962 "delete_from_cursor", 2,
963 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
965 gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_MOD1_MASK,
966 "delete_from_cursor", 2,
967 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
969 gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
970 "insert_at_cursor", 1,
971 GTK_TYPE_STRING, " ");
972 gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_MOD1_MASK,
973 "insert_at_cursor", 1,
974 GTK_TYPE_STRING, " ");
976 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_MOD1_MASK,
977 "delete_from_cursor", 2,
978 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
983 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
986 gtk_binding_entry_add_signal (binding_set, GDK_w, GDK_CONTROL_MASK,
989 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
990 "copy_clipboard", 0);
992 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
993 "paste_clipboard", 0);
995 gtk_binding_entry_add_signal (binding_set, GDK_y, GDK_CONTROL_MASK,
996 "paste_clipboard", 0);
999 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1000 "toggle_overwrite", 0);
1001 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1002 "toggle_overwrite", 0);
1004 /* Control-tab focus motion */
1005 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
1007 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1008 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
1010 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1011 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, GDK_CONTROL_MASK,
1013 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1015 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1017 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1018 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1020 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1021 gtk_binding_entry_add_signal (binding_set, GDK_ISO_Left_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1023 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1027 gtk_text_view_init (GtkTextView *text_view)
1031 widget = GTK_WIDGET (text_view);
1033 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1035 /* Set up default style */
1036 text_view->wrap_mode = GTK_WRAP_NONE;
1037 text_view->pixels_above_lines = 0;
1038 text_view->pixels_below_lines = 0;
1039 text_view->pixels_inside_wrap = 0;
1040 text_view->justify = GTK_JUSTIFY_LEFT;
1041 text_view->left_margin = 0;
1042 text_view->right_margin = 0;
1043 text_view->indent = 0;
1044 text_view->tabs = NULL;
1045 text_view->editable = TRUE;
1047 gtk_drag_dest_set (widget,
1049 target_table, G_N_ELEMENTS (target_table),
1050 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1052 text_view->virtual_cursor_x = -1;
1053 text_view->virtual_cursor_y = -1;
1055 /* This object is completely private. No external entity can gain a reference
1056 * to it; so we create it here and destroy it in finalize ().
1058 text_view->im_context = gtk_im_multicontext_new ();
1060 g_signal_connect (G_OBJECT (text_view->im_context), "commit",
1061 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1062 g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
1063 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1064 g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
1065 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1066 g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
1067 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1069 text_view->cursor_visible = TRUE;
1071 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1074 text_view->drag_start_x = -1;
1075 text_view->drag_start_y = -1;
1077 text_view->pending_place_cursor_button = 0;
1079 /* We handle all our own redrawing */
1080 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1084 * gtk_text_view_new:
1086 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1087 * before using the text view, an empty default buffer will be created
1088 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1089 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1091 * Return value: a new #GtkTextView
1094 gtk_text_view_new (void)
1096 return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1100 * gtk_text_view_new_with_buffer:
1101 * @buffer: a #GtkTextBuffer
1103 * Creates a new #GtkTextView widget displaying the buffer
1104 * @buffer. One buffer can be shared among many widgets.
1105 * @buffer may be NULL to create a default buffer, in which case
1106 * this function is equivalent to gtk_text_view_new(). The
1107 * text view adds its own reference count to the buffer; it does not
1108 * take over an existing reference.
1110 * Return value: a new #GtkTextView.
1113 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1115 GtkTextView *text_view;
1117 text_view = (GtkTextView*)gtk_text_view_new ();
1119 gtk_text_view_set_buffer (text_view, buffer);
1121 return GTK_WIDGET (text_view);
1125 * gtk_text_view_set_buffer:
1126 * @text_view: a #GtkTextView
1127 * @buffer: a #GtkTextBuffer
1129 * Sets @buffer as the buffer being displayed by @text_view. The previous
1130 * buffer displayed by the text view is unreferenced, and a reference is
1131 * added to @buffer. If you owned a reference to @buffer before passing it
1132 * to this function, you must remove that reference yourself; #GtkTextView
1133 * will not "adopt" it.
1137 gtk_text_view_set_buffer (GtkTextView *text_view,
1138 GtkTextBuffer *buffer)
1140 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1141 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1143 if (text_view->buffer == buffer)
1146 if (text_view->buffer != NULL)
1148 /* Destroy all anchored children */
1152 copy = g_slist_copy (text_view->children);
1154 while (tmp_list != NULL)
1156 GtkTextViewChild *vc = tmp_list->data;
1160 gtk_widget_destroy (vc->widget);
1161 /* vc may now be invalid! */
1164 tmp_list = g_slist_next (tmp_list);
1167 g_slist_free (copy);
1169 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1170 gtk_text_view_mark_set_handler, text_view);
1171 g_object_unref (G_OBJECT (text_view->buffer));
1172 text_view->dnd_mark = NULL;
1174 if (GTK_WIDGET_REALIZED (text_view))
1175 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
1176 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1179 text_view->buffer = buffer;
1185 g_object_ref (G_OBJECT (buffer));
1187 if (text_view->layout)
1188 gtk_text_layout_set_buffer (text_view->layout, buffer);
1190 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1192 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1196 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1200 text_view->first_para_pixels = 0;
1202 g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1203 G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1205 if (GTK_WIDGET_REALIZED (text_view))
1206 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
1207 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1210 if (GTK_WIDGET_VISIBLE (text_view))
1211 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1213 gtk_text_view_invalidate (text_view);
1216 static GtkTextBuffer*
1217 get_buffer (GtkTextView *text_view)
1219 if (text_view->buffer == NULL)
1222 b = gtk_text_buffer_new (NULL);
1223 gtk_text_view_set_buffer (text_view, b);
1224 g_object_unref (G_OBJECT (b));
1227 return text_view->buffer;
1231 * gtk_text_view_get_buffer:
1232 * @text_view: a #GtkTextView
1234 * Returns the #GtkTextBuffer being displayed by this text view.
1235 * The reference count on the buffer is not incremented; the caller
1236 * of this function won't own a new reference.
1238 * Return value: a #GtkTextBuffer
1241 gtk_text_view_get_buffer (GtkTextView *text_view)
1243 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1245 return get_buffer (text_view);
1249 * gtk_text_view_get_iter_at_location:
1250 * @text_view: a #GtkTextView
1251 * @iter: a #GtkTextIter
1252 * @x: x position, in buffer coordinates
1253 * @y: y position, in buffer coordinates
1255 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1256 * coordinates are coordinates for the entire buffer, not just the
1257 * currently-displayed portion. If you have coordinates from an
1258 * event, you have to convert those to buffer coordinates with
1259 * gtk_text_view_window_to_buffer_coords().
1263 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1268 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1269 g_return_if_fail (iter != NULL);
1270 g_return_if_fail (text_view->layout != NULL);
1272 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1279 * gtk_text_view_get_iter_location:
1280 * @text_view: a #GtkTextView
1281 * @iter: a #GtkTextIter
1282 * @location: bounds of the character at @iter
1284 * Gets a rectangle which roughly contains the character at @iter.
1285 * The rectangle position is in buffer coordinates; use
1286 * gtk_text_view_buffer_to_window_coords() to convert these
1287 * coordinates to coordinates for one of the windows in the text view.
1291 gtk_text_view_get_iter_location (GtkTextView *text_view,
1292 const GtkTextIter *iter,
1293 GdkRectangle *location)
1295 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1296 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1298 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1302 * gtk_text_view_get_line_yrange:
1303 * @text_view: a #GtkTextView
1304 * @iter: a #GtkTextIter
1305 * @y: return location for a y coordinate
1306 * @height: return location for a height
1308 * Gets the y coordinate of the top of the line containing @iter,
1309 * and the height of the line. The coordinate is a buffer coordinate;
1310 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1314 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1315 const GtkTextIter *iter,
1319 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1320 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1322 gtk_text_layout_get_line_yrange (text_view->layout,
1329 * gtk_text_view_get_line_at_y:
1330 * @text_view: a #GtkTextView
1331 * @target_iter: a #GtkTextIter
1332 * @y: a y coordinate
1333 * @line_top: return location for top coordinate of the line
1335 * Gets the #GtkTextIter at the start of the line containing
1336 * the coordinate @y. @y is in buffer coordinates, convert from
1337 * window coordinates with gtk_text_view_window_to_buffer_coords().
1338 * If non-%NULL, @line_top will be filled with the coordinate of the top
1342 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1343 GtkTextIter *target_iter,
1347 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1349 gtk_text_layout_get_line_at_y (text_view->layout,
1356 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1358 DV (g_print (" Setting adj to raw value %g\n", val));
1360 /* We don't really want to clamp to upper; we want to clamp to
1361 upper - page_size which is the highest value the scrollbar
1362 will let us reach. */
1363 if (val > (adj->upper - adj->page_size))
1364 val = adj->upper - adj->page_size;
1366 if (val < adj->lower)
1369 if (val != adj->value)
1371 DV (g_print (" Setting adj to clamped value %g\n", val));
1372 gtk_adjustment_set_value (adj, val);
1380 * gtk_text_view_scroll_to_iter:
1381 * @text_view: a #GtkTextView
1382 * @iter: a #GtkTextIter
1383 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1384 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1385 * @xalign: horizontal alignment of mark within visible area.
1386 * @yalign: vertical alignment of mark within visible area
1388 * Scrolls @text_view so that @iter is on the screen in the position
1389 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1390 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1391 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1392 * possibly not scrolling at all. The effective screen for purposes
1393 * of this function is reduced by a margin of size @within_margin.
1394 * NOTE: This function uses the currently-computed height of the
1395 * lines in the text buffer. Note that line heights are computed
1396 * in an idle handler; so this function may not have the desired effect
1397 * if it's called before the height computations. To avoid oddness,
1398 * consider using gtk_text_view_scroll_to_mark() which saves a point
1399 * to be scrolled to after line validation.
1401 * Return value: %TRUE if scrolling occurred
1404 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1406 gdouble within_margin,
1412 GdkRectangle screen;
1417 gboolean retval = FALSE;
1419 gint screen_xoffset, screen_yoffset;
1420 gint current_x_scroll, current_y_scroll;
1422 /* FIXME why don't we do the validate-at-scroll-destination thing
1423 * from flush_scroll in this function? I think it wasn't done before
1424 * because changed_handler was screwed up, but I could be wrong.
1427 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1428 g_return_val_if_fail (iter != NULL, FALSE);
1429 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1430 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1431 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1433 widget = GTK_WIDGET (text_view);
1435 DV(g_print(G_STRLOC"\n"));
1437 gtk_text_layout_get_iter_location (text_view->layout,
1441 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1443 current_x_scroll = text_view->xoffset;
1444 current_y_scroll = text_view->yoffset;
1446 screen.x = current_x_scroll;
1447 screen.y = current_y_scroll;
1448 screen.width = SCREEN_WIDTH (widget);
1449 screen.height = SCREEN_HEIGHT (widget);
1451 screen_xoffset = screen.width * within_margin;
1452 screen_yoffset = screen.height * within_margin;
1454 screen.x += screen_xoffset;
1455 screen.y += screen_yoffset;
1456 screen.width -= screen_xoffset * 2;
1457 screen.height -= screen_yoffset * 2;
1459 /* paranoia check */
1460 if (screen.width < 1)
1462 if (screen.height < 1)
1465 screen_right = screen.x + screen.width;
1466 screen_bottom = screen.y + screen.height;
1468 /* The alignment affects the point in the target character that we
1469 * choose to align. If we're doing right/bottom alignment, we align
1470 * the right/bottom edge of the character the mark is at; if we're
1471 * doing left/top we align the left/top edge of the character; if
1472 * we're doing center alignment we align the center of the
1476 /* Vertical scroll */
1479 scroll_dest = current_y_scroll;
1483 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1485 /* if scroll_dest < screen.y, we move a negative increment (up),
1486 * else a positive increment (down)
1488 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1492 /* move minimum to get onscreen */
1493 if (rect.y < screen.y)
1495 scroll_dest = rect.y;
1496 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1498 else if ((rect.y + rect.height) > screen_bottom)
1500 scroll_dest = rect.y + rect.height;
1501 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1505 if (scroll_inc != 0)
1507 retval = set_adjustment_clamped (get_vadjustment (text_view),
1508 current_y_scroll + scroll_inc);
1510 DV (g_print (" vert increment %d\n", scroll_inc));
1513 /* Horizontal scroll */
1516 scroll_dest = current_x_scroll;
1520 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1522 /* if scroll_dest < screen.y, we move a negative increment (left),
1523 * else a positive increment (right)
1525 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1529 /* move minimum to get onscreen */
1530 if (rect.x < screen.x)
1532 scroll_dest = rect.x;
1533 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1535 else if ((rect.x + rect.width) > screen_right)
1537 scroll_dest = rect.x + rect.width;
1538 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1542 if (scroll_inc != 0)
1544 retval = set_adjustment_clamped (get_hadjustment (text_view),
1545 current_x_scroll + scroll_inc);
1547 DV (g_print (" horiz increment %d\n", scroll_inc));
1551 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1553 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1559 free_pending_scroll (GtkTextPendingScroll *scroll)
1561 if (!gtk_text_mark_get_deleted (scroll->mark))
1562 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1564 g_object_unref (G_OBJECT (scroll->mark));
1569 gtk_text_view_queue_scroll (GtkTextView *text_view,
1571 gdouble within_margin,
1577 GtkTextPendingScroll *scroll;
1579 DV(g_print(G_STRLOC"\n"));
1581 scroll = g_new (GtkTextPendingScroll, 1);
1583 scroll->within_margin = within_margin;
1584 scroll->use_align = use_align;
1585 scroll->xalign = xalign;
1586 scroll->yalign = yalign;
1588 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1590 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1593 gtk_text_mark_get_left_gravity (mark));
1595 g_object_ref (G_OBJECT (scroll->mark));
1597 if (text_view->pending_scroll)
1598 free_pending_scroll (text_view->pending_scroll);
1600 text_view->pending_scroll = scroll;
1604 gtk_text_view_flush_scroll (GtkTextView *text_view)
1607 GtkTextPendingScroll *scroll;
1610 DV(g_print(G_STRLOC"\n"));
1612 if (text_view->pending_scroll == NULL)
1615 scroll = text_view->pending_scroll;
1617 /* avoid recursion */
1618 text_view->pending_scroll = NULL;
1620 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1622 /* Validate arbitrary area around the scroll destination, so the adjustment
1623 * can meaningfully point into that area
1625 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1626 gtk_text_layout_validate_yrange (text_view->layout, &iter, -300, 300);
1628 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1630 /* Ensure we have updated width/height */
1631 gtk_text_view_update_adjustments (text_view);
1633 retval = gtk_text_view_scroll_to_iter (text_view,
1635 scroll->within_margin,
1640 free_pending_scroll (scroll);
1646 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1648 if (upper != adj->upper)
1650 gdouble min = MAX (0.0, upper - adj->page_size);
1651 gboolean value_changed = FALSE;
1655 if (adj->value > min)
1658 value_changed = TRUE;
1661 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1662 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1666 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1667 gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1673 gtk_text_view_update_adjustments (GtkTextView *text_view)
1675 gint width = 0, height = 0;
1677 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1679 if (text_view->layout)
1680 gtk_text_layout_get_size (text_view->layout, &width, &height);
1682 if (text_view->width != width || text_view->height != height)
1684 text_view->width = width;
1685 text_view->height = height;
1687 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1688 MAX (SCREEN_WIDTH (text_view), width));
1689 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1690 MAX (SCREEN_HEIGHT (text_view), height));
1692 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1694 /* Set up the step sizes; we'll say that a page is
1695 our allocation minus one step, and a step is
1696 1/10 of our allocation. */
1697 text_view->hadjustment->step_increment =
1698 SCREEN_WIDTH (text_view) / 10.0;
1699 text_view->hadjustment->page_increment =
1700 SCREEN_WIDTH (text_view) * 0.9;
1702 text_view->vadjustment->step_increment =
1703 SCREEN_HEIGHT (text_view) / 10.0;
1704 text_view->vadjustment->page_increment =
1705 SCREEN_HEIGHT (text_view) * 0.9;
1707 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1708 gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1713 gtk_text_view_update_layout_width (GtkTextView *text_view)
1715 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1717 gtk_text_view_ensure_layout (text_view);
1719 gtk_text_layout_set_screen_width (text_view->layout,
1720 SCREEN_WIDTH (text_view));
1724 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1727 gint cursor_x_pos, cursor_y_pos;
1729 if (text_view->layout == NULL)
1732 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1734 area.x = cursor_x_pos;
1735 area.y = cursor_y_pos;
1736 area.width = area.height = 0;
1738 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1742 * gtk_text_view_scroll_to_mark:
1743 * @text_view: a #GtkTextView
1744 * @mark: a #GtkTextMark
1745 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1746 * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1747 * @xalign: horizontal alignment of mark within visible area.
1748 * @yalign: vertical alignment of mark within visible area
1750 * Scrolls @text_view so that @mark is on the screen in the position
1751 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1752 * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1753 * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1754 * possibly not scrolling at all. The effective screen for purposes
1755 * of this function is reduced by a margin of size @within_margin.
1759 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1761 gdouble within_margin,
1766 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1767 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1768 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1769 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1770 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1772 gtk_text_view_queue_scroll (text_view, mark,
1778 /* If no validation is pending, we need to go ahead and force an
1781 if (text_view->layout &&
1782 gtk_text_layout_is_valid (text_view->layout))
1783 gtk_text_view_flush_scroll (text_view);
1787 * gtk_text_view_scroll_mark_onscreen:
1788 * @text_view: a #GtkTextView
1789 * @mark: a mark in the buffer for @text_view
1791 * Scrolls @text_view the minimum distance such that @mark is contained
1792 * within the visible area of the widget.
1796 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1799 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1800 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1802 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1806 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1808 GdkRectangle visible_rect;
1809 gtk_text_view_get_visible_rect (text_view, &visible_rect);
1811 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1813 visible_rect.y + visible_rect.height);
1817 * gtk_text_view_move_mark_onscreen:
1818 * @text_view: a #GtkTextView
1819 * @mark: a #GtkTextMark
1821 * Moves a mark within the buffer so that it's
1822 * located within the currently-visible text area.
1824 * Return value: %TRUE if the mark moved (wasn't already onscreen)
1827 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1832 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1833 g_return_val_if_fail (mark != NULL, FALSE);
1835 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1837 if (clamp_iter_onscreen (text_view, &iter))
1839 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1847 * gtk_text_view_get_visible_rect:
1848 * @text_view: a #GtkTextView
1849 * @visible_rect: rectangle to fill
1851 * Fills @visible_rect with the currently-visible
1852 * region of the buffer, in buffer coordinates. Convert to window coordinates
1853 * with gtk_text_view_buffer_to_window_coords().
1856 gtk_text_view_get_visible_rect (GtkTextView *text_view,
1857 GdkRectangle *visible_rect)
1861 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1863 widget = GTK_WIDGET (text_view);
1867 visible_rect->x = text_view->xoffset;
1868 visible_rect->y = text_view->yoffset;
1869 visible_rect->width = SCREEN_WIDTH (widget);
1870 visible_rect->height = SCREEN_HEIGHT (widget);
1872 DV(g_print(" visible rect: %d,%d %d x %d\n",
1875 visible_rect->width,
1876 visible_rect->height));
1881 * gtk_text_view_set_wrap_mode:
1882 * @text_view: a #GtkTextView
1883 * @wrap_mode: a #GtkWrapMode
1885 * Sets the line wrapping for the view.
1888 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1889 GtkWrapMode wrap_mode)
1891 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1893 if (text_view->wrap_mode != wrap_mode)
1895 text_view->wrap_mode = wrap_mode;
1897 if (text_view->layout)
1899 text_view->layout->default_style->wrap_mode = wrap_mode;
1900 gtk_text_layout_default_style_changed (text_view->layout);
1904 g_object_notify (G_OBJECT (text_view), "wrap_mode");
1908 * gtk_text_view_get_wrap_mode:
1909 * @text_view: a #GtkTextView
1911 * Gets the line wrapping for the view.
1913 * Return value: the line wrap setting
1916 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1918 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1920 return text_view->wrap_mode;
1924 * gtk_text_view_set_editable:
1925 * @text_view: a #GtkTextView
1926 * @setting: whether it's editable
1928 * Sets the default editability of the #GtkTextView. You can override
1929 * this default setting with tags in the buffer, using the "editable"
1930 * attribute of tags.
1933 gtk_text_view_set_editable (GtkTextView *text_view,
1936 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1937 setting = setting != FALSE;
1939 if (text_view->editable != setting)
1941 text_view->editable = setting;
1943 if (text_view->layout)
1945 text_view->layout->default_style->editable = text_view->editable;
1946 gtk_text_layout_default_style_changed (text_view->layout);
1949 g_object_notify (G_OBJECT (text_view), "editable");
1954 * gtk_text_view_get_editable:
1955 * @text_view: a #GtkTextView
1957 * Returns the default editability of the #GtkTextView. Tags in the
1958 * buffer may override this setting for some ranges of text.
1960 * Return value: whether text is editable by default
1963 gtk_text_view_get_editable (GtkTextView *text_view)
1965 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1967 return text_view->editable;
1971 * gtk_text_view_set_pixels_above_lines:
1972 * @text_view: a #GtkTextView
1973 * @pixels_above_lines: pixels above paragraphs
1975 * Sets the default number of blank pixels above paragraphs in @text_view.
1976 * Tags in the buffer for @text_view may override the defaults.
1980 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1981 gint pixels_above_lines)
1983 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1985 if (text_view->pixels_above_lines != pixels_above_lines)
1987 text_view->pixels_above_lines = pixels_above_lines;
1989 if (text_view->layout)
1991 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1992 gtk_text_layout_default_style_changed (text_view->layout);
1995 g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
2000 * gtk_text_view_get_pixels_above_lines:
2001 * @text_view: a #GtkTextView
2003 * Gets the default number of pixels to put above paragraphs.
2005 * Return value: default number of pixels above paragraphs
2008 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2010 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2012 return text_view->pixels_above_lines;
2016 * gtk_text_view_set_pixels_below_lines:
2017 * @text_view: a #GtkTextView
2018 * @pixels_below_lines: pixels below paragraphs
2020 * Sets the default number of pixels of blank space
2021 * to put below paragraphs in @text_view. May be overridden
2022 * by tags applied to @text_view's buffer.
2026 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2027 gint pixels_below_lines)
2029 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2031 if (text_view->pixels_below_lines != pixels_below_lines)
2033 text_view->pixels_below_lines = pixels_below_lines;
2035 if (text_view->layout)
2037 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2038 gtk_text_layout_default_style_changed (text_view->layout);
2041 g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
2046 * gtk_text_view_get_pixels_below_lines:
2047 * @text_view: a #GtkTextView
2049 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2051 * Return value: default number of blank pixels below paragraphs
2054 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2056 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2058 return text_view->pixels_below_lines;
2062 * gtk_text_view_set_pixels_inside_wrap:
2063 * @text_view: a #GtkTextView
2064 * @pixels_inside_wrap: default number of pixels between wrapped lines
2066 * Sets the default number of pixels of blank space to leave between
2067 * display/wrapped lines within a paragraph. May be overridden by
2068 * tags in @text_view's buffer.
2072 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2073 gint pixels_inside_wrap)
2075 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2077 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2079 text_view->pixels_inside_wrap = pixels_inside_wrap;
2081 if (text_view->layout)
2083 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2084 gtk_text_layout_default_style_changed (text_view->layout);
2087 g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
2092 * gtk_text_view_get_pixels_inside_wrap:
2093 * @text_view: a #GtkTextView
2095 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2097 * Return value: default number of pixels of blank space between wrapped lines
2100 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2102 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2104 return text_view->pixels_inside_wrap;
2108 * gtk_text_view_set_justification:
2109 * @text_view: a #GtkTextView
2110 * @justification: justification
2112 * Sets the default justification of text in @text_view.
2113 * Tags in the view's buffer may override the default.
2117 gtk_text_view_set_justification (GtkTextView *text_view,
2118 GtkJustification justification)
2120 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2122 if (text_view->justify != justification)
2124 text_view->justify = justification;
2126 if (text_view->layout)
2128 text_view->layout->default_style->justification = justification;
2129 gtk_text_layout_default_style_changed (text_view->layout);
2132 g_object_notify (G_OBJECT (text_view), "justification");
2137 * gtk_text_view_get_justification:
2138 * @text_view: a #GtkTextView
2140 * Gets the default justification of paragraphs in @text_view.
2141 * Tags in the buffer may override the default.
2143 * Return value: default justification
2146 gtk_text_view_get_justification (GtkTextView *text_view)
2148 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2150 return text_view->justify;
2154 * gtk_text_view_set_left_margin:
2155 * @text_view: a #GtkTextView
2156 * @left_margin: left margin in pixels
2158 * Sets the default left margin for text in @text_view.
2159 * Tags in the buffer may override the default.
2163 gtk_text_view_set_left_margin (GtkTextView *text_view,
2166 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2168 if (text_view->left_margin != left_margin)
2170 text_view->left_margin = left_margin;
2172 if (text_view->layout)
2174 text_view->layout->default_style->left_margin = left_margin;
2175 gtk_text_layout_default_style_changed (text_view->layout);
2178 g_object_notify (G_OBJECT (text_view), "left_margin");
2183 * gtk_text_view_get_left_margin:
2184 * @text_view: a #GtkTextView
2186 * Gets the default left margin size of paragraphs in the @text_view.
2187 * Tags in the buffer may override the default.
2189 * Return value: left margin in pixels
2192 gtk_text_view_get_left_margin (GtkTextView *text_view)
2194 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2196 return text_view->left_margin;
2200 * gtk_text_view_set_right_margin:
2201 * @text_view: a #GtkTextView
2202 * @right_margin: right margin in pixels
2204 * Sets the default right margin for text in the text view.
2205 * Tags in the buffer may override the default.
2209 gtk_text_view_set_right_margin (GtkTextView *text_view,
2212 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2214 if (text_view->right_margin != right_margin)
2216 text_view->right_margin = right_margin;
2218 if (text_view->layout)
2220 text_view->layout->default_style->right_margin = right_margin;
2221 gtk_text_layout_default_style_changed (text_view->layout);
2224 g_object_notify (G_OBJECT (text_view), "right_margin");
2229 * gtk_text_view_get_right_margin:
2230 * @text_view: a #GtkTextView
2232 * Gets the default right margin for text in @text_view. Tags
2233 * in the buffer may override the default.
2235 * Return value: right margin in pixels
2238 gtk_text_view_get_right_margin (GtkTextView *text_view)
2240 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2242 return text_view->right_margin;
2246 * gtk_text_view_set_indent:
2247 * @text_view: a #GtkTextView
2248 * @indent: indentation in pixels
2250 * Sets the default indentation for paragraphs in @text_view.
2251 * Tags in the buffer may override the default.
2255 gtk_text_view_set_indent (GtkTextView *text_view,
2258 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2260 if (text_view->indent != indent)
2262 text_view->indent = indent;
2264 if (text_view->layout)
2266 text_view->layout->default_style->indent = indent;
2267 gtk_text_layout_default_style_changed (text_view->layout);
2270 g_object_notify (G_OBJECT (text_view), "indent");
2275 * gtk_text_view_get_indent:
2276 * @text_view: a #GtkTextView
2278 * Gets the default indentation of paragraphs in @text_view.
2279 * Tags in the view's buffer may override the default.
2280 * The indentation may be negative.
2282 * Return value: number of pixels of indentation
2285 gtk_text_view_get_indent (GtkTextView *text_view)
2287 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2289 return text_view->indent;
2293 * gtk_text_view_set_tabs:
2294 * @text_view: a #GtkTextView
2295 * @tabs: tabs as a #PangoTabArray
2297 * Sets the default tab stops for paragraphs in @text_view.
2298 * Tags in the buffer may override the default.
2302 gtk_text_view_set_tabs (GtkTextView *text_view,
2303 PangoTabArray *tabs)
2305 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2307 if (text_view->tabs)
2308 pango_tab_array_free (text_view->tabs);
2310 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2312 if (text_view->layout)
2314 /* some unkosher futzing in internal struct details... */
2315 if (text_view->layout->default_style->tabs)
2316 pango_tab_array_free (text_view->layout->default_style->tabs);
2318 text_view->layout->default_style->tabs =
2319 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2321 gtk_text_layout_default_style_changed (text_view->layout);
2324 g_object_notify (G_OBJECT (text_view), "tabs");
2328 * gtk_text_view_get_tabs:
2329 * @text_view: a #GtkTextView
2331 * Gets the default tabs for @text_view. Tags in the buffer may
2332 * override the defaults. The returned array will be %NULL if
2333 * "standard" (8-space) tabs are used. Free the return value
2334 * with pango_tab_array_free().
2336 * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2339 gtk_text_view_get_tabs (GtkTextView *text_view)
2341 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2343 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2347 * gtk_text_view_set_cursor_visible:
2348 * @text_view: a #GtkTextView
2349 * @setting: whether to show the insertion cursor
2351 * Toggles whether the insertion point is displayed. A buffer with no editable
2352 * text probably shouldn't have a visible cursor, so you may want to turn
2356 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2359 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2361 setting = (setting != FALSE);
2363 if (text_view->cursor_visible != setting)
2365 text_view->cursor_visible = setting;
2367 if (GTK_WIDGET_HAS_FOCUS (text_view))
2369 if (text_view->layout)
2371 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2372 gtk_text_view_check_cursor_blink (text_view);
2376 g_object_notify (G_OBJECT (text_view), "cursor_visible");
2381 * gtk_text_view_get_cursor_visible:
2382 * @text_view: a #GtkTextView
2384 * Find out whether the cursor is being displayed.
2386 * Return value: whether the insertion mark is visible
2389 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2391 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2393 return text_view->cursor_visible;
2398 * gtk_text_view_place_cursor_onscreen:
2399 * @text_view: a #GtkTextView
2401 * Moves the cursor to the currently visible region of the
2402 * buffer, it it isn't there already.
2404 * Return value: TRUE if the cursor had to be moved.
2407 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2411 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2413 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2414 gtk_text_buffer_get_mark (get_buffer (text_view),
2417 if (clamp_iter_onscreen (text_view, &insert))
2419 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2427 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2429 if (text_view->first_validate_idle != 0)
2431 g_source_remove (text_view->first_validate_idle);
2432 text_view->first_validate_idle = 0;
2435 if (text_view->incremental_validate_idle != 0)
2437 g_source_remove (text_view->incremental_validate_idle);
2438 text_view->incremental_validate_idle = 0;
2443 gtk_text_view_destroy (GtkObject *object)
2445 GtkTextView *text_view;
2446 GtkTextLayout *layout;
2448 text_view = GTK_TEXT_VIEW (object);
2450 layout = text_view->layout;
2452 gtk_text_view_remove_validate_idles (text_view);
2453 gtk_text_view_set_buffer (text_view, NULL);
2454 gtk_text_view_destroy_layout (text_view);
2456 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2460 gtk_text_view_finalize (GObject *object)
2462 GtkTextView *text_view;
2464 text_view = GTK_TEXT_VIEW (object);
2466 g_return_if_fail (text_view->buffer == NULL);
2468 gtk_text_view_destroy_layout (text_view);
2469 gtk_text_view_set_buffer (text_view, NULL);
2471 if (text_view->pending_scroll)
2473 free_pending_scroll (text_view->pending_scroll);
2474 text_view->pending_scroll = NULL;
2477 if (text_view->hadjustment)
2478 g_object_unref (G_OBJECT (text_view->hadjustment));
2479 if (text_view->vadjustment)
2480 g_object_unref (G_OBJECT (text_view->vadjustment));
2482 text_window_free (text_view->text_window);
2484 if (text_view->left_window)
2485 text_window_free (text_view->left_window);
2487 if (text_view->top_window)
2488 text_window_free (text_view->top_window);
2490 if (text_view->right_window)
2491 text_window_free (text_view->right_window);
2493 if (text_view->bottom_window)
2494 text_window_free (text_view->bottom_window);
2496 g_object_unref (G_OBJECT (text_view->im_context));
2498 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2502 gtk_text_view_set_property (GObject *object,
2504 const GValue *value,
2507 GtkTextView *text_view;
2509 text_view = GTK_TEXT_VIEW (object);
2513 case PROP_PIXELS_ABOVE_LINES:
2514 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2517 case PROP_PIXELS_BELOW_LINES:
2518 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2521 case PROP_PIXELS_INSIDE_WRAP:
2522 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2526 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2529 case PROP_WRAP_MODE:
2530 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2533 case PROP_JUSTIFICATION:
2534 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2537 case PROP_LEFT_MARGIN:
2538 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2541 case PROP_RIGHT_MARGIN:
2542 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2546 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2550 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2553 case PROP_CURSOR_VISIBLE:
2554 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2558 g_assert_not_reached ();
2564 gtk_text_view_get_property (GObject *object,
2569 GtkTextView *text_view;
2571 text_view = GTK_TEXT_VIEW (object);
2575 case PROP_PIXELS_ABOVE_LINES:
2576 g_value_set_int (value, text_view->pixels_above_lines);
2579 case PROP_PIXELS_BELOW_LINES:
2580 g_value_set_int (value, text_view->pixels_below_lines);
2583 case PROP_PIXELS_INSIDE_WRAP:
2584 g_value_set_int (value, text_view->pixels_inside_wrap);
2588 g_value_set_boolean (value, text_view->editable);
2591 case PROP_WRAP_MODE:
2592 g_value_set_enum (value, text_view->wrap_mode);
2595 case PROP_JUSTIFICATION:
2596 g_value_set_enum (value, text_view->justify);
2599 case PROP_LEFT_MARGIN:
2600 g_value_set_int (value, text_view->left_margin);
2603 case PROP_RIGHT_MARGIN:
2604 g_value_set_int (value, text_view->right_margin);
2608 g_value_set_int (value, text_view->indent);
2612 g_value_set_boxed (value, text_view->tabs);
2615 case PROP_CURSOR_VISIBLE:
2616 g_value_set_boolean (value, text_view->cursor_visible);
2620 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2626 gtk_text_view_size_request (GtkWidget *widget,
2627 GtkRequisition *requisition)
2629 GtkTextView *text_view;
2631 gint focus_edge_width;
2633 gboolean interior_focus;
2635 text_view = GTK_TEXT_VIEW (widget);
2637 gtk_widget_style_get (widget,
2638 "interior_focus", &interior_focus,
2639 "focus_line_width", &focus_width,
2643 focus_edge_width = 0;
2645 focus_edge_width = focus_width;
2647 if (text_view->layout)
2649 text_view->text_window->requisition.width = text_view->layout->width;
2650 text_view->text_window->requisition.height = text_view->layout->height;
2654 text_view->text_window->requisition.width = 0;
2655 text_view->text_window->requisition.height = 0;
2658 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2659 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2661 if (text_view->left_window)
2662 requisition->width += text_view->left_window->requisition.width;
2664 if (text_view->right_window)
2665 requisition->width += text_view->right_window->requisition.width;
2667 if (text_view->top_window)
2668 requisition->height += text_view->top_window->requisition.height;
2670 if (text_view->bottom_window)
2671 requisition->height += text_view->bottom_window->requisition.height;
2673 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2674 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2676 tmp_list = text_view->children;
2677 while (tmp_list != NULL)
2679 GtkTextViewChild *child = tmp_list->data;
2683 GtkRequisition child_req;
2684 GtkRequisition old_req;
2686 gtk_widget_get_child_requisition (child->widget, &old_req);
2688 gtk_widget_size_request (child->widget, &child_req);
2690 gtk_widget_get_child_requisition (child->widget, &child_req);
2692 /* Invalidate layout lines if required */
2693 if (text_view->layout &&
2694 (old_req.width != child_req.width ||
2695 old_req.height != child_req.height))
2696 gtk_text_child_anchor_queue_resize (child->anchor,
2701 GtkRequisition child_req;
2703 gtk_widget_size_request (child->widget, &child_req);
2706 tmp_list = g_slist_next (tmp_list);
2711 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
2712 GtkTextViewChild *vc,
2713 GtkAllocation *allocation)
2719 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2723 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2726 buffer_y += vc->from_top_of_line;
2728 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2729 allocation->y = buffer_y - text_view->yoffset;
2731 gtk_widget_get_child_requisition (vc->widget, &req);
2732 allocation->width = req.width;
2733 allocation->height = req.height;
2737 gtk_text_view_update_child_allocation (GtkTextView *text_view,
2738 GtkTextViewChild *vc)
2740 GtkAllocation allocation;
2742 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2744 gtk_widget_size_allocate (vc->widget, &allocation);
2747 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2749 vc->widget->allocation.x,
2750 vc->widget->allocation.y,
2751 text_view->yoffset);
2756 gtk_text_view_child_allocated (GtkTextLayout *layout,
2762 GtkTextViewChild *vc = NULL;
2763 GtkTextView *text_view = data;
2765 /* x,y is the position of the child from the top of the line, and
2766 * from the left of the buffer. We have to translate that into text
2767 * window coordinates, then size_allocate the child.
2770 vc = g_object_get_data (G_OBJECT (child),
2771 "gtk-text-view-child");
2773 g_assert (vc != NULL);
2775 DV (g_print ("child allocated at %d,%d\n", x, y));
2777 vc->from_left_of_buffer = x;
2778 vc->from_top_of_line = y;
2780 gtk_text_view_update_child_allocation (text_view, vc);
2784 gtk_text_view_allocate_children (GtkTextView *text_view)
2788 DV(g_print(G_STRLOC"\n"));
2790 tmp_list = text_view->children;
2791 while (tmp_list != NULL)
2793 GtkTextViewChild *child = tmp_list->data;
2797 /* We need to force-validate the regions containing
2800 GtkTextIter child_loc;
2801 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2805 gtk_text_layout_validate_yrange (text_view->layout,
2811 GtkAllocation allocation;
2812 GtkRequisition child_req;
2814 g_assert (child != NULL);
2816 allocation.x = child->x;
2817 allocation.y = child->y;
2819 gtk_widget_get_child_requisition (child->widget, &child_req);
2821 allocation.width = child_req.width;
2822 allocation.height = child_req.height;
2824 gtk_widget_size_allocate (child->widget, &allocation);
2827 tmp_list = g_slist_next (tmp_list);
2832 gtk_text_view_size_allocate (GtkWidget *widget,
2833 GtkAllocation *allocation)
2835 GtkTextView *text_view;
2836 GtkTextIter first_para;
2838 GtkAdjustment *vadj;
2839 gboolean yoffset_changed = FALSE;
2841 GdkRectangle text_rect;
2842 GdkRectangle left_rect;
2843 GdkRectangle right_rect;
2844 GdkRectangle top_rect;
2845 GdkRectangle bottom_rect;
2846 gint focus_edge_width;
2848 gboolean interior_focus;
2849 gboolean size_changed;
2851 text_view = GTK_TEXT_VIEW (widget);
2853 DV(g_print(G_STRLOC"\n"));
2856 widget->allocation.width != allocation->width ||
2857 widget->allocation.height != allocation->height;
2859 widget->allocation = *allocation;
2861 if (GTK_WIDGET_REALIZED (widget))
2863 gdk_window_move_resize (widget->window,
2864 allocation->x, allocation->y,
2865 allocation->width, allocation->height);
2868 /* distribute width/height among child windows. Ensure all
2869 * windows get at least a 1x1 allocation.
2872 gtk_widget_style_get (widget,
2873 "interior_focus", &interior_focus,
2874 "focus_line_width", &focus_width,
2878 focus_edge_width = 0;
2880 focus_edge_width = focus_width;
2882 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2884 if (text_view->left_window)
2885 left_rect.width = text_view->left_window->requisition.width;
2887 left_rect.width = 0;
2889 width -= left_rect.width;
2891 if (text_view->right_window)
2892 right_rect.width = text_view->right_window->requisition.width;
2894 right_rect.width = 0;
2896 width -= right_rect.width;
2898 text_rect.width = MAX (1, width);
2900 top_rect.width = text_rect.width;
2901 bottom_rect.width = text_rect.width;
2904 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2906 if (text_view->top_window)
2907 top_rect.height = text_view->top_window->requisition.height;
2909 top_rect.height = 0;
2911 height -= top_rect.height;
2913 if (text_view->bottom_window)
2914 bottom_rect.height = text_view->bottom_window->requisition.height;
2916 bottom_rect.height = 0;
2918 height -= bottom_rect.height;
2920 text_rect.height = MAX (1, height);
2922 left_rect.height = text_rect.height;
2923 right_rect.height = text_rect.height;
2926 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2927 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2929 text_rect.x = left_rect.x + left_rect.width;
2930 text_rect.y = top_rect.y + top_rect.height;
2932 left_rect.y = text_rect.y;
2933 right_rect.y = text_rect.y;
2935 top_rect.x = text_rect.x;
2936 bottom_rect.x = text_rect.x;
2938 right_rect.x = text_rect.x + text_rect.width;
2939 bottom_rect.y = text_rect.y + text_rect.height;
2941 text_window_size_allocate (text_view->text_window,
2944 if (text_view->left_window)
2945 text_window_size_allocate (text_view->left_window,
2948 if (text_view->right_window)
2949 text_window_size_allocate (text_view->right_window,
2952 if (text_view->top_window)
2953 text_window_size_allocate (text_view->top_window,
2956 if (text_view->bottom_window)
2957 text_window_size_allocate (text_view->bottom_window,
2960 gtk_text_view_update_layout_width (text_view);
2962 /* Note that this will do some layout validation */
2963 gtk_text_view_allocate_children (text_view);
2965 /* Now adjust the value of the adjustment to keep the cursor at the
2966 * same place in the buffer
2968 gtk_text_view_get_first_para_iter (text_view, &first_para);
2969 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2971 y += text_view->first_para_pixels;
2973 /* Ensure h/v adj exist */
2974 get_hadjustment (text_view);
2975 get_vadjustment (text_view);
2977 vadj = text_view->vadjustment;
2978 if (y > vadj->upper - vadj->page_size)
2979 y = MAX (0, vadj->upper - vadj->page_size);
2981 if (y != text_view->yoffset)
2983 vadj->value = text_view->yoffset = y;
2984 yoffset_changed = TRUE;
2987 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2988 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) / 2;
2989 text_view->hadjustment->lower = 0;
2990 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2992 gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2994 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2995 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) / 2;
2996 text_view->vadjustment->lower = 0;
2997 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2999 gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
3001 if (yoffset_changed)
3002 gtk_adjustment_value_changed (vadj);
3004 if (text_view->first_validate_idle != 0)
3006 /* The GTK resize loop processes all the pending exposes right
3007 * after doing the resize stuff, so the idle sizer won't have a
3008 * chance to run. So we do the work here.
3011 g_source_remove (text_view->first_validate_idle);
3012 text_view->first_validate_idle = 0;
3014 if (!gtk_text_view_flush_scroll (text_view))
3015 gtk_text_view_validate_onscreen (text_view);
3018 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3021 if (size_changed && GTK_WIDGET_REALIZED (widget))
3022 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3026 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3029 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3030 text_view->first_para_mark);
3034 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3036 GtkWidget *widget = GTK_WIDGET (text_view);
3038 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3040 if (SCREEN_HEIGHT (widget) > 0)
3042 GtkTextIter first_para;
3044 /* Be sure we've validated the stuff onscreen; if we
3045 * scrolled, these calls won't have any effect, because
3046 * they were called in the recursive validate_onscreen
3048 gtk_text_view_get_first_para_iter (text_view, &first_para);
3050 gtk_text_layout_validate_yrange (text_view->layout,
3053 text_view->first_para_pixels +
3054 SCREEN_HEIGHT (widget));
3057 text_view->onscreen_validated = TRUE;
3059 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3061 /* This can have the odd side effect of triggering a scroll, which should
3062 * flip "onscreen_validated" back to FALSE, but should also get us
3063 * back into this function to turn it on again.
3065 gtk_text_view_update_adjustments (text_view);
3067 g_assert (text_view->onscreen_validated);
3071 first_validate_callback (gpointer data)
3073 GtkTextView *text_view = data;
3075 GDK_THREADS_ENTER ();
3077 /* Note that some of this code is duplicated at the end of size_allocate,
3078 * keep in sync with that.
3081 DV(g_print(G_STRLOC"\n"));
3083 /* Do this first, which means that if an "invalidate"
3084 * occurs during any of this process, a new first_validate_callback
3085 * will be installed, and we'll start again.
3087 text_view->first_validate_idle = 0;
3089 /* be sure we have up-to-date screen size set on the
3092 gtk_text_view_update_layout_width (text_view);
3094 /* Bail out if we invalidated stuff; scrolling right away will just
3095 * confuse the issue.
3097 if (text_view->first_validate_idle != 0)
3099 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3103 /* scroll to any marks, if that's pending. This can
3104 * jump us to the validation codepath used for scrolling
3105 * onscreen, if so we bail out.
3107 if (!gtk_text_view_flush_scroll (text_view))
3108 gtk_text_view_validate_onscreen (text_view);
3110 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3112 g_assert (text_view->onscreen_validated);
3116 GDK_THREADS_LEAVE ();
3122 incremental_validate_callback (gpointer data)
3124 GtkTextView *text_view = data;
3125 gboolean result = TRUE;
3127 GDK_THREADS_ENTER ();
3129 DV(g_print(G_STRLOC"\n"));
3131 gtk_text_layout_validate (text_view->layout, 2000);
3133 gtk_text_view_update_adjustments (text_view);
3135 if (gtk_text_layout_is_valid (text_view->layout))
3137 text_view->incremental_validate_idle = 0;
3141 GDK_THREADS_LEAVE ();
3147 gtk_text_view_invalidate (GtkTextView *text_view)
3149 text_view->onscreen_validated = FALSE;
3151 DV(g_print(">Invalidate, onscreen_validated = FALSE ("G_STRLOC")\n"));
3153 if (!text_view->first_validate_idle)
3155 text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3156 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3157 text_view->first_validate_idle));
3160 if (!text_view->incremental_validate_idle)
3162 text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3163 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3164 text_view->incremental_validate_idle));
3169 invalidated_handler (GtkTextLayout *layout,
3172 GtkTextView *text_view;
3174 text_view = GTK_TEXT_VIEW (data);
3176 gtk_text_view_invalidate (text_view);
3180 changed_handler (GtkTextLayout *layout,
3186 GtkTextView *text_view;
3188 GdkRectangle visible_rect;
3189 GdkRectangle redraw_rect;
3191 text_view = GTK_TEXT_VIEW (data);
3192 widget = GTK_WIDGET (data);
3194 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3196 if (GTK_WIDGET_REALIZED (text_view))
3198 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3200 redraw_rect.x = visible_rect.x;
3201 redraw_rect.width = visible_rect.width;
3202 redraw_rect.y = start_y;
3204 if (old_height == new_height)
3205 redraw_rect.height = old_height;
3207 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3209 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3211 /* text_window_invalidate_rect() takes buffer coordinates */
3212 text_window_invalidate_rect (text_view->text_window,
3215 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3219 redraw_rect.height));
3221 if (text_view->left_window)
3222 text_window_invalidate_rect (text_view->left_window,
3224 if (text_view->right_window)
3225 text_window_invalidate_rect (text_view->right_window,
3227 if (text_view->top_window)
3228 text_window_invalidate_rect (text_view->top_window,
3230 if (text_view->bottom_window)
3231 text_window_invalidate_rect (text_view->bottom_window,
3234 gtk_text_view_update_im_spot_location (text_view);
3238 if (old_height != new_height)
3240 gboolean yoffset_changed = FALSE;
3242 int new_first_para_top;
3243 int old_first_para_top;
3246 /* If the bottom of the old area was above the top of the
3247 * screen, we need to scroll to keep the current top of the
3248 * screen in place. Remember that first_para_pixels is the
3249 * position of the top of the screen in coordinates relative to
3250 * the first paragraph onscreen.
3252 * In short we are adding the height delta of the portion of the
3253 * changed region above first_para_mark to text_view->yoffset.
3255 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3256 text_view->first_para_mark);
3258 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3260 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3262 if (new_first_para_top != old_first_para_top)
3264 text_view->yoffset += new_first_para_top - old_first_para_top;
3266 get_vadjustment (text_view)->value = text_view->yoffset;
3267 yoffset_changed = TRUE;
3270 if (yoffset_changed)
3271 gtk_adjustment_value_changed (get_vadjustment (text_view));
3273 /* FIXME be smarter about which anchored widgets we update */
3275 tmp_list = text_view->children;
3276 while (tmp_list != NULL)
3278 GtkTextViewChild *child = tmp_list->data;
3281 gtk_text_view_update_child_allocation (text_view, child);
3283 tmp_list = g_slist_next (tmp_list);
3288 GtkRequisition old_req;
3289 GtkRequisition new_req;
3291 old_req = widget->requisition;
3293 /* Use this instead of gtk_widget_size_request wrapper
3294 * to avoid the optimization which just returns widget->requisition
3295 * if a resize hasn't been queued.
3297 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3299 if (old_req.width != new_req.width ||
3300 old_req.height != new_req.height)
3301 gtk_widget_queue_resize (widget);
3306 gtk_text_view_realize_cursor_gc (GtkTextView *text_view)
3308 GdkColor *cursor_color;
3309 GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
3311 if (text_view->cursor_gc)
3312 gdk_gc_unref (text_view->cursor_gc);
3314 gtk_widget_style_get (GTK_WIDGET (text_view), "cursor_color", &cursor_color, NULL);
3317 cursor_color = &red;
3319 text_view->cursor_gc = gdk_gc_new (text_view->text_window->bin_window);
3320 gdk_gc_set_rgb_fg_color (text_view->cursor_gc, cursor_color);
3324 gtk_text_view_realize (GtkWidget *widget)
3326 GtkTextView *text_view;
3327 GdkWindowAttr attributes;
3328 gint attributes_mask;
3331 text_view = GTK_TEXT_VIEW (widget);
3332 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3334 attributes.window_type = GDK_WINDOW_CHILD;
3335 attributes.x = widget->allocation.x;
3336 attributes.y = widget->allocation.y;
3337 attributes.width = widget->allocation.width;
3338 attributes.height = widget->allocation.height;
3339 attributes.wclass = GDK_INPUT_OUTPUT;
3340 attributes.visual = gtk_widget_get_visual (widget);
3341 attributes.colormap = gtk_widget_get_colormap (widget);
3342 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3344 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3346 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3347 &attributes, attributes_mask);
3348 gdk_window_set_user_data (widget->window, widget);
3350 /* must come before text_window_realize calls */
3351 widget->style = gtk_style_attach (widget->style, widget->window);
3353 gdk_window_set_background (widget->window,
3354 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3356 text_window_realize (text_view->text_window, widget->window);
3358 if (text_view->left_window)
3359 text_window_realize (text_view->left_window,
3362 if (text_view->top_window)
3363 text_window_realize (text_view->top_window,
3366 if (text_view->right_window)
3367 text_window_realize (text_view->right_window,
3370 if (text_view->bottom_window)
3371 text_window_realize (text_view->bottom_window,
3374 gtk_text_view_realize_cursor_gc (text_view);
3376 gtk_text_view_ensure_layout (text_view);
3378 if (text_view->buffer)
3379 gtk_text_buffer_add_selection_clipboard (text_view->buffer,
3380 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3382 tmp_list = text_view->children;
3383 while (tmp_list != NULL)
3385 GtkTextViewChild *vc = tmp_list->data;
3387 text_view_child_set_parent_window (text_view, vc);
3389 tmp_list = tmp_list->next;
3394 gtk_text_view_unrealize (GtkWidget *widget)
3396 GtkTextView *text_view;
3398 text_view = GTK_TEXT_VIEW (widget);
3400 if (text_view->buffer)
3401 gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
3402 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3404 if (text_view->cursor_gc)
3406 gdk_gc_unref (text_view->cursor_gc);
3407 text_view->cursor_gc = NULL;
3410 gtk_text_view_remove_validate_idles (text_view);
3412 if (text_view->popup_menu)
3414 gtk_widget_destroy (text_view->popup_menu);
3415 text_view->popup_menu = NULL;
3418 text_window_unrealize (text_view->text_window);
3420 if (text_view->left_window)
3421 text_window_unrealize (text_view->left_window);
3423 if (text_view->top_window)
3424 text_window_unrealize (text_view->top_window);
3426 if (text_view->right_window)
3427 text_window_unrealize (text_view->right_window);
3429 if (text_view->bottom_window)
3430 text_window_unrealize (text_view->bottom_window);
3432 gtk_text_view_destroy_layout (text_view);
3434 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3438 gtk_text_view_style_set (GtkWidget *widget,
3439 GtkStyle *previous_style)
3441 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3443 if (GTK_WIDGET_REALIZED (widget))
3445 gdk_window_set_background (widget->window,
3446 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3448 gdk_window_set_background (text_view->text_window->bin_window,
3449 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3451 if (text_view->left_window)
3452 gdk_window_set_background (text_view->left_window->bin_window,
3453 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3454 if (text_view->right_window)
3455 gdk_window_set_background (text_view->right_window->bin_window,
3456 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3458 if (text_view->top_window)
3459 gdk_window_set_background (text_view->top_window->bin_window,
3460 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3462 if (text_view->bottom_window)
3463 gdk_window_set_background (text_view->bottom_window->bin_window,
3464 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3466 gtk_text_view_realize_cursor_gc (text_view);
3469 if (text_view->layout && previous_style)
3471 gtk_text_view_set_attributes_from_style (text_view,
3472 text_view->layout->default_style,
3474 gtk_text_layout_default_style_changed (text_view->layout);
3479 gtk_text_view_direction_changed (GtkWidget *widget,
3480 GtkTextDirection previous_direction)
3482 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3484 if (text_view->layout)
3486 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3487 gtk_text_layout_default_style_changed (text_view->layout);
3493 set_invisible_cursor (GdkWindow *window)
3495 GdkBitmap *empty_bitmap;
3498 char invisible_cursor_bits[] = { 0x0 };
3500 useless.red = useless.green = useless.blue = 0;
3503 empty_bitmap = gdk_bitmap_create_from_data (window,
3504 invisible_cursor_bits,
3507 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3512 gdk_window_set_cursor (window, cursor);
3514 gdk_cursor_unref (cursor);
3516 g_object_unref (empty_bitmap);
3520 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3522 if (text_view->mouse_cursor_obscured)
3525 set_invisible_cursor (text_view->text_window->bin_window);
3527 text_view->mouse_cursor_obscured = TRUE;
3535 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3538 switch (event->type)
3540 case GDK_MOTION_NOTIFY:
3541 *x = event->motion.x;
3542 *y = event->motion.y;
3546 case GDK_BUTTON_PRESS:
3547 case GDK_2BUTTON_PRESS:
3548 case GDK_3BUTTON_PRESS:
3549 case GDK_BUTTON_RELEASE:
3550 *x = event->button.x;
3551 *y = event->button.y;
3556 case GDK_KEY_RELEASE:
3557 case GDK_ENTER_NOTIFY:
3558 case GDK_LEAVE_NOTIFY:
3559 case GDK_PROPERTY_NOTIFY:
3560 case GDK_SELECTION_CLEAR:
3561 case GDK_SELECTION_REQUEST:
3562 case GDK_SELECTION_NOTIFY:
3563 case GDK_PROXIMITY_IN:
3564 case GDK_PROXIMITY_OUT:
3565 case GDK_DRAG_ENTER:
3566 case GDK_DRAG_LEAVE:
3567 case GDK_DRAG_MOTION:
3568 case GDK_DRAG_STATUS:
3569 case GDK_DROP_START:
3570 case GDK_DROP_FINISHED:
3580 emit_event_on_tags (GtkWidget *widget,
3586 gboolean retval = FALSE;
3587 GtkTextView *text_view;
3589 text_view = GTK_TEXT_VIEW (widget);
3591 tags = gtk_text_iter_get_tags (iter);
3596 GtkTextTag *tag = tmp->data;
3598 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3604 tmp = g_slist_next (tmp);
3607 g_slist_free (tags);
3613 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3615 GtkTextView *text_view;
3618 text_view = GTK_TEXT_VIEW (widget);
3620 if (text_view->layout == NULL ||
3621 get_buffer (text_view) == NULL)
3624 if (event->any.window != text_view->text_window->bin_window)
3627 if (get_event_coordinates (event, &x, &y))
3631 x += text_view->xoffset;
3632 y += text_view->yoffset;
3634 /* FIXME this is slow and we do it twice per event.
3635 * My favorite solution is to have GtkTextLayout cache
3636 * the last couple lookups.
3638 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3642 return emit_event_on_tags (widget, event, &iter);
3644 else if (event->type == GDK_KEY_PRESS ||
3645 event->type == GDK_KEY_RELEASE)
3647 GtkTextMark *insert;
3650 insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3653 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3655 return emit_event_on_tags (widget, event, &iter);
3662 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3664 gboolean retval = FALSE;
3665 gboolean obscure = FALSE;
3666 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3667 GtkTextMark *insert;
3670 if (text_view->layout == NULL ||
3671 get_buffer (text_view) == NULL)
3674 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3675 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3676 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3677 gtk_im_context_filter_keypress (text_view->im_context, event))
3679 text_view->need_im_reset = TRUE;
3683 else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3684 GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3686 else if (event->keyval == GDK_Return ||
3687 event->keyval == GDK_KP_Enter)
3689 gtk_text_view_commit_text (text_view, "\n");
3694 /* Pass through Tab as literal tab, unless Control is held down */
3695 else if ((event->keyval == GDK_Tab ||
3696 event->keyval == GDK_KP_Tab ||
3697 event->keyval == GDK_ISO_Left_Tab) &&
3698 !(event->state & GDK_CONTROL_MASK))
3700 /* If the text isn't editable, move the focus instead */
3701 if (text_view->editable)
3703 gtk_text_view_commit_text (text_view, "\t");
3707 gtk_text_view_move_focus (text_view,
3708 (event->state & GDK_SHIFT_MASK) ?
3709 GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3717 gtk_text_view_obscure_mouse_cursor (text_view);
3719 gtk_text_view_pend_cursor_blink (text_view);
3725 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3727 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3728 GtkTextMark *insert;
3731 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3734 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3735 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3736 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3737 gtk_im_context_filter_keypress (text_view->im_context, event))
3739 text_view->need_im_reset = TRUE;
3743 return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3747 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3749 GtkTextView *text_view;
3751 text_view = GTK_TEXT_VIEW (widget);
3753 text_view->disable_scroll_on_focus = TRUE;
3754 gtk_widget_grab_focus (widget);
3755 text_view->disable_scroll_on_focus = FALSE;
3757 if (event->window != text_view->text_window->bin_window)
3759 /* Remove selection if any. */
3760 gtk_text_view_unselect (text_view);
3766 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3767 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3768 else if (event->button == 3)
3769 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3772 if (event->type == GDK_BUTTON_PRESS)
3774 gtk_text_view_reset_im_context (text_view);
3776 if (event->button == 1)
3778 /* If we're in the selection, start a drag copy/move of the
3779 * selection; otherwise, start creating a new selection.
3782 GtkTextIter start, end;
3784 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3786 event->x + text_view->xoffset,
3787 event->y + text_view->yoffset);
3789 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3791 gtk_text_iter_in_range (&iter, &start, &end))
3793 text_view->drag_start_x = event->x;
3794 text_view->drag_start_y = event->y;
3795 text_view->pending_place_cursor_button = event->button;
3799 gtk_text_view_start_selection_drag (text_view, &iter, event);
3804 else if (event->button == 2)
3808 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3810 event->x + text_view->xoffset,
3811 event->y + text_view->yoffset);
3813 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3814 gtk_clipboard_get (GDK_SELECTION_PRIMARY),
3816 text_view->editable);
3819 else if (event->button == 3)
3821 gtk_text_view_do_popup (text_view, event);
3824 else if ((event->type == GDK_2BUTTON_PRESS ||
3825 event->type == GDK_3BUTTON_PRESS) &&
3828 GtkTextIter start, end;
3830 /* End the selection drag, otherwise we'd clear the new
3831 * word/line selection on button release
3833 gtk_text_view_end_selection_drag (text_view, event);
3835 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3837 event->x + text_view->xoffset,
3838 event->y + text_view->yoffset);
3842 if (event->type == GDK_2BUTTON_PRESS)
3844 if (gtk_text_iter_inside_word (&start))
3846 if (!gtk_text_iter_starts_word (&start))
3847 gtk_text_iter_backward_word_start (&start);
3849 if (!gtk_text_iter_ends_word (&end))
3850 gtk_text_iter_forward_word_end (&end);
3853 else if (event->type == GDK_3BUTTON_PRESS)
3855 if (gtk_text_view_starts_display_line (text_view, &start))
3857 /* If on a display line boundary, we assume the user
3858 * clicked off the end of a line and we therefore select
3859 * the line before the boundary.
3861 gtk_text_view_backward_display_line_start (text_view, &start);
3865 /* start isn't on the start of a line, so we move it to the
3866 * start, and move end to the end unless it's already there.
3868 gtk_text_view_backward_display_line_start (text_view, &start);
3870 if (!gtk_text_view_starts_display_line (text_view, &end))
3871 gtk_text_view_forward_display_line_end (text_view, &end);
3875 if (event->state & GDK_SHIFT_MASK)
3877 /* Take union of old and new selection */
3878 GtkTextIter old_start, old_end;
3880 gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3881 &old_start, &old_end);
3883 gtk_text_iter_order (&start, &old_start);
3884 gtk_text_iter_order (&old_end, &end);
3886 /* Now start is the first of the starts, and end is the
3891 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3894 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3898 text_view->just_selected_element = TRUE;
3907 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3909 GtkTextView *text_view;
3911 text_view = GTK_TEXT_VIEW (widget);
3913 if (event->window != text_view->text_window->bin_window)
3916 if (event->button == 1)
3918 if (text_view->drag_start_x >= 0)
3920 text_view->drag_start_x = -1;
3921 text_view->drag_start_y = -1;
3924 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3926 else if (text_view->just_selected_element)
3928 text_view->just_selected_element = FALSE;
3931 else if (text_view->pending_place_cursor_button == event->button)
3935 /* Unselect everything; we clicked inside selection, but
3936 * didn't move by the drag threshold, so just clear selection
3939 gtk_text_layout_get_iter_at_pixel (text_view->layout,
3941 event->x + text_view->xoffset,
3942 event->y + text_view->yoffset);
3944 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3946 text_view->pending_place_cursor_button = 0;
3956 keymap_direction_changed (GdkKeymap *keymap,
3957 GtkTextView *text_view)
3959 gtk_text_view_check_keymap_direction (text_view);
3963 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3965 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3967 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
3968 gtk_widget_queue_draw (widget);
3970 DV(g_print (G_STRLOC": focus_in_event\n"));
3972 if (text_view->cursor_visible && text_view->layout)
3974 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3975 gtk_text_view_check_cursor_blink (text_view);
3978 g_signal_connect (gdk_keymap_get_default (),
3979 "direction_changed",
3980 G_CALLBACK (keymap_direction_changed), text_view);
3981 gtk_text_view_check_keymap_direction (text_view);
3983 text_view->need_im_reset = TRUE;
3984 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3990 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3992 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3994 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
3995 gtk_widget_queue_draw (widget);
3997 DV(g_print (G_STRLOC": focus_out_event\n"));
3999 if (text_view->cursor_visible && text_view->layout)
4001 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4002 gtk_text_view_check_cursor_blink (text_view);
4005 g_signal_handlers_disconnect_by_func (gdk_keymap_get_default (),
4006 keymap_direction_changed,
4009 text_view->need_im_reset = TRUE;
4010 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4016 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4018 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4020 if (text_view->mouse_cursor_obscured)
4024 cursor = gdk_cursor_new (GDK_XTERM);
4025 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4026 gdk_cursor_unref (cursor);
4027 text_view->mouse_cursor_obscured = FALSE;
4030 if (event->window == text_view->text_window->bin_window &&
4031 text_view->drag_start_x >= 0)
4035 gdk_window_get_pointer (text_view->text_window->bin_window,
4038 if (gtk_drag_check_threshold (widget,
4039 text_view->drag_start_x,
4040 text_view->drag_start_y,
4044 gint buffer_x, buffer_y;
4046 gtk_text_view_window_to_buffer_coords (text_view,
4047 GTK_TEXT_WINDOW_TEXT,
4048 text_view->drag_start_x,
4049 text_view->drag_start_y,
4053 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4055 buffer_x, buffer_y);
4057 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4066 gtk_text_view_paint (GtkWidget *widget,
4068 GdkEventExpose *event)
4070 GtkTextView *text_view;
4071 GList *child_exposes;
4074 text_view = GTK_TEXT_VIEW (widget);
4076 g_return_if_fail (text_view->layout != NULL);
4077 g_return_if_fail (text_view->xoffset >= 0);
4078 g_return_if_fail (text_view->yoffset >= 0);
4080 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4081 text_view->first_validate_idle));
4083 if (!text_view->onscreen_validated)
4085 g_warning (G_STRLOC ": somehow some text lines were modified or scrolling occurred since the last validation of lines on the screen - this is a known bug, no need to report.");
4090 printf ("painting %d,%d %d x %d\n",
4092 area->width, area->height);
4095 child_exposes = NULL;
4096 gtk_text_layout_draw (text_view->layout,
4098 text_view->text_window->bin_window,
4099 text_view->cursor_gc,
4103 area->width, area->height,
4106 tmp_list = child_exposes;
4107 while (tmp_list != NULL)
4109 GtkWidget *child = tmp_list->data;
4111 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4115 g_object_unref (G_OBJECT (child));
4117 tmp_list = tmp_list->next;
4120 g_list_free (child_exposes);
4124 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4128 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4129 GTK_TEXT_WINDOW_TEXT))
4131 DV(g_print (">Exposed ("G_STRLOC")\n"));
4132 gtk_text_view_paint (widget, &event->area, event);
4135 if (event->window == widget->window)
4136 gtk_text_view_draw_focus (widget);
4138 /* Propagate exposes to all children not in the buffer. */
4139 tmp_list = GTK_TEXT_VIEW (widget)->children;
4140 while (tmp_list != NULL)
4142 GtkTextViewChild *vc = tmp_list->data;
4144 /* propagate_expose checks that event->window matches
4147 if (vc->type != GTK_TEXT_WINDOW_TEXT)
4148 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4152 tmp_list = tmp_list->next;
4159 gtk_text_view_draw_focus (GtkWidget *widget)
4161 gboolean interior_focus;
4163 /* We clear the focus if we are in interior focus mode. */
4164 gtk_widget_style_get (widget,
4165 "interior_focus", &interior_focus,
4168 if (GTK_WIDGET_DRAWABLE (widget))
4170 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4172 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4173 NULL, widget, "textview",
4175 widget->allocation.width,
4176 widget->allocation.height);
4180 gdk_window_clear (widget->window);
4186 gtk_text_view_grab_focus (GtkWidget *widget)
4188 GtkTextView *text_view;
4190 text_view = GTK_TEXT_VIEW (widget);
4192 GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4194 if (!text_view->disable_scroll_on_focus)
4195 gtk_text_view_scroll_mark_onscreen (text_view,
4196 gtk_text_buffer_get_mark (get_buffer (text_view),
4201 gtk_text_view_focus (GtkWidget *widget,
4202 GtkDirectionType direction)
4204 GtkTextView *text_view;
4205 GtkContainer *container;
4207 text_view = GTK_TEXT_VIEW (widget);
4208 container = GTK_CONTAINER (widget);
4210 return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4218 gtk_text_view_add (GtkContainer *container,
4221 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4222 g_return_if_fail (GTK_IS_WIDGET (child));
4224 /* This is pretty random. */
4225 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4227 GTK_TEXT_WINDOW_WIDGET,
4232 gtk_text_view_remove (GtkContainer *container,
4236 GtkTextView *text_view;
4237 GtkTextViewChild *vc;
4239 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4240 g_return_if_fail (GTK_IS_WIDGET (child));
4241 g_return_if_fail (child->parent == (GtkWidget*) container);
4243 text_view = GTK_TEXT_VIEW (container);
4246 iter = text_view->children;
4248 while (iter != NULL)
4252 if (vc->widget == child)
4255 iter = g_slist_next (iter);
4258 g_assert (iter != NULL); /* be sure we had the child in the list */
4260 text_view->children = g_slist_remove (text_view->children, vc);
4262 gtk_widget_unparent (vc->widget);
4264 text_view_child_free (vc);
4268 gtk_text_view_forall (GtkContainer *container,
4269 gboolean include_internals,
4270 GtkCallback callback,
4271 gpointer callback_data)
4274 GtkTextView *text_view;
4277 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4278 g_return_if_fail (callback != NULL);
4280 text_view = GTK_TEXT_VIEW (container);
4282 copy = g_slist_copy (text_view->children);
4285 while (iter != NULL)
4287 GtkTextViewChild *vc = iter->data;
4289 (* callback) (vc->widget, callback_data);
4291 iter = g_slist_next (iter);
4294 g_slist_free (copy);
4297 #define CURSOR_ON_MULTIPLIER 0.66
4298 #define CURSOR_OFF_MULTIPLIER 0.34
4299 #define CURSOR_PEND_MULTIPLIER 1.0
4302 cursor_blinks (GtkTextView *text_view)
4304 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4307 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4310 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4313 g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4318 get_cursor_time (GtkTextView *text_view)
4320 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4323 g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4333 blink_cb (gpointer data)
4335 GtkTextView *text_view;
4338 GDK_THREADS_ENTER ();
4340 text_view = GTK_TEXT_VIEW (data);
4342 g_assert (text_view->layout);
4343 g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4344 g_assert (text_view->cursor_visible);
4346 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4349 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4353 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4357 gtk_text_layout_set_cursor_visible (text_view->layout,
4360 GDK_THREADS_LEAVE ();
4362 /* Remove ourselves */
4368 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4370 if (text_view->blink_timeout)
4372 gtk_timeout_remove (text_view->blink_timeout);
4373 text_view->blink_timeout = 0;
4378 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4380 if (text_view->layout != NULL &&
4381 text_view->cursor_visible &&
4382 GTK_WIDGET_HAS_FOCUS (text_view))
4384 if (cursor_blinks (text_view))
4386 if (text_view->blink_timeout == 0)
4388 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4390 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4396 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4400 gtk_text_view_stop_cursor_blink (text_view);
4405 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4407 if (text_view->layout != NULL &&
4408 text_view->cursor_visible &&
4409 GTK_WIDGET_HAS_FOCUS (text_view) &&
4410 cursor_blinks (text_view))
4412 if (text_view->blink_timeout != 0)
4414 gtk_timeout_remove (text_view->blink_timeout);
4415 text_view->blink_timeout = 0;
4418 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4420 text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4428 * Key binding handlers
4432 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4433 GtkTextIter *newplace,
4438 gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4444 gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4449 /* FIXME when we are unfrozen and can change GtkMovementStep,
4452 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4455 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4456 GtkMovementStep step,
4458 gboolean extend_selection)
4461 GtkTextIter newplace;
4463 gint cursor_x_pos = 0;
4465 gtk_text_view_reset_im_context (text_view);
4467 if (step == GTK_MOVEMENT_PAGES)
4469 gtk_text_view_scroll_pages (text_view, count);
4470 gtk_text_view_pend_cursor_blink (text_view);
4473 else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4475 gtk_text_view_scroll_hpages (text_view, count);
4476 gtk_text_view_pend_cursor_blink (text_view);
4480 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4481 gtk_text_buffer_get_mark (get_buffer (text_view),
4485 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4486 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4490 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4491 gtk_text_iter_forward_cursor_positions (&newplace, count);
4494 case GTK_MOVEMENT_VISUAL_POSITIONS:
4495 gtk_text_layout_move_iter_visually (text_view->layout,
4499 case GTK_MOVEMENT_WORDS:
4501 gtk_text_iter_backward_word_starts (&newplace, -count);
4503 gtk_text_iter_forward_word_ends (&newplace, count);
4506 case GTK_MOVEMENT_DISPLAY_LINES:
4507 gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4508 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4511 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4513 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4514 else if (count < -1)
4515 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4518 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4521 case GTK_MOVEMENT_PARAGRAPHS:
4524 if (!gtk_text_iter_ends_line (&newplace))
4526 gtk_text_iter_forward_to_line_end (&newplace);
4532 if (gtk_text_iter_get_line_offset (&newplace) > 0)
4534 gtk_text_iter_set_line_offset (&newplace, 0);
4541 gtk_text_iter_forward_lines (&newplace, count);
4542 gtk_text_iter_set_line_offset (&newplace, 0);
4546 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4549 if (!gtk_text_iter_ends_line (&newplace))
4550 gtk_text_iter_forward_to_line_end (&newplace);
4554 gtk_text_iter_set_line_offset (&newplace, 0);
4558 case GTK_MOVEMENT_BUFFER_ENDS:
4560 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4562 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4569 if (!gtk_text_iter_equal (&insert, &newplace))
4571 if (extend_selection)
4572 gtk_text_buffer_move_mark (get_buffer (text_view),
4573 gtk_text_buffer_get_mark (get_buffer (text_view),
4577 gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4579 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4580 gtk_text_view_scroll_mark_onscreen (text_view,
4581 gtk_text_buffer_get_mark (get_buffer (text_view),
4584 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4586 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4590 gtk_text_view_pend_cursor_blink (text_view);
4594 gtk_text_view_move_cursor (GtkTextView *text_view,
4595 GtkMovementStep step,
4597 gboolean extend_selection)
4599 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4603 gtk_text_view_page_horizontally (GtkTextView *text_view,
4605 gboolean extend_selection)
4607 gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4608 count, extend_selection);
4612 gtk_text_view_set_anchor (GtkTextView *text_view)
4616 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4617 gtk_text_buffer_get_mark (get_buffer (text_view),
4620 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4624 gtk_text_view_scroll_pages (GtkTextView *text_view,
4630 gint cursor_x_pos, cursor_y_pos;
4631 GtkTextIter new_insert;
4635 g_return_if_fail (text_view->vadjustment != NULL);
4637 adj = text_view->vadjustment;
4639 /* Validate the region that will be brought into view by the cursor motion
4643 gtk_text_view_get_first_para_iter (text_view, &anchor);
4644 y0 = adj->page_size;
4645 y1 = adj->page_size + count * adj->page_increment;
4649 gtk_text_view_get_first_para_iter (text_view, &anchor);
4650 y0 = count * adj->page_increment + adj->page_size;
4654 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4655 /* FIXME do we need to update the adjustment ranges here? */
4657 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4659 /* already at top, just be sure we are at offset 0 */
4660 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4661 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4663 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4665 /* already at bottom, just be sure we are at the end */
4666 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4667 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4671 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4673 newval = adj->value;
4674 oldval = adj->value;
4676 newval += count * adj->page_increment;
4678 set_adjustment_clamped (adj, newval);
4679 cursor_y_pos += adj->value - oldval;
4681 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4682 clamp_iter_onscreen (text_view, &new_insert);
4683 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4685 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4688 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4689 * only guarantees 1 pixel onscreen.
4691 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4692 gtk_text_view_scroll_mark_onscreen (text_view,
4693 gtk_text_buffer_get_mark (get_buffer (text_view),
4698 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4704 gint cursor_x_pos, cursor_y_pos;
4705 GtkTextIter new_insert;
4708 g_return_if_fail (text_view->hadjustment != NULL);
4710 adj = text_view->hadjustment;
4712 /* Validate the line that we're moving within.
4714 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4716 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4717 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4718 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4719 /* FIXME do we need to update the adjustment ranges here? */
4721 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4723 /* already at far left, just be sure we are at offset 0 */
4724 gtk_text_iter_set_line_offset (&new_insert, 0);
4725 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4727 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4729 /* already at far right, just be sure we are at the end */
4730 gtk_text_iter_forward_to_line_end (&new_insert);
4731 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4735 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4737 newval = adj->value;
4738 oldval = adj->value;
4740 newval += count * adj->page_increment;
4742 set_adjustment_clamped (adj, newval);
4743 cursor_x_pos += adj->value - oldval;
4745 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4746 clamp_iter_onscreen (text_view, &new_insert);
4747 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4749 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4752 /* FIXME for lines shorter than the overall widget width, this results in a
4753 * "bounce" effect as we scroll to the right of the widget, then scroll
4754 * back to get the end of the line onscreen.
4755 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4758 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4759 * only guarantees 1 pixel onscreen.
4761 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4762 gtk_text_view_scroll_mark_onscreen (text_view,
4763 gtk_text_buffer_get_mark (get_buffer (text_view),
4768 whitespace (gunichar ch, gpointer user_data)
4770 return (ch == ' ' || ch == '\t');
4774 not_whitespace (gunichar ch, gpointer user_data)
4776 return !whitespace (ch, user_data);
4780 find_whitepace_region (const GtkTextIter *center,
4781 GtkTextIter *start, GtkTextIter *end)
4786 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4787 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4788 if (whitespace (gtk_text_iter_get_char (end), NULL))
4789 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4791 return !gtk_text_iter_equal (start, end);
4795 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4798 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4799 text_view->editable);
4803 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4810 gboolean leave_one = FALSE;
4812 gtk_text_view_reset_im_context (text_view);
4814 if (type == GTK_DELETE_CHARS)
4816 /* Char delete deletes the selection, if one exists */
4817 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4818 text_view->editable))
4822 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4824 gtk_text_buffer_get_mark (get_buffer (text_view),
4832 case GTK_DELETE_CHARS:
4833 gtk_text_iter_forward_cursor_positions (&end, count);
4836 case GTK_DELETE_WORD_ENDS:
4838 gtk_text_iter_forward_word_ends (&end, count);
4840 gtk_text_iter_backward_word_starts (&start, 0 - count);
4843 case GTK_DELETE_WORDS:
4846 case GTK_DELETE_DISPLAY_LINE_ENDS:
4849 case GTK_DELETE_DISPLAY_LINES:
4852 case GTK_DELETE_PARAGRAPH_ENDS:
4853 /* If we're already at a newline, we need to
4854 * simply delete that newline, instead of
4855 * moving to the next one.
4857 if (gtk_text_iter_ends_line (&end))
4859 gtk_text_iter_forward_line (&end);
4865 if (!gtk_text_iter_forward_to_line_end (&end))
4871 /* FIXME figure out what a negative count means
4875 case GTK_DELETE_PARAGRAPHS:
4878 gtk_text_iter_set_line_offset (&start, 0);
4879 gtk_text_iter_forward_to_line_end (&end);
4881 /* Do the lines beyond the first. */
4884 gtk_text_iter_forward_to_line_end (&end);
4890 /* FIXME negative count? */
4894 case GTK_DELETE_WHITESPACE:
4896 find_whitepace_region (&insert, &start, &end);
4904 if (!gtk_text_iter_equal (&start, &end))
4906 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4908 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4909 text_view->editable))
4912 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4914 text_view->editable);
4917 gtk_text_buffer_end_user_action (get_buffer (text_view));
4919 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4920 gtk_text_view_scroll_mark_onscreen (text_view,
4921 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4926 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4928 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4929 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4930 text_view->editable);
4931 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4932 gtk_text_view_scroll_mark_onscreen (text_view,
4933 gtk_text_buffer_get_mark (get_buffer (text_view),
4938 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4940 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4941 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
4942 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4943 gtk_text_view_scroll_mark_onscreen (text_view,
4944 gtk_text_buffer_get_mark (get_buffer (text_view),
4949 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4951 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4952 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4954 text_view->editable);
4955 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4956 gtk_text_view_scroll_mark_onscreen (text_view,
4957 gtk_text_buffer_get_mark (get_buffer (text_view),
4962 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4964 text_view->overwrite_mode = !text_view->overwrite_mode;
4968 gtk_text_view_move_focus (GtkTextView *text_view,
4969 GtkDirectionType direction_type)
4971 GtkWidget *toplevel;
4974 gtk_widget_get_ancestor (GTK_WIDGET (text_view), GTK_TYPE_WINDOW);
4976 if (toplevel == NULL)
4979 /* Propagate to toplevel */
4980 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus",
4989 gtk_text_view_unselect (GtkTextView *text_view)
4993 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4995 gtk_text_buffer_get_mark (get_buffer (text_view),
4998 gtk_text_buffer_move_mark (get_buffer (text_view),
4999 gtk_text_buffer_get_mark (get_buffer (text_view),
5005 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5006 const gchar *mark_name)
5009 GdkModifierType state;
5010 GtkTextIter newplace;
5012 /* DV(g_print (G_STRLOC": begin\n")); */
5014 gdk_window_get_pointer (text_view->text_window->bin_window,
5017 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5018 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5020 x + text_view->xoffset,
5021 y + text_view->yoffset);
5025 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5027 DV(g_print (G_STRLOC": move mark\n"));
5028 gtk_text_buffer_move_mark (get_buffer (text_view),
5032 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5033 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5038 selection_scan_timeout (gpointer data)
5040 GtkTextView *text_view;
5042 GDK_THREADS_ENTER ();
5044 text_view = GTK_TEXT_VIEW (data);
5046 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5047 move_mark_to_pointer_and_scroll (text_view, "insert");
5049 GDK_THREADS_LEAVE ();
5051 return TRUE; /* remain installed. */
5054 #define DND_SCROLL_MARGIN 0.20
5057 drag_scan_timeout (gpointer data)
5059 GtkTextView *text_view;
5061 GdkModifierType state;
5062 GtkTextIter newplace;
5064 GDK_THREADS_ENTER ();
5066 text_view = GTK_TEXT_VIEW (data);
5068 gdk_window_get_pointer (text_view->text_window->bin_window,
5071 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5073 x + text_view->xoffset,
5074 y + text_view->yoffset);
5076 gtk_text_buffer_move_mark (get_buffer (text_view),
5077 text_view->dnd_mark,
5080 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5081 gtk_text_view_scroll_to_mark (text_view,
5082 text_view->dnd_mark,
5083 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5085 GDK_THREADS_LEAVE ();
5091 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5093 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5094 move_mark_to_pointer_and_scroll (text_view, "insert");
5096 /* If we had to scroll offscreen, insert a timeout to do so
5097 * again. Note that in the timeout, even if the mouse doesn't
5098 * move, due to this scroll xoffset/yoffset will have changed
5099 * and we'll need to scroll again.
5101 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5102 gtk_timeout_remove (text_view->scroll_timeout);
5104 text_view->scroll_timeout =
5105 gtk_timeout_add (50, selection_scan_timeout, text_view);
5111 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5112 const GtkTextIter *iter,
5113 GdkEventButton *button)
5115 GtkTextIter newplace;
5116 GtkTextBuffer *buffer;
5118 g_return_if_fail (text_view->selection_drag_handler == 0);
5120 gtk_grab_add (GTK_WIDGET (text_view));
5122 buffer = get_buffer (text_view);
5126 if (button->state & GDK_SHIFT_MASK)
5128 /* Extend selection */
5129 GtkTextIter start, end;
5131 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5133 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5135 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5138 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5141 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5143 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5146 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5152 /* Replace selection */
5153 gtk_text_buffer_place_cursor (buffer, &newplace);
5156 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5157 "motion_notify_event",
5158 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5162 /* returns whether we were really dragging */
5164 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5166 if (text_view->selection_drag_handler == 0)
5169 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5170 text_view->selection_drag_handler = 0;
5172 if (text_view->scroll_timeout != 0)
5174 gtk_timeout_remove (text_view->scroll_timeout);
5175 text_view->scroll_timeout = 0;
5178 /* one last update to current position */
5179 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5180 move_mark_to_pointer_and_scroll (text_view, "insert");
5182 gtk_grab_remove (GTK_WIDGET (text_view));
5192 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5193 GtkTextAttributes *values,
5196 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5197 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5200 pango_font_description_free (values->font);
5202 values->font = pango_font_description_copy (style->font_desc);
5206 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5208 if (text_view->layout)
5210 gboolean split_cursor;
5211 GtkTextDirection new_dir;
5212 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5214 g_object_get (G_OBJECT (settings),
5215 "gtk-split-cursor", &split_cursor,
5218 new_dir = GTK_TEXT_DIR_NONE;
5220 new_dir = (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
5221 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5223 if (text_view->layout->cursor_direction != new_dir)
5224 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5229 gtk_text_view_ensure_layout (GtkTextView *text_view)
5233 widget = GTK_WIDGET (text_view);
5235 if (text_view->layout == NULL)
5237 GtkTextAttributes *style;
5238 PangoContext *ltr_context, *rtl_context;
5241 DV(g_print(G_STRLOC"\n"));
5243 text_view->layout = gtk_text_layout_new ();
5245 g_signal_connect (G_OBJECT (text_view->layout),
5247 G_CALLBACK (invalidated_handler),
5250 g_signal_connect (G_OBJECT (text_view->layout),
5252 G_CALLBACK (changed_handler),
5255 g_signal_connect (G_OBJECT (text_view->layout),
5257 G_CALLBACK (gtk_text_view_child_allocated),
5260 if (get_buffer (text_view))
5261 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5263 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5264 gtk_text_view_pend_cursor_blink (text_view);
5266 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5268 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5269 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5270 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5271 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5273 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5275 g_object_unref (G_OBJECT (ltr_context));
5276 g_object_unref (G_OBJECT (rtl_context));
5278 gtk_text_view_check_keymap_direction (text_view);
5280 style = gtk_text_attributes_new ();
5282 gtk_widget_ensure_style (widget);
5283 gtk_text_view_set_attributes_from_style (text_view,
5284 style, widget->style);
5286 style->pixels_above_lines = text_view->pixels_above_lines;
5287 style->pixels_below_lines = text_view->pixels_below_lines;
5288 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5289 style->left_margin = text_view->left_margin;
5290 style->right_margin = text_view->right_margin;
5291 style->indent = text_view->indent;
5292 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5294 style->wrap_mode = text_view->wrap_mode;
5295 style->justification = text_view->justify;
5296 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5298 gtk_text_layout_set_default_style (text_view->layout, style);
5300 gtk_text_attributes_unref (style);
5302 /* Set layout for all anchored children */
5304 tmp_list = text_view->children;
5305 while (tmp_list != NULL)
5307 GtkTextViewChild *vc = tmp_list->data;
5311 gtk_text_anchored_child_set_layout (vc->widget,
5313 /* vc may now be invalid! */
5316 tmp_list = g_slist_next (tmp_list);
5322 * gtk_text_view_get_default_attributes:
5323 * @text_view: a #GtkTextView
5325 * Obtains a copy of the default text attributes. These are the
5326 * attributes used for text unless a tag overrides them.
5327 * You'd typically pass the default attributes in to
5328 * gtk_text_iter_get_attributes() in order to get the
5329 * attributes in effect at a given text position.
5331 * The return value is a copy owned by the caller of this function,
5332 * and should be freed.
5334 * Return value: a new #GtkTextAttributes
5337 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5339 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5341 gtk_text_view_ensure_layout (text_view);
5343 return gtk_text_attributes_copy (text_view->layout->default_style);
5347 gtk_text_view_destroy_layout (GtkTextView *text_view)
5349 if (text_view->layout)
5353 gtk_text_view_remove_validate_idles (text_view);
5355 /* Remove layout from all anchored children */
5356 tmp_list = text_view->children;
5357 while (tmp_list != NULL)
5359 GtkTextViewChild *vc = tmp_list->data;
5363 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5364 /* vc may now be invalid! */
5367 tmp_list = g_slist_next (tmp_list);
5370 gtk_text_view_stop_cursor_blink (text_view);
5371 gtk_text_view_end_selection_drag (text_view, NULL);
5373 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5374 invalidated_handler, text_view);
5375 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5376 changed_handler, text_view);
5377 g_object_unref (G_OBJECT (text_view->layout));
5378 text_view->layout = NULL;
5383 gtk_text_view_reset_im_context (GtkTextView *text_view)
5385 if (text_view->need_im_reset)
5387 text_view->need_im_reset = FALSE;
5388 gtk_im_context_reset (text_view->im_context);
5397 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5398 const GtkTextIter *iter,
5399 GdkEventMotion *event)
5401 GdkDragContext *context;
5402 GtkTargetList *target_list;
5404 text_view->drag_start_x = -1;
5405 text_view->drag_start_y = -1;
5406 text_view->pending_place_cursor_button = 0;
5408 target_list = gtk_target_list_new (target_table,
5409 G_N_ELEMENTS (target_table));
5411 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5412 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5413 1, (GdkEvent*)event);
5415 gtk_target_list_unref (target_list);
5417 gtk_drag_set_icon_default (context);
5421 gtk_text_view_drag_begin (GtkWidget *widget,
5422 GdkDragContext *context)
5428 gtk_text_view_drag_end (GtkWidget *widget,
5429 GdkDragContext *context)
5431 GtkTextView *text_view;
5433 text_view = GTK_TEXT_VIEW (widget);
5437 gtk_text_view_drag_data_get (GtkWidget *widget,
5438 GdkDragContext *context,
5439 GtkSelectionData *selection_data,
5443 GtkTextView *text_view;
5445 text_view = GTK_TEXT_VIEW (widget);
5447 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5449 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5451 gtk_selection_data_set (selection_data,
5452 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5465 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5468 /* Extract the selected text */
5469 str = gtk_text_iter_get_visible_text (&start, &end);
5474 gtk_selection_data_set_text (selection_data, str, -1);
5481 gtk_text_view_drag_data_delete (GtkWidget *widget,
5482 GdkDragContext *context)
5484 GtkTextView *text_view;
5486 text_view = GTK_TEXT_VIEW (widget);
5488 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5489 TRUE, GTK_TEXT_VIEW (widget)->editable);
5493 gtk_text_view_drag_leave (GtkWidget *widget,
5494 GdkDragContext *context,
5497 GtkTextView *text_view;
5499 text_view = GTK_TEXT_VIEW (widget);
5501 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5503 if (text_view->scroll_timeout != 0)
5504 gtk_timeout_remove (text_view->scroll_timeout);
5506 text_view->scroll_timeout = 0;
5510 gtk_text_view_drag_motion (GtkWidget *widget,
5511 GdkDragContext *context,
5516 GtkTextIter newplace;
5517 GtkTextView *text_view;
5520 GdkRectangle target_rect;
5522 GdkDragAction suggested_action = 0;
5524 text_view = GTK_TEXT_VIEW (widget);
5526 target_rect = text_view->text_window->allocation;
5528 if (x < target_rect.x ||
5529 y < target_rect.y ||
5530 x > (target_rect.x + target_rect.width) ||
5531 y > (target_rect.y + target_rect.height))
5532 return FALSE; /* outside the text window, allow parent widgets to handle event */
5534 gtk_text_view_window_to_buffer_coords (text_view,
5535 GTK_TEXT_WINDOW_WIDGET,
5539 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5543 if (gtk_drag_dest_find_target (widget, context,
5544 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5546 /* can't accept any of the offered targets */
5548 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5550 gtk_text_iter_in_range (&newplace, &start, &end))
5552 /* We're inside the selection. */
5556 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5558 GtkWidget *source_widget;
5560 suggested_action = context->suggested_action;
5562 source_widget = gtk_drag_get_source_widget (context);
5564 if (source_widget == widget)
5566 /* Default to MOVE, unless the user has
5567 * pressed ctrl or alt to affect available actions
5569 if ((context->actions & GDK_ACTION_MOVE) != 0)
5570 suggested_action = GDK_ACTION_MOVE;
5575 /* Can't drop here. */
5579 if (suggested_action != 0)
5581 gtk_text_mark_set_visible (text_view->dnd_mark,
5582 text_view->cursor_visible);
5584 gdk_drag_status (context, suggested_action, time);
5588 gdk_drag_status (context, 0, time);
5589 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5592 gtk_text_buffer_move_mark (get_buffer (text_view),
5593 text_view->dnd_mark,
5596 DV(g_print (G_STRLOC": scrolling to mark\n"));
5597 gtk_text_view_scroll_to_mark (text_view,
5598 text_view->dnd_mark,
5599 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5601 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5602 gtk_timeout_remove (text_view->scroll_timeout);
5604 text_view->scroll_timeout =
5605 gtk_timeout_add (50, drag_scan_timeout, text_view);
5607 /* TRUE return means don't propagate the drag motion to parent
5608 * widgets that may also be drop sites.
5614 gtk_text_view_drag_drop (GtkWidget *widget,
5615 GdkDragContext *context,
5620 GtkTextView *text_view;
5621 GtkTextIter drop_point;
5622 GdkAtom target = GDK_NONE;
5624 text_view = GTK_TEXT_VIEW (widget);
5626 if (text_view->scroll_timeout != 0)
5627 gtk_timeout_remove (text_view->scroll_timeout);
5629 text_view->scroll_timeout = 0;
5631 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5633 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5635 text_view->dnd_mark);
5637 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5638 target = gtk_drag_dest_find_target (widget, context, NULL);
5640 if (target != GDK_NONE)
5641 gtk_drag_get_data (widget, context, target, time);
5643 gtk_drag_finish (context, FALSE, FALSE, time);
5649 insert_text_data (GtkTextView *text_view,
5650 GtkTextIter *drop_point,
5651 GtkSelectionData *selection_data)
5655 str = gtk_selection_data_get_text (selection_data);
5659 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5660 drop_point, str, -1,
5661 text_view->editable);
5667 gtk_text_view_drag_data_received (GtkWidget *widget,
5668 GdkDragContext *context,
5671 GtkSelectionData *selection_data,
5675 GtkTextIter drop_point;
5676 GtkTextView *text_view;
5677 gboolean success = FALSE;
5679 text_view = GTK_TEXT_VIEW (widget);
5681 if (!text_view->dnd_mark)
5684 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5686 text_view->dnd_mark);
5688 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5691 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5693 GtkTextBuffer *src_buffer = NULL;
5694 GtkTextIter start, end;
5695 gboolean copy_tags = TRUE;
5697 if (selection_data->length != sizeof (src_buffer))
5700 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5702 if (src_buffer == NULL)
5705 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5707 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5708 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5711 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5716 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5720 text_view->editable);
5725 str = gtk_text_iter_get_visible_text (&start, &end);
5726 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5727 &drop_point, str, -1,
5728 text_view->editable);
5734 insert_text_data (text_view, &drop_point, selection_data);
5739 gtk_drag_finish (context, success,
5740 success && context->action == GDK_ACTION_MOVE,
5744 static GtkAdjustment*
5745 get_hadjustment (GtkTextView *text_view)
5747 if (text_view->hadjustment == NULL)
5748 gtk_text_view_set_scroll_adjustments (text_view,
5749 NULL, /* forces creation */
5750 text_view->vadjustment);
5752 return text_view->hadjustment;
5755 static GtkAdjustment*
5756 get_vadjustment (GtkTextView *text_view)
5758 if (text_view->vadjustment == NULL)
5759 gtk_text_view_set_scroll_adjustments (text_view,
5760 text_view->hadjustment,
5761 NULL); /* forces creation */
5762 return text_view->vadjustment;
5767 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5768 GtkAdjustment *hadj,
5769 GtkAdjustment *vadj)
5771 gboolean need_adjust = FALSE;
5773 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5776 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5778 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5780 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5782 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5784 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5786 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5787 g_object_unref (G_OBJECT (text_view->hadjustment));
5790 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5792 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5793 g_object_unref (G_OBJECT (text_view->vadjustment));
5796 if (text_view->hadjustment != hadj)
5798 text_view->hadjustment = hadj;
5799 g_object_ref (G_OBJECT (text_view->hadjustment));
5800 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5802 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5803 (GtkSignalFunc) gtk_text_view_value_changed,
5808 if (text_view->vadjustment != vadj)
5810 text_view->vadjustment = vadj;
5811 g_object_ref (G_OBJECT (text_view->vadjustment));
5812 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5814 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5815 (GtkSignalFunc) gtk_text_view_value_changed,
5821 gtk_text_view_value_changed (NULL, text_view);
5825 gtk_text_view_value_changed (GtkAdjustment *adj,
5826 GtkTextView *text_view)
5833 /* Note that we oddly call this function with adj == NULL
5837 text_view->onscreen_validated = FALSE;
5839 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5840 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5841 adj ? adj->value : 0.0));
5843 if (adj == text_view->hadjustment)
5845 dx = text_view->xoffset - (gint)adj->value;
5846 text_view->xoffset = adj->value;
5848 else if (adj == text_view->vadjustment)
5850 dy = text_view->yoffset - (gint)adj->value;
5851 text_view->yoffset = adj->value;
5853 if (text_view->layout)
5855 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5857 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5859 text_view->first_para_pixels = adj->value - line_top;
5863 if (dx != 0 || dy != 0)
5867 if (GTK_WIDGET_REALIZED (text_view))
5871 if (text_view->left_window)
5872 text_window_scroll (text_view->left_window, 0, dy);
5873 if (text_view->right_window)
5874 text_window_scroll (text_view->right_window, 0, dy);
5879 if (text_view->top_window)
5880 text_window_scroll (text_view->top_window, dx, 0);
5881 if (text_view->bottom_window)
5882 text_window_scroll (text_view->bottom_window, dx, 0);
5885 /* It looks nicer to scroll the main area last, because
5886 * it takes a while, and making the side areas update
5887 * afterward emphasizes the slowness of scrolling the
5890 text_window_scroll (text_view->text_window, dx, dy);
5893 /* Children are now "moved" in the text window, poke
5894 * into widget->allocation for each child
5896 tmp_list = text_view->children;
5897 while (tmp_list != NULL)
5899 GtkTextViewChild *child = tmp_list->data;
5903 child->widget->allocation.x -= dx;
5904 child->widget->allocation.y -= dy;
5907 g_print ("allocation for %p tweaked to %d,%d\n",
5909 child->widget->allocation.x,
5910 child->widget->allocation.y);
5914 tmp_list = g_slist_next (tmp_list);
5918 /* This could result in invalidation, which would install the
5919 * first_validate_idle, which would validate onscreen;
5920 * but we're going to go ahead and validate here, so
5921 * first_validate_idle shouldn't have anything to do.
5923 gtk_text_view_update_layout_width (text_view);
5925 /* note that validation of onscreen could invoke this function
5926 * recursively, by scrolling to maintain first_para, or in response
5927 * to updating the layout width, however there is no problem with
5928 * that, or shouldn't be.
5930 gtk_text_view_validate_onscreen (text_view);
5932 /* process exposes */
5933 if (GTK_WIDGET_REALIZED (text_view))
5935 if (text_view->left_window)
5936 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
5938 if (text_view->right_window)
5939 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
5941 if (text_view->top_window)
5942 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
5944 if (text_view->bottom_window)
5945 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
5947 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
5950 /* If this got installed, get rid of it, it's just a waste of time. */
5951 if (text_view->first_validate_idle != 0)
5953 g_source_remove (text_view->first_validate_idle);
5954 text_view->first_validate_idle = 0;
5957 gtk_text_view_update_im_spot_location (text_view);
5959 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
5963 gtk_text_view_commit_handler (GtkIMContext *context,
5965 GtkTextView *text_view)
5967 gtk_text_view_commit_text (text_view, str);
5971 gtk_text_view_commit_text (GtkTextView *text_view,
5974 gboolean had_selection;
5976 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5978 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5981 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5982 text_view->editable);
5984 if (!strcmp (str, "\n"))
5986 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
5987 text_view->editable);
5991 if (!had_selection && text_view->overwrite_mode)
5992 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
5993 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5994 text_view->editable);
5997 gtk_text_buffer_end_user_action (get_buffer (text_view));
5999 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6000 gtk_text_view_scroll_mark_onscreen (text_view,
6001 gtk_text_buffer_get_mark (get_buffer (text_view),
6006 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6007 GtkTextView *text_view)
6010 PangoAttrList *attrs;
6013 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6014 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6016 pango_attr_list_unref (attrs);
6021 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6022 GtkTextView *text_view)
6029 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6030 gtk_text_buffer_get_insert (text_view->buffer));
6033 pos = gtk_text_iter_get_line_index (&start);
6034 gtk_text_iter_set_line_offset (&start, 0);
6035 gtk_text_iter_forward_to_line_end (&end);
6037 text = gtk_text_iter_get_slice (&start, &end);
6038 gtk_im_context_set_surrounding (context, text, -1, pos);
6045 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6048 GtkTextView *text_view)
6053 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6054 gtk_text_buffer_get_insert (text_view->buffer));
6057 gtk_text_iter_forward_chars (&start, offset);
6058 gtk_text_iter_forward_chars (&end, offset + n_chars);
6060 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6066 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6067 const GtkTextIter *location,
6071 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6072 gboolean need_reset = FALSE;
6074 if (mark == gtk_text_buffer_get_insert (buffer))
6076 text_view->virtual_cursor_x = -1;
6077 text_view->virtual_cursor_y = -1;
6078 gtk_text_view_update_im_spot_location (text_view);
6081 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6087 gtk_text_view_reset_im_context (text_view);
6091 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6095 GdkRectangle strong_pos;
6098 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6099 gtk_text_buffer_get_mark (get_buffer (text_view),
6102 if ((x && text_view->virtual_cursor_x == -1) ||
6103 (y && text_view->virtual_cursor_y == -1))
6104 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6108 if (text_view->virtual_cursor_x != -1)
6109 *x = text_view->virtual_cursor_x;
6116 if (text_view->virtual_cursor_x != -1)
6117 *y = text_view->virtual_cursor_y;
6119 *y = strong_pos.y + strong_pos.height / 2;
6124 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6128 GdkRectangle strong_pos;
6131 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6132 gtk_text_buffer_get_mark (get_buffer (text_view),
6135 if (x == -1 || y == -1)
6136 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6138 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6139 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6142 /* Quick hack of a popup menu
6145 activate_cb (GtkWidget *menuitem,
6146 GtkTextView *text_view)
6148 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6149 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6153 append_action_signal (GtkTextView *text_view,
6155 const gchar *stock_id,
6156 const gchar *signal,
6159 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6161 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6162 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6163 GTK_SIGNAL_FUNC (activate_cb), text_view);
6165 gtk_widget_set_sensitive (menuitem, sensitive);
6167 gtk_widget_show (menuitem);
6168 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6172 popup_menu_detach (GtkWidget *attach_widget,
6175 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6179 popup_position_func (GtkMenu *menu,
6185 GtkTextView *text_view;
6187 GdkRectangle cursor_rect;
6188 GdkRectangle onscreen_rect;
6189 gint root_x, root_y;
6193 text_view = GTK_TEXT_VIEW (user_data);
6194 widget = GTK_WIDGET (text_view);
6196 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6198 gdk_window_get_origin (widget->window, &root_x, &root_y);
6200 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6202 gtk_text_buffer_get_insert (get_buffer (text_view)));
6204 gtk_text_view_get_iter_location (text_view,
6208 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6210 gtk_widget_size_request (text_view->popup_menu, &req);
6212 /* can't use rectangle_intersect since cursor rect can have 0 width */
6213 if (cursor_rect.x >= onscreen_rect.x &&
6214 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6215 cursor_rect.y >= onscreen_rect.y &&
6216 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6218 gtk_text_view_buffer_to_window_coords (text_view,
6219 GTK_TEXT_WINDOW_WIDGET,
6220 cursor_rect.x, cursor_rect.y,
6221 &cursor_rect.x, &cursor_rect.y);
6223 *x = root_x + cursor_rect.x + cursor_rect.width;
6224 *y = root_y + cursor_rect.y + cursor_rect.height;
6228 /* Just center the menu, since cursor is offscreen. */
6229 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6230 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6234 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6235 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6237 *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
6238 *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
6243 GtkTextView *text_view;
6249 range_contains_editable_text (const GtkTextIter *start,
6250 const GtkTextIter *end,
6251 gboolean default_editability)
6253 GtkTextIter iter = *start;
6255 while (gtk_text_iter_compare (&iter, end) < 0)
6257 if (gtk_text_iter_editable (&iter, default_editability))
6260 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6267 unichar_chosen_func (const char *text,
6270 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6272 gtk_text_view_commit_text (text_view, text);
6276 popup_targets_received (GtkClipboard *clipboard,
6277 GtkSelectionData *data,
6280 PopupInfo *info = user_data;
6281 GtkTextView *text_view = info->text_view;
6283 if (GTK_WIDGET_REALIZED (text_view))
6285 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6286 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6288 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6289 GtkWidget *menuitem;
6291 gboolean have_selection;
6292 gboolean can_insert;
6294 GtkTextIter sel_start, sel_end;
6296 if (text_view->popup_menu)
6297 gtk_widget_destroy (text_view->popup_menu);
6299 text_view->popup_menu = gtk_menu_new ();
6301 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6302 GTK_WIDGET (text_view),
6305 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6306 &sel_start, &sel_end);
6308 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6310 gtk_text_buffer_get_insert (get_buffer (text_view)));
6312 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6314 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6316 range_contains_editable_text (&sel_start, &sel_end,
6317 text_view->editable));
6318 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6320 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6321 can_insert && clipboard_contains_text);
6323 menuitem = gtk_separator_menu_item_new ();
6324 gtk_widget_show (menuitem);
6325 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6327 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6328 gtk_widget_show (menuitem);
6329 submenu = gtk_menu_new ();
6330 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6331 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6333 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6334 GTK_MENU_SHELL (submenu));
6336 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6337 gtk_widget_show (menuitem);
6338 gtk_widget_set_sensitive (menuitem, can_insert);
6340 submenu = gtk_menu_new ();
6341 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6342 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6344 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6345 unichar_chosen_func,
6348 gtk_signal_emit (GTK_OBJECT (text_view),
6349 signals[POPULATE_POPUP],
6350 text_view->popup_menu);
6353 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6355 info->button, info->time);
6358 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6359 popup_position_func, text_view,
6360 0, gtk_get_current_event_time ());
6361 _gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu));
6365 g_object_unref (text_view);
6370 gtk_text_view_do_popup (GtkTextView *text_view,
6371 GdkEventButton *event)
6373 PopupInfo *info = g_new (PopupInfo, 1);
6375 /* In order to know what entries we should make sensitive, we
6376 * ask for the current targets of the clipboard, and when
6377 * we get them, then we actually pop up the menu.
6379 info->text_view = g_object_ref (text_view);
6383 info->button = event->button;
6384 info->time = event->time;
6389 info->time = gtk_get_current_event_time ();
6392 gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
6393 gdk_atom_intern ("TARGETS", FALSE),
6394 popup_targets_received,
6399 gtk_text_view_popup_menu (GtkWidget *widget)
6401 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6404 /* Child GdkWindows */
6407 static GtkTextWindow*
6408 text_window_new (GtkTextWindowType type,
6411 gint height_request)
6415 win = g_new (GtkTextWindow, 1);
6418 win->widget = widget;
6420 win->bin_window = NULL;
6421 win->requisition.width = width_request;
6422 win->requisition.height = height_request;
6423 win->allocation.width = width_request;
6424 win->allocation.height = height_request;
6425 win->allocation.x = 0;
6426 win->allocation.y = 0;
6432 text_window_free (GtkTextWindow *win)
6435 text_window_unrealize (win);
6441 text_window_realize (GtkTextWindow *win,
6444 GdkWindowAttr attributes;
6445 gint attributes_mask;
6448 attributes.window_type = GDK_WINDOW_CHILD;
6449 attributes.x = win->allocation.x;
6450 attributes.y = win->allocation.y;
6451 attributes.width = win->allocation.width;
6452 attributes.height = win->allocation.height;
6453 attributes.wclass = GDK_INPUT_OUTPUT;
6454 attributes.visual = gtk_widget_get_visual (win->widget);
6455 attributes.colormap = gtk_widget_get_colormap (win->widget);
6456 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6458 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6460 win->window = gdk_window_new (parent,
6464 gdk_window_show (win->window);
6465 gdk_window_set_user_data (win->window, win->widget);
6469 attributes.width = win->allocation.width;
6470 attributes.height = win->allocation.height;
6471 attributes.event_mask = (GDK_EXPOSURE_MASK |
6473 GDK_KEY_PRESS_MASK |
6474 GDK_BUTTON_PRESS_MASK |
6475 GDK_BUTTON_RELEASE_MASK |
6476 GDK_POINTER_MOTION_MASK |
6477 GDK_POINTER_MOTION_HINT_MASK |
6478 gtk_widget_get_events (win->widget));
6480 win->bin_window = gdk_window_new (win->window,
6484 gdk_window_show (win->bin_window);
6485 gdk_window_set_user_data (win->bin_window, win->widget);
6487 if (win->type == GTK_TEXT_WINDOW_TEXT)
6490 cursor = gdk_cursor_new (GDK_XTERM);
6491 gdk_window_set_cursor (win->bin_window, cursor);
6492 gdk_cursor_unref (cursor);
6494 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6498 gdk_window_set_background (win->bin_window,
6499 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6503 gdk_window_set_background (win->bin_window,
6504 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6507 g_object_set_qdata (G_OBJECT (win->window),
6508 g_quark_from_static_string ("gtk-text-view-text-window"),
6511 g_object_set_qdata (G_OBJECT (win->bin_window),
6512 g_quark_from_static_string ("gtk-text-view-text-window"),
6517 text_window_unrealize (GtkTextWindow *win)
6519 if (win->type == GTK_TEXT_WINDOW_TEXT)
6521 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6525 gdk_window_set_user_data (win->window, NULL);
6526 gdk_window_set_user_data (win->bin_window, NULL);
6527 gdk_window_destroy (win->bin_window);
6528 gdk_window_destroy (win->window);
6530 win->bin_window = NULL;
6534 text_window_size_allocate (GtkTextWindow *win,
6537 win->allocation = *rect;
6541 gdk_window_move_resize (win->window,
6543 rect->width, rect->height);
6545 gdk_window_resize (win->bin_window,
6546 rect->width, rect->height);
6551 text_window_scroll (GtkTextWindow *win,
6555 if (dx != 0 || dy != 0)
6557 gdk_window_scroll (win->bin_window, dx, dy);
6562 text_window_invalidate_rect (GtkTextWindow *win,
6565 GdkRectangle window_rect;
6567 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6574 window_rect.width = rect->width;
6575 window_rect.height = rect->height;
6577 /* Adjust the rect as appropriate */
6581 case GTK_TEXT_WINDOW_TEXT:
6584 case GTK_TEXT_WINDOW_LEFT:
6585 case GTK_TEXT_WINDOW_RIGHT:
6587 window_rect.width = win->allocation.width;
6590 case GTK_TEXT_WINDOW_TOP:
6591 case GTK_TEXT_WINDOW_BOTTOM:
6593 window_rect.height = win->allocation.height;
6597 g_warning ("%s: bug!", G_STRLOC);
6602 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6606 GdkColor color = { 0, 65535, 0, 0 };
6607 GdkGC *gc = gdk_gc_new (win->bin_window);
6608 gdk_gc_set_rgb_fg_color (gc, &color);
6609 gdk_draw_rectangle (win->bin_window,
6610 gc, TRUE, window_rect.x, window_rect.y,
6611 window_rect.width, window_rect.height);
6618 text_window_get_width (GtkTextWindow *win)
6620 return win->allocation.width;
6624 text_window_get_height (GtkTextWindow *win)
6626 return win->allocation.height;
6630 text_window_get_allocation (GtkTextWindow *win,
6633 *rect = win->allocation;
6640 * gtk_text_view_get_window:
6641 * @text_view: a #GtkTextView
6642 * @win: window to get
6644 * Retrieves the #GdkWindow corresponding to an area of the text view;
6645 * possible windows include the overall widget window, child windows
6646 * on the left, right, top, bottom, and the window that displays the
6647 * text buffer. Windows are %NULL and nonexistent if their width or
6648 * height is 0, and are nonexistent before the widget has been
6651 * Return value: a #GdkWindow, or %NULL
6654 gtk_text_view_get_window (GtkTextView *text_view,
6655 GtkTextWindowType win)
6657 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6661 case GTK_TEXT_WINDOW_WIDGET:
6662 return GTK_WIDGET (text_view)->window;
6665 case GTK_TEXT_WINDOW_TEXT:
6666 return text_view->text_window->bin_window;
6669 case GTK_TEXT_WINDOW_LEFT:
6670 if (text_view->left_window)
6671 return text_view->left_window->bin_window;
6676 case GTK_TEXT_WINDOW_RIGHT:
6677 if (text_view->right_window)
6678 return text_view->right_window->bin_window;
6683 case GTK_TEXT_WINDOW_TOP:
6684 if (text_view->top_window)
6685 return text_view->top_window->bin_window;
6690 case GTK_TEXT_WINDOW_BOTTOM:
6691 if (text_view->bottom_window)
6692 return text_view->bottom_window->bin_window;
6697 case GTK_TEXT_WINDOW_PRIVATE:
6698 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", __FUNCTION__);
6703 g_warning ("%s: Unknown GtkTextWindowType", __FUNCTION__);
6708 * gtk_text_view_get_window_type:
6709 * @text_view: a #GtkTextView
6710 * @window: a window type
6712 * Usually used to find out which window an event corresponds to.
6713 * If you connect to an event signal on @text_view, this function
6714 * should be called on <literal>event->window</literal> to
6715 * see which window it was.
6717 * Return value: the window type.
6720 gtk_text_view_get_window_type (GtkTextView *text_view,
6725 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6726 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6728 if (window == GTK_WIDGET (text_view)->window)
6729 return GTK_TEXT_WINDOW_WIDGET;
6731 win = g_object_get_qdata (G_OBJECT (window),
6732 g_quark_try_string ("gtk-text-view-text-window"));
6738 return GTK_TEXT_WINDOW_PRIVATE;
6743 buffer_to_widget (GtkTextView *text_view,
6749 gint focus_edge_width;
6750 gboolean interior_focus;
6753 gtk_widget_style_get (GTK_WIDGET (text_view),
6754 "interior_focus", &interior_focus,
6755 "focus_line_width", &focus_width,
6759 focus_edge_width = 0;
6761 focus_edge_width = focus_width;
6765 *window_x = buffer_x - text_view->xoffset + focus_edge_width;
6766 if (text_view->left_window)
6767 *window_x += text_view->left_window->allocation.width;
6772 *window_y = buffer_y - text_view->yoffset + focus_edge_width;
6773 if (text_view->top_window)
6774 *window_y += text_view->top_window->allocation.height;
6779 widget_to_text_window (GtkTextWindow *win,
6786 *window_x = widget_x - win->allocation.x;
6789 *window_y = widget_y - win->allocation.y;
6793 buffer_to_text_window (GtkTextView *text_view,
6802 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6803 "for a nonexistent or private child window of GtkTextView");
6807 buffer_to_widget (text_view,
6809 window_x, window_y);
6811 widget_to_text_window (win,
6812 window_x ? *window_x : 0,
6813 window_y ? *window_y : 0,
6819 * gtk_text_view_buffer_to_window_coords:
6820 * @text_view: a #GtkTextView
6821 * @win: a #GtkTextWindowType
6822 * @buffer_x: buffer x coordinate
6823 * @buffer_y: buffer y coordinate
6824 * @window_x: window x coordinate return location
6825 * @window_y: window y coordinate return location
6827 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6828 * @win, and stores the result in (@window_x, @window_y).
6831 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6832 GtkTextWindowType win,
6838 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6842 case GTK_TEXT_WINDOW_WIDGET:
6843 buffer_to_widget (text_view,
6845 window_x, window_y);
6848 case GTK_TEXT_WINDOW_TEXT:
6850 *window_x = buffer_x - text_view->xoffset;
6852 *window_y = buffer_y - text_view->yoffset;
6855 case GTK_TEXT_WINDOW_LEFT:
6856 buffer_to_text_window (text_view,
6857 text_view->left_window,
6859 window_x, window_y);
6862 case GTK_TEXT_WINDOW_RIGHT:
6863 buffer_to_text_window (text_view,
6864 text_view->right_window,
6866 window_x, window_y);
6869 case GTK_TEXT_WINDOW_TOP:
6870 buffer_to_text_window (text_view,
6871 text_view->top_window,
6873 window_x, window_y);
6876 case GTK_TEXT_WINDOW_BOTTOM:
6877 buffer_to_text_window (text_view,
6878 text_view->bottom_window,
6880 window_x, window_y);
6883 case GTK_TEXT_WINDOW_PRIVATE:
6884 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6888 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6894 widget_to_buffer (GtkTextView *text_view,
6900 gint focus_edge_width;
6901 gboolean interior_focus;
6904 gtk_widget_style_get (GTK_WIDGET (text_view),
6905 "interior_focus", &interior_focus,
6906 "focus_line_width", &focus_width,
6910 focus_edge_width = 0;
6912 focus_edge_width = focus_width;
6916 *buffer_x = widget_x - focus_edge_width + text_view->xoffset;
6917 if (text_view->left_window)
6918 *buffer_x -= text_view->left_window->allocation.width;
6923 *buffer_y = widget_y - focus_edge_width + text_view->yoffset;
6924 if (text_view->top_window)
6925 *buffer_y -= text_view->top_window->allocation.height;
6930 text_window_to_widget (GtkTextWindow *win,
6937 *widget_x = window_x + win->allocation.x;
6940 *widget_y = window_y + win->allocation.y;
6944 text_window_to_buffer (GtkTextView *text_view,
6953 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
6954 "coordinates for a nonexistent child window.");
6958 text_window_to_widget (win,
6964 widget_to_buffer (text_view,
6965 buffer_x ? *buffer_x : 0,
6966 buffer_y ? *buffer_y : 0,
6972 * gtk_text_view_window_to_buffer_coords:
6973 * @text_view: a #GtkTextView
6974 * @win: a #GtkTextWindowType
6975 * @window_x: window x coordinate
6976 * @window_y: window y coordinate
6977 * @buffer_x: buffer x coordinate return location
6978 * @buffer_y: buffer y coordinate return location
6980 * Converts coordinates on the window identified by @win to buffer
6981 * coordinates, storing the result in (@buffer_x,@buffer_y).
6984 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
6985 GtkTextWindowType win,
6991 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6995 case GTK_TEXT_WINDOW_WIDGET:
6996 widget_to_buffer (text_view,
6998 buffer_x, buffer_y);
7001 case GTK_TEXT_WINDOW_TEXT:
7003 *buffer_x = window_x + text_view->xoffset;
7005 *buffer_y = window_y + text_view->yoffset;
7008 case GTK_TEXT_WINDOW_LEFT:
7009 text_window_to_buffer (text_view,
7010 text_view->left_window,
7012 buffer_x, buffer_y);
7015 case GTK_TEXT_WINDOW_RIGHT:
7016 text_window_to_buffer (text_view,
7017 text_view->right_window,
7019 buffer_x, buffer_y);
7022 case GTK_TEXT_WINDOW_TOP:
7023 text_window_to_buffer (text_view,
7024 text_view->top_window,
7026 buffer_x, buffer_y);
7029 case GTK_TEXT_WINDOW_BOTTOM:
7030 text_window_to_buffer (text_view,
7031 text_view->bottom_window,
7033 buffer_x, buffer_y);
7036 case GTK_TEXT_WINDOW_PRIVATE:
7037 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7041 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7047 set_window_width (GtkTextView *text_view,
7049 GtkTextWindowType type,
7050 GtkTextWindow **winp)
7056 text_window_free (*winp);
7058 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7065 *winp = text_window_new (type,
7066 GTK_WIDGET (text_view),
7068 /* if the widget is already realized we need to realize the child manually */
7069 if (GTK_WIDGET_REALIZED (text_view))
7070 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7074 if ((*winp)->requisition.width == width)
7077 (*winp)->requisition.width = width;
7080 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7086 set_window_height (GtkTextView *text_view,
7088 GtkTextWindowType type,
7089 GtkTextWindow **winp)
7095 text_window_free (*winp);
7097 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7104 *winp = text_window_new (type,
7105 GTK_WIDGET (text_view),
7108 /* if the widget is already realized we need to realize the child manually */
7109 if (GTK_WIDGET_REALIZED (text_view))
7110 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7114 if ((*winp)->requisition.height == height)
7117 (*winp)->requisition.height = height;
7120 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7125 * gtk_text_view_set_border_window_size:
7126 * @text_view: a #GtkTextView
7127 * @type: window to affect
7128 * @size: width or height of the window
7130 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7131 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7132 * Automatically destroys the corresponding window if the size is set
7133 * to 0, and creates the window if the size is set to non-zero. This
7134 * function can only be used for the "border windows," it doesn't work
7135 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7136 * #GTK_TEXT_WINDOW_PRIVATE.
7139 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7140 GtkTextWindowType type,
7144 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7145 g_return_if_fail (size >= 0);
7149 case GTK_TEXT_WINDOW_LEFT:
7150 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7151 &text_view->left_window);
7154 case GTK_TEXT_WINDOW_RIGHT:
7155 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7156 &text_view->right_window);
7159 case GTK_TEXT_WINDOW_TOP:
7160 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7161 &text_view->top_window);
7164 case GTK_TEXT_WINDOW_BOTTOM:
7165 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7166 &text_view->bottom_window);
7170 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7176 * gtk_text_view_get_border_window_size:
7177 * @text_view: a #GtkTextView
7178 * @type: window to return size from
7180 * Gets the width of the specified border window. See
7181 * gtk_text_view_set_border_window_size().
7183 * Return value: width of window
7186 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7187 GtkTextWindowType type)
7189 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7193 case GTK_TEXT_WINDOW_LEFT:
7194 if (text_view->left_window)
7195 return text_view->left_window->requisition.width;
7197 case GTK_TEXT_WINDOW_RIGHT:
7198 if (text_view->right_window)
7199 return text_view->right_window->requisition.width;
7201 case GTK_TEXT_WINDOW_TOP:
7202 if (text_view->top_window)
7203 return text_view->top_window->requisition.height;
7205 case GTK_TEXT_WINDOW_BOTTOM:
7206 if (text_view->bottom_window)
7207 return text_view->bottom_window->requisition.height;
7210 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7221 static GtkTextViewChild*
7222 text_view_child_new_anchored (GtkWidget *child,
7223 GtkTextChildAnchor *anchor,
7224 GtkTextLayout *layout)
7226 GtkTextViewChild *vc;
7228 vc = g_new (GtkTextViewChild, 1);
7231 vc->anchor = anchor;
7233 vc->from_top_of_line = 0;
7234 vc->from_left_of_buffer = 0;
7236 g_object_ref (G_OBJECT (vc->widget));
7237 g_object_ref (G_OBJECT (vc->anchor));
7239 g_object_set_data (G_OBJECT (child),
7240 "gtk-text-view-child",
7243 gtk_text_child_anchor_register_child (anchor, child, layout);
7248 static GtkTextViewChild*
7249 text_view_child_new_window (GtkWidget *child,
7250 GtkTextWindowType type,
7254 GtkTextViewChild *vc;
7256 vc = g_new (GtkTextViewChild, 1);
7261 vc->from_top_of_line = 0;
7262 vc->from_left_of_buffer = 0;
7264 g_object_ref (G_OBJECT (vc->widget));
7270 g_object_set_data (G_OBJECT (child),
7271 "gtk-text-view-child",
7278 text_view_child_free (GtkTextViewChild *child)
7280 g_object_set_data (G_OBJECT (child->widget),
7281 "gtk-text-view-child", NULL);
7285 gtk_text_child_anchor_unregister_child (child->anchor,
7287 g_object_unref (G_OBJECT (child->anchor));
7290 g_object_unref (G_OBJECT (child->widget));
7296 text_view_child_set_parent_window (GtkTextView *text_view,
7297 GtkTextViewChild *vc)
7300 gtk_widget_set_parent_window (vc->widget,
7301 text_view->text_window->bin_window);
7305 window = gtk_text_view_get_window (text_view,
7307 gtk_widget_set_parent_window (vc->widget, window);
7312 add_child (GtkTextView *text_view,
7313 GtkTextViewChild *vc)
7315 text_view->children = g_slist_prepend (text_view->children,
7318 if (GTK_WIDGET_REALIZED (text_view))
7319 text_view_child_set_parent_window (text_view, vc);
7321 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7325 * gtk_text_view_add_child_at_anchor:
7326 * @text_view: a #GtkTextView
7327 * @child: a #GtkWidget
7328 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7330 * Adds a child widget in the text buffer, at the given @anchor.
7334 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7336 GtkTextChildAnchor *anchor)
7338 GtkTextViewChild *vc;
7340 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7341 g_return_if_fail (GTK_IS_WIDGET (child));
7342 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7343 g_return_if_fail (child->parent == NULL);
7345 gtk_text_view_ensure_layout (text_view);
7347 vc = text_view_child_new_anchored (child, anchor,
7350 add_child (text_view, vc);
7352 g_assert (vc->widget == child);
7353 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7357 * gtk_text_view_add_child_in_window:
7358 * @text_view: a #GtkTextView
7359 * @child: a #GtkWidget
7360 * @which_window: which window the child should appear in
7361 * @xpos: X position of child in window coordinates
7362 * @ypos: Y position of child in window coordinates
7364 * Adds a child at fixed coordinates in one of the text widget's
7365 * windows. The window must have nonzero size (see
7366 * gtk_text_view_set_border_window_size()). Note that the child
7367 * coordinates are given relative to the #GdkWindow in question, and
7368 * that these coordinates have no sane relationship to scrolling. When
7369 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7370 * irrelevant, the child floats above all scrollable areas. But when
7371 * placing a child in one of the scrollable windows (border windows or
7372 * text window), you'll need to compute the child's correct position
7373 * in buffer coordinates any time scrolling occurs or buffer changes
7374 * occur, and then call gtk_text_view_move_child() to update the
7375 * child's position. Unfortunately there's no good way to detect that
7376 * scrolling has occurred, using the current API; a possible hack
7377 * would be to update all child positions when the scroll adjustments
7378 * change or the text buffer changes. See bug 64518 on
7379 * bugzilla.gnome.org for status of fixing this issue.
7383 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7385 GtkTextWindowType which_window,
7389 GtkTextViewChild *vc;
7391 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7392 g_return_if_fail (GTK_IS_WIDGET (child));
7393 g_return_if_fail (child->parent == NULL);
7395 vc = text_view_child_new_window (child, which_window,
7398 add_child (text_view, vc);
7400 g_assert (vc->widget == child);
7401 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7405 * gtk_text_view_move_child:
7406 * @text_view: a #GtkTextView
7407 * @child: child widget already added to the text view
7408 * @xpos: new X position in window coordinates
7409 * @ypos: new Y position in window coordinates
7411 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7415 gtk_text_view_move_child (GtkTextView *text_view,
7420 GtkTextViewChild *vc;
7422 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7423 g_return_if_fail (GTK_IS_WIDGET (child));
7424 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7426 vc = g_object_get_data (G_OBJECT (child),
7427 "gtk-text-view-child");
7429 g_assert (vc != NULL);
7431 if (vc->x == xpos &&
7438 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7439 gtk_widget_queue_resize (child);
7443 /* Iterator operations */
7446 * gtk_text_view_forward_display_line:
7447 * @text_view: a #GtkTextView
7448 * @iter: a #GtkTextIter
7450 * Moves the given @iter forward by one display (wrapped) line. A
7451 * display line is different from a paragraph. Paragraphs are
7452 * separated by newlines or other paragraph separator characters.
7453 * Display lines are created by line-wrapping a paragraph. If
7454 * wrapping is turned off, display lines and paragraphs will be the
7455 * same. Display lines are divided differently for each view, since
7456 * they depend on the view's width; paragraphs are the same in all
7457 * views, since they depend on the contents of the #GtkTextBuffer.
7459 * Return value: %TRUE if @iter was moved and is not on the end iterator
7462 gtk_text_view_forward_display_line (GtkTextView *text_view,
7465 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7466 g_return_val_if_fail (iter != NULL, FALSE);
7468 gtk_text_view_ensure_layout (text_view);
7470 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7474 * gtk_text_view_backward_display_line:
7475 * @text_view: a #GtkTextView
7476 * @iter: a #GtkTextIter
7478 * Moves the given @iter backward by one display (wrapped) line. A
7479 * display line is different from a paragraph. Paragraphs are
7480 * separated by newlines or other paragraph separator characters.
7481 * Display lines are created by line-wrapping a paragraph. If
7482 * wrapping is turned off, display lines and paragraphs will be the
7483 * same. Display lines are divided differently for each view, since
7484 * they depend on the view's width; paragraphs are the same in all
7485 * views, since they depend on the contents of the #GtkTextBuffer.
7487 * Return value: %TRUE if @iter was moved and is not on the end iterator
7490 gtk_text_view_backward_display_line (GtkTextView *text_view,
7493 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7494 g_return_val_if_fail (iter != NULL, FALSE);
7496 gtk_text_view_ensure_layout (text_view);
7498 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7502 * gtk_text_view_forward_display_line_end:
7503 * @text_view: a #GtkTextView
7504 * @iter: a #GtkTextIter
7506 * Moves the given @iter forward to the next display line end. 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_end (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_line_end (text_view->layout, iter, 1);
7530 * gtk_text_view_backward_display_line_start:
7531 * @text_view: a #GtkTextView
7532 * @iter: a #GtkTextIter
7534 * Moves the given @iter backward to the next display line start. 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_start (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_line_end (text_view->layout, iter, -1);
7558 * gtk_text_view_starts_display_line:
7559 * @text_view: a #GtkTextView
7560 * @iter: a #GtkTextIter
7562 * Determines whether @iter is at the start of a display line.
7563 * See gtk_text_view_forward_display_line() for an explanation of
7564 * display lines vs. paragraphs.
7566 * Return value: %TRUE if @iter begins a wrapped line
7569 gtk_text_view_starts_display_line (GtkTextView *text_view,
7570 const GtkTextIter *iter)
7572 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7573 g_return_val_if_fail (iter != NULL, FALSE);
7575 gtk_text_view_ensure_layout (text_view);
7577 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7581 * gtk_text_view_move_visually:
7582 * @text_view: a #GtkTextView
7583 * @iter: a #GtkTextIter
7584 * @count: number of lines to move
7586 * Moves @iter up or down by @count display (wrapped) lines.
7587 * See gtk_text_view_forward_display_line() for an explanation of
7588 * display lines vs. paragraphs.
7590 * Return value: %TRUE if @iter moved and is not on the end iterator
7593 gtk_text_view_move_visually (GtkTextView *text_view,
7597 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7598 g_return_val_if_fail (iter != NULL, FALSE);
7600 gtk_text_view_ensure_layout (text_view);
7602 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);