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