]> Pileus Git - ~andy/gtk/blob - gtk/gtktextview.c
Hmm, setter functions normally assign the new value to the object field.
[~andy/gtk] / gtk / gtktextview.c
1 /* GTK - The GIMP Toolkit
2  * gtktextview.c Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include <string.h>
28
29 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
30 #include "gtkbindings.h"
31 #include "gtkdnd.h"
32 #include "gtkimagemenuitem.h"
33 #include "gtkintl.h"
34 #include "gtkmain.h"
35 #include "gtkmarshalers.h"
36 #include "gtkmenu.h"
37 #include "gtkmenuitem.h"
38 #include "gtkseparatormenuitem.h"
39 #include "gtksettings.h"
40 #include "gtksignal.h"
41 #include "gtkstock.h"
42 #include "gtktextdisplay.h"
43 #include "gtktextview.h"
44 #include "gtkimmulticontext.h"
45 #include "gdk/gdkkeysyms.h"
46 #include <string.h>
47
48 /* How scrolling, validation, exposes, etc. work.
49  *
50  * The expose_event handler has the invariant that the onscreen lines
51  * have been validated.
52  *
53  * There are two ways that onscreen lines can become invalid. The first
54  * is to change which lines are onscreen. This happens when the value
55  * of a scroll adjustment changes. So the code path begins in
56  * gtk_text_view_value_changed() and goes like this:
57  *   - gdk_window_scroll() to reflect the new adjustment value
58  *   - validate the lines that were moved onscreen
59  *   - gdk_window_process_updates() to handle the exposes immediately
60  *
61  * The second way is that you get the "invalidated" signal from the layout,
62  * indicating that lines have become invalid. This code path begins in
63  * invalidated_handler() and goes like this:
64  *   - install high-priority idle which does the rest of the steps
65  *   - if a scroll is pending from scroll_to_mark(), do the scroll,
66  *     jumping to the gtk_text_view_value_changed() code path
67  *   - otherwise, validate the onscreen lines
68  *   - DO NOT process updates
69  *
70  * In both cases, validating the onscreen lines can trigger a scroll
71  * due to maintaining the first_para on the top of the screen.
72  * If validation triggers a scroll, we jump to the top of the code path
73  * for value_changed, and bail out of the current code path.
74  *
75  * Also, in size_allocate, if we invalidate some lines from changing
76  * the layout width, we need to go ahead and run the high-priority idle,
77  * because GTK sends exposes right after doing the size allocates without
78  * returning to the main loop. This is also why the high-priority idle
79  * is at a higher priority than resizing.
80  *
81  */
82
83 #define SCREEN_WIDTH(widget) text_window_get_width (GTK_TEXT_VIEW (widget)->text_window)
84 #define SCREEN_HEIGHT(widget) text_window_get_height (GTK_TEXT_VIEW (widget)->text_window)
85
86 #if 0
87 #define DEBUG_VALIDATION_AND_SCROLLING
88 #endif
89
90 #ifdef DEBUG_VALIDATION_AND_SCROLLING
91 #define DV(x) (x)
92 #else
93 #define DV(x)
94 #endif
95
96 struct _GtkTextPendingScroll
97 {
98   GtkTextMark   *mark;
99   gdouble        within_margin;
100   gboolean       use_align;
101   gdouble        xalign;
102   gdouble        yalign;
103 };
104   
105 enum
106 {
107   SET_SCROLL_ADJUSTMENTS,
108   POPULATE_POPUP,
109   MOVE_CURSOR,
110   SET_ANCHOR,
111   INSERT_AT_CURSOR,
112   DELETE_FROM_CURSOR,
113   CUT_CLIPBOARD,
114   COPY_CLIPBOARD,
115   PASTE_CLIPBOARD,
116   TOGGLE_OVERWRITE,
117   LAST_SIGNAL
118 };
119
120 enum
121 {
122   PROP_0,
123   PROP_PIXELS_ABOVE_LINES,
124   PROP_PIXELS_BELOW_LINES,
125   PROP_PIXELS_INSIDE_WRAP,
126   PROP_EDITABLE,
127   PROP_WRAP_MODE,
128   PROP_JUSTIFICATION,
129   PROP_LEFT_MARGIN,
130   PROP_RIGHT_MARGIN,
131   PROP_INDENT,
132   PROP_TABS,
133   PROP_CURSOR_VISIBLE,
134   LAST_PROP
135 };
136
137 static void gtk_text_view_init                 (GtkTextView      *text_view);
138 static void gtk_text_view_class_init           (GtkTextViewClass *klass);
139 static void gtk_text_view_destroy              (GtkObject        *object);
140 static void gtk_text_view_finalize             (GObject          *object);
141 static void gtk_text_view_set_property         (GObject         *object,
142                                                 guint            prop_id,
143                                                 const GValue    *value,
144                                                 GParamSpec      *pspec);
145 static void gtk_text_view_get_property         (GObject         *object,
146                                                 guint            prop_id,
147                                                 GValue          *value,
148                                                 GParamSpec      *pspec);
149 static void gtk_text_view_size_request         (GtkWidget        *widget,
150                                                 GtkRequisition   *requisition);
151 static void gtk_text_view_size_allocate        (GtkWidget        *widget,
152                                                 GtkAllocation    *allocation);
153 static void gtk_text_view_realize              (GtkWidget        *widget);
154 static void gtk_text_view_unrealize            (GtkWidget        *widget);
155 static void gtk_text_view_style_set            (GtkWidget        *widget,
156                                                 GtkStyle         *previous_style);
157 static void gtk_text_view_direction_changed    (GtkWidget        *widget,
158                                                 GtkTextDirection  previous_direction);
159 static gint gtk_text_view_event                (GtkWidget        *widget,
160                                                 GdkEvent         *event);
161 static gint gtk_text_view_key_press_event      (GtkWidget        *widget,
162                                                 GdkEventKey      *event);
163 static gint gtk_text_view_key_release_event    (GtkWidget        *widget,
164                                                 GdkEventKey      *event);
165 static gint gtk_text_view_button_press_event   (GtkWidget        *widget,
166                                                 GdkEventButton   *event);
167 static gint gtk_text_view_button_release_event (GtkWidget        *widget,
168                                                 GdkEventButton   *event);
169 static gint gtk_text_view_focus_in_event       (GtkWidget        *widget,
170                                                 GdkEventFocus    *event);
171 static gint gtk_text_view_focus_out_event      (GtkWidget        *widget,
172                                                 GdkEventFocus    *event);
173 static gint gtk_text_view_motion_event         (GtkWidget        *widget,
174                                                 GdkEventMotion   *event);
175 static gint gtk_text_view_expose_event         (GtkWidget        *widget,
176                                                 GdkEventExpose   *expose);
177 static void gtk_text_view_draw_focus           (GtkWidget        *widget);
178 static void gtk_text_view_grab_focus           (GtkWidget        *widget);
179
180 /* Source side drag signals */
181 static void gtk_text_view_drag_begin       (GtkWidget        *widget,
182                                             GdkDragContext   *context);
183 static void gtk_text_view_drag_end         (GtkWidget        *widget,
184                                             GdkDragContext   *context);
185 static void gtk_text_view_drag_data_get    (GtkWidget        *widget,
186                                             GdkDragContext   *context,
187                                             GtkSelectionData *selection_data,
188                                             guint             info,
189                                             guint             time);
190 static void gtk_text_view_drag_data_delete (GtkWidget        *widget,
191                                             GdkDragContext   *context);
192
193 /* Target side drag signals */
194 static void     gtk_text_view_drag_leave         (GtkWidget        *widget,
195                                                   GdkDragContext   *context,
196                                                   guint             time);
197 static gboolean gtk_text_view_drag_motion        (GtkWidget        *widget,
198                                                   GdkDragContext   *context,
199                                                   gint              x,
200                                                   gint              y,
201                                                   guint             time);
202 static gboolean gtk_text_view_drag_drop          (GtkWidget        *widget,
203                                                   GdkDragContext   *context,
204                                                   gint              x,
205                                                   gint              y,
206                                                   guint             time);
207 static void     gtk_text_view_drag_data_received (GtkWidget        *widget,
208                                                   GdkDragContext   *context,
209                                                   gint              x,
210                                                   gint              y,
211                                                   GtkSelectionData *selection_data,
212                                                   guint             info,
213                                                   guint             time);
214
215 static void gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
216                                                   GtkAdjustment *hadj,
217                                                   GtkAdjustment *vadj);
218 static void gtk_text_view_popup_menu             (GtkWidget     *widget);
219
220 static void gtk_text_view_move_cursor      (GtkTextView           *text_view,
221                                             GtkMovementStep        step,
222                                             gint                   count,
223                                             gboolean               extend_selection);
224 static void gtk_text_view_set_anchor       (GtkTextView           *text_view);
225 static void gtk_text_view_scroll_pages     (GtkTextView           *text_view,
226                                             gint                   count);
227 static void gtk_text_view_insert_at_cursor (GtkTextView           *text_view,
228                                             const gchar           *str);
229 static void gtk_text_view_delete_from_cursor (GtkTextView           *text_view,
230                                               GtkDeleteType          type,
231                                               gint                   count);
232 static void gtk_text_view_cut_clipboard    (GtkTextView           *text_view);
233 static void gtk_text_view_copy_clipboard   (GtkTextView           *text_view);
234 static void gtk_text_view_paste_clipboard  (GtkTextView           *text_view);
235 static void gtk_text_view_toggle_overwrite (GtkTextView           *text_view);
236 static void gtk_text_view_unselect         (GtkTextView           *text_view);
237
238 static void     gtk_text_view_validate_onscreen     (GtkTextView        *text_view);
239 static void     gtk_text_view_get_first_para_iter   (GtkTextView        *text_view,
240                                                      GtkTextIter        *iter);
241 static void     gtk_text_view_update_layout_width       (GtkTextView        *text_view);
242 static void     gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
243                                                          GtkTextAttributes *values,
244                                                          GtkStyle           *style);
245 static void     gtk_text_view_ensure_layout          (GtkTextView        *text_view);
246 static void     gtk_text_view_destroy_layout         (GtkTextView        *text_view);
247 static void     gtk_text_view_check_keymap_direction (GtkTextView        *text_view);
248 static void     gtk_text_view_reset_im_context       (GtkTextView        *text_view);
249 static void     gtk_text_view_start_selection_drag   (GtkTextView        *text_view,
250                                                       const GtkTextIter  *iter,
251                                                       GdkEventButton     *event);
252 static gboolean gtk_text_view_end_selection_drag     (GtkTextView        *text_view,
253                                                       GdkEventButton     *event);
254 static void     gtk_text_view_start_selection_dnd    (GtkTextView        *text_view,
255                                                       const GtkTextIter  *iter,
256                                                       GdkEventMotion     *event);
257 static void     gtk_text_view_check_cursor_blink     (GtkTextView        *text_view);
258 static void     gtk_text_view_pend_cursor_blink      (GtkTextView        *text_view);
259 static void     gtk_text_view_stop_cursor_blink      (GtkTextView        *text_view);
260
261 static void     gtk_text_view_value_changed                (GtkAdjustment *adj,
262                                                             GtkTextView   *view);
263 static void     gtk_text_view_commit_handler               (GtkIMContext  *context,
264                                                             const gchar   *str,
265                                                             GtkTextView   *text_view);
266 static void     gtk_text_view_commit_text                  (GtkTextView   *text_view,
267                                                             const gchar   *text);
268 static void     gtk_text_view_preedit_changed_handler      (GtkIMContext  *context,
269                                                             GtkTextView   *text_view);
270 static gboolean gtk_text_view_retrieve_surrounding_handler (GtkIMContext  *context,
271                                                             GtkTextView   *text_view);
272 static gboolean gtk_text_view_delete_surrounding_handler   (GtkIMContext  *context,
273                                                             gint           offset,
274                                                             gint           n_chars,
275                                                             GtkTextView   *text_view);
276
277 static void gtk_text_view_mark_set_handler       (GtkTextBuffer     *buffer,
278                                                   const GtkTextIter *location,
279                                                   GtkTextMark       *mark,
280                                                   gpointer           data);
281 static void gtk_text_view_get_virtual_cursor_pos (GtkTextView       *text_view,
282                                                   gint              *x,
283                                                   gint              *y);
284 static void gtk_text_view_set_virtual_cursor_pos (GtkTextView       *text_view,
285                                                   gint               x,
286                                                   gint               y);
287
288 static GtkAdjustment* get_hadjustment            (GtkTextView       *text_view);
289 static GtkAdjustment* get_vadjustment            (GtkTextView       *text_view);
290
291 static void gtk_text_view_do_popup               (GtkTextView       *text_view,
292                                                   GdkEventButton    *event);
293
294 static void gtk_text_view_queue_scroll           (GtkTextView   *text_view,
295                                                   GtkTextMark   *mark,
296                                                   gdouble        within_margin,
297                                                   gboolean       use_align,
298                                                   gdouble        xalign,
299                                                   gdouble        yalign);
300
301 static gboolean gtk_text_view_flush_scroll       (GtkTextView *text_view);
302 static void     gtk_text_view_update_adjustments (GtkTextView *text_view);
303 static void     gtk_text_view_invalidate         (GtkTextView *text_view);
304
305 static void gtk_text_view_update_im_spot_location (GtkTextView *text_view);
306
307 /* Container methods */
308 static void gtk_text_view_add    (GtkContainer *container,
309                                   GtkWidget    *child);
310 static void gtk_text_view_remove (GtkContainer *container,
311                                   GtkWidget    *child);
312 static void gtk_text_view_forall (GtkContainer *container,
313                                   gboolean      include_internals,
314                                   GtkCallback   callback,
315                                   gpointer      callback_data);
316
317 /* FIXME probably need the focus methods. */
318
319 typedef struct _GtkTextViewChild GtkTextViewChild;
320
321 struct _GtkTextViewChild
322 {
323   GtkWidget *widget;
324
325   GtkTextChildAnchor *anchor;
326
327   gint from_top_of_line;
328   gint from_left_of_buffer;
329   
330   /* These are ignored if anchor != NULL */
331   GtkTextWindowType type;
332   gint x;
333   gint y;
334 };
335
336 static GtkTextViewChild* text_view_child_new_anchored      (GtkWidget          *child,
337                                                             GtkTextChildAnchor *anchor,
338                                                             GtkTextLayout      *layout);
339 static GtkTextViewChild* text_view_child_new_window        (GtkWidget          *child,
340                                                             GtkTextWindowType   type,
341                                                             gint                x,
342                                                             gint                y);
343 static void              text_view_child_free              (GtkTextViewChild   *child);
344 static void              text_view_child_set_parent_window (GtkTextView        *text_view,
345                                                             GtkTextViewChild   *child);
346
347 struct _GtkTextWindow
348 {
349   GtkTextWindowType type;
350   GtkWidget *widget;
351   GdkWindow *window;
352   GdkWindow *bin_window;
353   GtkRequisition requisition;
354   GdkRectangle allocation;
355 };
356
357 static GtkTextWindow *text_window_new             (GtkTextWindowType  type,
358                                                    GtkWidget         *widget,
359                                                    gint               width_request,
360                                                    gint               height_request);
361 static void           text_window_free            (GtkTextWindow     *win);
362 static void           text_window_realize         (GtkTextWindow     *win,
363                                                    GdkWindow         *parent);
364 static void           text_window_unrealize       (GtkTextWindow     *win);
365 static void           text_window_size_allocate   (GtkTextWindow     *win,
366                                                    GdkRectangle      *rect);
367 static void           text_window_scroll          (GtkTextWindow     *win,
368                                                    gint               dx,
369                                                    gint               dy);
370 static void           text_window_invalidate_rect (GtkTextWindow     *win,
371                                                    GdkRectangle      *rect);
372
373 static gint           text_window_get_width       (GtkTextWindow     *win);
374 static gint           text_window_get_height      (GtkTextWindow     *win);
375 static void           text_window_get_allocation  (GtkTextWindow     *win,
376                                                    GdkRectangle      *rect);
377
378
379 enum
380 {
381   TARGET_STRING,
382   TARGET_TEXT,
383   TARGET_COMPOUND_TEXT,
384   TARGET_UTF8_STRING,
385   TARGET_TEXT_BUFFER_CONTENTS
386 };
387
388 static GtkTargetEntry target_table[] = {
389   { "GTK_TEXT_BUFFER_CONTENTS", GTK_TARGET_SAME_APP,
390     TARGET_TEXT_BUFFER_CONTENTS },
391   { "UTF8_STRING", 0, TARGET_UTF8_STRING },
392   { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
393   { "TEXT", 0, TARGET_TEXT },
394   { "text/plain", 0, TARGET_STRING },
395   { "STRING",     0, TARGET_STRING }
396 };
397
398 static GtkContainerClass *parent_class = NULL;
399 static guint signals[LAST_SIGNAL] = { 0 };
400
401 GtkType
402 gtk_text_view_get_type (void)
403 {
404   static GtkType our_type = 0;
405
406   if (our_type == 0)
407     {
408       static const GtkTypeInfo our_info =
409       {
410         "GtkTextView",
411         sizeof (GtkTextView),
412         sizeof (GtkTextViewClass),
413         (GtkClassInitFunc) gtk_text_view_class_init,
414         (GtkObjectInitFunc) gtk_text_view_init,
415         /* reserved_1 */ NULL,
416         /* reserved_2 */ NULL,
417         (GtkClassInitFunc) NULL
418       };
419
420       our_type = gtk_type_unique (GTK_TYPE_CONTAINER, &our_info);
421     }
422
423   return our_type;
424 }
425
426 static void
427 add_move_binding (GtkBindingSet  *binding_set,
428                   guint           keyval,
429                   guint           modmask,
430                   GtkMovementStep step,
431                   gint            count)
432 {
433   g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
434
435   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
436                                 "move_cursor", 3,
437                                 GTK_TYPE_ENUM, step,
438                                 GTK_TYPE_INT, count,
439                                 GTK_TYPE_BOOL, FALSE);
440
441   /* Selection-extending version */
442   gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
443                                 "move_cursor", 3,
444                                 GTK_TYPE_ENUM, step,
445                                 GTK_TYPE_INT, count,
446                                 GTK_TYPE_BOOL, TRUE);
447 }
448
449 static void
450 gtk_text_view_class_init (GtkTextViewClass *klass)
451 {
452   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
453   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
454   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
455   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
456   GtkBindingSet *binding_set;
457
458   parent_class = gtk_type_class (GTK_TYPE_CONTAINER);
459
460   /* Default handlers and virtual methods
461    */
462   gobject_class->set_property = gtk_text_view_set_property;
463   gobject_class->get_property = gtk_text_view_get_property;
464
465   object_class->destroy = gtk_text_view_destroy;
466   gobject_class->finalize = gtk_text_view_finalize;
467
468   widget_class->realize = gtk_text_view_realize;
469   widget_class->unrealize = gtk_text_view_unrealize;
470   widget_class->style_set = gtk_text_view_style_set;
471   widget_class->direction_changed = gtk_text_view_direction_changed;
472   widget_class->size_request = gtk_text_view_size_request;
473   widget_class->size_allocate = gtk_text_view_size_allocate;
474   widget_class->event = gtk_text_view_event;
475   widget_class->key_press_event = gtk_text_view_key_press_event;
476   widget_class->key_release_event = gtk_text_view_key_release_event;
477   widget_class->button_press_event = gtk_text_view_button_press_event;
478   widget_class->button_release_event = gtk_text_view_button_release_event;
479   widget_class->focus_in_event = gtk_text_view_focus_in_event;
480   widget_class->focus_out_event = gtk_text_view_focus_out_event;
481   widget_class->motion_notify_event = gtk_text_view_motion_event;
482   widget_class->expose_event = gtk_text_view_expose_event;
483   widget_class->grab_focus = gtk_text_view_grab_focus;
484   
485   widget_class->drag_begin = gtk_text_view_drag_begin;
486   widget_class->drag_end = gtk_text_view_drag_end;
487   widget_class->drag_data_get = gtk_text_view_drag_data_get;
488   widget_class->drag_data_delete = gtk_text_view_drag_data_delete;
489
490   widget_class->drag_leave = gtk_text_view_drag_leave;
491   widget_class->drag_motion = gtk_text_view_drag_motion;
492   widget_class->drag_drop = gtk_text_view_drag_drop;
493   widget_class->drag_data_received = gtk_text_view_drag_data_received;
494
495   widget_class->popup_menu = gtk_text_view_popup_menu;
496   
497   container_class->add = gtk_text_view_add;
498   container_class->remove = gtk_text_view_remove;
499   container_class->forall = gtk_text_view_forall;
500
501   klass->move_cursor = gtk_text_view_move_cursor;
502   klass->set_anchor = gtk_text_view_set_anchor;
503   klass->insert_at_cursor = gtk_text_view_insert_at_cursor;
504   klass->delete_from_cursor = gtk_text_view_delete_from_cursor;
505   klass->cut_clipboard = gtk_text_view_cut_clipboard;
506   klass->copy_clipboard = gtk_text_view_copy_clipboard;
507   klass->paste_clipboard = gtk_text_view_paste_clipboard;
508   klass->toggle_overwrite = gtk_text_view_toggle_overwrite;
509   klass->set_scroll_adjustments = gtk_text_view_set_scroll_adjustments;
510
511   /*
512    * Properties
513    */
514  
515   g_object_class_install_property (gobject_class,
516                                    PROP_PIXELS_ABOVE_LINES,
517                                    g_param_spec_int ("pixels_above_lines",
518                                                      _("Pixels Above Lines"),
519                                                      _("Pixels of blank space above paragraphs"),
520                                                      0,
521                                                      G_MAXINT,
522                                                      0,
523                                                      G_PARAM_READWRITE));
524  
525   g_object_class_install_property (gobject_class,
526                                    PROP_PIXELS_BELOW_LINES,
527                                    g_param_spec_int ("pixels_below_lines",
528                                                      _("Pixels Below Lines"),
529                                                      _("Pixels of blank space below paragraphs"),
530                                                      0,
531                                                      G_MAXINT,
532                                                      0,
533                                                      G_PARAM_READWRITE));
534  
535   g_object_class_install_property (gobject_class,
536                                    PROP_PIXELS_INSIDE_WRAP,
537                                    g_param_spec_int ("pixels_inside_wrap",
538                                                      _("Pixels Inside Wrap"),
539                                                      _("Pixels of blank space between wrapped lines in a paragraph"),
540                                                      0,
541                                                      G_MAXINT,
542                                                      0,
543                                                      G_PARAM_READWRITE));
544
545   g_object_class_install_property (gobject_class,
546                                    PROP_EDITABLE,
547                                    g_param_spec_boolean ("editable",
548                                                          _("Editable"),
549                                                          _("Whether the text can be modified by the user"),
550                                                          TRUE,
551                                                          G_PARAM_READWRITE));
552
553   g_object_class_install_property (gobject_class,
554                                    PROP_WRAP_MODE,
555                                    g_param_spec_enum ("wrap_mode",
556                                                       _("Wrap Mode"),
557                                                       _("Whether to wrap lines never, at word boundaries, or at character boundaries"),
558                                                       GTK_TYPE_WRAP_MODE,
559                                                       GTK_WRAP_NONE,
560                                                       G_PARAM_READWRITE));
561  
562   g_object_class_install_property (gobject_class,
563                                    PROP_JUSTIFICATION,
564                                    g_param_spec_enum ("justification",
565                                                       _("Justification"),
566                                                       _("Left, right, or center justification"),
567                                                       GTK_TYPE_JUSTIFICATION,
568                                                       GTK_JUSTIFY_LEFT,
569                                                       G_PARAM_READWRITE));
570  
571   g_object_class_install_property (gobject_class,
572                                    PROP_LEFT_MARGIN,
573                                    g_param_spec_int ("left_margin",
574                                                      _("Left Margin"),
575                                                      _("Width of the left margin in pixels"),
576                                                      0,
577                                                      G_MAXINT,
578                                                      0,
579                                                      G_PARAM_READWRITE));
580
581   g_object_class_install_property (gobject_class,
582                                    PROP_RIGHT_MARGIN,
583                                    g_param_spec_int ("right_margin",
584                                                      _("Right Margin"),
585                                                      _("Width of the right margin in pixels"),
586                                                      0,
587                                                      G_MAXINT,
588                                                      0,
589                                                      G_PARAM_READWRITE));
590
591   g_object_class_install_property (gobject_class,
592                                    PROP_INDENT,
593                                    g_param_spec_int ("indent",
594                                                      _("Indent"),
595                                                      _("Amount to indent the paragraph, in pixels"),
596                                                      0,
597                                                      G_MAXINT,
598                                                      0,
599                                                      G_PARAM_READWRITE));
600
601   g_object_class_install_property (gobject_class,
602                                    PROP_TABS,
603                                    g_param_spec_boxed ("tabs",
604                                                        _("Tabs"),
605                                                        _("Custom tabs for this text"),
606                                                        PANGO_TYPE_TAB_ARRAY,
607                                                        G_PARAM_READWRITE));
608
609   g_object_class_install_property (gobject_class,
610                                    PROP_CURSOR_VISIBLE,
611                                    g_param_spec_boolean ("cursor_visible",
612                                                          _("Cursor Visible"),
613                                                          _("If the insertion cursor is shown"),
614                                                          TRUE,
615                                                          G_PARAM_READWRITE));
616
617   
618   /*
619    * Style properties
620    */
621
622   gtk_widget_class_install_style_property (widget_class,
623                                            g_param_spec_boxed ("cursor_color",
624                                                                _("Cursor color"),
625                                                                _("Color with which to draw insertion cursor"),
626                                                                GDK_TYPE_COLOR,
627                                                                G_PARAM_READABLE));
628
629
630   /*
631    * Signals
632    */
633
634   signals[MOVE_CURSOR] =
635     gtk_signal_new ("move_cursor",
636                     GTK_RUN_LAST | GTK_RUN_ACTION,
637                     GTK_CLASS_TYPE (object_class),
638                     GTK_SIGNAL_OFFSET (GtkTextViewClass, move_cursor),
639                     _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
640                     GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
641
642   signals[SET_ANCHOR] =
643     gtk_signal_new ("set_anchor",
644                     GTK_RUN_LAST | GTK_RUN_ACTION,
645                     GTK_CLASS_TYPE (object_class),
646                     GTK_SIGNAL_OFFSET (GtkTextViewClass, set_anchor),
647                     _gtk_marshal_VOID__VOID,
648                     GTK_TYPE_NONE, 0);
649
650   signals[INSERT_AT_CURSOR] =
651     gtk_signal_new ("insert_at_cursor",
652                     GTK_RUN_LAST | GTK_RUN_ACTION,
653                     GTK_CLASS_TYPE (object_class),
654                     GTK_SIGNAL_OFFSET (GtkTextViewClass, insert_at_cursor),
655                     _gtk_marshal_VOID__STRING,
656                     GTK_TYPE_NONE, 1, GTK_TYPE_STRING);
657
658   signals[DELETE_FROM_CURSOR] =
659     gtk_signal_new ("delete_from_cursor",
660                     GTK_RUN_LAST | GTK_RUN_ACTION,
661                     GTK_CLASS_TYPE (object_class),
662                     GTK_SIGNAL_OFFSET (GtkTextViewClass, delete_from_cursor),
663                     _gtk_marshal_VOID__ENUM_INT,
664                     GTK_TYPE_NONE, 2, GTK_TYPE_DELETE_TYPE, GTK_TYPE_INT);
665
666   signals[CUT_CLIPBOARD] =
667     gtk_signal_new ("cut_clipboard",
668                     GTK_RUN_LAST | GTK_RUN_ACTION,
669                     GTK_CLASS_TYPE (object_class),
670                     GTK_SIGNAL_OFFSET (GtkTextViewClass, cut_clipboard),
671                     _gtk_marshal_VOID__VOID,
672                     GTK_TYPE_NONE, 0);
673
674   signals[COPY_CLIPBOARD] =
675     gtk_signal_new ("copy_clipboard",
676                     GTK_RUN_LAST | GTK_RUN_ACTION,
677                     GTK_CLASS_TYPE (object_class),
678                     GTK_SIGNAL_OFFSET (GtkTextViewClass, copy_clipboard),
679                     _gtk_marshal_VOID__VOID,
680                     GTK_TYPE_NONE, 0);
681
682   signals[PASTE_CLIPBOARD] =
683     gtk_signal_new ("paste_clipboard",
684                     GTK_RUN_LAST | GTK_RUN_ACTION,
685                     GTK_CLASS_TYPE (object_class),
686                     GTK_SIGNAL_OFFSET (GtkTextViewClass, paste_clipboard),
687                     _gtk_marshal_VOID__VOID,
688                     GTK_TYPE_NONE, 0);
689
690   signals[TOGGLE_OVERWRITE] =
691     gtk_signal_new ("toggle_overwrite",
692                     GTK_RUN_LAST | GTK_RUN_ACTION,
693                     GTK_CLASS_TYPE (object_class),
694                     GTK_SIGNAL_OFFSET (GtkTextViewClass, toggle_overwrite),
695                     _gtk_marshal_VOID__VOID,
696                     GTK_TYPE_NONE, 0);
697
698   signals[SET_SCROLL_ADJUSTMENTS] =
699     gtk_signal_new ("set_scroll_adjustments",
700                     GTK_RUN_LAST,
701                     GTK_CLASS_TYPE (object_class),
702                     GTK_SIGNAL_OFFSET (GtkTextViewClass, set_scroll_adjustments),
703                     _gtk_marshal_VOID__OBJECT_OBJECT,
704                     GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
705   widget_class->set_scroll_adjustments_signal = signals[SET_SCROLL_ADJUSTMENTS];
706
707   signals[POPULATE_POPUP] =
708     gtk_signal_new ("populate_popup",
709                     GTK_RUN_LAST,
710                     GTK_CLASS_TYPE (object_class),
711                     GTK_SIGNAL_OFFSET (GtkTextViewClass, populate_popup),
712                     _gtk_marshal_VOID__OBJECT,
713                     GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
714   
715   /*
716    * Key bindings
717    */
718
719   binding_set = gtk_binding_set_by_class (klass);
720
721   /* Moving the insertion point */
722   add_move_binding (binding_set, GDK_Right, 0,
723                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
724
725   add_move_binding (binding_set, GDK_KP_Right, 0,
726                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
727   
728   add_move_binding (binding_set, GDK_Left, 0,
729                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
730
731   add_move_binding (binding_set, GDK_KP_Left, 0,
732                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
733   
734   add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
735                     GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
736
737   add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
738                     GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
739
740   add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
741                     GTK_MOVEMENT_WORDS, 1);
742
743   add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
744                     GTK_MOVEMENT_WORDS, 1);
745   
746   add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
747                     GTK_MOVEMENT_WORDS, -1);
748
749   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
750                     GTK_MOVEMENT_WORDS, 1);
751   
752   /* Eventually we want to move by display lines, not paragraphs */
753   add_move_binding (binding_set, GDK_Up, 0,
754                     GTK_MOVEMENT_DISPLAY_LINES, -1);
755
756   add_move_binding (binding_set, GDK_KP_Up, 0,
757                     GTK_MOVEMENT_DISPLAY_LINES, -1);
758   
759   add_move_binding (binding_set, GDK_Down, 0,
760                     GTK_MOVEMENT_DISPLAY_LINES, 1);
761
762   add_move_binding (binding_set, GDK_KP_Down, 0,
763                     GTK_MOVEMENT_DISPLAY_LINES, 1);
764   
765   add_move_binding (binding_set, GDK_p, GDK_CONTROL_MASK,
766                     GTK_MOVEMENT_DISPLAY_LINES, -1);
767
768   add_move_binding (binding_set, GDK_n, GDK_CONTROL_MASK,
769                     GTK_MOVEMENT_DISPLAY_LINES, 1);
770
771   add_move_binding (binding_set, GDK_Up, GDK_CONTROL_MASK,
772                     GTK_MOVEMENT_PARAGRAPHS, -1);
773
774   add_move_binding (binding_set, GDK_KP_Up, GDK_CONTROL_MASK,
775                     GTK_MOVEMENT_PARAGRAPHS, -1);
776   
777   add_move_binding (binding_set, GDK_Down, GDK_CONTROL_MASK,
778                     GTK_MOVEMENT_PARAGRAPHS, 1);
779
780   add_move_binding (binding_set, GDK_KP_Down, GDK_CONTROL_MASK,
781                     GTK_MOVEMENT_PARAGRAPHS, 1);
782   
783   add_move_binding (binding_set, GDK_a, GDK_CONTROL_MASK,
784                     GTK_MOVEMENT_PARAGRAPH_ENDS, -1);
785
786   add_move_binding (binding_set, GDK_e, GDK_CONTROL_MASK,
787                     GTK_MOVEMENT_PARAGRAPH_ENDS, 1);
788
789   add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
790                     GTK_MOVEMENT_WORDS, 1);
791
792   add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
793                     GTK_MOVEMENT_WORDS, -1);
794
795   add_move_binding (binding_set, GDK_Home, 0,
796                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
797
798   add_move_binding (binding_set, GDK_KP_Home, 0,
799                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
800   
801   add_move_binding (binding_set, GDK_End, 0,
802                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
803
804   add_move_binding (binding_set, GDK_KP_End, 0,
805                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
806   
807   add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
808                     GTK_MOVEMENT_BUFFER_ENDS, -1);
809
810   add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
811                     GTK_MOVEMENT_BUFFER_ENDS, -1);
812   
813   add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
814                     GTK_MOVEMENT_BUFFER_ENDS, 1);
815
816   add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
817                     GTK_MOVEMENT_BUFFER_ENDS, 1);
818   
819   add_move_binding (binding_set, GDK_Page_Up, 0,
820                     GTK_MOVEMENT_PAGES, -1);
821
822   add_move_binding (binding_set, GDK_KP_Page_Up, 0,
823                     GTK_MOVEMENT_PAGES, -1);
824   
825   add_move_binding (binding_set, GDK_Page_Down, 0,
826                     GTK_MOVEMENT_PAGES, 1);
827
828   add_move_binding (binding_set, GDK_KP_Page_Down, 0,
829                     GTK_MOVEMENT_PAGES, 1);
830   
831   /* Setting the cut/paste/copy anchor */
832   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK,
833                                 "set_anchor", 0);
834   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_CONTROL_MASK,
835                                 "set_anchor", 0);
836   
837   /* Deleting text */
838   gtk_binding_entry_add_signal (binding_set, GDK_Delete, 0,
839                                 "delete_from_cursor", 2,
840                                 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
841                                 GTK_TYPE_INT, 1);
842
843   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, 0,
844                                 "delete_from_cursor", 2,
845                                 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
846                                 GTK_TYPE_INT, 1);
847   
848   gtk_binding_entry_add_signal (binding_set, GDK_d, GDK_CONTROL_MASK,
849                                 "delete_from_cursor", 2,
850                                 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
851                                 GTK_TYPE_INT, 1);
852
853   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, 0,
854                                 "delete_from_cursor", 2,
855                                 GTK_TYPE_ENUM, GTK_DELETE_CHARS,
856                                 GTK_TYPE_INT, -1);
857
858   gtk_binding_entry_add_signal (binding_set, GDK_Delete, GDK_CONTROL_MASK,
859                                 "delete_from_cursor", 2,
860                                 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
861                                 GTK_TYPE_INT, 1);
862
863   gtk_binding_entry_add_signal (binding_set, GDK_KP_Delete, GDK_CONTROL_MASK,
864                                 "delete_from_cursor", 2,
865                                 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
866                                 GTK_TYPE_INT, 1);
867   
868   gtk_binding_entry_add_signal (binding_set, GDK_d, GDK_MOD1_MASK,
869                                 "delete_from_cursor", 2,
870                                 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
871                                 GTK_TYPE_INT, 1);
872
873   gtk_binding_entry_add_signal (binding_set, GDK_BackSpace, GDK_CONTROL_MASK,
874                                 "delete_from_cursor", 2,
875                                 GTK_TYPE_ENUM, GTK_DELETE_WORD_ENDS,
876                                 GTK_TYPE_INT, -1);
877
878   gtk_binding_entry_add_signal (binding_set, GDK_k, GDK_CONTROL_MASK,
879                                 "delete_from_cursor", 2,
880                                 GTK_TYPE_ENUM, GTK_DELETE_PARAGRAPH_ENDS,
881                                 GTK_TYPE_INT, 1);
882
883   gtk_binding_entry_add_signal (binding_set, GDK_u, GDK_CONTROL_MASK,
884                                 "delete_from_cursor", 2,
885                                 GTK_TYPE_ENUM, GTK_DELETE_PARAGRAPHS,
886                                 GTK_TYPE_INT, 1);
887
888   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
889                                 "delete_from_cursor", 2,
890                                 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
891                                 GTK_TYPE_INT, 1);
892   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_MOD1_MASK,
893                                 "delete_from_cursor", 2,
894                                 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
895                                 GTK_TYPE_INT, 1);
896   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_MOD1_MASK,
897                                 "insert_at_cursor", 1,
898                                 GTK_TYPE_STRING, " ");
899   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, GDK_MOD1_MASK,
900                                 "insert_at_cursor", 1,
901                                 GTK_TYPE_STRING, " ");
902   
903   gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_MOD1_MASK,
904                                 "delete_from_cursor", 2,
905                                 GTK_TYPE_ENUM, GTK_DELETE_WHITESPACE,
906                                 GTK_TYPE_INT, 1);
907
908   /* Cut/copy/paste */
909
910   gtk_binding_entry_add_signal (binding_set, GDK_x, GDK_CONTROL_MASK,
911                                 "cut_clipboard", 0);
912
913   gtk_binding_entry_add_signal (binding_set, GDK_w, GDK_CONTROL_MASK,
914                                 "cut_clipboard", 0);
915
916   gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
917                                 "copy_clipboard", 0);
918
919   gtk_binding_entry_add_signal (binding_set, GDK_v, GDK_CONTROL_MASK,
920                                 "paste_clipboard", 0);
921
922   gtk_binding_entry_add_signal (binding_set, GDK_y, GDK_CONTROL_MASK,
923                                 "paste_clipboard", 0);
924
925   /* Overwrite */
926   gtk_binding_entry_add_signal (binding_set, GDK_Insert, 0,
927                                 "toggle_overwrite", 0);
928   gtk_binding_entry_add_signal (binding_set, GDK_KP_Insert, 0,
929                                 "toggle_overwrite", 0);
930 }
931
932 static void
933 gtk_text_view_init (GtkTextView *text_view)
934 {
935   GtkWidget *widget;
936
937   widget = GTK_WIDGET (text_view);
938
939   GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
940
941   /* Set up default style */
942   text_view->wrap_mode = GTK_WRAP_NONE;
943   text_view->pixels_above_lines = 0;
944   text_view->pixels_below_lines = 0;
945   text_view->pixels_inside_wrap = 0;
946   text_view->justify = GTK_JUSTIFY_LEFT;
947   text_view->left_margin = 0;
948   text_view->right_margin = 0;
949   text_view->indent = 0;
950   text_view->tabs = NULL;
951   text_view->editable = TRUE;
952
953   gtk_drag_dest_set (widget,
954                      0,
955                      target_table, G_N_ELEMENTS (target_table),
956                      GDK_ACTION_COPY | GDK_ACTION_MOVE);
957
958   text_view->virtual_cursor_x = -1;
959   text_view->virtual_cursor_y = -1;
960
961   /* This object is completely private. No external entity can gain a reference
962    * to it; so we create it here and destroy it in finalize ().
963    */
964   text_view->im_context = gtk_im_multicontext_new ();
965
966   g_signal_connect (G_OBJECT (text_view->im_context), "commit",
967                     G_CALLBACK (gtk_text_view_commit_handler), text_view);
968   g_signal_connect (G_OBJECT (text_view->im_context), "preedit_changed",
969                     G_CALLBACK (gtk_text_view_preedit_changed_handler), text_view);
970   g_signal_connect (G_OBJECT (text_view->im_context), "retrieve_surrounding",
971                     G_CALLBACK (gtk_text_view_retrieve_surrounding_handler), text_view);
972   g_signal_connect (G_OBJECT (text_view->im_context), "delete_surrounding",
973                     G_CALLBACK (gtk_text_view_delete_surrounding_handler), text_view);
974
975   text_view->cursor_visible = TRUE;
976
977   text_view->text_window = text_window_new (GTK_TEXT_WINDOW_TEXT,
978                                             widget, 200, 200);
979
980   text_view->drag_start_x = -1;
981   text_view->drag_start_y = -1;
982
983   text_view->pending_place_cursor_button = 0;
984
985   /* We handle all our own redrawing */
986   gtk_widget_set_redraw_on_allocate (widget, FALSE);
987 }
988
989 /**
990  * gtk_text_view_new:
991  *
992  * Creates a new #GtkTextView. If you don't call gtk_text_view_set_buffer()
993  * before using the text view, an empty default buffer will be created
994  * for you. Get the buffer with gtk_text_view_get_buffer(). If you want
995  * to specify your own buffer, consider gtk_text_view_new_with_buffer().
996  *
997  * Return value: a new #GtkTextView
998  **/
999 GtkWidget*
1000 gtk_text_view_new (void)
1001 {
1002   return GTK_WIDGET (gtk_type_new (gtk_text_view_get_type ()));
1003 }
1004
1005 /**
1006  * gtk_text_view_new_with_buffer:
1007  * @buffer: a #GtkTextBuffer
1008  *
1009  * Creates a new #GtkTextView widget displaying the buffer
1010  * @buffer. One buffer can be shared among many widgets.
1011  * @buffer may be NULL to create a default buffer, in which case
1012  * this function is equivalent to gtk_text_view_new(). The
1013  * text view adds its own reference count to the buffer; it does not
1014  * take over an existing reference.
1015  *
1016  * Return value: a new #GtkTextView.
1017  **/
1018 GtkWidget*
1019 gtk_text_view_new_with_buffer (GtkTextBuffer *buffer)
1020 {
1021   GtkTextView *text_view;
1022
1023   text_view = (GtkTextView*)gtk_text_view_new ();
1024
1025   gtk_text_view_set_buffer (text_view, buffer);
1026
1027   return GTK_WIDGET (text_view);
1028 }
1029
1030 /**
1031  * gtk_text_view_set_buffer:
1032  * @text_view: a #GtkTextView
1033  * @buffer: a #GtkTextBuffer
1034  *
1035  * Sets @buffer as the buffer being displayed by @text_view. The previous
1036  * buffer displayed by the text view is unreferenced, and a reference is
1037  * added to @buffer. If you owned a reference to @buffer before passing it
1038  * to this function, you must remove that reference yourself; #GtkTextView
1039  * will not "adopt" it.
1040  *
1041  **/
1042 void
1043 gtk_text_view_set_buffer (GtkTextView   *text_view,
1044                           GtkTextBuffer *buffer)
1045 {
1046   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1047   g_return_if_fail (buffer == NULL || GTK_IS_TEXT_BUFFER (buffer));
1048
1049   if (text_view->buffer == buffer)
1050     return;
1051
1052   if (text_view->buffer != NULL)
1053     {
1054       /* Destroy all anchored children */
1055       GSList *tmp_list;
1056       GSList *copy;
1057
1058       copy = g_slist_copy (text_view->children);
1059       tmp_list = copy;
1060       while (tmp_list != NULL)
1061         {
1062           GtkTextViewChild *vc = tmp_list->data;
1063
1064           if (vc->anchor)
1065             {
1066               gtk_widget_destroy (vc->widget);
1067               /* vc may now be invalid! */
1068             }
1069
1070           tmp_list = g_slist_next (tmp_list);
1071         }
1072
1073       g_slist_free (copy);
1074
1075       g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->buffer),
1076                                             gtk_text_view_mark_set_handler, text_view);
1077       g_object_unref (G_OBJECT (text_view->buffer));
1078       text_view->dnd_mark = NULL;
1079
1080       if (GTK_WIDGET_REALIZED (text_view))
1081         gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
1082                                                     gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1083     }
1084
1085   text_view->buffer = buffer;
1086
1087   if (buffer != NULL)
1088     {
1089       GtkTextIter start;
1090
1091       g_object_ref (G_OBJECT (buffer));
1092
1093       if (text_view->layout)
1094         gtk_text_layout_set_buffer (text_view->layout, buffer);
1095
1096       gtk_text_buffer_get_iter_at_offset (text_view->buffer, &start, 0);
1097
1098       text_view->dnd_mark = gtk_text_buffer_create_mark (text_view->buffer,
1099                                                          "gtk_drag_target",
1100                                                          &start, FALSE);
1101
1102       text_view->first_para_mark = gtk_text_buffer_create_mark (text_view->buffer,
1103                                                                 NULL,
1104                                                                 &start, TRUE);
1105
1106       text_view->first_para_pixels = 0;
1107
1108       g_signal_connect (G_OBJECT (text_view->buffer), "mark_set",
1109                         G_CALLBACK (gtk_text_view_mark_set_handler), text_view);
1110
1111       if (GTK_WIDGET_REALIZED (text_view))
1112         gtk_text_buffer_add_selection_clipboard (text_view->buffer,
1113                                                  gtk_clipboard_get (GDK_SELECTION_PRIMARY));
1114     }
1115   
1116   if (GTK_WIDGET_VISIBLE (text_view))
1117     gtk_widget_queue_draw (GTK_WIDGET (text_view));
1118   
1119   gtk_text_view_invalidate (text_view);
1120 }
1121
1122 static GtkTextBuffer*
1123 get_buffer (GtkTextView *text_view)
1124 {
1125   if (text_view->buffer == NULL)
1126     {
1127       GtkTextBuffer *b;
1128       b = gtk_text_buffer_new (NULL);
1129       gtk_text_view_set_buffer (text_view, b);
1130       g_object_unref (G_OBJECT (b));
1131     }
1132
1133   return text_view->buffer;
1134 }
1135
1136 /**
1137  * gtk_text_view_get_buffer:
1138  * @text_view: a #GtkTextView
1139  *
1140  * Returns the #GtkTextBuffer being displayed by this text view.
1141  * The reference count on the buffer is not incremented; the caller
1142  * of this function won't own a new reference.
1143  *
1144  * Return value: a #GtkTextBuffer
1145  **/
1146 GtkTextBuffer*
1147 gtk_text_view_get_buffer (GtkTextView *text_view)
1148 {
1149   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
1150
1151   return get_buffer (text_view);
1152 }
1153
1154 /**
1155  * gtk_text_view_get_iter_at_location:
1156  * @text_view: a #GtkTextView
1157  * @iter: a #GtkTextIter
1158  * @x: x position, in buffer coordinates
1159  * @y: y position, in buffer coordinates
1160  *
1161  * Retrieves the iterator at buffer coordinates @x and @y. Buffer
1162  * coordinates are coordinates for the entire buffer, not just the
1163  * currently-displayed portion.  If you have coordinates from an
1164  * event, you have to convert those to buffer coordinates with
1165  * gtk_text_view_window_to_buffer_coords().
1166  *
1167  **/
1168 void
1169 gtk_text_view_get_iter_at_location (GtkTextView *text_view,
1170                                     GtkTextIter *iter,
1171                                     gint         x,
1172                                     gint         y)
1173 {
1174   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1175   g_return_if_fail (iter != NULL);
1176   g_return_if_fail (text_view->layout != NULL);
1177
1178   gtk_text_layout_get_iter_at_pixel (text_view->layout,
1179                                      iter,
1180                                      x,
1181                                      y);
1182 }
1183
1184 /**
1185  * gtk_text_view_get_iter_location:
1186  * @text_view: a #GtkTextView
1187  * @iter: a #GtkTextIter
1188  * @location: bounds of the character at @iter
1189  *
1190  * Gets a rectangle which roughly contains the character at @iter.
1191  * The rectangle position is in buffer coordinates; use
1192  * gtk_text_view_buffer_to_window_coords() to convert these
1193  * coordinates to coordinates for one of the windows in the text view.
1194  *
1195  **/
1196 void
1197 gtk_text_view_get_iter_location (GtkTextView       *text_view,
1198                                  const GtkTextIter *iter,
1199                                  GdkRectangle      *location)
1200 {
1201   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1202   g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1203
1204   gtk_text_layout_get_iter_location (text_view->layout, iter, location);
1205 }
1206
1207 /**
1208  * gtk_text_view_get_line_yrange:
1209  * @text_view: a #GtkTextView
1210  * @iter: a #GtkTextIter
1211  * @y: return location for a y coordinate
1212  * @height: return location for a height
1213  *
1214  * Gets the y coordinate of the top of the line containing @iter,
1215  * and the height of the line. The coordinate is a buffer coordinate;
1216  * convert to window coordinates with gtk_text_view_buffer_to_window_coords().
1217  *
1218  **/
1219 void
1220 gtk_text_view_get_line_yrange (GtkTextView       *text_view,
1221                                const GtkTextIter *iter,
1222                                gint              *y,
1223                                gint              *height)
1224 {
1225   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1226   g_return_if_fail (gtk_text_iter_get_buffer (iter) == get_buffer (text_view));
1227
1228   gtk_text_layout_get_line_yrange (text_view->layout,
1229                                    iter,
1230                                    y,
1231                                    height);
1232 }
1233
1234 /**
1235  * gtk_text_view_get_line_at_y:
1236  * @text_view: a #GtkTextView
1237  * @target_iter: a #GtkTextIter
1238  * @y: a y coordinate
1239  * @line_top: return location for top coordinate of the line
1240  *
1241  * Gets the #GtkTextIter at the start of the line containing
1242  * the coordinate @y. @y is in buffer coordinates, convert from
1243  * window coordinates with gtk_text_view_window_to_buffer_coords().
1244  * If non-%NULL, @line_top will be filled with the coordinate of the top
1245  * edge of the line.
1246  **/
1247 void
1248 gtk_text_view_get_line_at_y (GtkTextView *text_view,
1249                              GtkTextIter *target_iter,
1250                              gint         y,
1251                              gint        *line_top)
1252 {
1253   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1254
1255   gtk_text_layout_get_line_at_y (text_view->layout,
1256                                  target_iter,
1257                                  y,
1258                                  line_top);
1259 }
1260
1261 static gboolean
1262 set_adjustment_clamped (GtkAdjustment *adj, gdouble val)
1263 {
1264   DV (g_print ("  Setting adj to raw value %g\n", val));
1265   
1266   /* We don't really want to clamp to upper; we want to clamp to
1267      upper - page_size which is the highest value the scrollbar
1268      will let us reach. */
1269   if (val > (adj->upper - adj->page_size))
1270     val = adj->upper - adj->page_size;
1271
1272   if (val < adj->lower)
1273     val = adj->lower;
1274
1275   if (val != adj->value)
1276     {
1277       DV (g_print ("  Setting adj to clamped value %g\n", val));
1278       gtk_adjustment_set_value (adj, val);
1279       return TRUE;
1280     }
1281   else
1282     return FALSE;
1283 }
1284
1285 /**
1286  * gtk_text_view_scroll_to_iter:
1287  * @text_view: a #GtkTextView
1288  * @iter: a #GtkTextIter
1289  * @within_margin: margin as a [0.0,0.5) fraction of screen size
1290  * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1291  * @xalign: horizontal alignment of mark within visible area.
1292  * @yalign: vertical alignment of mark within visible area
1293  *
1294  * Scrolls @text_view so that @iter is on the screen in the position
1295  * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1296  * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1297  * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1298  * possibly not scrolling at all. The effective screen for purposes
1299  * of this function is reduced by a margin of size @within_margin.
1300  * NOTE: This function uses the currently-computed height of the
1301  * lines in the text buffer. Note that line heights are computed
1302  * in an idle handler; so this function may not have the desired effect
1303  * if it's called before the height computations. To avoid oddness,
1304  * consider using gtk_text_view_scroll_to_mark() which saves a point
1305  * to be scrolled to after line validation.
1306  *
1307  * Return value: %TRUE if scrolling occurred
1308  **/
1309 gboolean
1310 gtk_text_view_scroll_to_iter (GtkTextView   *text_view,
1311                               GtkTextIter   *iter,
1312                               gdouble        within_margin,
1313                               gboolean       use_align,
1314                               gdouble        xalign,
1315                               gdouble        yalign)
1316 {
1317   GdkRectangle rect;
1318   GdkRectangle screen;
1319   gint screen_bottom;
1320   gint screen_right;
1321   gint scroll_dest;
1322   GtkWidget *widget;
1323   gboolean retval = FALSE;
1324   gint scroll_inc;
1325   gint screen_xoffset, screen_yoffset;
1326   gint current_x_scroll, current_y_scroll;
1327
1328   /* FIXME why don't we do the validate-at-scroll-destination thing
1329    * from flush_scroll in this function? I think it wasn't done before
1330    * because changed_handler was screwed up, but I could be wrong.
1331    */
1332   
1333   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1334   g_return_val_if_fail (iter != NULL, FALSE);
1335   g_return_val_if_fail (within_margin >= 0.0 && within_margin < 0.5, FALSE);
1336   g_return_val_if_fail (xalign >= 0.0 && xalign <= 1.0, FALSE);
1337   g_return_val_if_fail (yalign >= 0.0 && yalign <= 1.0, FALSE);
1338   
1339   widget = GTK_WIDGET (text_view);
1340
1341   DV(g_print(G_STRLOC"\n"));
1342   
1343   gtk_text_layout_get_iter_location (text_view->layout,
1344                                      iter,
1345                                      &rect);
1346
1347   DV (g_print (" target rect %d,%d  %d x %d\n", rect.x, rect.y, rect.width, rect.height));
1348   
1349   current_x_scroll = text_view->xoffset;
1350   current_y_scroll = text_view->yoffset;
1351
1352   screen.x = current_x_scroll;
1353   screen.y = current_y_scroll;
1354   screen.width = SCREEN_WIDTH (widget);
1355   screen.height = SCREEN_HEIGHT (widget);
1356   
1357   screen_xoffset = screen.width * within_margin;
1358   screen_yoffset = screen.height * within_margin;
1359   
1360   screen.x += screen_xoffset;
1361   screen.y += screen_yoffset;
1362   screen.width -= screen_xoffset * 2;
1363   screen.height -= screen_yoffset * 2;
1364
1365   /* paranoia check */
1366   if (screen.width < 1)
1367     screen.width = 1;
1368   if (screen.height < 1)
1369     screen.height = 1;
1370   
1371   screen_right = screen.x + screen.width;
1372   screen_bottom = screen.y + screen.height;
1373   
1374   /* The alignment affects the point in the target character that we
1375    * choose to align. If we're doing right/bottom alignment, we align
1376    * the right/bottom edge of the character the mark is at; if we're
1377    * doing left/top we align the left/top edge of the character; if
1378    * we're doing center alignment we align the center of the
1379    * character.
1380    */
1381   
1382   /* Vertical scroll */
1383
1384   scroll_inc = 0;
1385   scroll_dest = current_y_scroll;
1386   
1387   if (use_align)
1388     {      
1389       scroll_dest = rect.y + (rect.height * yalign) - (screen.height * yalign);
1390       
1391       /* if scroll_dest < screen.y, we move a negative increment (up),
1392        * else a positive increment (down)
1393        */
1394       scroll_inc = scroll_dest - screen.y + screen_yoffset;
1395     }
1396   else
1397     {
1398       /* move minimum to get onscreen */
1399       if (rect.y < screen.y)
1400         {
1401           scroll_dest = rect.y;
1402           scroll_inc = scroll_dest - screen.y - screen_yoffset;
1403         }
1404       else if ((rect.y + rect.height) > screen_bottom)
1405         {
1406           scroll_dest = rect.y + rect.height;
1407           scroll_inc = scroll_dest - screen_bottom + screen_yoffset;
1408         }
1409     }  
1410   
1411   if (scroll_inc != 0)
1412     {
1413       retval = set_adjustment_clamped (get_vadjustment (text_view),
1414                                        current_y_scroll + scroll_inc);
1415
1416       DV (g_print (" vert increment %d\n", scroll_inc));
1417     }
1418
1419   /* Horizontal scroll */
1420   
1421   scroll_inc = 0;
1422   scroll_dest = current_x_scroll;
1423   
1424   if (use_align)
1425     {      
1426       scroll_dest = rect.x + (rect.width * xalign) - (screen.width * xalign);
1427
1428       /* if scroll_dest < screen.y, we move a negative increment (left),
1429        * else a positive increment (right)
1430        */
1431       scroll_inc = scroll_dest - screen.x + screen_xoffset;
1432     }
1433   else
1434     {
1435       /* move minimum to get onscreen */
1436       if (rect.x < screen.x)
1437         {
1438           scroll_dest = rect.x;
1439           scroll_inc = scroll_dest - screen.x - screen_xoffset;
1440         }
1441       else if ((rect.x + rect.width) > screen_right)
1442         {
1443           scroll_dest = rect.x + rect.width;
1444           scroll_inc = scroll_dest - screen_right + screen_xoffset;
1445         }
1446     }
1447   
1448   if (scroll_inc != 0)
1449     {
1450       retval = set_adjustment_clamped (get_hadjustment (text_view),
1451                                        current_x_scroll + scroll_inc);
1452
1453       DV (g_print (" horiz increment %d\n", scroll_inc));
1454     }
1455   
1456   if (retval)
1457     DV(g_print (">Actually scrolled ("G_STRLOC")\n"));
1458   else
1459     DV(g_print (">Didn't end up scrolling ("G_STRLOC")\n"));
1460   
1461   return retval;
1462 }
1463
1464 static void
1465 free_pending_scroll (GtkTextPendingScroll *scroll)
1466 {
1467   if (!gtk_text_mark_get_deleted (scroll->mark))
1468     gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (scroll->mark),
1469                                  scroll->mark);
1470   g_object_unref (G_OBJECT (scroll->mark));
1471   g_free (scroll);
1472 }
1473
1474 static void
1475 gtk_text_view_queue_scroll (GtkTextView   *text_view,
1476                             GtkTextMark   *mark,
1477                             gdouble        within_margin,
1478                             gboolean       use_align,
1479                             gdouble        xalign,
1480                             gdouble        yalign)
1481 {
1482   GtkTextIter iter;
1483   GtkTextPendingScroll *scroll;
1484
1485   DV(g_print(G_STRLOC"\n"));
1486   
1487   scroll = g_new (GtkTextPendingScroll, 1);
1488
1489   scroll->within_margin = within_margin;
1490   scroll->use_align = use_align;
1491   scroll->xalign = xalign;
1492   scroll->yalign = yalign;
1493   
1494   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1495
1496   scroll->mark = gtk_text_buffer_create_mark (get_buffer (text_view),
1497                                               NULL,
1498                                               &iter,
1499                                               gtk_text_mark_get_left_gravity (mark));
1500
1501   g_object_ref (G_OBJECT (scroll->mark));
1502   
1503   if (text_view->pending_scroll)
1504     free_pending_scroll (text_view->pending_scroll);
1505
1506   text_view->pending_scroll = scroll;
1507 }
1508
1509 static gboolean
1510 gtk_text_view_flush_scroll (GtkTextView *text_view)
1511 {
1512   GtkTextIter iter;
1513   GtkTextPendingScroll *scroll;
1514   gboolean retval;
1515   
1516   DV(g_print(G_STRLOC"\n"));
1517   
1518   if (text_view->pending_scroll == NULL)
1519     return FALSE;
1520
1521   scroll = text_view->pending_scroll;
1522
1523   /* avoid recursion */
1524   text_view->pending_scroll = NULL;
1525   
1526   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, scroll->mark);
1527
1528   /* Validate arbitrary area around the scroll destination, so the adjustment
1529    * can meaningfully point into that area
1530    */
1531   DV(g_print (">Validating scroll destination ("G_STRLOC")\n"));
1532   gtk_text_layout_validate_yrange (text_view->layout, &iter, -300, 300);
1533   
1534   DV(g_print (">Done validating scroll destination ("G_STRLOC")\n"));
1535
1536   /* Ensure we have updated width/height */
1537   gtk_text_view_update_adjustments (text_view);
1538   
1539   retval = gtk_text_view_scroll_to_iter (text_view,
1540                                          &iter,
1541                                          scroll->within_margin,
1542                                          scroll->use_align,
1543                                          scroll->xalign,
1544                                          scroll->yalign);
1545   
1546   free_pending_scroll (scroll);
1547
1548   return retval;
1549 }
1550
1551 static void
1552 gtk_text_view_set_adjustment_upper (GtkAdjustment *adj, gdouble upper)
1553 {  
1554   if (upper != adj->upper)
1555     {
1556       gdouble min = MAX (0.0, upper - adj->page_size);
1557       gboolean value_changed = FALSE;
1558
1559       adj->upper = upper;
1560
1561       if (adj->value > min)
1562         {
1563           adj->value = min;
1564           value_changed = TRUE;
1565         }
1566
1567       gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
1568       DV(g_print(">Changed adj upper to %g ("G_STRLOC")\n", upper));
1569       
1570       if (value_changed)
1571         {
1572           DV(g_print(">Changed adj value because upper decreased ("G_STRLOC")\n"));
1573           gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed");
1574         }
1575     }
1576 }
1577
1578 static void
1579 gtk_text_view_update_adjustments (GtkTextView *text_view)
1580 {
1581   gint width = 0, height = 0;
1582
1583   DV(g_print(">Updating adjustments ("G_STRLOC")\n"));
1584
1585   if (text_view->layout)
1586     gtk_text_layout_get_size (text_view->layout, &width, &height);
1587
1588   if (text_view->width != width || text_view->height != height)
1589     {
1590       text_view->width = width;
1591       text_view->height = height;
1592
1593       gtk_text_view_set_adjustment_upper (get_hadjustment (text_view),
1594                                           MAX (SCREEN_WIDTH (text_view), width));
1595       gtk_text_view_set_adjustment_upper (get_vadjustment (text_view),
1596                                           MAX (SCREEN_HEIGHT (text_view), height));
1597       
1598       /* hadj/vadj exist since we called get_hadjustment/get_vadjustment above */
1599
1600       /* Set up the step sizes; we'll say that a page is
1601          our allocation minus one step, and a step is
1602          1/10 of our allocation. */
1603       text_view->hadjustment->step_increment =
1604         SCREEN_WIDTH (text_view) / 10.0;
1605       text_view->hadjustment->page_increment =
1606         SCREEN_WIDTH (text_view) * 0.9;
1607       
1608       text_view->vadjustment->step_increment =
1609         SCREEN_HEIGHT (text_view) / 10.0;
1610       text_view->vadjustment->page_increment =
1611         SCREEN_HEIGHT (text_view) * 0.9;
1612
1613       gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1614       gtk_signal_emit_by_name (GTK_OBJECT (get_hadjustment (text_view)), "changed");
1615     }
1616 }
1617
1618 static void
1619 gtk_text_view_update_layout_width (GtkTextView *text_view)
1620 {
1621   DV(g_print(">Updating layout width ("G_STRLOC")\n"));
1622   
1623   gtk_text_view_ensure_layout (text_view);
1624
1625   gtk_text_layout_set_screen_width (text_view->layout,
1626                                     SCREEN_WIDTH (text_view));
1627 }
1628
1629 static void
1630 gtk_text_view_update_im_spot_location (GtkTextView *text_view)
1631 {
1632   GdkRectangle area;
1633   gint cursor_x_pos, cursor_y_pos;
1634
1635   if (text_view->layout == NULL)
1636     return;
1637   
1638   gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
1639
1640   area.x = cursor_x_pos;
1641   area.y = cursor_y_pos;
1642   area.width = area.height = 0;
1643
1644   gtk_im_context_set_cursor_location (text_view->im_context, &area);
1645 }
1646
1647 /**
1648  * gtk_text_view_scroll_to_mark:
1649  * @text_view: a #GtkTextView
1650  * @mark: a #GtkTextMark
1651  * @within_margin: margin as a [0.0,0.5) fraction of screen size
1652  * @use_align: whether to use alignment arguments (if %FALSE, just get the mark onscreen)
1653  * @xalign: horizontal alignment of mark within visible area.
1654  * @yalign: vertical alignment of mark within visible area
1655  *
1656  * Scrolls @text_view so that @mark is on the screen in the position
1657  * indicated by @xalign and @yalign. An alignment of 0.0 indicates
1658  * left or top, 1.0 indicates right or bottom, 0.5 means center. If @use_align
1659  * is %FALSE, the text scrolls the minimal distance to get the mark onscreen,
1660  * possibly not scrolling at all. The effective screen for purposes
1661  * of this function is reduced by a margin of size @within_margin.
1662  *
1663  **/
1664 void
1665 gtk_text_view_scroll_to_mark (GtkTextView *text_view,
1666                               GtkTextMark *mark,
1667                               gdouble      within_margin,
1668                               gboolean     use_align,
1669                               gdouble      xalign,
1670                               gdouble      yalign)
1671 {  
1672   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1673   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1674   g_return_if_fail (within_margin >= 0.0 && within_margin < 0.5);
1675   g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
1676   g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
1677
1678   gtk_text_view_queue_scroll (text_view, mark,
1679                               within_margin,
1680                               use_align,
1681                               xalign,
1682                               yalign);
1683
1684   /* If no validation is pending, we need to go ahead and force an
1685    * immediate scroll.
1686    */
1687   if (text_view->layout &&
1688       gtk_text_layout_is_valid (text_view->layout))
1689     gtk_text_view_flush_scroll (text_view);
1690 }
1691
1692 void
1693 gtk_text_view_scroll_mark_onscreen (GtkTextView *text_view,
1694                                     GtkTextMark *mark)
1695 {
1696   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1697   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1698
1699   gtk_text_view_scroll_to_mark (text_view, mark, 0.0, FALSE, 0.0, 0.0);
1700 }
1701
1702 static gboolean
1703 clamp_iter_onscreen (GtkTextView *text_view, GtkTextIter *iter)
1704 {
1705   GdkRectangle visible_rect;
1706   gtk_text_view_get_visible_rect (text_view, &visible_rect);
1707
1708   return gtk_text_layout_clamp_iter_to_vrange (text_view->layout, iter,
1709                                                visible_rect.y,
1710                                                visible_rect.y + visible_rect.height);
1711 }
1712
1713 /**
1714  * gtk_text_view_move_mark_onscreen:
1715  * @text_view: a #GtkTextView
1716  * @mark: a #GtkTextMark
1717  *
1718  * Moves a mark within the buffer so that it's
1719  * located within the currently-visible text area.
1720  *
1721  * Return value: %TRUE if the mark moved (wasn't already onscreen)
1722  **/
1723 gboolean
1724 gtk_text_view_move_mark_onscreen (GtkTextView *text_view,
1725                                   GtkTextMark *mark)
1726 {
1727   GtkTextIter iter;
1728
1729   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1730   g_return_val_if_fail (mark != NULL, FALSE);
1731
1732   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, mark);
1733
1734   if (clamp_iter_onscreen (text_view, &iter))
1735     {
1736       gtk_text_buffer_move_mark (get_buffer (text_view), mark, &iter);
1737       return TRUE;
1738     }
1739   else
1740     return FALSE;
1741 }
1742
1743 /**
1744  * gtk_text_view_get_visible_rect:
1745  * @text_view: a #GtkTextView
1746  * @visible_rect: rectangle to fill
1747  *
1748  * Fills @visible_rect with the currently-visible
1749  * region of the buffer, in buffer coordinates. Convert to window coordinates
1750  * with gtk_text_view_buffer_to_window_coords().
1751  **/
1752 void
1753 gtk_text_view_get_visible_rect (GtkTextView  *text_view,
1754                                 GdkRectangle *visible_rect)
1755 {
1756   GtkWidget *widget;
1757
1758   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1759
1760   widget = GTK_WIDGET (text_view);
1761
1762   if (visible_rect)
1763     {
1764       visible_rect->x = text_view->xoffset;
1765       visible_rect->y = text_view->yoffset;
1766       visible_rect->width = SCREEN_WIDTH (widget);
1767       visible_rect->height = SCREEN_HEIGHT (widget);
1768
1769       DV(g_print(" visible rect: %d,%d %d x %d\n",
1770                  visible_rect->x,
1771                  visible_rect->y,
1772                  visible_rect->width,
1773                  visible_rect->height));
1774     }
1775 }
1776
1777 /**
1778  * gtk_text_view_set_wrap_mode:
1779  * @text_view: a #GtkTextView
1780  * @wrap_mode: a #GtkWrapMode
1781  *
1782  * Sets the line wrapping for the view.
1783  **/
1784 void
1785 gtk_text_view_set_wrap_mode (GtkTextView *text_view,
1786                              GtkWrapMode  wrap_mode)
1787 {
1788   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1789
1790   if (text_view->wrap_mode != wrap_mode)
1791     {
1792       text_view->wrap_mode = wrap_mode;
1793
1794       if (text_view->layout)
1795         {
1796           text_view->layout->default_style->wrap_mode = wrap_mode;
1797           gtk_text_layout_default_style_changed (text_view->layout);
1798         }
1799     }
1800
1801   g_object_notify (G_OBJECT (text_view), "wrap_mode");
1802 }
1803
1804 /**
1805  * gtk_text_view_get_wrap_mode:
1806  * @text_view: a #GtkTextView
1807  *
1808  * Gets the line wrapping for the view.
1809  *
1810  * Return value: the line wrap setting
1811  **/
1812 GtkWrapMode
1813 gtk_text_view_get_wrap_mode (GtkTextView *text_view)
1814 {
1815   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_WRAP_NONE);
1816
1817   return text_view->wrap_mode;
1818 }
1819
1820 /**
1821  * gtk_text_view_set_editable:
1822  * @text_view: a #GtkTextView
1823  * @setting: whether it's editable
1824  *
1825  * Sets the default editability of the #GtkTextView. You can override
1826  * this default setting with tags in the buffer, using the "editable"
1827  * attribute of tags.
1828  **/
1829 void
1830 gtk_text_view_set_editable (GtkTextView *text_view,
1831                             gboolean     setting)
1832 {
1833   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1834
1835   if (text_view->editable != setting)
1836     {
1837       text_view->editable = setting;
1838
1839       if (text_view->layout)
1840         {
1841           text_view->layout->default_style->editable = text_view->editable;
1842           gtk_text_layout_default_style_changed (text_view->layout);
1843         }
1844     }
1845
1846   g_object_notify (G_OBJECT (text_view), "editable");
1847 }
1848
1849 /**
1850  * gtk_text_view_get_editable:
1851  * @text_view: a #GtkTextView
1852  *
1853  * Returns the default editability of the #GtkTextView. Tags in the
1854  * buffer may override this setting for some ranges of text.
1855  *
1856  * Return value: whether text is editable by default
1857  **/
1858 gboolean
1859 gtk_text_view_get_editable (GtkTextView *text_view)
1860 {
1861   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
1862
1863   return text_view->editable;
1864 }
1865
1866 void
1867 gtk_text_view_set_pixels_above_lines (GtkTextView *text_view,
1868                                       gint         pixels_above_lines)
1869 {
1870   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1871
1872   if (text_view->pixels_above_lines != pixels_above_lines)
1873     {
1874       text_view->pixels_above_lines = pixels_above_lines;
1875
1876       if (text_view->layout)
1877         {
1878           text_view->layout->default_style->pixels_above_lines = pixels_above_lines;
1879           gtk_text_layout_default_style_changed (text_view->layout);
1880         }
1881     }
1882
1883   g_object_notify (G_OBJECT (text_view), "pixels_above_lines");
1884 }
1885
1886 gint
1887 gtk_text_view_get_pixels_above_lines (GtkTextView *text_view)
1888 {
1889   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1890
1891   return text_view->pixels_above_lines;
1892 }
1893
1894 void
1895 gtk_text_view_set_pixels_below_lines (GtkTextView *text_view,
1896                                       gint         pixels_below_lines)
1897 {
1898   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1899
1900   if (text_view->pixels_below_lines != pixels_below_lines)
1901     {
1902       text_view->pixels_below_lines = pixels_below_lines;
1903
1904       if (text_view->layout)
1905         {
1906           text_view->layout->default_style->pixels_below_lines = pixels_below_lines;
1907           gtk_text_layout_default_style_changed (text_view->layout);
1908         }
1909     }
1910
1911   g_object_notify (G_OBJECT (text_view), "pixels_below_lines");
1912 }
1913
1914 gint
1915 gtk_text_view_get_pixels_below_lines (GtkTextView *text_view)
1916 {
1917   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1918
1919   return text_view->pixels_below_lines;
1920 }
1921
1922 void
1923 gtk_text_view_set_pixels_inside_wrap (GtkTextView *text_view,
1924                                       gint         pixels_inside_wrap)
1925 {
1926   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1927
1928   if (text_view->pixels_inside_wrap != pixels_inside_wrap)
1929     {
1930       text_view->pixels_inside_wrap = pixels_inside_wrap;
1931
1932       if (text_view->layout)
1933         {
1934           text_view->layout->default_style->pixels_inside_wrap = pixels_inside_wrap;
1935           gtk_text_layout_default_style_changed (text_view->layout);
1936         }
1937     }
1938   g_object_notify (G_OBJECT (text_view), "pixels_inside_wrap");
1939 }
1940
1941 gint
1942 gtk_text_view_get_pixels_inside_wrap (GtkTextView *text_view)
1943 {
1944   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
1945
1946   return text_view->pixels_inside_wrap;
1947 }
1948
1949 void
1950 gtk_text_view_set_justification (GtkTextView     *text_view,
1951                                  GtkJustification justify)
1952 {
1953   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1954
1955   if (text_view->justify != justify)
1956     {
1957       text_view->justify = justify;
1958
1959       if (text_view->layout)
1960         {
1961           text_view->layout->default_style->justification = justify;
1962           gtk_text_layout_default_style_changed (text_view->layout);
1963         }
1964     }
1965
1966   g_object_notify (G_OBJECT (text_view), "justification");
1967 }
1968
1969 GtkJustification
1970 gtk_text_view_get_justification (GtkTextView *text_view)
1971 {
1972   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), GTK_JUSTIFY_LEFT);
1973
1974   return text_view->justify;
1975 }
1976
1977 void
1978 gtk_text_view_set_left_margin (GtkTextView *text_view,
1979                                gint         left_margin)
1980 {
1981   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
1982
1983   if (text_view->left_margin != left_margin)
1984     {
1985       text_view->left_margin = left_margin;
1986
1987       if (text_view->layout)
1988         {
1989           text_view->layout->default_style->left_margin = left_margin;
1990           gtk_text_layout_default_style_changed (text_view->layout);
1991         }
1992     }
1993
1994   g_object_notify (G_OBJECT (text_view), "left_margin");
1995 }
1996
1997 gint
1998 gtk_text_view_get_left_margin (GtkTextView *text_view)
1999 {
2000   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2001
2002   return text_view->left_margin;
2003 }
2004
2005 void
2006 gtk_text_view_set_right_margin (GtkTextView *text_view,
2007                                 gint         right_margin)
2008 {
2009   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2010
2011   if (text_view->right_margin != right_margin)
2012     {
2013       text_view->right_margin = right_margin;
2014
2015       if (text_view->layout)
2016         {
2017           text_view->layout->default_style->right_margin = right_margin;
2018           gtk_text_layout_default_style_changed (text_view->layout);
2019         }
2020     }
2021
2022   g_object_notify (G_OBJECT (text_view), "right_margin");
2023 }
2024
2025 gint
2026 gtk_text_view_get_right_margin (GtkTextView *text_view)
2027 {
2028   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2029
2030   return text_view->right_margin;
2031 }
2032
2033 void
2034 gtk_text_view_set_indent (GtkTextView *text_view,
2035                           gint         indent)
2036 {
2037   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2038
2039   if (text_view->indent != indent)
2040     {
2041       text_view->indent = indent;
2042
2043       if (text_view->layout)
2044         {
2045           text_view->layout->default_style->indent = indent;
2046           gtk_text_layout_default_style_changed (text_view->layout);
2047         }
2048     }
2049   g_object_notify (G_OBJECT (text_view), "indent");
2050 }
2051
2052 gint
2053 gtk_text_view_get_indent (GtkTextView *text_view)
2054 {
2055   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
2056
2057   return text_view->indent;
2058 }
2059
2060 void
2061 gtk_text_view_set_tabs (GtkTextView   *text_view,
2062                         PangoTabArray *tabs)
2063 {
2064   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2065
2066   if (text_view->tabs)
2067     pango_tab_array_free (text_view->tabs);
2068
2069   text_view->tabs = tabs ? pango_tab_array_copy (tabs) : NULL;
2070
2071   if (text_view->layout)
2072     {
2073       /* some unkosher futzing in internal struct details... */
2074       if (text_view->layout->default_style->tabs)
2075         pango_tab_array_free (text_view->layout->default_style->tabs);
2076
2077       text_view->layout->default_style->tabs =
2078         text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2079
2080       gtk_text_layout_default_style_changed (text_view->layout);
2081     }
2082
2083   g_object_notify (G_OBJECT (text_view), "tabs");
2084 }
2085
2086 PangoTabArray*
2087 gtk_text_view_get_tabs (GtkTextView *text_view)
2088 {
2089   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
2090
2091   return text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
2092 }
2093
2094 /**
2095  * gtk_text_view_set_cursor_visible:
2096  * @text_view: a #GtkTextView
2097  * @setting: whether to show the insertion cursor
2098  *
2099  * Toggles whether the insertion point is displayed. A buffer with no editable
2100  * text probably shouldn't have a visible cursor, so you may want to turn
2101  * the cursor off.
2102  **/
2103 void
2104 gtk_text_view_set_cursor_visible    (GtkTextView   *text_view,
2105                                      gboolean       setting)
2106 {
2107   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
2108
2109   setting = (setting != FALSE);
2110
2111   if (text_view->cursor_visible != setting)
2112     {
2113       text_view->cursor_visible = setting;
2114
2115       if (GTK_WIDGET_HAS_FOCUS (text_view))
2116         {
2117           if (text_view->layout)
2118             {
2119               gtk_text_layout_set_cursor_visible (text_view->layout, setting);
2120               gtk_text_view_check_cursor_blink (text_view);
2121             }
2122         }
2123     }
2124
2125   g_object_notify (G_OBJECT (text_view), "cursor_visible");
2126 }
2127
2128 /**
2129  * gtk_text_view_get_cursor_visible:
2130  * @text_view: a #GtkTextView
2131  *
2132  * Find out whether the cursor is being displayed.
2133  *
2134  * Return value: whether the insertion mark is visible
2135  **/
2136 gboolean
2137 gtk_text_view_get_cursor_visible    (GtkTextView   *text_view)
2138 {
2139   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2140
2141   return text_view->cursor_visible;
2142 }
2143
2144
2145 /**
2146  * gtk_text_view_place_cursor_onscreen:
2147  * @text_view: a #GtkTextView
2148  *
2149  * Moves the cursor to the currently visible region of the
2150  * buffer, it it isn't there already.
2151  *
2152  * Return value: TRUE if the cursor had to be moved.
2153  **/
2154 gboolean
2155 gtk_text_view_place_cursor_onscreen (GtkTextView *text_view)
2156 {
2157   GtkTextIter insert;
2158
2159   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
2160
2161   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
2162                                     gtk_text_buffer_get_mark (get_buffer (text_view),
2163                                                               "insert"));
2164
2165   if (clamp_iter_onscreen (text_view, &insert))
2166     {
2167       gtk_text_buffer_place_cursor (get_buffer (text_view), &insert);
2168       return TRUE;
2169     }
2170   else
2171     return FALSE;
2172 }
2173
2174 static void
2175 gtk_text_view_remove_validate_idles (GtkTextView *text_view)
2176 {
2177   if (text_view->first_validate_idle != 0)
2178     {
2179       g_source_remove (text_view->first_validate_idle);
2180       text_view->first_validate_idle = 0;
2181     }
2182
2183   if (text_view->incremental_validate_idle != 0)
2184     {
2185       g_source_remove (text_view->incremental_validate_idle);
2186       text_view->incremental_validate_idle = 0;
2187     }
2188 }
2189
2190 static void
2191 gtk_text_view_destroy (GtkObject *object)
2192 {
2193   GtkTextView *text_view;
2194   GtkTextLayout *layout;
2195   
2196   text_view = GTK_TEXT_VIEW (object);
2197
2198   layout = text_view->layout;
2199   
2200   gtk_text_view_remove_validate_idles (text_view);
2201   gtk_text_view_set_buffer (text_view, NULL);
2202   gtk_text_view_destroy_layout (text_view);
2203
2204   (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
2205 }
2206
2207 static void
2208 gtk_text_view_finalize (GObject *object)
2209 {
2210   GtkTextView *text_view;
2211
2212   text_view = GTK_TEXT_VIEW (object);
2213
2214   g_return_if_fail (text_view->buffer == NULL);
2215
2216   gtk_text_view_destroy_layout (text_view);
2217   gtk_text_view_set_buffer (text_view, NULL);
2218   
2219   if (text_view->pending_scroll)
2220     {
2221       free_pending_scroll (text_view->pending_scroll);
2222       text_view->pending_scroll = NULL;
2223     }
2224   
2225   if (text_view->hadjustment)
2226     g_object_unref (G_OBJECT (text_view->hadjustment));
2227   if (text_view->vadjustment)
2228     g_object_unref (G_OBJECT (text_view->vadjustment));
2229
2230   text_window_free (text_view->text_window);
2231
2232   if (text_view->left_window)
2233     text_window_free (text_view->left_window);
2234
2235   if (text_view->top_window)
2236     text_window_free (text_view->top_window);
2237
2238   if (text_view->right_window)
2239     text_window_free (text_view->right_window);
2240
2241   if (text_view->bottom_window)
2242     text_window_free (text_view->bottom_window);
2243
2244   g_object_unref (G_OBJECT (text_view->im_context));
2245
2246   (* G_OBJECT_CLASS (parent_class)->finalize) (object);
2247 }
2248
2249 static void
2250 gtk_text_view_set_property (GObject         *object,
2251                             guint            prop_id,
2252                             const GValue    *value,
2253                             GParamSpec      *pspec)
2254 {
2255   GtkTextView *text_view;
2256
2257   text_view = GTK_TEXT_VIEW (object);
2258
2259   switch (prop_id)
2260     {
2261     case PROP_PIXELS_ABOVE_LINES:
2262       gtk_text_view_set_pixels_above_lines (text_view, g_value_get_int (value));
2263       break;
2264
2265     case PROP_PIXELS_BELOW_LINES:
2266       gtk_text_view_set_pixels_below_lines (text_view, g_value_get_int (value));
2267       break;
2268
2269     case PROP_PIXELS_INSIDE_WRAP:
2270       gtk_text_view_set_pixels_inside_wrap (text_view, g_value_get_int (value));
2271       break;
2272
2273     case PROP_EDITABLE:
2274       gtk_text_view_set_editable (text_view, g_value_get_boolean (value));
2275       break;
2276
2277     case PROP_WRAP_MODE:
2278       gtk_text_view_set_wrap_mode (text_view, g_value_get_enum (value));
2279       break;
2280       
2281     case PROP_JUSTIFICATION:
2282       gtk_text_view_set_justification (text_view, g_value_get_enum (value));
2283       break;
2284
2285     case PROP_LEFT_MARGIN:
2286       gtk_text_view_set_left_margin (text_view, g_value_get_int (value));
2287       break;
2288
2289     case PROP_RIGHT_MARGIN:
2290       gtk_text_view_set_right_margin (text_view, g_value_get_int (value));
2291       break;
2292
2293     case PROP_INDENT:
2294       gtk_text_view_set_indent (text_view, g_value_get_int (value));
2295       break;
2296
2297     case PROP_TABS:
2298       gtk_text_view_set_tabs (text_view, g_value_get_object (value));
2299       break;
2300
2301     case PROP_CURSOR_VISIBLE:
2302       gtk_text_view_set_cursor_visible (text_view, g_value_get_boolean (value));
2303       break;
2304
2305     default:
2306       g_assert_not_reached ();
2307       break;
2308     }
2309 }
2310
2311 static void
2312 gtk_text_view_get_property (GObject         *object,
2313                             guint            prop_id,
2314                             GValue          *value,
2315                             GParamSpec      *pspec)
2316 {
2317   GtkTextView *text_view;
2318
2319   text_view = GTK_TEXT_VIEW (object);
2320
2321   switch (prop_id)
2322     {
2323     case PROP_PIXELS_ABOVE_LINES:
2324       g_value_set_int (value, text_view->pixels_above_lines);
2325       break;
2326
2327     case PROP_PIXELS_BELOW_LINES:
2328       g_value_set_int (value, text_view->pixels_below_lines);
2329       break;
2330
2331     case PROP_PIXELS_INSIDE_WRAP:
2332       g_value_set_int (value, text_view->pixels_inside_wrap);
2333       break;
2334
2335     case PROP_EDITABLE:
2336       g_value_set_boolean (value, text_view->editable);
2337       break;
2338       
2339     case PROP_WRAP_MODE:
2340       g_value_set_enum (value, text_view->wrap_mode);
2341       break;
2342
2343     case PROP_JUSTIFICATION:
2344       g_value_set_enum (value, text_view->justify);
2345       break;
2346
2347     case PROP_LEFT_MARGIN:
2348       g_value_set_int (value, text_view->left_margin);
2349       break;
2350
2351     case PROP_RIGHT_MARGIN:
2352       g_value_set_int (value, text_view->right_margin);
2353       break;
2354
2355     case PROP_INDENT:
2356       g_value_set_int (value, text_view->indent);
2357       break;
2358
2359     case PROP_TABS:
2360       g_value_set_object (value, gtk_text_view_get_tabs (text_view));
2361       break;
2362
2363     case PROP_CURSOR_VISIBLE:
2364       g_value_set_boolean (value, text_view->cursor_visible);
2365       break;
2366
2367     default:
2368       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2369       break;
2370     }
2371 }
2372
2373 static void
2374 gtk_text_view_size_request (GtkWidget      *widget,
2375                             GtkRequisition *requisition)
2376 {
2377   GtkTextView *text_view;
2378   GSList *tmp_list;
2379   gint focus_edge_width;
2380   gboolean interior_focus;
2381   
2382   text_view = GTK_TEXT_VIEW (widget);
2383
2384   gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
2385
2386   if (interior_focus)
2387     focus_edge_width = 0;
2388   else
2389     focus_edge_width = 1;
2390
2391   if (text_view->layout)
2392     {
2393       text_view->text_window->requisition.width = text_view->layout->width;
2394       text_view->text_window->requisition.height = text_view->layout->height;
2395     }
2396   else
2397     {
2398       text_view->text_window->requisition.width = 0;
2399       text_view->text_window->requisition.height = 0;
2400     }
2401   
2402   requisition->width = text_view->text_window->requisition.width + focus_edge_width * 2;
2403   requisition->height = text_view->text_window->requisition.height + focus_edge_width * 2;
2404
2405   if (text_view->left_window)
2406     requisition->width += text_view->left_window->requisition.width;
2407
2408   if (text_view->right_window)
2409     requisition->width += text_view->right_window->requisition.width;
2410
2411   if (text_view->top_window)
2412     requisition->height += text_view->top_window->requisition.height;
2413
2414   if (text_view->bottom_window)
2415     requisition->height += text_view->bottom_window->requisition.height;
2416
2417   requisition->width += GTK_CONTAINER (text_view)->border_width * 2;
2418   requisition->height += GTK_CONTAINER (text_view)->border_width * 2;
2419   
2420   tmp_list = text_view->children;
2421   while (tmp_list != NULL)
2422     {
2423       GtkTextViewChild *child = tmp_list->data;
2424
2425       if (child->anchor)
2426         {
2427           GtkRequisition child_req;
2428           GtkRequisition old_req;
2429
2430           gtk_widget_get_child_requisition (child->widget, &old_req);
2431           
2432           gtk_widget_size_request (child->widget, &child_req);
2433
2434           gtk_widget_get_child_requisition (child->widget, &child_req);
2435
2436           /* Invalidate layout lines if required */
2437           if (text_view->layout &&
2438               (old_req.width != child_req.width ||
2439                old_req.height != child_req.height))
2440             gtk_text_child_anchor_queue_resize (child->anchor,
2441                                                 text_view->layout);
2442         }
2443       else
2444         {
2445           GtkRequisition child_req;
2446           
2447           gtk_widget_size_request (child->widget, &child_req);
2448         }
2449
2450       tmp_list = g_slist_next (tmp_list);
2451     }
2452 }
2453
2454 static void
2455 gtk_text_view_compute_child_allocation (GtkTextView      *text_view,
2456                                         GtkTextViewChild *vc,
2457                                         GtkAllocation    *allocation)
2458 {
2459   gint buffer_y;
2460   GtkTextIter iter;
2461   GtkRequisition req;
2462   
2463   gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2464                                             &iter,
2465                                             vc->anchor);
2466
2467   gtk_text_layout_get_line_yrange (text_view->layout, &iter,
2468                                    &buffer_y, NULL);
2469
2470   buffer_y += vc->from_top_of_line;
2471
2472   allocation->x = vc->from_left_of_buffer - text_view->xoffset;
2473   allocation->y = buffer_y - text_view->yoffset;
2474
2475   gtk_widget_get_child_requisition (vc->widget, &req);
2476   allocation->width = req.width;
2477   allocation->height = req.height;
2478 }
2479
2480 static void
2481 gtk_text_view_update_child_allocation (GtkTextView      *text_view,
2482                                        GtkTextViewChild *vc)
2483 {
2484   GtkAllocation allocation;
2485
2486   gtk_text_view_compute_child_allocation (text_view, vc, &allocation);
2487   
2488   gtk_widget_size_allocate (vc->widget, &allocation);
2489
2490 #if 0
2491   g_print ("allocation for %p allocated to %d,%d yoffset = %d\n",
2492            vc->widget,
2493            vc->widget->allocation.x,
2494            vc->widget->allocation.y,
2495            text_view->yoffset);
2496 #endif
2497 }
2498
2499 static void
2500 gtk_text_view_child_allocated (GtkTextLayout *layout,
2501                                GtkWidget     *child,
2502                                gint           x,
2503                                gint           y,
2504                                gpointer       data)
2505 {
2506   GtkTextViewChild *vc = NULL;
2507   GtkTextView *text_view = data;
2508   
2509   /* x,y is the position of the child from the top of the line, and
2510    * from the left of the buffer. We have to translate that into text
2511    * window coordinates, then size_allocate the child.
2512    */
2513
2514   vc = g_object_get_data (G_OBJECT (child),
2515                           "gtk-text-view-child");
2516
2517   g_assert (vc != NULL);
2518
2519   DV (g_print ("child allocated at %d,%d\n", x, y));
2520   
2521   vc->from_left_of_buffer = x;
2522   vc->from_top_of_line = y;
2523
2524   gtk_text_view_update_child_allocation (text_view, vc);
2525 }
2526
2527 static void
2528 gtk_text_view_allocate_children (GtkTextView *text_view)
2529 {
2530   GSList *tmp_list;
2531
2532   DV(g_print(G_STRLOC"\n"));
2533   
2534   tmp_list = text_view->children;
2535   while (tmp_list != NULL)
2536     {
2537       GtkTextViewChild *child = tmp_list->data;
2538
2539       if (child->anchor)
2540         {
2541           /* We need to force-validate the regions containing
2542            * children.
2543            */
2544           GtkTextIter child_loc;
2545           gtk_text_buffer_get_iter_at_child_anchor (get_buffer (text_view),
2546                                                     &child_loc,
2547                                                     child->anchor);
2548
2549           gtk_text_layout_validate_yrange (text_view->layout,
2550                                            &child_loc,
2551                                            0, 1);
2552         }
2553       else
2554         {
2555           GtkAllocation allocation;          
2556           GtkRequisition child_req;
2557              
2558           g_assert (child != NULL);
2559           
2560           allocation.x = child->x;
2561           allocation.y = child->y;
2562
2563           gtk_widget_get_child_requisition (child->widget, &child_req);
2564           
2565           allocation.width = child_req.width;
2566           allocation.height = child_req.height;
2567           
2568           gtk_widget_size_allocate (child->widget, &allocation);          
2569         }
2570
2571       tmp_list = g_slist_next (tmp_list);
2572     }
2573 }
2574
2575 static void
2576 gtk_text_view_size_allocate (GtkWidget *widget,
2577                              GtkAllocation *allocation)
2578 {
2579   GtkTextView *text_view;
2580   GtkTextIter first_para;
2581   gint y;
2582   GtkAdjustment *vadj;
2583   gboolean yoffset_changed = FALSE;
2584   gint width, height;
2585   GdkRectangle text_rect;
2586   GdkRectangle left_rect;
2587   GdkRectangle right_rect;
2588   GdkRectangle top_rect;
2589   GdkRectangle bottom_rect;
2590   gint focus_edge_width;
2591   gboolean interior_focus;
2592   gboolean size_changed;
2593   
2594   text_view = GTK_TEXT_VIEW (widget);
2595
2596   DV(g_print(G_STRLOC"\n"));
2597
2598   size_changed =
2599     widget->allocation.width != allocation->width ||
2600     widget->allocation.height != allocation->height;
2601   
2602   widget->allocation = *allocation;
2603
2604   if (GTK_WIDGET_REALIZED (widget))
2605     {
2606       gdk_window_move_resize (widget->window,
2607                               allocation->x, allocation->y,
2608                               allocation->width, allocation->height);
2609     }
2610
2611   /* distribute width/height among child windows. Ensure all
2612    * windows get at least a 1x1 allocation.
2613    */
2614
2615   gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
2616   
2617   if (interior_focus)
2618     focus_edge_width = 0;
2619   else
2620     focus_edge_width = 1;
2621   
2622   width = allocation->width - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2623
2624   if (text_view->left_window)
2625     left_rect.width = text_view->left_window->requisition.width;
2626   else
2627     left_rect.width = 0;
2628
2629   width -= left_rect.width;
2630
2631   if (text_view->right_window)
2632     right_rect.width = text_view->right_window->requisition.width;
2633   else
2634     right_rect.width = 0;
2635
2636   width -= right_rect.width;
2637
2638   text_rect.width = MAX (1, width);
2639
2640   top_rect.width = text_rect.width;
2641   bottom_rect.width = text_rect.width;
2642
2643
2644   height = allocation->height - focus_edge_width * 2 - GTK_CONTAINER (text_view)->border_width * 2;
2645
2646   if (text_view->top_window)
2647     top_rect.height = text_view->top_window->requisition.height;
2648   else
2649     top_rect.height = 0;
2650
2651   height -= top_rect.height;
2652
2653   if (text_view->bottom_window)
2654     bottom_rect.height = text_view->bottom_window->requisition.height;
2655   else
2656     bottom_rect.height = 0;
2657
2658   height -= bottom_rect.height;
2659
2660   text_rect.height = MAX (1, height);
2661
2662   left_rect.height = text_rect.height;
2663   right_rect.height = text_rect.height;
2664
2665   /* Origins */
2666   left_rect.x = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2667   top_rect.y = focus_edge_width + GTK_CONTAINER (text_view)->border_width;
2668
2669   text_rect.x = left_rect.x + left_rect.width;
2670   text_rect.y = top_rect.y + top_rect.height;
2671
2672   left_rect.y = text_rect.y;
2673   right_rect.y = text_rect.y;
2674
2675   top_rect.x = text_rect.x;
2676   bottom_rect.x = text_rect.x;
2677
2678   right_rect.x = text_rect.x + text_rect.width;
2679   bottom_rect.y = text_rect.y + text_rect.height;
2680
2681   text_window_size_allocate (text_view->text_window,
2682                              &text_rect);
2683
2684   if (text_view->left_window)
2685     text_window_size_allocate (text_view->left_window,
2686                                &left_rect);
2687
2688   if (text_view->right_window)
2689     text_window_size_allocate (text_view->right_window,
2690                                &right_rect);
2691
2692   if (text_view->top_window)
2693     text_window_size_allocate (text_view->top_window,
2694                                &top_rect);
2695
2696   if (text_view->bottom_window)
2697     text_window_size_allocate (text_view->bottom_window,
2698                                &bottom_rect);
2699
2700   gtk_text_view_update_layout_width (text_view);
2701   
2702   /* Note that this will do some layout validation */
2703   gtk_text_view_allocate_children (text_view);
2704
2705   /* Now adjust the value of the adjustment to keep the cursor at the
2706    * same place in the buffer
2707    */
2708   gtk_text_view_get_first_para_iter (text_view, &first_para);
2709   gtk_text_layout_get_line_yrange (text_view->layout, &first_para, &y, NULL);
2710
2711   y += text_view->first_para_pixels;
2712
2713   /* Ensure h/v adj exist */
2714   get_hadjustment (text_view);
2715   get_vadjustment (text_view);
2716
2717   vadj = text_view->vadjustment;
2718   if (y > vadj->upper - vadj->page_size)
2719     y = MAX (0, vadj->upper - vadj->page_size);
2720
2721   if (y != text_view->yoffset)
2722     {
2723       vadj->value = text_view->yoffset = y;
2724       yoffset_changed = TRUE;
2725     }
2726
2727   text_view->hadjustment->page_size = SCREEN_WIDTH (text_view);
2728   text_view->hadjustment->page_increment = SCREEN_WIDTH (text_view) / 2;
2729   text_view->hadjustment->lower = 0;
2730   text_view->hadjustment->upper = MAX (SCREEN_WIDTH (text_view),
2731                                        text_view->width);
2732   gtk_signal_emit_by_name (GTK_OBJECT (text_view->hadjustment), "changed");
2733
2734   text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2735   text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) / 2;
2736   text_view->vadjustment->lower = 0;
2737   text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2738                                        text_view->height);
2739   gtk_signal_emit_by_name (GTK_OBJECT (text_view->vadjustment), "changed");
2740
2741   if (yoffset_changed)
2742     gtk_adjustment_value_changed (vadj);
2743
2744   if (text_view->first_validate_idle != 0)
2745     {
2746       /* The GTK resize loop processes all the pending exposes right
2747        * after doing the resize stuff, so the idle sizer won't have a
2748        * chance to run. So we do the work here. 
2749        */
2750
2751       g_source_remove (text_view->first_validate_idle);
2752       text_view->first_validate_idle = 0;
2753       
2754       if (!gtk_text_view_flush_scroll (text_view))
2755         gtk_text_view_validate_onscreen (text_view);
2756     }
2757
2758   /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
2759    * be invalidated
2760    */
2761   if (size_changed && GTK_WIDGET_REALIZED (widget))
2762     gdk_window_invalidate_rect (widget->window, NULL, FALSE);
2763 }
2764
2765 static void
2766 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
2767                                    GtkTextIter *iter)
2768 {
2769   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
2770                                     text_view->first_para_mark);
2771 }
2772
2773 static void
2774 gtk_text_view_validate_onscreen (GtkTextView *text_view)
2775 {
2776   GtkWidget *widget = GTK_WIDGET (text_view);
2777   
2778   DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
2779   
2780   if (SCREEN_HEIGHT (widget) > 0)
2781     {
2782       GtkTextIter first_para;
2783
2784       /* Be sure we've validated the stuff onscreen; if we
2785        * scrolled, these calls won't have any effect, because
2786        * they were called in the recursive validate_onscreen
2787        */
2788       gtk_text_view_get_first_para_iter (text_view, &first_para);
2789
2790       gtk_text_layout_validate_yrange (text_view->layout,
2791                                        &first_para,
2792                                        0,
2793                                        text_view->first_para_pixels +
2794                                        SCREEN_HEIGHT (widget));
2795     }
2796
2797   text_view->onscreen_validated = TRUE;
2798
2799   DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
2800   
2801   /* This can have the odd side effect of triggering a scroll, which should
2802    * flip "onscreen_validated" back to FALSE, but should also get us
2803    * back into this function to turn it on again.
2804    */
2805   gtk_text_view_update_adjustments (text_view);
2806
2807   g_assert (text_view->onscreen_validated);
2808 }
2809
2810 static gboolean
2811 first_validate_callback (gpointer data)
2812 {
2813   GtkTextView *text_view = data;
2814
2815   GDK_THREADS_ENTER ();
2816   
2817   /* Note that some of this code is duplicated at the end of size_allocate,
2818    * keep in sync with that.
2819    */
2820   
2821   DV(g_print(G_STRLOC"\n"));
2822
2823   /* Do this first, which means that if an "invalidate"
2824    * occurs during any of this process, a new first_validate_callback
2825    * will be installed, and we'll start again.
2826    */
2827   text_view->first_validate_idle = 0;
2828   
2829   /* be sure we have up-to-date screen size set on the
2830    * layout.
2831    */
2832   gtk_text_view_update_layout_width (text_view);
2833
2834   /* Bail out if we invalidated stuff; scrolling right away will just
2835    * confuse the issue.
2836    */
2837   if (text_view->first_validate_idle != 0)
2838     {
2839       DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
2840     }
2841   else
2842     {
2843       /* scroll to any marks, if that's pending. This can
2844        * jump us to the validation codepath used for scrolling
2845        * onscreen, if so we bail out.
2846        */
2847       if (!gtk_text_view_flush_scroll (text_view))
2848         gtk_text_view_validate_onscreen (text_view);
2849       
2850       DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
2851       
2852       g_assert (text_view->onscreen_validated);
2853       
2854     }
2855   
2856   GDK_THREADS_LEAVE ();
2857   
2858   return FALSE;
2859 }
2860
2861 static gboolean
2862 incremental_validate_callback (gpointer data)
2863 {
2864   GtkTextView *text_view = data;
2865   gboolean result = TRUE;
2866
2867   GDK_THREADS_ENTER ();
2868   
2869   DV(g_print(G_STRLOC"\n"));
2870   
2871   gtk_text_layout_validate (text_view->layout, 2000);
2872
2873   gtk_text_view_update_adjustments (text_view);
2874   
2875   if (gtk_text_layout_is_valid (text_view->layout))
2876     {
2877       text_view->incremental_validate_idle = 0;
2878       result = FALSE;
2879     }
2880
2881   GDK_THREADS_LEAVE ();
2882
2883   return result;
2884 }
2885
2886 static void
2887 gtk_text_view_invalidate (GtkTextView *text_view)
2888 {
2889   text_view->onscreen_validated = FALSE;
2890   
2891   DV(g_print(">Invalidate, onscreen_validated = FALSE ("G_STRLOC")\n"));
2892   
2893   if (!text_view->first_validate_idle)
2894     {
2895       text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
2896       DV (g_print (G_STRLOC": adding first validate idle %d\n",
2897                    text_view->first_validate_idle));
2898     }
2899       
2900   if (!text_view->incremental_validate_idle)
2901     {
2902       text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
2903       DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
2904                    text_view->incremental_validate_idle));
2905     }
2906 }
2907
2908 static void
2909 invalidated_handler (GtkTextLayout *layout,
2910                      gpointer       data)
2911 {
2912   GtkTextView *text_view;
2913
2914   text_view = GTK_TEXT_VIEW (data);
2915
2916   gtk_text_view_invalidate (text_view);
2917 }
2918
2919 static void
2920 changed_handler (GtkTextLayout     *layout,
2921                  gint               start_y,
2922                  gint               old_height,
2923                  gint               new_height,
2924                  gpointer           data)
2925 {
2926   GtkTextView *text_view;
2927   GtkWidget *widget;
2928   GdkRectangle visible_rect;
2929   GdkRectangle redraw_rect;
2930   
2931   text_view = GTK_TEXT_VIEW (data);
2932   widget = GTK_WIDGET (data);
2933   
2934   DV(g_print(">Lines Validated ("G_STRLOC")\n"));
2935
2936   if (GTK_WIDGET_REALIZED (text_view))
2937     {      
2938       gtk_text_view_get_visible_rect (text_view, &visible_rect);
2939
2940       redraw_rect.x = visible_rect.x;
2941       redraw_rect.width = visible_rect.width;
2942       redraw_rect.y = start_y;
2943
2944       if (old_height == new_height)
2945         redraw_rect.height = old_height;
2946       else
2947         redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
2948
2949       if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
2950         {
2951           /* text_window_invalidate_rect() takes buffer coordinates */
2952           text_window_invalidate_rect (text_view->text_window,
2953                                        &redraw_rect);
2954
2955           DV(g_print(" invalidated rect: %d,%d %d x %d\n",
2956                      redraw_rect.x,
2957                      redraw_rect.y,
2958                      redraw_rect.width,
2959                      redraw_rect.height));
2960           
2961           if (text_view->left_window)
2962             text_window_invalidate_rect (text_view->left_window,
2963                                          &redraw_rect);
2964           if (text_view->right_window)
2965             text_window_invalidate_rect (text_view->right_window,
2966                                          &redraw_rect);
2967           if (text_view->top_window)
2968             text_window_invalidate_rect (text_view->top_window,
2969                                          &redraw_rect);
2970           if (text_view->bottom_window)
2971             text_window_invalidate_rect (text_view->bottom_window,
2972                                          &redraw_rect);
2973
2974           gtk_text_view_update_im_spot_location (text_view);
2975         }
2976     }
2977   
2978   if (old_height != new_height)
2979     {
2980       gboolean yoffset_changed = FALSE;
2981       GSList *tmp_list;
2982       int new_first_para_top;
2983       int old_first_para_top;
2984       GtkTextIter first;
2985       
2986       /* If the bottom of the old area was above the top of the
2987        * screen, we need to scroll to keep the current top of the
2988        * screen in place.  Remember that first_para_pixels is the
2989        * position of the top of the screen in coordinates relative to
2990        * the first paragraph onscreen.
2991        *
2992        * In short we are adding the height delta of the portion of the
2993        * changed region above first_para_mark to text_view->yoffset.
2994        */
2995       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
2996                                         text_view->first_para_mark);
2997
2998       gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
2999
3000       old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3001
3002       if (new_first_para_top != old_first_para_top)
3003         {
3004           text_view->yoffset += new_first_para_top - old_first_para_top;
3005           
3006           get_vadjustment (text_view)->value = text_view->yoffset;
3007           yoffset_changed = TRUE;
3008         }
3009
3010       if (yoffset_changed)
3011         gtk_adjustment_value_changed (get_vadjustment (text_view));
3012
3013       /* FIXME be smarter about which anchored widgets we update */
3014
3015       tmp_list = text_view->children;
3016       while (tmp_list != NULL)
3017         {
3018           GtkTextViewChild *child = tmp_list->data;
3019
3020           if (child->anchor)
3021             gtk_text_view_update_child_allocation (text_view, child);
3022
3023           tmp_list = g_slist_next (tmp_list);
3024         }
3025     }
3026
3027   {
3028     GtkRequisition old_req;
3029     GtkRequisition new_req;
3030
3031     old_req = widget->requisition;
3032     gtk_widget_size_request (widget, &new_req);
3033
3034     if (old_req.width != new_req.width ||
3035         old_req.height != new_req.height)
3036       gtk_widget_queue_resize (widget);
3037   }
3038 }
3039
3040 static void
3041 gtk_text_view_realize_cursor_gc (GtkTextView *text_view)
3042 {
3043   GdkColor *cursor_color;
3044   GdkColor red = { 0, 0xffff, 0x0000, 0x0000 };
3045   
3046   if (text_view->cursor_gc)
3047     gdk_gc_unref (text_view->cursor_gc);
3048
3049   gtk_widget_style_get (GTK_WIDGET (text_view), "cursor_color", &cursor_color, NULL);
3050
3051   if (!cursor_color)
3052     cursor_color = &red;
3053
3054   text_view->cursor_gc = gdk_gc_new (text_view->text_window->bin_window);
3055   gdk_gc_set_rgb_fg_color (text_view->cursor_gc, cursor_color);
3056 }
3057
3058 static void
3059 gtk_text_view_realize (GtkWidget *widget)
3060 {
3061   GtkTextView *text_view;
3062   GdkWindowAttr attributes;
3063   gint attributes_mask;
3064   GSList *tmp_list;
3065   
3066   text_view = GTK_TEXT_VIEW (widget);
3067   GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3068
3069   attributes.window_type = GDK_WINDOW_CHILD;
3070   attributes.x = widget->allocation.x;
3071   attributes.y = widget->allocation.y;
3072   attributes.width = widget->allocation.width;
3073   attributes.height = widget->allocation.height;
3074   attributes.wclass = GDK_INPUT_OUTPUT;
3075   attributes.visual = gtk_widget_get_visual (widget);
3076   attributes.colormap = gtk_widget_get_colormap (widget);
3077   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3078
3079   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3080
3081   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3082                                    &attributes, attributes_mask);
3083   gdk_window_set_user_data (widget->window, widget);
3084
3085   /* must come before text_window_realize calls */
3086   widget->style = gtk_style_attach (widget->style, widget->window);
3087
3088   gdk_window_set_background (widget->window,
3089                              &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3090
3091   text_window_realize (text_view->text_window, widget->window);
3092
3093   if (text_view->left_window)
3094     text_window_realize (text_view->left_window,
3095                          widget->window);
3096
3097   if (text_view->top_window)
3098     text_window_realize (text_view->top_window,
3099                          widget->window);
3100
3101   if (text_view->right_window)
3102     text_window_realize (text_view->right_window,
3103                          widget->window);
3104
3105   if (text_view->bottom_window)
3106     text_window_realize (text_view->bottom_window,
3107                          widget->window);
3108
3109   gtk_text_view_realize_cursor_gc (text_view);
3110
3111   gtk_text_view_ensure_layout (text_view);
3112
3113   if (text_view->buffer)
3114     gtk_text_buffer_add_selection_clipboard (text_view->buffer,
3115                                              gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3116
3117   tmp_list = text_view->children;
3118   while (tmp_list != NULL)
3119     {
3120       GtkTextViewChild *vc = tmp_list->data;
3121       
3122       text_view_child_set_parent_window (text_view, vc);
3123       
3124       tmp_list = tmp_list->next;
3125     }
3126 }
3127
3128 static void
3129 gtk_text_view_unrealize (GtkWidget *widget)
3130 {
3131   GtkTextView *text_view;
3132   
3133   text_view = GTK_TEXT_VIEW (widget);
3134
3135   if (text_view->buffer)
3136     gtk_text_buffer_remove_selection_clipboard (text_view->buffer,
3137                                                 gtk_clipboard_get (GDK_SELECTION_PRIMARY));
3138
3139   if (text_view->cursor_gc)
3140     {
3141       gdk_gc_unref (text_view->cursor_gc);
3142       text_view->cursor_gc = NULL;
3143     }
3144
3145   gtk_text_view_remove_validate_idles (text_view);
3146
3147   if (text_view->popup_menu)
3148     {
3149       gtk_widget_destroy (text_view->popup_menu);
3150       text_view->popup_menu = NULL;
3151     }
3152
3153   text_window_unrealize (text_view->text_window);
3154
3155   if (text_view->left_window)
3156     text_window_unrealize (text_view->left_window);
3157
3158   if (text_view->top_window)
3159     text_window_unrealize (text_view->top_window);
3160
3161   if (text_view->right_window)
3162     text_window_unrealize (text_view->right_window);
3163
3164   if (text_view->bottom_window)
3165     text_window_unrealize (text_view->bottom_window);
3166
3167   gtk_text_view_destroy_layout (text_view);
3168   
3169   (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3170 }
3171
3172 static void
3173 gtk_text_view_style_set (GtkWidget *widget,
3174                          GtkStyle  *previous_style)
3175 {
3176   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3177
3178   if (GTK_WIDGET_REALIZED (widget))
3179     {
3180       gdk_window_set_background (widget->window,
3181                                  &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3182
3183       gdk_window_set_background (text_view->text_window->bin_window,
3184                                  &widget->style->base[GTK_WIDGET_STATE (widget)]);
3185
3186       if (text_view->left_window)
3187         gdk_window_set_background (text_view->left_window->bin_window,
3188                                    &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3189       if (text_view->right_window)
3190         gdk_window_set_background (text_view->right_window->bin_window,
3191                                    &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3192
3193       if (text_view->top_window)
3194         gdk_window_set_background (text_view->top_window->bin_window,
3195                                    &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3196
3197       if (text_view->bottom_window)
3198         gdk_window_set_background (text_view->bottom_window->bin_window,
3199                                    &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3200       
3201       gtk_text_view_realize_cursor_gc (text_view);
3202     }
3203
3204   if (text_view->layout && previous_style)
3205     {
3206       gtk_text_view_set_attributes_from_style (text_view,
3207                                                text_view->layout->default_style,
3208                                                widget->style);
3209       gtk_text_layout_default_style_changed (text_view->layout);
3210     }
3211 }
3212
3213 static void
3214 gtk_text_view_direction_changed (GtkWidget        *widget,
3215                                  GtkTextDirection  previous_direction)
3216 {
3217   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3218
3219   if (text_view->layout)
3220     {
3221       text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3222       gtk_text_layout_default_style_changed (text_view->layout);
3223     }
3224 }
3225
3226 /*
3227  * Events
3228  */
3229
3230 static gboolean
3231 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3232 {
3233   if (event)
3234     switch (event->type)
3235       {
3236       case GDK_MOTION_NOTIFY:
3237         *x = event->motion.x;
3238         *y = event->motion.y;
3239         return TRUE;
3240         break;
3241
3242       case GDK_BUTTON_PRESS:
3243       case GDK_2BUTTON_PRESS:
3244       case GDK_3BUTTON_PRESS:
3245       case GDK_BUTTON_RELEASE:
3246         *x = event->button.x;
3247         *y = event->button.y;
3248         return TRUE;
3249         break;
3250
3251       case GDK_KEY_PRESS:
3252       case GDK_KEY_RELEASE:
3253       case GDK_ENTER_NOTIFY:
3254       case GDK_LEAVE_NOTIFY:
3255       case GDK_PROPERTY_NOTIFY:
3256       case GDK_SELECTION_CLEAR:
3257       case GDK_SELECTION_REQUEST:
3258       case GDK_SELECTION_NOTIFY:
3259       case GDK_PROXIMITY_IN:
3260       case GDK_PROXIMITY_OUT:
3261       case GDK_DRAG_ENTER:
3262       case GDK_DRAG_LEAVE:
3263       case GDK_DRAG_MOTION:
3264       case GDK_DRAG_STATUS:
3265       case GDK_DROP_START:
3266       case GDK_DROP_FINISHED:
3267       default:
3268         return FALSE;
3269         break;
3270       }
3271
3272   return FALSE;
3273 }
3274
3275 static gint
3276 emit_event_on_tags (GtkWidget   *widget,
3277                     GdkEvent    *event,
3278                     GtkTextIter *iter)
3279 {
3280   GSList *tags;
3281   GSList *tmp;
3282   gboolean retval = FALSE;
3283   GtkTextView *text_view;
3284
3285   text_view = GTK_TEXT_VIEW (widget);
3286
3287   tags = gtk_text_iter_get_tags (iter);
3288
3289   tmp = tags;
3290   while (tmp != NULL)
3291     {
3292       GtkTextTag *tag = tmp->data;
3293
3294       if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3295         {
3296           retval = TRUE;
3297           break;
3298         }
3299
3300       tmp = g_slist_next (tmp);
3301     }
3302
3303   g_slist_free (tags);
3304
3305   return retval;
3306 }
3307
3308 static gint
3309 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3310 {
3311   GtkTextView *text_view;
3312   gint x = 0, y = 0;
3313
3314   text_view = GTK_TEXT_VIEW (widget);
3315
3316   if (text_view->layout == NULL ||
3317       get_buffer (text_view) == NULL)
3318     return FALSE;
3319
3320   if (event->any.window != text_view->text_window->bin_window)
3321     return FALSE;
3322
3323   if (get_event_coordinates (event, &x, &y))
3324     {
3325       GtkTextIter iter;
3326
3327       x += text_view->xoffset;
3328       y += text_view->yoffset;
3329
3330       /* FIXME this is slow and we do it twice per event.
3331        * My favorite solution is to have GtkTextLayout cache
3332        * the last couple lookups.
3333        */
3334       gtk_text_layout_get_iter_at_pixel (text_view->layout,
3335                                          &iter,
3336                                          x, y);
3337
3338       return emit_event_on_tags (widget, event, &iter);
3339     }
3340   else if (event->type == GDK_KEY_PRESS ||
3341            event->type == GDK_KEY_RELEASE)
3342     {
3343       GtkTextMark *insert;
3344       GtkTextIter iter;
3345
3346       insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3347                                          "insert");
3348
3349       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3350
3351       return emit_event_on_tags (widget, event, &iter);
3352     }
3353   else
3354     return FALSE;
3355 }
3356
3357 static gint
3358 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3359 {
3360   gboolean retval = FALSE;
3361   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3362   GtkTextMark *insert;
3363   GtkTextIter iter;
3364   
3365   if (text_view->layout == NULL ||
3366       get_buffer (text_view) == NULL)
3367     return FALSE;
3368
3369   insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3370   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3371   if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3372       gtk_im_context_filter_keypress (text_view->im_context, event))
3373     {
3374       text_view->need_im_reset = TRUE;
3375       retval = TRUE;
3376     }
3377   else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3378            GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3379     retval = TRUE;
3380   else if (event->keyval == GDK_Return ||
3381            event->keyval == GDK_KP_Enter)
3382     {
3383       gtk_text_view_commit_text (text_view, "\n");
3384       retval = TRUE;
3385     }
3386   /* Pass through Tab as literal tab, unless Control is held down */
3387   else if ((event->keyval == GDK_Tab ||
3388             event->keyval == GDK_KP_Tab ||
3389             event->keyval == GDK_ISO_Left_Tab) &&
3390            !(event->state & GDK_CONTROL_MASK))
3391     {
3392       gtk_text_view_commit_text (text_view, "\t");
3393       retval = TRUE;
3394     }
3395   else
3396     retval = FALSE;
3397
3398   gtk_text_view_pend_cursor_blink (text_view);
3399
3400   return retval;
3401 }
3402
3403 static gint
3404 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3405 {
3406   return FALSE;
3407 }
3408
3409 static gint
3410 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3411 {
3412   GtkTextView *text_view;
3413
3414   text_view = GTK_TEXT_VIEW (widget);
3415
3416   text_view->disable_scroll_on_focus = TRUE;
3417   gtk_widget_grab_focus (widget);
3418   text_view->disable_scroll_on_focus = FALSE;
3419
3420   if (event->window != text_view->text_window->bin_window)
3421     {
3422       /* Remove selection if any. */
3423       gtk_text_view_unselect (text_view);
3424       return FALSE;
3425     }
3426
3427 #if 0
3428   /* debug hack */
3429   if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3430     _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3431   else if (event->button == 3)
3432     gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3433 #endif
3434
3435   if (event->type == GDK_BUTTON_PRESS)
3436     {
3437       gtk_text_view_reset_im_context (text_view);
3438
3439       if (event->button == 1)
3440         {
3441           /* If we're in the selection, start a drag copy/move of the
3442            * selection; otherwise, start creating a new selection.
3443            */
3444           GtkTextIter iter;
3445           GtkTextIter start, end;
3446
3447           gtk_text_layout_get_iter_at_pixel (text_view->layout,
3448                                              &iter,
3449                                              event->x + text_view->xoffset,
3450                                              event->y + text_view->yoffset);
3451
3452           if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3453                                                     &start, &end) &&
3454               gtk_text_iter_in_range (&iter, &start, &end))
3455             {
3456               text_view->drag_start_x = event->x;
3457               text_view->drag_start_y = event->y;
3458               text_view->pending_place_cursor_button = event->button;
3459             }
3460           else
3461             {
3462               gtk_text_view_start_selection_drag (text_view, &iter, event);
3463             }
3464
3465           return TRUE;
3466         }
3467       else if (event->button == 2)
3468         {
3469           GtkTextIter iter;
3470
3471           gtk_text_layout_get_iter_at_pixel (text_view->layout,
3472                                              &iter,
3473                                              event->x + text_view->xoffset,
3474                                              event->y + text_view->yoffset);
3475
3476           gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3477                                            gtk_clipboard_get (GDK_SELECTION_PRIMARY),
3478                                            &iter,
3479                                            text_view->editable);
3480           return TRUE;
3481         }
3482       else if (event->button == 3)
3483         {
3484           gtk_text_view_do_popup (text_view, event);
3485         }
3486     }
3487   else if ((event->type == GDK_2BUTTON_PRESS ||
3488             event->type == GDK_3BUTTON_PRESS) &&
3489            event->button == 1)
3490     {
3491       GtkTextIter start, end;
3492
3493       /* End the selection drag, otherwise we'd clear the new
3494        * word/line selection on button release
3495        */
3496       gtk_text_view_end_selection_drag (text_view, event);
3497
3498       gtk_text_layout_get_iter_at_pixel (text_view->layout,
3499                                          &start,
3500                                          event->x + text_view->xoffset,
3501                                          event->y + text_view->yoffset); 
3502
3503       end = start;
3504       
3505       if (event->type == GDK_2BUTTON_PRESS)
3506         {
3507           if (gtk_text_iter_inside_word (&start))
3508             {
3509               if (!gtk_text_iter_starts_word (&start))
3510                 gtk_text_iter_backward_word_start (&start);
3511               
3512               if (!gtk_text_iter_ends_word (&end))
3513                 gtk_text_iter_forward_word_end (&end);
3514             }
3515         }
3516       else if (event->type == GDK_3BUTTON_PRESS)
3517         {
3518           if (gtk_text_view_starts_display_line (text_view, &start))
3519             {
3520               /* If on a display line boundary, we assume the user
3521                * clicked off the end of a line and we therefore select
3522                * the line before the boundary.
3523                */
3524               gtk_text_view_backward_display_line_start (text_view, &start);
3525             }
3526           else
3527             {
3528               /* start isn't on the start of a line, so we move it to the
3529                * start, and move end to the end unless it's already there.
3530                */
3531               gtk_text_view_backward_display_line_start (text_view, &start);
3532
3533               if (!gtk_text_view_starts_display_line (text_view, &end))
3534                 gtk_text_view_forward_display_line_end (text_view, &end);
3535             }
3536         }
3537
3538       gtk_text_buffer_move_mark (get_buffer (text_view),
3539                                  gtk_text_buffer_get_selection_bound (get_buffer (text_view)),
3540                                  &start);
3541       gtk_text_buffer_move_mark (get_buffer (text_view),
3542                                  gtk_text_buffer_get_insert (get_buffer (text_view)),
3543                                  &end);
3544
3545       text_view->just_selected_element = TRUE;
3546       
3547       return TRUE;
3548     }
3549   
3550   return FALSE;
3551 }
3552
3553 static gint
3554 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3555 {
3556   GtkTextView *text_view;
3557
3558   text_view = GTK_TEXT_VIEW (widget);
3559
3560   if (event->window != text_view->text_window->bin_window)
3561     return FALSE;
3562
3563   if (event->button == 1)
3564     {
3565       if (text_view->drag_start_x >= 0)
3566         {
3567           text_view->drag_start_x = -1;
3568           text_view->drag_start_y = -1;
3569         }
3570
3571       if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3572         return TRUE;
3573       else if (text_view->just_selected_element)
3574         {
3575           text_view->just_selected_element = FALSE;
3576           return FALSE;
3577         }
3578       else if (text_view->pending_place_cursor_button == event->button)
3579         {
3580           GtkTextIter iter;
3581
3582           /* Unselect everything; we clicked inside selection, but
3583            * didn't move by the drag threshold, so just clear selection
3584            * and place cursor.
3585            */
3586           gtk_text_layout_get_iter_at_pixel (text_view->layout,
3587                                              &iter,
3588                                              event->x + text_view->xoffset,
3589                                              event->y + text_view->yoffset);
3590
3591           gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3592
3593           text_view->pending_place_cursor_button = 0;
3594           
3595           return FALSE;
3596         }
3597     }
3598
3599   return FALSE;
3600 }
3601
3602 static void
3603 keymap_direction_changed (GdkKeymap   *keymap,
3604                           GtkTextView *text_view)
3605 {
3606   gtk_text_view_check_keymap_direction (text_view);
3607 }
3608
3609 static gint
3610 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3611 {
3612   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3613
3614   GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
3615   gtk_widget_queue_draw (widget);
3616
3617   DV(g_print (G_STRLOC": focus_in_event\n"));
3618   
3619   if (text_view->cursor_visible && text_view->layout)
3620     {
3621       gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3622       gtk_text_view_check_cursor_blink (text_view);
3623     }
3624
3625   g_signal_connect (gdk_keymap_get_default (),
3626                     "direction_changed",
3627                     G_CALLBACK (keymap_direction_changed), text_view);
3628   gtk_text_view_check_keymap_direction (text_view);
3629   
3630   text_view->need_im_reset = TRUE;
3631   gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
3632
3633   return FALSE;
3634 }
3635
3636 static gint
3637 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
3638 {
3639   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3640
3641   GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
3642   gtk_widget_queue_draw (widget);
3643
3644   DV(g_print (G_STRLOC": focus_out_event\n"));
3645   
3646   if (text_view->cursor_visible && text_view->layout)
3647     {
3648       gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
3649       gtk_text_view_check_cursor_blink (text_view);
3650     }
3651
3652   g_signal_handlers_disconnect_by_func (gdk_keymap_get_default (),
3653                                         keymap_direction_changed,
3654                                         text_view);
3655
3656   text_view->need_im_reset = TRUE;
3657   gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
3658
3659   return FALSE;
3660 }
3661
3662 static gint
3663 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
3664 {
3665   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3666
3667   if (event->window == text_view->text_window->bin_window &&
3668       text_view->drag_start_x >= 0)
3669     {
3670       gint x, y;
3671
3672       gdk_window_get_pointer (text_view->text_window->bin_window,
3673                               &x, &y, NULL);
3674
3675       if (gtk_drag_check_threshold (widget,
3676                                     text_view->drag_start_x, 
3677                                     text_view->drag_start_y,
3678                                     x, y))
3679         {
3680           GtkTextIter iter;
3681           gint buffer_x, buffer_y;
3682
3683           gtk_text_view_window_to_buffer_coords (text_view,
3684                                                  GTK_TEXT_WINDOW_TEXT,
3685                                                  text_view->drag_start_x,
3686                                                  text_view->drag_start_y,
3687                                                  &buffer_x,
3688                                                  &buffer_y);
3689
3690           gtk_text_layout_get_iter_at_pixel (text_view->layout,
3691                                              &iter,
3692                                              buffer_x, buffer_y);
3693
3694           gtk_text_view_start_selection_dnd (text_view, &iter, event);
3695           return TRUE;
3696         }
3697     }
3698
3699   return FALSE;
3700 }
3701
3702 static void
3703 gtk_text_view_paint (GtkWidget      *widget,
3704                      GdkRectangle   *area,
3705                      GdkEventExpose *event)
3706 {
3707   GtkTextView *text_view;
3708   GList *child_exposes;
3709   GList *tmp_list;
3710   
3711   text_view = GTK_TEXT_VIEW (widget);
3712
3713   g_return_if_fail (text_view->layout != NULL);
3714   g_return_if_fail (text_view->xoffset >= 0);
3715   g_return_if_fail (text_view->yoffset >= 0);
3716   
3717   DV (g_print (G_STRLOC": first_validate_idle: %d\n",
3718                text_view->first_validate_idle));
3719   
3720   if (!text_view->onscreen_validated)
3721     {
3722       g_warning (G_STRLOC ": somehow some text lines were modified or scrolling occurred since the last validation of lines on the screen - this is a known bug, no need to report.");
3723       G_BREAKPOINT ();
3724     }
3725   
3726 #if 0
3727   printf ("painting %d,%d  %d x %d\n",
3728           area->x, area->y,
3729           area->width, area->height);
3730 #endif
3731
3732   child_exposes = NULL;
3733   gtk_text_layout_draw (text_view->layout,
3734                         widget,
3735                         text_view->text_window->bin_window,
3736                         text_view->cursor_gc,
3737                         text_view->xoffset,
3738                         text_view->yoffset,
3739                         area->x, area->y,
3740                         area->width, area->height,
3741                         &child_exposes);
3742
3743   tmp_list = child_exposes;
3744   while (tmp_list != NULL)
3745     {
3746       GtkWidget *child = tmp_list->data;
3747       
3748       gtk_container_propagate_expose (GTK_CONTAINER (text_view),
3749                                       child,
3750                                       event);
3751
3752       g_object_unref (G_OBJECT (child));
3753       
3754       tmp_list = tmp_list->next;
3755     }
3756
3757   g_list_free (child_exposes);
3758 }
3759
3760 static gint
3761 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
3762 {  
3763   if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
3764                                                  GTK_TEXT_WINDOW_TEXT))
3765     {
3766       DV(g_print (">Exposed ("G_STRLOC")\n"));
3767       gtk_text_view_paint (widget, &event->area, event);
3768     }
3769
3770   if (event->window == widget->window)
3771     gtk_text_view_draw_focus (widget);
3772
3773   return TRUE;
3774 }
3775
3776 static void
3777 gtk_text_view_draw_focus (GtkWidget *widget)
3778 {
3779   gboolean interior_focus;
3780
3781   /* We clear the focus if we are in interior focus mode. */
3782   gtk_widget_style_get (widget, "interior_focus", &interior_focus, NULL);
3783   
3784   if (GTK_WIDGET_DRAWABLE (widget))
3785     {
3786       if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
3787         {          
3788           gtk_paint_focus (widget->style, widget->window,
3789                            NULL, widget, "textview",
3790                            0, 0,
3791                            widget->allocation.width - 1,
3792                            widget->allocation.height - 1);
3793         }
3794       else
3795         {
3796           gdk_window_clear (widget->window);
3797         }
3798     }
3799 }
3800
3801 static void
3802 gtk_text_view_grab_focus (GtkWidget *widget)
3803 {
3804   GtkTextView *text_view;
3805
3806   text_view = GTK_TEXT_VIEW (widget);
3807   
3808   GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
3809
3810   if (!text_view->disable_scroll_on_focus)
3811     gtk_text_view_scroll_mark_onscreen (text_view,
3812                                         gtk_text_buffer_get_mark (get_buffer (text_view),
3813                                                                   "insert"));
3814 }
3815
3816 /*
3817  * Container
3818  */
3819
3820 static void
3821 gtk_text_view_add (GtkContainer *container,
3822                    GtkWidget    *child)
3823 {
3824   g_return_if_fail (GTK_IS_TEXT_VIEW (container));
3825   g_return_if_fail (GTK_IS_WIDGET (child));
3826
3827   /* This is pretty random. */
3828   gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
3829                                      child,
3830                                      GTK_TEXT_WINDOW_WIDGET,
3831                                      0, 0);
3832 }
3833
3834 static void
3835 gtk_text_view_remove (GtkContainer *container,
3836                       GtkWidget    *child)
3837 {
3838   GSList *iter;
3839   GtkTextView *text_view;
3840   GtkTextViewChild *vc;
3841
3842   g_return_if_fail (GTK_IS_TEXT_VIEW (container));
3843   g_return_if_fail (GTK_IS_WIDGET (child));
3844   g_return_if_fail (child->parent == (GtkWidget*) container);
3845
3846   text_view = GTK_TEXT_VIEW (container);
3847
3848   vc = NULL;
3849   iter = text_view->children;
3850
3851   while (iter != NULL)
3852     {
3853       vc = iter->data;
3854
3855       if (vc->widget == child)
3856         break;
3857
3858       iter = g_slist_next (iter);
3859     }
3860
3861   g_assert (iter != NULL); /* be sure we had the child in the list */
3862
3863   text_view->children = g_slist_remove (text_view->children, vc);
3864
3865   gtk_widget_unparent (vc->widget);
3866
3867   text_view_child_free (vc);
3868 }
3869
3870 static void
3871 gtk_text_view_forall (GtkContainer *container,
3872                       gboolean      include_internals,
3873                       GtkCallback   callback,
3874                       gpointer      callback_data)
3875 {
3876   GSList *iter;
3877   GtkTextView *text_view;
3878   GSList *copy;
3879
3880   g_return_if_fail (GTK_IS_TEXT_VIEW (container));
3881   g_return_if_fail (callback != NULL);
3882
3883   text_view = GTK_TEXT_VIEW (container);
3884
3885   copy = g_slist_copy (text_view->children);
3886   iter = copy;
3887
3888   while (iter != NULL)
3889     {
3890       GtkTextViewChild *vc = iter->data;
3891
3892       (* callback) (vc->widget, callback_data);
3893
3894       iter = g_slist_next (iter);
3895     }
3896
3897   g_slist_free (copy);
3898 }
3899
3900 #define CURSOR_ON_MULTIPLIER 0.66
3901 #define CURSOR_OFF_MULTIPLIER 0.34
3902 #define CURSOR_PEND_MULTIPLIER 1.0
3903
3904 static gboolean
3905 cursor_blinks (GtkTextView *text_view)
3906 {
3907   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
3908   gboolean blink;
3909
3910 #ifdef DEBUG_VALIDATION_AND_SCROLLING
3911   return FALSE;
3912 #endif
3913   if (gtk_debug_flags & GTK_DEBUG_UPDATES)
3914     return FALSE;
3915   
3916   g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
3917   return blink;
3918 }
3919
3920 static gint
3921 get_cursor_time (GtkTextView *text_view)
3922 {
3923   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
3924   gint time;
3925
3926   g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
3927
3928   return time;
3929 }
3930
3931 /*
3932  * Blink!
3933  */
3934
3935 static gint
3936 blink_cb (gpointer data)
3937 {
3938   GtkTextView *text_view;
3939   gboolean visible;
3940
3941   GDK_THREADS_ENTER ();
3942
3943   text_view = GTK_TEXT_VIEW (data);
3944   
3945   g_assert (text_view->layout);
3946   g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
3947   g_assert (text_view->cursor_visible);
3948
3949   visible = gtk_text_layout_get_cursor_visible (text_view->layout);
3950
3951   if (visible)
3952     text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
3953                                                 blink_cb,
3954                                                 text_view);
3955   else
3956     text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
3957                                                 blink_cb,
3958                                                 text_view);
3959   
3960   gtk_text_layout_set_cursor_visible (text_view->layout,
3961                                       !visible);
3962
3963   GDK_THREADS_LEAVE ();
3964
3965   /* Remove ourselves */
3966   return FALSE;
3967 }
3968
3969
3970 static void
3971 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
3972 {
3973   if (text_view->blink_timeout)  
3974     { 
3975       gtk_timeout_remove (text_view->blink_timeout);
3976       text_view->blink_timeout = 0;
3977     }
3978 }
3979
3980 static void
3981 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
3982 {
3983   if (text_view->layout != NULL &&
3984       text_view->cursor_visible &&
3985       GTK_WIDGET_HAS_FOCUS (text_view))
3986     {
3987       if (cursor_blinks (text_view))
3988         {
3989           if (text_view->blink_timeout == 0)
3990             {
3991               gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
3992               
3993               text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
3994                                                           blink_cb,
3995                                                           text_view);
3996             }
3997         }
3998       else
3999         gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);   
4000     }
4001   else
4002     {
4003       gtk_text_view_stop_cursor_blink (text_view);
4004     }
4005 }
4006
4007 static void
4008 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4009 {
4010   if (text_view->layout != NULL &&
4011       text_view->cursor_visible &&
4012       GTK_WIDGET_HAS_FOCUS (text_view) &&
4013       cursor_blinks (text_view))
4014     {
4015       if (text_view->blink_timeout != 0)
4016         {
4017           gtk_timeout_remove (text_view->blink_timeout);
4018           text_view->blink_timeout = 0;
4019         }
4020       
4021       gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4022       
4023       text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4024                                                   blink_cb,
4025                                                   text_view);
4026     }
4027 }
4028
4029
4030 /*
4031  * Key binding handlers
4032  */
4033
4034 static void
4035 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4036                                   GtkTextIter *newplace,
4037                                   gint         count)
4038 {
4039   while (count < 0)
4040     {
4041       gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4042       count++;
4043     }
4044
4045   while (count > 0)
4046     {
4047       gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4048       count--;
4049     }
4050 }
4051
4052 static void
4053 gtk_text_view_move_cursor (GtkTextView     *text_view,
4054                            GtkMovementStep  step,
4055                            gint             count,
4056                            gboolean         extend_selection)
4057 {
4058   GtkTextIter insert;
4059   GtkTextIter newplace;
4060
4061   gint cursor_x_pos = 0;
4062
4063   gtk_text_view_reset_im_context (text_view);
4064
4065   if (step == GTK_MOVEMENT_PAGES)
4066     {
4067       gtk_text_view_scroll_pages (text_view, count);
4068       gtk_text_view_pend_cursor_blink (text_view);
4069       return;
4070     }
4071
4072   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4073                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4074                                                               "insert"));
4075   newplace = insert;
4076
4077   if (step == GTK_MOVEMENT_DISPLAY_LINES)
4078     gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4079
4080   switch (step)
4081     {
4082     case GTK_MOVEMENT_LOGICAL_POSITIONS:
4083       gtk_text_iter_forward_cursor_positions (&newplace, count);
4084       break;
4085
4086     case GTK_MOVEMENT_VISUAL_POSITIONS:
4087       gtk_text_layout_move_iter_visually (text_view->layout,
4088                                           &newplace, count);
4089       break;
4090
4091     case GTK_MOVEMENT_WORDS:
4092       if (count < 0)
4093         gtk_text_iter_backward_word_starts (&newplace, -count);
4094       else if (count > 0)
4095         gtk_text_iter_forward_word_ends (&newplace, count);
4096       break;
4097
4098     case GTK_MOVEMENT_DISPLAY_LINES:
4099       gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4100       gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4101       break;
4102
4103     case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4104       if (count > 1)
4105         gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4106       else if (count < -1)
4107         gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4108
4109       if (count != 0)
4110         gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4111       break;
4112
4113     case GTK_MOVEMENT_PARAGRAPHS:
4114       gtk_text_iter_forward_lines (&newplace, count);
4115       gtk_text_iter_set_line_offset (&newplace, 0);
4116       break;
4117
4118     case GTK_MOVEMENT_PARAGRAPH_ENDS:
4119       if (count > 0)
4120         {
4121           if (!gtk_text_iter_ends_line (&newplace))
4122             gtk_text_iter_forward_to_line_end (&newplace);
4123         }
4124       else if (count < 0)
4125         {
4126           gtk_text_iter_set_line_offset (&newplace, 0);
4127         }
4128       break;
4129
4130     case GTK_MOVEMENT_BUFFER_ENDS:
4131       if (count > 0)
4132         gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4133       else if (count < 0)
4134         gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4135       break;
4136
4137     default:
4138       break;
4139     }
4140
4141   if (!gtk_text_iter_equal (&insert, &newplace))
4142     {
4143       if (extend_selection)
4144         gtk_text_buffer_move_mark (get_buffer (text_view),
4145                                    gtk_text_buffer_get_mark (get_buffer (text_view),
4146                                                              "insert"),
4147                                    &newplace);
4148       else
4149         gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4150
4151       DV(g_print (G_STRLOC": scrolling onscreen\n"));
4152       gtk_text_view_scroll_mark_onscreen (text_view,
4153                                           gtk_text_buffer_get_mark (get_buffer (text_view),
4154                                                                     "insert"));
4155
4156       if (step == GTK_MOVEMENT_DISPLAY_LINES)
4157         {
4158           gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4159         }
4160     }
4161
4162   gtk_text_view_pend_cursor_blink (text_view);
4163 }
4164
4165 static void
4166 gtk_text_view_set_anchor (GtkTextView *text_view)
4167 {
4168   GtkTextIter insert;
4169
4170   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4171                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4172                                                               "insert"));
4173
4174   gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4175 }
4176
4177 static void
4178 gtk_text_view_scroll_pages (GtkTextView *text_view,
4179                             gint         count)
4180 {
4181   gdouble newval;
4182   GtkAdjustment *adj;
4183   gint cursor_x_pos, cursor_y_pos;
4184   GtkTextIter new_insert;
4185   GtkTextIter anchor;
4186   gint y0, y1;
4187
4188   g_return_if_fail (text_view->vadjustment != NULL);
4189
4190   adj = text_view->vadjustment;
4191
4192   /* Validate the region that will be brought into view by the cursor motion
4193    */
4194   if (count < 0)
4195     {
4196       gtk_text_view_get_first_para_iter (text_view, &anchor);
4197       y0 = adj->page_size;
4198       y1 = adj->page_size + count * adj->page_increment;
4199     }
4200   else
4201     {
4202       gtk_text_view_get_first_para_iter (text_view, &anchor);
4203       y0 = count * adj->page_increment + adj->page_size;
4204       y1 = 0;
4205     }
4206
4207   gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4208   /* FIXME do we need to update the adjustment ranges here? */
4209   
4210   gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4211
4212   newval = adj->value;
4213
4214   newval += count * adj->page_increment;
4215
4216   cursor_y_pos += newval - adj->value;
4217   set_adjustment_clamped (adj, newval);
4218
4219   gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4220   clamp_iter_onscreen (text_view, &new_insert);
4221   gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4222
4223   gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4224
4225   /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4226    * only guarantees 1 pixel onscreen.
4227    */
4228   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4229   gtk_text_view_scroll_mark_onscreen (text_view,
4230                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4231                                                                 "insert"));
4232 }
4233
4234 static gboolean
4235 whitespace (gunichar ch, gpointer user_data)
4236 {
4237   return (ch == ' ' || ch == '\t');
4238 }
4239
4240 static gboolean
4241 not_whitespace (gunichar ch, gpointer user_data)
4242 {
4243   return !whitespace (ch, user_data);
4244 }
4245
4246 static gboolean
4247 find_whitepace_region (const GtkTextIter *center,
4248                        GtkTextIter *start, GtkTextIter *end)
4249 {
4250   *start = *center;
4251   *end = *center;
4252
4253   if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4254     gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4255   if (whitespace (gtk_text_iter_get_char (end), NULL))
4256     gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4257
4258   return !gtk_text_iter_equal (start, end);
4259 }
4260
4261 static void
4262 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4263                                 const gchar *str)
4264 {
4265   gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4266                                                 text_view->editable);
4267 }
4268
4269 static void
4270 gtk_text_view_delete_from_cursor (GtkTextView   *text_view,
4271                                   GtkDeleteType  type,
4272                                   gint           count)
4273 {
4274   GtkTextIter insert;
4275   GtkTextIter start;
4276   GtkTextIter end;
4277   gboolean leave_one = FALSE;
4278
4279   gtk_text_view_reset_im_context (text_view);
4280
4281   if (type == GTK_DELETE_CHARS)
4282     {
4283       /* Char delete deletes the selection, if one exists */
4284       if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4285                                             text_view->editable))
4286         return;
4287     }
4288
4289   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4290                                     &insert,
4291                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4292                                                               "insert"));
4293
4294   start = insert;
4295   end = insert;
4296
4297   switch (type)
4298     {
4299     case GTK_DELETE_CHARS:
4300       gtk_text_iter_forward_cursor_positions (&end, count);
4301       break;
4302
4303     case GTK_DELETE_WORD_ENDS:
4304       if (count > 0)
4305         gtk_text_iter_forward_word_ends (&end, count);
4306       else if (count < 0)
4307         gtk_text_iter_backward_word_starts (&start, 0 - count);
4308       break;
4309
4310     case GTK_DELETE_WORDS:
4311       break;
4312
4313     case GTK_DELETE_DISPLAY_LINE_ENDS:
4314       break;
4315
4316     case GTK_DELETE_DISPLAY_LINES:
4317       break;
4318
4319     case GTK_DELETE_PARAGRAPH_ENDS:
4320       /* If we're already at a newline, we need to
4321        * simply delete that newline, instead of
4322        * moving to the next one.
4323        */
4324       if (gtk_text_iter_ends_line (&end))
4325         {
4326           gtk_text_iter_forward_line (&end);
4327           --count;
4328         }
4329
4330       while (count > 0)
4331         {
4332           if (!gtk_text_iter_forward_to_line_end (&end))
4333             break;
4334
4335           --count;
4336         }
4337
4338       /* FIXME figure out what a negative count means
4339          and support that */
4340       break;
4341
4342     case GTK_DELETE_PARAGRAPHS:
4343       if (count > 0)
4344         {
4345           gtk_text_iter_set_line_offset (&start, 0);
4346           gtk_text_iter_forward_to_line_end (&end);
4347
4348           /* Do the lines beyond the first. */
4349           while (count > 1)
4350             {
4351               gtk_text_iter_forward_to_line_end (&end);
4352
4353               --count;
4354             }
4355         }
4356
4357       /* FIXME negative count? */
4358
4359       break;
4360
4361     case GTK_DELETE_WHITESPACE:
4362       {
4363         find_whitepace_region (&insert, &start, &end);
4364       }
4365       break;
4366
4367     default:
4368       break;
4369     }
4370
4371   if (!gtk_text_iter_equal (&start, &end))
4372     {
4373       gtk_text_buffer_begin_user_action (get_buffer (text_view));
4374
4375       if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4376                                               text_view->editable))
4377         {
4378           if (leave_one)
4379             gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4380                                                           " ", 1,
4381                                                           text_view->editable);
4382         }
4383
4384       gtk_text_buffer_end_user_action (get_buffer (text_view));
4385
4386       DV(g_print (G_STRLOC": scrolling onscreen\n"));
4387       gtk_text_view_scroll_mark_onscreen (text_view,
4388                                           gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4389     }
4390 }
4391
4392 static void
4393 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4394 {
4395   gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4396                                  gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4397                                  text_view->editable);
4398   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4399   gtk_text_view_scroll_mark_onscreen (text_view,
4400                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4401                                                                 "insert"));
4402 }
4403
4404 static void
4405 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4406 {
4407   gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4408                                   gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
4409   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4410   gtk_text_view_scroll_mark_onscreen (text_view,
4411                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4412                                                                 "insert"));
4413 }
4414
4415 static void
4416 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4417 {
4418   gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4419                                    gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4420                                    NULL,
4421                                    text_view->editable);
4422   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4423   gtk_text_view_scroll_mark_onscreen (text_view,
4424                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4425                                                                 "insert"));
4426 }
4427
4428 static void
4429 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4430 {
4431   text_view->overwrite_mode = !text_view->overwrite_mode;
4432 }
4433
4434 /*
4435  * Selections
4436  */
4437
4438 static void
4439 gtk_text_view_unselect (GtkTextView *text_view)
4440 {
4441   GtkTextIter insert;
4442
4443   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4444                                     &insert,
4445                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4446                                                               "insert"));
4447
4448   gtk_text_buffer_move_mark (get_buffer (text_view),
4449                              gtk_text_buffer_get_mark (get_buffer (text_view),
4450                                                        "selection_bound"),
4451                              &insert);
4452 }
4453
4454 static void
4455 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
4456                                  const gchar *mark_name)
4457 {
4458   gint x, y;
4459   GdkModifierType state;
4460   GtkTextIter newplace;
4461
4462   /*   DV(g_print (G_STRLOC": begin\n")); */
4463   
4464   gdk_window_get_pointer (text_view->text_window->bin_window,
4465                           &x, &y, &state);
4466
4467   /*   DV(g_print (G_STRLOC": get iter at pixel\n"); */
4468   gtk_text_layout_get_iter_at_pixel (text_view->layout,
4469                                      &newplace,
4470                                      x + text_view->xoffset,
4471                                      y + text_view->yoffset);
4472
4473   {
4474     GtkTextMark *mark =
4475       gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
4476
4477     DV(g_print (G_STRLOC": move mark\n"));
4478     gtk_text_buffer_move_mark (get_buffer (text_view),
4479                                mark,
4480                                &newplace);
4481
4482     DV(g_print (G_STRLOC": scrolling onscreen\n"));
4483     gtk_text_view_scroll_mark_onscreen (text_view, mark);
4484   }
4485 }
4486
4487 static gint
4488 selection_scan_timeout (gpointer data)
4489 {
4490   GtkTextView *text_view;
4491
4492   GDK_THREADS_ENTER ();
4493   
4494   text_view = GTK_TEXT_VIEW (data);
4495
4496   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
4497   move_mark_to_pointer_and_scroll (text_view, "insert");
4498
4499   GDK_THREADS_LEAVE ();
4500   
4501   return TRUE; /* remain installed. */
4502 }
4503
4504 #define DND_SCROLL_MARGIN 0.20
4505
4506 static gint
4507 drag_scan_timeout (gpointer data)
4508 {
4509   GtkTextView *text_view;
4510   gint x, y;
4511   GdkModifierType state;
4512   GtkTextIter newplace;
4513
4514   GDK_THREADS_ENTER ();
4515   
4516   text_view = GTK_TEXT_VIEW (data);
4517
4518   gdk_window_get_pointer (text_view->text_window->bin_window,
4519                           &x, &y, &state);
4520   
4521   gtk_text_layout_get_iter_at_pixel (text_view->layout,
4522                                      &newplace,
4523                                      x + text_view->xoffset,
4524                                      y + text_view->yoffset);
4525   
4526   gtk_text_buffer_move_mark (get_buffer (text_view),
4527                              text_view->dnd_mark,
4528                              &newplace);
4529
4530   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4531   gtk_text_view_scroll_to_mark (text_view,
4532                                 text_view->dnd_mark,
4533                                 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
4534
4535   GDK_THREADS_LEAVE ();
4536   
4537   return TRUE;
4538 }
4539
4540 static gint
4541 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
4542 {
4543   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
4544   move_mark_to_pointer_and_scroll (text_view, "insert");
4545
4546   /* If we had to scroll offscreen, insert a timeout to do so
4547    * again. Note that in the timeout, even if the mouse doesn't
4548    * move, due to this scroll xoffset/yoffset will have changed
4549    * and we'll need to scroll again.
4550    */
4551   if (text_view->scroll_timeout != 0) /* reset on every motion event */
4552     gtk_timeout_remove (text_view->scroll_timeout);
4553   
4554   text_view->scroll_timeout =
4555     gtk_timeout_add (50, selection_scan_timeout, text_view);
4556
4557   return TRUE;
4558 }
4559
4560 static void
4561 gtk_text_view_start_selection_drag (GtkTextView       *text_view,
4562                                     const GtkTextIter *iter,
4563                                     GdkEventButton    *button)
4564 {
4565   GtkTextIter newplace;
4566
4567   g_return_if_fail (text_view->selection_drag_handler == 0);
4568
4569   gtk_grab_add (GTK_WIDGET (text_view));
4570
4571   newplace = *iter;
4572
4573   gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4574
4575   text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
4576                                                           "motion_notify_event",
4577                                                           GTK_SIGNAL_FUNC (selection_motion_event_handler),
4578                                                           NULL);
4579 }
4580
4581 /* returns whether we were really dragging */
4582 static gboolean
4583 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
4584 {
4585   if (text_view->selection_drag_handler == 0)
4586     return FALSE;
4587
4588   gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
4589   text_view->selection_drag_handler = 0;
4590
4591   if (text_view->scroll_timeout != 0)
4592     {
4593       gtk_timeout_remove (text_view->scroll_timeout);
4594       text_view->scroll_timeout = 0;
4595     }
4596
4597   /* one last update to current position */
4598   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
4599   move_mark_to_pointer_and_scroll (text_view, "insert");
4600
4601   gtk_grab_remove (GTK_WIDGET (text_view));
4602
4603   return TRUE;
4604 }
4605
4606 /*
4607  * Layout utils
4608  */
4609
4610 static void
4611 gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
4612                                          GtkTextAttributes  *values,
4613                                          GtkStyle           *style)
4614 {
4615   values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
4616   values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
4617
4618   if (values->font)
4619     pango_font_description_free (values->font);
4620
4621   values->font = pango_font_description_copy (style->font_desc);
4622 }
4623
4624 static void
4625 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
4626 {
4627   if (text_view->layout)
4628     {
4629       gboolean split_cursor;
4630       GtkTextDirection new_dir;
4631       GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4632   
4633       g_object_get (G_OBJECT (settings),
4634                     "gtk-split-cursor", &split_cursor,
4635                     NULL);
4636       if (split_cursor)
4637         new_dir = GTK_TEXT_DIR_NONE;
4638       else
4639         new_dir = (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
4640           GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
4641       
4642       if (text_view->layout->cursor_direction != new_dir)
4643         gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
4644     }
4645 }
4646
4647 static void
4648 gtk_text_view_ensure_layout (GtkTextView *text_view)
4649 {
4650   GtkWidget *widget;
4651
4652   widget = GTK_WIDGET (text_view);
4653
4654   if (text_view->layout == NULL)
4655     {
4656       GtkTextAttributes *style;
4657       PangoContext *ltr_context, *rtl_context;
4658       GSList *tmp_list;
4659
4660       DV(g_print(G_STRLOC"\n"));
4661       
4662       text_view->layout = gtk_text_layout_new ();
4663
4664       g_signal_connect (G_OBJECT (text_view->layout),
4665                         "invalidated",
4666                         G_CALLBACK (invalidated_handler),
4667                         text_view);
4668
4669       g_signal_connect (G_OBJECT (text_view->layout),
4670                         "changed",
4671                         G_CALLBACK (changed_handler),
4672                         text_view);
4673
4674       g_signal_connect (G_OBJECT (text_view->layout),
4675                         "allocate_child",
4676                         G_CALLBACK (gtk_text_view_child_allocated),
4677                         text_view);
4678       
4679       if (get_buffer (text_view))
4680         gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
4681
4682       if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
4683         gtk_text_view_pend_cursor_blink (text_view);
4684       else
4685         gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4686
4687       ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
4688       pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
4689       rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
4690       pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
4691
4692       gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
4693
4694       g_object_unref (G_OBJECT (ltr_context));
4695       g_object_unref (G_OBJECT (rtl_context));
4696
4697       gtk_text_view_check_keymap_direction (text_view);
4698
4699       style = gtk_text_attributes_new ();
4700
4701       gtk_widget_ensure_style (widget);
4702       gtk_text_view_set_attributes_from_style (text_view,
4703                                                style, widget->style);
4704
4705       style->pixels_above_lines = text_view->pixels_above_lines;
4706       style->pixels_below_lines = text_view->pixels_below_lines;
4707       style->pixels_inside_wrap = text_view->pixels_inside_wrap;
4708       style->left_margin = text_view->left_margin;
4709       style->right_margin = text_view->right_margin;
4710       style->indent = text_view->indent;
4711       style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
4712
4713       style->wrap_mode = text_view->wrap_mode;
4714       style->justification = text_view->justify;
4715       style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
4716
4717       gtk_text_layout_set_default_style (text_view->layout, style);
4718
4719       gtk_text_attributes_unref (style);
4720
4721       /* Set layout for all anchored children */
4722
4723       tmp_list = text_view->children;
4724       while (tmp_list != NULL)
4725         {
4726           GtkTextViewChild *vc = tmp_list->data;
4727
4728           if (vc->anchor)
4729             {
4730               gtk_text_anchored_child_set_layout (vc->widget,
4731                                                   text_view->layout);
4732               /* vc may now be invalid! */
4733             }
4734
4735           tmp_list = g_slist_next (tmp_list);
4736         }
4737     }
4738 }
4739
4740 /**
4741  * gtk_text_view_get_default_attributes:
4742  * @text_view: a #GtkTextView
4743  * 
4744  * Obtains a copy of the default text attributes. These are the
4745  * attributes used for text unless a tag overrides them.
4746  * You'd typically pass the default attributes in to
4747  * gtk_text_tag_get_attributes() in order to get the
4748  * attributes in effect at a given text position.
4749  *
4750  * The return value is a copy owned by the caller of this function,
4751  * and should be freed.
4752  * 
4753  * Return value: a new #GtkTextAttributes
4754  **/
4755 GtkTextAttributes*
4756 gtk_text_view_get_default_attributes (GtkTextView *text_view)
4757 {
4758   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
4759   
4760   gtk_text_view_ensure_layout (text_view);
4761
4762   return gtk_text_attributes_copy (text_view->layout->default_style);
4763 }
4764
4765 static void
4766 gtk_text_view_destroy_layout (GtkTextView *text_view)
4767 {
4768   if (text_view->layout)
4769     {
4770       GSList *tmp_list;
4771
4772       gtk_text_view_remove_validate_idles (text_view);
4773       
4774       /* Remove layout from all anchored children */
4775       tmp_list = text_view->children;
4776       while (tmp_list != NULL)
4777         {
4778           GtkTextViewChild *vc = tmp_list->data;
4779
4780           if (vc->anchor)
4781             {
4782               gtk_text_anchored_child_set_layout (vc->widget, NULL);
4783               /* vc may now be invalid! */
4784             }
4785
4786           tmp_list = g_slist_next (tmp_list);
4787         }
4788       
4789       gtk_text_view_stop_cursor_blink (text_view);
4790       gtk_text_view_end_selection_drag (text_view, NULL);
4791
4792       g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
4793                                             invalidated_handler, text_view);
4794       g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
4795                                             changed_handler, text_view);
4796       g_object_unref (G_OBJECT (text_view->layout));
4797       text_view->layout = NULL;
4798     }
4799 }
4800
4801 static void
4802 gtk_text_view_reset_im_context (GtkTextView *text_view)
4803 {
4804   if (text_view->need_im_reset)
4805     {
4806       text_view->need_im_reset = FALSE;
4807       gtk_im_context_reset (text_view->im_context);
4808     }
4809 }
4810
4811 /*
4812  * DND feature
4813  */
4814
4815 static void
4816 gtk_text_view_start_selection_dnd (GtkTextView       *text_view,
4817                                    const GtkTextIter *iter,
4818                                    GdkEventMotion    *event)
4819 {
4820   GdkDragContext *context;
4821   GtkTargetList *target_list;
4822
4823   text_view->drag_start_x = -1;
4824   text_view->drag_start_y = -1;
4825   text_view->pending_place_cursor_button = 0;
4826   
4827   target_list = gtk_target_list_new (target_table,
4828                                      G_N_ELEMENTS (target_table));
4829
4830   context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
4831                             GDK_ACTION_COPY | GDK_ACTION_MOVE,
4832                             1, (GdkEvent*)event);
4833
4834   gtk_target_list_unref (target_list);
4835
4836   gtk_drag_set_icon_default (context);
4837 }
4838
4839 static void
4840 gtk_text_view_drag_begin (GtkWidget        *widget,
4841                           GdkDragContext   *context)
4842 {
4843   /* do nothing */
4844 }
4845
4846 static void
4847 gtk_text_view_drag_end (GtkWidget        *widget,
4848                         GdkDragContext   *context)
4849 {
4850   GtkTextView *text_view;
4851
4852   text_view = GTK_TEXT_VIEW (widget);
4853 }
4854
4855 static void
4856 gtk_text_view_drag_data_get (GtkWidget        *widget,
4857                              GdkDragContext   *context,
4858                              GtkSelectionData *selection_data,
4859                              guint             info,
4860                              guint             time)
4861 {
4862   GtkTextView *text_view;
4863
4864   text_view = GTK_TEXT_VIEW (widget);
4865
4866   if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
4867     {
4868       GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
4869
4870       gtk_selection_data_set (selection_data,
4871                               gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
4872                               8, /* bytes */
4873                               (void*)&buffer,
4874                               sizeof (buffer));
4875     }
4876   else
4877     {
4878       gchar *str;
4879       GtkTextIter start;
4880       GtkTextIter end;
4881
4882       str = NULL;
4883
4884       if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4885                                                 &start, &end))
4886         {
4887           /* Extract the selected text */
4888           str = gtk_text_iter_get_visible_text (&start, &end);
4889         }
4890
4891       if (str)
4892         {
4893           gtk_selection_data_set_text (selection_data, str, -1);
4894           g_free (str);
4895         }
4896     }
4897 }
4898
4899 static void
4900 gtk_text_view_drag_data_delete (GtkWidget        *widget,
4901                                 GdkDragContext   *context)
4902 {
4903   GtkTextView *text_view;
4904
4905   text_view = GTK_TEXT_VIEW (widget);
4906
4907   gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
4908                                     TRUE, GTK_TEXT_VIEW (widget)->editable);
4909 }
4910
4911 static void
4912 gtk_text_view_drag_leave (GtkWidget        *widget,
4913                           GdkDragContext   *context,
4914                           guint             time)
4915 {
4916   GtkTextView *text_view;
4917
4918   text_view = GTK_TEXT_VIEW (widget);
4919
4920   gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
4921   
4922   if (text_view->scroll_timeout != 0)
4923     gtk_timeout_remove (text_view->scroll_timeout);
4924
4925   text_view->scroll_timeout = 0;
4926 }
4927
4928 static gboolean
4929 gtk_text_view_drag_motion (GtkWidget        *widget,
4930                            GdkDragContext   *context,
4931                            gint              x,
4932                            gint              y,
4933                            guint             time)
4934 {
4935   GtkTextIter newplace;
4936   GtkTextView *text_view;
4937   GtkTextIter start;
4938   GtkTextIter end;
4939   GdkRectangle target_rect;
4940   gint bx, by;
4941   GdkDragAction suggested_action = 0;
4942   
4943   text_view = GTK_TEXT_VIEW (widget);
4944
4945   target_rect = text_view->text_window->allocation;
4946   
4947   if (x < target_rect.x ||
4948       y < target_rect.y ||
4949       x > (target_rect.x + target_rect.width) ||
4950       y > (target_rect.y + target_rect.height))
4951     return FALSE; /* outside the text window, allow parent widgets to handle event */
4952
4953   gtk_text_view_window_to_buffer_coords (text_view,
4954                                          GTK_TEXT_WINDOW_WIDGET,
4955                                          x, y,
4956                                          &bx, &by);
4957
4958   gtk_text_layout_get_iter_at_pixel (text_view->layout,
4959                                      &newplace,
4960                                      bx, by);  
4961
4962   if (gtk_drag_dest_find_target (widget, context,
4963                                  gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
4964     {
4965       /* can't accept any of the offered targets */
4966     }                                 
4967   else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4968                                             &start, &end) &&
4969            gtk_text_iter_in_range (&newplace, &start, &end))
4970     {
4971       /* We're inside the selection. */
4972     }
4973   else
4974     {      
4975       if (gtk_text_iter_can_insert (&newplace, text_view->editable))
4976         {
4977           GtkWidget *source_widget;
4978           
4979           suggested_action = context->suggested_action;
4980           
4981           source_widget = gtk_drag_get_source_widget (context);
4982           
4983           if (source_widget == widget)
4984             {
4985               /* Default to MOVE, unless the user has
4986                * pressed ctrl or alt to affect available actions
4987                */
4988               if ((context->actions & GDK_ACTION_MOVE) != 0)
4989                 suggested_action = GDK_ACTION_MOVE;
4990             }
4991         }
4992       else
4993         {
4994           /* Can't drop here. */
4995         }
4996     }
4997
4998   if (suggested_action != 0)
4999     {
5000       gtk_text_mark_set_visible (text_view->dnd_mark,
5001                                  text_view->cursor_visible);
5002       
5003       gdk_drag_status (context, suggested_action, time);
5004     }
5005   else
5006     {
5007       gdk_drag_status (context, 0, time);
5008       gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5009     }
5010       
5011   gtk_text_buffer_move_mark (get_buffer (text_view),
5012                              text_view->dnd_mark,
5013                              &newplace);
5014
5015   DV(g_print (G_STRLOC": scrolling to mark\n"));
5016   gtk_text_view_scroll_to_mark (text_view,
5017                                 text_view->dnd_mark,
5018                                 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5019   
5020   if (text_view->scroll_timeout != 0) /* reset on every motion event */
5021     gtk_timeout_remove (text_view->scroll_timeout);
5022       
5023   text_view->scroll_timeout =
5024     gtk_timeout_add (50, drag_scan_timeout, text_view);
5025
5026   /* TRUE return means don't propagate the drag motion to parent
5027    * widgets that may also be drop sites.
5028    */
5029   return TRUE;
5030 }
5031
5032 static gboolean
5033 gtk_text_view_drag_drop (GtkWidget        *widget,
5034                          GdkDragContext   *context,
5035                          gint              x,
5036                          gint              y,
5037                          guint             time)
5038 {
5039   GtkTextView *text_view;
5040   GtkTextIter drop_point;
5041   GdkAtom target = GDK_NONE;
5042   
5043   text_view = GTK_TEXT_VIEW (widget);
5044   
5045   if (text_view->scroll_timeout != 0)
5046     gtk_timeout_remove (text_view->scroll_timeout);
5047
5048   text_view->scroll_timeout = 0;
5049
5050   gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5051
5052   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5053                                     &drop_point,
5054                                     text_view->dnd_mark);
5055
5056   if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5057     target = gtk_drag_dest_find_target (widget, context, NULL);
5058
5059   if (target != GDK_NONE)
5060     gtk_drag_get_data (widget, context, target, time);
5061   else
5062     gtk_drag_finish (context, FALSE, FALSE, time);
5063
5064   return TRUE;
5065 }
5066
5067 static void
5068 insert_text_data (GtkTextView      *text_view,
5069                   GtkTextIter      *drop_point,
5070                   GtkSelectionData *selection_data)
5071 {
5072   gchar *str;
5073
5074   str = gtk_selection_data_get_text (selection_data);
5075
5076   if (str)
5077     {
5078       gtk_text_buffer_insert_interactive (get_buffer (text_view),
5079                                           drop_point, str, -1,
5080                                           text_view->editable);
5081       g_free (str);
5082     }
5083 }
5084
5085 static void
5086 gtk_text_view_drag_data_received (GtkWidget        *widget,
5087                                   GdkDragContext   *context,
5088                                   gint              x,
5089                                   gint              y,
5090                                   GtkSelectionData *selection_data,
5091                                   guint             info,
5092                                   guint             time)
5093 {
5094   GtkTextIter drop_point;
5095   GtkTextView *text_view;
5096   gboolean success = FALSE;
5097
5098   text_view = GTK_TEXT_VIEW (widget);
5099
5100   if (!text_view->dnd_mark)
5101     goto done;
5102
5103   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5104                                     &drop_point,
5105                                     text_view->dnd_mark);
5106   
5107   if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5108     goto done;
5109
5110   if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5111     {
5112       GtkTextBuffer *src_buffer = NULL;
5113       GtkTextIter start, end;
5114       gboolean copy_tags = TRUE;
5115
5116       if (selection_data->length != sizeof (src_buffer))
5117         return;
5118
5119       memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5120
5121       if (src_buffer == NULL)
5122         return;
5123
5124       g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5125
5126       if (gtk_text_buffer_get_tag_table (src_buffer) !=
5127           gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5128         copy_tags = FALSE;
5129
5130       if (gtk_text_buffer_get_selection_bounds (src_buffer,
5131                                                 &start,
5132                                                 &end))
5133         {
5134           if (copy_tags)
5135             gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5136                                                       &drop_point,
5137                                                       &start,
5138                                                       &end,
5139                                                       text_view->editable);
5140           else
5141             {
5142               gchar *str;
5143
5144               str = gtk_text_iter_get_visible_text (&start, &end);
5145               gtk_text_buffer_insert_interactive (get_buffer (text_view),
5146                                                   &drop_point, str, -1,
5147                                                   text_view->editable);
5148               g_free (str);
5149             }
5150         }
5151     }
5152   else
5153     insert_text_data (text_view, &drop_point, selection_data);
5154
5155   success = TRUE;
5156
5157  done:
5158   gtk_drag_finish (context, success,
5159                    success && context->action == GDK_ACTION_MOVE,
5160                    time);
5161 }
5162
5163 static GtkAdjustment*
5164 get_hadjustment (GtkTextView *text_view)
5165 {
5166   if (text_view->hadjustment == NULL)
5167     gtk_text_view_set_scroll_adjustments (text_view,
5168                                           NULL, /* forces creation */
5169                                           text_view->vadjustment);
5170
5171   return text_view->hadjustment;
5172 }
5173
5174 static GtkAdjustment*
5175 get_vadjustment (GtkTextView *text_view)
5176 {
5177   if (text_view->vadjustment == NULL)
5178     gtk_text_view_set_scroll_adjustments (text_view,
5179                                           text_view->hadjustment,
5180                                           NULL); /* forces creation */
5181   return text_view->vadjustment;
5182 }
5183
5184
5185 static void
5186 gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
5187                                       GtkAdjustment *hadj,
5188                                       GtkAdjustment *vadj)
5189 {
5190   gboolean need_adjust = FALSE;
5191
5192   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5193
5194   if (hadj)
5195     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5196   else
5197     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5198   if (vadj)
5199     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5200   else
5201     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5202
5203   if (text_view->hadjustment && (text_view->hadjustment != hadj))
5204     {
5205       gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5206       g_object_unref (G_OBJECT (text_view->hadjustment));
5207     }
5208
5209   if (text_view->vadjustment && (text_view->vadjustment != vadj))
5210     {
5211       gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5212       g_object_unref (G_OBJECT (text_view->vadjustment));
5213     }
5214
5215   if (text_view->hadjustment != hadj)
5216     {
5217       text_view->hadjustment = hadj;
5218       g_object_ref (G_OBJECT (text_view->hadjustment));
5219       gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5220       
5221       gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5222                           (GtkSignalFunc) gtk_text_view_value_changed,
5223                           text_view);
5224       need_adjust = TRUE;
5225     }
5226
5227   if (text_view->vadjustment != vadj)
5228     {
5229       text_view->vadjustment = vadj;
5230       g_object_ref (G_OBJECT (text_view->vadjustment));
5231       gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5232       
5233       gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5234                           (GtkSignalFunc) gtk_text_view_value_changed,
5235                           text_view);
5236       need_adjust = TRUE;
5237     }
5238
5239   if (need_adjust)
5240     gtk_text_view_value_changed (NULL, text_view);
5241 }
5242
5243 static void
5244 gtk_text_view_value_changed (GtkAdjustment *adj,
5245                              GtkTextView   *text_view)
5246 {
5247   GtkTextIter iter;
5248   gint line_top;
5249   gint dx = 0;
5250   gint dy = 0;
5251   
5252   /* Note that we oddly call this function with adj == NULL
5253    * sometimes
5254    */
5255   
5256   text_view->onscreen_validated = FALSE;
5257
5258   DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5259              adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5260              adj ? adj->value : 0.0));
5261   
5262   if (adj == text_view->hadjustment)
5263     {
5264       dx = text_view->xoffset - (gint)adj->value;
5265       text_view->xoffset = adj->value;
5266     }
5267   else if (adj == text_view->vadjustment)
5268     {
5269       dy = text_view->yoffset - (gint)adj->value;
5270       text_view->yoffset = adj->value;
5271
5272       if (text_view->layout)
5273         {
5274           gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5275
5276           gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5277
5278           text_view->first_para_pixels = adj->value - line_top;
5279         }
5280     }
5281   
5282   if (dx != 0 || dy != 0)
5283     {
5284       GSList *tmp_list;
5285
5286       if (GTK_WIDGET_REALIZED (text_view))
5287         {
5288           if (dy != 0)
5289             {
5290               if (text_view->left_window)
5291                 text_window_scroll (text_view->left_window, 0, dy);
5292               if (text_view->right_window)
5293                 text_window_scroll (text_view->right_window, 0, dy);
5294             }
5295       
5296           if (dx != 0)
5297             {
5298               if (text_view->top_window)
5299                 text_window_scroll (text_view->top_window, dx, 0);
5300               if (text_view->bottom_window)
5301                 text_window_scroll (text_view->bottom_window, dx, 0);
5302             }
5303       
5304           /* It looks nicer to scroll the main area last, because
5305            * it takes a while, and making the side areas update
5306            * afterward emphasizes the slowness of scrolling the
5307            * main area.
5308            */
5309           text_window_scroll (text_view->text_window, dx, dy);
5310         }
5311       
5312       /* Children are now "moved" in the text window, poke
5313        * into widget->allocation for each child
5314        */
5315       tmp_list = text_view->children;
5316       while (tmp_list != NULL)
5317         {
5318           GtkTextViewChild *child = tmp_list->data;
5319           
5320           if (child->anchor)
5321             {              
5322               child->widget->allocation.x -= dx;
5323               child->widget->allocation.y -= dy;
5324
5325 #if 0
5326               g_print ("allocation for %p tweaked to %d,%d\n",
5327                        child->widget,
5328                        child->widget->allocation.x,
5329                        child->widget->allocation.y);
5330 #endif
5331             }
5332           
5333           tmp_list = g_slist_next (tmp_list);
5334         }
5335     }
5336
5337   /* This could result in invalidation, which would install the
5338    * first_validate_idle, which would validate onscreen;
5339    * but we're going to go ahead and validate here, so
5340    * first_validate_idle shouldn't have anything to do.
5341    */
5342   gtk_text_view_update_layout_width (text_view);
5343   
5344   /* note that validation of onscreen could invoke this function
5345    * recursively, by scrolling to maintain first_para, or in response
5346    * to updating the layout width, however there is no problem with
5347    * that, or shouldn't be.
5348    */
5349   gtk_text_view_validate_onscreen (text_view);
5350   
5351   /* process exposes */
5352   if (GTK_WIDGET_REALIZED (text_view))
5353     {
5354       if (text_view->left_window)
5355         gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
5356
5357       if (text_view->right_window)
5358         gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
5359
5360       if (text_view->top_window)
5361         gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
5362       
5363       if (text_view->bottom_window)
5364         gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
5365   
5366       gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
5367     }
5368
5369   /* If this got installed, get rid of it, it's just a waste of time. */
5370   if (text_view->first_validate_idle != 0)
5371     {
5372       g_source_remove (text_view->first_validate_idle);
5373       text_view->first_validate_idle = 0;
5374     }
5375
5376   gtk_text_view_update_im_spot_location (text_view);
5377   
5378   DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
5379 }
5380
5381 static void
5382 gtk_text_view_commit_handler (GtkIMContext  *context,
5383                               const gchar   *str,
5384                               GtkTextView   *text_view)
5385 {
5386   gtk_text_view_commit_text (text_view, str);
5387 }
5388
5389 static void
5390 gtk_text_view_commit_text (GtkTextView   *text_view,
5391                            const gchar   *str)
5392 {
5393   gboolean had_selection;
5394   
5395   gtk_text_buffer_begin_user_action (get_buffer (text_view));
5396
5397   had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5398                                                         NULL, NULL);
5399   
5400   gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5401                                     text_view->editable);
5402
5403   if (!strcmp (str, "\n"))
5404     {
5405       gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
5406                                                     text_view->editable);
5407     }
5408   else
5409     {
5410       if (!had_selection && text_view->overwrite_mode)
5411         gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
5412       gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5413                                                     text_view->editable);
5414     }
5415
5416   gtk_text_buffer_end_user_action (get_buffer (text_view));
5417
5418   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5419   gtk_text_view_scroll_mark_onscreen (text_view,
5420                                       gtk_text_buffer_get_mark (get_buffer (text_view),
5421                                                                 "insert"));
5422 }
5423
5424 static void
5425 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
5426                                        GtkTextView  *text_view)
5427 {
5428   gchar *str;
5429   PangoAttrList *attrs;
5430   gint cursor_pos;
5431
5432   gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
5433   gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
5434
5435   pango_attr_list_unref (attrs);
5436   g_free (str);
5437 }
5438
5439 static gboolean
5440 gtk_text_view_retrieve_surrounding_handler (GtkIMContext  *context,
5441                                             GtkTextView   *text_view)
5442 {
5443   GtkTextIter start;
5444   GtkTextIter end;
5445   gint pos;
5446   gchar *text;
5447
5448   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
5449                                     gtk_text_buffer_get_insert (text_view->buffer));
5450   end = start;
5451
5452   pos = gtk_text_iter_get_line_index (&start);
5453   gtk_text_iter_set_line_offset (&start, 0);
5454   gtk_text_iter_forward_to_line_end (&end);
5455
5456   text = gtk_text_iter_get_slice (&start, &end);
5457   gtk_im_context_set_surrounding (context, text, -1, pos);
5458   g_free (text);
5459
5460   return TRUE;
5461 }
5462
5463 static gboolean
5464 gtk_text_view_delete_surrounding_handler (GtkIMContext  *context,
5465                                           gint           offset,
5466                                           gint           n_chars,
5467                                           GtkTextView   *text_view)
5468 {
5469   GtkTextIter start;
5470   GtkTextIter end;
5471
5472   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
5473                                     gtk_text_buffer_get_insert (text_view->buffer));
5474   end = start;
5475
5476   gtk_text_iter_forward_chars (&start, offset);
5477   gtk_text_iter_forward_chars (&end, offset + n_chars);
5478
5479   gtk_text_buffer_delete (text_view->buffer, &start, &end);
5480
5481   return TRUE;
5482 }
5483
5484 static void
5485 gtk_text_view_mark_set_handler (GtkTextBuffer     *buffer,
5486                                 const GtkTextIter *location,
5487                                 GtkTextMark       *mark,
5488                                 gpointer           data)
5489 {
5490   GtkTextView *text_view = GTK_TEXT_VIEW (data);
5491   gboolean need_reset = FALSE;
5492
5493   if (mark == gtk_text_buffer_get_insert (buffer))
5494     {
5495       text_view->virtual_cursor_x = -1;
5496       text_view->virtual_cursor_y = -1;
5497       gtk_text_view_update_im_spot_location (text_view);
5498       need_reset = TRUE;
5499     }
5500   else if (mark == gtk_text_buffer_get_selection_bound (buffer))
5501     {
5502       need_reset = TRUE;
5503     }
5504
5505   if (need_reset)
5506     gtk_text_view_reset_im_context (text_view);
5507 }
5508
5509 static void
5510 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
5511                                       gint        *x,
5512                                       gint        *y)
5513 {
5514   GdkRectangle strong_pos;
5515   GtkTextIter insert;
5516
5517   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5518                                     gtk_text_buffer_get_mark (get_buffer (text_view),
5519                                                               "insert"));
5520
5521   if ((x && text_view->virtual_cursor_x == -1) ||
5522       (y && text_view->virtual_cursor_y == -1))
5523     gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
5524
5525   if (x)
5526     {
5527       if (text_view->virtual_cursor_x != -1)
5528         *x = text_view->virtual_cursor_x;
5529       else
5530         *x = strong_pos.x;
5531     }
5532
5533   if (y)
5534     {
5535       if (text_view->virtual_cursor_x != -1)
5536         *y = text_view->virtual_cursor_y;
5537       else
5538         *y = strong_pos.y + strong_pos.height / 2;
5539     }
5540 }
5541
5542 static void
5543 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
5544                                       gint         x,
5545                                       gint         y)
5546 {
5547   GdkRectangle strong_pos;
5548   GtkTextIter insert;
5549
5550   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5551                                     gtk_text_buffer_get_mark (get_buffer (text_view),
5552                                                               "insert"));
5553
5554   if (x == -1 || y == -1)
5555     gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
5556
5557   text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
5558   text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
5559 }
5560
5561 /* Quick hack of a popup menu
5562  */
5563 static void
5564 activate_cb (GtkWidget   *menuitem,
5565              GtkTextView *text_view)
5566 {
5567   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5568   gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
5569 }
5570
5571 static void
5572 append_action_signal (GtkTextView  *text_view,
5573                       GtkWidget    *menu,
5574                       const gchar  *stock_id,
5575                       const gchar  *signal,
5576                       gboolean      sensitive)
5577 {
5578   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5579
5580   g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
5581   gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
5582                       GTK_SIGNAL_FUNC (activate_cb), text_view);
5583
5584   gtk_widget_set_sensitive (menuitem, sensitive);
5585   
5586   gtk_widget_show (menuitem);
5587   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5588 }
5589
5590 static void
5591 popup_menu_detach (GtkWidget *attach_widget,
5592                    GtkMenu   *menu)
5593 {
5594   GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
5595 }
5596
5597 static void
5598 popup_position_func (GtkMenu   *menu,
5599                      gint      *x,
5600                      gint      *y,
5601                      gboolean  *push_in,
5602                      gpointer   user_data)
5603 {
5604   GtkTextView *text_view;
5605   GtkWidget *widget;
5606   GdkRectangle cursor_rect;
5607   GdkRectangle onscreen_rect;
5608   gint root_x, root_y;
5609   GtkTextIter iter;
5610   GtkRequisition req;      
5611   
5612   text_view = GTK_TEXT_VIEW (user_data);
5613   widget = GTK_WIDGET (text_view);
5614   
5615   g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
5616
5617   gdk_window_get_origin (widget->window, &root_x, &root_y);
5618
5619   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5620                                     &iter,
5621                                     gtk_text_buffer_get_insert (get_buffer (text_view)));
5622
5623   gtk_text_view_get_iter_location (text_view,
5624                                    &iter,
5625                                    &cursor_rect);
5626
5627   gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
5628   
5629   gtk_widget_size_request (text_view->popup_menu, &req);
5630
5631   /* can't use rectangle_intersect since cursor rect can have 0 width */
5632   if (cursor_rect.x >= onscreen_rect.x &&
5633       cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
5634       cursor_rect.y >= onscreen_rect.y &&
5635       cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
5636     {    
5637       gtk_text_view_buffer_to_window_coords (text_view,
5638                                              GTK_TEXT_WINDOW_WIDGET,
5639                                              cursor_rect.x, cursor_rect.y,
5640                                              &cursor_rect.x, &cursor_rect.y);
5641
5642       *x = root_x + cursor_rect.x + cursor_rect.width;
5643       *y = root_y + cursor_rect.y + cursor_rect.height;
5644     }
5645   else
5646     {
5647       /* Just center the menu, since cursor is offscreen. */      
5648       *x = root_x + (widget->allocation.width / 2 - req.width / 2);
5649       *y = root_y + (widget->allocation.height / 2 - req.height / 2);      
5650     }
5651
5652   /* Ensure sanity */
5653   *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
5654   *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
5655
5656   *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
5657   *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
5658 }
5659
5660 typedef struct
5661 {
5662   GtkTextView *text_view;
5663   gint button;
5664   guint time;
5665 } PopupInfo;
5666
5667 static gboolean
5668 range_contains_editable_text (const GtkTextIter *start,
5669                               const GtkTextIter *end,
5670                               gboolean default_editability)
5671 {
5672   GtkTextIter iter = *start;
5673
5674   while (gtk_text_iter_compare (&iter, end) < 0)
5675     {
5676       if (gtk_text_iter_editable (&iter, default_editability))
5677         return TRUE;
5678       
5679       gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
5680     }
5681
5682   return FALSE;
5683 }                             
5684
5685 static void
5686 popup_targets_received (GtkClipboard     *clipboard,
5687                         GtkSelectionData *data,
5688                         gpointer          user_data)
5689 {
5690   PopupInfo *info = user_data;
5691   GtkTextView *text_view = info->text_view;
5692   
5693   if (GTK_WIDGET_REALIZED (text_view))
5694     {
5695       /* We implicitely rely here on the fact that if we are pasting ourself, we'll
5696        * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
5697        */
5698       gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
5699       GtkWidget *menuitem;
5700       GtkWidget *submenu;
5701       gboolean have_selection;
5702       gboolean can_insert;
5703       GtkTextIter iter;
5704       GtkTextIter sel_start, sel_end;
5705       
5706       if (text_view->popup_menu)
5707         gtk_widget_destroy (text_view->popup_menu);
5708
5709       text_view->popup_menu = gtk_menu_new ();
5710       
5711       gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
5712                                  GTK_WIDGET (text_view),
5713                                  popup_menu_detach);
5714       
5715       have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5716                                                              &sel_start, &sel_end);
5717       
5718       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5719                                         &iter,
5720                                         gtk_text_buffer_get_insert (get_buffer (text_view)));
5721       
5722       can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
5723       
5724       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
5725                             have_selection &&
5726                             range_contains_editable_text (&sel_start, &sel_end,
5727                                                           text_view->editable));
5728       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
5729                             have_selection);
5730       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
5731                             can_insert && clipboard_contains_text);
5732       
5733       menuitem = gtk_separator_menu_item_new ();
5734       gtk_widget_show (menuitem);
5735       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
5736       
5737       menuitem = gtk_menu_item_new_with_label (_("Input Methods"));
5738       gtk_widget_show (menuitem);
5739       submenu = gtk_menu_new ();
5740       gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5741       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
5742       
5743       gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
5744                                             GTK_MENU_SHELL (submenu));
5745       
5746       gtk_signal_emit (GTK_OBJECT (text_view),
5747                        signals[POPULATE_POPUP],
5748                        text_view->popup_menu);
5749       
5750       if (info->button)
5751         gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
5752                         NULL, NULL,
5753                         info->button, info->time);
5754       else
5755         gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
5756                         popup_position_func, text_view,
5757                         0, gtk_get_current_event_time ());
5758     }
5759
5760   g_object_unref (text_view);
5761   g_free (info);
5762 }
5763
5764 static void
5765 gtk_text_view_do_popup (GtkTextView    *text_view,
5766                         GdkEventButton *event)
5767 {
5768   PopupInfo *info = g_new (PopupInfo, 1);
5769
5770   /* In order to know what entries we should make sensitive, we
5771    * ask for the current targets of the clipboard, and when
5772    * we get them, then we actually pop up the menu.
5773    */
5774   info->text_view = g_object_ref (text_view);
5775   
5776   if (event)
5777     {
5778       info->button = event->button;
5779       info->time = event->time;
5780     }
5781   else
5782     {
5783       info->button = 0;
5784       info->time = gtk_get_current_event_time ();
5785     }
5786
5787   gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
5788                                   gdk_atom_intern ("TARGETS", FALSE),
5789                                   popup_targets_received,
5790                                   info);
5791 }
5792
5793 static void
5794 gtk_text_view_popup_menu (GtkWidget *widget)
5795 {
5796   gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);  
5797 }
5798
5799 /* Child GdkWindows */
5800
5801
5802 static GtkTextWindow*
5803 text_window_new (GtkTextWindowType  type,
5804                  GtkWidget         *widget,
5805                  gint               width_request,
5806                  gint               height_request)
5807 {
5808   GtkTextWindow *win;
5809
5810   win = g_new (GtkTextWindow, 1);
5811
5812   win->type = type;
5813   win->widget = widget;
5814   win->window = NULL;
5815   win->bin_window = NULL;
5816   win->requisition.width = width_request;
5817   win->requisition.height = height_request;
5818   win->allocation.width = width_request;
5819   win->allocation.height = height_request;
5820   win->allocation.x = 0;
5821   win->allocation.y = 0;
5822
5823   return win;
5824 }
5825
5826 static void
5827 text_window_free (GtkTextWindow *win)
5828 {
5829   if (win->window)
5830     text_window_unrealize (win);
5831
5832   g_free (win);
5833 }
5834
5835 static void
5836 text_window_realize (GtkTextWindow *win,
5837                      GdkWindow     *parent)
5838 {
5839   GdkWindowAttr attributes;
5840   gint attributes_mask;
5841   GdkCursor *cursor;
5842
5843   attributes.window_type = GDK_WINDOW_CHILD;
5844   attributes.x = win->allocation.x;
5845   attributes.y = win->allocation.y;
5846   attributes.width = win->allocation.width;
5847   attributes.height = win->allocation.height;
5848   attributes.wclass = GDK_INPUT_OUTPUT;
5849   attributes.visual = gtk_widget_get_visual (win->widget);
5850   attributes.colormap = gtk_widget_get_colormap (win->widget);
5851   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
5852
5853   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
5854
5855   win->window = gdk_window_new (parent,
5856                                 &attributes,
5857                                 attributes_mask);
5858
5859   gdk_window_show (win->window);
5860   gdk_window_set_user_data (win->window, win->widget);
5861
5862   attributes.x = 0;
5863   attributes.y = 0;
5864   attributes.width = win->allocation.width;
5865   attributes.height = win->allocation.height;
5866   attributes.event_mask = (GDK_EXPOSURE_MASK            |
5867                            GDK_SCROLL_MASK              |
5868                            GDK_KEY_PRESS_MASK           |
5869                            GDK_BUTTON_PRESS_MASK        |
5870                            GDK_BUTTON_RELEASE_MASK      |
5871                            GDK_POINTER_MOTION_MASK      |
5872                            GDK_POINTER_MOTION_HINT_MASK |
5873                            gtk_widget_get_events (win->widget));
5874
5875   win->bin_window = gdk_window_new (win->window,
5876                                     &attributes,
5877                                     attributes_mask);
5878
5879   gdk_window_show (win->bin_window);
5880   gdk_window_set_user_data (win->bin_window, win->widget);
5881
5882   if (win->type == GTK_TEXT_WINDOW_TEXT)
5883     {
5884       /* I-beam cursor */
5885       cursor = gdk_cursor_new (GDK_XTERM);
5886       gdk_window_set_cursor (win->bin_window, cursor);
5887       gdk_cursor_destroy (cursor);
5888
5889       gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
5890                                         win->window);
5891
5892
5893       gdk_window_set_background (win->bin_window,
5894                                  &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
5895     }
5896   else
5897     {
5898       gdk_window_set_background (win->bin_window,
5899                                  &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
5900     }
5901
5902   g_object_set_qdata (G_OBJECT (win->window),
5903                       g_quark_from_static_string ("gtk-text-view-text-window"),
5904                       win);
5905
5906   g_object_set_qdata (G_OBJECT (win->bin_window),
5907                       g_quark_from_static_string ("gtk-text-view-text-window"),
5908                       win);
5909 }
5910
5911 static void
5912 text_window_unrealize (GtkTextWindow *win)
5913 {
5914   if (win->type == GTK_TEXT_WINDOW_TEXT)
5915     {
5916       gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
5917                                         NULL);
5918     }
5919
5920   gdk_window_set_user_data (win->window, NULL);
5921   gdk_window_set_user_data (win->bin_window, NULL);
5922   gdk_window_destroy (win->bin_window);
5923   gdk_window_destroy (win->window);
5924   win->window = NULL;
5925   win->bin_window = NULL;
5926 }
5927
5928 static void
5929 text_window_size_allocate (GtkTextWindow *win,
5930                            GdkRectangle  *rect)
5931 {
5932   win->allocation = *rect;
5933
5934   if (win->window)
5935     {
5936       gdk_window_move_resize (win->window,
5937                               rect->x, rect->y,
5938                               rect->width, rect->height);
5939
5940       gdk_window_resize (win->bin_window,
5941                          rect->width, rect->height);
5942     }
5943 }
5944
5945 static void
5946 text_window_scroll        (GtkTextWindow *win,
5947                            gint           dx,
5948                            gint           dy)
5949 {
5950   if (dx != 0 || dy != 0)
5951     {
5952       gdk_window_scroll (win->bin_window, dx, dy);
5953     }
5954 }
5955
5956 static void
5957 text_window_invalidate_rect (GtkTextWindow *win,
5958                              GdkRectangle  *rect)
5959 {
5960   GdkRectangle window_rect;
5961
5962   gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
5963                                          win->type,
5964                                          rect->x,
5965                                          rect->y,
5966                                          &window_rect.x,
5967                                          &window_rect.y);
5968
5969   window_rect.width = rect->width;
5970   window_rect.height = rect->height;
5971   
5972   /* Adjust the rect as appropriate */
5973   
5974   switch (win->type)
5975     {
5976     case GTK_TEXT_WINDOW_TEXT:
5977       break;
5978
5979     case GTK_TEXT_WINDOW_LEFT:
5980     case GTK_TEXT_WINDOW_RIGHT:
5981       window_rect.x = 0;
5982       window_rect.width = win->allocation.width;
5983       break;
5984
5985     case GTK_TEXT_WINDOW_TOP:
5986     case GTK_TEXT_WINDOW_BOTTOM:
5987       window_rect.y = 0;
5988       window_rect.height = win->allocation.height;
5989       break;
5990
5991     default:
5992       g_warning ("%s: bug!", G_STRLOC);
5993       return;
5994       break;
5995     }
5996           
5997   gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
5998
5999 #if 0
6000   {
6001     GdkColor color = { 0, 65535, 0, 0 };
6002     GdkGC *gc = gdk_gc_new (win->bin_window);
6003     gdk_gc_set_rgb_fg_color (gc, &color);
6004     gdk_draw_rectangle (win->bin_window,
6005                         gc, TRUE, window_rect.x, window_rect.y,
6006                         window_rect.width, window_rect.height);
6007     gdk_gc_unref (gc);
6008   }
6009 #endif
6010 }
6011
6012 static gint
6013 text_window_get_width (GtkTextWindow *win)
6014 {
6015   return win->allocation.width;
6016 }
6017
6018 static gint
6019 text_window_get_height (GtkTextWindow *win)
6020 {
6021   return win->allocation.height;
6022 }
6023
6024 static void
6025 text_window_get_allocation (GtkTextWindow *win,
6026                             GdkRectangle  *rect)
6027 {
6028   *rect = win->allocation;
6029 }
6030
6031 /* Windows */
6032
6033
6034 /**
6035  * gtk_text_view_get_window:
6036  * @text_view: a #GtkTextView
6037  * @win: window to get
6038  *
6039  * Retrieves the #GdkWindow corresponding to an area of the text view;
6040  * possible windows include the overall widget window, child windows
6041  * on the left, right, top, bottom, and the window that displays the
6042  * text buffer. Windows are %NULL and nonexistent if their width or
6043  * height is 0, and are nonexistent before the widget has been
6044  * realized.
6045  *
6046  * Return value: a #GdkWindow, or %NULL
6047  **/
6048 GdkWindow*
6049 gtk_text_view_get_window (GtkTextView *text_view,
6050                           GtkTextWindowType win)
6051 {
6052   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6053
6054   switch (win)
6055     {
6056     case GTK_TEXT_WINDOW_WIDGET:
6057       return GTK_WIDGET (text_view)->window;
6058       break;
6059
6060     case GTK_TEXT_WINDOW_TEXT:
6061       return text_view->text_window->bin_window;
6062       break;
6063
6064     case GTK_TEXT_WINDOW_LEFT:
6065       if (text_view->left_window)
6066         return text_view->left_window->bin_window;
6067       else
6068         return NULL;
6069       break;
6070
6071     case GTK_TEXT_WINDOW_RIGHT:
6072       if (text_view->right_window)
6073         return text_view->right_window->bin_window;
6074       else
6075         return NULL;
6076       break;
6077
6078     case GTK_TEXT_WINDOW_TOP:
6079       if (text_view->top_window)
6080         return text_view->top_window->bin_window;
6081       else
6082         return NULL;
6083       break;
6084
6085     case GTK_TEXT_WINDOW_BOTTOM:
6086       if (text_view->bottom_window)
6087         return text_view->bottom_window->bin_window;
6088       else
6089         return NULL;
6090       break;
6091
6092     default:
6093       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6094       return NULL;
6095       break;
6096     }
6097 }
6098
6099 /**
6100  * gtk_text_view_get_window_type:
6101  * @text_view: a #GtkTextView
6102  * @window: a window type
6103  *
6104  * Usually used to find out which window an event corresponds to.
6105  * If you connect to an event signal on @text_view, this function
6106  * should be called on <literal>event-&gt;window</literal> to
6107  * see which window it was.
6108  *
6109  * Return value: the window type.
6110  **/
6111 GtkTextWindowType
6112 gtk_text_view_get_window_type (GtkTextView *text_view,
6113                                GdkWindow   *window)
6114 {
6115   GtkTextWindow *win;
6116
6117   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6118   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6119
6120   if (window == GTK_WIDGET (text_view)->window)
6121     return GTK_TEXT_WINDOW_WIDGET;
6122
6123   win = g_object_get_qdata (G_OBJECT (window),
6124                             g_quark_try_string ("gtk-text-view-text-window"));
6125
6126   if (win)
6127     return win->type;
6128   else
6129     {
6130       return GTK_TEXT_WINDOW_PRIVATE;
6131     }
6132 }
6133
6134 static void
6135 buffer_to_widget (GtkTextView      *text_view,
6136                   gint              buffer_x,
6137                   gint              buffer_y,
6138                   gint             *window_x,
6139                   gint             *window_y)
6140 {
6141   gint focus_edge_width;
6142   gboolean interior_focus;
6143   
6144   gtk_widget_style_get (GTK_WIDGET (text_view), "interior_focus", &interior_focus, NULL);
6145
6146   if (interior_focus)
6147     focus_edge_width = 0;
6148   else
6149     focus_edge_width = 1;
6150   
6151   if (window_x)
6152     {
6153       *window_x = buffer_x - text_view->xoffset + focus_edge_width;
6154       if (text_view->left_window)
6155         *window_x += text_view->left_window->allocation.width;
6156     }
6157
6158   if (window_y)
6159     {
6160       *window_y = buffer_y - text_view->yoffset + focus_edge_width;
6161       if (text_view->top_window)
6162         *window_y += text_view->top_window->allocation.height;
6163     }
6164 }
6165
6166 static void
6167 widget_to_text_window (GtkTextWindow *win,
6168                        gint           widget_x,
6169                        gint           widget_y,
6170                        gint          *window_x,
6171                        gint          *window_y)
6172 {
6173   if (window_x)
6174     *window_x = widget_x - win->allocation.x;
6175
6176   if (window_y)
6177     *window_y = widget_y - win->allocation.y;
6178 }
6179
6180 static void
6181 buffer_to_text_window (GtkTextView   *text_view,
6182                        GtkTextWindow *win,
6183                        gint           buffer_x,
6184                        gint           buffer_y,
6185                        gint          *window_x,
6186                        gint          *window_y)
6187 {
6188   if (win == NULL)
6189     {
6190       g_warning ("Attempt to convert text buffer coordinates to coordinates "
6191                  "for a nonexistent or private child window of GtkTextView");
6192       return;
6193     }
6194
6195   buffer_to_widget (text_view,
6196                     buffer_x, buffer_y,
6197                     window_x, window_y);
6198
6199   widget_to_text_window (win,
6200                          window_x ? *window_x : 0,
6201                          window_y ? *window_y : 0,
6202                          window_x,
6203                          window_y);
6204 }
6205
6206 /**
6207  * gtk_text_view_buffer_to_window_coords:
6208  * @text_view: a #GtkTextView
6209  * @win: a #GtkTextWindowType
6210  * @buffer_x: buffer x coordinate
6211  * @buffer_y: buffer y coordinate
6212  * @window_x: window x coordinate return location
6213  * @window_y: window y coordinate return location
6214  *
6215  * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6216  * @win, and stores the result in (@window_x, @window_y).
6217  **/
6218 void
6219 gtk_text_view_buffer_to_window_coords (GtkTextView      *text_view,
6220                                        GtkTextWindowType win,
6221                                        gint              buffer_x,
6222                                        gint              buffer_y,
6223                                        gint             *window_x,
6224                                        gint             *window_y)
6225 {
6226   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6227
6228   switch (win)
6229     {
6230     case GTK_TEXT_WINDOW_WIDGET:
6231       buffer_to_widget (text_view,
6232                         buffer_x, buffer_y,
6233                         window_x, window_y);
6234       break;
6235
6236     case GTK_TEXT_WINDOW_TEXT:
6237       if (window_x)
6238         *window_x = buffer_x - text_view->xoffset;
6239       if (window_y)
6240         *window_y = buffer_y - text_view->yoffset;
6241       break;
6242
6243     case GTK_TEXT_WINDOW_LEFT:
6244       buffer_to_text_window (text_view,
6245                              text_view->left_window,
6246                              buffer_x, buffer_y,
6247                              window_x, window_y);
6248       break;
6249
6250     case GTK_TEXT_WINDOW_RIGHT:
6251       buffer_to_text_window (text_view,
6252                              text_view->right_window,
6253                              buffer_x, buffer_y,
6254                              window_x, window_y);
6255       break;
6256
6257     case GTK_TEXT_WINDOW_TOP:
6258       buffer_to_text_window (text_view,
6259                              text_view->top_window,
6260                              buffer_x, buffer_y,
6261                              window_x, window_y);
6262       break;
6263
6264     case GTK_TEXT_WINDOW_BOTTOM:
6265       buffer_to_text_window (text_view,
6266                              text_view->bottom_window,
6267                              buffer_x, buffer_y,
6268                              window_x, window_y);
6269       break;
6270
6271     case GTK_TEXT_WINDOW_PRIVATE:
6272       g_warning ("%s: can't get coords for private windows", G_STRLOC);
6273       break;
6274
6275     default:
6276       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6277       break;
6278     }
6279 }
6280
6281 static void
6282 widget_to_buffer (GtkTextView *text_view,
6283                   gint         widget_x,
6284                   gint         widget_y,
6285                   gint        *buffer_x,
6286                   gint        *buffer_y)
6287 {
6288   gint focus_edge_width;
6289   gboolean interior_focus;
6290   
6291   gtk_widget_style_get (GTK_WIDGET (text_view), "interior_focus", &interior_focus, NULL);
6292
6293   if (interior_focus)
6294     focus_edge_width = 0;
6295   else
6296     focus_edge_width = 1;
6297   
6298   if (buffer_x)
6299     {
6300       *buffer_x = widget_x - focus_edge_width + text_view->xoffset;
6301       if (text_view->left_window)
6302         *buffer_x -= text_view->left_window->allocation.width;
6303     }
6304
6305   if (buffer_y)
6306     {
6307       *buffer_y = widget_y - focus_edge_width + text_view->yoffset;
6308       if (text_view->top_window)
6309         *buffer_y -= text_view->top_window->allocation.height;
6310     }
6311 }
6312
6313 static void
6314 text_window_to_widget (GtkTextWindow *win,
6315                        gint           window_x,
6316                        gint           window_y,
6317                        gint          *widget_x,
6318                        gint          *widget_y)
6319 {
6320   if (widget_x)
6321     *widget_x = window_x + win->allocation.x;
6322
6323   if (widget_y)
6324     *widget_y = window_y + win->allocation.y;
6325 }
6326
6327 static void
6328 text_window_to_buffer (GtkTextView   *text_view,
6329                        GtkTextWindow *win,
6330                        gint           window_x,
6331                        gint           window_y,
6332                        gint          *buffer_x,
6333                        gint          *buffer_y)
6334 {
6335   if (win == NULL)
6336     {
6337       g_warning ("Attempt to convert GtkTextView buffer coordinates into "
6338                  "coordinates for a nonexistent child window.");
6339       return;
6340     }
6341
6342   text_window_to_widget (win,
6343                          window_x,
6344                          window_y,
6345                          buffer_x,
6346                          buffer_y);
6347
6348   widget_to_buffer (text_view,
6349                     buffer_x ? *buffer_x : 0,
6350                     buffer_y ? *buffer_y : 0,
6351                     buffer_x,
6352                     buffer_y);
6353 }
6354
6355 /**
6356  * gtk_text_view_window_to_buffer_coords:
6357  * @text_view: a #GtkTextView
6358  * @win: a #GtkTextWindowType
6359  * @window_x: window x coordinate
6360  * @window_y: window y coordinate
6361  * @buffer_x: buffer x coordinate return location
6362  * @buffer_y: buffer y coordinate return location
6363  *
6364  * Converts coordinates on the window identified by @win to buffer
6365  * coordinates, storing the result in (@buffer_x,@buffer_y).
6366  **/
6367 void
6368 gtk_text_view_window_to_buffer_coords (GtkTextView      *text_view,
6369                                        GtkTextWindowType win,
6370                                        gint              window_x,
6371                                        gint              window_y,
6372                                        gint             *buffer_x,
6373                                        gint             *buffer_y)
6374 {
6375   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6376
6377   switch (win)
6378     {
6379     case GTK_TEXT_WINDOW_WIDGET:
6380       widget_to_buffer (text_view,
6381                         window_x, window_y,
6382                         buffer_x, buffer_y);
6383       break;
6384
6385     case GTK_TEXT_WINDOW_TEXT:
6386       if (buffer_x)
6387         *buffer_x = window_x + text_view->xoffset;
6388       if (buffer_y)
6389         *buffer_y = window_y + text_view->yoffset;
6390       break;
6391
6392     case GTK_TEXT_WINDOW_LEFT:
6393       text_window_to_buffer (text_view,
6394                              text_view->left_window,
6395                              window_x, window_y,
6396                              buffer_x, buffer_y);
6397       break;
6398
6399     case GTK_TEXT_WINDOW_RIGHT:
6400       text_window_to_buffer (text_view,
6401                              text_view->right_window,
6402                              window_x, window_y,
6403                              buffer_x, buffer_y);
6404       break;
6405
6406     case GTK_TEXT_WINDOW_TOP:
6407       text_window_to_buffer (text_view,
6408                              text_view->top_window,
6409                              window_x, window_y,
6410                              buffer_x, buffer_y);
6411       break;
6412
6413     case GTK_TEXT_WINDOW_BOTTOM:
6414       text_window_to_buffer (text_view,
6415                              text_view->bottom_window,
6416                              window_x, window_y,
6417                              buffer_x, buffer_y);
6418       break;
6419
6420     case GTK_TEXT_WINDOW_PRIVATE:
6421       g_warning ("%s: can't get coords for private windows", G_STRLOC);
6422       break;
6423
6424     default:
6425       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6426       break;
6427     }
6428 }
6429
6430 static void
6431 set_window_width (GtkTextView      *text_view,
6432                   gint              width,
6433                   GtkTextWindowType type,
6434                   GtkTextWindow   **winp)
6435 {
6436   if (width == 0)
6437     {
6438       if (*winp)
6439         {
6440           text_window_free (*winp);
6441           *winp = NULL;
6442           gtk_widget_queue_resize (GTK_WIDGET (text_view));
6443         }
6444     }
6445   else
6446     {
6447       if (*winp == NULL)
6448         {
6449           *winp = text_window_new (type,
6450                                    GTK_WIDGET (text_view),
6451                                    width, 0);
6452           /* if the widget is already realized we need to realize the child manually */
6453           if (GTK_WIDGET_REALIZED (text_view))
6454             text_window_realize (*winp, GTK_WIDGET (text_view)->window);
6455         }
6456       else
6457         {
6458           if ((*winp)->requisition.width == width)
6459             return;
6460
6461           (*winp)->requisition.width = width;
6462         }
6463
6464       gtk_widget_queue_resize (GTK_WIDGET (text_view));
6465     }
6466 }
6467
6468
6469 static void
6470 set_window_height (GtkTextView      *text_view,
6471                    gint              height,
6472                    GtkTextWindowType type,
6473                    GtkTextWindow   **winp)
6474 {
6475   if (height == 0)
6476     {
6477       if (*winp)
6478         {
6479           text_window_free (*winp);
6480           *winp = NULL;
6481           gtk_widget_queue_resize (GTK_WIDGET (text_view));
6482         }
6483     }
6484   else
6485     {
6486       if (*winp == NULL)
6487         {
6488           *winp = text_window_new (type,
6489                                    GTK_WIDGET (text_view),
6490                                    0, height);
6491
6492           /* if the widget is already realized we need to realize the child manually */
6493           if (GTK_WIDGET_REALIZED (text_view))
6494             text_window_realize (*winp, GTK_WIDGET (text_view)->window);
6495         }
6496       else
6497         {
6498           if ((*winp)->requisition.height == height)
6499             return;
6500
6501           (*winp)->requisition.height = height;
6502         }
6503
6504       gtk_widget_queue_resize (GTK_WIDGET (text_view));
6505     }
6506 }
6507
6508 /**
6509  * gtk_text_view_set_border_window_size:
6510  * @text_view: a #GtkTextView
6511  * @type: window to affect
6512  * @size: width or height of the window
6513  *
6514  * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
6515  * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
6516  * Automatically destroys the corresponding window if the size is set
6517  * to 0, and creates the window if the size is set to non-zero.  This
6518  * function can only be used for the "border windows," it doesn't work
6519  * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
6520  * #GTK_TEXT_WINDOW_PRIVATE.
6521  **/
6522 void
6523 gtk_text_view_set_border_window_size (GtkTextView      *text_view,
6524                                       GtkTextWindowType type,
6525                                       gint              size)
6526
6527 {
6528   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6529   g_return_if_fail (size >= 0);
6530
6531   switch (type)
6532     {
6533     case GTK_TEXT_WINDOW_LEFT:
6534       set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
6535                         &text_view->left_window);
6536       break;
6537
6538     case GTK_TEXT_WINDOW_RIGHT:
6539       set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
6540                         &text_view->right_window);
6541       break;
6542
6543     case GTK_TEXT_WINDOW_TOP:
6544       set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
6545                          &text_view->top_window);
6546       break;
6547
6548     case GTK_TEXT_WINDOW_BOTTOM:
6549       set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
6550                          &text_view->bottom_window);
6551       break;
6552
6553     default:
6554       g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
6555       break;
6556     }
6557 }
6558
6559 /**
6560  * gtk_text_view_get_border_window_size:
6561  * @text_view: a #GtkTextView
6562  * @type: window to return size from
6563  *
6564  * Gets the width of the specified border window. See
6565  * gtk_text_view_set_border_window_size().
6566  *
6567  * Return value: width of window
6568  **/
6569 gint
6570 gtk_text_view_get_border_window_size (GtkTextView       *text_view,
6571                                       GtkTextWindowType  type)
6572 {
6573   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6574   
6575   switch (type)
6576     {
6577     case GTK_TEXT_WINDOW_LEFT:
6578       if (text_view->left_window)
6579         return text_view->left_window->requisition.width;
6580       
6581     case GTK_TEXT_WINDOW_RIGHT:
6582       if (text_view->right_window)
6583         return text_view->right_window->requisition.width;
6584       
6585     case GTK_TEXT_WINDOW_TOP:
6586       if (text_view->top_window)
6587         return text_view->top_window->requisition.height;
6588
6589     case GTK_TEXT_WINDOW_BOTTOM:
6590       if (text_view->bottom_window)
6591         return text_view->bottom_window->requisition.height;
6592       
6593     default:
6594       g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
6595       break;
6596     }
6597
6598   return 0;
6599 }
6600
6601 /*
6602  * Child widgets
6603  */
6604
6605 static GtkTextViewChild*
6606 text_view_child_new_anchored (GtkWidget          *child,
6607                               GtkTextChildAnchor *anchor,
6608                               GtkTextLayout      *layout)
6609 {
6610   GtkTextViewChild *vc;
6611
6612   vc = g_new (GtkTextViewChild, 1);
6613
6614   vc->widget = child;
6615   vc->anchor = anchor;
6616
6617   vc->from_top_of_line = 0;
6618   vc->from_left_of_buffer = 0;
6619   
6620   g_object_ref (G_OBJECT (vc->widget));
6621   g_object_ref (G_OBJECT (vc->anchor));
6622
6623   g_object_set_data (G_OBJECT (child),
6624                      "gtk-text-view-child",
6625                      vc);
6626
6627   gtk_text_child_anchor_register_child (anchor, child, layout);
6628   
6629   return vc;
6630 }
6631
6632 static GtkTextViewChild*
6633 text_view_child_new_window (GtkWidget          *child,
6634                             GtkTextWindowType   type,
6635                             gint                x,
6636                             gint                y)
6637 {
6638   GtkTextViewChild *vc;
6639
6640   vc = g_new (GtkTextViewChild, 1);
6641
6642   vc->widget = child;
6643   vc->anchor = NULL;
6644
6645   vc->from_top_of_line = 0;
6646   vc->from_left_of_buffer = 0;
6647   
6648   g_object_ref (G_OBJECT (vc->widget));
6649
6650   vc->type = type;
6651   vc->x = x;
6652   vc->y = y;
6653
6654   g_object_set_data (G_OBJECT (child),
6655                      "gtk-text-view-child",
6656                      vc);
6657   
6658   return vc;
6659 }
6660
6661 static void
6662 text_view_child_free (GtkTextViewChild *child)
6663 {
6664   g_object_set_data (G_OBJECT (child->widget),
6665                      "gtk-text-view-child", NULL);
6666
6667   if (child->anchor)
6668     {
6669       gtk_text_child_anchor_unregister_child (child->anchor,
6670                                               child->widget);
6671       g_object_unref (G_OBJECT (child->anchor));
6672     }
6673
6674   g_object_unref (G_OBJECT (child->widget));
6675
6676   g_free (child);
6677 }
6678
6679 static void
6680 text_view_child_set_parent_window (GtkTextView      *text_view,
6681                                    GtkTextViewChild *vc)
6682 {
6683   if (vc->anchor)
6684     gtk_widget_set_parent_window (vc->widget,
6685                                   text_view->text_window->bin_window);
6686   else
6687     {
6688       GdkWindow *window;
6689       window = gtk_text_view_get_window (text_view,
6690                                          vc->type);
6691       gtk_widget_set_parent_window (vc->widget, window);
6692     }
6693 }
6694
6695 static void
6696 add_child (GtkTextView      *text_view,
6697            GtkTextViewChild *vc)
6698 {
6699   text_view->children = g_slist_prepend (text_view->children,
6700                                          vc);
6701
6702   if (GTK_WIDGET_REALIZED (text_view))
6703     text_view_child_set_parent_window (text_view, vc);
6704   
6705   gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
6706 }
6707
6708 void
6709 gtk_text_view_add_child_at_anchor (GtkTextView          *text_view,
6710                                    GtkWidget            *child,
6711                                    GtkTextChildAnchor   *anchor)
6712 {
6713   GtkTextViewChild *vc;
6714
6715   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6716   g_return_if_fail (GTK_IS_WIDGET (child));
6717   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
6718   g_return_if_fail (child->parent == NULL);
6719
6720   gtk_text_view_ensure_layout (text_view);
6721
6722   vc = text_view_child_new_anchored (child, anchor,
6723                                      text_view->layout);
6724
6725   add_child (text_view, vc);
6726
6727   g_assert (vc->widget == child);
6728   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
6729 }
6730
6731 /**
6732  * gtk_text_view_add_child_in_window:
6733  * @text_view: a #GtkTextView
6734  * @child: a #GtkWidget
6735  * @which_window: which window the child should appear in
6736  * @xpos: X position of child in window coordinates
6737  * @ypos: Y position of child in window coordinates
6738  *
6739  * Adds a child at fixed coordinates in one of the text widget's
6740  * windows.  The window must have nonzero size (see
6741  * gtk_text_view_set_border_window_size()).  Note that the child
6742  * coordinates are given relative to the #GdkWindow in question, and
6743  * that these coordinates have no sane relationship to scrolling. When
6744  * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
6745  * irrelevant, the child floats above all scrollable areas. But when
6746  * placing a child in one of the scrollable windows (border windows or
6747  * text window), you'll need to compute the child's correct position
6748  * in buffer coordinates any time scrolling occurs or buffer changes
6749  * occur, and then call gtk_text_view_move_child() to update the
6750  * child's position. Unfortunately there's no good way to detect that
6751  * scrolling has occurred, using the current API; a possible hack
6752  * would be to update all child positions when the scroll adjustments
6753  * change or the text buffer changes. See bug 64518 on
6754  * bugzilla.gnome.org for status of fixing this issue.
6755  *
6756  **/
6757 void
6758 gtk_text_view_add_child_in_window (GtkTextView          *text_view,
6759                                    GtkWidget            *child,
6760                                    GtkTextWindowType     which_window,
6761                                    gint                  xpos,
6762                                    gint                  ypos)
6763 {
6764   GtkTextViewChild *vc;
6765
6766   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6767   g_return_if_fail (GTK_IS_WIDGET (child));
6768   g_return_if_fail (child->parent == NULL);
6769
6770   vc = text_view_child_new_window (child, which_window,
6771                                    xpos, ypos);
6772
6773   add_child (text_view, vc);
6774
6775   g_assert (vc->widget == child);
6776   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
6777 }
6778
6779 /**
6780  * gtk_text_view_move_child:
6781  * @text_view: a #GtkTextView
6782  * @child: child widget already added to the text view
6783  * @xpos: new X position in window coordinates
6784  * @ypos: new Y position in window coordinates
6785  *
6786  * Updates the position of a child, as for gtk_text_view_add_child_in_window().
6787  * 
6788  **/
6789 void
6790 gtk_text_view_move_child          (GtkTextView          *text_view,
6791                                    GtkWidget            *child,
6792                                    gint                  xpos,
6793                                    gint                  ypos)
6794 {
6795   GtkTextViewChild *vc;
6796
6797   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6798   g_return_if_fail (GTK_IS_WIDGET (child));
6799   g_return_if_fail (child->parent == (GtkWidget*) text_view);
6800
6801   vc = g_object_get_data (G_OBJECT (child),
6802                           "gtk-text-view-child");
6803
6804   g_assert (vc != NULL);
6805
6806   if (vc->x == xpos &&
6807       vc->y == ypos)
6808     return;
6809   
6810   vc->x = xpos;
6811   vc->y = ypos;
6812
6813   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
6814     gtk_widget_queue_resize (child);
6815 }
6816
6817
6818 /* Iterator operations */
6819
6820 gboolean
6821 gtk_text_view_forward_display_line (GtkTextView *text_view,
6822                                     GtkTextIter *iter)
6823 {
6824   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6825   g_return_val_if_fail (iter != NULL, FALSE);
6826
6827   gtk_text_view_ensure_layout (text_view);
6828
6829   return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
6830 }
6831
6832 gboolean
6833 gtk_text_view_backward_display_line (GtkTextView *text_view,
6834                                      GtkTextIter *iter)
6835 {
6836   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6837   g_return_val_if_fail (iter != NULL, FALSE);
6838
6839   gtk_text_view_ensure_layout (text_view);
6840
6841   return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
6842 }
6843
6844 gboolean
6845 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
6846                                         GtkTextIter *iter)
6847 {
6848   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6849   g_return_val_if_fail (iter != NULL, FALSE);
6850
6851   gtk_text_view_ensure_layout (text_view);
6852
6853   return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
6854 }
6855
6856 gboolean
6857 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
6858                                            GtkTextIter *iter)
6859 {
6860   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6861   g_return_val_if_fail (iter != NULL, FALSE);
6862
6863   gtk_text_view_ensure_layout (text_view);
6864
6865   return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
6866 }
6867
6868 gboolean
6869 gtk_text_view_starts_display_line (GtkTextView       *text_view,
6870                                    const GtkTextIter *iter)
6871 {
6872   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6873   g_return_val_if_fail (iter != NULL, FALSE);
6874
6875   gtk_text_view_ensure_layout (text_view);
6876
6877   return gtk_text_layout_iter_starts_line (text_view->layout, iter);
6878 }
6879
6880 gboolean
6881 gtk_text_view_move_visually (GtkTextView *text_view,
6882                              GtkTextIter *iter,
6883                              gint         count)
6884 {
6885   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6886   g_return_val_if_fail (iter != NULL, FALSE);
6887
6888   gtk_text_view_ensure_layout (text_view);
6889
6890   return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
6891 }