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;
4709 g_return_if_fail (text_view->hadjustment != NULL);
4711 adj = text_view->hadjustment;
4713 /* Validate the line that we're moving within.
4715 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4717 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4718 gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4719 gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4720 /* FIXME do we need to update the adjustment ranges here? */
4722 if (count < 0 && adj->value <= (adj->lower + 1e-12))
4724 /* already at far left, just be sure we are at offset 0 */
4725 gtk_text_iter_set_line_offset (&new_insert, 0);
4726 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4728 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4730 /* already at far right, just be sure we are at the end */
4731 gtk_text_iter_forward_to_line_end (&new_insert);
4732 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4736 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4738 newval = adj->value;
4739 oldval = adj->value;
4741 newval += count * adj->page_increment;
4743 set_adjustment_clamped (adj, newval);
4744 cursor_x_pos += adj->value - oldval;
4746 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4747 clamp_iter_onscreen (text_view, &new_insert);
4748 gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4750 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4753 /* FIXME for lines shorter than the overall widget width, this results in a
4754 * "bounce" effect as we scroll to the right of the widget, then scroll
4755 * back to get the end of the line onscreen.
4756 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
4759 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4760 * only guarantees 1 pixel onscreen.
4762 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4763 gtk_text_view_scroll_mark_onscreen (text_view,
4764 gtk_text_buffer_get_mark (get_buffer (text_view),
4769 whitespace (gunichar ch, gpointer user_data)
4771 return (ch == ' ' || ch == '\t');
4775 not_whitespace (gunichar ch, gpointer user_data)
4777 return !whitespace (ch, user_data);
4781 find_whitepace_region (const GtkTextIter *center,
4782 GtkTextIter *start, GtkTextIter *end)
4787 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4788 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4789 if (whitespace (gtk_text_iter_get_char (end), NULL))
4790 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4792 return !gtk_text_iter_equal (start, end);
4796 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4799 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4800 text_view->editable);
4804 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
4811 gboolean leave_one = FALSE;
4813 gtk_text_view_reset_im_context (text_view);
4815 if (type == GTK_DELETE_CHARS)
4817 /* Char delete deletes the selection, if one exists */
4818 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4819 text_view->editable))
4823 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4825 gtk_text_buffer_get_mark (get_buffer (text_view),
4833 case GTK_DELETE_CHARS:
4834 gtk_text_iter_forward_cursor_positions (&end, count);
4837 case GTK_DELETE_WORD_ENDS:
4839 gtk_text_iter_forward_word_ends (&end, count);
4841 gtk_text_iter_backward_word_starts (&start, 0 - count);
4844 case GTK_DELETE_WORDS:
4847 case GTK_DELETE_DISPLAY_LINE_ENDS:
4850 case GTK_DELETE_DISPLAY_LINES:
4853 case GTK_DELETE_PARAGRAPH_ENDS:
4854 /* If we're already at a newline, we need to
4855 * simply delete that newline, instead of
4856 * moving to the next one.
4858 if (gtk_text_iter_ends_line (&end))
4860 gtk_text_iter_forward_line (&end);
4866 if (!gtk_text_iter_forward_to_line_end (&end))
4872 /* FIXME figure out what a negative count means
4876 case GTK_DELETE_PARAGRAPHS:
4879 gtk_text_iter_set_line_offset (&start, 0);
4880 gtk_text_iter_forward_to_line_end (&end);
4882 /* Do the lines beyond the first. */
4885 gtk_text_iter_forward_to_line_end (&end);
4891 /* FIXME negative count? */
4895 case GTK_DELETE_WHITESPACE:
4897 find_whitepace_region (&insert, &start, &end);
4905 if (!gtk_text_iter_equal (&start, &end))
4907 gtk_text_buffer_begin_user_action (get_buffer (text_view));
4909 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4910 text_view->editable))
4913 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4915 text_view->editable);
4918 gtk_text_buffer_end_user_action (get_buffer (text_view));
4920 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4921 gtk_text_view_scroll_mark_onscreen (text_view,
4922 gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4927 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4929 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4930 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4931 text_view->editable);
4932 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4933 gtk_text_view_scroll_mark_onscreen (text_view,
4934 gtk_text_buffer_get_mark (get_buffer (text_view),
4939 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4941 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4942 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
4943 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4944 gtk_text_view_scroll_mark_onscreen (text_view,
4945 gtk_text_buffer_get_mark (get_buffer (text_view),
4950 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4952 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4953 gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4955 text_view->editable);
4956 DV(g_print (G_STRLOC": scrolling onscreen\n"));
4957 gtk_text_view_scroll_mark_onscreen (text_view,
4958 gtk_text_buffer_get_mark (get_buffer (text_view),
4963 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4965 text_view->overwrite_mode = !text_view->overwrite_mode;
4969 gtk_text_view_move_focus (GtkTextView *text_view,
4970 GtkDirectionType direction_type)
4972 GtkWidget *toplevel;
4975 gtk_widget_get_ancestor (GTK_WIDGET (text_view), GTK_TYPE_WINDOW);
4977 if (toplevel == NULL)
4980 /* Propagate to toplevel */
4981 g_signal_emit_by_name (G_OBJECT (toplevel), "move_focus",
4990 gtk_text_view_unselect (GtkTextView *text_view)
4994 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4996 gtk_text_buffer_get_mark (get_buffer (text_view),
4999 gtk_text_buffer_move_mark (get_buffer (text_view),
5000 gtk_text_buffer_get_mark (get_buffer (text_view),
5006 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5007 const gchar *mark_name)
5010 GdkModifierType state;
5011 GtkTextIter newplace;
5013 /* DV(g_print (G_STRLOC": begin\n")); */
5015 gdk_window_get_pointer (text_view->text_window->bin_window,
5018 /* DV(g_print (G_STRLOC": get iter at pixel\n"); */
5019 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5021 x + text_view->xoffset,
5022 y + text_view->yoffset);
5026 gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5028 DV(g_print (G_STRLOC": move mark\n"));
5029 gtk_text_buffer_move_mark (get_buffer (text_view),
5033 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5034 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5039 selection_scan_timeout (gpointer data)
5041 GtkTextView *text_view;
5043 GDK_THREADS_ENTER ();
5045 text_view = GTK_TEXT_VIEW (data);
5047 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5048 move_mark_to_pointer_and_scroll (text_view, "insert");
5050 GDK_THREADS_LEAVE ();
5052 return TRUE; /* remain installed. */
5055 #define DND_SCROLL_MARGIN 0.20
5058 drag_scan_timeout (gpointer data)
5060 GtkTextView *text_view;
5062 GdkModifierType state;
5063 GtkTextIter newplace;
5065 GDK_THREADS_ENTER ();
5067 text_view = GTK_TEXT_VIEW (data);
5069 gdk_window_get_pointer (text_view->text_window->bin_window,
5072 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5074 x + text_view->xoffset,
5075 y + text_view->yoffset);
5077 gtk_text_buffer_move_mark (get_buffer (text_view),
5078 text_view->dnd_mark,
5081 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5082 gtk_text_view_scroll_to_mark (text_view,
5083 text_view->dnd_mark,
5084 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5086 GDK_THREADS_LEAVE ();
5092 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5094 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5095 move_mark_to_pointer_and_scroll (text_view, "insert");
5097 /* If we had to scroll offscreen, insert a timeout to do so
5098 * again. Note that in the timeout, even if the mouse doesn't
5099 * move, due to this scroll xoffset/yoffset will have changed
5100 * and we'll need to scroll again.
5102 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5103 gtk_timeout_remove (text_view->scroll_timeout);
5105 text_view->scroll_timeout =
5106 gtk_timeout_add (50, selection_scan_timeout, text_view);
5112 gtk_text_view_start_selection_drag (GtkTextView *text_view,
5113 const GtkTextIter *iter,
5114 GdkEventButton *button)
5116 GtkTextIter newplace;
5117 GtkTextBuffer *buffer;
5119 g_return_if_fail (text_view->selection_drag_handler == 0);
5121 gtk_grab_add (GTK_WIDGET (text_view));
5123 buffer = get_buffer (text_view);
5127 if (button->state & GDK_SHIFT_MASK)
5129 /* Extend selection */
5130 GtkTextIter start, end;
5132 gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5134 if (gtk_text_iter_compare (&newplace, &start) <= 0)
5136 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5139 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5142 else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5144 gtk_text_buffer_move_mark_by_name (buffer, "insert",
5147 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5153 /* Replace selection */
5154 gtk_text_buffer_place_cursor (buffer, &newplace);
5157 text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
5158 "motion_notify_event",
5159 GTK_SIGNAL_FUNC (selection_motion_event_handler),
5163 /* returns whether we were really dragging */
5165 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5167 if (text_view->selection_drag_handler == 0)
5170 gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
5171 text_view->selection_drag_handler = 0;
5173 if (text_view->scroll_timeout != 0)
5175 gtk_timeout_remove (text_view->scroll_timeout);
5176 text_view->scroll_timeout = 0;
5179 /* one last update to current position */
5180 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5181 move_mark_to_pointer_and_scroll (text_view, "insert");
5183 gtk_grab_remove (GTK_WIDGET (text_view));
5193 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
5194 GtkTextAttributes *values,
5197 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5198 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5201 pango_font_description_free (values->font);
5203 values->font = pango_font_description_copy (style->font_desc);
5207 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5209 if (text_view->layout)
5211 gboolean split_cursor;
5212 GtkTextDirection new_dir;
5213 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5215 g_object_get (G_OBJECT (settings),
5216 "gtk-split-cursor", &split_cursor,
5219 new_dir = GTK_TEXT_DIR_NONE;
5221 new_dir = (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
5222 GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5224 if (text_view->layout->cursor_direction != new_dir)
5225 gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5230 gtk_text_view_ensure_layout (GtkTextView *text_view)
5234 widget = GTK_WIDGET (text_view);
5236 if (text_view->layout == NULL)
5238 GtkTextAttributes *style;
5239 PangoContext *ltr_context, *rtl_context;
5242 DV(g_print(G_STRLOC"\n"));
5244 text_view->layout = gtk_text_layout_new ();
5246 g_signal_connect (G_OBJECT (text_view->layout),
5248 G_CALLBACK (invalidated_handler),
5251 g_signal_connect (G_OBJECT (text_view->layout),
5253 G_CALLBACK (changed_handler),
5256 g_signal_connect (G_OBJECT (text_view->layout),
5258 G_CALLBACK (gtk_text_view_child_allocated),
5261 if (get_buffer (text_view))
5262 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5264 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5265 gtk_text_view_pend_cursor_blink (text_view);
5267 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5269 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5270 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5271 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5272 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5274 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5276 g_object_unref (G_OBJECT (ltr_context));
5277 g_object_unref (G_OBJECT (rtl_context));
5279 gtk_text_view_check_keymap_direction (text_view);
5281 style = gtk_text_attributes_new ();
5283 gtk_widget_ensure_style (widget);
5284 gtk_text_view_set_attributes_from_style (text_view,
5285 style, widget->style);
5287 style->pixels_above_lines = text_view->pixels_above_lines;
5288 style->pixels_below_lines = text_view->pixels_below_lines;
5289 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5290 style->left_margin = text_view->left_margin;
5291 style->right_margin = text_view->right_margin;
5292 style->indent = text_view->indent;
5293 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5295 style->wrap_mode = text_view->wrap_mode;
5296 style->justification = text_view->justify;
5297 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5299 gtk_text_layout_set_default_style (text_view->layout, style);
5301 gtk_text_attributes_unref (style);
5303 /* Set layout for all anchored children */
5305 tmp_list = text_view->children;
5306 while (tmp_list != NULL)
5308 GtkTextViewChild *vc = tmp_list->data;
5312 gtk_text_anchored_child_set_layout (vc->widget,
5314 /* vc may now be invalid! */
5317 tmp_list = g_slist_next (tmp_list);
5323 * gtk_text_view_get_default_attributes:
5324 * @text_view: a #GtkTextView
5326 * Obtains a copy of the default text attributes. These are the
5327 * attributes used for text unless a tag overrides them.
5328 * You'd typically pass the default attributes in to
5329 * gtk_text_iter_get_attributes() in order to get the
5330 * attributes in effect at a given text position.
5332 * The return value is a copy owned by the caller of this function,
5333 * and should be freed.
5335 * Return value: a new #GtkTextAttributes
5338 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5340 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5342 gtk_text_view_ensure_layout (text_view);
5344 return gtk_text_attributes_copy (text_view->layout->default_style);
5348 gtk_text_view_destroy_layout (GtkTextView *text_view)
5350 if (text_view->layout)
5354 gtk_text_view_remove_validate_idles (text_view);
5356 /* Remove layout from all anchored children */
5357 tmp_list = text_view->children;
5358 while (tmp_list != NULL)
5360 GtkTextViewChild *vc = tmp_list->data;
5364 gtk_text_anchored_child_set_layout (vc->widget, NULL);
5365 /* vc may now be invalid! */
5368 tmp_list = g_slist_next (tmp_list);
5371 gtk_text_view_stop_cursor_blink (text_view);
5372 gtk_text_view_end_selection_drag (text_view, NULL);
5374 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5375 invalidated_handler, text_view);
5376 g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
5377 changed_handler, text_view);
5378 g_object_unref (G_OBJECT (text_view->layout));
5379 text_view->layout = NULL;
5384 gtk_text_view_reset_im_context (GtkTextView *text_view)
5386 if (text_view->need_im_reset)
5388 text_view->need_im_reset = FALSE;
5389 gtk_im_context_reset (text_view->im_context);
5398 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
5399 const GtkTextIter *iter,
5400 GdkEventMotion *event)
5402 GdkDragContext *context;
5403 GtkTargetList *target_list;
5405 text_view->drag_start_x = -1;
5406 text_view->drag_start_y = -1;
5407 text_view->pending_place_cursor_button = 0;
5409 target_list = gtk_target_list_new (target_table,
5410 G_N_ELEMENTS (target_table));
5412 context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5413 GDK_ACTION_COPY | GDK_ACTION_MOVE,
5414 1, (GdkEvent*)event);
5416 gtk_target_list_unref (target_list);
5418 gtk_drag_set_icon_default (context);
5422 gtk_text_view_drag_begin (GtkWidget *widget,
5423 GdkDragContext *context)
5429 gtk_text_view_drag_end (GtkWidget *widget,
5430 GdkDragContext *context)
5432 GtkTextView *text_view;
5434 text_view = GTK_TEXT_VIEW (widget);
5438 gtk_text_view_drag_data_get (GtkWidget *widget,
5439 GdkDragContext *context,
5440 GtkSelectionData *selection_data,
5444 GtkTextView *text_view;
5446 text_view = GTK_TEXT_VIEW (widget);
5448 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5450 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5452 gtk_selection_data_set (selection_data,
5453 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5466 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5469 /* Extract the selected text */
5470 str = gtk_text_iter_get_visible_text (&start, &end);
5475 gtk_selection_data_set_text (selection_data, str, -1);
5482 gtk_text_view_drag_data_delete (GtkWidget *widget,
5483 GdkDragContext *context)
5485 GtkTextView *text_view;
5487 text_view = GTK_TEXT_VIEW (widget);
5489 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5490 TRUE, GTK_TEXT_VIEW (widget)->editable);
5494 gtk_text_view_drag_leave (GtkWidget *widget,
5495 GdkDragContext *context,
5498 GtkTextView *text_view;
5500 text_view = GTK_TEXT_VIEW (widget);
5502 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5504 if (text_view->scroll_timeout != 0)
5505 gtk_timeout_remove (text_view->scroll_timeout);
5507 text_view->scroll_timeout = 0;
5511 gtk_text_view_drag_motion (GtkWidget *widget,
5512 GdkDragContext *context,
5517 GtkTextIter newplace;
5518 GtkTextView *text_view;
5521 GdkRectangle target_rect;
5523 GdkDragAction suggested_action = 0;
5525 text_view = GTK_TEXT_VIEW (widget);
5527 target_rect = text_view->text_window->allocation;
5529 if (x < target_rect.x ||
5530 y < target_rect.y ||
5531 x > (target_rect.x + target_rect.width) ||
5532 y > (target_rect.y + target_rect.height))
5533 return FALSE; /* outside the text window, allow parent widgets to handle event */
5535 gtk_text_view_window_to_buffer_coords (text_view,
5536 GTK_TEXT_WINDOW_WIDGET,
5540 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5544 if (gtk_drag_dest_find_target (widget, context,
5545 gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5547 /* can't accept any of the offered targets */
5549 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5551 gtk_text_iter_in_range (&newplace, &start, &end))
5553 /* We're inside the selection. */
5557 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5559 GtkWidget *source_widget;
5561 suggested_action = context->suggested_action;
5563 source_widget = gtk_drag_get_source_widget (context);
5565 if (source_widget == widget)
5567 /* Default to MOVE, unless the user has
5568 * pressed ctrl or alt to affect available actions
5570 if ((context->actions & GDK_ACTION_MOVE) != 0)
5571 suggested_action = GDK_ACTION_MOVE;
5576 /* Can't drop here. */
5580 if (suggested_action != 0)
5582 gtk_text_mark_set_visible (text_view->dnd_mark,
5583 text_view->cursor_visible);
5585 gdk_drag_status (context, suggested_action, time);
5589 gdk_drag_status (context, 0, time);
5590 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5593 gtk_text_buffer_move_mark (get_buffer (text_view),
5594 text_view->dnd_mark,
5597 DV(g_print (G_STRLOC": scrolling to mark\n"));
5598 gtk_text_view_scroll_to_mark (text_view,
5599 text_view->dnd_mark,
5600 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5602 if (text_view->scroll_timeout != 0) /* reset on every motion event */
5603 gtk_timeout_remove (text_view->scroll_timeout);
5605 text_view->scroll_timeout =
5606 gtk_timeout_add (50, drag_scan_timeout, text_view);
5608 /* TRUE return means don't propagate the drag motion to parent
5609 * widgets that may also be drop sites.
5615 gtk_text_view_drag_drop (GtkWidget *widget,
5616 GdkDragContext *context,
5621 GtkTextView *text_view;
5622 GtkTextIter drop_point;
5623 GdkAtom target = GDK_NONE;
5625 text_view = GTK_TEXT_VIEW (widget);
5627 if (text_view->scroll_timeout != 0)
5628 gtk_timeout_remove (text_view->scroll_timeout);
5630 text_view->scroll_timeout = 0;
5632 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5634 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5636 text_view->dnd_mark);
5638 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5639 target = gtk_drag_dest_find_target (widget, context, NULL);
5641 if (target != GDK_NONE)
5642 gtk_drag_get_data (widget, context, target, time);
5644 gtk_drag_finish (context, FALSE, FALSE, time);
5650 insert_text_data (GtkTextView *text_view,
5651 GtkTextIter *drop_point,
5652 GtkSelectionData *selection_data)
5656 str = gtk_selection_data_get_text (selection_data);
5660 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5661 drop_point, str, -1,
5662 text_view->editable);
5668 gtk_text_view_drag_data_received (GtkWidget *widget,
5669 GdkDragContext *context,
5672 GtkSelectionData *selection_data,
5676 GtkTextIter drop_point;
5677 GtkTextView *text_view;
5678 gboolean success = FALSE;
5680 text_view = GTK_TEXT_VIEW (widget);
5682 if (!text_view->dnd_mark)
5685 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5687 text_view->dnd_mark);
5689 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5692 if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5694 GtkTextBuffer *src_buffer = NULL;
5695 GtkTextIter start, end;
5696 gboolean copy_tags = TRUE;
5698 if (selection_data->length != sizeof (src_buffer))
5701 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5703 if (src_buffer == NULL)
5706 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5708 if (gtk_text_buffer_get_tag_table (src_buffer) !=
5709 gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5712 if (gtk_text_buffer_get_selection_bounds (src_buffer,
5717 gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5721 text_view->editable);
5726 str = gtk_text_iter_get_visible_text (&start, &end);
5727 gtk_text_buffer_insert_interactive (get_buffer (text_view),
5728 &drop_point, str, -1,
5729 text_view->editable);
5735 insert_text_data (text_view, &drop_point, selection_data);
5740 gtk_drag_finish (context, success,
5741 success && context->action == GDK_ACTION_MOVE,
5745 static GtkAdjustment*
5746 get_hadjustment (GtkTextView *text_view)
5748 if (text_view->hadjustment == NULL)
5749 gtk_text_view_set_scroll_adjustments (text_view,
5750 NULL, /* forces creation */
5751 text_view->vadjustment);
5753 return text_view->hadjustment;
5756 static GtkAdjustment*
5757 get_vadjustment (GtkTextView *text_view)
5759 if (text_view->vadjustment == NULL)
5760 gtk_text_view_set_scroll_adjustments (text_view,
5761 text_view->hadjustment,
5762 NULL); /* forces creation */
5763 return text_view->vadjustment;
5768 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
5769 GtkAdjustment *hadj,
5770 GtkAdjustment *vadj)
5772 gboolean need_adjust = FALSE;
5774 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5777 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5779 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5781 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5783 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5785 if (text_view->hadjustment && (text_view->hadjustment != hadj))
5787 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5788 g_object_unref (G_OBJECT (text_view->hadjustment));
5791 if (text_view->vadjustment && (text_view->vadjustment != vadj))
5793 gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5794 g_object_unref (G_OBJECT (text_view->vadjustment));
5797 if (text_view->hadjustment != hadj)
5799 text_view->hadjustment = hadj;
5800 g_object_ref (G_OBJECT (text_view->hadjustment));
5801 gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5803 gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5804 (GtkSignalFunc) gtk_text_view_value_changed,
5809 if (text_view->vadjustment != vadj)
5811 text_view->vadjustment = vadj;
5812 g_object_ref (G_OBJECT (text_view->vadjustment));
5813 gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5815 gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5816 (GtkSignalFunc) gtk_text_view_value_changed,
5822 gtk_text_view_value_changed (NULL, text_view);
5826 gtk_text_view_value_changed (GtkAdjustment *adj,
5827 GtkTextView *text_view)
5834 /* Note that we oddly call this function with adj == NULL
5838 text_view->onscreen_validated = FALSE;
5840 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5841 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5842 adj ? adj->value : 0.0));
5844 if (adj == text_view->hadjustment)
5846 dx = text_view->xoffset - (gint)adj->value;
5847 text_view->xoffset = adj->value;
5849 else if (adj == text_view->vadjustment)
5851 dy = text_view->yoffset - (gint)adj->value;
5852 text_view->yoffset = adj->value;
5854 if (text_view->layout)
5856 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5858 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5860 text_view->first_para_pixels = adj->value - line_top;
5864 if (dx != 0 || dy != 0)
5868 if (GTK_WIDGET_REALIZED (text_view))
5872 if (text_view->left_window)
5873 text_window_scroll (text_view->left_window, 0, dy);
5874 if (text_view->right_window)
5875 text_window_scroll (text_view->right_window, 0, dy);
5880 if (text_view->top_window)
5881 text_window_scroll (text_view->top_window, dx, 0);
5882 if (text_view->bottom_window)
5883 text_window_scroll (text_view->bottom_window, dx, 0);
5886 /* It looks nicer to scroll the main area last, because
5887 * it takes a while, and making the side areas update
5888 * afterward emphasizes the slowness of scrolling the
5891 text_window_scroll (text_view->text_window, dx, dy);
5894 /* Children are now "moved" in the text window, poke
5895 * into widget->allocation for each child
5897 tmp_list = text_view->children;
5898 while (tmp_list != NULL)
5900 GtkTextViewChild *child = tmp_list->data;
5904 child->widget->allocation.x -= dx;
5905 child->widget->allocation.y -= dy;
5908 g_print ("allocation for %p tweaked to %d,%d\n",
5910 child->widget->allocation.x,
5911 child->widget->allocation.y);
5915 tmp_list = g_slist_next (tmp_list);
5919 /* This could result in invalidation, which would install the
5920 * first_validate_idle, which would validate onscreen;
5921 * but we're going to go ahead and validate here, so
5922 * first_validate_idle shouldn't have anything to do.
5924 gtk_text_view_update_layout_width (text_view);
5926 /* note that validation of onscreen could invoke this function
5927 * recursively, by scrolling to maintain first_para, or in response
5928 * to updating the layout width, however there is no problem with
5929 * that, or shouldn't be.
5931 gtk_text_view_validate_onscreen (text_view);
5933 /* process exposes */
5934 if (GTK_WIDGET_REALIZED (text_view))
5936 if (text_view->left_window)
5937 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
5939 if (text_view->right_window)
5940 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
5942 if (text_view->top_window)
5943 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
5945 if (text_view->bottom_window)
5946 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
5948 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
5951 /* If this got installed, get rid of it, it's just a waste of time. */
5952 if (text_view->first_validate_idle != 0)
5954 g_source_remove (text_view->first_validate_idle);
5955 text_view->first_validate_idle = 0;
5958 gtk_text_view_update_im_spot_location (text_view);
5960 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
5964 gtk_text_view_commit_handler (GtkIMContext *context,
5966 GtkTextView *text_view)
5968 gtk_text_view_commit_text (text_view, str);
5972 gtk_text_view_commit_text (GtkTextView *text_view,
5975 gboolean had_selection;
5977 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5979 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5982 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5983 text_view->editable);
5985 if (!strcmp (str, "\n"))
5987 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
5988 text_view->editable);
5992 if (!had_selection && text_view->overwrite_mode)
5993 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
5994 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5995 text_view->editable);
5998 gtk_text_buffer_end_user_action (get_buffer (text_view));
6000 DV(g_print (G_STRLOC": scrolling onscreen\n"));
6001 gtk_text_view_scroll_mark_onscreen (text_view,
6002 gtk_text_buffer_get_mark (get_buffer (text_view),
6007 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6008 GtkTextView *text_view)
6011 PangoAttrList *attrs;
6014 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6015 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6017 pango_attr_list_unref (attrs);
6022 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
6023 GtkTextView *text_view)
6030 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6031 gtk_text_buffer_get_insert (text_view->buffer));
6034 pos = gtk_text_iter_get_line_index (&start);
6035 gtk_text_iter_set_line_offset (&start, 0);
6036 gtk_text_iter_forward_to_line_end (&end);
6038 text = gtk_text_iter_get_slice (&start, &end);
6039 gtk_im_context_set_surrounding (context, text, -1, pos);
6046 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
6049 GtkTextView *text_view)
6054 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
6055 gtk_text_buffer_get_insert (text_view->buffer));
6058 gtk_text_iter_forward_chars (&start, offset);
6059 gtk_text_iter_forward_chars (&end, offset + n_chars);
6061 gtk_text_buffer_delete (text_view->buffer, &start, &end);
6067 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
6068 const GtkTextIter *location,
6072 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6073 gboolean need_reset = FALSE;
6075 if (mark == gtk_text_buffer_get_insert (buffer))
6077 text_view->virtual_cursor_x = -1;
6078 text_view->virtual_cursor_y = -1;
6079 gtk_text_view_update_im_spot_location (text_view);
6082 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6088 gtk_text_view_reset_im_context (text_view);
6092 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6096 GdkRectangle strong_pos;
6099 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6100 gtk_text_buffer_get_mark (get_buffer (text_view),
6103 if ((x && text_view->virtual_cursor_x == -1) ||
6104 (y && text_view->virtual_cursor_y == -1))
6105 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6109 if (text_view->virtual_cursor_x != -1)
6110 *x = text_view->virtual_cursor_x;
6117 if (text_view->virtual_cursor_x != -1)
6118 *y = text_view->virtual_cursor_y;
6120 *y = strong_pos.y + strong_pos.height / 2;
6125 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6129 GdkRectangle strong_pos;
6132 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6133 gtk_text_buffer_get_mark (get_buffer (text_view),
6136 if (x == -1 || y == -1)
6137 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6139 text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6140 text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6143 /* Quick hack of a popup menu
6146 activate_cb (GtkWidget *menuitem,
6147 GtkTextView *text_view)
6149 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6150 gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
6154 append_action_signal (GtkTextView *text_view,
6156 const gchar *stock_id,
6157 const gchar *signal,
6160 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6162 g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6163 gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
6164 GTK_SIGNAL_FUNC (activate_cb), text_view);
6166 gtk_widget_set_sensitive (menuitem, sensitive);
6168 gtk_widget_show (menuitem);
6169 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6173 popup_menu_detach (GtkWidget *attach_widget,
6176 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6180 popup_position_func (GtkMenu *menu,
6186 GtkTextView *text_view;
6188 GdkRectangle cursor_rect;
6189 GdkRectangle onscreen_rect;
6190 gint root_x, root_y;
6194 text_view = GTK_TEXT_VIEW (user_data);
6195 widget = GTK_WIDGET (text_view);
6197 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6199 gdk_window_get_origin (widget->window, &root_x, &root_y);
6201 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6203 gtk_text_buffer_get_insert (get_buffer (text_view)));
6205 gtk_text_view_get_iter_location (text_view,
6209 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6211 gtk_widget_size_request (text_view->popup_menu, &req);
6213 /* can't use rectangle_intersect since cursor rect can have 0 width */
6214 if (cursor_rect.x >= onscreen_rect.x &&
6215 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6216 cursor_rect.y >= onscreen_rect.y &&
6217 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6219 gtk_text_view_buffer_to_window_coords (text_view,
6220 GTK_TEXT_WINDOW_WIDGET,
6221 cursor_rect.x, cursor_rect.y,
6222 &cursor_rect.x, &cursor_rect.y);
6224 *x = root_x + cursor_rect.x + cursor_rect.width;
6225 *y = root_y + cursor_rect.y + cursor_rect.height;
6229 /* Just center the menu, since cursor is offscreen. */
6230 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6231 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
6235 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6236 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6238 *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
6239 *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
6244 GtkTextView *text_view;
6250 range_contains_editable_text (const GtkTextIter *start,
6251 const GtkTextIter *end,
6252 gboolean default_editability)
6254 GtkTextIter iter = *start;
6256 while (gtk_text_iter_compare (&iter, end) < 0)
6258 if (gtk_text_iter_editable (&iter, default_editability))
6261 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6268 unichar_chosen_func (const char *text,
6271 GtkTextView *text_view = GTK_TEXT_VIEW (data);
6273 gtk_text_view_commit_text (text_view, text);
6277 menu_select_first_child (GtkMenu *menu)
6279 GList *children = gtk_container_get_children (GTK_CONTAINER (menu));
6281 GList *tmp_list = children;
6284 GtkWidget *child = tmp_list->data;
6286 if (GTK_WIDGET_VISIBLE (child))
6288 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu), child);
6292 tmp_list = tmp_list->next;
6295 g_list_free (children);
6299 popup_targets_received (GtkClipboard *clipboard,
6300 GtkSelectionData *data,
6303 PopupInfo *info = user_data;
6304 GtkTextView *text_view = info->text_view;
6306 if (GTK_WIDGET_REALIZED (text_view))
6308 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6309 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6311 gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6312 GtkWidget *menuitem;
6314 gboolean have_selection;
6315 gboolean can_insert;
6317 GtkTextIter sel_start, sel_end;
6319 if (text_view->popup_menu)
6320 gtk_widget_destroy (text_view->popup_menu);
6322 text_view->popup_menu = gtk_menu_new ();
6324 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6325 GTK_WIDGET (text_view),
6328 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6329 &sel_start, &sel_end);
6331 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6333 gtk_text_buffer_get_insert (get_buffer (text_view)));
6335 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6337 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6339 range_contains_editable_text (&sel_start, &sel_end,
6340 text_view->editable));
6341 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6343 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6344 can_insert && clipboard_contains_text);
6346 menuitem = gtk_separator_menu_item_new ();
6347 gtk_widget_show (menuitem);
6348 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6350 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6351 gtk_widget_show (menuitem);
6352 submenu = gtk_menu_new ();
6353 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6354 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6356 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6357 GTK_MENU_SHELL (submenu));
6359 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6360 gtk_widget_show (menuitem);
6361 gtk_widget_set_sensitive (menuitem, can_insert);
6363 submenu = gtk_menu_new ();
6364 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6365 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6367 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6368 unichar_chosen_func,
6371 gtk_signal_emit (GTK_OBJECT (text_view),
6372 signals[POPULATE_POPUP],
6373 text_view->popup_menu);
6376 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6378 info->button, info->time);
6381 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6382 popup_position_func, text_view,
6383 0, gtk_get_current_event_time ());
6384 menu_select_first_child (GTK_MENU (text_view->popup_menu));
6388 g_object_unref (text_view);
6393 gtk_text_view_do_popup (GtkTextView *text_view,
6394 GdkEventButton *event)
6396 PopupInfo *info = g_new (PopupInfo, 1);
6398 /* In order to know what entries we should make sensitive, we
6399 * ask for the current targets of the clipboard, and when
6400 * we get them, then we actually pop up the menu.
6402 info->text_view = g_object_ref (text_view);
6406 info->button = event->button;
6407 info->time = event->time;
6412 info->time = gtk_get_current_event_time ();
6415 gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
6416 gdk_atom_intern ("TARGETS", FALSE),
6417 popup_targets_received,
6422 gtk_text_view_popup_menu (GtkWidget *widget)
6424 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
6427 /* Child GdkWindows */
6430 static GtkTextWindow*
6431 text_window_new (GtkTextWindowType type,
6434 gint height_request)
6438 win = g_new (GtkTextWindow, 1);
6441 win->widget = widget;
6443 win->bin_window = NULL;
6444 win->requisition.width = width_request;
6445 win->requisition.height = height_request;
6446 win->allocation.width = width_request;
6447 win->allocation.height = height_request;
6448 win->allocation.x = 0;
6449 win->allocation.y = 0;
6455 text_window_free (GtkTextWindow *win)
6458 text_window_unrealize (win);
6464 text_window_realize (GtkTextWindow *win,
6467 GdkWindowAttr attributes;
6468 gint attributes_mask;
6471 attributes.window_type = GDK_WINDOW_CHILD;
6472 attributes.x = win->allocation.x;
6473 attributes.y = win->allocation.y;
6474 attributes.width = win->allocation.width;
6475 attributes.height = win->allocation.height;
6476 attributes.wclass = GDK_INPUT_OUTPUT;
6477 attributes.visual = gtk_widget_get_visual (win->widget);
6478 attributes.colormap = gtk_widget_get_colormap (win->widget);
6479 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6481 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6483 win->window = gdk_window_new (parent,
6487 gdk_window_show (win->window);
6488 gdk_window_set_user_data (win->window, win->widget);
6492 attributes.width = win->allocation.width;
6493 attributes.height = win->allocation.height;
6494 attributes.event_mask = (GDK_EXPOSURE_MASK |
6496 GDK_KEY_PRESS_MASK |
6497 GDK_BUTTON_PRESS_MASK |
6498 GDK_BUTTON_RELEASE_MASK |
6499 GDK_POINTER_MOTION_MASK |
6500 GDK_POINTER_MOTION_HINT_MASK |
6501 gtk_widget_get_events (win->widget));
6503 win->bin_window = gdk_window_new (win->window,
6507 gdk_window_show (win->bin_window);
6508 gdk_window_set_user_data (win->bin_window, win->widget);
6510 if (win->type == GTK_TEXT_WINDOW_TEXT)
6513 cursor = gdk_cursor_new (GDK_XTERM);
6514 gdk_window_set_cursor (win->bin_window, cursor);
6515 gdk_cursor_unref (cursor);
6517 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6521 gdk_window_set_background (win->bin_window,
6522 &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6526 gdk_window_set_background (win->bin_window,
6527 &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6530 g_object_set_qdata (G_OBJECT (win->window),
6531 g_quark_from_static_string ("gtk-text-view-text-window"),
6534 g_object_set_qdata (G_OBJECT (win->bin_window),
6535 g_quark_from_static_string ("gtk-text-view-text-window"),
6540 text_window_unrealize (GtkTextWindow *win)
6542 if (win->type == GTK_TEXT_WINDOW_TEXT)
6544 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6548 gdk_window_set_user_data (win->window, NULL);
6549 gdk_window_set_user_data (win->bin_window, NULL);
6550 gdk_window_destroy (win->bin_window);
6551 gdk_window_destroy (win->window);
6553 win->bin_window = NULL;
6557 text_window_size_allocate (GtkTextWindow *win,
6560 win->allocation = *rect;
6564 gdk_window_move_resize (win->window,
6566 rect->width, rect->height);
6568 gdk_window_resize (win->bin_window,
6569 rect->width, rect->height);
6574 text_window_scroll (GtkTextWindow *win,
6578 if (dx != 0 || dy != 0)
6580 gdk_window_scroll (win->bin_window, dx, dy);
6585 text_window_invalidate_rect (GtkTextWindow *win,
6588 GdkRectangle window_rect;
6590 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6597 window_rect.width = rect->width;
6598 window_rect.height = rect->height;
6600 /* Adjust the rect as appropriate */
6604 case GTK_TEXT_WINDOW_TEXT:
6607 case GTK_TEXT_WINDOW_LEFT:
6608 case GTK_TEXT_WINDOW_RIGHT:
6610 window_rect.width = win->allocation.width;
6613 case GTK_TEXT_WINDOW_TOP:
6614 case GTK_TEXT_WINDOW_BOTTOM:
6616 window_rect.height = win->allocation.height;
6620 g_warning ("%s: bug!", G_STRLOC);
6625 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6629 GdkColor color = { 0, 65535, 0, 0 };
6630 GdkGC *gc = gdk_gc_new (win->bin_window);
6631 gdk_gc_set_rgb_fg_color (gc, &color);
6632 gdk_draw_rectangle (win->bin_window,
6633 gc, TRUE, window_rect.x, window_rect.y,
6634 window_rect.width, window_rect.height);
6641 text_window_get_width (GtkTextWindow *win)
6643 return win->allocation.width;
6647 text_window_get_height (GtkTextWindow *win)
6649 return win->allocation.height;
6653 text_window_get_allocation (GtkTextWindow *win,
6656 *rect = win->allocation;
6663 * gtk_text_view_get_window:
6664 * @text_view: a #GtkTextView
6665 * @win: window to get
6667 * Retrieves the #GdkWindow corresponding to an area of the text view;
6668 * possible windows include the overall widget window, child windows
6669 * on the left, right, top, bottom, and the window that displays the
6670 * text buffer. Windows are %NULL and nonexistent if their width or
6671 * height is 0, and are nonexistent before the widget has been
6674 * Return value: a #GdkWindow, or %NULL
6677 gtk_text_view_get_window (GtkTextView *text_view,
6678 GtkTextWindowType win)
6680 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6684 case GTK_TEXT_WINDOW_WIDGET:
6685 return GTK_WIDGET (text_view)->window;
6688 case GTK_TEXT_WINDOW_TEXT:
6689 return text_view->text_window->bin_window;
6692 case GTK_TEXT_WINDOW_LEFT:
6693 if (text_view->left_window)
6694 return text_view->left_window->bin_window;
6699 case GTK_TEXT_WINDOW_RIGHT:
6700 if (text_view->right_window)
6701 return text_view->right_window->bin_window;
6706 case GTK_TEXT_WINDOW_TOP:
6707 if (text_view->top_window)
6708 return text_view->top_window->bin_window;
6713 case GTK_TEXT_WINDOW_BOTTOM:
6714 if (text_view->bottom_window)
6715 return text_view->bottom_window->bin_window;
6721 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6728 * gtk_text_view_get_window_type:
6729 * @text_view: a #GtkTextView
6730 * @window: a window type
6732 * Usually used to find out which window an event corresponds to.
6733 * If you connect to an event signal on @text_view, this function
6734 * should be called on <literal>event->window</literal> to
6735 * see which window it was.
6737 * Return value: the window type.
6740 gtk_text_view_get_window_type (GtkTextView *text_view,
6745 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6746 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6748 if (window == GTK_WIDGET (text_view)->window)
6749 return GTK_TEXT_WINDOW_WIDGET;
6751 win = g_object_get_qdata (G_OBJECT (window),
6752 g_quark_try_string ("gtk-text-view-text-window"));
6758 return GTK_TEXT_WINDOW_PRIVATE;
6763 buffer_to_widget (GtkTextView *text_view,
6769 gint focus_edge_width;
6770 gboolean interior_focus;
6773 gtk_widget_style_get (GTK_WIDGET (text_view),
6774 "interior_focus", &interior_focus,
6775 "focus_line_width", &focus_width,
6779 focus_edge_width = 0;
6781 focus_edge_width = focus_width;
6785 *window_x = buffer_x - text_view->xoffset + focus_edge_width;
6786 if (text_view->left_window)
6787 *window_x += text_view->left_window->allocation.width;
6792 *window_y = buffer_y - text_view->yoffset + focus_edge_width;
6793 if (text_view->top_window)
6794 *window_y += text_view->top_window->allocation.height;
6799 widget_to_text_window (GtkTextWindow *win,
6806 *window_x = widget_x - win->allocation.x;
6809 *window_y = widget_y - win->allocation.y;
6813 buffer_to_text_window (GtkTextView *text_view,
6822 g_warning ("Attempt to convert text buffer coordinates to coordinates "
6823 "for a nonexistent or private child window of GtkTextView");
6827 buffer_to_widget (text_view,
6829 window_x, window_y);
6831 widget_to_text_window (win,
6832 window_x ? *window_x : 0,
6833 window_y ? *window_y : 0,
6839 * gtk_text_view_buffer_to_window_coords:
6840 * @text_view: a #GtkTextView
6841 * @win: a #GtkTextWindowType
6842 * @buffer_x: buffer x coordinate
6843 * @buffer_y: buffer y coordinate
6844 * @window_x: window x coordinate return location
6845 * @window_y: window y coordinate return location
6847 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6848 * @win, and stores the result in (@window_x, @window_y).
6851 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
6852 GtkTextWindowType win,
6858 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6862 case GTK_TEXT_WINDOW_WIDGET:
6863 buffer_to_widget (text_view,
6865 window_x, window_y);
6868 case GTK_TEXT_WINDOW_TEXT:
6870 *window_x = buffer_x - text_view->xoffset;
6872 *window_y = buffer_y - text_view->yoffset;
6875 case GTK_TEXT_WINDOW_LEFT:
6876 buffer_to_text_window (text_view,
6877 text_view->left_window,
6879 window_x, window_y);
6882 case GTK_TEXT_WINDOW_RIGHT:
6883 buffer_to_text_window (text_view,
6884 text_view->right_window,
6886 window_x, window_y);
6889 case GTK_TEXT_WINDOW_TOP:
6890 buffer_to_text_window (text_view,
6891 text_view->top_window,
6893 window_x, window_y);
6896 case GTK_TEXT_WINDOW_BOTTOM:
6897 buffer_to_text_window (text_view,
6898 text_view->bottom_window,
6900 window_x, window_y);
6903 case GTK_TEXT_WINDOW_PRIVATE:
6904 g_warning ("%s: can't get coords for private windows", G_STRLOC);
6908 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6914 widget_to_buffer (GtkTextView *text_view,
6920 gint focus_edge_width;
6921 gboolean interior_focus;
6924 gtk_widget_style_get (GTK_WIDGET (text_view),
6925 "interior_focus", &interior_focus,
6926 "focus_line_width", &focus_width,
6930 focus_edge_width = 0;
6932 focus_edge_width = focus_width;
6936 *buffer_x = widget_x - focus_edge_width + text_view->xoffset;
6937 if (text_view->left_window)
6938 *buffer_x -= text_view->left_window->allocation.width;
6943 *buffer_y = widget_y - focus_edge_width + text_view->yoffset;
6944 if (text_view->top_window)
6945 *buffer_y -= text_view->top_window->allocation.height;
6950 text_window_to_widget (GtkTextWindow *win,
6957 *widget_x = window_x + win->allocation.x;
6960 *widget_y = window_y + win->allocation.y;
6964 text_window_to_buffer (GtkTextView *text_view,
6973 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
6974 "coordinates for a nonexistent child window.");
6978 text_window_to_widget (win,
6984 widget_to_buffer (text_view,
6985 buffer_x ? *buffer_x : 0,
6986 buffer_y ? *buffer_y : 0,
6992 * gtk_text_view_window_to_buffer_coords:
6993 * @text_view: a #GtkTextView
6994 * @win: a #GtkTextWindowType
6995 * @window_x: window x coordinate
6996 * @window_y: window y coordinate
6997 * @buffer_x: buffer x coordinate return location
6998 * @buffer_y: buffer y coordinate return location
7000 * Converts coordinates on the window identified by @win to buffer
7001 * coordinates, storing the result in (@buffer_x,@buffer_y).
7004 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
7005 GtkTextWindowType win,
7011 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7015 case GTK_TEXT_WINDOW_WIDGET:
7016 widget_to_buffer (text_view,
7018 buffer_x, buffer_y);
7021 case GTK_TEXT_WINDOW_TEXT:
7023 *buffer_x = window_x + text_view->xoffset;
7025 *buffer_y = window_y + text_view->yoffset;
7028 case GTK_TEXT_WINDOW_LEFT:
7029 text_window_to_buffer (text_view,
7030 text_view->left_window,
7032 buffer_x, buffer_y);
7035 case GTK_TEXT_WINDOW_RIGHT:
7036 text_window_to_buffer (text_view,
7037 text_view->right_window,
7039 buffer_x, buffer_y);
7042 case GTK_TEXT_WINDOW_TOP:
7043 text_window_to_buffer (text_view,
7044 text_view->top_window,
7046 buffer_x, buffer_y);
7049 case GTK_TEXT_WINDOW_BOTTOM:
7050 text_window_to_buffer (text_view,
7051 text_view->bottom_window,
7053 buffer_x, buffer_y);
7056 case GTK_TEXT_WINDOW_PRIVATE:
7057 g_warning ("%s: can't get coords for private windows", G_STRLOC);
7061 g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7067 set_window_width (GtkTextView *text_view,
7069 GtkTextWindowType type,
7070 GtkTextWindow **winp)
7076 text_window_free (*winp);
7078 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7085 *winp = text_window_new (type,
7086 GTK_WIDGET (text_view),
7088 /* if the widget is already realized we need to realize the child manually */
7089 if (GTK_WIDGET_REALIZED (text_view))
7090 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7094 if ((*winp)->requisition.width == width)
7097 (*winp)->requisition.width = width;
7100 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7106 set_window_height (GtkTextView *text_view,
7108 GtkTextWindowType type,
7109 GtkTextWindow **winp)
7115 text_window_free (*winp);
7117 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7124 *winp = text_window_new (type,
7125 GTK_WIDGET (text_view),
7128 /* if the widget is already realized we need to realize the child manually */
7129 if (GTK_WIDGET_REALIZED (text_view))
7130 text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7134 if ((*winp)->requisition.height == height)
7137 (*winp)->requisition.height = height;
7140 gtk_widget_queue_resize (GTK_WIDGET (text_view));
7145 * gtk_text_view_set_border_window_size:
7146 * @text_view: a #GtkTextView
7147 * @type: window to affect
7148 * @size: width or height of the window
7150 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7151 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7152 * Automatically destroys the corresponding window if the size is set
7153 * to 0, and creates the window if the size is set to non-zero. This
7154 * function can only be used for the "border windows," it doesn't work
7155 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7156 * #GTK_TEXT_WINDOW_PRIVATE.
7159 gtk_text_view_set_border_window_size (GtkTextView *text_view,
7160 GtkTextWindowType type,
7164 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7165 g_return_if_fail (size >= 0);
7169 case GTK_TEXT_WINDOW_LEFT:
7170 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7171 &text_view->left_window);
7174 case GTK_TEXT_WINDOW_RIGHT:
7175 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7176 &text_view->right_window);
7179 case GTK_TEXT_WINDOW_TOP:
7180 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7181 &text_view->top_window);
7184 case GTK_TEXT_WINDOW_BOTTOM:
7185 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7186 &text_view->bottom_window);
7190 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7196 * gtk_text_view_get_border_window_size:
7197 * @text_view: a #GtkTextView
7198 * @type: window to return size from
7200 * Gets the width of the specified border window. See
7201 * gtk_text_view_set_border_window_size().
7203 * Return value: width of window
7206 gtk_text_view_get_border_window_size (GtkTextView *text_view,
7207 GtkTextWindowType type)
7209 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7213 case GTK_TEXT_WINDOW_LEFT:
7214 if (text_view->left_window)
7215 return text_view->left_window->requisition.width;
7217 case GTK_TEXT_WINDOW_RIGHT:
7218 if (text_view->right_window)
7219 return text_view->right_window->requisition.width;
7221 case GTK_TEXT_WINDOW_TOP:
7222 if (text_view->top_window)
7223 return text_view->top_window->requisition.height;
7225 case GTK_TEXT_WINDOW_BOTTOM:
7226 if (text_view->bottom_window)
7227 return text_view->bottom_window->requisition.height;
7230 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7241 static GtkTextViewChild*
7242 text_view_child_new_anchored (GtkWidget *child,
7243 GtkTextChildAnchor *anchor,
7244 GtkTextLayout *layout)
7246 GtkTextViewChild *vc;
7248 vc = g_new (GtkTextViewChild, 1);
7251 vc->anchor = anchor;
7253 vc->from_top_of_line = 0;
7254 vc->from_left_of_buffer = 0;
7256 g_object_ref (G_OBJECT (vc->widget));
7257 g_object_ref (G_OBJECT (vc->anchor));
7259 g_object_set_data (G_OBJECT (child),
7260 "gtk-text-view-child",
7263 gtk_text_child_anchor_register_child (anchor, child, layout);
7268 static GtkTextViewChild*
7269 text_view_child_new_window (GtkWidget *child,
7270 GtkTextWindowType type,
7274 GtkTextViewChild *vc;
7276 vc = g_new (GtkTextViewChild, 1);
7281 vc->from_top_of_line = 0;
7282 vc->from_left_of_buffer = 0;
7284 g_object_ref (G_OBJECT (vc->widget));
7290 g_object_set_data (G_OBJECT (child),
7291 "gtk-text-view-child",
7298 text_view_child_free (GtkTextViewChild *child)
7300 g_object_set_data (G_OBJECT (child->widget),
7301 "gtk-text-view-child", NULL);
7305 gtk_text_child_anchor_unregister_child (child->anchor,
7307 g_object_unref (G_OBJECT (child->anchor));
7310 g_object_unref (G_OBJECT (child->widget));
7316 text_view_child_set_parent_window (GtkTextView *text_view,
7317 GtkTextViewChild *vc)
7320 gtk_widget_set_parent_window (vc->widget,
7321 text_view->text_window->bin_window);
7325 window = gtk_text_view_get_window (text_view,
7327 gtk_widget_set_parent_window (vc->widget, window);
7332 add_child (GtkTextView *text_view,
7333 GtkTextViewChild *vc)
7335 text_view->children = g_slist_prepend (text_view->children,
7338 if (GTK_WIDGET_REALIZED (text_view))
7339 text_view_child_set_parent_window (text_view, vc);
7341 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7345 * gtk_text_view_add_child_at_anchor:
7346 * @text_view: a #GtkTextView
7347 * @child: a #GtkWidget
7348 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7350 * Adds a child widget in the text buffer, at the given @anchor.
7354 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
7356 GtkTextChildAnchor *anchor)
7358 GtkTextViewChild *vc;
7360 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7361 g_return_if_fail (GTK_IS_WIDGET (child));
7362 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7363 g_return_if_fail (child->parent == NULL);
7365 gtk_text_view_ensure_layout (text_view);
7367 vc = text_view_child_new_anchored (child, anchor,
7370 add_child (text_view, vc);
7372 g_assert (vc->widget == child);
7373 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7377 * gtk_text_view_add_child_in_window:
7378 * @text_view: a #GtkTextView
7379 * @child: a #GtkWidget
7380 * @which_window: which window the child should appear in
7381 * @xpos: X position of child in window coordinates
7382 * @ypos: Y position of child in window coordinates
7384 * Adds a child at fixed coordinates in one of the text widget's
7385 * windows. The window must have nonzero size (see
7386 * gtk_text_view_set_border_window_size()). Note that the child
7387 * coordinates are given relative to the #GdkWindow in question, and
7388 * that these coordinates have no sane relationship to scrolling. When
7389 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7390 * irrelevant, the child floats above all scrollable areas. But when
7391 * placing a child in one of the scrollable windows (border windows or
7392 * text window), you'll need to compute the child's correct position
7393 * in buffer coordinates any time scrolling occurs or buffer changes
7394 * occur, and then call gtk_text_view_move_child() to update the
7395 * child's position. Unfortunately there's no good way to detect that
7396 * scrolling has occurred, using the current API; a possible hack
7397 * would be to update all child positions when the scroll adjustments
7398 * change or the text buffer changes. See bug 64518 on
7399 * bugzilla.gnome.org for status of fixing this issue.
7403 gtk_text_view_add_child_in_window (GtkTextView *text_view,
7405 GtkTextWindowType which_window,
7409 GtkTextViewChild *vc;
7411 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7412 g_return_if_fail (GTK_IS_WIDGET (child));
7413 g_return_if_fail (child->parent == NULL);
7415 vc = text_view_child_new_window (child, which_window,
7418 add_child (text_view, vc);
7420 g_assert (vc->widget == child);
7421 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7425 * gtk_text_view_move_child:
7426 * @text_view: a #GtkTextView
7427 * @child: child widget already added to the text view
7428 * @xpos: new X position in window coordinates
7429 * @ypos: new Y position in window coordinates
7431 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7435 gtk_text_view_move_child (GtkTextView *text_view,
7440 GtkTextViewChild *vc;
7442 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7443 g_return_if_fail (GTK_IS_WIDGET (child));
7444 g_return_if_fail (child->parent == (GtkWidget*) text_view);
7446 vc = g_object_get_data (G_OBJECT (child),
7447 "gtk-text-view-child");
7449 g_assert (vc != NULL);
7451 if (vc->x == xpos &&
7458 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7459 gtk_widget_queue_resize (child);
7463 /* Iterator operations */
7466 * gtk_text_view_forward_display_line:
7467 * @text_view: a #GtkTextView
7468 * @iter: a #GtkTextIter
7470 * Moves the given @iter forward by one display (wrapped) line. A
7471 * display line is different from a paragraph. Paragraphs are
7472 * separated by newlines or other paragraph separator characters.
7473 * Display lines are created by line-wrapping a paragraph. If
7474 * wrapping is turned off, display lines and paragraphs will be the
7475 * same. Display lines are divided differently for each view, since
7476 * they depend on the view's width; paragraphs are the same in all
7477 * views, since they depend on the contents of the #GtkTextBuffer.
7479 * Return value: %TRUE if @iter was moved and is not on the end iterator
7482 gtk_text_view_forward_display_line (GtkTextView *text_view,
7485 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7486 g_return_val_if_fail (iter != NULL, FALSE);
7488 gtk_text_view_ensure_layout (text_view);
7490 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7494 * gtk_text_view_backward_display_line:
7495 * @text_view: a #GtkTextView
7496 * @iter: a #GtkTextIter
7498 * Moves the given @iter backward by one display (wrapped) line. A
7499 * display line is different from a paragraph. Paragraphs are
7500 * separated by newlines or other paragraph separator characters.
7501 * Display lines are created by line-wrapping a paragraph. If
7502 * wrapping is turned off, display lines and paragraphs will be the
7503 * same. Display lines are divided differently for each view, since
7504 * they depend on the view's width; paragraphs are the same in all
7505 * views, since they depend on the contents of the #GtkTextBuffer.
7507 * Return value: %TRUE if @iter was moved and is not on the end iterator
7510 gtk_text_view_backward_display_line (GtkTextView *text_view,
7513 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7514 g_return_val_if_fail (iter != NULL, FALSE);
7516 gtk_text_view_ensure_layout (text_view);
7518 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7522 * gtk_text_view_forward_display_line_end:
7523 * @text_view: a #GtkTextView
7524 * @iter: a #GtkTextIter
7526 * Moves the given @iter forward to the next display line end. A
7527 * display line is different from a paragraph. Paragraphs are
7528 * separated by newlines or other paragraph separator characters.
7529 * Display lines are created by line-wrapping a paragraph. If
7530 * wrapping is turned off, display lines and paragraphs will be the
7531 * same. Display lines are divided differently for each view, since
7532 * they depend on the view's width; paragraphs are the same in all
7533 * views, since they depend on the contents of the #GtkTextBuffer.
7535 * Return value: %TRUE if @iter was moved and is not on the end iterator
7538 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7541 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7542 g_return_val_if_fail (iter != NULL, FALSE);
7544 gtk_text_view_ensure_layout (text_view);
7546 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7550 * gtk_text_view_backward_display_line_start:
7551 * @text_view: a #GtkTextView
7552 * @iter: a #GtkTextIter
7554 * Moves the given @iter backward to the next display line start. A
7555 * display line is different from a paragraph. Paragraphs are
7556 * separated by newlines or other paragraph separator characters.
7557 * Display lines are created by line-wrapping a paragraph. If
7558 * wrapping is turned off, display lines and paragraphs will be the
7559 * same. Display lines are divided differently for each view, since
7560 * they depend on the view's width; paragraphs are the same in all
7561 * views, since they depend on the contents of the #GtkTextBuffer.
7563 * Return value: %TRUE if @iter was moved and is not on the end iterator
7566 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7569 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7570 g_return_val_if_fail (iter != NULL, FALSE);
7572 gtk_text_view_ensure_layout (text_view);
7574 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7578 * gtk_text_view_starts_display_line:
7579 * @text_view: a #GtkTextView
7580 * @iter: a #GtkTextIter
7582 * Determines whether @iter is at the start of a display line.
7583 * See gtk_text_view_forward_display_line() for an explanation of
7584 * display lines vs. paragraphs.
7586 * Return value: %TRUE if @iter begins a wrapped line
7589 gtk_text_view_starts_display_line (GtkTextView *text_view,
7590 const GtkTextIter *iter)
7592 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7593 g_return_val_if_fail (iter != NULL, FALSE);
7595 gtk_text_view_ensure_layout (text_view);
7597 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7601 * gtk_text_view_move_visually:
7602 * @text_view: a #GtkTextView
7603 * @iter: a #GtkTextIter
7604 * @count: number of lines to move
7606 * Moves @iter up or down by @count display (wrapped) lines.
7607 * See gtk_text_view_forward_display_line() for an explanation of
7608 * display lines vs. paragraphs.
7610 * Return value: %TRUE if @iter moved and is not on the end iterator
7613 gtk_text_view_move_visually (GtkTextView *text_view,
7617 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7618 g_return_val_if_fail (iter != NULL, FALSE);
7620 gtk_text_view_ensure_layout (text_view);
7622 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);