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