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