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