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