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