]> Pileus Git - ~andy/gtk/blob - gtk/gtktextview.c
New static function to set the background of all windows.
[~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   text_view->vadjustment->page_size = SCREEN_HEIGHT (text_view);
2991   text_view->vadjustment->page_increment = SCREEN_HEIGHT (text_view) * 0.9;
2992   text_view->vadjustment->step_increment = SCREEN_HEIGHT (text_view) * 0.1;
2993   text_view->vadjustment->lower = 0;
2994   text_view->vadjustment->upper = MAX (SCREEN_HEIGHT (text_view),
2995                                        text_view->height);
2996   gtk_adjustment_changed (text_view->vadjustment);
2997
2998   if (yoffset_changed)
2999     gtk_adjustment_value_changed (vadj);
3000
3001   /* The GTK resize loop processes all the pending exposes right
3002    * after doing the resize stuff, so the idle sizer won't have a
3003    * chance to run. So we do the work here. 
3004    */
3005   gtk_text_view_flush_first_validate (text_view);
3006
3007   /* widget->window doesn't get auto-redrawn as the layout is computed, so has to
3008    * be invalidated
3009    */
3010   if (size_changed && GTK_WIDGET_REALIZED (widget))
3011     gdk_window_invalidate_rect (widget->window, NULL, FALSE);
3012 }
3013
3014 static void
3015 gtk_text_view_get_first_para_iter (GtkTextView *text_view,
3016                                    GtkTextIter *iter)
3017 {
3018   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), iter,
3019                                     text_view->first_para_mark);
3020 }
3021
3022 static void
3023 gtk_text_view_validate_onscreen (GtkTextView *text_view)
3024 {
3025   GtkWidget *widget = GTK_WIDGET (text_view);
3026   
3027   DV(g_print(">Validating onscreen ("G_STRLOC")\n"));
3028   
3029   if (SCREEN_HEIGHT (widget) > 0)
3030     {
3031       GtkTextIter first_para;
3032
3033       /* Be sure we've validated the stuff onscreen; if we
3034        * scrolled, these calls won't have any effect, because
3035        * they were called in the recursive validate_onscreen
3036        */
3037       gtk_text_view_get_first_para_iter (text_view, &first_para);
3038
3039       gtk_text_layout_validate_yrange (text_view->layout,
3040                                        &first_para,
3041                                        0,
3042                                        text_view->first_para_pixels +
3043                                        SCREEN_HEIGHT (widget));
3044     }
3045
3046   text_view->onscreen_validated = TRUE;
3047
3048   DV(g_print(">Done validating onscreen, onscreen_validated = TRUE ("G_STRLOC")\n"));
3049   
3050   /* This can have the odd side effect of triggering a scroll, which should
3051    * flip "onscreen_validated" back to FALSE, but should also get us
3052    * back into this function to turn it on again.
3053    */
3054   gtk_text_view_update_adjustments (text_view);
3055
3056   g_assert (text_view->onscreen_validated);
3057 }
3058
3059 static void
3060 gtk_text_view_flush_first_validate (GtkTextView *text_view)
3061 {
3062   if (text_view->first_validate_idle == 0)
3063     return;
3064
3065   /* Do this first, which means that if an "invalidate"
3066    * occurs during any of this process, a new first_validate_callback
3067    * will be installed, and we'll start again.
3068    */
3069   DV (g_print ("removing first validate in %s\n", G_STRLOC));
3070   g_source_remove (text_view->first_validate_idle);
3071   text_view->first_validate_idle = 0;
3072   
3073   /* be sure we have up-to-date screen size set on the
3074    * layout.
3075    */
3076   gtk_text_view_update_layout_width (text_view);
3077
3078   /* Bail out if we invalidated stuff; scrolling right away will just
3079    * confuse the issue.
3080    */
3081   if (text_view->first_validate_idle != 0)
3082     {
3083       DV(g_print(">Width change forced requeue ("G_STRLOC")\n"));
3084     }
3085   else
3086     {
3087       /* scroll to any marks, if that's pending. This can jump us to
3088        * the validation codepath used for scrolling onscreen, if so we
3089        * bail out.  It won't jump if already in that codepath since
3090        * value_changed is not recursive, so also validate if
3091        * necessary.
3092        */
3093       if (!gtk_text_view_flush_scroll (text_view) ||
3094           !text_view->onscreen_validated)
3095         gtk_text_view_validate_onscreen (text_view);
3096       
3097       DV(g_print(">Leaving first validate idle ("G_STRLOC")\n"));
3098       
3099       g_assert (text_view->onscreen_validated);
3100     }
3101 }
3102
3103 static gboolean
3104 first_validate_callback (gpointer data)
3105 {
3106   GtkTextView *text_view = data;
3107
3108   GDK_THREADS_ENTER ();
3109   
3110   /* Note that some of this code is duplicated at the end of size_allocate,
3111    * keep in sync with that.
3112    */
3113   
3114   DV(g_print(G_STRLOC"\n"));
3115
3116   gtk_text_view_flush_first_validate (text_view);
3117   
3118   GDK_THREADS_LEAVE ();
3119   
3120   return FALSE;
3121 }
3122
3123 static gboolean
3124 incremental_validate_callback (gpointer data)
3125 {
3126   GtkTextView *text_view = data;
3127   gboolean result = TRUE;
3128
3129   GDK_THREADS_ENTER ();
3130   
3131   DV(g_print(G_STRLOC"\n"));
3132   
3133   gtk_text_layout_validate (text_view->layout, 2000);
3134
3135   gtk_text_view_update_adjustments (text_view);
3136   
3137   if (gtk_text_layout_is_valid (text_view->layout))
3138     {
3139       text_view->incremental_validate_idle = 0;
3140       result = FALSE;
3141     }
3142
3143   GDK_THREADS_LEAVE ();
3144
3145   return result;
3146 }
3147
3148 static void
3149 gtk_text_view_invalidate (GtkTextView *text_view)
3150 {  
3151   DV (g_print (">Invalidate, onscreen_validated = %d now FALSE ("G_STRLOC")\n",
3152                text_view->onscreen_validated));
3153
3154   text_view->onscreen_validated = FALSE;
3155
3156   /* We'll invalidate when the layout is created */
3157   if (text_view->layout == NULL)
3158     return;
3159   
3160   if (!text_view->first_validate_idle)
3161     {
3162       text_view->first_validate_idle = g_idle_add_full (GTK_PRIORITY_RESIZE - 2, first_validate_callback, text_view, NULL);
3163       DV (g_print (G_STRLOC": adding first validate idle %d\n",
3164                    text_view->first_validate_idle));
3165     }
3166       
3167   if (!text_view->incremental_validate_idle)
3168     {
3169       text_view->incremental_validate_idle = g_idle_add_full (GTK_TEXT_VIEW_PRIORITY_VALIDATE, incremental_validate_callback, text_view, NULL);
3170       DV (g_print (G_STRLOC": adding incremental validate idle %d\n",
3171                    text_view->incremental_validate_idle));
3172     }
3173 }
3174
3175 static void
3176 invalidated_handler (GtkTextLayout *layout,
3177                      gpointer       data)
3178 {
3179   GtkTextView *text_view;
3180
3181   text_view = GTK_TEXT_VIEW (data);
3182
3183   DV (g_print ("Invalidating due to layout invalidate signal\n"));
3184   gtk_text_view_invalidate (text_view);
3185 }
3186
3187 static void
3188 changed_handler (GtkTextLayout     *layout,
3189                  gint               start_y,
3190                  gint               old_height,
3191                  gint               new_height,
3192                  gpointer           data)
3193 {
3194   GtkTextView *text_view;
3195   GtkWidget *widget;
3196   GdkRectangle visible_rect;
3197   GdkRectangle redraw_rect;
3198   
3199   text_view = GTK_TEXT_VIEW (data);
3200   widget = GTK_WIDGET (data);
3201   
3202   DV(g_print(">Lines Validated ("G_STRLOC")\n"));
3203
3204   if (GTK_WIDGET_REALIZED (text_view))
3205     {      
3206       gtk_text_view_get_visible_rect (text_view, &visible_rect);
3207
3208       redraw_rect.x = visible_rect.x;
3209       redraw_rect.width = visible_rect.width;
3210       redraw_rect.y = start_y;
3211
3212       if (old_height == new_height)
3213         redraw_rect.height = old_height;
3214       else if (start_y + old_height > visible_rect.y)
3215         redraw_rect.height = MAX (0, visible_rect.y + visible_rect.height - start_y);
3216       else
3217         redraw_rect.height = 0;
3218         
3219       if (gdk_rectangle_intersect (&redraw_rect, &visible_rect, &redraw_rect))
3220         {
3221           /* text_window_invalidate_rect() takes buffer coordinates */
3222           text_window_invalidate_rect (text_view->text_window,
3223                                        &redraw_rect);
3224
3225           DV(g_print(" invalidated rect: %d,%d %d x %d\n",
3226                      redraw_rect.x,
3227                      redraw_rect.y,
3228                      redraw_rect.width,
3229                      redraw_rect.height));
3230           
3231           if (text_view->left_window)
3232             text_window_invalidate_rect (text_view->left_window,
3233                                          &redraw_rect);
3234           if (text_view->right_window)
3235             text_window_invalidate_rect (text_view->right_window,
3236                                          &redraw_rect);
3237           if (text_view->top_window)
3238             text_window_invalidate_rect (text_view->top_window,
3239                                          &redraw_rect);
3240           if (text_view->bottom_window)
3241             text_window_invalidate_rect (text_view->bottom_window,
3242                                          &redraw_rect);
3243
3244           gtk_text_view_update_im_spot_location (text_view);
3245         }
3246     }
3247   
3248   if (old_height != new_height)
3249     {
3250       gboolean yoffset_changed = FALSE;
3251       GSList *tmp_list;
3252       int new_first_para_top;
3253       int old_first_para_top;
3254       GtkTextIter first;
3255       
3256       /* If the bottom of the old area was above the top of the
3257        * screen, we need to scroll to keep the current top of the
3258        * screen in place.  Remember that first_para_pixels is the
3259        * position of the top of the screen in coordinates relative to
3260        * the first paragraph onscreen.
3261        *
3262        * In short we are adding the height delta of the portion of the
3263        * changed region above first_para_mark to text_view->yoffset.
3264        */
3265       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &first,
3266                                         text_view->first_para_mark);
3267
3268       gtk_text_layout_get_line_yrange (layout, &first, &new_first_para_top, NULL);
3269
3270       old_first_para_top = text_view->yoffset - text_view->first_para_pixels;
3271
3272       if (new_first_para_top != old_first_para_top)
3273         {
3274           text_view->yoffset += new_first_para_top - old_first_para_top;
3275           
3276           get_vadjustment (text_view)->value = text_view->yoffset;
3277           yoffset_changed = TRUE;
3278         }
3279
3280       if (yoffset_changed)
3281         {
3282           DV(g_print ("Changing scroll position (%s)\n", G_STRLOC));
3283           gtk_adjustment_value_changed (get_vadjustment (text_view));
3284         }
3285
3286       /* FIXME be smarter about which anchored widgets we update */
3287
3288       tmp_list = text_view->children;
3289       while (tmp_list != NULL)
3290         {
3291           GtkTextViewChild *child = tmp_list->data;
3292
3293           if (child->anchor)
3294             gtk_text_view_update_child_allocation (text_view, child);
3295
3296           tmp_list = g_slist_next (tmp_list);
3297         }
3298     }
3299
3300   {
3301     GtkRequisition old_req;
3302     GtkRequisition new_req;
3303
3304     old_req = widget->requisition;
3305
3306     /* Use this instead of gtk_widget_size_request wrapper
3307      * to avoid the optimization which just returns widget->requisition
3308      * if a resize hasn't been queued.
3309      */
3310     GTK_WIDGET_GET_CLASS (widget)->size_request (widget, &new_req);
3311
3312     if (old_req.width != new_req.width ||
3313         old_req.height != new_req.height)
3314       {
3315         /* FIXME http://bugzilla.gnome.org/show_bug.cgi?id=72258 */
3316         _gtk_size_group_queue_resize (widget);
3317       }
3318   }
3319 }
3320
3321 static void
3322 gtk_text_view_realize (GtkWidget *widget)
3323 {
3324   GtkTextView *text_view;
3325   GdkWindowAttr attributes;
3326   gint attributes_mask;
3327   GSList *tmp_list;
3328   
3329   text_view = GTK_TEXT_VIEW (widget);
3330   GTK_WIDGET_SET_FLAGS (text_view, GTK_REALIZED);
3331
3332   attributes.window_type = GDK_WINDOW_CHILD;
3333   attributes.x = widget->allocation.x;
3334   attributes.y = widget->allocation.y;
3335   attributes.width = widget->allocation.width;
3336   attributes.height = widget->allocation.height;
3337   attributes.wclass = GDK_INPUT_OUTPUT;
3338   attributes.visual = gtk_widget_get_visual (widget);
3339   attributes.colormap = gtk_widget_get_colormap (widget);
3340   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK;
3341
3342   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
3343
3344   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
3345                                    &attributes, attributes_mask);
3346   gdk_window_set_user_data (widget->window, widget);
3347
3348   /* must come before text_window_realize calls */
3349   widget->style = gtk_style_attach (widget->style, widget->window);
3350
3351   gdk_window_set_background (widget->window,
3352                              &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3353
3354   text_window_realize (text_view->text_window, widget->window);
3355
3356   if (text_view->left_window)
3357     text_window_realize (text_view->left_window,
3358                          widget->window);
3359
3360   if (text_view->top_window)
3361     text_window_realize (text_view->top_window,
3362                          widget->window);
3363
3364   if (text_view->right_window)
3365     text_window_realize (text_view->right_window,
3366                          widget->window);
3367
3368   if (text_view->bottom_window)
3369     text_window_realize (text_view->bottom_window,
3370                          widget->window);
3371
3372   gtk_text_view_ensure_layout (text_view);
3373
3374   if (text_view->buffer)
3375     {
3376       GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3377                                                           GDK_SELECTION_PRIMARY);
3378       gtk_text_buffer_add_selection_clipboard (text_view->buffer, clipboard);
3379     }
3380
3381   tmp_list = text_view->children;
3382   while (tmp_list != NULL)
3383     {
3384       GtkTextViewChild *vc = tmp_list->data;
3385       
3386       text_view_child_set_parent_window (text_view, vc);
3387       
3388       tmp_list = tmp_list->next;
3389     }
3390 }
3391
3392 static void
3393 gtk_text_view_unrealize (GtkWidget *widget)
3394 {
3395   GtkTextView *text_view;
3396   
3397   text_view = GTK_TEXT_VIEW (widget);
3398
3399   if (text_view->buffer)
3400     {
3401       GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
3402                                                           GDK_SELECTION_PRIMARY);
3403       gtk_text_buffer_remove_selection_clipboard (text_view->buffer, clipboard);
3404     }
3405
3406   gtk_text_view_remove_validate_idles (text_view);
3407
3408   if (text_view->popup_menu)
3409     {
3410       gtk_widget_destroy (text_view->popup_menu);
3411       text_view->popup_menu = NULL;
3412     }
3413
3414   text_window_unrealize (text_view->text_window);
3415
3416   if (text_view->left_window)
3417     text_window_unrealize (text_view->left_window);
3418
3419   if (text_view->top_window)
3420     text_window_unrealize (text_view->top_window);
3421
3422   if (text_view->right_window)
3423     text_window_unrealize (text_view->right_window);
3424
3425   if (text_view->bottom_window)
3426     text_window_unrealize (text_view->bottom_window);
3427
3428   gtk_text_view_destroy_layout (text_view);
3429   
3430   (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
3431 }
3432
3433 static void 
3434 gtk_text_view_set_background (GtkTextView *text_view)
3435 {
3436   GtkWidget *widget = GTK_WIDGET (text_view);
3437
3438   gdk_window_set_background (widget->window,
3439                              &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3440   
3441   gdk_window_set_background (text_view->text_window->bin_window,
3442                              &widget->style->base[GTK_WIDGET_STATE (widget)]);
3443   
3444   if (text_view->left_window)
3445     gdk_window_set_background (text_view->left_window->bin_window,
3446                                &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3447   if (text_view->right_window)
3448     gdk_window_set_background (text_view->right_window->bin_window,
3449                                &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3450   
3451   if (text_view->top_window)
3452     gdk_window_set_background (text_view->top_window->bin_window,
3453                                &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3454   
3455   if (text_view->bottom_window)
3456     gdk_window_set_background (text_view->bottom_window->bin_window,
3457                                &widget->style->bg[GTK_WIDGET_STATE (widget)]);
3458 }
3459
3460 static void
3461 gtk_text_view_style_set (GtkWidget *widget,
3462                          GtkStyle  *previous_style)
3463 {
3464   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3465
3466   if (GTK_WIDGET_REALIZED (widget))
3467     {
3468       gtk_text_view_set_background (text_view);
3469     }
3470
3471   if (text_view->layout && previous_style)
3472     {
3473       gtk_text_view_set_attributes_from_style (text_view,
3474                                                text_view->layout->default_style,
3475                                                widget->style);
3476       gtk_text_layout_default_style_changed (text_view->layout);
3477     }
3478 }
3479
3480 static void
3481 gtk_text_view_direction_changed (GtkWidget        *widget,
3482                                  GtkTextDirection  previous_direction)
3483 {
3484   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3485
3486   if (text_view->layout)
3487     {
3488       text_view->layout->default_style->direction = gtk_widget_get_direction (widget);
3489       gtk_text_layout_default_style_changed (text_view->layout);
3490     }
3491 }
3492
3493 static void
3494 gtk_text_view_state_changed (GtkWidget      *widget,
3495                              GtkStateType    previous_state)
3496 {
3497   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3498
3499   if (GTK_WIDGET_REALIZED (widget))
3500     {
3501       gtk_text_view_set_background (text_view);
3502     }
3503
3504   if (!GTK_WIDGET_IS_SENSITIVE (widget))
3505     {
3506       /* Clear any selection */
3507       gtk_text_view_unselect (text_view);
3508     }
3509   
3510   gtk_widget_queue_draw (widget);
3511 }
3512
3513 static void
3514 set_invisible_cursor (GdkWindow *window)
3515 {
3516   GdkBitmap *empty_bitmap;
3517   GdkCursor *cursor;
3518   GdkColor useless;
3519   char invisible_cursor_bits[] = { 0x0 };       
3520         
3521   useless.red = useless.green = useless.blue = 0;
3522   useless.pixel = 0;
3523   
3524   empty_bitmap = gdk_bitmap_create_from_data (window,
3525                                               invisible_cursor_bits,
3526                                               1, 1);
3527   
3528   cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
3529                                        empty_bitmap,
3530                                        &useless,
3531                                        &useless, 0, 0);
3532   
3533   gdk_window_set_cursor (window, cursor);
3534   
3535   gdk_cursor_unref (cursor);
3536   
3537   g_object_unref (empty_bitmap);
3538 }
3539
3540 static void
3541 gtk_text_view_obscure_mouse_cursor (GtkTextView *text_view)
3542 {
3543   if (text_view->mouse_cursor_obscured)
3544     return;
3545
3546   set_invisible_cursor (text_view->text_window->bin_window);
3547   
3548   text_view->mouse_cursor_obscured = TRUE;  
3549 }
3550
3551 /*
3552  * Events
3553  */
3554
3555 static gboolean
3556 get_event_coordinates (GdkEvent *event, gint *x, gint *y)
3557 {
3558   if (event)
3559     switch (event->type)
3560       {
3561       case GDK_MOTION_NOTIFY:
3562         *x = event->motion.x;
3563         *y = event->motion.y;
3564         return TRUE;
3565         break;
3566
3567       case GDK_BUTTON_PRESS:
3568       case GDK_2BUTTON_PRESS:
3569       case GDK_3BUTTON_PRESS:
3570       case GDK_BUTTON_RELEASE:
3571         *x = event->button.x;
3572         *y = event->button.y;
3573         return TRUE;
3574         break;
3575
3576       case GDK_KEY_PRESS:
3577       case GDK_KEY_RELEASE:
3578       case GDK_ENTER_NOTIFY:
3579       case GDK_LEAVE_NOTIFY:
3580       case GDK_PROPERTY_NOTIFY:
3581       case GDK_SELECTION_CLEAR:
3582       case GDK_SELECTION_REQUEST:
3583       case GDK_SELECTION_NOTIFY:
3584       case GDK_PROXIMITY_IN:
3585       case GDK_PROXIMITY_OUT:
3586       case GDK_DRAG_ENTER:
3587       case GDK_DRAG_LEAVE:
3588       case GDK_DRAG_MOTION:
3589       case GDK_DRAG_STATUS:
3590       case GDK_DROP_START:
3591       case GDK_DROP_FINISHED:
3592       default:
3593         return FALSE;
3594         break;
3595       }
3596
3597   return FALSE;
3598 }
3599
3600 static gint
3601 emit_event_on_tags (GtkWidget   *widget,
3602                     GdkEvent    *event,
3603                     GtkTextIter *iter)
3604 {
3605   GSList *tags;
3606   GSList *tmp;
3607   gboolean retval = FALSE;
3608   GtkTextView *text_view;
3609
3610   text_view = GTK_TEXT_VIEW (widget);
3611
3612   tags = gtk_text_iter_get_tags (iter);
3613
3614   tmp = tags;
3615   while (tmp != NULL)
3616     {
3617       GtkTextTag *tag = tmp->data;
3618
3619       if (gtk_text_tag_event (tag, G_OBJECT (widget), event, iter))
3620         {
3621           retval = TRUE;
3622           break;
3623         }
3624
3625       tmp = g_slist_next (tmp);
3626     }
3627
3628   g_slist_free (tags);
3629
3630   return retval;
3631 }
3632
3633 static gint
3634 gtk_text_view_event (GtkWidget *widget, GdkEvent *event)
3635 {
3636   GtkTextView *text_view;
3637   gint x = 0, y = 0;
3638
3639   text_view = GTK_TEXT_VIEW (widget);
3640
3641   if (text_view->layout == NULL ||
3642       get_buffer (text_view) == NULL)
3643     return FALSE;
3644
3645   if (event->any.window != text_view->text_window->bin_window)
3646     return FALSE;
3647
3648   if (get_event_coordinates (event, &x, &y))
3649     {
3650       GtkTextIter iter;
3651
3652       x += text_view->xoffset;
3653       y += text_view->yoffset;
3654
3655       /* FIXME this is slow and we do it twice per event.
3656        * My favorite solution is to have GtkTextLayout cache
3657        * the last couple lookups.
3658        */
3659       gtk_text_layout_get_iter_at_pixel (text_view->layout,
3660                                          &iter,
3661                                          x, y);
3662
3663       return emit_event_on_tags (widget, event, &iter);
3664     }
3665   else if (event->type == GDK_KEY_PRESS ||
3666            event->type == GDK_KEY_RELEASE)
3667     {
3668       GtkTextMark *insert;
3669       GtkTextIter iter;
3670
3671       insert = gtk_text_buffer_get_mark (get_buffer (text_view),
3672                                          "insert");
3673
3674       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3675
3676       return emit_event_on_tags (widget, event, &iter);
3677     }
3678   else
3679     return FALSE;
3680 }
3681
3682 static gint
3683 gtk_text_view_key_press_event (GtkWidget *widget, GdkEventKey *event)
3684 {
3685   gboolean retval = FALSE;
3686   gboolean obscure = FALSE;
3687   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3688   GtkTextMark *insert;
3689   GtkTextIter iter;
3690   gboolean can_insert;
3691   
3692   if (text_view->layout == NULL ||
3693       get_buffer (text_view) == NULL)
3694     return FALSE;
3695
3696   insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3697   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3698   can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
3699   if (can_insert &&
3700       gtk_im_context_filter_keypress (text_view->im_context, event))
3701     {
3702       text_view->need_im_reset = TRUE;
3703       obscure = TRUE;
3704       retval = TRUE;
3705     }
3706   /* Binding set */
3707   else if (GTK_WIDGET_CLASS (parent_class)->key_press_event &&
3708            GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
3709     retval = TRUE;
3710   /* use overall editability not can_insert, more predictable for users */
3711   else if (text_view->editable &&
3712            (event->keyval == GDK_Return ||
3713             event->keyval == GDK_KP_Enter))
3714     {
3715       /* this won't actually insert the newline if the cursor isn't
3716        * editable
3717        */
3718       gtk_text_view_commit_text (text_view, "\n");
3719
3720       obscure = TRUE;
3721       retval = TRUE;
3722     }
3723   /* Pass through Tab as literal tab, unless Control is held down */
3724   else if ((event->keyval == GDK_Tab ||
3725             event->keyval == GDK_KP_Tab) &&
3726            !(event->state & GDK_CONTROL_MASK))
3727     {
3728       /* If the text widget isn't editable overall, move the focus
3729        * instead
3730        */
3731       if (text_view->editable)
3732         {
3733           gtk_text_view_commit_text (text_view, "\t");
3734           obscure = TRUE;
3735         }
3736       else
3737         gtk_text_view_move_focus (text_view,
3738                                   (event->state & GDK_SHIFT_MASK) ?
3739                                   GTK_DIR_TAB_BACKWARD: GTK_DIR_TAB_FORWARD);
3740
3741       retval = TRUE;
3742     }
3743   else
3744     retval = FALSE;
3745
3746   if (obscure)
3747     gtk_text_view_obscure_mouse_cursor (text_view);
3748   
3749   gtk_text_view_pend_cursor_blink (text_view);
3750
3751   return retval;
3752 }
3753
3754 static gint
3755 gtk_text_view_key_release_event (GtkWidget *widget, GdkEventKey *event)
3756 {
3757   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3758   GtkTextMark *insert;
3759   GtkTextIter iter;
3760
3761   if (text_view->layout == NULL || get_buffer (text_view) == NULL)
3762     return FALSE;
3763       
3764   insert = gtk_text_buffer_get_insert (get_buffer (text_view));
3765   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &iter, insert);
3766   if (gtk_text_iter_can_insert (&iter, text_view->editable) &&
3767       gtk_im_context_filter_keypress (text_view->im_context, event))
3768     {
3769       text_view->need_im_reset = TRUE;
3770       return TRUE;
3771     }
3772   else
3773     return GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);
3774 }
3775
3776 static gint
3777 gtk_text_view_button_press_event (GtkWidget *widget, GdkEventButton *event)
3778 {
3779   GtkTextView *text_view;
3780
3781   text_view = GTK_TEXT_VIEW (widget);
3782
3783   text_view->disable_scroll_on_focus = TRUE;
3784   gtk_widget_grab_focus (widget);
3785   text_view->disable_scroll_on_focus = FALSE;
3786
3787   if (event->window != text_view->text_window->bin_window)
3788     {
3789       /* Remove selection if any. */
3790       gtk_text_view_unselect (text_view);
3791       return FALSE;
3792     }
3793
3794 #if 0
3795   /* debug hack */
3796   if (event->button == 3 && (event->state & GDK_CONTROL_MASK) != 0)
3797     _gtk_text_buffer_spew (GTK_TEXT_VIEW (widget)->buffer);
3798   else if (event->button == 3)
3799     gtk_text_layout_spew (GTK_TEXT_VIEW (widget)->layout);
3800 #endif
3801
3802   if (event->type == GDK_BUTTON_PRESS)
3803     {
3804       gtk_text_view_reset_im_context (text_view);
3805
3806       if (event->button == 1)
3807         {
3808           /* If we're in the selection, start a drag copy/move of the
3809            * selection; otherwise, start creating a new selection.
3810            */
3811           GtkTextIter iter;
3812           GtkTextIter start, end;
3813
3814           gtk_text_layout_get_iter_at_pixel (text_view->layout,
3815                                              &iter,
3816                                              event->x + text_view->xoffset,
3817                                              event->y + text_view->yoffset);
3818
3819           if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3820                                                     &start, &end) &&
3821               gtk_text_iter_in_range (&iter, &start, &end))
3822             {
3823               text_view->drag_start_x = event->x;
3824               text_view->drag_start_y = event->y;
3825               text_view->pending_place_cursor_button = event->button;
3826             }
3827           else
3828             {
3829               gtk_text_view_start_selection_drag (text_view, &iter, event);
3830             }
3831
3832           return TRUE;
3833         }
3834       else if (event->button == 2)
3835         {
3836           GtkTextIter iter;
3837
3838           gtk_text_layout_get_iter_at_pixel (text_view->layout,
3839                                              &iter,
3840                                              event->x + text_view->xoffset,
3841                                              event->y + text_view->yoffset);
3842
3843           gtk_text_buffer_paste_clipboard (get_buffer (text_view),
3844                                            gtk_widget_get_clipboard (widget, GDK_SELECTION_PRIMARY),
3845                                            &iter,
3846                                            text_view->editable);
3847           return TRUE;
3848         }
3849       else if (event->button == 3)
3850         {
3851           gtk_text_view_do_popup (text_view, event);
3852           return TRUE;
3853         }
3854     }
3855   else if ((event->type == GDK_2BUTTON_PRESS ||
3856             event->type == GDK_3BUTTON_PRESS) &&
3857            event->button == 1)
3858     {
3859       GtkTextIter start, end;      
3860       
3861       /* End the selection drag, otherwise we'd clear the new
3862        * word/line selection on button release
3863        */
3864       gtk_text_view_end_selection_drag (text_view, event);
3865
3866       gtk_text_layout_get_iter_at_pixel (text_view->layout,
3867                                          &start,
3868                                          event->x + text_view->xoffset,
3869                                          event->y + text_view->yoffset); 
3870
3871       end = start;
3872       
3873       if (event->type == GDK_2BUTTON_PRESS)
3874         {
3875           if (gtk_text_iter_inside_word (&start))
3876             {
3877               if (!gtk_text_iter_starts_word (&start))
3878                 gtk_text_iter_backward_word_start (&start);
3879               
3880               if (!gtk_text_iter_ends_word (&end))
3881                 gtk_text_iter_forward_word_end (&end);
3882             }
3883         }
3884       else if (event->type == GDK_3BUTTON_PRESS)
3885         {
3886           if (gtk_text_view_starts_display_line (text_view, &start))
3887             {
3888               /* If on a display line boundary, we assume the user
3889                * clicked off the end of a line and we therefore select
3890                * the line before the boundary.
3891                */
3892               gtk_text_view_backward_display_line_start (text_view, &start);
3893             }
3894           else
3895             {
3896               /* start isn't on the start of a line, so we move it to the
3897                * start, and move end to the end unless it's already there.
3898                */
3899               gtk_text_view_backward_display_line_start (text_view, &start);
3900
3901               if (!gtk_text_view_starts_display_line (text_view, &end))
3902                 gtk_text_view_forward_display_line_end (text_view, &end);
3903             }
3904         }
3905
3906       if (event->state & GDK_SHIFT_MASK)
3907         {
3908           /* Take union of old and new selection */
3909           GtkTextIter old_start, old_end;
3910           
3911           gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
3912                                                 &old_start, &old_end);
3913
3914           gtk_text_iter_order (&start, &old_start);
3915           gtk_text_iter_order (&old_end, &end);
3916
3917           /* Now start is the first of the starts, and end is the
3918            * last of the ends
3919            */
3920         }
3921       
3922       gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3923                                          "selection_bound",
3924                                          &start);
3925       gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
3926                                          "insert",
3927                                          &end);
3928
3929       text_view->just_selected_element = TRUE;
3930       
3931       return TRUE;
3932     }
3933   
3934   return FALSE;
3935 }
3936
3937 static gint
3938 gtk_text_view_button_release_event (GtkWidget *widget, GdkEventButton *event)
3939 {
3940   GtkTextView *text_view;
3941
3942   text_view = GTK_TEXT_VIEW (widget);
3943
3944   if (event->window != text_view->text_window->bin_window)
3945     return FALSE;
3946
3947   if (event->button == 1)
3948     {
3949       if (text_view->drag_start_x >= 0)
3950         {
3951           text_view->drag_start_x = -1;
3952           text_view->drag_start_y = -1;
3953         }
3954
3955       if (gtk_text_view_end_selection_drag (GTK_TEXT_VIEW (widget), event))
3956         return TRUE;
3957       else if (text_view->just_selected_element)
3958         {
3959           text_view->just_selected_element = FALSE;
3960           return FALSE;
3961         }
3962       else if (text_view->pending_place_cursor_button == event->button)
3963         {
3964           GtkTextIter iter;
3965
3966           /* Unselect everything; we clicked inside selection, but
3967            * didn't move by the drag threshold, so just clear selection
3968            * and place cursor.
3969            */
3970           gtk_text_layout_get_iter_at_pixel (text_view->layout,
3971                                              &iter,
3972                                              event->x + text_view->xoffset,
3973                                              event->y + text_view->yoffset);
3974
3975           gtk_text_buffer_place_cursor (get_buffer (text_view), &iter);
3976
3977           text_view->pending_place_cursor_button = 0;
3978           
3979           return FALSE;
3980         }
3981     }
3982
3983   return FALSE;
3984 }
3985
3986 static void
3987 keymap_direction_changed (GdkKeymap   *keymap,
3988                           GtkTextView *text_view)
3989 {
3990   gtk_text_view_check_keymap_direction (text_view);
3991 }
3992
3993 static gint
3994 gtk_text_view_focus_in_event (GtkWidget *widget, GdkEventFocus *event)
3995 {
3996   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
3997
3998   gtk_widget_queue_draw (widget);
3999
4000   DV(g_print (G_STRLOC": focus_in_event\n"));
4001   
4002   if (text_view->cursor_visible && text_view->layout)
4003     {
4004       gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4005       gtk_text_view_check_cursor_blink (text_view);
4006     }
4007
4008   g_signal_connect (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4009                     "direction_changed",
4010                     G_CALLBACK (keymap_direction_changed), text_view);
4011   gtk_text_view_check_keymap_direction (text_view);
4012   
4013   text_view->need_im_reset = TRUE;
4014   gtk_im_context_focus_in (GTK_TEXT_VIEW (widget)->im_context);
4015
4016   return FALSE;
4017 }
4018
4019 static gint
4020 gtk_text_view_focus_out_event (GtkWidget *widget, GdkEventFocus *event)
4021 {
4022   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4023
4024   gtk_widget_queue_draw (widget);
4025
4026   DV(g_print (G_STRLOC": focus_out_event\n"));
4027   
4028   if (text_view->cursor_visible && text_view->layout)
4029     {
4030       gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
4031       gtk_text_view_check_cursor_blink (text_view);
4032     }
4033
4034   g_signal_handlers_disconnect_by_func (gdk_keymap_get_for_display (gtk_widget_get_display (widget)),
4035                                         keymap_direction_changed,
4036                                         text_view);
4037
4038   text_view->need_im_reset = TRUE;
4039   gtk_im_context_focus_out (GTK_TEXT_VIEW (widget)->im_context);
4040
4041   return FALSE;
4042 }
4043
4044 static gint
4045 gtk_text_view_motion_event (GtkWidget *widget, GdkEventMotion *event)
4046 {
4047   GtkTextView *text_view = GTK_TEXT_VIEW (widget);
4048
4049   if (text_view->mouse_cursor_obscured)
4050     {
4051       GdkCursor *cursor;
4052       
4053       cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4054                                            GDK_XTERM);
4055       gdk_window_set_cursor (text_view->text_window->bin_window, cursor);
4056       gdk_cursor_unref (cursor);
4057       text_view->mouse_cursor_obscured = FALSE;
4058     }
4059
4060   if (event->window == text_view->text_window->bin_window &&
4061       text_view->drag_start_x >= 0)
4062     {
4063       gint x, y;
4064
4065       gdk_window_get_pointer (text_view->text_window->bin_window,
4066                               &x, &y, NULL);
4067
4068       if (gtk_drag_check_threshold (widget,
4069                                     text_view->drag_start_x, 
4070                                     text_view->drag_start_y,
4071                                     x, y))
4072         {
4073           GtkTextIter iter;
4074           gint buffer_x, buffer_y;
4075
4076           gtk_text_view_window_to_buffer_coords (text_view,
4077                                                  GTK_TEXT_WINDOW_TEXT,
4078                                                  text_view->drag_start_x,
4079                                                  text_view->drag_start_y,
4080                                                  &buffer_x,
4081                                                  &buffer_y);
4082
4083           gtk_text_layout_get_iter_at_pixel (text_view->layout,
4084                                              &iter,
4085                                              buffer_x, buffer_y);
4086
4087           gtk_text_view_start_selection_dnd (text_view, &iter, event);
4088           return TRUE;
4089         }
4090     }
4091
4092   return FALSE;
4093 }
4094
4095 static void
4096 gtk_text_view_paint (GtkWidget      *widget,
4097                      GdkRectangle   *area,
4098                      GdkEventExpose *event)
4099 {
4100   GtkTextView *text_view;
4101   GList *child_exposes;
4102   GList *tmp_list;
4103   GdkRegion *updates;
4104   
4105   text_view = GTK_TEXT_VIEW (widget);
4106
4107   g_return_if_fail (text_view->layout != NULL);
4108   g_return_if_fail (text_view->xoffset >= 0);
4109   g_return_if_fail (text_view->yoffset >= 0);
4110
4111   while (text_view->first_validate_idle != 0)
4112     {
4113       DV (g_print (G_STRLOC": first_validate_idle: %d\n",
4114                    text_view->first_validate_idle));
4115       gtk_text_view_flush_first_validate (text_view);
4116     }
4117
4118   /* More regions could have become invalid in the above loop */
4119   updates = gdk_window_get_update_area (text_view->text_window->bin_window);
4120   if (updates)
4121     {
4122       GdkRectangle rect;
4123       
4124       gdk_region_get_clipbox (updates, &rect);
4125
4126       gdk_rectangle_union (area, &rect, area);
4127       
4128       gdk_region_destroy (updates);
4129     }
4130   
4131   if (!text_view->onscreen_validated)
4132     {
4133       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.");
4134       g_assert_not_reached ();
4135     }
4136   
4137 #if 0
4138   printf ("painting %d,%d  %d x %d\n",
4139           area->x, area->y,
4140           area->width, area->height);
4141 #endif
4142
4143   child_exposes = NULL;
4144   gtk_text_layout_draw (text_view->layout,
4145                         widget,
4146                         text_view->text_window->bin_window,
4147                         NULL,
4148                         text_view->xoffset,
4149                         text_view->yoffset,
4150                         area->x, area->y,
4151                         area->width, area->height,
4152                         &child_exposes);
4153
4154   tmp_list = child_exposes;
4155   while (tmp_list != NULL)
4156     {
4157       GtkWidget *child = tmp_list->data;
4158       
4159       gtk_container_propagate_expose (GTK_CONTAINER (text_view),
4160                                       child,
4161                                       event);
4162
4163       g_object_unref (child);
4164       
4165       tmp_list = tmp_list->next;
4166     }
4167
4168   g_list_free (child_exposes);
4169 }
4170
4171 static gint
4172 gtk_text_view_expose_event (GtkWidget *widget, GdkEventExpose *event)
4173 {
4174   GSList *tmp_list;
4175   
4176   if (event->window == gtk_text_view_get_window (GTK_TEXT_VIEW (widget),
4177                                                  GTK_TEXT_WINDOW_TEXT))
4178     {
4179       DV(g_print (">Exposed ("G_STRLOC")\n"));
4180       gtk_text_view_paint (widget, &event->area, event);
4181     }
4182
4183   if (event->window == widget->window)
4184     gtk_text_view_draw_focus (widget);
4185
4186   /* Propagate exposes to all children not in the buffer. */
4187   tmp_list = GTK_TEXT_VIEW (widget)->children;
4188   while (tmp_list != NULL)
4189     {
4190       GtkTextViewChild *vc = tmp_list->data;
4191
4192       /* propagate_expose checks that event->window matches
4193        * child->window
4194        */
4195       if (vc->type != GTK_TEXT_WINDOW_TEXT)
4196         gtk_container_propagate_expose (GTK_CONTAINER (widget),
4197                                         vc->widget,
4198                                         event);
4199       
4200       tmp_list = tmp_list->next;
4201     }
4202   
4203   return FALSE;
4204 }
4205
4206 static void
4207 gtk_text_view_draw_focus (GtkWidget *widget)
4208 {
4209   gboolean interior_focus;
4210
4211   /* We clear the focus if we are in interior focus mode. */
4212   gtk_widget_style_get (widget,
4213                         "interior_focus", &interior_focus,
4214                         NULL);
4215   
4216   if (GTK_WIDGET_DRAWABLE (widget))
4217     {
4218       if (GTK_WIDGET_HAS_FOCUS (widget) && !interior_focus)
4219         {          
4220           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget), 
4221                            NULL, widget, "textview",
4222                            0, 0,
4223                            widget->allocation.width,
4224                            widget->allocation.height);
4225         }
4226       else
4227         {
4228           gdk_window_clear (widget->window);
4229         }
4230     }
4231 }
4232
4233 static void
4234 gtk_text_view_grab_focus (GtkWidget *widget)
4235 {
4236   GtkTextView *text_view;
4237
4238   text_view = GTK_TEXT_VIEW (widget);
4239   
4240   GTK_WIDGET_CLASS (parent_class)->grab_focus (widget);
4241
4242   if (!text_view->disable_scroll_on_focus)
4243     gtk_text_view_scroll_mark_onscreen (text_view,
4244                                         gtk_text_buffer_get_mark (get_buffer (text_view),
4245                                                                   "insert"));
4246 }
4247
4248 static gboolean
4249 gtk_text_view_focus (GtkWidget        *widget,
4250                      GtkDirectionType  direction)
4251 {
4252   GtkTextView *text_view;
4253   GtkContainer *container;
4254   gboolean result;
4255   
4256   text_view = GTK_TEXT_VIEW (widget);
4257   container = GTK_CONTAINER (widget);  
4258
4259   if (!gtk_widget_is_focus (widget) &&
4260       container->focus_child == NULL)
4261     {
4262       gtk_widget_grab_focus (widget);
4263       return TRUE;
4264     }
4265   else
4266     {
4267       /*
4268        * Unset CAN_FOCUS flag so that gtk_container_focus() allows
4269        * children to get the focus
4270        */
4271       GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS); 
4272       result = GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
4273       GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS); 
4274
4275       return result;
4276     }
4277 }
4278
4279 /*
4280  * Container
4281  */
4282
4283 static void
4284 gtk_text_view_add (GtkContainer *container,
4285                    GtkWidget    *child)
4286 {
4287   g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4288   g_return_if_fail (GTK_IS_WIDGET (child));
4289
4290   /* This is pretty random. */
4291   gtk_text_view_add_child_in_window (GTK_TEXT_VIEW (container),
4292                                      child,
4293                                      GTK_TEXT_WINDOW_WIDGET,
4294                                      0, 0);
4295 }
4296
4297 static void
4298 gtk_text_view_remove (GtkContainer *container,
4299                       GtkWidget    *child)
4300 {
4301   GSList *iter;
4302   GtkTextView *text_view;
4303   GtkTextViewChild *vc;
4304
4305   g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4306   g_return_if_fail (GTK_IS_WIDGET (child));
4307   g_return_if_fail (child->parent == (GtkWidget*) container);
4308
4309   text_view = GTK_TEXT_VIEW (container);
4310
4311   vc = NULL;
4312   iter = text_view->children;
4313
4314   while (iter != NULL)
4315     {
4316       vc = iter->data;
4317
4318       if (vc->widget == child)
4319         break;
4320
4321       iter = g_slist_next (iter);
4322     }
4323
4324   g_assert (iter != NULL); /* be sure we had the child in the list */
4325
4326   text_view->children = g_slist_remove (text_view->children, vc);
4327
4328   gtk_widget_unparent (vc->widget);
4329
4330   text_view_child_free (vc);
4331 }
4332
4333 static void
4334 gtk_text_view_forall (GtkContainer *container,
4335                       gboolean      include_internals,
4336                       GtkCallback   callback,
4337                       gpointer      callback_data)
4338 {
4339   GSList *iter;
4340   GtkTextView *text_view;
4341   GSList *copy;
4342
4343   g_return_if_fail (GTK_IS_TEXT_VIEW (container));
4344   g_return_if_fail (callback != NULL);
4345
4346   text_view = GTK_TEXT_VIEW (container);
4347
4348   copy = g_slist_copy (text_view->children);
4349   iter = copy;
4350
4351   while (iter != NULL)
4352     {
4353       GtkTextViewChild *vc = iter->data;
4354
4355       (* callback) (vc->widget, callback_data);
4356
4357       iter = g_slist_next (iter);
4358     }
4359
4360   g_slist_free (copy);
4361 }
4362
4363 #define CURSOR_ON_MULTIPLIER 0.66
4364 #define CURSOR_OFF_MULTIPLIER 0.34
4365 #define CURSOR_PEND_MULTIPLIER 1.0
4366
4367 static gboolean
4368 cursor_blinks (GtkTextView *text_view)
4369 {
4370   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4371   gboolean blink;
4372
4373 #ifdef DEBUG_VALIDATION_AND_SCROLLING
4374   return FALSE;
4375 #endif
4376   if (gtk_debug_flags & GTK_DEBUG_UPDATES)
4377     return FALSE;
4378   
4379   g_object_get (G_OBJECT (settings), "gtk-cursor-blink", &blink, NULL);
4380   return blink;
4381 }
4382
4383 static gint
4384 get_cursor_time (GtkTextView *text_view)
4385 {
4386   GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
4387   gint time;
4388
4389   g_object_get (G_OBJECT (settings), "gtk-cursor-blink-time", &time, NULL);
4390
4391   return time;
4392 }
4393
4394 /*
4395  * Blink!
4396  */
4397
4398 static gint
4399 blink_cb (gpointer data)
4400 {
4401   GtkTextView *text_view;
4402   gboolean visible;
4403
4404   GDK_THREADS_ENTER ();
4405
4406   text_view = GTK_TEXT_VIEW (data);
4407   
4408   g_assert (text_view->layout);
4409   g_assert (GTK_WIDGET_HAS_FOCUS (text_view));
4410   g_assert (text_view->cursor_visible);
4411
4412   visible = gtk_text_layout_get_cursor_visible (text_view->layout);
4413
4414   if (visible)
4415     text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4416                                                 blink_cb,
4417                                                 text_view);
4418   else
4419     text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_ON_MULTIPLIER,
4420                                                 blink_cb,
4421                                                 text_view);
4422   
4423   gtk_text_layout_set_cursor_visible (text_view->layout,
4424                                       !visible);
4425
4426   GDK_THREADS_LEAVE ();
4427
4428   /* Remove ourselves */
4429   return FALSE;
4430 }
4431
4432
4433 static void
4434 gtk_text_view_stop_cursor_blink (GtkTextView *text_view)
4435 {
4436   if (text_view->blink_timeout)  
4437     { 
4438       gtk_timeout_remove (text_view->blink_timeout);
4439       text_view->blink_timeout = 0;
4440     }
4441 }
4442
4443 static void
4444 gtk_text_view_check_cursor_blink (GtkTextView *text_view)
4445 {
4446   if (text_view->layout != NULL &&
4447       text_view->cursor_visible &&
4448       GTK_WIDGET_HAS_FOCUS (text_view))
4449     {
4450       if (cursor_blinks (text_view))
4451         {
4452           if (text_view->blink_timeout == 0)
4453             {
4454               gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4455               
4456               text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_OFF_MULTIPLIER,
4457                                                           blink_cb,
4458                                                           text_view);
4459             }
4460         }
4461       else
4462         gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);   
4463     }
4464   else
4465     {
4466       gtk_text_view_stop_cursor_blink (text_view);
4467     }
4468 }
4469
4470 static void
4471 gtk_text_view_pend_cursor_blink(GtkTextView *text_view)
4472 {
4473   if (text_view->layout != NULL &&
4474       text_view->cursor_visible &&
4475       GTK_WIDGET_HAS_FOCUS (text_view) &&
4476       cursor_blinks (text_view))
4477     {
4478       if (text_view->blink_timeout != 0)
4479         {
4480           gtk_timeout_remove (text_view->blink_timeout);
4481           text_view->blink_timeout = 0;
4482         }
4483       
4484       gtk_text_layout_set_cursor_visible (text_view->layout, TRUE);
4485       
4486       text_view->blink_timeout = gtk_timeout_add (get_cursor_time (text_view) * CURSOR_PEND_MULTIPLIER,
4487                                                   blink_cb,
4488                                                   text_view);
4489     }
4490 }
4491
4492
4493 /*
4494  * Key binding handlers
4495  */
4496
4497 static void
4498 gtk_text_view_move_iter_by_lines (GtkTextView *text_view,
4499                                   GtkTextIter *newplace,
4500                                   gint         count)
4501 {
4502   while (count < 0)
4503     {
4504       gtk_text_layout_move_iter_to_previous_line (text_view->layout, newplace);
4505       count++;
4506     }
4507
4508   while (count > 0)
4509     {
4510       gtk_text_layout_move_iter_to_next_line (text_view->layout, newplace);
4511       count--;
4512     }
4513 }
4514
4515 static void
4516 move_cursor (GtkTextView       *text_view,
4517              const GtkTextIter *new_location,
4518              gboolean           extend_selection)
4519 {
4520   if (extend_selection)
4521     gtk_text_buffer_move_mark_by_name (get_buffer (text_view),
4522                                        "insert",
4523                                        new_location);
4524   else
4525     gtk_text_buffer_place_cursor (get_buffer (text_view),
4526                                   new_location);
4527 }
4528
4529 /* FIXME when we are unfrozen and can change GtkMovementStep,
4530  * fix this
4531  */
4532 #define PAGE_HORIZONTALLY_HACK_VALUE 57
4533
4534 static void
4535 gtk_text_view_move_cursor_internal (GtkTextView     *text_view,
4536                                     GtkMovementStep  step,
4537                                     gint             count,
4538                                     gboolean         extend_selection)
4539 {
4540   GtkTextIter insert;
4541   GtkTextIter newplace;
4542
4543   gint cursor_x_pos = 0;
4544
4545   gtk_text_view_reset_im_context (text_view);
4546
4547   if (step == GTK_MOVEMENT_PAGES)
4548     {
4549       gtk_text_view_scroll_pages (text_view, count, extend_selection);
4550       gtk_text_view_pend_cursor_blink (text_view);
4551       return;
4552     }
4553   else if (step == PAGE_HORIZONTALLY_HACK_VALUE)
4554     {
4555       gtk_text_view_scroll_hpages (text_view, count, extend_selection);
4556       gtk_text_view_pend_cursor_blink (text_view);
4557       return;
4558     }
4559
4560   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4561                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4562                                                               "insert"));
4563   newplace = insert;
4564
4565   if (step == GTK_MOVEMENT_DISPLAY_LINES)
4566     gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, NULL);
4567
4568   switch (step)
4569     {
4570     case GTK_MOVEMENT_LOGICAL_POSITIONS:
4571       gtk_text_iter_forward_cursor_positions (&newplace, count);
4572       break;
4573
4574     case GTK_MOVEMENT_VISUAL_POSITIONS:
4575       gtk_text_layout_move_iter_visually (text_view->layout,
4576                                           &newplace, count);
4577       break;
4578
4579     case GTK_MOVEMENT_WORDS:
4580       if (count < 0)
4581         gtk_text_iter_backward_word_starts (&newplace, -count);
4582       else if (count > 0)
4583         gtk_text_iter_forward_word_ends (&newplace, count);
4584       break;
4585
4586     case GTK_MOVEMENT_DISPLAY_LINES:
4587       gtk_text_view_move_iter_by_lines (text_view, &newplace, count);
4588       gtk_text_layout_move_iter_to_x (text_view->layout, &newplace, cursor_x_pos);
4589       break;
4590
4591     case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4592       if (count > 1)
4593         gtk_text_view_move_iter_by_lines (text_view, &newplace, --count);
4594       else if (count < -1)
4595         gtk_text_view_move_iter_by_lines (text_view, &newplace, ++count);
4596
4597       if (count != 0)
4598         gtk_text_layout_move_iter_to_line_end (text_view->layout, &newplace, count);
4599       break;
4600
4601     case GTK_MOVEMENT_PARAGRAPHS:
4602       if (count > 0)
4603         {
4604           if (!gtk_text_iter_ends_line (&newplace))
4605             {
4606               gtk_text_iter_forward_to_line_end (&newplace);
4607               --count;
4608             }
4609         }
4610       else if (count < 0)
4611         {
4612           if (gtk_text_iter_get_line_offset (&newplace) > 0)
4613             {
4614               gtk_text_iter_set_line_offset (&newplace, 0);
4615               ++count;
4616             }
4617         }
4618
4619       if (count != 0)
4620         {
4621           gtk_text_iter_forward_lines (&newplace, count);
4622           gtk_text_iter_set_line_offset (&newplace, 0);
4623         }
4624       break;
4625
4626     case GTK_MOVEMENT_PARAGRAPH_ENDS:
4627       if (count > 0)
4628         {
4629           if (!gtk_text_iter_ends_line (&newplace))
4630             gtk_text_iter_forward_to_line_end (&newplace);
4631         }
4632       else if (count < 0)
4633         {
4634           gtk_text_iter_set_line_offset (&newplace, 0);
4635         }
4636       break;
4637
4638     case GTK_MOVEMENT_BUFFER_ENDS:
4639       if (count > 0)
4640         gtk_text_buffer_get_end_iter (get_buffer (text_view), &newplace);
4641       else if (count < 0)
4642         gtk_text_buffer_get_iter_at_offset (get_buffer (text_view), &newplace, 0);
4643       break;
4644       
4645     default:
4646       break;
4647     }
4648
4649   if (!gtk_text_iter_equal (&insert, &newplace))
4650     {
4651       move_cursor (text_view, &newplace, extend_selection);
4652
4653       DV(g_print (G_STRLOC": scrolling onscreen\n"));
4654       gtk_text_view_scroll_mark_onscreen (text_view,
4655                                           gtk_text_buffer_get_mark (get_buffer (text_view),
4656                                                                     "insert"));
4657
4658       if (step == GTK_MOVEMENT_DISPLAY_LINES)
4659         {
4660           gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, -1);
4661         }
4662     }
4663
4664   gtk_text_view_pend_cursor_blink (text_view);
4665 }
4666
4667 static void
4668 gtk_text_view_move_cursor (GtkTextView     *text_view,
4669                            GtkMovementStep  step,
4670                            gint             count,
4671                            gboolean         extend_selection)
4672 {
4673   gtk_text_view_move_cursor_internal (text_view, step, count, extend_selection);
4674 }
4675
4676 static void
4677 gtk_text_view_page_horizontally (GtkTextView     *text_view,
4678                                  gint             count,
4679                                  gboolean         extend_selection)
4680 {
4681   gtk_text_view_move_cursor_internal (text_view, PAGE_HORIZONTALLY_HACK_VALUE,
4682                                       count, extend_selection);
4683 }
4684
4685 static void
4686 gtk_text_view_set_anchor (GtkTextView *text_view)
4687 {
4688   GtkTextIter insert;
4689
4690   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
4691                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4692                                                               "insert"));
4693
4694   gtk_text_buffer_create_mark (get_buffer (text_view), "anchor", &insert, TRUE);
4695 }
4696
4697 static void
4698 gtk_text_view_scroll_pages (GtkTextView *text_view,
4699                             gint         count,
4700                             gboolean     extend_selection)
4701 {
4702   gdouble newval;
4703   gdouble oldval;
4704   GtkAdjustment *adj;
4705   gint cursor_x_pos, cursor_y_pos;
4706   GtkTextIter new_insert;
4707   GtkTextIter anchor;
4708   gint y0, y1;
4709
4710   g_return_if_fail (text_view->vadjustment != NULL);
4711
4712   cancel_pending_scroll (text_view);
4713   
4714   adj = text_view->vadjustment;
4715
4716   /* Validate the region that will be brought into view by the cursor motion
4717    */
4718   if (count < 0)
4719     {
4720       gtk_text_view_get_first_para_iter (text_view, &anchor);
4721       y0 = adj->page_size;
4722       y1 = adj->page_size + count * adj->page_increment;
4723     }
4724   else
4725     {
4726       gtk_text_view_get_first_para_iter (text_view, &anchor);
4727       y0 = count * adj->page_increment + adj->page_size;
4728       y1 = 0;
4729     }
4730
4731   gtk_text_layout_validate_yrange (text_view->layout, &anchor, y0, y1);
4732   /* FIXME do we need to update the adjustment ranges here? */
4733
4734   if (count < 0 && adj->value <= (adj->lower + 1e-12))
4735     {
4736       /* already at top, just be sure we are at offset 0 */
4737       gtk_text_buffer_get_start_iter (get_buffer (text_view), &new_insert);
4738       move_cursor (text_view, &new_insert, extend_selection);
4739     }
4740   else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4741     {
4742       /* already at bottom, just be sure we are at the end */
4743       gtk_text_buffer_get_end_iter (get_buffer (text_view), &new_insert);
4744       move_cursor (text_view, &new_insert, extend_selection);
4745     }
4746   else
4747     {
4748       gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4749
4750       newval = adj->value;
4751       oldval = adj->value;
4752   
4753       newval += count * adj->page_increment;
4754
4755       set_adjustment_clamped (adj, newval);
4756       cursor_y_pos += adj->value - oldval;
4757
4758       gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4759       clamp_iter_onscreen (text_view, &new_insert);
4760       move_cursor (text_view, &new_insert, extend_selection);
4761
4762       gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4763     }
4764   
4765   /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4766    * only guarantees 1 pixel onscreen.
4767    */
4768   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4769   gtk_text_view_scroll_mark_onscreen (text_view,
4770                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4771                                                                 "insert"));
4772 }
4773
4774 static void
4775 gtk_text_view_scroll_hpages (GtkTextView *text_view,
4776                              gint         count,
4777                              gboolean     extend_selection)
4778 {
4779   gdouble newval;
4780   gdouble oldval;
4781   GtkAdjustment *adj;
4782   gint cursor_x_pos, cursor_y_pos;
4783   GtkTextIter new_insert;
4784   gint y, height;
4785   
4786   g_return_if_fail (text_view->hadjustment != NULL);
4787
4788   cancel_pending_scroll (text_view);
4789   
4790   adj = text_view->hadjustment;
4791
4792   /* Validate the line that we're moving within.
4793    */
4794   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4795                                     &new_insert,
4796                                     gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
4797   gtk_text_layout_get_line_yrange (text_view->layout, &new_insert, &y, &height);
4798   gtk_text_layout_validate_yrange (text_view->layout, &new_insert, y, y + height);
4799   /* FIXME do we need to update the adjustment ranges here? */
4800   
4801   if (count < 0 && adj->value <= (adj->lower + 1e-12))
4802     {
4803       /* already at far left, just be sure we are at offset 0 */
4804       gtk_text_iter_set_line_offset (&new_insert, 0);
4805       move_cursor (text_view, &new_insert, extend_selection);
4806     }
4807   else if (count > 0 && adj->value >= (adj->upper - adj->page_size - 1e-12))
4808     {
4809       /* already at far right, just be sure we are at the end */
4810       gtk_text_iter_forward_to_line_end (&new_insert);
4811       move_cursor (text_view, &new_insert, extend_selection);
4812     }
4813   else
4814     {
4815       gtk_text_view_get_virtual_cursor_pos (text_view, &cursor_x_pos, &cursor_y_pos);
4816
4817       newval = adj->value;
4818       oldval = adj->value;
4819   
4820       newval += count * adj->page_increment;
4821
4822       set_adjustment_clamped (adj, newval);
4823       cursor_x_pos += adj->value - oldval;
4824
4825       gtk_text_layout_get_iter_at_pixel (text_view->layout, &new_insert, cursor_x_pos, cursor_y_pos);
4826       clamp_iter_onscreen (text_view, &new_insert);
4827       move_cursor (text_view, &new_insert, extend_selection);
4828
4829       gtk_text_view_set_virtual_cursor_pos (text_view, cursor_x_pos, cursor_y_pos);
4830     }
4831
4832   /*  FIXME for lines shorter than the overall widget width, this results in a
4833    *  "bounce" effect as we scroll to the right of the widget, then scroll
4834    *  back to get the end of the line onscreen.
4835    *      http://bugzilla.gnome.org/show_bug.cgi?id=68963
4836    */
4837   
4838   /* Adjust to have the cursor _entirely_ onscreen, move_mark_onscreen
4839    * only guarantees 1 pixel onscreen.
4840    */
4841   DV(g_print (G_STRLOC": scrolling onscreen\n"));
4842   gtk_text_view_scroll_mark_onscreen (text_view,
4843                                       gtk_text_buffer_get_mark (get_buffer (text_view),
4844                                                                 "insert"));
4845 }
4846
4847 static gboolean
4848 whitespace (gunichar ch, gpointer user_data)
4849 {
4850   return (ch == ' ' || ch == '\t');
4851 }
4852
4853 static gboolean
4854 not_whitespace (gunichar ch, gpointer user_data)
4855 {
4856   return !whitespace (ch, user_data);
4857 }
4858
4859 static gboolean
4860 find_whitepace_region (const GtkTextIter *center,
4861                        GtkTextIter *start, GtkTextIter *end)
4862 {
4863   *start = *center;
4864   *end = *center;
4865
4866   if (gtk_text_iter_backward_find_char (start, not_whitespace, NULL, NULL))
4867     gtk_text_iter_forward_char (start); /* we want the first whitespace... */
4868   if (whitespace (gtk_text_iter_get_char (end), NULL))
4869     gtk_text_iter_forward_find_char (end, not_whitespace, NULL, NULL);
4870
4871   return !gtk_text_iter_equal (start, end);
4872 }
4873
4874 static void
4875 gtk_text_view_insert_at_cursor (GtkTextView *text_view,
4876                                 const gchar *str)
4877 {
4878   gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
4879                                                 text_view->editable);
4880 }
4881
4882 static void
4883 gtk_text_view_delete_from_cursor (GtkTextView   *text_view,
4884                                   GtkDeleteType  type,
4885                                   gint           count)
4886 {
4887   GtkTextIter insert;
4888   GtkTextIter start;
4889   GtkTextIter end;
4890   gboolean leave_one = FALSE;
4891
4892   gtk_text_view_reset_im_context (text_view);
4893
4894   if (type == GTK_DELETE_CHARS)
4895     {
4896       /* Char delete deletes the selection, if one exists */
4897       if (gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
4898                                             text_view->editable))
4899         return;
4900     }
4901
4902   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
4903                                     &insert,
4904                                     gtk_text_buffer_get_mark (get_buffer (text_view),
4905                                                               "insert"));
4906
4907   start = insert;
4908   end = insert;
4909
4910   switch (type)
4911     {
4912     case GTK_DELETE_CHARS:
4913       gtk_text_iter_forward_cursor_positions (&end, count);
4914       break;
4915
4916     case GTK_DELETE_WORD_ENDS:
4917       if (count > 0)
4918         gtk_text_iter_forward_word_ends (&end, count);
4919       else if (count < 0)
4920         gtk_text_iter_backward_word_starts (&start, 0 - count);
4921       break;
4922
4923     case GTK_DELETE_WORDS:
4924       break;
4925
4926     case GTK_DELETE_DISPLAY_LINE_ENDS:
4927       break;
4928
4929     case GTK_DELETE_DISPLAY_LINES:
4930       break;
4931
4932     case GTK_DELETE_PARAGRAPH_ENDS:
4933       /* If we're already at a newline, we need to
4934        * simply delete that newline, instead of
4935        * moving to the next one.
4936        */
4937       if (gtk_text_iter_ends_line (&end))
4938         {
4939           gtk_text_iter_forward_line (&end);
4940           --count;
4941         }
4942
4943       while (count > 0)
4944         {
4945           if (!gtk_text_iter_forward_to_line_end (&end))
4946             break;
4947
4948           --count;
4949         }
4950
4951       /* FIXME figure out what a negative count means
4952          and support that */
4953       break;
4954
4955     case GTK_DELETE_PARAGRAPHS:
4956       if (count > 0)
4957         {
4958           gtk_text_iter_set_line_offset (&start, 0);
4959           gtk_text_iter_forward_to_line_end (&end);
4960
4961           /* Do the lines beyond the first. */
4962           while (count > 1)
4963             {
4964               gtk_text_iter_forward_to_line_end (&end);
4965
4966               --count;
4967             }
4968         }
4969
4970       /* FIXME negative count? */
4971
4972       break;
4973
4974     case GTK_DELETE_WHITESPACE:
4975       {
4976         find_whitepace_region (&insert, &start, &end);
4977       }
4978       break;
4979
4980     default:
4981       break;
4982     }
4983
4984   if (!gtk_text_iter_equal (&start, &end))
4985     {
4986       gtk_text_buffer_begin_user_action (get_buffer (text_view));
4987
4988       if (gtk_text_buffer_delete_interactive (get_buffer (text_view), &start, &end,
4989                                               text_view->editable))
4990         {
4991           if (leave_one)
4992             gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view),
4993                                                           " ", 1,
4994                                                           text_view->editable);
4995         }
4996
4997       gtk_text_buffer_end_user_action (get_buffer (text_view));
4998
4999       DV(g_print (G_STRLOC": scrolling onscreen\n"));
5000       gtk_text_view_scroll_mark_onscreen (text_view,
5001                                           gtk_text_buffer_get_mark (get_buffer (text_view), "insert"));
5002     }
5003 }
5004
5005 static void
5006 gtk_text_view_cut_clipboard (GtkTextView *text_view)
5007 {
5008   GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5009                                                       GDK_SELECTION_CLIPBOARD);
5010   
5011   gtk_text_buffer_cut_clipboard (get_buffer (text_view),
5012                                  clipboard,
5013                                  text_view->editable);
5014   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5015   gtk_text_view_scroll_mark_onscreen (text_view,
5016                                       gtk_text_buffer_get_mark (get_buffer (text_view),
5017                                                                 "insert"));
5018 }
5019
5020 static void
5021 gtk_text_view_copy_clipboard (GtkTextView *text_view)
5022 {
5023   GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5024                                                       GDK_SELECTION_CLIPBOARD);
5025   
5026   gtk_text_buffer_copy_clipboard (get_buffer (text_view),
5027                                   clipboard);
5028   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5029   gtk_text_view_scroll_mark_onscreen (text_view,
5030                                       gtk_text_buffer_get_mark (get_buffer (text_view),
5031                                                                 "insert"));
5032 }
5033
5034 static void
5035 gtk_text_view_paste_clipboard (GtkTextView *text_view)
5036 {
5037   GtkClipboard *clipboard = gtk_widget_get_clipboard (GTK_WIDGET (text_view),
5038                                                       GDK_SELECTION_CLIPBOARD);
5039   
5040   gtk_text_buffer_paste_clipboard (get_buffer (text_view),
5041                                    clipboard,
5042                                    NULL,
5043                                    text_view->editable);
5044   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5045   gtk_text_view_scroll_mark_onscreen (text_view,
5046                                       gtk_text_buffer_get_mark (get_buffer (text_view),
5047                                                                 "insert"));
5048 }
5049
5050 static void
5051 gtk_text_view_toggle_overwrite (GtkTextView *text_view)
5052 {
5053   text_view->overwrite_mode = !text_view->overwrite_mode;
5054 }
5055
5056 static void
5057 gtk_text_view_move_focus (GtkTextView     *text_view,
5058                           GtkDirectionType direction_type)
5059 {
5060   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (text_view));
5061
5062   if (!GTK_WIDGET_TOPLEVEL (toplevel))
5063     return;
5064
5065   /* Propagate to toplevel */
5066   g_signal_emit_by_name (toplevel, "move_focus", direction_type);
5067 }
5068
5069 /*
5070  * Selections
5071  */
5072
5073 static void
5074 gtk_text_view_unselect (GtkTextView *text_view)
5075 {
5076   GtkTextIter insert;
5077
5078   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5079                                     &insert,
5080                                     gtk_text_buffer_get_mark (get_buffer (text_view),
5081                                                               "insert"));
5082
5083   gtk_text_buffer_move_mark (get_buffer (text_view),
5084                              gtk_text_buffer_get_mark (get_buffer (text_view),
5085                                                        "selection_bound"),
5086                              &insert);
5087 }
5088
5089 static void
5090 move_mark_to_pointer_and_scroll (GtkTextView *text_view,
5091                                  const gchar *mark_name)
5092 {
5093   gint x, y;
5094   GdkModifierType state;
5095   GtkTextIter newplace;
5096
5097   /*   DV(g_print (G_STRLOC": begin\n")); */
5098   
5099   gdk_window_get_pointer (text_view->text_window->bin_window,
5100                           &x, &y, &state);
5101
5102   /*   DV(g_print (G_STRLOC": get iter at pixel\n"); */
5103   gtk_text_layout_get_iter_at_pixel (text_view->layout,
5104                                      &newplace,
5105                                      x + text_view->xoffset,
5106                                      y + text_view->yoffset);
5107
5108   {
5109     GtkTextMark *mark =
5110       gtk_text_buffer_get_mark (get_buffer (text_view), mark_name);
5111
5112     /* This may invalidate the layout */
5113     DV(g_print (G_STRLOC": move mark\n"));
5114     gtk_text_buffer_move_mark (get_buffer (text_view),
5115                                mark,
5116                                &newplace);
5117
5118     DV(g_print (G_STRLOC": scrolling onscreen\n"));
5119     gtk_text_view_scroll_mark_onscreen (text_view, mark);
5120   }
5121
5122   DV (g_print ("first validate idle leaving %s is %d\n",
5123                G_STRLOC, text_view->first_validate_idle));
5124 }
5125
5126 static gint
5127 selection_scan_timeout (gpointer data)
5128 {
5129   GtkTextView *text_view;
5130
5131   GDK_THREADS_ENTER ();
5132   
5133   text_view = GTK_TEXT_VIEW (data);
5134
5135   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5136   move_mark_to_pointer_and_scroll (text_view, "insert");
5137
5138   GDK_THREADS_LEAVE ();
5139   
5140   return TRUE; /* remain installed. */
5141 }
5142
5143 #define DND_SCROLL_MARGIN 0.20
5144
5145 static gint
5146 drag_scan_timeout (gpointer data)
5147 {
5148   GtkTextView *text_view;
5149   gint x, y;
5150   GdkModifierType state;
5151   GtkTextIter newplace;
5152
5153   GDK_THREADS_ENTER ();
5154   
5155   text_view = GTK_TEXT_VIEW (data);
5156
5157   gdk_window_get_pointer (text_view->text_window->bin_window,
5158                           &x, &y, &state);
5159   
5160   gtk_text_layout_get_iter_at_pixel (text_view->layout,
5161                                      &newplace,
5162                                      x + text_view->xoffset,
5163                                      y + text_view->yoffset);
5164   
5165   gtk_text_buffer_move_mark (get_buffer (text_view),
5166                              text_view->dnd_mark,
5167                              &newplace);
5168
5169   DV(g_print (G_STRLOC": scrolling onscreen\n"));
5170   gtk_text_view_scroll_to_mark (text_view,
5171                                 text_view->dnd_mark,
5172                                 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5173
5174   GDK_THREADS_LEAVE ();
5175   
5176   return TRUE;
5177 }
5178
5179 static gint
5180 selection_motion_event_handler (GtkTextView *text_view, GdkEventMotion *event, gpointer data)
5181 {
5182   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5183   move_mark_to_pointer_and_scroll (text_view, "insert");
5184
5185   /* If we had to scroll offscreen, insert a timeout to do so
5186    * again. Note that in the timeout, even if the mouse doesn't
5187    * move, due to this scroll xoffset/yoffset will have changed
5188    * and we'll need to scroll again.
5189    */
5190   if (text_view->scroll_timeout != 0) /* reset on every motion event */
5191     gtk_timeout_remove (text_view->scroll_timeout);
5192   
5193   text_view->scroll_timeout =
5194     gtk_timeout_add (50, selection_scan_timeout, text_view);
5195
5196   return TRUE;
5197 }
5198
5199 static void
5200 gtk_text_view_start_selection_drag (GtkTextView       *text_view,
5201                                     const GtkTextIter *iter,
5202                                     GdkEventButton    *button)
5203 {
5204   GtkTextIter newplace;
5205   GtkTextBuffer *buffer;
5206   
5207   g_return_if_fail (text_view->selection_drag_handler == 0);
5208
5209   gtk_grab_add (GTK_WIDGET (text_view));
5210
5211   buffer = get_buffer (text_view);
5212   
5213   newplace = *iter;
5214
5215   if (button->state & GDK_SHIFT_MASK)
5216     {
5217       /* Extend selection */
5218       GtkTextIter start, end;
5219
5220       gtk_text_buffer_get_selection_bounds (buffer, &start, &end);
5221
5222       if (gtk_text_iter_compare (&newplace, &start) <= 0)
5223         {
5224           gtk_text_buffer_move_mark_by_name (buffer, "insert",
5225                                              &newplace);
5226
5227           gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5228                                              &end);
5229         }
5230       else if (gtk_text_iter_compare (&newplace, &end) >= 0)
5231         {
5232           gtk_text_buffer_move_mark_by_name (buffer, "insert",
5233                                              &newplace);
5234
5235           gtk_text_buffer_move_mark_by_name (buffer, "selection_bound",
5236                                              &start);
5237         }
5238     }
5239   else
5240     {
5241       /* Replace selection */
5242       gtk_text_buffer_place_cursor (buffer, &newplace);
5243     }
5244
5245   text_view->selection_drag_handler = g_signal_connect (text_view,
5246                                                         "motion_notify_event",
5247                                                         G_CALLBACK (selection_motion_event_handler),
5248                                                         NULL);
5249 }
5250
5251 /* returns whether we were really dragging */
5252 static gboolean
5253 gtk_text_view_end_selection_drag (GtkTextView *text_view, GdkEventButton *event)
5254 {
5255   if (text_view->selection_drag_handler == 0)
5256     return FALSE;
5257
5258   g_signal_handler_disconnect (text_view, text_view->selection_drag_handler);
5259   text_view->selection_drag_handler = 0;
5260
5261   if (text_view->scroll_timeout != 0)
5262     {
5263       gtk_timeout_remove (text_view->scroll_timeout);
5264       text_view->scroll_timeout = 0;
5265     }
5266
5267   /* one last update to current position */
5268   DV(g_print (G_STRLOC": calling move_mark_to_pointer_and_scroll\n"));
5269   move_mark_to_pointer_and_scroll (text_view, "insert");
5270
5271   gtk_grab_remove (GTK_WIDGET (text_view));
5272
5273   return TRUE;
5274 }
5275
5276 /*
5277  * Layout utils
5278  */
5279
5280 static void
5281 gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
5282                                          GtkTextAttributes  *values,
5283                                          GtkStyle           *style)
5284 {
5285   values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
5286   values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
5287
5288   if (values->font)
5289     pango_font_description_free (values->font);
5290
5291   values->font = pango_font_description_copy (style->font_desc);
5292 }
5293
5294 static void
5295 gtk_text_view_check_keymap_direction (GtkTextView *text_view)
5296 {
5297   if (text_view->layout)
5298     {
5299       gboolean split_cursor;
5300       GtkTextDirection new_dir;
5301       GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (text_view));
5302   
5303       g_object_get (G_OBJECT (settings),
5304                     "gtk-split-cursor", &split_cursor,
5305                     NULL);
5306       if (split_cursor)
5307         {
5308           new_dir = GTK_TEXT_DIR_NONE;
5309         }
5310       else
5311         {
5312           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (text_view)));
5313           new_dir = (gdk_keymap_get_direction (keymap) == PANGO_DIRECTION_LTR) ?
5314             GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5315         }
5316       
5317       if (text_view->layout->cursor_direction != new_dir)
5318         gtk_text_layout_set_cursor_direction (text_view->layout, new_dir);
5319     }
5320 }
5321
5322 static void
5323 gtk_text_view_ensure_layout (GtkTextView *text_view)
5324 {
5325   GtkWidget *widget;
5326
5327   widget = GTK_WIDGET (text_view);
5328
5329   if (text_view->layout == NULL)
5330     {
5331       GtkTextAttributes *style;
5332       PangoContext *ltr_context, *rtl_context;
5333       GSList *tmp_list;
5334
5335       DV(g_print(G_STRLOC"\n"));
5336       
5337       text_view->layout = gtk_text_layout_new ();
5338
5339       g_signal_connect (text_view->layout,
5340                         "invalidated",
5341                         G_CALLBACK (invalidated_handler),
5342                         text_view);
5343
5344       g_signal_connect (text_view->layout,
5345                         "changed",
5346                         G_CALLBACK (changed_handler),
5347                         text_view);
5348
5349       g_signal_connect (text_view->layout,
5350                         "allocate_child",
5351                         G_CALLBACK (gtk_text_view_child_allocated),
5352                         text_view);
5353       
5354       if (get_buffer (text_view))
5355         gtk_text_layout_set_buffer (text_view->layout, get_buffer (text_view));
5356
5357       if ((GTK_WIDGET_HAS_FOCUS (text_view) && text_view->cursor_visible))
5358         gtk_text_view_pend_cursor_blink (text_view);
5359       else
5360         gtk_text_layout_set_cursor_visible (text_view->layout, FALSE);
5361
5362       ltr_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5363       pango_context_set_base_dir (ltr_context, PANGO_DIRECTION_LTR);
5364       rtl_context = gtk_widget_create_pango_context (GTK_WIDGET (text_view));
5365       pango_context_set_base_dir (rtl_context, PANGO_DIRECTION_RTL);
5366
5367       gtk_text_layout_set_contexts (text_view->layout, ltr_context, rtl_context);
5368
5369       g_object_unref (ltr_context);
5370       g_object_unref (rtl_context);
5371
5372       gtk_text_view_check_keymap_direction (text_view);
5373
5374       style = gtk_text_attributes_new ();
5375
5376       gtk_widget_ensure_style (widget);
5377       gtk_text_view_set_attributes_from_style (text_view,
5378                                                style, widget->style);
5379
5380       style->pixels_above_lines = text_view->pixels_above_lines;
5381       style->pixels_below_lines = text_view->pixels_below_lines;
5382       style->pixels_inside_wrap = text_view->pixels_inside_wrap;
5383       style->left_margin = text_view->left_margin;
5384       style->right_margin = text_view->right_margin;
5385       style->indent = text_view->indent;
5386       style->tabs = text_view->tabs ? pango_tab_array_copy (text_view->tabs) : NULL;
5387
5388       style->wrap_mode = text_view->wrap_mode;
5389       style->justification = text_view->justify;
5390       style->direction = gtk_widget_get_direction (GTK_WIDGET (text_view));
5391
5392       gtk_text_layout_set_default_style (text_view->layout, style);
5393
5394       gtk_text_attributes_unref (style);
5395
5396       /* Set layout for all anchored children */
5397
5398       tmp_list = text_view->children;
5399       while (tmp_list != NULL)
5400         {
5401           GtkTextViewChild *vc = tmp_list->data;
5402
5403           if (vc->anchor)
5404             {
5405               gtk_text_anchored_child_set_layout (vc->widget,
5406                                                   text_view->layout);
5407               /* vc may now be invalid! */
5408             }
5409
5410           tmp_list = g_slist_next (tmp_list);
5411         }
5412
5413       gtk_text_view_invalidate (text_view);
5414     }
5415 }
5416
5417 /**
5418  * gtk_text_view_get_default_attributes:
5419  * @text_view: a #GtkTextView
5420  * 
5421  * Obtains a copy of the default text attributes. These are the
5422  * attributes used for text unless a tag overrides them.
5423  * You'd typically pass the default attributes in to
5424  * gtk_text_iter_get_attributes() in order to get the
5425  * attributes in effect at a given text position.
5426  *
5427  * The return value is a copy owned by the caller of this function,
5428  * and should be freed.
5429  * 
5430  * Return value: a new #GtkTextAttributes
5431  **/
5432 GtkTextAttributes*
5433 gtk_text_view_get_default_attributes (GtkTextView *text_view)
5434 {
5435   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
5436   
5437   gtk_text_view_ensure_layout (text_view);
5438
5439   return gtk_text_attributes_copy (text_view->layout->default_style);
5440 }
5441
5442 static void
5443 gtk_text_view_destroy_layout (GtkTextView *text_view)
5444 {
5445   if (text_view->layout)
5446     {
5447       GSList *tmp_list;
5448
5449       gtk_text_view_remove_validate_idles (text_view);
5450
5451       g_signal_handlers_disconnect_by_func (text_view->layout,
5452                                             invalidated_handler,
5453                                             text_view);
5454       g_signal_handlers_disconnect_by_func (text_view->layout,
5455                                             changed_handler, 
5456                                             text_view);
5457       
5458       /* Remove layout from all anchored children */
5459       tmp_list = text_view->children;
5460       while (tmp_list != NULL)
5461         {
5462           GtkTextViewChild *vc = tmp_list->data;
5463
5464           if (vc->anchor)
5465             {
5466               gtk_text_anchored_child_set_layout (vc->widget, NULL);
5467               /* vc may now be invalid! */
5468             }
5469
5470           tmp_list = g_slist_next (tmp_list);
5471         }
5472       
5473       gtk_text_view_stop_cursor_blink (text_view);
5474       gtk_text_view_end_selection_drag (text_view, NULL);
5475
5476       g_object_unref (text_view->layout);
5477       text_view->layout = NULL;
5478     }
5479 }
5480
5481 static void
5482 gtk_text_view_reset_im_context (GtkTextView *text_view)
5483 {
5484   if (text_view->need_im_reset)
5485     {
5486       text_view->need_im_reset = FALSE;
5487       gtk_im_context_reset (text_view->im_context);
5488     }
5489 }
5490
5491 /*
5492  * DND feature
5493  */
5494
5495 static void
5496 gtk_text_view_start_selection_dnd (GtkTextView       *text_view,
5497                                    const GtkTextIter *iter,
5498                                    GdkEventMotion    *event)
5499 {
5500   GdkDragContext *context;
5501   GtkTargetList *target_list;
5502
5503   text_view->drag_start_x = -1;
5504   text_view->drag_start_y = -1;
5505   text_view->pending_place_cursor_button = 0;
5506   
5507   target_list = gtk_target_list_new (target_table,
5508                                      G_N_ELEMENTS (target_table));
5509
5510   context = gtk_drag_begin (GTK_WIDGET (text_view), target_list,
5511                             GDK_ACTION_COPY | GDK_ACTION_MOVE,
5512                             1, (GdkEvent*)event);
5513
5514   gtk_target_list_unref (target_list);
5515
5516   gtk_drag_set_icon_default (context);
5517 }
5518
5519 static void
5520 gtk_text_view_drag_begin (GtkWidget        *widget,
5521                           GdkDragContext   *context)
5522 {
5523   /* do nothing */
5524 }
5525
5526 static void
5527 gtk_text_view_drag_end (GtkWidget        *widget,
5528                         GdkDragContext   *context)
5529 {
5530   GtkTextView *text_view;
5531
5532   text_view = GTK_TEXT_VIEW (widget);
5533 }
5534
5535 static void
5536 gtk_text_view_drag_data_get (GtkWidget        *widget,
5537                              GdkDragContext   *context,
5538                              GtkSelectionData *selection_data,
5539                              guint             info,
5540                              guint             time)
5541 {
5542   GtkTextView *text_view;
5543
5544   text_view = GTK_TEXT_VIEW (widget);
5545
5546   if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5547     {
5548       GtkTextBuffer *buffer = gtk_text_view_get_buffer (text_view);
5549
5550       gtk_selection_data_set (selection_data,
5551                               gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
5552                               8, /* bytes */
5553                               (void*)&buffer,
5554                               sizeof (buffer));
5555     }
5556   else
5557     {
5558       gchar *str;
5559       GtkTextIter start;
5560       GtkTextIter end;
5561
5562       str = NULL;
5563
5564       if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5565                                                 &start, &end))
5566         {
5567           /* Extract the selected text */
5568           str = gtk_text_iter_get_visible_text (&start, &end);
5569         }
5570
5571       if (str)
5572         {
5573           gtk_selection_data_set_text (selection_data, str, -1);
5574           g_free (str);
5575         }
5576     }
5577 }
5578
5579 static void
5580 gtk_text_view_drag_data_delete (GtkWidget        *widget,
5581                                 GdkDragContext   *context)
5582 {
5583   GtkTextView *text_view;
5584
5585   text_view = GTK_TEXT_VIEW (widget);
5586
5587   gtk_text_buffer_delete_selection (GTK_TEXT_VIEW (widget)->buffer,
5588                                     TRUE, GTK_TEXT_VIEW (widget)->editable);
5589 }
5590
5591 static void
5592 gtk_text_view_drag_leave (GtkWidget        *widget,
5593                           GdkDragContext   *context,
5594                           guint             time)
5595 {
5596   GtkTextView *text_view;
5597
5598   text_view = GTK_TEXT_VIEW (widget);
5599
5600   gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5601   
5602   if (text_view->scroll_timeout != 0)
5603     gtk_timeout_remove (text_view->scroll_timeout);
5604
5605   text_view->scroll_timeout = 0;
5606 }
5607
5608 static gboolean
5609 gtk_text_view_drag_motion (GtkWidget        *widget,
5610                            GdkDragContext   *context,
5611                            gint              x,
5612                            gint              y,
5613                            guint             time)
5614 {
5615   GtkTextIter newplace;
5616   GtkTextView *text_view;
5617   GtkTextIter start;
5618   GtkTextIter end;
5619   GdkRectangle target_rect;
5620   gint bx, by;
5621   GdkDragAction suggested_action = 0;
5622   
5623   text_view = GTK_TEXT_VIEW (widget);
5624
5625   target_rect = text_view->text_window->allocation;
5626   
5627   if (x < target_rect.x ||
5628       y < target_rect.y ||
5629       x > (target_rect.x + target_rect.width) ||
5630       y > (target_rect.y + target_rect.height))
5631     return FALSE; /* outside the text window, allow parent widgets to handle event */
5632
5633   gtk_text_view_window_to_buffer_coords (text_view,
5634                                          GTK_TEXT_WINDOW_WIDGET,
5635                                          x, y,
5636                                          &bx, &by);
5637
5638   gtk_text_layout_get_iter_at_pixel (text_view->layout,
5639                                      &newplace,
5640                                      bx, by);  
5641
5642   if (gtk_drag_dest_find_target (widget, context,
5643                                  gtk_drag_dest_get_target_list (widget)) == GDK_NONE)
5644     {
5645       /* can't accept any of the offered targets */
5646     }                                 
5647   else if (gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
5648                                             &start, &end) &&
5649            gtk_text_iter_compare (&newplace, &start) >= 0 &&
5650            gtk_text_iter_compare (&newplace, &end) <= 0)
5651     {
5652       /* We're inside the selection. */
5653     }
5654   else
5655     {      
5656       if (gtk_text_iter_can_insert (&newplace, text_view->editable))
5657         {
5658           GtkWidget *source_widget;
5659           
5660           suggested_action = context->suggested_action;
5661           
5662           source_widget = gtk_drag_get_source_widget (context);
5663           
5664           if (source_widget == widget)
5665             {
5666               /* Default to MOVE, unless the user has
5667                * pressed ctrl or alt to affect available actions
5668                */
5669               if ((context->actions & GDK_ACTION_MOVE) != 0)
5670                 suggested_action = GDK_ACTION_MOVE;
5671             }
5672         }
5673       else
5674         {
5675           /* Can't drop here. */
5676         }
5677     }
5678
5679   if (suggested_action != 0)
5680     {
5681       gtk_text_mark_set_visible (text_view->dnd_mark,
5682                                  text_view->cursor_visible);
5683       
5684       gdk_drag_status (context, suggested_action, time);
5685     }
5686   else
5687     {
5688       gdk_drag_status (context, 0, time);
5689       gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5690     }
5691       
5692   gtk_text_buffer_move_mark (get_buffer (text_view),
5693                              text_view->dnd_mark,
5694                              &newplace);
5695
5696   DV(g_print (G_STRLOC": scrolling to mark\n"));
5697   gtk_text_view_scroll_to_mark (text_view,
5698                                 text_view->dnd_mark,
5699                                 DND_SCROLL_MARGIN, FALSE, 0.0, 0.0);
5700   
5701   if (text_view->scroll_timeout != 0) /* reset on every motion event */
5702     gtk_timeout_remove (text_view->scroll_timeout);
5703       
5704   text_view->scroll_timeout =
5705     gtk_timeout_add (50, drag_scan_timeout, text_view);
5706
5707   /* TRUE return means don't propagate the drag motion to parent
5708    * widgets that may also be drop sites.
5709    */
5710   return TRUE;
5711 }
5712
5713 static gboolean
5714 gtk_text_view_drag_drop (GtkWidget        *widget,
5715                          GdkDragContext   *context,
5716                          gint              x,
5717                          gint              y,
5718                          guint             time)
5719 {
5720   GtkTextView *text_view;
5721   GtkTextIter drop_point;
5722   GdkAtom target = GDK_NONE;
5723   
5724   text_view = GTK_TEXT_VIEW (widget);
5725   
5726   if (text_view->scroll_timeout != 0)
5727     gtk_timeout_remove (text_view->scroll_timeout);
5728
5729   text_view->scroll_timeout = 0;
5730
5731   gtk_text_mark_set_visible (text_view->dnd_mark, FALSE);
5732
5733   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5734                                     &drop_point,
5735                                     text_view->dnd_mark);
5736
5737   if (gtk_text_iter_can_insert (&drop_point, text_view->editable))
5738     target = gtk_drag_dest_find_target (widget, context, NULL);
5739
5740   if (target != GDK_NONE)
5741     gtk_drag_get_data (widget, context, target, time);
5742   else
5743     gtk_drag_finish (context, FALSE, FALSE, time);
5744
5745   return TRUE;
5746 }
5747
5748 static void
5749 insert_text_data (GtkTextView      *text_view,
5750                   GtkTextIter      *drop_point,
5751                   GtkSelectionData *selection_data)
5752 {
5753   gchar *str;
5754
5755   str = gtk_selection_data_get_text (selection_data);
5756
5757   if (str)
5758     {
5759       gtk_text_buffer_insert_interactive (get_buffer (text_view),
5760                                           drop_point, str, -1,
5761                                           text_view->editable);
5762       g_free (str);
5763     }
5764 }
5765
5766 static void
5767 gtk_text_view_drag_data_received (GtkWidget        *widget,
5768                                   GdkDragContext   *context,
5769                                   gint              x,
5770                                   gint              y,
5771                                   GtkSelectionData *selection_data,
5772                                   guint             info,
5773                                   guint             time)
5774 {
5775   GtkTextIter drop_point;
5776   GtkTextView *text_view;
5777   gboolean success = FALSE;
5778
5779   text_view = GTK_TEXT_VIEW (widget);
5780
5781   if (!text_view->dnd_mark)
5782     goto done;
5783
5784   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
5785                                     &drop_point,
5786                                     text_view->dnd_mark);
5787   
5788   if (!gtk_text_iter_can_insert (&drop_point, text_view->editable))
5789     goto done;
5790
5791   if (selection_data->target == gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
5792     {
5793       GtkTextBuffer *src_buffer = NULL;
5794       GtkTextIter start, end;
5795       gboolean copy_tags = TRUE;
5796
5797       if (selection_data->length != sizeof (src_buffer))
5798         return;
5799
5800       memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
5801
5802       if (src_buffer == NULL)
5803         return;
5804
5805       g_return_if_fail (GTK_IS_TEXT_BUFFER (src_buffer));
5806
5807       if (gtk_text_buffer_get_tag_table (src_buffer) !=
5808           gtk_text_buffer_get_tag_table (get_buffer (text_view)))
5809         copy_tags = FALSE;
5810
5811       if (gtk_text_buffer_get_selection_bounds (src_buffer,
5812                                                 &start,
5813                                                 &end))
5814         {
5815           if (copy_tags)
5816             gtk_text_buffer_insert_range_interactive (get_buffer (text_view),
5817                                                       &drop_point,
5818                                                       &start,
5819                                                       &end,
5820                                                       text_view->editable);
5821           else
5822             {
5823               gchar *str;
5824
5825               str = gtk_text_iter_get_visible_text (&start, &end);
5826               gtk_text_buffer_insert_interactive (get_buffer (text_view),
5827                                                   &drop_point, str, -1,
5828                                                   text_view->editable);
5829               g_free (str);
5830             }
5831         }
5832     }
5833   else
5834     insert_text_data (text_view, &drop_point, selection_data);
5835
5836   success = TRUE;
5837
5838  done:
5839   gtk_drag_finish (context, success,
5840                    success && context->action == GDK_ACTION_MOVE,
5841                    time);
5842 }
5843
5844 static GtkAdjustment*
5845 get_hadjustment (GtkTextView *text_view)
5846 {
5847   if (text_view->hadjustment == NULL)
5848     gtk_text_view_set_scroll_adjustments (text_view,
5849                                           NULL, /* forces creation */
5850                                           text_view->vadjustment);
5851
5852   return text_view->hadjustment;
5853 }
5854
5855 static GtkAdjustment*
5856 get_vadjustment (GtkTextView *text_view)
5857 {
5858   if (text_view->vadjustment == NULL)
5859     gtk_text_view_set_scroll_adjustments (text_view,
5860                                           text_view->hadjustment,
5861                                           NULL); /* forces creation */
5862   return text_view->vadjustment;
5863 }
5864
5865
5866 static void
5867 gtk_text_view_set_scroll_adjustments (GtkTextView   *text_view,
5868                                       GtkAdjustment *hadj,
5869                                       GtkAdjustment *vadj)
5870 {
5871   gboolean need_adjust = FALSE;
5872
5873   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
5874
5875   if (hadj)
5876     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
5877   else
5878     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5879   if (vadj)
5880     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
5881   else
5882     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
5883
5884   if (text_view->hadjustment && (text_view->hadjustment != hadj))
5885     {
5886       g_signal_handlers_disconnect_by_func (text_view->hadjustment,
5887                                             gtk_text_view_value_changed,
5888                                             text_view);
5889       g_object_unref (text_view->hadjustment);
5890     }
5891
5892   if (text_view->vadjustment && (text_view->vadjustment != vadj))
5893     {
5894       g_signal_handlers_disconnect_by_func (text_view->vadjustment,
5895                                             gtk_text_view_value_changed,
5896                                             text_view);
5897       g_object_unref (text_view->vadjustment);
5898     }
5899
5900   if (text_view->hadjustment != hadj)
5901     {
5902       text_view->hadjustment = hadj;
5903       g_object_ref (text_view->hadjustment);
5904       gtk_object_sink (GTK_OBJECT (text_view->hadjustment));
5905       
5906       g_signal_connect (text_view->hadjustment, "value_changed",
5907                         G_CALLBACK (gtk_text_view_value_changed),
5908                         text_view);
5909       need_adjust = TRUE;
5910     }
5911
5912   if (text_view->vadjustment != vadj)
5913     {
5914       text_view->vadjustment = vadj;
5915       g_object_ref (text_view->vadjustment);
5916       gtk_object_sink (GTK_OBJECT (text_view->vadjustment));
5917       
5918       g_signal_connect (text_view->vadjustment, "value_changed",
5919                         G_CALLBACK (gtk_text_view_value_changed),
5920                         text_view);
5921       need_adjust = TRUE;
5922     }
5923
5924   if (need_adjust)
5925     gtk_text_view_value_changed (NULL, text_view);
5926 }
5927
5928 /* FIXME this adjust_allocation is a big cut-and-paste from
5929  * GtkCList, needs to be some "official" way to do this
5930  * factored out.
5931  */
5932 typedef struct
5933 {
5934   GdkWindow *window;
5935   int dx;
5936   int dy;
5937 } ScrollData;
5938
5939 /* The window to which widget->window is relative */
5940 #define ALLOCATION_WINDOW(widget)               \
5941    (GTK_WIDGET_NO_WINDOW (widget) ?             \
5942     (widget)->window :                          \
5943      gdk_window_get_parent ((widget)->window))
5944
5945 static void
5946 adjust_allocation_recurse (GtkWidget *widget,
5947                            gpointer   data)
5948 {
5949   ScrollData *scroll_data = data;
5950
5951   /* Need to really size allocate instead of just poking
5952    * into widget->allocation if the widget is not realized.
5953    * FIXME someone figure out why this was.
5954    */
5955   if (!GTK_WIDGET_REALIZED (widget))
5956     {
5957       if (GTK_WIDGET_VISIBLE (widget))
5958         {
5959           GdkRectangle tmp_rectangle = widget->allocation;
5960           tmp_rectangle.x += scroll_data->dx;
5961           tmp_rectangle.y += scroll_data->dy;
5962           
5963           gtk_widget_size_allocate (widget, &tmp_rectangle);
5964         }
5965     }
5966   else
5967     {
5968       if (ALLOCATION_WINDOW (widget) == scroll_data->window)
5969         {
5970           widget->allocation.x += scroll_data->dx;
5971           widget->allocation.y += scroll_data->dy;
5972           
5973           if (GTK_IS_CONTAINER (widget))
5974             gtk_container_forall (GTK_CONTAINER (widget),
5975                                   adjust_allocation_recurse,
5976                                   data);
5977         }
5978     }
5979 }
5980
5981 static void
5982 adjust_allocation (GtkWidget *widget,
5983                    int        dx,
5984                    int        dy)
5985 {
5986   ScrollData scroll_data;
5987
5988   if (GTK_WIDGET_REALIZED (widget))
5989     scroll_data.window = ALLOCATION_WINDOW (widget);
5990   else
5991     scroll_data.window = NULL;
5992     
5993   scroll_data.dx = dx;
5994   scroll_data.dy = dy;
5995   
5996   adjust_allocation_recurse (widget, &scroll_data);
5997 }
5998             
5999 static void
6000 gtk_text_view_value_changed (GtkAdjustment *adj,
6001                              GtkTextView   *text_view)
6002 {
6003   GtkTextIter iter;
6004   gint line_top;
6005   gint dx = 0;
6006   gint dy = 0;
6007   
6008   /* Note that we oddly call this function with adj == NULL
6009    * sometimes
6010    */
6011   
6012   text_view->onscreen_validated = FALSE;
6013
6014   DV(g_print(">Scroll offset changed %s/%g, onscreen_validated = FALSE ("G_STRLOC")\n",
6015              adj == text_view->hadjustment ? "hadj" : adj == text_view->vadjustment ? "vadj" : "none",
6016              adj ? adj->value : 0.0));
6017   
6018   if (adj == text_view->hadjustment)
6019     {
6020       dx = text_view->xoffset - (gint)adj->value;
6021       text_view->xoffset = adj->value;
6022     }
6023   else if (adj == text_view->vadjustment)
6024     {
6025       dy = text_view->yoffset - (gint)adj->value;
6026       text_view->yoffset = adj->value;
6027
6028       if (text_view->layout)
6029         {
6030           gtk_text_layout_get_line_at_y (text_view->layout, &iter, adj->value, &line_top);
6031
6032           gtk_text_buffer_move_mark (get_buffer (text_view), text_view->first_para_mark, &iter);
6033
6034           text_view->first_para_pixels = adj->value - line_top;
6035         }
6036     }
6037   
6038   if (dx != 0 || dy != 0)
6039     {
6040       GSList *tmp_list;
6041
6042       if (GTK_WIDGET_REALIZED (text_view))
6043         {
6044           if (dy != 0)
6045             {
6046               if (text_view->left_window)
6047                 text_window_scroll (text_view->left_window, 0, dy);
6048               if (text_view->right_window)
6049                 text_window_scroll (text_view->right_window, 0, dy);
6050             }
6051       
6052           if (dx != 0)
6053             {
6054               if (text_view->top_window)
6055                 text_window_scroll (text_view->top_window, dx, 0);
6056               if (text_view->bottom_window)
6057                 text_window_scroll (text_view->bottom_window, dx, 0);
6058             }
6059       
6060           /* It looks nicer to scroll the main area last, because
6061            * it takes a while, and making the side areas update
6062            * afterward emphasizes the slowness of scrolling the
6063            * main area.
6064            */
6065           text_window_scroll (text_view->text_window, dx, dy);
6066         }
6067       
6068       /* Children are now "moved" in the text window, poke
6069        * into widget->allocation for each child
6070        */
6071       tmp_list = text_view->children;
6072       while (tmp_list != NULL)
6073         {
6074           GtkTextViewChild *child = tmp_list->data;
6075           
6076           if (child->anchor)
6077             adjust_allocation (child->widget, dx, dy);
6078           
6079           tmp_list = g_slist_next (tmp_list);
6080         }
6081     }
6082
6083   /* This could result in invalidation, which would install the
6084    * first_validate_idle, which would validate onscreen;
6085    * but we're going to go ahead and validate here, so
6086    * first_validate_idle shouldn't have anything to do.
6087    */
6088   gtk_text_view_update_layout_width (text_view);
6089   
6090   /* note that validation of onscreen could invoke this function
6091    * recursively, by scrolling to maintain first_para, or in response
6092    * to updating the layout width, however there is no problem with
6093    * that, or shouldn't be.
6094    */
6095   gtk_text_view_validate_onscreen (text_view);
6096   
6097   /* process exposes */
6098   if (GTK_WIDGET_REALIZED (text_view))
6099     {
6100       DV (g_print ("Processing updates (%s)\n", G_STRLOC));
6101       
6102       if (text_view->left_window)
6103         gdk_window_process_updates (text_view->left_window->bin_window, TRUE);
6104
6105       if (text_view->right_window)
6106         gdk_window_process_updates (text_view->right_window->bin_window, TRUE);
6107
6108       if (text_view->top_window)
6109         gdk_window_process_updates (text_view->top_window->bin_window, TRUE);
6110       
6111       if (text_view->bottom_window)
6112         gdk_window_process_updates (text_view->bottom_window->bin_window, TRUE);
6113   
6114       gdk_window_process_updates (text_view->text_window->bin_window, TRUE);
6115     }
6116
6117   /* If this got installed, get rid of it, it's just a waste of time. */
6118   if (text_view->first_validate_idle != 0)
6119     {
6120       g_source_remove (text_view->first_validate_idle);
6121       text_view->first_validate_idle = 0;
6122     }
6123
6124   gtk_text_view_update_im_spot_location (text_view);
6125   
6126   DV(g_print(">End scroll offset changed handler ("G_STRLOC")\n"));
6127 }
6128
6129 static void
6130 gtk_text_view_commit_handler (GtkIMContext  *context,
6131                               const gchar   *str,
6132                               GtkTextView   *text_view)
6133 {
6134   gtk_text_view_commit_text (text_view, str);
6135 }
6136
6137 static void
6138 gtk_text_view_commit_text (GtkTextView   *text_view,
6139                            const gchar   *str)
6140 {
6141   gboolean had_selection;
6142   
6143   gtk_text_buffer_begin_user_action (get_buffer (text_view));
6144
6145   had_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6146                                                         NULL, NULL);
6147   
6148   gtk_text_buffer_delete_selection (get_buffer (text_view), TRUE,
6149                                     text_view->editable);
6150
6151   if (!strcmp (str, "\n"))
6152     {
6153       gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), "\n", 1,
6154                                                     text_view->editable);
6155     }
6156   else
6157     {
6158       if (!had_selection && text_view->overwrite_mode)
6159         {
6160           GtkTextIter insert;
6161           
6162           gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6163                                             &insert,
6164                                             gtk_text_buffer_get_mark (get_buffer (text_view),
6165                                                                       "insert"));
6166           if (!gtk_text_iter_ends_line (&insert))
6167             gtk_text_view_delete_from_cursor (text_view, GTK_DELETE_CHARS, 1);
6168         }
6169       gtk_text_buffer_insert_interactive_at_cursor (get_buffer (text_view), str, -1,
6170                                                     text_view->editable);
6171     }
6172
6173   gtk_text_buffer_end_user_action (get_buffer (text_view));
6174
6175   DV(g_print (G_STRLOC": scrolling onscreen\n"));
6176   gtk_text_view_scroll_mark_onscreen (text_view,
6177                                       gtk_text_buffer_get_mark (get_buffer (text_view),
6178                                                                 "insert"));
6179 }
6180
6181 static void
6182 gtk_text_view_preedit_changed_handler (GtkIMContext *context,
6183                                        GtkTextView  *text_view)
6184 {
6185   gchar *str;
6186   PangoAttrList *attrs;
6187   gint cursor_pos;
6188
6189   gtk_im_context_get_preedit_string (context, &str, &attrs, &cursor_pos);
6190   gtk_text_layout_set_preedit_string (text_view->layout, str, attrs, cursor_pos);
6191
6192   pango_attr_list_unref (attrs);
6193   g_free (str);
6194 }
6195
6196 static gboolean
6197 gtk_text_view_retrieve_surrounding_handler (GtkIMContext  *context,
6198                                             GtkTextView   *text_view)
6199 {
6200   GtkTextIter start;
6201   GtkTextIter end;
6202   gint pos;
6203   gchar *text;
6204
6205   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
6206                                     gtk_text_buffer_get_insert (text_view->buffer));
6207   end = start;
6208
6209   pos = gtk_text_iter_get_line_index (&start);
6210   gtk_text_iter_set_line_offset (&start, 0);
6211   gtk_text_iter_forward_to_line_end (&end);
6212
6213   text = gtk_text_iter_get_slice (&start, &end);
6214   gtk_im_context_set_surrounding (context, text, -1, pos);
6215   g_free (text);
6216
6217   return TRUE;
6218 }
6219
6220 static gboolean
6221 gtk_text_view_delete_surrounding_handler (GtkIMContext  *context,
6222                                           gint           offset,
6223                                           gint           n_chars,
6224                                           GtkTextView   *text_view)
6225 {
6226   GtkTextIter start;
6227   GtkTextIter end;
6228
6229   gtk_text_buffer_get_iter_at_mark (text_view->buffer, &start,  
6230                                     gtk_text_buffer_get_insert (text_view->buffer));
6231   end = start;
6232
6233   gtk_text_iter_forward_chars (&start, offset);
6234   gtk_text_iter_forward_chars (&end, offset + n_chars);
6235
6236   gtk_text_buffer_delete (text_view->buffer, &start, &end);
6237
6238   return TRUE;
6239 }
6240
6241 static void
6242 gtk_text_view_mark_set_handler (GtkTextBuffer     *buffer,
6243                                 const GtkTextIter *location,
6244                                 GtkTextMark       *mark,
6245                                 gpointer           data)
6246 {
6247   GtkTextView *text_view = GTK_TEXT_VIEW (data);
6248   gboolean need_reset = FALSE;
6249
6250   if (mark == gtk_text_buffer_get_insert (buffer))
6251     {
6252       text_view->virtual_cursor_x = -1;
6253       text_view->virtual_cursor_y = -1;
6254       gtk_text_view_update_im_spot_location (text_view);
6255       need_reset = TRUE;
6256     }
6257   else if (mark == gtk_text_buffer_get_selection_bound (buffer))
6258     {
6259       need_reset = TRUE;
6260     }
6261
6262   if (need_reset)
6263     gtk_text_view_reset_im_context (text_view);
6264 }
6265
6266 static void
6267 gtk_text_view_get_virtual_cursor_pos (GtkTextView *text_view,
6268                                       gint        *x,
6269                                       gint        *y)
6270 {
6271   GdkRectangle strong_pos;
6272   GtkTextIter insert;
6273
6274   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6275                                     gtk_text_buffer_get_mark (get_buffer (text_view),
6276                                                               "insert"));
6277
6278   if ((x && text_view->virtual_cursor_x == -1) ||
6279       (y && text_view->virtual_cursor_y == -1))
6280     gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6281
6282   if (x)
6283     {
6284       if (text_view->virtual_cursor_x != -1)
6285         *x = text_view->virtual_cursor_x;
6286       else
6287         *x = strong_pos.x;
6288     }
6289
6290   if (y)
6291     {
6292       if (text_view->virtual_cursor_x != -1)
6293         *y = text_view->virtual_cursor_y;
6294       else
6295         *y = strong_pos.y + strong_pos.height / 2;
6296     }
6297 }
6298
6299 static void
6300 gtk_text_view_set_virtual_cursor_pos (GtkTextView *text_view,
6301                                       gint         x,
6302                                       gint         y)
6303 {
6304   GdkRectangle strong_pos;
6305   GtkTextIter insert;
6306
6307   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view), &insert,
6308                                     gtk_text_buffer_get_mark (get_buffer (text_view),
6309                                                               "insert"));
6310
6311   if (x == -1 || y == -1)
6312     gtk_text_layout_get_cursor_locations (text_view->layout, &insert, &strong_pos, NULL);
6313
6314   text_view->virtual_cursor_x = (x == -1) ? strong_pos.x : x;
6315   text_view->virtual_cursor_y = (y == -1) ? strong_pos.y + strong_pos.height / 2 : y;
6316 }
6317
6318 /* Quick hack of a popup menu
6319  */
6320 static void
6321 activate_cb (GtkWidget   *menuitem,
6322              GtkTextView *text_view)
6323 {
6324   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6325   g_signal_emit_by_name (text_view, signal);
6326 }
6327
6328 static void
6329 append_action_signal (GtkTextView  *text_view,
6330                       GtkWidget    *menu,
6331                       const gchar  *stock_id,
6332                       const gchar  *signal,
6333                       gboolean      sensitive)
6334 {
6335   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6336
6337   g_object_set_data (G_OBJECT (menuitem), "gtk-signal", (char *)signal);
6338   g_signal_connect (menuitem, "activate",
6339                     G_CALLBACK (activate_cb), text_view);
6340
6341   gtk_widget_set_sensitive (menuitem, sensitive);
6342   
6343   gtk_widget_show (menuitem);
6344   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6345 }
6346
6347 static void
6348 popup_menu_detach (GtkWidget *attach_widget,
6349                    GtkMenu   *menu)
6350 {
6351   GTK_TEXT_VIEW (attach_widget)->popup_menu = NULL;
6352 }
6353
6354 static void
6355 popup_position_func (GtkMenu   *menu,
6356                      gint      *x,
6357                      gint      *y,
6358                      gboolean  *push_in,
6359                      gpointer   user_data)
6360 {
6361   GtkTextView *text_view;
6362   GtkWidget *widget;
6363   GdkRectangle cursor_rect;
6364   GdkRectangle onscreen_rect;
6365   gint root_x, root_y;
6366   GtkTextIter iter;
6367   GtkRequisition req;      
6368   GdkScreen *screen;
6369   
6370   text_view = GTK_TEXT_VIEW (user_data);
6371   widget = GTK_WIDGET (text_view);
6372   
6373   g_return_if_fail (GTK_WIDGET_REALIZED (text_view));
6374   
6375   screen = gtk_widget_get_screen (widget);
6376
6377   gdk_window_get_origin (widget->window, &root_x, &root_y);
6378
6379   gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6380                                     &iter,
6381                                     gtk_text_buffer_get_insert (get_buffer (text_view)));
6382
6383   gtk_text_view_get_iter_location (text_view,
6384                                    &iter,
6385                                    &cursor_rect);
6386
6387   gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
6388   
6389   gtk_widget_size_request (text_view->popup_menu, &req);
6390
6391   /* can't use rectangle_intersect since cursor rect can have 0 width */
6392   if (cursor_rect.x >= onscreen_rect.x &&
6393       cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
6394       cursor_rect.y >= onscreen_rect.y &&
6395       cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
6396     {    
6397       gtk_text_view_buffer_to_window_coords (text_view,
6398                                              GTK_TEXT_WINDOW_WIDGET,
6399                                              cursor_rect.x, cursor_rect.y,
6400                                              &cursor_rect.x, &cursor_rect.y);
6401
6402       *x = root_x + cursor_rect.x + cursor_rect.width;
6403       *y = root_y + cursor_rect.y + cursor_rect.height;
6404     }
6405   else
6406     {
6407       /* Just center the menu, since cursor is offscreen. */      
6408       *x = root_x + (widget->allocation.width / 2 - req.width / 2);
6409       *y = root_y + (widget->allocation.height / 2 - req.height / 2);      
6410     }
6411
6412   /* Ensure sanity */
6413   *x = CLAMP (*x, root_x, (root_x + widget->allocation.width));
6414   *y = CLAMP (*y, root_y, (root_y + widget->allocation.height));
6415
6416   *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6417   *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6418 }
6419
6420 typedef struct
6421 {
6422   GtkTextView *text_view;
6423   gint button;
6424   guint time;
6425 } PopupInfo;
6426
6427 static gboolean
6428 range_contains_editable_text (const GtkTextIter *start,
6429                               const GtkTextIter *end,
6430                               gboolean default_editability)
6431 {
6432   GtkTextIter iter = *start;
6433
6434   while (gtk_text_iter_compare (&iter, end) < 0)
6435     {
6436       if (gtk_text_iter_editable (&iter, default_editability))
6437         return TRUE;
6438       
6439       gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
6440     }
6441
6442   return FALSE;
6443 }                             
6444
6445 static void
6446 unichar_chosen_func (const char *text,
6447                      gpointer    data)
6448 {
6449   GtkTextView *text_view = GTK_TEXT_VIEW (data);
6450
6451   gtk_text_view_commit_text (text_view, text);
6452 }
6453
6454 static void
6455 popup_targets_received (GtkClipboard     *clipboard,
6456                         GtkSelectionData *data,
6457                         gpointer          user_data)
6458 {
6459   PopupInfo *info = user_data;
6460   GtkTextView *text_view = info->text_view;
6461   
6462   if (GTK_WIDGET_REALIZED (text_view))
6463     {
6464       /* We implicitely rely here on the fact that if we are pasting ourself, we'll
6465        * have text targets as well as the private GTK_TEXT_BUFFER_CONTENTS target.
6466        */
6467       gboolean clipboard_contains_text = gtk_selection_data_targets_include_text (data);
6468       GtkWidget *menuitem;
6469       GtkWidget *submenu;
6470       gboolean have_selection;
6471       gboolean can_insert;
6472       GtkTextIter iter;
6473       GtkTextIter sel_start, sel_end;
6474       
6475       if (text_view->popup_menu)
6476         gtk_widget_destroy (text_view->popup_menu);
6477
6478       text_view->popup_menu = gtk_menu_new ();
6479       
6480       gtk_menu_attach_to_widget (GTK_MENU (text_view->popup_menu),
6481                                  GTK_WIDGET (text_view),
6482                                  popup_menu_detach);
6483       
6484       have_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view),
6485                                                              &sel_start, &sel_end);
6486       
6487       gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
6488                                         &iter,
6489                                         gtk_text_buffer_get_insert (get_buffer (text_view)));
6490       
6491       can_insert = gtk_text_iter_can_insert (&iter, text_view->editable);
6492       
6493       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
6494                             have_selection &&
6495                             range_contains_editable_text (&sel_start, &sel_end,
6496                                                           text_view->editable));
6497       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
6498                             have_selection);
6499       append_action_signal (text_view, text_view->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
6500                             can_insert && clipboard_contains_text);
6501       
6502       menuitem = gtk_separator_menu_item_new ();
6503       gtk_widget_show (menuitem);
6504       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6505       
6506       menuitem = gtk_menu_item_new_with_mnemonic (_("Input _Methods"));
6507       gtk_widget_show (menuitem);
6508       submenu = gtk_menu_new ();
6509       gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);
6510       gtk_menu_shell_append (GTK_MENU_SHELL (text_view->popup_menu), menuitem);
6511       
6512       gtk_im_multicontext_append_menuitems (GTK_IM_MULTICONTEXT (text_view->im_context),
6513                                             GTK_MENU_SHELL (submenu));
6514
6515       menuitem = gtk_menu_item_new_with_mnemonic (_("_Insert Unicode control character"));
6516       gtk_widget_show (menuitem);
6517       gtk_widget_set_sensitive (menuitem, can_insert);
6518       
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_text_util_append_special_char_menuitems (GTK_MENU_SHELL (submenu),
6524                                                     unichar_chosen_func,
6525                                                     text_view);
6526       
6527       g_signal_emit (text_view,
6528                      signals[POPULATE_POPUP],
6529                      0,
6530                      text_view->popup_menu);
6531       
6532       if (info->button)
6533         gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6534                         NULL, NULL,
6535                         info->button, info->time);
6536       else
6537         {
6538           gtk_menu_popup (GTK_MENU (text_view->popup_menu), NULL, NULL,
6539                           popup_position_func, text_view,
6540                           0, gtk_get_current_event_time ());
6541           gtk_menu_shell_select_first (GTK_MENU_SHELL (text_view->popup_menu), FALSE);
6542         }
6543     }
6544
6545   g_object_unref (text_view);
6546   g_free (info);
6547 }
6548
6549 static void
6550 gtk_text_view_do_popup (GtkTextView    *text_view,
6551                         GdkEventButton *event)
6552 {
6553   PopupInfo *info = g_new (PopupInfo, 1);
6554
6555   /* should not need this, see http://bugzilla.gnome.org/show_bug.cgi?id=74620 */
6556   gtk_text_view_end_selection_drag (text_view, event);
6557   
6558   /* In order to know what entries we should make sensitive, we
6559    * ask for the current targets of the clipboard, and when
6560    * we get them, then we actually pop up the menu.
6561    */
6562   info->text_view = g_object_ref (text_view);
6563   
6564   if (event)
6565     {
6566       info->button = event->button;
6567       info->time = event->time;
6568     }
6569   else
6570     {
6571       info->button = 0;
6572       info->time = gtk_get_current_event_time ();
6573     }
6574
6575   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
6576                                                             GDK_SELECTION_CLIPBOARD),
6577                                   gdk_atom_intern ("TARGETS", FALSE),
6578                                   popup_targets_received,
6579                                   info);
6580 }
6581
6582 static gboolean
6583 gtk_text_view_popup_menu (GtkWidget *widget)
6584 {
6585   gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);  
6586   return TRUE;
6587 }
6588
6589 /* Child GdkWindows */
6590
6591
6592 static GtkTextWindow*
6593 text_window_new (GtkTextWindowType  type,
6594                  GtkWidget         *widget,
6595                  gint               width_request,
6596                  gint               height_request)
6597 {
6598   GtkTextWindow *win;
6599
6600   win = g_new (GtkTextWindow, 1);
6601
6602   win->type = type;
6603   win->widget = widget;
6604   win->window = NULL;
6605   win->bin_window = NULL;
6606   win->requisition.width = width_request;
6607   win->requisition.height = height_request;
6608   win->allocation.width = width_request;
6609   win->allocation.height = height_request;
6610   win->allocation.x = 0;
6611   win->allocation.y = 0;
6612
6613   return win;
6614 }
6615
6616 static void
6617 text_window_free (GtkTextWindow *win)
6618 {
6619   if (win->window)
6620     text_window_unrealize (win);
6621
6622   g_free (win);
6623 }
6624
6625 static void
6626 text_window_realize (GtkTextWindow *win,
6627                      GdkWindow     *parent)
6628 {
6629   GdkWindowAttr attributes;
6630   gint attributes_mask;
6631   GdkCursor *cursor;
6632
6633   attributes.window_type = GDK_WINDOW_CHILD;
6634   attributes.x = win->allocation.x;
6635   attributes.y = win->allocation.y;
6636   attributes.width = win->allocation.width;
6637   attributes.height = win->allocation.height;
6638   attributes.wclass = GDK_INPUT_OUTPUT;
6639   attributes.visual = gtk_widget_get_visual (win->widget);
6640   attributes.colormap = gtk_widget_get_colormap (win->widget);
6641   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
6642
6643   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
6644
6645   win->window = gdk_window_new (parent,
6646                                 &attributes,
6647                                 attributes_mask);
6648
6649   gdk_window_show (win->window);
6650   gdk_window_set_user_data (win->window, win->widget);
6651
6652   attributes.x = 0;
6653   attributes.y = 0;
6654   attributes.width = win->allocation.width;
6655   attributes.height = win->allocation.height;
6656   attributes.event_mask = (GDK_EXPOSURE_MASK            |
6657                            GDK_SCROLL_MASK              |
6658                            GDK_KEY_PRESS_MASK           |
6659                            GDK_BUTTON_PRESS_MASK        |
6660                            GDK_BUTTON_RELEASE_MASK      |
6661                            GDK_POINTER_MOTION_MASK      |
6662                            GDK_POINTER_MOTION_HINT_MASK |
6663                            gtk_widget_get_events (win->widget));
6664
6665   win->bin_window = gdk_window_new (win->window,
6666                                     &attributes,
6667                                     attributes_mask);
6668
6669   gdk_window_show (win->bin_window);
6670   gdk_window_set_user_data (win->bin_window, win->widget);
6671
6672   if (win->type == GTK_TEXT_WINDOW_TEXT)
6673     {
6674       /* I-beam cursor */
6675       cursor = gdk_cursor_new_for_display (gdk_drawable_get_display (parent),
6676                                            GDK_XTERM);
6677       gdk_window_set_cursor (win->bin_window, cursor);
6678       gdk_cursor_unref (cursor);
6679
6680       gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6681                                         win->window);
6682
6683
6684       gdk_window_set_background (win->bin_window,
6685                                  &win->widget->style->base[GTK_WIDGET_STATE (win->widget)]);
6686     }
6687   else
6688     {
6689       gdk_window_set_background (win->bin_window,
6690                                  &win->widget->style->bg[GTK_WIDGET_STATE (win->widget)]);
6691     }
6692
6693   g_object_set_qdata (G_OBJECT (win->window),
6694                       g_quark_from_static_string ("gtk-text-view-text-window"),
6695                       win);
6696
6697   g_object_set_qdata (G_OBJECT (win->bin_window),
6698                       g_quark_from_static_string ("gtk-text-view-text-window"),
6699                       win);
6700 }
6701
6702 static void
6703 text_window_unrealize (GtkTextWindow *win)
6704 {
6705   if (win->type == GTK_TEXT_WINDOW_TEXT)
6706     {
6707       gtk_im_context_set_client_window (GTK_TEXT_VIEW (win->widget)->im_context,
6708                                         NULL);
6709     }
6710
6711   gdk_window_set_user_data (win->window, NULL);
6712   gdk_window_set_user_data (win->bin_window, NULL);
6713   gdk_window_destroy (win->bin_window);
6714   gdk_window_destroy (win->window);
6715   win->window = NULL;
6716   win->bin_window = NULL;
6717 }
6718
6719 static void
6720 text_window_size_allocate (GtkTextWindow *win,
6721                            GdkRectangle  *rect)
6722 {
6723   win->allocation = *rect;
6724
6725   if (win->window)
6726     {
6727       gdk_window_move_resize (win->window,
6728                               rect->x, rect->y,
6729                               rect->width, rect->height);
6730
6731       gdk_window_resize (win->bin_window,
6732                          rect->width, rect->height);
6733     }
6734 }
6735
6736 static void
6737 text_window_scroll        (GtkTextWindow *win,
6738                            gint           dx,
6739                            gint           dy)
6740 {
6741   if (dx != 0 || dy != 0)
6742     {
6743       gdk_window_scroll (win->bin_window, dx, dy);
6744     }
6745 }
6746
6747 static void
6748 text_window_invalidate_rect (GtkTextWindow *win,
6749                              GdkRectangle  *rect)
6750 {
6751   GdkRectangle window_rect;
6752
6753   gtk_text_view_buffer_to_window_coords (GTK_TEXT_VIEW (win->widget),
6754                                          win->type,
6755                                          rect->x,
6756                                          rect->y,
6757                                          &window_rect.x,
6758                                          &window_rect.y);
6759
6760   window_rect.width = rect->width;
6761   window_rect.height = rect->height;
6762   
6763   /* Adjust the rect as appropriate */
6764   
6765   switch (win->type)
6766     {
6767     case GTK_TEXT_WINDOW_TEXT:
6768       break;
6769
6770     case GTK_TEXT_WINDOW_LEFT:
6771     case GTK_TEXT_WINDOW_RIGHT:
6772       window_rect.x = 0;
6773       window_rect.width = win->allocation.width;
6774       break;
6775
6776     case GTK_TEXT_WINDOW_TOP:
6777     case GTK_TEXT_WINDOW_BOTTOM:
6778       window_rect.y = 0;
6779       window_rect.height = win->allocation.height;
6780       break;
6781
6782     default:
6783       g_warning ("%s: bug!", G_STRLOC);
6784       return;
6785       break;
6786     }
6787           
6788   gdk_window_invalidate_rect (win->bin_window, &window_rect, FALSE);
6789
6790 #if 0
6791   {
6792     GdkColor color = { 0, 65535, 0, 0 };
6793     GdkGC *gc = gdk_gc_new (win->bin_window);
6794     gdk_gc_set_rgb_fg_color (gc, &color);
6795     gdk_draw_rectangle (win->bin_window,
6796                         gc, TRUE, window_rect.x, window_rect.y,
6797                         window_rect.width, window_rect.height);
6798     g_object_unref (gc);
6799   }
6800 #endif
6801 }
6802
6803 static gint
6804 text_window_get_width (GtkTextWindow *win)
6805 {
6806   return win->allocation.width;
6807 }
6808
6809 static gint
6810 text_window_get_height (GtkTextWindow *win)
6811 {
6812   return win->allocation.height;
6813 }
6814
6815 /* Windows */
6816
6817
6818 /**
6819  * gtk_text_view_get_window:
6820  * @text_view: a #GtkTextView
6821  * @win: window to get
6822  *
6823  * Retrieves the #GdkWindow corresponding to an area of the text view;
6824  * possible windows include the overall widget window, child windows
6825  * on the left, right, top, bottom, and the window that displays the
6826  * text buffer. Windows are %NULL and nonexistent if their width or
6827  * height is 0, and are nonexistent before the widget has been
6828  * realized.
6829  *
6830  * Return value: a #GdkWindow, or %NULL
6831  **/
6832 GdkWindow*
6833 gtk_text_view_get_window (GtkTextView *text_view,
6834                           GtkTextWindowType win)
6835 {
6836   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), NULL);
6837
6838   switch (win)
6839     {
6840     case GTK_TEXT_WINDOW_WIDGET:
6841       return GTK_WIDGET (text_view)->window;
6842       break;
6843
6844     case GTK_TEXT_WINDOW_TEXT:
6845       return text_view->text_window->bin_window;
6846       break;
6847
6848     case GTK_TEXT_WINDOW_LEFT:
6849       if (text_view->left_window)
6850         return text_view->left_window->bin_window;
6851       else
6852         return NULL;
6853       break;
6854
6855     case GTK_TEXT_WINDOW_RIGHT:
6856       if (text_view->right_window)
6857         return text_view->right_window->bin_window;
6858       else
6859         return NULL;
6860       break;
6861
6862     case GTK_TEXT_WINDOW_TOP:
6863       if (text_view->top_window)
6864         return text_view->top_window->bin_window;
6865       else
6866         return NULL;
6867       break;
6868
6869     case GTK_TEXT_WINDOW_BOTTOM:
6870       if (text_view->bottom_window)
6871         return text_view->bottom_window->bin_window;
6872       else
6873         return NULL;
6874       break;
6875
6876     case GTK_TEXT_WINDOW_PRIVATE:
6877       g_warning ("%s: You can't get GTK_TEXT_WINDOW_PRIVATE, it has \"PRIVATE\" in the name because it is private.", G_GNUC_FUNCTION);
6878       return NULL;
6879       break;
6880     }
6881
6882   g_warning ("%s: Unknown GtkTextWindowType", G_GNUC_FUNCTION);
6883   return NULL;
6884 }
6885
6886 /**
6887  * gtk_text_view_get_window_type:
6888  * @text_view: a #GtkTextView
6889  * @window: a window type
6890  *
6891  * Usually used to find out which window an event corresponds to.
6892  * If you connect to an event signal on @text_view, this function
6893  * should be called on <literal>event-&gt;window</literal> to
6894  * see which window it was.
6895  *
6896  * Return value: the window type.
6897  **/
6898 GtkTextWindowType
6899 gtk_text_view_get_window_type (GtkTextView *text_view,
6900                                GdkWindow   *window)
6901 {
6902   GtkTextWindow *win;
6903
6904   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
6905   g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
6906
6907   if (window == GTK_WIDGET (text_view)->window)
6908     return GTK_TEXT_WINDOW_WIDGET;
6909
6910   win = g_object_get_qdata (G_OBJECT (window),
6911                             g_quark_try_string ("gtk-text-view-text-window"));
6912
6913   if (win)
6914     return win->type;
6915   else
6916     {
6917       return GTK_TEXT_WINDOW_PRIVATE;
6918     }
6919 }
6920
6921 static void
6922 buffer_to_widget (GtkTextView      *text_view,
6923                   gint              buffer_x,
6924                   gint              buffer_y,
6925                   gint             *window_x,
6926                   gint             *window_y)
6927 {  
6928   if (window_x)
6929     {
6930       *window_x = buffer_x - text_view->xoffset;
6931       *window_x += text_view->text_window->allocation.x;
6932     }
6933
6934   if (window_y)
6935     {
6936       *window_y = buffer_y - text_view->yoffset;
6937       *window_y += text_view->text_window->allocation.y;
6938     }
6939 }
6940
6941 static void
6942 widget_to_text_window (GtkTextWindow *win,
6943                        gint           widget_x,
6944                        gint           widget_y,
6945                        gint          *window_x,
6946                        gint          *window_y)
6947 {
6948   if (window_x)
6949     *window_x = widget_x - win->allocation.x;
6950
6951   if (window_y)
6952     *window_y = widget_y - win->allocation.y;
6953 }
6954
6955 static void
6956 buffer_to_text_window (GtkTextView   *text_view,
6957                        GtkTextWindow *win,
6958                        gint           buffer_x,
6959                        gint           buffer_y,
6960                        gint          *window_x,
6961                        gint          *window_y)
6962 {
6963   if (win == NULL)
6964     {
6965       g_warning ("Attempt to convert text buffer coordinates to coordinates "
6966                  "for a nonexistent or private child window of GtkTextView");
6967       return;
6968     }
6969
6970   buffer_to_widget (text_view,
6971                     buffer_x, buffer_y,
6972                     window_x, window_y);
6973
6974   widget_to_text_window (win,
6975                          window_x ? *window_x : 0,
6976                          window_y ? *window_y : 0,
6977                          window_x,
6978                          window_y);
6979 }
6980
6981 /**
6982  * gtk_text_view_buffer_to_window_coords:
6983  * @text_view: a #GtkTextView
6984  * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
6985  * @buffer_x: buffer x coordinate
6986  * @buffer_y: buffer y coordinate
6987  * @window_x: window x coordinate return location
6988  * @window_y: window y coordinate return location
6989  *
6990  * Converts coordinate (@buffer_x, @buffer_y) to coordinates for the window
6991  * @win, and stores the result in (@window_x, @window_y). 
6992  *
6993  * Note that you can't convert coordinates for a nonexisting window (see 
6994  * gtk_text_view_set_border_window_size()).
6995  **/
6996 void
6997 gtk_text_view_buffer_to_window_coords (GtkTextView      *text_view,
6998                                        GtkTextWindowType win,
6999                                        gint              buffer_x,
7000                                        gint              buffer_y,
7001                                        gint             *window_x,
7002                                        gint             *window_y)
7003 {
7004   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7005
7006   switch (win)
7007     {
7008     case GTK_TEXT_WINDOW_WIDGET:
7009       buffer_to_widget (text_view,
7010                         buffer_x, buffer_y,
7011                         window_x, window_y);
7012       break;
7013
7014     case GTK_TEXT_WINDOW_TEXT:
7015       if (window_x)
7016         *window_x = buffer_x - text_view->xoffset;
7017       if (window_y)
7018         *window_y = buffer_y - text_view->yoffset;
7019       break;
7020
7021     case GTK_TEXT_WINDOW_LEFT:
7022       buffer_to_text_window (text_view,
7023                              text_view->left_window,
7024                              buffer_x, buffer_y,
7025                              window_x, window_y);
7026       break;
7027
7028     case GTK_TEXT_WINDOW_RIGHT:
7029       buffer_to_text_window (text_view,
7030                              text_view->right_window,
7031                              buffer_x, buffer_y,
7032                              window_x, window_y);
7033       break;
7034
7035     case GTK_TEXT_WINDOW_TOP:
7036       buffer_to_text_window (text_view,
7037                              text_view->top_window,
7038                              buffer_x, buffer_y,
7039                              window_x, window_y);
7040       break;
7041
7042     case GTK_TEXT_WINDOW_BOTTOM:
7043       buffer_to_text_window (text_view,
7044                              text_view->bottom_window,
7045                              buffer_x, buffer_y,
7046                              window_x, window_y);
7047       break;
7048
7049     case GTK_TEXT_WINDOW_PRIVATE:
7050       g_warning ("%s: can't get coords for private windows", G_STRLOC);
7051       break;
7052
7053     default:
7054       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7055       break;
7056     }
7057 }
7058
7059 static void
7060 widget_to_buffer (GtkTextView *text_view,
7061                   gint         widget_x,
7062                   gint         widget_y,
7063                   gint        *buffer_x,
7064                   gint        *buffer_y)
7065 {  
7066   if (buffer_x)
7067     {
7068       *buffer_x = widget_x + text_view->xoffset;
7069       *buffer_x -= text_view->text_window->allocation.x;
7070     }
7071
7072   if (buffer_y)
7073     {
7074       *buffer_y = widget_y + text_view->yoffset;
7075       *buffer_y -= text_view->text_window->allocation.y;
7076     }
7077 }
7078
7079 static void
7080 text_window_to_widget (GtkTextWindow *win,
7081                        gint           window_x,
7082                        gint           window_y,
7083                        gint          *widget_x,
7084                        gint          *widget_y)
7085 {
7086   if (widget_x)
7087     *widget_x = window_x + win->allocation.x;
7088
7089   if (widget_y)
7090     *widget_y = window_y + win->allocation.y;
7091 }
7092
7093 static void
7094 text_window_to_buffer (GtkTextView   *text_view,
7095                        GtkTextWindow *win,
7096                        gint           window_x,
7097                        gint           window_y,
7098                        gint          *buffer_x,
7099                        gint          *buffer_y)
7100 {
7101   if (win == NULL)
7102     {
7103       g_warning ("Attempt to convert GtkTextView buffer coordinates into "
7104                  "coordinates for a nonexistent child window.");
7105       return;
7106     }
7107
7108   text_window_to_widget (win,
7109                          window_x,
7110                          window_y,
7111                          buffer_x,
7112                          buffer_y);
7113
7114   widget_to_buffer (text_view,
7115                     buffer_x ? *buffer_x : 0,
7116                     buffer_y ? *buffer_y : 0,
7117                     buffer_x,
7118                     buffer_y);
7119 }
7120
7121 /**
7122  * gtk_text_view_window_to_buffer_coords:
7123  * @text_view: a #GtkTextView
7124  * @win: a #GtkTextWindowType except #GTK_TEXT_WINDOW_PRIVATE
7125  * @window_x: window x coordinate
7126  * @window_y: window y coordinate
7127  * @buffer_x: buffer x coordinate return location
7128  * @buffer_y: buffer y coordinate return location
7129  *
7130  * Converts coordinates on the window identified by @win to buffer
7131  * coordinates, storing the result in (@buffer_x,@buffer_y).
7132  *
7133  * Note that you can't convert coordinates for a nonexisting window (see 
7134  * gtk_text_view_set_border_window_size()).
7135  **/
7136 void
7137 gtk_text_view_window_to_buffer_coords (GtkTextView      *text_view,
7138                                        GtkTextWindowType win,
7139                                        gint              window_x,
7140                                        gint              window_y,
7141                                        gint             *buffer_x,
7142                                        gint             *buffer_y)
7143 {
7144   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7145
7146   switch (win)
7147     {
7148     case GTK_TEXT_WINDOW_WIDGET:
7149       widget_to_buffer (text_view,
7150                         window_x, window_y,
7151                         buffer_x, buffer_y);
7152       break;
7153
7154     case GTK_TEXT_WINDOW_TEXT:
7155       if (buffer_x)
7156         *buffer_x = window_x + text_view->xoffset;
7157       if (buffer_y)
7158         *buffer_y = window_y + text_view->yoffset;
7159       break;
7160
7161     case GTK_TEXT_WINDOW_LEFT:
7162       text_window_to_buffer (text_view,
7163                              text_view->left_window,
7164                              window_x, window_y,
7165                              buffer_x, buffer_y);
7166       break;
7167
7168     case GTK_TEXT_WINDOW_RIGHT:
7169       text_window_to_buffer (text_view,
7170                              text_view->right_window,
7171                              window_x, window_y,
7172                              buffer_x, buffer_y);
7173       break;
7174
7175     case GTK_TEXT_WINDOW_TOP:
7176       text_window_to_buffer (text_view,
7177                              text_view->top_window,
7178                              window_x, window_y,
7179                              buffer_x, buffer_y);
7180       break;
7181
7182     case GTK_TEXT_WINDOW_BOTTOM:
7183       text_window_to_buffer (text_view,
7184                              text_view->bottom_window,
7185                              window_x, window_y,
7186                              buffer_x, buffer_y);
7187       break;
7188
7189     case GTK_TEXT_WINDOW_PRIVATE:
7190       g_warning ("%s: can't get coords for private windows", G_STRLOC);
7191       break;
7192
7193     default:
7194       g_warning ("%s: Unknown GtkTextWindowType", G_STRLOC);
7195       break;
7196     }
7197 }
7198
7199 static void
7200 set_window_width (GtkTextView      *text_view,
7201                   gint              width,
7202                   GtkTextWindowType type,
7203                   GtkTextWindow   **winp)
7204 {
7205   if (width == 0)
7206     {
7207       if (*winp)
7208         {
7209           text_window_free (*winp);
7210           *winp = NULL;
7211           gtk_widget_queue_resize (GTK_WIDGET (text_view));
7212         }
7213     }
7214   else
7215     {
7216       if (*winp == NULL)
7217         {
7218           *winp = text_window_new (type,
7219                                    GTK_WIDGET (text_view),
7220                                    width, 0);
7221           /* if the widget is already realized we need to realize the child manually */
7222           if (GTK_WIDGET_REALIZED (text_view))
7223             text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7224         }
7225       else
7226         {
7227           if ((*winp)->requisition.width == width)
7228             return;
7229
7230           (*winp)->requisition.width = width;
7231         }
7232
7233       gtk_widget_queue_resize (GTK_WIDGET (text_view));
7234     }
7235 }
7236
7237
7238 static void
7239 set_window_height (GtkTextView      *text_view,
7240                    gint              height,
7241                    GtkTextWindowType type,
7242                    GtkTextWindow   **winp)
7243 {
7244   if (height == 0)
7245     {
7246       if (*winp)
7247         {
7248           text_window_free (*winp);
7249           *winp = NULL;
7250           gtk_widget_queue_resize (GTK_WIDGET (text_view));
7251         }
7252     }
7253   else
7254     {
7255       if (*winp == NULL)
7256         {
7257           *winp = text_window_new (type,
7258                                    GTK_WIDGET (text_view),
7259                                    0, height);
7260
7261           /* if the widget is already realized we need to realize the child manually */
7262           if (GTK_WIDGET_REALIZED (text_view))
7263             text_window_realize (*winp, GTK_WIDGET (text_view)->window);
7264         }
7265       else
7266         {
7267           if ((*winp)->requisition.height == height)
7268             return;
7269
7270           (*winp)->requisition.height = height;
7271         }
7272
7273       gtk_widget_queue_resize (GTK_WIDGET (text_view));
7274     }
7275 }
7276
7277 /**
7278  * gtk_text_view_set_border_window_size:
7279  * @text_view: a #GtkTextView
7280  * @type: window to affect
7281  * @size: width or height of the window
7282  *
7283  * Sets the width of %GTK_TEXT_WINDOW_LEFT or %GTK_TEXT_WINDOW_RIGHT,
7284  * or the height of %GTK_TEXT_WINDOW_TOP or %GTK_TEXT_WINDOW_BOTTOM.
7285  * Automatically destroys the corresponding window if the size is set
7286  * to 0, and creates the window if the size is set to non-zero.  This
7287  * function can only be used for the "border windows," it doesn't work
7288  * with #GTK_TEXT_WINDOW_WIDGET, #GTK_TEXT_WINDOW_TEXT, or
7289  * #GTK_TEXT_WINDOW_PRIVATE.
7290  **/
7291 void
7292 gtk_text_view_set_border_window_size (GtkTextView      *text_view,
7293                                       GtkTextWindowType type,
7294                                       gint              size)
7295
7296 {
7297   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7298   g_return_if_fail (size >= 0);
7299
7300   switch (type)
7301     {
7302     case GTK_TEXT_WINDOW_LEFT:
7303       set_window_width (text_view, size, GTK_TEXT_WINDOW_LEFT,
7304                         &text_view->left_window);
7305       break;
7306
7307     case GTK_TEXT_WINDOW_RIGHT:
7308       set_window_width (text_view, size, GTK_TEXT_WINDOW_RIGHT,
7309                         &text_view->right_window);
7310       break;
7311
7312     case GTK_TEXT_WINDOW_TOP:
7313       set_window_height (text_view, size, GTK_TEXT_WINDOW_TOP,
7314                          &text_view->top_window);
7315       break;
7316
7317     case GTK_TEXT_WINDOW_BOTTOM:
7318       set_window_height (text_view, size, GTK_TEXT_WINDOW_BOTTOM,
7319                          &text_view->bottom_window);
7320       break;
7321
7322     default:
7323       g_warning ("Can only set size of left/right/top/bottom border windows with gtk_text_view_set_border_window_size()");
7324       break;
7325     }
7326 }
7327
7328 /**
7329  * gtk_text_view_get_border_window_size:
7330  * @text_view: a #GtkTextView
7331  * @type: window to return size from
7332  *
7333  * Gets the width of the specified border window. See
7334  * gtk_text_view_set_border_window_size().
7335  *
7336  * Return value: width of window
7337  **/
7338 gint
7339 gtk_text_view_get_border_window_size (GtkTextView       *text_view,
7340                                       GtkTextWindowType  type)
7341 {
7342   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), 0);
7343   
7344   switch (type)
7345     {
7346     case GTK_TEXT_WINDOW_LEFT:
7347       if (text_view->left_window)
7348         return text_view->left_window->requisition.width;
7349       
7350     case GTK_TEXT_WINDOW_RIGHT:
7351       if (text_view->right_window)
7352         return text_view->right_window->requisition.width;
7353       
7354     case GTK_TEXT_WINDOW_TOP:
7355       if (text_view->top_window)
7356         return text_view->top_window->requisition.height;
7357
7358     case GTK_TEXT_WINDOW_BOTTOM:
7359       if (text_view->bottom_window)
7360         return text_view->bottom_window->requisition.height;
7361       
7362     default:
7363       g_warning ("Can only get size of left/right/top/bottom border windows with gtk_text_view_get_border_window_size()");
7364       break;
7365     }
7366
7367   return 0;
7368 }
7369
7370 /*
7371  * Child widgets
7372  */
7373
7374 static GtkTextViewChild*
7375 text_view_child_new_anchored (GtkWidget          *child,
7376                               GtkTextChildAnchor *anchor,
7377                               GtkTextLayout      *layout)
7378 {
7379   GtkTextViewChild *vc;
7380
7381   vc = g_new (GtkTextViewChild, 1);
7382
7383   vc->type = GTK_TEXT_WINDOW_PRIVATE;
7384   vc->widget = child;
7385   vc->anchor = anchor;
7386
7387   vc->from_top_of_line = 0;
7388   vc->from_left_of_buffer = 0;
7389   
7390   g_object_ref (vc->widget);
7391   g_object_ref (vc->anchor);
7392
7393   g_object_set_data (G_OBJECT (child),
7394                      "gtk-text-view-child",
7395                      vc);
7396
7397   gtk_text_child_anchor_register_child (anchor, child, layout);
7398   
7399   return vc;
7400 }
7401
7402 static GtkTextViewChild*
7403 text_view_child_new_window (GtkWidget          *child,
7404                             GtkTextWindowType   type,
7405                             gint                x,
7406                             gint                y)
7407 {
7408   GtkTextViewChild *vc;
7409
7410   vc = g_new (GtkTextViewChild, 1);
7411
7412   vc->widget = child;
7413   vc->anchor = NULL;
7414
7415   vc->from_top_of_line = 0;
7416   vc->from_left_of_buffer = 0;
7417  
7418   g_object_ref (vc->widget);
7419
7420   vc->type = type;
7421   vc->x = x;
7422   vc->y = y;
7423
7424   g_object_set_data (G_OBJECT (child),
7425                      "gtk-text-view-child",
7426                      vc);
7427   
7428   return vc;
7429 }
7430
7431 static void
7432 text_view_child_free (GtkTextViewChild *child)
7433 {
7434   g_object_set_data (G_OBJECT (child->widget),
7435                      "gtk-text-view-child", NULL);
7436
7437   if (child->anchor)
7438     {
7439       gtk_text_child_anchor_unregister_child (child->anchor,
7440                                               child->widget);
7441       g_object_unref (child->anchor);
7442     }
7443
7444   g_object_unref (child->widget);
7445
7446   g_free (child);
7447 }
7448
7449 static void
7450 text_view_child_set_parent_window (GtkTextView      *text_view,
7451                                    GtkTextViewChild *vc)
7452 {
7453   if (vc->anchor)
7454     gtk_widget_set_parent_window (vc->widget,
7455                                   text_view->text_window->bin_window);
7456   else
7457     {
7458       GdkWindow *window;
7459       window = gtk_text_view_get_window (text_view,
7460                                          vc->type);
7461       gtk_widget_set_parent_window (vc->widget, window);
7462     }
7463 }
7464
7465 static void
7466 add_child (GtkTextView      *text_view,
7467            GtkTextViewChild *vc)
7468 {
7469   text_view->children = g_slist_prepend (text_view->children,
7470                                          vc);
7471
7472   if (GTK_WIDGET_REALIZED (text_view))
7473     text_view_child_set_parent_window (text_view, vc);
7474   
7475   gtk_widget_set_parent (vc->widget, GTK_WIDGET (text_view));
7476 }
7477
7478 /**
7479  * gtk_text_view_add_child_at_anchor:
7480  * @text_view: a #GtkTextView
7481  * @child: a #GtkWidget
7482  * @anchor: a #GtkTextChildAnchor in the #GtkTextBuffer for @text_view
7483  * 
7484  * Adds a child widget in the text buffer, at the given @anchor.
7485  * 
7486  **/
7487 void
7488 gtk_text_view_add_child_at_anchor (GtkTextView          *text_view,
7489                                    GtkWidget            *child,
7490                                    GtkTextChildAnchor   *anchor)
7491 {
7492   GtkTextViewChild *vc;
7493
7494   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7495   g_return_if_fail (GTK_IS_WIDGET (child));
7496   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
7497   g_return_if_fail (child->parent == NULL);
7498
7499   gtk_text_view_ensure_layout (text_view);
7500
7501   vc = text_view_child_new_anchored (child, anchor,
7502                                      text_view->layout);
7503
7504   add_child (text_view, vc);
7505
7506   g_assert (vc->widget == child);
7507   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7508 }
7509
7510 /**
7511  * gtk_text_view_add_child_in_window:
7512  * @text_view: a #GtkTextView
7513  * @child: a #GtkWidget
7514  * @which_window: which window the child should appear in
7515  * @xpos: X position of child in window coordinates
7516  * @ypos: Y position of child in window coordinates
7517  *
7518  * Adds a child at fixed coordinates in one of the text widget's
7519  * windows.  The window must have nonzero size (see
7520  * gtk_text_view_set_border_window_size()).  Note that the child
7521  * coordinates are given relative to the #GdkWindow in question, and
7522  * that these coordinates have no sane relationship to scrolling. When
7523  * placing a child in #GTK_TEXT_WINDOW_WIDGET, scrolling is
7524  * irrelevant, the child floats above all scrollable areas. But when
7525  * placing a child in one of the scrollable windows (border windows or
7526  * text window), you'll need to compute the child's correct position
7527  * in buffer coordinates any time scrolling occurs or buffer changes
7528  * occur, and then call gtk_text_view_move_child() to update the
7529  * child's position. Unfortunately there's no good way to detect that
7530  * scrolling has occurred, using the current API; a possible hack
7531  * would be to update all child positions when the scroll adjustments
7532  * change or the text buffer changes. See bug 64518 on
7533  * bugzilla.gnome.org for status of fixing this issue.
7534  *
7535  **/
7536 void
7537 gtk_text_view_add_child_in_window (GtkTextView          *text_view,
7538                                    GtkWidget            *child,
7539                                    GtkTextWindowType     which_window,
7540                                    gint                  xpos,
7541                                    gint                  ypos)
7542 {
7543   GtkTextViewChild *vc;
7544
7545   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7546   g_return_if_fail (GTK_IS_WIDGET (child));
7547   g_return_if_fail (child->parent == NULL);
7548
7549   vc = text_view_child_new_window (child, which_window,
7550                                    xpos, ypos);
7551
7552   add_child (text_view, vc);
7553
7554   g_assert (vc->widget == child);
7555   g_assert (gtk_widget_get_parent (child) == GTK_WIDGET (text_view));
7556 }
7557
7558 /**
7559  * gtk_text_view_move_child:
7560  * @text_view: a #GtkTextView
7561  * @child: child widget already added to the text view
7562  * @xpos: new X position in window coordinates
7563  * @ypos: new Y position in window coordinates
7564  *
7565  * Updates the position of a child, as for gtk_text_view_add_child_in_window().
7566  * 
7567  **/
7568 void
7569 gtk_text_view_move_child          (GtkTextView          *text_view,
7570                                    GtkWidget            *child,
7571                                    gint                  xpos,
7572                                    gint                  ypos)
7573 {
7574   GtkTextViewChild *vc;
7575
7576   g_return_if_fail (GTK_IS_TEXT_VIEW (text_view));
7577   g_return_if_fail (GTK_IS_WIDGET (child));
7578   g_return_if_fail (child->parent == (GtkWidget*) text_view);
7579
7580   vc = g_object_get_data (G_OBJECT (child),
7581                           "gtk-text-view-child");
7582
7583   g_assert (vc != NULL);
7584
7585   if (vc->x == xpos &&
7586       vc->y == ypos)
7587     return;
7588   
7589   vc->x = xpos;
7590   vc->y = ypos;
7591
7592   if (GTK_WIDGET_VISIBLE (child) && GTK_WIDGET_VISIBLE (text_view))
7593     gtk_widget_queue_resize (child);
7594 }
7595
7596
7597 /* Iterator operations */
7598
7599 /**
7600  * gtk_text_view_forward_display_line:
7601  * @text_view: a #GtkTextView
7602  * @iter: a #GtkTextIter
7603  * 
7604  * Moves the given @iter forward by one display (wrapped) line.  A
7605  * display line is different from a paragraph. Paragraphs are
7606  * separated by newlines or other paragraph separator characters.
7607  * Display lines are created by line-wrapping a paragraph.  If
7608  * wrapping is turned off, display lines and paragraphs will be the
7609  * same. Display lines are divided differently for each view, since
7610  * they depend on the view's width; paragraphs are the same in all
7611  * views, since they depend on the contents of the #GtkTextBuffer.
7612  * 
7613  * Return value: %TRUE if @iter was moved and is not on the end iterator
7614  **/
7615 gboolean
7616 gtk_text_view_forward_display_line (GtkTextView *text_view,
7617                                     GtkTextIter *iter)
7618 {
7619   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7620   g_return_val_if_fail (iter != NULL, FALSE);
7621
7622   gtk_text_view_ensure_layout (text_view);
7623
7624   return gtk_text_layout_move_iter_to_next_line (text_view->layout, iter);
7625 }
7626
7627 /**
7628  * gtk_text_view_backward_display_line:
7629  * @text_view: a #GtkTextView
7630  * @iter: a #GtkTextIter
7631  * 
7632  * Moves the given @iter backward by one display (wrapped) line.  A
7633  * display line is different from a paragraph. Paragraphs are
7634  * separated by newlines or other paragraph separator characters.
7635  * Display lines are created by line-wrapping a paragraph.  If
7636  * wrapping is turned off, display lines and paragraphs will be the
7637  * same. Display lines are divided differently for each view, since
7638  * they depend on the view's width; paragraphs are the same in all
7639  * views, since they depend on the contents of the #GtkTextBuffer.
7640  * 
7641  * Return value: %TRUE if @iter was moved and is not on the end iterator
7642  **/
7643 gboolean
7644 gtk_text_view_backward_display_line (GtkTextView *text_view,
7645                                      GtkTextIter *iter)
7646 {
7647   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7648   g_return_val_if_fail (iter != NULL, FALSE);
7649
7650   gtk_text_view_ensure_layout (text_view);
7651
7652   return gtk_text_layout_move_iter_to_previous_line (text_view->layout, iter);
7653 }
7654
7655 /**
7656  * gtk_text_view_forward_display_line_end:
7657  * @text_view: a #GtkTextView
7658  * @iter: a #GtkTextIter
7659  * 
7660  * Moves the given @iter forward to the next display line end.  A
7661  * display line is different from a paragraph. Paragraphs are
7662  * separated by newlines or other paragraph separator characters.
7663  * Display lines are created by line-wrapping a paragraph.  If
7664  * wrapping is turned off, display lines and paragraphs will be the
7665  * same. Display lines are divided differently for each view, since
7666  * they depend on the view's width; paragraphs are the same in all
7667  * views, since they depend on the contents of the #GtkTextBuffer.
7668  * 
7669  * Return value: %TRUE if @iter was moved and is not on the end iterator
7670  **/
7671 gboolean
7672 gtk_text_view_forward_display_line_end (GtkTextView *text_view,
7673                                         GtkTextIter *iter)
7674 {
7675   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7676   g_return_val_if_fail (iter != NULL, FALSE);
7677
7678   gtk_text_view_ensure_layout (text_view);
7679
7680   return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, 1);
7681 }
7682
7683 /**
7684  * gtk_text_view_backward_display_line_start:
7685  * @text_view: a #GtkTextView
7686  * @iter: a #GtkTextIter
7687  * 
7688  * Moves the given @iter backward to the next display line start.  A
7689  * display line is different from a paragraph. Paragraphs are
7690  * separated by newlines or other paragraph separator characters.
7691  * Display lines are created by line-wrapping a paragraph.  If
7692  * wrapping is turned off, display lines and paragraphs will be the
7693  * same. Display lines are divided differently for each view, since
7694  * they depend on the view's width; paragraphs are the same in all
7695  * views, since they depend on the contents of the #GtkTextBuffer.
7696  * 
7697  * Return value: %TRUE if @iter was moved and is not on the end iterator
7698  **/
7699 gboolean
7700 gtk_text_view_backward_display_line_start (GtkTextView *text_view,
7701                                            GtkTextIter *iter)
7702 {
7703   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7704   g_return_val_if_fail (iter != NULL, FALSE);
7705
7706   gtk_text_view_ensure_layout (text_view);
7707
7708   return gtk_text_layout_move_iter_to_line_end (text_view->layout, iter, -1);
7709 }
7710
7711 /**
7712  * gtk_text_view_starts_display_line:
7713  * @text_view: a #GtkTextView
7714  * @iter: a #GtkTextIter
7715  * 
7716  * Determines whether @iter is at the start of a display line.
7717  * See gtk_text_view_forward_display_line() for an explanation of
7718  * display lines vs. paragraphs.
7719  * 
7720  * Return value: %TRUE if @iter begins a wrapped line
7721  **/
7722 gboolean
7723 gtk_text_view_starts_display_line (GtkTextView       *text_view,
7724                                    const GtkTextIter *iter)
7725 {
7726   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7727   g_return_val_if_fail (iter != NULL, FALSE);
7728
7729   gtk_text_view_ensure_layout (text_view);
7730
7731   return gtk_text_layout_iter_starts_line (text_view->layout, iter);
7732 }
7733
7734 /**
7735  * gtk_text_view_move_visually:
7736  * @text_view: a #GtkTextView
7737  * @iter: a #GtkTextIter
7738  * @count:   number of characters to move (negative moves left, positive moves right)
7739  *
7740  * Move the iterator a given number of characters visually, treating
7741  * it as the strong cursor position. If @count is positive, then the
7742  * new strong cursor position will be @count positions to the right of
7743  * the old cursor position. If @count is negative then the new strong
7744  * cursor position will be @count positions to the left of the old
7745  * cursor position.
7746  *
7747  * In the presence of bidirection text, the correspondence
7748  * between logical and visual order will depend on the direction
7749  * of the current run, and there may be jumps when the cursor
7750  * is moved off of the end of a run.
7751  * 
7752  * Return value: %TRUE if @iter moved and is not on the end iterator
7753  **/
7754 gboolean
7755 gtk_text_view_move_visually (GtkTextView *text_view,
7756                              GtkTextIter *iter,
7757                              gint         count)
7758 {
7759   g_return_val_if_fail (GTK_IS_TEXT_VIEW (text_view), FALSE);
7760   g_return_val_if_fail (iter != NULL, FALSE);
7761
7762   gtk_text_view_ensure_layout (text_view);
7763
7764   return gtk_text_layout_move_iter_visually (text_view->layout, iter, count);
7765 }