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. Interesting
29 * parts are marked using comment block following this one.
30 * This modification is based on the GtkSText of GTK 1.2.8
43 #include <gdk/gdkkeysyms.h>
44 #include <gdk/gdki18n.h>
45 #include <gtk/gtkmain.h>
46 #include <gtk/gtkselection.h>
47 #include <gtk/gtksignal.h>
51 * compile time settings
53 #define INITIAL_BUFFER_SIZE 1024
54 #define INITIAL_LINE_CACHE_SIZE 256
55 #define MIN_GAP_SIZE 256
58 * intending to introduce a paragraph delimiter '\r' because
59 * '\r' are being stripped by sylpheed
61 #define LINE_DELIM '\n'
62 #define MIN_TEXT_WIDTH_LINES 20
63 #define MIN_TEXT_HEIGHT_LINES 10
64 #define TEXT_BORDER_ROOM 1
65 #define LINE_WRAP_ROOM 8 /* The bitmaps are 6 wide. */
66 #define DEFAULT_TAB_STOP_WIDTH 4
67 #define SCROLL_PIXELS 5
68 #define KEY_SCROLL_PIXELS 10
69 #define SCROLL_TIME 100
70 #define FREEZE_LENGTH 1024
73 /* Freeze text when inserting or deleting more than this many characters */
76 * could also mark paragraphs using marks, but don't know anything yet
77 * about consistency when characters are removed
79 #define SET_PROPERTY_MARK(m, p, o) do { \
80 (m)->property = (p); \
83 #define MARK_CURRENT_PROPERTY(mark) ((TextProperty*)(mark)->property->data)
84 #define MARK_NEXT_PROPERTY(mark) ((TextProperty*)(mark)->property->next->data)
85 #define MARK_PREV_PROPERTY(mark) ((TextProperty*)((mark)->property->prev ? \
86 (mark)->property->prev->data \
88 #define MARK_PREV_LIST_PTR(mark) ((mark)->property->prev)
89 #define MARK_LIST_PTR(mark) ((mark)->property)
90 #define MARK_NEXT_LIST_PTR(mark) ((mark)->property->next)
91 #define MARK_OFFSET(mark) ((mark)->offset)
92 #define MARK_PROPERTY_LENGTH(mark) (MARK_CURRENT_PROPERTY(mark)->length)
95 #define MARK_CURRENT_FONT(text, mark) \
96 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
97 MARK_CURRENT_PROPERTY(mark)->font->gdk_font : \
98 GTK_WIDGET (text)->style->font)
99 #define MARK_CURRENT_FORE(text, mark) \
100 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FOREGROUND) ? \
101 &MARK_CURRENT_PROPERTY(mark)->fore_color : \
102 &((GtkWidget *)text)->style->text[((GtkWidget *)text)->state])
103 #define MARK_CURRENT_BACK(text, mark) \
104 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_BACKGROUND) ? \
105 &MARK_CURRENT_PROPERTY(mark)->back_color : \
106 &((GtkWidget *)text)->style->base[((GtkWidget *)text)->state])
107 #define MARK_CURRENT_TEXT_FONT(text, mark) \
108 ((MARK_CURRENT_PROPERTY(mark)->flags & PROPERTY_FONT) ? \
109 MARK_CURRENT_PROPERTY(mark)->font : \
112 #define TEXT_LENGTH(t) ((t)->text_end - (t)->gap_size)
113 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
114 #define LINE_HEIGHT(l) ((l).font_ascent + (l).font_descent)
115 #define LINE_CONTAINS(l, i) ((l).start.index <= (i) && (l).end.index >= (i))
116 #define LINE_STARTS_AT(l, i) ((l).start.index == (i))
117 #define LINE_START_PIXEL(l) ((l).tab_cont.pixel_offset)
118 #define LAST_INDEX(t, m) ((m).index == TEXT_LENGTH(t))
119 #define CACHE_DATA(c) (*(LineParams*)(c)->data)
129 typedef struct _TextProperty TextProperty;
130 typedef struct _TabStopMark TabStopMark;
131 typedef struct _PrevTabCont PrevTabCont;
132 typedef struct _FetchLinesData FetchLinesData;
133 typedef struct _LineParams LineParams;
134 typedef struct _SetVerticalScrollData SetVerticalScrollData;
139 typedef gint (*LineIteratorFunction) (GtkSText* text, LineParams* lp, void* data);
147 struct _SetVerticalScrollData {
149 gint last_didnt_wrap;
150 gint last_line_start;
151 GtkSPropertyMark mark;
156 /* The actual font. */
160 gint16 char_widths[256];
164 PROPERTY_FONT = 1 << 0,
165 PROPERTY_FOREGROUND = 1 << 1,
166 PROPERTY_BACKGROUND = 1 << 2
174 /* Background Color. */
177 /* Foreground Color. */
180 /* Show which properties are set */
181 TextPropertyFlags flags;
183 /* Length of this property. */
189 GList* tab_stops; /* Index into list containing the next tab position. If
190 * NULL, using default widths. */
197 TabStopMark tab_start;
200 struct _FetchLinesData
213 guint displayable_chars;
216 PrevTabCont tab_cont;
217 PrevTabCont tab_cont_next;
219 GtkSPropertyMark start;
220 GtkSPropertyMark end;
224 static void gtk_stext_class_init (GtkSTextClass *klass);
225 static void gtk_stext_set_arg (GtkObject *object,
228 static void gtk_stext_get_arg (GtkObject *object,
231 static void gtk_stext_init (GtkSText *text);
232 static void gtk_stext_destroy (GtkObject *object);
233 static void gtk_stext_finalize (GtkObject *object);
234 static void gtk_stext_realize (GtkWidget *widget);
235 static void gtk_stext_unrealize (GtkWidget *widget);
236 static void gtk_stext_style_set (GtkWidget *widget,
237 GtkStyle *previous_style);
238 static void gtk_stext_state_changed (GtkWidget *widget,
239 GtkStateType previous_state);
240 static void gtk_stext_draw_focus (GtkWidget *widget);
241 static void gtk_stext_size_request (GtkWidget *widget,
242 GtkRequisition *requisition);
243 static void gtk_stext_size_allocate (GtkWidget *widget,
244 GtkAllocation *allocation);
245 static void gtk_stext_adjustment (GtkAdjustment *adjustment,
247 static void gtk_stext_disconnect (GtkAdjustment *adjustment,
250 static void gtk_stext_insert_text (GtkEditable *editable,
251 const gchar *new_text,
252 gint new_text_length,
254 static void gtk_stext_delete_text (GtkEditable *editable,
257 static void gtk_stext_update_text (GtkEditable *editable,
260 static gchar *gtk_stext_get_chars (GtkEditable *editable,
263 static void gtk_stext_set_selection (GtkEditable *editable,
266 static void gtk_stext_real_set_editable (GtkEditable *editable,
267 gboolean is_editable);
270 static void gtk_stext_draw (GtkWidget *widget,
272 static gint gtk_stext_expose (GtkWidget *widget,
273 GdkEventExpose *event);
274 static gint gtk_stext_button_press (GtkWidget *widget,
275 GdkEventButton *event);
276 static gint gtk_stext_button_release (GtkWidget *widget,
277 GdkEventButton *event);
278 static gint gtk_stext_motion_notify (GtkWidget *widget,
279 GdkEventMotion *event);
280 static gint gtk_stext_key_press (GtkWidget *widget,
282 static gint gtk_stext_focus_in (GtkWidget *widget,
283 GdkEventFocus *event);
284 static gint gtk_stext_focus_out (GtkWidget *widget,
285 GdkEventFocus *event);
287 static void move_gap (GtkSText* text, guint index);
288 static void make_forward_space (GtkSText* text, guint len);
290 /* Property management */
291 static GtkSTextFont* get_text_font (GdkFont* gfont);
292 static void text_font_unref (GtkSTextFont *text_font);
294 static void insert_text_property (GtkSText* text, GdkFont* font,
295 GdkColor *fore, GdkColor* back, guint len);
296 static TextProperty* new_text_property (GtkSText *text, GdkFont* font,
297 GdkColor* fore, GdkColor* back, guint length);
298 static void destroy_text_property (TextProperty *prop);
299 static void init_properties (GtkSText *text);
300 static void realize_property (GtkSText *text, TextProperty *prop);
301 static void realize_properties (GtkSText *text);
302 static void unrealize_property (GtkSText *text, TextProperty *prop);
303 static void unrealize_properties (GtkSText *text);
305 static void delete_text_property (GtkSText* text, guint len);
307 static guint pixel_height_of (GtkSText* text, GList* cache_line);
309 /* Property Movement and Size Computations */
310 static void advance_mark (GtkSPropertyMark* mark);
311 static void decrement_mark (GtkSPropertyMark* mark);
312 static void advance_mark_n (GtkSPropertyMark* mark, gint n);
313 static void decrement_mark_n (GtkSPropertyMark* mark, gint n);
314 static void move_mark_n (GtkSPropertyMark* mark, gint n);
315 static GtkSPropertyMark find_mark (GtkSText* text, guint mark_position);
316 static GtkSPropertyMark find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near);
317 static void find_line_containing_point (GtkSText* text, guint point,
321 static void compute_lines_pixels (GtkSText* text, guint char_count,
322 guint *lines, guint *pixels);
324 static gint total_line_height (GtkSText* text,
327 static LineParams find_line_params (GtkSText* text,
328 const GtkSPropertyMark *mark,
329 const PrevTabCont *tab_cont,
330 PrevTabCont *next_cont);
331 static void recompute_geometry (GtkSText* text);
332 static void insert_expose (GtkSText* text, guint old_pixels, gint nchars, guint new_line_count);
333 static void delete_expose (GtkSText* text,
337 static GdkGC *create_bg_gc (GtkSText *text);
338 static void clear_area (GtkSText *text, GdkRectangle *area);
339 static void draw_line (GtkSText* text,
342 static void draw_line_wrap (GtkSText* text,
344 static void draw_cursor (GtkSText* text, gint absolute);
345 static void undraw_cursor (GtkSText* text, gint absolute);
346 static gint drawn_cursor_min (GtkSText* text);
347 static gint drawn_cursor_max (GtkSText* text);
348 static void expose_text (GtkSText* text, GdkRectangle *area, gboolean cursor);
350 /* Search and Placement. */
351 static void find_cursor (GtkSText* text,
353 static void find_cursor_at_line (GtkSText* text,
354 const LineParams* start_line,
356 static void find_mouse_cursor (GtkSText* text, gint x, gint y);
359 static void adjust_adj (GtkSText* text, GtkAdjustment* adj);
360 static void scroll_up (GtkSText* text, gint diff);
361 static void scroll_down (GtkSText* text, gint diff);
362 static void scroll_int (GtkSText* text, gint diff);
364 static void process_exposes (GtkSText *text);
366 /* Cache Management. */
367 static void free_cache (GtkSText* text);
368 static GList* remove_cache_line (GtkSText* text, GList* list);
371 static void move_cursor_buffer_ver (GtkSText *text, int dir);
372 static void move_cursor_page_ver (GtkSText *text, int dir);
373 static void move_cursor_ver (GtkSText *text, int count);
374 static void move_cursor_hor (GtkSText *text, int count);
378 static void move_cursor_to_display_row_end (GtkSText *text);
379 static void move_cursor_to_display_row_start (GtkSText *text);
380 static void move_cursor_to_display_row_up (GtkSText *text);
381 static void move_cursor_to_display_row_down (GtkSText *text);
382 static void reset_persist_col_pos (GtkSText *text);
384 /* Binding actions */
385 static void gtk_stext_move_cursor (GtkEditable *editable,
388 static void gtk_stext_move_word (GtkEditable *editable,
390 static void gtk_stext_move_page (GtkEditable *editable,
393 static void gtk_stext_move_to_row (GtkEditable *editable,
395 static void gtk_stext_move_to_column (GtkEditable *editable,
397 static void gtk_stext_kill_char (GtkEditable *editable,
399 static void gtk_stext_kill_word (GtkEditable *editable,
401 static void gtk_stext_kill_line (GtkEditable *editable,
405 static void gtk_stext_move_forward_character (GtkSText *text);
406 static void gtk_stext_move_backward_character (GtkSText *text);
407 static void gtk_stext_move_forward_word (GtkSText *text);
408 static void gtk_stext_move_backward_word (GtkSText *text);
409 static void gtk_stext_move_beginning_of_line (GtkSText *text);
410 static void gtk_stext_move_end_of_line (GtkSText *text);
411 static void gtk_stext_move_next_line (GtkSText *text);
412 static void gtk_stext_move_previous_line (GtkSText *text);
414 static void gtk_stext_delete_forward_character (GtkSText *text);
415 static void gtk_stext_delete_backward_character (GtkSText *text);
416 static void gtk_stext_delete_forward_word (GtkSText *text);
417 static void gtk_stext_delete_backward_word (GtkSText *text);
418 static void gtk_stext_delete_line (GtkSText *text);
419 static void gtk_stext_delete_to_line_end (GtkSText *text);
420 static void gtk_stext_select_word (GtkSText *text,
422 static void gtk_stext_select_line (GtkSText *text,
425 static void gtk_stext_set_position (GtkEditable *editable,
432 static void gtk_stext_enable_blink (GtkSText *text);
433 static void gtk_stext_disable_blink (GtkSText *text);
435 /* #define DEBUG_GTK_STEXT */
437 #if defined(DEBUG_GTK_STEXT) && defined(__GNUC__)
438 /* Debugging utilities. */
439 static void gtk_stext_assert_mark (GtkSText *text,
440 GtkSPropertyMark *mark,
441 GtkSPropertyMark *before,
442 GtkSPropertyMark *after,
447 static void gtk_stext_assert (GtkSText *text,
450 static void gtk_stext_show_cache_line (GtkSText *text, GList *cache,
451 const char* what, const char* func, gint line);
452 static void gtk_stext_show_cache (GtkSText *text, const char* func, gint line);
453 static void gtk_stext_show_adj (GtkSText *text,
458 static void gtk_stext_show_props (GtkSText* test,
462 #define TDEBUG(args) g_message args
463 #define TEXT_ASSERT(text) gtk_stext_assert (text,__PRETTY_FUNCTION__,__LINE__)
464 #define TEXT_ASSERT_MARK(text,mark,msg) gtk_stext_assert_mark (text,mark, \
465 __PRETTY_FUNCTION__,msg,__LINE__)
466 #define TEXT_SHOW(text) gtk_stext_show_cache (text, __PRETTY_FUNCTION__,__LINE__)
467 #define TEXT_SHOW_LINE(text,line,msg) gtk_stext_show_cache_line (text,line,msg,\
468 __PRETTY_FUNCTION__,__LINE__)
469 #define TEXT_SHOW_ADJ(text,adj,msg) gtk_stext_show_adj (text,adj,msg, \
470 __PRETTY_FUNCTION__,__LINE__)
473 #define TEXT_ASSERT(text)
474 #define TEXT_ASSERT_MARK(text,mark,msg)
475 #define TEXT_SHOW(text)
476 #define TEXT_SHOW_LINE(text,line,msg)
477 #define TEXT_SHOW_ADJ(text,adj,msg)
481 #if defined(AHX_DEBUG)
482 # define XDEBUG(args) g_message args
484 # define XDEBUG(args)
485 #endif /* AHX_DEBUG */
487 /* Memory Management. */
488 static GMemChunk *params_mem_chunk = NULL;
489 static GMemChunk *text_property_chunk = NULL;
491 static GtkWidgetClass *parent_class = NULL;
494 static const GtkTextFunction control_keys[26] =
496 (GtkTextFunction)gtk_stext_move_beginning_of_line, /* a */
497 (GtkTextFunction)gtk_stext_move_backward_character, /* b */
498 (GtkTextFunction)gtk_editable_copy_clipboard, /* c */
499 (GtkTextFunction)gtk_stext_delete_forward_character, /* d */
500 (GtkTextFunction)gtk_stext_move_end_of_line, /* e */
501 (GtkTextFunction)gtk_stext_move_forward_character, /* f */
503 (GtkTextFunction)gtk_stext_delete_backward_character, /* h */
506 (GtkTextFunction)gtk_stext_delete_to_line_end, /* k */
509 (GtkTextFunction)gtk_stext_move_next_line, /* n */
511 (GtkTextFunction)gtk_stext_move_previous_line, /* p */
516 (GtkTextFunction)gtk_stext_delete_line, /* u */
517 (GtkTextFunction)gtk_editable_paste_clipboard, /* v */
518 (GtkTextFunction)gtk_stext_delete_backward_word, /* w */
519 (GtkTextFunction)gtk_editable_cut_clipboard, /* x */
524 static const GtkTextFunction alt_keys[26] =
527 (GtkTextFunction)gtk_stext_move_backward_word, /* b */
529 (GtkTextFunction)gtk_stext_delete_forward_word, /* d */
531 (GtkTextFunction)gtk_stext_move_forward_word, /* f */
556 #define line_arrow_width 6
557 #define line_arrow_height 9
558 static unsigned char line_arrow_bits[] = {
559 0x00, 0x00, 0x04, 0x0c, 0x18, 0x3f, 0x18, 0x0c, 0x04};
562 #define line_wrap_width 6
563 #define line_wrap_height 9
564 static unsigned char line_wrap_bits[] = {
565 0x1e, 0x3e, 0x30, 0x30, 0x39, 0x1f, 0x0f, 0x0f, 0x1f, };
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 = 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,
822 gtk_stext_set_word_wrap (GtkSText *text,
825 g_return_if_fail (text != NULL);
826 g_return_if_fail (GTK_IS_STEXT (text));
828 text->word_wrap = (word_wrap != FALSE);
830 if (GTK_WIDGET_REALIZED (text))
832 recompute_geometry (text);
833 gtk_widget_queue_draw (GTK_WIDGET (text));
838 gtk_stext_set_line_wrap (GtkSText *text,
841 g_return_if_fail (text != NULL);
842 g_return_if_fail (GTK_IS_STEXT (text));
844 text->line_wrap = (line_wrap != FALSE);
846 if (GTK_WIDGET_REALIZED (text))
848 recompute_geometry (text);
849 gtk_widget_queue_draw (GTK_WIDGET (text));
854 gtk_stext_set_editable (GtkSText *text,
855 gboolean is_editable)
857 g_return_if_fail (text != NULL);
858 g_return_if_fail (GTK_IS_STEXT (text));
860 gtk_editable_set_editable (GTK_EDITABLE (text), is_editable);
864 gtk_stext_real_set_editable (GtkEditable *editable,
865 gboolean is_editable)
869 g_return_if_fail (editable != NULL);
870 g_return_if_fail (GTK_IS_STEXT (editable));
872 text = GTK_STEXT (editable);
874 editable->editable = (is_editable != FALSE);
876 if (GTK_WIDGET_REALIZED (text))
878 recompute_geometry (text);
879 gtk_widget_queue_draw (GTK_WIDGET (text));
884 gtk_stext_set_adjustments (GtkSText *text,
888 g_return_if_fail (text != NULL);
889 g_return_if_fail (GTK_IS_STEXT (text));
891 g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
893 hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
895 g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
897 vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
899 if (text->hadj && (text->hadj != hadj))
901 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
902 gtk_object_unref (GTK_OBJECT (text->hadj));
905 if (text->vadj && (text->vadj != vadj))
907 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
908 gtk_object_unref (GTK_OBJECT (text->vadj));
911 if (text->hadj != hadj)
914 gtk_object_ref (GTK_OBJECT (text->hadj));
915 gtk_object_sink (GTK_OBJECT (text->hadj));
917 gtk_signal_connect (GTK_OBJECT (text->hadj), "changed",
918 (GtkSignalFunc) gtk_stext_adjustment,
920 gtk_signal_connect (GTK_OBJECT (text->hadj), "value_changed",
921 (GtkSignalFunc) gtk_stext_adjustment,
923 gtk_signal_connect (GTK_OBJECT (text->hadj), "disconnect",
924 (GtkSignalFunc) gtk_stext_disconnect,
926 gtk_stext_adjustment (hadj, text);
929 if (text->vadj != vadj)
932 gtk_object_ref (GTK_OBJECT (text->vadj));
933 gtk_object_sink (GTK_OBJECT (text->vadj));
935 gtk_signal_connect (GTK_OBJECT (text->vadj), "changed",
936 (GtkSignalFunc) gtk_stext_adjustment,
938 gtk_signal_connect (GTK_OBJECT (text->vadj), "value_changed",
939 (GtkSignalFunc) gtk_stext_adjustment,
941 gtk_signal_connect (GTK_OBJECT (text->vadj), "disconnect",
942 (GtkSignalFunc) gtk_stext_disconnect,
944 gtk_stext_adjustment (vadj, text);
949 gtk_stext_set_point (GtkSText *text,
952 g_return_if_fail (text != NULL);
953 g_return_if_fail (GTK_IS_STEXT (text));
954 g_return_if_fail (index <= TEXT_LENGTH (text));
956 text->point = find_mark (text, index);
960 gtk_stext_get_point (GtkSText *text)
962 g_return_val_if_fail (text != NULL, 0);
963 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
965 return text->point.index;
969 gtk_stext_get_length (GtkSText *text)
971 g_return_val_if_fail (text != NULL, 0);
972 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
974 return TEXT_LENGTH (text);
978 gtk_stext_freeze (GtkSText *text)
980 g_return_if_fail (text != NULL);
981 g_return_if_fail (GTK_IS_STEXT (text));
983 text->freeze_count++;
984 undraw_cursor (text, FALSE);
988 gtk_stext_thaw (GtkSText *text)
990 g_return_if_fail (text != NULL);
991 g_return_if_fail (GTK_IS_STEXT (text));
993 if (text->freeze_count)
994 if (!(--text->freeze_count) && GTK_WIDGET_REALIZED (text))
996 recompute_geometry (text);
997 gtk_widget_queue_draw (GTK_WIDGET (text));
999 draw_cursor (text, FALSE);
1003 gtk_stext_insert (GtkSText *text,
1010 GtkEditable *editable = GTK_EDITABLE (text);
1011 gboolean frozen = FALSE;
1013 gint new_line_count = 1;
1014 guint old_height = 0;
1019 g_return_if_fail (text != NULL);
1020 g_return_if_fail (GTK_IS_STEXT (text));
1022 g_return_if_fail (chars != NULL);
1025 if (!nchars || !chars)
1027 nchars = strlen (chars);
1031 if (!text->freeze_count && (length > FREEZE_LENGTH))
1033 gtk_stext_freeze (text);
1037 if (!text->freeze_count && (text->line_start_cache != NULL))
1039 find_line_containing_point (text, text->point.index, TRUE);
1040 old_height = total_line_height (text, text->current_line, 1);
1043 if ((TEXT_LENGTH (text) == 0) && (text->use_wchar == FALSE))
1046 widget = GTK_WIDGET (text);
1047 gtk_widget_ensure_style (widget);
1048 if ((widget->style) && (widget->style->font->type == GDK_FONT_FONTSET))
1050 text->use_wchar = TRUE;
1051 g_free (text->text.ch);
1052 text->text.wc = g_new (GdkWChar, INITIAL_BUFFER_SIZE);
1053 text->text_len = INITIAL_BUFFER_SIZE;
1054 if (text->scratch_buffer.ch)
1055 g_free (text->scratch_buffer.ch);
1056 text->scratch_buffer.wc = NULL;
1057 text->scratch_buffer_len = 0;
1061 move_gap (text, text->point.index);
1062 make_forward_space (text, length);
1064 if (text->use_wchar)
1066 char *chars_nt = (char *)chars;
1069 chars_nt = g_new (char, length+1);
1070 memcpy (chars_nt, chars, length);
1071 chars_nt[length] = 0;
1073 numwcs = gdk_mbstowcs (text->text.wc + text->gap_position, chars_nt,
1075 if (chars_nt != chars)
1083 memcpy(text->text.ch + text->gap_position, chars, length);
1086 if (!text->freeze_count && (text->line_start_cache != NULL))
1088 if (text->use_wchar)
1090 for (i=0; i<numwcs; i++)
1091 if (text->text.wc[text->gap_position + i] == '\n')
1096 for (i=0; i<numwcs; i++)
1097 if (text->text.ch[text->gap_position + i] == '\n')
1104 insert_text_property (text, font, fore, back, numwcs);
1106 text->gap_size -= numwcs;
1107 text->gap_position += numwcs;
1109 if (text->point.index < text->first_line_start_index)
1110 text->first_line_start_index += numwcs;
1111 if (text->point.index < editable->selection_start_pos)
1112 editable->selection_start_pos += numwcs;
1113 if (text->point.index < editable->selection_end_pos)
1114 editable->selection_end_pos += numwcs;
1115 /* We'll reset the cursor later anyways if we aren't frozen */
1116 if (text->point.index < text->cursor_mark.index)
1117 text->cursor_mark.index += numwcs;
1119 advance_mark_n (&text->point, numwcs);
1121 if (!text->freeze_count && (text->line_start_cache != NULL))
1122 insert_expose (text, old_height, numwcs, new_line_count);
1126 gtk_stext_thaw (text);
1130 gtk_stext_backward_delete (GtkSText *text,
1133 g_return_val_if_fail (text != NULL, 0);
1134 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1136 if (nchars > text->point.index || nchars <= 0)
1139 gtk_stext_set_point (text, text->point.index - nchars);
1141 return gtk_stext_forward_delete (text, nchars);
1145 gtk_stext_forward_delete (GtkSText *text,
1148 guint old_lines, old_height;
1149 GtkEditable *editable = GTK_EDITABLE (text);
1150 gboolean frozen = FALSE;
1152 g_return_val_if_fail (text != NULL, 0);
1153 g_return_val_if_fail (GTK_IS_STEXT (text), 0);
1155 if (text->point.index + nchars > TEXT_LENGTH (text) || nchars <= 0)
1158 if (!text->freeze_count && nchars > FREEZE_LENGTH)
1160 gtk_stext_freeze (text);
1164 if (!text->freeze_count && text->line_start_cache != NULL)
1166 /* We need to undraw the cursor here, since we may later
1167 * delete the cursor's property
1169 undraw_cursor (text, FALSE);
1170 find_line_containing_point (text, text->point.index, TRUE);
1171 compute_lines_pixels (text, nchars, &old_lines, &old_height);
1174 /* FIXME, or resizing after deleting will be odd */
1175 if (text->point.index < text->first_line_start_index)
1177 if (text->point.index + nchars >= text->first_line_start_index)
1179 text->first_line_start_index = text->point.index;
1180 while ((text->first_line_start_index > 0) &&
1181 (GTK_STEXT_INDEX (text, text->first_line_start_index - 1)
1183 text->first_line_start_index -= 1;
1187 text->first_line_start_index -= nchars;
1190 if (text->point.index < editable->selection_start_pos)
1191 editable->selection_start_pos -=
1192 MIN(nchars, editable->selection_start_pos - text->point.index);
1193 if (text->point.index < editable->selection_end_pos)
1194 editable->selection_end_pos -=
1195 MIN(nchars, editable->selection_end_pos - text->point.index);
1196 /* We'll reset the cursor later anyways if we aren't frozen */
1197 if (text->point.index < text->cursor_mark.index)
1198 move_mark_n (&text->cursor_mark,
1199 -MIN(nchars, text->cursor_mark.index - text->point.index));
1201 move_gap (text, text->point.index);
1203 text->gap_size += nchars;
1205 delete_text_property (text, nchars);
1207 if (!text->freeze_count && (text->line_start_cache != NULL))
1209 delete_expose (text, nchars, old_lines, old_height);
1210 draw_cursor (text, FALSE);
1214 gtk_stext_thaw (text);
1220 gtk_stext_set_position (GtkEditable *editable,
1223 GtkSText *text = (GtkSText *) editable;
1225 undraw_cursor (text, FALSE);
1226 text->cursor_mark = find_mark (text, position);
1227 find_cursor (text, TRUE);
1228 draw_cursor (text, FALSE);
1229 gtk_editable_select_region (editable, 0, 0);
1232 /* SYLPHEED - set_position used. Need to find out whether there's
1233 * a better way to handle this */
1234 static void gtk_stext_set_position_X (GtkEditable *editable,
1237 GtkSText *text = (GtkSText *) editable;
1239 undraw_cursor (text, FALSE);
1240 text->cursor_mark = find_mark (text, position);
1241 find_cursor (text, TRUE);
1242 draw_cursor (text, FALSE);
1246 gtk_stext_get_chars (GtkEditable *editable,
1254 g_return_val_if_fail (editable != NULL, NULL);
1255 g_return_val_if_fail (GTK_IS_STEXT (editable), NULL);
1256 text = GTK_STEXT (editable);
1259 end_pos = TEXT_LENGTH (text);
1261 if ((start_pos < 0) ||
1262 (end_pos > TEXT_LENGTH (text)) ||
1263 (end_pos < start_pos))
1266 move_gap (text, TEXT_LENGTH (text));
1267 make_forward_space (text, 1);
1269 if (text->use_wchar)
1272 ch = text->text.wc[end_pos];
1273 text->text.wc[end_pos] = 0;
1274 retval = gdk_wcstombs (text->text.wc + start_pos);
1275 text->text.wc[end_pos] = ch;
1280 ch = text->text.ch[end_pos];
1281 text->text.ch[end_pos] = 0;
1282 retval = g_strdup (text->text.ch + start_pos);
1283 text->text.ch[end_pos] = ch;
1291 gtk_stext_destroy (GtkObject *object)
1295 g_return_if_fail (object != NULL);
1296 g_return_if_fail (GTK_IS_STEXT (object));
1298 text = (GtkSText*) object;
1300 gtk_signal_disconnect_by_data (GTK_OBJECT (text->hadj), text);
1301 gtk_signal_disconnect_by_data (GTK_OBJECT (text->vadj), text);
1305 gtk_timeout_remove (text->timer);
1312 gtk_stext_disable_blink(text);
1314 GTK_OBJECT_CLASS(parent_class)->destroy (object);
1318 gtk_stext_finalize (GtkObject *object)
1323 g_return_if_fail (object != NULL);
1324 g_return_if_fail (GTK_IS_STEXT (object));
1326 text = (GtkSText *)object;
1328 gtk_object_unref (GTK_OBJECT (text->hadj));
1329 gtk_object_unref (GTK_OBJECT (text->vadj));
1331 /* Clean up the internal structures */
1332 if (text->use_wchar)
1333 g_free (text->text.wc);
1335 g_free (text->text.ch);
1337 tmp_list = text->text_properties;
1340 destroy_text_property (tmp_list->data);
1341 tmp_list = tmp_list->next;
1344 if (text->current_font)
1345 text_font_unref (text->current_font);
1347 g_list_free (text->text_properties);
1349 if (text->use_wchar)
1351 if (text->scratch_buffer.wc)
1352 g_free (text->scratch_buffer.wc);
1356 if (text->scratch_buffer.ch)
1357 g_free (text->scratch_buffer.ch);
1360 g_list_free (text->tab_stops);
1362 GTK_OBJECT_CLASS(parent_class)->finalize (object);
1366 gtk_stext_realize (GtkWidget *widget)
1369 GtkEditable *editable;
1370 GdkWindowAttr attributes;
1371 gint attributes_mask;
1373 g_return_if_fail (widget != NULL);
1374 g_return_if_fail (GTK_IS_STEXT (widget));
1376 text = GTK_STEXT (widget);
1377 editable = GTK_EDITABLE (widget);
1378 GTK_WIDGET_SET_FLAGS (text, GTK_REALIZED);
1380 attributes.window_type = GDK_WINDOW_CHILD;
1381 attributes.x = widget->allocation.x;
1382 attributes.y = widget->allocation.y;
1383 attributes.width = widget->allocation.width;
1384 attributes.height = widget->allocation.height;
1385 attributes.wclass = GDK_INPUT_OUTPUT;
1386 attributes.visual = gtk_widget_get_visual (widget);
1387 attributes.colormap = gtk_widget_get_colormap (widget);
1388 attributes.event_mask = gtk_widget_get_events (widget);
1389 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1390 GDK_BUTTON_PRESS_MASK |
1391 GDK_BUTTON_RELEASE_MASK |
1392 GDK_BUTTON_MOTION_MASK |
1393 GDK_ENTER_NOTIFY_MASK |
1394 GDK_LEAVE_NOTIFY_MASK |
1395 GDK_KEY_PRESS_MASK);
1396 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1398 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1399 gdk_window_set_user_data (widget->window, text);
1401 attributes.x = (widget->style->klass->xthickness + TEXT_BORDER_ROOM);
1402 attributes.y = (widget->style->klass->ythickness + TEXT_BORDER_ROOM);
1403 attributes.width = MAX (1, (gint)widget->allocation.width - (gint)attributes.x * 2);
1404 attributes.height = MAX (1, (gint)widget->allocation.height - (gint)attributes.y * 2);
1406 attributes.cursor = gdk_cursor_new (GDK_XTERM);
1407 attributes_mask |= GDK_WA_CURSOR;
1409 text->text_area = gdk_window_new (widget->window, &attributes, attributes_mask);
1410 gdk_window_set_user_data (text->text_area, text);
1412 gdk_cursor_destroy (attributes.cursor); /* The X server will keep it around as long as necessary */
1414 widget->style = gtk_style_attach (widget->style, widget->window);
1416 /* Can't call gtk_style_set_background here because it's handled specially */
1417 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1418 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1420 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1421 text->bg_gc = create_bg_gc (text);
1423 text->line_wrap_bitmap = gdk_bitmap_create_from_data (text->text_area,
1424 (gchar*) line_wrap_bits,
1428 text->line_arrow_bitmap = gdk_bitmap_create_from_data (text->text_area,
1429 (gchar*) line_arrow_bits,
1433 text->gc = gdk_gc_new (text->text_area);
1434 gdk_gc_set_exposures (text->gc, TRUE);
1435 gdk_gc_set_foreground (text->gc, &widget->style->text[GTK_STATE_NORMAL]);
1437 #ifdef USE_GTKGDK_XIM
1438 if (gdk_im_ready () && (editable->ic_attr = gdk_ic_attr_new ()) != NULL)
1441 GdkColormap *colormap;
1443 GdkICAttr *attr = editable->ic_attr;
1444 GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
1446 GdkIMStyle supported_style = GDK_IM_PREEDIT_NONE |
1447 GDK_IM_PREEDIT_NOTHING |
1448 GDK_IM_PREEDIT_POSITION |
1449 GDK_IM_STATUS_NONE |
1450 GDK_IM_STATUS_NOTHING;
1452 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1453 supported_style &= ~GDK_IM_PREEDIT_POSITION;
1455 attr->style = style = gdk_im_decide_style (supported_style);
1456 attr->client_window = text->text_area;
1458 if ((colormap = gtk_widget_get_colormap (widget)) !=
1459 gtk_widget_get_default_colormap ())
1461 attrmask |= GDK_IC_PREEDIT_COLORMAP;
1462 attr->preedit_colormap = colormap;
1465 switch (style & GDK_IM_PREEDIT_MASK)
1467 case GDK_IM_PREEDIT_POSITION:
1468 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1470 g_warning ("over-the-spot style requires fontset");
1474 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
1475 gdk_window_get_size (text->text_area, &width, &height);
1476 attr->spot_location.x = 0;
1477 attr->spot_location.y = height;
1478 attr->preedit_area.x = 0;
1479 attr->preedit_area.y = 0;
1480 attr->preedit_area.width = width;
1481 attr->preedit_area.height = height;
1482 attr->preedit_fontset = widget->style->font;
1486 editable->ic = gdk_ic_new (attr, attrmask);
1488 if (editable->ic == NULL)
1489 g_warning ("Can't create input context.");
1492 mask = gdk_window_get_events (text->text_area);
1493 mask |= gdk_ic_get_events (editable->ic);
1494 gdk_window_set_events (text->text_area, mask);
1496 if (GTK_WIDGET_HAS_FOCUS (widget))
1497 gdk_im_begin (editable->ic, text->text_area);
1502 realize_properties (text);
1503 gdk_window_show (text->text_area);
1504 init_properties (text);
1506 if (editable->selection_start_pos != editable->selection_end_pos)
1507 gtk_editable_claim_selection (editable, TRUE, GDK_CURRENT_TIME);
1509 recompute_geometry (text);
1513 gtk_stext_style_set (GtkWidget *widget,
1514 GtkStyle *previous_style)
1516 GtkSText *text = GTK_STEXT (widget);
1518 if (GTK_WIDGET_REALIZED (widget))
1520 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1521 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1525 gdk_gc_destroy (text->bg_gc);
1529 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1530 text->bg_gc = create_bg_gc (text);
1532 recompute_geometry (text);
1535 if (text->current_font)
1536 text_font_unref (text->current_font);
1537 text->current_font = get_text_font (widget->style->font);
1541 gtk_stext_state_changed (GtkWidget *widget,
1542 GtkStateType previous_state)
1544 GtkSText *text = GTK_STEXT (widget);
1546 if (GTK_WIDGET_REALIZED (widget))
1548 gdk_window_set_background (widget->window, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1549 gdk_window_set_background (text->text_area, &widget->style->base[GTK_WIDGET_STATE (widget)]);
1554 gtk_stext_unrealize (GtkWidget *widget)
1558 g_return_if_fail (widget != NULL);
1559 g_return_if_fail (GTK_IS_STEXT (widget));
1561 text = GTK_STEXT (widget);
1563 #ifdef USE_GTKGDK_XIM
1564 if (GTK_EDITABLE (widget)->ic)
1566 gdk_ic_destroy (GTK_EDITABLE (widget)->ic);
1567 GTK_EDITABLE (widget)->ic = NULL;
1569 if (GTK_EDITABLE (widget)->ic_attr)
1571 gdk_ic_attr_destroy (GTK_EDITABLE (widget)->ic_attr);
1572 GTK_EDITABLE (widget)->ic_attr = NULL;
1576 gdk_window_set_user_data (text->text_area, NULL);
1577 gdk_window_destroy (text->text_area);
1578 text->text_area = NULL;
1580 gdk_gc_destroy (text->gc);
1585 gdk_gc_destroy (text->bg_gc);
1589 gdk_pixmap_unref (text->line_wrap_bitmap);
1590 gdk_pixmap_unref (text->line_arrow_bitmap);
1592 unrealize_properties (text);
1596 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
1597 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1601 clear_focus_area (GtkSText *text, gint area_x, gint area_y, gint area_width, gint area_height)
1603 GtkWidget *widget = GTK_WIDGET (text);
1606 gint ythick = TEXT_BORDER_ROOM + widget->style->klass->ythickness;
1607 gint xthick = TEXT_BORDER_ROOM + widget->style->klass->xthickness;
1611 if (area_width == 0 || area_height == 0)
1614 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
1616 gdk_window_get_size (widget->style->bg_pixmap[GTK_STATE_NORMAL], &width, &height);
1618 gdk_gc_set_ts_origin (text->bg_gc,
1619 (- (gint)text->first_onscreen_hor_pixel + xthick) % width,
1620 (- (gint)text->first_onscreen_ver_pixel + ythick) % height);
1625 gc = widget->style->bg_gc[widget->state];
1628 gdk_draw_rectangle (GTK_WIDGET (text)->window, gc, TRUE,
1629 area_x, area_y, area_width, area_height);
1633 gtk_stext_draw_focus (GtkWidget *widget)
1639 g_return_if_fail (widget != NULL);
1640 g_return_if_fail (GTK_IS_STEXT (widget));
1642 text = GTK_STEXT (widget);
1644 if (GTK_WIDGET_DRAWABLE (widget))
1646 gint ythick = widget->style->klass->ythickness;
1647 gint xthick = widget->style->klass->xthickness;
1648 gint xextra = TEXT_BORDER_ROOM;
1649 gint yextra = TEXT_BORDER_ROOM;
1651 TDEBUG (("in gtk_stext_draw_focus\n"));
1655 width = widget->allocation.width;
1656 height = widget->allocation.height;
1658 if (GTK_WIDGET_HAS_FOCUS (widget))
1667 gtk_paint_focus (widget->style, widget->window,
1668 NULL, widget, "text",
1670 widget->allocation.width - 1,
1671 widget->allocation.height - 1);
1674 gtk_paint_shadow (widget->style, widget->window,
1675 GTK_STATE_NORMAL, GTK_SHADOW_IN,
1676 NULL, widget, "text",
1677 x, y, width, height);
1681 width -= 2 * xthick;
1682 height -= 2 * ythick;
1685 clear_focus_area (text, x, y, width, yextra);
1687 clear_focus_area (text, x, y + yextra,
1688 xextra, y + height - 2 * yextra);
1690 clear_focus_area (text, x + width - xextra, y + yextra,
1691 xextra, height - 2 * ythick);
1693 clear_focus_area (text, x, x + height - yextra, width, yextra);
1697 TDEBUG (("in gtk_stext_draw_focus (undrawable !!!)\n"));
1702 gtk_stext_size_request (GtkWidget *widget,
1703 GtkRequisition *requisition)
1710 g_return_if_fail (widget != NULL);
1711 g_return_if_fail (GTK_IS_STEXT (widget));
1712 g_return_if_fail (requisition != NULL);
1714 xthickness = widget->style->klass->xthickness + TEXT_BORDER_ROOM;
1715 ythickness = widget->style->klass->ythickness + TEXT_BORDER_ROOM;
1717 char_height = MIN_TEXT_HEIGHT_LINES * (widget->style->font->ascent +
1718 widget->style->font->descent);
1720 char_width = MIN_TEXT_WIDTH_LINES * (gdk_text_width (widget->style->font,
1721 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
1725 requisition->width = char_width + xthickness * 2;
1726 requisition->height = char_height + ythickness * 2;
1730 gtk_stext_size_allocate (GtkWidget *widget,
1731 GtkAllocation *allocation)
1734 GtkEditable *editable;
1736 g_return_if_fail (widget != NULL);
1737 g_return_if_fail (GTK_IS_STEXT (widget));
1738 g_return_if_fail (allocation != NULL);
1740 text = GTK_STEXT (widget);
1741 editable = GTK_EDITABLE (widget);
1743 widget->allocation = *allocation;
1744 if (GTK_WIDGET_REALIZED (widget))
1746 gdk_window_move_resize (widget->window,
1747 allocation->x, allocation->y,
1748 allocation->width, allocation->height);
1750 gdk_window_move_resize (text->text_area,
1751 widget->style->klass->xthickness + TEXT_BORDER_ROOM,
1752 widget->style->klass->ythickness + TEXT_BORDER_ROOM,
1753 MAX (1, (gint)widget->allocation.width - (gint)(widget->style->klass->xthickness +
1754 (gint)TEXT_BORDER_ROOM) * 2),
1755 MAX (1, (gint)widget->allocation.height - (gint)(widget->style->klass->ythickness +
1756 (gint)TEXT_BORDER_ROOM) * 2));
1758 #ifdef USE_GTKGDK_XIM
1759 if (editable->ic && (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
1763 gdk_window_get_size (text->text_area, &width, &height);
1764 editable->ic_attr->preedit_area.width = width;
1765 editable->ic_attr->preedit_area.height = height;
1767 gdk_ic_set_attr (editable->ic,
1768 editable->ic_attr, GDK_IC_PREEDIT_AREA);
1772 recompute_geometry (text);
1777 gtk_stext_draw (GtkWidget *widget,
1780 g_return_if_fail (widget != NULL);
1781 g_return_if_fail (GTK_IS_STEXT (widget));
1782 g_return_if_fail (area != NULL);
1784 if (GTK_WIDGET_DRAWABLE (widget))
1786 expose_text (GTK_STEXT (widget), area, TRUE);
1787 gtk_widget_draw_focus (widget);
1792 gtk_stext_expose (GtkWidget *widget,
1793 GdkEventExpose *event)
1795 g_return_val_if_fail (widget != NULL, FALSE);
1796 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1797 g_return_val_if_fail (event != NULL, FALSE);
1799 if (event->window == GTK_STEXT (widget)->text_area)
1801 TDEBUG (("in gtk_stext_expose (expose)\n"));
1802 expose_text (GTK_STEXT (widget), &event->area, TRUE);
1804 else if (event->count == 0)
1806 TDEBUG (("in gtk_stext_expose (focus)\n"));
1807 gtk_widget_draw_focus (widget);
1814 gtk_stext_scroll_timeout (gpointer data)
1817 GdkEventMotion event;
1819 GdkModifierType mask;
1821 GDK_THREADS_ENTER ();
1823 text = GTK_STEXT (data);
1826 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
1828 if (mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK))
1835 gtk_stext_motion_notify (GTK_WIDGET (text), &event);
1838 GDK_THREADS_LEAVE ();
1844 gtk_stext_button_press (GtkWidget *widget,
1845 GdkEventButton *event)
1848 GtkEditable *editable;
1849 static GdkAtom ctext_atom = GDK_NONE;
1851 g_return_val_if_fail (widget != NULL, FALSE);
1852 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1853 g_return_val_if_fail (event != NULL, FALSE);
1855 if (ctext_atom == GDK_NONE)
1856 ctext_atom = gdk_atom_intern ("COMPOUND_TEXT", FALSE);
1858 text = GTK_STEXT (widget);
1859 editable = GTK_EDITABLE (widget);
1861 if (text->button && (event->button != text->button))
1864 text->button = event->button;
1866 if (!GTK_WIDGET_HAS_FOCUS (widget))
1867 gtk_widget_grab_focus (widget);
1869 if (event->button == 1)
1871 switch (event->type)
1873 case GDK_BUTTON_PRESS:
1874 gtk_grab_add (widget);
1876 undraw_cursor (text, FALSE);
1877 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1878 draw_cursor (text, FALSE);
1880 /* Set it now, so we display things right. We'll unset it
1881 * later if things don't work out */
1882 editable->has_selection = TRUE;
1883 gtk_stext_set_selection (GTK_EDITABLE(text),
1884 text->cursor_mark.index,
1885 text->cursor_mark.index);
1889 case GDK_2BUTTON_PRESS:
1890 gtk_stext_select_word (text, event->time);
1893 case GDK_3BUTTON_PRESS:
1894 gtk_stext_select_line (text, event->time);
1901 else if (event->type == GDK_BUTTON_PRESS)
1903 if ((event->button == 2) && editable->editable)
1905 if (editable->selection_start_pos == editable->selection_end_pos ||
1906 editable->has_selection)
1908 undraw_cursor (text, FALSE);
1909 find_mouse_cursor (text, (gint)event->x, (gint)event->y);
1910 draw_cursor (text, FALSE);
1914 gtk_selection_convert (widget, GDK_SELECTION_PRIMARY,
1915 ctext_atom, event->time);
1919 gtk_grab_add (widget);
1921 undraw_cursor (text, FALSE);
1922 find_mouse_cursor (text, event->x, event->y);
1923 draw_cursor (text, FALSE);
1925 gtk_stext_set_selection (GTK_EDITABLE(text),
1926 text->cursor_mark.index,
1927 text->cursor_mark.index);
1929 editable->has_selection = FALSE;
1930 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1931 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1939 gtk_stext_button_release (GtkWidget *widget,
1940 GdkEventButton *event)
1943 GtkEditable *editable;
1944 g_return_val_if_fail (widget != NULL, FALSE);
1945 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
1946 g_return_val_if_fail (event != NULL, FALSE);
1948 text = GTK_STEXT (widget);
1950 gtk_grab_remove (widget);
1952 if (text->button != event->button)
1959 gtk_timeout_remove (text->timer);
1963 if (event->button == 1)
1965 text = GTK_STEXT (widget);
1966 editable = GTK_EDITABLE (widget);
1968 gtk_grab_remove (widget);
1970 editable->has_selection = FALSE;
1971 if (editable->selection_start_pos != editable->selection_end_pos)
1973 if (gtk_selection_owner_set (widget,
1974 GDK_SELECTION_PRIMARY,
1976 editable->has_selection = TRUE;
1978 gtk_stext_update_text (editable, editable->selection_start_pos,
1979 editable->selection_end_pos);
1983 if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
1984 gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY, event->time);
1987 else if (event->button == 3)
1989 gtk_grab_remove (widget);
1992 undraw_cursor (text, FALSE);
1993 find_cursor (text, TRUE);
1994 draw_cursor (text, FALSE);
2000 gtk_stext_motion_notify (GtkWidget *widget,
2001 GdkEventMotion *event)
2006 GdkModifierType mask;
2008 g_return_val_if_fail (widget != NULL, FALSE);
2009 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2010 g_return_val_if_fail (event != NULL, FALSE);
2012 text = GTK_STEXT (widget);
2016 mask = event->state;
2017 if (event->is_hint || (text->text_area != event->window))
2019 gdk_window_get_pointer (text->text_area, &x, &y, &mask);
2022 if ((text->button == 0) ||
2023 !(mask & (GDK_BUTTON1_MASK | GDK_BUTTON3_MASK)))
2026 gdk_window_get_size (text->text_area, NULL, &height);
2028 if ((y < 0) || (y > height))
2030 if (text->timer == 0)
2032 text->timer = gtk_timeout_add (SCROLL_TIME,
2033 gtk_stext_scroll_timeout,
2037 scroll_int (text, y/2);
2039 scroll_int (text, (y - height)/2);
2045 undraw_cursor (GTK_STEXT (widget), FALSE);
2046 find_mouse_cursor (GTK_STEXT (widget), x, y);
2047 draw_cursor (GTK_STEXT (widget), FALSE);
2049 gtk_stext_set_selection (GTK_EDITABLE(text),
2050 GTK_EDITABLE(text)->selection_start_pos,
2051 text->cursor_mark.index);
2057 gtk_stext_insert_text (GtkEditable *editable,
2058 const gchar *new_text,
2059 gint new_text_length,
2062 GtkSText *text = GTK_STEXT (editable);
2064 GdkColor *fore, *back;
2066 TextProperty *property;
2068 gtk_stext_set_point (text, *position);
2070 property = MARK_CURRENT_PROPERTY (&text->point);
2071 font = property->flags & PROPERTY_FONT ? property->font->gdk_font : NULL;
2072 fore = property->flags & PROPERTY_FOREGROUND ? &property->fore_color : NULL;
2073 back = property->flags & PROPERTY_BACKGROUND ? &property->back_color : NULL;
2075 gtk_stext_insert (text, font, fore, back, new_text, new_text_length);
2077 *position = text->point.index;
2081 gtk_stext_delete_text (GtkEditable *editable,
2087 g_return_if_fail (start_pos >= 0);
2089 text = GTK_STEXT (editable);
2091 gtk_stext_set_point (text, start_pos);
2093 end_pos = TEXT_LENGTH (text);
2095 if (end_pos > start_pos)
2096 gtk_stext_forward_delete (text, end_pos - start_pos);
2100 gtk_stext_key_press (GtkWidget *widget,
2104 GtkEditable *editable;
2109 g_return_val_if_fail (widget != NULL, FALSE);
2110 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2111 g_return_val_if_fail (event != NULL, FALSE);
2115 text = GTK_STEXT (widget);
2116 editable = GTK_EDITABLE (widget);
2118 key = event->keyval;
2121 if ((GTK_EDITABLE(text)->editable == FALSE))
2123 switch (event->keyval)
2126 if (event->state & GDK_CONTROL_MASK)
2127 scroll_int (text, -text->vadj->value);
2132 if (event->state & GDK_CONTROL_MASK)
2133 scroll_int (text, +text->vadj->upper);
2137 case GDK_Page_Up: scroll_int (text, -text->vadj->page_increment); break;
2138 case GDK_Page_Down: scroll_int (text, +text->vadj->page_increment); break;
2140 scroll_int (text, -KEY_SCROLL_PIXELS);
2142 case GDK_Down: scroll_int (text, +KEY_SCROLL_PIXELS); break;
2144 if (event->state & GDK_CONTROL_MASK)
2145 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2156 gint extend_selection;
2158 guint initial_pos = editable->current_pos;
2160 text->point = find_mark (text, text->cursor_mark.index);
2162 extend_selection = event->state & GDK_SHIFT_MASK;
2163 extend_start = FALSE;
2165 if (extend_selection)
2167 editable->has_selection = TRUE;
2169 if (editable->selection_start_pos == editable->selection_end_pos)
2171 editable->selection_start_pos = text->point.index;
2172 editable->selection_end_pos = text->point.index;
2175 extend_start = (text->point.index == editable->selection_start_pos);
2176 gtk_stext_disable_blink(text);
2182 if (!extend_selection)
2183 gtk_stext_enable_blink(text);
2185 if (event->keyval != GDK_Up && event->keyval != GDK_Down) {
2186 reset_persist_col_pos(text);
2189 switch (event->keyval)
2192 if (event->state & GDK_CONTROL_MASK)
2193 move_cursor_buffer_ver (text, -1);
2195 if (text->wrap_rmargin > 0) {
2196 /* SYLPHEED: line start */
2197 move_cursor_to_display_row_start(text);
2200 gtk_stext_move_beginning_of_line (text);
2205 if (event->state & GDK_CONTROL_MASK)
2206 move_cursor_buffer_ver (text, +1);
2208 if (text->wrap_rmargin > 0) {
2209 /* SYLPHEED: line end */
2210 move_cursor_to_display_row_end(text);
2213 gtk_stext_move_end_of_line(text);
2218 move_cursor_page_ver (text, -1);
2220 case GDK_Page_Down: move_cursor_page_ver (text, +1); break;
2221 /* CUA has Ctrl-Up/Ctrl-Down as paragraph up down */
2223 if (text->wrap_rmargin > 0) {
2226 move_cursor_to_display_row_up(text);
2229 move_cursor_ver (text, -1);
2233 move_cursor_to_display_row_down(text);
2234 // move_cursor_ver (text, +1);
2237 if (event->state & GDK_CONTROL_MASK)
2238 gtk_stext_move_backward_word (text);
2240 move_cursor_hor (text, -1);
2243 if (event->state & GDK_CONTROL_MASK)
2244 gtk_stext_move_forward_word (text);
2246 move_cursor_hor (text, +1);
2250 if (event->state & GDK_CONTROL_MASK)
2251 gtk_stext_delete_backward_word (text);
2253 gtk_stext_delete_backward_character (text);
2256 gtk_stext_delete_line (text);
2259 if (event->state & GDK_SHIFT_MASK)
2261 extend_selection = FALSE;
2262 gtk_editable_paste_clipboard (editable);
2264 else if (event->state & GDK_CONTROL_MASK)
2266 gtk_editable_copy_clipboard (editable);
2270 /* gtk_toggle_insert(text) -- IMPLEMENT */
2275 if (event->state & GDK_CONTROL_MASK) {
2276 gtk_stext_delete_forward_word (text);
2278 else if (event->state & GDK_SHIFT_MASK)
2280 extend_selection = FALSE;
2281 gtk_editable_cut_clipboard (editable);
2284 gtk_stext_delete_forward_character (text);
2289 position = text->point.index;
2290 gtk_editable_insert_text (editable, "\t", 1, &position);
2293 if (event->state & GDK_CONTROL_MASK)
2294 gtk_signal_emit_by_name (GTK_OBJECT (text), "activate");
2297 position = text->point.index;
2298 gtk_editable_insert_text (editable, "\n", 1, &position);
2302 /* Don't insert literally */
2309 if (event->state & GDK_CONTROL_MASK)
2311 if ((key >= 'A') && (key <= 'Z'))
2314 if ((key >= 'a') && (key <= 'z') && control_keys[(int) (key - 'a')])
2316 (* control_keys[(int) (key - 'a')]) (editable, event->time);
2322 else if (event->state & GDK_MOD1_MASK)
2324 if ((key >= 'A') && (key <= 'Z'))
2327 if ((key >= 'a') && (key <= 'z') && alt_keys[(int) (key - 'a')])
2329 (* alt_keys[(int) (key - 'a')]) (editable, event->time);
2335 else if (event->length > 0)
2337 extend_selection = FALSE;
2339 gtk_editable_delete_selection (editable);
2340 position = text->point.index;
2341 gtk_editable_insert_text (editable, event->string, event->length, &position);
2349 if (return_val && (editable->current_pos != initial_pos))
2351 if (extend_selection)
2353 if (editable->current_pos < editable->selection_start_pos)
2354 gtk_stext_set_selection (editable, editable->current_pos,
2355 editable->selection_end_pos);
2356 else if (editable->current_pos > editable->selection_end_pos)
2357 gtk_stext_set_selection (editable, editable->selection_start_pos,
2358 editable->current_pos);
2362 gtk_stext_set_selection (editable, editable->current_pos,
2363 editable->selection_end_pos);
2365 gtk_stext_set_selection (editable, editable->selection_start_pos,
2366 editable->current_pos);
2370 gtk_stext_set_selection (editable, 0, 0);
2372 gtk_editable_claim_selection (editable,
2373 editable->selection_start_pos != editable->selection_end_pos,
2382 gtk_stext_focus_in (GtkWidget *widget,
2383 GdkEventFocus *event)
2385 g_return_val_if_fail (widget != NULL, FALSE);
2386 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2387 g_return_val_if_fail (event != NULL, FALSE);
2389 TDEBUG (("in gtk_stext_focus_in\n"));
2391 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
2392 gtk_widget_draw_focus (widget);
2394 #ifdef USE_GTKGDK_XIM
2395 if (GTK_EDITABLE(widget)->ic)
2396 gdk_im_begin (GTK_EDITABLE(widget)->ic, GTK_STEXT(widget)->text_area);
2399 draw_cursor (GTK_STEXT(widget), TRUE);
2400 GTK_STEXT(widget)->cursor_visible = TRUE;
2401 gtk_stext_enable_blink(GTK_STEXT(widget));
2407 gtk_stext_focus_out (GtkWidget *widget,
2408 GdkEventFocus *event)
2410 g_return_val_if_fail (widget != NULL, FALSE);
2411 g_return_val_if_fail (GTK_IS_STEXT (widget), FALSE);
2412 g_return_val_if_fail (event != NULL, FALSE);
2414 TDEBUG (("in gtk_stext_focus_out\n"));
2416 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
2417 gtk_widget_draw_focus (widget);
2420 * should disable blink before undrawing the cursor - disabling blink
2421 * redraws the cursor.
2423 gtk_stext_disable_blink(GTK_STEXT(widget));
2424 undraw_cursor (GTK_STEXT(widget), TRUE);
2425 GTK_STEXT(widget)->cursor_visible = FALSE;
2427 #ifdef USE_GTKGDK_XIM
2435 gtk_stext_adjustment (GtkAdjustment *adjustment,
2440 g_return_if_fail (adjustment != NULL);
2441 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2442 g_return_if_fail (text != NULL);
2443 g_return_if_fail (GTK_IS_STEXT (text));
2445 /* Clamp the value here, because we'll get really confused
2446 * if someone tries to move the adjusment outside of the
2449 old_val = adjustment->value;
2451 adjustment->value = MIN (adjustment->value, adjustment->upper - adjustment->page_size);
2452 adjustment->value = MAX (adjustment->value, 0.0);
2454 if (adjustment->value != old_val)
2456 gtk_signal_handler_block_by_func (GTK_OBJECT (adjustment),
2457 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2459 gtk_adjustment_changed (adjustment);
2460 gtk_signal_handler_unblock_by_func (GTK_OBJECT (adjustment),
2461 GTK_SIGNAL_FUNC (gtk_stext_adjustment),
2465 /* Just ignore it if we haven't been size-allocated and realized yet */
2466 if (text->line_start_cache == NULL)
2469 if (adjustment == text->hadj)
2471 g_warning ("horizontal scrolling not implemented");
2475 gint diff = ((gint)adjustment->value) - text->last_ver_value;
2479 undraw_cursor (text, FALSE);
2482 scroll_down (text, diff);
2483 else /* if (diff < 0) */
2484 scroll_up (text, diff);
2486 draw_cursor (text, FALSE);
2488 text->last_ver_value = adjustment->value;
2494 gtk_stext_disconnect (GtkAdjustment *adjustment,
2497 g_return_if_fail (adjustment != NULL);
2498 g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
2499 g_return_if_fail (text != NULL);
2500 g_return_if_fail (GTK_IS_STEXT (text));
2502 if (adjustment == text->hadj)
2503 gtk_stext_set_adjustments (text, NULL, text->vadj);
2504 if (adjustment == text->vadj)
2505 gtk_stext_set_adjustments (text, text->hadj, NULL);
2509 static GtkSPropertyMark
2510 find_this_line_start_mark (GtkSText* text, guint point_position, const GtkSPropertyMark* near)
2512 GtkSPropertyMark mark;
2514 mark = find_mark_near (text, point_position, near);
2516 while (mark.index > 0 &&
2517 GTK_STEXT_INDEX (text, mark.index - 1) != LINE_DELIM)
2518 decrement_mark (&mark);
2524 init_tab_cont (GtkSText* text, PrevTabCont* tab_cont)
2526 tab_cont->pixel_offset = 0;
2527 tab_cont->tab_start.tab_stops = text->tab_stops;
2528 tab_cont->tab_start.to_next_tab = (gulong) text->tab_stops->data;
2530 if (!tab_cont->tab_start.to_next_tab)
2531 tab_cont->tab_start.to_next_tab = text->default_tab_width;
2535 line_params_iterate (GtkSText* text,
2536 const GtkSPropertyMark* mark0,
2537 const PrevTabCont* tab_mark0,
2540 LineIteratorFunction iter)
2541 /* mark0 MUST be a real line start. if ALLOC, allocate line params
2542 * from a mem chunk. DATA is passed to ITER_CALL, which is called
2543 * for each line following MARK, iteration continues unless ITER_CALL
2546 GtkSPropertyMark mark = *mark0;
2547 PrevTabCont tab_conts[2];
2548 LineParams *lp, lpbuf;
2549 gint tab_cont_index = 0;
2552 tab_conts[0] = *tab_mark0;
2554 init_tab_cont (text, tab_conts);
2559 lp = g_chunk_new (LineParams, params_mem_chunk);
2563 *lp = find_line_params (text, &mark, tab_conts + tab_cont_index,
2564 tab_conts + (tab_cont_index + 1) % 2);
2566 if ((*iter) (text, lp, data))
2569 if (LAST_INDEX (text, lp->end))
2573 advance_mark (&mark);
2574 tab_cont_index = (tab_cont_index + 1) % 2;
2579 fetch_lines_iterator (GtkSText* text, LineParams* lp, void* data)
2581 FetchLinesData *fldata = (FetchLinesData*) data;
2583 fldata->new_lines = g_list_prepend (fldata->new_lines, lp);
2585 switch (fldata->fl_type)
2587 case FetchLinesCount:
2588 if (!text->line_wrap || !lp->wraps)
2591 if (fldata->data >= fldata->data_max)
2595 case FetchLinesPixels:
2597 fldata->data += LINE_HEIGHT(*lp);
2599 if (fldata->data >= fldata->data_max)
2609 fetch_lines (GtkSText* text,
2610 const GtkSPropertyMark* mark0,
2611 const PrevTabCont* tab_cont0,
2615 FetchLinesData fl_data;
2617 fl_data.new_lines = NULL;
2619 fl_data.data_max = data;
2620 fl_data.fl_type = fl_type;
2622 line_params_iterate (text, mark0, tab_cont0, TRUE, &fl_data, fetch_lines_iterator);
2624 return g_list_reverse (fl_data.new_lines);
2628 fetch_lines_backward (GtkSText* text)
2630 GList* new_lines = NULL, *new_line_start;
2631 GtkSPropertyMark mark;
2633 if (CACHE_DATA(text->line_start_cache).start.index == 0)
2636 mark = find_this_line_start_mark (text,
2637 CACHE_DATA(text->line_start_cache).start.index - 1,
2638 &CACHE_DATA(text->line_start_cache).start);
2640 new_line_start = new_lines = fetch_lines (text, &mark, NULL, FetchLinesCount, 1);
2642 while (new_line_start->next)
2643 new_line_start = new_line_start->next;
2645 new_line_start->next = text->line_start_cache;
2646 text->line_start_cache->prev = new_line_start;
2650 fetch_lines_forward (GtkSText* text, gint line_count)
2652 GtkSPropertyMark mark;
2653 GList* line = text->line_start_cache;
2658 mark = CACHE_DATA(line).end;
2660 if (LAST_INDEX (text, mark))
2663 advance_mark(&mark);
2665 line->next = fetch_lines (text, &mark, &CACHE_DATA(line).tab_cont_next, FetchLinesCount, line_count);
2668 line->next->prev = line;
2671 /* Compute the number of lines, and vertical pixels for n characters
2672 * starting from the point
2675 compute_lines_pixels (GtkSText* text, guint char_count,
2676 guint *lines, guint *pixels)
2678 GList *line = text->current_line;
2679 gint chars_left = char_count;
2684 /* If chars_left == 0, that means we're joining two lines in a
2685 * deletion, so add in the values for the next line as well
2687 for (; line && chars_left >= 0; line = line->next)
2689 *pixels += LINE_HEIGHT(CACHE_DATA(line));
2691 if (line == text->current_line)
2692 chars_left -= CACHE_DATA(line).end.index - text->point.index + 1;
2694 chars_left -= CACHE_DATA(line).end.index - CACHE_DATA(line).start.index + 1;
2696 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2700 chars_left = 0; /* force another loop */
2703 fetch_lines_forward (text, 1);
2708 total_line_height (GtkSText* text, GList* line, gint line_count)
2712 for (; line && line_count > 0; line = line->next)
2714 height += LINE_HEIGHT(CACHE_DATA(line));
2716 if (!text->line_wrap || !CACHE_DATA(line).wraps)
2720 fetch_lines_forward (text, line_count);
2727 swap_lines (GtkSText* text, GList* old, GList* new, guint old_line_count)
2729 if (old == text->line_start_cache)
2733 for (; old_line_count > 0; old_line_count -= 1)
2735 while (text->line_start_cache &&
2737 CACHE_DATA(text->line_start_cache).wraps)
2738 remove_cache_line(text, text->line_start_cache);
2740 remove_cache_line(text, text->line_start_cache);
2743 last = g_list_last (new);
2745 last->next = text->line_start_cache;
2747 if (text->line_start_cache)
2748 text->line_start_cache->prev = last;
2750 text->line_start_cache = new;
2756 g_assert (old->prev);
2760 for (; old_line_count > 0; old_line_count -= 1)
2762 while (old && text->line_wrap && CACHE_DATA(old).wraps)
2763 old = remove_cache_line (text, old);
2765 old = remove_cache_line (text, old);
2771 last = g_list_last (new);
2781 correct_cache_delete (GtkSText* text, gint nchars, gint lines)
2783 GList* cache = text->current_line;
2786 for (i = 0; cache && i < lines; i += 1, cache = cache->next)
2789 for (; cache; cache = cache->next)
2791 GtkSPropertyMark *start = &CACHE_DATA(cache).start;
2792 GtkSPropertyMark *end = &CACHE_DATA(cache).end;
2794 start->index -= nchars;
2795 end->index -= nchars;
2797 if (LAST_INDEX (text, text->point) &&
2798 start->index == text->point.index)
2799 *start = text->point;
2800 else if (start->property == text->point.property)
2801 start->offset = start->index - (text->point.index - text->point.offset);
2803 if (LAST_INDEX (text, text->point) &&
2804 end->index == text->point.index)
2806 if (end->property == text->point.property)
2807 end->offset = end->index - (text->point.index - text->point.offset);
2809 /*TEXT_ASSERT_MARK(text, start, "start");*/
2810 /*TEXT_ASSERT_MARK(text, end, "end");*/
2815 delete_expose (GtkSText* text, guint nchars, guint old_lines, guint old_pixels)
2817 GtkWidget *widget = GTK_WIDGET (text);
2820 guint new_pixels = 0;
2822 GList* new_line = NULL;
2825 text->cursor_virtual_x = 0;
2827 correct_cache_delete (text, nchars, old_lines);
2829 pixel_height = pixel_height_of(text, text->current_line) -
2830 LINE_HEIGHT(CACHE_DATA(text->current_line));
2832 if (CACHE_DATA(text->current_line).start.index == text->point.index)
2833 CACHE_DATA(text->current_line).start = text->point;
2835 new_line = fetch_lines (text,
2836 &CACHE_DATA(text->current_line).start,
2837 &CACHE_DATA(text->current_line).tab_cont,
2841 swap_lines (text, text->current_line, new_line, old_lines);
2843 text->current_line = new_line;
2845 new_pixels = total_line_height (text, new_line, 1);
2847 gdk_window_get_size (text->text_area, &width, &height);
2849 if (old_pixels != new_pixels)
2851 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
2853 gdk_draw_pixmap (text->text_area,
2857 pixel_height + old_pixels,
2859 pixel_height + new_pixels,
2863 text->vadj->upper += new_pixels;
2864 text->vadj->upper -= old_pixels;
2865 adjust_adj (text, text->vadj);
2869 rect.y = pixel_height;
2871 rect.height = new_pixels;
2873 expose_text (text, &rect, FALSE);
2874 gtk_stext_draw_focus ( (GtkWidget *) text);
2876 text->cursor_mark = text->point;
2878 find_cursor (text, TRUE);
2880 if (old_pixels != new_pixels)
2882 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
2885 rect.y = pixel_height + new_pixels;
2887 rect.height = height - rect.y;
2889 expose_text (text, &rect, FALSE);
2892 process_exposes (text);
2899 /* note, the point has already been moved forward */
2901 correct_cache_insert (GtkSText* text, gint nchars)
2904 GtkSPropertyMark *start;
2905 GtkSPropertyMark *end;
2907 /* If we inserted a property exactly at the beginning of the
2908 * line, we have to correct here, or fetch_lines will
2911 start = &CACHE_DATA(text->current_line).start;
2912 if (start->index == text->point.index - nchars)
2914 *start = text->point;
2915 move_mark_n (start, -nchars);
2918 /* Now correct the offsets, and check for start or end marks that
2919 * are after the point, yet point to a property before the point's
2920 * property. This indicates that they are meant to point to the
2921 * second half of a property we split in insert_text_property(), so
2922 * we fix them up that way.
2924 cache = text->current_line->next;
2926 for (; cache; cache = cache->next)
2928 start = &CACHE_DATA(cache).start;
2929 end = &CACHE_DATA(cache).end;
2931 if (LAST_INDEX (text, text->point) &&
2932 start->index == text->point.index)
2933 *start = text->point;
2936 if (start->property == text->point.property)
2938 start->offset += nchars;
2939 start->index += nchars;
2941 else if (start->property->next &&
2942 (start->property->next->next == text->point.property))
2944 /* We split the property, and this is the second half */
2945 start->offset -= MARK_CURRENT_PROPERTY (start)->length;
2946 start->index += nchars;
2947 start->property = text->point.property;
2950 start->index += nchars;
2953 if (LAST_INDEX (text, text->point) &&
2954 end->index == text->point.index)
2958 if (end->property == text->point.property)
2960 end->offset += nchars;
2961 end->index += nchars;
2963 else if (end->property->next &&
2964 (end->property->next->next == text->point.property))
2966 /* We split the property, and this is the second half */
2967 end->offset -= MARK_CURRENT_PROPERTY (end)->length;
2968 end->index += nchars;
2969 end->property = text->point.property;
2972 end->index += nchars;
2975 /*TEXT_ASSERT_MARK(text, start, "start");*/
2976 /*TEXT_ASSERT_MARK(text, end, "end");*/
2982 insert_expose (GtkSText* text, guint old_pixels, gint nchars,
2983 guint new_line_count)
2985 GtkWidget *widget = GTK_WIDGET (text);
2988 guint new_pixels = 0;
2990 GList* new_lines = NULL;
2993 text->cursor_virtual_x = 0;
2995 undraw_cursor (text, FALSE);
2997 correct_cache_insert (text, nchars);
2999 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3001 pixel_height = pixel_height_of(text, text->current_line) -
3002 LINE_HEIGHT(CACHE_DATA(text->current_line));
3004 new_lines = fetch_lines (text,
3005 &CACHE_DATA(text->current_line).start,
3006 &CACHE_DATA(text->current_line).tab_cont,
3010 swap_lines (text, text->current_line, new_lines, 1);
3012 text->current_line = new_lines;
3014 new_pixels = total_line_height (text, new_lines, new_line_count);
3016 gdk_window_get_size (text->text_area, &width, &height);
3018 if (old_pixels != new_pixels)
3020 if (!widget->style->bg_pixmap[GTK_STATE_NORMAL])
3022 gdk_draw_pixmap (text->text_area,
3026 pixel_height + old_pixels,
3028 pixel_height + new_pixels,
3030 height + (old_pixels - new_pixels) - pixel_height);
3033 text->vadj->upper += new_pixels;
3034 text->vadj->upper -= old_pixels;
3035 adjust_adj (text, text->vadj);
3039 rect.y = pixel_height;
3041 rect.height = new_pixels;
3043 expose_text (text, &rect, FALSE);
3044 gtk_stext_draw_focus ( (GtkWidget *) text);
3046 text->cursor_mark = text->point;
3048 find_cursor (text, TRUE);
3050 draw_cursor (text, FALSE);
3052 if (old_pixels != new_pixels)
3054 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
3057 rect.y = pixel_height + new_pixels;
3059 rect.height = height - rect.y;
3061 expose_text (text, &rect, FALSE);
3064 process_exposes (text);
3067 TEXT_SHOW_ADJ (text, text->vadj, "vadj");
3072 /* Text property functions */
3075 font_hash (gconstpointer font)
3077 return gdk_font_id ((const GdkFont*) font);
3080 static GHashTable *font_cache_table = NULL;
3082 static GtkSTextFont*
3083 get_text_font (GdkFont* gfont)
3088 if (!font_cache_table)
3089 font_cache_table = g_hash_table_new (font_hash, (GCompareFunc) gdk_font_equal);
3091 tf = g_hash_table_lookup (font_cache_table, gfont);
3099 tf = g_new (GtkSTextFont, 1);
3102 tf->gdk_font = gfont;
3103 gdk_font_ref (gfont);
3105 for(i = 0; i < 256; i += 1)
3106 tf->char_widths[i] = gdk_char_width (gfont, (char)i);
3108 g_hash_table_insert (font_cache_table, gfont, tf);
3114 text_font_unref (GtkSTextFont *text_font)
3116 text_font->ref_count--;
3117 if (text_font->ref_count == 0)
3119 g_hash_table_remove (font_cache_table, text_font->gdk_font);
3120 gdk_font_unref (text_font->gdk_font);
3126 text_properties_equal (TextProperty* prop, GdkFont* font, GdkColor *fore, GdkColor *back)
3128 if (prop->flags & PROPERTY_FONT)
3131 GtkSTextFont *text_font;
3136 text_font = get_text_font (font);
3138 retval = (prop->font == text_font);
3139 text_font_unref (text_font);
3148 if (prop->flags & PROPERTY_FOREGROUND)
3150 if (!fore || !gdk_color_equal (&prop->fore_color, fore))
3157 if (prop->flags & PROPERTY_BACKGROUND)
3159 if (!back || !gdk_color_equal (&prop->back_color, back))
3170 realize_property (GtkSText *text, TextProperty *prop)
3172 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3174 if (prop->flags & PROPERTY_FOREGROUND)
3175 gdk_colormap_alloc_color (colormap, &prop->fore_color, FALSE, FALSE);
3177 if (prop->flags & PROPERTY_BACKGROUND)
3178 gdk_colormap_alloc_color (colormap, &prop->back_color, FALSE, FALSE);
3182 realize_properties (GtkSText *text)
3184 GList *tmp_list = text->text_properties;
3188 realize_property (text, tmp_list->data);
3190 tmp_list = tmp_list->next;
3195 unrealize_property (GtkSText *text, TextProperty *prop)
3197 GdkColormap *colormap = gtk_widget_get_colormap (GTK_WIDGET (text));
3199 if (prop->flags & PROPERTY_FOREGROUND)
3200 gdk_colormap_free_colors (colormap, &prop->fore_color, 1);
3202 if (prop->flags & PROPERTY_BACKGROUND)
3203 gdk_colormap_free_colors (colormap, &prop->back_color, 1);
3207 unrealize_properties (GtkSText *text)
3209 GList *tmp_list = text->text_properties;
3213 unrealize_property (text, tmp_list->data);
3215 tmp_list = tmp_list->next;
3219 static TextProperty*
3220 new_text_property (GtkSText *text, GdkFont *font, GdkColor* fore,
3221 GdkColor* back, guint length)
3225 if (text_property_chunk == NULL)
3227 text_property_chunk = g_mem_chunk_new ("text property mem chunk",
3228 sizeof(TextProperty),
3229 1024*sizeof(TextProperty),
3233 prop = g_chunk_new(TextProperty, text_property_chunk);
3238 prop->flags |= PROPERTY_FONT;
3239 prop->font = get_text_font (font);
3246 prop->flags |= PROPERTY_FOREGROUND;
3247 prop->fore_color = *fore;
3252 prop->flags |= PROPERTY_BACKGROUND;
3253 prop->back_color = *back;
3256 prop->length = length;
3258 if (GTK_WIDGET_REALIZED (text))
3259 realize_property (text, prop);
3265 destroy_text_property (TextProperty *prop)
3268 text_font_unref (prop->font);
3270 g_mem_chunk_free (text_property_chunk, prop);
3273 /* Flop the memory between the point and the gap around like a
3276 move_gap (GtkSText* text, guint index)
3278 if (text->gap_position < index)
3280 gint diff = index - text->gap_position;
3282 if (text->use_wchar)
3283 g_memmove (text->text.wc + text->gap_position,
3284 text->text.wc + text->gap_position + text->gap_size,
3285 diff*sizeof (GdkWChar));
3287 g_memmove (text->text.ch + text->gap_position,
3288 text->text.ch + text->gap_position + text->gap_size,
3291 text->gap_position = index;
3293 else if (text->gap_position > index)
3295 gint diff = text->gap_position - index;
3297 if (text->use_wchar)
3298 g_memmove (text->text.wc + index + text->gap_size,
3299 text->text.wc + index,
3300 diff*sizeof (GdkWChar));
3302 g_memmove (text->text.ch + index + text->gap_size,
3303 text->text.ch + index,
3306 text->gap_position = index;
3310 /* Increase the gap size. */
3312 make_forward_space (GtkSText* text, guint len)
3314 if (text->gap_size < len)
3316 guint sum = MAX(2*len, MIN_GAP_SIZE) + text->text_end;
3318 if (sum >= text->text_len)
3322 while (i <= sum) i <<= 1;
3324 if (text->use_wchar)
3325 text->text.wc = (GdkWChar *)g_realloc(text->text.wc,
3326 i*sizeof(GdkWChar));
3328 text->text.ch = (guchar *)g_realloc(text->text.ch, i);
3332 if (text->use_wchar)
3333 g_memmove (text->text.wc + text->gap_position + text->gap_size + 2*len,
3334 text->text.wc + text->gap_position + text->gap_size,
3335 (text->text_end - (text->gap_position + text->gap_size))
3338 g_memmove (text->text.ch + text->gap_position + text->gap_size + 2*len,
3339 text->text.ch + text->gap_position + text->gap_size,
3340 text->text_end - (text->gap_position + text->gap_size));
3342 text->text_end += len*2;
3343 text->gap_size += len*2;
3347 /* Inserts into the text property list a list element that guarantees
3348 * that for len characters following the point, text has the correct
3349 * property. does not move point. adjusts text_properties_point and
3350 * text_properties_point_offset relative to the current value of
3353 insert_text_property (GtkSText* text, GdkFont* font,
3354 GdkColor *fore, GdkColor* back, guint len)
3356 GtkSPropertyMark *mark = &text->point;
3357 TextProperty* forward_prop = MARK_CURRENT_PROPERTY(mark);
3358 TextProperty* backward_prop = MARK_PREV_PROPERTY(mark);
3360 if (MARK_OFFSET(mark) == 0)
3362 /* Point is on the boundary of two properties.
3363 * If it is the same as either, grow, else insert
3366 if (text_properties_equal(forward_prop, font, fore, back))
3368 /* Grow the property in front of us. */
3370 MARK_PROPERTY_LENGTH(mark) += len;
3372 else if (backward_prop &&
3373 text_properties_equal(backward_prop, font, fore, back))
3375 /* Grow property behind us, point property and offset
3378 SET_PROPERTY_MARK (&text->point,
3379 MARK_PREV_LIST_PTR (mark),
3380 backward_prop->length);
3382 backward_prop->length += len;
3384 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3385 (forward_prop->length == 1))
3387 /* Next property just has last position, take it over */
3389 if (GTK_WIDGET_REALIZED (text))
3390 unrealize_property (text, forward_prop);
3392 forward_prop->flags = 0;
3395 forward_prop->flags |= PROPERTY_FONT;
3396 forward_prop->font = get_text_font (font);
3399 forward_prop->font = NULL;
3403 forward_prop->flags |= PROPERTY_FOREGROUND;
3404 forward_prop->fore_color = *fore;
3408 forward_prop->flags |= PROPERTY_BACKGROUND;
3409 forward_prop->back_color = *back;
3411 forward_prop->length += len;
3413 if (GTK_WIDGET_REALIZED (text))
3414 realize_property (text, forward_prop);
3418 /* Splice a new property into the list. */
3420 GList* new_prop = g_list_alloc();
3422 new_prop->next = MARK_LIST_PTR(mark);
3423 new_prop->prev = MARK_PREV_LIST_PTR(mark);
3424 new_prop->next->prev = new_prop;
3427 new_prop->prev->next = new_prop;
3429 new_prop->data = new_text_property (text, font, fore, back, len);
3431 SET_PROPERTY_MARK (mark, new_prop, 0);
3436 /* The following will screw up the line_start cache,
3437 * we'll fix it up in correct_cache_insert
3440 /* In the middle of forward_prop, if properties are equal,
3441 * just add to its length, else split it into two and splice
3443 if (text_properties_equal (forward_prop, font, fore, back))
3445 forward_prop->length += len;
3447 else if ((MARK_NEXT_LIST_PTR(mark) == NULL) &&
3448 (MARK_OFFSET(mark) + 1 == forward_prop->length))
3450 /* Inserting before only the last position in the text */
3453 forward_prop->length -= 1;
3455 new_prop = g_list_alloc();
3456 new_prop->data = new_text_property (text, font, fore, back, len+1);
3457 new_prop->prev = MARK_LIST_PTR(mark);
3458 new_prop->next = NULL;
3459 MARK_NEXT_LIST_PTR(mark) = new_prop;
3461 SET_PROPERTY_MARK (mark, new_prop, 0);
3465 GList* new_prop = g_list_alloc();
3466 GList* new_prop_forward = g_list_alloc();
3467 gint old_length = forward_prop->length;
3468 GList* next = MARK_NEXT_LIST_PTR(mark);
3470 /* Set the new lengths according to where they are split. Construct
3471 * two new properties. */
3472 forward_prop->length = MARK_OFFSET(mark);
3474 new_prop_forward->data =
3475 new_text_property(text,
3476 forward_prop->flags & PROPERTY_FONT ?
3477 forward_prop->font->gdk_font : NULL,
3478 forward_prop->flags & PROPERTY_FOREGROUND ?
3479 &forward_prop->fore_color : NULL,
3480 forward_prop->flags & PROPERTY_BACKGROUND ?
3481 &forward_prop->back_color : NULL,
3482 old_length - forward_prop->length);
3484 new_prop->data = new_text_property(text, font, fore, back, len);
3486 /* Now splice things in. */
3487 MARK_NEXT_LIST_PTR(mark) = new_prop;
3488 new_prop->prev = MARK_LIST_PTR(mark);
3490 new_prop->next = new_prop_forward;
3491 new_prop_forward->prev = new_prop;
3493 new_prop_forward->next = next;
3496 next->prev = new_prop_forward;
3498 SET_PROPERTY_MARK (mark, new_prop, 0);
3502 while (text->text_properties_end->next)
3503 text->text_properties_end = text->text_properties_end->next;
3505 while (text->text_properties->prev)
3506 text->text_properties = text->text_properties->prev;
3510 delete_text_property (GtkSText* text, guint nchars)
3512 /* Delete nchars forward from point. */
3514 /* Deleting text properties is problematical, because we
3515 * might be storing around marks pointing to a property.
3517 * The marks in question and how we handle them are:
3519 * point: We know the new value, since it will be at the
3520 * end of the deleted text, and we move it there
3522 * cursor: We just remove the mark and set it equal to the
3523 * point after the operation.
3524 * line-start cache: We replace most affected lines.
3525 * The current line gets used to fetch the new
3526 * lines so, if necessary, (delete at the beginning
3527 * of a line) we fix it up by setting it equal to the
3535 for(; nchars; nchars -= 1)
3537 prop = MARK_CURRENT_PROPERTY(&text->point);
3541 if (prop->length == 0)
3543 tmp = MARK_LIST_PTR (&text->point);
3545 is_first = tmp == text->text_properties;
3547 MARK_LIST_PTR (&text->point) = g_list_remove_link (tmp, tmp);
3548 text->point.offset = 0;
3550 if (GTK_WIDGET_REALIZED (text))
3551 unrealize_property (text, prop);
3553 destroy_text_property (prop);
3554 g_list_free_1 (tmp);
3556 prop = MARK_CURRENT_PROPERTY (&text->point);
3559 text->text_properties = MARK_LIST_PTR (&text->point);
3561 g_assert (prop->length != 0);
3563 else if (prop->length == text->point.offset)
3565 MARK_LIST_PTR (&text->point) = MARK_NEXT_LIST_PTR (&text->point);
3566 text->point.offset = 0;
3570 /* Check to see if we have just the single final position remaining
3571 * along in a property; if so, combine it with the previous property
3573 if (LAST_INDEX (text, text->point) &&
3574 (MARK_OFFSET (&text->point) == 0) &&
3575 (MARK_PREV_LIST_PTR(&text->point) != NULL))
3577 tmp = MARK_LIST_PTR (&text->point);
3578 prop = MARK_CURRENT_PROPERTY(&text->point);
3580 MARK_LIST_PTR (&text->point) = MARK_PREV_LIST_PTR (&text->point);
3581 MARK_CURRENT_PROPERTY(&text->point)->length += 1;
3582 MARK_NEXT_LIST_PTR(&text->point) = NULL;
3584 text->point.offset = MARK_CURRENT_PROPERTY(&text->point)->length - 1;
3586 if (GTK_WIDGET_REALIZED (text))
3587 unrealize_property (text, prop);
3589 destroy_text_property (prop);
3590 g_list_free_1 (tmp);
3595 init_properties (GtkSText *text)
3597 if (!text->text_properties)
3599 text->text_properties = g_list_alloc();
3600 text->text_properties->next = NULL;
3601 text->text_properties->prev = NULL;
3602 text->text_properties->data = new_text_property (text, NULL, NULL, NULL, 1);
3603 text->text_properties_end = text->text_properties;
3605 SET_PROPERTY_MARK (&text->point, text->text_properties, 0);
3607 text->point.index = 0;
3612 /**********************************************************************/
3613 /* Property Movement */
3614 /**********************************************************************/
3617 move_mark_n (GtkSPropertyMark* mark, gint n)
3620 advance_mark_n(mark, n);
3622 decrement_mark_n(mark, -n);
3626 advance_mark (GtkSPropertyMark* mark)
3628 TextProperty* prop = MARK_CURRENT_PROPERTY (mark);
3632 if (prop->length > mark->offset + 1)
3636 mark->property = MARK_NEXT_LIST_PTR (mark);
3642 advance_mark_n (GtkSPropertyMark* mark, gint n)
3649 i = 0; /* otherwise it migth not be init. */
3650 prop = MARK_CURRENT_PROPERTY(mark);
3652 if ((prop->length - mark->offset - 1) < n) { /* if we need to change prop. */
3653 /* to make it easier */
3654 n += (mark->offset);
3655 mark->index -= mark->offset;
3657 /* first we take seven-mile-leaps to get to the right text
3659 while ((n-i) > prop->length - 1) {
3661 mark->index += prop->length;
3662 mark->property = MARK_NEXT_LIST_PTR (mark);
3663 prop = MARK_CURRENT_PROPERTY (mark);
3667 /* and then the rest */
3668 mark->index += n - i;
3669 mark->offset += n - i;
3673 decrement_mark (GtkSPropertyMark* mark)
3677 if (mark->offset > 0)
3681 mark->property = MARK_PREV_LIST_PTR (mark);
3682 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3687 decrement_mark_n (GtkSPropertyMark* mark, gint n)
3691 while (mark->offset < n) {
3692 /* jump to end of prev */
3693 n -= mark->offset + 1;
3694 mark->index -= mark->offset + 1;
3695 mark->property = MARK_PREV_LIST_PTR (mark);
3696 mark->offset = MARK_CURRENT_PROPERTY (mark)->length - 1;
3704 static GtkSPropertyMark
3705 find_mark (GtkSText* text, guint mark_position)
3707 return find_mark_near (text, mark_position, &text->point);
3711 * You can also start from the end, what a drag.
3713 static GtkSPropertyMark
3714 find_mark_near (GtkSText* text, guint mark_position, const GtkSPropertyMark* near)
3719 GtkSPropertyMark mark;
3722 diffa = mark_position + 1;
3724 diffa = mark_position - near->index;
3726 diffb = mark_position;
3738 mark.property = text->text_properties;
3742 move_mark_n (&mark, mark_position - mark.index);
3747 /* This routine must be called with scroll == FALSE, only when
3748 * point is at least partially on screen
3752 find_line_containing_point (GtkSText* text, guint point,
3758 text->current_line = NULL;
3762 /* Scroll backwards until the point is on screen
3764 while (CACHE_DATA(text->line_start_cache).start.index > point)
3765 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache)));
3767 /* Now additionally try to make sure that the point is fully on screen
3771 while (text->first_cut_pixels != 0 &&
3772 text->line_start_cache->next &&
3773 CACHE_DATA(text->line_start_cache->next).start.index > point)
3774 scroll_int (text, - LINE_HEIGHT(CACHE_DATA(text->line_start_cache->next)));
3777 gdk_window_get_size (text->text_area, NULL, &height);
3779 for (cache = text->line_start_cache; cache; cache = cache->next)
3783 if (CACHE_DATA(cache).end.index >= point ||
3784 LAST_INDEX(text, CACHE_DATA(cache).end))
3786 text->current_line = cache; /* LOOK HERE, this proc has an
3787 * important side effect. */
3791 TEXT_SHOW_LINE (text, cache, "cache");
3793 if (cache->next == NULL)
3794 fetch_lines_forward (text, 1);
3798 lph = pixel_height_of (text, cache->next);
3800 /* Scroll the bottom of the line is on screen, or until
3801 * the line is the first onscreen line.
3803 while (cache->next != text->line_start_cache && lph > height)
3805 TEXT_SHOW_LINE (text, cache, "cache");
3806 TEXT_SHOW_LINE (text, cache->next, "cache->next");
3807 scroll_int (text, LINE_HEIGHT(CACHE_DATA(cache->next)));
3808 lph = pixel_height_of (text, cache->next);
3813 g_assert_not_reached (); /* Must set text->current_line here */
3817 pixel_height_of (GtkSText* text, GList* cache_line)
3819 gint pixels = - text->first_cut_pixels;
3820 GList *cache = text->line_start_cache;
3823 pixels += LINE_HEIGHT (CACHE_DATA(cache));
3825 if (cache->data == cache_line->data)
3828 cache = cache->next;
3834 /**********************************************************************/
3835 /* Search and Placement */
3836 /**********************************************************************/
3839 find_char_width (GtkSText* text, const GtkSPropertyMark *mark, const TabStopMark *tab_mark)
3842 gint16* char_widths;
3844 if (LAST_INDEX (text, *mark))
3847 ch = GTK_STEXT_INDEX (text, mark->index);
3848 char_widths = MARK_CURRENT_TEXT_FONT (text, mark)->char_widths;
3852 return tab_mark->to_next_tab * char_widths[' '];
3854 else if (!text->use_wchar)
3856 return char_widths[ch];
3860 return gdk_char_width_wc(MARK_CURRENT_TEXT_FONT(text, mark)->gdk_font, ch);
3865 advance_tab_mark (GtkSText* text, TabStopMark* tab_mark, GdkWChar ch)
3867 if (tab_mark->to_next_tab == 1 || ch == '\t')
3869 if (tab_mark->tab_stops->next)
3871 tab_mark->tab_stops = tab_mark->tab_stops->next;
3872 tab_mark->to_next_tab = (gulong) tab_mark->tab_stops->data;
3876 tab_mark->to_next_tab = text->default_tab_width;
3881 tab_mark->to_next_tab -= 1;
3886 advance_tab_mark_n (GtkSText* text, TabStopMark* tab_mark, gint n)
3890 advance_tab_mark (text, tab_mark, 0);
3894 find_cursor_at_line (GtkSText* text, const LineParams* start_line, gint pixel_height)
3897 GtkEditable *editable = (GtkEditable *)text;
3899 GtkSPropertyMark mark = start_line->start;
3900 TabStopMark tab_mark = start_line->tab_cont.tab_start;
3901 gint pixel_width = LINE_START_PIXEL (*start_line);
3903 while (mark.index < text->cursor_mark.index)
3905 pixel_width += find_char_width (text, &mark, &tab_mark);
3907 advance_tab_mark (text, &tab_mark, GTK_STEXT_INDEX(text, mark.index));
3908 advance_mark (&mark);
3911 text->cursor_pos_x = pixel_width;
3912 text->cursor_pos_y = pixel_height;
3913 text->cursor_char_offset = start_line->font_descent;
3914 text->cursor_mark = mark;
3916 ch = LAST_INDEX (text, mark) ?
3917 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
3919 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3920 text->cursor_char = 0;
3922 text->cursor_char = ch;
3924 #ifdef USE_GTKGDK_XIM
3925 if (GTK_WIDGET_HAS_FOCUS(text) && gdk_im_ready() && editable->ic &&
3926 (gdk_ic_get_style (editable->ic) & GDK_IM_PREEDIT_POSITION))
3928 GdkICAttributesType mask = GDK_IC_SPOT_LOCATION |
3929 GDK_IC_PREEDIT_FOREGROUND |
3930 GDK_IC_PREEDIT_BACKGROUND;
3932 editable->ic_attr->spot_location.x = text->cursor_pos_x;
3933 editable->ic_attr->spot_location.y
3934 = text->cursor_pos_y - text->cursor_char_offset;
3935 editable->ic_attr->preedit_foreground = *MARK_CURRENT_FORE (text, &mark);
3936 editable->ic_attr->preedit_background = *MARK_CURRENT_BACK (text, &mark);
3938 if (MARK_CURRENT_FONT (text, &mark)->type == GDK_FONT_FONTSET)
3940 mask |= GDK_IC_PREEDIT_FONTSET;
3941 editable->ic_attr->preedit_fontset = MARK_CURRENT_FONT (text, &mark);
3944 gdk_ic_set_attr (editable->ic, editable->ic_attr, mask);
3950 find_cursor (GtkSText* text, gboolean scroll)
3952 if (GTK_WIDGET_REALIZED (text))
3954 find_line_containing_point (text, text->cursor_mark.index, scroll);
3956 if (text->current_line)
3957 find_cursor_at_line (text,
3958 &CACHE_DATA(text->current_line),
3959 pixel_height_of(text, text->current_line));
3962 GTK_EDITABLE (text)->current_pos = text->cursor_mark.index;
3966 find_mouse_cursor_at_line (GtkSText *text, const LineParams* lp,
3967 guint line_pixel_height,
3970 GtkSPropertyMark mark = lp->start;
3971 TabStopMark tab_mark = lp->tab_cont.tab_start;
3973 gint char_width = find_char_width(text, &mark, &tab_mark);
3974 gint pixel_width = LINE_START_PIXEL (*lp) + (char_width+1)/2;
3976 text->cursor_pos_y = line_pixel_height;
3980 GdkWChar ch = LAST_INDEX (text, mark) ?
3981 LINE_DELIM : GTK_STEXT_INDEX (text, mark.index);
3983 if (button_x < pixel_width || mark.index == lp->end.index)
3985 text->cursor_pos_x = pixel_width - (char_width+1)/2;
3986 text->cursor_mark = mark;
3987 text->cursor_char_offset = lp->font_descent;
3989 if ((text->use_wchar) ? gdk_iswspace (ch) : isspace (ch))
3990 text->cursor_char = 0;
3992 text->cursor_char = ch;
3997 advance_tab_mark (text, &tab_mark, ch);
3998 advance_mark (&mark);
4000 pixel_width += char_width/2;
4002 char_width = find_char_width (text, &mark, &tab_mark);
4004 pixel_width += (char_width+1)/2;
4009 find_mouse_cursor (GtkSText* text, gint x, gint y)
4012 GList* cache = text->line_start_cache;
4016 pixel_height = - text->first_cut_pixels;
4018 for (; cache; cache = cache->next)
4020 pixel_height += LINE_HEIGHT(CACHE_DATA(cache));
4022 if (y < pixel_height || !cache->next)
4024 find_mouse_cursor_at_line (text, &CACHE_DATA(cache), pixel_height, x);
4026 find_cursor (text, FALSE);
4033 /**********************************************************************/
4035 /**********************************************************************/
4038 free_cache (GtkSText* text)
4040 GList* cache = text->line_start_cache;
4045 cache = cache->prev;
4047 text->line_start_cache = cache;
4050 for (; cache; cache = cache->next)
4051 g_mem_chunk_free (params_mem_chunk, cache->data);
4053 g_list_free (text->line_start_cache);
4055 text->line_start_cache = NULL;
4059 remove_cache_line (GtkSText* text, GList* member)
4066 if (member == text->line_start_cache)
4067 text->line_start_cache = text->line_start_cache->next;
4070 member->prev->next = member->next;
4073 member->next->prev = member->prev;
4075 list = member->next;
4077 g_mem_chunk_free (params_mem_chunk, member->data);
4078 g_list_free_1 (member);
4083 /**********************************************************************/
4085 /**********************************************************************/
4088 move_cursor_buffer_ver (GtkSText *text, int dir)
4090 undraw_cursor (text, FALSE);
4094 scroll_int (text, text->vadj->upper);
4095 text->cursor_mark = find_this_line_start_mark (text,
4097 &text->cursor_mark);
4101 scroll_int (text, - text->vadj->value);
4102 text->cursor_mark = find_this_line_start_mark (text,
4104 &text->cursor_mark);
4107 find_cursor (text, TRUE);
4108 draw_cursor (text, FALSE);
4112 move_cursor_page_ver (GtkSText *text, int dir)
4114 scroll_int (text, dir * text->vadj->page_increment);
4118 move_cursor_ver (GtkSText *text, int count)
4121 GtkSPropertyMark mark;
4124 mark = find_this_line_start_mark (text, text->cursor_mark.index, &text->cursor_mark);
4125 offset = text->cursor_mark.index - mark.index;
4127 if (offset > text->cursor_virtual_x)
4128 text->cursor_virtual_x = offset;
4132 if (mark.index == 0)
4135 decrement_mark (&mark);
4136 mark = find_this_line_start_mark (text, mark.index, &mark);
4140 mark = text->cursor_mark;
4142 while (!LAST_INDEX(text, mark) && GTK_STEXT_INDEX(text, mark.index) != LINE_DELIM)
4143 advance_mark (&mark);
4145 if (LAST_INDEX(text, mark))
4148 advance_mark (&mark);
4151 for (i=0; i < text->cursor_virtual_x; i += 1, advance_mark(&mark))
4152 if (LAST_INDEX(text, mark) ||
4153 GTK_STEXT_INDEX(text, mark.index) == LINE_DELIM)
4156 undraw_cursor (text, FALSE);
4158 text->cursor_mark = mark;
4160 find_cursor (text, TRUE);
4162 draw_cursor (text, FALSE);
4166 move_cursor_hor (GtkSText *text, int count)
4168 /* count should be +-1. */
4169 if ( (count > 0 && text->cursor_mark.index + count > TEXT_LENGTH(text)) ||
4170 (count < 0 && text->cursor_mark.index < (- count)) ||
4174 text->cursor_virtual_x = 0;
4176 undraw_cursor (text, FALSE);
4178 move_mark_n (&text->cursor_mark, count);
4180 find_cursor (text, TRUE);
4182 draw_cursor (text, FALSE);
4185 /* SYLPHEED - the default cursor movement of GtkText is aeons ahead of the
4186 * current technology level of the current generation of programmers and
4187 * end users. so sylpheed has to take a more mundane approach to editing.
4190 * move_cursor_to_display_row_end() (end of display line)
4191 * move_cursor_to_display_row_home() (home of display line)
4192 * move_cursor_to_display_row_up() (line up)
4193 * move_cursor_to_display_row_down() (line down)
4197 * SYLPHEED_TODO: for some reason the line fetcher also returns markers
4198 * of just one character! should investigate this... -- alfons
4201 static void move_cursor_to_display_row_end(GtkSText *text)
4203 LineParams lp = CACHE_DATA(text->current_line);
4204 int to_move = (lp.end.index - text->cursor_mark.index);
4206 /* advance this much */
4208 move_cursor_hor(text, to_move);
4212 static void move_cursor_to_display_row_start(GtkSText *text)
4214 LineParams lp = CACHE_DATA(text->current_line);
4215 int to_move = (text->cursor_mark.index - lp.start.index);
4217 move_cursor_hor(text, -to_move);
4222 static gboolean range_intersect(gint x1, gint x2, gint y1, gint y2)
4225 if (x1 > x2) { tmp = x1; x1 = x2; x2 = tmp; }
4226 if (y1 > y2) { tmp = y1; y1 = y2; y1 = tmp; }
4227 if (y1 < x1) { tmp = x1; x1 = y1; y1 = tmp; tmp = x2; x2 = y2; y2 = tmp; }
4237 static gint back_display_row_fetcher(GtkSText *text,
4241 if (range_intersect(data->start, data->end, params->start.index, params->end.index)) {
4242 XDEBUG( ("%s(%d) - FOUND search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4243 data->start, data->end,
4244 params->start.index, params->end.index) );
4249 XDEBUG( ("%s(%d) - NEXT search (%d, %d), current (%d, %d)", __FILE__, __LINE__,
4250 data->start, data->end,
4251 params->start.index, params->end.index) );
4257 static void move_cursor_to_display_row_up(GtkSText *text)
4263 GtkSPropertyMark mark;
4265 /* make sure the back display lines are in the cache */
4266 fetch_lines_backward(text);
4268 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4271 if (mark.index == 0) {
4272 XDEBUG ( ("%s(%d) top of buffer", __FILE__, __LINE__) );
4276 /* we need to previous DISPLAY row not the previous BUFFER line, so we go one line back,
4277 * and iterate over the lines until we have a LineParams that matches the original */
4279 lp = CACHE_DATA(text->current_line);
4280 col = (text->cursor_mark.index - lp.start.index);
4281 data.start = lp.start.index;
4282 data.end = lp.end.index;
4284 /* get the previous line */
4285 if (mark.index - 1 > 0) {
4286 decrement_mark(&mark);
4287 XDEBUG( ("%s(%d) mark decrement", __FILE__, __LINE__) );
4288 if (mark.index - 1 > 0) {
4289 GtkSPropertyMark smark = mark;
4290 XDEBUG( ("%s(%d) finding line start mark", __FILE__, __LINE__) );
4291 mark = find_this_line_start_mark(text, smark.index -1, &smark);
4295 /* let's get the previous display line */
4296 XDEBUG( ("%s(%d) iterating to get display lines", __FILE__, __LINE__) );
4297 line_params_iterate(text, &mark, NULL, FALSE, &data,
4298 (LineIteratorFunction)back_display_row_fetcher);
4299 XDEBUG( ("%s(%d) done iterating. found = %d", __FILE__, __LINE__, data.found) );
4302 if (col < text->persist_column) col = text->persist_column;
4303 else text->persist_column = col;
4304 new_index = data.lp.start.index + col;
4305 XDEBUG( ("%s(%d) - new index = %d", __FILE__, __LINE__, new_index) );
4306 if (new_index > data.lp.end.index) {
4307 new_index = data.lp.end.index;
4309 /* and move the cursor */
4310 XDEBUG( ("%s(%d) - setting index", __FILE__, __LINE__) );
4311 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4322 static gint forward_display_row_fetcher(GtkSText *text,
4327 XDEBUG( ("%s(%d) - FW RETURNS. search (%d, %d), current (%d, %d)",
4328 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4330 data->completed = TRUE;
4333 else if (range_intersect(data->start, data->end, lp->start.index, lp->end.index)) {
4334 XDEBUG( ("%s(%d) - FW FOUND IT. search (%d, %d), current (%d, %d)",
4335 __FILE__, __LINE__, data->start, data->end, lp->start.index, lp->end.index) );
4344 static void move_cursor_to_display_row_down (GtkSText *text)
4349 GtkSPropertyMark mark;
4350 fdrf data = { FALSE, FALSE };
4352 fetch_lines_forward(text, 1);
4353 mark = find_this_line_start_mark(text, text->cursor_mark.index, &text->cursor_mark);
4354 lp = CACHE_DATA(text->current_line);
4355 col = (text->cursor_mark.index - lp.start.index);
4357 data.start = lp.start.index;
4358 data.end = lp.end.index;
4360 /* find the next DISPLAY line */
4361 XDEBUG( ("%s(%d) - FW iterating", __FILE__, __LINE__) ) ;
4362 line_params_iterate(text, &mark, NULL, FALSE, &data,
4363 (LineIteratorFunction)forward_display_row_fetcher);
4364 XDEBUG( ("%s(%d) - FW done iterating", __FILE__, __LINE__) );
4366 if (data.completed) {
4367 if (col < text->persist_column) col = text->persist_column;
4368 else text->persist_column = col;
4369 new_index = data.lp.start.index + col;
4370 if (new_index > data.lp.end.index) {
4371 new_index = data.lp.end.index;
4373 /* and move the cursor */
4374 gtk_stext_set_position_X(GTK_EDITABLE(text), new_index);
4378 static void reset_persist_col_pos(GtkSText *text)
4380 text->persist_column = 0;
4384 gtk_stext_move_cursor (GtkEditable *editable,
4391 move_cursor_hor (GTK_STEXT (editable), 1);
4396 move_cursor_hor (GTK_STEXT (editable), -1);
4402 move_cursor_ver (GTK_STEXT (editable), 1);
4407 move_cursor_ver (GTK_STEXT (editable), -1);
4412 gtk_stext_move_forward_character (GtkSText *text)
4414 move_cursor_hor (text, 1);
4418 gtk_stext_move_backward_character (GtkSText *text)
4420 move_cursor_hor (text, -1);
4424 gtk_stext_move_next_line (GtkSText *text)
4426 move_cursor_ver (text, 1);
4430 gtk_stext_move_previous_line (GtkSText *text)
4432 move_cursor_ver (text, -1);
4436 gtk_stext_move_word (GtkEditable *editable,
4442 gtk_stext_move_forward_word (GTK_STEXT (editable));
4447 gtk_stext_move_backward_word (GTK_STEXT (editable));
4452 gtk_stext_move_forward_word (GtkSText *text)
4454 text->cursor_virtual_x = 0;
4456 undraw_cursor (text, FALSE);
4458 if (text->use_wchar)
4460 while (!LAST_INDEX (text, text->cursor_mark) &&
4461 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4462 advance_mark (&text->cursor_mark);
4464 while (!LAST_INDEX (text, text->cursor_mark) &&
4465 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4466 advance_mark (&text->cursor_mark);
4470 while (!LAST_INDEX (text, text->cursor_mark) &&
4471 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4472 advance_mark (&text->cursor_mark);
4474 while (!LAST_INDEX (text, text->cursor_mark) &&
4475 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index)))
4476 advance_mark (&text->cursor_mark);
4479 find_cursor (text, TRUE);
4480 draw_cursor (text, FALSE);
4484 gtk_stext_move_backward_word (GtkSText *text)
4486 text->cursor_virtual_x = 0;
4488 undraw_cursor (text, FALSE);
4490 if (text->use_wchar)
4492 while ((text->cursor_mark.index > 0) &&
4493 !gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4494 decrement_mark (&text->cursor_mark);
4496 while ((text->cursor_mark.index > 0) &&
4497 gdk_iswalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4498 decrement_mark (&text->cursor_mark);
4502 while ((text->cursor_mark.index > 0) &&
4503 !isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4504 decrement_mark (&text->cursor_mark);
4506 while ((text->cursor_mark.index > 0) &&
4507 isalnum (GTK_STEXT_INDEX(text, text->cursor_mark.index-1)))
4508 decrement_mark (&text->cursor_mark);
4511 find_cursor (text, TRUE);
4512 draw_cursor (text, FALSE);
4516 gtk_stext_move_page (GtkEditable *editable,
4521 scroll_int (GTK_STEXT (editable),
4522 y * GTK_STEXT(editable)->vadj->page_increment);
4526 gtk_stext_move_to_row (GtkEditable *editable,
4532 gtk_stext_move_to_column (GtkEditable *editable,