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