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