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