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