]> Pileus Git - ~andy/gtk/blob - gtk/gtktextview.c
Don't install cursors on insensitive widgets. (#358864, Jan Schampera)
[~andy/gtk] / gtk / gtktextview.c
1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3  * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 /*
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/.
26  */
27
28 #include <config.h>
29 #include <string.h>
30
31 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
32 #include "gtkbindings.h"
33 #include "gtkdnd.h"
34 #include "gtkimagemenuitem.h"
35 #include "gtkintl.h"
36 #include "gtkmain.h"
37 #include "gtkmarshalers.h"
38 #include "gtkmenu.h"
39 #include "gtkmenuitem.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtksettings.h"
42 #include "gtkstock.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"
51 #include "gtkalias.h"
52
53 /* How scrolling, validation, exposes, etc. work.
54  *
55  * The expose_event handler has the invariant that the onscreen lines
56  * have been validated.
57  *
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
65  *
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
74  *
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.
79  *
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.
85  *
86  */
87
88 #if 0
89 #define DEBUG_VALIDATION_AND_SCROLLING
90 #endif
91
92 #ifdef DEBUG_VALIDATION_AND_SCROLLING
93 #define DV(x) (x)
94 #else
95 #define DV(x)
96 #endif
97
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)
100
101 #define SPACE_FOR_CURSOR 1
102
103 typedef struct _GtkTextViewPrivate GtkTextViewPrivate;
104
105 #define GTK_TEXT_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_VIEW, GtkTextViewPrivate))
106
107 struct _GtkTextViewPrivate 
108 {
109   guint blink_time;  /* time in msec the cursor has blinked since last user event */
110 };
111
112
113 struct _GtkTextPendingScroll
114 {
115   GtkTextMark   *mark;
116   gdouble        within_margin;
117   gboolean       use_align;
118   gdouble        xalign;
119   gdouble        yalign;
120 };
121   
122 enum
123 {
124   SET_SCROLL_ADJUSTMENTS,
125   POPULATE_POPUP,
126   MOVE_CURSOR,
127   PAGE_HORIZONTALLY,
128   SET_ANCHOR,
129   INSERT_AT_CURSOR,
130   DELETE_FROM_CURSOR,
131   BACKSPACE,
132   CUT_CLIPBOARD,
133   COPY_CLIPBOARD,
134   PASTE_CLIPBOARD,
135   TOGGLE_OVERWRITE,
136   MOVE_FOCUS,
137   MOVE_VIEWPORT,
138   SELECT_ALL,
139   LAST_SIGNAL
140 };
141
142 enum
143 {
144   PROP_0,
145   PROP_PIXELS_ABOVE_LINES,
146   PROP_PIXELS_BELOW_LINES,
147   PROP_PIXELS_INSIDE_WRAP,
148   PROP_EDITABLE,
149   PROP_WRAP_MODE,
150   PROP_JUSTIFICATION,
151   PROP_LEFT_MARGIN,
152   PROP_RIGHT_MARGIN,
153   PROP_INDENT,
154   PROP_TABS,
155   PROP_CURSOR_VISIBLE,
156   PROP_BUFFER,
157   PROP_OVERWRITE,
158   PROP_ACCEPTS_TAB,
159   LAST_PROP
160 };
161
162 static void gtk_text_view_destroy              (GtkObject        *object);
163 static void gtk_text_view_finalize             (GObject          *object);
164 static void gtk_text_view_set_property         (GObject         *object,
165                                                 guint            prop_id,
166                                                 const GValue    *value,
167                                                 GParamSpec      *pspec);
168 static void gtk_text_view_get_property         (GObject         *object,
169                                                 guint            prop_id,
170                                                 GValue          *value,
171                                                 GParamSpec      *pspec);
172 static void gtk_text_view_size_request         (GtkWidget        *widget,
173                                                 GtkRequisition   *requisition);
174 static void gtk_text_view_size_allocate        (GtkWidget        *widget,
175                                                 GtkAllocation    *allocation);
176 static void gtk_text_view_realize              (GtkWidget        *widget);
177 static void gtk_text_view_unrealize            (GtkWidget        *widget);
178 static void gtk_text_view_style_set            (GtkWidget        *widget,
179                                                 GtkStyle         *previous_style);
180 static void gtk_text_view_direction_changed    (GtkWidget        *widget,
181                                                 GtkTextDirection  previous_direction);
182 static void gtk_text_view_grab_notify          (GtkWidget        *widget,
183                                                 gboolean         was_grabbed);
184 static void gtk_text_view_state_changed        (GtkWidget        *widget,
185                                                 GtkStateType      previous_state);
186
187 static gint gtk_text_view_event                (GtkWidget        *widget,
188                                                 GdkEvent         *event);
189 static gint gtk_text_view_key_press_event      (GtkWidget        *widget,
190                                                 GdkEventKey      *event);
191 static gint gtk_text_view_key_release_event    (GtkWidget        *widget,
192                                                 GdkEventKey      *event);
193 static gint gtk_text_view_button_press_event   (GtkWidget        *widget,
194                                                 GdkEventButton   *event);
195 static gint gtk_text_view_button_release_event (GtkWidget        *widget,
196                                                 GdkEventButton   *event);
197 static gint gtk_text_view_focus_in_event       (GtkWidget        *widget,
198                                                 GdkEventFocus    *event);
199 static gint gtk_text_view_focus_out_event      (GtkWidget        *widget,
200                                                 GdkEventFocus    *event);
201 static gint gtk_text_view_motion_event         (GtkWidget        *widget,
202                                                 GdkEventMotion   *event);
203 static gint gtk_text_view_expose_event         (GtkWidget        *widget,
204                                                 GdkEventExpose   *expose);
205 static void gtk_text_view_draw_focus           (GtkWidget        *widget);
206 static gboolean gtk_text_view_focus            (GtkWidget        *widget,
207                                                 GtkDirectionType  direction);
208 static void gtk_text_view_select_all           (GtkWidget        *widget,
209                                                 gboolean          select);
210
211
212 /* Source side drag signals */
213 static void gtk_text_view_drag_begin       (GtkWidget        *widget,
214                                             GdkDragContext   *context);
215 static void gtk_text_view_drag_end         (GtkWidget        *widget,
216                                             GdkDragContext   *context);
217 static void gtk_text_view_drag_data_get    (GtkWidget        *widget,
218                                             GdkDragContext   *context,
219                                             GtkSelectionData *selection_data,
220                                             guint             info,
221                                             guint             time);
222 static void gtk_text_view_drag_data_delete (GtkWidget        *widget,
223                                             GdkDragContext   *context);
224
225 /* Target side drag signals */
226 static void     gtk_text_view_drag_leave         (GtkWidget        *widget,
227                                                   GdkDragContext   *context,
228                                                   guint             time);
229 static gboolean gtk_text_view_drag_motion        (GtkWidget        *widget,
230                                                   GdkDragContext   *context,
231                                                   gint              x,
232                                                   gint              y,
233                                                   guint             time);
234 static gboolean gtk_text_view_drag_drop          (GtkWidget        *widget,
235                                                   GdkDragContext   *context,
236                                                   gint              x,
237                                                   gint              y,
238                                                   guint             time);
239 static void     gtk_text_view_drag_data_received (GtkWidget        *widget,
240                                                   GdkDragContext   *context,
241                                                   gint              x,
242                                                   gint              y,
243                                                   GtkSelectionData *selection_data,
244                                                   guint             info,
245                                                   guint             time);
246
247 static void gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
248                                                   GtkAdjustment *hadj,
249                                                   GtkAdjustment *vadj);
250 static gboolean gtk_text_view_popup_menu         (GtkWidget     *widget);
251
252 static void gtk_text_view_move_cursor       (GtkTextView           *text_view,
253                                              GtkMovementStep        step,
254                                              gint                   count,
255                                              gboolean               extend_selection);
256 static void gtk_text_view_page_horizontally (GtkTextView          *text_view,
257                                              gint                  count,
258                                              gboolean              extend_selection);
259 static void gtk_text_view_move_viewport     (GtkTextView           *text_view,
260                                              GtkScrollStep          step,
261                                              gint                   count);
262 static void gtk_text_view_set_anchor       (GtkTextView           *text_view);
263 static gboolean gtk_text_view_scroll_pages (GtkTextView           *text_view,
264                                             gint                   count,
265                                             gboolean               extend_selection);
266 static gboolean gtk_text_view_scroll_hpages(GtkTextView           *text_view,
267                                             gint                   count,
268                                             gboolean               extend_selection);
269 static void gtk_text_view_insert_at_cursor (GtkTextView           *text_view,
270                                             const gchar           *str);
271 static void gtk_text_view_delete_from_cursor (GtkTextView           *text_view,
272                                               GtkDeleteType          type,
273                                               gint                   count);
274 static void gtk_text_view_backspace        (GtkTextView           *text_view);
275 static void gtk_text_view_cut_clipboard    (GtkTextView           *text_view);
276 static void gtk_text_view_copy_clipboard   (GtkTextView           *text_view);
277 static void gtk_text_view_paste_clipboard  (GtkTextView           *text_view);
278 static void gtk_text_view_toggle_overwrite (GtkTextView           *text_view);
279 static void gtk_text_view_move_focus       (GtkTextView           *text_view,
280                                             GtkDirectionType       direction_type);
281 static void gtk_text_view_unselect         (GtkTextView           *text_view);
282
283 static void     gtk_text_view_validate_onscreen     (GtkTextView        *text_view);
284 static void     gtk_text_view_get_first_para_iter   (GtkTextView        *text_view,
285                                                      GtkTextIter        *iter);
286 static void     gtk_text_view_update_layout_width       (GtkTextView        *text_view);
287 static void     gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
288                                                          GtkTextAttributes *values,
289                                                          GtkStyle           *style);
290 static void     gtk_text_view_ensure_layout          (GtkTextView        *text_view);
291 static void     gtk_text_view_destroy_layout         (GtkTextView        *text_view);
292 static void     gtk_text_view_check_keymap_direction (GtkTextView        *text_view);
293 static void     gtk_text_view_reset_im_context       (GtkTextView        *text_view);
294 static void     gtk_text_view_start_selection_drag   (GtkTextView        *text_view,
295                                                       const GtkTextIter  *iter,
296                                                       GdkEventButton     *event);
297 static gboolean gtk_text_view_end_selection_drag     (GtkTextView        *text_view,
298                                                       GdkEventButton     *event);
299 static void     gtk_text_view_start_selection_dnd    (GtkTextView        *text_view,
300                                                       const GtkTextIter  *iter,
301                                                       GdkEventMotion     *event);
302 static void     gtk_text_view_check_cursor_blink     (GtkTextView        *text_view);
303 static void     gtk_text_view_pend_cursor_blink      (GtkTextView        *text_view);
304 static void     gtk_text_view_stop_cursor_blink      (GtkTextView        *text_view);
305 static void     gtk_text_view_reset_blink_time       (GtkTextView        *text_view);
306
307 static void     gtk_text_view_value_changed                (GtkAdjustment *adj,
308                                                             GtkTextView   *view);
309 static void     gtk_text_view_commit_handler               (GtkIMContext  *context,
310                                                             const gchar   *str,
311                                                             GtkTextView   *text_view);
312 static void     gtk_text_view_commit_text                  (GtkTextView   *text_view,
313                                                             const gchar   *text);
314 static void     gtk_text_view_preedit_changed_handler      (GtkIMContext  *context,
315                                                             GtkTextView   *text_view);
316 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext  *context,
317                                                             GtkTextView   *text_view);
318 static gboolean gtk_text_view_delete_surrounding_handler   (GtkIMContext  *context,
319                                                             gint           offset,
320                                                             gint           n_chars,
321                                                             GtkTextView   *text_view);
322
323 static void gtk_text_view_mark_set_handler       (GtkTextBuffer     *buffer,
324                                                   const GtkTextIter *location,
325                                                   GtkTextMark       *mark,
326                                                   gpointer           data);
327 static void gtk_text_view_target_list_notify     (GtkTextBuffer     *buffer,
328                                                   const GParamSpec  *pspec,
329                                                   gpointer           data);
330 static void gtk_text_view_get_cursor_location    (GtkTextView       *text_view,
331                                                   GdkRectangle      *pos);
332 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView       *text_view,
333                                                   gint              *x,
334                                                   gint              *y);
335 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView       *text_view,
336                                                   gint               x,
337                                                   gint               y);
338
339 static GtkAdjustment* get_hadjustment            (GtkTextView       *text_view);
340 static GtkAdjustment* get_vadjustment            (GtkTextView       *text_view);
341
342 static void gtk_text_view_do_popup               (GtkTextView       *text_view,
343                                                   GdkEventButton    *event);
344
345 static void gtk_text_view_queue_scroll           (GtkTextView   *text_view,
346                                                   GtkTextMark   *mark,
347                                                   gdouble        within_margin,
348                                                   gboolean       use_align,
349                                                   gdouble        xalign,
350                                                   gdouble        yalign);
351
352 static gboolean gtk_text_view_flush_scroll         (GtkTextView *text_view);
353 static void     gtk_text_view_update_adjustments   (GtkTextView *text_view);
354 static void     gtk_text_view_invalidate           (GtkTextView *text_view);
355 static void     gtk_text_view_flush_first_validate (GtkTextView *text_view);
356
357 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
358
359 /* Container methods */
360 static void gtk_text_view_add    (GtkContainer *container,
361                                   GtkWidget    *child);
362 static void gtk_text_view_remove (GtkContainer *container,
363                                   GtkWidget    *child);
364 static void gtk_text_view_forall (GtkContainer *container,
365                                   gboolean      include_internals,
366                                   GtkCallback   callback,
367                                   gpointer      callback_data);
368
369 /* FIXME probably need the focus methods. */
370
371 typedef struct _GtkTextViewChild GtkTextViewChild;
372
373 struct _GtkTextViewChild
374 {
375   GtkWidget *widget;
376
377   GtkTextChildAnchor *anchor;
378
379   gint from_top_of_line;
380   gint from_left_of_buffer;
381   
382   /* These are ignored if anchor != NULL */
383   GtkTextWindowType type;
384   gint x;
385   gint y;
386 };
387
388 static GtkTextViewChild* text_view_child_new_anchored      (GtkWidget          *child,
389                                                             GtkTextChildAnchor *anchor,
390                                                             GtkTextLayout      *layout);
391 static GtkTextViewChild* text_view_child_new_window        (GtkWidget          *child,
392                                                             GtkTextWindowType   type,
393                                                             gint                x,
394                                                             gint                y);
395 static void              text_view_child_free              (GtkTextViewChild   *child);
396 static void              text_view_child_set_parent_window (GtkTextView        *text_view,
397                                                             GtkTextViewChild   *child);
398
399 struct _GtkTextWindow
400 {
401   GtkTextWindowType type;
402   GtkWidget *widget;
403   GdkWindow *window;
404   GdkWindow *bin_window;
405   GtkRequisition requisition;
406   GdkRectangle allocation;
407 };
408
409 static GtkTextWindow *text_window_new             (GtkTextWindowType  type,
410                                                    GtkWidget         *widget,
411                                                    gint               width_request,
412                                                    gint               height_request);
413 static void           text_window_free            (GtkTextWindow     *win);
414 static void           text_window_realize         (GtkTextWindow     *win,
415                                                    GtkWidget         *widget);
416 static void           text_window_unrealize       (GtkTextWindow     *win);
417 static void           text_window_size_allocate   (GtkTextWindow     *win,
418                                                    GdkRectangle      *rect);
419 static void           text_window_scroll          (GtkTextWindow     *win,
420                                                    gint               dx,
421                                                    gint               dy);
422 static void           text_window_invalidate_rect (GtkTextWindow     *win,
423                                                    GdkRectangle      *rect);
424 static void           text_window_invalidate_cursors (GtkTextWindow  *win);
425
426 static gint           text_window_get_width       (GtkTextWindow     *win);
427 static gint           text_window_get_height      (GtkTextWindow     *win);
428
429
430 static guint signals[LAST_SIGNAL] = { 0 };
431
432 G_DEFINE_TYPE (GtkTextView, gtk_text_view, GTK_TYPE_CONTAINER)
433
434 static void
435 add_move_binding (GtkBindingSet  *binding_set,
436                   guint           keyval,
437                   guint           modmask,
438                   GtkMovementStep step,
439                   gint            count)
440 {
441   g_assert ((modmask & GDK_SHIFT_MASK) == 0);
442
443   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
444                                 "move_cursor", 3,
445                                 G_TYPE_ENUM, step,
446                                 G_TYPE_INT, count,
447                                 G_TYPE_BOOLEAN, FALSE);
448
449   /* Selection-extending version */
450   gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
451                                 "move_cursor", 3,
452                                 G_TYPE_ENUM, step,
453                                 G_TYPE_INT, count,
454                                 G_TYPE_BOOLEAN, TRUE);
455 }
456
457 static void
458 gtk_text_view_class_init (GtkTextViewClass *klass)
459 {
460   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
461   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
462   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
463   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
464   GtkBindingSet *binding_set;
465
466   /* Default handlers and virtual methods
467    */
468   gobject_class->set_property = gtk_text_view_set_property;
469   gobject_class->get_property = gtk_text_view_get_property;
470
471   object_class->destroy = gtk_text_view_destroy;
472   gobject_class->finalize = gtk_text_view_finalize;
473
474   widget_class->realize = gtk_text_view_realize;
475   widget_class->unrealize = gtk_text_view_unrealize;
476   widget_class->style_set = gtk_text_view_style_set;
477   widget_class->direction_changed = gtk_text_view_direction_changed;
478   widget_class->grab_notify = gtk_text_view_grab_notify;
479   widget_class->state_changed = gtk_text_view_state_changed;
480   widget_class->size_request = gtk_text_view_size_request;
481   widget_class->size_allocate = gtk_text_view_size_allocate;
482   widget_class->event = gtk_text_view_event;
483   widget_class->key_press_event = gtk_text_view_key_press_event;
484   widget_class->key_release_event = gtk_text_view_key_release_event;
485   widget_class->button_press_event = gtk_text_view_button_press_event;
486   widget_class->button_release_event = gtk_text_view_button_release_event;
487   widget_class->focus_in_event = gtk_text_view_focus_in_event;
488   widget_class->focus_out_event = gtk_text_view_focus_out_event;
489   widget_class->motion_notify_event = gtk_text_view_motion_event;
490   widget_class->expose_event = gtk_text_view_expose_event;
491   widget_class->focus = gtk_text_view_focus;
492   
493   widget_class->drag_begin = gtk_text_view_drag_begin;
494   widget_class->drag_end = gtk_text_view_drag_end;
495   widget_class->drag_data_get = gtk_text_view_drag_data_get;
496   widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
497
498   widget_class->drag_leave = gtk_text_view_drag_leave;
499   widget_class->drag_motion = gtk_text_view_drag_motion;
500   widget_class->drag_drop = gtk_text_view_drag_drop;
501   widget_class->drag_data_received = gtk_text_view_drag_data_received;
502
503   widget_class->popup_menu = gtk_text_view_popup_menu;
504   
505   container_class->add = gtk_text_view_add;
506   container_class->remove = gtk_text_view_remove;
507   container_class->forall = gtk_text_view_forall;
508
509   klass->move_cursor = gtk_text_view_move_cursor;
510   klass->page_horizontally = gtk_text_view_page_horizontally;
511   klass->set_anchor = gtk_text_view_set_anchor;
512   klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
513   klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
514   klass->backspace = gtk_text_view_backspace;
515   klass->cut_clipboard = gtk_text_view_cut_clipboard;
516   klass->copy_clipboard = gtk_text_view_copy_clipboard;
517   klass->paste_clipboard = gtk_text_view_paste_clipboard;
518   klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
519   klass->move_focus = gtk_text_view_move_focus;
520   klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
521
522   /*
523    * Properties
524    */
525  
526   g_object_class_install_property (gobject_class,
527                                    PROP_PIXELS_ABOVE_LINES,
528                                    g_param_spec_int ("pixels-above-lines",
529                                                      P_("Pixels Above Lines"),
530                                                      P_("Pixels of blank space above paragraphs"),
531                                                      0,
532                                                      G_MAXINT,
533                                                      0,
534                                                      GTK_PARAM_READWRITE));
535  
536   g_object_class_install_property (gobject_class,
537                                    PROP_PIXELS_BELOW_LINES,
538                                    g_param_spec_int ("pixels-below-lines",
539                                                      P_("Pixels Below Lines"),
540                                                      P_("Pixels of blank space below paragraphs"),
541                                                      0,
542                                                      G_MAXINT,
543                                                      0,
544                                                      GTK_PARAM_READWRITE));
545  
546   g_object_class_install_property (gobject_class,
547                                    PROP_PIXELS_INSIDE_WRAP,
548                                    g_param_spec_int ("pixels-inside-wrap",
549                                                      P_("Pixels Inside Wrap"),
550                                                      P_("Pixels of blank space between wrapped lines in a paragraph"),
551                                                      0,
552                                                      G_MAXINT,
553                                                      0,
554                                                      GTK_PARAM_READWRITE));
555
556   g_object_class_install_property (gobject_class,
557                                    PROP_EDITABLE,
558                                    g_param_spec_boolean ("editable",
559                                                          P_("Editable"),
560                                                          P_("Whether the text can be modified by the user"),
561                                                          TRUE,
562                                                          GTK_PARAM_READWRITE));
563
564   g_object_class_install_property (gobject_class,
565                                    PROP_WRAP_MODE,
566                                    g_param_spec_enum ("wrap-mode",
567                                                       P_("Wrap Mode"),
568                                                       P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
569                                                       GTK_TYPE_WRAP_MODE,
570                                                       GTK_WRAP_NONE,
571                                                       GTK_PARAM_READWRITE));
572  
573   g_object_class_install_property (gobject_class,
574                                    PROP_JUSTIFICATION,
575                                    g_param_spec_enum ("justification",
576                                                       P_("Justification"),
577                                                       P_("Left, right, or center justification"),
578                                                       GTK_TYPE_JUSTIFICATION,
579                                                       GTK_JUSTIFY_LEFT,
580                                                       GTK_PARAM_READWRITE));
581  
582   g_object_class_install_property (gobject_class,
583                                    PROP_LEFT_MARGIN,
584                                    g_param_spec_int ("left-margin",
585                                                      P_("Left Margin"),
586                                                      P_("Width of the left margin in pixels"),
587                                                      0,
588                                                      G_MAXINT,
589                                                      0,
590                                                      GTK_PARAM_READWRITE));
591
592   g_object_class_install_property (gobject_class,
593                                    PROP_RIGHT_MARGIN,
594                                    g_param_spec_int ("right-margin",
595                                                      P_("Right Margin"),
596                                                      P_("Width of the right margin in pixels"),
597                                                      0,
598                                                      G_MAXINT,
599                                                      0,
600                                                      GTK_PARAM_READWRITE));
601
602   g_object_class_install_property (gobject_class,
603                                    PROP_INDENT,
604                                    g_param_spec_int ("indent",
605                                                      P_("Indent"),
606                                                      P_("Amount to indent the paragraph, in pixels"),
607                                                      0,
608                                                      G_MAXINT,
609                                                      0,
610                                                      GTK_PARAM_READWRITE));
611
612   g_object_class_install_property (gobject_class,
613                                    PROP_TABS,
614                                    g_param_spec_boxed ("tabs",
615                                                        P_("Tabs"),
616                                                        P_("Custom tabs for this text"),
617                                                        PANGO_TYPE_TAB_ARRAY,
618                                                        GTK_PARAM_READWRITE));
619
620   g_object_class_install_property (gobject_class,
621                                    PROP_CURSOR_VISIBLE,
622                                    g_param_spec_boolean ("cursor-visible",
623                                                          P_("Cursor Visible"),
624                                                          P_("If the insertion cursor is shown"),
625                                                          TRUE,
626                                                          GTK_PARAM_READWRITE));
627
628   g_object_class_install_property (gobject_class,
629                                    PROP_BUFFER,
630                                    g_param_spec_object ("buffer",
631                                                         P_("Buffer"),
632                                                         P_("The buffer which is displayed"),
633                                                         GTK_TYPE_TEXT_BUFFER,
634                                                         GTK_PARAM_READWRITE));
635
636   g_object_class_install_property (gobject_class,
637                                    PROP_OVERWRITE,
638                                    g_param_spec_boolean ("overwrite",
639                                                          P_("Overwrite mode"),
640                                                          P_("Whether entered text overwrites existing contents"),
641                                                          FALSE,
642                                                          GTK_PARAM_READWRITE));
643
644   g_object_class_install_property (gobject_class,
645                                    PROP_ACCEPTS_TAB,
646                                    g_param_spec_boolean ("accepts-tab",
647                                                          P_("Accepts tab"),
648                                                          P_("Whether Tab will result in a tab character being entered"),
649                                                          TRUE,
650                                                          GTK_PARAM_READWRITE));
651
652   /*
653    * Style properties
654    */
655   gtk_widget_class_install_style_property (widget_class,
656                                            g_param_spec_boxed ("error-underline-color",
657                                                                P_("Error underline color"),
658                                                                P_("Color with which to draw error-indication underlines"),
659                                                                GDK_TYPE_COLOR,
660                                                                GTK_PARAM_READABLE));
661   
662   /*
663    * Signals
664    */
665
666   /**
667    * GtkTextView::move-cursor: 
668    * @widget: the object which received the signal
669    * @step: the granularity of the move, as a #GtkMovementStep
670    * @count: the number of @step units to move
671    * @extend_selection: %TRUE if the move should extend the selection
672    *  
673    * The ::move-cursor signal is a keybinding signal which gets emitted
674    * when the user initiates a cursor movement. 
675    *
676    * Applications should not connect to it, but may emit it with 
677    * g_signal_emit_by_name() if they need to control scrolling 
678    * programmatically.
679    *
680    */
681   signals[MOVE_CURSOR] = 
682     g_signal_new (I_("move_cursor"),
683                   G_OBJECT_CLASS_TYPE (gobject_class), 
684                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, 
685                   G_STRUCT_OFFSET (GtkTextViewClass, move_cursor),
686                   NULL, NULL, 
687                   _gtk_marshal_VOID__ENUM_INT_BOOLEAN, 
688                   G_TYPE_NONE, 3,
689                   GTK_TYPE_MOVEMENT_STEP, 
690                   G_TYPE_INT, 
691                   G_TYPE_BOOLEAN);
692
693   signals[PAGE_HORIZONTALLY] =
694     g_signal_new (I_("page_horizontally"),
695                   G_OBJECT_CLASS_TYPE (gobject_class),
696                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
697                   G_STRUCT_OFFSET (GtkTextViewClass, page_horizontally),
698                   NULL, NULL,
699                   _gtk_marshal_VOID__INT_BOOLEAN,
700                   G_TYPE_NONE, 2,
701                   G_TYPE_INT,
702                   G_TYPE_BOOLEAN);
703   
704   signals[MOVE_VIEWPORT] =
705     _gtk_binding_signal_new (I_("move_viewport"),
706                              G_OBJECT_CLASS_TYPE (gobject_class),
707                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
708                              G_CALLBACK (gtk_text_view_move_viewport),
709                              NULL, NULL,
710                              _gtk_marshal_VOID__ENUM_INT,
711                              G_TYPE_NONE, 2,
712                              GTK_TYPE_SCROLL_STEP,
713                              G_TYPE_INT);
714
715   signals[SET_ANCHOR] =
716     g_signal_new (I_("set_anchor"),
717                   G_OBJECT_CLASS_TYPE (gobject_class),
718                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
719                   G_STRUCT_OFFSET (GtkTextViewClass, set_anchor),
720                   NULL, NULL,
721                   _gtk_marshal_VOID__VOID,
722                   G_TYPE_NONE, 0);
723
724   signals[INSERT_AT_CURSOR] =
725     g_signal_new (I_("insert_at_cursor"),
726                   G_OBJECT_CLASS_TYPE (gobject_class),
727                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
728                   G_STRUCT_OFFSET (GtkTextViewClass, insert_at_cursor),
729                   NULL, NULL,
730                   _gtk_marshal_VOID__STRING,
731                   G_TYPE_NONE, 1,
732                   G_TYPE_STRING);
733
734   signals[DELETE_FROM_CURSOR] =
735     g_signal_new (I_("delete_from_cursor"),
736                   G_OBJECT_CLASS_TYPE (gobject_class),
737                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
738                   G_STRUCT_OFFSET (GtkTextViewClass, delete_from_cursor),
739                   NULL, NULL,
740                   _gtk_marshal_VOID__ENUM_INT,
741                   G_TYPE_NONE, 2,
742                   GTK_TYPE_DELETE_TYPE,
743                   G_TYPE_INT);
744
745   signals[BACKSPACE] =
746     g_signal_new (I_("backspace"),
747                   G_OBJECT_CLASS_TYPE (gobject_class),
748                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
749                   G_STRUCT_OFFSET (GtkTextViewClass, backspace),
750                   NULL, NULL,
751                   _gtk_marshal_VOID__VOID,
752                   G_TYPE_NONE, 0);
753
754   signals[CUT_CLIPBOARD] =
755     g_signal_new (I_("cut_clipboard"),
756                   G_OBJECT_CLASS_TYPE (gobject_class),
757                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
758                   G_STRUCT_OFFSET (GtkTextViewClass, cut_clipboard),
759                   NULL, NULL,
760                   _gtk_marshal_VOID__VOID,
761                   G_TYPE_NONE, 0);
762
763   signals[COPY_CLIPBOARD] =
764     g_signal_new (I_("copy_clipboard"),
765                   G_OBJECT_CLASS_TYPE (gobject_class),
766                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
767                   G_STRUCT_OFFSET (GtkTextViewClass, copy_clipboard),
768                   NULL, NULL,
769                   _gtk_marshal_VOID__VOID,
770                   G_TYPE_NONE, 0);
771
772   signals[PASTE_CLIPBOARD] =
773     g_signal_new (I_("paste_clipboard"),
774                   G_OBJECT_CLASS_TYPE (gobject_class),
775                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
776                   G_STRUCT_OFFSET (GtkTextViewClass, paste_clipboard),
777                   NULL, NULL,
778                   _gtk_marshal_VOID__VOID,
779                   G_TYPE_NONE, 0);
780
781   signals[TOGGLE_OVERWRITE] =
782     g_signal_new (I_("toggle_overwrite"),
783                   G_OBJECT_CLASS_TYPE (gobject_class),
784                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
785                   G_STRUCT_OFFSET (GtkTextViewClass, toggle_overwrite),
786                   NULL, NULL,
787                   _gtk_marshal_VOID__VOID,
788                   G_TYPE_NONE, 0);
789
790   signals[MOVE_FOCUS] =
791     g_signal_new (I_("move_focus"),
792                   G_OBJECT_CLASS_TYPE (gobject_class),
793                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
794                   G_STRUCT_OFFSET (GtkTextViewClass, move_focus),
795                   NULL, NULL,
796                   _gtk_marshal_VOID__ENUM,
797                   G_TYPE_NONE, 1,
798                   GTK_TYPE_DIRECTION_TYPE);
799   
800   signals[SET_SCROLL_ADJUSTMENTS] =
801     g_signal_new (I_("set_scroll_adjustments"),
802                   G_OBJECT_CLASS_TYPE (gobject_class),
803                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
804                   G_STRUCT_OFFSET (GtkTextViewClass, set_scroll_adjustments),
805                   NULL, NULL,
806                   _gtk_marshal_VOID__OBJECT_OBJECT,
807                   G_TYPE_NONE, 2,
808                   GTK_TYPE_ADJUSTMENT,
809                   GTK_TYPE_ADJUSTMENT);
810   widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
811
812   signals[POPULATE_POPUP] =
813     g_signal_new (I_("populate_popup"),
814                   G_OBJECT_CLASS_TYPE (gobject_class),
815                   G_SIGNAL_RUN_LAST,
816                   G_STRUCT_OFFSET (GtkTextViewClass, populate_popup),
817                   NULL, NULL,
818                   _gtk_marshal_VOID__OBJECT,
819                   G_TYPE_NONE, 1,
820                   GTK_TYPE_MENU);
821   
822   signals[SELECT_ALL] =
823     _gtk_binding_signal_new (I_("select_all"),
824                              G_OBJECT_CLASS_TYPE (object_class),
825                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
826                              G_CALLBACK (gtk_text_view_select_all),
827                              NULL, NULL,
828                              _gtk_marshal_VOID__BOOLEAN,
829                              G_TYPE_NONE, 1,
830                              G_TYPE_BOOLEAN, TRUE);
831
832   
833   /*
834    * Key bindings
835    */
836
837   binding_set = gtk_binding_set_by_class (klass);
838   
839   /* Moving the insertion point */
840   add_move_binding (binding_set, GDK_Right, 0,
841                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
842
843   add_move_binding (binding_set, GDK_KP_Right, 0,
844                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
845   
846   add_move_binding (binding_set, GDK_Left, 0,
847                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
848
849   add_move_binding (binding_set, GDK_KP_Left, 0,
850                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
851   
852   add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
853                     GTK_MOVEMENT_WORDS, 1);
854
855   add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
856                     GTK_MOVEMENT_WORDS, 1);
857   
858   add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
859                     GTK_MOVEMENT_WORDS, -1);
860
861   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
862                     GTK_MOVEMENT_WORDS, -1);
863   
864   add_move_binding (binding_set, GDK_Up, 0,
865                     GTK_MOVEMENT_DISPLAY_LINES, -1);
866
867   add_move_binding (binding_set, GDK_KP_Up, 0,
868                     GTK_MOVEMENT_DISPLAY_LINES, -1);
869   
870   add_move_binding (binding_set, GDK_Down, 0,
871                     GTK_MOVEMENT_DISPLAY_LINES, 1);
872
873   add_move_binding (binding_set, GDK_KP_Down, 0,
874                     GTK_MOVEMENT_DISPLAY_LINES, 1);
875   
876   add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
877                     GTK_MOVEMENT_PARAGRAPHS, -1);
878
879   add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
880                     GTK_MOVEMENT_PARAGRAPHS, -1);
881   
882   add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
883                     GTK_MOVEMENT_PARAGRAPHS, 1);
884
885   add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
886                     GTK_MOVEMENT_PARAGRAPHS, 1);
887   
888   add_move_binding (binding_set, GDK_Home, 0,
889                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
890
891   add_move_binding (binding_set, GDK_KP_Home, 0,
892                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
893   
894   add_move_binding (binding_set, GDK_End, 0,
895                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
896
897   add_move_binding (binding_set, GDK_KP_End, 0,
898                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
899   
900   add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
901                     GTK_MOVEMENT_BUFFER_ENDS, -1);
902
903   add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
904                     GTK_MOVEMENT_BUFFER_ENDS, -1);
905   
906   add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
907                     GTK_MOVEMENT_BUFFER_ENDS, 1);
908
909   add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
910                     GTK_MOVEMENT_BUFFER_ENDS, 1);
911   
912   add_move_binding (binding_set, GDK_Page_Up, 0,
913                     GTK_MOVEMENT_PAGES, -1);
914
915   add_move_binding (binding_set, GDK_KP_Page_Up, 0,
916                     GTK_MOVEMENT_PAGES, -1);
917   
918   add_move_binding (binding_set, GDK_Page_Down, 0,
919                     GTK_MOVEMENT_PAGES, 1);
920
921   add_move_binding (binding_set, GDK_KP_Page_Down, 0,
922                     GTK_MOVEMENT_PAGES, 1);
923
924   add_move_binding (binding_set, GDK_Page_Up, GDK_CONTROL_MASK,
925                     GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
926
927   add_move_binding (binding_set, GDK_KP_Page_Up, GDK_CONTROL_MASK,
928                     GTK_MOVEMENT_HORIZONTAL_PAGES, -1);
929   
930   add_move_binding (binding_set, GDK_Page_Down, GDK_CONTROL_MASK,
931                     GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
932
933   add_move_binding (binding_set, GDK_KP_Page_Down, GDK_CONTROL_MASK,
934                     GTK_MOVEMENT_HORIZONTAL_PAGES, 1);
935
936   /* Select all */
937   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
938                                 "select_all", 1,
939                                 G_TYPE_BOOLEAN, TRUE);
940
941   gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
942                                 "select_all", 1,
943                                 G_TYPE_BOOLEAN, TRUE);
944   
945   /* Unselect all */
946   gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
947                                  "select_all", 1,
948                                  G_TYPE_BOOLEAN, FALSE);
949
950   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
951                                  "select_all", 1,
952                                  G_TYPE_BOOLEAN, FALSE);
953
954   /* Deleting text */
955   gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
956                                 "delete_from_cursor", 2,
957                                 G_TYPE_ENUM, GTK_DELETE_CHARS,
958                                 G_TYPE_INT, 1);
959
960   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
961                                 "delete_from_cursor", 2,
962                                 G_TYPE_ENUM, GTK_DELETE_CHARS,
963                                 G_TYPE_INT, 1);
964   
965   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
966                                 "backspace", 0);
967
968   /* Make this do the same as Backspace, to help with mis-typing */
969   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_SHIFT_MASK,
970                                 "backspace", 0);
971
972   gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
973                                 "delete_from_cursor", 2,
974                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
975                                 G_TYPE_INT, 1);
976
977   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
978                                 "delete_from_cursor", 2,
979                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
980                                 G_TYPE_INT, 1);
981   
982   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
983                                 "delete_from_cursor", 2,
984                                 G_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
985                                 G_TYPE_INT, -1);
986
987   /* Cut/copy/paste */
988
989   gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
990                                 "cut_clipboard", 0);
991   gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
992                                 "copy_clipboard", 0);
993   gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
994                                 "paste_clipboard", 0);
995
996   gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_SHIFT_MASK,
997                                 "cut_clipboard", 0);
998   gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_CONTROL_MASK,
999                                 "copy_clipboard", 0);
1000   gtk_binding_entry_add_signal (binding_set, GDK_Insert, GDK_SHIFT_MASK,
1001                                 "paste_clipboard", 0);
1002
1003   /* Overwrite */
1004   gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
1005                                 "toggle_overwrite", 0);
1006   gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
1007                                 "toggle_overwrite", 0);
1008
1009   /* Control-tab focus motion */
1010   gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_CONTROL_MASK,
1011                                 "move_focus", 1,
1012                                 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1013   gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_CONTROL_MASK,
1014                                 "move_focus", 1,
1015                                 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_FORWARD);
1016   
1017   gtk_binding_entry_add_signal (binding_set, GDK_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1018                                 "move_focus", 1,
1019                                 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1020   gtk_binding_entry_add_signal (binding_set, GDK_KP_Tab, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
1021                                 "move_focus", 1,
1022                                 GTK_TYPE_DIRECTION_TYPE, GTK_DIR_TAB_BACKWARD);
1023
1024   g_type_class_add_private (gobject_class, sizeof (GtkTextViewPrivate));
1025 }
1026
1027 static void
1028 gtk_text_view_init (GtkTextView *text_view)
1029 {
1030   GtkWidget *widget = GTK_WIDGET (text_view);
1031   GtkTargetList *target_list;
1032
1033   GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1034
1035   /* Set up default style */
1036   text_view->wrap_mode = GTK_WRAP_NONE;
1037   text_view->pixels_above_lines = 0;
1038   text_view->pixels_below_lines = 0;
1039   text_view->pixels_inside_wrap = 0;
1040   text_view->justify = GTK_JUSTIFY_LEFT;
1041   text_view->left_margin = 0;
1042   text_view->right_margin = 0;
1043   text_view->indent = 0;
1044   text_view->tabs = NULL;
1045   text_view->editable = TRUE;
1046
1047   gtk_drag_dest_set (widget, 0, NULL, 0,
1048                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
1049
1050   target_list = gtk_target_list_new (NULL, 0);
1051   gtk_drag_dest_set_target_list (widget, target_list);
1052   gtk_target_list_unref (target_list);
1053
1054   text_view->virtual_cursor_x = -1;
1055   text_view->virtual_cursor_y = -1;
1056
1057   /* This object is completely private. No external entity can gain a reference
1058    * to it; so we create it here and destroy it in finalize ().
1059    */
1060   text_view->im_context = gtk_im_multicontext_new ();
1061
1062   g_signal_connect (text_view->im_context, "commit",
1063                     G_CALLBACK (gtk_text_view_commit_handler), text_view);
1064   g_signal_connect (text_view->im_context, "preedit_changed",
1065                     G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
1066   g_signal_connect (text_view->im_context, "retrieve_surrounding",
1067                     G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
1068   g_signal_connect (text_view->im_context, "delete_surrounding",
1069                     G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
1070
1071   text_view->cursor_visible = TRUE;
1072
1073   text_view->accepts_tab = TRUE;
1074
1075   text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
1076                                             widget, 200, 200);
1077
1078   text_view->drag_start_x = -1;
1079   text_view->drag_start_y = -1;
1080
1081   text_view->pending_place_cursor_button = 0;
1082
1083   /* We handle all our own redrawing */
1084   gtk_widget_set_redraw_on_allocate (widget, FALSE);
1085 }
1086
1087 /**
1088  * gtk_text_view_new:
1089  *
1090  * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
1091  * before using the text view, an empty default buffer will be created
1092  * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
1093  * to specify your own buffer, consider gtk_text_view_new_with_buffer().
1094  *
1095  * Return value: a new #GtkTextView
1096  **/
1097 GtkWidget*
1098 gtk_text_view_new (void)
1099 {
1100   return g_object_new (GTK_TYPE_TEXT_VIEW, NULL);
1101 }
1102
1103 /**
1104  * gtk_text_view_new_with_buffer:
1105  * @buffer: a #GtkTextBuffer
1106  *
1107  * Creates a new #GtkTextView widget displaying the buffer
1108  * @buffer. One buffer can be shared among many widgets.
1109  * @buffer may be NULL to create a default buffer, in which case
1110  * this function is equivalent to gtk_text_view_new(). The
1111  * text view adds its own reference count to the buffer; it does not
1112  * take over an existing reference.
1113  *
1114  * Return value: a new #GtkTextView.
1115  **/
1116 GtkWidget*
1117 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1118 {
1119   GtkTextView *text_view;
1120
1121   text_view = (GtkTextView*)gtk_text_view_new ();
1122
1123   gtk_text_view_set_buffer (text_view, buffer);
1124
1125   return GTK_WIDGET (text_view);
1126 }
1127
1128 /**
1129  * gtk_text_view_set_buffer:
1130  * @text_view: a #GtkTextView
1131  * @buffer: a #GtkTextBuffer
1132  *
1133  * Sets @buffer as the buffer being displayed by @text_view. The previous
1134  * buffer displayed by the text view is unreferenced, and a reference is
1135  * added to @buffer. If you owned a reference to @buffer before passing it
1136  * to this function, you must remove that reference yourself; #GtkTextView
1137  * will not "adopt" it.
1138  *
1139  **/
1140 void
1141 gtk_text_view_set_buffer (GtkTextView   *text_view,
1142                           GtkTextBuffer *buffer)
1143 {
1144   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1145   g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1146
1147   if (text_view->buffer == buffer)
1148     return;
1149
1150   if (text_view->buffer != NULL)
1151     {
1152       /* Destroy all anchored children */
1153       GSList *tmp_list;
1154       GSList *copy;
1155
1156       copy = g_slist_copy (text_view->children);
1157       tmp_list = copy;
1158       while (tmp_list != NULL)
1159         {
1160           GtkTextViewChild *vc = tmp_list->data;
1161
1162           if (vc->anchor)
1163             {
1164               gtk_widget_destroy (vc->widget);
1165               /* vc may now be invalid! */
1166             }
1167
1168           tmp_list = g_slist_next (tmp_list);
1169         }
1170
1171       g_slist_free (copy);
1172
1173       g_signal_handlers_disconnect_by_func (text_view->buffer,
1174                                             gtk_text_view_mark_set_handler,
1175                                             text_view);
1176       g_signal_handlers_disconnect_by_func (text_view->buffer,
1177                                             gtk_text_view_target_list_notify,
1178                                             text_view);
1179       g_object_unref (text_view->buffer);
1180       text_view->dnd_mark = NULL;
1181
1182       if (GTK_WIDGET_REALIZED (text_view))
1183         {
1184           GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1185                                                               GDK_SELECTION_PRIMARY);
1186           gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
1187         }
1188     }
1189
1190   text_view->buffer = buffer;
1191
1192   if (buffer != NULL)
1193     {
1194       GtkTextIter start;
1195
1196       g_object_ref (buffer);
1197
1198       if (text_view->layout)
1199         gtk_text_layout_set_buffer (text_view->layout, buffer);
1200
1201       gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1202
1203       text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1204                                                          "gtk_drag_target",
1205                                                          &start, FALSE);
1206
1207       text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1208                                                                 NULL,
1209                                                                 &start, TRUE);
1210
1211       text_view->first_para_pixels = 0;
1212
1213       g_signal_connect (text_view->buffer, "mark_set",
1214                         G_CALLBACK (gtk_text_view_mark_set_handler),
1215                         text_view);
1216       g_signal_connect (text_view->buffer, "notify::paste-target-list",
1217                         G_CALLBACK (gtk_text_view_target_list_notify),
1218                         text_view);
1219
1220       gtk_text_view_target_list_notify (text_view->buffer, NULL, text_view);
1221
1222       if (GTK_WIDGET_REALIZED (text_view))
1223         {
1224           GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
1225                                                               GDK_SELECTION_PRIMARY);
1226           gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
1227         }
1228     }
1229
1230   g_object_notify (G_OBJECT (text_view), "buffer");
1231   
1232   if (GTK_WIDGET_VISIBLE (text_view))
1233     gtk_widget_queue_draw (GTK_WIDGET (text_view));
1234
1235   DV(g_print ("Invalidating due to set_buffer\n"));
1236   gtk_text_view_invalidate (text_view);
1237 }
1238
1239 static GtkTextBuffer*
1240 get_buffer (GtkTextView *text_view)
1241 {
1242   if (text_view->buffer == NULL)
1243     {
1244       GtkTextBuffer *b;
1245       b = gtk_text_buffer_new (NULL);
1246       gtk_text_view_set_buffer (text_view, b);
1247       g_object_unref (b);
1248     }
1249
1250   return text_view->buffer;
1251 }
1252
1253 /**
1254  * gtk_text_view_get_buffer:
1255  * @text_view: a #GtkTextView
1256  *
1257  * Returns the #GtkTextBuffer being displayed by this text view.
1258  * The reference count on the buffer is not incremented; the caller
1259  * of this function won't own a new reference.
1260  *
1261  * Return value: a #GtkTextBuffer
1262  **/
1263 GtkTextBuffer*
1264 gtk_text_view_get_buffer (GtkTextView *text_view)
1265 {
1266   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1267
1268   return get_buffer (text_view);
1269 }
1270
1271 /**
1272  * gtk_text_view_get_iter_at_location:
1273  * @text_view: a #GtkTextView
1274  * @iter: a #GtkTextIter
1275  * @x: x position, in buffer coordinates
1276  * @y: y position, in buffer coordinates
1277  *
1278  * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1279  * coordinates are coordinates for the entire buffer, not just the
1280  * currently-displayed portion.  If you have coordinates from an
1281  * event, you have to convert those to buffer coordinates with
1282  * gtk_text_view_window_to_buffer_coords().
1283  *
1284  **/
1285 void
1286 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1287                                     GtkTextIter *iter,
1288                                     gint         x,
1289                                     gint         y)
1290 {
1291   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1292   g_return_if_fail (iter != NULL);
1293
1294   gtk_text_view_ensure_layout (text_view);
1295   
1296   gtk_text_layout_get_iter_at_pixel (text_view->layout,
1297                                      iter, x, y);
1298 }
1299
1300 /**
1301  * gtk_text_view_get_iter_at_position:
1302  * @text_view: a #GtkTextView
1303  * @iter: a #GtkTextIter
1304  * @trailing: location to store an integer indicating where
1305  *    in the grapheme the user clicked. It will either be
1306  *    zero, or the number of characters in the grapheme. 
1307  *    0 represents the trailing edge of the grapheme.
1308  * @x: x position, in buffer coordinates
1309  * @y: y position, in buffer coordinates
1310  *
1311  * Retrieves the iterator pointing to the character at buffer 
1312  * coordinates @x and @y. Buffer coordinates are coordinates for 
1313  * the entire buffer, not just the currently-displayed portion.  
1314  * If you have coordinates from an event, you have to convert 
1315  * those to buffer coordinates with 
1316  * gtk_text_view_window_to_buffer_coords().
1317  *
1318  * Note that this is different from gtk_text_view_get_iter_at_location(),
1319  * which returns cursor locations, i.e. positions <emphasis>between</emphasis>
1320  * characters.
1321  *
1322  * Since: 2.6
1323  **/
1324 void
1325 gtk_text_view_get_iter_at_position (GtkTextView *text_view,
1326                                     GtkTextIter *iter,
1327                                     gint        *trailing,
1328                                     gint         x,
1329                                     gint         y)
1330 {
1331   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1332   g_return_if_fail (iter != NULL);
1333
1334   gtk_text_view_ensure_layout (text_view);
1335   
1336   gtk_text_layout_get_iter_at_position (text_view->layout,
1337                                         iter, trailing, x, y);
1338 }
1339
1340 /**
1341  * gtk_text_view_get_iter_location:
1342  * @text_view: a #GtkTextView
1343  * @iter: a #GtkTextIter
1344  * @location: bounds of the character at @iter
1345  *
1346  * Gets a rectangle which roughly contains the character at @iter.
1347  * The rectangle position is in buffer coordinates; use
1348  * gtk_text_view_buffer_to_window_coords() to convert these
1349  * coordinates to coordinates for one of the windows in the text view.
1350  *
1351  **/
1352 void
1353 gtk_text_view_get_iter_location (GtkTextView       *text_view,
1354                                  const GtkTextIter *iter,
1355                                  GdkRectangle      *location)
1356 {
1357   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1358   g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1359
1360   gtk_text_view_ensure_layout (text_view);
1361   
1362   gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1363 }
1364
1365 /**
1366  * gtk_text_view_get_line_yrange:
1367  * @text_view: a #GtkTextView
1368  * @iter: a #GtkTextIter
1369  * @y: return location for a y coordinate
1370  * @height: return location for a height
1371  *
1372  * Gets the y coordinate of the top of the line containing @iter,
1373  * and the height of the line. The coordinate is a buffer coordinate;
1374  * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1375  *
1376  **/
1377 void
1378 gtk_text_view_get_line_yrange (GtkTextView       *text_view,
1379                                const GtkTextIter *iter,
1380                                gint              *y,
1381                                gint              *height)
1382 {
1383   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1384   g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1385
1386   gtk_text_view_ensure_layout (text_view);
1387   
1388   gtk_text_layout_get_line_yrange (text_view->layout,
1389                                    iter,
1390                                    y,
1391                                    height);
1392 }
1393
1394 /**
1395  * gtk_text_view_get_line_at_y:
1396  * @text_view: a #GtkTextView
1397  * @target_iter: a #GtkTextIter
1398  * @y: a y coordinate
1399  * @line_top: return location for top coordinate of the line
1400  *
1401  * Gets the #GtkTextIter at the start of the line containing
1402  * the coordinate @y. @y is in buffer coordinates, convert from
1403  * window coordinates with gtk_text_view_window_to_buffer_coords().
1404  * If non-%NULL, @line_top will be filled with the coordinate of the top
1405  * edge of the line.
1406  **/
1407 void
1408 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1409                              GtkTextIter *target_iter,
1410                              gint         y,
1411                              gint        *line_top)
1412 {
1413   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1414
1415   gtk_text_view_ensure_layout (text_view);
1416   
1417   gtk_text_layout_get_line_at_y (text_view->layout,
1418                                  target_iter,
1419                                  y,
1420                                  line_top);
1421 }
1422
1423 static gboolean
1424 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1425 {
1426   DV (g_print ("  Setting adj to raw value %g\n", val));
1427   
1428   /* We don't really want to clamp to upper; we want to clamp to
1429      upper - page_size which is the highest value the scrollbar
1430      will let us reach. */
1431   if (val > (adj->upper - adj->page_size))
1432     val = adj->upper - adj->page_size;
1433
1434   if (val < adj->lower)
1435     val = adj->lower;
1436
1437   if (val != adj->value)
1438     {
1439       DV (g_print ("  Setting adj to clamped value %g\n", val));
1440       gtk_adjustment_set_value (adj, val);
1441       return TRUE;
1442     }
1443   else
1444     return FALSE;
1445 }
1446
1447 /**
1448  * gtk_text_view_scroll_to_iter:
1449  * @text_view: a #GtkTextView
1450  * @iter: a #GtkTextIter
1451  * @within_margin: margin as a [0.0,0.5) fraction of screen size
1452  * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1453  * @xalign: horizontal alignment of mark within visible area.
1454  * @yalign: vertical alignment of mark within visible area
1455  *
1456  * Scrolls @text_view so that @iter is on the screen in the position
1457  * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1458  * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1459  * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1460  * possibly not scrolling at all. The effective screen for purposes
1461  * of this function is reduced by a margin of size @within_margin.
1462  * NOTE: This function uses the currently-computed height of the
1463  * lines in the text buffer. Note that line heights are computed
1464  * in an idle handler; so this function may not have the desired effect
1465  * if it's called before the height computations. To avoid oddness,
1466  * consider using gtk_text_view_scroll_to_mark() which saves a point
1467  * to be scrolled to after line validation.
1468  *
1469  * Return value: %TRUE if scrolling occurred
1470  **/
1471 gboolean
1472 gtk_text_view_scroll_to_iter (GtkTextView   *text_view,
1473                               GtkTextIter   *iter,
1474                               gdouble        within_margin,
1475                               gboolean       use_align,
1476                               gdouble        xalign,
1477                               gdouble        yalign)
1478 {
1479   GdkRectangle rect;
1480   GdkRectangle screen;
1481   gint screen_bottom;
1482   gint screen_right;
1483   gint scroll_dest;
1484   GtkWidget *widget;
1485   gboolean retval = FALSE;
1486   gint scroll_inc;
1487   gint screen_xoffset, screen_yoffset;
1488   gint current_x_scroll, current_y_scroll;
1489
1490   /* FIXME why don't we do the validate-at-scroll-destination thing
1491    * from flush_scroll in this function? I think it wasn't done before
1492    * because changed_handler was screwed up, but I could be wrong.
1493    */
1494   
1495   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1496   g_return_val_if_fail (iter != NULL, FALSE);
1497   g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1498   g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1499   g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1500   
1501   widget = GTK_WIDGET (text_view);
1502
1503   DV(g_print(G_STRLOC"\n"));
1504   
1505   gtk_text_layout_get_iter_location (text_view->layout,
1506                                      iter,
1507                                      &rect);
1508
1509   DV (g_print (" target rect %d,%d  %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1510   
1511   current_x_scroll = text_view->xoffset;
1512   current_y_scroll = text_view->yoffset;
1513
1514   screen.x = current_x_scroll;
1515   screen.y = current_y_scroll;
1516   screen.width = SCREEN_WIDTH (widget);
1517   screen.height = SCREEN_HEIGHT (widget);
1518   
1519   screen_xoffset = screen.width * within_margin;
1520   screen_yoffset = screen.height * within_margin;
1521   
1522   screen.x += screen_xoffset;
1523   screen.y += screen_yoffset;
1524   screen.width -= screen_xoffset * 2;
1525   screen.height -= screen_yoffset * 2;
1526
1527   /* paranoia check */
1528   if (screen.width < 1)
1529     screen.width = 1;
1530   if (screen.height < 1)
1531     screen.height = 1;
1532   
1533   /* The -1 here ensures that we leave enough space to draw the cursor
1534    * when this function is used for horizontal scrolling. 
1535    */
1536   screen_right = screen.x + screen.width - 1;
1537   screen_bottom = screen.y + screen.height;
1538   
1539   /* The alignment affects the point in the target character that we
1540    * choose to align. If we're doing right/bottom alignment, we align
1541    * the right/bottom edge of the character the mark is at; if we're
1542    * doing left/top we align the left/top edge of the character; if
1543    * we're doing center alignment we align the center of the
1544    * character.
1545    */
1546   
1547   /* Vertical scroll */
1548
1549   scroll_inc = 0;
1550   scroll_dest = current_y_scroll;
1551   
1552   if (use_align)
1553     {      
1554       scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1555       
1556       /* if scroll_dest < screen.y, we move a negative increment (up),
1557        * else a positive increment (down)
1558        */
1559       scroll_inc = scroll_dest - screen.y + screen_yoffset;
1560     }
1561   else
1562     {
1563       /* move minimum to get onscreen */
1564       if (rect.y < screen.y)
1565         {
1566           scroll_dest = rect.y;
1567           scroll_inc = scroll_dest - screen.y - screen_yoffset;
1568         }
1569       else if ((rect.y + rect.height) > screen_bottom)
1570         {
1571           scroll_dest = rect.y + rect.height;
1572           scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1573         }
1574     }  
1575   
1576   if (scroll_inc != 0)
1577     {
1578       retval = set_adjustment_clamped (get_vadjustment (text_view),
1579                                        current_y_scroll + scroll_inc);
1580
1581       DV (g_print (" vert increment %d\n", scroll_inc));
1582     }
1583
1584   /* Horizontal scroll */
1585   
1586   scroll_inc = 0;
1587   scroll_dest = current_x_scroll;
1588   
1589   if (use_align)
1590     {      
1591       scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1592
1593       /* if scroll_dest < screen.y, we move a negative increment (left),
1594        * else a positive increment (right)
1595        */
1596       scroll_inc = scroll_dest - screen.x + screen_xoffset;
1597     }
1598   else
1599     {
1600       /* move minimum to get onscreen */
1601       if (rect.x < screen.x)
1602         {
1603           scroll_dest = rect.x;
1604           scroll_inc = scroll_dest - screen.x - screen_xoffset;
1605         }
1606       else if ((rect.x + rect.width) > screen_right)
1607         {
1608           scroll_dest = rect.x + rect.width;
1609           scroll_inc = scroll_dest - screen_right + screen_xoffset;
1610         }
1611     }
1612   
1613   if (scroll_inc != 0)
1614     {
1615       retval = set_adjustment_clamped (get_hadjustment (text_view),
1616                                        current_x_scroll + scroll_inc);
1617
1618       DV (g_print (" horiz increment %d\n", scroll_inc));
1619     }
1620   
1621   if (retval)
1622     DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1623   else
1624     DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1625   
1626   return retval;
1627 }
1628
1629 static void
1630 free_pending_scroll (GtkTextPendingScroll *scroll)
1631 {
1632   if (!gtk_text_mark_get_deleted (scroll->mark))
1633     gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1634                                  scroll->mark);
1635   g_object_unref (scroll->mark);
1636   g_free (scroll);
1637 }
1638
1639 static void
1640 cancel_pending_scroll (GtkTextView *text_view)
1641 {
1642   if (text_view->pending_scroll)
1643     {
1644       free_pending_scroll (text_view->pending_scroll);
1645       text_view->pending_scroll = NULL;
1646     }
1647 }
1648     
1649 static void
1650 gtk_text_view_queue_scroll (GtkTextView   *text_view,
1651                             GtkTextMark   *mark,
1652                             gdouble        within_margin,
1653                             gboolean       use_align,
1654                             gdouble        xalign,
1655                             gdouble        yalign)
1656 {
1657   GtkTextIter iter;
1658   GtkTextPendingScroll *scroll;
1659
1660   DV(g_print(G_STRLOC"\n"));
1661   
1662   scroll = g_new (GtkTextPendingScroll, 1);
1663
1664   scroll->within_margin = within_margin;
1665   scroll->use_align = use_align;
1666   scroll->xalign = xalign;
1667   scroll->yalign = yalign;
1668   
1669   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1670
1671   scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1672                                               NULL,
1673                                               &iter,
1674                                               gtk_text_mark_get_left_gravity (mark));
1675
1676   g_object_ref (scroll->mark);
1677   
1678   cancel_pending_scroll (text_view);
1679
1680   text_view->pending_scroll = scroll;
1681 }
1682
1683 static gboolean
1684 gtk_text_view_flush_scroll (GtkTextView *text_view)
1685 {
1686   GtkTextIter iter;
1687   GtkTextPendingScroll *scroll;
1688   gboolean retval;
1689   GtkWidget *widget;
1690
1691   widget = GTK_WIDGET (text_view);
1692   
1693   DV(g_print(G_STRLOC"\n"));
1694   
1695   if (text_view->pending_scroll == NULL)
1696     {
1697       DV (g_print ("in flush scroll, no pending scroll\n"));
1698       return FALSE;
1699     }
1700
1701   scroll = text_view->pending_scroll;
1702
1703   /* avoid recursion */
1704   text_view->pending_scroll = NULL;
1705   
1706   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1707
1708   /* Validate area around the scroll destination, so the adjustment
1709    * can meaningfully point into that area. We must validate
1710    * enough area to be sure that after we scroll, everything onscreen
1711    * is valid; otherwise, validation will maintain the first para
1712    * in one place, but may push the target iter off the bottom of
1713    * the screen.
1714    */
1715   DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1716   gtk_text_layout_validate_yrange (text_view->layout, &iter,
1717                                    - (widget->allocation.height * 2),
1718                                    widget->allocation.height * 2);
1719   
1720   DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1721
1722   /* Ensure we have updated width/height */
1723   gtk_text_view_update_adjustments (text_view);
1724   
1725   retval = gtk_text_view_scroll_to_iter (text_view,
1726                                          &iter,
1727                                          scroll->within_margin,
1728                                          scroll->use_align,
1729                                          scroll->xalign,
1730                                          scroll->yalign);
1731
1732   free_pending_scroll (scroll);
1733
1734   return retval;
1735 }
1736
1737 static void
1738 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1739 {  
1740   if (upper != adj->upper)
1741     {
1742       gdouble min = MAX (0.0, upper - adj->page_size);
1743       gboolean value_changed = FALSE;
1744
1745       adj->upper = upper;
1746
1747       if (adj->value > min)
1748         {
1749           adj->value = min;
1750           value_changed = TRUE;
1751         }
1752
1753       gtk_adjustment_changed (adj);
1754       DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1755       
1756       if (value_changed)
1757         {
1758           DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1759           gtk_adjustment_value_changed (adj);
1760         }
1761     }
1762 }
1763
1764 static void
1765 gtk_text_view_update_adjustments (GtkTextView *text_view)
1766 {
1767   gint width = 0, height = 0;
1768
1769   DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1770
1771   if (text_view->layout)
1772     gtk_text_layout_get_size (text_view->layout, &width, &height);
1773
1774   /* Make room for the cursor after the last character in the widest line */
1775   width += SPACE_FOR_CURSOR;
1776
1777   if (text_view->width != width || text_view->height != height)
1778     {
1779       if (text_view->width != width)
1780         text_view->width_changed = TRUE;
1781
1782       text_view->width = width;
1783       text_view->height = height;
1784
1785       gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1786                                           MAX (SCREEN_WIDTH (text_view), width));
1787       gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1788                                           MAX (SCREEN_HEIGHT (text_view), height));
1789       
1790       /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1791
1792       /* Set up the step sizes; we'll say that a page is
1793          our allocation minus one step, and a step is
1794          1/10 of our allocation. */
1795       text_view->hadjustment->step_increment =
1796         SCREEN_WIDTH (text_view) / 10.0;
1797       text_view->hadjustment->page_increment =
1798         SCREEN_WIDTH (text_view) * 0.9;
1799       
1800       text_view->vadjustment->step_increment =
1801         SCREEN_HEIGHT (text_view) / 10.0;
1802       text_view->vadjustment->page_increment =
1803         SCREEN_HEIGHT (text_view) * 0.9;
1804
1805       gtk_adjustment_changed (get_hadjustment (text_view));
1806       gtk_adjustment_changed (get_vadjustment (text_view));
1807     }
1808 }
1809
1810 static void
1811 gtk_text_view_update_layout_width (GtkTextView *text_view)
1812 {
1813   DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1814   
1815   gtk_text_view_ensure_layout (text_view);
1816
1817   gtk_text_layout_set_screen_width (text_view->layout,
1818                                     MAX (1, SCREEN_WIDTH (text_view) - SPACE_FOR_CURSOR));
1819 }
1820
1821 static void
1822 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1823 {
1824   GdkRectangle area;
1825
1826   if (text_view->layout == NULL)
1827     return;
1828   
1829   gtk_text_view_get_cursor_location (text_view, &area);
1830
1831   area.x -= text_view->xoffset;
1832   area.y -= text_view->yoffset;
1833     
1834   /* Width returned by Pango indicates direction of cursor,
1835    * by it's sign more than the size of cursor.
1836    */
1837   area.width = 0;
1838
1839   gtk_im_context_set_cursor_location (text_view->im_context, &area);
1840 }
1841
1842 /**
1843  * gtk_text_view_scroll_to_mark:
1844  * @text_view: a #GtkTextView
1845  * @mark: a #GtkTextMark
1846  * @within_margin: margin as a [0.0,0.5) fraction of screen size
1847  * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1848  * @xalign: horizontal alignment of mark within visible area.
1849  * @yalign: vertical alignment of mark within visible area
1850  *
1851  * Scrolls @text_view so that @mark is on the screen in the position
1852  * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1853  * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1854  * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1855  * possibly not scrolling at all. The effective screen for purposes
1856  * of this function is reduced by a margin of size @within_margin.
1857  *
1858  **/
1859 void
1860 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1861                               GtkTextMark *mark,
1862                               gdouble      within_margin,
1863                               gboolean     use_align,
1864                               gdouble      xalign,
1865                               gdouble      yalign)
1866 {  
1867   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1868   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1869   g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1870   g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1871   g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1872
1873   gtk_text_view_queue_scroll (text_view, mark,
1874                               within_margin,
1875                               use_align,
1876                               xalign,
1877                               yalign);
1878
1879   /* If no validation is pending, we need to go ahead and force an
1880    * immediate scroll.
1881    */
1882   if (text_view->layout &&
1883       gtk_text_layout_is_valid (text_view->layout))
1884     gtk_text_view_flush_scroll (text_view);
1885 }
1886
1887 /**
1888  * gtk_text_view_scroll_mark_onscreen:
1889  * @text_view: a #GtkTextView
1890  * @mark: a mark in the buffer for @text_view
1891  * 
1892  * Scrolls @text_view the minimum distance such that @mark is contained
1893  * within the visible area of the widget.
1894  * 
1895  **/
1896 void
1897 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1898                                     GtkTextMark *mark)
1899 {
1900   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1901   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1902
1903   gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1904 }
1905
1906 static gboolean
1907 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1908 {
1909   GdkRectangle visible_rect;
1910   gtk_text_view_get_visible_rect (text_view, &visible_rect);
1911
1912   return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1913                                                visible_rect.y,
1914                                                visible_rect.y + visible_rect.height);
1915 }
1916
1917 /**
1918  * gtk_text_view_move_mark_onscreen:
1919  * @text_view: a #GtkTextView
1920  * @mark: a #GtkTextMark
1921  *
1922  * Moves a mark within the buffer so that it's
1923  * located within the currently-visible text area.
1924  *
1925  * Return value: %TRUE if the mark moved (wasn't already onscreen)
1926  **/
1927 gboolean
1928 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1929                                   GtkTextMark *mark)
1930 {
1931   GtkTextIter iter;
1932
1933   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1934   g_return_val_if_fail (mark != NULL, FALSE);
1935
1936   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1937
1938   if (clamp_iter_onscreen (text_view, &iter))
1939     {
1940       gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1941       return TRUE;
1942     }
1943   else
1944     return FALSE;
1945 }
1946
1947 /**
1948  * gtk_text_view_get_visible_rect:
1949  * @text_view: a #GtkTextView
1950  * @visible_rect: rectangle to fill
1951  *
1952  * Fills @visible_rect with the currently-visible
1953  * region of the buffer, in buffer coordinates. Convert to window coordinates
1954  * with gtk_text_view_buffer_to_window_coords().
1955  **/
1956 void
1957 gtk_text_view_get_visible_rect (GtkTextView  *text_view,
1958                                 GdkRectangle *visible_rect)
1959 {
1960   GtkWidget *widget;
1961
1962   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1963
1964   widget = GTK_WIDGET (text_view);
1965
1966   if (visible_rect)
1967     {
1968       visible_rect->x = text_view->xoffset;
1969       visible_rect->y = text_view->yoffset;
1970       visible_rect->width = SCREEN_WIDTH (widget);
1971       visible_rect->height = SCREEN_HEIGHT (widget);
1972
1973       DV(g_print(" visible rect: %d,%d %d x %d\n",
1974                  visible_rect->x,
1975                  visible_rect->y,
1976                  visible_rect->width,
1977                  visible_rect->height));
1978     }
1979 }
1980
1981 /**
1982  * gtk_text_view_set_wrap_mode:
1983  * @text_view: a #GtkTextView
1984  * @wrap_mode: a #GtkWrapMode
1985  *
1986  * Sets the line wrapping for the view.
1987  **/
1988 void
1989 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1990                              GtkWrapMode  wrap_mode)
1991 {
1992   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1993
1994   if (text_view->wrap_mode != wrap_mode)
1995     {
1996       text_view->wrap_mode = wrap_mode;
1997
1998       if (text_view->layout)
1999         {
2000           text_view->layout->default_style->wrap_mode = wrap_mode;
2001           gtk_text_layout_default_style_changed (text_view->layout);
2002         }
2003     }
2004
2005   g_object_notify (G_OBJECT (text_view), "wrap-mode");
2006 }
2007
2008 /**
2009  * gtk_text_view_get_wrap_mode:
2010  * @text_view: a #GtkTextView
2011  *
2012  * Gets the line wrapping for the view.
2013  *
2014  * Return value: the line wrap setting
2015  **/
2016 GtkWrapMode
2017 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
2018 {
2019   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
2020
2021   return text_view->wrap_mode;
2022 }
2023
2024 /**
2025  * gtk_text_view_set_editable:
2026  * @text_view: a #GtkTextView
2027  * @setting: whether it's editable
2028  *
2029  * Sets the default editability of the #GtkTextView. You can override
2030  * this default setting with tags in the buffer, using the "editable"
2031  * attribute of tags.
2032  **/
2033 void
2034 gtk_text_view_set_editable (GtkTextView *text_view,
2035                             gboolean     setting)
2036 {
2037   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2038   setting = setting != FALSE;
2039
2040   if (text_view->editable != setting)
2041     {
2042       text_view->editable = setting;
2043
2044       if (text_view->layout)
2045         {
2046           text_view->layout->default_style->editable = text_view->editable;
2047           gtk_text_layout_default_style_changed (text_view->layout);
2048         }
2049
2050       g_object_notify (G_OBJECT (text_view), "editable");
2051     }
2052 }
2053
2054 /**
2055  * gtk_text_view_get_editable:
2056  * @text_view: a #GtkTextView
2057  *
2058  * Returns the default editability of the #GtkTextView. Tags in the
2059  * buffer may override this setting for some ranges of text.
2060  *
2061  * Return value: whether text is editable by default
2062  **/
2063 gboolean
2064 gtk_text_view_get_editable (GtkTextView *text_view)
2065 {
2066   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2067
2068   return text_view->editable;
2069 }
2070
2071 /**
2072  * gtk_text_view_set_pixels_above_lines:
2073  * @text_view: a #GtkTextView
2074  * @pixels_above_lines: pixels above paragraphs
2075  * 
2076  * Sets the default number of blank pixels above paragraphs in @text_view.
2077  * Tags in the buffer for @text_view may override the defaults.
2078  * 
2079  **/
2080 void
2081 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
2082                                       gint         pixels_above_lines)
2083 {
2084   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2085
2086   if (text_view->pixels_above_lines != pixels_above_lines)
2087     {
2088       text_view->pixels_above_lines = pixels_above_lines;
2089
2090       if (text_view->layout)
2091         {
2092           text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
2093           gtk_text_layout_default_style_changed (text_view->layout);
2094         }
2095
2096       g_object_notify (G_OBJECT (text_view), "pixels-above-lines");
2097     }
2098 }
2099
2100 /**
2101  * gtk_text_view_get_pixels_above_lines:
2102  * @text_view: a #GtkTextView
2103  * 
2104  * Gets the default number of pixels to put above paragraphs.
2105  * 
2106  * Return value: default number of pixels above paragraphs
2107  **/
2108 gint
2109 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
2110 {
2111   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2112
2113   return text_view->pixels_above_lines;
2114 }
2115
2116 /**
2117  * gtk_text_view_set_pixels_below_lines:
2118  * @text_view: a #GtkTextView
2119  * @pixels_below_lines: pixels below paragraphs 
2120  *
2121  * Sets the default number of pixels of blank space
2122  * to put below paragraphs in @text_view. May be overridden
2123  * by tags applied to @text_view's buffer. 
2124  * 
2125  **/
2126 void
2127 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
2128                                       gint         pixels_below_lines)
2129 {
2130   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2131
2132   if (text_view->pixels_below_lines != pixels_below_lines)
2133     {
2134       text_view->pixels_below_lines = pixels_below_lines;
2135
2136       if (text_view->layout)
2137         {
2138           text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
2139           gtk_text_layout_default_style_changed (text_view->layout);
2140         }
2141
2142       g_object_notify (G_OBJECT (text_view), "pixels-below-lines");
2143     }
2144 }
2145
2146 /**
2147  * gtk_text_view_get_pixels_below_lines:
2148  * @text_view: a #GtkTextView
2149  * 
2150  * Gets the value set by gtk_text_view_set_pixels_below_lines().
2151  * 
2152  * Return value: default number of blank pixels below paragraphs
2153  **/
2154 gint
2155 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
2156 {
2157   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2158
2159   return text_view->pixels_below_lines;
2160 }
2161
2162 /**
2163  * gtk_text_view_set_pixels_inside_wrap:
2164  * @text_view: a #GtkTextView
2165  * @pixels_inside_wrap: default number of pixels between wrapped lines
2166  *
2167  * Sets the default number of pixels of blank space to leave between
2168  * display/wrapped lines within a paragraph. May be overridden by
2169  * tags in @text_view's buffer.
2170  * 
2171  **/
2172 void
2173 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
2174                                       gint         pixels_inside_wrap)
2175 {
2176   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2177
2178   if (text_view->pixels_inside_wrap != pixels_inside_wrap)
2179     {
2180       text_view->pixels_inside_wrap = pixels_inside_wrap;
2181
2182       if (text_view->layout)
2183         {
2184           text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
2185           gtk_text_layout_default_style_changed (text_view->layout);
2186         }
2187
2188       g_object_notify (G_OBJECT (text_view), "pixels-inside-wrap");
2189     }
2190 }
2191
2192 /**
2193  * gtk_text_view_get_pixels_inside_wrap:
2194  * @text_view: a #GtkTextView
2195  * 
2196  * Gets the value set by gtk_text_view_set_pixels_inside_wrap().
2197  * 
2198  * Return value: default number of pixels of blank space between wrapped lines
2199  **/
2200 gint
2201 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
2202 {
2203   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2204
2205   return text_view->pixels_inside_wrap;
2206 }
2207
2208 /**
2209  * gtk_text_view_set_justification:
2210  * @text_view: a #GtkTextView
2211  * @justification: justification
2212  *
2213  * Sets the default justification of text in @text_view.
2214  * Tags in the view's buffer may override the default.
2215  * 
2216  **/
2217 void
2218 gtk_text_view_set_justification (GtkTextView     *text_view,
2219                                  GtkJustification justification)
2220 {
2221   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2222
2223   if (text_view->justify != justification)
2224     {
2225       text_view->justify = justification;
2226
2227       if (text_view->layout)
2228         {
2229           text_view->layout->default_style->justification = justification;
2230           gtk_text_layout_default_style_changed (text_view->layout);
2231         }
2232
2233       g_object_notify (G_OBJECT (text_view), "justification");
2234     }
2235 }
2236
2237 /**
2238  * gtk_text_view_get_justification:
2239  * @text_view: a #GtkTextView
2240  * 
2241  * Gets the default justification of paragraphs in @text_view.
2242  * Tags in the buffer may override the default.
2243  * 
2244  * Return value: default justification
2245  **/
2246 GtkJustification
2247 gtk_text_view_get_justification (GtkTextView *text_view)
2248 {
2249   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
2250
2251   return text_view->justify;
2252 }
2253
2254 /**
2255  * gtk_text_view_set_left_margin:
2256  * @text_view: a #GtkTextView
2257  * @left_margin: left margin in pixels
2258  * 
2259  * Sets the default left margin for text in @text_view.
2260  * Tags in the buffer may override the default.
2261  * 
2262  **/
2263 void
2264 gtk_text_view_set_left_margin (GtkTextView *text_view,
2265                                gint         left_margin)
2266 {
2267   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2268
2269   if (text_view->left_margin != left_margin)
2270     {
2271       text_view->left_margin = left_margin;
2272
2273       if (text_view->layout)
2274         {
2275           text_view->layout->default_style->left_margin = left_margin;
2276           gtk_text_layout_default_style_changed (text_view->layout);
2277         }
2278
2279       g_object_notify (G_OBJECT (text_view), "left-margin");
2280     }
2281 }
2282
2283 /**
2284  * gtk_text_view_get_left_margin:
2285  * @text_view: a #GtkTextView
2286  * 
2287  * Gets the default left margin size of paragraphs in the @text_view.
2288  * Tags in the buffer may override the default.
2289  * 
2290  * Return value: left margin in pixels
2291  **/
2292 gint
2293 gtk_text_view_get_left_margin (GtkTextView *text_view)
2294 {
2295   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2296
2297   return text_view->left_margin;
2298 }
2299
2300 /**
2301  * gtk_text_view_set_right_margin:
2302  * @text_view: a #GtkTextView
2303  * @right_margin: right margin in pixels
2304  *
2305  * Sets the default right margin for text in the text view.
2306  * Tags in the buffer may override the default.
2307  * 
2308  **/
2309 void
2310 gtk_text_view_set_right_margin (GtkTextView *text_view,
2311                                 gint         right_margin)
2312 {
2313   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2314
2315   if (text_view->right_margin != right_margin)
2316     {
2317       text_view->right_margin = right_margin;
2318
2319       if (text_view->layout)
2320         {
2321           text_view->layout->default_style->right_margin = right_margin;
2322           gtk_text_layout_default_style_changed (text_view->layout);
2323         }
2324
2325       g_object_notify (G_OBJECT (text_view), "right-margin");
2326     }
2327 }
2328
2329 /**
2330  * gtk_text_view_get_right_margin:
2331  * @text_view: a #GtkTextView
2332  * 
2333  * Gets the default right margin for text in @text_view. Tags
2334  * in the buffer may override the default.
2335  * 
2336  * Return value: right margin in pixels
2337  **/
2338 gint
2339 gtk_text_view_get_right_margin (GtkTextView *text_view)
2340 {
2341   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2342
2343   return text_view->right_margin;
2344 }
2345
2346 /**
2347  * gtk_text_view_set_indent:
2348  * @text_view: a #GtkTextView
2349  * @indent: indentation in pixels
2350  *
2351  * Sets the default indentation for paragraphs in @text_view.
2352  * Tags in the buffer may override the default.
2353  * 
2354  **/
2355 void
2356 gtk_text_view_set_indent (GtkTextView *text_view,
2357                           gint         indent)
2358 {
2359   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2360
2361   if (text_view->indent != indent)
2362     {
2363       text_view->indent = indent;
2364
2365       if (text_view->layout)
2366         {
2367           text_view->layout->default_style->indent = indent;
2368           gtk_text_layout_default_style_changed (text_view->layout);
2369         }
2370
2371       g_object_notify (G_OBJECT (text_view), "indent");
2372     }
2373 }
2374
2375 /**
2376  * gtk_text_view_get_indent:
2377  * @text_view: a #GtkTextView
2378  * 
2379  * Gets the default indentation of paragraphs in @text_view.
2380  * Tags in the view's buffer may override the default.
2381  * The indentation may be negative.
2382  * 
2383  * Return value: number of pixels of indentation
2384  **/
2385 gint
2386 gtk_text_view_get_indent (GtkTextView *text_view)
2387 {
2388   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2389
2390   return text_view->indent;
2391 }
2392
2393 /**
2394  * gtk_text_view_set_tabs:
2395  * @text_view: a #GtkTextView
2396  * @tabs: tabs as a #PangoTabArray
2397  *
2398  * Sets the default tab stops for paragraphs in @text_view.
2399  * Tags in the buffer may override the default.
2400  * 
2401  **/
2402 void
2403 gtk_text_view_set_tabs (GtkTextView   *text_view,
2404                         PangoTabArray *tabs)
2405 {
2406   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2407
2408   if (text_view->tabs)
2409     pango_tab_array_free (text_view->tabs);
2410
2411   text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2412
2413   if (text_view->layout)
2414     {
2415       /* some unkosher futzing in internal struct details... */
2416       if (text_view->layout->default_style->tabs)
2417         pango_tab_array_free (text_view->layout->default_style->tabs);
2418
2419       text_view->layout->default_style->tabs =
2420         text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2421
2422       gtk_text_layout_default_style_changed (text_view->layout);
2423     }
2424
2425   g_object_notify (G_OBJECT (text_view), "tabs");
2426 }
2427
2428 /**
2429  * gtk_text_view_get_tabs:
2430  * @text_view: a #GtkTextView
2431  * 
2432  * Gets the default tabs for @text_view. Tags in the buffer may
2433  * override the defaults. The returned array will be %NULL if
2434  * "standard" (8-space) tabs are used. Free the return value
2435  * with pango_tab_array_free().
2436  * 
2437  * Return value: copy of default tab array, or %NULL if "standard" tabs are used; must be freed with pango_tab_array_free().
2438  **/
2439 PangoTabArray*
2440 gtk_text_view_get_tabs (GtkTextView *text_view)
2441 {
2442   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2443
2444   return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2445 }
2446
2447 /**
2448  * gtk_text_view_set_cursor_visible:
2449  * @text_view: a #GtkTextView
2450  * @setting: whether to show the insertion cursor
2451  *
2452  * Toggles whether the insertion point is displayed. A buffer with no editable
2453  * text probably shouldn't have a visible cursor, so you may want to turn
2454  * the cursor off.
2455  **/
2456 void
2457 gtk_text_view_set_cursor_visible (GtkTextView *text_view,
2458                                   gboolean     setting)
2459 {
2460   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2461
2462   setting = (setting != FALSE);
2463
2464   if (text_view->cursor_visible != setting)
2465     {
2466       text_view->cursor_visible = setting;
2467
2468       if (GTK_WIDGET_HAS_FOCUS (text_view))
2469         {
2470           if (text_view->layout)
2471             {
2472               gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2473               gtk_text_view_check_cursor_blink (text_view);
2474             }
2475         }
2476
2477       g_object_notify (G_OBJECT (text_view), "cursor-visible");
2478     }
2479 }
2480
2481 /**
2482  * gtk_text_view_get_cursor_visible:
2483  * @text_view: a #GtkTextView
2484  *
2485  * Find out whether the cursor is being displayed.
2486  *
2487  * Return value: whether the insertion mark is visible
2488  **/
2489 gboolean
2490 gtk_text_view_get_cursor_visible (GtkTextView *text_view)
2491 {
2492   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2493
2494   return text_view->cursor_visible;
2495 }
2496
2497
2498 /**
2499  * gtk_text_view_place_cursor_onscreen:
2500  * @text_view: a #GtkTextView
2501  *
2502  * Moves the cursor to the currently visible region of the
2503  * buffer, it it isn't there already.
2504  *
2505  * Return value: TRUE if the cursor had to be moved.
2506  **/
2507 gboolean
2508 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2509 {
2510   GtkTextIter insert;
2511
2512   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2513
2514   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2515                                     gtk_text_buffer_get_mark (get_buffer (text_view),
2516                                                               "insert"));
2517
2518   if (clamp_iter_onscreen (text_view, &insert))
2519     {
2520       gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2521       return TRUE;
2522     }
2523   else
2524     return FALSE;
2525 }
2526
2527 static void
2528 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2529 {
2530   if (text_view->first_validate_idle != 0)
2531     {
2532       DV (g_print ("Removing first validate idle: %s\n", G_STRLOC));
2533       g_source_remove (text_view->first_validate_idle);
2534       text_view->first_validate_idle = 0;
2535     }
2536
2537   if (text_view->incremental_validate_idle != 0)
2538     {
2539       g_source_remove (text_view->incremental_validate_idle);
2540       text_view->incremental_validate_idle = 0;
2541     }
2542 }
2543
2544 static void
2545 gtk_text_view_destroy (GtkObject *object)
2546 {
2547   GtkTextView *text_view;
2548   
2549   text_view = GTK_TEXT_VIEW (object);
2550
2551   gtk_text_view_remove_validate_idles (text_view);
2552   gtk_text_view_set_buffer (text_view, NULL);
2553   gtk_text_view_destroy_layout (text_view);
2554
2555   (* GTK_OBJECT_CLASS (gtk_text_view_parent_class)->destroy) (object);
2556 }
2557
2558 static void
2559 gtk_text_view_finalize (GObject *object)
2560 {
2561   GtkTextView *text_view;
2562
2563   text_view = GTK_TEXT_VIEW (object);
2564
2565   g_assert (text_view->buffer == NULL);
2566
2567   gtk_text_view_destroy_layout (text_view);
2568   gtk_text_view_set_buffer (text_view, NULL);
2569   
2570   cancel_pending_scroll (text_view);
2571
2572   if (text_view->tabs)
2573     pango_tab_array_free (text_view->tabs);
2574   
2575   if (text_view->hadjustment)
2576     g_object_unref (text_view->hadjustment);
2577   if (text_view->vadjustment)
2578     g_object_unref (text_view->vadjustment);
2579
2580   text_window_free (text_view->text_window);
2581
2582   if (text_view->left_window)
2583     text_window_free (text_view->left_window);
2584
2585   if (text_view->top_window)
2586     text_window_free (text_view->top_window);
2587
2588   if (text_view->right_window)
2589     text_window_free (text_view->right_window);
2590
2591   if (text_view->bottom_window)
2592     text_window_free (text_view->bottom_window);
2593
2594   g_object_unref (text_view->im_context);
2595   
2596   (* G_OBJECT_CLASS (gtk_text_view_parent_class)->finalize) (object);
2597 }
2598
2599 static void
2600 gtk_text_view_set_property (GObject         *object,
2601                             guint            prop_id,
2602                             const GValue    *value,
2603                             GParamSpec      *pspec)
2604 {
2605   GtkTextView *text_view;
2606
2607   text_view = GTK_TEXT_VIEW (object);
2608
2609   switch (prop_id)
2610     {
2611     case PROP_PIXELS_ABOVE_LINES:
2612       gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2613       break;
2614
2615     case PROP_PIXELS_BELOW_LINES:
2616       gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2617       break;
2618
2619     case PROP_PIXELS_INSIDE_WRAP:
2620       gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2621       break;
2622
2623     case PROP_EDITABLE:
2624       gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2625       break;
2626
2627     case PROP_WRAP_MODE:
2628       gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2629       break;
2630       
2631     case PROP_JUSTIFICATION:
2632       gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2633       break;
2634
2635     case PROP_LEFT_MARGIN:
2636       gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2637       break;
2638
2639     case PROP_RIGHT_MARGIN:
2640       gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2641       break;
2642
2643     case PROP_INDENT:
2644       gtk_text_view_set_indent (text_view, g_value_get_int (value));
2645       break;
2646
2647     case PROP_TABS:
2648       gtk_text_view_set_tabs (text_view, g_value_get_boxed (value));
2649       break;
2650
2651     case PROP_CURSOR_VISIBLE:
2652       gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2653       break;
2654
2655     case PROP_OVERWRITE:
2656       gtk_text_view_set_overwrite (text_view, g_value_get_boolean (value));
2657       break;
2658
2659     case PROP_BUFFER:
2660       gtk_text_view_set_buffer (text_view, GTK_TEXT_BUFFER (g_value_get_object (value)));
2661       break;
2662
2663     case PROP_ACCEPTS_TAB:
2664       gtk_text_view_set_accepts_tab (text_view, g_value_get_boolean (value));
2665       break;
2666       
2667     default:
2668       g_assert_not_reached ();
2669       break;
2670     }
2671 }
2672
2673 static void
2674 gtk_text_view_get_property (GObject         *object,
2675                             guint            prop_id,
2676                             GValue          *value,
2677                             GParamSpec      *pspec)
2678 {
2679   GtkTextView *text_view;
2680
2681   text_view = GTK_TEXT_VIEW (object);
2682
2683   switch (prop_id)
2684     {
2685     case PROP_PIXELS_ABOVE_LINES:
2686       g_value_set_int (value, text_view->pixels_above_lines);
2687       break;
2688
2689     case PROP_PIXELS_BELOW_LINES:
2690       g_value_set_int (value, text_view->pixels_below_lines);
2691       break;
2692
2693     case PROP_PIXELS_INSIDE_WRAP:
2694       g_value_set_int (value, text_view->pixels_inside_wrap);
2695       break;
2696
2697     case PROP_EDITABLE:
2698       g_value_set_boolean (value, text_view->editable);
2699       break;
2700       
2701     case PROP_WRAP_MODE:
2702       g_value_set_enum (value, text_view->wrap_mode);
2703       break;
2704
2705     case PROP_JUSTIFICATION:
2706       g_value_set_enum (value, text_view->justify);
2707       break;
2708
2709     case PROP_LEFT_MARGIN:
2710       g_value_set_int (value, text_view->left_margin);
2711       break;
2712
2713     case PROP_RIGHT_MARGIN:
2714       g_value_set_int (value, text_view->right_margin);
2715       break;
2716
2717     case PROP_INDENT:
2718       g_value_set_int (value, text_view->indent);
2719       break;
2720
2721     case PROP_TABS:
2722       g_value_set_boxed (value, text_view->tabs);
2723       break;
2724
2725     case PROP_CURSOR_VISIBLE:
2726       g_value_set_boolean (value, text_view->cursor_visible);
2727       break;
2728
2729     case PROP_BUFFER:
2730       g_value_set_object (value, get_buffer (text_view));
2731       break;
2732
2733     case PROP_OVERWRITE:
2734       g_value_set_boolean (value, text_view->overwrite_mode);
2735       break;
2736
2737     case PROP_ACCEPTS_TAB:
2738       g_value_set_boolean (value, text_view->accepts_tab);
2739       break;
2740       
2741     default:
2742       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2743       break;
2744     }
2745 }
2746
2747 static void
2748 gtk_text_view_size_request (GtkWidget      *widget,
2749                             GtkRequisition *requisition)
2750 {
2751   GtkTextView *text_view;
2752   GSList *tmp_list;
2753   gint focus_edge_width;
2754   gint focus_width;
2755   gboolean interior_focus;
2756   
2757   text_view = GTK_TEXT_VIEW (widget);
2758
2759   gtk_widget_style_get (widget,
2760                         "interior-focus", &interior_focus,
2761                         "focus-line-width", &focus_width,
2762                         NULL);
2763
2764   if (interior_focus)
2765     focus_edge_width = 0;
2766   else
2767     focus_edge_width = focus_width;
2768
2769   if (text_view->layout)
2770     {
2771       text_view->text_window->requisition.width = text_view->layout->width;
2772       text_view->text_window->requisition.height = text_view->layout->height;
2773     }
2774   else
2775     {
2776       text_view->text_window->requisition.width = 0;
2777       text_view->text_window->requisition.height = 0;
2778     }
2779   
2780   requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2781   requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2782
2783   if (text_view->left_window)
2784     requisition->width += text_view->left_window->requisition.width;
2785
2786   if (text_view->right_window)
2787     requisition->width += text_view->right_window->requisition.width;
2788
2789   if (text_view->top_window)
2790     requisition->height += text_view->top_window->requisition.height;
2791
2792   if (text_view->bottom_window)
2793     requisition->height += text_view->bottom_window->requisition.height;
2794
2795   requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2796   requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2797   
2798   tmp_list = text_view->children;
2799   while (tmp_list != NULL)
2800     {
2801       GtkTextViewChild *child = tmp_list->data;
2802
2803       if (child->anchor)
2804         {
2805           GtkRequisition child_req;
2806           GtkRequisition old_req;
2807
2808           gtk_widget_get_child_requisition (child->widget, &old_req);
2809           
2810           gtk_widget_size_request (child->widget, &child_req);
2811
2812           gtk_widget_get_child_requisition (child->widget, &child_req);
2813
2814           /* Invalidate layout lines if required */
2815           if (text_view->layout &&
2816               (old_req.width != child_req.width ||
2817                old_req.height != child_req.height))
2818             gtk_text_child_anchor_queue_resize (child->anchor,
2819                                                 text_view->layout);
2820         }
2821       else
2822         {
2823           GtkRequisition child_req;
2824           
2825           gtk_widget_size_request (child->widget, &child_req);
2826         }
2827
2828       tmp_list = g_slist_next (tmp_list);
2829     }
2830 }
2831
2832 static void
2833 gtk_text_view_compute_child_allocation (GtkTextView      *text_view,
2834                                         GtkTextViewChild *vc,
2835                                         GtkAllocation    *allocation)
2836 {
2837   gint buffer_y;
2838   GtkTextIter iter;
2839   GtkRequisition req;
2840   
2841   gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2842                                             &iter,
2843                                             vc->anchor);
2844
2845   gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2846                                    &buffer_y, NULL);
2847
2848   buffer_y += vc->from_top_of_line;
2849
2850   allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2851   allocation->y = buffer_y - text_view->yoffset;
2852
2853   gtk_widget_get_child_requisition (vc->widget, &req);
2854   allocation->width = req.width;
2855   allocation->height = req.height;
2856 }
2857
2858 static void
2859 gtk_text_view_update_child_allocation (GtkTextView      *text_view,
2860                                        GtkTextViewChild *vc)
2861 {
2862   GtkAllocation allocation;
2863
2864   gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2865   
2866   gtk_widget_size_allocate (vc->widget, &allocation);
2867
2868 #if 0
2869   g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2870            vc->widget,
2871            vc->widget->allocation.x,
2872            vc->widget->allocation.y,
2873            text_view->yoffset);
2874 #endif
2875 }
2876
2877 static void
2878 gtk_text_view_child_allocated (GtkTextLayout *layout,
2879                                GtkWidget     *child,
2880                                gint           x,
2881                                gint           y,
2882                                gpointer       data)
2883 {
2884   GtkTextViewChild *vc = NULL;
2885   GtkTextView *text_view = data;
2886   
2887   /* x,y is the position of the child from the top of the line, and
2888    * from the left of the buffer. We have to translate that into text
2889    * window coordinates, then size_allocate the child.
2890    */
2891
2892   vc = g_object_get_data (G_OBJECT (child),
2893                           "gtk-text-view-child");
2894
2895   g_assert (vc != NULL);
2896
2897   DV (g_print ("child allocated at %d,%d\n", x, y));
2898   
2899   vc->from_left_of_buffer = x;
2900   vc->from_top_of_line = y;
2901
2902   gtk_text_view_update_child_allocation (text_view, vc);
2903 }
2904
2905 static void
2906 gtk_text_view_allocate_children (GtkTextView *text_view)
2907 {
2908   GSList *tmp_list;
2909
2910   DV(g_print(G_STRLOC"\n"));
2911   
2912   tmp_list = text_view->children;
2913   while (tmp_list != NULL)
2914     {
2915       GtkTextViewChild *child = tmp_list->data;
2916
2917       g_assert (child != NULL);
2918           
2919       if (child->anchor)
2920         {
2921           /* We need to force-validate the regions containing
2922            * children.
2923            */
2924           GtkTextIter child_loc;
2925           gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2926                                                     &child_loc,
2927                                                     child->anchor);
2928
2929           /* Since anchored children are only ever allocated from
2930            * gtk_text_layout_get_line_display() we have to make sure
2931            * that the display line caching in the layout doesn't 
2932            * get in the way. Invalidating the layout around the anchor
2933            * achieves this.
2934            */ 
2935           if (GTK_WIDGET_ALLOC_NEEDED (child->widget))
2936             {
2937               GtkTextIter end = child_loc;
2938               gtk_text_iter_forward_char (&end);
2939               gtk_text_layout_invalidate (text_view->layout, &child_loc, &end);
2940             }
2941
2942           gtk_text_layout_validate_yrange (text_view->layout,
2943                                            &child_loc,
2944                                            0, 1);
2945         }
2946       else
2947         {
2948           GtkAllocation allocation;          
2949           GtkRequisition child_req;
2950              
2951           allocation.x = child->x;
2952           allocation.y = child->y;
2953
2954           gtk_widget_get_child_requisition (child->widget, &child_req);
2955           
2956           allocation.width = child_req.width;
2957           allocation.height = child_req.height;
2958           
2959           gtk_widget_size_allocate (child->widget, &allocation);          
2960         }
2961
2962       tmp_list = g_slist_next (tmp_list);
2963     }
2964 }
2965
2966 static void
2967 gtk_text_view_size_allocate (GtkWidget *widget,
2968                              GtkAllocation *allocation)
2969 {
2970   GtkTextView *text_view;
2971   GtkTextIter first_para;
2972   gint y;
2973   gint width, height;
2974   GdkRectangle text_rect;
2975   GdkRectangle left_rect;
2976   GdkRectangle right_rect;
2977   GdkRectangle top_rect;
2978   GdkRectangle bottom_rect;
2979   gint focus_edge_width;
2980   gint focus_width;
2981   gboolean interior_focus;
2982   gboolean size_changed;
2983   
2984   text_view = GTK_TEXT_VIEW (widget);
2985
2986   DV(g_print(G_STRLOC"\n"));
2987
2988   size_changed =
2989     widget->allocation.width != allocation->width ||
2990     widget->allocation.height != allocation->height;
2991   
2992   widget->allocation = *allocation;
2993
2994   if (GTK_WIDGET_REALIZED (widget))
2995     {
2996       gdk_window_move_resize (widget->window,
2997                               allocation->x, allocation->y,
2998                               allocation->width, allocation->height);
2999     }
3000
3001   /* distribute width/height among child windows. Ensure all
3002    * windows get at least a 1x1 allocation.
3003    */
3004
3005   gtk_widget_style_get (widget,
3006                         "interior-focus", &interior_focus,
3007                         "focus-line-width", &focus_width,
3008                         NULL);
3009
3010   if (interior_focus)
3011     focus_edge_width = 0;
3012   else
3013     focus_edge_width = focus_width;
3014   
3015   width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3016
3017   if (text_view->left_window)
3018     left_rect.width = text_view->left_window->requisition.width;
3019   else
3020     left_rect.width = 0;
3021
3022   width -= left_rect.width;
3023
3024   if (text_view->right_window)
3025     right_rect.width = text_view->right_window->requisition.width;
3026   else
3027     right_rect.width = 0;
3028
3029   width -= right_rect.width;
3030
3031   text_rect.width = MAX (1, width);
3032
3033   top_rect.width = text_rect.width;
3034   bottom_rect.width = text_rect.width;
3035
3036
3037   height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
3038
3039   if (text_view->top_window)
3040     top_rect.height = text_view->top_window->requisition.height;
3041   else
3042     top_rect.height = 0;
3043
3044   height -= top_rect.height;
3045
3046   if (text_view->bottom_window)
3047     bottom_rect.height = text_view->bottom_window->requisition.height;
3048   else
3049     bottom_rect.height = 0;
3050
3051   height -= bottom_rect.height;
3052
3053   text_rect.height = MAX (1, height);
3054
3055   left_rect.height = text_rect.height;
3056   right_rect.height = text_rect.height;
3057
3058   /* Origins */
3059   left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3060   top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
3061
3062   text_rect.x = left_rect.x + left_rect.width;
3063   text_rect.y = top_rect.y + top_rect.height;
3064
3065   left_rect.y = text_rect.y;
3066   right_rect.y = text_rect.y;
3067
3068   top_rect.x = text_rect.x;
3069   bottom_rect.x = text_rect.x;
3070
3071   right_rect.x = text_rect.x + text_rect.width;
3072   bottom_rect.y = text_rect.y + text_rect.height;
3073
3074   text_window_size_allocate (text_view->text_window,
3075                              &text_rect);
3076
3077   if (text_view->left_window)
3078     text_window_size_allocate (text_view->left_window,
3079                                &left_rect);
3080
3081   if (text_view->right_window)
3082     text_window_size_allocate (text_view->right_window,
3083                                &right_rect);
3084
3085   if (text_view->top_window)
3086     text_window_size_allocate (text_view->top_window,
3087                                &top_rect);
3088
3089   if (text_view->bottom_window)
3090     text_window_size_allocate (text_view->bottom_window,
3091                                &bottom_rect);
3092
3093   gtk_text_view_update_layout_width (text_view);
3094   
3095   /* Note that this will do some layout validation */
3096   gtk_text_view_allocate_children (text_view);
3097
3098   /* Ensure h/v adj exist */
3099   get_hadjustment (text_view);
3100   get_vadjustment (text_view);
3101
3102   text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
3103   text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) * 0.9;
3104   text_view->hadjustment->step_increment = SCREEN_WIDTH (text_view) * 0.1;
3105   text_view->hadjustment->lower = 0;
3106   text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
3107                                        text_view->width);
3108
3109   if (text_view->hadjustment->value > text_view->hadjustment->upper - text_view->hadjustment->page_size)
3110     gtk_adjustment_set_value (text_view->hadjustment, MAX (0, text_view->hadjustment->upper - text_view->hadjustment->page_size));
3111
3112   gtk_adjustment_changed (text_view->hadjustment);
3113
3114   text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
3115   text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
3116   text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
3117   text_view->vadjustment->lower = 0;
3118   text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
3119                                        text_view->height);
3120
3121   /* Now adjust the value of the adjustment to keep the cursor at the
3122    * same place in the buffer
3123    */
3124   gtk_text_view_get_first_para_iter (text_view, &first_para);
3125   gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
3126
3127   y += text_view->first_para_pixels;
3128
3129   if (y > text_view->vadjustment->upper - text_view->vadjustment->page_size)
3130     y = MAX (0, text_view->vadjustment->upper - text_view->vadjustment->page_size);
3131
3132   if (y != text_view->yoffset)
3133     gtk_adjustment_set_value (text_view->vadjustment, y);
3134
3135   gtk_adjustment_changed (text_view->vadjustment);
3136
3137   /* The GTK resize loop processes all the pending exposes right
3138    * after doing the resize stuff, so the idle sizer won't have a
3139    * chance to run. So we do the work here. 
3140    */
3141   gtk_text_view_flush_first_validate (text_view);
3142
3143   /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3144    * be invalidated
3145    */
3146   if (size_changed && GTK_WIDGET_REALIZED (widget))
3147     gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3148 }
3149
3150 static void
3151 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3152                                    GtkTextIter *iter)
3153 {
3154   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3155                                     text_view->first_para_mark);
3156 }
3157
3158 static void
3159 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3160 {
3161   GtkWidget *widget = GTK_WIDGET (text_view);
3162   
3163   DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3164   
3165   if (SCREEN_HEIGHT (widget) > 0)
3166     {
3167       GtkTextIter first_para;
3168
3169       /* Be sure we've validated the stuff onscreen; if we
3170        * scrolled, these calls won't have any effect, because
3171        * they were called in the recursive validate_onscreen
3172        */
3173       gtk_text_view_get_first_para_iter (text_view, &first_para);
3174
3175       gtk_text_layout_validate_yrange (text_view->layout,
3176                                        &first_para,
3177                                        0,
3178                                        text_view->first_para_pixels +
3179                                        SCREEN_HEIGHT (widget));
3180     }
3181
3182   text_view->onscreen_validated = TRUE;
3183
3184   DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3185   
3186   /* This can have the odd side effect of triggering a scroll, which should
3187    * flip "onscreen_validated" back to FALSE, but should also get us
3188    * back into this function to turn it on again.
3189    */
3190   gtk_text_view_update_adjustments (text_view);
3191
3192   g_assert (text_view->onscreen_validated);
3193 }
3194
3195 static void
3196 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3197 {
3198   if (text_view->first_validate_idle == 0)
3199     return;
3200
3201   /* Do this first, which means that if an "invalidate"
3202    * occurs during any of this process, a new first_validate_callback
3203    * will be installed, and we'll start again.
3204    */
3205   DV (g_print ("removing first validate in %s\n", G_STRLOC));
3206   g_source_remove (text_view->first_validate_idle);
3207   text_view->first_validate_idle = 0;
3208   
3209   /* be sure we have up-to-date screen size set on the
3210    * layout.
3211    */
3212   gtk_text_view_update_layout_width (text_view);
3213
3214   /* Bail out if we invalidated stuff; scrolling right away will just
3215    * confuse the issue.
3216    */
3217   if (text_view->first_validate_idle != 0)
3218     {
3219       DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3220     }
3221   else
3222     {
3223       /* scroll to any marks, if that's pending. This can jump us to
3224        * the validation codepath used for scrolling onscreen, if so we
3225        * bail out.  It won't jump if already in that codepath since
3226        * value_changed is not recursive, so also validate if
3227        * necessary.
3228        */
3229       if (!gtk_text_view_flush_scroll (text_view) ||
3230           !text_view->onscreen_validated)
3231         gtk_text_view_validate_onscreen (text_view);
3232       
3233       DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3234       
3235       g_assert (text_view->onscreen_validated);
3236     }
3237 }
3238
3239 static gboolean
3240 first_validate_callback (gpointer data)
3241 {
3242   GtkTextView *text_view = data;
3243
3244   /* Note that some of this code is duplicated at the end of size_allocate,
3245    * keep in sync with that.
3246    */
3247   
3248   DV(g_print(G_STRLOC"\n"));
3249
3250   gtk_text_view_flush_first_validate (text_view);
3251   
3252   return FALSE;
3253 }
3254
3255 static gboolean
3256 incremental_validate_callback (gpointer data)
3257 {
3258   GtkTextView *text_view = data;
3259   gboolean result = TRUE;
3260
3261   DV(g_print(G_STRLOC"\n"));
3262   
3263   gtk_text_layout_validate (text_view->layout, 2000);
3264
3265   gtk_text_view_update_adjustments (text_view);
3266   
3267   if (gtk_text_layout_is_valid (text_view->layout))
3268     {
3269       text_view->incremental_validate_idle = 0;
3270       result = FALSE;
3271     }
3272
3273   return result;
3274 }
3275
3276 static void
3277 gtk_text_view_invalidate (GtkTextView *text_view)
3278 {  
3279   DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3280                text_view->onscreen_validated));
3281
3282   text_view->onscreen_validated = FALSE;
3283
3284   /* We'll invalidate when the layout is created */
3285   if (text_view->layout == NULL)
3286     return;
3287   
3288   if (!text_view->first_validate_idle)
3289     {
3290       text_view->first_validate_idle = gdk_threads_add_idle_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3291       DV (g_print (G_STRLOC": adding first validate idle %d\n",
3292                    text_view->first_validate_idle));
3293     }
3294       
3295   if (!text_view->incremental_validate_idle)
3296     {
3297       text_view->incremental_validate_idle = gdk_threads_add_idle_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3298       DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3299                    text_view->incremental_validate_idle));
3300     }
3301 }
3302
3303 static void
3304 invalidated_handler (GtkTextLayout *layout,
3305                      gpointer       data)
3306 {
3307   GtkTextView *text_view;
3308
3309   text_view = GTK_TEXT_VIEW (data);
3310
3311   DV (g_print ("Invalidating due to layout invalidate signal\n"));
3312   gtk_text_view_invalidate (text_view);
3313 }
3314
3315 static void
3316 changed_handler (GtkTextLayout     *layout,
3317                  gint               start_y,
3318                  gint               old_height,
3319                  gint               new_height,
3320                  gpointer           data)
3321 {
3322   GtkTextView *text_view;
3323   GtkWidget *widget;
3324   GdkRectangle visible_rect;
3325   GdkRectangle redraw_rect;
3326   
3327   text_view = GTK_TEXT_VIEW (data);
3328   widget = GTK_WIDGET (data);
3329   
3330   DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3331
3332   if (GTK_WIDGET_REALIZED (text_view))
3333     {      
3334       gtk_text_view_get_visible_rect (text_view, &visible_rect);
3335
3336       redraw_rect.x = visible_rect.x;
3337       redraw_rect.width = visible_rect.width;
3338       redraw_rect.y = start_y;
3339
3340       if (old_height == new_height)
3341         redraw_rect.height = old_height;
3342       else if (start_y + old_height > visible_rect.y)
3343         redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3344       else
3345         redraw_rect.height = 0;
3346         
3347       if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3348         {
3349           /* text_window_invalidate_rect() takes buffer coordinates */
3350           text_window_invalidate_rect (text_view->text_window,
3351                                        &redraw_rect);
3352
3353           DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3354                      redraw_rect.x,
3355                      redraw_rect.y,
3356                      redraw_rect.width,
3357                      redraw_rect.height));
3358           
3359           if (text_view->left_window)
3360             text_window_invalidate_rect (text_view->left_window,
3361                                          &redraw_rect);
3362           if (text_view->right_window)
3363             text_window_invalidate_rect (text_view->right_window,
3364                                          &redraw_rect);
3365           if (text_view->top_window)
3366             text_window_invalidate_rect (text_view->top_window,
3367                                          &redraw_rect);
3368           if (text_view->bottom_window)
3369             text_window_invalidate_rect (text_view->bottom_window,
3370                                          &redraw_rect);
3371
3372           gtk_text_view_update_im_spot_location (text_view);
3373         }
3374     }
3375   
3376   if (old_height != new_height)
3377     {
3378       gboolean yoffset_changed = FALSE;
3379       GSList *tmp_list;
3380       int new_first_para_top;
3381       int old_first_para_top;
3382       GtkTextIter first;
3383       
3384       /* If the bottom of the old area was above the top of the
3385        * screen, we need to scroll to keep the current top of the
3386        * screen in place.  Remember that first_para_pixels is the
3387        * position of the top of the screen in coordinates relative to
3388        * the first paragraph onscreen.
3389        *
3390        * In short we are adding the height delta of the portion of the
3391        * changed region above first_para_mark to text_view->yoffset.
3392        */
3393       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3394                                         text_view->first_para_mark);
3395
3396       gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3397
3398       old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3399
3400       if (new_first_para_top != old_first_para_top)
3401         {
3402           text_view->yoffset += new_first_para_top - old_first_para_top;
3403           
3404           get_vadjustment (text_view)->value = text_view->yoffset;
3405           yoffset_changed = TRUE;
3406         }
3407
3408       if (yoffset_changed)
3409         {
3410           DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3411           gtk_adjustment_value_changed (get_vadjustment (text_view));
3412         }
3413
3414       /* FIXME be smarter about which anchored widgets we update */
3415
3416       tmp_list = text_view->children;
3417       while (tmp_list != NULL)
3418         {
3419           GtkTextViewChild *child = tmp_list->data;
3420
3421           if (child->anchor)
3422             gtk_text_view_update_child_allocation (text_view, child);
3423
3424           tmp_list = g_slist_next (tmp_list);
3425         }
3426     }
3427
3428   {
3429     GtkRequisition old_req;
3430     GtkRequisition new_req;
3431
3432     old_req = widget->requisition;
3433
3434     /* Use this instead of gtk_widget_size_request wrapper
3435      * to avoid the optimization which just returns widget->requisition
3436      * if a resize hasn't been queued.
3437      */
3438     GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3439
3440     if (old_req.width != new_req.width ||
3441         old_req.height != new_req.height)
3442       {
3443         gtk_widget_queue_resize_no_redraw (widget);
3444       }
3445   }
3446 }
3447
3448 static void
3449 gtk_text_view_realize (GtkWidget *widget)
3450 {
3451   GtkTextView *text_view;
3452   GdkWindowAttr attributes;
3453   gint attributes_mask;
3454   GSList *tmp_list;
3455   
3456   text_view = GTK_TEXT_VIEW (widget);
3457   GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3458
3459   attributes.window_type = GDK_WINDOW_CHILD;
3460   attributes.x = widget->allocation.x;
3461   attributes.y = widget->allocation.y;
3462   attributes.width = widget->allocation.width;
3463   attributes.height = widget->allocation.height;
3464   attributes.wclass = GDK_INPUT_OUTPUT;
3465   attributes.visual = gtk_widget_get_visual (widget);
3466   attributes.colormap = gtk_widget_get_colormap (widget);
3467   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3468
3469   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3470
3471   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3472                                    &attributes, attributes_mask);
3473   gdk_window_set_user_data (widget->window, widget);
3474
3475   /* must come before text_window_realize calls */
3476   widget->style = gtk_style_attach (widget->style, widget->window);
3477
3478   gdk_window_set_background (widget->window,
3479                              &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3480
3481   text_window_realize (text_view->text_window, widget);
3482
3483   if (text_view->left_window)
3484     text_window_realize (text_view->left_window, widget);
3485
3486   if (text_view->top_window)
3487     text_window_realize (text_view->top_window, widget);
3488
3489   if (text_view->right_window)
3490     text_window_realize (text_view->right_window, widget);
3491
3492   if (text_view->bottom_window)
3493     text_window_realize (text_view->bottom_window, widget);
3494
3495   gtk_text_view_ensure_layout (text_view);
3496
3497   if (text_view->buffer)
3498     {
3499       GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3500                                                           GDK_SELECTION_PRIMARY);
3501       gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3502     }
3503
3504   tmp_list = text_view->children;
3505   while (tmp_list != NULL)
3506     {
3507       GtkTextViewChild *vc = tmp_list->data;
3508       
3509       text_view_child_set_parent_window (text_view, vc);
3510       
3511       tmp_list = tmp_list->next;
3512     }
3513 }
3514
3515 static void
3516 gtk_text_view_unrealize (GtkWidget *widget)
3517 {
3518   GtkTextView *text_view;
3519   
3520   text_view = GTK_TEXT_VIEW (widget);
3521
3522   if (text_view->buffer)
3523     {
3524       GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3525                                                           GDK_SELECTION_PRIMARY);
3526       gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3527     }
3528
3529   gtk_text_view_remove_validate_idles (text_view);
3530
3531   if (text_view->popup_menu)
3532     {
3533       gtk_widget_destroy (text_view->popup_menu);
3534       text_view->popup_menu = NULL;
3535     }
3536
3537   text_window_unrealize (text_view->text_window);
3538
3539   if (text_view->left_window)
3540     text_window_unrealize (text_view->left_window);
3541
3542   if (text_view->top_window)
3543     text_window_unrealize (text_view->top_window);
3544
3545   if (text_view->right_window)
3546     text_window_unrealize (text_view->right_window);
3547
3548   if (text_view->bottom_window)
3549     text_window_unrealize (text_view->bottom_window);
3550
3551   gtk_text_view_destroy_layout (text_view);
3552   
3553   (* GTK_WIDGET_CLASS (gtk_text_view_parent_class)->unrealize) (widget);
3554 }
3555
3556 static void 
3557 gtk_text_view_set_background (GtkTextView *text_view)
3558 {
3559   GtkWidget *widget = GTK_WIDGET (text_view);
3560
3561   gdk_window_set_background (widget->window,
3562                              &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3563   
3564   gdk_window_set_background (text_view->text_window->bin_window,
3565                              &widget->style->base[GTK_WIDGET_STATE (widget)]);
3566   
3567   if (text_view->left_window)
3568     gdk_window_set_background (text_view->left_window->bin_window,
3569                                &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3570   if (text_view->right_window)
3571     gdk_window_set_background (text_view->right_window->bin_window,
3572                                &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3573   
3574   if (text_view->top_window)
3575     gdk_window_set_background (text_view->top_window->bin_window,
3576                                &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3577   
3578   if (text_view->bottom_window)
3579     gdk_window_set_background (text_view->bottom_window->bin_window,
3580                                &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3581 }
3582
3583 static void
3584 gtk_text_view_style_set (GtkWidget *widget,
3585                          GtkStyle  *previous_style)
3586 {
3587   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3588   PangoContext *ltr_context, *rtl_context;
3589
3590   if (GTK_WIDGET_REALIZED (widget))
3591     {
3592       gtk_text_view_set_background (text_view);
3593     }
3594
3595   if (text_view->layout && previous_style)
3596     {
3597       gtk_text_view_set_attributes_from_style (text_view,
3598                                                text_view->layout->default_style,
3599                                                widget->style);
3600       
3601       
3602       ltr_context = gtk_widget_create_pango_context (widget);
3603       pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
3604       rtl_context = gtk_widget_create_pango_context (widget);
3605       pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
3606
3607       gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
3608
3609       g_object_unref (ltr_context);
3610       g_object_unref (rtl_context);
3611     }
3612 }
3613
3614 static void
3615 gtk_text_view_direction_changed (GtkWidget        *widget,
3616                                  GtkTextDirection  previous_direction)
3617 {
3618   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3619
3620   if (text_view->layout)
3621     {
3622       text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3623
3624       gtk_text_layout_default_style_changed (text_view->layout);
3625     }
3626 }
3627
3628 static void
3629 gtk_text_view_state_changed (GtkWidget      *widget,
3630                              GtkStateType    previous_state)
3631 {
3632   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3633   GdkCursor *cursor;
3634
3635   if (GTK_WIDGET_REALIZED (widget))
3636     {
3637       gtk_text_view_set_background (text_view);
3638
3639       if (GTK_WIDGET_IS_SENSITIVE (widget))
3640         cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
3641       else
3642         cursor = NULL;
3643
3644       gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3645
3646       if (cursor)
3647       gdk_cursor_unref (cursor);
3648
3649       text_view->mouse_cursor_obscured = FALSE;
3650     }
3651
3652   if (!GTK_WIDGET_IS_SENSITIVE (widget))
3653     {
3654       /* Clear any selection */
3655       gtk_text_view_unselect (text_view);
3656     }
3657   
3658   gtk_widget_queue_draw (widget);
3659 }
3660
3661 static void
3662 set_invisible_cursor (GdkWindow *window)
3663 {
3664   GdkBitmap *empty_bitmap;
3665   GdkCursor *cursor;
3666   GdkColor useless;
3667   char invisible_cursor_bits[] = { 0x0 };       
3668         
3669   useless.red = useless.green = useless.blue = 0;
3670   useless.pixel = 0;
3671   
3672   empty_bitmap = gdk_bitmap_create_from_data (window,
3673                                               invisible_cursor_bits,
3674                                               1, 1);
3675   
3676   cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3677                                        empty_bitmap,
3678                                        &useless,
3679                                        &useless, 0, 0);
3680   
3681   gdk_window_set_cursor (window, cursor);
3682   
3683   gdk_cursor_unref (cursor);
3684   
3685   g_object_unref (empty_bitmap);
3686 }
3687
3688 static void
3689 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3690 {
3691   if (text_view->mouse_cursor_obscured)
3692     return;
3693
3694   set_invisible_cursor (text_view->text_window->bin_window);
3695   
3696   text_view->mouse_cursor_obscured = TRUE;  
3697 }
3698
3699 static void
3700 gtk_text_view_unobscure_mouse_cursor (GtkTextView *text_view)
3701 {
3702   if (text_view->mouse_cursor_obscured)
3703     {
3704       GdkCursor *cursor;
3705       
3706       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)),
3707                                            GDK_XTERM);
3708       gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
3709       gdk_cursor_unref (cursor);
3710       text_view->mouse_cursor_obscured = FALSE;
3711     }
3712 }
3713
3714 static void
3715 gtk_text_view_grab_notify (GtkWidget *widget,
3716                            gboolean   was_grabbed)
3717 {
3718   if (!was_grabbed)
3719     {
3720       gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), NULL);
3721       gtk_text_view_unobscure_mouse_cursor (GTK_TEXT_VIEW (widget));
3722     }
3723 }
3724
3725
3726 /*
3727  * Events
3728  */
3729
3730 static gboolean
3731 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3732 {
3733   if (event)
3734     switch (event->type)
3735       {
3736       case GDK_MOTION_NOTIFY:
3737         *x = event->motion.x;
3738         *y = event->motion.y;
3739         return TRUE;
3740         break;
3741
3742       case GDK_BUTTON_PRESS:
3743       case GDK_2BUTTON_PRESS:
3744       case GDK_3BUTTON_PRESS:
3745       case GDK_BUTTON_RELEASE:
3746         *x = event->button.x;
3747         *y = event->button.y;
3748         return TRUE;
3749         break;
3750
3751       case GDK_KEY_PRESS:
3752       case GDK_KEY_RELEASE:
3753       case GDK_ENTER_NOTIFY:
3754       case GDK_LEAVE_NOTIFY:
3755       case GDK_PROPERTY_NOTIFY:
3756       case GDK_SELECTION_CLEAR:
3757       case GDK_SELECTION_REQUEST:
3758       case GDK_SELECTION_NOTIFY:
3759       case GDK_PROXIMITY_IN:
3760       case GDK_PROXIMITY_OUT:
3761       case GDK_DRAG_ENTER:
3762       case GDK_DRAG_LEAVE:
3763       case GDK_DRAG_MOTION:
3764       case GDK_DRAG_STATUS:
3765       case GDK_DROP_START:
3766       case GDK_DROP_FINISHED:
3767       default:
3768         return FALSE;
3769         break;
3770       }
3771
3772   return FALSE;
3773 }
3774
3775 static gint
3776 emit_event_on_tags (GtkWidget   *widget,
3777                     GdkEvent    *event,
3778                     GtkTextIter *iter)
3779 {
3780   GSList *tags;
3781   GSList *tmp;
3782   gboolean retval = FALSE;
3783
3784   tags = gtk_text_iter_get_tags (iter);
3785
3786   tmp = tags;
3787   while (tmp != NULL)
3788     {
3789       GtkTextTag *tag = tmp->data;
3790
3791       if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3792         {
3793           retval = TRUE;
3794           break;
3795         }
3796
3797       tmp = g_slist_next (tmp);
3798     }
3799
3800   g_slist_free (tags);
3801
3802   return retval;
3803 }
3804
3805 static gint
3806 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3807 {
3808   GtkTextView *text_view;
3809   gint x = 0, y = 0;
3810
3811   text_view = GTK_TEXT_VIEW (widget);
3812
3813   if (text_view->layout == NULL ||
3814       get_buffer (text_view) == NULL)
3815     return FALSE;
3816
3817   if (event->any.window != text_view->text_window->bin_window)
3818     return FALSE;
3819
3820   if (get_event_coordinates (event, &x, &y))
3821     {
3822       GtkTextIter iter;
3823
3824       x += text_view->xoffset;
3825       y += text_view->yoffset;
3826
3827       /* FIXME this is slow and we do it twice per event.
3828        * My favorite solution is to have GtkTextLayout cache
3829        * the last couple lookups.
3830        */
3831       gtk_text_layout_get_iter_at_pixel (text_view->layout,
3832                                          &iter,
3833                                          x, y);
3834
3835       return emit_event_on_tags (widget, event, &iter);
3836     }
3837   else if (event->type == GDK_KEY_PRESS ||
3838            event->type == GDK_KEY_RELEASE)
3839     {
3840       GtkTextMark *insert;
3841       GtkTextIter iter;
3842
3843       insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3844                                          "insert");
3845
3846       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3847
3848       return emit_event_on_tags (widget, event, &iter);
3849     }
3850   else
3851     return FALSE;
3852 }
3853
3854 static gint
3855 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3856 {
3857   gboolean retval = FALSE;
3858   gboolean obscure = FALSE;
3859   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3860   GtkTextMark *insert;
3861   GtkTextIter iter;
3862   gboolean can_insert;
3863   
3864   if (text_view->layout == NULL ||
3865       get_buffer (text_view) == NULL)
3866     return FALSE;
3867
3868   insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3869   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3870   can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3871   if (can_insert &&
3872       gtk_im_context_filter_keypress (text_view->im_context, event))
3873     {
3874       text_view->need_im_reset = TRUE;
3875       obscure = TRUE;
3876       retval = TRUE;
3877     }
3878   /* Binding set */
3879   else if (GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event &&
3880            GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_press_event (widget, event))
3881     retval = TRUE;
3882   /* use overall editability not can_insert, more predictable for users */
3883   else if (text_view->editable &&
3884            (event->keyval == GDK_Return ||
3885             event->keyval == GDK_KP_Enter))
3886     {
3887       /* this won't actually insert the newline if the cursor isn't
3888        * editable
3889        */
3890       gtk_text_view_reset_im_context (text_view);
3891       gtk_text_view_commit_text (text_view, "\n");
3892
3893       obscure = TRUE;
3894       retval = TRUE;
3895     }
3896   /* Pass through Tab as literal tab, unless Control is held down */
3897   else if ((event->keyval == GDK_Tab ||
3898             event->keyval == GDK_KP_Tab) &&
3899            !(event->state & GDK_CONTROL_MASK))
3900     {
3901       /* If the text widget isn't editable overall, or if the application
3902        * has turned off "accepts_tab", move the focus instead
3903        */
3904       if (text_view->accepts_tab && text_view->editable)
3905         {
3906           gtk_text_view_reset_im_context (text_view);
3907           gtk_text_view_commit_text (text_view, "\t");
3908           obscure = TRUE;
3909         }
3910       else
3911         gtk_text_view_move_focus (text_view,
3912                                   (event->state & GDK_SHIFT_MASK) ?
3913                                   GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3914
3915       retval = TRUE;
3916     }
3917   else
3918     retval = FALSE;
3919
3920   if (obscure)
3921     gtk_text_view_obscure_mouse_cursor (text_view);
3922   
3923   gtk_text_view_reset_blink_time (text_view);
3924   gtk_text_view_pend_cursor_blink (text_view);
3925
3926   if (!retval && event->length)
3927     gtk_widget_error_bell (widget);
3928
3929   return retval;
3930 }
3931
3932 static gint
3933 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3934 {
3935   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3936   GtkTextMark *insert;
3937   GtkTextIter iter;
3938
3939   if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3940     return FALSE;
3941       
3942   insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3943   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3944   if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3945       gtk_im_context_filter_keypress (text_view->im_context, event))
3946     {
3947       text_view->need_im_reset = TRUE;
3948       return TRUE;
3949     }
3950   else
3951     return GTK_WIDGET_CLASS (gtk_text_view_parent_class)->key_release_event (widget, event);
3952 }
3953
3954 static gint
3955 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3956 {
3957   GtkTextView *text_view;
3958
3959   text_view = GTK_TEXT_VIEW (widget);
3960
3961   gtk_widget_grab_focus (widget);
3962
3963   if (event->window != text_view->text_window->bin_window)
3964     {
3965       /* Remove selection if any. */
3966       gtk_text_view_unselect (text_view);
3967       return FALSE;
3968     }
3969
3970   gtk_text_view_reset_blink_time (text_view);
3971
3972 #if 0
3973   /* debug hack */
3974   if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3975     _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3976   else if (event->button == 3)
3977     gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3978 #endif
3979
3980   if (event->type == GDK_BUTTON_PRESS)
3981     {
3982       gtk_text_view_reset_im_context (text_view);
3983
3984       if (event->button == 1)
3985         {
3986           /* If we're in the selection, start a drag copy/move of the
3987            * selection; otherwise, start creating a new selection.
3988            */
3989           GtkTextIter iter;
3990           GtkTextIter start, end;
3991
3992           gtk_text_layout_get_iter_at_pixel (text_view->layout,
3993                                              &iter,
3994                                              event->x + text_view->xoffset,
3995                                              event->y + text_view->yoffset);
3996
3997           if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3998                                                     &start, &end) &&
3999               gtk_text_iter_in_range (&iter, &start, &end))
4000             {
4001               text_view->drag_start_x = event->x;
4002               text_view->drag_start_y = event->y;
4003               text_view->pending_place_cursor_button = event->button;
4004             }
4005           else
4006             {
4007               gtk_text_view_start_selection_drag (text_view, &iter, event);
4008             }
4009
4010           return TRUE;
4011         }
4012       else if (event->button == 2)
4013         {
4014           GtkTextIter iter;
4015
4016           gtk_text_layout_get_iter_at_pixel (text_view->layout,
4017                                              &iter,
4018                                              event->x + text_view->xoffset,
4019                                              event->y + text_view->yoffset);
4020
4021           gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4022                                            gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
4023                                            &iter,
4024                                            text_view->editable);
4025           return TRUE;
4026         }
4027       else if (event->button == 3)
4028         {
4029           gtk_text_view_do_popup (text_view, event);
4030           return TRUE;
4031         }
4032     }
4033   else if ((event->type == GDK_2BUTTON_PRESS ||
4034             event->type == GDK_3BUTTON_PRESS) &&
4035            event->button == 1) 
4036     {
4037       GtkTextIter iter;
4038
4039       gtk_text_view_end_selection_drag (text_view, event);      
4040
4041       gtk_text_layout_get_iter_at_pixel (text_view->layout,
4042                                          &iter,
4043                                          event->x + text_view->xoffset,
4044                                          event->y + text_view->yoffset);
4045       
4046       gtk_text_view_start_selection_drag (text_view, &iter, event);
4047       return TRUE;
4048     }
4049   
4050   return FALSE;
4051 }
4052
4053 static gint
4054 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
4055 {
4056   GtkTextView *text_view;
4057
4058   text_view = GTK_TEXT_VIEW (widget);
4059
4060   if (event->window != text_view->text_window->bin_window)
4061     return FALSE;
4062
4063   if (event->button == 1)
4064     {
4065       if (text_view->drag_start_x >= 0)
4066         {
4067           text_view->drag_start_x = -1;
4068           text_view->drag_start_y = -1;
4069         }
4070
4071       if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
4072         return TRUE;
4073       else if (text_view->pending_place_cursor_button == event->button)
4074         {
4075           GtkTextIter iter;
4076
4077           /* Unselect everything; we clicked inside selection, but
4078            * didn't move by the drag threshold, so just clear selection
4079            * and place cursor.
4080            */
4081           gtk_text_layout_get_iter_at_pixel (text_view->layout,
4082                                              &iter,
4083                                              event->x + text_view->xoffset,
4084                                              event->y + text_view->yoffset);
4085
4086           gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
4087           gtk_text_view_check_cursor_blink (text_view);
4088           
4089           text_view->pending_place_cursor_button = 0;
4090           
4091           return FALSE;
4092         }
4093     }
4094
4095   return FALSE;
4096 }
4097
4098 static void
4099 keymap_direction_changed (GdkKeymap   *keymap,
4100                           GtkTextView *text_view)
4101 {
4102   gtk_text_view_check_keymap_direction (text_view);
4103 }
4104
4105 static gint
4106 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
4107 {
4108   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4109
4110   gtk_widget_queue_draw (widget);
4111
4112   DV(g_print (G_STRLOC": focus_in_event\n"));
4113
4114   gtk_text_view_reset_blink_time (text_view);
4115
4116   if (text_view->cursor_visible && text_view->layout)
4117     {
4118       gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4119       gtk_text_view_check_cursor_blink (text_view);
4120     }
4121
4122   g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4123                     "direction_changed",
4124                     G_CALLBACK (keymap_direction_changed), text_view);
4125   gtk_text_view_check_keymap_direction (text_view);
4126   
4127   text_view->need_im_reset = TRUE;
4128   gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4129
4130   return FALSE;
4131 }
4132
4133 static gint
4134 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4135 {
4136   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4137
4138   gtk_widget_queue_draw (widget);
4139
4140   DV(g_print (G_STRLOC": focus_out_event\n"));
4141   
4142   if (text_view->cursor_visible && text_view->layout)
4143     {
4144       gtk_text_view_check_cursor_blink (text_view);
4145       gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4146     }
4147
4148   g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4149                                         keymap_direction_changed,
4150                                         text_view);
4151
4152   text_view->need_im_reset = TRUE;
4153   gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4154
4155   return FALSE;
4156 }
4157
4158 static gint
4159 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4160 {
4161   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4162
4163   gtk_text_view_unobscure_mouse_cursor (text_view);
4164
4165   if (event->window == text_view->text_window->bin_window &&
4166       text_view->drag_start_x >= 0)
4167     {
4168       gint x, y;
4169
4170       gdk_window_get_pointer (text_view->text_window->bin_window,
4171                               &x, &y, NULL);
4172
4173       if (gtk_drag_check_threshold (widget,
4174                                     text_view->drag_start_x, 
4175                                     text_view->drag_start_y,
4176                                     x, y))
4177         {
4178           GtkTextIter iter;
4179           gint buffer_x, buffer_y;
4180
4181           gtk_text_view_window_to_buffer_coords (text_view,
4182                                                  GTK_TEXT_WINDOW_TEXT,
4183                                                  text_view->drag_start_x,
4184                                                  text_view->drag_start_y,
4185                                                  &buffer_x,
4186                                                  &buffer_y);
4187
4188           gtk_text_layout_get_iter_at_pixel (text_view->layout,
4189                                              &iter,
4190                                              buffer_x, buffer_y);
4191
4192           gtk_text_view_start_selection_dnd (text_view, &iter, event);
4193           return TRUE;
4194         }
4195     }
4196
4197   return FALSE;
4198 }
4199
4200 static void
4201 gtk_text_view_paint (GtkWidget      *widget,
4202                      GdkRectangle   *area,
4203                      GdkEventExpose *event)
4204 {
4205   GtkTextView *text_view;
4206   GList *child_exposes;
4207   GList *tmp_list;
4208   GdkRegion *updates;
4209   
4210   text_view = GTK_TEXT_VIEW (widget);
4211
4212   g_return_if_fail (text_view->layout != NULL);
4213   g_return_if_fail (text_view->xoffset >= 0);
4214   g_return_if_fail (text_view->yoffset >= 0);
4215
4216   while (text_view->first_validate_idle != 0)
4217     {
4218       DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4219                    text_view->first_validate_idle));
4220       gtk_text_view_flush_first_validate (text_view);
4221     }
4222
4223   /* More regions could have become invalid in the above loop */
4224   updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4225   if (updates)
4226     {
4227       GdkRectangle rect;
4228       
4229       gdk_region_get_clipbox (updates, &rect);
4230
4231       gdk_rectangle_union (area, &rect, area);
4232       
4233       gdk_region_destroy (updates);
4234     }
4235   
4236   if (!text_view->onscreen_validated)
4237     {
4238       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.");
4239       g_assert_not_reached ();
4240     }
4241   
4242 #if 0
4243   printf ("painting %d,%d  %d x %d\n",
4244           area->x, area->y,
4245           area->width, area->height);
4246 #endif
4247
4248   child_exposes = NULL;
4249   gtk_text_layout_draw (text_view->layout,
4250                         widget,
4251                         text_view->text_window->bin_window,
4252                         NULL,
4253                         text_view->xoffset,
4254                         text_view->yoffset,
4255                         area->x, area->y,
4256                         area->width, area->height,
4257                         &child_exposes);
4258
4259   tmp_list = child_exposes;
4260   while (tmp_list != NULL)
4261     {
4262       GtkWidget *child = tmp_list->data;
4263   
4264       gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4265                                       child,
4266                                       event);
4267
4268       g_object_unref (child);
4269       
4270       tmp_list = tmp_list->next;
4271     }
4272
4273   g_list_free (child_exposes);
4274 }
4275
4276 static gint
4277 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4278 {
4279   GSList *tmp_list;
4280   
4281   if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4282                                                  GTK_TEXT_WINDOW_TEXT))
4283     {
4284       DV(g_print (">Exposed ("G_STRLOC")\n"));
4285       gtk_text_view_paint (widget, &event->area, event);
4286     }
4287
4288   if (event->window == widget->window)
4289     gtk_text_view_draw_focus (widget);
4290
4291   /* Propagate exposes to all unanchored children. 
4292    * Anchored children are handled in gtk_text_view_paint(). 
4293    */
4294   tmp_list = GTK_TEXT_VIEW (widget)->children;
4295   while (tmp_list != NULL)
4296     {
4297       GtkTextViewChild *vc = tmp_list->data;
4298
4299       /* propagate_expose checks that event->window matches
4300        * child->window
4301        */
4302       if (!vc->anchor)
4303         gtk_container_propagate_expose (GTK_CONTAINER (widget),
4304                                         vc->widget,
4305                                         event);
4306       
4307       tmp_list = tmp_list->next;
4308     }
4309   
4310   return FALSE;
4311 }
4312
4313 static void
4314 gtk_text_view_draw_focus (GtkWidget *widget)
4315 {
4316   gboolean interior_focus;
4317
4318   /* We clear the focus if we are in interior focus mode. */
4319   gtk_widget_style_get (widget,
4320                         "interior-focus", &interior_focus,
4321                         NULL);
4322   
4323   if (GTK_WIDGET_DRAWABLE (widget))
4324     {
4325       if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4326         {          
4327           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget), 
4328                            NULL, widget, "textview",
4329                            0, 0,
4330                            widget->allocation.width,
4331                            widget->allocation.height);
4332         }
4333       else
4334         {
4335           gdk_window_clear (widget->window);
4336         }
4337     }
4338 }
4339
4340 static gboolean
4341 gtk_text_view_focus (GtkWidget        *widget,
4342                      GtkDirectionType  direction)
4343 {
4344   GtkContainer *container;
4345   gboolean result;
4346   
4347   container = GTK_CONTAINER (widget);  
4348
4349   if (!gtk_widget_is_focus (widget) &&
4350       container->focus_child == NULL)
4351     {
4352       gtk_widget_grab_focus (widget);
4353       return TRUE;
4354     }
4355   else
4356     {
4357       /*
4358        * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4359        * children to get the focus
4360        */
4361       GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS); 
4362       result = GTK_WIDGET_CLASS (gtk_text_view_parent_class)->focus (widget, direction);
4363       GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS); 
4364
4365       return result;
4366     }
4367 }
4368
4369 /*
4370  * Container
4371  */
4372
4373 static void
4374 gtk_text_view_add (GtkContainer *container,
4375                    GtkWidget    *child)
4376 {
4377   /* This is pretty random. */
4378   gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4379                                      child,
4380                                      GTK_TEXT_WINDOW_WIDGET,
4381                                      0, 0);
4382 }
4383
4384 static void
4385 gtk_text_view_remove (GtkContainer *container,
4386                       GtkWidget    *child)
4387 {
4388   GSList *iter;
4389   GtkTextView *text_view;
4390   GtkTextViewChild *vc;
4391
4392   text_view = GTK_TEXT_VIEW (container);
4393
4394   vc = NULL;
4395   iter = text_view->children;
4396
4397   while (iter != NULL)
4398     {
4399       vc = iter->data;
4400
4401       if (vc->widget == child)
4402         break;
4403
4404       iter = g_slist_next (iter);
4405     }
4406
4407   g_assert (iter != NULL); /* be sure we had the child in the list */
4408
4409   text_view->children = g_slist_remove (text_view->children, vc);
4410
4411   gtk_widget_unparent (vc->widget);
4412
4413   text_view_child_free (vc);
4414 }
4415
4416 static void
4417 gtk_text_view_forall (GtkContainer *container,
4418                       gboolean      include_internals,
4419                       GtkCallback   callback,
4420                       gpointer      callback_data)
4421 {
4422   GSList *iter;
4423   GtkTextView *text_view;
4424   GSList *copy;
4425
4426   g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4427   g_return_if_fail (callback != NULL);
4428
4429   text_view = GTK_TEXT_VIEW (container);
4430
4431   copy = g_slist_copy (text_view->children);
4432   iter = copy;
4433
4434   while (iter != NULL)
4435     {
4436       GtkTextViewChild *vc = iter->data;
4437
4438       (* callback) (vc->widget, callback_data);
4439
4440       iter = g_slist_next (iter);
4441     }
4442
4443   g_slist_free (copy);
4444 }
4445
4446 #define CURSOR_ON_MULTIPLIER 2
4447 #define CURSOR_OFF_MULTIPLIER 1
4448 #define CURSOR_PEND_MULTIPLIER 3
4449 #define CURSOR_DIVIDER 3
4450
4451 static gboolean
4452 cursor_blinks (GtkTextView *text_view)
4453 {
4454   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4455   gboolean blink;
4456
4457 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4458   return FALSE;
4459 #endif
4460   if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4461     return FALSE;
4462
4463   g_object_get (settings, "gtk-cursor-blink", &blink, NULL);
4464
4465   if (!blink)
4466     return FALSE;
4467
4468   if (text_view->editable)
4469     {
4470       GtkTextMark *insert;
4471       GtkTextIter iter;
4472       
4473       insert = gtk_text_buffer_get_insert (get_buffer (text_view));
4474       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
4475       
4476       if (gtk_text_iter_editable (&iter, text_view->editable))
4477         return blink;
4478     }
4479
4480   return FALSE;
4481 }
4482
4483 static gint
4484 get_cursor_time (GtkTextView *text_view)
4485 {
4486   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4487   gint time;
4488
4489   g_object_get (settings, "gtk-cursor-blink-time", &time, NULL);
4490
4491   return time;
4492 }
4493
4494 static gint
4495 get_cursor_blink_timeout (GtkTextView *text_view)
4496 {
4497   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4498   gint time;
4499
4500   g_object_get (settings, "gtk-cursor-blink-timeout", &time, NULL);
4501
4502   return time;
4503 }
4504
4505
4506 /*
4507  * Blink!
4508  */
4509
4510 static gint
4511 blink_cb (gpointer data)
4512 {
4513   GtkTextView *text_view;
4514   GtkTextViewPrivate *priv;
4515   gboolean visible;
4516   gint blink_timeout;
4517
4518   text_view = GTK_TEXT_VIEW (data);
4519   priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4520
4521   if (!GTK_WIDGET_HAS_FOCUS (text_view))
4522     {
4523       g_warning ("GtkTextView - did not receive focus-out-event. If you\n"
4524                  "connect a handler to this signal, it must return\n"
4525                  "FALSE so the text view gets the event as well");
4526
4527       gtk_text_view_check_cursor_blink (text_view);
4528
4529       return FALSE;
4530     }
4531
4532   g_assert (text_view->layout);
4533   g_assert (text_view->cursor_visible);
4534
4535   visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4536
4537   blink_timeout = get_cursor_blink_timeout (text_view);
4538   if (priv->blink_time > 1000 * blink_timeout &&
4539       blink_timeout < G_MAXINT/1000) 
4540     {
4541       /* we've blinked enough without the user doing anything, stop blinking */
4542       visible = 0;
4543       text_view->blink_timeout = 0;
4544     } 
4545   else if (visible)
4546     text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4547                                               blink_cb,
4548                                               text_view);
4549   else 
4550     {
4551       text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER / CURSOR_DIVIDER,
4552                                                 blink_cb,
4553                                                 text_view);
4554       priv->blink_time += get_cursor_time (text_view);
4555     }
4556
4557   /* Block changed_handler while changing the layout's cursor visibility
4558    * because it would expose the whole paragraph. Instead, we expose
4559    * the cursor's area(s) manually below.
4560    */
4561   g_signal_handlers_block_by_func (text_view->layout,
4562                                    changed_handler,
4563                                    text_view);
4564   gtk_text_layout_set_cursor_visible (text_view->layout, !visible);
4565   g_signal_handlers_unblock_by_func (text_view->layout,
4566                                      changed_handler,
4567                                      text_view);
4568
4569   text_window_invalidate_cursors (text_view->text_window);
4570
4571   /* Remove ourselves */
4572   return FALSE;
4573 }
4574
4575
4576 static void
4577 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4578 {
4579   if (text_view->blink_timeout)  
4580     { 
4581       g_source_remove (text_view->blink_timeout);
4582       text_view->blink_timeout = 0;
4583     }
4584 }
4585
4586 static void
4587 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4588 {
4589   if (text_view->layout != NULL &&
4590       text_view->cursor_visible &&
4591       GTK_WIDGET_HAS_FOCUS (text_view))
4592     {
4593       if (cursor_blinks (text_view))
4594         {
4595           if (text_view->blink_timeout == 0)
4596             {
4597               gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4598               
4599               text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER / CURSOR_DIVIDER,
4600                                                         blink_cb,
4601                                                         text_view);
4602             }
4603         }
4604       else
4605         {
4606           gtk_text_view_stop_cursor_blink (text_view);
4607           gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4608         }
4609     }
4610   else
4611     {
4612       gtk_text_view_stop_cursor_blink (text_view);
4613       gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4614     }
4615 }
4616
4617 static void
4618 gtk_text_view_pend_cursor_blink (GtkTextView *text_view)
4619 {
4620   if (text_view->layout != NULL &&
4621       text_view->cursor_visible &&
4622       GTK_WIDGET_HAS_FOCUS (text_view) &&
4623       cursor_blinks (text_view))
4624     {
4625       gtk_text_view_stop_cursor_blink (text_view);
4626       gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4627       
4628       text_view->blink_timeout = gdk_threads_add_timeout (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER / CURSOR_DIVIDER,
4629                                                 blink_cb,
4630                                                 text_view);
4631     }
4632 }
4633
4634 static void
4635 gtk_text_view_reset_blink_time (GtkTextView *text_view)
4636 {
4637   GtkTextViewPrivate *priv;
4638
4639   priv = GTK_TEXT_VIEW_GET_PRIVATE (text_view);
4640
4641   priv->blink_time = 0;
4642 }
4643
4644
4645 /*
4646  * Key binding handlers
4647  */
4648
4649 static gboolean
4650 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4651                                   GtkTextIter *newplace,
4652                                   gint         count)
4653 {
4654   gboolean ret = TRUE;
4655
4656   while (count < 0)
4657     {
4658       ret = gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4659       count++;
4660     }
4661
4662   while (count > 0)
4663     {
4664       ret = gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4665       count--;
4666     }
4667
4668   return ret;
4669 }
4670
4671 static void
4672 move_cursor (GtkTextView       *text_view,
4673              const GtkTextIter *new_location,
4674              gboolean           extend_selection)
4675 {
4676   if (extend_selection)
4677     gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4678                                        "insert",
4679                                        new_location);
4680   else
4681       gtk_text_buffer_place_cursor (get_buffer (text_view),
4682                                     new_location);
4683   gtk_text_view_check_cursor_blink (text_view);
4684 }
4685
4686 static void
4687 gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
4688                                     GtkMovementStep  step,
4689                                     gint             count,
4690                                     gboolean         extend_selection)
4691 {
4692   GtkTextIter insert;
4693   GtkTextIter newplace;
4694   gint cursor_x_pos = 0;
4695   GtkDirectionType leave_direction = -1;
4696
4697   if (!text_view->cursor_visible) 
4698     {
4699       GtkScrollStep scroll_step;
4700
4701       switch (step) 
4702         {
4703         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4704         case GTK_MOVEMENT_VISUAL_POSITIONS:
4705         case GTK_MOVEMENT_WORDS:
4706           scroll_step = GTK_SCROLL_HORIZONTAL_STEPS;
4707           break;
4708         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4709           scroll_step = GTK_SCROLL_HORIZONTAL_ENDS;
4710           break;          
4711         case GTK_MOVEMENT_DISPLAY_LINES:
4712         case GTK_MOVEMENT_PARAGRAPHS:
4713         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4714           scroll_step = GTK_SCROLL_STEPS;
4715           break;
4716         case GTK_MOVEMENT_PAGES:
4717           scroll_step = GTK_SCROLL_PAGES;
4718           break;
4719         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4720           scroll_step = GTK_SCROLL_HORIZONTAL_PAGES;
4721           break;
4722         case GTK_MOVEMENT_BUFFER_ENDS:
4723           scroll_step = GTK_SCROLL_ENDS;
4724           break;
4725         default:
4726           scroll_step = GTK_SCROLL_PAGES;
4727           break;
4728         }
4729       
4730       gtk_text_view_move_viewport (text_view, scroll_step, count);
4731
4732       return;
4733     }
4734
4735   gtk_text_view_reset_im_context (text_view);
4736
4737   if (step == GTK_MOVEMENT_PAGES)
4738     {
4739       if (!gtk_text_view_scroll_pages (text_view, count, extend_selection))
4740         gtk_widget_error_bell (GTK_WIDGET (text_view));
4741
4742       gtk_text_view_check_cursor_blink (text_view);
4743       gtk_text_view_pend_cursor_blink (text_view);
4744       return;
4745     }
4746   else if (step == GTK_MOVEMENT_HORIZONTAL_PAGES)
4747     {
4748       if (!gtk_text_view_scroll_hpages (text_view, count, extend_selection))
4749         gtk_widget_error_bell (GTK_WIDGET (text_view));
4750
4751       gtk_text_view_check_cursor_blink (text_view);
4752       gtk_text_view_pend_cursor_blink (text_view);
4753       return;
4754     }
4755
4756   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4757                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4758                                                               "insert"));
4759   newplace = insert;
4760
4761   if (step == GTK_MOVEMENT_DISPLAY_LINES)
4762     gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4763
4764   switch (step)
4765     {
4766     case GTK_MOVEMENT_LOGICAL_POSITIONS:
4767       gtk_text_iter_forward_visible_cursor_positions (&newplace, count);
4768       break;
4769
4770     case GTK_MOVEMENT_VISUAL_POSITIONS:
4771       gtk_text_layout_move_iter_visually (text_view->layout,
4772                                           &newplace, count);
4773       break;
4774
4775     case GTK_MOVEMENT_WORDS:
4776       if (count < 0)
4777         gtk_text_iter_backward_visible_word_starts (&newplace, -count);
4778       else if (count > 0) 
4779         {
4780           if (!gtk_text_iter_forward_visible_word_ends (&newplace, count))
4781             gtk_text_iter_forward_to_line_end (&newplace);
4782         }
4783       break;
4784
4785     case GTK_MOVEMENT_DISPLAY_LINES:
4786       if (count < 0)
4787         {
4788           leave_direction = GTK_DIR_UP;
4789
4790           if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
4791             gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4792           else
4793             gtk_text_iter_set_line_offset (&newplace, 0);
4794         }
4795       if (count > 0)
4796         {
4797           leave_direction = GTK_DIR_DOWN;
4798
4799           if (gtk_text_view_move_iter_by_lines (text_view, &newplace, count))
4800             gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4801           else
4802             gtk_text_iter_forward_to_line_end (&newplace);
4803         }
4804       break;
4805
4806     case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4807       if (count > 1)
4808         gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4809       else if (count < -1)
4810         gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4811
4812       if (count != 0)
4813         gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4814       break;
4815
4816     case GTK_MOVEMENT_PARAGRAPHS:
4817       if (count > 0)
4818         {
4819           if (!gtk_text_iter_ends_line (&newplace))
4820             {
4821               gtk_text_iter_forward_to_line_end (&newplace);
4822               --count;
4823             }
4824           gtk_text_iter_forward_visible_lines (&newplace, count);
4825           gtk_text_iter_forward_to_line_end (&newplace);
4826         }
4827       else if (count < 0)
4828         {
4829           if (gtk_text_iter_get_line_offset (&newplace) > 0)
4830             gtk_text_iter_set_line_offset (&newplace, 0);
4831           gtk_text_iter_forward_visible_lines (&newplace, count);
4832           gtk_text_iter_set_line_offset (&newplace, 0);
4833         }
4834       break;
4835
4836     case GTK_MOVEMENT_PARAGRAPH_ENDS:
4837       if (count > 0)
4838         {
4839           if (!gtk_text_iter_ends_line (&newplace))
4840             gtk_text_iter_forward_to_line_end (&newplace);
4841         }
4842       else if (count < 0)
4843         {
4844           gtk_text_iter_set_line_offset (&newplace, 0);
4845         }
4846       break;
4847
4848     case GTK_MOVEMENT_BUFFER_ENDS:
4849       if (count > 0)
4850         gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4851       else if (count < 0)
4852         gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4853      break;
4854       
4855     default:
4856       break;
4857     }
4858
4859   /* call move_cursor() even if the cursor hasn't moved, since it 
4860      cancels the selection
4861   */
4862   move_cursor (text_view, &newplace, extend_selection);
4863
4864   if (!gtk_text_iter_equal (&insert, &newplace))
4865     {
4866       DV(g_print (G_STRLOC": scrolling onscreen\n"));
4867       gtk_text_view_scroll_mark_onscreen (text_view,
4868                                           gtk_text_buffer_get_mark (get_buffer (text_view),
4869                                                                     "insert"));
4870
4871       if (step == GTK_MOVEMENT_DISPLAY_LINES)
4872         gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4873     }
4874   else if (leave_direction != -1)
4875     {
4876       if (!gtk_widget_keynav_failed (GTK_WIDGET (text_view),
4877                                      leave_direction))
4878         {
4879           gtk_text_view_move_focus (text_view, leave_direction);
4880         }
4881     }
4882   else
4883     {
4884       gtk_widget_error_bell (GTK_WIDGET (text_view));
4885     }
4886
4887   gtk_text_view_check_cursor_blink (text_view);
4888   gtk_text_view_pend_cursor_blink (text_view);
4889 }
4890
4891 static void
4892 gtk_text_view_move_cursor (GtkTextView     *text_view,
4893                            GtkMovementStep  step,
4894                            gint             count,
4895                            gboolean         extend_selection)
4896 {
4897   gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4898 }
4899
4900 static void
4901 gtk_text_view_page_horizontally (GtkTextView     *text_view,
4902                                  gint             count,
4903                                  gboolean         extend_selection)
4904 {
4905   gtk_text_view_move_cursor_internal (text_view, GTK_MOVEMENT_HORIZONTAL_PAGES,
4906                                       count, extend_selection);
4907 }
4908
4909
4910 static void
4911 gtk_text_view_move_viewport (GtkTextView     *text_view,
4912                              GtkScrollStep    step,
4913                              gint             count)
4914 {
4915   GtkAdjustment *adjustment;
4916   gdouble increment;
4917   
4918   switch (step) 
4919     {
4920     case GTK_SCROLL_STEPS:
4921     case GTK_SCROLL_PAGES:
4922     case GTK_SCROLL_ENDS:
4923       adjustment = get_vadjustment (text_view);
4924       break;
4925     case GTK_SCROLL_HORIZONTAL_STEPS:
4926     case GTK_SCROLL_HORIZONTAL_PAGES:
4927     case GTK_SCROLL_HORIZONTAL_ENDS:
4928       adjustment = get_hadjustment (text_view);
4929       break;
4930     default:
4931       adjustment = get_vadjustment (text_view);
4932       break;
4933     }
4934
4935   switch (step) 
4936     {
4937     case GTK_SCROLL_STEPS:
4938     case GTK_SCROLL_HORIZONTAL_STEPS:
4939       increment = adjustment->step_increment;
4940       break;
4941     case GTK_SCROLL_PAGES:
4942     case GTK_SCROLL_HORIZONTAL_PAGES:
4943       increment = adjustment->page_increment;
4944       break;
4945     case GTK_SCROLL_ENDS:
4946     case GTK_SCROLL_HORIZONTAL_ENDS:
4947       increment = adjustment->upper - adjustment->lower;
4948       break;
4949     default:
4950       increment = 0.0;
4951       break;
4952     }
4953
4954   set_adjustment_clamped (adjustment, adjustment->value + count * increment);
4955 }
4956
4957 static void
4958 gtk_text_view_set_anchor (GtkTextView *text_view)
4959 {
4960   GtkTextIter insert;
4961
4962   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4963                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4964                                                               "insert"));
4965
4966   gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4967 }
4968
4969 static gboolean
4970 gtk_text_view_scroll_pages (GtkTextView *text_view,
4971                             gint         count,
4972                             gboolean     extend_selection)
4973 {
4974   gdouble newval;
4975   gdouble oldval;
4976   GtkAdjustment *adj;
4977   gint cursor_x_pos, cursor_y_pos;
4978   GtkTextIter old_insert;
4979   GtkTextIter new_insert;
4980   GtkTextIter anchor;
4981   gint y0, y1;
4982
4983   g_return_val_if_fail (text_view->vadjustment != NULL, FALSE);
4984   
4985   adj = text_view->vadjustment;
4986
4987   /* Make sure we start from the current cursor position, even
4988    * if it was offscreen, but don't queue more scrolls if we're
4989    * already behind.
4990    */
4991   if (text_view->pending_scroll)
4992     cancel_pending_scroll (text_view);
4993   else
4994     gtk_text_view_scroll_mark_onscreen (text_view,
4995                                         gtk_text_buffer_get_mark (get_buffer (text_view),
4996                                                                   "insert"));
4997
4998   /* Validate the region that will be brought into view by the cursor motion
4999    */
5000   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5001                                     &old_insert,
5002                                     gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5003
5004   if (count < 0)
5005     {
5006       gtk_text_view_get_first_para_iter (text_view, &anchor);
5007       y0 = adj->page_size;
5008       y1 = adj->page_size + count * adj->page_increment;
5009     }
5010   else
5011     {
5012       gtk_text_view_get_first_para_iter (text_view, &anchor);
5013       y0 = count * adj->page_increment + adj->page_size;
5014       y1 = 0;
5015     }
5016
5017   gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
5018   /* FIXME do we need to update the adjustment ranges here? */
5019
5020   new_insert = old_insert;
5021
5022   if (count < 0 && adj->value <= (adj->lower + 1e-12))
5023     {
5024       /* already at top, just be sure we are at offset 0 */
5025       gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
5026       move_cursor (text_view, &new_insert, extend_selection);
5027     }
5028   else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5029     {
5030       /* already at bottom, just be sure we are at the end */
5031       gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
5032       move_cursor (text_view, &new_insert, extend_selection);
5033     }
5034   else
5035     {
5036       gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5037
5038       oldval = adj->value;
5039       newval = adj->value;
5040
5041       newval += count * adj->page_increment;
5042
5043       set_adjustment_clamped (adj, newval);
5044       cursor_y_pos += adj->value - oldval;
5045
5046       gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5047       clamp_iter_onscreen (text_view, &new_insert);
5048       move_cursor (text_view, &new_insert, extend_selection);
5049
5050       gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5051     }
5052   
5053   /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5054    * only guarantees 1 pixel onscreen.
5055    */
5056   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5057   gtk_text_view_scroll_mark_onscreen (text_view,
5058                                       gtk_text_buffer_get_mark (get_buffer (text_view),
5059                                                                 "insert"));
5060
5061   return !gtk_text_iter_equal (&old_insert, &new_insert);
5062 }
5063
5064 static gboolean
5065 gtk_text_view_scroll_hpages (GtkTextView *text_view,
5066                              gint         count,
5067                              gboolean     extend_selection)
5068 {
5069   gdouble newval;
5070   gdouble oldval;
5071   GtkAdjustment *adj;
5072   gint cursor_x_pos, cursor_y_pos;
5073   GtkTextIter old_insert;
5074   GtkTextIter new_insert;
5075   gint y, height;
5076   
5077   g_return_val_if_fail (text_view->hadjustment != NULL, FALSE);
5078
5079   adj = text_view->hadjustment;
5080
5081   /* Make sure we start from the current cursor position, even
5082    * if it was offscreen, but don't queue more scrolls if we're
5083    * already behind.
5084    */
5085   if (text_view->pending_scroll)
5086     cancel_pending_scroll (text_view);
5087   else
5088     gtk_text_view_scroll_mark_onscreen (text_view,
5089                                         gtk_text_buffer_get_mark (get_buffer (text_view),
5090                                                                   "insert"));
5091
5092   /* Validate the line that we're moving within.
5093    */
5094   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5095                                     &old_insert,
5096                                     gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5097   gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
5098   gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
5099   /* FIXME do we need to update the adjustment ranges here? */
5100
5101   new_insert = old_insert;
5102
5103   if (count < 0 && adj->value <= (adj->lower + 1e-12))
5104     {
5105       /* already at far left, just be sure we are at offset 0 */
5106       gtk_text_iter_set_line_offset (&new_insert, 0);
5107       move_cursor (text_view, &new_insert, extend_selection);
5108     }
5109   else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
5110     {
5111       /* already at far right, just be sure we are at the end */
5112       if (!gtk_text_iter_ends_line (&new_insert))
5113           gtk_text_iter_forward_to_line_end (&new_insert);
5114       move_cursor (text_view, &new_insert, extend_selection);
5115     }
5116   else
5117     {
5118       gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
5119
5120       oldval = adj->value;
5121       newval = adj->value;
5122
5123       newval += count * adj->page_increment;
5124
5125       set_adjustment_clamped (adj, newval);
5126       cursor_x_pos += adj->value - oldval;
5127
5128       gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
5129       clamp_iter_onscreen (text_view, &new_insert);
5130       move_cursor (text_view, &new_insert, extend_selection);
5131
5132       gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
5133     }
5134
5135   /*  FIXME for lines shorter than the overall widget width, this results in a
5136    *  "bounce" effect as we scroll to the right of the widget, then scroll
5137    *  back to get the end of the line onscreen.
5138    *      http://bugzilla.gnome.org/show_bug.cgi?id=68963
5139    */
5140   
5141   /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
5142    * only guarantees 1 pixel onscreen.
5143    */
5144   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5145   gtk_text_view_scroll_mark_onscreen (text_view,
5146                                       gtk_text_buffer_get_mark (get_buffer (text_view),
5147                                                                 "insert"));
5148
5149   return !gtk_text_iter_equal (&old_insert, &new_insert);
5150 }
5151
5152 static gboolean
5153 whitespace (gunichar ch, gpointer user_data)
5154 {
5155   return (ch == ' ' || ch == '\t');
5156 }
5157
5158 static gboolean
5159 not_whitespace (gunichar ch, gpointer user_data)
5160 {
5161   return !whitespace (ch, user_data);
5162 }
5163
5164 static gboolean
5165 find_whitepace_region (const GtkTextIter *center,
5166                        GtkTextIter *start, GtkTextIter *end)
5167 {
5168   *start = *center;
5169   *end = *center;
5170
5171   if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
5172     gtk_text_iter_forward_char (start); /* we want the first whitespace... */
5173   if (whitespace (gtk_text_iter_get_char (end), NULL))
5174     gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
5175
5176   return !gtk_text_iter_equal (start, end);
5177 }
5178
5179 static void
5180 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
5181                                 const gchar *str)
5182 {
5183   if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5184                                                      text_view->editable))
5185     {
5186       gtk_widget_error_bell (GTK_WIDGET (text_view));
5187     }
5188 }
5189
5190 static void
5191 gtk_text_view_delete_from_cursor (GtkTextView   *text_view,
5192                                   GtkDeleteType  type,
5193                                   gint           count)
5194 {
5195   GtkTextIter insert;
5196   GtkTextIter start;
5197   GtkTextIter end;
5198   gboolean leave_one = FALSE;
5199
5200   gtk_text_view_reset_im_context (text_view);
5201
5202   if (type == GTK_DELETE_CHARS)
5203     {
5204       /* Char delete deletes the selection, if one exists */
5205       if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5206                                             text_view->editable))
5207         return;
5208     }
5209
5210   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5211                                     &insert,
5212                                     gtk_text_buffer_get_mark (get_buffer (text_view),
5213                                                               "insert"));
5214
5215   start = insert;
5216   end = insert;
5217
5218   switch (type)
5219     {
5220     case GTK_DELETE_CHARS:
5221       gtk_text_iter_forward_cursor_positions (&end, count);
5222       break;
5223
5224     case GTK_DELETE_WORD_ENDS:
5225       if (count > 0)
5226         gtk_text_iter_forward_word_ends (&end, count);
5227       else if (count < 0)
5228         gtk_text_iter_backward_word_starts (&start, 0 - count);
5229       break;
5230
5231     case GTK_DELETE_WORDS:
5232       break;
5233
5234     case GTK_DELETE_DISPLAY_LINE_ENDS:
5235       break;
5236
5237     case GTK_DELETE_DISPLAY_LINES:
5238       break;
5239
5240     case GTK_DELETE_PARAGRAPH_ENDS:
5241       /* If we're already at a newline, we need to
5242        * simply delete that newline, instead of
5243        * moving to the next one.
5244        */
5245       if (gtk_text_iter_ends_line (&end))
5246         {
5247           gtk_text_iter_forward_line (&end);
5248           --count;
5249         }
5250
5251       while (count > 0)
5252         {
5253           if (!gtk_text_iter_forward_to_line_end (&end))
5254             break;
5255
5256           --count;
5257         }
5258
5259       /* FIXME figure out what a negative count means
5260          and support that */
5261       break;
5262
5263     case GTK_DELETE_PARAGRAPHS:
5264       if (count > 0)
5265         {
5266           gtk_text_iter_set_line_offset (&start, 0);
5267           gtk_text_iter_forward_to_line_end (&end);
5268
5269           /* Do the lines beyond the first. */
5270           while (count > 1)
5271             {
5272               gtk_text_iter_forward_to_line_end (&end);
5273
5274               --count;
5275             }
5276         }
5277
5278       /* FIXME negative count? */
5279
5280       break;
5281
5282     case GTK_DELETE_WHITESPACE:
5283       {
5284         find_whitepace_region (&insert, &start, &end);
5285       }
5286       break;
5287
5288     default:
5289       break;
5290     }
5291
5292   if (!gtk_text_iter_equal (&start, &end))
5293     {
5294       gtk_text_buffer_begin_user_action (get_buffer (text_view));
5295
5296       if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
5297                                               text_view->editable))
5298         {
5299           if (leave_one)
5300             gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
5301                                                           " ", 1,
5302                                                           text_view->editable);
5303         }
5304       else
5305         {
5306           gtk_widget_error_bell (GTK_WIDGET (text_view));
5307         }
5308
5309       gtk_text_buffer_end_user_action (get_buffer (text_view));
5310       gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5311
5312       DV(g_print (G_STRLOC": scrolling onscreen\n"));
5313       gtk_text_view_scroll_mark_onscreen (text_view,
5314                                           gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5315     }
5316   else
5317     {
5318       gtk_widget_error_bell (GTK_WIDGET (text_view));
5319     }
5320 }
5321
5322 static void
5323 gtk_text_view_backspace (GtkTextView *text_view)
5324 {
5325   GtkTextIter insert;
5326
5327   gtk_text_view_reset_im_context (text_view);
5328
5329   /* Backspace deletes the selection, if one exists */
5330   if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5331                                         text_view->editable))
5332     return;
5333
5334   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5335                                     &insert,
5336                                     gtk_text_buffer_get_insert (get_buffer (text_view)));
5337
5338   if (gtk_text_buffer_backspace (get_buffer (text_view), &insert,
5339                                  TRUE, text_view->editable))
5340     {
5341       gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
5342       DV(g_print (G_STRLOC": scrolling onscreen\n"));
5343       gtk_text_view_scroll_mark_onscreen (text_view,
5344                                           gtk_text_buffer_get_insert (get_buffer (text_view)));
5345     }
5346   else
5347     {
5348       gtk_widget_error_bell (GTK_WIDGET (text_view));
5349     }
5350 }
5351
5352 static void
5353 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5354 {
5355   GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5356                                                       GDK_SELECTION_CLIPBOARD);
5357   
5358   gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5359                                  clipboard,
5360                                  text_view->editable);
5361   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5362   gtk_text_view_scroll_mark_onscreen (text_view,
5363                                       gtk_text_buffer_get_insert (get_buffer (text_view)));
5364 }
5365
5366 static void
5367 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5368 {
5369   GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5370                                                       GDK_SELECTION_CLIPBOARD);
5371   
5372   gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5373                                   clipboard);
5374
5375   /* on copy do not scroll, we are already onscreen */
5376 }
5377
5378 static void
5379 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5380 {
5381   GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5382                                                       GDK_SELECTION_CLIPBOARD);
5383   
5384   gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5385                                    clipboard,
5386                                    NULL,
5387                                    text_view->editable);
5388   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5389   gtk_text_view_scroll_mark_onscreen (text_view,
5390                                       gtk_text_buffer_get_insert (get_buffer (text_view)));
5391 }
5392
5393 static void
5394 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5395 {
5396   text_view->overwrite_mode = !text_view->overwrite_mode;
5397   g_object_notify (G_OBJECT (text_view), "overwrite");
5398 }
5399
5400 /**
5401  * gtk_text_view_get_overwrite:
5402  * @text_view: a #GtkTextView
5403  *
5404  * Returns whether the #GtkTextView is in overwrite mode or not.
5405  *
5406  * Return value: whether @text_view is in overwrite mode or not.
5407  * 
5408  * Since: 2.4
5409  **/
5410 gboolean
5411 gtk_text_view_get_overwrite (GtkTextView *text_view)
5412 {
5413   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5414
5415   return text_view->overwrite_mode;
5416 }
5417
5418 /**
5419  * gtk_text_view_set_overwrite:
5420  * @text_view: a #GtkTextView
5421  * @overwrite: %TRUE to turn on overwrite mode, %FALSE to turn it off
5422  *
5423  * Changes the #GtkTextView overwrite mode.
5424  *
5425  * Since: 2.4
5426  **/
5427 void
5428 gtk_text_view_set_overwrite (GtkTextView *text_view,
5429                              gboolean     overwrite)
5430 {
5431   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5432   overwrite = overwrite != FALSE;
5433
5434   if (text_view->overwrite_mode != overwrite)
5435     {
5436       text_view->overwrite_mode = overwrite;
5437
5438       g_object_notify (G_OBJECT (text_view), "overwrite");
5439     }
5440 }
5441
5442 /**
5443  * gtk_text_view_set_accepts_tab:
5444  * @text_view: A #GtkTextView
5445  * @accepts_tab: %TRUE if pressing the Tab key should insert a tab character, %FALSE, if pressing the Tab key should move the keyboard focus.
5446  * 
5447  * Sets the behavior of the text widget when the Tab key is pressed. If @accepts_tab
5448  * is %TRUE a tab character is inserted. If @accepts_tab is %FALSE the keyboard focus
5449  * is moved to the next widget in the focus chain.
5450  * 
5451  * Since: 2.4
5452  **/
5453 void
5454 gtk_text_view_set_accepts_tab (GtkTextView *text_view,
5455                                gboolean     accepts_tab)
5456 {
5457   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5458
5459   accepts_tab = accepts_tab != FALSE;
5460
5461   if (text_view->accepts_tab != accepts_tab)
5462     {
5463       text_view->accepts_tab = accepts_tab;
5464
5465       g_object_notify (G_OBJECT (text_view), "accepts-tab");
5466     }
5467 }
5468
5469 /**
5470  * gtk_text_view_get_accepts_tab:
5471  * @text_view: A #GtkTextView
5472  * 
5473  * Returns whether pressing the Tab key inserts a tab characters.
5474  * gtk_text_view_set_accepts_tab().
5475  * 
5476  * Return value: %TRUE if pressing the Tab key inserts a tab character, %FALSE if pressing the Tab key moves the keyboard focus.
5477  * 
5478  * Since: 2.4
5479  **/
5480 gboolean
5481 gtk_text_view_get_accepts_tab (GtkTextView *text_view)
5482 {
5483   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
5484
5485   return text_view->accepts_tab;
5486 }
5487
5488 static void
5489 gtk_text_view_move_focus (GtkTextView     *text_view,
5490                           GtkDirectionType direction_type)
5491 {
5492   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5493
5494   if (!GTK_WIDGET_TOPLEVEL (toplevel))
5495     return;
5496
5497   /* Propagate to toplevel */
5498   g_signal_emit_by_name (toplevel, "move_focus", direction_type);
5499 }
5500
5501 /*
5502  * Selections
5503  */
5504
5505 static void
5506 gtk_text_view_unselect (GtkTextView *text_view)
5507 {
5508   GtkTextIter insert;
5509
5510   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5511                                     &insert,
5512                                     gtk_text_buffer_get_mark (get_buffer (text_view),
5513                                                               "insert"));
5514
5515   gtk_text_buffer_move_mark (get_buffer (text_view),
5516                              gtk_text_buffer_get_mark (get_buffer (text_view),
5517                                                        "selection_bound"),
5518                              &insert);
5519 }
5520
5521 static void
5522 get_iter_at_pointer (GtkTextView *text_view,
5523                      GtkTextIter *iter,
5524                      gint        *x,
5525                      gint        *y)
5526 {
5527   gint xcoord, ycoord;
5528   GdkModifierType state;
5529
5530   gdk_window_get_pointer (text_view->text_window->bin_window,
5531                           &xcoord, &ycoord, &state);
5532   
5533   gtk_text_layout_get_iter_at_pixel (text_view->layout,
5534                                      iter,
5535                                      xcoord + text_view->xoffset,
5536                                      ycoord + text_view->yoffset);
5537   if (x)
5538     *x = xcoord;
5539
5540   if (y)
5541     *y = ycoord;
5542 }
5543
5544 static void
5545 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5546                                  const gchar *mark_name)
5547 {
5548   GtkTextIter newplace;
5549   GtkTextMark *mark;
5550
5551   get_iter_at_pointer (text_view, &newplace, NULL, NULL);
5552   
5553   mark = gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5554   
5555   /* This may invalidate the layout */
5556   DV(g_print (G_STRLOC": move mark\n"));
5557   
5558   gtk_text_buffer_move_mark (get_buffer (text_view),
5559                              mark,
5560                              &newplace);
5561   
5562   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5563   gtk_text_view_scroll_mark_onscreen (text_view, mark);
5564
5565   DV (g_print ("first validate idle leaving %s is %d\n",
5566                G_STRLOC, text_view->first_validate_idle));
5567 }
5568
5569 static gboolean
5570 selection_scan_timeout (gpointer data)
5571 {
5572   GtkTextView *text_view;
5573
5574   text_view = GTK_TEXT_VIEW (data);
5575
5576   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5577   gtk_text_view_scroll_mark_onscreen (text_view, 
5578                                       gtk_text_buffer_get_mark (get_buffer (text_view),
5579                                                                 "insert"));
5580
5581   return TRUE; /* remain installed. */
5582 }
5583
5584 #define UPPER_OFFSET_ANCHOR 0.8
5585 #define LOWER_OFFSET_ANCHOR 0.2
5586
5587 static gboolean
5588 check_scroll (gdouble offset, GtkAdjustment *adj)
5589 {
5590   if ((offset > UPPER_OFFSET_ANCHOR &&
5591        adj->value + adj->page_size < adj->upper) ||
5592       (offset < LOWER_OFFSET_ANCHOR &&
5593        adj->value > adj->lower))
5594     return TRUE;
5595
5596   return FALSE;
5597 }
5598
5599 static gint
5600 drag_scan_timeout (gpointer data)
5601 {
5602   GtkTextView *text_view;
5603   GtkTextIter newplace;
5604   gint x, y, width, height;
5605   gdouble pointer_xoffset, pointer_yoffset;
5606
5607   text_view = GTK_TEXT_VIEW (data);
5608
5609   get_iter_at_pointer (text_view, &newplace, &x, &y);
5610   gdk_drawable_get_size (text_view->text_window->bin_window, &width, &height);
5611
5612   gtk_text_buffer_move_mark (get_buffer (text_view),
5613                              text_view->dnd_mark,
5614                              &newplace);
5615
5616   pointer_xoffset = (gdouble) x / width;
5617   pointer_yoffset = (gdouble) y / height;
5618
5619   if (check_scroll (pointer_xoffset, text_view->hadjustment) ||
5620       check_scroll (pointer_yoffset, text_view->vadjustment))
5621     {
5622       /* do not make offsets surpass lower nor upper anchors, this makes
5623        * scrolling speed relative to the distance of the pointer to the
5624        * anchors when it moves beyond them.
5625        */
5626       pointer_xoffset = CLAMP (pointer_xoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
5627       pointer_yoffset = CLAMP (pointer_yoffset, LOWER_OFFSET_ANCHOR, UPPER_OFFSET_ANCHOR);
5628
5629       gtk_text_view_scroll_to_mark (text_view,
5630                                     text_view->dnd_mark,
5631                                     0., TRUE, pointer_xoffset, pointer_yoffset);
5632     }
5633
5634   return TRUE;
5635 }
5636
5637 typedef enum 
5638 {
5639   SELECT_CHARACTERS,
5640   SELECT_WORDS,
5641   SELECT_LINES
5642 } SelectionGranularity;
5643
5644 /*
5645  * Move @start and @end to the boundaries of the selection unit (indicated by 
5646  * @granularity) which contained @start initially.
5647  * If the selction unit is SELECT_WORDS and @start is not contained in a word
5648  * the selection is extended to all the white spaces between the end of the 
5649  * word preceding @start and the start of the one following.
5650  */
5651 static void
5652 extend_selection (GtkTextView *text_view, 
5653                   SelectionGranularity granularity, 
5654                   GtkTextIter *start, 
5655                   GtkTextIter *end)
5656 {
5657   *end = *start;
5658
5659   if (granularity == SELECT_WORDS) 
5660     {
5661       if (gtk_text_iter_inside_word (start))
5662         {
5663           if (!gtk_text_iter_starts_word (start))
5664             gtk_text_iter_backward_visible_word_start (start);
5665           
5666           if (!gtk_text_iter_ends_word (end))
5667             {
5668               if (!gtk_text_iter_forward_visible_word_end (end))
5669                 gtk_text_iter_forward_to_end (end);
5670             }
5671         }
5672       else
5673         {
5674           GtkTextIter tmp;
5675
5676           tmp = *start;
5677           if (gtk_text_iter_backward_visible_word_start (&tmp))
5678             gtk_text_iter_forward_visible_word_end (&tmp);
5679
5680           if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
5681             *start = tmp;
5682           else
5683             gtk_text_iter_set_line_offset (start, 0);
5684
5685           tmp = *end;
5686           if (!gtk_text_iter_forward_visible_word_end (&tmp))
5687             gtk_text_iter_forward_to_end (&tmp);
5688
5689           if (gtk_text_iter_ends_word (&tmp))
5690             gtk_text_iter_backward_visible_word_start (&tmp);
5691
5692           if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
5693             *end = tmp;
5694           else
5695             gtk_text_iter_forward_to_line_end (end);
5696         }
5697     }
5698   else if (granularity == SELECT_LINES) 
5699     {
5700       if (gtk_text_view_starts_display_line (text_view, start))
5701         {
5702           /* If on a display line boundary, we assume the user
5703            * clicked off the end of a line and we therefore select
5704            * the line before the boundary.
5705            */
5706           gtk_text_view_backward_display_line_start (text_view, start);
5707         }
5708       else
5709         {
5710           /* start isn't on the start of a line, so we move it to the
5711            * start, and move end to the end unless it's already there.
5712            */
5713           gtk_text_view_backward_display_line_start (text_view, start);
5714           
5715           if (!gtk_text_view_starts_display_line (text_view, end))
5716             gtk_text_view_forward_display_line_end (text_view, end);
5717         }
5718     }
5719 }
5720  
5721
5722 typedef struct
5723 {
5724   SelectionGranularity granularity;
5725   GtkTextMark *orig_start;
5726   GtkTextMark *orig_end;
5727 } SelectionData;
5728
5729 static void
5730 selection_data_free (SelectionData *data)
5731 {
5732   if (data->orig_start != NULL)
5733     gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_start),
5734                                  data->orig_start);
5735   if (data->orig_end != NULL)
5736     gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (data->orig_end),
5737                                  data->orig_end);
5738   g_free (data);
5739 }
5740
5741 static gint
5742 selection_motion_event_handler (GtkTextView    *text_view, 
5743                                 GdkEventMotion *event, 
5744                                 SelectionData  *data)
5745 {
5746   if (data->granularity == SELECT_CHARACTERS) 
5747     {
5748       move_mark_to_pointer_and_scroll (text_view, "insert");
5749     }
5750   else 
5751     {
5752       GtkTextIter cursor, start, end;
5753       GtkTextIter orig_start, orig_end;
5754       GtkTextBuffer *buffer;
5755       
5756       buffer = get_buffer (text_view);
5757
5758       gtk_text_buffer_get_iter_at_mark (buffer, &orig_start, data->orig_start);
5759       gtk_text_buffer_get_iter_at_mark (buffer, &orig_end, data->orig_end);
5760
5761       get_iter_at_pointer (text_view, &cursor, NULL, NULL);
5762       
5763       start = cursor;
5764       extend_selection (text_view, data->granularity, &start, &end);
5765
5766       /* either the selection extends to the front, or end (or not) */
5767       if (gtk_text_iter_compare (&cursor, &orig_start) < 0)
5768         gtk_text_buffer_select_range (buffer, &start, &orig_end);
5769       else
5770         gtk_text_buffer_select_range (buffer, &end, &orig_start);
5771
5772       gtk_text_view_scroll_mark_onscreen (text_view, 
5773                                           gtk_text_buffer_get_insert (buffer));
5774     }
5775
5776   /* If we had to scroll offscreen, insert a timeout to do so
5777    * again. Note that in the timeout, even if the mouse doesn't
5778    * move, due to this scroll xoffset/yoffset will have changed
5779    * and we'll need to scroll again.
5780    */
5781   if (text_view->scroll_timeout != 0) /* reset on every motion event */
5782     g_source_remove (text_view->scroll_timeout);
5783   
5784   text_view->scroll_timeout =
5785     gdk_threads_add_timeout (50, selection_scan_timeout, text_view);
5786
5787   return TRUE;
5788 }
5789
5790 static void
5791 gtk_text_view_start_selection_drag (GtkTextView       *text_view,
5792                                     const GtkTextIter *iter,
5793                                     GdkEventButton    *button)
5794 {
5795   GtkTextIter cursor, ins, bound;
5796   GtkTextIter orig_start, orig_end;
5797   GtkTextBuffer *buffer;
5798   SelectionData *data;
5799
5800   if (text_view->selection_drag_handler != 0)
5801     return;
5802   
5803   data = g_new0 (SelectionData, 1);
5804
5805   if (button->type == GDK_2BUTTON_PRESS)
5806     data->granularity = SELECT_WORDS;
5807   else if (button->type == GDK_3BUTTON_PRESS)
5808     data->granularity = SELECT_LINES;
5809   else 
5810     data->granularity = SELECT_CHARACTERS;
5811
5812   gtk_grab_add (GTK_WIDGET (text_view));
5813
5814   buffer = get_buffer (text_view);
5815   
5816   cursor = *iter;
5817   ins = cursor;
5818   
5819   extend_selection (text_view, data->granularity, &ins, &bound);
5820   orig_start = ins;
5821   orig_end = bound;
5822
5823   if (button->state & GDK_SHIFT_MASK)
5824     {
5825       /* Extend selection */
5826       GtkTextIter old_ins, old_bound;
5827       GtkTextIter old_start, old_end;
5828
5829       gtk_text_buffer_get_iter_at_mark (buffer, &old_ins, gtk_text_buffer_get_insert (buffer));
5830       gtk_text_buffer_get_iter_at_mark (buffer, &old_bound, gtk_text_buffer_get_selection_bound (buffer));
5831       old_start = old_ins;
5832       old_end = old_bound;
5833       gtk_text_iter_order (&old_start, &old_end);
5834       
5835       /* move the front cursor, if the mouse is in front of the selection. Should the
5836        * cursor however be inside the selection (this happens on tripple click) then we
5837        * move the side which was last moved (current insert mark) */
5838       if (gtk_text_iter_compare (&cursor, &old_start) <= 0 ||
5839           (gtk_text_iter_compare (&cursor, &old_end) < 0 && 
5840            gtk_text_iter_compare (&old_ins, &old_bound) <= 0))
5841         {
5842           bound = old_end;
5843           orig_start = old_end;
5844           orig_end = old_end;
5845         }
5846       else
5847         {
5848           ins = bound;
5849           bound = old_start;
5850           orig_end = bound;
5851           orig_start = bound;
5852         }
5853     }
5854
5855   gtk_text_buffer_select_range (buffer, &ins, &bound);
5856
5857   gtk_text_iter_order (&orig_start, &orig_end);
5858   data->orig_start = gtk_text_buffer_create_mark (buffer, NULL,
5859                                                   &orig_start, TRUE);
5860   data->orig_end = gtk_text_buffer_create_mark (buffer, NULL,
5861                                                 &orig_end, TRUE);
5862
5863   gtk_text_view_check_cursor_blink (text_view);
5864
5865   text_view->selection_drag_handler = g_signal_connect_data (text_view,
5866                                                              "motion_notify_event",
5867                                                              G_CALLBACK (selection_motion_event_handler),
5868                                                              data,
5869                                                              (GClosureNotify) selection_data_free, 0);  
5870 }
5871
5872 /* returns whether we were really dragging */
5873 static gboolean
5874 gtk_text_view_end_selection_drag (GtkTextView    *text_view, 
5875                                   GdkEventButton *event)
5876 {
5877   if (text_view->selection_drag_handler == 0)
5878     return FALSE;
5879
5880   g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
5881   text_view->selection_drag_handler = 0;
5882
5883   if (text_view->scroll_timeout != 0)
5884     {
5885       g_source_remove (text_view->scroll_timeout);
5886       text_view->scroll_timeout = 0;
5887     }
5888
5889   gtk_grab_remove (GTK_WIDGET (text_view));
5890
5891   return TRUE;
5892 }
5893
5894 /*
5895  * Layout utils
5896  */
5897
5898 static void
5899 gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
5900                                          GtkTextAttributes  *values,
5901                                          GtkStyle           *style)
5902 {
5903   values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5904   values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5905
5906   if (values->font)
5907     pango_font_description_free (values->font);
5908
5909   values->font = pango_font_description_copy (style->font_desc);
5910 }
5911
5912 static void
5913 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5914 {
5915   if (text_view->layout)
5916     {
5917       GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5918       GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5919       GtkTextDirection new_cursor_dir;
5920       GtkTextDirection new_keyboard_dir;
5921       gboolean split_cursor;
5922
5923       g_object_get (settings,
5924                     "gtk-split-cursor", &split_cursor,
5925                     NULL);
5926       
5927       if (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_RTL)
5928         new_keyboard_dir = GTK_TEXT_DIR_RTL;
5929       else
5930         new_keyboard_dir  = GTK_TEXT_DIR_LTR;
5931   
5932       if (split_cursor)
5933         new_cursor_dir = GTK_TEXT_DIR_NONE;
5934       else
5935         new_cursor_dir = new_keyboard_dir;
5936       
5937       gtk_text_layout_set_cursor_direction (text_view->layout, new_cursor_dir);
5938       gtk_text_layout_set_keyboard_direction (text_view->layout, new_keyboard_dir);
5939     }
5940 }
5941
5942 static void
5943 gtk_text_view_ensure_layout (GtkTextView *text_view)
5944 {
5945   GtkWidget *widget;
5946
5947   widget = GTK_WIDGET (text_view);
5948
5949   if (text_view->layout == NULL)
5950     {
5951       GtkTextAttributes *style;
5952       PangoContext *ltr_context, *rtl_context;
5953       GSList *tmp_list;
5954
5955       DV(g_print(G_STRLOC"\n"));
5956       
5957       text_view->layout = gtk_text_layout_new ();
5958
5959       g_signal_connect (text_view->layout,
5960                         "invalidated",
5961                         G_CALLBACK (invalidated_handler),
5962                         text_view);
5963
5964       g_signal_connect (text_view->layout,
5965                         "changed",
5966                         G_CALLBACK (changed_handler),
5967                         text_view);
5968
5969       g_signal_connect (text_view->layout,
5970                         "allocate_child",
5971                         G_CALLBACK (gtk_text_view_child_allocated),
5972                         text_view);
5973       
5974       if (get_buffer (text_view))
5975         gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5976
5977       if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5978         gtk_text_view_pend_cursor_blink (text_view);
5979       else
5980         gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5981
5982       ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5983       pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5984       rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5985       pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5986
5987       gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5988
5989       g_object_unref (ltr_context);
5990       g_object_unref (rtl_context);
5991
5992       gtk_text_view_check_keymap_direction (text_view);
5993
5994       style = gtk_text_attributes_new ();
5995
5996       gtk_widget_ensure_style (widget);
5997       gtk_text_view_set_attributes_from_style (text_view,
5998                                                style, widget->style);
5999
6000       style->pixels_above_lines = text_view->pixels_above_lines;
6001       style->pixels_below_lines = text_view->pixels_below_lines;
6002       style->pixels_inside_wrap = text_view->pixels_inside_wrap;
6003       style->left_margin = text_view->left_margin;
6004       style->right_margin = text_view->right_margin;
6005       style->indent = text_view->indent;
6006       style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
6007
6008       style->wrap_mode = text_view->wrap_mode;
6009       style->justification = text_view->justify;
6010       style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
6011
6012       gtk_text_layout_set_default_style (text_view->layout, style);
6013
6014       gtk_text_attributes_unref (style);
6015
6016       /* Set layout for all anchored children */
6017
6018       tmp_list = text_view->children;
6019       while (tmp_list != NULL)
6020         {
6021           GtkTextViewChild *vc = tmp_list->data;
6022
6023           if (vc->anchor)
6024             {
6025               gtk_text_anchored_child_set_layout (vc->widget,
6026                                                   text_view->layout);
6027               /* vc may now be invalid! */
6028             }
6029
6030           tmp_list = g_slist_next (tmp_list);
6031         }
6032
6033       gtk_text_view_invalidate (text_view);
6034     }
6035 }
6036
6037 /**
6038  * gtk_text_view_get_default_attributes:
6039  * @text_view: a #GtkTextView
6040  * 
6041  * Obtains a copy of the default text attributes. These are the
6042  * attributes used for text unless a tag overrides them.
6043  * You'd typically pass the default attributes in to
6044  * gtk_text_iter_get_attributes() in order to get the
6045  * attributes in effect at a given text position.
6046  *
6047  * The return value is a copy owned by the caller of this function,
6048  * and should be freed.
6049  * 
6050  * Return value: a new #GtkTextAttributes
6051  **/
6052 GtkTextAttributes*
6053 gtk_text_view_get_default_attributes (GtkTextView *text_view)
6054 {
6055   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6056   
6057   gtk_text_view_ensure_layout (text_view);
6058
6059   return gtk_text_attributes_copy (text_view->layout->default_style);
6060 }
6061
6062 static void
6063 gtk_text_view_destroy_layout (GtkTextView *text_view)
6064 {
6065   if (text_view->layout)
6066     {
6067       GSList *tmp_list;
6068
6069       gtk_text_view_remove_validate_idles (text_view);
6070
6071       g_signal_handlers_disconnect_by_func (text_view->layout,
6072                                             invalidated_handler,
6073                                             text_view);
6074       g_signal_handlers_disconnect_by_func (text_view->layout,
6075                                             changed_handler, 
6076                                             text_view);
6077       
6078       /* Remove layout from all anchored children */
6079       tmp_list = text_view->children;
6080       while (tmp_list != NULL)
6081         {
6082           GtkTextViewChild *vc = tmp_list->data;
6083
6084           if (vc->anchor)
6085             {
6086               gtk_text_anchored_child_set_layout (vc->widget, NULL);
6087               /* vc may now be invalid! */
6088             }
6089
6090           tmp_list = g_slist_next (tmp_list);
6091         }
6092       
6093       gtk_text_view_stop_cursor_blink (text_view);
6094       gtk_text_view_end_selection_drag (text_view, NULL);
6095
6096       g_object_unref (text_view->layout);
6097       text_view->layout = NULL;
6098     }
6099 }
6100
6101 static void
6102 gtk_text_view_reset_im_context (GtkTextView *text_view)
6103 {
6104   if (text_view->need_im_reset)
6105     {
6106       text_view->need_im_reset = FALSE;
6107       gtk_im_context_reset (text_view->im_context);
6108     }
6109 }
6110
6111 /*
6112  * DND feature
6113  */
6114
6115 static void
6116 drag_begin_cb (GtkWidget      *widget,
6117                GdkDragContext *context,
6118                gpointer        data)
6119 {
6120   GtkTextView   *text_view = GTK_TEXT_VIEW (widget);
6121   GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6122   GtkTextIter    start;
6123   GtkTextIter    end;
6124   GdkPixmap     *pixmap = NULL;
6125
6126   g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
6127
6128   if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6129     pixmap = _gtk_text_util_create_rich_drag_icon (widget, buffer, &start, &end);
6130
6131   if (pixmap)
6132     {
6133       gtk_drag_set_icon_pixmap (context,
6134                                 gdk_drawable_get_colormap (pixmap),
6135                                 pixmap,
6136                                 NULL,
6137                                 -2, -2);
6138       g_object_unref (pixmap);
6139     }
6140   else
6141     {
6142       gtk_drag_set_icon_default (context);
6143     }
6144 }
6145
6146 static void
6147 gtk_text_view_start_selection_dnd (GtkTextView       *text_view,
6148                                    const GtkTextIter *iter,
6149                                    GdkEventMotion    *event)
6150 {
6151   GtkTargetList *target_list;
6152
6153   text_view->drag_start_x = -1;
6154   text_view->drag_start_y = -1;
6155   text_view->pending_place_cursor_button = 0;
6156
6157   target_list = gtk_text_buffer_get_copy_target_list (get_buffer (text_view));
6158
6159   g_signal_connect (text_view, "drag_begin",
6160                     G_CALLBACK (drag_begin_cb), NULL);
6161   gtk_drag_begin (GTK_WIDGET (text_view), target_list,
6162                   GDK_ACTION_COPY | GDK_ACTION_MOVE,
6163                   1, (GdkEvent*)event);
6164 }
6165
6166 static void
6167 gtk_text_view_drag_begin (GtkWidget        *widget,
6168                           GdkDragContext   *context)
6169 {
6170   /* do nothing */
6171 }
6172
6173 static void
6174 gtk_text_view_drag_end (GtkWidget        *widget,
6175                         GdkDragContext   *context)
6176 {
6177 }
6178
6179 static void
6180 gtk_text_view_drag_data_get (GtkWidget        *widget,
6181                              GdkDragContext   *context,
6182                              GtkSelectionData *selection_data,
6183                              guint             info,
6184                              guint             time)
6185 {
6186   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
6187   GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
6188
6189   if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6190     {
6191       gtk_selection_data_set (selection_data,
6192                               gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
6193                               8, /* bytes */
6194                               (void*)&buffer,
6195                               sizeof (buffer));
6196     }
6197   else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6198     {
6199       GtkTextIter start;
6200       GtkTextIter end;
6201       guint8 *str = NULL;
6202       gsize len;
6203
6204       if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6205         {
6206           /* Extract the selected text */
6207           str = gtk_text_buffer_serialize (buffer, buffer,
6208                                            selection_data->target,
6209                                            &start, &end,
6210                                            &len);
6211         }
6212
6213       if (str)
6214         {
6215           gtk_selection_data_set (selection_data,
6216                                   selection_data->target,
6217                                   8, /* bytes */
6218                                   (guchar *) str, len);
6219           g_free (str);
6220         }
6221     }
6222   else
6223     {
6224       GtkTextIter start;
6225       GtkTextIter end;
6226       gchar *str = NULL;
6227
6228       if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
6229         {
6230           /* Extract the selected text */
6231           str = gtk_text_iter_get_visible_text (&start, &end);
6232         }
6233
6234       if (str)
6235         {
6236           gtk_selection_data_set_text (selection_data, str, -1);
6237           g_free (str);
6238         }
6239     }
6240 }
6241
6242 static void
6243 gtk_text_view_drag_data_delete (GtkWidget        *widget,
6244                                 GdkDragContext   *context)
6245 {
6246   gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
6247                                     TRUE, GTK_TEXT_VIEW (widget)->editable);
6248 }
6249
6250 static void
6251 gtk_text_view_drag_leave (GtkWidget        *widget,
6252                           GdkDragContext   *context,
6253                           guint             time)
6254 {
6255   GtkTextView *text_view;
6256
6257   text_view = GTK_TEXT_VIEW (widget);
6258
6259   gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6260   
6261   if (text_view->scroll_timeout != 0)
6262     g_source_remove (text_view->scroll_timeout);
6263
6264   text_view->scroll_timeout = 0;
6265 }
6266
6267 static gboolean
6268 gtk_text_view_drag_motion (GtkWidget        *widget,
6269                            GdkDragContext   *context,
6270                            gint              x,
6271                            gint              y,
6272                            guint             time)
6273 {
6274   GtkTextIter newplace;
6275   GtkTextView *text_view;
6276   GtkTextIter start;
6277   GtkTextIter end;
6278   GdkRectangle target_rect;
6279   gint bx, by;
6280   GdkAtom target;
6281   GdkDragAction suggested_action = 0;
6282   
6283   text_view = GTK_TEXT_VIEW (widget);
6284
6285   target_rect = text_view->text_window->allocation;
6286   
6287   if (x < target_rect.x ||
6288       y < target_rect.y ||
6289       x > (target_rect.x + target_rect.width) ||
6290       y > (target_rect.y + target_rect.height))
6291     return FALSE; /* outside the text window, allow parent widgets to handle event */
6292
6293   gtk_text_view_window_to_buffer_coords (text_view,
6294                                          GTK_TEXT_WINDOW_WIDGET,
6295                                          x, y,
6296                                          &bx, &by);
6297
6298   gtk_text_layout_get_iter_at_pixel (text_view->layout,
6299                                      &newplace,
6300                                      bx, by);  
6301
6302   target = gtk_drag_dest_find_target (widget, context,
6303                                       gtk_drag_dest_get_target_list (widget));
6304
6305   if (target == GDK_NONE)
6306     {
6307       /* can't accept any of the offered targets */
6308     }                                 
6309   else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6310                                                  &start, &end) &&
6311            gtk_text_iter_compare (&newplace, &start) >= 0 &&
6312            gtk_text_iter_compare (&newplace, &end) <= 0)
6313     {
6314       /* We're inside the selection. */
6315     }
6316   else
6317     {      
6318       if (gtk_text_iter_can_insert (&newplace, text_view->editable))
6319         {
6320           GtkWidget *source_widget;
6321           
6322           suggested_action = context->suggested_action;
6323           
6324           source_widget = gtk_drag_get_source_widget (context);
6325           
6326           if (source_widget == widget)
6327             {
6328               /* Default to MOVE, unless the user has
6329                * pressed ctrl or alt to affect available actions
6330                */
6331               if ((context->actions & GDK_ACTION_MOVE) != 0)
6332                 suggested_action = GDK_ACTION_MOVE;
6333             }
6334         }
6335       else
6336         {
6337           /* Can't drop here. */
6338         }
6339     }
6340
6341   if (suggested_action != 0)
6342     {
6343       gtk_text_mark_set_visible (text_view->dnd_mark,
6344                                  text_view->cursor_visible);
6345       
6346       gdk_drag_status (context, suggested_action, time);
6347     }
6348   else
6349     {
6350       gdk_drag_status (context, 0, time);
6351       gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6352     }
6353       
6354   if (!text_view->scroll_timeout)
6355     text_view->scroll_timeout =
6356       gdk_threads_add_timeout (100, drag_scan_timeout, text_view);
6357
6358   /* TRUE return means don't propagate the drag motion to parent
6359    * widgets that may also be drop sites.
6360    */
6361   return TRUE;
6362 }
6363
6364 static gboolean
6365 gtk_text_view_drag_drop (GtkWidget        *widget,
6366                          GdkDragContext   *context,
6367                          gint              x,
6368                          gint              y,
6369                          guint             time)
6370 {
6371   GtkTextView *text_view;
6372   GtkTextIter drop_point;
6373   GdkAtom target = GDK_NONE;
6374   
6375   text_view = GTK_TEXT_VIEW (widget);
6376   
6377   if (text_view->scroll_timeout != 0)
6378     g_source_remove (text_view->scroll_timeout);
6379
6380   text_view->scroll_timeout = 0;
6381
6382   gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
6383
6384   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6385                                     &drop_point,
6386                                     text_view->dnd_mark);
6387
6388   if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
6389     target = gtk_drag_dest_find_target (widget, context, NULL);
6390
6391   if (target != GDK_NONE)
6392     gtk_drag_get_data (widget, context, target, time);
6393   else
6394     gtk_drag_finish (context, FALSE, FALSE, time);
6395
6396   return TRUE;
6397 }
6398
6399 static void
6400 insert_text_data (GtkTextView      *text_view,
6401                   GtkTextIter      *drop_point,
6402                   GtkSelectionData *selection_data)
6403 {
6404   guchar *str;
6405
6406   str = gtk_selection_data_get_text (selection_data);
6407
6408   if (str)
6409     {
6410       if (!gtk_text_buffer_insert_interactive (get_buffer (text_view),
6411                                                drop_point, (gchar *) str, -1,
6412                                                text_view->editable))
6413         {
6414           gtk_widget_error_bell (GTK_WIDGET (text_view));
6415         }
6416
6417       g_free (str);
6418     }
6419 }
6420
6421 static void
6422 gtk_text_view_drag_data_received (GtkWidget        *widget,
6423                                   GdkDragContext   *context,
6424                                   gint              x,
6425                                   gint              y,
6426                                   GtkSelectionData *selection_data,
6427                                   guint             info,
6428                                   guint             time)
6429 {
6430   GtkTextIter drop_point;
6431   GtkTextView *text_view;
6432   gboolean success = FALSE;
6433   GtkTextBuffer *buffer = NULL;
6434
6435   text_view = GTK_TEXT_VIEW (widget);
6436
6437   if (!text_view->dnd_mark)
6438     goto done;
6439
6440   buffer = get_buffer (text_view);
6441
6442   gtk_text_buffer_get_iter_at_mark (buffer,
6443                                     &drop_point,
6444                                     text_view->dnd_mark);
6445   
6446   if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
6447     goto done;
6448
6449   success = TRUE;
6450
6451   gtk_text_buffer_begin_user_action (buffer);
6452
6453   if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
6454     {
6455       GtkTextBuffer *src_buffer = NULL;
6456       GtkTextIter start, end;
6457       gboolean copy_tags = TRUE;
6458
6459       if (selection_data->length != sizeof (src_buffer))
6460         return;
6461
6462       memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
6463
6464       if (src_buffer == NULL)
6465         return;
6466
6467       g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
6468
6469       if (gtk_text_buffer_get_tag_table (src_buffer) !=
6470           gtk_text_buffer_get_tag_table (buffer))
6471         {
6472           /*  try to find a suitable rich text target instead  */
6473           GdkAtom *atoms;
6474           gint     n_atoms;
6475           GList   *list;
6476           GdkAtom  target = GDK_NONE;
6477
6478           copy_tags = FALSE;
6479
6480           atoms = gtk_text_buffer_get_deserialize_formats (buffer, &n_atoms);
6481
6482           for (list = context->targets; list; list = g_list_next (list))
6483             {
6484               gint i;
6485
6486               for (i = 0; i < n_atoms; i++)
6487                 if (GUINT_TO_POINTER (atoms[i]) == list->data)
6488                   {
6489                     target = atoms[i];
6490                     break;
6491                   }
6492             }
6493
6494           g_free (atoms);
6495
6496           if (target != GDK_NONE)
6497             {
6498               gtk_drag_get_data (widget, context, target, time);
6499               gtk_text_buffer_end_user_action (buffer);
6500               return;
6501             }
6502         }
6503
6504       if (gtk_text_buffer_get_selection_bounds (src_buffer,
6505                                                 &start,
6506                                                 &end))
6507         {
6508           if (copy_tags)
6509             gtk_text_buffer_insert_range_interactive (buffer,
6510                                                       &drop_point,
6511                                                       &start,
6512                                                       &end,
6513                                                       text_view->editable);
6514           else
6515             {
6516               gchar *str;
6517
6518               str = gtk_text_iter_get_visible_text (&start, &end);
6519               gtk_text_buffer_insert_interactive (buffer,
6520                                                   &drop_point, str, -1,
6521                                                   text_view->editable);
6522               g_free (str);
6523             }
6524         }
6525     }
6526   else if (selection_data->length > 0 &&
6527            info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
6528     {
6529       gboolean retval;
6530       GError *error = NULL;
6531
6532       retval = gtk_text_buffer_deserialize (buffer, buffer,
6533                                             selection_data->target,
6534                                             &drop_point,
6535                                             (guint8 *) selection_data->data,
6536                                             selection_data->length,
6537                                             &error);
6538
6539       if (!retval)
6540         {
6541           g_warning ("error pasting: %s\n", error->message);
6542           g_clear_error (&error);
6543         }
6544     }
6545   else
6546     insert_text_data (text_view, &drop_point, selection_data);
6547
6548  done:
6549   gtk_drag_finish (context, success,
6550                    success && context->action == GDK_ACTION_MOVE,
6551                    time);
6552
6553   if (success)
6554     {
6555       gtk_text_buffer_get_iter_at_mark (buffer,
6556                                         &drop_point,
6557                                         text_view->dnd_mark);
6558       gtk_text_buffer_place_cursor (buffer, &drop_point);
6559
6560       gtk_text_buffer_end_user_action (buffer);
6561     }
6562 }
6563
6564 static GtkAdjustment*
6565 get_hadjustment (GtkTextView *text_view)
6566 {
6567   if (text_view->hadjustment == NULL)
6568     gtk_text_view_set_scroll_adjustments (text_view,
6569                                           NULL, /* forces creation */
6570                                           text_view->vadjustment);
6571
6572   return text_view->hadjustment;
6573 }
6574
6575 static GtkAdjustment*
6576 get_vadjustment (GtkTextView *text_view)
6577 {
6578   if (text_view->vadjustment == NULL)
6579     gtk_text_view_set_scroll_adjustments (text_view,
6580                                           text_view->hadjustment,
6581                                           NULL); /* forces creation */
6582   return text_view->vadjustment;
6583 }
6584
6585
6586 static void
6587 gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
6588                                       GtkAdjustment *hadj,
6589                                       GtkAdjustment *vadj)
6590 {
6591   gboolean need_adjust = FALSE;
6592
6593   if (hadj)
6594     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
6595   else
6596     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6597   if (vadj)
6598     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
6599   else
6600     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
6601
6602   if (text_view->hadjustment && (text_view->hadjustment != hadj))
6603     {
6604       g_signal_handlers_disconnect_by_func (text_view->hadjustment,
6605                                             gtk_text_view_value_changed,
6606                                             text_view);
6607       g_object_unref (text_view->hadjustment);
6608     }
6609
6610   if (text_view->vadjustment && (text_view->vadjustment != vadj))
6611     {
6612       g_signal_handlers_disconnect_by_func (text_view->vadjustment,
6613                                             gtk_text_view_value_changed,
6614                                             text_view);
6615       g_object_unref (text_view->vadjustment);
6616     }
6617
6618   if (text_view->hadjustment != hadj)
6619     {
6620       text_view->hadjustment = hadj;
6621       g_object_ref_sink (text_view->hadjustment);
6622       
6623       g_signal_connect (text_view->hadjustment, "value_changed",
6624                         G_CALLBACK (gtk_text_view_value_changed),
6625                         text_view);
6626       need_adjust = TRUE;
6627     }
6628
6629   if (text_view->vadjustment != vadj)
6630     {
6631       text_view->vadjustment = vadj;
6632       g_object_ref_sink (text_view->vadjustment);
6633       
6634       g_signal_connect (text_view->vadjustment, "value_changed",
6635                         G_CALLBACK (gtk_text_view_value_changed),
6636                         text_view);
6637       need_adjust = TRUE;
6638     }
6639
6640   if (need_adjust)
6641     gtk_text_view_value_changed (NULL, text_view);
6642 }
6643
6644 /* FIXME this adjust_allocation is a big cut-and-paste from
6645  * GtkCList, needs to be some "official" way to do this
6646  * factored out.
6647  */
6648 typedef struct
6649 {
6650   GdkWindow *window;
6651   int dx;
6652   int dy;
6653 } ScrollData;
6654
6655 /* The window to which widget->window is relative */
6656 #define ALLOCATION_WINDOW(widget)               \
6657    (GTK_WIDGET_NO_WINDOW (widget) ?             \
6658     (widget)->window :                          \
6659      gdk_window_get_parent ((widget)->window))
6660
6661 static void
6662 adjust_allocation_recurse (GtkWidget *widget,
6663                            gpointer   data)
6664 {
6665   ScrollData *scroll_data = data;
6666
6667   /* Need to really size allocate instead of just poking
6668    * into widget->allocation if the widget is not realized.
6669    * FIXME someone figure out why this was.
6670    */
6671   if (!GTK_WIDGET_REALIZED (widget))
6672     {
6673       if (GTK_WIDGET_VISIBLE (widget))
6674         {
6675           GdkRectangle tmp_rectangle = widget->allocation;
6676           tmp_rectangle.x += scroll_data->dx;
6677           tmp_rectangle.y += scroll_data->dy;
6678           
6679           gtk_widget_size_allocate (widget, &tmp_rectangle);
6680         }
6681     }
6682   else
6683     {
6684       if (ALLOCATION_WINDOW (widget) == scroll_data->window)
6685         {
6686           widget->allocation.x += scroll_data->dx;
6687           widget->allocation.y += scroll_data->dy;
6688           
6689           if (GTK_IS_CONTAINER (widget))
6690             gtk_container_forall (GTK_CONTAINER (widget),
6691                                   adjust_allocation_recurse,
6692                                   data);
6693         }
6694     }
6695 }
6696
6697 static void
6698 adjust_allocation (GtkWidget *widget,
6699                    int        dx,
6700                    int        dy)
6701 {
6702   ScrollData scroll_data;
6703
6704   if (GTK_WIDGET_REALIZED (widget))
6705     scroll_data.window = ALLOCATION_WINDOW (widget);
6706   else
6707     scroll_data.window = NULL;
6708     
6709   scroll_data.dx = dx;
6710   scroll_data.dy = dy;
6711   
6712   adjust_allocation_recurse (widget, &scroll_data);
6713 }
6714             
6715 static void
6716 gtk_text_view_value_changed (GtkAdjustment *adj,
6717                              GtkTextView   *text_view)
6718 {
6719   GtkTextIter iter;
6720   gint line_top;
6721   gint dx = 0;
6722   gint dy = 0;
6723   
6724   /* Note that we oddly call this function with adj == NULL
6725    * sometimes
6726    */
6727   
6728   text_view->onscreen_validated = FALSE;
6729
6730   DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
6731              adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
6732              adj ? adj->value : 0.0));
6733   
6734   if (adj == text_view->hadjustment)
6735     {
6736       dx = text_view->xoffset - (gint)adj->value;
6737       text_view->xoffset = adj->value;
6738
6739       /* If the change is due to a size change we need 
6740        * to invalidate the entire text window because there might be
6741        * right-aligned or centered text 
6742        */
6743       if (text_view->width_changed)
6744         {
6745           if (GTK_WIDGET_REALIZED (text_view))
6746             gdk_window_invalidate_rect (text_view->text_window->bin_window, NULL, FALSE);
6747           
6748           text_view->width_changed = FALSE;
6749         }
6750     }
6751   else if (adj == text_view->vadjustment)
6752     {
6753       dy = text_view->yoffset - (gint)adj->value;
6754       text_view->yoffset = adj->value;
6755
6756       if (text_view->layout)
6757         {
6758           gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
6759
6760           gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
6761
6762           text_view->first_para_pixels = adj->value - line_top;
6763         }
6764     }
6765   
6766   if (dx != 0 || dy != 0)
6767     {
6768       GSList *tmp_list;
6769
6770       if (GTK_WIDGET_REALIZED (text_view))
6771         {
6772           if (dy != 0)
6773             {
6774               if (text_view->left_window)
6775                 text_window_scroll (text_view->left_window, 0, dy);
6776               if (text_view->right_window)
6777                 text_window_scroll (text_view->right_window, 0, dy);
6778             }
6779       
6780           if (dx != 0)
6781             {
6782               if (text_view->top_window)
6783                 text_window_scroll (text_view->top_window, dx, 0);
6784               if (text_view->bottom_window)
6785                 text_window_scroll (text_view->bottom_window, dx, 0);
6786             }
6787       
6788           /* It looks nicer to scroll the main area last, because
6789            * it takes a while, and making the side areas update
6790            * afterward emphasizes the slowness of scrolling the
6791            * main area.
6792            */
6793           text_window_scroll (text_view->text_window, dx, dy);
6794         }
6795       
6796       /* Children are now "moved" in the text window, poke
6797        * into widget->allocation for each child
6798        */
6799       tmp_list = text_view->children;
6800       while (tmp_list != NULL)
6801         {
6802           GtkTextViewChild *child = tmp_list->data;
6803           
6804           if (child->anchor)
6805             adjust_allocation (child->widget, dx, dy);
6806           
6807           tmp_list = g_slist_next (tmp_list);
6808         }
6809     }
6810
6811   /* This could result in invalidation, which would install the
6812    * first_validate_idle, which would validate onscreen;
6813    * but we're going to go ahead and validate here, so
6814    * first_validate_idle shouldn't have anything to do.
6815    */
6816   gtk_text_view_update_layout_width (text_view);
6817   
6818   /* note that validation of onscreen could invoke this function
6819    * recursively, by scrolling to maintain first_para, or in response
6820    * to updating the layout width, however there is no problem with
6821    * that, or shouldn't be.
6822    */
6823   gtk_text_view_validate_onscreen (text_view);
6824   
6825   /* process exposes */
6826   if (GTK_WIDGET_REALIZED (text_view))
6827     {
6828       DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6829       
6830       if (text_view->left_window)
6831         gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6832
6833       if (text_view->right_window)
6834         gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6835
6836       if (text_view->top_window)
6837         gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6838       
6839       if (text_view->bottom_window)
6840         gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6841   
6842       gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6843     }
6844
6845   /* If this got installed, get rid of it, it's just a waste of time. */
6846   if (text_view->first_validate_idle != 0)
6847     {
6848       g_source_remove (text_view->first_validate_idle);
6849       text_view->first_validate_idle = 0;
6850     }
6851
6852   gtk_text_view_update_im_spot_location (text_view);
6853   
6854   DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6855 }
6856
6857 static void
6858 gtk_text_view_commit_handler (GtkIMContext  *context,
6859                               const gchar   *str,
6860                               GtkTextView   *text_view)
6861 {
6862   gtk_text_view_commit_text (text_view, str);
6863 }
6864
6865 static void
6866 gtk_text_view_commit_text (GtkTextView   *text_view,
6867                            const gchar   *str)
6868 {
6869   gboolean had_selection;
6870   
6871   gtk_text_buffer_begin_user_action (get_buffer (text_view));
6872
6873   had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6874                                                         NULL, NULL);
6875   
6876   gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6877                                     text_view->editable);
6878
6879   if (!strcmp (str, "\n"))
6880     {
6881       if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6882                                                          text_view->editable))
6883         {
6884           gtk_widget_error_bell (GTK_WIDGET (text_view));
6885         }
6886     }
6887   else
6888     {
6889       if (!had_selection && text_view->overwrite_mode)
6890         {
6891           GtkTextIter insert;
6892           
6893           gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6894                                             &insert,
6895                                             gtk_text_buffer_get_mark (get_buffer (text_view),
6896                                                                       "insert"));
6897           if (!gtk_text_iter_ends_line (&insert))
6898             gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6899         }
6900
6901       if (!gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6902                                                          text_view->editable))
6903         {
6904           gtk_widget_error_bell (GTK_WIDGET (text_view));
6905         }
6906     }
6907
6908   gtk_text_buffer_end_user_action (get_buffer (text_view));
6909
6910   gtk_text_view_set_virtual_cursor_pos (text_view, -1, -1);
6911   DV(g_print (G_STRLOC": scrolling onscreen\n"));
6912   gtk_text_view_scroll_mark_onscreen (text_view,
6913                                       gtk_text_buffer_get_mark (get_buffer (text_view),
6914                                                                 "insert"));
6915 }
6916
6917 static void
6918 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6919                                        GtkTextView  *text_view)
6920 {
6921   gchar *str;
6922   PangoAttrList *attrs;
6923   gint cursor_pos;
6924
6925   gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6926   gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6927   pango_attr_list_unref (attrs);
6928   g_free (str);
6929
6930   if (GTK_WIDGET_HAS_FOCUS (text_view))
6931     gtk_text_view_scroll_mark_onscreen (text_view,
6932                                         gtk_text_buffer_get_mark (get_buffer (text_view),
6933                                                                   "insert"));
6934 }
6935
6936 static gboolean
6937 gtk_text_view_retrieve_surrounding_handler (GtkIMContext  *context,
6938                                             GtkTextView   *text_view)
6939 {
6940   GtkTextIter start;
6941   GtkTextIter end;
6942   gint pos;
6943   gchar *text;
6944
6945   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
6946                                     gtk_text_buffer_get_insert (text_view->buffer));
6947   end = start;
6948
6949   pos = gtk_text_iter_get_line_index (&start);
6950   gtk_text_iter_set_line_offset (&start, 0);
6951   gtk_text_iter_forward_to_line_end (&end);
6952
6953   text = gtk_text_iter_get_slice (&start, &end);
6954   gtk_im_context_set_surrounding (context, text, -1, pos);
6955   g_free (text);
6956
6957   return TRUE;
6958 }
6959
6960 static gboolean
6961 gtk_text_view_delete_surrounding_handler (GtkIMContext  *context,
6962                                           gint           offset,
6963                                           gint           n_chars,
6964                                           GtkTextView   *text_view)
6965 {
6966   GtkTextIter start;
6967   GtkTextIter end;
6968
6969   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
6970                                     gtk_text_buffer_get_insert (text_view->buffer));
6971   end = start;
6972
6973   gtk_text_iter_forward_chars (&start, offset);
6974   gtk_text_iter_forward_chars (&end, offset + n_chars);
6975
6976   gtk_text_buffer_delete_interactive (text_view->buffer, &start, &end,
6977                                       text_view->editable);
6978
6979   return TRUE;
6980 }
6981
6982 static void
6983 gtk_text_view_mark_set_handler (GtkTextBuffer     *buffer,
6984                                 const GtkTextIter *location,
6985                                 GtkTextMark       *mark,
6986                                 gpointer           data)
6987 {
6988   GtkTextView *text_view = GTK_TEXT_VIEW (data);
6989   gboolean need_reset = FALSE;
6990
6991   if (mark == gtk_text_buffer_get_insert (buffer))
6992     {
6993       text_view->virtual_cursor_x = -1;
6994       text_view->virtual_cursor_y = -1;
6995       gtk_text_view_update_im_spot_location (text_view);
6996       need_reset = TRUE;
6997     }
6998   else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6999     {
7000       need_reset = TRUE;
7001     }
7002
7003   if (need_reset)
7004     gtk_text_view_reset_im_context (text_view);
7005 }
7006
7007 static void
7008 gtk_text_view_target_list_notify (GtkTextBuffer    *buffer,
7009                                   const GParamSpec *pspec,
7010                                   gpointer          data)
7011 {
7012   GtkWidget     *widget = GTK_WIDGET (data);
7013   GtkTargetList *view_list;
7014   GtkTargetList *buffer_list;
7015   GList         *list;
7016
7017   view_list = gtk_drag_dest_get_target_list (widget);
7018   buffer_list = gtk_text_buffer_get_paste_target_list (buffer);
7019
7020   if (view_list)
7021     gtk_target_list_ref (view_list);
7022   else
7023     view_list = gtk_target_list_new (NULL, 0);
7024
7025   list = view_list->list;
7026   while (list)
7027     {
7028       GtkTargetPair *pair = list->data;
7029
7030       list = g_list_next (list); /* get next element before removing */
7031
7032       if (pair->info >= GTK_TEXT_BUFFER_TARGET_INFO_TEXT &&
7033           pair->info <= GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
7034         {
7035           gtk_target_list_remove (view_list, pair->target);
7036         }
7037     }
7038
7039   for (list = buffer_list->list; list; list = g_list_next (list))
7040     {
7041       GtkTargetPair *pair = list->data;
7042
7043       gtk_target_list_add (view_list, pair->target, pair->flags, pair->info);
7044     }
7045
7046   gtk_drag_dest_set_target_list (widget, view_list);
7047   gtk_target_list_unref (view_list);
7048 }
7049
7050 static void
7051 gtk_text_view_get_cursor_location  (GtkTextView   *text_view,
7052                                     GdkRectangle  *pos)
7053 {
7054   GtkTextIter insert;
7055   
7056   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
7057                                     gtk_text_buffer_get_mark (get_buffer (text_view),
7058                                                               "insert"));
7059
7060   gtk_text_layout_get_cursor_locations (text_view->layout, &insert, pos, NULL);
7061 }
7062
7063 static void
7064 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
7065                                       gint        *x,
7066                                       gint        *y)
7067 {
7068   GdkRectangle pos;
7069
7070   if ((x && text_view->virtual_cursor_x == -1) ||
7071       (y && text_view->virtual_cursor_y == -1))
7072     gtk_text_view_get_cursor_location (text_view, &pos);
7073
7074   if (x)
7075     {
7076       if (text_view->virtual_cursor_x != -1)
7077         *x = text_view->virtual_cursor_x;
7078       else
7079         *x = pos.x;
7080     }
7081
7082   if (y)
7083     {
7084       if (text_view->virtual_cursor_x != -1)
7085         *y = text_view->virtual_cursor_y;
7086       else
7087         *y = pos.y + pos.height / 2;
7088     }
7089 }
7090
7091 static void
7092 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
7093                                       gint         x,
7094                                       gint         y)
7095 {
7096   GdkRectangle pos;
7097
7098   if (!text_view->layout)
7099     return;
7100
7101   if (x == -1 || y == -1)
7102     gtk_text_view_get_cursor_location (text_view, &pos);
7103
7104   text_view->virtual_cursor_x = (x == -1) ? pos.x : x;
7105   text_view->virtual_cursor_y = (y == -1) ? pos.y + pos.height / 2 : y;
7106 }
7107
7108 /* Quick hack of a popup menu
7109  */
7110 static void
7111 activate_cb (GtkWidget   *menuitem,
7112              GtkTextView *text_view)
7113 {
7114   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
7115   g_signal_emit_by_name (text_view, signal);
7116 }
7117
7118 static void
7119 append_action_signal (GtkTextView  *text_view,
7120                       GtkWidget    *menu,
7121                       const gchar  *stock_id,
7122                       const gchar  *signal,
7123                       gboolean      sensitive)
7124 {
7125   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
7126
7127   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
7128   g_signal_connect (menuitem, "activate",
7129                     G_CALLBACK (activate_cb), text_view);
7130
7131   gtk_widget_set_sensitive (menuitem, sensitive);
7132   
7133   gtk_widget_show (menuitem);
7134   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
7135 }
7136
7137 static void
7138 gtk_text_view_select_all (GtkWidget *widget,
7139                           gboolean select)
7140 {
7141   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
7142   GtkTextBuffer *buffer;
7143   GtkTextIter start_iter, end_iter, insert;
7144
7145   buffer = text_view->buffer;
7146   if (select) 
7147     {
7148       gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
7149       gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
7150     }
7151   else 
7152     {
7153       gtk_text_buffer_get_iter_at_mark (buffer, &insert,
7154                                         gtk_text_buffer_get_insert (buffer));
7155       gtk_text_buffer_move_mark_by_name (buffer, "selection_bound", &insert);
7156     }
7157 }
7158
7159 static void
7160 select_all_cb (GtkWidget   *menuitem,
7161                GtkTextView *text_view)
7162 {
7163   gtk_text_view_select_all (GTK_WIDGET (text_view), TRUE);
7164 }
7165
7166 static void
7167 delete_cb (GtkTextView *text_view)
7168 {
7169   gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
7170                                     text_view->editable);
7171 }
7172
7173 static void
7174 popup_menu_detach (GtkWidget *attach_widget,
7175                    GtkMenu   *menu)
7176 {
7177   GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
7178 }
7179
7180 static void
7181 popup_position_func (GtkMenu   *menu,
7182                      gint      *x,
7183                      gint      *y,
7184                      gboolean  *push_in,
7185                      gpointer   user_data)
7186 {
7187   GtkTextView *text_view;
7188   GtkWidget *widget;
7189   GdkRectangle cursor_rect;
7190   GdkRectangle onscreen_rect;
7191   gint root_x, root_y;
7192   GtkTextIter iter;
7193   GtkRequisition req;      
7194   GdkScreen *screen;
7195   gint monitor_num;
7196   GdkRectangle monitor;
7197       
7198   text_view = GTK_TEXT_VIEW (user_data);
7199   widget = GTK_WIDGET (text_view);
7200   
7201   g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
7202   
7203   screen = gtk_widget_get_screen (widget);
7204
7205   gdk_window_get_origin (widget->window, &root_x, &root_y);
7206
7207   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7208                                     &iter,
7209                                     gtk_text_buffer_get_insert (get_buffer (text_view)));
7210
7211   gtk_text_view_get_iter_location (text_view,
7212                                    &iter,
7213                                    &cursor_rect);
7214
7215   gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
7216   
7217   gtk_widget_size_request (text_view->popup_menu, &req);
7218
7219   /* can't use rectangle_intersect since cursor rect can have 0 width */
7220   if (cursor_rect.x >= onscreen_rect.x &&
7221       cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
7222       cursor_rect.y >= onscreen_rect.y &&
7223       cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
7224     {    
7225       gtk_text_view_buffer_to_window_coords (text_view,
7226                                              GTK_TEXT_WINDOW_WIDGET,
7227                                              cursor_rect.x, cursor_rect.y,
7228                                              &cursor_rect.x, &cursor_rect.y);
7229
7230       *x = root_x + cursor_rect.x + cursor_rect.width;
7231       *y = root_y + cursor_rect.y + cursor_rect.height;
7232     }
7233   else
7234     {
7235       /* Just center the menu, since cursor is offscreen. */      
7236       *x = root_x + (widget->allocation.width / 2 - req.width / 2);
7237       *y = root_y + (widget->allocation.height / 2 - req.height / 2);      
7238     }
7239   
7240   /* Ensure sanity */
7241   *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
7242   *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
7243
7244   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
7245   gtk_menu_set_monitor (menu, monitor_num);
7246   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
7247
7248   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
7249   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
7250
7251   *push_in = FALSE;
7252 }
7253
7254 typedef struct
7255 {
7256   GtkTextView *text_view;
7257   gint button;
7258   guint time;
7259 } PopupInfo;
7260
7261 static gboolean
7262 range_contains_editable_text (const GtkTextIter *start,
7263                               const GtkTextIter *end,
7264                               gboolean default_editability)
7265 {
7266   GtkTextIter iter = *start;
7267
7268   while (gtk_text_iter_compare (&iter, end) < 0)
7269     {
7270       if (gtk_text_iter_editable (&iter, default_editability))
7271         return TRUE;
7272       
7273       gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
7274     }
7275
7276   return FALSE;
7277 }                             
7278
7279 static void
7280 unichar_chosen_func (const char *text,
7281                      gpointer    data)
7282 {
7283   GtkTextView *text_view = GTK_TEXT_VIEW (data);
7284
7285   gtk_text_view_commit_text (text_view, text);
7286 }
7287
7288 static void
7289 popup_targets_received (GtkClipboard     *clipboard,
7290                         GtkSelectionData *data,
7291                         gpointer          user_data)
7292 {
7293   PopupInfo *info = user_data;
7294   GtkTextView *text_view = info->text_view;
7295   
7296   if (GTK_WIDGET_REALIZED (text_view))
7297     {
7298       /* We implicitely rely here on the fact that if we are pasting ourself, we'll
7299        * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
7300        */
7301       gboolean clipboard_contains_text;
7302       GtkWidget *menuitem;
7303       GtkWidget *submenu;
7304       gboolean have_selection;
7305       gboolean can_insert;
7306       GtkTextIter iter;
7307       GtkTextIter sel_start, sel_end;
7308       gboolean show_input_method_menu;
7309       gboolean show_unicode_menu;
7310       
7311       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
7312
7313       if (text_view->popup_menu)
7314         gtk_widget_destroy (text_view->popup_menu);
7315
7316       text_view->popup_menu = gtk_menu_new ();
7317       
7318       gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
7319                                  GTK_WIDGET (text_view),
7320                                  popup_menu_detach);
7321       
7322       have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
7323                                                              &sel_start, &sel_end);
7324       
7325       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
7326                                         &iter,
7327                                         gtk_text_buffer_get_insert (get_buffer (text_view)));
7328       
7329       can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
7330       
7331       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
7332                             have_selection &&
7333                             range_contains_editable_text (&sel_start, &sel_end,
7334                                                           text_view->editable));
7335       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
7336                             have_selection);
7337       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
7338                             can_insert && clipboard_contains_text);
7339       
7340       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
7341       gtk_widget_set_sensitive (menuitem, 
7342                                 have_selection &&
7343                                 range_contains_editable_text (&sel_start, &sel_end,
7344                                                               text_view->editable));
7345       g_signal_connect_swapped (menuitem, "activate",
7346                                 G_CALLBACK (delete_cb), text_view);
7347       gtk_widget_show (menuitem);
7348       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7349
7350       menuitem = gtk_separator_menu_item_new ();
7351       gtk_widget_show (menuitem);
7352       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7353
7354       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
7355       g_signal_connect (menuitem, "activate",
7356                         G_CALLBACK (select_all_cb), text_view);
7357       gtk_widget_show (menuitem);
7358       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7359
7360       g_object_get (gtk_widget_get_settings (GTK_WIDGET (text_view)),
7361                     "gtk-show-input-method-menu", &show_input_method_menu,
7362                     "gtk-show-unicode-menu", &show_unicode_menu,
7363                     NULL);
7364       
7365       if (show_input_method_menu || show_unicode_menu)
7366         {
7367           menuitem = gtk_separator_menu_item_new ();
7368           gtk_widget_show (menuitem);
7369           gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7370         }
7371
7372       if (show_input_method_menu)
7373         {
7374           menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
7375           gtk_widget_show (menuitem);
7376           gtk_widget_set_sensitive (menuitem, can_insert);
7377
7378           submenu = gtk_menu_new ();
7379           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7380           gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
7381           
7382           gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
7383                                                 GTK_MENU_SHELL (submenu));
7384         }
7385
7386       if (show_unicode_menu)
7387         {
7388           menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode Control Character"));
7389           gtk_widget_show (menuitem);
7390           gtk_widget_set_sensitive (menuitem, can_insert);
7391       
7392           submenu = gtk_menu_new ();
7393           gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
7394           gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);      
7395           
7396           _gtk_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
7397                                                         unichar_chosen_func,
7398                                                         text_view);
7399         }
7400           
7401       g_signal_emit (text_view,
7402                      signals[POPULATE_POPUP],
7403                      0,
7404                      text_view->popup_menu);
7405       
7406       if (info->button)
7407         gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7408                         NULL, NULL,
7409                         info->button, info->time);
7410       else
7411         {
7412           gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
7413                           popup_position_func, text_view,
7414                           0, gtk_get_current_event_time ());
7415           gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
7416         }
7417     }
7418
7419   g_object_unref (text_view);
7420   g_free (info);
7421 }
7422
7423 static void
7424 gtk_text_view_do_popup (GtkTextView    *text_view,
7425                         GdkEventButton *event)
7426 {
7427   PopupInfo *info = g_new (PopupInfo, 1);
7428
7429   /* In order to know what entries we should make sensitive, we
7430    * ask for the current targets of the clipboard, and when
7431    * we get them, then we actually pop up the menu.
7432    */
7433   info->text_view = g_object_ref (text_view);
7434   
7435   if (event)
7436     {
7437       info->button = event->button;
7438       info->time = event->time;
7439     }
7440   else
7441     {
7442       info->button = 0;
7443       info->time = gtk_get_current_event_time ();
7444     }
7445
7446   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
7447                                                             GDK_SELECTION_CLIPBOARD),
7448                                   gdk_atom_intern_static_string ("TARGETS"),
7449                                   popup_targets_received,
7450                                   info);
7451 }
7452
7453 static gboolean
7454 gtk_text_view_popup_menu (GtkWidget *widget)
7455 {
7456   gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);  
7457   return TRUE;
7458 }
7459
7460 /* Child GdkWindows */
7461
7462
7463 static GtkTextWindow*
7464 text_window_new (GtkTextWindowType  type,
7465                  GtkWidget         *widget,
7466                  gint               width_request,
7467                  gint               height_request)
7468 {
7469   GtkTextWindow *win;
7470
7471   win = g_new (GtkTextWindow, 1);
7472
7473   win->type = type;
7474   win->widget = widget;
7475   win->window = NULL;
7476   win->bin_window = NULL;
7477   win->requisition.width = width_request;
7478   win->requisition.height = height_request;
7479   win->allocation.width = width_request;
7480   win->allocation.height = height_request;
7481   win->allocation.x = 0;
7482   win->allocation.y = 0;
7483
7484   return win;
7485 }
7486
7487 static void
7488 text_window_free (GtkTextWindow *win)
7489 {
7490   if (win->window)
7491     text_window_unrealize (win);
7492
7493   g_free (win);
7494 }
7495
7496 static void
7497 text_window_realize (GtkTextWindow *win,
7498                      GtkWidget     *widget)
7499 {
7500   GdkWindowAttr attributes;
7501   gint attributes_mask;
7502   GdkCursor *cursor;
7503
7504   attributes.window_type = GDK_WINDOW_CHILD;
7505   attributes.x = win->allocation.x;
7506   attributes.y = win->allocation.y;
7507   attributes.width = win->allocation.width;
7508   attributes.height = win->allocation.height;
7509   attributes.wclass = GDK_INPUT_OUTPUT;
7510   attributes.visual = gtk_widget_get_visual (win->widget);
7511   attributes.colormap = gtk_widget_get_colormap (win->widget);
7512   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
7513
7514   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
7515
7516   win->window = gdk_window_new (widget->window,
7517                                 &attributes,
7518                                 attributes_mask);
7519
7520   gdk_window_set_back_pixmap (win->window, NULL, FALSE);
7521   
7522   gdk_window_show (win->window);
7523   gdk_window_set_user_data (win->window, win->widget);
7524   gdk_window_lower (win->window);
7525
7526   attributes.x = 0;
7527   attributes.y = 0;
7528   attributes.width = win->allocation.width;
7529   attributes.height = win->allocation.height;
7530   attributes.event_mask = (GDK_EXPOSURE_MASK            |
7531                            GDK_SCROLL_MASK              |
7532                            GDK_KEY_PRESS_MASK           |
7533                            GDK_BUTTON_PRESS_MASK        |
7534                            GDK_BUTTON_RELEASE_MASK      |
7535                            GDK_POINTER_MOTION_MASK      |
7536                            GDK_POINTER_MOTION_HINT_MASK |
7537                            gtk_widget_get_events (win->widget));
7538
7539   win->bin_window = gdk_window_new (win->window,
7540                                     &attributes,
7541                                     attributes_mask);
7542
7543   gdk_window_show (win->bin_window);
7544   gdk_window_set_user_data (win->bin_window, win->widget);
7545
7546   if (win->type == GTK_TEXT_WINDOW_TEXT)
7547     {
7548       if (GTK_WIDGET_IS_SENSITIVE (widget))
7549         {
7550           /* I-beam cursor */
7551           cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (widget->window),
7552                                                GDK_XTERM);
7553           gdk_window_set_cursor (win->bin_window, cursor);
7554           gdk_cursor_unref (cursor);
7555         } 
7556
7557       gtk_im_context_set_client_window (GTK_TEXT_VIEW (widget)->im_context,
7558                                         win->window);
7559
7560
7561       gdk_window_set_background (win->bin_window,
7562                                  &widget->style->base[GTK_WIDGET_STATE (widget)]);
7563     }
7564   else
7565     {
7566       gdk_window_set_background (win->bin_window,
7567                                  &widget->style->bg[GTK_WIDGET_STATE (widget)]);
7568     }
7569
7570   g_object_set_qdata (G_OBJECT (win->window),
7571                       g_quark_from_static_string ("gtk-text-view-text-window"),
7572                       win);
7573
7574   g_object_set_qdata (G_OBJECT (win->bin_window),
7575                       g_quark_from_static_string ("gtk-text-view-text-window"),
7576                       win);
7577 }
7578
7579 static void
7580 text_window_unrealize (GtkTextWindow *win)
7581 {
7582   if (win->type == GTK_TEXT_WINDOW_TEXT)
7583     {
7584       gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
7585                                         NULL);
7586     }
7587
7588   gdk_window_set_user_data (win->window, NULL);
7589   gdk_window_set_user_data (win->bin_window, NULL);
7590   gdk_window_destroy (win->bin_window);
7591   gdk_window_destroy (win->window);
7592   win->window = NULL;
7593   win->bin_window = NULL;
7594 }
7595
7596 static void
7597 text_window_size_allocate (GtkTextWindow *win,
7598                            GdkRectangle  *rect)
7599 {
7600   win->allocation = *rect;
7601
7602   if (win->window)
7603     {
7604       gdk_window_move_resize (win->window,
7605                               rect->x, rect->y,
7606                               rect->width, rect->height);
7607
7608       gdk_window_resize (win->bin_window,
7609                          rect->width, rect->height);
7610     }
7611 }
7612
7613 static void
7614 text_window_scroll        (GtkTextWindow *win,
7615                            gint           dx,
7616                            gint           dy)
7617 {
7618   if (dx != 0 || dy != 0)
7619     {
7620       gdk_window_scroll (win->bin_window, dx, dy);
7621     }
7622 }
7623
7624 static void
7625 text_window_invalidate_rect (GtkTextWindow *win,
7626                              GdkRectangle  *rect)
7627 {
7628   GdkRectangle window_rect;
7629
7630   gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
7631                                          win->type,
7632                                          rect->x,
7633                                          rect->y,
7634                                          &window_rect.x,
7635                                          &window_rect.y);
7636
7637   window_rect.width = rect->width;
7638   window_rect.height = rect->height;
7639   
7640   /* Adjust the rect as appropriate */
7641   
7642   switch (win->type)
7643     {
7644     case GTK_TEXT_WINDOW_TEXT:
7645       break;
7646
7647     case GTK_TEXT_WINDOW_LEFT:
7648     case GTK_TEXT_WINDOW_RIGHT:
7649       window_rect.x = 0;
7650       window_rect.width = win->allocation.width;
7651       break;
7652
7653     case GTK_TEXT_WINDOW_TOP:
7654     case GTK_TEXT_WINDOW_BOTTOM:
7655       window_rect.y = 0;
7656       window_rect.height = win->allocation.height;
7657       break;
7658
7659     default:
7660       g_warning ("%s: bug!", G_STRLOC);
7661       return;
7662       break;
7663     }
7664           
7665   gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
7666
7667 #if 0
7668   {
7669     cairo_t *cr = gdk_cairo_create (win->bin_window);
7670     gdk_cairo_rectangle (cr, &window_rect);
7671     cairo_set_source_rgb  (cr, 1.0, 0.0, 0.0);  /* red */
7672     cairo_fill (cr);
7673     cairo_destroy (cr);
7674   }
7675 #endif
7676 }
7677
7678 static void
7679 text_window_invalidate_cursors (GtkTextWindow *win)
7680 {
7681   GtkTextView *text_view = GTK_TEXT_VIEW (win->widget);
7682   GtkTextIter  iter;
7683   GdkRectangle strong;
7684   GdkRectangle weak;
7685   gboolean     draw_arrow;
7686   gfloat       cursor_aspect_ratio;
7687   gint         stem_width;
7688   gint         arrow_width;
7689
7690   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &iter,
7691                                     gtk_text_buffer_get_insert (text_view->buffer));
7692
7693   gtk_text_layout_get_cursor_locations (text_view->layout, &iter,
7694                                         &strong, &weak);
7695
7696   /* cursor width calculation as in gtkstyle.c:draw_insertion_cursor(),
7697    * ignoring the text direction be exposing both sides of the cursor
7698    */
7699
7700   draw_arrow = (strong.x != weak.x || strong.y != weak.y);
7701
7702   gtk_widget_style_get (win->widget,
7703                         "cursor-aspect-ratio", &cursor_aspect_ratio,
7704                         NULL);
7705   
7706   stem_width = strong.height * cursor_aspect_ratio + 1;
7707   arrow_width = stem_width + 1;
7708
7709   strong.width = stem_width;
7710
7711   /* round up to the next even number */
7712   if (stem_width & 1)
7713     stem_width++;
7714
7715   strong.x     -= stem_width / 2;
7716   strong.width += stem_width;
7717
7718   if (draw_arrow)
7719     {
7720       strong.x     -= arrow_width;
7721       strong.width += arrow_width * 2;
7722     }
7723
7724   text_window_invalidate_rect (win, &strong);
7725
7726   if (draw_arrow) /* == have weak */
7727     {
7728       stem_width = weak.height * cursor_aspect_ratio + 1;
7729       arrow_width = stem_width + 1;
7730
7731       weak.width = stem_width;
7732
7733       /* round up to the next even number */
7734       if (stem_width & 1)
7735         stem_width++;
7736
7737       weak.x     -= stem_width / 2;
7738       weak.width += stem_width;
7739
7740       weak.x     -= arrow_width;
7741       weak.width += arrow_width * 2;
7742
7743       text_window_invalidate_rect (win, &weak);
7744     }
7745 }
7746
7747 static gint
7748 text_window_get_width (GtkTextWindow *win)
7749 {
7750   return win->allocation.width;
7751 }
7752
7753 static gint
7754 text_window_get_height (GtkTextWindow *win)
7755 {
7756   return win->allocation.height;
7757 }
7758
7759 /* Windows */
7760
7761
7762 /**
7763  * gtk_text_view_get_window:
7764  * @text_view: a #GtkTextView
7765  * @win: window to get
7766  *
7767  * Retrieves the #GdkWindow corresponding to an area of the text view;
7768  * possible windows include the overall widget window, child windows
7769  * on the left, right, top, bottom, and the window that displays the
7770  * text buffer. Windows are %NULL and nonexistent if their width or
7771  * height is 0, and are nonexistent before the widget has been
7772  * realized.
7773  *
7774  * Return value: a #GdkWindow, or %NULL
7775  **/
7776 GdkWindow*
7777 gtk_text_view_get_window (GtkTextView *text_view,
7778                           GtkTextWindowType win)
7779 {
7780   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
7781
7782   switch (win)
7783     {
7784     case GTK_TEXT_WINDOW_WIDGET:
7785       return GTK_WIDGET (text_view)->window;
7786       break;
7787
7788     case GTK_TEXT_WINDOW_TEXT:
7789       return text_view->text_window->bin_window;
7790       break;
7791
7792     case GTK_TEXT_WINDOW_LEFT:
7793       if (text_view->left_window)
7794         return text_view->left_window->bin_window;
7795       else
7796         return NULL;
7797       break;
7798
7799     case GTK_TEXT_WINDOW_RIGHT:
7800       if (text_view->right_window)
7801         return text_view->right_window->bin_window;
7802       else
7803         return NULL;
7804       break;
7805
7806     case GTK_TEXT_WINDOW_TOP:
7807       if (text_view->top_window)
7808         return text_view->top_window->bin_window;
7809       else
7810         return NULL;
7811       break;
7812
7813     case GTK_TEXT_WINDOW_BOTTOM:
7814       if (text_view->bottom_window)
7815         return text_view->bottom_window->bin_window;
7816       else
7817         return NULL;
7818       break;
7819
7820     case GTK_TEXT_WINDOW_PRIVATE:
7821       g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
7822       return NULL;
7823       break;
7824     }
7825
7826   g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
7827   return NULL;
7828 }
7829
7830 /**
7831  * gtk_text_view_get_window_type:
7832  * @text_view: a #GtkTextView
7833  * @window: a window type
7834  *
7835  * Usually used to find out which window an event corresponds to.
7836  * If you connect to an event signal on @text_view, this function
7837  * should be called on <literal>event-&gt;window</literal> to
7838  * see which window it was.
7839  *
7840  * Return value: the window type.
7841  **/
7842 GtkTextWindowType
7843 gtk_text_view_get_window_type (GtkTextView *text_view,
7844                                GdkWindow   *window)
7845 {
7846   GtkTextWindow *win;
7847
7848   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7849   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
7850
7851   if (window == GTK_WIDGET (text_view)->window)
7852     return GTK_TEXT_WINDOW_WIDGET;
7853
7854   win = g_object_get_qdata (G_OBJECT (window),
7855                             g_quark_try_string ("gtk-text-view-text-window"));
7856
7857   if (win)
7858     return win->type;
7859   else
7860     {
7861       return GTK_TEXT_WINDOW_PRIVATE;
7862     }
7863 }
7864
7865 static void
7866 buffer_to_widget (GtkTextView      *text_view,
7867                   gint              buffer_x,
7868                   gint              buffer_y,
7869                   gint             *window_x,
7870                   gint             *window_y)
7871 {  
7872   if (window_x)
7873     {
7874       *window_x = buffer_x - text_view->xoffset;
7875       *window_x += text_view->text_window->allocation.x;
7876     }
7877
7878   if (window_y)
7879     {
7880       *window_y = buffer_y - text_view->yoffset;
7881       *window_y += text_view->text_window->allocation.y;
7882     }
7883 }
7884
7885 static void
7886 widget_to_text_window (GtkTextWindow *win,
7887                        gint           widget_x,
7888                        gint           widget_y,
7889                        gint          *window_x,
7890                        gint          *window_y)
7891 {
7892   if (window_x)
7893     *window_x = widget_x - win->allocation.x;
7894
7895   if (window_y)
7896     *window_y = widget_y - win->allocation.y;
7897 }
7898
7899 static void
7900 buffer_to_text_window (GtkTextView   *text_view,
7901                        GtkTextWindow *win,
7902                        gint           buffer_x,
7903                        gint           buffer_y,
7904                        gint          *window_x,
7905                        gint          *window_y)
7906 {
7907   if (win == NULL)
7908     {
7909       g_warning ("Attempt to convert text buffer coordinates to coordinates "
7910                  "for a nonexistent or private child window of GtkTextView");
7911       return;
7912     }
7913
7914   buffer_to_widget (text_view,
7915                     buffer_x, buffer_y,
7916                     window_x, window_y);
7917
7918   widget_to_text_window (win,
7919                          window_x ? *window_x : 0,
7920                          window_y ? *window_y : 0,
7921                          window_x,
7922                          window_y);
7923 }
7924
7925 /**
7926  * gtk_text_view_buffer_to_window_coords:
7927  * @text_view: a #GtkTextView
7928  * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7929  * @buffer_x: buffer x coordinate
7930  * @buffer_y: buffer y coordinate
7931  * @window_x: window x coordinate return location
7932  * @window_y: window y coordinate return location
7933  *
7934  * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
7935  * @win, and stores the result in (@window_x, @window_y). 
7936  *
7937  * Note that you can't convert coordinates for a nonexisting window (see 
7938  * gtk_text_view_set_border_window_size()).
7939  **/
7940 void
7941 gtk_text_view_buffer_to_window_coords (GtkTextView      *text_view,
7942                                        GtkTextWindowType win,
7943                                        gint              buffer_x,
7944                                        gint              buffer_y,
7945                                        gint             *window_x,
7946                                        gint             *window_y)
7947 {
7948   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7949
7950   switch (win)
7951     {
7952     case GTK_TEXT_WINDOW_WIDGET:
7953       buffer_to_widget (text_view,
7954                         buffer_x, buffer_y,
7955                         window_x, window_y);
7956       break;
7957
7958     case GTK_TEXT_WINDOW_TEXT:
7959       if (window_x)
7960         *window_x = buffer_x - text_view->xoffset;
7961       if (window_y)
7962         *window_y = buffer_y - text_view->yoffset;
7963       break;
7964
7965     case GTK_TEXT_WINDOW_LEFT:
7966       buffer_to_text_window (text_view,
7967                              text_view->left_window,
7968                              buffer_x, buffer_y,
7969                              window_x, window_y);
7970       break;
7971
7972     case GTK_TEXT_WINDOW_RIGHT:
7973       buffer_to_text_window (text_view,
7974                              text_view->right_window,
7975                              buffer_x, buffer_y,
7976                              window_x, window_y);
7977       break;
7978
7979     case GTK_TEXT_WINDOW_TOP:
7980       buffer_to_text_window (text_view,
7981                              text_view->top_window,
7982                              buffer_x, buffer_y,
7983                              window_x, window_y);
7984       break;
7985
7986     case GTK_TEXT_WINDOW_BOTTOM:
7987       buffer_to_text_window (text_view,
7988                              text_view->bottom_window,
7989                              buffer_x, buffer_y,
7990                              window_x, window_y);
7991       break;
7992
7993     case GTK_TEXT_WINDOW_PRIVATE:
7994       g_warning ("%s: can't get coords for private windows", G_STRLOC);
7995       break;
7996
7997     default:
7998       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7999       break;
8000     }
8001 }
8002
8003 static void
8004 widget_to_buffer (GtkTextView *text_view,
8005                   gint         widget_x,
8006                   gint         widget_y,
8007                   gint        *buffer_x,
8008                   gint        *buffer_y)
8009 {  
8010   if (buffer_x)
8011     {
8012       *buffer_x = widget_x + text_view->xoffset;
8013       *buffer_x -= text_view->text_window->allocation.x;
8014     }
8015
8016   if (buffer_y)
8017     {
8018       *buffer_y = widget_y + text_view->yoffset;
8019       *buffer_y -= text_view->text_window->allocation.y;
8020     }
8021 }
8022
8023 static void
8024 text_window_to_widget (GtkTextWindow *win,
8025                        gint           window_x,
8026                        gint           window_y,
8027                        gint          *widget_x,
8028                        gint          *widget_y)
8029 {
8030   if (widget_x)
8031     *widget_x = window_x + win->allocation.x;
8032
8033   if (widget_y)
8034     *widget_y = window_y + win->allocation.y;
8035 }
8036
8037 static void
8038 text_window_to_buffer (GtkTextView   *text_view,
8039                        GtkTextWindow *win,
8040                        gint           window_x,
8041                        gint           window_y,
8042                        gint          *buffer_x,
8043                        gint          *buffer_y)
8044 {
8045   if (win == NULL)
8046     {
8047       g_warning ("Attempt to convert GtkTextView buffer coordinates into "
8048                  "coordinates for a nonexistent child window.");
8049       return;
8050     }
8051
8052   text_window_to_widget (win,
8053                          window_x,
8054                          window_y,
8055                          buffer_x,
8056                          buffer_y);
8057
8058   widget_to_buffer (text_view,
8059                     buffer_x ? *buffer_x : 0,
8060                     buffer_y ? *buffer_y : 0,
8061                     buffer_x,
8062                     buffer_y);
8063 }
8064
8065 /**
8066  * gtk_text_view_window_to_buffer_coords:
8067  * @text_view: a #GtkTextView
8068  * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
8069  * @window_x: window x coordinate
8070  * @window_y: window y coordinate
8071  * @buffer_x: buffer x coordinate return location
8072  * @buffer_y: buffer y coordinate return location
8073  *
8074  * Converts coordinates on the window identified by @win to buffer
8075  * coordinates, storing the result in (@buffer_x,@buffer_y).
8076  *
8077  * Note that you can't convert coordinates for a nonexisting window (see 
8078  * gtk_text_view_set_border_window_size()).
8079  **/
8080 void
8081 gtk_text_view_window_to_buffer_coords (GtkTextView      *text_view,
8082                                        GtkTextWindowType win,
8083                                        gint              window_x,
8084                                        gint              window_y,
8085                                        gint             *buffer_x,
8086                                        gint             *buffer_y)
8087 {
8088   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8089
8090   switch (win)
8091     {
8092     case GTK_TEXT_WINDOW_WIDGET:
8093       widget_to_buffer (text_view,
8094                         window_x, window_y,
8095                         buffer_x, buffer_y);
8096       break;
8097
8098     case GTK_TEXT_WINDOW_TEXT:
8099       if (buffer_x)
8100         *buffer_x = window_x + text_view->xoffset;
8101       if (buffer_y)
8102         *buffer_y = window_y + text_view->yoffset;
8103       break;
8104
8105     case GTK_TEXT_WINDOW_LEFT:
8106       text_window_to_buffer (text_view,
8107                              text_view->left_window,
8108                              window_x, window_y,
8109                              buffer_x, buffer_y);
8110       break;
8111
8112     case GTK_TEXT_WINDOW_RIGHT:
8113       text_window_to_buffer (text_view,
8114                              text_view->right_window,
8115                              window_x, window_y,
8116                              buffer_x, buffer_y);
8117       break;
8118
8119     case GTK_TEXT_WINDOW_TOP:
8120       text_window_to_buffer (text_view,
8121                              text_view->top_window,
8122                              window_x, window_y,
8123                              buffer_x, buffer_y);
8124       break;
8125
8126     case GTK_TEXT_WINDOW_BOTTOM:
8127       text_window_to_buffer (text_view,
8128                              text_view->bottom_window,
8129                              window_x, window_y,
8130                              buffer_x, buffer_y);
8131       break;
8132
8133     case GTK_TEXT_WINDOW_PRIVATE:
8134       g_warning ("%s: can't get coords for private windows", G_STRLOC);
8135       break;
8136
8137     default:
8138       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
8139       break;
8140     }
8141 }
8142
8143 static void
8144 set_window_width (GtkTextView      *text_view,
8145                   gint              width,
8146                   GtkTextWindowType type,
8147                   GtkTextWindow   **winp)
8148 {
8149   if (width == 0)
8150     {
8151       if (*winp)
8152         {
8153           text_window_free (*winp);
8154           *winp = NULL;
8155           gtk_widget_queue_resize (GTK_WIDGET (text_view));
8156         }
8157     }
8158   else
8159     {
8160       if (*winp == NULL)
8161         {
8162           *winp = text_window_new (type,
8163                                    GTK_WIDGET (text_view),
8164                                    width, 0);
8165           /* if the widget is already realized we need to realize the child manually */
8166           if (GTK_WIDGET_REALIZED (text_view))
8167             text_window_realize (*winp, GTK_WIDGET (text_view)->window);
8168         }
8169       else
8170         {
8171           if ((*winp)->requisition.width == width)
8172             return;
8173
8174           (*winp)->requisition.width = width;
8175         }
8176
8177       gtk_widget_queue_resize (GTK_WIDGET (text_view));
8178     }
8179 }
8180
8181
8182 static void
8183 set_window_height (GtkTextView      *text_view,
8184                    gint              height,
8185                    GtkTextWindowType type,
8186                    GtkTextWindow   **winp)
8187 {
8188   if (height == 0)
8189     {
8190       if (*winp)
8191         {
8192           text_window_free (*winp);
8193           *winp = NULL;
8194           gtk_widget_queue_resize (GTK_WIDGET (text_view));
8195         }
8196     }
8197   else
8198     {
8199       if (*winp == NULL)
8200         {
8201           *winp = text_window_new (type,
8202                                    GTK_WIDGET (text_view),
8203                                    0, height);
8204
8205           /* if the widget is already realized we need to realize the child manually */
8206           if (GTK_WIDGET_REALIZED (text_view))
8207             text_window_realize (*winp, GTK_WIDGET (text_view)->window);
8208         }
8209       else
8210         {
8211           if ((*winp)->requisition.height == height)
8212             return;
8213
8214           (*winp)->requisition.height = height;
8215         }
8216
8217       gtk_widget_queue_resize (GTK_WIDGET (text_view));
8218     }
8219 }
8220
8221 /**
8222  * gtk_text_view_set_border_window_size:
8223  * @text_view: a #GtkTextView
8224  * @type: window to affect
8225  * @size: width or height of the window
8226  *
8227  * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
8228  * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
8229  * Automatically destroys the corresponding window if the size is set
8230  * to 0, and creates the window if the size is set to non-zero.  This
8231  * function can only be used for the "border windows," it doesn't work
8232  * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
8233  * #GTK_TEXT_WINDOW_PRIVATE.
8234  **/
8235 void
8236 gtk_text_view_set_border_window_size (GtkTextView      *text_view,
8237                                       GtkTextWindowType type,
8238                                       gint              size)
8239
8240 {
8241   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8242   g_return_if_fail (size >= 0);
8243
8244   switch (type)
8245     {
8246     case GTK_TEXT_WINDOW_LEFT:
8247       set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
8248                         &text_view->left_window);
8249       break;
8250
8251     case GTK_TEXT_WINDOW_RIGHT:
8252       set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
8253                         &text_view->right_window);
8254       break;
8255
8256     case GTK_TEXT_WINDOW_TOP:
8257       set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
8258                          &text_view->top_window);
8259       break;
8260
8261     case GTK_TEXT_WINDOW_BOTTOM:
8262       set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
8263                          &text_view->bottom_window);
8264       break;
8265
8266     default:
8267       g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
8268       break;
8269     }
8270 }
8271
8272 /**
8273  * gtk_text_view_get_border_window_size:
8274  * @text_view: a #GtkTextView
8275  * @type: window to return size from
8276  *
8277  * Gets the width of the specified border window. See
8278  * gtk_text_view_set_border_window_size().
8279  *
8280  * Return value: width of window
8281  **/
8282 gint
8283 gtk_text_view_get_border_window_size (GtkTextView       *text_view,
8284                                       GtkTextWindowType  type)
8285 {
8286   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
8287   
8288   switch (type)
8289     {
8290     case GTK_TEXT_WINDOW_LEFT:
8291       if (text_view->left_window)
8292         return text_view->left_window->requisition.width;
8293       break;
8294       
8295     case GTK_TEXT_WINDOW_RIGHT:
8296       if (text_view->right_window)
8297         return text_view->right_window->requisition.width;
8298       break;
8299       
8300     case GTK_TEXT_WINDOW_TOP:
8301       if (text_view->top_window)
8302         return text_view->top_window->requisition.height;
8303       break;
8304
8305     case GTK_TEXT_WINDOW_BOTTOM:
8306       if (text_view->bottom_window)
8307         return text_view->bottom_window->requisition.height;
8308       break;
8309       
8310     default:
8311       g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
8312       break;
8313     }
8314
8315   return 0;
8316 }
8317
8318 /*
8319  * Child widgets
8320  */
8321
8322 static GtkTextViewChild*
8323 text_view_child_new_anchored (GtkWidget          *child,
8324                               GtkTextChildAnchor *anchor,
8325                               GtkTextLayout      *layout)
8326 {
8327   GtkTextViewChild *vc;
8328
8329   vc = g_new (GtkTextViewChild, 1);
8330
8331   vc->type = GTK_TEXT_WINDOW_PRIVATE;
8332   vc->widget = child;
8333   vc->anchor = anchor;
8334
8335   vc->from_top_of_line = 0;
8336   vc->from_left_of_buffer = 0;
8337   
8338   g_object_ref (vc->widget);
8339   g_object_ref (vc->anchor);
8340
8341   g_object_set_data (G_OBJECT (child),
8342                      I_("gtk-text-view-child"),
8343                      vc);
8344
8345   gtk_text_child_anchor_register_child (anchor, child, layout);
8346   
8347   return vc;
8348 }
8349
8350 static GtkTextViewChild*
8351 text_view_child_new_window (GtkWidget          *child,
8352                             GtkTextWindowType   type,
8353                             gint                x,
8354                             gint                y)
8355 {
8356   GtkTextViewChild *vc;
8357
8358   vc = g_new (GtkTextViewChild, 1);
8359
8360   vc->widget = child;
8361   vc->anchor = NULL;
8362
8363   vc->from_top_of_line = 0;
8364   vc->from_left_of_buffer = 0;
8365  
8366   g_object_ref (vc->widget);
8367
8368   vc->type = type;
8369   vc->x = x;
8370   vc->y = y;
8371
8372   g_object_set_data (G_OBJECT (child),
8373                      I_("gtk-text-view-child"),
8374                      vc);
8375   
8376   return vc;
8377 }
8378
8379 static void
8380 text_view_child_free (GtkTextViewChild *child)
8381 {
8382   g_object_set_data (G_OBJECT (child->widget),
8383                      I_("gtk-text-view-child"), NULL);
8384
8385   if (child->anchor)
8386     {
8387       gtk_text_child_anchor_unregister_child (child->anchor,
8388                                               child->widget);
8389       g_object_unref (child->anchor);
8390     }
8391
8392   g_object_unref (child->widget);
8393
8394   g_free (child);
8395 }
8396
8397 static void
8398 text_view_child_set_parent_window (GtkTextView      *text_view,
8399                                    GtkTextViewChild *vc)
8400 {
8401   if (vc->anchor)
8402     gtk_widget_set_parent_window (vc->widget,
8403                                   text_view->text_window->bin_window);
8404   else
8405     {
8406       GdkWindow *window;
8407       window = gtk_text_view_get_window (text_view,
8408                                          vc->type);
8409       gtk_widget_set_parent_window (vc->widget, window);
8410     }
8411 }
8412
8413 static void
8414 add_child (GtkTextView      *text_view,
8415            GtkTextViewChild *vc)
8416 {
8417   text_view->children = g_slist_prepend (text_view->children,
8418                                          vc);
8419
8420   if (GTK_WIDGET_REALIZED (text_view))
8421     text_view_child_set_parent_window (text_view, vc);
8422   
8423   gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
8424 }
8425
8426 /**
8427  * gtk_text_view_add_child_at_anchor:
8428  * @text_view: a #GtkTextView
8429  * @child: a #GtkWidget
8430  * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
8431  * 
8432  * Adds a child widget in the text buffer, at the given @anchor.
8433  * 
8434  **/
8435 void
8436 gtk_text_view_add_child_at_anchor (GtkTextView          *text_view,
8437                                    GtkWidget            *child,
8438                                    GtkTextChildAnchor   *anchor)
8439 {
8440   GtkTextViewChild *vc;
8441
8442   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8443   g_return_if_fail (GTK_IS_WIDGET (child));
8444   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
8445   g_return_if_fail (child->parent == NULL);
8446
8447   gtk_text_view_ensure_layout (text_view);
8448
8449   vc = text_view_child_new_anchored (child, anchor,
8450                                      text_view->layout);
8451
8452   add_child (text_view, vc);
8453
8454   g_assert (vc->widget == child);
8455   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8456 }
8457
8458 /**
8459  * gtk_text_view_add_child_in_window:
8460  * @text_view: a #GtkTextView
8461  * @child: a #GtkWidget
8462  * @which_window: which window the child should appear in
8463  * @xpos: X position of child in window coordinates
8464  * @ypos: Y position of child in window coordinates
8465  *
8466  * Adds a child at fixed coordinates in one of the text widget's
8467  * windows.  The window must have nonzero size (see
8468  * gtk_text_view_set_border_window_size()).  Note that the child
8469  * coordinates are given relative to the #GdkWindow in question, and
8470  * that these coordinates have no sane relationship to scrolling. When
8471  * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
8472  * irrelevant, the child floats above all scrollable areas. But when
8473  * placing a child in one of the scrollable windows (border windows or
8474  * text window), you'll need to compute the child's correct position
8475  * in buffer coordinates any time scrolling occurs or buffer changes
8476  * occur, and then call gtk_text_view_move_child() to update the
8477  * child's position. Unfortunately there's no good way to detect that
8478  * scrolling has occurred, using the current API; a possible hack
8479  * would be to update all child positions when the scroll adjustments
8480  * change or the text buffer changes. See bug 64518 on
8481  * bugzilla.gnome.org for status of fixing this issue.
8482  *
8483  **/
8484 void
8485 gtk_text_view_add_child_in_window (GtkTextView          *text_view,
8486                                    GtkWidget            *child,
8487                                    GtkTextWindowType     which_window,
8488                                    gint                  xpos,
8489                                    gint                  ypos)
8490 {
8491   GtkTextViewChild *vc;
8492
8493   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8494   g_return_if_fail (GTK_IS_WIDGET (child));
8495   g_return_if_fail (child->parent == NULL);
8496
8497   vc = text_view_child_new_window (child, which_window,
8498                                    xpos, ypos);
8499
8500   add_child (text_view, vc);
8501
8502   g_assert (vc->widget == child);
8503   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
8504 }
8505
8506 /**
8507  * gtk_text_view_move_child:
8508  * @text_view: a #GtkTextView
8509  * @child: child widget already added to the text view
8510  * @xpos: new X position in window coordinates
8511  * @ypos: new Y position in window coordinates
8512  *
8513  * Updates the position of a child, as for gtk_text_view_add_child_in_window().
8514  * 
8515  **/
8516 void
8517 gtk_text_view_move_child          (GtkTextView          *text_view,
8518                                    GtkWidget            *child,
8519                                    gint                  xpos,
8520                                    gint                  ypos)
8521 {
8522   GtkTextViewChild *vc;
8523
8524   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
8525   g_return_if_fail (GTK_IS_WIDGET (child));
8526   g_return_if_fail (child->parent == (GtkWidget*) text_view);
8527
8528   vc = g_object_get_data (G_OBJECT (child),
8529                           "gtk-text-view-child");
8530
8531   g_assert (vc != NULL);
8532
8533   if (vc->x == xpos &&
8534       vc->y == ypos)
8535     return;
8536   
8537   vc->x = xpos;
8538   vc->y = ypos;
8539
8540   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
8541     gtk_widget_queue_resize (child);
8542 }
8543
8544
8545 /* Iterator operations */
8546
8547 /**
8548  * gtk_text_view_forward_display_line:
8549  * @text_view: a #GtkTextView
8550  * @iter: a #GtkTextIter
8551  * 
8552  * Moves the given @iter forward by one display (wrapped) line.  A
8553  * display line is different from a paragraph. Paragraphs are
8554  * separated by newlines or other paragraph separator characters.
8555  * Display lines are created by line-wrapping a paragraph.  If
8556  * wrapping is turned off, display lines and paragraphs will be the
8557  * same. Display lines are divided differently for each view, since
8558  * they depend on the view's width; paragraphs are the same in all
8559  * views, since they depend on the contents of the #GtkTextBuffer.
8560  * 
8561  * Return value: %TRUE if @iter was moved and is not on the end iterator
8562  **/
8563 gboolean
8564 gtk_text_view_forward_display_line (GtkTextView *text_view,
8565                                     GtkTextIter *iter)
8566 {
8567   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8568   g_return_val_if_fail (iter != NULL, FALSE);
8569
8570   gtk_text_view_ensure_layout (text_view);
8571
8572   return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
8573 }
8574
8575 /**
8576  * gtk_text_view_backward_display_line:
8577  * @text_view: a #GtkTextView
8578  * @iter: a #GtkTextIter
8579  * 
8580  * Moves the given @iter backward by one display (wrapped) line.  A
8581  * display line is different from a paragraph. Paragraphs are
8582  * separated by newlines or other paragraph separator characters.
8583  * Display lines are created by line-wrapping a paragraph.  If
8584  * wrapping is turned off, display lines and paragraphs will be the
8585  * same. Display lines are divided differently for each view, since
8586  * they depend on the view's width; paragraphs are the same in all
8587  * views, since they depend on the contents of the #GtkTextBuffer.
8588  * 
8589  * Return value: %TRUE if @iter was moved and is not on the end iterator
8590  **/
8591 gboolean
8592 gtk_text_view_backward_display_line (GtkTextView *text_view,
8593                                      GtkTextIter *iter)
8594 {
8595   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8596   g_return_val_if_fail (iter != NULL, FALSE);
8597
8598   gtk_text_view_ensure_layout (text_view);
8599
8600   return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
8601 }
8602
8603 /**
8604  * gtk_text_view_forward_display_line_end:
8605  * @text_view: a #GtkTextView
8606  * @iter: a #GtkTextIter
8607  * 
8608  * Moves the given @iter forward to the next display line end.  A
8609  * display line is different from a paragraph. Paragraphs are
8610  * separated by newlines or other paragraph separator characters.
8611  * Display lines are created by line-wrapping a paragraph.  If
8612  * wrapping is turned off, display lines and paragraphs will be the
8613  * same. Display lines are divided differently for each view, since
8614  * they depend on the view's width; paragraphs are the same in all
8615  * views, since they depend on the contents of the #GtkTextBuffer.
8616  * 
8617  * Return value: %TRUE if @iter was moved and is not on the end iterator
8618  **/
8619 gboolean
8620 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
8621                                         GtkTextIter *iter)
8622 {
8623   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8624   g_return_val_if_fail (iter != NULL, FALSE);
8625
8626   gtk_text_view_ensure_layout (text_view);
8627
8628   return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
8629 }
8630
8631 /**
8632  * gtk_text_view_backward_display_line_start:
8633  * @text_view: a #GtkTextView
8634  * @iter: a #GtkTextIter
8635  * 
8636  * Moves the given @iter backward to the next display line start.  A
8637  * display line is different from a paragraph. Paragraphs are
8638  * separated by newlines or other paragraph separator characters.
8639  * Display lines are created by line-wrapping a paragraph.  If
8640  * wrapping is turned off, display lines and paragraphs will be the
8641  * same. Display lines are divided differently for each view, since
8642  * they depend on the view's width; paragraphs are the same in all
8643  * views, since they depend on the contents of the #GtkTextBuffer.
8644  * 
8645  * Return value: %TRUE if @iter was moved and is not on the end iterator
8646  **/
8647 gboolean
8648 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
8649                                            GtkTextIter *iter)
8650 {
8651   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8652   g_return_val_if_fail (iter != NULL, FALSE);
8653
8654   gtk_text_view_ensure_layout (text_view);
8655
8656   return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
8657 }
8658
8659 /**
8660  * gtk_text_view_starts_display_line:
8661  * @text_view: a #GtkTextView
8662  * @iter: a #GtkTextIter
8663  * 
8664  * Determines whether @iter is at the start of a display line.
8665  * See gtk_text_view_forward_display_line() for an explanation of
8666  * display lines vs. paragraphs.
8667  * 
8668  * Return value: %TRUE if @iter begins a wrapped line
8669  **/
8670 gboolean
8671 gtk_text_view_starts_display_line (GtkTextView       *text_view,
8672                                    const GtkTextIter *iter)
8673 {
8674   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8675   g_return_val_if_fail (iter != NULL, FALSE);
8676
8677   gtk_text_view_ensure_layout (text_view);
8678
8679   return gtk_text_layout_iter_starts_line (text_view->layout, iter);
8680 }
8681
8682 /**
8683  * gtk_text_view_move_visually:
8684  * @text_view: a #GtkTextView
8685  * @iter: a #GtkTextIter
8686  * @count:   number of characters to move (negative moves left, positive moves right)
8687  *
8688  * Move the iterator a given number of characters visually, treating
8689  * it as the strong cursor position. If @count is positive, then the
8690  * new strong cursor position will be @count positions to the right of
8691  * the old cursor position. If @count is negative then the new strong
8692  * cursor position will be @count positions to the left of the old
8693  * cursor position.
8694  *
8695  * In the presence of bidirection text, the correspondence
8696  * between logical and visual order will depend on the direction
8697  * of the current run, and there may be jumps when the cursor
8698  * is moved off of the end of a run.
8699  * 
8700  * Return value: %TRUE if @iter moved and is not on the end iterator
8701  **/
8702 gboolean
8703 gtk_text_view_move_visually (GtkTextView *text_view,
8704                              GtkTextIter *iter,
8705                              gint         count)
8706 {
8707   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
8708   g_return_val_if_fail (iter != NULL, FALSE);
8709
8710   gtk_text_view_ensure_layout (text_view);
8711
8712   return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
8713 }
8714
8715 #define __GTK_TEXT_VIEW_C__
8716 #include "gtkaliasdef.c"