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