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