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