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