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