]> Pileus Git - ~andy/gtk/blob - gtk/gtktext.c
Replace a lot of idle and timeout calls by the new gdk_threads api.
[~andy/gtk] / gtk / gtktext.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 #undef GDK_DISABLE_DEPRECATED
28 #undef GTK_DISABLE_DEPRECATED
29
30 #include <config.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include "gdk/gdkkeysyms.h"
34 #include "gdk/gdki18n.h"
35 #include "gtkmain.h"
36 #include "gtkmarshalers.h"
37 #include "gtkselection.h"
38 #include "gtksignal.h"
39 #include "gtkstyle.h"
40 #include "gtkobject.h"
41 #define GTK_ENABLE_BROKEN
42 #include "gtktext.h"
43 #include "line-wrap.xbm"
44 #include "line-arrow.xbm"
45 #include "gtkprivate.h"
46 #include "gtkintl.h"
47 #include "gtkalias.h"
48
49
50 #define INITIAL_BUFFER_SIZE      1024
51 #define INITIAL_LINE_CACHE_SIZE  256
52 #define MIN_GAP_SIZE             256
53 #define LINE_DELIM               '\n'
54 #define MIN_TEXT_WIDTH_LINES     20
55 #define MIN_TEXT_HEIGHT_LINES    10
56 #define TEXT_BORDER_ROOM         1
57 #define LINE_WRAP_ROOM           8           /* The bitmaps are 6 wide. */
58 #define DEFAULT_TAB_STOP_WIDTH   4
59 #define SCROLL_PIXELS            5
60 #define KEY_SCROLL_PIXELS        10
61 #define SCROLL_TIME              100
62 #define FREEZE_LENGTH            1024        
63 /* Freeze text when inserting or deleting more than this many characters */
64
65 #define SET_PROPERTY_MARK(m, p, o)  do {                   \
66                                       (m)->property = (p); \
67                                       (m)->offset = (o);   \
68                                     } while (0)
69 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
70 #define MARK_NEXT_PROPERTY(mark)    ((TextProperty*)(mark)->property->next->data)
71 #define MARK_PREV_PROPERTY(mark)    ((TextProperty*)((mark)->property->prev ?     \
72                                                      (mark)->property->prev->data \
73                                                      : NULL))
74 #define MARK_PREV_LIST_PTR(mark)    ((mark)->property->prev)
75 #define MARK_LIST_PTR(mark)         ((mark)->property)
76 #define MARK_NEXT_LIST_PTR(mark)    ((mark)->property->next)
77 #define MARK_OFFSET(mark)           ((mark)->offset)
78 #define MARK_PROPERTY_LENGTH(mark)  (MARK_CURRENT_PROPERTY(mark)->length)
79
80
81 #define MARK_CURRENT_FONT(text, mark) \
82   ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
83          MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
84          gtk_style_get_font (GTK_WIDGET (text)->style))
85 #define MARK_CURRENT_FORE(text, mark) \
86   ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
87          &MARK_CURRENT_PROPERTY(mark)->fore_color : \
88          &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
89 #define MARK_CURRENT_BACK(text, mark) \
90   ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
91          &MARK_CURRENT_PROPERTY(mark)->back_color : \
92          &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
93 #define MARK_CURRENT_TEXT_FONT(text, mark) \
94   ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
95          MARK_CURRENT_PROPERTY(mark)->font : \
96          text->current_font)
97
98 #define TEXT_LENGTH(t)              ((t)->text_end - (t)->gap_size)
99 #define FONT_HEIGHT(f)              ((f)->ascent + (f)->descent)
100 #define LINE_HEIGHT(l)              ((l).font_ascent + (l).font_descent)
101 #define LINE_CONTAINS(l, i)         ((l).start.index <= (i) && (l).end.index >= (i))
102 #define LINE_STARTS_AT(l, i)        ((l).start.index == (i))
103 #define LINE_START_PIXEL(l)         ((l).tab_cont.pixel_offset)
104 #define LAST_INDEX(t, m)            ((m).index == TEXT_LENGTH(t))
105 #define CACHE_DATA(c)               (*(LineParams*)(c)->data)
106
107 enum {
108   PROP_0,
109   PROP_HADJUSTMENT,
110   PROP_VADJUSTMENT,
111   PROP_LINE_WRAP,
112   PROP_WORD_WRAP
113 };
114
115 typedef struct _TextProperty          TextProperty;
116 typedef struct _TabStopMark           TabStopMark;
117 typedef struct _PrevTabCont           PrevTabCont;
118 typedef struct _FetchLinesData        FetchLinesData;
119 typedef struct _LineParams            LineParams;
120 typedef struct _SetVerticalScrollData SetVerticalScrollData;
121
122 typedef gint (*LineIteratorFunction) (GtkText* text, LineParams* lp, void* data);
123
124 typedef enum
125 {
126   FetchLinesPixels,
127   FetchLinesCount
128 } FLType;
129
130 struct _SetVerticalScrollData {
131   gint pixel_height;
132   gint last_didnt_wrap;
133   gint last_line_start;
134   GtkPropertyMark mark;
135 };
136
137 struct _GtkTextFont
138 {
139   /* The actual font. */
140   GdkFont *gdk_font;
141   guint ref_count;
142
143   gint16 char_widths[256];
144 };
145
146 typedef enum {
147   PROPERTY_FONT =       1 << 0,
148   PROPERTY_FOREGROUND = 1 << 1,
149   PROPERTY_BACKGROUND = 1 << 2
150 } TextPropertyFlags;
151
152 struct _TextProperty
153 {
154   /* Font. */
155   GtkTextFont* font;
156
157   /* Background Color. */
158   GdkColor back_color;
159   
160   /* Foreground Color. */
161   GdkColor fore_color;
162
163   /* Show which properties are set */
164   TextPropertyFlags flags;
165
166   /* Length of this property. */
167   guint length;
168 };
169
170 struct _TabStopMark
171 {
172   GList* tab_stops; /* Index into list containing the next tab position.  If
173                      * NULL, using default widths. */
174   gint to_next_tab;
175 };
176
177 struct _PrevTabCont
178 {
179   guint pixel_offset;
180   TabStopMark tab_start;
181 };
182
183 struct _FetchLinesData
184 {
185   GList* new_lines;
186   FLType fl_type;
187   gint data;
188   gint data_max;
189 };
190
191 struct _LineParams
192 {
193   guint font_ascent;
194   guint font_descent;
195   guint pixel_width;
196   guint displayable_chars;
197   guint wraps : 1;
198   
199   PrevTabCont tab_cont;
200   PrevTabCont tab_cont_next;
201   
202   GtkPropertyMark start;
203   GtkPropertyMark end;
204 };
205
206
207 static void  gtk_text_class_init     (GtkTextClass   *klass);
208 static void  gtk_text_set_property   (GObject         *object,
209                                       guint            prop_id,
210                                       const GValue    *value,
211                                       GParamSpec      *pspec);
212 static void  gtk_text_get_property   (GObject         *object,
213                                       guint            prop_id,
214                                       GValue          *value,
215                                       GParamSpec      *pspec);
216 static void  gtk_text_editable_init  (GtkEditableClass *iface);
217 static void  gtk_text_init           (GtkText        *text);
218 static void  gtk_text_destroy        (GtkObject      *object);
219 static void  gtk_text_finalize       (GObject        *object);
220 static void  gtk_text_realize        (GtkWidget      *widget);
221 static void  gtk_text_unrealize      (GtkWidget      *widget);
222 static void  gtk_text_style_set      (GtkWidget      *widget,
223                                       GtkStyle       *previous_style);
224 static void  gtk_text_state_changed  (GtkWidget      *widget,
225                                       GtkStateType    previous_state);
226 static void  gtk_text_draw_focus     (GtkWidget      *widget);
227 static void  gtk_text_size_request   (GtkWidget      *widget,
228                                       GtkRequisition *requisition);
229 static void  gtk_text_size_allocate  (GtkWidget      *widget,
230                                       GtkAllocation  *allocation);
231 static void  gtk_text_adjustment     (GtkAdjustment  *adjustment,
232                                       GtkText        *text);
233 static void   gtk_text_insert_text       (GtkEditable    *editable,
234                                           const gchar    *new_text,
235                                           gint            new_text_length,
236                                           gint           *position);
237 static void   gtk_text_delete_text       (GtkEditable    *editable,
238                                           gint            start_pos,
239                                           gint            end_pos);
240 static void   gtk_text_update_text       (GtkOldEditable *old_editable,
241                                           gint            start_pos,
242                                           gint            end_pos);
243 static gchar *gtk_text_get_chars         (GtkOldEditable *old_editable,
244                                           gint            start,
245                                           gint            end);
246 static void   gtk_text_set_selection     (GtkOldEditable *old_editable,
247                                           gint            start,
248                                           gint            end);
249 static void   gtk_text_real_set_editable (GtkOldEditable *old_editable,
250                                           gboolean        is_editable);
251
252 static void  gtk_text_adjustment_destroyed (GtkAdjustment  *adjustment,
253                                             GtkText        *text);
254
255 /* Event handlers */
256 static gint  gtk_text_expose            (GtkWidget         *widget,
257                                          GdkEventExpose    *event);
258 static gint  gtk_text_button_press      (GtkWidget         *widget,
259                                          GdkEventButton    *event);
260 static gint  gtk_text_button_release    (GtkWidget         *widget,
261                                          GdkEventButton    *event);
262 static gint  gtk_text_motion_notify     (GtkWidget         *widget,
263                                          GdkEventMotion    *event);
264 static gint  gtk_text_key_press         (GtkWidget         *widget,
265                                          GdkEventKey       *event);
266
267 static void move_gap (GtkText* text, guint index);
268 static void make_forward_space (GtkText* text, guint len);
269
270 /* Property management */
271 static GtkTextFont* get_text_font (GdkFont* gfont);
272 static void         text_font_unref (GtkTextFont *text_font);
273
274 static void insert_text_property (GtkText* text, GdkFont* font,
275                                   const GdkColor *fore, const GdkColor* back, guint len);
276 static TextProperty* new_text_property (GtkText *text, GdkFont* font, 
277                                         const GdkColor* fore, const GdkColor* back, guint length);
278 static void destroy_text_property (TextProperty *prop);
279 static void init_properties      (GtkText *text);
280 static void realize_property     (GtkText *text, TextProperty *prop);
281 static void realize_properties   (GtkText *text);
282 static void unrealize_property   (GtkText *text, TextProperty *prop);
283 static void unrealize_properties (GtkText *text);
284
285 static void delete_text_property (GtkText* text, guint len);
286
287 static guint pixel_height_of (GtkText* text, GList* cache_line);
288
289 /* Property Movement and Size Computations */
290 static void advance_mark (GtkPropertyMark* mark);
291 static void decrement_mark (GtkPropertyMark* mark);
292 static void advance_mark_n (GtkPropertyMark* mark, gint n);
293 static void decrement_mark_n (GtkPropertyMark* mark, gint n);
294 static void move_mark_n (GtkPropertyMark* mark, gint n);
295 static GtkPropertyMark find_mark (GtkText* text, guint mark_position);
296 static GtkPropertyMark find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near);
297 static void find_line_containing_point (GtkText* text, guint point,
298                                         gboolean scroll);
299
300 /* Display */
301 static void compute_lines_pixels (GtkText* text, guint char_count,
302                                   guint *lines, guint *pixels);
303
304 static gint total_line_height (GtkText* text,
305                                GList* line,
306                                gint line_count);
307 static LineParams find_line_params (GtkText* text,
308                                     const GtkPropertyMark *mark,
309                                     const PrevTabCont *tab_cont,
310                                     PrevTabCont *next_cont);
311 static void recompute_geometry (GtkText* text);
312 static void insert_expose (GtkText* text, guint old_pixels, gint nchars, guint new_line_count);
313 static void delete_expose (GtkText* text,
314                            guint nchars,
315                            guint old_lines, 
316                            guint old_pixels);
317 static GdkGC *create_bg_gc (GtkText *text);
318 static void clear_area (GtkText *text, GdkRectangle *area);
319 static void draw_line (GtkText* text,
320                        gint pixel_height,
321                        LineParams* lp);
322 static void draw_line_wrap (GtkText* text,
323                             guint height);
324 static void draw_cursor (GtkText* text, gint absolute);
325 static void undraw_cursor (GtkText* text, gint absolute);
326 static gint drawn_cursor_min (GtkText* text);
327 static gint drawn_cursor_max (GtkText* text);
328 static void expose_text (GtkText* text, GdkRectangle *area, gboolean cursor);
329
330 /* Search and Placement. */
331 static void find_cursor (GtkText* text,
332                          gboolean scroll);
333 static void find_cursor_at_line (GtkText* text,
334                                  const LineParams* start_line,
335                                  gint pixel_height);
336 static void find_mouse_cursor (GtkText* text, gint x, gint y);
337
338 /* Scrolling. */
339 static void adjust_adj  (GtkText* text, GtkAdjustment* adj);
340 static void scroll_up   (GtkText* text, gint diff);
341 static void scroll_down (GtkText* text, gint diff);
342 static void scroll_int  (GtkText* text, gint diff);
343
344 static void process_exposes (GtkText *text);
345
346 /* Cache Management. */
347 static void   free_cache        (GtkText* text);
348 static GList* remove_cache_line (GtkText* text, GList* list);
349
350 /* Key Motion. */
351 static void move_cursor_buffer_ver (GtkText *text, int dir);
352 static void move_cursor_page_ver (GtkText *text, int dir);
353 static void move_cursor_ver (GtkText *text, int count);
354 static void move_cursor_hor (GtkText *text, int count);
355
356 /* Binding actions */
357 static void gtk_text_move_cursor    (GtkOldEditable *old_editable,
358                                      gint            x,
359                                      gint            y);
360 static void gtk_text_move_word      (GtkOldEditable *old_editable,
361                                      gint            n);
362 static void gtk_text_move_page      (GtkOldEditable *old_editable,
363                                      gint            x,
364                                      gint            y);
365 static void gtk_text_move_to_row    (GtkOldEditable *old_editable,
366                                      gint            row);
367 static void gtk_text_move_to_column (GtkOldEditable *old_editable,
368                                      gint            row);
369 static void gtk_text_kill_char      (GtkOldEditable *old_editable,
370                                      gint            direction);
371 static void gtk_text_kill_word      (GtkOldEditable *old_editable,
372                                      gint            direction);
373 static void gtk_text_kill_line      (GtkOldEditable *old_editable,
374                                      gint            direction);
375
376 /* To be removed */
377 static void gtk_text_move_forward_character    (GtkText          *text);
378 static void gtk_text_move_backward_character   (GtkText          *text);
379 static void gtk_text_move_forward_word         (GtkText          *text);
380 static void gtk_text_move_backward_word        (GtkText          *text);
381 static void gtk_text_move_beginning_of_line    (GtkText          *text);
382 static void gtk_text_move_end_of_line          (GtkText          *text);
383 static void gtk_text_move_next_line            (GtkText          *text);
384 static void gtk_text_move_previous_line        (GtkText          *text);
385
386 static void gtk_text_delete_forward_character  (GtkText          *text);
387 static void gtk_text_delete_backward_character (GtkText          *text);
388 static void gtk_text_delete_forward_word       (GtkText          *text);
389 static void gtk_text_delete_backward_word      (GtkText          *text);
390 static void gtk_text_delete_line               (GtkText          *text);
391 static void gtk_text_delete_to_line_end        (GtkText          *text);
392 static void gtk_text_select_word               (GtkText          *text,
393                                                 guint32           time);
394 static void gtk_text_select_line               (GtkText          *text,
395                                                 guint32           time);
396
397 static void gtk_text_set_position (GtkOldEditable *old_editable,
398                                    gint            position);
399
400 /* #define DEBUG_GTK_TEXT */
401
402 #if defined(DEBUG_GTK_TEXT) && defined(__GNUC__)
403 /* Debugging utilities. */
404 static void gtk_text_assert_mark (GtkText         *text,
405                                   GtkPropertyMark *mark,
406                                   GtkPropertyMark *before,
407                                   GtkPropertyMark *after,
408                                   const gchar     *msg,
409                                   const gchar     *where,
410                                   gint             line);
411
412 static void gtk_text_assert (GtkText         *text,
413                              const gchar     *msg,
414                              gint             line);
415 static void gtk_text_show_cache_line (GtkText *text, GList *cache,
416                                       const char* what, const char* func, gint line);
417 static void gtk_text_show_cache (GtkText *text, const char* func, gint line);
418 static void gtk_text_show_adj (GtkText *text,
419                                GtkAdjustment *adj,
420                                const char* what,
421                                const char* func,
422                                gint line);
423 static void gtk_text_show_props (GtkText* test,
424                                  const char* func,
425                                  int line);
426
427 #define TDEBUG(args) g_message args
428 #define TEXT_ASSERT(text) gtk_text_assert (text,__PRETTY_FUNCTION__,__LINE__)
429 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_text_assert_mark (text,mark, \
430                                            __PRETTY_FUNCTION__,msg,__LINE__)
431 #define TEXT_SHOW(text) gtk_text_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
432 #define TEXT_SHOW_LINE(text,line,msg) gtk_text_show_cache_line (text,line,msg,\
433                                            __PRETTY_FUNCTION__,__LINE__)
434 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_text_show_adj (text,adj,msg, \
435                                           __PRETTY_FUNCTION__,__LINE__)
436 #else
437 #define TDEBUG(args)
438 #define TEXT_ASSERT(text)
439 #define TEXT_ASSERT_MARK(text,mark,msg)
440 #define TEXT_SHOW(text)
441 #define TEXT_SHOW_LINE(text,line,msg)
442 #define TEXT_SHOW_ADJ(text,adj,msg)
443 #endif
444
445 static GtkWidgetClass *parent_class = NULL;
446
447 /**********************************************************************/
448 /*                              Widget Crap                           */
449 /**********************************************************************/
450
451 GtkType
452 gtk_text_get_type (void)
453 {
454   static GtkType text_type = 0;
455   
456   if (!text_type)
457     {
458       static const GtkTypeInfo text_info =
459       {
460         "GtkText",
461         sizeof (GtkText),
462         sizeof (GtkTextClass),
463         (GtkClassInitFunc) gtk_text_class_init,
464         (GtkObjectInitFunc) gtk_text_init,
465         /* reserved_1 */ NULL,
466         /* reserved_2 */ NULL,
467         (GtkClassInitFunc) NULL,
468       };
469
470       static const GInterfaceInfo editable_info =
471       {
472         (GInterfaceInitFunc) gtk_text_editable_init, /* interface_init */
473         NULL, /* interface_finalize */
474         NULL  /* interface_data */
475       };
476       
477       I_("GtkText");
478       text_type = gtk_type_unique (GTK_TYPE_OLD_EDITABLE, &text_info);
479       g_type_add_interface_static (text_type,
480                                    GTK_TYPE_EDITABLE,
481                                    &editable_info);
482     }
483   
484   return text_type;
485 }
486
487 static void
488 gtk_text_class_init (GtkTextClass *class)
489 {
490   GObjectClass *gobject_class;
491   GtkObjectClass *object_class;
492   GtkWidgetClass *widget_class;
493   GtkOldEditableClass *old_editable_class;
494
495   gobject_class = G_OBJECT_CLASS (class);
496   object_class = (GtkObjectClass*) class;
497   widget_class = (GtkWidgetClass*) class;
498   old_editable_class = (GtkOldEditableClass*) class;
499   parent_class = gtk_type_class (GTK_TYPE_OLD_EDITABLE);
500
501   gobject_class->finalize = gtk_text_finalize;
502   gobject_class->set_property = gtk_text_set_property;
503   gobject_class->get_property = gtk_text_get_property;
504   
505   object_class->destroy = gtk_text_destroy;
506   
507   widget_class->realize = gtk_text_realize;
508   widget_class->unrealize = gtk_text_unrealize;
509   widget_class->style_set = gtk_text_style_set;
510   widget_class->state_changed = gtk_text_state_changed;
511   widget_class->size_request = gtk_text_size_request;
512   widget_class->size_allocate = gtk_text_size_allocate;
513   widget_class->expose_event = gtk_text_expose;
514   widget_class->button_press_event = gtk_text_button_press;
515   widget_class->button_release_event = gtk_text_button_release;
516   widget_class->motion_notify_event = gtk_text_motion_notify;
517   widget_class->key_press_event = gtk_text_key_press;
518
519   old_editable_class->set_editable = gtk_text_real_set_editable;
520   
521   old_editable_class->move_cursor = gtk_text_move_cursor;
522   old_editable_class->move_word = gtk_text_move_word;
523   old_editable_class->move_page = gtk_text_move_page;
524   old_editable_class->move_to_row = gtk_text_move_to_row;
525   old_editable_class->move_to_column = gtk_text_move_to_column;
526   
527   old_editable_class->kill_char = gtk_text_kill_char;
528   old_editable_class->kill_word = gtk_text_kill_word;
529   old_editable_class->kill_line = gtk_text_kill_line;
530   
531   old_editable_class->update_text = gtk_text_update_text;
532   old_editable_class->get_chars   = gtk_text_get_chars;
533   old_editable_class->set_selection = gtk_text_set_selection;
534   old_editable_class->set_position = gtk_text_set_position;
535
536   class->set_scroll_adjustments = gtk_text_set_adjustments;
537
538   g_object_class_install_property (gobject_class,
539                                    PROP_HADJUSTMENT,
540                                    g_param_spec_object ("hadjustment",
541                                                         P_("Horizontal Adjustment"),
542                                                         P_("Horizontal adjustment for the text widget"),
543                                                         GTK_TYPE_ADJUSTMENT,
544                                                         GTK_PARAM_READWRITE));
545
546   g_object_class_install_property (gobject_class,
547                                    PROP_VADJUSTMENT,
548                                    g_param_spec_object ("vadjustment",
549                                                         P_("Vertical Adjustment"),
550                                                         P_("Vertical adjustment for the text widget"),
551                                                         GTK_TYPE_ADJUSTMENT,
552                                                         GTK_PARAM_READWRITE));
553
554   g_object_class_install_property (gobject_class,
555                                    PROP_LINE_WRAP,
556                                    g_param_spec_boolean ("line-wrap",
557                                                          P_("Line Wrap"),
558                                                          P_("Whether lines are wrapped at widget edges"),
559                                                          TRUE,
560                                                          GTK_PARAM_READWRITE));
561
562   g_object_class_install_property (gobject_class,
563                                    PROP_WORD_WRAP,
564                                    g_param_spec_boolean ("word-wrap",
565                                                          P_("Word Wrap"),
566                                                          P_("Whether words are wrapped at widget edges"),
567                                                          FALSE,
568                                                          GTK_PARAM_READWRITE));
569
570   widget_class->set_scroll_adjustments_signal =
571     gtk_signal_new (I_("set_scroll_adjustments"),
572                     GTK_RUN_LAST,
573                     GTK_CLASS_TYPE (object_class),
574                     GTK_SIGNAL_OFFSET (GtkTextClass, set_scroll_adjustments),
575                     _gtk_marshal_VOID__OBJECT_OBJECT,
576                     GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
577 }
578
579 static void
580 gtk_text_set_property (GObject         *object,
581                        guint            prop_id,
582                        const GValue    *value,
583                        GParamSpec      *pspec)
584 {
585   GtkText *text;
586   
587   text = GTK_TEXT (object);
588   
589   switch (prop_id)
590     {
591     case PROP_HADJUSTMENT:
592       gtk_text_set_adjustments (text,
593                                 g_value_get_object (value),
594                                 text->vadj);
595       break;
596     case PROP_VADJUSTMENT:
597       gtk_text_set_adjustments (text,
598                                 text->hadj,
599                                 g_value_get_object (value));
600       break;
601     case PROP_LINE_WRAP:
602       gtk_text_set_line_wrap (text, g_value_get_boolean (value));
603       break;
604     case PROP_WORD_WRAP:
605       gtk_text_set_word_wrap (text, g_value_get_boolean (value));
606       break;
607     default:
608       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
609       break;
610     }
611 }
612
613 static void
614 gtk_text_get_property (GObject         *object,
615                        guint            prop_id,
616                        GValue          *value,
617                        GParamSpec      *pspec)
618 {
619   GtkText *text;
620   
621   text = GTK_TEXT (object);
622   
623   switch (prop_id)
624     {
625     case PROP_HADJUSTMENT:
626       g_value_set_object (value, text->hadj);
627       break;
628     case PROP_VADJUSTMENT:
629       g_value_set_object (value, text->vadj);
630       break;
631     case PROP_LINE_WRAP:
632       g_value_set_boolean (value, text->line_wrap);
633       break;
634     case PROP_WORD_WRAP:
635       g_value_set_boolean (value, text->word_wrap);
636       break;
637     default:
638       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
639       break;
640     }
641 }
642
643 static void
644 gtk_text_editable_init (GtkEditableClass *iface)
645 {
646   iface->insert_text = gtk_text_insert_text;
647   iface->delete_text = gtk_text_delete_text;
648 }
649
650 static void
651 gtk_text_init (GtkText *text)
652 {
653   GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
654
655   text->text_area = NULL;
656   text->hadj = NULL;
657   text->vadj = NULL;
658   text->gc = NULL;
659   text->bg_gc = NULL;
660   text->line_wrap_bitmap = NULL;
661   text->line_arrow_bitmap = NULL;
662   
663   text->use_wchar = FALSE;
664   text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
665   text->text_len = INITIAL_BUFFER_SIZE;
666  
667   text->scratch_buffer.ch = NULL;
668   text->scratch_buffer_len = 0;
669  
670   text->freeze_count = 0;
671   
672   text->default_tab_width = 4;
673   text->tab_stops = NULL;
674   
675   text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
676   text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
677   
678   text->line_start_cache = NULL;
679   text->first_cut_pixels = 0;
680   
681   text->line_wrap = TRUE;
682   text->word_wrap = FALSE;
683   
684   text->timer = 0;
685   text->button = 0;
686   
687   text->current_font = NULL;
688   
689   init_properties (text);
690   
691   GTK_OLD_EDITABLE (text)->editable = FALSE;
692   
693   gtk_text_set_adjustments (text, NULL, NULL);
694   gtk_editable_set_position (GTK_EDITABLE (text), 0);
695 }
696
697 GtkWidget*
698 gtk_text_new (GtkAdjustment *hadj,
699               GtkAdjustment *vadj)
700 {
701   GtkWidget *text;
702
703   if (hadj)
704     g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
705   if (vadj)
706     g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
707
708   text = gtk_widget_new (GTK_TYPE_TEXT,
709                          "hadjustment", hadj,
710                          "vadjustment", vadj,
711                          NULL);
712
713   return text;
714 }
715
716 void
717 gtk_text_set_word_wrap (GtkText *text,
718                         gboolean word_wrap)
719 {
720   g_return_if_fail (GTK_IS_TEXT (text));
721   
722   text->word_wrap = (word_wrap != FALSE);
723   
724   if (GTK_WIDGET_REALIZED (text))
725     {
726       recompute_geometry (text);
727       gtk_widget_queue_draw (GTK_WIDGET (text));
728     }
729   
730   g_object_notify (G_OBJECT (text), "word-wrap");
731 }
732
733 void
734 gtk_text_set_line_wrap (GtkText *text,
735                         gboolean line_wrap)
736 {
737   g_return_if_fail (GTK_IS_TEXT (text));
738   
739   text->line_wrap = (line_wrap != FALSE);
740   
741   if (GTK_WIDGET_REALIZED (text))
742     {
743       recompute_geometry (text);
744       gtk_widget_queue_draw (GTK_WIDGET (text));
745     }
746   
747   g_object_notify (G_OBJECT (text), "line-wrap");
748 }
749
750 void
751 gtk_text_set_editable (GtkText *text,
752                        gboolean is_editable)
753 {
754   g_return_if_fail (GTK_IS_TEXT (text));
755   
756   gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
757 }
758
759 static void
760 gtk_text_real_set_editable (GtkOldEditable *old_editable,
761                             gboolean        is_editable)
762 {
763   GtkText *text;
764   
765   g_return_if_fail (GTK_IS_TEXT (old_editable));
766   
767   text = GTK_TEXT (old_editable);
768   
769   old_editable->editable = (is_editable != FALSE);
770   
771   if (is_editable)
772     draw_cursor (text, TRUE);
773   else
774     undraw_cursor (text, TRUE);
775 }
776
777 void
778 gtk_text_set_adjustments (GtkText       *text,
779                           GtkAdjustment *hadj,
780                           GtkAdjustment *vadj)
781 {
782   g_return_if_fail (GTK_IS_TEXT (text));
783   if (hadj)
784     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
785   else
786     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
787   if (vadj)
788     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
789   else
790     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
791   
792   if (text->hadj && (text->hadj != hadj))
793     {
794       gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
795       gtk_object_unref (GTK_OBJECT (text->hadj));
796     }
797   
798   if (text->vadj && (text->vadj != vadj))
799     {
800       gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
801       gtk_object_unref (GTK_OBJECT (text->vadj));
802     }
803   
804   g_object_freeze_notify (G_OBJECT (text));
805   if (text->hadj != hadj)
806     {
807       text->hadj = hadj;
808       g_object_ref_sink (text->hadj);
809       
810       gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
811                           (GtkSignalFunc) gtk_text_adjustment,
812                           text);
813       gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
814                           (GtkSignalFunc) gtk_text_adjustment,
815                           text);
816       gtk_signal_connect (GTK_OBJECT (text->hadj), "destroy",
817                           (GtkSignalFunc) gtk_text_adjustment_destroyed,
818                           text);
819       gtk_text_adjustment (hadj, text);
820
821       g_object_notify (G_OBJECT (text), "hadjustment");
822     }
823   
824   if (text->vadj != vadj)
825     {
826       text->vadj = vadj;
827       g_object_ref_sink (text->vadj);
828       
829       gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
830                           (GtkSignalFunc) gtk_text_adjustment,
831                           text);
832       gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
833                           (GtkSignalFunc) gtk_text_adjustment,
834                           text);
835       gtk_signal_connect (GTK_OBJECT (text->vadj), "destroy",
836                           (GtkSignalFunc) gtk_text_adjustment_destroyed,
837                           text);
838       gtk_text_adjustment (vadj, text);
839
840       g_object_notify (G_OBJECT (text), "vadjustment");
841     }
842   g_object_thaw_notify (G_OBJECT (text));
843 }
844
845 void
846 gtk_text_set_point (GtkText *text,
847                     guint    index)
848 {
849   g_return_if_fail (GTK_IS_TEXT (text));
850   g_return_if_fail (index <= TEXT_LENGTH (text));
851   
852   text->point = find_mark (text, index);
853 }
854
855 guint
856 gtk_text_get_point (GtkText *text)
857 {
858   g_return_val_if_fail (GTK_IS_TEXT (text), 0);
859   
860   return text->point.index;
861 }
862
863 guint
864 gtk_text_get_length (GtkText *text)
865 {
866   g_return_val_if_fail (GTK_IS_TEXT (text), 0);
867   
868   return TEXT_LENGTH (text);
869 }
870
871 void
872 gtk_text_freeze (GtkText *text)
873 {
874   g_return_if_fail (GTK_IS_TEXT (text));
875   
876   text->freeze_count++;
877 }
878
879 void
880 gtk_text_thaw (GtkText *text)
881 {
882   g_return_if_fail (GTK_IS_TEXT (text));
883   
884   if (text->freeze_count)
885     if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
886       {
887         recompute_geometry (text);
888         gtk_widget_queue_draw (GTK_WIDGET (text));
889       }
890 }
891
892 void
893 gtk_text_insert (GtkText        *text,
894                  GdkFont        *font,
895                  const GdkColor *fore,
896                  const GdkColor *back,
897                  const char     *chars,
898                  gint            nchars)
899 {
900   GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
901   gboolean frozen = FALSE;
902   
903   gint new_line_count = 1;
904   guint old_height = 0;
905   guint length;
906   guint i;
907   gint numwcs;
908   
909   g_return_if_fail (GTK_IS_TEXT (text));
910
911   if (nchars < 0)
912     length = strlen (chars);
913   else
914     length = nchars;
915   
916   if (length == 0)
917     return;
918   
919   if (!text->freeze_count && (length > FREEZE_LENGTH))
920     {
921       gtk_text_freeze (text);
922       frozen = TRUE;
923     }
924   
925   if (!text->freeze_count && (text->line_start_cache != NULL))
926     {
927       find_line_containing_point (text, text->point.index, TRUE);
928       old_height = total_line_height (text, text->current_line, 1);
929     }
930   
931   if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
932     {
933       GtkWidget *widget = GTK_WIDGET (text);
934       
935       gtk_widget_ensure_style (widget);
936       if ((widget->style) &&
937           (gtk_style_get_font (widget->style)->type == GDK_FONT_FONTSET))
938         {
939           text->use_wchar = TRUE;
940           g_free (text->text.ch);
941           text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
942           text->text_len = INITIAL_BUFFER_SIZE;
943           if (text->scratch_buffer.ch)
944             g_free (text->scratch_buffer.ch);
945           text->scratch_buffer.wc = NULL;
946           text->scratch_buffer_len = 0;
947         }
948     }
949  
950   move_gap (text, text->point.index);
951   make_forward_space (text, length);
952  
953   if (text->use_wchar)
954     {
955       char *chars_nt = (char *)chars;
956       if (nchars > 0)
957         {
958           chars_nt = g_new (char, length+1);
959           memcpy (chars_nt, chars, length);
960           chars_nt[length] = 0;
961         }
962       numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
963                              length);
964       if (chars_nt != chars)
965         g_free(chars_nt);
966       if (numwcs < 0)
967         numwcs = 0;
968     }
969   else
970     {
971       numwcs = length;
972       memcpy(text->text.ch + text->gap_position, chars, length);
973     }
974  
975   if (!text->freeze_count && (text->line_start_cache != NULL))
976     {
977       if (text->use_wchar)
978         {
979           for (i=0; i<numwcs; i++)
980             if (text->text.wc[text->gap_position + i] == '\n')
981               new_line_count++;
982         }
983       else
984         {
985           for (i=0; i<numwcs; i++)
986             if (text->text.ch[text->gap_position + i] == '\n')
987               new_line_count++;
988         }
989     }
990  
991   if (numwcs > 0)
992     {
993       insert_text_property (text, font, fore, back, numwcs);
994    
995       text->gap_size -= numwcs;
996       text->gap_position += numwcs;
997    
998       if (text->point.index < text->first_line_start_index)
999         text->first_line_start_index += numwcs;
1000       if (text->point.index < old_editable->selection_start_pos)
1001         old_editable->selection_start_pos += numwcs;
1002       if (text->point.index < old_editable->selection_end_pos)
1003         old_editable->selection_end_pos += numwcs;
1004       /* We'll reset the cursor later anyways if we aren't frozen */
1005       if (text->point.index < text->cursor_mark.index)
1006         text->cursor_mark.index += numwcs;
1007   
1008       advance_mark_n (&text->point, numwcs);
1009   
1010       if (!text->freeze_count && (text->line_start_cache != NULL))
1011         insert_expose (text, old_height, numwcs, new_line_count);
1012     }
1013
1014   if (frozen)
1015     gtk_text_thaw (text);
1016 }
1017
1018 gboolean
1019 gtk_text_backward_delete (GtkText *text,
1020                           guint    nchars)
1021 {
1022   g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1023   
1024   if (nchars > text->point.index || nchars <= 0)
1025     return FALSE;
1026   
1027   gtk_text_set_point (text, text->point.index - nchars);
1028   
1029   return gtk_text_forward_delete (text, nchars);
1030 }
1031
1032 gboolean
1033 gtk_text_forward_delete (GtkText *text,
1034                          guint    nchars)
1035 {
1036   guint old_lines = 0, old_height = 0;
1037   GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
1038   gboolean frozen = FALSE;
1039   
1040   g_return_val_if_fail (GTK_IS_TEXT (text), FALSE);
1041   
1042   if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1043     return FALSE;
1044   
1045   if (!text->freeze_count && nchars > FREEZE_LENGTH)
1046     {
1047       gtk_text_freeze (text);
1048       frozen = TRUE;
1049     }
1050   
1051   if (!text->freeze_count && text->line_start_cache != NULL)
1052     {
1053       /* We need to undraw the cursor here, since we may later
1054        * delete the cursor's property
1055        */
1056       undraw_cursor (text, FALSE);
1057       find_line_containing_point (text, text->point.index, TRUE);
1058       compute_lines_pixels (text, nchars, &old_lines, &old_height);
1059     }
1060   
1061   /* FIXME, or resizing after deleting will be odd */
1062   if (text->point.index < text->first_line_start_index)
1063     {
1064       if (text->point.index + nchars >= text->first_line_start_index)
1065         {
1066           text->first_line_start_index = text->point.index;
1067           while ((text->first_line_start_index > 0) &&
1068                  (GTK_TEXT_INDEX (text, text->first_line_start_index - 1)
1069                   != LINE_DELIM))
1070             text->first_line_start_index -= 1;
1071           
1072         }
1073       else
1074         text->first_line_start_index -= nchars;
1075     }
1076   
1077   if (text->point.index < old_editable->selection_start_pos)
1078     old_editable->selection_start_pos -= 
1079       MIN(nchars, old_editable->selection_start_pos - text->point.index);
1080   if (text->point.index < old_editable->selection_end_pos)
1081     old_editable->selection_end_pos -= 
1082       MIN(nchars, old_editable->selection_end_pos - text->point.index);
1083   /* We'll reset the cursor later anyways if we aren't frozen */
1084   if (text->point.index < text->cursor_mark.index)
1085     move_mark_n (&text->cursor_mark, 
1086                  -MIN(nchars, text->cursor_mark.index - text->point.index));
1087   
1088   move_gap (text, text->point.index);
1089   
1090   text->gap_size += nchars;
1091   
1092   delete_text_property (text, nchars);
1093   
1094   if (!text->freeze_count && (text->line_start_cache != NULL))
1095     {
1096       delete_expose (text, nchars, old_lines, old_height);
1097       draw_cursor (text, FALSE);
1098     }
1099   
1100   if (frozen)
1101     gtk_text_thaw (text);
1102   
1103   return TRUE;
1104 }
1105
1106 static void
1107 gtk_text_set_position (GtkOldEditable *old_editable,
1108                        gint            position)
1109 {
1110   GtkText *text = (GtkText *) old_editable;
1111
1112   if (position < 0)
1113     position = gtk_text_get_length (text);                                    
1114   
1115   undraw_cursor (text, FALSE);
1116   text->cursor_mark = find_mark (text, position);
1117   find_cursor (text, TRUE);
1118   draw_cursor (text, FALSE);
1119   gtk_editable_select_region (GTK_EDITABLE (old_editable), 0, 0);
1120 }
1121
1122 static gchar *    
1123 gtk_text_get_chars (GtkOldEditable *old_editable,
1124                     gint            start_pos,
1125                     gint            end_pos)
1126 {
1127   GtkText *text;
1128
1129   gchar *retval;
1130   
1131   g_return_val_if_fail (GTK_IS_TEXT (old_editable), NULL);
1132   text = GTK_TEXT (old_editable);
1133   
1134   if (end_pos < 0)
1135     end_pos = TEXT_LENGTH (text);
1136   
1137   if ((start_pos < 0) || 
1138       (end_pos > TEXT_LENGTH (text)) || 
1139       (end_pos < start_pos))
1140     return NULL;
1141   
1142   move_gap (text, TEXT_LENGTH (text));
1143   make_forward_space (text, 1);
1144
1145   if (text->use_wchar)
1146     {
1147       GdkWChar ch;
1148       ch = text->text.wc[end_pos];
1149       text->text.wc[end_pos] = 0;
1150       retval = gdk_wcstombs (text->text.wc + start_pos);
1151       text->text.wc[end_pos] = ch;
1152     }
1153   else
1154     {
1155       guchar ch;
1156       ch = text->text.ch[end_pos];
1157       text->text.ch[end_pos] = 0;
1158       retval = g_strdup (text->text.ch + start_pos);
1159       text->text.ch[end_pos] = ch;
1160     }
1161
1162   return retval;
1163 }
1164
1165
1166 static void
1167 gtk_text_destroy (GtkObject *object)
1168 {
1169   GtkText *text;
1170   
1171   g_return_if_fail (GTK_IS_TEXT (object));
1172   
1173   text = GTK_TEXT (object);
1174
1175   if (text->hadj)
1176     {
1177       gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1178       gtk_object_unref (GTK_OBJECT (text->hadj));
1179       text->hadj = NULL;
1180     }
1181   if (text->vadj)
1182     {
1183       gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1184       gtk_object_unref (GTK_OBJECT (text->vadj));
1185       text->vadj = NULL;
1186     }
1187
1188   if (text->timer)
1189     {
1190       g_source_remove (text->timer);
1191       text->timer = 0;
1192     }
1193   
1194   GTK_OBJECT_CLASS(parent_class)->destroy (object);
1195 }
1196
1197 static void
1198 gtk_text_finalize (GObject *object)
1199 {
1200   GtkText *text;
1201   GList *tmp_list;
1202   
1203   g_return_if_fail (GTK_IS_TEXT (object));
1204   
1205   text = GTK_TEXT (object);
1206
1207   /* Clean up the internal structures */
1208   if (text->use_wchar)
1209     g_free (text->text.wc);
1210   else
1211     g_free (text->text.ch);
1212   
1213   tmp_list = text->text_properties;
1214   while (tmp_list)
1215     {
1216       destroy_text_property (tmp_list->data);
1217       tmp_list = tmp_list->next;
1218     }
1219
1220   if (text->current_font)
1221     text_font_unref (text->current_font);
1222   
1223   g_list_free (text->text_properties);
1224   
1225   if (text->use_wchar)
1226     {
1227       if (text->scratch_buffer.wc)
1228         g_free (text->scratch_buffer.wc);
1229     }
1230   else
1231     {
1232       if (text->scratch_buffer.ch)
1233         g_free (text->scratch_buffer.ch);
1234     }
1235   
1236   g_list_free (text->tab_stops);
1237   
1238   G_OBJECT_CLASS (parent_class)->finalize (object);
1239 }
1240
1241 static void
1242 gtk_text_realize (GtkWidget *widget)
1243 {
1244   GtkText *text;
1245   GtkOldEditable *old_editable;
1246   GdkWindowAttr attributes;
1247   gint attributes_mask;
1248   
1249   g_return_if_fail (GTK_IS_TEXT (widget));
1250   
1251   text = GTK_TEXT (widget);
1252   old_editable = GTK_OLD_EDITABLE (widget);
1253   GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1254   
1255   attributes.window_type = GDK_WINDOW_CHILD;
1256   attributes.x = widget->allocation.x;
1257   attributes.y = widget->allocation.y;
1258   attributes.width = widget->allocation.width;
1259   attributes.height = widget->allocation.height;
1260   attributes.wclass = GDK_INPUT_OUTPUT;
1261   attributes.visual = gtk_widget_get_visual (widget);
1262   attributes.colormap = gtk_widget_get_colormap (widget);
1263   attributes.event_mask = gtk_widget_get_events (widget);
1264   attributes.event_mask |= (GDK_EXPOSURE_MASK |
1265                             GDK_BUTTON_PRESS_MASK |
1266                             GDK_BUTTON_RELEASE_MASK |
1267                             GDK_BUTTON_MOTION_MASK |
1268                             GDK_ENTER_NOTIFY_MASK |
1269                             GDK_LEAVE_NOTIFY_MASK |
1270                             GDK_KEY_PRESS_MASK);
1271   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1272   
1273   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1274   gdk_window_set_user_data (widget->window, text);
1275   
1276   attributes.x = (widget->style->xthickness + TEXT_BORDER_ROOM);
1277   attributes.y = (widget->style->ythickness + TEXT_BORDER_ROOM);
1278   attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1279   attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1280
1281   attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
1282   attributes_mask |= GDK_WA_CURSOR;
1283   
1284   text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1285   gdk_window_set_user_data (text->text_area, text);
1286
1287   gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1288   
1289   widget->style = gtk_style_attach (widget->style, widget->window);
1290   
1291   /* Can't call gtk_style_set_background here because it's handled specially */
1292   gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1293   gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1294
1295   if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1296     text->bg_gc = create_bg_gc (text);
1297   
1298   text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1299                                                         (gchar*) line_wrap_bits,
1300                                                         line_wrap_width,
1301                                                         line_wrap_height);
1302   
1303   text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1304                                                          (gchar*) line_arrow_bits,
1305                                                          line_arrow_width,
1306                                                          line_arrow_height);
1307   
1308   text->gc = gdk_gc_new (text->text_area);
1309   gdk_gc_set_exposures (text->gc, TRUE);
1310   gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1311   
1312   realize_properties (text);
1313   gdk_window_show (text->text_area);
1314   init_properties (text);
1315
1316   if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1317     gtk_old_editable_claim_selection (old_editable, TRUE, GDK_CURRENT_TIME);
1318   
1319   recompute_geometry (text);
1320 }
1321
1322 static void 
1323 gtk_text_style_set (GtkWidget *widget,
1324                     GtkStyle  *previous_style)
1325 {
1326   GtkText *text = GTK_TEXT (widget);
1327
1328   if (GTK_WIDGET_REALIZED (widget))
1329     {
1330       gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1331       gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1332       
1333       if (text->bg_gc)
1334         {
1335           gdk_gc_destroy (text->bg_gc);
1336           text->bg_gc = NULL;
1337         }
1338
1339       if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1340         text->bg_gc = create_bg_gc (text);
1341
1342       recompute_geometry (text);
1343     }
1344   
1345   if (text->current_font)
1346     text_font_unref (text->current_font);
1347   text->current_font = get_text_font (gtk_style_get_font (widget->style));
1348 }
1349
1350 static void
1351 gtk_text_state_changed (GtkWidget   *widget,
1352                         GtkStateType previous_state)
1353 {
1354   GtkText *text = GTK_TEXT (widget);
1355   
1356   if (GTK_WIDGET_REALIZED (widget))
1357     {
1358       gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1359       gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1360     }
1361 }
1362
1363 static void
1364 gtk_text_unrealize (GtkWidget *widget)
1365 {
1366   GtkText *text;
1367   
1368   g_return_if_fail (GTK_IS_TEXT (widget));
1369   
1370   text = GTK_TEXT (widget);
1371
1372   gdk_window_set_user_data (text->text_area, NULL);
1373   gdk_window_destroy (text->text_area);
1374   text->text_area = NULL;
1375   
1376   gdk_gc_destroy (text->gc);
1377   text->gc = NULL;
1378
1379   if (text->bg_gc)
1380     {
1381       gdk_gc_destroy (text->bg_gc);
1382       text->bg_gc = NULL;
1383     }
1384   
1385   gdk_pixmap_unref (text->line_wrap_bitmap);
1386   gdk_pixmap_unref (text->line_arrow_bitmap);
1387
1388   unrealize_properties (text);
1389
1390   free_cache (text);
1391
1392   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1393     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1394 }
1395
1396 static void
1397 clear_focus_area (GtkText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1398 {
1399   GtkWidget *widget = GTK_WIDGET (text);
1400   GdkGC *gc;
1401  
1402   gint ythick = TEXT_BORDER_ROOM + widget->style->ythickness;
1403   gint xthick = TEXT_BORDER_ROOM + widget->style->xthickness;
1404   
1405   gint width, height;
1406   
1407   if (area_width == 0 || area_height == 0)
1408     return;
1409     
1410   if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1411     {
1412       gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1413   
1414       gdk_gc_set_ts_origin (text->bg_gc,
1415                             (- text->first_onscreen_hor_pixel + xthick) % width,
1416                             (- text->first_onscreen_ver_pixel + ythick) % height);
1417       
1418        gc = text->bg_gc;
1419     }
1420   else
1421     gc = widget->style->base_gc[widget->state];
1422
1423   gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1424                       area_x, area_y, area_width, area_height);
1425 }
1426
1427 static void
1428 gtk_text_draw_focus (GtkWidget *widget)
1429 {
1430   GtkText *text;
1431   gint width, height;
1432   gint x, y;
1433   
1434   g_return_if_fail (GTK_IS_TEXT (widget));
1435   
1436   text = GTK_TEXT (widget);
1437   
1438   if (GTK_WIDGET_DRAWABLE (widget))
1439     {
1440       gint ythick = widget->style->ythickness;
1441       gint xthick = widget->style->xthickness;
1442       gint xextra = TEXT_BORDER_ROOM;
1443       gint yextra = TEXT_BORDER_ROOM;
1444       
1445       TDEBUG (("in gtk_text_draw_focus\n"));
1446       
1447       x = 0;
1448       y = 0;
1449       width = widget->allocation.width;
1450       height = widget->allocation.height;
1451       
1452       if (GTK_WIDGET_HAS_FOCUS (widget))
1453         {
1454           x += 1;
1455           y += 1;
1456           width -=  2;
1457           height -= 2;
1458           xextra -= 1;
1459           yextra -= 1;
1460
1461           gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
1462                            NULL, widget, "text",
1463                            0, 0,
1464                            widget->allocation.width,
1465                            widget->allocation.height);
1466         }
1467
1468       gtk_paint_shadow (widget->style, widget->window,
1469                         GTK_STATE_NORMAL, GTK_SHADOW_IN,
1470                         NULL, widget, "text",
1471                         x, y, width, height);
1472
1473       x += xthick; 
1474       y += ythick;
1475       width -= 2 * xthick;
1476       height -= 2 * ythick;
1477       
1478       /* top rect */
1479       clear_focus_area (text, x, y, width, yextra);
1480       /* left rect */
1481       clear_focus_area (text, x, y + yextra, 
1482                         xextra, y + height - 2 * yextra);
1483       /* right rect */
1484       clear_focus_area (text, x + width - xextra, y + yextra, 
1485                         xextra, height - 2 * ythick);
1486       /* bottom rect */
1487       clear_focus_area (text, x, x + height - yextra, width, yextra);
1488     }
1489   else
1490     {
1491       TDEBUG (("in gtk_text_draw_focus (undrawable !!!)\n"));
1492     }
1493 }
1494
1495 static void
1496 gtk_text_size_request (GtkWidget      *widget,
1497                        GtkRequisition *requisition)
1498 {
1499   GdkFont *font;
1500   gint xthickness;
1501   gint ythickness;
1502   gint char_height;
1503   gint char_width;
1504   
1505   g_return_if_fail (GTK_IS_TEXT (widget));
1506   g_return_if_fail (requisition != NULL);
1507   
1508   xthickness = widget->style->xthickness + TEXT_BORDER_ROOM;
1509   ythickness = widget->style->ythickness + TEXT_BORDER_ROOM;
1510
1511   font = gtk_style_get_font (widget->style);
1512   
1513   char_height = MIN_TEXT_HEIGHT_LINES * (font->ascent +
1514                                          font->descent);
1515   
1516   char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (font,
1517                                                        "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1518                                                        26)
1519                                        / 26);
1520   
1521   requisition->width  = char_width  + xthickness * 2;
1522   requisition->height = char_height + ythickness * 2;
1523 }
1524
1525 static void
1526 gtk_text_size_allocate (GtkWidget     *widget,
1527                         GtkAllocation *allocation)
1528 {
1529   GtkText *text;
1530   
1531   g_return_if_fail (GTK_IS_TEXT (widget));
1532   g_return_if_fail (allocation != NULL);
1533   
1534   text = GTK_TEXT (widget);
1535   
1536   widget->allocation = *allocation;
1537   if (GTK_WIDGET_REALIZED (widget))
1538     {
1539       gdk_window_move_resize (widget->window,
1540                               allocation->x, allocation->y,
1541                               allocation->width, allocation->height);
1542       
1543       gdk_window_move_resize (text->text_area,
1544                               widget->style->xthickness + TEXT_BORDER_ROOM,
1545                               widget->style->ythickness + TEXT_BORDER_ROOM,
1546                               MAX (1, (gint)widget->allocation.width - (gint)(widget->style->xthickness +
1547                                                           (gint)TEXT_BORDER_ROOM) * 2),
1548                               MAX (1, (gint)widget->allocation.height - (gint)(widget->style->ythickness +
1549                                                            (gint)TEXT_BORDER_ROOM) * 2));
1550       
1551       recompute_geometry (text);
1552     }
1553 }
1554
1555 static gint
1556 gtk_text_expose (GtkWidget      *widget,
1557                  GdkEventExpose *event)
1558 {
1559   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1560   g_return_val_if_fail (event != NULL, FALSE);
1561   
1562   if (event->window == GTK_TEXT (widget)->text_area)
1563     {
1564       TDEBUG (("in gtk_text_expose (expose)\n"));
1565       expose_text (GTK_TEXT (widget), &event->area, TRUE);
1566     }
1567   else if (event->count == 0)
1568     {
1569       TDEBUG (("in gtk_text_expose (focus)\n"));
1570       gtk_text_draw_focus (widget);
1571     }
1572   
1573   return FALSE;
1574 }
1575
1576 static gint
1577 gtk_text_scroll_timeout (gpointer data)
1578 {
1579   GtkText *text;
1580   gint x, y;
1581   GdkModifierType mask;
1582   
1583   text = GTK_TEXT (data);
1584   
1585   text->timer = 0;
1586   gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1587   
1588   if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1589     {
1590       GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
1591       
1592       event->motion.is_hint = 0;
1593       event->motion.x = x;
1594       event->motion.y = y;
1595       event->motion.state = mask;
1596       
1597       gtk_text_motion_notify (GTK_WIDGET (text), (GdkEventMotion *)event);
1598
1599       gdk_event_free (event);
1600     }
1601
1602   return FALSE;
1603 }
1604
1605 static gint
1606 gtk_text_button_press (GtkWidget      *widget,
1607                        GdkEventButton *event)
1608 {
1609   GtkText *text;
1610   GtkOldEditable *old_editable;
1611   
1612   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1613   g_return_val_if_fail (event != NULL, FALSE);
1614   
1615   text = GTK_TEXT (widget);
1616   old_editable = GTK_OLD_EDITABLE (widget);
1617   
1618   if (text->button && (event->button != text->button))
1619     return FALSE;
1620   
1621   text->button = event->button;
1622   
1623   if (!GTK_WIDGET_HAS_FOCUS (widget))
1624     gtk_widget_grab_focus (widget);
1625   
1626   if (event->button == 1)
1627     {
1628       switch (event->type)
1629         {
1630         case GDK_BUTTON_PRESS:
1631           gtk_grab_add (widget);
1632           
1633           undraw_cursor (text, FALSE);
1634           find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1635           draw_cursor (text, FALSE);
1636           
1637           /* Set it now, so we display things right. We'll unset it
1638            * later if things don't work out */
1639           old_editable->has_selection = TRUE;
1640           gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1641                                   text->cursor_mark.index,
1642                                   text->cursor_mark.index);
1643           
1644           break;
1645           
1646         case GDK_2BUTTON_PRESS:
1647           gtk_text_select_word (text, event->time);
1648           break;
1649           
1650         case GDK_3BUTTON_PRESS:
1651           gtk_text_select_line (text, event->time);
1652           break;
1653           
1654         default:
1655           break;
1656         }
1657     }
1658   else if (event->type == GDK_BUTTON_PRESS)
1659     {
1660       if ((event->button == 2) && old_editable->editable)
1661         {
1662           if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1663               old_editable->has_selection)
1664             {
1665               undraw_cursor (text, FALSE);
1666               find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1667               draw_cursor (text, FALSE);
1668               
1669             }
1670           
1671           gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1672                                  gdk_atom_intern_static_string ("UTF8_STRING"),
1673                                  event->time);
1674         }
1675       else
1676         {
1677           GdkDisplay *display = gtk_widget_get_display (widget);
1678           
1679           gtk_grab_add (widget);
1680           
1681           undraw_cursor (text, FALSE);
1682           find_mouse_cursor (text, event->x, event->y);
1683           draw_cursor (text, FALSE);
1684           
1685           gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1686                                   text->cursor_mark.index,
1687                                   text->cursor_mark.index);
1688           
1689           old_editable->has_selection = FALSE;
1690           if (gdk_selection_owner_get_for_display (display,
1691                                                    GDK_SELECTION_PRIMARY) == widget->window)
1692             gtk_selection_owner_set_for_display (display,
1693                                                  NULL, 
1694                                                  GDK_SELECTION_PRIMARY,
1695                                                  event->time);
1696         }
1697     }
1698   
1699   return TRUE;
1700 }
1701
1702 static gint
1703 gtk_text_button_release (GtkWidget      *widget,
1704                          GdkEventButton *event)
1705 {
1706   GtkText *text;
1707   GtkOldEditable *old_editable;
1708   GdkDisplay *display;
1709
1710   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1711   g_return_val_if_fail (event != NULL, FALSE);
1712   
1713   text = GTK_TEXT (widget);
1714   
1715   gtk_grab_remove (widget);
1716   
1717   if (text->button != event->button)
1718     return FALSE;
1719   
1720   text->button = 0;
1721   
1722   if (text->timer)
1723     {
1724       g_source_remove (text->timer);
1725       text->timer = 0;
1726     }
1727   
1728   if (event->button == 1)
1729     {
1730       text = GTK_TEXT (widget);
1731       old_editable = GTK_OLD_EDITABLE (widget);
1732       display = gtk_widget_get_display (widget);
1733       
1734       gtk_grab_remove (widget);
1735       
1736       old_editable->has_selection = FALSE;
1737       if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1738         {
1739           if (gtk_selection_owner_set_for_display (display,
1740                                                    widget,
1741                                                    GDK_SELECTION_PRIMARY,
1742                                                    event->time))
1743             old_editable->has_selection = TRUE;
1744           else
1745             gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1746                                   old_editable->selection_end_pos);
1747         }
1748       else
1749         {
1750           if (gdk_selection_owner_get_for_display (display,
1751                                                    GDK_SELECTION_PRIMARY) == widget->window)
1752             gtk_selection_owner_set_for_display (display, 
1753                                                  NULL,
1754                                                  GDK_SELECTION_PRIMARY, 
1755                                                  event->time);
1756         }
1757     }
1758   else if (event->button == 3)
1759     {
1760       gtk_grab_remove (widget);
1761     }
1762   
1763   undraw_cursor (text, FALSE);
1764   find_cursor (text, TRUE);
1765   draw_cursor (text, FALSE);
1766   
1767   return TRUE;
1768 }
1769
1770 static gint
1771 gtk_text_motion_notify (GtkWidget      *widget,
1772                         GdkEventMotion *event)
1773 {
1774   GtkText *text;
1775   gint x, y;
1776   gint height;
1777   GdkModifierType mask;
1778   
1779   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1780   g_return_val_if_fail (event != NULL, FALSE);
1781   
1782   text = GTK_TEXT (widget);
1783   
1784   x = event->x;
1785   y = event->y;
1786   mask = event->state;
1787   if (event->is_hint || (text->text_area != event->window))
1788     {
1789       gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1790     }
1791   
1792   if ((text->button == 0) ||
1793       !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1794     return FALSE;
1795   
1796   gdk_window_get_size (text->text_area, NULL, &height);
1797   
1798   if ((y < 0) || (y > height))
1799     {
1800       if (text->timer == 0)
1801         {
1802           text->timer = gdk_threads_add_timeout (SCROLL_TIME, 
1803                                        gtk_text_scroll_timeout,
1804                                        text);
1805           
1806           if (y < 0)
1807             scroll_int (text, y/2);
1808           else
1809             scroll_int (text, (y - height)/2);
1810         }
1811       else
1812         return FALSE;
1813     }
1814   
1815   undraw_cursor (GTK_TEXT (widget), FALSE);
1816   find_mouse_cursor (GTK_TEXT (widget), x, y);
1817   draw_cursor (GTK_TEXT (widget), FALSE);
1818   
1819   gtk_text_set_selection (GTK_OLD_EDITABLE (text), 
1820                           GTK_OLD_EDITABLE (text)->selection_start_pos,
1821                           text->cursor_mark.index);
1822   
1823   return FALSE;
1824 }
1825
1826 static void 
1827 gtk_text_insert_text    (GtkEditable       *editable,
1828                          const gchar       *new_text,
1829                          gint               new_text_length,
1830                          gint              *position)
1831 {
1832   GtkText *text = GTK_TEXT (editable);
1833   GdkFont *font;
1834   GdkColor *fore, *back;
1835
1836   TextProperty *property;
1837
1838   gtk_text_set_point (text, *position);
1839
1840   property = MARK_CURRENT_PROPERTY (&text->point);
1841   font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL; 
1842   fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL; 
1843   back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL; 
1844   
1845   gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1846
1847   *position = text->point.index;
1848 }
1849
1850 static void 
1851 gtk_text_delete_text    (GtkEditable       *editable,
1852                          gint               start_pos,
1853                          gint               end_pos)
1854 {
1855   GtkText *text;
1856   
1857   g_return_if_fail (start_pos >= 0);
1858   
1859   text = GTK_TEXT (editable);
1860   
1861   gtk_text_set_point (text, start_pos);
1862   if (end_pos < 0)
1863     end_pos = TEXT_LENGTH (text);
1864   
1865   if (end_pos > start_pos)
1866     gtk_text_forward_delete (text, end_pos - start_pos);
1867 }
1868
1869 static gint
1870 gtk_text_key_press (GtkWidget   *widget,
1871                     GdkEventKey *event)
1872 {
1873   GtkText *text;
1874   GtkOldEditable *old_editable;
1875   gchar key;
1876   gint return_val;
1877   gint position;
1878   
1879   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1880   g_return_val_if_fail (event != NULL, FALSE);
1881   
1882   text = GTK_TEXT (widget);
1883   old_editable = GTK_OLD_EDITABLE (widget);
1884   
1885   key = event->keyval;
1886   return_val = TRUE;
1887   
1888   if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1889     {
1890       switch (event->keyval)
1891         {
1892         case GDK_Home:
1893         case GDK_KP_Home:
1894           if (event->state & GDK_CONTROL_MASK)
1895             scroll_int (text, -text->vadj->value);
1896           else
1897             return_val = FALSE;
1898           break;
1899         case GDK_End:
1900         case GDK_KP_End:
1901           if (event->state & GDK_CONTROL_MASK)
1902             scroll_int (text, +text->vadj->upper); 
1903           else
1904             return_val = FALSE;
1905           break;
1906         case GDK_KP_Page_Up:
1907         case GDK_Page_Up:   scroll_int (text, -text->vadj->page_increment); break;
1908         case GDK_KP_Page_Down:
1909         case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1910         case GDK_KP_Up:
1911         case GDK_Up:        scroll_int (text, -KEY_SCROLL_PIXELS); break;
1912         case GDK_KP_Down:
1913         case GDK_Down:      scroll_int (text, +KEY_SCROLL_PIXELS); break;
1914         case GDK_Return:
1915         case GDK_KP_Enter:
1916           if (event->state & GDK_CONTROL_MASK)
1917             gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1918           else
1919             return_val = FALSE;
1920           break;
1921         default:
1922           return_val = FALSE;
1923           break;
1924         }
1925     }
1926   else
1927     {
1928       gint extend_selection;
1929       gint extend_start;
1930       guint initial_pos = old_editable->current_pos;
1931       
1932       text->point = find_mark (text, text->cursor_mark.index);
1933       
1934       extend_selection = event->state & GDK_SHIFT_MASK;
1935       extend_start = FALSE;
1936       
1937       if (extend_selection)
1938         {
1939           old_editable->has_selection = TRUE;
1940           
1941           if (old_editable->selection_start_pos == old_editable->selection_end_pos)
1942             {
1943               old_editable->selection_start_pos = text->point.index;
1944               old_editable->selection_end_pos = text->point.index;
1945             }
1946           
1947           extend_start = (text->point.index == old_editable->selection_start_pos);
1948         }
1949       
1950       switch (event->keyval)
1951         {
1952         case GDK_KP_Home:
1953         case GDK_Home:
1954           if (event->state & GDK_CONTROL_MASK)
1955             move_cursor_buffer_ver (text, -1);
1956           else
1957             gtk_text_move_beginning_of_line (text);
1958           break;
1959         case GDK_KP_End:
1960         case GDK_End:
1961           if (event->state & GDK_CONTROL_MASK)
1962             move_cursor_buffer_ver (text, +1);
1963           else
1964             gtk_text_move_end_of_line (text);
1965           break;
1966         case GDK_KP_Page_Up:
1967         case GDK_Page_Up:   move_cursor_page_ver (text, -1); break;
1968         case GDK_KP_Page_Down:
1969         case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
1970           /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
1971         case GDK_KP_Up:
1972         case GDK_Up:        move_cursor_ver (text, -1); break;
1973         case GDK_KP_Down:
1974         case GDK_Down:      move_cursor_ver (text, +1); break;
1975         case GDK_KP_Left:
1976         case GDK_Left:
1977           if (event->state & GDK_CONTROL_MASK)
1978             gtk_text_move_backward_word (text);
1979           else
1980             move_cursor_hor (text, -1); 
1981           break;
1982         case GDK_KP_Right:
1983         case GDK_Right:     
1984           if (event->state & GDK_CONTROL_MASK)
1985             gtk_text_move_forward_word (text);
1986           else
1987             move_cursor_hor (text, +1); 
1988           break;
1989           
1990         case GDK_BackSpace:
1991           if (event->state & GDK_CONTROL_MASK)
1992             gtk_text_delete_backward_word (text);
1993           else
1994             gtk_text_delete_backward_character (text);
1995           break;
1996         case GDK_Clear:
1997           gtk_text_delete_line (text);
1998           break;
1999         case GDK_KP_Insert:
2000         case GDK_Insert:
2001           if (event->state & GDK_SHIFT_MASK)
2002             {
2003               extend_selection = FALSE;
2004               gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2005             }
2006           else if (event->state & GDK_CONTROL_MASK)
2007             {
2008               gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2009             }
2010           else
2011             {
2012               /* gtk_toggle_insert(text) -- IMPLEMENT */
2013             }
2014           break;
2015         case GDK_Delete:
2016         case GDK_KP_Delete:
2017           if (event->state & GDK_CONTROL_MASK)
2018             gtk_text_delete_forward_word (text);
2019           else if (event->state & GDK_SHIFT_MASK)
2020             {
2021               extend_selection = FALSE;
2022               gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2023             }
2024           else
2025             gtk_text_delete_forward_character (text);
2026           break;
2027         case GDK_Tab:
2028         case GDK_ISO_Left_Tab:
2029         case GDK_KP_Tab:
2030           position = text->point.index;
2031           gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2032           break;
2033         case GDK_KP_Enter:
2034         case GDK_Return:
2035           if (event->state & GDK_CONTROL_MASK)
2036             gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2037           else
2038             {
2039               position = text->point.index;
2040               gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2041             }
2042           break;
2043         case GDK_Escape:
2044           /* Don't insert literally */
2045           return_val = FALSE;
2046           break;
2047           
2048         default:
2049           return_val = FALSE;
2050           
2051           if (event->state & GDK_CONTROL_MASK)
2052             {
2053               return_val = TRUE;
2054               if ((key >= 'A') && (key <= 'Z'))
2055                 key -= 'A' - 'a';
2056
2057               switch (key)
2058                 {
2059                 case 'a':
2060                   gtk_text_move_beginning_of_line (text);
2061                   break;
2062                 case 'b':
2063                   gtk_text_move_backward_character (text);
2064                   break;
2065                 case 'c':
2066                   gtk_editable_copy_clipboard (text);
2067                   break;
2068                 case 'd':
2069                   gtk_text_delete_forward_character (text);
2070                   break;
2071                 case 'e':
2072                   gtk_text_move_end_of_line (text);
2073                   break;
2074                 case 'f':
2075                   gtk_text_move_forward_character (text);
2076                   break;
2077                 case 'h':
2078                   gtk_text_delete_backward_character (text);
2079                   break;
2080                 case 'k':
2081                   gtk_text_delete_to_line_end (text);
2082                   break;
2083                 case 'n':
2084                   gtk_text_move_next_line (text);
2085                   break;
2086                 case 'p':
2087                   gtk_text_move_previous_line (text);
2088                   break;
2089                 case 'u':
2090                   gtk_text_delete_line (text);
2091                   break;
2092                 case 'v':
2093                   gtk_editable_paste_clipboard (text);
2094                   break;
2095                 case 'w':
2096                   gtk_text_delete_backward_word (text);
2097                   break;
2098                 case 'x':
2099                   gtk_editable_cut_clipboard (text);
2100                   break;
2101                 default:
2102                   return_val = FALSE;
2103                 }
2104
2105               break;
2106             }
2107           else if (event->state & GDK_MOD1_MASK)
2108             {
2109               return_val = TRUE;
2110               if ((key >= 'A') && (key <= 'Z'))
2111                 key -= 'A' - 'a';
2112               
2113               switch (key)
2114                 {
2115                 case 'b':
2116                   gtk_text_move_backward_word (text);
2117                   break;
2118                 case 'd':
2119                   gtk_text_delete_forward_word (text);
2120                   break;
2121                 case 'f':
2122                   gtk_text_move_forward_word (text);
2123                   break;
2124                 default:
2125                   return_val = FALSE;
2126                 }
2127               
2128               break;
2129             }
2130           else if (event->length > 0)
2131             {
2132               extend_selection = FALSE;
2133               
2134               gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2135               position = text->point.index;
2136               gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2137               
2138               return_val = TRUE;
2139             }
2140         }
2141       
2142       if (return_val && (old_editable->current_pos != initial_pos))
2143         {
2144           if (extend_selection)
2145             {
2146               if (old_editable->current_pos < old_editable->selection_start_pos)
2147                 gtk_text_set_selection (old_editable, old_editable->current_pos,
2148                                         old_editable->selection_end_pos);
2149               else if (old_editable->current_pos > old_editable->selection_end_pos)
2150                 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2151                                         old_editable->current_pos);
2152               else
2153                 {
2154                   if (extend_start)
2155                     gtk_text_set_selection (old_editable, old_editable->current_pos,
2156                                             old_editable->selection_end_pos);
2157                   else
2158                     gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2159                                             old_editable->current_pos);
2160                 }
2161             }
2162           else
2163             gtk_text_set_selection (old_editable, 0, 0);
2164           
2165           gtk_old_editable_claim_selection (old_editable,
2166                                             old_editable->selection_start_pos != old_editable->selection_end_pos,
2167                                             event->time);
2168         }
2169     }
2170   
2171   return return_val;
2172 }
2173
2174 static void
2175 gtk_text_adjustment (GtkAdjustment *adjustment,
2176                      GtkText       *text)
2177 {
2178   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2179   g_return_if_fail (GTK_IS_TEXT (text));
2180   
2181   /* Just ignore it if we haven't been size-allocated and realized yet */
2182   if (text->line_start_cache == NULL) 
2183     return;
2184   
2185   if (adjustment == text->hadj)
2186     {
2187       g_warning ("horizontal scrolling not implemented");
2188     }
2189   else
2190     {
2191       gint diff = ((gint)adjustment->value) - text->last_ver_value;
2192       
2193       if (diff != 0)
2194         {
2195           undraw_cursor (text, FALSE);
2196           
2197           if (diff > 0)
2198             scroll_down (text, diff);
2199           else /* if (diff < 0) */
2200             scroll_up (text, diff);
2201           
2202           draw_cursor (text, FALSE);
2203           
2204           text->last_ver_value = adjustment->value;
2205         }
2206     }
2207 }
2208
2209 static void
2210 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2211                                GtkText       *text)
2212 {
2213   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2214   g_return_if_fail (GTK_IS_TEXT (text));
2215
2216   if (adjustment == text->hadj)
2217     gtk_text_set_adjustments (text, NULL, text->vadj);
2218   if (adjustment == text->vadj)
2219     gtk_text_set_adjustments (text, text->hadj, NULL);
2220 }
2221
2222
2223 static GtkPropertyMark
2224 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2225 {
2226   GtkPropertyMark mark;
2227   
2228   mark = find_mark_near (text, point_position, near);
2229   
2230   while (mark.index > 0 &&
2231          GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2232     decrement_mark (&mark);
2233   
2234   return mark;
2235 }
2236
2237 static void
2238 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2239 {
2240   tab_cont->pixel_offset          = 0;
2241   tab_cont->tab_start.tab_stops   = text->tab_stops;
2242   tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2243   
2244   if (!tab_cont->tab_start.to_next_tab)
2245     tab_cont->tab_start.to_next_tab = text->default_tab_width;
2246 }
2247
2248 static void
2249 line_params_iterate (GtkText* text,
2250                      const GtkPropertyMark* mark0,
2251                      const PrevTabCont* tab_mark0,
2252                      gint8 alloc,
2253                      void* data,
2254                      LineIteratorFunction iter)
2255      /* mark0 MUST be a real line start.  if ALLOC, allocate line params
2256       * from a mem chunk.  DATA is passed to ITER_CALL, which is called
2257       * for each line following MARK, iteration continues unless ITER_CALL
2258       * returns TRUE. */
2259 {
2260   GtkPropertyMark mark = *mark0;
2261   PrevTabCont  tab_conts[2];
2262   LineParams   *lp, lpbuf;
2263   gint         tab_cont_index = 0;
2264   
2265   if (tab_mark0)
2266     tab_conts[0] = *tab_mark0;
2267   else
2268     init_tab_cont (text, tab_conts);
2269   
2270   for (;;)
2271     {
2272       if (alloc)
2273         lp = g_slice_new (LineParams);
2274       else
2275         lp = &lpbuf;
2276       
2277       *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2278                               tab_conts + (tab_cont_index + 1) % 2);
2279       
2280       if ((*iter) (text, lp, data))
2281         return;
2282       
2283       if (LAST_INDEX (text, lp->end))
2284         break;
2285       
2286       mark = lp->end;
2287       advance_mark (&mark);
2288       tab_cont_index = (tab_cont_index + 1) % 2;
2289     }
2290 }
2291
2292 static gint
2293 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2294 {
2295   FetchLinesData *fldata = (FetchLinesData*) data;
2296   
2297   fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2298   
2299   switch (fldata->fl_type)
2300     {
2301     case FetchLinesCount:
2302       if (!text->line_wrap || !lp->wraps)
2303         fldata->data += 1;
2304       
2305       if (fldata->data >= fldata->data_max)
2306         return TRUE;
2307       
2308       break;
2309     case FetchLinesPixels:
2310       
2311       fldata->data += LINE_HEIGHT(*lp);
2312       
2313       if (fldata->data >= fldata->data_max)
2314         return TRUE;
2315       
2316       break;
2317     }
2318   
2319   return FALSE;
2320 }
2321
2322 static GList*
2323 fetch_lines (GtkText* text,
2324              const GtkPropertyMark* mark0,
2325              const PrevTabCont* tab_cont0,
2326              FLType fl_type,
2327              gint data)
2328 {
2329   FetchLinesData fl_data;
2330   
2331   fl_data.new_lines = NULL;
2332   fl_data.data      = 0;
2333   fl_data.data_max  = data;
2334   fl_data.fl_type   = fl_type;
2335   
2336   line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2337   
2338   return g_list_reverse (fl_data.new_lines);
2339 }
2340
2341 static void
2342 fetch_lines_backward (GtkText* text)
2343 {
2344   GList *new_line_start;
2345   GtkPropertyMark mark;
2346   
2347   if (CACHE_DATA(text->line_start_cache).start.index == 0)
2348     return;
2349   
2350   mark = find_this_line_start_mark (text,
2351                                     CACHE_DATA(text->line_start_cache).start.index - 1,
2352                                     &CACHE_DATA(text->line_start_cache).start);
2353   
2354   new_line_start = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2355   
2356   while (new_line_start->next)
2357     new_line_start = new_line_start->next;
2358   
2359   new_line_start->next = text->line_start_cache;
2360   text->line_start_cache->prev = new_line_start;
2361 }
2362
2363 static void
2364 fetch_lines_forward (GtkText* text, gint line_count)
2365 {
2366   GtkPropertyMark mark;
2367   GList* line = text->line_start_cache;
2368   
2369   while(line->next)
2370     line = line->next;
2371   
2372   mark = CACHE_DATA(line).end;
2373   
2374   if (LAST_INDEX (text, mark))
2375     return;
2376   
2377   advance_mark(&mark);
2378   
2379   line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2380   
2381   if (line->next)
2382     line->next->prev = line;
2383 }
2384
2385 /* Compute the number of lines, and vertical pixels for n characters
2386  * starting from the point 
2387  */
2388 static void
2389 compute_lines_pixels (GtkText* text, guint char_count,
2390                       guint *lines, guint *pixels)
2391 {
2392   GList *line = text->current_line;
2393   gint chars_left = char_count;
2394   
2395   *lines = 0;
2396   *pixels = 0;
2397   
2398   /* If chars_left == 0, that means we're joining two lines in a
2399    * deletion, so add in the values for the next line as well 
2400    */
2401   for (; line && chars_left >= 0; line = line->next)
2402     {
2403       *pixels += LINE_HEIGHT(CACHE_DATA(line));
2404       
2405       if (line == text->current_line)
2406         chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2407       else
2408         chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2409       
2410       if (!text->line_wrap || !CACHE_DATA(line).wraps)
2411         *lines += 1;
2412       else
2413         if (chars_left < 0)
2414           chars_left = 0;       /* force another loop */
2415       
2416       if (!line->next)
2417         fetch_lines_forward (text, 1);
2418     }
2419 }
2420
2421 static gint
2422 total_line_height (GtkText* text, GList* line, gint line_count)
2423 {
2424   gint height = 0;
2425   
2426   for (; line && line_count > 0; line = line->next)
2427     {
2428       height += LINE_HEIGHT(CACHE_DATA(line));
2429       
2430       if (!text->line_wrap || !CACHE_DATA(line).wraps)
2431         line_count -= 1;
2432       
2433       if (!line->next)
2434         fetch_lines_forward (text, line_count);
2435     }
2436   
2437   return height;
2438 }
2439
2440 static void
2441 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2442 {
2443   if (old == text->line_start_cache)
2444     {
2445       GList* last;
2446       
2447       for (; old_line_count > 0; old_line_count -= 1)
2448         {
2449           while (text->line_start_cache &&
2450                  text->line_wrap &&
2451                  CACHE_DATA(text->line_start_cache).wraps)
2452             remove_cache_line(text, text->line_start_cache);
2453           
2454           remove_cache_line(text, text->line_start_cache);
2455         }
2456       
2457       last = g_list_last (new);
2458       
2459       last->next = text->line_start_cache;
2460       
2461       if (text->line_start_cache)
2462         text->line_start_cache->prev = last;
2463       
2464       text->line_start_cache = new;
2465     }
2466   else
2467     {
2468       GList *last;
2469       
2470       g_assert (old->prev);
2471       
2472       last = old->prev;
2473       
2474       for (; old_line_count > 0; old_line_count -= 1)
2475         {
2476           while (old && text->line_wrap && CACHE_DATA(old).wraps)
2477             old = remove_cache_line (text, old);
2478           
2479           old = remove_cache_line (text, old);
2480         }
2481       
2482       last->next = new;
2483       new->prev = last;
2484       
2485       last = g_list_last (new);
2486       
2487       last->next = old;
2488       
2489       if (old)
2490         old->prev = last;
2491     }
2492 }
2493
2494 static void
2495 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2496 {
2497   GList* cache = text->current_line;
2498   gint i;
2499   
2500   for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2501     /* nothing */;
2502   
2503   for (; cache; cache = cache->next)
2504     {
2505       GtkPropertyMark *start = &CACHE_DATA(cache).start;
2506       GtkPropertyMark *end = &CACHE_DATA(cache).end;
2507       
2508       start->index -= nchars;
2509       end->index -= nchars;
2510       
2511       if (LAST_INDEX (text, text->point) &&
2512           start->index == text->point.index)
2513         *start = text->point;
2514       else if (start->property == text->point.property)
2515         start->offset = start->index - (text->point.index - text->point.offset);
2516       
2517       if (LAST_INDEX (text, text->point) &&
2518           end->index == text->point.index)
2519         *end = text->point;
2520       if (end->property == text->point.property)
2521         end->offset = end->index - (text->point.index - text->point.offset);
2522       
2523       /*TEXT_ASSERT_MARK(text, start, "start");*/
2524       /*TEXT_ASSERT_MARK(text, end, "end");*/
2525     }
2526 }
2527
2528 static void
2529 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2530 {
2531   GtkWidget *widget = GTK_WIDGET (text);
2532   
2533   gint pixel_height;
2534   guint new_pixels = 0;
2535   GdkRectangle rect;
2536   GList* new_line = NULL;
2537   gint width, height;
2538   
2539   text->cursor_virtual_x = 0;
2540   
2541   correct_cache_delete (text, nchars, old_lines);
2542   
2543   pixel_height = pixel_height_of(text, text->current_line) -
2544     LINE_HEIGHT(CACHE_DATA(text->current_line));
2545   
2546   if (CACHE_DATA(text->current_line).start.index == text->point.index)
2547     CACHE_DATA(text->current_line).start = text->point;
2548   
2549   new_line = fetch_lines (text,
2550                           &CACHE_DATA(text->current_line).start,
2551                           &CACHE_DATA(text->current_line).tab_cont,
2552                           FetchLinesCount,
2553                           1);
2554   
2555   swap_lines (text, text->current_line, new_line, old_lines);
2556   
2557   text->current_line = new_line;
2558   
2559   new_pixels = total_line_height (text, new_line, 1);
2560   
2561   gdk_window_get_size (text->text_area, &width, &height);
2562   
2563   if (old_pixels != new_pixels)
2564     {
2565       if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2566         {
2567           gdk_draw_pixmap (text->text_area,
2568                            text->gc,
2569                            text->text_area,
2570                            0,
2571                            pixel_height + old_pixels,
2572                            0,
2573                            pixel_height + new_pixels,
2574                            width,
2575                            height);
2576         }
2577       text->vadj->upper += new_pixels;
2578       text->vadj->upper -= old_pixels;
2579       adjust_adj (text, text->vadj);
2580     }
2581   
2582   rect.x = 0;
2583   rect.y = pixel_height;
2584   rect.width = width;
2585   rect.height = new_pixels;
2586   
2587   expose_text (text, &rect, FALSE);
2588   gtk_text_draw_focus ( (GtkWidget *) text);
2589   
2590   text->cursor_mark = text->point;
2591   
2592   find_cursor (text, TRUE);
2593   
2594   if (old_pixels != new_pixels)
2595     {
2596       if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2597         {
2598           rect.x = 0;
2599           rect.y = pixel_height + new_pixels;
2600           rect.width = width;
2601           rect.height = height - rect.y;
2602           
2603           expose_text (text, &rect, FALSE);
2604         }
2605       else
2606         process_exposes (text);
2607     }
2608   
2609   TEXT_ASSERT (text);
2610   TEXT_SHOW(text);
2611 }
2612
2613 /* note, the point has already been moved forward */
2614 static void
2615 correct_cache_insert (GtkText* text, gint nchars)
2616 {
2617   GList *cache;
2618   GtkPropertyMark *start;
2619   GtkPropertyMark *end;
2620   gboolean was_split = FALSE;
2621   
2622   /* We need to distinguish whether the property was split in the
2623    * insert or not, so we check if the point (which points after
2624    * the insertion here), points to the same character as the
2625    * point before. Ugh.
2626    */
2627   if (nchars > 0)
2628     {
2629       GtkPropertyMark tmp_mark = text->point;
2630       move_mark_n (&tmp_mark, -1);
2631       
2632       if (tmp_mark.property != text->point.property)
2633         was_split = TRUE;
2634     }
2635   
2636   /* If we inserted a property exactly at the beginning of the
2637    * line, we have to correct here, or fetch_lines will
2638    * fetch junk.
2639    */
2640   start = &CACHE_DATA(text->current_line).start;
2641
2642   /* Check if if we split exactly at the beginning of the line:
2643    * (was_split won't be set if we are inserting at the end of the text, 
2644    *  so we don't check)
2645    */
2646   if (start->offset ==  MARK_CURRENT_PROPERTY (start)->length)
2647     SET_PROPERTY_MARK (start, start->property->next, 0);
2648   /* Check if we inserted a property at the beginning of the text: */
2649   else if (was_split &&
2650            (start->property == text->point.property) &&
2651            (start->index == text->point.index - nchars))
2652     SET_PROPERTY_MARK (start, start->property->prev, 0);
2653
2654   /* Now correct the offsets, and check for start or end marks that
2655    * are after the point, yet point to a property before the point's
2656    * property. This indicates that they are meant to point to the
2657    * second half of a property we split in insert_text_property(), so
2658    * we fix them up that way.  
2659    */
2660   cache = text->current_line->next;
2661   
2662   for (; cache; cache = cache->next)
2663     {
2664       start = &CACHE_DATA(cache).start;
2665       end = &CACHE_DATA(cache).end;
2666       
2667       if (LAST_INDEX (text, text->point) &&
2668           start->index == text->point.index)
2669         *start = text->point;
2670       else if (start->index >= text->point.index - nchars)
2671         {
2672           if (!was_split && start->property == text->point.property)
2673             move_mark_n(start, nchars);
2674           else
2675             {
2676               if (start->property->next &&
2677                   (start->property->next->next == text->point.property))
2678                 {
2679                   g_assert (start->offset >=  MARK_CURRENT_PROPERTY (start)->length);
2680                   start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2681                   start->property = text->point.property;
2682                 }
2683               start->index += nchars;
2684             }
2685         }
2686       
2687       if (LAST_INDEX (text, text->point) &&
2688           end->index == text->point.index)
2689         *end = text->point;
2690       if (end->index >= text->point.index - nchars)
2691         {
2692           if (!was_split && end->property == text->point.property)
2693             move_mark_n(end, nchars);
2694           else 
2695             {
2696               if (end->property->next &&
2697                   (end->property->next->next == text->point.property))
2698                 {
2699                   g_assert (end->offset >=  MARK_CURRENT_PROPERTY (end)->length);
2700                   end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2701                   end->property = text->point.property;
2702                 }
2703               end->index += nchars;
2704             }
2705         }
2706       
2707       /*TEXT_ASSERT_MARK(text, start, "start");*/
2708       /*TEXT_ASSERT_MARK(text, end, "end");*/
2709     }
2710 }
2711
2712
2713 static void
2714 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2715                guint new_line_count)
2716 {
2717   GtkWidget *widget = GTK_WIDGET (text);
2718   
2719   gint pixel_height;
2720   guint new_pixels = 0;
2721   GdkRectangle rect;
2722   GList* new_lines = NULL;
2723   gint width, height;
2724   
2725   text->cursor_virtual_x = 0;
2726   
2727   undraw_cursor (text, FALSE);
2728   
2729   correct_cache_insert (text, nchars);
2730   
2731   TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2732   
2733   pixel_height = pixel_height_of(text, text->current_line) -
2734     LINE_HEIGHT(CACHE_DATA(text->current_line));
2735   
2736   new_lines = fetch_lines (text,
2737                            &CACHE_DATA(text->current_line).start,
2738                            &CACHE_DATA(text->current_line).tab_cont,
2739                            FetchLinesCount,
2740                            new_line_count);
2741   
2742   swap_lines (text, text->current_line, new_lines, 1);
2743   
2744   text->current_line = new_lines;
2745   
2746   new_pixels = total_line_height (text, new_lines, new_line_count);
2747   
2748   gdk_window_get_size (text->text_area, &width, &height);
2749   
2750   if (old_pixels != new_pixels)
2751     {
2752       if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2753         {
2754           gdk_draw_pixmap (text->text_area,
2755                            text->gc,
2756                            text->text_area,
2757                            0,
2758                            pixel_height + old_pixels,
2759                            0,
2760                            pixel_height + new_pixels,
2761                            width,
2762                            height + (old_pixels - new_pixels) - pixel_height);
2763           
2764         }
2765       text->vadj->upper += new_pixels;
2766       text->vadj->upper -= old_pixels;
2767       adjust_adj (text, text->vadj);
2768     }
2769   
2770   rect.x = 0;
2771   rect.y = pixel_height;
2772   rect.width = width;
2773   rect.height = new_pixels;
2774   
2775   expose_text (text, &rect, FALSE);
2776   gtk_text_draw_focus ( (GtkWidget *) text);
2777   
2778   text->cursor_mark = text->point;
2779   
2780   find_cursor (text, TRUE);
2781   
2782   draw_cursor (text, FALSE);
2783   
2784   if (old_pixels != new_pixels)
2785     {
2786       if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2787         {
2788           rect.x = 0;
2789           rect.y = pixel_height + new_pixels;
2790           rect.width = width;
2791           rect.height = height - rect.y;
2792           
2793           expose_text (text, &rect, FALSE);
2794         }
2795       else
2796         process_exposes (text);
2797     }
2798   
2799   TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2800   TEXT_ASSERT (text);
2801   TEXT_SHOW(text);
2802 }
2803
2804 /* Text property functions */
2805
2806 static guint
2807 font_hash (gconstpointer font)
2808 {
2809   return gdk_font_id ((const GdkFont*) font);
2810 }
2811
2812 static GHashTable *font_cache_table = NULL;
2813
2814 static GtkTextFont*
2815 get_text_font (GdkFont* gfont)
2816 {
2817   GtkTextFont* tf;
2818   gint i;
2819   
2820   if (!font_cache_table)
2821     font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2822   
2823   tf = g_hash_table_lookup (font_cache_table, gfont);
2824   
2825   if (tf)
2826     {
2827       tf->ref_count++;
2828       return tf;
2829     }
2830
2831   tf = g_new (GtkTextFont, 1);
2832   tf->ref_count = 1;
2833
2834   tf->gdk_font = gfont;
2835   gdk_font_ref (gfont);
2836   
2837   for(i = 0; i < 256; i += 1)
2838     tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2839   
2840   g_hash_table_insert (font_cache_table, gfont, tf);
2841   
2842   return tf;
2843 }
2844
2845 static void
2846 text_font_unref (GtkTextFont *text_font)
2847 {
2848   text_font->ref_count--;
2849   if (text_font->ref_count == 0)
2850     {
2851       g_hash_table_remove (font_cache_table, text_font->gdk_font);
2852       gdk_font_unref (text_font->gdk_font);
2853       g_free (text_font);
2854     }
2855 }
2856
2857 static gint
2858 text_properties_equal (TextProperty* prop, GdkFont* font, const GdkColor *fore, const GdkColor *back)
2859 {
2860   if (prop->flags & PROPERTY_FONT)
2861     {
2862       gboolean retval;
2863       GtkTextFont *text_font;
2864
2865       if (!font)
2866         return FALSE;
2867
2868       text_font = get_text_font (font);
2869
2870       retval = (prop->font == text_font);
2871       text_font_unref (text_font);
2872       
2873       if (!retval)
2874         return FALSE;
2875     }
2876   else
2877     if (font != NULL)
2878       return FALSE;
2879
2880   if (prop->flags & PROPERTY_FOREGROUND)
2881     {
2882       if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2883         return FALSE;
2884     }
2885   else
2886     if (fore != NULL)
2887       return FALSE;
2888
2889   if (prop->flags & PROPERTY_BACKGROUND)
2890     {
2891       if (!back || !gdk_color_equal (&prop->back_color, back))
2892         return FALSE;
2893     }
2894   else
2895     if (back != NULL)
2896       return FALSE;
2897   
2898   return TRUE;
2899 }
2900
2901 static void
2902 realize_property (GtkText *text, TextProperty *prop)
2903 {
2904   GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2905
2906   if (prop->flags & PROPERTY_FOREGROUND)
2907     gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2908   
2909   if (prop->flags & PROPERTY_BACKGROUND)
2910     gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2911 }
2912
2913 static void
2914 realize_properties (GtkText *text)
2915 {
2916   GList *tmp_list = text->text_properties;
2917
2918   while (tmp_list)
2919     {
2920       realize_property (text, tmp_list->data);
2921       
2922       tmp_list = tmp_list->next;
2923     }
2924 }
2925
2926 static void
2927 unrealize_property (GtkText *text, TextProperty *prop)
2928 {
2929   GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2930
2931   if (prop->flags & PROPERTY_FOREGROUND)
2932     gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2933   
2934   if (prop->flags & PROPERTY_BACKGROUND)
2935     gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2936 }
2937
2938 static void
2939 unrealize_properties (GtkText *text)
2940 {
2941   GList *tmp_list = text->text_properties;
2942
2943   while (tmp_list)
2944     {
2945       unrealize_property (text, tmp_list->data);
2946
2947       tmp_list = tmp_list->next;
2948     }
2949 }
2950
2951 static TextProperty*
2952 new_text_property (GtkText *text, GdkFont *font, const GdkColor* fore,
2953                    const GdkColor* back, guint length)
2954 {
2955   TextProperty *prop;
2956   
2957   prop = g_slice_new (TextProperty);
2958
2959   prop->flags = 0;
2960   if (font)
2961     {
2962       prop->flags |= PROPERTY_FONT;
2963       prop->font = get_text_font (font);
2964     }
2965   else
2966     prop->font = NULL;
2967   
2968   if (fore)
2969     {
2970       prop->flags |= PROPERTY_FOREGROUND;
2971       prop->fore_color = *fore;
2972     }
2973       
2974   if (back)
2975     {
2976       prop->flags |= PROPERTY_BACKGROUND;
2977       prop->back_color = *back;
2978     }
2979
2980   prop->length = length;
2981
2982   if (GTK_WIDGET_REALIZED (text))
2983     realize_property (text, prop);
2984
2985   return prop;
2986 }
2987
2988 static void
2989 destroy_text_property (TextProperty *prop)
2990 {
2991   if (prop->font)
2992     text_font_unref (prop->font);
2993   
2994   g_slice_free (TextProperty, prop);
2995 }
2996
2997 /* Flop the memory between the point and the gap around like a
2998  * dead fish. */
2999 static void
3000 move_gap (GtkText* text, guint index)
3001 {
3002   if (text->gap_position < index)
3003     {
3004       gint diff = index - text->gap_position;
3005       
3006       if (text->use_wchar)
3007         g_memmove (text->text.wc + text->gap_position,
3008                    text->text.wc + text->gap_position + text->gap_size,
3009                    diff*sizeof (GdkWChar));
3010       else
3011         g_memmove (text->text.ch + text->gap_position,
3012                    text->text.ch + text->gap_position + text->gap_size,
3013                    diff);
3014       
3015       text->gap_position = index;
3016     }
3017   else if (text->gap_position > index)
3018     {
3019       gint diff = text->gap_position - index;
3020       
3021       if (text->use_wchar)
3022         g_memmove (text->text.wc + index + text->gap_size,
3023                    text->text.wc + index,
3024                    diff*sizeof (GdkWChar));
3025       else
3026         g_memmove (text->text.ch + index + text->gap_size,
3027                    text->text.ch + index,
3028                    diff);
3029       
3030       text->gap_position = index;
3031     }
3032 }
3033
3034 /* Increase the gap size. */
3035 static void
3036 make_forward_space (GtkText* text, guint len)
3037 {
3038   if (text->gap_size < len)
3039     {
3040       guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3041       
3042       if (sum >= text->text_len)
3043         {
3044           guint i = 1;
3045           
3046           while (i <= sum) i <<= 1;
3047           
3048           if (text->use_wchar)
3049             text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3050                                                   i*sizeof(GdkWChar));
3051           else
3052             text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3053           text->text_len = i;
3054         }
3055       
3056       if (text->use_wchar)
3057         g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3058                    text->text.wc + text->gap_position + text->gap_size,
3059                    (text->text_end - (text->gap_position + text->gap_size))
3060                    *sizeof(GdkWChar));
3061       else
3062         g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3063                    text->text.ch + text->gap_position + text->gap_size,
3064                    text->text_end - (text->gap_position + text->gap_size));
3065       
3066       text->text_end += len*2;
3067       text->gap_size += len*2;
3068     }
3069 }
3070
3071 /* Inserts into the text property list a list element that guarantees
3072  * that for len characters following the point, text has the correct
3073  * property.  does not move point.  adjusts text_properties_point and
3074  * text_properties_point_offset relative to the current value of
3075  * point. */
3076 static void
3077 insert_text_property (GtkText* text, GdkFont* font,
3078                       const GdkColor *fore, const GdkColor* back, guint len)
3079 {
3080   GtkPropertyMark *mark = &text->point;
3081   TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3082   TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3083   
3084   if (MARK_OFFSET(mark) == 0)
3085     {
3086       /* Point is on the boundary of two properties.
3087        * If it is the same as either, grow, else insert
3088        * a new one. */
3089       
3090       if (text_properties_equal(forward_prop, font, fore, back))
3091         {
3092           /* Grow the property in front of us. */
3093           
3094           MARK_PROPERTY_LENGTH(mark) += len;
3095         }
3096       else if (backward_prop &&
3097                text_properties_equal(backward_prop, font, fore, back))
3098         {
3099           /* Grow property behind us, point property and offset
3100            * change. */
3101           
3102           SET_PROPERTY_MARK (&text->point,
3103                              MARK_PREV_LIST_PTR (mark),
3104                              backward_prop->length);
3105           
3106           backward_prop->length += len;
3107         }
3108       else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3109                (forward_prop->length == 1))
3110         {
3111           /* Next property just has last position, take it over */
3112
3113           if (GTK_WIDGET_REALIZED (text))
3114             unrealize_property (text, forward_prop);
3115
3116           forward_prop->flags = 0;
3117           if (font)
3118             {
3119               forward_prop->flags |= PROPERTY_FONT;
3120               forward_prop->font = get_text_font (font);
3121             }
3122           else
3123             forward_prop->font = NULL;
3124             
3125           if (fore)
3126             {
3127               forward_prop->flags |= PROPERTY_FOREGROUND;
3128               forward_prop->fore_color = *fore;
3129             }
3130           if (back)
3131             {
3132               forward_prop->flags |= PROPERTY_BACKGROUND;
3133               forward_prop->back_color = *back;
3134             }
3135           forward_prop->length += len;
3136
3137           if (GTK_WIDGET_REALIZED (text))
3138             realize_property (text, forward_prop);
3139         }
3140       else
3141         {
3142           /* Splice a new property into the list. */
3143           
3144           GList* new_prop = g_list_alloc();
3145           
3146           new_prop->next = MARK_LIST_PTR(mark);
3147           new_prop->prev = MARK_PREV_LIST_PTR(mark);
3148           new_prop->next->prev = new_prop;
3149           
3150           if (new_prop->prev)
3151             new_prop->prev->next = new_prop;
3152
3153           new_prop->data = new_text_property (text, font, fore, back, len);
3154
3155           SET_PROPERTY_MARK (mark, new_prop, 0);
3156         }
3157     }
3158   else
3159     {
3160       /* The following will screw up the line_start cache,
3161        * we'll fix it up in correct_cache_insert
3162        */
3163       
3164       /* In the middle of forward_prop, if properties are equal,
3165        * just add to its length, else split it into two and splice
3166        * in a new one. */
3167       if (text_properties_equal (forward_prop, font, fore, back))
3168         {
3169           forward_prop->length += len;
3170         }
3171       else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3172                (MARK_OFFSET(mark) + 1 == forward_prop->length))
3173         {
3174           /* Inserting before only the last position in the text */
3175           
3176           GList* new_prop;
3177           forward_prop->length -= 1;
3178           
3179           new_prop = g_list_alloc();
3180           new_prop->data = new_text_property (text, font, fore, back, len+1);
3181           new_prop->prev = MARK_LIST_PTR(mark);
3182           new_prop->next = NULL;
3183           MARK_NEXT_LIST_PTR(mark) = new_prop;
3184           
3185           SET_PROPERTY_MARK (mark, new_prop, 0);
3186         }
3187       else
3188         {
3189           GList* new_prop = g_list_alloc();
3190           GList* new_prop_forward = g_list_alloc();
3191           gint old_length = forward_prop->length;
3192           GList* next = MARK_NEXT_LIST_PTR(mark);
3193           
3194           /* Set the new lengths according to where they are split.  Construct
3195            * two new properties. */
3196           forward_prop->length = MARK_OFFSET(mark);
3197
3198           new_prop_forward->data = 
3199             new_text_property(text,
3200                               forward_prop->flags & PROPERTY_FONT ? 
3201                                      forward_prop->font->gdk_font : NULL,
3202                               forward_prop->flags & PROPERTY_FOREGROUND ? 
3203                                      &forward_prop->fore_color : NULL,
3204                               forward_prop->flags & PROPERTY_BACKGROUND ? 
3205                                      &forward_prop->back_color : NULL,
3206                               old_length - forward_prop->length);
3207
3208           new_prop->data = new_text_property(text, font, fore, back, len);
3209
3210           /* Now splice things in. */
3211           MARK_NEXT_LIST_PTR(mark) = new_prop;
3212           new_prop->prev = MARK_LIST_PTR(mark);
3213           
3214           new_prop->next = new_prop_forward;
3215           new_prop_forward->prev = new_prop;
3216           
3217           new_prop_forward->next = next;
3218           
3219           if (next)
3220             next->prev = new_prop_forward;
3221           
3222           SET_PROPERTY_MARK (mark, new_prop, 0);
3223         }
3224     }
3225   
3226   while (text->text_properties_end->next)
3227     text->text_properties_end = text->text_properties_end->next;
3228   
3229   while (text->text_properties->prev)
3230     text->text_properties = text->text_properties->prev;
3231 }
3232
3233 static void
3234 delete_text_property (GtkText* text, guint nchars)
3235 {
3236   /* Delete nchars forward from point. */
3237   
3238   /* Deleting text properties is problematical, because we
3239    * might be storing around marks pointing to a property.
3240    *
3241    * The marks in question and how we handle them are:
3242    *
3243    *  point: We know the new value, since it will be at the
3244    *         end of the deleted text, and we move it there
3245    *         first.
3246    *  cursor: We just remove the mark and set it equal to the
3247    *         point after the operation.
3248    *  line-start cache: We replace most affected lines.
3249    *         The current line gets used to fetch the new
3250    *         lines so, if necessary, (delete at the beginning
3251    *         of a line) we fix it up by setting it equal to the
3252    *         point.
3253    */
3254   
3255   TextProperty *prop;
3256   GList        *tmp;
3257   gint          is_first;
3258   
3259   for(; nchars; nchars -= 1)
3260     {
3261       prop = MARK_CURRENT_PROPERTY(&text->point);
3262       
3263       prop->length -= 1;
3264       
3265       if (prop->length == 0)
3266         {
3267           tmp = MARK_LIST_PTR (&text->point);
3268           
3269           is_first = tmp == text->text_properties;
3270           
3271           MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3272           text->point.offset = 0;
3273
3274           if (GTK_WIDGET_REALIZED (text))
3275             unrealize_property (text, prop);
3276
3277           destroy_text_property (prop);
3278           g_list_free_1 (tmp);
3279           
3280           prop = MARK_CURRENT_PROPERTY (&text->point);
3281           
3282           if (is_first)
3283             text->text_properties = MARK_LIST_PTR (&text->point);
3284           
3285           g_assert (prop->length != 0);
3286         }
3287       else if (prop->length == text->point.offset)
3288         {
3289           MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3290           text->point.offset = 0;
3291         }
3292     }
3293   
3294   /* Check to see if we have just the single final position remaining
3295    * along in a property; if so, combine it with the previous property
3296    */
3297   if (LAST_INDEX (text, text->point) && 
3298       (MARK_OFFSET (&text->point) == 0) &&
3299       (MARK_PREV_LIST_PTR(&text->point) != NULL))
3300     {
3301       tmp = MARK_LIST_PTR (&text->point);
3302       prop = MARK_CURRENT_PROPERTY(&text->point);
3303       
3304       MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3305       MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3306       MARK_NEXT_LIST_PTR(&text->point) = NULL;
3307       
3308       text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3309       
3310       if (GTK_WIDGET_REALIZED (text))
3311         unrealize_property (text, prop);
3312
3313       destroy_text_property (prop);
3314       g_list_free_1 (tmp);
3315     }
3316 }
3317
3318 static void
3319 init_properties (GtkText *text)
3320 {
3321   if (!text->text_properties)
3322     {
3323       text->text_properties = g_list_alloc();
3324       text->text_properties->next = NULL;
3325       text->text_properties->prev = NULL;
3326       text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3327       text->text_properties_end = text->text_properties;
3328       
3329       SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3330       
3331       text->point.index = 0;
3332     }
3333 }
3334
3335
3336 /**********************************************************************/
3337 /*                         Property Movement                          */
3338 /**********************************************************************/
3339
3340 static void
3341 move_mark_n (GtkPropertyMark* mark, gint n)
3342 {
3343   if (n > 0)
3344     advance_mark_n(mark, n);
3345   else if (n < 0)
3346     decrement_mark_n(mark, -n);
3347 }
3348
3349 static void
3350 advance_mark (GtkPropertyMark* mark)
3351 {
3352   TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3353   
3354   mark->index += 1;
3355   
3356   if (prop->length > mark->offset + 1)
3357     mark->offset += 1;
3358   else
3359     {
3360       mark->property = MARK_NEXT_LIST_PTR (mark);
3361       mark->offset   = 0;
3362     }
3363 }
3364
3365 static void
3366 advance_mark_n (GtkPropertyMark* mark, gint n)
3367 {
3368   gint i;
3369   TextProperty* prop;
3370
3371   g_assert (n > 0);
3372
3373   i = 0;                        /* otherwise it migth not be init. */
3374   prop = MARK_CURRENT_PROPERTY(mark);
3375
3376   if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3377     /* to make it easier */
3378     n += (mark->offset);
3379     mark->index -= mark->offset;
3380     mark->offset = 0;
3381     /* first we take seven-mile-leaps to get to the right text
3382      * property. */
3383     while ((n-i) > prop->length - 1) {
3384       i += prop->length;
3385       mark->index += prop->length;
3386       mark->property = MARK_NEXT_LIST_PTR (mark);
3387       prop = MARK_CURRENT_PROPERTY (mark);
3388     }
3389   }
3390
3391   /* and then the rest */
3392   mark->index += n - i;
3393   mark->offset += n - i;
3394 }
3395
3396 static void
3397 decrement_mark (GtkPropertyMark* mark)
3398 {
3399   mark->index -= 1;
3400   
3401   if (mark->offset > 0)
3402     mark->offset -= 1;
3403   else
3404     {
3405       mark->property = MARK_PREV_LIST_PTR (mark);
3406       mark->offset   = MARK_CURRENT_PROPERTY (mark)->length - 1;
3407     }
3408 }
3409
3410 static void
3411 decrement_mark_n (GtkPropertyMark* mark, gint n)
3412 {
3413   g_assert (n > 0);
3414
3415   while (mark->offset < n) {
3416     /* jump to end of prev */
3417     n -= mark->offset + 1;
3418     mark->index -= mark->offset + 1;
3419     mark->property = MARK_PREV_LIST_PTR (mark);
3420     mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3421   }
3422
3423   /* and the rest */
3424   mark->index -= n;
3425   mark->offset -= n;
3426 }
3427  
3428 static GtkPropertyMark
3429 find_mark (GtkText* text, guint mark_position)
3430 {
3431   return find_mark_near (text, mark_position, &text->point);
3432 }
3433
3434 /*
3435  * You can also start from the end, what a drag.
3436  */
3437 static GtkPropertyMark
3438 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3439 {
3440   gint diffa;
3441   gint diffb;
3442   
3443   GtkPropertyMark mark;
3444   
3445   if (!near)
3446     diffa = mark_position + 1;
3447   else
3448     diffa = mark_position - near->index;
3449   
3450   diffb = mark_position;
3451   
3452   if (diffa < 0)
3453     diffa = -diffa;
3454   
3455   if (diffa <= diffb)
3456     {
3457       mark = *near;
3458     }
3459   else
3460     {
3461       mark.index = 0;
3462       mark.property = text->text_properties;
3463       mark.offset = 0;
3464     }
3465
3466   move_mark_n (&mark, mark_position - mark.index);
3467    
3468   return mark;
3469 }
3470
3471 /* This routine must be called with scroll == FALSE, only when
3472  * point is at least partially on screen
3473  */
3474
3475 static void
3476 find_line_containing_point (GtkText* text, guint point,
3477                             gboolean scroll)
3478 {
3479   GList* cache;
3480   gint height;
3481   
3482   text->current_line = NULL;
3483
3484   TEXT_SHOW (text);
3485
3486   /* Scroll backwards until the point is on screen
3487    */
3488   while (CACHE_DATA(text->line_start_cache).start.index > point)
3489     scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3490
3491   /* Now additionally try to make sure that the point is fully on screen
3492    */
3493   if (scroll)
3494     {
3495       while (text->first_cut_pixels != 0 && 
3496              text->line_start_cache->next &&
3497              CACHE_DATA(text->line_start_cache->next).start.index > point)
3498         scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3499     }
3500
3501   gdk_window_get_size (text->text_area, NULL, &height);
3502   
3503   for (cache = text->line_start_cache; cache; cache = cache->next)
3504     {
3505       guint lph;
3506       
3507       if (CACHE_DATA(cache).end.index >= point ||
3508           LAST_INDEX(text, CACHE_DATA(cache).end))
3509         {
3510           text->current_line = cache; /* LOOK HERE, this proc has an
3511                                        * important side effect. */
3512           return;
3513         }
3514       
3515       TEXT_SHOW_LINE (text, cache, "cache");
3516       
3517       if (cache->next == NULL)
3518         fetch_lines_forward (text, 1);
3519       
3520       if (scroll)
3521         {
3522           lph = pixel_height_of (text, cache->next);
3523           
3524           /* Scroll the bottom of the line is on screen, or until
3525            * the line is the first onscreen line.
3526            */
3527           while (cache->next != text->line_start_cache && lph > height)
3528             {
3529               TEXT_SHOW_LINE (text, cache, "cache");
3530               TEXT_SHOW_LINE (text, cache->next, "cache->next");
3531               scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3532               lph = pixel_height_of (text, cache->next);
3533             }
3534         }
3535     }
3536   
3537   g_assert_not_reached (); /* Must set text->current_line here */
3538 }
3539
3540 static guint
3541 pixel_height_of (GtkText* text, GList* cache_line)
3542 {
3543   gint pixels = - text->first_cut_pixels;
3544   GList *cache = text->line_start_cache;
3545   
3546   while (TRUE) {
3547     pixels += LINE_HEIGHT (CACHE_DATA(cache));
3548     
3549     if (cache->data == cache_line->data)
3550       break;
3551     
3552     cache = cache->next;
3553   }
3554   
3555   return pixels;
3556 }
3557
3558 /**********************************************************************/
3559 /*                      Search and Placement                          */
3560 /**********************************************************************/
3561
3562 static gint
3563 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3564 {
3565   GdkWChar ch;
3566   gint16* char_widths;
3567   
3568   if (LAST_INDEX (text, *mark))
3569     return 0;
3570   
3571   ch = GTK_TEXT_INDEX (text, mark->index);
3572   char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3573
3574   if (ch == '\t')
3575     {
3576       return tab_mark->to_next_tab * char_widths[' '];
3577     }
3578   else if (ch < 256)
3579     {
3580       return char_widths[ch];
3581     }
3582   else
3583     {
3584       return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3585     }
3586 }
3587
3588 static void
3589 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3590 {
3591   if (tab_mark->to_next_tab == 1 || ch == '\t')
3592     {
3593       if (tab_mark->tab_stops->next)
3594         {
3595           tab_mark->tab_stops = tab_mark->tab_stops->next;
3596           tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3597         }
3598       else
3599         {
3600           tab_mark->to_next_tab = text->default_tab_width;
3601         }
3602     }
3603   else
3604     {
3605       tab_mark->to_next_tab -= 1;
3606     }
3607 }
3608
3609 static void
3610 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3611      /* No tabs! */
3612 {
3613   while (n--)
3614     advance_tab_mark (text, tab_mark, 0);
3615 }
3616
3617 static void
3618 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3619 {
3620   GdkWChar ch;
3621   
3622   GtkPropertyMark mark        = start_line->start;
3623   TabStopMark  tab_mark    = start_line->tab_cont.tab_start;
3624   gint         pixel_width = LINE_START_PIXEL (*start_line);
3625   
3626   while (mark.index < text->cursor_mark.index)
3627     {
3628       pixel_width += find_char_width (text, &mark, &tab_mark);
3629       
3630       advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3631       advance_mark (&mark);
3632     }
3633   
3634   text->cursor_pos_x       = pixel_width;
3635   text->cursor_pos_y       = pixel_height;
3636   text->cursor_char_offset = start_line->font_descent;
3637   text->cursor_mark        = mark;
3638   
3639   ch = LAST_INDEX (text, mark) ? 
3640     LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3641   
3642   if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3643     text->cursor_char = 0;
3644   else
3645     text->cursor_char = ch;
3646 }
3647
3648 static void
3649 find_cursor (GtkText* text, gboolean scroll)
3650 {
3651   if (GTK_WIDGET_REALIZED (text))
3652     {
3653       find_line_containing_point (text, text->cursor_mark.index, scroll);
3654       
3655       if (text->current_line)
3656         find_cursor_at_line (text,
3657                              &CACHE_DATA(text->current_line),
3658                              pixel_height_of(text, text->current_line));
3659     }
3660   
3661   GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3662 }
3663
3664 static void
3665 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3666                            guint line_pixel_height,
3667                            gint button_x)
3668 {
3669   GtkPropertyMark mark     = lp->start;
3670   TabStopMark  tab_mark = lp->tab_cont.tab_start;
3671   
3672   gint char_width = find_char_width(text, &mark, &tab_mark);
3673   gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3674   
3675   text->cursor_pos_y = line_pixel_height;
3676   
3677   for (;;)
3678     {
3679       GdkWChar ch = LAST_INDEX (text, mark) ? 
3680         LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3681       
3682       if (button_x < pixel_width || mark.index == lp->end.index)
3683         {
3684           text->cursor_pos_x       = pixel_width - (char_width+1)/2;
3685           text->cursor_mark        = mark;
3686           text->cursor_char_offset = lp->font_descent;
3687           
3688           if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3689             text->cursor_char = 0;
3690           else
3691             text->cursor_char = ch;
3692           
3693           break;
3694         }
3695       
3696       advance_tab_mark (text, &tab_mark, ch);
3697       advance_mark (&mark);
3698       
3699       pixel_width += char_width/2;
3700       
3701       char_width = find_char_width (text, &mark, &tab_mark);
3702       
3703       pixel_width += (char_width+1)/2;
3704     }
3705 }
3706
3707 static void
3708 find_mouse_cursor (GtkText* text, gint x, gint y)
3709 {
3710   gint pixel_height;
3711   GList* cache = text->line_start_cache;
3712   
3713   g_assert (cache);
3714   
3715   pixel_height = - text->first_cut_pixels;
3716   
3717   for (; cache; cache = cache->next)
3718     {
3719       pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3720       
3721       if (y < pixel_height || !cache->next)
3722         {
3723           find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3724           
3725           find_cursor (text, FALSE);
3726           
3727           return;
3728         }
3729     }
3730 }
3731
3732 /**********************************************************************/
3733 /*                          Cache Manager                             */
3734 /**********************************************************************/
3735
3736 static void
3737 free_cache (GtkText* text)
3738 {
3739   GList* cache = text->line_start_cache;
3740   
3741   if (cache)
3742     {
3743       while (cache->prev)
3744         cache = cache->prev;
3745       
3746       text->line_start_cache = cache;
3747     }
3748   
3749   for (; cache; cache = cache->next)
3750     g_slice_free (LineParams, cache->data);
3751   
3752   g_list_free (text->line_start_cache);
3753   
3754   text->line_start_cache = NULL;
3755 }
3756
3757 static GList*
3758 remove_cache_line (GtkText* text, GList* member)
3759 {
3760   GList *list;
3761   
3762   if (member == NULL)
3763     return NULL;
3764   
3765   if (member == text->line_start_cache)
3766     text->line_start_cache = text->line_start_cache->next;
3767   
3768   if (member->prev)
3769     member->prev->next = member->next;
3770   
3771   if (member->next)
3772     member->next->prev = member->prev;
3773   
3774   list = member->next;
3775   
3776   g_slice_free (LineParams, member->data);
3777   g_list_free_1 (member);
3778   
3779   return list;
3780 }
3781
3782 /**********************************************************************/
3783 /*                           Key Motion                               */
3784 /**********************************************************************/
3785
3786 static void
3787 move_cursor_buffer_ver (GtkText *text, int dir)
3788 {
3789   undraw_cursor (text, FALSE);
3790   
3791   if (dir > 0)
3792     {
3793       scroll_int (text, text->vadj->upper);
3794       text->cursor_mark = find_this_line_start_mark (text,
3795                                                      TEXT_LENGTH (text),
3796                                                      &text->cursor_mark);
3797     }
3798   else
3799     {
3800       scroll_int (text, - text->vadj->value);
3801       text->cursor_mark = find_this_line_start_mark (text,
3802                                                      0,
3803                                                      &text->cursor_mark);
3804     }
3805   
3806   find_cursor (text, TRUE);
3807   draw_cursor (text, FALSE);
3808 }
3809
3810 static void
3811 move_cursor_page_ver (GtkText *text, int dir)
3812 {
3813   scroll_int (text, dir * text->vadj->page_increment);
3814 }
3815
3816 static void
3817 move_cursor_ver (GtkText *text, int count)
3818 {
3819   gint i;
3820   GtkPropertyMark mark;
3821   gint offset;
3822   
3823   mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3824   offset = text->cursor_mark.index - mark.index;
3825   
3826   if (offset > text->cursor_virtual_x)
3827     text->cursor_virtual_x = offset;
3828   
3829   if (count < 0)
3830     {
3831       if (mark.index == 0)
3832         return;
3833       
3834       decrement_mark (&mark);
3835       mark = find_this_line_start_mark (text, mark.index, &mark);
3836     }
3837   else
3838     {
3839       mark = text->cursor_mark;
3840       
3841       while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3842         advance_mark (&mark);
3843       
3844       if (LAST_INDEX(text, mark))
3845         return;
3846       
3847       advance_mark (&mark);
3848     }
3849   
3850   for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3851     if (LAST_INDEX(text, mark) ||
3852         GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3853       break;
3854   
3855   undraw_cursor (text, FALSE);
3856   
3857   text->cursor_mark = mark;
3858   
3859   find_cursor (text, TRUE);
3860   
3861   draw_cursor (text, FALSE);
3862 }
3863
3864 static void
3865 move_cursor_hor (GtkText *text, int count)
3866 {
3867   /* count should be +-1. */
3868   if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3869        (count < 0 && text->cursor_mark.index < (- count)) ||
3870        (count == 0) )
3871     return;
3872   
3873   text->cursor_virtual_x = 0;
3874   
3875   undraw_cursor (text, FALSE);
3876   
3877   move_mark_n (&text->cursor_mark, count);
3878   
3879   find_cursor (text, TRUE);
3880   
3881   draw_cursor (text, FALSE);
3882 }
3883
3884 static void 
3885 gtk_text_move_cursor (GtkOldEditable *old_editable,
3886                       gint            x,
3887                       gint            y)
3888 {
3889   if (x > 0)
3890     {
3891       while (x-- != 0)
3892         move_cursor_hor (GTK_TEXT (old_editable), 1);
3893     }
3894   else if (x < 0)
3895     {
3896       while (x++ != 0)
3897         move_cursor_hor (GTK_TEXT (old_editable), -1);
3898     }
3899   
3900   if (y > 0)
3901     {
3902       while (y-- != 0)
3903         move_cursor_ver (GTK_TEXT (old_editable), 1);
3904     }
3905   else if (y < 0)
3906     {
3907       while (y++ != 0)
3908         move_cursor_ver (GTK_TEXT (old_editable), -1);
3909     }
3910 }
3911
3912 static void
3913 gtk_text_move_forward_character (GtkText *text)
3914 {
3915   move_cursor_hor (text, 1);
3916 }
3917
3918 static void
3919 gtk_text_move_backward_character (GtkText *text)
3920 {
3921   move_cursor_hor (text, -1);
3922 }
3923
3924 static void
3925 gtk_text_move_next_line (GtkText *text)
3926 {
3927   move_cursor_ver (text, 1);
3928 }
3929
3930 static void
3931 gtk_text_move_previous_line (GtkText *text)
3932 {
3933   move_cursor_ver (text, -1);
3934 }
3935
3936 static void 
3937 gtk_text_move_word (GtkOldEditable *old_editable,
3938                     gint            n)
3939 {
3940   if (n > 0)
3941     {
3942       while (n-- != 0)
3943         gtk_text_move_forward_word (GTK_TEXT (old_editable));
3944     }
3945   else if (n < 0)
3946     {
3947       while (n++ != 0)
3948         gtk_text_move_backward_word (GTK_TEXT (old_editable));
3949     }
3950 }
3951
3952 static void
3953 gtk_text_move_forward_word (GtkText *text)
3954 {
3955   text->cursor_virtual_x = 0;
3956   
3957   undraw_cursor (text, FALSE);
3958   
3959   if (text->use_wchar)
3960     {
3961       while (!LAST_INDEX (text, text->cursor_mark) && 
3962              !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3963         advance_mark (&text->cursor_mark);
3964       
3965       while (!LAST_INDEX (text, text->cursor_mark) && 
3966              gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3967         advance_mark (&text->cursor_mark);
3968     }
3969   else
3970     {
3971       while (!LAST_INDEX (text, text->cursor_mark) && 
3972              !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3973         advance_mark (&text->cursor_mark);
3974       
3975       while (!LAST_INDEX (text, text->cursor_mark) && 
3976              isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3977         advance_mark (&text->cursor_mark);
3978     }
3979   
3980   find_cursor (text, TRUE);
3981   draw_cursor (text, FALSE);
3982 }
3983
3984 static void
3985 gtk_text_move_backward_word (GtkText *text)
3986 {
3987   text->cursor_virtual_x = 0;
3988   
3989   undraw_cursor (text, FALSE);
3990   
3991   if (text->use_wchar)
3992     {
3993       while ((text->cursor_mark.index > 0) &&
3994              !gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3995         decrement_mark (&text->cursor_mark);
3996       
3997       while ((text->cursor_mark.index > 0) &&
3998              gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
3999         decrement_mark (&text->cursor_mark);
4000     }
4001   else
4002     {
4003       while ((text->cursor_mark.index > 0) &&
4004              !isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4005         decrement_mark (&text->cursor_mark);
4006       
4007       while ((text->cursor_mark.index > 0) &&
4008              isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4009         decrement_mark (&text->cursor_mark);
4010     }
4011   
4012   find_cursor (text, TRUE);
4013   draw_cursor (text, FALSE);
4014 }
4015
4016 static void 
4017 gtk_text_move_page (GtkOldEditable *old_editable,
4018                     gint            x,
4019                     gint            y)
4020 {
4021   if (y != 0)
4022     scroll_int (GTK_TEXT (old_editable), 
4023                 y * GTK_TEXT(old_editable)->vadj->page_increment);  
4024 }
4025
4026 static void 
4027 gtk_text_move_to_row (GtkOldEditable *old_editable,
4028                       gint            row)
4029 {
4030 }
4031
4032 static void 
4033 gtk_text_move_to_column (GtkOldEditable *old_editable,
4034                          gint            column)
4035 {
4036   GtkText *text;
4037   
4038   text = GTK_TEXT (old_editable);
4039   
4040   text->cursor_virtual_x = 0;   /* FIXME */
4041   
4042   undraw_cursor (text, FALSE);
4043   
4044   /* Move to the beginning of the line */
4045   while ((text->cursor_mark.index > 0) &&
4046          (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4047     decrement_mark (&text->cursor_mark);
4048   
4049   while (!LAST_INDEX (text, text->cursor_mark) &&
4050          (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4051     {
4052       if (column > 0)
4053         column--;
4054       else if (column == 0)
4055         break;
4056       
4057       advance_mark (&text->cursor_mark);
4058     }
4059   
4060   find_cursor (text, TRUE);
4061   draw_cursor (text, FALSE);
4062 }
4063
4064 static void
4065 gtk_text_move_beginning_of_line (GtkText *text)
4066 {
4067   gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4068   
4069 }
4070
4071 static void
4072 gtk_text_move_end_of_line (GtkText *text)
4073 {
4074   gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4075 }
4076
4077 static void 
4078 gtk_text_kill_char (GtkOldEditable *old_editable,
4079                     gint            direction)
4080 {
4081   GtkText *text;
4082   
4083   text = GTK_TEXT (old_editable);
4084   
4085   if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4086     gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4087   else
4088     {
4089       if (direction >= 0)
4090         {
4091           if (text->point.index + 1 <= TEXT_LENGTH (text))
4092             gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4093         }
4094       else
4095         {
4096           if (text->point.index > 0)
4097             gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4098         }
4099     }
4100 }
4101
4102 static void
4103 gtk_text_delete_forward_character (GtkText *text)
4104 {
4105   gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4106 }
4107
4108 static void
4109 gtk_text_delete_backward_character (GtkText *text)
4110 {
4111   gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4112 }
4113
4114 static void 
4115 gtk_text_kill_word (GtkOldEditable *old_editable,
4116                     gint            direction)
4117 {
4118   if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4119     gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4120   else
4121     {
4122       gint old_pos = old_editable->current_pos;
4123       if (direction >= 0)
4124         {
4125           gtk_text_move_word (old_editable, 1);
4126           gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4127         }
4128       else
4129         {
4130           gtk_text_move_word (old_editable, -1);
4131           gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4132         }
4133     }
4134 }
4135
4136 static void
4137 gtk_text_delete_forward_word (GtkText *text)
4138 {
4139   gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4140 }
4141
4142 static void
4143 gtk_text_delete_backward_word (GtkText *text)
4144 {
4145   gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4146 }
4147
4148 static void 
4149 gtk_text_kill_line (GtkOldEditable *old_editable,
4150                     gint            direction)
4151 {
4152   gint old_pos = old_editable->current_pos;
4153   if (direction >= 0)
4154     {
4155       gtk_text_move_to_column (old_editable, -1);
4156       gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4157     }
4158   else
4159     {
4160       gtk_text_move_to_column (old_editable, 0);
4161       gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4162     }
4163 }
4164
4165 static void
4166 gtk_text_delete_line (GtkText *text)
4167 {
4168   gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4169   gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4170 }
4171
4172 static void
4173 gtk_text_delete_to_line_end (GtkText *text)
4174 {
4175   gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4176 }
4177
4178 static void
4179 gtk_text_select_word (GtkText *text, guint32 time)
4180 {
4181   gint start_pos;
4182   gint end_pos;
4183   
4184   GtkOldEditable *old_editable;
4185   old_editable = GTK_OLD_EDITABLE (text);
4186   
4187   gtk_text_move_backward_word (text);
4188   start_pos = text->cursor_mark.index;
4189   
4190   gtk_text_move_forward_word (text);
4191   end_pos = text->cursor_mark.index;
4192   
4193   old_editable->has_selection = TRUE;
4194   gtk_text_set_selection (old_editable, start_pos, end_pos);
4195   gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4196 }
4197
4198 static void
4199 gtk_text_select_line (GtkText *text, guint32 time)
4200 {
4201   gint start_pos;
4202   gint end_pos;
4203   
4204   GtkOldEditable *old_editable;
4205   old_editable = GTK_OLD_EDITABLE (text);
4206   
4207   gtk_text_move_beginning_of_line (text);
4208   start_pos = text->cursor_mark.index;
4209   
4210   gtk_text_move_end_of_line (text);
4211   gtk_text_move_forward_character (text);
4212   end_pos = text->cursor_mark.index;
4213   
4214   old_editable->has_selection = TRUE;
4215   gtk_text_set_selection (old_editable, start_pos, end_pos);
4216   gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4217 }
4218
4219 /**********************************************************************/
4220 /*                            Scrolling                               */
4221 /**********************************************************************/
4222
4223 static void
4224 adjust_adj (GtkText* text, GtkAdjustment* adj)
4225 {
4226   gint height;
4227   
4228   gdk_window_get_size (text->text_area, NULL, &height);
4229   
4230   adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4231   adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4232   adj->page_size      = MIN (adj->upper, height);
4233   adj->value          = MIN (adj->value, adj->upper - adj->page_size);
4234   adj->value          = MAX (adj->value, 0.0);
4235   
4236   gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4237 }
4238
4239 static gint
4240 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4241 {
4242   SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4243   
4244   if ((text->first_line_start_index >= lp->start.index) &&
4245       (text->first_line_start_index <= lp->end.index))
4246     {
4247       svdata->mark = lp->start;
4248   
4249       if (text->first_line_start_index == lp->start.index)
4250         {
4251           text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4252         }
4253       else
4254         {
4255           text->first_onscreen_ver_pixel = svdata->pixel_height;
4256           text->first_cut_pixels = 0;
4257         }
4258       
4259       text->vadj->value = text->first_onscreen_ver_pixel;
4260     }
4261   
4262   svdata->pixel_height += LINE_HEIGHT (*lp);
4263   
4264   return FALSE;
4265 }
4266
4267 static gint
4268 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4269 {
4270   SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4271   gint return_val;
4272   
4273   if (svdata->pixel_height <= (gint) text->vadj->value &&
4274       svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4275     {
4276       svdata->mark = lp->start;
4277       
4278       text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4279       text->first_onscreen_ver_pixel = svdata->pixel_height;
4280       text->first_line_start_index = lp->start.index;
4281       
4282       return_val = TRUE;
4283     }
4284   else
4285     {
4286       svdata->pixel_height += LINE_HEIGHT (*lp);
4287       
4288       return_val = FALSE;
4289     }
4290   
4291   return return_val;
4292 }
4293
4294 static GtkPropertyMark
4295 set_vertical_scroll (GtkText* text)
4296 {
4297   GtkPropertyMark mark = find_mark (text, 0);
4298   SetVerticalScrollData data;
4299   gint height;
4300   gint orig_value;
4301   
4302   data.pixel_height = 0;
4303   line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4304   
4305   text->vadj->upper = data.pixel_height;
4306   orig_value = (gint) text->vadj->value;
4307   
4308   gdk_window_get_size (text->text_area, NULL, &height);
4309   
4310   text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4311   text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4312   text->vadj->page_size      = MIN (text->vadj->upper, height);
4313   text->vadj->value          = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4314   text->vadj->value          = MAX (text->vadj->value, 0.0);
4315   
4316   text->last_ver_value = (gint)text->vadj->value;
4317   
4318   gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4319   
4320   if (text->vadj->value != orig_value)
4321     {
4322       /* We got clipped, and don't really know which line to put first. */
4323       data.pixel_height = 0;
4324       data.last_didnt_wrap = TRUE;
4325       
4326       line_params_iterate (text, &mark, NULL,
4327                            FALSE, &data,
4328                            set_vertical_scroll_find_iterator);
4329     }
4330
4331   return data.mark;
4332 }
4333
4334 static void
4335 scroll_int (GtkText* text, gint diff)
4336 {
4337   gdouble upper;
4338   
4339   text->vadj->value += diff;
4340   
4341   upper = text->vadj->upper - text->vadj->page_size;
4342   text->vadj->value = MIN (text->vadj->value, upper);
4343   text->vadj->value = MAX (text->vadj->value, 0.0);
4344   
4345   gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4346 }
4347
4348 static void 
4349 process_exposes (GtkText *text)
4350 {
4351   GdkEvent *event;
4352   
4353   /* Make sure graphics expose events are processed before scrolling
4354    * again */
4355   
4356   while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4357     {
4358       gtk_widget_send_expose (GTK_WIDGET (text), event);
4359       if (event->expose.count == 0)
4360         {
4361           gdk_event_free (event);
4362           break;
4363         }
4364       gdk_event_free (event);
4365     }
4366 }
4367
4368 static gint last_visible_line_height (GtkText* text)
4369 {
4370   GList *cache = text->line_start_cache;
4371   gint height;
4372   
4373   gdk_window_get_size (text->text_area, NULL, &height);
4374   
4375   for (; cache->next; cache = cache->next)
4376     if (pixel_height_of(text, cache->next) > height)
4377       break;
4378   
4379   if (cache)
4380     return pixel_height_of(text, cache) - 1;
4381   else
4382     return 0;
4383 }
4384
4385 static gint first_visible_line_height (GtkText* text)
4386 {
4387   if (text->first_cut_pixels)
4388     return pixel_height_of(text, text->line_start_cache) + 1;
4389   else
4390     return 1;
4391 }
4392
4393 static void
4394 scroll_down (GtkText* text, gint diff0)
4395 {
4396   GdkRectangle rect;
4397   gint real_diff = 0;
4398   gint width, height;
4399   
4400   text->first_onscreen_ver_pixel += diff0;
4401   
4402   while (diff0-- > 0)
4403     {
4404       g_assert (text->line_start_cache);
4405       
4406       if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4407         {
4408           text->first_cut_pixels += 1;
4409         }
4410       else
4411         {
4412           text->first_cut_pixels = 0;
4413           
4414           text->line_start_cache = text->line_start_cache->next;
4415           
4416           text->first_line_start_index =
4417             CACHE_DATA(text->line_start_cache).start.index;
4418           
4419           if (!text->line_start_cache->next)
4420             fetch_lines_forward (text, 1);
4421         }
4422       
4423       real_diff += 1;
4424     }
4425   
4426   gdk_window_get_size (text->text_area, &width, &height);
4427   if (height > real_diff)
4428     gdk_draw_pixmap (text->text_area,
4429                      text->gc,
4430                      text->text_area,
4431                      0,
4432                      real_diff,
4433                      0,
4434                      0,
4435                      width,
4436                      height - real_diff);
4437   
4438   rect.x      = 0;
4439   rect.y      = MAX (0, height - real_diff);
4440   rect.width  = width;
4441   rect.height = MIN (height, real_diff);
4442   
4443   expose_text (text, &rect, FALSE);
4444   gtk_text_draw_focus ( (GtkWidget *) text);
4445   
4446   if (text->current_line)
4447     {
4448       gint cursor_min;
4449       
4450       text->cursor_pos_y -= real_diff;
4451       cursor_min = drawn_cursor_min(text);
4452       
4453       if (cursor_min < 0)
4454         find_mouse_cursor (text, text->cursor_pos_x,
4455                            first_visible_line_height (text));
4456     }
4457   
4458   if (height > real_diff)
4459     process_exposes (text);
4460 }
4461
4462 static void
4463 scroll_up (GtkText* text, gint diff0)
4464 {
4465   gint real_diff = 0;
4466   GdkRectangle rect;
4467   gint width, height;
4468   
4469   text->first_onscreen_ver_pixel += diff0;
4470   
4471   while (diff0++ < 0)
4472     {
4473       g_assert (text->line_start_cache);
4474       
4475       if (text->first_cut_pixels > 0)
4476         {
4477           text->first_cut_pixels -= 1;
4478         }
4479       else
4480         {
4481           if (!text->line_start_cache->prev)
4482             fetch_lines_backward (text);
4483           
4484           text->line_start_cache = text->line_start_cache->prev;
4485           
4486           text->first_line_start_index =
4487             CACHE_DATA(text->line_start_cache).start.index;
4488           
4489           text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4490         }
4491       
4492       real_diff += 1;
4493     }
4494   
4495   gdk_window_get_size (text->text_area, &width, &height);
4496   if (height > real_diff)
4497     gdk_draw_pixmap (text->text_area,
4498                      text->gc,
4499                      text->text_area,
4500                      0,
4501                      0,
4502                      0,
4503                      real_diff,
4504                      width,
4505                      height - real_diff);
4506   
4507   rect.x      = 0;
4508   rect.y      = 0;
4509   rect.width  = width;
4510   rect.height = MIN (height, real_diff);
4511   
4512   expose_text (text, &rect, FALSE);
4513   gtk_text_draw_focus ( (GtkWidget *) text);
4514   
4515   if (text->current_line)
4516     {
4517       gint cursor_max;
4518       gint height;
4519       
4520       text->cursor_pos_y += real_diff;
4521       cursor_max = drawn_cursor_max(text);
4522       gdk_window_get_size (text->text_area, NULL, &height);
4523       
4524       if (cursor_max >= height)
4525         find_mouse_cursor (text, text->cursor_pos_x,
4526                            last_visible_line_height (text));
4527     }
4528   
4529   if (height > real_diff)
4530     process_exposes (text);
4531 }
4532
4533 /**********************************************************************/
4534 /*                            Display Code                            */
4535 /**********************************************************************/
4536
4537 /* Assumes mark starts a line.  Calculates the height, width, and
4538  * displayable character count of a single DISPLAYABLE line.  That
4539  * means that in line-wrap mode, this does may not compute the
4540  * properties of an entire line. */
4541 static LineParams
4542 find_line_params (GtkText* text,
4543                   const GtkPropertyMark* mark,
4544                   const PrevTabCont *tab_cont,
4545                   PrevTabCont *next_cont)
4546 {
4547   LineParams lp;
4548   TabStopMark tab_mark = tab_cont->tab_start;
4549   guint max_display_pixels;
4550   GdkWChar ch;
4551   gint ch_width;
4552   GdkFont *font;
4553   
4554   gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4555   max_display_pixels -= LINE_WRAP_ROOM;
4556   
4557   lp.wraps             = 0;
4558   lp.tab_cont          = *tab_cont;
4559   lp.start             = *mark;
4560   lp.end               = *mark;
4561   lp.pixel_width       = tab_cont->pixel_offset;
4562   lp.displayable_chars = 0;
4563   lp.font_ascent       = 0;
4564   lp.font_descent      = 0;
4565   
4566   init_tab_cont (text, next_cont);
4567   
4568   while (!LAST_INDEX(text, lp.end))
4569     {
4570       g_assert (lp.end.property);
4571       
4572       ch   = GTK_TEXT_INDEX (text, lp.end.index);
4573       font = MARK_CURRENT_FONT (text, &lp.end);
4574
4575       if (ch == LINE_DELIM)
4576         {
4577           /* Newline doesn't count in computation of line height, even
4578            * if its in a bigger font than the rest of the line.  Unless,
4579            * of course, there are no other characters. */
4580           
4581           if (!lp.font_ascent && !lp.font_descent)
4582             {
4583               lp.font_ascent = font->ascent;
4584               lp.font_descent = font->descent;
4585             }
4586           
4587           lp.tab_cont_next = *next_cont;
4588           
4589           return lp;
4590         }
4591       
4592       ch_width = find_char_width (text, &lp.end, &tab_mark);
4593       
4594       if ((ch_width + lp.pixel_width > max_display_pixels) &&
4595           (lp.end.index > lp.start.index))
4596         {
4597           lp.wraps = 1;
4598           
4599           if (text->line_wrap)
4600             {
4601               next_cont->tab_start    = tab_mark;
4602               next_cont->pixel_offset = 0;
4603               
4604               if (ch == '\t')
4605                 {
4606                   /* Here's the tough case, a tab is wrapping. */
4607                   gint pixels_avail = max_display_pixels - lp.pixel_width;
4608                   gint space_width  = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4609                   gint spaces_avail = pixels_avail / space_width;
4610                   
4611                   if (spaces_avail == 0)
4612                     {
4613                       decrement_mark (&lp.end);
4614                     }
4615                   else
4616                     {
4617                       advance_tab_mark (text, &next_cont->tab_start, '\t');
4618                       next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4619                                                                spaces_avail);
4620                       lp.displayable_chars += 1;
4621                     }
4622                 }
4623               else
4624                 {
4625                   if (text->word_wrap)
4626                     {
4627                       GtkPropertyMark saved_mark = lp.end;
4628                       guint saved_characters = lp.displayable_chars;
4629                       
4630                       lp.displayable_chars += 1;
4631                       
4632                       if (text->use_wchar)
4633                         {
4634                           while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4635                                  (lp.end.index > lp.start.index))
4636                             {
4637                               decrement_mark (&lp.end);
4638                               lp.displayable_chars -= 1;
4639                             }
4640                         }
4641                       else
4642                         {
4643                           while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4644                                  (lp.end.index > lp.start.index))
4645                             {
4646                               decrement_mark (&lp.end);
4647                               lp.displayable_chars -= 1;
4648                             }
4649                         }
4650                       
4651                       /* If whole line is one word, revert to char wrapping */
4652                       if (lp.end.index == lp.start.index)
4653                         {
4654                           lp.end = saved_mark;
4655                           lp.displayable_chars = saved_characters;
4656                           decrement_mark (&lp.end);
4657                         }
4658                     }
4659                   else
4660                     {
4661                       /* Don't include this character, it will wrap. */
4662                       decrement_mark (&lp.end);
4663                     }
4664                 }
4665               
4666               lp.tab_cont_next = *next_cont;
4667               
4668               return lp;
4669             }
4670         }
4671       else
4672         {
4673           lp.displayable_chars += 1;
4674         }
4675       
4676       lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4677       lp.font_descent = MAX (font->descent, lp.font_descent);
4678       lp.pixel_width  += ch_width;
4679       
4680       advance_mark(&lp.end);
4681       advance_tab_mark (text, &tab_mark, ch);
4682     }
4683   
4684   if (LAST_INDEX(text, lp.start))
4685     {
4686       /* Special case, empty last line. */
4687       font = MARK_CURRENT_FONT (text, &lp.end);
4688
4689       lp.font_ascent = font->ascent;
4690       lp.font_descent = font->descent;
4691     }
4692   
4693   lp.tab_cont_next = *next_cont;
4694   
4695   return lp;
4696 }
4697
4698 static void
4699 expand_scratch_buffer (GtkText* text, guint len)
4700 {
4701   if (len >= text->scratch_buffer_len)
4702     {
4703       guint i = 1;
4704       
4705       while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4706       
4707       if (text->use_wchar)
4708         {
4709           if (text->scratch_buffer.wc)
4710             text->scratch_buffer.wc = g_new (GdkWChar, i);
4711           else
4712             text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4713                                               i*sizeof (GdkWChar));
4714         }
4715       else
4716         {
4717           if (text->scratch_buffer.ch)
4718             text->scratch_buffer.ch = g_new (guchar, i);
4719           else
4720             text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4721         }
4722       
4723       text->scratch_buffer_len = i;
4724     }
4725 }
4726
4727 /* Side effect: modifies text->gc
4728  */
4729 static void
4730 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4731               gint x, gint y, gint width, gint height,
4732               gboolean already_cleared)
4733 {
4734   GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4735
4736   if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4737        mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4738     {
4739       gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4740                          old_editable->has_selection ?
4741                             GTK_STATE_SELECTED : GTK_STATE_ACTIVE, 
4742                          GTK_SHADOW_NONE,
4743                          NULL, GTK_WIDGET(text), "text",
4744                          x, y, width, height);
4745     }
4746   else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4747                             &GTK_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4748     {
4749       gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4750
4751       gdk_draw_rectangle (text->text_area,
4752                           text->gc,
4753                           TRUE, x, y, width, height);
4754     }
4755   else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4756     {
4757       GdkRectangle rect;
4758       
4759       rect.x = x;
4760       rect.y = y;
4761       rect.width = width;
4762       rect.height = height;
4763       
4764       clear_area (text, &rect);
4765     }
4766   else if (!already_cleared)
4767     gdk_window_clear_area (text->text_area, x, y, width, height);
4768 }
4769
4770 static void
4771 draw_line (GtkText* text,
4772            gint pixel_start_height,
4773            LineParams* lp)
4774 {
4775   GdkGCValues gc_values;
4776   gint i;
4777   gint len = 0;
4778   guint running_offset = lp->tab_cont.pixel_offset;
4779   union { GdkWChar *wc; guchar *ch; } buffer;
4780   GdkGC *fg_gc;
4781   
4782   GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4783   
4784   guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4785   guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4786   
4787   GtkPropertyMark mark = lp->start;
4788   TabStopMark tab_mark = lp->tab_cont.tab_start;
4789   gint pixel_height = pixel_start_height + lp->font_ascent;
4790   guint chars = lp->displayable_chars;
4791   
4792   /* First provide a contiguous segment of memory.  This makes reading
4793    * the code below *much* easier, and only incurs the cost of copying
4794    * when the line being displayed spans the gap. */
4795   if (mark.index <= text->gap_position &&
4796       mark.index + chars > text->gap_position)
4797     {
4798       expand_scratch_buffer (text, chars);
4799       
4800       if (text->use_wchar)
4801         {
4802           for (i = 0; i < chars; i += 1)
4803             text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4804           buffer.wc = text->scratch_buffer.wc;
4805         }
4806       else
4807         {
4808           for (i = 0; i < chars; i += 1)
4809             text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4810           buffer.ch = text->scratch_buffer.ch;
4811         }
4812     }
4813   else
4814     {
4815       if (text->use_wchar)
4816         {
4817           if (mark.index >= text->gap_position)
4818             buffer.wc = text->text.wc + mark.index + text->gap_size;
4819           else
4820             buffer.wc = text->text.wc + mark.index;
4821         }
4822       else
4823         {
4824           if (mark.index >= text->gap_position)
4825             buffer.ch = text->text.ch + mark.index + text->gap_size;
4826           else
4827             buffer.ch = text->text.ch + mark.index;
4828         }
4829     }
4830   
4831   
4832   if (running_offset > 0)
4833     {
4834       draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4835                     LINE_HEIGHT (*lp), TRUE);
4836     }
4837   
4838   while (chars > 0)
4839     {
4840       len = 0;
4841       if ((text->use_wchar && buffer.wc[0] != '\t') ||
4842           (!text->use_wchar && buffer.ch[0] != '\t'))
4843         {
4844           union { GdkWChar *wc; guchar *ch; } next_tab;
4845           gint pixel_width;
4846           GdkFont *font;
4847
4848           next_tab.wc = NULL;
4849           if (text->use_wchar)
4850             for (i=0; i<chars; i++)
4851               {
4852                 if (buffer.wc[i] == '\t')
4853                   {
4854                     next_tab.wc = buffer.wc + i;
4855                     break;
4856                   }
4857               }
4858           else
4859             next_tab.ch = memchr (buffer.ch, '\t', chars);
4860
4861           len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4862           
4863           if (text->use_wchar)
4864             {
4865               if (next_tab.wc)
4866                 len = MIN (len, next_tab.wc - buffer.wc);
4867             }
4868           else
4869             {
4870               if (next_tab.ch)
4871                 len = MIN (len, next_tab.ch - buffer.ch);
4872             }
4873
4874           if (mark.index < selection_start_pos)
4875             len = MIN (len, selection_start_pos - mark.index);
4876           else if (mark.index < selection_end_pos)
4877             len = MIN (len, selection_end_pos - mark.index);
4878
4879           font = MARK_CURRENT_FONT (text, &mark);
4880           if (font->type == GDK_FONT_FONT)
4881             {
4882               gdk_gc_set_font (text->gc, font);
4883               gdk_gc_get_values (text->gc, &gc_values);
4884               if (text->use_wchar)
4885                 pixel_width = gdk_text_width_wc (gc_values.font,
4886                                                  buffer.wc, len);
4887               else
4888               pixel_width = gdk_text_width (gc_values.font,
4889                                               buffer.ch, len);
4890             }
4891           else
4892             {
4893               if (text->use_wchar)
4894                 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4895               else
4896                 pixel_width = gdk_text_width (font, buffer.ch, len);
4897             }
4898           
4899           draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4900                         pixel_width, LINE_HEIGHT (*lp), TRUE);
4901           
4902           if ((mark.index >= selection_start_pos) && 
4903               (mark.index < selection_end_pos))
4904             {
4905               if (old_editable->has_selection)
4906                 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4907               else
4908                 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4909             }
4910           else
4911             {
4912               gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4913               fg_gc = text->gc;
4914             }
4915
4916           if (text->use_wchar)
4917             gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4918                               fg_gc,
4919                               running_offset,
4920                               pixel_height,
4921                               buffer.wc,
4922                               len);
4923           else
4924             gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4925                            fg_gc,
4926                            running_offset,
4927                            pixel_height,
4928                            buffer.ch,
4929                            len);
4930           
4931           running_offset += pixel_width;
4932           
4933           advance_tab_mark_n (text, &tab_mark, len);
4934         }
4935       else
4936         {
4937           gint pixels_remaining;
4938           gint space_width;
4939           gint spaces_avail;
4940               
4941           len = 1;
4942           
4943           gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4944           pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4945           
4946           space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4947           
4948           spaces_avail = pixels_remaining / space_width;
4949           spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4950
4951           draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4952                         spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4953
4954           running_offset += tab_mark.to_next_tab *
4955             MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4956
4957           advance_tab_mark (text, &tab_mark, '\t');
4958         }
4959       
4960       advance_mark_n (&mark, len);
4961       if (text->use_wchar)
4962         buffer.wc += len;
4963       else
4964         buffer.ch += len;
4965       chars -= len;
4966     }
4967 }
4968
4969 static void
4970 draw_line_wrap (GtkText* text, guint height /* baseline height */)
4971 {
4972   gint width;
4973   GdkPixmap *bitmap;
4974   gint bitmap_width;
4975   gint bitmap_height;
4976   
4977   if (text->line_wrap)
4978     {
4979       bitmap = text->line_wrap_bitmap;
4980       bitmap_width = line_wrap_width;
4981       bitmap_height = line_wrap_height;
4982     }
4983   else
4984     {
4985       bitmap = text->line_arrow_bitmap;
4986       bitmap_width = line_arrow_width;
4987       bitmap_height = line_arrow_height;
4988     }
4989   
4990   gdk_window_get_size (text->text_area, &width, NULL);
4991   width -= LINE_WRAP_ROOM;
4992   
4993   gdk_gc_set_stipple (text->gc,
4994                       bitmap);
4995   
4996   gdk_gc_set_fill (text->gc, GDK_STIPPLED);
4997   
4998   gdk_gc_set_foreground (text->gc, &GTK_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
4999   
5000   gdk_gc_set_ts_origin (text->gc,
5001                         width + 1,
5002                         height - bitmap_height - 1);
5003   
5004   gdk_draw_rectangle (text->text_area,
5005                       text->gc,
5006                       TRUE,
5007                       width + 1,
5008                       height - bitmap_height - 1 /* one pixel above the baseline. */,
5009                       bitmap_width,
5010                       bitmap_height);
5011   
5012   gdk_gc_set_ts_origin (text->gc, 0, 0);
5013   
5014   gdk_gc_set_fill (text->gc, GDK_SOLID);
5015 }
5016
5017 static void
5018 undraw_cursor (GtkText* text, gint absolute)
5019 {
5020   GtkOldEditable *old_editable = (GtkOldEditable *) text;
5021
5022   TDEBUG (("in undraw_cursor\n"));
5023   
5024   if (absolute)
5025     text->cursor_drawn_level = 0;
5026   
5027   if ((text->cursor_drawn_level ++ == 0) &&
5028       (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5029       GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5030     {
5031       GdkFont* font;
5032       
5033       g_assert(text->cursor_mark.property);
5034
5035       font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5036
5037       draw_bg_rect (text, &text->cursor_mark, 
5038                     text->cursor_pos_x,
5039                     text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5040                     1, font->ascent + 1, FALSE);
5041       
5042       if (text->cursor_char)
5043         {
5044           if (font->type == GDK_FONT_FONT)
5045             gdk_gc_set_font (text->gc, font);
5046
5047           gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5048
5049           gdk_draw_text_wc (text->text_area, font,
5050                          text->gc,
5051                          text->cursor_pos_x,
5052                          text->cursor_pos_y - text->cursor_char_offset,
5053                          &text->cursor_char,
5054                          1);
5055         }
5056     }
5057 }
5058
5059 static gint
5060 drawn_cursor_min (GtkText* text)
5061 {
5062   GdkFont* font;
5063   
5064   g_assert(text->cursor_mark.property);
5065   
5066   font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5067   
5068   return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5069 }
5070
5071 static gint
5072 drawn_cursor_max (GtkText* text)
5073 {
5074   g_assert(text->cursor_mark.property);
5075   
5076   return text->cursor_pos_y - text->cursor_char_offset;
5077 }
5078
5079 static void
5080 draw_cursor (GtkText* text, gint absolute)
5081 {
5082   GtkOldEditable *old_editable = (GtkOldEditable *)text;
5083   
5084   TDEBUG (("in draw_cursor\n"));
5085   
5086   if (absolute)
5087     text->cursor_drawn_level = 1;
5088   
5089   if ((--text->cursor_drawn_level == 0) &&
5090       old_editable->editable &&
5091       (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5092       GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5093     {
5094       GdkFont* font;
5095       
5096       g_assert (text->cursor_mark.property);
5097
5098       font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5099
5100       gdk_gc_set_foreground (text->gc, &GTK_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5101       
5102       gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5103                      text->cursor_pos_y - text->cursor_char_offset,
5104                      text->cursor_pos_x,
5105                      text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5106     }
5107 }
5108
5109 static GdkGC *
5110 create_bg_gc (GtkText *text)
5111 {
5112   GdkGCValues values;
5113   
5114   values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5115   values.fill = GDK_TILED;
5116
5117   return gdk_gc_new_with_values (text->text_area, &values,
5118                                  GDK_GC_FILL | GDK_GC_TILE);
5119 }
5120
5121 static void
5122 clear_area (GtkText *text, GdkRectangle *area)
5123 {
5124   GtkWidget *widget = GTK_WIDGET (text);
5125   
5126   if (text->bg_gc)
5127     {
5128       gint width, height;
5129       
5130       gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5131       
5132       gdk_gc_set_ts_origin (text->bg_gc,
5133                             (- text->first_onscreen_hor_pixel) % width,
5134                             (- text->first_onscreen_ver_pixel) % height);
5135
5136       gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5137                           area->x, area->y, area->width, area->height);
5138     }
5139   else
5140     gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5141 }
5142
5143 static void
5144 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5145 {
5146   GList *cache = text->line_start_cache;
5147   gint pixels = - text->first_cut_pixels;
5148   gint min_y = MAX (0, area->y);
5149   gint max_y = MAX (0, area->y + area->height);
5150   gint height;
5151   
5152   gdk_window_get_size (text->text_area, NULL, &height);
5153   max_y = MIN (max_y, height);
5154   
5155   TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5156   
5157   clear_area (text, area);
5158   
5159   for (; pixels < height; cache = cache->next)
5160     {
5161       if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5162         {
5163           draw_line (text, pixels, &CACHE_DATA(cache));
5164           
5165           if (CACHE_DATA(cache).wraps)
5166             draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5167         }
5168       
5169       if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5170         {
5171           if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5172               CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5173             {
5174               /* We undraw and draw the cursor here to get the drawn
5175                * level right ... FIXME - maybe the second parameter
5176                * of draw_cursor should work differently
5177                */
5178               undraw_cursor (text, FALSE);
5179               draw_cursor (text, FALSE);
5180             }
5181         }
5182       
5183       pixels += LINE_HEIGHT(CACHE_DATA(cache));
5184       
5185       if (!cache->next)
5186         {
5187           fetch_lines_forward (text, 1);
5188           
5189           if (!cache->next)
5190             break;
5191         }
5192     }
5193 }
5194
5195 static void 
5196 gtk_text_update_text (GtkOldEditable    *old_editable,
5197                       gint               start_pos,
5198                       gint               end_pos)
5199 {
5200   GtkText *text = GTK_TEXT (old_editable);
5201   
5202   GList *cache = text->line_start_cache;
5203   gint pixels = - text->first_cut_pixels;
5204   GdkRectangle area;
5205   gint width;
5206   gint height;
5207   
5208   if (end_pos < 0)
5209     end_pos = TEXT_LENGTH (text);
5210   
5211   if (end_pos < start_pos)
5212     return;
5213   
5214   gdk_window_get_size (text->text_area, &width, &height);
5215   area.x = 0;
5216   area.y = -1;
5217   area.width = width;
5218   area.height = 0;
5219   
5220   TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5221   
5222   for (; pixels < height; cache = cache->next)
5223     {
5224       if (CACHE_DATA(cache).start.index < end_pos)
5225         {
5226           if (CACHE_DATA(cache).end.index >= start_pos)
5227             {
5228               if (area.y < 0)
5229                 area.y = MAX(0,pixels);
5230               area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5231             }
5232         }
5233       else
5234         break;
5235       
5236       pixels += LINE_HEIGHT(CACHE_DATA(cache));
5237       
5238       if (!cache->next)
5239         {
5240           fetch_lines_forward (text, 1);
5241           
5242           if (!cache->next)
5243             break;
5244         }
5245     }
5246   
5247   if (area.y >= 0)
5248     expose_text (text, &area, TRUE);
5249 }
5250
5251 static void
5252 recompute_geometry (GtkText* text)
5253 {
5254   GtkPropertyMark mark, start_mark;
5255   GList *new_lines;
5256   gint height;
5257   gint width;
5258   
5259   free_cache (text);
5260   
5261   mark = start_mark = set_vertical_scroll (text);
5262
5263   /* We need a real start of a line when calling fetch_lines().
5264    * not the start of a wrapped line.
5265    */
5266   while (mark.index > 0 &&
5267          GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5268     decrement_mark (&mark);
5269
5270   gdk_window_get_size (text->text_area, &width, &height);
5271
5272   /* Fetch an entire line, to make sure that we get all the text
5273    * we backed over above, in addition to enough text to fill up
5274    * the space vertically
5275    */
5276
5277   new_lines = fetch_lines (text,
5278                            &mark,
5279                            NULL,
5280                            FetchLinesCount,
5281                            1);
5282
5283   mark = CACHE_DATA (g_list_last (new_lines)).end;
5284   if (!LAST_INDEX (text, mark))
5285     {
5286       advance_mark (&mark);
5287
5288       new_lines = g_list_concat (new_lines, 
5289                                  fetch_lines (text,
5290                                               &mark,
5291                                               NULL,
5292                                               FetchLinesPixels,
5293                                               height + text->first_cut_pixels));
5294     }
5295
5296   /* Now work forward to the actual first onscreen line */
5297
5298   while (CACHE_DATA (new_lines).start.index < start_mark.index)
5299     new_lines = new_lines->next;
5300   
5301   text->line_start_cache = new_lines;
5302   
5303   find_cursor (text, TRUE);
5304 }
5305
5306 /**********************************************************************/
5307 /*                            Selection                               */
5308 /**********************************************************************/
5309
5310 static void 
5311 gtk_text_set_selection  (GtkOldEditable  *old_editable,
5312                          gint             start,
5313                          gint             end)
5314 {
5315   GtkText *text = GTK_TEXT (old_editable);
5316   
5317   guint start1, end1, start2, end2;
5318   
5319   if (end < 0)
5320     end = TEXT_LENGTH (text);
5321   
5322   start1 = MIN(start,end);
5323   end1 = MAX(start,end);
5324   start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5325   end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5326   
5327   if (start2 < start1)
5328     {
5329       guint tmp;
5330       
5331       tmp = start1; start1 = start2; start2 = tmp;
5332       tmp = end1;   end1   = end2;   end2   = tmp;
5333     }
5334   
5335   undraw_cursor (text, FALSE);
5336   old_editable->selection_start_pos = start;
5337   old_editable->selection_end_pos = end;
5338   draw_cursor (text, FALSE);
5339   
5340   /* Expose only what changed */
5341   
5342   if (start1 < start2)
5343     gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5344   
5345   if (end2 > end1)
5346     gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5347   else if (end2 < end1)
5348     gtk_text_update_text (old_editable, end2, end1);
5349 }
5350
5351
5352 /**********************************************************************/
5353 /*                              Debug                                 */
5354 /**********************************************************************/
5355
5356 #ifdef DEBUG_GTK_TEXT
5357 static void
5358 gtk_text_show_cache_line (GtkText *text, GList *cache,
5359                           const char* what, const char* func, gint line)
5360 {
5361   LineParams *lp = &CACHE_DATA(cache);
5362   gint i;
5363   
5364   if (cache == text->line_start_cache)
5365     g_message ("Line Start Cache: ");
5366   
5367   if (cache == text->current_line)
5368     g_message("Current Line: ");
5369   
5370   g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5371              func,
5372              line,
5373              what,
5374              lp->start.index,
5375              lp->end.index,
5376              LINE_HEIGHT(*lp));
5377   
5378   for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5379     g_message ("%c", GTK_TEXT_INDEX (text, i));
5380   
5381   g_message (")\n");
5382 }
5383
5384 static void
5385 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5386 {
5387   GList *l = text->line_start_cache;
5388   
5389   if (!l) {
5390     return;
5391   }
5392   
5393   /* back up to the absolute beginning of the line cache */
5394   while (l->prev)
5395     l = l->prev;
5396   
5397   g_message ("*** line cache ***\n");
5398   for (; l; l = l->next)
5399     gtk_text_show_cache_line (text, l, "all", func, line);
5400 }
5401
5402 static void
5403 gtk_text_assert_mark (GtkText         *text,
5404                       GtkPropertyMark *mark,
5405                       GtkPropertyMark *before,
5406                       GtkPropertyMark *after,
5407                       const gchar     *msg,
5408                       const gchar     *where,
5409                       gint             line)
5410 {
5411   GtkPropertyMark correct_mark = find_mark (text, mark->index);
5412   
5413   if (mark->offset != correct_mark.offset ||
5414       mark->property != correct_mark.property)
5415     g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5416 }
5417
5418 static void
5419 gtk_text_assert (GtkText         *text,
5420                  const gchar     *msg,
5421                  gint             line)
5422 {
5423   GList* cache = text->line_start_cache;
5424   GtkPropertyMark* before_mark = NULL;
5425   GtkPropertyMark* after_mark = NULL;
5426   
5427   gtk_text_show_props (text, msg, line);
5428   
5429   for (; cache->prev; cache = cache->prev)
5430     /* nothing */;
5431   
5432   g_message ("*** line markers ***\n");
5433   
5434   for (; cache; cache = cache->next)
5435     {
5436       after_mark = &CACHE_DATA(cache).end;
5437       gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5438       before_mark = &CACHE_DATA(cache).start;
5439       
5440       if (cache->next)
5441         after_mark = &CACHE_DATA(cache->next).start;
5442       else
5443         after_mark = NULL;
5444       
5445       gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5446       before_mark = &CACHE_DATA(cache).end;
5447     }
5448 }
5449
5450 static void
5451 gtk_text_show_adj (GtkText *text,
5452                    GtkAdjustment *adj,
5453                    const char* what,
5454                    const char* func,
5455                    gint line)
5456 {
5457   g_message ("*** adjustment ***\n");
5458   
5459   g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5460              func,
5461              line,
5462              what,
5463              adj->lower,
5464              adj->upper,
5465              adj->value,
5466              adj->step_increment,
5467              adj->page_increment,
5468              adj->page_size);
5469 }
5470
5471 static void
5472 gtk_text_show_props (GtkText *text,
5473                      const char* msg,
5474                      int line)
5475 {
5476   GList* props = text->text_properties;
5477   int proplen = 0;
5478   
5479   g_message ("%s:%d: ", msg, line);
5480   
5481   for (; props; props = props->next)
5482     {
5483       TextProperty *p = (TextProperty*)props->data;
5484       
5485       proplen += p->length;
5486
5487       g_message ("[%d,%p,", p->length, p);
5488       if (p->flags & PROPERTY_FONT)
5489         g_message ("%p,", p->font);
5490       else
5491         g_message ("-,");
5492       if (p->flags & PROPERTY_FOREGROUND)
5493         g_message ("%ld, ", p->fore_color.pixel);
5494       else
5495         g_message ("-,");
5496       if (p->flags & PROPERTY_BACKGROUND)
5497         g_message ("%ld] ", p->back_color.pixel);
5498       else
5499         g_message ("-] ");
5500     }
5501   
5502   g_message ("\n");
5503   
5504   if (proplen - 1 != TEXT_LENGTH(text))
5505     g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);
5506 }
5507 #endif
5508
5509 #define __GTK_TEXT_C__
5510 #include "gtkaliasdef.c"