]> Pileus Git - ~andy/gtk/blob - gtk/gtktext.c
Updated Bulgarian translation by Alexander Shopov <ash@contact.bg>
[~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   GDK_THREADS_ENTER ();
1584
1585   text = GTK_TEXT (data);
1586   
1587   text->timer = 0;
1588   gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1589   
1590   if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1591     {
1592       GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
1593       
1594       event->motion.is_hint = 0;
1595       event->motion.x = x;
1596       event->motion.y = y;
1597       event->motion.state = mask;
1598       
1599       gtk_text_motion_notify (GTK_WIDGET (text), (GdkEventMotion *)event);
1600
1601       gdk_event_free (event);
1602     }
1603
1604   GDK_THREADS_LEAVE ();
1605   
1606   return FALSE;
1607 }
1608
1609 static gint
1610 gtk_text_button_press (GtkWidget      *widget,
1611                        GdkEventButton *event)
1612 {
1613   GtkText *text;
1614   GtkOldEditable *old_editable;
1615   
1616   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1617   g_return_val_if_fail (event != NULL, FALSE);
1618   
1619   text = GTK_TEXT (widget);
1620   old_editable = GTK_OLD_EDITABLE (widget);
1621   
1622   if (text->button && (event->button != text->button))
1623     return FALSE;
1624   
1625   text->button = event->button;
1626   
1627   if (!GTK_WIDGET_HAS_FOCUS (widget))
1628     gtk_widget_grab_focus (widget);
1629   
1630   if (event->button == 1)
1631     {
1632       switch (event->type)
1633         {
1634         case GDK_BUTTON_PRESS:
1635           gtk_grab_add (widget);
1636           
1637           undraw_cursor (text, FALSE);
1638           find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1639           draw_cursor (text, FALSE);
1640           
1641           /* Set it now, so we display things right. We'll unset it
1642            * later if things don't work out */
1643           old_editable->has_selection = TRUE;
1644           gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1645                                   text->cursor_mark.index,
1646                                   text->cursor_mark.index);
1647           
1648           break;
1649           
1650         case GDK_2BUTTON_PRESS:
1651           gtk_text_select_word (text, event->time);
1652           break;
1653           
1654         case GDK_3BUTTON_PRESS:
1655           gtk_text_select_line (text, event->time);
1656           break;
1657           
1658         default:
1659           break;
1660         }
1661     }
1662   else if (event->type == GDK_BUTTON_PRESS)
1663     {
1664       if ((event->button == 2) && old_editable->editable)
1665         {
1666           if (old_editable->selection_start_pos == old_editable->selection_end_pos ||
1667               old_editable->has_selection)
1668             {
1669               undraw_cursor (text, FALSE);
1670               find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1671               draw_cursor (text, FALSE);
1672               
1673             }
1674           
1675           gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1676                                  gdk_atom_intern_static_string ("UTF8_STRING"),
1677                                  event->time);
1678         }
1679       else
1680         {
1681           GdkDisplay *display = gtk_widget_get_display (widget);
1682           
1683           gtk_grab_add (widget);
1684           
1685           undraw_cursor (text, FALSE);
1686           find_mouse_cursor (text, event->x, event->y);
1687           draw_cursor (text, FALSE);
1688           
1689           gtk_text_set_selection (GTK_OLD_EDITABLE (text),
1690                                   text->cursor_mark.index,
1691                                   text->cursor_mark.index);
1692           
1693           old_editable->has_selection = FALSE;
1694           if (gdk_selection_owner_get_for_display (display,
1695                                                    GDK_SELECTION_PRIMARY) == widget->window)
1696             gtk_selection_owner_set_for_display (display,
1697                                                  NULL, 
1698                                                  GDK_SELECTION_PRIMARY,
1699                                                  event->time);
1700         }
1701     }
1702   
1703   return TRUE;
1704 }
1705
1706 static gint
1707 gtk_text_button_release (GtkWidget      *widget,
1708                          GdkEventButton *event)
1709 {
1710   GtkText *text;
1711   GtkOldEditable *old_editable;
1712   GdkDisplay *display;
1713
1714   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1715   g_return_val_if_fail (event != NULL, FALSE);
1716   
1717   text = GTK_TEXT (widget);
1718   
1719   gtk_grab_remove (widget);
1720   
1721   if (text->button != event->button)
1722     return FALSE;
1723   
1724   text->button = 0;
1725   
1726   if (text->timer)
1727     {
1728       g_source_remove (text->timer);
1729       text->timer = 0;
1730     }
1731   
1732   if (event->button == 1)
1733     {
1734       text = GTK_TEXT (widget);
1735       old_editable = GTK_OLD_EDITABLE (widget);
1736       display = gtk_widget_get_display (widget);
1737       
1738       gtk_grab_remove (widget);
1739       
1740       old_editable->has_selection = FALSE;
1741       if (old_editable->selection_start_pos != old_editable->selection_end_pos)
1742         {
1743           if (gtk_selection_owner_set_for_display (display,
1744                                                    widget,
1745                                                    GDK_SELECTION_PRIMARY,
1746                                                    event->time))
1747             old_editable->has_selection = TRUE;
1748           else
1749             gtk_text_update_text (old_editable, old_editable->selection_start_pos,
1750                                   old_editable->selection_end_pos);
1751         }
1752       else
1753         {
1754           if (gdk_selection_owner_get_for_display (display,
1755                                                    GDK_SELECTION_PRIMARY) == widget->window)
1756             gtk_selection_owner_set_for_display (display, 
1757                                                  NULL,
1758                                                  GDK_SELECTION_PRIMARY, 
1759                                                  event->time);
1760         }
1761     }
1762   else if (event->button == 3)
1763     {
1764       gtk_grab_remove (widget);
1765     }
1766   
1767   undraw_cursor (text, FALSE);
1768   find_cursor (text, TRUE);
1769   draw_cursor (text, FALSE);
1770   
1771   return TRUE;
1772 }
1773
1774 static gint
1775 gtk_text_motion_notify (GtkWidget      *widget,
1776                         GdkEventMotion *event)
1777 {
1778   GtkText *text;
1779   gint x, y;
1780   gint height;
1781   GdkModifierType mask;
1782   
1783   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1784   g_return_val_if_fail (event != NULL, FALSE);
1785   
1786   text = GTK_TEXT (widget);
1787   
1788   x = event->x;
1789   y = event->y;
1790   mask = event->state;
1791   if (event->is_hint || (text->text_area != event->window))
1792     {
1793       gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1794     }
1795   
1796   if ((text->button == 0) ||
1797       !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
1798     return FALSE;
1799   
1800   gdk_window_get_size (text->text_area, NULL, &height);
1801   
1802   if ((y < 0) || (y > height))
1803     {
1804       if (text->timer == 0)
1805         {
1806           text->timer = g_timeout_add (SCROLL_TIME, 
1807                                        gtk_text_scroll_timeout,
1808                                        text);
1809           
1810           if (y < 0)
1811             scroll_int (text, y/2);
1812           else
1813             scroll_int (text, (y - height)/2);
1814         }
1815       else
1816         return FALSE;
1817     }
1818   
1819   undraw_cursor (GTK_TEXT (widget), FALSE);
1820   find_mouse_cursor (GTK_TEXT (widget), x, y);
1821   draw_cursor (GTK_TEXT (widget), FALSE);
1822   
1823   gtk_text_set_selection (GTK_OLD_EDITABLE (text), 
1824                           GTK_OLD_EDITABLE (text)->selection_start_pos,
1825                           text->cursor_mark.index);
1826   
1827   return FALSE;
1828 }
1829
1830 static void 
1831 gtk_text_insert_text    (GtkEditable       *editable,
1832                          const gchar       *new_text,
1833                          gint               new_text_length,
1834                          gint              *position)
1835 {
1836   GtkText *text = GTK_TEXT (editable);
1837   GdkFont *font;
1838   GdkColor *fore, *back;
1839
1840   TextProperty *property;
1841
1842   gtk_text_set_point (text, *position);
1843
1844   property = MARK_CURRENT_PROPERTY (&text->point);
1845   font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL; 
1846   fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL; 
1847   back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL; 
1848   
1849   gtk_text_insert (text, font, fore, back, new_text, new_text_length);
1850
1851   *position = text->point.index;
1852 }
1853
1854 static void 
1855 gtk_text_delete_text    (GtkEditable       *editable,
1856                          gint               start_pos,
1857                          gint               end_pos)
1858 {
1859   GtkText *text;
1860   
1861   g_return_if_fail (start_pos >= 0);
1862   
1863   text = GTK_TEXT (editable);
1864   
1865   gtk_text_set_point (text, start_pos);
1866   if (end_pos < 0)
1867     end_pos = TEXT_LENGTH (text);
1868   
1869   if (end_pos > start_pos)
1870     gtk_text_forward_delete (text, end_pos - start_pos);
1871 }
1872
1873 static gint
1874 gtk_text_key_press (GtkWidget   *widget,
1875                     GdkEventKey *event)
1876 {
1877   GtkText *text;
1878   GtkOldEditable *old_editable;
1879   gchar key;
1880   gint return_val;
1881   gint position;
1882   
1883   g_return_val_if_fail (GTK_IS_TEXT (widget), FALSE);
1884   g_return_val_if_fail (event != NULL, FALSE);
1885   
1886   text = GTK_TEXT (widget);
1887   old_editable = GTK_OLD_EDITABLE (widget);
1888   
1889   key = event->keyval;
1890   return_val = TRUE;
1891   
1892   if ((GTK_OLD_EDITABLE(text)->editable == FALSE))
1893     {
1894       switch (event->keyval)
1895         {
1896         case GDK_Home:
1897         case GDK_KP_Home:
1898           if (event->state & GDK_CONTROL_MASK)
1899             scroll_int (text, -text->vadj->value);
1900           else
1901             return_val = FALSE;
1902           break;
1903         case GDK_End:
1904         case GDK_KP_End:
1905           if (event->state & GDK_CONTROL_MASK)
1906             scroll_int (text, +text->vadj->upper); 
1907           else
1908             return_val = FALSE;
1909           break;
1910         case GDK_KP_Page_Up:
1911         case GDK_Page_Up:   scroll_int (text, -text->vadj->page_increment); break;
1912         case GDK_KP_Page_Down:
1913         case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
1914         case GDK_KP_Up:
1915         case GDK_Up:        scroll_int (text, -KEY_SCROLL_PIXELS); break;
1916         case GDK_KP_Down:
1917         case GDK_Down:      scroll_int (text, +KEY_SCROLL_PIXELS); break;
1918         case GDK_Return:
1919         case GDK_KP_Enter:
1920           if (event->state & GDK_CONTROL_MASK)
1921             gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
1922           else
1923             return_val = FALSE;
1924           break;
1925         default:
1926           return_val = FALSE;
1927           break;
1928         }
1929     }
1930   else
1931     {
1932       gint extend_selection;
1933       gint extend_start;
1934       guint initial_pos = old_editable->current_pos;
1935       
1936       text->point = find_mark (text, text->cursor_mark.index);
1937       
1938       extend_selection = event->state & GDK_SHIFT_MASK;
1939       extend_start = FALSE;
1940       
1941       if (extend_selection)
1942         {
1943           old_editable->has_selection = TRUE;
1944           
1945           if (old_editable->selection_start_pos == old_editable->selection_end_pos)
1946             {
1947               old_editable->selection_start_pos = text->point.index;
1948               old_editable->selection_end_pos = text->point.index;
1949             }
1950           
1951           extend_start = (text->point.index == old_editable->selection_start_pos);
1952         }
1953       
1954       switch (event->keyval)
1955         {
1956         case GDK_KP_Home:
1957         case GDK_Home:
1958           if (event->state & GDK_CONTROL_MASK)
1959             move_cursor_buffer_ver (text, -1);
1960           else
1961             gtk_text_move_beginning_of_line (text);
1962           break;
1963         case GDK_KP_End:
1964         case GDK_End:
1965           if (event->state & GDK_CONTROL_MASK)
1966             move_cursor_buffer_ver (text, +1);
1967           else
1968             gtk_text_move_end_of_line (text);
1969           break;
1970         case GDK_KP_Page_Up:
1971         case GDK_Page_Up:   move_cursor_page_ver (text, -1); break;
1972         case GDK_KP_Page_Down:
1973         case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
1974           /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
1975         case GDK_KP_Up:
1976         case GDK_Up:        move_cursor_ver (text, -1); break;
1977         case GDK_KP_Down:
1978         case GDK_Down:      move_cursor_ver (text, +1); break;
1979         case GDK_KP_Left:
1980         case GDK_Left:
1981           if (event->state & GDK_CONTROL_MASK)
1982             gtk_text_move_backward_word (text);
1983           else
1984             move_cursor_hor (text, -1); 
1985           break;
1986         case GDK_KP_Right:
1987         case GDK_Right:     
1988           if (event->state & GDK_CONTROL_MASK)
1989             gtk_text_move_forward_word (text);
1990           else
1991             move_cursor_hor (text, +1); 
1992           break;
1993           
1994         case GDK_BackSpace:
1995           if (event->state & GDK_CONTROL_MASK)
1996             gtk_text_delete_backward_word (text);
1997           else
1998             gtk_text_delete_backward_character (text);
1999           break;
2000         case GDK_Clear:
2001           gtk_text_delete_line (text);
2002           break;
2003         case GDK_KP_Insert:
2004         case GDK_Insert:
2005           if (event->state & GDK_SHIFT_MASK)
2006             {
2007               extend_selection = FALSE;
2008               gtk_editable_paste_clipboard (GTK_EDITABLE (old_editable));
2009             }
2010           else if (event->state & GDK_CONTROL_MASK)
2011             {
2012               gtk_editable_copy_clipboard (GTK_EDITABLE (old_editable));
2013             }
2014           else
2015             {
2016               /* gtk_toggle_insert(text) -- IMPLEMENT */
2017             }
2018           break;
2019         case GDK_Delete:
2020         case GDK_KP_Delete:
2021           if (event->state & GDK_CONTROL_MASK)
2022             gtk_text_delete_forward_word (text);
2023           else if (event->state & GDK_SHIFT_MASK)
2024             {
2025               extend_selection = FALSE;
2026               gtk_editable_cut_clipboard (GTK_EDITABLE (old_editable));
2027             }
2028           else
2029             gtk_text_delete_forward_character (text);
2030           break;
2031         case GDK_Tab:
2032         case GDK_ISO_Left_Tab:
2033         case GDK_KP_Tab:
2034           position = text->point.index;
2035           gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\t", 1, &position);
2036           break;
2037         case GDK_KP_Enter:
2038         case GDK_Return:
2039           if (event->state & GDK_CONTROL_MASK)
2040             gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2041           else
2042             {
2043               position = text->point.index;
2044               gtk_editable_insert_text (GTK_EDITABLE (old_editable), "\n", 1, &position);
2045             }
2046           break;
2047         case GDK_Escape:
2048           /* Don't insert literally */
2049           return_val = FALSE;
2050           break;
2051           
2052         default:
2053           return_val = FALSE;
2054           
2055           if (event->state & GDK_CONTROL_MASK)
2056             {
2057               return_val = TRUE;
2058               if ((key >= 'A') && (key <= 'Z'))
2059                 key -= 'A' - 'a';
2060
2061               switch (key)
2062                 {
2063                 case 'a':
2064                   gtk_text_move_beginning_of_line (text);
2065                   break;
2066                 case 'b':
2067                   gtk_text_move_backward_character (text);
2068                   break;
2069                 case 'c':
2070                   gtk_editable_copy_clipboard (text);
2071                   break;
2072                 case 'd':
2073                   gtk_text_delete_forward_character (text);
2074                   break;
2075                 case 'e':
2076                   gtk_text_move_end_of_line (text);
2077                   break;
2078                 case 'f':
2079                   gtk_text_move_forward_character (text);
2080                   break;
2081                 case 'h':
2082                   gtk_text_delete_backward_character (text);
2083                   break;
2084                 case 'k':
2085                   gtk_text_delete_to_line_end (text);
2086                   break;
2087                 case 'n':
2088                   gtk_text_move_next_line (text);
2089                   break;
2090                 case 'p':
2091                   gtk_text_move_previous_line (text);
2092                   break;
2093                 case 'u':
2094                   gtk_text_delete_line (text);
2095                   break;
2096                 case 'v':
2097                   gtk_editable_paste_clipboard (text);
2098                   break;
2099                 case 'w':
2100                   gtk_text_delete_backward_word (text);
2101                   break;
2102                 case 'x':
2103                   gtk_editable_cut_clipboard (text);
2104                   break;
2105                 default:
2106                   return_val = FALSE;
2107                 }
2108
2109               break;
2110             }
2111           else if (event->state & GDK_MOD1_MASK)
2112             {
2113               return_val = TRUE;
2114               if ((key >= 'A') && (key <= 'Z'))
2115                 key -= 'A' - 'a';
2116               
2117               switch (key)
2118                 {
2119                 case 'b':
2120                   gtk_text_move_backward_word (text);
2121                   break;
2122                 case 'd':
2123                   gtk_text_delete_forward_word (text);
2124                   break;
2125                 case 'f':
2126                   gtk_text_move_forward_word (text);
2127                   break;
2128                 default:
2129                   return_val = FALSE;
2130                 }
2131               
2132               break;
2133             }
2134           else if (event->length > 0)
2135             {
2136               extend_selection = FALSE;
2137               
2138               gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
2139               position = text->point.index;
2140               gtk_editable_insert_text (GTK_EDITABLE (old_editable), event->string, event->length, &position);
2141               
2142               return_val = TRUE;
2143             }
2144         }
2145       
2146       if (return_val && (old_editable->current_pos != initial_pos))
2147         {
2148           if (extend_selection)
2149             {
2150               if (old_editable->current_pos < old_editable->selection_start_pos)
2151                 gtk_text_set_selection (old_editable, old_editable->current_pos,
2152                                         old_editable->selection_end_pos);
2153               else if (old_editable->current_pos > old_editable->selection_end_pos)
2154                 gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2155                                         old_editable->current_pos);
2156               else
2157                 {
2158                   if (extend_start)
2159                     gtk_text_set_selection (old_editable, old_editable->current_pos,
2160                                             old_editable->selection_end_pos);
2161                   else
2162                     gtk_text_set_selection (old_editable, old_editable->selection_start_pos,
2163                                             old_editable->current_pos);
2164                 }
2165             }
2166           else
2167             gtk_text_set_selection (old_editable, 0, 0);
2168           
2169           gtk_old_editable_claim_selection (old_editable,
2170                                             old_editable->selection_start_pos != old_editable->selection_end_pos,
2171                                             event->time);
2172         }
2173     }
2174   
2175   return return_val;
2176 }
2177
2178 static void
2179 gtk_text_adjustment (GtkAdjustment *adjustment,
2180                      GtkText       *text)
2181 {
2182   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2183   g_return_if_fail (GTK_IS_TEXT (text));
2184   
2185   /* Just ignore it if we haven't been size-allocated and realized yet */
2186   if (text->line_start_cache == NULL) 
2187     return;
2188   
2189   if (adjustment == text->hadj)
2190     {
2191       g_warning ("horizontal scrolling not implemented");
2192     }
2193   else
2194     {
2195       gint diff = ((gint)adjustment->value) - text->last_ver_value;
2196       
2197       if (diff != 0)
2198         {
2199           undraw_cursor (text, FALSE);
2200           
2201           if (diff > 0)
2202             scroll_down (text, diff);
2203           else /* if (diff < 0) */
2204             scroll_up (text, diff);
2205           
2206           draw_cursor (text, FALSE);
2207           
2208           text->last_ver_value = adjustment->value;
2209         }
2210     }
2211 }
2212
2213 static void
2214 gtk_text_adjustment_destroyed (GtkAdjustment *adjustment,
2215                                GtkText       *text)
2216 {
2217   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2218   g_return_if_fail (GTK_IS_TEXT (text));
2219
2220   if (adjustment == text->hadj)
2221     gtk_text_set_adjustments (text, NULL, text->vadj);
2222   if (adjustment == text->vadj)
2223     gtk_text_set_adjustments (text, text->hadj, NULL);
2224 }
2225
2226
2227 static GtkPropertyMark
2228 find_this_line_start_mark (GtkText* text, guint point_position, const GtkPropertyMark* near)
2229 {
2230   GtkPropertyMark mark;
2231   
2232   mark = find_mark_near (text, point_position, near);
2233   
2234   while (mark.index > 0 &&
2235          GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2236     decrement_mark (&mark);
2237   
2238   return mark;
2239 }
2240
2241 static void
2242 init_tab_cont (GtkText* text, PrevTabCont* tab_cont)
2243 {
2244   tab_cont->pixel_offset          = 0;
2245   tab_cont->tab_start.tab_stops   = text->tab_stops;
2246   tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2247   
2248   if (!tab_cont->tab_start.to_next_tab)
2249     tab_cont->tab_start.to_next_tab = text->default_tab_width;
2250 }
2251
2252 static void
2253 line_params_iterate (GtkText* text,
2254                      const GtkPropertyMark* mark0,
2255                      const PrevTabCont* tab_mark0,
2256                      gint8 alloc,
2257                      void* data,
2258                      LineIteratorFunction iter)
2259      /* mark0 MUST be a real line start.  if ALLOC, allocate line params
2260       * from a mem chunk.  DATA is passed to ITER_CALL, which is called
2261       * for each line following MARK, iteration continues unless ITER_CALL
2262       * returns TRUE. */
2263 {
2264   GtkPropertyMark mark = *mark0;
2265   PrevTabCont  tab_conts[2];
2266   LineParams   *lp, lpbuf;
2267   gint         tab_cont_index = 0;
2268   
2269   if (tab_mark0)
2270     tab_conts[0] = *tab_mark0;
2271   else
2272     init_tab_cont (text, tab_conts);
2273   
2274   for (;;)
2275     {
2276       if (alloc)
2277         lp = g_slice_new (LineParams);
2278       else
2279         lp = &lpbuf;
2280       
2281       *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2282                               tab_conts + (tab_cont_index + 1) % 2);
2283       
2284       if ((*iter) (text, lp, data))
2285         return;
2286       
2287       if (LAST_INDEX (text, lp->end))
2288         break;
2289       
2290       mark = lp->end;
2291       advance_mark (&mark);
2292       tab_cont_index = (tab_cont_index + 1) % 2;
2293     }
2294 }
2295
2296 static gint
2297 fetch_lines_iterator (GtkText* text, LineParams* lp, void* data)
2298 {
2299   FetchLinesData *fldata = (FetchLinesData*) data;
2300   
2301   fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2302   
2303   switch (fldata->fl_type)
2304     {
2305     case FetchLinesCount:
2306       if (!text->line_wrap || !lp->wraps)
2307         fldata->data += 1;
2308       
2309       if (fldata->data >= fldata->data_max)
2310         return TRUE;
2311       
2312       break;
2313     case FetchLinesPixels:
2314       
2315       fldata->data += LINE_HEIGHT(*lp);
2316       
2317       if (fldata->data >= fldata->data_max)
2318         return TRUE;
2319       
2320       break;
2321     }
2322   
2323   return FALSE;
2324 }
2325
2326 static GList*
2327 fetch_lines (GtkText* text,
2328              const GtkPropertyMark* mark0,
2329              const PrevTabCont* tab_cont0,
2330              FLType fl_type,
2331              gint data)
2332 {
2333   FetchLinesData fl_data;
2334   
2335   fl_data.new_lines = NULL;
2336   fl_data.data      = 0;
2337   fl_data.data_max  = data;
2338   fl_data.fl_type   = fl_type;
2339   
2340   line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2341   
2342   return g_list_reverse (fl_data.new_lines);
2343 }
2344
2345 static void
2346 fetch_lines_backward (GtkText* text)
2347 {
2348   GList *new_line_start;
2349   GtkPropertyMark mark;
2350   
2351   if (CACHE_DATA(text->line_start_cache).start.index == 0)
2352     return;
2353   
2354   mark = find_this_line_start_mark (text,
2355                                     CACHE_DATA(text->line_start_cache).start.index - 1,
2356                                     &CACHE_DATA(text->line_start_cache).start);
2357   
2358   new_line_start = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2359   
2360   while (new_line_start->next)
2361     new_line_start = new_line_start->next;
2362   
2363   new_line_start->next = text->line_start_cache;
2364   text->line_start_cache->prev = new_line_start;
2365 }
2366
2367 static void
2368 fetch_lines_forward (GtkText* text, gint line_count)
2369 {
2370   GtkPropertyMark mark;
2371   GList* line = text->line_start_cache;
2372   
2373   while(line->next)
2374     line = line->next;
2375   
2376   mark = CACHE_DATA(line).end;
2377   
2378   if (LAST_INDEX (text, mark))
2379     return;
2380   
2381   advance_mark(&mark);
2382   
2383   line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2384   
2385   if (line->next)
2386     line->next->prev = line;
2387 }
2388
2389 /* Compute the number of lines, and vertical pixels for n characters
2390  * starting from the point 
2391  */
2392 static void
2393 compute_lines_pixels (GtkText* text, guint char_count,
2394                       guint *lines, guint *pixels)
2395 {
2396   GList *line = text->current_line;
2397   gint chars_left = char_count;
2398   
2399   *lines = 0;
2400   *pixels = 0;
2401   
2402   /* If chars_left == 0, that means we're joining two lines in a
2403    * deletion, so add in the values for the next line as well 
2404    */
2405   for (; line && chars_left >= 0; line = line->next)
2406     {
2407       *pixels += LINE_HEIGHT(CACHE_DATA(line));
2408       
2409       if (line == text->current_line)
2410         chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2411       else
2412         chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2413       
2414       if (!text->line_wrap || !CACHE_DATA(line).wraps)
2415         *lines += 1;
2416       else
2417         if (chars_left < 0)
2418           chars_left = 0;       /* force another loop */
2419       
2420       if (!line->next)
2421         fetch_lines_forward (text, 1);
2422     }
2423 }
2424
2425 static gint
2426 total_line_height (GtkText* text, GList* line, gint line_count)
2427 {
2428   gint height = 0;
2429   
2430   for (; line && line_count > 0; line = line->next)
2431     {
2432       height += LINE_HEIGHT(CACHE_DATA(line));
2433       
2434       if (!text->line_wrap || !CACHE_DATA(line).wraps)
2435         line_count -= 1;
2436       
2437       if (!line->next)
2438         fetch_lines_forward (text, line_count);
2439     }
2440   
2441   return height;
2442 }
2443
2444 static void
2445 swap_lines (GtkText* text, GList* old, GList* new, guint old_line_count)
2446 {
2447   if (old == text->line_start_cache)
2448     {
2449       GList* last;
2450       
2451       for (; old_line_count > 0; old_line_count -= 1)
2452         {
2453           while (text->line_start_cache &&
2454                  text->line_wrap &&
2455                  CACHE_DATA(text->line_start_cache).wraps)
2456             remove_cache_line(text, text->line_start_cache);
2457           
2458           remove_cache_line(text, text->line_start_cache);
2459         }
2460       
2461       last = g_list_last (new);
2462       
2463       last->next = text->line_start_cache;
2464       
2465       if (text->line_start_cache)
2466         text->line_start_cache->prev = last;
2467       
2468       text->line_start_cache = new;
2469     }
2470   else
2471     {
2472       GList *last;
2473       
2474       g_assert (old->prev);
2475       
2476       last = old->prev;
2477       
2478       for (; old_line_count > 0; old_line_count -= 1)
2479         {
2480           while (old && text->line_wrap && CACHE_DATA(old).wraps)
2481             old = remove_cache_line (text, old);
2482           
2483           old = remove_cache_line (text, old);
2484         }
2485       
2486       last->next = new;
2487       new->prev = last;
2488       
2489       last = g_list_last (new);
2490       
2491       last->next = old;
2492       
2493       if (old)
2494         old->prev = last;
2495     }
2496 }
2497
2498 static void
2499 correct_cache_delete (GtkText* text, gint nchars, gint lines)
2500 {
2501   GList* cache = text->current_line;
2502   gint i;
2503   
2504   for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2505     /* nothing */;
2506   
2507   for (; cache; cache = cache->next)
2508     {
2509       GtkPropertyMark *start = &CACHE_DATA(cache).start;
2510       GtkPropertyMark *end = &CACHE_DATA(cache).end;
2511       
2512       start->index -= nchars;
2513       end->index -= nchars;
2514       
2515       if (LAST_INDEX (text, text->point) &&
2516           start->index == text->point.index)
2517         *start = text->point;
2518       else if (start->property == text->point.property)
2519         start->offset = start->index - (text->point.index - text->point.offset);
2520       
2521       if (LAST_INDEX (text, text->point) &&
2522           end->index == text->point.index)
2523         *end = text->point;
2524       if (end->property == text->point.property)
2525         end->offset = end->index - (text->point.index - text->point.offset);
2526       
2527       /*TEXT_ASSERT_MARK(text, start, "start");*/
2528       /*TEXT_ASSERT_MARK(text, end, "end");*/
2529     }
2530 }
2531
2532 static void
2533 delete_expose (GtkText* text, guint nchars, guint old_lines, guint old_pixels)
2534 {
2535   GtkWidget *widget = GTK_WIDGET (text);
2536   
2537   gint pixel_height;
2538   guint new_pixels = 0;
2539   GdkRectangle rect;
2540   GList* new_line = NULL;
2541   gint width, height;
2542   
2543   text->cursor_virtual_x = 0;
2544   
2545   correct_cache_delete (text, nchars, old_lines);
2546   
2547   pixel_height = pixel_height_of(text, text->current_line) -
2548     LINE_HEIGHT(CACHE_DATA(text->current_line));
2549   
2550   if (CACHE_DATA(text->current_line).start.index == text->point.index)
2551     CACHE_DATA(text->current_line).start = text->point;
2552   
2553   new_line = fetch_lines (text,
2554                           &CACHE_DATA(text->current_line).start,
2555                           &CACHE_DATA(text->current_line).tab_cont,
2556                           FetchLinesCount,
2557                           1);
2558   
2559   swap_lines (text, text->current_line, new_line, old_lines);
2560   
2561   text->current_line = new_line;
2562   
2563   new_pixels = total_line_height (text, new_line, 1);
2564   
2565   gdk_window_get_size (text->text_area, &width, &height);
2566   
2567   if (old_pixels != new_pixels)
2568     {
2569       if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2570         {
2571           gdk_draw_pixmap (text->text_area,
2572                            text->gc,
2573                            text->text_area,
2574                            0,
2575                            pixel_height + old_pixels,
2576                            0,
2577                            pixel_height + new_pixels,
2578                            width,
2579                            height);
2580         }
2581       text->vadj->upper += new_pixels;
2582       text->vadj->upper -= old_pixels;
2583       adjust_adj (text, text->vadj);
2584     }
2585   
2586   rect.x = 0;
2587   rect.y = pixel_height;
2588   rect.width = width;
2589   rect.height = new_pixels;
2590   
2591   expose_text (text, &rect, FALSE);
2592   gtk_text_draw_focus ( (GtkWidget *) text);
2593   
2594   text->cursor_mark = text->point;
2595   
2596   find_cursor (text, TRUE);
2597   
2598   if (old_pixels != new_pixels)
2599     {
2600       if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2601         {
2602           rect.x = 0;
2603           rect.y = pixel_height + new_pixels;
2604           rect.width = width;
2605           rect.height = height - rect.y;
2606           
2607           expose_text (text, &rect, FALSE);
2608         }
2609       else
2610         process_exposes (text);
2611     }
2612   
2613   TEXT_ASSERT (text);
2614   TEXT_SHOW(text);
2615 }
2616
2617 /* note, the point has already been moved forward */
2618 static void
2619 correct_cache_insert (GtkText* text, gint nchars)
2620 {
2621   GList *cache;
2622   GtkPropertyMark *start;
2623   GtkPropertyMark *end;
2624   gboolean was_split = FALSE;
2625   
2626   /* We need to distinguish whether the property was split in the
2627    * insert or not, so we check if the point (which points after
2628    * the insertion here), points to the same character as the
2629    * point before. Ugh.
2630    */
2631   if (nchars > 0)
2632     {
2633       GtkPropertyMark tmp_mark = text->point;
2634       move_mark_n (&tmp_mark, -1);
2635       
2636       if (tmp_mark.property != text->point.property)
2637         was_split = TRUE;
2638     }
2639   
2640   /* If we inserted a property exactly at the beginning of the
2641    * line, we have to correct here, or fetch_lines will
2642    * fetch junk.
2643    */
2644   start = &CACHE_DATA(text->current_line).start;
2645
2646   /* Check if if we split exactly at the beginning of the line:
2647    * (was_split won't be set if we are inserting at the end of the text, 
2648    *  so we don't check)
2649    */
2650   if (start->offset ==  MARK_CURRENT_PROPERTY (start)->length)
2651     SET_PROPERTY_MARK (start, start->property->next, 0);
2652   /* Check if we inserted a property at the beginning of the text: */
2653   else if (was_split &&
2654            (start->property == text->point.property) &&
2655            (start->index == text->point.index - nchars))
2656     SET_PROPERTY_MARK (start, start->property->prev, 0);
2657
2658   /* Now correct the offsets, and check for start or end marks that
2659    * are after the point, yet point to a property before the point's
2660    * property. This indicates that they are meant to point to the
2661    * second half of a property we split in insert_text_property(), so
2662    * we fix them up that way.  
2663    */
2664   cache = text->current_line->next;
2665   
2666   for (; cache; cache = cache->next)
2667     {
2668       start = &CACHE_DATA(cache).start;
2669       end = &CACHE_DATA(cache).end;
2670       
2671       if (LAST_INDEX (text, text->point) &&
2672           start->index == text->point.index)
2673         *start = text->point;
2674       else if (start->index >= text->point.index - nchars)
2675         {
2676           if (!was_split && start->property == text->point.property)
2677             move_mark_n(start, nchars);
2678           else
2679             {
2680               if (start->property->next &&
2681                   (start->property->next->next == text->point.property))
2682                 {
2683                   g_assert (start->offset >=  MARK_CURRENT_PROPERTY (start)->length);
2684                   start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2685                   start->property = text->point.property;
2686                 }
2687               start->index += nchars;
2688             }
2689         }
2690       
2691       if (LAST_INDEX (text, text->point) &&
2692           end->index == text->point.index)
2693         *end = text->point;
2694       if (end->index >= text->point.index - nchars)
2695         {
2696           if (!was_split && end->property == text->point.property)
2697             move_mark_n(end, nchars);
2698           else 
2699             {
2700               if (end->property->next &&
2701                   (end->property->next->next == text->point.property))
2702                 {
2703                   g_assert (end->offset >=  MARK_CURRENT_PROPERTY (end)->length);
2704                   end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2705                   end->property = text->point.property;
2706                 }
2707               end->index += nchars;
2708             }
2709         }
2710       
2711       /*TEXT_ASSERT_MARK(text, start, "start");*/
2712       /*TEXT_ASSERT_MARK(text, end, "end");*/
2713     }
2714 }
2715
2716
2717 static void
2718 insert_expose (GtkText* text, guint old_pixels, gint nchars,
2719                guint new_line_count)
2720 {
2721   GtkWidget *widget = GTK_WIDGET (text);
2722   
2723   gint pixel_height;
2724   guint new_pixels = 0;
2725   GdkRectangle rect;
2726   GList* new_lines = NULL;
2727   gint width, height;
2728   
2729   text->cursor_virtual_x = 0;
2730   
2731   undraw_cursor (text, FALSE);
2732   
2733   correct_cache_insert (text, nchars);
2734   
2735   TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2736   
2737   pixel_height = pixel_height_of(text, text->current_line) -
2738     LINE_HEIGHT(CACHE_DATA(text->current_line));
2739   
2740   new_lines = fetch_lines (text,
2741                            &CACHE_DATA(text->current_line).start,
2742                            &CACHE_DATA(text->current_line).tab_cont,
2743                            FetchLinesCount,
2744                            new_line_count);
2745   
2746   swap_lines (text, text->current_line, new_lines, 1);
2747   
2748   text->current_line = new_lines;
2749   
2750   new_pixels = total_line_height (text, new_lines, new_line_count);
2751   
2752   gdk_window_get_size (text->text_area, &width, &height);
2753   
2754   if (old_pixels != new_pixels)
2755     {
2756       if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2757         {
2758           gdk_draw_pixmap (text->text_area,
2759                            text->gc,
2760                            text->text_area,
2761                            0,
2762                            pixel_height + old_pixels,
2763                            0,
2764                            pixel_height + new_pixels,
2765                            width,
2766                            height + (old_pixels - new_pixels) - pixel_height);
2767           
2768         }
2769       text->vadj->upper += new_pixels;
2770       text->vadj->upper -= old_pixels;
2771       adjust_adj (text, text->vadj);
2772     }
2773   
2774   rect.x = 0;
2775   rect.y = pixel_height;
2776   rect.width = width;
2777   rect.height = new_pixels;
2778   
2779   expose_text (text, &rect, FALSE);
2780   gtk_text_draw_focus ( (GtkWidget *) text);
2781   
2782   text->cursor_mark = text->point;
2783   
2784   find_cursor (text, TRUE);
2785   
2786   draw_cursor (text, FALSE);
2787   
2788   if (old_pixels != new_pixels)
2789     {
2790       if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2791         {
2792           rect.x = 0;
2793           rect.y = pixel_height + new_pixels;
2794           rect.width = width;
2795           rect.height = height - rect.y;
2796           
2797           expose_text (text, &rect, FALSE);
2798         }
2799       else
2800         process_exposes (text);
2801     }
2802   
2803   TEXT_SHOW_ADJ (text, text->vadj, "vadj");
2804   TEXT_ASSERT (text);
2805   TEXT_SHOW(text);
2806 }
2807
2808 /* Text property functions */
2809
2810 static guint
2811 font_hash (gconstpointer font)
2812 {
2813   return gdk_font_id ((const GdkFont*) font);
2814 }
2815
2816 static GHashTable *font_cache_table = NULL;
2817
2818 static GtkTextFont*
2819 get_text_font (GdkFont* gfont)
2820 {
2821   GtkTextFont* tf;
2822   gint i;
2823   
2824   if (!font_cache_table)
2825     font_cache_table = g_hash_table_new (font_hash, (GEqualFunc) gdk_font_equal);
2826   
2827   tf = g_hash_table_lookup (font_cache_table, gfont);
2828   
2829   if (tf)
2830     {
2831       tf->ref_count++;
2832       return tf;
2833     }
2834
2835   tf = g_new (GtkTextFont, 1);
2836   tf->ref_count = 1;
2837
2838   tf->gdk_font = gfont;
2839   gdk_font_ref (gfont);
2840   
2841   for(i = 0; i < 256; i += 1)
2842     tf->char_widths[i] = gdk_char_width (gfont, (char)i);
2843   
2844   g_hash_table_insert (font_cache_table, gfont, tf);
2845   
2846   return tf;
2847 }
2848
2849 static void
2850 text_font_unref (GtkTextFont *text_font)
2851 {
2852   text_font->ref_count--;
2853   if (text_font->ref_count == 0)
2854     {
2855       g_hash_table_remove (font_cache_table, text_font->gdk_font);
2856       gdk_font_unref (text_font->gdk_font);
2857       g_free (text_font);
2858     }
2859 }
2860
2861 static gint
2862 text_properties_equal (TextProperty* prop, GdkFont* font, const GdkColor *fore, const GdkColor *back)
2863 {
2864   if (prop->flags & PROPERTY_FONT)
2865     {
2866       gboolean retval;
2867       GtkTextFont *text_font;
2868
2869       if (!font)
2870         return FALSE;
2871
2872       text_font = get_text_font (font);
2873
2874       retval = (prop->font == text_font);
2875       text_font_unref (text_font);
2876       
2877       if (!retval)
2878         return FALSE;
2879     }
2880   else
2881     if (font != NULL)
2882       return FALSE;
2883
2884   if (prop->flags & PROPERTY_FOREGROUND)
2885     {
2886       if (!fore || !gdk_color_equal (&prop->fore_color, fore))
2887         return FALSE;
2888     }
2889   else
2890     if (fore != NULL)
2891       return FALSE;
2892
2893   if (prop->flags & PROPERTY_BACKGROUND)
2894     {
2895       if (!back || !gdk_color_equal (&prop->back_color, back))
2896         return FALSE;
2897     }
2898   else
2899     if (back != NULL)
2900       return FALSE;
2901   
2902   return TRUE;
2903 }
2904
2905 static void
2906 realize_property (GtkText *text, TextProperty *prop)
2907 {
2908   GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2909
2910   if (prop->flags & PROPERTY_FOREGROUND)
2911     gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
2912   
2913   if (prop->flags & PROPERTY_BACKGROUND)
2914     gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
2915 }
2916
2917 static void
2918 realize_properties (GtkText *text)
2919 {
2920   GList *tmp_list = text->text_properties;
2921
2922   while (tmp_list)
2923     {
2924       realize_property (text, tmp_list->data);
2925       
2926       tmp_list = tmp_list->next;
2927     }
2928 }
2929
2930 static void
2931 unrealize_property (GtkText *text, TextProperty *prop)
2932 {
2933   GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
2934
2935   if (prop->flags & PROPERTY_FOREGROUND)
2936     gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
2937   
2938   if (prop->flags & PROPERTY_BACKGROUND)
2939     gdk_colormap_free_colors (colormap, &prop->back_color, 1);
2940 }
2941
2942 static void
2943 unrealize_properties (GtkText *text)
2944 {
2945   GList *tmp_list = text->text_properties;
2946
2947   while (tmp_list)
2948     {
2949       unrealize_property (text, tmp_list->data);
2950
2951       tmp_list = tmp_list->next;
2952     }
2953 }
2954
2955 static TextProperty*
2956 new_text_property (GtkText *text, GdkFont *font, const GdkColor* fore,
2957                    const GdkColor* back, guint length)
2958 {
2959   TextProperty *prop;
2960   
2961   prop = g_slice_new (TextProperty);
2962
2963   prop->flags = 0;
2964   if (font)
2965     {
2966       prop->flags |= PROPERTY_FONT;
2967       prop->font = get_text_font (font);
2968     }
2969   else
2970     prop->font = NULL;
2971   
2972   if (fore)
2973     {
2974       prop->flags |= PROPERTY_FOREGROUND;
2975       prop->fore_color = *fore;
2976     }
2977       
2978   if (back)
2979     {
2980       prop->flags |= PROPERTY_BACKGROUND;
2981       prop->back_color = *back;
2982     }
2983
2984   prop->length = length;
2985
2986   if (GTK_WIDGET_REALIZED (text))
2987     realize_property (text, prop);
2988
2989   return prop;
2990 }
2991
2992 static void
2993 destroy_text_property (TextProperty *prop)
2994 {
2995   if (prop->font)
2996     text_font_unref (prop->font);
2997   
2998   g_slice_free (TextProperty, prop);
2999 }
3000
3001 /* Flop the memory between the point and the gap around like a
3002  * dead fish. */
3003 static void
3004 move_gap (GtkText* text, guint index)
3005 {
3006   if (text->gap_position < index)
3007     {
3008       gint diff = index - text->gap_position;
3009       
3010       if (text->use_wchar)
3011         g_memmove (text->text.wc + text->gap_position,
3012                    text->text.wc + text->gap_position + text->gap_size,
3013                    diff*sizeof (GdkWChar));
3014       else
3015         g_memmove (text->text.ch + text->gap_position,
3016                    text->text.ch + text->gap_position + text->gap_size,
3017                    diff);
3018       
3019       text->gap_position = index;
3020     }
3021   else if (text->gap_position > index)
3022     {
3023       gint diff = text->gap_position - index;
3024       
3025       if (text->use_wchar)
3026         g_memmove (text->text.wc + index + text->gap_size,
3027                    text->text.wc + index,
3028                    diff*sizeof (GdkWChar));
3029       else
3030         g_memmove (text->text.ch + index + text->gap_size,
3031                    text->text.ch + index,
3032                    diff);
3033       
3034       text->gap_position = index;
3035     }
3036 }
3037
3038 /* Increase the gap size. */
3039 static void
3040 make_forward_space (GtkText* text, guint len)
3041 {
3042   if (text->gap_size < len)
3043     {
3044       guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3045       
3046       if (sum >= text->text_len)
3047         {
3048           guint i = 1;
3049           
3050           while (i <= sum) i <<= 1;
3051           
3052           if (text->use_wchar)
3053             text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3054                                                   i*sizeof(GdkWChar));
3055           else
3056             text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3057           text->text_len = i;
3058         }
3059       
3060       if (text->use_wchar)
3061         g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3062                    text->text.wc + text->gap_position + text->gap_size,
3063                    (text->text_end - (text->gap_position + text->gap_size))
3064                    *sizeof(GdkWChar));
3065       else
3066         g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3067                    text->text.ch + text->gap_position + text->gap_size,
3068                    text->text_end - (text->gap_position + text->gap_size));
3069       
3070       text->text_end += len*2;
3071       text->gap_size += len*2;
3072     }
3073 }
3074
3075 /* Inserts into the text property list a list element that guarantees
3076  * that for len characters following the point, text has the correct
3077  * property.  does not move point.  adjusts text_properties_point and
3078  * text_properties_point_offset relative to the current value of
3079  * point. */
3080 static void
3081 insert_text_property (GtkText* text, GdkFont* font,
3082                       const GdkColor *fore, const GdkColor* back, guint len)
3083 {
3084   GtkPropertyMark *mark = &text->point;
3085   TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3086   TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3087   
3088   if (MARK_OFFSET(mark) == 0)
3089     {
3090       /* Point is on the boundary of two properties.
3091        * If it is the same as either, grow, else insert
3092        * a new one. */
3093       
3094       if (text_properties_equal(forward_prop, font, fore, back))
3095         {
3096           /* Grow the property in front of us. */
3097           
3098           MARK_PROPERTY_LENGTH(mark) += len;
3099         }
3100       else if (backward_prop &&
3101                text_properties_equal(backward_prop, font, fore, back))
3102         {
3103           /* Grow property behind us, point property and offset
3104            * change. */
3105           
3106           SET_PROPERTY_MARK (&text->point,
3107                              MARK_PREV_LIST_PTR (mark),
3108                              backward_prop->length);
3109           
3110           backward_prop->length += len;
3111         }
3112       else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3113                (forward_prop->length == 1))
3114         {
3115           /* Next property just has last position, take it over */
3116
3117           if (GTK_WIDGET_REALIZED (text))
3118             unrealize_property (text, forward_prop);
3119
3120           forward_prop->flags = 0;
3121           if (font)
3122             {
3123               forward_prop->flags |= PROPERTY_FONT;
3124               forward_prop->font = get_text_font (font);
3125             }
3126           else
3127             forward_prop->font = NULL;
3128             
3129           if (fore)
3130             {
3131               forward_prop->flags |= PROPERTY_FOREGROUND;
3132               forward_prop->fore_color = *fore;
3133             }
3134           if (back)
3135             {
3136               forward_prop->flags |= PROPERTY_BACKGROUND;
3137               forward_prop->back_color = *back;
3138             }
3139           forward_prop->length += len;
3140
3141           if (GTK_WIDGET_REALIZED (text))
3142             realize_property (text, forward_prop);
3143         }
3144       else
3145         {
3146           /* Splice a new property into the list. */
3147           
3148           GList* new_prop = g_list_alloc();
3149           
3150           new_prop->next = MARK_LIST_PTR(mark);
3151           new_prop->prev = MARK_PREV_LIST_PTR(mark);
3152           new_prop->next->prev = new_prop;
3153           
3154           if (new_prop->prev)
3155             new_prop->prev->next = new_prop;
3156
3157           new_prop->data = new_text_property (text, font, fore, back, len);
3158
3159           SET_PROPERTY_MARK (mark, new_prop, 0);
3160         }
3161     }
3162   else
3163     {
3164       /* The following will screw up the line_start cache,
3165        * we'll fix it up in correct_cache_insert
3166        */
3167       
3168       /* In the middle of forward_prop, if properties are equal,
3169        * just add to its length, else split it into two and splice
3170        * in a new one. */
3171       if (text_properties_equal (forward_prop, font, fore, back))
3172         {
3173           forward_prop->length += len;
3174         }
3175       else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3176                (MARK_OFFSET(mark) + 1 == forward_prop->length))
3177         {
3178           /* Inserting before only the last position in the text */
3179           
3180           GList* new_prop;
3181           forward_prop->length -= 1;
3182           
3183           new_prop = g_list_alloc();
3184           new_prop->data = new_text_property (text, font, fore, back, len+1);
3185           new_prop->prev = MARK_LIST_PTR(mark);
3186           new_prop->next = NULL;
3187           MARK_NEXT_LIST_PTR(mark) = new_prop;
3188           
3189           SET_PROPERTY_MARK (mark, new_prop, 0);
3190         }
3191       else
3192         {
3193           GList* new_prop = g_list_alloc();
3194           GList* new_prop_forward = g_list_alloc();
3195           gint old_length = forward_prop->length;
3196           GList* next = MARK_NEXT_LIST_PTR(mark);
3197           
3198           /* Set the new lengths according to where they are split.  Construct
3199            * two new properties. */
3200           forward_prop->length = MARK_OFFSET(mark);
3201
3202           new_prop_forward->data = 
3203             new_text_property(text,
3204                               forward_prop->flags & PROPERTY_FONT ? 
3205                                      forward_prop->font->gdk_font : NULL,
3206                               forward_prop->flags & PROPERTY_FOREGROUND ? 
3207                                      &forward_prop->fore_color : NULL,
3208                               forward_prop->flags & PROPERTY_BACKGROUND ? 
3209                                      &forward_prop->back_color : NULL,
3210                               old_length - forward_prop->length);
3211
3212           new_prop->data = new_text_property(text, font, fore, back, len);
3213
3214           /* Now splice things in. */
3215           MARK_NEXT_LIST_PTR(mark) = new_prop;
3216           new_prop->prev = MARK_LIST_PTR(mark);
3217           
3218           new_prop->next = new_prop_forward;
3219           new_prop_forward->prev = new_prop;
3220           
3221           new_prop_forward->next = next;
3222           
3223           if (next)
3224             next->prev = new_prop_forward;
3225           
3226           SET_PROPERTY_MARK (mark, new_prop, 0);
3227         }
3228     }
3229   
3230   while (text->text_properties_end->next)
3231     text->text_properties_end = text->text_properties_end->next;
3232   
3233   while (text->text_properties->prev)
3234     text->text_properties = text->text_properties->prev;
3235 }
3236
3237 static void
3238 delete_text_property (GtkText* text, guint nchars)
3239 {
3240   /* Delete nchars forward from point. */
3241   
3242   /* Deleting text properties is problematical, because we
3243    * might be storing around marks pointing to a property.
3244    *
3245    * The marks in question and how we handle them are:
3246    *
3247    *  point: We know the new value, since it will be at the
3248    *         end of the deleted text, and we move it there
3249    *         first.
3250    *  cursor: We just remove the mark and set it equal to the
3251    *         point after the operation.
3252    *  line-start cache: We replace most affected lines.
3253    *         The current line gets used to fetch the new
3254    *         lines so, if necessary, (delete at the beginning
3255    *         of a line) we fix it up by setting it equal to the
3256    *         point.
3257    */
3258   
3259   TextProperty *prop;
3260   GList        *tmp;
3261   gint          is_first;
3262   
3263   for(; nchars; nchars -= 1)
3264     {
3265       prop = MARK_CURRENT_PROPERTY(&text->point);
3266       
3267       prop->length -= 1;
3268       
3269       if (prop->length == 0)
3270         {
3271           tmp = MARK_LIST_PTR (&text->point);
3272           
3273           is_first = tmp == text->text_properties;
3274           
3275           MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3276           text->point.offset = 0;
3277
3278           if (GTK_WIDGET_REALIZED (text))
3279             unrealize_property (text, prop);
3280
3281           destroy_text_property (prop);
3282           g_list_free_1 (tmp);
3283           
3284           prop = MARK_CURRENT_PROPERTY (&text->point);
3285           
3286           if (is_first)
3287             text->text_properties = MARK_LIST_PTR (&text->point);
3288           
3289           g_assert (prop->length != 0);
3290         }
3291       else if (prop->length == text->point.offset)
3292         {
3293           MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3294           text->point.offset = 0;
3295         }
3296     }
3297   
3298   /* Check to see if we have just the single final position remaining
3299    * along in a property; if so, combine it with the previous property
3300    */
3301   if (LAST_INDEX (text, text->point) && 
3302       (MARK_OFFSET (&text->point) == 0) &&
3303       (MARK_PREV_LIST_PTR(&text->point) != NULL))
3304     {
3305       tmp = MARK_LIST_PTR (&text->point);
3306       prop = MARK_CURRENT_PROPERTY(&text->point);
3307       
3308       MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3309       MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3310       MARK_NEXT_LIST_PTR(&text->point) = NULL;
3311       
3312       text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3313       
3314       if (GTK_WIDGET_REALIZED (text))
3315         unrealize_property (text, prop);
3316
3317       destroy_text_property (prop);
3318       g_list_free_1 (tmp);
3319     }
3320 }
3321
3322 static void
3323 init_properties (GtkText *text)
3324 {
3325   if (!text->text_properties)
3326     {
3327       text->text_properties = g_list_alloc();
3328       text->text_properties->next = NULL;
3329       text->text_properties->prev = NULL;
3330       text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3331       text->text_properties_end = text->text_properties;
3332       
3333       SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3334       
3335       text->point.index = 0;
3336     }
3337 }
3338
3339
3340 /**********************************************************************/
3341 /*                         Property Movement                          */
3342 /**********************************************************************/
3343
3344 static void
3345 move_mark_n (GtkPropertyMark* mark, gint n)
3346 {
3347   if (n > 0)
3348     advance_mark_n(mark, n);
3349   else if (n < 0)
3350     decrement_mark_n(mark, -n);
3351 }
3352
3353 static void
3354 advance_mark (GtkPropertyMark* mark)
3355 {
3356   TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3357   
3358   mark->index += 1;
3359   
3360   if (prop->length > mark->offset + 1)
3361     mark->offset += 1;
3362   else
3363     {
3364       mark->property = MARK_NEXT_LIST_PTR (mark);
3365       mark->offset   = 0;
3366     }
3367 }
3368
3369 static void
3370 advance_mark_n (GtkPropertyMark* mark, gint n)
3371 {
3372   gint i;
3373   TextProperty* prop;
3374
3375   g_assert (n > 0);
3376
3377   i = 0;                        /* otherwise it migth not be init. */
3378   prop = MARK_CURRENT_PROPERTY(mark);
3379
3380   if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3381     /* to make it easier */
3382     n += (mark->offset);
3383     mark->index -= mark->offset;
3384     mark->offset = 0;
3385     /* first we take seven-mile-leaps to get to the right text
3386      * property. */
3387     while ((n-i) > prop->length - 1) {
3388       i += prop->length;
3389       mark->index += prop->length;
3390       mark->property = MARK_NEXT_LIST_PTR (mark);
3391       prop = MARK_CURRENT_PROPERTY (mark);
3392     }
3393   }
3394
3395   /* and then the rest */
3396   mark->index += n - i;
3397   mark->offset += n - i;
3398 }
3399
3400 static void
3401 decrement_mark (GtkPropertyMark* mark)
3402 {
3403   mark->index -= 1;
3404   
3405   if (mark->offset > 0)
3406     mark->offset -= 1;
3407   else
3408     {
3409       mark->property = MARK_PREV_LIST_PTR (mark);
3410       mark->offset   = MARK_CURRENT_PROPERTY (mark)->length - 1;
3411     }
3412 }
3413
3414 static void
3415 decrement_mark_n (GtkPropertyMark* mark, gint n)
3416 {
3417   g_assert (n > 0);
3418
3419   while (mark->offset < n) {
3420     /* jump to end of prev */
3421     n -= mark->offset + 1;
3422     mark->index -= mark->offset + 1;
3423     mark->property = MARK_PREV_LIST_PTR (mark);
3424     mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3425   }
3426
3427   /* and the rest */
3428   mark->index -= n;
3429   mark->offset -= n;
3430 }
3431  
3432 static GtkPropertyMark
3433 find_mark (GtkText* text, guint mark_position)
3434 {
3435   return find_mark_near (text, mark_position, &text->point);
3436 }
3437
3438 /*
3439  * You can also start from the end, what a drag.
3440  */
3441 static GtkPropertyMark
3442 find_mark_near (GtkText* text, guint mark_position, const GtkPropertyMark* near)
3443 {
3444   gint diffa;
3445   gint diffb;
3446   
3447   GtkPropertyMark mark;
3448   
3449   if (!near)
3450     diffa = mark_position + 1;
3451   else
3452     diffa = mark_position - near->index;
3453   
3454   diffb = mark_position;
3455   
3456   if (diffa < 0)
3457     diffa = -diffa;
3458   
3459   if (diffa <= diffb)
3460     {
3461       mark = *near;
3462     }
3463   else
3464     {
3465       mark.index = 0;
3466       mark.property = text->text_properties;
3467       mark.offset = 0;
3468     }
3469
3470   move_mark_n (&mark, mark_position - mark.index);
3471    
3472   return mark;
3473 }
3474
3475 /* This routine must be called with scroll == FALSE, only when
3476  * point is at least partially on screen
3477  */
3478
3479 static void
3480 find_line_containing_point (GtkText* text, guint point,
3481                             gboolean scroll)
3482 {
3483   GList* cache;
3484   gint height;
3485   
3486   text->current_line = NULL;
3487
3488   TEXT_SHOW (text);
3489
3490   /* Scroll backwards until the point is on screen
3491    */
3492   while (CACHE_DATA(text->line_start_cache).start.index > point)
3493     scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3494
3495   /* Now additionally try to make sure that the point is fully on screen
3496    */
3497   if (scroll)
3498     {
3499       while (text->first_cut_pixels != 0 && 
3500              text->line_start_cache->next &&
3501              CACHE_DATA(text->line_start_cache->next).start.index > point)
3502         scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3503     }
3504
3505   gdk_window_get_size (text->text_area, NULL, &height);
3506   
3507   for (cache = text->line_start_cache; cache; cache = cache->next)
3508     {
3509       guint lph;
3510       
3511       if (CACHE_DATA(cache).end.index >= point ||
3512           LAST_INDEX(text, CACHE_DATA(cache).end))
3513         {
3514           text->current_line = cache; /* LOOK HERE, this proc has an
3515                                        * important side effect. */
3516           return;
3517         }
3518       
3519       TEXT_SHOW_LINE (text, cache, "cache");
3520       
3521       if (cache->next == NULL)
3522         fetch_lines_forward (text, 1);
3523       
3524       if (scroll)
3525         {
3526           lph = pixel_height_of (text, cache->next);
3527           
3528           /* Scroll the bottom of the line is on screen, or until
3529            * the line is the first onscreen line.
3530            */
3531           while (cache->next != text->line_start_cache && lph > height)
3532             {
3533               TEXT_SHOW_LINE (text, cache, "cache");
3534               TEXT_SHOW_LINE (text, cache->next, "cache->next");
3535               scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3536               lph = pixel_height_of (text, cache->next);
3537             }
3538         }
3539     }
3540   
3541   g_assert_not_reached (); /* Must set text->current_line here */
3542 }
3543
3544 static guint
3545 pixel_height_of (GtkText* text, GList* cache_line)
3546 {
3547   gint pixels = - text->first_cut_pixels;
3548   GList *cache = text->line_start_cache;
3549   
3550   while (TRUE) {
3551     pixels += LINE_HEIGHT (CACHE_DATA(cache));
3552     
3553     if (cache->data == cache_line->data)
3554       break;
3555     
3556     cache = cache->next;
3557   }
3558   
3559   return pixels;
3560 }
3561
3562 /**********************************************************************/
3563 /*                      Search and Placement                          */
3564 /**********************************************************************/
3565
3566 static gint
3567 find_char_width (GtkText* text, const GtkPropertyMark *mark, const TabStopMark *tab_mark)
3568 {
3569   GdkWChar ch;
3570   gint16* char_widths;
3571   
3572   if (LAST_INDEX (text, *mark))
3573     return 0;
3574   
3575   ch = GTK_TEXT_INDEX (text, mark->index);
3576   char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3577
3578   if (ch == '\t')
3579     {
3580       return tab_mark->to_next_tab * char_widths[' '];
3581     }
3582   else if (ch < 256)
3583     {
3584       return char_widths[ch];
3585     }
3586   else
3587     {
3588       return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3589     }
3590 }
3591
3592 static void
3593 advance_tab_mark (GtkText* text, TabStopMark* tab_mark, GdkWChar ch)
3594 {
3595   if (tab_mark->to_next_tab == 1 || ch == '\t')
3596     {
3597       if (tab_mark->tab_stops->next)
3598         {
3599           tab_mark->tab_stops = tab_mark->tab_stops->next;
3600           tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3601         }
3602       else
3603         {
3604           tab_mark->to_next_tab = text->default_tab_width;
3605         }
3606     }
3607   else
3608     {
3609       tab_mark->to_next_tab -= 1;
3610     }
3611 }
3612
3613 static void
3614 advance_tab_mark_n (GtkText* text, TabStopMark* tab_mark, gint n)
3615      /* No tabs! */
3616 {
3617   while (n--)
3618     advance_tab_mark (text, tab_mark, 0);
3619 }
3620
3621 static void
3622 find_cursor_at_line (GtkText* text, const LineParams* start_line, gint pixel_height)
3623 {
3624   GdkWChar ch;
3625   
3626   GtkPropertyMark mark        = start_line->start;
3627   TabStopMark  tab_mark    = start_line->tab_cont.tab_start;
3628   gint         pixel_width = LINE_START_PIXEL (*start_line);
3629   
3630   while (mark.index < text->cursor_mark.index)
3631     {
3632       pixel_width += find_char_width (text, &mark, &tab_mark);
3633       
3634       advance_tab_mark (text, &tab_mark, GTK_TEXT_INDEX(text, mark.index));
3635       advance_mark (&mark);
3636     }
3637   
3638   text->cursor_pos_x       = pixel_width;
3639   text->cursor_pos_y       = pixel_height;
3640   text->cursor_char_offset = start_line->font_descent;
3641   text->cursor_mark        = mark;
3642   
3643   ch = LAST_INDEX (text, mark) ? 
3644     LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3645   
3646   if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3647     text->cursor_char = 0;
3648   else
3649     text->cursor_char = ch;
3650 }
3651
3652 static void
3653 find_cursor (GtkText* text, gboolean scroll)
3654 {
3655   if (GTK_WIDGET_REALIZED (text))
3656     {
3657       find_line_containing_point (text, text->cursor_mark.index, scroll);
3658       
3659       if (text->current_line)
3660         find_cursor_at_line (text,
3661                              &CACHE_DATA(text->current_line),
3662                              pixel_height_of(text, text->current_line));
3663     }
3664   
3665   GTK_OLD_EDITABLE (text)->current_pos = text->cursor_mark.index;
3666 }
3667
3668 static void
3669 find_mouse_cursor_at_line (GtkText *text, const LineParams* lp,
3670                            guint line_pixel_height,
3671                            gint button_x)
3672 {
3673   GtkPropertyMark mark     = lp->start;
3674   TabStopMark  tab_mark = lp->tab_cont.tab_start;
3675   
3676   gint char_width = find_char_width(text, &mark, &tab_mark);
3677   gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3678   
3679   text->cursor_pos_y = line_pixel_height;
3680   
3681   for (;;)
3682     {
3683       GdkWChar ch = LAST_INDEX (text, mark) ? 
3684         LINE_DELIM : GTK_TEXT_INDEX (text, mark.index);
3685       
3686       if (button_x < pixel_width || mark.index == lp->end.index)
3687         {
3688           text->cursor_pos_x       = pixel_width - (char_width+1)/2;
3689           text->cursor_mark        = mark;
3690           text->cursor_char_offset = lp->font_descent;
3691           
3692           if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3693             text->cursor_char = 0;
3694           else
3695             text->cursor_char = ch;
3696           
3697           break;
3698         }
3699       
3700       advance_tab_mark (text, &tab_mark, ch);
3701       advance_mark (&mark);
3702       
3703       pixel_width += char_width/2;
3704       
3705       char_width = find_char_width (text, &mark, &tab_mark);
3706       
3707       pixel_width += (char_width+1)/2;
3708     }
3709 }
3710
3711 static void
3712 find_mouse_cursor (GtkText* text, gint x, gint y)
3713 {
3714   gint pixel_height;
3715   GList* cache = text->line_start_cache;
3716   
3717   g_assert (cache);
3718   
3719   pixel_height = - text->first_cut_pixels;
3720   
3721   for (; cache; cache = cache->next)
3722     {
3723       pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
3724       
3725       if (y < pixel_height || !cache->next)
3726         {
3727           find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
3728           
3729           find_cursor (text, FALSE);
3730           
3731           return;
3732         }
3733     }
3734 }
3735
3736 /**********************************************************************/
3737 /*                          Cache Manager                             */
3738 /**********************************************************************/
3739
3740 static void
3741 free_cache (GtkText* text)
3742 {
3743   GList* cache = text->line_start_cache;
3744   
3745   if (cache)
3746     {
3747       while (cache->prev)
3748         cache = cache->prev;
3749       
3750       text->line_start_cache = cache;
3751     }
3752   
3753   for (; cache; cache = cache->next)
3754     g_slice_free (LineParams, cache->data);
3755   
3756   g_list_free (text->line_start_cache);
3757   
3758   text->line_start_cache = NULL;
3759 }
3760
3761 static GList*
3762 remove_cache_line (GtkText* text, GList* member)
3763 {
3764   GList *list;
3765   
3766   if (member == NULL)
3767     return NULL;
3768   
3769   if (member == text->line_start_cache)
3770     text->line_start_cache = text->line_start_cache->next;
3771   
3772   if (member->prev)
3773     member->prev->next = member->next;
3774   
3775   if (member->next)
3776     member->next->prev = member->prev;
3777   
3778   list = member->next;
3779   
3780   g_slice_free (LineParams, member->data);
3781   g_list_free_1 (member);
3782   
3783   return list;
3784 }
3785
3786 /**********************************************************************/
3787 /*                           Key Motion                               */
3788 /**********************************************************************/
3789
3790 static void
3791 move_cursor_buffer_ver (GtkText *text, int dir)
3792 {
3793   undraw_cursor (text, FALSE);
3794   
3795   if (dir > 0)
3796     {
3797       scroll_int (text, text->vadj->upper);
3798       text->cursor_mark = find_this_line_start_mark (text,
3799                                                      TEXT_LENGTH (text),
3800                                                      &text->cursor_mark);
3801     }
3802   else
3803     {
3804       scroll_int (text, - text->vadj->value);
3805       text->cursor_mark = find_this_line_start_mark (text,
3806                                                      0,
3807                                                      &text->cursor_mark);
3808     }
3809   
3810   find_cursor (text, TRUE);
3811   draw_cursor (text, FALSE);
3812 }
3813
3814 static void
3815 move_cursor_page_ver (GtkText *text, int dir)
3816 {
3817   scroll_int (text, dir * text->vadj->page_increment);
3818 }
3819
3820 static void
3821 move_cursor_ver (GtkText *text, int count)
3822 {
3823   gint i;
3824   GtkPropertyMark mark;
3825   gint offset;
3826   
3827   mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
3828   offset = text->cursor_mark.index - mark.index;
3829   
3830   if (offset > text->cursor_virtual_x)
3831     text->cursor_virtual_x = offset;
3832   
3833   if (count < 0)
3834     {
3835       if (mark.index == 0)
3836         return;
3837       
3838       decrement_mark (&mark);
3839       mark = find_this_line_start_mark (text, mark.index, &mark);
3840     }
3841   else
3842     {
3843       mark = text->cursor_mark;
3844       
3845       while (!LAST_INDEX(text, mark) && GTK_TEXT_INDEX(text, mark.index) != LINE_DELIM)
3846         advance_mark (&mark);
3847       
3848       if (LAST_INDEX(text, mark))
3849         return;
3850       
3851       advance_mark (&mark);
3852     }
3853   
3854   for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
3855     if (LAST_INDEX(text, mark) ||
3856         GTK_TEXT_INDEX(text, mark.index) == LINE_DELIM)
3857       break;
3858   
3859   undraw_cursor (text, FALSE);
3860   
3861   text->cursor_mark = mark;
3862   
3863   find_cursor (text, TRUE);
3864   
3865   draw_cursor (text, FALSE);
3866 }
3867
3868 static void
3869 move_cursor_hor (GtkText *text, int count)
3870 {
3871   /* count should be +-1. */
3872   if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
3873        (count < 0 && text->cursor_mark.index < (- count)) ||
3874        (count == 0) )
3875     return;
3876   
3877   text->cursor_virtual_x = 0;
3878   
3879   undraw_cursor (text, FALSE);
3880   
3881   move_mark_n (&text->cursor_mark, count);
3882   
3883   find_cursor (text, TRUE);
3884   
3885   draw_cursor (text, FALSE);
3886 }
3887
3888 static void 
3889 gtk_text_move_cursor (GtkOldEditable *old_editable,
3890                       gint            x,
3891                       gint            y)
3892 {
3893   if (x > 0)
3894     {
3895       while (x-- != 0)
3896         move_cursor_hor (GTK_TEXT (old_editable), 1);
3897     }
3898   else if (x < 0)
3899     {
3900       while (x++ != 0)
3901         move_cursor_hor (GTK_TEXT (old_editable), -1);
3902     }
3903   
3904   if (y > 0)
3905     {
3906       while (y-- != 0)
3907         move_cursor_ver (GTK_TEXT (old_editable), 1);
3908     }
3909   else if (y < 0)
3910     {
3911       while (y++ != 0)
3912         move_cursor_ver (GTK_TEXT (old_editable), -1);
3913     }
3914 }
3915
3916 static void
3917 gtk_text_move_forward_character (GtkText *text)
3918 {
3919   move_cursor_hor (text, 1);
3920 }
3921
3922 static void
3923 gtk_text_move_backward_character (GtkText *text)
3924 {
3925   move_cursor_hor (text, -1);
3926 }
3927
3928 static void
3929 gtk_text_move_next_line (GtkText *text)
3930 {
3931   move_cursor_ver (text, 1);
3932 }
3933
3934 static void
3935 gtk_text_move_previous_line (GtkText *text)
3936 {
3937   move_cursor_ver (text, -1);
3938 }
3939
3940 static void 
3941 gtk_text_move_word (GtkOldEditable *old_editable,
3942                     gint            n)
3943 {
3944   if (n > 0)
3945     {
3946       while (n-- != 0)
3947         gtk_text_move_forward_word (GTK_TEXT (old_editable));
3948     }
3949   else if (n < 0)
3950     {
3951       while (n++ != 0)
3952         gtk_text_move_backward_word (GTK_TEXT (old_editable));
3953     }
3954 }
3955
3956 static void
3957 gtk_text_move_forward_word (GtkText *text)
3958 {
3959   text->cursor_virtual_x = 0;
3960   
3961   undraw_cursor (text, FALSE);
3962   
3963   if (text->use_wchar)
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       while (!LAST_INDEX (text, text->cursor_mark) && 
3970              gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3971         advance_mark (&text->cursor_mark);
3972     }
3973   else
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       while (!LAST_INDEX (text, text->cursor_mark) && 
3980              isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index)))
3981         advance_mark (&text->cursor_mark);
3982     }
3983   
3984   find_cursor (text, TRUE);
3985   draw_cursor (text, FALSE);
3986 }
3987
3988 static void
3989 gtk_text_move_backward_word (GtkText *text)
3990 {
3991   text->cursor_virtual_x = 0;
3992   
3993   undraw_cursor (text, FALSE);
3994   
3995   if (text->use_wchar)
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       while ((text->cursor_mark.index > 0) &&
4002              gdk_iswalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4003         decrement_mark (&text->cursor_mark);
4004     }
4005   else
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       while ((text->cursor_mark.index > 0) &&
4012              isalnum (GTK_TEXT_INDEX(text, text->cursor_mark.index-1)))
4013         decrement_mark (&text->cursor_mark);
4014     }
4015   
4016   find_cursor (text, TRUE);
4017   draw_cursor (text, FALSE);
4018 }
4019
4020 static void 
4021 gtk_text_move_page (GtkOldEditable *old_editable,
4022                     gint            x,
4023                     gint            y)
4024 {
4025   if (y != 0)
4026     scroll_int (GTK_TEXT (old_editable), 
4027                 y * GTK_TEXT(old_editable)->vadj->page_increment);  
4028 }
4029
4030 static void 
4031 gtk_text_move_to_row (GtkOldEditable *old_editable,
4032                       gint            row)
4033 {
4034 }
4035
4036 static void 
4037 gtk_text_move_to_column (GtkOldEditable *old_editable,
4038                          gint            column)
4039 {
4040   GtkText *text;
4041   
4042   text = GTK_TEXT (old_editable);
4043   
4044   text->cursor_virtual_x = 0;   /* FIXME */
4045   
4046   undraw_cursor (text, FALSE);
4047   
4048   /* Move to the beginning of the line */
4049   while ((text->cursor_mark.index > 0) &&
4050          (GTK_TEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4051     decrement_mark (&text->cursor_mark);
4052   
4053   while (!LAST_INDEX (text, text->cursor_mark) &&
4054          (GTK_TEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4055     {
4056       if (column > 0)
4057         column--;
4058       else if (column == 0)
4059         break;
4060       
4061       advance_mark (&text->cursor_mark);
4062     }
4063   
4064   find_cursor (text, TRUE);
4065   draw_cursor (text, FALSE);
4066 }
4067
4068 static void
4069 gtk_text_move_beginning_of_line (GtkText *text)
4070 {
4071   gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4072   
4073 }
4074
4075 static void
4076 gtk_text_move_end_of_line (GtkText *text)
4077 {
4078   gtk_text_move_to_column (GTK_OLD_EDITABLE (text), -1);
4079 }
4080
4081 static void 
4082 gtk_text_kill_char (GtkOldEditable *old_editable,
4083                     gint            direction)
4084 {
4085   GtkText *text;
4086   
4087   text = GTK_TEXT (old_editable);
4088   
4089   if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4090     gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4091   else
4092     {
4093       if (direction >= 0)
4094         {
4095           if (text->point.index + 1 <= TEXT_LENGTH (text))
4096             gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index, text->point.index + 1);
4097         }
4098       else
4099         {
4100           if (text->point.index > 0)
4101             gtk_editable_delete_text (GTK_EDITABLE (old_editable), text->point.index - 1, text->point.index);
4102         }
4103     }
4104 }
4105
4106 static void
4107 gtk_text_delete_forward_character (GtkText *text)
4108 {
4109   gtk_text_kill_char (GTK_OLD_EDITABLE (text), 1);
4110 }
4111
4112 static void
4113 gtk_text_delete_backward_character (GtkText *text)
4114 {
4115   gtk_text_kill_char (GTK_OLD_EDITABLE (text), -1);
4116 }
4117
4118 static void 
4119 gtk_text_kill_word (GtkOldEditable *old_editable,
4120                     gint            direction)
4121 {
4122   if (old_editable->selection_start_pos != old_editable->selection_end_pos)
4123     gtk_editable_delete_selection (GTK_EDITABLE (old_editable));
4124   else
4125     {
4126       gint old_pos = old_editable->current_pos;
4127       if (direction >= 0)
4128         {
4129           gtk_text_move_word (old_editable, 1);
4130           gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4131         }
4132       else
4133         {
4134           gtk_text_move_word (old_editable, -1);
4135           gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4136         }
4137     }
4138 }
4139
4140 static void
4141 gtk_text_delete_forward_word (GtkText *text)
4142 {
4143   gtk_text_kill_word (GTK_OLD_EDITABLE (text), 1);
4144 }
4145
4146 static void
4147 gtk_text_delete_backward_word (GtkText *text)
4148 {
4149   gtk_text_kill_word (GTK_OLD_EDITABLE (text), -1);
4150 }
4151
4152 static void 
4153 gtk_text_kill_line (GtkOldEditable *old_editable,
4154                     gint            direction)
4155 {
4156   gint old_pos = old_editable->current_pos;
4157   if (direction >= 0)
4158     {
4159       gtk_text_move_to_column (old_editable, -1);
4160       gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_pos, old_editable->current_pos);
4161     }
4162   else
4163     {
4164       gtk_text_move_to_column (old_editable, 0);
4165       gtk_editable_delete_text (GTK_EDITABLE (old_editable), old_editable->current_pos, old_pos);
4166     }
4167 }
4168
4169 static void
4170 gtk_text_delete_line (GtkText *text)
4171 {
4172   gtk_text_move_to_column (GTK_OLD_EDITABLE (text), 0);
4173   gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4174 }
4175
4176 static void
4177 gtk_text_delete_to_line_end (GtkText *text)
4178 {
4179   gtk_text_kill_line (GTK_OLD_EDITABLE (text), 1);
4180 }
4181
4182 static void
4183 gtk_text_select_word (GtkText *text, guint32 time)
4184 {
4185   gint start_pos;
4186   gint end_pos;
4187   
4188   GtkOldEditable *old_editable;
4189   old_editable = GTK_OLD_EDITABLE (text);
4190   
4191   gtk_text_move_backward_word (text);
4192   start_pos = text->cursor_mark.index;
4193   
4194   gtk_text_move_forward_word (text);
4195   end_pos = text->cursor_mark.index;
4196   
4197   old_editable->has_selection = TRUE;
4198   gtk_text_set_selection (old_editable, start_pos, end_pos);
4199   gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4200 }
4201
4202 static void
4203 gtk_text_select_line (GtkText *text, guint32 time)
4204 {
4205   gint start_pos;
4206   gint end_pos;
4207   
4208   GtkOldEditable *old_editable;
4209   old_editable = GTK_OLD_EDITABLE (text);
4210   
4211   gtk_text_move_beginning_of_line (text);
4212   start_pos = text->cursor_mark.index;
4213   
4214   gtk_text_move_end_of_line (text);
4215   gtk_text_move_forward_character (text);
4216   end_pos = text->cursor_mark.index;
4217   
4218   old_editable->has_selection = TRUE;
4219   gtk_text_set_selection (old_editable, start_pos, end_pos);
4220   gtk_old_editable_claim_selection (old_editable, start_pos != end_pos, time);
4221 }
4222
4223 /**********************************************************************/
4224 /*                            Scrolling                               */
4225 /**********************************************************************/
4226
4227 static void
4228 adjust_adj (GtkText* text, GtkAdjustment* adj)
4229 {
4230   gint height;
4231   
4232   gdk_window_get_size (text->text_area, NULL, &height);
4233   
4234   adj->step_increment = MIN (adj->upper, SCROLL_PIXELS);
4235   adj->page_increment = MIN (adj->upper, height - KEY_SCROLL_PIXELS);
4236   adj->page_size      = MIN (adj->upper, height);
4237   adj->value          = MIN (adj->value, adj->upper - adj->page_size);
4238   adj->value          = MAX (adj->value, 0.0);
4239   
4240   gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4241 }
4242
4243 static gint
4244 set_vertical_scroll_iterator (GtkText* text, LineParams* lp, void* data)
4245 {
4246   SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4247   
4248   if ((text->first_line_start_index >= lp->start.index) &&
4249       (text->first_line_start_index <= lp->end.index))
4250     {
4251       svdata->mark = lp->start;
4252   
4253       if (text->first_line_start_index == lp->start.index)
4254         {
4255           text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4256         }
4257       else
4258         {
4259           text->first_onscreen_ver_pixel = svdata->pixel_height;
4260           text->first_cut_pixels = 0;
4261         }
4262       
4263       text->vadj->value = text->first_onscreen_ver_pixel;
4264     }
4265   
4266   svdata->pixel_height += LINE_HEIGHT (*lp);
4267   
4268   return FALSE;
4269 }
4270
4271 static gint
4272 set_vertical_scroll_find_iterator (GtkText* text, LineParams* lp, void* data)
4273 {
4274   SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4275   gint return_val;
4276   
4277   if (svdata->pixel_height <= (gint) text->vadj->value &&
4278       svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4279     {
4280       svdata->mark = lp->start;
4281       
4282       text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4283       text->first_onscreen_ver_pixel = svdata->pixel_height;
4284       text->first_line_start_index = lp->start.index;
4285       
4286       return_val = TRUE;
4287     }
4288   else
4289     {
4290       svdata->pixel_height += LINE_HEIGHT (*lp);
4291       
4292       return_val = FALSE;
4293     }
4294   
4295   return return_val;
4296 }
4297
4298 static GtkPropertyMark
4299 set_vertical_scroll (GtkText* text)
4300 {
4301   GtkPropertyMark mark = find_mark (text, 0);
4302   SetVerticalScrollData data;
4303   gint height;
4304   gint orig_value;
4305   
4306   data.pixel_height = 0;
4307   line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4308   
4309   text->vadj->upper = data.pixel_height;
4310   orig_value = (gint) text->vadj->value;
4311   
4312   gdk_window_get_size (text->text_area, NULL, &height);
4313   
4314   text->vadj->step_increment = MIN (text->vadj->upper, SCROLL_PIXELS);
4315   text->vadj->page_increment = MIN (text->vadj->upper, height - KEY_SCROLL_PIXELS);
4316   text->vadj->page_size      = MIN (text->vadj->upper, height);
4317   text->vadj->value          = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4318   text->vadj->value          = MAX (text->vadj->value, 0.0);
4319   
4320   text->last_ver_value = (gint)text->vadj->value;
4321   
4322   gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4323   
4324   if (text->vadj->value != orig_value)
4325     {
4326       /* We got clipped, and don't really know which line to put first. */
4327       data.pixel_height = 0;
4328       data.last_didnt_wrap = TRUE;
4329       
4330       line_params_iterate (text, &mark, NULL,
4331                            FALSE, &data,
4332                            set_vertical_scroll_find_iterator);
4333     }
4334
4335   return data.mark;
4336 }
4337
4338 static void
4339 scroll_int (GtkText* text, gint diff)
4340 {
4341   gdouble upper;
4342   
4343   text->vadj->value += diff;
4344   
4345   upper = text->vadj->upper - text->vadj->page_size;
4346   text->vadj->value = MIN (text->vadj->value, upper);
4347   text->vadj->value = MAX (text->vadj->value, 0.0);
4348   
4349   gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4350 }
4351
4352 static void 
4353 process_exposes (GtkText *text)
4354 {
4355   GdkEvent *event;
4356   
4357   /* Make sure graphics expose events are processed before scrolling
4358    * again */
4359   
4360   while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4361     {
4362       gtk_widget_send_expose (GTK_WIDGET (text), event);
4363       if (event->expose.count == 0)
4364         {
4365           gdk_event_free (event);
4366           break;
4367         }
4368       gdk_event_free (event);
4369     }
4370 }
4371
4372 static gint last_visible_line_height (GtkText* text)
4373 {
4374   GList *cache = text->line_start_cache;
4375   gint height;
4376   
4377   gdk_window_get_size (text->text_area, NULL, &height);
4378   
4379   for (; cache->next; cache = cache->next)
4380     if (pixel_height_of(text, cache->next) > height)
4381       break;
4382   
4383   if (cache)
4384     return pixel_height_of(text, cache) - 1;
4385   else
4386     return 0;
4387 }
4388
4389 static gint first_visible_line_height (GtkText* text)
4390 {
4391   if (text->first_cut_pixels)
4392     return pixel_height_of(text, text->line_start_cache) + 1;
4393   else
4394     return 1;
4395 }
4396
4397 static void
4398 scroll_down (GtkText* text, gint diff0)
4399 {
4400   GdkRectangle rect;
4401   gint real_diff = 0;
4402   gint width, height;
4403   
4404   text->first_onscreen_ver_pixel += diff0;
4405   
4406   while (diff0-- > 0)
4407     {
4408       g_assert (text->line_start_cache);
4409       
4410       if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4411         {
4412           text->first_cut_pixels += 1;
4413         }
4414       else
4415         {
4416           text->first_cut_pixels = 0;
4417           
4418           text->line_start_cache = text->line_start_cache->next;
4419           
4420           text->first_line_start_index =
4421             CACHE_DATA(text->line_start_cache).start.index;
4422           
4423           if (!text->line_start_cache->next)
4424             fetch_lines_forward (text, 1);
4425         }
4426       
4427       real_diff += 1;
4428     }
4429   
4430   gdk_window_get_size (text->text_area, &width, &height);
4431   if (height > real_diff)
4432     gdk_draw_pixmap (text->text_area,
4433                      text->gc,
4434                      text->text_area,
4435                      0,
4436                      real_diff,
4437                      0,
4438                      0,
4439                      width,
4440                      height - real_diff);
4441   
4442   rect.x      = 0;
4443   rect.y      = MAX (0, height - real_diff);
4444   rect.width  = width;
4445   rect.height = MIN (height, real_diff);
4446   
4447   expose_text (text, &rect, FALSE);
4448   gtk_text_draw_focus ( (GtkWidget *) text);
4449   
4450   if (text->current_line)
4451     {
4452       gint cursor_min;
4453       
4454       text->cursor_pos_y -= real_diff;
4455       cursor_min = drawn_cursor_min(text);
4456       
4457       if (cursor_min < 0)
4458         find_mouse_cursor (text, text->cursor_pos_x,
4459                            first_visible_line_height (text));
4460     }
4461   
4462   if (height > real_diff)
4463     process_exposes (text);
4464 }
4465
4466 static void
4467 scroll_up (GtkText* text, gint diff0)
4468 {
4469   gint real_diff = 0;
4470   GdkRectangle rect;
4471   gint width, height;
4472   
4473   text->first_onscreen_ver_pixel += diff0;
4474   
4475   while (diff0++ < 0)
4476     {
4477       g_assert (text->line_start_cache);
4478       
4479       if (text->first_cut_pixels > 0)
4480         {
4481           text->first_cut_pixels -= 1;
4482         }
4483       else
4484         {
4485           if (!text->line_start_cache->prev)
4486             fetch_lines_backward (text);
4487           
4488           text->line_start_cache = text->line_start_cache->prev;
4489           
4490           text->first_line_start_index =
4491             CACHE_DATA(text->line_start_cache).start.index;
4492           
4493           text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
4494         }
4495       
4496       real_diff += 1;
4497     }
4498   
4499   gdk_window_get_size (text->text_area, &width, &height);
4500   if (height > real_diff)
4501     gdk_draw_pixmap (text->text_area,
4502                      text->gc,
4503                      text->text_area,
4504                      0,
4505                      0,
4506                      0,
4507                      real_diff,
4508                      width,
4509                      height - real_diff);
4510   
4511   rect.x      = 0;
4512   rect.y      = 0;
4513   rect.width  = width;
4514   rect.height = MIN (height, real_diff);
4515   
4516   expose_text (text, &rect, FALSE);
4517   gtk_text_draw_focus ( (GtkWidget *) text);
4518   
4519   if (text->current_line)
4520     {
4521       gint cursor_max;
4522       gint height;
4523       
4524       text->cursor_pos_y += real_diff;
4525       cursor_max = drawn_cursor_max(text);
4526       gdk_window_get_size (text->text_area, NULL, &height);
4527       
4528       if (cursor_max >= height)
4529         find_mouse_cursor (text, text->cursor_pos_x,
4530                            last_visible_line_height (text));
4531     }
4532   
4533   if (height > real_diff)
4534     process_exposes (text);
4535 }
4536
4537 /**********************************************************************/
4538 /*                            Display Code                            */
4539 /**********************************************************************/
4540
4541 /* Assumes mark starts a line.  Calculates the height, width, and
4542  * displayable character count of a single DISPLAYABLE line.  That
4543  * means that in line-wrap mode, this does may not compute the
4544  * properties of an entire line. */
4545 static LineParams
4546 find_line_params (GtkText* text,
4547                   const GtkPropertyMark* mark,
4548                   const PrevTabCont *tab_cont,
4549                   PrevTabCont *next_cont)
4550 {
4551   LineParams lp;
4552   TabStopMark tab_mark = tab_cont->tab_start;
4553   guint max_display_pixels;
4554   GdkWChar ch;
4555   gint ch_width;
4556   GdkFont *font;
4557   
4558   gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
4559   max_display_pixels -= LINE_WRAP_ROOM;
4560   
4561   lp.wraps             = 0;
4562   lp.tab_cont          = *tab_cont;
4563   lp.start             = *mark;
4564   lp.end               = *mark;
4565   lp.pixel_width       = tab_cont->pixel_offset;
4566   lp.displayable_chars = 0;
4567   lp.font_ascent       = 0;
4568   lp.font_descent      = 0;
4569   
4570   init_tab_cont (text, next_cont);
4571   
4572   while (!LAST_INDEX(text, lp.end))
4573     {
4574       g_assert (lp.end.property);
4575       
4576       ch   = GTK_TEXT_INDEX (text, lp.end.index);
4577       font = MARK_CURRENT_FONT (text, &lp.end);
4578
4579       if (ch == LINE_DELIM)
4580         {
4581           /* Newline doesn't count in computation of line height, even
4582            * if its in a bigger font than the rest of the line.  Unless,
4583            * of course, there are no other characters. */
4584           
4585           if (!lp.font_ascent && !lp.font_descent)
4586             {
4587               lp.font_ascent = font->ascent;
4588               lp.font_descent = font->descent;
4589             }
4590           
4591           lp.tab_cont_next = *next_cont;
4592           
4593           return lp;
4594         }
4595       
4596       ch_width = find_char_width (text, &lp.end, &tab_mark);
4597       
4598       if ((ch_width + lp.pixel_width > max_display_pixels) &&
4599           (lp.end.index > lp.start.index))
4600         {
4601           lp.wraps = 1;
4602           
4603           if (text->line_wrap)
4604             {
4605               next_cont->tab_start    = tab_mark;
4606               next_cont->pixel_offset = 0;
4607               
4608               if (ch == '\t')
4609                 {
4610                   /* Here's the tough case, a tab is wrapping. */
4611                   gint pixels_avail = max_display_pixels - lp.pixel_width;
4612                   gint space_width  = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
4613                   gint spaces_avail = pixels_avail / space_width;
4614                   
4615                   if (spaces_avail == 0)
4616                     {
4617                       decrement_mark (&lp.end);
4618                     }
4619                   else
4620                     {
4621                       advance_tab_mark (text, &next_cont->tab_start, '\t');
4622                       next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
4623                                                                spaces_avail);
4624                       lp.displayable_chars += 1;
4625                     }
4626                 }
4627               else
4628                 {
4629                   if (text->word_wrap)
4630                     {
4631                       GtkPropertyMark saved_mark = lp.end;
4632                       guint saved_characters = lp.displayable_chars;
4633                       
4634                       lp.displayable_chars += 1;
4635                       
4636                       if (text->use_wchar)
4637                         {
4638                           while (!gdk_iswspace (GTK_TEXT_INDEX (text, lp.end.index)) &&
4639                                  (lp.end.index > lp.start.index))
4640                             {
4641                               decrement_mark (&lp.end);
4642                               lp.displayable_chars -= 1;
4643                             }
4644                         }
4645                       else
4646                         {
4647                           while (!isspace(GTK_TEXT_INDEX (text, lp.end.index)) &&
4648                                  (lp.end.index > lp.start.index))
4649                             {
4650                               decrement_mark (&lp.end);
4651                               lp.displayable_chars -= 1;
4652                             }
4653                         }
4654                       
4655                       /* If whole line is one word, revert to char wrapping */
4656                       if (lp.end.index == lp.start.index)
4657                         {
4658                           lp.end = saved_mark;
4659                           lp.displayable_chars = saved_characters;
4660                           decrement_mark (&lp.end);
4661                         }
4662                     }
4663                   else
4664                     {
4665                       /* Don't include this character, it will wrap. */
4666                       decrement_mark (&lp.end);
4667                     }
4668                 }
4669               
4670               lp.tab_cont_next = *next_cont;
4671               
4672               return lp;
4673             }
4674         }
4675       else
4676         {
4677           lp.displayable_chars += 1;
4678         }
4679       
4680       lp.font_ascent = MAX (font->ascent, lp.font_ascent);
4681       lp.font_descent = MAX (font->descent, lp.font_descent);
4682       lp.pixel_width  += ch_width;
4683       
4684       advance_mark(&lp.end);
4685       advance_tab_mark (text, &tab_mark, ch);
4686     }
4687   
4688   if (LAST_INDEX(text, lp.start))
4689     {
4690       /* Special case, empty last line. */
4691       font = MARK_CURRENT_FONT (text, &lp.end);
4692
4693       lp.font_ascent = font->ascent;
4694       lp.font_descent = font->descent;
4695     }
4696   
4697   lp.tab_cont_next = *next_cont;
4698   
4699   return lp;
4700 }
4701
4702 static void
4703 expand_scratch_buffer (GtkText* text, guint len)
4704 {
4705   if (len >= text->scratch_buffer_len)
4706     {
4707       guint i = 1;
4708       
4709       while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
4710       
4711       if (text->use_wchar)
4712         {
4713           if (text->scratch_buffer.wc)
4714             text->scratch_buffer.wc = g_new (GdkWChar, i);
4715           else
4716             text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
4717                                               i*sizeof (GdkWChar));
4718         }
4719       else
4720         {
4721           if (text->scratch_buffer.ch)
4722             text->scratch_buffer.ch = g_new (guchar, i);
4723           else
4724             text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
4725         }
4726       
4727       text->scratch_buffer_len = i;
4728     }
4729 }
4730
4731 /* Side effect: modifies text->gc
4732  */
4733 static void
4734 draw_bg_rect (GtkText* text, GtkPropertyMark *mark,
4735               gint x, gint y, gint width, gint height,
4736               gboolean already_cleared)
4737 {
4738   GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4739
4740   if ((mark->index >= MIN(old_editable->selection_start_pos, old_editable->selection_end_pos) &&
4741        mark->index < MAX(old_editable->selection_start_pos, old_editable->selection_end_pos)))
4742     {
4743       gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
4744                          old_editable->has_selection ?
4745                             GTK_STATE_SELECTED : GTK_STATE_ACTIVE, 
4746                          GTK_SHADOW_NONE,
4747                          NULL, GTK_WIDGET(text), "text",
4748                          x, y, width, height);
4749     }
4750   else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
4751                             &GTK_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
4752     {
4753       gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
4754
4755       gdk_draw_rectangle (text->text_area,
4756                           text->gc,
4757                           TRUE, x, y, width, height);
4758     }
4759   else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
4760     {
4761       GdkRectangle rect;
4762       
4763       rect.x = x;
4764       rect.y = y;
4765       rect.width = width;
4766       rect.height = height;
4767       
4768       clear_area (text, &rect);
4769     }
4770   else if (!already_cleared)
4771     gdk_window_clear_area (text->text_area, x, y, width, height);
4772 }
4773
4774 static void
4775 draw_line (GtkText* text,
4776            gint pixel_start_height,
4777            LineParams* lp)
4778 {
4779   GdkGCValues gc_values;
4780   gint i;
4781   gint len = 0;
4782   guint running_offset = lp->tab_cont.pixel_offset;
4783   union { GdkWChar *wc; guchar *ch; } buffer;
4784   GdkGC *fg_gc;
4785   
4786   GtkOldEditable *old_editable = GTK_OLD_EDITABLE (text);
4787   
4788   guint selection_start_pos = MIN (old_editable->selection_start_pos, old_editable->selection_end_pos);
4789   guint selection_end_pos = MAX (old_editable->selection_start_pos, old_editable->selection_end_pos);
4790   
4791   GtkPropertyMark mark = lp->start;
4792   TabStopMark tab_mark = lp->tab_cont.tab_start;
4793   gint pixel_height = pixel_start_height + lp->font_ascent;
4794   guint chars = lp->displayable_chars;
4795   
4796   /* First provide a contiguous segment of memory.  This makes reading
4797    * the code below *much* easier, and only incurs the cost of copying
4798    * when the line being displayed spans the gap. */
4799   if (mark.index <= text->gap_position &&
4800       mark.index + chars > text->gap_position)
4801     {
4802       expand_scratch_buffer (text, chars);
4803       
4804       if (text->use_wchar)
4805         {
4806           for (i = 0; i < chars; i += 1)
4807             text->scratch_buffer.wc[i] = GTK_TEXT_INDEX(text, mark.index + i);
4808           buffer.wc = text->scratch_buffer.wc;
4809         }
4810       else
4811         {
4812           for (i = 0; i < chars; i += 1)
4813             text->scratch_buffer.ch[i] = GTK_TEXT_INDEX(text, mark.index + i);
4814           buffer.ch = text->scratch_buffer.ch;
4815         }
4816     }
4817   else
4818     {
4819       if (text->use_wchar)
4820         {
4821           if (mark.index >= text->gap_position)
4822             buffer.wc = text->text.wc + mark.index + text->gap_size;
4823           else
4824             buffer.wc = text->text.wc + mark.index;
4825         }
4826       else
4827         {
4828           if (mark.index >= text->gap_position)
4829             buffer.ch = text->text.ch + mark.index + text->gap_size;
4830           else
4831             buffer.ch = text->text.ch + mark.index;
4832         }
4833     }
4834   
4835   
4836   if (running_offset > 0)
4837     {
4838       draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
4839                     LINE_HEIGHT (*lp), TRUE);
4840     }
4841   
4842   while (chars > 0)
4843     {
4844       len = 0;
4845       if ((text->use_wchar && buffer.wc[0] != '\t') ||
4846           (!text->use_wchar && buffer.ch[0] != '\t'))
4847         {
4848           union { GdkWChar *wc; guchar *ch; } next_tab;
4849           gint pixel_width;
4850           GdkFont *font;
4851
4852           next_tab.wc = NULL;
4853           if (text->use_wchar)
4854             for (i=0; i<chars; i++)
4855               {
4856                 if (buffer.wc[i] == '\t')
4857                   {
4858                     next_tab.wc = buffer.wc + i;
4859                     break;
4860                   }
4861               }
4862           else
4863             next_tab.ch = memchr (buffer.ch, '\t', chars);
4864
4865           len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
4866           
4867           if (text->use_wchar)
4868             {
4869               if (next_tab.wc)
4870                 len = MIN (len, next_tab.wc - buffer.wc);
4871             }
4872           else
4873             {
4874               if (next_tab.ch)
4875                 len = MIN (len, next_tab.ch - buffer.ch);
4876             }
4877
4878           if (mark.index < selection_start_pos)
4879             len = MIN (len, selection_start_pos - mark.index);
4880           else if (mark.index < selection_end_pos)
4881             len = MIN (len, selection_end_pos - mark.index);
4882
4883           font = MARK_CURRENT_FONT (text, &mark);
4884           if (font->type == GDK_FONT_FONT)
4885             {
4886               gdk_gc_set_font (text->gc, font);
4887               gdk_gc_get_values (text->gc, &gc_values);
4888               if (text->use_wchar)
4889                 pixel_width = gdk_text_width_wc (gc_values.font,
4890                                                  buffer.wc, len);
4891               else
4892               pixel_width = gdk_text_width (gc_values.font,
4893                                               buffer.ch, len);
4894             }
4895           else
4896             {
4897               if (text->use_wchar)
4898                 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
4899               else
4900                 pixel_width = gdk_text_width (font, buffer.ch, len);
4901             }
4902           
4903           draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4904                         pixel_width, LINE_HEIGHT (*lp), TRUE);
4905           
4906           if ((mark.index >= selection_start_pos) && 
4907               (mark.index < selection_end_pos))
4908             {
4909               if (old_editable->has_selection)
4910                 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_SELECTED];
4911               else
4912                 fg_gc = GTK_WIDGET(text)->style->text_gc[GTK_STATE_ACTIVE];
4913             }
4914           else
4915             {
4916               gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
4917               fg_gc = text->gc;
4918             }
4919
4920           if (text->use_wchar)
4921             gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
4922                               fg_gc,
4923                               running_offset,
4924                               pixel_height,
4925                               buffer.wc,
4926                               len);
4927           else
4928             gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
4929                            fg_gc,
4930                            running_offset,
4931                            pixel_height,
4932                            buffer.ch,
4933                            len);
4934           
4935           running_offset += pixel_width;
4936           
4937           advance_tab_mark_n (text, &tab_mark, len);
4938         }
4939       else
4940         {
4941           gint pixels_remaining;
4942           gint space_width;
4943           gint spaces_avail;
4944               
4945           len = 1;
4946           
4947           gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
4948           pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
4949           
4950           space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4951           
4952           spaces_avail = pixels_remaining / space_width;
4953           spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
4954
4955           draw_bg_rect (text, &mark, running_offset, pixel_start_height,
4956                         spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
4957
4958           running_offset += tab_mark.to_next_tab *
4959             MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
4960
4961           advance_tab_mark (text, &tab_mark, '\t');
4962         }
4963       
4964       advance_mark_n (&mark, len);
4965       if (text->use_wchar)
4966         buffer.wc += len;
4967       else
4968         buffer.ch += len;
4969       chars -= len;
4970     }
4971 }
4972
4973 static void
4974 draw_line_wrap (GtkText* text, guint height /* baseline height */)
4975 {
4976   gint width;
4977   GdkPixmap *bitmap;
4978   gint bitmap_width;
4979   gint bitmap_height;
4980   
4981   if (text->line_wrap)
4982     {
4983       bitmap = text->line_wrap_bitmap;
4984       bitmap_width = line_wrap_width;
4985       bitmap_height = line_wrap_height;
4986     }
4987   else
4988     {
4989       bitmap = text->line_arrow_bitmap;
4990       bitmap_width = line_arrow_width;
4991       bitmap_height = line_arrow_height;
4992     }
4993   
4994   gdk_window_get_size (text->text_area, &width, NULL);
4995   width -= LINE_WRAP_ROOM;
4996   
4997   gdk_gc_set_stipple (text->gc,
4998                       bitmap);
4999   
5000   gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5001   
5002   gdk_gc_set_foreground (text->gc, &GTK_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5003   
5004   gdk_gc_set_ts_origin (text->gc,
5005                         width + 1,
5006                         height - bitmap_height - 1);
5007   
5008   gdk_draw_rectangle (text->text_area,
5009                       text->gc,
5010                       TRUE,
5011                       width + 1,
5012                       height - bitmap_height - 1 /* one pixel above the baseline. */,
5013                       bitmap_width,
5014                       bitmap_height);
5015   
5016   gdk_gc_set_ts_origin (text->gc, 0, 0);
5017   
5018   gdk_gc_set_fill (text->gc, GDK_SOLID);
5019 }
5020
5021 static void
5022 undraw_cursor (GtkText* text, gint absolute)
5023 {
5024   GtkOldEditable *old_editable = (GtkOldEditable *) text;
5025
5026   TDEBUG (("in undraw_cursor\n"));
5027   
5028   if (absolute)
5029     text->cursor_drawn_level = 0;
5030   
5031   if ((text->cursor_drawn_level ++ == 0) &&
5032       (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5033       GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5034     {
5035       GdkFont* font;
5036       
5037       g_assert(text->cursor_mark.property);
5038
5039       font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5040
5041       draw_bg_rect (text, &text->cursor_mark, 
5042                     text->cursor_pos_x,
5043                     text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5044                     1, font->ascent + 1, FALSE);
5045       
5046       if (text->cursor_char)
5047         {
5048           if (font->type == GDK_FONT_FONT)
5049             gdk_gc_set_font (text->gc, font);
5050
5051           gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5052
5053           gdk_draw_text_wc (text->text_area, font,
5054                          text->gc,
5055                          text->cursor_pos_x,
5056                          text->cursor_pos_y - text->cursor_char_offset,
5057                          &text->cursor_char,
5058                          1);
5059         }
5060     }
5061 }
5062
5063 static gint
5064 drawn_cursor_min (GtkText* text)
5065 {
5066   GdkFont* font;
5067   
5068   g_assert(text->cursor_mark.property);
5069   
5070   font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5071   
5072   return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5073 }
5074
5075 static gint
5076 drawn_cursor_max (GtkText* text)
5077 {
5078   g_assert(text->cursor_mark.property);
5079   
5080   return text->cursor_pos_y - text->cursor_char_offset;
5081 }
5082
5083 static void
5084 draw_cursor (GtkText* text, gint absolute)
5085 {
5086   GtkOldEditable *old_editable = (GtkOldEditable *)text;
5087   
5088   TDEBUG (("in draw_cursor\n"));
5089   
5090   if (absolute)
5091     text->cursor_drawn_level = 1;
5092   
5093   if ((--text->cursor_drawn_level == 0) &&
5094       old_editable->editable &&
5095       (old_editable->selection_start_pos == old_editable->selection_end_pos) &&
5096       GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5097     {
5098       GdkFont* font;
5099       
5100       g_assert (text->cursor_mark.property);
5101
5102       font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5103
5104       gdk_gc_set_foreground (text->gc, &GTK_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5105       
5106       gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x,
5107                      text->cursor_pos_y - text->cursor_char_offset,
5108                      text->cursor_pos_x,
5109                      text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5110     }
5111 }
5112
5113 static GdkGC *
5114 create_bg_gc (GtkText *text)
5115 {
5116   GdkGCValues values;
5117   
5118   values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5119   values.fill = GDK_TILED;
5120
5121   return gdk_gc_new_with_values (text->text_area, &values,
5122                                  GDK_GC_FILL | GDK_GC_TILE);
5123 }
5124
5125 static void
5126 clear_area (GtkText *text, GdkRectangle *area)
5127 {
5128   GtkWidget *widget = GTK_WIDGET (text);
5129   
5130   if (text->bg_gc)
5131     {
5132       gint width, height;
5133       
5134       gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5135       
5136       gdk_gc_set_ts_origin (text->bg_gc,
5137                             (- text->first_onscreen_hor_pixel) % width,
5138                             (- text->first_onscreen_ver_pixel) % height);
5139
5140       gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5141                           area->x, area->y, area->width, area->height);
5142     }
5143   else
5144     gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5145 }
5146
5147 static void
5148 expose_text (GtkText* text, GdkRectangle *area, gboolean cursor)
5149 {
5150   GList *cache = text->line_start_cache;
5151   gint pixels = - text->first_cut_pixels;
5152   gint min_y = MAX (0, area->y);
5153   gint max_y = MAX (0, area->y + area->height);
5154   gint height;
5155   
5156   gdk_window_get_size (text->text_area, NULL, &height);
5157   max_y = MIN (max_y, height);
5158   
5159   TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5160   
5161   clear_area (text, area);
5162   
5163   for (; pixels < height; cache = cache->next)
5164     {
5165       if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5166         {
5167           draw_line (text, pixels, &CACHE_DATA(cache));
5168           
5169           if (CACHE_DATA(cache).wraps)
5170             draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5171         }
5172       
5173       if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5174         {
5175           if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5176               CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5177             {
5178               /* We undraw and draw the cursor here to get the drawn
5179                * level right ... FIXME - maybe the second parameter
5180                * of draw_cursor should work differently
5181                */
5182               undraw_cursor (text, FALSE);
5183               draw_cursor (text, FALSE);
5184             }
5185         }
5186       
5187       pixels += LINE_HEIGHT(CACHE_DATA(cache));
5188       
5189       if (!cache->next)
5190         {
5191           fetch_lines_forward (text, 1);
5192           
5193           if (!cache->next)
5194             break;
5195         }
5196     }
5197 }
5198
5199 static void 
5200 gtk_text_update_text (GtkOldEditable    *old_editable,
5201                       gint               start_pos,
5202                       gint               end_pos)
5203 {
5204   GtkText *text = GTK_TEXT (old_editable);
5205   
5206   GList *cache = text->line_start_cache;
5207   gint pixels = - text->first_cut_pixels;
5208   GdkRectangle area;
5209   gint width;
5210   gint height;
5211   
5212   if (end_pos < 0)
5213     end_pos = TEXT_LENGTH (text);
5214   
5215   if (end_pos < start_pos)
5216     return;
5217   
5218   gdk_window_get_size (text->text_area, &width, &height);
5219   area.x = 0;
5220   area.y = -1;
5221   area.width = width;
5222   area.height = 0;
5223   
5224   TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5225   
5226   for (; pixels < height; cache = cache->next)
5227     {
5228       if (CACHE_DATA(cache).start.index < end_pos)
5229         {
5230           if (CACHE_DATA(cache).end.index >= start_pos)
5231             {
5232               if (area.y < 0)
5233                 area.y = MAX(0,pixels);
5234               area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5235             }
5236         }
5237       else
5238         break;
5239       
5240       pixels += LINE_HEIGHT(CACHE_DATA(cache));
5241       
5242       if (!cache->next)
5243         {
5244           fetch_lines_forward (text, 1);
5245           
5246           if (!cache->next)
5247             break;
5248         }
5249     }
5250   
5251   if (area.y >= 0)
5252     expose_text (text, &area, TRUE);
5253 }
5254
5255 static void
5256 recompute_geometry (GtkText* text)
5257 {
5258   GtkPropertyMark mark, start_mark;
5259   GList *new_lines;
5260   gint height;
5261   gint width;
5262   
5263   free_cache (text);
5264   
5265   mark = start_mark = set_vertical_scroll (text);
5266
5267   /* We need a real start of a line when calling fetch_lines().
5268    * not the start of a wrapped line.
5269    */
5270   while (mark.index > 0 &&
5271          GTK_TEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5272     decrement_mark (&mark);
5273
5274   gdk_window_get_size (text->text_area, &width, &height);
5275
5276   /* Fetch an entire line, to make sure that we get all the text
5277    * we backed over above, in addition to enough text to fill up
5278    * the space vertically
5279    */
5280
5281   new_lines = fetch_lines (text,
5282                            &mark,
5283                            NULL,
5284                            FetchLinesCount,
5285                            1);
5286
5287   mark = CACHE_DATA (g_list_last (new_lines)).end;
5288   if (!LAST_INDEX (text, mark))
5289     {
5290       advance_mark (&mark);
5291
5292       new_lines = g_list_concat (new_lines, 
5293                                  fetch_lines (text,
5294                                               &mark,
5295                                               NULL,
5296                                               FetchLinesPixels,
5297                                               height + text->first_cut_pixels));
5298     }
5299
5300   /* Now work forward to the actual first onscreen line */
5301
5302   while (CACHE_DATA (new_lines).start.index < start_mark.index)
5303     new_lines = new_lines->next;
5304   
5305   text->line_start_cache = new_lines;
5306   
5307   find_cursor (text, TRUE);
5308 }
5309
5310 /**********************************************************************/
5311 /*                            Selection                               */
5312 /**********************************************************************/
5313
5314 static void 
5315 gtk_text_set_selection  (GtkOldEditable  *old_editable,
5316                          gint             start,
5317                          gint             end)
5318 {
5319   GtkText *text = GTK_TEXT (old_editable);
5320   
5321   guint start1, end1, start2, end2;
5322   
5323   if (end < 0)
5324     end = TEXT_LENGTH (text);
5325   
5326   start1 = MIN(start,end);
5327   end1 = MAX(start,end);
5328   start2 = MIN(old_editable->selection_start_pos, old_editable->selection_end_pos);
5329   end2 = MAX(old_editable->selection_start_pos, old_editable->selection_end_pos);
5330   
5331   if (start2 < start1)
5332     {
5333       guint tmp;
5334       
5335       tmp = start1; start1 = start2; start2 = tmp;
5336       tmp = end1;   end1   = end2;   end2   = tmp;
5337     }
5338   
5339   undraw_cursor (text, FALSE);
5340   old_editable->selection_start_pos = start;
5341   old_editable->selection_end_pos = end;
5342   draw_cursor (text, FALSE);
5343   
5344   /* Expose only what changed */
5345   
5346   if (start1 < start2)
5347     gtk_text_update_text (old_editable, start1, MIN(end1, start2));
5348   
5349   if (end2 > end1)
5350     gtk_text_update_text (old_editable, MAX(end1, start2), end2);
5351   else if (end2 < end1)
5352     gtk_text_update_text (old_editable, end2, end1);
5353 }
5354
5355
5356 /**********************************************************************/
5357 /*                              Debug                                 */
5358 /**********************************************************************/
5359
5360 #ifdef DEBUG_GTK_TEXT
5361 static void
5362 gtk_text_show_cache_line (GtkText *text, GList *cache,
5363                           const char* what, const char* func, gint line)
5364 {
5365   LineParams *lp = &CACHE_DATA(cache);
5366   gint i;
5367   
5368   if (cache == text->line_start_cache)
5369     g_message ("Line Start Cache: ");
5370   
5371   if (cache == text->current_line)
5372     g_message("Current Line: ");
5373   
5374   g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
5375              func,
5376              line,
5377              what,
5378              lp->start.index,
5379              lp->end.index,
5380              LINE_HEIGHT(*lp));
5381   
5382   for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
5383     g_message ("%c", GTK_TEXT_INDEX (text, i));
5384   
5385   g_message (")\n");
5386 }
5387
5388 static void
5389 gtk_text_show_cache (GtkText *text, const char* func, gint line)
5390 {
5391   GList *l = text->line_start_cache;
5392   
5393   if (!l) {
5394     return;
5395   }
5396   
5397   /* back up to the absolute beginning of the line cache */
5398   while (l->prev)
5399     l = l->prev;
5400   
5401   g_message ("*** line cache ***\n");
5402   for (; l; l = l->next)
5403     gtk_text_show_cache_line (text, l, "all", func, line);
5404 }
5405
5406 static void
5407 gtk_text_assert_mark (GtkText         *text,
5408                       GtkPropertyMark *mark,
5409                       GtkPropertyMark *before,
5410                       GtkPropertyMark *after,
5411                       const gchar     *msg,
5412                       const gchar     *where,
5413                       gint             line)
5414 {
5415   GtkPropertyMark correct_mark = find_mark (text, mark->index);
5416   
5417   if (mark->offset != correct_mark.offset ||
5418       mark->property != correct_mark.property)
5419     g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
5420 }
5421
5422 static void
5423 gtk_text_assert (GtkText         *text,
5424                  const gchar     *msg,
5425                  gint             line)
5426 {
5427   GList* cache = text->line_start_cache;
5428   GtkPropertyMark* before_mark = NULL;
5429   GtkPropertyMark* after_mark = NULL;
5430   
5431   gtk_text_show_props (text, msg, line);
5432   
5433   for (; cache->prev; cache = cache->prev)
5434     /* nothing */;
5435   
5436   g_message ("*** line markers ***\n");
5437   
5438   for (; cache; cache = cache->next)
5439     {
5440       after_mark = &CACHE_DATA(cache).end;
5441       gtk_text_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
5442       before_mark = &CACHE_DATA(cache).start;
5443       
5444       if (cache->next)
5445         after_mark = &CACHE_DATA(cache->next).start;
5446       else
5447         after_mark = NULL;
5448       
5449       gtk_text_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
5450       before_mark = &CACHE_DATA(cache).end;
5451     }
5452 }
5453
5454 static void
5455 gtk_text_show_adj (GtkText *text,
5456                    GtkAdjustment *adj,
5457                    const char* what,
5458                    const char* func,
5459                    gint line)
5460 {
5461   g_message ("*** adjustment ***\n");
5462   
5463   g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
5464              func,
5465              line,
5466              what,
5467              adj->lower,
5468              adj->upper,
5469              adj->value,
5470              adj->step_increment,
5471              adj->page_increment,
5472              adj->page_size);
5473 }
5474
5475 static void
5476 gtk_text_show_props (GtkText *text,
5477                      const char* msg,
5478                      int line)
5479 {
5480   GList* props = text->text_properties;
5481   int proplen = 0;
5482   
5483   g_message ("%s:%d: ", msg, line);
5484   
5485   for (; props; props = props->next)
5486     {
5487       TextProperty *p = (TextProperty*)props->data;
5488       
5489       proplen += p->length;
5490
5491       g_message ("[%d,%p,", p->length, p);
5492       if (p->flags & PROPERTY_FONT)
5493         g_message ("%p,", p->font);
5494       else
5495         g_message ("-,");
5496       if (p->flags & PROPERTY_FOREGROUND)
5497         g_message ("%ld, ", p->fore_color.pixel);
5498       else
5499         g_message ("-,");
5500       if (p->flags & PROPERTY_BACKGROUND)
5501         g_message ("%ld] ", p->back_color.pixel);
5502       else
5503         g_message ("-] ");
5504     }
5505   
5506   g_message ("\n");
5507   
5508   if (proplen - 1 != TEXT_LENGTH(text))
5509     g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);
5510 }
5511 #endif
5512
5513 #define __GTK_TEXT_C__
5514 #include "gtkaliasdef.c"