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