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