1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
28 * Modified by the Sylpheed Team and others 2001-2002. Interesting
29 * parts are marked using comment block following this one.
30 * This modification is based on the GtkText of GTK 1.2.10
39 #include <gdk/gdkkeysyms.h>
40 #include <gdk/gdki18n.h>
41 #include <gtk/gtkmain.h>
42 #include <gtk/gtkselection.h>
43 #include <gtk/gtksignal.h>
50 #define line_arrow_width 6
51 #define line_arrow_height 9
52 static unsigned char line_arrow_bits[] = {
53 0x00, 0x00, 0x04, 0x0c, 0x18, 0x3f, 0x18, 0x0c, 0x04};
56 #define line_wrap_width 6
57 #define line_wrap_height 9
58 static unsigned char line_wrap_bits[] = {
59 0x1e, 0x3e, 0x30, 0x30, 0x39, 0x1f, 0x0f, 0x0f, 0x1f, };
62 * compile time settings
64 #define INITIAL_BUFFER_SIZE 1024
65 #define INITIAL_LINE_CACHE_SIZE 256
66 #define MIN_GAP_SIZE 256
69 * intending to introduce a paragraph delimiter '\r' because
70 * '\r' are being stripped by sylpheed
72 #define LINE_DELIM '\n'
73 #define MIN_TEXT_WIDTH_LINES 20
74 #define MIN_TEXT_HEIGHT_LINES 10
75 #define TEXT_BORDER_ROOM 1
76 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
77 #define DEFAULT_TAB_STOP_WIDTH 4
78 #define SCROLL_PIXELS 5
79 #define KEY_SCROLL_PIXELS 10
80 #define SCROLL_TIME 100
81 #define FREEZE_LENGTH 1024
82 /* Freeze text when inserting or deleting more than this many characters */
85 * could also mark paragraphs using marks, but don't know anything yet
86 * about consistency when characters are removed
88 #define SET_PROPERTY_MARK(m, p, o) do { \
89 (m)->property = (p); \
92 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
93 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
94 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
95 (mark)->property->prev->data \
97 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
98 #define MARK_LIST_PTR(mark) ((mark)->property)
99 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
100 #define MARK_OFFSET(mark) ((mark)->offset)
101 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
104 #define MARK_CURRENT_FONT(text, mark) \
105 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
106 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
107 GTK_WIDGET (text)->style->font)
108 #define MARK_CURRENT_FORE(text, mark) \
109 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
110 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
111 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
112 #define MARK_CURRENT_BACK(text, mark) \
113 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
114 &MARK_CURRENT_PROPERTY(mark)->back_color : \
115 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
116 #define MARK_CURRENT_TEXT_FONT(text, mark) \
117 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
118 MARK_CURRENT_PROPERTY(mark)->font : \
121 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
122 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
123 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
124 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
125 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
126 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
127 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
128 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
138 typedef struct _TextProperty TextProperty;
139 typedef struct _TabStopMark TabStopMark;
140 typedef struct _PrevTabCont PrevTabCont;
141 typedef struct _FetchLinesData FetchLinesData;
142 typedef struct _LineParams LineParams;
143 typedef struct _SetVerticalScrollData SetVerticalScrollData;
148 typedef gint (*LineIteratorFunction) (GtkSText* text, LineParams* lp, void* data);
156 struct _SetVerticalScrollData {
158 gint last_didnt_wrap;
159 gint last_line_start;
160 GtkSPropertyMark mark;
165 /* The actual font. */
169 gint16 char_widths[256];
173 PROPERTY_FONT = 1 << 0,
174 PROPERTY_FOREGROUND = 1 << 1,
175 PROPERTY_BACKGROUND = 1 << 2
183 /* Background Color. */
186 /* Foreground Color. */
189 /* Show which properties are set */
190 TextPropertyFlags flags;
192 /* Length of this property. */
198 GList* tab_stops; /* Index into list containing the next tab position. If
199 * NULL, using default widths. */
206 TabStopMark tab_start;
209 struct _FetchLinesData
222 guint displayable_chars;
225 PrevTabCont tab_cont;
226 PrevTabCont tab_cont_next;
228 GtkSPropertyMark start;
229 GtkSPropertyMark end;
233 static void gtk_stext_class_init (GtkSTextClass *klass);
234 static void gtk_stext_set_arg (GtkObject *object,
237 static void gtk_stext_get_arg (GtkObject *object,
240 static void gtk_stext_init (GtkSText *text);
241 static void gtk_stext_destroy (GtkObject *object);
242 static void gtk_stext_finalize (GtkObject *object);
243 static void gtk_stext_realize (GtkWidget *widget);
244 static void gtk_stext_unrealize (GtkWidget *widget);
245 static void gtk_stext_style_set (GtkWidget *widget,
246 GtkStyle *previous_style);
247 static void gtk_stext_state_changed (GtkWidget *widget,
248 GtkStateType previous_state);
249 static void gtk_stext_draw_focus (GtkWidget *widget);
250 static void gtk_stext_size_request (GtkWidget *widget,
251 GtkRequisition *requisition);
252 static void gtk_stext_size_allocate (GtkWidget *widget,
253 GtkAllocation *allocation);
254 static void gtk_stext_adjustment (GtkAdjustment *adjustment,
256 static void gtk_stext_disconnect (GtkAdjustment *adjustment,
259 static void gtk_stext_insert_text (GtkEditable *editable,
260 const gchar *new_text,
261 gint new_text_length,
263 static void gtk_stext_delete_text (GtkEditable *editable,
266 static void gtk_stext_update_text (GtkEditable *editable,
269 static gchar *gtk_stext_get_chars (GtkEditable *editable,
272 static void gtk_stext_set_selection (GtkEditable *editable,
275 static void gtk_stext_real_set_editable(GtkEditable *editable,
276 gboolean is_editable);
279 static void gtk_stext_draw (GtkWidget *widget,
281 static gint gtk_stext_expose (GtkWidget *widget,
282 GdkEventExpose *event);
283 static gint gtk_stext_button_press (GtkWidget *widget,
284 GdkEventButton *event);
285 static gint gtk_stext_button_release (GtkWidget *widget,
286 GdkEventButton *event);
287 static gint gtk_stext_motion_notify (GtkWidget *widget,
288 GdkEventMotion *event);
289 static gint gtk_stext_key_press (GtkWidget *widget,
291 static gint gtk_stext_focus_in (GtkWidget *widget,
292 GdkEventFocus *event);
293 static gint gtk_stext_focus_out (GtkWidget *widget,
294 GdkEventFocus *event);
296 static void move_gap (GtkSText* text, guint index);
297 static void make_forward_space (GtkSText* text, guint len);
299 /* Property management */
300 static GtkSTextFont* get_text_font (GdkFont* gfont);
301 static void text_font_unref (GtkSTextFont *text_font);
303 static void insert_text_property (GtkSText* text, GdkFont* font,
304 GdkColor *fore, GdkColor* back, guint len);
305 static TextProperty* new_text_property (GtkSText *text, GdkFont* font,
306 GdkColor* fore, GdkColor* back, guint length);
307 static void destroy_text_property (TextProperty *prop);
308 static void init_properties (GtkSText *text);
309 static void realize_property (GtkSText *text, TextProperty *prop);
310 static void realize_properties (GtkSText *text);
311 static void unrealize_property (GtkSText *text, TextProperty *prop);
312 static void unrealize_properties (GtkSText *text);
314 static void delete_text_property (GtkSText* text, guint len);
316 static guint pixel_height_of (GtkSText* text, GList* cache_line);
318 /* Property Movement and Size Computations */
319 static void advance_mark (GtkSPropertyMark* mark);
320 static void decrement_mark (GtkSPropertyMark* mark);
321 static void advance_mark_n (GtkSPropertyMark* mark, gint n);
322 static void decrement_mark_n (GtkSPropertyMark* mark, gint n);
323 static void move_mark_n (GtkSPropertyMark* mark, gint n);
324 static GtkSPropertyMark find_mark (GtkSText* text, guint mark_position);
325 static GtkSPropertyMark find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near);
326 static void find_line_containing_point (GtkSText* text, guint point,
330 static void compute_lines_pixels (GtkSText* text, guint char_count,
331 guint *lines, guint *pixels);
333 static gint total_line_height (GtkSText* text,
336 static LineParams find_line_params (GtkSText* text,
337 const GtkSPropertyMark *mark,
338 const PrevTabCont *tab_cont,
339 PrevTabCont *next_cont);
340 static void recompute_geometry (GtkSText* text);
341 static void insert_expose (GtkSText* text, guint old_pixels, gint nchars, guint new_line_count);
342 static void delete_expose (GtkSText* text,
346 static GdkGC *create_bg_gc (GtkSText *text);
347 static void clear_area (GtkSText *text, GdkRectangle *area);
348 static void draw_line (GtkSText* text,
351 static void draw_line_wrap (GtkSText* text,
353 static void draw_cursor (GtkSText* text, gint absolute);
354 static void undraw_cursor (GtkSText* text, gint absolute);
355 static gint drawn_cursor_min (GtkSText* text);
356 static gint drawn_cursor_max (GtkSText* text);
357 static void expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor);
359 /* Search and Placement. */
360 static void find_cursor (GtkSText* text,
362 static void find_cursor_at_line (GtkSText* text,
363 const LineParams* start_line,
365 static void find_mouse_cursor (GtkSText* text, gint x, gint y);
368 static void adjust_adj (GtkSText* text, GtkAdjustment* adj);
369 static void scroll_up (GtkSText* text, gint diff);
370 static void scroll_down (GtkSText* text, gint diff);
371 static void scroll_int (GtkSText* text, gint diff);
373 static void process_exposes (GtkSText *text);
375 /* Cache Management. */
376 static void free_cache (GtkSText* text);
377 static GList* remove_cache_line (GtkSText* text, GList* list);
380 static void move_cursor_buffer_ver (GtkSText *text, int dir);
381 static void move_cursor_page_ver (GtkSText *text, int dir);
382 static void move_cursor_ver (GtkSText *text, int count);
383 static void move_cursor_hor (GtkSText *text, int count);
387 static void move_cursor_to_display_row_end (GtkSText *text);
388 static void move_cursor_to_display_row_start (GtkSText *text);
389 static void move_cursor_to_display_row_up (GtkSText *text);
390 static void move_cursor_to_display_row_down (GtkSText *text);
391 static void reset_persist_col_pos (GtkSText *text);
393 /* Binding actions */
394 static void gtk_stext_move_cursor (GtkEditable *editable,
397 static void gtk_stext_move_word (GtkEditable *editable,
399 static void gtk_stext_move_page (GtkEditable *editable,
402 static void gtk_stext_move_to_row (GtkEditable *editable,
404 static void gtk_stext_move_to_column (GtkEditable *editable,
406 static void gtk_stext_kill_char (GtkEditable *editable,
408 static void gtk_stext_kill_word (GtkEditable *editable,
410 static void gtk_stext_kill_line (GtkEditable *editable,
415 static void gtk_stext_move_forward_character (GtkSText *text);
416 static void gtk_stext_move_backward_character (GtkSText *text);
417 static void gtk_stext_move_forward_word (GtkSText *text);
418 static void gtk_stext_move_backward_word (GtkSText *text);
419 static void gtk_stext_move_beginning_of_line (GtkSText *text);
420 static void gtk_stext_move_end_of_line (GtkSText *text);
421 static void gtk_stext_move_next_line (GtkSText *text);
422 static void gtk_stext_move_previous_line (GtkSText *text);
424 static void gtk_stext_delete_forward_character (GtkSText *text);
425 static void gtk_stext_delete_backward_character (GtkSText *text);
426 static void gtk_stext_delete_forward_word (GtkSText *text);
427 static void gtk_stext_delete_backward_word (GtkSText *text);
428 static void gtk_stext_delete_line (GtkSText *text);
429 static void gtk_stext_delete_to_line_end (GtkSText *text);
431 static void gtk_stext_select_word (GtkSText *text,
433 static void gtk_stext_select_line (GtkSText *text,
436 static void gtk_stext_set_position (GtkEditable *editable,
442 static void gtk_stext_enable_blink (GtkSText *text);
443 static void gtk_stext_disable_blink (GtkSText *text);
445 /* #define DEBUG_GTK_STEXT */
447 #if defined(DEBUG_GTK_STEXT) && defined(__GNUC__)
448 /* Debugging utilities. */
449 static void gtk_stext_assert_mark (GtkSText *text,
450 GtkSPropertyMark *mark,
451 GtkSPropertyMark *before,
452 GtkSPropertyMark *after,
457 static void gtk_stext_assert (GtkSText *text,
460 static void gtk_stext_show_cache_line (GtkSText *text, GList *cache,
461 const char* what, const char* func, gint line);
462 static void gtk_stext_show_cache (GtkSText *text, const char* func, gint line);
463 static void gtk_stext_show_adj (GtkSText *text,
468 static void gtk_stext_show_props (GtkSText* test,
472 #define TDEBUG(args) g_message args
473 #define TEXT_ASSERT(text) gtk_stext_assert (text,__PRETTY_FUNCTION__,__LINE__)
474 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_stext_assert_mark (text,mark, \
475 __PRETTY_FUNCTION__,msg,__LINE__)
476 #define TEXT_SHOW(text) gtk_stext_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
477 #define TEXT_SHOW_LINE(text,line,msg) gtk_stext_show_cache_line (text,line,msg,\
478 __PRETTY_FUNCTION__,__LINE__)
479 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_stext_show_adj (text,adj,msg, \
480 __PRETTY_FUNCTION__,__LINE__)
483 #define TEXT_ASSERT(text)
484 #define TEXT_ASSERT_MARK(text,mark,msg)
485 #define TEXT_SHOW(text)
486 #define TEXT_SHOW_LINE(text,line,msg)
487 #define TEXT_SHOW_ADJ(text,adj,msg)
491 #if defined(AHX_DEBUG)
492 # define XDEBUG(args) g_message args
494 # define XDEBUG(args)
495 #endif /* AHX_DEBUG */
497 /* Memory Management. */
498 static GMemChunk *params_mem_chunk = NULL;
499 static GMemChunk *text_property_chunk = NULL;
501 static GtkWidgetClass *parent_class = NULL;
505 static const GtkTextFunction control_keys[26] =
507 (GtkTextFunction)gtk_stext_move_beginning_of_line, /* a */
508 (GtkTextFunction)gtk_stext_move_backward_character, /* b */
509 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
510 (GtkTextFunction)gtk_stext_delete_forward_character, /* d */
511 (GtkTextFunction)gtk_stext_move_end_of_line, /* e */
512 (GtkTextFunction)gtk_stext_move_forward_character, /* f */
514 (GtkTextFunction)gtk_stext_delete_backward_character,/* h */
517 (GtkTextFunction)gtk_stext_delete_to_line_end, /* k */
520 (GtkTextFunction)gtk_stext_move_next_line, /* n */
522 (GtkTextFunction)gtk_stext_move_previous_line, /* p */
527 (GtkTextFunction)gtk_stext_delete_line, /* u */
528 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
529 (GtkTextFunction)gtk_stext_delete_backward_word, /* w */
530 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
535 static const GtkTextFunction alt_keys[26] =
538 (GtkTextFunction)gtk_stext_move_backward_word, /* b */
540 (GtkTextFunction)gtk_stext_delete_forward_word, /* d */
542 (GtkTextFunction)gtk_stext_move_forward_word, /* f */
567 /**********************************************************************/
569 /**********************************************************************/
572 gtk_stext_get_type (void)
574 static GtkType text_type = 0;
578 static const GtkTypeInfo text_info =
582 sizeof (GtkSTextClass),
583 (GtkClassInitFunc) gtk_stext_class_init,
584 (GtkObjectInitFunc) gtk_stext_init,
585 /* reserved_1 */ NULL,
586 /* reserved_2 */ NULL,
587 (GtkClassInitFunc) NULL,
590 text_type = gtk_type_unique (GTK_TYPE_EDITABLE, &text_info);
597 gtk_stext_class_init (GtkSTextClass *class)
599 GtkObjectClass *object_class;
600 GtkWidgetClass *widget_class;
601 GtkEditableClass *editable_class;
603 object_class = (GtkObjectClass*) class;
604 widget_class = (GtkWidgetClass*) class;
605 editable_class = (GtkEditableClass*) class;
606 parent_class = gtk_type_class (GTK_TYPE_EDITABLE);
608 gtk_object_add_arg_type ("GtkSText::hadjustment",
610 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
612 gtk_object_add_arg_type ("GtkSText::vadjustment",
614 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT,
616 gtk_object_add_arg_type ("GtkSText::line_wrap",
620 gtk_object_add_arg_type ("GtkSText::word_wrap",
625 object_class->set_arg = gtk_stext_set_arg;
626 object_class->get_arg = gtk_stext_get_arg;
627 object_class->destroy = gtk_stext_destroy;
628 object_class->finalize = gtk_stext_finalize;
630 widget_class->realize = gtk_stext_realize;
631 widget_class->unrealize = gtk_stext_unrealize;
632 widget_class->style_set = gtk_stext_style_set;
633 widget_class->state_changed = gtk_stext_state_changed;
634 widget_class->draw_focus = gtk_stext_draw_focus;
635 widget_class->size_request = gtk_stext_size_request;
636 widget_class->size_allocate = gtk_stext_size_allocate;
637 widget_class->draw = gtk_stext_draw;
638 widget_class->expose_event = gtk_stext_expose;
639 widget_class->button_press_event = gtk_stext_button_press;
640 widget_class->button_release_event = gtk_stext_button_release;
641 widget_class->motion_notify_event = gtk_stext_motion_notify;
642 widget_class->key_press_event = gtk_stext_key_press;
643 widget_class->focus_in_event = gtk_stext_focus_in;
644 widget_class->focus_out_event = gtk_stext_focus_out;
646 widget_class->set_scroll_adjustments_signal =
647 gtk_signal_new ("set_scroll_adjustments",
650 GTK_SIGNAL_OFFSET (GtkSTextClass, set_scroll_adjustments),
651 gtk_marshal_NONE__POINTER_POINTER,
652 GTK_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
654 editable_class->set_editable = gtk_stext_real_set_editable;
655 editable_class->insert_text = gtk_stext_insert_text;
656 editable_class->delete_text = gtk_stext_delete_text;
658 editable_class->move_cursor = gtk_stext_move_cursor;
659 editable_class->move_word = gtk_stext_move_word;
660 editable_class->move_page = gtk_stext_move_page;
661 editable_class->move_to_row = gtk_stext_move_to_row;
662 editable_class->move_to_column = gtk_stext_move_to_column;
664 editable_class->kill_char = gtk_stext_kill_char;
665 editable_class->kill_word = gtk_stext_kill_word;
666 editable_class->kill_line = gtk_stext_kill_line;
668 editable_class->update_text = gtk_stext_update_text;
669 editable_class->get_chars = gtk_stext_get_chars;
670 editable_class->set_selection = gtk_stext_set_selection;
671 editable_class->set_position = gtk_stext_set_position;
673 class->set_scroll_adjustments = gtk_stext_set_adjustments;
677 gtk_stext_set_arg (GtkObject *object,
683 text = GTK_STEXT (object);
687 case ARG_HADJUSTMENT:
688 gtk_stext_set_adjustments (text,
689 GTK_VALUE_POINTER (*arg),
692 case ARG_VADJUSTMENT:
693 gtk_stext_set_adjustments (text,
695 GTK_VALUE_POINTER (*arg));
698 gtk_stext_set_line_wrap (text, GTK_VALUE_BOOL (*arg));
701 gtk_stext_set_word_wrap (text, GTK_VALUE_BOOL (*arg));
709 gtk_stext_get_arg (GtkObject *object,
715 text = GTK_STEXT (object);
719 case ARG_HADJUSTMENT:
720 GTK_VALUE_POINTER (*arg) = text->hadj;
722 case ARG_VADJUSTMENT:
723 GTK_VALUE_POINTER (*arg) = text->vadj;
726 GTK_VALUE_BOOL (*arg) = text->line_wrap;
729 GTK_VALUE_BOOL (*arg) = text->word_wrap;
732 arg->type = GTK_TYPE_INVALID;
738 gtk_stext_init (GtkSText *text)
740 GTK_WIDGET_SET_FLAGS (text, GTK_CAN_FOCUS);
742 text->text_area = NULL;
747 text->line_wrap_bitmap = NULL;
748 text->line_arrow_bitmap = NULL;
750 text->use_wchar = FALSE;
751 text->text.ch = g_new (guchar, INITIAL_BUFFER_SIZE);
752 text->text_len = INITIAL_BUFFER_SIZE;
754 text->scratch_buffer.ch = NULL;
755 text->scratch_buffer_len = 0;
757 text->freeze_count = 0;
759 if (!params_mem_chunk)
760 params_mem_chunk = g_mem_chunk_new ("LineParams",
762 256 * sizeof (LineParams),
765 text->default_tab_width = 4;
766 text->tab_stops = NULL;
768 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
769 text->tab_stops = g_list_prepend (text->tab_stops, (void*)8);
771 text->line_start_cache = NULL;
772 text->first_cut_pixels = 0;
774 text->line_wrap = TRUE;
775 text->word_wrap = FALSE;
780 text->current_font = NULL;
783 * timer for blinking cursor
785 text->cursor_visible = FALSE; /* don't know whether gtktext stores this somewhere */
786 text->cursor_timer_on = TRUE;
787 text->cursor_off_ms = 500;
788 text->cursor_on_ms = 500;
789 text->cursor_timer_id = 0;
790 text->cursor_idle_time_timer_id = 0;
791 text->wrap_rmargin = 0;
792 text->cursor_type = GTK_STEXT_CURSOR_LINE;
793 text->persist_column = 0;
795 init_properties (text);
797 GTK_EDITABLE (text)->editable = FALSE;
799 gtk_editable_set_position (GTK_EDITABLE (text), 0);
803 gtk_stext_new (GtkAdjustment *hadj,
809 g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadj), NULL);
811 g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadj), NULL);
813 text = gtk_widget_new (GTK_TYPE_STEXT,
819 * force widget name to be GtkText so it silently adapts
820 * the GtkText widget's style...
822 gtk_widget_set_name (text, "GtkText");
823 gtk_widget_ensure_style (text);
829 gtk_stext_set_word_wrap (GtkSText *text,
832 g_return_if_fail (text != NULL);
833 g_return_if_fail (GTK_IS_STEXT (text));
835 text->word_wrap = (word_wrap != FALSE);
837 if (GTK_WIDGET_REALIZED (text))
839 recompute_geometry (text);
840 gtk_widget_queue_draw (GTK_WIDGET (text));
845 gtk_stext_set_line_wrap (GtkSText *text,
848 g_return_if_fail (text != NULL);
849 g_return_if_fail (GTK_IS_STEXT (text));
851 text->line_wrap = (line_wrap != FALSE);
853 if (GTK_WIDGET_REALIZED (text))
855 recompute_geometry (text);
856 gtk_widget_queue_draw (GTK_WIDGET (text));
861 gtk_stext_set_editable (GtkSText *text,
862 gboolean is_editable)
864 g_return_if_fail (text != NULL);
865 g_return_if_fail (GTK_IS_STEXT (text));
867 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
871 gtk_stext_real_set_editable (GtkEditable *editable,
872 gboolean is_editable)
876 g_return_if_fail (editable != NULL);
877 g_return_if_fail (GTK_IS_STEXT (editable));
879 text = GTK_STEXT (editable);
881 editable->editable = (is_editable != FALSE);
883 if (GTK_WIDGET_REALIZED (text))
885 recompute_geometry (text);
886 gtk_widget_queue_draw (GTK_WIDGET (text));
891 gtk_stext_set_adjustments (GtkSText *text,
895 g_return_if_fail (text != NULL);
896 g_return_if_fail (GTK_IS_STEXT (text));
898 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
900 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
902 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
904 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
906 if (text->hadj && (text->hadj != hadj))
908 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
909 gtk_object_unref (GTK_OBJECT (text->hadj));
912 if (text->vadj && (text->vadj != vadj))
914 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
915 gtk_object_unref (GTK_OBJECT (text->vadj));
918 if (text->hadj != hadj)
921 gtk_object_ref (GTK_OBJECT (text->hadj));
922 gtk_object_sink (GTK_OBJECT (text->hadj));
924 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
925 (GtkSignalFunc) gtk_stext_adjustment,
927 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
928 (GtkSignalFunc) gtk_stext_adjustment,
930 gtk_signal_connect (GTK_OBJECT (text->hadj), "disconnect",
931 (GtkSignalFunc) gtk_stext_disconnect,
933 gtk_stext_adjustment (hadj, text);
936 if (text->vadj != vadj)
939 gtk_object_ref (GTK_OBJECT (text->vadj));
940 gtk_object_sink (GTK_OBJECT (text->vadj));
942 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
943 (GtkSignalFunc) gtk_stext_adjustment,
945 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
946 (GtkSignalFunc) gtk_stext_adjustment,
948 gtk_signal_connect (GTK_OBJECT (text->vadj), "disconnect",
949 (GtkSignalFunc) gtk_stext_disconnect,
951 gtk_stext_adjustment (vadj, text);
956 gtk_stext_set_point (GtkSText *text,
959 g_return_if_fail (text != NULL);
960 g_return_if_fail (GTK_IS_STEXT (text));
961 g_return_if_fail (index <= TEXT_LENGTH (text));
963 text->point = find_mark (text, index);
967 gtk_stext_get_point (GtkSText *text)
969 g_return_val_if_fail (text != NULL, 0);
970 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
972 return text->point.index;
976 gtk_stext_get_length (GtkSText *text)
978 g_return_val_if_fail (text != NULL, 0);
979 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
981 return TEXT_LENGTH (text);
985 gtk_stext_freeze (GtkSText *text)
987 g_return_if_fail (text != NULL);
988 g_return_if_fail (GTK_IS_STEXT (text));
990 text->freeze_count++;
991 undraw_cursor (text, FALSE);
995 gtk_stext_thaw (GtkSText *text)
997 g_return_if_fail (text != NULL);
998 g_return_if_fail (GTK_IS_STEXT (text));
1000 if (text->freeze_count)
1001 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
1003 recompute_geometry (text);
1004 gtk_widget_queue_draw (GTK_WIDGET (text));
1006 draw_cursor (text, FALSE);
1011 gtk_stext_compact_buffer (GtkSText *text)
1013 g_return_if_fail (text != NULL);
1014 g_return_if_fail (GTK_IS_STEXT (text));
1015 move_gap (text, gtk_stext_get_length(text));
1019 gtk_stext_insert (GtkSText *text,
1026 GtkEditable *editable = GTK_EDITABLE (text);
1027 gboolean frozen = FALSE;
1029 gint new_line_count = 1;
1030 guint old_height = 0;
1035 g_return_if_fail (text != NULL);
1036 g_return_if_fail (GTK_IS_STEXT (text));
1038 g_return_if_fail (chars != NULL);
1041 if (!nchars || !chars)
1043 nchars = strlen (chars);
1047 if (!text->freeze_count && (length > FREEZE_LENGTH))
1049 gtk_stext_freeze (text);
1053 if (!text->freeze_count && (text->line_start_cache != NULL))
1055 find_line_containing_point (text, text->point.index, TRUE);
1056 old_height = total_line_height (text, text->current_line, 1);
1059 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1062 widget = GTK_WIDGET (text);
1063 gtk_widget_ensure_style (widget);
1064 if ((widget->style) && (widget->style->font->type == GDK_FONT_FONTSET))
1066 text->use_wchar = TRUE;
1067 g_free (text->text.ch);
1068 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1069 text->text_len = INITIAL_BUFFER_SIZE;
1070 if (text->scratch_buffer.ch)
1071 g_free (text->scratch_buffer.ch);
1072 text->scratch_buffer.wc = NULL;
1073 text->scratch_buffer_len = 0;
1077 move_gap (text, text->point.index);
1078 make_forward_space (text, length);
1080 if (text->use_wchar)
1082 char *chars_nt = (char *)chars;
1085 chars_nt = g_new (char, length+1);
1086 memcpy (chars_nt, chars, length);
1087 chars_nt[length] = 0;
1089 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1091 if (chars_nt != chars)
1099 memcpy(text->text.ch + text->gap_position, chars, length);
1102 if (!text->freeze_count && (text->line_start_cache != NULL))
1104 if (text->use_wchar)
1106 for (i=0; i<numwcs; i++)
1107 if (text->text.wc[text->gap_position + i] == '\n')
1112 for (i=0; i<numwcs; i++)
1113 if (text->text.ch[text->gap_position + i] == '\n')
1120 insert_text_property (text, font, fore, back, numwcs);
1122 text->gap_size -= numwcs;
1123 text->gap_position += numwcs;
1125 if (text->point.index < text->first_line_start_index)
1126 text->first_line_start_index += numwcs;
1127 if (text->point.index < editable->selection_start_pos)
1128 editable->selection_start_pos += numwcs;
1129 if (text->point.index < editable->selection_end_pos)
1130 editable->selection_end_pos += numwcs;
1131 /* We'll reset the cursor later anyways if we aren't frozen */
1132 if (text->point.index < text->cursor_mark.index)
1133 text->cursor_mark.index += numwcs;
1135 advance_mark_n (&text->point, numwcs);
1137 if (!text->freeze_count && (text->line_start_cache != NULL))
1138 insert_expose (text, old_height, numwcs, new_line_count);
1142 gtk_stext_thaw (text);
1146 gtk_stext_backward_delete (GtkSText *text,
1149 g_return_val_if_fail (text != NULL, 0);
1150 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1152 if (nchars > text->point.index || nchars <= 0)
1155 gtk_stext_set_point (text, text->point.index - nchars);
1157 return gtk_stext_forward_delete (text, nchars);
1161 gtk_stext_forward_delete (GtkSText *text,
1164 guint old_lines, old_height;
1165 GtkEditable *editable = GTK_EDITABLE (text);
1166 gboolean frozen = FALSE;
1168 g_return_val_if_fail (text != NULL, 0);
1169 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1171 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1174 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1176 gtk_stext_freeze (text);
1180 if (!text->freeze_count && text->line_start_cache != NULL)
1182 /* We need to undraw the cursor here, since we may later
1183 * delete the cursor's property
1185 undraw_cursor (text, FALSE);
1186 find_line_containing_point (text, text->point.index, TRUE);
1187 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1190 /* FIXME, or resizing after deleting will be odd */
1191 if (text->point.index < text->first_line_start_index)
1193 if (text->point.index + nchars >= text->first_line_start_index)
1195 text->first_line_start_index = text->point.index;
1196 while ((text->first_line_start_index > 0) &&
1197 (GTK_STEXT_INDEX (text, text->first_line_start_index - 1)
1199 text->first_line_start_index -= 1;
1203 text->first_line_start_index -= nchars;
1206 if (text->point.index < editable->selection_start_pos)
1207 editable->selection_start_pos -=
1208 MIN(nchars, editable->selection_start_pos - text->point.index);
1209 if (text->point.index < editable->selection_end_pos)
1210 editable->selection_end_pos -=
1211 MIN(nchars, editable->selection_end_pos - text->point.index);
1212 /* We'll reset the cursor later anyways if we aren't frozen */
1213 if (text->point.index < text->cursor_mark.index)
1214 move_mark_n (&text->cursor_mark,
1215 -MIN(nchars, text->cursor_mark.index - text->point.index));
1217 move_gap (text, text->point.index);
1219 text->gap_size += nchars;
1221 delete_text_property (text, nchars);
1223 if (!text->freeze_count && (text->line_start_cache != NULL))
1225 delete_expose (text, nchars, old_lines, old_height);
1226 draw_cursor (text, FALSE);
1230 gtk_stext_thaw (text);
1236 gtk_stext_set_position (GtkEditable *editable,
1239 GtkSText *text = (GtkSText *) editable;
1241 undraw_cursor (text, FALSE);
1242 text->cursor_mark = find_mark (text, position);
1243 find_cursor (text, TRUE);
1244 draw_cursor (text, FALSE);
1245 gtk_editable_select_region (editable, 0, 0);
1248 /* SYLPHEED - set_position used. Need to find out whether there's
1249 * a better way to handle this */
1250 static void gtk_stext_set_position_X (GtkEditable *editable,
1253 GtkSText *text = (GtkSText *) editable;
1255 undraw_cursor (text, FALSE);
1256 text->cursor_mark = find_mark (text, position);
1257 find_cursor (text, TRUE);
1258 draw_cursor (text, FALSE);
1262 gtk_stext_get_chars (GtkEditable *editable,
1270 g_return_val_if_fail (editable != NULL, NULL);
1271 g_return_val_if_fail (GTK_IS_STEXT (editable), NULL);
1272 text = GTK_STEXT (editable);
1275 end_pos = TEXT_LENGTH (text);
1277 if ((start_pos < 0) ||
1278 (end_pos > TEXT_LENGTH (text)) ||
1279 (end_pos < start_pos))
1282 move_gap (text, TEXT_LENGTH (text));
1283 make_forward_space (text, 1);
1285 if (text->use_wchar)
1288 ch = text->text.wc[end_pos];
1289 text->text.wc[end_pos] = 0;
1290 retval = gdk_wcstombs (text->text.wc + start_pos);
1291 text->text.wc[end_pos] = ch;
1296 ch = text->text.ch[end_pos];
1297 text->text.ch[end_pos] = 0;
1298 retval = g_strdup (text->text.ch + start_pos);
1299 text->text.ch[end_pos] = ch;
1307 gtk_stext_destroy (GtkObject *object)
1311 g_return_if_fail (object != NULL);
1312 g_return_if_fail (GTK_IS_STEXT (object));
1314 text = (GtkSText*) object;
1316 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1317 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1321 gtk_timeout_remove (text->timer);
1328 gtk_stext_disable_blink (text);
1330 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1334 gtk_stext_finalize (GtkObject *object)
1339 g_return_if_fail (object != NULL);
1340 g_return_if_fail (GTK_IS_STEXT (object));
1342 text = (GtkSText *)object;
1344 gtk_object_unref (GTK_OBJECT (text->hadj));
1345 gtk_object_unref (GTK_OBJECT (text->vadj));
1347 /* Clean up the internal structures */
1348 if (text->use_wchar)
1349 g_free (text->text.wc);
1351 g_free (text->text.ch);
1353 tmp_list = text->text_properties;
1356 destroy_text_property (tmp_list->data);
1357 tmp_list = tmp_list->next;
1360 if (text->current_font)
1361 text_font_unref (text->current_font);
1363 g_list_free (text->text_properties);
1365 if (text->use_wchar)
1367 if (text->scratch_buffer.wc)
1368 g_free (text->scratch_buffer.wc);
1372 if (text->scratch_buffer.ch)
1373 g_free (text->scratch_buffer.ch);
1376 g_list_free (text->tab_stops);
1378 GTK_OBJECT_CLASS(parent_class)->finalize (object);
1382 gtk_stext_realize (GtkWidget *widget)
1385 GtkEditable *editable;
1386 GdkWindowAttr attributes;
1387 gint attributes_mask;
1389 g_return_if_fail (widget != NULL);
1390 g_return_if_fail (GTK_IS_STEXT (widget));
1392 text = GTK_STEXT (widget);
1393 editable = GTK_EDITABLE (widget);
1394 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1396 attributes.window_type = GDK_WINDOW_CHILD;
1397 attributes.x = widget->allocation.x;
1398 attributes.y = widget->allocation.y;
1399 attributes.width = widget->allocation.width;
1400 attributes.height = widget->allocation.height;
1401 attributes.wclass = GDK_INPUT_OUTPUT;
1402 attributes.visual = gtk_widget_get_visual (widget);
1403 attributes.colormap = gtk_widget_get_colormap (widget);
1404 attributes.event_mask = gtk_widget_get_events (widget);
1405 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1406 GDK_BUTTON_PRESS_MASK |
1407 GDK_BUTTON_RELEASE_MASK |
1408 GDK_BUTTON_MOTION_MASK |
1409 GDK_ENTER_NOTIFY_MASK |
1410 GDK_LEAVE_NOTIFY_MASK |
1411 GDK_KEY_PRESS_MASK);
1412 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1414 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1415 gdk_window_set_user_data (widget->window, text);
1417 attributes.x = (widget->style->klass->xthickness + TEXT_BORDER_ROOM);
1418 attributes.y = (widget->style->klass->ythickness + TEXT_BORDER_ROOM);
1419 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1420 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1422 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1423 attributes_mask |= GDK_WA_CURSOR;
1425 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1426 gdk_window_set_user_data (text->text_area, text);
1428 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1430 widget->style = gtk_style_attach (widget->style, widget->window);
1432 /* Can't call gtk_style_set_background here because it's handled specially */
1433 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1434 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1436 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1437 text->bg_gc = create_bg_gc (text);
1439 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1440 (gchar*) line_wrap_bits,
1444 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1445 (gchar*) line_arrow_bits,
1449 text->gc = gdk_gc_new (text->text_area);
1450 gdk_gc_set_exposures (text->gc, TRUE);
1451 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1453 #ifdef USE_GTKGDK_XIM
1454 if (gdk_im_ready () && (editable->ic_attr = gdk_ic_attr_new ()) != NULL)
1457 GdkColormap *colormap;
1459 GdkICAttr *attr = editable->ic_attr;
1460 GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
1462 GdkIMStyle supported_style = GDK_IM_PREEDIT_NONE |
1463 GDK_IM_PREEDIT_NOTHING |
1464 GDK_IM_PREEDIT_POSITION |
1465 GDK_IM_STATUS_NONE |
1466 GDK_IM_STATUS_NOTHING;
1468 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1469 supported_style &= ~GDK_IM_PREEDIT_POSITION;
1471 attr->style = style = gdk_im_decide_style (supported_style);
1472 attr->client_window = text->text_area;
1474 if ((colormap = gtk_widget_get_colormap (widget)) !=
1475 gtk_widget_get_default_colormap ())
1477 attrmask |= GDK_IC_PREEDIT_COLORMAP;
1478 attr->preedit_colormap = colormap;
1481 switch (style & GDK_IM_PREEDIT_MASK)
1483 case GDK_IM_PREEDIT_POSITION:
1484 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1486 g_warning ("over-the-spot style requires fontset");
1490 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
1491 gdk_window_get_size (text->text_area, &width, &height);
1492 attr->spot_location.x = 0;
1493 attr->spot_location.y = height;
1494 attr->preedit_area.x = 0;
1495 attr->preedit_area.y = 0;
1496 attr->preedit_area.width = width;
1497 attr->preedit_area.height = height;
1498 attr->preedit_fontset = widget->style->font;
1502 editable->ic = gdk_ic_new (attr, attrmask);
1504 if (editable->ic == NULL)
1505 g_warning ("Can't create input context.");
1508 mask = gdk_window_get_events (text->text_area);
1509 mask |= gdk_ic_get_events (editable->ic);
1510 gdk_window_set_events (text->text_area, mask);
1512 if (GTK_WIDGET_HAS_FOCUS (widget))
1513 gdk_im_begin (editable->ic, text->text_area);
1518 realize_properties (text);
1519 gdk_window_show (text->text_area);
1520 init_properties (text);
1522 if (editable->selection_start_pos != editable->selection_end_pos)
1523 gtk_editable_claim_selection (editable, TRUE, GDK_CURRENT_TIME);
1525 recompute_geometry (text);
1529 gtk_stext_style_set (GtkWidget *widget,
1530 GtkStyle *previous_style)
1532 GtkSText *text = GTK_STEXT (widget);
1534 if (GTK_WIDGET_REALIZED (widget))
1536 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1537 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1541 gdk_gc_destroy (text->bg_gc);
1545 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1546 text->bg_gc = create_bg_gc (text);
1548 recompute_geometry (text);
1551 if (text->current_font)
1552 text_font_unref (text->current_font);
1553 text->current_font = get_text_font (widget->style->font);
1557 gtk_stext_state_changed (GtkWidget *widget,
1558 GtkStateType previous_state)
1560 GtkSText *text = GTK_STEXT (widget);
1562 if (GTK_WIDGET_REALIZED (widget))
1564 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1565 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1570 gtk_stext_unrealize (GtkWidget *widget)
1574 g_return_if_fail (widget != NULL);
1575 g_return_if_fail (GTK_IS_STEXT (widget));
1577 text = GTK_STEXT (widget);
1579 #ifdef USE_GTKGDK_XIM
1580 if (GTK_EDITABLE (widget)->ic)
1582 gdk_ic_destroy (GTK_EDITABLE (widget)->ic);
1583 GTK_EDITABLE (widget)->ic = NULL;
1585 if (GTK_EDITABLE (widget)->ic_attr)
1587 gdk_ic_attr_destroy (GTK_EDITABLE (widget)->ic_attr);
1588 GTK_EDITABLE (widget)->ic_attr = NULL;
1592 gdk_window_set_user_data (text->text_area, NULL);
1593 gdk_window_destroy (text->text_area);
1594 text->text_area = NULL;
1596 gdk_gc_destroy (text->gc);
1601 gdk_gc_destroy (text->bg_gc);
1605 gdk_pixmap_unref (text->line_wrap_bitmap);
1606 gdk_pixmap_unref (text->line_arrow_bitmap);
1608 unrealize_properties (text);
1612 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1613 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1617 clear_focus_area (GtkSText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1619 GtkWidget *widget = GTK_WIDGET (text);
1622 gint ythick = TEXT_BORDER_ROOM + widget->style->klass->ythickness;
1623 gint xthick = TEXT_BORDER_ROOM + widget->style->klass->xthickness;
1627 if (area_width == 0 || area_height == 0)
1630 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1632 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1634 gdk_gc_set_ts_origin (text->bg_gc,
1635 (- (gint)text->first_onscreen_hor_pixel + xthick) % width,
1636 (- (gint)text->first_onscreen_ver_pixel + ythick) % height);
1641 gc = widget->style->bg_gc[widget->state];
1644 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1645 area_x, area_y, area_width, area_height);
1649 gtk_stext_draw_focus (GtkWidget *widget)
1655 g_return_if_fail (widget != NULL);
1656 g_return_if_fail (GTK_IS_STEXT (widget));
1658 text = GTK_STEXT (widget);
1660 if (GTK_WIDGET_DRAWABLE (widget))
1662 gint ythick = widget->style->klass->ythickness;
1663 gint xthick = widget->style->klass->xthickness;
1664 gint xextra = TEXT_BORDER_ROOM;
1665 gint yextra = TEXT_BORDER_ROOM;
1667 TDEBUG (("in gtk_stext_draw_focus\n"));
1671 width = widget->allocation.width;
1672 height = widget->allocation.height;
1674 if (GTK_WIDGET_HAS_FOCUS (widget))
1683 gtk_paint_focus (widget->style, widget->window,
1684 NULL, widget, "text",
1686 widget->allocation.width - 1,
1687 widget->allocation.height - 1);
1690 gtk_paint_shadow (widget->style, widget->window,
1691 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1692 NULL, widget, "text",
1693 x, y, width, height);
1697 width -= 2 * xthick;
1698 height -= 2 * ythick;
1701 clear_focus_area (text, x, y, width, yextra);
1703 clear_focus_area (text, x, y + yextra,
1704 xextra, y + height - 2 * yextra);
1706 clear_focus_area (text, x + width - xextra, y + yextra,
1707 xextra, height - 2 * ythick);
1709 clear_focus_area (text, x, x + height - yextra, width, yextra);
1713 TDEBUG (("in gtk_stext_draw_focus (undrawable !!!)\n"));
1718 gtk_stext_size_request (GtkWidget *widget,
1719 GtkRequisition *requisition)
1726 g_return_if_fail (widget != NULL);
1727 g_return_if_fail (GTK_IS_STEXT (widget));
1728 g_return_if_fail (requisition != NULL);
1730 xthickness = widget->style->klass->xthickness + TEXT_BORDER_ROOM;
1731 ythickness = widget->style->klass->ythickness + TEXT_BORDER_ROOM;
1733 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1734 widget->style->font->descent);
1736 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1737 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1741 requisition->width = char_width + xthickness * 2;
1742 requisition->height = char_height + ythickness * 2;
1746 gtk_stext_size_allocate (GtkWidget *widget,
1747 GtkAllocation *allocation)
1750 GtkEditable *editable;
1752 g_return_if_fail (widget != NULL);
1753 g_return_if_fail (GTK_IS_STEXT (widget));
1754 g_return_if_fail (allocation != NULL);
1756 text = GTK_STEXT (widget);
1757 editable = GTK_EDITABLE (widget);
1759 widget->allocation = *allocation;
1760 if (GTK_WIDGET_REALIZED (widget))
1762 gdk_window_move_resize (widget->window,
1763 allocation->x, allocation->y,
1764 allocation->width, allocation->height);
1766 gdk_window_move_resize (text->text_area,
1767 widget->style->klass->xthickness + TEXT_BORDER_ROOM,
1768 widget->style->klass->ythickness + TEXT_BORDER_ROOM,
1769 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->klass->xthickness +
1770 (gint)TEXT_BORDER_ROOM) * 2),
1771 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->klass->ythickness +
1772 (gint)TEXT_BORDER_ROOM) * 2));
1774 #ifdef USE_GTKGDK_XIM
1775 if (editable->ic && (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
1779 gdk_window_get_size (text->text_area, &width, &height);
1780 editable->ic_attr->preedit_area.width = width;
1781 editable->ic_attr->preedit_area.height = height;
1783 gdk_ic_set_attr (editable->ic,
1784 editable->ic_attr, GDK_IC_PREEDIT_AREA);
1788 recompute_geometry (text);
1793 gtk_stext_draw (GtkWidget *widget,
1796 g_return_if_fail (widget != NULL);
1797 g_return_if_fail (GTK_IS_STEXT (widget));
1798 g_return_if_fail (area != NULL);
1800 if (GTK_WIDGET_DRAWABLE (widget))
1802 expose_text (GTK_STEXT (widget), area, TRUE);
1803 gtk_widget_draw_focus (widget);
1808 gtk_stext_expose (GtkWidget *widget,
1809 GdkEventExpose *event)
1811 g_return_val_if_fail (widget != NULL, FALSE);
1812 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1813 g_return_val_if_fail (event != NULL, FALSE);
1815 if (event->window == GTK_STEXT (widget)->text_area)
1817 TDEBUG (("in gtk_stext_expose (expose)\n"));
1818 expose_text (GTK_STEXT (widget), &event->area, TRUE);
1820 else if (event->count == 0)
1822 TDEBUG (("in gtk_stext_expose (focus)\n"));
1823 gtk_widget_draw_focus (widget);
1830 gtk_stext_scroll_timeout (gpointer data)
1833 GdkEventMotion event;
1835 GdkModifierType mask;
1837 GDK_THREADS_ENTER ();
1839 text = GTK_STEXT (data);
1842 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1844 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1851 gtk_stext_motion_notify (GTK_WIDGET (text), &event);
1854 GDK_THREADS_LEAVE ();
1860 gtk_stext_button_press (GtkWidget *widget,
1861 GdkEventButton *event)
1864 GtkEditable *editable;
1865 static GdkAtom ctext_atom = GDK_NONE;
1867 g_return_val_if_fail (widget != NULL, FALSE);
1868 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1869 g_return_val_if_fail (event != NULL, FALSE);
1871 if (ctext_atom == GDK_NONE)
1872 ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
1874 text = GTK_STEXT (widget);
1875 editable = GTK_EDITABLE (widget);
1877 if (text->button && (event->button != text->button))
1880 text->button = event->button;
1882 if (!GTK_WIDGET_HAS_FOCUS (widget))
1883 gtk_widget_grab_focus (widget);
1885 if (event->button == 1)
1887 switch (event->type)
1889 case GDK_BUTTON_PRESS:
1890 gtk_grab_add (widget);
1892 undraw_cursor (text, FALSE);
1893 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1894 draw_cursor (text, FALSE);
1896 /* Set it now, so we display things right. We'll unset it
1897 * later if things don't work out */
1898 editable->has_selection = TRUE;
1899 gtk_stext_set_selection (GTK_EDITABLE(text),
1900 text->cursor_mark.index,
1901 text->cursor_mark.index);
1905 case GDK_2BUTTON_PRESS:
1906 gtk_stext_select_word (text, event->time);
1909 case GDK_3BUTTON_PRESS:
1910 gtk_stext_select_line (text, event->time);
1917 else if (event->type == GDK_BUTTON_PRESS)
1919 if ((event->button == 2) && editable->editable)
1921 if (editable->selection_start_pos == editable->selection_end_pos ||
1922 editable->has_selection)
1924 undraw_cursor (text, FALSE);
1925 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1926 draw_cursor (text, FALSE);
1930 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1931 ctext_atom, event->time);
1933 /* SYLPHEED: cancel current selection after pasting */
1934 gtk_stext_set_selection (GTK_EDITABLE(text),
1935 text->cursor_mark.index,
1936 text->cursor_mark.index);
1937 editable->has_selection = FALSE;
1941 gtk_grab_add (widget);
1943 undraw_cursor (text, FALSE);
1944 find_mouse_cursor (text, event->x, event->y);
1945 draw_cursor (text, FALSE);
1947 gtk_stext_set_selection (GTK_EDITABLE(text),
1948 text->cursor_mark.index,
1949 text->cursor_mark.index);
1951 editable->has_selection = FALSE;
1952 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1953 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1961 gtk_stext_button_release (GtkWidget *widget,
1962 GdkEventButton *event)
1965 GtkEditable *editable;
1966 g_return_val_if_fail (widget != NULL, FALSE);
1967 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1968 g_return_val_if_fail (event != NULL, FALSE);
1970 text = GTK_STEXT (widget);
1972 gtk_grab_remove (widget);
1974 if (text->button != event->button)
1981 gtk_timeout_remove (text->timer);
1985 if (event->button == 1)
1987 text = GTK_STEXT (widget);
1988 editable = GTK_EDITABLE (widget);
1990 gtk_grab_remove (widget);
1992 editable->has_selection = FALSE;
1993 if (editable->selection_start_pos != editable->selection_end_pos)
1995 if (gtk_selection_owner_set (widget,
1996 GDK_SELECTION_PRIMARY,
1998 editable->has_selection = TRUE;
2000 gtk_stext_update_text (editable, editable->selection_start_pos,
2001 editable->selection_end_pos);
2005 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
2006 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
2009 else if (event->button == 3)
2011 gtk_grab_remove (widget);
2014 undraw_cursor (text, FALSE);
2015 find_cursor (text, TRUE);
2016 draw_cursor (text, FALSE);
2022 gtk_stext_motion_notify (GtkWidget *widget,
2023 GdkEventMotion *event)
2028 GdkModifierType mask;
2030 g_return_val_if_fail (widget != NULL, FALSE);
2031 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2032 g_return_val_if_fail (event != NULL, FALSE);
2034 text = GTK_STEXT (widget);
2038 mask = event->state;
2039 if (event->is_hint || (text->text_area != event->window))
2041 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
2044 if ((text->button == 0) ||
2045 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
2048 gdk_window_get_size (text->text_area, NULL, &height);
2050 if ((y < 0) || (y > height))
2052 if (text->timer == 0)
2054 text->timer = gtk_timeout_add (SCROLL_TIME,
2055 gtk_stext_scroll_timeout,
2059 scroll_int (text, y/2);
2061 scroll_int (text, (y - height)/2);
2067 undraw_cursor (GTK_STEXT (widget), FALSE);
2068 find_mouse_cursor (GTK_STEXT (widget), x, y);
2069 draw_cursor (GTK_STEXT (widget), FALSE);
2071 gtk_stext_set_selection (GTK_EDITABLE(text),
2072 GTK_EDITABLE(text)->selection_start_pos,
2073 text->cursor_mark.index);
2079 gtk_stext_insert_text (GtkEditable *editable,
2080 const gchar *new_text,
2081 gint new_text_length,
2084 GtkSText *text = GTK_STEXT (editable);
2086 GdkColor *fore, *back;
2088 TextProperty *property;
2090 gtk_stext_set_point (text, *position);
2092 property = MARK_CURRENT_PROPERTY (&text->point);
2093 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
2094 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
2095 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
2097 gtk_stext_insert (text, font, fore, back, new_text, new_text_length);
2099 *position = text->point.index;
2103 gtk_stext_delete_text (GtkEditable *editable,
2109 g_return_if_fail (start_pos >= 0);
2111 text = GTK_STEXT (editable);
2113 gtk_stext_set_point (text, start_pos);
2115 end_pos = TEXT_LENGTH (text);
2117 if (end_pos > start_pos)
2118 gtk_stext_forward_delete (text, end_pos - start_pos);
2122 gtk_stext_key_press (GtkWidget *widget,
2126 GtkEditable *editable;
2131 g_return_val_if_fail (widget != NULL, FALSE);
2132 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2133 g_return_val_if_fail (event != NULL, FALSE);
2137 text = GTK_STEXT (widget);
2138 editable = GTK_EDITABLE (widget);
2140 key = event->keyval;
2143 if ((GTK_EDITABLE(text)->editable == FALSE))
2145 switch (event->keyval)
2148 if (event->state & GDK_CONTROL_MASK)
2149 scroll_int (text, -text->vadj->value);
2154 if (event->state & GDK_CONTROL_MASK)
2155 scroll_int (text, +text->vadj->upper);
2159 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
2160 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
2161 case GDK_Up: scroll_int (text, -KEY_SCROLL_PIXELS); break;
2162 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
2164 if (event->state & GDK_CONTROL_MASK)
2165 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2176 gint extend_selection;
2178 guint initial_pos = editable->current_pos;
2180 text->point = find_mark (text, text->cursor_mark.index);
2182 extend_selection = (event->state & GDK_SHIFT_MASK) &&
2183 (event->keyval != GDK_Return);
2184 extend_start = FALSE;
2186 if (extend_selection)
2188 editable->has_selection = TRUE;
2190 if (editable->selection_start_pos == editable->selection_end_pos)
2192 editable->selection_start_pos = text->point.index;
2193 editable->selection_end_pos = text->point.index;
2196 extend_start = (text->point.index == editable->selection_start_pos);
2197 /* SYLPHEED: cursor */
2198 gtk_stext_disable_blink (text);
2202 gtk_stext_enable_blink (text);
2208 if (event->keyval != GDK_Up && event->keyval != GDK_Down) {
2209 reset_persist_col_pos(text);
2212 switch (event->keyval)
2215 if (event->state & GDK_CONTROL_MASK) {
2216 if (text->wrap_rmargin == 0) {
2217 /* SYLPHEED: old behaviour */
2218 move_cursor_buffer_ver (text, -1);
2221 /* SYLPHEED: contrived, but "trusty" */
2222 move_cursor_buffer_ver(text, -1);
2223 move_cursor_to_display_row_start(text);
2227 if (text->wrap_rmargin > 0) {
2228 /* SYLPHEED: line start */
2229 move_cursor_to_display_row_start(text);
2232 gtk_stext_move_beginning_of_line (text);
2237 if (event->state & GDK_CONTROL_MASK) {
2238 /* SYLPHEED: a little bit contrived... */
2239 if (text->wrap_rmargin == 0) {
2241 move_cursor_buffer_ver (text, +1);
2244 move_cursor_buffer_ver(text, +1);
2245 move_cursor_to_display_row_end(text);
2249 if (text->wrap_rmargin > 0) {
2250 /* SYLPHEED: line end */
2251 move_cursor_to_display_row_end(text);
2254 gtk_stext_move_end_of_line(text);
2259 move_cursor_page_ver (text, -1);
2261 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2262 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2264 if (text->wrap_rmargin > 0) {
2267 move_cursor_to_display_row_up(text);
2270 move_cursor_ver (text, -1);
2274 move_cursor_to_display_row_down(text);
2275 /* move_cursor_ver (text, +1); */
2278 if (event->state & GDK_CONTROL_MASK)
2279 gtk_stext_move_backward_word (text);
2281 move_cursor_hor (text, -1);
2284 if (event->state & GDK_CONTROL_MASK)
2285 gtk_stext_move_forward_word (text);
2287 move_cursor_hor (text, +1);
2291 if (event->state & GDK_CONTROL_MASK)
2292 gtk_stext_delete_backward_word (text);
2294 gtk_stext_delete_backward_character (text);
2297 gtk_stext_delete_line (text);
2300 if (event->state & GDK_SHIFT_MASK)
2302 extend_selection = FALSE;
2303 gtk_editable_paste_clipboard (editable);
2305 else if (event->state & GDK_CONTROL_MASK)
2307 gtk_editable_copy_clipboard (editable);
2311 /* gtk_toggle_insert(text) -- IMPLEMENT */
2315 if (event->state & GDK_CONTROL_MASK)
2316 gtk_stext_delete_forward_word (text);
2317 else if (event->state & GDK_SHIFT_MASK)
2319 extend_selection = FALSE;
2320 gtk_editable_cut_clipboard (editable);
2323 gtk_stext_delete_forward_character (text);
2326 position = text->point.index;
2327 gtk_editable_insert_text (editable, "\t", 1, &position);
2330 if (event->state & GDK_CONTROL_MASK)
2331 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2334 position = text->point.index;
2335 gtk_editable_insert_text (editable, "\n", 1, &position);
2339 /* Don't insert literally */
2347 if (event->state & GDK_CONTROL_MASK)
2349 if ((key >= 'A') && (key <= 'Z'))
2352 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2354 (* control_keys[(int) (key - 'a')]) (editable, event->time);
2360 else if (event->state & GDK_MOD1_MASK)
2362 if ((key >= 'A') && (key <= 'Z'))
2365 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2367 (* alt_keys[(int) (key - 'a')]) (editable, event->time);
2373 else if (event->length > 0)
2375 if (!(event->state & GDK_CONTROL_MASK) &&
2376 !(event->state & GDK_MOD1_MASK) &&
2377 (event->length > 0))
2379 extend_selection = FALSE;
2381 gtk_editable_delete_selection (editable);
2382 position = text->point.index;
2383 gtk_editable_insert_text (editable, event->string, event->length, &position);
2391 if (return_val && (editable->current_pos != initial_pos))
2393 if (extend_selection)
2395 if (editable->current_pos < editable->selection_start_pos)
2396 gtk_stext_set_selection (editable, editable->current_pos,
2397 editable->selection_end_pos);
2398 else if (editable->current_pos > editable->selection_end_pos)
2399 gtk_stext_set_selection (editable, editable->selection_start_pos,
2400 editable->current_pos);
2404 gtk_stext_set_selection (editable, editable->current_pos,
2405 editable->selection_end_pos);
2407 gtk_stext_set_selection (editable, editable->selection_start_pos,
2408 editable->current_pos);
2412 gtk_stext_set_selection (editable, 0, 0);
2414 gtk_editable_claim_selection (editable,
2415 editable->selection_start_pos != editable->selection_end_pos,
2424 gtk_stext_focus_in (GtkWidget *widget,
2425 GdkEventFocus *event)
2427 g_return_val_if_fail (widget != NULL, FALSE);
2428 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2429 g_return_val_if_fail (event != NULL, FALSE);
2431 TDEBUG (("in gtk_stext_focus_in\n"));
2433 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2434 gtk_widget_draw_focus (widget);
2436 #ifdef USE_GTKGDK_XIM
2437 if (GTK_EDITABLE(widget)->ic)
2438 gdk_im_begin (GTK_EDITABLE(widget)->ic, GTK_STEXT(widget)->text_area);
2441 draw_cursor (GTK_STEXT(widget), TRUE);
2442 /* SYLPHEED: cursor */
2443 GTK_STEXT(widget)->cursor_visible = TRUE;
2444 gtk_stext_enable_blink (GTK_STEXT(widget));
2450 gtk_stext_focus_out (GtkWidget *widget,
2451 GdkEventFocus *event)
2453 g_return_val_if_fail (widget != NULL, FALSE);
2454 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2455 g_return_val_if_fail (event != NULL, FALSE);
2457 TDEBUG (("in gtk_stext_focus_out\n"));
2459 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2460 gtk_widget_draw_focus (widget);
2463 * should disable blink before undrawing the cursor - disabling blink
2464 * redraws the cursor.
2466 gtk_stext_disable_blink (GTK_STEXT(widget));
2467 undraw_cursor (GTK_STEXT(widget), TRUE);
2468 GTK_STEXT(widget)->cursor_visible = FALSE;
2470 #ifdef USE_GTKGDK_XIM
2478 gtk_stext_adjustment (GtkAdjustment *adjustment,
2483 g_return_if_fail (adjustment != NULL);
2484 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2485 g_return_if_fail (text != NULL);
2486 g_return_if_fail (GTK_IS_STEXT (text));
2488 /* Clamp the value here, because we'll get really confused
2489 * if someone tries to move the adjusment outside of the
2492 old_val = adjustment->value;
2494 adjustment->value = MIN (adjustment->value, adjustment->upper - adjustment->page_size);
2495 adjustment->value = MAX (adjustment->value, 0.0);
2497 if (adjustment->value != old_val)
2499 gtk_signal_handler_block_by_func (GTK_OBJECT (adjustment),
2500 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2502 gtk_adjustment_changed (adjustment);
2503 gtk_signal_handler_unblock_by_func (GTK_OBJECT (adjustment),
2504 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2508 /* Just ignore it if we haven't been size-allocated and realized yet */
2509 if (text->line_start_cache == NULL)
2512 if (adjustment == text->hadj)
2514 g_warning ("horizontal scrolling not implemented");
2518 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2522 undraw_cursor (text, FALSE);
2525 scroll_down (text, diff);
2526 else /* if (diff < 0) */
2527 scroll_up (text, diff);
2529 draw_cursor (text, FALSE);
2531 text->last_ver_value = adjustment->value;
2537 gtk_stext_disconnect (GtkAdjustment *adjustment,
2540 g_return_if_fail (adjustment != NULL);
2541 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2542 g_return_if_fail (text != NULL);
2543 g_return_if_fail (GTK_IS_STEXT (text));
2545 if (adjustment == text->hadj)
2546 gtk_stext_set_adjustments (text, NULL, text->vadj);
2547 if (adjustment == text->vadj)
2548 gtk_stext_set_adjustments (text, text->hadj, NULL);
2552 static GtkSPropertyMark
2553 find_this_line_start_mark (GtkSText* text, guint point_position, const GtkSPropertyMark* near)
2555 GtkSPropertyMark mark;
2557 mark = find_mark_near (text, point_position, near);
2559 while (mark.index > 0 &&
2560 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2561 decrement_mark (&mark);
2567 init_tab_cont (GtkSText* text, PrevTabCont* tab_cont)
2569 tab_cont->pixel_offset = 0;
2570 tab_cont->tab_start.tab_stops = text->tab_stops;
2571 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2573 if (!tab_cont->tab_start.to_next_tab)
2574 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2578 line_params_iterate (GtkSText* text,
2579 const GtkSPropertyMark* mark0,
2580 const PrevTabCont* tab_mark0,
2583 LineIteratorFunction iter)
2584 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2585 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2586 * for each line following MARK, iteration continues unless ITER_CALL
2589 GtkSPropertyMark mark = *mark0;
2590 PrevTabCont tab_conts[2];
2591 LineParams *lp, lpbuf;
2592 gint tab_cont_index = 0;
2595 tab_conts[0] = *tab_mark0;
2597 init_tab_cont (text, tab_conts);
2602 lp = g_chunk_new (LineParams, params_mem_chunk);
2606 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2607 tab_conts + (tab_cont_index + 1) % 2);
2609 if ((*iter) (text, lp, data))
2612 if (LAST_INDEX (text, lp->end))
2616 advance_mark (&mark);
2617 tab_cont_index = (tab_cont_index + 1) % 2;
2622 fetch_lines_iterator (GtkSText* text, LineParams* lp, void* data)
2624 FetchLinesData *fldata = (FetchLinesData*) data;
2626 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2628 switch (fldata->fl_type)
2630 case FetchLinesCount:
2631 if (!text->line_wrap || !lp->wraps)
2634 if (fldata->data >= fldata->data_max)
2638 case FetchLinesPixels:
2640 fldata->data += LINE_HEIGHT(*lp);
2642 if (fldata->data >= fldata->data_max)
2652 fetch_lines (GtkSText* text,
2653 const GtkSPropertyMark* mark0,
2654 const PrevTabCont* tab_cont0,
2658 FetchLinesData fl_data;
2660 fl_data.new_lines = NULL;
2662 fl_data.data_max = data;
2663 fl_data.fl_type = fl_type;
2665 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2667 return g_list_reverse (fl_data.new_lines);
2671 fetch_lines_backward (GtkSText* text)
2673 GList* new_lines = NULL, *new_line_start;
2674 GtkSPropertyMark mark;
2676 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2679 mark = find_this_line_start_mark (text,
2680 CACHE_DATA(text->line_start_cache).start.index - 1,
2681 &CACHE_DATA(text->line_start_cache).start);
2683 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2685 while (new_line_start->next)
2686 new_line_start = new_line_start->next;
2688 new_line_start->next = text->line_start_cache;
2689 text->line_start_cache->prev = new_line_start;
2693 fetch_lines_forward (GtkSText* text, gint line_count)
2695 GtkSPropertyMark mark;
2696 GList* line = text->line_start_cache;
2701 mark = CACHE_DATA(line).end;
2703 if (LAST_INDEX (text, mark))
2706 advance_mark(&mark);
2708 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2711 line->next->prev = line;
2714 /* Compute the number of lines, and vertical pixels for n characters
2715 * starting from the point
2718 compute_lines_pixels (GtkSText* text, guint char_count,
2719 guint *lines, guint *pixels)
2721 GList *line = text->current_line;
2722 gint chars_left = char_count;
2727 /* If chars_left == 0, that means we're joining two lines in a
2728 * deletion, so add in the values for the next line as well
2730 for (; line && chars_left >= 0; line = line->next)
2732 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2734 if (line == text->current_line)
2735 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2737 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2739 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2743 chars_left = 0; /* force another loop */
2746 fetch_lines_forward (text, 1);
2751 total_line_height (GtkSText* text, GList* line, gint line_count)
2755 for (; line && line_count > 0; line = line->next)
2757 height += LINE_HEIGHT(CACHE_DATA(line));
2759 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2763 fetch_lines_forward (text, line_count);
2770 swap_lines (GtkSText* text, GList* old, GList* new, guint old_line_count)
2772 if (old == text->line_start_cache)
2776 for (; old_line_count > 0; old_line_count -= 1)
2778 while (text->line_start_cache &&
2780 CACHE_DATA(text->line_start_cache).wraps)
2781 remove_cache_line(text, text->line_start_cache);
2783 remove_cache_line(text, text->line_start_cache);
2786 last = g_list_last (new);
2788 last->next = text->line_start_cache;
2790 if (text->line_start_cache)
2791 text->line_start_cache->prev = last;
2793 text->line_start_cache = new;
2799 g_assert (old->prev);
2803 for (; old_line_count > 0; old_line_count -= 1)
2805 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2806 old = remove_cache_line (text, old);
2808 old = remove_cache_line (text, old);
2814 last = g_list_last (new);
2824 correct_cache_delete (GtkSText* text, gint nchars, gint lines)
2826 GList* cache = text->current_line;
2829 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2832 for (; cache; cache = cache->next)
2834 GtkSPropertyMark *start = &CACHE_DATA(cache).start;
2835 GtkSPropertyMark *end = &CACHE_DATA(cache).end;
2837 start->index -= nchars;
2838 end->index -= nchars;
2840 if (LAST_INDEX (text, text->point) &&
2841 start->index == text->point.index)
2842 *start = text->point;
2843 else if (start->property == text->point.property)
2844 start->offset = start->index - (text->point.index - text->point.offset);
2846 if (LAST_INDEX (text, text->point) &&
2847 end->index == text->point.index)
2849 if (end->property == text->point.property)
2850 end->offset = end->index - (text->point.index - text->point.offset);
2852 /*TEXT_ASSERT_MARK(text, start, "start");*/
2853 /*TEXT_ASSERT_MARK(text, end, "end");*/
2858 delete_expose (GtkSText* text, guint nchars, guint old_lines, guint old_pixels)
2860 GtkWidget *widget = GTK_WIDGET (text);
2863 guint new_pixels = 0;
2865 GList* new_line = NULL;
2868 text->cursor_virtual_x = 0;
2870 correct_cache_delete (text, nchars, old_lines);
2872 pixel_height = pixel_height_of(text, text->current_line) -
2873 LINE_HEIGHT(CACHE_DATA(text->current_line));
2875 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2876 CACHE_DATA(text->current_line).start = text->point;
2878 new_line = fetch_lines (text,
2879 &CACHE_DATA(text->current_line).start,
2880 &CACHE_DATA(text->current_line).tab_cont,
2884 swap_lines (text, text->current_line, new_line, old_lines);
2886 text->current_line = new_line;
2888 new_pixels = total_line_height (text, new_line, 1);
2890 gdk_window_get_size (text->text_area, &width, &height);
2892 if (old_pixels != new_pixels)
2894 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2896 gdk_draw_pixmap (text->text_area,
2900 pixel_height + old_pixels,
2902 pixel_height + new_pixels,
2906 text->vadj->upper += new_pixels;
2907 text->vadj->upper -= old_pixels;
2908 adjust_adj (text, text->vadj);
2912 rect.y = pixel_height;
2914 rect.height = new_pixels;
2916 expose_text (text, &rect, FALSE);
2917 gtk_stext_draw_focus ( (GtkWidget *) text);
2919 text->cursor_mark = text->point;
2921 find_cursor (text, TRUE);
2923 if (old_pixels != new_pixels)
2925 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2928 rect.y = pixel_height + new_pixels;
2930 rect.height = height - rect.y;
2932 expose_text (text, &rect, FALSE);
2935 process_exposes (text);
2942 /* note, the point has already been moved forward */
2944 correct_cache_insert (GtkSText* text, gint nchars)
2947 GtkSPropertyMark *start;
2948 GtkSPropertyMark *end;
2950 /* If we inserted a property exactly at the beginning of the
2951 * line, we have to correct here, or fetch_lines will
2954 start = &CACHE_DATA(text->current_line).start;
2955 if (start->index == text->point.index - nchars)
2957 *start = text->point;
2958 move_mark_n (start, -nchars);
2961 /* Now correct the offsets, and check for start or end marks that
2962 * are after the point, yet point to a property before the point's
2963 * property. This indicates that they are meant to point to the
2964 * second half of a property we split in insert_text_property(), so
2965 * we fix them up that way.
2967 cache = text->current_line->next;
2969 for (; cache; cache = cache->next)
2971 start = &CACHE_DATA(cache).start;
2972 end = &CACHE_DATA(cache).end;
2974 if (LAST_INDEX (text, text->point) &&
2975 start->index == text->point.index)
2976 *start = text->point;
2979 if (start->property == text->point.property)
2981 start->offset += nchars;
2982 start->index += nchars;
2984 else if (start->property->next &&
2985 (start->property->next->next == text->point.property))
2987 /* We split the property, and this is the second half */
2988 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2989 start->index += nchars;
2990 start->property = text->point.property;
2993 start->index += nchars;
2996 if (LAST_INDEX (text, text->point) &&
2997 end->index == text->point.index)
3001 if (end->property == text->point.property)
3003 end->offset += nchars;
3004 end->index += nchars;
3006 else if (end->property->next &&
3007 (end->property->next->next == text->point.property))
3009 /* We split the property, and this is the second half */
3010 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
3011 end->index += nchars;
3012 end->property = text->point.property;
3015 end->index += nchars;
3018 /*TEXT_ASSERT_MARK(text, start, "start");*/
3019 /*TEXT_ASSERT_MARK(text, end, "end");*/
3025 insert_expose (GtkSText* text, guint old_pixels, gint nchars,
3026 guint new_line_count)
3028 GtkWidget *widget = GTK_WIDGET (text);
3031 guint new_pixels = 0;
3033 GList* new_lines = NULL;
3036 text->cursor_virtual_x = 0;
3038 undraw_cursor (text, FALSE);
3040 correct_cache_insert (text, nchars);
3042 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3044 pixel_height = pixel_height_of(text, text->current_line) -
3045 LINE_HEIGHT(CACHE_DATA(text->current_line));
3047 new_lines = fetch_lines (text,
3048 &CACHE_DATA(text->current_line).start,
3049 &CACHE_DATA(text->current_line).tab_cont,
3053 swap_lines (text, text->current_line, new_lines, 1);
3055 text->current_line = new_lines;
3057 new_pixels = total_line_height (text, new_lines, new_line_count);
3059 gdk_window_get_size (text->text_area, &width, &height);
3061 if (old_pixels != new_pixels)
3063 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
3065 gdk_draw_pixmap (text->text_area,
3069 pixel_height + old_pixels,
3071 pixel_height + new_pixels,
3073 height + (old_pixels - new_pixels) - pixel_height);
3076 text->vadj->upper += new_pixels;
3077 text->vadj->upper -= old_pixels;
3078 adjust_adj (text, text->vadj);
3082 rect.y = pixel_height;
3084 rect.height = new_pixels;
3086 expose_text (text, &rect, FALSE);
3087 gtk_stext_draw_focus ( (GtkWidget *) text);
3089 text->cursor_mark = text->point;
3091 find_cursor (text, TRUE);
3093 draw_cursor (text, FALSE);
3095 if (old_pixels != new_pixels)
3097 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
3100 rect.y = pixel_height + new_pixels;
3102 rect.height = height - rect.y;
3104 expose_text (text, &rect, FALSE);
3107 process_exposes (text);
3110 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3115 /* Text property functions */
3118 font_hash (gconstpointer font)
3120 return gdk_font_id ((const GdkFont*) font);
3123 static GHashTable *font_cache_table = NULL;
3125 static GtkSTextFont*
3126 get_text_font (GdkFont* gfont)
3131 if (!font_cache_table)
3132 font_cache_table = g_hash_table_new (font_hash, (GCompareFunc) gdk_font_equal);
3134 tf = g_hash_table_lookup (font_cache_table, gfont);
3142 tf = g_new (GtkSTextFont, 1);
3145 tf->gdk_font = gfont;
3146 gdk_font_ref (gfont);
3148 for(i = 0; i < 256; i += 1)
3149 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
3151 g_hash_table_insert (font_cache_table, gfont, tf);
3157 text_font_unref (GtkSTextFont *text_font)
3159 text_font->ref_count--;
3160 if (text_font->ref_count == 0)
3162 g_hash_table_remove (font_cache_table, text_font->gdk_font);
3163 gdk_font_unref (text_font->gdk_font);
3169 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
3171 if (prop->flags & PROPERTY_FONT)
3174 GtkSTextFont *text_font;
3179 text_font = get_text_font (font);
3181 retval = (prop->font == text_font);
3182 text_font_unref (text_font);
3191 if (prop->flags & PROPERTY_FOREGROUND)
3193 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
3200 if (prop->flags & PROPERTY_BACKGROUND)
3202 if (!back || !gdk_color_equal (&prop->back_color, back))
3213 realize_property (GtkSText *text, TextProperty *prop)
3215 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3217 if (prop->flags & PROPERTY_FOREGROUND)
3218 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
3220 if (prop->flags & PROPERTY_BACKGROUND)
3221 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
3225 realize_properties (GtkSText *text)
3227 GList *tmp_list = text->text_properties;
3231 realize_property (text, tmp_list->data);
3233 tmp_list = tmp_list->next;
3238 unrealize_property (GtkSText *text, TextProperty *prop)
3240 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3242 if (prop->flags & PROPERTY_FOREGROUND)
3243 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
3245 if (prop->flags & PROPERTY_BACKGROUND)
3246 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
3250 unrealize_properties (GtkSText *text)
3252 GList *tmp_list = text->text_properties;
3256 unrealize_property (text, tmp_list->data);
3258 tmp_list = tmp_list->next;
3262 static TextProperty*
3263 new_text_property (GtkSText *text, GdkFont *font, GdkColor* fore,
3264 GdkColor* back, guint length)
3268 if (text_property_chunk == NULL)
3270 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
3271 sizeof(TextProperty),
3272 1024*sizeof(TextProperty),
3276 prop = g_chunk_new(TextProperty, text_property_chunk);
3281 prop->flags |= PROPERTY_FONT;
3282 prop->font = get_text_font (font);
3289 prop->flags |= PROPERTY_FOREGROUND;
3290 prop->fore_color = *fore;
3295 prop->flags |= PROPERTY_BACKGROUND;
3296 prop->back_color = *back;
3299 prop->length = length;
3301 if (GTK_WIDGET_REALIZED (text))
3302 realize_property (text, prop);
3308 destroy_text_property (TextProperty *prop)
3311 text_font_unref (prop->font);
3313 g_mem_chunk_free (text_property_chunk, prop);
3316 /* Flop the memory between the point and the gap around like a
3319 move_gap (GtkSText* text, guint index)
3321 if (text->gap_position < index)
3323 gint diff = index - text->gap_position;
3325 if (text->use_wchar)
3326 g_memmove (text->text.wc + text->gap_position,
3327 text->text.wc + text->gap_position + text->gap_size,
3328 diff*sizeof (GdkWChar));
3330 g_memmove (text->text.ch + text->gap_position,
3331 text->text.ch + text->gap_position + text->gap_size,
3334 text->gap_position = index;
3336 else if (text->gap_position > index)
3338 gint diff = text->gap_position - index;
3340 if (text->use_wchar)
3341 g_memmove (text->text.wc + index + text->gap_size,
3342 text->text.wc + index,
3343 diff*sizeof (GdkWChar));
3345 g_memmove (text->text.ch + index + text->gap_size,
3346 text->text.ch + index,
3349 text->gap_position = index;
3353 /* Increase the gap size. */
3355 make_forward_space (GtkSText* text, guint len)
3357 if (text->gap_size < len)
3359 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3361 if (sum >= text->text_len)
3365 while (i <= sum) i <<= 1;
3367 if (text->use_wchar)
3368 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3369 i*sizeof(GdkWChar));
3371 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3375 if (text->use_wchar)
3376 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3377 text->text.wc + text->gap_position + text->gap_size,
3378 (text->text_end - (text->gap_position + text->gap_size))
3381 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3382 text->text.ch + text->gap_position + text->gap_size,
3383 text->text_end - (text->gap_position + text->gap_size));
3385 text->text_end += len*2;
3386 text->gap_size += len*2;
3390 /* Inserts into the text property list a list element that guarantees
3391 * that for len characters following the point, text has the correct
3392 * property. does not move point. adjusts text_properties_point and
3393 * text_properties_point_offset relative to the current value of
3396 insert_text_property (GtkSText* text, GdkFont* font,
3397 GdkColor *fore, GdkColor* back, guint len)
3399 GtkSPropertyMark *mark = &text->point;
3400 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3401 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3403 if (MARK_OFFSET(mark) == 0)
3405 /* Point is on the boundary of two properties.
3406 * If it is the same as either, grow, else insert
3409 if (text_properties_equal(forward_prop, font, fore, back))
3411 /* Grow the property in front of us. */
3413 MARK_PROPERTY_LENGTH(mark) += len;
3415 else if (backward_prop &&
3416 text_properties_equal(backward_prop, font, fore, back))
3418 /* Grow property behind us, point property and offset
3421 SET_PROPERTY_MARK (&text->point,
3422 MARK_PREV_LIST_PTR (mark),
3423 backward_prop->length);
3425 backward_prop->length += len;
3427 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3428 (forward_prop->length == 1))
3430 /* Next property just has last position, take it over */
3432 if (GTK_WIDGET_REALIZED (text))
3433 unrealize_property (text, forward_prop);
3435 forward_prop->flags = 0;
3438 forward_prop->flags |= PROPERTY_FONT;
3439 forward_prop->font = get_text_font (font);
3442 forward_prop->font = NULL;
3446 forward_prop->flags |= PROPERTY_FOREGROUND;
3447 forward_prop->fore_color = *fore;
3451 forward_prop->flags |= PROPERTY_BACKGROUND;
3452 forward_prop->back_color = *back;
3454 forward_prop->length += len;
3456 if (GTK_WIDGET_REALIZED (text))
3457 realize_property (text, forward_prop);
3461 /* Splice a new property into the list. */
3463 GList* new_prop = g_list_alloc();
3465 new_prop->next = MARK_LIST_PTR(mark);
3466 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3467 new_prop->next->prev = new_prop;
3470 new_prop->prev->next = new_prop;
3472 new_prop->data = new_text_property (text, font, fore, back, len);
3474 SET_PROPERTY_MARK (mark, new_prop, 0);
3479 /* The following will screw up the line_start cache,
3480 * we'll fix it up in correct_cache_insert
3483 /* In the middle of forward_prop, if properties are equal,
3484 * just add to its length, else split it into two and splice
3486 if (text_properties_equal (forward_prop, font, fore, back))
3488 forward_prop->length += len;
3490 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3491 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3493 /* Inserting before only the last position in the text */
3496 forward_prop->length -= 1;
3498 new_prop = g_list_alloc();
3499 new_prop->data = new_text_property (text, font, fore, back, len+1);
3500 new_prop->prev = MARK_LIST_PTR(mark);
3501 new_prop->next = NULL;
3502 MARK_NEXT_LIST_PTR(mark) = new_prop;
3504 SET_PROPERTY_MARK (mark, new_prop, 0);
3508 GList* new_prop = g_list_alloc();
3509 GList* new_prop_forward = g_list_alloc();
3510 gint old_length = forward_prop->length;
3511 GList* next = MARK_NEXT_LIST_PTR(mark);
3513 /* Set the new lengths according to where they are split. Construct
3514 * two new properties. */
3515 forward_prop->length = MARK_OFFSET(mark);
3517 new_prop_forward->data =
3518 new_text_property(text,
3519 forward_prop->flags & PROPERTY_FONT ?
3520 forward_prop->font->gdk_font : NULL,
3521 forward_prop->flags & PROPERTY_FOREGROUND ?
3522 &forward_prop->fore_color : NULL,
3523 forward_prop->flags & PROPERTY_BACKGROUND ?
3524 &forward_prop->back_color : NULL,
3525 old_length - forward_prop->length);
3527 new_prop->data = new_text_property(text, font, fore, back, len);
3529 /* Now splice things in. */
3530 MARK_NEXT_LIST_PTR(mark) = new_prop;
3531 new_prop->prev = MARK_LIST_PTR(mark);
3533 new_prop->next = new_prop_forward;
3534 new_prop_forward->prev = new_prop;
3536 new_prop_forward->next = next;
3539 next->prev = new_prop_forward;
3541 SET_PROPERTY_MARK (mark, new_prop, 0);
3545 while (text->text_properties_end->next)
3546 text->text_properties_end = text->text_properties_end->next;
3548 while (text->text_properties->prev)
3549 text->text_properties = text->text_properties->prev;
3553 delete_text_property (GtkSText* text, guint nchars)
3555 /* Delete nchars forward from point. */
3557 /* Deleting text properties is problematical, because we
3558 * might be storing around marks pointing to a property.
3560 * The marks in question and how we handle them are:
3562 * point: We know the new value, since it will be at the
3563 * end of the deleted text, and we move it there
3565 * cursor: We just remove the mark and set it equal to the
3566 * point after the operation.
3567 * line-start cache: We replace most affected lines.
3568 * The current line gets used to fetch the new
3569 * lines so, if necessary, (delete at the beginning
3570 * of a line) we fix it up by setting it equal to the
3578 for(; nchars; nchars -= 1)
3580 prop = MARK_CURRENT_PROPERTY(&text->point);
3584 if (prop->length == 0)
3586 tmp = MARK_LIST_PTR (&text->point);
3588 is_first = tmp == text->text_properties;
3590 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3591 text->point.offset = 0;
3593 if (GTK_WIDGET_REALIZED (text))
3594 unrealize_property (text, prop);
3596 destroy_text_property (prop);
3597 g_list_free_1 (tmp);
3599 prop = MARK_CURRENT_PROPERTY (&text->point);
3602 text->text_properties = MARK_LIST_PTR (&text->point);
3604 g_assert (prop->length != 0);
3606 else if (prop->length == text->point.offset)
3608 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3609 text->point.offset = 0;
3613 /* Check to see if we have just the single final position remaining
3614 * along in a property; if so, combine it with the previous property
3616 if (LAST_INDEX (text, text->point) &&
3617 (MARK_OFFSET (&text->point) == 0) &&
3618 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3620 tmp = MARK_LIST_PTR (&text->point);
3621 prop = MARK_CURRENT_PROPERTY(&text->point);
3623 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3624 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3625 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3627 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3629 if (GTK_WIDGET_REALIZED (text))
3630 unrealize_property (text, prop);
3632 destroy_text_property (prop);
3633 g_list_free_1 (tmp);
3638 init_properties (GtkSText *text)
3640 if (!text->text_properties)
3642 text->text_properties = g_list_alloc();
3643 text->text_properties->next = NULL;
3644 text->text_properties->prev = NULL;
3645 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3646 text->text_properties_end = text->text_properties;
3648 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3650 text->point.index = 0;
3655 /**********************************************************************/
3656 /* Property Movement */
3657 /**********************************************************************/
3660 move_mark_n (GtkSPropertyMark* mark, gint n)
3663 advance_mark_n(mark, n);
3665 decrement_mark_n(mark, -n);
3669 advance_mark (GtkSPropertyMark* mark)
3671 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3675 if (prop->length > mark->offset + 1)
3679 mark->property = MARK_NEXT_LIST_PTR (mark);
3685 advance_mark_n (GtkSPropertyMark* mark, gint n)
3692 i = 0; /* otherwise it migth not be init. */
3693 prop = MARK_CURRENT_PROPERTY(mark);
3695 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3696 /* to make it easier */
3697 n += (mark->offset);
3698 mark->index -= mark->offset;
3700 /* first we take seven-mile-leaps to get to the right text
3702 while ((n-i) > prop->length - 1) {
3704 mark->index += prop->length;
3705 mark->property = MARK_NEXT_LIST_PTR (mark);
3706 prop = MARK_CURRENT_PROPERTY (mark);
3710 /* and then the rest */
3711 mark->index += n - i;
3712 mark->offset += n - i;
3716 decrement_mark (GtkSPropertyMark* mark)
3720 if (mark->offset > 0)
3724 mark->property = MARK_PREV_LIST_PTR (mark);
3725 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3730 decrement_mark_n (GtkSPropertyMark* mark, gint n)
3734 while (mark->offset < n) {
3735 /* jump to end of prev */
3736 n -= mark->offset + 1;
3737 mark->index -= mark->offset + 1;
3738 mark->property = MARK_PREV_LIST_PTR (mark);
3739 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3747 static GtkSPropertyMark
3748 find_mark (GtkSText* text, guint mark_position)
3750 return find_mark_near (text, mark_position, &text->point);
3754 * You can also start from the end, what a drag.
3756 static GtkSPropertyMark
3757 find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near)
3762 GtkSPropertyMark mark;
3765 diffa = mark_position + 1;
3767 diffa = mark_position - near->index;
3769 diffb = mark_position;
3781 mark.property = text->text_properties;
3785 move_mark_n (&mark, mark_position - mark.index);
3790 /* This routine must be called with scroll == FALSE, only when
3791 * point is at least partially on screen
3795 find_line_containing_point (GtkSText* text, guint point,
3801 text->current_line = NULL;
3805 /* Scroll backwards until the point is on screen
3807 while (CACHE_DATA(text->line_start_cache).start.index > point)
3808 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3810 /* Now additionally try to make sure that the point is fully on screen
3814 while (text->first_cut_pixels != 0 &&
3815 text->line_start_cache->next &&
3816 CACHE_DATA(text->line_start_cache->next).start.index > point)
3817 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3820 gdk_window_get_size (text->text_area, NULL, &height);
3822 for (cache = text->line_start_cache; cache; cache = cache->next)
3826 if (CACHE_DATA(cache).end.index >= point ||
3827 LAST_INDEX(text, CACHE_DATA(cache).end))
3829 text->current_line = cache; /* LOOK HERE, this proc has an
3830 * important side effect. */
3834 TEXT_SHOW_LINE (text, cache, "cache");
3836 if (cache->next == NULL)
3837 fetch_lines_forward (text, 1);
3841 lph = pixel_height_of (text, cache->next);
3843 /* Scroll the bottom of the line is on screen, or until
3844 * the line is the first onscreen line.
3846 while (cache->next != text->line_start_cache && lph > height)
3848 TEXT_SHOW_LINE (text, cache, "cache");
3849 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3850 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3851 lph = pixel_height_of (text, cache->next);
3856 g_assert_not_reached (); /* Must set text->current_line here */
3860 pixel_height_of (GtkSText* text, GList* cache_line)
3862 gint pixels = - text->first_cut_pixels;
3863 GList *cache = text->line_start_cache;
3866 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3868 if (cache->data == cache_line->data)
3871 cache = cache->next;
3877 /**********************************************************************/
3878 /* Search and Placement */
3879 /**********************************************************************/
3882 find_char_width (GtkSText* text, const GtkSPropertyMark *mark, const TabStopMark *tab_mark)
3885 gint16* char_widths;
3887 if (LAST_INDEX (text, *mark))
3890 ch = GTK_STEXT_INDEX (text, mark->index);
3891 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3895 return tab_mark->to_next_tab * char_widths[' '];
3897 else if (!text->use_wchar)
3899 return char_widths[ch];
3903 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3908 advance_tab_mark (GtkSText* text, TabStopMark* tab_mark, GdkWChar ch)
3910 if (tab_mark->to_next_tab == 1 || ch == '\t')
3912 if (tab_mark->tab_stops->next)
3914 tab_mark->tab_stops = tab_mark->tab_stops->next;
3915 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3919 tab_mark->to_next_tab = text->default_tab_width;
3924 tab_mark->to_next_tab -= 1;
3929 advance_tab_mark_n (GtkSText* text, TabStopMark* tab_mark, gint n)
3933 advance_tab_mark (text, tab_mark, 0);
3937 find_cursor_at_line (GtkSText* text, const LineParams* start_line, gint pixel_height)
3940 GtkEditable *editable = (GtkEditable *)text;
3942 GtkSPropertyMark mark = start_line->start;
3943 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3944 gint pixel_width = LINE_START_PIXEL (*start_line);
3946 while (mark.index < text->cursor_mark.index)
3948 pixel_width += find_char_width (text, &mark, &tab_mark);
3950 advance_tab_mark (text, &tab_mark, GTK_STEXT_INDEX(text, mark.index));
3951 advance_mark (&mark);
3954 text->cursor_pos_x = pixel_width;
3955 text->cursor_pos_y = pixel_height;
3956 text->cursor_char_offset = start_line->font_descent;
3957 text->cursor_mark = mark;
3959 ch = LAST_INDEX (text, mark) ?
3960 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
3962 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3963 text->cursor_char = 0;
3965 text->cursor_char = ch;
3967 #ifdef USE_GTKGDK_XIM
3968 if (GTK_WIDGET_HAS_FOCUS(text) && gdk_im_ready() && editable->ic &&
3969 (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
3971 GdkICAttributesType mask = GDK_IC_SPOT_LOCATION |
3972 GDK_IC_PREEDIT_FOREGROUND |
3973 GDK_IC_PREEDIT_BACKGROUND;
3975 editable->ic_attr->spot_location.x = text->cursor_pos_x;
3976 editable->ic_attr->spot_location.y
3977 = text->cursor_pos_y - text->cursor_char_offset;
3978 editable->ic_attr->preedit_foreground = *MARK_CURRENT_FORE (text, &mark);
3979 editable->ic_attr->preedit_background = *MARK_CURRENT_BACK (text, &mark);
3981 if (MARK_CURRENT_FONT (text, &mark)->type == GDK_FONT_FONTSET)
3983 mask |= GDK_IC_PREEDIT_FONTSET;
3984 editable->ic_attr->preedit_fontset = MARK_CURRENT_FONT (text, &mark);
3987 gdk_ic_set_attr (editable->ic, editable->ic_attr, mask);
3993 find_cursor (GtkSText* text, gboolean scroll)
3995 if (GTK_WIDGET_REALIZED (text))
3997 find_line_containing_point (text, text->cursor_mark.index, scroll);
3999 if (text->current_line)
4000 find_cursor_at_line (text,
4001 &CACHE_DATA(text->current_line),
4002 pixel_height_of(text, text->current_line));
4005 GTK_EDITABLE (text)->current_pos = text->cursor_mark.index;
4009 find_mouse_cursor_at_line (GtkSText *text, const LineParams* lp,
4010 guint line_pixel_height,
4013 GtkSPropertyMark mark = lp->start;
4014 TabStopMark tab_mark = lp->tab_cont.tab_start;
4016 gint char_width = find_char_width(text, &mark, &tab_mark);
4017 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
4019 text->cursor_pos_y = line_pixel_height;
4023 GdkWChar ch = LAST_INDEX (text, mark) ?
4024 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
4026 if (button_x < pixel_width || mark.index == lp->end.index)
4028 text->cursor_pos_x = pixel_width - (char_width+1)/2;
4029 text->cursor_mark = mark;
4030 text->cursor_char_offset = lp->font_descent;
4032 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
4033 text->cursor_char = 0;
4035 text->cursor_char = ch;
4040 advance_tab_mark (text, &tab_mark, ch);
4041 advance_mark (&mark);
4043 pixel_width += char_width/2;
4045 char_width = find_char_width (text, &mark, &tab_mark);
4047 pixel_width += (char_width+1)/2;
4052 find_mouse_cursor (GtkSText* text, gint x, gint y)
4055 GList* cache = text->line_start_cache;
4059 pixel_height = - text->first_cut_pixels;
4061 for (; cache; cache = cache->next)
4063 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
4065 if (y < pixel_height || !cache->next)
4067 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
4069 find_cursor (text, FALSE);
4076 /**********************************************************************/
4078 /**********************************************************************/
4081 free_cache (GtkSText* text)
4083 GList* cache = text->line_start_cache;
4088 cache = cache->prev;
4090 text->line_start_cache = cache;
4093 for (; cache; cache = cache->next)
4094 g_mem_chunk_free (params_mem_chunk, cache->data);
4096 g_list_free (text->line_start_cache);
4098 text->line_start_cache = NULL;
4102 remove_cache_line (GtkSText* text, GList* member)
4109 if (member == text->line_start_cache)
4110 text->line_start_cache = text->line_start_cache->next;
4113 member->prev->next = member->next;
4116 member->next->prev = member->prev;
4118 list = member->next;
4120 g_mem_chunk_free (params_mem_chunk, member->data);
4121 g_list_free_1 (member);
4126 /**********************************************************************/
4128 /**********************************************************************/
4131 move_cursor_buffer_ver (GtkSText *text, int dir)
4133 undraw_cursor (text, FALSE);
4137 scroll_int (text, text->vadj->upper);
4138 text->cursor_mark = find_this_line_start_mark (text,
4140 &text->cursor_mark);
4144 scroll_int (text, - text->vadj->value);
4145 text->cursor_mark = find_this_line_start_mark (text,
4147 &text->cursor_mark);
4150 find_cursor (text, TRUE);
4151 draw_cursor (text, FALSE);
4155 move_cursor_page_ver (GtkSText *text, int dir)
4157 scroll_int (text, dir * text->vadj->page_increment);
4161 move_cursor_ver (GtkSText *text, int count)
4164 GtkSPropertyMark mark;
4167 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
4168 offset = text->cursor_mark.index - mark.index;
4170 if (offset > text->cursor_virtual_x)
4171 text->cursor_virtual_x = offset;
4175 if (mark.index == 0)
4178 decrement_mark (&mark);
4179 mark = find_this_line_start_mark (text, mark.index, &mark);
4183 mark = text->cursor_mark;
4185 while (!LAST_INDEX(text, mark) && GTK_STEXT_INDEX(text, mark.index) != LINE_DELIM)
4186 advance_mark (&mark);
4188 if (LAST_INDEX(text, mark))
4191 advance_mark (&mark);
4194 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
4195 if (LAST_INDEX(text, mark) ||
4196 GTK_STEXT_INDEX(text, mark.index) == LINE_DELIM)
4199 undraw_cursor (text, FALSE);
4201 text->cursor_mark = mark;
4203 find_cursor (text, TRUE);
4205 draw_cursor (text, FALSE);
4209 move_cursor_hor (GtkSText *text, int count)
4211 /* count should be +-1. */
4212 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
4213 (count < 0 && text->cursor_mark.index < (- count)) ||
4217 text->cursor_virtual_x = 0;
4219 undraw_cursor (text, FALSE);
4221 move_mark_n (&text->cursor_mark, count);
4223 find_cursor (text, TRUE);
4225 draw_cursor (text, FALSE);
4228 /* SYLPHEED - the default cursor movement of GtkText is aeons ahead of the
4229 * current technology level of the current generation of programmers and
4230 * end users. so sylpheed has to take a more mundane approach to editing.
4233 * move_cursor_to_display_row_end() (end of display line)
4234 * move_cursor_to_display_row_home() (home of display line)
4235 * move_cursor_to_display_row_up() (line up)
4236 * move_cursor_to_display_row_down() (line down)
4240 * SYLPHEED_TODO: for some reason the line fetcher also returns markers
4241 * of just one character! should investigate this... -- alfons
4244 static void move_cursor_to_display_row_end(GtkSText *text)
4246 LineParams lp = CACHE_DATA(text->current_line);
4247 int to_move = (lp.end.index - text->cursor_mark.index);
4249 /* advance this much */
4251 move_cursor_hor(text, to_move);
4255 static void move_cursor_to_display_row_start(GtkSText *text)
4257 LineParams lp = CACHE_DATA(text->current_line);
4258 int to_move = (text->cursor_mark.index - lp.start.index);
4260 move_cursor_hor(text, -to_move);
4265 static gboolean range_intersect(guint x1, guint x2, guint y1, guint y2)
4268 if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; }
4269 if (y1 > y2) { tmp = y1; y1 = y2; y1 = tmp; }
4270 if (y1 < x1) { tmp = x1; x1 = y1; y1 = tmp; tmp = x2; x2 = y2; y2 = tmp; }
4280 static gint back_display_row_fetcher(GtkSText *text,
4284 if (range_intersect(data->start, data->end, params->start.index, params->end.index)) {
4285 XDEBUG( ("%s(%d) - FOUND search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4286 data->start, data->end,
4287 params->start.index, params->end.index) );
4292 XDEBUG( ("%s(%d) - NEXT search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4293 data->start, data->end,
4294 params->start.index, params->end.index) );
4300 static void move_cursor_to_display_row_up(GtkSText *text)
4306 GtkSPropertyMark mark;
4308 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4311 if (mark.index == 0) {
4312 XDEBUG ( ("%s(%d) top of buffer", __FILE__, __LINE__) );
4315 /* we need previous DISPLAY row not the previous BUFFER line, so we go to start
4316 * of paragraph, and iterate over the lines until we have a LineParams that matches
4318 lp = CACHE_DATA(text->current_line);
4319 col = (text->cursor_mark.index - lp.start.index);
4320 data.start = lp.start.index;
4321 data.end = lp.end.index;
4323 /* get the previous line */
4324 if (mark.index != 0) {
4325 decrement_mark(&mark);
4326 if (mark.index != 0) {
4327 GtkSPropertyMark smark = mark;
4328 XDEBUG( ("%s(%d) finding line start mark", __FILE__, __LINE__) );
4329 mark = find_this_line_start_mark(text, smark.index -1, &smark);
4333 /* let's get the previous display line */
4334 XDEBUG( ("%s(%d) iterating to get display lines", __FILE__, __LINE__) );
4335 line_params_iterate(text, &mark, NULL, FALSE, &data,
4336 (LineIteratorFunction)back_display_row_fetcher);
4337 XDEBUG( ("%s(%d) done iterating. found = %d", __FILE__, __LINE__, data.found) );
4340 if (col < text->persist_column) col = text->persist_column;
4341 else text->persist_column = col;
4342 new_index = data.lp.start.index + col;
4343 XDEBUG( ("%s(%d) - new index = %d", __FILE__, __LINE__, new_index) );
4344 if (new_index > data.lp.end.index) {
4345 new_index = data.lp.end.index;
4347 /* and move the cursor */
4348 XDEBUG( ("%s(%d) - setting index", __FILE__, __LINE__) );
4349 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4360 #if defined(AHX_DEBUG)
4361 static void print_line(GtkSText *text, guint start, guint end)
4363 gchar *buf = alloca(2048), *walk = buf;
4365 memset(buf, 0, 2048);
4366 for (; start <= end; start++) {
4367 *walk++ = GTK_STEXT_INDEX(text, start);
4369 XDEBUG( ("%s", buf) );
4373 static gint forward_display_row_fetcher(GtkSText *text,
4378 XDEBUG( ("%s(%d) - FW RETURNS. search (%d, %d), current (%d, %d)",
4379 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4381 data->completed = TRUE;
4382 print_line(text, lp->start.index, lp->end.index);
4385 else if (range_intersect(data->start, data->end, lp->start.index, lp->end.index)) {
4386 XDEBUG( ("%s(%d) - FW FOUND IT. search (%d, %d), current (%d, %d)",
4387 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4396 static void move_cursor_to_display_row_down (GtkSText *text)
4401 GtkSPropertyMark mark;
4402 fdrf data = { FALSE, FALSE };
4404 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4405 lp = CACHE_DATA(text->current_line);
4406 col = (text->cursor_mark.index - lp.start.index);
4408 data.start = lp.start.index;
4409 data.end = lp.end.index;
4411 /* find the next DISPLAY line */
4412 XDEBUG( ("%s(%d) - FW iterating", __FILE__, __LINE__) ) ;
4413 line_params_iterate(text, &mark, NULL, FALSE, &data,
4414 (LineIteratorFunction)forward_display_row_fetcher);
4415 XDEBUG( ("%s(%d) - FW done iterating", __FILE__, __LINE__) );
4417 if (data.completed) {
4418 if (col < text->persist_column) col = text->persist_column;
4419 else text->persist_column = col;
4420 new_index = data.lp.start.index + col;
4421 if (new_index > data.lp.end.index) {
4422 new_index = data.lp.end.index;
4424 /* and move the cursor */
4425 XDEBUG( ("%s(%d) - FW set pos %d", __FILE__, __LINE__, new_index) );
4426 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4430 static void reset_persist_col_pos(GtkSText *text)
4432 text->persist_column = 0;
4436 gtk_stext_move_cursor (GtkEditable *editable,
4443 move_cursor_hor (GTK_STEXT (editable), 1);
4448 move_cursor_hor (GTK_STEXT (editable), -1);
4454 move_cursor_ver (GTK_STEXT (editable), 1);
4459 move_cursor_ver (GTK_STEXT (editable), -1);
4464 gtk_stext_move_forward_character (GtkSText *text)
4466 move_cursor_hor (text, 1);
4470 gtk_stext_move_backward_character (GtkSText *text)
4472 move_cursor_hor (text, -1);
4476 gtk_stext_move_next_line (GtkSText *text)
4478 move_cursor_ver (text, 1);
4482 gtk_stext_move_previous_line (GtkSText *text)
4484 move_cursor_ver (text, -1);
4488 gtk_stext_move_word (GtkEditable *editable,
4494 gtk_stext_move_forward_word (GTK_STEXT (editable));
4499 gtk_stext_move_backward_word (GTK_STEXT (editable));
4504 gtk_stext_move_forward_word (GtkSText *text)
4506 text->cursor_virtual_x = 0;
4508 undraw_cursor (text, FALSE);
4510 if (text->use_wchar)
4512 while (!LAST_INDEX (text, text->cursor_mark) &&
4513 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4514 advance_mark (&text->cursor_mark);
4516 while (!LAST_INDEX (text, text->cursor_mark) &&
4517 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4518 advance_mark (&text->cursor_mark);
4522 while (!LAST_INDEX (text, text->cursor_mark) &&
4523 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4524 advance_mark (&text->cursor_mark);
4526 while (!LAST_INDEX (text, text->cursor_mark) &&
4527 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4528 advance_mark (&text->cursor_mark);
4531 find_cursor (text, TRUE);
4532 draw_cursor (text, FALSE);
4536 gtk_stext_move_backward_word (GtkSText *text)
4538 text->cursor_virtual_x = 0;
4540 undraw_cursor (text, FALSE);
4542 if (text->use_wchar)
4544 while ((text->cursor_mark.index > 0) &&
4545 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4546 decrement_mark (&text->cursor_mark);
4548 while ((text->cursor_mark.index > 0) &&
4549 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4550 decrement_mark (&text->cursor_mark);
4554 while ((text->cursor_mark.index > 0) &&
4555 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4556 decrement_mark (&text->cursor_mark);
4558 while ((text->cursor_mark.index > 0) &&
4559 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4560 decrement_mark (&text->cursor_mark);
4563 find_cursor (text, TRUE);
4564 draw_cursor (text, FALSE);
4568 gtk_stext_move_page (GtkEditable *editable,
4573 scroll_int (GTK_STEXT (editable),
4574 y * GTK_STEXT(editable)->vadj->page_increment);
4578 gtk_stext_move_to_row (GtkEditable *editable,
4584 gtk_stext_move_to_column (GtkEditable *editable,
4589 text = GTK_STEXT (editable);
4591 text->cursor_virtual_x = 0; /* FIXME */
4593 undraw_cursor (text, FALSE);
4595 /* Move to the beginning of the line */
4596 while ((text->cursor_mark.index > 0) &&
4597 (GTK_STEXT_INDEX (text, text->cursor_mark.index - 1) != LINE_DELIM))
4598 decrement_mark (&text->cursor_mark);
4600 while (!LAST_INDEX (text, text->cursor_mark) &&
4601 (GTK_STEXT_INDEX (text, text->cursor_mark.index) != LINE_DELIM))
4605 else if (column == 0)
4608 advance_mark (&text->cursor_mark);
4611 find_cursor (text, TRUE);
4612 draw_cursor (text, FALSE);
4616 gtk_stext_move_beginning_of_line (GtkSText *text)
4618 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4623 gtk_stext_move_end_of_line (GtkSText *text)
4625 gtk_stext_move_to_column (GTK_EDITABLE (text), -1);
4629 gtk_stext_kill_char (GtkEditable *editable,
4634 text = GTK_STEXT (editable);
4636 if (editable->selection_start_pos != editable->selection_end_pos)
4637 gtk_editable_delete_selection (editable);
4642 if (text->point.index + 1 <= TEXT_LENGTH (text))
4643 gtk_editable_delete_text (editable, text->point.index, text->point.index + 1);
4647 if (text->point.index > 0)
4648 gtk_editable_delete_text (editable, text->point.index - 1, text->point.index);
4654 gtk_stext_delete_forward_character (GtkSText *text)
4656 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4660 gtk_stext_delete_backward_character (GtkSText *text)
4662 gtk_stext_kill_char (GTK_EDITABLE (text), -1);
4666 gtk_stext_kill_word (GtkEditable *editable,
4669 if (editable->selection_start_pos != editable->selection_end_pos)
4670 gtk_editable_delete_selection (editable);
4673 gint old_pos = editable->current_pos;
4676 gtk_stext_move_word (editable, 1);
4677 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4681 gtk_stext_move_word (editable, -1);
4682 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4688 gtk_stext_delete_forward_word (GtkSText *text)
4690 gtk_stext_kill_word (GTK_EDITABLE (text), 1);
4694 gtk_stext_delete_backward_word (GtkSText *text)
4696 gtk_stext_kill_word (GTK_EDITABLE (text), -1);
4700 gtk_stext_kill_line (GtkEditable *editable,
4703 gint old_pos = editable->current_pos;
4706 gtk_stext_move_to_column (editable, -1);
4707 gtk_editable_delete_text (editable, old_pos, editable->current_pos);
4711 gtk_stext_move_to_column (editable, 0);
4712 gtk_editable_delete_text (editable, editable->current_pos, old_pos);
4717 gtk_stext_delete_line (GtkSText *text)
4719 gtk_stext_move_to_column (GTK_EDITABLE (text), 0);
4720 if (GTK_STEXT_INDEX(text, GTK_EDITABLE (text)->current_pos) == LINE_DELIM)
4722 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4726 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4731 gtk_stext_delete_to_line_end (GtkSText *text)
4733 if (GTK_STEXT_INDEX(text, GTK_EDITABLE (text)->current_pos) == LINE_DELIM)
4735 gtk_stext_kill_char (GTK_EDITABLE (text), 1);
4739 gtk_stext_kill_line (GTK_EDITABLE (text), 1);
4744 gtk_stext_select_word (GtkSText *text, guint32 time)
4749 GtkEditable *editable;
4750 editable = GTK_EDITABLE (text);
4752 gtk_stext_move_backward_word (text);
4753 start_pos = text->cursor_mark.index;
4755 gtk_stext_move_forward_word (text);
4756 end_pos = text->cursor_mark.index;
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);
4764 gtk_stext_select_line (GtkSText *text, guint32 time)
4769 GtkEditable *editable;
4770 editable = GTK_EDITABLE (text);
4772 gtk_stext_move_beginning_of_line (text);
4773 start_pos = text->cursor_mark.index;
4775 gtk_stext_move_end_of_line (text);
4776 gtk_stext_move_forward_character (text);
4777 end_pos = text->cursor_mark.index;
4779 editable->has_selection = TRUE;
4780 gtk_stext_set_selection (editable, start_pos, end_pos);
4781 gtk_editable_claim_selection (editable, start_pos != end_pos, time);
4784 /**********************************************************************/
4786 /**********************************************************************/
4789 adjust_adj (GtkSText* text, GtkAdjustment* adj)
4793 gdk_window_get_size (text->text_area, NULL, &height);
4795 adj->step_increment = MIN (adj->upper, (float) SCROLL_PIXELS);
4796 adj->page_increment = MIN (adj->upper, height - (float) KEY_SCROLL_PIXELS);
4797 adj->page_size = MIN (adj->upper, height);
4798 adj->value = MIN (adj->value, adj->upper - adj->page_size);
4799 adj->value = MAX (adj->value, 0.0);
4801 gtk_signal_emit_by_name (GTK_OBJECT (adj), "changed");
4805 set_vertical_scroll_iterator (GtkSText* text, LineParams* lp, void* data)
4807 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4809 if ((text->first_line_start_index >= lp->start.index) &&
4810 (text->first_line_start_index <= lp->end.index))
4812 svdata->mark = lp->start;
4814 if (text->first_line_start_index == lp->start.index)
4816 text->first_onscreen_ver_pixel = svdata->pixel_height + text->first_cut_pixels;
4820 text->first_onscreen_ver_pixel = svdata->pixel_height;
4821 text->first_cut_pixels = 0;
4824 text->vadj->value = (float) text->first_onscreen_ver_pixel;
4827 svdata->pixel_height += LINE_HEIGHT (*lp);
4833 set_vertical_scroll_find_iterator (GtkSText* text, LineParams* lp, void* data)
4835 SetVerticalScrollData *svdata = (SetVerticalScrollData *) data;
4838 if (svdata->pixel_height <= (gint) text->vadj->value &&
4839 svdata->pixel_height + LINE_HEIGHT(*lp) > (gint) text->vadj->value)
4841 svdata->mark = lp->start;
4843 text->first_cut_pixels = (gint)text->vadj->value - svdata->pixel_height;
4844 text->first_onscreen_ver_pixel = svdata->pixel_height;
4845 text->first_line_start_index = lp->start.index;
4851 svdata->pixel_height += LINE_HEIGHT (*lp);
4859 static GtkSPropertyMark
4860 set_vertical_scroll (GtkSText* text)
4862 GtkSPropertyMark mark = find_mark (text, 0);
4863 SetVerticalScrollData data;
4867 data.pixel_height = 0;
4868 line_params_iterate (text, &mark, NULL, FALSE, &data, set_vertical_scroll_iterator);
4870 text->vadj->upper = (float) data.pixel_height;
4871 orig_value = (gint) text->vadj->value;
4873 gdk_window_get_size (text->text_area, NULL, &height);
4875 text->vadj->step_increment = MIN (text->vadj->upper, (float) SCROLL_PIXELS);
4876 text->vadj->page_increment = MIN (text->vadj->upper, height - (float) KEY_SCROLL_PIXELS);
4877 text->vadj->page_size = MIN (text->vadj->upper, height);
4878 text->vadj->value = MIN (text->vadj->value, text->vadj->upper - text->vadj->page_size);
4879 text->vadj->value = MAX (text->vadj->value, 0.0);
4881 text->last_ver_value = (gint)text->vadj->value;
4883 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "changed");
4885 if (text->vadj->value != orig_value)
4887 /* We got clipped, and don't really know which line to put first. */
4888 data.pixel_height = 0;
4889 data.last_didnt_wrap = TRUE;
4891 line_params_iterate (text, &mark, NULL,
4893 set_vertical_scroll_find_iterator);
4900 scroll_int (GtkSText* text, gint diff)
4904 text->vadj->value += diff;
4906 upper = text->vadj->upper - text->vadj->page_size;
4907 text->vadj->value = MIN (text->vadj->value, upper);
4908 text->vadj->value = MAX (text->vadj->value, 0.0);
4910 gtk_signal_emit_by_name (GTK_OBJECT (text->vadj), "value_changed");
4914 process_exposes (GtkSText *text)
4918 /* Make sure graphics expose events are processed before scrolling
4921 while ((event = gdk_event_get_graphics_expose (text->text_area)) != NULL)
4923 gtk_widget_event (GTK_WIDGET (text), event);
4924 if (event->expose.count == 0)
4926 gdk_event_free (event);
4929 gdk_event_free (event);
4933 static gint last_visible_line_height (GtkSText* text)
4935 GList *cache = text->line_start_cache;
4938 gdk_window_get_size (text->text_area, NULL, &height);
4940 for (; cache->next; cache = cache->next)
4941 if (pixel_height_of(text, cache->next) > height)
4945 return pixel_height_of(text, cache) - 1;
4950 static gint first_visible_line_height (GtkSText* text)
4952 if (text->first_cut_pixels)
4953 return pixel_height_of(text, text->line_start_cache) + 1;
4959 scroll_down (GtkSText* text, gint diff0)
4965 text->first_onscreen_ver_pixel += diff0;
4969 if (text->first_cut_pixels < LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1)
4971 text->first_cut_pixels += 1;
4975 text->first_cut_pixels = 0;
4977 text->line_start_cache = text->line_start_cache->next;
4978 g_assert (text->line_start_cache);
4980 text->first_line_start_index =
4981 CACHE_DATA(text->line_start_cache).start.index;
4983 if (!text->line_start_cache->next)
4984 fetch_lines_forward (text, 1);
4990 gdk_window_get_size (text->text_area, &width, &height);
4991 if (height > real_diff)
4992 gdk_draw_pixmap (text->text_area,
5000 height - real_diff);
5003 rect.y = MAX (0, height - real_diff);
5005 rect.height = MIN (height, real_diff);
5007 expose_text (text, &rect, FALSE);
5008 gtk_stext_draw_focus ( (GtkWidget *) text);
5010 if (text->current_line)
5014 text->cursor_pos_y -= real_diff;
5015 cursor_min = drawn_cursor_min(text);
5018 find_mouse_cursor (text, text->cursor_pos_x,
5019 first_visible_line_height (text));
5022 if (height > real_diff)
5023 process_exposes (text);
5027 scroll_up (GtkSText* text, gint diff0)
5033 text->first_onscreen_ver_pixel += diff0;
5037 g_assert (text->line_start_cache);
5039 if (text->first_cut_pixels > 0)
5041 text->first_cut_pixels -= 1;
5045 if (!text->line_start_cache->prev)
5046 fetch_lines_backward (text);
5048 text->line_start_cache = text->line_start_cache->prev;
5050 text->first_line_start_index =
5051 CACHE_DATA(text->line_start_cache).start.index;
5053 text->first_cut_pixels = LINE_HEIGHT(CACHE_DATA(text->line_start_cache)) - 1;
5059 gdk_window_get_size (text->text_area, &width, &height);
5060 if (height > real_diff)
5061 gdk_draw_pixmap (text->text_area,
5069 height - real_diff);
5074 rect.height = MIN (height, real_diff);
5076 expose_text (text, &rect, FALSE);
5077 gtk_stext_draw_focus ( (GtkWidget *) text);
5079 if (text->current_line)
5084 text->cursor_pos_y += real_diff;
5085 cursor_max = drawn_cursor_max(text);
5086 gdk_window_get_size (text->text_area, NULL, &height);
5088 if (cursor_max >= height)
5089 find_mouse_cursor (text, text->cursor_pos_x,
5090 last_visible_line_height (text));
5093 if (height > real_diff)
5094 process_exposes (text);
5097 /**********************************************************************/
5099 /**********************************************************************/
5101 /* Assumes mark starts a line. Calculates the height, width, and
5102 * displayable character count of a single DISPLAYABLE line. That
5103 * means that in line-wrap mode, this does may not compute the
5104 * properties of an entire line. */
5106 find_line_params (GtkSText* text,
5107 const GtkSPropertyMark* mark,
5108 const PrevTabCont *tab_cont,
5109 PrevTabCont *next_cont)
5112 TabStopMark tab_mark = tab_cont->tab_start;
5113 guint max_display_pixels;
5118 gdk_window_get_size (text->text_area, (gint*) &max_display_pixels, NULL);
5120 if (text->wrap_rmargin) {
5121 /* SYLPHEED - since sylpheed is a mail program, assume we work with
5122 * fixed fonts. only assume we're using one font! */
5123 font = MARK_CURRENT_FONT(text, mark);
5125 /* SYLPHEED - ok for multi byte charsets to check for ASCII char?
5127 if (text->use_wchar)
5128 ch_width = gdk_char_width_wc(font, 'W');
5130 ch_width = gdk_char_width(font, 'W');
5132 /* SYLPHEED - max_display_chars has the rmargin in pixels
5134 max_display_pixels = text->wrap_rmargin * ch_width;
5137 /* SYLPHEED - we don't draw ugly word wrapping thing
5138 * if our wrap margin is set */
5139 if (!text->wrap_rmargin &&
5140 ((GTK_EDITABLE (text)->editable || !text->word_wrap)))
5141 max_display_pixels -= LINE_WRAP_ROOM;
5144 lp.tab_cont = *tab_cont;
5147 lp.pixel_width = tab_cont->pixel_offset;
5148 lp.displayable_chars = 0;
5150 lp.font_descent = 0;
5152 init_tab_cont (text, next_cont);
5154 while (!LAST_INDEX(text, lp.end))
5156 g_assert (lp.end.property);
5158 ch = GTK_STEXT_INDEX (text, lp.end.index);
5159 font = MARK_CURRENT_FONT (text, &lp.end);
5161 if (ch == LINE_DELIM)
5163 /* Newline doesn't count in computation of line height, even
5164 * if its in a bigger font than the rest of the line. Unless,
5165 * of course, there are no other characters. */
5167 if (!lp.font_ascent && !lp.font_descent)
5169 lp.font_ascent = font->ascent;
5170 lp.font_descent = font->descent;
5173 lp.tab_cont_next = *next_cont;
5178 ch_width = find_char_width (text, &lp.end, &tab_mark);
5180 if ((ch_width + lp.pixel_width > max_display_pixels) &&
5181 (lp.end.index > lp.start.index))
5185 if (text->line_wrap)
5187 next_cont->tab_start = tab_mark;
5188 next_cont->pixel_offset = 0;
5192 /* Here's the tough case, a tab is wrapping. */
5193 gint pixels_avail = max_display_pixels - lp.pixel_width;
5194 gint space_width = MARK_CURRENT_TEXT_FONT(text, &lp.end)->char_widths[' '];
5195 gint spaces_avail = pixels_avail / space_width;
5197 if (spaces_avail == 0)
5199 decrement_mark (&lp.end);
5203 advance_tab_mark (text, &next_cont->tab_start, '\t');
5204 next_cont->pixel_offset = space_width * (tab_mark.to_next_tab -
5206 lp.displayable_chars += 1;
5211 if (text->word_wrap)
5213 GtkSPropertyMark saved_mark = lp.end;
5214 guint saved_characters = lp.displayable_chars;
5216 lp.displayable_chars += 1;
5218 if (text->use_wchar)
5220 while (!gdk_iswspace (GTK_STEXT_INDEX (text, lp.end.index)) &&
5221 (lp.end.index > lp.start.index))
5223 decrement_mark (&lp.end);
5224 lp.displayable_chars -= 1;
5229 while (!isspace(GTK_STEXT_INDEX (text, lp.end.index)) &&
5230 (lp.end.index > lp.start.index))
5232 decrement_mark (&lp.end);
5233 lp.displayable_chars -= 1;
5237 /* If whole line is one word, revert to char wrapping */
5238 if (lp.end.index == lp.start.index)
5240 /* SYLPHEED: don't wrap URLs */
5241 if (gtkut_stext_is_uri_string(text, lp.end.index,
5242 gtk_stext_get_length(text)))
5244 lp.end = saved_mark;
5245 lp.displayable_chars = saved_characters + 1;
5250 lp.end = saved_mark;
5251 lp.displayable_chars = saved_characters;
5252 decrement_mark (&lp.end);
5257 /* Don't include this character, it will wrap. */
5258 decrement_mark (&lp.end);
5262 lp.tab_cont_next = *next_cont;
5269 lp.displayable_chars += 1;
5273 lp.font_ascent = MAX (font->ascent, lp.font_ascent);
5274 lp.font_descent = MAX (font->descent, lp.font_descent);
5275 lp.pixel_width += ch_width;
5277 advance_mark(&lp.end);
5278 advance_tab_mark (text, &tab_mark, ch);
5281 if (LAST_INDEX(text, lp.start))
5283 /* Special case, empty last line. */
5284 font = MARK_CURRENT_FONT (text, &lp.end);
5286 lp.font_ascent = font->ascent;
5287 lp.font_descent = font->descent;
5290 lp.tab_cont_next = *next_cont;
5296 expand_scratch_buffer (GtkSText* text, guint len)
5298 if (len >= text->scratch_buffer_len)
5302 while (i <= len && i < MIN_GAP_SIZE) i <<= 1;
5304 if (text->use_wchar)
5306 if (!text->scratch_buffer.wc)
5307 text->scratch_buffer.wc = g_new (GdkWChar, i);
5309 text->scratch_buffer.wc = g_realloc (text->scratch_buffer.wc,
5310 i * sizeof (GdkWChar));
5314 if (!text->scratch_buffer.ch)
5315 text->scratch_buffer.ch = g_new (guchar, i);
5317 text->scratch_buffer.ch = g_realloc (text->scratch_buffer.ch, i);
5320 text->scratch_buffer_len = i;
5324 /* Side effect: modifies text->gc
5328 draw_bg_rect (GtkSText* text, GtkSPropertyMark *mark,
5329 gint x, gint y, gint width, gint height,
5330 gboolean already_cleared)
5332 GtkEditable *editable = GTK_EDITABLE(text);
5334 if ((mark->index >= MIN(editable->selection_start_pos, editable->selection_end_pos) &&
5335 mark->index < MAX(editable->selection_start_pos, editable->selection_end_pos)))
5337 gtk_paint_flat_box(GTK_WIDGET(text)->style, text->text_area,
5338 editable->has_selection ?
5339 GTK_STATE_SELECTED : GTK_STATE_ACTIVE,
5341 NULL, GTK_WIDGET(text), "text",
5342 x, y, width, height);
5344 else if (!gdk_color_equal(MARK_CURRENT_BACK (text, mark),
5345 >K_WIDGET(text)->style->base[GTK_WIDGET_STATE (text)]))
5347 gdk_gc_set_foreground (text->gc, MARK_CURRENT_BACK (text, mark));
5349 gdk_draw_rectangle (text->text_area,
5351 TRUE, x, y, width, height);
5353 else if (GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL])
5360 rect.height = height;
5362 clear_area (text, &rect);
5364 else if (!already_cleared)
5365 gdk_window_clear_area (text->text_area, x, y, width, height);
5369 draw_line (GtkSText* text,
5370 gint pixel_start_height,
5373 GdkGCValues gc_values;
5376 guint running_offset = lp->tab_cont.pixel_offset;
5377 union { GdkWChar *wc; guchar *ch; } buffer;
5380 GtkEditable *editable = GTK_EDITABLE(text);
5382 guint selection_start_pos = MIN (editable->selection_start_pos, editable->selection_end_pos);
5383 guint selection_end_pos = MAX (editable->selection_start_pos, editable->selection_end_pos);
5385 GtkSPropertyMark mark = lp->start;
5386 TabStopMark tab_mark = lp->tab_cont.tab_start;
5387 gint pixel_height = pixel_start_height + lp->font_ascent;
5388 guint chars = lp->displayable_chars;
5390 /* First provide a contiguous segment of memory. This makes reading
5391 * the code below *much* easier, and only incurs the cost of copying
5392 * when the line being displayed spans the gap. */
5393 if (mark.index <= text->gap_position &&
5394 mark.index + chars > text->gap_position)
5396 expand_scratch_buffer (text, chars);
5398 if (text->use_wchar)
5400 for (i = 0; i < chars; i += 1)
5401 text->scratch_buffer.wc[i] = GTK_STEXT_INDEX(text, mark.index + i);
5402 buffer.wc = text->scratch_buffer.wc;
5406 for (i = 0; i < chars; i += 1)
5407 text->scratch_buffer.ch[i] = GTK_STEXT_INDEX(text, mark.index + i);
5408 buffer.ch = text->scratch_buffer.ch;
5413 if (text->use_wchar)
5415 if (mark.index >= text->gap_position)
5416 buffer.wc = text->text.wc + mark.index + text->gap_size;
5418 buffer.wc = text->text.wc + mark.index;
5422 if (mark.index >= text->gap_position)
5423 buffer.ch = text->text.ch + mark.index + text->gap_size;
5425 buffer.ch = text->text.ch + mark.index;
5430 if (running_offset > 0)
5432 draw_bg_rect (text, &mark, 0, pixel_start_height, running_offset,
5433 LINE_HEIGHT (*lp), TRUE);
5439 if ((text->use_wchar && buffer.wc[0] != '\t') ||
5440 (!text->use_wchar && buffer.ch[0] != '\t'))
5442 union { GdkWChar *wc; guchar *ch; } next_tab;
5447 if (text->use_wchar)
5448 for (i=0; i<chars; i++)
5450 if (buffer.wc[i] == '\t')
5452 next_tab.wc = buffer.wc + i;
5457 next_tab.ch = memchr (buffer.ch, '\t', chars);
5459 len = MIN (MARK_CURRENT_PROPERTY (&mark)->length - mark.offset, chars);
5461 if (text->use_wchar)
5464 len = MIN (len, next_tab.wc - buffer.wc);
5469 len = MIN (len, next_tab.ch - buffer.ch);
5472 if (mark.index < selection_start_pos)
5473 len = MIN (len, selection_start_pos - mark.index);
5474 else if (mark.index < selection_end_pos)
5475 len = MIN (len, selection_end_pos - mark.index);
5477 font = MARK_CURRENT_FONT (text, &mark);
5478 if (font->type == GDK_FONT_FONT)
5480 gdk_gc_set_font (text->gc, font);
5481 gdk_gc_get_values (text->gc, &gc_values);
5482 if (text->use_wchar)
5483 pixel_width = gdk_text_width_wc (gc_values.font,
5486 pixel_width = gdk_text_width (gc_values.font,
5491 if (text->use_wchar)
5492 pixel_width = gdk_text_width_wc (font, buffer.wc, len);
5494 pixel_width = gdk_text_width (font, buffer.ch, len);
5497 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5498 pixel_width, LINE_HEIGHT (*lp), TRUE);
5500 if ((mark.index >= selection_start_pos) &&
5501 (mark.index < selection_end_pos))
5503 if (editable->has_selection)
5504 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_SELECTED];
5506 fg_gc = GTK_WIDGET(text)->style->fg_gc[GTK_STATE_ACTIVE];
5510 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &mark));
5514 if (text->use_wchar)
5515 gdk_draw_text_wc (text->text_area, MARK_CURRENT_FONT (text, &mark),
5522 gdk_draw_text (text->text_area, MARK_CURRENT_FONT (text, &mark),
5529 running_offset += pixel_width;
5531 advance_tab_mark_n (text, &tab_mark, len);
5535 gint pixels_remaining;
5541 gdk_window_get_size (text->text_area, &pixels_remaining, NULL);
5542 if (GTK_EDITABLE (text)->editable || !text->word_wrap)
5543 pixels_remaining -= (LINE_WRAP_ROOM + running_offset);
5545 pixels_remaining -= running_offset;
5547 space_width = MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5549 spaces_avail = pixels_remaining / space_width;
5550 spaces_avail = MIN (spaces_avail, tab_mark.to_next_tab);
5552 draw_bg_rect (text, &mark, running_offset, pixel_start_height,
5553 spaces_avail * space_width, LINE_HEIGHT (*lp), TRUE);
5555 running_offset += tab_mark.to_next_tab *
5556 MARK_CURRENT_TEXT_FONT(text, &mark)->char_widths[' '];
5558 advance_tab_mark (text, &tab_mark, '\t');
5561 advance_mark_n (&mark, len);
5562 if (text->use_wchar)
5571 draw_line_wrap (GtkSText* text, guint height /* baseline height */)
5578 if (!GTK_EDITABLE (text)->editable && text->word_wrap)
5581 /* SYLPHEED - don't draw ugly word wrapping thing if
5582 * our wrap margin is set */
5583 if (text->wrap_rmargin > 0) {
5587 if (text->line_wrap)
5589 bitmap = text->line_wrap_bitmap;
5590 bitmap_width = line_wrap_width;
5591 bitmap_height = line_wrap_height;
5595 bitmap = text->line_arrow_bitmap;
5596 bitmap_width = line_arrow_width;
5597 bitmap_height = line_arrow_height;
5600 gdk_window_get_size (text->text_area, &width, NULL);
5601 width -= LINE_WRAP_ROOM;
5603 gdk_gc_set_stipple (text->gc,
5606 gdk_gc_set_fill (text->gc, GDK_STIPPLED);
5608 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5610 gdk_gc_set_ts_origin (text->gc,
5612 height - bitmap_height - 1);
5614 gdk_draw_rectangle (text->text_area,
5618 height - bitmap_height - 1 /* one pixel above the baseline. */,
5622 gdk_gc_set_ts_origin (text->gc, 0, 0);
5624 gdk_gc_set_fill (text->gc, GDK_SOLID);
5628 undraw_cursor (GtkSText* text, gint absolute)
5630 GtkEditable *editable = (GtkEditable *)text;
5632 TDEBUG (("in undraw_cursor\n"));
5635 text->cursor_drawn_level = 0;
5637 if ((text->cursor_drawn_level ++ == 0) &&
5638 (editable->selection_start_pos == editable->selection_end_pos) &&
5639 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5645 g_assert(text->cursor_mark.property);
5647 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5650 * changed the cursor to a real block (TM)
5652 if (text->cursor_type == GTK_STEXT_CURSOR_BLOCK)
5654 if (text->cursor_char)
5656 if (text->use_wchar)
5657 pixel_width = gdk_char_width_wc (font, text->cursor_char);
5659 pixel_width = gdk_char_width (font, (guchar)text->cursor_char);
5663 pixel_width = gdk_char_width (font, 'W');
5666 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5668 draw_bg_rect (text, &text->cursor_mark,
5670 text->cursor_pos_y - (pixel_height + 1),
5677 draw_bg_rect (text, &text->cursor_mark,
5679 text->cursor_pos_y - text->cursor_char_offset - font->ascent,
5680 2, font->descent + font->ascent + 1, FALSE);
5683 if (text->cursor_char)
5685 if (font->type == GDK_FONT_FONT)
5686 gdk_gc_set_font (text->gc, font);
5688 gdk_gc_set_foreground (text->gc, MARK_CURRENT_FORE (text, &text->cursor_mark));
5690 if (text->use_wchar)
5691 gdk_draw_text_wc (text->text_area, font,
5694 text->cursor_pos_y - text->cursor_char_offset,
5699 guchar ch = text->cursor_char;
5700 gdk_draw_text (text->text_area, font,
5703 text->cursor_pos_y - text->cursor_char_offset,
5712 drawn_cursor_min (GtkSText* text)
5716 g_assert(text->cursor_mark.property);
5718 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5720 return text->cursor_pos_y - text->cursor_char_offset - font->ascent;
5724 drawn_cursor_max (GtkSText* text)
5728 g_assert(text->cursor_mark.property);
5730 font = MARK_CURRENT_FONT(text, &text->cursor_mark);
5732 return text->cursor_pos_y - text->cursor_char_offset;
5736 draw_cursor (GtkSText* text, gint absolute)
5738 GtkEditable *editable = (GtkEditable *)text;
5740 TDEBUG (("in draw_cursor\n"));
5743 text->cursor_drawn_level = 1;
5745 if ((--text->cursor_drawn_level == 0) &&
5746 editable->editable &&
5747 (editable->selection_start_pos == editable->selection_end_pos) &&
5748 GTK_WIDGET_DRAWABLE (text) && text->line_start_cache)
5754 g_assert (text->cursor_mark.property);
5756 font = MARK_CURRENT_FONT (text, &text->cursor_mark);
5759 * change the cursor to a real block (TM)
5761 if (text->cursor_type == GTK_STEXT_CURSOR_BLOCK)
5763 if (text->cursor_char)
5765 if (text->use_wchar)
5766 pixel_width = gdk_char_width_wc (font, text->cursor_char);
5768 pixel_width = gdk_char_width (font, (guchar)text->cursor_char);
5772 pixel_width = gdk_char_width (font, 'W');
5775 pixel_height = LINE_HEIGHT(CACHE_DATA(text->current_line));
5777 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5778 gdk_gc_set_function (text->gc, GDK_INVERT);
5779 gdk_draw_rectangle (text->text_area, text->gc, TRUE,
5781 text->cursor_pos_y - pixel_height,
5784 gdk_gc_set_function (text->gc, GDK_COPY);
5788 gdk_gc_set_line_attributes(text->gc, 2, GDK_LINE_SOLID, GDK_CAP_NOT_LAST, GDK_JOIN_MITER);
5789 gdk_gc_set_foreground (text->gc, >K_WIDGET (text)->style->text[GTK_STATE_NORMAL]);
5791 gdk_draw_line (text->text_area, text->gc, text->cursor_pos_x + 1,
5792 text->cursor_pos_y + font->descent - text->cursor_char_offset,
5793 text->cursor_pos_x + 1,
5794 text->cursor_pos_y - text->cursor_char_offset - font->ascent);
5800 create_bg_gc (GtkSText *text)
5804 values.tile = GTK_WIDGET (text)->style->bg_pixmap[GTK_STATE_NORMAL];
5805 values.fill = GDK_TILED;
5807 return gdk_gc_new_with_values (text->text_area, &values,
5808 GDK_GC_FILL | GDK_GC_TILE);
5812 clear_area (GtkSText *text, GdkRectangle *area)
5814 GtkWidget *widget = GTK_WIDGET (text);
5820 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
5822 gdk_gc_set_ts_origin (text->bg_gc,
5823 (- (gint)text->first_onscreen_hor_pixel) % width,
5824 (- (gint)text->first_onscreen_ver_pixel) % height);
5826 gdk_draw_rectangle (text->text_area, text->bg_gc, TRUE,
5827 area->x, area->y, area->width, area->height);
5830 gdk_window_clear_area (text->text_area, area->x, area->y, area->width, area->height);
5834 expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor)
5836 GList *cache = text->line_start_cache;
5837 gint pixels = - text->first_cut_pixels;
5838 gint min_y = MAX (0, area->y);
5839 gint max_y = MAX (0, area->y + area->height);
5842 gdk_window_get_size (text->text_area, NULL, &height);
5843 max_y = MIN (max_y, height);
5845 TDEBUG (("in expose x=%d y=%d w=%d h=%d\n", area->x, area->y, area->width, area->height));
5847 clear_area (text, area);
5849 for (; pixels < height; cache = cache->next)
5851 if (pixels < max_y && (pixels + (gint)LINE_HEIGHT(CACHE_DATA(cache))) >= min_y)
5853 draw_line (text, pixels, &CACHE_DATA(cache));
5855 if (CACHE_DATA(cache).wraps)
5856 draw_line_wrap (text, pixels + CACHE_DATA(cache).font_ascent);
5859 if (cursor && GTK_WIDGET_HAS_FOCUS (text))
5861 if (CACHE_DATA(cache).start.index <= text->cursor_mark.index &&
5862 CACHE_DATA(cache).end.index >= text->cursor_mark.index)
5864 /* We undraw and draw the cursor here to get the drawn
5865 * level right ... FIXME - maybe the second parameter
5866 * of draw_cursor should work differently
5868 undraw_cursor (text, FALSE);
5869 draw_cursor (text, FALSE);
5873 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5877 fetch_lines_forward (text, 1);
5886 gtk_stext_update_text (GtkEditable *editable,
5890 GtkSText *text = GTK_STEXT (editable);
5892 GList *cache = text->line_start_cache;
5893 gint pixels = - text->first_cut_pixels;
5899 end_pos = TEXT_LENGTH (text);
5901 if (end_pos < start_pos)
5904 gdk_window_get_size (text->text_area, &width, &height);
5910 TDEBUG (("in expose span start=%d stop=%d\n", start_pos, end_pos));
5912 for (; pixels < height; cache = cache->next)
5914 if (CACHE_DATA(cache).start.index < end_pos)
5916 if (CACHE_DATA(cache).end.index >= start_pos)
5919 area.y = MAX(0,pixels);
5920 area.height = pixels + LINE_HEIGHT(CACHE_DATA(cache)) - area.y;
5926 pixels += LINE_HEIGHT(CACHE_DATA(cache));
5930 fetch_lines_forward (text, 1);
5938 expose_text (text, &area, TRUE);
5942 recompute_geometry (GtkSText* text)
5944 GtkSPropertyMark mark, start_mark;
5951 mark = start_mark = set_vertical_scroll (text);
5953 /* We need a real start of a line when calling fetch_lines().
5954 * not the start of a wrapped line.
5956 while (mark.index > 0 &&
5957 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
5958 decrement_mark (&mark);
5960 gdk_window_get_size (text->text_area, &width, &height);
5962 /* Fetch an entire line, to make sure that we get all the text
5963 * we backed over above, in addition to enough text to fill up
5964 * the space vertically
5967 new_lines = fetch_lines (text,
5973 mark = CACHE_DATA (g_list_last (new_lines)).end;
5974 if (!LAST_INDEX (text, mark))
5976 advance_mark (&mark);
5978 new_lines = g_list_concat (new_lines,
5983 height + text->first_cut_pixels));
5986 /* Now work forward to the actual first onscreen line */
5988 while (CACHE_DATA (new_lines).start.index < start_mark.index)
5989 new_lines = new_lines->next;
5991 text->line_start_cache = new_lines;
5993 find_cursor (text, TRUE);
5996 /**********************************************************************/
5998 /**********************************************************************/
6001 gtk_stext_set_selection (GtkEditable *editable,
6005 GtkSText *text = GTK_STEXT (editable);
6007 guint start1, end1, start2, end2;
6010 end = TEXT_LENGTH (text);
6012 start1 = MIN(start,end);
6013 end1 = MAX(start,end);
6014 start2 = MIN(editable->selection_start_pos, editable->selection_end_pos);
6015 end2 = MAX(editable->selection_start_pos, editable->selection_end_pos);
6017 if (start2 < start1)
6021 tmp = start1; start1 = start2; start2 = tmp;
6022 tmp = end1; end1 = end2; end2 = tmp;
6025 undraw_cursor (text, FALSE);
6026 editable->selection_start_pos = start;
6027 editable->selection_end_pos = end;
6028 draw_cursor (text, FALSE);
6030 /* Expose only what changed */
6032 if (start1 < start2)
6033 gtk_stext_update_text (editable, start1, MIN(end1, start2));
6036 gtk_stext_update_text (editable, MAX(end1, start2), end2);
6037 else if (end2 < end1)
6038 gtk_stext_update_text (editable, end2, end1);
6047 stext_blink_timer_proc (GtkSText *text)
6049 if (text->cursor_state_on)
6051 text->cursor_state_on = FALSE;
6052 undraw_cursor (text, TRUE);
6053 /* kill this timer... */
6054 gtk_timeout_remove (text->cursor_timer_id);
6055 text->cursor_timer_id = gtk_timeout_add (text->cursor_off_ms,
6056 (GtkFunction) stext_blink_timer_proc,
6061 text->cursor_state_on = TRUE;
6062 draw_cursor (text, TRUE);
6063 /* kill this timer... */
6064 gtk_timeout_remove (text->cursor_timer_id);
6065 text->cursor_timer_id = gtk_timeout_add (text->cursor_on_ms,
6066 (GtkFunction) stext_blink_timer_proc,
6074 stext_idle_timer_proc (GtkSText *text)
6076 /* make sure the cursor timer is off */
6077 if (text->cursor_timer_id)
6079 gtk_timeout_remove (text->cursor_timer_id);
6080 text->cursor_timer_id = 0;
6083 /* assuming it's always on when calling this function ... */
6084 text->cursor_state_on = TRUE;
6085 text->cursor_timer_id = gtk_timeout_add (text->cursor_on_ms,
6086 (GtkFunction) stext_blink_timer_proc,
6088 /* make sure we kill the timer (could perhaps make this function return
6089 FALSE (not documented in the current docs). should check the source. */
6090 gtk_idle_remove (text->cursor_idle_time_timer_id);
6091 text->cursor_idle_time_timer_id = 0;
6097 gtk_stext_enable_blink (GtkSText *text)
6099 if (text->cursor_timer_on)
6101 gtk_stext_disable_blink (text);
6102 text->cursor_idle_time_timer_id = gtk_idle_add ((GtkFunction) stext_idle_timer_proc,
6108 gtk_stext_disable_blink (GtkSText *text)
6110 if (text->cursor_timer_on)
6112 if (text->cursor_idle_time_timer_id)
6114 gtk_idle_remove (text->cursor_idle_time_timer_id);
6115 text->cursor_idle_time_timer_id = 0;
6117 if (text->cursor_timer_id)
6119 gtk_timeout_remove (text->cursor_timer_id);
6120 text->cursor_timer_id = 0;
6122 draw_cursor(text, TRUE);
6127 gtk_stext_set_blink (GtkSText *text, gboolean blinking_on)
6129 if (text->cursor_timer_on != blinking_on)
6131 if (text->cursor_timer_on)
6133 /* text widget already created? */
6134 if (text->cursor_visible)
6136 gtk_stext_disable_blink (text);
6138 text->cursor_timer_on = FALSE;
6142 if (text->cursor_visible)
6144 gtk_stext_enable_blink (text);
6146 text->cursor_timer_on = TRUE;
6152 void gtk_stext_set_wrap_rmargin (GtkSText *text, gint rmargin)
6154 /* not particularly impressive, but it does the job. */
6155 /* TODO: currently only allowed to set this after a
6156 * gtk_stext_new() */
6157 text->wrap_rmargin = rmargin >= 0 ? rmargin : 0;
6160 /**********************************************************************/
6162 /**********************************************************************/
6164 #ifdef DEBUG_GTK_STEXT
6166 gtk_stext_show_cache_line (GtkSText *text, GList *cache,
6167 const char* what, const char* func, gint line)
6169 LineParams *lp = &CACHE_DATA(cache);
6172 if (cache == text->line_start_cache)
6173 g_message ("Line Start Cache: ");
6175 if (cache == text->current_line)
6176 g_message("Current Line: ");
6178 g_message ("%s:%d: cache line %s s=%d,e=%d,lh=%d (",
6186 for (i = lp->start.index; i < (lp->end.index + lp->wraps); i += 1)
6187 g_message ("%c", GTK_STEXT_INDEX (text, i));
6193 gtk_stext_show_cache (GtkSText *text, const char* func, gint line)
6195 GList *l = text->line_start_cache;
6201 /* back up to the absolute beginning of the line cache */
6205 g_message ("*** line cache ***\n");
6206 for (; l; l = l->next)
6207 gtk_stext_show_cache_line (text, l, "all", func, line);
6211 gtk_stext_assert_mark (GtkSText *text,
6212 GtkSPropertyMark *mark,
6213 GtkSPropertyMark *before,
6214 GtkSPropertyMark *after,
6219 GtkSPropertyMark correct_mark = find_mark (text, mark->index);
6221 if (mark->offset != correct_mark.offset ||
6222 mark->property != correct_mark.property)
6223 g_warning ("incorrect %s text property marker in %s:%d, index %d -- bad!", where, msg, line, mark->index);
6227 gtk_stext_assert (GtkSText *text,
6231 GList* cache = text->line_start_cache;
6232 GtkSPropertyMark* before_mark = NULL;
6233 GtkSPropertyMark* after_mark = NULL;
6235 gtk_stext_show_props (text, msg, line);
6237 for (; cache->prev; cache = cache->prev)
6240 g_message ("*** line markers ***\n");
6242 for (; cache; cache = cache->next)
6244 after_mark = &CACHE_DATA(cache).end;
6245 gtk_stext_assert_mark (text, &CACHE_DATA(cache).start, before_mark, after_mark, msg, "start", line);
6246 before_mark = &CACHE_DATA(cache).start;
6249 after_mark = &CACHE_DATA(cache->next).start;
6253 gtk_stext_assert_mark (text, &CACHE_DATA(cache).end, before_mark, after_mark, msg, "end", line);
6254 before_mark = &CACHE_DATA(cache).end;
6259 gtk_stext_show_adj (GtkSText *text,
6265 g_message ("*** adjustment ***\n");
6267 g_message ("%s:%d: %s adjustment l=%.1f u=%.1f v=%.1f si=%.1f pi=%.1f ps=%.1f\n",
6274 adj->step_increment,
6275 adj->page_increment,
6280 gtk_stext_show_props (GtkSText *text,
6284 GList* props = text->text_properties;
6287 g_message ("%s:%d: ", msg, line);
6289 for (; props; props = props->next)
6291 TextProperty *p = (TextProperty*)props->data;
6293 proplen += p->length;
6295 g_message ("[%d,%p,", p->length, p);
6296 if (p->flags & PROPERTY_FONT)
6297 g_message ("%p,", p->font);
6300 if (p->flags & PROPERTY_FOREGROUND)
6301 g_message ("%ld, ", p->fore_color.pixel);
6304 if (p->flags & PROPERTY_BACKGROUND)
6305 g_message ("%ld] ", p->back_color.pixel);
6312 if (proplen - 1 != TEXT_LENGTH(text))
6313 g_warning ("incorrect property list length in %s:%d -- bad!", msg, line);