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