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