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