]> Pileus Git - ~andy/gtk/blob - gtk/gtktextview.c
fix Control-E so it doesn't move to a new line each time you press it
[~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         {
4120           if (!gtk_text_iter_ends_line (&newplace))
4121             gtk_text_iter_forward_to_line_end (&newplace);
4122         }
4123       else if (count < 0)
4124         {
4125           gtk_text_iter_set_line_offset (&newplace, 0);
4126         }
4127       break;
4128
4129     case GTK_MOVEMENT_BUFFER_ENDS:
4130       if (count > 0)
4131         gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4132       else if (count < 0)
4133         gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4134       break;
4135
4136     default:
4137       break;
4138     }
4139
4140   if (!gtk_text_iter_equal (&insert, &newplace))
4141     {
4142       if (extend_selection)
4143         gtk_text_buffer_move_mark (get_buffer (text_view),
4144                                    gtk_text_buffer_get_mark (get_buffer (text_view),
4145                                                              "insert"),
4146                                    &newplace);
4147       else
4148         gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4149
4150       DV(g_print (G_STRLOC": scrolling onscreen\n"));
4151       gtk_text_view_scroll_mark_onscreen (text_view,
4152                                           gtk_text_buffer_get_mark (get_buffer (text_view),
4153                                                                     "insert"));
4154
4155       if (step == GTK_MOVEMENT_DISPLAY_LINES)
4156         {
4157           gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4158         }
4159     }
4160
4161   gtk_text_view_pend_cursor_blink (text_view);
4162 }
4163
4164 static void
4165 gtk_text_view_set_anchor (GtkTextView *text_view)
4166 {
4167   GtkTextIter insert;
4168
4169   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4170                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4171                                                               "insert"));
4172
4173   gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4174 }
4175
4176 static void
4177 gtk_text_view_scroll_pages (GtkTextView *text_view,
4178                             gint         count)
4179 {
4180   gdouble newval;
4181   GtkAdjustment *adj;
4182   gint cursor_x_pos, cursor_y_pos;
4183   GtkTextIter new_insert;
4184   GtkTextIter anchor;
4185   gint y0, y1;
4186
4187   g_return_if_fail (text_view->vadjustment != NULL);
4188
4189   adj = text_view->vadjustment;
4190
4191   /* Validate the region that will be brought into view by the cursor motion
4192    */
4193   if (count < 0)
4194     {
4195       gtk_text_view_get_first_para_iter (text_view, &anchor);
4196       y0 = adj->page_size;
4197       y1 = adj->page_size + count * adj->page_increment;
4198     }
4199   else
4200     {
4201       gtk_text_view_get_first_para_iter (text_view, &anchor);
4202       y0 = count * adj->page_increment + adj->page_size;
4203       y1 = 0;
4204     }
4205
4206   gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4207   /* FIXME do we need to update the adjustment ranges here? */
4208   
4209   gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4210
4211   newval = adj->value;
4212
4213   newval += count * adj->page_increment;
4214
4215   cursor_y_pos += newval - adj->value;
4216   set_adjustment_clamped (adj, newval);
4217
4218   gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4219   clamp_iter_onscreen (text_view, &new_insert);
4220   gtk_text_buffer_place_cursor (get_buffer (text_view), &new_insert);
4221
4222   gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4223
4224   /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4225    * only guarantees 1 pixel onscreen.
4226    */
4227   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4228   gtk_text_view_scroll_mark_onscreen (text_view,
4229                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4230                                                                 "insert"));
4231 }
4232
4233 static gboolean
4234 whitespace (gunichar ch, gpointer user_data)
4235 {
4236   return (ch == ' ' || ch == '\t');
4237 }
4238
4239 static gboolean
4240 not_whitespace (gunichar ch, gpointer user_data)
4241 {
4242   return !whitespace (ch, user_data);
4243 }
4244
4245 static gboolean
4246 find_whitepace_region (const GtkTextIter *center,
4247                        GtkTextIter *start, GtkTextIter *end)
4248 {
4249   *start = *center;
4250   *end = *center;
4251
4252   if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4253     gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4254   if (whitespace (gtk_text_iter_get_char (end), NULL))
4255     gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4256
4257   return !gtk_text_iter_equal (start, end);
4258 }
4259
4260 static void
4261 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4262                                 const gchar *str)
4263 {
4264   gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4265                                                 text_view->editable);
4266 }
4267
4268 static void
4269 gtk_text_view_delete_from_cursor (GtkTextView   *text_view,
4270                                   GtkDeleteType  type,
4271                                   gint           count)
4272 {
4273   GtkTextIter insert;
4274   GtkTextIter start;
4275   GtkTextIter end;
4276   gboolean leave_one = FALSE;
4277
4278   gtk_text_view_reset_im_context (text_view);
4279
4280   if (type == GTK_DELETE_CHARS)
4281     {
4282       /* Char delete deletes the selection, if one exists */
4283       if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4284                                             text_view->editable))
4285         return;
4286     }
4287
4288   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4289                                     &insert,
4290                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4291                                                               "insert"));
4292
4293   start = insert;
4294   end = insert;
4295
4296   switch (type)
4297     {
4298     case GTK_DELETE_CHARS:
4299       gtk_text_iter_forward_cursor_positions (&end, count);
4300       break;
4301
4302     case GTK_DELETE_WORD_ENDS:
4303       if (count > 0)
4304         gtk_text_iter_forward_word_ends (&end, count);
4305       else if (count < 0)
4306         gtk_text_iter_backward_word_starts (&start, 0 - count);
4307       break;
4308
4309     case GTK_DELETE_WORDS:
4310       break;
4311
4312     case GTK_DELETE_DISPLAY_LINE_ENDS:
4313       break;
4314
4315     case GTK_DELETE_DISPLAY_LINES:
4316       break;
4317
4318     case GTK_DELETE_PARAGRAPH_ENDS:
4319       /* If we're already at a newline, we need to
4320        * simply delete that newline, instead of
4321        * moving to the next one.
4322        */
4323       if (gtk_text_iter_ends_line (&end))
4324         {
4325           gtk_text_iter_forward_line (&end);
4326           --count;
4327         }
4328
4329       while (count > 0)
4330         {
4331           if (!gtk_text_iter_forward_to_line_end (&end))
4332             break;
4333
4334           --count;
4335         }
4336
4337       /* FIXME figure out what a negative count means
4338          and support that */
4339       break;
4340
4341     case GTK_DELETE_PARAGRAPHS:
4342       if (count > 0)
4343         {
4344           gtk_text_iter_set_line_offset (&start, 0);
4345           gtk_text_iter_forward_to_line_end (&end);
4346
4347           /* Do the lines beyond the first. */
4348           while (count > 1)
4349             {
4350               gtk_text_iter_forward_to_line_end (&end);
4351
4352               --count;
4353             }
4354         }
4355
4356       /* FIXME negative count? */
4357
4358       break;
4359
4360     case GTK_DELETE_WHITESPACE:
4361       {
4362         find_whitepace_region (&insert, &start, &end);
4363       }
4364       break;
4365
4366     default:
4367       break;
4368     }
4369
4370   if (!gtk_text_iter_equal (&start, &end))
4371     {
4372       gtk_text_buffer_begin_user_action (get_buffer (text_view));
4373
4374       if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4375                                               text_view->editable))
4376         {
4377           if (leave_one)
4378             gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4379                                                           " ", 1,
4380                                                           text_view->editable);
4381         }
4382
4383       gtk_text_buffer_end_user_action (get_buffer (text_view));
4384
4385       DV(g_print (G_STRLOC": scrolling onscreen\n"));
4386       gtk_text_view_scroll_mark_onscreen (text_view,
4387                                           gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4388     }
4389 }
4390
4391 static void
4392 gtk_text_view_cut_clipboard (GtkTextView *text_view)
4393 {
4394   gtk_text_buffer_cut_clipboard (get_buffer (text_view),
4395                                  gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4396                                  text_view->editable);
4397   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4398   gtk_text_view_scroll_mark_onscreen (text_view,
4399                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4400                                                                 "insert"));
4401 }
4402
4403 static void
4404 gtk_text_view_copy_clipboard (GtkTextView *text_view)
4405 {
4406   gtk_text_buffer_copy_clipboard (get_buffer (text_view),
4407                                   gtk_clipboard_get (GDK_SELECTION_CLIPBOARD));
4408   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4409   gtk_text_view_scroll_mark_onscreen (text_view,
4410                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4411                                                                 "insert"));
4412 }
4413
4414 static void
4415 gtk_text_view_paste_clipboard (GtkTextView *text_view)
4416 {
4417   gtk_text_buffer_paste_clipboard (get_buffer (text_view),
4418                                    gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
4419                                    NULL,
4420                                    text_view->editable);
4421   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4422   gtk_text_view_scroll_mark_onscreen (text_view,
4423                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4424                                                                 "insert"));
4425 }
4426
4427 static void
4428 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
4429 {
4430   text_view->overwrite_mode = !text_view->overwrite_mode;
4431 }
4432
4433 /*
4434  * Selections
4435  */
4436
4437 static void
4438 gtk_text_view_unselect (GtkTextView *text_view)
4439 {
4440   GtkTextIter insert;
4441
4442   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4443                                     &insert,
4444                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4445                                                               "insert"));
4446
4447   gtk_text_buffer_move_mark (get_buffer (text_view),
4448                              gtk_text_buffer_get_mark (get_buffer (text_view),
4449                                                        "selection_bound"),
4450                              &insert);
4451 }
4452
4453 static void
4454 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
4455                                  const gchar *mark_name)
4456 {
4457   gint x, y;
4458   GdkModifierType state;
4459   GtkTextIter newplace;
4460
4461   /*   DV(g_print (G_STRLOC": begin\n")); */
4462   
4463   gdk_window_get_pointer (text_view->text_window->bin_window,
4464                           &x, &y, &state);
4465
4466   /*   DV(g_print (G_STRLOC": get iter at pixel\n"); */
4467   gtk_text_layout_get_iter_at_pixel (text_view->layout,
4468                                      &newplace,
4469                                      x + text_view->xoffset,
4470                                      y + text_view->yoffset);
4471
4472   {
4473     GtkTextMark *mark =
4474       gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
4475
4476     DV(g_print (G_STRLOC": move mark\n"));
4477     gtk_text_buffer_move_mark (get_buffer (text_view),
4478                                mark,
4479                                &newplace);
4480
4481     DV(g_print (G_STRLOC": scrolling onscreen\n"));
4482     gtk_text_view_scroll_mark_onscreen (text_view, mark);
4483   }
4484 }
4485
4486 static gint
4487 selection_scan_timeout (gpointer data)
4488 {
4489   GtkTextView *text_view;
4490
4491   GDK_THREADS_ENTER ();
4492   
4493   text_view = GTK_TEXT_VIEW (data);
4494
4495   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
4496   move_mark_to_pointer_and_scroll (text_view, "insert");
4497
4498   GDK_THREADS_LEAVE ();
4499   
4500   return TRUE; /* remain installed. */
4501 }
4502
4503 #define DND_SCROLL_MARGIN 0.20
4504
4505 static gint
4506 drag_scan_timeout (gpointer data)
4507 {
4508   GtkTextView *text_view;
4509   gint x, y;
4510   GdkModifierType state;
4511   GtkTextIter newplace;
4512
4513   GDK_THREADS_ENTER ();
4514   
4515   text_view = GTK_TEXT_VIEW (data);
4516
4517   gdk_window_get_pointer (text_view->text_window->bin_window,
4518                           &x, &y, &state);
4519   
4520   gtk_text_layout_get_iter_at_pixel (text_view->layout,
4521                                      &newplace,
4522                                      x + text_view->xoffset,
4523                                      y + text_view->yoffset);
4524   
4525   gtk_text_buffer_move_mark (get_buffer (text_view),
4526                              text_view->dnd_mark,
4527                              &newplace);
4528
4529   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4530   gtk_text_view_scroll_to_mark (text_view,
4531                                 text_view->dnd_mark,
4532                                 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
4533
4534   GDK_THREADS_LEAVE ();
4535   
4536   return TRUE;
4537 }
4538
4539 static gint
4540 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
4541 {
4542   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
4543   move_mark_to_pointer_and_scroll (text_view, "insert");
4544
4545   /* If we had to scroll offscreen, insert a timeout to do so
4546    * again. Note that in the timeout, even if the mouse doesn't
4547    * move, due to this scroll xoffset/yoffset will have changed
4548    * and we'll need to scroll again.
4549    */
4550   if (text_view->scroll_timeout != 0) /* reset on every motion event */
4551     gtk_timeout_remove (text_view->scroll_timeout);
4552   
4553   text_view->scroll_timeout =
4554     gtk_timeout_add (50, selection_scan_timeout, text_view);
4555
4556   return TRUE;
4557 }
4558
4559 static void
4560 gtk_text_view_start_selection_drag (GtkTextView       *text_view,
4561                                     const GtkTextIter *iter,
4562                                     GdkEventButton    *button)
4563 {
4564   GtkTextIter newplace;
4565
4566   g_return_if_fail (text_view->selection_drag_handler == 0);
4567
4568   gtk_grab_add (GTK_WIDGET (text_view));
4569
4570   newplace = *iter;
4571
4572   gtk_text_buffer_place_cursor (get_buffer (text_view), &newplace);
4573
4574   text_view->selection_drag_handler = gtk_signal_connect (GTK_OBJECT (text_view),
4575                                                           "motion_notify_event",
4576                                                           GTK_SIGNAL_FUNC (selection_motion_event_handler),
4577                                                           NULL);
4578 }
4579
4580 /* returns whether we were really dragging */
4581 static gboolean
4582 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
4583 {
4584   if (text_view->selection_drag_handler == 0)
4585     return FALSE;
4586
4587   gtk_signal_disconnect (GTK_OBJECT (text_view), text_view->selection_drag_handler);
4588   text_view->selection_drag_handler = 0;
4589
4590   if (text_view->scroll_timeout != 0)
4591     {
4592       gtk_timeout_remove (text_view->scroll_timeout);
4593       text_view->scroll_timeout = 0;
4594     }
4595
4596   /* one last update to current position */
4597   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
4598   move_mark_to_pointer_and_scroll (text_view, "insert");
4599
4600   gtk_grab_remove (GTK_WIDGET (text_view));
4601
4602   return TRUE;
4603 }
4604
4605 /*
4606  * Layout utils
4607  */
4608
4609 static void
4610 gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
4611                                          GtkTextAttributes  *values,
4612                                          GtkStyle           *style)
4613 {
4614   values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
4615   values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
4616
4617   if (values->font)
4618     pango_font_description_free (values->font);
4619
4620   values->font = pango_font_description_copy (style->font_desc);
4621 }
4622
4623 static void
4624 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
4625 {
4626   if (text_view->layout)
4627     {
4628       gboolean split_cursor;
4629       GtkTextDirection new_dir;
4630       GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4631   
4632       g_object_get (G_OBJECT (settings),
4633                     "gtk-split-cursor", &split_cursor,
4634                     NULL);
4635       if (split_cursor)
4636         new_dir = GTK_TEXT_DIR_NONE;
4637       else
4638         new_dir = (gdk_keymap_get_direction (gdk_keymap_get_default ()) == PANGO_DIRECTION_LTR) ?
4639           GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
4640       
4641       if (text_view->layout->cursor_direction != new_dir)
4642         gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
4643     }
4644 }
4645
4646 static void
4647 gtk_text_view_ensure_layout (GtkTextView *text_view)
4648 {
4649   GtkWidget *widget;
4650
4651   widget = GTK_WIDGET (text_view);
4652
4653   if (text_view->layout == NULL)
4654     {
4655       GtkTextAttributes *style;
4656       PangoContext *ltr_context, *rtl_context;
4657       GSList *tmp_list;
4658
4659       DV(g_print(G_STRLOC"\n"));
4660       
4661       text_view->layout = gtk_text_layout_new ();
4662
4663       g_signal_connect (G_OBJECT (text_view->layout),
4664                         "invalidated",
4665                         G_CALLBACK (invalidated_handler),
4666                         text_view);
4667
4668       g_signal_connect (G_OBJECT (text_view->layout),
4669                         "changed",
4670                         G_CALLBACK (changed_handler),
4671                         text_view);
4672
4673       g_signal_connect (G_OBJECT (text_view->layout),
4674                         "allocate_child",
4675                         G_CALLBACK (gtk_text_view_child_allocated),
4676                         text_view);
4677       
4678       if (get_buffer (text_view))
4679         gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
4680
4681       if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
4682         gtk_text_view_pend_cursor_blink (text_view);
4683       else
4684         gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4685
4686       ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
4687       pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
4688       rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
4689       pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
4690
4691       gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
4692
4693       g_object_unref (G_OBJECT (ltr_context));
4694       g_object_unref (G_OBJECT (rtl_context));
4695
4696       gtk_text_view_check_keymap_direction (text_view);
4697
4698       style = gtk_text_attributes_new ();
4699
4700       gtk_widget_ensure_style (widget);
4701       gtk_text_view_set_attributes_from_style (text_view,
4702                                                style, widget->style);
4703
4704       style->pixels_above_lines = text_view->pixels_above_lines;
4705       style->pixels_below_lines = text_view->pixels_below_lines;
4706       style->pixels_inside_wrap = text_view->pixels_inside_wrap;
4707       style->left_margin = text_view->left_margin;
4708       style->right_margin = text_view->right_margin;
4709       style->indent = text_view->indent;
4710       style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
4711
4712       style->wrap_mode = text_view->wrap_mode;
4713       style->justification = text_view->justify;
4714       style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
4715
4716       gtk_text_layout_set_default_style (text_view->layout, style);
4717
4718       gtk_text_attributes_unref (style);
4719
4720       /* Set layout for all anchored children */
4721
4722       tmp_list = text_view->children;
4723       while (tmp_list != NULL)
4724         {
4725           GtkTextViewChild *vc = tmp_list->data;
4726
4727           if (vc->anchor)
4728             {
4729               gtk_text_anchored_child_set_layout (vc->widget,
4730                                                   text_view->layout);
4731               /* vc may now be invalid! */
4732             }
4733
4734           tmp_list = g_slist_next (tmp_list);
4735         }
4736     }
4737 }
4738
4739 /**
4740  * gtk_text_view_get_default_attributes:
4741  * @text_view: a #GtkTextView
4742  * 
4743  * Obtains a copy of the default text attributes. These are the
4744  * attributes used for text unless a tag overrides them.
4745  * You'd typically pass the default attributes in to
4746  * gtk_text_tag_get_attributes() in order to get the
4747  * attributes in effect at a given text position.
4748  *
4749  * The return value is a copy owned by the caller of this function,
4750  * and should be freed.
4751  * 
4752  * Return value: a new #GtkTextAttributes
4753  **/
4754 GtkTextAttributes*
4755 gtk_text_view_get_default_attributes (GtkTextView *text_view)
4756 {
4757   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
4758   
4759   gtk_text_view_ensure_layout (text_view);
4760
4761   return gtk_text_attributes_copy (text_view->layout->default_style);
4762 }
4763
4764 static void
4765 gtk_text_view_destroy_layout (GtkTextView *text_view)
4766 {
4767   if (text_view->layout)
4768     {
4769       GSList *tmp_list;
4770
4771       gtk_text_view_remove_validate_idles (text_view);
4772       
4773       /* Remove layout from all anchored children */
4774       tmp_list = text_view->children;
4775       while (tmp_list != NULL)
4776         {
4777           GtkTextViewChild *vc = tmp_list->data;
4778
4779           if (vc->anchor)
4780             {
4781               gtk_text_anchored_child_set_layout (vc->widget, NULL);
4782               /* vc may now be invalid! */
4783             }
4784
4785           tmp_list = g_slist_next (tmp_list);
4786         }
4787       
4788       gtk_text_view_stop_cursor_blink (text_view);
4789       gtk_text_view_end_selection_drag (text_view, NULL);
4790
4791       g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
4792                                             invalidated_handler, text_view);
4793       g_signal_handlers_disconnect_by_func (G_OBJECT (text_view->layout),
4794                                             changed_handler, text_view);
4795       g_object_unref (G_OBJECT (text_view->layout));
4796       text_view->layout = NULL;
4797     }
4798 }
4799
4800 static void
4801 gtk_text_view_reset_im_context (GtkTextView *text_view)
4802 {
4803   if (text_view->need_im_reset)
4804     {
4805       text_view->need_im_reset = FALSE;
4806       gtk_im_context_reset (text_view->im_context);
4807     }
4808 }
4809
4810 /*
4811  * DND feature
4812  */
4813
4814 static void
4815 gtk_text_view_start_selection_dnd (GtkTextView       *text_view,
4816                                    const GtkTextIter *iter,
4817                                    GdkEventMotion    *event)
4818 {
4819   GdkDragContext *context;
4820   GtkTargetList *target_list;
4821
4822   text_view->drag_start_x = -1;
4823   text_view->drag_start_y = -1;
4824   text_view->pending_place_cursor_button = 0;
4825   
4826   target_list = gtk_target_list_new (target_table,
4827                                      G_N_ELEMENTS (target_table));
4828
4829   context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
4830                             GDK_ACTION_COPY | GDK_ACTION_MOVE,
4831                             1, (GdkEvent*)event);
4832
4833   gtk_target_list_unref (target_list);
4834
4835   gtk_drag_set_icon_default (context);
4836 }
4837
4838 static void
4839 gtk_text_view_drag_begin (GtkWidget        *widget,
4840                           GdkDragContext   *context)
4841 {
4842   /* do nothing */
4843 }
4844
4845 static void
4846 gtk_text_view_drag_end (GtkWidget        *widget,
4847                         GdkDragContext   *context)
4848 {
4849   GtkTextView *text_view;
4850
4851   text_view = GTK_TEXT_VIEW (widget);
4852 }
4853
4854 static void
4855 gtk_text_view_drag_data_get (GtkWidget        *widget,
4856                              GdkDragContext   *context,
4857                              GtkSelectionData *selection_data,
4858                              guint             info,
4859                              guint             time)
4860 {
4861   GtkTextView *text_view;
4862
4863   text_view = GTK_TEXT_VIEW (widget);
4864
4865   if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
4866     {
4867       GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
4868
4869       gtk_selection_data_set (selection_data,
4870                               gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
4871                               8, /* bytes */
4872                               (void*)&buffer,
4873                               sizeof (buffer));
4874     }
4875   else
4876     {
4877       gchar *str;
4878       GtkTextIter start;
4879       GtkTextIter end;
4880
4881       str = NULL;
4882
4883       if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4884                                                 &start, &end))
4885         {
4886           /* Extract the selected text */
4887           str = gtk_text_iter_get_visible_text (&start, &end);
4888         }
4889
4890       if (str)
4891         {
4892           gtk_selection_data_set_text (selection_data, str, -1);
4893           g_free (str);
4894         }
4895     }
4896 }
4897
4898 static void
4899 gtk_text_view_drag_data_delete (GtkWidget        *widget,
4900                                 GdkDragContext   *context)
4901 {
4902   GtkTextView *text_view;
4903
4904   text_view = GTK_TEXT_VIEW (widget);
4905
4906   gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
4907                                     TRUE, GTK_TEXT_VIEW (widget)->editable);
4908 }
4909
4910 static void
4911 gtk_text_view_drag_leave (GtkWidget        *widget,
4912                           GdkDragContext   *context,
4913                           guint             time)
4914 {
4915   GtkTextView *text_view;
4916
4917   text_view = GTK_TEXT_VIEW (widget);
4918
4919   gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
4920   
4921   if (text_view->scroll_timeout != 0)
4922     gtk_timeout_remove (text_view->scroll_timeout);
4923
4924   text_view->scroll_timeout = 0;
4925 }
4926
4927 static gboolean
4928 gtk_text_view_drag_motion (GtkWidget        *widget,
4929                            GdkDragContext   *context,
4930                            gint              x,
4931                            gint              y,
4932                            guint             time)
4933 {
4934   GtkTextIter newplace;
4935   GtkTextView *text_view;
4936   GtkTextIter start;
4937   GtkTextIter end;
4938   GdkRectangle target_rect;
4939   gint bx, by;
4940   GdkDragAction suggested_action = 0;
4941   
4942   text_view = GTK_TEXT_VIEW (widget);
4943
4944   target_rect = text_view->text_window->allocation;
4945   
4946   if (x < target_rect.x ||
4947       y < target_rect.y ||
4948       x > (target_rect.x + target_rect.width) ||
4949       y > (target_rect.y + target_rect.height))
4950     return FALSE; /* outside the text window, allow parent widgets to handle event */
4951
4952   gtk_text_view_window_to_buffer_coords (text_view,
4953                                          GTK_TEXT_WINDOW_WIDGET,
4954                                          x, y,
4955                                          &bx, &by);
4956
4957   gtk_text_layout_get_iter_at_pixel (text_view->layout,
4958                                      &newplace,
4959                                      bx, by);  
4960
4961   if (gtk_drag_dest_find_target (widget, context,
4962                                  gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
4963     {
4964       /* can't accept any of the offered targets */
4965     }                                 
4966   else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
4967                                             &start, &end) &&
4968            gtk_text_iter_in_range (&newplace, &start, &end))
4969     {
4970       /* We're inside the selection. */
4971     }
4972   else
4973     {      
4974       if (gtk_text_iter_can_insert (&newplace, text_view->editable))
4975         {
4976           GtkWidget *source_widget;
4977           
4978           suggested_action = context->suggested_action;
4979           
4980           source_widget = gtk_drag_get_source_widget (context);
4981           
4982           if (source_widget == widget)
4983             {
4984               /* Default to MOVE, unless the user has
4985                * pressed ctrl or alt to affect available actions
4986                */
4987               if ((context->actions & GDK_ACTION_MOVE) != 0)
4988                 suggested_action = GDK_ACTION_MOVE;
4989             }
4990         }
4991       else
4992         {
4993           /* Can't drop here. */
4994         }
4995     }
4996
4997   if (suggested_action != 0)
4998     {
4999       gtk_text_mark_set_visible (text_view->dnd_mark,
5000                                  text_view->cursor_visible);
5001       
5002       gdk_drag_status (context, suggested_action, time);
5003     }
5004   else
5005     {
5006       gdk_drag_status (context, 0, time);
5007       gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5008     }
5009       
5010   gtk_text_buffer_move_mark (get_buffer (text_view),
5011                              text_view->dnd_mark,
5012                              &newplace);
5013
5014   DV(g_print (G_STRLOC": scrolling to mark\n"));
5015   gtk_text_view_scroll_to_mark (text_view,
5016                                 text_view->dnd_mark,
5017                                 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5018   
5019   if (text_view->scroll_timeout != 0) /* reset on every motion event */
5020     gtk_timeout_remove (text_view->scroll_timeout);
5021       
5022   text_view->scroll_timeout =
5023     gtk_timeout_add (50, drag_scan_timeout, text_view);
5024
5025   /* TRUE return means don't propagate the drag motion to parent
5026    * widgets that may also be drop sites.
5027    */
5028   return TRUE;
5029 }
5030
5031 static gboolean
5032 gtk_text_view_drag_drop (GtkWidget        *widget,
5033                          GdkDragContext   *context,
5034                          gint              x,
5035                          gint              y,
5036                          guint             time)
5037 {
5038   GtkTextView *text_view;
5039   GtkTextIter drop_point;
5040   GdkAtom target = GDK_NONE;
5041   
5042   text_view = GTK_TEXT_VIEW (widget);
5043   
5044   if (text_view->scroll_timeout != 0)
5045     gtk_timeout_remove (text_view->scroll_timeout);
5046
5047   text_view->scroll_timeout = 0;
5048
5049   gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5050
5051   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5052                                     &drop_point,
5053                                     text_view->dnd_mark);
5054
5055   if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5056     target = gtk_drag_dest_find_target (widget, context, NULL);
5057
5058   if (target != GDK_NONE)
5059     gtk_drag_get_data (widget, context, target, time);
5060   else
5061     gtk_drag_finish (context, FALSE, FALSE, time);
5062
5063   return TRUE;
5064 }
5065
5066 static void
5067 insert_text_data (GtkTextView      *text_view,
5068                   GtkTextIter      *drop_point,
5069                   GtkSelectionData *selection_data)
5070 {
5071   gchar *str;
5072
5073   str = gtk_selection_data_get_text (selection_data);
5074
5075   if (str)
5076     {
5077       gtk_text_buffer_insert_interactive (get_buffer (text_view),
5078                                           drop_point, str, -1,
5079                                           text_view->editable);
5080       g_free (str);
5081     }
5082 }
5083
5084 static void
5085 gtk_text_view_drag_data_received (GtkWidget        *widget,
5086                                   GdkDragContext   *context,
5087                                   gint              x,
5088                                   gint              y,
5089                                   GtkSelectionData *selection_data,
5090                                   guint             info,
5091                                   guint             time)
5092 {
5093   GtkTextIter drop_point;
5094   GtkTextView *text_view;
5095   gboolean success = FALSE;
5096
5097   text_view = GTK_TEXT_VIEW (widget);
5098
5099   if (!text_view->dnd_mark)
5100     goto done;
5101
5102   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5103                                     &drop_point,
5104                                     text_view->dnd_mark);
5105   
5106   if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5107     goto done;
5108
5109   if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5110     {
5111       GtkTextBuffer *src_buffer = NULL;
5112       GtkTextIter start, end;
5113       gboolean copy_tags = TRUE;
5114
5115       if (selection_data->length != sizeof (src_buffer))
5116         return;
5117
5118       memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5119
5120       if (src_buffer == NULL)
5121         return;
5122
5123       g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5124
5125       if (gtk_text_buffer_get_tag_table (src_buffer) !=
5126           gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5127         copy_tags = FALSE;
5128
5129       if (gtk_text_buffer_get_selection_bounds (src_buffer,
5130                                                 &start,
5131                                                 &end))
5132         {
5133           if (copy_tags)
5134             gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5135                                                       &drop_point,
5136                                                       &start,
5137                                                       &end,
5138                                                       text_view->editable);
5139           else
5140             {
5141               gchar *str;
5142
5143               str = gtk_text_iter_get_visible_text (&start, &end);
5144               gtk_text_buffer_insert_interactive (get_buffer (text_view),
5145                                                   &drop_point, str, -1,
5146                                                   text_view->editable);
5147               g_free (str);
5148             }
5149         }
5150     }
5151   else
5152     insert_text_data (text_view, &drop_point, selection_data);
5153
5154   success = TRUE;
5155
5156  done:
5157   gtk_drag_finish (context, success,
5158                    success && context->action == GDK_ACTION_MOVE,
5159                    time);
5160 }
5161
5162 static GtkAdjustment*
5163 get_hadjustment (GtkTextView *text_view)
5164 {
5165   if (text_view->hadjustment == NULL)
5166     gtk_text_view_set_scroll_adjustments (text_view,
5167                                           NULL, /* forces creation */
5168                                           text_view->vadjustment);
5169
5170   return text_view->hadjustment;
5171 }
5172
5173 static GtkAdjustment*
5174 get_vadjustment (GtkTextView *text_view)
5175 {
5176   if (text_view->vadjustment == NULL)
5177     gtk_text_view_set_scroll_adjustments (text_view,
5178                                           text_view->hadjustment,
5179                                           NULL); /* forces creation */
5180   return text_view->vadjustment;
5181 }
5182
5183
5184 static void
5185 gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
5186                                       GtkAdjustment *hadj,
5187                                       GtkAdjustment *vadj)
5188 {
5189   gboolean need_adjust = FALSE;
5190
5191   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5192
5193   if (hadj)
5194     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5195   else
5196     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5197   if (vadj)
5198     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5199   else
5200     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5201
5202   if (text_view->hadjustment && (text_view->hadjustment != hadj))
5203     {
5204       gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->hadjustment), text_view);
5205       g_object_unref (G_OBJECT (text_view->hadjustment));
5206     }
5207
5208   if (text_view->vadjustment && (text_view->vadjustment != vadj))
5209     {
5210       gtk_signal_disconnect_by_data (GTK_OBJECT (text_view->vadjustment), text_view);
5211       g_object_unref (G_OBJECT (text_view->vadjustment));
5212     }
5213
5214   if (text_view->hadjustment != hadj)
5215     {
5216       text_view->hadjustment = hadj;
5217       g_object_ref (G_OBJECT (text_view->hadjustment));
5218       gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5219       
5220       gtk_signal_connect (GTK_OBJECT (text_view->hadjustment), "value_changed",
5221                           (GtkSignalFunc) gtk_text_view_value_changed,
5222                           text_view);
5223       need_adjust = TRUE;
5224     }
5225
5226   if (text_view->vadjustment != vadj)
5227     {
5228       text_view->vadjustment = vadj;
5229       g_object_ref (G_OBJECT (text_view->vadjustment));
5230       gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5231       
5232       gtk_signal_connect (GTK_OBJECT (text_view->vadjustment), "value_changed",
5233                           (GtkSignalFunc) gtk_text_view_value_changed,
5234                           text_view);
5235       need_adjust = TRUE;
5236     }
5237
5238   if (need_adjust)
5239     gtk_text_view_value_changed (NULL, text_view);
5240 }
5241
5242 static void
5243 gtk_text_view_value_changed (GtkAdjustment *adj,
5244                              GtkTextView   *text_view)
5245 {
5246   GtkTextIter iter;
5247   gint line_top;
5248   gint dx = 0;
5249   gint dy = 0;
5250   
5251   /* Note that we oddly call this function with adj == NULL
5252    * sometimes
5253    */
5254   
5255   text_view->onscreen_validated = FALSE;
5256
5257   DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
5258              adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
5259              adj ? adj->value : 0.0));
5260   
5261   if (adj == text_view->hadjustment)
5262     {
5263       dx = text_view->xoffset - (gint)adj->value;
5264       text_view->xoffset = adj->value;
5265     }
5266   else if (adj == text_view->vadjustment)
5267     {
5268       dy = text_view->yoffset - (gint)adj->value;
5269       text_view->yoffset = adj->value;
5270
5271       if (text_view->layout)
5272         {
5273           gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
5274
5275           gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
5276
5277           text_view->first_para_pixels = adj->value - line_top;
5278         }
5279     }
5280   
5281   if (dx != 0 || dy != 0)
5282     {
5283       GSList *tmp_list;
5284
5285       if (GTK_WIDGET_REALIZED (text_view))
5286         {
5287           if (dy != 0)
5288             {
5289               if (text_view->left_window)
5290                 text_window_scroll (text_view->left_window, 0, dy);
5291               if (text_view->right_window)
5292                 text_window_scroll (text_view->right_window, 0, dy);
5293             }
5294       
5295           if (dx != 0)
5296             {
5297               if (text_view->top_window)
5298                 text_window_scroll (text_view->top_window, dx, 0);
5299               if (text_view->bottom_window)
5300                 text_window_scroll (text_view->bottom_window, dx, 0);
5301             }
5302       
5303           /* It looks nicer to scroll the main area last, because
5304            * it takes a while, and making the side areas update
5305            * afterward emphasizes the slowness of scrolling the
5306            * main area.
5307            */
5308           text_window_scroll (text_view->text_window, dx, dy);
5309         }
5310       
5311       /* Children are now "moved" in the text window, poke
5312        * into widget->allocation for each child
5313        */
5314       tmp_list = text_view->children;
5315       while (tmp_list != NULL)
5316         {
5317           GtkTextViewChild *child = tmp_list->data;
5318           
5319           if (child->anchor)
5320             {              
5321               child->widget->allocation.x -= dx;
5322               child->widget->allocation.y -= dy;
5323
5324 #if 0
5325               g_print ("allocation for %p tweaked to %d,%d\n",
5326                        child->widget,
5327                        child->widget->allocation.x,
5328                        child->widget->allocation.y);
5329 #endif
5330             }
5331           
5332           tmp_list = g_slist_next (tmp_list);
5333         }
5334     }
5335
5336   /* This could result in invalidation, which would install the
5337    * first_validate_idle, which would validate onscreen;
5338    * but we're going to go ahead and validate here, so
5339    * first_validate_idle shouldn't have anything to do.
5340    */
5341   gtk_text_view_update_layout_width (text_view);
5342   
5343   /* note that validation of onscreen could invoke this function
5344    * recursively, by scrolling to maintain first_para, or in response
5345    * to updating the layout width, however there is no problem with
5346    * that, or shouldn't be.
5347    */
5348   gtk_text_view_validate_onscreen (text_view);
5349   
5350   /* process exposes */
5351   if (GTK_WIDGET_REALIZED (text_view))
5352     {
5353       if (text_view->left_window)
5354         gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
5355
5356       if (text_view->right_window)
5357         gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
5358
5359       if (text_view->top_window)
5360         gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
5361       
5362       if (text_view->bottom_window)
5363         gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
5364   
5365       gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
5366     }
5367
5368   /* If this got installed, get rid of it, it's just a waste of time. */
5369   if (text_view->first_validate_idle != 0)
5370     {
5371       g_source_remove (text_view->first_validate_idle);
5372       text_view->first_validate_idle = 0;
5373     }
5374
5375   gtk_text_view_update_im_spot_location (text_view);
5376   
5377   DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
5378 }
5379
5380 static void
5381 gtk_text_view_commit_handler (GtkIMContext  *context,
5382                               const gchar   *str,
5383                               GtkTextView   *text_view)
5384 {
5385   gtk_text_view_commit_text (text_view, str);
5386 }
5387
5388 static void
5389 gtk_text_view_commit_text (GtkTextView   *text_view,
5390                            const gchar   *str)
5391 {
5392   gboolean had_selection;
5393   
5394   gtk_text_buffer_begin_user_action (get_buffer (text_view));
5395
5396   had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5397                                                         NULL, NULL);
5398   
5399   gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
5400                                     text_view->editable);
5401
5402   if (!strcmp (str, "\n"))
5403     {
5404       gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
5405                                                     text_view->editable);
5406     }
5407   else
5408     {
5409       if (!had_selection && text_view->overwrite_mode)
5410         gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
5411       gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
5412                                                     text_view->editable);
5413     }
5414
5415   gtk_text_buffer_end_user_action (get_buffer (text_view));
5416
5417   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5418   gtk_text_view_scroll_mark_onscreen (text_view,
5419                                       gtk_text_buffer_get_mark (get_buffer (text_view),
5420                                                                 "insert"));
5421 }
5422
5423 static void
5424 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
5425                                        GtkTextView  *text_view)
5426 {
5427   gchar *str;
5428   PangoAttrList *attrs;
5429   gint cursor_pos;
5430
5431   gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
5432   gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
5433
5434   pango_attr_list_unref (attrs);
5435   g_free (str);
5436 }
5437
5438 static gboolean
5439 gtk_text_view_retrieve_surrounding_handler (GtkIMContext  *context,
5440                                             GtkTextView   *text_view)
5441 {
5442   GtkTextIter start;
5443   GtkTextIter end;
5444   gint pos;
5445   gchar *text;
5446
5447   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
5448                                     gtk_text_buffer_get_insert (text_view->buffer));
5449   end = start;
5450
5451   pos = gtk_text_iter_get_line_index (&start);
5452   gtk_text_iter_set_line_offset (&start, 0);
5453   gtk_text_iter_forward_to_line_end (&end);
5454
5455   text = gtk_text_iter_get_slice (&start, &end);
5456   gtk_im_context_set_surrounding (context, text, -1, pos);
5457   g_free (text);
5458
5459   return TRUE;
5460 }
5461
5462 static gboolean
5463 gtk_text_view_delete_surrounding_handler (GtkIMContext  *context,
5464                                           gint           offset,
5465                                           gint           n_chars,
5466                                           GtkTextView   *text_view)
5467 {
5468   GtkTextIter start;
5469   GtkTextIter end;
5470
5471   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
5472                                     gtk_text_buffer_get_insert (text_view->buffer));
5473   end = start;
5474
5475   gtk_text_iter_forward_chars (&start, offset);
5476   gtk_text_iter_forward_chars (&end, offset + n_chars);
5477
5478   gtk_text_buffer_delete (text_view->buffer, &start, &end);
5479
5480   return TRUE;
5481 }
5482
5483 static void
5484 gtk_text_view_mark_set_handler (GtkTextBuffer     *buffer,
5485                                 const GtkTextIter *location,
5486                                 GtkTextMark       *mark,
5487                                 gpointer           data)
5488 {
5489   GtkTextView *text_view = GTK_TEXT_VIEW (data);
5490   gboolean need_reset = FALSE;
5491
5492   if (mark == gtk_text_buffer_get_insert (buffer))
5493     {
5494       text_view->virtual_cursor_x = -1;
5495       text_view->virtual_cursor_y = -1;
5496       gtk_text_view_update_im_spot_location (text_view);
5497       need_reset = TRUE;
5498     }
5499   else if (mark == gtk_text_buffer_get_selection_bound (buffer))
5500     {
5501       need_reset = TRUE;
5502     }
5503
5504   if (need_reset)
5505     gtk_text_view_reset_im_context (text_view);
5506 }
5507
5508 static void
5509 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
5510                                       gint        *x,
5511                                       gint        *y)
5512 {
5513   GdkRectangle strong_pos;
5514   GtkTextIter insert;
5515
5516   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5517                                     gtk_text_buffer_get_mark (get_buffer (text_view),
5518                                                               "insert"));
5519
5520   if ((x && text_view->virtual_cursor_x == -1) ||
5521       (y && text_view->virtual_cursor_y == -1))
5522     gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
5523
5524   if (x)
5525     {
5526       if (text_view->virtual_cursor_x != -1)
5527         *x = text_view->virtual_cursor_x;
5528       else
5529         *x = strong_pos.x;
5530     }
5531
5532   if (y)
5533     {
5534       if (text_view->virtual_cursor_x != -1)
5535         *y = text_view->virtual_cursor_y;
5536       else
5537         *y = strong_pos.y + strong_pos.height / 2;
5538     }
5539 }
5540
5541 static void
5542 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
5543                                       gint         x,
5544                                       gint         y)
5545 {
5546   GdkRectangle strong_pos;
5547   GtkTextIter insert;
5548
5549   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
5550                                     gtk_text_buffer_get_mark (get_buffer (text_view),
5551                                                               "insert"));
5552
5553   if (x == -1 || y == -1)
5554     gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
5555
5556   text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
5557   text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
5558 }
5559
5560 /* Quick hack of a popup menu
5561  */
5562 static void
5563 activate_cb (GtkWidget   *menuitem,
5564              GtkTextView *text_view)
5565 {
5566   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
5567   gtk_signal_emit_by_name (GTK_OBJECT (text_view), signal);
5568 }
5569
5570 static void
5571 append_action_signal (GtkTextView  *text_view,
5572                       GtkWidget    *menu,
5573                       const gchar  *stock_id,
5574                       const gchar  *signal,
5575                       gboolean      sensitive)
5576 {
5577   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
5578
5579   g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
5580   gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
5581                       GTK_SIGNAL_FUNC (activate_cb), text_view);
5582
5583   gtk_widget_set_sensitive (menuitem, sensitive);
5584   
5585   gtk_widget_show (menuitem);
5586   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
5587 }
5588
5589 static void
5590 popup_menu_detach (GtkWidget *attach_widget,
5591                    GtkMenu   *menu)
5592 {
5593   GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
5594 }
5595
5596 static void
5597 popup_position_func (GtkMenu   *menu,
5598                      gint      *x,
5599                      gint      *y,
5600                      gboolean  *push_in,
5601                      gpointer   user_data)
5602 {
5603   GtkTextView *text_view;
5604   GtkWidget *widget;
5605   GdkRectangle cursor_rect;
5606   GdkRectangle onscreen_rect;
5607   gint root_x, root_y;
5608   GtkTextIter iter;
5609   GtkRequisition req;      
5610   
5611   text_view = GTK_TEXT_VIEW (user_data);
5612   widget = GTK_WIDGET (text_view);
5613   
5614   g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
5615
5616   gdk_window_get_origin (widget->window, &root_x, &root_y);
5617
5618   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5619                                     &iter,
5620                                     gtk_text_buffer_get_insert (get_buffer (text_view)));
5621
5622   gtk_text_view_get_iter_location (text_view,
5623                                    &iter,
5624                                    &cursor_rect);
5625
5626   gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
5627   
5628   gtk_widget_size_request (text_view->popup_menu, &req);
5629
5630   /* can't use rectangle_intersect since cursor rect can have 0 width */
5631   if (cursor_rect.x >= onscreen_rect.x &&
5632       cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
5633       cursor_rect.y >= onscreen_rect.y &&
5634       cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
5635     {    
5636       gtk_text_view_buffer_to_window_coords (text_view,
5637                                              GTK_TEXT_WINDOW_WIDGET,
5638                                              cursor_rect.x, cursor_rect.y,
5639                                              &cursor_rect.x, &cursor_rect.y);
5640
5641       *x = root_x + cursor_rect.x + cursor_rect.width;
5642       *y = root_y + cursor_rect.y + cursor_rect.height;
5643     }
5644   else
5645     {
5646       /* Just center the menu, since cursor is offscreen. */      
5647       *x = root_x + (widget->allocation.width / 2 - req.width / 2);
5648       *y = root_y + (widget->allocation.height / 2 - req.height / 2);      
5649     }
5650
5651   /* Ensure sanity */
5652   *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
5653   *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
5654
5655   *x = CLAMP (*x, 0, MAX (0, gdk_screen_width () - req.width));
5656   *y = CLAMP (*y, 0, MAX (0, gdk_screen_height () - req.height));
5657 }
5658
5659 typedef struct
5660 {
5661   GtkTextView *text_view;
5662   gint button;
5663   guint time;
5664 } PopupInfo;
5665
5666 static gboolean
5667 range_contains_editable_text (const GtkTextIter *start,
5668                               const GtkTextIter *end,
5669                               gboolean default_editability)
5670 {
5671   GtkTextIter iter = *start;
5672
5673   while (gtk_text_iter_compare (&iter, end) < 0)
5674     {
5675       if (gtk_text_iter_editable (&iter, default_editability))
5676         return TRUE;
5677       
5678       gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
5679     }
5680
5681   return FALSE;
5682 }                             
5683
5684 static void
5685 popup_targets_received (GtkClipboard     *clipboard,
5686                         GtkSelectionData *data,
5687                         gpointer          user_data)
5688 {
5689   PopupInfo *info = user_data;
5690   GtkTextView *text_view = info->text_view;
5691   
5692   if (GTK_WIDGET_REALIZED (text_view))
5693     {
5694       /* We implicitely rely here on the fact that if we are pasting ourself, we'll
5695        * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
5696        */
5697       gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
5698       GtkWidget *menuitem;
5699       GtkWidget *submenu;
5700       gboolean have_selection;
5701       gboolean can_insert;
5702       GtkTextIter iter;
5703       GtkTextIter sel_start, sel_end;
5704       
5705       if (text_view->popup_menu)
5706         gtk_widget_destroy (text_view->popup_menu);
5707
5708       text_view->popup_menu = gtk_menu_new ();
5709       
5710       gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
5711                                  GTK_WIDGET (text_view),
5712                                  popup_menu_detach);
5713       
5714       have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5715                                                              &sel_start, &sel_end);
5716       
5717       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5718                                         &iter,
5719                                         gtk_text_buffer_get_insert (get_buffer (text_view)));
5720       
5721       can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
5722       
5723       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
5724                             have_selection &&
5725                             range_contains_editable_text (&sel_start, &sel_end,
5726                                                           text_view->editable));
5727       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
5728                             have_selection);
5729       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
5730                             can_insert && clipboard_contains_text);
5731       
5732       menuitem = gtk_separator_menu_item_new ();
5733       gtk_widget_show (menuitem);
5734       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
5735       
5736       menuitem = gtk_menu_item_new_with_label (_("Input Methods"));
5737       gtk_widget_show (menuitem);
5738       submenu = gtk_menu_new ();
5739       gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
5740       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
5741       
5742       gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
5743                                             GTK_MENU_SHELL (submenu));
5744       
5745       gtk_signal_emit (GTK_OBJECT (text_view),
5746                        signals[POPULATE_POPUP],
5747                        text_view->popup_menu);
5748       
5749       if (info->button)
5750         gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
5751                         NULL, NULL,
5752                         info->button, info->time);
5753       else
5754         gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
5755                         popup_position_func, text_view,
5756                         0, gtk_get_current_event_time ());
5757     }
5758
5759   g_object_unref (text_view);
5760   g_free (info);
5761 }
5762
5763 static void
5764 gtk_text_view_do_popup (GtkTextView    *text_view,
5765                         GdkEventButton *event)
5766 {
5767   PopupInfo *info = g_new (PopupInfo, 1);
5768
5769   /* In order to know what entries we should make sensitive, we
5770    * ask for the current targets of the clipboard, and when
5771    * we get them, then we actually pop up the menu.
5772    */
5773   info->text_view = g_object_ref (text_view);
5774   
5775   if (event)
5776     {
5777       info->button = event->button;
5778       info->time = event->time;
5779     }
5780   else
5781     {
5782       info->button = 0;
5783       info->time = gtk_get_current_event_time ();
5784     }
5785
5786   gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
5787                                   gdk_atom_intern ("TARGETS", FALSE),
5788                                   popup_targets_received,
5789                                   info);
5790 }
5791
5792 static void
5793 gtk_text_view_popup_menu (GtkWidget *widget)
5794 {
5795   gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);  
5796 }
5797
5798 /* Child GdkWindows */
5799
5800
5801 static GtkTextWindow*
5802 text_window_new (GtkTextWindowType  type,
5803                  GtkWidget         *widget,
5804                  gint               width_request,
5805                  gint               height_request)
5806 {
5807   GtkTextWindow *win;
5808
5809   win = g_new (GtkTextWindow, 1);
5810
5811   win->type = type;
5812   win->widget = widget;
5813   win->window = NULL;
5814   win->bin_window = NULL;
5815   win->requisition.width = width_request;
5816   win->requisition.height = height_request;
5817   win->allocation.width = width_request;
5818   win->allocation.height = height_request;
5819   win->allocation.x = 0;
5820   win->allocation.y = 0;
5821
5822   return win;
5823 }
5824
5825 static void
5826 text_window_free (GtkTextWindow *win)
5827 {
5828   if (win->window)
5829     text_window_unrealize (win);
5830
5831   g_free (win);
5832 }
5833
5834 static void
5835 text_window_realize (GtkTextWindow *win,
5836                      GdkWindow     *parent)
5837 {
5838   GdkWindowAttr attributes;
5839   gint attributes_mask;
5840   GdkCursor *cursor;
5841
5842   attributes.window_type = GDK_WINDOW_CHILD;
5843   attributes.x = win->allocation.x;
5844   attributes.y = win->allocation.y;
5845   attributes.width = win->allocation.width;
5846   attributes.height = win->allocation.height;
5847   attributes.wclass = GDK_INPUT_OUTPUT;
5848   attributes.visual = gtk_widget_get_visual (win->widget);
5849   attributes.colormap = gtk_widget_get_colormap (win->widget);
5850   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
5851
5852   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
5853
5854   win->window = gdk_window_new (parent,
5855                                 &attributes,
5856                                 attributes_mask);
5857
5858   gdk_window_show (win->window);
5859   gdk_window_set_user_data (win->window, win->widget);
5860
5861   attributes.x = 0;
5862   attributes.y = 0;
5863   attributes.width = win->allocation.width;
5864   attributes.height = win->allocation.height;
5865   attributes.event_mask = (GDK_EXPOSURE_MASK            |
5866                            GDK_SCROLL_MASK              |
5867                            GDK_KEY_PRESS_MASK           |
5868                            GDK_BUTTON_PRESS_MASK        |
5869                            GDK_BUTTON_RELEASE_MASK      |
5870                            GDK_POINTER_MOTION_MASK      |
5871                            GDK_POINTER_MOTION_HINT_MASK |
5872                            gtk_widget_get_events (win->widget));
5873
5874   win->bin_window = gdk_window_new (win->window,
5875                                     &attributes,
5876                                     attributes_mask);
5877
5878   gdk_window_show (win->bin_window);
5879   gdk_window_set_user_data (win->bin_window, win->widget);
5880
5881   if (win->type == GTK_TEXT_WINDOW_TEXT)
5882     {
5883       /* I-beam cursor */
5884       cursor = gdk_cursor_new (GDK_XTERM);
5885       gdk_window_set_cursor (win->bin_window, cursor);
5886       gdk_cursor_destroy (cursor);
5887
5888       gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
5889                                         win->window);
5890
5891
5892       gdk_window_set_background (win->bin_window,
5893                                  &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
5894     }
5895   else
5896     {
5897       gdk_window_set_background (win->bin_window,
5898                                  &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
5899     }
5900
5901   g_object_set_qdata (G_OBJECT (win->window),
5902                       g_quark_from_static_string ("gtk-text-view-text-window"),
5903                       win);
5904
5905   g_object_set_qdata (G_OBJECT (win->bin_window),
5906                       g_quark_from_static_string ("gtk-text-view-text-window"),
5907                       win);
5908 }
5909
5910 static void
5911 text_window_unrealize (GtkTextWindow *win)
5912 {
5913   if (win->type == GTK_TEXT_WINDOW_TEXT)
5914     {
5915       gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
5916                                         NULL);
5917     }
5918
5919   gdk_window_set_user_data (win->window, NULL);
5920   gdk_window_set_user_data (win->bin_window, NULL);
5921   gdk_window_destroy (win->bin_window);
5922   gdk_window_destroy (win->window);
5923   win->window = NULL;
5924   win->bin_window = NULL;
5925 }
5926
5927 static void
5928 text_window_size_allocate (GtkTextWindow *win,
5929                            GdkRectangle  *rect)
5930 {
5931   win->allocation = *rect;
5932
5933   if (win->window)
5934     {
5935       gdk_window_move_resize (win->window,
5936                               rect->x, rect->y,
5937                               rect->width, rect->height);
5938
5939       gdk_window_resize (win->bin_window,
5940                          rect->width, rect->height);
5941     }
5942 }
5943
5944 static void
5945 text_window_scroll        (GtkTextWindow *win,
5946                            gint           dx,
5947                            gint           dy)
5948 {
5949   if (dx != 0 || dy != 0)
5950     {
5951       gdk_window_scroll (win->bin_window, dx, dy);
5952     }
5953 }
5954
5955 static void
5956 text_window_invalidate_rect (GtkTextWindow *win,
5957                              GdkRectangle  *rect)
5958 {
5959   GdkRectangle window_rect;
5960
5961   gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
5962                                          win->type,
5963                                          rect->x,
5964                                          rect->y,
5965                                          &window_rect.x,
5966                                          &window_rect.y);
5967
5968   window_rect.width = rect->width;
5969   window_rect.height = rect->height;
5970   
5971   /* Adjust the rect as appropriate */
5972   
5973   switch (win->type)
5974     {
5975     case GTK_TEXT_WINDOW_TEXT:
5976       break;
5977
5978     case GTK_TEXT_WINDOW_LEFT:
5979     case GTK_TEXT_WINDOW_RIGHT:
5980       window_rect.x = 0;
5981       window_rect.width = win->allocation.width;
5982       break;
5983
5984     case GTK_TEXT_WINDOW_TOP:
5985     case GTK_TEXT_WINDOW_BOTTOM:
5986       window_rect.y = 0;
5987       window_rect.height = win->allocation.height;
5988       break;
5989
5990     default:
5991       g_warning ("%s: bug!", G_STRLOC);
5992       return;
5993       break;
5994     }
5995           
5996   gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
5997
5998 #if 0
5999   {
6000     GdkColor color = { 0, 65535, 0, 0 };
6001     GdkGC *gc = gdk_gc_new (win->bin_window);
6002     gdk_gc_set_rgb_fg_color (gc, &color);
6003     gdk_draw_rectangle (win->bin_window,
6004                         gc, TRUE, window_rect.x, window_rect.y,
6005                         window_rect.width, window_rect.height);
6006     gdk_gc_unref (gc);
6007   }
6008 #endif
6009 }
6010
6011 static gint
6012 text_window_get_width (GtkTextWindow *win)
6013 {
6014   return win->allocation.width;
6015 }
6016
6017 static gint
6018 text_window_get_height (GtkTextWindow *win)
6019 {
6020   return win->allocation.height;
6021 }
6022
6023 static void
6024 text_window_get_allocation (GtkTextWindow *win,
6025                             GdkRectangle  *rect)
6026 {
6027   *rect = win->allocation;
6028 }
6029
6030 /* Windows */
6031
6032
6033 /**
6034  * gtk_text_view_get_window:
6035  * @text_view: a #GtkTextView
6036  * @win: window to get
6037  *
6038  * Retrieves the #GdkWindow corresponding to an area of the text view;
6039  * possible windows include the overall widget window, child windows
6040  * on the left, right, top, bottom, and the window that displays the
6041  * text buffer. Windows are %NULL and nonexistent if their width or
6042  * height is 0, and are nonexistent before the widget has been
6043  * realized.
6044  *
6045  * Return value: a #GdkWindow, or %NULL
6046  **/
6047 GdkWindow*
6048 gtk_text_view_get_window (GtkTextView *text_view,
6049                           GtkTextWindowType win)
6050 {
6051   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6052
6053   switch (win)
6054     {
6055     case GTK_TEXT_WINDOW_WIDGET:
6056       return GTK_WIDGET (text_view)->window;
6057       break;
6058
6059     case GTK_TEXT_WINDOW_TEXT:
6060       return text_view->text_window->bin_window;
6061       break;
6062
6063     case GTK_TEXT_WINDOW_LEFT:
6064       if (text_view->left_window)
6065         return text_view->left_window->bin_window;
6066       else
6067         return NULL;
6068       break;
6069
6070     case GTK_TEXT_WINDOW_RIGHT:
6071       if (text_view->right_window)
6072         return text_view->right_window->bin_window;
6073       else
6074         return NULL;
6075       break;
6076
6077     case GTK_TEXT_WINDOW_TOP:
6078       if (text_view->top_window)
6079         return text_view->top_window->bin_window;
6080       else
6081         return NULL;
6082       break;
6083
6084     case GTK_TEXT_WINDOW_BOTTOM:
6085       if (text_view->bottom_window)
6086         return text_view->bottom_window->bin_window;
6087       else
6088         return NULL;
6089       break;
6090
6091     default:
6092       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6093       return NULL;
6094       break;
6095     }
6096 }
6097
6098 /**
6099  * gtk_text_view_get_window_type:
6100  * @text_view: a #GtkTextView
6101  * @window: a window type
6102  *
6103  * Usually used to find out which window an event corresponds to.
6104  * If you connect to an event signal on @text_view, this function
6105  * should be called on <literal>event-&gt;window</literal> to
6106  * see which window it was.
6107  *
6108  * Return value: the window type.
6109  **/
6110 GtkTextWindowType
6111 gtk_text_view_get_window_type (GtkTextView *text_view,
6112                                GdkWindow   *window)
6113 {
6114   GtkTextWindow *win;
6115
6116   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6117   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6118
6119   if (window == GTK_WIDGET (text_view)->window)
6120     return GTK_TEXT_WINDOW_WIDGET;
6121
6122   win = g_object_get_qdata (G_OBJECT (window),
6123                             g_quark_try_string ("gtk-text-view-text-window"));
6124
6125   if (win)
6126     return win->type;
6127   else
6128     {
6129       return GTK_TEXT_WINDOW_PRIVATE;
6130     }
6131 }
6132
6133 static void
6134 buffer_to_widget (GtkTextView      *text_view,
6135                   gint              buffer_x,
6136                   gint              buffer_y,
6137                   gint             *window_x,
6138                   gint             *window_y)
6139 {
6140   gint focus_edge_width;
6141   gboolean interior_focus;
6142   
6143   gtk_widget_style_get (GTK_WIDGET (text_view), "interior_focus", &interior_focus, NULL);
6144
6145   if (interior_focus)
6146     focus_edge_width = 0;
6147   else
6148     focus_edge_width = 1;
6149   
6150   if (window_x)
6151     {
6152       *window_x = buffer_x - text_view->xoffset + focus_edge_width;
6153       if (text_view->left_window)
6154         *window_x += text_view->left_window->allocation.width;
6155     }
6156
6157   if (window_y)
6158     {
6159       *window_y = buffer_y - text_view->yoffset + focus_edge_width;
6160       if (text_view->top_window)
6161         *window_y += text_view->top_window->allocation.height;
6162     }
6163 }
6164
6165 static void
6166 widget_to_text_window (GtkTextWindow *win,
6167                        gint           widget_x,
6168                        gint           widget_y,
6169                        gint          *window_x,
6170                        gint          *window_y)
6171 {
6172   if (window_x)
6173     *window_x = widget_x - win->allocation.x;
6174
6175   if (window_y)
6176     *window_y = widget_y - win->allocation.y;
6177 }
6178
6179 static void
6180 buffer_to_text_window (GtkTextView   *text_view,
6181                        GtkTextWindow *win,
6182                        gint           buffer_x,
6183                        gint           buffer_y,
6184                        gint          *window_x,
6185                        gint          *window_y)
6186 {
6187   if (win == NULL)
6188     {
6189       g_warning ("Attempt to convert text buffer coordinates to coordinates "
6190                  "for a nonexistent or private child window of GtkTextView");
6191       return;
6192     }
6193
6194   buffer_to_widget (text_view,
6195                     buffer_x, buffer_y,
6196                     window_x, window_y);
6197
6198   widget_to_text_window (win,
6199                          window_x ? *window_x : 0,
6200                          window_y ? *window_y : 0,
6201                          window_x,
6202                          window_y);
6203 }
6204
6205 /**
6206  * gtk_text_view_buffer_to_window_coords:
6207  * @text_view: a #GtkTextView
6208  * @win: a #GtkTextWindowType
6209  * @buffer_x: buffer x coordinate
6210  * @buffer_y: buffer y coordinate
6211  * @window_x: window x coordinate return location
6212  * @window_y: window y coordinate return location
6213  *
6214  * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6215  * @win, and stores the result in (@window_x, @window_y).
6216  **/
6217 void
6218 gtk_text_view_buffer_to_window_coords (GtkTextView      *text_view,
6219                                        GtkTextWindowType win,
6220                                        gint              buffer_x,
6221                                        gint              buffer_y,
6222                                        gint             *window_x,
6223                                        gint             *window_y)
6224 {
6225   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6226
6227   switch (win)
6228     {
6229     case GTK_TEXT_WINDOW_WIDGET:
6230       buffer_to_widget (text_view,
6231                         buffer_x, buffer_y,
6232                         window_x, window_y);
6233       break;
6234
6235     case GTK_TEXT_WINDOW_TEXT:
6236       if (window_x)
6237         *window_x = buffer_x - text_view->xoffset;
6238       if (window_y)
6239         *window_y = buffer_y - text_view->yoffset;
6240       break;
6241
6242     case GTK_TEXT_WINDOW_LEFT:
6243       buffer_to_text_window (text_view,
6244                              text_view->left_window,
6245                              buffer_x, buffer_y,
6246                              window_x, window_y);
6247       break;
6248
6249     case GTK_TEXT_WINDOW_RIGHT:
6250       buffer_to_text_window (text_view,
6251                              text_view->right_window,
6252                              buffer_x, buffer_y,
6253                              window_x, window_y);
6254       break;
6255
6256     case GTK_TEXT_WINDOW_TOP:
6257       buffer_to_text_window (text_view,
6258                              text_view->top_window,
6259                              buffer_x, buffer_y,
6260                              window_x, window_y);
6261       break;
6262
6263     case GTK_TEXT_WINDOW_BOTTOM:
6264       buffer_to_text_window (text_view,
6265                              text_view->bottom_window,
6266                              buffer_x, buffer_y,
6267                              window_x, window_y);
6268       break;
6269
6270     case GTK_TEXT_WINDOW_PRIVATE:
6271       g_warning ("%s: can't get coords for private windows", G_STRLOC);
6272       break;
6273
6274     default:
6275       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6276       break;
6277     }
6278 }
6279
6280 static void
6281 widget_to_buffer (GtkTextView *text_view,
6282                   gint         widget_x,
6283                   gint         widget_y,
6284                   gint        *buffer_x,
6285                   gint        *buffer_y)
6286 {
6287   gint focus_edge_width;
6288   gboolean interior_focus;
6289   
6290   gtk_widget_style_get (GTK_WIDGET (text_view), "interior_focus", &interior_focus, NULL);
6291
6292   if (interior_focus)
6293     focus_edge_width = 0;
6294   else
6295     focus_edge_width = 1;
6296   
6297   if (buffer_x)
6298     {
6299       *buffer_x = widget_x - focus_edge_width + text_view->xoffset;
6300       if (text_view->left_window)
6301         *buffer_x -= text_view->left_window->allocation.width;
6302     }
6303
6304   if (buffer_y)
6305     {
6306       *buffer_y = widget_y - focus_edge_width + text_view->yoffset;
6307       if (text_view->top_window)
6308         *buffer_y -= text_view->top_window->allocation.height;
6309     }
6310 }
6311
6312 static void
6313 text_window_to_widget (GtkTextWindow *win,
6314                        gint           window_x,
6315                        gint           window_y,
6316                        gint          *widget_x,
6317                        gint          *widget_y)
6318 {
6319   if (widget_x)
6320     *widget_x = window_x + win->allocation.x;
6321
6322   if (widget_y)
6323     *widget_y = window_y + win->allocation.y;
6324 }
6325
6326 static void
6327 text_window_to_buffer (GtkTextView   *text_view,
6328                        GtkTextWindow *win,
6329                        gint           window_x,
6330                        gint           window_y,
6331                        gint          *buffer_x,
6332                        gint          *buffer_y)
6333 {
6334   if (win == NULL)
6335     {
6336       g_warning ("Attempt to convert GtkTextView buffer coordinates into "
6337                  "coordinates for a nonexistent child window.");
6338       return;
6339     }
6340
6341   text_window_to_widget (win,
6342                          window_x,
6343                          window_y,
6344                          buffer_x,
6345                          buffer_y);
6346
6347   widget_to_buffer (text_view,
6348                     buffer_x ? *buffer_x : 0,
6349                     buffer_y ? *buffer_y : 0,
6350                     buffer_x,
6351                     buffer_y);
6352 }
6353
6354 /**
6355  * gtk_text_view_window_to_buffer_coords:
6356  * @text_view: a #GtkTextView
6357  * @win: a #GtkTextWindowType
6358  * @window_x: window x coordinate
6359  * @window_y: window y coordinate
6360  * @buffer_x: buffer x coordinate return location
6361  * @buffer_y: buffer y coordinate return location
6362  *
6363  * Converts coordinates on the window identified by @win to buffer
6364  * coordinates, storing the result in (@buffer_x,@buffer_y).
6365  **/
6366 void
6367 gtk_text_view_window_to_buffer_coords (GtkTextView      *text_view,
6368                                        GtkTextWindowType win,
6369                                        gint              window_x,
6370                                        gint              window_y,
6371                                        gint             *buffer_x,
6372                                        gint             *buffer_y)
6373 {
6374   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6375
6376   switch (win)
6377     {
6378     case GTK_TEXT_WINDOW_WIDGET:
6379       widget_to_buffer (text_view,
6380                         window_x, window_y,
6381                         buffer_x, buffer_y);
6382       break;
6383
6384     case GTK_TEXT_WINDOW_TEXT:
6385       if (buffer_x)
6386         *buffer_x = window_x + text_view->xoffset;
6387       if (buffer_y)
6388         *buffer_y = window_y + text_view->yoffset;
6389       break;
6390
6391     case GTK_TEXT_WINDOW_LEFT:
6392       text_window_to_buffer (text_view,
6393                              text_view->left_window,
6394                              window_x, window_y,
6395                              buffer_x, buffer_y);
6396       break;
6397
6398     case GTK_TEXT_WINDOW_RIGHT:
6399       text_window_to_buffer (text_view,
6400                              text_view->right_window,
6401                              window_x, window_y,
6402                              buffer_x, buffer_y);
6403       break;
6404
6405     case GTK_TEXT_WINDOW_TOP:
6406       text_window_to_buffer (text_view,
6407                              text_view->top_window,
6408                              window_x, window_y,
6409                              buffer_x, buffer_y);
6410       break;
6411
6412     case GTK_TEXT_WINDOW_BOTTOM:
6413       text_window_to_buffer (text_view,
6414                              text_view->bottom_window,
6415                              window_x, window_y,
6416                              buffer_x, buffer_y);
6417       break;
6418
6419     case GTK_TEXT_WINDOW_PRIVATE:
6420       g_warning ("%s: can't get coords for private windows", G_STRLOC);
6421       break;
6422
6423     default:
6424       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
6425       break;
6426     }
6427 }
6428
6429 static void
6430 set_window_width (GtkTextView      *text_view,
6431                   gint              width,
6432                   GtkTextWindowType type,
6433                   GtkTextWindow   **winp)
6434 {
6435   if (width == 0)
6436     {
6437       if (*winp)
6438         {
6439           text_window_free (*winp);
6440           *winp = NULL;
6441           gtk_widget_queue_resize (GTK_WIDGET (text_view));
6442         }
6443     }
6444   else
6445     {
6446       if (*winp == NULL)
6447         {
6448           *winp = text_window_new (type,
6449                                    GTK_WIDGET (text_view),
6450                                    width, 0);
6451           /* if the widget is already realized we need to realize the child manually */
6452           if (GTK_WIDGET_REALIZED (text_view))
6453             text_window_realize (*winp, GTK_WIDGET (text_view)->window);
6454         }
6455       else
6456         {
6457           if ((*winp)->requisition.width == width)
6458             return;
6459         }
6460
6461       gtk_widget_queue_resize (GTK_WIDGET (text_view));
6462     }
6463 }
6464
6465
6466 static void
6467 set_window_height (GtkTextView      *text_view,
6468                    gint              height,
6469                    GtkTextWindowType type,
6470                    GtkTextWindow   **winp)
6471 {
6472   if (height == 0)
6473     {
6474       if (*winp)
6475         {
6476           text_window_free (*winp);
6477           *winp = NULL;
6478           gtk_widget_queue_resize (GTK_WIDGET (text_view));
6479         }
6480     }
6481   else
6482     {
6483       if (*winp == NULL)
6484         {
6485           *winp = text_window_new (type,
6486                                    GTK_WIDGET (text_view),
6487                                    0, height);
6488
6489           /* if the widget is already realized we need to realize the child manually */
6490           if (GTK_WIDGET_REALIZED (text_view))
6491             text_window_realize (*winp, GTK_WIDGET (text_view)->window);
6492         }
6493       else
6494         {
6495           if ((*winp)->requisition.height == height)
6496             return;
6497         }
6498
6499       gtk_widget_queue_resize (GTK_WIDGET (text_view));
6500     }
6501 }
6502
6503 /**
6504  * gtk_text_view_set_border_window_size:
6505  * @text_view: a #GtkTextView
6506  * @type: window to affect
6507  * @size: width or height of the window
6508  *
6509  * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
6510  * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
6511  * Automatically destroys the corresponding window if the size is set
6512  * to 0, and creates the window if the size is set to non-zero.  This
6513  * function can only be used for the "border windows," it doesn't work
6514  * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
6515  * #GTK_TEXT_WINDOW_PRIVATE.
6516  **/
6517 void
6518 gtk_text_view_set_border_window_size (GtkTextView      *text_view,
6519                                       GtkTextWindowType type,
6520                                       gint              size)
6521
6522 {
6523   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6524   g_return_if_fail (size >= 0);
6525
6526   switch (type)
6527     {
6528     case GTK_TEXT_WINDOW_LEFT:
6529       set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
6530                         &text_view->left_window);
6531       break;
6532
6533     case GTK_TEXT_WINDOW_RIGHT:
6534       set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
6535                         &text_view->right_window);
6536       break;
6537
6538     case GTK_TEXT_WINDOW_TOP:
6539       set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
6540                          &text_view->top_window);
6541       break;
6542
6543     case GTK_TEXT_WINDOW_BOTTOM:
6544       set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
6545                          &text_view->bottom_window);
6546       break;
6547
6548     default:
6549       g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
6550       break;
6551     }
6552 }
6553
6554 /**
6555  * gtk_text_view_get_border_window_size:
6556  * @text_view: a #GtkTextView
6557  * @type: window to return size from
6558  *
6559  * Gets the width of the specified border window. See
6560  * gtk_text_view_set_border_window_size().
6561  *
6562  * Return value: width of window
6563  **/
6564 gint
6565 gtk_text_view_get_border_window_size (GtkTextView       *text_view,
6566                                       GtkTextWindowType  type)
6567 {
6568   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6569   
6570   switch (type)
6571     {
6572     case GTK_TEXT_WINDOW_LEFT:
6573       if (text_view->left_window)
6574         return text_view->left_window->requisition.width;
6575       
6576     case GTK_TEXT_WINDOW_RIGHT:
6577       if (text_view->right_window)
6578         return text_view->right_window->requisition.width;
6579       
6580     case GTK_TEXT_WINDOW_TOP:
6581       if (text_view->top_window)
6582         return text_view->top_window->requisition.height;
6583
6584     case GTK_TEXT_WINDOW_BOTTOM:
6585       if (text_view->bottom_window)
6586         return text_view->bottom_window->requisition.height;
6587       
6588     default:
6589       g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
6590       break;
6591     }
6592
6593   return 0;
6594 }
6595
6596 /*
6597  * Child widgets
6598  */
6599
6600 static GtkTextViewChild*
6601 text_view_child_new_anchored (GtkWidget          *child,
6602                               GtkTextChildAnchor *anchor,
6603                               GtkTextLayout      *layout)
6604 {
6605   GtkTextViewChild *vc;
6606
6607   vc = g_new (GtkTextViewChild, 1);
6608
6609   vc->widget = child;
6610   vc->anchor = anchor;
6611
6612   vc->from_top_of_line = 0;
6613   vc->from_left_of_buffer = 0;
6614   
6615   g_object_ref (G_OBJECT (vc->widget));
6616   g_object_ref (G_OBJECT (vc->anchor));
6617
6618   g_object_set_data (G_OBJECT (child),
6619                      "gtk-text-view-child",
6620                      vc);
6621
6622   gtk_text_child_anchor_register_child (anchor, child, layout);
6623   
6624   return vc;
6625 }
6626
6627 static GtkTextViewChild*
6628 text_view_child_new_window (GtkWidget          *child,
6629                             GtkTextWindowType   type,
6630                             gint                x,
6631                             gint                y)
6632 {
6633   GtkTextViewChild *vc;
6634
6635   vc = g_new (GtkTextViewChild, 1);
6636
6637   vc->widget = child;
6638   vc->anchor = NULL;
6639
6640   vc->from_top_of_line = 0;
6641   vc->from_left_of_buffer = 0;
6642   
6643   g_object_ref (G_OBJECT (vc->widget));
6644
6645   vc->type = type;
6646   vc->x = x;
6647   vc->y = y;
6648
6649   g_object_set_data (G_OBJECT (child),
6650                      "gtk-text-view-child",
6651                      vc);
6652   
6653   return vc;
6654 }
6655
6656 static void
6657 text_view_child_free (GtkTextViewChild *child)
6658 {
6659   g_object_set_data (G_OBJECT (child->widget),
6660                      "gtk-text-view-child", NULL);
6661
6662   if (child->anchor)
6663     {
6664       gtk_text_child_anchor_unregister_child (child->anchor,
6665                                               child->widget);
6666       g_object_unref (G_OBJECT (child->anchor));
6667     }
6668
6669   g_object_unref (G_OBJECT (child->widget));
6670
6671   g_free (child);
6672 }
6673
6674 static void
6675 text_view_child_set_parent_window (GtkTextView      *text_view,
6676                                    GtkTextViewChild *vc)
6677 {
6678   if (vc->anchor)
6679     gtk_widget_set_parent_window (vc->widget,
6680                                   text_view->text_window->bin_window);
6681   else
6682     {
6683       GdkWindow *window;
6684       window = gtk_text_view_get_window (text_view,
6685                                          vc->type);
6686       gtk_widget_set_parent_window (vc->widget, window);
6687     }
6688 }
6689
6690 static void
6691 add_child (GtkTextView      *text_view,
6692            GtkTextViewChild *vc)
6693 {
6694   text_view->children = g_slist_prepend (text_view->children,
6695                                          vc);
6696
6697   if (GTK_WIDGET_REALIZED (text_view))
6698     text_view_child_set_parent_window (text_view, vc);
6699   
6700   gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
6701 }
6702
6703 void
6704 gtk_text_view_add_child_at_anchor (GtkTextView          *text_view,
6705                                    GtkWidget            *child,
6706                                    GtkTextChildAnchor   *anchor)
6707 {
6708   GtkTextViewChild *vc;
6709
6710   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6711   g_return_if_fail (GTK_IS_WIDGET (child));
6712   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
6713   g_return_if_fail (child->parent == NULL);
6714
6715   gtk_text_view_ensure_layout (text_view);
6716
6717   vc = text_view_child_new_anchored (child, anchor,
6718                                      text_view->layout);
6719
6720   add_child (text_view, vc);
6721
6722   g_assert (vc->widget == child);
6723   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
6724 }
6725
6726 /**
6727  * gtk_text_view_add_child_in_window:
6728  * @text_view: a #GtkTextView
6729  * @child: a #GtkWidget
6730  * @which_window: which window the child should appear in
6731  * @xpos: X position of child in window coordinates
6732  * @ypos: Y position of child in window coordinates
6733  *
6734  * Adds a child at fixed coordinates in one of the text widget's
6735  * windows.  The window must have nonzero size (see
6736  * gtk_text_view_set_border_window_size()).  Note that the child
6737  * coordinates are given relative to the #GdkWindow in question, and
6738  * that these coordinates have no sane relationship to scrolling. When
6739  * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
6740  * irrelevant, the child floats above all scrollable areas. But when
6741  * placing a child in one of the scrollable windows (border windows or
6742  * text window), you'll need to compute the child's correct position
6743  * in buffer coordinates any time scrolling occurs or buffer changes
6744  * occur, and then call gtk_text_view_move_child() to update the
6745  * child's position. Unfortunately there's no good way to detect that
6746  * scrolling has occurred, using the current API; a possible hack
6747  * would be to update all child positions when the scroll adjustments
6748  * change or the text buffer changes. See bug 64518 on
6749  * bugzilla.gnome.org for status of fixing this issue.
6750  *
6751  **/
6752 void
6753 gtk_text_view_add_child_in_window (GtkTextView          *text_view,
6754                                    GtkWidget            *child,
6755                                    GtkTextWindowType     which_window,
6756                                    gint                  xpos,
6757                                    gint                  ypos)
6758 {
6759   GtkTextViewChild *vc;
6760
6761   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6762   g_return_if_fail (GTK_IS_WIDGET (child));
6763   g_return_if_fail (child->parent == NULL);
6764
6765   vc = text_view_child_new_window (child, which_window,
6766                                    xpos, ypos);
6767
6768   add_child (text_view, vc);
6769
6770   g_assert (vc->widget == child);
6771   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
6772 }
6773
6774 /**
6775  * gtk_text_view_move_child:
6776  * @text_view: a #GtkTextView
6777  * @child: child widget already added to the text view
6778  * @xpos: new X position in window coordinates
6779  * @ypos: new Y position in window coordinates
6780  *
6781  * Updates the position of a child, as for gtk_text_view_add_child_in_window().
6782  * 
6783  **/
6784 void
6785 gtk_text_view_move_child          (GtkTextView          *text_view,
6786                                    GtkWidget            *child,
6787                                    gint                  xpos,
6788                                    gint                  ypos)
6789 {
6790   GtkTextViewChild *vc;
6791
6792   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
6793   g_return_if_fail (GTK_IS_WIDGET (child));
6794   g_return_if_fail (child->parent == (GtkWidget*) text_view);
6795
6796   vc = g_object_get_data (G_OBJECT (child),
6797                           "gtk-text-view-child");
6798
6799   g_assert (vc != NULL);
6800
6801   if (vc->x == xpos &&
6802       vc->y == ypos)
6803     return;
6804   
6805   vc->x = xpos;
6806   vc->y = ypos;
6807
6808   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
6809     gtk_widget_queue_resize (child);
6810 }
6811
6812
6813 /* Iterator operations */
6814
6815 gboolean
6816 gtk_text_view_forward_display_line (GtkTextView *text_view,
6817                                     GtkTextIter *iter)
6818 {
6819   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6820   g_return_val_if_fail (iter != NULL, FALSE);
6821
6822   gtk_text_view_ensure_layout (text_view);
6823
6824   return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
6825 }
6826
6827 gboolean
6828 gtk_text_view_backward_display_line (GtkTextView *text_view,
6829                                      GtkTextIter *iter)
6830 {
6831   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6832   g_return_val_if_fail (iter != NULL, FALSE);
6833
6834   gtk_text_view_ensure_layout (text_view);
6835
6836   return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
6837 }
6838
6839 gboolean
6840 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
6841                                         GtkTextIter *iter)
6842 {
6843   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6844   g_return_val_if_fail (iter != NULL, FALSE);
6845
6846   gtk_text_view_ensure_layout (text_view);
6847
6848   return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
6849 }
6850
6851 gboolean
6852 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
6853                                            GtkTextIter *iter)
6854 {
6855   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6856   g_return_val_if_fail (iter != NULL, FALSE);
6857
6858   gtk_text_view_ensure_layout (text_view);
6859
6860   return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
6861 }
6862
6863 gboolean
6864 gtk_text_view_starts_display_line (GtkTextView       *text_view,
6865                                    const GtkTextIter *iter)
6866 {
6867   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6868   g_return_val_if_fail (iter != NULL, FALSE);
6869
6870   gtk_text_view_ensure_layout (text_view);
6871
6872   return gtk_text_layout_iter_starts_line (text_view->layout, iter);
6873 }
6874
6875 gboolean
6876 gtk_text_view_move_visually (GtkTextView *text_view,
6877                              GtkTextIter *iter,
6878                              gint         count)
6879 {
6880   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
6881   g_return_val_if_fail (iter != NULL, FALSE);
6882
6883   gtk_text_view_ensure_layout (text_view);
6884
6885   return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
6886 }