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