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