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