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