1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3 * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
32 #include "gtkbindings.h"
34 #include "gtkimagemenuitem.h"
37 #include "gtkmarshalers.h"
39 #include "gtkmenuitem.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtksettings.h"
43 #include "gtktextbufferrichtext.h"
44 #include "gtktextdisplay.h"
45 #include "gtktextview.h"
46 #include "gtkimmulticontext.h"
47 #include "gdk/gdkkeysyms.h"
48 #include "gtkprivate.h"
49 #include "gtktextutil.h"
50 #include "gtkwindow.h"
53 /* How scrolling, validation, exposes, etc. work.
55 * The expose_event handler has the invariant that the onscreen lines
56 * have been validated.
58 * There are two ways that onscreen lines can become invalid. The first
59 * is to change which lines are onscreen. This happens when the value
60 * of a scroll adjustment changes. So the code path begins in
61 * gtk_text_view_value_changed() and goes like this:
62 * - gdk_window_scroll() to reflect the new adjustment value
63 * - validate the lines that were moved onscreen
64 * - gdk_window_process_updates() to handle the exposes immediately
66 * The second way is that you get the "invalidated" signal from the layout,
67 * indicating that lines have become invalid. This code path begins in
68 * invalidated_handler() and goes like this:
69 * - install high-priority idle which does the rest of the steps
70 * - if a scroll is pending from scroll_to_mark(), do the scroll,
71 * jumping to the gtk_text_view_value_changed() code path
72 * - otherwise, validate the onscreen lines
73 * - DO NOT process updates
75 * In both cases, validating the onscreen lines can trigger a scroll
76 * due to maintaining the first_para on the top of the screen.
77 * If validation triggers a scroll, we jump to the top of the code path
78 * for value_changed, and bail out of the current code path.
80 * Also, in size_allocate, if we invalidate some lines from changing
81 * the layout width, we need to go ahead and run the high-priority idle,
82 * because GTK sends exposes right after doing the size allocates without
83 * returning to the main loop. This is also why the high-priority idle
84 * is at a higher priority than resizing.
89 #define DEBUG_VALIDATION_AND_SCROLLING
92 #ifdef DEBUG_VALIDATION_AND_SCROLLING
98 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
99 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
101 #define SPACE_FOR_CURSOR 1
103 typedef struct _GtkTextViewPrivate GtkTextViewPrivate;
105 #define GTK_TEXT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_VIEW, GtkTextViewPrivate))
107 struct _GtkTextViewPrivate
109 guint blink_time; /* time in msec the cursor has blinked since last user event */
114 struct _GtkTextPendingScroll
117 gdouble within_margin;
125 SET_SCROLL_ADJUSTMENTS,
139 TOGGLE_CURSOR_VISIBLE,
146 PROP_PIXELS_ABOVE_LINES,
147 PROP_PIXELS_BELOW_LINES,
148 PROP_PIXELS_INSIDE_WRAP,
163 static void gtk_text_view_destroy (GtkObject *object);
164 static void gtk_text_view_finalize (GObject *object);
165 static void gtk_text_view_set_property (GObject *object,
169 static void gtk_text_view_get_property (GObject *object,
173 static void gtk_text_view_size_request (GtkWidget *widget,
174 GtkRequisition *requisition);
175 static void gtk_text_view_size_allocate (GtkWidget *widget,
176 GtkAllocation *allocation);
177 static void gtk_text_view_realize (GtkWidget *widget);
178 static void gtk_text_view_unrealize (GtkWidget *widget);
179 static void gtk_text_view_style_set (GtkWidget *widget,
180 GtkStyle *previous_style);
181 static void gtk_text_view_direction_changed (GtkWidget *widget,
182 GtkTextDirection previous_direction);
183 static void gtk_text_view_grab_notify (GtkWidget *widget,
184 gboolean was_grabbed);
185 static void gtk_text_view_state_changed (GtkWidget *widget,
186 GtkStateType previous_state);
188 static gint gtk_text_view_event (GtkWidget *widget,
190 static gint gtk_text_view_key_press_event (GtkWidget *widget,
192 static gint gtk_text_view_key_release_event (GtkWidget *widget,
194 static gint gtk_text_view_button_press_event (GtkWidget *widget,
195 GdkEventButton *event);
196 static gint gtk_text_view_button_release_event (GtkWidget *widget,
197 GdkEventButton *event);
198 static gint gtk_text_view_focus_in_event (GtkWidget *widget,
199 GdkEventFocus *event);
200 static gint gtk_text_view_focus_out_event (GtkWidget *widget,
201 GdkEventFocus *event);
202 static gint gtk_text_view_motion_event (GtkWidget *widget,
203 GdkEventMotion *event);
204 static gint gtk_text_view_expose_event (GtkWidget *widget,
205 GdkEventExpose *expose);
206 static void gtk_text_view_draw_focus (GtkWidget *widget);
207 static gboolean gtk_text_view_focus (GtkWidget *widget,
208 GtkDirectionType direction);
209 static void gtk_text_view_move_focus (GtkWidget *widget,
210 GtkDirectionType direction_type);
211 static void gtk_text_view_select_all (GtkWidget *widget,
215 /* Source side drag signals */
216 static void gtk_text_view_drag_begin (GtkWidget *widget,
217 GdkDragContext *context);
218 static void gtk_text_view_drag_end (GtkWidget *widget,
219 GdkDragContext *context);
220 static void gtk_text_view_drag_data_get (GtkWidget *widget,
221 GdkDragContext *context,
222 GtkSelectionData *selection_data,
225 static void gtk_text_view_drag_data_delete (GtkWidget *widget,
226 GdkDragContext *context);
228 /* Target side drag signals */
229 static void gtk_text_view_drag_leave (GtkWidget *widget,
230 GdkDragContext *context,
232 static gboolean gtk_text_view_drag_motion (GtkWidget *widget,
233 GdkDragContext *context,
237 static gboolean gtk_text_view_drag_drop (GtkWidget *widget,
238 GdkDragContext *context,
242 static void gtk_text_view_drag_data_received (GtkWidget *widget,
243 GdkDragContext *context,
246 GtkSelectionData *selection_data,
250 static void gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
252 GtkAdjustment *vadj);
253 static gboolean gtk_text_view_popup_menu (GtkWidget *widget);
255 static void gtk_text_view_move_cursor (GtkTextView *text_view,
256 GtkMovementStep step,
258 gboolean extend_selection);
259 static void gtk_text_view_page_horizontally (GtkTextView *text_view,
261 gboolean extend_selection);
262 static gboolean gtk_text_view_move_viewport (GtkTextView *text_view,
265 static void gtk_text_view_set_anchor (GtkTextView *text_view);
266 static gboolean gtk_text_view_scroll_pages (GtkTextView *text_view,
268 gboolean extend_selection);
269 static gboolean gtk_text_view_scroll_hpages(GtkTextView *text_view,
271 gboolean extend_selection);
272 static void gtk_text_view_insert_at_cursor (GtkTextView *text_view,
274 static void gtk_text_view_delete_from_cursor (GtkTextView *text_view,
277 static void gtk_text_view_backspace (GtkTextView *text_view);
278 static void gtk_text_view_cut_clipboard (GtkTextView *text_view);
279 static void gtk_text_view_copy_clipboard (GtkTextView *text_view);
280 static void gtk_text_view_paste_clipboard (GtkTextView *text_view);
281 static void gtk_text_view_toggle_overwrite (GtkTextView *text_view);
282 static void gtk_text_view_toggle_cursor_visible (GtkTextView *text_view);
283 static void gtk_text_view_compat_move_focus(GtkTextView *text_view,
284 GtkDirectionType direction_type);
285 static void gtk_text_view_unselect (GtkTextView *text_view);
287 static void gtk_text_view_validate_onscreen (GtkTextView *text_view);
288 static void gtk_text_view_get_first_para_iter (GtkTextView *text_view,
290 static void gtk_text_view_update_layout_width (GtkTextView *text_view);
291 static void gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
292 GtkTextAttributes *values,
294 static void gtk_text_view_ensure_layout (GtkTextView *text_view);
295 static void gtk_text_view_destroy_layout (GtkTextView *text_view);
296 static void gtk_text_view_check_keymap_direction (GtkTextView *text_view);
297 static void gtk_text_view_reset_im_context (GtkTextView *text_view);
298 static void gtk_text_view_start_selection_drag (GtkTextView *text_view,
299 const GtkTextIter *iter,
300 GdkEventButton *event);
301 static gboolean gtk_text_view_end_selection_drag (GtkTextView *text_view);
302 static void gtk_text_view_start_selection_dnd (GtkTextView *text_view,
303 const GtkTextIter *iter,
304 GdkEventMotion *event);
305 static void gtk_text_view_check_cursor_blink (GtkTextView *text_view);
306 static void gtk_text_view_pend_cursor_blink (GtkTextView *text_view);
307 static void gtk_text_view_stop_cursor_blink (GtkTextView *text_view);
308 static void gtk_text_view_reset_blink_time (GtkTextView *text_view);
310 static void gtk_text_view_value_changed (GtkAdjustment *adj,
312 static void gtk_text_view_commit_handler (GtkIMContext *context,
314 GtkTextView *text_view);
315 static void gtk_text_view_commit_text (GtkTextView *text_view,
317 static void gtk_text_view_preedit_changed_handler (GtkIMContext *context,
318 GtkTextView *text_view);
319 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
320 GtkTextView *text_view);
321 static gboolean gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
324 GtkTextView *text_view);
326 static void gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
327 const GtkTextIter *location,
330 static void gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
331 const GParamSpec *pspec,
333 static void gtk_text_view_get_cursor_location (GtkTextView *text_view,
335 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
338 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
342 static GtkAdjustment* get_hadjustment (GtkTextView *text_view);
343 static GtkAdjustment* get_vadjustment (GtkTextView *text_view);
345 static void gtk_text_view_do_popup (GtkTextView *text_view,
346 GdkEventButton *event);
348 static void gtk_text_view_queue_scroll (GtkTextView *text_view,
350 gdouble within_margin,
355 static gboolean gtk_text_view_flush_scroll (GtkTextView *text_view);
356 static void gtk_text_view_update_adjustments (GtkTextView *text_view);
357 static void gtk_text_view_invalidate (GtkTextView *text_view);
358 static void gtk_text_view_flush_first_validate (GtkTextView *text_view);
360 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
362 /* Container methods */
363 static void gtk_text_view_add (GtkContainer *container,
365 static void gtk_text_view_remove (GtkContainer *container,
367 static void gtk_text_view_forall (GtkContainer *container,
368 gboolean include_internals,
369 GtkCallback callback,
370 gpointer callback_data);
372 /* FIXME probably need the focus methods. */
374 typedef struct _GtkTextViewChild GtkTextViewChild;
376 struct _GtkTextViewChild
380 GtkTextChildAnchor *anchor;
382 gint from_top_of_line;
383 gint from_left_of_buffer;
385 /* These are ignored if anchor != NULL */
386 GtkTextWindowType type;
391 static GtkTextViewChild* text_view_child_new_anchored (GtkWidget *child,
392 GtkTextChildAnchor *anchor,
393 GtkTextLayout *layout);
394 static GtkTextViewChild* text_view_child_new_window (GtkWidget *child,
395 GtkTextWindowType type,
398 static void text_view_child_free (GtkTextViewChild *child);
399 static void text_view_child_set_parent_window (GtkTextView *text_view,
400 GtkTextViewChild *child);
402 struct _GtkTextWindow
404 GtkTextWindowType type;
407 GdkWindow *bin_window;
408 GtkRequisition requisition;
409 GdkRectangle allocation;
412 static GtkTextWindow *text_window_new (GtkTextWindowType type,
415 gint height_request);
416 static void text_window_free (GtkTextWindow *win);
417 static void text_window_realize (GtkTextWindow *win,
419 static void text_window_unrealize (GtkTextWindow *win);
420 static void text_window_size_allocate (GtkTextWindow *win,
422 static void text_window_scroll (GtkTextWindow *win,
425 static void text_window_invalidate_rect (GtkTextWindow *win,
427 static void text_window_invalidate_cursors (GtkTextWindow *win);
429 static gint text_window_get_width (GtkTextWindow *win);
430 static gint text_window_get_height (GtkTextWindow *win);
433 static guint signals[LAST_SIGNAL] = { 0 };
435 G_DEFINE_TYPE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER)
438 add_move_binding (GtkBindingSet *binding_set,
441 GtkMovementStep step,
444 g_assert ((modmask & GDK_SHIFT_MASK) == 0);
446 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
450 G_TYPE_BOOLEAN, FALSE);
452 /* Selection-extending version */
453 gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
457 G_TYPE_BOOLEAN, TRUE);
461 gtk_text_view_class_init (GtkTextViewClass *klass)
463 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
464 GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
465 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
466 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
467 GtkBindingSet *binding_set;
469 /* Default handlers and virtual methods
471 gobject_class->set_property = gtk_text_view_set_property;
472 gobject_class->get_property = gtk_text_view_get_property;
474 object_class->destroy = gtk_text_view_destroy;
475 gobject_class->finalize = gtk_text_view_finalize;
477 widget_class->realize = gtk_text_view_realize;
478 widget_class->unrealize = gtk_text_view_unrealize;
479 widget_class->style_set = gtk_text_view_style_set;
480 widget_class->direction_changed = gtk_text_view_direction_changed;
481 widget_class->grab_notify = gtk_text_view_grab_notify;
482 widget_class->state_changed = gtk_text_view_state_changed;
483 widget_class->size_request = gtk_text_view_size_request;
484 widget_class->size_allocate = gtk_text_view_size_allocate;
485 widget_class->event = gtk_text_view_event;
486 widget_class->key_press_event = gtk_text_view_key_press_event;
487 widget_class->key_release_event = gtk_text_view_key_release_event;
488 widget_class->button_press_event = gtk_text_view_button_press_event;
489 widget_class->button_release_event = gtk_text_view_button_release_event;
490 widget_class->focus_in_event = gtk_text_view_focus_in_event;
491 widget_class->focus_out_event = gtk_text_view_focus_out_event;
492 widget_class->motion_notify_event = gtk_text_view_motion_event;
493 widget_class->expose_event = gtk_text_view_expose_event;
494 widget_class->focus = gtk_text_view_focus;
496 /* need to override the base class function via override_class_closure,
497 * because the signal slot is not available in GtkWidgetCLass
499 g_signal_override_class_closure (g_signal_lookup ("move-focus",
502 g_cclosure_new (G_CALLBACK (gtk_text_view_move_focus),
505 widget_class->drag_begin = gtk_text_view_drag_begin;
506 widget_class->drag_end = gtk_text_view_drag_end;
507 widget_class->drag_data_get = gtk_text_view_drag_data_get;
508 widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
510 widget_class->drag_leave = gtk_text_view_drag_leave;
511 widget_class->drag_motion = gtk_text_view_drag_motion;
512 widget_class->drag_drop = gtk_text_view_drag_drop;
513 widget_class->drag_data_received = gtk_text_view_drag_data_received;
515 widget_class->popup_menu = gtk_text_view_popup_menu;
517 container_class->add = gtk_text_view_add;
518 container_class->remove = gtk_text_view_remove;
519 container_class->forall = gtk_text_view_forall;
521 klass->move_cursor = gtk_text_view_move_cursor;
522 klass->page_horizontally = gtk_text_view_page_horizontally;
523 klass->set_anchor = gtk_text_view_set_anchor;
524 klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
525 klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
526 klass->backspace = gtk_text_view_backspace;
527 klass->cut_clipboard = gtk_text_view_cut_clipboard;
528 klass->copy_clipboard = gtk_text_view_copy_clipboard;
529 klass->paste_clipboard = gtk_text_view_paste_clipboard;
530 klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
531 klass->move_focus = gtk_text_view_compat_move_focus;
532 klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
538 g_object_class_install_property (gobject_class,
539 PROP_PIXELS_ABOVE_LINES,
540 g_param_spec_int ("pixels-above-lines",
541 P_("Pixels Above Lines"),
542 P_("Pixels of blank space above paragraphs"),
546 GTK_PARAM_READWRITE));
548 g_object_class_install_property (gobject_class,
549 PROP_PIXELS_BELOW_LINES,
550 g_param_spec_int ("pixels-below-lines",
551 P_("Pixels Below Lines"),
552 P_("Pixels of blank space below paragraphs"),
556 GTK_PARAM_READWRITE));
558 g_object_class_install_property (gobject_class,
559 PROP_PIXELS_INSIDE_WRAP,
560 g_param_spec_int ("pixels-inside-wrap",
561 P_("Pixels Inside Wrap"),
562 P_("Pixels of blank space between wrapped lines in a paragraph"),
566 GTK_PARAM_READWRITE));
568 g_object_class_install_property (gobject_class,
570 g_param_spec_boolean ("editable",
572 P_("Whether the text can be modified by the user"),
574 GTK_PARAM_READWRITE));
576 g_object_class_install_property (gobject_class,
578 g_param_spec_enum ("wrap-mode",
580 P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
583 GTK_PARAM_READWRITE));
585 g_object_class_install_property (gobject_class,
587 g_param_spec_enum ("justification",
589 P_("Left, right, or center justification"),
590 GTK_TYPE_JUSTIFICATION,
592 GTK_PARAM_READWRITE));
594 g_object_class_install_property (gobject_class,
596 g_param_spec_int ("left-margin",
598 P_("Width of the left margin in pixels"),
602 GTK_PARAM_READWRITE));
604 g_object_class_install_property (gobject_class,
606 g_param_spec_int ("right-margin",
608 P_("Width of the right margin in pixels"),
612 GTK_PARAM_READWRITE));
614 g_object_class_install_property (gobject_class,
616 g_param_spec_int ("indent",
618 P_("Amount to indent the paragraph, in pixels"),
622 GTK_PARAM_READWRITE));
624 g_object_class_install_property (gobject_class,
626 g_param_spec_boxed ("tabs",
628 P_("Custom tabs for this text"),
629 PANGO_TYPE_TAB_ARRAY,
630 GTK_PARAM_READWRITE));
632 g_object_class_install_property (gobject_class,
634 g_param_spec_boolean ("cursor-visible",
635 P_("Cursor Visible"),
636 P_("If the insertion cursor is shown"),
638 GTK_PARAM_READWRITE));
640 g_object_class_install_property (gobject_class,
642 g_param_spec_object ("buffer",
644 P_("The buffer which is displayed"),
645 GTK_TYPE_TEXT_BUFFER,
646 GTK_PARAM_READWRITE));
648 g_object_class_install_property (gobject_class,
650 g_param_spec_boolean ("overwrite",
651 P_("Overwrite mode"),
652 P_("Whether entered text overwrites existing contents"),
654 GTK_PARAM_READWRITE));
656 g_object_class_install_property (gobject_class,
658 g_param_spec_boolean ("accepts-tab",
660 P_("Whether Tab will result in a tab character being entered"),
662 GTK_PARAM_READWRITE));
667 gtk_widget_class_install_style_property (widget_class,
668 g_param_spec_boxed ("error-underline-color",
669 P_("Error underline color"),
670 P_("Color with which to draw error-indication underlines"),
672 GTK_PARAM_READABLE));
679 * GtkTextView::move-cursor:
680 * @text_view: the object which received the signal
681 * @step: the granularity of the move, as a #GtkMovementStep
682 * @count: the number of @step units to move
683 * @extend_selection: %TRUE if the move should extend the selection
685 * The ::move-cursor signal is a
686 * <link linkend="keybinding-signals">keybinding signal</link>
687 * which gets emitted when the user initiates a cursor movement.
688 * If the cursor is not visible in @text_view, this signal causes
689 * the viewport to be moved instead.
691 * Applications should not connect to it, but may emit it with
692 * g_signal_emit_by_name() if they need to control scrolling
695 * The default bindings for this signal come in two variants,
696 * the variant with the Shift modifier extends the selection,
697 * the variant without the Shift modifer does not.
698 * There are too many key combinations to list them all here.
700 * <listitem>Arrow keys move by individual characters/lines</listitem>
701 * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
702 * <listitem>Home/End keys move to the ends of the buffer</listitem>
703 * <listitem>PageUp/PageDown keys move vertically by pages</listitem>
704 * <listitem>Ctrl-PageUp/PageDown keys move horizontally by pages</listitem>
707 signals[MOVE_CURSOR] =
708 g_signal_new (I_("move_cursor"),
709 G_OBJECT_CLASS_TYPE (gobject_class),
710 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
711 G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
713 _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
715 GTK_TYPE_MOVEMENT_STEP,
720 * GtkTextView::page-horizontally:
721 * @text_view: the object which received the signal
722 * @count: the number of @step units to move
723 * @extend_selection: %TRUE if the move should extend the selection
725 * The ::page-horizontally signal is a
726 * <link linkend="keybinding-signals">keybinding signal</link>
727 * which can be bound to key combinations to allow the user
728 * to initiate horizontal cursor movement by pages.
730 * This signal should not be used anymore, instead use the
731 * #GtkTextview::move-cursor signal with the #GTK_MOVEMENT_HORIZONTAL_PAGES
734 signals[PAGE_HORIZONTALLY] =
735 g_signal_new (I_("page_horizontally"),
736 G_OBJECT_CLASS_TYPE (gobject_class),
737 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
738 G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
740 _gtk_marshal_VOID__INT_BOOLEAN,
746 * GtkTextView::move-viewport:
747 * @text_view: the object which received the signal
748 * @step: the granularity of the move, as a #GtkMovementStep
749 * @count: the number of @step units to move
751 * The ::move-viewport signal is a
752 * <link linkend="keybinding-signals">keybinding signal</link>
753 * which can be bound to key combinations to allow the user
754 * to move the viewport, i.e. change what part of the text view
755 * is visible in a containing scrolled window.
757 * There are no default bindings for this signal.
759 signals[MOVE_VIEWPORT] =
760 _gtk_binding_signal_new (I_("move_viewport"),
761 G_OBJECT_CLASS_TYPE (gobject_class),
762 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
763 G_CALLBACK (gtk_text_view_move_viewport),
765 _gtk_marshal_VOID__ENUM_INT,
767 GTK_TYPE_SCROLL_STEP,
770 signals[SET_ANCHOR] =
771 g_signal_new (I_("set_anchor"),
772 G_OBJECT_CLASS_TYPE (gobject_class),
773 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
774 G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
776 _gtk_marshal_VOID__VOID,
779 signals[INSERT_AT_CURSOR] =
780 g_signal_new (I_("insert_at_cursor"),
781 G_OBJECT_CLASS_TYPE (gobject_class),
782 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
783 G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
785 _gtk_marshal_VOID__STRING,
790 * GtkTextView::delete-from-cursor:
791 * @text_view: the object which received the signal
792 * @type: the granularity of the deletion, as a #GtkDeleteType
793 * @count: the number of @type units to delete
795 * The ::delete-from-cursor signal is a
796 * <link linkend="keybinding-signals">keybinding signal</link>
797 * which gets emitted when the user initiates a text deletion.
799 * If the @type is %GTK_DELETE_CHARS, GTK+ deletes the selection
800 * if there is one, otherwise it deletes the requested number
803 * The default bindings for this signal are
804 * Delete for deleting a character, Ctrl-Delete for
805 * deleting a word and Ctrl-Backspace for deleting a word
808 signals[DELETE_FROM_CURSOR] =
809 g_signal_new (I_("delete_from_cursor"),
810 G_OBJECT_CLASS_TYPE (gobject_class),
811 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
812 G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
814 _gtk_marshal_VOID__ENUM_INT,
816 GTK_TYPE_DELETE_TYPE,
820 * GtkTextView::backspace:
821 * @text_view: the object which received the signal
823 * The ::backspace signal is a
824 * <link linkend="keybinding-signals">keybinding signal</link>
825 * which gets emitted when the user asks for it.
827 * The default bindings for this signal are
828 * Backspace and Shift-Backspace.
831 g_signal_new (I_("backspace"),
832 G_OBJECT_CLASS_TYPE (gobject_class),
833 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
834 G_STRUCT_OFFSET (GtkTextViewClass, backspace),
836 _gtk_marshal_VOID__VOID,
840 * GtkTextView::cut-clipboard:
841 * @text_view: the object which received the signal
843 * The ::cut-clipboard signal is a
844 * <link linkend="keybinding-signals">keybinding signal</link>
845 * which gets emitted to cut the selection to the clipboard.
847 * The default bindings for this signal are
848 * Ctrl-x and Shift-Delete.
850 signals[CUT_CLIPBOARD] =
851 g_signal_new (I_("cut_clipboard"),
852 G_OBJECT_CLASS_TYPE (gobject_class),
853 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
854 G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
856 _gtk_marshal_VOID__VOID,
860 * GtkTextView::copy-clipboard:
861 * @text_view: the object which received the signal
863 * The ::copy-clipboard signal is a
864 * <link linkend="keybinding-signals">keybinding signal</link>
865 * which gets emitted to copy the selection to the clipboard.
867 * The default bindings for this signal are
868 * Ctrl-c and Ctrl-Insert.
870 signals[COPY_CLIPBOARD] =
871 g_signal_new (I_("copy_clipboard"),
872 G_OBJECT_CLASS_TYPE (gobject_class),
873 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
874 G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
876 _gtk_marshal_VOID__VOID,
880 * GtkTextView::paste-clipboard:
881 * @text_view: the object which received the signal
883 * The ::paste-clipboard signal is a
884 * <link linkend="keybinding-signals">keybinding signal</link>
885 * which gets emitted to paste the contents of the clipboard
886 * into the text view.
888 * The default bindings for this signal are
889 * Ctrl-v and Shift-Insert.
891 signals[PASTE_CLIPBOARD] =
892 g_signal_new (I_("paste_clipboard"),
893 G_OBJECT_CLASS_TYPE (gobject_class),
894 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
895 G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
897 _gtk_marshal_VOID__VOID,
901 * GtkTextView::toggle-overwrite:
902 * @text_view: the object which received the signal
904 * The ::toggle-overwrite signal is a
905 * <link linkend="keybinding-signals">keybinding signal</link>
906 * which gets emitted to change the editability of the text view.
908 * The default bindings for this signal is Insert.
910 signals[TOGGLE_OVERWRITE] =
911 g_signal_new (I_("toggle_overwrite"),
912 G_OBJECT_CLASS_TYPE (gobject_class),
913 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
914 G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
916 _gtk_marshal_VOID__VOID,
919 signals[SET_SCROLL_ADJUSTMENTS] =
920 g_signal_new (I_("set_scroll_adjustments"),
921 G_OBJECT_CLASS_TYPE (gobject_class),
922 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
923 G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
925 _gtk_marshal_VOID__OBJECT_OBJECT,
928 GTK_TYPE_ADJUSTMENT);
929 widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
931 signals[POPULATE_POPUP] =
932 g_signal_new (I_("populate_popup"),
933 G_OBJECT_CLASS_TYPE (gobject_class),
935 G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
937 _gtk_marshal_VOID__OBJECT,
942 * GtkTextView::select-all:
943 * @text_view: the object which received the signal
944 * @select: %TRUE to select, %FALSE to unselect
946 * The ::select-all signal is a
947 * <link linkend="keybinding-signals">keybinding signal</link>
948 * which gets emitted to select or unselect the complete
949 * contents of the text view.
951 * The default bindings for this signal are Ctrl-a and Ctrl-/
952 * for selecting and Shift-Ctrl-a and Ctrl-\ for unselecting.
954 signals[SELECT_ALL] =
955 _gtk_binding_signal_new (I_("select_all"),
956 G_OBJECT_CLASS_TYPE (object_class),
957 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
958 G_CALLBACK (gtk_text_view_select_all),
960 _gtk_marshal_VOID__BOOLEAN,
961 G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
964 * GtkTextView::toggle-cursor-visible:
965 * @text_view: the object which received the signal
967 * The ::toggle-cursor-visible signal is a
968 * <link linkend="keybinding-signals">keybinding signal</link>
969 * which gets emitted to toggle the visibility of the cursor.
971 * The default binding for this signal is F7.
973 signals[TOGGLE_CURSOR_VISIBLE] =
974 _gtk_binding_signal_new (I_("toggle_cursor_visible"),
975 G_OBJECT_CLASS_TYPE (object_class),
976 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
977 G_CALLBACK (gtk_text_view_toggle_cursor_visible),
979 _gtk_marshal_VOID__VOID,
986 binding_set = gtk_binding_set_by_class (klass);
988 /* Moving the insertion point */
989 add_move_binding (binding_set, GDK_Right, 0,
990 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
992 add_move_binding (binding_set, GDK_KP_Right, 0,
993 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
995 add_move_binding (binding_set, GDK_Left, 0,
996 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
998 add_move_binding (binding_set, GDK_KP_Left, 0,
999 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
1001 add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
1002 GTK_MOVEMENT_WORDS, 1);
1004 add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
1005 GTK_MOVEMENT_WORDS, 1);
1007 add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
1008 GTK_MOVEMENT_WORDS, -1);
1010 add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
1011 GTK_MOVEMENT_WORDS, -1);
1013 add_move_binding (binding_set, GDK_Up, 0,
1014 GTK_MOVEMENT_DISPLAY_LINES, -1);
1016 add_move_binding (binding_set, GDK_KP_Up, 0,
1017 GTK_MOVEMENT_DISPLAY_LINES, -1);
1019 add_move_binding (binding_set, GDK_Down, 0,
1020 GTK_MOVEMENT_DISPLAY_LINES, 1);
1022 add_move_binding (binding_set, GDK_KP_Down, 0,
1023 GTK_MOVEMENT_DISPLAY_LINES, 1);
1025 add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
1026 GTK_MOVEMENT_PARAGRAPHS, -1);
1028 add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
1029 GTK_MOVEMENT_PARAGRAPHS, -1);
1031 add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
1032 GTK_MOVEMENT_PARAGRAPHS, 1);
1034 add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
1035 GTK_MOVEMENT_PARAGRAPHS, 1);
1037 add_move_binding (binding_set, GDK_Home, 0,
1038 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1040 add_move_binding (binding_set, GDK_KP_Home, 0,
1041 GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
1043 add_move_binding (binding_set, GDK_End, 0,
1044 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1046 add_move_binding (binding_set, GDK_KP_End, 0,
1047 GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
1049 add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
1050 GTK_MOVEMENT_BUFFER_ENDS, -1);
1052 add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
1053 GTK_MOVEMENT_BUFFER_ENDS, -1);
1055 add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
1056 GTK_MOVEMENT_BUFFER_ENDS, 1);
1058 add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
1059 GTK_MOVEMENT_BUFFER_ENDS, 1);
1061 add_move_binding (binding_set, GDK_Page_Up, 0,
1062 GTK_MOVEMENT_PAGES, -1);
1064 add_move_binding (binding_set, GDK_KP_Page_Up, 0,
1065 GTK_MOVEMENT_PAGES, -1);
1067 add_move_binding (binding_set, GDK_Page_Down, 0,
1068 GTK_MOVEMENT_PAGES, 1);
1070 add_move_binding (binding_set, GDK_KP_Page_Down, 0,
1071 GTK_MOVEMENT_PAGES, 1);
1073 add_move_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
1074 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1076 add_move_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
1077 GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
1079 add_move_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
1080 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1082 add_move_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
1083 GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
1086 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
1088 G_TYPE_BOOLEAN, TRUE);
1090 gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
1092 G_TYPE_BOOLEAN, TRUE);
1095 gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
1097 G_TYPE_BOOLEAN, FALSE);
1099 gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1101 G_TYPE_BOOLEAN, FALSE);
1104 gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
1105 "delete_from_cursor", 2,
1106 G_TYPE_ENUM, GTK_DELETE_CHARS,
1109 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
1110 "delete_from_cursor", 2,
1111 G_TYPE_ENUM, GTK_DELETE_CHARS,
1114 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
1117 /* Make this do the same as Backspace, to help with mis-typing */
1118 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
1121 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
1122 "delete_from_cursor", 2,
1123 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1126 gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
1127 "delete_from_cursor", 2,
1128 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1131 gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
1132 "delete_from_cursor", 2,
1133 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
1136 /* Cut/copy/paste */
1138 gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
1139 "cut_clipboard", 0);
1140 gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
1141 "copy_clipboard", 0);
1142 gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
1143 "paste_clipboard", 0);
1145 gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
1146 "cut_clipboard", 0);
1147 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
1148 "copy_clipboard", 0);
1149 gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
1150 "paste_clipboard", 0);
1153 gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1154 "toggle_overwrite", 0);
1155 gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1156 "toggle_overwrite", 0);
1159 gtk_binding_entry_add_signal (binding_set, GDK_F7, 0,
1160 "toggle_cursor_visible", 0);
1162 /* Control-tab focus motion */
1163 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
1165 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1166 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
1168 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1170 gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1172 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1173 gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1175 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1177 g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1181 gtk_text_view_init (GtkTextView *text_view)
1183 GtkWidget *widget = GTK_WIDGET (text_view);
1184 GtkTargetList *target_list;
1186 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1188 /* Set up default style */
1189 text_view->wrap_mode = GTK_WRAP_NONE;
1190 text_view->pixels_above_lines = 0;
1191 text_view->pixels_below_lines = 0;
1192 text_view->pixels_inside_wrap = 0;
1193 text_view->justify = GTK_JUSTIFY_LEFT;
1194 text_view->left_margin = 0;
1195 text_view->right_margin = 0;
1196 text_view->indent = 0;
1197 text_view->tabs = NULL;
1198 text_view->editable = TRUE;
1200 gtk_drag_dest_set (widget, 0, NULL, 0,
1201 GDK_ACTION_COPY | GDK_ACTION_MOVE);
1203 target_list = gtk_target_list_new (NULL, 0);
1204 gtk_drag_dest_set_target_list (widget, target_list);
1205 gtk_target_list_unref (target_list);
1207 text_view->virtual_cursor_x = -1;
1208 text_view->virtual_cursor_y = -1;
1210 /* This object is completely private. No external entity can gain a reference
1211 * to it; so we create it here and destroy it in finalize ().
1213 text_view->im_context = gtk_im_multicontext_new ();
1215 g_signal_connect (text_view->im_context, "commit",
1216 G_CALLBACK (gtk_text_view_commit_handler), text_view);
1217 g_signal_connect (text_view->im_context, "preedit_changed",
1218 G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1219 g_signal_connect (text_view->im_context, "retrieve_surrounding",
1220 G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1221 g_signal_connect (text_view->im_context, "delete_surrounding",
1222 G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1224 text_view->cursor_visible = TRUE;
1226 text_view->accepts_tab = TRUE;
1228 text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1231 text_view->drag_start_x = -1;
1232 text_view->drag_start_y = -1;
1234 text_view->pending_place_cursor_button = 0;
1236 /* We handle all our own redrawing */
1237 gtk_widget_set_redraw_on_allocate (widget, FALSE);
1241 * gtk_text_view_new:
1243 * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1244 * before using the text view, an empty default buffer will be created
1245 * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1246 * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1248 * Return value: a new #GtkTextView
1251 gtk_text_view_new (void)
1253 return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1257 * gtk_text_view_new_with_buffer:
1258 * @buffer: a #GtkTextBuffer
1260 * Creates a new #GtkTextView widget displaying the buffer
1261 * @buffer. One buffer can be shared among many widgets.
1262 * @buffer may be %NULL to create a default buffer, in which case
1263 * this function is equivalent to gtk_text_view_new(). The
1264 * text view adds its own reference count to the buffer; it does not
1265 * take over an existing reference.
1267 * Return value: a new #GtkTextView.
1270 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1272 GtkTextView *text_view;
1274 text_view = (GtkTextView*)gtk_text_view_new ();
1276 gtk_text_view_set_buffer (text_view, buffer);
1278 return GTK_WIDGET (text_view);
1282 * gtk_text_view_set_buffer:
1283 * @text_view: a #GtkTextView
1284 * @buffer: a #GtkTextBuffer
1286 * Sets @buffer as the buffer being displayed by @text_view. The previous
1287 * buffer displayed by the text view is unreferenced, and a reference is
1288 * added to @buffer. If you owned a reference to @buffer before passing it
1289 * to this function, you must remove that reference yourself; #GtkTextView
1290 * will not "adopt" it.
1293 gtk_text_view_set_buffer (GtkTextView *text_view,
1294 GtkTextBuffer *buffer)
1296 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1297 g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1299 if (text_view->buffer == buffer)
1302 if (text_view->buffer != NULL)
1304 /* Destroy all anchored children */
1308 copy = g_slist_copy (text_view->children);
1310 while (tmp_list != NULL)
1312 GtkTextViewChild *vc = tmp_list->data;
1316 gtk_widget_destroy (vc->widget);
1317 /* vc may now be invalid! */
1320 tmp_list = g_slist_next (tmp_list);
1323 g_slist_free (copy);
1325 g_signal_handlers_disconnect_by_func (text_view->buffer,
1326 gtk_text_view_mark_set_handler,
1328 g_signal_handlers_disconnect_by_func (text_view->buffer,
1329 gtk_text_view_target_list_notify,
1331 g_object_unref (text_view->buffer);
1332 text_view->dnd_mark = NULL;
1333 text_view->first_para_mark = NULL;
1335 if (GTK_WIDGET_REALIZED (text_view))
1337 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1338 GDK_SELECTION_PRIMARY);
1339 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1343 text_view->buffer = buffer;
1349 g_object_ref (buffer);
1351 if (text_view->layout)
1352 gtk_text_layout_set_buffer (text_view->layout, buffer);
1354 gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1356 text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1360 text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1364 text_view->first_para_pixels = 0;
1366 g_signal_connect (text_view->buffer, "mark_set",
1367 G_CALLBACK (gtk_text_view_mark_set_handler),
1369 g_signal_connect (text_view->buffer, "notify::paste-target-list",
1370 G_CALLBACK (gtk_text_view_target_list_notify),
1373 gtk_text_view_target_list_notify (text_view->buffer, NULL, text_view);
1375 if (GTK_WIDGET_REALIZED (text_view))
1377 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1378 GDK_SELECTION_PRIMARY);
1379 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1383 g_object_notify (G_OBJECT (text_view), "buffer");
1385 if (GTK_WIDGET_VISIBLE (text_view))
1386 gtk_widget_queue_draw (GTK_WIDGET (text_view));
1388 DV(g_print ("Invalidating due to set_buffer\n"));
1389 gtk_text_view_invalidate (text_view);
1392 static GtkTextBuffer*
1393 get_buffer (GtkTextView *text_view)
1395 if (text_view->buffer == NULL)
1398 b = gtk_text_buffer_new (NULL);
1399 gtk_text_view_set_buffer (text_view, b);
1403 return text_view->buffer;
1407 * gtk_text_view_get_buffer:
1408 * @text_view: a #GtkTextView
1410 * Returns the #GtkTextBuffer being displayed by this text view.
1411 * The reference count on the buffer is not incremented; the caller
1412 * of this function won't own a new reference.
1414 * Return value: a #GtkTextBuffer
1417 gtk_text_view_get_buffer (GtkTextView *text_view)
1419 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1421 return get_buffer (text_view);
1425 * gtk_text_view_get_iter_at_location:
1426 * @text_view: a #GtkTextView
1427 * @iter: a #GtkTextIter
1428 * @x: x position, in buffer coordinates
1429 * @y: y position, in buffer coordinates
1431 * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1432 * coordinates are coordinates for the entire buffer, not just the
1433 * currently-displayed portion. If you have coordinates from an
1434 * event, you have to convert those to buffer coordinates with
1435 * gtk_text_view_window_to_buffer_coords().
1438 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1443 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1444 g_return_if_fail (iter != NULL);
1446 gtk_text_view_ensure_layout (text_view);
1448 gtk_text_layout_get_iter_at_pixel (text_view->layout,
1453 * gtk_text_view_get_iter_at_position:
1454 * @text_view: a #GtkTextView
1455 * @iter: a #GtkTextIter
1456 * @trailing: location to store an integer indicating where
1457 * in the grapheme the user clicked. It will either be
1458 * zero, or the number of characters in the grapheme.
1459 * 0 represents the trailing edge of the grapheme.
1460 * @x: x position, in buffer coordinates
1461 * @y: y position, in buffer coordinates
1463 * Retrieves the iterator pointing to the character at buffer
1464 * coordinates @x and @y. Buffer coordinates are coordinates for
1465 * the entire buffer, not just the currently-displayed portion.
1466 * If you have coordinates from an event, you have to convert
1467 * those to buffer coordinates with
1468 * gtk_text_view_window_to_buffer_coords().
1470 * Note that this is different from gtk_text_view_get_iter_at_location(),
1471 * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1477 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1483 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1484 g_return_if_fail (iter != NULL);
1486 gtk_text_view_ensure_layout (text_view);
1488 gtk_text_layout_get_iter_at_position (text_view->layout,
1489 iter, trailing, x, y);
1493 * gtk_text_view_get_iter_location:
1494 * @text_view: a #GtkTextView
1495 * @iter: a #GtkTextIter
1496 * @location: bounds of the character at @iter
1498 * Gets a rectangle which roughly contains the character at @iter.
1499 * The rectangle position is in buffer coordinates; use
1500 * gtk_text_view_buffer_to_window_coords() to convert these
1501 * coordinates to coordinates for one of the windows in the text view.
1504 gtk_text_view_get_iter_location (GtkTextView *text_view,
1505 const GtkTextIter *iter,
1506 GdkRectangle *location)
1508 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1509 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1511 gtk_text_view_ensure_layout (text_view);
1513 gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1517 * gtk_text_view_get_line_yrange:
1518 * @text_view: a #GtkTextView
1519 * @iter: a #GtkTextIter
1520 * @y: return location for a y coordinate
1521 * @height: return location for a height
1523 * Gets the y coordinate of the top of the line containing @iter,
1524 * and the height of the line. The coordinate is a buffer coordinate;
1525 * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1528 gtk_text_view_get_line_yrange (GtkTextView *text_view,
1529 const GtkTextIter *iter,
1533 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1534 g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1536 gtk_text_view_ensure_layout (text_view);
1538 gtk_text_layout_get_line_yrange (text_view->layout,
1545 * gtk_text_view_get_line_at_y:
1546 * @text_view: a #GtkTextView
1547 * @target_iter: a #GtkTextIter
1548 * @y: a y coordinate
1549 * @line_top: return location for top coordinate of the line
1551 * Gets the #GtkTextIter at the start of the line containing
1552 * the coordinate @y. @y is in buffer coordinates, convert from
1553 * window coordinates with gtk_text_view_window_to_buffer_coords().
1554 * If non-%NULL, @line_top will be filled with the coordinate of the top
1558 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1559 GtkTextIter *target_iter,
1563 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1565 gtk_text_view_ensure_layout (text_view);
1567 gtk_text_layout_get_line_at_y (text_view->layout,
1574 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1576 DV (g_print (" Setting adj to raw value %g\n", val));
1578 /* We don't really want to clamp to upper; we want to clamp to
1579 upper - page_size which is the highest value the scrollbar
1580 will let us reach. */
1581 if (val > (adj->upper - adj->page_size))
1582 val = adj->upper - adj->page_size;
1584 if (val < adj->lower)
1587 if (val != adj->value)
1589 DV (g_print (" Setting adj to clamped value %g\n", val));
1590 gtk_adjustment_set_value (adj, val);
1598 * gtk_text_view_scroll_to_iter:
1599 * @text_view: a #GtkTextView
1600 * @iter: a #GtkTextIter
1601 * @within_margin: margin as a [0.0,0.5) fraction of screen size
1602 * @use_align: whether to use alignment arguments (if %FALSE,
1603 * just get the mark onscreen)
1604 * @xalign: horizontal alignment of mark within visible area
1605 * @yalign: vertical alignment of mark within visible area
1607 * Scrolls @text_view so that @iter is on the screen in the position
1608 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1609 * left or top, 1.0 indicates right or bottom, 0.5 means center.
1610 * If @use_align is %FALSE, the text scrolls the minimal distance to
1611 * get the mark onscreen, possibly not scrolling at all. The effective
1612 * screen for purposes of this function is reduced by a margin of size
1615 * Note that this function uses the currently-computed height of the
1616 * lines in the text buffer. Line heights are computed in an idle
1617 * handler; so this function may not have the desired effect if it's
1618 * called before the height computations. To avoid oddness, consider
1619 * using gtk_text_view_scroll_to_mark() which saves a point to be
1620 * scrolled to after line validation.
1622 * Return value: %TRUE if scrolling occurred
1625 gtk_text_view_scroll_to_iter (GtkTextView *text_view,
1627 gdouble within_margin,
1633 GdkRectangle screen;
1638 gboolean retval = FALSE;
1640 gint screen_xoffset, screen_yoffset;
1641 gint current_x_scroll, current_y_scroll;
1643 /* FIXME why don't we do the validate-at-scroll-destination thing
1644 * from flush_scroll in this function? I think it wasn't done before
1645 * because changed_handler was screwed up, but I could be wrong.
1648 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1649 g_return_val_if_fail (iter != NULL, FALSE);
1650 g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1651 g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1652 g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1654 widget = GTK_WIDGET (text_view);
1656 DV(g_print(G_STRLOC"\n"));
1658 gtk_text_layout_get_iter_location (text_view->layout,
1662 DV (g_print (" target rect %d,%d %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1664 current_x_scroll = text_view->xoffset;
1665 current_y_scroll = text_view->yoffset;
1667 screen.x = current_x_scroll;
1668 screen.y = current_y_scroll;
1669 screen.width = SCREEN_WIDTH (widget);
1670 screen.height = SCREEN_HEIGHT (widget);
1672 screen_xoffset = screen.width * within_margin;
1673 screen_yoffset = screen.height * within_margin;
1675 screen.x += screen_xoffset;
1676 screen.y += screen_yoffset;
1677 screen.width -= screen_xoffset * 2;
1678 screen.height -= screen_yoffset * 2;
1680 /* paranoia check */
1681 if (screen.width < 1)
1683 if (screen.height < 1)
1686 /* The -1 here ensures that we leave enough space to draw the cursor
1687 * when this function is used for horizontal scrolling.
1689 screen_right = screen.x + screen.width - 1;
1690 screen_bottom = screen.y + screen.height;
1692 /* The alignment affects the point in the target character that we
1693 * choose to align. If we're doing right/bottom alignment, we align
1694 * the right/bottom edge of the character the mark is at; if we're
1695 * doing left/top we align the left/top edge of the character; if
1696 * we're doing center alignment we align the center of the
1700 /* Vertical scroll */
1703 scroll_dest = current_y_scroll;
1707 scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1709 /* if scroll_dest < screen.y, we move a negative increment (up),
1710 * else a positive increment (down)
1712 scroll_inc = scroll_dest - screen.y + screen_yoffset;
1716 /* move minimum to get onscreen */
1717 if (rect.y < screen.y)
1719 scroll_dest = rect.y;
1720 scroll_inc = scroll_dest - screen.y - screen_yoffset;
1722 else if ((rect.y + rect.height) > screen_bottom)
1724 scroll_dest = rect.y + rect.height;
1725 scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1729 if (scroll_inc != 0)
1731 retval = set_adjustment_clamped (get_vadjustment (text_view),
1732 current_y_scroll + scroll_inc);
1734 DV (g_print (" vert increment %d\n", scroll_inc));
1737 /* Horizontal scroll */
1740 scroll_dest = current_x_scroll;
1744 scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1746 /* if scroll_dest < screen.y, we move a negative increment (left),
1747 * else a positive increment (right)
1749 scroll_inc = scroll_dest - screen.x + screen_xoffset;
1753 /* move minimum to get onscreen */
1754 if (rect.x < screen.x)
1756 scroll_dest = rect.x;
1757 scroll_inc = scroll_dest - screen.x - screen_xoffset;
1759 else if ((rect.x + rect.width) > screen_right)
1761 scroll_dest = rect.x + rect.width;
1762 scroll_inc = scroll_dest - screen_right + screen_xoffset;
1766 if (scroll_inc != 0)
1768 retval = set_adjustment_clamped (get_hadjustment (text_view),
1769 current_x_scroll + scroll_inc);
1771 DV (g_print (" horiz increment %d\n", scroll_inc));
1775 DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1777 DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1783 free_pending_scroll (GtkTextPendingScroll *scroll)
1785 if (!gtk_text_mark_get_deleted (scroll->mark))
1786 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1788 g_object_unref (scroll->mark);
1793 cancel_pending_scroll (GtkTextView *text_view)
1795 if (text_view->pending_scroll)
1797 free_pending_scroll (text_view->pending_scroll);
1798 text_view->pending_scroll = NULL;
1803 gtk_text_view_queue_scroll (GtkTextView *text_view,
1805 gdouble within_margin,
1811 GtkTextPendingScroll *scroll;
1813 DV(g_print(G_STRLOC"\n"));
1815 scroll = g_new (GtkTextPendingScroll, 1);
1817 scroll->within_margin = within_margin;
1818 scroll->use_align = use_align;
1819 scroll->xalign = xalign;
1820 scroll->yalign = yalign;
1822 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1824 scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1827 gtk_text_mark_get_left_gravity (mark));
1829 g_object_ref (scroll->mark);
1831 cancel_pending_scroll (text_view);
1833 text_view->pending_scroll = scroll;
1837 gtk_text_view_flush_scroll (GtkTextView *text_view)
1840 GtkTextPendingScroll *scroll;
1844 widget = GTK_WIDGET (text_view);
1846 DV(g_print(G_STRLOC"\n"));
1848 if (text_view->pending_scroll == NULL)
1850 DV (g_print ("in flush scroll, no pending scroll\n"));
1854 scroll = text_view->pending_scroll;
1856 /* avoid recursion */
1857 text_view->pending_scroll = NULL;
1859 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1861 /* Validate area around the scroll destination, so the adjustment
1862 * can meaningfully point into that area. We must validate
1863 * enough area to be sure that after we scroll, everything onscreen
1864 * is valid; otherwise, validation will maintain the first para
1865 * in one place, but may push the target iter off the bottom of
1868 DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1869 gtk_text_layout_validate_yrange (text_view->layout, &iter,
1870 - (widget->allocation.height * 2),
1871 widget->allocation.height * 2);
1873 DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1875 /* Ensure we have updated width/height */
1876 gtk_text_view_update_adjustments (text_view);
1878 retval = gtk_text_view_scroll_to_iter (text_view,
1880 scroll->within_margin,
1885 free_pending_scroll (scroll);
1891 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1893 if (upper != adj->upper)
1895 gdouble min = MAX (0.0, upper - adj->page_size);
1896 gboolean value_changed = FALSE;
1900 if (adj->value > min)
1903 value_changed = TRUE;
1906 gtk_adjustment_changed (adj);
1907 DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1911 DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1912 gtk_adjustment_value_changed (adj);
1918 gtk_text_view_update_adjustments (GtkTextView *text_view)
1920 gint width = 0, height = 0;
1922 DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1924 if (text_view->layout)
1925 gtk_text_layout_get_size (text_view->layout, &width, &height);
1927 /* Make room for the cursor after the last character in the widest line */
1928 width += SPACE_FOR_CURSOR;
1930 if (text_view->width != width || text_view->height != height)
1932 if (text_view->width != width)
1933 text_view->width_changed = TRUE;
1935 text_view->width = width;
1936 text_view->height = height;
1938 gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1939 MAX (SCREEN_WIDTH (text_view), width));
1940 gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1941 MAX (SCREEN_HEIGHT (text_view), height));
1943 /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1945 /* Set up the step sizes; we'll say that a page is
1946 our allocation minus one step, and a step is
1947 1/10 of our allocation. */
1948 text_view->hadjustment->step_increment =
1949 SCREEN_WIDTH (text_view) / 10.0;
1950 text_view->hadjustment->page_increment =
1951 SCREEN_WIDTH (text_view) * 0.9;
1953 text_view->vadjustment->step_increment =
1954 SCREEN_HEIGHT (text_view) / 10.0;
1955 text_view->vadjustment->page_increment =
1956 SCREEN_HEIGHT (text_view) * 0.9;
1958 gtk_adjustment_changed (get_hadjustment (text_view));
1959 gtk_adjustment_changed (get_vadjustment (text_view));
1964 gtk_text_view_update_layout_width (GtkTextView *text_view)
1966 DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1968 gtk_text_view_ensure_layout (text_view);
1970 gtk_text_layout_set_screen_width (text_view->layout,
1971 MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
1975 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1979 if (text_view->layout == NULL)
1982 gtk_text_view_get_cursor_location (text_view, &area);
1984 area.x -= text_view->xoffset;
1985 area.y -= text_view->yoffset;
1987 /* Width returned by Pango indicates direction of cursor,
1988 * by it's sign more than the size of cursor.
1992 gtk_im_context_set_cursor_location (text_view->im_context, &area);
1996 do_update_im_spot_location (gpointer text_view)
1998 GtkTextViewPrivate *priv;
2000 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2001 priv->im_spot_idle = 0;
2003 gtk_text_view_update_im_spot_location (text_view);
2008 queue_update_im_spot_location (GtkTextView *text_view)
2010 GtkTextViewPrivate *priv;
2012 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2014 /* Use priority a little higher than GTK_TEXT_VIEW_PRIORITY_VALIDATE,
2015 * so we don't wait until the entire buffer has been validated. */
2016 if (!priv->im_spot_idle)
2017 priv->im_spot_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE - 1,
2018 do_update_im_spot_location,
2024 flush_update_im_spot_location (GtkTextView *text_view)
2026 GtkTextViewPrivate *priv;
2028 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2030 if (priv->im_spot_idle)
2032 g_source_remove (priv->im_spot_idle);
2033 priv->im_spot_idle = 0;
2034 gtk_text_view_update_im_spot_location (text_view);
2039 * gtk_text_view_scroll_to_mark:
2040 * @text_view: a #GtkTextView
2041 * @mark: a #GtkTextMark
2042 * @within_margin: margin as a [0.0,0.5) fraction of screen size
2043 * @use_align: whether to use alignment arguments (if %FALSE, just
2044 * get the mark onscreen)
2045 * @xalign: horizontal alignment of mark within visible area
2046 * @yalign: vertical alignment of mark within visible area
2048 * Scrolls @text_view so that @mark is on the screen in the position
2049 * indicated by @xalign and @yalign. An alignment of 0.0 indicates
2050 * left or top, 1.0 indicates right or bottom, 0.5 means center.
2051 * If @use_align is %FALSE, the text scrolls the minimal distance to
2052 * get the mark onscreen, possibly not scrolling at all. The effective
2053 * screen for purposes of this function is reduced by a margin of size
2057 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
2059 gdouble within_margin,
2064 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2065 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2066 g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
2067 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
2068 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
2070 gtk_text_view_queue_scroll (text_view, mark,
2076 /* If no validation is pending, we need to go ahead and force an
2079 if (text_view->layout &&
2080 gtk_text_layout_is_valid (text_view->layout))
2081 gtk_text_view_flush_scroll (text_view);
2085 * gtk_text_view_scroll_mark_onscreen:
2086 * @text_view: a #GtkTextView
2087 * @mark: a mark in the buffer for @text_view
2089 * Scrolls @text_view the minimum distance such that @mark is contained
2090 * within the visible area of the widget.
2093 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
2096 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2097 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2099 gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
2103 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
2105 GdkRectangle visible_rect;
2106 gtk_text_view_get_visible_rect (text_view, &visible_rect);
2108 return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
2110 visible_rect.y + visible_rect.height);
2114 * gtk_text_view_move_mark_onscreen:
2115 * @text_view: a #GtkTextView
2116 * @mark: a #GtkTextMark
2118 * Moves a mark within the buffer so that it's
2119 * located within the currently-visible text area.
2121 * Return value: %TRUE if the mark moved (wasn't already onscreen)
2124 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
2129 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2130 g_return_val_if_fail (mark != NULL, FALSE);
2132 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
2134 if (clamp_iter_onscreen (text_view, &iter))
2136 gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
2144 * gtk_text_view_get_visible_rect:
2145 * @text_view: a #GtkTextView
2146 * @visible_rect: rectangle to fill
2148 * Fills @visible_rect with the currently-visible
2149 * region of the buffer, in buffer coordinates. Convert to window coordinates
2150 * with gtk_text_view_buffer_to_window_coords().
2153 gtk_text_view_get_visible_rect (GtkTextView *text_view,
2154 GdkRectangle *visible_rect)
2158 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2160 widget = GTK_WIDGET (text_view);
2164 visible_rect->x = text_view->xoffset;
2165 visible_rect->y = text_view->yoffset;
2166 visible_rect->width = SCREEN_WIDTH (widget);
2167 visible_rect->height = SCREEN_HEIGHT (widget);
2169 DV(g_print(" visible rect: %d,%d %d x %d\n",
2172 visible_rect->width,
2173 visible_rect->height));
2178 * gtk_text_view_set_wrap_mode:
2179 * @text_view: a #GtkTextView
2180 * @wrap_mode: a #GtkWrapMode
2182 * Sets the line wrapping for the view.
2185 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
2186 GtkWrapMode wrap_mode)
2188 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2190 if (text_view->wrap_mode != wrap_mode)
2192 text_view->wrap_mode = wrap_mode;
2194 if (text_view->layout)
2196 text_view->layout->default_style->wrap_mode = wrap_mode;
2197 gtk_text_layout_default_style_changed (text_view->layout);
2201 g_object_notify (G_OBJECT (text_view), "wrap-mode");
2205 * gtk_text_view_get_wrap_mode:
2206 * @text_view: a #GtkTextView
2208 * Gets the line wrapping for the view.
2210 * Return value: the line wrap setting
2213 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2215 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2217 return text_view->wrap_mode;
2221 * gtk_text_view_set_editable:
2222 * @text_view: a #GtkTextView
2223 * @setting: whether it's editable
2225 * Sets the default editability of the #GtkTextView. You can override
2226 * this default setting with tags in the buffer, using the "editable"
2227 * attribute of tags.
2230 gtk_text_view_set_editable (GtkTextView *text_view,
2233 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2234 setting = setting != FALSE;
2236 if (text_view->editable != setting)
2238 text_view->editable = setting;
2240 if (text_view->layout)
2242 gtk_text_layout_set_overwrite_mode (text_view->layout,
2243 text_view->overwrite_mode && text_view->editable);
2244 text_view->layout->default_style->editable = text_view->editable;
2245 gtk_text_layout_default_style_changed (text_view->layout);
2248 g_object_notify (G_OBJECT (text_view), "editable");
2253 * gtk_text_view_get_editable:
2254 * @text_view: a #GtkTextView
2256 * Returns the default editability of the #GtkTextView. Tags in the
2257 * buffer may override this setting for some ranges of text.
2259 * Return value: whether text is editable by default
2262 gtk_text_view_get_editable (GtkTextView *text_view)
2264 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2266 return text_view->editable;
2270 * gtk_text_view_set_pixels_above_lines:
2271 * @text_view: a #GtkTextView
2272 * @pixels_above_lines: pixels above paragraphs
2274 * Sets the default number of blank pixels above paragraphs in @text_view.
2275 * Tags in the buffer for @text_view may override the defaults.
2278 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2279 gint pixels_above_lines)
2281 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2283 if (text_view->pixels_above_lines != pixels_above_lines)
2285 text_view->pixels_above_lines = pixels_above_lines;
2287 if (text_view->layout)
2289 text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
2290 gtk_text_layout_default_style_changed (text_view->layout);
2293 g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2298 * gtk_text_view_get_pixels_above_lines:
2299 * @text_view: a #GtkTextView
2301 * Gets the default number of pixels to put above paragraphs.
2303 * Return value: default number of pixels above paragraphs
2306 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2308 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2310 return text_view->pixels_above_lines;
2314 * gtk_text_view_set_pixels_below_lines:
2315 * @text_view: a #GtkTextView
2316 * @pixels_below_lines: pixels below paragraphs
2318 * Sets the default number of pixels of blank space
2319 * to put below paragraphs in @text_view. May be overridden
2320 * by tags applied to @text_view's buffer.
2323 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2324 gint pixels_below_lines)
2326 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2328 if (text_view->pixels_below_lines != pixels_below_lines)
2330 text_view->pixels_below_lines = pixels_below_lines;
2332 if (text_view->layout)
2334 text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2335 gtk_text_layout_default_style_changed (text_view->layout);
2338 g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2343 * gtk_text_view_get_pixels_below_lines:
2344 * @text_view: a #GtkTextView
2346 * Gets the value set by gtk_text_view_set_pixels_below_lines().
2348 * Return value: default number of blank pixels below paragraphs
2351 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2353 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2355 return text_view->pixels_below_lines;
2359 * gtk_text_view_set_pixels_inside_wrap:
2360 * @text_view: a #GtkTextView
2361 * @pixels_inside_wrap: default number of pixels between wrapped lines
2363 * Sets the default number of pixels of blank space to leave between
2364 * display/wrapped lines within a paragraph. May be overridden by
2365 * tags in @text_view's buffer.
2368 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2369 gint pixels_inside_wrap)
2371 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2373 if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2375 text_view->pixels_inside_wrap = pixels_inside_wrap;
2377 if (text_view->layout)
2379 text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2380 gtk_text_layout_default_style_changed (text_view->layout);
2383 g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2388 * gtk_text_view_get_pixels_inside_wrap:
2389 * @text_view: a #GtkTextView
2391 * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2393 * Return value: default number of pixels of blank space between wrapped lines
2396 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2398 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2400 return text_view->pixels_inside_wrap;
2404 * gtk_text_view_set_justification:
2405 * @text_view: a #GtkTextView
2406 * @justification: justification
2408 * Sets the default justification of text in @text_view.
2409 * Tags in the view's buffer may override the default.
2413 gtk_text_view_set_justification (GtkTextView *text_view,
2414 GtkJustification justification)
2416 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2418 if (text_view->justify != justification)
2420 text_view->justify = justification;
2422 if (text_view->layout)
2424 text_view->layout->default_style->justification = justification;
2425 gtk_text_layout_default_style_changed (text_view->layout);
2428 g_object_notify (G_OBJECT (text_view), "justification");
2433 * gtk_text_view_get_justification:
2434 * @text_view: a #GtkTextView
2436 * Gets the default justification of paragraphs in @text_view.
2437 * Tags in the buffer may override the default.
2439 * Return value: default justification
2442 gtk_text_view_get_justification (GtkTextView *text_view)
2444 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2446 return text_view->justify;
2450 * gtk_text_view_set_left_margin:
2451 * @text_view: a #GtkTextView
2452 * @left_margin: left margin in pixels
2454 * Sets the default left margin for text in @text_view.
2455 * Tags in the buffer may override the default.
2458 gtk_text_view_set_left_margin (GtkTextView *text_view,
2461 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2463 if (text_view->left_margin != left_margin)
2465 text_view->left_margin = left_margin;
2467 if (text_view->layout)
2469 text_view->layout->default_style->left_margin = left_margin;
2470 gtk_text_layout_default_style_changed (text_view->layout);
2473 g_object_notify (G_OBJECT (text_view), "left-margin");
2478 * gtk_text_view_get_left_margin:
2479 * @text_view: a #GtkTextView
2481 * Gets the default left margin size of paragraphs in the @text_view.
2482 * Tags in the buffer may override the default.
2484 * Return value: left margin in pixels
2487 gtk_text_view_get_left_margin (GtkTextView *text_view)
2489 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2491 return text_view->left_margin;
2495 * gtk_text_view_set_right_margin:
2496 * @text_view: a #GtkTextView
2497 * @right_margin: right margin in pixels
2499 * Sets the default right margin for text in the text view.
2500 * Tags in the buffer may override the default.
2503 gtk_text_view_set_right_margin (GtkTextView *text_view,
2506 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2508 if (text_view->right_margin != right_margin)
2510 text_view->right_margin = right_margin;
2512 if (text_view->layout)
2514 text_view->layout->default_style->right_margin = right_margin;
2515 gtk_text_layout_default_style_changed (text_view->layout);
2518 g_object_notify (G_OBJECT (text_view), "right-margin");
2523 * gtk_text_view_get_right_margin:
2524 * @text_view: a #GtkTextView
2526 * Gets the default right margin for text in @text_view. Tags
2527 * in the buffer may override the default.
2529 * Return value: right margin in pixels
2532 gtk_text_view_get_right_margin (GtkTextView *text_view)
2534 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2536 return text_view->right_margin;
2540 * gtk_text_view_set_indent:
2541 * @text_view: a #GtkTextView
2542 * @indent: indentation in pixels
2544 * Sets the default indentation for paragraphs in @text_view.
2545 * Tags in the buffer may override the default.
2548 gtk_text_view_set_indent (GtkTextView *text_view,
2551 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2553 if (text_view->indent != indent)
2555 text_view->indent = indent;
2557 if (text_view->layout)
2559 text_view->layout->default_style->indent = indent;
2560 gtk_text_layout_default_style_changed (text_view->layout);
2563 g_object_notify (G_OBJECT (text_view), "indent");
2568 * gtk_text_view_get_indent:
2569 * @text_view: a #GtkTextView
2571 * Gets the default indentation of paragraphs in @text_view.
2572 * Tags in the view's buffer may override the default.
2573 * The indentation may be negative.
2575 * Return value: number of pixels of indentation
2578 gtk_text_view_get_indent (GtkTextView *text_view)
2580 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2582 return text_view->indent;
2586 * gtk_text_view_set_tabs:
2587 * @text_view: a #GtkTextView
2588 * @tabs: tabs as a #PangoTabArray
2590 * Sets the default tab stops for paragraphs in @text_view.
2591 * Tags in the buffer may override the default.
2594 gtk_text_view_set_tabs (GtkTextView *text_view,
2595 PangoTabArray *tabs)
2597 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2599 if (text_view->tabs)
2600 pango_tab_array_free (text_view->tabs);
2602 text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2604 if (text_view->layout)
2606 /* some unkosher futzing in internal struct details... */
2607 if (text_view->layout->default_style->tabs)
2608 pango_tab_array_free (text_view->layout->default_style->tabs);
2610 text_view->layout->default_style->tabs =
2611 text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2613 gtk_text_layout_default_style_changed (text_view->layout);
2616 g_object_notify (G_OBJECT (text_view), "tabs");
2620 * gtk_text_view_get_tabs:
2621 * @text_view: a #GtkTextView
2623 * Gets the default tabs for @text_view. Tags in the buffer may
2624 * override the defaults. The returned array will be %NULL if
2625 * "standard" (8-space) tabs are used. Free the return value
2626 * with pango_tab_array_free().
2628 * Return value: copy of default tab array, or %NULL if "standard"
2629 * tabs are used; must be freed with pango_tab_array_free().
2632 gtk_text_view_get_tabs (GtkTextView *text_view)
2634 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2636 return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2640 gtk_text_view_toggle_cursor_visible (GtkTextView *text_view)
2642 gtk_text_view_set_cursor_visible (text_view, !text_view->cursor_visible);
2646 * gtk_text_view_set_cursor_visible:
2647 * @text_view: a #GtkTextView
2648 * @setting: whether to show the insertion cursor
2650 * Toggles whether the insertion point is displayed. A buffer with no editable
2651 * text probably shouldn't have a visible cursor, so you may want to turn
2655 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2658 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2660 setting = (setting != FALSE);
2662 if (text_view->cursor_visible != setting)
2664 text_view->cursor_visible = setting;
2666 if (GTK_WIDGET_HAS_FOCUS (text_view))
2668 if (text_view->layout)
2670 gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2671 gtk_text_view_check_cursor_blink (text_view);
2675 g_object_notify (G_OBJECT (text_view), "cursor-visible");
2680 * gtk_text_view_get_cursor_visible:
2681 * @text_view: a #GtkTextView
2683 * Find out whether the cursor is being displayed.
2685 * Return value: whether the insertion mark is visible
2688 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2690 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2692 return text_view->cursor_visible;
2697 * gtk_text_view_place_cursor_onscreen:
2698 * @text_view: a #GtkTextView
2700 * Moves the cursor to the currently visible region of the
2701 * buffer, it it isn't there already.
2703 * Return value: %TRUE if the cursor had to be moved.
2706 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2710 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2712 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2713 gtk_text_buffer_get_insert (get_buffer (text_view)));
2715 if (clamp_iter_onscreen (text_view, &insert))
2717 gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2725 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2727 if (text_view->first_validate_idle != 0)
2729 DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2730 g_source_remove (text_view->first_validate_idle);
2731 text_view->first_validate_idle = 0;
2734 if (text_view->incremental_validate_idle != 0)
2736 g_source_remove (text_view->incremental_validate_idle);
2737 text_view->incremental_validate_idle = 0;
2742 gtk_text_view_destroy (GtkObject *object)
2744 GtkTextView *text_view;
2745 GtkTextViewPrivate *priv;
2747 text_view = GTK_TEXT_VIEW (object);
2748 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
2750 gtk_text_view_remove_validate_idles (text_view);
2751 gtk_text_view_set_buffer (text_view, NULL);
2752 gtk_text_view_destroy_layout (text_view);
2754 if (text_view->scroll_timeout)
2756 g_source_remove (text_view->scroll_timeout);
2757 text_view->scroll_timeout = 0;
2760 if (priv->im_spot_idle)
2762 g_source_remove (priv->im_spot_idle);
2763 priv->im_spot_idle = 0;
2766 (* GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy) (object);
2770 gtk_text_view_finalize (GObject *object)
2772 GtkTextView *text_view;
2774 text_view = GTK_TEXT_VIEW (object);
2776 g_assert (text_view->buffer == NULL);
2778 gtk_text_view_destroy_layout (text_view);
2779 gtk_text_view_set_buffer (text_view, NULL);
2781 cancel_pending_scroll (text_view);
2783 if (text_view->tabs)
2784 pango_tab_array_free (text_view->tabs);
2786 if (text_view->hadjustment)
2787 g_object_unref (text_view->hadjustment);
2788 if (text_view->vadjustment)
2789 g_object_unref (text_view->vadjustment);
2791 text_window_free (text_view->text_window);
2793 if (text_view->left_window)
2794 text_window_free (text_view->left_window);
2796 if (text_view->top_window)
2797 text_window_free (text_view->top_window);
2799 if (text_view->right_window)
2800 text_window_free (text_view->right_window);
2802 if (text_view->bottom_window)
2803 text_window_free (text_view->bottom_window);
2805 g_object_unref (text_view->im_context);
2807 (* G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize) (object);
2811 gtk_text_view_set_property (GObject *object,
2813 const GValue *value,
2816 GtkTextView *text_view;
2818 text_view = GTK_TEXT_VIEW (object);
2822 case PROP_PIXELS_ABOVE_LINES:
2823 gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2826 case PROP_PIXELS_BELOW_LINES:
2827 gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2830 case PROP_PIXELS_INSIDE_WRAP:
2831 gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2835 gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2838 case PROP_WRAP_MODE:
2839 gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2842 case PROP_JUSTIFICATION:
2843 gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2846 case PROP_LEFT_MARGIN:
2847 gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2850 case PROP_RIGHT_MARGIN:
2851 gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2855 gtk_text_view_set_indent (text_view, g_value_get_int (value));
2859 gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2862 case PROP_CURSOR_VISIBLE:
2863 gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2866 case PROP_OVERWRITE:
2867 gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
2871 gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
2874 case PROP_ACCEPTS_TAB:
2875 gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
2879 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2885 gtk_text_view_get_property (GObject *object,
2890 GtkTextView *text_view;
2892 text_view = GTK_TEXT_VIEW (object);
2896 case PROP_PIXELS_ABOVE_LINES:
2897 g_value_set_int (value, text_view->pixels_above_lines);
2900 case PROP_PIXELS_BELOW_LINES:
2901 g_value_set_int (value, text_view->pixels_below_lines);
2904 case PROP_PIXELS_INSIDE_WRAP:
2905 g_value_set_int (value, text_view->pixels_inside_wrap);
2909 g_value_set_boolean (value, text_view->editable);
2912 case PROP_WRAP_MODE:
2913 g_value_set_enum (value, text_view->wrap_mode);
2916 case PROP_JUSTIFICATION:
2917 g_value_set_enum (value, text_view->justify);
2920 case PROP_LEFT_MARGIN:
2921 g_value_set_int (value, text_view->left_margin);
2924 case PROP_RIGHT_MARGIN:
2925 g_value_set_int (value, text_view->right_margin);
2929 g_value_set_int (value, text_view->indent);
2933 g_value_set_boxed (value, text_view->tabs);
2936 case PROP_CURSOR_VISIBLE:
2937 g_value_set_boolean (value, text_view->cursor_visible);
2941 g_value_set_object (value, get_buffer (text_view));
2944 case PROP_OVERWRITE:
2945 g_value_set_boolean (value, text_view->overwrite_mode);
2948 case PROP_ACCEPTS_TAB:
2949 g_value_set_boolean (value, text_view->accepts_tab);
2953 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2959 gtk_text_view_size_request (GtkWidget *widget,
2960 GtkRequisition *requisition)
2962 GtkTextView *text_view;
2964 gint focus_edge_width;
2966 gboolean interior_focus;
2968 text_view = GTK_TEXT_VIEW (widget);
2970 gtk_widget_style_get (widget,
2971 "interior-focus", &interior_focus,
2972 "focus-line-width", &focus_width,
2976 focus_edge_width = 0;
2978 focus_edge_width = focus_width;
2980 if (text_view->layout)
2982 text_view->text_window->requisition.width = text_view->layout->width;
2983 text_view->text_window->requisition.height = text_view->layout->height;
2987 text_view->text_window->requisition.width = 0;
2988 text_view->text_window->requisition.height = 0;
2991 requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2992 requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2994 if (text_view->left_window)
2995 requisition->width += text_view->left_window->requisition.width;
2997 if (text_view->right_window)
2998 requisition->width += text_view->right_window->requisition.width;
3000 if (text_view->top_window)
3001 requisition->height += text_view->top_window->requisition.height;
3003 if (text_view->bottom_window)
3004 requisition->height += text_view->bottom_window->requisition.height;
3006 requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
3007 requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
3009 tmp_list = text_view->children;
3010 while (tmp_list != NULL)
3012 GtkTextViewChild *child = tmp_list->data;
3016 GtkRequisition child_req;
3017 GtkRequisition old_req;
3019 gtk_widget_get_child_requisition (child->widget, &old_req);
3021 gtk_widget_size_request (child->widget, &child_req);
3023 gtk_widget_get_child_requisition (child->widget, &child_req);
3025 /* Invalidate layout lines if required */
3026 if (text_view->layout &&
3027 (old_req.width != child_req.width ||
3028 old_req.height != child_req.height))
3029 gtk_text_child_anchor_queue_resize (child->anchor,
3034 GtkRequisition child_req;
3036 gtk_widget_size_request (child->widget, &child_req);
3039 tmp_list = g_slist_next (tmp_list);
3044 gtk_text_view_compute_child_allocation (GtkTextView *text_view,
3045 GtkTextViewChild *vc,
3046 GtkAllocation *allocation)
3052 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3056 gtk_text_layout_get_line_yrange (text_view->layout, &iter,
3059 buffer_y += vc->from_top_of_line;
3061 allocation->x = vc->from_left_of_buffer - text_view->xoffset;
3062 allocation->y = buffer_y - text_view->yoffset;
3064 gtk_widget_get_child_requisition (vc->widget, &req);
3065 allocation->width = req.width;
3066 allocation->height = req.height;
3070 gtk_text_view_update_child_allocation (GtkTextView *text_view,
3071 GtkTextViewChild *vc)
3073 GtkAllocation allocation;
3075 gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
3077 gtk_widget_size_allocate (vc->widget, &allocation);
3080 g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
3082 vc->widget->allocation.x,
3083 vc->widget->allocation.y,
3084 text_view->yoffset);
3089 gtk_text_view_child_allocated (GtkTextLayout *layout,
3095 GtkTextViewChild *vc = NULL;
3096 GtkTextView *text_view = data;
3098 /* x,y is the position of the child from the top of the line, and
3099 * from the left of the buffer. We have to translate that into text
3100 * window coordinates, then size_allocate the child.
3103 vc = g_object_get_data (G_OBJECT (child),
3104 "gtk-text-view-child");
3106 g_assert (vc != NULL);
3108 DV (g_print ("child allocated at %d,%d\n", x, y));
3110 vc->from_left_of_buffer = x;
3111 vc->from_top_of_line = y;
3113 gtk_text_view_update_child_allocation (text_view, vc);
3117 gtk_text_view_allocate_children (GtkTextView *text_view)
3121 DV(g_print(G_STRLOC"\n"));
3123 tmp_list = text_view->children;
3124 while (tmp_list != NULL)
3126 GtkTextViewChild *child = tmp_list->data;
3128 g_assert (child != NULL);
3132 /* We need to force-validate the regions containing
3135 GtkTextIter child_loc;
3136 gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
3140 /* Since anchored children are only ever allocated from
3141 * gtk_text_layout_get_line_display() we have to make sure
3142 * that the display line caching in the layout doesn't
3143 * get in the way. Invalidating the layout around the anchor
3146 if (GTK_WIDGET_ALLOC_NEEDED (child->widget))
3148 GtkTextIter end = child_loc;
3149 gtk_text_iter_forward_char (&end);
3150 gtk_text_layout_invalidate (text_view->layout, &child_loc, &end);
3153 gtk_text_layout_validate_yrange (text_view->layout,
3159 GtkAllocation allocation;
3160 GtkRequisition child_req;
3162 allocation.x = child->x;
3163 allocation.y = child->y;
3165 gtk_widget_get_child_requisition (child->widget, &child_req);
3167 allocation.width = child_req.width;
3168 allocation.height = child_req.height;
3170 gtk_widget_size_allocate (child->widget, &allocation);
3173 tmp_list = g_slist_next (tmp_list);
3178 gtk_text_view_size_allocate (GtkWidget *widget,
3179 GtkAllocation *allocation)
3181 GtkTextView *text_view;
3182 GtkTextIter first_para;
3185 GdkRectangle text_rect;
3186 GdkRectangle left_rect;
3187 GdkRectangle right_rect;
3188 GdkRectangle top_rect;
3189 GdkRectangle bottom_rect;
3190 gint focus_edge_width;
3192 gboolean interior_focus;
3193 gboolean size_changed;
3195 text_view = GTK_TEXT_VIEW (widget);
3197 DV(g_print(G_STRLOC"\n"));
3200 widget->allocation.width != allocation->width ||
3201 widget->allocation.height != allocation->height;
3203 widget->allocation = *allocation;
3205 if (GTK_WIDGET_REALIZED (widget))
3207 gdk_window_move_resize (widget->window,
3208 allocation->x, allocation->y,
3209 allocation->width, allocation->height);
3212 /* distribute width/height among child windows. Ensure all
3213 * windows get at least a 1x1 allocation.
3216 gtk_widget_style_get (widget,
3217 "interior-focus", &interior_focus,
3218 "focus-line-width", &focus_width,
3222 focus_edge_width = 0;
3224 focus_edge_width = focus_width;
3226 width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3228 if (text_view->left_window)
3229 left_rect.width = text_view->left_window->requisition.width;
3231 left_rect.width = 0;
3233 width -= left_rect.width;
3235 if (text_view->right_window)
3236 right_rect.width = text_view->right_window->requisition.width;
3238 right_rect.width = 0;
3240 width -= right_rect.width;
3242 text_rect.width = MAX (1, width);
3244 top_rect.width = text_rect.width;
3245 bottom_rect.width = text_rect.width;
3248 height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3250 if (text_view->top_window)
3251 top_rect.height = text_view->top_window->requisition.height;
3253 top_rect.height = 0;
3255 height -= top_rect.height;
3257 if (text_view->bottom_window)
3258 bottom_rect.height = text_view->bottom_window->requisition.height;
3260 bottom_rect.height = 0;
3262 height -= bottom_rect.height;
3264 text_rect.height = MAX (1, height);
3266 left_rect.height = text_rect.height;
3267 right_rect.height = text_rect.height;
3270 left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3271 top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3273 text_rect.x = left_rect.x + left_rect.width;
3274 text_rect.y = top_rect.y + top_rect.height;
3276 left_rect.y = text_rect.y;
3277 right_rect.y = text_rect.y;
3279 top_rect.x = text_rect.x;
3280 bottom_rect.x = text_rect.x;
3282 right_rect.x = text_rect.x + text_rect.width;
3283 bottom_rect.y = text_rect.y + text_rect.height;
3285 text_window_size_allocate (text_view->text_window,
3288 if (text_view->left_window)
3289 text_window_size_allocate (text_view->left_window,
3292 if (text_view->right_window)
3293 text_window_size_allocate (text_view->right_window,
3296 if (text_view->top_window)
3297 text_window_size_allocate (text_view->top_window,
3300 if (text_view->bottom_window)
3301 text_window_size_allocate (text_view->bottom_window,
3304 gtk_text_view_update_layout_width (text_view);
3306 /* Note that this will do some layout validation */
3307 gtk_text_view_allocate_children (text_view);
3309 /* Ensure h/v adj exist */
3310 get_hadjustment (text_view);
3311 get_vadjustment (text_view);
3313 text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
3314 text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
3315 text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
3316 text_view->hadjustment->lower = 0;
3317 text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
3320 if (text_view->hadjustment->value > text_view->hadjustment->upper - text_view->hadjustment->page_size)
3321 gtk_adjustment_set_value (text_view->hadjustment, MAX (0, text_view->hadjustment->upper - text_view->hadjustment->page_size));
3323 gtk_adjustment_changed (text_view->hadjustment);
3325 text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
3326 text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
3327 text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
3328 text_view->vadjustment->lower = 0;
3329 text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
3332 /* Now adjust the value of the adjustment to keep the cursor at the
3333 * same place in the buffer
3335 gtk_text_view_get_first_para_iter (text_view, &first_para);
3336 gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
3338 y += text_view->first_para_pixels;
3340 if (y > text_view->vadjustment->upper - text_view->vadjustment->page_size)
3341 y = MAX (0, text_view->vadjustment->upper - text_view->vadjustment->page_size);
3343 if (y != text_view->yoffset)
3344 gtk_adjustment_set_value (text_view->vadjustment, y);
3346 gtk_adjustment_changed (text_view->vadjustment);
3348 /* The GTK resize loop processes all the pending exposes right
3349 * after doing the resize stuff, so the idle sizer won't have a
3350 * chance to run. So we do the work here.
3352 gtk_text_view_flush_first_validate (text_view);
3354 /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3357 if (size_changed && GTK_WIDGET_REALIZED (widget))
3358 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3362 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3365 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3366 text_view->first_para_mark);
3370 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3372 GtkWidget *widget = GTK_WIDGET (text_view);
3374 DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3376 if (SCREEN_HEIGHT (widget) > 0)
3378 GtkTextIter first_para;
3380 /* Be sure we've validated the stuff onscreen; if we
3381 * scrolled, these calls won't have any effect, because
3382 * they were called in the recursive validate_onscreen
3384 gtk_text_view_get_first_para_iter (text_view, &first_para);
3386 gtk_text_layout_validate_yrange (text_view->layout,
3389 text_view->first_para_pixels +
3390 SCREEN_HEIGHT (widget));
3393 text_view->onscreen_validated = TRUE;
3395 DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3397 /* This can have the odd side effect of triggering a scroll, which should
3398 * flip "onscreen_validated" back to FALSE, but should also get us
3399 * back into this function to turn it on again.
3401 gtk_text_view_update_adjustments (text_view);
3403 g_assert (text_view->onscreen_validated);
3407 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3409 if (text_view->first_validate_idle == 0)
3412 /* Do this first, which means that if an "invalidate"
3413 * occurs during any of this process, a new first_validate_callback
3414 * will be installed, and we'll start again.
3416 DV (g_print ("removing first validate in %s\n", G_STRLOC));
3417 g_source_remove (text_view->first_validate_idle);
3418 text_view->first_validate_idle = 0;
3420 /* be sure we have up-to-date screen size set on the
3423 gtk_text_view_update_layout_width (text_view);
3425 /* Bail out if we invalidated stuff; scrolling right away will just
3426 * confuse the issue.
3428 if (text_view->first_validate_idle != 0)
3430 DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3434 /* scroll to any marks, if that's pending. This can jump us to
3435 * the validation codepath used for scrolling onscreen, if so we
3436 * bail out. It won't jump if already in that codepath since
3437 * value_changed is not recursive, so also validate if
3440 if (!gtk_text_view_flush_scroll (text_view) ||
3441 !text_view->onscreen_validated)
3442 gtk_text_view_validate_onscreen (text_view);
3444 DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3446 g_assert (text_view->onscreen_validated);
3451 first_validate_callback (gpointer data)
3453 GtkTextView *text_view = data;
3455 /* Note that some of this code is duplicated at the end of size_allocate,
3456 * keep in sync with that.
3459 DV(g_print(G_STRLOC"\n"));
3461 gtk_text_view_flush_first_validate (text_view);
3467 incremental_validate_callback (gpointer data)
3469 GtkTextView *text_view = data;
3470 gboolean result = TRUE;
3472 DV(g_print(G_STRLOC"\n"));
3474 gtk_text_layout_validate (text_view->layout, 2000);
3476 gtk_text_view_update_adjustments (text_view);
3478 if (gtk_text_layout_is_valid (text_view->layout))
3480 text_view->incremental_validate_idle = 0;
3488 gtk_text_view_invalidate (GtkTextView *text_view)
3490 DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3491 text_view->onscreen_validated));
3493 text_view->onscreen_validated = FALSE;
3495 /* We'll invalidate when the layout is created */
3496 if (text_view->layout == NULL)
3499 if (!text_view->first_validate_idle)
3501 text_view->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3502 DV (g_print (G_STRLOC": adding first validate idle %d\n",
3503 text_view->first_validate_idle));
3506 if (!text_view->incremental_validate_idle)
3508 text_view->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3509 DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3510 text_view->incremental_validate_idle));
3515 invalidated_handler (GtkTextLayout *layout,
3518 GtkTextView *text_view;
3520 text_view = GTK_TEXT_VIEW (data);
3522 DV (g_print ("Invalidating due to layout invalidate signal\n"));
3523 gtk_text_view_invalidate (text_view);
3527 changed_handler (GtkTextLayout *layout,
3533 GtkTextView *text_view;
3535 GdkRectangle visible_rect;
3536 GdkRectangle redraw_rect;
3538 text_view = GTK_TEXT_VIEW (data);
3539 widget = GTK_WIDGET (data);
3541 DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3543 if (GTK_WIDGET_REALIZED (text_view))
3545 gtk_text_view_get_visible_rect (text_view, &visible_rect);
3547 redraw_rect.x = visible_rect.x;
3548 redraw_rect.width = visible_rect.width;
3549 redraw_rect.y = start_y;
3551 if (old_height == new_height)
3552 redraw_rect.height = old_height;
3553 else if (start_y + old_height > visible_rect.y)
3554 redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3556 redraw_rect.height = 0;
3558 if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3560 /* text_window_invalidate_rect() takes buffer coordinates */
3561 text_window_invalidate_rect (text_view->text_window,
3564 DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3568 redraw_rect.height));
3570 if (text_view->left_window)
3571 text_window_invalidate_rect (text_view->left_window,
3573 if (text_view->right_window)
3574 text_window_invalidate_rect (text_view->right_window,
3576 if (text_view->top_window)
3577 text_window_invalidate_rect (text_view->top_window,
3579 if (text_view->bottom_window)
3580 text_window_invalidate_rect (text_view->bottom_window,
3583 queue_update_im_spot_location (text_view);
3587 if (old_height != new_height)
3589 gboolean yoffset_changed = FALSE;
3591 int new_first_para_top;
3592 int old_first_para_top;
3595 /* If the bottom of the old area was above the top of the
3596 * screen, we need to scroll to keep the current top of the
3597 * screen in place. Remember that first_para_pixels is the
3598 * position of the top of the screen in coordinates relative to
3599 * the first paragraph onscreen.
3601 * In short we are adding the height delta of the portion of the
3602 * changed region above first_para_mark to text_view->yoffset.
3604 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3605 text_view->first_para_mark);
3607 gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3609 old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3611 if (new_first_para_top != old_first_para_top)
3613 text_view->yoffset += new_first_para_top - old_first_para_top;
3615 get_vadjustment (text_view)->value = text_view->yoffset;
3616 yoffset_changed = TRUE;
3619 if (yoffset_changed)
3621 DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3622 gtk_adjustment_value_changed (get_vadjustment (text_view));
3625 /* FIXME be smarter about which anchored widgets we update */
3627 tmp_list = text_view->children;
3628 while (tmp_list != NULL)
3630 GtkTextViewChild *child = tmp_list->data;
3633 gtk_text_view_update_child_allocation (text_view, child);
3635 tmp_list = g_slist_next (tmp_list);
3640 GtkRequisition old_req;
3641 GtkRequisition new_req;
3643 old_req = widget->requisition;
3645 /* Use this instead of gtk_widget_size_request wrapper
3646 * to avoid the optimization which just returns widget->requisition
3647 * if a resize hasn't been queued.
3649 GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3651 if (old_req.width != new_req.width ||
3652 old_req.height != new_req.height)
3654 gtk_widget_queue_resize_no_redraw (widget);
3660 gtk_text_view_realize (GtkWidget *widget)
3662 GtkTextView *text_view;
3663 GdkWindowAttr attributes;
3664 gint attributes_mask;
3667 text_view = GTK_TEXT_VIEW (widget);
3668 GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3670 attributes.window_type = GDK_WINDOW_CHILD;
3671 attributes.x = widget->allocation.x;
3672 attributes.y = widget->allocation.y;
3673 attributes.width = widget->allocation.width;
3674 attributes.height = widget->allocation.height;
3675 attributes.wclass = GDK_INPUT_OUTPUT;
3676 attributes.visual = gtk_widget_get_visual (widget);
3677 attributes.colormap = gtk_widget_get_colormap (widget);
3678 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3680 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3682 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3683 &attributes, attributes_mask);
3684 gdk_window_set_user_data (widget->window, widget);
3686 /* must come before text_window_realize calls */
3687 widget->style = gtk_style_attach (widget->style, widget->window);
3689 gdk_window_set_background (widget->window,
3690 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3692 text_window_realize (text_view->text_window, widget);
3694 if (text_view->left_window)
3695 text_window_realize (text_view->left_window, widget);
3697 if (text_view->top_window)
3698 text_window_realize (text_view->top_window, widget);
3700 if (text_view->right_window)
3701 text_window_realize (text_view->right_window, widget);
3703 if (text_view->bottom_window)
3704 text_window_realize (text_view->bottom_window, widget);
3706 gtk_text_view_ensure_layout (text_view);
3708 if (text_view->buffer)
3710 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3711 GDK_SELECTION_PRIMARY);
3712 gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3715 tmp_list = text_view->children;
3716 while (tmp_list != NULL)
3718 GtkTextViewChild *vc = tmp_list->data;
3720 text_view_child_set_parent_window (text_view, vc);
3722 tmp_list = tmp_list->next;
3727 gtk_text_view_unrealize (GtkWidget *widget)
3729 GtkTextView *text_view;
3731 text_view = GTK_TEXT_VIEW (widget);
3733 if (text_view->buffer)
3735 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3736 GDK_SELECTION_PRIMARY);
3737 gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3740 gtk_text_view_remove_validate_idles (text_view);
3742 if (text_view->popup_menu)
3744 gtk_widget_destroy (text_view->popup_menu);
3745 text_view->popup_menu = NULL;
3748 text_window_unrealize (text_view->text_window);
3750 if (text_view->left_window)
3751 text_window_unrealize (text_view->left_window);
3753 if (text_view->top_window)
3754 text_window_unrealize (text_view->top_window);
3756 if (text_view->right_window)
3757 text_window_unrealize (text_view->right_window);
3759 if (text_view->bottom_window)
3760 text_window_unrealize (text_view->bottom_window);
3762 gtk_text_view_destroy_layout (text_view);
3764 (* GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize) (widget);
3768 gtk_text_view_set_background (GtkTextView *text_view)
3770 GtkWidget *widget = GTK_WIDGET (text_view);
3772 gdk_window_set_background (widget->window,
3773 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3775 gdk_window_set_background (text_view->text_window->bin_window,
3776 &widget->style->base[GTK_WIDGET_STATE (widget)]);
3778 if (text_view->left_window)
3779 gdk_window_set_background (text_view->left_window->bin_window,
3780 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3781 if (text_view->right_window)
3782 gdk_window_set_background (text_view->right_window->bin_window,
3783 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3785 if (text_view->top_window)
3786 gdk_window_set_background (text_view->top_window->bin_window,
3787 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3789 if (text_view->bottom_window)
3790 gdk_window_set_background (text_view->bottom_window->bin_window,
3791 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3795 gtk_text_view_style_set (GtkWidget *widget,
3796 GtkStyle *previous_style)
3798 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3799 PangoContext *ltr_context, *rtl_context;
3801 if (GTK_WIDGET_REALIZED (widget))
3803 gtk_text_view_set_background (text_view);
3806 if (text_view->layout && previous_style)
3808 gtk_text_view_set_attributes_from_style (text_view,
3809 text_view->layout->default_style,
3813 ltr_context = gtk_widget_create_pango_context (widget);
3814 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
3815 rtl_context = gtk_widget_create_pango_context (widget);
3816 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
3818 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
3820 g_object_unref (ltr_context);
3821 g_object_unref (rtl_context);
3826 gtk_text_view_direction_changed (GtkWidget *widget,
3827 GtkTextDirection previous_direction)
3829 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3831 if (text_view->layout)
3833 text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3835 gtk_text_layout_default_style_changed (text_view->layout);
3840 gtk_text_view_state_changed (GtkWidget *widget,
3841 GtkStateType previous_state)
3843 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3846 if (GTK_WIDGET_REALIZED (widget))
3848 gtk_text_view_set_background (text_view);
3850 if (GTK_WIDGET_IS_SENSITIVE (widget))
3851 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3855 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3858 gdk_cursor_unref (cursor);
3860 text_view->mouse_cursor_obscured = FALSE;
3863 if (!GTK_WIDGET_IS_SENSITIVE (widget))
3865 /* Clear any selection */
3866 gtk_text_view_unselect (text_view);
3869 gtk_widget_queue_draw (widget);
3873 set_invisible_cursor (GdkWindow *window)
3875 GdkBitmap *empty_bitmap;
3878 char invisible_cursor_bits[] = { 0x0 };
3880 useless.red = useless.green = useless.blue = 0;
3883 empty_bitmap = gdk_bitmap_create_from_data (window,
3884 invisible_cursor_bits,
3887 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3892 gdk_window_set_cursor (window, cursor);
3894 gdk_cursor_unref (cursor);
3896 g_object_unref (empty_bitmap);
3900 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3902 if (text_view->mouse_cursor_obscured)
3905 set_invisible_cursor (text_view->text_window->bin_window);
3907 text_view->mouse_cursor_obscured = TRUE;
3911 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
3913 if (text_view->mouse_cursor_obscured)
3917 cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
3919 gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3920 gdk_cursor_unref (cursor);
3921 text_view->mouse_cursor_obscured = FALSE;
3926 gtk_text_view_grab_notify (GtkWidget *widget,
3927 gboolean was_grabbed)
3931 gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget));
3932 gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
3942 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3945 switch (event->type)
3947 case GDK_MOTION_NOTIFY:
3948 *x = event->motion.x;
3949 *y = event->motion.y;
3953 case GDK_BUTTON_PRESS:
3954 case GDK_2BUTTON_PRESS:
3955 case GDK_3BUTTON_PRESS:
3956 case GDK_BUTTON_RELEASE:
3957 *x = event->button.x;
3958 *y = event->button.y;
3963 case GDK_KEY_RELEASE:
3964 case GDK_ENTER_NOTIFY:
3965 case GDK_LEAVE_NOTIFY:
3966 case GDK_PROPERTY_NOTIFY:
3967 case GDK_SELECTION_CLEAR:
3968 case GDK_SELECTION_REQUEST:
3969 case GDK_SELECTION_NOTIFY:
3970 case GDK_PROXIMITY_IN:
3971 case GDK_PROXIMITY_OUT:
3972 case GDK_DRAG_ENTER:
3973 case GDK_DRAG_LEAVE:
3974 case GDK_DRAG_MOTION:
3975 case GDK_DRAG_STATUS:
3976 case GDK_DROP_START:
3977 case GDK_DROP_FINISHED:
3987 emit_event_on_tags (GtkWidget *widget,
3993 gboolean retval = FALSE;
3995 tags = gtk_text_iter_get_tags (iter);
4000 GtkTextTag *tag = tmp->data;
4002 if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
4008 tmp = g_slist_next (tmp);
4011 g_slist_free (tags);
4017 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
4019 GtkTextView *text_view;
4022 text_view = GTK_TEXT_VIEW (widget);
4024 if (text_view->layout == NULL ||
4025 get_buffer (text_view) == NULL)
4028 if (event->any.window != text_view->text_window->bin_window)
4031 if (get_event_coordinates (event, &x, &y))
4035 x += text_view->xoffset;
4036 y += text_view->yoffset;
4038 /* FIXME this is slow and we do it twice per event.
4039 * My favorite solution is to have GtkTextLayout cache
4040 * the last couple lookups.
4042 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4046 return emit_event_on_tags (widget, event, &iter);
4048 else if (event->type == GDK_KEY_PRESS ||
4049 event->type == GDK_KEY_RELEASE)
4053 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter,
4054 gtk_text_buffer_get_insert (get_buffer (text_view)));
4056 return emit_event_on_tags (widget, event, &iter);
4063 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
4065 gboolean retval = FALSE;
4066 gboolean obscure = FALSE;
4067 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4068 GtkTextMark *insert;
4070 gboolean can_insert;
4072 if (text_view->layout == NULL ||
4073 get_buffer (text_view) == NULL)
4076 /* Make sure input method knows where it is */
4077 flush_update_im_spot_location (text_view);
4079 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4080 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4081 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
4082 if (gtk_im_context_filter_keypress (text_view->im_context, event))
4084 text_view->need_im_reset = TRUE;
4086 gtk_text_view_reset_im_context (text_view);
4087 obscure = can_insert;
4091 else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event &&
4092 GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
4094 /* use overall editability not can_insert, more predictable for users */
4095 else if (text_view->editable &&
4096 (event->keyval == GDK_Return ||
4097 event->keyval == GDK_KP_Enter))
4099 /* this won't actually insert the newline if the cursor isn't
4102 gtk_text_view_reset_im_context (text_view);
4103 gtk_text_view_commit_text (text_view, "\n");
4108 /* Pass through Tab as literal tab, unless Control is held down */
4109 else if ((event->keyval == GDK_Tab ||
4110 event->keyval == GDK_KP_Tab ||
4111 event->keyval == GDK_ISO_Left_Tab) &&
4112 !(event->state & GDK_CONTROL_MASK))
4114 /* If the text widget isn't editable overall, or if the application
4115 * has turned off "accepts_tab", move the focus instead
4117 if (text_view->accepts_tab && text_view->editable)
4119 gtk_text_view_reset_im_context (text_view);
4120 gtk_text_view_commit_text (text_view, "\t");
4124 g_signal_emit_by_name (text_view, "move-focus",
4125 (event->state & GDK_SHIFT_MASK) ?
4126 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
4134 gtk_text_view_obscure_mouse_cursor (text_view);
4136 gtk_text_view_reset_blink_time (text_view);
4137 gtk_text_view_pend_cursor_blink (text_view);
4143 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
4145 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4146 GtkTextMark *insert;
4149 if (text_view->layout == NULL || get_buffer (text_view) == NULL)
4152 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4153 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4154 if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
4155 gtk_im_context_filter_keypress (text_view->im_context, event))
4157 text_view->need_im_reset = TRUE;
4161 return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
4165 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
4167 GtkTextView *text_view;
4169 text_view = GTK_TEXT_VIEW (widget);
4171 gtk_widget_grab_focus (widget);
4173 if (event->window != text_view->text_window->bin_window)
4175 /* Remove selection if any. */
4176 gtk_text_view_unselect (text_view);
4180 gtk_text_view_reset_blink_time (text_view);
4184 if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
4185 _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
4186 else if (event->button == 3)
4187 gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
4190 if (event->type == GDK_BUTTON_PRESS)
4192 gtk_text_view_reset_im_context (text_view);
4194 if (event->button == 1)
4196 /* If we're in the selection, start a drag copy/move of the
4197 * selection; otherwise, start creating a new selection.
4200 GtkTextIter start, end;
4202 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4204 event->x + text_view->xoffset,
4205 event->y + text_view->yoffset);
4207 if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4209 gtk_text_iter_in_range (&iter, &start, &end))
4211 text_view->drag_start_x = event->x;
4212 text_view->drag_start_y = event->y;
4213 text_view->pending_place_cursor_button = event->button;
4217 gtk_text_view_start_selection_drag (text_view, &iter, event);
4222 else if (event->button == 2)
4226 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4228 event->x + text_view->xoffset,
4229 event->y + text_view->yoffset);
4231 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4232 gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4234 text_view->editable);
4237 else if (event->button == 3)
4239 gtk_text_view_do_popup (text_view, event);
4243 else if ((event->type == GDK_2BUTTON_PRESS ||
4244 event->type == GDK_3BUTTON_PRESS) &&
4249 gtk_text_view_end_selection_drag (text_view);
4251 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4253 event->x + text_view->xoffset,
4254 event->y + text_view->yoffset);
4256 gtk_text_view_start_selection_drag (text_view, &iter, event);
4264 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4266 GtkTextView *text_view;
4268 text_view = GTK_TEXT_VIEW (widget);
4270 if (event->window != text_view->text_window->bin_window)
4273 if (event->button == 1)
4275 if (text_view->drag_start_x >= 0)
4277 text_view->drag_start_x = -1;
4278 text_view->drag_start_y = -1;
4281 if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget)))
4283 else if (text_view->pending_place_cursor_button == event->button)
4287 /* Unselect everything; we clicked inside selection, but
4288 * didn't move by the drag threshold, so just clear selection
4291 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4293 event->x + text_view->xoffset,
4294 event->y + text_view->yoffset);
4296 gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4297 gtk_text_view_check_cursor_blink (text_view);
4299 text_view->pending_place_cursor_button = 0;
4309 keymap_direction_changed (GdkKeymap *keymap,
4310 GtkTextView *text_view)
4312 gtk_text_view_check_keymap_direction (text_view);
4316 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4318 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4320 gtk_widget_queue_draw (widget);
4322 DV(g_print (G_STRLOC": focus_in_event\n"));
4324 gtk_text_view_reset_blink_time (text_view);
4326 if (text_view->cursor_visible && text_view->layout)
4328 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4329 gtk_text_view_check_cursor_blink (text_view);
4332 g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4333 "direction_changed",
4334 G_CALLBACK (keymap_direction_changed), text_view);
4335 gtk_text_view_check_keymap_direction (text_view);
4337 text_view->need_im_reset = TRUE;
4338 gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4344 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4346 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4348 gtk_text_view_end_selection_drag (text_view);
4350 gtk_widget_queue_draw (widget);
4352 DV(g_print (G_STRLOC": focus_out_event\n"));
4354 if (text_view->cursor_visible && text_view->layout)
4356 gtk_text_view_check_cursor_blink (text_view);
4357 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4360 g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4361 keymap_direction_changed,
4364 text_view->need_im_reset = TRUE;
4365 gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4371 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4373 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4375 gtk_text_view_unobscure_mouse_cursor (text_view);
4377 if (event->window == text_view->text_window->bin_window &&
4378 text_view->drag_start_x >= 0)
4383 gdk_event_request_motions (event);
4385 if (gtk_drag_check_threshold (widget,
4386 text_view->drag_start_x,
4387 text_view->drag_start_y,
4391 gint buffer_x, buffer_y;
4393 gtk_text_view_window_to_buffer_coords (text_view,
4394 GTK_TEXT_WINDOW_TEXT,
4395 text_view->drag_start_x,
4396 text_view->drag_start_y,
4400 gtk_text_layout_get_iter_at_pixel (text_view->layout,
4402 buffer_x, buffer_y);
4404 gtk_text_view_start_selection_dnd (text_view, &iter, event);
4413 gtk_text_view_paint (GtkWidget *widget,
4415 GdkEventExpose *event)
4417 GtkTextView *text_view;
4418 GList *child_exposes;
4422 text_view = GTK_TEXT_VIEW (widget);
4424 g_return_if_fail (text_view->layout != NULL);
4425 g_return_if_fail (text_view->xoffset >= 0);
4426 g_return_if_fail (text_view->yoffset >= 0);
4428 while (text_view->first_validate_idle != 0)
4430 DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4431 text_view->first_validate_idle));
4432 gtk_text_view_flush_first_validate (text_view);
4435 /* More regions could have become invalid in the above loop */
4436 updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4441 gdk_region_get_clipbox (updates, &rect);
4443 gdk_rectangle_union (area, &rect, area);
4445 gdk_region_destroy (updates);
4448 if (!text_view->onscreen_validated)
4450 g_warning (G_STRLOC ": somehow some text lines were modified or scrolling occurred since the last validation of lines on the screen - may be a text widget bug.");
4451 g_assert_not_reached ();
4455 printf ("painting %d,%d %d x %d\n",
4457 area->width, area->height);
4460 child_exposes = NULL;
4461 gtk_text_layout_draw (text_view->layout,
4463 text_view->text_window->bin_window,
4468 area->width, area->height,
4471 tmp_list = child_exposes;
4472 while (tmp_list != NULL)
4474 GtkWidget *child = tmp_list->data;
4476 gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4480 g_object_unref (child);
4482 tmp_list = tmp_list->next;
4485 g_list_free (child_exposes);
4489 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4493 if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4494 GTK_TEXT_WINDOW_TEXT))
4496 DV(g_print (">Exposed ("G_STRLOC")\n"));
4497 gtk_text_view_paint (widget, &event->area, event);
4500 if (event->window == widget->window)
4501 gtk_text_view_draw_focus (widget);
4503 /* Propagate exposes to all unanchored children.
4504 * Anchored children are handled in gtk_text_view_paint().
4506 tmp_list = GTK_TEXT_VIEW (widget)->children;
4507 while (tmp_list != NULL)
4509 GtkTextViewChild *vc = tmp_list->data;
4511 /* propagate_expose checks that event->window matches
4515 gtk_container_propagate_expose (GTK_CONTAINER (widget),
4519 tmp_list = tmp_list->next;
4526 gtk_text_view_draw_focus (GtkWidget *widget)
4528 gboolean interior_focus;
4530 /* We clear the focus if we are in interior focus mode. */
4531 gtk_widget_style_get (widget,
4532 "interior-focus", &interior_focus,
4535 if (GTK_WIDGET_DRAWABLE (widget))
4537 if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4539 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
4540 NULL, widget, "textview",
4542 widget->allocation.width,
4543 widget->allocation.height);
4547 gdk_window_clear (widget->window);
4553 gtk_text_view_focus (GtkWidget *widget,
4554 GtkDirectionType direction)
4556 GtkContainer *container;
4559 container = GTK_CONTAINER (widget);
4561 if (!gtk_widget_is_focus (widget) &&
4562 container->focus_child == NULL)
4564 gtk_widget_grab_focus (widget);
4570 * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4571 * children to get the focus
4573 GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
4574 result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4575 GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
4582 gtk_text_view_move_focus (GtkWidget *widget,
4583 GtkDirectionType direction_type)
4585 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4587 if (GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus)
4588 GTK_TEXT_VIEW_GET_CLASS (text_view)->move_focus (text_view,
4597 gtk_text_view_add (GtkContainer *container,
4600 /* This is pretty random. */
4601 gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4603 GTK_TEXT_WINDOW_WIDGET,
4608 gtk_text_view_remove (GtkContainer *container,
4612 GtkTextView *text_view;
4613 GtkTextViewChild *vc;
4615 text_view = GTK_TEXT_VIEW (container);
4618 iter = text_view->children;
4620 while (iter != NULL)
4624 if (vc->widget == child)
4627 iter = g_slist_next (iter);
4630 g_assert (iter != NULL); /* be sure we had the child in the list */
4632 text_view->children = g_slist_remove (text_view->children, vc);
4634 gtk_widget_unparent (vc->widget);
4636 text_view_child_free (vc);
4640 gtk_text_view_forall (GtkContainer *container,
4641 gboolean include_internals,
4642 GtkCallback callback,
4643 gpointer callback_data)
4646 GtkTextView *text_view;
4649 g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4650 g_return_if_fail (callback != NULL);
4652 text_view = GTK_TEXT_VIEW (container);
4654 copy = g_slist_copy (text_view->children);
4657 while (iter != NULL)
4659 GtkTextViewChild *vc = iter->data;
4661 (* callback) (vc->widget, callback_data);
4663 iter = g_slist_next (iter);
4666 g_slist_free (copy);
4669 #define CURSOR_ON_MULTIPLIER 2
4670 #define CURSOR_OFF_MULTIPLIER 1
4671 #define CURSOR_PEND_MULTIPLIER 3
4672 #define CURSOR_DIVIDER 3
4675 cursor_blinks (GtkTextView *text_view)
4677 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4680 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4683 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4686 g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4691 if (text_view->editable)
4693 GtkTextMark *insert;
4696 insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4697 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4699 if (gtk_text_iter_editable (&iter, text_view->editable))
4707 get_cursor_time (GtkTextView *text_view)
4709 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4712 g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
4718 get_cursor_blink_timeout (GtkTextView *text_view)
4720 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4723 g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
4734 blink_cb (gpointer data)
4736 GtkTextView *text_view;
4737 GtkTextViewPrivate *priv;
4741 text_view = GTK_TEXT_VIEW (data);
4742 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4744 if (!GTK_WIDGET_HAS_FOCUS (text_view))
4746 g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
4747 "connect a handler to this signal, it must return\n"
4748 "FALSE so the text view gets the event as well");
4750 gtk_text_view_check_cursor_blink (text_view);
4755 g_assert (text_view->layout);
4756 g_assert (text_view->cursor_visible);
4758 visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4760 blink_timeout = get_cursor_blink_timeout (text_view);
4761 if (priv->blink_time > 1000 * blink_timeout &&
4762 blink_timeout < G_MAXINT/1000)
4764 /* we've blinked enough without the user doing anything, stop blinking */
4766 text_view->blink_timeout = 0;
4769 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4774 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
4777 priv->blink_time += get_cursor_time (text_view);
4780 /* Block changed_handler while changing the layout's cursor visibility
4781 * because it would expose the whole paragraph. Instead, we expose
4782 * the cursor's area(s) manually below.
4784 g_signal_handlers_block_by_func (text_view->layout,
4787 gtk_text_layout_set_cursor_visible (text_view->layout, !visible);
4788 g_signal_handlers_unblock_by_func (text_view->layout,
4792 text_window_invalidate_cursors (text_view->text_window);
4794 /* Remove ourselves */
4800 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4802 if (text_view->blink_timeout)
4804 g_source_remove (text_view->blink_timeout);
4805 text_view->blink_timeout = 0;
4810 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4812 if (text_view->layout != NULL &&
4813 text_view->cursor_visible &&
4814 GTK_WIDGET_HAS_FOCUS (text_view))
4816 if (cursor_blinks (text_view))
4818 if (text_view->blink_timeout == 0)
4820 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4822 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4829 gtk_text_view_stop_cursor_blink (text_view);
4830 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4835 gtk_text_view_stop_cursor_blink (text_view);
4836 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4841 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
4843 if (text_view->layout != NULL &&
4844 text_view->cursor_visible &&
4845 GTK_WIDGET_HAS_FOCUS (text_view) &&
4846 cursor_blinks (text_view))
4848 gtk_text_view_stop_cursor_blink (text_view);
4849 gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4851 text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
4858 gtk_text_view_reset_blink_time (GtkTextView *text_view)
4860 GtkTextViewPrivate *priv;
4862 priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4864 priv->blink_time = 0;
4869 * Key binding handlers
4873 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4874 GtkTextIter *newplace,
4877 gboolean ret = TRUE;
4881 ret = gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4887 ret = gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4895 move_cursor (GtkTextView *text_view,
4896 const GtkTextIter *new_location,
4897 gboolean extend_selection)
4899 if (extend_selection)
4900 gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4904 gtk_text_buffer_place_cursor (get_buffer (text_view),
4906 gtk_text_view_check_cursor_blink (text_view);
4910 gtk_text_view_move_cursor_internal (GtkTextView *text_view,
4911 GtkMovementStep step,
4913 gboolean extend_selection)
4916 GtkTextIter newplace;
4917 gint cursor_x_pos = 0;
4918 GtkDirectionType leave_direction = -1;
4920 if (!text_view->cursor_visible)
4922 GtkScrollStep scroll_step;
4926 case GTK_MOVEMENT_VISUAL_POSITIONS:
4927 leave_direction = count > 0 ? GTK_DIR_RIGHT : GTK_DIR_LEFT;
4929 case GTK_MOVEMENT_LOGICAL_POSITIONS:
4930 case GTK_MOVEMENT_WORDS:
4931 scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
4933 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4934 scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
4936 case GTK_MOVEMENT_DISPLAY_LINES:
4937 leave_direction = count > 0 ? GTK_DIR_DOWN : GTK_DIR_UP;
4939 case GTK_MOVEMENT_PARAGRAPHS:
4940 case GTK_MOVEMENT_PARAGRAPH_ENDS:
4941 scroll_step = GTK_SCROLL_STEPS;
4943 case GTK_MOVEMENT_PAGES:
4944 scroll_step = GTK_SCROLL_PAGES;
4946 case GTK_MOVEMENT_HORIZONTAL_PAGES:
4947 scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
4949 case GTK_MOVEMENT_BUFFER_ENDS:
4950 scroll_step = GTK_SCROLL_ENDS;
4953 scroll_step = GTK_SCROLL_PAGES;
4957 if (!gtk_text_view_move_viewport (text_view, scroll_step, count))
4959 if (leave_direction != -1 &&
4960 !gtk_widget_keynav_failed (GTK_WIDGET (text_view),
4963 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
4970 gtk_text_view_reset_im_context (text_view);
4972 if (step == GTK_MOVEMENT_PAGES)
4974 if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
4975 gtk_widget_error_bell (GTK_WIDGET (text_view));
4977 gtk_text_view_check_cursor_blink (text_view);
4978 gtk_text_view_pend_cursor_blink (text_view);
4981 else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
4983 if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
4984 gtk_widget_error_bell (GTK_WIDGET (text_view));
4986 gtk_text_view_check_cursor_blink (text_view);
4987 gtk_text_view_pend_cursor_blink (text_view);
4991 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4992 gtk_text_buffer_get_insert (get_buffer (text_view)));
4995 if (step == GTK_MOVEMENT_DISPLAY_LINES)
4996 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
5000 case GTK_MOVEMENT_LOGICAL_POSITIONS:
5001 gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
5004 case GTK_MOVEMENT_VISUAL_POSITIONS:
5005 gtk_text_layout_move_iter_visually (text_view->layout,
5009 case GTK_MOVEMENT_WORDS:
5011 gtk_text_iter_backward_visible_word_starts (&newplace, -count);
5014 if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
5015 gtk_text_iter_forward_to_line_end (&newplace);
5019 case GTK_MOVEMENT_DISPLAY_LINES:
5022 leave_direction = GTK_DIR_UP;
5024 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5025 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5027 gtk_text_iter_set_line_offset (&newplace, 0);
5031 leave_direction = GTK_DIR_DOWN;
5033 if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
5034 gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
5036 gtk_text_iter_forward_to_line_end (&newplace);
5040 case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
5042 gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
5043 else if (count < -1)
5044 gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
5047 gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
5050 case GTK_MOVEMENT_PARAGRAPHS:
5053 if (!gtk_text_iter_ends_line (&newplace))
5055 gtk_text_iter_forward_to_line_end (&newplace);
5058 gtk_text_iter_forward_visible_lines (&newplace, count);
5059 gtk_text_iter_forward_to_line_end (&newplace);
5063 if (gtk_text_iter_get_line_offset (&newplace) > 0)
5064 gtk_text_iter_set_line_offset (&newplace, 0);
5065 gtk_text_iter_forward_visible_lines (&newplace, count);
5066 gtk_text_iter_set_line_offset (&newplace, 0);
5070 case GTK_MOVEMENT_PARAGRAPH_ENDS:
5073 if (!gtk_text_iter_ends_line (&newplace))
5074 gtk_text_iter_forward_to_line_end (&newplace);
5078 gtk_text_iter_set_line_offset (&newplace, 0);
5082 case GTK_MOVEMENT_BUFFER_ENDS:
5084 gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
5086 gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
5093 /* call move_cursor() even if the cursor hasn't moved, since it
5094 cancels the selection
5096 move_cursor (text_view, &newplace, extend_selection);
5098 if (!gtk_text_iter_equal (&insert, &newplace))
5100 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5101 gtk_text_view_scroll_mark_onscreen (text_view,
5102 gtk_text_buffer_get_insert (get_buffer (text_view)));
5104 if (step == GTK_MOVEMENT_DISPLAY_LINES)
5105 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
5107 else if (leave_direction != -1)
5109 if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
5112 g_signal_emit_by_name (text_view, "move-focus", leave_direction);
5117 gtk_widget_error_bell (GTK_WIDGET (text_view));
5120 gtk_text_view_check_cursor_blink (text_view);
5121 gtk_text_view_pend_cursor_blink (text_view);
5125 gtk_text_view_move_cursor (GtkTextView *text_view,
5126 GtkMovementStep step,
5128 gboolean extend_selection)
5130 gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
5134 gtk_text_view_page_horizontally (GtkTextView *text_view,
5136 gboolean extend_selection)
5138 gtk_text_view_move_cursor_internal (text_view, GTK_MOVEMENT_HORIZONTAL_PAGES,
5139 count, extend_selection);
5144 gtk_text_view_move_viewport (GtkTextView *text_view,
5148 GtkAdjustment *adjustment;
5153 case GTK_SCROLL_STEPS:
5154 case GTK_SCROLL_PAGES:
5155 case GTK_SCROLL_ENDS:
5156 adjustment = get_vadjustment (text_view);
5158 case GTK_SCROLL_HORIZONTAL_STEPS:
5159 case GTK_SCROLL_HORIZONTAL_PAGES:
5160 case GTK_SCROLL_HORIZONTAL_ENDS:
5161 adjustment = get_hadjustment (text_view);
5164 adjustment = get_vadjustment (text_view);
5170 case GTK_SCROLL_STEPS:
5171 case GTK_SCROLL_HORIZONTAL_STEPS:
5172 increment = adjustment->step_increment;
5174 case GTK_SCROLL_PAGES:
5175 case GTK_SCROLL_HORIZONTAL_PAGES:
5176 increment = adjustment->page_increment;
5178 case GTK_SCROLL_ENDS:
5179 case GTK_SCROLL_HORIZONTAL_ENDS:
5180 increment = adjustment->upper - adjustment->lower;
5187 return set_adjustment_clamped (adjustment, adjustment->value + count * increment);
5191 gtk_text_view_set_anchor (GtkTextView *text_view)
5195 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5196 gtk_text_buffer_get_insert (get_buffer (text_view)));
5198 gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
5202 gtk_text_view_scroll_pages (GtkTextView *text_view,
5204 gboolean extend_selection)
5209 gint cursor_x_pos, cursor_y_pos;
5210 GtkTextMark *insert_mark;
5211 GtkTextIter old_insert;
5212 GtkTextIter new_insert;
5216 g_return_val_if_fail (text_view->vadjustment != NULL, FALSE);
5218 adj = text_view->vadjustment;
5220 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5222 /* Make sure we start from the current cursor position, even
5223 * if it was offscreen, but don't queue more scrolls if we're
5226 if (text_view->pending_scroll)
5227 cancel_pending_scroll (text_view);
5229 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5231 /* Validate the region that will be brought into view by the cursor motion
5233 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5234 &old_insert, insert_mark);
5238 gtk_text_view_get_first_para_iter (text_view, &anchor);
5239 y0 = adj->page_size;
5240 y1 = adj->page_size + count * adj->page_increment;
5244 gtk_text_view_get_first_para_iter (text_view, &anchor);
5245 y0 = count * adj->page_increment + adj->page_size;
5249 gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
5250 /* FIXME do we need to update the adjustment ranges here? */
5252 new_insert = old_insert;
5254 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5256 /* already at top, just be sure we are at offset 0 */
5257 gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5258 move_cursor (text_view, &new_insert, extend_selection);
5260 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5262 /* already at bottom, just be sure we are at the end */
5263 gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5264 move_cursor (text_view, &new_insert, extend_selection);
5268 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5270 oldval = adj->value;
5271 newval = adj->value;
5273 newval += count * adj->page_increment;
5275 set_adjustment_clamped (adj, newval);
5276 cursor_y_pos += adj->value - oldval;
5278 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5279 clamp_iter_onscreen (text_view, &new_insert);
5280 move_cursor (text_view, &new_insert, extend_selection);
5282 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5285 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5286 * only guarantees 1 pixel onscreen.
5288 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5289 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5291 return !gtk_text_iter_equal (&old_insert, &new_insert);
5295 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5297 gboolean extend_selection)
5302 gint cursor_x_pos, cursor_y_pos;
5303 GtkTextMark *insert_mark;
5304 GtkTextIter old_insert;
5305 GtkTextIter new_insert;
5308 g_return_val_if_fail (text_view->hadjustment != NULL, FALSE);
5310 adj = text_view->hadjustment;
5312 insert_mark = gtk_text_buffer_get_insert (get_buffer (text_view));
5314 /* Make sure we start from the current cursor position, even
5315 * if it was offscreen, but don't queue more scrolls if we're
5318 if (text_view->pending_scroll)
5319 cancel_pending_scroll (text_view);
5321 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5323 /* Validate the line that we're moving within.
5325 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5326 &old_insert, insert_mark);
5328 gtk_text_layout_get_line_yrange (text_view->layout, &old_insert, &y, &height);
5329 gtk_text_layout_validate_yrange (text_view->layout, &old_insert, y, y + height);
5330 /* FIXME do we need to update the adjustment ranges here? */
5332 new_insert = old_insert;
5334 if (count < 0 && adj->value <= (adj->lower + 1e-12))
5336 /* already at far left, just be sure we are at offset 0 */
5337 gtk_text_iter_set_line_offset (&new_insert, 0);
5338 move_cursor (text_view, &new_insert, extend_selection);
5340 else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5342 /* already at far right, just be sure we are at the end */
5343 if (!gtk_text_iter_ends_line (&new_insert))
5344 gtk_text_iter_forward_to_line_end (&new_insert);
5345 move_cursor (text_view, &new_insert, extend_selection);
5349 gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5351 oldval = adj->value;
5352 newval = adj->value;
5354 newval += count * adj->page_increment;
5356 set_adjustment_clamped (adj, newval);
5357 cursor_x_pos += adj->value - oldval;
5359 gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5360 clamp_iter_onscreen (text_view, &new_insert);
5361 move_cursor (text_view, &new_insert, extend_selection);
5363 gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5366 /* FIXME for lines shorter than the overall widget width, this results in a
5367 * "bounce" effect as we scroll to the right of the widget, then scroll
5368 * back to get the end of the line onscreen.
5369 * http://bugzilla.gnome.org/show_bug.cgi?id=68963
5372 /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5373 * only guarantees 1 pixel onscreen.
5375 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5376 gtk_text_view_scroll_mark_onscreen (text_view, insert_mark);
5378 return !gtk_text_iter_equal (&old_insert, &new_insert);
5382 whitespace (gunichar ch, gpointer user_data)
5384 return (ch == ' ' || ch == '\t');
5388 not_whitespace (gunichar ch, gpointer user_data)
5390 return !whitespace (ch, user_data);
5394 find_whitepace_region (const GtkTextIter *center,
5395 GtkTextIter *start, GtkTextIter *end)
5400 if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5401 gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5402 if (whitespace (gtk_text_iter_get_char (end), NULL))
5403 gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5405 return !gtk_text_iter_equal (start, end);
5409 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5412 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5413 text_view->editable))
5415 gtk_widget_error_bell (GTK_WIDGET (text_view));
5420 gtk_text_view_delete_from_cursor (GtkTextView *text_view,
5427 gboolean leave_one = FALSE;
5429 gtk_text_view_reset_im_context (text_view);
5431 if (type == GTK_DELETE_CHARS)
5433 /* Char delete deletes the selection, if one exists */
5434 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5435 text_view->editable))
5439 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5440 gtk_text_buffer_get_insert (get_buffer (text_view)));
5447 case GTK_DELETE_CHARS:
5448 gtk_text_iter_forward_cursor_positions (&end, count);
5451 case GTK_DELETE_WORD_ENDS:
5453 gtk_text_iter_forward_word_ends (&end, count);
5455 gtk_text_iter_backward_word_starts (&start, 0 - count);
5458 case GTK_DELETE_WORDS:
5461 case GTK_DELETE_DISPLAY_LINE_ENDS:
5464 case GTK_DELETE_DISPLAY_LINES:
5467 case GTK_DELETE_PARAGRAPH_ENDS:
5468 /* If we're already at a newline, we need to
5469 * simply delete that newline, instead of
5470 * moving to the next one.
5472 if (gtk_text_iter_ends_line (&end))
5474 gtk_text_iter_forward_line (&end);
5480 if (!gtk_text_iter_forward_to_line_end (&end))
5486 /* FIXME figure out what a negative count means
5490 case GTK_DELETE_PARAGRAPHS:
5493 gtk_text_iter_set_line_offset (&start, 0);
5494 gtk_text_iter_forward_to_line_end (&end);
5496 /* Do the lines beyond the first. */
5499 gtk_text_iter_forward_to_line_end (&end);
5505 /* FIXME negative count? */
5509 case GTK_DELETE_WHITESPACE:
5511 find_whitepace_region (&insert, &start, &end);
5519 if (!gtk_text_iter_equal (&start, &end))
5521 gtk_text_buffer_begin_user_action (get_buffer (text_view));
5523 if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5524 text_view->editable))
5527 gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5529 text_view->editable);
5533 gtk_widget_error_bell (GTK_WIDGET (text_view));
5536 gtk_text_buffer_end_user_action (get_buffer (text_view));
5537 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5539 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5540 gtk_text_view_scroll_mark_onscreen (text_view,
5541 gtk_text_buffer_get_insert (get_buffer (text_view)));
5545 gtk_widget_error_bell (GTK_WIDGET (text_view));
5550 gtk_text_view_backspace (GtkTextView *text_view)
5554 gtk_text_view_reset_im_context (text_view);
5556 /* Backspace deletes the selection, if one exists */
5557 if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5558 text_view->editable))
5561 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5563 gtk_text_buffer_get_insert (get_buffer (text_view)));
5565 if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5566 TRUE, text_view->editable))
5568 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5569 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5570 gtk_text_view_scroll_mark_onscreen (text_view,
5571 gtk_text_buffer_get_insert (get_buffer (text_view)));
5575 gtk_widget_error_bell (GTK_WIDGET (text_view));
5580 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5582 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5583 GDK_SELECTION_CLIPBOARD);
5585 gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5587 text_view->editable);
5588 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5589 gtk_text_view_scroll_mark_onscreen (text_view,
5590 gtk_text_buffer_get_insert (get_buffer (text_view)));
5594 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5596 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5597 GDK_SELECTION_CLIPBOARD);
5599 gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5602 /* on copy do not scroll, we are already onscreen */
5606 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5608 GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5609 GDK_SELECTION_CLIPBOARD);
5611 gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5614 text_view->editable);
5615 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5616 gtk_text_view_scroll_mark_onscreen (text_view,
5617 gtk_text_buffer_get_insert (get_buffer (text_view)));
5621 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5623 if (text_view->text_window)
5624 text_window_invalidate_cursors (text_view->text_window);
5626 text_view->overwrite_mode = !text_view->overwrite_mode;
5628 if (text_view->layout)
5629 gtk_text_layout_set_overwrite_mode (text_view->layout,
5630 text_view->overwrite_mode && text_view->editable);
5632 if (text_view->text_window)
5633 text_window_invalidate_cursors (text_view->text_window);
5635 gtk_text_view_pend_cursor_blink (text_view);
5637 g_object_notify (G_OBJECT (text_view), "overwrite");
5641 * gtk_text_view_get_overwrite:
5642 * @text_view: a #GtkTextView
5644 * Returns whether the #GtkTextView is in overwrite mode or not.
5646 * Return value: whether @text_view is in overwrite mode or not.
5651 gtk_text_view_get_overwrite (GtkTextView *text_view)
5653 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5655 return text_view->overwrite_mode;
5659 * gtk_text_view_set_overwrite:
5660 * @text_view: a #GtkTextView
5661 * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
5663 * Changes the #GtkTextView overwrite mode.
5668 gtk_text_view_set_overwrite (GtkTextView *text_view,
5671 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5672 overwrite = overwrite != FALSE;
5674 if (text_view->overwrite_mode != overwrite)
5675 gtk_text_view_toggle_overwrite (text_view);
5679 * gtk_text_view_set_accepts_tab:
5680 * @text_view: A #GtkTextView
5681 * @accepts_tab: %TRUE if pressing the Tab key should insert a tab
5682 * character, %FALSE, if pressing the Tab key should move the
5685 * Sets the behavior of the text widget when the Tab key is pressed.
5686 * If @accepts_tab is %TRUE, a tab character is inserted. If @accepts_tab
5687 * is %FALSE the keyboard focus is moved to the next widget in the focus
5693 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
5694 gboolean accepts_tab)
5696 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5698 accepts_tab = accepts_tab != FALSE;
5700 if (text_view->accepts_tab != accepts_tab)
5702 text_view->accepts_tab = accepts_tab;
5704 g_object_notify (G_OBJECT (text_view), "accepts-tab");
5709 * gtk_text_view_get_accepts_tab:
5710 * @text_view: A #GtkTextView
5712 * Returns whether pressing the Tab key inserts a tab characters.
5713 * gtk_text_view_set_accepts_tab().
5715 * Return value: %TRUE if pressing the Tab key inserts a tab character,
5716 * %FALSE if pressing the Tab key moves the keyboard focus.
5721 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
5723 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5725 return text_view->accepts_tab;
5729 gtk_text_view_compat_move_focus (GtkTextView *text_view,
5730 GtkDirectionType direction_type)
5732 GSignalInvocationHint *hint = g_signal_get_invocation_hint (text_view);
5734 /* as of GTK+ 2.12, the "move-focus" signal has been moved to GtkWidget,
5735 * the evil code below makes sure that both emitting the signal and
5736 * calling the virtual function directly continue to work as expetcted
5739 if (hint->signal_id == g_signal_lookup ("move-focus", GTK_TYPE_WIDGET))
5741 /* if this is a signal emission, chain up */
5743 GValue instance_and_params[2] = { { 0, }, { 0, } };
5744 GValue return_value = { 0, };
5746 g_value_init (&instance_and_params[0], GTK_TYPE_WIDGET);
5747 g_value_set_object (&instance_and_params[0], text_view);
5749 g_value_init (&instance_and_params[1], GTK_TYPE_DIRECTION_TYPE);
5750 g_value_set_enum (&instance_and_params[1], direction_type);
5752 g_value_init (&return_value, G_TYPE_BOOLEAN);
5754 g_signal_chain_from_overridden (instance_and_params, &return_value);
5756 g_value_unset (&instance_and_params[0]);
5757 g_value_unset (&instance_and_params[1]);
5758 g_value_unset (&return_value);
5762 /* otherwise emit the signal, since somebody called the virtual
5766 g_signal_emit_by_name (text_view, "move-focus", direction_type);
5775 gtk_text_view_unselect (GtkTextView *text_view)
5779 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5780 gtk_text_buffer_get_insert (get_buffer (text_view)));
5782 gtk_text_buffer_move_mark (get_buffer (text_view),
5783 gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
5788 get_iter_at_pointer (GtkTextView *text_view,
5793 gint xcoord, ycoord;
5794 GdkModifierType state;
5796 gdk_window_get_pointer (text_view->text_window->bin_window,
5797 &xcoord, &ycoord, &state);
5799 gtk_text_layout_get_iter_at_pixel (text_view->layout,
5801 xcoord + text_view->xoffset,
5802 ycoord + text_view->yoffset);
5811 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5812 const gchar *mark_name)
5814 GtkTextIter newplace;
5817 get_iter_at_pointer (text_view, &newplace, NULL, NULL);
5819 mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5821 /* This may invalidate the layout */
5822 DV(g_print (G_STRLOC": move mark\n"));
5824 gtk_text_buffer_move_mark (get_buffer (text_view),
5828 DV(g_print (G_STRLOC": scrolling onscreen\n"));
5829 gtk_text_view_scroll_mark_onscreen (text_view, mark);
5831 DV (g_print ("first validate idle leaving %s is %d\n",
5832 G_STRLOC, text_view->first_validate_idle));
5836 selection_scan_timeout (gpointer data)
5838 GtkTextView *text_view;
5840 text_view = GTK_TEXT_VIEW (data);
5842 DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5843 gtk_text_view_scroll_mark_onscreen (text_view,
5844 gtk_text_buffer_get_insert (get_buffer (text_view)));
5846 return TRUE; /* remain installed. */
5849 #define UPPER_OFFSET_ANCHOR 0.8
5850 #define LOWER_OFFSET_ANCHOR 0.2
5853 check_scroll (gdouble offset, GtkAdjustment *adj)
5855 if ((offset > UPPER_OFFSET_ANCHOR &&
5856 adj->value + adj->page_size < adj->upper) ||
5857 (offset < LOWER_OFFSET_ANCHOR &&
5858 adj->value > adj->lower))
5865 drag_scan_timeout (gpointer data)
5867 GtkTextView *text_view;
5868 GtkTextIter newplace;
5869 gint x, y, width, height;
5870 gdouble pointer_xoffset, pointer_yoffset;
5872 text_view = GTK_TEXT_VIEW (data);
5874 get_iter_at_pointer (text_view, &newplace, &x, &y);
5875 gdk_drawable_get_size (text_view->text_window->bin_window, &width, &height);
5877 gtk_text_buffer_move_mark (get_buffer (text_view),
5878 text_view->dnd_mark,
5881 pointer_xoffset = (gdouble) x / width;
5882 pointer_yoffset = (gdouble) y / height;
5884 if (check_scroll (pointer_xoffset, text_view->hadjustment) ||
5885 check_scroll (pointer_yoffset, text_view->vadjustment))
5887 /* do not make offsets surpass lower nor upper anchors, this makes
5888 * scrolling speed relative to the distance of the pointer to the
5889 * anchors when it moves beyond them.
5891 pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
5892 pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
5894 gtk_text_view_scroll_to_mark (text_view,
5895 text_view->dnd_mark,
5896 0., TRUE, pointer_xoffset, pointer_yoffset);
5907 } SelectionGranularity;
5910 * Move @start and @end to the boundaries of the selection unit (indicated by
5911 * @granularity) which contained @start initially.
5912 * If the selction unit is SELECT_WORDS and @start is not contained in a word
5913 * the selection is extended to all the white spaces between the end of the
5914 * word preceding @start and the start of the one following.
5917 extend_selection (GtkTextView *text_view,
5918 SelectionGranularity granularity,
5924 if (granularity == SELECT_WORDS)
5926 if (gtk_text_iter_inside_word (start))
5928 if (!gtk_text_iter_starts_word (start))
5929 gtk_text_iter_backward_visible_word_start (start);
5931 if (!gtk_text_iter_ends_word (end))
5933 if (!gtk_text_iter_forward_visible_word_end (end))
5934 gtk_text_iter_forward_to_end (end);
5942 if (gtk_text_iter_backward_visible_word_start (&tmp))
5943 gtk_text_iter_forward_visible_word_end (&tmp);
5945 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
5948 gtk_text_iter_set_line_offset (start, 0);
5951 if (!gtk_text_iter_forward_visible_word_end (&tmp))
5952 gtk_text_iter_forward_to_end (&tmp);
5954 if (gtk_text_iter_ends_word (&tmp))
5955 gtk_text_iter_backward_visible_word_start (&tmp);
5957 if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
5960 gtk_text_iter_forward_to_line_end (end);
5963 else if (granularity == SELECT_LINES)
5965 if (gtk_text_view_starts_display_line (text_view, start))
5967 /* If on a display line boundary, we assume the user
5968 * clicked off the end of a line and we therefore select
5969 * the line before the boundary.
5971 gtk_text_view_backward_display_line_start (text_view, start);
5975 /* start isn't on the start of a line, so we move it to the
5976 * start, and move end to the end unless it's already there.
5978 gtk_text_view_backward_display_line_start (text_view, start);
5980 if (!gtk_text_view_starts_display_line (text_view, end))
5981 gtk_text_view_forward_display_line_end (text_view, end);
5989 SelectionGranularity granularity;
5990 GtkTextMark *orig_start;
5991 GtkTextMark *orig_end;
5995 selection_data_free (SelectionData *data)
5997 if (data->orig_start != NULL)
5998 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
6000 if (data->orig_end != NULL)
6001 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
6007 selection_motion_event_handler (GtkTextView *text_view,
6008 GdkEventMotion *event,
6009 SelectionData *data)
6011 gdk_event_request_motions (event);
6013 if (data->granularity == SELECT_CHARACTERS)
6015 move_mark_to_pointer_and_scroll (text_view, "insert");
6019 GtkTextIter cursor, start, end;
6020 GtkTextIter orig_start, orig_end;
6021 GtkTextBuffer *buffer;
6023 buffer = get_buffer (text_view);
6025 gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
6026 gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
6028 get_iter_at_pointer (text_view, &cursor, NULL, NULL);
6031 extend_selection (text_view, data->granularity, &start, &end);
6033 /* either the selection extends to the front, or end (or not) */
6034 if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
6035 gtk_text_buffer_select_range (buffer, &start, &orig_end);
6037 gtk_text_buffer_select_range (buffer, &end, &orig_start);
6039 gtk_text_view_scroll_mark_onscreen (text_view,
6040 gtk_text_buffer_get_insert (buffer));
6043 /* If we had to scroll offscreen, insert a timeout to do so
6044 * again. Note that in the timeout, even if the mouse doesn't
6045 * move, due to this scroll xoffset/yoffset will have changed
6046 * and we'll need to scroll again.
6048 if (text_view->scroll_timeout != 0) /* reset on every motion event */
6049 g_source_remove (text_view->scroll_timeout);
6051 text_view->scroll_timeout =
6052 gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
6058 gtk_text_view_start_selection_drag (GtkTextView *text_view,
6059 const GtkTextIter *iter,
6060 GdkEventButton *button)
6062 GtkTextIter cursor, ins, bound;
6063 GtkTextIter orig_start, orig_end;
6064 GtkTextBuffer *buffer;
6065 SelectionData *data;
6067 if (text_view->selection_drag_handler != 0)
6070 data = g_new0 (SelectionData, 1);
6072 if (button->type == GDK_2BUTTON_PRESS)
6073 data->granularity = SELECT_WORDS;
6074 else if (button->type == GDK_3BUTTON_PRESS)
6075 data->granularity = SELECT_LINES;
6077 data->granularity = SELECT_CHARACTERS;
6079 gtk_grab_add (GTK_WIDGET (text_view));
6081 buffer = get_buffer (text_view);
6086 extend_selection (text_view, data->granularity, &ins, &bound);
6090 if (button->state & GDK_SHIFT_MASK)
6092 /* Extend selection */
6093 GtkTextIter old_ins, old_bound;
6094 GtkTextIter old_start, old_end;
6096 gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
6097 gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
6098 old_start = old_ins;
6099 old_end = old_bound;
6100 gtk_text_iter_order (&old_start, &old_end);
6102 /* move the front cursor, if the mouse is in front of the selection. Should the
6103 * cursor however be inside the selection (this happens on tripple click) then we
6104 * move the side which was last moved (current insert mark) */
6105 if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
6106 (gtk_text_iter_compare (&cursor, &old_end) < 0 &&
6107 gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
6110 orig_start = old_end;
6122 gtk_text_buffer_select_range (buffer, &ins, &bound);
6124 gtk_text_iter_order (&orig_start, &orig_end);
6125 data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
6127 data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
6130 gtk_text_view_check_cursor_blink (text_view);
6132 text_view->selection_drag_handler = g_signal_connect_data (text_view,
6133 "motion_notify_event",
6134 G_CALLBACK (selection_motion_event_handler),
6136 (GClosureNotify) selection_data_free, 0);
6139 /* returns whether we were really dragging */
6141 gtk_text_view_end_selection_drag (GtkTextView *text_view)
6143 if (text_view->selection_drag_handler == 0)
6146 g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
6147 text_view->selection_drag_handler = 0;
6149 if (text_view->scroll_timeout != 0)
6151 g_source_remove (text_view->scroll_timeout);
6152 text_view->scroll_timeout = 0;
6155 gtk_grab_remove (GTK_WIDGET (text_view));
6165 gtk_text_view_set_attributes_from_style (GtkTextView *text_view,
6166 GtkTextAttributes *values,
6169 values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
6170 values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
6173 pango_font_description_free (values->font);
6175 values->font = pango_font_description_copy (style->font_desc);
6179 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
6181 if (text_view->layout)
6183 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
6184 GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
6185 GtkTextDirection new_cursor_dir;
6186 GtkTextDirection new_keyboard_dir;
6187 gboolean split_cursor;
6189 g_object_get (settings,
6190 "gtk-split-cursor", &split_cursor,
6193 if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
6194 new_keyboard_dir = GTK_TEXT_DIR_RTL;
6196 new_keyboard_dir = GTK_TEXT_DIR_LTR;
6199 new_cursor_dir = GTK_TEXT_DIR_NONE;
6201 new_cursor_dir = new_keyboard_dir;
6203 gtk_text_layout_set_cursor_direction (text_view->layout, new_cursor_dir);
6204 gtk_text_layout_set_keyboard_direction (text_view->layout, new_keyboard_dir);
6209 gtk_text_view_ensure_layout (GtkTextView *text_view)
6213 widget = GTK_WIDGET (text_view);
6215 if (text_view->layout == NULL)
6217 GtkTextAttributes *style;
6218 PangoContext *ltr_context, *rtl_context;
6221 DV(g_print(G_STRLOC"\n"));
6223 text_view->layout = gtk_text_layout_new ();
6225 g_signal_connect (text_view->layout,
6227 G_CALLBACK (invalidated_handler),
6230 g_signal_connect (text_view->layout,
6232 G_CALLBACK (changed_handler),
6235 g_signal_connect (text_view->layout,
6237 G_CALLBACK (gtk_text_view_child_allocated),
6240 if (get_buffer (text_view))
6241 gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
6243 if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
6244 gtk_text_view_pend_cursor_blink (text_view);
6246 gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
6248 gtk_text_layout_set_overwrite_mode (text_view->layout,
6249 text_view->overwrite_mode && text_view->editable);
6251 ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6252 pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
6253 rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
6254 pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
6256 gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
6258 g_object_unref (ltr_context);
6259 g_object_unref (rtl_context);
6261 gtk_text_view_check_keymap_direction (text_view);
6263 style = gtk_text_attributes_new ();
6265 gtk_widget_ensure_style (widget);
6266 gtk_text_view_set_attributes_from_style (text_view,
6267 style, widget->style);
6269 style->pixels_above_lines = text_view->pixels_above_lines;
6270 style->pixels_below_lines = text_view->pixels_below_lines;
6271 style->pixels_inside_wrap = text_view->pixels_inside_wrap;
6272 style->left_margin = text_view->left_margin;
6273 style->right_margin = text_view->right_margin;
6274 style->indent = text_view->indent;
6275 style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
6277 style->wrap_mode = text_view->wrap_mode;
6278 style->justification = text_view->justify;
6279 style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6281 gtk_text_layout_set_default_style (text_view->layout, style);
6283 gtk_text_attributes_unref (style);
6285 /* Set layout for all anchored children */
6287 tmp_list = text_view->children;
6288 while (tmp_list != NULL)
6290 GtkTextViewChild *vc = tmp_list->data;
6294 gtk_text_anchored_child_set_layout (vc->widget,
6296 /* vc may now be invalid! */
6299 tmp_list = g_slist_next (tmp_list);
6302 gtk_text_view_invalidate (text_view);
6307 * gtk_text_view_get_default_attributes:
6308 * @text_view: a #GtkTextView
6310 * Obtains a copy of the default text attributes. These are the
6311 * attributes used for text unless a tag overrides them.
6312 * You'd typically pass the default attributes in to
6313 * gtk_text_iter_get_attributes() in order to get the
6314 * attributes in effect at a given text position.
6316 * The return value is a copy owned by the caller of this function,
6317 * and should be freed.
6319 * Return value: a new #GtkTextAttributes
6322 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6324 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6326 gtk_text_view_ensure_layout (text_view);
6328 return gtk_text_attributes_copy (text_view->layout->default_style);
6332 gtk_text_view_destroy_layout (GtkTextView *text_view)
6334 if (text_view->layout)
6338 gtk_text_view_remove_validate_idles (text_view);
6340 g_signal_handlers_disconnect_by_func (text_view->layout,
6341 invalidated_handler,
6343 g_signal_handlers_disconnect_by_func (text_view->layout,
6347 /* Remove layout from all anchored children */
6348 tmp_list = text_view->children;
6349 while (tmp_list != NULL)
6351 GtkTextViewChild *vc = tmp_list->data;
6355 gtk_text_anchored_child_set_layout (vc->widget, NULL);
6356 /* vc may now be invalid! */
6359 tmp_list = g_slist_next (tmp_list);
6362 gtk_text_view_stop_cursor_blink (text_view);
6363 gtk_text_view_end_selection_drag (text_view);
6365 g_object_unref (text_view->layout);
6366 text_view->layout = NULL;
6371 gtk_text_view_reset_im_context (GtkTextView *text_view)
6373 if (text_view->need_im_reset)
6375 text_view->need_im_reset = FALSE;
6376 gtk_im_context_reset (text_view->im_context);
6385 drag_begin_cb (GtkWidget *widget,
6386 GdkDragContext *context,
6389 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6390 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6393 GdkPixmap *pixmap = NULL;
6395 g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6397 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6398 pixmap = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6402 gtk_drag_set_icon_pixmap (context,
6403 gdk_drawable_get_colormap (pixmap),
6407 g_object_unref (pixmap);
6411 gtk_drag_set_icon_default (context);
6416 gtk_text_view_start_selection_dnd (GtkTextView *text_view,
6417 const GtkTextIter *iter,
6418 GdkEventMotion *event)
6420 GtkTargetList *target_list;
6422 text_view->drag_start_x = -1;
6423 text_view->drag_start_y = -1;
6424 text_view->pending_place_cursor_button = 0;
6426 target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6428 g_signal_connect (text_view, "drag_begin",
6429 G_CALLBACK (drag_begin_cb), NULL);
6430 gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6431 GDK_ACTION_COPY | GDK_ACTION_MOVE,
6432 1, (GdkEvent*)event);
6436 gtk_text_view_drag_begin (GtkWidget *widget,
6437 GdkDragContext *context)
6443 gtk_text_view_drag_end (GtkWidget *widget,
6444 GdkDragContext *context)
6449 gtk_text_view_drag_data_get (GtkWidget *widget,
6450 GdkDragContext *context,
6451 GtkSelectionData *selection_data,
6455 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6456 GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6458 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6460 gtk_selection_data_set (selection_data,
6461 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6466 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6473 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6475 /* Extract the selected text */
6476 str = gtk_text_buffer_serialize (buffer, buffer,
6477 selection_data->target,
6484 gtk_selection_data_set (selection_data,
6485 selection_data->target,
6487 (guchar *) str, len);
6497 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6499 /* Extract the selected text */
6500 str = gtk_text_iter_get_visible_text (&start, &end);
6505 gtk_selection_data_set_text (selection_data, str, -1);
6512 gtk_text_view_drag_data_delete (GtkWidget *widget,
6513 GdkDragContext *context)
6515 gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
6516 TRUE, GTK_TEXT_VIEW (widget)->editable);
6520 gtk_text_view_drag_leave (GtkWidget *widget,
6521 GdkDragContext *context,
6524 GtkTextView *text_view;
6526 text_view = GTK_TEXT_VIEW (widget);
6528 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6530 if (text_view->scroll_timeout != 0)
6531 g_source_remove (text_view->scroll_timeout);
6533 text_view->scroll_timeout = 0;
6537 gtk_text_view_drag_motion (GtkWidget *widget,
6538 GdkDragContext *context,
6543 GtkTextIter newplace;
6544 GtkTextView *text_view;
6547 GdkRectangle target_rect;
6550 GdkDragAction suggested_action = 0;
6552 text_view = GTK_TEXT_VIEW (widget);
6554 target_rect = text_view->text_window->allocation;
6556 if (x < target_rect.x ||
6557 y < target_rect.y ||
6558 x > (target_rect.x + target_rect.width) ||
6559 y > (target_rect.y + target_rect.height))
6560 return FALSE; /* outside the text window, allow parent widgets to handle event */
6562 gtk_text_view_window_to_buffer_coords (text_view,
6563 GTK_TEXT_WINDOW_WIDGET,
6567 gtk_text_layout_get_iter_at_pixel (text_view->layout,
6571 target = gtk_drag_dest_find_target (widget, context,
6572 gtk_drag_dest_get_target_list (widget));
6574 if (target == GDK_NONE)
6576 /* can't accept any of the offered targets */
6578 else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6580 gtk_text_iter_compare (&newplace, &start) >= 0 &&
6581 gtk_text_iter_compare (&newplace, &end) <= 0)
6583 /* We're inside the selection. */
6587 if (gtk_text_iter_can_insert (&newplace, text_view->editable))
6589 GtkWidget *source_widget;
6591 suggested_action = context->suggested_action;
6593 source_widget = gtk_drag_get_source_widget (context);
6595 if (source_widget == widget)
6597 /* Default to MOVE, unless the user has
6598 * pressed ctrl or alt to affect available actions
6600 if ((context->actions & GDK_ACTION_MOVE) != 0)
6601 suggested_action = GDK_ACTION_MOVE;
6606 /* Can't drop here. */
6610 if (suggested_action != 0)
6612 gtk_text_mark_set_visible (text_view->dnd_mark,
6613 text_view->cursor_visible);
6615 gdk_drag_status (context, suggested_action, time);
6619 gdk_drag_status (context, 0, time);
6620 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6623 if (!text_view->scroll_timeout)
6624 text_view->scroll_timeout =
6625 gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
6627 /* TRUE return means don't propagate the drag motion to parent
6628 * widgets that may also be drop sites.
6634 gtk_text_view_drag_drop (GtkWidget *widget,
6635 GdkDragContext *context,
6640 GtkTextView *text_view;
6641 GtkTextIter drop_point;
6642 GdkAtom target = GDK_NONE;
6644 text_view = GTK_TEXT_VIEW (widget);
6646 if (text_view->scroll_timeout != 0)
6647 g_source_remove (text_view->scroll_timeout);
6649 text_view->scroll_timeout = 0;
6651 gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6653 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6655 text_view->dnd_mark);
6657 if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
6658 target = gtk_drag_dest_find_target (widget, context, NULL);
6660 if (target != GDK_NONE)
6661 gtk_drag_get_data (widget, context, target, time);
6663 gtk_drag_finish (context, FALSE, FALSE, time);
6669 insert_text_data (GtkTextView *text_view,
6670 GtkTextIter *drop_point,
6671 GtkSelectionData *selection_data)
6675 str = gtk_selection_data_get_text (selection_data);
6679 if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
6680 drop_point, (gchar *) str, -1,
6681 text_view->editable))
6683 gtk_widget_error_bell (GTK_WIDGET (text_view));
6691 gtk_text_view_drag_data_received (GtkWidget *widget,
6692 GdkDragContext *context,
6695 GtkSelectionData *selection_data,
6699 GtkTextIter drop_point;
6700 GtkTextView *text_view;
6701 gboolean success = FALSE;
6702 GtkTextBuffer *buffer = NULL;
6704 text_view = GTK_TEXT_VIEW (widget);
6706 if (!text_view->dnd_mark)
6709 buffer = get_buffer (text_view);
6711 gtk_text_buffer_get_iter_at_mark (buffer,
6713 text_view->dnd_mark);
6715 if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
6720 gtk_text_buffer_begin_user_action (buffer);
6722 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6724 GtkTextBuffer *src_buffer = NULL;
6725 GtkTextIter start, end;
6726 gboolean copy_tags = TRUE;
6728 if (selection_data->length != sizeof (src_buffer))
6731 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
6733 if (src_buffer == NULL)
6736 g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
6738 if (gtk_text_buffer_get_tag_table (src_buffer) !=
6739 gtk_text_buffer_get_tag_table (buffer))
6741 /* try to find a suitable rich text target instead */
6745 GdkAtom target = GDK_NONE;
6749 atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
6751 for (list = context->targets; list; list = g_list_next (list))
6755 for (i = 0; i < n_atoms; i++)
6756 if (GUINT_TO_POINTER (atoms[i]) == list->data)
6765 if (target != GDK_NONE)
6767 gtk_drag_get_data (widget, context, target, time);
6768 gtk_text_buffer_end_user_action (buffer);
6773 if (gtk_text_buffer_get_selection_bounds (src_buffer,
6778 gtk_text_buffer_insert_range_interactive (buffer,
6782 text_view->editable);
6787 str = gtk_text_iter_get_visible_text (&start, &end);
6788 gtk_text_buffer_insert_interactive (buffer,
6789 &drop_point, str, -1,
6790 text_view->editable);
6795 else if (selection_data->length > 0 &&
6796 info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6799 GError *error = NULL;
6801 retval = gtk_text_buffer_deserialize (buffer, buffer,
6802 selection_data->target,
6804 (guint8 *) selection_data->data,
6805 selection_data->length,
6810 g_warning ("error pasting: %s\n", error->message);
6811 g_clear_error (&error);
6815 insert_text_data (text_view, &drop_point, selection_data);
6818 gtk_drag_finish (context, success,
6819 success && context->action == GDK_ACTION_MOVE,
6824 gtk_text_buffer_get_iter_at_mark (buffer,
6826 text_view->dnd_mark);
6827 gtk_text_buffer_place_cursor (buffer, &drop_point);
6829 gtk_text_buffer_end_user_action (buffer);
6833 static GtkAdjustment*
6834 get_hadjustment (GtkTextView *text_view)
6836 if (text_view->hadjustment == NULL)
6837 gtk_text_view_set_scroll_adjustments (text_view,
6838 NULL, /* forces creation */
6839 text_view->vadjustment);
6841 return text_view->hadjustment;
6844 static GtkAdjustment*
6845 get_vadjustment (GtkTextView *text_view)
6847 if (text_view->vadjustment == NULL)
6848 gtk_text_view_set_scroll_adjustments (text_view,
6849 text_view->hadjustment,
6850 NULL); /* forces creation */
6851 return text_view->vadjustment;
6856 gtk_text_view_set_scroll_adjustments (GtkTextView *text_view,
6857 GtkAdjustment *hadj,
6858 GtkAdjustment *vadj)
6860 gboolean need_adjust = FALSE;
6863 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
6865 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6867 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
6869 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6871 if (text_view->hadjustment && (text_view->hadjustment != hadj))
6873 g_signal_handlers_disconnect_by_func (text_view->hadjustment,
6874 gtk_text_view_value_changed,
6876 g_object_unref (text_view->hadjustment);
6879 if (text_view->vadjustment && (text_view->vadjustment != vadj))
6881 g_signal_handlers_disconnect_by_func (text_view->vadjustment,
6882 gtk_text_view_value_changed,
6884 g_object_unref (text_view->vadjustment);
6887 if (text_view->hadjustment != hadj)
6889 text_view->hadjustment = hadj;
6890 g_object_ref_sink (text_view->hadjustment);
6892 g_signal_connect (text_view->hadjustment, "value_changed",
6893 G_CALLBACK (gtk_text_view_value_changed),
6898 if (text_view->vadjustment != vadj)
6900 text_view->vadjustment = vadj;
6901 g_object_ref_sink (text_view->vadjustment);
6903 g_signal_connect (text_view->vadjustment, "value_changed",
6904 G_CALLBACK (gtk_text_view_value_changed),
6910 gtk_text_view_value_changed (NULL, text_view);
6913 /* FIXME this adjust_allocation is a big cut-and-paste from
6914 * GtkCList, needs to be some "official" way to do this
6924 /* The window to which widget->window is relative */
6925 #define ALLOCATION_WINDOW(widget) \
6926 (GTK_WIDGET_NO_WINDOW (widget) ? \
6927 (widget)->window : \
6928 gdk_window_get_parent ((widget)->window))
6931 adjust_allocation_recurse (GtkWidget *widget,
6934 ScrollData *scroll_data = data;
6936 /* Need to really size allocate instead of just poking
6937 * into widget->allocation if the widget is not realized.
6938 * FIXME someone figure out why this was.
6940 if (!GTK_WIDGET_REALIZED (widget))
6942 if (GTK_WIDGET_VISIBLE (widget))
6944 GdkRectangle tmp_rectangle = widget->allocation;
6945 tmp_rectangle.x += scroll_data->dx;
6946 tmp_rectangle.y += scroll_data->dy;
6948 gtk_widget_size_allocate (widget, &tmp_rectangle);
6953 if (ALLOCATION_WINDOW (widget) == scroll_data->window)
6955 widget->allocation.x += scroll_data->dx;
6956 widget->allocation.y += scroll_data->dy;
6958 if (GTK_IS_CONTAINER (widget))
6959 gtk_container_forall (GTK_CONTAINER (widget),
6960 adjust_allocation_recurse,
6967 adjust_allocation (GtkWidget *widget,
6971 ScrollData scroll_data;
6973 if (GTK_WIDGET_REALIZED (widget))
6974 scroll_data.window = ALLOCATION_WINDOW (widget);
6976 scroll_data.window = NULL;
6978 scroll_data.dx = dx;
6979 scroll_data.dy = dy;
6981 adjust_allocation_recurse (widget, &scroll_data);
6985 gtk_text_view_value_changed (GtkAdjustment *adj,
6986 GtkTextView *text_view)
6993 /* Note that we oddly call this function with adj == NULL
6997 text_view->onscreen_validated = FALSE;
6999 DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
7000 adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
7001 adj ? adj->value : 0.0));
7003 if (adj == text_view->hadjustment)
7005 dx = text_view->xoffset - (gint)adj->value;
7006 text_view->xoffset = adj->value;
7008 /* If the change is due to a size change we need
7009 * to invalidate the entire text window because there might be
7010 * right-aligned or centered text
7012 if (text_view->width_changed)
7014 if (GTK_WIDGET_REALIZED (text_view))
7015 gdk_window_invalidate_rect (text_view->text_window->bin_window, NULL, FALSE);
7017 text_view->width_changed = FALSE;
7020 else if (adj == text_view->vadjustment)
7022 dy = text_view->yoffset - (gint)adj->value;
7023 text_view->yoffset = adj->value;
7025 if (text_view->layout)
7027 gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
7029 gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
7031 text_view->first_para_pixels = adj->value - line_top;
7035 if (dx != 0 || dy != 0)
7039 if (GTK_WIDGET_REALIZED (text_view))
7043 if (text_view->left_window)
7044 text_window_scroll (text_view->left_window, 0, dy);
7045 if (text_view->right_window)
7046 text_window_scroll (text_view->right_window, 0, dy);
7051 if (text_view->top_window)
7052 text_window_scroll (text_view->top_window, dx, 0);
7053 if (text_view->bottom_window)
7054 text_window_scroll (text_view->bottom_window, dx, 0);
7057 /* It looks nicer to scroll the main area last, because
7058 * it takes a while, and making the side areas update
7059 * afterward emphasizes the slowness of scrolling the
7062 text_window_scroll (text_view->text_window, dx, dy);
7065 /* Children are now "moved" in the text window, poke
7066 * into widget->allocation for each child
7068 tmp_list = text_view->children;
7069 while (tmp_list != NULL)
7071 GtkTextViewChild *child = tmp_list->data;
7074 adjust_allocation (child->widget, dx, dy);
7076 tmp_list = g_slist_next (tmp_list);
7080 /* This could result in invalidation, which would install the
7081 * first_validate_idle, which would validate onscreen;
7082 * but we're going to go ahead and validate here, so
7083 * first_validate_idle shouldn't have anything to do.
7085 gtk_text_view_update_layout_width (text_view);
7087 /* note that validation of onscreen could invoke this function
7088 * recursively, by scrolling to maintain first_para, or in response
7089 * to updating the layout width, however there is no problem with
7090 * that, or shouldn't be.
7092 gtk_text_view_validate_onscreen (text_view);
7094 /* process exposes */
7095 if (GTK_WIDGET_REALIZED (text_view))
7097 DV (g_print ("Processing updates (%s)\n", G_STRLOC));
7099 if (text_view->left_window)
7100 gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
7102 if (text_view->right_window)
7103 gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
7105 if (text_view->top_window)
7106 gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
7108 if (text_view->bottom_window)
7109 gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
7111 gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
7114 /* If this got installed, get rid of it, it's just a waste of time. */
7115 if (text_view->first_validate_idle != 0)
7117 g_source_remove (text_view->first_validate_idle);
7118 text_view->first_validate_idle = 0;
7121 gtk_text_view_update_im_spot_location (text_view);
7123 DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
7127 gtk_text_view_commit_handler (GtkIMContext *context,
7129 GtkTextView *text_view)
7131 gtk_text_view_commit_text (text_view, str);
7135 gtk_text_view_commit_text (GtkTextView *text_view,
7138 gboolean had_selection;
7140 gtk_text_buffer_begin_user_action (get_buffer (text_view));
7142 had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7145 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7146 text_view->editable);
7148 if (!strcmp (str, "\n"))
7150 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
7151 text_view->editable))
7153 gtk_widget_error_bell (GTK_WIDGET (text_view));
7158 if (!had_selection && text_view->overwrite_mode)
7162 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7164 gtk_text_buffer_get_insert (get_buffer (text_view)));
7165 if (!gtk_text_iter_ends_line (&insert))
7166 gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
7169 if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
7170 text_view->editable))
7172 gtk_widget_error_bell (GTK_WIDGET (text_view));
7176 gtk_text_buffer_end_user_action (get_buffer (text_view));
7178 gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
7179 DV(g_print (G_STRLOC": scrolling onscreen\n"));
7180 gtk_text_view_scroll_mark_onscreen (text_view,
7181 gtk_text_buffer_get_insert (get_buffer (text_view)));
7185 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
7186 GtkTextView *text_view)
7189 PangoAttrList *attrs;
7193 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7194 gtk_text_buffer_get_insert (text_view->buffer));
7196 /* Keypress events are passed to input method even if cursor position is not editable;
7197 * so beep here if it's multi-key input sequence, input method will be reset in
7198 * key-press-event handler. */
7199 if (!gtk_text_iter_can_insert (&iter, text_view->editable))
7201 gtk_widget_error_bell (GTK_WIDGET (text_view));
7205 gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
7206 gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
7207 pango_attr_list_unref (attrs);
7210 if (GTK_WIDGET_HAS_FOCUS (text_view))
7211 gtk_text_view_scroll_mark_onscreen (text_view,
7212 gtk_text_buffer_get_insert (get_buffer (text_view)));
7216 gtk_text_view_retrieve_surrounding_handler (GtkIMContext *context,
7217 GtkTextView *text_view)
7224 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7225 gtk_text_buffer_get_insert (text_view->buffer));
7228 pos = gtk_text_iter_get_line_index (&start);
7229 gtk_text_iter_set_line_offset (&start, 0);
7230 gtk_text_iter_forward_to_line_end (&end);
7232 text = gtk_text_iter_get_slice (&start, &end);
7233 gtk_im_context_set_surrounding (context, text, -1, pos);
7240 gtk_text_view_delete_surrounding_handler (GtkIMContext *context,
7243 GtkTextView *text_view)
7248 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,
7249 gtk_text_buffer_get_insert (text_view->buffer));
7252 gtk_text_iter_forward_chars (&start, offset);
7253 gtk_text_iter_forward_chars (&end, offset + n_chars);
7255 gtk_text_buffer_delete_interactive (text_view->buffer, &start, &end,
7256 text_view->editable);
7262 gtk_text_view_mark_set_handler (GtkTextBuffer *buffer,
7263 const GtkTextIter *location,
7267 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7268 gboolean need_reset = FALSE;
7270 if (mark == gtk_text_buffer_get_insert (buffer))
7272 text_view->virtual_cursor_x = -1;
7273 text_view->virtual_cursor_y = -1;
7274 gtk_text_view_update_im_spot_location (text_view);
7277 else if (mark == gtk_text_buffer_get_selection_bound (buffer))
7283 gtk_text_view_reset_im_context (text_view);
7287 gtk_text_view_target_list_notify (GtkTextBuffer *buffer,
7288 const GParamSpec *pspec,
7291 GtkWidget *widget = GTK_WIDGET (data);
7292 GtkTargetList *view_list;
7293 GtkTargetList *buffer_list;
7296 view_list = gtk_drag_dest_get_target_list (widget);
7297 buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7300 gtk_target_list_ref (view_list);
7302 view_list = gtk_target_list_new (NULL, 0);
7304 list = view_list->list;
7307 GtkTargetPair *pair = list->data;
7309 list = g_list_next (list); /* get next element before removing */
7311 if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7312 pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7314 gtk_target_list_remove (view_list, pair->target);
7318 for (list = buffer_list->list; list; list = g_list_next (list))
7320 GtkTargetPair *pair = list->data;
7322 gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7325 gtk_drag_dest_set_target_list (widget, view_list);
7326 gtk_target_list_unref (view_list);
7330 gtk_text_view_get_cursor_location (GtkTextView *text_view,
7335 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7336 gtk_text_buffer_get_insert (get_buffer (text_view)));
7338 gtk_text_layout_get_cursor_locations (text_view->layout, &insert, pos, NULL);
7342 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7348 if ((x && text_view->virtual_cursor_x == -1) ||
7349 (y && text_view->virtual_cursor_y == -1))
7350 gtk_text_view_get_cursor_location (text_view, &pos);
7354 if (text_view->virtual_cursor_x != -1)
7355 *x = text_view->virtual_cursor_x;
7362 if (text_view->virtual_cursor_x != -1)
7363 *y = text_view->virtual_cursor_y;
7365 *y = pos.y + pos.height / 2;
7370 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7376 if (!text_view->layout)
7379 if (x == -1 || y == -1)
7380 gtk_text_view_get_cursor_location (text_view, &pos);
7382 text_view->virtual_cursor_x = (x == -1) ? pos.x : x;
7383 text_view->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7386 /* Quick hack of a popup menu
7389 activate_cb (GtkWidget *menuitem,
7390 GtkTextView *text_view)
7392 const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7393 g_signal_emit_by_name (text_view, signal);
7397 append_action_signal (GtkTextView *text_view,
7399 const gchar *stock_id,
7400 const gchar *signal,
7403 GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7405 g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7406 g_signal_connect (menuitem, "activate",
7407 G_CALLBACK (activate_cb), text_view);
7409 gtk_widget_set_sensitive (menuitem, sensitive);
7411 gtk_widget_show (menuitem);
7412 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7416 gtk_text_view_select_all (GtkWidget *widget,
7419 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
7420 GtkTextBuffer *buffer;
7421 GtkTextIter start_iter, end_iter, insert;
7423 buffer = text_view->buffer;
7426 gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
7427 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
7431 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
7432 gtk_text_buffer_get_insert (buffer));
7433 gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
7438 select_all_cb (GtkWidget *menuitem,
7439 GtkTextView *text_view)
7441 gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
7445 delete_cb (GtkTextView *text_view)
7447 gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7448 text_view->editable);
7452 popup_menu_detach (GtkWidget *attach_widget,
7455 GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
7459 popup_position_func (GtkMenu *menu,
7465 GtkTextView *text_view;
7467 GdkRectangle cursor_rect;
7468 GdkRectangle onscreen_rect;
7469 gint root_x, root_y;
7474 GdkRectangle monitor;
7476 text_view = GTK_TEXT_VIEW (user_data);
7477 widget = GTK_WIDGET (text_view);
7479 g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
7481 screen = gtk_widget_get_screen (widget);
7483 gdk_window_get_origin (widget->window, &root_x, &root_y);
7485 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7487 gtk_text_buffer_get_insert (get_buffer (text_view)));
7489 gtk_text_view_get_iter_location (text_view,
7493 gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
7495 gtk_widget_size_request (text_view->popup_menu, &req);
7497 /* can't use rectangle_intersect since cursor rect can have 0 width */
7498 if (cursor_rect.x >= onscreen_rect.x &&
7499 cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
7500 cursor_rect.y >= onscreen_rect.y &&
7501 cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
7503 gtk_text_view_buffer_to_window_coords (text_view,
7504 GTK_TEXT_WINDOW_WIDGET,
7505 cursor_rect.x, cursor_rect.y,
7506 &cursor_rect.x, &cursor_rect.y);
7508 *x = root_x + cursor_rect.x + cursor_rect.width;
7509 *y = root_y + cursor_rect.y + cursor_rect.height;
7513 /* Just center the menu, since cursor is offscreen. */
7514 *x = root_x + (widget->allocation.width / 2 - req.width / 2);
7515 *y = root_y + (widget->allocation.height / 2 - req.height / 2);
7519 *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
7520 *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
7522 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
7523 gtk_menu_set_monitor (menu, monitor_num);
7524 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
7526 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
7527 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
7534 GtkTextView *text_view;
7540 range_contains_editable_text (const GtkTextIter *start,
7541 const GtkTextIter *end,
7542 gboolean default_editability)
7544 GtkTextIter iter = *start;
7546 while (gtk_text_iter_compare (&iter, end) < 0)
7548 if (gtk_text_iter_editable (&iter, default_editability))
7551 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
7558 unichar_chosen_func (const char *text,
7561 GtkTextView *text_view = GTK_TEXT_VIEW (data);
7563 gtk_text_view_commit_text (text_view, text);
7567 popup_targets_received (GtkClipboard *clipboard,
7568 GtkSelectionData *data,
7571 PopupInfo *info = user_data;
7572 GtkTextView *text_view = info->text_view;
7574 if (GTK_WIDGET_REALIZED (text_view))
7576 /* We implicitely rely here on the fact that if we are pasting ourself, we'll
7577 * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
7579 gboolean clipboard_contains_text;
7580 GtkWidget *menuitem;
7582 gboolean have_selection;
7583 gboolean can_insert;
7585 GtkTextIter sel_start, sel_end;
7586 gboolean show_input_method_menu;
7587 gboolean show_unicode_menu;
7589 clipboard_contains_text = gtk_selection_data_targets_include_text (data);
7591 if (text_view->popup_menu)
7592 gtk_widget_destroy (text_view->popup_menu);
7594 text_view->popup_menu = gtk_menu_new ();
7596 gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
7597 GTK_WIDGET (text_view),
7600 have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7601 &sel_start, &sel_end);
7603 gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7605 gtk_text_buffer_get_insert (get_buffer (text_view)));
7607 can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
7609 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
7611 range_contains_editable_text (&sel_start, &sel_end,
7612 text_view->editable));
7613 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
7615 append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
7616 can_insert && clipboard_contains_text);
7618 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
7619 gtk_widget_set_sensitive (menuitem,
7621 range_contains_editable_text (&sel_start, &sel_end,
7622 text_view->editable));
7623 g_signal_connect_swapped (menuitem, "activate",
7624 G_CALLBACK (delete_cb), text_view);
7625 gtk_widget_show (menuitem);
7626 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7628 menuitem = gtk_separator_menu_item_new ();
7629 gtk_widget_show (menuitem);
7630 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7632 menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
7633 g_signal_connect (menuitem, "activate",
7634 G_CALLBACK (select_all_cb), text_view);
7635 gtk_widget_show (menuitem);
7636 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7638 g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
7639 "gtk-show-input-method-menu", &show_input_method_menu,
7640 "gtk-show-unicode-menu", &show_unicode_menu,
7643 if (show_input_method_menu || show_unicode_menu)
7645 menuitem = gtk_separator_menu_item_new ();
7646 gtk_widget_show (menuitem);
7647 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7650 if (show_input_method_menu)
7652 menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
7653 gtk_widget_show (menuitem);
7654 gtk_widget_set_sensitive (menuitem, can_insert);
7656 submenu = gtk_menu_new ();
7657 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7658 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7660 gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
7661 GTK_MENU_SHELL (submenu));
7664 if (show_unicode_menu)
7666 menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
7667 gtk_widget_show (menuitem);
7668 gtk_widget_set_sensitive (menuitem, can_insert);
7670 submenu = gtk_menu_new ();
7671 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7672 gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7674 _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
7675 unichar_chosen_func,
7679 g_signal_emit (text_view,
7680 signals[POPULATE_POPUP],
7682 text_view->popup_menu);
7685 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7687 info->button, info->time);
7690 gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7691 popup_position_func, text_view,
7692 0, gtk_get_current_event_time ());
7693 gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
7697 g_object_unref (text_view);
7702 gtk_text_view_do_popup (GtkTextView *text_view,
7703 GdkEventButton *event)
7705 PopupInfo *info = g_new (PopupInfo, 1);
7707 /* In order to know what entries we should make sensitive, we
7708 * ask for the current targets of the clipboard, and when
7709 * we get them, then we actually pop up the menu.
7711 info->text_view = g_object_ref (text_view);
7715 info->button = event->button;
7716 info->time = event->time;
7721 info->time = gtk_get_current_event_time ();
7724 gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
7725 GDK_SELECTION_CLIPBOARD),
7726 gdk_atom_intern_static_string ("TARGETS"),
7727 popup_targets_received,
7732 gtk_text_view_popup_menu (GtkWidget *widget)
7734 gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
7738 /* Child GdkWindows */
7741 static GtkTextWindow*
7742 text_window_new (GtkTextWindowType type,
7745 gint height_request)
7749 win = g_new (GtkTextWindow, 1);
7752 win->widget = widget;
7754 win->bin_window = NULL;
7755 win->requisition.width = width_request;
7756 win->requisition.height = height_request;
7757 win->allocation.width = width_request;
7758 win->allocation.height = height_request;
7759 win->allocation.x = 0;
7760 win->allocation.y = 0;
7766 text_window_free (GtkTextWindow *win)
7769 text_window_unrealize (win);
7775 text_window_realize (GtkTextWindow *win,
7778 GdkWindowAttr attributes;
7779 gint attributes_mask;
7782 attributes.window_type = GDK_WINDOW_CHILD;
7783 attributes.x = win->allocation.x;
7784 attributes.y = win->allocation.y;
7785 attributes.width = win->allocation.width;
7786 attributes.height = win->allocation.height;
7787 attributes.wclass = GDK_INPUT_OUTPUT;
7788 attributes.visual = gtk_widget_get_visual (win->widget);
7789 attributes.colormap = gtk_widget_get_colormap (win->widget);
7790 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
7792 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
7794 win->window = gdk_window_new (widget->window,
7798 gdk_window_set_back_pixmap (win->window, NULL, FALSE);
7800 gdk_window_show (win->window);
7801 gdk_window_set_user_data (win->window, win->widget);
7802 gdk_window_lower (win->window);
7806 attributes.width = win->allocation.width;
7807 attributes.height = win->allocation.height;
7808 attributes.event_mask = (GDK_EXPOSURE_MASK |
7810 GDK_KEY_PRESS_MASK |
7811 GDK_BUTTON_PRESS_MASK |
7812 GDK_BUTTON_RELEASE_MASK |
7813 GDK_POINTER_MOTION_MASK |
7814 GDK_POINTER_MOTION_HINT_MASK |
7815 gtk_widget_get_events (win->widget));
7817 win->bin_window = gdk_window_new (win->window,
7821 gdk_window_show (win->bin_window);
7822 gdk_window_set_user_data (win->bin_window, win->widget);
7824 if (win->type == GTK_TEXT_WINDOW_TEXT)
7826 if (GTK_WIDGET_IS_SENSITIVE (widget))
7829 cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (widget->window),
7831 gdk_window_set_cursor (win->bin_window, cursor);
7832 gdk_cursor_unref (cursor);
7835 gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->im_context,
7839 gdk_window_set_background (win->bin_window,
7840 &widget->style->base[GTK_WIDGET_STATE (widget)]);
7844 gdk_window_set_background (win->bin_window,
7845 &widget->style->bg[GTK_WIDGET_STATE (widget)]);
7848 g_object_set_qdata (G_OBJECT (win->window),
7849 g_quark_from_static_string ("gtk-text-view-text-window"),
7852 g_object_set_qdata (G_OBJECT (win->bin_window),
7853 g_quark_from_static_string ("gtk-text-view-text-window"),
7858 text_window_unrealize (GtkTextWindow *win)
7860 if (win->type == GTK_TEXT_WINDOW_TEXT)
7862 gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
7866 gdk_window_set_user_data (win->window, NULL);
7867 gdk_window_set_user_data (win->bin_window, NULL);
7868 gdk_window_destroy (win->bin_window);
7869 gdk_window_destroy (win->window);
7871 win->bin_window = NULL;
7875 text_window_size_allocate (GtkTextWindow *win,
7878 win->allocation = *rect;
7882 gdk_window_move_resize (win->window,
7884 rect->width, rect->height);
7886 gdk_window_resize (win->bin_window,
7887 rect->width, rect->height);
7892 text_window_scroll (GtkTextWindow *win,
7896 if (dx != 0 || dy != 0)
7898 gdk_window_scroll (win->bin_window, dx, dy);
7903 text_window_invalidate_rect (GtkTextWindow *win,
7906 GdkRectangle window_rect;
7908 gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
7915 window_rect.width = rect->width;
7916 window_rect.height = rect->height;
7918 /* Adjust the rect as appropriate */
7922 case GTK_TEXT_WINDOW_TEXT:
7925 case GTK_TEXT_WINDOW_LEFT:
7926 case GTK_TEXT_WINDOW_RIGHT:
7928 window_rect.width = win->allocation.width;
7931 case GTK_TEXT_WINDOW_TOP:
7932 case GTK_TEXT_WINDOW_BOTTOM:
7934 window_rect.height = win->allocation.height;
7938 g_warning ("%s: bug!", G_STRFUNC);
7943 gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
7947 cairo_t *cr = gdk_cairo_create (win->bin_window);
7948 gdk_cairo_rectangle (cr, &window_rect);
7949 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); /* red */
7957 text_window_invalidate_cursors (GtkTextWindow *win)
7959 GtkTextView *text_view = GTK_TEXT_VIEW (win->widget);
7961 GdkRectangle strong;
7963 gboolean draw_arrow;
7964 gfloat cursor_aspect_ratio;
7968 gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7969 gtk_text_buffer_get_insert (text_view->buffer));
7971 if (_gtk_text_layout_get_block_cursor (text_view->layout, &strong))
7973 text_window_invalidate_rect (win, &strong);
7977 gtk_text_layout_get_cursor_locations (text_view->layout, &iter,
7980 /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
7981 * ignoring the text direction be exposing both sides of the cursor
7984 draw_arrow = (strong.x != weak.x || strong.y != weak.y);
7986 gtk_widget_style_get (win->widget,
7987 "cursor-aspect-ratio", &cursor_aspect_ratio,
7990 stem_width = strong.height * cursor_aspect_ratio + 1;
7991 arrow_width = stem_width + 1;
7993 strong.width = stem_width;
7995 /* round up to the next even number */
7999 strong.x -= stem_width / 2;
8000 strong.width += stem_width;
8004 strong.x -= arrow_width;
8005 strong.width += arrow_width * 2;
8008 text_window_invalidate_rect (win, &strong);
8010 if (draw_arrow) /* == have weak */
8012 stem_width = weak.height * cursor_aspect_ratio + 1;
8013 arrow_width = stem_width + 1;
8015 weak.width = stem_width;
8017 /* round up to the next even number */
8021 weak.x -= stem_width / 2;
8022 weak.width += stem_width;
8024 weak.x -= arrow_width;
8025 weak.width += arrow_width * 2;
8027 text_window_invalidate_rect (win, &weak);
8032 text_window_get_width (GtkTextWindow *win)
8034 return win->allocation.width;
8038 text_window_get_height (GtkTextWindow *win)
8040 return win->allocation.height;
8047 * gtk_text_view_get_window:
8048 * @text_view: a #GtkTextView
8049 * @win: window to get
8051 * Retrieves the #GdkWindow corresponding to an area of the text view;
8052 * possible windows include the overall widget window, child windows
8053 * on the left, right, top, bottom, and the window that displays the
8054 * text buffer. Windows are %NULL and nonexistent if their width or
8055 * height is 0, and are nonexistent before the widget has been
8058 * Return value: a #GdkWindow, or %NULL
8061 gtk_text_view_get_window (GtkTextView *text_view,
8062 GtkTextWindowType win)
8064 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
8068 case GTK_TEXT_WINDOW_WIDGET:
8069 return GTK_WIDGET (text_view)->window;
8072 case GTK_TEXT_WINDOW_TEXT:
8073 return text_view->text_window->bin_window;
8076 case GTK_TEXT_WINDOW_LEFT:
8077 if (text_view->left_window)
8078 return text_view->left_window->bin_window;
8083 case GTK_TEXT_WINDOW_RIGHT:
8084 if (text_view->right_window)
8085 return text_view->right_window->bin_window;
8090 case GTK_TEXT_WINDOW_TOP:
8091 if (text_view->top_window)
8092 return text_view->top_window->bin_window;
8097 case GTK_TEXT_WINDOW_BOTTOM:
8098 if (text_view->bottom_window)
8099 return text_view->bottom_window->bin_window;
8104 case GTK_TEXT_WINDOW_PRIVATE:
8105 g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_STRFUNC);
8110 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8115 * gtk_text_view_get_window_type:
8116 * @text_view: a #GtkTextView
8117 * @window: a window type
8119 * Usually used to find out which window an event corresponds to.
8120 * If you connect to an event signal on @text_view, this function
8121 * should be called on <literal>event->window</literal> to
8122 * see which window it was.
8124 * Return value: the window type.
8127 gtk_text_view_get_window_type (GtkTextView *text_view,
8132 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8133 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
8135 if (window == GTK_WIDGET (text_view)->window)
8136 return GTK_TEXT_WINDOW_WIDGET;
8138 win = g_object_get_qdata (G_OBJECT (window),
8139 g_quark_try_string ("gtk-text-view-text-window"));
8145 return GTK_TEXT_WINDOW_PRIVATE;
8150 buffer_to_widget (GtkTextView *text_view,
8158 *window_x = buffer_x - text_view->xoffset;
8159 *window_x += text_view->text_window->allocation.x;
8164 *window_y = buffer_y - text_view->yoffset;
8165 *window_y += text_view->text_window->allocation.y;
8170 widget_to_text_window (GtkTextWindow *win,
8177 *window_x = widget_x - win->allocation.x;
8180 *window_y = widget_y - win->allocation.y;
8184 buffer_to_text_window (GtkTextView *text_view,
8193 g_warning ("Attempt to convert text buffer coordinates to coordinates "
8194 "for a nonexistent or private child window of GtkTextView");
8198 buffer_to_widget (text_view,
8200 window_x, window_y);
8202 widget_to_text_window (win,
8203 window_x ? *window_x : 0,
8204 window_y ? *window_y : 0,
8210 * gtk_text_view_buffer_to_window_coords:
8211 * @text_view: a #GtkTextView
8212 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8213 * @buffer_x: buffer x coordinate
8214 * @buffer_y: buffer y coordinate
8215 * @window_x: window x coordinate return location
8216 * @window_y: window y coordinate return location
8218 * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
8219 * @win, and stores the result in (@window_x, @window_y).
8221 * Note that you can't convert coordinates for a nonexisting window (see
8222 * gtk_text_view_set_border_window_size()).
8225 gtk_text_view_buffer_to_window_coords (GtkTextView *text_view,
8226 GtkTextWindowType win,
8232 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8236 case GTK_TEXT_WINDOW_WIDGET:
8237 buffer_to_widget (text_view,
8239 window_x, window_y);
8242 case GTK_TEXT_WINDOW_TEXT:
8244 *window_x = buffer_x - text_view->xoffset;
8246 *window_y = buffer_y - text_view->yoffset;
8249 case GTK_TEXT_WINDOW_LEFT:
8250 buffer_to_text_window (text_view,
8251 text_view->left_window,
8253 window_x, window_y);
8256 case GTK_TEXT_WINDOW_RIGHT:
8257 buffer_to_text_window (text_view,
8258 text_view->right_window,
8260 window_x, window_y);
8263 case GTK_TEXT_WINDOW_TOP:
8264 buffer_to_text_window (text_view,
8265 text_view->top_window,
8267 window_x, window_y);
8270 case GTK_TEXT_WINDOW_BOTTOM:
8271 buffer_to_text_window (text_view,
8272 text_view->bottom_window,
8274 window_x, window_y);
8277 case GTK_TEXT_WINDOW_PRIVATE:
8278 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8282 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8288 widget_to_buffer (GtkTextView *text_view,
8296 *buffer_x = widget_x + text_view->xoffset;
8297 *buffer_x -= text_view->text_window->allocation.x;
8302 *buffer_y = widget_y + text_view->yoffset;
8303 *buffer_y -= text_view->text_window->allocation.y;
8308 text_window_to_widget (GtkTextWindow *win,
8315 *widget_x = window_x + win->allocation.x;
8318 *widget_y = window_y + win->allocation.y;
8322 text_window_to_buffer (GtkTextView *text_view,
8331 g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8332 "coordinates for a nonexistent child window.");
8336 text_window_to_widget (win,
8342 widget_to_buffer (text_view,
8343 buffer_x ? *buffer_x : 0,
8344 buffer_y ? *buffer_y : 0,
8350 * gtk_text_view_window_to_buffer_coords:
8351 * @text_view: a #GtkTextView
8352 * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8353 * @window_x: window x coordinate
8354 * @window_y: window y coordinate
8355 * @buffer_x: buffer x coordinate return location
8356 * @buffer_y: buffer y coordinate return location
8358 * Converts coordinates on the window identified by @win to buffer
8359 * coordinates, storing the result in (@buffer_x,@buffer_y).
8361 * Note that you can't convert coordinates for a nonexisting window (see
8362 * gtk_text_view_set_border_window_size()).
8365 gtk_text_view_window_to_buffer_coords (GtkTextView *text_view,
8366 GtkTextWindowType win,
8372 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8376 case GTK_TEXT_WINDOW_WIDGET:
8377 widget_to_buffer (text_view,
8379 buffer_x, buffer_y);
8382 case GTK_TEXT_WINDOW_TEXT:
8384 *buffer_x = window_x + text_view->xoffset;
8386 *buffer_y = window_y + text_view->yoffset;
8389 case GTK_TEXT_WINDOW_LEFT:
8390 text_window_to_buffer (text_view,
8391 text_view->left_window,
8393 buffer_x, buffer_y);
8396 case GTK_TEXT_WINDOW_RIGHT:
8397 text_window_to_buffer (text_view,
8398 text_view->right_window,
8400 buffer_x, buffer_y);
8403 case GTK_TEXT_WINDOW_TOP:
8404 text_window_to_buffer (text_view,
8405 text_view->top_window,
8407 buffer_x, buffer_y);
8410 case GTK_TEXT_WINDOW_BOTTOM:
8411 text_window_to_buffer (text_view,
8412 text_view->bottom_window,
8414 buffer_x, buffer_y);
8417 case GTK_TEXT_WINDOW_PRIVATE:
8418 g_warning ("%s: can't get coords for private windows", G_STRFUNC);
8422 g_warning ("%s: Unknown GtkTextWindowType", G_STRFUNC);
8428 set_window_width (GtkTextView *text_view,
8430 GtkTextWindowType type,
8431 GtkTextWindow **winp)
8437 text_window_free (*winp);
8439 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8446 *winp = text_window_new (type,
8447 GTK_WIDGET (text_view),
8449 /* if the widget is already realized we need to realize the child manually */
8450 if (GTK_WIDGET_REALIZED (text_view))
8451 text_window_realize (*winp, GTK_WIDGET (text_view));
8455 if ((*winp)->requisition.width == width)
8458 (*winp)->requisition.width = width;
8461 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8467 set_window_height (GtkTextView *text_view,
8469 GtkTextWindowType type,
8470 GtkTextWindow **winp)
8476 text_window_free (*winp);
8478 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8485 *winp = text_window_new (type,
8486 GTK_WIDGET (text_view),
8489 /* if the widget is already realized we need to realize the child manually */
8490 if (GTK_WIDGET_REALIZED (text_view))
8491 text_window_realize (*winp, GTK_WIDGET (text_view));
8495 if ((*winp)->requisition.height == height)
8498 (*winp)->requisition.height = height;
8501 gtk_widget_queue_resize (GTK_WIDGET (text_view));
8506 * gtk_text_view_set_border_window_size:
8507 * @text_view: a #GtkTextView
8508 * @type: window to affect
8509 * @size: width or height of the window
8511 * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
8512 * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
8513 * Automatically destroys the corresponding window if the size is set
8514 * to 0, and creates the window if the size is set to non-zero. This
8515 * function can only be used for the "border windows," it doesn't work
8516 * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
8517 * #GTK_TEXT_WINDOW_PRIVATE.
8520 gtk_text_view_set_border_window_size (GtkTextView *text_view,
8521 GtkTextWindowType type,
8525 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8526 g_return_if_fail (size >= 0);
8530 case GTK_TEXT_WINDOW_LEFT:
8531 set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
8532 &text_view->left_window);
8535 case GTK_TEXT_WINDOW_RIGHT:
8536 set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
8537 &text_view->right_window);
8540 case GTK_TEXT_WINDOW_TOP:
8541 set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
8542 &text_view->top_window);
8545 case GTK_TEXT_WINDOW_BOTTOM:
8546 set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
8547 &text_view->bottom_window);
8551 g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
8557 * gtk_text_view_get_border_window_size:
8558 * @text_view: a #GtkTextView
8559 * @type: window to return size from
8561 * Gets the width of the specified border window. See
8562 * gtk_text_view_set_border_window_size().
8564 * Return value: width of window
8567 gtk_text_view_get_border_window_size (GtkTextView *text_view,
8568 GtkTextWindowType type)
8570 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8574 case GTK_TEXT_WINDOW_LEFT:
8575 if (text_view->left_window)
8576 return text_view->left_window->requisition.width;
8579 case GTK_TEXT_WINDOW_RIGHT:
8580 if (text_view->right_window)
8581 return text_view->right_window->requisition.width;
8584 case GTK_TEXT_WINDOW_TOP:
8585 if (text_view->top_window)
8586 return text_view->top_window->requisition.height;
8589 case GTK_TEXT_WINDOW_BOTTOM:
8590 if (text_view->bottom_window)
8591 return text_view->bottom_window->requisition.height;
8595 g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
8606 static GtkTextViewChild*
8607 text_view_child_new_anchored (GtkWidget *child,
8608 GtkTextChildAnchor *anchor,
8609 GtkTextLayout *layout)
8611 GtkTextViewChild *vc;
8613 vc = g_new (GtkTextViewChild, 1);
8615 vc->type = GTK_TEXT_WINDOW_PRIVATE;
8617 vc->anchor = anchor;
8619 vc->from_top_of_line = 0;
8620 vc->from_left_of_buffer = 0;
8622 g_object_ref (vc->widget);
8623 g_object_ref (vc->anchor);
8625 g_object_set_data (G_OBJECT (child),
8626 I_("gtk-text-view-child"),
8629 gtk_text_child_anchor_register_child (anchor, child, layout);
8634 static GtkTextViewChild*
8635 text_view_child_new_window (GtkWidget *child,
8636 GtkTextWindowType type,
8640 GtkTextViewChild *vc;
8642 vc = g_new (GtkTextViewChild, 1);
8647 vc->from_top_of_line = 0;
8648 vc->from_left_of_buffer = 0;
8650 g_object_ref (vc->widget);
8656 g_object_set_data (G_OBJECT (child),
8657 I_("gtk-text-view-child"),
8664 text_view_child_free (GtkTextViewChild *child)
8666 g_object_set_data (G_OBJECT (child->widget),
8667 I_("gtk-text-view-child"), NULL);
8671 gtk_text_child_anchor_unregister_child (child->anchor,
8673 g_object_unref (child->anchor);
8676 g_object_unref (child->widget);
8682 text_view_child_set_parent_window (GtkTextView *text_view,
8683 GtkTextViewChild *vc)
8686 gtk_widget_set_parent_window (vc->widget,
8687 text_view->text_window->bin_window);
8691 window = gtk_text_view_get_window (text_view,
8693 gtk_widget_set_parent_window (vc->widget, window);
8698 add_child (GtkTextView *text_view,
8699 GtkTextViewChild *vc)
8701 text_view->children = g_slist_prepend (text_view->children,
8704 if (GTK_WIDGET_REALIZED (text_view))
8705 text_view_child_set_parent_window (text_view, vc);
8707 gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
8711 * gtk_text_view_add_child_at_anchor:
8712 * @text_view: a #GtkTextView
8713 * @child: a #GtkWidget
8714 * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
8716 * Adds a child widget in the text buffer, at the given @anchor.
8719 gtk_text_view_add_child_at_anchor (GtkTextView *text_view,
8721 GtkTextChildAnchor *anchor)
8723 GtkTextViewChild *vc;
8725 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8726 g_return_if_fail (GTK_IS_WIDGET (child));
8727 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
8728 g_return_if_fail (child->parent == NULL);
8730 gtk_text_view_ensure_layout (text_view);
8732 vc = text_view_child_new_anchored (child, anchor,
8735 add_child (text_view, vc);
8737 g_assert (vc->widget == child);
8738 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8742 * gtk_text_view_add_child_in_window:
8743 * @text_view: a #GtkTextView
8744 * @child: a #GtkWidget
8745 * @which_window: which window the child should appear in
8746 * @xpos: X position of child in window coordinates
8747 * @ypos: Y position of child in window coordinates
8749 * Adds a child at fixed coordinates in one of the text widget's
8750 * windows. The window must have nonzero size (see
8751 * gtk_text_view_set_border_window_size()). Note that the child
8752 * coordinates are given relative to the #GdkWindow in question, and
8753 * that these coordinates have no sane relationship to scrolling. When
8754 * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
8755 * irrelevant, the child floats above all scrollable areas. But when
8756 * placing a child in one of the scrollable windows (border windows or
8757 * text window), you'll need to compute the child's correct position
8758 * in buffer coordinates any time scrolling occurs or buffer changes
8759 * occur, and then call gtk_text_view_move_child() to update the
8760 * child's position. Unfortunately there's no good way to detect that
8761 * scrolling has occurred, using the current API; a possible hack
8762 * would be to update all child positions when the scroll adjustments
8763 * change or the text buffer changes. See bug 64518 on
8764 * bugzilla.gnome.org for status of fixing this issue.
8767 gtk_text_view_add_child_in_window (GtkTextView *text_view,
8769 GtkTextWindowType which_window,
8773 GtkTextViewChild *vc;
8775 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8776 g_return_if_fail (GTK_IS_WIDGET (child));
8777 g_return_if_fail (child->parent == NULL);
8779 vc = text_view_child_new_window (child, which_window,
8782 add_child (text_view, vc);
8784 g_assert (vc->widget == child);
8785 g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8789 * gtk_text_view_move_child:
8790 * @text_view: a #GtkTextView
8791 * @child: child widget already added to the text view
8792 * @xpos: new X position in window coordinates
8793 * @ypos: new Y position in window coordinates
8795 * Updates the position of a child, as for gtk_text_view_add_child_in_window().
8798 gtk_text_view_move_child (GtkTextView *text_view,
8803 GtkTextViewChild *vc;
8805 g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8806 g_return_if_fail (GTK_IS_WIDGET (child));
8807 g_return_if_fail (child->parent == (GtkWidget*) text_view);
8809 vc = g_object_get_data (G_OBJECT (child),
8810 "gtk-text-view-child");
8812 g_assert (vc != NULL);
8814 if (vc->x == xpos &&
8821 if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
8822 gtk_widget_queue_resize (child);
8826 /* Iterator operations */
8829 * gtk_text_view_forward_display_line:
8830 * @text_view: a #GtkTextView
8831 * @iter: a #GtkTextIter
8833 * Moves the given @iter forward by one display (wrapped) line.
8834 * A display line is different from a paragraph. Paragraphs are
8835 * separated by newlines or other paragraph separator characters.
8836 * Display lines are created by line-wrapping a paragraph. If
8837 * wrapping is turned off, display lines and paragraphs will be the
8838 * same. Display lines are divided differently for each view, since
8839 * they depend on the view's width; paragraphs are the same in all
8840 * views, since they depend on the contents of the #GtkTextBuffer.
8842 * Return value: %TRUE if @iter was moved and is not on the end iterator
8845 gtk_text_view_forward_display_line (GtkTextView *text_view,
8848 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8849 g_return_val_if_fail (iter != NULL, FALSE);
8851 gtk_text_view_ensure_layout (text_view);
8853 return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
8857 * gtk_text_view_backward_display_line:
8858 * @text_view: a #GtkTextView
8859 * @iter: a #GtkTextIter
8861 * Moves the given @iter backward by one display (wrapped) line.
8862 * A display line is different from a paragraph. Paragraphs are
8863 * separated by newlines or other paragraph separator characters.
8864 * Display lines are created by line-wrapping a paragraph. If
8865 * wrapping is turned off, display lines and paragraphs will be the
8866 * same. Display lines are divided differently for each view, since
8867 * they depend on the view's width; paragraphs are the same in all
8868 * views, since they depend on the contents of the #GtkTextBuffer.
8870 * Return value: %TRUE if @iter was moved and is not on the end iterator
8873 gtk_text_view_backward_display_line (GtkTextView *text_view,
8876 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8877 g_return_val_if_fail (iter != NULL, FALSE);
8879 gtk_text_view_ensure_layout (text_view);
8881 return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
8885 * gtk_text_view_forward_display_line_end:
8886 * @text_view: a #GtkTextView
8887 * @iter: a #GtkTextIter
8889 * Moves the given @iter forward to the next display line end.
8890 * A display line is different from a paragraph. Paragraphs are
8891 * separated by newlines or other paragraph separator characters.
8892 * Display lines are created by line-wrapping a paragraph. If
8893 * wrapping is turned off, display lines and paragraphs will be the
8894 * same. Display lines are divided differently for each view, since
8895 * they depend on the view's width; paragraphs are the same in all
8896 * views, since they depend on the contents of the #GtkTextBuffer.
8898 * Return value: %TRUE if @iter was moved and is not on the end iterator
8901 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
8904 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8905 g_return_val_if_fail (iter != NULL, FALSE);
8907 gtk_text_view_ensure_layout (text_view);
8909 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
8913 * gtk_text_view_backward_display_line_start:
8914 * @text_view: a #GtkTextView
8915 * @iter: a #GtkTextIter
8917 * Moves the given @iter backward to the next display line start.
8918 * A display line is different from a paragraph. Paragraphs are
8919 * separated by newlines or other paragraph separator characters.
8920 * Display lines are created by line-wrapping a paragraph. If
8921 * wrapping is turned off, display lines and paragraphs will be the
8922 * same. Display lines are divided differently for each view, since
8923 * they depend on the view's width; paragraphs are the same in all
8924 * views, since they depend on the contents of the #GtkTextBuffer.
8926 * Return value: %TRUE if @iter was moved and is not on the end iterator
8929 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
8932 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8933 g_return_val_if_fail (iter != NULL, FALSE);
8935 gtk_text_view_ensure_layout (text_view);
8937 return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
8941 * gtk_text_view_starts_display_line:
8942 * @text_view: a #GtkTextView
8943 * @iter: a #GtkTextIter
8945 * Determines whether @iter is at the start of a display line.
8946 * See gtk_text_view_forward_display_line() for an explanation of
8947 * display lines vs. paragraphs.
8949 * Return value: %TRUE if @iter begins a wrapped line
8952 gtk_text_view_starts_display_line (GtkTextView *text_view,
8953 const GtkTextIter *iter)
8955 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8956 g_return_val_if_fail (iter != NULL, FALSE);
8958 gtk_text_view_ensure_layout (text_view);
8960 return gtk_text_layout_iter_starts_line (text_view->layout, iter);
8964 * gtk_text_view_move_visually:
8965 * @text_view: a #GtkTextView
8966 * @iter: a #GtkTextIter
8967 * @count: number of characters to move (negative moves left,
8968 * positive moves right)
8970 * Move the iterator a given number of characters visually, treating
8971 * it as the strong cursor position. If @count is positive, then the
8972 * new strong cursor position will be @count positions to the right of
8973 * the old cursor position. If @count is negative then the new strong
8974 * cursor position will be @count positions to the left of the old
8977 * In the presence of bi-directional text, the correspondence
8978 * between logical and visual order will depend on the direction
8979 * of the current run, and there may be jumps when the cursor
8980 * is moved off of the end of a run.
8982 * Return value: %TRUE if @iter moved and is not on the end iterator
8985 gtk_text_view_move_visually (GtkTextView *text_view,
8989 g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8990 g_return_val_if_fail (iter != NULL, FALSE);
8992 gtk_text_view_ensure_layout (text_view);
8994 return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
8997 #define __GTK_TEXT_VIEW_C__
8998 #include "gtkaliasdef.c"