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