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